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