Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 1 | #include <linux/module.h> |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 2 | #include <linux/buffer_head.h> |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 3 | #include <linux/fs.h> |
| 4 | #include <linux/pagemap.h> |
| 5 | #include <linux/highmem.h> |
| 6 | #include <linux/time.h> |
| 7 | #include <linux/init.h> |
| 8 | #include <linux/string.h> |
| 9 | #include <linux/smp_lock.h> |
| 10 | #include <linux/backing-dev.h> |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 11 | #include <linux/mpage.h> |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 12 | #include <linux/swap.h> |
| 13 | #include <linux/writeback.h> |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 14 | #include "ctree.h" |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 15 | #include "disk-io.h" |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 16 | #include "transaction.h" |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 17 | #include "btrfs_inode.h" |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 18 | #include "ioctl.h" |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 19 | |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 20 | void btrfs_fsinfo_release(struct kobject *obj) |
| 21 | { |
| 22 | struct btrfs_fs_info *fsinfo = container_of(obj, |
| 23 | struct btrfs_fs_info, kobj); |
| 24 | kfree(fsinfo); |
| 25 | } |
| 26 | |
| 27 | struct kobj_type btrfs_fsinfo_ktype = { |
| 28 | .release = btrfs_fsinfo_release, |
| 29 | }; |
| 30 | |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 31 | struct btrfs_iget_args { |
| 32 | u64 ino; |
| 33 | struct btrfs_root *root; |
| 34 | }; |
| 35 | |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 36 | decl_subsys(btrfs, &btrfs_fsinfo_ktype, NULL); |
| 37 | |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 38 | #define BTRFS_SUPER_MAGIC 0x9123682E |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 39 | |
| 40 | static struct inode_operations btrfs_dir_inode_operations; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 41 | static struct inode_operations btrfs_dir_ro_inode_operations; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 42 | static struct super_operations btrfs_super_ops; |
| 43 | static struct file_operations btrfs_dir_file_operations; |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 44 | static struct inode_operations btrfs_file_inode_operations; |
| 45 | static struct address_space_operations btrfs_aops; |
| 46 | static struct file_operations btrfs_file_operations; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 47 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 48 | static void btrfs_read_locked_inode(struct inode *inode) |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 49 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 50 | struct btrfs_path *path; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 51 | struct btrfs_inode_item *inode_item; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 52 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 53 | struct btrfs_key location; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 54 | int ret; |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 55 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 56 | path = btrfs_alloc_path(); |
| 57 | BUG_ON(!path); |
| 58 | btrfs_init_path(path); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 59 | mutex_lock(&root->fs_info->fs_mutex); |
| 60 | |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 61 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
| 62 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 63 | if (ret) { |
Chris Mason | 7cfcc17 | 2007-04-02 14:53:59 -0400 | [diff] [blame] | 64 | btrfs_free_path(path); |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 65 | goto make_bad; |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 66 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 67 | inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), |
| 68 | path->slots[0], |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 69 | struct btrfs_inode_item); |
| 70 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 71 | inode->i_mode = btrfs_inode_mode(inode_item); |
| 72 | inode->i_nlink = btrfs_inode_nlink(inode_item); |
| 73 | inode->i_uid = btrfs_inode_uid(inode_item); |
| 74 | inode->i_gid = btrfs_inode_gid(inode_item); |
| 75 | inode->i_size = btrfs_inode_size(inode_item); |
| 76 | inode->i_atime.tv_sec = btrfs_timespec_sec(&inode_item->atime); |
| 77 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(&inode_item->atime); |
| 78 | inode->i_mtime.tv_sec = btrfs_timespec_sec(&inode_item->mtime); |
| 79 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(&inode_item->mtime); |
| 80 | inode->i_ctime.tv_sec = btrfs_timespec_sec(&inode_item->ctime); |
| 81 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(&inode_item->ctime); |
| 82 | inode->i_blocks = btrfs_inode_nblocks(inode_item); |
| 83 | inode->i_generation = btrfs_inode_generation(inode_item); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 84 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 85 | btrfs_free_path(path); |
| 86 | inode_item = NULL; |
| 87 | |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 88 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | 1b05da2 | 2007-04-10 12:13:09 -0400 | [diff] [blame] | 89 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 90 | switch (inode->i_mode & S_IFMT) { |
| 91 | #if 0 |
| 92 | default: |
| 93 | init_special_inode(inode, inode->i_mode, |
| 94 | btrfs_inode_rdev(inode_item)); |
| 95 | break; |
| 96 | #endif |
| 97 | case S_IFREG: |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 98 | inode->i_mapping->a_ops = &btrfs_aops; |
| 99 | inode->i_fop = &btrfs_file_operations; |
| 100 | inode->i_op = &btrfs_file_inode_operations; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 101 | break; |
| 102 | case S_IFDIR: |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 103 | inode->i_fop = &btrfs_dir_file_operations; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 104 | if (root == root->fs_info->tree_root) |
| 105 | inode->i_op = &btrfs_dir_ro_inode_operations; |
| 106 | else |
| 107 | inode->i_op = &btrfs_dir_inode_operations; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 108 | break; |
| 109 | case S_IFLNK: |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 110 | // inode->i_op = &page_symlink_inode_operations; |
| 111 | break; |
| 112 | } |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 113 | return; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 114 | |
| 115 | make_bad: |
| 116 | btrfs_release_path(root, path); |
| 117 | btrfs_free_path(path); |
| 118 | mutex_unlock(&root->fs_info->fs_mutex); |
| 119 | make_bad_inode(inode); |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 120 | } |
| 121 | |
Chris Mason | 5f443fd | 2007-03-27 13:42:32 -0400 | [diff] [blame] | 122 | static int btrfs_unlink_trans(struct btrfs_trans_handle *trans, |
| 123 | struct btrfs_root *root, |
| 124 | struct inode *dir, |
| 125 | struct dentry *dentry) |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 126 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 127 | struct btrfs_path *path; |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 128 | const char *name = dentry->d_name.name; |
| 129 | int name_len = dentry->d_name.len; |
| 130 | int ret; |
| 131 | u64 objectid; |
| 132 | struct btrfs_dir_item *di; |
| 133 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 134 | path = btrfs_alloc_path(); |
| 135 | BUG_ON(!path); |
| 136 | btrfs_init_path(path); |
| 137 | ret = btrfs_lookup_dir_item(trans, root, path, dir->i_ino, |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 138 | name, name_len, -1); |
| 139 | if (ret < 0) |
| 140 | goto err; |
| 141 | if (ret > 0) { |
| 142 | ret = -ENOENT; |
| 143 | goto err; |
| 144 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 145 | di = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0], |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 146 | struct btrfs_dir_item); |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 147 | objectid = btrfs_disk_key_objectid(&di->location); |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 148 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 149 | ret = btrfs_del_item(trans, root, path); |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 150 | BUG_ON(ret); |
Chris Mason | 5f26f77 | 2007-04-05 10:38:44 -0400 | [diff] [blame] | 151 | |
| 152 | btrfs_release_path(root, path); |
| 153 | ret = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, |
| 154 | objectid, -1); |
| 155 | BUG_ON(ret); |
| 156 | ret = btrfs_del_item(trans, root, path); |
| 157 | BUG_ON(ret); |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 158 | dentry->d_inode->i_ctime = dir->i_ctime; |
| 159 | err: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 160 | btrfs_release_path(root, path); |
| 161 | btrfs_free_path(path); |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 162 | if (ret == 0) { |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 163 | inode_dec_link_count(dentry->d_inode); |
Chris Mason | 5f26f77 | 2007-04-05 10:38:44 -0400 | [diff] [blame] | 164 | dir->i_size -= name_len * 2; |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 165 | mark_inode_dirty(dir); |
| 166 | } |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 167 | return ret; |
| 168 | } |
| 169 | |
Chris Mason | 5f443fd | 2007-03-27 13:42:32 -0400 | [diff] [blame] | 170 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) |
| 171 | { |
| 172 | struct btrfs_root *root; |
| 173 | struct btrfs_trans_handle *trans; |
| 174 | int ret; |
| 175 | |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 176 | root = BTRFS_I(dir)->root; |
Chris Mason | 5f443fd | 2007-03-27 13:42:32 -0400 | [diff] [blame] | 177 | mutex_lock(&root->fs_info->fs_mutex); |
| 178 | trans = btrfs_start_transaction(root, 1); |
| 179 | ret = btrfs_unlink_trans(trans, root, dir, dentry); |
| 180 | btrfs_end_transaction(trans, root); |
| 181 | mutex_unlock(&root->fs_info->fs_mutex); |
| 182 | return ret; |
| 183 | } |
| 184 | |
| 185 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 186 | { |
| 187 | struct inode *inode = dentry->d_inode; |
| 188 | int err; |
| 189 | int ret; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 190 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 191 | struct btrfs_path *path; |
Chris Mason | 5f443fd | 2007-03-27 13:42:32 -0400 | [diff] [blame] | 192 | struct btrfs_key key; |
| 193 | struct btrfs_trans_handle *trans; |
Chris Mason | 5f26f77 | 2007-04-05 10:38:44 -0400 | [diff] [blame] | 194 | struct btrfs_key found_key; |
| 195 | int found_type; |
Chris Mason | 5f443fd | 2007-03-27 13:42:32 -0400 | [diff] [blame] | 196 | struct btrfs_leaf *leaf; |
Chris Mason | 5f26f77 | 2007-04-05 10:38:44 -0400 | [diff] [blame] | 197 | char *goodnames = ".."; |
Chris Mason | 5f443fd | 2007-03-27 13:42:32 -0400 | [diff] [blame] | 198 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 199 | path = btrfs_alloc_path(); |
| 200 | BUG_ON(!path); |
| 201 | btrfs_init_path(path); |
Chris Mason | 5f443fd | 2007-03-27 13:42:32 -0400 | [diff] [blame] | 202 | mutex_lock(&root->fs_info->fs_mutex); |
| 203 | trans = btrfs_start_transaction(root, 1); |
| 204 | key.objectid = inode->i_ino; |
| 205 | key.offset = (u64)-1; |
Chris Mason | 5f26f77 | 2007-04-05 10:38:44 -0400 | [diff] [blame] | 206 | key.flags = (u32)-1; |
| 207 | while(1) { |
| 208 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 209 | if (ret < 0) { |
| 210 | err = ret; |
| 211 | goto out; |
| 212 | } |
| 213 | BUG_ON(ret == 0); |
| 214 | if (path->slots[0] == 0) { |
| 215 | err = -ENOENT; |
| 216 | goto out; |
| 217 | } |
| 218 | path->slots[0]--; |
| 219 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
| 220 | btrfs_disk_key_to_cpu(&found_key, |
| 221 | &leaf->items[path->slots[0]].key); |
| 222 | found_type = btrfs_key_type(&found_key); |
| 223 | if (found_key.objectid != inode->i_ino) { |
| 224 | err = -ENOENT; |
| 225 | goto out; |
| 226 | } |
| 227 | if ((found_type != BTRFS_DIR_ITEM_KEY && |
| 228 | found_type != BTRFS_DIR_INDEX_KEY) || |
| 229 | (!btrfs_match_dir_item_name(root, path, goodnames, 2) && |
| 230 | !btrfs_match_dir_item_name(root, path, goodnames, 1))) { |
| 231 | err = -ENOTEMPTY; |
| 232 | goto out; |
| 233 | } |
| 234 | ret = btrfs_del_item(trans, root, path); |
| 235 | BUG_ON(ret); |
Chris Mason | 5f443fd | 2007-03-27 13:42:32 -0400 | [diff] [blame] | 236 | |
Chris Mason | 5f26f77 | 2007-04-05 10:38:44 -0400 | [diff] [blame] | 237 | if (found_type == BTRFS_DIR_ITEM_KEY && found_key.offset == 1) |
| 238 | break; |
| 239 | btrfs_release_path(root, path); |
Chris Mason | 5f443fd | 2007-03-27 13:42:32 -0400 | [diff] [blame] | 240 | } |
Chris Mason | 5f26f77 | 2007-04-05 10:38:44 -0400 | [diff] [blame] | 241 | ret = 0; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 242 | btrfs_release_path(root, path); |
Chris Mason | 5f443fd | 2007-03-27 13:42:32 -0400 | [diff] [blame] | 243 | |
| 244 | /* now the directory is empty */ |
| 245 | err = btrfs_unlink_trans(trans, root, dir, dentry); |
| 246 | if (!err) { |
| 247 | inode->i_size = 0; |
| 248 | } |
| 249 | out: |
Chris Mason | 7cfcc17 | 2007-04-02 14:53:59 -0400 | [diff] [blame] | 250 | btrfs_release_path(root, path); |
| 251 | btrfs_free_path(path); |
Chris Mason | 5f443fd | 2007-03-27 13:42:32 -0400 | [diff] [blame] | 252 | mutex_unlock(&root->fs_info->fs_mutex); |
| 253 | ret = btrfs_end_transaction(trans, root); |
| 254 | if (ret && !err) |
| 255 | err = ret; |
| 256 | return err; |
| 257 | } |
| 258 | |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 259 | static int btrfs_free_inode(struct btrfs_trans_handle *trans, |
| 260 | struct btrfs_root *root, |
| 261 | struct inode *inode) |
| 262 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 263 | struct btrfs_path *path; |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 264 | int ret; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 265 | |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 266 | clear_inode(inode); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 267 | |
| 268 | path = btrfs_alloc_path(); |
| 269 | BUG_ON(!path); |
| 270 | btrfs_init_path(path); |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 271 | ret = btrfs_lookup_inode(trans, root, path, |
| 272 | &BTRFS_I(inode)->location, -1); |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 273 | BUG_ON(ret); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 274 | ret = btrfs_del_item(trans, root, path); |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 275 | BUG_ON(ret); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 276 | btrfs_free_path(path); |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 277 | return ret; |
| 278 | } |
| 279 | |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 280 | static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans, |
| 281 | struct btrfs_root *root, |
| 282 | struct inode *inode) |
| 283 | { |
| 284 | int ret; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 285 | struct btrfs_path *path; |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 286 | struct btrfs_key key; |
| 287 | struct btrfs_disk_key *found_key; |
| 288 | struct btrfs_leaf *leaf; |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 289 | struct btrfs_file_extent_item *fi = NULL; |
| 290 | u64 extent_start = 0; |
| 291 | u64 extent_num_blocks = 0; |
| 292 | int found_extent; |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 293 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 294 | path = btrfs_alloc_path(); |
| 295 | BUG_ON(!path); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 296 | /* FIXME, add redo link to tree so we don't leak on crash */ |
| 297 | key.objectid = inode->i_ino; |
| 298 | key.offset = (u64)-1; |
| 299 | key.flags = 0; |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 300 | /* |
| 301 | * use BTRFS_CSUM_ITEM_KEY because it is larger than inline keys |
| 302 | * or extent data |
| 303 | */ |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 304 | btrfs_set_key_type(&key, BTRFS_CSUM_ITEM_KEY); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 305 | while(1) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 306 | btrfs_init_path(path); |
| 307 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 308 | if (ret < 0) { |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 309 | goto error; |
| 310 | } |
| 311 | if (ret > 0) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 312 | BUG_ON(path->slots[0] == 0); |
| 313 | path->slots[0]--; |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 314 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 315 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
| 316 | found_key = &leaf->items[path->slots[0]].key; |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 317 | if (btrfs_disk_key_objectid(found_key) != inode->i_ino) |
| 318 | break; |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 319 | if (btrfs_disk_key_type(found_key) != BTRFS_CSUM_ITEM_KEY && |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 320 | btrfs_disk_key_type(found_key) != BTRFS_INLINE_DATA_KEY && |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 321 | btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY) |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 322 | break; |
| 323 | if (btrfs_disk_key_offset(found_key) < inode->i_size) |
| 324 | break; |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 325 | if (btrfs_disk_key_type(found_key) == BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 326 | fi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), |
| 327 | path->slots[0], |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 328 | struct btrfs_file_extent_item); |
| 329 | extent_start = btrfs_file_extent_disk_blocknr(fi); |
| 330 | extent_num_blocks = |
| 331 | btrfs_file_extent_disk_num_blocks(fi); |
| 332 | inode->i_blocks -= |
| 333 | btrfs_file_extent_num_blocks(fi) >> 9; |
| 334 | found_extent = 1; |
| 335 | } else { |
| 336 | found_extent = 0; |
| 337 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 338 | ret = btrfs_del_item(trans, root, path); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 339 | BUG_ON(ret); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 340 | btrfs_release_path(root, path); |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 341 | if (found_extent) { |
| 342 | ret = btrfs_free_extent(trans, root, extent_start, |
| 343 | extent_num_blocks, 0); |
| 344 | BUG_ON(ret); |
| 345 | } |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 346 | } |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 347 | ret = 0; |
| 348 | error: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 349 | btrfs_release_path(root, path); |
| 350 | btrfs_free_path(path); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 351 | return ret; |
| 352 | } |
| 353 | |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 354 | static void btrfs_delete_inode(struct inode *inode) |
| 355 | { |
| 356 | struct btrfs_trans_handle *trans; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 357 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 358 | int ret; |
| 359 | |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 360 | truncate_inode_pages(&inode->i_data, 0); |
| 361 | if (is_bad_inode(inode)) { |
| 362 | goto no_delete; |
| 363 | } |
| 364 | inode->i_size = 0; |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 365 | mutex_lock(&root->fs_info->fs_mutex); |
| 366 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 367 | if (S_ISREG(inode->i_mode)) { |
| 368 | ret = btrfs_truncate_in_trans(trans, root, inode); |
| 369 | BUG_ON(ret); |
| 370 | } |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 371 | btrfs_free_inode(trans, root, inode); |
| 372 | btrfs_end_transaction(trans, root); |
| 373 | mutex_unlock(&root->fs_info->fs_mutex); |
| 374 | return; |
| 375 | no_delete: |
| 376 | clear_inode(inode); |
| 377 | } |
| 378 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 379 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 380 | struct btrfs_key *location) |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 381 | { |
| 382 | const char *name = dentry->d_name.name; |
| 383 | int namelen = dentry->d_name.len; |
| 384 | struct btrfs_dir_item *di; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 385 | struct btrfs_path *path; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 386 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 387 | int ret; |
| 388 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 389 | path = btrfs_alloc_path(); |
| 390 | BUG_ON(!path); |
| 391 | btrfs_init_path(path); |
| 392 | ret = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 393 | namelen, 0); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 394 | if (ret || !btrfs_match_dir_item_name(root, path, name, namelen)) { |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 395 | location->objectid = 0; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 396 | ret = 0; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 397 | goto out; |
| 398 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 399 | di = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0], |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 400 | struct btrfs_dir_item); |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 401 | btrfs_disk_key_to_cpu(location, &di->location); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 402 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 403 | btrfs_release_path(root, path); |
| 404 | btrfs_free_path(path); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 405 | return ret; |
| 406 | } |
| 407 | |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 408 | int fixup_tree_root_location(struct btrfs_root *root, |
| 409 | struct btrfs_key *location, |
| 410 | struct btrfs_root **sub_root) |
| 411 | { |
| 412 | struct btrfs_path *path; |
| 413 | struct btrfs_root_item *ri; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 414 | |
| 415 | if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY) |
| 416 | return 0; |
| 417 | if (location->objectid == BTRFS_ROOT_TREE_OBJECTID) |
| 418 | return 0; |
| 419 | |
| 420 | path = btrfs_alloc_path(); |
| 421 | BUG_ON(!path); |
| 422 | mutex_lock(&root->fs_info->fs_mutex); |
| 423 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 424 | *sub_root = btrfs_read_fs_root(root->fs_info, location); |
| 425 | if (IS_ERR(*sub_root)) |
| 426 | return PTR_ERR(*sub_root); |
| 427 | |
| 428 | ri = &(*sub_root)->root_item; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 429 | location->objectid = btrfs_root_dirid(ri); |
| 430 | location->flags = 0; |
| 431 | btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); |
| 432 | location->offset = 0; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 433 | |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 434 | btrfs_free_path(path); |
| 435 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 436 | return 0; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 437 | } |
| 438 | |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 439 | int btrfs_init_locked_inode(struct inode *inode, void *p) |
| 440 | { |
| 441 | struct btrfs_iget_args *args = p; |
| 442 | inode->i_ino = args->ino; |
| 443 | BTRFS_I(inode)->root = args->root; |
| 444 | return 0; |
| 445 | } |
| 446 | |
| 447 | int btrfs_find_actor(struct inode *inode, void *opaque) |
| 448 | { |
| 449 | struct btrfs_iget_args *args = opaque; |
| 450 | return (args->ino == inode->i_ino && |
| 451 | args->root == BTRFS_I(inode)->root); |
| 452 | } |
| 453 | |
| 454 | struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid, |
| 455 | struct btrfs_root *root) |
| 456 | { |
| 457 | struct inode *inode; |
| 458 | struct btrfs_iget_args args; |
| 459 | args.ino = objectid; |
| 460 | args.root = root; |
| 461 | |
| 462 | inode = iget5_locked(s, objectid, btrfs_find_actor, |
| 463 | btrfs_init_locked_inode, |
| 464 | (void *)&args); |
| 465 | return inode; |
| 466 | } |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 467 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 468 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
| 469 | struct nameidata *nd) |
| 470 | { |
| 471 | struct inode * inode; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 472 | struct btrfs_inode *bi = BTRFS_I(dir); |
| 473 | struct btrfs_root *root = bi->root; |
| 474 | struct btrfs_root *sub_root = root; |
| 475 | struct btrfs_key location; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 476 | int ret; |
| 477 | |
| 478 | if (dentry->d_name.len > BTRFS_NAME_LEN) |
| 479 | return ERR_PTR(-ENAMETOOLONG); |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 480 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 481 | ret = btrfs_inode_by_name(dir, dentry, &location); |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 482 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 483 | if (ret < 0) |
| 484 | return ERR_PTR(ret); |
| 485 | inode = NULL; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 486 | if (location.objectid) { |
| 487 | ret = fixup_tree_root_location(root, &location, &sub_root); |
| 488 | if (ret < 0) |
| 489 | return ERR_PTR(ret); |
| 490 | if (ret > 0) |
| 491 | return ERR_PTR(-ENOENT); |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 492 | inode = btrfs_iget_locked(dir->i_sb, location.objectid, |
| 493 | sub_root); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 494 | if (!inode) |
| 495 | return ERR_PTR(-EACCES); |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 496 | if (inode->i_state & I_NEW) { |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 497 | if (sub_root != root) { |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 498 | printk("adding new root for inode %lu root %p (found %p)\n", inode->i_ino, sub_root, BTRFS_I(inode)->root); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 499 | igrab(inode); |
| 500 | sub_root->inode = inode; |
| 501 | } |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 502 | BTRFS_I(inode)->root = sub_root; |
| 503 | memcpy(&BTRFS_I(inode)->location, &location, |
| 504 | sizeof(location)); |
| 505 | btrfs_read_locked_inode(inode); |
| 506 | unlock_new_inode(inode); |
| 507 | } |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 508 | } |
| 509 | return d_splice_alias(inode, dentry); |
| 510 | } |
| 511 | |
| 512 | static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir) |
| 513 | { |
| 514 | struct inode *inode = filp->f_path.dentry->d_inode; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 515 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 516 | struct btrfs_item *item; |
| 517 | struct btrfs_dir_item *di; |
| 518 | struct btrfs_key key; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 519 | struct btrfs_path *path; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 520 | int ret; |
| 521 | u32 nritems; |
| 522 | struct btrfs_leaf *leaf; |
| 523 | int slot; |
| 524 | int advance; |
| 525 | unsigned char d_type = DT_UNKNOWN; |
Chris Mason | 7f5c151 | 2007-03-23 15:56:19 -0400 | [diff] [blame] | 526 | int over = 0; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 527 | int key_type = BTRFS_DIR_INDEX_KEY; |
| 528 | |
| 529 | /* FIXME, use a real flag for deciding about the key type */ |
| 530 | if (root->fs_info->tree_root == root) |
| 531 | key_type = BTRFS_DIR_ITEM_KEY; |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 532 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 533 | key.objectid = inode->i_ino; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 534 | key.flags = 0; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 535 | btrfs_set_key_type(&key, key_type); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 536 | key.offset = filp->f_pos; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 537 | path = btrfs_alloc_path(); |
| 538 | btrfs_init_path(path); |
| 539 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Chris Mason | 1b05da2 | 2007-04-10 12:13:09 -0400 | [diff] [blame] | 540 | if (ret < 0) |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 541 | goto err; |
Chris Mason | 7f5c151 | 2007-03-23 15:56:19 -0400 | [diff] [blame] | 542 | advance = 0; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 543 | while(1) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 544 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 545 | nritems = btrfs_header_nritems(&leaf->header); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 546 | slot = path->slots[0]; |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 547 | if (advance || slot >= nritems) { |
| 548 | if (slot >= nritems -1) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 549 | ret = btrfs_next_leaf(root, path); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 550 | if (ret) |
| 551 | break; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 552 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 553 | nritems = btrfs_header_nritems(&leaf->header); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 554 | slot = path->slots[0]; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 555 | } else { |
| 556 | slot++; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 557 | path->slots[0]++; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 558 | } |
| 559 | } |
| 560 | advance = 1; |
| 561 | item = leaf->items + slot; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 562 | if (btrfs_disk_key_objectid(&item->key) != key.objectid) |
| 563 | break; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 564 | if (key_type == BTRFS_DIR_INDEX_KEY && |
Chris Mason | 1b05da2 | 2007-04-10 12:13:09 -0400 | [diff] [blame] | 565 | btrfs_disk_key_offset(&item->key) > root->highest_inode) |
Chris Mason | 5be6f7f | 2007-04-05 13:35:25 -0400 | [diff] [blame] | 566 | break; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 567 | if (btrfs_disk_key_type(&item->key) != key_type) |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 568 | continue; |
Chris Mason | 7f5c151 | 2007-03-23 15:56:19 -0400 | [diff] [blame] | 569 | if (btrfs_disk_key_offset(&item->key) < filp->f_pos) |
| 570 | continue; |
Chris Mason | 7fcde0e | 2007-04-05 12:13:21 -0400 | [diff] [blame] | 571 | filp->f_pos = btrfs_disk_key_offset(&item->key); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 572 | advance = 1; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 573 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 574 | over = filldir(dirent, (const char *)(di + 1), |
| 575 | btrfs_dir_name_len(di), |
| 576 | btrfs_disk_key_offset(&item->key), |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 577 | btrfs_disk_key_objectid(&di->location), d_type); |
Chris Mason | 7fcde0e | 2007-04-05 12:13:21 -0400 | [diff] [blame] | 578 | if (over) |
| 579 | goto nopos; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 580 | } |
Chris Mason | 7fcde0e | 2007-04-05 12:13:21 -0400 | [diff] [blame] | 581 | filp->f_pos++; |
| 582 | nopos: |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 583 | ret = 0; |
| 584 | err: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 585 | btrfs_release_path(root, path); |
| 586 | btrfs_free_path(path); |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 587 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 588 | return ret; |
| 589 | } |
| 590 | |
| 591 | static void btrfs_put_super (struct super_block * sb) |
| 592 | { |
| 593 | struct btrfs_root *root = btrfs_sb(sb); |
| 594 | int ret; |
| 595 | |
| 596 | ret = close_ctree(root); |
| 597 | if (ret) { |
| 598 | printk("close ctree returns %d\n", ret); |
| 599 | } |
| 600 | sb->s_fs_info = NULL; |
| 601 | } |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 602 | |
| 603 | static int btrfs_fill_super(struct super_block * sb, void * data, int silent) |
| 604 | { |
| 605 | struct inode * inode; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 606 | struct dentry * root_dentry; |
| 607 | struct btrfs_super_block *disk_super; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 608 | struct btrfs_root *tree_root; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 609 | struct btrfs_inode *bi; |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 610 | |
| 611 | sb->s_maxbytes = MAX_LFS_FILESIZE; |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 612 | sb->s_magic = BTRFS_SUPER_MAGIC; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 613 | sb->s_op = &btrfs_super_ops; |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 614 | sb->s_time_gran = 1; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 615 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 616 | tree_root = open_ctree(sb); |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 617 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 618 | if (!tree_root) { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 619 | printk("btrfs: open_ctree failed\n"); |
| 620 | return -EIO; |
| 621 | } |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 622 | sb->s_fs_info = tree_root; |
| 623 | disk_super = tree_root->fs_info->disk_super; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 624 | printk("read in super total blocks %Lu root %Lu\n", |
| 625 | btrfs_super_total_blocks(disk_super), |
| 626 | btrfs_super_root_dir(disk_super)); |
| 627 | |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 628 | inode = btrfs_iget_locked(sb, btrfs_super_root_dir(disk_super), |
| 629 | tree_root); |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 630 | bi = BTRFS_I(inode); |
| 631 | bi->location.objectid = inode->i_ino; |
| 632 | bi->location.offset = 0; |
| 633 | bi->location.flags = 0; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 634 | bi->root = tree_root; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 635 | btrfs_set_key_type(&bi->location, BTRFS_INODE_ITEM_KEY); |
| 636 | |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 637 | if (!inode) |
| 638 | return -ENOMEM; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 639 | if (inode->i_state & I_NEW) { |
| 640 | btrfs_read_locked_inode(inode); |
| 641 | unlock_new_inode(inode); |
| 642 | } |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 643 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 644 | root_dentry = d_alloc_root(inode); |
| 645 | if (!root_dentry) { |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 646 | iput(inode); |
| 647 | return -ENOMEM; |
| 648 | } |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 649 | sb->s_root = root_dentry; |
| 650 | |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 651 | return 0; |
| 652 | } |
| 653 | |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 654 | static void fill_inode_item(struct btrfs_inode_item *item, |
| 655 | struct inode *inode) |
| 656 | { |
| 657 | btrfs_set_inode_uid(item, inode->i_uid); |
| 658 | btrfs_set_inode_gid(item, inode->i_gid); |
| 659 | btrfs_set_inode_size(item, inode->i_size); |
| 660 | btrfs_set_inode_mode(item, inode->i_mode); |
| 661 | btrfs_set_inode_nlink(item, inode->i_nlink); |
| 662 | btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec); |
| 663 | btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec); |
| 664 | btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec); |
| 665 | btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec); |
| 666 | btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec); |
| 667 | btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec); |
| 668 | btrfs_set_inode_nblocks(item, inode->i_blocks); |
| 669 | btrfs_set_inode_generation(item, inode->i_generation); |
| 670 | } |
| 671 | |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 672 | static int btrfs_update_inode(struct btrfs_trans_handle *trans, |
| 673 | struct btrfs_root *root, |
| 674 | struct inode *inode) |
| 675 | { |
| 676 | struct btrfs_inode_item *inode_item; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 677 | struct btrfs_path *path; |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 678 | int ret; |
| 679 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 680 | path = btrfs_alloc_path(); |
| 681 | BUG_ON(!path); |
| 682 | btrfs_init_path(path); |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 683 | ret = btrfs_lookup_inode(trans, root, path, |
| 684 | &BTRFS_I(inode)->location, 1); |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 685 | if (ret) { |
| 686 | if (ret > 0) |
| 687 | ret = -ENOENT; |
| 688 | goto failed; |
| 689 | } |
| 690 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 691 | inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), |
| 692 | path->slots[0], |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 693 | struct btrfs_inode_item); |
| 694 | |
| 695 | fill_inode_item(inode_item, inode); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 696 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | 1b05da2 | 2007-04-10 12:13:09 -0400 | [diff] [blame] | 697 | ret = 0; |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 698 | failed: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 699 | btrfs_release_path(root, path); |
| 700 | btrfs_free_path(path); |
Chris Mason | 1b05da2 | 2007-04-10 12:13:09 -0400 | [diff] [blame] | 701 | return ret; |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 702 | } |
| 703 | |
| 704 | static int btrfs_write_inode(struct inode *inode, int wait) |
| 705 | { |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 706 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 707 | struct btrfs_trans_handle *trans; |
| 708 | int ret; |
| 709 | |
| 710 | mutex_lock(&root->fs_info->fs_mutex); |
| 711 | trans = btrfs_start_transaction(root, 1); |
| 712 | ret = btrfs_update_inode(trans, root, inode); |
| 713 | if (wait) |
| 714 | btrfs_commit_transaction(trans, root); |
| 715 | else |
| 716 | btrfs_end_transaction(trans, root); |
| 717 | mutex_unlock(&root->fs_info->fs_mutex); |
| 718 | return ret; |
| 719 | } |
| 720 | |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 721 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 722 | struct btrfs_root *root, |
| 723 | u64 objectid, int mode) |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 724 | { |
| 725 | struct inode *inode; |
| 726 | struct btrfs_inode_item inode_item; |
Chris Mason | 1b05da2 | 2007-04-10 12:13:09 -0400 | [diff] [blame] | 727 | struct btrfs_key *location; |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 728 | int ret; |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 729 | |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 730 | inode = new_inode(root->fs_info->sb); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 731 | if (!inode) |
| 732 | return ERR_PTR(-ENOMEM); |
| 733 | |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 734 | BTRFS_I(inode)->root = root; |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 735 | |
| 736 | inode->i_uid = current->fsuid; |
| 737 | inode->i_gid = current->fsgid; |
| 738 | inode->i_mode = mode; |
| 739 | inode->i_ino = objectid; |
| 740 | inode->i_blocks = 0; |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 741 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 742 | fill_inode_item(&inode_item, inode); |
Chris Mason | 1b05da2 | 2007-04-10 12:13:09 -0400 | [diff] [blame] | 743 | location = &BTRFS_I(inode)->location; |
| 744 | location->objectid = objectid; |
| 745 | location->flags = 0; |
| 746 | location->offset = 0; |
| 747 | btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 748 | |
| 749 | ret = btrfs_insert_inode(trans, root, objectid, &inode_item); |
| 750 | BUG_ON(ret); |
| 751 | |
| 752 | insert_inode_hash(inode); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 753 | return inode; |
| 754 | } |
| 755 | |
| 756 | static int btrfs_add_link(struct btrfs_trans_handle *trans, |
| 757 | struct dentry *dentry, struct inode *inode) |
| 758 | { |
| 759 | int ret; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 760 | struct btrfs_key key; |
| 761 | struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root; |
| 762 | key.objectid = inode->i_ino; |
| 763 | key.flags = 0; |
| 764 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); |
| 765 | key.offset = 0; |
| 766 | |
| 767 | ret = btrfs_insert_dir_item(trans, root, |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 768 | dentry->d_name.name, dentry->d_name.len, |
| 769 | dentry->d_parent->d_inode->i_ino, |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 770 | &key, 0); |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 771 | if (ret == 0) { |
Chris Mason | 5f26f77 | 2007-04-05 10:38:44 -0400 | [diff] [blame] | 772 | dentry->d_parent->d_inode->i_size += dentry->d_name.len * 2; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 773 | ret = btrfs_update_inode(trans, root, |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 774 | dentry->d_parent->d_inode); |
| 775 | } |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 776 | return ret; |
| 777 | } |
| 778 | |
| 779 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, |
| 780 | struct dentry *dentry, struct inode *inode) |
| 781 | { |
| 782 | int err = btrfs_add_link(trans, dentry, inode); |
| 783 | if (!err) { |
| 784 | d_instantiate(dentry, inode); |
| 785 | return 0; |
| 786 | } |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 787 | if (err > 0) |
| 788 | err = -EEXIST; |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 789 | return err; |
| 790 | } |
| 791 | |
| 792 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
| 793 | int mode, struct nameidata *nd) |
| 794 | { |
| 795 | struct btrfs_trans_handle *trans; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 796 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 797 | struct inode *inode; |
| 798 | int err; |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 799 | int drop_inode = 0; |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 800 | u64 objectid; |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 801 | |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 802 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 803 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 804 | |
| 805 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); |
| 806 | if (err) { |
| 807 | err = -ENOSPC; |
| 808 | goto out_unlock; |
| 809 | } |
| 810 | |
| 811 | inode = btrfs_new_inode(trans, root, objectid, mode); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 812 | err = PTR_ERR(inode); |
| 813 | if (IS_ERR(inode)) |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 814 | goto out_unlock; |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 815 | // FIXME mark the inode dirty |
| 816 | err = btrfs_add_nondir(trans, dentry, inode); |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 817 | if (err) |
| 818 | drop_inode = 1; |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 819 | else { |
| 820 | inode->i_mapping->a_ops = &btrfs_aops; |
| 821 | inode->i_fop = &btrfs_file_operations; |
| 822 | inode->i_op = &btrfs_file_inode_operations; |
| 823 | } |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 824 | dir->i_sb->s_dirt = 1; |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 825 | out_unlock: |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 826 | btrfs_end_transaction(trans, root); |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 827 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 828 | |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 829 | if (drop_inode) { |
| 830 | inode_dec_link_count(inode); |
| 831 | iput(inode); |
| 832 | } |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 833 | return err; |
| 834 | } |
| 835 | |
Chris Mason | f792203 | 2007-03-25 20:17:36 -0400 | [diff] [blame] | 836 | static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans, |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 837 | struct btrfs_root *root, |
| 838 | u64 objectid, u64 dirid) |
Chris Mason | f792203 | 2007-03-25 20:17:36 -0400 | [diff] [blame] | 839 | { |
Chris Mason | f792203 | 2007-03-25 20:17:36 -0400 | [diff] [blame] | 840 | int ret; |
| 841 | char buf[2]; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 842 | struct btrfs_key key; |
| 843 | |
Chris Mason | f792203 | 2007-03-25 20:17:36 -0400 | [diff] [blame] | 844 | buf[0] = '.'; |
| 845 | buf[1] = '.'; |
| 846 | |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 847 | key.objectid = objectid; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 848 | key.offset = 0; |
| 849 | key.flags = 0; |
| 850 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); |
| 851 | |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 852 | ret = btrfs_insert_dir_item(trans, root, buf, 1, objectid, |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 853 | &key, 1); |
Chris Mason | f792203 | 2007-03-25 20:17:36 -0400 | [diff] [blame] | 854 | if (ret) |
| 855 | goto error; |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 856 | key.objectid = dirid; |
| 857 | ret = btrfs_insert_dir_item(trans, root, buf, 2, objectid, |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 858 | &key, 1); |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 859 | if (ret) |
| 860 | goto error; |
Chris Mason | f792203 | 2007-03-25 20:17:36 -0400 | [diff] [blame] | 861 | error: |
| 862 | return ret; |
| 863 | } |
| 864 | |
| 865 | static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) |
| 866 | { |
| 867 | struct inode *inode; |
| 868 | struct btrfs_trans_handle *trans; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 869 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | f792203 | 2007-03-25 20:17:36 -0400 | [diff] [blame] | 870 | int err = 0; |
| 871 | int drop_on_err = 0; |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 872 | u64 objectid; |
Chris Mason | f792203 | 2007-03-25 20:17:36 -0400 | [diff] [blame] | 873 | |
| 874 | mutex_lock(&root->fs_info->fs_mutex); |
| 875 | trans = btrfs_start_transaction(root, 1); |
| 876 | if (IS_ERR(trans)) { |
| 877 | err = PTR_ERR(trans); |
| 878 | goto out_unlock; |
| 879 | } |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 880 | |
| 881 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); |
| 882 | if (err) { |
| 883 | err = -ENOSPC; |
| 884 | goto out_unlock; |
| 885 | } |
| 886 | |
| 887 | inode = btrfs_new_inode(trans, root, objectid, S_IFDIR | mode); |
Chris Mason | f792203 | 2007-03-25 20:17:36 -0400 | [diff] [blame] | 888 | if (IS_ERR(inode)) { |
| 889 | err = PTR_ERR(inode); |
| 890 | goto out_fail; |
| 891 | } |
| 892 | drop_on_err = 1; |
| 893 | inode->i_op = &btrfs_dir_inode_operations; |
| 894 | inode->i_fop = &btrfs_dir_file_operations; |
| 895 | |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 896 | err = btrfs_make_empty_dir(trans, root, inode->i_ino, dir->i_ino); |
| 897 | if (err) |
| 898 | goto out_fail; |
| 899 | |
| 900 | inode->i_size = 6; |
| 901 | err = btrfs_update_inode(trans, root, inode); |
Chris Mason | f792203 | 2007-03-25 20:17:36 -0400 | [diff] [blame] | 902 | if (err) |
| 903 | goto out_fail; |
| 904 | err = btrfs_add_link(trans, dentry, inode); |
| 905 | if (err) |
| 906 | goto out_fail; |
| 907 | d_instantiate(dentry, inode); |
Chris Mason | f792203 | 2007-03-25 20:17:36 -0400 | [diff] [blame] | 908 | drop_on_err = 0; |
| 909 | |
| 910 | out_fail: |
| 911 | btrfs_end_transaction(trans, root); |
| 912 | out_unlock: |
| 913 | mutex_unlock(&root->fs_info->fs_mutex); |
| 914 | if (drop_on_err) |
| 915 | iput(inode); |
| 916 | return err; |
| 917 | } |
| 918 | |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 919 | static int btrfs_sync_fs(struct super_block *sb, int wait) |
| 920 | { |
| 921 | struct btrfs_trans_handle *trans; |
| 922 | struct btrfs_root *root; |
| 923 | int ret; |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 924 | root = btrfs_sb(sb); |
Chris Mason | df2ce34 | 2007-03-23 11:00:45 -0400 | [diff] [blame] | 925 | |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 926 | sb->s_dirt = 0; |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 927 | if (!wait) { |
Chris Mason | 7cfcc17 | 2007-04-02 14:53:59 -0400 | [diff] [blame] | 928 | filemap_flush(root->fs_info->btree_inode->i_mapping); |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 929 | return 0; |
| 930 | } |
Chris Mason | 7cfcc17 | 2007-04-02 14:53:59 -0400 | [diff] [blame] | 931 | filemap_write_and_wait(root->fs_info->btree_inode->i_mapping); |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 932 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 933 | trans = btrfs_start_transaction(root, 1); |
| 934 | ret = btrfs_commit_transaction(trans, root); |
| 935 | sb->s_dirt = 0; |
| 936 | BUG_ON(ret); |
| 937 | printk("btrfs sync_fs\n"); |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 938 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 939 | return 0; |
| 940 | } |
| 941 | |
Chris Mason | e8f05c4 | 2007-04-04 14:30:09 -0400 | [diff] [blame] | 942 | #if 0 |
Chris Mason | df24a2b | 2007-04-04 09:36:31 -0400 | [diff] [blame] | 943 | static int btrfs_get_block_inline(struct inode *inode, sector_t iblock, |
| 944 | struct buffer_head *result, int create) |
| 945 | { |
| 946 | struct btrfs_root *root = btrfs_sb(inode->i_sb); |
| 947 | struct btrfs_path *path; |
| 948 | struct btrfs_key key; |
| 949 | struct btrfs_leaf *leaf; |
| 950 | int num_bytes = result->b_size; |
| 951 | int item_size; |
| 952 | int ret; |
| 953 | u64 pos; |
| 954 | char *ptr; |
| 955 | int copy_size; |
| 956 | int err = 0; |
| 957 | char *safe_ptr; |
| 958 | char *data_ptr; |
| 959 | |
| 960 | path = btrfs_alloc_path(); |
| 961 | BUG_ON(!path); |
| 962 | |
| 963 | WARN_ON(create); |
| 964 | if (create) { |
| 965 | return 0; |
| 966 | } |
| 967 | pos = iblock << inode->i_blkbits; |
| 968 | key.objectid = inode->i_ino; |
| 969 | key.flags = 0; |
| 970 | btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY); |
| 971 | ptr = kmap(result->b_page); |
| 972 | safe_ptr = ptr; |
| 973 | ptr += (pos & (PAGE_CACHE_SIZE -1)); |
| 974 | again: |
| 975 | key.offset = pos; |
| 976 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 977 | if (ret) { |
| 978 | if (ret < 0) |
| 979 | err = ret; |
| 980 | else |
| 981 | err = 0; |
| 982 | goto out; |
| 983 | } |
| 984 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
| 985 | item_size = btrfs_item_size(leaf->items + path->slots[0]); |
| 986 | copy_size = min(num_bytes, item_size); |
| 987 | data_ptr = btrfs_item_ptr(leaf, path->slots[0], char); |
| 988 | WARN_ON(safe_ptr + PAGE_CACHE_SIZE < ptr + copy_size); |
| 989 | memcpy(ptr, data_ptr, copy_size); |
| 990 | pos += copy_size; |
| 991 | num_bytes -= copy_size; |
| 992 | WARN_ON(num_bytes < 0); |
| 993 | ptr += copy_size; |
| 994 | btrfs_release_path(root, path); |
| 995 | if (num_bytes != 0) { |
| 996 | if (pos >= i_size_read(inode)) |
| 997 | memset(ptr, 0, num_bytes); |
| 998 | else |
| 999 | goto again; |
| 1000 | } |
| 1001 | set_buffer_uptodate(result); |
| 1002 | map_bh(result, inode->i_sb, 0); |
| 1003 | err = 0; |
| 1004 | out: |
| 1005 | btrfs_free_path(path); |
| 1006 | kunmap(result->b_page); |
| 1007 | return err; |
| 1008 | } |
Chris Mason | e8f05c4 | 2007-04-04 14:30:09 -0400 | [diff] [blame] | 1009 | #endif |
Chris Mason | df24a2b | 2007-04-04 09:36:31 -0400 | [diff] [blame] | 1010 | |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1011 | static int btrfs_get_block_lock(struct inode *inode, sector_t iblock, |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1012 | struct buffer_head *result, int create) |
| 1013 | { |
| 1014 | int ret; |
| 1015 | int err = 0; |
| 1016 | u64 blocknr; |
| 1017 | u64 extent_start = 0; |
| 1018 | u64 extent_end = 0; |
| 1019 | u64 objectid = inode->i_ino; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1020 | struct btrfs_path *path; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 1021 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1022 | struct btrfs_trans_handle *trans = NULL; |
| 1023 | struct btrfs_file_extent_item *item; |
| 1024 | struct btrfs_leaf *leaf; |
| 1025 | struct btrfs_disk_key *found_key; |
| 1026 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1027 | path = btrfs_alloc_path(); |
| 1028 | BUG_ON(!path); |
| 1029 | btrfs_init_path(path); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame^] | 1030 | if (create) { |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1031 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame^] | 1032 | WARN_ON(1); |
| 1033 | } |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1034 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1035 | ret = btrfs_lookup_file_extent(trans, root, path, |
Chris Mason | 9773a78 | 2007-03-27 11:26:26 -0400 | [diff] [blame] | 1036 | inode->i_ino, |
Chris Mason | 79b2cb1 | 2007-04-10 13:51:41 -0400 | [diff] [blame] | 1037 | iblock << inode->i_blkbits, create); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1038 | if (ret < 0) { |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1039 | err = ret; |
| 1040 | goto out; |
| 1041 | } |
| 1042 | |
| 1043 | if (ret != 0) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1044 | if (path->slots[0] == 0) { |
| 1045 | btrfs_release_path(root, path); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1046 | goto allocate; |
| 1047 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1048 | path->slots[0]--; |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1049 | } |
| 1050 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1051 | item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0], |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1052 | struct btrfs_file_extent_item); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1053 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1054 | blocknr = btrfs_file_extent_disk_blocknr(item); |
| 1055 | blocknr += btrfs_file_extent_offset(item); |
| 1056 | |
Chris Mason | 48ddc6f | 2007-04-10 13:20:21 -0400 | [diff] [blame] | 1057 | /* exact match found, use it, FIXME, deal with extents |
| 1058 | * other than the page size |
| 1059 | */ |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame^] | 1060 | if (0 && ret == 0) { |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1061 | err = 0; |
Chris Mason | 79b2cb1 | 2007-04-10 13:51:41 -0400 | [diff] [blame] | 1062 | if (create && |
| 1063 | btrfs_file_extent_generation(item) != trans->transid) { |
Chris Mason | 48ddc6f | 2007-04-10 13:20:21 -0400 | [diff] [blame] | 1064 | struct btrfs_key ins; |
| 1065 | ret = btrfs_alloc_extent(trans, root, 1, |
| 1066 | blocknr, (u64)-1, &ins); |
| 1067 | BUG_ON(ret); |
| 1068 | btrfs_set_file_extent_disk_blocknr(item, ins.objectid); |
| 1069 | mark_buffer_dirty(path->nodes[0]); |
| 1070 | ret = btrfs_free_extent(trans, root, |
| 1071 | blocknr, 1, 0); |
| 1072 | BUG_ON(ret); |
| 1073 | blocknr = ins.objectid; |
| 1074 | |
| 1075 | } |
Chris Mason | 8352d8a | 2007-04-12 10:43:05 -0400 | [diff] [blame] | 1076 | btrfs_map_bh_to_logical(root, result, blocknr); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1077 | goto out; |
| 1078 | } |
| 1079 | |
| 1080 | /* are we inside the extent that was found? */ |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1081 | found_key = &leaf->items[path->slots[0]].key; |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1082 | if (btrfs_disk_key_objectid(found_key) != objectid || |
| 1083 | btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY) { |
| 1084 | extent_end = 0; |
| 1085 | extent_start = 0; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1086 | btrfs_release_path(root, path); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1087 | goto allocate; |
| 1088 | } |
| 1089 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1090 | extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key); |
Chris Mason | 9773a78 | 2007-03-27 11:26:26 -0400 | [diff] [blame] | 1091 | extent_start = extent_start >> inode->i_blkbits; |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1092 | extent_start += btrfs_file_extent_offset(item); |
| 1093 | extent_end = extent_start + btrfs_file_extent_num_blocks(item); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1094 | if (iblock >= extent_start && iblock < extent_end) { |
| 1095 | err = 0; |
Chris Mason | 8352d8a | 2007-04-12 10:43:05 -0400 | [diff] [blame] | 1096 | btrfs_map_bh_to_logical(root, result, blocknr + iblock - |
| 1097 | extent_start); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1098 | goto out; |
| 1099 | } |
| 1100 | allocate: |
| 1101 | /* ok, create a new extent */ |
| 1102 | if (!create) { |
| 1103 | err = 0; |
| 1104 | goto out; |
| 1105 | } |
Chris Mason | 9773a78 | 2007-03-27 11:26:26 -0400 | [diff] [blame] | 1106 | ret = btrfs_alloc_file_extent(trans, root, objectid, |
| 1107 | iblock << inode->i_blkbits, |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1108 | 1, extent_end, &blocknr); |
| 1109 | if (ret) { |
| 1110 | err = ret; |
| 1111 | goto out; |
| 1112 | } |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 1113 | inode->i_blocks += inode->i_sb->s_blocksize >> 9; |
| 1114 | set_buffer_new(result); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1115 | map_bh(result, inode->i_sb, blocknr); |
| 1116 | |
Chris Mason | 8352d8a | 2007-04-12 10:43:05 -0400 | [diff] [blame] | 1117 | btrfs_map_bh_to_logical(root, result, blocknr); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1118 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1119 | btrfs_release_path(root, path); |
| 1120 | btrfs_free_path(path); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1121 | if (trans) |
| 1122 | btrfs_end_transaction(trans, root); |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1123 | return err; |
| 1124 | } |
| 1125 | |
| 1126 | static int btrfs_get_block(struct inode *inode, sector_t iblock, |
| 1127 | struct buffer_head *result, int create) |
| 1128 | { |
| 1129 | int err; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 1130 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1131 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | e8f05c4 | 2007-04-04 14:30:09 -0400 | [diff] [blame] | 1132 | err = btrfs_get_block_lock(inode, iblock, result, create); |
| 1133 | // err = btrfs_get_block_inline(inode, iblock, result, create); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1134 | mutex_unlock(&root->fs_info->fs_mutex); |
| 1135 | return err; |
| 1136 | } |
| 1137 | |
| 1138 | static int btrfs_prepare_write(struct file *file, struct page *page, |
| 1139 | unsigned from, unsigned to) |
| 1140 | { |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 1141 | return nobh_prepare_write(page, from, to, btrfs_get_block); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1142 | } |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1143 | static int btrfs_commit_write(struct file *file, struct page *page, |
| 1144 | unsigned from, unsigned to) |
| 1145 | { |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1146 | return nobh_commit_write(file, page, from, to); |
| 1147 | } |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1148 | |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 1149 | static void btrfs_write_super(struct super_block *sb) |
| 1150 | { |
| 1151 | btrfs_sync_fs(sb, 1); |
| 1152 | } |
| 1153 | |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1154 | static int btrfs_readpage(struct file *file, struct page *page) |
| 1155 | { |
| 1156 | return mpage_readpage(page, btrfs_get_block); |
| 1157 | } |
| 1158 | |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1159 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
| 1160 | { |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 1161 | return nobh_writepage(page, btrfs_get_block, wbc); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1162 | } |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 1163 | |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 1164 | static void btrfs_truncate(struct inode *inode) |
| 1165 | { |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 1166 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 1167 | int ret; |
| 1168 | struct btrfs_trans_handle *trans; |
| 1169 | |
| 1170 | if (!S_ISREG(inode->i_mode)) |
| 1171 | return; |
| 1172 | if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) |
| 1173 | return; |
| 1174 | |
Chris Mason | e8f05c4 | 2007-04-04 14:30:09 -0400 | [diff] [blame] | 1175 | nobh_truncate_page(inode->i_mapping, inode->i_size); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 1176 | |
| 1177 | /* FIXME, add redo link to tree so we don't leak on crash */ |
| 1178 | mutex_lock(&root->fs_info->fs_mutex); |
| 1179 | trans = btrfs_start_transaction(root, 1); |
| 1180 | ret = btrfs_truncate_in_trans(trans, root, inode); |
| 1181 | BUG_ON(ret); |
| 1182 | ret = btrfs_end_transaction(trans, root); |
| 1183 | BUG_ON(ret); |
| 1184 | mutex_unlock(&root->fs_info->fs_mutex); |
| 1185 | mark_inode_dirty(inode); |
| 1186 | } |
| 1187 | |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1188 | static int btrfs_copy_from_user(loff_t pos, int num_pages, int write_bytes, |
| 1189 | struct page **prepared_pages, |
| 1190 | const char __user * buf) |
| 1191 | { |
| 1192 | long page_fault = 0; |
| 1193 | int i; |
| 1194 | int offset = pos & (PAGE_CACHE_SIZE - 1); |
| 1195 | |
| 1196 | for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) { |
| 1197 | size_t count = min_t(size_t, |
| 1198 | PAGE_CACHE_SIZE - offset, write_bytes); |
| 1199 | struct page *page = prepared_pages[i]; |
| 1200 | fault_in_pages_readable(buf, count); |
| 1201 | |
| 1202 | /* Copy data from userspace to the current page */ |
| 1203 | kmap(page); |
| 1204 | page_fault = __copy_from_user(page_address(page) + offset, |
| 1205 | buf, count); |
| 1206 | /* Flush processor's dcache for this page */ |
| 1207 | flush_dcache_page(page); |
| 1208 | kunmap(page); |
| 1209 | buf += count; |
| 1210 | write_bytes -= count; |
| 1211 | |
| 1212 | if (page_fault) |
| 1213 | break; |
| 1214 | } |
| 1215 | return page_fault ? -EFAULT : 0; |
| 1216 | } |
| 1217 | |
| 1218 | static void btrfs_drop_pages(struct page **pages, size_t num_pages) |
| 1219 | { |
| 1220 | size_t i; |
| 1221 | for (i = 0; i < num_pages; i++) { |
| 1222 | if (!pages[i]) |
| 1223 | break; |
| 1224 | unlock_page(pages[i]); |
| 1225 | mark_page_accessed(pages[i]); |
| 1226 | page_cache_release(pages[i]); |
| 1227 | } |
| 1228 | } |
| 1229 | static int dirty_and_release_pages(struct btrfs_trans_handle *trans, |
| 1230 | struct btrfs_root *root, |
| 1231 | struct file *file, |
| 1232 | struct page **pages, |
| 1233 | size_t num_pages, |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame^] | 1234 | u64 extent_offset, |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1235 | loff_t pos, |
| 1236 | size_t write_bytes) |
| 1237 | { |
| 1238 | int i; |
| 1239 | int offset; |
| 1240 | int err = 0; |
| 1241 | int ret; |
| 1242 | int this_write; |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 1243 | struct inode *inode = file->f_path.dentry->d_inode; |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1244 | |
| 1245 | for (i = 0; i < num_pages; i++) { |
| 1246 | offset = pos & (PAGE_CACHE_SIZE -1); |
| 1247 | this_write = min(PAGE_CACHE_SIZE - offset, write_bytes); |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 1248 | /* FIXME, one block at a time */ |
| 1249 | |
| 1250 | mutex_lock(&root->fs_info->fs_mutex); |
| 1251 | trans = btrfs_start_transaction(root, 1); |
| 1252 | btrfs_csum_file_block(trans, root, inode->i_ino, |
| 1253 | pages[i]->index << PAGE_CACHE_SHIFT, |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame^] | 1254 | extent_offset, |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 1255 | kmap(pages[i]), PAGE_CACHE_SIZE); |
| 1256 | kunmap(pages[i]); |
| 1257 | SetPageChecked(pages[i]); |
| 1258 | ret = btrfs_end_transaction(trans, root); |
| 1259 | BUG_ON(ret); |
| 1260 | mutex_unlock(&root->fs_info->fs_mutex); |
| 1261 | |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1262 | ret = nobh_commit_write(file, pages[i], offset, |
| 1263 | offset + this_write); |
| 1264 | pos += this_write; |
| 1265 | if (ret) { |
| 1266 | err = ret; |
| 1267 | goto failed; |
| 1268 | } |
| 1269 | WARN_ON(this_write > write_bytes); |
| 1270 | write_bytes -= this_write; |
| 1271 | } |
| 1272 | failed: |
| 1273 | return err; |
| 1274 | } |
| 1275 | |
| 1276 | static int prepare_pages(struct btrfs_trans_handle *trans, |
| 1277 | struct btrfs_root *root, |
| 1278 | struct file *file, |
| 1279 | struct page **pages, |
| 1280 | size_t num_pages, |
| 1281 | loff_t pos, |
Chris Mason | 2932f3e | 2007-04-10 14:22:02 -0400 | [diff] [blame] | 1282 | unsigned long first_index, |
| 1283 | unsigned long last_index, |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame^] | 1284 | size_t write_bytes, |
| 1285 | u64 alloc_extent_start) |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1286 | { |
| 1287 | int i; |
| 1288 | unsigned long index = pos >> PAGE_CACHE_SHIFT; |
| 1289 | struct inode *inode = file->f_path.dentry->d_inode; |
| 1290 | int offset; |
| 1291 | int err = 0; |
| 1292 | int ret; |
| 1293 | int this_write; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame^] | 1294 | struct buffer_head *bh; |
| 1295 | struct buffer_head *head; |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1296 | loff_t isize = i_size_read(inode); |
| 1297 | |
| 1298 | memset(pages, 0, num_pages * sizeof(struct page *)); |
| 1299 | |
| 1300 | for (i = 0; i < num_pages; i++) { |
| 1301 | pages[i] = grab_cache_page(inode->i_mapping, index + i); |
| 1302 | if (!pages[i]) { |
| 1303 | err = -ENOMEM; |
| 1304 | goto failed_release; |
| 1305 | } |
| 1306 | offset = pos & (PAGE_CACHE_SIZE -1); |
| 1307 | this_write = min(PAGE_CACHE_SIZE - offset, write_bytes); |
Chris Mason | 2932f3e | 2007-04-10 14:22:02 -0400 | [diff] [blame] | 1308 | if (!PageUptodate(pages[i]) && |
| 1309 | (pages[i]->index == first_index || |
| 1310 | pages[i]->index == last_index) && pos < isize) { |
| 1311 | ret = mpage_readpage(pages[i], btrfs_get_block); |
| 1312 | BUG_ON(ret); |
| 1313 | lock_page(pages[i]); |
| 1314 | } |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame^] | 1315 | create_empty_buffers(pages[i], root->fs_info->sb->s_blocksize, |
| 1316 | (1 << BH_Uptodate)); |
| 1317 | head = page_buffers(pages[i]); |
| 1318 | bh = head; |
| 1319 | do { |
| 1320 | err = btrfs_map_bh_to_logical(root, bh, |
| 1321 | alloc_extent_start); |
| 1322 | BUG_ON(err); |
| 1323 | if (err) |
| 1324 | goto failed_truncate; |
| 1325 | bh = bh->b_this_page; |
| 1326 | alloc_extent_start++; |
| 1327 | } while (bh != head); |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1328 | pos += this_write; |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1329 | WARN_ON(this_write > write_bytes); |
| 1330 | write_bytes -= this_write; |
| 1331 | } |
| 1332 | return 0; |
| 1333 | |
| 1334 | failed_release: |
| 1335 | btrfs_drop_pages(pages, num_pages); |
| 1336 | return err; |
| 1337 | |
| 1338 | failed_truncate: |
| 1339 | btrfs_drop_pages(pages, num_pages); |
| 1340 | if (pos > isize) |
| 1341 | vmtruncate(inode, isize); |
| 1342 | return err; |
| 1343 | } |
| 1344 | |
| 1345 | static ssize_t btrfs_file_write(struct file *file, const char __user *buf, |
| 1346 | size_t count, loff_t *ppos) |
| 1347 | { |
| 1348 | loff_t pos; |
| 1349 | size_t num_written = 0; |
| 1350 | int err = 0; |
| 1351 | int ret = 0; |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1352 | struct inode *inode = file->f_path.dentry->d_inode; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 1353 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1354 | struct page *pages[1]; |
Chris Mason | 2932f3e | 2007-04-10 14:22:02 -0400 | [diff] [blame] | 1355 | unsigned long first_index; |
| 1356 | unsigned long last_index; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame^] | 1357 | u64 start_pos; |
| 1358 | u64 num_blocks; |
| 1359 | u64 alloc_extent_start; |
| 1360 | u64 orig_extent_start; |
| 1361 | struct btrfs_trans_handle *trans; |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1362 | |
| 1363 | if (file->f_flags & O_DIRECT) |
| 1364 | return -EINVAL; |
| 1365 | pos = *ppos; |
| 1366 | |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame^] | 1367 | start_pos = pos & ~(root->blocksize - 1); |
| 1368 | /* FIXME */ |
| 1369 | if (start_pos != pos) |
| 1370 | return -EINVAL; |
| 1371 | num_blocks = (count + pos - start_pos + root->blocksize - 1) >> |
| 1372 | inode->i_blkbits; |
| 1373 | |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1374 | vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE); |
| 1375 | current->backing_dev_info = inode->i_mapping->backing_dev_info; |
| 1376 | err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode)); |
| 1377 | if (err) |
| 1378 | goto out; |
| 1379 | if (count == 0) |
| 1380 | goto out; |
| 1381 | err = remove_suid(file->f_path.dentry); |
| 1382 | if (err) |
| 1383 | goto out; |
| 1384 | file_update_time(file); |
| 1385 | mutex_lock(&inode->i_mutex); |
Chris Mason | 2932f3e | 2007-04-10 14:22:02 -0400 | [diff] [blame] | 1386 | first_index = pos >> PAGE_CACHE_SHIFT; |
| 1387 | last_index = (pos + count) >> PAGE_CACHE_SHIFT; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame^] | 1388 | |
| 1389 | mutex_lock(&root->fs_info->fs_mutex); |
| 1390 | trans = btrfs_start_transaction(root, 1); |
| 1391 | if (!trans) { |
| 1392 | err = -ENOMEM; |
| 1393 | goto out_unlock; |
| 1394 | } |
| 1395 | ret = btrfs_alloc_file_extent(trans, root, inode->i_ino, |
| 1396 | start_pos, num_blocks, 1, |
| 1397 | &alloc_extent_start); |
| 1398 | BUG_ON(ret); |
| 1399 | |
| 1400 | orig_extent_start = start_pos; |
| 1401 | ret = btrfs_end_transaction(trans, root); |
| 1402 | BUG_ON(ret); |
| 1403 | mutex_unlock(&root->fs_info->fs_mutex); |
| 1404 | |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1405 | while(count > 0) { |
| 1406 | size_t offset = pos & (PAGE_CACHE_SIZE - 1); |
| 1407 | size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset); |
| 1408 | size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >> |
| 1409 | PAGE_CACHE_SHIFT; |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 1410 | ret = prepare_pages(NULL, root, file, pages, num_pages, |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame^] | 1411 | pos, first_index, last_index, |
| 1412 | write_bytes, alloc_extent_start); |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1413 | BUG_ON(ret); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame^] | 1414 | /* FIXME blocks != pagesize */ |
| 1415 | alloc_extent_start += num_pages; |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1416 | ret = btrfs_copy_from_user(pos, num_pages, |
| 1417 | write_bytes, pages, buf); |
| 1418 | BUG_ON(ret); |
| 1419 | |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 1420 | ret = dirty_and_release_pages(NULL, root, file, pages, |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame^] | 1421 | num_pages, orig_extent_start, |
| 1422 | pos, write_bytes); |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1423 | BUG_ON(ret); |
| 1424 | btrfs_drop_pages(pages, num_pages); |
| 1425 | |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1426 | buf += write_bytes; |
| 1427 | count -= write_bytes; |
| 1428 | pos += write_bytes; |
| 1429 | num_written += write_bytes; |
| 1430 | |
| 1431 | balance_dirty_pages_ratelimited(inode->i_mapping); |
| 1432 | cond_resched(); |
| 1433 | } |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame^] | 1434 | out_unlock: |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1435 | mutex_unlock(&inode->i_mutex); |
| 1436 | out: |
| 1437 | *ppos = pos; |
| 1438 | current->backing_dev_info = NULL; |
| 1439 | return num_written ? num_written : err; |
| 1440 | } |
| 1441 | |
Chris Mason | e8f05c4 | 2007-04-04 14:30:09 -0400 | [diff] [blame] | 1442 | #if 0 |
Chris Mason | df24a2b | 2007-04-04 09:36:31 -0400 | [diff] [blame] | 1443 | static ssize_t inline_one_page(struct btrfs_root *root, struct inode *inode, |
| 1444 | struct page *page, loff_t pos, |
| 1445 | size_t offset, size_t write_bytes) |
| 1446 | { |
| 1447 | struct btrfs_path *path; |
| 1448 | struct btrfs_trans_handle *trans; |
| 1449 | struct btrfs_key key; |
| 1450 | struct btrfs_leaf *leaf; |
| 1451 | struct btrfs_key found_key; |
| 1452 | int ret; |
| 1453 | size_t copy_size = 0; |
| 1454 | char *dst = NULL; |
| 1455 | int err = 0; |
| 1456 | size_t num_written = 0; |
| 1457 | |
| 1458 | path = btrfs_alloc_path(); |
| 1459 | BUG_ON(!path); |
| 1460 | mutex_lock(&root->fs_info->fs_mutex); |
| 1461 | trans = btrfs_start_transaction(root, 1); |
| 1462 | key.objectid = inode->i_ino; |
| 1463 | key.flags = 0; |
| 1464 | btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY); |
| 1465 | |
| 1466 | again: |
| 1467 | key.offset = pos; |
| 1468 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 1469 | if (ret < 0) { |
| 1470 | err = ret; |
| 1471 | goto out; |
| 1472 | } |
| 1473 | if (ret == 0) { |
| 1474 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
| 1475 | btrfs_disk_key_to_cpu(&found_key, |
| 1476 | &leaf->items[path->slots[0]].key); |
| 1477 | copy_size = btrfs_item_size(leaf->items + path->slots[0]); |
| 1478 | dst = btrfs_item_ptr(leaf, path->slots[0], char); |
| 1479 | copy_size = min(write_bytes, copy_size); |
| 1480 | goto copyit; |
| 1481 | } else { |
| 1482 | int slot = path->slots[0]; |
| 1483 | if (slot > 0) { |
| 1484 | slot--; |
| 1485 | } |
| 1486 | // FIXME find max key |
| 1487 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
| 1488 | btrfs_disk_key_to_cpu(&found_key, |
| 1489 | &leaf->items[slot].key); |
| 1490 | if (found_key.objectid != inode->i_ino) |
| 1491 | goto insert; |
| 1492 | if (btrfs_key_type(&found_key) != BTRFS_INLINE_DATA_KEY) |
| 1493 | goto insert; |
| 1494 | copy_size = btrfs_item_size(leaf->items + slot); |
| 1495 | if (found_key.offset + copy_size <= pos) |
| 1496 | goto insert; |
| 1497 | dst = btrfs_item_ptr(leaf, path->slots[0], char); |
| 1498 | dst += pos - found_key.offset; |
| 1499 | copy_size = copy_size - (pos - found_key.offset); |
| 1500 | BUG_ON(copy_size < 0); |
| 1501 | copy_size = min(write_bytes, copy_size); |
| 1502 | WARN_ON(copy_size == 0); |
| 1503 | goto copyit; |
| 1504 | } |
| 1505 | insert: |
| 1506 | btrfs_release_path(root, path); |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 1507 | copy_size = min(write_bytes, |
| 1508 | (size_t)BTRFS_LEAF_DATA_SIZE(root) - |
| 1509 | sizeof(struct btrfs_item) * 4); |
Chris Mason | df24a2b | 2007-04-04 09:36:31 -0400 | [diff] [blame] | 1510 | ret = btrfs_insert_empty_item(trans, root, path, &key, copy_size); |
| 1511 | BUG_ON(ret); |
| 1512 | dst = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), |
| 1513 | path->slots[0], char); |
| 1514 | copyit: |
| 1515 | WARN_ON(copy_size == 0); |
| 1516 | WARN_ON(dst + copy_size > |
| 1517 | btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), |
| 1518 | path->slots[0], char) + |
| 1519 | btrfs_item_size(btrfs_buffer_leaf(path->nodes[0])->items + |
| 1520 | path->slots[0])); |
| 1521 | btrfs_memcpy(root, path->nodes[0]->b_data, dst, |
| 1522 | page_address(page) + offset, copy_size); |
| 1523 | mark_buffer_dirty(path->nodes[0]); |
| 1524 | btrfs_release_path(root, path); |
| 1525 | pos += copy_size; |
| 1526 | offset += copy_size; |
| 1527 | num_written += copy_size; |
| 1528 | write_bytes -= copy_size; |
| 1529 | if (write_bytes) |
| 1530 | goto again; |
| 1531 | out: |
| 1532 | btrfs_free_path(path); |
| 1533 | ret = btrfs_end_transaction(trans, root); |
| 1534 | BUG_ON(ret); |
| 1535 | mutex_unlock(&root->fs_info->fs_mutex); |
| 1536 | return num_written ? num_written : err; |
| 1537 | } |
| 1538 | |
| 1539 | static ssize_t btrfs_file_inline_write(struct file *file, |
| 1540 | const char __user *buf, |
| 1541 | size_t count, loff_t *ppos) |
| 1542 | { |
| 1543 | loff_t pos; |
| 1544 | size_t num_written = 0; |
| 1545 | int err = 0; |
| 1546 | int ret = 0; |
| 1547 | struct inode *inode = file->f_path.dentry->d_inode; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 1548 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | df24a2b | 2007-04-04 09:36:31 -0400 | [diff] [blame] | 1549 | unsigned long page_index; |
| 1550 | |
| 1551 | if (file->f_flags & O_DIRECT) |
| 1552 | return -EINVAL; |
| 1553 | pos = *ppos; |
| 1554 | |
| 1555 | vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE); |
| 1556 | current->backing_dev_info = inode->i_mapping->backing_dev_info; |
| 1557 | err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode)); |
| 1558 | if (err) |
| 1559 | goto out; |
| 1560 | if (count == 0) |
| 1561 | goto out; |
| 1562 | err = remove_suid(file->f_path.dentry); |
| 1563 | if (err) |
| 1564 | goto out; |
| 1565 | file_update_time(file); |
| 1566 | mutex_lock(&inode->i_mutex); |
| 1567 | while(count > 0) { |
| 1568 | size_t offset = pos & (PAGE_CACHE_SIZE - 1); |
| 1569 | size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset); |
| 1570 | struct page *page; |
| 1571 | |
| 1572 | page_index = pos >> PAGE_CACHE_SHIFT; |
| 1573 | page = grab_cache_page(inode->i_mapping, page_index); |
| 1574 | if (!PageUptodate(page)) { |
| 1575 | ret = mpage_readpage(page, btrfs_get_block); |
| 1576 | BUG_ON(ret); |
| 1577 | lock_page(page); |
| 1578 | } |
| 1579 | ret = btrfs_copy_from_user(pos, 1, |
| 1580 | write_bytes, &page, buf); |
| 1581 | BUG_ON(ret); |
| 1582 | write_bytes = inline_one_page(root, inode, page, pos, |
| 1583 | offset, write_bytes); |
| 1584 | SetPageUptodate(page); |
| 1585 | if (write_bytes > 0 && pos + write_bytes > inode->i_size) { |
| 1586 | i_size_write(inode, pos + write_bytes); |
| 1587 | mark_inode_dirty(inode); |
| 1588 | } |
| 1589 | page_cache_release(page); |
| 1590 | unlock_page(page); |
| 1591 | if (write_bytes < 0) |
| 1592 | goto out_unlock; |
| 1593 | buf += write_bytes; |
| 1594 | count -= write_bytes; |
| 1595 | pos += write_bytes; |
| 1596 | num_written += write_bytes; |
| 1597 | |
| 1598 | balance_dirty_pages_ratelimited(inode->i_mapping); |
| 1599 | cond_resched(); |
| 1600 | } |
| 1601 | out_unlock: |
| 1602 | mutex_unlock(&inode->i_mutex); |
| 1603 | out: |
| 1604 | *ppos = pos; |
| 1605 | current->backing_dev_info = NULL; |
| 1606 | return num_written ? num_written : err; |
| 1607 | } |
Chris Mason | e8f05c4 | 2007-04-04 14:30:09 -0400 | [diff] [blame] | 1608 | #endif |
Chris Mason | df24a2b | 2007-04-04 09:36:31 -0400 | [diff] [blame] | 1609 | |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 1610 | static int btrfs_read_actor(read_descriptor_t *desc, struct page *page, |
| 1611 | unsigned long offset, unsigned long size) |
| 1612 | { |
| 1613 | char *kaddr; |
| 1614 | unsigned long left, count = desc->count; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 1615 | struct inode *inode = page->mapping->host; |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 1616 | |
| 1617 | if (size > count) |
| 1618 | size = count; |
| 1619 | |
| 1620 | if (!PageChecked(page)) { |
| 1621 | /* FIXME, do it per block */ |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 1622 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 1623 | int ret = btrfs_csum_verify_file_block(root, |
| 1624 | page->mapping->host->i_ino, |
| 1625 | page->index << PAGE_CACHE_SHIFT, |
| 1626 | kmap(page), PAGE_CACHE_SIZE); |
| 1627 | if (ret) { |
| 1628 | printk("failed to verify ino %lu page %lu\n", |
| 1629 | page->mapping->host->i_ino, |
| 1630 | page->index); |
| 1631 | memset(page_address(page), 0, PAGE_CACHE_SIZE); |
| 1632 | } |
| 1633 | SetPageChecked(page); |
| 1634 | kunmap(page); |
| 1635 | } |
| 1636 | /* |
| 1637 | * Faults on the destination of a read are common, so do it before |
| 1638 | * taking the kmap. |
| 1639 | */ |
| 1640 | if (!fault_in_pages_writeable(desc->arg.buf, size)) { |
| 1641 | kaddr = kmap_atomic(page, KM_USER0); |
| 1642 | left = __copy_to_user_inatomic(desc->arg.buf, |
| 1643 | kaddr + offset, size); |
| 1644 | kunmap_atomic(kaddr, KM_USER0); |
| 1645 | if (left == 0) |
| 1646 | goto success; |
| 1647 | } |
| 1648 | |
| 1649 | /* Do it the slow way */ |
| 1650 | kaddr = kmap(page); |
| 1651 | left = __copy_to_user(desc->arg.buf, kaddr + offset, size); |
| 1652 | kunmap(page); |
| 1653 | |
| 1654 | if (left) { |
| 1655 | size -= left; |
| 1656 | desc->error = -EFAULT; |
| 1657 | } |
| 1658 | success: |
| 1659 | desc->count = count - size; |
| 1660 | desc->written += size; |
| 1661 | desc->arg.buf += size; |
| 1662 | return size; |
| 1663 | } |
| 1664 | |
| 1665 | /** |
| 1666 | * btrfs_file_aio_read - filesystem read routine |
| 1667 | * @iocb: kernel I/O control block |
| 1668 | * @iov: io vector request |
| 1669 | * @nr_segs: number of segments in the iovec |
| 1670 | * @pos: current file position |
| 1671 | */ |
| 1672 | static ssize_t btrfs_file_aio_read(struct kiocb *iocb, const struct iovec *iov, |
| 1673 | unsigned long nr_segs, loff_t pos) |
| 1674 | { |
| 1675 | struct file *filp = iocb->ki_filp; |
| 1676 | ssize_t retval; |
| 1677 | unsigned long seg; |
| 1678 | size_t count; |
| 1679 | loff_t *ppos = &iocb->ki_pos; |
| 1680 | |
| 1681 | count = 0; |
| 1682 | for (seg = 0; seg < nr_segs; seg++) { |
| 1683 | const struct iovec *iv = &iov[seg]; |
| 1684 | |
| 1685 | /* |
| 1686 | * If any segment has a negative length, or the cumulative |
| 1687 | * length ever wraps negative then return -EINVAL. |
| 1688 | */ |
| 1689 | count += iv->iov_len; |
| 1690 | if (unlikely((ssize_t)(count|iv->iov_len) < 0)) |
| 1691 | return -EINVAL; |
| 1692 | if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len)) |
| 1693 | continue; |
| 1694 | if (seg == 0) |
| 1695 | return -EFAULT; |
| 1696 | nr_segs = seg; |
| 1697 | count -= iv->iov_len; /* This segment is no good */ |
| 1698 | break; |
| 1699 | } |
| 1700 | retval = 0; |
| 1701 | if (count) { |
| 1702 | for (seg = 0; seg < nr_segs; seg++) { |
| 1703 | read_descriptor_t desc; |
| 1704 | |
| 1705 | desc.written = 0; |
| 1706 | desc.arg.buf = iov[seg].iov_base; |
| 1707 | desc.count = iov[seg].iov_len; |
| 1708 | if (desc.count == 0) |
| 1709 | continue; |
| 1710 | desc.error = 0; |
| 1711 | do_generic_file_read(filp, ppos, &desc, |
| 1712 | btrfs_read_actor); |
| 1713 | retval += desc.written; |
| 1714 | if (desc.error) { |
| 1715 | retval = retval ?: desc.error; |
| 1716 | break; |
| 1717 | } |
| 1718 | } |
| 1719 | } |
| 1720 | return retval; |
| 1721 | } |
| 1722 | |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 1723 | static int create_subvol(struct btrfs_root *root, char *name, int namelen) |
| 1724 | { |
| 1725 | struct btrfs_trans_handle *trans; |
| 1726 | struct btrfs_key key; |
| 1727 | struct btrfs_root_item root_item; |
| 1728 | struct btrfs_inode_item *inode_item; |
| 1729 | struct buffer_head *subvol; |
| 1730 | struct btrfs_leaf *leaf; |
| 1731 | struct btrfs_root *new_root; |
| 1732 | struct inode *inode; |
| 1733 | int ret; |
| 1734 | u64 objectid; |
| 1735 | u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID; |
| 1736 | |
| 1737 | mutex_lock(&root->fs_info->fs_mutex); |
| 1738 | trans = btrfs_start_transaction(root, 1); |
| 1739 | BUG_ON(!trans); |
| 1740 | |
| 1741 | subvol = btrfs_alloc_free_block(trans, root); |
| 1742 | leaf = btrfs_buffer_leaf(subvol); |
| 1743 | btrfs_set_header_nritems(&leaf->header, 0); |
| 1744 | btrfs_set_header_level(&leaf->header, 0); |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 1745 | btrfs_set_header_blocknr(&leaf->header, bh_blocknr(subvol)); |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 1746 | btrfs_set_header_generation(&leaf->header, trans->transid); |
| 1747 | memcpy(leaf->header.fsid, root->fs_info->disk_super->fsid, |
| 1748 | sizeof(leaf->header.fsid)); |
| 1749 | |
| 1750 | inode_item = &root_item.inode; |
| 1751 | memset(inode_item, 0, sizeof(*inode_item)); |
| 1752 | btrfs_set_inode_generation(inode_item, 1); |
| 1753 | btrfs_set_inode_size(inode_item, 3); |
| 1754 | btrfs_set_inode_nlink(inode_item, 1); |
| 1755 | btrfs_set_inode_nblocks(inode_item, 1); |
| 1756 | btrfs_set_inode_mode(inode_item, S_IFDIR | 0755); |
| 1757 | |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 1758 | btrfs_set_root_blocknr(&root_item, bh_blocknr(subvol)); |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 1759 | btrfs_set_root_refs(&root_item, 1); |
| 1760 | |
| 1761 | mark_buffer_dirty(subvol); |
| 1762 | brelse(subvol); |
| 1763 | subvol = NULL; |
| 1764 | |
| 1765 | ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root, |
| 1766 | 0, &objectid); |
| 1767 | BUG_ON(ret); |
| 1768 | |
| 1769 | btrfs_set_root_dirid(&root_item, new_dirid); |
| 1770 | |
| 1771 | key.objectid = objectid; |
| 1772 | key.offset = 1; |
| 1773 | key.flags = 0; |
| 1774 | btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); |
| 1775 | ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, |
| 1776 | &root_item); |
| 1777 | BUG_ON(ret); |
| 1778 | |
| 1779 | /* |
| 1780 | * insert the directory item |
| 1781 | */ |
| 1782 | key.offset = (u64)-1; |
| 1783 | ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root, |
| 1784 | name, namelen, |
| 1785 | root->fs_info->sb->s_root->d_inode->i_ino, |
| 1786 | &key, 0); |
| 1787 | BUG_ON(ret); |
| 1788 | |
| 1789 | ret = btrfs_commit_transaction(trans, root); |
| 1790 | BUG_ON(ret); |
| 1791 | |
| 1792 | new_root = btrfs_read_fs_root(root->fs_info, &key); |
| 1793 | BUG_ON(!new_root); |
| 1794 | |
| 1795 | trans = btrfs_start_transaction(new_root, 1); |
| 1796 | BUG_ON(!trans); |
| 1797 | |
| 1798 | inode = btrfs_new_inode(trans, new_root, new_dirid, S_IFDIR | 0700); |
| 1799 | inode->i_op = &btrfs_dir_inode_operations; |
| 1800 | inode->i_fop = &btrfs_dir_file_operations; |
| 1801 | |
| 1802 | ret = btrfs_make_empty_dir(trans, new_root, new_dirid, new_dirid); |
| 1803 | BUG_ON(ret); |
| 1804 | |
| 1805 | inode->i_nlink = 1; |
| 1806 | inode->i_size = 6; |
| 1807 | ret = btrfs_update_inode(trans, new_root, inode); |
| 1808 | BUG_ON(ret); |
| 1809 | |
| 1810 | ret = btrfs_commit_transaction(trans, new_root); |
| 1811 | BUG_ON(ret); |
| 1812 | |
| 1813 | iput(inode); |
| 1814 | |
| 1815 | mutex_unlock(&root->fs_info->fs_mutex); |
| 1816 | return 0; |
| 1817 | } |
| 1818 | |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 1819 | static int create_snapshot(struct btrfs_root *root, char *name, int namelen) |
| 1820 | { |
| 1821 | struct btrfs_trans_handle *trans; |
| 1822 | struct btrfs_key key; |
| 1823 | struct btrfs_root_item new_root_item; |
| 1824 | int ret; |
| 1825 | u64 objectid; |
| 1826 | |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 1827 | if (!root->ref_cows) |
| 1828 | return -EINVAL; |
| 1829 | |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 1830 | mutex_lock(&root->fs_info->fs_mutex); |
| 1831 | trans = btrfs_start_transaction(root, 1); |
| 1832 | BUG_ON(!trans); |
| 1833 | |
| 1834 | ret = btrfs_update_inode(trans, root, root->inode); |
| 1835 | BUG_ON(ret); |
| 1836 | |
Chris Mason | 1b05da2 | 2007-04-10 12:13:09 -0400 | [diff] [blame] | 1837 | ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root, |
| 1838 | 0, &objectid); |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 1839 | BUG_ON(ret); |
| 1840 | |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 1841 | memcpy(&new_root_item, &root->root_item, |
| 1842 | sizeof(new_root_item)); |
| 1843 | |
| 1844 | key.objectid = objectid; |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 1845 | key.offset = 1; |
| 1846 | key.flags = 0; |
| 1847 | btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 1848 | btrfs_set_root_blocknr(&new_root_item, bh_blocknr(root->node)); |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 1849 | |
| 1850 | ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, |
| 1851 | &new_root_item); |
| 1852 | BUG_ON(ret); |
| 1853 | |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 1854 | /* |
| 1855 | * insert the directory item |
| 1856 | */ |
| 1857 | key.offset = (u64)-1; |
| 1858 | ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root, |
| 1859 | name, namelen, |
| 1860 | root->fs_info->sb->s_root->d_inode->i_ino, |
| 1861 | &key, 0); |
| 1862 | |
| 1863 | BUG_ON(ret); |
| 1864 | |
| 1865 | ret = btrfs_inc_root_ref(trans, root); |
| 1866 | BUG_ON(ret); |
| 1867 | |
| 1868 | ret = btrfs_commit_transaction(trans, root); |
| 1869 | BUG_ON(ret); |
| 1870 | mutex_unlock(&root->fs_info->fs_mutex); |
| 1871 | return 0; |
| 1872 | } |
| 1873 | |
Chris Mason | 8352d8a | 2007-04-12 10:43:05 -0400 | [diff] [blame] | 1874 | static int add_disk(struct btrfs_root *root, char *name, int namelen) |
| 1875 | { |
| 1876 | struct block_device *bdev; |
| 1877 | struct btrfs_path *path; |
| 1878 | struct super_block *sb = root->fs_info->sb; |
| 1879 | struct btrfs_root *dev_root = root->fs_info->dev_root; |
| 1880 | struct btrfs_trans_handle *trans; |
| 1881 | struct btrfs_device_item *dev_item; |
| 1882 | struct btrfs_key key; |
| 1883 | u16 item_size; |
| 1884 | u64 num_blocks; |
| 1885 | u64 new_blocks; |
Chris Mason | b4100d6 | 2007-04-12 12:14:00 -0400 | [diff] [blame] | 1886 | u64 device_id; |
Chris Mason | 8352d8a | 2007-04-12 10:43:05 -0400 | [diff] [blame] | 1887 | int ret; |
Chris Mason | b4100d6 | 2007-04-12 12:14:00 -0400 | [diff] [blame] | 1888 | |
Chris Mason | 8352d8a | 2007-04-12 10:43:05 -0400 | [diff] [blame] | 1889 | printk("adding disk %s\n", name); |
| 1890 | path = btrfs_alloc_path(); |
| 1891 | if (!path) |
| 1892 | return -ENOMEM; |
| 1893 | num_blocks = btrfs_super_total_blocks(root->fs_info->disk_super); |
| 1894 | bdev = open_bdev_excl(name, O_RDWR, sb); |
| 1895 | if (IS_ERR(bdev)) { |
| 1896 | ret = PTR_ERR(bdev); |
| 1897 | printk("open bdev excl failed ret %d\n", ret); |
| 1898 | goto out_nolock; |
| 1899 | } |
| 1900 | set_blocksize(bdev, sb->s_blocksize); |
| 1901 | new_blocks = bdev->bd_inode->i_size >> sb->s_blocksize_bits; |
| 1902 | key.objectid = num_blocks; |
| 1903 | key.offset = new_blocks; |
| 1904 | key.flags = 0; |
| 1905 | btrfs_set_key_type(&key, BTRFS_DEV_ITEM_KEY); |
| 1906 | |
| 1907 | mutex_lock(&dev_root->fs_info->fs_mutex); |
| 1908 | trans = btrfs_start_transaction(dev_root, 1); |
| 1909 | item_size = sizeof(*dev_item) + namelen; |
| 1910 | printk("insert empty on %Lu %Lu %u size %d\n", num_blocks, new_blocks, key.flags, item_size); |
| 1911 | ret = btrfs_insert_empty_item(trans, dev_root, path, &key, item_size); |
| 1912 | if (ret) { |
| 1913 | printk("insert failed %d\n", ret); |
| 1914 | close_bdev_excl(bdev); |
| 1915 | if (ret > 0) |
| 1916 | ret = -EEXIST; |
| 1917 | goto out; |
| 1918 | } |
| 1919 | dev_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), |
| 1920 | path->slots[0], struct btrfs_device_item); |
| 1921 | btrfs_set_device_pathlen(dev_item, namelen); |
| 1922 | memcpy(dev_item + 1, name, namelen); |
Chris Mason | b4100d6 | 2007-04-12 12:14:00 -0400 | [diff] [blame] | 1923 | |
| 1924 | device_id = btrfs_super_last_device_id(root->fs_info->disk_super) + 1; |
| 1925 | btrfs_set_super_last_device_id(root->fs_info->disk_super, device_id); |
| 1926 | btrfs_set_device_id(dev_item, device_id); |
Chris Mason | 8352d8a | 2007-04-12 10:43:05 -0400 | [diff] [blame] | 1927 | mark_buffer_dirty(path->nodes[0]); |
| 1928 | |
Chris Mason | b4100d6 | 2007-04-12 12:14:00 -0400 | [diff] [blame] | 1929 | ret = btrfs_insert_dev_radix(root, bdev, device_id, num_blocks, |
| 1930 | new_blocks); |
Chris Mason | 8352d8a | 2007-04-12 10:43:05 -0400 | [diff] [blame] | 1931 | |
| 1932 | if (!ret) { |
| 1933 | btrfs_set_super_total_blocks(root->fs_info->disk_super, |
| 1934 | num_blocks + new_blocks); |
| 1935 | i_size_write(root->fs_info->btree_inode, |
| 1936 | (num_blocks + new_blocks) << |
| 1937 | root->fs_info->btree_inode->i_blkbits); |
| 1938 | } |
| 1939 | |
| 1940 | out: |
| 1941 | ret = btrfs_commit_transaction(trans, dev_root); |
| 1942 | BUG_ON(ret); |
| 1943 | mutex_unlock(&root->fs_info->fs_mutex); |
| 1944 | out_nolock: |
| 1945 | btrfs_free_path(path); |
| 1946 | |
| 1947 | return ret; |
| 1948 | } |
| 1949 | |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 1950 | static int btrfs_ioctl(struct inode *inode, struct file *filp, unsigned int |
| 1951 | cmd, unsigned long arg) |
| 1952 | { |
| 1953 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1954 | struct btrfs_ioctl_vol_args vol_args; |
Chris Mason | 8352d8a | 2007-04-12 10:43:05 -0400 | [diff] [blame] | 1955 | int ret = 0; |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 1956 | int namelen; |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 1957 | struct btrfs_path *path; |
| 1958 | u64 root_dirid; |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 1959 | |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 1960 | switch (cmd) { |
| 1961 | case BTRFS_IOC_SNAP_CREATE: |
| 1962 | if (copy_from_user(&vol_args, |
| 1963 | (struct btrfs_ioctl_vol_args __user *)arg, |
| 1964 | sizeof(vol_args))) |
| 1965 | return -EFAULT; |
| 1966 | namelen = strlen(vol_args.name); |
| 1967 | if (namelen > BTRFS_VOL_NAME_MAX) |
| 1968 | return -EINVAL; |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 1969 | path = btrfs_alloc_path(); |
| 1970 | if (!path) |
| 1971 | return -ENOMEM; |
Chris Mason | 2d13d8d | 2007-04-10 20:07:20 -0400 | [diff] [blame] | 1972 | root_dirid = root->fs_info->sb->s_root->d_inode->i_ino, |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 1973 | mutex_lock(&root->fs_info->fs_mutex); |
| 1974 | ret = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root, |
| 1975 | path, root_dirid, |
| 1976 | vol_args.name, namelen, 0); |
| 1977 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | 2d13d8d | 2007-04-10 20:07:20 -0400 | [diff] [blame] | 1978 | btrfs_free_path(path); |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 1979 | if (ret == 0) |
| 1980 | return -EEXIST; |
| 1981 | |
| 1982 | if (root == root->fs_info->tree_root) |
| 1983 | ret = create_subvol(root, vol_args.name, namelen); |
| 1984 | else |
| 1985 | ret = create_snapshot(root, vol_args.name, namelen); |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 1986 | WARN_ON(ret); |
| 1987 | break; |
Chris Mason | 8352d8a | 2007-04-12 10:43:05 -0400 | [diff] [blame] | 1988 | case BTRFS_IOC_ADD_DISK: |
| 1989 | if (copy_from_user(&vol_args, |
| 1990 | (struct btrfs_ioctl_vol_args __user *)arg, |
| 1991 | sizeof(vol_args))) |
| 1992 | return -EFAULT; |
| 1993 | namelen = strlen(vol_args.name); |
| 1994 | if (namelen > BTRFS_VOL_NAME_MAX) |
| 1995 | return -EINVAL; |
| 1996 | vol_args.name[namelen] = '\0'; |
| 1997 | ret = add_disk(root, vol_args.name, namelen); |
| 1998 | break; |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 1999 | default: |
| 2000 | return -ENOTTY; |
| 2001 | } |
Chris Mason | 8352d8a | 2007-04-12 10:43:05 -0400 | [diff] [blame] | 2002 | return ret; |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 2003 | } |
| 2004 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 2005 | static struct kmem_cache *btrfs_inode_cachep; |
| 2006 | struct kmem_cache *btrfs_trans_handle_cachep; |
| 2007 | struct kmem_cache *btrfs_transaction_cachep; |
| 2008 | struct kmem_cache *btrfs_bit_radix_cachep; |
| 2009 | struct kmem_cache *btrfs_path_cachep; |
| 2010 | |
| 2011 | /* |
| 2012 | * Called inside transaction, so use GFP_NOFS |
| 2013 | */ |
| 2014 | static struct inode *btrfs_alloc_inode(struct super_block *sb) |
| 2015 | { |
| 2016 | struct btrfs_inode *ei; |
| 2017 | |
| 2018 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); |
| 2019 | if (!ei) |
| 2020 | return NULL; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 2021 | return &ei->vfs_inode; |
| 2022 | } |
| 2023 | |
| 2024 | static void btrfs_destroy_inode(struct inode *inode) |
| 2025 | { |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 2026 | WARN_ON(!list_empty(&inode->i_dentry)); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 2027 | WARN_ON(inode->i_data.nrpages); |
| 2028 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 2029 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 2030 | } |
| 2031 | |
| 2032 | static void init_once(void * foo, struct kmem_cache * cachep, |
| 2033 | unsigned long flags) |
| 2034 | { |
| 2035 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; |
| 2036 | |
| 2037 | if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) == |
| 2038 | SLAB_CTOR_CONSTRUCTOR) { |
| 2039 | inode_init_once(&ei->vfs_inode); |
| 2040 | } |
| 2041 | } |
| 2042 | |
| 2043 | static int init_inodecache(void) |
| 2044 | { |
| 2045 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache", |
| 2046 | sizeof(struct btrfs_inode), |
| 2047 | 0, (SLAB_RECLAIM_ACCOUNT| |
| 2048 | SLAB_MEM_SPREAD), |
| 2049 | init_once, NULL); |
| 2050 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache", |
| 2051 | sizeof(struct btrfs_trans_handle), |
| 2052 | 0, (SLAB_RECLAIM_ACCOUNT| |
| 2053 | SLAB_MEM_SPREAD), |
| 2054 | NULL, NULL); |
| 2055 | btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache", |
| 2056 | sizeof(struct btrfs_transaction), |
| 2057 | 0, (SLAB_RECLAIM_ACCOUNT| |
| 2058 | SLAB_MEM_SPREAD), |
| 2059 | NULL, NULL); |
| 2060 | btrfs_path_cachep = kmem_cache_create("btrfs_path_cache", |
| 2061 | sizeof(struct btrfs_transaction), |
| 2062 | 0, (SLAB_RECLAIM_ACCOUNT| |
| 2063 | SLAB_MEM_SPREAD), |
| 2064 | NULL, NULL); |
| 2065 | btrfs_bit_radix_cachep = kmem_cache_create("btrfs_radix", |
| 2066 | 256, |
| 2067 | 0, (SLAB_RECLAIM_ACCOUNT| |
| 2068 | SLAB_MEM_SPREAD | |
| 2069 | SLAB_DESTROY_BY_RCU), |
| 2070 | NULL, NULL); |
| 2071 | if (btrfs_inode_cachep == NULL || btrfs_trans_handle_cachep == NULL || |
| 2072 | btrfs_transaction_cachep == NULL || btrfs_bit_radix_cachep == NULL) |
| 2073 | return -ENOMEM; |
| 2074 | return 0; |
| 2075 | } |
| 2076 | |
| 2077 | static void destroy_inodecache(void) |
| 2078 | { |
| 2079 | kmem_cache_destroy(btrfs_inode_cachep); |
| 2080 | kmem_cache_destroy(btrfs_trans_handle_cachep); |
| 2081 | kmem_cache_destroy(btrfs_transaction_cachep); |
| 2082 | kmem_cache_destroy(btrfs_bit_radix_cachep); |
| 2083 | kmem_cache_destroy(btrfs_path_cachep); |
| 2084 | } |
| 2085 | |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 2086 | static int btrfs_get_sb(struct file_system_type *fs_type, |
| 2087 | int flags, const char *dev_name, void *data, struct vfsmount *mnt) |
| 2088 | { |
| 2089 | return get_sb_bdev(fs_type, flags, dev_name, data, |
| 2090 | btrfs_fill_super, mnt); |
| 2091 | } |
| 2092 | |
| 2093 | static struct file_system_type btrfs_fs_type = { |
| 2094 | .owner = THIS_MODULE, |
| 2095 | .name = "btrfs", |
| 2096 | .get_sb = btrfs_get_sb, |
| 2097 | .kill_sb = kill_block_super, |
| 2098 | .fs_flags = FS_REQUIRES_DEV, |
| 2099 | }; |
| 2100 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 2101 | static struct super_operations btrfs_super_ops = { |
| 2102 | .statfs = simple_statfs, |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 2103 | .delete_inode = btrfs_delete_inode, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 2104 | .put_super = btrfs_put_super, |
| 2105 | .read_inode = btrfs_read_locked_inode, |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 2106 | .write_super = btrfs_write_super, |
| 2107 | .sync_fs = btrfs_sync_fs, |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 2108 | .write_inode = btrfs_write_inode, |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 2109 | .alloc_inode = btrfs_alloc_inode, |
| 2110 | .destroy_inode = btrfs_destroy_inode, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 2111 | }; |
| 2112 | |
| 2113 | static struct inode_operations btrfs_dir_inode_operations = { |
| 2114 | .lookup = btrfs_lookup, |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 2115 | .create = btrfs_create, |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 2116 | .unlink = btrfs_unlink, |
Chris Mason | f792203 | 2007-03-25 20:17:36 -0400 | [diff] [blame] | 2117 | .mkdir = btrfs_mkdir, |
Chris Mason | 5f443fd | 2007-03-27 13:42:32 -0400 | [diff] [blame] | 2118 | .rmdir = btrfs_rmdir, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 2119 | }; |
| 2120 | |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 2121 | static struct inode_operations btrfs_dir_ro_inode_operations = { |
| 2122 | .lookup = btrfs_lookup, |
| 2123 | }; |
| 2124 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 2125 | static struct file_operations btrfs_dir_file_operations = { |
| 2126 | .llseek = generic_file_llseek, |
| 2127 | .read = generic_read_dir, |
| 2128 | .readdir = btrfs_readdir, |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 2129 | .ioctl = btrfs_ioctl, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 2130 | }; |
| 2131 | |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 2132 | static struct address_space_operations btrfs_aops = { |
| 2133 | .readpage = btrfs_readpage, |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 2134 | .writepage = btrfs_writepage, |
| 2135 | .sync_page = block_sync_page, |
| 2136 | .prepare_write = btrfs_prepare_write, |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 2137 | .commit_write = btrfs_commit_write, |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 2138 | }; |
| 2139 | |
| 2140 | static struct inode_operations btrfs_file_inode_operations = { |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 2141 | .truncate = btrfs_truncate, |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 2142 | }; |
| 2143 | |
| 2144 | static struct file_operations btrfs_file_operations = { |
| 2145 | .llseek = generic_file_llseek, |
| 2146 | .read = do_sync_read, |
Chris Mason | e8f05c4 | 2007-04-04 14:30:09 -0400 | [diff] [blame] | 2147 | .aio_read = btrfs_file_aio_read, |
| 2148 | .write = btrfs_file_write, |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 2149 | .mmap = generic_file_mmap, |
| 2150 | .open = generic_file_open, |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 2151 | .ioctl = btrfs_ioctl, |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 2152 | }; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 2153 | |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 2154 | static int __init init_btrfs_fs(void) |
| 2155 | { |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 2156 | int err; |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 2157 | printk("btrfs loaded!\n"); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 2158 | err = init_inodecache(); |
| 2159 | if (err) |
| 2160 | return err; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 2161 | kset_set_kset_s(&btrfs_subsys, fs_subsys); |
| 2162 | err = subsystem_register(&btrfs_subsys); |
| 2163 | if (err) |
| 2164 | goto out; |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 2165 | return register_filesystem(&btrfs_fs_type); |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 2166 | out: |
| 2167 | destroy_inodecache(); |
| 2168 | return err; |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 2169 | } |
| 2170 | |
| 2171 | static void __exit exit_btrfs_fs(void) |
| 2172 | { |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 2173 | destroy_inodecache(); |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 2174 | unregister_filesystem(&btrfs_fs_type); |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 2175 | subsystem_unregister(&btrfs_subsys); |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 2176 | printk("btrfs unloaded\n"); |
| 2177 | } |
| 2178 | |
| 2179 | module_init(init_btrfs_fs) |
| 2180 | module_exit(exit_btrfs_fs) |
| 2181 | |
| 2182 | MODULE_LICENSE("GPL"); |