]> git.hungrycats.org Git - btrfs-progs/commitdiff
btrfs-progs: scrub: only restore the old scrub limit after scrub is finished
authorQu Wenruo <wqu@suse.com>
Tue, 1 Sep 2026 22:11:21 +0000 (07:41 +0930)
committerQu Wenruo <wqu@suse.com>
Mon, 5 Oct 2026 08:51:31 +0000 (19:21 +1030)
[BUG]
There is a bug report that the --limit option of `btrfs scrub start`
doesn't properly apply for all devices.

[CAUSE]
It turns out that the timing we restore the scrub limit is too early:

 scrub_start()
 |- read_scrub_device_limit()
 |- write_scrub_device_limit()
 |  The old limit and new limit are written before we start scrub.
 |
 |- pthread_create()
 |  Now we start threads to do the real scrub ioctl for each device
 |
 |- write_scrub_device_limit()
 |  Now we already restore the old throughput for the first device.
 |
 |- pthread_join()
    We only wait for the scrub to finish after we have restored the old
    limit, which normally is no limit.

[FIX]
Move the old limit restore code after we have waited for all scrub
threads.

Issue: #1165
Fixes: 7c1c88e41668 ("btrfs-progs: scrub: add the new --limit option to set the throughput limit at runtime")
Signed-off-by: Qu Wenruo <wqu@suse.com>
cmds/scrub.c

index 825b91da910f87675de6d4aa3463650c7813050b..cb02d9216498a35f52062f5d5548b8715656f12e 100644 (file)
@@ -1594,14 +1594,6 @@ static int scrub_start(const struct cmd_struct *cmd, int argc, char **argv,
 
        err = 0;
        for (i = 0; i < fi_args.num_devices; ++i) {
-               /* Revert to the older scrub limit. */
-               ret = write_scrub_device_limit(fdmnt, di_args[i].devid, sp[i].old_limit);
-               if (ret < 0) {
-                       errno = -ret;
-                       warning("failed to reset scrub throughput limit on devid %llu: %m",
-                               di_args[i].devid);
-               }
-
                if (sp[i].skip)
                        continue;
                devid = di_args[i].devid;
@@ -1645,6 +1637,16 @@ static int scrub_start(const struct cmd_struct *cmd, int argc, char **argv,
                        e_correctable++;
        }
 
+       /* Revert to the older scrub limits. */
+       for (i = 0; i < fi_args.num_devices; ++i) {
+               ret = write_scrub_device_limit(fdmnt, di_args[i].devid, sp[i].old_limit);
+               if (ret < 0) {
+                       errno = -ret;
+                       warning("failed to reset scrub throughput limit on devid %llu: %m",
+                               di_args[i].devid);
+               }
+       }
+
        if (do_print) {
                const char *append = "done";
                u64 total_bytes_scrubbed = 0;