aboutsummaryrefslogtreecommitdiffstats
path: root/fs/btrfs/inode.c
diff options
context:
space:
mode:
authorChris Mason <chris.mason@oracle.com>2011-05-28 07:00:39 -0400
committerChris Mason <chris.mason@oracle.com>2011-05-28 07:00:39 -0400
commitff5714cca971848963b87d6b477c16ca8abbaa54 (patch)
tree17ee34b9b5e00804df4059503f82f6f1d94383a9 /fs/btrfs/inode.c
parent174ba50915b08dcfd07c8b5fb795b46a165fa09a (diff)
parentd90c732122a1f6d0efe388a8a204f67f144b2eb3 (diff)
Merge branch 'for-chris' of
git://git.kernel.org/pub/scm/linux/kernel/git/josef/btrfs-work into for-linus Conflicts: fs/btrfs/disk-io.c fs/btrfs/extent-tree.c fs/btrfs/free-space-cache.c fs/btrfs/inode.c fs/btrfs/transaction.c Signed-off-by: Chris Mason <chris.mason@oracle.com>
Diffstat (limited to 'fs/btrfs/inode.c')
-rw-r--r--fs/btrfs/inode.c258
1 files changed, 136 insertions, 122 deletions
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index bb51bb1fa44f..a83e44bf3206 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -138,7 +138,6 @@ static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
138 return -ENOMEM; 138 return -ENOMEM;
139 139
140 path->leave_spinning = 1; 140 path->leave_spinning = 1;
141 btrfs_set_trans_block_group(trans, inode);
142 141
143 key.objectid = btrfs_ino(inode); 142 key.objectid = btrfs_ino(inode);
144 key.offset = start; 143 key.offset = start;
@@ -426,9 +425,8 @@ again:
426 } 425 }
427 } 426 }
428 if (start == 0) { 427 if (start == 0) {
429 trans = btrfs_join_transaction(root, 1); 428 trans = btrfs_join_transaction(root);
430 BUG_ON(IS_ERR(trans)); 429 BUG_ON(IS_ERR(trans));
431 btrfs_set_trans_block_group(trans, inode);
432 trans->block_rsv = &root->fs_info->delalloc_block_rsv; 430 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
433 431
434 /* lets try to make an inline extent */ 432 /* lets try to make an inline extent */
@@ -623,8 +621,9 @@ retry:
623 async_extent->start + async_extent->ram_size - 1, 621 async_extent->start + async_extent->ram_size - 1,
624 GFP_NOFS); 622 GFP_NOFS);
625 623
626 trans = btrfs_join_transaction(root, 1); 624 trans = btrfs_join_transaction(root);
627 BUG_ON(IS_ERR(trans)); 625 BUG_ON(IS_ERR(trans));
626 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
628 ret = btrfs_reserve_extent(trans, root, 627 ret = btrfs_reserve_extent(trans, root,
629 async_extent->compressed_size, 628 async_extent->compressed_size,
630 async_extent->compressed_size, 629 async_extent->compressed_size,
@@ -793,9 +792,8 @@ static noinline int cow_file_range(struct inode *inode,
793 int ret = 0; 792 int ret = 0;
794 793
795 BUG_ON(is_free_space_inode(root, inode)); 794 BUG_ON(is_free_space_inode(root, inode));
796 trans = btrfs_join_transaction(root, 1); 795 trans = btrfs_join_transaction(root);
797 BUG_ON(IS_ERR(trans)); 796 BUG_ON(IS_ERR(trans));
798 btrfs_set_trans_block_group(trans, inode);
799 trans->block_rsv = &root->fs_info->delalloc_block_rsv; 797 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
800 798
801 num_bytes = (end - start + blocksize) & ~(blocksize - 1); 799 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
@@ -1077,10 +1075,12 @@ static noinline int run_delalloc_nocow(struct inode *inode,
1077 nolock = is_free_space_inode(root, inode); 1075 nolock = is_free_space_inode(root, inode);
1078 1076
1079 if (nolock) 1077 if (nolock)
1080 trans = btrfs_join_transaction_nolock(root, 1); 1078 trans = btrfs_join_transaction_nolock(root);
1081 else 1079 else
1082 trans = btrfs_join_transaction(root, 1); 1080 trans = btrfs_join_transaction(root);
1081
1083 BUG_ON(IS_ERR(trans)); 1082 BUG_ON(IS_ERR(trans));
1083 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1084 1084
1085 cow_start = (u64)-1; 1085 cow_start = (u64)-1;
1086 cur_offset = start; 1086 cur_offset = start;
@@ -1519,8 +1519,6 @@ static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
1519{ 1519{
1520 struct btrfs_ordered_sum *sum; 1520 struct btrfs_ordered_sum *sum;
1521 1521
1522 btrfs_set_trans_block_group(trans, inode);
1523
1524 list_for_each_entry(sum, list, list) { 1522 list_for_each_entry(sum, list, list) {
1525 btrfs_csum_file_blocks(trans, 1523 btrfs_csum_file_blocks(trans,
1526 BTRFS_I(inode)->root->fs_info->csum_root, sum); 1524 BTRFS_I(inode)->root->fs_info->csum_root, sum);
@@ -1735,11 +1733,10 @@ static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
1735 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent); 1733 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1736 if (!ret) { 1734 if (!ret) {
1737 if (nolock) 1735 if (nolock)
1738 trans = btrfs_join_transaction_nolock(root, 1); 1736 trans = btrfs_join_transaction_nolock(root);
1739 else 1737 else
1740 trans = btrfs_join_transaction(root, 1); 1738 trans = btrfs_join_transaction(root);
1741 BUG_ON(IS_ERR(trans)); 1739 BUG_ON(IS_ERR(trans));
1742 btrfs_set_trans_block_group(trans, inode);
1743 trans->block_rsv = &root->fs_info->delalloc_block_rsv; 1740 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1744 ret = btrfs_update_inode(trans, root, inode); 1741 ret = btrfs_update_inode(trans, root, inode);
1745 BUG_ON(ret); 1742 BUG_ON(ret);
@@ -1752,11 +1749,10 @@ static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
1752 0, &cached_state, GFP_NOFS); 1749 0, &cached_state, GFP_NOFS);
1753 1750
1754 if (nolock) 1751 if (nolock)
1755 trans = btrfs_join_transaction_nolock(root, 1); 1752 trans = btrfs_join_transaction_nolock(root);
1756 else 1753 else
1757 trans = btrfs_join_transaction(root, 1); 1754 trans = btrfs_join_transaction(root);
1758 BUG_ON(IS_ERR(trans)); 1755 BUG_ON(IS_ERR(trans));
1759 btrfs_set_trans_block_group(trans, inode);
1760 trans->block_rsv = &root->fs_info->delalloc_block_rsv; 1756 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1761 1757
1762 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags)) 1758 if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
@@ -2431,7 +2427,7 @@ int btrfs_orphan_cleanup(struct btrfs_root *root)
2431 (u64)-1); 2427 (u64)-1);
2432 2428
2433 if (root->orphan_block_rsv || root->orphan_item_inserted) { 2429 if (root->orphan_block_rsv || root->orphan_item_inserted) {
2434 trans = btrfs_join_transaction(root, 1); 2430 trans = btrfs_join_transaction(root);
2435 if (!IS_ERR(trans)) 2431 if (!IS_ERR(trans))
2436 btrfs_end_transaction(trans, root); 2432 btrfs_end_transaction(trans, root);
2437 } 2433 }
@@ -2511,12 +2507,12 @@ static void btrfs_read_locked_inode(struct inode *inode)
2511 struct btrfs_root *root = BTRFS_I(inode)->root; 2507 struct btrfs_root *root = BTRFS_I(inode)->root;
2512 struct btrfs_key location; 2508 struct btrfs_key location;
2513 int maybe_acls; 2509 int maybe_acls;
2514 u64 alloc_group_block;
2515 u32 rdev; 2510 u32 rdev;
2516 int ret; 2511 int ret;
2517 2512
2518 path = btrfs_alloc_path(); 2513 path = btrfs_alloc_path();
2519 BUG_ON(!path); 2514 BUG_ON(!path);
2515 path->leave_spinning = 1;
2520 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); 2516 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
2521 2517
2522 ret = btrfs_lookup_inode(NULL, root, path, &location, 0); 2518 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
@@ -2526,6 +2522,12 @@ static void btrfs_read_locked_inode(struct inode *inode)
2526 leaf = path->nodes[0]; 2522 leaf = path->nodes[0];
2527 inode_item = btrfs_item_ptr(leaf, path->slots[0], 2523 inode_item = btrfs_item_ptr(leaf, path->slots[0],
2528 struct btrfs_inode_item); 2524 struct btrfs_inode_item);
2525 if (!leaf->map_token)
2526 map_private_extent_buffer(leaf, (unsigned long)inode_item,
2527 sizeof(struct btrfs_inode_item),
2528 &leaf->map_token, &leaf->kaddr,
2529 &leaf->map_start, &leaf->map_len,
2530 KM_USER1);
2529 2531
2530 inode->i_mode = btrfs_inode_mode(leaf, inode_item); 2532 inode->i_mode = btrfs_inode_mode(leaf, inode_item);
2531 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item); 2533 inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
@@ -2555,8 +2557,6 @@ static void btrfs_read_locked_inode(struct inode *inode)
2555 BTRFS_I(inode)->index_cnt = (u64)-1; 2557 BTRFS_I(inode)->index_cnt = (u64)-1;
2556 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item); 2558 BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
2557 2559
2558 alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
2559
2560 /* 2560 /*
2561 * try to precache a NULL acl entry for files that don't have 2561 * try to precache a NULL acl entry for files that don't have
2562 * any xattrs or acls 2562 * any xattrs or acls
@@ -2566,8 +2566,11 @@ static void btrfs_read_locked_inode(struct inode *inode)
2566 if (!maybe_acls) 2566 if (!maybe_acls)
2567 cache_no_acl(inode); 2567 cache_no_acl(inode);
2568 2568
2569 BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0, 2569 if (leaf->map_token) {
2570 alloc_group_block, 0); 2570 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2571 leaf->map_token = NULL;
2572 }
2573
2571 btrfs_free_path(path); 2574 btrfs_free_path(path);
2572 inode_item = NULL; 2575 inode_item = NULL;
2573 2576
@@ -2647,7 +2650,7 @@ static void fill_inode_item(struct btrfs_trans_handle *trans,
2647 btrfs_set_inode_transid(leaf, item, trans->transid); 2650 btrfs_set_inode_transid(leaf, item, trans->transid);
2648 btrfs_set_inode_rdev(leaf, item, inode->i_rdev); 2651 btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
2649 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags); 2652 btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
2650 btrfs_set_inode_block_group(leaf, item, BTRFS_I(inode)->block_group); 2653 btrfs_set_inode_block_group(leaf, item, 0);
2651 2654
2652 if (leaf->map_token) { 2655 if (leaf->map_token) {
2653 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1); 2656 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
@@ -3004,8 +3007,6 @@ static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
3004 if (IS_ERR(trans)) 3007 if (IS_ERR(trans))
3005 return PTR_ERR(trans); 3008 return PTR_ERR(trans);
3006 3009
3007 btrfs_set_trans_block_group(trans, dir);
3008
3009 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0); 3010 btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3010 3011
3011 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode, 3012 ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
@@ -3094,8 +3095,6 @@ static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3094 if (IS_ERR(trans)) 3095 if (IS_ERR(trans))
3095 return PTR_ERR(trans); 3096 return PTR_ERR(trans);
3096 3097
3097 btrfs_set_trans_block_group(trans, dir);
3098
3099 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) { 3098 if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
3100 err = btrfs_unlink_subvol(trans, root, dir, 3099 err = btrfs_unlink_subvol(trans, root, dir,
3101 BTRFS_I(inode)->location.objectid, 3100 BTRFS_I(inode)->location.objectid,
@@ -3514,7 +3513,6 @@ int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
3514 err = PTR_ERR(trans); 3513 err = PTR_ERR(trans);
3515 break; 3514 break;
3516 } 3515 }
3517 btrfs_set_trans_block_group(trans, inode);
3518 3516
3519 err = btrfs_drop_extents(trans, inode, cur_offset, 3517 err = btrfs_drop_extents(trans, inode, cur_offset,
3520 cur_offset + hole_size, 3518 cur_offset + hole_size,
@@ -3650,7 +3648,6 @@ void btrfs_evict_inode(struct inode *inode)
3650 while (1) { 3648 while (1) {
3651 trans = btrfs_start_transaction(root, 0); 3649 trans = btrfs_start_transaction(root, 0);
3652 BUG_ON(IS_ERR(trans)); 3650 BUG_ON(IS_ERR(trans));
3653 btrfs_set_trans_block_group(trans, inode);
3654 trans->block_rsv = root->orphan_block_rsv; 3651 trans->block_rsv = root->orphan_block_rsv;
3655 3652
3656 ret = btrfs_block_rsv_check(trans, root, 3653 ret = btrfs_block_rsv_check(trans, root,
@@ -4133,7 +4130,8 @@ static int btrfs_real_readdir(struct file *filp, void *dirent,
4133 path = btrfs_alloc_path(); 4130 path = btrfs_alloc_path();
4134 if (!path) 4131 if (!path)
4135 return -ENOMEM; 4132 return -ENOMEM;
4136 path->reada = 2; 4133
4134 path->reada = 1;
4137 4135
4138 if (key_type == BTRFS_DIR_INDEX_KEY) { 4136 if (key_type == BTRFS_DIR_INDEX_KEY) {
4139 INIT_LIST_HEAD(&ins_list); 4137 INIT_LIST_HEAD(&ins_list);
@@ -4274,12 +4272,11 @@ int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
4274 4272
4275 if (wbc->sync_mode == WB_SYNC_ALL) { 4273 if (wbc->sync_mode == WB_SYNC_ALL) {
4276 if (nolock) 4274 if (nolock)
4277 trans = btrfs_join_transaction_nolock(root, 1); 4275 trans = btrfs_join_transaction_nolock(root);
4278 else 4276 else
4279 trans = btrfs_join_transaction(root, 1); 4277 trans = btrfs_join_transaction(root);
4280 if (IS_ERR(trans)) 4278 if (IS_ERR(trans))
4281 return PTR_ERR(trans); 4279 return PTR_ERR(trans);
4282 btrfs_set_trans_block_group(trans, inode);
4283 if (nolock) 4280 if (nolock)
4284 ret = btrfs_end_transaction_nolock(trans, root); 4281 ret = btrfs_end_transaction_nolock(trans, root);
4285 else 4282 else
@@ -4303,9 +4300,8 @@ void btrfs_dirty_inode(struct inode *inode)
4303 if (BTRFS_I(inode)->dummy_inode) 4300 if (BTRFS_I(inode)->dummy_inode)
4304 return; 4301 return;
4305 4302
4306 trans = btrfs_join_transaction(root, 1); 4303 trans = btrfs_join_transaction(root);
4307 BUG_ON(IS_ERR(trans)); 4304 BUG_ON(IS_ERR(trans));
4308 btrfs_set_trans_block_group(trans, inode);
4309 4305
4310 ret = btrfs_update_inode(trans, root, inode); 4306 ret = btrfs_update_inode(trans, root, inode);
4311 if (ret && ret == -ENOSPC) { 4307 if (ret && ret == -ENOSPC) {
@@ -4319,7 +4315,6 @@ void btrfs_dirty_inode(struct inode *inode)
4319 PTR_ERR(trans)); 4315 PTR_ERR(trans));
4320 return; 4316 return;
4321 } 4317 }
4322 btrfs_set_trans_block_group(trans, inode);
4323 4318
4324 ret = btrfs_update_inode(trans, root, inode); 4319 ret = btrfs_update_inode(trans, root, inode);
4325 if (ret) { 4320 if (ret) {
@@ -4418,8 +4413,8 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
4418 struct btrfs_root *root, 4413 struct btrfs_root *root,
4419 struct inode *dir, 4414 struct inode *dir,
4420 const char *name, int name_len, 4415 const char *name, int name_len,
4421 u64 ref_objectid, u64 objectid, 4416 u64 ref_objectid, u64 objectid, int mode,
4422 u64 alloc_hint, int mode, u64 *index) 4417 u64 *index)
4423{ 4418{
4424 struct inode *inode; 4419 struct inode *inode;
4425 struct btrfs_inode_item *inode_item; 4420 struct btrfs_inode_item *inode_item;
@@ -4472,8 +4467,6 @@ static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
4472 owner = 0; 4467 owner = 0;
4473 else 4468 else
4474 owner = 1; 4469 owner = 1;
4475 BTRFS_I(inode)->block_group =
4476 btrfs_find_block_group(root, 0, alloc_hint, owner);
4477 4470
4478 key[0].objectid = objectid; 4471 key[0].objectid = objectid;
4479 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY); 4472 btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
@@ -4629,15 +4622,13 @@ static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
4629 if (IS_ERR(trans)) 4622 if (IS_ERR(trans))
4630 return PTR_ERR(trans); 4623 return PTR_ERR(trans);
4631 4624
4632 btrfs_set_trans_block_group(trans, dir);
4633
4634 err = btrfs_find_free_ino(root, &objectid); 4625 err = btrfs_find_free_ino(root, &objectid);
4635 if (err) 4626 if (err)
4636 goto out_unlock; 4627 goto out_unlock;
4637 4628
4638 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, 4629 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
4639 dentry->d_name.len, btrfs_ino(dir), objectid, 4630 dentry->d_name.len, btrfs_ino(dir), objectid,
4640 BTRFS_I(dir)->block_group, mode, &index); 4631 mode, &index);
4641 if (IS_ERR(inode)) { 4632 if (IS_ERR(inode)) {
4642 err = PTR_ERR(inode); 4633 err = PTR_ERR(inode);
4643 goto out_unlock; 4634 goto out_unlock;
@@ -4649,7 +4640,6 @@ static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
4649 goto out_unlock; 4640 goto out_unlock;
4650 } 4641 }
4651 4642
4652 btrfs_set_trans_block_group(trans, inode);
4653 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); 4643 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
4654 if (err) 4644 if (err)
4655 drop_inode = 1; 4645 drop_inode = 1;
@@ -4658,8 +4648,6 @@ static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
4658 init_special_inode(inode, inode->i_mode, rdev); 4648 init_special_inode(inode, inode->i_mode, rdev);
4659 btrfs_update_inode(trans, root, inode); 4649 btrfs_update_inode(trans, root, inode);
4660 } 4650 }
4661 btrfs_update_inode_block_group(trans, inode);
4662 btrfs_update_inode_block_group(trans, dir);
4663out_unlock: 4651out_unlock:
4664 nr = trans->blocks_used; 4652 nr = trans->blocks_used;
4665 btrfs_end_transaction_throttle(trans, root); 4653 btrfs_end_transaction_throttle(trans, root);
@@ -4692,15 +4680,13 @@ static int btrfs_create(struct inode *dir, struct dentry *dentry,
4692 if (IS_ERR(trans)) 4680 if (IS_ERR(trans))
4693 return PTR_ERR(trans); 4681 return PTR_ERR(trans);
4694 4682
4695 btrfs_set_trans_block_group(trans, dir);
4696
4697 err = btrfs_find_free_ino(root, &objectid); 4683 err = btrfs_find_free_ino(root, &objectid);
4698 if (err) 4684 if (err)
4699 goto out_unlock; 4685 goto out_unlock;
4700 4686
4701 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, 4687 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
4702 dentry->d_name.len, btrfs_ino(dir), objectid, 4688 dentry->d_name.len, btrfs_ino(dir), objectid,
4703 BTRFS_I(dir)->block_group, mode, &index); 4689 mode, &index);
4704 if (IS_ERR(inode)) { 4690 if (IS_ERR(inode)) {
4705 err = PTR_ERR(inode); 4691 err = PTR_ERR(inode);
4706 goto out_unlock; 4692 goto out_unlock;
@@ -4712,7 +4698,6 @@ static int btrfs_create(struct inode *dir, struct dentry *dentry,
4712 goto out_unlock; 4698 goto out_unlock;
4713 } 4699 }
4714 4700
4715 btrfs_set_trans_block_group(trans, inode);
4716 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); 4701 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
4717 if (err) 4702 if (err)
4718 drop_inode = 1; 4703 drop_inode = 1;
@@ -4723,8 +4708,6 @@ static int btrfs_create(struct inode *dir, struct dentry *dentry,
4723 inode->i_op = &btrfs_file_inode_operations; 4708 inode->i_op = &btrfs_file_inode_operations;
4724 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; 4709 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
4725 } 4710 }
4726 btrfs_update_inode_block_group(trans, inode);
4727 btrfs_update_inode_block_group(trans, dir);
4728out_unlock: 4711out_unlock:
4729 nr = trans->blocks_used; 4712 nr = trans->blocks_used;
4730 btrfs_end_transaction_throttle(trans, root); 4713 btrfs_end_transaction_throttle(trans, root);
@@ -4771,8 +4754,6 @@ static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
4771 4754
4772 btrfs_inc_nlink(inode); 4755 btrfs_inc_nlink(inode);
4773 inode->i_ctime = CURRENT_TIME; 4756 inode->i_ctime = CURRENT_TIME;
4774
4775 btrfs_set_trans_block_group(trans, dir);
4776 ihold(inode); 4757 ihold(inode);
4777 4758
4778 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index); 4759 err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
@@ -4781,7 +4762,6 @@ static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
4781 drop_inode = 1; 4762 drop_inode = 1;
4782 } else { 4763 } else {
4783 struct dentry *parent = dget_parent(dentry); 4764 struct dentry *parent = dget_parent(dentry);
4784 btrfs_update_inode_block_group(trans, dir);
4785 err = btrfs_update_inode(trans, root, inode); 4765 err = btrfs_update_inode(trans, root, inode);
4786 BUG_ON(err); 4766 BUG_ON(err);
4787 btrfs_log_new_name(trans, inode, NULL, parent); 4767 btrfs_log_new_name(trans, inode, NULL, parent);
@@ -4818,7 +4798,6 @@ static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
4818 trans = btrfs_start_transaction(root, 5); 4798 trans = btrfs_start_transaction(root, 5);
4819 if (IS_ERR(trans)) 4799 if (IS_ERR(trans))
4820 return PTR_ERR(trans); 4800 return PTR_ERR(trans);
4821 btrfs_set_trans_block_group(trans, dir);
4822 4801
4823 err = btrfs_find_free_ino(root, &objectid); 4802 err = btrfs_find_free_ino(root, &objectid);
4824 if (err) 4803 if (err)
@@ -4826,8 +4805,7 @@ static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
4826 4805
4827 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, 4806 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
4828 dentry->d_name.len, btrfs_ino(dir), objectid, 4807 dentry->d_name.len, btrfs_ino(dir), objectid,
4829 BTRFS_I(dir)->block_group, S_IFDIR | mode, 4808 S_IFDIR | mode, &index);
4830 &index);
4831 if (IS_ERR(inode)) { 4809 if (IS_ERR(inode)) {
4832 err = PTR_ERR(inode); 4810 err = PTR_ERR(inode);
4833 goto out_fail; 4811 goto out_fail;
@@ -4841,7 +4819,6 @@ static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
4841 4819
4842 inode->i_op = &btrfs_dir_inode_operations; 4820 inode->i_op = &btrfs_dir_inode_operations;
4843 inode->i_fop = &btrfs_dir_file_operations; 4821 inode->i_fop = &btrfs_dir_file_operations;
4844 btrfs_set_trans_block_group(trans, inode);
4845 4822
4846 btrfs_i_size_write(inode, 0); 4823 btrfs_i_size_write(inode, 0);
4847 err = btrfs_update_inode(trans, root, inode); 4824 err = btrfs_update_inode(trans, root, inode);
@@ -4855,8 +4832,6 @@ static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
4855 4832
4856 d_instantiate(dentry, inode); 4833 d_instantiate(dentry, inode);
4857 drop_on_err = 0; 4834 drop_on_err = 0;
4858 btrfs_update_inode_block_group(trans, inode);
4859 btrfs_update_inode_block_group(trans, dir);
4860 4835
4861out_fail: 4836out_fail:
4862 nr = trans->blocks_used; 4837 nr = trans->blocks_used;
@@ -4989,7 +4964,15 @@ again:
4989 4964
4990 if (!path) { 4965 if (!path) {
4991 path = btrfs_alloc_path(); 4966 path = btrfs_alloc_path();
4992 BUG_ON(!path); 4967 if (!path) {
4968 err = -ENOMEM;
4969 goto out;
4970 }
4971 /*
4972 * Chances are we'll be called again, so go ahead and do
4973 * readahead
4974 */
4975 path->reada = 1;
4993 } 4976 }
4994 4977
4995 ret = btrfs_lookup_file_extent(trans, root, path, 4978 ret = btrfs_lookup_file_extent(trans, root, path,
@@ -5130,8 +5113,10 @@ again:
5130 kunmap(page); 5113 kunmap(page);
5131 free_extent_map(em); 5114 free_extent_map(em);
5132 em = NULL; 5115 em = NULL;
5116
5133 btrfs_release_path(path); 5117 btrfs_release_path(path);
5134 trans = btrfs_join_transaction(root, 1); 5118 trans = btrfs_join_transaction(root);
5119
5135 if (IS_ERR(trans)) 5120 if (IS_ERR(trans))
5136 return ERR_CAST(trans); 5121 return ERR_CAST(trans);
5137 goto again; 5122 goto again;
@@ -5375,7 +5360,7 @@ static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
5375 btrfs_drop_extent_cache(inode, start, start + len - 1, 0); 5360 btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
5376 } 5361 }
5377 5362
5378 trans = btrfs_join_transaction(root, 0); 5363 trans = btrfs_join_transaction(root);
5379 if (IS_ERR(trans)) 5364 if (IS_ERR(trans))
5380 return ERR_CAST(trans); 5365 return ERR_CAST(trans);
5381 5366
@@ -5611,7 +5596,7 @@ static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
5611 * to make sure the current transaction stays open 5596 * to make sure the current transaction stays open
5612 * while we look for nocow cross refs 5597 * while we look for nocow cross refs
5613 */ 5598 */
5614 trans = btrfs_join_transaction(root, 0); 5599 trans = btrfs_join_transaction(root);
5615 if (IS_ERR(trans)) 5600 if (IS_ERR(trans))
5616 goto must_cow; 5601 goto must_cow;
5617 5602
@@ -5750,7 +5735,7 @@ again:
5750 5735
5751 BUG_ON(!ordered); 5736 BUG_ON(!ordered);
5752 5737
5753 trans = btrfs_join_transaction(root, 1); 5738 trans = btrfs_join_transaction(root);
5754 if (IS_ERR(trans)) { 5739 if (IS_ERR(trans)) {
5755 err = -ENOMEM; 5740 err = -ENOMEM;
5756 goto out; 5741 goto out;
@@ -6500,6 +6485,7 @@ out:
6500static int btrfs_truncate(struct inode *inode) 6485static int btrfs_truncate(struct inode *inode)
6501{ 6486{
6502 struct btrfs_root *root = BTRFS_I(inode)->root; 6487 struct btrfs_root *root = BTRFS_I(inode)->root;
6488 struct btrfs_block_rsv *rsv;
6503 int ret; 6489 int ret;
6504 int err = 0; 6490 int err = 0;
6505 struct btrfs_trans_handle *trans; 6491 struct btrfs_trans_handle *trans;
@@ -6513,28 +6499,80 @@ static int btrfs_truncate(struct inode *inode)
6513 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1); 6499 btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
6514 btrfs_ordered_update_i_size(inode, inode->i_size, NULL); 6500 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
6515 6501
6516 trans = btrfs_start_transaction(root, 5); 6502 /*
6517 if (IS_ERR(trans)) 6503 * Yes ladies and gentelment, this is indeed ugly. The fact is we have
6518 return PTR_ERR(trans); 6504 * 3 things going on here
6505 *
6506 * 1) We need to reserve space for our orphan item and the space to
6507 * delete our orphan item. Lord knows we don't want to have a dangling
6508 * orphan item because we didn't reserve space to remove it.
6509 *
6510 * 2) We need to reserve space to update our inode.
6511 *
6512 * 3) We need to have something to cache all the space that is going to
6513 * be free'd up by the truncate operation, but also have some slack
6514 * space reserved in case it uses space during the truncate (thank you
6515 * very much snapshotting).
6516 *
6517 * And we need these to all be seperate. The fact is we can use alot of
6518 * space doing the truncate, and we have no earthly idea how much space
6519 * we will use, so we need the truncate reservation to be seperate so it
6520 * doesn't end up using space reserved for updating the inode or
6521 * removing the orphan item. We also need to be able to stop the
6522 * transaction and start a new one, which means we need to be able to
6523 * update the inode several times, and we have no idea of knowing how
6524 * many times that will be, so we can't just reserve 1 item for the
6525 * entirety of the opration, so that has to be done seperately as well.
6526 * Then there is the orphan item, which does indeed need to be held on
6527 * to for the whole operation, and we need nobody to touch this reserved
6528 * space except the orphan code.
6529 *
6530 * So that leaves us with
6531 *
6532 * 1) root->orphan_block_rsv - for the orphan deletion.
6533 * 2) rsv - for the truncate reservation, which we will steal from the
6534 * transaction reservation.
6535 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
6536 * updating the inode.
6537 */
6538 rsv = btrfs_alloc_block_rsv(root);
6539 if (!rsv)
6540 return -ENOMEM;
6541 btrfs_add_durable_block_rsv(root->fs_info, rsv);
6519 6542
6520 btrfs_set_trans_block_group(trans, inode); 6543 trans = btrfs_start_transaction(root, 4);
6544 if (IS_ERR(trans)) {
6545 err = PTR_ERR(trans);
6546 goto out;
6547 }
6548
6549 /*
6550 * Reserve space for the truncate process. Truncate should be adding
6551 * space, but if there are snapshots it may end up using space.
6552 */
6553 ret = btrfs_truncate_reserve_metadata(trans, root, rsv);
6554 BUG_ON(ret);
6521 6555
6522 ret = btrfs_orphan_add(trans, inode); 6556 ret = btrfs_orphan_add(trans, inode);
6523 if (ret) { 6557 if (ret) {
6524 btrfs_end_transaction(trans, root); 6558 btrfs_end_transaction(trans, root);
6525 return ret; 6559 goto out;
6526 } 6560 }
6527 6561
6528 nr = trans->blocks_used; 6562 nr = trans->blocks_used;
6529 btrfs_end_transaction(trans, root); 6563 btrfs_end_transaction(trans, root);
6530 btrfs_btree_balance_dirty(root, nr); 6564 btrfs_btree_balance_dirty(root, nr);
6531 6565
6532 /* Now start a transaction for the truncate */ 6566 /*
6533 trans = btrfs_start_transaction(root, 0); 6567 * Ok so we've already migrated our bytes over for the truncate, so here
6534 if (IS_ERR(trans)) 6568 * just reserve the one slot we need for updating the inode.
6535 return PTR_ERR(trans); 6569 */
6536 btrfs_set_trans_block_group(trans, inode); 6570 trans = btrfs_start_transaction(root, 1);
6537 trans->block_rsv = root->orphan_block_rsv; 6571 if (IS_ERR(trans)) {
6572 err = PTR_ERR(trans);
6573 goto out;
6574 }
6575 trans->block_rsv = rsv;
6538 6576
6539 /* 6577 /*
6540 * setattr is responsible for setting the ordered_data_close flag, 6578 * setattr is responsible for setting the ordered_data_close flag,
@@ -6558,24 +6596,17 @@ static int btrfs_truncate(struct inode *inode)
6558 6596
6559 while (1) { 6597 while (1) {
6560 if (!trans) { 6598 if (!trans) {
6561 trans = btrfs_start_transaction(root, 0); 6599 trans = btrfs_start_transaction(root, 3);
6562 if (IS_ERR(trans)) 6600 if (IS_ERR(trans)) {
6563 return PTR_ERR(trans); 6601 err = PTR_ERR(trans);
6564 btrfs_set_trans_block_group(trans, inode); 6602 goto out;
6565 trans->block_rsv = root->orphan_block_rsv; 6603 }
6566 }
6567 6604
6568 ret = btrfs_block_rsv_check(trans, root, 6605 ret = btrfs_truncate_reserve_metadata(trans, root,
6569 root->orphan_block_rsv, 0, 5); 6606 rsv);
6570 if (ret == -EAGAIN) { 6607 BUG_ON(ret);
6571 ret = btrfs_commit_transaction(trans, root); 6608
6572 if (ret) 6609 trans->block_rsv = rsv;
6573 return ret;
6574 trans = NULL;
6575 continue;
6576 } else if (ret) {
6577 err = ret;
6578 break;
6579 } 6610 }
6580 6611
6581 ret = btrfs_truncate_inode_items(trans, root, inode, 6612 ret = btrfs_truncate_inode_items(trans, root, inode,
@@ -6586,6 +6617,7 @@ static int btrfs_truncate(struct inode *inode)
6586 break; 6617 break;
6587 } 6618 }
6588 6619
6620 trans->block_rsv = &root->fs_info->trans_block_rsv;
6589 ret = btrfs_update_inode(trans, root, inode); 6621 ret = btrfs_update_inode(trans, root, inode);
6590 if (ret) { 6622 if (ret) {
6591 err = ret; 6623 err = ret;
@@ -6599,6 +6631,7 @@ static int btrfs_truncate(struct inode *inode)
6599 } 6631 }
6600 6632
6601 if (ret == 0 && inode->i_nlink > 0) { 6633 if (ret == 0 && inode->i_nlink > 0) {
6634 trans->block_rsv = root->orphan_block_rsv;
6602 ret = btrfs_orphan_del(trans, inode); 6635 ret = btrfs_orphan_del(trans, inode);
6603 if (ret) 6636 if (ret)
6604 err = ret; 6637 err = ret;
@@ -6610,15 +6643,20 @@ static int btrfs_truncate(struct inode *inode)
6610 ret = btrfs_orphan_del(NULL, inode); 6643 ret = btrfs_orphan_del(NULL, inode);
6611 } 6644 }
6612 6645
6646 trans->block_rsv = &root->fs_info->trans_block_rsv;
6613 ret = btrfs_update_inode(trans, root, inode); 6647 ret = btrfs_update_inode(trans, root, inode);
6614 if (ret && !err) 6648 if (ret && !err)
6615 err = ret; 6649 err = ret;
6616 6650
6617 nr = trans->blocks_used; 6651 nr = trans->blocks_used;
6618 ret = btrfs_end_transaction_throttle(trans, root); 6652 ret = btrfs_end_transaction_throttle(trans, root);
6653 btrfs_btree_balance_dirty(root, nr);
6654
6655out:
6656 btrfs_free_block_rsv(root, rsv);
6657
6619 if (ret && !err) 6658 if (ret && !err)
6620 err = ret; 6659 err = ret;
6621 btrfs_btree_balance_dirty(root, nr);
6622 6660
6623 return err; 6661 return err;
6624} 6662}
@@ -6627,15 +6665,14 @@ static int btrfs_truncate(struct inode *inode)
6627 * create a new subvolume directory/inode (helper for the ioctl). 6665 * create a new subvolume directory/inode (helper for the ioctl).
6628 */ 6666 */
6629int btrfs_create_subvol_root(struct btrfs_trans_handle *trans, 6667int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
6630 struct btrfs_root *new_root, 6668 struct btrfs_root *new_root, u64 new_dirid)
6631 u64 new_dirid, u64 alloc_hint)
6632{ 6669{
6633 struct inode *inode; 6670 struct inode *inode;
6634 int err; 6671 int err;
6635 u64 index = 0; 6672 u64 index = 0;
6636 6673
6637 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid, 6674 inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
6638 new_dirid, alloc_hint, S_IFDIR | 0700, &index); 6675 new_dirid, S_IFDIR | 0700, &index);
6639 if (IS_ERR(inode)) 6676 if (IS_ERR(inode))
6640 return PTR_ERR(inode); 6677 return PTR_ERR(inode);
6641 inode->i_op = &btrfs_dir_inode_operations; 6678 inode->i_op = &btrfs_dir_inode_operations;
@@ -6748,21 +6785,6 @@ void btrfs_destroy_inode(struct inode *inode)
6748 spin_unlock(&root->fs_info->ordered_extent_lock); 6785 spin_unlock(&root->fs_info->ordered_extent_lock);
6749 } 6786 }
6750 6787
6751 if (root == root->fs_info->tree_root) {
6752 struct btrfs_block_group_cache *block_group;
6753
6754 block_group = btrfs_lookup_block_group(root->fs_info,
6755 BTRFS_I(inode)->block_group);
6756 if (block_group && block_group->inode == inode) {
6757 spin_lock(&block_group->lock);
6758 block_group->inode = NULL;
6759 spin_unlock(&block_group->lock);
6760 btrfs_put_block_group(block_group);
6761 } else if (block_group) {
6762 btrfs_put_block_group(block_group);
6763 }
6764 }
6765
6766 spin_lock(&root->orphan_lock); 6788 spin_lock(&root->orphan_lock);
6767 if (!list_empty(&BTRFS_I(inode)->i_orphan)) { 6789 if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
6768 printk(KERN_INFO "BTRFS: inode %llu still on the orphan list\n", 6790 printk(KERN_INFO "BTRFS: inode %llu still on the orphan list\n",
@@ -6948,8 +6970,6 @@ static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
6948 goto out_notrans; 6970 goto out_notrans;
6949 } 6971 }
6950 6972
6951 btrfs_set_trans_block_group(trans, new_dir);
6952
6953 if (dest != root) 6973 if (dest != root)
6954 btrfs_record_root_in_trans(trans, dest); 6974 btrfs_record_root_in_trans(trans, dest);
6955 6975
@@ -7131,16 +7151,13 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
7131 if (IS_ERR(trans)) 7151 if (IS_ERR(trans))
7132 return PTR_ERR(trans); 7152 return PTR_ERR(trans);
7133 7153
7134 btrfs_set_trans_block_group(trans, dir);
7135
7136 err = btrfs_find_free_ino(root, &objectid); 7154 err = btrfs_find_free_ino(root, &objectid);
7137 if (err) 7155 if (err)
7138 goto out_unlock; 7156 goto out_unlock;
7139 7157
7140 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name, 7158 inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
7141 dentry->d_name.len, btrfs_ino(dir), objectid, 7159 dentry->d_name.len, btrfs_ino(dir), objectid,
7142 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO, 7160 S_IFLNK|S_IRWXUGO, &index);
7143 &index);
7144 if (IS_ERR(inode)) { 7161 if (IS_ERR(inode)) {
7145 err = PTR_ERR(inode); 7162 err = PTR_ERR(inode);
7146 goto out_unlock; 7163 goto out_unlock;
@@ -7152,7 +7169,6 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
7152 goto out_unlock; 7169 goto out_unlock;
7153 } 7170 }
7154 7171
7155 btrfs_set_trans_block_group(trans, inode);
7156 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index); 7172 err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
7157 if (err) 7173 if (err)
7158 drop_inode = 1; 7174 drop_inode = 1;
@@ -7163,8 +7179,6 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
7163 inode->i_op = &btrfs_file_inode_operations; 7179 inode->i_op = &btrfs_file_inode_operations;
7164 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops; 7180 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
7165 } 7181 }
7166 btrfs_update_inode_block_group(trans, inode);
7167 btrfs_update_inode_block_group(trans, dir);
7168 if (drop_inode) 7182 if (drop_inode)
7169 goto out_unlock; 7183 goto out_unlock;
7170 7184