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