]> git.hungrycats.org Git - btrfs-progs/commitdiff
btrfs-progs: check: add option to skip qgroup accounting verification
authorDongjiang Zhu <zhudongjiang@fnnas.com>
Mon, 29 Jun 2026 12:28:52 +0000 (20:28 +0800)
committerDavid Sterba <dsterba@suse.com>
Tue, 14 Jul 2026 22:28:53 +0000 (00:28 +0200)
Qgroup accounting verification can be expensive because it walks extents
and backrefs to recompute referenced/exclusive numbers.

Add --skip-qgroup-accounting so btrfs check can still load qgroup
status/info/relation items from the quota tree without verifying the
accounting numbers.

Reject the option with --repair and --qgroup-report, as both depend on
full accounting verification results.

Reviewed-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: Dongjiang Zhu <zhudongjiang@fnnas.com>
Documentation/btrfs-check.rst
check/main.c
check/qgroup-verify.c
check/qgroup-verify.h
mkfs/main.c

index cdcc603fe815eeb6d6cecdc900af27f7f0bdb810..99fea5ae1d5d0d743f8372e4cf5407f1be004d4e 100644 (file)
@@ -58,6 +58,16 @@ SAFE OR ADVISORY OPTIONS
         This expects that the filesystem is otherwise OK, and is basically an offline
         *scrub* that does not repair data from spare copies.
 
+--skip-qgroup-accounting
+        skip verification of qgroup referenced/exclusive accounting.
+
+        Qgroup status, info and relation items are still loaded from the quota
+        tree. This option cannot be used with *--repair* or *--qgroup-report*.
+
+        This can significantly reduce runtime on filesystems with many snapshots
+        or reflinked files, as full qgroup accounting verification can require
+        extensive backref walks.
+
 --chunk-root <bytenr>
         use the given offset *bytenr* for the chunk tree root
 
index 78e2515e1d71be7799b79342f468cf8fbed07e31..a60b9b0b6086f1a88250ef0b383b0da8b577bbbe 100644 (file)
@@ -86,6 +86,7 @@ bool no_holes = false;
 bool is_free_space_tree = false;
 bool init_extent_tree = false;
 bool check_data_csum = false;
+static bool check_qgroup_accounting = true;
 static bool found_free_ino_cache = false;
 static bool found_unknown_key = false;
 struct cache_tree *roots_info_cache = NULL;
@@ -217,7 +218,9 @@ static void print_status_check_line(void *p)
                "[5/7] checking csums against data             " :
                "[5/7] checking csums (without verifying data) ",
                "[6/7] checking root refs                      ",
-               "[7/7] checking quota groups                   ",
+               check_qgroup_accounting ?
+               "[7/7] checking quota groups                   " :
+               "[7/7] checking qgroups (no accounting verify) ",
        };
        time_t elapsed;
        int hours;
@@ -10599,6 +10602,7 @@ static const char * const cmd_check_usage[] = {
        "",
        "Check and reporting options:",
        OPTLINE("--check-data-csum", "verify checksums of data blocks"),
+       OPTLINE("--skip-qgroup-accounting", "skip quota group accounting verification"),
        OPTLINE("-Q, --qgroup-report", "print a report on qgroup consistency"),
        OPTLINE("-E, --subvol-extents SUBVOLID", "print subvolume extents and sharing state"),
        OPTLINE("-p, --progress", "indicate progress"),
@@ -10638,7 +10642,7 @@ static int cmd_check(const struct cmd_struct *cmd, int argc, char **argv)
                        GETOPT_VAL_INIT_EXTENT, GETOPT_VAL_CHECK_CSUM,
                        GETOPT_VAL_READONLY, GETOPT_VAL_CHUNK_TREE,
                        GETOPT_VAL_MODE, GETOPT_VAL_CLEAR_SPACE_CACHE,
-                       GETOPT_VAL_FORCE };
+                       GETOPT_VAL_FORCE, GETOPT_VAL_SKIP_QGROUP_ACCOUNTING };
                static const struct option long_options[] = {
                        { "super", required_argument, NULL, 's' },
                        { "repair", no_argument, NULL, GETOPT_VAL_REPAIR },
@@ -10649,6 +10653,8 @@ static int cmd_check(const struct cmd_struct *cmd, int argc, char **argv)
                                GETOPT_VAL_INIT_EXTENT },
                        { "check-data-csum", no_argument, NULL,
                                GETOPT_VAL_CHECK_CSUM },
+                       { "skip-qgroup-accounting", no_argument, NULL,
+                               GETOPT_VAL_SKIP_QGROUP_ACCOUNTING },
                        { "backup", no_argument, NULL, 'b' },
                        { "subvol-extents", required_argument, NULL, 'E' },
                        { "qgroup-report", no_argument, NULL, 'Q' },
@@ -10721,6 +10727,9 @@ static int cmd_check(const struct cmd_struct *cmd, int argc, char **argv)
                        case GETOPT_VAL_CHECK_CSUM:
                                check_data_csum = true;
                                break;
+                       case GETOPT_VAL_SKIP_QGROUP_ACCOUNTING:
+                               check_qgroup_accounting = false;
+                               break;
                        case GETOPT_VAL_MODE:
                                check_mode = parse_check_mode(optarg);
                                if (check_mode == CHECK_MODE_UNKNOWN) {
@@ -10764,6 +10773,15 @@ static int cmd_check(const struct cmd_struct *cmd, int argc, char **argv)
                exit(1);
        }
 
