From patchwork Fri Jan 16 20:23:28 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Benjamin Marzinski X-Patchwork-Id: 5650521 X-Patchwork-Delegate: christophe.varoqui@free.fr Return-Path: X-Original-To: patchwork-dm-devel@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork1.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork1.web.kernel.org (Postfix) with ESMTP id 5E1B09F6E2 for ; Fri, 16 Jan 2015 21:32:14 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 7152020357 for ; Fri, 16 Jan 2015 21:32:13 +0000 (UTC) Received: from mx4-phx2.redhat.com (mx4-phx2.redhat.com [209.132.183.25]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPS id 6D4992034B for ; Fri, 16 Jan 2015 21:32:12 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by mx4-phx2.redhat.com (8.13.8/8.13.8) with ESMTP id t0GLSoDn011038; Fri, 16 Jan 2015 16:28:50 -0500 Received: from int-mx13.intmail.prod.int.phx2.redhat.com (int-mx13.intmail.prod.int.phx2.redhat.com [10.5.11.26]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id t0GLRmS5011869 for ; Fri, 16 Jan 2015 16:27:48 -0500 Received: from redhat.com (ask-08.lab.msp.redhat.com [10.15.85.8]) by int-mx13.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with SMTP id t0GLRlHZ012091; Fri, 16 Jan 2015 16:27:47 -0500 Received: by redhat.com (sSMTP sendmail emulation); Fri, 16 Jan 2015 14:23:43 -0600 From: "Benjamin Marzinski" To: device-mapper development Date: Fri, 16 Jan 2015 14:23:28 -0600 Message-Id: <1421439812-11826-5-git-send-email-bmarzins@redhat.com> In-Reply-To: <1421439812-11826-1-git-send-email-bmarzins@redhat.com> References: <1421439812-11826-1-git-send-email-bmarzins@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.26 X-loop: dm-devel@redhat.com Subject: [dm-devel] [PATCH 4/8] libmultipath: refactor partmaps code X-BeenThere: dm-devel@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk Reply-To: device-mapper development List-Id: device-mapper development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Sender: dm-devel-bounces@redhat.com Errors-To: dm-devel-bounces@redhat.com X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=unavailable 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 There was a lot of code duplication between dm_remove_partmaps and dm_rename_partmaps. This patch factors that out into do_foreach_partmaps. Signed-off-by: Benjamin Marzinski --- libmultipath/devmapper.c | 156 +++++++++++++++++++---------------------------- 1 file changed, 62 insertions(+), 94 deletions(-) diff --git a/libmultipath/devmapper.c b/libmultipath/devmapper.c index 9e585f9..cddb9de 100644 --- a/libmultipath/devmapper.c +++ b/libmultipath/devmapper.c @@ -998,8 +998,9 @@ bad: return NULL; } -int -dm_remove_partmaps (const char * mapname, int need_sync) +static int +do_foreach_partmaps (const char * mapname, int (*partmap_func)(char *, void *), + void *data) { struct dm_task *dmt; struct dm_names *names; @@ -1051,26 +1052,8 @@ dm_remove_partmaps (const char * mapname, int need_sync) */ strstr(params, dev_t) ) { - /* - * then it's a kpartx generated partition. - * remove it. - */ - /* - * if the opencount is 0 maybe some other - * partitions depend on it. - */ - if (dm_get_opencount(names->name)) { - dm_remove_partmaps(names->name, need_sync); - if (dm_get_opencount(names->name)) { - condlog(2, "%s: map in use", - names->name); - goto out; - } - } - condlog(4, "partition map %s removed", - names->name); - dm_simplecmd_flush(DM_DEVICE_REMOVE, names->name, - need_sync, 0); + if (partmap_func(names->name, data) != 0) + goto out; } next = names->next; @@ -1083,6 +1066,35 @@ out: return r; } +struct remove_data { + int need_sync; +}; + +static int +remove_partmap(char *name, void *data) +{ + struct remove_data *rd = (struct remove_data *)data; + + if (dm_get_opencount(name)) { + dm_remove_partmaps(name, rd->need_sync); + if (dm_get_opencount(name)) { + condlog(2, "%s: map in use", name); + return 1; + } + } + condlog(4, "partition map %s removed", name); + dm_simplecmd_flush(DM_DEVICE_REMOVE, name, + rd->need_sync, 0); + return 0; +} + +int +dm_remove_partmaps (const char * mapname, int need_sync) +{ + struct remove_data rd = { need_sync }; + return do_foreach_partmaps(mapname, remove_partmap, &rd); +} + static struct dm_info * alloc_dminfo (void) { @@ -1132,88 +1144,44 @@ out: return r; } -int -dm_rename_partmaps (char * old, char * new) +struct rename_data { + char *old; + char *new; + char *delim; +}; + +static int +rename_partmap (char *name, void *data) { - struct dm_task *dmt; - struct dm_names *names; - unsigned next = 0; char buff[PARAMS_SIZE]; - unsigned long long size; - char dev_t[32]; - int r = 1; int offset; - char *delim; + struct rename_data *rd = (struct rename_data *)data; - if (!(dmt = dm_task_create(DM_DEVICE_LIST))) - return 1; - - dm_task_no_open_count(dmt); - - if (!dm_task_run(dmt)) - goto out; - - if (!(names = dm_task_get_names(dmt))) - goto out; + if (strncmp(name, rd->old, strlen(rd->old)) != 0) + return 0; + for (offset = strlen(rd->old); name[offset] && !(isdigit(name[offset])); offset++); /* do nothing */ + snprintf(buff, PARAMS_SIZE, "%s%s%s", rd->new, rd->delim, + name + offset); + dm_rename(name, buff); + condlog(4, "partition map %s renamed", name); + return 0; +} - if (!names->dev) { - r = 0; /* this is perfectly valid */ - goto out; - } +int +dm_rename_partmaps (char * old, char * new) +{ + struct rename_data rd; - if (dm_dev_t(old, &dev_t[0], 32)) - goto out; + rd.old = old; + rd.new = new; if (conf->partition_delim) - delim = conf->partition_delim; + rd.delim = conf->partition_delim; if (isdigit(new[strlen(new)-1])) - delim = "p"; + rd.delim = "p"; else - delim = ""; - - do { - if ( - /* - * if devmap target is "linear" - */ - (dm_type(names->name, TGT_PART) > 0) && - - /* - * and the multipath mapname and the part mapname start - * the same - */ - !strncmp(names->name, old, strlen(old)) && - - /* - * and we can fetch the map table from the kernel - */ - !dm_get_map(names->name, &size, &buff[0]) && - - /* - * and the table maps over the multipath map - */ - strstr(buff, dev_t) - ) { - /* - * then it's a kpartx generated partition. - * Rename it. - */ - for (offset = strlen(old); names->name[offset] && !(isdigit(names->name[offset])); offset++); /* do nothing */ - snprintf(buff, PARAMS_SIZE, "%s%s%s", - new, delim, names->name + offset); - dm_rename(names->name, buff); - condlog(4, "partition map %s renamed", - names->name); - } - - next = names->next; - names = (void *) names + next; - } while (next); - - r = 0; -out: - dm_task_destroy (dmt); - return r; + rd.delim = ""; + return do_foreach_partmaps(old, rename_partmap, &rd); } int