md-autodetect.c 7.6 KB
Newer Older
1
// SPDX-License-Identifier: GPL-2.0
2 3 4 5 6
#include <linux/kernel.h>
#include <linux/blkdev.h>
#include <linux/init.h>
#include <linux/mount.h>
#include <linux/major.h>
7
#include <linux/delay.h>
8
#include <linux/init_syscalls.h>
9
#include <linux/raid/detect.h>
10 11
#include <linux/raid/md_u.h>
#include <linux/raid/md_p.h>
12
#include "md.h"
L
Linus Torvalds 已提交
13 14 15 16 17 18 19 20 21 22

/*
 * When md (and any require personalities) are compiled into the kernel
 * (not a module), arrays can be assembles are boot time using with AUTODETECT
 * where specially marked partitions are registered with md_autodetect_dev(),
 * and with MD_BOOT where devices to be collected are given on the boot line
 * with md=.....
 * The code for that is here.
 */

23 24 25 26 27 28
#ifdef CONFIG_MD_AUTODETECT
static int __initdata raid_noautodetect;
#else
static int __initdata raid_noautodetect=1;
#endif
static int __initdata raid_autopart;
L
Linus Torvalds 已提交
29

C
Christoph Hellwig 已提交
30
static struct md_setup_args {
L
Linus Torvalds 已提交
31 32
	int minor;
	int partitioned;
33
	int level;
L
Linus Torvalds 已提交
34 35
	int chunk;
	char *device_names;
36
} md_setup_args[256] __initdata;
L
Linus Torvalds 已提交
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61

static int md_setup_ents __initdata;

/*
 * Parse the command-line parameters given our kernel, but do not
 * actually try to invoke the MD device now; that is handled by
 * md_setup_drive after the low-level disk drivers have initialised.
 *
 * 27/11/1999: Fixed to work correctly with the 2.3 kernel (which
 *             assigns the task of parsing integer arguments to the
 *             invoked program now).  Added ability to initialise all
 *             the MD devices (by specifying multiple "md=" lines)
 *             instead of just one.  -- KTK
 * 18May2000: Added support for persistent-superblock arrays:
 *             md=n,0,factor,fault,device-list   uses RAID0 for device n
 *             md=n,-1,factor,fault,device-list  uses LINEAR for device n
 *             md=n,device-list      reads a RAID superblock from the devices
 *             elements in device-list are read by name_to_kdev_t so can be
 *             a hex number or something like /dev/hda1 /dev/sdb
 * 2001-06-03: Dave Cinege <dcinege@psychosis.com>
 *		Shifted name_to_kdev_t() and related operations to md_set_drive()
 *		for later execution. Rewrote section to make devfs compatible.
 */
static int __init md_setup(char *str)
{
62
	int minor, level, factor, fault, partitioned = 0;
L
Linus Torvalds 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82
	char *pername = "";
	char *str1;
	int ent;

	if (*str == 'd') {
		partitioned = 1;
		str++;
	}
	if (get_option(&str, &minor) != 2) {	/* MD Number */
		printk(KERN_WARNING "md: Too few arguments supplied to md=.\n");
		return 0;
	}
	str1 = str;
	for (ent=0 ; ent< md_setup_ents ; ent++)
		if (md_setup_args[ent].minor == minor &&
		    md_setup_args[ent].partitioned == partitioned) {
			printk(KERN_WARNING "md: md=%s%d, Specified more than once. "
			       "Replacing previous definition.\n", partitioned?"d":"", minor);
			break;
		}
83
	if (ent >= ARRAY_SIZE(md_setup_args)) {
L
Linus Torvalds 已提交
84 85 86 87 88
		printk(KERN_WARNING "md: md=%s%d - too many md initialisations\n", partitioned?"d":"", minor);
		return 0;
	}
	if (ent >= md_setup_ents)
		md_setup_ents++;
89
	switch (get_option(&str, &level)) {	/* RAID level */
L
Linus Torvalds 已提交
90 91 92 93 94 95 96
	case 2: /* could be 0 or -1.. */
		if (level == 0 || level == LEVEL_LINEAR) {
			if (get_option(&str, &factor) != 2 ||	/* Chunk Size */
					get_option(&str, &fault) != 2) {
				printk(KERN_WARNING "md: Too few arguments supplied to md=.\n");
				return 0;
			}
97
			md_setup_args[ent].level = level;
L
Linus Torvalds 已提交
98
			md_setup_args[ent].chunk = 1 << (factor+12);
99
			if (level ==  LEVEL_LINEAR)
L
Linus Torvalds 已提交
100
				pername = "linear";
101
			else
L
Linus Torvalds 已提交
102 103 104
				pername = "raid0";
			break;
		}
105
		fallthrough;
L
Linus Torvalds 已提交
106 107
	case 1: /* the first device is numeric */
		str = str1;
108
		fallthrough;
L
Linus Torvalds 已提交
109
	case 0:
110
		md_setup_args[ent].level = LEVEL_NONE;
L
Linus Torvalds 已提交
111 112 113 114 115 116 117 118 119 120 121 122
		pername="super-block";
	}

	printk(KERN_INFO "md: Will configure md%d (%s) from %s, below.\n",
		minor, pername, str);
	md_setup_args[ent].device_names = str;
	md_setup_args[ent].partitioned = partitioned;
	md_setup_args[ent].minor = minor;

	return 1;
}

