do_mounts.c 13 KB

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