Merge tag 'drm-misc-fixes-2023-02-23' of git://anongit.freedesktop.org/drm/drm-misc...
[linux-block.git] / drivers / md / md-autodetect.c
CommitLineData
b2441318 1// SPDX-License-Identifier: GPL-2.0
4f5b246b
CH
2#include <linux/kernel.h>
3#include <linux/blkdev.h>
4#include <linux/init.h>
4f5b246b
CH
5#include <linux/mount.h>
6#include <linux/major.h>
f8b77d39 7#include <linux/delay.h>
716308a5 8#include <linux/init_syscalls.h>
4f5b246b 9#include <linux/raid/detect.h>
bff61975
N
10#include <linux/raid/md_u.h>
11#include <linux/raid/md_p.h>
d82fa81c 12#include "md.h"
1da177e4 13
1da177e4
LT
14/*
15 * When md (and any require personalities) are compiled into the kernel
16 * (not a module), arrays can be assembles are boot time using with AUTODETECT
17 * where specially marked partitions are registered with md_autodetect_dev(),
18 * and with MD_BOOT where devices to be collected are given on the boot line
19 * with md=.....
20 * The code for that is here.
21 */
22
a364092a
AV
23#ifdef CONFIG_MD_AUTODETECT
24static int __initdata raid_noautodetect;
25#else
26static int __initdata raid_noautodetect=1;
27#endif
28static int __initdata raid_autopart;
1da177e4 29
d1100488 30static struct md_setup_args {
1da177e4
LT
31 int minor;
32 int partitioned;
2604b703 33 int level;
1da177e4
LT
34 int chunk;
35 char *device_names;
e8703fe1 36} md_setup_args[256] __initdata;
1da177e4
LT
37
38static int md_setup_ents __initdata;
39
1da177e4
LT
40/*
41 * Parse the command-line parameters given our kernel, but do not
42 * actually try to invoke the MD device now; that is handled by
43 * md_setup_drive after the low-level disk drivers have initialised.
44 *
45 * 27/11/1999: Fixed to work correctly with the 2.3 kernel (which
46 * assigns the task of parsing integer arguments to the
47 * invoked program now). Added ability to initialise all
48 * the MD devices (by specifying multiple "md=" lines)
49 * instead of just one. -- KTK
50 * 18May2000: Added support for persistent-superblock arrays:
51 * md=n,0,factor,fault,device-list uses RAID0 for device n
52 * md=n,-1,factor,fault,device-list uses LINEAR for device n
53 * md=n,device-list reads a RAID superblock from the devices
54 * elements in device-list are read by name_to_kdev_t so can be
55 * a hex number or something like /dev/hda1 /dev/sdb
56 * 2001-06-03: Dave Cinege <dcinege@psychosis.com>
57 * Shifted name_to_kdev_t() and related operations to md_set_drive()
58 * for later execution. Rewrote section to make devfs compatible.
59 */
60static int __init md_setup(char *str)
61{
2604b703 62 int minor, level, factor, fault, partitioned = 0;
1da177e4
LT
63 char *pername = "";
64 char *str1;
65 int ent;
66
67 if (*str == 'd') {
68 partitioned = 1;
69 str++;
70 }
71 if (get_option(&str, &minor) != 2) { /* MD Number */
72 printk(KERN_WARNING "md: Too few arguments supplied to md=.\n");
73 return 0;
74 }
75 str1 = str;
1da177e4
LT
76 for (ent=0 ; ent< md_setup_ents ; ent++)
77 if (md_setup_args[ent].minor == minor &&
78 md_setup_args[ent].partitioned == partitioned) {
79 printk(KERN_WARNING "md: md=%s%d, Specified more than once. "
80 "Replacing previous definition.\n", partitioned?"d":"", minor);
81 break;
82 }
e8703fe1 83 if (ent >= ARRAY_SIZE(md_setup_args)) {
1da177e4
LT
84 printk(KERN_WARNING "md: md=%s%d - too many md initialisations\n", partitioned?"d":"", minor);
85 return 0;
86 }
87 if (ent >= md_setup_ents)
88 md_setup_ents++;
2604b703 89 switch (get_option(&str, &level)) { /* RAID level */
1da177e4
LT
90 case 2: /* could be 0 or -1.. */
91 if (level == 0 || level == LEVEL_LINEAR) {
92 if (get_option(&str, &factor) != 2 || /* Chunk Size */
93 get_option(&str, &fault) != 2) {
94 printk(KERN_WARNING "md: Too few arguments supplied to md=.\n");
95 return 0;
96 }
2604b703 97 md_setup_args[ent].level = level;
1da177e4 98 md_setup_args[ent].chunk = 1 << (factor+12);
2604b703 99 if (level == LEVEL_LINEAR)
1da177e4 100 pername = "linear";
2604b703 101 else
1da177e4 102 pername = "raid0";
1da177e4
LT
103 break;
104 }
df561f66 105 fallthrough;
1da177e4
LT
106 case 1: /* the first device is numeric */
107 str = str1;
df561f66 108 fallthrough;
1da177e4 109 case 0:
2604b703 110 md_setup_args[ent].level = LEVEL_NONE;
1da177e4
LT
111 pername="super-block";
112 }
113
114 printk(KERN_INFO "md: Will configure md%d (%s) from %s, below.\n",
115 minor, pername, str);
116 md_setup_args[ent].device_names = str;
117 md_setup_args[ent].partitioned = partitioned;
118 md_setup_args[ent].minor = minor;
119
120 return 1;
121}
122
d1100488 123static void __init md_setup_drive(struct md_setup_args *args)
1da177e4 124{
7e0adbfc
CH
125 char *devname = args->device_names;
126 dev_t devices[MD_SB_DISKS + 1], mdev;
127 struct mdu_array_info_s ainfo = { };
7e0adbfc
CH
128 struct mddev *mddev;
129 int err = 0, i;
d1100488 130 char name[16];
1da177e4 131
7e0adbfc
CH
132 if (args->partitioned) {
133 mdev = MKDEV(mdp_major, args->minor << MdpMinorShift);
134 sprintf(name, "md_d%d", args->minor);
135 } else {
136 mdev = MKDEV(MD_MAJOR, args->minor);
137 sprintf(name, "md%d", args->minor);
138 }
1da177e4 139
d1100488
CH
140 for (i = 0; i < MD_SB_DISKS && devname != NULL; i++) {
141 struct kstat stat;
142 char *p;
143 char comp_name[64];
7e0adbfc 144 dev_t dev;
1da177e4 145
d1100488
CH
146 p = strchr(devname, ',');
147 if (p)
148 *p++ = 0;
1da177e4 149
d1100488
CH
150 dev = name_to_dev_t(devname);
151 if (strncmp(devname, "/dev/", 5) == 0)
152 devname += 5;
153 snprintf(comp_name, 63, "/dev/%s", devname);
716308a5 154 if (init_stat(comp_name, &stat, 0) == 0 && S_ISBLK(stat.mode))
d1100488
CH
155 dev = new_decode_dev(stat.rdev);
156 if (!dev) {
7e0adbfc 157 pr_warn("md: Unknown device name: %s\n", devname);
d1100488
CH
158 break;
159 }
1da177e4 160
d1100488
CH
161 devices[i] = dev;
162 devname = p;
163 }
164 devices[i] = 0;
1da177e4 165
d1100488
CH
166 if (!i)
167 return;
1da177e4 168
7e0adbfc 169 pr_info("md: Loading %s: %s\n", name, args->device_names);
1da177e4 170
34cb92c0
CH
171 mddev = md_alloc(mdev, name);
172 if (IS_ERR(mddev)) {
173 pr_err("md: md_alloc failed - cannot start array %s\n", name);
d1100488
CH
174 return;
175 }
7e0adbfc 176
7e0adbfc
CH
177 err = mddev_lock(mddev);
178 if (err) {
179 pr_err("md: failed to lock array %s\n", name);
34cb92c0 180 goto out_mddev_put;
7e0adbfc
CH
181 }
182
183 if (!list_empty(&mddev->disks) || mddev->raid_disks) {
184 pr_warn("md: Ignoring %s, already autodetected. (Use raid=noautodetect)\n",
185 name);
186 goto out_unlock;
d1100488 187 }
1da177e4 188
d1100488
CH
189 if (args->level != LEVEL_NONE) {
190 /* non-persistent */
d1100488 191 ainfo.level = args->level;
7e0adbfc 192 ainfo.md_minor = args->minor;
d1100488 193 ainfo.not_persistent = 1;
d1100488 194 ainfo.state = (1 << MD_SB_CLEAN);
d1100488 195 ainfo.chunk_size = args->chunk;
7e0adbfc
CH
196 while (devices[ainfo.raid_disks])
197 ainfo.raid_disks++;
198 }
199
200 err = md_set_array_info(mddev, &ainfo);
201
202 for (i = 0; i <= MD_SB_DISKS && devices[i]; i++) {
203 struct mdu_disk_info_s dinfo = {
204 .major = MAJOR(devices[i]),
205 .minor = MINOR(devices[i]),
206 };
207
208 if (args->level != LEVEL_NONE) {
d1100488
CH
209 dinfo.number = i;
210 dinfo.raid_disk = i;
7e0adbfc
CH
211 dinfo.state =
212 (1 << MD_DISK_ACTIVE) | (1 << MD_DISK_SYNC);
1da177e4 213 }
7e0adbfc
CH
214
215 md_add_new_disk(mddev, &dinfo);
1da177e4 216 }
7e0adbfc 217
d1100488 218 if (!err)
7e0adbfc 219 err = do_md_run(mddev);
d1100488 220 if (err)
7e0adbfc
CH
221 pr_warn("md: starting %s failed\n", name);
222out_unlock:
223 mddev_unlock(mddev);
34cb92c0
CH
224out_mddev_put:
225 mddev_put(mddev);
1da177e4
LT
226}
227
228static int __init raid_setup(char *str)
229{
230 int len, pos;
231
232 len = strlen(str) + 1;
233 pos = 0;
234
235 while (pos < len) {
236 char *comma = strchr(str+pos, ',');
237 int wlen;
238 if (comma)
239 wlen = (comma-str)-pos;
240 else wlen = (len-1)-pos;
241
242 if (!strncmp(str, "noautodetect", wlen))
243 raid_noautodetect = 1;
a364092a
AV
244 if (!strncmp(str, "autodetect", wlen))
245 raid_noautodetect = 0;
1da177e4
LT
246 if (strncmp(str, "partitionable", wlen)==0)
247 raid_autopart = 1;
248 if (strncmp(str, "part", wlen)==0)
249 raid_autopart = 1;
250 pos += wlen+1;
251 }
252 return 1;
253}
254
255__setup("raid=", raid_setup);
256__setup("md=", md_setup);
257
ff083c83 258static void __init autodetect_raid(void)
82cbc11a 259{
82cbc11a
IM
260 /*
261 * Since we don't want to detect and use half a raid array, we need to
262 * wait for the known devices to complete their probing
263 */
264 printk(KERN_INFO "md: Waiting for all devices to be available before autodetect\n");
265 printk(KERN_INFO "md: If you don't use raid, use raid=noautodetect\n");
216773a7
AV
266
267 wait_for_device_probe();
d82fa81c 268 md_autostart_arrays(raid_autopart);
82cbc11a
IM
269}
270
1da177e4
LT
271void __init md_run_setup(void)
272{
d1100488
CH
273 int ent;
274
1da177e4 275 if (raid_noautodetect)
a364092a 276 printk(KERN_INFO "md: Skipping autodetection of RAID arrays. (raid=autodetect will force)\n");
82cbc11a
IM
277 else
278 autodetect_raid();
d1100488
CH
279
280 for (ent = 0; ent < md_setup_ents; ent++)
281 md_setup_drive(&md_setup_args[ent]);
1da177e4 282}