From patchwork Fri Nov 12 17:59:36 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: hugo-lkml@carfax.org.uk X-Patchwork-Id: 320362 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 oACHxoNr021887 for ; Fri, 12 Nov 2010 17:59:50 GMT Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753944Ab0KLR7q (ORCPT ); Fri, 12 Nov 2010 12:59:46 -0500 Received: from frost.carfax.org.uk ([212.13.194.111]:3270 "EHLO frost.carfax.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752919Ab0KLR7p (ORCPT ); Fri, 12 Nov 2010 12:59:45 -0500 Received: from intmx.carfax.org.uk ([10.0.0.5] helo=vlad.carfax.org.uk ident=Debian-exim) by frost.carfax.org.uk with esmtp (Exim 4.69) (envelope-from ) id 1PGxuU-0001AR-6V; Fri, 12 Nov 2010 17:59:43 +0000 Received: from selene.carfax.org.uk ([10.0.0.7]) by vlad.carfax.org.uk with smtp (Exim 4.72) (envelope-from ) id 1PGxuS-0007sQ-N8; Fri, 12 Nov 2010 17:59:37 +0000 Received: by selene.carfax.org.uk (sSMTP sendmail emulation); Fri, 12 Nov 2010 17:59:36 +0000 Date: Fri, 12 Nov 2010 17:59:36 +0000 From: Hugo Mills To: Chris Samuel , Li Zefan , linux-btrfs@vger.kernel.org, Goffredo Baroncelli , Chris Mason , liubo Subject: Re: [PATCH v2 2/2] Cancel filesystem balance. Message-ID: <20101112175936.GA2339@selene> Mail-Followup-To: Hugo Mills , Chris Samuel , Li Zefan , linux-btrfs@vger.kernel.org, Goffredo Baroncelli , Chris Mason , liubo References: <9b18b334410714aba2867f96e949f19243536ddc.1289522188.git.hugo@carfax.org.uk> <4CDC9971.6080903@cn.fujitsu.com> <4CDCC258.1050402@csamuel.org> <20101112113655.GA2377@selene> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <20101112113655.GA2377@selene> X-GPG-Fingerprint: 8C59 86C7 81F3 93FE BB02 DDB1 20AC B3BE 515C 238D X-GPG-Key: 515C238D X-Parrot: It is no more. It has joined the choir invisible. X-IRC-Nicks: darksatanic darkersatanic darkling darkthing User-Agent: Mutt/1.5.20 (2009-06-14) 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: On Fri, Nov 12, 2010 at 11:36:55AM +0000, Hugo Mills wrote: > On Fri, Nov 12, 2010 at 03:28:08PM +1100, Chris Samuel wrote: > > On 12/11/10 12:33, Li Zefan wrote: > > > > > Is there any blocker that prevents us from canceling balance > > > by just Ctrl+C ? > > > > Given that there's been at least 1 report of it taking 12 hours > > to balance a non-trivial amount of data I suspect putting this > > operation into the background by default and having the cancel > > option might be a better plan. > > Only 12 hours? Last time I tried it, it took 19. :) > > It would certainly be easy enough to fork a copy of the userspace > tool to run the ioctl in the background. Probably a little more work > to make the balance a kernel thread. I'd prefer the former, for > ease of implementation. [...] 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.3 (demeter1.kernel.org [140.211.167.41]); Fri, 12 Nov 2010 17:59:50 +0000 (UTC) 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..13be603 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,8 +796,29 @@ 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); + printf("ioctl\n"); + sleep(60); + /* ret = ioctl(fdmnt, BTRFS_IOC_BALANCE, &args); */ close(fdmnt); if(ret<0){ fprintf(stderr, "ERROR: balancing '%s'\n", path); diff --git a/man/btrfs.8.in b/man/btrfs.8.in index 3f7642e..1410aaa 100644 --- a/man/btrfs.8.in +++ b/man/btrfs.8.in @@ -27,7 +27,7 @@ btrfs \- control a btrfs filesystem .PP \fBbtrfs\fP \fBdevice show\fP\fI |