Btrfs: make btrfs_search_forward return with nodes unlocked
authorFilipe Manana <fdmanana@suse.com>
Mon, 4 Aug 2014 18:37:21 +0000 (19:37 +0100)
committerChris Mason <clm@fb.com>
Wed, 17 Sep 2014 20:38:02 +0000 (13:38 -0700)
None of the uses of btrfs_search_forward() need to have the path
nodes (level >= 1) read locked, only the leaf needs to be locked
while the caller processes it. Therefore make it return a path
with all nodes unlocked, except for the leaf.

This change is motivated by the observation that during a file
fsync we repeatdly call btrfs_search_forward() and process the
returned leaf while upper nodes of the returned path (level >= 1)
are read locked, which unnecessarily blocks other tasks that want
to write to the same fs/subvol btree.
Therefore instead of modifying the fsync code to unlock all nodes
with level >= 1 immediately after calling btrfs_search_forward(),
change btrfs_search_forward() to do it, so that it benefits all
callers.

Signed-off-by: Filipe Manana <fdmanana@suse.com>
Signed-off-by: Chris Mason <clm@fb.com>
fs/btrfs/ctree.c
fs/btrfs/ioctl.c
fs/btrfs/tree-log.c
fs/btrfs/uuid-tree.c
fs/btrfs/volumes.c

index 783ea3bac7dc19c371479322f33a3e48c2ef0ee4..39021bf2df9a9c0cb702f2ca427c640a02a8c3c9 100644 (file)
@@ -5144,8 +5144,9 @@ int btrfs_search_forward(struct btrfs_root *root, struct btrfs_key *min_key,
        u32 nritems;
        int level;
        int ret = 1;
+       int keep_locks = path->keep_locks;
 
-       WARN_ON(!path->keep_locks);
+       path->keep_locks = 1;
 again:
        cur = btrfs_read_lock_root_node(root);
        level = btrfs_header_level(cur);
@@ -5209,7 +5210,6 @@ find_next_key:
                path->slots[level] = slot;
                if (level == path->lowest_level) {
                        ret = 0;
-                       unlock_up(path, level, 1, 0, NULL);
                        goto out;
                }
                btrfs_set_path_blocking(path);
@@ -5224,9 +5224,12 @@ find_next_key:
                btrfs_clear_path_blocking(path, NULL, 0);
        }
 out:
-       if (ret == 0)
+       path->keep_locks = keep_locks;
+       if (ret == 0) {
+               btrfs_unlock_up_safe(path, path->lowest_level + 1);
+               btrfs_set_path_blocking(path);
                memcpy(min_key, &found_key, sizeof(found_key));
-       btrfs_set_path_blocking(path);
+       }
        return ret;
 }
 
index 4492e017bdb216ae6d58a71e24b8e5d589ec8a97..85ca3cce8554931cd0346dd1e61ac8f8fe28a751 100644 (file)
@@ -936,12 +936,9 @@ static int find_new_extents(struct btrfs_root *root,
        min_key.offset = *off;
 
        while (1) {
-               path->keep_locks = 1;
                ret = btrfs_search_forward(root, &min_key, path, newer_than);
                if (ret != 0)
                        goto none;
-               path->keep_locks = 0;
-               btrfs_unlock_up_safe(path, 1);
 process_slot:
                if (min_key.objectid != ino)
                        goto none;
@@ -2088,8 +2085,6 @@ static noinline int search_ioctl(struct inode *inode,
        key.type = sk->min_type;
        key.offset = sk->min_offset;
 
-       path->keep_locks = 1;
-
        while (1) {
                ret = btrfs_search_forward(root, &key, path, sk->min_transid);
                if (ret != 0) {
index 7b6d1428f033bf3b1465de7682658bde1bedef63..82db14f5cf87838c86738df43a29db89cd7ca3e2 100644 (file)
@@ -2983,8 +2983,6 @@ static noinline int log_dir_items(struct btrfs_trans_handle *trans,
        min_key.type = key_type;
        min_key.offset = min_offset;
 
-       path->keep_locks = 1;
-
        ret = btrfs_search_forward(root, &min_key, path, trans->transid);
 
        /*
@@ -3964,7 +3962,6 @@ static int btrfs_log_inode(struct btrfs_trans_handle *trans,
                err = ret;
                goto out_unlock;
        }
-       path->keep_locks = 1;
 
        while (1) {
                ins_nr = 0;
index f6a4c03ee7d8fc1cf3aedc3f2b8edf0007992ff7..778282944530f6baba8a27037e191d28922421a1 100644 (file)
@@ -279,7 +279,6 @@ int btrfs_uuid_tree_iterate(struct btrfs_fs_info *fs_info,
        key.offset = 0;
 
 again_search_slot:
-       path->keep_locks = 1;
        ret = btrfs_search_forward(root, &key, path, 0);
        if (ret) {
                if (ret > 0)
index 46cd29ad31566abe31bdbd3ea8c8e4d0c8d256b0..ff8386eb3367c05b312d98b100d0e4cc82a33a0f 100644 (file)
@@ -3625,8 +3625,6 @@ static int btrfs_uuid_scan_kthread(void *data)
        max_key.type = BTRFS_ROOT_ITEM_KEY;
        max_key.offset = (u64)-1;
 
-       path->keep_locks = 1;
-
        while (1) {
                ret = btrfs_search_forward(root, &key, path, 0);
                if (ret) {