dm-devel.redhat.com archive mirror
 help / color / mirror / Atom feed
From: Christoph Hellwig <hch@lst.de>
To: Jens Axboe <axboe@kernel.dk>
Cc: linux-bcache@vger.kernel.org, Jan Kara <jack@suse.cz>,
	linux-fsdevel@vger.kernel.org, Mike Snitzer <snitzer@redhat.com>,
	Konrad Rzeszutek Wilk <konrad.wilk@oracle.com>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Jan Kara <jack@suse.com>, Josef Bacik <josef@toxicpanda.com>,
	Coly Li <colyli@suse.de>,
	linux-block@vger.kernel.org, Richard Weinberger <richard@nod.at>,
	dm-devel@redhat.com, linux-mtd@lists.infradead.org,
	Johannes Thumshirn <johannes.thumshirn@wdc.com>,
	Tejun Heo <tj@kernel.org>,
	xen-devel@lists.xenproject.org, linux-mm@kvack.org
Subject: [dm-devel] [PATCH 17/45] init: refactor name_to_dev_t
Date: Tue, 24 Nov 2020 14:27:23 +0100	[thread overview]
Message-ID: <20201124132751.3747337-18-hch@lst.de> (raw)
In-Reply-To: <20201124132751.3747337-1-hch@lst.de>

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 <hch@lst.de>
Reviewed-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Reviewed-by: Jan Kara <jack@suse.cz>
Reviewed-by: Johannes Thumshirn <johannes.thumshirn@wdc.com>
---
 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 block_device *bdev,
 }
 #endif /* CONFIG_SYSFS */
 
+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 = MKDEV(0, 0);
-	return devt;
-}
 #endif /* CONFIG_BLOCK */
 
 #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;
 }
 
-
 /**
  * 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, const void *data)
 
 	return 0;
 }
-#endif
+
+static dev_t devt_from_partlabel(const char *label)
+{
+	struct device *dev;
+	dev_t devt = 0;
+
+	dev = class_find_device(&block_class, NULL, label, &match_dev_by_label);
+	if (dev) {
+		devt = dev->devt;
+		put_device(dev);
+	}
+
+	return devt;
+}
+
+static dev_t devt_from_devname(const char *name)
+{
+	dev_t devt = 0;
+	int part;
+	char s[32];
+	char *p;
+
+	if (strlen(name) > 31)
+		return 0;
+	strcpy(s, name);
+	for (p = s; *p; p++) {
+		if (*p == '/')
+			*p = '!';
+	}
+
+	devt = 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 == s || !*p || *p == '0')
+		return 0;
+
+	/* try disk name without <part number> */
+	part = simple_strtoul(p, NULL, 10);
+	*p = '\0';
+	devt = blk_lookup_devt(s, part);
+	if (devt)
+		return devt;
+
+	/* try disk name without p<part number> */
+	if (p < s + 2 || !isdigit(p[-2]) || p[-1] != 'p')
+		return 0;
+	p[-1] = '\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 = 0;
+	char *p, dummy;
+
+	if (sscanf(name, "%u:%u%c", &maj, &min, &dummy) == 2 ||
+	    sscanf(name, "%u:%u:%u:%c", &maj, &min, &offset, &dummy) == 3) {
+		devt = MKDEV(maj, min);
+		if (maj != MAJOR(devt) || min != MINOR(devt))
+			return 0;
+	} else {
+		devt = new_decode_dev(simple_strtoul(name, &p, 16));
+		if (*p)
+			return 0;
+	}
+
+	return devt;
+}
 
 /*
  *	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 = 0;
-	int part;
-
+	if (strcmp(name, "/dev/nfs") == 0)
+		return Root_NFS;
+	if (strcmp(name, "/dev/cifs") == 0)
+		return Root_CIFS;
+	if (strcmp(name, "/dev/ram") == 0)
+		return Root_RAM0;
 #ifdef CONFIG_BLOCK
-	if (strncmp(name, "PARTUUID=", 9) == 0) {
-		name += 9;
-		res = devt_from_partuuid(name);
-		if (!res)
-			goto fail;
-		goto done;
-	} else if (strncmp(name, "PARTLABEL=", 10) == 0) {
-		struct device *dev;
-
-		dev = class_find_device(&block_class, NULL, name + 10,
-					&match_dev_by_label);
-		if (!dev)
-			goto fail;
-
-		res = dev->devt;
-		put_device(dev);
-		goto done;
-	}
+	if (strncmp(name, "PARTUUID=", 9) == 0)
+		return devt_from_partuuid(name + 9);
+	if (strncmp(name, "PARTLABEL=", 10) == 0)
+		return devt_from_partlabel(name + 10);
+	if (strncmp(name, "/dev/", 5) == 0)
+		return devt_from_devname(name + 5);
 #endif
-
-	if (strncmp(name, "/dev/", 5) != 0) {
-		unsigned maj, min, offset;
-		char dummy;
-
-		if ((sscanf(name, "%u:%u%c", &maj, &min, &dummy) == 2) ||
-		    (sscanf(name, "%u:%u:%u:%c", &maj, &min, &offset, &dummy) == 3)) {
-			res = MKDEV(maj, min);
-			if (maj != MAJOR(res) || min != MINOR(res))
-				goto fail;
-		} else {
-			res = new_decode_dev(simple_strtoul(name, &p, 16));
-			if (*p)
-				goto fail;
-		}
-		goto done;
-	}
-
-	name += 5;
-	res = Root_NFS;
-	if (strcmp(name, "nfs") == 0)
-		goto done;
-	res = Root_CIFS;
-	if (strcmp(name, "cifs") == 0)
-		goto done;
-	res = Root_RAM0;
-	if (strcmp(name, "ram") == 0)
-		goto done;
-
-	if (strlen(name) > 31)
-		goto fail;
-	strcpy(s, name);
-	for (p = s; *p; p++)
-		if (*p == '/')
-			*p = '!';
-	res = 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 == s || !*p || *p == '0')
-		goto fail;
-
-	/* try disk name without <part number> */
-	part = simple_strtoul(p, NULL, 10);
-	*p = '\0';
-	res = blk_lookup_devt(s, part);
-	if (res)
-		goto done;
-
-	/* try disk name without p<part number> */
-	if (p < s + 2 || !isdigit(p[-2]) || p[-1] != 'p')
-		goto fail;
-	p[-1] = '\0';
-	res = 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);
 
