do_mounts.c 12 KB

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