btrfs-progs: mkfs rootdir: use lgetxattr() not to follow a symbolic link
[platform/upstream/btrfs-progs.git] / qgroup-verify.c
index f6df12d..571b4d4 100644 (file)
@@ -27,7 +27,7 @@
 #include "disk-io.h"
 #include "print-tree.h"
 #include "utils.h"
-#include "ulist.h"
+#include "kernel-shared/ulist.h"
 #include "rbtree-utils.h"
 #include "transaction.h"
 #include "repair.h"
@@ -330,9 +330,18 @@ static int find_parent_roots(struct ulist *roots, u64 parent)
         * For each unresolved root, we recurse
         */
        ref = find_ref_bytenr(parent);
+       if (!ref) {
+               error("bytenr ref not found for parent %llu",
+                               (unsigned long long)parent);
+               return -EIO;
+       }
        node = &ref->bytenr_node;
-       BUG_ON(ref == NULL);
-       BUG_ON(ref->bytenr != parent);
+       if (ref->bytenr != parent) {
+               error("found bytenr ref does not match parent: %llu != %llu",
+                               (unsigned long long)ref->bytenr,
+                               (unsigned long long)parent);
+               return -EIO;
+       }
 
        {
                /*
@@ -341,9 +350,15 @@ static int find_parent_roots(struct ulist *roots, u64 parent)
                 */
                struct rb_node *prev_node = rb_prev(&ref->bytenr_node);
                struct ref *prev;
+
                if (prev_node) {
                        prev = rb_entry(prev_node, struct ref, bytenr_node);
-                       BUG_ON(prev->bytenr == parent);
+                       if (prev->bytenr == parent) {
+                               error(
+                               "unexpected: prev bytenr same as parent: %llu",
+                                               (unsigned long long)parent);
+                               return -EIO;
+                       }
                }
        }
 
@@ -354,6 +369,11 @@ static int find_parent_roots(struct ulist *roots, u64 parent)
                                if (ret < 0)
                                        goto out;
                        }
+               } else if (ref->parent == ref->bytenr) {
+                       /*
+                        * Special loop case for tree reloc tree
+                        */
+                       ref->root = BTRFS_TREE_RELOC_OBJECTID;
                } else {
                        ret = find_parent_roots(roots, ref->parent);
                        if (ret < 0)
@@ -563,6 +583,8 @@ static u64 resolve_one_root(u64 bytenr)
 
        if (ref->root)
                return ref->root;
+       if (ref->parent == bytenr)
+               return BTRFS_TREE_RELOC_OBJECTID;
        return resolve_one_root(ref->parent);
 }
 
