aboutsummaryrefslogtreecommitdiffstats
path: root/fs
diff options
context:
space:
mode:
authorChris Mason <chris.mason@oracle.com>2008-06-25 16:01:30 -0400
committerChris Mason <chris.mason@oracle.com>2008-09-25 11:04:03 -0400
commita213501153fd66e2359e091b1612841305ba6551 (patch)
tree1f4c316c9d3cad0eb6d9be14cb0e67e46e815dce /fs
parent925baeddc5b0764a53f2214a1253251bab0e0324 (diff)
Btrfs: Replace the big fs_mutex with a collection of other locks
Extent alloctions are still protected by a large alloc_mutex. Objectid allocations are covered by a objectid mutex Other btree operations are protected by a lock on individual btree nodes Signed-off-by: Chris Mason <chris.mason@oracle.com>
Diffstat (limited to 'fs')
-rw-r--r--fs/btrfs/ctree.c40
-rw-r--r--fs/btrfs/ctree.h5
-rw-r--r--fs/btrfs/disk-io.c15
-rw-r--r--fs/btrfs/extent-tree.c18
-rw-r--r--fs/btrfs/file.c7
-rw-r--r--fs/btrfs/inode-map.c8
-rw-r--r--fs/btrfs/inode.c76
-rw-r--r--fs/btrfs/ioctl.c24
-rw-r--r--fs/btrfs/super.c2
-rw-r--r--fs/btrfs/transaction.c42
-rw-r--r--fs/btrfs/volumes.c19
-rw-r--r--fs/btrfs/xattr.c10
12 files changed, 101 insertions, 165 deletions
diff --git a/fs/btrfs/ctree.c b/fs/btrfs/ctree.c
index 75625c68fdd8..dff4da082d06 100644
--- a/fs/btrfs/ctree.c
+++ b/fs/btrfs/ctree.c
@@ -63,6 +63,9 @@ void btrfs_free_path(struct btrfs_path *p)
63void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p) 63void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p)
64{ 64{
65 int i; 65 int i;
66 int skip = p->skip_locking;
67 int keep = p->keep_locks;
68
66 for (i = 0; i < BTRFS_MAX_LEVEL; i++) { 69 for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
67 if (!p->nodes[i]) 70 if (!p->nodes[i])
68 continue; 71 continue;
@@ -73,6 +76,8 @@ void btrfs_release_path(struct btrfs_root *root, struct btrfs_path *p)
73 free_extent_buffer(p->nodes[i]); 76 free_extent_buffer(p->nodes[i]);
74 } 77 }
75 memset(p, 0, sizeof(*p)); 78 memset(p, 0, sizeof(*p));
79 p->skip_locking = skip;
80 p->keep_locks = keep;
76} 81}
77 82
78struct extent_buffer *btrfs_root_node(struct btrfs_root *root) 83struct extent_buffer *btrfs_root_node(struct btrfs_root *root)
@@ -1202,13 +1207,19 @@ static void unlock_up(struct btrfs_path *path, int level, int lowest_unlock)
1202 u32 nritems; 1207 u32 nritems;
1203 t = path->nodes[i]; 1208 t = path->nodes[i];
1204 nritems = btrfs_header_nritems(t); 1209 nritems = btrfs_header_nritems(t);
1205 if (path->slots[i] >= nritems - 1) { 1210 if (nritems < 2 || path->slots[i] >= nritems - 2) {
1211if (path->keep_locks) {
1212//printk("path %p skip level now %d\n", path, skip_level);
1213}
1206 skip_level = i + 1; 1214 skip_level = i + 1;
1207 continue; 1215 continue;
1208 } 1216 }
1209 } 1217 }
1210 t = path->nodes[i]; 1218 t = path->nodes[i];
1211 if (i >= lowest_unlock && i > skip_level && path->locks[i]) { 1219 if (i >= lowest_unlock && i > skip_level && path->locks[i]) {
1220if (path->keep_locks) {
1221//printk("path %p unlocking level %d slot %d nritems %d skip_level %d\n", path, i, path->slots[i], btrfs_header_nritems(t), skip_level);
1222}
1212 btrfs_tree_unlock(t); 1223 btrfs_tree_unlock(t);
1213 path->locks[i] = 0; 1224 path->locks[i] = 0;
1214 } 1225 }
@@ -1243,7 +1254,6 @@ int btrfs_search_slot(struct btrfs_trans_handle *trans, struct btrfs_root
1243 lowest_level = p->lowest_level; 1254 lowest_level = p->lowest_level;
1244 WARN_ON(lowest_level && ins_len); 1255 WARN_ON(lowest_level && ins_len);
1245 WARN_ON(p->nodes[0] != NULL); 1256 WARN_ON(p->nodes[0] != NULL);
1246 // WARN_ON(!mutex_is_locked(&root->fs_info->fs_mutex));
1247 WARN_ON(root == root->fs_info->extent_root && 1257 WARN_ON(root == root->fs_info->extent_root &&
1248 !mutex_is_locked(&root->fs_info->alloc_mutex)); 1258 !mutex_is_locked(&root->fs_info->alloc_mutex));
1249 WARN_ON(root == root->fs_info->chunk_root && 1259 WARN_ON(root == root->fs_info->chunk_root &&
@@ -1321,7 +1331,7 @@ again:
1321 b = read_node_slot(root, b, slot); 1331 b = read_node_slot(root, b, slot);
1322 if (!p->skip_locking) 1332 if (!p->skip_locking)
1323 btrfs_tree_lock(b); 1333 btrfs_tree_lock(b);
1324 unlock_up(p, level, lowest_unlock); 1334 unlock_up(p, level + 1, lowest_unlock);
1325 } else { 1335 } else {
1326 p->slots[level] = slot; 1336 p->slots[level] = slot;
1327 if (ins_len > 0 && btrfs_leaf_free_space(root, b) < 1337 if (ins_len > 0 && btrfs_leaf_free_space(root, b) <
@@ -1804,6 +1814,8 @@ static int push_leaf_right(struct btrfs_trans_handle *trans, struct btrfs_root
1804 if (slot >= btrfs_header_nritems(upper) - 1) 1814 if (slot >= btrfs_header_nritems(upper) - 1)
1805 return 1; 1815 return 1;
1806 1816
1817 WARN_ON(!btrfs_tree_locked(path->nodes[1]));
1818
1807 right = read_node_slot(root, upper, slot + 1); 1819 right = read_node_slot(root, upper, slot + 1);
1808 btrfs_tree_lock(right); 1820 btrfs_tree_lock(right);
1809 free_space = btrfs_leaf_free_space(root, right); 1821 free_space = btrfs_leaf_free_space(root, right);
@@ -1981,6 +1993,8 @@ static int push_leaf_left(struct btrfs_trans_handle *trans, struct btrfs_root
1981 return 1; 1993 return 1;
1982 } 1994 }
1983 1995
1996 WARN_ON(!btrfs_tree_locked(path->nodes[1]));
1997
1984 left = read_node_slot(root, path->nodes[1], slot - 1); 1998 left = read_node_slot(root, path->nodes[1], slot - 1);
1985 btrfs_tree_lock(left); 1999 btrfs_tree_lock(left);
1986 free_space = btrfs_leaf_free_space(root, left); 2000 free_space = btrfs_leaf_free_space(root, left);
@@ -2957,15 +2971,16 @@ int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
2957 2971
2958 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1); 2972 btrfs_item_key_to_cpu(path->nodes[0], &key, nritems - 1);
2959 2973
2960 path->keep_locks = 1;
2961 btrfs_release_path(root, path); 2974 btrfs_release_path(root, path);
2975 path->keep_locks = 1;
2962 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 2976 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2963 path->keep_locks = 0; 2977 path->keep_locks = 0;
2964 2978
2965 if (ret < 0) 2979 if (ret < 0)
2966 return ret; 2980 return ret;
2967 2981
2968 if (path->slots[0] < nritems - 1) { 2982 nritems = btrfs_header_nritems(path->nodes[0]);
2983 if (nritems > 0 && path->slots[0] < nritems - 1) {
2969 goto done; 2984 goto done;
2970 } 2985 }
2971 2986
@@ -2992,8 +3007,17 @@ int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
2992 reada_for_search(root, path, level, slot, 0); 3007 reada_for_search(root, path, level, slot, 0);
2993 3008
2994 next = read_node_slot(root, c, slot); 3009 next = read_node_slot(root, c, slot);
2995 if (!path->skip_locking) 3010 if (!path->skip_locking) {
3011 if (!btrfs_tree_locked(c)) {
3012 int i;
3013 WARN_ON(1);
3014printk("path %p no lock on level %d\n", path, level);
3015for (i = 0; i < BTRFS_MAX_LEVEL; i++) {
3016printk("path %p level %d slot %d nritems %d\n", path, i, path->slots[i], btrfs_header_nritems(path->nodes[i]));
3017}
3018 }
2996 btrfs_tree_lock(next); 3019 btrfs_tree_lock(next);
3020 }
2997 break; 3021 break;
2998 } 3022 }
2999 path->slots[level] = slot; 3023 path->slots[level] = slot;
@@ -3011,8 +3035,10 @@ int btrfs_next_leaf(struct btrfs_root *root, struct btrfs_path *path)
3011 if (level == 1 && path->locks[1] && path->reada) 3035 if (level == 1 && path->locks[1] && path->reada)
3012 reada_for_search(root, path, level, slot, 0); 3036 reada_for_search(root, path, level, slot, 0);
3013 next = read_node_slot(root, next, 0); 3037 next = read_node_slot(root, next, 0);
3014 if (!path->skip_locking) 3038 if (!path->skip_locking) {
3039 WARN_ON(!btrfs_tree_locked(path->nodes[level]));
3015 btrfs_tree_lock(next); 3040 btrfs_tree_lock(next);
3041 }
3016 } 3042 }
3017done: 3043done:
3018 unlock_up(path, 0, 1); 3044 unlock_up(path, 0, 1);
diff --git a/fs/btrfs/ctree.h b/fs/btrfs/ctree.h
index 50891b39f366..692b8ea42de1 100644
--- a/fs/btrfs/ctree.h
+++ b/fs/btrfs/ctree.h
@@ -519,9 +519,9 @@ struct btrfs_fs_info {
519 struct backing_dev_info bdi; 519 struct backing_dev_info bdi;
520 spinlock_t hash_lock; 520 spinlock_t hash_lock;
521 struct mutex trans_mutex; 521 struct mutex trans_mutex;
522 struct mutex fs_mutex;
523 struct mutex alloc_mutex; 522 struct mutex alloc_mutex;
524 struct mutex chunk_mutex; 523 struct mutex chunk_mutex;
524 struct mutex drop_mutex;
525 struct list_head trans_list; 525 struct list_head trans_list;
526 struct list_head hashers; 526 struct list_head hashers;
527 struct list_head dead_roots; 527 struct list_head dead_roots;
@@ -554,7 +554,7 @@ struct btrfs_fs_info {
554 struct completion kobj_unregister; 554 struct completion kobj_unregister;
555 int do_barriers; 555 int do_barriers;
556 int closing; 556 int closing;
557 unsigned long throttles; 557 atomic_t throttles;
558 558
559 u64 total_pinned; 559 u64 total_pinned;
560 struct list_head dirty_cowonly_roots; 560 struct list_head dirty_cowonly_roots;
@@ -594,6 +594,7 @@ struct btrfs_root {
594 struct inode *inode; 594 struct inode *inode;
595 struct kobject root_kobj; 595 struct kobject root_kobj;
596 struct completion kobj_unregister; 596 struct completion kobj_unregister;
597 struct mutex objectid_mutex;
597 u64 objectid; 598 u64 objectid;
598 u64 last_trans; 599 u64 last_trans;
599 600
diff --git a/fs/btrfs/disk-io.c b/fs/btrfs/disk-io.c
index fe40bdd984ff..f638803549e0 100644
--- a/fs/btrfs/disk-io.c
+++ b/fs/btrfs/disk-io.c
@@ -724,6 +724,7 @@ static int __setup_root(u32 nodesize, u32 leafsize, u32 sectorsize,
724 724
725 INIT_LIST_HEAD(&root->dirty_list); 725 INIT_LIST_HEAD(&root->dirty_list);
726 spin_lock_init(&root->node_lock); 726 spin_lock_init(&root->node_lock);
727 mutex_init(&root->objectid_mutex);
727 memset(&root->root_key, 0, sizeof(root->root_key)); 728 memset(&root->root_key, 0, sizeof(root->root_key));
728 memset(&root->root_item, 0, sizeof(root->root_item)); 729 memset(&root->root_item, 0, sizeof(root->root_item));
729 memset(&root->defrag_progress, 0, sizeof(root->defrag_progress)); 730 memset(&root->defrag_progress, 0, sizeof(root->defrag_progress));
@@ -1146,6 +1147,7 @@ struct btrfs_root *open_ctree(struct super_block *sb,
1146 INIT_LIST_HEAD(&fs_info->space_info); 1147 INIT_LIST_HEAD(&fs_info->space_info);
1147 btrfs_mapping_init(&fs_info->mapping_tree); 1148 btrfs_mapping_init(&fs_info->mapping_tree);
1148 atomic_set(&fs_info->nr_async_submits, 0); 1149 atomic_set(&fs_info->nr_async_submits, 0);
1150 atomic_set(&fs_info->throttles, 0);
1149 fs_info->sb = sb; 1151 fs_info->sb = sb;
1150 fs_info->max_extent = (u64)-1; 1152 fs_info->max_extent = (u64)-1;
1151 fs_info->max_inline = 8192 * 1024; 1153 fs_info->max_inline = 8192 * 1024;
@@ -1199,7 +1201,7 @@ struct btrfs_root *open_ctree(struct super_block *sb,
1199 mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS); 1201 mapping_set_gfp_mask(fs_info->btree_inode->i_mapping, GFP_NOFS);
1200 1202
1201 mutex_init(&fs_info->trans_mutex); 1203 mutex_init(&fs_info->trans_mutex);
1202 mutex_init(&fs_info->fs_mutex); 1204 mutex_init(&fs_info->drop_mutex);
1203 mutex_init(&fs_info->alloc_mutex); 1205 mutex_init(&fs_info->alloc_mutex);
1204 mutex_init(&fs_info->chunk_mutex); 1206 mutex_init(&fs_info->chunk_mutex);
1205 1207
@@ -1278,8 +1280,6 @@ struct btrfs_root *open_ctree(struct super_block *sb,
1278 goto fail_sb_buffer; 1280 goto fail_sb_buffer;
1279 } 1281 }
1280 1282
1281 mutex_lock(&fs_info->fs_mutex);
1282
1283 mutex_lock(&fs_info->chunk_mutex); 1283 mutex_lock(&fs_info->chunk_mutex);
1284 ret = btrfs_read_sys_array(tree_root); 1284 ret = btrfs_read_sys_array(tree_root);
1285 mutex_unlock(&fs_info->chunk_mutex); 1285 mutex_unlock(&fs_info->chunk_mutex);
@@ -1342,7 +1342,6 @@ struct btrfs_root *open_ctree(struct super_block *sb,
1342 fs_info->metadata_alloc_profile = (u64)-1; 1342 fs_info->metadata_alloc_profile = (u64)-1;
1343 fs_info->system_alloc_profile = fs_info->metadata_alloc_profile; 1343 fs_info->system_alloc_profile = fs_info->metadata_alloc_profile;
1344 1344
1345 mutex_unlock(&fs_info->fs_mutex);
1346 return tree_root; 1345 return tree_root;
1347 1346
1348fail_extent_root: 1347fail_extent_root:
@@ -1350,7 +1349,6 @@ fail_extent_root:
1350fail_tree_root: 1349fail_tree_root:
1351 free_extent_buffer(tree_root->node); 1350 free_extent_buffer(tree_root->node);
1352fail_sys_array: 1351fail_sys_array:
1353 mutex_unlock(&fs_info->fs_mutex);
1354fail_sb_buffer: 1352fail_sb_buffer:
1355 extent_io_tree_empty_lru(&BTRFS_I(fs_info->btree_inode)->io_tree); 1353 extent_io_tree_empty_lru(&BTRFS_I(fs_info->btree_inode)->io_tree);
1356 btrfs_stop_workers(&fs_info->workers); 1354 btrfs_stop_workers(&fs_info->workers);
@@ -1562,8 +1560,9 @@ int close_ctree(struct btrfs_root *root)
1562 struct btrfs_fs_info *fs_info = root->fs_info; 1560 struct btrfs_fs_info *fs_info = root->fs_info;
1563 1561
1564 fs_info->closing = 1; 1562 fs_info->closing = 1;
1563 smp_mb();
1564
1565 btrfs_transaction_flush_work(root); 1565 btrfs_transaction_flush_work(root);
1566 mutex_lock(&fs_info->fs_mutex);
1567 btrfs_defrag_dirty_roots(root->fs_info); 1566 btrfs_defrag_dirty_roots(root->fs_info);
1568 trans = btrfs_start_transaction(root, 1); 1567 trans = btrfs_start_transaction(root, 1);
1569 ret = btrfs_commit_transaction(trans, root); 1568 ret = btrfs_commit_transaction(trans, root);
@@ -1574,7 +1573,6 @@ int close_ctree(struct btrfs_root *root)
1574 BUG_ON(ret); 1573 BUG_ON(ret);
1575 1574
1576 write_ctree_super(NULL, root); 1575 write_ctree_super(NULL, root);
1577 mutex_unlock(&fs_info->fs_mutex);
1578 1576
1579 btrfs_transaction_flush_work(root); 1577 btrfs_transaction_flush_work(root);
1580 1578
@@ -1679,7 +1677,8 @@ void btrfs_throttle(struct btrfs_root *root)
1679 struct backing_dev_info *bdi; 1677 struct backing_dev_info *bdi;
1680 1678
1681 bdi = &root->fs_info->bdi; 1679 bdi = &root->fs_info->bdi;
1682 if (root->fs_info->throttles && bdi_write_congested(bdi)) { 1680 if (atomic_read(&root->fs_info->throttles) &&
1681 bdi_write_congested(bdi)) {
1683#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18) 1682#if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,18)
1684 congestion_wait(WRITE, HZ/20); 1683 congestion_wait(WRITE, HZ/20);
1685#else 1684#else
diff --git a/fs/btrfs/extent-tree.c b/fs/btrfs/extent-tree.c
index 7e40c516fe62..890b9e9d8e27 100644
--- a/fs/btrfs/extent-tree.c
+++ b/fs/btrfs/extent-tree.c
@@ -1577,9 +1577,11 @@ static int __free_extent(struct btrfs_trans_handle *trans, struct btrfs_root
1577 } 1577 }
1578 1578
1579 /* block accounting for super block */ 1579 /* block accounting for super block */
1580 spin_lock_irq(&info->delalloc_lock);
1580 super_used = btrfs_super_bytes_used(&info->super_copy); 1581 super_used = btrfs_super_bytes_used(&info->super_copy);
1581 btrfs_set_super_bytes_used(&info->super_copy, 1582 btrfs_set_super_bytes_used(&info->super_copy,
1582 super_used - num_bytes); 1583 super_used - num_bytes);
1584 spin_unlock_irq(&info->delalloc_lock);
1583 1585
1584 /* block accounting for root item */ 1586 /* block accounting for root item */
1585 root_used = btrfs_root_used(&root->root_item); 1587 root_used = btrfs_root_used(&root->root_item);
@@ -1968,8 +1970,10 @@ again:
1968 } 1970 }
1969 1971
1970 /* block accounting for super block */ 1972 /* block accounting for super block */
1973 spin_lock_irq(&info->delalloc_lock);
1971 super_used = btrfs_super_bytes_used(&info->super_copy); 1974 super_used = btrfs_super_bytes_used(&info->super_copy);
1972 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes); 1975 btrfs_set_super_bytes_used(&info->super_copy, super_used + num_bytes);
1976 spin_unlock_irq(&info->delalloc_lock);
1973 1977
1974 /* block accounting for root item */ 1978 /* block accounting for root item */
1975 root_used = btrfs_root_used(&root->root_item); 1979 root_used = btrfs_root_used(&root->root_item);
@@ -2172,12 +2176,12 @@ static void noinline reada_walk_down(struct btrfs_root *root,
2172 continue; 2176 continue;
2173 } 2177 }
2174 } 2178 }
2175 mutex_unlock(&root->fs_info->fs_mutex); 2179 mutex_unlock(&root->fs_info->alloc_mutex);
2176 ret = readahead_tree_block(root, bytenr, blocksize, 2180 ret = readahead_tree_block(root, bytenr, blocksize,
2177 btrfs_node_ptr_generation(node, i)); 2181 btrfs_node_ptr_generation(node, i));
2178 last = bytenr + blocksize; 2182 last = bytenr + blocksize;
2179 cond_resched(); 2183 cond_resched();
2180 mutex_lock(&root->fs_info->fs_mutex); 2184 mutex_lock(&root->fs_info->alloc_mutex);
2181 if (ret) 2185 if (ret)
2182 break; 2186 break;
2183 } 2187 }
@@ -2254,11 +2258,9 @@ static int noinline walk_down_tree(struct btrfs_trans_handle *trans,
2254 free_extent_buffer(next); 2258 free_extent_buffer(next);
2255 reada_walk_down(root, cur, path->slots[*level]); 2259 reada_walk_down(root, cur, path->slots[*level]);
2256 2260
2257 mutex_unlock(&root->fs_info->fs_mutex);
2258 mutex_unlock(&root->fs_info->alloc_mutex); 2261 mutex_unlock(&root->fs_info->alloc_mutex);
2259 next = read_tree_block(root, bytenr, blocksize, 2262 next = read_tree_block(root, bytenr, blocksize,
2260 ptr_gen); 2263 ptr_gen);
2261 mutex_lock(&root->fs_info->fs_mutex);
2262 mutex_lock(&root->fs_info->alloc_mutex); 2264 mutex_lock(&root->fs_info->alloc_mutex);
2263 2265
2264 /* we've dropped the lock, double check */ 2266 /* we've dropped the lock, double check */
@@ -2381,6 +2383,7 @@ int btrfs_drop_snapshot(struct btrfs_trans_handle *trans, struct btrfs_root
2381 int orig_level; 2383 int orig_level;
2382 struct btrfs_root_item *root_item = &root->root_item; 2384 struct btrfs_root_item *root_item = &root->root_item;
2383 2385
2386 WARN_ON(!mutex_is_locked(&root->fs_info->drop_mutex));
2384 path = btrfs_alloc_path(); 2387 path = btrfs_alloc_path();
2385 BUG_ON(!path); 2388 BUG_ON(!path);
2386 2389
@@ -2710,7 +2713,6 @@ static int noinline relocate_one_reference(struct btrfs_root *extent_root,
2710 *last_file_root == ref_root) 2713 *last_file_root == ref_root)
2711 goto out; 2714 goto out;
2712 2715
2713 mutex_unlock(&extent_root->fs_info->fs_mutex);
2714 inode = btrfs_iget_locked(extent_root->fs_info->sb, 2716 inode = btrfs_iget_locked(extent_root->fs_info->sb,
2715 ref_objectid, found_root); 2717 ref_objectid, found_root);
2716 if (inode->i_state & I_NEW) { 2718 if (inode->i_state & I_NEW) {
@@ -2727,7 +2729,6 @@ static int noinline relocate_one_reference(struct btrfs_root *extent_root,
2727 * the latest version of the tree root 2729 * the latest version of the tree root
2728 */ 2730 */
2729 if (is_bad_inode(inode)) { 2731 if (is_bad_inode(inode)) {
2730 mutex_lock(&extent_root->fs_info->fs_mutex);
2731 goto out; 2732 goto out;
2732 } 2733 }
2733 *last_file_objectid = inode->i_ino; 2734 *last_file_objectid = inode->i_ino;
@@ -2736,7 +2737,6 @@ static int noinline relocate_one_reference(struct btrfs_root *extent_root,
2736 2737
2737 relocate_inode_pages(inode, ref_offset, extent_key->offset); 2738 relocate_inode_pages(inode, ref_offset, extent_key->offset);
2738 iput(inode); 2739 iput(inode);
2739 mutex_lock(&extent_root->fs_info->fs_mutex);
2740 } else { 2740 } else {
2741 struct btrfs_trans_handle *trans; 2741 struct btrfs_trans_handle *trans;
2742 struct extent_buffer *eb; 2742 struct extent_buffer *eb;
@@ -3033,9 +3033,7 @@ next:
3033 3033
3034 if (progress && need_resched()) { 3034 if (progress && need_resched()) {
3035 memcpy(&key, &found_key, sizeof(key)); 3035 memcpy(&key, &found_key, sizeof(key));
3036 mutex_unlock(&root->fs_info->fs_mutex);
3037 cond_resched(); 3036 cond_resched();
3038 mutex_lock(&root->fs_info->fs_mutex);
3039 btrfs_release_path(root, path); 3037 btrfs_release_path(root, path);
3040 btrfs_search_slot(NULL, root, &key, path, 0, 0); 3038 btrfs_search_slot(NULL, root, &key, path, 0, 0);
3041 progress = 0; 3039 progress = 0;
@@ -3068,9 +3066,7 @@ next:
3068 trans = btrfs_start_transaction(tree_root, 1); 3066 trans = btrfs_start_transaction(tree_root, 1);
3069 btrfs_commit_transaction(trans, tree_root); 3067 btrfs_commit_transaction(trans, tree_root);
3070 3068
3071 mutex_unlock(&root->fs_info->fs_mutex);
3072 btrfs_clean_old_snapshots(tree_root); 3069 btrfs_clean_old_snapshots(tree_root);
3073 mutex_lock(&root->fs_info->fs_mutex);
3074 3070
3075 trans = btrfs_start_transaction(tree_root, 1); 3071 trans = btrfs_start_transaction(tree_root, 1);
3076 btrfs_commit_transaction(trans, tree_root); 3072 btrfs_commit_transaction(trans, tree_root);
diff --git a/fs/btrfs/file.c b/fs/btrfs/file.c
index 73c6d085bd90..18bbe108a0e6 100644
--- a/fs/btrfs/file.c
+++ b/fs/btrfs/file.c
@@ -252,7 +252,6 @@ static int noinline dirty_and_release_pages(struct btrfs_trans_handle *trans,
252 end_of_last_block = start_pos + num_bytes - 1; 252 end_of_last_block = start_pos + num_bytes - 1;
253 253
254 lock_extent(io_tree, start_pos, end_of_last_block, GFP_NOFS); 254 lock_extent(io_tree, start_pos, end_of_last_block, GFP_NOFS);
255 mutex_lock(&root->fs_info->fs_mutex);
256 trans = btrfs_start_transaction(root, 1); 255 trans = btrfs_start_transaction(root, 1);
257 if (!trans) { 256 if (!trans) {
258 err = -ENOMEM; 257 err = -ENOMEM;
@@ -341,7 +340,6 @@ static int noinline dirty_and_release_pages(struct btrfs_trans_handle *trans,
341failed: 340failed:
342 err = btrfs_end_transaction(trans, root); 341 err = btrfs_end_transaction(trans, root);
343out_unlock: 342out_unlock:
344 mutex_unlock(&root->fs_info->fs_mutex);
345 unlock_extent(io_tree, start_pos, end_of_last_block, GFP_NOFS); 343 unlock_extent(io_tree, start_pos, end_of_last_block, GFP_NOFS);
346 return err; 344 return err;
347} 345}
@@ -905,9 +903,7 @@ static ssize_t btrfs_file_write(struct file *file, const char __user *buf,
905 WARN_ON(num_pages > nrptrs); 903 WARN_ON(num_pages > nrptrs);
906 memset(pages, 0, sizeof(pages)); 904 memset(pages, 0, sizeof(pages));
907 905
908 mutex_lock(&root->fs_info->fs_mutex);
909 ret = btrfs_check_free_space(root, write_bytes, 0); 906 ret = btrfs_check_free_space(root, write_bytes, 0);
910 mutex_unlock(&root->fs_info->fs_mutex);
911 if (ret) 907 if (ret)
912 goto out; 908 goto out;
913 909
@@ -998,9 +994,9 @@ static int btrfs_sync_file(struct file *file,
998 * check the transaction that last modified this inode 994 * check the transaction that last modified this inode
999 * and see if its already been committed 995 * and see if its already been committed
1000 */ 996 */
1001 mutex_lock(&root->fs_info->fs_mutex);
1002 if (!BTRFS_I(inode)->last_trans) 997 if (!BTRFS_I(inode)->last_trans)
1003 goto out; 998 goto out;
999
1004 mutex_lock(&root->fs_info->trans_mutex); 1000 mutex_lock(&root->fs_info->trans_mutex);
1005 if (BTRFS_I(inode)->last_trans <= 1001 if (BTRFS_I(inode)->last_trans <=
1006 root->fs_info->last_trans_committed) { 1002 root->fs_info->last_trans_committed) {
@@ -1023,7 +1019,6 @@ static int btrfs_sync_file(struct file *file,
1023 } 1019 }
1024 ret = btrfs_commit_transaction(trans, root); 1020 ret = btrfs_commit_transaction(trans, root);
1025out: 1021out:
1026 mutex_unlock(&root->fs_info->fs_mutex);
1027 return ret > 0 ? EIO : ret; 1022 return ret > 0 ? EIO : ret;
1028} 1023}
1029 1024
diff --git a/fs/btrfs/inode-map.c b/fs/btrfs/inode-map.c
index a0925eabdaa2..298346ae1481 100644
--- a/fs/btrfs/inode-map.c
+++ b/fs/btrfs/inode-map.c
@@ -69,6 +69,12 @@ int btrfs_find_free_objectid(struct btrfs_trans_handle *trans,
69 struct btrfs_key search_key; 69 struct btrfs_key search_key;
70 u64 search_start = dirid; 70 u64 search_start = dirid;
71 71
72 mutex_lock(&root->objectid_mutex);
73 if (root->last_inode_alloc) {
74 *objectid = ++root->last_inode_alloc;
75 mutex_unlock(&root->objectid_mutex);
76 return 0;
77 }
72 path = btrfs_alloc_path(); 78 path = btrfs_alloc_path();
73 BUG_ON(!path); 79 BUG_ON(!path);
74 search_start = root->last_inode_alloc; 80 search_start = root->last_inode_alloc;
@@ -124,9 +130,11 @@ found:
124 btrfs_release_path(root, path); 130 btrfs_release_path(root, path);
125 btrfs_free_path(path); 131 btrfs_free_path(path);
126 BUG_ON(*objectid < search_start); 132 BUG_ON(*objectid < search_start);
133 mutex_unlock(&root->objectid_mutex);
127 return 0; 134 return 0;
128error: 135error:
129 btrfs_release_path(root, path); 136 btrfs_release_path(root, path);
130 btrfs_free_path(path); 137 btrfs_free_path(path);
138 mutex_unlock(&root->objectid_mutex);
131 return ret; 139 return ret;
132} 140}
diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c
index 61bd8953a683..b2251e27ac84 100644
--- a/fs/btrfs/inode.c
+++ b/fs/btrfs/inode.c
@@ -79,12 +79,15 @@ static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
79int btrfs_check_free_space(struct btrfs_root *root, u64 num_required, 79int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
80 int for_del) 80 int for_del)
81{ 81{
82 u64 total = btrfs_super_total_bytes(&root->fs_info->super_copy); 82 u64 total;
83 u64 used = btrfs_super_bytes_used(&root->fs_info->super_copy); 83 u64 used;
84 u64 thresh; 84 u64 thresh;
85 unsigned long flags; 85 unsigned long flags;
86 int ret = 0; 86 int ret = 0;
87 87
88 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
89 total = btrfs_super_total_bytes(&root->fs_info->super_copy);
90 used = btrfs_super_bytes_used(&root->fs_info->super_copy);
88 if (for_del) 91 if (for_del)
89 thresh = total * 90; 92 thresh = total * 90;
90 else 93 else
@@ -92,7 +95,6 @@ int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
92 95
93 do_div(thresh, 100); 96 do_div(thresh, 100);
94 97
95 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
96 if (used + root->fs_info->delalloc_bytes + num_required > thresh) 98 if (used + root->fs_info->delalloc_bytes + num_required > thresh)
97 ret = -ENOSPC; 99 ret = -ENOSPC;
98 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags); 100 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
@@ -115,7 +117,6 @@ static int cow_file_range(struct inode *inode, u64 start, u64 end)
115 trans = btrfs_start_transaction(root, 1); 117 trans = btrfs_start_transaction(root, 1);
116 BUG_ON(!trans); 118 BUG_ON(!trans);
117 btrfs_set_trans_block_group(trans, inode); 119 btrfs_set_trans_block_group(trans, inode);
118 mutex_unlock(&root->fs_info->fs_mutex);
119 120
120 num_bytes = (end - start + blocksize) & ~(blocksize - 1); 121 num_bytes = (end - start + blocksize) & ~(blocksize - 1);
121 num_bytes = max(blocksize, num_bytes); 122 num_bytes = max(blocksize, num_bytes);
@@ -160,7 +161,6 @@ static int cow_file_range(struct inode *inode, u64 start, u64 end)
160 btrfs_add_ordered_inode(inode); 161 btrfs_add_ordered_inode(inode);
161 btrfs_update_inode(trans, root, inode); 162 btrfs_update_inode(trans, root, inode);
162out: 163out:
163 mutex_lock(&root->fs_info->fs_mutex);
164 btrfs_end_transaction(trans, root); 164 btrfs_end_transaction(trans, root);
165 return ret; 165 return ret;
166} 166}
@@ -269,14 +269,13 @@ static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
269{ 269{
270 struct btrfs_root *root = BTRFS_I(inode)->root; 270 struct btrfs_root *root = BTRFS_I(inode)->root;
271 int ret; 271 int ret;
272 mutex_lock(&root->fs_info->fs_mutex); 272
273 if (btrfs_test_opt(root, NODATACOW) || 273 if (btrfs_test_opt(root, NODATACOW) ||
274 btrfs_test_flag(inode, NODATACOW)) 274 btrfs_test_flag(inode, NODATACOW))
275 ret = run_delalloc_nocow(inode, start, end); 275 ret = run_delalloc_nocow(inode, start, end);
276 else 276 else
277 ret = cow_file_range(inode, start, end); 277 ret = cow_file_range(inode, start, end);
278 278
279 mutex_unlock(&root->fs_info->fs_mutex);
280 return ret; 279 return ret;
281} 280}
282 281
@@ -349,17 +348,13 @@ int __btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
349 ret = btrfs_csum_one_bio(root, bio, &sums); 348 ret = btrfs_csum_one_bio(root, bio, &sums);
350 BUG_ON(ret); 349 BUG_ON(ret);
351 350
352 mutex_lock(&root->fs_info->fs_mutex);
353 trans = btrfs_start_transaction(root, 1); 351 trans = btrfs_start_transaction(root, 1);
354 mutex_unlock(&root->fs_info->fs_mutex);
355 352
356 btrfs_set_trans_block_group(trans, inode); 353 btrfs_set_trans_block_group(trans, inode);
357 btrfs_csum_file_blocks(trans, root, inode, bio, sums); 354 btrfs_csum_file_blocks(trans, root, inode, bio, sums);
358 355
359 mutex_lock(&root->fs_info->fs_mutex);
360 ret = btrfs_end_transaction(trans, root); 356 ret = btrfs_end_transaction(trans, root);
361 BUG_ON(ret); 357 BUG_ON(ret);
362 mutex_unlock(&root->fs_info->fs_mutex);
363 358
364 kfree(sums); 359 kfree(sums);
365 360
@@ -404,7 +399,6 @@ int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
404 btrfs_test_flag(inode, NODATASUM)) 399 btrfs_test_flag(inode, NODATASUM))
405 return 0; 400 return 0;
406 401
407 mutex_lock(&root->fs_info->fs_mutex);
408 path = btrfs_alloc_path(); 402 path = btrfs_alloc_path();
409 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0); 403 item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
410 if (IS_ERR(item)) { 404 if (IS_ERR(item)) {
@@ -422,7 +416,6 @@ int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
422out: 416out:
423 if (path) 417 if (path)
424 btrfs_free_path(path); 418 btrfs_free_path(path);
425 mutex_unlock(&root->fs_info->fs_mutex);
426 return ret; 419 return ret;
427} 420}
428 421
@@ -616,7 +609,6 @@ void btrfs_read_locked_inode(struct inode *inode)
616 609
617 path = btrfs_alloc_path(); 610 path = btrfs_alloc_path();
618 BUG_ON(!path); 611 BUG_ON(!path);
619 mutex_lock(&root->fs_info->fs_mutex);
620 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location)); 612 memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
621 613
622 ret = btrfs_lookup_inode(NULL, root, path, &location, 0); 614 ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
@@ -662,8 +654,6 @@ void btrfs_read_locked_inode(struct inode *inode)
662 btrfs_free_path(path); 654 btrfs_free_path(path);
663 inode_item = NULL; 655 inode_item = NULL;
664 656
665 mutex_unlock(&root->fs_info->fs_mutex);
666
667 switch (inode->i_mode & S_IFMT) { 657 switch (inode->i_mode & S_IFMT) {
668 case S_IFREG: 658 case S_IFREG:
669 inode->i_mapping->a_ops = &btrfs_aops; 659 inode->i_mapping->a_ops = &btrfs_aops;
@@ -691,9 +681,7 @@ void btrfs_read_locked_inode(struct inode *inode)
691 return; 681 return;
692 682
693make_bad: 683make_bad:
694 btrfs_release_path(root, path);
695 btrfs_free_path(path); 684 btrfs_free_path(path);
696 mutex_unlock(&root->fs_info->fs_mutex);
697 make_bad_inode(inode); 685 make_bad_inode(inode);
698} 686}
699 687
@@ -758,7 +746,6 @@ int btrfs_update_inode(struct btrfs_trans_handle *trans,
758 btrfs_set_inode_last_trans(trans, inode); 746 btrfs_set_inode_last_trans(trans, inode);
759 ret = 0; 747 ret = 0;
760failed: 748failed:
761 btrfs_release_path(root, path);
762 btrfs_free_path(path); 749 btrfs_free_path(path);
763 return ret; 750 return ret;
764} 751}
@@ -849,7 +836,6 @@ static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
849 unsigned long nr = 0; 836 unsigned long nr = 0;
850 837
851 root = BTRFS_I(dir)->root; 838 root = BTRFS_I(dir)->root;
852 mutex_lock(&root->fs_info->fs_mutex);
853 839
854 ret = btrfs_check_free_space(root, 1, 1); 840 ret = btrfs_check_free_space(root, 1, 1);
855 if (ret) 841 if (ret)
@@ -871,7 +857,6 @@ static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
871 857
872 btrfs_end_transaction(trans, root); 858 btrfs_end_transaction(trans, root);
873fail: 859fail:
874 mutex_unlock(&root->fs_info->fs_mutex);
875 btrfs_btree_balance_dirty(root, nr); 860 btrfs_btree_balance_dirty(root, nr);
876 btrfs_throttle(root); 861 btrfs_throttle(root);
877 return ret; 862 return ret;
@@ -890,7 +875,6 @@ static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
890 return -ENOTEMPTY; 875 return -ENOTEMPTY;
891 } 876 }
892 877
893 mutex_lock(&root->fs_info->fs_mutex);
894 ret = btrfs_check_free_space(root, 1, 1); 878 ret = btrfs_check_free_space(root, 1, 1);
895 if (ret) 879 if (ret)
896 goto fail; 880 goto fail;
@@ -907,7 +891,6 @@ static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
907 nr = trans->blocks_used; 891 nr = trans->blocks_used;
908 ret = btrfs_end_transaction(trans, root); 892 ret = btrfs_end_transaction(trans, root);
909fail: 893fail:
910 mutex_unlock(&root->fs_info->fs_mutex);
911 btrfs_btree_balance_dirty(root, nr); 894 btrfs_btree_balance_dirty(root, nr);
912 btrfs_throttle(root); 895 btrfs_throttle(root);
913 896
@@ -1129,7 +1112,6 @@ error:
1129 ret = btrfs_del_items(trans, root, path, pending_del_slot, 1112 ret = btrfs_del_items(trans, root, path, pending_del_slot,
1130 pending_del_nr); 1113 pending_del_nr);
1131 } 1114 }
1132 btrfs_release_path(root, path);
1133 btrfs_free_path(path); 1115 btrfs_free_path(path);
1134 inode->i_sb->s_dirt = 1; 1116 inode->i_sb->s_dirt = 1;
1135 return ret; 1117 return ret;
@@ -1234,9 +1216,7 @@ static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1234 if (attr->ia_size <= hole_start) 1216 if (attr->ia_size <= hole_start)
1235 goto out; 1217 goto out;
1236 1218
1237 mutex_lock(&root->fs_info->fs_mutex);
1238 err = btrfs_check_free_space(root, 1, 0); 1219 err = btrfs_check_free_space(root, 1, 0);
1239 mutex_unlock(&root->fs_info->fs_mutex);
1240 if (err) 1220 if (err)
1241 goto fail; 1221 goto fail;
1242 1222
@@ -1245,7 +1225,6 @@ static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1245 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS); 1225 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
1246 hole_size = block_end - hole_start; 1226 hole_size = block_end - hole_start;
1247 1227
1248 mutex_lock(&root->fs_info->fs_mutex);
1249 trans = btrfs_start_transaction(root, 1); 1228 trans = btrfs_start_transaction(root, 1);
1250 btrfs_set_trans_block_group(trans, inode); 1229 btrfs_set_trans_block_group(trans, inode);
1251 err = btrfs_drop_extents(trans, root, inode, 1230 err = btrfs_drop_extents(trans, root, inode,
@@ -1262,7 +1241,6 @@ static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1262 btrfs_check_file(root, inode); 1241 btrfs_check_file(root, inode);
1263 } 1242 }
1264 btrfs_end_transaction(trans, root); 1243 btrfs_end_transaction(trans, root);
1265 mutex_unlock(&root->fs_info->fs_mutex);
1266 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS); 1244 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
1267 if (err) 1245 if (err)
1268 return err; 1246 return err;
@@ -1286,7 +1264,6 @@ void btrfs_delete_inode(struct inode *inode)
1286 } 1264 }
1287 1265
1288 inode->i_size = 0; 1266 inode->i_size = 0;
1289 mutex_lock(&root->fs_info->fs_mutex);
1290 trans = btrfs_start_transaction(root, 1); 1267 trans = btrfs_start_transaction(root, 1);
1291 1268
1292 btrfs_set_trans_block_group(trans, inode); 1269 btrfs_set_trans_block_group(trans, inode);
@@ -1298,7 +1275,6 @@ void btrfs_delete_inode(struct inode *inode)
1298 clear_inode(inode); 1275 clear_inode(inode);
1299 1276
1300 btrfs_end_transaction(trans, root); 1277 btrfs_end_transaction(trans, root);
1301 mutex_unlock(&root->fs_info->fs_mutex);
1302 btrfs_btree_balance_dirty(root, nr); 1278 btrfs_btree_balance_dirty(root, nr);
1303 btrfs_throttle(root); 1279 btrfs_throttle(root);
1304 return; 1280 return;
@@ -1306,7 +1282,6 @@ void btrfs_delete_inode(struct inode *inode)
1306no_delete_lock: 1282no_delete_lock:
1307 nr = trans->blocks_used; 1283 nr = trans->blocks_used;
1308 btrfs_end_transaction(trans, root); 1284 btrfs_end_transaction(trans, root);
1309 mutex_unlock(&root->fs_info->fs_mutex);
1310 btrfs_btree_balance_dirty(root, nr); 1285 btrfs_btree_balance_dirty(root, nr);
1311 btrfs_throttle(root); 1286 btrfs_throttle(root);
1312no_delete: 1287no_delete:
@@ -1402,7 +1377,6 @@ static int fixup_tree_root_location(struct btrfs_root *root,
1402 1377
1403 path = btrfs_alloc_path(); 1378 path = btrfs_alloc_path();
1404 BUG_ON(!path); 1379 BUG_ON(!path);
1405 mutex_lock(&root->fs_info->fs_mutex);
1406 1380
1407 *sub_root = btrfs_read_fs_root(root->fs_info, location, 1381 *sub_root = btrfs_read_fs_root(root->fs_info, location,
1408 dentry->d_name.name, 1382 dentry->d_name.name,
@@ -1416,7 +1390,6 @@ static int fixup_tree_root_location(struct btrfs_root *root,
1416 location->offset = 0; 1390 location->offset = 0;
1417 1391
1418 btrfs_free_path(path); 1392 btrfs_free_path(path);
1419 mutex_unlock(&root->fs_info->fs_mutex);
1420 return 0; 1393 return 0;
1421} 1394}
1422 1395
@@ -1482,9 +1455,7 @@ static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1482 if (dentry->d_name.len > BTRFS_NAME_LEN) 1455 if (dentry->d_name.len > BTRFS_NAME_LEN)
1483 return ERR_PTR(-ENAMETOOLONG); 1456 return ERR_PTR(-ENAMETOOLONG);
1484 1457
1485 mutex_lock(&root->fs_info->fs_mutex);
1486 ret = btrfs_inode_by_name(dir, dentry, &location); 1458 ret = btrfs_inode_by_name(dir, dentry, &location);
1487 mutex_unlock(&root->fs_info->fs_mutex);
1488 1459
1489 if (ret < 0) 1460 if (ret < 0)
1490 return ERR_PTR(ret); 1461 return ERR_PTR(ret);
@@ -1559,7 +1530,6 @@ static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1559 filp->f_pos = 1; 1530 filp->f_pos = 1;
1560 } 1531 }
1561 1532
1562 mutex_lock(&root->fs_info->fs_mutex);
1563 key.objectid = inode->i_ino; 1533 key.objectid = inode->i_ino;
1564 path = btrfs_alloc_path(); 1534 path = btrfs_alloc_path();
1565 path->reada = 2; 1535 path->reada = 2;
@@ -1668,9 +1638,7 @@ read_dir_items:
1668nopos: 1638nopos:
1669 ret = 0; 1639 ret = 0;
1670err: 1640err:
1671 btrfs_release_path(root, path);
1672 btrfs_free_path(path); 1641 btrfs_free_path(path);
1673 mutex_unlock(&root->fs_info->fs_mutex);
1674 return ret; 1642 return ret;
1675} 1643}
1676 1644
@@ -1681,11 +1649,9 @@ int btrfs_write_inode(struct inode *inode, int wait)
1681 int ret = 0; 1649 int ret = 0;
1682 1650
1683 if (wait) { 1651 if (wait) {
1684 mutex_lock(&root->fs_info->fs_mutex);
1685 trans = btrfs_start_transaction(root, 1); 1652 trans = btrfs_start_transaction(root, 1);
1686 btrfs_set_trans_block_group(trans, inode); 1653 btrfs_set_trans_block_group(trans, inode);
1687 ret = btrfs_commit_transaction(trans, root); 1654 ret = btrfs_commit_transaction(trans, root);
1688 mutex_unlock(&root->fs_info->fs_mutex);
1689 } 1655 }
1690 return ret; 1656 return ret;
1691} 1657}
@@ -1701,12 +1667,10 @@ void btrfs_dirty_inode(struct inode *inode)
1701 struct btrfs_root *root = BTRFS_I(inode)->root; 1667 struct btrfs_root *root = BTRFS_I(inode)->root;
1702 struct btrfs_trans_handle *trans; 1668 struct btrfs_trans_handle *trans;
1703 1669
1704 mutex_lock(&root->fs_info->fs_mutex);
1705 trans = btrfs_start_transaction(root, 1); 1670 trans = btrfs_start_transaction(root, 1);
1706 btrfs_set_trans_block_group(trans, inode); 1671 btrfs_set_trans_block_group(trans, inode);
1707 btrfs_update_inode(trans, root, inode); 1672 btrfs_update_inode(trans, root, inode);
1708 btrfs_end_transaction(trans, root); 1673 btrfs_end_transaction(trans, root);
1709 mutex_unlock(&root->fs_info->fs_mutex);
1710} 1674}
1711 1675
1712static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans, 1676static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
@@ -1874,7 +1838,6 @@ static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1874 if (!new_valid_dev(rdev)) 1838 if (!new_valid_dev(rdev))
1875 return -EINVAL; 1839 return -EINVAL;
1876 1840
1877 mutex_lock(&root->fs_info->fs_mutex);
1878 err = btrfs_check_free_space(root, 1, 0); 1841 err = btrfs_check_free_space(root, 1, 0);
1879 if (err) 1842 if (err)
1880 goto fail; 1843 goto fail;
@@ -1912,8 +1875,6 @@ out_unlock:
1912 nr = trans->blocks_used; 1875 nr = trans->blocks_used;
1913 btrfs_end_transaction(trans, root); 1876 btrfs_end_transaction(trans, root);
1914fail: 1877fail:
1915 mutex_unlock(&root->fs_info->fs_mutex);
1916
1917 if (drop_inode) { 1878 if (drop_inode) {
1918 inode_dec_link_count(inode); 1879 inode_dec_link_count(inode);
1919 iput(inode); 1880 iput(inode);
@@ -1934,7 +1895,6 @@ static int btrfs_create(struct inode *dir, struct dentry *dentry,
1934 unsigned long nr = 0; 1895 unsigned long nr = 0;
1935 u64 objectid; 1896 u64 objectid;
1936 1897
1937 mutex_lock(&root->fs_info->fs_mutex);
1938 err = btrfs_check_free_space(root, 1, 0); 1898 err = btrfs_check_free_space(root, 1, 0);
1939 if (err) 1899 if (err)
1940 goto fail; 1900 goto fail;
@@ -1980,8 +1940,6 @@ out_unlock:
1980 nr = trans->blocks_used; 1940 nr = trans->blocks_used;
1981 btrfs_end_transaction(trans, root); 1941 btrfs_end_transaction(trans, root);
1982fail: 1942fail:
1983 mutex_unlock(&root->fs_info->fs_mutex);
1984
1985 if (drop_inode) { 1943 if (drop_inode) {
1986 inode_dec_link_count(inode); 1944 inode_dec_link_count(inode);
1987 iput(inode); 1945 iput(inode);
@@ -2009,7 +1967,6 @@ static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
2009#else 1967#else
2010 inc_nlink(inode); 1968 inc_nlink(inode);
2011#endif 1969#endif
2012 mutex_lock(&root->fs_info->fs_mutex);
2013 err = btrfs_check_free_space(root, 1, 0); 1970 err = btrfs_check_free_space(root, 1, 0);
2014 if (err) 1971 if (err)
2015 goto fail; 1972 goto fail;
@@ -2032,8 +1989,6 @@ static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
2032 nr = trans->blocks_used; 1989 nr = trans->blocks_used;
2033 btrfs_end_transaction(trans, root); 1990 btrfs_end_transaction(trans, root);
2034fail: 1991fail:
2035 mutex_unlock(&root->fs_info->fs_mutex);
2036
2037 if (drop_inode) { 1992 if (drop_inode) {
2038 inode_dec_link_count(inode); 1993 inode_dec_link_count(inode);
2039 iput(inode); 1994 iput(inode);
@@ -2053,7 +2008,6 @@ static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2053 u64 objectid = 0; 2008 u64 objectid = 0;
2054 unsigned long nr = 1; 2009 unsigned long nr = 1;
2055 2010
2056 mutex_lock(&root->fs_info->fs_mutex);
2057 err = btrfs_check_free_space(root, 1, 0); 2011 err = btrfs_check_free_space(root, 1, 0);
2058 if (err) 2012 if (err)
2059 goto out_unlock; 2013 goto out_unlock;
@@ -2106,7 +2060,6 @@ out_fail:
2106 btrfs_end_transaction(trans, root); 2060 btrfs_end_transaction(trans, root);
2107 2061
2108out_unlock: 2062out_unlock:
2109 mutex_unlock(&root->fs_info->fs_mutex);
2110 if (drop_on_err) 2063 if (drop_on_err)
2111 iput(inode); 2064 iput(inode);
2112 btrfs_btree_balance_dirty(root, nr); 2065 btrfs_btree_balance_dirty(root, nr);
@@ -2199,7 +2152,6 @@ struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
2199 2152
2200 path = btrfs_alloc_path(); 2153 path = btrfs_alloc_path();
2201 BUG_ON(!path); 2154 BUG_ON(!path);
2202 mutex_lock(&root->fs_info->fs_mutex);
2203 2155
2204again: 2156again:
2205 spin_lock(&em_tree->lock); 2157 spin_lock(&em_tree->lock);
@@ -2402,7 +2354,6 @@ out:
2402 if (!err) 2354 if (!err)
2403 err = ret; 2355 err = ret;
2404 } 2356 }
2405 mutex_unlock(&root->fs_info->fs_mutex);
2406 if (err) { 2357 if (err) {
2407 free_extent_map(em); 2358 free_extent_map(em);
2408 WARN_ON(1); 2359 WARN_ON(1);
@@ -2584,9 +2535,7 @@ int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
2584 int ret; 2535 int ret;
2585 u64 page_start; 2536 u64 page_start;
2586 2537
2587 mutex_lock(&root->fs_info->fs_mutex);
2588 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0); 2538 ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
2589 mutex_unlock(&root->fs_info->fs_mutex);
2590 if (ret) 2539 if (ret)
2591 goto out; 2540 goto out;
2592 2541
@@ -2631,7 +2580,6 @@ static void btrfs_truncate(struct inode *inode)
2631 2580
2632 btrfs_truncate_page(inode->i_mapping, inode->i_size); 2581 btrfs_truncate_page(inode->i_mapping, inode->i_size);
2633 2582
2634 mutex_lock(&root->fs_info->fs_mutex);
2635 trans = btrfs_start_transaction(root, 1); 2583 trans = btrfs_start_transaction(root, 1);
2636 btrfs_set_trans_block_group(trans, inode); 2584 btrfs_set_trans_block_group(trans, inode);
2637 2585
@@ -2643,7 +2591,6 @@ static void btrfs_truncate(struct inode *inode)
2643 2591
2644 ret = btrfs_end_transaction(trans, root); 2592 ret = btrfs_end_transaction(trans, root);
2645 BUG_ON(ret); 2593 BUG_ON(ret);
2646 mutex_unlock(&root->fs_info->fs_mutex);
2647 btrfs_btree_balance_dirty(root, nr); 2594 btrfs_btree_balance_dirty(root, nr);
2648 btrfs_throttle(root); 2595 btrfs_throttle(root);
2649} 2596}
@@ -2827,7 +2774,6 @@ static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2827 struct inode *new_inode = new_dentry->d_inode; 2774 struct inode *new_inode = new_dentry->d_inode;
2828 struct inode *old_inode = old_dentry->d_inode; 2775 struct inode *old_inode = old_dentry->d_inode;
2829 struct timespec ctime = CURRENT_TIME; 2776 struct timespec ctime = CURRENT_TIME;
2830 struct btrfs_path *path;
2831 int ret; 2777 int ret;
2832 2778
2833 if (S_ISDIR(old_inode->i_mode) && new_inode && 2779 if (S_ISDIR(old_inode->i_mode) && new_inode &&
@@ -2835,7 +2781,6 @@ static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2835 return -ENOTEMPTY; 2781 return -ENOTEMPTY;
2836 } 2782 }
2837 2783
2838 mutex_lock(&root->fs_info->fs_mutex);
2839 ret = btrfs_check_free_space(root, 1, 0); 2784 ret = btrfs_check_free_space(root, 1, 0);
2840 if (ret) 2785 if (ret)
2841 goto out_unlock; 2786 goto out_unlock;
@@ -2843,11 +2788,6 @@ static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2843 trans = btrfs_start_transaction(root, 1); 2788 trans = btrfs_start_transaction(root, 1);
2844 2789
2845 btrfs_set_trans_block_group(trans, new_dir); 2790 btrfs_set_trans_block_group(trans, new_dir);
2846 path = btrfs_alloc_path();
2847 if (!path) {
2848 ret = -ENOMEM;
2849 goto out_fail;
2850 }
2851 2791
2852 old_dentry->d_inode->i_nlink++; 2792 old_dentry->d_inode->i_nlink++;
2853 old_dir->i_ctime = old_dir->i_mtime = ctime; 2793 old_dir->i_ctime = old_dir->i_mtime = ctime;
@@ -2869,10 +2809,8 @@ static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2869 goto out_fail; 2809 goto out_fail;
2870 2810
2871out_fail: 2811out_fail:
2872 btrfs_free_path(path);
2873 btrfs_end_transaction(trans, root); 2812 btrfs_end_transaction(trans, root);
2874out_unlock: 2813out_unlock:
2875 mutex_unlock(&root->fs_info->fs_mutex);
2876 return ret; 2814 return ret;
2877} 2815}
2878 2816
@@ -2898,7 +2836,6 @@ static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2898 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root)) 2836 if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2899 return -ENAMETOOLONG; 2837 return -ENAMETOOLONG;
2900 2838
2901 mutex_lock(&root->fs_info->fs_mutex);
2902 err = btrfs_check_free_space(root, 1, 0); 2839 err = btrfs_check_free_space(root, 1, 0);
2903 if (err) 2840 if (err)
2904 goto out_fail; 2841 goto out_fail;
@@ -2979,7 +2916,6 @@ out_unlock:
2979 nr = trans->blocks_used; 2916 nr = trans->blocks_used;
2980 btrfs_end_transaction(trans, root); 2917 btrfs_end_transaction(trans, root);
2981out_fail: 2918out_fail:
2982 mutex_unlock(&root->fs_info->fs_mutex);
2983 if (drop_inode) { 2919 if (drop_inode) {
2984 inode_dec_link_count(inode); 2920 inode_dec_link_count(inode);
2985 iput(inode); 2921 iput(inode);
diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c
index 3fbf74e93dba..6002eb64daf9 100644
--- a/fs/btrfs/ioctl.c
+++ b/fs/btrfs/ioctl.c
@@ -63,7 +63,6 @@ static noinline int create_subvol(struct btrfs_root *root, char *name,
63 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID; 63 u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
64 unsigned long nr = 1; 64 unsigned long nr = 1;
65 65
66 mutex_lock(&root->fs_info->fs_mutex);
67 ret = btrfs_check_free_space(root, 1, 0); 66 ret = btrfs_check_free_space(root, 1, 0);
68 if (ret) 67 if (ret)
69 goto fail_commit; 68 goto fail_commit;
@@ -164,7 +163,6 @@ fail:
164 if (err && !ret) 163 if (err && !ret)
165 ret = err; 164 ret = err;
166fail_commit: 165fail_commit:
167 mutex_unlock(&root->fs_info->fs_mutex);
168 btrfs_btree_balance_dirty(root, nr); 166 btrfs_btree_balance_dirty(root, nr);
169 btrfs_throttle(root); 167 btrfs_throttle(root);
170 return ret; 168 return ret;
@@ -181,7 +179,6 @@ static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
181 if (!root->ref_cows) 179 if (!root->ref_cows)
182 return -EINVAL; 180 return -EINVAL;
183 181
184 mutex_lock(&root->fs_info->fs_mutex);
185 ret = btrfs_check_free_space(root, 1, 0); 182 ret = btrfs_check_free_space(root, 1, 0);
186 if (ret) 183 if (ret)
187 goto fail_unlock; 184 goto fail_unlock;
@@ -208,7 +205,6 @@ static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
208 err = btrfs_commit_transaction(trans, root); 205 err = btrfs_commit_transaction(trans, root);
209 206
210fail_unlock: 207fail_unlock:
211 mutex_unlock(&root->fs_info->fs_mutex);
212 btrfs_btree_balance_dirty(root, nr); 208 btrfs_btree_balance_dirty(root, nr);
213 btrfs_throttle(root); 209 btrfs_throttle(root);
214 return ret; 210 return ret;
@@ -228,9 +224,7 @@ int btrfs_defrag_file(struct file *file)
228 unsigned long i; 224 unsigned long i;
229 int ret; 225 int ret;
230 226
231 mutex_lock(&root->fs_info->fs_mutex);
232 ret = btrfs_check_free_space(root, inode->i_size, 0); 227 ret = btrfs_check_free_space(root, inode->i_size, 0);
233 mutex_unlock(&root->fs_info->fs_mutex);
234 if (ret) 228 if (ret)
235 return -ENOSPC; 229 return -ENOSPC;
236 230
@@ -315,7 +309,8 @@ static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
315 goto out; 309 goto out;
316 } 310 }
317 311
318 mutex_lock(&root->fs_info->fs_mutex); 312 mutex_lock(&root->fs_info->alloc_mutex);
313 mutex_lock(&root->fs_info->chunk_mutex);
319 sizestr = vol_args->name; 314 sizestr = vol_args->name;
320 devstr = strchr(sizestr, ':'); 315 devstr = strchr(sizestr, ':');
321 if (devstr) { 316 if (devstr) {
@@ -385,7 +380,8 @@ static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
385 } 380 }
386 381
387out_unlock: 382out_unlock:
388 mutex_unlock(&root->fs_info->fs_mutex); 383 mutex_lock(&root->fs_info->alloc_mutex);
384 mutex_lock(&root->fs_info->chunk_mutex);
389out: 385out:
390 kfree(vol_args); 386 kfree(vol_args);
391 return ret; 387 return ret;
@@ -428,11 +424,9 @@ static noinline int btrfs_ioctl_snap_create(struct btrfs_root *root,
428 } 424 }
429 425
430 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino, 426 root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
431 mutex_lock(&root->fs_info->fs_mutex);
432 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root, 427 di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
433 path, root_dirid, 428 path, root_dirid,
434 vol_args->name, namelen, 0); 429 vol_args->name, namelen, 0);
435 mutex_unlock(&root->fs_info->fs_mutex);
436 btrfs_free_path(path); 430 btrfs_free_path(path);
437 431
438 if (di && !IS_ERR(di)) { 432 if (di && !IS_ERR(di)) {
@@ -445,10 +439,12 @@ static noinline int btrfs_ioctl_snap_create(struct btrfs_root *root,
445 goto out; 439 goto out;
446 } 440 }
447 441
442 mutex_lock(&root->fs_info->drop_mutex);
448 if (root == root->fs_info->tree_root) 443 if (root == root->fs_info->tree_root)
449 ret = create_subvol(root, vol_args->name, namelen); 444 ret = create_subvol(root, vol_args->name, namelen);
450 else 445 else
451 ret = create_snapshot(root, vol_args->name, namelen); 446 ret = create_snapshot(root, vol_args->name, namelen);
447 mutex_unlock(&root->fs_info->drop_mutex);
452out: 448out:
453 kfree(vol_args); 449 kfree(vol_args);
454 return ret; 450 return ret;
@@ -461,10 +457,8 @@ static int btrfs_ioctl_defrag(struct file *file)
461 457
462 switch (inode->i_mode & S_IFMT) { 458 switch (inode->i_mode & S_IFMT) {
463 case S_IFDIR: 459 case S_IFDIR:
464 mutex_lock(&root->fs_info->fs_mutex);
465 btrfs_defrag_root(root, 0); 460 btrfs_defrag_root(root, 0);
466 btrfs_defrag_root(root->fs_info->extent_root, 0); 461 btrfs_defrag_root(root->fs_info->extent_root, 0);
467 mutex_unlock(&root->fs_info->fs_mutex);
468 break; 462 break;
469 case S_IFREG: 463 case S_IFREG:
470 btrfs_defrag_file(file); 464 btrfs_defrag_file(file);
@@ -588,7 +582,6 @@ long btrfs_ioctl_clone(struct file *file, unsigned long src_fd)
588 unlock_extent(&BTRFS_I(src)->io_tree, 0, (u64)-1, GFP_NOFS); 582 unlock_extent(&BTRFS_I(src)->io_tree, 0, (u64)-1, GFP_NOFS);
589 } 583 }
590 584
591 mutex_lock(&root->fs_info->fs_mutex);
592 trans = btrfs_start_transaction(root, 0); 585 trans = btrfs_start_transaction(root, 0);
593 path = btrfs_alloc_path(); 586 path = btrfs_alloc_path();
594 if (!path) { 587 if (!path) {
@@ -685,7 +678,6 @@ out:
685 unlock_extent(&BTRFS_I(src)->io_tree, 0, (u64)-1, GFP_NOFS); 678 unlock_extent(&BTRFS_I(src)->io_tree, 0, (u64)-1, GFP_NOFS);
686 679
687 btrfs_end_transaction(trans, root); 680 btrfs_end_transaction(trans, root);
688 mutex_unlock(&root->fs_info->fs_mutex);
689 681
690out_unlock: 682out_unlock:
691 mutex_unlock(&src->i_mutex); 683 mutex_unlock(&src->i_mutex);
@@ -711,7 +703,6 @@ long btrfs_ioctl_trans_start(struct file *file)
711 if (!capable(CAP_SYS_ADMIN)) 703 if (!capable(CAP_SYS_ADMIN))
712 return -EPERM; 704 return -EPERM;
713 705
714 mutex_lock(&root->fs_info->fs_mutex);
715 if (file->private_data) { 706 if (file->private_data) {
716 ret = -EINPROGRESS; 707 ret = -EINPROGRESS;
717 goto out; 708 goto out;
@@ -723,7 +714,6 @@ long btrfs_ioctl_trans_start(struct file *file)
723 ret = -ENOMEM; 714 ret = -ENOMEM;
724 /*printk(KERN_INFO "btrfs_ioctl_trans_start on %p\n", file);*/ 715 /*printk(KERN_INFO "btrfs_ioctl_trans_start on %p\n", file);*/
725out: 716out:
726 mutex_unlock(&root->fs_info->fs_mutex);
727 return ret; 717 return ret;
728} 718}
729 719
@@ -740,7 +730,6 @@ long btrfs_ioctl_trans_end(struct file *file)
740 struct btrfs_trans_handle *trans; 730 struct btrfs_trans_handle *trans;
741 int ret = 0; 731 int ret = 0;
742 732
743 mutex_lock(&root->fs_info->fs_mutex);
744 trans = file->private_data; 733 trans = file->private_data;
745 if (!trans) { 734 if (!trans) {
746 ret = -EINVAL; 735 ret = -EINVAL;
@@ -749,7 +738,6 @@ long btrfs_ioctl_trans_end(struct file *file)
749 btrfs_end_transaction(trans, root); 738 btrfs_end_transaction(trans, root);
750 file->private_data = 0; 739 file->private_data = 0;
751out: 740out:
752 mutex_unlock(&root->fs_info->fs_mutex);
753 return ret; 741 return ret;
754} 742}
755 743
diff --git a/fs/btrfs/super.c b/fs/btrfs/super.c
index 196d0e280b19..b61ded7a20c9 100644
--- a/fs/btrfs/super.c
+++ b/fs/btrfs/super.c
@@ -366,12 +366,10 @@ int btrfs_sync_fs(struct super_block *sb, int wait)
366 return 0; 366 return 0;
367 } 367 }
368 btrfs_clean_old_snapshots(root); 368 btrfs_clean_old_snapshots(root);
369 mutex_lock(&root->fs_info->fs_mutex);
370 btrfs_defrag_dirty_roots(root->fs_info); 369 btrfs_defrag_dirty_roots(root->fs_info);
371 trans = btrfs_start_transaction(root, 1); 370 trans = btrfs_start_transaction(root, 1);
372 ret = btrfs_commit_transaction(trans, root); 371 ret = btrfs_commit_transaction(trans, root);
373 sb->s_dirt = 0; 372 sb->s_dirt = 0;
374 mutex_unlock(&root->fs_info->fs_mutex);
375 return ret; 373 return ret;
376} 374}
377 375
diff --git a/fs/btrfs/transaction.c b/fs/btrfs/transaction.c
index 1ed433a71493..5a1ee0665ae8 100644
--- a/fs/btrfs/transaction.c
+++ b/fs/btrfs/transaction.c
@@ -370,6 +370,7 @@ int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
370 struct btrfs_trans_handle *trans; 370 struct btrfs_trans_handle *trans;
371 unsigned long nr; 371 unsigned long nr;
372 372
373 smp_mb();
373 if (root->defrag_running) 374 if (root->defrag_running)
374 return 0; 375 return 0;
375 trans = btrfs_start_transaction(root, 1); 376 trans = btrfs_start_transaction(root, 1);
@@ -378,16 +379,15 @@ int btrfs_defrag_root(struct btrfs_root *root, int cacheonly)
378 ret = btrfs_defrag_leaves(trans, root, cacheonly); 379 ret = btrfs_defrag_leaves(trans, root, cacheonly);
379 nr = trans->blocks_used; 380 nr = trans->blocks_used;
380 btrfs_end_transaction(trans, root); 381 btrfs_end_transaction(trans, root);
381 mutex_unlock(&info->fs_mutex);
382 btrfs_btree_balance_dirty(info->tree_root, nr); 382 btrfs_btree_balance_dirty(info->tree_root, nr);
383 cond_resched(); 383 cond_resched();
384 384
385 mutex_lock(&info->fs_mutex);
386 trans = btrfs_start_transaction(root, 1); 385 trans = btrfs_start_transaction(root, 1);
387 if (ret != -EAGAIN) 386 if (ret != -EAGAIN)
388 break; 387 break;
389 } 388 }
390 root->defrag_running = 0; 389 root->defrag_running = 0;
390 smp_mb();
391 radix_tree_tag_clear(&info->fs_roots_radix, 391 radix_tree_tag_clear(&info->fs_roots_radix,
392 (unsigned long)root->root_key.objectid, 392 (unsigned long)root->root_key.objectid,
393 BTRFS_ROOT_DEFRAG_TAG); 393 BTRFS_ROOT_DEFRAG_TAG);
@@ -435,14 +435,14 @@ static noinline int drop_dirty_roots(struct btrfs_root *tree_root,
435 while(!list_empty(list)) { 435 while(!list_empty(list)) {
436 struct btrfs_root *root; 436 struct btrfs_root *root;
437 437
438 mutex_lock(&tree_root->fs_info->fs_mutex);
439 dirty = list_entry(list->next, struct dirty_root, list); 438 dirty = list_entry(list->next, struct dirty_root, list);
440 list_del_init(&dirty->list); 439 list_del_init(&dirty->list);
441 440
442 num_bytes = btrfs_root_used(&dirty->root->root_item); 441 num_bytes = btrfs_root_used(&dirty->root->root_item);
443 root = dirty->latest_root; 442 root = dirty->latest_root;
444 root->fs_info->throttles++; 443 atomic_inc(&root->fs_info->throttles);
445 444
445 mutex_lock(&root->fs_info->drop_mutex);
446 while(1) { 446 while(1) {
447 trans = btrfs_start_transaction(tree_root, 1); 447 trans = btrfs_start_transaction(tree_root, 1);
448 ret = btrfs_drop_snapshot(trans, dirty->root); 448 ret = btrfs_drop_snapshot(trans, dirty->root);
@@ -459,14 +459,16 @@ static noinline int drop_dirty_roots(struct btrfs_root *tree_root,
459 nr = trans->blocks_used; 459 nr = trans->blocks_used;
460 ret = btrfs_end_transaction(trans, tree_root); 460 ret = btrfs_end_transaction(trans, tree_root);
461 BUG_ON(ret); 461 BUG_ON(ret);
462 mutex_unlock(&tree_root->fs_info->fs_mutex); 462
463 mutex_unlock(&root->fs_info->drop_mutex);
463 btrfs_btree_balance_dirty(tree_root, nr); 464 btrfs_btree_balance_dirty(tree_root, nr);
464 cond_resched(); 465 cond_resched();
465 mutex_lock(&tree_root->fs_info->fs_mutex); 466 mutex_lock(&root->fs_info->drop_mutex);
466 } 467 }
467 BUG_ON(ret); 468 BUG_ON(ret);
468 root->fs_info->throttles--; 469 atomic_dec(&root->fs_info->throttles);
469 470
471 mutex_lock(&root->fs_info->alloc_mutex);
470 num_bytes -= btrfs_root_used(&dirty->root->root_item); 472 num_bytes -= btrfs_root_used(&dirty->root->root_item);
471 bytes_used = btrfs_root_used(&root->root_item); 473 bytes_used = btrfs_root_used(&root->root_item);
472 if (num_bytes) { 474 if (num_bytes) {
@@ -474,11 +476,15 @@ static noinline int drop_dirty_roots(struct btrfs_root *tree_root,
474 btrfs_set_root_used(&root->root_item, 476 btrfs_set_root_used(&root->root_item,
475 bytes_used - num_bytes); 477 bytes_used - num_bytes);
476 } 478 }
479 mutex_unlock(&root->fs_info->alloc_mutex);
480
477 ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key); 481 ret = btrfs_del_root(trans, tree_root, &dirty->root->root_key);
478 if (ret) { 482 if (ret) {
479 BUG(); 483 BUG();
480 break; 484 break;
481 } 485 }
486 mutex_unlock(&root->fs_info->drop_mutex);
487
482 nr = trans->blocks_used; 488 nr = trans->blocks_used;
483 ret = btrfs_end_transaction(trans, tree_root); 489 ret = btrfs_end_transaction(trans, tree_root);
484 BUG_ON(ret); 490 BUG_ON(ret);
@@ -486,7 +492,6 @@ static noinline int drop_dirty_roots(struct btrfs_root *tree_root,
486 free_extent_buffer(dirty->root->node); 492 free_extent_buffer(dirty->root->node);
487 kfree(dirty->root); 493 kfree(dirty->root);
488 kfree(dirty); 494 kfree(dirty);
489 mutex_unlock(&tree_root->fs_info->fs_mutex);
490 495
491 btrfs_btree_balance_dirty(tree_root, nr); 496 btrfs_btree_balance_dirty(tree_root, nr);
492 cond_resched(); 497 cond_resched();
@@ -503,7 +508,7 @@ int btrfs_write_ordered_inodes(struct btrfs_trans_handle *trans,
503 u64 objectid = 0; 508 u64 objectid = 0;
504 int ret; 509 int ret;
505 510
506 root->fs_info->throttles++; 511 atomic_inc(&root->fs_info->throttles);
507 while(1) { 512 while(1) {
508 ret = btrfs_find_first_ordered_inode( 513 ret = btrfs_find_first_ordered_inode(
509 &cur_trans->ordered_inode_tree, 514 &cur_trans->ordered_inode_tree,
@@ -512,7 +517,6 @@ int btrfs_write_ordered_inodes(struct btrfs_trans_handle *trans,
512 break; 517 break;
513 518
514 mutex_unlock(&root->fs_info->trans_mutex); 519 mutex_unlock(&root->fs_info->trans_mutex);
515 mutex_unlock(&root->fs_info->fs_mutex);
516 520
517 if (S_ISREG(inode->i_mode)) { 521 if (S_ISREG(inode->i_mode)) {
518 atomic_inc(&BTRFS_I(inode)->ordered_writeback); 522 atomic_inc(&BTRFS_I(inode)->ordered_writeback);
@@ -521,7 +525,6 @@ int btrfs_write_ordered_inodes(struct btrfs_trans_handle *trans,
521 } 525 }
522 iput(inode); 526 iput(inode);
523 527
524 mutex_lock(&root->fs_info->fs_mutex);
525 mutex_lock(&root->fs_info->trans_mutex); 528 mutex_lock(&root->fs_info->trans_mutex);
526 } 529 }
527 while(1) { 530 while(1) {
@@ -533,7 +536,6 @@ int btrfs_write_ordered_inodes(struct btrfs_trans_handle *trans,
533 if (!ret) 536 if (!ret)
534 break; 537 break;
535 mutex_unlock(&root->fs_info->trans_mutex); 538 mutex_unlock(&root->fs_info->trans_mutex);
536 mutex_unlock(&root->fs_info->fs_mutex);
537 539
538 if (S_ISREG(inode->i_mode)) { 540 if (S_ISREG(inode->i_mode)) {
539 atomic_inc(&BTRFS_I(inode)->ordered_writeback); 541 atomic_inc(&BTRFS_I(inode)->ordered_writeback);
@@ -543,10 +545,9 @@ int btrfs_write_ordered_inodes(struct btrfs_trans_handle *trans,
543 atomic_dec(&inode->i_count); 545 atomic_dec(&inode->i_count);
544 iput(inode); 546 iput(inode);
545 547
546 mutex_lock(&root->fs_info->fs_mutex);
547 mutex_lock(&root->fs_info->trans_mutex); 548 mutex_lock(&root->fs_info->trans_mutex);
548 } 549 }
549 root->fs_info->throttles--; 550 atomic_dec(&root->fs_info->throttles);
550 return 0; 551 return 0;
551} 552}
552 553
@@ -661,7 +662,6 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
661 mutex_unlock(&root->fs_info->trans_mutex); 662 mutex_unlock(&root->fs_info->trans_mutex);
662 btrfs_end_transaction(trans, root); 663 btrfs_end_transaction(trans, root);
663 664
664 mutex_unlock(&root->fs_info->fs_mutex);
665 ret = wait_for_commit(root, cur_trans); 665 ret = wait_for_commit(root, cur_trans);
666 BUG_ON(ret); 666 BUG_ON(ret);
667 667
@@ -669,7 +669,6 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
669 put_transaction(cur_trans); 669 put_transaction(cur_trans);
670 mutex_unlock(&root->fs_info->trans_mutex); 670 mutex_unlock(&root->fs_info->trans_mutex);
671 671
672 mutex_lock(&root->fs_info->fs_mutex);
673 return 0; 672 return 0;
674 } 673 }
675 674
@@ -687,12 +686,10 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
687 struct btrfs_transaction, list); 686 struct btrfs_transaction, list);
688 if (!prev_trans->commit_done) { 687 if (!prev_trans->commit_done) {
689 prev_trans->use_count++; 688 prev_trans->use_count++;
690 mutex_unlock(&root->fs_info->fs_mutex);
691 mutex_unlock(&root->fs_info->trans_mutex); 689 mutex_unlock(&root->fs_info->trans_mutex);
692 690
693 wait_for_commit(root, prev_trans); 691 wait_for_commit(root, prev_trans);
694 692
695 mutex_lock(&root->fs_info->fs_mutex);
696 mutex_lock(&root->fs_info->trans_mutex); 693 mutex_lock(&root->fs_info->trans_mutex);
697 put_transaction(prev_trans); 694 put_transaction(prev_trans);
698 } 695 }
@@ -709,12 +706,10 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
709 else 706 else
710 timeout = 1; 707 timeout = 1;
711 708
712 mutex_unlock(&root->fs_info->fs_mutex);
713 mutex_unlock(&root->fs_info->trans_mutex); 709 mutex_unlock(&root->fs_info->trans_mutex);
714 710
715 schedule_timeout(timeout); 711 schedule_timeout(timeout);
716 712
717 mutex_lock(&root->fs_info->fs_mutex);
718 mutex_lock(&root->fs_info->trans_mutex); 713 mutex_lock(&root->fs_info->trans_mutex);
719 finish_wait(&cur_trans->writer_wait, &wait); 714 finish_wait(&cur_trans->writer_wait, &wait);
720 ret = btrfs_write_ordered_inodes(trans, root); 715 ret = btrfs_write_ordered_inodes(trans, root);
@@ -755,12 +750,10 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
755 btrfs_copy_pinned(root, pinned_copy); 750 btrfs_copy_pinned(root, pinned_copy);
756 751
757 mutex_unlock(&root->fs_info->trans_mutex); 752 mutex_unlock(&root->fs_info->trans_mutex);
758 mutex_unlock(&root->fs_info->fs_mutex);
759 ret = btrfs_write_and_wait_transaction(trans, root); 753 ret = btrfs_write_and_wait_transaction(trans, root);
760 BUG_ON(ret); 754 BUG_ON(ret);
761 write_ctree_super(trans, root); 755 write_ctree_super(trans, root);
762 756
763 mutex_lock(&root->fs_info->fs_mutex);
764 btrfs_finish_extent_commit(trans, root, pinned_copy); 757 btrfs_finish_extent_commit(trans, root, pinned_copy);
765 mutex_lock(&root->fs_info->trans_mutex); 758 mutex_lock(&root->fs_info->trans_mutex);
766 759
@@ -781,9 +774,7 @@ int btrfs_commit_transaction(struct btrfs_trans_handle *trans,
781 kmem_cache_free(btrfs_trans_handle_cachep, trans); 774 kmem_cache_free(btrfs_trans_handle_cachep, trans);
782 775
783 if (root->fs_info->closing) { 776 if (root->fs_info->closing) {
784 mutex_unlock(&root->fs_info->fs_mutex);
785 drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots); 777 drop_dirty_roots(root->fs_info->tree_root, &dirty_fs_roots);
786 mutex_lock(&root->fs_info->fs_mutex);
787 } 778 }
788 return ret; 779 return ret;
789} 780}
@@ -823,7 +814,7 @@ void btrfs_transaction_cleaner(struct work_struct *work)
823 unsigned long delay = HZ * 30; 814 unsigned long delay = HZ * 30;
824 int ret; 815 int ret;
825 816
826 mutex_lock(&root->fs_info->fs_mutex); 817 smp_mb();
827 if (root->fs_info->closing) 818 if (root->fs_info->closing)
828 goto out; 819 goto out;
829 820
@@ -844,7 +835,6 @@ void btrfs_transaction_cleaner(struct work_struct *work)
844 trans = btrfs_start_transaction(root, 1); 835 trans = btrfs_start_transaction(root, 1);
845 ret = btrfs_commit_transaction(trans, root); 836 ret = btrfs_commit_transaction(trans, root);
846out: 837out:
847 mutex_unlock(&root->fs_info->fs_mutex);
848 btrfs_clean_old_snapshots(root); 838 btrfs_clean_old_snapshots(root);
849 btrfs_transaction_queue_work(root, delay); 839 btrfs_transaction_queue_work(root, delay);
850} 840}
diff --git a/fs/btrfs/volumes.c b/fs/btrfs/volumes.c
index ba3968571024..869864ddcc29 100644
--- a/fs/btrfs/volumes.c
+++ b/fs/btrfs/volumes.c
@@ -866,7 +866,8 @@ int btrfs_rm_device(struct btrfs_root *root, char *device_path)
866 u64 devid; 866 u64 devid;
867 int ret = 0; 867 int ret = 0;
868 868
869 mutex_lock(&root->fs_info->fs_mutex); 869 mutex_lock(&root->fs_info->alloc_mutex);
870 mutex_lock(&root->fs_info->chunk_mutex);
870 mutex_lock(&uuid_mutex); 871 mutex_lock(&uuid_mutex);
871 872
872 all_avail = root->fs_info->avail_data_alloc_bits | 873 all_avail = root->fs_info->avail_data_alloc_bits |
@@ -984,7 +985,8 @@ error_close:
984 close_bdev_excl(bdev); 985 close_bdev_excl(bdev);
985out: 986out:
986 mutex_unlock(&uuid_mutex); 987 mutex_unlock(&uuid_mutex);
987 mutex_unlock(&root->fs_info->fs_mutex); 988 mutex_unlock(&root->fs_info->chunk_mutex);
989 mutex_unlock(&root->fs_info->alloc_mutex);
988 return ret; 990 return ret;
989} 991}
990 992
@@ -1003,7 +1005,10 @@ int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1003 if (!bdev) { 1005 if (!bdev) {
1004 return -EIO; 1006 return -EIO;
1005 } 1007 }
1006 mutex_lock(&root->fs_info->fs_mutex); 1008
1009 mutex_lock(&root->fs_info->alloc_mutex);
1010 mutex_lock(&root->fs_info->chunk_mutex);
1011
1007 trans = btrfs_start_transaction(root, 1); 1012 trans = btrfs_start_transaction(root, 1);
1008 devices = &root->fs_info->fs_devices->devices; 1013 devices = &root->fs_info->fs_devices->devices;
1009 list_for_each(cur, devices) { 1014 list_for_each(cur, devices) {
@@ -1057,7 +1062,9 @@ int btrfs_init_new_device(struct btrfs_root *root, char *device_path)
1057 root->fs_info->fs_devices->open_devices++; 1062 root->fs_info->fs_devices->open_devices++;
1058out: 1063out:
1059 btrfs_end_transaction(trans, root); 1064 btrfs_end_transaction(trans, root);
1060 mutex_unlock(&root->fs_info->fs_mutex); 1065 mutex_unlock(&root->fs_info->chunk_mutex);
1066 mutex_unlock(&root->fs_info->alloc_mutex);
1067
1061 return ret; 1068 return ret;
1062 1069
1063out_close_bdev: 1070out_close_bdev:
@@ -1297,9 +1304,10 @@ int btrfs_balance(struct btrfs_root *dev_root)
1297 struct btrfs_key found_key; 1304 struct btrfs_key found_key;
1298 1305
1299 1306
1307 BUG(); /* FIXME, needs locking */
1308
1300 dev_root = dev_root->fs_info->dev_root; 1309 dev_root = dev_root->fs_info->dev_root;
1301 1310
1302 mutex_lock(&dev_root->fs_info->fs_mutex);
1303 /* step one make some room on all the devices */ 1311 /* step one make some room on all the devices */
1304 list_for_each(cur, devices) { 1312 list_for_each(cur, devices) {
1305 device = list_entry(cur, struct btrfs_device, dev_list); 1313 device = list_entry(cur, struct btrfs_device, dev_list);
@@ -1368,7 +1376,6 @@ int btrfs_balance(struct btrfs_root *dev_root)
1368 ret = 0; 1376 ret = 0;
1369error: 1377error:
1370 btrfs_free_path(path); 1378 btrfs_free_path(path);
1371 mutex_unlock(&dev_root->fs_info->fs_mutex);
1372 return ret; 1379 return ret;
1373} 1380}
1374 1381
diff --git a/fs/btrfs/xattr.c b/fs/btrfs/xattr.c
index f63488dc2f16..3e9eb91c3c89 100644
--- a/fs/btrfs/xattr.c
+++ b/fs/btrfs/xattr.c
@@ -153,7 +153,6 @@ ssize_t btrfs_xattr_get(struct inode *inode, int name_index,
153 return -ENOMEM; 153 return -ENOMEM;
154 } 154 }
155 155
156 mutex_lock(&root->fs_info->fs_mutex);
157 /* lookup the xattr by name */ 156 /* lookup the xattr by name */
158 di = btrfs_lookup_xattr(NULL, root, path, inode->i_ino, name, 157 di = btrfs_lookup_xattr(NULL, root, path, inode->i_ino, name,
159 strlen(name), 0); 158 strlen(name), 0);
@@ -181,7 +180,6 @@ ssize_t btrfs_xattr_get(struct inode *inode, int name_index,
181 ret = btrfs_dir_data_len(leaf, di); 180 ret = btrfs_dir_data_len(leaf, di);
182 181
183out: 182out:
184 mutex_unlock(&root->fs_info->fs_mutex);
185 kfree(name); 183 kfree(name);
186 btrfs_free_path(path); 184 btrfs_free_path(path);
187 return ret; 185 return ret;
@@ -210,7 +208,6 @@ int btrfs_xattr_set(struct inode *inode, int name_index,
210 return -ENOMEM; 208 return -ENOMEM;
211 } 209 }
212 210
213 mutex_lock(&root->fs_info->fs_mutex);
214 trans = btrfs_start_transaction(root, 1); 211 trans = btrfs_start_transaction(root, 1);
215 btrfs_set_trans_block_group(trans, inode); 212 btrfs_set_trans_block_group(trans, inode);
216 213
@@ -260,7 +257,6 @@ out:
260 } 257 }
261 258
262 btrfs_end_transaction(trans, root); 259 btrfs_end_transaction(trans, root);
263 mutex_unlock(&root->fs_info->fs_mutex);
264 kfree(name); 260 kfree(name);
265 btrfs_free_path(path); 261 btrfs_free_path(path);
266 262
@@ -297,8 +293,6 @@ ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size)
297 return -ENOMEM; 293 return -ENOMEM;
298 path->reada = 2; 294 path->reada = 2;
299 295
300 mutex_lock(&root->fs_info->fs_mutex);
301
302 /* search for our xattrs */ 296 /* search for our xattrs */
303 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); 297 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
304 if (ret < 0) 298 if (ret < 0)
@@ -379,15 +373,13 @@ ssize_t btrfs_listxattr(struct dentry *dentry, char *buffer, size_t size)
379 ret = total_size; 373 ret = total_size;
380 374
381err: 375err:
382 mutex_unlock(&root->fs_info->fs_mutex);
383 btrfs_free_path(path); 376 btrfs_free_path(path);
384 377
385 return ret; 378 return ret;
386} 379}
387 380
388/* 381/*
389 * delete all the xattrs associated with the inode. fs_mutex should be 382 * delete all the xattrs associated with the inode.
390 * held when we come into here
391 */ 383 */
392int btrfs_delete_xattrs(struct btrfs_trans_handle *trans, 384int btrfs_delete_xattrs(struct btrfs_trans_handle *trans,
393 struct btrfs_root *root, struct inode *inode) 385 struct btrfs_root *root, struct inode *inode)