C
Christoph Hellwig 已提交
123
static void __init md_setup_drive(struct md_setup_args *args)
L
Linus Torvalds 已提交
124
{
125 126 127 128 129 130
	char *devname = args->device_names;
	dev_t devices[MD_SB_DISKS + 1], mdev;
	struct mdu_array_info_s ainfo = { };
	struct block_device *bdev;
	struct mddev *mddev;
	int err = 0, i;
C
Christoph Hellwig 已提交
131
	char name[16];
L
Linus Torvalds 已提交
132

133 134 135 136 137 138 139
	if (args->partitioned) {
		mdev = MKDEV(mdp_major, args->minor << MdpMinorShift);
		sprintf(name, "md_d%d", args->minor);
	} else {
		mdev = MKDEV(MD_MAJOR, args->minor);
		sprintf(name, "md%d", args->minor);
	}
L
Linus Torvalds 已提交
140

C
Christoph Hellwig 已提交
141 142 143 144
	for (i = 0; i < MD_SB_DISKS && devname != NULL; i++) {
		struct kstat stat;
		char *p;
		char comp_name[64];
145
		dev_t dev;
L
Linus Torvalds 已提交
146

C
Christoph Hellwig 已提交
147 148 149
		p = strchr(devname, ',');
		if (p)
			*p++ = 0;
L
Linus Torvalds 已提交
150

C
Christoph Hellwig 已提交
151 152 153 154
		dev = name_to_dev_t(devname);
		if (strncmp(devname, "/dev/", 5) == 0)
			devname += 5;
		snprintf(comp_name, 63, "/dev/%s", devname);
155
		if (init_stat(comp_name, &stat, 0) == 0 && S_ISBLK(stat.mode))
C
Christoph Hellwig 已提交
156 157
			dev = new_decode_dev(stat.rdev);
		if (!dev) {
158
			pr_warn("md: Unknown device name: %s\n", devname);
C
Christoph Hellwig 已提交
159 160
			break;
		}
L
Linus Torvalds 已提交
161

C
Christoph Hellwig 已提交
162 163 164 165
		devices[i] = dev;
		devname = p;
	}
	devices[i] = 0;
L
Linus Torvalds 已提交
166

C
Christoph Hellwig 已提交
167 168
	if (!i)
		return;
L
Linus Torvalds 已提交
169

170
	pr_info("md: Loading %s: %s\n", name, args->device_names);
L
Linus Torvalds 已提交
171

172
	md_alloc(mdev, name);
173 174 175
	bdev = blkdev_get_by_dev(mdev, FMODE_READ, NULL);
	if (IS_ERR(bdev)) {
		pr_err("md: open failed - cannot start array %s\n", name);
C
Christoph Hellwig 已提交
176 177
		return;
	}
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196

	err = -EIO;
	if (WARN(bdev->bd_disk->fops != &md_fops,
			"Opening block device %x resulted in non-md device\n",
			mdev))
		goto out_blkdev_put;

	mddev = bdev->bd_disk->private_data;

	err = mddev_lock(mddev);
	if (err) {
		pr_err("md: failed to lock array %s\n", name);
		goto out_blkdev_put;
	}

	if (!list_empty(&mddev->disks) || mddev->raid_disks) {
		pr_warn("md: Ignoring %s, already autodetected. (Use raid=noautodetect)\n",
		       name);
		goto out_unlock;
C
Christoph Hellwig 已提交
197
	}
L
Linus Torvalds 已提交
198

C
Christoph Hellwig 已提交
199 200 201
	if (args->level != LEVEL_NONE) {
		/* non-persistent */
		ainfo.level = args->level;
202
		ainfo.md_minor = args->minor;
C
Christoph Hellwig 已提交
203 204 205
		ainfo.not_persistent = 1;
		ainfo.state = (1 << MD_SB_CLEAN);
		ainfo.chunk_size = args->chunk;
206 207 208 209 210 211 212 213 214 215 216 217 218
		while (devices[ainfo.raid_disks])
			ainfo.raid_disks++;
	}

	err = md_set_array_info(mddev, &ainfo);

	for (i = 0; i <= MD_SB_DISKS && devices[i]; i++) {
		struct mdu_disk_info_s dinfo = {
			.major	= MAJOR(devices[i]),
			.minor	= MINOR(devices[i]),
		};

		if (args->level != LEVEL_NONE) {
C
Christoph Hellwig 已提交
219 220
			dinfo.number = i;
			dinfo.raid_disk = i;
221 222
			dinfo.state =
				(1 << MD_DISK_ACTIVE) | (1 << MD_DISK_SYNC);
L
Linus Torvalds 已提交
223
		}
224 225

		md_add_new_disk(mddev, &dinfo);
L
Linus Torvalds 已提交
226
	}
227

C
Christoph Hellwig 已提交
228
	if (!err)
229
		err = do_md_run(mddev);
C
Christoph Hellwig 已提交
230
	if (err)
231 232 233 234 235
		pr_warn("md: starting %s failed\n", name);
out_unlock:
	mddev_unlock(mddev);
out_blkdev_put:
	blkdev_put(bdev, FMODE_READ);
L
Linus Torvalds 已提交
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253
}

