From patchwork Fri May 5 22:05:53 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Martin Wilck X-Patchwork-Id: 9714421 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.web.codeaurora.org (Postfix) with ESMTP id 175C660387 for ; Fri, 5 May 2017 22:06:48 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id F119E286B0 for ; Fri, 5 May 2017 22:06:47 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D82B328696; Fri, 5 May 2017 22:06:47 +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=-6.9 required=2.0 tests=BAYES_00,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 4490A2866A for ; Fri, 5 May 2017 22:06:47 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id BB5508047D; Fri, 5 May 2017 22:06:45 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com BB5508047D Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=suse.com Authentication-Results: ext-mx04.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=dm-devel-bounces@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com BB5508047D Received: from colo-mx.corp.redhat.com (unknown [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 407D77D67C; Fri, 5 May 2017 22:06:45 +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 2B2454ED32; Fri, 5 May 2017 22:06:43 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id v45M6foG015052 for ; Fri, 5 May 2017 18:06:41 -0400 Received: by smtp.corp.redhat.com (Postfix) id 66ED160472; Fri, 5 May 2017 22:06:41 +0000 (UTC) Delivered-To: dm-devel@redhat.com Received: from mx1.redhat.com (ext-mx09.extmail.prod.ext.phx2.redhat.com [10.5.110.38]) by smtp.corp.redhat.com (Postfix) with ESMTPS id B714119EF6; Fri, 5 May 2017 22:06:39 +0000 (UTC) Received: from mx1.suse.de (mx2.suse.de [195.135.220.15]) (using TLSv1 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 612084E33E; Fri, 5 May 2017 22:06:38 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 612084E33E Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=suse.com Authentication-Results: ext-mx09.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=mwilck@suse.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 612084E33E X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay1.suse.de (charybdis-ext.suse.de [195.135.220.254]) by mx1.suse.de (Postfix) with ESMTP id 20C18ABF1; Fri, 5 May 2017 22:06:37 +0000 (UTC) From: Martin Wilck To: Christophe Varoqui , Hannes Reinecke , Benjamin Marzinski Date: Sat, 6 May 2017 00:05:53 +0200 Message-Id: <20170505220559.7248-5-mwilck@suse.com> In-Reply-To: <20170505220559.7248-1-mwilck@suse.com> References: <20170505220559.7248-1-mwilck@suse.com> X-Greylist: Sender passed SPF test, Sender IP whitelisted by DNSRBL, ACL 203 matched, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Fri, 05 May 2017 22:06:38 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Fri, 05 May 2017 22:06:38 +0000 (UTC) for IP:'195.135.220.15' DOMAIN:'mx2.suse.de' HELO:'mx1.suse.de' FROM:'mwilck@suse.com' RCPT:'' X-RedHat-Spam-Score: -1.901 (BAYES_50, DCC_REPUT_00_12, RCVD_IN_DNSWL_MED, SPF_PASS) 195.135.220.15 mx2.suse.de 195.135.220.15 mx2.suse.de X-Scanned-By: MIMEDefang 2.78 on 10.5.110.38 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-loop: dm-devel@redhat.com Cc: dm-devel@redhat.com Subject: [dm-devel] [PATCH 04/10] kpartx: dm_remove_partmaps: support non-dm devices 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: , MIME-Version: 1.0 Sender: dm-devel-bounces@redhat.com Errors-To: dm-devel-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Fri, 05 May 2017 22:06:46 +0000 (UTC) X-Virus-Scanned: ClamAV using ClamSMTP Commit 3d709241 improved partition removal, but broke support for handling partitions of non-dm devices such as loop devices or RAM disks. This requires passing the dev_t of the device down to do_foreach_partmap(). Doing so, there's little use in trying to derive major/minor numbers from the "mapname" any more (which actually is the device name for non-DM devices). But we can use this to find out whether the device in question is a dm device. The test for UUID match doesn't work for non-DM devices (this shall be improved in a later patch in this series). The test for equal name of parent dev and partition is only valid for dm devices. For non-dm devices such as loop, "/dev/mapper/loop0" could, theoretically, be a partition of "/dev/loop0" (and we don't want to rely on map names). Fixes: 3d709241 "kpartx: sanitize delete partitions" Signed-off-by: Martin Wilck --- kpartx/devmapper.c | 26 +++++++++++++++++--------- kpartx/devmapper.h | 2 +- kpartx/kpartx.c | 3 ++- 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/kpartx/devmapper.c b/kpartx/devmapper.c index cf6650c6..8f48a705 100644 --- a/kpartx/devmapper.c +++ b/kpartx/devmapper.c @@ -8,6 +8,7 @@ #include #include #include +#include #include "devmapper.h" #define UUID_PREFIX "part%d-" @@ -418,8 +419,8 @@ dm_compare_uuid(const char *mapuuid, const char *partname) return 1; if (!strncmp(partuuid, "part", 4)) { - char *p = strstr(partuuid, "mpath-"); - if (p && !strcmp(mapuuid, p)) + char *p = strchr(partuuid, '-'); + if (p && !strcmp(mapuuid, p + 1)) r = 0; } free(partuuid); @@ -432,6 +433,7 @@ struct remove_data { static int do_foreach_partmaps (const char * mapname, const char *uuid, + dev_t devt, int (*partmap_func)(const char *, void *), void *data) { @@ -443,6 +445,7 @@ do_foreach_partmaps (const char * mapname, const char *uuid, int major, minor; char dev_t[32]; int r = 1; + int is_dmdev = 1; if (!(dmt = dm_task_create(DM_DEVICE_LIST))) return 1; @@ -460,15 +463,20 @@ do_foreach_partmaps (const char * mapname, const char *uuid, goto out; } - if (dm_devn(mapname, &major, &minor)) - goto out; + if (dm_devn(mapname, &major, &minor) || + (major != major(devt) || minor != minor(devt))) + /* + * The latter could happen if a dm device "/dev/mapper/loop0" + * exits while kpartx is called on "/dev/loop0". + */ + is_dmdev = 0; - sprintf(dev_t, "%d:%d", major, minor); + sprintf(dev_t, "%d:%d", major(devt), minor(devt)); do { /* * skip our devmap */ - if (!strcmp(names->name, mapname)) + if (is_dmdev && !strcmp(names->name, mapname)) goto next; /* @@ -496,7 +504,7 @@ do_foreach_partmaps (const char * mapname, const char *uuid, /* * skip if uuids don't match */ - if (dm_compare_uuid(uuid, names->name)) { + if (is_dmdev && uuid && dm_compare_uuid(uuid, names->name)) { if (rd->verbose) printf("%s: is not a kpartx partition. Not removing\n", names->name); @@ -537,10 +545,10 @@ remove_partmap(const char *name, void *data) } int -dm_remove_partmaps (char * mapname, char *uuid, int verbose) +dm_remove_partmaps (char * mapname, char *uuid, dev_t devt, int verbose) { struct remove_data rd = { verbose }; - return do_foreach_partmaps(mapname, uuid, remove_partmap, &rd); + return do_foreach_partmaps(mapname, uuid, devt, remove_partmap, &rd); } #define FEATURE_NO_PART "no_partitions" diff --git a/kpartx/devmapper.h b/kpartx/devmapper.h index 9988ec0f..2e28c780 100644 --- a/kpartx/devmapper.h +++ b/kpartx/devmapper.h @@ -18,7 +18,7 @@ char * dm_mapname(int major, int minor); dev_t dm_get_first_dep(char *devname); char * dm_mapuuid(const char *mapname); int dm_devn (const char * mapname, int *major, int *minor); -int dm_remove_partmaps (char * mapname, char *uuid, int verbose); +int dm_remove_partmaps (char * mapname, char *uuid, dev_t devt, int verbose); int dm_no_partitions(char * mapname); #endif /* _KPARTX_DEVMAPPER_H */ diff --git a/kpartx/kpartx.c b/kpartx/kpartx.c index e9b09492..e2056b7f 100644 --- a/kpartx/kpartx.c +++ b/kpartx/kpartx.c @@ -451,7 +451,8 @@ main(int argc, char **argv){ break; case DELETE: - r = dm_remove_partmaps(mapname, uuid, verbose); + r = dm_remove_partmaps(mapname, uuid, buf.st_rdev, + verbose); if (loopdev) { if (del_loop(loopdev)) { if (verbose)