public inbox for linux-btrfs@vger.kernel.org
 help / color / mirror / Atom feed
From: Marcos Paulo de Souza <marcos.souza.org@gmail.com>
To: unlisted-recipients:; (no To-header on input)
Cc: dsterba@suse.com, linux-btrfs@vger.kernel.org,
	mpdesouza@suse.com, anand.jain@oracle.com
Subject: [PATCH btrfs-progs 2/2] btrfs-progs: balance: Verify EINPROGRESS on background balance
Date: Wed, 30 Oct 2019 20:36:41 -0300	[thread overview]
Message-ID: <20191030233641.30123-3-marcos.souza.org@gmail.com> (raw)
In-Reply-To: <20191030233641.30123-1-marcos.souza.org@gmail.com>

From: Marcos Paulo de Souza <mpdesouza@suse.com>

Introduce a sleep of 3 seconds after triggering balance to check if
isn't there another balance already being executed.

Fix: #167

Signed-off-by: Marcos Paulo de Souza <mpdesouza@suse.com>
---
 cmds/balance.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

diff --git a/cmds/balance.c b/cmds/balance.c
index f0394a2e..2accc102 100644
--- a/cmds/balance.c
+++ b/cmds/balance.c
@@ -22,6 +22,7 @@
 #include <sys/ioctl.h>
 #include <sys/types.h>
 #include <sys/stat.h>
+#include <sys/wait.h>
 #include <fcntl.h>
 #include <errno.h>
 
@@ -507,6 +508,7 @@ static int cmd_balance_start(const struct cmd_struct *cmd,
 	int force = 0;
 	int verbose = 0;
 	int background = 0;
+	pid_t child;
 	unsigned start_flags = 0;
 	int i;
 
@@ -639,13 +641,13 @@ static int cmd_balance_start(const struct cmd_struct *cmd,
 	if (verbose)
 		dump_ioctl_balance_args(&args);
 	if (background) {
-		switch (fork()) {
+		switch (child = fork()) {
 		case (-1):
 			error("unable to fork to run balance in background");
 			return 1;
 		case (0):
 			setsid();
-			switch(fork()) {
+			switch(child = fork()) {
 			case (-1):
 				error(
 				"unable to fork to run balance in background");
@@ -663,10 +665,21 @@ static int cmd_balance_start(const struct cmd_struct *cmd,
 				open("/dev/null", O_WRONLY);
 				break;
 			default:
+				/* wait up to three seconds to check if balance
+				 * isn't already running */
+				i = 0;
+				while (waitpid(child, NULL, WNOHANG) == 0 && i++ < 3)
+					sleep(1);
+
+				/* ensure that any error message from
+				 * do_balance is flushed */
+				fflush(stderr);
 				exit(0);
 			}
 			break;
 		default:
+			/* Wait for the first child to return */
+			waitpid(child, NULL, 0);
 			exit(0);
 		}
 	}
-- 
2.23.0


  parent reply	other threads:[~2019-10-30 23:34 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-10-30 23:36 [PATCH btrfs-progs 0/2] balance: check balance errors on background Marcos Paulo de Souza
2019-10-30 23:36 ` [PATCH btrfs-progs 1/2] btrfs-progs: balance: Don't set stderr to /dev/null on balance_start Marcos Paulo de Souza
2019-10-30 23:36 ` Marcos Paulo de Souza [this message]
2019-10-31 10:00 ` [PATCH btrfs-progs 0/2] balance: check balance errors on background Anand Jain
2019-11-15 12:18   ` David Sterba

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20191030233641.30123-3-marcos.souza.org@gmail.com \
    --to=marcos.souza.org@gmail.com \
    --cc=anand.jain@oracle.com \
    --cc=dsterba@suse.com \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=mpdesouza@suse.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox