md-autodetect.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/kernel.h>
  3. #include <linux/blkdev.h>
  4. #include <linux/init.h>
  5. #include <linux/mount.h>
  6. #include <linux/major.h>
  7. #include <linux/delay.h>
  8. #include <linux/init_syscalls.h>
  9. #include <linux/raid/detect.h>
  10. #include <linux/raid/md_u.h>
  11. #include <linux/raid/md_p.h>
  12. #include "md.h"
  13. /*
  14. * When md (and any require personalities) are compiled into the kernel
  15. * (not a module), arrays can be assembles are boot time using with AUTODETECT
  16. * where specially marked partitions are registered with md_autodetect_dev(),
  17. * and with MD_BOOT where devices to be collected are given on the boot line
  18. * with md=.....
  19. * The code for that is here.
  20. */
  21. #ifdef CONFIG_MD_AUTODETECT
  22. static int __initdata raid_noautodetect;
  23. #else
  24. static int __initdata raid_noautodetect=1;
  25. #endif
  26. static int __initdata raid_autopart;
  27. static struct md_setup_args {
  28. int minor;
  29. int partitioned;
  30. int level;
  31. int chunk;
  32. char *device_names;
  33. } md_setup_args[256] __initdata;
  34. static int md_setup_ents __initdata;
  35. /*
  36. * Parse the command-line parameters given our kernel, but do not
  37. * actually try to invoke the MD device now; that is handled by
  38. * md_setup_drive after the low-level disk drivers have initialised.
  39. *
  40. * 27/11/1999: Fixed to work correctly with the 2.3 kernel (which
  41. * assigns the task of parsing integer arguments to the
  42. * invoked program now). Added ability to initialise all
  43. * the MD devices (by specifying multiple "md=" lines)
  44. * instead of just one. -- KTK
  45. * 18May2000: Added support for persistent-superblock arrays:
  46. * md=n,0,factor,fault,device-list uses RAID0 for device n
  47. * md=n,-1,factor,fault,device-list uses LINEAR for device n
  48. * md=n,device-list reads a RAID superblock from the devices
  49. * elements in device-list are read by name_to_kdev_t so can be
  50. * a hex number or something like /dev/hda1 /dev/sdb
  51. * 2001-06-03: Dave Cinege <[email protected]>
  52. * Shifted name_to_kdev_t() and related operations to md_set_drive()
  53. * for later execution. Rewrote section to make devfs compatible.
  54. */
  55. static int __init md_setup(char *str)
  56. {
  57. int minor, level, factor, fault, partitioned = 0;
  58. char *pername = "";
  59. char *str1;
  60. int ent;
  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;
  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. }
  77. if (ent >= ARRAY_SIZE(md_setup_args)) {
  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++;
  83. switch (get_option(&str, &level)) { /* RAID level */
  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. }
  91. md_setup_args[ent].level = level;
  92. md_setup_args[ent].chunk = 1 << (factor+12);
  93. if (level == LEVEL_LINEAR)
  94. pername = "linear";
  95. else
  96. pername = "raid0";
  97. break;
  98. }
  99. fallthrough;
  100. case 1: /* the first device is numeric */
  101. str = str1;
  102. fallthrough;
  103. case 0:
  104. md_setup_args[ent].level = LEVEL_NONE;
  105. pername="super-block";
  106. }
  107. printk(KERN_INFO "md: Will configure md%d (%s) from %s, below.\n",
  108. minor, pername, str);
  109. md_setup_args[ent].device_names = str;
  110. md_setup_args[ent].partitioned = partitioned;
  111. md_setup_args[ent].minor = minor;
  112. return 1;
  113. }
  114. static void __init md_setup_drive(struct md_setup_args *args)
  115. {
  116. char *devname = args->device_names;
  117. dev_t devices[MD_SB_DISKS + 1], mdev;
  118. struct mdu_array_info_s ainfo = { };
  119. struct mddev *mddev;
  120. int err = 0, i;
  121. char name[16];
  122. if (args->partitioned) {
  123. mdev = MKDEV(mdp_major, args->minor << MdpMinorShift);
  124. sprintf(name, "md_d%d", args->minor);
  125. } else {
  126. mdev = MKDEV(MD_MAJOR, args->minor);
  127. sprintf(name, "md%d", args->minor);
  128. }
  129. for (i = 0; i < MD_SB_DISKS && devname != NULL; i++) {
  130. struct kstat stat;
  131. char *p;
  132. char comp_name[64];
  133. dev_t dev;
  134. p = strchr(devname, ',');
  135. if (p)
  136. *p++ = 0;
  137. dev = name_to_dev_t(devname);
  138. if (strncmp(devname, "/dev/", 5) == 0)
  139. devname += 5;
  140. snprintf(comp_name, 63, "/dev/%s", devname);
  141. if (init_stat(comp_name, &stat, 0) == 0 && S_ISBLK(stat.mode))
  142. dev = new_decode_dev(stat.rdev);
  143. if (!dev) {
  144. pr_warn("md: Unknown device name: %s\n", devname);
  145. break;
  146. }
  147. devices[i] = dev;
  148. devname = p;
  149. }
  150. devices[i] = 0;
  151. if (!i)
  152. return;
  153. pr_info("md: Loading %s: %s\n", name, args->device_names);
  154. mddev = md_alloc(mdev, name);
  155. if (IS_ERR(mddev)) {
  156. pr_err("md: md_alloc failed - cannot start array %s\n", name);
  157. return;
  158. }
  159. err = mddev_lock(mddev);
  160. if (err) {
  161. pr_err("md: failed to lock array %s\n", name);
  162. goto out_mddev_put;
  163. }
  164. if (!list_empty(&mddev->disks) || mddev->raid_disks) {
  165. pr_warn("md: Ignoring %s, already autodetected. (Use raid=noautodetect)\n",
  166. name);
  167. goto out_unlock;
  168. }
  169. if (args->level != LEVEL_NONE) {
  170. /* non-persistent */
  171. ainfo.level = args->level;
  172. ainfo.md_minor = args->minor;
  173. ainfo.not_persistent = 1;
  174. ainfo.state = (1 << MD_SB_CLEAN);
  175. ainfo.chunk_size = args->chunk;
  176. while (devices[ainfo.raid_disks])
  177. ainfo.raid_disks++;
  178. }
  179. err = md_set_array_info(mddev, &ainfo);
  180. for (i = 0; i <= MD_SB_DISKS && devices[i]; i++) {
  181. struct mdu_disk_info_s dinfo = {
  182. .major = MAJOR(devices[i]),
  183. .minor = MINOR(devices[i]),
  184. };
  185. if (args->level != LEVEL_NONE) {
  186. dinfo.number = i;
  187. dinfo.raid_disk = i;
  188. dinfo.state =
  189. (1 << MD_DISK_ACTIVE) | (1 << MD_DISK_SYNC);
  190. }
  191. md_add_new_disk(mddev, &dinfo);
  192. }
  193. if (!err)
  194. err = do_md_run(mddev);
  195. if (err)
  196. pr_warn("md: starting %s failed\n", name);
  197. out_unlock:
  198. mddev_unlock(mddev);
  199. out_mddev_put:
  200. mddev_put(mddev);
  201. }
  202. static int __init raid_setup(char *str)
  203. {
  204. int len, pos;
  205. len = strlen(str) + 1;
  206. pos = 0;
  207. while (pos < len) {
  208. char *comma = strchr(str+pos, ',');
  209. int wlen;
  210. if (comma)
  211. wlen = (comma-str)-pos;
  212. else wlen = (len-1)-pos;
  213. if (!strncmp(str, "noautodetect", wlen))
  214. raid_noautodetect = 1;
  215. if (!strncmp(str, "autodetect", wlen))
  216. raid_noautodetect = 0;
  217. if (strncmp(str, "partitionable", wlen)==0)
  218. raid_autopart = 1;
  219. if (strncmp(str, "part", wlen)==0)
  220. raid_autopart = 1;
  221. pos += wlen+1;
  222. }
  223. return 1;
  224. }
  225. __setup("raid=", raid_setup);
  226. __setup("md=", md_setup);
  227. static void __init autodetect_raid(void)
  228. {
  229. /*
  230. * Since we don't want to detect and use half a raid array, we need to
  231. * wait for the known devices to complete their probing
  232. */
  233. printk(KERN_INFO "md: Waiting for all devices to be available before autodetect\n");
  234. printk(KERN_INFO "md: If you don't use raid, use raid=noautodetect\n");
  235. wait_for_device_probe();
  236. md_autostart_arrays(raid_autopart);
  237. }
  238. void __init md_run_setup(void)
  239. {
  240. int ent;
  241. if (raid_noautodetect)
  242. printk(KERN_INFO "md: Skipping autodetection of RAID arrays. (raid=autodetect will force)\n");
  243. else
  244. autodetect_raid();
  245. for (ent = 0; ent < md_setup_ents; ent++)
  246. md_setup_drive(&md_setup_args[ent]);
  247. }