do_mounts.c 14 KB

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