do_mounts.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. #include <linux/module.h>
  2. #include <linux/sched.h>
  3. #include <linux/ctype.h>
  4. #include <linux/fd.h>
  5. #include <linux/tty.h>
  6. #include <linux/suspend.h>
  7. #include <linux/root_dev.h>
  8. #include <linux/security.h>
  9. #include <linux/delay.h>
  10. #include <linux/genhd.h>
  11. #include <linux/mount.h>
  12. #include <linux/device.h>
  13. #include <linux/init.h>
  14. #include <linux/fs.h>
  15. #include <linux/initrd.h>
  16. #include <linux/async.h>
  17. #include <linux/fs_struct.h>
  18. #include <linux/slab.h>
  19. #include <linux/nfs_fs.h>
  20. #include <linux/nfs_fs_sb.h>
  21. #include <linux/nfs_mount.h>
  22. #include "do_mounts.h"
  23. int __initdata rd_doload; /* 1 = load RAM disk, 0 = don't load */
  24. int root_mountflags = MS_RDONLY | MS_SILENT;
  25. static char * __initdata root_device_name;
  26. static char __initdata saved_root_name[64];
  27. static int root_wait;
  28. dev_t ROOT_DEV;
  29. static int __init load_ramdisk(char *str)
  30. {
  31. rd_doload = simple_strtol(str,NULL,0) & 3;
  32. return 1;
  33. }
  34. __setup("load_ramdisk=", load_ramdisk);
  35. static int __init readonly(char *str)
  36. {
  37. if (*str)
  38. return 0;
  39. root_mountflags |= MS_RDONLY;
  40. return 1;
  41. }
  42. static int __init readwrite(char *str)
  43. {
  44. if (*str)
  45. return 0;
  46. root_mountflags &= ~MS_RDONLY;
  47. return 1;
  48. }
  49. __setup("ro", readonly);
  50. __setup("rw", readwrite);
  51. #ifdef CONFIG_BLOCK
  52. /**
  53. * match_dev_by_uuid - callback for finding a partition using its uuid
  54. * @dev: device passed in by the caller
  55. * @data: opaque pointer to a 36 byte char array with a UUID
  56. *
  57. * Returns 1 if the device matches, and 0 otherwise.
  58. */
  59. static int match_dev_by_uuid(struct device *dev, void *data)
  60. {
  61. u8 *uuid = data;
  62. struct hd_struct *part = dev_to_part(dev);
  63. if (!part->info)
  64. goto no_match;
  65. if (memcmp(uuid, part->info->uuid, sizeof(part->info->uuid)))
  66. goto no_match;
  67. return 1;
  68. no_match:
  69. return 0;
  70. }
  71. /**
  72. * devt_from_partuuid - looks up the dev_t of a partition by its UUID
  73. * @uuid: min 36 byte char array containing a hex ascii UUID
  74. *
  75. * The function will return the first partition which contains a matching
  76. * UUID value in its partition_meta_info struct. This does not search
  77. * by filesystem UUIDs.
  78. *
  79. * If @uuid is followed by a "/PARTNROFF=%d", then the number will be
  80. * extracted and used as an offset from the partition identified by the UUID.
  81. *
  82. * Returns the matching dev_t on success or 0 on failure.
  83. */
  84. static dev_t devt_from_partuuid(char *uuid_str)
  85. {
  86. dev_t res = 0;
  87. struct device *dev = NULL;
  88. u8 uuid[16];
  89. struct gendisk *disk;
  90. struct hd_struct *part;
  91. int offset = 0;
  92. if (strlen(uuid_str) < 36)
  93. goto done;
  94. /* Check for optional partition number offset attributes. */
  95. if (uuid_str[36]) {
  96. char c = 0;
  97. /* Explicitly fail on poor PARTUUID syntax. */
  98. if (sscanf(&uuid_str[36],
  99. "/PARTNROFF=%d%c", &offset, &c) != 1) {
  100. printk(KERN_ERR "VFS: PARTUUID= is invalid.\n"
  101. "Expected PARTUUID=<valid-uuid-id>[/PARTNROFF=%%d]\n");
  102. if (root_wait)
  103. printk(KERN_ERR
  104. "Disabling rootwait; root= is invalid.\n");
  105. root_wait = 0;
  106. goto done;
  107. }
  108. }
  109. /* Pack the requested UUID in the expected format. */
  110. part_pack_uuid(uuid_str, uuid);
  111. dev = class_find_device(&block_class, NULL, uuid, &match_dev_by_uuid);
  112. if (!dev)
  113. goto done;
  114. res = dev->devt;
  115. /* Attempt to find the partition by offset. */
  116. if (!offset)
  117. goto no_offset;
  118. res = 0;
  119. disk = part_to_disk(dev_to_part(dev));
  120. part = disk_get_part(disk, dev_to_part(dev)->partno + offset);
  121. if (part) {
  122. res = part_devt(part);
  123. put_device(part_to_dev(part));
  124. }
  125. no_offset:
  126. put_device(dev);
  127. done:
  128. return res;
  129. }
  130. #endif
  131. /*
  132. * Convert a name into device number. We accept the following variants:
  133. *
  134. * 1) device number in hexadecimal represents itself
  135. * 2) /dev/nfs represents Root_NFS (0xff)
  136. * 3) /dev/<disk_name> represents the device number of disk
  137. * 4) /dev/<disk_name><decimal> represents the device number
  138. * of partition - device number of disk plus the partition number
  139. * 5) /dev/<disk_name>p<decimal> - same as the above, that form is
  140. * used when disk name of partitioned disk ends on a digit.
  141. * 6) PARTUUID=00112233-4455-6677-8899-AABBCCDDEEFF representing the
  142. * unique id of a partition if the partition table provides it.
  143. * 7) PARTUUID=<UUID>/PARTNROFF=<int> to select a partition in relation to
  144. * a partition with a known unique id.
  145. *
  146. * If name doesn't have fall into the categories above, we return (0,0).
  147. * block_class is used to check if something is a disk name. If the disk
  148. * name contains slashes, the device name has them replaced with
  149. * bangs.
  150. */
  151. dev_t name_to_dev_t(char *name)
  152. {
  153. char s[32];
  154. char *p;
  155. dev_t res = 0;
  156. int part;
  157. #ifdef CONFIG_BLOCK
  158. if (strncmp(name, "PARTUUID=", 9) == 0) {
  159. name += 9;
  160. res = devt_from_partuuid(name);
  161. if (!res)
  162. goto fail;
  163. goto done;
  164. }
  165. #endif
  166. if (strncmp(name, "/dev/", 5) != 0) {
  167. unsigned maj, min;
  168. if (sscanf(name, "%u:%u", &maj, &min) == 2) {
  169. res = MKDEV(maj, min);
  170. if (maj != MAJOR(res) || min != MINOR(res))
  171. goto fail;
  172. } else {
  173. res = new_decode_dev(simple_strtoul(name, &p, 16));
  174. if (*p)
  175. goto fail;
  176. }
  177. goto done;
  178. }
  179. name += 5;
  180. res = Root_NFS;
  181. if (strcmp(name, "nfs") == 0)
  182. goto done;
  183. res = Root_RAM0;
  184. if (strcmp(name, "ram") == 0)
  185. goto done;
  186. if (strlen(name) > 31)
  187. goto fail;
  188. strcpy(s, name);
  189. for (p = s; *p; p++)
  190. if (*p == '/')
  191. *p = '!';
  192. res = blk_lookup_devt(s, 0);
  193. if (res)
  194. goto done;
  195. /*
  196. * try non-existent, but valid partition, which may only exist
  197. * after revalidating the disk, like partitioned md devices
  198. */
  199. while (p > s && isdigit(p[-1]))
  200. p--;
  201. if (p == s || !*p || *p == '0')
  202. goto fail;
  203. /* try disk name without <part number> */
  204. part = simple_strtoul(p, NULL, 10);
  205. *p = '\0';
  206. res = blk_lookup_devt(s, part);
  207. if (res)
  208. goto done;
  209. /* try disk name without p<part number> */
  210. if (p < s + 2 || !isdigit(p[-2]) || p[-1] != 'p')
  211. goto fail;
  212. p[-1] = '\0';
  213. res = blk_lookup_devt(s, part);
  214. if (res)
  215. goto done;
  216. fail:
  217. return 0;
  218. done:
  219. return res;
  220. }
  221. static int __init root_dev_setup(char *line)
  222. {
  223. strlcpy(saved_root_name, line, sizeof(saved_root_name));
  224. return 1;
  225. }
  226. __setup("root=", root_dev_setup);
  227. static int __init rootwait_setup(char *str)
  228. {
  229. if (*str)
  230. return 0;
  231. root_wait = 1;
  232. return 1;
  233. }
  234. __setup("rootwait", rootwait_setup);
  235. static char * __initdata root_mount_data;
  236. static int __init root_data_setup(char *str)
  237. {
  238. root_mount_data = str;
  239. return 1;
  240. }
  241. static char * __initdata root_fs_names;
  242. static int __init fs_names_setup(char *str)
  243. {
  244. root_fs_names = str;
  245. return 1;
  246. }
  247. static unsigned int __initdata root_delay;
  248. static int __init root_delay_setup(char *str)
  249. {
  250. root_delay = simple_strtoul(str, NULL, 0);
  251. return 1;
  252. }
  253. __setup("rootflags=", root_data_setup);
  254. __setup("rootfstype=", fs_names_setup);
  255. __setup("rootdelay=", root_delay_setup);
  256. static void __init get_fs_names(char *page)
  257. {
  258. char *s = page;
  259. if (root_fs_names) {
  260. strcpy(page, root_fs_names);
  261. while (*s++) {
  262. if (s[-1] == ',')
  263. s[-1] = '\0';
  264. }
  265. } else {
  266. int len = get_filesystem_list(page);
  267. char *p, *next;
  268. page[len] = '\0';
  269. for (p = page-1; p; p = next) {
  270. next = strchr(++p, '\n');
  271. if (*p++ != '\t')
  272. continue;
  273. while ((*s++ = *p++) != '\n')
  274. ;
  275. s[-1] = '\0';
  276. }
  277. }
  278. *s = '\0';
  279. }
  280. static int __init do_mount_root(char *name, char *fs, int flags, void *data)
  281. {
  282. int err = sys_mount(name, "/root", fs, flags, data);
  283. if (err)
  284. return err;
  285. sys_chdir((const char __user __force *)"/root");
  286. ROOT_DEV = current->fs->pwd.mnt->mnt_sb->s_dev;
  287. printk(KERN_INFO
  288. "VFS: Mounted root (%s filesystem)%s on device %u:%u.\n",
  289. current->fs->pwd.mnt->mnt_sb->s_type->name,
  290. current->fs->pwd.mnt->mnt_sb->s_flags & MS_RDONLY ?
  291. " readonly" : "", MAJOR(ROOT_DEV), MINOR(ROOT_DEV));
  292. return 0;
  293. }
  294. void __init mount_block_root(char *name, int flags)
  295. {
  296. char *fs_names = __getname_gfp(GFP_KERNEL
  297. | __GFP_NOTRACK_FALSE_POSITIVE);
  298. char *p;
  299. #ifdef CONFIG_BLOCK
  300. char b[BDEVNAME_SIZE];
  301. #else
  302. const char *b = name;
  303. #endif
  304. get_fs_names(fs_names);
  305. retry:
  306. for (p = fs_names; *p; p += strlen(p)+1) {
  307. int err = do_mount_root(name, p, flags, root_mount_data);
  308. switch (err) {
  309. case 0:
  310. goto out;
  311. case -EACCES:
  312. flags |= MS_RDONLY;
  313. goto retry;
  314. case -EINVAL:
  315. continue;
  316. }
  317. /*
  318. * Allow the user to distinguish between failed sys_open
  319. * and bad superblock on root device.
  320. * and give them a list of the available devices
  321. */
  322. #ifdef CONFIG_BLOCK
  323. __bdevname(ROOT_DEV, b);
  324. #endif
  325. printk("VFS: Cannot open root device \"%s\" or %s\n",
  326. root_device_name, b);
  327. printk("Please append a correct \"root=\" boot option; here are the available partitions:\n");
  328. printk_all_partitions();
  329. #ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT
  330. printk("DEBUG_BLOCK_EXT_DEVT is enabled, you need to specify "
  331. "explicit textual name for \"root=\" boot option.\n");
  332. #endif
  333. panic("VFS: Unable to mount root fs on %s", b);
  334. }
  335. printk("List of all partitions:\n");
  336. printk_all_partitions();
  337. printk("No filesystem could mount root, tried: ");
  338. for (p = fs_names; *p; p += strlen(p)+1)
  339. printk(" %s", p);
  340. printk("\n");
  341. #ifdef CONFIG_BLOCK
  342. __bdevname(ROOT_DEV, b);
  343. #endif
  344. panic("VFS: Unable to mount root fs on %s", b);
  345. out:
  346. putname(fs_names);
  347. }
  348. #ifdef CONFIG_ROOT_NFS
  349. static int __init mount_nfs_root(void)
  350. {
  351. char *root_dev, *root_data;
  352. if (nfs_root_data(&root_dev, &root_data) != 0)
  353. return 0;
  354. if (do_mount_root(root_dev, "nfs", root_mountflags, root_data) != 0)
  355. return 0;
  356. return 1;
  357. }
  358. #endif
  359. #if defined(CONFIG_BLK_DEV_RAM) || defined(CONFIG_BLK_DEV_FD)
  360. void __init change_floppy(char *fmt, ...)
  361. {
  362. struct termios termios;
  363. char buf[80];
  364. char c;
  365. int fd;
  366. va_list args;
  367. va_start(args, fmt);
  368. vsprintf(buf, fmt, args);
  369. va_end(args);
  370. fd = sys_open("/dev/root", O_RDWR | O_NDELAY, 0);
  371. if (fd >= 0) {
  372. sys_ioctl(fd, FDEJECT, 0);
  373. sys_close(fd);
  374. }
  375. printk(KERN_NOTICE "VFS: Insert %s and press ENTER\n", buf);
  376. fd = sys_open("/dev/console", O_RDWR, 0);
  377. if (fd >= 0) {
  378. sys_ioctl(fd, TCGETS, (long)&termios);
  379. termios.c_lflag &= ~ICANON;
  380. sys_ioctl(fd, TCSETSF, (long)&termios);
  381. sys_read(fd, &c, 1);
  382. termios.c_lflag |= ICANON;
  383. sys_ioctl(fd, TCSETSF, (long)&termios);
  384. sys_close(fd);
  385. }
  386. }
  387. #endif
  388. void __init mount_root(void)
  389. {
  390. #ifdef CONFIG_ROOT_NFS
  391. if (MAJOR(ROOT_DEV) == UNNAMED_MAJOR) {
  392. if (mount_nfs_root())
  393. return;
  394. printk(KERN_ERR "VFS: Unable to mount root fs via NFS, trying floppy.\n");
  395. ROOT_DEV = Root_FD0;
  396. }
  397. #endif
  398. #ifdef CONFIG_BLK_DEV_FD
  399. if (MAJOR(ROOT_DEV) == FLOPPY_MAJOR) {
  400. /* rd_doload is 2 for a dual initrd/ramload setup */
  401. if (rd_doload==2) {
  402. if (rd_load_disk(1)) {
  403. ROOT_DEV = Root_RAM1;
  404. root_device_name = NULL;
  405. }
  406. } else
  407. change_floppy("root floppy");
  408. }
  409. #endif
  410. #ifdef CONFIG_BLOCK
  411. create_dev("/dev/root", ROOT_DEV);
  412. mount_block_root("/dev/root", root_mountflags);
  413. #endif
  414. }
  415. /*
  416. * Prepare the namespace - decide what/where to mount, load ramdisks, etc.
  417. */
  418. void __init prepare_namespace(void)
  419. {
  420. int is_floppy;
  421. if (root_delay) {
  422. printk(KERN_INFO "Waiting %dsec before mounting root device...\n",
  423. root_delay);
  424. ssleep(root_delay);
  425. }
  426. /*
  427. * wait for the known devices to complete their probing
  428. *
  429. * Note: this is a potential source of long boot delays.
  430. * For example, it is not atypical to wait 5 seconds here
  431. * for the touchpad of a laptop to initialize.
  432. */
  433. wait_for_device_probe();
  434. md_run_setup();
  435. if (saved_root_name[0]) {
  436. root_device_name = saved_root_name;
  437. if (!strncmp(root_device_name, "mtd", 3) ||
  438. !strncmp(root_device_name, "ubi", 3)) {
  439. mount_block_root(root_device_name, root_mountflags);
  440. goto out;
  441. }
  442. ROOT_DEV = name_to_dev_t(root_device_name);
  443. if (strncmp(root_device_name, "/dev/", 5) == 0)
  444. root_device_name += 5;
  445. }
  446. if (initrd_load())
  447. goto out;
  448. /* wait for any asynchronous scanning to complete */
  449. if ((ROOT_DEV == 0) && root_wait) {
  450. printk(KERN_INFO "Waiting for root device %s...\n",
  451. saved_root_name);
  452. while (driver_probe_done() != 0 ||
  453. (ROOT_DEV = name_to_dev_t(saved_root_name)) == 0)
  454. msleep(100);
  455. async_synchronize_full();
  456. }
  457. is_floppy = MAJOR(ROOT_DEV) == FLOPPY_MAJOR;
  458. if (is_floppy && rd_doload && rd_load_disk(0))
  459. ROOT_DEV = Root_RAM0;
  460. mount_root();
  461. out:
  462. devtmpfs_mount("dev");
  463. sys_mount(".", "/", NULL, MS_MOVE, NULL);
  464. sys_chroot((const char __user __force *)".");
  465. }