| 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); | |
| 5a82ae89 SW |
105 | |
| 106 | static int wakedelay = 2; /* delay before mounting root in seconds */ | |
| 107 | TUNABLE_INT("vfs.root.wakedelay", &wakedelay); | |
| 108 | ||
| 984263bc MD |
109 | /* |
| 110 | * Find and mount the root filesystem | |
| 111 | */ | |
| 112 | static void | |
| 113 | vfs_mountroot(void *junk) | |
| 114 | { | |
| b13267a5 | 115 | cdev_t save_rootdev = rootdev; |
| a2b57962 MD |
116 | int i; |
| 117 | int dummy; | |
| 984263bc | 118 | |
| cd29885a MD |
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. | |
| 1c0fe2ad MD |
123 | * |
| 124 | * Messages can fly around here so get good synchronization | |
| 125 | * coverage. | |
| a2b57962 | 126 | * |
| 5a82ae89 | 127 | * XXX - Delay some more (default: 2s) to help drivers which pickup |
| a2b57962 MD |
128 | * devices asynchronously and are not caught by CAM's initial |
| 129 | * probe. | |
| cd29885a | 130 | */ |
| 2a32d680 | 131 | sync_devs(); |
| 5a82ae89 | 132 | tsleep(&dummy, 0, "syncer", hz * wakedelay); |
| a2b57962 | 133 | |
| cd29885a | 134 | |
| 984263bc MD |
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 | */ | |
| bc01a404 | 170 | if (!vfs_mountroot_try(kgetenv("vfs.root.mountfrom"))) |
| 984263bc MD |
171 | return; |
| 172 | ||
| 6b98993e MD |
173 | /* |
| 174 | * If a vfs set rootdev, try it (XXX VINUM HACK!) | |
| 175 | */ | |
| 028066b1 | 176 | if (save_rootdev != NULL) { |
| 6b98993e MD |
177 | rootdev = save_rootdev; |
| 178 | if (!vfs_mountroot_try("")) | |
| 179 | return; | |
| 180 | } | |
| 181 | ||
| 984263bc MD |
182 | /* |
| 183 | * Try values that may have been computed by the machine-dependant | |
| 184 | * legacy code. | |
| 185 | */ | |
| 6b98993e | 186 | if (rootdevnames[0] && !vfs_mountroot_try(rootdevnames[0])) |
| 984263bc | 187 | return; |
| 6b98993e | 188 | if (rootdevnames[1] && !vfs_mountroot_try(rootdevnames[1])) |
| 984263bc MD |
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 | ||
| cd29885a MD |
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; | |
| 358c69e9 YT |
221 | const char *devfs_path, *init_chroot; |
| 222 | char *dev_malloced = NULL; | |
| cd29885a | 223 | |
| 358c69e9 YT |
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 | } | |
| cd29885a MD |
234 | /* |
| 235 | * Lookup the requested path and extract the nch and vnode. | |
| 236 | */ | |
| 237 | error = nlookup_init_raw(&nd, | |
| 358c69e9 | 238 | devfs_path, UIO_SYSSPACE, NLC_FOLLOW, |
| cd29885a MD |
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 | } | |
| 358c69e9 YT |
251 | if (dev_malloced != NULL) |
| 252 | kfree(dev_malloced, M_MOUNT), dev_malloced = NULL; | |
| 253 | devfs_path = NULL; | |
| cd29885a MD |
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"); | |
| 2247fe02 | 292 | vsetflags(vp, VMOUNT); |
| 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...) */ | |
| 2247fe02 | 339 | vclrflags(vp, VMOUNT); |
| cd29885a MD |
340 | mountlist_insert(mp, MNTINS_LAST); |
| 341 | vn_unlock(vp); | |
| 342 | //checkdirs(&mp->mnt_ncmounton, &mp->mnt_ncmountpt); | |
| 343 | error = vfs_allocate_syncvnode(mp); | |
| 344 | if (error) { | |
| 345 | devfs_debug(DEVFS_DEBUG_SHOW, "vfs_mountroot_devfs: vfs_allocate_syncvnode failed\n"); | |
| 346 | } | |
| 347 | vfs_unbusy(mp); | |
| 348 | error = VFS_START(mp, 0); | |
| 349 | vrele(vp); | |
| 350 | } else { | |
| 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); | |
| 2247fe02 | 356 | vclrflags(vp, VMOUNT); |
| cd29885a MD |
357 | mp->mnt_vfc->vfc_refcount--; |
| 358 | vfs_unbusy(mp); | |
| 359 | kfree(mp, M_MOUNT); | |
| 360 | cache_drop(&nch); | |
| 361 | vput(vp); | |
| 362 | devfs_debug(DEVFS_DEBUG_SHOW, "vfs_mountroot_devfs: mount failed\n"); | |
| 363 | } | |
| 364 | ||
| 365 | devfs_debug(DEVFS_DEBUG_DEBUG, "rootmount_devfs done with error: %d\n", error); | |
| 366 | return (error); | |
| 367 | } | |
| 368 | ||
| 369 | ||
| 984263bc MD |
370 | /* |
| 371 | * Mount (mountfrom) as the root filesystem. | |
| 372 | */ | |
| 373 | static int | |
| 6b98993e | 374 | vfs_mountroot_try(const char *mountfrom) |
| 984263bc | 375 | { |
| da4f706e | 376 | struct mount *mp; |
| 6b98993e | 377 | char *vfsname, *devname; |
| 984263bc MD |
378 | int error; |
| 379 | char patt[32]; | |
| 6b08c773 SW |
380 | const char *cp, *ep; |
| 381 | char *mf; | |
| 69f27b85 FT |
382 | struct proc *p; |
| 383 | struct vnode *vp; | |
| 984263bc MD |
384 | |
| 385 | vfsname = NULL; | |
| 6b98993e | 386 | devname = NULL; |
| 984263bc MD |
387 | mp = NULL; |
| 388 | error = EINVAL; | |
| 389 | ||
| 390 | if (mountfrom == NULL) | |
| 391 | return(error); /* don't complain */ | |
| 392 | ||
| e43a034f | 393 | crit_enter(); |
| 6ea70f76 | 394 | kprintf("Mounting root from %s\n", mountfrom); |
| e43a034f | 395 | crit_exit(); |
| 984263bc | 396 | |
| 6b08c773 | 397 | cp = mountfrom; |
| 6b98993e | 398 | /* parse vfs name and devname */ |
| efda3bd0 MD |
399 | vfsname = kmalloc(MFSNAMELEN, M_MOUNT, M_WAITOK); |
| 400 | devname = kmalloc(MNAMELEN, M_MOUNT, M_WAITOK); | |
| 1f1d24d2 AP |
401 | mf = kmalloc(MFSNAMELEN+MNAMELEN, M_MOUNT, M_WAITOK); |
| 402 | for(;;) { | |
| 403 | for (ep = cp; (*ep != 0) && (*ep != ';'); ep++); | |
| 1f1d24d2 AP |
404 | bzero(vfsname, MFSNAMELEN); |
| 405 | bzero(devname, MNAMELEN); | |
| 406 | bzero(mf, MFSNAMELEN+MNAMELEN); | |
| 407 | strncpy(mf, cp, MFSNAMELEN+MNAMELEN); | |
| 408 | ||
| 409 | vfsname[0] = devname[0] = 0; | |
| 410 | ksprintf(patt, "%%%d[a-z0-9]:%%%ds", MFSNAMELEN, MNAMELEN); | |
| 411 | if (ksscanf(mf, patt, vfsname, devname) < 1) | |
| 412 | goto end; | |
| 413 | ||
| 414 | /* allocate a root mount */ | |
| 415 | error = vfs_rootmountalloc(vfsname, | |
| 416 | devname[0] != 0 ? devname : ROOTNAME, &mp); | |
| 417 | if (error != 0) { | |
| 418 | kprintf("Can't allocate root mount for filesystem '%s': %d\n", | |
| 419 | vfsname, error); | |
| 420 | goto end; | |
| 421 | } | |
| 422 | mp->mnt_flag |= MNT_ROOTFS; | |
| 984263bc | 423 | |
| 1f1d24d2 | 424 | /* do our best to set rootdev */ |
| 104cb849 MN |
425 | if ((strcmp(vfsname, "hammer") != 0) && (devname[0] != 0) && |
| 426 | setrootbyname(devname)) | |
| 1f1d24d2 | 427 | kprintf("setrootbyname failed\n"); |
| 984263bc | 428 | |
| 1f1d24d2 AP |
429 | /* If the root device is a type "memory disk", mount RW */ |
| 430 | if (rootdev != NULL && dev_is_good(rootdev) && | |
| 431 | (dev_dflags(rootdev) & D_MEMDISK)) { | |
| 432 | mp->mnt_flag &= ~MNT_RDONLY; | |
| 433 | } | |
| 984263bc | 434 | |
| 1f1d24d2 | 435 | error = VFS_MOUNT(mp, NULL, NULL, proc0.p_ucred); |
| 984263bc | 436 | |
| 1f1d24d2 AP |
437 | if (!error) |
| 438 | break; | |
| 439 | end: | |
| 440 | if(*ep == 0) | |
| 441 | break; | |
| 442 | cp = ep + 1; | |
| cd29885a MD |
443 | } |
| 444 | ||
| 984263bc | 445 | if (vfsname != NULL) |
| efda3bd0 | 446 | kfree(vfsname, M_MOUNT); |
| 6b98993e | 447 | if (devname != NULL) |
| efda3bd0 | 448 | kfree(devname, M_MOUNT); |
| 1f1d24d2 AP |
449 | if (mf != NULL) |
| 450 | kfree(mf, M_MOUNT); | |
| f2365172 | 451 | if (error == 0) { |
| 984263bc | 452 | /* register with list of mounted filesystems */ |
| 861905fb | 453 | mountlist_insert(mp, MNTINS_FIRST); |
| 984263bc | 454 | |
| 8a8d5d85 | 455 | /* sanity check system clock against root fs timestamp */ |
| 984263bc | 456 | inittodr(mp->mnt_time); |
| 69f27b85 FT |
457 | |
| 458 | /* Get the vnode for '/'. Set p->p_fd->fd_cdir to reference it. */ | |
| 459 | mp = mountlist_boot_getfirst(); | |
| 460 | if (VFS_ROOT(mp, &vp)) | |
| 461 | panic("cannot find root vnode"); | |
| 462 | if (mp->mnt_ncmountpt.ncp == NULL) { | |
| 463 | cache_allocroot(&mp->mnt_ncmountpt, mp, vp); | |
| 464 | cache_unlock(&mp->mnt_ncmountpt); /* leave ref intact */ | |
| 465 | } | |
| 466 | p = curproc; | |
| 467 | p->p_fd->fd_cdir = vp; | |
| 468 | vref(p->p_fd->fd_cdir); | |
| 469 | p->p_fd->fd_rdir = vp; | |
| 470 | vref(p->p_fd->fd_rdir); | |
| 471 | vfs_cache_setroot(vp, cache_hold(&mp->mnt_ncmountpt)); | |
| 472 | vn_unlock(vp); /* leave ref intact */ | |
| 473 | cache_copy(&mp->mnt_ncmountpt, &p->p_fd->fd_ncdir); | |
| 474 | cache_copy(&mp->mnt_ncmountpt, &p->p_fd->fd_nrdir); | |
| 475 | ||
| f9642f56 | 476 | vfs_unbusy(mp); |
| f2365172 MD |
477 | if (mp->mnt_syncer == NULL) { |
| 478 | error = vfs_allocate_syncvnode(mp); | |
| 479 | if (error) | |
| 480 | kprintf("Warning: no syncer vp for root!\n"); | |
| 481 | error = 0; | |
| 482 | } | |
| 69f27b85 | 483 | VFS_START( mp, 0 ); |
| f2365172 MD |
484 | } else { |
| 485 | if (mp != NULL) { | |
| 486 | vfs_unbusy(mp); | |
| 487 | kfree(mp, M_MOUNT); | |
| 488 | } | |
| 489 | kprintf("Root mount failed: %d\n", error); | |
| 984263bc MD |
490 | } |
| 491 | return(error); | |
| 492 | } | |
| 493 | ||
| cd29885a | 494 | |
| 3a3826b3 | 495 | static void |
| b28e21ef SW |
496 | vfs_mountroot_ask_callback(char *name, cdev_t dev, bool is_alias, |
| 497 | void *arg __unused) | |
| 3a3826b3 | 498 | { |
| b28e21ef SW |
499 | if (!is_alias && dev_is_good(dev) && (dev_dflags(dev) & D_DISK)) |
| 500 | kprintf(" \"%s\" ", name); | |
| 3a3826b3 AH |
501 | } |
| 502 | ||
| cd29885a | 503 | |
| 984263bc MD |
504 | /* |
| 505 | * Spin prompting on the console for a suitable root filesystem | |
| 506 | */ | |
| 507 | static int | |
| 508 | vfs_mountroot_ask(void) | |
| 509 | { | |
| 510 | char name[128]; | |
| cee92776 | 511 | int llimit = 100; |
| 984263bc | 512 | |
| cee92776 MD |
513 | kprintf("\nManual root filesystem specification:\n"); |
| 514 | kprintf(" <fstype>:<device> Specify root (e.g. ufs:da0s1a)\n"); | |
| 515 | kprintf(" ? List valid disk boot devices\n"); | |
| 516 | kprintf(" panic Just panic\n"); | |
| 517 | kprintf(" abort Abort manual input\n"); | |
| 518 | while (llimit--) { | |
| 6ea70f76 | 519 | kprintf("\nmountroot> "); |
| cee92776 MD |
520 | |
| 521 | if (getline(name, 128) < 0) | |
| 522 | break; | |
| 523 | if (name[0] == 0) { | |
| 524 | ; | |
| 525 | } else if (name[0] == '?') { | |
| cd29885a MD |
526 | kprintf("Possibly valid devices for root FS:\n"); |
| 527 | //enumerate all disk devices | |
| 3a3826b3 | 528 | devfs_scan_callback(vfs_mountroot_ask_callback, NULL); |
| 6ea70f76 | 529 | kprintf("\n"); |
| 984263bc | 530 | continue; |
| cee92776 MD |
531 | } else if (strcmp(name, "panic") == 0) { |
| 532 | panic("panic from console"); | |
| 533 | } else if (strcmp(name, "abort") == 0) { | |
| 534 | break; | |
| 535 | } else if (vfs_mountroot_try(name) == 0) { | |
| 984263bc | 536 | return(0); |
| cee92776 | 537 | } |
| 984263bc | 538 | } |
| cee92776 | 539 | return(1); |
| 0e9b9130 MD |
540 | } |
| 541 | ||
| cd29885a | 542 | |
| cee92776 MD |
543 | static int |
| 544 | getline(char *cp, int limit) | |
| 984263bc MD |
545 | { |
| 546 | char *lp; | |
| 547 | int c; | |
| 548 | ||
| 549 | lp = cp; | |
| 550 | for (;;) { | |
| 7f12a969 MD |
551 | c = cngetc(); |
| 552 | ||
| 984263bc MD |
553 | switch (c) { |
| 554 | case -1: | |
| cee92776 | 555 | return(-1); |
| 984263bc MD |
556 | case '\n': |
| 557 | case '\r': | |
| 7f12a969 | 558 | kprintf("\n"); |
| 984263bc | 559 | *lp++ = '\0'; |
| cee92776 | 560 | return(0); |
| 984263bc MD |
561 | case '\b': |
| 562 | case '\177': | |
| 563 | if (lp > cp) { | |
| 7f12a969 | 564 | kprintf("\b \b"); |
| 984263bc | 565 | lp--; |
| 7f12a969 MD |
566 | } else { |
| 567 | kprintf("%c", 7); | |
| 984263bc MD |
568 | } |
| 569 | continue; | |
| 570 | case '#': | |
| 7f12a969 | 571 | kprintf("#"); |
| 984263bc MD |
572 | lp--; |
| 573 | if (lp < cp) | |
| 574 | lp = cp; | |
| 575 | continue; | |
| 576 | case '@': | |
| 577 | case 'u' & 037: | |
| 578 | lp = cp; | |
| 6ea70f76 | 579 | kprintf("%c", '\n'); |
| 984263bc MD |
580 | continue; |
| 581 | default: | |
| cee92776 MD |
582 | if (lp - cp >= limit - 1) { |
| 583 | kprintf("%c", 7); | |
| 584 | } else { | |
| 7f12a969 | 585 | kprintf("%c", c); |
| cee92776 MD |
586 | *lp++ = c; |
| 587 | } | |
| 588 | continue; | |
| 984263bc MD |
589 | } |
| 590 | } | |
| 591 | } | |
| 592 | ||
| 593 | /* | |
| b13267a5 | 594 | * Convert a given name to the cdev_t of the disk-like device |
| 984263bc MD |
595 | * it refers to. |
| 596 | */ | |
| b13267a5 | 597 | cdev_t |
| bc01a404 | 598 | kgetdiskbyname(const char *name) |
| e4c9c0c8 | 599 | { |
| b13267a5 | 600 | cdev_t rdev; |
| 984263bc | 601 | |
| 6b98993e MD |
602 | /* |
| 603 | * Get the base name of the device | |
| 604 | */ | |
| 1f2de5d4 MD |
605 | if (strncmp(name, __SYS_PATH_DEV, sizeof(__SYS_PATH_DEV) - 1) == 0) |
| 606 | name += sizeof(__SYS_PATH_DEV) - 1; | |
| 6b98993e MD |
607 | |
| 608 | /* | |
| 609 | * Locate the device | |
| 610 | */ | |
| a7a95662 | 611 | rdev = devfs_find_device_by_name("%s", name); |
| cd29885a | 612 | if (rdev == NULL) { |
| 1a8913f1 | 613 | kprintf("no disk named '%s'\n", name); |
| 6b98993e | 614 | } |
| 6b98993e MD |
615 | /* |
| 616 | * FOUND DEVICE | |
| 617 | */ | |
| e4c9c0c8 | 618 | return(rdev); |
| 984263bc MD |
619 | } |
| 620 | ||
| 621 | /* | |
| 622 | * Set rootdev to match (name), given that we expect it to | |
| 623 | * refer to a disk-like device. | |
| 624 | */ | |
| 625 | static int | |
| 626 | setrootbyname(char *name) | |
| 627 | { | |
| b13267a5 | 628 | cdev_t diskdev; |
| 984263bc | 629 | |
| bc01a404 | 630 | diskdev = kgetdiskbyname(name); |
| 028066b1 | 631 | if (diskdev != NULL) { |
| 984263bc MD |
632 | rootdev = diskdev; |
| 633 | return (0); | |
| 634 | } | |
| cd29885a MD |
635 | /* set to NULL if kgetdiskbyname() fails so that if the first rootdev is |
| 636 | * found by fails to mount and the second one isn't found, mountroot_try | |
| 637 | * doesn't try again with the first one | |
| 638 | */ | |
| 639 | rootdev = NULL; | |
| 984263bc MD |
640 | return (1); |
| 641 | } | |
| 642 | ||
| 643 | #ifdef DDB | |
| 644 | DB_SHOW_COMMAND(disk, db_getdiskbyname) | |
| 645 | { | |
| b13267a5 | 646 | cdev_t dev; |
| 984263bc MD |
647 | |
| 648 | if (modif[0] == '\0') { | |
| 649 | db_error("usage: show disk/devicename"); | |
| 650 | return; | |
| 651 | } | |
| bc01a404 | 652 | dev = kgetdiskbyname(modif); |
| 028066b1 | 653 | if (dev != NULL) |
| b13267a5 | 654 | db_printf("cdev_t = %p\n", dev); |
| 984263bc MD |
655 | else |
| 656 | db_printf("No disk device matched.\n"); | |
| 657 | } | |
| 658 | #endif | |
| b480ee28 AH |
659 | |
| 660 | static int | |
| 661 | vfs_sysctl_real_root(SYSCTL_HANDLER_ARGS) | |
| 662 | { | |
| 663 | char *real_root; | |
| 664 | size_t len; | |
| 665 | int error; | |
| 666 | ||
| 667 | real_root = kgetenv("vfs.root.realroot"); | |
| 668 | ||
| 669 | if (real_root == NULL) | |
| 670 | real_root = ""; | |
| 671 | ||
| 672 | len = strlen(real_root) + 1; | |
| 673 | ||
| 674 | error = sysctl_handle_string(oidp, real_root, len, req); | |
| 675 | ||
| 676 | return error; | |
| 677 | } | |
| 678 | ||
| 679 | SYSCTL_PROC(_vfs, OID_AUTO, real_root, | |
| 680 | CTLTYPE_STRING | CTLFLAG_RD, 0, 0, vfs_sysctl_real_root, | |
| 681 | "A", "Real root mount string"); |