blob: 8cb6db54285ba64f81af9ba2b388a7c216ceec61 [file] [log] [blame]
H Hartley Sweetenc67e5382012-05-31 16:26:10 -07001/*
2 * Many of the syscalls used in this file expect some of the arguments
3 * to be __user pointers not __kernel pointers. To limit the sparse
4 * noise, turn off sparse checking for this file.
5 */
6#ifdef __CHECKER__
7#undef __CHECKER__
8#warning "Sparse checking disabled for this file"
9#endif
10
Alexey Dobriyanf8b77d32008-10-29 14:01:05 -070011#include <linux/delay.h>
NeilBrownbff61972009-03-31 14:33:13 +110012#include <linux/raid/md_u.h>
13#include <linux/raid/md_p.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070014
15#include "do_mounts.h"
16
17/*
18 * When md (and any require personalities) are compiled into the kernel
19 * (not a module), arrays can be assembles are boot time using with AUTODETECT
20 * where specially marked partitions are registered with md_autodetect_dev(),
21 * and with MD_BOOT where devices to be collected are given on the boot line
22 * with md=.....
23 * The code for that is here.
24 */
25
Arjan van de Vena3640922008-09-21 15:44:32 -070026#ifdef CONFIG_MD_AUTODETECT
27static int __initdata raid_noautodetect;
28#else
29static int __initdata raid_noautodetect=1;
30#endif
31static int __initdata raid_autopart;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33static struct {
34 int minor;
35 int partitioned;
NeilBrown2604b702006-01-06 00:20:36 -080036 int level;
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 int chunk;
38 char *device_names;
NeilBrowne8703fe2006-10-03 01:15:59 -070039} md_setup_args[256] __initdata;
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41static int md_setup_ents __initdata;
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043/*
44 * Parse the command-line parameters given our kernel, but do not
45 * actually try to invoke the MD device now; that is handled by
46 * md_setup_drive after the low-level disk drivers have initialised.
47 *
48 * 27/11/1999: Fixed to work correctly with the 2.3 kernel (which
49 * assigns the task of parsing integer arguments to the
50 * invoked program now). Added ability to initialise all
51 * the MD devices (by specifying multiple "md=" lines)
52 * instead of just one. -- KTK
53 * 18May2000: Added support for persistent-superblock arrays:
54 * md=n,0,factor,fault,device-list uses RAID0 for device n
55 * md=n,-1,factor,fault,device-list uses LINEAR for device n
56 * md=n,device-list reads a RAID superblock from the devices
57 * elements in device-list are read by name_to_kdev_t so can be
58 * a hex number or something like /dev/hda1 /dev/sdb
59 * 2001-06-03: Dave Cinege <dcinege@psychosis.com>
60 * Shifted name_to_kdev_t() and related operations to md_set_drive()
61 * for later execution. Rewrote section to make devfs compatible.
62 */
63static int __init md_setup(char *str)
64{
NeilBrown2604b702006-01-06 00:20:36 -080065 int minor, level, factor, fault, partitioned = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 char *pername = "";
67 char *str1;
68 int ent;
69
70 if (*str == 'd') {
71 partitioned = 1;
72 str++;
73 }
74 if (get_option(&str, &minor) != 2) { /* MD Number */
75 printk(KERN_WARNING "md: Too few arguments supplied to md=.\n");
76 return 0;
77 }
78 str1 = str;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 for (ent=0 ; ent< md_setup_ents ; ent++)
80 if (md_setup_args[ent].minor == minor &&
81 md_setup_args[ent].partitioned == partitioned) {
82 printk(KERN_WARNING "md: md=%s%d, Specified more than once. "
83 "Replacing previous definition.\n", partitioned?"d":"", minor);
84 break;
85 }
NeilBrowne8703fe2006-10-03 01:15:59 -070086 if (ent >= ARRAY_SIZE(md_setup_args)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 printk(KERN_WARNING "md: md=%s%d - too many md initialisations\n", partitioned?"d":"", minor);
88 return 0;
89 }
90 if (ent >= md_setup_ents)
91 md_setup_ents++;
NeilBrown2604b702006-01-06 00:20:36 -080092 switch (get_option(&str, &level)) { /* RAID level */
Linus Torvalds1da177e2005-04-16 15:20:36 -070093 case 2: /* could be 0 or -1.. */
94 if (level == 0 || level == LEVEL_LINEAR) {
95 if (get_option(&str, &factor) != 2 || /* Chunk Size */
96 get_option(&str, &fault) != 2) {
97 printk(KERN_WARNING "md: Too few arguments supplied to md=.\n");
98 return 0;
99 }
NeilBrown2604b702006-01-06 00:20:36 -0800100 md_setup_args[ent].level = level;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 md_setup_args[ent].chunk = 1 << (factor+12);
NeilBrown2604b702006-01-06 00:20:36 -0800102 if (level == LEVEL_LINEAR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 pername = "linear";
NeilBrown2604b702006-01-06 00:20:36 -0800104 else
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 pername = "raid0";
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 break;
107 }
108 /* FALL THROUGH */
109 case 1: /* the first device is numeric */
110 str = str1;
111 /* FALL THROUGH */
112 case 0:
NeilBrown2604b702006-01-06 00:20:36 -0800113 md_setup_args[ent].level = LEVEL_NONE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 pername="super-block";
115 }
116
117 printk(KERN_INFO "md: Will configure md%d (%s) from %s, below.\n",
118 minor, pername, str);
119 md_setup_args[ent].device_names = str;
120 md_setup_args[ent].partitioned = partitioned;
121 md_setup_args[ent].minor = minor;
122
123 return 1;
124}
125
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126static void __init md_setup_drive(void)
127{
128 int minor, i, ent, partitioned;
129 dev_t dev;
130 dev_t devices[MD_SB_DISKS+1];
131
132 for (ent = 0; ent < md_setup_ents ; ent++) {
133 int fd;
134 int err = 0;
135 char *devname;
136 mdu_disk_info_t dinfo;
Greg Kroah-Hartmanbdaf8522005-06-20 21:15:16 -0700137 char name[16];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 minor = md_setup_args[ent].minor;
140 partitioned = md_setup_args[ent].partitioned;
141 devname = md_setup_args[ent].device_names;
142
143 sprintf(name, "/dev/md%s%d", partitioned?"_d":"", minor);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 if (partitioned)
145 dev = MKDEV(mdp_major, minor << MdpMinorShift);
146 else
147 dev = MKDEV(MD_MAJOR, minor);
Greg Kroah-Hartmanbdaf8522005-06-20 21:15:16 -0700148 create_dev(name, dev);
Harvey Harrisond613c3e2008-04-28 14:13:14 -0700149 for (i = 0; i < MD_SB_DISKS && devname != NULL; i++) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 char *p;
151 char comp_name[64];
152 u32 rdev;
153
154 p = strchr(devname, ',');
155 if (p)
156 *p++ = 0;
157
158 dev = name_to_dev_t(devname);
159 if (strncmp(devname, "/dev/", 5) == 0)
160 devname += 5;
161 snprintf(comp_name, 63, "/dev/%s", devname);
162 rdev = bstat(comp_name);
163 if (rdev)
164 dev = new_decode_dev(rdev);
165 if (!dev) {
166 printk(KERN_WARNING "md: Unknown device name: %s\n", devname);
167 break;
168 }
169
170 devices[i] = dev;
171
172 devname = p;
173 }
174 devices[i] = 0;
175
176 if (!i)
177 continue;
178
179 printk(KERN_INFO "md: Loading md%s%d: %s\n",
180 partitioned ? "_d" : "", minor,
181 md_setup_args[ent].device_names);
182
183 fd = sys_open(name, 0, 0);
184 if (fd < 0) {
185 printk(KERN_ERR "md: open failed - cannot start "
186 "array %s\n", name);
187 continue;
188 }
189 if (sys_ioctl(fd, SET_ARRAY_INFO, 0) == -EBUSY) {
190 printk(KERN_WARNING
191 "md: Ignoring md=%d, already autodetected. (Use raid=noautodetect)\n",
192 minor);
193 sys_close(fd);
194 continue;
195 }
196
NeilBrown2604b702006-01-06 00:20:36 -0800197 if (md_setup_args[ent].level != LEVEL_NONE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 /* non-persistent */
199 mdu_array_info_t ainfo;
NeilBrown2604b702006-01-06 00:20:36 -0800200 ainfo.level = md_setup_args[ent].level;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 ainfo.size = 0;
202 ainfo.nr_disks =0;
203 ainfo.raid_disks =0;
204 while (devices[ainfo.raid_disks])
205 ainfo.raid_disks++;
206 ainfo.md_minor =minor;
207 ainfo.not_persistent = 1;
208
209 ainfo.state = (1 << MD_SB_CLEAN);
210 ainfo.layout = 0;
211 ainfo.chunk_size = md_setup_args[ent].chunk;
212 err = sys_ioctl(fd, SET_ARRAY_INFO, (long)&ainfo);
213 for (i = 0; !err && i <= MD_SB_DISKS; i++) {
214 dev = devices[i];
215 if (!dev)
216 break;
217 dinfo.number = i;
218 dinfo.raid_disk = i;
219 dinfo.state = (1<<MD_DISK_ACTIVE)|(1<<MD_DISK_SYNC);
220 dinfo.major = MAJOR(dev);
221 dinfo.minor = MINOR(dev);
222 err = sys_ioctl(fd, ADD_NEW_DISK, (long)&dinfo);
223 }
224 } else {
225 /* persistent */
226 for (i = 0; i <= MD_SB_DISKS; i++) {
227 dev = devices[i];
228 if (!dev)
229 break;
230 dinfo.major = MAJOR(dev);
231 dinfo.minor = MINOR(dev);
232 sys_ioctl(fd, ADD_NEW_DISK, (long)&dinfo);
233 }
234 }
235 if (!err)
236 err = sys_ioctl(fd, RUN_ARRAY, 0);
237 if (err)
238 printk(KERN_WARNING "md: starting md%d failed\n", minor);
239 else {
240 /* reread the partition table.
241 * I (neilb) and not sure why this is needed, but I cannot
242 * boot a kernel with devfs compiled in from partitioned md
243 * array without it
244 */
245 sys_close(fd);
246 fd = sys_open(name, 0, 0);
247 sys_ioctl(fd, BLKRRPART, 0);
248 }
249 sys_close(fd);
250 }
251}
252
253static int __init raid_setup(char *str)
254{
255 int len, pos;
256
257 len = strlen(str) + 1;
258 pos = 0;
259
260 while (pos < len) {
261 char *comma = strchr(str+pos, ',');
262 int wlen;
263 if (comma)
264 wlen = (comma-str)-pos;
265 else wlen = (len-1)-pos;
266
267 if (!strncmp(str, "noautodetect", wlen))
268 raid_noautodetect = 1;
Arjan van de Vena3640922008-09-21 15:44:32 -0700269 if (!strncmp(str, "autodetect", wlen))
270 raid_noautodetect = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 if (strncmp(str, "partitionable", wlen)==0)
272 raid_autopart = 1;
273 if (strncmp(str, "part", wlen)==0)
274 raid_autopart = 1;
275 pos += wlen+1;
276 }
277 return 1;
278}
279
280__setup("raid=", raid_setup);
281__setup("md=", md_setup);
282
Mike Frysingerff083c82009-01-06 14:40:53 -0800283static void __init autodetect_raid(void)
Ingo Molnar82cbc112008-08-18 12:54:00 +0200284{
285 int fd;
286
287 /*
288 * Since we don't want to detect and use half a raid array, we need to
289 * wait for the known devices to complete their probing
290 */
291 printk(KERN_INFO "md: Waiting for all devices to be available before autodetect\n");
292 printk(KERN_INFO "md: If you don't use raid, use raid=noautodetect\n");
Arjan van de Ven216773a2009-02-14 01:59:06 +0100293
294 wait_for_device_probe();
295
H Hartley Sweetenc67e5382012-05-31 16:26:10 -0700296 fd = sys_open("/dev/md0", 0, 0);
Ingo Molnar82cbc112008-08-18 12:54:00 +0200297 if (fd >= 0) {
298 sys_ioctl(fd, RAID_AUTORUN, raid_autopart);
299 sys_close(fd);
300 }
301}
302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303void __init md_run_setup(void)
304{
Greg Kroah-Hartmanbdaf8522005-06-20 21:15:16 -0700305 create_dev("/dev/md0", MKDEV(MD_MAJOR, 0));
Arjan van de Ven589f8002008-07-20 13:07:09 -0700306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 if (raid_noautodetect)
Arjan van de Vena3640922008-09-21 15:44:32 -0700308 printk(KERN_INFO "md: Skipping autodetection of RAID arrays. (raid=autodetect will force)\n");
Ingo Molnar82cbc112008-08-18 12:54:00 +0200309 else
310 autodetect_raid();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 md_setup_drive();
312}