do_mounts.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  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. char *fs_names = __getname_gfp(GFP_KERNEL
  308. | __GFP_NOTRACK_FALSE_POSITIVE);
  309. char *p;
  310. #ifdef CONFIG_BLOCK
  311. char b[BDEVNAME_SIZE];
  312. #else
  313. const char *b = name;
  314. #endif
  315. get_fs_names(fs_names);
  316. retry:
  317. for (p = fs_names; *p; p += strlen(p)+1) {
  318. int err = do_mount_root(name, p, flags, root_mount_data);
  319. switch (err) {
  320. case 0:
  321. goto out;
  322. case -EACCES:
  323. flags |= MS_RDONLY;
  324. goto retry;
  325. case -EINVAL:
  326. continue;
  327. }
  328. /*
  329. * Allow the user to distinguish between failed sys_open
  330. * and bad superblock on root device.
  331. * and give them a list of the available devices
  332. */
  333. #ifdef CONFIG_BLOCK
  334. __bdevname(ROOT_DEV, b);
  335. #endif
  336. printk("VFS: Cannot open root device \"%s\" or %s: error %d\n",
  337. root_device_name, b, err);
  338. printk("Please append a correct \"root=\" boot option; here are the available partitions:\n");
  339. printk_all_partitions();
  340. #ifdef CONFIG_DEBUG_BLOCK_EXT_DEVT
  341. printk("DEBUG_BLOCK_EXT_DEVT is enabled, you need to specify "
  342. "explicit textual name for \"root=\" boot option.\n");
  343. #endif
  344. panic("VFS: Unable to mount root fs on %s", b);
  345. }
  346. printk("List of all partitions:\n");
  347. printk_all_partitions();
  348. printk("No filesystem could mount root, tried: ");
  349. for (p = fs_names; *p; p += strlen(p)+1)
  350. printk(" %s", p);
  351. printk("\n");
  352. #ifdef CONFIG_BLOCK
  353. __bdevname(ROOT_DEV, b);
  354. #endif
  355. panic("VFS: Unable to mount root fs on %s", b);
  356. out:
  357. putname(fs_names);
  358. }
  359. #ifdef CONFIG_ROOT_NFS
  360. #define NFSROOT_TIMEOUT_MIN 5
  361. #define NFSROOT_TIMEOUT_MAX 30
  362. #define NFSROOT_RETRY_MAX 5
  363. static int __init mount_nfs_root(void)
  364. {
  365. char *root_dev, *root_data;
  366. unsigned int timeout;
  367. int try, err;
  368. err = nfs_root_data(&root_dev, &root_data);
  369. if (err != 0)
  370. return 0;
  371. /*
  372. * The server or network may not be ready, so try several
  373. * times. Stop after a few tries in case the client wants
  374. * to fall back to other boot methods.
  375. */
  376. timeout = NFSROOT_TIMEOUT_MIN;
  377. for (try = 1; ; try++) {
  378. err = do_mount_root(root_dev, "nfs",
  379. root_mountflags, root_data);
  380. if (err == 0)
  381. return 1;
  382. if (try > NFSROOT_RETRY_MAX)
  383. break;
  384. /* Wait, in case the server refused us immediately */
  385. ssleep(timeout);
  386. timeout <<= 1;
  387. if (timeout > NFSROOT_TIMEOUT_MAX)
  388. timeout = NFSROOT_TIMEOUT_MAX;
  389. }
  390. return 0;
  391. }
  392. #endif
  393. #if defined(CONFIG_BLK_DEV_RAM) || defined(CONFIG_BLK_DEV_FD)
  394. void __init change_floppy(char *fmt, ...)
  395. {
  396. struct termios termios;
  397. char buf[80];
  398. char c;
  399. int fd;
  400. va_list args;
  401. va_start(args, fmt);
  402. vsprintf(buf, fmt, args);
  403. va_end(args);
  404. fd = sys_open("/dev/root", O_RDWR | O_NDELAY, 0);
  405. if (fd >= 0) {
  406. sys_ioctl(fd, FDEJECT, 0);
  407. sys_close(fd);
  408. }
  409. printk(KERN_NOTICE "VFS: Insert %s and press ENTER\n", buf);
  410. fd = sys_open("/dev/console", O_RDWR, 0);
  411. if (fd >= 0) {
  412. sys_ioctl(fd, TCGETS, (long)&termios);
  413. termios.c_lflag &= ~ICANON;
  414. sys_ioctl(fd, TCSETSF, (long)&termios);
  415. sys_read(fd, &c, 1);
  416. termios.c_lflag |= ICANON;
  417. sys_ioctl(fd, TCSETSF, (long)&termios);
  418. sys_close(fd);
  419. }
  420. }
  421. #endif
  422. void __init mount_root(void)
  423. {
  424. #ifdef CONFIG_ROOT_NFS
  425. if (ROOT_DEV == Root_NFS) {
  426. if (mount_nfs_root())
  427. return;
  428. printk(KERN_ERR "VFS: Unable to mount root fs via NFS, trying floppy.\n");
  429. ROOT_DEV = Root_FD0;
  430. }
  431. #endif
  432. #ifdef CONFIG_BLK_DEV_FD
  433. if (MAJOR(ROOT_DEV) == FLOPPY_MAJOR) {
  434. /* rd_doload is 2 for a dual initrd/ramload setup */
  435. if (rd_doload==2) {
  436. if (rd_load_disk(1)) {
  437. ROOT_DEV = Root_RAM1;
  438. root_device_name = NULL;
  439. }
  440. } else
  441. change_floppy("root floppy");
  442. }
  443. #endif
  444. #ifdef CONFIG_BLOCK
  445. create_dev("/dev/root", ROOT_DEV);
  446. mount_block_root("/dev/root", root_mountflags);
  447. #endif
  448. }
  449. /*
  450. * Prepare the namespace - decide what/where to mount, load ramdisks, etc.
  451. */
  452. void __init prepare_namespace(void)
  453. {
  454. int is_floppy;
  455. if (root_delay) {
  456. printk(KERN_INFO "Waiting %dsec before mounting root device...\n",
  457. root_delay);
  458. ssleep(root_delay);
  459. }
  460. /*
  461. * wait for the known devices to complete their probing
  462. *
  463. * Note: this is a potential source of long boot delays.
  464. * For example, it is not atypical to wait 5 seconds here
  465. * for the touchpad of a laptop to initialize.
  466. */
  467. wait_for_device_probe();
  468. md_run_setup();
  469. if (saved_root_name[0]) {
  470. root_device_name = saved_root_name;
  471. if (!strncmp(root_device_name, "mtd", 3) ||
  472. !strncmp(root_device_name, "ubi", 3)) {
  473. mount_block_root(root_device_name, root_mountflags);
  474. goto out;
  475. }
  476. ROOT_DEV = name_to_dev_t(root_device_name);
  477. if (strncmp(root_device_name, "/dev/", 5) == 0)
  478. root_device_name += 5;
  479. }
  480. if (initrd_load())
  481. goto out;
  482. /* wait for any asynchronous scanning to complete */
  483. if ((ROOT_DEV == 0) && root_wait) {
  484. printk(KERN_INFO "Waiting for root device %s...\n",
  485. saved_root_name);
  486. while (driver_probe_done() != 0 ||
  487. (ROOT_DEV = name_to_dev_t(saved_root_name)) == 0)
  488. msleep(100);
  489. async_synchronize_full();
  490. }
  491. is_floppy = MAJOR(ROOT_DEV) == FLOPPY_MAJOR;
  492. if (is_floppy && rd_doload && rd_load_disk(0))
  493. ROOT_DEV = Root_RAM0;
  494. mount_root();
  495. out:
  496. devtmpfs_mount("dev");
  497. sys_mount(".", "/", NULL, MS_MOVE, NULL);
  498. sys_chroot(".");
  499. }