do_mounts.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  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: 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 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) device number in hexadecimal represents itself
  160. * 2) /dev/nfs represents Root_NFS (0xff)
  161. * 3) /dev/<disk_name> represents the device number of disk
  162. * 4) /dev/<disk_name><decimal> represents the device number
  163. * of partition - device number of disk plus the partition number
  164. * 5) /dev/<disk_name>p<decimal> - same as the above, that form is
  165. * used when disk name of partitioned disk ends on a digit.
  166. * 6) PARTUUID=00112233-4455-6677-8899-AABBCCDDEEFF representing the
  167. * unique id of a partition if the partition table provides it.
  168. * The UUID may be either an EFI/GPT UUID, or refer to an MSDOS
  169. * partition using the format SSSSSSSS-PP, where SSSSSSSS is a zero-
  170. * filled hex representation of the 32-bit "NT disk signature", and PP
  171. * is a zero-filled hex representation of the 1-based partition number.
  172. * 7) PARTUUID=<UUID>/PARTNROFF=<int> to select a partition in relation to
  173. * a partition with a known unique id.
  174. * 8) <major>:<minor> major and minor number of the device separated by
  175. * a colon.
  176. *
  177. * If name doesn't have fall into the categories above, we return (0,0).
  178. * block_class is used to check if something is a disk name. If the disk
  179. * name contains slashes, the device name has them replaced with
  180. * bangs.
  181. */
  182. dev_t name_to_dev_t(char *name)
  183. {
  184. char s[32];
  185. char *p;
  186. dev_t res = 0;
  187. int part;
  188. #ifdef CONFIG_BLOCK
  189. if (strncmp(name, "PARTUUID=", 9) == 0) {
  190. name += 9;
  191. res = devt_from_partuuid(name);
  192. if (!res)
  193. goto fail;
  194. goto done;
  195. }
  196. #endif
  197. if (strncmp(name, "/dev/", 5) != 0) {
  198. unsigned maj, min;
  199. if (sscanf(name, "%u:%u", &maj, &min) == 2) {
  200. res = MKDEV(maj, min);
  201. if (maj != MAJOR(res) || min != MINOR(res))
  202. goto fail;
  203. } else {
  204. res = new_decode_dev(simple_strtoul(name, &p, 16));
  205. if (*p)
  206. goto fail;
  207. }
  208. goto done;
  209. }
  210. name += 5;
  211. res = Root_NFS;
  212. if (strcmp(name, "nfs") == 0)
  213. goto done;
  214. res = Root_RAM0;
  215. if (strcmp(name, "ram") == 0)
  216. goto done;
  217. if (strlen(name) > 31)
  218. goto fail;
  219. strcpy(s, name);
  220. for (p = s; *p; p++)
  221. if (*p == '/')
  222. *p = '!';
  223. res = blk_lookup_devt(s, 0);
  224. if (res)
  225. goto done;
  226. /*
  227. * try non-existent, but valid partition, which may only exist
  228. * after revalidating the disk, like partitioned md devices
  229. */
  230. while (p > s && isdigit(p[-1]))
  231. p--;
  232. if (p == s || !*p || *p == '0')
  233. goto fail;
  234. /* try disk name without <part number> */
  235. part = simple_strtoul(p, NULL, 10);
  236. *p = '\0';
  237. res = blk_lookup_devt(s, part);
  238. if (res)
  239. goto done;
  240. /* try disk name without p<part number> */
  241. if (p < s + 2 || !isdigit(p[-2]) || p[-1] != 'p')
  242. goto fail;
  243. p[-1] = '\0';
  244. res = blk_lookup_devt(s, part);
  245. if (res)
  246. goto done;
  247. fail:
  248. return 0;
  249. done:
  250. return res;
  251. }
  252. static int __init root_dev_setup(char *line)
  253. {
  254. strlcpy(saved_root_name, line, sizeof(saved_root_name));
  255. return 1;
  256. }
  257. __setup("root=", root_dev_setup);
  258. static int __init rootwait_setup(char *str)
  259. {
  260. if (*str)
  261. return 0;
  262. root_wait = 1;
  263. return 1;
  264. }
  265. __setup("rootwait", rootwait_setup);
  266. static char * __initdata root_mount_data;
  267. static int __init root_data_setup(char *str)
  268. {
  269. root_mount_data = str;
  270. return 1;
  271. }
  272. static char * __initdata root_fs_names;
  273. static int __init fs_names_setup(char *str)
  274. {
  275. root_fs_names = str;
  276. return 1;
  277. }
  278. static unsigned int __initdata root_delay;
  279. static int __init root_delay_setup(char *str)
  280. {
  281. root_delay = simple_strtoul(str, NULL, 0);
  282. return 1;
  283. }
  284. __setup("rootflags=", root_data_setup);
  285. __setup("rootfstype=", fs_names_setup);
  286. __setup("rootdelay=", root_delay_setup);
  287. static void __init get_fs_names(char *page)
  288. {
  289. char *s = page;
  290. if (root_fs_names) {
  291. strcpy(page, root_fs_names);
  292. while (*s++) {
  293. if (s[-1] == ',')
  294. s[-1] = '\0';
  295. }
  296. } else {
  297. int len = get_filesystem_list(page);
  298. char *p, *next;
  299. page[len] = '\0';
  300. for (p = page-1; p; p = next) {
  301. next = strchr(++p, '\n');
  302. if (*p++ != '\t')
  303. continue;
  304. while ((*s++ = *p++) != '\n')
  305. ;
  306. s[-1] = '\0';
  307. }
  308. }
  309. *s = '\0';
  310. }
  311. static int __init do_mount_root(char *name, char *fs, int flags, void *data)
  312. {
  313. struct super_block *s;
  314. int err = sys_mount(name, "/root", fs, flags, data);
  315. if (err)
  316. return err;
  317. sys_chdir("/root");
  318. s = current->fs->pwd.dentry->d_sb;
  319. ROOT_DEV = s->s_dev;
  320. printk(KERN_INFO
  321. "VFS: Mounted root (%s filesystem)%s on device %u:%u.\n",
  322. s->s_type->name,
  323. s->s_flags & MS_RDONLY ? " readonly" : "",
  324. MAJOR(ROOT_DEV), MINOR(ROOT_DEV));
  325. return 0;
  326. }
  327. void __init mount_block_root(char *name, int flags)
  328. {
  329. struct page *page = alloc_page(GFP_KERNEL |
  330. __GFP_NOTRACK_FALSE_POSITIVE);
  331. char *fs_names = page_address(page);
  332. char *p;
  333. #ifdef CONFIG_BLOCK
  334. char b[BDEVNAME_SIZE];
  335. #else
  336. const char *b = name;
  337. #endif
  338. get_fs_names(fs_names);
  339. retry:
  340. for (p = fs_names; *p; p += strlen(p)+1) {
  341. int err = do_mount_root(name, p, flags, root_mount_data);
  342. switch (err) {
  343. case 0:
  344. goto out;
  345. case -EACCES:
  346. flags |= MS_RDONLY;
  347. goto retry;
  348. case -EINVAL:
  349. continue;
  350. }
  351. /*
  352. * Allow the user to distinguish between failed sys_open
  353. * and bad superblock on root device.
  354. * and give them a list of the available devices
  355. */
  356. #ifdef CONFIG_BLOCK
  357. __bdevname(ROOT_DEV, b);
  358. #endif
  359. printk("VFS: Cannot open root device \"%s\" or %s: error %d\n",
  360. root_device_name, b, err);
  361. printk("Please append a correct \"root=\" boot option; here are the available partitions:\n");
  362. printk_all_partitions();
  363. #ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT
  364. printk("DEBUG_BLOCK_EXT_DEVT is enabled, you need to specify "
  365. "explicit textual name for \"root=\" boot option.\n");
  366. #endif
  367. panic("VFS: Unable to mount root fs on %s", b);
  368. }
  369. printk("List of all partitions:\n");
  370. printk_all_partitions();
  371. printk("No filesystem could mount root, tried: ");
  372. for (p = fs_names; *p; p += strlen(p)+1)
  373. printk(" %s", p);
  374. printk("\n");
  375. #ifdef CONFIG_BLOCK
  376. __bdevname(ROOT_DEV, b);
  377. #endif
  378. panic("VFS: Unable to mount root fs on %s", b);
  379. out:
  380. put_page(page);
  381. }
  382. #ifdef CONFIG_ROOT_NFS
  383. #define NFSROOT_TIMEOUT_MIN 5
  384. #define NFSROOT_TIMEOUT_MAX 30
  385. #define NFSROOT_RETRY_MAX 5
  386. static int __init mount_nfs_root(void)
  387. {
  388. char *root_dev, *root_data;
  389. unsigned int timeout;
  390. int try, err;
  391. err = nfs_root_data(&root_dev, &root_data);
  392. if (err != 0)
  393. return 0;
  394. /*
  395. * The server or network may not be ready, so try several
  396. * times. Stop after a few tries in case the client wants
  397. * to fall back to other boot methods.
  398. */
  399. timeout = NFSROOT_TIMEOUT_MIN;
  400. for (try = 1; ; try++) {
  401. err = do_mount_root(root_dev, "nfs",
  402. root_mountflags, root_data);
  403. if (err == 0)
  404. return 1;
  405. if (try > NFSROOT_RETRY_MAX)
  406. break;
  407. /* Wait, in case the server refused us immediately */
  408. ssleep(timeout);
  409. timeout <<= 1;
  410. if (timeout > NFSROOT_TIMEOUT_MAX)
  411. timeout = NFSROOT_TIMEOUT_MAX;
  412. }
  413. return 0;
  414. }
  415. #endif
  416. #if defined(CONFIG_BLK_DEV_RAM) || defined(CONFIG_BLK_DEV_FD)
  417. void __init change_floppy(char *fmt, ...)
  418. {
  419. struct termios termios;
  420. char buf[80];
  421. char c;
  422. int fd;
  423. va_list args;
  424. va_start(args, fmt);
  425. vsprintf(buf, fmt, args);
  426. va_end(args);
  427. fd = sys_open("/dev/root", O_RDWR | O_NDELAY, 0);
  428. if (fd >= 0) {
  429. sys_ioctl(fd, FDEJECT, 0);
  430. sys_close(fd);
  431. }
  432. printk(KERN_NOTICE "VFS: Insert %s and press ENTER\n", buf);
  433. fd = sys_open("/dev/console", O_RDWR, 0);
  434. if (fd >= 0) {
  435. sys_ioctl(fd, TCGETS, (long)&termios);
  436. termios.c_lflag &= ~ICANON;
  437. sys_ioctl(fd, TCSETSF, (long)&termios);
  438. sys_read(fd, &c, 1);
  439. termios.c_lflag |= ICANON;
  440. sys_ioctl(fd, TCSETSF, (long)&termios);
  441. sys_close(fd);
  442. }
  443. }
  444. #endif
  445. void __init mount_root(void)
  446. {
  447. #ifdef CONFIG_ROOT_NFS
  448. if (ROOT_DEV == Root_NFS) {
  449. if (mount_nfs_root())
  450. return;
  451. printk(KERN_ERR "VFS: Unable to mount root fs via NFS, trying floppy.\n");
  452. ROOT_DEV = Root_FD0;
  453. }
  454. #endif
  455. #ifdef CONFIG_BLK_DEV_FD
  456. if (MAJOR(ROOT_DEV) == FLOPPY_MAJOR) {
  457. /* rd_doload is 2 for a dual initrd/ramload setup */
  458. if (rd_doload==2) {
  459. if (rd_load_disk(1)) {
  460. ROOT_DEV = Root_RAM1;
  461. root_device_name = NULL;
  462. }
  463. } else
  464. change_floppy("root floppy");
  465. }
  466. #endif
  467. #ifdef CONFIG_BLOCK
  468. create_dev("/dev/root", ROOT_DEV);
  469. mount_block_root("/dev/root", root_mountflags);
  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(100);
  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. sys_mount(".", "/", NULL, MS_MOVE, NULL);
  521. sys_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. }