From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-16.5 required=3.0 tests=BAYES_00,DKIM_INVALID, DKIM_SIGNED,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS,URIBL_BLOCKED,USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id ADF36C71155 for ; Thu, 26 Nov 2020 13:07:36 +0000 (UTC) Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.kernel.org (Postfix) with ESMTP id 2995A221F1 for ; Thu, 26 Nov 2020 13:07:36 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=infradead.org header.i=@infradead.org header.b="CdbC5hlw" DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 2995A221F1 Authentication-Results: mail.kernel.org; dmarc=none (p=none dis=none) header.from=lst.de Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=owner-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix) id F1EC76B008A; Thu, 26 Nov 2020 08:07:11 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id EF5706B008C; Thu, 26 Nov 2020 08:07:11 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id D49E86B0092; Thu, 26 Nov 2020 08:07:11 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from forelay.hostedemail.com (smtprelay0068.hostedemail.com [216.40.44.68]) by kanga.kvack.org (Postfix) with ESMTP id BC0FE6B008A for ; Thu, 26 Nov 2020 08:07:11 -0500 (EST) Received: from smtpin25.hostedemail.com (10.5.19.251.rfc1918.com [10.5.19.251]) by forelay04.hostedemail.com (Postfix) with ESMTP id 8CB5B1EE6 for ; Thu, 26 Nov 2020 13:07:11 +0000 (UTC) X-FDA: 77526595062.25.women67_4a084172737f Received: from filter.hostedemail.com (10.5.16.251.rfc1918.com [10.5.16.251]) by smtpin25.hostedemail.com (Postfix) with ESMTP id 6A0A01804E3A1 for ; Thu, 26 Nov 2020 13:07:11 +0000 (UTC) X-HE-Tag: women67_4a084172737f X-Filterd-Recvd-Size: 8384 Received: from casper.infradead.org (casper.infradead.org [90.155.50.34]) by imf25.hostedemail.com (Postfix) with ESMTP for ; Thu, 26 Nov 2020 13:07:10 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=infradead.org; s=casper.20170209; h=Content-Transfer-Encoding:MIME-Version: References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From:Sender:Reply-To: Content-Type:Content-ID:Content-Description; bh=aXV3aDcarMWm87S3K48qh326Jqh2T5iEz0okUiuykIU=; b=CdbC5hlwj31l+0IcsFPnJP1nL1 +kxO6fnofY15Q8oWyZkGLPmr7gioA2In6NDJY5N83HYYS68cvToFcHGWLrMo7DDpM//js2sxCscAV 70J3mUQiLizIOkrp+AWra1+ubkP00mS52zOg4rtxpHO5F2qyOwxNAAq+8RfKOFTNzFchlm1NnwdFy B9vQoYXRi4VrsNweREv2Bi5/AEghh77WnXpyFZqpajHkQOfvk9ozHa05FKD6BAXWpjbiQduV+184Z BnJS0sT1e1hyRM1MlkFl6avl9qflwdCVAnriAgpt09AQLVsFImFuQvsKjpG3KoJpbzehpvTz5cfpc afAK1+Sg==; Received: from [2001:4bb8:18c:1dd6:27b8:b8a1:c13e:ceb1] (helo=localhost) by casper.infradead.org with esmtpsa (Exim 4.92.3 #3 (Red Hat Linux)) id 1kiGz9-00040j-PL; Thu, 26 Nov 2020 13:06:56 +0000 From: Christoph Hellwig To: Jens Axboe Cc: Tejun Heo , Josef Bacik , Coly Li , Mike Snitzer , Greg Kroah-Hartman , Jan Kara , Johannes Thumshirn , dm-devel@redhat.com, Jan Kara , linux-block@vger.kernel.org, linux-bcache@vger.kernel.org, linux-mtd@lists.infradead.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org Subject: [PATCH 16/44] init: refactor name_to_dev_t Date: Thu, 26 Nov 2020 14:03:54 +0100 Message-Id: <20201126130422.92945-17-hch@lst.de> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201126130422.92945-1-hch@lst.de> References: <20201126130422.92945-1-hch@lst.de> MIME-Version: 1.0 X-SRS-Rewrite: SMTP reverse-path rewritten from by casper.infradead.org. See http://www.infradead.org/rpr.html Content-Transfer-Encoding: quoted-printable X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: Split each case into a self-contained helper, and move the block dependent code entirely under the pre-existing #ifdef CONFIG_BLOCK. This allows to remove the blk_lookup_devt stub in genhd.h. Signed-off-by: Christoph Hellwig Reviewed-by: Greg Kroah-Hartman Reviewed-by: Jan Kara Reviewed-by: Johannes Thumshirn Acked-by: Tejun Heo --- include/linux/genhd.h | 7 +- init/do_mounts.c | 183 +++++++++++++++++++++--------------------- 2 files changed, 91 insertions(+), 99 deletions(-) diff --git a/include/linux/genhd.h b/include/linux/genhd.h index 22f5b9fd96f8bf..ca5e356084c353 100644 --- a/include/linux/genhd.h +++ b/include/linux/genhd.h @@ -388,18 +388,13 @@ static inline void bd_unlink_disk_holder(struct blo= ck_device *bdev, } #endif /* CONFIG_SYSFS */ =20 +dev_t blk_lookup_devt(const char *name, int partno); #ifdef CONFIG_BLOCK void printk_all_partitions(void); -dev_t blk_lookup_devt(const char *name, int partno); #else /* CONFIG_BLOCK */ static inline void printk_all_partitions(void) { } -static inline dev_t blk_lookup_devt(const char *name, int partno) -{ - dev_t devt =3D MKDEV(0, 0); - return devt; -} #endif /* CONFIG_BLOCK */ =20 #endif /* _LINUX_GENHD_H */ diff --git a/init/do_mounts.c b/init/do_mounts.c index b5f9604d0c98a2..aef2f24461c7f1 100644 --- a/init/do_mounts.c +++ b/init/do_mounts.c @@ -90,7 +90,6 @@ static int match_dev_by_uuid(struct device *dev, const = void *data) return 0; } =20 - /** * devt_from_partuuid - looks up the dev_t of a partition by its UUID * @uuid_str: char array containing ascii UUID @@ -186,7 +185,83 @@ static int match_dev_by_label(struct device *dev, co= nst void *data) =20 return 0; } -#endif + +static dev_t devt_from_partlabel(const char *label) +{ + struct device *dev; + dev_t devt =3D 0; + + dev =3D class_find_device(&block_class, NULL, label, &match_dev_by_labe= l); + if (dev) { + devt =3D dev->devt; + put_device(dev); + } + + return devt; +} + +static dev_t devt_from_devname(const char *name) +{ + dev_t devt =3D 0; + int part; + char s[32]; + char *p; + + if (strlen(name) > 31) + return 0; + strcpy(s, name); + for (p =3D s; *p; p++) { + if (*p =3D=3D '/') + *p =3D '!'; + } + + devt =3D blk_lookup_devt(s, 0); + if (devt) + return devt; + + /* + * Try non-existent, but valid partition, which may only exist after + * opening the device, like partitioned md devices. + */ + while (p > s && isdigit(p[-1])) + p--; + if (p =3D=3D s || !*p || *p =3D=3D '0') + return 0; + + /* try disk name without */ + part =3D simple_strtoul(p, NULL, 10); + *p =3D '\0'; + devt =3D blk_lookup_devt(s, part); + if (devt) + return devt; + + /* try disk name without p */ + if (p < s + 2 || !isdigit(p[-2]) || p[-1] !=3D 'p') + return 0; + p[-1] =3D '\0'; + return blk_lookup_devt(s, part); +} +#endif /* CONFIG_BLOCK */ + +static dev_t devt_from_devnum(const char *name) +{ + unsigned maj, min, offset; + dev_t devt =3D 0; + char *p, dummy; + + if (sscanf(name, "%u:%u%c", &maj, &min, &dummy) =3D=3D 2 || + sscanf(name, "%u:%u:%u:%c", &maj, &min, &offset, &dummy) =3D=3D 3) = { + devt =3D MKDEV(maj, min); + if (maj !=3D MAJOR(devt) || min !=3D MINOR(devt)) + return 0; + } else { + devt =3D new_decode_dev(simple_strtoul(name, &p, 16)); + if (*p) + return 0; + } + + return devt; +} =20 /* * Convert a name into device number. We accept the following variants: @@ -218,101 +293,23 @@ static int match_dev_by_label(struct device *dev, = const void *data) * name contains slashes, the device name has them replaced with * bangs. */ - dev_t name_to_dev_t(const char *name) { - char s[32]; - char *p; - dev_t res =3D 0; - int part; - + if (strcmp(name, "/dev/nfs") =3D=3D 0) + return Root_NFS; + if (strcmp(name, "/dev/cifs") =3D=3D 0) + return Root_CIFS; + if (strcmp(name, "/dev/ram") =3D=3D 0) + return Root_RAM0; #ifdef CONFIG_BLOCK - if (strncmp(name, "PARTUUID=3D", 9) =3D=3D 0) { - name +=3D 9; - res =3D devt_from_partuuid(name); - if (!res) - goto fail; - goto done; - } else if (strncmp(name, "PARTLABEL=3D", 10) =3D=3D 0) { - struct device *dev; - - dev =3D class_find_device(&block_class, NULL, name + 10, - &match_dev_by_label); - if (!dev) - goto fail; - - res =3D dev->devt; - put_device(dev); - goto done; - } + if (strncmp(name, "PARTUUID=3D", 9) =3D=3D 0) + return devt_from_partuuid(name + 9); + if (strncmp(name, "PARTLABEL=3D", 10) =3D=3D 0) + return devt_from_partlabel(name + 10); + if (strncmp(name, "/dev/", 5) =3D=3D 0) + return devt_from_devname(name + 5); #endif - - if (strncmp(name, "/dev/", 5) !=3D 0) { - unsigned maj, min, offset; - char dummy; - - if ((sscanf(name, "%u:%u%c", &maj, &min, &dummy) =3D=3D 2) || - (sscanf(name, "%u:%u:%u:%c", &maj, &min, &offset, &dummy) =3D=3D 3= )) { - res =3D MKDEV(maj, min); - if (maj !=3D MAJOR(res) || min !=3D MINOR(res)) - goto fail; - } else { - res =3D new_decode_dev(simple_strtoul(name, &p, 16)); - if (*p) - goto fail; - } - goto done; - } - - name +=3D 5; - res =3D Root_NFS; - if (strcmp(name, "nfs") =3D=3D 0) - goto done; - res =3D Root_CIFS; - if (strcmp(name, "cifs") =3D=3D 0) - goto done; - res =3D Root_RAM0; - if (strcmp(name, "ram") =3D=3D 0) - goto done; - - if (strlen(name) > 31) - goto fail; - strcpy(s, name); - for (p =3D s; *p; p++) - if (*p =3D=3D '/') - *p =3D '!'; - res =3D blk_lookup_devt(s, 0); - if (res) - goto done; - - /* - * try non-existent, but valid partition, which may only exist - * after revalidating the disk, like partitioned md devices - */ - while (p > s && isdigit(p[-1])) - p--; - if (p =3D=3D s || !*p || *p =3D=3D '0') - goto fail; - - /* try disk name without */ - part =3D simple_strtoul(p, NULL, 10); - *p =3D '\0'; - res =3D blk_lookup_devt(s, part); - if (res) - goto done; - - /* try disk name without p */ - if (p < s + 2 || !isdigit(p[-2]) || p[-1] !=3D 'p') - goto fail; - p[-1] =3D '\0'; - res =3D blk_lookup_devt(s, part); - if (res) - goto done; - -fail: - return 0; -done: - return res; + return devt_from_devnum(name); } EXPORT_SYMBOL_GPL(name_to_dev_t); =20 --=20 2.29.2