+       if (!check_qgroup_accounting && opt_check_repair) {
+               error("--skip-qgroup-accounting is not compatible with --repair");
+               exit(1);
+       }
+       if (!check_qgroup_accounting && qgroup_report) {
+               error("--skip-qgroup-accounting is not compatible with --qgroup-report");
+               exit(1);
+       }
+
        if (opt_check_repair && !force) {
                int delay = 10;
 
@@ -10884,7 +10902,7 @@ static int cmd_check(const struct cmd_struct *cmd, int argc, char **argv)
 
        if (qgroup_report) {
                printf("Print quota groups report for %s\nUUID: %s\n", argv[optind], uuidbuf);
-               ret = qgroup_verify_all(gfs_info);
+               ret = qgroup_verify(gfs_info, true);
                err |= !!ret;
                if (ret >= 0)
                        report_qgroups(1);
@@ -11145,26 +11163,32 @@ static int cmd_check(const struct cmd_struct *cmd, int argc, char **argv)
 
        if (gfs_info->quota_enabled) {
                if (!g_task_ctx.progress_enabled) {
-                       fprintf(stderr, "[8/8] checking quota groups\n");
+                       if (check_qgroup_accounting)
+                               fprintf(stderr, "[8/8] checking quota groups\n");
+                       else
+                               fprintf(stderr,
+                       "[8/8] checking quota groups (without verifying accounting)\n");
                } else {
                        g_task_ctx.tp = TASK_QGROUPS;
                        task_start(g_task_ctx.info, &g_task_ctx.start_time, &g_task_ctx.item_count);
                }
-               qgroup_verify_ret = qgroup_verify_all(gfs_info);
+               qgroup_verify_ret = qgroup_verify(gfs_info, check_qgroup_accounting);
                task_stop(g_task_ctx.info);
                if (qgroup_verify_ret < 0) {
                        error("failed to check quota groups");
                        err |= !!qgroup_verify_ret;
                        goto out;
                }
-               report_qgroups(0);
-               ret = repair_qgroups(gfs_info, &qgroups_repaired, false);
-               if (ret) {
-                       error("failed to repair quota groups");
-                       goto out;
+               if (check_qgroup_accounting) {
+                       report_qgroups(0);
+                       ret = repair_qgroups(gfs_info, &qgroups_repaired, false);
+                       if (ret) {
+                               error("failed to repair quota groups");
+                               goto out;
+                       }
+                       if (qgroup_verify_ret && (!qgroups_repaired || ret))
+                               err |= !!qgroup_verify_ret;
                }
-               if (qgroup_verify_ret && (!qgroups_repaired || ret))
-                       err |= !!qgroup_verify_ret;
                ret = 0;
        } else {
                fprintf(stderr,
index 3a2f847d40356bca9c01ec470ce4be9dc41147d3..a2e654094de298313786903d6be3d6d8e4d32d6d 100644 (file)
@@ -1451,13 +1451,16 @@ static bool is_bad_qgroup(struct qgroup_count *count)
 }
 
 /*
- * Verify all qgroup numbers.
+ * Load qgroup items and optionally verify qgroup numbers.
+ *
+ * @check_accounting true: verify referenced/exclusive accounting.
+ * @check_accounting false: only load qgroup status/info/relation items.
  *
  * Return <0 for fatal errors (e.g. ENOMEM or failed to read quota tree)
- * Return 0 if all qgroup numbers are correct or no need to check (under rescan)
- * Return >0 if qgroup numbers are inconsistent.
+ * Return 0 if requested checks pass or accounting mismatches are ignored.
+ * Return >0 if qgroup numbers are inconsistent and not ignored.
  */
-int qgroup_verify_all(struct btrfs_fs_info *info)
+int qgroup_verify(struct btrfs_fs_info *info, bool check_accounting)
 {
        struct rb_node *n;
        int ret;
@@ -1486,6 +1489,9 @@ int qgroup_verify_all(struct btrfs_fs_info *info)
            counts.rescan_running == 0)
                skip_err = true;
 
+       if (!check_accounting)
+               goto out;
+
        /*
         * Put all extent refs into our rbtree
         */
index 1489acbae20f6fdf101092232b2aa0e01da1b15c..56149c38bcc1d65ed9341e0242778f285ecc1ae8 100644 (file)
@@ -24,7 +24,7 @@
 
 struct btrfs_fs_info;
 
-int qgroup_verify_all(struct btrfs_fs_info *info);
+int qgroup_verify(struct btrfs_fs_info *info, bool check_accounting);
 void report_qgroups(int all);
 int repair_qgroups(struct btrfs_fs_info *info, int *repaired, bool silent);
 
index b8b4cb7d14094842c923d9a1ade3651aadc5c1b7..f59df0e3cfa161afda90a02eec2a9bd917fbbffa 100644 (file)
@@ -1046,7 +1046,7 @@ static int setup_quota_root(struct btrfs_fs_info *fs_info)
         * Qgroup is setup but with wrong info, use qgroup-verify
         * infrastructure to repair them.  (Just acts as offline rescan)
         */
-       ret = qgroup_verify_all(fs_info);
+       ret = qgroup_verify(fs_info, true);
        if (ret < 0) {
                errno = -ret;
                error("qgroup rescan failed: %m");