do_mounts.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  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/ramfs.h>
  20. #include <linux/shmem_fs.h>
  21. #include <linux/nfs_fs.h>
  22. #include <linux/nfs_fs_sb.h>
  23. #include <linux/nfs_mount.h>
  24. #include "do_mounts.h"
  25. int __initdata rd_doload; /* 1 = load RAM disk, 0 = don't load */
  26. int root_mountflags = MS_RDONLY | MS_SILENT;
  27. static char * __initdata root_device_name;
  28. static char __initdata saved_root_name[64];
  29. static int root_wait;
  30. dev_t ROOT_DEV;
  31. static int __init load_ramdisk(char *str)
  32. {
  33. rd_doload = simple_strtol(str,NULL,0) & 3;
  34. return 1;
  35. }
  36. __setup("load_ramdisk=", load_ramdisk);
  37. static int __init readonly(char *str)
  38. {
  39. if (*str)
  40. return 0;
  41. root_mountflags |= MS_RDONLY;
  42. return 1;
  43. }
  44. static int __init readwrite(char *str)
  45. {
  46. if (*str)
  47. return 0;
  48. root_mountflags &= ~MS_RDONLY;
  49. return 1;
  50. }
  51. __setup("ro", readonly);
  52. __setup("rw", readwrite);
  53. #ifdef CONFIG_BLOCK
  54. struct uuidcmp {
  55. const char *uuid;
  56. int len;
  57. };
  58. /**
  59. * match_dev_by_uuid - callback for finding a partition using its uuid
  60. * @dev: device passed in by the caller
  61. * @data: opaque pointer to the desired struct uuidcmp to match
  62. *
  63. * Returns 1 if the device matches, and 0 otherwise.
  64. */
  65. static int match_dev_by_uuid(struct device *dev, const void *data)
  66. {
  67. const struct uuidcmp *cmp = data;
  68. struct hd_struct *part = dev_to_part(dev);
  69. if (!part->info)
  70. goto no_match;
  71. if (strncasecmp(cmp->uuid, part->info->uuid, cmp->len))
  72. goto no_match;
  73. return 1;
  74. no_match:
  75. return 0;
  76. }
  77. /**
  78. * devt_from_partuuid - looks up the dev_t of a partition by its UUID
  79. * @uuid_str: char array containing ascii UUID
  80. *
  81. * The function will return the first partition which contains a matching
  82. * UUID value in its partition_meta_info struct. This does not search
  83. * by filesystem UUIDs.
  84. *
  85. * If @uuid_str is followed by a "/PARTNROFF=%d", then the number will be
  86. * extracted and used as an offset from the partition identified by the UUID.
  87. *
  88. * Returns the matching dev_t on success or 0 on failure.
  89. */
  90. static dev_t devt_from_partuuid(const char *uuid_str)
  91. {
  92. dev_t res = 0;
  93. struct uuidcmp cmp;
  94. struct device *dev = NULL;
  95. struct gendisk *disk;
  96. struct hd_struct *part;
  97. int offset = 0;
  98. bool clear_root_wait = false;
  99. char *slash;
  100. cmp.uuid = uuid_str;
  101. slash = strchr(uuid_str, '/');
  102. /* Check for optional partition number offset attributes. */
  103. if (slash) {
  104. char c = 0;
  105. /* Explicitly fail on poor PARTUUID syntax. */
  106. if (sscanf(slash + 1,
  107. "PARTNROFF=%d%c", &offset, &c) != 1) {
  108. clear_root_wait = true;
  109. goto done;
  110. }
  111. cmp.len = slash - uuid_str;
  112. } else {
  113. cmp.len = strlen(uuid_str);
  114. }
  115. if (!cmp.len) {
  116. clear_root_wait = true;
  117. goto done;
  118. }
  119. dev = class_find_device(&block_class, NULL, &cmp,
  120. &match_dev_by_uuid);
  121. if (!dev)
  122. goto done;
  123. res = dev->devt;
  124. /* Attempt to find the partition by offset. */
  125. if (!offset)
  126. goto no_offset;
  127. res = 0;
  128. disk = part_to_disk(dev_to_part(dev));
  129. part = disk_get_part(disk, dev_to_part(dev)->partno + offset);
  130. if (part) {
  131. res = part_devt(part);
  132. put_device(part_to_dev(part));
  133. }
  134. no_offset:
  135. put_device(dev);
  136. done:
  137. if (clear_root_wait) {
  138. pr_err("VFS: PARTUUID= is invalid.\n"
  139. "Expected PARTUUID=<valid-uuid-id>[/PARTNROFF=%%d]\n");
  140. if (root_wait)
  141. pr_err("Disabling rootwait; root= is invalid.\n");
  142. root_wait = 0;
  143. }
  144. return res;
  145. }
  146. #endif
  147. /*
  148. * Convert a name into device number. We accept the following variants:
  149. *
  150. * 1) <hex_major><hex_minor> device number in hexadecimal represents itself
  151. * no leading 0x, for example b302.
  152. * 2) /dev/nfs represents Root_NFS (0xff)
  153. * 3) /dev/<disk_name> represents the device number of disk
  154. * 4) /dev/<disk_name><decimal> represents the device number
  155. * of partition - device number of disk plus the partition number
  156. * 5) /dev/<disk_name>p<decimal> - same as the above, that form is
  157. * used when disk name of partitioned disk ends on a digit.
  158. * 6) PARTUUID=00112233-4455-6677-8899-AABBCCDDEEFF representing the
  159. * unique id of a partition if the partition table provides it.
  160. * The UUID may be either an EFI/GPT UUID, or refer to an MSDOS
  161. * partition using the format SSSSSSSS-PP, where SSSSSSSS is a zero-
  162. * filled hex representation of the 32-bit "NT disk signature", and PP
  163. * is a zero-filled hex representation of the 1-based partition number.
  164. * 7) PARTUUID=<UUID>/PARTNROFF=<int> to select a partition in relation to
  165. * a partition with a known unique id.
  166. * 8) <major>:<minor> major and minor number of the device separated by
  167. * a colon.
  168. *
  169. * If name doesn't have fall into the categories above, we return (0,0).
  170. * block_class is used to check if something is a disk name. If the disk
  171. * name contains slashes, the device name has them replaced with
  172. * bangs.
  173. */
  174. dev_t name_to_dev_t(const char *name)
  175. {
  176. char s[32];
  177. char *p;
  178. dev_t res = 0;
  179. int part;
  180. #ifdef CONFIG_BLOCK
  181. if (strncmp(name, "PARTUUID=", 9) == 0) {
  182. name += 9;
  183. res = devt_from_partuuid(name);
  184. if (!res)
  185. goto fail;
  186. goto done;
  187. }
  188. #endif
  189. if (strncmp(name, "/dev/", 5) != 0) {
  190. unsigned maj, min, offset;
  191. char dummy;
  192. if ((sscanf(name, "%u:%u%c", &maj, &min, &dummy) == 2) ||
  193. (sscanf(name, "%u:%u:%u:%c", &maj, &min, &offset, &dummy) == 3)) {
  194. res = MKDEV(maj, min);
  195. if (maj != MAJOR(res) || min != MINOR(res))
  196. goto fail;
  197. } else {
  198. res = new_decode_dev(simple_strtoul(name, &p, 16));
  199. if (*p)
  200. goto fail;
  201. }
  202. goto done;
  203. }
  204. name += 5;
  205. res = Root_NFS;
  206. if (strcmp(name, "nfs") == 0)
  207. goto done;
  208. res = Root_RAM0;
  209. if (strcmp(name, "ram") == 0)
  210. goto done;
  211. if (strlen(name) > 31)
  212. goto fail;
  213. strcpy(s, name);
  214. for (p = s; *p; p++)
  215. if (*p == '/')
  216. *p = '!';
  217. res = blk_lookup_devt(s, 0);
  218. if (res)
  219. goto done;
  220. /*
  221. * try non-existent, but valid partition, which may only exist
  222. * after revalidating the disk, like partitioned md devices
  223. */
  224. while (p > s && isdigit(p[-1]))
  225. p--;
  226. if (p == s || !*p || *p == '0')
  227. goto fail;
  228. /* try disk name without <part number> */
  229. part = simple_strtoul(p, NULL, 10);
  230. *p = '\0';
  231. res = blk_lookup_devt(s, part);
  232. if (res)
  233. goto done;
  234. /* try disk name without p<part number> */
  235. if (p < s + 2 || !isdigit(p[-2]) || p[-1] != 'p')
  236. goto fail;
  237. p[-1] = '\0';
  238. res = blk_lookup_devt(s, part);
  239. if (res)
  240. goto done;
  241. fail:
  242. return 0;
  243. done:
  244. return res;
  245. }
  246. EXPORT_SYMBOL_GPL(name_to_dev_t);
  247. static int __init root_dev_setup(char *line)
  248. {
  249. strlcpy(saved_root_name, line, sizeof(saved_root_name));
  250. return 1;
  251. }
  252. __setup("root=", root_dev_setup);
  253. static int __init rootwait_setup(char *str)
  254. {
  255. if (*str)
  256. return 0;
  257. root_wait = 1;
  258. return 1;
  259. }
  260. __setup("rootwait", rootwait_setup);
  261. static char * __initdata root_mount_data;
  262. static int __init root_data_setup(char *str)
  263. {
  264. root_mount_data = str;
  265. return 1;
  266. }
  267. static char * __initdata root_fs_names;
  268. static int __init fs_names_setup(char *str)
  269. {
  270. root_fs_names = str;
  271. return 1;
  272. }
  273. static unsigned int __initdata root_delay;
  274. static int __init root_delay_setup(char *str)
  275. {
  276. root_delay = simple_strtoul(str, NULL, 0);
  277. return 1;
  278. }
  279. __setup("rootflags=", root_data_setup);
  280. __setup("rootfstype=", fs_names_setup);
  281. __setup("rootdelay=", root_delay_setup);
  282. static void __init get_fs_names(char *page)
  283. {
  284. char *s = page;
  285. if (root_fs_names) {
  286. strcpy(page, root_fs_names);
  287. while (*s++) {
  288. if (s[-1] == ',')
  289. s[-1] = '\0';
  290. }
  291. } else {
  292. int len = get_filesystem_list(page);
  293. char *p, *next;
  294. page[len] = '\0';
  295. for (p = page-1; p; p = next) {
  296. next = strchr(++p, '\n');
  297. if (*p++ != '\t')
  298. continue;
  299. while ((*s++ = *p++) != '\n')
  300. ;
  301. s[-1] = '\0';
  302. }
  303. }
  304. *s = '\0';
  305. }
  306. static int __init do_mount_root(char *name, char *fs, int flags, void *data)
  307. {
  308. struct super_block *s;
  309. int err = ksys_mount(name, "/root", fs, flags, data);
  310. if (err)
  311. return err;
  312. ksys_chdir("/root");
  313. s = current->fs->pwd.dentry->d_sb;
  314. ROOT_DEV = s->s_dev;
  315. printk(KERN_INFO
  316. "VFS: Mounted root (%s filesystem)%s on device %u:%u.\n",
  317. s->s_type->name,
  318. sb_rdonly(s) ? " readonly" : "",
  319. MAJOR(ROOT_DEV), MINOR(ROOT_DEV));
  320. return 0;
  321. }
  322. void __init mount_block_root(char *name, int flags)
  323. {
  324. struct page *page = alloc_page(GFP_KERNEL);
  325. char *fs_names = page_address(page);
  326. char *p;
  327. #ifdef CONFIG_BLOCK
  328. char b[BDEVNAME_SIZE];
  329. #else
  330. const char *b = name;
  331. #endif
  332. get_fs_names(fs_names);
  333. retry:
  334. for (p = fs_names; *p; p += strlen(p)+1) {
  335. int err = do_mount_root(name, p, flags, root_mount_data);
  336. switch (err) {
  337. case 0:
  338. goto out;
  339. case -EACCES:
  340. case -EINVAL:
  341. continue;
  342. }
  343. /*
  344. * Allow the user to distinguish between failed sys_open
  345. * and bad superblock on root device.
  346. * and give them a list of the available devices
  347. */
  348. #ifdef CONFIG_BLOCK
  349. __bdevname(ROOT_DEV, b);
  350. #endif
  351. printk("VFS: Cannot open root device \"%s\" or %s: error %d\n",
  352. root_device_name, b, err);
  353. printk("Please append a correct \"root=\" boot option; here are the available partitions:\n");
  354. printk_all_partitions();
  355. #ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT
  356. printk("DEBUG_BLOCK_EXT_DEVT is enabled, you need to specify "
  357. "explicit textual name for \"root=\" boot option.\n");
  358. #endif
  359. panic("VFS: Unable to mount root fs on %s", b);
  360. }
  361. if (!(flags & SB_RDONLY)) {
  362. flags |= SB_RDONLY;
  363. goto retry;
  364. }
  365. printk("List of all partitions:\n");
  366. printk_all_partitions();
  367. printk("No filesystem could mount root, tried: ");
  368. for (p = fs_names; *p; p += strlen(p)+1)
  369. printk(" %s", p);
  370. printk("\n");
  371. #ifdef CONFIG_BLOCK
  372. __bdevname(ROOT_DEV, b);
  373. #endif
  374. panic("VFS: Unable to mount root fs on %s", b);
  375. out:
  376. put_page(page);
  377. }
  378. #ifdef CONFIG_ROOT_NFS
  379. #define NFSROOT_TIMEOUT_MIN 5
  380. #define NFSROOT_TIMEOUT_MAX 30
  381. #define NFSROOT_RETRY_MAX 5
  382. static int __init mount_nfs_root(void)
  383. {
  384. char *root_dev, *root_data;
  385. unsigned int timeout;
  386. int try, err;
  387. err = nfs_root_data(&root_dev, &root_data);
  388. if (err != 0)
  389. return 0;
  390. /*
  391. * The server or network may not be ready, so try several
  392. * times. Stop after a few tries in case the client wants
  393. * to fall back to other boot methods.
  394. */
  395. timeout = NFSROOT_TIMEOUT_MIN;
  396. for (try = 1; ; try++) {
  397. err = do_mount_root(root_dev, "nfs",
  398. root_mountflags, root_data);
  399. if (err == 0)
  400. return 1;
  401. if (try > NFSROOT_RETRY_MAX)
  402. break;
  403. /* Wait, in case the server refused us immediately */
  404. ssleep(timeout);
  405. timeout <<= 1;
  406. if (timeout > NFSROOT_TIMEOUT_MAX)
  407. timeout = NFSROOT_TIMEOUT_MAX;
  408. }
  409. return 0;
  410. }
  411. #endif
  412. #if defined(CONFIG_BLK_DEV_RAM) || defined(CONFIG_BLK_DEV_FD)
  413. void __init change_floppy(char *fmt, ...)
  414. {
  415. struct termios termios;
  416. char buf[80];
  417. char c;
  418. int fd;
  419. va_list args;
  420. va_start(args, fmt);
  421. vsprintf(buf, fmt, args);
  422. va_end(args);
  423. fd = ksys_open("/dev/root", O_RDWR | O_NDELAY, 0);
  424. if (fd >= 0) {
  425. ksys_ioctl(fd, FDEJECT, 0);
  426. ksys_close(fd);
  427. }
  428. printk(KERN_NOTICE "VFS: Insert %s and press ENTER\n", buf);
  429. fd = ksys_open("/dev/console", O_RDWR, 0);
  430. if (fd >= 0) {
  431. ksys_ioctl(fd, TCGETS, (long)&termios);
  432. termios.c_lflag &= ~ICANON;
  433. ksys_ioctl(fd, TCSETSF, (long)&termios);
  434. ksys_read(fd, &c, 1);
  435. termios.c_lflag |= ICANON;
  436. ksys_ioctl(fd, TCSETSF, (long)&termios);
  437. ksys_close(fd);
  438. }
  439. }
  440. #endif
  441. void __init mount_root(void)
  442. {
  443. #ifdef CONFIG_ROOT_NFS
  444. if (ROOT_DEV == Root_NFS) {
  445. if (mount_nfs_root())
  446. return;
  447. printk(KERN_ERR "VFS: Unable to mount root fs via NFS, trying floppy.\n");
  448. ROOT_DEV = Root_FD0;
  449. }
  450. #endif
  451. #ifdef CONFIG_BLK_DEV_FD
  452. if (MAJOR(ROOT_DEV) == FLOPPY_MAJOR) {
  453. /* rd_doload is 2 for a dual initrd/ramload setup */
  454. if (rd_doload==2) {
  455. if (rd_load_disk(1)) {
  456. ROOT_DEV = Root_RAM1;
  457. root_device_name = NULL;
  458. }
  459. } else
  460. change_floppy("root floppy");
  461. }
  462. #endif
  463. #ifdef CONFIG_BLOCK
  464. {
  465. int err = create_dev("/dev/root", ROOT_DEV);
  466. if (err < 0)
  467. pr_emerg("Failed to create /dev/root: %d\n", err);
  468. mount_block_root("/dev/root", root_mountflags);
  469. }
  470. #endif
  471. }
  472. /*
  473. * Prepare the namespace - decide what/where to mount, load ramdisks, etc.
  474. */
  475. void __init prepare_namespace(void)
  476. {
  477. int is_floppy;
  478. if (root_delay) {
  479. printk(KERN_INFO "Waiting %d sec before mounting root device...\n",
  480. root_delay);
  481. ssleep(root_delay);
  482. }
  483. /*
  484. * wait for the known devices to complete their probing
  485. *
  486. * Note: this is a potential source of long boot delays.
  487. * For example, it is not atypical to wait 5 seconds here
  488. * for the touchpad of a laptop to initialize.
  489. */
  490. wait_for_device_probe();
  491. md_run_setup();
  492. if (saved_root_name[0]) {
  493. root_device_name = saved_root_name;
  494. if (!strncmp(root_device_name, "mtd", 3) ||
  495. !strncmp(root_device_name, "ubi", 3)) {
  496. mount_block_root(root_device_name, root_mountflags);
  497. goto out;
  498. }
  499. ROOT_DEV = name_to_dev_t(root_device_name);
  500. if (strncmp(root_device_name, "/dev/", 5) == 0)
  501. root_device_name += 5;
  502. }
  503. if (initrd_load())
  504. goto out;
  505. /* wait for any asynchronous scanning to complete */
  506. if ((ROOT_DEV == 0) && root_wait) {
  507. printk(KERN_INFO "Waiting for root device %s...\n",
  508. saved_root_name);
  509. while (driver_probe_done() != 0 ||
  510. (ROOT_DEV = name_to_dev_t(saved_root_name)) == 0)
  511. msleep(5);
  512. async_synchronize_full();
  513. }
  514. is_floppy = MAJOR(ROOT_DEV) == FLOPPY_MAJOR;
  515. if (is_floppy && rd_doload && rd_load_disk(0))
  516. ROOT_DEV = Root_RAM0;
  517. mount_root();
  518. out:
  519. devtmpfs_mount("dev");
  520. ksys_mount(".", "/", NULL, MS_MOVE, NULL);
  521. ksys_chroot(".");
  522. }
  523. static bool is_tmpfs;
  524. static struct dentry *rootfs_mount(struct file_system_type *fs_type,
  525. int flags, const char *dev_name, void *data)
  526. {
  527. static unsigned long once;
  528. void *fill = ramfs_fill_super;
  529. if (test_and_set_bit(0, &once))
  530. return ERR_PTR(-ENODEV);
  531. if (IS_ENABLED(CONFIG_TMPFS) && is_tmpfs)
  532. fill = shmem_fill_super;
  533. return mount_nodev(fs_type, flags, data, fill);
  534. }
  535. static struct file_system_type rootfs_fs_type = {
  536. .name = "rootfs",
  537. .mount = rootfs_mount,
  538. .kill_sb = kill_litter_super,
  539. };
  540. int __init init_rootfs(void)
  541. {
  542. int err = register_filesystem(&rootfs_fs_type);
  543. if (err)
  544. return err;
  545. if (IS_ENABLED(CONFIG_TMPFS) && !saved_root_name[0] &&
  546. (!root_fs_names || strstr(root_fs_names, "tmpfs"))) {
  547. err = shmem_init();
  548. is_tmpfs = true;
  549. } else {
  550. err = init_ramfs_fs();
  551. }
  552. if (err)
  553. unregister_filesystem(&rootfs_fs_type);
  554. return err;
  555. }