@@ -689,7 +711,7 @@ static int travel_tree(struct btrfs_fs_info *info, struct btrfs_root *root,
 //     printf("travel_tree: bytenr: %llu\tnum_bytes: %llu\tref_parent: %llu\n",
 //            bytenr, num_bytes, ref_parent);
 
-       eb = read_tree_block(root, bytenr, num_bytes, 0);
+       eb = read_tree_block(info, bytenr, 0);
        if (!extent_buffer_uptodate(eb))
                return -EIO;
 
@@ -714,7 +736,7 @@ static int travel_tree(struct btrfs_fs_info *info, struct btrfs_root *root,
        nr = btrfs_header_nritems(eb);
        for (i = 0; i < nr; i++) {
                new_bytenr = btrfs_node_blockptr(eb, i);
-               new_num_bytes = root->nodesize;
+               new_num_bytes = info->nodesize;
 
                ret = travel_tree(info, root, new_bytenr, new_num_bytes,
                                  ref_parent);
@@ -733,6 +755,9 @@ static int add_refs_for_implied(struct btrfs_fs_info *info, u64 bytenr,
        struct btrfs_root *root;
        struct btrfs_key key;
 
+       /* Tree reloc tree doesn't contribute qgroup, skip it */
+       if (root_id == BTRFS_TREE_RELOC_OBJECTID)
+               return 0;
        key.objectid = root_id;
        key.type = BTRFS_ROOT_ITEM_KEY;
        key.offset = (u64)-1;
@@ -1160,7 +1185,7 @@ static int scan_extents(struct btrfs_fs_info *info,
                                bytenr = key.objectid;
                                num_bytes = key.offset;
                                if (key.type == BTRFS_METADATA_ITEM_KEY) {
-                                       num_bytes = info->extent_root->nodesize;
+                                       num_bytes = info->nodesize;
                                        meta = 1;
                                }
 
@@ -1454,29 +1479,24 @@ static int repair_qgroup_info(struct btrfs_fs_info *info,
        int ret;
        struct btrfs_root *root = info->quota_root;
        struct btrfs_trans_handle *trans;
-       struct btrfs_path *path;
+       struct btrfs_path path;
        struct btrfs_qgroup_info_item *info_item;
        struct btrfs_key key;
 
        printf("Repair qgroup %llu/%llu\n", btrfs_qgroup_level(count->qgroupid),
               btrfs_qgroup_subvid(count->qgroupid));
 
-       path = btrfs_alloc_path();
-       if (!path)
-               return -ENOMEM;
-
        trans = btrfs_start_transaction(root, 1);
-       if (IS_ERR(trans)) {
-               btrfs_free_path(path);
+       if (IS_ERR(trans))
                return PTR_ERR(trans);
-       }
 
+       btrfs_init_path(&path);
        key.objectid = 0;
        key.type = BTRFS_QGROUP_INFO_KEY;
        key.offset = count->qgroupid;
-       ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
+       ret = btrfs_search_slot(trans, root, &key, &path, 0, 1);
        if (ret) {
-               error("Could not find disk item for qgroup %llu/%llu.\n",
+               error("could not find disk item for qgroup %llu/%llu",
                      btrfs_qgroup_level(count->qgroupid),
                      btrfs_qgroup_subvid(count->qgroupid));
                if (ret > 0)
@@ -1484,27 +1504,27 @@ static int repair_qgroup_info(struct btrfs_fs_info *info,
                goto out;
        }
 
-       info_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
+       info_item = btrfs_item_ptr(path.nodes[0], path.slots[0],
                                   struct btrfs_qgroup_info_item);
 
-       btrfs_set_qgroup_info_generation(path->nodes[0], info_item,
+       btrfs_set_qgroup_info_generation(path.nodes[0], info_item,
                                         trans->transid);
 
-       btrfs_set_qgroup_info_referenced(path->nodes[0], info_item,
+       btrfs_set_qgroup_info_referenced(path.nodes[0], info_item,
                                         count->info.referenced);
-       btrfs_set_qgroup_info_referenced_compressed(path->nodes[0], info_item,
+       btrfs_set_qgroup_info_referenced_compressed(path.nodes[0], info_item,
                                            count->info.referenced_compressed);
 
-       btrfs_set_qgroup_info_exclusive(path->nodes[0], info_item,
+       btrfs_set_qgroup_info_exclusive(path.nodes[0], info_item,
                                        count->info.exclusive);
-       btrfs_set_qgroup_info_exclusive_compressed(path->nodes[0], info_item,
+       btrfs_set_qgroup_info_exclusive_compressed(path.nodes[0], info_item,
                                           count->info.exclusive_compressed);
 
-       btrfs_mark_buffer_dirty(path->nodes[0]);
+       btrfs_mark_buffer_dirty(path.nodes[0]);
 
 out:
        btrfs_commit_transaction(trans, root);
-       btrfs_free_path(path);
+       btrfs_release_path(&path);
 
        return ret;
 }
@@ -1514,53 +1534,48 @@ static int repair_qgroup_status(struct btrfs_fs_info *info)
        int ret;
        struct btrfs_root *root = info->quota_root;
        struct btrfs_trans_handle *trans;
-       struct btrfs_path *path;
+       struct btrfs_path path;
        struct btrfs_key key;
        struct btrfs_qgroup_status_item *status_item;
 
        printf("Repair qgroup status item\n");
 
-       path = btrfs_alloc_path();
-       if (!path)
-               return -ENOMEM;
-
        trans = btrfs_start_transaction(root, 1);
-       if (IS_ERR(trans)) {
-               btrfs_free_path(path);
+       if (IS_ERR(trans))
                return PTR_ERR(trans);
-       }
 
+       btrfs_init_path(&path);
        key.objectid = 0;
        key.type = BTRFS_QGROUP_STATUS_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) {
-               error("Could not find qgroup status item\n");
+               error("could not find qgroup status item");
                if (ret > 0)
                        ret = -ENOENT;
                goto out;
        }
 
-       status_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
+       status_item = btrfs_item_ptr(path.nodes[0], path.slots[0],
                                     struct btrfs_qgroup_status_item);
-       btrfs_set_qgroup_status_flags(path->nodes[0], status_item,
+       btrfs_set_qgroup_status_flags(path.nodes[0], status_item,
                                      BTRFS_QGROUP_STATUS_FLAG_ON);
-       btrfs_set_qgroup_status_rescan(path->nodes[0], status_item, 0);
-       btrfs_set_qgroup_status_generation(path->nodes[0], status_item,
+       btrfs_set_qgroup_status_rescan(path.nodes[0], status_item, 0);
+       btrfs_set_qgroup_status_generation(path.nodes[0], status_item,
                                           trans->transid);
 
-       btrfs_mark_buffer_dirty(path->nodes[0]);
+       btrfs_mark_buffer_dirty(path.nodes[0]);
 
 out:
        btrfs_commit_transaction(trans, root);
-       btrfs_free_path(path);
+       btrfs_release_path(&path);
 
        return ret;
 }
 
 int repair_qgroups(struct btrfs_fs_info *info, int *repaired)
 {
-       int ret;
+       int ret = 0;
        struct qgroup_count *count, *tmpcount;
 
        *repaired = 0;