From patchwork Wed Sep 4 15:22:35 2013 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wang Shilong X-Patchwork-Id: 2853761 Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.19.201]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 0089E9F485 for ; Wed, 4 Sep 2013 15:23:58 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 175B4204D3 for ; Wed, 4 Sep 2013 15:23:53 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 58E66204CB for ; Wed, 4 Sep 2013 15:23:51 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S934998Ab3IDPXl (ORCPT ); Wed, 4 Sep 2013 11:23:41 -0400 Received: from mail-pa0-f47.google.com ([209.85.220.47]:45582 "EHLO mail-pa0-f47.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S934783Ab3IDPXj (ORCPT ); Wed, 4 Sep 2013 11:23:39 -0400 Received: by mail-pa0-f47.google.com with SMTP id kl13so575803pab.6 for ; Wed, 04 Sep 2013 08:23:39 -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:in-reply-to:references; bh=A7onoY/U6N5CX51BWjmi9eFU0aYsX640STFtSZQJjfw=; b=ZcRJQYfMiUlTe/LTDbM4+M8th/cxufjSaLqmZmwRvmGiHNadYg+GZZkyjZUjnuhcYx Mph8z/LlSor2l5cfLCzWOz5yBEdkoLWrm/P4rS9l3P0jRDECVe7S0XDDuyZ93Gpmmyfn vprLhBx2I2DR+qddjY5B7qmNq4xPnWcjc4Zefmdb9g1/5A4AjN8/6UjF5aHEylaKZrnI Rl1gRzJDoAPBpVmf2jjlLyFQg9pYx/qGzw7FcvcH3KOSJtGUK5ltFkV/xkUXPH7B8J6H uNyJRqrMThU4CrSSXatCiJKK/Gm6Ky6I7ohC4EqH1FppTP4Ttx2mpEm9rKwKsFk3a/8N N5Xw== X-Received: by 10.67.11.103 with SMTP id eh7mr3862650pad.153.1378308219176; Wed, 04 Sep 2013 08:23:39 -0700 (PDT) Received: from localhost.localdomain.localdomain ([223.65.191.73]) by mx.google.com with ESMTPSA id uw6sm29467271pbc.8.1969.12.31.16.00.00 (version=TLSv1 cipher=RC4-SHA bits=128/128); Wed, 04 Sep 2013 08:23:38 -0700 (PDT) From: Wang Shilong To: linux-btrfs@vger.kernel.org Cc: dsterba@suse.cz, sandeen@redhat.com Subject: [PATCH 18/20] Btrfs-progs: fix magic return value in cmds-balance.c Date: Wed, 4 Sep 2013 23:22:35 +0800 Message-Id: <1378308157-4621-19-git-send-email-wangshilong1991@gmail.com> X-Mailer: git-send-email 1.7.11.7 In-Reply-To: <1378308157-4621-1-git-send-email-wangshilong1991@gmail.com> References: <1378308157-4621-1-git-send-email-wangshilong1991@gmail.com> Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Spam-Status: No, score=-9.2 required=5.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RP_MATCHES_RCVD, T_DKIM_INVALID, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Wang Shilong If there is no balance in progress.resume/pause/cancel will return 2. For usage or syntal errors will return 1. 0 means operations return successfully. Signed-off-by: Wang Shilong --- cmds-balance.c | 93 ++++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 58 insertions(+), 35 deletions(-) diff --git a/cmds-balance.c b/cmds-balance.c index b7382ef..fd68051 100644 --- a/cmds-balance.c +++ b/cmds-balance.c @@ -58,7 +58,7 @@ static int parse_one_profile(const char *profile, u64 *flags) *flags |= BTRFS_AVAIL_ALLOC_BIT_SINGLE; } else { fprintf(stderr, "Unknown profile '%s'\n", profile); - return 1; + return -ENOENT; } return 0; @@ -68,12 +68,14 @@ static int parse_profiles(char *profiles, u64 *flags) { char *this_char; char *save_ptr = NULL; /* Satisfy static checkers */ + int ret; for (this_char = strtok_r(profiles, "|", &save_ptr); this_char != NULL; this_char = strtok_r(NULL, "|", &save_ptr)) { - if (parse_one_profile(this_char, flags)) - return 1; + ret = parse_one_profile(this_char, flags); + if (ret) + return ret; } return 0; @@ -86,7 +88,7 @@ static int parse_u64(const char *str, u64 *result) val = strtoull(str, &endptr, 10); if (*endptr) - return 1; + return -EINVAL; *result = val; return 0; @@ -95,6 +97,7 @@ static int parse_u64(const char *str, u64 *result) static int parse_range(const char *range, u64 *start, u64 *end) { char *dots; + int ret; dots = strstr(range, ".."); if (dots) { @@ -107,29 +110,31 @@ static int parse_range(const char *range, u64 *start, u64 *end) *end = (u64)-1; skipped++; } else { - if (parse_u64(rest, end)) - return 1; + ret = parse_u64(rest, end); + if (ret) + return ret; } if (dots == range) { *start = 0; skipped++; } else { + ret = parse_u64(rest, end); if (parse_u64(range, start)) - return 1; + return ret; } if (*start >= *end) { fprintf(stderr, "Range %llu..%llu doesn't make " "sense\n", (unsigned long long)*start, (unsigned long long)*end); - return 1; + return -EINVAL; } if (skipped <= 1) return 0; } - return 1; + return -EINVAL; } static int parse_filters(char *filters, struct btrfs_balance_args *args) @@ -137,6 +142,7 @@ static int parse_filters(char *filters, struct btrfs_balance_args *args) char *this_char; char *value; char *save_ptr = NULL; /* Satisfy static checkers */ + int ret = 0; if (!filters) return 0; @@ -150,70 +156,72 @@ static int parse_filters(char *filters, struct btrfs_balance_args *args) if (!value || !*value) { fprintf(stderr, "the profiles filter requires " "an argument\n"); - return 1; + return -EINVAL; } if (parse_profiles(value, &args->profiles)) { fprintf(stderr, "Invalid profiles argument\n"); - return 1; + return -EINVAL; } args->flags |= BTRFS_BALANCE_ARGS_PROFILES; } else if (!strcmp(this_char, "usage")) { if (!value || !*value) { fprintf(stderr, "the usage filter requires " "an argument\n"); - return 1; + return -EINVAL; } if (parse_u64(value, &args->usage) || args->usage > 100) { fprintf(stderr, "Invalid usage argument: %s\n", value); - return 1; + return -EINVAL; } args->flags |= BTRFS_BALANCE_ARGS_USAGE; } else if (!strcmp(this_char, "devid")) { if (!value || !*value) { fprintf(stderr, "the devid filter requires " "an argument\n"); - return 1; + return -EINVAL; } if (parse_u64(value, &args->devid) || args->devid == 0) { fprintf(stderr, "Invalid devid argument: %s\n", value); - return 1; + return -EINVAL; } args->flags |= BTRFS_BALANCE_ARGS_DEVID; } else if (!strcmp(this_char, "drange")) { if (!value || !*value) { fprintf(stderr, "the drange filter requires " "an argument\n"); - return 1; + return -EINVAL; } - if (parse_range(value, &args->pstart, &args->pend)) { + ret = parse_range(value, &args->pstart, &args->pend); + if (ret) { fprintf(stderr, "Invalid drange argument\n"); - return 1; + return ret; } args->flags |= BTRFS_BALANCE_ARGS_DRANGE; } else if (!strcmp(this_char, "vrange")) { if (!value || !*value) { fprintf(stderr, "the vrange filter requires " "an argument\n"); - return 1; + return -EINVAL; } - if (parse_range(value, &args->vstart, &args->vend)) { + ret = parse_range(value, &args->vstart, &args->vend); + if (ret) { fprintf(stderr, "Invalid vrange argument\n"); - return 1; + return ret; } args->flags |= BTRFS_BALANCE_ARGS_VRANGE; } else if (!strcmp(this_char, "convert")) { if (!value || !*value) { fprintf(stderr, "the convert option requires " "an argument\n"); - return 1; + return -EINVAL; } if (parse_one_profile(value, &args->target)) { fprintf(stderr, "Invalid convert argument\n"); - return 1; + return -EINVAL; } args->flags |= BTRFS_BALANCE_ARGS_CONVERT; } else if (!strcmp(this_char, "soft")) { @@ -221,7 +229,7 @@ static int parse_filters(char *filters, struct btrfs_balance_args *args) } else { fprintf(stderr, "Unrecognized balance option '%s'\n", this_char); - return 1; + return -EINVAL; } } @@ -297,9 +305,10 @@ static int do_balance(const char *path, struct btrfs_ioctl_balance_args *args, DIR *dirstream = NULL; fd = open_file_or_dir(path, &dirstream); + e = errno; if (fd < 0) { fprintf(stderr, "ERROR: can't access to '%s'\n", path); - return 12; + return e; } ret = ioctl(fd, BTRFS_IOC_BALANCE_V2, args); @@ -330,7 +339,7 @@ static int do_balance(const char *path, struct btrfs_ioctl_balance_args *args, if (e != EINPROGRESS) fprintf(stderr, "There may be more info in " "syslog - try dmesg | tail\n"); - ret = 19; + ret = 1; } } else { printf("Done, had to relocate %llu out of %llu chunks\n", @@ -370,6 +379,7 @@ static int cmd_balance_start(int argc, char **argv) int verbose = 0; int nofilters = 1; int i; + int ret = 0; memset(&args, 0, sizeof(args)); @@ -473,7 +483,8 @@ static int cmd_balance_start(int argc, char **argv) if (verbose) dump_ioctl_balance_args(&args); - return do_balance(argv[optind], &args, nofilters); + ret = do_balance(argv[optind], &args, nofilters); + return !!ret; } static const char * const cmd_balance_pause_usage[] = { @@ -498,7 +509,7 @@ static int cmd_balance_pause(int argc, char **argv) fd = open_file_or_dir(path, &dirstream); if (fd < 0) { fprintf(stderr, "ERROR: can't access to '%s'\n", path); - return 12; + return 1; } ret = ioctl(fd, BTRFS_IOC_BALANCE_CTL, BTRFS_BALANCE_CTL_PAUSE); @@ -508,7 +519,10 @@ static int cmd_balance_pause(int argc, char **argv) if (ret < 0) { fprintf(stderr, "ERROR: balance pause on '%s' failed - %s\n", path, (e == ENOTCONN) ? "Not running" : strerror(e)); - return 19; + if (e == ENOTCONN) + return 2; + else + return 1; } return 0; @@ -536,7 +550,7 @@ static int cmd_balance_cancel(int argc, char **argv) fd = open_file_or_dir(path, &dirstream); if (fd < 0) { fprintf(stderr, "ERROR: can't access to '%s'\n", path); - return 12; + return 1; } ret = ioctl(fd, BTRFS_IOC_BALANCE_CTL, BTRFS_BALANCE_CTL_CANCEL); @@ -546,7 +560,10 @@ static int cmd_balance_cancel(int argc, char **argv) if (ret < 0) { fprintf(stderr, "ERROR: balance cancel on '%s' failed - %s\n", path, (e == ENOTCONN) ? "Not in progress" : strerror(e)); - return 19; + if (e == ENOTCONN) + return 2; + else + return 1; } return 0; @@ -575,7 +592,7 @@ static int cmd_balance_resume(int argc, char **argv) fd = open_file_or_dir(path, &dirstream); if (fd < 0) { fprintf(stderr, "ERROR: can't access to '%s'\n", path); - return 12; + return 1; } memset(&args, 0, sizeof(args)); @@ -596,12 +613,15 @@ static int cmd_balance_resume(int argc, char **argv) "failed - %s\n", path, (e == ENOTCONN) ? "Not in progress" : "Already running"); - return 19; + if (e == ENOTCONN) + return 2; + else + return 1; } else { fprintf(stderr, "ERROR: error during balancing '%s' - %s\n" "There may be more info in syslog - try dmesg | tail\n", path, strerror(e)); - return 19; + return 1; } } else { printf("Done, had to relocate %llu out of %llu chunks\n", @@ -719,6 +739,8 @@ const struct cmd_group balance_cmd_group = { int cmd_balance(int argc, char **argv) { + int ret = 0; + if (argc == 2) { /* old 'btrfs filesystem balance ' syntax */ struct btrfs_ioctl_balance_args args; @@ -726,7 +748,8 @@ int cmd_balance(int argc, char **argv) memset(&args, 0, sizeof(args)); args.flags |= BTRFS_BALANCE_TYPE_MASK; - return do_balance(argv[1], &args, 1); + ret = do_balance(argv[1], &args, 1); + return !!ret; } return handle_command_group(&balance_cmd_group, argc, argv);