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