do_mounts.c 13 KB

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