vfs_mount.c revision 162407
11590Srgrimes/*- 21590Srgrimes * Copyright (c) 1999-2004 Poul-Henning Kamp 31590Srgrimes * Copyright (c) 1999 Michael Smith 41590Srgrimes * Copyright (c) 1989, 1993 51590Srgrimes * The Regents of the University of California. All rights reserved. 61590Srgrimes * (c) UNIX System Laboratories, Inc. 71590Srgrimes * All or some portions of this file are derived from material licensed 81590Srgrimes * to the University of California by American Telephone and Telegraph 91590Srgrimes * Co. or Unix System Laboratories, Inc. and are reproduced herein with 101590Srgrimes * the permission of UNIX System Laboratories, Inc. 111590Srgrimes * 121590Srgrimes * Redistribution and use in source and binary forms, with or without 131590Srgrimes * modification, are permitted provided that the following conditions 141590Srgrimes * are met: 151590Srgrimes * 1. Redistributions of source code must retain the above copyright 161590Srgrimes * notice, this list of conditions and the following disclaimer. 171590Srgrimes * 2. Redistributions in binary form must reproduce the above copyright 181590Srgrimes * notice, this list of conditions and the following disclaimer in the 191590Srgrimes * documentation and/or other materials provided with the distribution. 201590Srgrimes * 4. Neither the name of the University nor the names of its contributors 211590Srgrimes * may be used to endorse or promote products derived from this software 221590Srgrimes * without specific prior written permission. 231590Srgrimes * 241590Srgrimes * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 251590Srgrimes * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 261590Srgrimes * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 271590Srgrimes * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 281590Srgrimes * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 291590Srgrimes * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 301590Srgrimes * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 311590Srgrimes * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 321590Srgrimes * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 331590Srgrimes * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 341590Srgrimes * SUCH DAMAGE. 351590Srgrimes */ 361590Srgrimes 371590Srgrimes#include <sys/cdefs.h> 381590Srgrimes__FBSDID("$FreeBSD: head/sys/kern/vfs_mount.c 162407 2006-09-18 15:35:22Z kib $"); 3927422Scharnier 401590Srgrimes#include <sys/param.h> 411590Srgrimes#include <sys/conf.h> 421590Srgrimes#include <sys/jail.h> 431590Srgrimes#include <sys/kernel.h> 441590Srgrimes#include <sys/libkern.h> 4527422Scharnier#include <sys/mac.h> 4623694Speter#include <sys/malloc.h> 4727422Scharnier#include <sys/mount.h> 4827422Scharnier#include <sys/mutex.h> 4927422Scharnier#include <sys/namei.h> 501590Srgrimes#include <sys/proc.h> 511590Srgrimes#include <sys/filedesc.h> 521590Srgrimes#include <sys/reboot.h> 531590Srgrimes#include <sys/syscallsubr.h> 541590Srgrimes#include <sys/sysproto.h> 551590Srgrimes#include <sys/sx.h> 561590Srgrimes#include <sys/sysctl.h> 571590Srgrimes#include <sys/sysent.h> 581590Srgrimes#include <sys/systm.h> 591590Srgrimes#include <sys/vnode.h> 6023694Speter#include <vm/uma.h> 611590Srgrimes 621590Srgrimes#include <geom/geom.h> 631590Srgrimes 641590Srgrimes#include <machine/stdarg.h> 651590Srgrimes 661590Srgrimes#include <security/audit/audit.h> 671590Srgrimes 681590Srgrimes#include "opt_rootdevname.h" 691590Srgrimes#include "opt_ddb.h" 701590Srgrimes#include "opt_mac.h" 711590Srgrimes 721590Srgrimes#ifdef DDB 731590Srgrimes#include <ddb/ddb.h> 741590Srgrimes#endif 751590Srgrimes 761590Srgrimes#define ROOTNAME "root_device" 771590Srgrimes#define VFS_MOUNTARG_SIZE_MAX (1024 * 64) 781590Srgrimes 791590Srgrimesstatic int vfs_domount(struct thread *td, const char *fstype, 801590Srgrimes char *fspath, int fsflags, void *fsdata); 811590Srgrimesstatic struct mount *vfs_mount_alloc(struct vnode *dvp, struct vfsconf *vfsp, 821590Srgrimes const char *fspath, struct thread *td); 831590Srgrimesstatic int vfs_mountroot_ask(void); 841590Srgrimesstatic int vfs_mountroot_try(const char *mountfrom); 851590Srgrimesstatic int vfs_donmount(struct thread *td, int fsflags, 861590Srgrimes struct uio *fsoptions); 871590Srgrimesstatic void free_mntarg(struct mntarg *ma); 881590Srgrimesstatic void vfs_mount_destroy(struct mount *); 891590Srgrimesstatic int vfs_getopt_pos(struct vfsoptlist *opts, const char *name); 901590Srgrimes 911590Srgrimesstatic int usermount = 0; 921590SrgrimesSYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0, 931590Srgrimes "Unprivileged users may mount and unmount file systems"); 941590Srgrimes 951590SrgrimesMALLOC_DEFINE(M_MOUNT, "mount", "vfs mount structure"); 961590SrgrimesMALLOC_DEFINE(M_VNODE_MARKER, "vnodemarker", "vnode marker"); 971590Srgrimesstatic uma_zone_t mount_zone; 981590Srgrimes 991590Srgrimes/* List of mounted filesystems. */ 1001590Srgrimesstruct mntlist mountlist = TAILQ_HEAD_INITIALIZER(mountlist); 1011590Srgrimes 1021590Srgrimes/* For any iteration/modification of mountlist */ 1031590Srgrimesstruct mtx mountlist_mtx; 1041590SrgrimesMTX_SYSINIT(mountlist, &mountlist_mtx, "mountlist", MTX_DEF); 1051590Srgrimes 1061590SrgrimesTAILQ_HEAD(vfsoptlist, vfsopt); 1071590Srgrimesstruct vfsopt { 1081590Srgrimes TAILQ_ENTRY(vfsopt) link; 1091590Srgrimes char *name; 1101590Srgrimes void *value; 1111590Srgrimes int len; 1121590Srgrimes}; 1131590Srgrimes 1141590Srgrimes/* 1151590Srgrimes * The vnode of the system's root (/ in the filesystem, without chroot 1161590Srgrimes * active.) 1171590Srgrimes */ 1181590Srgrimesstruct vnode *rootvnode; 1191590Srgrimes 1201590Srgrimes/* 1211590Srgrimes * The root filesystem is detailed in the kernel environment variable 1221590Srgrimes * vfs.root.mountfrom, which is expected to be in the general format 1231590Srgrimes * 1241590Srgrimes * <vfsname>:[<path>] 1251590Srgrimes * vfsname := the name of a VFS known to the kernel and capable 1261590Srgrimes * of being mounted as root 1271590Srgrimes * path := disk device name or other data used by the filesystem 1281590Srgrimes * to locate its physical store 12924360Simp */ 1301590Srgrimes 1311590Srgrimes/* 1321590Srgrimes * Global opts, taken by all filesystems 1331590Srgrimes */ 1341590Srgrimesstatic const char *global_opts[] = { 1351590Srgrimes "errmsg", 1361590Srgrimes "fstype", 1371590Srgrimes "fspath", 1381590Srgrimes "rdonly", 1391590Srgrimes "ro", 1401590Srgrimes "rw", 1411590Srgrimes "suid", 1421590Srgrimes "exec", 1431590Srgrimes "update", 1441590Srgrimes NULL 1451590Srgrimes}; 1461590Srgrimes 1471590Srgrimes/* 1481590Srgrimes * The root specifiers we will try if RB_CDROM is specified. 1491590Srgrimes */ 1501590Srgrimesstatic char *cdrom_rootdevnames[] = { 1511590Srgrimes "cd9660:cd0", 1521590Srgrimes "cd9660:acd0", 1531590Srgrimes NULL 1541590Srgrimes}; 1551590Srgrimes 1561590Srgrimes/* legacy find-root code */ 1571590Srgrimeschar *rootdevnames[2] = {NULL, NULL}; 1581590Srgrimes#ifndef ROOTDEVNAME 1591590Srgrimes# define ROOTDEVNAME NULL 1601590Srgrimes#endif 1611590Srgrimesstatic const char *ctrootdevname = ROOTDEVNAME; 1621590Srgrimes 1631590Srgrimes/* 1641590Srgrimes * --------------------------------------------------------------------- 1651590Srgrimes * Functions for building and sanitizing the mount options 1661590Srgrimes */ 1671590Srgrimes 1681590Srgrimes/* Remove one mount option. */ 1691590Srgrimesstatic void 1701590Srgrimesvfs_freeopt(struct vfsoptlist *opts, struct vfsopt *opt) 1711590Srgrimes{ 1721590Srgrimes 1731590Srgrimes TAILQ_REMOVE(opts, opt, link); 1741590Srgrimes free(opt->name, M_MOUNT); 1751590Srgrimes if (opt->value != NULL) 1761590Srgrimes free(opt->value, M_MOUNT); 1771590Srgrimes#ifdef INVARIANTS 1781590Srgrimes else if (opt->len != 0) 1791590Srgrimes panic("%s: mount option with NULL value but length != 0", 1801590Srgrimes __func__); 1811590Srgrimes#endif 1821590Srgrimes free(opt, M_MOUNT); 1831590Srgrimes} 1841590Srgrimes 1851590Srgrimes/* Release all resources related to the mount options. */ 1861590Srgrimesstatic void 1871590Srgrimesvfs_freeopts(struct vfsoptlist *opts) 1881590Srgrimes{ 1891590Srgrimes struct vfsopt *opt; 1901590Srgrimes 1911590Srgrimes while (!TAILQ_EMPTY(opts)) { 1921590Srgrimes opt = TAILQ_FIRST(opts); 1931590Srgrimes vfs_freeopt(opts, opt); 1941590Srgrimes } 1951590Srgrimes free(opts, M_MOUNT); 1961590Srgrimes} 1971590Srgrimes 1981590Srgrimes/* 1991590Srgrimes * Check if options are equal (with or without the "no" prefix). 2001590Srgrimes */ 2011590Srgrimesstatic int 2021590Srgrimesvfs_equalopts(const char *opt1, const char *opt2) 2031590Srgrimes{ 2041590Srgrimes 2051590Srgrimes /* "opt" vs. "opt" or "noopt" vs. "noopt" */ 2061590Srgrimes if (strcmp(opt1, opt2) == 0) 2071590Srgrimes return (1); 2081590Srgrimes /* "noopt" vs. "opt" */ 2091590Srgrimes if (strncmp(opt1, "no", 2) == 0 && strcmp(opt1 + 2, opt2) == 0) 2101590Srgrimes return (1); 2111590Srgrimes /* "opt" vs. "noopt" */ 2121590Srgrimes if (strncmp(opt2, "no", 2) == 0 && strcmp(opt1, opt2 + 2) == 0) 2131590Srgrimes return (1); 2141590Srgrimes return (0); 2151590Srgrimes} 2161590Srgrimes 2171590Srgrimes/* 2181590Srgrimes * If a mount option is specified several times, 2191590Srgrimes * (with or without the "no" prefix) only keep 2201590Srgrimes * the last occurence of it. 2211590Srgrimes */ 2221590Srgrimesstatic void 2231590Srgrimesvfs_sanitizeopts(struct vfsoptlist *opts) 2241590Srgrimes{ 2251590Srgrimes struct vfsopt *opt, *opt2, *tmp; 2261590Srgrimes 2271590Srgrimes TAILQ_FOREACH_REVERSE(opt, opts, vfsoptlist, link) { 2281590Srgrimes opt2 = TAILQ_PREV(opt, vfsoptlist, link); 2291590Srgrimes while (opt2 != NULL) { 2301590Srgrimes if (vfs_equalopts(opt->name, opt2->name)) { 2311590Srgrimes tmp = TAILQ_PREV(opt2, vfsoptlist, link); 2321590Srgrimes vfs_freeopt(opts, opt2); 2331590Srgrimes opt2 = tmp; 2341590Srgrimes } else { 2351590Srgrimes opt2 = TAILQ_PREV(opt2, vfsoptlist, link); 2361590Srgrimes } 2371590Srgrimes } 2381590Srgrimes } 2391590Srgrimes} 2401590Srgrimes 2411590Srgrimes/* 2421590Srgrimes * Build a linked list of mount options from a struct uio. 2431590Srgrimes */ 2441590Srgrimesstatic int 2451590Srgrimesvfs_buildopts(struct uio *auio, struct vfsoptlist **options) 2461590Srgrimes{ 2471590Srgrimes struct vfsoptlist *opts; 2481590Srgrimes struct vfsopt *opt; 2491590Srgrimes size_t memused; 2501590Srgrimes unsigned int i, iovcnt; 2511590Srgrimes int error, namelen, optlen; 2521590Srgrimes 2531590Srgrimes opts = malloc(sizeof(struct vfsoptlist), M_MOUNT, M_WAITOK); 2541590Srgrimes TAILQ_INIT(opts); 2551590Srgrimes memused = 0; 2561590Srgrimes iovcnt = auio->uio_iovcnt; 2571590Srgrimes for (i = 0; i < iovcnt; i += 2) { 2581590Srgrimes opt = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK); 2591590Srgrimes namelen = auio->uio_iov[i].iov_len; 2601590Srgrimes optlen = auio->uio_iov[i + 1].iov_len; 2611590Srgrimes opt->name = malloc(namelen, M_MOUNT, M_WAITOK); 2621590Srgrimes opt->value = NULL; 2631590Srgrimes opt->len = 0; 2641590Srgrimes 2651590Srgrimes /* 2661590Srgrimes * Do this early, so jumps to "bad" will free the current 2671590Srgrimes * option. 2681590Srgrimes */ 2691590Srgrimes TAILQ_INSERT_TAIL(opts, opt, link); 2701590Srgrimes memused += sizeof(struct vfsopt) + optlen + namelen; 2711590Srgrimes 2721590Srgrimes /* 2731590Srgrimes * Avoid consuming too much memory, and attempts to overflow 2741590Srgrimes * memused. 2751590Srgrimes */ 2761590Srgrimes if (memused > VFS_MOUNTARG_SIZE_MAX || 2771590Srgrimes optlen > VFS_MOUNTARG_SIZE_MAX || 2781590Srgrimes namelen > VFS_MOUNTARG_SIZE_MAX) { 2791590Srgrimes error = EINVAL; 2801590Srgrimes goto bad; 2811590Srgrimes } 2821590Srgrimes 28319069Sphk if (auio->uio_segflg == UIO_SYSSPACE) { 2841590Srgrimes bcopy(auio->uio_iov[i].iov_base, opt->name, namelen); 2851590Srgrimes } else { 2861590Srgrimes error = copyin(auio->uio_iov[i].iov_base, opt->name, 2871590Srgrimes namelen); 2881590Srgrimes if (error) 2891590Srgrimes goto bad; 2901590Srgrimes } 2911590Srgrimes /* Ensure names are null-terminated strings. */ 2921590Srgrimes if (opt->name[namelen - 1] != '\0') { 2931590Srgrimes error = EINVAL; 2941590Srgrimes goto bad; 2951590Srgrimes } 29623694Speter if (optlen != 0) { 29723694Speter opt->len = optlen; 29823694Speter opt->value = malloc(optlen, M_MOUNT, M_WAITOK); 29923694Speter if (auio->uio_segflg == UIO_SYSSPACE) { 3001590Srgrimes bcopy(auio->uio_iov[i + 1].iov_base, opt->value, 3018874Srgrimes optlen); 3021590Srgrimes } else { 3031590Srgrimes error = copyin(auio->uio_iov[i + 1].iov_base, 3041590Srgrimes opt->value, optlen); 3051590Srgrimes if (error) 3061590Srgrimes goto bad; 3071590Srgrimes } 3081590Srgrimes } 3091590Srgrimes } 31019069Sphk vfs_sanitizeopts(opts); 31119069Sphk *options = opts; 3121590Srgrimes return (0); 3131590Srgrimesbad: 3141590Srgrimes vfs_freeopts(opts); 3151590Srgrimes return (error); 3161590Srgrimes} 3171590Srgrimes 3181590Srgrimes/* 3191590Srgrimes * Merge the old mount options with the new ones passed 3201590Srgrimes * in the MNT_UPDATE case. 3211590Srgrimes */ 32223694Speterstatic void 3231590Srgrimesvfs_mergeopts(struct vfsoptlist *toopts, struct vfsoptlist *opts) 3241590Srgrimes{ 3251590Srgrimes struct vfsopt *opt, *opt2, *new; 3261590Srgrimes 3271590Srgrimes TAILQ_FOREACH(opt, opts, link) { 3281590Srgrimes /* 3291590Srgrimes * Check that this option hasn't been redefined 3301590Srgrimes * nor cancelled with a "no" mount option. 3311590Srgrimes */ 3321590Srgrimes opt2 = TAILQ_FIRST(toopts); 3331590Srgrimes while (opt2 != NULL) { 3341590Srgrimes if (strcmp(opt2->name, opt->name) == 0) 3351590Srgrimes goto next; 3361590Srgrimes if (strncmp(opt2->name, "no", 2) == 0 && 3371590Srgrimes strcmp(opt2->name + 2, opt->name) == 0) { 3381590Srgrimes vfs_freeopt(toopts, opt2); 3391590Srgrimes goto next; 3401590Srgrimes } 3411590Srgrimes opt2 = TAILQ_NEXT(opt2, link); 3421590Srgrimes } 3431590Srgrimes /* We want this option, duplicate it. */ 3441590Srgrimes new = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK); 3451590Srgrimes new->name = malloc(strlen(opt->name) + 1, M_MOUNT, M_WAITOK); 3461590Srgrimes strcpy(new->name, opt->name); 3471590Srgrimes if (opt->len != 0) { 3481590Srgrimes new->value = malloc(opt->len, M_MOUNT, M_WAITOK); 3491590Srgrimes bcopy(opt->value, new->value, opt->len); 3501590Srgrimes } else { 3511590Srgrimes new->value = NULL; 3521590Srgrimes } 3531590Srgrimes new->len = opt->len; 3541590Srgrimes TAILQ_INSERT_TAIL(toopts, new, link); 3551590Srgrimesnext: 3561590Srgrimes continue; 3571590Srgrimes } 3581590Srgrimes} 3591590Srgrimes 3601590Srgrimes/* 3611590Srgrimes * --------------------------------------------------------------------- 3621590Srgrimes * Mount a filesystem 3631590Srgrimes */ 36421811Sjoergint 36523694Speternmount(td, uap) 36621811Sjoerg struct thread *td; 3671590Srgrimes struct nmount_args /* { 3681590Srgrimes struct iovec *iovp; 3691590Srgrimes unsigned int iovcnt; 3701590Srgrimes int flags; 3711590Srgrimes } */ *uap; 3721590Srgrimes{ 3731590Srgrimes struct uio *auio; 3741590Srgrimes struct iovec *iov; 3751590Srgrimes unsigned int i; 3761590Srgrimes int error; 3771590Srgrimes u_int iovcnt; 3781590Srgrimes 3791590Srgrimes AUDIT_ARG(fflags, uap->flags); 3801590Srgrimes 3811590Srgrimes /* Kick out MNT_ROOTFS early as it is legal internally */ 3821590Srgrimes if (uap->flags & MNT_ROOTFS) 3831590Srgrimes return (EINVAL); 3841590Srgrimes 3851590Srgrimes iovcnt = uap->iovcnt; 3861590Srgrimes /* 3871590Srgrimes * Check that we have an even number of iovec's 3881590Srgrimes * and that we have at least two options. 3891590Srgrimes */ 3901590Srgrimes if ((iovcnt & 1) || (iovcnt < 4)) 3911590Srgrimes return (EINVAL); 3921590Srgrimes 3931590Srgrimes error = copyinuio(uap->iovp, iovcnt, &auio); 3941590Srgrimes if (error) 3951590Srgrimes return (error); 3961590Srgrimes iov = auio->uio_iov; 3971590Srgrimes for (i = 0; i < iovcnt; i++) { 3981590Srgrimes if (iov->iov_len > MMAXOPTIONLEN) { 3991590Srgrimes free(auio, M_IOV); 4001590Srgrimes return (EINVAL); 4011590Srgrimes } 4021590Srgrimes iov++; 4031590Srgrimes } 4041590Srgrimes error = vfs_donmount(td, uap->flags, auio); 4051590Srgrimes 4061590Srgrimes free(auio, M_IOV); 4071590Srgrimes return (error); 4081590Srgrimes} 4091590Srgrimes 4101590Srgrimes/* 4111590Srgrimes * --------------------------------------------------------------------- 4121590Srgrimes * Various utility functions 4131590Srgrimes */ 4141590Srgrimes 4151590Srgrimesvoid 4161590Srgrimesvfs_ref(struct mount *mp) 4171590Srgrimes{ 4181590Srgrimes 4191590Srgrimes MNT_ILOCK(mp); 4201590Srgrimes MNT_REF(mp); 4211590Srgrimes MNT_IUNLOCK(mp); 4221590Srgrimes} 4231590Srgrimes 4241590Srgrimesvoid 4251590Srgrimesvfs_rel(struct mount *mp) 4261590Srgrimes{ 4271590Srgrimes 4281590Srgrimes MNT_ILOCK(mp); 4291590Srgrimes MNT_REL(mp); 4301590Srgrimes MNT_IUNLOCK(mp); 4311590Srgrimes} 4321590Srgrimes 4331590Srgrimesstatic int 4341590Srgrimesmount_init(void *mem, int size, int flags) 4351590Srgrimes{ 4361590Srgrimes struct mount *mp; 4371590Srgrimes 4381590Srgrimes mp = (struct mount *)mem; 4391590Srgrimes mtx_init(&mp->mnt_mtx, "struct mount mtx", NULL, MTX_DEF); 4401590Srgrimes lockinit(&mp->mnt_lock, PVFS, "vfslock", 0, 0); 4411590Srgrimes return (0); 4421590Srgrimes} 4431590Srgrimes 4441590Srgrimesstatic void 4451590Srgrimesmount_fini(void *mem, int size) 4461590Srgrimes{ 4471590Srgrimes struct mount *mp; 4481590Srgrimes 4491590Srgrimes mp = (struct mount *)mem; 4501590Srgrimes lockdestroy(&mp->mnt_lock); 4511590Srgrimes mtx_destroy(&mp->mnt_mtx); 4521590Srgrimes} 4531590Srgrimes 4541590Srgrimes/* 4551590Srgrimes * Allocate and initialize the mount point struct. 4561590Srgrimes */ 4571590Srgrimesstatic struct mount * 4581590Srgrimesvfs_mount_alloc(struct vnode *vp, struct vfsconf *vfsp, 4591590Srgrimes const char *fspath, struct thread *td) 4601590Srgrimes{ 4611590Srgrimes struct mount *mp; 46221811Sjoerg 4631590Srgrimes mp = uma_zalloc(mount_zone, M_WAITOK); 4641590Srgrimes bzero(&mp->mnt_startzero, 4651590Srgrimes __rangeof(struct mount, mnt_startzero, mnt_endzero)); 4661590Srgrimes TAILQ_INIT(&mp->mnt_nvnodelist); 4671590Srgrimes mp->mnt_nvnodelistsize = 0; 4681590Srgrimes mp->mnt_ref = 0; 4691590Srgrimes (void) vfs_busy(mp, LK_NOWAIT, 0, td); 4701590Srgrimes mp->mnt_op = vfsp->vfc_vfsops; 4711590Srgrimes mp->mnt_vfc = vfsp; 4721590Srgrimes vfsp->vfc_refcount++; /* XXX Unlocked */ 4731590Srgrimes mp->mnt_stat.f_type = vfsp->vfc_typenum; 4741590Srgrimes mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK; 4751590Srgrimes strlcpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN); 4761590Srgrimes mp->mnt_vnodecovered = vp; 4771590Srgrimes mp->mnt_cred = crdup(td->td_ucred); 4781590Srgrimes mp->mnt_stat.f_owner = td->td_ucred->cr_uid; 4791590Srgrimes strlcpy(mp->mnt_stat.f_mntonname, fspath, MNAMELEN); 4801590Srgrimes mp->mnt_iosize_max = DFLTPHYS; 4811590Srgrimes#ifdef MAC 4821590Srgrimes mac_init_mount(mp); 4831590Srgrimes mac_create_mount(td->td_ucred, mp); 4841590Srgrimes#endif 48523694Speter arc4rand(&mp->mnt_hashseed, sizeof mp->mnt_hashseed, 0); 4861590Srgrimes return (mp); 4871590Srgrimes} 4881590Srgrimes 4891590Srgrimes/* 4901590Srgrimes * Destroy the mount struct previously allocated by vfs_mount_alloc(). 4911590Srgrimes */ 4921590Srgrimesstatic void 4931590Srgrimesvfs_mount_destroy(struct mount *mp) 4941590Srgrimes{ 4951590Srgrimes int i; 4961590Srgrimes 4971590Srgrimes MNT_ILOCK(mp); 4981590Srgrimes for (i = 0; mp->mnt_ref && i < 3; i++) 4991590Srgrimes msleep(mp, MNT_MTX(mp), PVFS, "mntref", hz); 5001590Srgrimes /* 5011590Srgrimes * This will always cause a 3 second delay in rebooting due to 5021590Srgrimes * refs on the root mountpoint that never go away. Most of these 5031590Srgrimes * are held by init which never exits. 5041590Srgrimes */ 5051590Srgrimes if (i == 3 && (!rebooting || bootverbose)) 5061590Srgrimes printf("Mount point %s had %d dangling refs\n", 5071590Srgrimes mp->mnt_stat.f_mntonname, mp->mnt_ref); 5081590Srgrimes if (mp->mnt_holdcnt != 0) { 5091590Srgrimes printf("Waiting for mount point to be unheld\n"); 5101590Srgrimes while (mp->mnt_holdcnt != 0) { 5111590Srgrimes mp->mnt_holdcntwaiters++; 5121590Srgrimes msleep(&mp->mnt_holdcnt, MNT_MTX(mp), 5131590Srgrimes PZERO, "mntdestroy", 0); 5141590Srgrimes mp->mnt_holdcntwaiters--; 5151590Srgrimes } 5161590Srgrimes printf("mount point unheld\n"); 5171590Srgrimes } 5181590Srgrimes if (mp->mnt_writeopcount > 0) { 5191590Srgrimes printf("Waiting for mount point write ops\n"); 5208874Srgrimes while (mp->mnt_writeopcount > 0) { 5211590Srgrimes mp->mnt_kern_flag |= MNTK_SUSPEND; 5221590Srgrimes msleep(&mp->mnt_writeopcount, 5231590Srgrimes MNT_MTX(mp), 5241590Srgrimes PZERO, "mntdestroy2", 0); 5251590Srgrimes } 5261590Srgrimes printf("mount point write ops completed\n"); 5271590Srgrimes } 5281590Srgrimes if (mp->mnt_secondary_writes > 0) { 5291590Srgrimes printf("Waiting for mount point secondary write ops\n"); 5301590Srgrimes while (mp->mnt_secondary_writes > 0) { 5311590Srgrimes mp->mnt_kern_flag |= MNTK_SUSPEND; 5321590Srgrimes msleep(&mp->mnt_secondary_writes, 5331590Srgrimes MNT_MTX(mp), 5341590Srgrimes PZERO, "mntdestroy3", 0); 5351590Srgrimes } 5361590Srgrimes printf("mount point secondary write ops completed\n"); 5371590Srgrimes } 5381590Srgrimes MNT_IUNLOCK(mp); 5391590Srgrimes mp->mnt_vfc->vfc_refcount--; 5401590Srgrimes if (!TAILQ_EMPTY(&mp->mnt_nvnodelist)) { 5411590Srgrimes struct vnode *vp; 5421590Srgrimes 5431590Srgrimes TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) 5441590Srgrimes vprint("", vp); 5451590Srgrimes panic("unmount: dangling vnode"); 5461590Srgrimes } 5471590Srgrimes MNT_ILOCK(mp); 5481590Srgrimes if (mp->mnt_kern_flag & MNTK_MWAIT) 5491590Srgrimes wakeup(mp); 5501590Srgrimes if (mp->mnt_writeopcount != 0) 5511590Srgrimes panic("vfs_mount_destroy: nonzero writeopcount"); 5521590Srgrimes if (mp->mnt_secondary_writes != 0) 5531590Srgrimes panic("vfs_mount_destroy: nonzero secondary_writes"); 5541590Srgrimes if (mp->mnt_nvnodelistsize != 0) 5551590Srgrimes panic("vfs_mount_destroy: nonzero nvnodelistsize"); 5561590Srgrimes mp->mnt_writeopcount = -1000; 5571590Srgrimes mp->mnt_nvnodelistsize = -1000; 5581590Srgrimes mp->mnt_secondary_writes = -1000; 5591590Srgrimes MNT_IUNLOCK(mp); 5601590Srgrimes#ifdef MAC 5611590Srgrimes mac_destroy_mount(mp); 5621590Srgrimes#endif 5631590Srgrimes if (mp->mnt_opt != NULL) 5641590Srgrimes vfs_freeopts(mp->mnt_opt); 5651590Srgrimes crfree(mp->mnt_cred); 5661590Srgrimes uma_zfree(mount_zone, mp); 5671590Srgrimes} 5681590Srgrimes 5691590Srgrimesstatic int 5701590Srgrimesvfs_donmount(struct thread *td, int fsflags, struct uio *fsoptions) 5711590Srgrimes{ 5721590Srgrimes struct vfsoptlist *optlist; 5731590Srgrimes struct vfsopt *opt, *noro_opt; 5741590Srgrimes char *fstype, *fspath, *errmsg; 5751590Srgrimes int error, fstypelen, fspathlen, errmsg_len, errmsg_pos; 5761590Srgrimes int has_rw, has_noro; 5771590Srgrimes 5781590Srgrimes errmsg = NULL; 5791590Srgrimes errmsg_len = 0; 5801590Srgrimes errmsg_pos = -1; 5811590Srgrimes has_rw = 0; 5821590Srgrimes has_noro = 0; 5831590Srgrimes 5841590Srgrimes error = vfs_buildopts(fsoptions, &optlist); 5851590Srgrimes if (error) 5861590Srgrimes return (error); 5871590Srgrimes 58827422Scharnier if (vfs_getopt(optlist, "errmsg", (void **)&errmsg, &errmsg_len) == 0) 58927422Scharnier errmsg_pos = vfs_getopt_pos(optlist, "errmsg"); 59027422Scharnier 59127422Scharnier /* 5921590Srgrimes * We need these two options before the others, 5931590Srgrimes * and they are mandatory for any filesystem. 594 * Ensure they are NUL terminated as well. 595 */ 596 fstypelen = 0; 597 error = vfs_getopt(optlist, "fstype", (void **)&fstype, &fstypelen); 598 if (error || fstype[fstypelen - 1] != '\0') { 599 error = EINVAL; 600 if (errmsg != NULL) 601 strncpy(errmsg, "Invalid fstype", errmsg_len); 602 goto bail; 603 } 604 fspathlen = 0; 605 error = vfs_getopt(optlist, "fspath", (void **)&fspath, &fspathlen); 606 if (error || fspath[fspathlen - 1] != '\0') { 607 error = EINVAL; 608 if (errmsg != NULL) 609 strncpy(errmsg, "Invalid fspath", errmsg_len); 610 goto bail; 611 } 612 613 /* 614 * We need to see if we have the "update" option 615 * before we call vfs_domount(), since vfs_domount() has special 616 * logic based on MNT_UPDATE. This is very important 617 * when we want to update the root filesystem. 618 */ 619 TAILQ_FOREACH(opt, optlist, link) { 620 if (strcmp(opt->name, "update") == 0) 621 fsflags |= MNT_UPDATE; 622 else if (strcmp(opt->name, "async") == 0) 623 fsflags |= MNT_ASYNC; 624 else if (strcmp(opt->name, "force") == 0) 625 fsflags |= MNT_FORCE; 626 else if (strcmp(opt->name, "multilabel") == 0) 627 fsflags |= MNT_MULTILABEL; 628 else if (strcmp(opt->name, "noasync") == 0) 629 fsflags &= ~MNT_ASYNC; 630 else if (strcmp(opt->name, "noatime") == 0) 631 fsflags |= MNT_NOATIME; 632 else if (strcmp(opt->name, "noclusterr") == 0) 633 fsflags |= MNT_NOCLUSTERR; 634 else if (strcmp(opt->name, "noclusterw") == 0) 635 fsflags |= MNT_NOCLUSTERW; 636 else if (strcmp(opt->name, "noexec") == 0) 637 fsflags |= MNT_NOEXEC; 638 else if (strcmp(opt->name, "nosuid") == 0) 639 fsflags |= MNT_NOSUID; 640 else if (strcmp(opt->name, "nosymfollow") == 0) 641 fsflags |= MNT_NOSYMFOLLOW; 642 else if (strcmp(opt->name, "noro") == 0) { 643 fsflags &= ~MNT_RDONLY; 644 has_noro = 1; 645 } 646 else if (strcmp(opt->name, "rw") == 0) { 647 fsflags &= ~MNT_RDONLY; 648 has_rw = 1; 649 } 650 else if (strcmp(opt->name, "ro") == 0 || 651 strcmp(opt->name, "rdonly") == 0) 652 fsflags |= MNT_RDONLY; 653 else if (strcmp(opt->name, "snapshot") == 0) 654 fsflags |= MNT_SNAPSHOT; 655 else if (strcmp(opt->name, "suiddir") == 0) 656 fsflags |= MNT_SUIDDIR; 657 else if (strcmp(opt->name, "sync") == 0) 658 fsflags |= MNT_SYNCHRONOUS; 659 else if (strcmp(opt->name, "union") == 0) 660 fsflags |= MNT_UNION; 661 } 662 663 /* 664 * If "rw" was specified as a mount option, and we 665 * are trying to update a mount-point from "ro" to "rw", 666 * we need a mount option "noro", since in vfs_mergeopts(), 667 * "noro" will cancel "ro", but "rw" will not do anything. 668 */ 669 if (has_rw && !has_noro) { 670 noro_opt = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK); 671 noro_opt->name = strdup("noro", M_MOUNT); 672 noro_opt->value = NULL; 673 noro_opt->len = 0; 674 TAILQ_INSERT_TAIL(optlist, noro_opt, link); 675 } 676 677 /* 678 * Be ultra-paranoid about making sure the type and fspath 679 * variables will fit in our mp buffers, including the 680 * terminating NUL. 681 */ 682 if (fstypelen >= MFSNAMELEN - 1 || fspathlen >= MNAMELEN - 1) { 683 error = ENAMETOOLONG; 684 goto bail; 685 } 686 687 mtx_lock(&Giant); 688 error = vfs_domount(td, fstype, fspath, fsflags, optlist); 689 mtx_unlock(&Giant); 690bail: 691 /* copyout the errmsg */ 692 if (errmsg_pos != -1 && ((2 * errmsg_pos + 1) < fsoptions->uio_iovcnt) 693 && errmsg_len > 0 && errmsg != NULL) { 694 if (fsoptions->uio_segflg == UIO_SYSSPACE) { 695 bcopy(errmsg, 696 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_base, 697 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_len); 698 } else { 699 copyout(errmsg, 700 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_base, 701 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_len); 702 } 703 } 704 705 if (error != 0) 706 vfs_freeopts(optlist); 707 return (error); 708} 709 710/* 711 * --------------------------------------------------------------------- 712 * Old mount API. 713 */ 714#ifndef _SYS_SYSPROTO_H_ 715struct mount_args { 716 char *type; 717 char *path; 718 int flags; 719 caddr_t data; 720}; 721#endif 722/* ARGSUSED */ 723int 724mount(td, uap) 725 struct thread *td; 726 struct mount_args /* { 727 char *type; 728 char *path; 729 int flags; 730 caddr_t data; 731 } */ *uap; 732{ 733 char *fstype; 734 struct vfsconf *vfsp = NULL; 735 struct mntarg *ma = NULL; 736 int error; 737 738 AUDIT_ARG(fflags, uap->flags); 739 740 /* Kick out MNT_ROOTFS early as it is legal internally */ 741 uap->flags &= ~MNT_ROOTFS; 742 743 fstype = malloc(MFSNAMELEN, M_TEMP, M_WAITOK); 744 error = copyinstr(uap->type, fstype, MFSNAMELEN, NULL); 745 if (error) { 746 free(fstype, M_TEMP); 747 return (error); 748 } 749 750 AUDIT_ARG(text, fstype); 751 mtx_lock(&Giant); 752 vfsp = vfs_byname_kld(fstype, td, &error); 753 free(fstype, M_TEMP); 754 if (vfsp == NULL) { 755 mtx_unlock(&Giant); 756 return (ENOENT); 757 } 758 if (vfsp->vfc_vfsops->vfs_cmount == NULL) { 759 mtx_unlock(&Giant); 760 return (EOPNOTSUPP); 761 } 762 763 ma = mount_argsu(ma, "fstype", uap->type, MNAMELEN); 764 ma = mount_argsu(ma, "fspath", uap->path, MNAMELEN); 765 ma = mount_argb(ma, uap->flags & MNT_RDONLY, "noro"); 766 ma = mount_argb(ma, !(uap->flags & MNT_NOSUID), "nosuid"); 767 ma = mount_argb(ma, !(uap->flags & MNT_NOEXEC), "noexec"); 768 769 error = vfsp->vfc_vfsops->vfs_cmount(ma, uap->data, uap->flags, td); 770 mtx_unlock(&Giant); 771 return (error); 772} 773 774 775/* 776 * vfs_domount(): actually attempt a filesystem mount. 777 */ 778static int 779vfs_domount( 780 struct thread *td, /* Calling thread. */ 781 const char *fstype, /* Filesystem type. */ 782 char *fspath, /* Mount path. */ 783 int fsflags, /* Flags common to all filesystems. */ 784 void *fsdata /* Options local to the filesystem. */ 785 ) 786{ 787 struct vnode *vp; 788 struct mount *mp; 789 struct vfsconf *vfsp; 790 struct export_args export; 791 int error, flag = 0, kern_flag = 0; 792 struct vattr va; 793 struct nameidata nd; 794 795 mtx_assert(&Giant, MA_OWNED); 796 /* 797 * Be ultra-paranoid about making sure the type and fspath 798 * variables will fit in our mp buffers, including the 799 * terminating NUL. 800 */ 801 if (strlen(fstype) >= MFSNAMELEN || strlen(fspath) >= MNAMELEN) 802 return (ENAMETOOLONG); 803 804 if (jailed(td->td_ucred)) 805 return (EPERM); 806 if (usermount == 0) { 807 if ((error = suser(td)) != 0) 808 return (error); 809 } 810 811 /* 812 * Do not allow NFS export or MNT_SUIDDIR by unprivileged users. 813 */ 814 if (fsflags & (MNT_EXPORTED | MNT_SUIDDIR)) { 815 if ((error = suser(td)) != 0) 816 return (error); 817 } 818 /* 819 * Silently enforce MNT_NOSUID and MNT_USER for 820 * unprivileged users. 821 */ 822 if (suser(td) != 0) 823 fsflags |= MNT_NOSUID | MNT_USER; 824 825 /* Load KLDs before we lock the covered vnode to avoid reversals. */ 826 vfsp = NULL; 827 if ((fsflags & MNT_UPDATE) == 0) { 828 /* Don't try to load KLDs if we're mounting the root. */ 829 if (fsflags & MNT_ROOTFS) 830 vfsp = vfs_byname(fstype); 831 else 832 vfsp = vfs_byname_kld(fstype, td, &error); 833 if (vfsp == NULL) 834 return (ENODEV); 835 } 836 /* 837 * Get vnode to be covered 838 */ 839 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_SYSSPACE, 840 fspath, td); 841 if ((error = namei(&nd)) != 0) 842 return (error); 843 NDFREE(&nd, NDF_ONLY_PNBUF); 844 vp = nd.ni_vp; 845 if (fsflags & MNT_UPDATE) { 846 if ((vp->v_vflag & VV_ROOT) == 0) { 847 vput(vp); 848 return (EINVAL); 849 } 850 mp = vp->v_mount; 851 flag = mp->mnt_flag; 852 kern_flag = mp->mnt_kern_flag; 853 /* 854 * We only allow the filesystem to be reloaded if it 855 * is currently mounted read-only. 856 */ 857 if ((fsflags & MNT_RELOAD) && 858 ((mp->mnt_flag & MNT_RDONLY) == 0)) { 859 vput(vp); 860 return (EOPNOTSUPP); /* Needs translation */ 861 } 862 /* 863 * Only privileged root, or (if MNT_USER is set) the user that 864 * did the original mount is permitted to update it. 865 */ 866 error = vfs_suser(mp, td); 867 if (error) { 868 vput(vp); 869 return (error); 870 } 871 if (vfs_busy(mp, LK_NOWAIT, 0, td)) { 872 vput(vp); 873 return (EBUSY); 874 } 875 VI_LOCK(vp); 876 if ((vp->v_iflag & VI_MOUNT) != 0 || 877 vp->v_mountedhere != NULL) { 878 VI_UNLOCK(vp); 879 vfs_unbusy(mp, td); 880 vput(vp); 881 return (EBUSY); 882 } 883 vp->v_iflag |= VI_MOUNT; 884 VI_UNLOCK(vp); 885 mp->mnt_flag |= fsflags & 886 (MNT_RELOAD | MNT_FORCE | MNT_UPDATE | MNT_SNAPSHOT | MNT_ROOTFS); 887 VOP_UNLOCK(vp, 0, td); 888 mp->mnt_optnew = fsdata; 889 vfs_mergeopts(mp->mnt_optnew, mp->mnt_opt); 890 } else { 891 /* 892 * If the user is not root, ensure that they own the directory 893 * onto which we are attempting to mount. 894 */ 895 error = VOP_GETATTR(vp, &va, td->td_ucred, td); 896 if (error) { 897 vput(vp); 898 return (error); 899 } 900 if (va.va_uid != td->td_ucred->cr_uid) { 901 if ((error = suser(td)) != 0) { 902 vput(vp); 903 return (error); 904 } 905 } 906 error = vinvalbuf(vp, V_SAVE, td, 0, 0); 907 if (error != 0) { 908 vput(vp); 909 return (error); 910 } 911 if (vp->v_type != VDIR) { 912 vput(vp); 913 return (ENOTDIR); 914 } 915 VI_LOCK(vp); 916 if ((vp->v_iflag & VI_MOUNT) != 0 || 917 vp->v_mountedhere != NULL) { 918 VI_UNLOCK(vp); 919 vput(vp); 920 return (EBUSY); 921 } 922 vp->v_iflag |= VI_MOUNT; 923 VI_UNLOCK(vp); 924 925 /* 926 * Allocate and initialize the filesystem. 927 */ 928 mp = vfs_mount_alloc(vp, vfsp, fspath, td); 929 VOP_UNLOCK(vp, 0, td); 930 931 /* XXXMAC: pass to vfs_mount_alloc? */ 932 mp->mnt_optnew = fsdata; 933 } 934 935 /* 936 * Set the mount level flags. 937 */ 938 if (fsflags & MNT_RDONLY) 939 mp->mnt_flag |= MNT_RDONLY; 940 mp->mnt_flag &=~ MNT_UPDATEMASK; 941 mp->mnt_flag |= fsflags & (MNT_UPDATEMASK | MNT_FORCE | MNT_ROOTFS); 942 /* 943 * Mount the filesystem. 944 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they 945 * get. No freeing of cn_pnbuf. 946 */ 947 error = VFS_MOUNT(mp, td); 948 949 /* 950 * Process the export option only if we are 951 * updating mount options. 952 */ 953 if (!error && (fsflags & MNT_UPDATE)) { 954 if (vfs_copyopt(mp->mnt_optnew, "export", &export, 955 sizeof(export)) == 0) 956 error = vfs_export(mp, &export); 957 } 958 959 if (!error) { 960 if (mp->mnt_opt != NULL) 961 vfs_freeopts(mp->mnt_opt); 962 mp->mnt_opt = mp->mnt_optnew; 963 (void)VFS_STATFS(mp, &mp->mnt_stat, td); 964 } 965 /* 966 * Prevent external consumers of mount options from reading 967 * mnt_optnew. 968 */ 969 mp->mnt_optnew = NULL; 970 if (mp->mnt_flag & MNT_UPDATE) { 971 mp->mnt_flag &= 972 ~(MNT_UPDATE | MNT_RELOAD | MNT_FORCE | MNT_SNAPSHOT); 973 if (error) { 974 mp->mnt_flag = flag; 975 mp->mnt_kern_flag = kern_flag; 976 } 977 if ((mp->mnt_flag & MNT_RDONLY) == 0) { 978 if (mp->mnt_syncer == NULL) 979 error = vfs_allocate_syncvnode(mp); 980 } else { 981 if (mp->mnt_syncer != NULL) 982 vrele(mp->mnt_syncer); 983 mp->mnt_syncer = NULL; 984 } 985 vfs_unbusy(mp, td); 986 VI_LOCK(vp); 987 vp->v_iflag &= ~VI_MOUNT; 988 VI_UNLOCK(vp); 989 vrele(vp); 990 return (error); 991 } 992 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 993 /* 994 * Put the new filesystem on the mount list after root. 995 */ 996 cache_purge(vp); 997 if (!error) { 998 struct vnode *newdp; 999 1000 VI_LOCK(vp); 1001 vp->v_iflag &= ~VI_MOUNT; 1002 VI_UNLOCK(vp); 1003 vp->v_mountedhere = mp; 1004 mtx_lock(&mountlist_mtx); 1005 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list); 1006 mtx_unlock(&mountlist_mtx); 1007 vfs_event_signal(NULL, VQ_MOUNT, 0); 1008 if (VFS_ROOT(mp, LK_EXCLUSIVE, &newdp, td)) 1009 panic("mount: lost mount"); 1010 mountcheckdirs(vp, newdp); 1011 vput(newdp); 1012 VOP_UNLOCK(vp, 0, td); 1013 if ((mp->mnt_flag & MNT_RDONLY) == 0) 1014 error = vfs_allocate_syncvnode(mp); 1015 vfs_unbusy(mp, td); 1016 if (error) 1017 vrele(vp); 1018 } else { 1019 VI_LOCK(vp); 1020 vp->v_iflag &= ~VI_MOUNT; 1021 VI_UNLOCK(vp); 1022 vfs_unbusy(mp, td); 1023 vfs_mount_destroy(mp); 1024 vput(vp); 1025 } 1026 return (error); 1027} 1028 1029/* 1030 * --------------------------------------------------------------------- 1031 * Unmount a filesystem. 1032 * 1033 * Note: unmount takes a path to the vnode mounted on as argument, 1034 * not special file (as before). 1035 */ 1036#ifndef _SYS_SYSPROTO_H_ 1037struct unmount_args { 1038 char *path; 1039 int flags; 1040}; 1041#endif 1042/* ARGSUSED */ 1043int 1044unmount(td, uap) 1045 struct thread *td; 1046 register struct unmount_args /* { 1047 char *path; 1048 int flags; 1049 } */ *uap; 1050{ 1051 struct mount *mp; 1052 char *pathbuf; 1053 int error, id0, id1; 1054 1055 if (jailed(td->td_ucred)) 1056 return (EPERM); 1057 if (usermount == 0) { 1058 if ((error = suser(td)) != 0) 1059 return (error); 1060 } 1061 1062 pathbuf = malloc(MNAMELEN, M_TEMP, M_WAITOK); 1063 error = copyinstr(uap->path, pathbuf, MNAMELEN, NULL); 1064 if (error) { 1065 free(pathbuf, M_TEMP); 1066 return (error); 1067 } 1068 AUDIT_ARG(upath, td, pathbuf, ARG_UPATH1); 1069 mtx_lock(&Giant); 1070 if (uap->flags & MNT_BYFSID) { 1071 /* Decode the filesystem ID. */ 1072 if (sscanf(pathbuf, "FSID:%d:%d", &id0, &id1) != 2) { 1073 mtx_unlock(&Giant); 1074 free(pathbuf, M_TEMP); 1075 return (EINVAL); 1076 } 1077 1078 mtx_lock(&mountlist_mtx); 1079 TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) { 1080 if (mp->mnt_stat.f_fsid.val[0] == id0 && 1081 mp->mnt_stat.f_fsid.val[1] == id1) 1082 break; 1083 } 1084 mtx_unlock(&mountlist_mtx); 1085 } else { 1086 mtx_lock(&mountlist_mtx); 1087 TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) { 1088 if (strcmp(mp->mnt_stat.f_mntonname, pathbuf) == 0) 1089 break; 1090 } 1091 mtx_unlock(&mountlist_mtx); 1092 } 1093 free(pathbuf, M_TEMP); 1094 if (mp == NULL) { 1095 /* 1096 * Previously we returned ENOENT for a nonexistent path and 1097 * EINVAL for a non-mountpoint. We cannot tell these apart 1098 * now, so in the !MNT_BYFSID case return the more likely 1099 * EINVAL for compatibility. 1100 */ 1101 mtx_unlock(&Giant); 1102 return ((uap->flags & MNT_BYFSID) ? ENOENT : EINVAL); 1103 } 1104 1105 /* 1106 * Don't allow unmounting the root filesystem. 1107 */ 1108 if (mp->mnt_flag & MNT_ROOTFS) { 1109 mtx_unlock(&Giant); 1110 return (EINVAL); 1111 } 1112 error = dounmount(mp, uap->flags, td); 1113 mtx_unlock(&Giant); 1114 return (error); 1115} 1116 1117/* 1118 * Do the actual filesystem unmount. 1119 */ 1120int 1121dounmount(mp, flags, td) 1122 struct mount *mp; 1123 int flags; 1124 struct thread *td; 1125{ 1126 struct vnode *coveredvp, *fsrootvp; 1127 int error; 1128 int async_flag; 1129 1130 mtx_assert(&Giant, MA_OWNED); 1131 1132 if ((coveredvp = mp->mnt_vnodecovered) != NULL) { 1133 VI_LOCK(coveredvp); 1134 vholdl(coveredvp); 1135 error = vn_lock(coveredvp, LK_EXCLUSIVE | LK_INTERLOCK, td); 1136 vdrop(coveredvp); 1137 /* 1138 * Check for mp being unmounted while waiting for the 1139 * covered vnode lock. 1140 */ 1141 if (error) 1142 return (error); 1143 if (coveredvp->v_mountedhere != mp) { 1144 VOP_UNLOCK(coveredvp, 0, td); 1145 return (EBUSY); 1146 } 1147 } 1148 /* 1149 * Only privileged root, or (if MNT_USER is set) the user that did the 1150 * original mount is permitted to unmount this filesystem. 1151 */ 1152 error = vfs_suser(mp, td); 1153 if (error) { 1154 VOP_UNLOCK(coveredvp, 0, td); 1155 return (error); 1156 } 1157 1158 MNT_ILOCK(mp); 1159 if (mp->mnt_kern_flag & MNTK_UNMOUNT) { 1160 MNT_IUNLOCK(mp); 1161 if (coveredvp) 1162 VOP_UNLOCK(coveredvp, 0, td); 1163 return (EBUSY); 1164 } 1165 mp->mnt_kern_flag |= MNTK_UNMOUNT; 1166 /* Allow filesystems to detect that a forced unmount is in progress. */ 1167 if (flags & MNT_FORCE) 1168 mp->mnt_kern_flag |= MNTK_UNMOUNTF; 1169 error = lockmgr(&mp->mnt_lock, LK_DRAIN | LK_INTERLOCK | 1170 ((flags & MNT_FORCE) ? 0 : LK_NOWAIT), MNT_MTX(mp), td); 1171 if (error) { 1172 MNT_ILOCK(mp); 1173 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF); 1174 if (mp->mnt_kern_flag & MNTK_MWAIT) 1175 wakeup(mp); 1176 MNT_IUNLOCK(mp); 1177 if (coveredvp) 1178 VOP_UNLOCK(coveredvp, 0, td); 1179 return (error); 1180 } 1181 vn_start_write(NULL, &mp, V_WAIT); 1182 1183 if (mp->mnt_flag & MNT_EXPUBLIC) 1184 vfs_setpublicfs(NULL, NULL, NULL); 1185 1186 vfs_msync(mp, MNT_WAIT); 1187 async_flag = mp->mnt_flag & MNT_ASYNC; 1188 mp->mnt_flag &= ~MNT_ASYNC; 1189 cache_purgevfs(mp); /* remove cache entries for this file sys */ 1190 if (mp->mnt_syncer != NULL) 1191 vrele(mp->mnt_syncer); 1192 /* 1193 * For forced unmounts, move process cdir/rdir refs on the fs root 1194 * vnode to the covered vnode. For non-forced unmounts we want 1195 * such references to cause an EBUSY error. 1196 */ 1197 if ((flags & MNT_FORCE) && 1198 VFS_ROOT(mp, LK_EXCLUSIVE, &fsrootvp, td) == 0) { 1199 if (mp->mnt_vnodecovered != NULL) 1200 mountcheckdirs(fsrootvp, mp->mnt_vnodecovered); 1201 if (fsrootvp == rootvnode) { 1202 vrele(rootvnode); 1203 rootvnode = NULL; 1204 } 1205 vput(fsrootvp); 1206 } 1207 if (((mp->mnt_flag & MNT_RDONLY) || 1208 (error = VFS_SYNC(mp, MNT_WAIT, td)) == 0) || 1209 (flags & MNT_FORCE)) { 1210 error = VFS_UNMOUNT(mp, flags, td); 1211 } 1212 vn_finished_write(mp); 1213 if (error) { 1214 /* Undo cdir/rdir and rootvnode changes made above. */ 1215 if ((flags & MNT_FORCE) && 1216 VFS_ROOT(mp, LK_EXCLUSIVE, &fsrootvp, td) == 0) { 1217 if (mp->mnt_vnodecovered != NULL) 1218 mountcheckdirs(mp->mnt_vnodecovered, fsrootvp); 1219 if (rootvnode == NULL) { 1220 rootvnode = fsrootvp; 1221 vref(rootvnode); 1222 } 1223 vput(fsrootvp); 1224 } 1225 if ((mp->mnt_flag & MNT_RDONLY) == 0 && mp->mnt_syncer == NULL) 1226 (void) vfs_allocate_syncvnode(mp); 1227 MNT_ILOCK(mp); 1228 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF); 1229 mp->mnt_flag |= async_flag; 1230 lockmgr(&mp->mnt_lock, LK_RELEASE, NULL, td); 1231 if (mp->mnt_kern_flag & MNTK_MWAIT) 1232 wakeup(mp); 1233 MNT_IUNLOCK(mp); 1234 if (coveredvp) 1235 VOP_UNLOCK(coveredvp, 0, td); 1236 return (error); 1237 } 1238 mtx_lock(&mountlist_mtx); 1239 TAILQ_REMOVE(&mountlist, mp, mnt_list); 1240 mtx_unlock(&mountlist_mtx); 1241 if (coveredvp != NULL) { 1242 coveredvp->v_mountedhere = NULL; 1243 vput(coveredvp); 1244 } 1245 vfs_event_signal(NULL, VQ_UNMOUNT, 0); 1246 lockmgr(&mp->mnt_lock, LK_RELEASE, NULL, td); 1247 vfs_mount_destroy(mp); 1248 return (0); 1249} 1250 1251/* 1252 * --------------------------------------------------------------------- 1253 * Mounting of root filesystem 1254 * 1255 */ 1256 1257struct root_hold_token { 1258 const char *who; 1259 LIST_ENTRY(root_hold_token) list; 1260}; 1261 1262static LIST_HEAD(, root_hold_token) root_holds = 1263 LIST_HEAD_INITIALIZER(&root_holds); 1264 1265struct root_hold_token * 1266root_mount_hold(const char *identifier) 1267{ 1268 struct root_hold_token *h; 1269 1270 h = malloc(sizeof *h, M_DEVBUF, M_ZERO | M_WAITOK); 1271 h->who = identifier; 1272 mtx_lock(&mountlist_mtx); 1273 LIST_INSERT_HEAD(&root_holds, h, list); 1274 mtx_unlock(&mountlist_mtx); 1275 return (h); 1276} 1277 1278void 1279root_mount_rel(struct root_hold_token *h) 1280{ 1281 1282 mtx_lock(&mountlist_mtx); 1283 LIST_REMOVE(h, list); 1284 wakeup(&root_holds); 1285 mtx_unlock(&mountlist_mtx); 1286 free(h, M_DEVBUF); 1287} 1288 1289static void 1290root_mount_wait(void) 1291{ 1292 struct root_hold_token *h; 1293 1294 for (;;) { 1295 DROP_GIANT(); 1296 g_waitidle(); 1297 PICKUP_GIANT(); 1298 mtx_lock(&mountlist_mtx); 1299 if (LIST_EMPTY(&root_holds)) { 1300 mtx_unlock(&mountlist_mtx); 1301 break; 1302 } 1303 printf("Root mount waiting for:"); 1304 LIST_FOREACH(h, &root_holds, list) 1305 printf(" %s", h->who); 1306 printf("\n"); 1307 msleep(&root_holds, &mountlist_mtx, PZERO | PDROP, "roothold", 1308 hz); 1309 } 1310} 1311 1312static void 1313set_rootvnode(struct thread *td) 1314{ 1315 struct proc *p; 1316 1317 if (VFS_ROOT(TAILQ_FIRST(&mountlist), LK_EXCLUSIVE, &rootvnode, td)) 1318 panic("Cannot find root vnode"); 1319 1320 p = td->td_proc; 1321 FILEDESC_LOCK(p->p_fd); 1322 1323 if (p->p_fd->fd_cdir != NULL) 1324 vrele(p->p_fd->fd_cdir); 1325 p->p_fd->fd_cdir = rootvnode; 1326 VREF(rootvnode); 1327 1328 if (p->p_fd->fd_rdir != NULL) 1329 vrele(p->p_fd->fd_rdir); 1330 p->p_fd->fd_rdir = rootvnode; 1331 VREF(rootvnode); 1332 1333 FILEDESC_UNLOCK(p->p_fd); 1334 1335 VOP_UNLOCK(rootvnode, 0, td); 1336} 1337 1338/* 1339 * Mount /devfs as our root filesystem, but do not put it on the mountlist 1340 * yet. Create a /dev -> / symlink so that absolute pathnames will lookup. 1341 */ 1342 1343static void 1344devfs_first(void) 1345{ 1346 struct thread *td = curthread; 1347 struct vfsoptlist *opts; 1348 struct vfsconf *vfsp; 1349 struct mount *mp = NULL; 1350 int error; 1351 1352 vfsp = vfs_byname("devfs"); 1353 KASSERT(vfsp != NULL, ("Could not find devfs by name")); 1354 if (vfsp == NULL) 1355 return; 1356 1357 mp = vfs_mount_alloc(NULLVP, vfsp, "/dev", td); 1358 1359 error = VFS_MOUNT(mp, td); 1360 KASSERT(error == 0, ("VFS_MOUNT(devfs) failed %d", error)); 1361 if (error) 1362 return; 1363 1364 opts = malloc(sizeof(struct vfsoptlist), M_MOUNT, M_WAITOK); 1365 TAILQ_INIT(opts); 1366 mp->mnt_opt = opts; 1367 1368 mtx_lock(&mountlist_mtx); 1369 TAILQ_INSERT_HEAD(&mountlist, mp, mnt_list); 1370 mtx_unlock(&mountlist_mtx); 1371 1372 set_rootvnode(td); 1373 1374 error = kern_symlink(td, "/", "dev", UIO_SYSSPACE); 1375 if (error) 1376 printf("kern_symlink /dev -> / returns %d\n", error); 1377} 1378 1379/* 1380 * Surgically move our devfs to be mounted on /dev. 1381 */ 1382 1383static void 1384devfs_fixup(struct thread *td) 1385{ 1386 struct nameidata nd; 1387 int error; 1388 struct vnode *vp, *dvp; 1389 struct mount *mp; 1390 1391 /* Remove our devfs mount from the mountlist and purge the cache */ 1392 mtx_lock(&mountlist_mtx); 1393 mp = TAILQ_FIRST(&mountlist); 1394 TAILQ_REMOVE(&mountlist, mp, mnt_list); 1395 mtx_unlock(&mountlist_mtx); 1396 cache_purgevfs(mp); 1397 1398 VFS_ROOT(mp, LK_EXCLUSIVE, &dvp, td); 1399 VI_LOCK(dvp); 1400 dvp->v_iflag &= ~VI_MOUNT; 1401 dvp->v_mountedhere = NULL; 1402 VI_UNLOCK(dvp); 1403 1404 /* Set up the real rootvnode, and purge the cache */ 1405 TAILQ_FIRST(&mountlist)->mnt_vnodecovered = NULL; 1406 set_rootvnode(td); 1407 cache_purgevfs(rootvnode->v_mount); 1408 1409 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, "/dev", td); 1410 error = namei(&nd); 1411 if (error) { 1412 printf("Lookup of /dev for devfs, error: %d\n", error); 1413 return; 1414 } 1415 NDFREE(&nd, NDF_ONLY_PNBUF); 1416 vp = nd.ni_vp; 1417 if (vp->v_type != VDIR) { 1418 vput(vp); 1419 } 1420 error = vinvalbuf(vp, V_SAVE, td, 0, 0); 1421 if (error) { 1422 vput(vp); 1423 } 1424 cache_purge(vp); 1425 mp->mnt_vnodecovered = vp; 1426 vp->v_mountedhere = mp; 1427 mtx_lock(&mountlist_mtx); 1428 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list); 1429 mtx_unlock(&mountlist_mtx); 1430 VOP_UNLOCK(vp, 0, td); 1431 vput(dvp); 1432 vfs_unbusy(mp, td); 1433 1434 /* Unlink the no longer needed /dev/dev -> / symlink */ 1435 kern_unlink(td, "/dev/dev", UIO_SYSSPACE); 1436} 1437 1438/* 1439 * Report errors during filesystem mounting. 1440 */ 1441void 1442vfs_mount_error(struct mount *mp, const char *fmt, ...) 1443{ 1444 struct vfsoptlist *moptlist = mp->mnt_optnew; 1445 va_list ap; 1446 int error, len; 1447 char *errmsg; 1448 1449 error = vfs_getopt(moptlist, "errmsg", (void **)&errmsg, &len); 1450 if (error || errmsg == NULL || len <= 0) 1451 return; 1452 1453 va_start(ap, fmt); 1454 vsnprintf(errmsg, (size_t)len, fmt, ap); 1455 va_end(ap); 1456} 1457 1458/* 1459 * Find and mount the root filesystem 1460 */ 1461void 1462vfs_mountroot(void) 1463{ 1464 char *cp; 1465 int error, i, asked = 0; 1466 1467 root_mount_wait(); 1468 1469 mount_zone = uma_zcreate("Mountpoints", sizeof(struct mount), 1470 NULL, NULL, mount_init, mount_fini, 1471 UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 1472 devfs_first(); 1473 1474 /* 1475 * We are booted with instructions to prompt for the root filesystem. 1476 */ 1477 if (boothowto & RB_ASKNAME) { 1478 if (!vfs_mountroot_ask()) 1479 return; 1480 asked = 1; 1481 } 1482 1483 /* 1484 * The root filesystem information is compiled in, and we are 1485 * booted with instructions to use it. 1486 */ 1487 if (ctrootdevname != NULL && (boothowto & RB_DFLTROOT)) { 1488 if (!vfs_mountroot_try(ctrootdevname)) 1489 return; 1490 ctrootdevname = NULL; 1491 } 1492 1493 /* 1494 * We've been given the generic "use CDROM as root" flag. This is 1495 * necessary because one media may be used in many different 1496 * devices, so we need to search for them. 1497 */ 1498 if (boothowto & RB_CDROM) { 1499 for (i = 0; cdrom_rootdevnames[i] != NULL; i++) { 1500 if (!vfs_mountroot_try(cdrom_rootdevnames[i])) 1501 return; 1502 } 1503 } 1504 1505 /* 1506 * Try to use the value read by the loader from /etc/fstab, or 1507 * supplied via some other means. This is the preferred 1508 * mechanism. 1509 */ 1510 cp = getenv("vfs.root.mountfrom"); 1511 if (cp != NULL) { 1512 error = vfs_mountroot_try(cp); 1513 freeenv(cp); 1514 if (!error) 1515 return; 1516 } 1517 1518 /* 1519 * Try values that may have been computed by code during boot 1520 */ 1521 if (!vfs_mountroot_try(rootdevnames[0])) 1522 return; 1523 if (!vfs_mountroot_try(rootdevnames[1])) 1524 return; 1525 1526 /* 1527 * If we (still) have a compiled-in default, try it. 1528 */ 1529 if (ctrootdevname != NULL) 1530 if (!vfs_mountroot_try(ctrootdevname)) 1531 return; 1532 /* 1533 * Everything so far has failed, prompt on the console if we haven't 1534 * already tried that. 1535 */ 1536 if (!asked) 1537 if (!vfs_mountroot_ask()) 1538 return; 1539 1540 panic("Root mount failed, startup aborted."); 1541} 1542 1543/* 1544 * Mount (mountfrom) as the root filesystem. 1545 */ 1546static int 1547vfs_mountroot_try(const char *mountfrom) 1548{ 1549 struct mount *mp; 1550 char *vfsname, *path; 1551 time_t timebase; 1552 int error; 1553 char patt[32]; 1554 1555 vfsname = NULL; 1556 path = NULL; 1557 mp = NULL; 1558 error = EINVAL; 1559 1560 if (mountfrom == NULL) 1561 return (error); /* don't complain */ 1562 printf("Trying to mount root from %s\n", mountfrom); 1563 1564 /* parse vfs name and path */ 1565 vfsname = malloc(MFSNAMELEN, M_MOUNT, M_WAITOK); 1566 path = malloc(MNAMELEN, M_MOUNT, M_WAITOK); 1567 vfsname[0] = path[0] = 0; 1568 sprintf(patt, "%%%d[a-z0-9]:%%%ds", MFSNAMELEN, MNAMELEN); 1569 if (sscanf(mountfrom, patt, vfsname, path) < 1) 1570 goto out; 1571 1572 if (path[0] == '\0') 1573 strcpy(path, ROOTNAME); 1574 1575 error = kernel_vmount( 1576 MNT_RDONLY | MNT_ROOTFS, 1577 "fstype", vfsname, 1578 "fspath", "/", 1579 "from", path, 1580 NULL); 1581 if (error == 0) { 1582 /* 1583 * We mount devfs prior to mounting the / FS, so the first 1584 * entry will typically be devfs. 1585 */ 1586 mp = TAILQ_FIRST(&mountlist); 1587 KASSERT(mp != NULL, ("%s: mountlist is empty", __func__)); 1588 1589 /* 1590 * Iterate over all currently mounted file systems and use 1591 * the time stamp found to check and/or initialize the RTC. 1592 * Typically devfs has no time stamp and the only other FS 1593 * is the actual / FS. 1594 * Call inittodr() only once and pass it the largest of the 1595 * timestamps we encounter. 1596 */ 1597 timebase = 0; 1598 do { 1599 if (mp->mnt_time > timebase) 1600 timebase = mp->mnt_time; 1601 mp = TAILQ_NEXT(mp, mnt_list); 1602 } while (mp != NULL); 1603 inittodr(timebase); 1604 1605 devfs_fixup(curthread); 1606 } 1607out: 1608 free(path, M_MOUNT); 1609 free(vfsname, M_MOUNT); 1610 return (error); 1611} 1612 1613/* 1614 * --------------------------------------------------------------------- 1615 * Interactive root filesystem selection code. 1616 */ 1617 1618static int 1619vfs_mountroot_ask(void) 1620{ 1621 char name[128]; 1622 1623 for(;;) { 1624 printf("\nManual root filesystem specification:\n"); 1625 printf(" <fstype>:<device> Mount <device> using filesystem <fstype>\n"); 1626#if defined(__amd64__) || defined(__i386__) || defined(__ia64__) 1627 printf(" eg. ufs:da0s1a\n"); 1628#else 1629 printf(" eg. ufs:/dev/da0a\n"); 1630#endif 1631 printf(" ? List valid disk boot devices\n"); 1632 printf(" <empty line> Abort manual input\n"); 1633 printf("\nmountroot> "); 1634 gets(name, sizeof(name), 1); 1635 if (name[0] == '\0') 1636 return (1); 1637 if (name[0] == '?') { 1638 printf("\nList of GEOM managed disk devices:\n "); 1639 g_dev_print(); 1640 continue; 1641 } 1642 if (!vfs_mountroot_try(name)) 1643 return (0); 1644 } 1645} 1646 1647/* 1648 * --------------------------------------------------------------------- 1649 * Functions for querying mount options/arguments from filesystems. 1650 */ 1651 1652/* 1653 * Check that no unknown options are given 1654 */ 1655int 1656vfs_filteropt(struct vfsoptlist *opts, const char **legal) 1657{ 1658 struct vfsopt *opt; 1659 const char **t, *p; 1660 1661 1662 TAILQ_FOREACH(opt, opts, link) { 1663 p = opt->name; 1664 if (p[0] == 'n' && p[1] == 'o') 1665 p += 2; 1666 for(t = global_opts; *t != NULL; t++) 1667 if (!strcmp(*t, p)) 1668 break; 1669 if (*t != NULL) 1670 continue; 1671 for(t = legal; *t != NULL; t++) 1672 if (!strcmp(*t, p)) 1673 break; 1674 if (*t != NULL) 1675 continue; 1676 printf("mount option <%s> is unknown\n", p); 1677 return (EINVAL); 1678 } 1679 return (0); 1680} 1681 1682/* 1683 * Get a mount option by its name. 1684 * 1685 * Return 0 if the option was found, ENOENT otherwise. 1686 * If len is non-NULL it will be filled with the length 1687 * of the option. If buf is non-NULL, it will be filled 1688 * with the address of the option. 1689 */ 1690int 1691vfs_getopt(opts, name, buf, len) 1692 struct vfsoptlist *opts; 1693 const char *name; 1694 void **buf; 1695 int *len; 1696{ 1697 struct vfsopt *opt; 1698 1699 KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL")); 1700 1701 TAILQ_FOREACH(opt, opts, link) { 1702 if (strcmp(name, opt->name) == 0) { 1703 if (len != NULL) 1704 *len = opt->len; 1705 if (buf != NULL) 1706 *buf = opt->value; 1707 return (0); 1708 } 1709 } 1710 return (ENOENT); 1711} 1712 1713static int 1714vfs_getopt_pos(struct vfsoptlist *opts, const char *name) 1715{ 1716 struct vfsopt *opt; 1717 int i; 1718 1719 if (opts == NULL) 1720 return (-1); 1721 1722 i = 0; 1723 TAILQ_FOREACH(opt, opts, link) { 1724 if (strcmp(name, opt->name) == 0) 1725 return (i); 1726 ++i; 1727 } 1728 return (-1); 1729} 1730 1731char * 1732vfs_getopts(struct vfsoptlist *opts, const char *name, int *error) 1733{ 1734 struct vfsopt *opt; 1735 1736 *error = 0; 1737 TAILQ_FOREACH(opt, opts, link) { 1738 if (strcmp(name, opt->name) != 0) 1739 continue; 1740 if (((char *)opt->value)[opt->len - 1] != '\0') { 1741 *error = EINVAL; 1742 return (NULL); 1743 } 1744 return (opt->value); 1745 } 1746 return (NULL); 1747} 1748 1749int 1750vfs_flagopt(struct vfsoptlist *opts, const char *name, u_int *w, u_int val) 1751{ 1752 struct vfsopt *opt; 1753 1754 TAILQ_FOREACH(opt, opts, link) { 1755 if (strcmp(name, opt->name) == 0) { 1756 if (w != NULL) 1757 *w |= val; 1758 return (1); 1759 } 1760 } 1761 if (w != NULL) 1762 *w &= ~val; 1763 return (0); 1764} 1765 1766int 1767vfs_scanopt(struct vfsoptlist *opts, const char *name, const char *fmt, ...) 1768{ 1769 va_list ap; 1770 struct vfsopt *opt; 1771 int ret; 1772 1773 KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL")); 1774 1775 TAILQ_FOREACH(opt, opts, link) { 1776 if (strcmp(name, opt->name) != 0) 1777 continue; 1778 if (((char *)opt->value)[opt->len - 1] != '\0') 1779 return (0); 1780 va_start(ap, fmt); 1781 ret = vsscanf(opt->value, fmt, ap); 1782 va_end(ap); 1783 return (ret); 1784 } 1785 return (0); 1786} 1787 1788/* 1789 * Find and copy a mount option. 1790 * 1791 * The size of the buffer has to be specified 1792 * in len, if it is not the same length as the 1793 * mount option, EINVAL is returned. 1794 * Returns ENOENT if the option is not found. 1795 */ 1796int 1797vfs_copyopt(opts, name, dest, len) 1798 struct vfsoptlist *opts; 1799 const char *name; 1800 void *dest; 1801 int len; 1802{ 1803 struct vfsopt *opt; 1804 1805 KASSERT(opts != NULL, ("vfs_copyopt: caller passed 'opts' as NULL")); 1806 1807 TAILQ_FOREACH(opt, opts, link) { 1808 if (strcmp(name, opt->name) == 0) { 1809 if (len != opt->len) 1810 return (EINVAL); 1811 bcopy(opt->value, dest, opt->len); 1812 return (0); 1813 } 1814 } 1815 return (ENOENT); 1816} 1817 1818/* 1819 * This is a helper function for filesystems to traverse their 1820 * vnodes. See MNT_VNODE_FOREACH() in sys/mount.h 1821 */ 1822 1823struct vnode * 1824__mnt_vnode_next(struct vnode **mvp, struct mount *mp) 1825{ 1826 struct vnode *vp; 1827 1828 mtx_assert(MNT_MTX(mp), MA_OWNED); 1829 1830 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch")); 1831 vp = TAILQ_NEXT(*mvp, v_nmntvnodes); 1832 while (vp != NULL && vp->v_type == VMARKER) 1833 vp = TAILQ_NEXT(vp, v_nmntvnodes); 1834 1835 /* Check if we are done */ 1836 if (vp == NULL) { 1837 __mnt_vnode_markerfree(mvp, mp); 1838 return (NULL); 1839 } 1840 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes); 1841 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes); 1842 return (vp); 1843} 1844 1845struct vnode * 1846__mnt_vnode_first(struct vnode **mvp, struct mount *mp) 1847{ 1848 struct vnode *vp; 1849 1850 mtx_assert(MNT_MTX(mp), MA_OWNED); 1851 1852 vp = TAILQ_FIRST(&mp->mnt_nvnodelist); 1853 while (vp != NULL && vp->v_type == VMARKER) 1854 vp = TAILQ_NEXT(vp, v_nmntvnodes); 1855 1856 /* Check if we are done */ 1857 if (vp == NULL) { 1858 *mvp = NULL; 1859 return (NULL); 1860 } 1861 mp->mnt_holdcnt++; 1862 MNT_IUNLOCK(mp); 1863 *mvp = (struct vnode *) malloc(sizeof(struct vnode), 1864 M_VNODE_MARKER, 1865 M_WAITOK | M_ZERO); 1866 MNT_ILOCK(mp); 1867 (*mvp)->v_type = VMARKER; 1868 1869 vp = TAILQ_FIRST(&mp->mnt_nvnodelist); 1870 while (vp != NULL && vp->v_type == VMARKER) 1871 vp = TAILQ_NEXT(vp, v_nmntvnodes); 1872 1873 /* Check if we are done */ 1874 if (vp == NULL) { 1875 MNT_IUNLOCK(mp); 1876 free(*mvp, M_VNODE_MARKER); 1877 MNT_ILOCK(mp); 1878 *mvp = NULL; 1879 mp->mnt_holdcnt--; 1880 if (mp->mnt_holdcnt == 0 && mp->mnt_holdcntwaiters != 0) 1881 wakeup(&mp->mnt_holdcnt); 1882 return (NULL); 1883 } 1884 mp->mnt_markercnt++; 1885 (*mvp)->v_mount = mp; 1886 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes); 1887 return (vp); 1888} 1889 1890 1891void 1892__mnt_vnode_markerfree(struct vnode **mvp, struct mount *mp) 1893{ 1894 1895 if (*mvp == NULL) 1896 return; 1897 1898 mtx_assert(MNT_MTX(mp), MA_OWNED); 1899 1900 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch")); 1901 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes); 1902 MNT_IUNLOCK(mp); 1903 free(*mvp, M_VNODE_MARKER); 1904 MNT_ILOCK(mp); 1905 *mvp = NULL; 1906 1907 mp->mnt_markercnt--; 1908 mp->mnt_holdcnt--; 1909 if (mp->mnt_holdcnt == 0 && mp->mnt_holdcntwaiters != 0) 1910 wakeup(&mp->mnt_holdcnt); 1911} 1912 1913 1914int 1915__vfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td) 1916{ 1917 int error; 1918 1919 error = mp->mnt_op->vfs_statfs(mp, &mp->mnt_stat, td); 1920 if (sbp != &mp->mnt_stat) 1921 *sbp = mp->mnt_stat; 1922 return (error); 1923} 1924 1925void 1926vfs_mountedfrom(struct mount *mp, const char *from) 1927{ 1928 1929 bzero(mp->mnt_stat.f_mntfromname, sizeof mp->mnt_stat.f_mntfromname); 1930 strlcpy(mp->mnt_stat.f_mntfromname, from, 1931 sizeof mp->mnt_stat.f_mntfromname); 1932} 1933 1934/* 1935 * --------------------------------------------------------------------- 1936 * This is the api for building mount args and mounting filesystems from 1937 * inside the kernel. 1938 * 1939 * The API works by accumulation of individual args. First error is 1940 * latched. 1941 * 1942 * XXX: should be documented in new manpage kernel_mount(9) 1943 */ 1944 1945/* A memory allocation which must be freed when we are done */ 1946struct mntaarg { 1947 SLIST_ENTRY(mntaarg) next; 1948}; 1949 1950/* The header for the mount arguments */ 1951struct mntarg { 1952 struct iovec *v; 1953 int len; 1954 int error; 1955 SLIST_HEAD(, mntaarg) list; 1956}; 1957 1958/* 1959 * Add a boolean argument. 1960 * 1961 * flag is the boolean value. 1962 * name must start with "no". 1963 */ 1964struct mntarg * 1965mount_argb(struct mntarg *ma, int flag, const char *name) 1966{ 1967 1968 KASSERT(name[0] == 'n' && name[1] == 'o', 1969 ("mount_argb(...,%s): name must start with 'no'", name)); 1970 1971 return (mount_arg(ma, name + (flag ? 2 : 0), NULL, 0)); 1972} 1973 1974/* 1975 * Add an argument printf style 1976 */ 1977struct mntarg * 1978mount_argf(struct mntarg *ma, const char *name, const char *fmt, ...) 1979{ 1980 va_list ap; 1981 struct mntaarg *maa; 1982 struct sbuf *sb; 1983 int len; 1984 1985 if (ma == NULL) { 1986 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO); 1987 SLIST_INIT(&ma->list); 1988 } 1989 if (ma->error) 1990 return (ma); 1991 1992 ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2), 1993 M_MOUNT, M_WAITOK); 1994 ma->v[ma->len].iov_base = (void *)(uintptr_t)name; 1995 ma->v[ma->len].iov_len = strlen(name) + 1; 1996 ma->len++; 1997 1998 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND); 1999 va_start(ap, fmt); 2000 sbuf_vprintf(sb, fmt, ap); 2001 va_end(ap); 2002 sbuf_finish(sb); 2003 len = sbuf_len(sb) + 1; 2004 maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO); 2005 SLIST_INSERT_HEAD(&ma->list, maa, next); 2006 bcopy(sbuf_data(sb), maa + 1, len); 2007 sbuf_delete(sb); 2008 2009 ma->v[ma->len].iov_base = maa + 1; 2010 ma->v[ma->len].iov_len = len; 2011 ma->len++; 2012 2013 return (ma); 2014} 2015 2016/* 2017 * Add an argument which is a userland string. 2018 */ 2019struct mntarg * 2020mount_argsu(struct mntarg *ma, const char *name, const void *val, int len) 2021{ 2022 struct mntaarg *maa; 2023 char *tbuf; 2024 2025 if (val == NULL) 2026 return (ma); 2027 if (ma == NULL) { 2028 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO); 2029 SLIST_INIT(&ma->list); 2030 } 2031 if (ma->error) 2032 return (ma); 2033 maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO); 2034 SLIST_INSERT_HEAD(&ma->list, maa, next); 2035 tbuf = (void *)(maa + 1); 2036 ma->error = copyinstr(val, tbuf, len, NULL); 2037 return (mount_arg(ma, name, tbuf, -1)); 2038} 2039 2040/* 2041 * Plain argument. 2042 * 2043 * If length is -1, use printf. 2044 */ 2045struct mntarg * 2046mount_arg(struct mntarg *ma, const char *name, const void *val, int len) 2047{ 2048 2049 if (ma == NULL) { 2050 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO); 2051 SLIST_INIT(&ma->list); 2052 } 2053 if (ma->error) 2054 return (ma); 2055 2056 ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2), 2057 M_MOUNT, M_WAITOK); 2058 ma->v[ma->len].iov_base = (void *)(uintptr_t)name; 2059 ma->v[ma->len].iov_len = strlen(name) + 1; 2060 ma->len++; 2061 2062 ma->v[ma->len].iov_base = (void *)(uintptr_t)val; 2063 if (len < 0) 2064 ma->v[ma->len].iov_len = strlen(val) + 1; 2065 else 2066 ma->v[ma->len].iov_len = len; 2067 ma->len++; 2068 return (ma); 2069} 2070 2071/* 2072 * Free a mntarg structure 2073 */ 2074static void 2075free_mntarg(struct mntarg *ma) 2076{ 2077 struct mntaarg *maa; 2078 2079 while (!SLIST_EMPTY(&ma->list)) { 2080 maa = SLIST_FIRST(&ma->list); 2081 SLIST_REMOVE_HEAD(&ma->list, next); 2082 free(maa, M_MOUNT); 2083 } 2084 free(ma->v, M_MOUNT); 2085 free(ma, M_MOUNT); 2086} 2087 2088/* 2089 * Mount a filesystem 2090 */ 2091int 2092kernel_mount(struct mntarg *ma, int flags) 2093{ 2094 struct uio auio; 2095 int error; 2096 2097 KASSERT(ma != NULL, ("kernel_mount NULL ma")); 2098 KASSERT(ma->v != NULL, ("kernel_mount NULL ma->v")); 2099 KASSERT(!(ma->len & 1), ("kernel_mount odd ma->len (%d)", ma->len)); 2100 2101 auio.uio_iov = ma->v; 2102 auio.uio_iovcnt = ma->len; 2103 auio.uio_segflg = UIO_SYSSPACE; 2104 2105 error = ma->error; 2106 if (!error) 2107 error = vfs_donmount(curthread, flags, &auio); 2108 free_mntarg(ma); 2109 return (error); 2110} 2111 2112/* 2113 * A printflike function to mount a filesystem. 2114 */ 2115int 2116kernel_vmount(int flags, ...) 2117{ 2118 struct mntarg *ma = NULL; 2119 va_list ap; 2120 const char *cp; 2121 const void *vp; 2122 int error; 2123 2124 va_start(ap, flags); 2125 for (;;) { 2126 cp = va_arg(ap, const char *); 2127 if (cp == NULL) 2128 break; 2129 vp = va_arg(ap, const void *); 2130 ma = mount_arg(ma, cp, vp, -1); 2131 } 2132 va_end(ap); 2133 2134 error = kernel_mount(ma, flags); 2135 return (error); 2136} 2137