From patchwork Wed Apr 8 13:15:09 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Petr Janecek X-Patchwork-Id: 11480103 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id A8BB892C for ; Wed, 8 Apr 2020 13:15:14 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 931D320747 for ; Wed, 8 Apr 2020 13:15:14 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728526AbgDHNPO (ORCPT ); Wed, 8 Apr 2020 09:15:14 -0400 Received: from mail-wm1-f66.google.com ([209.85.128.66]:54787 "EHLO mail-wm1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726709AbgDHNPN (ORCPT ); Wed, 8 Apr 2020 09:15:13 -0400 Received: by mail-wm1-f66.google.com with SMTP id h2so5093710wmb.4 for ; Wed, 08 Apr 2020 06:15:12 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:subject:message-id:mime-version :content-disposition:user-agent; bh=eGB+z88QBKI4z7bpY2ndD948lGvPF1y9bISTrgy54ew=; b=DSJN+D3Y447gnDbuY7RghbK3npUbF4cU3PgwQo+aGJaSal+R05BgBIN01PL7hcat9b Y8T7NI13ILHviHvdwEl3MVSsi6Th6mCRUaH/QI7ppx1CyUolIKsQLBHmKynEpQvs3Pr3 bv9cGhYITjqxASprljvZWetk/u3/x2Gn377EvhrcwsX+Wm4w93gE9Yb8O5h9Dq9XQi4Y d1GoV2u3lXfLMHS7Rn6gcK8A5vFLRsGZ4XXBS/lkoyB5Zap60OP/o6wz3E9KADI+WkoK gAjC02/sgFvz/+Aifp4xZ+2UF4vud0Q/CbwqdAYvhmRpi7VU4Ab1IxXA3+j0JUmOMZXe Yjpw== X-Gm-Message-State: AGi0Pubw0iSugG3S3IIKk6wBqmiN1B/okVYQt6Pgvi8e5Ec4eKUN312I 6i0W7q5Wv5mSTA51anG208BLCPLc X-Google-Smtp-Source: APiQypJYGGvUOwMZh871wS89FB0yYFs4GikOYOzfTEcC20rSFI7mrMMAgJt4PEV5TiBlZslZAe8r1g== X-Received: by 2002:a1c:9a87:: with SMTP id c129mr4365173wme.149.1586351711373; Wed, 08 Apr 2020 06:15:11 -0700 (PDT) Received: from jkm (79-98-75-217.sys-data.com. [79.98.75.217]) by smtp.gmail.com with ESMTPSA id n6sm6926941wmn.10.2020.04.08.06.15.10 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Apr 2020 06:15:10 -0700 (PDT) Date: Wed, 8 Apr 2020 15:15:09 +0200 From: Petr Janecek To: linux-btrfs@vger.kernel.org Subject: [RFC PATCH 1/3] btrfs-progs: move '--background' fork()s to do_balance() Message-ID: <20200408131508.GB19101@jkm> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org Move the '--background' forking to happen after btrfs_open_dir(), so that the possible 'wrong path' error message can be seen the user. --- cmds/balance.c | 78 ++++++++++++++++++++++++++------------------------ 1 file changed, 40 insertions(+), 38 deletions(-) diff --git a/cmds/balance.c b/cmds/balance.c index 5392a604..fada2ab3 100644 --- a/cmds/balance.c +++ b/cmds/balance.c @@ -426,8 +426,9 @@ static int do_balance_v1(int fd) } enum { - BALANCE_START_FILTERS = 1 << 0, - BALANCE_START_NOWARN = 1 << 1 + BALANCE_START_FILTERS = 1 << 0, + BALANCE_START_NOWARN = 1 << 1, + BALANCE_START_BACKGROUND = 1 << 2 }; static int do_balance(const char *path, struct btrfs_ioctl_balance_args *args, @@ -436,11 +437,47 @@ static int do_balance(const char *path, struct btrfs_ioctl_balance_args *args, int fd; int ret; DIR *dirstream = NULL; + int i __attribute__((unused)); fd = btrfs_open_dir(path, &dirstream, 1); if (fd < 0) return 1; + 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. + */ + 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); + } + } + ret = ioctl(fd, BTRFS_IOC_BALANCE_V2, args); if (ret < 0) { /* @@ -510,7 +547,6 @@ static int cmd_balance_start(const struct cmd_struct *cmd, &args.meta, NULL }; int force = 0; int verbose = 0; - int background = 0; unsigned start_flags = 0; int i; @@ -570,7 +606,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; default: usage_unknown_option(cmd, argv); @@ -642,40 +678,6 @@ static int cmd_balance_start(const struct cmd_struct *cmd, 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; - 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); } From patchwork Wed Apr 8 13:15:19 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Petr Janecek X-Patchwork-Id: 11480105 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 9667F92C for ; Wed, 8 Apr 2020 13:15:24 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 70C2220747 for ; Wed, 8 Apr 2020 13:15:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728568AbgDHNPX (ORCPT ); Wed, 8 Apr 2020 09:15:23 -0400 Received: from mail-wr1-f66.google.com ([209.85.221.66]:41111 "EHLO mail-wr1-f66.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726709AbgDHNPX (ORCPT ); Wed, 8 Apr 2020 09:15:23 -0400 Received: by mail-wr1-f66.google.com with SMTP id h9so7766579wrc.8 for ; Wed, 08 Apr 2020 06:15:22 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:subject:message-id:mime-version :content-disposition:user-agent; bh=q5NRqQ/Pd7yu7xBdLNTwU7Nrqby8mAe+y6vZEBSVJ5o=; b=YOxSpxq4UcAuqgwbEtJTFkLmkw0O6IePuC6bF6QrvZjQ+tGRQq/vKaNdcP+hSvvhAh 8BkJ/ClyHElgNpCHar678dQ6xrqLN9GywMR5aJymrHwcM2UqXuraVrBFw2npNQ1+8mPI WwyhdQSz5qTNbcLnR/ngdCOjjLDV5XbbmaAMNYpFQ+00XuVNiFNR1HEO2kQhql4XdNR/ 8AKoPw/LfXF1PQJmDkna0ZPjFv4owpU2PXLYG+QGlqEP02eJjdtXnYHkXSpw2Wru4Sxq A9RLko792+bWaB/gwjQycOq3dXbyIe8AXJ4F01pwcFiP1LbOobmUym9O7Hh7o9ALxHk3 GcEA== X-Gm-Message-State: AGi0PuZcNhBbOgr+oUtkBrxfIfG3qQZmmx6pqrJNzImzJOC2gJVLV205 bUHtgVRmBxyU6sgvISl6puyP8W4V X-Google-Smtp-Source: APiQypKAzvd4TvFEOfHXK/0yaVJ9lZgPHlB7a3fUNeSb1zhLFeL4PJUNC8FgCcz+uh7oGhsuHbMKCQ== X-Received: by 2002:adf:d4ce:: with SMTP id w14mr8649481wrk.135.1586351721054; Wed, 08 Apr 2020 06:15:21 -0700 (PDT) Received: from jkm (79-98-75-217.sys-data.com. [79.98.75.217]) by smtp.gmail.com with ESMTPSA id b191sm6573999wmd.39.2020.04.08.06.15.20 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Apr 2020 06:15:20 -0700 (PDT) Date: Wed, 8 Apr 2020 15:15:19 +0200 From: Petr Janecek To: linux-btrfs@vger.kernel.org Subject: [RFC PATCH 2/3] btrfs_progs: _PROGRESS ioctl for error output in do_balance() Message-ID: <20200408131517.GC19101@jkm> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org With 'btrfs balance start --background ...', all the potential error messages are lost. Add ioctl() to check for basic permission denied and balance in progress conditions. --- cmds/balance.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/cmds/balance.c b/cmds/balance.c index fada2ab3..3d4deb27 100644 --- a/cmds/balance.c +++ b/cmds/balance.c @@ -444,6 +444,24 @@ static int do_balance(const char *path, struct btrfs_ioctl_balance_args *args, return 1; if (flags & BALANCE_START_BACKGROUND) { + /* + * We are not going to see any error output from the + * forked process. So do a sanity check that the + * balance is likely to start. + */ + struct btrfs_ioctl_balance_args stat_args; + ret = ioctl(fd, BTRFS_IOC_BALANCE_PROGRESS, &stat_args); + if (ret == 0) { + error("error during balancing '%s': " + "Operation now in progress", path); + ret = 1; + goto out; + } else if (errno != ENOTCONN) { + error("error during balancing '%s': %m", path); + ret = 1; + goto out; + } + switch (fork()) { case (-1): error("unable to fork to run balance in background"); From patchwork Wed Apr 8 13:15:28 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Petr Janecek X-Patchwork-Id: 11480107 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 2C86D1805 for ; Wed, 8 Apr 2020 13:15:34 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 1469F20747 for ; Wed, 8 Apr 2020 13:15:34 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728634AbgDHNPd (ORCPT ); Wed, 8 Apr 2020 09:15:33 -0400 Received: from mail-wr1-f67.google.com ([209.85.221.67]:39981 "EHLO mail-wr1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726709AbgDHNPc (ORCPT ); Wed, 8 Apr 2020 09:15:32 -0400 Received: by mail-wr1-f67.google.com with SMTP id s8so7756114wrt.7 for ; Wed, 08 Apr 2020 06:15:31 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:subject:message-id:mime-version :content-disposition:user-agent; bh=JOSl5eYZlfMo8mpeMGlcc623ymS2nHMK+RAOQKnLx+U=; b=MME9xI9pYRA4gpYBEHBVbE6By3gTutTrDPWf/rSFlLLp0W5dCtBVzD5dxFA5NroL6q YDOlMSKqJMLmiXKrH9UG02ycMiJoX5BwTB0N9q1FBM91f701cWFEe0U86+TP5jTvcDhi PN2seNOJNc+DTiiLNSzupaDzGW0LOf+xUQkX5BiClOJ7Q9dzgEXzx/L5qNrJKpnSAWyl PaGF6YRpFEhoFo2t4CkM8QX2yI6VJ3GjHzcynyDqW1uV2sgBkAjX5SQNjfN8v1PXoHTT lH8NQ1GWol6L52EpdhlFhutUbfRE3KWyu4pZnV9dMzClVQm4NI2HLAV5MHFBzjURU8jA jvsg== X-Gm-Message-State: AGi0PuabdwOi96TvOCmZte5DMyZ/A4fZ9MLtfbM8Ahn+6d6oPSrl5la0 sHIsd0QhpWI4z8pMy7xNNlWq0l+Y X-Google-Smtp-Source: APiQypJvvgPSW8OAnG1YBbPN6X5Mf2p+6P9/oMePiDyfHUvNdMp33ojNMsc/ZE+EFJe6M1BIjauWIA== X-Received: by 2002:adf:f8c1:: with SMTP id f1mr8630901wrq.345.1586351730445; Wed, 08 Apr 2020 06:15:30 -0700 (PDT) Received: from jkm (79-98-75-217.sys-data.com. [79.98.75.217]) by smtp.gmail.com with ESMTPSA id j31sm30248234wre.36.2020.04.08.06.15.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 08 Apr 2020 06:15:29 -0700 (PDT) Date: Wed, 8 Apr 2020 15:15:28 +0200 From: Petr Janecek To: linux-btrfs@vger.kernel.org Subject: [RFC PATCH 3/3] btrfs-progs: add '--background' option for '--full-balance' Message-ID: <20200408131527.GD19101@jkm> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.10.1 (2018-07-13) Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org Add the '--background' option to 'btrfs balance --full-balance' (without the 'start'). --- cmds/balance.c | 29 ++++++++++++++++++++++++++++- 1 file changed, 28 insertions(+), 1 deletion(-) diff --git a/cmds/balance.c b/cmds/balance.c index 3d4deb27..506016ba 100644 --- a/cmds/balance.c +++ b/cmds/balance.c @@ -939,11 +939,38 @@ static DEFINE_SIMPLE_COMMAND(balance_status, "status"); static int cmd_balance_full(const struct cmd_struct *cmd, int argc, char **argv) { struct btrfs_ioctl_balance_args args; + unsigned start_flags = BALANCE_START_NOWARN; memset(&args, 0, sizeof(args)); args.flags |= BTRFS_BALANCE_TYPE_MASK; - return do_balance(argv[1], &args, BALANCE_START_NOWARN); + optind = 0; + while (1) { + enum { GETOPT_VAL_BACKGROUND = 256 }; + static const struct option longopts[] = { + { "background", no_argument, NULL, + GETOPT_VAL_BACKGROUND }, + { "bg", no_argument, NULL, GETOPT_VAL_BACKGROUND }, + { NULL, 0, NULL, 0 } + }; + + int opt = getopt_long(argc, argv, "", longopts, NULL); + if (opt < 0) + break; + + switch (opt) { + case GETOPT_VAL_BACKGROUND: + start_flags |= BALANCE_START_BACKGROUND; + break; + default: + usage_unknown_option(cmd, argv); + } + } + + if (check_argc_exact(argc - optind, 1)) + return 1; + + return do_balance(argv[optind], &args, start_flags); } static DEFINE_COMMAND(balance_full, "--full-balance", cmd_balance_full, NULL, NULL, CMD_HIDDEN);