aboutsummaryrefslogtreecommitdiffstats
path: root/fs/btrfs/inode.c
diff options
context:
space:
mode:
authorYan, Zheng <zheng.yan@oracle.com>2010-05-16 10:48:46 -0400
committerChris Mason <chris.mason@oracle.com>2010-05-25 10:34:50 -0400
commita22285a6a32390195235171b89d157ed1a1fe932 (patch)
tree3fabc88a029e1af4f2fdcc708e7b62ef3cf3703a /fs/btrfs/inode.c
parentf0486c68e4bd9a06a5904d3eeb3a0d73a83befb8 (diff)
Btrfs: Integrate metadata reservation with start_transaction
Besides simplify the code, this change makes sure all metadata reservation for normal metadata operations are released after committing transaction. Changes since V1: Add code that check if unlink and rmdir will free space. Add ENOSPC handling for clone ioctl. Signed-off-by: Yan Zheng <zheng.yan@oracle.com> Signed-off-by: Chris Mason <chris.mason@oracle.com>
Diffstat (limited to 'fs/btrfs/inode.c')
-rw-r--r--fs/btrfs/inode.c403
1 files changed, 257 insertions, 146 deletions
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index f44425081c02..c4b0fd12df68 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -2135,7 +2135,7 @@ void btrfs_orphan_cleanup(struct btrfs_root *root)
2135 * do a destroy_inode 2135 * do a destroy_inode
2136 */ 2136 */
2137 if (is_bad_inode(inode)) { 2137 if (is_bad_inode(inode)) {
2138 trans = btrfs_start_transaction(root, 1); 2138 trans = btrfs_start_transaction(root, 0);
2139 btrfs_orphan_del(trans, inode); 2139 btrfs_orphan_del(trans, inode);
2140 btrfs_end_transaction(trans, root); 2140 btrfs_end_transaction(trans, root);
2141 iput(inode); 2141 iput(inode);
@@ -2478,29 +2478,201 @@ out:
2478 return ret; 2478 return ret;
2479} 2479}
2480 2480
2481static int btrfs_unlink(struct inode *dir, struct dentry *dentry) 2481/* helper to check if there is any shared block in the path */
2482static int check_path_shared(struct btrfs_root *root,
2483 struct btrfs_path *path)
2484{
2485 struct extent_buffer *eb;
2486 int level;
2487 int ret;
2488 u64 refs;
2489
2490 for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
2491 if (!path->nodes[level])
2492 break;
2493 eb = path->nodes[level];
2494 if (!btrfs_block_can_be_shared(root, eb))
2495 continue;
2496 ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len,
2497 &refs, NULL);
2498 if (refs > 1)
2499 return 1;
2500 }
2501 return 0;
2502}
2503
2504/*
2505 * helper to start transaction for unlink and rmdir.
2506 *
2507 * unlink and rmdir are special in btrfs, they do not always free space.
2508 * so in enospc case, we should make sure they will free space before
2509 * allowing them to use the global metadata reservation.
2510 */
2511static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2512 struct dentry *dentry)
2482{ 2513{
2483 struct btrfs_root *root;
2484 struct btrfs_trans_handle *trans; 2514 struct btrfs_trans_handle *trans;
2515 struct btrfs_root *root = BTRFS_I(dir)->root;
2516 struct btrfs_path *path;
2517 struct btrfs_inode_ref *ref;
2518 struct btrfs_dir_item *di;
2485 struct inode *inode = dentry->d_inode; 2519 struct inode *inode = dentry->d_inode;
2520 u64 index;
2521 int check_link = 1;
2522 int err = -ENOSPC;
2486 int ret; 2523 int ret;
2487 unsigned long nr = 0;
2488 2524
2489 root = BTRFS_I(dir)->root; 2525 trans = btrfs_start_transaction(root, 10);
2526 if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
2527 return trans;
2490 2528
2491 /* 2529 if (inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
2492 * 5 items for unlink inode 2530 return ERR_PTR(-ENOSPC);
2493 * 1 for orphan
2494 */
2495 ret = btrfs_reserve_metadata_space(root, 6);
2496 if (ret)
2497 return ret;
2498 2531
2499 trans = btrfs_start_transaction(root, 1); 2532 /* check if there is someone else holds reference */
2533 if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
2534 return ERR_PTR(-ENOSPC);
2535
2536 if (atomic_read(&inode->i_count) > 2)
2537 return ERR_PTR(-ENOSPC);
2538
2539 if (xchg(&root->fs_info->enospc_unlink, 1))
2540 return ERR_PTR(-ENOSPC);
2541
2542 path = btrfs_alloc_path();
2543 if (!path) {
2544 root->fs_info->enospc_unlink = 0;
2545 return ERR_PTR(-ENOMEM);
2546 }
2547
2548 trans = btrfs_start_transaction(root, 0);
2500 if (IS_ERR(trans)) { 2549 if (IS_ERR(trans)) {
2501 btrfs_unreserve_metadata_space(root, 6); 2550 btrfs_free_path(path);
2502 return PTR_ERR(trans); 2551 root->fs_info->enospc_unlink = 0;
2552 return trans;
2553 }
2554
2555 path->skip_locking = 1;
2556 path->search_commit_root = 1;
2557
2558 ret = btrfs_lookup_inode(trans, root, path,
2559 &BTRFS_I(dir)->location, 0);
2560 if (ret < 0) {
2561 err = ret;
2562 goto out;
2563 }
2564 if (ret == 0) {
2565 if (check_path_shared(root, path))
2566 goto out;
2567 } else {
2568 check_link = 0;
2503 } 2569 }
2570 btrfs_release_path(root, path);
2571
2572 ret = btrfs_lookup_inode(trans, root, path,
2573 &BTRFS_I(inode)->location, 0);
2574 if (ret < 0) {
2575 err = ret;
2576 goto out;
2577 }
2578 if (ret == 0) {
2579 if (check_path_shared(root, path))
2580 goto out;
2581 } else {
2582 check_link = 0;
2583 }
2584 btrfs_release_path(root, path);
2585
2586 if (ret == 0 && S_ISREG(inode->i_mode)) {
2587 ret = btrfs_lookup_file_extent(trans, root, path,
2588 inode->i_ino, (u64)-1, 0);
2589 if (ret < 0) {
2590 err = ret;
2591 goto out;
2592 }
2593 BUG_ON(ret == 0);
2594 if (check_path_shared(root, path))
2595 goto out;
2596 btrfs_release_path(root, path);
2597 }
2598
2599 if (!check_link) {
2600 err = 0;
2601 goto out;
2602 }
2603
2604 di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2605 dentry->d_name.name, dentry->d_name.len, 0);
2606 if (IS_ERR(di)) {
2607 err = PTR_ERR(di);
2608 goto out;
2609 }
2610 if (di) {
2611 if (check_path_shared(root, path))
2612 goto out;
2613 } else {
2614 err = 0;
2615 goto out;
2616 }
2617 btrfs_release_path(root, path);
2618
2619 ref = btrfs_lookup_inode_ref(trans, root, path,
2620 dentry->d_name.name, dentry->d_name.len,
2621 inode->i_ino, dir->i_ino, 0);
2622 if (IS_ERR(ref)) {
2623 err = PTR_ERR(ref);
2624 goto out;
2625 }
2626 BUG_ON(!ref);
2627 if (check_path_shared(root, path))
2628 goto out;
2629 index = btrfs_inode_ref_index(path->nodes[0], ref);
2630 btrfs_release_path(root, path);
2631
2632 di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino, index,
2633 dentry->d_name.name, dentry->d_name.len, 0);
2634 if (IS_ERR(di)) {
2635 err = PTR_ERR(di);
2636 goto out;
2637 }
2638 BUG_ON(ret == -ENOENT);
2639 if (check_path_shared(root, path))
2640 goto out;
2641
2642 err = 0;
2643out:
2644 btrfs_free_path(path);
2645 if (err) {
2646 btrfs_end_transaction(trans, root);
2647 root->fs_info->enospc_unlink = 0;
2648 return ERR_PTR(err);
2649 }
2650
2651 trans->block_rsv = &root->fs_info->global_block_rsv;
2652 return trans;
2653}
2654
2655static void __unlink_end_trans(struct btrfs_trans_handle *trans,
2656 struct btrfs_root *root)
2657{
2658 if (trans->block_rsv == &root->fs_info->global_block_rsv) {
2659 BUG_ON(!root->fs_info->enospc_unlink);
2660 root->fs_info->enospc_unlink = 0;
2661 }
2662 btrfs_end_transaction_throttle(trans, root);
2663}
2664
2665static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
2666{
2667 struct btrfs_root *root = BTRFS_I(dir)->root;
2668 struct btrfs_trans_handle *trans;
2669 struct inode *inode = dentry->d_inode;
2670 int ret;
2671 unsigned long nr = 0;
2672
2673 trans = __unlink_start_trans(dir, dentry);
2674 if (IS_ERR(trans))
2675 return PTR_ERR(trans);
2504 2676
2505 btrfs_set_trans_block_group(trans, dir); 2677 btrfs_set_trans_block_group(trans, dir);
2506 2678
@@ -2508,14 +2680,15 @@ static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
2508 2680
2509 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, 2681 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2510 dentry->d_name.name, dentry->d_name.len); 2682 dentry->d_name.name, dentry->d_name.len);
2683 BUG_ON(ret);
2511 2684
2512 if (inode->i_nlink == 0) 2685 if (inode->i_nlink == 0) {
2513 ret = btrfs_orphan_add(trans, inode); 2686 ret = btrfs_orphan_add(trans, inode);
2687 BUG_ON(ret);
2688 }
2514 2689
2515 nr = trans->blocks_used; 2690 nr = trans->blocks_used;
2516 2691 __unlink_end_trans(trans, root);
2517 btrfs_end_transaction_throttle(trans, root);
2518 btrfs_unreserve_metadata_space(root, 6);
2519 btrfs_btree_balance_dirty(root, nr); 2692 btrfs_btree_balance_dirty(root, nr);
2520 return ret; 2693 return ret;
2521} 2694}
@@ -2587,7 +2760,6 @@ static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
2587{ 2760{
2588 struct inode *inode = dentry->d_inode; 2761 struct inode *inode = dentry->d_inode;
2589 int err = 0; 2762 int err = 0;
2590 int ret;
2591 struct btrfs_root *root = BTRFS_I(dir)->root; 2763 struct btrfs_root *root = BTRFS_I(dir)->root;
2592 struct btrfs_trans_handle *trans; 2764 struct btrfs_trans_handle *trans;
2593 unsigned long nr = 0; 2765 unsigned long nr = 0;
@@ -2596,15 +2768,9 @@ static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
2596 inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) 2768 inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
2597 return -ENOTEMPTY; 2769 return -ENOTEMPTY;
2598 2770
2599 ret = btrfs_reserve_metadata_space(root, 5); 2771 trans = __unlink_start_trans(dir, dentry);
2600 if (ret) 2772 if (IS_ERR(trans))
2601 return ret;
2602
2603 trans = btrfs_start_transaction(root, 1);
2604 if (IS_ERR(trans)) {
2605 btrfs_unreserve_metadata_space(root, 5);
2606 return PTR_ERR(trans); 2773 return PTR_ERR(trans);
2607 }
2608 2774
2609 btrfs_set_trans_block_group(trans, dir); 2775 btrfs_set_trans_block_group(trans, dir);
2610 2776
@@ -2627,12 +2793,9 @@ static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
2627 btrfs_i_size_write(inode, 0); 2793 btrfs_i_size_write(inode, 0);
2628out: 2794out:
2629 nr = trans->blocks_used; 2795 nr = trans->blocks_used;
2630 ret = btrfs_end_transaction_throttle(trans, root); 2796 __unlink_end_trans(trans, root);
2631 btrfs_unreserve_metadata_space(root, 5);
2632 btrfs_btree_balance_dirty(root, nr); 2797 btrfs_btree_balance_dirty(root, nr);
2633 2798
2634 if (ret && !err)
2635 err = ret;
2636 return err; 2799 return err;
2637} 2800}
2638 2801
@@ -3145,7 +3308,7 @@ int btrfs_cont_expand(struct inode *inode, loff_t size)
3145 struct btrfs_trans_handle *trans; 3308 struct btrfs_trans_handle *trans;
3146 struct btrfs_root *root = BTRFS_I(inode)->root; 3309 struct btrfs_root *root = BTRFS_I(inode)->root;
3147 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree; 3310 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3148 struct extent_map *em; 3311 struct extent_map *em = NULL;
3149 struct extent_state *cached_state = NULL; 3312 struct extent_state *cached_state = NULL;
3150 u64 mask = root->sectorsize - 1; 3313 u64 mask = root->sectorsize - 1;
3151 u64 hole_start = (inode->i_size + mask) & ~mask; 3314 u64 hole_start = (inode->i_size + mask) & ~mask;
@@ -3183,11 +3346,11 @@ int btrfs_cont_expand(struct inode *inode, loff_t size)
3183 u64 hint_byte = 0; 3346 u64 hint_byte = 0;
3184 hole_size = last_byte - cur_offset; 3347 hole_size = last_byte - cur_offset;
3185 3348
3186 err = btrfs_reserve_metadata_space(root, 2); 3349 trans = btrfs_start_transaction(root, 2);
3187 if (err) 3350 if (IS_ERR(trans)) {
3351 err = PTR_ERR(trans);
3188 break; 3352 break;
3189 3353 }
3190 trans = btrfs_start_transaction(root, 1);
3191 btrfs_set_trans_block_group(trans, inode); 3354 btrfs_set_trans_block_group(trans, inode);
3192 3355
3193 err = btrfs_drop_extents(trans, inode, cur_offset, 3356 err = btrfs_drop_extents(trans, inode, cur_offset,
@@ -3205,14 +3368,15 @@ int btrfs_cont_expand(struct inode *inode, loff_t size)
3205 last_byte - 1, 0); 3368 last_byte - 1, 0);
3206 3369
3207 btrfs_end_transaction(trans, root); 3370 btrfs_end_transaction(trans, root);
3208 btrfs_unreserve_metadata_space(root, 2);
3209 } 3371 }
3210 free_extent_map(em); 3372 free_extent_map(em);
3373 em = NULL;
3211 cur_offset = last_byte; 3374 cur_offset = last_byte;
3212 if (cur_offset >= block_end) 3375 if (cur_offset >= block_end)
3213 break; 3376 break;
3214 } 3377 }
3215 3378
3379 free_extent_map(em);
3216 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state, 3380 unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
3217 GFP_NOFS); 3381 GFP_NOFS);
3218 return err; 3382 return err;
@@ -3239,10 +3403,6 @@ static int btrfs_setattr_size(struct inode *inode, struct iattr *attr)
3239 } 3403 }
3240 } 3404 }
3241 3405
3242 ret = btrfs_reserve_metadata_space(root, 1);
3243 if (ret)
3244 return ret;
3245
3246 trans = btrfs_start_transaction(root, 1); 3406 trans = btrfs_start_transaction(root, 1);
3247 btrfs_set_trans_block_group(trans, inode); 3407 btrfs_set_trans_block_group(trans, inode);
3248 3408
@@ -3251,7 +3411,6 @@ static int btrfs_setattr_size(struct inode *inode, struct iattr *attr)
3251 3411
3252 nr = trans->blocks_used; 3412 nr = trans->blocks_used;
3253 btrfs_end_transaction(trans, root); 3413 btrfs_end_transaction(trans, root);
3254 btrfs_unreserve_metadata_space(root, 1);
3255 btrfs_btree_balance_dirty(root, nr); 3414 btrfs_btree_balance_dirty(root, nr);
3256 3415
3257 if (attr->ia_size > inode->i_size) { 3416 if (attr->ia_size > inode->i_size) {
@@ -4223,26 +4382,21 @@ static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
4223 if (!new_valid_dev(rdev)) 4382 if (!new_valid_dev(rdev))
4224 return -EINVAL; 4383 return -EINVAL;
4225 4384
4385 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
4386 if (err)
4387 return err;
4388
4226 /* 4389 /*
4227 * 2 for inode item and ref 4390 * 2 for inode item and ref
4228 * 2 for dir items 4391 * 2 for dir items
4229 * 1 for xattr if selinux is on 4392 * 1 for xattr if selinux is on
4230 */ 4393 */
4231 err = btrfs_reserve_metadata_space(root, 5); 4394 trans = btrfs_start_transaction(root, 5);
4232 if (err) 4395 if (IS_ERR(trans))
4233 return err; 4396 return PTR_ERR(trans);
4234 4397
4235 trans = btrfs_start_transaction(root, 1);
4236 if (!trans)
4237 goto fail;
4238 btrfs_set_trans_block_group(trans, dir); 4398 btrfs_set_trans_block_group(trans, dir);
4239 4399
4240 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
4241 if (err) {
4242 err = -ENOSPC;
4243 goto out_unlock;
4244 }
4245
4246 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, 4400 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
4247 dentry->d_name.len, 4401 dentry->d_name.len,
4248 dentry->d_parent->d_inode->i_ino, objectid, 4402 dentry->d_parent->d_inode->i_ino, objectid,
@@ -4271,13 +4425,11 @@ static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
4271out_unlock: 4425out_unlock:
4272 nr = trans->blocks_used; 4426 nr = trans->blocks_used;
4273 btrfs_end_transaction_throttle(trans, root); 4427 btrfs_end_transaction_throttle(trans, root);
4274fail: 4428 btrfs_btree_balance_dirty(root, nr);
4275 btrfs_unreserve_metadata_space(root, 5);
4276 if (drop_inode) { 4429 if (drop_inode) {
4277 inode_dec_link_count(inode); 4430 inode_dec_link_count(inode);
4278 iput(inode); 4431 iput(inode);
4279 } 4432 }
4280 btrfs_btree_balance_dirty(root, nr);
4281 return err; 4433 return err;
4282} 4434}
4283 4435
@@ -4287,32 +4439,26 @@ static int btrfs_create(struct inode *dir, struct dentry *dentry,
4287 struct btrfs_trans_handle *trans; 4439 struct btrfs_trans_handle *trans;
4288 struct btrfs_root *root = BTRFS_I(dir)->root; 4440 struct btrfs_root *root = BTRFS_I(dir)->root;
4289 struct inode *inode = NULL; 4441 struct inode *inode = NULL;
4290 int err;
4291 int drop_inode = 0; 4442 int drop_inode = 0;
4443 int err;
4292 unsigned long nr = 0; 4444 unsigned long nr = 0;
4293 u64 objectid; 4445 u64 objectid;
4294 u64 index = 0; 4446 u64 index = 0;
4295 4447
4448 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
4449 if (err)
4450 return err;
4296 /* 4451 /*
4297 * 2 for inode item and ref 4452 * 2 for inode item and ref
4298 * 2 for dir items 4453 * 2 for dir items
4299 * 1 for xattr if selinux is on 4454 * 1 for xattr if selinux is on
4300 */ 4455 */
4301 err = btrfs_reserve_metadata_space(root, 5); 4456 trans = btrfs_start_transaction(root, 5);
4302 if (err) 4457 if (IS_ERR(trans))
4303 return err; 4458 return PTR_ERR(trans);
4304 4459
4305 trans = btrfs_start_transaction(root, 1);
4306 if (!trans)
4307 goto fail;
4308 btrfs_set_trans_block_group(trans, dir); 4460 btrfs_set_trans_block_group(trans, dir);
4309 4461
4310 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
4311 if (err) {
4312 err = -ENOSPC;
4313 goto out_unlock;
4314 }
4315
4316 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, 4462 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
4317 dentry->d_name.len, 4463 dentry->d_name.len,
4318 dentry->d_parent->d_inode->i_ino, 4464 dentry->d_parent->d_inode->i_ino,
@@ -4344,8 +4490,6 @@ static int btrfs_create(struct inode *dir, struct dentry *dentry,
4344out_unlock: 4490out_unlock:
4345 nr = trans->blocks_used; 4491 nr = trans->blocks_used;
4346 btrfs_end_transaction_throttle(trans, root); 4492 btrfs_end_transaction_throttle(trans, root);
4347fail:
4348 btrfs_unreserve_metadata_space(root, 5);
4349 if (drop_inode) { 4493 if (drop_inode) {
4350 inode_dec_link_count(inode); 4494 inode_dec_link_count(inode);
4351 iput(inode); 4495 iput(inode);
@@ -4372,21 +4516,21 @@ static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
4372 if (root->objectid != BTRFS_I(inode)->root->objectid) 4516 if (root->objectid != BTRFS_I(inode)->root->objectid)
4373 return -EPERM; 4517 return -EPERM;
4374 4518
4375 /*
4376 * 1 item for inode ref
4377 * 2 items for dir items
4378 */
4379 err = btrfs_reserve_metadata_space(root, 3);
4380 if (err)
4381 return err;
4382
4383 btrfs_inc_nlink(inode); 4519 btrfs_inc_nlink(inode);
4384 4520
4385 err = btrfs_set_inode_index(dir, &index); 4521 err = btrfs_set_inode_index(dir, &index);
4386 if (err) 4522 if (err)
4387 goto fail; 4523 goto fail;
4388 4524
4389 trans = btrfs_start_transaction(root, 1); 4525 /*
4526 * 1 item for inode ref
4527 * 2 items for dir items
4528 */
4529 trans = btrfs_start_transaction(root, 3);
4530 if (IS_ERR(trans)) {
4531 err = PTR_ERR(trans);
4532 goto fail;
4533 }
4390 4534
4391 btrfs_set_trans_block_group(trans, dir); 4535 btrfs_set_trans_block_group(trans, dir);
4392 atomic_inc(&inode->i_count); 4536 atomic_inc(&inode->i_count);
@@ -4405,7 +4549,6 @@ static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
4405 nr = trans->blocks_used; 4549 nr = trans->blocks_used;
4406 btrfs_end_transaction_throttle(trans, root); 4550 btrfs_end_transaction_throttle(trans, root);
4407fail: 4551fail:
4408 btrfs_unreserve_metadata_space(root, 3);
4409 if (drop_inode) { 4552 if (drop_inode) {
4410 inode_dec_link_count(inode); 4553 inode_dec_link_count(inode);
4411 iput(inode); 4554 iput(inode);
@@ -4425,28 +4568,20 @@ static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
4425 u64 index = 0; 4568 u64 index = 0;
4426 unsigned long nr = 1; 4569 unsigned long nr = 1;
4427 4570
4571 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
4572 if (err)
4573 return err;
4574
4428 /* 4575 /*
4429 * 2 items for inode and ref 4576 * 2 items for inode and ref
4430 * 2 items for dir items 4577 * 2 items for dir items
4431 * 1 for xattr if selinux is on 4578 * 1 for xattr if selinux is on
4432 */ 4579 */
4433 err = btrfs_reserve_metadata_space(root, 5); 4580 trans = btrfs_start_transaction(root, 5);
4434 if (err) 4581 if (IS_ERR(trans))
4435 return err; 4582 return PTR_ERR(trans);
4436
4437 trans = btrfs_start_transaction(root, 1);
4438 if (!trans) {
4439 err = -ENOMEM;
4440 goto out_unlock;
4441 }
4442 btrfs_set_trans_block_group(trans, dir); 4583 btrfs_set_trans_block_group(trans, dir);
4443 4584
4444 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
4445 if (err) {
4446 err = -ENOSPC;
4447 goto out_fail;
4448 }
4449
4450 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, 4585 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
4451 dentry->d_name.len, 4586 dentry->d_name.len,
4452 dentry->d_parent->d_inode->i_ino, objectid, 4587 dentry->d_parent->d_inode->i_ino, objectid,
@@ -4486,9 +4621,6 @@ static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
4486out_fail: 4621out_fail:
4487 nr = trans->blocks_used; 4622 nr = trans->blocks_used;
4488 btrfs_end_transaction_throttle(trans, root); 4623 btrfs_end_transaction_throttle(trans, root);
4489
4490out_unlock:
4491 btrfs_unreserve_metadata_space(root, 5);
4492 if (drop_on_err) 4624 if (drop_on_err)
4493 iput(inode); 4625 iput(inode);
4494 btrfs_btree_balance_dirty(root, nr); 4626 btrfs_btree_balance_dirty(root, nr);
@@ -5426,19 +5558,6 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
5426 if (S_ISDIR(old_inode->i_mode) && new_inode && 5558 if (S_ISDIR(old_inode->i_mode) && new_inode &&
5427 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) 5559 new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
5428 return -ENOTEMPTY; 5560 return -ENOTEMPTY;
5429
5430 /*
5431 * We want to reserve the absolute worst case amount of items. So if
5432 * both inodes are subvols and we need to unlink them then that would
5433 * require 4 item modifications, but if they are both normal inodes it
5434 * would require 5 item modifications, so we'll assume their normal
5435 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
5436 * should cover the worst case number of items we'll modify.
5437 */
5438 ret = btrfs_reserve_metadata_space(root, 11);
5439 if (ret)
5440 return ret;
5441
5442 /* 5561 /*
5443 * we're using rename to replace one file with another. 5562 * we're using rename to replace one file with another.
5444 * and the replacement file is large. Start IO on it now so 5563 * and the replacement file is large. Start IO on it now so
@@ -5451,8 +5570,18 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
5451 /* close the racy window with snapshot create/destroy ioctl */ 5570 /* close the racy window with snapshot create/destroy ioctl */
5452 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) 5571 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
5453 down_read(&root->fs_info->subvol_sem); 5572 down_read(&root->fs_info->subvol_sem);
5573 /*
5574 * We want to reserve the absolute worst case amount of items. So if
5575 * both inodes are subvols and we need to unlink them then that would
5576 * require 4 item modifications, but if they are both normal inodes it
5577 * would require 5 item modifications, so we'll assume their normal
5578 * inodes. So 5 * 2 is 10, plus 1 for the new link, so 11 total items
5579 * should cover the worst case number of items we'll modify.
5580 */
5581 trans = btrfs_start_transaction(root, 20);
5582 if (IS_ERR(trans))
5583 return PTR_ERR(trans);
5454 5584
5455 trans = btrfs_start_transaction(root, 1);
5456 btrfs_set_trans_block_group(trans, new_dir); 5585 btrfs_set_trans_block_group(trans, new_dir);
5457 5586
5458 if (dest != root) 5587 if (dest != root)
@@ -5551,7 +5680,6 @@ out_fail:
5551 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID) 5680 if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
5552 up_read(&root->fs_info->subvol_sem); 5681 up_read(&root->fs_info->subvol_sem);
5553 5682
5554 btrfs_unreserve_metadata_space(root, 11);
5555 return ret; 5683 return ret;
5556} 5684}
5557 5685
@@ -5658,26 +5786,20 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
5658 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) 5786 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
5659 return -ENAMETOOLONG; 5787 return -ENAMETOOLONG;
5660 5788
5789 err = btrfs_find_free_objectid(NULL, root, dir->i_ino, &objectid);
5790 if (err)
5791 return err;
5661 /* 5792 /*
5662 * 2 items for inode item and ref 5793 * 2 items for inode item and ref
5663 * 2 items for dir items 5794 * 2 items for dir items
5664 * 1 item for xattr if selinux is on 5795 * 1 item for xattr if selinux is on
5665 */ 5796 */
5666 err = btrfs_reserve_metadata_space(root, 5); 5797 trans = btrfs_start_transaction(root, 5);
5667 if (err) 5798 if (IS_ERR(trans))
5668 return err; 5799 return PTR_ERR(trans);
5669 5800
5670 trans = btrfs_start_transaction(root, 1);
5671 if (!trans)
5672 goto out_fail;
5673 btrfs_set_trans_block_group(trans, dir); 5801 btrfs_set_trans_block_group(trans, dir);
5674 5802
5675 err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
5676 if (err) {
5677 err = -ENOSPC;
5678 goto out_unlock;
5679 }
5680
5681 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, 5803 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
5682 dentry->d_name.len, 5804 dentry->d_name.len,
5683 dentry->d_parent->d_inode->i_ino, objectid, 5805 dentry->d_parent->d_inode->i_ino, objectid,
@@ -5749,8 +5871,6 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
5749out_unlock: 5871out_unlock:
5750 nr = trans->blocks_used; 5872 nr = trans->blocks_used;
5751 btrfs_end_transaction_throttle(trans, root); 5873 btrfs_end_transaction_throttle(trans, root);
5752out_fail:
5753 btrfs_unreserve_metadata_space(root, 5);
5754 if (drop_inode) { 5874 if (drop_inode) {
5755 inode_dec_link_count(inode); 5875 inode_dec_link_count(inode);
5756 iput(inode); 5876 iput(inode);
@@ -5771,21 +5891,18 @@ static int prealloc_file_range(struct inode *inode, u64 start, u64 end,
5771 u64 i_size; 5891 u64 i_size;
5772 5892
5773 while (num_bytes > 0) { 5893 while (num_bytes > 0) {
5774 trans = btrfs_start_transaction(root, 1); 5894 trans = btrfs_start_transaction(root, 3);
5895 if (IS_ERR(trans)) {
5896 ret = PTR_ERR(trans);
5897 break;
5898 }
5775 5899
5776 ret = btrfs_reserve_extent(trans, root, num_bytes, 5900 ret = btrfs_reserve_extent(trans, root, num_bytes,
5777 root->sectorsize, 0, alloc_hint, 5901 root->sectorsize, 0, alloc_hint,
5778 (u64)-1, &ins, 1); 5902 (u64)-1, &ins, 1);
5779 if (ret) { 5903 if (ret) {
5780 WARN_ON(1); 5904 btrfs_end_transaction(trans, root);
5781 goto stop_trans; 5905 break;
5782 }
5783
5784 ret = btrfs_reserve_metadata_space(root, 3);
5785 if (ret) {
5786 btrfs_free_reserved_extent(root, ins.objectid,
5787 ins.offset);
5788 goto stop_trans;
5789 } 5906 }
5790 5907
5791 ret = insert_reserved_file_extent(trans, inode, 5908 ret = insert_reserved_file_extent(trans, inode,
@@ -5819,14 +5936,8 @@ static int prealloc_file_range(struct inode *inode, u64 start, u64 end,
5819 BUG_ON(ret); 5936 BUG_ON(ret);
5820 5937
5821 btrfs_end_transaction(trans, root); 5938 btrfs_end_transaction(trans, root);
5822 btrfs_unreserve_metadata_space(root, 3);
5823 } 5939 }
5824 return ret; 5940 return ret;
5825
5826stop_trans:
5827 btrfs_end_transaction(trans, root);
5828 return ret;
5829
5830} 5941}
5831 5942
5832static long btrfs_fallocate(struct inode *inode, int mode, 5943static long btrfs_fallocate(struct inode *inode, int mode,