All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] btrfs-progs: balance: print warn msg in background
@ 2021-11-06  6:34 Sidong Yang
  0 siblings, 0 replies; only message in thread
From: Sidong Yang @ 2021-11-06  6:34 UTC (permalink / raw)
  To: linux-btrfs; +Cc: Sidong Yang

The commit 099157d8 has problem that when it executes in background, it
would not print warning message. Because before printing it makes
background process that can't print anymore. This patch fixes this
problem by making background process after printing message. For that,
this patch adds background flags for do_balance().

Signed-off-by: Sidong Yang <realwakka@gmail.com>
---
 cmds/balance.c | 100 +++++++++++++++++++++++++------------------------
 1 file changed, 51 insertions(+), 49 deletions(-)

diff --git a/cmds/balance.c b/cmds/balance.c
index 2e903b5c..1e1db468 100644
--- a/cmds/balance.c
+++ b/cmds/balance.c
@@ -300,7 +300,8 @@ static int do_balance_v1(int fd)
 
 enum {
 	BALANCE_START_FILTERS = 1 << 0,
-	BALANCE_START_NOWARN  = 1 << 1
+	BALANCE_START_NOWARN  = 1 << 1,
+	BALANCE_START_BACKGROUND = 1 << 2
 };
 
 static int do_balance(const char *path, struct btrfs_ioctl_balance_args *args,
@@ -310,18 +311,6 @@ static int do_balance(const char *path, struct btrfs_ioctl_balance_args *args,
 	int ret;
 	DIR *dirstream = NULL;
 
-	fd = btrfs_open_dir(path, &dirstream, 1);
-	if (fd < 0)
-		return 1;
-
-	ret = check_running_fs_exclop(fd, BTRFS_EXCLOP_BALANCE, enqueue);
-	if (ret != 0) {
-		if (ret < 0)
-			error("unable to check status of exclusive operation: %m");
-		close_file_or_dir(fd, dirstream);
-		return 1;
-	}
-
 	if (!(flags & BALANCE_START_FILTERS) && !(flags & BALANCE_START_NOWARN)) {
 		int delay = 10;
 
@@ -340,6 +329,54 @@ static int do_balance(const char *path, struct btrfs_ioctl_balance_args *args,
 		printf("\nStarting balance without any filters.\n");
 	}
 
+	if (flags & BALANCE_START_BACKGROUND) {
+		switch (fork()) {
+		case (-1):
+			error("unable to fork to run balance in background");
+			return 1;
+		case (0):
+			setsid();
+			switch(fork()) {
+			case (-1):
+				error(
+				"unable to fork to run balance in background");
+				exit(1);
+			case (0):
+				/*
+				 * Read the return value to silence compiler
+				 * warning. Change to / should succeed and
+				 * we're not in a security-sensitive context.
+				 */
+				ret = chdir("/");
+				close(0);
+				close(1);
+				close(2);
+				open("/dev/null", O_RDONLY);
+				open("/dev/null", O_WRONLY);
+				open("/dev/null", O_WRONLY);
+				break;
+			default:
+				exit(0);
+			}
+			break;
+		default:
+			exit(0);
+		}
+	}
+
+	fd = btrfs_open_dir(path, &dirstream, 1);
+	if (fd < 0)
+		return 1;
+
+	ret = check_running_fs_exclop(fd, BTRFS_EXCLOP_BALANCE, enqueue);
+	if (ret != 0) {
+		if (ret < 0)
+			error("unable to check status of exclusive operation: %m");
+		close_file_or_dir(fd, dirstream);
+		return 1;
+	}
+
+
 	ret = ioctl(fd, BTRFS_IOC_BALANCE_V2, args);
 	if (ret < 0) {
 		/*
@@ -415,7 +452,6 @@ static int cmd_balance_start(const struct cmd_struct *cmd,
 	struct btrfs_balance_args *ptrs[] = { &args.data, &args.sys,
 						&args.meta, NULL };
 	int force = 0;
-	int background = 0;
 	bool enqueue = false;
 	unsigned start_flags = 0;
 	bool raid56_warned = false;
@@ -479,7 +515,7 @@ static int cmd_balance_start(const struct cmd_struct *cmd,
 			start_flags |= BALANCE_START_NOWARN;
 			break;
 		case GETOPT_VAL_BACKGROUND:
-			background = 1;
+			start_flags |= BALANCE_START_BACKGROUND;
 			break;
 		case GETOPT_VAL_ENQUEUE:
 			enqueue = true;
@@ -569,40 +605,6 @@ static int cmd_balance_start(const struct cmd_struct *cmd,
 		args.flags |= BTRFS_BALANCE_FORCE;
 	if (bconf.verbose > BTRFS_BCONF_QUIET)
 		dump_ioctl_balance_args(&args);
-	if (background) {
-		switch (fork()) {
-		case (-1):
-			error("unable to fork to run balance in background");
-			return 1;
-		case (0):
-			setsid();
-			switch(fork()) {
-			case (-1):
-				error(
-				"unable to fork to run balance in background");
-				exit(1);
-			case (0):
-				/*
-				 * Read the return value to silence compiler
-				 * warning. Change to / should succeed and
-				 * we're not in a security-sensitive context.
-				 */
-				i = chdir("/");
-				close(0);
-				close(1);
-				close(2);
-				open("/dev/null", O_RDONLY);
-				open("/dev/null", O_WRONLY);
-				open("/dev/null", O_WRONLY);
-				break;
-			default:
-				exit(0);
-			}
-			break;
-		default:
-			exit(0);
-		}
-	}
 
 	return do_balance(argv[optind], &args, start_flags, enqueue);
 }
-- 
2.25.1


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

only message in thread, other threads:[~2021-11-06  6:34 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-11-06  6:34 [PATCH] btrfs-progs: balance: print warn msg in background Sidong Yang

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.