do_mounts_md.c 8.2 KB

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