From patchwork Mon Dec 10 09:49:59 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Wilck X-Patchwork-Id: 10720965 X-Patchwork-Delegate: christophe.varoqui@free.fr Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 8924E112E for ; Mon, 10 Dec 2018 09:51:02 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 7745A29C61 for ; Mon, 10 Dec 2018 09:51:02 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 6B5FF29C04; Mon, 10 Dec 2018 09:51:02 +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=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) (using TLSv1.2 with cipher DHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 92F9429BCB for ; Mon, 10 Dec 2018 09:51:01 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 9BB9C811DD; Mon, 10 Dec 2018 09:51:00 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 705471054FD3; Mon, 10 Dec 2018 09:51:00 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 304533F608; Mon, 10 Dec 2018 09:51:00 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id wBA9osGW010927 for ; Mon, 10 Dec 2018 04:50:54 -0500 Received: by smtp.corp.redhat.com (Postfix) id 014775DD8B; Mon, 10 Dec 2018 09:50:54 +0000 (UTC) Delivered-To: dm-devel@redhat.com Received: from mx1.redhat.com (ext-mx20.extmail.prod.ext.phx2.redhat.com [10.5.110.49]) by smtp.corp.redhat.com (Postfix) with ESMTPS id E1EC517CE9; Mon, 10 Dec 2018 09:50:53 +0000 (UTC) Received: from smtp2.provo.novell.com (smtp2.provo.novell.com [137.65.250.81]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 4B9F4307CDC5; Mon, 10 Dec 2018 09:50:52 +0000 (UTC) Received: from apollon.suse.de.de (prv-ext-foundry1int.gns.novell.com [137.65.251.240]) by smtp2.provo.novell.com with ESMTP (TLS encrypted); Mon, 10 Dec 2018 02:50:49 -0700 From: Martin Wilck To: Christophe Varoqui Date: Mon, 10 Dec 2018 10:49:59 +0100 Message-Id: <20181210094959.11338-25-mwilck@suse.com> In-Reply-To: <20181210094959.11338-1-mwilck@suse.com> References: <20181210094959.11338-1-mwilck@suse.com> MIME-Version: 1.0 X-Greylist: Sender passed SPF test, Sender IP whitelisted by DNSRBL, ACL 216 matched, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.49]); Mon, 10 Dec 2018 09:50:52 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.49]); Mon, 10 Dec 2018 09:50:52 +0000 (UTC) for IP:'137.65.250.81' DOMAIN:'smtp2.provo.novell.com' HELO:'smtp2.provo.novell.com' FROM:'mwilck@suse.com' RCPT:'' X-RedHat-Spam-Score: -2.301 (RCVD_IN_DNSWL_MED, SPF_PASS) 137.65.250.81 smtp2.provo.novell.com 137.65.250.81 smtp2.provo.novell.com X-Scanned-By: MIMEDefang 2.84 on 10.5.110.49 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-loop: dm-devel@redhat.com Cc: dm-devel@redhat.com, Martin Wilck Subject: [dm-devel] [PATCH v3 24/24] multipath: use symbolic return value and exit code X-BeenThere: dm-devel@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: device-mapper development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Sender: dm-devel-bounces@redhat.com Errors-To: dm-devel-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Mon, 10 Dec 2018 09:51:01 +0000 (UTC) X-Virus-Scanned: ClamAV using ClamSMTP Use an enum to represent the return code and exit status of multipath and its most important subroutine, configure(). This clarifies the confusing use of booleans and status codes a bit, hopefully. This patch doesn't introduce a change in behavior. Reviewed-by: Benjamin Marzinski Signed-off-by: Martin Wilck --- multipath/main.c | 120 ++++++++++++++++++++++++++--------------------- 1 file changed, 66 insertions(+), 54 deletions(-) diff --git a/multipath/main.c b/multipath/main.c index 1d34b87b..f40c179b 100644 --- a/multipath/main.c +++ b/multipath/main.c @@ -68,6 +68,19 @@ int logsink; struct udev *udev; struct config *multipath_conf; +/* + * Return values of configure(), print_cmd_valid(), and main(). + * RTVL_{YES,NO} are synonyms for RTVL_{OK,FAIL} for the CMD_VALID_PATH case. + */ +enum { + RTVL_OK = 0, + RTVL_YES = RTVL_OK, + RTVL_FAIL = 1, + RTVL_NO = RTVL_FAIL, + RTVL_MAYBE, /* only used internally, never returned */ + RTVL_RETRY, /* returned by configure(), not by main() */ +}; + struct config *get_multipath_config(void) { return multipath_conf; @@ -475,15 +488,14 @@ retry: static int print_cmd_valid(int k, const vector pathvec, struct config *conf) { - static const int vals[] = { 1, 0, 2 }; int wait = FIND_MULTIPATHS_NEVER; struct timespec until; struct path *pp; - if (k < 0 || k >= (sizeof(vals) / sizeof(int))) - return 1; + if (k != RTVL_YES && k != RTVL_NO && k != RTVL_MAYBE) + return RTVL_NO; - if (k == 2) { + if (k == RTVL_MAYBE) { /* * Caller ensures that pathvec[0] is the path to * examine. @@ -493,7 +505,7 @@ static int print_cmd_valid(int k, const vector pathvec, wait = find_multipaths_check_timeout( pp, pp->find_multipaths_timeout, &until); if (wait != FIND_MULTIPATHS_WAITING) - k = 1; + k = RTVL_NO; } else if (pathvec != NULL && (pp = VECTOR_SLOT(pathvec, 0))) wait = find_multipaths_check_timeout(pp, 0, &until); if (wait == FIND_MULTIPATHS_WAITING) @@ -501,8 +513,10 @@ static int print_cmd_valid(int k, const vector pathvec, until.tv_sec, until.tv_nsec/1000); else if (wait == FIND_MULTIPATHS_WAIT_DONE) printf("FIND_MULTIPATHS_WAIT_UNTIL=\"0\"\n"); - printf("DM_MULTIPATH_DEVICE_PATH=\"%d\"\n", vals[k]); - return k == 1; + printf("DM_MULTIPATH_DEVICE_PATH=\"%d\"\n", + k == RTVL_MAYBE ? 2 : k == RTVL_YES ? 1 : 0); + /* Never return RTVL_MAYBE */ + return k == RTVL_NO ? RTVL_NO : RTVL_YES; } /* @@ -524,12 +538,6 @@ static bool released_to_systemd(void) return ret; } -/* - * Return value: - * -1: Retry - * 0: Success - * 1: Failure - */ static int configure (struct config *conf, enum mpath_cmds cmd, enum devtypes dev_type, char *devpath) @@ -537,7 +545,7 @@ configure (struct config *conf, enum mpath_cmds cmd, vector curmp = NULL; vector pathvec = NULL; struct vectors vecs; - int r = 1, rc; + int r = RTVL_FAIL, rc; int di_flag = 0; char * refwwid = NULL; char * dev = NULL; @@ -585,21 +593,23 @@ configure (struct config *conf, enum mpath_cmds cmd, goto out; } if (cmd == CMD_REMOVE_WWID) { - r = remove_wwid(refwwid); - if (r == 0) + rc = remove_wwid(refwwid); + if (rc == 0) { printf("wwid '%s' removed\n", refwwid); - else if (r == 1) { + r = RTVL_OK; + } else if (rc == 1) { printf("wwid '%s' not in wwids file\n", refwwid); - r = 0; + r = RTVL_OK; } goto out; } if (cmd == CMD_ADD_WWID) { - r = remember_wwid(refwwid); - if (r >= 0) + rc = remember_wwid(refwwid); + if (rc >= 0) { printf("wwid '%s' added\n", refwwid); - else + r = RTVL_OK; + } else printf("failed adding '%s' to wwids file\n", refwwid); goto out; @@ -614,13 +624,13 @@ configure (struct config *conf, enum mpath_cmds cmd, */ if (cmd == CMD_VALID_PATH) { if (is_failed_wwid(refwwid) == WWID_IS_FAILED) { - r = 1; + r = RTVL_NO; goto print_valid; } if ((!find_multipaths_on(conf) && ignore_wwids_on(conf)) || check_wwids_file(refwwid, 0) == 0) - r = 0; + r = RTVL_YES; if (!ignore_wwids_on(conf)) goto print_valid; /* At this point, either r==0 or find_multipaths_on. */ @@ -630,7 +640,7 @@ configure (struct config *conf, enum mpath_cmds cmd, * Quick check if path is already multipathed. */ if (sysfs_is_multipathed(VECTOR_SLOT(pathvec, 0))) { - r = 0; + r = RTVL_YES; goto print_valid; } @@ -644,10 +654,10 @@ configure (struct config *conf, enum mpath_cmds cmd, * Leave DM_MULTIPATH_DEVICE_PATH="0". */ if (released) { - r = 1; + r = RTVL_NO; goto print_valid; } - if (r == 0) + if (r == RTVL_YES) goto print_valid; /* find_multipaths_on: Fall through to path detection */ } @@ -703,13 +713,12 @@ configure (struct config *conf, enum mpath_cmds cmd, * the refwwid, or there is more than one path matching * the refwwid, then the path is valid */ if (VECTOR_SIZE(curmp) != 0) { - r = 0; + r = RTVL_YES; goto print_valid; } else if (VECTOR_SIZE(pathvec) > 1) - r = 0; + r = RTVL_YES; else - /* Use r=2 as an indication for "maybe" */ - r = 2; + r = RTVL_MAYBE; /* * If opening the path with O_EXCL fails, the path @@ -739,13 +748,14 @@ configure (struct config *conf, enum mpath_cmds cmd, /* * Check if we raced with multipathd */ - r = !sysfs_is_multipathed(VECTOR_SLOT(pathvec, 0)); + r = sysfs_is_multipathed(VECTOR_SLOT(pathvec, 0)) ? + RTVL_YES : RTVL_NO; } goto print_valid; } if (cmd != CMD_CREATE && cmd != CMD_DRY_RUN) { - r = 0; + r = RTVL_OK; goto out; } @@ -754,7 +764,7 @@ configure (struct config *conf, enum mpath_cmds cmd, */ rc = coalesce_paths(&vecs, NULL, refwwid, conf->force_reload, cmd); - r = rc == CP_RETRY ? -1 : rc == CP_OK ? 0 : 1; + r = rc == CP_RETRY ? RTVL_RETRY : rc == CP_OK ? RTVL_OK : RTVL_FAIL; print_valid: if (cmd == CMD_VALID_PATH) @@ -855,7 +865,7 @@ main (int argc, char *argv[]) int arg; extern char *optarg; extern int optind; - int r = 1; + int r = RTVL_FAIL; enum mpath_cmds cmd = CMD_CREATE; enum devtypes dev_type = DEV_NONE; char *dev = NULL; @@ -866,7 +876,7 @@ main (int argc, char *argv[]) logsink = 0; conf = load_config(DEFAULT_CONFIGFILE); if (!conf) - exit(1); + exit(RTVL_FAIL); multipath_conf = conf; conf->retrigger_tries = 0; while ((arg = getopt(argc, argv, ":adcChl::FfM:v:p:b:BrR:itTquUwW")) != EOF ) { @@ -877,7 +887,7 @@ main (int argc, char *argv[]) if (sizeof(optarg) > sizeof(char *) || !isdigit(optarg[0])) { usage (argv[0]); - exit(1); + exit(RTVL_FAIL); } conf->verbosity = atoi(optarg); @@ -924,7 +934,7 @@ main (int argc, char *argv[]) if (conf->pgpolicy_flag == IOPOLICY_UNDEF) { printf("'%s' is not a valid policy\n", optarg); usage(argv[0]); - exit(1); + exit(RTVL_FAIL); } break; case 'r': @@ -934,14 +944,14 @@ main (int argc, char *argv[]) conf->find_multipaths |= _FIND_MULTIPATHS_I; break; case 't': - r = dump_config(conf, NULL, NULL); + r = dump_config(conf, NULL, NULL) ? RTVL_FAIL : RTVL_OK; goto out_free_config; case 'T': cmd = CMD_DUMP_CONFIG; break; case 'h': usage(argv[0]); - exit(0); + exit(RTVL_OK); case 'u': cmd = CMD_VALID_PATH; dev_type = DEV_UEVENT; @@ -965,20 +975,20 @@ main (int argc, char *argv[]) case ':': fprintf(stderr, "Missing option argument\n"); usage(argv[0]); - exit(1); + exit(RTVL_FAIL); case '?': fprintf(stderr, "Unknown switch: %s\n", optarg); usage(argv[0]); - exit(1); + exit(RTVL_FAIL); default: usage(argv[0]); - exit(1); + exit(RTVL_FAIL); } } if (getuid() != 0) { fprintf(stderr, "need to be root\n"); - exit(1); + exit(RTVL_FAIL); } if (optind < argc) { @@ -1016,7 +1026,8 @@ main (int argc, char *argv[]) /* Failing here is non-fatal */ init_foreign(conf->multipath_dir); if (cmd == CMD_USABLE_PATHS) { - r = check_usable_paths(conf, dev, dev_type); + r = check_usable_paths(conf, dev, dev_type) ? + RTVL_FAIL : RTVL_OK; goto out; } if (cmd == CMD_VALID_PATH && @@ -1032,7 +1043,7 @@ main (int argc, char *argv[]) if (fd == -1) { condlog(3, "%s: daemon is not running", dev); if (!systemd_service_enabled(dev)) { - r = print_cmd_valid(1, NULL, conf); + r = print_cmd_valid(RTVL_NO, NULL, conf); goto out; } } else @@ -1046,9 +1057,9 @@ main (int argc, char *argv[]) switch(delegate_to_multipathd(cmd, dev, dev_type, conf)) { case DELEGATE_OK: - exit(0); + exit(RTVL_OK); case DELEGATE_ERROR: - exit(1); + exit(RTVL_FAIL); case NOT_DELEGATED: break; } @@ -1064,8 +1075,8 @@ main (int argc, char *argv[]) goto out; } if (dm_get_maps(curmp) == 0) - r = replace_wwids(curmp); - if (r == 0) + r = replace_wwids(curmp) ? RTVL_FAIL : RTVL_OK; + if (r == RTVL_OK) printf("successfully reset wwids\n"); vector_foreach_slot_backwards(curmp, mpp, i) { vector_del_slot(curmp, i); @@ -1078,17 +1089,18 @@ main (int argc, char *argv[]) retries = conf->remove_retries; if (conf->remove == FLUSH_ONE) { if (dev_type == DEV_DEVMAP) { - r = dm_suspend_and_flush_map(dev, retries); + r = dm_suspend_and_flush_map(dev, retries) ? + RTVL_FAIL : RTVL_OK; } else condlog(0, "must provide a map name to remove"); goto out; } else if (conf->remove == FLUSH_ALL) { - r = dm_flush_maps(retries); + r = dm_flush_maps(retries) ? RTVL_FAIL : RTVL_OK; goto out; } - while ((r = configure(conf, cmd, dev_type, dev)) < 0) + while ((r = configure(conf, cmd, dev_type, dev)) == RTVL_RETRY) condlog(3, "restart multipath configuration process"); out: @@ -1103,8 +1115,8 @@ out: * multipath -u must exit with status 0, otherwise udev won't * import its output. */ - if (cmd == CMD_VALID_PATH && dev_type == DEV_UEVENT && r == 1) - r = 0; + if (cmd == CMD_VALID_PATH && dev_type == DEV_UEVENT && r == RTVL_NO) + r = RTVL_OK; if (dev_type == DEV_UEVENT) closelog();