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

Cache object: 798f0c8e390a83ba68e6359e48464ce7


[ 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.