btrfs-progs: check: introduce function to check dir_item
[platform/upstream/btrfs-progs.git] / btrfs-convert.c
index 3a5dd12..4b4cea4 100644 (file)
@@ -968,7 +968,7 @@ static int create_image(struct btrfs_root *root,
 {
        struct btrfs_inode_item buf;
        struct btrfs_trans_handle *trans;
-       struct btrfs_path *path = NULL;
+       struct btrfs_path path;
        struct btrfs_key key;
        struct cache_extent *cache;
        struct cache_tree used_tmp;
@@ -985,6 +985,7 @@ static int create_image(struct btrfs_root *root,
                return -ENOMEM;
 
        cache_tree_init(&used_tmp);
+       btrfs_init_path(&path);
 
        ret = btrfs_find_free_objectid(trans, root, BTRFS_FIRST_FREE_OBJECTID,
                                       &ino);
@@ -1001,24 +1002,19 @@ static int create_image(struct btrfs_root *root,
        if (ret < 0)
                goto out;
 
-       path = btrfs_alloc_path();
-       if (!path) {
-               ret = -ENOMEM;
-               goto out;
-       }
        key.objectid = ino;
        key.type = BTRFS_INODE_ITEM_KEY;
        key.offset = 0;
 
-       ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
+       ret = btrfs_search_slot(trans, root, &key, &path, 0, 1);
        if (ret) {
                ret = (ret > 0 ? -ENOENT : ret);
                goto out;
        }
-       read_extent_buffer(path->nodes[0], &buf,
-                       btrfs_item_ptr_offset(path->nodes[0], path->slots[0]),
+       read_extent_buffer(path.nodes[0], &buf,
+                       btrfs_item_ptr_offset(path.nodes[0], path.slots[0]),
                        sizeof(buf));
-       btrfs_release_path(path);
+       btrfs_release_path(&path);
 
        /*
         * Create a new used space cache, which doesn't contain the reserved
@@ -1056,18 +1052,18 @@ static int create_image(struct btrfs_root *root,
        key.objectid = ino;
        key.type = BTRFS_INODE_ITEM_KEY;
        key.offset = 0;
-       ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
+       ret = btrfs_search_slot(trans, root, &key, &path, 0, 1);
        if (ret) {
                ret = (ret > 0 ? -ENOENT : ret);
                goto out;
        }
        btrfs_set_stack_inode_size(&buf, cfg->num_bytes);
-       write_extent_buffer(path->nodes[0], &buf,
-                       btrfs_item_ptr_offset(path->nodes[0], path->slots[0]),
+       write_extent_buffer(path.nodes[0], &buf,
+                       btrfs_item_ptr_offset(path.nodes[0], path.slots[0]),
                        sizeof(buf));
 out:
        free_extent_cache_tree(&used_tmp);
-       btrfs_free_path(path);
+       btrfs_release_path(&path);
        btrfs_commit_transaction(trans, root);
        return ret;
 }
@@ -1079,7 +1075,7 @@ static struct btrfs_root* link_subvol(struct btrfs_root *root,
        struct btrfs_fs_info *fs_info = root->fs_info;
        struct btrfs_root *tree_root = fs_info->tree_root;
        struct btrfs_root *new_root = NULL;
-       struct btrfs_path *path;
+       struct btrfs_path path;
        struct btrfs_inode_item *inode_item;
        struct extent_buffer *leaf;
        struct btrfs_key key;
@@ -1094,28 +1090,25 @@ static struct btrfs_root* link_subvol(struct btrfs_root *root,
        if (len == 0 || len > BTRFS_NAME_LEN)
                return NULL;
 
-       path = btrfs_alloc_path();
-       if (!path)
-               return NULL;
-
+       btrfs_init_path(&path);
        key.objectid = dirid;
        key.type = BTRFS_DIR_INDEX_KEY;
        key.offset = (u64)-1;
 
-       ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
+       ret = btrfs_search_slot(NULL, root, &key, &path, 0, 0);
        if (ret <= 0) {
                error("search for DIR_INDEX dirid %llu failed: %d",
                                (unsigned long long)dirid, ret);
                goto fail;
        }
 
-       if (path->slots[0] > 0) {
-               path->slots[0]--;
-               btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
+       if (path.slots[0] > 0) {
+               path.slots[0]--;
+               btrfs_item_key_to_cpu(path.nodes[0], &key, path.slots[0]);
                if (key.objectid == dirid && key.type == BTRFS_DIR_INDEX_KEY)
                        index = key.offset + 1;
        }
-       btrfs_release_path(path);
+       btrfs_release_path(&path);
 
        trans = btrfs_start_transaction(root, 1);
        if (!trans) {
@@ -1127,14 +1120,14 @@ static struct btrfs_root* link_subvol(struct btrfs_root *root,
        key.offset = 0;
        key.type =  BTRFS_INODE_ITEM_KEY;
 
-       ret = btrfs_lookup_inode(trans, root, path, &key, 1);
+       ret = btrfs_lookup_inode(trans, root, &path, &key, 1);
        if (ret) {
                error("search for INODE_ITEM %llu failed: %d",
                                (unsigned long long)dirid, ret);
                goto fail;
        }
-       leaf = path->nodes[0];
-       inode_item = btrfs_item_ptr(leaf, path->slots[0],
+       leaf = path.nodes[0];
+       inode_item = btrfs_item_ptr(leaf, path.slots[0],
                                    struct btrfs_inode_item);
 
        key.objectid = root_objectid;
@@ -1159,7 +1152,7 @@ static struct btrfs_root* link_subvol(struct btrfs_root *root,
        btrfs_set_inode_size(leaf, inode_item, len * 2 +
                             btrfs_inode_size(leaf, inode_item));
        btrfs_mark_buffer_dirty(leaf);
-       btrfs_release_path(path);
+       btrfs_release_path(&path);
 
        /* add the backref first */
        ret = btrfs_add_root_ref(trans, tree_root, root_objectid,
@@ -1194,7 +1187,7 @@ static struct btrfs_root* link_subvol(struct btrfs_root *root,
                new_root = NULL;
        }
 fail:
-       btrfs_free_path(path);
+       btrfs_init_path(&path);
        return new_root;
 }