diff mbox series

[RFC] btrfs-progs: scrub: add basic support for SCRUB_LOGICAL

Message ID 20230617050935.138165-1-wqu@suse.com (mailing list archive)
State New, archived
Headers show
Series [RFC] btrfs-progs: scrub: add basic support for SCRUB_LOGICAL | expand

Commit Message

Qu Wenruo June 17, 2023, 5:09 a.m. UTC
This patch is only for test purpose.

It addes a new option, "-l", to "scrub start" subcommand, and only
support foreground scrub and full report.

Signed-off-by: Qu Wenruo <wqu@suse.com>
---
 cmds/scrub.c               | 33 +++++++++++++++++++++++++++++++--
 kernel-shared/uapi/btrfs.h |  3 ++-
 2 files changed, 33 insertions(+), 3 deletions(-)
diff mbox series

Patch

diff --git a/cmds/scrub.c b/cmds/scrub.c
index 96e3bb5c..82d69975 100644
--- a/cmds/scrub.c
+++ b/cmds/scrub.c
@@ -326,7 +326,8 @@  static void print_scrub_dev(struct btrfs_ioctl_dev_info_args *di,
 		if (raw)
 			print_scrub_full(p);
 		else
-			print_scrub_summary(p, ss, di->bytes_used);
+			print_scrub_summary(p, ss, p->tree_bytes_scrubbed +
+						   p->data_bytes_scrubbed);
 	}
 }
 
@@ -1146,6 +1147,24 @@  static int is_scrub_running_in_kernel(int fd,
 	return 0;
 }
 
+static int scrub_one_fs(int fd, bool readonly)
+{
+	struct btrfs_ioctl_scrub_args scrub_args = { 0 };
+	int ret;
+
+	scrub_args.flags = BTRFS_SCRUB_LOGICAL;
+	scrub_args.end = U64_MAX;
+	if (readonly)
+		scrub_args.flags |= BTRFS_SCRUB_READONLY;
+	ret = ioctl(fd, BTRFS_IOC_SCRUB, &scrub_args);
+	if (ret < 0) {
+		error("failed to do logical scrub: ret=%d errno=%d\n", ret, errno);
+		return ret;
+	}
+	print_scrub_full(&scrub_args.progress);
+	return 0;
+}
+
 static int scrub_start(const struct cmd_struct *cmd, int argc, char **argv,
 		       bool resume)
 {
@@ -1164,6 +1183,7 @@  static int scrub_start(const struct cmd_struct *cmd, int argc, char **argv,
 	bool do_background = true;
 	bool do_wait = false;
 	bool do_print = false;
+	bool do_logical_scrub = false;
 	int do_quiet = !bconf.verbose; /*Read the global quiet option if set*/
 	bool do_record = true;
 	bool readonly = false;
@@ -1195,7 +1215,7 @@  static int scrub_start(const struct cmd_struct *cmd, int argc, char **argv,
 	bool force = false;
 	bool nothing_to_resume = false;
 
-	while ((c = getopt(argc, argv, "BdqrRc:n:f")) != -1) {
+	while ((c = getopt(argc, argv, "BdqrRc:n:fl")) != -1) {
 		switch (c) {
 		case 'B':
 			do_background = false;
@@ -1224,6 +1244,9 @@  static int scrub_start(const struct cmd_struct *cmd, int argc, char **argv,
 		case 'f':
 			force = true;
 			break;
+		case 'l':
+			do_logical_scrub = true;
+			break;
 		default:
 			usage_unknown_option(cmd, argv);
 		}
@@ -1252,6 +1275,12 @@  static int scrub_start(const struct cmd_struct *cmd, int argc, char **argv,
 	if (fdmnt < 0)
 		return 1;
 
+	if (do_logical_scrub) {
+		ret = scrub_one_fs(fdmnt, readonly);
+		if (ret < 0)
+			err = 1;
+		goto out;
+	}
 	ret = get_fs_info(path, &fi_args, &di_args);
 	if (ret) {
 		errno = -ret;
diff --git a/kernel-shared/uapi/btrfs.h b/kernel-shared/uapi/btrfs.h
index 0859a7cc..6038ee4c 100644
--- a/kernel-shared/uapi/btrfs.h
+++ b/kernel-shared/uapi/btrfs.h
@@ -199,7 +199,8 @@  struct btrfs_scrub_progress {
 					 * Intermittent error. */
 };
 
-#define BTRFS_SCRUB_READONLY	1
+#define BTRFS_SCRUB_READONLY		(1ULL << 0)
+#define BTRFS_SCRUB_LOGICAL		(1ULL << 1)
 struct btrfs_ioctl_scrub_args {
 	__u64 devid;				/* in */
 	__u64 start;				/* in */