Chris Mason | 6cbd557 | 2007-06-12 09:07:21 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 Oracle. All rights reserved. |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU General Public |
| 6 | * License v2 as published by the Free Software Foundation. |
| 7 | * |
| 8 | * This program is distributed in the hope that it will be useful, |
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 11 | * General Public License for more details. |
| 12 | * |
| 13 | * You should have received a copy of the GNU General Public |
| 14 | * License along with this program; if not, write to the |
| 15 | * Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 16 | * Boston, MA 021110-1307, USA. |
| 17 | */ |
| 18 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 19 | #include <linux/buffer_head.h> |
| 20 | #include <linux/fs.h> |
| 21 | #include <linux/pagemap.h> |
| 22 | #include <linux/highmem.h> |
| 23 | #include <linux/time.h> |
| 24 | #include <linux/init.h> |
| 25 | #include <linux/string.h> |
| 26 | #include <linux/smp_lock.h> |
| 27 | #include <linux/backing-dev.h> |
| 28 | #include <linux/mpage.h> |
| 29 | #include <linux/swap.h> |
| 30 | #include <linux/writeback.h> |
| 31 | #include <linux/statfs.h> |
| 32 | #include <linux/compat.h> |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 33 | #include <linux/bit_spinlock.h> |
Chris Mason | 92fee66 | 2007-07-25 12:31:35 -0400 | [diff] [blame^] | 34 | #include <linux/version.h> |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 35 | #include "ctree.h" |
| 36 | #include "disk-io.h" |
| 37 | #include "transaction.h" |
| 38 | #include "btrfs_inode.h" |
| 39 | #include "ioctl.h" |
| 40 | #include "print-tree.h" |
| 41 | |
| 42 | struct btrfs_iget_args { |
| 43 | u64 ino; |
| 44 | struct btrfs_root *root; |
| 45 | }; |
| 46 | |
| 47 | static struct inode_operations btrfs_dir_inode_operations; |
| 48 | static struct inode_operations btrfs_symlink_inode_operations; |
| 49 | static struct inode_operations btrfs_dir_ro_inode_operations; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 50 | static struct inode_operations btrfs_special_inode_operations; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 51 | static struct inode_operations btrfs_file_inode_operations; |
| 52 | static struct address_space_operations btrfs_aops; |
| 53 | static struct address_space_operations btrfs_symlink_aops; |
| 54 | static struct file_operations btrfs_dir_file_operations; |
| 55 | |
| 56 | static struct kmem_cache *btrfs_inode_cachep; |
| 57 | struct kmem_cache *btrfs_trans_handle_cachep; |
| 58 | struct kmem_cache *btrfs_transaction_cachep; |
| 59 | struct kmem_cache *btrfs_bit_radix_cachep; |
| 60 | struct kmem_cache *btrfs_path_cachep; |
| 61 | |
| 62 | #define S_SHIFT 12 |
| 63 | static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = { |
| 64 | [S_IFREG >> S_SHIFT] = BTRFS_FT_REG_FILE, |
| 65 | [S_IFDIR >> S_SHIFT] = BTRFS_FT_DIR, |
| 66 | [S_IFCHR >> S_SHIFT] = BTRFS_FT_CHRDEV, |
| 67 | [S_IFBLK >> S_SHIFT] = BTRFS_FT_BLKDEV, |
| 68 | [S_IFIFO >> S_SHIFT] = BTRFS_FT_FIFO, |
| 69 | [S_IFSOCK >> S_SHIFT] = BTRFS_FT_SOCK, |
| 70 | [S_IFLNK >> S_SHIFT] = BTRFS_FT_SYMLINK, |
| 71 | }; |
| 72 | |
| 73 | void btrfs_read_locked_inode(struct inode *inode) |
| 74 | { |
| 75 | struct btrfs_path *path; |
| 76 | struct btrfs_inode_item *inode_item; |
| 77 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 78 | struct btrfs_key location; |
| 79 | u64 alloc_group_block; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 80 | u32 rdev; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 81 | int ret; |
| 82 | |
| 83 | path = btrfs_alloc_path(); |
| 84 | BUG_ON(!path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 85 | mutex_lock(&root->fs_info->fs_mutex); |
| 86 | |
| 87 | memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); |
| 88 | ret = btrfs_lookup_inode(NULL, root, path, &location, 0); |
| 89 | if (ret) { |
| 90 | btrfs_free_path(path); |
| 91 | goto make_bad; |
| 92 | } |
| 93 | inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), |
| 94 | path->slots[0], |
| 95 | struct btrfs_inode_item); |
| 96 | |
| 97 | inode->i_mode = btrfs_inode_mode(inode_item); |
| 98 | inode->i_nlink = btrfs_inode_nlink(inode_item); |
| 99 | inode->i_uid = btrfs_inode_uid(inode_item); |
| 100 | inode->i_gid = btrfs_inode_gid(inode_item); |
| 101 | inode->i_size = btrfs_inode_size(inode_item); |
| 102 | inode->i_atime.tv_sec = btrfs_timespec_sec(&inode_item->atime); |
| 103 | inode->i_atime.tv_nsec = btrfs_timespec_nsec(&inode_item->atime); |
| 104 | inode->i_mtime.tv_sec = btrfs_timespec_sec(&inode_item->mtime); |
| 105 | inode->i_mtime.tv_nsec = btrfs_timespec_nsec(&inode_item->mtime); |
| 106 | inode->i_ctime.tv_sec = btrfs_timespec_sec(&inode_item->ctime); |
| 107 | inode->i_ctime.tv_nsec = btrfs_timespec_nsec(&inode_item->ctime); |
| 108 | inode->i_blocks = btrfs_inode_nblocks(inode_item); |
| 109 | inode->i_generation = btrfs_inode_generation(inode_item); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 110 | inode->i_rdev = 0; |
| 111 | rdev = btrfs_inode_rdev(inode_item); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 112 | alloc_group_block = btrfs_inode_block_group(inode_item); |
| 113 | BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info, |
| 114 | alloc_group_block); |
| 115 | |
| 116 | btrfs_free_path(path); |
| 117 | inode_item = NULL; |
| 118 | |
| 119 | mutex_unlock(&root->fs_info->fs_mutex); |
| 120 | |
| 121 | switch (inode->i_mode & S_IFMT) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 122 | case S_IFREG: |
| 123 | inode->i_mapping->a_ops = &btrfs_aops; |
| 124 | inode->i_fop = &btrfs_file_operations; |
| 125 | inode->i_op = &btrfs_file_inode_operations; |
| 126 | break; |
| 127 | case S_IFDIR: |
| 128 | inode->i_fop = &btrfs_dir_file_operations; |
| 129 | if (root == root->fs_info->tree_root) |
| 130 | inode->i_op = &btrfs_dir_ro_inode_operations; |
| 131 | else |
| 132 | inode->i_op = &btrfs_dir_inode_operations; |
| 133 | break; |
| 134 | case S_IFLNK: |
| 135 | inode->i_op = &btrfs_symlink_inode_operations; |
| 136 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
| 137 | break; |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 138 | default: |
| 139 | init_special_inode(inode, inode->i_mode, rdev); |
| 140 | break; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 141 | } |
| 142 | return; |
| 143 | |
| 144 | make_bad: |
| 145 | btrfs_release_path(root, path); |
| 146 | btrfs_free_path(path); |
| 147 | mutex_unlock(&root->fs_info->fs_mutex); |
| 148 | make_bad_inode(inode); |
| 149 | } |
| 150 | |
| 151 | static void fill_inode_item(struct btrfs_inode_item *item, |
| 152 | struct inode *inode) |
| 153 | { |
| 154 | btrfs_set_inode_uid(item, inode->i_uid); |
| 155 | btrfs_set_inode_gid(item, inode->i_gid); |
| 156 | btrfs_set_inode_size(item, inode->i_size); |
| 157 | btrfs_set_inode_mode(item, inode->i_mode); |
| 158 | btrfs_set_inode_nlink(item, inode->i_nlink); |
| 159 | btrfs_set_timespec_sec(&item->atime, inode->i_atime.tv_sec); |
| 160 | btrfs_set_timespec_nsec(&item->atime, inode->i_atime.tv_nsec); |
| 161 | btrfs_set_timespec_sec(&item->mtime, inode->i_mtime.tv_sec); |
| 162 | btrfs_set_timespec_nsec(&item->mtime, inode->i_mtime.tv_nsec); |
| 163 | btrfs_set_timespec_sec(&item->ctime, inode->i_ctime.tv_sec); |
| 164 | btrfs_set_timespec_nsec(&item->ctime, inode->i_ctime.tv_nsec); |
| 165 | btrfs_set_inode_nblocks(item, inode->i_blocks); |
| 166 | btrfs_set_inode_generation(item, inode->i_generation); |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 167 | btrfs_set_inode_rdev(item, inode->i_rdev); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 168 | btrfs_set_inode_block_group(item, |
| 169 | BTRFS_I(inode)->block_group->key.objectid); |
| 170 | } |
| 171 | |
| 172 | static int btrfs_update_inode(struct btrfs_trans_handle *trans, |
| 173 | struct btrfs_root *root, |
| 174 | struct inode *inode) |
| 175 | { |
| 176 | struct btrfs_inode_item *inode_item; |
| 177 | struct btrfs_path *path; |
| 178 | int ret; |
| 179 | |
| 180 | path = btrfs_alloc_path(); |
| 181 | BUG_ON(!path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 182 | ret = btrfs_lookup_inode(trans, root, path, |
| 183 | &BTRFS_I(inode)->location, 1); |
| 184 | if (ret) { |
| 185 | if (ret > 0) |
| 186 | ret = -ENOENT; |
| 187 | goto failed; |
| 188 | } |
| 189 | |
| 190 | inode_item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), |
| 191 | path->slots[0], |
| 192 | struct btrfs_inode_item); |
| 193 | |
| 194 | fill_inode_item(inode_item, inode); |
| 195 | btrfs_mark_buffer_dirty(path->nodes[0]); |
| 196 | ret = 0; |
| 197 | failed: |
| 198 | btrfs_release_path(root, path); |
| 199 | btrfs_free_path(path); |
| 200 | return ret; |
| 201 | } |
| 202 | |
| 203 | |
| 204 | static int btrfs_unlink_trans(struct btrfs_trans_handle *trans, |
| 205 | struct btrfs_root *root, |
| 206 | struct inode *dir, |
| 207 | struct dentry *dentry) |
| 208 | { |
| 209 | struct btrfs_path *path; |
| 210 | const char *name = dentry->d_name.name; |
| 211 | int name_len = dentry->d_name.len; |
| 212 | int ret = 0; |
| 213 | u64 objectid; |
| 214 | struct btrfs_dir_item *di; |
| 215 | |
| 216 | path = btrfs_alloc_path(); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 217 | if (!path) { |
| 218 | ret = -ENOMEM; |
| 219 | goto err; |
| 220 | } |
| 221 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 222 | di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino, |
| 223 | name, name_len, -1); |
| 224 | if (IS_ERR(di)) { |
| 225 | ret = PTR_ERR(di); |
| 226 | goto err; |
| 227 | } |
| 228 | if (!di) { |
| 229 | ret = -ENOENT; |
| 230 | goto err; |
| 231 | } |
| 232 | objectid = btrfs_disk_key_objectid(&di->location); |
| 233 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 234 | if (ret) |
| 235 | goto err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 236 | btrfs_release_path(root, path); |
| 237 | |
| 238 | di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, |
| 239 | objectid, name, name_len, -1); |
| 240 | if (IS_ERR(di)) { |
| 241 | ret = PTR_ERR(di); |
| 242 | goto err; |
| 243 | } |
| 244 | if (!di) { |
| 245 | ret = -ENOENT; |
| 246 | goto err; |
| 247 | } |
| 248 | ret = btrfs_delete_one_dir_name(trans, root, path, di); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 249 | |
| 250 | dentry->d_inode->i_ctime = dir->i_ctime; |
| 251 | err: |
| 252 | btrfs_free_path(path); |
| 253 | if (!ret) { |
| 254 | dir->i_size -= name_len * 2; |
Chris Mason | 79c4458 | 2007-06-25 10:09:33 -0400 | [diff] [blame] | 255 | dir->i_mtime = dir->i_ctime = CURRENT_TIME; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 256 | btrfs_update_inode(trans, root, dir); |
| 257 | drop_nlink(dentry->d_inode); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 258 | ret = btrfs_update_inode(trans, root, dentry->d_inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 259 | dir->i_sb->s_dirt = 1; |
| 260 | } |
| 261 | return ret; |
| 262 | } |
| 263 | |
| 264 | static int btrfs_unlink(struct inode *dir, struct dentry *dentry) |
| 265 | { |
| 266 | struct btrfs_root *root; |
| 267 | struct btrfs_trans_handle *trans; |
| 268 | int ret; |
| 269 | |
| 270 | root = BTRFS_I(dir)->root; |
| 271 | mutex_lock(&root->fs_info->fs_mutex); |
| 272 | trans = btrfs_start_transaction(root, 1); |
| 273 | btrfs_set_trans_block_group(trans, dir); |
| 274 | ret = btrfs_unlink_trans(trans, root, dir, dentry); |
| 275 | btrfs_end_transaction(trans, root); |
| 276 | mutex_unlock(&root->fs_info->fs_mutex); |
| 277 | btrfs_btree_balance_dirty(root); |
| 278 | return ret; |
| 279 | } |
| 280 | |
| 281 | static int btrfs_rmdir(struct inode *dir, struct dentry *dentry) |
| 282 | { |
| 283 | struct inode *inode = dentry->d_inode; |
| 284 | int err; |
| 285 | int ret; |
| 286 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 287 | struct btrfs_path *path; |
| 288 | struct btrfs_key key; |
| 289 | struct btrfs_trans_handle *trans; |
| 290 | struct btrfs_key found_key; |
| 291 | int found_type; |
| 292 | struct btrfs_leaf *leaf; |
| 293 | char *goodnames = ".."; |
| 294 | |
| 295 | path = btrfs_alloc_path(); |
| 296 | BUG_ON(!path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 297 | mutex_lock(&root->fs_info->fs_mutex); |
| 298 | trans = btrfs_start_transaction(root, 1); |
| 299 | btrfs_set_trans_block_group(trans, dir); |
| 300 | key.objectid = inode->i_ino; |
| 301 | key.offset = (u64)-1; |
| 302 | key.flags = (u32)-1; |
| 303 | while(1) { |
| 304 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 305 | if (ret < 0) { |
| 306 | err = ret; |
| 307 | goto out; |
| 308 | } |
| 309 | BUG_ON(ret == 0); |
| 310 | if (path->slots[0] == 0) { |
| 311 | err = -ENOENT; |
| 312 | goto out; |
| 313 | } |
| 314 | path->slots[0]--; |
| 315 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
| 316 | btrfs_disk_key_to_cpu(&found_key, |
| 317 | &leaf->items[path->slots[0]].key); |
| 318 | found_type = btrfs_key_type(&found_key); |
| 319 | if (found_key.objectid != inode->i_ino) { |
| 320 | err = -ENOENT; |
| 321 | goto out; |
| 322 | } |
| 323 | if ((found_type != BTRFS_DIR_ITEM_KEY && |
| 324 | found_type != BTRFS_DIR_INDEX_KEY) || |
| 325 | (!btrfs_match_dir_item_name(root, path, goodnames, 2) && |
| 326 | !btrfs_match_dir_item_name(root, path, goodnames, 1))) { |
| 327 | err = -ENOTEMPTY; |
| 328 | goto out; |
| 329 | } |
| 330 | ret = btrfs_del_item(trans, root, path); |
| 331 | BUG_ON(ret); |
| 332 | |
| 333 | if (found_type == BTRFS_DIR_ITEM_KEY && found_key.offset == 1) |
| 334 | break; |
| 335 | btrfs_release_path(root, path); |
| 336 | } |
| 337 | ret = 0; |
| 338 | btrfs_release_path(root, path); |
| 339 | |
| 340 | /* now the directory is empty */ |
| 341 | err = btrfs_unlink_trans(trans, root, dir, dentry); |
| 342 | if (!err) { |
| 343 | inode->i_size = 0; |
| 344 | } |
| 345 | out: |
| 346 | btrfs_release_path(root, path); |
| 347 | btrfs_free_path(path); |
| 348 | mutex_unlock(&root->fs_info->fs_mutex); |
| 349 | ret = btrfs_end_transaction(trans, root); |
| 350 | btrfs_btree_balance_dirty(root); |
| 351 | if (ret && !err) |
| 352 | err = ret; |
| 353 | return err; |
| 354 | } |
| 355 | |
| 356 | static int btrfs_free_inode(struct btrfs_trans_handle *trans, |
| 357 | struct btrfs_root *root, |
| 358 | struct inode *inode) |
| 359 | { |
| 360 | struct btrfs_path *path; |
| 361 | int ret; |
| 362 | |
| 363 | clear_inode(inode); |
| 364 | |
| 365 | path = btrfs_alloc_path(); |
| 366 | BUG_ON(!path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 367 | ret = btrfs_lookup_inode(trans, root, path, |
| 368 | &BTRFS_I(inode)->location, -1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 369 | if (ret > 0) |
| 370 | ret = -ENOENT; |
| 371 | if (!ret) |
| 372 | ret = btrfs_del_item(trans, root, path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 373 | btrfs_free_path(path); |
| 374 | return ret; |
| 375 | } |
| 376 | |
| 377 | /* |
| 378 | * truncates go from a high offset to a low offset. So, walk |
| 379 | * from hi to lo in the node and issue readas. Stop when you find |
| 380 | * keys from a different objectid |
| 381 | */ |
| 382 | static void reada_truncate(struct btrfs_root *root, struct btrfs_path *path, |
| 383 | u64 objectid) |
| 384 | { |
| 385 | struct btrfs_node *node; |
| 386 | int i; |
| 387 | int nritems; |
| 388 | u64 item_objectid; |
| 389 | u64 blocknr; |
| 390 | int slot; |
| 391 | int ret; |
| 392 | |
| 393 | if (!path->nodes[1]) |
| 394 | return; |
| 395 | node = btrfs_buffer_node(path->nodes[1]); |
| 396 | slot = path->slots[1]; |
| 397 | if (slot == 0) |
| 398 | return; |
| 399 | nritems = btrfs_header_nritems(&node->header); |
| 400 | for (i = slot - 1; i >= 0; i--) { |
| 401 | item_objectid = btrfs_disk_key_objectid(&node->ptrs[i].key); |
| 402 | if (item_objectid != objectid) |
| 403 | break; |
| 404 | blocknr = btrfs_node_blockptr(node, i); |
| 405 | ret = readahead_tree_block(root, blocknr); |
| 406 | if (ret) |
| 407 | break; |
| 408 | } |
| 409 | } |
| 410 | |
| 411 | /* |
| 412 | * this can truncate away extent items, csum items and directory items. |
| 413 | * It starts at a high offset and removes keys until it can't find |
| 414 | * any higher than i_size. |
| 415 | * |
| 416 | * csum items that cross the new i_size are truncated to the new size |
| 417 | * as well. |
| 418 | */ |
| 419 | static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans, |
| 420 | struct btrfs_root *root, |
| 421 | struct inode *inode) |
| 422 | { |
| 423 | int ret; |
| 424 | struct btrfs_path *path; |
| 425 | struct btrfs_key key; |
| 426 | struct btrfs_disk_key *found_key; |
| 427 | u32 found_type; |
| 428 | struct btrfs_leaf *leaf; |
| 429 | struct btrfs_file_extent_item *fi; |
| 430 | u64 extent_start = 0; |
| 431 | u64 extent_num_blocks = 0; |
| 432 | u64 item_end = 0; |
| 433 | int found_extent; |
| 434 | int del_item; |
| 435 | |
| 436 | path = btrfs_alloc_path(); |
| 437 | BUG_ON(!path); |
| 438 | /* FIXME, add redo link to tree so we don't leak on crash */ |
| 439 | key.objectid = inode->i_ino; |
| 440 | key.offset = (u64)-1; |
| 441 | key.flags = (u32)-1; |
| 442 | while(1) { |
| 443 | btrfs_init_path(path); |
| 444 | fi = NULL; |
| 445 | ret = btrfs_search_slot(trans, root, &key, path, -1, 1); |
| 446 | if (ret < 0) { |
| 447 | goto error; |
| 448 | } |
| 449 | if (ret > 0) { |
| 450 | BUG_ON(path->slots[0] == 0); |
| 451 | path->slots[0]--; |
| 452 | } |
| 453 | reada_truncate(root, path, inode->i_ino); |
| 454 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
| 455 | found_key = &leaf->items[path->slots[0]].key; |
| 456 | found_type = btrfs_disk_key_type(found_key); |
| 457 | |
| 458 | if (btrfs_disk_key_objectid(found_key) != inode->i_ino) |
| 459 | break; |
| 460 | if (found_type != BTRFS_CSUM_ITEM_KEY && |
| 461 | found_type != BTRFS_DIR_ITEM_KEY && |
| 462 | found_type != BTRFS_DIR_INDEX_KEY && |
| 463 | found_type != BTRFS_EXTENT_DATA_KEY) |
| 464 | break; |
| 465 | |
| 466 | item_end = btrfs_disk_key_offset(found_key); |
| 467 | if (found_type == BTRFS_EXTENT_DATA_KEY) { |
| 468 | fi = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), |
| 469 | path->slots[0], |
| 470 | struct btrfs_file_extent_item); |
| 471 | if (btrfs_file_extent_type(fi) != |
| 472 | BTRFS_FILE_EXTENT_INLINE) { |
| 473 | item_end += btrfs_file_extent_num_blocks(fi) << |
| 474 | inode->i_blkbits; |
| 475 | } |
| 476 | } |
| 477 | if (found_type == BTRFS_CSUM_ITEM_KEY) { |
| 478 | ret = btrfs_csum_truncate(trans, root, path, |
| 479 | inode->i_size); |
| 480 | BUG_ON(ret); |
| 481 | } |
| 482 | if (item_end < inode->i_size) { |
| 483 | if (found_type) { |
| 484 | btrfs_set_key_type(&key, found_type - 1); |
| 485 | continue; |
| 486 | } |
| 487 | break; |
| 488 | } |
| 489 | if (btrfs_disk_key_offset(found_key) >= inode->i_size) |
| 490 | del_item = 1; |
| 491 | else |
| 492 | del_item = 0; |
| 493 | found_extent = 0; |
| 494 | |
| 495 | /* FIXME, shrink the extent if the ref count is only 1 */ |
| 496 | if (found_type == BTRFS_EXTENT_DATA_KEY && |
| 497 | btrfs_file_extent_type(fi) != |
| 498 | BTRFS_FILE_EXTENT_INLINE) { |
| 499 | u64 num_dec; |
| 500 | if (!del_item) { |
| 501 | u64 orig_num_blocks = |
| 502 | btrfs_file_extent_num_blocks(fi); |
| 503 | extent_num_blocks = inode->i_size - |
| 504 | btrfs_disk_key_offset(found_key) + |
| 505 | root->blocksize - 1; |
| 506 | extent_num_blocks >>= inode->i_blkbits; |
| 507 | btrfs_set_file_extent_num_blocks(fi, |
| 508 | extent_num_blocks); |
| 509 | inode->i_blocks -= (orig_num_blocks - |
| 510 | extent_num_blocks) << 3; |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 511 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 512 | } else { |
| 513 | extent_start = |
| 514 | btrfs_file_extent_disk_blocknr(fi); |
| 515 | extent_num_blocks = |
| 516 | btrfs_file_extent_disk_num_blocks(fi); |
| 517 | /* FIXME blocksize != 4096 */ |
| 518 | num_dec = btrfs_file_extent_num_blocks(fi) << 3; |
| 519 | if (extent_start != 0) { |
| 520 | found_extent = 1; |
| 521 | inode->i_blocks -= num_dec; |
| 522 | } |
| 523 | } |
| 524 | } |
| 525 | if (del_item) { |
| 526 | ret = btrfs_del_item(trans, root, path); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 527 | if (ret) |
| 528 | goto error; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 529 | } else { |
| 530 | break; |
| 531 | } |
| 532 | btrfs_release_path(root, path); |
| 533 | if (found_extent) { |
| 534 | ret = btrfs_free_extent(trans, root, extent_start, |
| 535 | extent_num_blocks, 0); |
| 536 | BUG_ON(ret); |
| 537 | } |
| 538 | } |
| 539 | ret = 0; |
| 540 | error: |
| 541 | btrfs_release_path(root, path); |
| 542 | btrfs_free_path(path); |
| 543 | inode->i_sb->s_dirt = 1; |
| 544 | return ret; |
| 545 | } |
| 546 | |
| 547 | /* |
| 548 | * taken from block_truncate_page, but does cow as it zeros out |
| 549 | * any bytes left in the last page in the file. |
| 550 | */ |
| 551 | static int btrfs_truncate_page(struct address_space *mapping, loff_t from) |
| 552 | { |
| 553 | struct inode *inode = mapping->host; |
| 554 | unsigned blocksize = 1 << inode->i_blkbits; |
| 555 | pgoff_t index = from >> PAGE_CACHE_SHIFT; |
| 556 | unsigned offset = from & (PAGE_CACHE_SIZE-1); |
| 557 | struct page *page; |
| 558 | char *kaddr; |
| 559 | int ret = 0; |
| 560 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 561 | u64 alloc_hint = 0; |
| 562 | struct btrfs_key ins; |
| 563 | struct btrfs_trans_handle *trans; |
| 564 | |
| 565 | if ((offset & (blocksize - 1)) == 0) |
| 566 | goto out; |
| 567 | |
| 568 | ret = -ENOMEM; |
| 569 | page = grab_cache_page(mapping, index); |
| 570 | if (!page) |
| 571 | goto out; |
| 572 | |
| 573 | if (!PageUptodate(page)) { |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 574 | ret = btrfs_readpage(NULL, page); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 575 | lock_page(page); |
| 576 | if (!PageUptodate(page)) { |
| 577 | ret = -EIO; |
| 578 | goto out; |
| 579 | } |
| 580 | } |
| 581 | mutex_lock(&root->fs_info->fs_mutex); |
| 582 | trans = btrfs_start_transaction(root, 1); |
| 583 | btrfs_set_trans_block_group(trans, inode); |
| 584 | |
| 585 | ret = btrfs_drop_extents(trans, root, inode, |
| 586 | page->index << PAGE_CACHE_SHIFT, |
| 587 | (page->index + 1) << PAGE_CACHE_SHIFT, |
| 588 | &alloc_hint); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 589 | if (ret) |
| 590 | goto out; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 591 | ret = btrfs_alloc_extent(trans, root, inode->i_ino, 1, |
| 592 | alloc_hint, (u64)-1, &ins, 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 593 | if (ret) |
| 594 | goto out; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 595 | ret = btrfs_insert_file_extent(trans, root, inode->i_ino, |
| 596 | page->index << PAGE_CACHE_SHIFT, |
| 597 | ins.objectid, 1, 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 598 | if (ret) |
| 599 | goto out; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 600 | SetPageChecked(page); |
| 601 | kaddr = kmap(page); |
| 602 | memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset); |
| 603 | flush_dcache_page(page); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 604 | ret = btrfs_csum_file_block(trans, root, inode->i_ino, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 605 | page->index << PAGE_CACHE_SHIFT, |
| 606 | kaddr, PAGE_CACHE_SIZE); |
| 607 | kunmap(page); |
| 608 | btrfs_end_transaction(trans, root); |
| 609 | mutex_unlock(&root->fs_info->fs_mutex); |
| 610 | |
| 611 | set_page_dirty(page); |
| 612 | unlock_page(page); |
| 613 | page_cache_release(page); |
| 614 | out: |
| 615 | return ret; |
| 616 | } |
| 617 | |
| 618 | static int btrfs_setattr(struct dentry *dentry, struct iattr *attr) |
| 619 | { |
| 620 | struct inode *inode = dentry->d_inode; |
| 621 | int err; |
| 622 | |
| 623 | err = inode_change_ok(inode, attr); |
| 624 | if (err) |
| 625 | return err; |
| 626 | |
| 627 | if (S_ISREG(inode->i_mode) && |
| 628 | attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) { |
| 629 | struct btrfs_trans_handle *trans; |
| 630 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 631 | u64 mask = root->blocksize - 1; |
| 632 | u64 pos = (inode->i_size + mask) & ~mask; |
| 633 | u64 hole_size; |
| 634 | |
| 635 | if (attr->ia_size <= pos) |
| 636 | goto out; |
| 637 | |
| 638 | btrfs_truncate_page(inode->i_mapping, inode->i_size); |
| 639 | |
| 640 | hole_size = (attr->ia_size - pos + mask) & ~mask; |
| 641 | hole_size >>= inode->i_blkbits; |
| 642 | |
| 643 | mutex_lock(&root->fs_info->fs_mutex); |
| 644 | trans = btrfs_start_transaction(root, 1); |
| 645 | btrfs_set_trans_block_group(trans, inode); |
| 646 | err = btrfs_insert_file_extent(trans, root, inode->i_ino, |
| 647 | pos, 0, 0, hole_size); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 648 | btrfs_end_transaction(trans, root); |
| 649 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 650 | if (err) |
| 651 | return err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 652 | } |
| 653 | out: |
| 654 | err = inode_setattr(inode, attr); |
| 655 | |
| 656 | return err; |
| 657 | } |
| 658 | void btrfs_delete_inode(struct inode *inode) |
| 659 | { |
| 660 | struct btrfs_trans_handle *trans; |
| 661 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 662 | int ret; |
| 663 | |
| 664 | truncate_inode_pages(&inode->i_data, 0); |
| 665 | if (is_bad_inode(inode)) { |
| 666 | goto no_delete; |
| 667 | } |
| 668 | inode->i_size = 0; |
| 669 | mutex_lock(&root->fs_info->fs_mutex); |
| 670 | trans = btrfs_start_transaction(root, 1); |
| 671 | btrfs_set_trans_block_group(trans, inode); |
| 672 | ret = btrfs_truncate_in_trans(trans, root, inode); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 673 | if (ret) |
| 674 | goto no_delete_lock; |
| 675 | ret = btrfs_free_inode(trans, root, inode); |
| 676 | if (ret) |
| 677 | goto no_delete_lock; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 678 | btrfs_end_transaction(trans, root); |
| 679 | mutex_unlock(&root->fs_info->fs_mutex); |
| 680 | btrfs_btree_balance_dirty(root); |
| 681 | return; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 682 | |
| 683 | no_delete_lock: |
| 684 | btrfs_end_transaction(trans, root); |
| 685 | mutex_unlock(&root->fs_info->fs_mutex); |
| 686 | btrfs_btree_balance_dirty(root); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 687 | no_delete: |
| 688 | clear_inode(inode); |
| 689 | } |
| 690 | |
| 691 | /* |
| 692 | * this returns the key found in the dir entry in the location pointer. |
| 693 | * If no dir entries were found, location->objectid is 0. |
| 694 | */ |
| 695 | static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry, |
| 696 | struct btrfs_key *location) |
| 697 | { |
| 698 | const char *name = dentry->d_name.name; |
| 699 | int namelen = dentry->d_name.len; |
| 700 | struct btrfs_dir_item *di; |
| 701 | struct btrfs_path *path; |
| 702 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 703 | int ret; |
| 704 | |
| 705 | path = btrfs_alloc_path(); |
| 706 | BUG_ON(!path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 707 | di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name, |
| 708 | namelen, 0); |
| 709 | if (!di || IS_ERR(di)) { |
| 710 | location->objectid = 0; |
| 711 | ret = 0; |
| 712 | goto out; |
| 713 | } |
| 714 | btrfs_disk_key_to_cpu(location, &di->location); |
| 715 | out: |
| 716 | btrfs_release_path(root, path); |
| 717 | btrfs_free_path(path); |
| 718 | return ret; |
| 719 | } |
| 720 | |
| 721 | /* |
| 722 | * when we hit a tree root in a directory, the btrfs part of the inode |
| 723 | * needs to be changed to reflect the root directory of the tree root. This |
| 724 | * is kind of like crossing a mount point. |
| 725 | */ |
| 726 | static int fixup_tree_root_location(struct btrfs_root *root, |
| 727 | struct btrfs_key *location, |
| 728 | struct btrfs_root **sub_root) |
| 729 | { |
| 730 | struct btrfs_path *path; |
| 731 | struct btrfs_root_item *ri; |
| 732 | |
| 733 | if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY) |
| 734 | return 0; |
| 735 | if (location->objectid == BTRFS_ROOT_TREE_OBJECTID) |
| 736 | return 0; |
| 737 | |
| 738 | path = btrfs_alloc_path(); |
| 739 | BUG_ON(!path); |
| 740 | mutex_lock(&root->fs_info->fs_mutex); |
| 741 | |
| 742 | *sub_root = btrfs_read_fs_root(root->fs_info, location); |
| 743 | if (IS_ERR(*sub_root)) |
| 744 | return PTR_ERR(*sub_root); |
| 745 | |
| 746 | ri = &(*sub_root)->root_item; |
| 747 | location->objectid = btrfs_root_dirid(ri); |
| 748 | location->flags = 0; |
| 749 | btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); |
| 750 | location->offset = 0; |
| 751 | |
| 752 | btrfs_free_path(path); |
| 753 | mutex_unlock(&root->fs_info->fs_mutex); |
| 754 | return 0; |
| 755 | } |
| 756 | |
| 757 | static int btrfs_init_locked_inode(struct inode *inode, void *p) |
| 758 | { |
| 759 | struct btrfs_iget_args *args = p; |
| 760 | inode->i_ino = args->ino; |
| 761 | BTRFS_I(inode)->root = args->root; |
| 762 | return 0; |
| 763 | } |
| 764 | |
| 765 | static int btrfs_find_actor(struct inode *inode, void *opaque) |
| 766 | { |
| 767 | struct btrfs_iget_args *args = opaque; |
| 768 | return (args->ino == inode->i_ino && |
| 769 | args->root == BTRFS_I(inode)->root); |
| 770 | } |
| 771 | |
| 772 | struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid, |
| 773 | struct btrfs_root *root) |
| 774 | { |
| 775 | struct inode *inode; |
| 776 | struct btrfs_iget_args args; |
| 777 | args.ino = objectid; |
| 778 | args.root = root; |
| 779 | |
| 780 | inode = iget5_locked(s, objectid, btrfs_find_actor, |
| 781 | btrfs_init_locked_inode, |
| 782 | (void *)&args); |
| 783 | return inode; |
| 784 | } |
| 785 | |
| 786 | static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry, |
| 787 | struct nameidata *nd) |
| 788 | { |
| 789 | struct inode * inode; |
| 790 | struct btrfs_inode *bi = BTRFS_I(dir); |
| 791 | struct btrfs_root *root = bi->root; |
| 792 | struct btrfs_root *sub_root = root; |
| 793 | struct btrfs_key location; |
| 794 | int ret; |
| 795 | |
| 796 | if (dentry->d_name.len > BTRFS_NAME_LEN) |
| 797 | return ERR_PTR(-ENAMETOOLONG); |
| 798 | mutex_lock(&root->fs_info->fs_mutex); |
| 799 | ret = btrfs_inode_by_name(dir, dentry, &location); |
| 800 | mutex_unlock(&root->fs_info->fs_mutex); |
| 801 | if (ret < 0) |
| 802 | return ERR_PTR(ret); |
| 803 | inode = NULL; |
| 804 | if (location.objectid) { |
| 805 | ret = fixup_tree_root_location(root, &location, &sub_root); |
| 806 | if (ret < 0) |
| 807 | return ERR_PTR(ret); |
| 808 | if (ret > 0) |
| 809 | return ERR_PTR(-ENOENT); |
| 810 | inode = btrfs_iget_locked(dir->i_sb, location.objectid, |
| 811 | sub_root); |
| 812 | if (!inode) |
| 813 | return ERR_PTR(-EACCES); |
| 814 | if (inode->i_state & I_NEW) { |
| 815 | /* the inode and parent dir are two different roots */ |
| 816 | if (sub_root != root) { |
| 817 | igrab(inode); |
| 818 | sub_root->inode = inode; |
| 819 | } |
| 820 | BTRFS_I(inode)->root = sub_root; |
| 821 | memcpy(&BTRFS_I(inode)->location, &location, |
| 822 | sizeof(location)); |
| 823 | btrfs_read_locked_inode(inode); |
| 824 | unlock_new_inode(inode); |
| 825 | } |
| 826 | } |
| 827 | return d_splice_alias(inode, dentry); |
| 828 | } |
| 829 | |
| 830 | /* |
| 831 | * readahead one full node of leaves as long as their keys include |
| 832 | * the objectid supplied |
| 833 | */ |
| 834 | static void reada_leaves(struct btrfs_root *root, struct btrfs_path *path, |
| 835 | u64 objectid) |
| 836 | { |
| 837 | struct btrfs_node *node; |
| 838 | int i; |
| 839 | u32 nritems; |
| 840 | u64 item_objectid; |
| 841 | u64 blocknr; |
| 842 | int slot; |
| 843 | int ret; |
| 844 | |
| 845 | if (!path->nodes[1]) |
| 846 | return; |
| 847 | node = btrfs_buffer_node(path->nodes[1]); |
| 848 | slot = path->slots[1]; |
| 849 | nritems = btrfs_header_nritems(&node->header); |
| 850 | for (i = slot + 1; i < nritems; i++) { |
| 851 | item_objectid = btrfs_disk_key_objectid(&node->ptrs[i].key); |
| 852 | if (item_objectid != objectid) |
| 853 | break; |
| 854 | blocknr = btrfs_node_blockptr(node, i); |
| 855 | ret = readahead_tree_block(root, blocknr); |
| 856 | if (ret) |
| 857 | break; |
| 858 | } |
| 859 | } |
| 860 | static unsigned char btrfs_filetype_table[] = { |
| 861 | DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK |
| 862 | }; |
| 863 | |
| 864 | static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir) |
| 865 | { |
| 866 | struct inode *inode = filp->f_path.dentry->d_inode; |
| 867 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 868 | struct btrfs_item *item; |
| 869 | struct btrfs_dir_item *di; |
| 870 | struct btrfs_key key; |
| 871 | struct btrfs_path *path; |
| 872 | int ret; |
| 873 | u32 nritems; |
| 874 | struct btrfs_leaf *leaf; |
| 875 | int slot; |
| 876 | int advance; |
| 877 | unsigned char d_type; |
| 878 | int over = 0; |
| 879 | u32 di_cur; |
| 880 | u32 di_total; |
| 881 | u32 di_len; |
| 882 | int key_type = BTRFS_DIR_INDEX_KEY; |
| 883 | |
| 884 | /* FIXME, use a real flag for deciding about the key type */ |
| 885 | if (root->fs_info->tree_root == root) |
| 886 | key_type = BTRFS_DIR_ITEM_KEY; |
| 887 | mutex_lock(&root->fs_info->fs_mutex); |
| 888 | key.objectid = inode->i_ino; |
| 889 | key.flags = 0; |
| 890 | btrfs_set_key_type(&key, key_type); |
| 891 | key.offset = filp->f_pos; |
| 892 | path = btrfs_alloc_path(); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 893 | ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); |
| 894 | if (ret < 0) |
| 895 | goto err; |
| 896 | advance = 0; |
| 897 | reada_leaves(root, path, inode->i_ino); |
| 898 | while(1) { |
| 899 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
| 900 | nritems = btrfs_header_nritems(&leaf->header); |
| 901 | slot = path->slots[0]; |
| 902 | if (advance || slot >= nritems) { |
| 903 | if (slot >= nritems -1) { |
| 904 | reada_leaves(root, path, inode->i_ino); |
| 905 | ret = btrfs_next_leaf(root, path); |
| 906 | if (ret) |
| 907 | break; |
| 908 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
| 909 | nritems = btrfs_header_nritems(&leaf->header); |
| 910 | slot = path->slots[0]; |
| 911 | } else { |
| 912 | slot++; |
| 913 | path->slots[0]++; |
| 914 | } |
| 915 | } |
| 916 | advance = 1; |
| 917 | item = leaf->items + slot; |
| 918 | if (btrfs_disk_key_objectid(&item->key) != key.objectid) |
| 919 | break; |
| 920 | if (btrfs_disk_key_type(&item->key) != key_type) |
| 921 | break; |
| 922 | if (btrfs_disk_key_offset(&item->key) < filp->f_pos) |
| 923 | continue; |
| 924 | filp->f_pos = btrfs_disk_key_offset(&item->key); |
| 925 | advance = 1; |
| 926 | di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item); |
| 927 | di_cur = 0; |
| 928 | di_total = btrfs_item_size(leaf->items + slot); |
| 929 | while(di_cur < di_total) { |
| 930 | d_type = btrfs_filetype_table[btrfs_dir_type(di)]; |
| 931 | over = filldir(dirent, (const char *)(di + 1), |
| 932 | btrfs_dir_name_len(di), |
| 933 | btrfs_disk_key_offset(&item->key), |
| 934 | btrfs_disk_key_objectid(&di->location), |
| 935 | d_type); |
| 936 | if (over) |
| 937 | goto nopos; |
| 938 | di_len = btrfs_dir_name_len(di) + sizeof(*di); |
| 939 | di_cur += di_len; |
| 940 | di = (struct btrfs_dir_item *)((char *)di + di_len); |
| 941 | } |
| 942 | } |
| 943 | filp->f_pos++; |
| 944 | nopos: |
| 945 | ret = 0; |
| 946 | err: |
| 947 | btrfs_release_path(root, path); |
| 948 | btrfs_free_path(path); |
| 949 | mutex_unlock(&root->fs_info->fs_mutex); |
| 950 | return ret; |
| 951 | } |
| 952 | |
| 953 | int btrfs_write_inode(struct inode *inode, int wait) |
| 954 | { |
| 955 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 956 | struct btrfs_trans_handle *trans; |
| 957 | int ret = 0; |
| 958 | |
| 959 | if (wait) { |
| 960 | mutex_lock(&root->fs_info->fs_mutex); |
| 961 | trans = btrfs_start_transaction(root, 1); |
| 962 | btrfs_set_trans_block_group(trans, inode); |
| 963 | ret = btrfs_commit_transaction(trans, root); |
| 964 | mutex_unlock(&root->fs_info->fs_mutex); |
| 965 | } |
| 966 | return ret; |
| 967 | } |
| 968 | |
| 969 | /* |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 970 | * This is somewhat expensive, updating the tree every time the |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 971 | * inode changes. But, it is most likely to find the inode in cache. |
| 972 | * FIXME, needs more benchmarking...there are no reasons other than performance |
| 973 | * to keep or drop this code. |
| 974 | */ |
| 975 | void btrfs_dirty_inode(struct inode *inode) |
| 976 | { |
| 977 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 978 | struct btrfs_trans_handle *trans; |
| 979 | |
| 980 | mutex_lock(&root->fs_info->fs_mutex); |
| 981 | trans = btrfs_start_transaction(root, 1); |
| 982 | btrfs_set_trans_block_group(trans, inode); |
| 983 | btrfs_update_inode(trans, root, inode); |
| 984 | btrfs_end_transaction(trans, root); |
| 985 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 986 | } |
| 987 | |
| 988 | static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, |
| 989 | struct btrfs_root *root, |
| 990 | u64 objectid, |
| 991 | struct btrfs_block_group_cache *group, |
| 992 | int mode) |
| 993 | { |
| 994 | struct inode *inode; |
| 995 | struct btrfs_inode_item inode_item; |
| 996 | struct btrfs_key *location; |
| 997 | int ret; |
| 998 | int owner; |
| 999 | |
| 1000 | inode = new_inode(root->fs_info->sb); |
| 1001 | if (!inode) |
| 1002 | return ERR_PTR(-ENOMEM); |
| 1003 | |
| 1004 | BTRFS_I(inode)->root = root; |
| 1005 | if (mode & S_IFDIR) |
| 1006 | owner = 0; |
| 1007 | else |
| 1008 | owner = 1; |
| 1009 | group = btrfs_find_block_group(root, group, 0, 0, owner); |
| 1010 | BTRFS_I(inode)->block_group = group; |
| 1011 | |
| 1012 | inode->i_uid = current->fsuid; |
| 1013 | inode->i_gid = current->fsgid; |
| 1014 | inode->i_mode = mode; |
| 1015 | inode->i_ino = objectid; |
| 1016 | inode->i_blocks = 0; |
| 1017 | inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME; |
| 1018 | fill_inode_item(&inode_item, inode); |
| 1019 | location = &BTRFS_I(inode)->location; |
| 1020 | location->objectid = objectid; |
| 1021 | location->flags = 0; |
| 1022 | location->offset = 0; |
| 1023 | btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY); |
| 1024 | |
| 1025 | ret = btrfs_insert_inode(trans, root, objectid, &inode_item); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1026 | if (ret) |
| 1027 | return ERR_PTR(ret); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1028 | insert_inode_hash(inode); |
| 1029 | return inode; |
| 1030 | } |
| 1031 | |
| 1032 | static inline u8 btrfs_inode_type(struct inode *inode) |
| 1033 | { |
| 1034 | return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT]; |
| 1035 | } |
| 1036 | |
| 1037 | static int btrfs_add_link(struct btrfs_trans_handle *trans, |
| 1038 | struct dentry *dentry, struct inode *inode) |
| 1039 | { |
| 1040 | int ret; |
| 1041 | struct btrfs_key key; |
| 1042 | struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root; |
Chris Mason | 79c4458 | 2007-06-25 10:09:33 -0400 | [diff] [blame] | 1043 | struct inode *parent_inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1044 | key.objectid = inode->i_ino; |
| 1045 | key.flags = 0; |
| 1046 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); |
| 1047 | key.offset = 0; |
| 1048 | |
| 1049 | ret = btrfs_insert_dir_item(trans, root, |
| 1050 | dentry->d_name.name, dentry->d_name.len, |
| 1051 | dentry->d_parent->d_inode->i_ino, |
| 1052 | &key, btrfs_inode_type(inode)); |
| 1053 | if (ret == 0) { |
Chris Mason | 79c4458 | 2007-06-25 10:09:33 -0400 | [diff] [blame] | 1054 | parent_inode = dentry->d_parent->d_inode; |
| 1055 | parent_inode->i_size += dentry->d_name.len * 2; |
| 1056 | parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1057 | ret = btrfs_update_inode(trans, root, |
| 1058 | dentry->d_parent->d_inode); |
| 1059 | } |
| 1060 | return ret; |
| 1061 | } |
| 1062 | |
| 1063 | static int btrfs_add_nondir(struct btrfs_trans_handle *trans, |
| 1064 | struct dentry *dentry, struct inode *inode) |
| 1065 | { |
| 1066 | int err = btrfs_add_link(trans, dentry, inode); |
| 1067 | if (!err) { |
| 1068 | d_instantiate(dentry, inode); |
| 1069 | return 0; |
| 1070 | } |
| 1071 | if (err > 0) |
| 1072 | err = -EEXIST; |
| 1073 | return err; |
| 1074 | } |
| 1075 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 1076 | static int btrfs_mknod(struct inode *dir, struct dentry *dentry, |
| 1077 | int mode, dev_t rdev) |
| 1078 | { |
| 1079 | struct btrfs_trans_handle *trans; |
| 1080 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 1081 | struct inode *inode; |
| 1082 | int err; |
| 1083 | int drop_inode = 0; |
| 1084 | u64 objectid; |
| 1085 | |
| 1086 | if (!new_valid_dev(rdev)) |
| 1087 | return -EINVAL; |
| 1088 | |
| 1089 | mutex_lock(&root->fs_info->fs_mutex); |
| 1090 | trans = btrfs_start_transaction(root, 1); |
| 1091 | btrfs_set_trans_block_group(trans, dir); |
| 1092 | |
| 1093 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); |
| 1094 | if (err) { |
| 1095 | err = -ENOSPC; |
| 1096 | goto out_unlock; |
| 1097 | } |
| 1098 | |
| 1099 | inode = btrfs_new_inode(trans, root, objectid, |
| 1100 | BTRFS_I(dir)->block_group, mode); |
| 1101 | err = PTR_ERR(inode); |
| 1102 | if (IS_ERR(inode)) |
| 1103 | goto out_unlock; |
| 1104 | |
| 1105 | btrfs_set_trans_block_group(trans, inode); |
| 1106 | err = btrfs_add_nondir(trans, dentry, inode); |
| 1107 | if (err) |
| 1108 | drop_inode = 1; |
| 1109 | else { |
| 1110 | inode->i_op = &btrfs_special_inode_operations; |
| 1111 | init_special_inode(inode, inode->i_mode, rdev); |
| 1112 | } |
| 1113 | dir->i_sb->s_dirt = 1; |
| 1114 | btrfs_update_inode_block_group(trans, inode); |
| 1115 | btrfs_update_inode_block_group(trans, dir); |
| 1116 | out_unlock: |
| 1117 | btrfs_end_transaction(trans, root); |
| 1118 | mutex_unlock(&root->fs_info->fs_mutex); |
| 1119 | |
| 1120 | if (drop_inode) { |
| 1121 | inode_dec_link_count(inode); |
| 1122 | iput(inode); |
| 1123 | } |
| 1124 | btrfs_btree_balance_dirty(root); |
| 1125 | return err; |
| 1126 | } |
| 1127 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1128 | static int btrfs_create(struct inode *dir, struct dentry *dentry, |
| 1129 | int mode, struct nameidata *nd) |
| 1130 | { |
| 1131 | struct btrfs_trans_handle *trans; |
| 1132 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 1133 | struct inode *inode; |
| 1134 | int err; |
| 1135 | int drop_inode = 0; |
| 1136 | u64 objectid; |
| 1137 | |
| 1138 | mutex_lock(&root->fs_info->fs_mutex); |
| 1139 | trans = btrfs_start_transaction(root, 1); |
| 1140 | btrfs_set_trans_block_group(trans, dir); |
| 1141 | |
| 1142 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); |
| 1143 | if (err) { |
| 1144 | err = -ENOSPC; |
| 1145 | goto out_unlock; |
| 1146 | } |
| 1147 | |
| 1148 | inode = btrfs_new_inode(trans, root, objectid, |
| 1149 | BTRFS_I(dir)->block_group, mode); |
| 1150 | err = PTR_ERR(inode); |
| 1151 | if (IS_ERR(inode)) |
| 1152 | goto out_unlock; |
| 1153 | |
| 1154 | btrfs_set_trans_block_group(trans, inode); |
| 1155 | err = btrfs_add_nondir(trans, dentry, inode); |
| 1156 | if (err) |
| 1157 | drop_inode = 1; |
| 1158 | else { |
| 1159 | inode->i_mapping->a_ops = &btrfs_aops; |
| 1160 | inode->i_fop = &btrfs_file_operations; |
| 1161 | inode->i_op = &btrfs_file_inode_operations; |
| 1162 | } |
| 1163 | dir->i_sb->s_dirt = 1; |
| 1164 | btrfs_update_inode_block_group(trans, inode); |
| 1165 | btrfs_update_inode_block_group(trans, dir); |
| 1166 | out_unlock: |
| 1167 | btrfs_end_transaction(trans, root); |
| 1168 | mutex_unlock(&root->fs_info->fs_mutex); |
| 1169 | |
| 1170 | if (drop_inode) { |
| 1171 | inode_dec_link_count(inode); |
| 1172 | iput(inode); |
| 1173 | } |
| 1174 | btrfs_btree_balance_dirty(root); |
| 1175 | return err; |
| 1176 | } |
| 1177 | |
| 1178 | static int btrfs_link(struct dentry *old_dentry, struct inode *dir, |
| 1179 | struct dentry *dentry) |
| 1180 | { |
| 1181 | struct btrfs_trans_handle *trans; |
| 1182 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 1183 | struct inode *inode = old_dentry->d_inode; |
| 1184 | int err; |
| 1185 | int drop_inode = 0; |
| 1186 | |
| 1187 | if (inode->i_nlink == 0) |
| 1188 | return -ENOENT; |
| 1189 | |
| 1190 | inc_nlink(inode); |
| 1191 | mutex_lock(&root->fs_info->fs_mutex); |
| 1192 | trans = btrfs_start_transaction(root, 1); |
| 1193 | btrfs_set_trans_block_group(trans, dir); |
| 1194 | atomic_inc(&inode->i_count); |
| 1195 | err = btrfs_add_nondir(trans, dentry, inode); |
| 1196 | if (err) |
| 1197 | drop_inode = 1; |
| 1198 | dir->i_sb->s_dirt = 1; |
| 1199 | btrfs_update_inode_block_group(trans, dir); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1200 | err = btrfs_update_inode(trans, root, inode); |
| 1201 | if (err) |
| 1202 | drop_inode = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1203 | |
| 1204 | btrfs_end_transaction(trans, root); |
| 1205 | mutex_unlock(&root->fs_info->fs_mutex); |
| 1206 | |
| 1207 | if (drop_inode) { |
| 1208 | inode_dec_link_count(inode); |
| 1209 | iput(inode); |
| 1210 | } |
| 1211 | btrfs_btree_balance_dirty(root); |
| 1212 | return err; |
| 1213 | } |
| 1214 | |
| 1215 | static int btrfs_make_empty_dir(struct btrfs_trans_handle *trans, |
| 1216 | struct btrfs_root *root, |
| 1217 | u64 objectid, u64 dirid) |
| 1218 | { |
| 1219 | int ret; |
| 1220 | char buf[2]; |
| 1221 | struct btrfs_key key; |
| 1222 | |
| 1223 | buf[0] = '.'; |
| 1224 | buf[1] = '.'; |
| 1225 | |
| 1226 | key.objectid = objectid; |
| 1227 | key.offset = 0; |
| 1228 | key.flags = 0; |
| 1229 | btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY); |
| 1230 | |
| 1231 | ret = btrfs_insert_dir_item(trans, root, buf, 1, objectid, |
| 1232 | &key, BTRFS_FT_DIR); |
| 1233 | if (ret) |
| 1234 | goto error; |
| 1235 | key.objectid = dirid; |
| 1236 | ret = btrfs_insert_dir_item(trans, root, buf, 2, objectid, |
| 1237 | &key, BTRFS_FT_DIR); |
| 1238 | if (ret) |
| 1239 | goto error; |
| 1240 | error: |
| 1241 | return ret; |
| 1242 | } |
| 1243 | |
| 1244 | static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode) |
| 1245 | { |
| 1246 | struct inode *inode; |
| 1247 | struct btrfs_trans_handle *trans; |
| 1248 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 1249 | int err = 0; |
| 1250 | int drop_on_err = 0; |
| 1251 | u64 objectid; |
| 1252 | |
| 1253 | mutex_lock(&root->fs_info->fs_mutex); |
| 1254 | trans = btrfs_start_transaction(root, 1); |
| 1255 | btrfs_set_trans_block_group(trans, dir); |
| 1256 | if (IS_ERR(trans)) { |
| 1257 | err = PTR_ERR(trans); |
| 1258 | goto out_unlock; |
| 1259 | } |
| 1260 | |
| 1261 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); |
| 1262 | if (err) { |
| 1263 | err = -ENOSPC; |
| 1264 | goto out_unlock; |
| 1265 | } |
| 1266 | |
| 1267 | inode = btrfs_new_inode(trans, root, objectid, |
| 1268 | BTRFS_I(dir)->block_group, S_IFDIR | mode); |
| 1269 | if (IS_ERR(inode)) { |
| 1270 | err = PTR_ERR(inode); |
| 1271 | goto out_fail; |
| 1272 | } |
| 1273 | drop_on_err = 1; |
| 1274 | inode->i_op = &btrfs_dir_inode_operations; |
| 1275 | inode->i_fop = &btrfs_dir_file_operations; |
| 1276 | btrfs_set_trans_block_group(trans, inode); |
| 1277 | |
| 1278 | err = btrfs_make_empty_dir(trans, root, inode->i_ino, dir->i_ino); |
| 1279 | if (err) |
| 1280 | goto out_fail; |
| 1281 | |
| 1282 | inode->i_size = 6; |
| 1283 | err = btrfs_update_inode(trans, root, inode); |
| 1284 | if (err) |
| 1285 | goto out_fail; |
| 1286 | err = btrfs_add_link(trans, dentry, inode); |
| 1287 | if (err) |
| 1288 | goto out_fail; |
| 1289 | d_instantiate(dentry, inode); |
| 1290 | drop_on_err = 0; |
| 1291 | dir->i_sb->s_dirt = 1; |
| 1292 | btrfs_update_inode_block_group(trans, inode); |
| 1293 | btrfs_update_inode_block_group(trans, dir); |
| 1294 | |
| 1295 | out_fail: |
| 1296 | btrfs_end_transaction(trans, root); |
| 1297 | out_unlock: |
| 1298 | mutex_unlock(&root->fs_info->fs_mutex); |
| 1299 | if (drop_on_err) |
| 1300 | iput(inode); |
| 1301 | btrfs_btree_balance_dirty(root); |
| 1302 | return err; |
| 1303 | } |
| 1304 | |
| 1305 | /* |
| 1306 | * FIBMAP and others want to pass in a fake buffer head. They need to |
| 1307 | * use BTRFS_GET_BLOCK_NO_DIRECT to make sure we don't try to memcpy |
| 1308 | * any packed file data into the fake bh |
| 1309 | */ |
| 1310 | #define BTRFS_GET_BLOCK_NO_CREATE 0 |
| 1311 | #define BTRFS_GET_BLOCK_CREATE 1 |
| 1312 | #define BTRFS_GET_BLOCK_NO_DIRECT 2 |
| 1313 | |
| 1314 | /* |
| 1315 | * FIXME create==1 doe not work. |
| 1316 | */ |
| 1317 | static int btrfs_get_block_lock(struct inode *inode, sector_t iblock, |
| 1318 | struct buffer_head *result, int create) |
| 1319 | { |
| 1320 | int ret; |
| 1321 | int err = 0; |
| 1322 | u64 blocknr; |
| 1323 | u64 extent_start = 0; |
| 1324 | u64 extent_end = 0; |
| 1325 | u64 objectid = inode->i_ino; |
| 1326 | u32 found_type; |
| 1327 | u64 alloc_hint = 0; |
| 1328 | struct btrfs_path *path; |
| 1329 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1330 | struct btrfs_file_extent_item *item; |
| 1331 | struct btrfs_leaf *leaf; |
| 1332 | struct btrfs_disk_key *found_key; |
| 1333 | struct btrfs_trans_handle *trans = NULL; |
| 1334 | |
| 1335 | path = btrfs_alloc_path(); |
| 1336 | BUG_ON(!path); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1337 | if (create & BTRFS_GET_BLOCK_CREATE) { |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 1338 | /* |
| 1339 | * danger!, this only works if the page is properly up |
| 1340 | * to date somehow |
| 1341 | */ |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1342 | trans = btrfs_start_transaction(root, 1); |
| 1343 | if (!trans) { |
| 1344 | err = -ENOMEM; |
| 1345 | goto out; |
| 1346 | } |
| 1347 | ret = btrfs_drop_extents(trans, root, inode, |
| 1348 | iblock << inode->i_blkbits, |
| 1349 | (iblock + 1) << inode->i_blkbits, |
| 1350 | &alloc_hint); |
| 1351 | BUG_ON(ret); |
| 1352 | } |
| 1353 | |
| 1354 | ret = btrfs_lookup_file_extent(NULL, root, path, |
Aneesh | f1ace24 | 2007-06-13 16:18:26 -0400 | [diff] [blame] | 1355 | objectid, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1356 | iblock << inode->i_blkbits, 0); |
| 1357 | if (ret < 0) { |
| 1358 | err = ret; |
| 1359 | goto out; |
| 1360 | } |
| 1361 | |
| 1362 | if (ret != 0) { |
| 1363 | if (path->slots[0] == 0) { |
| 1364 | btrfs_release_path(root, path); |
| 1365 | goto not_found; |
| 1366 | } |
| 1367 | path->slots[0]--; |
| 1368 | } |
| 1369 | |
| 1370 | item = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), path->slots[0], |
| 1371 | struct btrfs_file_extent_item); |
| 1372 | leaf = btrfs_buffer_leaf(path->nodes[0]); |
| 1373 | blocknr = btrfs_file_extent_disk_blocknr(item); |
| 1374 | blocknr += btrfs_file_extent_offset(item); |
| 1375 | |
| 1376 | /* are we inside the extent that was found? */ |
| 1377 | found_key = &leaf->items[path->slots[0]].key; |
| 1378 | found_type = btrfs_disk_key_type(found_key); |
| 1379 | if (btrfs_disk_key_objectid(found_key) != objectid || |
| 1380 | found_type != BTRFS_EXTENT_DATA_KEY) { |
| 1381 | extent_end = 0; |
| 1382 | extent_start = 0; |
| 1383 | goto not_found; |
| 1384 | } |
| 1385 | found_type = btrfs_file_extent_type(item); |
| 1386 | extent_start = btrfs_disk_key_offset(&leaf->items[path->slots[0]].key); |
| 1387 | if (found_type == BTRFS_FILE_EXTENT_REG) { |
| 1388 | extent_start = extent_start >> inode->i_blkbits; |
| 1389 | extent_end = extent_start + btrfs_file_extent_num_blocks(item); |
| 1390 | err = 0; |
| 1391 | if (btrfs_file_extent_disk_blocknr(item) == 0) |
| 1392 | goto out; |
| 1393 | if (iblock >= extent_start && iblock < extent_end) { |
| 1394 | btrfs_map_bh_to_logical(root, result, blocknr + |
| 1395 | iblock - extent_start); |
| 1396 | goto out; |
| 1397 | } |
| 1398 | } else if (found_type == BTRFS_FILE_EXTENT_INLINE) { |
| 1399 | char *ptr; |
| 1400 | char *map; |
| 1401 | u32 size; |
| 1402 | |
| 1403 | if (create & BTRFS_GET_BLOCK_NO_DIRECT) { |
| 1404 | err = -EINVAL; |
| 1405 | goto out; |
| 1406 | } |
| 1407 | size = btrfs_file_extent_inline_len(leaf->items + |
| 1408 | path->slots[0]); |
| 1409 | extent_end = (extent_start + size) >> inode->i_blkbits; |
| 1410 | extent_start >>= inode->i_blkbits; |
| 1411 | if (iblock < extent_start || iblock > extent_end) { |
| 1412 | goto not_found; |
| 1413 | } |
| 1414 | ptr = btrfs_file_extent_inline_start(item); |
| 1415 | map = kmap(result->b_page); |
| 1416 | memcpy(map, ptr, size); |
| 1417 | memset(map + size, 0, PAGE_CACHE_SIZE - size); |
| 1418 | flush_dcache_page(result->b_page); |
| 1419 | kunmap(result->b_page); |
| 1420 | set_buffer_uptodate(result); |
| 1421 | SetPageChecked(result->b_page); |
| 1422 | btrfs_map_bh_to_logical(root, result, 0); |
| 1423 | } |
| 1424 | not_found: |
| 1425 | if (create & BTRFS_GET_BLOCK_CREATE) { |
| 1426 | struct btrfs_key ins; |
| 1427 | ret = btrfs_alloc_extent(trans, root, inode->i_ino, |
| 1428 | 1, alloc_hint, (u64)-1, |
| 1429 | &ins, 1); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1430 | if (ret) { |
| 1431 | err = ret; |
| 1432 | goto out; |
| 1433 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1434 | ret = btrfs_insert_file_extent(trans, root, inode->i_ino, |
| 1435 | iblock << inode->i_blkbits, |
| 1436 | ins.objectid, ins.offset, |
| 1437 | ins.offset); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1438 | if (ret) { |
| 1439 | err = ret; |
| 1440 | goto out; |
| 1441 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1442 | btrfs_map_bh_to_logical(root, result, ins.objectid); |
| 1443 | } |
| 1444 | out: |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1445 | if (trans) { |
| 1446 | ret = btrfs_end_transaction(trans, root); |
| 1447 | if (!err) |
| 1448 | err = ret; |
| 1449 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1450 | btrfs_free_path(path); |
| 1451 | return err; |
| 1452 | } |
| 1453 | |
| 1454 | int btrfs_get_block(struct inode *inode, sector_t iblock, |
| 1455 | struct buffer_head *result, int create) |
| 1456 | { |
| 1457 | int err; |
| 1458 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1459 | mutex_lock(&root->fs_info->fs_mutex); |
| 1460 | err = btrfs_get_block_lock(inode, iblock, result, create); |
| 1461 | mutex_unlock(&root->fs_info->fs_mutex); |
| 1462 | return err; |
| 1463 | } |
| 1464 | |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1465 | static int btrfs_get_block_csum(struct inode *inode, sector_t iblock, |
| 1466 | struct buffer_head *result, int create) |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 1467 | { |
| 1468 | int ret; |
| 1469 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1470 | struct page *page = result->b_page; |
| 1471 | u64 offset = (page->index << PAGE_CACHE_SHIFT) + bh_offset(result); |
| 1472 | struct btrfs_csum_item *item; |
| 1473 | struct btrfs_path *path = NULL; |
| 1474 | |
| 1475 | mutex_lock(&root->fs_info->fs_mutex); |
| 1476 | ret = btrfs_get_block_lock(inode, iblock, result, create); |
| 1477 | if (ret) |
| 1478 | goto out; |
| 1479 | |
| 1480 | path = btrfs_alloc_path(); |
| 1481 | item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, offset, 0); |
| 1482 | if (IS_ERR(item)) { |
| 1483 | ret = PTR_ERR(item); |
| 1484 | /* a csum that isn't present is a preallocated region. */ |
| 1485 | if (ret == -ENOENT || ret == -EFBIG) |
| 1486 | ret = 0; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1487 | result->b_private = NULL; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 1488 | goto out; |
| 1489 | } |
| 1490 | memcpy((char *)&result->b_private, &item->csum, BTRFS_CRC32_SIZE); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 1491 | out: |
| 1492 | if (path) |
| 1493 | btrfs_free_path(path); |
| 1494 | mutex_unlock(&root->fs_info->fs_mutex); |
| 1495 | return ret; |
| 1496 | } |
| 1497 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1498 | static int btrfs_get_block_bmap(struct inode *inode, sector_t iblock, |
| 1499 | struct buffer_head *result, int create) |
| 1500 | { |
| 1501 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 1502 | mutex_lock(&root->fs_info->fs_mutex); |
| 1503 | btrfs_get_block_lock(inode, iblock, result, BTRFS_GET_BLOCK_NO_DIRECT); |
| 1504 | mutex_unlock(&root->fs_info->fs_mutex); |
| 1505 | return 0; |
| 1506 | } |
| 1507 | |
| 1508 | static sector_t btrfs_bmap(struct address_space *as, sector_t block) |
| 1509 | { |
| 1510 | return generic_block_bmap(as, block, btrfs_get_block_bmap); |
| 1511 | } |
| 1512 | |
| 1513 | static int btrfs_prepare_write(struct file *file, struct page *page, |
| 1514 | unsigned from, unsigned to) |
| 1515 | { |
| 1516 | return block_prepare_write(page, from, to, btrfs_get_block); |
| 1517 | } |
| 1518 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 1519 | static void buffer_io_error(struct buffer_head *bh) |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1520 | { |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 1521 | char b[BDEVNAME_SIZE]; |
| 1522 | |
| 1523 | printk(KERN_ERR "Buffer I/O error on device %s, logical block %Lu\n", |
| 1524 | bdevname(bh->b_bdev, b), |
| 1525 | (unsigned long long)bh->b_blocknr); |
| 1526 | } |
| 1527 | |
| 1528 | /* |
| 1529 | * I/O completion handler for block_read_full_page() - pages |
| 1530 | * which come unlocked at the end of I/O. |
| 1531 | */ |
| 1532 | static void btrfs_end_buffer_async_read(struct buffer_head *bh, int uptodate) |
| 1533 | { |
| 1534 | unsigned long flags; |
| 1535 | struct buffer_head *first; |
| 1536 | struct buffer_head *tmp; |
| 1537 | struct page *page; |
| 1538 | int page_uptodate = 1; |
| 1539 | struct inode *inode; |
| 1540 | int ret; |
| 1541 | |
| 1542 | BUG_ON(!buffer_async_read(bh)); |
| 1543 | |
| 1544 | page = bh->b_page; |
| 1545 | inode = page->mapping->host; |
| 1546 | if (uptodate) { |
| 1547 | void *kaddr; |
| 1548 | struct btrfs_root *root = BTRFS_I(page->mapping->host)->root; |
| 1549 | if (bh->b_private) { |
| 1550 | char csum[BTRFS_CRC32_SIZE]; |
| 1551 | kaddr = kmap_atomic(page, KM_IRQ0); |
| 1552 | ret = btrfs_csum_data(root, kaddr + bh_offset(bh), |
| 1553 | bh->b_size, csum); |
| 1554 | BUG_ON(ret); |
| 1555 | if (memcmp(csum, &bh->b_private, BTRFS_CRC32_SIZE)) { |
| 1556 | u64 offset; |
| 1557 | offset = (page->index << PAGE_CACHE_SHIFT) + |
| 1558 | bh_offset(bh); |
| 1559 | printk("btrfs csum failed ino %lu off %llu\n", |
| 1560 | page->mapping->host->i_ino, |
| 1561 | (unsigned long long)offset); |
| 1562 | memset(kaddr + bh_offset(bh), 1, bh->b_size); |
| 1563 | flush_dcache_page(page); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 1564 | } |
| 1565 | kunmap_atomic(kaddr, KM_IRQ0); |
| 1566 | } |
| 1567 | set_buffer_uptodate(bh); |
| 1568 | } else { |
| 1569 | clear_buffer_uptodate(bh); |
| 1570 | if (printk_ratelimit()) |
| 1571 | buffer_io_error(bh); |
| 1572 | SetPageError(page); |
| 1573 | } |
| 1574 | |
| 1575 | /* |
| 1576 | * Be _very_ careful from here on. Bad things can happen if |
| 1577 | * two buffer heads end IO at almost the same time and both |
| 1578 | * decide that the page is now completely done. |
| 1579 | */ |
| 1580 | first = page_buffers(page); |
| 1581 | local_irq_save(flags); |
| 1582 | bit_spin_lock(BH_Uptodate_Lock, &first->b_state); |
| 1583 | clear_buffer_async_read(bh); |
| 1584 | unlock_buffer(bh); |
| 1585 | tmp = bh; |
| 1586 | do { |
| 1587 | if (!buffer_uptodate(tmp)) |
| 1588 | page_uptodate = 0; |
| 1589 | if (buffer_async_read(tmp)) { |
| 1590 | BUG_ON(!buffer_locked(tmp)); |
| 1591 | goto still_busy; |
| 1592 | } |
| 1593 | tmp = tmp->b_this_page; |
| 1594 | } while (tmp != bh); |
| 1595 | bit_spin_unlock(BH_Uptodate_Lock, &first->b_state); |
| 1596 | local_irq_restore(flags); |
| 1597 | |
| 1598 | /* |
| 1599 | * If none of the buffers had errors and they are all |
| 1600 | * uptodate then we can set the page uptodate. |
| 1601 | */ |
| 1602 | if (page_uptodate && !PageError(page)) |
| 1603 | SetPageUptodate(page); |
| 1604 | unlock_page(page); |
| 1605 | return; |
| 1606 | |
| 1607 | still_busy: |
| 1608 | bit_spin_unlock(BH_Uptodate_Lock, &first->b_state); |
| 1609 | local_irq_restore(flags); |
| 1610 | return; |
| 1611 | } |
| 1612 | |
| 1613 | /* |
| 1614 | * Generic "read page" function for block devices that have the normal |
| 1615 | * get_block functionality. This is most of the block device filesystems. |
| 1616 | * Reads the page asynchronously --- the unlock_buffer() and |
| 1617 | * set/clear_buffer_uptodate() functions propagate buffer state into the |
| 1618 | * page struct once IO has completed. |
| 1619 | */ |
| 1620 | int btrfs_readpage(struct file *file, struct page *page) |
| 1621 | { |
| 1622 | struct inode *inode = page->mapping->host; |
| 1623 | sector_t iblock, lblock; |
| 1624 | struct buffer_head *bh, *head, *arr[MAX_BUF_PER_PAGE]; |
| 1625 | unsigned int blocksize; |
| 1626 | int nr, i; |
| 1627 | int fully_mapped = 1; |
| 1628 | |
| 1629 | BUG_ON(!PageLocked(page)); |
| 1630 | blocksize = 1 << inode->i_blkbits; |
| 1631 | if (!page_has_buffers(page)) |
| 1632 | create_empty_buffers(page, blocksize, 0); |
| 1633 | head = page_buffers(page); |
| 1634 | |
| 1635 | iblock = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits); |
| 1636 | lblock = (i_size_read(inode)+blocksize-1) >> inode->i_blkbits; |
| 1637 | bh = head; |
| 1638 | nr = 0; |
| 1639 | i = 0; |
| 1640 | |
| 1641 | do { |
| 1642 | if (buffer_uptodate(bh)) |
| 1643 | continue; |
| 1644 | |
| 1645 | if (!buffer_mapped(bh)) { |
| 1646 | int err = 0; |
| 1647 | |
| 1648 | fully_mapped = 0; |
| 1649 | if (iblock < lblock) { |
| 1650 | WARN_ON(bh->b_size != blocksize); |
| 1651 | err = btrfs_get_block_csum(inode, iblock, |
| 1652 | bh, 0); |
| 1653 | if (err) |
| 1654 | SetPageError(page); |
| 1655 | } |
| 1656 | if (!buffer_mapped(bh)) { |
| 1657 | void *kaddr = kmap_atomic(page, KM_USER0); |
| 1658 | memset(kaddr + i * blocksize, 0, blocksize); |
| 1659 | flush_dcache_page(page); |
| 1660 | kunmap_atomic(kaddr, KM_USER0); |
| 1661 | if (!err) |
| 1662 | set_buffer_uptodate(bh); |
| 1663 | continue; |
| 1664 | } |
| 1665 | /* |
| 1666 | * get_block() might have updated the buffer |
| 1667 | * synchronously |
| 1668 | */ |
| 1669 | if (buffer_uptodate(bh)) |
| 1670 | continue; |
| 1671 | } |
| 1672 | arr[nr++] = bh; |
| 1673 | } while (i++, iblock++, (bh = bh->b_this_page) != head); |
| 1674 | |
| 1675 | if (fully_mapped) |
| 1676 | SetPageMappedToDisk(page); |
| 1677 | |
| 1678 | if (!nr) { |
| 1679 | /* |
| 1680 | * All buffers are uptodate - we can set the page uptodate |
| 1681 | * as well. But not if get_block() returned an error. |
| 1682 | */ |
| 1683 | if (!PageError(page)) |
| 1684 | SetPageUptodate(page); |
| 1685 | unlock_page(page); |
| 1686 | return 0; |
| 1687 | } |
| 1688 | |
| 1689 | /* Stage two: lock the buffers */ |
| 1690 | for (i = 0; i < nr; i++) { |
| 1691 | bh = arr[i]; |
| 1692 | lock_buffer(bh); |
| 1693 | bh->b_end_io = btrfs_end_buffer_async_read; |
| 1694 | set_buffer_async_read(bh); |
| 1695 | } |
| 1696 | |
| 1697 | /* |
| 1698 | * Stage 3: start the IO. Check for uptodateness |
| 1699 | * inside the buffer lock in case another process reading |
| 1700 | * the underlying blockdev brought it uptodate (the sct fix). |
| 1701 | */ |
| 1702 | for (i = 0; i < nr; i++) { |
| 1703 | bh = arr[i]; |
| 1704 | if (buffer_uptodate(bh)) |
| 1705 | btrfs_end_buffer_async_read(bh, 1); |
| 1706 | else |
| 1707 | submit_bh(READ, bh); |
| 1708 | } |
| 1709 | return 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1710 | } |
| 1711 | |
| 1712 | /* |
| 1713 | * Aside from a tiny bit of packed file data handling, this is the |
| 1714 | * same as the generic code. |
| 1715 | * |
| 1716 | * While block_write_full_page is writing back the dirty buffers under |
| 1717 | * the page lock, whoever dirtied the buffers may decide to clean them |
| 1718 | * again at any time. We handle that by only looking at the buffer |
| 1719 | * state inside lock_buffer(). |
| 1720 | * |
| 1721 | * If block_write_full_page() is called for regular writeback |
| 1722 | * (wbc->sync_mode == WB_SYNC_NONE) then it will redirty a page which has a |
| 1723 | * locked buffer. This only can happen if someone has written the buffer |
| 1724 | * directly, with submit_bh(). At the address_space level PageWriteback |
| 1725 | * prevents this contention from occurring. |
| 1726 | */ |
| 1727 | static int __btrfs_write_full_page(struct inode *inode, struct page *page, |
| 1728 | struct writeback_control *wbc) |
| 1729 | { |
| 1730 | int err; |
| 1731 | sector_t block; |
| 1732 | sector_t last_block; |
| 1733 | struct buffer_head *bh, *head; |
| 1734 | const unsigned blocksize = 1 << inode->i_blkbits; |
| 1735 | int nr_underway = 0; |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 1736 | struct btrfs_root *root = BTRFS_I(inode)->root; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1737 | |
| 1738 | BUG_ON(!PageLocked(page)); |
| 1739 | |
| 1740 | last_block = (i_size_read(inode) - 1) >> inode->i_blkbits; |
| 1741 | |
Chris Mason | 8c2383c | 2007-06-18 09:57:58 -0400 | [diff] [blame] | 1742 | /* no csumming allowed when from PF_MEMALLOC */ |
| 1743 | if (current->flags & PF_MEMALLOC) { |
| 1744 | redirty_page_for_writepage(wbc, page); |
| 1745 | unlock_page(page); |
| 1746 | return 0; |
| 1747 | } |
| 1748 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1749 | if (!page_has_buffers(page)) { |
| 1750 | create_empty_buffers(page, blocksize, |
| 1751 | (1 << BH_Dirty)|(1 << BH_Uptodate)); |
| 1752 | } |
| 1753 | |
| 1754 | /* |
| 1755 | * Be very careful. We have no exclusion from __set_page_dirty_buffers |
| 1756 | * here, and the (potentially unmapped) buffers may become dirty at |
| 1757 | * any time. If a buffer becomes dirty here after we've inspected it |
| 1758 | * then we just miss that fact, and the page stays dirty. |
| 1759 | * |
| 1760 | * Buffers outside i_size may be dirtied by __set_page_dirty_buffers; |
| 1761 | * handle that here by just cleaning them. |
| 1762 | */ |
| 1763 | |
| 1764 | block = (sector_t)page->index << (PAGE_CACHE_SHIFT - inode->i_blkbits); |
| 1765 | head = page_buffers(page); |
| 1766 | bh = head; |
| 1767 | |
| 1768 | /* |
| 1769 | * Get all the dirty buffers mapped to disk addresses and |
| 1770 | * handle any aliases from the underlying blockdev's mapping. |
| 1771 | */ |
| 1772 | do { |
| 1773 | if (block > last_block) { |
| 1774 | /* |
| 1775 | * mapped buffers outside i_size will occur, because |
| 1776 | * this page can be outside i_size when there is a |
| 1777 | * truncate in progress. |
| 1778 | */ |
| 1779 | /* |
| 1780 | * The buffer was zeroed by block_write_full_page() |
| 1781 | */ |
| 1782 | clear_buffer_dirty(bh); |
| 1783 | set_buffer_uptodate(bh); |
| 1784 | } else if (!buffer_mapped(bh) && buffer_dirty(bh)) { |
| 1785 | WARN_ON(bh->b_size != blocksize); |
| 1786 | err = btrfs_get_block(inode, block, bh, 0); |
| 1787 | if (err) { |
| 1788 | goto recover; |
| 1789 | } |
| 1790 | if (buffer_new(bh)) { |
| 1791 | /* blockdev mappings never come here */ |
| 1792 | clear_buffer_new(bh); |
| 1793 | } |
| 1794 | } |
| 1795 | bh = bh->b_this_page; |
| 1796 | block++; |
| 1797 | } while (bh != head); |
| 1798 | |
| 1799 | do { |
| 1800 | if (!buffer_mapped(bh)) |
| 1801 | continue; |
| 1802 | /* |
| 1803 | * If it's a fully non-blocking write attempt and we cannot |
| 1804 | * lock the buffer then redirty the page. Note that this can |
| 1805 | * potentially cause a busy-wait loop from pdflush and kswapd |
| 1806 | * activity, but those code paths have their own higher-level |
| 1807 | * throttling. |
| 1808 | */ |
| 1809 | if (wbc->sync_mode != WB_SYNC_NONE || !wbc->nonblocking) { |
| 1810 | lock_buffer(bh); |
| 1811 | } else if (test_set_buffer_locked(bh)) { |
| 1812 | redirty_page_for_writepage(wbc, page); |
| 1813 | continue; |
| 1814 | } |
| 1815 | if (test_clear_buffer_dirty(bh) && bh->b_blocknr != 0) { |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 1816 | struct btrfs_trans_handle *trans; |
| 1817 | int ret; |
| 1818 | u64 off = page->index << PAGE_CACHE_SHIFT; |
| 1819 | char *kaddr; |
| 1820 | |
| 1821 | off += bh_offset(bh); |
| 1822 | mutex_lock(&root->fs_info->fs_mutex); |
| 1823 | trans = btrfs_start_transaction(root, 1); |
| 1824 | btrfs_set_trans_block_group(trans, inode); |
| 1825 | kaddr = kmap(page); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 1826 | btrfs_csum_file_block(trans, root, inode->i_ino, |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 1827 | off, kaddr + bh_offset(bh), |
| 1828 | bh->b_size); |
| 1829 | kunmap(page); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 1830 | ret = btrfs_end_transaction(trans, root); |
| 1831 | BUG_ON(ret); |
| 1832 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 1833 | mark_buffer_async_write(bh); |
| 1834 | } else { |
| 1835 | unlock_buffer(bh); |
| 1836 | } |
| 1837 | } while ((bh = bh->b_this_page) != head); |
| 1838 | |
| 1839 | /* |
| 1840 | * The page and its buffers are protected by PageWriteback(), so we can |
| 1841 | * drop the bh refcounts early. |
| 1842 | */ |
| 1843 | BUG_ON(PageWriteback(page)); |
| 1844 | set_page_writeback(page); |
| 1845 | |
| 1846 | do { |
| 1847 | struct buffer_head *next = bh->b_this_page; |
| 1848 | if (buffer_async_write(bh)) { |
| 1849 | submit_bh(WRITE, bh); |
| 1850 | nr_underway++; |
| 1851 | } |
| 1852 | bh = next; |
| 1853 | } while (bh != head); |
| 1854 | unlock_page(page); |
| 1855 | |
| 1856 | err = 0; |
| 1857 | done: |
| 1858 | if (nr_underway == 0) { |
| 1859 | /* |
| 1860 | * The page was marked dirty, but the buffers were |
| 1861 | * clean. Someone wrote them back by hand with |
| 1862 | * ll_rw_block/submit_bh. A rare case. |
| 1863 | */ |
| 1864 | int uptodate = 1; |
| 1865 | do { |
| 1866 | if (!buffer_uptodate(bh)) { |
| 1867 | uptodate = 0; |
| 1868 | break; |
| 1869 | } |
| 1870 | bh = bh->b_this_page; |
| 1871 | } while (bh != head); |
| 1872 | if (uptodate) |
| 1873 | SetPageUptodate(page); |
| 1874 | end_page_writeback(page); |
| 1875 | } |
| 1876 | return err; |
| 1877 | |
| 1878 | recover: |
| 1879 | /* |
| 1880 | * ENOSPC, or some other error. We may already have added some |
| 1881 | * blocks to the file, so we need to write these out to avoid |
| 1882 | * exposing stale data. |
| 1883 | * The page is currently locked and not marked for writeback |
| 1884 | */ |
| 1885 | bh = head; |
| 1886 | /* Recovery: lock and submit the mapped buffers */ |
| 1887 | do { |
| 1888 | if (buffer_mapped(bh) && buffer_dirty(bh)) { |
| 1889 | lock_buffer(bh); |
| 1890 | mark_buffer_async_write(bh); |
| 1891 | } else { |
| 1892 | /* |
| 1893 | * The buffer may have been set dirty during |
| 1894 | * attachment to a dirty page. |
| 1895 | */ |
| 1896 | clear_buffer_dirty(bh); |
| 1897 | } |
| 1898 | } while ((bh = bh->b_this_page) != head); |
| 1899 | SetPageError(page); |
| 1900 | BUG_ON(PageWriteback(page)); |
| 1901 | set_page_writeback(page); |
| 1902 | do { |
| 1903 | struct buffer_head *next = bh->b_this_page; |
| 1904 | if (buffer_async_write(bh)) { |
| 1905 | clear_buffer_dirty(bh); |
| 1906 | submit_bh(WRITE, bh); |
| 1907 | nr_underway++; |
| 1908 | } |
| 1909 | bh = next; |
| 1910 | } while (bh != head); |
| 1911 | unlock_page(page); |
| 1912 | goto done; |
| 1913 | } |
| 1914 | |
| 1915 | static int btrfs_writepage(struct page *page, struct writeback_control *wbc) |
| 1916 | { |
| 1917 | struct inode * const inode = page->mapping->host; |
| 1918 | loff_t i_size = i_size_read(inode); |
| 1919 | const pgoff_t end_index = i_size >> PAGE_CACHE_SHIFT; |
| 1920 | unsigned offset; |
| 1921 | void *kaddr; |
| 1922 | |
| 1923 | /* Is the page fully inside i_size? */ |
| 1924 | if (page->index < end_index) |
| 1925 | return __btrfs_write_full_page(inode, page, wbc); |
| 1926 | |
| 1927 | /* Is the page fully outside i_size? (truncate in progress) */ |
| 1928 | offset = i_size & (PAGE_CACHE_SIZE-1); |
| 1929 | if (page->index >= end_index+1 || !offset) { |
| 1930 | /* |
| 1931 | * The page may have dirty, unmapped buffers. For example, |
| 1932 | * they may have been added in ext3_writepage(). Make them |
| 1933 | * freeable here, so the page does not leak. |
| 1934 | */ |
| 1935 | block_invalidatepage(page, 0); |
| 1936 | unlock_page(page); |
| 1937 | return 0; /* don't care */ |
| 1938 | } |
| 1939 | |
| 1940 | /* |
| 1941 | * The page straddles i_size. It must be zeroed out on each and every |
| 1942 | * writepage invokation because it may be mmapped. "A file is mapped |
| 1943 | * in multiples of the page size. For a file that is not a multiple of |
| 1944 | * the page size, the remaining memory is zeroed when mapped, and |
| 1945 | * writes to that region are not written out to the file." |
| 1946 | */ |
| 1947 | kaddr = kmap_atomic(page, KM_USER0); |
| 1948 | memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset); |
| 1949 | flush_dcache_page(page); |
| 1950 | kunmap_atomic(kaddr, KM_USER0); |
| 1951 | return __btrfs_write_full_page(inode, page, wbc); |
| 1952 | } |
| 1953 | |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 1954 | /* |
| 1955 | * btrfs_page_mkwrite() is not allowed to change the file size as it gets |
| 1956 | * called from a page fault handler when a page is first dirtied. Hence we must |
| 1957 | * be careful to check for EOF conditions here. We set the page up correctly |
| 1958 | * for a written page which means we get ENOSPC checking when writing into |
| 1959 | * holes and correct delalloc and unwritten extent mapping on filesystems that |
| 1960 | * support these features. |
| 1961 | * |
| 1962 | * We are not allowed to take the i_mutex here so we have to play games to |
| 1963 | * protect against truncate races as the page could now be beyond EOF. Because |
| 1964 | * vmtruncate() writes the inode size before removing pages, once we have the |
| 1965 | * page lock we can determine safely if the page is beyond EOF. If it is not |
| 1966 | * beyond EOF, then the page is guaranteed safe against truncation until we |
| 1967 | * unlock the page. |
| 1968 | */ |
| 1969 | int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page) |
| 1970 | { |
| 1971 | struct inode *inode = vma->vm_file->f_path.dentry->d_inode; |
| 1972 | unsigned long end; |
| 1973 | loff_t size; |
| 1974 | int ret = -EINVAL; |
| 1975 | |
| 1976 | lock_page(page); |
| 1977 | wait_on_page_writeback(page); |
Chris Mason | 9ebefb18 | 2007-06-15 13:50:00 -0400 | [diff] [blame] | 1978 | size = i_size_read(inode); |
| 1979 | if ((page->mapping != inode->i_mapping) || |
| 1980 | ((page->index << PAGE_CACHE_SHIFT) > size)) { |
| 1981 | /* page got truncated out from underneath us */ |
| 1982 | goto out_unlock; |
| 1983 | } |
| 1984 | |
| 1985 | /* page is wholly or partially inside EOF */ |
| 1986 | if (((page->index + 1) << PAGE_CACHE_SHIFT) > size) |
| 1987 | end = size & ~PAGE_CACHE_MASK; |
| 1988 | else |
| 1989 | end = PAGE_CACHE_SIZE; |
| 1990 | |
| 1991 | ret = btrfs_prepare_write(NULL, page, 0, end); |
| 1992 | if (!ret) |
| 1993 | ret = btrfs_commit_write(NULL, page, 0, end); |
| 1994 | |
| 1995 | out_unlock: |
| 1996 | unlock_page(page); |
| 1997 | return ret; |
| 1998 | } |
| 1999 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2000 | static void btrfs_truncate(struct inode *inode) |
| 2001 | { |
| 2002 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2003 | int ret; |
| 2004 | struct btrfs_trans_handle *trans; |
| 2005 | |
| 2006 | if (!S_ISREG(inode->i_mode)) |
| 2007 | return; |
| 2008 | if (IS_APPEND(inode) || IS_IMMUTABLE(inode)) |
| 2009 | return; |
| 2010 | |
| 2011 | btrfs_truncate_page(inode->i_mapping, inode->i_size); |
| 2012 | |
| 2013 | mutex_lock(&root->fs_info->fs_mutex); |
| 2014 | trans = btrfs_start_transaction(root, 1); |
| 2015 | btrfs_set_trans_block_group(trans, inode); |
| 2016 | |
| 2017 | /* FIXME, add redo link to tree so we don't leak on crash */ |
| 2018 | ret = btrfs_truncate_in_trans(trans, root, inode); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2019 | btrfs_update_inode(trans, root, inode); |
| 2020 | ret = btrfs_end_transaction(trans, root); |
| 2021 | BUG_ON(ret); |
| 2022 | mutex_unlock(&root->fs_info->fs_mutex); |
| 2023 | btrfs_btree_balance_dirty(root); |
| 2024 | } |
| 2025 | |
| 2026 | int btrfs_commit_write(struct file *file, struct page *page, |
| 2027 | unsigned from, unsigned to) |
| 2028 | { |
| 2029 | struct inode *inode = page->mapping->host; |
| 2030 | struct buffer_head *bh; |
| 2031 | loff_t pos = ((loff_t)page->index << PAGE_CACHE_SHIFT) + to; |
| 2032 | |
| 2033 | SetPageUptodate(page); |
| 2034 | bh = page_buffers(page); |
| 2035 | set_buffer_uptodate(bh); |
| 2036 | if (buffer_mapped(bh) && bh->b_blocknr != 0) { |
| 2037 | set_page_dirty(page); |
| 2038 | } |
| 2039 | if (pos > inode->i_size) { |
| 2040 | i_size_write(inode, pos); |
| 2041 | mark_inode_dirty(inode); |
| 2042 | } |
| 2043 | return 0; |
| 2044 | } |
| 2045 | |
| 2046 | static int create_subvol(struct btrfs_root *root, char *name, int namelen) |
| 2047 | { |
| 2048 | struct btrfs_trans_handle *trans; |
| 2049 | struct btrfs_key key; |
| 2050 | struct btrfs_root_item root_item; |
| 2051 | struct btrfs_inode_item *inode_item; |
| 2052 | struct buffer_head *subvol; |
| 2053 | struct btrfs_leaf *leaf; |
| 2054 | struct btrfs_root *new_root; |
| 2055 | struct inode *inode; |
| 2056 | struct inode *dir; |
| 2057 | int ret; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2058 | int err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2059 | u64 objectid; |
| 2060 | u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID; |
| 2061 | |
| 2062 | mutex_lock(&root->fs_info->fs_mutex); |
| 2063 | trans = btrfs_start_transaction(root, 1); |
| 2064 | BUG_ON(!trans); |
| 2065 | |
| 2066 | subvol = btrfs_alloc_free_block(trans, root, 0); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2067 | if (IS_ERR(subvol)) |
| 2068 | return PTR_ERR(subvol); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2069 | leaf = btrfs_buffer_leaf(subvol); |
| 2070 | btrfs_set_header_nritems(&leaf->header, 0); |
| 2071 | btrfs_set_header_level(&leaf->header, 0); |
| 2072 | btrfs_set_header_blocknr(&leaf->header, bh_blocknr(subvol)); |
| 2073 | btrfs_set_header_generation(&leaf->header, trans->transid); |
| 2074 | btrfs_set_header_owner(&leaf->header, root->root_key.objectid); |
| 2075 | memcpy(leaf->header.fsid, root->fs_info->disk_super->fsid, |
| 2076 | sizeof(leaf->header.fsid)); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 2077 | btrfs_mark_buffer_dirty(subvol); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2078 | |
| 2079 | inode_item = &root_item.inode; |
| 2080 | memset(inode_item, 0, sizeof(*inode_item)); |
| 2081 | btrfs_set_inode_generation(inode_item, 1); |
| 2082 | btrfs_set_inode_size(inode_item, 3); |
| 2083 | btrfs_set_inode_nlink(inode_item, 1); |
| 2084 | btrfs_set_inode_nblocks(inode_item, 1); |
| 2085 | btrfs_set_inode_mode(inode_item, S_IFDIR | 0755); |
| 2086 | |
| 2087 | btrfs_set_root_blocknr(&root_item, bh_blocknr(subvol)); |
| 2088 | btrfs_set_root_refs(&root_item, 1); |
Chris Mason | 5eda7b5 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2089 | memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress)); |
| 2090 | root_item.drop_level = 0; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2091 | brelse(subvol); |
| 2092 | subvol = NULL; |
| 2093 | |
| 2094 | ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root, |
| 2095 | 0, &objectid); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2096 | if (ret) |
| 2097 | goto fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2098 | |
| 2099 | btrfs_set_root_dirid(&root_item, new_dirid); |
| 2100 | |
| 2101 | key.objectid = objectid; |
| 2102 | key.offset = 1; |
| 2103 | key.flags = 0; |
| 2104 | btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); |
| 2105 | ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, |
| 2106 | &root_item); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2107 | if (ret) |
| 2108 | goto fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2109 | |
| 2110 | /* |
| 2111 | * insert the directory item |
| 2112 | */ |
| 2113 | key.offset = (u64)-1; |
| 2114 | dir = root->fs_info->sb->s_root->d_inode; |
| 2115 | ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root, |
| 2116 | name, namelen, dir->i_ino, &key, |
| 2117 | BTRFS_FT_DIR); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2118 | if (ret) |
| 2119 | goto fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2120 | |
| 2121 | ret = btrfs_commit_transaction(trans, root); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2122 | if (ret) |
| 2123 | goto fail_commit; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2124 | |
| 2125 | new_root = btrfs_read_fs_root(root->fs_info, &key); |
| 2126 | BUG_ON(!new_root); |
| 2127 | |
| 2128 | trans = btrfs_start_transaction(new_root, 1); |
| 2129 | BUG_ON(!trans); |
| 2130 | |
| 2131 | inode = btrfs_new_inode(trans, new_root, new_dirid, |
| 2132 | BTRFS_I(dir)->block_group, S_IFDIR | 0700); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2133 | if (IS_ERR(inode)) |
| 2134 | goto fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2135 | inode->i_op = &btrfs_dir_inode_operations; |
| 2136 | inode->i_fop = &btrfs_dir_file_operations; |
Chris Mason | 3408878 | 2007-06-12 11:36:58 -0400 | [diff] [blame] | 2137 | new_root->inode = inode; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2138 | |
| 2139 | ret = btrfs_make_empty_dir(trans, new_root, new_dirid, new_dirid); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2140 | if (ret) |
| 2141 | goto fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2142 | |
| 2143 | inode->i_nlink = 1; |
| 2144 | inode->i_size = 6; |
| 2145 | ret = btrfs_update_inode(trans, new_root, inode); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2146 | if (ret) |
| 2147 | goto fail; |
| 2148 | fail: |
| 2149 | err = btrfs_commit_transaction(trans, root); |
| 2150 | if (err && !ret) |
| 2151 | ret = err; |
| 2152 | fail_commit: |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2153 | mutex_unlock(&root->fs_info->fs_mutex); |
| 2154 | btrfs_btree_balance_dirty(root); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2155 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2156 | } |
| 2157 | |
| 2158 | static int create_snapshot(struct btrfs_root *root, char *name, int namelen) |
| 2159 | { |
| 2160 | struct btrfs_trans_handle *trans; |
| 2161 | struct btrfs_key key; |
| 2162 | struct btrfs_root_item new_root_item; |
| 2163 | int ret; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2164 | int err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2165 | u64 objectid; |
| 2166 | |
| 2167 | if (!root->ref_cows) |
| 2168 | return -EINVAL; |
| 2169 | |
| 2170 | mutex_lock(&root->fs_info->fs_mutex); |
| 2171 | trans = btrfs_start_transaction(root, 1); |
| 2172 | BUG_ON(!trans); |
| 2173 | |
| 2174 | ret = btrfs_update_inode(trans, root, root->inode); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2175 | if (ret) |
| 2176 | goto fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2177 | |
| 2178 | ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root, |
| 2179 | 0, &objectid); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2180 | if (ret) |
| 2181 | goto fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2182 | |
| 2183 | memcpy(&new_root_item, &root->root_item, |
| 2184 | sizeof(new_root_item)); |
| 2185 | |
| 2186 | key.objectid = objectid; |
| 2187 | key.offset = 1; |
| 2188 | key.flags = 0; |
| 2189 | btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY); |
| 2190 | btrfs_set_root_blocknr(&new_root_item, bh_blocknr(root->node)); |
| 2191 | |
| 2192 | ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key, |
| 2193 | &new_root_item); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2194 | if (ret) |
| 2195 | goto fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2196 | |
| 2197 | /* |
| 2198 | * insert the directory item |
| 2199 | */ |
| 2200 | key.offset = (u64)-1; |
| 2201 | ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root, |
| 2202 | name, namelen, |
| 2203 | root->fs_info->sb->s_root->d_inode->i_ino, |
| 2204 | &key, BTRFS_FT_DIR); |
| 2205 | |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2206 | if (ret) |
| 2207 | goto fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2208 | |
| 2209 | ret = btrfs_inc_root_ref(trans, root); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2210 | if (ret) |
| 2211 | goto fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2212 | |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2213 | fail: |
| 2214 | err = btrfs_commit_transaction(trans, root); |
| 2215 | if (err && !ret) |
| 2216 | ret = err; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2217 | mutex_unlock(&root->fs_info->fs_mutex); |
| 2218 | btrfs_btree_balance_dirty(root); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2219 | return ret; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2220 | } |
| 2221 | |
| 2222 | int btrfs_ioctl(struct inode *inode, struct file *filp, unsigned int |
| 2223 | cmd, unsigned long arg) |
| 2224 | { |
| 2225 | struct btrfs_root *root = BTRFS_I(inode)->root; |
| 2226 | struct btrfs_ioctl_vol_args vol_args; |
| 2227 | int ret = 0; |
| 2228 | struct btrfs_dir_item *di; |
| 2229 | int namelen; |
| 2230 | struct btrfs_path *path; |
| 2231 | u64 root_dirid; |
| 2232 | |
| 2233 | switch (cmd) { |
| 2234 | case BTRFS_IOC_SNAP_CREATE: |
| 2235 | if (copy_from_user(&vol_args, |
| 2236 | (struct btrfs_ioctl_vol_args __user *)arg, |
| 2237 | sizeof(vol_args))) |
| 2238 | return -EFAULT; |
| 2239 | namelen = strlen(vol_args.name); |
| 2240 | if (namelen > BTRFS_VOL_NAME_MAX) |
| 2241 | return -EINVAL; |
Chris Mason | 8a71264 | 2007-06-12 08:21:35 -0400 | [diff] [blame] | 2242 | if (strchr(vol_args.name, '/')) |
| 2243 | return -EINVAL; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2244 | path = btrfs_alloc_path(); |
| 2245 | if (!path) |
| 2246 | return -ENOMEM; |
| 2247 | root_dirid = root->fs_info->sb->s_root->d_inode->i_ino, |
| 2248 | mutex_lock(&root->fs_info->fs_mutex); |
| 2249 | di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root, |
| 2250 | path, root_dirid, |
| 2251 | vol_args.name, namelen, 0); |
| 2252 | mutex_unlock(&root->fs_info->fs_mutex); |
| 2253 | btrfs_free_path(path); |
| 2254 | if (di && !IS_ERR(di)) |
| 2255 | return -EEXIST; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2256 | if (IS_ERR(di)) |
| 2257 | return PTR_ERR(di); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2258 | |
| 2259 | if (root == root->fs_info->tree_root) |
| 2260 | ret = create_subvol(root, vol_args.name, namelen); |
| 2261 | else |
| 2262 | ret = create_snapshot(root, vol_args.name, namelen); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2263 | break; |
| 2264 | default: |
| 2265 | return -ENOTTY; |
| 2266 | } |
| 2267 | return ret; |
| 2268 | } |
| 2269 | |
| 2270 | #ifdef CONFIG_COMPAT |
| 2271 | long btrfs_compat_ioctl(struct file *file, unsigned int cmd, |
| 2272 | unsigned long arg) |
| 2273 | { |
| 2274 | struct inode *inode = file->f_path.dentry->d_inode; |
| 2275 | int ret; |
| 2276 | lock_kernel(); |
| 2277 | ret = btrfs_ioctl(inode, file, cmd, (unsigned long) compat_ptr(arg)); |
| 2278 | unlock_kernel(); |
| 2279 | return ret; |
| 2280 | |
| 2281 | } |
| 2282 | #endif |
| 2283 | |
| 2284 | /* |
| 2285 | * Called inside transaction, so use GFP_NOFS |
| 2286 | */ |
| 2287 | struct inode *btrfs_alloc_inode(struct super_block *sb) |
| 2288 | { |
| 2289 | struct btrfs_inode *ei; |
| 2290 | |
| 2291 | ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS); |
| 2292 | if (!ei) |
| 2293 | return NULL; |
| 2294 | return &ei->vfs_inode; |
| 2295 | } |
| 2296 | |
| 2297 | void btrfs_destroy_inode(struct inode *inode) |
| 2298 | { |
| 2299 | WARN_ON(!list_empty(&inode->i_dentry)); |
| 2300 | WARN_ON(inode->i_data.nrpages); |
| 2301 | |
| 2302 | kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode)); |
| 2303 | } |
| 2304 | |
| 2305 | static void init_once(void * foo, struct kmem_cache * cachep, |
| 2306 | unsigned long flags) |
| 2307 | { |
| 2308 | struct btrfs_inode *ei = (struct btrfs_inode *) foo; |
| 2309 | |
| 2310 | inode_init_once(&ei->vfs_inode); |
| 2311 | } |
| 2312 | |
| 2313 | void btrfs_destroy_cachep(void) |
| 2314 | { |
| 2315 | if (btrfs_inode_cachep) |
| 2316 | kmem_cache_destroy(btrfs_inode_cachep); |
| 2317 | if (btrfs_trans_handle_cachep) |
| 2318 | kmem_cache_destroy(btrfs_trans_handle_cachep); |
| 2319 | if (btrfs_transaction_cachep) |
| 2320 | kmem_cache_destroy(btrfs_transaction_cachep); |
| 2321 | if (btrfs_bit_radix_cachep) |
| 2322 | kmem_cache_destroy(btrfs_bit_radix_cachep); |
| 2323 | if (btrfs_path_cachep) |
| 2324 | kmem_cache_destroy(btrfs_path_cachep); |
| 2325 | } |
| 2326 | |
Chris Mason | 92fee66 | 2007-07-25 12:31:35 -0400 | [diff] [blame^] | 2327 | static struct kmem_cache *cache_create(const char *name, size_t size, |
| 2328 | unsigned long extra_flags, |
| 2329 | void (*ctor)(void *, struct kmem_cache *, |
| 2330 | unsigned long)) |
| 2331 | { |
| 2332 | return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT | |
| 2333 | SLAB_MEM_SPREAD | extra_flags), ctor |
| 2334 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23) |
| 2335 | ,NULL |
| 2336 | #endif |
| 2337 | ); |
| 2338 | } |
| 2339 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2340 | int btrfs_init_cachep(void) |
| 2341 | { |
Chris Mason | 92fee66 | 2007-07-25 12:31:35 -0400 | [diff] [blame^] | 2342 | btrfs_inode_cachep = cache_create("btrfs_inode_cache", |
| 2343 | sizeof(struct btrfs_inode), |
| 2344 | 0, init_once); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2345 | if (!btrfs_inode_cachep) |
| 2346 | goto fail; |
Chris Mason | 92fee66 | 2007-07-25 12:31:35 -0400 | [diff] [blame^] | 2347 | btrfs_trans_handle_cachep = cache_create("btrfs_trans_handle_cache", |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2348 | sizeof(struct btrfs_trans_handle), |
Chris Mason | 92fee66 | 2007-07-25 12:31:35 -0400 | [diff] [blame^] | 2349 | 0, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2350 | if (!btrfs_trans_handle_cachep) |
| 2351 | goto fail; |
Chris Mason | 92fee66 | 2007-07-25 12:31:35 -0400 | [diff] [blame^] | 2352 | btrfs_transaction_cachep = cache_create("btrfs_transaction_cache", |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2353 | sizeof(struct btrfs_transaction), |
Chris Mason | 92fee66 | 2007-07-25 12:31:35 -0400 | [diff] [blame^] | 2354 | 0, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2355 | if (!btrfs_transaction_cachep) |
| 2356 | goto fail; |
Chris Mason | 92fee66 | 2007-07-25 12:31:35 -0400 | [diff] [blame^] | 2357 | btrfs_path_cachep = cache_create("btrfs_path_cache", |
| 2358 | sizeof(struct btrfs_transaction), |
| 2359 | 0, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2360 | if (!btrfs_path_cachep) |
| 2361 | goto fail; |
Chris Mason | 92fee66 | 2007-07-25 12:31:35 -0400 | [diff] [blame^] | 2362 | btrfs_bit_radix_cachep = cache_create("btrfs_radix", 256, |
| 2363 | SLAB_DESTROY_BY_RCU, NULL); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2364 | if (!btrfs_bit_radix_cachep) |
| 2365 | goto fail; |
| 2366 | return 0; |
| 2367 | fail: |
| 2368 | btrfs_destroy_cachep(); |
| 2369 | return -ENOMEM; |
| 2370 | } |
| 2371 | |
| 2372 | static int btrfs_getattr(struct vfsmount *mnt, |
| 2373 | struct dentry *dentry, struct kstat *stat) |
| 2374 | { |
| 2375 | struct inode *inode = dentry->d_inode; |
| 2376 | generic_fillattr(inode, stat); |
| 2377 | stat->blksize = 256 * 1024; |
| 2378 | return 0; |
| 2379 | } |
| 2380 | |
| 2381 | static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry, |
| 2382 | struct inode * new_dir,struct dentry *new_dentry) |
| 2383 | { |
| 2384 | struct btrfs_trans_handle *trans; |
| 2385 | struct btrfs_root *root = BTRFS_I(old_dir)->root; |
| 2386 | struct inode *new_inode = new_dentry->d_inode; |
| 2387 | struct inode *old_inode = old_dentry->d_inode; |
| 2388 | struct timespec ctime = CURRENT_TIME; |
| 2389 | struct btrfs_path *path; |
| 2390 | struct btrfs_dir_item *di; |
| 2391 | int ret; |
| 2392 | |
| 2393 | if (S_ISDIR(old_inode->i_mode) && new_inode && |
| 2394 | new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) { |
| 2395 | return -ENOTEMPTY; |
| 2396 | } |
| 2397 | mutex_lock(&root->fs_info->fs_mutex); |
| 2398 | trans = btrfs_start_transaction(root, 1); |
| 2399 | btrfs_set_trans_block_group(trans, new_dir); |
| 2400 | path = btrfs_alloc_path(); |
| 2401 | if (!path) { |
| 2402 | ret = -ENOMEM; |
| 2403 | goto out_fail; |
| 2404 | } |
| 2405 | |
| 2406 | old_dentry->d_inode->i_nlink++; |
| 2407 | old_dir->i_ctime = old_dir->i_mtime = ctime; |
| 2408 | new_dir->i_ctime = new_dir->i_mtime = ctime; |
| 2409 | old_inode->i_ctime = ctime; |
| 2410 | if (S_ISDIR(old_inode->i_mode) && old_dir != new_dir) { |
| 2411 | struct btrfs_key *location = &BTRFS_I(new_dir)->location; |
| 2412 | u64 old_parent_oid; |
| 2413 | di = btrfs_lookup_dir_item(trans, root, path, old_inode->i_ino, |
| 2414 | "..", 2, -1); |
| 2415 | if (IS_ERR(di)) { |
| 2416 | ret = PTR_ERR(di); |
| 2417 | goto out_fail; |
| 2418 | } |
| 2419 | if (!di) { |
| 2420 | ret = -ENOENT; |
| 2421 | goto out_fail; |
| 2422 | } |
| 2423 | old_parent_oid = btrfs_disk_key_objectid(&di->location); |
| 2424 | ret = btrfs_del_item(trans, root, path); |
| 2425 | if (ret) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2426 | goto out_fail; |
| 2427 | } |
| 2428 | btrfs_release_path(root, path); |
| 2429 | |
| 2430 | di = btrfs_lookup_dir_index_item(trans, root, path, |
| 2431 | old_inode->i_ino, |
| 2432 | old_parent_oid, |
| 2433 | "..", 2, -1); |
| 2434 | if (IS_ERR(di)) { |
| 2435 | ret = PTR_ERR(di); |
| 2436 | goto out_fail; |
| 2437 | } |
| 2438 | if (!di) { |
| 2439 | ret = -ENOENT; |
| 2440 | goto out_fail; |
| 2441 | } |
| 2442 | ret = btrfs_del_item(trans, root, path); |
| 2443 | if (ret) { |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2444 | goto out_fail; |
| 2445 | } |
| 2446 | btrfs_release_path(root, path); |
| 2447 | |
| 2448 | ret = btrfs_insert_dir_item(trans, root, "..", 2, |
| 2449 | old_inode->i_ino, location, |
| 2450 | BTRFS_FT_DIR); |
| 2451 | if (ret) |
| 2452 | goto out_fail; |
| 2453 | } |
| 2454 | |
| 2455 | |
| 2456 | ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry); |
| 2457 | if (ret) |
| 2458 | goto out_fail; |
| 2459 | |
| 2460 | if (new_inode) { |
| 2461 | new_inode->i_ctime = CURRENT_TIME; |
| 2462 | ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry); |
| 2463 | if (ret) |
| 2464 | goto out_fail; |
| 2465 | if (S_ISDIR(new_inode->i_mode)) |
| 2466 | clear_nlink(new_inode); |
| 2467 | else |
| 2468 | drop_nlink(new_inode); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2469 | ret = btrfs_update_inode(trans, root, new_inode); |
| 2470 | if (ret) |
| 2471 | goto out_fail; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2472 | } |
| 2473 | ret = btrfs_add_link(trans, new_dentry, old_inode); |
| 2474 | if (ret) |
| 2475 | goto out_fail; |
| 2476 | |
| 2477 | out_fail: |
| 2478 | btrfs_free_path(path); |
| 2479 | btrfs_end_transaction(trans, root); |
| 2480 | mutex_unlock(&root->fs_info->fs_mutex); |
| 2481 | return ret; |
| 2482 | } |
| 2483 | |
| 2484 | static int btrfs_symlink(struct inode *dir, struct dentry *dentry, |
| 2485 | const char *symname) |
| 2486 | { |
| 2487 | struct btrfs_trans_handle *trans; |
| 2488 | struct btrfs_root *root = BTRFS_I(dir)->root; |
| 2489 | struct btrfs_path *path; |
| 2490 | struct btrfs_key key; |
| 2491 | struct inode *inode; |
| 2492 | int err; |
| 2493 | int drop_inode = 0; |
| 2494 | u64 objectid; |
| 2495 | int name_len; |
| 2496 | int datasize; |
| 2497 | char *ptr; |
| 2498 | struct btrfs_file_extent_item *ei; |
| 2499 | |
| 2500 | name_len = strlen(symname) + 1; |
| 2501 | if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) |
| 2502 | return -ENAMETOOLONG; |
| 2503 | mutex_lock(&root->fs_info->fs_mutex); |
| 2504 | trans = btrfs_start_transaction(root, 1); |
| 2505 | btrfs_set_trans_block_group(trans, dir); |
| 2506 | |
| 2507 | err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid); |
| 2508 | if (err) { |
| 2509 | err = -ENOSPC; |
| 2510 | goto out_unlock; |
| 2511 | } |
| 2512 | |
| 2513 | inode = btrfs_new_inode(trans, root, objectid, |
| 2514 | BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO); |
| 2515 | err = PTR_ERR(inode); |
| 2516 | if (IS_ERR(inode)) |
| 2517 | goto out_unlock; |
| 2518 | |
| 2519 | btrfs_set_trans_block_group(trans, inode); |
| 2520 | err = btrfs_add_nondir(trans, dentry, inode); |
| 2521 | if (err) |
| 2522 | drop_inode = 1; |
| 2523 | else { |
| 2524 | inode->i_mapping->a_ops = &btrfs_aops; |
| 2525 | inode->i_fop = &btrfs_file_operations; |
| 2526 | inode->i_op = &btrfs_file_inode_operations; |
| 2527 | } |
| 2528 | dir->i_sb->s_dirt = 1; |
| 2529 | btrfs_update_inode_block_group(trans, inode); |
| 2530 | btrfs_update_inode_block_group(trans, dir); |
| 2531 | if (drop_inode) |
| 2532 | goto out_unlock; |
| 2533 | |
| 2534 | path = btrfs_alloc_path(); |
| 2535 | BUG_ON(!path); |
| 2536 | key.objectid = inode->i_ino; |
| 2537 | key.offset = 0; |
| 2538 | key.flags = 0; |
| 2539 | btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY); |
| 2540 | datasize = btrfs_file_extent_calc_inline_size(name_len); |
| 2541 | err = btrfs_insert_empty_item(trans, root, path, &key, |
| 2542 | datasize); |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2543 | if (err) { |
| 2544 | drop_inode = 1; |
| 2545 | goto out_unlock; |
| 2546 | } |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2547 | ei = btrfs_item_ptr(btrfs_buffer_leaf(path->nodes[0]), |
| 2548 | path->slots[0], struct btrfs_file_extent_item); |
| 2549 | btrfs_set_file_extent_generation(ei, trans->transid); |
| 2550 | btrfs_set_file_extent_type(ei, |
| 2551 | BTRFS_FILE_EXTENT_INLINE); |
| 2552 | ptr = btrfs_file_extent_inline_start(ei); |
| 2553 | btrfs_memcpy(root, path->nodes[0]->b_data, |
| 2554 | ptr, symname, name_len); |
Chris Mason | ccd467d | 2007-06-28 15:57:36 -0400 | [diff] [blame] | 2555 | btrfs_mark_buffer_dirty(path->nodes[0]); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2556 | btrfs_free_path(path); |
| 2557 | inode->i_op = &btrfs_symlink_inode_operations; |
| 2558 | inode->i_mapping->a_ops = &btrfs_symlink_aops; |
| 2559 | inode->i_size = name_len - 1; |
Chris Mason | 54aa1f4 | 2007-06-22 14:16:25 -0400 | [diff] [blame] | 2560 | err = btrfs_update_inode(trans, root, inode); |
| 2561 | if (err) |
| 2562 | drop_inode = 1; |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2563 | |
| 2564 | out_unlock: |
| 2565 | btrfs_end_transaction(trans, root); |
| 2566 | mutex_unlock(&root->fs_info->fs_mutex); |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2567 | if (drop_inode) { |
| 2568 | inode_dec_link_count(inode); |
| 2569 | iput(inode); |
| 2570 | } |
| 2571 | btrfs_btree_balance_dirty(root); |
| 2572 | return err; |
| 2573 | } |
| 2574 | |
| 2575 | static struct inode_operations btrfs_dir_inode_operations = { |
| 2576 | .lookup = btrfs_lookup, |
| 2577 | .create = btrfs_create, |
| 2578 | .unlink = btrfs_unlink, |
| 2579 | .link = btrfs_link, |
| 2580 | .mkdir = btrfs_mkdir, |
| 2581 | .rmdir = btrfs_rmdir, |
| 2582 | .rename = btrfs_rename, |
| 2583 | .symlink = btrfs_symlink, |
| 2584 | .setattr = btrfs_setattr, |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2585 | .mknod = btrfs_mknod, |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2586 | }; |
| 2587 | |
| 2588 | static struct inode_operations btrfs_dir_ro_inode_operations = { |
| 2589 | .lookup = btrfs_lookup, |
| 2590 | }; |
| 2591 | |
| 2592 | static struct file_operations btrfs_dir_file_operations = { |
| 2593 | .llseek = generic_file_llseek, |
| 2594 | .read = generic_read_dir, |
| 2595 | .readdir = btrfs_readdir, |
| 2596 | .ioctl = btrfs_ioctl, |
| 2597 | #ifdef CONFIG_COMPAT |
| 2598 | .compat_ioctl = btrfs_compat_ioctl, |
| 2599 | #endif |
| 2600 | }; |
| 2601 | |
| 2602 | static struct address_space_operations btrfs_aops = { |
| 2603 | .readpage = btrfs_readpage, |
| 2604 | .writepage = btrfs_writepage, |
| 2605 | .sync_page = block_sync_page, |
| 2606 | .prepare_write = btrfs_prepare_write, |
| 2607 | .commit_write = btrfs_commit_write, |
| 2608 | .bmap = btrfs_bmap, |
| 2609 | }; |
| 2610 | |
| 2611 | static struct address_space_operations btrfs_symlink_aops = { |
| 2612 | .readpage = btrfs_readpage, |
| 2613 | .writepage = btrfs_writepage, |
| 2614 | }; |
| 2615 | |
| 2616 | static struct inode_operations btrfs_file_inode_operations = { |
| 2617 | .truncate = btrfs_truncate, |
| 2618 | .getattr = btrfs_getattr, |
| 2619 | .setattr = btrfs_setattr, |
| 2620 | }; |
| 2621 | |
Josef Bacik | 618e21d | 2007-07-11 10:18:17 -0400 | [diff] [blame] | 2622 | static struct inode_operations btrfs_special_inode_operations = { |
| 2623 | .getattr = btrfs_getattr, |
| 2624 | .setattr = btrfs_setattr, |
| 2625 | }; |
| 2626 | |
Chris Mason | 39279cc | 2007-06-12 06:35:45 -0400 | [diff] [blame] | 2627 | static struct inode_operations btrfs_symlink_inode_operations = { |
| 2628 | .readlink = generic_readlink, |
| 2629 | .follow_link = page_follow_link_light, |
| 2630 | .put_link = page_put_link, |
| 2631 | }; |