-- 
2.29.2

--
dm-devel mailing list
dm-devel@redhat.com
https://www.redhat.com/mailman/listinfo/dm-devel


  parent reply	other threads:[~2020-11-24 13:33 UTC|newest]

Thread overview: 92+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-24 13:27 [dm-devel] merge struct block_device and struct hd_struct v2 Christoph Hellwig
2020-11-24 13:27 ` [dm-devel] [PATCH 01/45] blk-cgroup: fix a hd_struct leak in blkcg_fill_root_iostats Christoph Hellwig
2020-11-25  6:41   ` Hannes Reinecke
2020-11-24 13:27 ` [dm-devel] [PATCH 02/45] filemap: consistently use ->f_mapping over ->i_mapping Christoph Hellwig
2020-11-24 13:53   ` Matthew Wilcox
2020-11-25  6:41   ` Hannes Reinecke
2020-11-25 12:16   ` Jan Kara
2020-11-24 13:27 ` [dm-devel] [PATCH 03/45] fs: remove get_super_thawed and get_super_exclusive_thawed Christoph Hellwig
2020-11-25  6:42   ` Hannes Reinecke
2020-11-25 12:19   ` Jan Kara
2020-11-24 13:27 ` [dm-devel] [PATCH 04/45] fs: simplify freeze_bdev/thaw_bdev Christoph Hellwig
2020-11-25  6:07   ` Chao Yu
2020-11-25 12:29   ` Jan Kara
2020-11-25 16:22     ` Christoph Hellwig
2020-11-24 13:27 ` [dm-devel] [PATCH 05/45] mtip32xx: remove the call to fsync_bdev on removal Christoph Hellwig
2020-11-25 12:31   ` Jan Kara
2020-11-24 13:27 ` [dm-devel] [PATCH 06/45] zram: remove the claim mechanism Christoph Hellwig
2020-11-25 12:37   ` Jan Kara
2020-11-24 13:27 ` [dm-devel] [PATCH 07/45] zram: do not call set_blocksize Christoph Hellwig
2020-11-24 13:27 ` [dm-devel] [PATCH 08/45] loop: " Christoph Hellwig
2020-11-25 13:37   ` Jan Kara
2020-11-24 13:27 ` [dm-devel] [PATCH 09/45] dm: simplify flush_bio initialization in __send_empty_flush Christoph Hellwig
2020-11-24 13:27 ` [dm-devel] [PATCH 10/45] dm: remove the block_device reference in struct mapped_device Christoph Hellwig
2020-11-24 13:27 ` [dm-devel] [PATCH 11/45] block: remove a duplicate __disk_get_part prototype Christoph Hellwig
2020-11-24 17:36   ` Tejun Heo
2020-11-24 13:27 ` [dm-devel] [PATCH 12/45] block: remove a superflous check in blkpg_do_ioctl Christoph Hellwig
2020-11-24 17:37   ` Tejun Heo
2020-11-25 13:39   ` Jan Kara
2020-11-24 13:27 ` [dm-devel] [PATCH 13/45] block: add a bdev_kobj helper Christoph Hellwig
2020-11-24 13:41   ` Coly Li
2020-11-24 17:37   ` Tejun Heo
2020-11-24 13:27 ` [dm-devel] [PATCH 14/45] block: use disk_part_iter_exit in disk_part_iter_next Christoph Hellwig
2020-11-24 17:37   ` Tejun Heo
2020-11-24 13:27 ` [dm-devel] [PATCH 15/45] block: use put_device in put_disk Christoph Hellwig
2020-11-24 17:37   ` Tejun Heo
2020-11-24 13:27 ` [dm-devel] [PATCH 16/45] block: change the hash used for looking up block devices Christoph Hellwig
2020-11-24 17:38   ` Tejun Heo
2020-11-24 13:27 ` Christoph Hellwig [this message]
2020-11-24 17:38   ` [dm-devel] [PATCH 17/45] init: refactor name_to_dev_t Tejun Heo
2020-11-24 13:27 ` [dm-devel] [PATCH 18/45] init: refactor devt_from_partuuid Christoph Hellwig
2020-11-24 17:38   ` Tejun Heo
2020-11-24 13:27 ` [dm-devel] [PATCH 19/45] init: cleanup match_dev_by_uuid and match_dev_by_label Christoph Hellwig
2020-11-24 17:39   ` Tejun Heo
2020-11-24 13:27 ` [dm-devel] [PATCH 20/45] block: refactor __blkdev_put Christoph Hellwig
2020-11-24 17:41   ` Tejun Heo
2020-11-24 13:27 ` [dm-devel] [PATCH 21/45] block: refactor blkdev_get Christoph Hellwig
2020-11-24 18:03   ` Tejun Heo
2020-11-24 13:27 ` [dm-devel] [PATCH 22/45] block: opencode devcgroup_inode_permission Christoph Hellwig
2020-11-24 18:05   ` Tejun Heo
2020-11-24 13:27 ` [dm-devel] [PATCH 23/45] block: remove i_bdev Christoph Hellwig
2020-11-24 13:38   ` Coly Li
2020-11-24 19:37   ` Tejun Heo
2020-11-25 16:29     ` Christoph Hellwig
2020-11-25 20:19       ` Tejun Heo
2020-11-24 13:27 ` [dm-devel] [PATCH 24/45] blk-cgroup: stop abusing get_gendisk Christoph Hellwig
2020-11-24 19:46   ` Tejun Heo
2020-11-24 13:27 ` [dm-devel] [PATCH 25/45] block: reference struct block_device from struct hd_struct Christoph Hellwig
2020-11-24 21:18   ` Tejun Heo
2020-11-25 16:45     ` Christoph Hellwig
2020-11-25 20:20       ` Tejun Heo
2020-11-26  8:16         ` Christoph Hellwig
2020-11-24 13:27 ` [dm-devel] [PATCH 26/45] block: remove ->bd_contains Christoph Hellwig
2020-11-24 21:19   ` Tejun Heo
2020-11-24 13:27 ` [dm-devel] [PATCH 27/45] block: simplify the block device claiming interface Christoph Hellwig
2020-11-24 21:19   ` Tejun Heo
2020-11-24 13:27 ` [dm-devel] [PATCH 28/45] block: simplify part_to_disk Christoph Hellwig
2020-11-24 21:20   ` Tejun Heo
2020-11-24 13:27 ` [dm-devel] [PATCH 29/45] block: initialize struct block_device in bdev_alloc Christoph Hellwig
2020-11-24 21:20   ` Tejun Heo
2020-11-24 13:27 ` [dm-devel] [PATCH 30/45] block: remove the nr_sects field in struct hd_struct Christoph Hellwig
2020-11-24 13:34   ` Coly Li
2020-11-25  6:10   ` Chao Yu
2020-11-24 13:27 ` [dm-devel] [PATCH 31/45] block: move disk stat accounting to struct block_device Christoph Hellwig
2020-11-24 13:27 ` [dm-devel] [PATCH 32/45] block: move the start_sect field " Christoph Hellwig
2020-11-24 13:27 ` [dm-devel] [PATCH 33/45] block: move the partition_meta_info " Christoph Hellwig
2020-11-24 13:27 ` [dm-devel] [PATCH 34/45] block: move holder_dir " Christoph Hellwig
2020-11-24 13:27 ` [dm-devel] [PATCH 35/45] block: move make_it_fail " Christoph Hellwig
2020-11-24 13:27 ` [dm-devel] [PATCH 36/45] block: move the policy field " Christoph Hellwig
2020-11-24 13:27 ` [dm-devel] [PATCH 37/45] block: allocate struct hd_struct as part of struct bdev_inode Christoph Hellwig
2020-11-24 13:27 ` [dm-devel] [PATCH 38/45] block: switch partition lookup to use struct block_device Christoph Hellwig
2020-11-24 13:32   ` Coly Li
2020-11-25  6:11   ` Chao Yu
2020-11-24 13:27 ` [dm-devel] [PATCH 39/45] block: remove the partno field from struct hd_struct Christoph Hellwig
2020-11-24 13:27 ` [dm-devel] [PATCH 40/45] block: pass a block_device to blk_alloc_devt Christoph Hellwig
2020-11-24 13:27 ` [dm-devel] [PATCH 41/45] block: pass a block_device to invalidate_partition Christoph Hellwig
2020-11-24 13:27 ` [dm-devel] [PATCH 42/45] block: switch disk_part_iter_* to use a struct block_device Christoph Hellwig
2020-11-24 13:27 ` [dm-devel] [PATCH 43/45] f2fs: remove a few bd_part checks Christoph Hellwig
2020-11-25  6:12   ` Chao Yu
2020-11-24 13:27 ` [dm-devel] [PATCH 44/45] block: merge struct block_device and struct hd_struct Christoph Hellwig
2020-11-24 13:27 ` [dm-devel] [PATCH 45/45] block: stop using bdget_disk for partition 0 Christoph Hellwig
2020-11-28 16:14 [dm-devel] merge struct block_device and struct hd_struct v4 Christoph Hellwig
2020-11-28 16:14 ` [dm-devel] [PATCH 17/45] init: refactor name_to_dev_t Christoph Hellwig
2020-11-30  7:15   ` Hannes Reinecke

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201124132751.3747337-18-hch@lst.de \
    --to=hch@lst.de \
    --cc=axboe@kernel.dk \
    --cc=colyli@suse.de \
    --cc=dm-devel@redhat.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=jack@suse.com \
    --cc=jack@suse.cz \
    --cc=johannes.thumshirn@wdc.com \
    --cc=josef@toxicpanda.com \
    --cc=konrad.wilk@oracle.com \
    --cc=linux-bcache@vger.kernel.org \
    --cc=linux-block@vger.kernel.org \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=linux-mtd@lists.infradead.org \
    --cc=richard@nod.at \
    --cc=snitzer@redhat.com \
    --cc=tj@kernel.org \
    --cc=xen-devel@lists.xenproject.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).