blob: 4c87ee1fe5d30fe1783f0800fde7e3888aad298e [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001
2#include <linux/raid/md.h>
Adrian Bunk73b4a242008-10-16 23:29:21 +03003#include <linux/delay.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -07004
5#include "do_mounts.h"
6
7/*
8 * When md (and any require personalities) are compiled into the kernel
9 * (not a module), arrays can be assembles are boot time using with AUTODETECT
10 * where specially marked partitions are registered with md_autodetect_dev(),
11 * and with MD_BOOT where devices to be collected are given on the boot line
12 * with md=.....
13 * The code for that is here.
14 */
15
Arjan van de Vena3640922008-09-21 15:44:32 -070016#ifdef CONFIG_MD_AUTODETECT
17static int __initdata raid_noautodetect;
18#else
19static int __initdata raid_noautodetect=1;
20#endif
21static int __initdata raid_autopart;
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23static struct {
24 int minor;
25 int partitioned;
NeilBrown2604b702006-01-06 00:20:36 -080026 int level;
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 int chunk;
28 char *device_names;
NeilBrowne8703fe2006-10-03 01:15:59 -070029} md_setup_args[256] __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030
31static int md_setup_ents __initdata;
32
Linus Torvalds1da177e2005-04-16 15:20:36 -070033/*
34 * Parse the command-line parameters given our kernel, but do not
35 * actually try to invoke the MD device now; that is handled by
36 * md_setup_drive after the low-level disk drivers have initialised.
37 *
38 * 27/11/1999: Fixed to work correctly with the 2.3 kernel (which
39 * assigns the task of parsing integer arguments to the
40 * invoked program now). Added ability to initialise all
41 * the MD devices (by specifying multiple "md=" lines)
42 * instead of just one. -- KTK
43 * 18May2000: Added support for persistent-superblock arrays:
44 * md=n,0,factor,fault,device-list uses RAID0 for device n
45 * md=n,-1,factor,fault,device-list uses LINEAR for device n
46 * md=n,device-list reads a RAID superblock from the devices
47 * elements in device-list are read by name_to_kdev_t so can be
48 * a hex number or something like /dev/hda1 /dev/sdb
49 * 2001-06-03: Dave Cinege <dcinege@psychosis.com>
50 * Shifted name_to_kdev_t() and related operations to md_set_drive()
51 * for later execution. Rewrote section to make devfs compatible.
52 */
53static int __init md_setup(char *str)
54{
NeilBrown2604b702006-01-06 00:20:36 -080055 int minor, level, factor, fault, partitioned = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 char *pername = "";
57 char *str1;
58 int ent;
59
60 if (*str == 'd') {
61 partitioned = 1;
62 str++;
63 }
64 if (get_option(&str, &minor) != 2) { /* MD Number */
65 printk(KERN_WARNING "md: Too few arguments supplied to md=.\n");
66 return 0;
67 }
68 str1 = str;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 for (ent=0 ; ent< md_setup_ents ; ent++)
70 if (md_setup_args[ent].minor == minor &&
71 md_setup_args[ent].partitioned == partitioned) {
72 printk(KERN_WARNING "md: md=%s%d, Specified more than once. "
73 "Replacing previous definition.\n", partitioned?"d":"", minor);
74 break;
75 }
NeilBrowne8703fe2006-10-03 01:15:59 -070076 if (ent >= ARRAY_SIZE(md_setup_args)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070077 printk(KERN_WARNING "md: md=%s%d - too many md initialisations\n", partitioned?"d":"", minor);
78 return 0;
79 }
80 if (ent >= md_setup_ents)
81 md_setup_ents++;
NeilBrown2604b702006-01-06 00:20:36 -080082 switch (get_option(&str, &level)) { /* RAID level */
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 case 2: /* could be 0 or -1.. */
84 if (level == 0 || level == LEVEL_LINEAR) {
85 if (get_option(&str, &factor) != 2 || /* Chunk Size */
86 get_option(&str, &fault) != 2) {
87 printk(KERN_WARNING "md: Too few arguments supplied to md=.\n");
88 return 0;
89 }
NeilBrown2604b702006-01-06 00:20:36 -080090 md_setup_args[ent].level = level;
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 md_setup_args[ent].chunk = 1 << (factor+12);
NeilBrown2604b702006-01-06 00:20:36 -080092 if (level == LEVEL_LINEAR)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 pername = "linear";
NeilBrown2604b702006-01-06 00:20:36 -080094 else
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 pername = "raid0";
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 break;
97 }
98 /* FALL THROUGH */
99 case 1: /* the first device is numeric */
100 str = str1;
101 /* FALL THROUGH */
102 case 0:
NeilBrown2604b702006-01-06 00:20:36 -0800103 md_setup_args[ent].level = LEVEL_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 pername="super-block";
105 }
106
107 printk(KERN_INFO "md: Will configure md%d (%s) from %s, below.\n",
108 minor, pername, str);
109 md_setup_args[ent].device_names = str;
110 md_setup_args[ent].partitioned = partitioned;
111 md_setup_args[ent].minor = minor;
112
113 return 1;
114}
115
116#define MdpMinorShift 6
117
118static void __init md_setup_drive(void)
119{
120 int minor, i, ent, partitioned;
121 dev_t dev;
122 dev_t devices[MD_SB_DISKS+1];
123
124 for (ent = 0; ent < md_setup_ents ; ent++) {
125 int fd;
126 int err = 0;
127 char *devname;
128 mdu_disk_info_t dinfo;
Greg Kroah-Hartmanbdaf8522005-06-20 21:15:16 -0700129 char name[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
131 minor = md_setup_args[ent].minor;
132 partitioned = md_setup_args[ent].partitioned;
133 devname = md_setup_args[ent].device_names;
134
135 sprintf(name, "/dev/md%s%d", partitioned?"_d":"", minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 if (partitioned)
137 dev = MKDEV(mdp_major, minor << MdpMinorShift);
138 else
139 dev = MKDEV(MD_MAJOR, minor);
Greg Kroah-Hartmanbdaf8522005-06-20 21:15:16 -0700140 create_dev(name, dev);
Harvey Harrisond613c3e2008-04-28 14:13:14 -0700141 for (i = 0; i < MD_SB_DISKS && devname != NULL; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 char *p;
143 char comp_name[64];
144 u32 rdev;
145
146 p = strchr(devname, ',');
147 if (p)
148 *p++ = 0;
149
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);
154 rdev = bstat(comp_name);
155 if (rdev)
156 dev = new_decode_dev(rdev);
157 if (!dev) {
158 printk(KERN_WARNING "md: Unknown device name: %s\n", devname);
159 break;
160 }
161
162 devices[i] = dev;
163
164 devname = p;
165 }
166 devices[i] = 0;
167
168 if (!i)
169 continue;
170
171 printk(KERN_INFO "md: Loading md%s%d: %s\n",
172 partitioned ? "_d" : "", minor,
173 md_setup_args[ent].device_names);
174
175 fd = sys_open(name, 0, 0);
176 if (fd < 0) {
177 printk(KERN_ERR "md: open failed - cannot start "
178 "array %s\n", name);
179 continue;
180 }
181 if (sys_ioctl(fd, SET_ARRAY_INFO, 0) == -EBUSY) {
182 printk(KERN_WARNING
183 "md: Ignoring md=%d, already autodetected. (Use raid=noautodetect)\n",
184 minor);
185 sys_close(fd);
186 continue;
187 }
188
NeilBrown2604b702006-01-06 00:20:36 -0800189 if (md_setup_args[ent].level != LEVEL_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 /* non-persistent */
191 mdu_array_info_t ainfo;
NeilBrown2604b702006-01-06 00:20:36 -0800192 ainfo.level = md_setup_args[ent].level;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 ainfo.size = 0;
194 ainfo.nr_disks =0;
195 ainfo.raid_disks =0;
196 while (devices[ainfo.raid_disks])
197 ainfo.raid_disks++;
198 ainfo.md_minor =minor;
199 ainfo.not_persistent = 1;
200
201 ainfo.state = (1 << MD_SB_CLEAN);
202 ainfo.layout = 0;
203 ainfo.chunk_size = md_setup_args[ent].chunk;
204 err = sys_ioctl(fd, SET_ARRAY_INFO, (long)&ainfo);
205 for (i = 0; !err && i <= MD_SB_DISKS; i++) {
206 dev = devices[i];
207 if (!dev)
208 break;
209 dinfo.number = i;
210 dinfo.raid_disk = i;
211 dinfo.state = (1<<MD_DISK_ACTIVE)|(1<<MD_DISK_SYNC);
212 dinfo.major = MAJOR(dev);
213 dinfo.minor = MINOR(dev);
214 err = sys_ioctl(fd, ADD_NEW_DISK, (long)&dinfo);
215 }
216 } else {
217 /* persistent */
218 for (i = 0; i <= MD_SB_DISKS; i++) {
219 dev = devices[i];
220 if (!dev)
221 break;
222 dinfo.major = MAJOR(dev);
223 dinfo.minor = MINOR(dev);
224 sys_ioctl(fd, ADD_NEW_DISK, (long)&dinfo);
225 }
226 }
227 if (!err)
228 err = sys_ioctl(fd, RUN_ARRAY, 0);
229 if (err)
230 printk(KERN_WARNING "md: starting md%d failed\n", minor);
231 else {
232 /* reread the partition table.
233 * I (neilb) and not sure why this is needed, but I cannot
234 * boot a kernel with devfs compiled in from partitioned md
235 * array without it
236 */
237 sys_close(fd);
238 fd = sys_open(name, 0, 0);
239 sys_ioctl(fd, BLKRRPART, 0);
240 }
241 sys_close(fd);
242 }
243}
244
245static int __init raid_setup(char *str)
246{
247 int len, pos;
248
249 len = strlen(str) + 1;
250 pos = 0;
251
252 while (pos < len) {
253 char *comma = strchr(str+pos, ',');
254 int wlen;
255 if (comma)
256 wlen = (comma-str)-pos;
257 else wlen = (len-1)-pos;
258
259 if (!strncmp(str, "noautodetect", wlen))
260 raid_noautodetect = 1;
Arjan van de Vena3640922008-09-21 15:44:32 -0700261 if (!strncmp(str, "autodetect", wlen))
262 raid_noautodetect = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 if (strncmp(str, "partitionable", wlen)==0)
264 raid_autopart = 1;
265 if (strncmp(str, "part", wlen)==0)
266 raid_autopart = 1;
267 pos += wlen+1;
268 }
269 return 1;
270}
271
272__setup("raid=", raid_setup);
273__setup("md=", md_setup);
274
Ingo Molnar82cbc112008-08-18 12:54:00 +0200275static void autodetect_raid(void)
276{
277 int fd;
278
279 /*
280 * Since we don't want to detect and use half a raid array, we need to
281 * wait for the known devices to complete their probing
282 */
283 printk(KERN_INFO "md: Waiting for all devices to be available before autodetect\n");
284 printk(KERN_INFO "md: If you don't use raid, use raid=noautodetect\n");
285 while (driver_probe_done() < 0)
286 msleep(100);
287 fd = sys_open("/dev/md0", 0, 0);
288 if (fd >= 0) {
289 sys_ioctl(fd, RAID_AUTORUN, raid_autopart);
290 sys_close(fd);
291 }
292}
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294void __init md_run_setup(void)
295{
Greg Kroah-Hartmanbdaf8522005-06-20 21:15:16 -0700296 create_dev("/dev/md0", MKDEV(MD_MAJOR, 0));
Arjan van de Ven589f8002008-07-20 13:07:09 -0700297
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298 if (raid_noautodetect)
Arjan van de Vena3640922008-09-21 15:44:32 -0700299 printk(KERN_INFO "md: Skipping autodetection of RAID arrays. (raid=autodetect will force)\n");
Ingo Molnar82cbc112008-08-18 12:54:00 +0200300 else
301 autodetect_raid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 md_setup_drive();
303}