+static int repair_qgroup_info(struct btrfs_fs_info *info,
+ struct qgroup_count *count)
+{
+ int ret;
+ struct btrfs_root *root = info->quota_root;
+ struct btrfs_trans_handle *trans;
+ 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));
+
+ trans = btrfs_start_transaction(root, 1);
+ 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);
+ if (ret) {
+ error("could not find disk item for qgroup %llu/%llu",
+ btrfs_qgroup_level(count->qgroupid),
+ btrfs_qgroup_subvid(count->qgroupid));
+ if (ret > 0)
+ ret = -ENOENT;
+ goto out;
+ }
+
+ 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,
+ trans->transid);
+
+ btrfs_set_qgroup_info_referenced(path.nodes[0], info_item,
+ count->info.referenced);
+ 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,
+ count->info.exclusive);
+ btrfs_set_qgroup_info_exclusive_compressed(path.nodes[0], info_item,
+ count->info.exclusive_compressed);
+
+ btrfs_mark_buffer_dirty(path.nodes[0]);
+
+out:
+ btrfs_commit_transaction(trans, root);
+ btrfs_release_path(&path);
+
+ return ret;
+}
+
+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_key key;
+ struct btrfs_qgroup_status_item *status_item;
+
+ printf("Repair qgroup status item\n");
+
+ trans = btrfs_start_transaction(root, 1);
+ 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);
+ if (ret) {
+ 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],
+ struct btrfs_qgroup_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,
+ trans->transid);
+
+ btrfs_mark_buffer_dirty(path.nodes[0]);
+
+out:
+ btrfs_commit_transaction(trans, root);
+ btrfs_release_path(&path);
+
+ return ret;
+}
+
+int repair_qgroups(struct btrfs_fs_info *info, int *repaired)
+{
+ int ret = 0;
+ struct qgroup_count *count, *tmpcount;
+
+ *repaired = 0;
+
+ if (!repair)
+ return 0;
+
+ list_for_each_entry_safe(count, tmpcount, &bad_qgroups, bad_list) {
+ ret = repair_qgroup_info(info, count);
+ if (ret) {
+ goto out;
+ }
+
+ (*repaired)++;
+
+ list_del_init(&count->bad_list);
+ }
+
+ /*
+ * Do this step last as we want the latest transaction id on
+ * our qgroup status to avoid a (useless) warning after
+ * mount.
+ */
+ if (*repaired || counts.qgroup_inconsist || counts.rescan_running) {
+ ret = repair_qgroup_status(info);
+ if (ret)
+ goto out;
+
+ (*repaired)++;
+ }
+
+out:
+ return ret;
+}