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); |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 153 | dentry->d_inode->i_ctime = dir->i_ctime; |
| 154 | err: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 155 | btrfs_release_path(root, path); |
| 156 | btrfs_free_path(path); |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 157 | if (ret == 0) { |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 158 | inode_dec_link_count(dentry->d_inode); |
Chris Mason | 5f26f77 | 2007-04-05 10:38:44 -0400 | [diff] [blame] | 159 | dir->i_size -= name_len * 2; |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 160 | mark_inode_dirty(dir); |
| 161 | } |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 162 | return ret; |
| 163 | } |
| 164 | |
Chris Mason | 5f443fd | 2007-03-27 13:42:32 -0400 | [diff] [blame] | 165 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) |
| 166 | { |
| 167 | struct btrfs_root *root; |
| 168 | struct btrfs_trans_handle *trans; |
| 169 | int ret; |
| 170 | |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 171 | root = BTRFS_I(dir)->root; |
Chris Mason | 5f443fd | 2007-03-27 13:42:32 -0400 | [diff] [blame] | 172 | mutex_lock(&root->fs_info->fs_mutex); |
| 173 | trans = btrfs_start_transaction(root, 1); |
| 174 | ret = btrfs_unlink_trans(trans, root, dir, dentry); |
| 175 | btrfs_end_transaction(trans, root); |
| 176 | mutex_unlock(&root->fs_info->fs_mutex); |
| 177 | return ret; |
| 178 | } |
| 179 | |
| 180 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 181 | { |
| 182 | struct inode *inode = dentry->d_inode; |
| 183 | int err; |
| 184 | int ret; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 185 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 186 | struct btrfs_path *path; |
Chris Mason | 5f443fd | 2007-03-27 13:42:32 -0400 | [diff] [blame] | 187 | struct btrfs_key key; |
| 188 | struct btrfs_trans_handle *trans; |
Chris Mason | 5f26f77 | 2007-04-05 10:38:44 -0400 | [diff] [blame] | 189 | struct btrfs_key found_key; |
| 190 | int found_type; |
Chris Mason | 5f443fd | 2007-03-27 13:42:32 -0400 | [diff] [blame] | 191 | struct btrfs_leaf *leaf; |
Chris Mason | 5f26f77 | 2007-04-05 10:38:44 -0400 | [diff] [blame] | 192 | char *goodnames = ".."; |
Chris Mason | 5f443fd | 2007-03-27 13:42:32 -0400 | [diff] [blame] | 193 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 194 | path = btrfs_alloc_path(); |
| 195 | BUG_ON(!path); |
| 196 | btrfs_init_path(path); |
Chris Mason | 5f443fd | 2007-03-27 13:42:32 -0400 | [diff] [blame] | 197 | mutex_lock(&root->fs_info->fs_mutex); |
| 198 | trans = btrfs_start_transaction(root, 1); |
| 199 | key.objectid = inode->i_ino; |
| 200 | key.offset = (u64)-1; |
Chris Mason | 5f26f77 | 2007-04-05 10:38:44 -0400 | [diff] [blame] | 201 | key.flags = (u32)-1; |
| 202 | while(1) { |
| 203 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 204 | if (ret < 0) { |
| 205 | err = ret; |
| 206 | goto out; |
| 207 | } |
| 208 | BUG_ON(ret == 0); |
| 209 | if (path->slots[0] == 0) { |
| 210 | err = -ENOENT; |
| 211 | goto out; |
| 212 | } |
| 213 | path->slots[0]--; |
| 214 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
| 215 | btrfs_disk_key_to_cpu(&found_key, |
| 216 | &leaf->items[path->slots[0]].key); |
| 217 | found_type = btrfs_key_type(&found_key); |
| 218 | if (found_key.objectid != inode->i_ino) { |
| 219 | err = -ENOENT; |
| 220 | goto out; |
| 221 | } |
| 222 | if ((found_type != BTRFS_DIR_ITEM_KEY && |
| 223 | found_type != BTRFS_DIR_INDEX_KEY) || |
| 224 | (!btrfs_match_dir_item_name(root, path, goodnames, 2) && |
| 225 | !btrfs_match_dir_item_name(root, path, goodnames, 1))) { |
| 226 | err = -ENOTEMPTY; |
| 227 | goto out; |
| 228 | } |
| 229 | ret = btrfs_del_item(trans, root, path); |
| 230 | BUG_ON(ret); |
Chris Mason | 5f443fd | 2007-03-27 13:42:32 -0400 | [diff] [blame] | 231 | |
Chris Mason | 5f26f77 | 2007-04-05 10:38:44 -0400 | [diff] [blame] | 232 | if (found_type == BTRFS_DIR_ITEM_KEY && found_key.offset == 1) |
| 233 | break; |
| 234 | btrfs_release_path(root, path); |
Chris Mason | 5f443fd | 2007-03-27 13:42:32 -0400 | [diff] [blame] | 235 | } |
Chris Mason | 5f26f77 | 2007-04-05 10:38:44 -0400 | [diff] [blame] | 236 | ret = 0; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 237 | btrfs_release_path(root, path); |
Chris Mason | 5f443fd | 2007-03-27 13:42:32 -0400 | [diff] [blame] | 238 | |
| 239 | /* now the directory is empty */ |
| 240 | err = btrfs_unlink_trans(trans, root, dir, dentry); |
| 241 | if (!err) { |
| 242 | inode->i_size = 0; |
| 243 | } |
| 244 | out: |
Chris Mason | 7cfcc17 | 2007-04-02 14:53:59 -0400 | [diff] [blame] | 245 | btrfs_release_path(root, path); |
| 246 | btrfs_free_path(path); |
Chris Mason | 5f443fd | 2007-03-27 13:42:32 -0400 | [diff] [blame] | 247 | mutex_unlock(&root->fs_info->fs_mutex); |
| 248 | ret = btrfs_end_transaction(trans, root); |
| 249 | if (ret && !err) |
| 250 | err = ret; |
| 251 | return err; |
| 252 | } |
| 253 | |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 254 | static int btrfs_free_inode(struct btrfs_trans_handle *trans, |
| 255 | struct btrfs_root *root, |
| 256 | struct inode *inode) |
| 257 | { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 258 | struct btrfs_path *path; |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 259 | int ret; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 260 | |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 261 | clear_inode(inode); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 262 | |
| 263 | path = btrfs_alloc_path(); |
| 264 | BUG_ON(!path); |
| 265 | btrfs_init_path(path); |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 266 | ret = btrfs_lookup_inode(trans, root, path, |
| 267 | &BTRFS_I(inode)->location, -1); |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 268 | BUG_ON(ret); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 269 | ret = btrfs_del_item(trans, root, path); |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 270 | BUG_ON(ret); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 271 | btrfs_free_path(path); |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 272 | return ret; |
| 273 | } |
| 274 | |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 275 | static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans, |
| 276 | struct btrfs_root *root, |
| 277 | struct inode *inode) |
| 278 | { |
| 279 | int ret; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 280 | struct btrfs_path *path; |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 281 | struct btrfs_key key; |
| 282 | struct btrfs_disk_key *found_key; |
| 283 | struct btrfs_leaf *leaf; |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 284 | struct btrfs_file_extent_item *fi = NULL; |
| 285 | u64 extent_start = 0; |
| 286 | u64 extent_num_blocks = 0; |
| 287 | int found_extent; |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 288 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 289 | path = btrfs_alloc_path(); |
| 290 | BUG_ON(!path); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 291 | /* FIXME, add redo link to tree so we don't leak on crash */ |
| 292 | key.objectid = inode->i_ino; |
| 293 | key.offset = (u64)-1; |
| 294 | key.flags = 0; |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 295 | /* |
| 296 | * use BTRFS_CSUM_ITEM_KEY because it is larger than inline keys |
| 297 | * or extent data |
| 298 | */ |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 299 | btrfs_set_key_type(&key, BTRFS_CSUM_ITEM_KEY); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 300 | while(1) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 301 | btrfs_init_path(path); |
| 302 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 303 | if (ret < 0) { |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 304 | goto error; |
| 305 | } |
| 306 | if (ret > 0) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 307 | BUG_ON(path->slots[0] == 0); |
| 308 | path->slots[0]--; |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 309 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 310 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
| 311 | found_key = &leaf->items[path->slots[0]].key; |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 312 | if (btrfs_disk_key_objectid(found_key) != inode->i_ino) |
| 313 | break; |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 314 | if (btrfs_disk_key_type(found_key) != BTRFS_CSUM_ITEM_KEY && |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 315 | btrfs_disk_key_type(found_key) != BTRFS_INLINE_DATA_KEY && |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 316 | btrfs_disk_key_type(found_key) != BTRFS_EXTENT_DATA_KEY) |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 317 | break; |
| 318 | if (btrfs_disk_key_offset(found_key) < inode->i_size) |
| 319 | break; |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame^] | 320 | found_extent = 0; |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 321 | if (btrfs_disk_key_type(found_key) == BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 322 | fi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), |
| 323 | path->slots[0], |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 324 | struct btrfs_file_extent_item); |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame^] | 325 | if (btrfs_file_extent_type(fi) != |
| 326 | BTRFS_FILE_EXTENT_INLINE) { |
| 327 | extent_start = |
| 328 | btrfs_file_extent_disk_blocknr(fi); |
| 329 | extent_num_blocks = |
| 330 | btrfs_file_extent_disk_num_blocks(fi); |
| 331 | /* FIXME blocksize != 4096 */ |
| 332 | inode->i_blocks -= |
| 333 | btrfs_file_extent_num_blocks(fi) << 3; |
| 334 | found_extent = 1; |
| 335 | } |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 336 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 337 | ret = btrfs_del_item(trans, root, path); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 338 | BUG_ON(ret); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 339 | btrfs_release_path(root, path); |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 340 | if (found_extent) { |
| 341 | ret = btrfs_free_extent(trans, root, extent_start, |
| 342 | extent_num_blocks, 0); |
| 343 | BUG_ON(ret); |
| 344 | } |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 345 | } |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 346 | ret = 0; |
| 347 | error: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 348 | btrfs_release_path(root, path); |
| 349 | btrfs_free_path(path); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 350 | return ret; |
| 351 | } |
| 352 | |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 353 | static void btrfs_delete_inode(struct inode *inode) |
| 354 | { |
| 355 | struct btrfs_trans_handle *trans; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 356 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 357 | int ret; |
| 358 | |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 359 | truncate_inode_pages(&inode->i_data, 0); |
| 360 | if (is_bad_inode(inode)) { |
| 361 | goto no_delete; |
| 362 | } |
| 363 | inode->i_size = 0; |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 364 | mutex_lock(&root->fs_info->fs_mutex); |
| 365 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 366 | if (S_ISREG(inode->i_mode)) { |
| 367 | ret = btrfs_truncate_in_trans(trans, root, inode); |
| 368 | BUG_ON(ret); |
| 369 | } |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 370 | btrfs_free_inode(trans, root, inode); |
| 371 | btrfs_end_transaction(trans, root); |
| 372 | mutex_unlock(&root->fs_info->fs_mutex); |
| 373 | return; |
| 374 | no_delete: |
| 375 | clear_inode(inode); |
| 376 | } |
| 377 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 378 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 379 | struct btrfs_key *location) |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 380 | { |
| 381 | const char *name = dentry->d_name.name; |
| 382 | int namelen = dentry->d_name.len; |
| 383 | struct btrfs_dir_item *di; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 384 | struct btrfs_path *path; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 385 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 386 | int ret; |
| 387 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 388 | path = btrfs_alloc_path(); |
| 389 | BUG_ON(!path); |
| 390 | btrfs_init_path(path); |
| 391 | ret = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 392 | namelen, 0); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 393 | if (ret || !btrfs_match_dir_item_name(root, path, name, namelen)) { |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 394 | location->objectid = 0; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 395 | ret = 0; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 396 | goto out; |
| 397 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 398 | 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] | 399 | struct btrfs_dir_item); |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 400 | btrfs_disk_key_to_cpu(location, &di->location); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 401 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 402 | btrfs_release_path(root, path); |
| 403 | btrfs_free_path(path); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 404 | return ret; |
| 405 | } |
| 406 | |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 407 | int fixup_tree_root_location(struct btrfs_root *root, |
| 408 | struct btrfs_key *location, |
| 409 | struct btrfs_root **sub_root) |
| 410 | { |
| 411 | struct btrfs_path *path; |
| 412 | struct btrfs_root_item *ri; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 413 | |
| 414 | if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY) |
| 415 | return 0; |
| 416 | if (location->objectid == BTRFS_ROOT_TREE_OBJECTID) |
| 417 | return 0; |
| 418 | |
| 419 | path = btrfs_alloc_path(); |
| 420 | BUG_ON(!path); |
| 421 | mutex_lock(&root->fs_info->fs_mutex); |
| 422 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 423 | *sub_root = btrfs_read_fs_root(root->fs_info, location); |
| 424 | if (IS_ERR(*sub_root)) |
| 425 | return PTR_ERR(*sub_root); |
| 426 | |
| 427 | ri = &(*sub_root)->root_item; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 428 | location->objectid = btrfs_root_dirid(ri); |
| 429 | location->flags = 0; |
| 430 | btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); |
| 431 | location->offset = 0; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 432 | |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 433 | btrfs_free_path(path); |
| 434 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 435 | return 0; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 436 | } |
| 437 | |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 438 | int btrfs_init_locked_inode(struct inode *inode, void *p) |
| 439 | { |
| 440 | struct btrfs_iget_args *args = p; |
| 441 | inode->i_ino = args->ino; |
| 442 | BTRFS_I(inode)->root = args->root; |
| 443 | return 0; |
| 444 | } |
| 445 | |
| 446 | int btrfs_find_actor(struct inode *inode, void *opaque) |
| 447 | { |
| 448 | struct btrfs_iget_args *args = opaque; |
| 449 | return (args->ino == inode->i_ino && |
| 450 | args->root == BTRFS_I(inode)->root); |
| 451 | } |
| 452 | |
| 453 | struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid, |
| 454 | struct btrfs_root *root) |
| 455 | { |
| 456 | struct inode *inode; |
| 457 | struct btrfs_iget_args args; |
| 458 | args.ino = objectid; |
| 459 | args.root = root; |
| 460 | |
| 461 | inode = iget5_locked(s, objectid, btrfs_find_actor, |
| 462 | btrfs_init_locked_inode, |
| 463 | (void *)&args); |
| 464 | return inode; |
| 465 | } |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 466 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 467 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
| 468 | struct nameidata *nd) |
| 469 | { |
| 470 | struct inode * inode; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 471 | struct btrfs_inode *bi = BTRFS_I(dir); |
| 472 | struct btrfs_root *root = bi->root; |
| 473 | struct btrfs_root *sub_root = root; |
| 474 | struct btrfs_key location; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 475 | int ret; |
| 476 | |
| 477 | if (dentry->d_name.len > BTRFS_NAME_LEN) |
| 478 | return ERR_PTR(-ENAMETOOLONG); |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 479 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 480 | ret = btrfs_inode_by_name(dir, dentry, &location); |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 481 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 482 | if (ret < 0) |
| 483 | return ERR_PTR(ret); |
| 484 | inode = NULL; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 485 | if (location.objectid) { |
| 486 | ret = fixup_tree_root_location(root, &location, &sub_root); |
| 487 | if (ret < 0) |
| 488 | return ERR_PTR(ret); |
| 489 | if (ret > 0) |
| 490 | return ERR_PTR(-ENOENT); |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 491 | inode = btrfs_iget_locked(dir->i_sb, location.objectid, |
| 492 | sub_root); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 493 | if (!inode) |
| 494 | return ERR_PTR(-EACCES); |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 495 | if (inode->i_state & I_NEW) { |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 496 | if (sub_root != root) { |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 497 | 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] | 498 | igrab(inode); |
| 499 | sub_root->inode = inode; |
| 500 | } |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 501 | BTRFS_I(inode)->root = sub_root; |
| 502 | memcpy(&BTRFS_I(inode)->location, &location, |
| 503 | sizeof(location)); |
| 504 | btrfs_read_locked_inode(inode); |
| 505 | unlock_new_inode(inode); |
| 506 | } |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 507 | } |
| 508 | return d_splice_alias(inode, dentry); |
| 509 | } |
| 510 | |
| 511 | static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir) |
| 512 | { |
| 513 | struct inode *inode = filp->f_path.dentry->d_inode; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 514 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 515 | struct btrfs_item *item; |
| 516 | struct btrfs_dir_item *di; |
| 517 | struct btrfs_key key; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 518 | struct btrfs_path *path; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 519 | int ret; |
| 520 | u32 nritems; |
| 521 | struct btrfs_leaf *leaf; |
| 522 | int slot; |
| 523 | int advance; |
| 524 | unsigned char d_type = DT_UNKNOWN; |
Chris Mason | 7f5c151 | 2007-03-23 15:56:19 -0400 | [diff] [blame] | 525 | int over = 0; |
Chris Mason | 70b2bef | 2007-04-17 15:39:32 -0400 | [diff] [blame] | 526 | int key_type = BTRFS_DIR_ITEM_KEY; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 527 | |
| 528 | /* FIXME, use a real flag for deciding about the key type */ |
| 529 | if (root->fs_info->tree_root == root) |
| 530 | key_type = BTRFS_DIR_ITEM_KEY; |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 531 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 532 | key.objectid = inode->i_ino; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 533 | key.flags = 0; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 534 | btrfs_set_key_type(&key, key_type); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 535 | key.offset = filp->f_pos; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 536 | path = btrfs_alloc_path(); |
| 537 | btrfs_init_path(path); |
| 538 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
Chris Mason | 1b05da2 | 2007-04-10 12:13:09 -0400 | [diff] [blame] | 539 | if (ret < 0) |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 540 | goto err; |
Chris Mason | 7f5c151 | 2007-03-23 15:56:19 -0400 | [diff] [blame] | 541 | advance = 0; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 542 | while(1) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 543 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 544 | nritems = btrfs_header_nritems(&leaf->header); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 545 | slot = path->slots[0]; |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 546 | if (advance || slot >= nritems) { |
| 547 | if (slot >= nritems -1) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 548 | ret = btrfs_next_leaf(root, path); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 549 | if (ret) |
| 550 | break; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 551 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 552 | nritems = btrfs_header_nritems(&leaf->header); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 553 | slot = path->slots[0]; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 554 | } else { |
| 555 | slot++; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 556 | path->slots[0]++; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 557 | } |
| 558 | } |
| 559 | advance = 1; |
| 560 | item = leaf->items + slot; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 561 | if (btrfs_disk_key_objectid(&item->key) != key.objectid) |
| 562 | break; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 563 | if (btrfs_disk_key_type(&item->key) != key_type) |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 564 | break; |
Chris Mason | 7f5c151 | 2007-03-23 15:56:19 -0400 | [diff] [blame] | 565 | if (btrfs_disk_key_offset(&item->key) < filp->f_pos) |
| 566 | continue; |
Chris Mason | 7fcde0e | 2007-04-05 12:13:21 -0400 | [diff] [blame] | 567 | filp->f_pos = btrfs_disk_key_offset(&item->key); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 568 | advance = 1; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 569 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 570 | over = filldir(dirent, (const char *)(di + 1), |
| 571 | btrfs_dir_name_len(di), |
| 572 | btrfs_disk_key_offset(&item->key), |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 573 | btrfs_disk_key_objectid(&di->location), d_type); |
Chris Mason | 7fcde0e | 2007-04-05 12:13:21 -0400 | [diff] [blame] | 574 | if (over) |
| 575 | goto nopos; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 576 | } |
Chris Mason | 7fcde0e | 2007-04-05 12:13:21 -0400 | [diff] [blame] | 577 | filp->f_pos++; |
| 578 | nopos: |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 579 | ret = 0; |
| 580 | err: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 581 | btrfs_release_path(root, path); |
| 582 | btrfs_free_path(path); |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 583 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 584 | return ret; |
| 585 | } |
| 586 | |
| 587 | static void btrfs_put_super (struct super_block * sb) |
| 588 | { |
| 589 | struct btrfs_root *root = btrfs_sb(sb); |
| 590 | int ret; |
| 591 | |
| 592 | ret = close_ctree(root); |
| 593 | if (ret) { |
| 594 | printk("close ctree returns %d\n", ret); |
| 595 | } |
| 596 | sb->s_fs_info = NULL; |
| 597 | } |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 598 | |
| 599 | static int btrfs_fill_super(struct super_block * sb, void * data, int silent) |
| 600 | { |
| 601 | struct inode * inode; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 602 | struct dentry * root_dentry; |
| 603 | struct btrfs_super_block *disk_super; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 604 | struct btrfs_root *tree_root; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 605 | struct btrfs_inode *bi; |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 606 | |
| 607 | sb->s_maxbytes = MAX_LFS_FILESIZE; |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 608 | sb->s_magic = BTRFS_SUPER_MAGIC; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 609 | sb->s_op = &btrfs_super_ops; |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 610 | sb->s_time_gran = 1; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 611 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 612 | tree_root = open_ctree(sb); |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 613 | |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 614 | if (!tree_root) { |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 615 | printk("btrfs: open_ctree failed\n"); |
| 616 | return -EIO; |
| 617 | } |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 618 | sb->s_fs_info = tree_root; |
| 619 | disk_super = tree_root->fs_info->disk_super; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 620 | printk("read in super total blocks %Lu root %Lu\n", |
| 621 | btrfs_super_total_blocks(disk_super), |
| 622 | btrfs_super_root_dir(disk_super)); |
| 623 | |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 624 | inode = btrfs_iget_locked(sb, btrfs_super_root_dir(disk_super), |
| 625 | tree_root); |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 626 | bi = BTRFS_I(inode); |
| 627 | bi->location.objectid = inode->i_ino; |
| 628 | bi->location.offset = 0; |
| 629 | bi->location.flags = 0; |
Chris Mason | 0f7d52f | 2007-04-09 10:42:37 -0400 | [diff] [blame] | 630 | bi->root = tree_root; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 631 | btrfs_set_key_type(&bi->location, BTRFS_INODE_ITEM_KEY); |
| 632 | |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 633 | if (!inode) |
| 634 | return -ENOMEM; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 635 | if (inode->i_state & I_NEW) { |
| 636 | btrfs_read_locked_inode(inode); |
| 637 | unlock_new_inode(inode); |
| 638 | } |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 639 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 640 | root_dentry = d_alloc_root(inode); |
| 641 | if (!root_dentry) { |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 642 | iput(inode); |
| 643 | return -ENOMEM; |
| 644 | } |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 645 | sb->s_root = root_dentry; |
| 646 | |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 647 | return 0; |
| 648 | } |
| 649 | |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 650 | static void fill_inode_item(struct btrfs_inode_item *item, |
| 651 | struct inode *inode) |
| 652 | { |
| 653 | btrfs_set_inode_uid(item, inode->i_uid); |
| 654 | btrfs_set_inode_gid(item, inode->i_gid); |
| 655 | btrfs_set_inode_size(item, inode->i_size); |
| 656 | btrfs_set_inode_mode(item, inode->i_mode); |
| 657 | btrfs_set_inode_nlink(item, inode->i_nlink); |
| 658 | btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec); |
| 659 | btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec); |
| 660 | btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec); |
| 661 | btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec); |
| 662 | btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec); |
| 663 | btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec); |
| 664 | btrfs_set_inode_nblocks(item, inode->i_blocks); |
| 665 | btrfs_set_inode_generation(item, inode->i_generation); |
| 666 | } |
| 667 | |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 668 | static int btrfs_update_inode(struct btrfs_trans_handle *trans, |
| 669 | struct btrfs_root *root, |
| 670 | struct inode *inode) |
| 671 | { |
| 672 | struct btrfs_inode_item *inode_item; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 673 | struct btrfs_path *path; |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 674 | int ret; |
| 675 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 676 | path = btrfs_alloc_path(); |
| 677 | BUG_ON(!path); |
| 678 | btrfs_init_path(path); |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 679 | ret = btrfs_lookup_inode(trans, root, path, |
| 680 | &BTRFS_I(inode)->location, 1); |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 681 | if (ret) { |
| 682 | if (ret > 0) |
| 683 | ret = -ENOENT; |
| 684 | goto failed; |
| 685 | } |
| 686 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 687 | inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), |
| 688 | path->slots[0], |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 689 | struct btrfs_inode_item); |
| 690 | |
| 691 | fill_inode_item(inode_item, inode); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 692 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | 1b05da2 | 2007-04-10 12:13:09 -0400 | [diff] [blame] | 693 | ret = 0; |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 694 | failed: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 695 | btrfs_release_path(root, path); |
| 696 | btrfs_free_path(path); |
Chris Mason | 1b05da2 | 2007-04-10 12:13:09 -0400 | [diff] [blame] | 697 | return ret; |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 698 | } |
| 699 | |
| 700 | static int btrfs_write_inode(struct inode *inode, int wait) |
| 701 | { |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 702 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 703 | struct btrfs_trans_handle *trans; |
| 704 | int ret; |
| 705 | |
| 706 | mutex_lock(&root->fs_info->fs_mutex); |
| 707 | trans = btrfs_start_transaction(root, 1); |
| 708 | ret = btrfs_update_inode(trans, root, inode); |
| 709 | if (wait) |
| 710 | btrfs_commit_transaction(trans, root); |
| 711 | else |
| 712 | btrfs_end_transaction(trans, root); |
| 713 | mutex_unlock(&root->fs_info->fs_mutex); |
| 714 | return ret; |
| 715 | } |
| 716 | |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 717 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 718 | struct btrfs_root *root, |
| 719 | u64 objectid, int mode) |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 720 | { |
| 721 | struct inode *inode; |
| 722 | struct btrfs_inode_item inode_item; |
Chris Mason | 1b05da2 | 2007-04-10 12:13:09 -0400 | [diff] [blame] | 723 | struct btrfs_key *location; |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 724 | int ret; |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 725 | |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 726 | inode = new_inode(root->fs_info->sb); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 727 | if (!inode) |
| 728 | return ERR_PTR(-ENOMEM); |
| 729 | |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 730 | BTRFS_I(inode)->root = root; |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 731 | |
| 732 | inode->i_uid = current->fsuid; |
| 733 | inode->i_gid = current->fsgid; |
| 734 | inode->i_mode = mode; |
| 735 | inode->i_ino = objectid; |
| 736 | inode->i_blocks = 0; |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 737 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 738 | fill_inode_item(&inode_item, inode); |
Chris Mason | 1b05da2 | 2007-04-10 12:13:09 -0400 | [diff] [blame] | 739 | location = &BTRFS_I(inode)->location; |
| 740 | location->objectid = objectid; |
| 741 | location->flags = 0; |
| 742 | location->offset = 0; |
| 743 | btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 744 | |
| 745 | ret = btrfs_insert_inode(trans, root, objectid, &inode_item); |
| 746 | BUG_ON(ret); |
| 747 | |
| 748 | insert_inode_hash(inode); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 749 | return inode; |
| 750 | } |
| 751 | |
| 752 | static int btrfs_add_link(struct btrfs_trans_handle *trans, |
| 753 | struct dentry *dentry, struct inode *inode) |
| 754 | { |
| 755 | int ret; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 756 | struct btrfs_key key; |
| 757 | struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root; |
| 758 | key.objectid = inode->i_ino; |
| 759 | key.flags = 0; |
| 760 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); |
| 761 | key.offset = 0; |
| 762 | |
| 763 | ret = btrfs_insert_dir_item(trans, root, |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 764 | dentry->d_name.name, dentry->d_name.len, |
| 765 | dentry->d_parent->d_inode->i_ino, |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 766 | &key, 0); |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 767 | if (ret == 0) { |
Chris Mason | 5f26f77 | 2007-04-05 10:38:44 -0400 | [diff] [blame] | 768 | dentry->d_parent->d_inode->i_size += dentry->d_name.len * 2; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 769 | ret = btrfs_update_inode(trans, root, |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 770 | dentry->d_parent->d_inode); |
| 771 | } |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 772 | return ret; |
| 773 | } |
| 774 | |
| 775 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, |
| 776 | struct dentry *dentry, struct inode *inode) |
| 777 | { |
| 778 | int err = btrfs_add_link(trans, dentry, inode); |
| 779 | if (!err) { |
| 780 | d_instantiate(dentry, inode); |
| 781 | return 0; |
| 782 | } |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 783 | if (err > 0) |
| 784 | err = -EEXIST; |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 785 | return err; |
| 786 | } |
| 787 | |
| 788 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
| 789 | int mode, struct nameidata *nd) |
| 790 | { |
| 791 | struct btrfs_trans_handle *trans; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 792 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 793 | struct inode *inode; |
| 794 | int err; |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 795 | int drop_inode = 0; |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 796 | u64 objectid; |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 797 | |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 798 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 799 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 800 | |
| 801 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); |
| 802 | if (err) { |
| 803 | err = -ENOSPC; |
| 804 | goto out_unlock; |
| 805 | } |
| 806 | |
| 807 | inode = btrfs_new_inode(trans, root, objectid, mode); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 808 | err = PTR_ERR(inode); |
| 809 | if (IS_ERR(inode)) |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 810 | goto out_unlock; |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 811 | // FIXME mark the inode dirty |
| 812 | err = btrfs_add_nondir(trans, dentry, inode); |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 813 | if (err) |
| 814 | drop_inode = 1; |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 815 | else { |
| 816 | inode->i_mapping->a_ops = &btrfs_aops; |
| 817 | inode->i_fop = &btrfs_file_operations; |
| 818 | inode->i_op = &btrfs_file_inode_operations; |
| 819 | } |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 820 | dir->i_sb->s_dirt = 1; |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 821 | out_unlock: |
Chris Mason | 22b0ebd | 2007-03-30 08:47:31 -0400 | [diff] [blame] | 822 | btrfs_end_transaction(trans, root); |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 823 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 824 | |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 825 | if (drop_inode) { |
| 826 | inode_dec_link_count(inode); |
| 827 | iput(inode); |
| 828 | } |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 829 | return err; |
| 830 | } |
| 831 | |
Chris Mason | f792203 | 2007-03-25 20:17:36 -0400 | [diff] [blame] | 832 | static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans, |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 833 | struct btrfs_root *root, |
| 834 | u64 objectid, u64 dirid) |
Chris Mason | f792203 | 2007-03-25 20:17:36 -0400 | [diff] [blame] | 835 | { |
Chris Mason | f792203 | 2007-03-25 20:17:36 -0400 | [diff] [blame] | 836 | int ret; |
| 837 | char buf[2]; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 838 | struct btrfs_key key; |
| 839 | |
Chris Mason | f792203 | 2007-03-25 20:17:36 -0400 | [diff] [blame] | 840 | buf[0] = '.'; |
| 841 | buf[1] = '.'; |
| 842 | |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 843 | key.objectid = objectid; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 844 | key.offset = 0; |
| 845 | key.flags = 0; |
| 846 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); |
| 847 | |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 848 | ret = btrfs_insert_dir_item(trans, root, buf, 1, objectid, |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 849 | &key, 1); |
Chris Mason | f792203 | 2007-03-25 20:17:36 -0400 | [diff] [blame] | 850 | if (ret) |
| 851 | goto error; |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 852 | key.objectid = dirid; |
| 853 | ret = btrfs_insert_dir_item(trans, root, buf, 2, objectid, |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 854 | &key, 1); |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 855 | if (ret) |
| 856 | goto error; |
Chris Mason | f792203 | 2007-03-25 20:17:36 -0400 | [diff] [blame] | 857 | error: |
| 858 | return ret; |
| 859 | } |
| 860 | |
| 861 | static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) |
| 862 | { |
| 863 | struct inode *inode; |
| 864 | struct btrfs_trans_handle *trans; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 865 | struct btrfs_root *root = BTRFS_I(dir)->root; |
Chris Mason | f792203 | 2007-03-25 20:17:36 -0400 | [diff] [blame] | 866 | int err = 0; |
| 867 | int drop_on_err = 0; |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 868 | u64 objectid; |
Chris Mason | f792203 | 2007-03-25 20:17:36 -0400 | [diff] [blame] | 869 | |
| 870 | mutex_lock(&root->fs_info->fs_mutex); |
| 871 | trans = btrfs_start_transaction(root, 1); |
| 872 | if (IS_ERR(trans)) { |
| 873 | err = PTR_ERR(trans); |
| 874 | goto out_unlock; |
| 875 | } |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 876 | |
| 877 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); |
| 878 | if (err) { |
| 879 | err = -ENOSPC; |
| 880 | goto out_unlock; |
| 881 | } |
| 882 | |
| 883 | inode = btrfs_new_inode(trans, root, objectid, S_IFDIR | mode); |
Chris Mason | f792203 | 2007-03-25 20:17:36 -0400 | [diff] [blame] | 884 | if (IS_ERR(inode)) { |
| 885 | err = PTR_ERR(inode); |
| 886 | goto out_fail; |
| 887 | } |
| 888 | drop_on_err = 1; |
| 889 | inode->i_op = &btrfs_dir_inode_operations; |
| 890 | inode->i_fop = &btrfs_dir_file_operations; |
| 891 | |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 892 | err = btrfs_make_empty_dir(trans, root, inode->i_ino, dir->i_ino); |
| 893 | if (err) |
| 894 | goto out_fail; |
| 895 | |
| 896 | inode->i_size = 6; |
| 897 | err = btrfs_update_inode(trans, root, inode); |
Chris Mason | f792203 | 2007-03-25 20:17:36 -0400 | [diff] [blame] | 898 | if (err) |
| 899 | goto out_fail; |
| 900 | err = btrfs_add_link(trans, dentry, inode); |
| 901 | if (err) |
| 902 | goto out_fail; |
| 903 | d_instantiate(dentry, inode); |
Chris Mason | f792203 | 2007-03-25 20:17:36 -0400 | [diff] [blame] | 904 | drop_on_err = 0; |
| 905 | |
| 906 | out_fail: |
| 907 | btrfs_end_transaction(trans, root); |
| 908 | out_unlock: |
| 909 | mutex_unlock(&root->fs_info->fs_mutex); |
| 910 | if (drop_on_err) |
| 911 | iput(inode); |
| 912 | return err; |
| 913 | } |
| 914 | |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 915 | static int btrfs_sync_fs(struct super_block *sb, int wait) |
| 916 | { |
| 917 | struct btrfs_trans_handle *trans; |
| 918 | struct btrfs_root *root; |
| 919 | int ret; |
Chris Mason | d98237b | 2007-03-28 13:57:48 -0400 | [diff] [blame] | 920 | root = btrfs_sb(sb); |
Chris Mason | df2ce34 | 2007-03-23 11:00:45 -0400 | [diff] [blame] | 921 | |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 922 | sb->s_dirt = 0; |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 923 | if (!wait) { |
Chris Mason | 7cfcc17 | 2007-04-02 14:53:59 -0400 | [diff] [blame] | 924 | filemap_flush(root->fs_info->btree_inode->i_mapping); |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 925 | return 0; |
| 926 | } |
Chris Mason | 7cfcc17 | 2007-04-02 14:53:59 -0400 | [diff] [blame] | 927 | filemap_write_and_wait(root->fs_info->btree_inode->i_mapping); |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 928 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 929 | trans = btrfs_start_transaction(root, 1); |
| 930 | ret = btrfs_commit_transaction(trans, root); |
| 931 | sb->s_dirt = 0; |
| 932 | BUG_ON(ret); |
| 933 | printk("btrfs sync_fs\n"); |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 934 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 935 | return 0; |
| 936 | } |
| 937 | |
Chris Mason | e8f05c4 | 2007-04-04 14:30:09 -0400 | [diff] [blame] | 938 | #if 0 |
Chris Mason | df24a2b | 2007-04-04 09:36:31 -0400 | [diff] [blame] | 939 | static int btrfs_get_block_inline(struct inode *inode, sector_t iblock, |
| 940 | struct buffer_head *result, int create) |
| 941 | { |
| 942 | struct btrfs_root *root = btrfs_sb(inode->i_sb); |
| 943 | struct btrfs_path *path; |
| 944 | struct btrfs_key key; |
| 945 | struct btrfs_leaf *leaf; |
| 946 | int num_bytes = result->b_size; |
| 947 | int item_size; |
| 948 | int ret; |
| 949 | u64 pos; |
| 950 | char *ptr; |
| 951 | int copy_size; |
| 952 | int err = 0; |
| 953 | char *safe_ptr; |
| 954 | char *data_ptr; |
| 955 | |
| 956 | path = btrfs_alloc_path(); |
| 957 | BUG_ON(!path); |
| 958 | |
| 959 | WARN_ON(create); |
| 960 | if (create) { |
| 961 | return 0; |
| 962 | } |
| 963 | pos = iblock << inode->i_blkbits; |
| 964 | key.objectid = inode->i_ino; |
| 965 | key.flags = 0; |
| 966 | btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY); |
| 967 | ptr = kmap(result->b_page); |
| 968 | safe_ptr = ptr; |
| 969 | ptr += (pos & (PAGE_CACHE_SIZE -1)); |
| 970 | again: |
| 971 | key.offset = pos; |
| 972 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 973 | if (ret) { |
| 974 | if (ret < 0) |
| 975 | err = ret; |
| 976 | else |
| 977 | err = 0; |
| 978 | goto out; |
| 979 | } |
| 980 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
| 981 | item_size = btrfs_item_size(leaf->items + path->slots[0]); |
| 982 | copy_size = min(num_bytes, item_size); |
| 983 | data_ptr = btrfs_item_ptr(leaf, path->slots[0], char); |
| 984 | WARN_ON(safe_ptr + PAGE_CACHE_SIZE < ptr + copy_size); |
| 985 | memcpy(ptr, data_ptr, copy_size); |
| 986 | pos += copy_size; |
| 987 | num_bytes -= copy_size; |
| 988 | WARN_ON(num_bytes < 0); |
| 989 | ptr += copy_size; |
| 990 | btrfs_release_path(root, path); |
| 991 | if (num_bytes != 0) { |
| 992 | if (pos >= i_size_read(inode)) |
| 993 | memset(ptr, 0, num_bytes); |
| 994 | else |
| 995 | goto again; |
| 996 | } |
| 997 | set_buffer_uptodate(result); |
| 998 | map_bh(result, inode->i_sb, 0); |
| 999 | err = 0; |
| 1000 | out: |
| 1001 | btrfs_free_path(path); |
| 1002 | kunmap(result->b_page); |
| 1003 | return err; |
| 1004 | } |
Chris Mason | e8f05c4 | 2007-04-04 14:30:09 -0400 | [diff] [blame] | 1005 | #endif |
Chris Mason | df24a2b | 2007-04-04 09:36:31 -0400 | [diff] [blame] | 1006 | |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1007 | static int btrfs_get_block_lock(struct inode *inode, sector_t iblock, |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1008 | struct buffer_head *result, int create) |
| 1009 | { |
| 1010 | int ret; |
| 1011 | int err = 0; |
| 1012 | u64 blocknr; |
| 1013 | u64 extent_start = 0; |
| 1014 | u64 extent_end = 0; |
| 1015 | u64 objectid = inode->i_ino; |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame^] | 1016 | u32 found_type; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1017 | struct btrfs_path *path; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 1018 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1019 | struct btrfs_file_extent_item *item; |
| 1020 | struct btrfs_leaf *leaf; |
| 1021 | struct btrfs_disk_key *found_key; |
| 1022 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1023 | path = btrfs_alloc_path(); |
| 1024 | BUG_ON(!path); |
| 1025 | btrfs_init_path(path); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 1026 | if (create) { |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 1027 | WARN_ON(1); |
| 1028 | } |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1029 | |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame^] | 1030 | ret = btrfs_lookup_file_extent(NULL, root, path, |
Chris Mason | 9773a78 | 2007-03-27 11:26:26 -0400 | [diff] [blame] | 1031 | inode->i_ino, |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame^] | 1032 | iblock << inode->i_blkbits, 0); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1033 | if (ret < 0) { |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1034 | err = ret; |
| 1035 | goto out; |
| 1036 | } |
| 1037 | |
| 1038 | if (ret != 0) { |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1039 | if (path->slots[0] == 0) { |
| 1040 | btrfs_release_path(root, path); |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame^] | 1041 | goto out; |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1042 | } |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1043 | path->slots[0]--; |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1044 | } |
| 1045 | |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1046 | 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] | 1047 | struct btrfs_file_extent_item); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1048 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1049 | blocknr = btrfs_file_extent_disk_blocknr(item); |
| 1050 | blocknr += btrfs_file_extent_offset(item); |
| 1051 | |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1052 | /* are we inside the extent that was found? */ |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1053 | found_key = &leaf->items[path->slots[0]].key; |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame^] | 1054 | found_type = btrfs_disk_key_type(found_key); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1055 | if (btrfs_disk_key_objectid(found_key) != objectid || |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame^] | 1056 | found_type != BTRFS_EXTENT_DATA_KEY) { |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1057 | extent_end = 0; |
| 1058 | extent_start = 0; |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1059 | btrfs_release_path(root, path); |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame^] | 1060 | goto out; |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1061 | } |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame^] | 1062 | found_type = btrfs_file_extent_type(item); |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1063 | extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key); |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame^] | 1064 | if (found_type == BTRFS_FILE_EXTENT_REG) { |
| 1065 | extent_start = extent_start >> inode->i_blkbits; |
| 1066 | extent_end = extent_start + btrfs_file_extent_num_blocks(item); |
| 1067 | if (iblock >= extent_start && iblock < extent_end) { |
| 1068 | err = 0; |
| 1069 | btrfs_map_bh_to_logical(root, result, blocknr + |
| 1070 | iblock - extent_start); |
| 1071 | goto out; |
| 1072 | } |
| 1073 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
| 1074 | char *ptr; |
| 1075 | char *map; |
| 1076 | u32 size; |
| 1077 | size = btrfs_file_extent_inline_len(leaf->items + |
| 1078 | path->slots[0]); |
| 1079 | extent_end = (extent_start + size) >> inode->i_blkbits; |
| 1080 | extent_start >>= inode->i_blkbits; |
| 1081 | if (iblock < extent_start || iblock > extent_end) { |
| 1082 | goto out; |
| 1083 | } |
| 1084 | ptr = btrfs_file_extent_inline_start(item); |
| 1085 | map = kmap(result->b_page); |
| 1086 | memcpy(map, ptr, size); |
| 1087 | memset(map + size, 0, PAGE_CACHE_SIZE - size); |
| 1088 | flush_dcache_page(result->b_page); |
| 1089 | kunmap(result->b_page); |
| 1090 | set_buffer_uptodate(result); |
| 1091 | SetPageChecked(result->b_page); |
| 1092 | btrfs_map_bh_to_logical(root, result, 0); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1093 | } |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1094 | out: |
Chris Mason | 5caf2a0 | 2007-04-02 11:20:42 -0400 | [diff] [blame] | 1095 | btrfs_release_path(root, path); |
| 1096 | btrfs_free_path(path); |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1097 | return err; |
| 1098 | } |
| 1099 | |
| 1100 | static int btrfs_get_block(struct inode *inode, sector_t iblock, |
| 1101 | struct buffer_head *result, int create) |
| 1102 | { |
| 1103 | int err; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 1104 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1105 | mutex_lock(&root->fs_info->fs_mutex); |
Chris Mason | e8f05c4 | 2007-04-04 14:30:09 -0400 | [diff] [blame] | 1106 | err = btrfs_get_block_lock(inode, iblock, result, create); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1107 | mutex_unlock(&root->fs_info->fs_mutex); |
| 1108 | return err; |
| 1109 | } |
| 1110 | |
| 1111 | static int btrfs_prepare_write(struct file *file, struct page *page, |
| 1112 | unsigned from, unsigned to) |
| 1113 | { |
Chris Mason | 6407bf6 | 2007-03-27 06:33:00 -0400 | [diff] [blame] | 1114 | return nobh_prepare_write(page, from, to, btrfs_get_block); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1115 | } |
| 1116 | |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 1117 | static void btrfs_write_super(struct super_block *sb) |
| 1118 | { |
| 1119 | btrfs_sync_fs(sb, 1); |
| 1120 | } |
| 1121 | |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1122 | static int btrfs_readpage(struct file *file, struct page *page) |
| 1123 | { |
| 1124 | return mpage_readpage(page, btrfs_get_block); |
| 1125 | } |
| 1126 | |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame^] | 1127 | /* |
| 1128 | * While block_write_full_page is writing back the dirty buffers under |
| 1129 | * the page lock, whoever dirtied the buffers may decide to clean them |
| 1130 | * again at any time. We handle that by only looking at the buffer |
| 1131 | * state inside lock_buffer(). |
| 1132 | * |
| 1133 | * If block_write_full_page() is called for regular writeback |
| 1134 | * (wbc->sync_mode == WB_SYNC_NONE) then it will redirty a page which has a |
| 1135 | * locked buffer. This only can happen if someone has written the buffer |
| 1136 | * directly, with submit_bh(). At the address_space level PageWriteback |
| 1137 | * prevents this contention from occurring. |
| 1138 | */ |
| 1139 | static int __btrfs_write_full_page(struct inode *inode, struct page *page, |
| 1140 | struct writeback_control *wbc) |
| 1141 | { |
| 1142 | int err; |
| 1143 | sector_t block; |
| 1144 | sector_t last_block; |
| 1145 | struct buffer_head *bh, *head; |
| 1146 | const unsigned blocksize = 1 << inode->i_blkbits; |
| 1147 | int nr_underway = 0; |
| 1148 | |
| 1149 | BUG_ON(!PageLocked(page)); |
| 1150 | |
| 1151 | last_block = (i_size_read(inode) - 1) >> inode->i_blkbits; |
| 1152 | |
| 1153 | if (!page_has_buffers(page)) { |
| 1154 | create_empty_buffers(page, blocksize, |
| 1155 | (1 << BH_Dirty)|(1 << BH_Uptodate)); |
| 1156 | } |
| 1157 | |
| 1158 | /* |
| 1159 | * Be very careful. We have no exclusion from __set_page_dirty_buffers |
| 1160 | * here, and the (potentially unmapped) buffers may become dirty at |
| 1161 | * any time. If a buffer becomes dirty here after we've inspected it |
| 1162 | * then we just miss that fact, and the page stays dirty. |
| 1163 | * |
| 1164 | * Buffers outside i_size may be dirtied by __set_page_dirty_buffers; |
| 1165 | * handle that here by just cleaning them. |
| 1166 | */ |
| 1167 | |
| 1168 | block = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits); |
| 1169 | head = page_buffers(page); |
| 1170 | bh = head; |
| 1171 | |
| 1172 | /* |
| 1173 | * Get all the dirty buffers mapped to disk addresses and |
| 1174 | * handle any aliases from the underlying blockdev's mapping. |
| 1175 | */ |
| 1176 | do { |
| 1177 | if (block > last_block) { |
| 1178 | /* |
| 1179 | * mapped buffers outside i_size will occur, because |
| 1180 | * this page can be outside i_size when there is a |
| 1181 | * truncate in progress. |
| 1182 | */ |
| 1183 | /* |
| 1184 | * The buffer was zeroed by block_write_full_page() |
| 1185 | */ |
| 1186 | clear_buffer_dirty(bh); |
| 1187 | set_buffer_uptodate(bh); |
| 1188 | } else if (!buffer_mapped(bh) && buffer_dirty(bh)) { |
| 1189 | WARN_ON(bh->b_size != blocksize); |
| 1190 | err = btrfs_get_block(inode, block, bh, 0); |
| 1191 | if (err) |
| 1192 | goto recover; |
| 1193 | if (buffer_new(bh)) { |
| 1194 | /* blockdev mappings never come here */ |
| 1195 | clear_buffer_new(bh); |
| 1196 | unmap_underlying_metadata(bh->b_bdev, |
| 1197 | bh->b_blocknr); |
| 1198 | } |
| 1199 | } |
| 1200 | bh = bh->b_this_page; |
| 1201 | block++; |
| 1202 | } while (bh != head); |
| 1203 | |
| 1204 | do { |
| 1205 | if (!buffer_mapped(bh)) |
| 1206 | continue; |
| 1207 | /* |
| 1208 | * If it's a fully non-blocking write attempt and we cannot |
| 1209 | * lock the buffer then redirty the page. Note that this can |
| 1210 | * potentially cause a busy-wait loop from pdflush and kswapd |
| 1211 | * activity, but those code paths have their own higher-level |
| 1212 | * throttling. |
| 1213 | */ |
| 1214 | if (wbc->sync_mode != WB_SYNC_NONE || !wbc->nonblocking) { |
| 1215 | lock_buffer(bh); |
| 1216 | } else if (test_set_buffer_locked(bh)) { |
| 1217 | redirty_page_for_writepage(wbc, page); |
| 1218 | continue; |
| 1219 | } |
| 1220 | if (test_clear_buffer_dirty(bh) && bh->b_blocknr != 0) { |
| 1221 | mark_buffer_async_write(bh); |
| 1222 | } else { |
| 1223 | unlock_buffer(bh); |
| 1224 | } |
| 1225 | } while ((bh = bh->b_this_page) != head); |
| 1226 | |
| 1227 | /* |
| 1228 | * The page and its buffers are protected by PageWriteback(), so we can |
| 1229 | * drop the bh refcounts early. |
| 1230 | */ |
| 1231 | BUG_ON(PageWriteback(page)); |
| 1232 | set_page_writeback(page); |
| 1233 | |
| 1234 | do { |
| 1235 | struct buffer_head *next = bh->b_this_page; |
| 1236 | if (buffer_async_write(bh)) { |
| 1237 | submit_bh(WRITE, bh); |
| 1238 | nr_underway++; |
| 1239 | } |
| 1240 | bh = next; |
| 1241 | } while (bh != head); |
| 1242 | unlock_page(page); |
| 1243 | |
| 1244 | err = 0; |
| 1245 | done: |
| 1246 | if (nr_underway == 0) { |
| 1247 | /* |
| 1248 | * The page was marked dirty, but the buffers were |
| 1249 | * clean. Someone wrote them back by hand with |
| 1250 | * ll_rw_block/submit_bh. A rare case. |
| 1251 | */ |
| 1252 | int uptodate = 1; |
| 1253 | do { |
| 1254 | if (!buffer_uptodate(bh)) { |
| 1255 | uptodate = 0; |
| 1256 | break; |
| 1257 | } |
| 1258 | bh = bh->b_this_page; |
| 1259 | } while (bh != head); |
| 1260 | if (uptodate) |
| 1261 | SetPageUptodate(page); |
| 1262 | end_page_writeback(page); |
| 1263 | /* |
| 1264 | * The page and buffer_heads can be released at any time from |
| 1265 | * here on. |
| 1266 | */ |
| 1267 | wbc->pages_skipped++; /* We didn't write this page */ |
| 1268 | } |
| 1269 | return err; |
| 1270 | |
| 1271 | recover: |
| 1272 | /* |
| 1273 | * ENOSPC, or some other error. We may already have added some |
| 1274 | * blocks to the file, so we need to write these out to avoid |
| 1275 | * exposing stale data. |
| 1276 | * The page is currently locked and not marked for writeback |
| 1277 | */ |
| 1278 | bh = head; |
| 1279 | /* Recovery: lock and submit the mapped buffers */ |
| 1280 | do { |
| 1281 | if (buffer_mapped(bh) && buffer_dirty(bh)) { |
| 1282 | lock_buffer(bh); |
| 1283 | mark_buffer_async_write(bh); |
| 1284 | } else { |
| 1285 | /* |
| 1286 | * The buffer may have been set dirty during |
| 1287 | * attachment to a dirty page. |
| 1288 | */ |
| 1289 | clear_buffer_dirty(bh); |
| 1290 | } |
| 1291 | } while ((bh = bh->b_this_page) != head); |
| 1292 | SetPageError(page); |
| 1293 | BUG_ON(PageWriteback(page)); |
| 1294 | set_page_writeback(page); |
| 1295 | do { |
| 1296 | struct buffer_head *next = bh->b_this_page; |
| 1297 | if (buffer_async_write(bh)) { |
| 1298 | clear_buffer_dirty(bh); |
| 1299 | submit_bh(WRITE, bh); |
| 1300 | nr_underway++; |
| 1301 | } |
| 1302 | bh = next; |
| 1303 | } while (bh != head); |
| 1304 | unlock_page(page); |
| 1305 | goto done; |
| 1306 | } |
| 1307 | |
| 1308 | /* |
| 1309 | * The generic ->writepage function for buffer-backed address_spaces |
| 1310 | */ |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1311 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
| 1312 | { |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame^] | 1313 | struct inode * const inode = page->mapping->host; |
| 1314 | loff_t i_size = i_size_read(inode); |
| 1315 | const pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT; |
| 1316 | unsigned offset; |
| 1317 | void *kaddr; |
| 1318 | |
| 1319 | /* Is the page fully inside i_size? */ |
| 1320 | if (page->index < end_index) |
| 1321 | return __btrfs_write_full_page(inode, page, wbc); |
| 1322 | |
| 1323 | /* Is the page fully outside i_size? (truncate in progress) */ |
| 1324 | offset = i_size & (PAGE_CACHE_SIZE-1); |
| 1325 | if (page->index >= end_index+1 || !offset) { |
| 1326 | /* |
| 1327 | * The page may have dirty, unmapped buffers. For example, |
| 1328 | * they may have been added in ext3_writepage(). Make them |
| 1329 | * freeable here, so the page does not leak. |
| 1330 | */ |
| 1331 | block_invalidatepage(page, 0); |
| 1332 | unlock_page(page); |
| 1333 | return 0; /* don't care */ |
| 1334 | } |
| 1335 | |
| 1336 | /* |
| 1337 | * The page straddles i_size. It must be zeroed out on each and every |
| 1338 | * writepage invokation because it may be mmapped. "A file is mapped |
| 1339 | * in multiples of the page size. For a file that is not a multiple of |
| 1340 | * the page size, the remaining memory is zeroed when mapped, and |
| 1341 | * writes to that region are not written out to the file." |
| 1342 | */ |
| 1343 | kaddr = kmap_atomic(page, KM_USER0); |
| 1344 | memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset); |
| 1345 | flush_dcache_page(page); |
| 1346 | kunmap_atomic(kaddr, KM_USER0); |
| 1347 | return __btrfs_write_full_page(inode, page, wbc); |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 1348 | } |
Chris Mason | d561c02 | 2007-03-23 19:47:49 -0400 | [diff] [blame] | 1349 | |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 1350 | static void btrfs_truncate(struct inode *inode) |
| 1351 | { |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 1352 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 1353 | int ret; |
| 1354 | struct btrfs_trans_handle *trans; |
| 1355 | |
| 1356 | if (!S_ISREG(inode->i_mode)) |
| 1357 | return; |
| 1358 | if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) |
| 1359 | return; |
| 1360 | |
Chris Mason | e8f05c4 | 2007-04-04 14:30:09 -0400 | [diff] [blame] | 1361 | nobh_truncate_page(inode->i_mapping, inode->i_size); |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 1362 | |
| 1363 | /* FIXME, add redo link to tree so we don't leak on crash */ |
| 1364 | mutex_lock(&root->fs_info->fs_mutex); |
| 1365 | trans = btrfs_start_transaction(root, 1); |
| 1366 | ret = btrfs_truncate_in_trans(trans, root, inode); |
| 1367 | BUG_ON(ret); |
| 1368 | ret = btrfs_end_transaction(trans, root); |
| 1369 | BUG_ON(ret); |
| 1370 | mutex_unlock(&root->fs_info->fs_mutex); |
| 1371 | mark_inode_dirty(inode); |
| 1372 | } |
| 1373 | |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame^] | 1374 | /* |
| 1375 | * Make sure any changes to nobh_commit_write() are reflected in |
| 1376 | * nobh_truncate_page(), since it doesn't call commit_write(). |
| 1377 | */ |
| 1378 | static int btrfs_commit_write(struct file *file, struct page *page, |
| 1379 | unsigned from, unsigned to) |
| 1380 | { |
| 1381 | struct inode *inode = page->mapping->host; |
| 1382 | struct buffer_head *bh; |
| 1383 | loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to; |
| 1384 | |
| 1385 | SetPageUptodate(page); |
| 1386 | bh = page_buffers(page); |
| 1387 | if (buffer_mapped(bh) && bh->b_blocknr != 0) { |
| 1388 | set_page_dirty(page); |
| 1389 | } |
| 1390 | if (pos > inode->i_size) { |
| 1391 | i_size_write(inode, pos); |
| 1392 | mark_inode_dirty(inode); |
| 1393 | } |
| 1394 | return 0; |
| 1395 | } |
| 1396 | |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1397 | static int btrfs_copy_from_user(loff_t pos, int num_pages, int write_bytes, |
| 1398 | struct page **prepared_pages, |
| 1399 | const char __user * buf) |
| 1400 | { |
| 1401 | long page_fault = 0; |
| 1402 | int i; |
| 1403 | int offset = pos & (PAGE_CACHE_SIZE - 1); |
| 1404 | |
| 1405 | for (i = 0; i < num_pages && write_bytes > 0; i++, offset = 0) { |
| 1406 | size_t count = min_t(size_t, |
| 1407 | PAGE_CACHE_SIZE - offset, write_bytes); |
| 1408 | struct page *page = prepared_pages[i]; |
| 1409 | fault_in_pages_readable(buf, count); |
| 1410 | |
| 1411 | /* Copy data from userspace to the current page */ |
| 1412 | kmap(page); |
| 1413 | page_fault = __copy_from_user(page_address(page) + offset, |
| 1414 | buf, count); |
| 1415 | /* Flush processor's dcache for this page */ |
| 1416 | flush_dcache_page(page); |
| 1417 | kunmap(page); |
| 1418 | buf += count; |
| 1419 | write_bytes -= count; |
| 1420 | |
| 1421 | if (page_fault) |
| 1422 | break; |
| 1423 | } |
| 1424 | return page_fault ? -EFAULT : 0; |
| 1425 | } |
| 1426 | |
| 1427 | static void btrfs_drop_pages(struct page **pages, size_t num_pages) |
| 1428 | { |
| 1429 | size_t i; |
| 1430 | for (i = 0; i < num_pages; i++) { |
| 1431 | if (!pages[i]) |
| 1432 | break; |
| 1433 | unlock_page(pages[i]); |
| 1434 | mark_page_accessed(pages[i]); |
| 1435 | page_cache_release(pages[i]); |
| 1436 | } |
| 1437 | } |
| 1438 | static int dirty_and_release_pages(struct btrfs_trans_handle *trans, |
| 1439 | struct btrfs_root *root, |
| 1440 | struct file *file, |
| 1441 | struct page **pages, |
| 1442 | size_t num_pages, |
| 1443 | loff_t pos, |
| 1444 | size_t write_bytes) |
| 1445 | { |
| 1446 | int i; |
| 1447 | int offset; |
| 1448 | int err = 0; |
| 1449 | int ret; |
| 1450 | int this_write; |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 1451 | struct inode *inode = file->f_path.dentry->d_inode; |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame^] | 1452 | struct buffer_head *bh; |
| 1453 | struct btrfs_file_extent_item *ei; |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1454 | |
| 1455 | for (i = 0; i < num_pages; i++) { |
| 1456 | offset = pos & (PAGE_CACHE_SIZE -1); |
| 1457 | this_write = min(PAGE_CACHE_SIZE - offset, write_bytes); |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 1458 | /* FIXME, one block at a time */ |
| 1459 | |
| 1460 | mutex_lock(&root->fs_info->fs_mutex); |
| 1461 | trans = btrfs_start_transaction(root, 1); |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame^] | 1462 | |
| 1463 | bh = page_buffers(pages[i]); |
| 1464 | if (buffer_mapped(bh) && bh->b_blocknr == 0) { |
| 1465 | struct btrfs_key key; |
| 1466 | struct btrfs_path *path; |
| 1467 | char *ptr; |
| 1468 | u32 datasize; |
| 1469 | |
| 1470 | path = btrfs_alloc_path(); |
| 1471 | BUG_ON(!path); |
| 1472 | key.objectid = inode->i_ino; |
| 1473 | key.offset = pages[i]->index << PAGE_CACHE_SHIFT; |
| 1474 | key.flags = 0; |
| 1475 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); |
| 1476 | BUG_ON(write_bytes >= PAGE_CACHE_SIZE); |
| 1477 | datasize = offset + |
| 1478 | btrfs_file_extent_calc_inline_size(write_bytes); |
| 1479 | ret = btrfs_insert_empty_item(trans, root, path, &key, |
| 1480 | datasize); |
| 1481 | BUG_ON(ret); |
| 1482 | ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), |
| 1483 | path->slots[0], struct btrfs_file_extent_item); |
| 1484 | btrfs_set_file_extent_generation(ei, trans->transid); |
| 1485 | btrfs_set_file_extent_type(ei, |
| 1486 | BTRFS_FILE_EXTENT_INLINE); |
| 1487 | ptr = btrfs_file_extent_inline_start(ei); |
| 1488 | memcpy(ptr, bh->b_data, offset + write_bytes); |
| 1489 | mark_buffer_dirty(path->nodes[0]); |
| 1490 | btrfs_free_path(path); |
| 1491 | } else { |
| 1492 | btrfs_csum_file_block(trans, root, inode->i_ino, |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 1493 | pages[i]->index << PAGE_CACHE_SHIFT, |
| 1494 | kmap(pages[i]), PAGE_CACHE_SIZE); |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame^] | 1495 | kunmap(pages[i]); |
| 1496 | } |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 1497 | SetPageChecked(pages[i]); |
| 1498 | ret = btrfs_end_transaction(trans, root); |
| 1499 | BUG_ON(ret); |
| 1500 | mutex_unlock(&root->fs_info->fs_mutex); |
| 1501 | |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame^] | 1502 | ret = btrfs_commit_write(file, pages[i], offset, |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1503 | offset + this_write); |
| 1504 | pos += this_write; |
| 1505 | if (ret) { |
| 1506 | err = ret; |
| 1507 | goto failed; |
| 1508 | } |
| 1509 | WARN_ON(this_write > write_bytes); |
| 1510 | write_bytes -= this_write; |
| 1511 | } |
| 1512 | failed: |
| 1513 | return err; |
| 1514 | } |
| 1515 | |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1516 | static int drop_extents(struct btrfs_trans_handle *trans, |
| 1517 | struct btrfs_root *root, |
| 1518 | struct inode *inode, |
| 1519 | u64 start, u64 end) |
| 1520 | { |
| 1521 | int ret; |
| 1522 | struct btrfs_key key; |
| 1523 | struct btrfs_leaf *leaf; |
| 1524 | int slot; |
| 1525 | struct btrfs_file_extent_item *extent; |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame^] | 1526 | u64 extent_end = 0; |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1527 | int keep; |
| 1528 | struct btrfs_file_extent_item old; |
| 1529 | struct btrfs_path *path; |
| 1530 | u64 search_start = start; |
| 1531 | int bookend; |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame^] | 1532 | int found_type; |
| 1533 | int found_extent; |
| 1534 | int found_inline; |
| 1535 | |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1536 | path = btrfs_alloc_path(); |
| 1537 | if (!path) |
| 1538 | return -ENOMEM; |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1539 | while(1) { |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 1540 | btrfs_release_path(root, path); |
| 1541 | ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino, |
| 1542 | search_start, -1); |
| 1543 | if (ret < 0) |
| 1544 | goto out; |
| 1545 | if (ret > 0) { |
| 1546 | if (path->slots[0] == 0) { |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame^] | 1547 | ret = 0; |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 1548 | goto out; |
| 1549 | } |
| 1550 | path->slots[0]--; |
| 1551 | } |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1552 | keep = 0; |
| 1553 | bookend = 0; |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame^] | 1554 | found_extent = 0; |
| 1555 | found_inline = 0; |
| 1556 | extent = NULL; |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1557 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
| 1558 | slot = path->slots[0]; |
| 1559 | btrfs_disk_key_to_cpu(&key, &leaf->items[slot].key); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1560 | if (key.offset >= end || key.objectid != inode->i_ino) { |
| 1561 | ret = 0; |
| 1562 | goto out; |
| 1563 | } |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame^] | 1564 | if (btrfs_key_type(&key) != BTRFS_EXTENT_DATA_KEY) { |
| 1565 | ret = 0; |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 1566 | goto out; |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame^] | 1567 | } |
| 1568 | extent = btrfs_item_ptr(leaf, slot, |
| 1569 | struct btrfs_file_extent_item); |
| 1570 | found_type = btrfs_file_extent_type(extent); |
| 1571 | if (found_type == BTRFS_FILE_EXTENT_REG) { |
| 1572 | extent_end = key.offset + |
| 1573 | (btrfs_file_extent_num_blocks(extent) << |
| 1574 | inode->i_blkbits); |
| 1575 | found_extent = 1; |
| 1576 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
| 1577 | found_inline = 1; |
| 1578 | extent_end = key.offset + |
| 1579 | btrfs_file_extent_inline_len(leaf->items + slot); |
| 1580 | } |
| 1581 | |
| 1582 | if (!found_extent && !found_inline) { |
| 1583 | ret = 0; |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 1584 | goto out; |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame^] | 1585 | } |
| 1586 | |
| 1587 | if (search_start >= extent_end) { |
| 1588 | ret = 0; |
| 1589 | goto out; |
| 1590 | } |
| 1591 | |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 1592 | search_start = extent_end; |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1593 | |
| 1594 | if (end < extent_end && end >= key.offset) { |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame^] | 1595 | if (found_extent) { |
| 1596 | memcpy(&old, extent, sizeof(old)); |
| 1597 | ret = btrfs_inc_extent_ref(trans, root, |
| 1598 | btrfs_file_extent_disk_blocknr(&old), |
| 1599 | btrfs_file_extent_disk_num_blocks(&old)); |
| 1600 | BUG_ON(ret); |
| 1601 | } |
| 1602 | WARN_ON(found_inline); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1603 | bookend = 1; |
| 1604 | } |
| 1605 | |
| 1606 | if (start > key.offset) { |
| 1607 | u64 new_num; |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 1608 | u64 old_num; |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1609 | /* truncate existing extent */ |
| 1610 | keep = 1; |
| 1611 | WARN_ON(start & (root->blocksize - 1)); |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame^] | 1612 | if (found_extent) { |
| 1613 | new_num = (start - key.offset) >> |
| 1614 | inode->i_blkbits; |
| 1615 | old_num = btrfs_file_extent_num_blocks(extent); |
| 1616 | inode->i_blocks -= (old_num - new_num) << 3; |
| 1617 | btrfs_set_file_extent_num_blocks(extent, |
| 1618 | new_num); |
| 1619 | mark_buffer_dirty(path->nodes[0]); |
| 1620 | } else { |
| 1621 | WARN_ON(1); |
| 1622 | /* |
| 1623 | ret = btrfs_truncate_item(trans, root, path, |
| 1624 | start - key.offset); |
| 1625 | BUG_ON(ret); |
| 1626 | */ |
| 1627 | } |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1628 | } |
| 1629 | if (!keep) { |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame^] | 1630 | u64 disk_blocknr = 0; |
| 1631 | u64 disk_num_blocks = 0; |
| 1632 | u64 extent_num_blocks = 0; |
| 1633 | if (found_extent) { |
| 1634 | disk_blocknr = |
| 1635 | btrfs_file_extent_disk_blocknr(extent); |
| 1636 | disk_num_blocks = |
| 1637 | btrfs_file_extent_disk_num_blocks(extent); |
| 1638 | extent_num_blocks = |
| 1639 | btrfs_file_extent_num_blocks(extent); |
| 1640 | } |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1641 | ret = btrfs_del_item(trans, root, path); |
| 1642 | BUG_ON(ret); |
| 1643 | btrfs_release_path(root, path); |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame^] | 1644 | if (found_extent) { |
| 1645 | inode->i_blocks -= |
| 1646 | btrfs_file_extent_num_blocks(extent) << 3; |
| 1647 | ret = btrfs_free_extent(trans, root, |
| 1648 | disk_blocknr, |
| 1649 | disk_num_blocks, 0); |
| 1650 | } |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1651 | |
| 1652 | BUG_ON(ret); |
| 1653 | if (!bookend && search_start >= end) { |
| 1654 | ret = 0; |
| 1655 | goto out; |
| 1656 | } |
| 1657 | if (!bookend) |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 1658 | continue; |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1659 | } |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame^] | 1660 | if (bookend && found_extent) { |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1661 | /* create bookend */ |
| 1662 | struct btrfs_key ins; |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1663 | ins.objectid = inode->i_ino; |
| 1664 | ins.offset = end; |
| 1665 | ins.flags = 0; |
| 1666 | btrfs_set_key_type(&ins, BTRFS_EXTENT_DATA_KEY); |
| 1667 | |
| 1668 | btrfs_release_path(root, path); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1669 | ret = btrfs_insert_empty_item(trans, root, path, &ins, |
| 1670 | sizeof(*extent)); |
| 1671 | BUG_ON(ret); |
| 1672 | extent = btrfs_item_ptr( |
| 1673 | btrfs_buffer_leaf(path->nodes[0]), |
| 1674 | path->slots[0], |
| 1675 | struct btrfs_file_extent_item); |
| 1676 | btrfs_set_file_extent_disk_blocknr(extent, |
| 1677 | btrfs_file_extent_disk_blocknr(&old)); |
| 1678 | btrfs_set_file_extent_disk_num_blocks(extent, |
| 1679 | btrfs_file_extent_disk_num_blocks(&old)); |
| 1680 | |
| 1681 | btrfs_set_file_extent_offset(extent, |
| 1682 | btrfs_file_extent_offset(&old) + |
| 1683 | ((end - key.offset) >> inode->i_blkbits)); |
| 1684 | WARN_ON(btrfs_file_extent_num_blocks(&old) < |
| 1685 | (end - key.offset) >> inode->i_blkbits); |
| 1686 | btrfs_set_file_extent_num_blocks(extent, |
| 1687 | btrfs_file_extent_num_blocks(&old) - |
| 1688 | ((end - key.offset) >> inode->i_blkbits)); |
| 1689 | |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame^] | 1690 | btrfs_set_file_extent_type(extent, |
| 1691 | BTRFS_FILE_EXTENT_REG); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1692 | btrfs_set_file_extent_generation(extent, |
| 1693 | btrfs_file_extent_generation(&old)); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1694 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 1695 | inode->i_blocks += |
| 1696 | btrfs_file_extent_num_blocks(extent) << 3; |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1697 | ret = 0; |
Chris Mason | 70b2bef | 2007-04-17 15:39:32 -0400 | [diff] [blame] | 1698 | goto out; |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1699 | } |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1700 | } |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1701 | out: |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1702 | btrfs_free_path(path); |
| 1703 | return ret; |
| 1704 | } |
| 1705 | |
| 1706 | static int prepare_pages(struct btrfs_root *root, |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1707 | struct file *file, |
| 1708 | struct page **pages, |
| 1709 | size_t num_pages, |
| 1710 | loff_t pos, |
Chris Mason | 2932f3e | 2007-04-10 14:22:02 -0400 | [diff] [blame] | 1711 | unsigned long first_index, |
| 1712 | unsigned long last_index, |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 1713 | size_t write_bytes, |
| 1714 | u64 alloc_extent_start) |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1715 | { |
| 1716 | int i; |
| 1717 | unsigned long index = pos >> PAGE_CACHE_SHIFT; |
| 1718 | struct inode *inode = file->f_path.dentry->d_inode; |
| 1719 | int offset; |
| 1720 | int err = 0; |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1721 | int this_write; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 1722 | struct buffer_head *bh; |
| 1723 | struct buffer_head *head; |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1724 | loff_t isize = i_size_read(inode); |
| 1725 | |
| 1726 | memset(pages, 0, num_pages * sizeof(struct page *)); |
| 1727 | |
| 1728 | for (i = 0; i < num_pages; i++) { |
| 1729 | pages[i] = grab_cache_page(inode->i_mapping, index + i); |
| 1730 | if (!pages[i]) { |
| 1731 | err = -ENOMEM; |
| 1732 | goto failed_release; |
| 1733 | } |
| 1734 | offset = pos & (PAGE_CACHE_SIZE -1); |
| 1735 | this_write = min(PAGE_CACHE_SIZE - offset, write_bytes); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 1736 | create_empty_buffers(pages[i], root->fs_info->sb->s_blocksize, |
| 1737 | (1 << BH_Uptodate)); |
| 1738 | head = page_buffers(pages[i]); |
| 1739 | bh = head; |
| 1740 | do { |
| 1741 | err = btrfs_map_bh_to_logical(root, bh, |
| 1742 | alloc_extent_start); |
| 1743 | BUG_ON(err); |
| 1744 | if (err) |
| 1745 | goto failed_truncate; |
| 1746 | bh = bh->b_this_page; |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame^] | 1747 | if (alloc_extent_start) |
| 1748 | alloc_extent_start++; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 1749 | } while (bh != head); |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1750 | pos += this_write; |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1751 | WARN_ON(this_write > write_bytes); |
| 1752 | write_bytes -= this_write; |
| 1753 | } |
| 1754 | return 0; |
| 1755 | |
| 1756 | failed_release: |
| 1757 | btrfs_drop_pages(pages, num_pages); |
| 1758 | return err; |
| 1759 | |
| 1760 | failed_truncate: |
| 1761 | btrfs_drop_pages(pages, num_pages); |
| 1762 | if (pos > isize) |
| 1763 | vmtruncate(inode, isize); |
| 1764 | return err; |
| 1765 | } |
| 1766 | |
| 1767 | static ssize_t btrfs_file_write(struct file *file, const char __user *buf, |
| 1768 | size_t count, loff_t *ppos) |
| 1769 | { |
| 1770 | loff_t pos; |
| 1771 | size_t num_written = 0; |
| 1772 | int err = 0; |
| 1773 | int ret = 0; |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1774 | struct inode *inode = file->f_path.dentry->d_inode; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 1775 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1776 | struct page *pages[8]; |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 1777 | struct page *pinned[2] = { NULL, NULL }; |
Chris Mason | 2932f3e | 2007-04-10 14:22:02 -0400 | [diff] [blame] | 1778 | unsigned long first_index; |
| 1779 | unsigned long last_index; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 1780 | u64 start_pos; |
| 1781 | u64 num_blocks; |
| 1782 | u64 alloc_extent_start; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 1783 | struct btrfs_trans_handle *trans; |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1784 | struct btrfs_key ins; |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1785 | |
| 1786 | if (file->f_flags & O_DIRECT) |
| 1787 | return -EINVAL; |
| 1788 | pos = *ppos; |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1789 | vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE); |
| 1790 | current->backing_dev_info = inode->i_mapping->backing_dev_info; |
| 1791 | err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode)); |
| 1792 | if (err) |
| 1793 | goto out; |
| 1794 | if (count == 0) |
| 1795 | goto out; |
| 1796 | err = remove_suid(file->f_path.dentry); |
| 1797 | if (err) |
| 1798 | goto out; |
| 1799 | file_update_time(file); |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 1800 | |
| 1801 | start_pos = pos & ~((u64)PAGE_CACHE_SIZE - 1); |
| 1802 | num_blocks = (count + pos - start_pos + root->blocksize - 1) >> |
| 1803 | inode->i_blkbits; |
| 1804 | |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1805 | mutex_lock(&inode->i_mutex); |
Chris Mason | 2932f3e | 2007-04-10 14:22:02 -0400 | [diff] [blame] | 1806 | first_index = pos >> PAGE_CACHE_SHIFT; |
| 1807 | last_index = (pos + count) >> PAGE_CACHE_SHIFT; |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 1808 | |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 1809 | if ((first_index << PAGE_CACHE_SHIFT) < inode->i_size && |
| 1810 | (pos & (PAGE_CACHE_SIZE - 1))) { |
| 1811 | pinned[0] = grab_cache_page(inode->i_mapping, first_index); |
| 1812 | if (!PageUptodate(pinned[0])) { |
| 1813 | ret = mpage_readpage(pinned[0], btrfs_get_block); |
| 1814 | BUG_ON(ret); |
| 1815 | } else { |
| 1816 | unlock_page(pinned[0]); |
| 1817 | } |
| 1818 | } |
| 1819 | if (first_index != last_index && |
| 1820 | (last_index << PAGE_CACHE_SHIFT) < inode->i_size && |
| 1821 | (count & (PAGE_CACHE_SIZE - 1))) { |
| 1822 | pinned[1] = grab_cache_page(inode->i_mapping, last_index); |
| 1823 | if (!PageUptodate(pinned[1])) { |
| 1824 | ret = mpage_readpage(pinned[1], btrfs_get_block); |
| 1825 | BUG_ON(ret); |
| 1826 | } else { |
| 1827 | unlock_page(pinned[1]); |
| 1828 | } |
| 1829 | } |
| 1830 | |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 1831 | mutex_lock(&root->fs_info->fs_mutex); |
| 1832 | trans = btrfs_start_transaction(root, 1); |
| 1833 | if (!trans) { |
| 1834 | err = -ENOMEM; |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1835 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 1836 | goto out_unlock; |
| 1837 | } |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 1838 | /* FIXME blocksize != 4096 */ |
| 1839 | inode->i_blocks += num_blocks << 3; |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1840 | if (start_pos < inode->i_size) { |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 1841 | /* FIXME blocksize != pagesize */ |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1842 | ret = drop_extents(trans, root, inode, |
| 1843 | start_pos, |
| 1844 | (pos + count + root->blocksize -1) & |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 1845 | ~((u64)root->blocksize - 1)); |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame^] | 1846 | BUG_ON(ret); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1847 | } |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame^] | 1848 | if (inode->i_size >= PAGE_CACHE_SIZE || pos + count < inode->i_size || |
| 1849 | pos + count - start_pos > BTRFS_MAX_INLINE_DATA_SIZE(root)) { |
| 1850 | ret = btrfs_alloc_extent(trans, root, num_blocks, 1, |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1851 | (u64)-1, &ins); |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame^] | 1852 | BUG_ON(ret); |
| 1853 | ret = btrfs_insert_file_extent(trans, root, inode->i_ino, |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1854 | start_pos, ins.objectid, ins.offset); |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame^] | 1855 | BUG_ON(ret); |
| 1856 | } else { |
| 1857 | ins.offset = 0; |
| 1858 | ins.objectid = 0; |
| 1859 | } |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1860 | BUG_ON(ret); |
| 1861 | alloc_extent_start = ins.objectid; |
| 1862 | ret = btrfs_end_transaction(trans, root); |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 1863 | mutex_unlock(&root->fs_info->fs_mutex); |
| 1864 | |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1865 | while(count > 0) { |
| 1866 | size_t offset = pos & (PAGE_CACHE_SIZE - 1); |
| 1867 | size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset); |
| 1868 | size_t num_pages = (write_bytes + PAGE_CACHE_SIZE - 1) >> |
| 1869 | PAGE_CACHE_SHIFT; |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1870 | |
| 1871 | memset(pages, 0, sizeof(pages)); |
| 1872 | ret = prepare_pages(root, file, pages, num_pages, |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 1873 | pos, first_index, last_index, |
| 1874 | write_bytes, alloc_extent_start); |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1875 | BUG_ON(ret); |
Chris Mason | b18c668 | 2007-04-17 13:26:50 -0400 | [diff] [blame] | 1876 | |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 1877 | /* FIXME blocks != pagesize */ |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame^] | 1878 | if (alloc_extent_start) |
| 1879 | alloc_extent_start += num_pages; |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1880 | ret = btrfs_copy_from_user(pos, num_pages, |
| 1881 | write_bytes, pages, buf); |
| 1882 | BUG_ON(ret); |
| 1883 | |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 1884 | ret = dirty_and_release_pages(NULL, root, file, pages, |
Chris Mason | 70b2bef | 2007-04-17 15:39:32 -0400 | [diff] [blame] | 1885 | num_pages, pos, write_bytes); |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1886 | BUG_ON(ret); |
| 1887 | btrfs_drop_pages(pages, num_pages); |
| 1888 | |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1889 | buf += write_bytes; |
| 1890 | count -= write_bytes; |
| 1891 | pos += write_bytes; |
| 1892 | num_written += write_bytes; |
| 1893 | |
| 1894 | balance_dirty_pages_ratelimited(inode->i_mapping); |
| 1895 | cond_resched(); |
| 1896 | } |
Chris Mason | 6567e83 | 2007-04-16 09:22:45 -0400 | [diff] [blame] | 1897 | out_unlock: |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1898 | mutex_unlock(&inode->i_mutex); |
| 1899 | out: |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 1900 | if (pinned[0]) |
| 1901 | page_cache_release(pinned[0]); |
| 1902 | if (pinned[1]) |
| 1903 | page_cache_release(pinned[1]); |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1904 | *ppos = pos; |
| 1905 | current->backing_dev_info = NULL; |
Chris Mason | a429e51 | 2007-04-18 16:15:28 -0400 | [diff] [blame] | 1906 | mark_inode_dirty(inode); |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 1907 | return num_written ? num_written : err; |
| 1908 | } |
| 1909 | |
Chris Mason | e8f05c4 | 2007-04-04 14:30:09 -0400 | [diff] [blame] | 1910 | #if 0 |
Chris Mason | df24a2b | 2007-04-04 09:36:31 -0400 | [diff] [blame] | 1911 | static ssize_t inline_one_page(struct btrfs_root *root, struct inode *inode, |
| 1912 | struct page *page, loff_t pos, |
| 1913 | size_t offset, size_t write_bytes) |
| 1914 | { |
| 1915 | struct btrfs_path *path; |
| 1916 | struct btrfs_trans_handle *trans; |
| 1917 | struct btrfs_key key; |
| 1918 | struct btrfs_leaf *leaf; |
| 1919 | struct btrfs_key found_key; |
| 1920 | int ret; |
| 1921 | size_t copy_size = 0; |
| 1922 | char *dst = NULL; |
| 1923 | int err = 0; |
| 1924 | size_t num_written = 0; |
| 1925 | |
| 1926 | path = btrfs_alloc_path(); |
| 1927 | BUG_ON(!path); |
| 1928 | mutex_lock(&root->fs_info->fs_mutex); |
| 1929 | trans = btrfs_start_transaction(root, 1); |
| 1930 | key.objectid = inode->i_ino; |
| 1931 | key.flags = 0; |
| 1932 | btrfs_set_key_type(&key, BTRFS_INLINE_DATA_KEY); |
| 1933 | |
| 1934 | again: |
| 1935 | key.offset = pos; |
| 1936 | ret = btrfs_search_slot(trans, root, &key, path, 0, 1); |
| 1937 | if (ret < 0) { |
| 1938 | err = ret; |
| 1939 | goto out; |
| 1940 | } |
| 1941 | if (ret == 0) { |
| 1942 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
| 1943 | btrfs_disk_key_to_cpu(&found_key, |
| 1944 | &leaf->items[path->slots[0]].key); |
| 1945 | copy_size = btrfs_item_size(leaf->items + path->slots[0]); |
| 1946 | dst = btrfs_item_ptr(leaf, path->slots[0], char); |
| 1947 | copy_size = min(write_bytes, copy_size); |
| 1948 | goto copyit; |
| 1949 | } else { |
| 1950 | int slot = path->slots[0]; |
| 1951 | if (slot > 0) { |
| 1952 | slot--; |
| 1953 | } |
| 1954 | // FIXME find max key |
| 1955 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
| 1956 | btrfs_disk_key_to_cpu(&found_key, |
| 1957 | &leaf->items[slot].key); |
| 1958 | if (found_key.objectid != inode->i_ino) |
| 1959 | goto insert; |
| 1960 | if (btrfs_key_type(&found_key) != BTRFS_INLINE_DATA_KEY) |
| 1961 | goto insert; |
| 1962 | copy_size = btrfs_item_size(leaf->items + slot); |
| 1963 | if (found_key.offset + copy_size <= pos) |
| 1964 | goto insert; |
| 1965 | dst = btrfs_item_ptr(leaf, path->slots[0], char); |
| 1966 | dst += pos - found_key.offset; |
| 1967 | copy_size = copy_size - (pos - found_key.offset); |
| 1968 | BUG_ON(copy_size < 0); |
| 1969 | copy_size = min(write_bytes, copy_size); |
| 1970 | WARN_ON(copy_size == 0); |
| 1971 | goto copyit; |
| 1972 | } |
| 1973 | insert: |
| 1974 | btrfs_release_path(root, path); |
Chris Mason | d4dbff9 | 2007-04-04 14:08:15 -0400 | [diff] [blame] | 1975 | copy_size = min(write_bytes, |
| 1976 | (size_t)BTRFS_LEAF_DATA_SIZE(root) - |
| 1977 | sizeof(struct btrfs_item) * 4); |
Chris Mason | df24a2b | 2007-04-04 09:36:31 -0400 | [diff] [blame] | 1978 | ret = btrfs_insert_empty_item(trans, root, path, &key, copy_size); |
| 1979 | BUG_ON(ret); |
| 1980 | dst = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), |
| 1981 | path->slots[0], char); |
| 1982 | copyit: |
| 1983 | WARN_ON(copy_size == 0); |
| 1984 | WARN_ON(dst + copy_size > |
| 1985 | btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), |
| 1986 | path->slots[0], char) + |
| 1987 | btrfs_item_size(btrfs_buffer_leaf(path->nodes[0])->items + |
| 1988 | path->slots[0])); |
| 1989 | btrfs_memcpy(root, path->nodes[0]->b_data, dst, |
| 1990 | page_address(page) + offset, copy_size); |
| 1991 | mark_buffer_dirty(path->nodes[0]); |
| 1992 | btrfs_release_path(root, path); |
| 1993 | pos += copy_size; |
| 1994 | offset += copy_size; |
| 1995 | num_written += copy_size; |
| 1996 | write_bytes -= copy_size; |
| 1997 | if (write_bytes) |
| 1998 | goto again; |
| 1999 | out: |
| 2000 | btrfs_free_path(path); |
| 2001 | ret = btrfs_end_transaction(trans, root); |
| 2002 | BUG_ON(ret); |
| 2003 | mutex_unlock(&root->fs_info->fs_mutex); |
| 2004 | return num_written ? num_written : err; |
| 2005 | } |
| 2006 | |
| 2007 | static ssize_t btrfs_file_inline_write(struct file *file, |
| 2008 | const char __user *buf, |
| 2009 | size_t count, loff_t *ppos) |
| 2010 | { |
| 2011 | loff_t pos; |
| 2012 | size_t num_written = 0; |
| 2013 | int err = 0; |
| 2014 | int ret = 0; |
| 2015 | struct inode *inode = file->f_path.dentry->d_inode; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 2016 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | df24a2b | 2007-04-04 09:36:31 -0400 | [diff] [blame] | 2017 | unsigned long page_index; |
| 2018 | |
| 2019 | if (file->f_flags & O_DIRECT) |
| 2020 | return -EINVAL; |
| 2021 | pos = *ppos; |
| 2022 | |
| 2023 | vfs_check_frozen(inode->i_sb, SB_FREEZE_WRITE); |
| 2024 | current->backing_dev_info = inode->i_mapping->backing_dev_info; |
| 2025 | err = generic_write_checks(file, &pos, &count, S_ISBLK(inode->i_mode)); |
| 2026 | if (err) |
| 2027 | goto out; |
| 2028 | if (count == 0) |
| 2029 | goto out; |
| 2030 | err = remove_suid(file->f_path.dentry); |
| 2031 | if (err) |
| 2032 | goto out; |
| 2033 | file_update_time(file); |
| 2034 | mutex_lock(&inode->i_mutex); |
| 2035 | while(count > 0) { |
| 2036 | size_t offset = pos & (PAGE_CACHE_SIZE - 1); |
| 2037 | size_t write_bytes = min(count, PAGE_CACHE_SIZE - offset); |
| 2038 | struct page *page; |
| 2039 | |
| 2040 | page_index = pos >> PAGE_CACHE_SHIFT; |
| 2041 | page = grab_cache_page(inode->i_mapping, page_index); |
| 2042 | if (!PageUptodate(page)) { |
| 2043 | ret = mpage_readpage(page, btrfs_get_block); |
| 2044 | BUG_ON(ret); |
| 2045 | lock_page(page); |
| 2046 | } |
| 2047 | ret = btrfs_copy_from_user(pos, 1, |
| 2048 | write_bytes, &page, buf); |
| 2049 | BUG_ON(ret); |
| 2050 | write_bytes = inline_one_page(root, inode, page, pos, |
| 2051 | offset, write_bytes); |
| 2052 | SetPageUptodate(page); |
| 2053 | if (write_bytes > 0 && pos + write_bytes > inode->i_size) { |
| 2054 | i_size_write(inode, pos + write_bytes); |
| 2055 | mark_inode_dirty(inode); |
| 2056 | } |
| 2057 | page_cache_release(page); |
| 2058 | unlock_page(page); |
| 2059 | if (write_bytes < 0) |
| 2060 | goto out_unlock; |
| 2061 | buf += write_bytes; |
| 2062 | count -= write_bytes; |
| 2063 | pos += write_bytes; |
| 2064 | num_written += write_bytes; |
| 2065 | |
| 2066 | balance_dirty_pages_ratelimited(inode->i_mapping); |
| 2067 | cond_resched(); |
| 2068 | } |
| 2069 | out_unlock: |
| 2070 | mutex_unlock(&inode->i_mutex); |
| 2071 | out: |
| 2072 | *ppos = pos; |
| 2073 | current->backing_dev_info = NULL; |
| 2074 | return num_written ? num_written : err; |
| 2075 | } |
Chris Mason | e8f05c4 | 2007-04-04 14:30:09 -0400 | [diff] [blame] | 2076 | #endif |
Chris Mason | df24a2b | 2007-04-04 09:36:31 -0400 | [diff] [blame] | 2077 | |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 2078 | static int btrfs_read_actor(read_descriptor_t *desc, struct page *page, |
| 2079 | unsigned long offset, unsigned long size) |
| 2080 | { |
| 2081 | char *kaddr; |
| 2082 | unsigned long left, count = desc->count; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 2083 | struct inode *inode = page->mapping->host; |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 2084 | |
| 2085 | if (size > count) |
| 2086 | size = count; |
| 2087 | |
| 2088 | if (!PageChecked(page)) { |
| 2089 | /* FIXME, do it per block */ |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 2090 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame^] | 2091 | |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 2092 | int ret = btrfs_csum_verify_file_block(root, |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame^] | 2093 | page->mapping->host->i_ino, |
| 2094 | page->index << PAGE_CACHE_SHIFT, |
| 2095 | kmap(page), PAGE_CACHE_SIZE); |
Chris Mason | f254e52 | 2007-03-29 15:15:27 -0400 | [diff] [blame] | 2096 | if (ret) { |
| 2097 | printk("failed to verify ino %lu page %lu\n", |
| 2098 | page->mapping->host->i_ino, |
| 2099 | page->index); |
| 2100 | memset(page_address(page), 0, PAGE_CACHE_SIZE); |
| 2101 | } |
| 2102 | SetPageChecked(page); |
| 2103 | kunmap(page); |
| 2104 | } |
| 2105 | /* |
| 2106 | * Faults on the destination of a read are common, so do it before |
| 2107 | * taking the kmap. |
| 2108 | */ |
| 2109 | if (!fault_in_pages_writeable(desc->arg.buf, size)) { |
| 2110 | kaddr = kmap_atomic(page, KM_USER0); |
| 2111 | left = __copy_to_user_inatomic(desc->arg.buf, |
| 2112 | kaddr + offset, size); |
| 2113 | kunmap_atomic(kaddr, KM_USER0); |
| 2114 | if (left == 0) |
| 2115 | goto success; |
| 2116 | } |
| 2117 | |
| 2118 | /* Do it the slow way */ |
| 2119 | kaddr = kmap(page); |
| 2120 | left = __copy_to_user(desc->arg.buf, kaddr + offset, size); |
| 2121 | kunmap(page); |
| 2122 | |
| 2123 | if (left) { |
| 2124 | size -= left; |
| 2125 | desc->error = -EFAULT; |
| 2126 | } |
| 2127 | success: |
| 2128 | desc->count = count - size; |
| 2129 | desc->written += size; |
| 2130 | desc->arg.buf += size; |
| 2131 | return size; |
| 2132 | } |
| 2133 | |
| 2134 | /** |
| 2135 | * btrfs_file_aio_read - filesystem read routine |
| 2136 | * @iocb: kernel I/O control block |
| 2137 | * @iov: io vector request |
| 2138 | * @nr_segs: number of segments in the iovec |
| 2139 | * @pos: current file position |
| 2140 | */ |
| 2141 | static ssize_t btrfs_file_aio_read(struct kiocb *iocb, const struct iovec *iov, |
| 2142 | unsigned long nr_segs, loff_t pos) |
| 2143 | { |
| 2144 | struct file *filp = iocb->ki_filp; |
| 2145 | ssize_t retval; |
| 2146 | unsigned long seg; |
| 2147 | size_t count; |
| 2148 | loff_t *ppos = &iocb->ki_pos; |
| 2149 | |
| 2150 | count = 0; |
| 2151 | for (seg = 0; seg < nr_segs; seg++) { |
| 2152 | const struct iovec *iv = &iov[seg]; |
| 2153 | |
| 2154 | /* |
| 2155 | * If any segment has a negative length, or the cumulative |
| 2156 | * length ever wraps negative then return -EINVAL. |
| 2157 | */ |
| 2158 | count += iv->iov_len; |
| 2159 | if (unlikely((ssize_t)(count|iv->iov_len) < 0)) |
| 2160 | return -EINVAL; |
| 2161 | if (access_ok(VERIFY_WRITE, iv->iov_base, iv->iov_len)) |
| 2162 | continue; |
| 2163 | if (seg == 0) |
| 2164 | return -EFAULT; |
| 2165 | nr_segs = seg; |
| 2166 | count -= iv->iov_len; /* This segment is no good */ |
| 2167 | break; |
| 2168 | } |
| 2169 | retval = 0; |
| 2170 | if (count) { |
| 2171 | for (seg = 0; seg < nr_segs; seg++) { |
| 2172 | read_descriptor_t desc; |
| 2173 | |
| 2174 | desc.written = 0; |
| 2175 | desc.arg.buf = iov[seg].iov_base; |
| 2176 | desc.count = iov[seg].iov_len; |
| 2177 | if (desc.count == 0) |
| 2178 | continue; |
| 2179 | desc.error = 0; |
| 2180 | do_generic_file_read(filp, ppos, &desc, |
| 2181 | btrfs_read_actor); |
| 2182 | retval += desc.written; |
| 2183 | if (desc.error) { |
| 2184 | retval = retval ?: desc.error; |
| 2185 | break; |
| 2186 | } |
| 2187 | } |
| 2188 | } |
| 2189 | return retval; |
| 2190 | } |
| 2191 | |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 2192 | static int create_subvol(struct btrfs_root *root, char *name, int namelen) |
| 2193 | { |
| 2194 | struct btrfs_trans_handle *trans; |
| 2195 | struct btrfs_key key; |
| 2196 | struct btrfs_root_item root_item; |
| 2197 | struct btrfs_inode_item *inode_item; |
| 2198 | struct buffer_head *subvol; |
| 2199 | struct btrfs_leaf *leaf; |
| 2200 | struct btrfs_root *new_root; |
| 2201 | struct inode *inode; |
| 2202 | int ret; |
| 2203 | u64 objectid; |
| 2204 | u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID; |
| 2205 | |
| 2206 | mutex_lock(&root->fs_info->fs_mutex); |
| 2207 | trans = btrfs_start_transaction(root, 1); |
| 2208 | BUG_ON(!trans); |
| 2209 | |
| 2210 | subvol = btrfs_alloc_free_block(trans, root); |
| 2211 | leaf = btrfs_buffer_leaf(subvol); |
| 2212 | btrfs_set_header_nritems(&leaf->header, 0); |
| 2213 | btrfs_set_header_level(&leaf->header, 0); |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 2214 | btrfs_set_header_blocknr(&leaf->header, bh_blocknr(subvol)); |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 2215 | btrfs_set_header_generation(&leaf->header, trans->transid); |
| 2216 | memcpy(leaf->header.fsid, root->fs_info->disk_super->fsid, |
| 2217 | sizeof(leaf->header.fsid)); |
| 2218 | |
| 2219 | inode_item = &root_item.inode; |
| 2220 | memset(inode_item, 0, sizeof(*inode_item)); |
| 2221 | btrfs_set_inode_generation(inode_item, 1); |
| 2222 | btrfs_set_inode_size(inode_item, 3); |
| 2223 | btrfs_set_inode_nlink(inode_item, 1); |
| 2224 | btrfs_set_inode_nblocks(inode_item, 1); |
| 2225 | btrfs_set_inode_mode(inode_item, S_IFDIR | 0755); |
| 2226 | |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 2227 | btrfs_set_root_blocknr(&root_item, bh_blocknr(subvol)); |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 2228 | btrfs_set_root_refs(&root_item, 1); |
| 2229 | |
| 2230 | mark_buffer_dirty(subvol); |
| 2231 | brelse(subvol); |
| 2232 | subvol = NULL; |
| 2233 | |
| 2234 | ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root, |
| 2235 | 0, &objectid); |
| 2236 | BUG_ON(ret); |
| 2237 | |
| 2238 | btrfs_set_root_dirid(&root_item, new_dirid); |
| 2239 | |
| 2240 | key.objectid = objectid; |
| 2241 | key.offset = 1; |
| 2242 | key.flags = 0; |
| 2243 | btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); |
| 2244 | ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, |
| 2245 | &root_item); |
| 2246 | BUG_ON(ret); |
| 2247 | |
| 2248 | /* |
| 2249 | * insert the directory item |
| 2250 | */ |
| 2251 | key.offset = (u64)-1; |
| 2252 | ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root, |
| 2253 | name, namelen, |
| 2254 | root->fs_info->sb->s_root->d_inode->i_ino, |
| 2255 | &key, 0); |
| 2256 | BUG_ON(ret); |
| 2257 | |
| 2258 | ret = btrfs_commit_transaction(trans, root); |
| 2259 | BUG_ON(ret); |
| 2260 | |
| 2261 | new_root = btrfs_read_fs_root(root->fs_info, &key); |
| 2262 | BUG_ON(!new_root); |
| 2263 | |
| 2264 | trans = btrfs_start_transaction(new_root, 1); |
| 2265 | BUG_ON(!trans); |
| 2266 | |
| 2267 | inode = btrfs_new_inode(trans, new_root, new_dirid, S_IFDIR | 0700); |
| 2268 | inode->i_op = &btrfs_dir_inode_operations; |
| 2269 | inode->i_fop = &btrfs_dir_file_operations; |
| 2270 | |
| 2271 | ret = btrfs_make_empty_dir(trans, new_root, new_dirid, new_dirid); |
| 2272 | BUG_ON(ret); |
| 2273 | |
| 2274 | inode->i_nlink = 1; |
| 2275 | inode->i_size = 6; |
| 2276 | ret = btrfs_update_inode(trans, new_root, inode); |
| 2277 | BUG_ON(ret); |
| 2278 | |
| 2279 | ret = btrfs_commit_transaction(trans, new_root); |
| 2280 | BUG_ON(ret); |
| 2281 | |
| 2282 | iput(inode); |
| 2283 | |
| 2284 | mutex_unlock(&root->fs_info->fs_mutex); |
| 2285 | return 0; |
| 2286 | } |
| 2287 | |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 2288 | static int create_snapshot(struct btrfs_root *root, char *name, int namelen) |
| 2289 | { |
| 2290 | struct btrfs_trans_handle *trans; |
| 2291 | struct btrfs_key key; |
| 2292 | struct btrfs_root_item new_root_item; |
| 2293 | int ret; |
| 2294 | u64 objectid; |
| 2295 | |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 2296 | if (!root->ref_cows) |
| 2297 | return -EINVAL; |
| 2298 | |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 2299 | mutex_lock(&root->fs_info->fs_mutex); |
| 2300 | trans = btrfs_start_transaction(root, 1); |
| 2301 | BUG_ON(!trans); |
| 2302 | |
| 2303 | ret = btrfs_update_inode(trans, root, root->inode); |
| 2304 | BUG_ON(ret); |
| 2305 | |
Chris Mason | 1b05da2 | 2007-04-10 12:13:09 -0400 | [diff] [blame] | 2306 | ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root, |
| 2307 | 0, &objectid); |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 2308 | BUG_ON(ret); |
| 2309 | |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 2310 | memcpy(&new_root_item, &root->root_item, |
| 2311 | sizeof(new_root_item)); |
| 2312 | |
| 2313 | key.objectid = objectid; |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 2314 | key.offset = 1; |
| 2315 | key.flags = 0; |
| 2316 | btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); |
Chris Mason | 7eccb90 | 2007-04-11 15:53:25 -0400 | [diff] [blame] | 2317 | btrfs_set_root_blocknr(&new_root_item, bh_blocknr(root->node)); |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 2318 | |
| 2319 | ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, |
| 2320 | &new_root_item); |
| 2321 | BUG_ON(ret); |
| 2322 | |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 2323 | /* |
| 2324 | * insert the directory item |
| 2325 | */ |
| 2326 | key.offset = (u64)-1; |
| 2327 | ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root, |
| 2328 | name, namelen, |
| 2329 | root->fs_info->sb->s_root->d_inode->i_ino, |
| 2330 | &key, 0); |
| 2331 | |
| 2332 | BUG_ON(ret); |
| 2333 | |
| 2334 | ret = btrfs_inc_root_ref(trans, root); |
| 2335 | BUG_ON(ret); |
| 2336 | |
| 2337 | ret = btrfs_commit_transaction(trans, root); |
| 2338 | BUG_ON(ret); |
| 2339 | mutex_unlock(&root->fs_info->fs_mutex); |
| 2340 | return 0; |
| 2341 | } |
| 2342 | |
Chris Mason | 8352d8a | 2007-04-12 10:43:05 -0400 | [diff] [blame] | 2343 | static int add_disk(struct btrfs_root *root, char *name, int namelen) |
| 2344 | { |
| 2345 | struct block_device *bdev; |
| 2346 | struct btrfs_path *path; |
| 2347 | struct super_block *sb = root->fs_info->sb; |
| 2348 | struct btrfs_root *dev_root = root->fs_info->dev_root; |
| 2349 | struct btrfs_trans_handle *trans; |
| 2350 | struct btrfs_device_item *dev_item; |
| 2351 | struct btrfs_key key; |
| 2352 | u16 item_size; |
| 2353 | u64 num_blocks; |
| 2354 | u64 new_blocks; |
Chris Mason | b4100d6 | 2007-04-12 12:14:00 -0400 | [diff] [blame] | 2355 | u64 device_id; |
Chris Mason | 8352d8a | 2007-04-12 10:43:05 -0400 | [diff] [blame] | 2356 | int ret; |
Chris Mason | b4100d6 | 2007-04-12 12:14:00 -0400 | [diff] [blame] | 2357 | |
Chris Mason | 8352d8a | 2007-04-12 10:43:05 -0400 | [diff] [blame] | 2358 | printk("adding disk %s\n", name); |
| 2359 | path = btrfs_alloc_path(); |
| 2360 | if (!path) |
| 2361 | return -ENOMEM; |
| 2362 | num_blocks = btrfs_super_total_blocks(root->fs_info->disk_super); |
| 2363 | bdev = open_bdev_excl(name, O_RDWR, sb); |
| 2364 | if (IS_ERR(bdev)) { |
| 2365 | ret = PTR_ERR(bdev); |
| 2366 | printk("open bdev excl failed ret %d\n", ret); |
| 2367 | goto out_nolock; |
| 2368 | } |
| 2369 | set_blocksize(bdev, sb->s_blocksize); |
| 2370 | new_blocks = bdev->bd_inode->i_size >> sb->s_blocksize_bits; |
| 2371 | key.objectid = num_blocks; |
| 2372 | key.offset = new_blocks; |
| 2373 | key.flags = 0; |
| 2374 | btrfs_set_key_type(&key, BTRFS_DEV_ITEM_KEY); |
| 2375 | |
| 2376 | mutex_lock(&dev_root->fs_info->fs_mutex); |
| 2377 | trans = btrfs_start_transaction(dev_root, 1); |
| 2378 | item_size = sizeof(*dev_item) + namelen; |
| 2379 | printk("insert empty on %Lu %Lu %u size %d\n", num_blocks, new_blocks, key.flags, item_size); |
| 2380 | ret = btrfs_insert_empty_item(trans, dev_root, path, &key, item_size); |
| 2381 | if (ret) { |
| 2382 | printk("insert failed %d\n", ret); |
| 2383 | close_bdev_excl(bdev); |
| 2384 | if (ret > 0) |
| 2385 | ret = -EEXIST; |
| 2386 | goto out; |
| 2387 | } |
| 2388 | dev_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), |
| 2389 | path->slots[0], struct btrfs_device_item); |
| 2390 | btrfs_set_device_pathlen(dev_item, namelen); |
| 2391 | memcpy(dev_item + 1, name, namelen); |
Chris Mason | b4100d6 | 2007-04-12 12:14:00 -0400 | [diff] [blame] | 2392 | |
| 2393 | device_id = btrfs_super_last_device_id(root->fs_info->disk_super) + 1; |
| 2394 | btrfs_set_super_last_device_id(root->fs_info->disk_super, device_id); |
| 2395 | btrfs_set_device_id(dev_item, device_id); |
Chris Mason | 8352d8a | 2007-04-12 10:43:05 -0400 | [diff] [blame] | 2396 | mark_buffer_dirty(path->nodes[0]); |
| 2397 | |
Chris Mason | b4100d6 | 2007-04-12 12:14:00 -0400 | [diff] [blame] | 2398 | ret = btrfs_insert_dev_radix(root, bdev, device_id, num_blocks, |
| 2399 | new_blocks); |
Chris Mason | 8352d8a | 2007-04-12 10:43:05 -0400 | [diff] [blame] | 2400 | |
| 2401 | if (!ret) { |
| 2402 | btrfs_set_super_total_blocks(root->fs_info->disk_super, |
| 2403 | num_blocks + new_blocks); |
| 2404 | i_size_write(root->fs_info->btree_inode, |
| 2405 | (num_blocks + new_blocks) << |
| 2406 | root->fs_info->btree_inode->i_blkbits); |
| 2407 | } |
| 2408 | |
| 2409 | out: |
| 2410 | ret = btrfs_commit_transaction(trans, dev_root); |
| 2411 | BUG_ON(ret); |
| 2412 | mutex_unlock(&root->fs_info->fs_mutex); |
| 2413 | out_nolock: |
| 2414 | btrfs_free_path(path); |
| 2415 | |
| 2416 | return ret; |
| 2417 | } |
| 2418 | |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 2419 | static int btrfs_ioctl(struct inode *inode, struct file *filp, unsigned int |
| 2420 | cmd, unsigned long arg) |
| 2421 | { |
| 2422 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2423 | struct btrfs_ioctl_vol_args vol_args; |
Chris Mason | 8352d8a | 2007-04-12 10:43:05 -0400 | [diff] [blame] | 2424 | int ret = 0; |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 2425 | int namelen; |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 2426 | struct btrfs_path *path; |
| 2427 | u64 root_dirid; |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 2428 | |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 2429 | switch (cmd) { |
| 2430 | case BTRFS_IOC_SNAP_CREATE: |
| 2431 | if (copy_from_user(&vol_args, |
| 2432 | (struct btrfs_ioctl_vol_args __user *)arg, |
| 2433 | sizeof(vol_args))) |
| 2434 | return -EFAULT; |
| 2435 | namelen = strlen(vol_args.name); |
| 2436 | if (namelen > BTRFS_VOL_NAME_MAX) |
| 2437 | return -EINVAL; |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 2438 | path = btrfs_alloc_path(); |
| 2439 | if (!path) |
| 2440 | return -ENOMEM; |
Chris Mason | 2d13d8d | 2007-04-10 20:07:20 -0400 | [diff] [blame] | 2441 | root_dirid = root->fs_info->sb->s_root->d_inode->i_ino, |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 2442 | mutex_lock(&root->fs_info->fs_mutex); |
| 2443 | ret = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root, |
| 2444 | path, root_dirid, |
| 2445 | vol_args.name, namelen, 0); |
| 2446 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | 2d13d8d | 2007-04-10 20:07:20 -0400 | [diff] [blame] | 2447 | btrfs_free_path(path); |
Chris Mason | 2619ba1 | 2007-04-10 16:58:11 -0400 | [diff] [blame] | 2448 | if (ret == 0) |
| 2449 | return -EEXIST; |
| 2450 | |
| 2451 | if (root == root->fs_info->tree_root) |
| 2452 | ret = create_subvol(root, vol_args.name, namelen); |
| 2453 | else |
| 2454 | ret = create_snapshot(root, vol_args.name, namelen); |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 2455 | WARN_ON(ret); |
| 2456 | break; |
Chris Mason | 8352d8a | 2007-04-12 10:43:05 -0400 | [diff] [blame] | 2457 | case BTRFS_IOC_ADD_DISK: |
| 2458 | if (copy_from_user(&vol_args, |
| 2459 | (struct btrfs_ioctl_vol_args __user *)arg, |
| 2460 | sizeof(vol_args))) |
| 2461 | return -EFAULT; |
| 2462 | namelen = strlen(vol_args.name); |
| 2463 | if (namelen > BTRFS_VOL_NAME_MAX) |
| 2464 | return -EINVAL; |
| 2465 | vol_args.name[namelen] = '\0'; |
| 2466 | ret = add_disk(root, vol_args.name, namelen); |
| 2467 | break; |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 2468 | default: |
| 2469 | return -ENOTTY; |
| 2470 | } |
Chris Mason | 8352d8a | 2007-04-12 10:43:05 -0400 | [diff] [blame] | 2471 | return ret; |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 2472 | } |
| 2473 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 2474 | static struct kmem_cache *btrfs_inode_cachep; |
| 2475 | struct kmem_cache *btrfs_trans_handle_cachep; |
| 2476 | struct kmem_cache *btrfs_transaction_cachep; |
| 2477 | struct kmem_cache *btrfs_bit_radix_cachep; |
| 2478 | struct kmem_cache *btrfs_path_cachep; |
| 2479 | |
| 2480 | /* |
| 2481 | * Called inside transaction, so use GFP_NOFS |
| 2482 | */ |
| 2483 | static struct inode *btrfs_alloc_inode(struct super_block *sb) |
| 2484 | { |
| 2485 | struct btrfs_inode *ei; |
| 2486 | |
| 2487 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); |
| 2488 | if (!ei) |
| 2489 | return NULL; |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 2490 | return &ei->vfs_inode; |
| 2491 | } |
| 2492 | |
| 2493 | static void btrfs_destroy_inode(struct inode *inode) |
| 2494 | { |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 2495 | WARN_ON(!list_empty(&inode->i_dentry)); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 2496 | WARN_ON(inode->i_data.nrpages); |
| 2497 | |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 2498 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 2499 | } |
| 2500 | |
| 2501 | static void init_once(void * foo, struct kmem_cache * cachep, |
| 2502 | unsigned long flags) |
| 2503 | { |
| 2504 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; |
| 2505 | |
| 2506 | if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) == |
| 2507 | SLAB_CTOR_CONSTRUCTOR) { |
| 2508 | inode_init_once(&ei->vfs_inode); |
| 2509 | } |
| 2510 | } |
| 2511 | |
| 2512 | static int init_inodecache(void) |
| 2513 | { |
| 2514 | btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache", |
| 2515 | sizeof(struct btrfs_inode), |
| 2516 | 0, (SLAB_RECLAIM_ACCOUNT| |
| 2517 | SLAB_MEM_SPREAD), |
| 2518 | init_once, NULL); |
| 2519 | btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache", |
| 2520 | sizeof(struct btrfs_trans_handle), |
| 2521 | 0, (SLAB_RECLAIM_ACCOUNT| |
| 2522 | SLAB_MEM_SPREAD), |
| 2523 | NULL, NULL); |
| 2524 | btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache", |
| 2525 | sizeof(struct btrfs_transaction), |
| 2526 | 0, (SLAB_RECLAIM_ACCOUNT| |
| 2527 | SLAB_MEM_SPREAD), |
| 2528 | NULL, NULL); |
| 2529 | btrfs_path_cachep = kmem_cache_create("btrfs_path_cache", |
| 2530 | sizeof(struct btrfs_transaction), |
| 2531 | 0, (SLAB_RECLAIM_ACCOUNT| |
| 2532 | SLAB_MEM_SPREAD), |
| 2533 | NULL, NULL); |
| 2534 | btrfs_bit_radix_cachep = kmem_cache_create("btrfs_radix", |
| 2535 | 256, |
| 2536 | 0, (SLAB_RECLAIM_ACCOUNT| |
| 2537 | SLAB_MEM_SPREAD | |
| 2538 | SLAB_DESTROY_BY_RCU), |
| 2539 | NULL, NULL); |
| 2540 | if (btrfs_inode_cachep == NULL || btrfs_trans_handle_cachep == NULL || |
| 2541 | btrfs_transaction_cachep == NULL || btrfs_bit_radix_cachep == NULL) |
| 2542 | return -ENOMEM; |
| 2543 | return 0; |
| 2544 | } |
| 2545 | |
| 2546 | static void destroy_inodecache(void) |
| 2547 | { |
| 2548 | kmem_cache_destroy(btrfs_inode_cachep); |
| 2549 | kmem_cache_destroy(btrfs_trans_handle_cachep); |
| 2550 | kmem_cache_destroy(btrfs_transaction_cachep); |
| 2551 | kmem_cache_destroy(btrfs_bit_radix_cachep); |
| 2552 | kmem_cache_destroy(btrfs_path_cachep); |
| 2553 | } |
| 2554 | |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 2555 | static int btrfs_get_sb(struct file_system_type *fs_type, |
| 2556 | int flags, const char *dev_name, void *data, struct vfsmount *mnt) |
| 2557 | { |
| 2558 | return get_sb_bdev(fs_type, flags, dev_name, data, |
| 2559 | btrfs_fill_super, mnt); |
| 2560 | } |
| 2561 | |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame^] | 2562 | |
| 2563 | static int btrfs_getattr(struct vfsmount *mnt, |
| 2564 | struct dentry *dentry, struct kstat *stat) |
| 2565 | { |
| 2566 | struct inode *inode = dentry->d_inode; |
| 2567 | generic_fillattr(inode, stat); |
| 2568 | stat->blksize = 256 * 1024; |
| 2569 | return 0; |
| 2570 | } |
| 2571 | |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 2572 | static struct file_system_type btrfs_fs_type = { |
| 2573 | .owner = THIS_MODULE, |
| 2574 | .name = "btrfs", |
| 2575 | .get_sb = btrfs_get_sb, |
| 2576 | .kill_sb = kill_block_super, |
| 2577 | .fs_flags = FS_REQUIRES_DEV, |
| 2578 | }; |
| 2579 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 2580 | static struct super_operations btrfs_super_ops = { |
| 2581 | .statfs = simple_statfs, |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 2582 | .delete_inode = btrfs_delete_inode, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 2583 | .put_super = btrfs_put_super, |
| 2584 | .read_inode = btrfs_read_locked_inode, |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 2585 | .write_super = btrfs_write_super, |
| 2586 | .sync_fs = btrfs_sync_fs, |
Chris Mason | 4730a4b | 2007-03-26 12:00:39 -0400 | [diff] [blame] | 2587 | .write_inode = btrfs_write_inode, |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 2588 | .alloc_inode = btrfs_alloc_inode, |
| 2589 | .destroy_inode = btrfs_destroy_inode, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 2590 | }; |
| 2591 | |
| 2592 | static struct inode_operations btrfs_dir_inode_operations = { |
| 2593 | .lookup = btrfs_lookup, |
Chris Mason | d571976 | 2007-03-23 10:01:08 -0400 | [diff] [blame] | 2594 | .create = btrfs_create, |
Chris Mason | 134e973 | 2007-03-25 13:44:56 -0400 | [diff] [blame] | 2595 | .unlink = btrfs_unlink, |
Chris Mason | f792203 | 2007-03-25 20:17:36 -0400 | [diff] [blame] | 2596 | .mkdir = btrfs_mkdir, |
Chris Mason | 5f443fd | 2007-03-27 13:42:32 -0400 | [diff] [blame] | 2597 | .rmdir = btrfs_rmdir, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 2598 | }; |
| 2599 | |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 2600 | static struct inode_operations btrfs_dir_ro_inode_operations = { |
| 2601 | .lookup = btrfs_lookup, |
| 2602 | }; |
| 2603 | |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 2604 | static struct file_operations btrfs_dir_file_operations = { |
| 2605 | .llseek = generic_file_llseek, |
| 2606 | .read = generic_read_dir, |
| 2607 | .readdir = btrfs_readdir, |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 2608 | .ioctl = btrfs_ioctl, |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 2609 | }; |
| 2610 | |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 2611 | static struct address_space_operations btrfs_aops = { |
| 2612 | .readpage = btrfs_readpage, |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 2613 | .writepage = btrfs_writepage, |
| 2614 | .sync_page = block_sync_page, |
| 2615 | .prepare_write = btrfs_prepare_write, |
Chris Mason | 75dfe39 | 2007-03-29 11:56:46 -0400 | [diff] [blame] | 2616 | .commit_write = btrfs_commit_write, |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 2617 | }; |
| 2618 | |
| 2619 | static struct inode_operations btrfs_file_inode_operations = { |
Chris Mason | f4b9aa8 | 2007-03-27 11:05:53 -0400 | [diff] [blame] | 2620 | .truncate = btrfs_truncate, |
Chris Mason | 236454d | 2007-04-19 13:37:44 -0400 | [diff] [blame^] | 2621 | .getattr = btrfs_getattr, |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 2622 | }; |
| 2623 | |
| 2624 | static struct file_operations btrfs_file_operations = { |
| 2625 | .llseek = generic_file_llseek, |
| 2626 | .read = do_sync_read, |
Chris Mason | e8f05c4 | 2007-04-04 14:30:09 -0400 | [diff] [blame] | 2627 | .aio_read = btrfs_file_aio_read, |
| 2628 | .write = btrfs_file_write, |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 2629 | .mmap = generic_file_mmap, |
| 2630 | .open = generic_file_open, |
Chris Mason | c5739bb | 2007-04-10 09:27:04 -0400 | [diff] [blame] | 2631 | .ioctl = btrfs_ioctl, |
Chris Mason | dee26a9 | 2007-03-26 16:00:06 -0400 | [diff] [blame] | 2632 | }; |
Chris Mason | e20d96d | 2007-03-22 12:13:20 -0400 | [diff] [blame] | 2633 | |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 2634 | static int __init init_btrfs_fs(void) |
| 2635 | { |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 2636 | int err; |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 2637 | printk("btrfs loaded!\n"); |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 2638 | err = init_inodecache(); |
| 2639 | if (err) |
| 2640 | return err; |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 2641 | kset_set_kset_s(&btrfs_subsys, fs_subsys); |
| 2642 | err = subsystem_register(&btrfs_subsys); |
| 2643 | if (err) |
| 2644 | goto out; |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 2645 | return register_filesystem(&btrfs_fs_type); |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 2646 | out: |
| 2647 | destroy_inodecache(); |
| 2648 | return err; |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 2649 | } |
| 2650 | |
| 2651 | static void __exit exit_btrfs_fs(void) |
| 2652 | { |
Chris Mason | 2c90e5d | 2007-04-02 10:50:19 -0400 | [diff] [blame] | 2653 | destroy_inodecache(); |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 2654 | unregister_filesystem(&btrfs_fs_type); |
Chris Mason | d6e4a42 | 2007-04-06 15:37:36 -0400 | [diff] [blame] | 2655 | subsystem_unregister(&btrfs_subsys); |
Chris Mason | 2e635a2 | 2007-03-21 11:12:56 -0400 | [diff] [blame] | 2656 | printk("btrfs unloaded\n"); |
| 2657 | } |
| 2658 | |
| 2659 | module_init(init_btrfs_fs) |
| 2660 | module_exit(exit_btrfs_fs) |
| 2661 | |
| 2662 | MODULE_LICENSE("GPL"); |