Greg Kroah-Hartman | b244131 | 2017-11-01 15:07:57 +0100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Christoph Hellwig | 4f5b246 | 2020-06-07 16:18:59 +0200 | [diff] [blame] | 2 | #include <linux/kernel.h> |
| 3 | #include <linux/blkdev.h> |
| 4 | #include <linux/init.h> |
Christoph Hellwig | 4f5b246 | 2020-06-07 16:18:59 +0200 | [diff] [blame] | 5 | #include <linux/mount.h> |
| 6 | #include <linux/major.h> |
Alexey Dobriyan | f8b77d3 | 2008-10-29 14:01:05 -0700 | [diff] [blame] | 7 | #include <linux/delay.h> |
Christoph Hellwig | 716308a | 2020-07-22 11:15:40 +0200 | [diff] [blame] | 8 | #include <linux/init_syscalls.h> |
Christoph Hellwig | 4f5b246 | 2020-06-07 16:18:59 +0200 | [diff] [blame] | 9 | #include <linux/raid/detect.h> |
NeilBrown | bff6197 | 2009-03-31 14:33:13 +1100 | [diff] [blame] | 10 | #include <linux/raid/md_u.h> |
| 11 | #include <linux/raid/md_p.h> |
Christoph Hellwig | d82fa81 | 2020-06-06 15:00:24 +0200 | [diff] [blame] | 12 | #include "md.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | /* |
| 15 | * When md (and any require personalities) are compiled into the kernel |
| 16 | * (not a module), arrays can be assembles are boot time using with AUTODETECT |
| 17 | * where specially marked partitions are registered with md_autodetect_dev(), |
| 18 | * and with MD_BOOT where devices to be collected are given on the boot line |
| 19 | * with md=..... |
| 20 | * The code for that is here. |
| 21 | */ |
| 22 | |
Arjan van de Ven | a364092 | 2008-09-21 15:44:32 -0700 | [diff] [blame] | 23 | #ifdef CONFIG_MD_AUTODETECT |
| 24 | static int __initdata raid_noautodetect; |
| 25 | #else |
| 26 | static int __initdata raid_noautodetect=1; |
| 27 | #endif |
| 28 | static int __initdata raid_autopart; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | |
Christoph Hellwig | d110048 | 2020-06-07 16:56:05 +0200 | [diff] [blame] | 30 | static struct md_setup_args { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | int minor; |
| 32 | int partitioned; |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 33 | int level; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | int chunk; |
| 35 | char *device_names; |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 36 | } md_setup_args[256] __initdata; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | |
| 38 | static int md_setup_ents __initdata; |
| 39 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | /* |
| 41 | * Parse the command-line parameters given our kernel, but do not |
| 42 | * actually try to invoke the MD device now; that is handled by |
| 43 | * md_setup_drive after the low-level disk drivers have initialised. |
| 44 | * |
| 45 | * 27/11/1999: Fixed to work correctly with the 2.3 kernel (which |
| 46 | * assigns the task of parsing integer arguments to the |
| 47 | * invoked program now). Added ability to initialise all |
| 48 | * the MD devices (by specifying multiple "md=" lines) |
| 49 | * instead of just one. -- KTK |
| 50 | * 18May2000: Added support for persistent-superblock arrays: |
| 51 | * md=n,0,factor,fault,device-list uses RAID0 for device n |
| 52 | * md=n,-1,factor,fault,device-list uses LINEAR for device n |
| 53 | * md=n,device-list reads a RAID superblock from the devices |
| 54 | * elements in device-list are read by name_to_kdev_t so can be |
| 55 | * a hex number or something like /dev/hda1 /dev/sdb |
| 56 | * 2001-06-03: Dave Cinege <dcinege@psychosis.com> |
| 57 | * Shifted name_to_kdev_t() and related operations to md_set_drive() |
| 58 | * for later execution. Rewrote section to make devfs compatible. |
| 59 | */ |
| 60 | static int __init md_setup(char *str) |
| 61 | { |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 62 | int minor, level, factor, fault, partitioned = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | char *pername = ""; |
| 64 | char *str1; |
| 65 | int ent; |
| 66 | |
| 67 | if (*str == 'd') { |
| 68 | partitioned = 1; |
| 69 | str++; |
| 70 | } |
| 71 | if (get_option(&str, &minor) != 2) { /* MD Number */ |
| 72 | printk(KERN_WARNING "md: Too few arguments supplied to md=.\n"); |
| 73 | return 0; |
| 74 | } |
| 75 | str1 = str; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 76 | for (ent=0 ; ent< md_setup_ents ; ent++) |
| 77 | if (md_setup_args[ent].minor == minor && |
| 78 | md_setup_args[ent].partitioned == partitioned) { |
| 79 | printk(KERN_WARNING "md: md=%s%d, Specified more than once. " |
| 80 | "Replacing previous definition.\n", partitioned?"d":"", minor); |
| 81 | break; |
| 82 | } |
NeilBrown | e8703fe | 2006-10-03 01:15:59 -0700 | [diff] [blame] | 83 | if (ent >= ARRAY_SIZE(md_setup_args)) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 84 | printk(KERN_WARNING "md: md=%s%d - too many md initialisations\n", partitioned?"d":"", minor); |
| 85 | return 0; |
| 86 | } |
| 87 | if (ent >= md_setup_ents) |
| 88 | md_setup_ents++; |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 89 | switch (get_option(&str, &level)) { /* RAID level */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | case 2: /* could be 0 or -1.. */ |
| 91 | if (level == 0 || level == LEVEL_LINEAR) { |
| 92 | if (get_option(&str, &factor) != 2 || /* Chunk Size */ |
| 93 | get_option(&str, &fault) != 2) { |
| 94 | printk(KERN_WARNING "md: Too few arguments supplied to md=.\n"); |
| 95 | return 0; |
| 96 | } |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 97 | md_setup_args[ent].level = level; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 98 | md_setup_args[ent].chunk = 1 << (factor+12); |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 99 | if (level == LEVEL_LINEAR) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | pername = "linear"; |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 101 | else |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | pername = "raid0"; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | break; |
| 104 | } |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 105 | fallthrough; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 106 | case 1: /* the first device is numeric */ |
| 107 | str = str1; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 108 | fallthrough; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | case 0: |
NeilBrown | 2604b70 | 2006-01-06 00:20:36 -0800 | [diff] [blame] | 110 | md_setup_args[ent].level = LEVEL_NONE; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 111 | pername="super-block"; |
| 112 | } |
| 113 | |
| 114 | printk(KERN_INFO "md: Will configure md%d (%s) from %s, below.\n", |
| 115 | minor, pername, str); |
| 116 | md_setup_args[ent].device_names = str; |
| 117 | md_setup_args[ent].partitioned = partitioned; |
| 118 | md_setup_args[ent].minor = minor; |
| 119 | |
| 120 | return 1; |
| 121 | } |
| 122 | |
Christoph Hellwig | d110048 | 2020-06-07 16:56:05 +0200 | [diff] [blame] | 123 | static void __init md_setup_drive(struct md_setup_args *args) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | { |
Christoph Hellwig | 7e0adbf | 2020-06-07 17:31:19 +0200 | [diff] [blame] | 125 | char *devname = args->device_names; |
| 126 | dev_t devices[MD_SB_DISKS + 1], mdev; |
| 127 | struct mdu_array_info_s ainfo = { }; |
| 128 | struct block_device *bdev; |
| 129 | struct mddev *mddev; |
| 130 | int err = 0, i; |
Christoph Hellwig | d110048 | 2020-06-07 16:56:05 +0200 | [diff] [blame] | 131 | char name[16]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 132 | |
Christoph Hellwig | 7e0adbf | 2020-06-07 17:31:19 +0200 | [diff] [blame] | 133 | if (args->partitioned) { |
| 134 | mdev = MKDEV(mdp_major, args->minor << MdpMinorShift); |
| 135 | sprintf(name, "md_d%d", args->minor); |
| 136 | } else { |
| 137 | mdev = MKDEV(MD_MAJOR, args->minor); |
| 138 | sprintf(name, "md%d", args->minor); |
| 139 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | |
Christoph Hellwig | d110048 | 2020-06-07 16:56:05 +0200 | [diff] [blame] | 141 | for (i = 0; i < MD_SB_DISKS && devname != NULL; i++) { |
| 142 | struct kstat stat; |
| 143 | char *p; |
| 144 | char comp_name[64]; |
Christoph Hellwig | 7e0adbf | 2020-06-07 17:31:19 +0200 | [diff] [blame] | 145 | dev_t dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 146 | |
Christoph Hellwig | d110048 | 2020-06-07 16:56:05 +0200 | [diff] [blame] | 147 | p = strchr(devname, ','); |
| 148 | if (p) |
| 149 | *p++ = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | |
Christoph Hellwig | d110048 | 2020-06-07 16:56:05 +0200 | [diff] [blame] | 151 | dev = name_to_dev_t(devname); |
| 152 | if (strncmp(devname, "/dev/", 5) == 0) |
| 153 | devname += 5; |
| 154 | snprintf(comp_name, 63, "/dev/%s", devname); |
Christoph Hellwig | 716308a | 2020-07-22 11:15:40 +0200 | [diff] [blame] | 155 | if (init_stat(comp_name, &stat, 0) == 0 && S_ISBLK(stat.mode)) |
Christoph Hellwig | d110048 | 2020-06-07 16:56:05 +0200 | [diff] [blame] | 156 | dev = new_decode_dev(stat.rdev); |
| 157 | if (!dev) { |
Christoph Hellwig | 7e0adbf | 2020-06-07 17:31:19 +0200 | [diff] [blame] | 158 | pr_warn("md: Unknown device name: %s\n", devname); |
Christoph Hellwig | d110048 | 2020-06-07 16:56:05 +0200 | [diff] [blame] | 159 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Christoph Hellwig | d110048 | 2020-06-07 16:56:05 +0200 | [diff] [blame] | 162 | devices[i] = dev; |
| 163 | devname = p; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | } |
Christoph Hellwig | d110048 | 2020-06-07 16:56:05 +0200 | [diff] [blame] | 165 | devices[i] = 0; |
| 166 | |
| 167 | if (!i) |
| 168 | return; |
| 169 | |
Christoph Hellwig | 7e0adbf | 2020-06-07 17:31:19 +0200 | [diff] [blame] | 170 | pr_info("md: Loading %s: %s\n", name, args->device_names); |
Christoph Hellwig | d110048 | 2020-06-07 16:56:05 +0200 | [diff] [blame] | 171 | |
Christoph Hellwig | 7e0adbf | 2020-06-07 17:31:19 +0200 | [diff] [blame] | 172 | bdev = blkdev_get_by_dev(mdev, FMODE_READ, NULL); |
| 173 | if (IS_ERR(bdev)) { |
| 174 | pr_err("md: open failed - cannot start array %s\n", name); |
Christoph Hellwig | d110048 | 2020-06-07 16:56:05 +0200 | [diff] [blame] | 175 | return; |
| 176 | } |
Christoph Hellwig | 7e0adbf | 2020-06-07 17:31:19 +0200 | [diff] [blame] | 177 | |
| 178 | err = -EIO; |
| 179 | if (WARN(bdev->bd_disk->fops != &md_fops, |
| 180 | "Opening block device %x resulted in non-md device\n", |
| 181 | mdev)) |
| 182 | goto out_blkdev_put; |
| 183 | |
| 184 | mddev = bdev->bd_disk->private_data; |
| 185 | |
| 186 | err = mddev_lock(mddev); |
| 187 | if (err) { |
| 188 | pr_err("md: failed to lock array %s\n", name); |
| 189 | goto out_blkdev_put; |
| 190 | } |
| 191 | |
| 192 | if (!list_empty(&mddev->disks) || mddev->raid_disks) { |
| 193 | pr_warn("md: Ignoring %s, already autodetected. (Use raid=noautodetect)\n", |
| 194 | name); |
| 195 | goto out_unlock; |
Christoph Hellwig | d110048 | 2020-06-07 16:56:05 +0200 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | if (args->level != LEVEL_NONE) { |
| 199 | /* non-persistent */ |
Christoph Hellwig | d110048 | 2020-06-07 16:56:05 +0200 | [diff] [blame] | 200 | ainfo.level = args->level; |
Christoph Hellwig | 7e0adbf | 2020-06-07 17:31:19 +0200 | [diff] [blame] | 201 | ainfo.md_minor = args->minor; |
| 202 | ainfo.not_persistent = 1; |
| 203 | ainfo.state = (1 << MD_SB_CLEAN); |
| 204 | ainfo.chunk_size = args->chunk; |
Christoph Hellwig | d110048 | 2020-06-07 16:56:05 +0200 | [diff] [blame] | 205 | while (devices[ainfo.raid_disks]) |
| 206 | ainfo.raid_disks++; |
Christoph Hellwig | 7e0adbf | 2020-06-07 17:31:19 +0200 | [diff] [blame] | 207 | } |
Christoph Hellwig | d110048 | 2020-06-07 16:56:05 +0200 | [diff] [blame] | 208 | |
Christoph Hellwig | 7e0adbf | 2020-06-07 17:31:19 +0200 | [diff] [blame] | 209 | err = md_set_array_info(mddev, &ainfo); |
| 210 | |
| 211 | for (i = 0; i <= MD_SB_DISKS && devices[i]; i++) { |
| 212 | struct mdu_disk_info_s dinfo = { |
| 213 | .major = MAJOR(devices[i]), |
| 214 | .minor = MINOR(devices[i]), |
| 215 | }; |
| 216 | |
| 217 | if (args->level != LEVEL_NONE) { |
Christoph Hellwig | d110048 | 2020-06-07 16:56:05 +0200 | [diff] [blame] | 218 | dinfo.number = i; |
| 219 | dinfo.raid_disk = i; |
Christoph Hellwig | 7e0adbf | 2020-06-07 17:31:19 +0200 | [diff] [blame] | 220 | dinfo.state = |
| 221 | (1 << MD_DISK_ACTIVE) | (1 << MD_DISK_SYNC); |
Christoph Hellwig | d110048 | 2020-06-07 16:56:05 +0200 | [diff] [blame] | 222 | } |
Christoph Hellwig | 7e0adbf | 2020-06-07 17:31:19 +0200 | [diff] [blame] | 223 | |
| 224 | md_add_new_disk(mddev, &dinfo); |
Christoph Hellwig | d110048 | 2020-06-07 16:56:05 +0200 | [diff] [blame] | 225 | } |
Christoph Hellwig | 7e0adbf | 2020-06-07 17:31:19 +0200 | [diff] [blame] | 226 | |
Christoph Hellwig | d110048 | 2020-06-07 16:56:05 +0200 | [diff] [blame] | 227 | if (!err) |
Christoph Hellwig | 7e0adbf | 2020-06-07 17:31:19 +0200 | [diff] [blame] | 228 | err = do_md_run(mddev); |
Christoph Hellwig | d110048 | 2020-06-07 16:56:05 +0200 | [diff] [blame] | 229 | if (err) |
Christoph Hellwig | 7e0adbf | 2020-06-07 17:31:19 +0200 | [diff] [blame] | 230 | pr_warn("md: starting %s failed\n", name); |
| 231 | out_unlock: |
| 232 | mddev_unlock(mddev); |
| 233 | out_blkdev_put: |
| 234 | blkdev_put(bdev, FMODE_READ); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | static int __init raid_setup(char *str) |
| 238 | { |
| 239 | int len, pos; |
| 240 | |
| 241 | len = strlen(str) + 1; |
| 242 | pos = 0; |
| 243 | |
| 244 | while (pos < len) { |
| 245 | char *comma = strchr(str+pos, ','); |
| 246 | int wlen; |
| 247 | if (comma) |
| 248 | wlen = (comma-str)-pos; |
| 249 | else wlen = (len-1)-pos; |
| 250 | |
| 251 | if (!strncmp(str, "noautodetect", wlen)) |
| 252 | raid_noautodetect = 1; |
Arjan van de Ven | a364092 | 2008-09-21 15:44:32 -0700 | [diff] [blame] | 253 | if (!strncmp(str, "autodetect", wlen)) |
| 254 | raid_noautodetect = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 | |
Mike Frysinger | ff083c8 | 2009-01-06 14:40:53 -0800 | [diff] [blame] | 267 | static void __init autodetect_raid(void) |
Ingo Molnar | 82cbc11 | 2008-08-18 12:54:00 +0200 | [diff] [blame] | 268 | { |
Ingo Molnar | 82cbc11 | 2008-08-18 12:54:00 +0200 | [diff] [blame] | 269 | /* |
| 270 | * Since we don't want to detect and use half a raid array, we need to |
| 271 | * wait for the known devices to complete their probing |
| 272 | */ |
| 273 | printk(KERN_INFO "md: Waiting for all devices to be available before autodetect\n"); |
| 274 | 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] | 275 | |
| 276 | wait_for_device_probe(); |
Christoph Hellwig | d82fa81 | 2020-06-06 15:00:24 +0200 | [diff] [blame] | 277 | md_autostart_arrays(raid_autopart); |
Ingo Molnar | 82cbc11 | 2008-08-18 12:54:00 +0200 | [diff] [blame] | 278 | } |
| 279 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | void __init md_run_setup(void) |
| 281 | { |
Christoph Hellwig | d110048 | 2020-06-07 16:56:05 +0200 | [diff] [blame] | 282 | int ent; |
| 283 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 284 | if (raid_noautodetect) |
Arjan van de Ven | a364092 | 2008-09-21 15:44:32 -0700 | [diff] [blame] | 285 | 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] | 286 | else |
| 287 | autodetect_raid(); |
Christoph Hellwig | d110048 | 2020-06-07 16:56:05 +0200 | [diff] [blame] | 288 | |
| 289 | for (ent = 0; ent < md_setup_ents; ent++) |
| 290 | md_setup_drive(&md_setup_args[ent]); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 291 | } |