From patchwork Tue Jun 21 15:16:59 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Austin S. Hemmelgarn" X-Patchwork-Id: 9190829 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id E7BC7601C0 for ; Tue, 21 Jun 2016 15:19:52 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id D5B2C2818B for ; Tue, 21 Jun 2016 15:19:52 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id CA2962823D; Tue, 21 Jun 2016 15:19:52 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, T_DKIM_INVALID autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 464AC2818B for ; Tue, 21 Jun 2016 15:19:52 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752245AbcFUPSK (ORCPT ); Tue, 21 Jun 2016 11:18:10 -0400 Received: from mail-it0-f65.google.com ([209.85.214.65]:34681 "EHLO mail-it0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751594AbcFUPSH (ORCPT ); Tue, 21 Jun 2016 11:18:07 -0400 Received: by mail-it0-f65.google.com with SMTP id f6so2559721ith.1 for ; Tue, 21 Jun 2016 08:17:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id; bh=X+Zj0VbiTrc4kYdzFrJj48/7dJCjyFIsTSHJT26acKw=; b=THF1Za1UeJlSu1/SS3uEXZI1ZwivSiyr+caxP3ILt8CGqxiLYoxwo7upej9/+anVWY Lb3XQb0XJW2W1Hq1MFNZXAKKjePl9R8KD1J0huKqWzv/s9BjXtYxsYtA+X4GdvEasdKM KcG5kfM1nFZrbk0nMK9Fr+OnoAojT9rNQ2RPUvxCv7XT/I+/Te98ObLayxR2TCimtKH8 MTp1u8CCGrf7YWYEAI/BRVXZuaxxES/e2fS5wpBn1BFWSCD0WFJ6ZNswO1wtmfQdJsSO OSUgEbnD8QlAH520HOk2DQRD6ANOi9RBYaNyb22pm9MJSimMPPWIkwGcXmmvCInVkBwp f3Bw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=X+Zj0VbiTrc4kYdzFrJj48/7dJCjyFIsTSHJT26acKw=; b=Es8jZZCIu4EaqvhZUshQ0WgWMxP2aAirEG5G4xOC8mlH9mYlxfrMMQ6hJd/5Ow0ShR VnfHJ70edA4ddGnSXTL4nAnHJFVqHNmWh+rcDqrxmV/Ie5oSqxLFlhXJ+zkoa+SHQw9F w0b+6yerYN4q2RmRyZsiqUX7Hehrr4Pf25EZFcrl6Z6aOZEOrIUNFoXEbc/A0+8QYxmV vc+ht+6wbnw5do763Lr8mBcyDf1G3y44EYDiKrk7HAzmebSctRAFVeO2RsJt+8q060aw xHj4THajKpX4xCcjqYjDC+F7OC7hyNFrMPxKB0l6Oi3vVHAbnH7ma2lWc6wXVtq2riAe F6Yw== X-Gm-Message-State: ALyK8tIOjbGWL1+n2Vxskq8ZNhIM1yPlZfaoavcG1jTDnjmK+B9O0+lVAzy9/kyWcXsISA== X-Received: by 10.36.124.66 with SMTP id a63mr6201018itd.17.1466522224851; Tue, 21 Jun 2016 08:17:04 -0700 (PDT) Received: from wild-karde.localdomain (rrcs-70-62-41-24.central.biz.rr.com. [70.62.41.24]) by smtp.googlemail.com with ESMTPSA id b17sm32288167ioe.3.2016.06.21.08.17.03 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Tue, 21 Jun 2016 08:17:03 -0700 (PDT) Received: by wild-karde.localdomain (Postfix, from userid 1000) id CBD3B1B30CD9; Tue, 21 Jun 2016 11:17:01 -0400 (EDT) From: "Austin S. Hemmelgarn" To: linux-btrfs@vger.kernel.org, dsterba@suse.com Cc: "Austin S. Hemmelgarn" Subject: [PATCH] btrfs-progs: add option to run balance as daemon Date: Tue, 21 Jun 2016 11:16:59 -0400 Message-Id: <20160621151659.32157-1-ahferroin7@gmail.com> X-Mailer: git-send-email 2.9.0 Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP Currently, balance operations are run synchronously in the foreground. This is nice for interactive management, but is kind of crappy when you start looking at automation and similar things. This patch adds an option to `btrfs balance start` to tell it to daemonize prior to running the balance operation, thus allowing us to preform balances asynchronously. The two biggest use cases I have for this are starting a balance on a remote server without establishing a full shell session, and being able to background the balance in a recovery shell (which usually has no job control) so I can still get progress information. Because it simply daemonizes prior to calling the balance ioctl, this doesn't actually need any kernel support. Signed-off-by: Austin S. Hemmelgarn --- This works as is, but there are two specific things I would love to eventually fix but don't have the time to fix right now: * There is no way to get any feedback from the balance operation. * Because of how everything works, trying to start a new balance with --background while one iw already running won't return an error but won't queue or start a new balance either. The first one is more a utility item than anything else, and probably would not be hard to add. Ideally, it should be output to a user specified file, and this should work even for a normal foreground balance. The second is very much a UX issue, but can't be easily sovled without doing some creative process monitoring from the parrent processes. Documentation/btrfs-balance.asciidoc | 2 ++ cmds-balance.c | 43 +++++++++++++++++++++++++++++++++++- 2 files changed, 44 insertions(+), 1 deletion(-) diff --git a/Documentation/btrfs-balance.asciidoc b/Documentation/btrfs-balance.asciidoc index 7df40b9..f487dbb 100644 --- a/Documentation/btrfs-balance.asciidoc +++ b/Documentation/btrfs-balance.asciidoc @@ -85,6 +85,8 @@ act on system chunks (requires '-f'), see `FILTERS` section for details about 'f be verbose and print balance filter arguments -f:::: force reducing of metadata integrity, eg. when going from 'raid1' to 'single' +--background:::: +run the balance operation asynchronously in the background *status* [-v] :: Show status of running or paused balance. diff --git a/cmds-balance.c b/cmds-balance.c index 708bbf4..66169b7 100644 --- a/cmds-balance.c +++ b/cmds-balance.c @@ -20,6 +20,9 @@ #include #include #include +#include +#include +#include #include #include "kerncompat.h" @@ -510,6 +513,7 @@ static const char * const cmd_balance_start_usage[] = { "-v be verbose", "-f force reducing of metadata integrity", "--full-balance do not print warning and do not delay start", + "--background run the balance as a background process", NULL }; @@ -520,6 +524,7 @@ static int cmd_balance_start(int argc, char **argv) &args.meta, NULL }; int force = 0; int verbose = 0; + int background = 0; unsigned start_flags = 0; int i; @@ -527,7 +532,8 @@ static int cmd_balance_start(int argc, char **argv) optind = 1; while (1) { - enum { GETOPT_VAL_FULL_BALANCE = 256 }; + enum { GETOPT_VAL_FULL_BALANCE = 256, + GETOPT_VAL_BACKGROUND = 257 }; static const struct option longopts[] = { { "data", optional_argument, NULL, 'd'}, { "metadata", optional_argument, NULL, 'm' }, @@ -536,6 +542,8 @@ static int cmd_balance_start(int argc, char **argv) { "verbose", no_argument, NULL, 'v' }, { "full-balance", no_argument, NULL, GETOPT_VAL_FULL_BALANCE }, + { "background", no_argument, NULL, + GETOPT_VAL_BACKGROUND }, { NULL, 0, NULL, 0 } }; @@ -574,6 +582,9 @@ static int cmd_balance_start(int argc, char **argv) case GETOPT_VAL_FULL_BALANCE: start_flags |= BALANCE_START_NOWARN; break; + case GETOPT_VAL_BACKGROUND: + background = 1; + break; default: usage(cmd_balance_start_usage); } @@ -626,6 +637,36 @@ static int cmd_balance_start(int argc, char **argv) args.flags |= BTRFS_BALANCE_FORCE; if (verbose) dump_ioctl_balance_args(&args); + if (background) { + switch (fork()) { + case (-1): + error("Unable to fork to run balance in background"); + return 1; + break; + case (0): + setsid(); + switch(fork()) { + case (-1): + error("Unable to fork to run balance in background"); + exit(1); + break; + case (0): + 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); }