FreeBSD/Linux Kernel Cross Reference
sys/kern/vfs_conf.c
1 /*-
2 * Copyright (c) 1999 Michael Smith
3 * All rights reserved.
4 * Copyright (c) 1999 Poul-Henning Kamp
5 * All rights reserved.
6 *
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.
15 *
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.
27 *
28 * $FreeBSD: src/sys/kern/vfs_conf.c,v 1.49.2.5 2003/01/07 11:56:53 joerg Exp $
29 */
30
31 /*
32 * Locate and mount the root filesystem.
33 *
34 * The root filesystem is detailed in the kernel environment variable
35 * vfs.root.mountfrom, which is expected to be in the general format
36 *
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
42 *
43 */
44
45 #include "opt_rootdevname.h"
46
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/device.h>
59 #include <sys/disk.h>
60 #include <sys/namecache.h>
61 #include <sys/paths.h>
62 #include <sys/thread2.h>
63 #include <sys/nlookup.h>
64 #include <sys/devfs.h>
65 #include <sys/sysctl.h>
66
67 #include "opt_ddb.h"
68 #ifdef DDB
69 #include <ddb/ddb.h>
70 #endif
71
72 MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount structure");
73
74 #define ROOTNAME "root_device"
75
76 struct vnode *rootvnode;
77 struct nchandle rootnch;
78
79 /*
80 * The root specifiers we will try if RB_CDROM is specified. Note that
81 * with DEVFS we do not use the compatibility slice's whole-disk 'c'
82 * partition. Instead we just use the whole disk, e.g. cd0 or cd0s0.
83 */
84 static char *cdrom_rootdevnames[] = {
85 "cd9660:cd0", /* SCSI (including AHCI and SILI) */
86 "cd9660:acd0", /* NATA */
87 "cd9660:cd1", /* SCSI (including AHCI and SILI) */
88 "cd9660:acd1", /* NATA */
89 "cd9660:cd8", /* USB */
90 "cd9660:cd9", /* USB */
91 NULL
92 };
93
94 int vfs_mountroot_devfs(void);
95 static void vfs_mountroot(void *junk);
96 static int vfs_mountroot_try(const char *mountfrom);
97 static int vfs_mountroot_ask(void);
98 static int getline(char *cp, int limit);
99
100 /* legacy find-root code */
101 char *rootdevnames[2] = {NULL, NULL};
102 static int setrootbyname(char *name);
103
104 SYSINIT(mountroot, SI_SUB_MOUNT_ROOT, SI_ORDER_SECOND, vfs_mountroot, NULL);
105
106 static int wakedelay = 2; /* delay before mounting root in seconds */
107 TUNABLE_INT("vfs.root.wakedelay", &wakedelay);
108
109 /*
110 * Find and mount the root filesystem
111 */
112 static void
113 vfs_mountroot(void *junk)
114 {
115 cdev_t save_rootdev = rootdev;
116 int i;
117 int dummy;
118
119 /*
120 * Make sure all disk devices created so far have also been probed,
121 * and also make sure that the newly created device nodes for
122 * probed disks are ready, too.
123 *
124 * Messages can fly around here so get good synchronization
125 * coverage.
126 *
127 * XXX - Delay some more (default: 2s) to help drivers which pickup
128 * devices asynchronously and are not caught by CAM's initial
129 * probe.
130 */
131 sync_devs();
132 tsleep(&dummy, 0, "syncer", hz * wakedelay);
133
134
135 /*
136 * The root filesystem information is compiled in, and we are
137 * booted with instructions to use it.
138 */
139 #ifdef ROOTDEVNAME
140 if ((boothowto & RB_DFLTROOT) &&
141 !vfs_mountroot_try(ROOTDEVNAME))
142 return;
143 #endif
144 /*
145 * We are booted with instructions to prompt for the root filesystem,
146 * or to use the compiled-in default when it doesn't exist.
147 */
148 if (boothowto & (RB_DFLTROOT | RB_ASKNAME)) {
149 if (!vfs_mountroot_ask())
150 return;
151 }
152
153 /*
154 * We've been given the generic "use CDROM as root" flag. This is
155 * necessary because one media may be used in many different
156 * devices, so we need to search for them.
157 */
158 if (boothowto & RB_CDROM) {
159 for (i = 0; cdrom_rootdevnames[i] != NULL; i++) {
160 if (!vfs_mountroot_try(cdrom_rootdevnames[i]))
161 return;
162 }
163 }
164
165 /*
166 * Try to use the value read by the loader from /etc/fstab, or
167 * supplied via some other means. This is the preferred
168 * mechanism.
169 */
170 if (!vfs_mountroot_try(kgetenv("vfs.root.mountfrom")))
171 return;
172
173 /*
174 * If a vfs set rootdev, try it (XXX VINUM HACK!)
175 */
176 if (save_rootdev != NULL) {
177 rootdev = save_rootdev;
178 if (!vfs_mountroot_try(""))
179 return;
180 }
181
182 /*
183 * Try values that may have been computed by the machine-dependant
184 * legacy code.
185 */
186 if (rootdevnames[0] && !vfs_mountroot_try(rootdevnames[0]))
187 return;
188 if (rootdevnames[1] && !vfs_mountroot_try(rootdevnames[1]))
189 return;
190
191 /*
192 * If we have a compiled-in default, and haven't already tried it, try
193 * it now.
194 */
195 #ifdef ROOTDEVNAME
196 if (!(boothowto & RB_DFLTROOT))
197 if (!vfs_mountroot_try(ROOTDEVNAME))
198 return;
199 #endif
200
201 /*
202 * Everything so far has failed, prompt on the console if we haven't
203 * already tried that.
204 */
205 if (!(boothowto & (RB_DFLTROOT | RB_ASKNAME)) && !vfs_mountroot_ask())
206 return;
207 panic("Root mount failed, startup aborted.");
208 }
209
210
211 int
212 vfs_mountroot_devfs(void)
213 {
214 struct vnode *vp;
215 struct nchandle nch;
216 struct nlookupdata nd;
217 struct mount *mp;
218 struct vfsconf *vfsp;
219 int error;
220 struct ucred *cred = proc0.p_ucred;
221 const char *devfs_path, *init_chroot;
222 char *dev_malloced = NULL;
223
224 if ((init_chroot = kgetenv("init_chroot")) != NULL) {
225 size_t l;
226
227 l = strlen(init_chroot) + sizeof("/dev");
228 dev_malloced = kmalloc(l, M_MOUNT, M_WAITOK);
229 ksnprintf(dev_malloced, l, "%s/dev", init_chroot);
230 devfs_path = dev_malloced;
231 } else {
232 devfs_path = "/dev";
233 }
234 /*
235 * Lookup the requested path and extract the nch and vnode.
236 */
237 error = nlookup_init_raw(&nd,
238 devfs_path, UIO_SYSSPACE, NLC_FOLLOW,
239 cred, &rootnch);
240
241 if (error == 0) {
242 devfs_debug(DEVFS_DEBUG_DEBUG, "vfs_mountroot_devfs: nlookup_init is ok...\n");
243 if ((error = nlookup(&nd)) == 0) {
244 devfs_debug(DEVFS_DEBUG_DEBUG, "vfs_mountroot_devfs: nlookup is ok...\n");
245 if (nd.nl_nch.ncp->nc_vp == NULL) {
246 devfs_debug(DEVFS_DEBUG_SHOW, "vfs_mountroot_devfs: nlookup: simply not found\n");
247 error = ENOENT;
248 }
249 }
250 }
251 if (dev_malloced != NULL)
252 kfree(dev_malloced, M_MOUNT), dev_malloced = NULL;
253 devfs_path = NULL;
254 if (error) {
255 nlookup_done(&nd);
256 devfs_debug(DEVFS_DEBUG_SHOW, "vfs_mountroot_devfs: nlookup failed, error: %d\n", error);
257 return (error);
258 }
259
260 /*
261 * Extract the locked+refd ncp and cleanup the nd structure
262 */
263 nch = nd.nl_nch;
264 cache_zero(&nd.nl_nch);
265 nlookup_done(&nd);
266
267 /*
268 * now we have the locked ref'd nch and unreferenced vnode.
269 */
270 vp = nch.ncp->nc_vp;
271 if ((error = vget(vp, LK_EXCLUSIVE)) != 0) {
272 cache_put(&nch);
273 devfs_debug(DEVFS_DEBUG_SHOW, "vfs_mountroot_devfs: vget failed\n");
274 return (error);
275 }
276 cache_unlock(&nch);
277
278 if ((error = vinvalbuf(vp, V_SAVE, 0, 0)) != 0) {
279 cache_drop(&nch);
280 vput(vp);
281 devfs_debug(DEVFS_DEBUG_SHOW, "vfs_mountroot_devfs: vinvalbuf failed\n");
282 return (error);
283 }
284 if (vp->v_type != VDIR) {
285 cache_drop(&nch);
286 vput(vp);
287 devfs_debug(DEVFS_DEBUG_SHOW, "vfs_mountroot_devfs: vp is not VDIR\n");
288 return (ENOTDIR);
289 }
290
291 vfsp = vfsconf_find_by_name("devfs");
292
293 /*
294 * Allocate and initialize the filesystem.
295 */
296 mp = kmalloc(sizeof(struct mount), M_MOUNT, M_ZERO|M_WAITOK);
297 mount_init(mp);
298 vfs_busy(mp, LK_NOWAIT);
299 mp->mnt_op = vfsp->vfc_vfsops;
300 mp->mnt_vfc = vfsp;
301 vfsp->vfc_refcount++;
302 mp->mnt_stat.f_type = vfsp->vfc_typenum;
303 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK;
304 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN);
305 mp->mnt_stat.f_owner = cred->cr_uid;
306 vn_unlock(vp);
307
308 /*
309 * Mount the filesystem.
310 */
311 error = VFS_MOUNT(mp, "/dev", NULL, cred);
312
313 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
314
315 /*
316 * Put the new filesystem on the mount list after root. The mount
317 * point gets its own mnt_ncmountpt (unless the VFS already set one
318 * up) which represents the root of the mount. The lookup code
319 * detects the mount point going forward and checks the root of
320 * the mount going backwards.
321 *
322 * It is not necessary to invalidate or purge the vnode underneath
323 * because elements under the mount will be given their own glue
324 * namecache record.
325 */
326 if (!error) {
327 if (mp->mnt_ncmountpt.ncp == NULL) {
328 /*
329 * allocate, then unlock, but leave the ref intact
330 */
331 cache_allocroot(&mp->mnt_ncmountpt, mp, NULL);
332 cache_unlock(&mp->mnt_ncmountpt);
333 }
334 mp->mnt_ncmounton = nch; /* inherits ref */
335 nch.ncp->nc_flag |= NCF_ISMOUNTPT;
336
337 /* XXX get the root of the fs and cache_setvp(mnt_ncmountpt...) */
338 mountlist_insert(mp, MNTINS_LAST);
339 vn_unlock(vp);
340 //checkdirs(&mp->mnt_ncmounton, &mp->mnt_ncmountpt);
341 error = vfs_allocate_syncvnode(mp);
342 if (error) {
343 devfs_debug(DEVFS_DEBUG_SHOW, "vfs_mountroot_devfs: vfs_allocate_syncvnode failed\n");
344 }
345 vfs_unbusy(mp);
346 error = VFS_START(mp, 0);
347 vrele(vp);
348 } else {
349 vn_syncer_thr_stop(mp);
350 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
351 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
352 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
353 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
354 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
355 mp->mnt_vfc->vfc_refcount--;
356 vfs_unbusy(mp);
357 kfree(mp, M_MOUNT);
358 cache_drop(&nch);
359 vput(vp);
360 devfs_debug(DEVFS_DEBUG_SHOW, "vfs_mountroot_devfs: mount failed\n");
361 }
362
363 devfs_debug(DEVFS_DEBUG_DEBUG, "rootmount_devfs done with error: %d\n", error);
364 return (error);
365 }
366
367
368 /*
369 * Mount (mountfrom) as the root filesystem.
370 */
371 static int
372 vfs_mountroot_try(const char *mountfrom)
373 {
374 struct mount *mp;
375 char *vfsname, *devname;
376 int error;
377 char patt[32];
378 const char *cp, *ep;
379 char *mf;
380 struct proc *p;
381 struct vnode *vp;
382
383 vfsname = NULL;
384 devname = NULL;
385 mp = NULL;
386 error = EINVAL;
387
388 if (mountfrom == NULL)
389 return(error); /* don't complain */
390
391 crit_enter();
392 kprintf("Mounting root from %s\n", mountfrom);
393 crit_exit();
394
395 cp = mountfrom;
396 /* parse vfs name and devname */
397 vfsname = kmalloc(MFSNAMELEN, M_MOUNT, M_WAITOK);
398 devname = kmalloc(MNAMELEN, M_MOUNT, M_WAITOK);
399 mf = kmalloc(MFSNAMELEN+MNAMELEN, M_MOUNT, M_WAITOK);
400 for(;;) {
401 for (ep = cp; (*ep != 0) && (*ep != ';'); ep++);
402 bzero(vfsname, MFSNAMELEN);
403 bzero(devname, MNAMELEN);
404 bzero(mf, MFSNAMELEN+MNAMELEN);
405 strncpy(mf, cp, MFSNAMELEN+MNAMELEN);
406
407 vfsname[0] = devname[0] = 0;
408 ksprintf(patt, "%%%d[a-z0-9]:%%%ds", MFSNAMELEN, MNAMELEN);
409 if (ksscanf(mf, patt, vfsname, devname) < 1)
410 goto end;
411
412 /* allocate a root mount */
413 error = vfs_rootmountalloc(vfsname,
414 devname[0] != 0 ? devname : ROOTNAME, &mp);
415 if (error != 0) {
416 kprintf("Can't allocate root mount for filesystem '%s': %d\n",
417 vfsname, error);
418 goto end;
419 }
420 mp->mnt_flag |= MNT_ROOTFS;
421
422 /* do our best to set rootdev */
423 if ((strcmp(vfsname, "hammer") != 0) && (devname[0] != 0) &&
424 setrootbyname(devname))
425 kprintf("setrootbyname failed\n");
426
427 /* If the root device is a type "memory disk", mount RW */
428 if (rootdev != NULL && dev_is_good(rootdev) &&
429 (dev_dflags(rootdev) & D_MEMDISK)) {
430 mp->mnt_flag &= ~MNT_RDONLY;
431 }
432
433 error = VFS_MOUNT(mp, NULL, NULL, proc0.p_ucred);
434
435 if (!error)
436 break;
437 end:
438 if(*ep == 0)
439 break;
440 cp = ep + 1;
441 }
442
443 if (vfsname != NULL)
444 kfree(vfsname, M_MOUNT);
445 if (devname != NULL)
446 kfree(devname, M_MOUNT);
447 if (mf != NULL)
448 kfree(mf, M_MOUNT);
449 if (error == 0) {
450 /* register with list of mounted filesystems */
451 mountlist_insert(mp, MNTINS_FIRST);
452
453 /* sanity check system clock against root fs timestamp */
454 inittodr(mp->mnt_time);
455
456 /* Get the vnode for '/'. Set p->p_fd->fd_cdir to reference it. */
457 mp = mountlist_boot_getfirst();
458 if (VFS_ROOT(mp, &vp))
459 panic("cannot find root vnode");
460 if (mp->mnt_ncmountpt.ncp == NULL) {
461 cache_allocroot(&mp->mnt_ncmountpt, mp, vp);
462 cache_unlock(&mp->mnt_ncmountpt); /* leave ref intact */
463 }
464 p = curproc;
465 p->p_fd->fd_cdir = vp;
466 vref(p->p_fd->fd_cdir);
467 p->p_fd->fd_rdir = vp;
468 vref(p->p_fd->fd_rdir);
469 vfs_cache_setroot(vp, cache_hold(&mp->mnt_ncmountpt));
470 vn_unlock(vp); /* leave ref intact */
471 cache_copy(&mp->mnt_ncmountpt, &p->p_fd->fd_ncdir);
472 cache_copy(&mp->mnt_ncmountpt, &p->p_fd->fd_nrdir);
473
474 vfs_unbusy(mp);
475 if (mp->mnt_syncer == NULL) {
476 error = vfs_allocate_syncvnode(mp);
477 if (error)
478 kprintf("Warning: no syncer vp for root!\n");
479 error = 0;
480 }
481 VFS_START( mp, 0 );
482 } else {
483 if (mp != NULL) {
484 vn_syncer_thr_stop(mp);
485 vfs_unbusy(mp);
486 kfree(mp, M_MOUNT);
487 }
488 kprintf("Root mount failed: %d\n", error);
489 }
490 return(error);
491 }
492
493
494 static void
495 vfs_mountroot_ask_callback(char *name, cdev_t dev, bool is_alias,
496 void *arg __unused)
497 {
498 if (!is_alias && dev_is_good(dev) && (dev_dflags(dev) & D_DISK))
499 kprintf(" \"%s\" ", name);
500 }
501
502
503 /*
504 * Spin prompting on the console for a suitable root filesystem
505 */
506 static int
507 vfs_mountroot_ask(void)
508 {
509 char name[128];
510 int llimit = 100;
511
512 kprintf("\nManual root filesystem specification:\n");
513 kprintf(" <fstype>:<device> Specify root (e.g. ufs:da0s1a)\n");
514 kprintf(" ? List valid disk boot devices\n");
515 kprintf(" panic Just panic\n");
516 kprintf(" abort Abort manual input\n");
517 while (llimit--) {
518 kprintf("\nmountroot> ");
519
520 if (getline(name, 128) < 0)
521 break;
522 if (name[0] == 0) {
523 ;
524 } else if (name[0] == '?') {
525 kprintf("Possibly valid devices for root FS:\n");
526 //enumerate all disk devices
527 devfs_scan_callback(vfs_mountroot_ask_callback, NULL);
528 kprintf("\n");
529 continue;
530 } else if (strcmp(name, "panic") == 0) {
531 panic("panic from console");
532 } else if (strcmp(name, "abort") == 0) {
533 break;
534 } else if (vfs_mountroot_try(name) == 0) {
535 return(0);
536 }
537 }
538 return(1);
539 }
540
541
542 static int
543 getline(char *cp, int limit)
544 {
545 char *lp;
546 int c;
547
548 lp = cp;
549 for (;;) {
550 c = cngetc();
551
552 switch (c) {
553 case -1:
554 return(-1);
555 case '\n':
556 case '\r':
557 kprintf("\n");
558 *lp++ = '\0';
559 return(0);
560 case '\b':
561 case '\177':
562 if (lp > cp) {
563 kprintf("\b \b");
564 lp--;
565 } else {
566 kprintf("%c", 7);
567 }
568 continue;
569 case '#':
570 kprintf("#");
571 lp--;
572 if (lp < cp)
573 lp = cp;
574 continue;
575 case '@':
576 case 'u' & 037:
577 lp = cp;
578 kprintf("%c", '\n');
579 continue;
580 default:
581 if (lp - cp >= limit - 1) {
582 kprintf("%c", 7);
583 } else {
584 kprintf("%c", c);
585 *lp++ = c;
586 }
587 continue;
588 }
589 }
590 }
591
592 /*
593 * Convert a given name to the cdev_t of the disk-like device
594 * it refers to.
595 */
596 cdev_t
597 kgetdiskbyname(const char *name)
598 {
599 cdev_t rdev;
600
601 /*
602 * Get the base name of the device
603 */
604 if (strncmp(name, __SYS_PATH_DEV, sizeof(__SYS_PATH_DEV) - 1) == 0)
605 name += sizeof(__SYS_PATH_DEV) - 1;
606
607 /*
608 * Locate the device
609 */
610 rdev = devfs_find_device_by_name("%s", name);
611 if (rdev == NULL) {
612 kprintf("no disk named '%s'\n", name);
613 }
614 /*
615 * FOUND DEVICE
616 */
617 return(rdev);
618 }
619
620 /*
621 * Set rootdev to match (name), given that we expect it to
622 * refer to a disk-like device.
623 */
624 static int
625 setrootbyname(char *name)
626 {
627 cdev_t diskdev;
628
629 diskdev = kgetdiskbyname(name);
630 if (diskdev != NULL) {
631 rootdev = diskdev;
632 return (0);
633 }
634 /* set to NULL if kgetdiskbyname() fails so that if the first rootdev is
635 * found by fails to mount and the second one isn't found, mountroot_try
636 * doesn't try again with the first one
637 */
638 rootdev = NULL;
639 return (1);
640 }
641
642 #ifdef DDB
643 DB_SHOW_COMMAND(disk, db_getdiskbyname)
644 {
645 cdev_t dev;
646
647 if (modif[0] == '\0') {
648 db_error("usage: show disk/devicename");
649 return;
650 }
651 dev = kgetdiskbyname(modif);
652 if (dev != NULL)
653 db_printf("cdev_t = %p\n", dev);
654 else
655 db_printf("No disk device matched.\n");
656 }
657 #endif
658
659 static int
660 vfs_sysctl_real_root(SYSCTL_HANDLER_ARGS)
661 {
662 char *real_root;
663 size_t len;
664 int error;
665
666 real_root = kgetenv("vfs.root.realroot");
667
668 if (real_root == NULL)
669 real_root = "";
670
671 len = strlen(real_root) + 1;
672
673 error = sysctl_handle_string(oidp, real_root, len, req);
674
675 return error;
676 }
677
678 SYSCTL_PROC(_vfs, OID_AUTO, real_root,
679 CTLTYPE_STRING | CTLFLAG_RD, 0, 0, vfs_sysctl_real_root,
680 "A", "Real root mount string");
Cache object: e5e848da7e40efb8dee6138f59cc93e3
|