The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/kern/vfs_mountroot.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * SPDX-License-Identifier: BSD-3-Clause
    3  *
    4  * Copyright (c) 2010 Marcel Moolenaar
    5  * Copyright (c) 1999-2004 Poul-Henning Kamp
    6  * Copyright (c) 1999 Michael Smith
    7  * Copyright (c) 1989, 1993
    8  *      The Regents of the University of California.  All rights reserved.
    9  * (c) UNIX System Laboratories, Inc.
   10  * All or some portions of this file are derived from material licensed
   11  * to the University of California by American Telephone and Telegraph
   12  * Co. or Unix System Laboratories, Inc. and are reproduced herein with
   13  * the permission of UNIX System Laboratories, Inc.
   14  *
   15  * Redistribution and use in source and binary forms, with or without
   16  * modification, are permitted provided that the following conditions
   17  * are met:
   18  * 1. Redistributions of source code must retain the above copyright
   19  *    notice, this list of conditions and the following disclaimer.
   20  * 2. Redistributions in binary form must reproduce the above copyright
   21  *    notice, this list of conditions and the following disclaimer in the
   22  *    documentation and/or other materials provided with the distribution.
   23  * 3. Neither the name of the University nor the names of its contributors
   24  *    may be used to endorse or promote products derived from this software
   25  *    without specific prior written permission.
   26  *
   27  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   28  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   29  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   30  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   31  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   32  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   33  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   34  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   35  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   36  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   37  * SUCH DAMAGE.
   38  */
   39 
   40 #include "opt_rootdevname.h"
   41 
   42 #include <sys/cdefs.h>
   43 __FBSDID("$FreeBSD$");
   44 
   45 #include <sys/param.h>
   46 #include <sys/conf.h>
   47 #include <sys/cons.h>
   48 #include <sys/eventhandler.h>
   49 #include <sys/fcntl.h>
   50 #include <sys/jail.h>
   51 #include <sys/kernel.h>
   52 #include <sys/malloc.h>
   53 #include <sys/mdioctl.h>
   54 #include <sys/mount.h>
   55 #include <sys/mutex.h>
   56 #include <sys/namei.h>
   57 #include <sys/priv.h>
   58 #include <sys/proc.h>
   59 #include <sys/filedesc.h>
   60 #include <sys/reboot.h>
   61 #include <sys/sbuf.h>
   62 #include <sys/stat.h>
   63 #include <sys/syscallsubr.h>
   64 #include <sys/sysproto.h>
   65 #include <sys/sx.h>
   66 #include <sys/sysctl.h>
   67 #include <sys/systm.h>
   68 #include <sys/vnode.h>
   69 
   70 #include <geom/geom.h>
   71 
   72 /*
   73  * The root filesystem is detailed in the kernel environment variable
   74  * vfs.root.mountfrom, which is expected to be in the general format
   75  *
   76  * <vfsname>:[<path>][  <vfsname>:[<path>] ...]
   77  * vfsname   := the name of a VFS known to the kernel and capable
   78  *              of being mounted as root
   79  * path      := disk device name or other data used by the filesystem
   80  *              to locate its physical store
   81  *
   82  * If the environment variable vfs.root.mountfrom is a space separated list,
   83  * each list element is tried in turn and the root filesystem will be mounted
   84  * from the first one that succeeds.
   85  *
   86  * The environment variable vfs.root.mountfrom.options is a comma delimited
   87  * set of string mount options.  These mount options must be parseable
   88  * by nmount() in the kernel.
   89  */
   90 
   91 static int parse_mount(char **);
   92 static struct mntarg *parse_mountroot_options(struct mntarg *, const char *);
   93 static int sysctl_vfs_root_mount_hold(SYSCTL_HANDLER_ARGS);
   94 static void vfs_mountroot_wait(void);
   95 static int vfs_mountroot_wait_if_neccessary(const char *fs, const char *dev);
   96 
   97 /*
   98  * The vnode of the system's root (/ in the filesystem, without chroot
   99  * active.)
  100  */
  101 struct vnode *rootvnode;
  102 
  103 /*
  104  * Mount of the system's /dev.
  105  */
  106 struct mount *rootdevmp;
  107 
  108 char *rootdevnames[2] = {NULL, NULL};
  109 
  110 struct mtx root_holds_mtx;
  111 MTX_SYSINIT(root_holds, &root_holds_mtx, "root_holds", MTX_DEF);
  112 
  113 static TAILQ_HEAD(, root_hold_token)    root_holds =
  114     TAILQ_HEAD_INITIALIZER(root_holds);
  115 
  116 enum action {
  117         A_CONTINUE,
  118         A_PANIC,
  119         A_REBOOT,
  120         A_RETRY
  121 };
  122 
  123 enum rh_flags {
  124         RH_FREE,
  125         RH_ALLOC,
  126         RH_ARG,
  127 };
  128 
  129 static enum action root_mount_onfail = A_CONTINUE;
  130 
  131 static int root_mount_mddev;
  132 static int root_mount_complete;
  133 
  134 /* By default wait up to 3 seconds for devices to appear. */
  135 static int root_mount_timeout = 3;
  136 TUNABLE_INT("vfs.mountroot.timeout", &root_mount_timeout);
  137 
  138 static int root_mount_always_wait = 0;
  139 SYSCTL_INT(_vfs, OID_AUTO, root_mount_always_wait, CTLFLAG_RDTUN,
  140     &root_mount_always_wait, 0,
  141     "Wait for root mount holds even if the root device already exists");
  142 
  143 SYSCTL_PROC(_vfs, OID_AUTO, root_mount_hold,
  144     CTLTYPE_STRING | CTLFLAG_RD | CTLFLAG_MPSAFE,
  145     NULL, 0, sysctl_vfs_root_mount_hold, "A",
  146     "List of root mount hold tokens");
  147 
  148 static int
  149 sysctl_vfs_root_mount_hold(SYSCTL_HANDLER_ARGS)
  150 {
  151         struct sbuf sb;
  152         struct root_hold_token *h;
  153         int error;
  154 
  155         sbuf_new(&sb, NULL, 256, SBUF_AUTOEXTEND | SBUF_INCLUDENUL);
  156 
  157         mtx_lock(&root_holds_mtx);
  158         TAILQ_FOREACH(h, &root_holds, list) {
  159                 if (h != TAILQ_FIRST(&root_holds))
  160                         sbuf_putc(&sb, ' ');
  161                 sbuf_printf(&sb, "%s", h->who);
  162         }
  163         mtx_unlock(&root_holds_mtx);
  164 
  165         error = sbuf_finish(&sb);
  166         if (error == 0)
  167                 error = SYSCTL_OUT(req, sbuf_data(&sb), sbuf_len(&sb));
  168         sbuf_delete(&sb);
  169         return (error);
  170 }
  171 
  172 struct root_hold_token *
  173 root_mount_hold(const char *identifier)
  174 {
  175         struct root_hold_token *h;
  176 
  177         h = malloc(sizeof *h, M_DEVBUF, M_ZERO | M_WAITOK);
  178         h->flags = RH_ALLOC;
  179         h->who = identifier;
  180         mtx_lock(&root_holds_mtx);
  181         TSHOLD("root mount");
  182         TAILQ_INSERT_TAIL(&root_holds, h, list);
  183         mtx_unlock(&root_holds_mtx);
  184         return (h);
  185 }
  186 
  187 void
  188 root_mount_hold_token(const char *identifier, struct root_hold_token *h)
  189 {
  190 #ifdef INVARIANTS
  191         struct root_hold_token *t;
  192 #endif
  193 
  194         h->flags = RH_ARG;
  195         h->who = identifier;
  196         mtx_lock(&root_holds_mtx);
  197 #ifdef INVARIANTS
  198         TAILQ_FOREACH(t, &root_holds, list) {
  199                 if (t == h) {
  200                         panic("Duplicate mount hold by '%s' on %p",
  201                             identifier, h);
  202                 }
  203         }
  204 #endif
  205         TSHOLD("root mount");
  206         TAILQ_INSERT_TAIL(&root_holds, h, list);
  207         mtx_unlock(&root_holds_mtx);
  208 }
  209 
  210 void
  211 root_mount_rel(struct root_hold_token *h)
  212 {
  213 
  214         if (h == NULL || h->flags == RH_FREE)
  215                 return;
  216 
  217         mtx_lock(&root_holds_mtx);
  218         TAILQ_REMOVE(&root_holds, h, list);
  219         TSRELEASE("root mount");
  220         wakeup(&root_holds);
  221         mtx_unlock(&root_holds_mtx);
  222         if (h->flags == RH_ALLOC) {
  223                 free(h, M_DEVBUF);
  224         } else
  225                 h->flags = RH_FREE;
  226 }
  227 
  228 int
  229 root_mounted(void)
  230 {
  231 
  232         /* No mutex is acquired here because int stores are atomic. */
  233         return (root_mount_complete);
  234 }
  235 
  236 static void
  237 set_rootvnode(void)
  238 {
  239 
  240         if (VFS_ROOT(TAILQ_FIRST(&mountlist), LK_EXCLUSIVE, &rootvnode))
  241                 panic("set_rootvnode: Cannot find root vnode");
  242 
  243         VOP_UNLOCK(rootvnode);
  244 
  245         pwd_set_rootvnode();
  246 }
  247 
  248 static int
  249 vfs_mountroot_devfs(struct thread *td, struct mount **mpp)
  250 {
  251         struct vfsoptlist *opts;
  252         struct vfsconf *vfsp;
  253         struct mount *mp;
  254         int error;
  255 
  256         *mpp = NULL;
  257 
  258         if (rootdevmp != NULL) {
  259                 /*
  260                  * Already have /dev; this happens during rerooting.
  261                  */
  262                 error = vfs_busy(rootdevmp, 0);
  263                 if (error != 0)
  264                         return (error);
  265                 *mpp = rootdevmp;
  266         } else {
  267                 vfsp = vfs_byname("devfs");
  268                 KASSERT(vfsp != NULL, ("Could not find devfs by name"));
  269                 if (vfsp == NULL)
  270                         return (ENOENT);
  271 
  272                 mp = vfs_mount_alloc(NULLVP, vfsp, "/dev", td->td_ucred);
  273 
  274                 error = VFS_MOUNT(mp);
  275                 KASSERT(error == 0, ("VFS_MOUNT(devfs) failed %d", error));
  276                 if (error)
  277                         return (error);
  278 
  279                 error = VFS_STATFS(mp, &mp->mnt_stat);
  280                 KASSERT(error == 0, ("VFS_STATFS(devfs) failed %d", error));
  281                 if (error)
  282                         return (error);
  283 
  284                 opts = malloc(sizeof(struct vfsoptlist), M_MOUNT, M_WAITOK);
  285                 TAILQ_INIT(opts);
  286                 mp->mnt_opt = opts;
  287 
  288                 mtx_lock(&mountlist_mtx);
  289                 TAILQ_INSERT_HEAD(&mountlist, mp, mnt_list);
  290                 mtx_unlock(&mountlist_mtx);
  291 
  292                 *mpp = mp;
  293                 rootdevmp = mp;
  294                 vfs_op_exit(mp);
  295         }
  296 
  297         set_rootvnode();
  298 
  299         error = kern_symlinkat(td, "/", AT_FDCWD, "dev", UIO_SYSSPACE);
  300         if (error)
  301                 printf("kern_symlink /dev -> / returns %d\n", error);
  302 
  303         return (error);
  304 }
  305 
  306 static void
  307 vfs_mountroot_shuffle(struct thread *td, struct mount *mpdevfs)
  308 {
  309         struct nameidata nd;
  310         struct mount *mporoot, *mpnroot;
  311         struct vnode *vp, *vporoot, *vpdevfs;
  312         char *fspath;
  313         int error;
  314 
  315         mpnroot = TAILQ_NEXT(mpdevfs, mnt_list);
  316 
  317         /* Shuffle the mountlist. */
  318         mtx_lock(&mountlist_mtx);
  319         mporoot = TAILQ_FIRST(&mountlist);
  320         TAILQ_REMOVE(&mountlist, mpdevfs, mnt_list);
  321         if (mporoot != mpdevfs) {
  322                 TAILQ_REMOVE(&mountlist, mpnroot, mnt_list);
  323                 TAILQ_INSERT_HEAD(&mountlist, mpnroot, mnt_list);
  324         }
  325         TAILQ_INSERT_TAIL(&mountlist, mpdevfs, mnt_list);
  326         mtx_unlock(&mountlist_mtx);
  327 
  328         cache_purgevfs(mporoot);
  329         if (mporoot != mpdevfs)
  330                 cache_purgevfs(mpdevfs);
  331 
  332         if (VFS_ROOT(mporoot, LK_EXCLUSIVE, &vporoot))
  333                 panic("vfs_mountroot_shuffle: Cannot find root vnode");
  334 
  335         VI_LOCK(vporoot);
  336         vporoot->v_iflag &= ~VI_MOUNT;
  337         vn_irflag_unset_locked(vporoot, VIRF_MOUNTPOINT);
  338         vporoot->v_mountedhere = NULL;
  339         VI_UNLOCK(vporoot);
  340         mporoot->mnt_flag &= ~MNT_ROOTFS;
  341         mporoot->mnt_vnodecovered = NULL;
  342         vput(vporoot);
  343 
  344         /* Set up the new rootvnode, and purge the cache */
  345         mpnroot->mnt_vnodecovered = NULL;
  346         set_rootvnode();
  347         cache_purgevfs(rootvnode->v_mount);
  348 
  349         if (mporoot != mpdevfs) {
  350                 /* Remount old root under /.mount or /mnt */
  351                 fspath = "/.mount";
  352                 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE,
  353                     fspath, td);
  354                 error = namei(&nd);
  355                 if (error) {
  356                         NDFREE(&nd, NDF_ONLY_PNBUF);
  357                         fspath = "/mnt";
  358                         NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE,
  359                             fspath, td);
  360                         error = namei(&nd);
  361                 }
  362                 if (!error) {
  363                         vp = nd.ni_vp;
  364                         error = (vp->v_type == VDIR) ? 0 : ENOTDIR;
  365                         if (!error)
  366                                 error = vinvalbuf(vp, V_SAVE, 0, 0);
  367                         if (!error) {
  368                                 cache_purge(vp);
  369                                 VI_LOCK(vp);
  370                                 mporoot->mnt_vnodecovered = vp;
  371                                 vn_irflag_set_locked(vp, VIRF_MOUNTPOINT);
  372                                 vp->v_mountedhere = mporoot;
  373                                 strlcpy(mporoot->mnt_stat.f_mntonname,
  374                                     fspath, MNAMELEN);
  375                                 VI_UNLOCK(vp);
  376                                 VOP_UNLOCK(vp);
  377                         } else
  378                                 vput(vp);
  379                 }
  380                 NDFREE(&nd, NDF_ONLY_PNBUF);
  381 
  382                 if (error)
  383                         printf("mountroot: unable to remount previous root "
  384                             "under /.mount or /mnt (error %d)\n", error);
  385         }
  386 
  387         /* Remount devfs under /dev */
  388         NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, "/dev", td);
  389         error = namei(&nd);
  390         if (!error) {
  391                 vp = nd.ni_vp;
  392                 error = (vp->v_type == VDIR) ? 0 : ENOTDIR;
  393                 if (!error)
  394                         error = vinvalbuf(vp, V_SAVE, 0, 0);
  395                 if (!error) {
  396                         vpdevfs = mpdevfs->mnt_vnodecovered;
  397                         if (vpdevfs != NULL) {
  398                                 cache_purge(vpdevfs);
  399                                 VI_LOCK(vpdevfs);
  400                                 vn_irflag_unset_locked(vpdevfs, VIRF_MOUNTPOINT);
  401                                 vpdevfs->v_mountedhere = NULL;
  402                                 VI_UNLOCK(vpdevfs);
  403                                 vrele(vpdevfs);
  404                         }
  405                         VI_LOCK(vp);
  406                         mpdevfs->mnt_vnodecovered = vp;
  407                         vn_irflag_set_locked(vp, VIRF_MOUNTPOINT);
  408                         vp->v_mountedhere = mpdevfs;
  409                         VI_UNLOCK(vp);
  410                         VOP_UNLOCK(vp);
  411                 } else
  412                         vput(vp);
  413         }
  414         if (error)
  415                 printf("mountroot: unable to remount devfs under /dev "
  416                     "(error %d)\n", error);
  417         NDFREE(&nd, NDF_ONLY_PNBUF);
  418 
  419         if (mporoot == mpdevfs) {
  420                 vfs_unbusy(mpdevfs);
  421                 /* Unlink the no longer needed /dev/dev -> / symlink */
  422                 error = kern_funlinkat(td, AT_FDCWD, "/dev/dev", FD_NONE,
  423                     UIO_SYSSPACE, 0, 0);
  424                 if (error)
  425                         printf("mountroot: unable to unlink /dev/dev "
  426                             "(error %d)\n", error);
  427         }
  428 }
  429 
  430 /*
  431  * Configuration parser.
  432  */
  433 
  434 /* Parser character classes. */
  435 #define CC_WHITESPACE           -1
  436 #define CC_NONWHITESPACE        -2
  437 
  438 /* Parse errors. */
  439 #define PE_EOF                  -1
  440 #define PE_EOL                  -2
  441 
  442 static __inline int
  443 parse_peek(char **conf)
  444 {
  445 
  446         return (**conf);
  447 }
  448 
  449 static __inline void
  450 parse_poke(char **conf, int c)
  451 {
  452 
  453         **conf = c;
  454 }
  455 
  456 static __inline void
  457 parse_advance(char **conf)
  458 {
  459 
  460         (*conf)++;
  461 }
  462 
  463 static int
  464 parse_skipto(char **conf, int mc)
  465 {
  466         int c, match;
  467 
  468         while (1) {
  469                 c = parse_peek(conf);
  470                 if (c == 0)
  471                         return (PE_EOF);
  472                 switch (mc) {
  473                 case CC_WHITESPACE:
  474                         match = (c == ' ' || c == '\t' || c == '\n') ? 1 : 0;
  475                         break;
  476                 case CC_NONWHITESPACE:
  477                         if (c == '\n')
  478                                 return (PE_EOL);
  479                         match = (c != ' ' && c != '\t') ? 1 : 0;
  480                         break;
  481                 default:
  482                         match = (c == mc) ? 1 : 0;
  483                         break;
  484                 }
  485                 if (match)
  486                         break;
  487                 parse_advance(conf);
  488         }
  489         return (0);
  490 }
  491 
  492 static int
  493 parse_token(char **conf, char **tok)
  494 {
  495         char *p;
  496         size_t len;
  497         int error;
  498 
  499         *tok = NULL;
  500         error = parse_skipto(conf, CC_NONWHITESPACE);
  501         if (error)
  502                 return (error);
  503         p = *conf;
  504         error = parse_skipto(conf, CC_WHITESPACE);
  505         len = *conf - p;
  506         *tok = malloc(len + 1, M_TEMP, M_WAITOK | M_ZERO);
  507         bcopy(p, *tok, len);
  508         return (0);
  509 }
  510 
  511 static void
  512 parse_dir_ask_printenv(const char *var)
  513 {
  514         char *val;
  515 
  516         val = kern_getenv(var);
  517         if (val != NULL) {
  518                 printf("  %s=%s\n", var, val);
  519                 freeenv(val);
  520         }
  521 }
  522 
  523 static int
  524 parse_dir_ask(char **conf)
  525 {
  526         char name[80];
  527         char *mnt;
  528         int error;
  529 
  530         vfs_mountroot_wait();
  531 
  532         printf("\nLoader variables:\n");
  533         parse_dir_ask_printenv("vfs.root.mountfrom");
  534         parse_dir_ask_printenv("vfs.root.mountfrom.options");
  535 
  536         printf("\nManual root filesystem specification:\n");
  537         printf("  <fstype>:<device> [options]\n");
  538         printf("      Mount <device> using filesystem <fstype>\n");
  539         printf("      and with the specified (optional) option list.\n");
  540         printf("\n");
  541         printf("    eg. ufs:/dev/da0s1a\n");
  542         printf("        zfs:zroot/ROOT/default\n");
  543         printf("        cd9660:/dev/cd0 ro\n");
  544         printf("          (which is equivalent to: ");
  545         printf("mount -t cd9660 -o ro /dev/cd0 /)\n");
  546         printf("\n");
  547         printf("  ?               List valid disk boot devices\n");
  548         printf("  .               Yield 1 second (for background tasks)\n");
  549         printf("  <empty line>    Abort manual input\n");
  550 
  551         do {
  552                 error = EINVAL;
  553                 printf("\nmountroot> ");
  554                 cngets(name, sizeof(name), GETS_ECHO);
  555                 if (name[0] == '\0')
  556                         break;
  557                 if (name[0] == '?' && name[1] == '\0') {
  558                         printf("\nList of GEOM managed disk devices:\n  ");
  559                         g_dev_print();
  560                         continue;
  561                 }
  562                 if (name[0] == '.' && name[1] == '\0') {
  563                         pause("rmask", hz);
  564                         continue;
  565                 }
  566                 mnt = name;
  567                 error = parse_mount(&mnt);
  568                 if (error == -1)
  569                         printf("Invalid file system specification.\n");
  570         } while (error != 0);
  571 
  572         return (error);
  573 }
  574 
  575 static int
  576 parse_dir_md(char **conf)
  577 {
  578         struct stat sb;
  579         struct thread *td;
  580         struct md_ioctl *mdio;
  581         char *path, *tok;
  582         int error, fd, len;
  583 
  584         td = curthread;
  585         fd = -1;
  586 
  587         error = parse_token(conf, &tok);
  588         if (error)
  589                 return (error);
  590 
  591         len = strlen(tok);
  592         mdio = malloc(sizeof(*mdio) + len + 1, M_TEMP, M_WAITOK | M_ZERO);
  593         path = (void *)(mdio + 1);
  594         bcopy(tok, path, len);
  595         free(tok, M_TEMP);
  596 
  597         /* Get file status. */
  598         error = kern_statat(td, 0, AT_FDCWD, path, UIO_SYSSPACE, &sb, NULL);
  599         if (error)
  600                 goto out;
  601 
  602         /* Open /dev/mdctl so that we can attach/detach. */
  603         error = kern_openat(td, AT_FDCWD, "/dev/" MDCTL_NAME, UIO_SYSSPACE,
  604             O_RDWR, 0);
  605         if (error)
  606                 goto out;
  607 
  608         fd = td->td_retval[0];
  609         mdio->md_version = MDIOVERSION;
  610         mdio->md_type = MD_VNODE;
  611 
  612         if (root_mount_mddev != -1) {
  613                 mdio->md_unit = root_mount_mddev;
  614                 (void)kern_ioctl(td, fd, MDIOCDETACH, (void *)mdio);
  615                 /* Ignore errors. We don't care. */
  616                 root_mount_mddev = -1;
  617         }
  618 
  619         mdio->md_file = (void *)(mdio + 1);
  620         mdio->md_options = MD_AUTOUNIT | MD_READONLY;
  621         mdio->md_mediasize = sb.st_size;
  622         mdio->md_unit = 0;
  623         error = kern_ioctl(td, fd, MDIOCATTACH, (void *)mdio);
  624         if (error)
  625                 goto out;
  626 
  627         if (mdio->md_unit > 9) {
  628                 printf("rootmount: too many md units\n");
  629                 mdio->md_file = NULL;
  630                 mdio->md_options = 0;
  631                 mdio->md_mediasize = 0;
  632                 error = kern_ioctl(td, fd, MDIOCDETACH, (void *)mdio);
  633                 /* Ignore errors. We don't care. */
  634                 error = ERANGE;
  635                 goto out;
  636         }
  637 
  638         root_mount_mddev = mdio->md_unit;
  639         printf(MD_NAME "%u attached to %s\n", root_mount_mddev, mdio->md_file);
  640 
  641  out:
  642         if (fd >= 0)
  643                 (void)kern_close(td, fd);
  644         free(mdio, M_TEMP);
  645         return (error);
  646 }
  647 
  648 static int
  649 parse_dir_onfail(char **conf)
  650 {
  651         char *action;
  652         int error;
  653 
  654         error = parse_token(conf, &action);
  655         if (error)
  656                 return (error);
  657 
  658         if (!strcmp(action, "continue"))
  659                 root_mount_onfail = A_CONTINUE;
  660         else if (!strcmp(action, "panic"))
  661                 root_mount_onfail = A_PANIC;
  662         else if (!strcmp(action, "reboot"))
  663                 root_mount_onfail = A_REBOOT;
  664         else if (!strcmp(action, "retry"))
  665                 root_mount_onfail = A_RETRY;
  666         else {
  667                 printf("rootmount: %s: unknown action\n", action);
  668                 error = EINVAL;
  669         }
  670 
  671         free(action, M_TEMP);
  672         return (0);
  673 }
  674 
  675 static int
  676 parse_dir_timeout(char **conf)
  677 {
  678         char *tok, *endtok;
  679         long secs;
  680         int error;
  681 
  682         error = parse_token(conf, &tok);
  683         if (error)
  684                 return (error);
  685 
  686         secs = strtol(tok, &endtok, 0);
  687         error = (secs < 0 || *endtok != '\0') ? EINVAL : 0;
  688         if (!error)
  689                 root_mount_timeout = secs;
  690         free(tok, M_TEMP);
  691         return (error);
  692 }
  693 
  694 static int
  695 parse_directive(char **conf)
  696 {
  697         char *dir;
  698         int error;
  699 
  700         error = parse_token(conf, &dir);
  701         if (error)
  702                 return (error);
  703 
  704         if (strcmp(dir, ".ask") == 0)
  705                 error = parse_dir_ask(conf);
  706         else if (strcmp(dir, ".md") == 0)
  707                 error = parse_dir_md(conf);
  708         else if (strcmp(dir, ".onfail") == 0)
  709                 error = parse_dir_onfail(conf);
  710         else if (strcmp(dir, ".timeout") == 0)
  711                 error = parse_dir_timeout(conf);
  712         else {
  713                 printf("mountroot: invalid directive `%s'\n", dir);
  714                 /* Ignore the rest of the line. */
  715                 (void)parse_skipto(conf, '\n');
  716                 error = EINVAL;
  717         }
  718         free(dir, M_TEMP);
  719         return (error);
  720 }
  721 
  722 static int
  723 parse_mount_dev_present(const char *dev)
  724 {
  725         struct nameidata nd;
  726         int error;
  727 
  728         NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, dev, curthread);
  729         error = namei(&nd);
  730         if (!error)
  731                 vput(nd.ni_vp);
  732         NDFREE(&nd, NDF_ONLY_PNBUF);
  733         return (error != 0) ? 0 : 1;
  734 }
  735 
  736 #define ERRMSGL 255
  737 static int
  738 parse_mount(char **conf)
  739 {
  740         char *errmsg;
  741         struct mntarg *ma;
  742         char *dev, *fs, *opts, *tok;
  743         int delay, error, timeout;
  744 
  745         error = parse_token(conf, &tok);
  746         if (error)
  747                 return (error);
  748         fs = tok;
  749         error = parse_skipto(&tok, ':');
  750         if (error) {
  751                 free(fs, M_TEMP);
  752                 return (error);
  753         }
  754         parse_poke(&tok, '\0');
  755         parse_advance(&tok);
  756         dev = tok;
  757 
  758         if (root_mount_mddev != -1) {
  759                 /* Handle substitution for the md unit number. */
  760                 tok = strstr(dev, "md#");
  761                 if (tok != NULL)
  762                         tok[2] = '' + root_mount_mddev;
  763         }
  764 
  765         /* Parse options. */
  766         error = parse_token(conf, &tok);
  767         opts = (error == 0) ? tok : NULL;
  768 
  769         printf("Trying to mount root from %s:%s [%s]...\n", fs, dev,
  770             (opts != NULL) ? opts : "");
  771 
  772         errmsg = malloc(ERRMSGL, M_TEMP, M_WAITOK | M_ZERO);
  773 
  774         if (vfs_byname(fs) == NULL) {
  775                 strlcpy(errmsg, "unknown file system", ERRMSGL);
  776                 error = ENOENT;
  777                 goto out;
  778         }
  779 
  780         error = vfs_mountroot_wait_if_neccessary(fs, dev);
  781         if (error != 0)
  782                 goto out;
  783 
  784         delay = hz / 10;
  785         timeout = root_mount_timeout * hz;
  786 
  787         for (;;) {
  788                 ma = NULL;
  789                 ma = mount_arg(ma, "fstype", fs, -1);
  790                 ma = mount_arg(ma, "fspath", "/", -1);
  791                 ma = mount_arg(ma, "from", dev, -1);
  792                 ma = mount_arg(ma, "errmsg", errmsg, ERRMSGL);
  793                 ma = mount_arg(ma, "ro", NULL, 0);
  794                 ma = parse_mountroot_options(ma, opts);
  795 
  796                 error = kernel_mount(ma, MNT_ROOTFS);
  797                 if (error == 0 || timeout <= 0)
  798                         break;
  799 
  800                 if (root_mount_timeout * hz == timeout ||
  801                     (bootverbose && timeout % hz == 0)) {
  802                         printf("Mounting from %s:%s failed with error %d; "
  803                             "retrying for %d more second%s\n", fs, dev, error,
  804                             timeout / hz, (timeout / hz > 1) ? "s" : "");
  805                 }
  806                 pause("rmretry", delay);
  807                 timeout -= delay;
  808         }
  809  out:
  810         if (error) {
  811                 printf("Mounting from %s:%s failed with error %d",
  812                     fs, dev, error);
  813                 if (errmsg[0] != '\0')
  814                         printf(": %s", errmsg);
  815                 printf(".\n");
  816         }
  817         free(fs, M_TEMP);
  818         free(errmsg, M_TEMP);
  819         if (opts != NULL)
  820                 free(opts, M_TEMP);
  821         /* kernel_mount can return -1 on error. */
  822         return ((error < 0) ? EDOOFUS : error);
  823 }
  824 #undef ERRMSGL
  825 
  826 static int
  827 vfs_mountroot_parse(struct sbuf *sb, struct mount *mpdevfs)
  828 {
  829         struct mount *mp;
  830         char *conf;
  831         int error;
  832 
  833         root_mount_mddev = -1;
  834 
  835 retry:
  836         conf = sbuf_data(sb);
  837         mp = TAILQ_NEXT(mpdevfs, mnt_list);
  838         error = (mp == NULL) ? 0 : EDOOFUS;
  839         root_mount_onfail = A_CONTINUE;
  840         while (mp == NULL) {
  841                 error = parse_skipto(&conf, CC_NONWHITESPACE);
  842                 if (error == PE_EOL) {
  843                         parse_advance(&conf);
  844                         continue;
  845                 }
  846                 if (error < 0)
  847                         break;
  848                 switch (parse_peek(&conf)) {
  849                 case '#':
  850                         error = parse_skipto(&conf, '\n');
  851                         break;
  852                 case '.':
  853                         error = parse_directive(&conf);
  854                         break;
  855                 default:
  856                         error = parse_mount(&conf);
  857                         if (error == -1) {
  858                                 printf("mountroot: invalid file system "
  859                                     "specification.\n");
  860                                 error = 0;
  861                         }
  862                         break;
  863                 }
  864                 if (error < 0)
  865                         break;
  866                 /* Ignore any trailing garbage on the line. */
  867                 if (parse_peek(&conf) != '\n') {
  868                         printf("mountroot: advancing to next directive...\n");
  869                         (void)parse_skipto(&conf, '\n');
  870                 }
  871                 mp = TAILQ_NEXT(mpdevfs, mnt_list);
  872         }
  873         if (mp != NULL)
  874                 return (0);
  875 
  876         /*
  877          * We failed to mount (a new) root.
  878          */
  879         switch (root_mount_onfail) {
  880         case A_CONTINUE:
  881                 break;
  882         case A_PANIC:
  883                 panic("mountroot: unable to (re-)mount root.");
  884                 /* NOTREACHED */
  885         case A_RETRY:
  886                 goto retry;
  887         case A_REBOOT:
  888                 kern_reboot(RB_NOSYNC);
  889                 /* NOTREACHED */
  890         }
  891 
  892         return (error);
  893 }
  894 
  895 static void
  896 vfs_mountroot_conf0(struct sbuf *sb)
  897 {
  898         char *s, *tok, *mnt, *opt;
  899         int error;
  900 
  901         sbuf_printf(sb, ".onfail panic\n");
  902         sbuf_printf(sb, ".timeout %d\n", root_mount_timeout);
  903         if (boothowto & RB_ASKNAME)
  904                 sbuf_printf(sb, ".ask\n");
  905 #ifdef ROOTDEVNAME
  906         if (boothowto & RB_DFLTROOT)
  907                 sbuf_printf(sb, "%s\n", ROOTDEVNAME);
  908 #endif
  909         if (boothowto & RB_CDROM) {
  910                 sbuf_printf(sb, "cd9660:/dev/cd0 ro\n");
  911                 sbuf_printf(sb, ".timeout 0\n");
  912                 sbuf_printf(sb, "cd9660:/dev/cd1 ro\n");
  913                 sbuf_printf(sb, ".timeout %d\n", root_mount_timeout);
  914         }
  915         s = kern_getenv("vfs.root.mountfrom");
  916         if (s != NULL) {
  917                 opt = kern_getenv("vfs.root.mountfrom.options");
  918                 tok = s;
  919                 error = parse_token(&tok, &mnt);
  920                 while (!error) {
  921                         sbuf_printf(sb, "%s %s\n", mnt,
  922                             (opt != NULL) ? opt : "");
  923                         free(mnt, M_TEMP);
  924                         error = parse_token(&tok, &mnt);
  925                 }
  926                 if (opt != NULL)
  927                         freeenv(opt);
  928                 freeenv(s);
  929         }
  930         if (rootdevnames[0] != NULL)
  931                 sbuf_printf(sb, "%s\n", rootdevnames[0]);
  932         if (rootdevnames[1] != NULL)
  933                 sbuf_printf(sb, "%s\n", rootdevnames[1]);
  934 #ifdef ROOTDEVNAME
  935         if (!(boothowto & RB_DFLTROOT))
  936                 sbuf_printf(sb, "%s\n", ROOTDEVNAME);
  937 #endif
  938         if (!(boothowto & RB_ASKNAME))
  939                 sbuf_printf(sb, ".ask\n");
  940 }
  941 
  942 static int
  943 vfs_mountroot_readconf(struct thread *td, struct sbuf *sb)
  944 {
  945         static char buf[128];
  946         struct nameidata nd;
  947         off_t ofs;
  948         ssize_t resid;
  949         int error, flags, len;
  950 
  951         NDINIT(&nd, LOOKUP, FOLLOW, UIO_SYSSPACE, "/.mount.conf", td);
  952         flags = FREAD;
  953         error = vn_open(&nd, &flags, 0, NULL);
  954         if (error)
  955                 return (error);
  956 
  957         NDFREE(&nd, NDF_ONLY_PNBUF);
  958         ofs = 0;
  959         len = sizeof(buf) - 1;
  960         while (1) {
  961                 error = vn_rdwr(UIO_READ, nd.ni_vp, buf, len, ofs,
  962                     UIO_SYSSPACE, IO_NODELOCKED, td->td_ucred,
  963                     NOCRED, &resid, td);
  964                 if (error)
  965                         break;
  966                 if (resid == len)
  967                         break;
  968                 buf[len - resid] = 0;
  969                 sbuf_printf(sb, "%s", buf);
  970                 ofs += len - resid;
  971         }
  972 
  973         VOP_UNLOCK(nd.ni_vp);
  974         vn_close(nd.ni_vp, FREAD, td->td_ucred, td);
  975         return (error);
  976 }
  977 
  978 static void
  979 vfs_mountroot_wait(void)
  980 {
  981         struct root_hold_token *h;
  982         struct timeval lastfail;
  983         int curfail;
  984 
  985         TSENTER();
  986 
  987         curfail = 0;
  988         lastfail.tv_sec = 0;
  989         ppsratecheck(&lastfail, &curfail, 1);
  990         while (1) {
  991                 g_waitidle();
  992                 mtx_lock(&root_holds_mtx);
  993                 if (TAILQ_EMPTY(&root_holds)) {
  994                         mtx_unlock(&root_holds_mtx);
  995                         break;
  996                 }
  997                 if (ppsratecheck(&lastfail, &curfail, 1)) {
  998                         printf("Root mount waiting for:");
  999                         TAILQ_FOREACH(h, &root_holds, list)
 1000                                 printf(" %s", h->who);
 1001                         printf("\n");
 1002                 }
 1003                 TSWAIT("root mount");
 1004                 msleep(&root_holds, &root_holds_mtx, PZERO | PDROP, "roothold",
 1005                     hz);
 1006                 TSUNWAIT("root mount");
 1007         }
 1008         g_waitidle();
 1009 
 1010         TSEXIT();
 1011 }
 1012 
 1013 static int
 1014 vfs_mountroot_wait_if_neccessary(const char *fs, const char *dev)
 1015 {
 1016         int delay, timeout;
 1017 
 1018         /*
 1019          * In case of ZFS and NFS we don't have a way to wait for
 1020          * specific device.  Also do the wait if the user forced that
 1021          * behaviour by setting vfs.root_mount_always_wait=1.
 1022          */
 1023         if (strcmp(fs, "zfs") == 0 || strstr(fs, "nfs") != NULL ||
 1024             dev[0] == '\0' || root_mount_always_wait != 0) {
 1025                 vfs_mountroot_wait();
 1026                 return (0);
 1027         }
 1028 
 1029         /*
 1030          * Otherwise, no point in waiting if the device is already there.
 1031          * Note that we must wait for GEOM to finish reconfiguring itself,
 1032          * eg for geom_part(4) to finish tasting.
 1033          */
 1034         g_waitidle();
 1035         if (parse_mount_dev_present(dev))
 1036                 return (0);
 1037 
 1038         /*
 1039          * No luck.  Let's wait.  This code looks weird, but it's that way
 1040          * to behave exactly as it used to work before.
 1041          */
 1042         vfs_mountroot_wait();
 1043         if (parse_mount_dev_present(dev))
 1044                 return (0);
 1045         printf("mountroot: waiting for device %s...\n", dev);
 1046         delay = hz / 10;
 1047         timeout = root_mount_timeout * hz;
 1048         do {
 1049                 pause("rmdev", delay);
 1050                 timeout -= delay;
 1051         } while (timeout > 0 && !parse_mount_dev_present(dev));
 1052 
 1053         if (timeout <= 0)
 1054                 return (ENODEV);
 1055 
 1056         return (0);
 1057 }
 1058 
 1059 void
 1060 vfs_mountroot(void)
 1061 {
 1062         struct mount *mp;
 1063         struct sbuf *sb;
 1064         struct thread *td;
 1065         time_t timebase;
 1066         int error;
 1067 
 1068         mtx_assert(&Giant, MA_NOTOWNED);
 1069 
 1070         TSENTER();
 1071 
 1072         td = curthread;
 1073 
 1074         sb = sbuf_new_auto();
 1075         vfs_mountroot_conf0(sb);
 1076         sbuf_finish(sb);
 1077 
 1078         error = vfs_mountroot_devfs(td, &mp);
 1079         while (!error) {
 1080                 error = vfs_mountroot_parse(sb, mp);
 1081                 if (!error) {
 1082                         vfs_mountroot_shuffle(td, mp);
 1083                         sbuf_clear(sb);
 1084                         error = vfs_mountroot_readconf(td, sb);
 1085                         sbuf_finish(sb);
 1086                 }
 1087         }
 1088 
 1089         sbuf_delete(sb);
 1090 
 1091         /*
 1092          * Iterate over all currently mounted file systems and use
 1093          * the time stamp found to check and/or initialize the RTC.
 1094          * Call inittodr() only once and pass it the largest of the
 1095          * timestamps we encounter.
 1096          */
 1097         timebase = 0;
 1098         mtx_lock(&mountlist_mtx);
 1099         mp = TAILQ_FIRST(&mountlist);
 1100         while (mp != NULL) {
 1101                 if (mp->mnt_time > timebase)
 1102                         timebase = mp->mnt_time;
 1103                 mp = TAILQ_NEXT(mp, mnt_list);
 1104         }
 1105         mtx_unlock(&mountlist_mtx);
 1106         inittodr(timebase);
 1107 
 1108         /* Keep prison0's root in sync with the global rootvnode. */
 1109         mtx_lock(&prison0.pr_mtx);
 1110         prison0.pr_root = rootvnode;
 1111         vref(prison0.pr_root);
 1112         mtx_unlock(&prison0.pr_mtx);
 1113 
 1114         mtx_lock(&root_holds_mtx);
 1115         atomic_store_rel_int(&root_mount_complete, 1);
 1116         wakeup(&root_mount_complete);
 1117         mtx_unlock(&root_holds_mtx);
 1118 
 1119         EVENTHANDLER_INVOKE(mountroot);
 1120 
 1121         TSEXIT();
 1122 }
 1123 
 1124 static struct mntarg *
 1125 parse_mountroot_options(struct mntarg *ma, const char *options)
 1126 {
 1127         char *p;
 1128         char *name, *name_arg;
 1129         char *val, *val_arg;
 1130         char *opts;
 1131 
 1132         if (options == NULL || options[0] == '\0')
 1133                 return (ma);
 1134 
 1135         p = opts = strdup(options, M_MOUNT);
 1136         if (opts == NULL) {
 1137                 return (ma);
 1138         }
 1139 
 1140         while((name = strsep(&p, ",")) != NULL) {
 1141                 if (name[0] == '\0')
 1142                         break;
 1143 
 1144                 val = strchr(name, '=');
 1145                 if (val != NULL) {
 1146                         *val = '\0';
 1147                         ++val;
 1148                 }
 1149                 if( strcmp(name, "rw") == 0 ||
 1150                     strcmp(name, "noro") == 0) {
 1151                         /*
 1152                          * The first time we mount the root file system,
 1153                          * we need to mount 'ro', so We need to ignore
 1154                          * 'rw' and 'noro' mount options.
 1155                          */
 1156                         continue;
 1157                 }
 1158                 name_arg = strdup(name, M_MOUNT);
 1159                 val_arg = NULL;
 1160                 if (val != NULL)
 1161                         val_arg = strdup(val, M_MOUNT);
 1162 
 1163                 ma = mount_arg(ma, name_arg, val_arg,
 1164                     (val_arg != NULL ? -1 : 0));
 1165         }
 1166         free(opts, M_MOUNT);
 1167         return (ma);
 1168 }

Cache object: bc4c1f6088b7cad0b4126cda81c8aeb0


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.