btrfs: introduce btrfs_lookup_match_dir
authorMarcos Paulo de Souza <mpdesouza@suse.com>
Mon, 26 Jul 2021 19:19:09 +0000 (16:19 -0300)
committerDavid Sterba <dsterba@suse.com>
Mon, 23 Aug 2021 11:19:02 +0000 (13:19 +0200)
btrfs_search_slot is called in multiple places in dir-item.c to search
for a dir entry, and then calling btrfs_match_dir_name to return a
btrfs_dir_item.

In order to reduce the number of callers of btrfs_search_slot, create a
common function that looks for the dir key, and if found call
btrfs_match_dir_item_name.

Signed-off-by: Marcos Paulo de Souza <mpdesouza@suse.com>
Reviewed-by: David Sterba <dsterba@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
fs/btrfs/dir-item.c

index 98b63eb..f1274d5 100644 (file)
@@ -170,6 +170,25 @@ out_free:
        return 0;
 }
 
+static struct btrfs_dir_item *btrfs_lookup_match_dir(
+                       struct btrfs_trans_handle *trans,
+                       struct btrfs_root *root, struct btrfs_path *path,
+                       struct btrfs_key *key, const char *name,
+                       int name_len, int mod)
+{
+       const int ins_len = (mod < 0 ? -1 : 0);
+       const int cow = (mod != 0);
+       int ret;
+
+       ret = btrfs_search_slot(trans, root, key, path, ins_len, cow);
+       if (ret < 0)
+               return ERR_PTR(ret);
+       if (ret > 0)
+               return ERR_PTR(-ENOENT);
+
+       return btrfs_match_dir_item_name(root->fs_info, path, name, name_len);
+}
+
 /*
  * lookup a directory item based on name.  'dir' is the objectid
  * we're searching in, and 'mod' tells us if you plan on deleting the
@@ -181,23 +200,18 @@ struct btrfs_dir_item *btrfs_lookup_dir_item(struct btrfs_trans_handle *trans,
                                             const char *name, int name_len,
                                             int mod)
 {
-       int ret;
        struct btrfs_key key;
-       int ins_len = mod < 0 ? -1 : 0;
-       int cow = mod != 0;
+       struct btrfs_dir_item *di;
 
        key.objectid = dir;
        key.type = BTRFS_DIR_ITEM_KEY;
-
        key.offset = btrfs_name_hash(name, name_len);
 
-       ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow);
-       if (ret < 0)
-               return ERR_PTR(ret);
-       if (ret > 0)
+       di = btrfs_lookup_match_dir(trans, root, path, &key, name, name_len, mod);
+       if (IS_ERR(di) && PTR_ERR(di) == -ENOENT)
                return NULL;
 
-       return btrfs_match_dir_item_name(root->fs_info, path, name, name_len);
+       return di;
 }
 
 int btrfs_check_dir_item_collision(struct btrfs_root *root, u64 dir,
@@ -211,7 +225,6 @@ int btrfs_check_dir_item_collision(struct btrfs_root *root, u64 dir,
        int slot;
        struct btrfs_path *path;
 
-
        path = btrfs_alloc_path();
        if (!path)
                return -ENOMEM;
@@ -220,20 +233,20 @@ int btrfs_check_dir_item_collision(struct btrfs_root *root, u64 dir,
        key.type = BTRFS_DIR_ITEM_KEY;
        key.offset = btrfs_name_hash(name, name_len);
 
-       ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
-
-       /* return back any errors */
-       if (ret < 0)
-               goto out;
+       di = btrfs_lookup_match_dir(NULL, root, path, &key, name, name_len, 0);
+       if (IS_ERR(di)) {
+               ret = PTR_ERR(di);
+               /* Nothing found, we're safe */
+               if (ret == -ENOENT) {
+                       ret = 0;
+                       goto out;
+               }
 
-       /* nothing found, we're safe */
-       if (ret > 0) {
-               ret = 0;
-               goto out;
+               if (ret < 0)
+                       goto out;
        }
 
        /* we found an item, look for our name in the item */
-       di = btrfs_match_dir_item_name(root->fs_info, path, name, name_len);
        if (di) {
                /* our exact name was found */
                ret = -EEXIST;
@@ -274,21 +287,13 @@ btrfs_lookup_dir_index_item(struct btrfs_trans_handle *trans,
                            u64 objectid, const char *name, int name_len,
                            int mod)
 {
-       int ret;
        struct btrfs_key key;
-       int ins_len = mod < 0 ? -1 : 0;
-       int cow = mod != 0;
 
        key.objectid = dir;
        key.type = BTRFS_DIR_INDEX_KEY;
        key.offset = objectid;
 
-       ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow);
-       if (ret < 0)
-               return ERR_PTR(ret);
-       if (ret > 0)
-               return ERR_PTR(-ENOENT);
-       return btrfs_match_dir_item_name(root->fs_info, path, name, name_len);
+       return btrfs_lookup_match_dir(trans, root, path, &key, name, name_len, mod);
 }
 
 struct btrfs_dir_item *
@@ -345,21 +350,18 @@ struct btrfs_dir_item *btrfs_lookup_xattr(struct btrfs_trans_handle *trans,
                                          const char *name, u16 name_len,
                                          int mod)
 {
-       int ret;
        struct btrfs_key key;
-       int ins_len = mod < 0 ? -1 : 0;
-       int cow = mod != 0;
+       struct btrfs_dir_item *di;
 
        key.objectid = dir;
        key.type = BTRFS_XATTR_ITEM_KEY;
        key.offset = btrfs_name_hash(name, name_len);
-       ret = btrfs_search_slot(trans, root, &key, path, ins_len, cow);
-       if (ret < 0)
-               return ERR_PTR(ret);
-       if (ret > 0)
+
+       di = btrfs_lookup_match_dir(trans, root, path, &key, name, name_len, mod);
+       if (IS_ERR(di) && PTR_ERR(di) == -ENOENT)
                return NULL;
 
-       return btrfs_match_dir_item_name(root->fs_info, path, name, name_len);
+       return di;
 }
 
 /*