kernel - Remove mplock from KTRACE paths
[dragonfly.git] / sys / kern / vfs_conf.c
blobd352aec4e1a496f725f5e470dccdb687eff43ca0
1 /*-
2 * Copyright (c) 1999 Michael Smith
3 * All rights reserved.
4 * Copyright (c) 1999 Poul-Henning Kamp
5 * All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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
26 * SUCH DAMAGE.
28 * $FreeBSD: src/sys/kern/vfs_conf.c,v 1.49.2.5 2003/01/07 11:56:53 joerg Exp $
32 * Locate and mount the root filesystem.
34 * The root filesystem is detailed in the kernel environment variable
35 * vfs.root.mountfrom, which is expected to be in the general format
37 * <vfsname>:[<path>]
38 * vfsname := the name of a VFS known to the kernel and capable
39 * of being mounted as root
40 * path := disk device name or other data used by the filesystem
41 * to locate its physical store
45 #include "opt_rootdevname.h"
47 #include <sys/param.h>
48 #include <sys/kernel.h>
49 #include <sys/systm.h>
50 #include <sys/proc.h>
51 #include <sys/vnode.h>
52 #include <sys/mount.h>
53 #include <sys/malloc.h>
54 #include <sys/reboot.h>
55 #include <sys/diskslice.h>
56 #include <sys/conf.h>
57 #include <sys/cons.h>
58 #include <sys/kbio.h>
59 #include <sys/device.h>
60 #include <sys/disk.h>
61 #include <sys/namecache.h>
62 #include <sys/paths.h>
63 #include <sys/thread2.h>
64 #include <sys/nlookup.h>
65 #include <sys/devfs.h>
66 #include <sys/sysctl.h>
68 #include "opt_ddb.h"
69 #ifdef DDB
70 #include <ddb/ddb.h>
71 #endif
73 MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount structure");
75 #define ROOTNAME "root_device"
77 struct vnode *rootvnode;
78 struct nchandle rootnch;
80 /*
81 * The root specifiers we will try if RB_CDROM is specified. Note that
82 * with DEVFS we do not use the compatibility slice's whole-disk 'c'
83 * partition. Instead we just use the whole disk, e.g. cd0 or cd0s0.
85 static char *cdrom_rootdevnames[] = {
86 "cd9660:cd0", /* SCSI (including AHCI and SILI) */
87 "cd9660:acd0", /* NATA */
88 "cd9660:cd1", /* SCSI (including AHCI and SILI) */
89 "cd9660:acd1", /* NATA */
90 "cd9660:cd8", /* USB */
91 "cd9660:cd9", /* USB */
92 NULL
95 int vfs_mountroot_devfs(void);
96 static void vfs_mountroot(void *junk);
97 static int vfs_mountroot_try(const char *mountfrom);
98 static int vfs_mountroot_ask(void);
99 static int get_line(char *cp, int limit);
101 /* legacy find-root code */
102 char *rootdevnames[2] = {NULL, NULL};
103 static int setrootbyname(char *name);
105 SYSINIT(mountroot, SI_SUB_MOUNT_ROOT, SI_ORDER_SECOND, vfs_mountroot, NULL);
107 static int wakedelay = 2; /* delay before mounting root in seconds */
108 TUNABLE_INT("vfs.root.wakedelay", &wakedelay);
111 * Find and mount the root filesystem
113 static void
114 vfs_mountroot(void *junk)
116 cdev_t save_rootdev = rootdev;
117 int i;
118 int dummy;
121 * Make sure all disk devices created so far have also been probed,
122 * and also make sure that the newly created device nodes for
123 * probed disks are ready, too.
125 * Messages can fly around here so get good synchronization
126 * coverage.
128 * XXX - Delay some more (default: 2s) to help drivers which pickup
129 * devices asynchronously and are not caught by CAM's initial
130 * probe.
132 sync_devs();
133 tsleep(&dummy, 0, "syncer", hz * wakedelay);
137 * The root filesystem information is compiled in, and we are
138 * booted with instructions to use it.
140 #ifdef ROOTDEVNAME
141 if ((boothowto & RB_DFLTROOT) &&
142 !vfs_mountroot_try(ROOTDEVNAME))
143 return;
144 #endif
146 * We are booted with instructions to prompt for the root filesystem,
147 * or to use the compiled-in default when it doesn't exist.
149 if (boothowto & (RB_DFLTROOT | RB_ASKNAME)) {
150 if (!vfs_mountroot_ask())
151 return;
155 * We've been given the generic "use CDROM as root" flag. This is
156 * necessary because one media may be used in many different
157 * devices, so we need to search for them.
159 if (boothowto & RB_CDROM) {
160 for (i = 0; cdrom_rootdevnames[i] != NULL; i++) {
161 if (!vfs_mountroot_try(cdrom_rootdevnames[i]))
162 return;
167 * Try to use the value read by the loader from /etc/fstab, or
168 * supplied via some other means. This is the preferred
169 * mechanism.
171 if (!vfs_mountroot_try(kgetenv("vfs.root.mountfrom")))
172 return;
175 * If a vfs set rootdev, try it (XXX VINUM HACK!)
177 if (save_rootdev != NULL) {
178 rootdev = save_rootdev;
179 if (!vfs_mountroot_try(""))
180 return;
184 * Try values that may have been computed by the machine-dependant
185 * legacy code.
187 if (rootdevnames[0] && !vfs_mountroot_try(rootdevnames[0]))
188 return;
189 if (rootdevnames[1] && !vfs_mountroot_try(rootdevnames[1]))
190 return;
193 * If we have a compiled-in default, and haven't already tried it, try
194 * it now.
196 #ifdef ROOTDEVNAME
197 if (!(boothowto & RB_DFLTROOT))
198 if (!vfs_mountroot_try(ROOTDEVNAME))
199 return;
200 #endif
203 * Everything so far has failed, prompt on the console if we haven't
204 * already tried that.
206 if (!(boothowto & (RB_DFLTROOT | RB_ASKNAME)) && !vfs_mountroot_ask())
207 return;
208 panic("Root mount failed, startup aborted.");
213 vfs_mountroot_devfs(void)
215 struct vnode *vp;
216 struct nchandle nch;
217 struct nlookupdata nd;
218 struct mount *mp;
219 struct vfsconf *vfsp;
220 int error;
221 struct ucred *cred = proc0.p_ucred;
222 const char *devfs_path, *init_chroot;
223 char *dev_malloced = NULL;
225 if ((init_chroot = kgetenv("init_chroot")) != NULL) {
226 size_t l;
228 l = strlen(init_chroot) + sizeof("/dev");
229 dev_malloced = kmalloc(l, M_MOUNT, M_WAITOK);
230 ksnprintf(dev_malloced, l, "%s/dev", init_chroot);
231 devfs_path = dev_malloced;
232 } else {
233 devfs_path = "/dev";
236 * Lookup the requested path and extract the nch and vnode.
238 error = nlookup_init_raw(&nd,
239 devfs_path, UIO_SYSSPACE, NLC_FOLLOW,
240 cred, &rootnch);
242 if (error == 0) {
243 devfs_debug(DEVFS_DEBUG_DEBUG, "vfs_mountroot_devfs: nlookup_init is ok...\n");
244 if ((error = nlookup(&nd)) == 0) {
245 devfs_debug(DEVFS_DEBUG_DEBUG, "vfs_mountroot_devfs: nlookup is ok...\n");
246 if (nd.nl_nch.ncp->nc_vp == NULL) {
247 devfs_debug(DEVFS_DEBUG_SHOW, "vfs_mountroot_devfs: nlookup: simply not found\n");
248 error = ENOENT;
252 if (dev_malloced != NULL)
253 kfree(dev_malloced, M_MOUNT), dev_malloced = NULL;
254 devfs_path = NULL;
255 if (error) {
256 nlookup_done(&nd);
257 devfs_debug(DEVFS_DEBUG_SHOW, "vfs_mountroot_devfs: nlookup failed, error: %d\n", error);
258 return (error);
262 * Extract the locked+refd ncp and cleanup the nd structure
264 nch = nd.nl_nch;
265 cache_zero(&nd.nl_nch);
266 nlookup_done(&nd);
269 * now we have the locked ref'd nch and unreferenced vnode.
271 vp = nch.ncp->nc_vp;
272 if ((error = vget(vp, LK_EXCLUSIVE)) != 0) {
273 cache_put(&nch);
274 devfs_debug(DEVFS_DEBUG_SHOW, "vfs_mountroot_devfs: vget failed\n");
275 return (error);
277 cache_unlock(&nch);
279 if ((error = vinvalbuf(vp, V_SAVE, 0, 0)) != 0) {
280 cache_drop(&nch);
281 vput(vp);
282 devfs_debug(DEVFS_DEBUG_SHOW, "vfs_mountroot_devfs: vinvalbuf failed\n");
283 return (error);
285 if (vp->v_type != VDIR) {
286 cache_drop(&nch);
287 vput(vp);
288 devfs_debug(DEVFS_DEBUG_SHOW, "vfs_mountroot_devfs: vp is not VDIR\n");
289 return (ENOTDIR);
292 vfsp = vfsconf_find_by_name("devfs");
295 * Allocate and initialize the filesystem.
297 mp = kmalloc(sizeof(struct mount), M_MOUNT, M_ZERO|M_WAITOK);
298 mount_init(mp);
299 vfs_busy(mp, LK_NOWAIT);
300 mp->mnt_op = vfsp->vfc_vfsops;
301 mp->mnt_vfc = vfsp;
302 vfsp->vfc_refcount++;
303 mp->mnt_stat.f_type = vfsp->vfc_typenum;
304 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
305 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
306 mp->mnt_stat.f_owner = cred->cr_uid;
307 vn_unlock(vp);
310 * Mount the filesystem.
312 error = VFS_MOUNT(mp, "/dev", NULL, cred);
314 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
317 * Put the new filesystem on the mount list after root. The mount
318 * point gets its own mnt_ncmountpt (unless the VFS already set one
319 * up) which represents the root of the mount. The lookup code
320 * detects the mount point going forward and checks the root of
321 * the mount going backwards.
323 * It is not necessary to invalidate or purge the vnode underneath
324 * because elements under the mount will be given their own glue
325 * namecache record.
327 if (!error) {
328 if (mp->mnt_ncmountpt.ncp == NULL) {
330 * allocate, then unlock, but leave the ref intact
332 cache_allocroot(&mp->mnt_ncmountpt, mp, NULL);
333 cache_unlock(&mp->mnt_ncmountpt);
335 vn_unlock(vp);
336 mp->mnt_ncmounton = nch; /* inherits ref */
337 cache_lock(&nch);
338 nch.ncp->nc_flag |= NCF_ISMOUNTPT;
339 cache_unlock(&nch);
340 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
343 * XXX get the root of the fs and
344 * cache_setvp(mnt_ncmountpt...)
346 mountlist_insert(mp, MNTINS_LAST);
347 vn_unlock(vp);
348 //checkdirs(&mp->mnt_ncmounton, &mp->mnt_ncmountpt);
349 error = vfs_allocate_syncvnode(mp);
350 if (error) {
351 devfs_debug(DEVFS_DEBUG_SHOW, "vfs_mountroot_devfs: vfs_allocate_syncvnode failed\n");
353 vfs_unbusy(mp);
354 error = VFS_START(mp, 0);
355 vrele(vp);
356 } else {
357 vn_syncer_thr_stop(mp);
358 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
359 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
360 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
361 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
362 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
363 mp->mnt_vfc->vfc_refcount--;
364 vfs_unbusy(mp);
365 kfree(mp, M_MOUNT);
366 cache_drop(&nch);
367 vput(vp);
368 devfs_debug(DEVFS_DEBUG_SHOW, "vfs_mountroot_devfs: mount failed\n");
371 devfs_debug(DEVFS_DEBUG_DEBUG, "rootmount_devfs done with error: %d\n", error);
372 return (error);
377 * Mount (mountfrom) as the root filesystem.
379 static int
380 vfs_mountroot_try(const char *mountfrom)
382 struct mount *mp;
383 char *vfsname, *devname;
384 int error;
385 char patt[32];
386 const char *cp, *ep;
387 char *mf;
388 struct proc *p;
389 struct vnode *vp;
391 vfsname = NULL;
392 devname = NULL;
393 mp = NULL;
394 error = EINVAL;
396 if (mountfrom == NULL)
397 return(error); /* don't complain */
399 crit_enter();
400 kprintf("Mounting root from %s\n", mountfrom);
401 crit_exit();
403 cp = mountfrom;
404 /* parse vfs name and devname */
405 vfsname = kmalloc(MFSNAMELEN, M_MOUNT, M_WAITOK);
406 devname = kmalloc(MNAMELEN, M_MOUNT, M_WAITOK);
407 mf = kmalloc(MFSNAMELEN+MNAMELEN, M_MOUNT, M_WAITOK);
408 for(;;) {
409 for (ep = cp; (*ep != 0) && (*ep != ';'); ep++);
410 bzero(vfsname, MFSNAMELEN);
411 bzero(devname, MNAMELEN);
412 bzero(mf, MFSNAMELEN+MNAMELEN);
413 strncpy(mf, cp, MFSNAMELEN+MNAMELEN);
415 vfsname[0] = devname[0] = 0;
416 ksprintf(patt, "%%%d[a-z0-9]:%%%ds", MFSNAMELEN, MNAMELEN);
417 if (ksscanf(mf, patt, vfsname, devname) < 1)
418 goto end;
420 /* allocate a root mount */
421 error = vfs_rootmountalloc(vfsname,
422 devname[0] != 0 ? devname : ROOTNAME, &mp);
423 if (error != 0) {
424 kprintf("Can't allocate root mount for filesystem '%s': %d\n",
425 vfsname, error);
426 goto end;
428 mp->mnt_flag |= MNT_ROOTFS;
430 /* do our best to set rootdev (really just for UFS) */
431 if (strcmp(vfsname, "hammer") != 0 &&
432 strcmp(vfsname, "hammer2") != 0 &&
433 (devname[0] != 0) && setrootbyname(devname)) {
434 kprintf("setrootbyname failed\n");
437 /* If the root device is a type "memory disk", mount RW */
438 if (rootdev != NULL && dev_is_good(rootdev) &&
439 (dev_dflags(rootdev) & D_MEMDISK)) {
440 mp->mnt_flag &= ~MNT_RDONLY;
443 error = VFS_MOUNT(mp, NULL, NULL, proc0.p_ucred);
445 if (!error)
446 break;
447 end:
448 if(*ep == 0)
449 break;
450 cp = ep + 1;
453 if (vfsname != NULL)
454 kfree(vfsname, M_MOUNT);
455 if (devname != NULL)
456 kfree(devname, M_MOUNT);
457 if (mf != NULL)
458 kfree(mf, M_MOUNT);
459 if (error == 0) {
460 /* register with list of mounted filesystems */
461 mountlist_insert(mp, MNTINS_FIRST);
463 /* sanity check system clock against root fs timestamp */
464 inittodr(mp->mnt_time);
466 /* Get the vnode for '/'. Set p->p_fd->fd_cdir to reference it. */
467 mp = mountlist_boot_getfirst();
468 if (VFS_ROOT(mp, &vp))
469 panic("cannot find root vnode");
470 if (mp->mnt_ncmountpt.ncp == NULL) {
471 cache_allocroot(&mp->mnt_ncmountpt, mp, vp);
472 cache_unlock(&mp->mnt_ncmountpt); /* leave ref intact */
474 p = curproc;
475 p->p_fd->fd_cdir = vp;
476 vref(p->p_fd->fd_cdir);
477 p->p_fd->fd_rdir = vp;
478 vref(p->p_fd->fd_rdir);
479 vfs_cache_setroot(vp, cache_hold(&mp->mnt_ncmountpt));
480 vn_unlock(vp); /* leave ref intact */
481 cache_copy(&mp->mnt_ncmountpt, &p->p_fd->fd_ncdir);
482 cache_copy(&mp->mnt_ncmountpt, &p->p_fd->fd_nrdir);
484 vfs_unbusy(mp);
485 if (mp->mnt_syncer == NULL) {
486 error = vfs_allocate_syncvnode(mp);
487 if (error)
488 kprintf("Warning: no syncer vp for root!\n");
489 error = 0;
491 VFS_START( mp, 0 );
492 } else {
493 if (mp != NULL) {
494 vn_syncer_thr_stop(mp);
495 vfs_unbusy(mp);
496 kfree(mp, M_MOUNT);
498 kprintf("Root mount failed: %d\n", error);
500 return(error);
504 static void
505 vfs_mountroot_ask_callback(char *name, cdev_t dev, bool is_alias,
506 void *arg __unused)
508 if (!is_alias && dev_is_good(dev) && (dev_dflags(dev) & D_DISK))
509 kprintf(" \"%s\" ", name);
514 * Spin prompting on the console for a suitable root filesystem
516 static int
517 vfs_mountroot_ask(void)
519 char name[128];
520 int llimit = 100;
522 kprintf("\nManual root filesystem specification:\n");
523 kprintf(" <fstype>:<device> Specify root (e.g. ufs:da0s1a)\n");
524 kprintf(" ? List valid disk boot devices\n");
525 kprintf(" panic Just panic\n");
526 kprintf(" abort Abort manual input\n");
527 while (llimit--) {
528 kprintf("\nmountroot> ");
530 if (get_line(name, 128) < 0)
531 break;
532 if (name[0] == 0) {
534 } else if (name[0] == '?') {
535 kprintf("Possibly valid devices for root FS:\n");
536 //enumerate all disk devices
537 devfs_scan_callback(vfs_mountroot_ask_callback, NULL);
538 kprintf("\n");
539 continue;
540 } else if (strcmp(name, "panic") == 0) {
541 panic("panic from console");
542 } else if (strcmp(name, "abort") == 0) {
543 break;
544 } else if (vfs_mountroot_try(name) == 0) {
545 return(0);
548 return(1);
552 static int
553 get_line(char *cp, int limit)
555 char *lp;
556 int dummy;
557 int c;
559 lp = cp;
560 cnpoll(TRUE);
561 for (;;) {
562 c = cncheckc();
564 switch (c) {
565 case NOKEY:
566 tsleep(&dummy, 0, "cnpoll", hz / 25);
567 break;
568 case -1:
569 goto done;
570 case '\n':
571 case '\r':
572 kprintf("\n");
573 *lp++ = '\0';
574 c = 0;
575 goto done;
576 case '\b':
577 case '\177':
578 if (lp > cp) {
579 kprintf("\b \b");
580 lp--;
581 } else {
582 kprintf("%c", 7);
584 continue;
585 case '#':
586 kprintf("#");
587 lp--;
588 if (lp < cp)
589 lp = cp;
590 continue;
591 case 'u' & 037:
592 /* NOTE: '@' no longer processed here, used for H2 */
593 lp = cp;
594 kprintf("%c", '\n');
595 continue;
596 default:
597 if (lp - cp >= limit - 1) {
598 kprintf("%c", 7);
599 } else {
600 kprintf("%c", c);
601 *lp++ = c;
603 continue;
606 done:
607 cnpoll(FALSE);
608 return c;
612 * Convert a given name to the cdev_t of the disk-like device
613 * it refers to.
615 cdev_t
616 kgetdiskbyname(const char *name)
618 cdev_t rdev;
621 * Get the base name of the device
623 if (strncmp(name, __SYS_PATH_DEV, sizeof(__SYS_PATH_DEV) - 1) == 0)
624 name += sizeof(__SYS_PATH_DEV) - 1;
627 * Locate the device
629 rdev = devfs_find_device_by_name("%s", name);
630 if (rdev == NULL) {
631 kprintf("no disk named '%s'\n", name);
634 * FOUND DEVICE
636 return(rdev);
640 * Set rootdev to match (name), given that we expect it to
641 * refer to a disk-like device.
643 static int
644 setrootbyname(char *name)
646 cdev_t diskdev;
648 diskdev = kgetdiskbyname(name);
649 if (diskdev != NULL) {
650 rootdev = diskdev;
651 return (0);
653 /* set to NULL if kgetdiskbyname() fails so that if the first rootdev is
654 * found by fails to mount and the second one isn't found, mountroot_try
655 * doesn't try again with the first one
657 rootdev = NULL;
658 return (1);
661 #ifdef DDB
662 DB_SHOW_COMMAND(disk, db_getdiskbyname)
664 cdev_t dev;
666 if (modif[0] == '\0') {
667 db_error("usage: show disk/devicename");
668 return;
670 dev = kgetdiskbyname(modif);
671 if (dev != NULL)
672 db_printf("cdev_t = %p\n", dev);
673 else
674 db_printf("No disk device matched.\n");
676 #endif
678 static int
679 vfs_sysctl_real_root(SYSCTL_HANDLER_ARGS)
681 char *real_root;
682 size_t len;
683 int error;
685 real_root = kgetenv("vfs.root.realroot");
687 if (real_root == NULL)
688 real_root = "";
690 len = strlen(real_root) + 1;
692 error = sysctl_handle_string(oidp, real_root, len, req);
694 return error;
697 SYSCTL_PROC(_vfs, OID_AUTO, real_root,
698 CTLTYPE_STRING | CTLFLAG_RD, 0, 0, vfs_sysctl_real_root,
699 "A", "Real root mount string");