do_mounts.c 15 KB

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