Commit 830cf3f7 authored by Christoph Hellwig's avatar Christoph Hellwig Committed by openeuler-sync-bot
Browse files

md: factor out a mddev_alloc_unit helper from mddev_find

mainline inclusion
from mainline-v5.13-rc1
commit 85c8c3c1
category: bugfix
bugzilla: https://gitee.com/openeuler/kernel/issues/I8O6NL
CVE: NA

Reference: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=85c8c3c1f8d9e31f626c93435dd91c2f85603e07



-------------------------------------------------

Split out a self contained helper to find a free minor for the md
"unit" number.

Signed-off-by: default avatarChristoph Hellwig <hch@lst.de>
Signed-off-by: default avatarSong Liu <song@kernel.org>
Signed-off-by: default avatarLi Lingfeng <lilingfeng3@huawei.com>
(cherry picked from commit 38f910c8)
parent 3b063171
Loading
Loading
Loading
Loading
+27 −20
Original line number Diff line number Diff line
@@ -723,6 +723,27 @@ static struct mddev *mddev_find_locked(dev_t unit)
	return NULL;
}

/* find an unused unit number */
static dev_t mddev_alloc_unit(void)
{
	static int next_minor = 512;
	int start = next_minor;
	bool is_free = 0;
	dev_t dev = 0;

	while (!is_free) {
		dev = MKDEV(MD_MAJOR, next_minor);
		next_minor++;
		if (next_minor > MINORMASK)
			next_minor = 0;
		if (next_minor == start)
			return 0;		/* Oh dear, all in use. */
		is_free = !mddev_find_locked(dev);
	}

	return dev;
}

static struct mddev *mddev_find(dev_t unit)
{
	struct mddev *mddev;
@@ -765,27 +786,13 @@ static struct mddev *mddev_find_or_alloc(dev_t unit)
			return new;
		}
	} else if (new) {
		/* find an unused unit number */
		static int next_minor = 512;
		int start = next_minor;
		int is_free = 0;
		int dev = 0;
		while (!is_free) {
			dev = MKDEV(MD_MAJOR, next_minor);
			next_minor++;
			if (next_minor > MINORMASK)
				next_minor = 0;
			if (next_minor == start) {
				/* Oh dear, all in use. */
		new->unit = mddev_alloc_unit();
		if (!new->unit) {
			spin_unlock(&all_mddevs_lock);
			kfree(new);
			return NULL;
		}

			is_free = !mddev_find_locked(dev);
		}
		new->unit = dev;
		new->md_minor = MINOR(dev);
		new->md_minor = MINOR(new->unit);
		new->hold_active = UNTIL_STOP;
		list_add(&new->all_mddevs, &all_mddevs);
		spin_unlock(&all_mddevs_lock);