do_mounts_md.c 7.9 KB

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