do_mounts.c 13 KB

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