static int __init raid_setup(char *str)
{
	int len, pos;

	len = strlen(str) + 1;
	pos = 0;

	while (pos < len) {
		char *comma = strchr(str+pos, ',');
		int wlen;
		if (comma)
			wlen = (comma-str)-pos;
		else	wlen = (len-1)-pos;

		if (!strncmp(str, "noautodetect", wlen))
			raid_noautodetect = 1;
254 255
		if (!strncmp(str, "autodetect", wlen))
			raid_noautodetect = 0;
L
Linus Torvalds 已提交
256 257 258 259 260 261 262 263 264 265 266 267
		if (strncmp(str, "partitionable", wlen)==0)
			raid_autopart = 1;
		if (strncmp(str, "part", wlen)==0)
			raid_autopart = 1;
		pos += wlen+1;
	}
	return 1;
}

__setup("raid=", raid_setup);
__setup("md=", md_setup);

268
static void __init autodetect_raid(void)
I
Ingo Molnar 已提交
269 270 271 272 273 274 275
{
	/*
	 * Since we don't want to detect and use half a raid array, we need to
	 * wait for the known devices to complete their probing
	 */
	printk(KERN_INFO "md: Waiting for all devices to be available before autodetect\n");
	printk(KERN_INFO "md: If you don't use raid, use raid=noautodetect\n");
276 277

	wait_for_device_probe();
278
	md_autostart_arrays(raid_autopart);
I
Ingo Molnar 已提交
279 280
}

L
Linus Torvalds 已提交
281 282
void __init md_run_setup(void)
{
C
Christoph Hellwig 已提交
283 284
	int ent;

L
Linus Torvalds 已提交
285
	if (raid_noautodetect)
286
		printk(KERN_INFO "md: Skipping autodetection of RAID arrays. (raid=autodetect will force)\n");
I
Ingo Molnar 已提交
287 288
	else
		autodetect_raid();
C
Christoph Hellwig 已提交
289 290 291

	for (ent = 0; ent < md_setup_ents; ent++)
		md_setup_drive(&md_setup_args[ent]);
L
Linus Torvalds 已提交
292
}