2 * Copyright (c) 1999 Michael Smith
4 * Copyright (c) 1999 Poul-Henning Kamp
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * $FreeBSD: src/sys/kern/vfs_conf.c,v 1.49.2.5 2003/01/07 11:56:53 joerg Exp $
29 * $DragonFly: src/sys/kern/vfs_conf.c,v 1.34 2008/05/24 19:08:28 dillon Exp $
33 * Locate and mount the root filesystem.
35 * The root filesystem is detailed in the kernel environment variable
36 * vfs.root.mountfrom, which is expected to be in the general format
39 * vfsname := the name of a VFS known to the kernel and capable
40 * of being mounted as root
41 * path := disk device name or other data used by the filesystem
42 * to locate its physical store
46 #include "opt_rootdevname.h"
48 #include <sys/param.h>
49 #include <sys/kernel.h>
50 #include <sys/systm.h>
52 #include <sys/vnode.h>
53 #include <sys/mount.h>
54 #include <sys/malloc.h>
55 #include <sys/reboot.h>
56 #include <sys/diskslice.h>
59 #include <sys/device.h>
61 #include <sys/namecache.h>
62 #include <sys/paths.h>
63 #include <sys/thread2.h>
64 #include <sys/nlookup.h>
65 #include <vfs/devfs/devfs.h>
72 MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount structure");
74 #define ROOTNAME "root_device"
76 struct vnode *rootvnode;
77 struct nchandle rootnch;
80 * The root specifiers we will try if RB_CDROM is specified. Note that
81 * the ATA driver will accept acd*a and acd*c, but the SCSI driver
82 * will only accept cd*c, so use 'c'.
84 * XXX TGEN NATA and, presumably, 'old'ATA will also accept the device name
85 * without any fake partition, since the major & minor are identical for all
86 * three (acd*, acd*a and acd*c). However, due to an as-of-yet undiscovered
87 * bug, acd0c ends up with minor 2 when using NATA and booting cold. Since
88 * NATA's acd_open() is unable to fulfill mounts on such 'ghost' cdevs, acd0
89 * and acd1 have been added to the list of CD-ROM root device names.
91 static char *cdrom_rootdevnames[] = {
101 int vfs_mountroot_devfs(void);
102 static void vfs_mountroot(void *junk);
103 static int vfs_mountroot_try(const char *mountfrom);
104 static int vfs_mountroot_ask(void);
105 static int getline(char *cp, int limit);
107 /* legacy find-root code */
108 char *rootdevnames[2] = {NULL, NULL};
109 static int setrootbyname(char *name);
111 SYSINIT(mountroot, SI_SUB_MOUNT_ROOT, SI_ORDER_SECOND, vfs_mountroot, NULL);
114 * Find and mount the root filesystem
117 vfs_mountroot(void *junk)
120 cdev_t save_rootdev = rootdev;
123 * Make sure all disk devices created so far have also been probed,
124 * and also make sure that the newly created device nodes for
125 * probed disks are ready, too.
131 * The root filesystem information is compiled in, and we are
132 * booted with instructions to use it.
135 if ((boothowto & RB_DFLTROOT) &&
136 !vfs_mountroot_try(ROOTDEVNAME))
140 * We are booted with instructions to prompt for the root filesystem,
141 * or to use the compiled-in default when it doesn't exist.
143 if (boothowto & (RB_DFLTROOT | RB_ASKNAME)) {
144 if (!vfs_mountroot_ask())
149 * We've been given the generic "use CDROM as root" flag. This is
150 * necessary because one media may be used in many different
151 * devices, so we need to search for them.
153 if (boothowto & RB_CDROM) {
154 for (i = 0; cdrom_rootdevnames[i] != NULL; i++) {
155 if (!vfs_mountroot_try(cdrom_rootdevnames[i]))
161 * Try to use the value read by the loader from /etc/fstab, or
162 * supplied via some other means. This is the preferred
165 if (!vfs_mountroot_try(kgetenv("vfs.root.mountfrom")))
169 * If a vfs set rootdev, try it (XXX VINUM HACK!)
171 if (save_rootdev != NULL) {
172 rootdev = save_rootdev;
173 if (!vfs_mountroot_try(""))
178 * Try values that may have been computed by the machine-dependant
181 if (rootdevnames[0] && !vfs_mountroot_try(rootdevnames[0]))
183 if (rootdevnames[1] && !vfs_mountroot_try(rootdevnames[1]))
187 * If we have a compiled-in default, and haven't already tried it, try
191 if (!(boothowto & RB_DFLTROOT))
192 if (!vfs_mountroot_try(ROOTDEVNAME))
197 * Everything so far has failed, prompt on the console if we haven't
198 * already tried that.
200 if (!(boothowto & (RB_DFLTROOT | RB_ASKNAME)) && !vfs_mountroot_ask())
202 panic("Root mount failed, startup aborted.");
207 vfs_mountroot_devfs(void)
211 struct nlookupdata nd;
213 struct vfsconf *vfsp;
215 struct ucred *cred = proc0.p_ucred;
218 * Lookup the requested path and extract the nch and vnode.
220 error = nlookup_init_raw(&nd,
221 "/dev", UIO_SYSSPACE, NLC_FOLLOW,
225 devfs_debug(DEVFS_DEBUG_DEBUG, "vfs_mountroot_devfs: nlookup_init is ok...\n");
226 if ((error = nlookup(&nd)) == 0) {
227 devfs_debug(DEVFS_DEBUG_DEBUG, "vfs_mountroot_devfs: nlookup is ok...\n");
228 if (nd.nl_nch.ncp->nc_vp == NULL) {
229 devfs_debug(DEVFS_DEBUG_SHOW, "vfs_mountroot_devfs: nlookup: simply not found\n");
236 devfs_debug(DEVFS_DEBUG_SHOW, "vfs_mountroot_devfs: nlookup failed, error: %d\n", error);
241 * Extract the locked+refd ncp and cleanup the nd structure
244 cache_zero(&nd.nl_nch);
248 * now we have the locked ref'd nch and unreferenced vnode.
251 if ((error = vget(vp, LK_EXCLUSIVE)) != 0) {
253 devfs_debug(DEVFS_DEBUG_SHOW, "vfs_mountroot_devfs: vget failed\n");
258 if ((error = vinvalbuf(vp, V_SAVE, 0, 0)) != 0) {
261 devfs_debug(DEVFS_DEBUG_SHOW, "vfs_mountroot_devfs: vinvalbuf failed\n");
264 if (vp->v_type != VDIR) {
267 devfs_debug(DEVFS_DEBUG_SHOW, "vfs_mountroot_devfs: vp is not VDIR\n");
271 vfsp = vfsconf_find_by_name("devfs");
272 vp->v_flag |= VMOUNT;
275 * Allocate and initialize the filesystem.
277 mp = kmalloc(sizeof(struct mount), M_MOUNT, M_ZERO|M_WAITOK);
278 TAILQ_INIT(&mp->mnt_nvnodelist);
279 TAILQ_INIT(&mp->mnt_reservedvnlist);
280 TAILQ_INIT(&mp->mnt_jlist);
281 mp->mnt_nvnodelistsize = 0;
282 lockinit(&mp->mnt_lock, "vfslock", 0, 0);
283 vfs_busy(mp, LK_NOWAIT);
284 mp->mnt_op = vfsp->vfc_vfsops;
286 vfsp->vfc_refcount++;
287 mp->mnt_stat.f_type = vfsp->vfc_typenum;
288 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
289 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
290 mp->mnt_stat.f_owner = cred->cr_uid;
291 mp->mnt_iosize_max = DFLTPHYS;
295 * Mount the filesystem.
297 error = VFS_MOUNT(mp, "/dev", NULL, cred);
299 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
302 * Put the new filesystem on the mount list after root. The mount
303 * point gets its own mnt_ncmountpt (unless the VFS already set one
304 * up) which represents the root of the mount. The lookup code
305 * detects the mount point going forward and checks the root of
306 * the mount going backwards.
308 * It is not necessary to invalidate or purge the vnode underneath
309 * because elements under the mount will be given their own glue
313 if (mp->mnt_ncmountpt.ncp == NULL) {
315 * allocate, then unlock, but leave the ref intact
317 cache_allocroot(&mp->mnt_ncmountpt, mp, NULL);
318 cache_unlock(&mp->mnt_ncmountpt);
320 mp->mnt_ncmounton = nch; /* inherits ref */
321 nch.ncp->nc_flag |= NCF_ISMOUNTPT;
323 /* XXX get the root of the fs and cache_setvp(mnt_ncmountpt...) */
324 vp->v_flag &= ~VMOUNT;
325 mountlist_insert(mp, MNTINS_LAST);
327 //checkdirs(&mp->mnt_ncmounton, &mp->mnt_ncmountpt);
328 error = vfs_allocate_syncvnode(mp);
330 devfs_debug(DEVFS_DEBUG_SHOW, "vfs_mountroot_devfs: vfs_allocate_syncvnode failed\n");
333 error = VFS_START(mp, 0);
336 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
337 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
338 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
339 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
340 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
341 vp->v_flag &= ~VMOUNT;
342 mp->mnt_vfc->vfc_refcount--;
347 devfs_debug(DEVFS_DEBUG_SHOW, "vfs_mountroot_devfs: mount failed\n");
350 devfs_debug(DEVFS_DEBUG_DEBUG, "rootmount_devfs done with error: %d\n", error);
356 * Mount (mountfrom) as the root filesystem.
359 vfs_mountroot_try(const char *mountfrom)
361 struct mount *mp, *mp2;
362 char *vfsname, *devname;
372 if (mountfrom == NULL)
373 return(error); /* don't complain */
376 kprintf("Mounting root from %s\n", mountfrom);
379 /* parse vfs name and devname */
380 vfsname = kmalloc(MFSNAMELEN, M_MOUNT, M_WAITOK);
381 devname = kmalloc(MNAMELEN, M_MOUNT, M_WAITOK);
382 vfsname[0] = devname[0] = 0;
383 ksprintf(patt, "%%%d[a-z0-9]:%%%ds", MFSNAMELEN, MNAMELEN);
384 if (ksscanf(mountfrom, patt, vfsname, devname) < 1)
387 /* allocate a root mount */
388 error = vfs_rootmountalloc(vfsname,
389 devname[0] != 0 ? devname : ROOTNAME, &mp);
391 kprintf("Can't allocate root mount for filesystem '%s': %d\n",
395 mp->mnt_flag |= MNT_ROOTFS;
397 /* do our best to set rootdev */
398 if ((devname[0] != 0) && setrootbyname(devname))
399 kprintf("setrootbyname failed\n");
401 /* If the root device is a type "memory disk", mount RW */
402 if (rootdev != NULL && dev_is_good(rootdev) &&
403 (dev_dflags(rootdev) & D_MEMDISK)) {
404 mp->mnt_flag &= ~MNT_RDONLY;
407 error = VFS_MOUNT(mp, NULL, NULL, proc0.p_ucred);
410 //kprintf("Trying vfs_mountroot_devfs!\n");
411 //vfs_mountroot_devfs();
416 kfree(vfsname, M_MOUNT);
418 kfree(devname, M_MOUNT);
420 /* register with list of mounted filesystems */
421 mountlist_insert(mp, MNTINS_FIRST);
423 /* sanity check system clock against root fs timestamp */
424 inittodr(mp->mnt_time);
426 if (mp->mnt_syncer == NULL) {
427 error = vfs_allocate_syncvnode(mp);
429 kprintf("Warning: no syncer vp for root!\n");
437 kprintf("Root mount failed: %d\n", error);
443 static void vfs_mountroot_ask_callback(cdev_t);
446 * Spin prompting on the console for a suitable root filesystem
450 vfs_mountroot_ask(void)
455 kprintf("\nManual root filesystem specification:\n");
456 kprintf(" <fstype>:<device> Specify root (e.g. ufs:da0s1a)\n");
457 kprintf(" ? List valid disk boot devices\n");
458 kprintf(" panic Just panic\n");
459 kprintf(" abort Abort manual input\n");
461 kprintf("\nmountroot> ");
463 if (getline(name, 128) < 0)
467 } else if (name[0] == '?') {
468 kprintf("Possibly valid devices for root FS:\n");
469 //enumerate all disk devices
470 devfs_scan_callback(vfs_mountroot_ask_callback);
473 } else if (strcmp(name, "panic") == 0) {
474 panic("panic from console");
475 } else if (strcmp(name, "abort") == 0) {
477 } else if (vfs_mountroot_try(name) == 0) {
486 vfs_mountroot_ask_callback(cdev_t dev)
488 if (dev_is_good(dev) && (dev_dflags(dev) & D_DISK))
489 kprintf(" \"%s\" ", dev->si_name);
494 getline(char *cp, int limit)
532 if (lp - cp >= limit - 1) {
544 * Convert a given name to the cdev_t of the disk-like device
556 kgetdiskbyname(const char *name)
560 int unit, slice, part;
564 * Get the base name of the device
566 if (strncmp(name, __SYS_PATH_DEV, sizeof(__SYS_PATH_DEV) - 1) == 0)
567 name += sizeof(__SYS_PATH_DEV) - 1;
568 cp = __DECONST(char *, name);
571 while (*cp >= 'a' && *cp <= 'z')
574 kprintf("missing device name\n");
582 unit = strtol(cp, &cp, 10);
583 if (name + nlen == (const char *)cp || unit < 0 || unit >= DKMAXUNITS) {
584 kprintf("bad unit: %d\n", unit);
589 * Get the slice. Note that if no partition or partition 'a' is
590 * specified, and no slice is specified, we will try both 'ad0a'
591 * (which is what you get when slice is 0), and also 'ad0' (the
592 * whole-disk partition, slice == 1).
606 if (*cp >= 'a' && *cp <= 'p') {
614 kprintf("junk after name: %s\n", cp);
621 rdev = devfs_find_device_by_name(name);
623 kprintf("no disk named '%s'\n", name);
632 * Set rootdev to match (name), given that we expect it to
633 * refer to a disk-like device.
636 setrootbyname(char *name)
640 diskdev = kgetdiskbyname(name);
641 if (diskdev != NULL) {
645 /* set to NULL if kgetdiskbyname() fails so that if the first rootdev is
646 * found by fails to mount and the second one isn't found, mountroot_try
647 * doesn't try again with the first one
654 DB_SHOW_COMMAND(disk, db_getdiskbyname)
658 if (modif[0] == '\0') {
659 db_error("usage: show disk/devicename");
662 dev = kgetdiskbyname(modif);
664 db_printf("cdev_t = %p\n", dev);
666 db_printf("No disk device matched.\n");