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