Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Alexey Dobriyan | f8b77d3 | 2008-10-29 14:01:05 -0700 | [diff] [blame] | 2 | #include <linux/delay.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 3 | #include <linux/raid/md_u.h> |
| 4 | #include <linux/raid/md_p.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | |
| 6 | #include "do_mounts.h" |
| 7 | |
| 8 | /* |
| 9 | * When md (and any require personalities) are compiled into the kernel |
| 10 | * (not a module), arrays can be assembles are boot time using with AUTODETECT |
| 11 | * where specially marked partitions are registered with md_autodetect_dev(), |
| 12 | * and with MD_BOOT where devices to be collected are given on the boot line |
| 13 | * with md=..... |
| 14 | * The code for that is here. |
| 15 | */ |
| 16 | |
Arjan van de Ven | a364092 | 2008-09-21 15:44:32 -0700 | [diff] [blame] | 17 | #ifdef CONFIG_MD_AUTODETECT |
| 18 | static int __initdata raid_noautodetect; |
| 19 | #else |
| 20 | static int __initdata raid_noautodetect=1; |
| 21 | #endif |
| 22 | static int __initdata raid_autopart; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
| 24 | static struct { |
| 25 | int minor; |
| 26 | int partitioned; |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 27 | int level; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | int chunk; |
| 29 | char *device_names; |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 30 | } md_setup_args[256] __initdata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | |
| 32 | static int md_setup_ents __initdata; |
| 33 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | /* |
| 35 | * Parse the command-line parameters given our kernel, but do not |
| 36 | * actually try to invoke the MD device now; that is handled by |
| 37 | * md_setup_drive after the low-level disk drivers have initialised. |
| 38 | * |
| 39 | * 27/11/1999: Fixed to work correctly with the 2.3 kernel (which |
| 40 | * assigns the task of parsing integer arguments to the |
| 41 | * invoked program now). Added ability to initialise all |
| 42 | * the MD devices (by specifying multiple "md=" lines) |
| 43 | * instead of just one. -- KTK |
| 44 | * 18May2000: Added support for persistent-superblock arrays: |
| 45 | * md=n,0,factor,fault,device-list uses RAID0 for device n |
| 46 | * md=n,-1,factor,fault,device-list uses LINEAR for device n |
| 47 | * md=n,device-list reads a RAID superblock from the devices |
| 48 | * elements in device-list are read by name_to_kdev_t so can be |
| 49 | * a hex number or something like /dev/hda1 /dev/sdb |
| 50 | * 2001-06-03: Dave Cinege <dcinege@psychosis.com> |
| 51 | * Shifted name_to_kdev_t() and related operations to md_set_drive() |
| 52 | * for later execution. Rewrote section to make devfs compatible. |
| 53 | */ |
| 54 | static int __init md_setup(char *str) |
| 55 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 56 | int minor, level, factor, fault, partitioned = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 57 | char *pername = ""; |
| 58 | char *str1; |
| 59 | int ent; |
| 60 | |
| 61 | if (*str == 'd') { |
| 62 | partitioned = 1; |
| 63 | str++; |
| 64 | } |
| 65 | if (get_option(&str, &minor) != 2) { /* MD Number */ |
| 66 | printk(KERN_WARNING "md: Too few arguments supplied to md=.\n"); |
| 67 | return 0; |
| 68 | } |
| 69 | str1 = str; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | for (ent=0 ; ent< md_setup_ents ; ent++) |
| 71 | if (md_setup_args[ent].minor == minor && |
| 72 | md_setup_args[ent].partitioned == partitioned) { |
| 73 | printk(KERN_WARNING "md: md=%s%d, Specified more than once. " |
| 74 | "Replacing previous definition.\n", partitioned?"d":"", minor); |
| 75 | break; |
| 76 | } |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 77 | if (ent >= ARRAY_SIZE(md_setup_args)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 78 | printk(KERN_WARNING "md: md=%s%d - too many md initialisations\n", partitioned?"d":"", minor); |
| 79 | return 0; |
| 80 | } |
| 81 | if (ent >= md_setup_ents) |
| 82 | md_setup_ents++; |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 83 | switch (get_option(&str, &level)) { /* RAID level */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | case 2: /* could be 0 or -1.. */ |
| 85 | if (level == 0 || level == LEVEL_LINEAR) { |
| 86 | if (get_option(&str, &factor) != 2 || /* Chunk Size */ |
| 87 | get_option(&str, &fault) != 2) { |
| 88 | printk(KERN_WARNING "md: Too few arguments supplied to md=.\n"); |
| 89 | return 0; |
| 90 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 91 | md_setup_args[ent].level = level; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | md_setup_args[ent].chunk = 1 << (factor+12); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 93 | if (level == LEVEL_LINEAR) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | pername = "linear"; |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 95 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | pername = "raid0"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | break; |
| 98 | } |
| 99 | /* FALL THROUGH */ |
| 100 | case 1: /* the first device is numeric */ |
| 101 | str = str1; |
| 102 | /* FALL THROUGH */ |
| 103 | case 0: |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 104 | md_setup_args[ent].level = LEVEL_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | pername="super-block"; |
| 106 | } |
| 107 | |
| 108 | printk(KERN_INFO "md: Will configure md%d (%s) from %s, below.\n", |
| 109 | minor, pername, str); |
| 110 | md_setup_args[ent].device_names = str; |
| 111 | md_setup_args[ent].partitioned = partitioned; |
| 112 | md_setup_args[ent].minor = minor; |
| 113 | |
| 114 | return 1; |
| 115 | } |
| 116 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | static void __init md_setup_drive(void) |
| 118 | { |
| 119 | int minor, i, ent, partitioned; |
| 120 | dev_t dev; |
| 121 | dev_t devices[MD_SB_DISKS+1]; |
| 122 | |
| 123 | for (ent = 0; ent < md_setup_ents ; ent++) { |
| 124 | int fd; |
| 125 | int err = 0; |
| 126 | char *devname; |
| 127 | mdu_disk_info_t dinfo; |
Greg Kroah-Hartman | bdaf852 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 128 | char name[16]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 129 | |
| 130 | minor = md_setup_args[ent].minor; |
| 131 | partitioned = md_setup_args[ent].partitioned; |
| 132 | devname = md_setup_args[ent].device_names; |
| 133 | |
| 134 | sprintf(name, "/dev/md%s%d", partitioned?"_d":"", minor); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 135 | if (partitioned) |
| 136 | dev = MKDEV(mdp_major, minor << MdpMinorShift); |
| 137 | else |
| 138 | dev = MKDEV(MD_MAJOR, minor); |
Greg Kroah-Hartman | bdaf852 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 139 | create_dev(name, dev); |
Harvey Harrison | d613c3e | 2008-04-28 14:13:14 -0700 | [diff] [blame] | 140 | for (i = 0; i < MD_SB_DISKS && devname != NULL; i++) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | char *p; |
| 142 | char comp_name[64]; |
| 143 | u32 rdev; |
| 144 | |
| 145 | p = strchr(devname, ','); |
| 146 | if (p) |
| 147 | *p++ = 0; |
| 148 | |
| 149 | dev = name_to_dev_t(devname); |
| 150 | if (strncmp(devname, "/dev/", 5) == 0) |
| 151 | devname += 5; |
| 152 | snprintf(comp_name, 63, "/dev/%s", devname); |
| 153 | rdev = bstat(comp_name); |
| 154 | if (rdev) |
| 155 | dev = new_decode_dev(rdev); |
| 156 | if (!dev) { |
| 157 | printk(KERN_WARNING "md: Unknown device name: %s\n", devname); |
| 158 | break; |
| 159 | } |
| 160 | |
| 161 | devices[i] = dev; |
| 162 | |
| 163 | devname = p; |
| 164 | } |
| 165 | devices[i] = 0; |
| 166 | |
| 167 | if (!i) |
| 168 | continue; |
| 169 | |
| 170 | printk(KERN_INFO "md: Loading md%s%d: %s\n", |
| 171 | partitioned ? "_d" : "", minor, |
| 172 | md_setup_args[ent].device_names); |
| 173 | |
Dominik Brodowski | bae217e | 2018-03-11 11:34:56 +0100 | [diff] [blame] | 174 | fd = ksys_open(name, 0, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | if (fd < 0) { |
| 176 | printk(KERN_ERR "md: open failed - cannot start " |
| 177 | "array %s\n", name); |
| 178 | continue; |
| 179 | } |
Dominik Brodowski | cbb60b9 | 2018-03-13 21:43:59 +0100 | [diff] [blame] | 180 | if (ksys_ioctl(fd, SET_ARRAY_INFO, 0) == -EBUSY) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 181 | printk(KERN_WARNING |
| 182 | "md: Ignoring md=%d, already autodetected. (Use raid=noautodetect)\n", |
| 183 | minor); |
Dominik Brodowski | 2ca2a09 | 2018-03-11 11:34:55 +0100 | [diff] [blame] | 184 | ksys_close(fd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | continue; |
| 186 | } |
| 187 | |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 188 | if (md_setup_args[ent].level != LEVEL_NONE) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 189 | /* non-persistent */ |
| 190 | mdu_array_info_t ainfo; |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 191 | ainfo.level = md_setup_args[ent].level; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 192 | ainfo.size = 0; |
| 193 | ainfo.nr_disks =0; |
| 194 | ainfo.raid_disks =0; |
| 195 | while (devices[ainfo.raid_disks]) |
| 196 | ainfo.raid_disks++; |
| 197 | ainfo.md_minor =minor; |
| 198 | ainfo.not_persistent = 1; |
| 199 | |
| 200 | ainfo.state = (1 << MD_SB_CLEAN); |
| 201 | ainfo.layout = 0; |
| 202 | ainfo.chunk_size = md_setup_args[ent].chunk; |
Dominik Brodowski | cbb60b9 | 2018-03-13 21:43:59 +0100 | [diff] [blame] | 203 | err = ksys_ioctl(fd, SET_ARRAY_INFO, (long)&ainfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | for (i = 0; !err && i <= MD_SB_DISKS; i++) { |
| 205 | dev = devices[i]; |
| 206 | if (!dev) |
| 207 | break; |
| 208 | dinfo.number = i; |
| 209 | dinfo.raid_disk = i; |
| 210 | dinfo.state = (1<<MD_DISK_ACTIVE)|(1<<MD_DISK_SYNC); |
| 211 | dinfo.major = MAJOR(dev); |
| 212 | dinfo.minor = MINOR(dev); |
Dominik Brodowski | cbb60b9 | 2018-03-13 21:43:59 +0100 | [diff] [blame] | 213 | err = ksys_ioctl(fd, ADD_NEW_DISK, |
| 214 | (long)&dinfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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); |
Dominik Brodowski | cbb60b9 | 2018-03-13 21:43:59 +0100 | [diff] [blame] | 224 | ksys_ioctl(fd, ADD_NEW_DISK, (long)&dinfo); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | } |
| 226 | } |
| 227 | if (!err) |
Dominik Brodowski | cbb60b9 | 2018-03-13 21:43:59 +0100 | [diff] [blame] | 228 | err = ksys_ioctl(fd, RUN_ARRAY, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | */ |
Dominik Brodowski | 2ca2a09 | 2018-03-11 11:34:55 +0100 | [diff] [blame] | 237 | ksys_close(fd); |
Dominik Brodowski | bae217e | 2018-03-11 11:34:56 +0100 | [diff] [blame] | 238 | fd = ksys_open(name, 0, 0); |
Dominik Brodowski | cbb60b9 | 2018-03-13 21:43:59 +0100 | [diff] [blame] | 239 | ksys_ioctl(fd, BLKRRPART, 0); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | } |
Dominik Brodowski | 2ca2a09 | 2018-03-11 11:34:55 +0100 | [diff] [blame] | 241 | ksys_close(fd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 242 | } |
| 243 | } |
| 244 | |
| 245 | static 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 Ven | a364092 | 2008-09-21 15:44:32 -0700 | [diff] [blame] | 261 | if (!strncmp(str, "autodetect", wlen)) |
| 262 | raid_noautodetect = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 263 | 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 | |
Mike Frysinger | ff083c8 | 2009-01-06 14:40:53 -0800 | [diff] [blame] | 275 | static void __init autodetect_raid(void) |
Ingo Molnar | 82cbc11 | 2008-08-18 12:54:00 +0200 | [diff] [blame] | 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"); |
Arjan van de Ven | 216773a | 2009-02-14 01:59:06 +0100 | [diff] [blame] | 285 | |
| 286 | wait_for_device_probe(); |
| 287 | |
Dominik Brodowski | bae217e | 2018-03-11 11:34:56 +0100 | [diff] [blame] | 288 | fd = ksys_open("/dev/md0", 0, 0); |
Ingo Molnar | 82cbc11 | 2008-08-18 12:54:00 +0200 | [diff] [blame] | 289 | if (fd >= 0) { |
Dominik Brodowski | cbb60b9 | 2018-03-13 21:43:59 +0100 | [diff] [blame] | 290 | ksys_ioctl(fd, RAID_AUTORUN, raid_autopart); |
Dominik Brodowski | 2ca2a09 | 2018-03-11 11:34:55 +0100 | [diff] [blame] | 291 | ksys_close(fd); |
Ingo Molnar | 82cbc11 | 2008-08-18 12:54:00 +0200 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 295 | void __init md_run_setup(void) |
| 296 | { |
Greg Kroah-Hartman | bdaf852 | 2005-06-20 21:15:16 -0700 | [diff] [blame] | 297 | create_dev("/dev/md0", MKDEV(MD_MAJOR, 0)); |
Arjan van de Ven | 589f800 | 2008-07-20 13:07:09 -0700 | [diff] [blame] | 298 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 299 | if (raid_noautodetect) |
Arjan van de Ven | a364092 | 2008-09-21 15:44:32 -0700 | [diff] [blame] | 300 | printk(KERN_INFO "md: Skipping autodetection of RAID arrays. (raid=autodetect will force)\n"); |
Ingo Molnar | 82cbc11 | 2008-08-18 12:54:00 +0200 | [diff] [blame] | 301 | else |
| 302 | autodetect_raid(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 303 | md_setup_drive(); |
| 304 | } |