| Commit | Line | Data |
|---|---|---|
| 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> | |
| 335dda38 | 58 | #include <sys/device.h> |
| cd29885a | 59 | #include <sys/disk.h> |
| 21739618 | 60 | #include <sys/namecache.h> |
| 1f2de5d4 | 61 | #include <sys/paths.h> |
| e43a034f | 62 | #include <sys/thread2.h> |
| cd29885a | 63 | #include <sys/nlookup.h> |
| 2c1e28dd | 64 | #include <sys/devfs.h> |
| b480ee28 | 65 | #include <sys/sysctl.h> |
| 984263bc MD |
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; | |
| 28623bf9 | 77 | struct nchandle rootnch; |
| 984263bc MD |
78 | |
| 79 | /* | |
| 6fcb48f2 | 80 | * The root specifiers we will try if RB_CDROM is specified. Note that |
| aec8eea4 MD |
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. | |
| 984263bc MD |
83 | */ |
| 84 | static char *cdrom_rootdevnames[] = { | |
| d81eda2e MD |
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 */ | |
| 984263bc MD |
91 | NULL |
| 92 | }; | |
| 93 | ||
| cd29885a | 94 | int vfs_mountroot_devfs(void); |
| 984263bc | 95 | static void vfs_mountroot(void *junk); |
| 6b98993e | 96 | static int vfs_mountroot_try(const char *mountfrom); |
| 984263bc | 97 | static int vfs_mountroot_ask(void); |
| cee92776 | 98 | static int getline(char *cp, int limit); |
| 984263bc MD |
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 | { | |
| b13267a5 | 112 | cdev_t save_rootdev = rootdev; |
| a2b57962 MD |
113 | int i; |
| 114 | int dummy; | |
| 984263bc | 115 | |
| cd29885a MD |
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. | |
| 1c0fe2ad MD |
120 | * |
| 121 | * Messages can fly around here so get good synchronization | |
| 122 | * coverage. | |
| a2b57962 MD |
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. | |
| cd29885a | 127 | */ |
| 2a32d680 | 128 | sync_devs(); |
| a2b57962 MD |
129 | tsleep(&dummy, 0, "syncer", hz*2); |
| 130 | ||
| cd29885a | 131 | |
| 984263bc MD |
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 | */ | |
| bc01a404 | 167 | if (!vfs_mountroot_try(kgetenv("vfs.root.mountfrom"))) |
| 984263bc MD |
168 | return; |
| 169 | ||
| 6b98993e MD |
170 | /* |
| 171 | * If a vfs set rootdev, try it (XXX VINUM HACK!) | |
| 172 | */ | |
| 028066b1 | 173 | if (save_rootdev != NULL) { |
| 6b98993e MD |
174 | rootdev = save_rootdev; |
| 175 | if (!vfs_mountroot_try("")) | |
| 176 | return; | |
| 177 | } | |
| 178 | ||
| 984263bc MD |
179 | /* |
| 180 | * Try values that may have been computed by the machine-dependant | |
| 181 | * legacy code. | |
| 182 | */ | |
| 6b98993e | 183 | if (rootdevnames[0] && !vfs_mountroot_try(rootdevnames[0])) |
| 984263bc | 184 | return; |
| 6b98993e | 185 | if (rootdevnames[1] && !vfs_mountroot_try(rootdevnames[1])) |
| 984263bc MD |
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 | ||
| cd29885a MD |
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; | |
| 358c69e9 YT |
218 | const char *devfs_path, *init_chroot; |
| 219 | char *dev_malloced = NULL; | |
| cd29885a | 220 | |
| 358c69e9 YT |
221 | if ((init_chroot = kgetenv("init_chroot")) != NULL) { |
| 222 | size_t l; | |
| 223 | ||
| 224 | l = strlen(init_chroot) + sizeof("/dev"); | |
| 225 | dev_malloced = kmalloc(l, M_MOUNT, M_WAITOK); | |
| 226 | ksnprintf(dev_malloced, l, "%s/dev", init_chroot); | |
| 227 | devfs_path = dev_malloced; | |
| 228 | } else { | |
| 229 | devfs_path = "/dev"; | |
| 230 | } | |
| cd29885a MD |
231 | /* |
| 232 | * Lookup the requested path and extract the nch and vnode. | |
| 233 | */ | |
| 234 | error = nlookup_init_raw(&nd, | |
| 358c69e9 | 235 | devfs_path, UIO_SYSSPACE, NLC_FOLLOW, |
| cd29885a MD |
236 | cred, &rootnch); |
| 237 | ||
| 238 | if (error == 0) { | |
| 239 | devfs_debug(DEVFS_DEBUG_DEBUG, "vfs_mountroot_devfs: nlookup_init is ok...\n"); | |
| 240 | if ((error = nlookup(&nd)) == 0) { | |
| 241 | devfs_debug(DEVFS_DEBUG_DEBUG, "vfs_mountroot_devfs: nlookup is ok...\n"); | |
| 242 | if (nd.nl_nch.ncp->nc_vp == NULL) { | |
| 243 | devfs_debug(DEVFS_DEBUG_SHOW, "vfs_mountroot_devfs: nlookup: simply not found\n"); | |
| 244 | error = ENOENT; | |
| 245 | } | |
| 246 | } | |
| 247 | } | |
| 358c69e9 YT |
248 | if (dev_malloced != NULL) |
| 249 | kfree(dev_malloced, M_MOUNT), dev_malloced = NULL; | |
| 250 | devfs_path = NULL; | |
| cd29885a MD |
251 | if (error) { |
| 252 | nlookup_done(&nd); | |
| 253 | devfs_debug(DEVFS_DEBUG_SHOW, "vfs_mountroot_devfs: nlookup failed, error: %d\n", error); | |
| 254 | return (error); | |
| 255 | } | |
| 256 | ||
| 257 | /* | |
| 258 | * Extract the locked+refd ncp and cleanup the nd structure | |
| 259 | */ | |
| 260 | nch = nd.nl_nch; | |
| 261 | cache_zero(&nd.nl_nch); | |
| 262 | nlookup_done(&nd); | |
| 263 | ||
| 264 | /* | |
| 265 | * now we have the locked ref'd nch and unreferenced vnode. | |
| 266 | */ | |
| 267 | vp = nch.ncp->nc_vp; | |
| 268 | if ((error = vget(vp, LK_EXCLUSIVE)) != 0) { | |
| 269 | cache_put(&nch); | |
| 270 | devfs_debug(DEVFS_DEBUG_SHOW, "vfs_mountroot_devfs: vget failed\n"); | |
| 271 | return (error); | |
| 272 | } | |
| 273 | cache_unlock(&nch); | |
| 274 | ||
| 275 | if ((error = vinvalbuf(vp, V_SAVE, 0, 0)) != 0) { | |
| 276 | cache_drop(&nch); | |
| 277 | vput(vp); | |
| 278 | devfs_debug(DEVFS_DEBUG_SHOW, "vfs_mountroot_devfs: vinvalbuf failed\n"); | |
| 279 | return (error); | |
| 280 | } | |
| 281 | if (vp->v_type != VDIR) { | |
| 282 | cache_drop(&nch); | |
| 283 | vput(vp); | |
| 284 | devfs_debug(DEVFS_DEBUG_SHOW, "vfs_mountroot_devfs: vp is not VDIR\n"); | |
| 285 | return (ENOTDIR); | |
| 286 | } | |
| 287 | ||
| 288 | vfsp = vfsconf_find_by_name("devfs"); | |
| 2247fe02 | 289 | vsetflags(vp, VMOUNT); |
| cd29885a MD |
290 | |
| 291 | /* | |
| 292 | * Allocate and initialize the filesystem. | |
| 293 | */ | |
| 294 | mp = kmalloc(sizeof(struct mount), M_MOUNT, M_ZERO|M_WAITOK); | |
| aac0aabd | 295 | mount_init(mp); |
| cd29885a MD |
296 | vfs_busy(mp, LK_NOWAIT); |
| 297 | mp->mnt_op = vfsp->vfc_vfsops; | |
| 298 | mp->mnt_vfc = vfsp; | |
| 299 | vfsp->vfc_refcount++; | |
| 300 | mp->mnt_stat.f_type = vfsp->vfc_typenum; | |
| 301 | mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK; | |
| 302 | strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN); | |
| 303 | mp->mnt_stat.f_owner = cred->cr_uid; | |
| cd29885a MD |
304 | vn_unlock(vp); |
| 305 | ||
| 306 | /* | |
| 307 | * Mount the filesystem. | |
| 308 | */ | |
| 309 | error = VFS_MOUNT(mp, "/dev", NULL, cred); | |
| 310 | ||
| 311 | vn_lock(vp, LK_EXCLUSIVE | LK_RETRY); | |
| 312 | ||
| 313 | /* | |
| 314 | * Put the new filesystem on the mount list after root. The mount | |
| 315 | * point gets its own mnt_ncmountpt (unless the VFS already set one | |
| 316 | * up) which represents the root of the mount. The lookup code | |
| 317 | * detects the mount point going forward and checks the root of | |
| 318 | * the mount going backwards. | |
| 319 | * | |
| 320 | * It is not necessary to invalidate or purge the vnode underneath | |
| 321 | * because elements under the mount will be given their own glue | |
| 322 | * namecache record. | |
| 323 | */ | |
| 324 | if (!error) { | |
| 325 | if (mp->mnt_ncmountpt.ncp == NULL) { | |
| 326 | /* | |
| 327 | * allocate, then unlock, but leave the ref intact | |
| 328 | */ | |
| 329 | cache_allocroot(&mp->mnt_ncmountpt, mp, NULL); | |
| 330 | cache_unlock(&mp->mnt_ncmountpt); | |
| 331 | } | |
| 332 | mp->mnt_ncmounton = nch; /* inherits ref */ | |
| 333 | nch.ncp->nc_flag |= NCF_ISMOUNTPT; | |
| 334 | ||
| 335 | /* XXX get the root of the fs and cache_setvp(mnt_ncmountpt...) */ | |
| 2247fe02 | 336 | vclrflags(vp, VMOUNT); |
| cd29885a MD |
337 | mountlist_insert(mp, MNTINS_LAST); |
| 338 | vn_unlock(vp); | |
| 339 | //checkdirs(&mp->mnt_ncmounton, &mp->mnt_ncmountpt); | |
| 340 | error = vfs_allocate_syncvnode(mp); | |
| 341 | if (error) { | |
| 342 | devfs_debug(DEVFS_DEBUG_SHOW, "vfs_mountroot_devfs: vfs_allocate_syncvnode failed\n"); | |
| 343 | } | |
| 344 | vfs_unbusy(mp); | |
| 345 | error = VFS_START(mp, 0); | |
| 346 | vrele(vp); | |
| 347 | } else { | |
| 348 | vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_coherency_ops); | |
| 349 | vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_journal_ops); | |
| 350 | vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_norm_ops); | |
| 351 | vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_spec_ops); | |
| 352 | vfs_rm_vnodeops(mp, NULL, &mp->mnt_vn_fifo_ops); | |
| 2247fe02 | 353 | vclrflags(vp, VMOUNT); |
| cd29885a MD |
354 | mp->mnt_vfc->vfc_refcount--; |
| 355 | vfs_unbusy(mp); | |
| 356 | kfree(mp, M_MOUNT); | |
| 357 | cache_drop(&nch); | |
| 358 | vput(vp); | |
| 359 | devfs_debug(DEVFS_DEBUG_SHOW, "vfs_mountroot_devfs: mount failed\n"); | |
| 360 | } | |
| 361 | ||
| 362 | devfs_debug(DEVFS_DEBUG_DEBUG, "rootmount_devfs done with error: %d\n", error); | |
| 363 | return (error); | |
| 364 | } | |
| 365 | ||
| 366 | ||
| 984263bc MD |
367 | /* |
| 368 | * Mount (mountfrom) as the root filesystem. | |
| 369 | */ | |
| 370 | static int | |
| 6b98993e | 371 | vfs_mountroot_try(const char *mountfrom) |
| 984263bc | 372 | { |
| da4f706e | 373 | struct mount *mp; |
| 6b98993e | 374 | char *vfsname, *devname; |
| 984263bc MD |
375 | int error; |
| 376 | char patt[32]; | |
| 6b08c773 SW |
377 | const char *cp, *ep; |
| 378 | char *mf; | |
| 69f27b85 FT |
379 | struct proc *p; |
| 380 | struct vnode *vp; | |
| 984263bc MD |
381 | |
| 382 | vfsname = NULL; | |
| 6b98993e | 383 | devname = NULL; |
| 984263bc MD |
384 | mp = NULL; |
| 385 | error = EINVAL; | |
| 386 | ||
| 387 | if (mountfrom == NULL) | |
| 388 | return(error); /* don't complain */ | |
| 389 | ||
| e43a034f | 390 | crit_enter(); |
| 6ea70f76 | 391 | kprintf("Mounting root from %s\n", mountfrom); |
| e43a034f | 392 | crit_exit(); |
| 984263bc | 393 | |
| 6b08c773 | 394 | cp = mountfrom; |
| 6b98993e | 395 | /* parse vfs name and devname */ |
| efda3bd0 MD |
396 | vfsname = kmalloc(MFSNAMELEN, M_MOUNT, M_WAITOK); |
| 397 | devname = kmalloc(MNAMELEN, M_MOUNT, M_WAITOK); | |
| 1f1d24d2 AP |
398 | mf = kmalloc(MFSNAMELEN+MNAMELEN, M_MOUNT, M_WAITOK); |
| 399 | for(;;) { | |
| 400 | for (ep = cp; (*ep != 0) && (*ep != ';'); ep++); | |
| 1f1d24d2 AP |
401 | bzero(vfsname, MFSNAMELEN); |
| 402 | bzero(devname, MNAMELEN); | |
| 403 | bzero(mf, MFSNAMELEN+MNAMELEN); | |
| 404 | strncpy(mf, cp, MFSNAMELEN+MNAMELEN); | |
| 405 | ||
| 406 | vfsname[0] = devname[0] = 0; | |
| 407 | ksprintf(patt, "%%%d[a-z0-9]:%%%ds", MFSNAMELEN, MNAMELEN); | |
| 408 | if (ksscanf(mf, patt, vfsname, devname) < 1) | |
| 409 | goto end; | |
| 410 | ||
| 411 | /* allocate a root mount */ | |
| 412 | error = vfs_rootmountalloc(vfsname, | |
| 413 | devname[0] != 0 ? devname : ROOTNAME, &mp); | |
| 414 | if (error != 0) { | |
| 415 | kprintf("Can't allocate root mount for filesystem '%s': %d\n", | |
| 416 | vfsname, error); | |
| 417 | goto end; | |
| 418 | } | |
| 419 | mp->mnt_flag |= MNT_ROOTFS; | |
| 984263bc | 420 | |
| 1f1d24d2 | 421 | /* do our best to set rootdev */ |
| 104cb849 MN |
422 | if ((strcmp(vfsname, "hammer") != 0) && (devname[0] != 0) && |
| 423 | setrootbyname(devname)) | |
| 1f1d24d2 | 424 | kprintf("setrootbyname failed\n"); |
| 984263bc | 425 | |
| 1f1d24d2 AP |
426 | /* If the root device is a type "memory disk", mount RW */ |
| 427 | if (rootdev != NULL && dev_is_good(rootdev) && | |
| 428 | (dev_dflags(rootdev) & D_MEMDISK)) { | |
| 429 | mp->mnt_flag &= ~MNT_RDONLY; | |
| 430 | } | |
| 984263bc | 431 | |
| 1f1d24d2 | 432 | error = VFS_MOUNT(mp, NULL, NULL, proc0.p_ucred); |
| 984263bc | 433 | |
| 1f1d24d2 AP |
434 | if (!error) |
| 435 | break; | |
| 436 | end: | |
| 437 | if(*ep == 0) | |
| 438 | break; | |
| 439 | cp = ep + 1; | |
| cd29885a MD |
440 | } |
| 441 | ||
| 984263bc | 442 | if (vfsname != NULL) |
| efda3bd0 | 443 | kfree(vfsname, M_MOUNT); |
| 6b98993e | 444 | if (devname != NULL) |
| efda3bd0 | 445 | kfree(devname, M_MOUNT); |
| 1f1d24d2 AP |
446 | if (mf != NULL) |
| 447 | kfree(mf, M_MOUNT); | |
| f2365172 | 448 | if (error == 0) { |
| 984263bc | 449 | /* register with list of mounted filesystems */ |
| 861905fb | 450 | mountlist_insert(mp, MNTINS_FIRST); |
| 984263bc | 451 | |
| 8a8d5d85 | 452 | /* sanity check system clock against root fs timestamp */ |
| 984263bc | 453 | inittodr(mp->mnt_time); |
| 69f27b85 FT |
454 | |
| 455 | /* Get the vnode for '/'. Set p->p_fd->fd_cdir to reference it. */ | |
| 456 | mp = mountlist_boot_getfirst(); | |
| 457 | if (VFS_ROOT(mp, &vp)) | |
| 458 | panic("cannot find root vnode"); | |
| 459 | if (mp->mnt_ncmountpt.ncp == NULL) { | |
| 460 | cache_allocroot(&mp->mnt_ncmountpt, mp, vp); | |
| 461 | cache_unlock(&mp->mnt_ncmountpt); /* leave ref intact */ | |
| 462 | } | |
| 463 | p = curproc; | |
| 464 | p->p_fd->fd_cdir = vp; | |
| 465 | vref(p->p_fd->fd_cdir); | |
| 466 | p->p_fd->fd_rdir = vp; | |
| 467 | vref(p->p_fd->fd_rdir); | |
| 468 | vfs_cache_setroot(vp, cache_hold(&mp->mnt_ncmountpt)); | |
| 469 | vn_unlock(vp); /* leave ref intact */ | |
| 470 | cache_copy(&mp->mnt_ncmountpt, &p->p_fd->fd_ncdir); | |
| 471 | cache_copy(&mp->mnt_ncmountpt, &p->p_fd->fd_nrdir); | |
| 472 | ||
| f9642f56 | 473 | vfs_unbusy(mp); |
| f2365172 MD |
474 | if (mp->mnt_syncer == NULL) { |
| 475 | error = vfs_allocate_syncvnode(mp); | |
| 476 | if (error) | |
| 477 | kprintf("Warning: no syncer vp for root!\n"); | |
| 478 | error = 0; | |
| 479 | } | |
| 69f27b85 | 480 | VFS_START( mp, 0 ); |
| f2365172 MD |
481 | } else { |
| 482 | if (mp != NULL) { | |
| 483 | vfs_unbusy(mp); | |
| 484 | kfree(mp, M_MOUNT); | |
| 485 | } | |
| 486 | kprintf("Root mount failed: %d\n", error); | |
| 984263bc MD |
487 | } |
| 488 | return(error); | |
| 489 | } | |
| 490 | ||
| cd29885a | 491 | |
| 3a3826b3 | 492 | static void |
| b28e21ef SW |
493 | vfs_mountroot_ask_callback(char *name, cdev_t dev, bool is_alias, |
| 494 | void *arg __unused) | |
| 3a3826b3 | 495 | { |
| b28e21ef SW |
496 | if (!is_alias && dev_is_good(dev) && (dev_dflags(dev) & D_DISK)) |
| 497 | kprintf(" \"%s\" ", name); | |
| 3a3826b3 AH |
498 | } |
| 499 | ||
| cd29885a | 500 | |
| 984263bc MD |
501 | /* |
| 502 | * Spin prompting on the console for a suitable root filesystem | |
| 503 | */ | |
| 504 | static int | |
| 505 | vfs_mountroot_ask(void) | |
| 506 | { | |
| 507 | char name[128]; | |
| cee92776 | 508 | int llimit = 100; |
| 984263bc | 509 | |
| cee92776 MD |
510 | kprintf("\nManual root filesystem specification:\n"); |
| 511 | kprintf(" <fstype>:<device> Specify root (e.g. ufs:da0s1a)\n"); | |
| 512 | kprintf(" ? List valid disk boot devices\n"); | |
| 513 | kprintf(" panic Just panic\n"); | |
| 514 | kprintf(" abort Abort manual input\n"); | |
| 515 | while (llimit--) { | |
| 6ea70f76 | 516 | kprintf("\nmountroot> "); |
| cee92776 MD |
517 | |
| 518 | if (getline(name, 128) < 0) | |
| 519 | break; | |
| 520 | if (name[0] == 0) { | |
| 521 | ; | |
| 522 | } else if (name[0] == '?') { | |
| cd29885a MD |
523 | kprintf("Possibly valid devices for root FS:\n"); |
| 524 | //enumerate all disk devices | |
| 3a3826b3 | 525 | devfs_scan_callback(vfs_mountroot_ask_callback, NULL); |
| 6ea70f76 | 526 | kprintf("\n"); |
| 984263bc | 527 | continue; |
| cee92776 MD |
528 | } else if (strcmp(name, "panic") == 0) { |
| 529 | panic("panic from console"); | |
| 530 | } else if (strcmp(name, "abort") == 0) { | |
| 531 | break; | |
| 532 | } else if (vfs_mountroot_try(name) == 0) { | |
| 984263bc | 533 | return(0); |
| cee92776 | 534 | } |
| 984263bc | 535 | } |
| cee92776 | 536 | return(1); |
| 0e9b9130 MD |
537 | } |
| 538 | ||
| cd29885a | 539 | |
| cee92776 MD |
540 | static int |
| 541 | getline(char *cp, int limit) | |
| 984263bc MD |
542 | { |
| 543 | char *lp; | |
| 544 | int c; | |
| 545 | ||
| 546 | lp = cp; | |
| 547 | for (;;) { | |
| 7f12a969 MD |
548 | c = cngetc(); |
| 549 | ||
| 984263bc MD |
550 | switch (c) { |
| 551 | case -1: | |
| cee92776 | 552 | return(-1); |
| 984263bc MD |
553 | case '\n': |
| 554 | case '\r': | |
| 7f12a969 | 555 | kprintf("\n"); |
| 984263bc | 556 | *lp++ = '\0'; |
| cee92776 | 557 | return(0); |
| 984263bc MD |
558 | case '\b': |
| 559 | case '\177': | |
| 560 | if (lp > cp) { | |
| 7f12a969 | 561 | kprintf("\b \b"); |
| 984263bc | 562 | lp--; |
| 7f12a969 MD |
563 | } else { |
| 564 | kprintf("%c", 7); | |
| 984263bc MD |
565 | } |
| 566 | continue; | |
| 567 | case '#': | |
| 7f12a969 | 568 | kprintf("#"); |
| 984263bc MD |
569 | lp--; |
| 570 | if (lp < cp) | |
| 571 | lp = cp; | |
| 572 | continue; | |
| 573 | case '@': | |
| 574 | case 'u' & 037: | |
| 575 | lp = cp; | |
| 6ea70f76 | 576 | kprintf("%c", '\n'); |
| 984263bc MD |
577 | continue; |
| 578 | default: | |
| cee92776 MD |
579 | if (lp - cp >= limit - 1) { |
| 580 | kprintf("%c", 7); | |
| 581 | } else { | |
| 7f12a969 | 582 | kprintf("%c", c); |
| cee92776 MD |
583 | *lp++ = c; |
| 584 | } | |
| 585 | continue; | |
| 984263bc MD |
586 | } |
| 587 | } | |
| 588 | } | |
| 589 | ||
| 590 | /* | |
| b13267a5 | 591 | * Convert a given name to the cdev_t of the disk-like device |
| 984263bc MD |
592 | * it refers to. |
| 593 | */ | |
| b13267a5 | 594 | cdev_t |
| bc01a404 | 595 | kgetdiskbyname(const char *name) |
| e4c9c0c8 | 596 | { |
| b13267a5 | 597 | cdev_t rdev; |
| 984263bc | 598 | |
| 6b98993e MD |
599 | /* |
| 600 | * Get the base name of the device | |
| 601 | */ | |
| 1f2de5d4 MD |
602 | if (strncmp(name, __SYS_PATH_DEV, sizeof(__SYS_PATH_DEV) - 1) == 0) |
| 603 | name += sizeof(__SYS_PATH_DEV) - 1; | |
| 6b98993e MD |
604 | |
| 605 | /* | |
| 606 | * Locate the device | |
| 607 | */ | |
| a7a95662 | 608 | rdev = devfs_find_device_by_name("%s", name); |
| cd29885a | 609 | if (rdev == NULL) { |
| 1a8913f1 | 610 | kprintf("no disk named '%s'\n", name); |
| 6b98993e | 611 | } |
| 6b98993e MD |
612 | /* |
| 613 | * FOUND DEVICE | |
| 614 | */ | |
| e4c9c0c8 | 615 | return(rdev); |
| 984263bc MD |
616 | } |
| 617 | ||
| 618 | /* | |
| 619 | * Set rootdev to match (name), given that we expect it to | |
| 620 | * refer to a disk-like device. | |
| 621 | */ | |
| 622 | static int | |
| 623 | setrootbyname(char *name) | |
| 624 | { | |
| b13267a5 | 625 | cdev_t diskdev; |
| 984263bc | 626 | |
| bc01a404 | 627 | diskdev = kgetdiskbyname(name); |
| 028066b1 | 628 | if (diskdev != NULL) { |
| 984263bc MD |
629 | rootdev = diskdev; |
| 630 | return (0); | |
| 631 | } | |
| cd29885a MD |
632 | /* set to NULL if kgetdiskbyname() fails so that if the first rootdev is |
| 633 | * found by fails to mount and the second one isn't found, mountroot_try | |
| 634 | * doesn't try again with the first one | |
| 635 | */ | |
| 636 | rootdev = NULL; | |
| 984263bc MD |
637 | return (1); |
| 638 | } | |
| 639 | ||
| 640 | #ifdef DDB | |
| 641 | DB_SHOW_COMMAND(disk, db_getdiskbyname) | |
| 642 | { | |
| b13267a5 | 643 | cdev_t dev; |
| 984263bc MD |
644 | |
| 645 | if (modif[0] == '\0') { | |
| 646 | db_error("usage: show disk/devicename"); | |
| 647 | return; | |
| 648 | } | |
| bc01a404 | 649 | dev = kgetdiskbyname(modif); |
| 028066b1 | 650 | if (dev != NULL) |
| b13267a5 | 651 | db_printf("cdev_t = %p\n", dev); |
| 984263bc MD |
652 | else |
| 653 | db_printf("No disk device matched.\n"); | |
| 654 | } | |
| 655 | #endif | |
| b480ee28 AH |
656 | |
| 657 | static int | |
| 658 | vfs_sysctl_real_root(SYSCTL_HANDLER_ARGS) | |
| 659 | { | |
| 660 | char *real_root; | |
| 661 | size_t len; | |
| 662 | int error; | |
| 663 | ||
| 664 | real_root = kgetenv("vfs.root.realroot"); | |
| 665 | ||
| 666 | if (real_root == NULL) | |
| 667 | real_root = ""; | |
| 668 | ||
| 669 | len = strlen(real_root) + 1; | |
| 670 | ||
| 671 | error = sysctl_handle_string(oidp, real_root, len, req); | |
| 672 | ||
| 673 | return error; | |
| 674 | } | |
| 675 | ||
| 676 | SYSCTL_PROC(_vfs, OID_AUTO, real_root, | |
| 677 | CTLTYPE_STRING | CTLFLAG_RD, 0, 0, vfs_sysctl_real_root, | |
| 678 | "A", "Real root mount string"); |