linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] btrfs-progs: cmd-device: add options to sync the fs after adding all devices
@ 2023-07-27  6:08 Qu Wenruo
  0 siblings, 0 replies; only message in thread
From: Qu Wenruo @ 2023-07-27  6:08 UTC (permalink / raw)
  To: linux-btrfs

Although kernels would commit transaction after adding each device, the
behavior itself would change to allow emergency device add (aka, without
extra devices the fs can not commit a transaction).

Unfortunately btrfs device add ioctl has no extra space for new flags,
so here we add extra flags --sync and --nosync to "btrfs device add"
subcommand.

This would allowing users to choose if the target filesystem needs to be
synced after all devices added.

The default behavior is --sync, to keep the behavior the same no matter
kernel versions.

Signed-off-by: Qu Wenruo <wqu@suse.com>
---
 Documentation/btrfs-device.rst |  8 ++++++++
 cmds/device.c                  | 22 +++++++++++++++++++++-
 2 files changed, 29 insertions(+), 1 deletion(-)

diff --git a/Documentation/btrfs-device.rst b/Documentation/btrfs-device.rst
index 0459e93681e1..30c3afca8e3b 100644
--- a/Documentation/btrfs-device.rst
+++ b/Documentation/btrfs-device.rst
@@ -41,6 +41,14 @@ add [-Kf] <device> [<device>...] <path>
         --enqueue
                 wait if there's another exclusive operation running, otherwise continue
 
+        --sync
+                sync the filesystem after all devices added
+
+        --nosync
+                do not sync the filesystem after all devices added.
+                This may not work as older kernels would commit transaction after
+                adding each device.
+
 remove [options] <device>|<devid> [<device>|<devid>...] <path>
         Remove device(s) from a filesystem identified by <path>
 
diff --git a/cmds/device.c b/cmds/device.c
index 94418d43d6d4..e4458d668ebc 100644
--- a/cmds/device.c
+++ b/cmds/device.c
@@ -42,6 +42,7 @@
 #include "cmds/commands.h"
 #include "cmds/filesystem-usage.h"
 #include "mkfs/common.h"
+#include "libbtrfsutil/btrfsutil.h"
 
 static const char * const device_cmd_group_usage[] = {
 	"btrfs device <command> [<args>]",
@@ -55,6 +56,8 @@ static const char * const cmd_device_add_usage[] = {
 	OPTLINE("-K|--nodiscard", "do not perform whole device TRIM on devices that report such capability"),
 	OPTLINE("-f|--force", "force overwrite existing filesystem on the disk"),
 	OPTLINE("--enqueue", "wait if there's another exclusive operation running, otherwise continue"),
+	OPTLINE("--sync", "sync the fs after all devices added"),
+	OPTLINE("--nosync", "do not sync the fs after all devices added"),
 	NULL
 };
 
@@ -68,17 +71,21 @@ static int cmd_device_add(const struct cmd_struct *cmd,
 	bool force = false;
 	int last_dev;
 	bool enqueue = false;
+	bool do_sync = true;
 	int zoned;
 	struct btrfs_ioctl_feature_flags feature_flags;
 
 	optind = 0;
 	while (1) {
 		int c;
-		enum { GETOPT_VAL_ENQUEUE = GETOPT_VAL_FIRST };
+		enum { GETOPT_VAL_ENQUEUE = GETOPT_VAL_FIRST,
+		       GETOPT_VAL_SYNC, GETOPT_VAL_NOSYNC };
 		static const struct option long_options[] = {
 			{ "nodiscard", optional_argument, NULL, 'K'},
 			{ "force", no_argument, NULL, 'f'},
 			{ "enqueue", no_argument, NULL, GETOPT_VAL_ENQUEUE},
+			{ "sync", no_argument, NULL, GETOPT_VAL_SYNC},
+			{ "nosync", no_argument, NULL, GETOPT_VAL_NOSYNC},
 			{ NULL, 0, NULL, 0}
 		};
 
@@ -95,6 +102,12 @@ static int cmd_device_add(const struct cmd_struct *cmd,
 		case GETOPT_VAL_ENQUEUE:
 			enqueue = true;
 			break;
+		case GETOPT_VAL_SYNC:
+			do_sync = true;
+			break;
+		case GETOPT_VAL_NOSYNC:
+			do_sync = false;
+			break;
 		default:
 			usage_unknown_option(cmd, argv);
 		}
@@ -181,6 +194,13 @@ static int cmd_device_add(const struct cmd_struct *cmd,
 	}
 
 error_out:
+	if (!ret && do_sync) {
+		ret = btrfs_util_sync_fd(fdmnt);
+		if (ret < 0) {
+			errno = -ret;
+			error("error syncing the fs: %m");
+		}
+	}
 	btrfs_warn_multiple_profiles(fdmnt);
 	close_file_or_dir(fdmnt, dirstream);
 	return !!ret;
-- 
2.41.0


^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2023-07-27  6:08 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-07-27  6:08 [PATCH] btrfs-progs: cmd-device: add options to sync the fs after adding all devices Qu Wenruo

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).