kernel - Add root mount support for hammer2
[dragonfly.git] / sys / kern / vfs_conf.c
CommitLineData
984263bc
MD
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>
984263bc
MD
56#include <sys/conf.h>
57#include <sys/cons.h>
ce7866b8 58#include <sys/kbio.h>
335dda38 59#include <sys/device.h>
cd29885a 60#include <sys/disk.h>
21739618 61#include <sys/namecache.h>
1f2de5d4 62#include <sys/paths.h>
e43a034f 63#include <sys/thread2.h>
cd29885a 64#include <sys/nlookup.h>
2c1e28dd 65#include <sys/devfs.h>
b480ee28 66#include <sys/sysctl.h>
984263bc
MD
67
68#include "opt_ddb.h"
69#ifdef DDB
70#include <ddb/ddb.h>
71#endif
72
73MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount structure");
74
75#define ROOTNAME "root_device"
76
77struct vnode *rootvnode;
28623bf9 78struct nchandle rootnch;
984263bc
MD
79
80/*
6fcb48f2 81 * The root specifiers we will try if RB_CDROM is specified. Note that
aec8eea4
MD
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.
984263bc
MD
84 */
85static char *cdrom_rootdevnames[] = {
d81eda2e
MD
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 */
984263bc
MD
92 NULL
93};
94
cd29885a 95int vfs_mountroot_devfs(void);
984263bc 96static void vfs_mountroot(void *junk);
6b98993e 97static int vfs_mountroot_try(const char *mountfrom);
984263bc 98static int vfs_mountroot_ask(void);
cee92776 99static int getline(char *cp, int limit);
984263bc
MD
100
101/* legacy find-root code */
102char *rootdevnames[2] = {NULL, NULL};
103static int setrootbyname(char *name);
104
105SYSINIT(mountroot, SI_SUB_MOUNT_ROOT, SI_ORDER_SECOND, vfs_mountroot, NULL);
5a82ae89
SW
106
107static int wakedelay = 2; /* delay before mounting root in seconds */
108TUNABLE_INT("vfs.root.wakedelay", &wakedelay);
109
984263bc
MD
110/*
111 * Find and mount the root filesystem
112 */
113static void
114vfs_mountroot(void *junk)
115{
b13267a5 116 cdev_t save_rootdev = rootdev;
a2b57962
MD
117 int i;
118 int dummy;
984263bc 119
cd29885a
MD
120 /*
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.
1c0fe2ad
MD
124 *
125 * Messages can fly around here so get good synchronization
126 * coverage.
a2b57962 127 *
5a82ae89 128 * XXX - Delay some more (default: 2s) to help drivers which pickup
a2b57962
MD
129 * devices asynchronously and are not caught by CAM's initial
130 * probe.
cd29885a 131 */
2a32d680 132 sync_devs();
5a82ae89 133 tsleep(&dummy, 0, "syncer", hz * wakedelay);
a2b57962 134
cd29885a 135
984263bc
MD
136 /*
137 * The root filesystem information is compiled in, and we are
138 * booted with instructions to use it.
139 */
140#ifdef ROOTDEVNAME
141 if ((boothowto & RB_DFLTROOT) &&
142 !vfs_mountroot_try(ROOTDEVNAME))
143 return;
144#endif
145 /*
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.
148 */
149 if (boothowto & (RB_DFLTROOT | RB_ASKNAME)) {
150 if (!vfs_mountroot_ask())
151 return;
152 }
153
154 /*
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.
158 */
159 if (boothowto & RB_CDROM) {
160 for (i = 0; cdrom_rootdevnames[i] != NULL; i++) {
161 if (!vfs_mountroot_try(cdrom_rootdevnames[i]))
162 return;
163 }
164 }
165
166 /*
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.
170 */
bc01a404 171 if (!vfs_mountroot_try(kgetenv("vfs.root.mountfrom")))
984263bc
MD
172 return;
173
6b98993e
MD
174 /*
175 * If a vfs set rootdev, try it (XXX VINUM HACK!)
176 */
028066b1 177 if (save_rootdev != NULL) {
6b98993e
MD
178 rootdev = save_rootdev;
179 if (!vfs_mountroot_try(""))
180 return;
181 }
182
984263bc
MD
183 /*
184 * Try values that may have been computed by the machine-dependant
185 * legacy code.
186 */
6b98993e 187 if (rootdevnames[0] && !vfs_mountroot_try(rootdevnames[0]))
984263bc 188 return;
6b98993e 189 if (rootdevnames[1] && !vfs_mountroot_try(rootdevnames[1]))
984263bc
MD
190 return;
191
192 /*
193 * If we have a compiled-in default, and haven't already tried it, try
194 * it now.
195 */
196#ifdef ROOTDEVNAME
197 if (!(boothowto & RB_DFLTROOT))
198 if (!vfs_mountroot_try(ROOTDEVNAME))
199 return;
200#endif
201
202 /*
203 * Everything so far has failed, prompt on the console if we haven't
204 * already tried that.
205 */
206 if (!(boothowto & (RB_DFLTROOT | RB_ASKNAME)) && !vfs_mountroot_ask())
207 return;
208 panic("Root mount failed, startup aborted.");
209}
210
cd29885a
MD
211
212int
213vfs_mountroot_devfs(void)
214{
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;
358c69e9
YT
222 const char *devfs_path, *init_chroot;
223 char *dev_malloced = NULL;
cd29885a 224
358c69e9
YT
225 if ((init_chroot = kgetenv("init_chroot")) != NULL) {
226 size_t l;
227
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";
234 }
cd29885a
MD
235 /*
236 * Lookup the requested path and extract the nch and vnode.
237 */
238 error = nlookup_init_raw(&nd,
358c69e9 239 devfs_path, UIO_SYSSPACE, NLC_FOLLOW,
cd29885a
MD
240 cred, &rootnch);
241
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;
249 }
250 }
251 }
358c69e9
YT
252 if (dev_malloced != NULL)
253 kfree(dev_malloced, M_MOUNT), dev_malloced = NULL;
254 devfs_path = NULL;
cd29885a
MD
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);
259 }
260
261 /*
262 * Extract the locked+refd ncp and cleanup the nd structure
263 */
264 nch = nd.nl_nch;
265 cache_zero(&nd.nl_nch);
266 nlookup_done(&nd);
267
268 /*
269 * now we have the locked ref'd nch and unreferenced vnode.
270 */
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);
276 }
277 cache_unlock(&nch);
278
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);
284 }
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);
290 }
291
292 vfsp = vfsconf_find_by_name("devfs");
cd29885a
MD
293
294 /*
295 * Allocate and initialize the filesystem.
296 */
297 mp = kmalloc(sizeof(struct mount), M_MOUNT, M_ZERO|M_WAITOK);
aac0aabd 298 mount_init(mp);
cd29885a
MD
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;
cd29885a
MD
307 vn_unlock(vp);
308
309 /*
310 * Mount the filesystem.
311 */
312 error = VFS_MOUNT(mp, "/dev", NULL, cred);
313
314 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
315
316 /*
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.
322 *
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.
326 */
327 if (!error) {
328 if (mp->mnt_ncmountpt.ncp == NULL) {
329 /*
330 * allocate, then unlock, but leave the ref intact
331 */
332 cache_allocroot(&mp->mnt_ncmountpt, mp, NULL);
333 cache_unlock(&mp->mnt_ncmountpt);
334 }
335 mp->mnt_ncmounton = nch; /* inherits ref */
336 nch.ncp->nc_flag |= NCF_ISMOUNTPT;
337
338 /* XXX get the root of the fs and cache_setvp(mnt_ncmountpt...) */
cd29885a
MD
339 mountlist_insert(mp, MNTINS_LAST);
340 vn_unlock(vp);
341 //checkdirs(&mp->mnt_ncmounton, &mp->mnt_ncmountpt);
342 error = vfs_allocate_syncvnode(mp);
343 if (error) {
344 devfs_debug(DEVFS_DEBUG_SHOW, "vfs_mountroot_devfs: vfs_allocate_syncvnode failed\n");
345 }
346 vfs_unbusy(mp);
347 error = VFS_START(mp, 0);
348 vrele(vp);
349 } else {
92f61d6d 350 vn_syncer_thr_stop(mp);
cd29885a
MD
351 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops);
352 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops);
353 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops);
354 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops);
355 vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops);
cd29885a
MD
356 mp->mnt_vfc->vfc_refcount--;
357 vfs_unbusy(mp);
358 kfree(mp, M_MOUNT);
359 cache_drop(&nch);
360 vput(vp);
361 devfs_debug(DEVFS_DEBUG_SHOW, "vfs_mountroot_devfs: mount failed\n");
362 }
363
364 devfs_debug(DEVFS_DEBUG_DEBUG, "rootmount_devfs done with error: %d\n", error);
365 return (error);
366}
367
368
984263bc
MD
369/*
370 * Mount (mountfrom) as the root filesystem.
371 */
372static int
6b98993e 373vfs_mountroot_try(const char *mountfrom)
984263bc 374{
da4f706e 375 struct mount *mp;
6b98993e 376 char *vfsname, *devname;
984263bc
MD
377 int error;
378 char patt[32];
6b08c773
SW
379 const char *cp, *ep;
380 char *mf;
69f27b85
FT
381 struct proc *p;
382 struct vnode *vp;
984263bc
MD
383
384 vfsname = NULL;
6b98993e 385 devname = NULL;
984263bc
MD
386 mp = NULL;
387 error = EINVAL;
388
389 if (mountfrom == NULL)
390 return(error); /* don't complain */
391
e43a034f 392 crit_enter();
6ea70f76 393 kprintf("Mounting root from %s\n", mountfrom);
e43a034f 394 crit_exit();
984263bc 395
6b08c773 396 cp = mountfrom;
6b98993e 397 /* parse vfs name and devname */
efda3bd0
MD
398 vfsname = kmalloc(MFSNAMELEN, M_MOUNT, M_WAITOK);
399 devname = kmalloc(MNAMELEN, M_MOUNT, M_WAITOK);
1f1d24d2
AP
400 mf = kmalloc(MFSNAMELEN+MNAMELEN, M_MOUNT, M_WAITOK);
401 for(;;) {
402 for (ep = cp; (*ep != 0) && (*ep != ';'); ep++);
1f1d24d2
AP
403 bzero(vfsname, MFSNAMELEN);
404 bzero(devname, MNAMELEN);
405 bzero(mf, MFSNAMELEN+MNAMELEN);
406 strncpy(mf, cp, MFSNAMELEN+MNAMELEN);
407
408 vfsname[0] = devname[0] = 0;
409 ksprintf(patt, "%%%d[a-z0-9]:%%%ds", MFSNAMELEN, MNAMELEN);
410 if (ksscanf(mf, patt, vfsname, devname) < 1)
411 goto end;
412
413 /* allocate a root mount */
414 error = vfs_rootmountalloc(vfsname,
415 devname[0] != 0 ? devname : ROOTNAME, &mp);
416 if (error != 0) {
417 kprintf("Can't allocate root mount for filesystem '%s': %d\n",
418 vfsname, error);
419 goto end;
420 }
421 mp->mnt_flag |= MNT_ROOTFS;
984263bc 422
77f0808b
MD
423 /* do our best to set rootdev (really just for UFS) */
424 if (strcmp(vfsname, "hammer") != 0 &&
425 strcmp(vfsname, "hammer2") != 0 &&
426 (devname[0] != 0) && setrootbyname(devname)) {
1f1d24d2 427 kprintf("setrootbyname failed\n");
77f0808b 428 }
984263bc 429
1f1d24d2
AP
430 /* If the root device is a type "memory disk", mount RW */
431 if (rootdev != NULL && dev_is_good(rootdev) &&
432 (dev_dflags(rootdev) & D_MEMDISK)) {
433 mp->mnt_flag &= ~MNT_RDONLY;
434 }
984263bc 435
1f1d24d2 436 error = VFS_MOUNT(mp, NULL, NULL, proc0.p_ucred);
984263bc 437
1f1d24d2
AP
438 if (!error)
439 break;
440end:
441 if(*ep == 0)
442 break;
443 cp = ep + 1;
cd29885a
MD
444 }
445
984263bc 446 if (vfsname != NULL)
efda3bd0 447 kfree(vfsname, M_MOUNT);
6b98993e 448 if (devname != NULL)
efda3bd0 449 kfree(devname, M_MOUNT);
1f1d24d2
AP
450 if (mf != NULL)
451 kfree(mf, M_MOUNT);
f2365172 452 if (error == 0) {
984263bc 453 /* register with list of mounted filesystems */
861905fb 454 mountlist_insert(mp, MNTINS_FIRST);
984263bc 455
8a8d5d85 456 /* sanity check system clock against root fs timestamp */
984263bc 457 inittodr(mp->mnt_time);
69f27b85
FT
458
459 /* Get the vnode for '/'. Set p->p_fd->fd_cdir to reference it. */
460 mp = mountlist_boot_getfirst();
461 if (VFS_ROOT(mp, &vp))
462 panic("cannot find root vnode");
463 if (mp->mnt_ncmountpt.ncp == NULL) {
464 cache_allocroot(&mp->mnt_ncmountpt, mp, vp);
465 cache_unlock(&mp->mnt_ncmountpt); /* leave ref intact */
466 }
467 p = curproc;
468 p->p_fd->fd_cdir = vp;
469 vref(p->p_fd->fd_cdir);
470 p->p_fd->fd_rdir = vp;
471 vref(p->p_fd->fd_rdir);
472 vfs_cache_setroot(vp, cache_hold(&mp->mnt_ncmountpt));
473 vn_unlock(vp); /* leave ref intact */
474 cache_copy(&mp->mnt_ncmountpt, &p->p_fd->fd_ncdir);
475 cache_copy(&mp->mnt_ncmountpt, &p->p_fd->fd_nrdir);
476
f9642f56 477 vfs_unbusy(mp);
f2365172
MD
478 if (mp->mnt_syncer == NULL) {
479 error = vfs_allocate_syncvnode(mp);
480 if (error)
481 kprintf("Warning: no syncer vp for root!\n");
482 error = 0;
483 }
69f27b85 484 VFS_START( mp, 0 );
f2365172
MD
485 } else {
486 if (mp != NULL) {
92f61d6d 487 vn_syncer_thr_stop(mp);
f2365172
MD
488 vfs_unbusy(mp);
489 kfree(mp, M_MOUNT);
490 }
491 kprintf("Root mount failed: %d\n", error);
984263bc
MD
492 }
493 return(error);
494}
495
cd29885a 496
3a3826b3 497static void
b28e21ef
SW
498vfs_mountroot_ask_callback(char *name, cdev_t dev, bool is_alias,
499 void *arg __unused)
3a3826b3 500{
b28e21ef
SW
501 if (!is_alias && dev_is_good(dev) && (dev_dflags(dev) & D_DISK))
502 kprintf(" \"%s\" ", name);
3a3826b3
AH
503}
504
cd29885a 505
984263bc
MD
506/*
507 * Spin prompting on the console for a suitable root filesystem
508 */
509static int
510vfs_mountroot_ask(void)
511{
512 char name[128];
cee92776 513 int llimit = 100;
984263bc 514
cee92776
MD
515 kprintf("\nManual root filesystem specification:\n");
516 kprintf(" <fstype>:<device> Specify root (e.g. ufs:da0s1a)\n");
517 kprintf(" ? List valid disk boot devices\n");
518 kprintf(" panic Just panic\n");
519 kprintf(" abort Abort manual input\n");
520 while (llimit--) {
6ea70f76 521 kprintf("\nmountroot> ");
cee92776
MD
522
523 if (getline(name, 128) < 0)
524 break;
525 if (name[0] == 0) {
526 ;
527 } else if (name[0] == '?') {
cd29885a
MD
528 kprintf("Possibly valid devices for root FS:\n");
529 //enumerate all disk devices
3a3826b3 530 devfs_scan_callback(vfs_mountroot_ask_callback, NULL);
6ea70f76 531 kprintf("\n");
984263bc 532 continue;
cee92776
MD
533 } else if (strcmp(name, "panic") == 0) {
534 panic("panic from console");
535 } else if (strcmp(name, "abort") == 0) {
536 break;
537 } else if (vfs_mountroot_try(name) == 0) {
984263bc 538 return(0);
cee92776 539 }
984263bc 540 }
cee92776 541 return(1);
0e9b9130
MD
542}
543
cd29885a 544
cee92776
MD
545static int
546getline(char *cp, int limit)
984263bc
MD
547{
548 char *lp;
ce7866b8 549 int dummy;
984263bc
MD
550 int c;
551
552 lp = cp;
ce7866b8 553 cnpoll(TRUE);
984263bc 554 for (;;) {
ce7866b8 555 c = cncheckc();
7f12a969 556
984263bc 557 switch (c) {
ce7866b8
MD
558 case NOKEY:
559 tsleep(&dummy, 0, "cnpoll", hz / 25);
560 break;
984263bc 561 case -1:
ce7866b8 562 goto done;
984263bc
MD
563 case '\n':
564 case '\r':
7f12a969 565 kprintf("\n");
984263bc 566 *lp++ = '\0';
ce7866b8
MD
567 c = 0;
568 goto done;
984263bc
MD
569 case '\b':
570 case '\177':
571 if (lp > cp) {
7f12a969 572 kprintf("\b \b");
984263bc 573 lp--;
7f12a969
MD
574 } else {
575 kprintf("%c", 7);
984263bc
MD
576 }
577 continue;
578 case '#':
7f12a969 579 kprintf("#");
984263bc
MD
580 lp--;
581 if (lp < cp)
582 lp = cp;
583 continue;
984263bc 584 case 'u' & 037:
a69ad464 585 /* NOTE: '@' no longer processed here, used for H2 */
984263bc 586 lp = cp;
6ea70f76 587 kprintf("%c", '\n');
984263bc
MD
588 continue;
589 default:
cee92776
MD
590 if (lp - cp >= limit - 1) {
591 kprintf("%c", 7);
592 } else {
7f12a969 593 kprintf("%c", c);
cee92776
MD
594 *lp++ = c;
595 }
596 continue;
984263bc
MD
597 }
598 }
ce7866b8
MD
599done:
600 cnpoll(FALSE);
601 return c;
984263bc
MD
602}
603
604/*
b13267a5 605 * Convert a given name to the cdev_t of the disk-like device
984263bc
MD
606 * it refers to.
607 */
b13267a5 608cdev_t
bc01a404 609kgetdiskbyname(const char *name)
e4c9c0c8 610{
b13267a5 611 cdev_t rdev;
984263bc 612
6b98993e
MD
613 /*
614 * Get the base name of the device
615 */
1f2de5d4
MD
616 if (strncmp(name, __SYS_PATH_DEV, sizeof(__SYS_PATH_DEV) - 1) == 0)
617 name += sizeof(__SYS_PATH_DEV) - 1;
6b98993e
MD
618
619 /*
620 * Locate the device
621 */
a7a95662 622 rdev = devfs_find_device_by_name("%s", name);
cd29885a 623 if (rdev == NULL) {
1a8913f1 624 kprintf("no disk named '%s'\n", name);
6b98993e 625 }
6b98993e
MD
626 /*
627 * FOUND DEVICE
628 */
e4c9c0c8 629 return(rdev);
984263bc
MD
630}
631
632/*
633 * Set rootdev to match (name), given that we expect it to
634 * refer to a disk-like device.
635 */
636static int
637setrootbyname(char *name)
638{
b13267a5 639 cdev_t diskdev;
984263bc 640
bc01a404 641 diskdev = kgetdiskbyname(name);
028066b1 642 if (diskdev != NULL) {
984263bc
MD
643 rootdev = diskdev;
644 return (0);
645 }
cd29885a
MD
646 /* set to NULL if kgetdiskbyname() fails so that if the first rootdev is
647 * found by fails to mount and the second one isn't found, mountroot_try
648 * doesn't try again with the first one
649 */
650 rootdev = NULL;
984263bc
MD
651 return (1);
652}
653
654#ifdef DDB
655DB_SHOW_COMMAND(disk, db_getdiskbyname)
656{
b13267a5 657 cdev_t dev;
984263bc
MD
658
659 if (modif[0] == '\0') {
660 db_error("usage: show disk/devicename");
661 return;
662 }
bc01a404 663 dev = kgetdiskbyname(modif);
028066b1 664 if (dev != NULL)
b13267a5 665 db_printf("cdev_t = %p\n", dev);
984263bc
MD
666 else
667 db_printf("No disk device matched.\n");
668}
669#endif
b480ee28
AH
670
671static int
672vfs_sysctl_real_root(SYSCTL_HANDLER_ARGS)
673{
674 char *real_root;
675 size_t len;
676 int error;
677
678 real_root = kgetenv("vfs.root.realroot");
679
680 if (real_root == NULL)
681 real_root = "";
682
683 len = strlen(real_root) + 1;
684
685 error = sysctl_handle_string(oidp, real_root, len, req);
686
687 return error;
688}
689
690SYSCTL_PROC(_vfs, OID_AUTO, real_root,
691 CTLTYPE_STRING | CTLFLAG_RD, 0, 0, vfs_sysctl_real_root,
692 "A", "Real root mount string");