1213365Smarcel/*- 2214006Smarcel * Copyright (c) 2010 Marcel Moolenaar 3213365Smarcel * Copyright (c) 1999-2004 Poul-Henning Kamp 4213365Smarcel * Copyright (c) 1999 Michael Smith 5213365Smarcel * Copyright (c) 1989, 1993 6213365Smarcel * The Regents of the University of California. All rights reserved. 7213365Smarcel * (c) UNIX System Laboratories, Inc. 8213365Smarcel * All or some portions of this file are derived from material licensed 9213365Smarcel * to the University of California by American Telephone and Telegraph 10213365Smarcel * Co. or Unix System Laboratories, Inc. and are reproduced herein with 11213365Smarcel * the permission of UNIX System Laboratories, Inc. 12213365Smarcel * 13213365Smarcel * Redistribution and use in source and binary forms, with or without 14213365Smarcel * modification, are permitted provided that the following conditions 15213365Smarcel * are met: 16213365Smarcel * 1. Redistributions of source code must retain the above copyright 17213365Smarcel * notice, this list of conditions and the following disclaimer. 18213365Smarcel * 2. Redistributions in binary form must reproduce the above copyright 19213365Smarcel * notice, this list of conditions and the following disclaimer in the 20213365Smarcel * documentation and/or other materials provided with the distribution. 21213365Smarcel * 4. Neither the name of the University nor the names of its contributors 22213365Smarcel * may be used to endorse or promote products derived from this software 23213365Smarcel * without specific prior written permission. 24213365Smarcel * 25213365Smarcel * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 26213365Smarcel * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 27213365Smarcel * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 28213365Smarcel * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 29213365Smarcel * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 30213365Smarcel * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 31213365Smarcel * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 32213365Smarcel * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 33213365Smarcel * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 34213365Smarcel * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 35213365Smarcel * SUCH DAMAGE. 36213365Smarcel */ 37213365Smarcel 38214006Smarcel#include "opt_rootdevname.h" 39214006Smarcel 40213365Smarcel#include <sys/cdefs.h> 41213365Smarcel__FBSDID("$FreeBSD: stable/10/sys/kern/vfs_mountroot.c 331276 2018-03-20 22:57:14Z ian $"); 42213365Smarcel 43213365Smarcel#include <sys/param.h> 44213365Smarcel#include <sys/conf.h> 45228634Savg#include <sys/cons.h> 46213365Smarcel#include <sys/fcntl.h> 47213365Smarcel#include <sys/jail.h> 48213365Smarcel#include <sys/kernel.h> 49213365Smarcel#include <sys/malloc.h> 50214006Smarcel#include <sys/mdioctl.h> 51213365Smarcel#include <sys/mount.h> 52213365Smarcel#include <sys/mutex.h> 53213365Smarcel#include <sys/namei.h> 54213365Smarcel#include <sys/priv.h> 55213365Smarcel#include <sys/proc.h> 56213365Smarcel#include <sys/filedesc.h> 57213365Smarcel#include <sys/reboot.h> 58223919Sae#include <sys/sbuf.h> 59214006Smarcel#include <sys/stat.h> 60213365Smarcel#include <sys/syscallsubr.h> 61213365Smarcel#include <sys/sysproto.h> 62213365Smarcel#include <sys/sx.h> 63213365Smarcel#include <sys/sysctl.h> 64213365Smarcel#include <sys/sysent.h> 65213365Smarcel#include <sys/systm.h> 66213365Smarcel#include <sys/vnode.h> 67213365Smarcel 68213365Smarcel#include <geom/geom.h> 69213365Smarcel 70213365Smarcel/* 71213365Smarcel * The root filesystem is detailed in the kernel environment variable 72213365Smarcel * vfs.root.mountfrom, which is expected to be in the general format 73213365Smarcel * 74213365Smarcel * <vfsname>:[<path>][ <vfsname>:[<path>] ...] 75213365Smarcel * vfsname := the name of a VFS known to the kernel and capable 76213365Smarcel * of being mounted as root 77213365Smarcel * path := disk device name or other data used by the filesystem 78213365Smarcel * to locate its physical store 79213365Smarcel * 80213365Smarcel * If the environment variable vfs.root.mountfrom is a space separated list, 81213365Smarcel * each list element is tried in turn and the root filesystem will be mounted 82302234Sbdrewery * from the first one that succeeds. 83213365Smarcel * 84213365Smarcel * The environment variable vfs.root.mountfrom.options is a comma delimited 85213365Smarcel * set of string mount options. These mount options must be parseable 86213365Smarcel * by nmount() in the kernel. 87213365Smarcel */ 88213365Smarcel 89214006Smarcelstatic int parse_mount(char **); 90214006Smarcelstatic struct mntarg *parse_mountroot_options(struct mntarg *, const char *); 91214006Smarcel 92213365Smarcel/* 93214006Smarcel * The vnode of the system's root (/ in the filesystem, without chroot 94214006Smarcel * active.) 95213365Smarcel */ 96214006Smarcelstruct vnode *rootvnode; 97213365Smarcel 98293742Strasz/* 99293742Strasz * Mount of the system's /dev. 100293742Strasz */ 101293742Straszstruct mount *rootdevmp; 102293742Strasz 103214006Smarcelchar *rootdevnames[2] = {NULL, NULL}; 104213365Smarcel 105267752Smavstruct mtx root_holds_mtx; 106267752SmavMTX_SYSINIT(root_holds, &root_holds_mtx, "root_holds", MTX_DEF); 107267752Smav 108213365Smarcelstruct root_hold_token { 109213365Smarcel const char *who; 110213365Smarcel LIST_ENTRY(root_hold_token) list; 111213365Smarcel}; 112213365Smarcel 113213365Smarcelstatic LIST_HEAD(, root_hold_token) root_holds = 114213365Smarcel LIST_HEAD_INITIALIZER(root_holds); 115213365Smarcel 116214006Smarcelenum action { 117214006Smarcel A_CONTINUE, 118214006Smarcel A_PANIC, 119214006Smarcel A_REBOOT, 120214006Smarcel A_RETRY 121214006Smarcel}; 122214006Smarcel 123214006Smarcelstatic enum action root_mount_onfail = A_CONTINUE; 124214006Smarcel 125214006Smarcelstatic int root_mount_mddev; 126213365Smarcelstatic int root_mount_complete; 127213365Smarcel 128214006Smarcel/* By default wait up to 3 seconds for devices to appear. */ 129214006Smarcelstatic int root_mount_timeout = 3; 130253910SmarcelTUNABLE_INT("vfs.mountroot.timeout", &root_mount_timeout); 131214006Smarcel 132213365Smarcelstruct root_hold_token * 133213365Smarcelroot_mount_hold(const char *identifier) 134213365Smarcel{ 135213365Smarcel struct root_hold_token *h; 136213365Smarcel 137213365Smarcel if (root_mounted()) 138213365Smarcel return (NULL); 139213365Smarcel 140213365Smarcel h = malloc(sizeof *h, M_DEVBUF, M_ZERO | M_WAITOK); 141213365Smarcel h->who = identifier; 142267752Smav mtx_lock(&root_holds_mtx); 143213365Smarcel LIST_INSERT_HEAD(&root_holds, h, list); 144267752Smav mtx_unlock(&root_holds_mtx); 145213365Smarcel return (h); 146213365Smarcel} 147213365Smarcel 148213365Smarcelvoid 149213365Smarcelroot_mount_rel(struct root_hold_token *h) 150213365Smarcel{ 151213365Smarcel 152213365Smarcel if (h == NULL) 153213365Smarcel return; 154267752Smav mtx_lock(&root_holds_mtx); 155213365Smarcel LIST_REMOVE(h, list); 156213365Smarcel wakeup(&root_holds); 157267752Smav mtx_unlock(&root_holds_mtx); 158213365Smarcel free(h, M_DEVBUF); 159213365Smarcel} 160213365Smarcel 161213365Smarcelint 162213365Smarcelroot_mounted(void) 163213365Smarcel{ 164213365Smarcel 165213365Smarcel /* No mutex is acquired here because int stores are atomic. */ 166213365Smarcel return (root_mount_complete); 167213365Smarcel} 168213365Smarcel 169213365Smarcelvoid 170213365Smarcelroot_mount_wait(void) 171213365Smarcel{ 172213365Smarcel 173213365Smarcel /* 174213365Smarcel * Panic on an obvious deadlock - the function can't be called from 175213365Smarcel * a thread which is doing the whole SYSINIT stuff. 176213365Smarcel */ 177213365Smarcel KASSERT(curthread->td_proc->p_pid != 0, 178213365Smarcel ("root_mount_wait: cannot be called from the swapper thread")); 179267752Smav mtx_lock(&root_holds_mtx); 180213365Smarcel while (!root_mount_complete) { 181267752Smav msleep(&root_mount_complete, &root_holds_mtx, PZERO, "rootwait", 182213365Smarcel hz); 183213365Smarcel } 184267752Smav mtx_unlock(&root_holds_mtx); 185213365Smarcel} 186213365Smarcel 187213365Smarcelstatic void 188213365Smarcelset_rootvnode(void) 189213365Smarcel{ 190213365Smarcel struct proc *p; 191213365Smarcel 192213365Smarcel if (VFS_ROOT(TAILQ_FIRST(&mountlist), LK_EXCLUSIVE, &rootvnode)) 193213365Smarcel panic("Cannot find root vnode"); 194213365Smarcel 195213365Smarcel VOP_UNLOCK(rootvnode, 0); 196213365Smarcel 197213365Smarcel p = curthread->td_proc; 198213365Smarcel FILEDESC_XLOCK(p->p_fd); 199213365Smarcel 200213365Smarcel if (p->p_fd->fd_cdir != NULL) 201213365Smarcel vrele(p->p_fd->fd_cdir); 202213365Smarcel p->p_fd->fd_cdir = rootvnode; 203213365Smarcel VREF(rootvnode); 204213365Smarcel 205213365Smarcel if (p->p_fd->fd_rdir != NULL) 206213365Smarcel vrele(p->p_fd->fd_rdir); 207213365Smarcel p->p_fd->fd_rdir = rootvnode; 208213365Smarcel VREF(rootvnode); 209213365Smarcel 210213365Smarcel FILEDESC_XUNLOCK(p->p_fd); 211213365Smarcel} 212213365Smarcel 213214006Smarcelstatic int 214214006Smarcelvfs_mountroot_devfs(struct thread *td, struct mount **mpp) 215213365Smarcel{ 216213365Smarcel struct vfsoptlist *opts; 217213365Smarcel struct vfsconf *vfsp; 218214006Smarcel struct mount *mp; 219213365Smarcel int error; 220213365Smarcel 221214006Smarcel *mpp = NULL; 222214006Smarcel 223293743Strasz if (rootdevmp != NULL) { 224293743Strasz /* 225293743Strasz * Already have /dev; this happens during rerooting. 226293743Strasz */ 227293743Strasz error = vfs_busy(rootdevmp, 0); 228293743Strasz if (error != 0) 229293743Strasz return (error); 230293743Strasz *mpp = rootdevmp; 231293743Strasz } else { 232293743Strasz vfsp = vfs_byname("devfs"); 233293743Strasz KASSERT(vfsp != NULL, ("Could not find devfs by name")); 234293743Strasz if (vfsp == NULL) 235293743Strasz return (ENOENT); 236213365Smarcel 237293743Strasz mp = vfs_mount_alloc(NULLVP, vfsp, "/dev", td->td_ucred); 238213365Smarcel 239293743Strasz error = VFS_MOUNT(mp); 240293743Strasz KASSERT(error == 0, ("VFS_MOUNT(devfs) failed %d", error)); 241293743Strasz if (error) 242293743Strasz return (error); 243213365Smarcel 244293743Strasz opts = malloc(sizeof(struct vfsoptlist), M_MOUNT, M_WAITOK); 245293743Strasz TAILQ_INIT(opts); 246293743Strasz mp->mnt_opt = opts; 247213365Smarcel 248293743Strasz mtx_lock(&mountlist_mtx); 249293743Strasz TAILQ_INSERT_HEAD(&mountlist, mp, mnt_list); 250293743Strasz mtx_unlock(&mountlist_mtx); 251213365Smarcel 252293743Strasz *mpp = mp; 253293743Strasz rootdevmp = mp; 254293743Strasz } 255293743Strasz 256213365Smarcel set_rootvnode(); 257213365Smarcel 258213365Smarcel error = kern_symlink(td, "/", "dev", UIO_SYSSPACE); 259213365Smarcel if (error) 260213365Smarcel printf("kern_symlink /dev -> / returns %d\n", error); 261214006Smarcel 262214006Smarcel return (error); 263213365Smarcel} 264213365Smarcel 265288400Sbdrewerystatic void 266214006Smarcelvfs_mountroot_shuffle(struct thread *td, struct mount *mpdevfs) 267213365Smarcel{ 268213365Smarcel struct nameidata nd; 269214006Smarcel struct mount *mporoot, *mpnroot; 270214006Smarcel struct vnode *vp, *vporoot, *vpdevfs; 271214006Smarcel char *fspath; 272213365Smarcel int error; 273213365Smarcel 274214006Smarcel mpnroot = TAILQ_NEXT(mpdevfs, mnt_list); 275214006Smarcel 276214006Smarcel /* Shuffle the mountlist. */ 277213365Smarcel mtx_lock(&mountlist_mtx); 278214006Smarcel mporoot = TAILQ_FIRST(&mountlist); 279214006Smarcel TAILQ_REMOVE(&mountlist, mpdevfs, mnt_list); 280214006Smarcel if (mporoot != mpdevfs) { 281214006Smarcel TAILQ_REMOVE(&mountlist, mpnroot, mnt_list); 282214006Smarcel TAILQ_INSERT_HEAD(&mountlist, mpnroot, mnt_list); 283214006Smarcel } 284214006Smarcel TAILQ_INSERT_TAIL(&mountlist, mpdevfs, mnt_list); 285213365Smarcel mtx_unlock(&mountlist_mtx); 286213365Smarcel 287214006Smarcel cache_purgevfs(mporoot); 288214006Smarcel if (mporoot != mpdevfs) 289214006Smarcel cache_purgevfs(mpdevfs); 290213365Smarcel 291214006Smarcel VFS_ROOT(mporoot, LK_EXCLUSIVE, &vporoot); 292214006Smarcel 293214006Smarcel VI_LOCK(vporoot); 294214006Smarcel vporoot->v_iflag &= ~VI_MOUNT; 295214006Smarcel VI_UNLOCK(vporoot); 296214006Smarcel vporoot->v_mountedhere = NULL; 297214006Smarcel mporoot->mnt_flag &= ~MNT_ROOTFS; 298214006Smarcel mporoot->mnt_vnodecovered = NULL; 299214006Smarcel vput(vporoot); 300214006Smarcel 301214006Smarcel /* Set up the new rootvnode, and purge the cache */ 302214006Smarcel mpnroot->mnt_vnodecovered = NULL; 303213365Smarcel set_rootvnode(); 304213365Smarcel cache_purgevfs(rootvnode->v_mount); 305213365Smarcel 306214006Smarcel if (mporoot != mpdevfs) { 307214006Smarcel /* Remount old root under /.mount or /mnt */ 308214006Smarcel fspath = "/.mount"; 309214006Smarcel NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, 310214006Smarcel fspath, td); 311214006Smarcel error = namei(&nd); 312214006Smarcel if (error) { 313214006Smarcel NDFREE(&nd, NDF_ONLY_PNBUF); 314214006Smarcel fspath = "/mnt"; 315214006Smarcel NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, 316214006Smarcel fspath, td); 317214006Smarcel error = namei(&nd); 318214006Smarcel } 319214006Smarcel if (!error) { 320214006Smarcel vp = nd.ni_vp; 321214006Smarcel error = (vp->v_type == VDIR) ? 0 : ENOTDIR; 322214006Smarcel if (!error) 323214006Smarcel error = vinvalbuf(vp, V_SAVE, 0, 0); 324214006Smarcel if (!error) { 325214006Smarcel cache_purge(vp); 326214006Smarcel mporoot->mnt_vnodecovered = vp; 327214006Smarcel vp->v_mountedhere = mporoot; 328214006Smarcel strlcpy(mporoot->mnt_stat.f_mntonname, 329214006Smarcel fspath, MNAMELEN); 330214006Smarcel VOP_UNLOCK(vp, 0); 331214006Smarcel } else 332214006Smarcel vput(vp); 333214006Smarcel } 334214006Smarcel NDFREE(&nd, NDF_ONLY_PNBUF); 335214006Smarcel 336214006Smarcel if (error && bootverbose) 337214006Smarcel printf("mountroot: unable to remount previous root " 338214006Smarcel "under /.mount or /mnt (error %d).\n", error); 339214006Smarcel } 340214006Smarcel 341214006Smarcel /* Remount devfs under /dev */ 342213365Smarcel NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, "/dev", td); 343213365Smarcel error = namei(&nd); 344214006Smarcel if (!error) { 345214006Smarcel vp = nd.ni_vp; 346214006Smarcel error = (vp->v_type == VDIR) ? 0 : ENOTDIR; 347214006Smarcel if (!error) 348214006Smarcel error = vinvalbuf(vp, V_SAVE, 0, 0); 349214006Smarcel if (!error) { 350214006Smarcel vpdevfs = mpdevfs->mnt_vnodecovered; 351214006Smarcel if (vpdevfs != NULL) { 352214006Smarcel cache_purge(vpdevfs); 353214006Smarcel vpdevfs->v_mountedhere = NULL; 354214006Smarcel vrele(vpdevfs); 355214006Smarcel } 356214006Smarcel mpdevfs->mnt_vnodecovered = vp; 357214006Smarcel vp->v_mountedhere = mpdevfs; 358214006Smarcel VOP_UNLOCK(vp, 0); 359214006Smarcel } else 360214006Smarcel vput(vp); 361213365Smarcel } 362214006Smarcel if (error && bootverbose) 363214006Smarcel printf("mountroot: unable to remount devfs under /dev " 364214006Smarcel "(error %d).\n", error); 365213365Smarcel NDFREE(&nd, NDF_ONLY_PNBUF); 366214006Smarcel 367214006Smarcel if (mporoot == mpdevfs) { 368214006Smarcel vfs_unbusy(mpdevfs); 369214006Smarcel /* Unlink the no longer needed /dev/dev -> / symlink */ 370214006Smarcel error = kern_unlink(td, "/dev/dev", UIO_SYSSPACE); 371214006Smarcel if (error && bootverbose) 372214006Smarcel printf("mountroot: unable to unlink /dev/dev " 373214006Smarcel "(error %d)\n", error); 374213365Smarcel } 375214006Smarcel} 376214006Smarcel 377214006Smarcel/* 378214006Smarcel * Configuration parser. 379214006Smarcel */ 380214006Smarcel 381214006Smarcel/* Parser character classes. */ 382214006Smarcel#define CC_WHITESPACE -1 383214006Smarcel#define CC_NONWHITESPACE -2 384214006Smarcel 385214006Smarcel/* Parse errors. */ 386214006Smarcel#define PE_EOF -1 387214006Smarcel#define PE_EOL -2 388214006Smarcel 389214006Smarcelstatic __inline int 390214006Smarcelparse_peek(char **conf) 391214006Smarcel{ 392214006Smarcel 393214006Smarcel return (**conf); 394214006Smarcel} 395214006Smarcel 396214006Smarcelstatic __inline void 397214006Smarcelparse_poke(char **conf, int c) 398214006Smarcel{ 399214006Smarcel 400214006Smarcel **conf = c; 401214006Smarcel} 402214006Smarcel 403214006Smarcelstatic __inline void 404214006Smarcelparse_advance(char **conf) 405214006Smarcel{ 406214006Smarcel 407214006Smarcel (*conf)++; 408214006Smarcel} 409214006Smarcel 410214006Smarcelstatic int 411214006Smarcelparse_skipto(char **conf, int mc) 412214006Smarcel{ 413214006Smarcel int c, match; 414214006Smarcel 415214006Smarcel while (1) { 416214006Smarcel c = parse_peek(conf); 417214006Smarcel if (c == 0) 418214006Smarcel return (PE_EOF); 419214006Smarcel switch (mc) { 420214006Smarcel case CC_WHITESPACE: 421214006Smarcel match = (c == ' ' || c == '\t' || c == '\n') ? 1 : 0; 422214006Smarcel break; 423214006Smarcel case CC_NONWHITESPACE: 424214006Smarcel if (c == '\n') 425214006Smarcel return (PE_EOL); 426214006Smarcel match = (c != ' ' && c != '\t') ? 1 : 0; 427214006Smarcel break; 428214006Smarcel default: 429214006Smarcel match = (c == mc) ? 1 : 0; 430214006Smarcel break; 431214006Smarcel } 432214006Smarcel if (match) 433214006Smarcel break; 434214006Smarcel parse_advance(conf); 435213365Smarcel } 436214006Smarcel return (0); 437214006Smarcel} 438213365Smarcel 439214006Smarcelstatic int 440214006Smarcelparse_token(char **conf, char **tok) 441214006Smarcel{ 442214006Smarcel char *p; 443214006Smarcel size_t len; 444214006Smarcel int error; 445214006Smarcel 446214006Smarcel *tok = NULL; 447214006Smarcel error = parse_skipto(conf, CC_NONWHITESPACE); 448213365Smarcel if (error) 449214006Smarcel return (error); 450214006Smarcel p = *conf; 451214006Smarcel error = parse_skipto(conf, CC_WHITESPACE); 452214006Smarcel len = *conf - p; 453214006Smarcel *tok = malloc(len + 1, M_TEMP, M_WAITOK | M_ZERO); 454214006Smarcel bcopy(p, *tok, len); 455214006Smarcel return (0); 456213365Smarcel} 457213365Smarcel 458214006Smarcelstatic void 459214006Smarcelparse_dir_ask_printenv(const char *var) 460213365Smarcel{ 461214006Smarcel char *val; 462213365Smarcel 463214006Smarcel val = getenv(var); 464214006Smarcel if (val != NULL) { 465214006Smarcel printf(" %s=%s\n", var, val); 466214006Smarcel freeenv(val); 467214006Smarcel } 468214006Smarcel} 469213365Smarcel 470214006Smarcelstatic int 471214006Smarcelparse_dir_ask(char **conf) 472214006Smarcel{ 473214006Smarcel char name[80]; 474214006Smarcel char *mnt; 475214006Smarcel int error; 476213365Smarcel 477214006Smarcel printf("\nLoader variables:\n"); 478214006Smarcel parse_dir_ask_printenv("vfs.root.mountfrom"); 479214006Smarcel parse_dir_ask_printenv("vfs.root.mountfrom.options"); 480213365Smarcel 481214006Smarcel printf("\nManual root filesystem specification:\n"); 482214006Smarcel printf(" <fstype>:<device> [options]\n"); 483214006Smarcel printf(" Mount <device> using filesystem <fstype>\n"); 484214006Smarcel printf(" and with the specified (optional) option list.\n"); 485214006Smarcel printf("\n"); 486214006Smarcel printf(" eg. ufs:/dev/da0s1a\n"); 487214006Smarcel printf(" zfs:tank\n"); 488214006Smarcel printf(" cd9660:/dev/acd0 ro\n"); 489214006Smarcel printf(" (which is equivalent to: "); 490214006Smarcel printf("mount -t cd9660 -o ro /dev/acd0 /)\n"); 491214006Smarcel printf("\n"); 492214006Smarcel printf(" ? List valid disk boot devices\n"); 493214006Smarcel printf(" . Yield 1 second (for background tasks)\n"); 494214006Smarcel printf(" <empty line> Abort manual input\n"); 495214006Smarcel 496226673Smarcel do { 497226673Smarcel error = EINVAL; 498226673Smarcel printf("\nmountroot> "); 499228634Savg cngets(name, sizeof(name), GETS_ECHO); 500226673Smarcel if (name[0] == '\0') 501226673Smarcel break; 502226673Smarcel if (name[0] == '?' && name[1] == '\0') { 503226673Smarcel printf("\nList of GEOM managed disk devices:\n "); 504226673Smarcel g_dev_print(); 505226673Smarcel continue; 506226673Smarcel } 507226673Smarcel if (name[0] == '.' && name[1] == '\0') { 508226673Smarcel pause("rmask", hz); 509226673Smarcel continue; 510226673Smarcel } 511226673Smarcel mnt = name; 512226673Smarcel error = parse_mount(&mnt); 513226673Smarcel if (error == -1) 514226673Smarcel printf("Invalid file system specification.\n"); 515226673Smarcel } while (error != 0); 516226673Smarcel 517214006Smarcel return (error); 518214006Smarcel} 519213365Smarcel 520214006Smarcelstatic int 521214006Smarcelparse_dir_md(char **conf) 522214006Smarcel{ 523214006Smarcel struct stat sb; 524214006Smarcel struct thread *td; 525214006Smarcel struct md_ioctl *mdio; 526214006Smarcel char *path, *tok; 527214006Smarcel int error, fd, len; 528213365Smarcel 529214006Smarcel td = curthread; 530214006Smarcel 531214006Smarcel error = parse_token(conf, &tok); 532214006Smarcel if (error) 533214006Smarcel return (error); 534214006Smarcel 535214006Smarcel len = strlen(tok); 536214006Smarcel mdio = malloc(sizeof(*mdio) + len + 1, M_TEMP, M_WAITOK | M_ZERO); 537214006Smarcel path = (void *)(mdio + 1); 538214006Smarcel bcopy(tok, path, len); 539214006Smarcel free(tok, M_TEMP); 540214006Smarcel 541214006Smarcel /* Get file status. */ 542214006Smarcel error = kern_stat(td, path, UIO_SYSSPACE, &sb); 543214006Smarcel if (error) 544214006Smarcel goto out; 545214006Smarcel 546214006Smarcel /* Open /dev/mdctl so that we can attach/detach. */ 547214006Smarcel error = kern_open(td, "/dev/" MDCTL_NAME, UIO_SYSSPACE, O_RDWR, 0); 548214006Smarcel if (error) 549214006Smarcel goto out; 550214006Smarcel 551214006Smarcel fd = td->td_retval[0]; 552214006Smarcel mdio->md_version = MDIOVERSION; 553214006Smarcel mdio->md_type = MD_VNODE; 554214006Smarcel 555214006Smarcel if (root_mount_mddev != -1) { 556214006Smarcel mdio->md_unit = root_mount_mddev; 557214006Smarcel DROP_GIANT(); 558214006Smarcel error = kern_ioctl(td, fd, MDIOCDETACH, (void *)mdio); 559214006Smarcel PICKUP_GIANT(); 560214006Smarcel /* Ignore errors. We don't care. */ 561214006Smarcel root_mount_mddev = -1; 562214006Smarcel } 563214006Smarcel 564214006Smarcel mdio->md_file = (void *)(mdio + 1); 565214006Smarcel mdio->md_options = MD_AUTOUNIT | MD_READONLY; 566214006Smarcel mdio->md_mediasize = sb.st_size; 567214006Smarcel mdio->md_unit = 0; 568214006Smarcel DROP_GIANT(); 569214006Smarcel error = kern_ioctl(td, fd, MDIOCATTACH, (void *)mdio); 570214006Smarcel PICKUP_GIANT(); 571214006Smarcel if (error) 572214006Smarcel goto out; 573214006Smarcel 574214006Smarcel if (mdio->md_unit > 9) { 575214006Smarcel printf("rootmount: too many md units\n"); 576214006Smarcel mdio->md_file = NULL; 577214006Smarcel mdio->md_options = 0; 578214006Smarcel mdio->md_mediasize = 0; 579214006Smarcel DROP_GIANT(); 580214006Smarcel error = kern_ioctl(td, fd, MDIOCDETACH, (void *)mdio); 581214006Smarcel PICKUP_GIANT(); 582214006Smarcel /* Ignore errors. We don't care. */ 583214006Smarcel error = ERANGE; 584214006Smarcel goto out; 585214006Smarcel } 586214006Smarcel 587214006Smarcel root_mount_mddev = mdio->md_unit; 588214006Smarcel printf(MD_NAME "%u attached to %s\n", root_mount_mddev, mdio->md_file); 589214006Smarcel 590214006Smarcel error = kern_close(td, fd); 591214006Smarcel 592214006Smarcel out: 593214006Smarcel free(mdio, M_TEMP); 594214006Smarcel return (error); 595214006Smarcel} 596214006Smarcel 597214006Smarcelstatic int 598214006Smarcelparse_dir_onfail(char **conf) 599214006Smarcel{ 600214006Smarcel char *action; 601214006Smarcel int error; 602214006Smarcel 603214006Smarcel error = parse_token(conf, &action); 604214006Smarcel if (error) 605214006Smarcel return (error); 606214006Smarcel 607214006Smarcel if (!strcmp(action, "continue")) 608214006Smarcel root_mount_onfail = A_CONTINUE; 609214006Smarcel else if (!strcmp(action, "panic")) 610214006Smarcel root_mount_onfail = A_PANIC; 611214006Smarcel else if (!strcmp(action, "reboot")) 612214006Smarcel root_mount_onfail = A_REBOOT; 613214006Smarcel else if (!strcmp(action, "retry")) 614214006Smarcel root_mount_onfail = A_RETRY; 615214006Smarcel else { 616214006Smarcel printf("rootmount: %s: unknown action\n", action); 617214006Smarcel error = EINVAL; 618214006Smarcel } 619214006Smarcel 620214006Smarcel free(action, M_TEMP); 621214006Smarcel return (0); 622214006Smarcel} 623214006Smarcel 624214006Smarcelstatic int 625214006Smarcelparse_dir_timeout(char **conf) 626214006Smarcel{ 627214006Smarcel char *tok, *endtok; 628214006Smarcel long secs; 629214006Smarcel int error; 630214006Smarcel 631214006Smarcel error = parse_token(conf, &tok); 632214006Smarcel if (error) 633214006Smarcel return (error); 634214006Smarcel 635214006Smarcel secs = strtol(tok, &endtok, 0); 636214006Smarcel error = (secs < 0 || *endtok != '\0') ? EINVAL : 0; 637214006Smarcel if (!error) 638214006Smarcel root_mount_timeout = secs; 639214006Smarcel free(tok, M_TEMP); 640214006Smarcel return (error); 641214006Smarcel} 642214006Smarcel 643214006Smarcelstatic int 644214006Smarcelparse_directive(char **conf) 645214006Smarcel{ 646214006Smarcel char *dir; 647214006Smarcel int error; 648214006Smarcel 649214006Smarcel error = parse_token(conf, &dir); 650214006Smarcel if (error) 651214006Smarcel return (error); 652214006Smarcel 653214006Smarcel if (strcmp(dir, ".ask") == 0) 654214006Smarcel error = parse_dir_ask(conf); 655214006Smarcel else if (strcmp(dir, ".md") == 0) 656214006Smarcel error = parse_dir_md(conf); 657214006Smarcel else if (strcmp(dir, ".onfail") == 0) 658214006Smarcel error = parse_dir_onfail(conf); 659214006Smarcel else if (strcmp(dir, ".timeout") == 0) 660214006Smarcel error = parse_dir_timeout(conf); 661214006Smarcel else { 662214006Smarcel printf("mountroot: invalid directive `%s'\n", dir); 663214006Smarcel /* Ignore the rest of the line. */ 664214006Smarcel (void)parse_skipto(conf, '\n'); 665214006Smarcel error = EINVAL; 666214006Smarcel } 667214006Smarcel free(dir, M_TEMP); 668214006Smarcel return (error); 669214006Smarcel} 670214006Smarcel 671214006Smarcelstatic int 672214006Smarcelparse_mount_dev_present(const char *dev) 673214006Smarcel{ 674214006Smarcel struct nameidata nd; 675214006Smarcel int error; 676214006Smarcel 677214006Smarcel NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, dev, curthread); 678214006Smarcel error = namei(&nd); 679214006Smarcel if (!error) 680214006Smarcel vput(nd.ni_vp); 681214006Smarcel NDFREE(&nd, NDF_ONLY_PNBUF); 682214006Smarcel return (error != 0) ? 0 : 1; 683214006Smarcel} 684214006Smarcel 685243868Skib#define ERRMSGL 255 686214006Smarcelstatic int 687214006Smarcelparse_mount(char **conf) 688214006Smarcel{ 689243868Skib char *errmsg; 690214006Smarcel struct mntarg *ma; 691214006Smarcel char *dev, *fs, *opts, *tok; 692214006Smarcel int delay, error, timeout; 693214006Smarcel 694214006Smarcel error = parse_token(conf, &tok); 695214006Smarcel if (error) 696214006Smarcel return (error); 697214006Smarcel fs = tok; 698214006Smarcel error = parse_skipto(&tok, ':'); 699214006Smarcel if (error) { 700214006Smarcel free(fs, M_TEMP); 701214006Smarcel return (error); 702214006Smarcel } 703214006Smarcel parse_poke(&tok, '\0'); 704214006Smarcel parse_advance(&tok); 705214006Smarcel dev = tok; 706214006Smarcel 707214006Smarcel if (root_mount_mddev != -1) { 708214006Smarcel /* Handle substitution for the md unit number. */ 709214006Smarcel tok = strstr(dev, "md#"); 710214006Smarcel if (tok != NULL) 711214006Smarcel tok[2] = '0' + root_mount_mddev; 712214006Smarcel } 713214006Smarcel 714214006Smarcel /* Parse options. */ 715214006Smarcel error = parse_token(conf, &tok); 716214006Smarcel opts = (error == 0) ? tok : NULL; 717214006Smarcel 718214006Smarcel printf("Trying to mount root from %s:%s [%s]...\n", fs, dev, 719214006Smarcel (opts != NULL) ? opts : ""); 720214006Smarcel 721243868Skib errmsg = malloc(ERRMSGL, M_TEMP, M_WAITOK | M_ZERO); 722214006Smarcel 723214006Smarcel if (vfs_byname(fs) == NULL) { 724255412Sdelphij strlcpy(errmsg, "unknown file system", ERRMSGL); 725214006Smarcel error = ENOENT; 726214006Smarcel goto out; 727214006Smarcel } 728214006Smarcel 729253847Sian if (strcmp(fs, "zfs") != 0 && strstr(fs, "nfs") == NULL && 730253847Sian dev[0] != '\0' && !parse_mount_dev_present(dev)) { 731214006Smarcel printf("mountroot: waiting for device %s ...\n", dev); 732214006Smarcel delay = hz / 10; 733214006Smarcel timeout = root_mount_timeout * hz; 734214006Smarcel do { 735214006Smarcel pause("rmdev", delay); 736214006Smarcel timeout -= delay; 737214006Smarcel } while (timeout > 0 && !parse_mount_dev_present(dev)); 738214006Smarcel if (timeout <= 0) { 739214006Smarcel error = ENODEV; 740214006Smarcel goto out; 741214006Smarcel } 742214006Smarcel } 743214006Smarcel 744331276Sian delay = hz / 10; 745331276Sian timeout = root_mount_timeout * hz; 746214006Smarcel 747331276Sian for (;;) { 748331276Sian ma = NULL; 749331276Sian ma = mount_arg(ma, "fstype", fs, -1); 750331276Sian ma = mount_arg(ma, "fspath", "/", -1); 751331276Sian ma = mount_arg(ma, "from", dev, -1); 752331276Sian ma = mount_arg(ma, "errmsg", errmsg, ERRMSGL); 753331276Sian ma = mount_arg(ma, "ro", NULL, 0); 754331276Sian ma = parse_mountroot_options(ma, opts); 755331276Sian 756331276Sian error = kernel_mount(ma, MNT_ROOTFS); 757331276Sian if (error == 0 || timeout <= 0) 758331276Sian break; 759331276Sian 760331276Sian if (root_mount_timeout * hz == timeout || 761331276Sian (bootverbose && timeout % hz == 0)) { 762331276Sian printf("Mounting from %s:%s failed with error %d; " 763331276Sian "retrying for %d more second%s\n", fs, dev, error, 764331276Sian timeout / hz, (timeout / hz > 1) ? "s" : ""); 765331276Sian } 766331276Sian pause("rmretry", delay); 767331276Sian timeout -= delay; 768331276Sian } 769214006Smarcel out: 770214006Smarcel if (error) { 771214006Smarcel printf("Mounting from %s:%s failed with error %d", 772214006Smarcel fs, dev, error); 773214006Smarcel if (errmsg[0] != '\0') 774214006Smarcel printf(": %s", errmsg); 775214006Smarcel printf(".\n"); 776214006Smarcel } 777214006Smarcel free(fs, M_TEMP); 778243868Skib free(errmsg, M_TEMP); 779214006Smarcel if (opts != NULL) 780214006Smarcel free(opts, M_TEMP); 781214006Smarcel /* kernel_mount can return -1 on error. */ 782214006Smarcel return ((error < 0) ? EDOOFUS : error); 783214006Smarcel} 784243868Skib#undef ERRMSGL 785214006Smarcel 786214006Smarcelstatic int 787214006Smarcelvfs_mountroot_parse(struct sbuf *sb, struct mount *mpdevfs) 788214006Smarcel{ 789214006Smarcel struct mount *mp; 790214006Smarcel char *conf; 791214006Smarcel int error; 792214006Smarcel 793214006Smarcel root_mount_mddev = -1; 794214006Smarcel 795214006Smarcelretry: 796214006Smarcel conf = sbuf_data(sb); 797214006Smarcel mp = TAILQ_NEXT(mpdevfs, mnt_list); 798214006Smarcel error = (mp == NULL) ? 0 : EDOOFUS; 799214006Smarcel root_mount_onfail = A_CONTINUE; 800214006Smarcel while (mp == NULL) { 801214006Smarcel error = parse_skipto(&conf, CC_NONWHITESPACE); 802214006Smarcel if (error == PE_EOL) { 803214006Smarcel parse_advance(&conf); 804214006Smarcel continue; 805214006Smarcel } 806214006Smarcel if (error < 0) 807214006Smarcel break; 808214006Smarcel switch (parse_peek(&conf)) { 809214006Smarcel case '#': 810214006Smarcel error = parse_skipto(&conf, '\n'); 811214006Smarcel break; 812214006Smarcel case '.': 813214006Smarcel error = parse_directive(&conf); 814214006Smarcel break; 815214006Smarcel default: 816214006Smarcel error = parse_mount(&conf); 817214006Smarcel break; 818214006Smarcel } 819214006Smarcel if (error < 0) 820214006Smarcel break; 821214006Smarcel /* Ignore any trailing garbage on the line. */ 822214006Smarcel if (parse_peek(&conf) != '\n') { 823214006Smarcel printf("mountroot: advancing to next directive...\n"); 824214006Smarcel (void)parse_skipto(&conf, '\n'); 825214006Smarcel } 826214006Smarcel mp = TAILQ_NEXT(mpdevfs, mnt_list); 827214006Smarcel } 828214006Smarcel if (mp != NULL) 829214006Smarcel return (0); 830214006Smarcel 831213365Smarcel /* 832214006Smarcel * We failed to mount (a new) root. 833213365Smarcel */ 834214006Smarcel switch (root_mount_onfail) { 835214006Smarcel case A_CONTINUE: 836214006Smarcel break; 837214006Smarcel case A_PANIC: 838214006Smarcel panic("mountroot: unable to (re-)mount root."); 839214006Smarcel /* NOTREACHED */ 840214006Smarcel case A_RETRY: 841214006Smarcel goto retry; 842214006Smarcel case A_REBOOT: 843214006Smarcel kern_reboot(RB_NOSYNC); 844214006Smarcel /* NOTREACHED */ 845213365Smarcel } 846213365Smarcel 847214006Smarcel return (error); 848214006Smarcel} 849214006Smarcel 850214006Smarcelstatic void 851214006Smarcelvfs_mountroot_conf0(struct sbuf *sb) 852214006Smarcel{ 853214006Smarcel char *s, *tok, *mnt, *opt; 854214006Smarcel int error; 855214006Smarcel 856214006Smarcel sbuf_printf(sb, ".onfail panic\n"); 857214006Smarcel sbuf_printf(sb, ".timeout %d\n", root_mount_timeout); 858214006Smarcel if (boothowto & RB_ASKNAME) 859214006Smarcel sbuf_printf(sb, ".ask\n"); 860214006Smarcel#ifdef ROOTDEVNAME 861214006Smarcel if (boothowto & RB_DFLTROOT) 862214006Smarcel sbuf_printf(sb, "%s\n", ROOTDEVNAME); 863214006Smarcel#endif 864213365Smarcel if (boothowto & RB_CDROM) { 865217163Snwhitehorn sbuf_printf(sb, "cd9660:/dev/cd0 ro\n"); 866214006Smarcel sbuf_printf(sb, ".timeout 0\n"); 867217163Snwhitehorn sbuf_printf(sb, "cd9660:/dev/acd0 ro\n"); 868214006Smarcel sbuf_printf(sb, ".timeout %d\n", root_mount_timeout); 869214006Smarcel } 870214006Smarcel s = getenv("vfs.root.mountfrom"); 871214006Smarcel if (s != NULL) { 872214006Smarcel opt = getenv("vfs.root.mountfrom.options"); 873214006Smarcel tok = s; 874214006Smarcel error = parse_token(&tok, &mnt); 875214006Smarcel while (!error) { 876214006Smarcel sbuf_printf(sb, "%s %s\n", mnt, 877214006Smarcel (opt != NULL) ? opt : ""); 878214006Smarcel free(mnt, M_TEMP); 879214006Smarcel error = parse_token(&tok, &mnt); 880213365Smarcel } 881214006Smarcel if (opt != NULL) 882214006Smarcel freeenv(opt); 883214006Smarcel freeenv(s); 884213365Smarcel } 885214006Smarcel if (rootdevnames[0] != NULL) 886214006Smarcel sbuf_printf(sb, "%s\n", rootdevnames[0]); 887214006Smarcel if (rootdevnames[1] != NULL) 888214006Smarcel sbuf_printf(sb, "%s\n", rootdevnames[1]); 889214006Smarcel#ifdef ROOTDEVNAME 890214006Smarcel if (!(boothowto & RB_DFLTROOT)) 891214006Smarcel sbuf_printf(sb, "%s\n", ROOTDEVNAME); 892214006Smarcel#endif 893214006Smarcel if (!(boothowto & RB_ASKNAME)) 894214006Smarcel sbuf_printf(sb, ".ask\n"); 895214006Smarcel} 896213365Smarcel 897214006Smarcelstatic int 898214006Smarcelvfs_mountroot_readconf(struct thread *td, struct sbuf *sb) 899214006Smarcel{ 900214006Smarcel static char buf[128]; 901214006Smarcel struct nameidata nd; 902214006Smarcel off_t ofs; 903231949Skib ssize_t resid; 904241896Skib int error, flags, len; 905214006Smarcel 906241896Skib NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, "/.mount.conf", td); 907214006Smarcel flags = FREAD; 908214006Smarcel error = vn_open(&nd, &flags, 0, NULL); 909214006Smarcel if (error) 910214006Smarcel return (error); 911214006Smarcel 912214006Smarcel NDFREE(&nd, NDF_ONLY_PNBUF); 913214006Smarcel ofs = 0; 914214006Smarcel len = sizeof(buf) - 1; 915214006Smarcel while (1) { 916214006Smarcel error = vn_rdwr(UIO_READ, nd.ni_vp, buf, len, ofs, 917214006Smarcel UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred, 918214006Smarcel NOCRED, &resid, td); 919214006Smarcel if (error) 920214006Smarcel break; 921214006Smarcel if (resid == len) 922214006Smarcel break; 923214006Smarcel buf[len - resid] = 0; 924214006Smarcel sbuf_printf(sb, "%s", buf); 925214006Smarcel ofs += len - resid; 926214006Smarcel } 927214006Smarcel 928214006Smarcel VOP_UNLOCK(nd.ni_vp, 0); 929214006Smarcel vn_close(nd.ni_vp, FREAD, td->td_ucred, td); 930214006Smarcel return (error); 931214006Smarcel} 932214006Smarcel 933214006Smarcelstatic void 934214006Smarcelvfs_mountroot_wait(void) 935214006Smarcel{ 936214006Smarcel struct root_hold_token *h; 937214006Smarcel struct timeval lastfail; 938214006Smarcel int curfail; 939214006Smarcel 940214006Smarcel curfail = 0; 941214006Smarcel while (1) { 942214006Smarcel DROP_GIANT(); 943214006Smarcel g_waitidle(); 944214006Smarcel PICKUP_GIANT(); 945267752Smav mtx_lock(&root_holds_mtx); 946214006Smarcel if (LIST_EMPTY(&root_holds)) { 947267752Smav mtx_unlock(&root_holds_mtx); 948214006Smarcel break; 949214006Smarcel } 950214006Smarcel if (ppsratecheck(&lastfail, &curfail, 1)) { 951214006Smarcel printf("Root mount waiting for:"); 952214006Smarcel LIST_FOREACH(h, &root_holds, list) 953214006Smarcel printf(" %s", h->who); 954214006Smarcel printf("\n"); 955214006Smarcel } 956267752Smav msleep(&root_holds, &root_holds_mtx, PZERO | PDROP, "roothold", 957214006Smarcel hz); 958214006Smarcel } 959214006Smarcel} 960214006Smarcel 961214006Smarcelvoid 962214006Smarcelvfs_mountroot(void) 963214006Smarcel{ 964214006Smarcel struct mount *mp; 965214006Smarcel struct sbuf *sb; 966214006Smarcel struct thread *td; 967214006Smarcel time_t timebase; 968214006Smarcel int error; 969214006Smarcel 970214006Smarcel td = curthread; 971214006Smarcel 972214006Smarcel vfs_mountroot_wait(); 973214006Smarcel 974214006Smarcel sb = sbuf_new_auto(); 975214006Smarcel vfs_mountroot_conf0(sb); 976214006Smarcel sbuf_finish(sb); 977214006Smarcel 978214006Smarcel error = vfs_mountroot_devfs(td, &mp); 979214006Smarcel while (!error) { 980214006Smarcel error = vfs_mountroot_parse(sb, mp); 981214006Smarcel if (!error) { 982288400Sbdrewery vfs_mountroot_shuffle(td, mp); 983288400Sbdrewery sbuf_clear(sb); 984288400Sbdrewery error = vfs_mountroot_readconf(td, sb); 985288400Sbdrewery sbuf_finish(sb); 986213365Smarcel } 987213365Smarcel } 988213365Smarcel 989214006Smarcel sbuf_delete(sb); 990213365Smarcel 991213365Smarcel /* 992214006Smarcel * Iterate over all currently mounted file systems and use 993214006Smarcel * the time stamp found to check and/or initialize the RTC. 994214006Smarcel * Call inittodr() only once and pass it the largest of the 995214006Smarcel * timestamps we encounter. 996213365Smarcel */ 997214006Smarcel timebase = 0; 998214006Smarcel mtx_lock(&mountlist_mtx); 999214006Smarcel mp = TAILQ_FIRST(&mountlist); 1000214006Smarcel while (mp != NULL) { 1001214006Smarcel if (mp->mnt_time > timebase) 1002214006Smarcel timebase = mp->mnt_time; 1003214006Smarcel mp = TAILQ_NEXT(mp, mnt_list); 1004214006Smarcel } 1005214006Smarcel mtx_unlock(&mountlist_mtx); 1006214006Smarcel inittodr(timebase); 1007213365Smarcel 1008214006Smarcel /* Keep prison0's root in sync with the global rootvnode. */ 1009214006Smarcel mtx_lock(&prison0.pr_mtx); 1010214006Smarcel prison0.pr_root = rootvnode; 1011214006Smarcel vref(prison0.pr_root); 1012214006Smarcel mtx_unlock(&prison0.pr_mtx); 1013213365Smarcel 1014267752Smav mtx_lock(&root_holds_mtx); 1015214006Smarcel atomic_store_rel_int(&root_mount_complete, 1); 1016214006Smarcel wakeup(&root_mount_complete); 1017267752Smav mtx_unlock(&root_holds_mtx); 1018248645Savg 1019248645Savg EVENTHANDLER_INVOKE(mountroot); 1020213365Smarcel} 1021213365Smarcel 1022213365Smarcelstatic struct mntarg * 1023213365Smarcelparse_mountroot_options(struct mntarg *ma, const char *options) 1024213365Smarcel{ 1025213365Smarcel char *p; 1026213365Smarcel char *name, *name_arg; 1027213365Smarcel char *val, *val_arg; 1028213365Smarcel char *opts; 1029213365Smarcel 1030213365Smarcel if (options == NULL || options[0] == '\0') 1031213365Smarcel return (ma); 1032213365Smarcel 1033213365Smarcel p = opts = strdup(options, M_MOUNT); 1034213365Smarcel if (opts == NULL) { 1035213365Smarcel return (ma); 1036214006Smarcel } 1037213365Smarcel 1038213365Smarcel while((name = strsep(&p, ",")) != NULL) { 1039213365Smarcel if (name[0] == '\0') 1040213365Smarcel break; 1041213365Smarcel 1042213365Smarcel val = strchr(name, '='); 1043213365Smarcel if (val != NULL) { 1044213365Smarcel *val = '\0'; 1045213365Smarcel ++val; 1046213365Smarcel } 1047213365Smarcel if( strcmp(name, "rw") == 0 || 1048213365Smarcel strcmp(name, "noro") == 0) { 1049213365Smarcel /* 1050213365Smarcel * The first time we mount the root file system, 1051213365Smarcel * we need to mount 'ro', so We need to ignore 1052213365Smarcel * 'rw' and 'noro' mount options. 1053213365Smarcel */ 1054213365Smarcel continue; 1055213365Smarcel } 1056213365Smarcel name_arg = strdup(name, M_MOUNT); 1057213365Smarcel val_arg = NULL; 1058214006Smarcel if (val != NULL) 1059213365Smarcel val_arg = strdup(val, M_MOUNT); 1060213365Smarcel 1061213365Smarcel ma = mount_arg(ma, name_arg, val_arg, 1062213365Smarcel (val_arg != NULL ? -1 : 0)); 1063213365Smarcel } 1064213365Smarcel free(opts, M_MOUNT); 1065213365Smarcel return (ma); 1066213365Smarcel} 1067