do_mounts.c 13 KB

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