From patchwork Sun Apr 10 21:16:30 2011 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hugo Mills X-Patchwork-Id: 697181 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by demeter1.kernel.org (8.14.4/8.14.3) with ESMTP id p3ALGdFg024061 for ; Sun, 10 Apr 2011 21:16:52 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1758021Ab1DJVQq (ORCPT ); Sun, 10 Apr 2011 17:16:46 -0400 Received: from frost.carfax.org.uk ([212.13.194.111]:1742 "EHLO frost.carfax.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1758005Ab1DJVQg (ORCPT ); Sun, 10 Apr 2011 17:16:36 -0400 Received: from ruthven.carfax.org.uk ([10.0.0.10]) by frost.carfax.org.uk with esmtp (Exim 4.69) (envelope-from ) id 1Q91zn-0005Al-4h; Sun, 10 Apr 2011 21:16:35 +0000 Received: from [10.0.0.10] (helo=ruthven.carfax.org.uk) by ruthven.carfax.org.uk with esmtp (Exim 4.72) (envelope-from ) id 1Q91zm-0003TJ-Sa; Sun, 10 Apr 2011 22:16:34 +0100 From: Hugo Mills To: chris.mason@oracle.com, linux-btrfs@vger.kernel.org Subject: [PATCH v5 4/8] Run userspace tool in background for balances. Date: Sun, 10 Apr 2011 22:16:30 +0100 Message-Id: X-Mailer: git-send-email 1.7.2.5 In-Reply-To: References: In-Reply-To: References: To: linux-btrfs@vger.kernel.org X-frost.carfax.org.uk-Spam-Score: -0.0 (/) X-frost.carfax.org.uk-Spam-Report: Spam detection software, running on the system "spamd2.lon.bitfolk.com", has identified this incoming email as possible spam. The original message has been attached to this so you can view it (if it isn't spam) or label similar future email. If you have any questions, see the administrator of that system for details. Content preview: This patch makes a balance operation fork and detach from the current terminal, to run the userspace side of the balance in the background. Introduce a --wait switch so that a synchronous balance can be done if the user requires. [...] Content analysis details: (-0.0 points, 5.0 required) pts rule name description ---- ---------------------- -------------------------------------------------- -0.0 T_RP_MATCHES_RCVD Envelope sender domain matches handover relay domain Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Greylist: IP, sender and recipient auto-whitelisted, not delayed by milter-greylist-4.2.6 (demeter1.kernel.org [140.211.167.41]); Sun, 10 Apr 2011 21:16:52 +0000 (UTC) This patch makes a balance operation fork and detach from the current terminal, to run the userspace side of the balance in the background. Introduce a --wait switch so that a synchronous balance can be done if the user requires. Signed-off-by: Hugo Mills --- btrfs.c | 8 ++++---- btrfs_cmds.c | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++-- man/btrfs.8.in | 19 ++++++++++--------- 3 files changed, 64 insertions(+), 15 deletions(-) diff --git a/btrfs.c b/btrfs.c index 93f7886..7b42658 100644 --- a/btrfs.c +++ b/btrfs.c @@ -91,12 +91,12 @@ static struct Command commands[] = { "filesystem df", "\n" "Show space usage information for a mount point\n." }, - { do_balance, 1, - "filesystem balance", "\n" + { do_balance, -1, + "filesystem balance", "[-w|--wait] \n" "Balance the chunks across the device." }, - { do_balance, 1, - "balance start", "\n" + { do_balance, -1, + "balance start", "[-w|--wait] \n" "Synonym for \"btrfs filesystem balance\"." }, { do_balance_progress, -1, diff --git a/btrfs_cmds.c b/btrfs_cmds.c index d246a8b..fadcb4f 100644 --- a/btrfs_cmds.c +++ b/btrfs_cmds.c @@ -754,12 +754,41 @@ int do_add_volume(int nargs, char **args) } +const struct option balance_options[] = { + { "wait", 0, NULL, 'w' }, + { NULL, 0, NULL, 0 } +}; + int do_balance(int argc, char **argv) { - int fdmnt, ret=0; + int background = 1; struct btrfs_ioctl_vol_args args; - char *path = argv[1]; + char *path; + int ttyfd; + + optind = 1; + while(1) { + int c = getopt_long(argc, argv, "w", balance_options, NULL); + if (c < 0) + break; + switch(c) { + case 'w': + background = 0; + break; + default: + fprintf(stderr, "Invalid arguments for balance\n"); + free(argv); + return 1; + } + } + + if(optind >= argc) { + fprintf(stderr, "No filesystem path given for balance\n"); + return 1; + } + + path = argv[optind]; fdmnt = open_file_or_dir(path); if (fdmnt < 0) { @@ -767,6 +796,25 @@ int do_balance(int argc, char **argv) return 12; } + if (background) { + int pid = fork(); + if (pid == 0) { + /* We're in the child, and can run in the background */ + ttyfd = open("/dev/tty", O_RDWR); + if (ttyfd > 0) + ioctl(ttyfd, TIOCNOTTY, 0); + /* Fall through to the BTRFS_IOC_BALANCE ioctl */ + } else if (pid > 0) { + /* We're in the parent, and the fork succeeded */ + printf("Background balance started\n"); + return 0; + } else { + /* We're in the parent, and the fork failed */ + fprintf(stderr, "ERROR: can't start background process -- %s\n", + strerror(errno)); + } + } + memset(&args, 0, sizeof(args)); ret = ioctl(fdmnt, BTRFS_IOC_BALANCE, &args); close(fdmnt); diff --git a/man/btrfs.8.in b/man/btrfs.8.in index d7fab80..95e39c3 100644 --- a/man/btrfs.8.in +++ b/man/btrfs.8.in @@ -23,13 +23,15 @@ btrfs \- control a btrfs filesystem .PP \fBbtrfs\fP \fBdevice scan\fP\fI [ [..]]\fP .PP +\fBbtrfs\fP \fBbalance start\fP [\fB-w\fP|\fB--wait\fP] \fI\fP +.PP \fBbtrfs\fP \fBbalance progress\fP [\fB-m\fP|\fB--monitor\fP] \fI\fP .PP \fBbtrfs\fP \fBbalance cancel\fP \fI\fP .PP \fBbtrfs\fP \fBdevice show\fP\fI |