vfs_mount.c revision 145249
1214501Srpaulo/*- 2214501Srpaulo * Copyright (c) 1999-2004 Poul-Henning Kamp 3214501Srpaulo * Copyright (c) 1999 Michael Smith 4214501Srpaulo * Copyright (c) 1989, 1993 5214501Srpaulo * The Regents of the University of California. All rights reserved. 6252726Srpaulo * (c) UNIX System Laboratories, Inc. 7252726Srpaulo * All or some portions of this file are derived from material licensed 8214501Srpaulo * to the University of California by American Telephone and Telegraph 9214501Srpaulo * Co. or Unix System Laboratories, Inc. and are reproduced herein with 10214501Srpaulo * the permission of UNIX System Laboratories, Inc. 11214501Srpaulo * 12214501Srpaulo * Redistribution and use in source and binary forms, with or without 13214501Srpaulo * modification, are permitted provided that the following conditions 14214501Srpaulo * are met: 15214501Srpaulo * 1. Redistributions of source code must retain the above copyright 16214501Srpaulo * notice, this list of conditions and the following disclaimer. 17214501Srpaulo * 2. Redistributions in binary form must reproduce the above copyright 18214501Srpaulo * notice, this list of conditions and the following disclaimer in the 19214501Srpaulo * documentation and/or other materials provided with the distribution. 20214501Srpaulo * 4. Neither the name of the University nor the names of its contributors 21214501Srpaulo * may be used to endorse or promote products derived from this software 22214501Srpaulo * without specific prior written permission. 23214501Srpaulo * 24214501Srpaulo * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 25214501Srpaulo * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26214501Srpaulo * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27214501Srpaulo * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 28214501Srpaulo * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29214501Srpaulo * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30214501Srpaulo * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31214501Srpaulo * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32214501Srpaulo * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33214501Srpaulo * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34214501Srpaulo * SUCH DAMAGE. 35214501Srpaulo */ 36214501Srpaulo 37214501Srpaulo#include <sys/cdefs.h> 38214501Srpaulo__FBSDID("$FreeBSD: head/sys/kern/vfs_mount.c 145249 2005-04-18 21:11:47Z phk $"); 39214501Srpaulo 40214501Srpaulo#include <sys/param.h> 41214501Srpaulo#include <sys/conf.h> 42214501Srpaulo#include <sys/jail.h> 43214501Srpaulo#include <sys/kernel.h> 44214501Srpaulo#include <sys/libkern.h> 45214501Srpaulo#include <sys/mac.h> 46214501Srpaulo#include <sys/malloc.h> 47214501Srpaulo#include <sys/mount.h> 48214501Srpaulo#include <sys/mutex.h> 49214501Srpaulo#include <sys/namei.h> 50214501Srpaulo#include <sys/proc.h> 51214501Srpaulo#include <sys/filedesc.h> 52214501Srpaulo#include <sys/reboot.h> 53214501Srpaulo#include <sys/syscallsubr.h> 54214501Srpaulo#include <sys/sysproto.h> 55214501Srpaulo#include <sys/sx.h> 56214501Srpaulo#include <sys/sysctl.h> 57214501Srpaulo#include <sys/sysent.h> 58214501Srpaulo#include <sys/systm.h> 59214501Srpaulo#include <sys/vnode.h> 60214501Srpaulo 61214501Srpaulo#include <geom/geom.h> 62214501Srpaulo 63214501Srpaulo#include <machine/stdarg.h> 64214501Srpaulo 65214501Srpaulo#include "opt_rootdevname.h" 66214501Srpaulo#include "opt_ddb.h" 67214501Srpaulo#include "opt_mac.h" 68214501Srpaulo 69214501Srpaulo#ifdef DDB 70214501Srpaulo#include <ddb/ddb.h> 71214501Srpaulo#endif 72214501Srpaulo 73214501Srpaulo#define ROOTNAME "root_device" 74214501Srpaulo#define VFS_MOUNTARG_SIZE_MAX (1024 * 64) 75214501Srpaulo 76214501Srpaulostatic int vfs_domount(struct thread *td, const char *fstype, 77214501Srpaulo char *fspath, int fsflags, void *fsdata); 78214501Srpaulostatic int vfs_mount_alloc(struct vnode *dvp, struct vfsconf *vfsp, 79214501Srpaulo const char *fspath, struct thread *td, struct mount **mpp); 80214501Srpaulostatic int vfs_mountroot_ask(void); 81static int vfs_mountroot_try(const char *mountfrom); 82static int vfs_donmount(struct thread *td, int fsflags, 83 struct uio *fsoptions); 84static void free_mntarg(struct mntarg *ma); 85static void vfs_mount_destroy(struct mount *, struct thread *); 86 87static int usermount = 0; 88SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0, 89 "Unprivileged users may mount and unmount file systems"); 90 91MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount structure"); 92 93/* List of mounted filesystems. */ 94struct mntlist mountlist = TAILQ_HEAD_INITIALIZER(mountlist); 95 96/* For any iteration/modification of mountlist */ 97struct mtx mountlist_mtx; 98MTX_SYSINIT(mountlist, &mountlist_mtx, "mountlist", MTX_DEF); 99 100TAILQ_HEAD(vfsoptlist, vfsopt); 101struct vfsopt { 102 TAILQ_ENTRY(vfsopt) link; 103 char *name; 104 void *value; 105 int len; 106}; 107 108/* 109 * The vnode of the system's root (/ in the filesystem, without chroot 110 * active.) 111 */ 112struct vnode *rootvnode; 113 114/* 115 * The root filesystem is detailed in the kernel environment variable 116 * vfs.root.mountfrom, which is expected to be in the general format 117 * 118 * <vfsname>:[<path>] 119 * vfsname := the name of a VFS known to the kernel and capable 120 * of being mounted as root 121 * path := disk device name or other data used by the filesystem 122 * to locate its physical store 123 */ 124 125/* 126 * Global opts, taken by all filesystems 127 */ 128static const char *global_opts[] = { 129 "fstype", 130 "fspath", 131 "ro", 132 "suid", 133 "exec", 134 NULL 135}; 136 137/* 138 * The root specifiers we will try if RB_CDROM is specified. 139 */ 140static char *cdrom_rootdevnames[] = { 141 "cd9660:cd0", 142 "cd9660:acd0", 143 NULL 144}; 145 146/* legacy find-root code */ 147char *rootdevnames[2] = {NULL, NULL}; 148#ifndef ROOTDEVNAME 149# define ROOTDEVNAME NULL 150#endif 151static const char *ctrootdevname = ROOTDEVNAME; 152 153/* 154 * --------------------------------------------------------------------- 155 * Functions for building and sanitizing the mount options 156 */ 157 158/* Remove one mount option. */ 159static void 160vfs_freeopt(struct vfsoptlist *opts, struct vfsopt *opt) 161{ 162 163 TAILQ_REMOVE(opts, opt, link); 164 free(opt->name, M_MOUNT); 165 if (opt->value != NULL) 166 free(opt->value, M_MOUNT); 167#ifdef INVARIANTS 168 else if (opt->len != 0) 169 panic("%s: mount option with NULL value but length != 0", 170 __func__); 171#endif 172 free(opt, M_MOUNT); 173} 174 175/* Release all resources related to the mount options. */ 176static void 177vfs_freeopts(struct vfsoptlist *opts) 178{ 179 struct vfsopt *opt; 180 181 while (!TAILQ_EMPTY(opts)) { 182 opt = TAILQ_FIRST(opts); 183 vfs_freeopt(opts, opt); 184 } 185 free(opts, M_MOUNT); 186} 187 188/* 189 * Check if options are equal (with or without the "no" prefix). 190 */ 191static int 192vfs_equalopts(const char *opt1, const char *opt2) 193{ 194 195 /* "opt" vs. "opt" or "noopt" vs. "noopt" */ 196 if (strcmp(opt1, opt2) == 0) 197 return (1); 198 /* "noopt" vs. "opt" */ 199 if (strncmp(opt1, "no", 2) == 0 && strcmp(opt1 + 2, opt2) == 0) 200 return (1); 201 /* "opt" vs. "noopt" */ 202 if (strncmp(opt2, "no", 2) == 0 && strcmp(opt1, opt2 + 2) == 0) 203 return (1); 204 return (0); 205} 206 207/* 208 * If a mount option is specified several times, 209 * (with or without the "no" prefix) only keep 210 * the last occurence of it. 211 */ 212static void 213vfs_sanitizeopts(struct vfsoptlist *opts) 214{ 215 struct vfsopt *opt, *opt2, *tmp; 216 217 TAILQ_FOREACH_REVERSE(opt, opts, vfsoptlist, link) { 218 opt2 = TAILQ_PREV(opt, vfsoptlist, link); 219 while (opt2 != NULL) { 220 if (vfs_equalopts(opt->name, opt2->name)) { 221 tmp = TAILQ_PREV(opt2, vfsoptlist, link); 222 vfs_freeopt(opts, opt2); 223 opt2 = tmp; 224 } else { 225 opt2 = TAILQ_PREV(opt2, vfsoptlist, link); 226 } 227 } 228 } 229} 230 231/* 232 * Build a linked list of mount options from a struct uio. 233 */ 234static int 235vfs_buildopts(struct uio *auio, struct vfsoptlist **options) 236{ 237 struct vfsoptlist *opts; 238 struct vfsopt *opt; 239 size_t memused; 240 unsigned int i, iovcnt; 241 int error, namelen, optlen; 242 243 opts = malloc(sizeof(struct vfsoptlist), M_MOUNT, M_WAITOK); 244 TAILQ_INIT(opts); 245 memused = 0; 246 iovcnt = auio->uio_iovcnt; 247 for (i = 0; i < iovcnt; i += 2) { 248 opt = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK); 249 namelen = auio->uio_iov[i].iov_len; 250 optlen = auio->uio_iov[i + 1].iov_len; 251 opt->name = malloc(namelen, M_MOUNT, M_WAITOK); 252 opt->value = NULL; 253 opt->len = 0; 254 255 /* 256 * Do this early, so jumps to "bad" will free the current 257 * option. 258 */ 259 TAILQ_INSERT_TAIL(opts, opt, link); 260 memused += sizeof(struct vfsopt) + optlen + namelen; 261 262 /* 263 * Avoid consuming too much memory, and attempts to overflow 264 * memused. 265 */ 266 if (memused > VFS_MOUNTARG_SIZE_MAX || 267 optlen > VFS_MOUNTARG_SIZE_MAX || 268 namelen > VFS_MOUNTARG_SIZE_MAX) { 269 error = EINVAL; 270 goto bad; 271 } 272 273 if (auio->uio_segflg == UIO_SYSSPACE) { 274 bcopy(auio->uio_iov[i].iov_base, opt->name, namelen); 275 } else { 276 error = copyin(auio->uio_iov[i].iov_base, opt->name, 277 namelen); 278 if (error) 279 goto bad; 280 } 281 /* Ensure names are null-terminated strings. */ 282 if (opt->name[namelen - 1] != '\0') { 283 error = EINVAL; 284 goto bad; 285 } 286 if (optlen != 0) { 287 opt->len = optlen; 288 opt->value = malloc(optlen, M_MOUNT, M_WAITOK); 289 if (auio->uio_segflg == UIO_SYSSPACE) { 290 bcopy(auio->uio_iov[i + 1].iov_base, opt->value, 291 optlen); 292 } else { 293 error = copyin(auio->uio_iov[i + 1].iov_base, 294 opt->value, optlen); 295 if (error) 296 goto bad; 297 } 298 } 299 } 300 vfs_sanitizeopts(opts); 301 *options = opts; 302 return (0); 303bad: 304 vfs_freeopts(opts); 305 return (error); 306} 307 308/* 309 * Merge the old mount options with the new ones passed 310 * in the MNT_UPDATE case. 311 */ 312static void 313vfs_mergeopts(struct vfsoptlist *toopts, struct vfsoptlist *opts) 314{ 315 struct vfsopt *opt, *opt2, *new; 316 317 TAILQ_FOREACH(opt, opts, link) { 318 /* 319 * Check that this option hasn't been redefined 320 * nor cancelled with a "no" mount option. 321 */ 322 opt2 = TAILQ_FIRST(toopts); 323 while (opt2 != NULL) { 324 if (strcmp(opt2->name, opt->name) == 0) 325 goto next; 326 if (strncmp(opt2->name, "no", 2) == 0 && 327 strcmp(opt2->name + 2, opt->name) == 0) { 328 vfs_freeopt(toopts, opt2); 329 goto next; 330 } 331 opt2 = TAILQ_NEXT(opt2, link); 332 } 333 /* We want this option, duplicate it. */ 334 new = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK); 335 new->name = malloc(strlen(opt->name) + 1, M_MOUNT, M_WAITOK); 336 strcpy(new->name, opt->name); 337 if (opt->len != 0) { 338 new->value = malloc(opt->len, M_MOUNT, M_WAITOK); 339 bcopy(opt->value, new->value, opt->len); 340 } else { 341 new->value = NULL; 342 } 343 new->len = opt->len; 344 TAILQ_INSERT_TAIL(toopts, new, link); 345next: 346 continue; 347 } 348} 349 350/* 351 * --------------------------------------------------------------------- 352 * Mount a filesystem 353 */ 354int 355nmount(td, uap) 356 struct thread *td; 357 struct nmount_args /* { 358 struct iovec *iovp; 359 unsigned int iovcnt; 360 int flags; 361 } */ *uap; 362{ 363 struct uio *auio; 364 struct iovec *iov; 365 unsigned int i; 366 int error; 367 u_int iovcnt; 368 369 /* Kick out MNT_ROOTFS early as it is legal internally */ 370 if (uap->flags & MNT_ROOTFS) 371 return (EINVAL); 372 373 iovcnt = uap->iovcnt; 374 /* 375 * Check that we have an even number of iovec's 376 * and that we have at least two options. 377 */ 378 if ((iovcnt & 1) || (iovcnt < 4)) 379 return (EINVAL); 380 381 error = copyinuio(uap->iovp, iovcnt, &auio); 382 if (error) 383 return (error); 384 iov = auio->uio_iov; 385 for (i = 0; i < iovcnt; i++) { 386 if (iov->iov_len > MMAXOPTIONLEN) { 387 free(auio, M_IOV); 388 return (EINVAL); 389 } 390 iov++; 391 } 392 error = vfs_donmount(td, uap->flags, auio); 393 free(auio, M_IOV); 394 return (error); 395} 396 397/* 398 * --------------------------------------------------------------------- 399 * Various utility functions 400 */ 401 402/* 403 * Allocate and initialize the mount point struct. 404 */ 405static int 406vfs_mount_alloc(struct vnode *vp, struct vfsconf *vfsp, 407 const char *fspath, struct thread *td, struct mount **mpp) 408{ 409 struct mount *mp; 410 411 mp = malloc(sizeof(struct mount), M_MOUNT, M_WAITOK | M_ZERO); 412 TAILQ_INIT(&mp->mnt_nvnodelist); 413 mp->mnt_nvnodelistsize = 0; 414 mtx_init(&mp->mnt_mtx, "struct mount mtx", NULL, MTX_DEF); 415 lockinit(&mp->mnt_lock, PVFS, "vfslock", 0, 0); 416 vfs_busy(mp, LK_NOWAIT, 0, td); 417 mp->mnt_op = vfsp->vfc_vfsops; 418 mp->mnt_vfc = vfsp; 419 vfsp->vfc_refcount++; 420 mp->mnt_stat.f_type = vfsp->vfc_typenum; 421 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK; 422 strlcpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN); 423 mp->mnt_vnodecovered = vp; 424 mp->mnt_cred = crdup(td->td_ucred); 425 mp->mnt_stat.f_owner = td->td_ucred->cr_uid; 426 strlcpy(mp->mnt_stat.f_mntonname, fspath, MNAMELEN); 427 mp->mnt_iosize_max = DFLTPHYS; 428#ifdef MAC 429 mac_init_mount(mp); 430 mac_create_mount(td->td_ucred, mp); 431#endif 432 arc4rand(&mp->mnt_hashseed, sizeof mp->mnt_hashseed, 0); 433 *mpp = mp; 434 return (0); 435} 436 437/* 438 * Destroy the mount struct previously allocated by vfs_mount_alloc(). 439 */ 440static void 441vfs_mount_destroy(struct mount *mp, struct thread *td) 442{ 443 444 mp->mnt_vfc->vfc_refcount--; 445 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist)) 446 panic("unmount: dangling vnode"); 447 vfs_unbusy(mp,td); 448 lockdestroy(&mp->mnt_lock); 449 MNT_ILOCK(mp); 450 if (mp->mnt_kern_flag & MNTK_MWAIT) 451 wakeup(mp); 452 MNT_IUNLOCK(mp); 453 mtx_destroy(&mp->mnt_mtx); 454#ifdef MAC 455 mac_destroy_mount(mp); 456#endif 457 if (mp->mnt_opt != NULL) 458 vfs_freeopts(mp->mnt_opt); 459 crfree(mp->mnt_cred); 460 free(mp, M_MOUNT); 461} 462 463static int 464vfs_donmount(struct thread *td, int fsflags, struct uio *fsoptions) 465{ 466 struct vfsoptlist *optlist; 467 char *fstype, *fspath; 468 int error, fstypelen, fspathlen; 469 470 error = vfs_buildopts(fsoptions, &optlist); 471 if (error) 472 return (error); 473 474 /* 475 * We need these two options before the others, 476 * and they are mandatory for any filesystem. 477 * Ensure they are NUL terminated as well. 478 */ 479 fstypelen = 0; 480 error = vfs_getopt(optlist, "fstype", (void **)&fstype, &fstypelen); 481 if (error || fstype[fstypelen - 1] != '\0') { 482 error = EINVAL; 483 goto bail; 484 } 485 fspathlen = 0; 486 error = vfs_getopt(optlist, "fspath", (void **)&fspath, &fspathlen); 487 if (error || fspath[fspathlen - 1] != '\0') { 488 error = EINVAL; 489 goto bail; 490 } 491 492 /* 493 * Be ultra-paranoid about making sure the type and fspath 494 * variables will fit in our mp buffers, including the 495 * terminating NUL. 496 */ 497 if (fstypelen >= MFSNAMELEN - 1 || fspathlen >= MNAMELEN - 1) { 498 error = ENAMETOOLONG; 499 goto bail; 500 } 501 502 mtx_lock(&Giant); 503 error = vfs_domount(td, fstype, fspath, fsflags, optlist); 504 mtx_unlock(&Giant); 505bail: 506 if (error) 507 vfs_freeopts(optlist); 508 return (error); 509} 510 511/* 512 * --------------------------------------------------------------------- 513 * Old mount API. 514 */ 515#ifndef _SYS_SYSPROTO_H_ 516struct mount_args { 517 char *type; 518 char *path; 519 int flags; 520 caddr_t data; 521}; 522#endif 523/* ARGSUSED */ 524int 525mount(td, uap) 526 struct thread *td; 527 struct mount_args /* { 528 char *type; 529 char *path; 530 int flags; 531 caddr_t data; 532 } */ *uap; 533{ 534 char *fstype; 535 struct vfsconf *vfsp = NULL; 536 struct mntarg *ma = NULL; 537 int error; 538 539 /* Kick out MNT_ROOTFS early as it is legal internally */ 540 uap->flags &= ~MNT_ROOTFS; 541 542 if (uap->data == NULL) 543 return (EINVAL); 544 545 fstype = malloc(MFSNAMELEN, M_TEMP, M_WAITOK); 546 error = copyinstr(uap->type, fstype, MFSNAMELEN, NULL); 547 if (!error) { 548 mtx_lock(&Giant); /* XXX ? */ 549 vfsp = vfs_byname_kld(fstype, td, &error); 550 mtx_unlock(&Giant); 551 } 552 free(fstype, M_TEMP); 553 if (error) 554 return (error); 555 if (vfsp == NULL) 556 return (ENOENT); 557 if (vfsp->vfc_vfsops->vfs_cmount == NULL) 558 return (EOPNOTSUPP); 559 560 ma = mount_argsu(ma, "fstype", uap->type, MNAMELEN); 561 ma = mount_argsu(ma, "fspath", uap->path, MNAMELEN); 562 ma = mount_argb(ma, uap->flags & MNT_RDONLY, "noro"); 563 ma = mount_argb(ma, !(uap->flags & MNT_NOSUID), "nosuid"); 564 ma = mount_argb(ma, !(uap->flags & MNT_NOEXEC), "noexec"); 565 566 error = vfsp->vfc_vfsops->vfs_cmount(ma, uap->data, uap->flags, td); 567 return (error); 568} 569 570 571/* 572 * vfs_domount(): actually attempt a filesystem mount. 573 */ 574static int 575vfs_domount( 576 struct thread *td, /* Flags common to all filesystems. */ 577 const char *fstype, /* Filesystem type. */ 578 char *fspath, /* Mount path. */ 579 int fsflags, /* Flags common to all filesystems. */ 580 void *fsdata /* Options local to the filesystem. */ 581 ) 582{ 583 struct vnode *vp; 584 struct mount *mp; 585 struct vfsconf *vfsp; 586 int error, flag = 0, kern_flag = 0; 587 struct vattr va; 588 struct nameidata nd; 589 590 mtx_assert(&Giant, MA_OWNED); 591 592 /* 593 * Be ultra-paranoid about making sure the type and fspath 594 * variables will fit in our mp buffers, including the 595 * terminating NUL. 596 */ 597 if (strlen(fstype) >= MFSNAMELEN || strlen(fspath) >= MNAMELEN) 598 return (ENAMETOOLONG); 599 600 if (jailed(td->td_ucred)) 601 return (EPERM); 602 if (usermount == 0) { 603 if ((error = suser(td)) != 0) 604 return (error); 605 } 606 607 /* 608 * Do not allow NFS export or MNT_SUIDDIR by unprivileged users. 609 */ 610 if (fsflags & (MNT_EXPORTED | MNT_SUIDDIR)) { 611 if ((error = suser(td)) != 0) 612 return (error); 613 } 614 /* 615 * Silently enforce MNT_NOSUID and MNT_USER for 616 * unprivileged users. 617 */ 618 if (suser(td) != 0) 619 fsflags |= MNT_NOSUID | MNT_USER; 620 /* 621 * Get vnode to be covered 622 */ 623 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspath, td); 624 if ((error = namei(&nd)) != 0) 625 return (error); 626 NDFREE(&nd, NDF_ONLY_PNBUF); 627 vp = nd.ni_vp; 628 if (fsflags & MNT_UPDATE) { 629 if ((vp->v_vflag & VV_ROOT) == 0) { 630 vput(vp); 631 return (EINVAL); 632 } 633 mp = vp->v_mount; 634 flag = mp->mnt_flag; 635 kern_flag = mp->mnt_kern_flag; 636 /* 637 * We only allow the filesystem to be reloaded if it 638 * is currently mounted read-only. 639 */ 640 if ((fsflags & MNT_RELOAD) && 641 ((mp->mnt_flag & MNT_RDONLY) == 0)) { 642 vput(vp); 643 return (EOPNOTSUPP); /* Needs translation */ 644 } 645 /* 646 * Only privileged root, or (if MNT_USER is set) the user that 647 * did the original mount is permitted to update it. 648 */ 649 error = vfs_suser(mp, td); 650 if (error) { 651 vput(vp); 652 return (error); 653 } 654 if (vfs_busy(mp, LK_NOWAIT, 0, td)) { 655 vput(vp); 656 return (EBUSY); 657 } 658 VI_LOCK(vp); 659 if ((vp->v_iflag & VI_MOUNT) != 0 || 660 vp->v_mountedhere != NULL) { 661 VI_UNLOCK(vp); 662 vfs_unbusy(mp, td); 663 vput(vp); 664 return (EBUSY); 665 } 666 vp->v_iflag |= VI_MOUNT; 667 VI_UNLOCK(vp); 668 mp->mnt_flag |= fsflags & 669 (MNT_RELOAD | MNT_FORCE | MNT_UPDATE | MNT_SNAPSHOT | MNT_ROOTFS); 670 VOP_UNLOCK(vp, 0, td); 671 mp->mnt_optnew = fsdata; 672 vfs_mergeopts(mp->mnt_optnew, mp->mnt_opt); 673 } else { 674 /* 675 * If the user is not root, ensure that they own the directory 676 * onto which we are attempting to mount. 677 */ 678 error = VOP_GETATTR(vp, &va, td->td_ucred, td); 679 if (error) { 680 vput(vp); 681 return (error); 682 } 683 if (va.va_uid != td->td_ucred->cr_uid) { 684 if ((error = suser(td)) != 0) { 685 vput(vp); 686 return (error); 687 } 688 } 689 error = vinvalbuf(vp, V_SAVE, td, 0, 0); 690 if (error != 0) { 691 vput(vp); 692 return (error); 693 } 694 if (vp->v_type != VDIR) { 695 vput(vp); 696 return (ENOTDIR); 697 } 698 vfsp = vfs_byname_kld(fstype, td, &error); 699 if (vfsp == NULL) { 700 vput(vp); 701 return (error); 702 } 703 VI_LOCK(vp); 704 if ((vp->v_iflag & VI_MOUNT) != 0 || 705 vp->v_mountedhere != NULL) { 706 VI_UNLOCK(vp); 707 vput(vp); 708 return (EBUSY); 709 } 710 vp->v_iflag |= VI_MOUNT; 711 VI_UNLOCK(vp); 712 713 /* 714 * Allocate and initialize the filesystem. 715 */ 716 error = vfs_mount_alloc(vp, vfsp, fspath, td, &mp); 717 if (error) { 718 vput(vp); 719 return (error); 720 } 721 VOP_UNLOCK(vp, 0, td); 722 723 /* XXXMAC: pass to vfs_mount_alloc? */ 724 mp->mnt_optnew = fsdata; 725 } 726 727 /* 728 * Set the mount level flags. 729 */ 730 if (fsflags & MNT_RDONLY) 731 mp->mnt_flag |= MNT_RDONLY; 732 mp->mnt_flag &=~ MNT_UPDATEMASK; 733 mp->mnt_flag |= fsflags & (MNT_UPDATEMASK | MNT_FORCE | MNT_ROOTFS); 734 /* 735 * Mount the filesystem. 736 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they 737 * get. No freeing of cn_pnbuf. 738 */ 739 error = VFS_MOUNT(mp, td); 740 if (!error) { 741 if (mp->mnt_opt != NULL) 742 vfs_freeopts(mp->mnt_opt); 743 mp->mnt_opt = mp->mnt_optnew; 744 VFS_STATFS(mp, &mp->mnt_stat, td); 745 } 746 /* 747 * Prevent external consumers of mount options from reading 748 * mnt_optnew. 749 */ 750 mp->mnt_optnew = NULL; 751 if (mp->mnt_flag & MNT_UPDATE) { 752 mp->mnt_flag &= 753 ~(MNT_UPDATE | MNT_RELOAD | MNT_FORCE | MNT_SNAPSHOT); 754 if (error) { 755 mp->mnt_flag = flag; 756 mp->mnt_kern_flag = kern_flag; 757 } 758 if ((mp->mnt_flag & MNT_RDONLY) == 0) { 759 if (mp->mnt_syncer == NULL) 760 error = vfs_allocate_syncvnode(mp); 761 } else { 762 if (mp->mnt_syncer != NULL) 763 vrele(mp->mnt_syncer); 764 mp->mnt_syncer = NULL; 765 } 766 vfs_unbusy(mp, td); 767 VI_LOCK(vp); 768 vp->v_iflag &= ~VI_MOUNT; 769 VI_UNLOCK(vp); 770 vrele(vp); 771 return (error); 772 } 773 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 774 /* 775 * Put the new filesystem on the mount list after root. 776 */ 777 cache_purge(vp); 778 if (!error) { 779 struct vnode *newdp; 780 781 VI_LOCK(vp); 782 vp->v_iflag &= ~VI_MOUNT; 783 VI_UNLOCK(vp); 784 vp->v_mountedhere = mp; 785 mtx_lock(&mountlist_mtx); 786 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list); 787 mtx_unlock(&mountlist_mtx); 788 vfs_event_signal(NULL, VQ_MOUNT, 0); 789 if (VFS_ROOT(mp, LK_EXCLUSIVE, &newdp, td)) 790 panic("mount: lost mount"); 791 mountcheckdirs(vp, newdp); 792 vput(newdp); 793 VOP_UNLOCK(vp, 0, td); 794 if ((mp->mnt_flag & MNT_RDONLY) == 0) 795 error = vfs_allocate_syncvnode(mp); 796 vfs_unbusy(mp, td); 797 if (error) 798 vrele(vp); 799 } else { 800 VI_LOCK(vp); 801 vp->v_iflag &= ~VI_MOUNT; 802 VI_UNLOCK(vp); 803 vfs_mount_destroy(mp, td); 804 vput(vp); 805 } 806 return (error); 807} 808 809/* 810 * --------------------------------------------------------------------- 811 * Unmount a filesystem. 812 * 813 * Note: unmount takes a path to the vnode mounted on as argument, 814 * not special file (as before). 815 */ 816#ifndef _SYS_SYSPROTO_H_ 817struct unmount_args { 818 char *path; 819 int flags; 820}; 821#endif 822/* ARGSUSED */ 823int 824unmount(td, uap) 825 struct thread *td; 826 register struct unmount_args /* { 827 char *path; 828 int flags; 829 } */ *uap; 830{ 831 struct mount *mp; 832 char *pathbuf; 833 int error, id0, id1; 834 835 if (jailed(td->td_ucred)) 836 return (EPERM); 837 if (usermount == 0) { 838 if ((error = suser(td)) != 0) 839 return (error); 840 } 841 842 pathbuf = malloc(MNAMELEN, M_TEMP, M_WAITOK); 843 error = copyinstr(uap->path, pathbuf, MNAMELEN, NULL); 844 if (error) { 845 free(pathbuf, M_TEMP); 846 return (error); 847 } 848 if (uap->flags & MNT_BYFSID) { 849 /* Decode the filesystem ID. */ 850 if (sscanf(pathbuf, "FSID:%d:%d", &id0, &id1) != 2) { 851 free(pathbuf, M_TEMP); 852 return (EINVAL); 853 } 854 855 mtx_lock(&mountlist_mtx); 856 TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) { 857 if (mp->mnt_stat.f_fsid.val[0] == id0 && 858 mp->mnt_stat.f_fsid.val[1] == id1) 859 break; 860 } 861 mtx_unlock(&mountlist_mtx); 862 } else { 863 mtx_lock(&mountlist_mtx); 864 TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) { 865 if (strcmp(mp->mnt_stat.f_mntonname, pathbuf) == 0) 866 break; 867 } 868 mtx_unlock(&mountlist_mtx); 869 } 870 free(pathbuf, M_TEMP); 871 if (mp == NULL) { 872 /* 873 * Previously we returned ENOENT for a nonexistent path and 874 * EINVAL for a non-mountpoint. We cannot tell these apart 875 * now, so in the !MNT_BYFSID case return the more likely 876 * EINVAL for compatibility. 877 */ 878 return ((uap->flags & MNT_BYFSID) ? ENOENT : EINVAL); 879 } 880 881 /* 882 * Only privileged root, or (if MNT_USER is set) the user that did the 883 * original mount is permitted to unmount this filesystem. 884 */ 885 error = vfs_suser(mp, td); 886 if (error) 887 return (error); 888 889 /* 890 * Don't allow unmounting the root filesystem. 891 */ 892 if (mp->mnt_flag & MNT_ROOTFS) 893 return (EINVAL); 894 mtx_lock(&Giant); 895 error = dounmount(mp, uap->flags, td); 896 mtx_unlock(&Giant); 897 return (error); 898} 899 900/* 901 * Do the actual filesystem unmount. 902 */ 903int 904dounmount(mp, flags, td) 905 struct mount *mp; 906 int flags; 907 struct thread *td; 908{ 909 struct vnode *coveredvp, *fsrootvp; 910 int error; 911 int async_flag; 912 913 mtx_assert(&Giant, MA_OWNED); 914 915 MNT_ILOCK(mp); 916 if (mp->mnt_kern_flag & MNTK_UNMOUNT) { 917 MNT_IUNLOCK(mp); 918 return (EBUSY); 919 } 920 mp->mnt_kern_flag |= MNTK_UNMOUNT; 921 /* Allow filesystems to detect that a forced unmount is in progress. */ 922 if (flags & MNT_FORCE) 923 mp->mnt_kern_flag |= MNTK_UNMOUNTF; 924 error = lockmgr(&mp->mnt_lock, LK_DRAIN | LK_INTERLOCK | 925 ((flags & MNT_FORCE) ? 0 : LK_NOWAIT), MNT_MTX(mp), td); 926 if (error) { 927 MNT_ILOCK(mp); 928 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF); 929 if (mp->mnt_kern_flag & MNTK_MWAIT) 930 wakeup(mp); 931 MNT_IUNLOCK(mp); 932 return (error); 933 } 934 vn_start_write(NULL, &mp, V_WAIT); 935 936 if (mp->mnt_flag & MNT_EXPUBLIC) 937 vfs_setpublicfs(NULL, NULL, NULL); 938 939 vfs_msync(mp, MNT_WAIT); 940 async_flag = mp->mnt_flag & MNT_ASYNC; 941 mp->mnt_flag &= ~MNT_ASYNC; 942 cache_purgevfs(mp); /* remove cache entries for this file sys */ 943 if (mp->mnt_syncer != NULL) 944 vrele(mp->mnt_syncer); 945 /* 946 * For forced unmounts, move process cdir/rdir refs on the fs root 947 * vnode to the covered vnode. For non-forced unmounts we want 948 * such references to cause an EBUSY error. 949 */ 950 if ((flags & MNT_FORCE) && 951 VFS_ROOT(mp, LK_EXCLUSIVE, &fsrootvp, td) == 0) { 952 if (mp->mnt_vnodecovered != NULL) 953 mountcheckdirs(fsrootvp, mp->mnt_vnodecovered); 954 if (fsrootvp == rootvnode) { 955 vrele(rootvnode); 956 rootvnode = NULL; 957 } 958 vput(fsrootvp); 959 } 960 if (((mp->mnt_flag & MNT_RDONLY) || 961 (error = VFS_SYNC(mp, MNT_WAIT, td)) == 0) || 962 (flags & MNT_FORCE)) { 963 error = VFS_UNMOUNT(mp, flags, td); 964 } 965 vn_finished_write(mp); 966 if (error) { 967 /* Undo cdir/rdir and rootvnode changes made above. */ 968 if ((flags & MNT_FORCE) && 969 VFS_ROOT(mp, LK_EXCLUSIVE, &fsrootvp, td) == 0) { 970 if (mp->mnt_vnodecovered != NULL) 971 mountcheckdirs(mp->mnt_vnodecovered, fsrootvp); 972 if (rootvnode == NULL) { 973 rootvnode = fsrootvp; 974 vref(rootvnode); 975 } 976 vput(fsrootvp); 977 } 978 if ((mp->mnt_flag & MNT_RDONLY) == 0 && mp->mnt_syncer == NULL) 979 (void) vfs_allocate_syncvnode(mp); 980 MNT_ILOCK(mp); 981 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF); 982 mp->mnt_flag |= async_flag; 983 lockmgr(&mp->mnt_lock, LK_RELEASE, NULL, td); 984 if (mp->mnt_kern_flag & MNTK_MWAIT) 985 wakeup(mp); 986 MNT_IUNLOCK(mp); 987 return (error); 988 } 989 mtx_lock(&mountlist_mtx); 990 TAILQ_REMOVE(&mountlist, mp, mnt_list); 991 if ((coveredvp = mp->mnt_vnodecovered) != NULL) 992 coveredvp->v_mountedhere = NULL; 993 mtx_unlock(&mountlist_mtx); 994 vfs_event_signal(NULL, VQ_UNMOUNT, 0); 995 vfs_mount_destroy(mp, td); 996 if (coveredvp != NULL) 997 vrele(coveredvp); 998 return (0); 999} 1000 1001/* 1002 * --------------------------------------------------------------------- 1003 * Mounting of root filesystem 1004 * 1005 */ 1006 1007static void 1008set_rootvnode(struct thread *td) 1009{ 1010 struct proc *p; 1011 1012 if (VFS_ROOT(TAILQ_FIRST(&mountlist), LK_EXCLUSIVE, &rootvnode, td)) 1013 panic("Cannot find root vnode"); 1014 1015 p = td->td_proc; 1016 FILEDESC_LOCK(p->p_fd); 1017 1018 if (p->p_fd->fd_cdir != NULL) 1019 vrele(p->p_fd->fd_cdir); 1020 p->p_fd->fd_cdir = rootvnode; 1021 VREF(rootvnode); 1022 1023 if (p->p_fd->fd_rdir != NULL) 1024 vrele(p->p_fd->fd_rdir); 1025 p->p_fd->fd_rdir = rootvnode; 1026 VREF(rootvnode); 1027 1028 FILEDESC_UNLOCK(p->p_fd); 1029 1030 VOP_UNLOCK(rootvnode, 0, td); 1031} 1032 1033/* 1034 * Mount /devfs as our root filesystem, but do not put it on the mountlist 1035 * yet. Create a /dev -> / symlink so that absolute pathnames will lookup. 1036 */ 1037 1038static struct mount * 1039devfs_first(void) 1040{ 1041 struct thread *td = curthread; 1042 struct vfsconf *vfsp; 1043 struct mount *mp = NULL; 1044 int error; 1045 1046 vfsp = vfs_byname("devfs"); 1047 KASSERT(vfsp != NULL, ("Could not find devfs by name")); 1048 if (vfsp == NULL) 1049 return(NULL); 1050 1051 error = vfs_mount_alloc(NULLVP, vfsp, "/dev", td, &mp); 1052 KASSERT(error == 0, ("vfs_mount_alloc failed %d", error)); 1053 if (error) 1054 return (NULL); 1055 1056 error = VFS_MOUNT(mp, curthread); 1057 KASSERT(error == 0, ("VFS_MOUNT(devfs) failed %d", error)); 1058 if (error) 1059 return (NULL); 1060 1061 mtx_lock(&mountlist_mtx); 1062 TAILQ_INSERT_HEAD(&mountlist, mp, mnt_list); 1063 mtx_unlock(&mountlist_mtx); 1064 1065 set_rootvnode(td); 1066 1067 error = kern_symlink(td, "/", "dev", UIO_SYSSPACE); 1068 if (error) 1069 printf("kern_symlink /dev -> / returns %d\n", error); 1070 1071 return (mp); 1072} 1073 1074/* 1075 * Surgically move our devfs to be mounted on /dev. 1076 */ 1077 1078static void 1079devfs_fixup(struct thread *td) 1080{ 1081 struct nameidata nd; 1082 int error; 1083 struct vnode *vp, *dvp; 1084 struct mount *mp; 1085 1086 /* Remove our devfs mount from the mountlist and purge the cache */ 1087 mtx_lock(&mountlist_mtx); 1088 mp = TAILQ_FIRST(&mountlist); 1089 TAILQ_REMOVE(&mountlist, mp, mnt_list); 1090 mtx_unlock(&mountlist_mtx); 1091 cache_purgevfs(mp); 1092 1093 VFS_ROOT(mp, LK_EXCLUSIVE, &dvp, td); 1094 VI_LOCK(dvp); 1095 dvp->v_iflag &= ~VI_MOUNT; 1096 dvp->v_mountedhere = NULL; 1097 VI_UNLOCK(dvp); 1098 1099 /* Set up the real rootvnode, and purge the cache */ 1100 TAILQ_FIRST(&mountlist)->mnt_vnodecovered = NULL; 1101 set_rootvnode(td); 1102 cache_purgevfs(rootvnode->v_mount); 1103 1104 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, "/dev", td); 1105 error = namei(&nd); 1106 if (error) { 1107 printf("Lookup of /dev for devfs, error: %d\n", error); 1108 return; 1109 } 1110 NDFREE(&nd, NDF_ONLY_PNBUF); 1111 vp = nd.ni_vp; 1112 if (vp->v_type != VDIR) { 1113 vput(vp); 1114 } 1115 error = vinvalbuf(vp, V_SAVE, td, 0, 0); 1116 if (error) { 1117 vput(vp); 1118 } 1119 cache_purge(vp); 1120 mp->mnt_vnodecovered = vp; 1121 vp->v_mountedhere = mp; 1122 mtx_lock(&mountlist_mtx); 1123 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list); 1124 mtx_unlock(&mountlist_mtx); 1125 VOP_UNLOCK(vp, 0, td); 1126 vfs_unbusy(mp, td); 1127 vput(dvp); 1128 1129 /* Unlink the no longer needed /dev/dev -> / symlink */ 1130 kern_unlink(td, "/dev/dev", UIO_SYSSPACE); 1131} 1132 1133/* 1134 * Find and mount the root filesystem 1135 */ 1136void 1137vfs_mountroot(void) 1138{ 1139 char *cp; 1140 int error, i, asked = 0; 1141 struct mount *mp; 1142 1143 /* 1144 * Wait for GEOM to settle down 1145 */ 1146 DROP_GIANT(); 1147 g_waitidle(); 1148 PICKUP_GIANT(); 1149 1150 mp = devfs_first(); 1151 1152 /* 1153 * We are booted with instructions to prompt for the root filesystem. 1154 */ 1155 if (boothowto & RB_ASKNAME) { 1156 if (!vfs_mountroot_ask()) 1157 return; 1158 asked = 1; 1159 } 1160 1161 /* 1162 * The root filesystem information is compiled in, and we are 1163 * booted with instructions to use it. 1164 */ 1165 if (ctrootdevname != NULL && (boothowto & RB_DFLTROOT)) { 1166 if (!vfs_mountroot_try(ctrootdevname)) 1167 return; 1168 ctrootdevname = NULL; 1169 } 1170 1171 /* 1172 * We've been given the generic "use CDROM as root" flag. This is 1173 * necessary because one media may be used in many different 1174 * devices, so we need to search for them. 1175 */ 1176 if (boothowto & RB_CDROM) { 1177 for (i = 0; cdrom_rootdevnames[i] != NULL; i++) { 1178 if (!vfs_mountroot_try(cdrom_rootdevnames[i])) 1179 return; 1180 } 1181 } 1182 1183 /* 1184 * Try to use the value read by the loader from /etc/fstab, or 1185 * supplied via some other means. This is the preferred 1186 * mechanism. 1187 */ 1188 cp = getenv("vfs.root.mountfrom"); 1189 if (cp != NULL) { 1190 error = vfs_mountroot_try(cp); 1191 freeenv(cp); 1192 if (!error) 1193 return; 1194 } 1195 1196 /* 1197 * Try values that may have been computed by code during boot 1198 */ 1199 if (!vfs_mountroot_try(rootdevnames[0])) 1200 return; 1201 if (!vfs_mountroot_try(rootdevnames[1])) 1202 return; 1203 1204 /* 1205 * If we (still) have a compiled-in default, try it. 1206 */ 1207 if (ctrootdevname != NULL) 1208 if (!vfs_mountroot_try(ctrootdevname)) 1209 return; 1210 /* 1211 * Everything so far has failed, prompt on the console if we haven't 1212 * already tried that. 1213 */ 1214 if (!asked) 1215 if (!vfs_mountroot_ask()) 1216 return; 1217 1218 panic("Root mount failed, startup aborted."); 1219} 1220 1221/* 1222 * Mount (mountfrom) as the root filesystem. 1223 */ 1224static int 1225vfs_mountroot_try(const char *mountfrom) 1226{ 1227 struct mount *mp; 1228 char *vfsname, *path; 1229 int error; 1230 char patt[32]; 1231 int s; 1232 1233 vfsname = NULL; 1234 path = NULL; 1235 mp = NULL; 1236 error = EINVAL; 1237 1238 if (mountfrom == NULL) 1239 return (error); /* don't complain */ 1240 1241 s = splcam(); /* Overkill, but annoying without it */ 1242 printf("Trying to mount root from %s\n", mountfrom); 1243 splx(s); 1244 1245 /* parse vfs name and path */ 1246 vfsname = malloc(MFSNAMELEN, M_MOUNT, M_WAITOK); 1247 path = malloc(MNAMELEN, M_MOUNT, M_WAITOK); 1248 vfsname[0] = path[0] = 0; 1249 sprintf(patt, "%%%d[a-z0-9]:%%%ds", MFSNAMELEN, MNAMELEN); 1250 if (sscanf(mountfrom, patt, vfsname, path) < 1) { 1251 free(path, M_MOUNT); 1252 free(vfsname, M_MOUNT); 1253 return (error); 1254 } 1255 1256 if (path[0] == '\0') 1257 strcpy(path, ROOTNAME); 1258 1259 error = kernel_vmount( 1260 MNT_RDONLY | MNT_ROOTFS, 1261 "fstype", vfsname, 1262 "fspath", "/", 1263 "from", path, 1264 NULL); 1265 if (error == 0) { 1266 /* 1267 * We mount devfs prior to mounting the / FS, so the first 1268 * entry will typically be devfs. 1269 */ 1270 mp = TAILQ_FIRST(&mountlist); 1271 KASSERT(mp != NULL, ("%s: mountlist is empty", __func__)); 1272 vfs_unbusy(mp, curthread); 1273 1274 /* 1275 * Iterate over all currently mounted file systems and use 1276 * the time stamp found to check and/or initialize the RTC. 1277 * Typically devfs has no time stamp and the only other FS 1278 * is the actual / FS. 1279 */ 1280 do { 1281 if (mp->mnt_time != 0) 1282 inittodr(mp->mnt_time); 1283 mp = TAILQ_NEXT(mp, mnt_list); 1284 } while (mp != NULL); 1285 1286 devfs_fixup(curthread); 1287 } 1288 return (error); 1289} 1290 1291/* 1292 * --------------------------------------------------------------------- 1293 * Interactive root filesystem selection code. 1294 */ 1295 1296static int 1297vfs_mountroot_ask(void) 1298{ 1299 char name[128]; 1300 1301 for(;;) { 1302 printf("\nManual root filesystem specification:\n"); 1303 printf(" <fstype>:<device> Mount <device> using filesystem <fstype>\n"); 1304#if defined(__i386__) || defined(__ia64__) 1305 printf(" eg. ufs:da0s1a\n"); 1306#else 1307 printf(" eg. ufs:/dev/da0a\n"); 1308#endif 1309 printf(" ? List valid disk boot devices\n"); 1310 printf(" <empty line> Abort manual input\n"); 1311 printf("\nmountroot> "); 1312 gets(name, sizeof(name), 1); 1313 if (name[0] == '\0') 1314 return (1); 1315 if (name[0] == '?') { 1316 printf("\nList of GEOM managed disk devices:\n "); 1317 g_dev_print(); 1318 continue; 1319 } 1320 if (!vfs_mountroot_try(name)) 1321 return (0); 1322 } 1323} 1324 1325/* 1326 * --------------------------------------------------------------------- 1327 * Functions for querying mount options/arguments from filesystems. 1328 */ 1329 1330/* 1331 * Check that no unknown options are given 1332 */ 1333int 1334vfs_filteropt(struct vfsoptlist *opts, const char **legal) 1335{ 1336 struct vfsopt *opt; 1337 const char **t, *p; 1338 1339 1340 TAILQ_FOREACH(opt, opts, link) { 1341 p = opt->name; 1342 if (p[0] == 'n' && p[1] == 'o') 1343 p += 2; 1344 for(t = global_opts; *t != NULL; t++) 1345 if (!strcmp(*t, p)) 1346 break; 1347 if (*t != NULL) 1348 continue; 1349 for(t = legal; *t != NULL; t++) 1350 if (!strcmp(*t, p)) 1351 break; 1352 if (*t != NULL) 1353 continue; 1354 printf("mount option <%s> is unknown\n", p); 1355 return (EINVAL); 1356 } 1357 return (0); 1358} 1359 1360/* 1361 * Get a mount option by its name. 1362 * 1363 * Return 0 if the option was found, ENOENT otherwise. 1364 * If len is non-NULL it will be filled with the length 1365 * of the option. If buf is non-NULL, it will be filled 1366 * with the address of the option. 1367 */ 1368int 1369vfs_getopt(opts, name, buf, len) 1370 struct vfsoptlist *opts; 1371 const char *name; 1372 void **buf; 1373 int *len; 1374{ 1375 struct vfsopt *opt; 1376 1377 KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL")); 1378 1379 TAILQ_FOREACH(opt, opts, link) { 1380 if (strcmp(name, opt->name) == 0) { 1381 if (len != NULL) 1382 *len = opt->len; 1383 if (buf != NULL) 1384 *buf = opt->value; 1385 return (0); 1386 } 1387 } 1388 return (ENOENT); 1389} 1390 1391char * 1392vfs_getopts(struct vfsoptlist *opts, const char *name, int *error) 1393{ 1394 struct vfsopt *opt; 1395 1396 *error = 0; 1397 TAILQ_FOREACH(opt, opts, link) { 1398 if (strcmp(name, opt->name) != 0) 1399 continue; 1400 if (((char *)opt->value)[opt->len - 1] != '\0') { 1401 *error = EINVAL; 1402 return (NULL); 1403 } 1404 return (opt->value); 1405 } 1406 return (NULL); 1407} 1408 1409int 1410vfs_flagopt(struct vfsoptlist *opts, const char *name, u_int *w, u_int val) 1411{ 1412 struct vfsopt *opt; 1413 1414 TAILQ_FOREACH(opt, opts, link) { 1415 if (strcmp(name, opt->name) == 0) { 1416 if (w != NULL) 1417 *w |= val; 1418 return (1); 1419 } 1420 } 1421 if (w != NULL) 1422 *w &= ~val; 1423 return (0); 1424} 1425 1426int 1427vfs_scanopt(struct vfsoptlist *opts, const char *name, const char *fmt, ...) 1428{ 1429 va_list ap; 1430 struct vfsopt *opt; 1431 int ret; 1432 1433 KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL")); 1434 1435 TAILQ_FOREACH(opt, opts, link) { 1436 if (strcmp(name, opt->name) != 0) 1437 continue; 1438 if (((char *)opt->value)[opt->len - 1] != '\0') 1439 return (0); 1440 va_start(ap, fmt); 1441 ret = vsscanf(opt->value, fmt, ap); 1442 va_end(ap); 1443 return (ret); 1444 } 1445 return (0); 1446} 1447 1448/* 1449 * Find and copy a mount option. 1450 * 1451 * The size of the buffer has to be specified 1452 * in len, if it is not the same length as the 1453 * mount option, EINVAL is returned. 1454 * Returns ENOENT if the option is not found. 1455 */ 1456int 1457vfs_copyopt(opts, name, dest, len) 1458 struct vfsoptlist *opts; 1459 const char *name; 1460 void *dest; 1461 int len; 1462{ 1463 struct vfsopt *opt; 1464 1465 KASSERT(opts != NULL, ("vfs_copyopt: caller passed 'opts' as NULL")); 1466 1467 TAILQ_FOREACH(opt, opts, link) { 1468 if (strcmp(name, opt->name) == 0) { 1469 if (len != opt->len) 1470 return (EINVAL); 1471 bcopy(opt->value, dest, opt->len); 1472 return (0); 1473 } 1474 } 1475 return (ENOENT); 1476} 1477 1478/* 1479 * This is a helper function for filesystems to traverse their 1480 * vnodes. See MNT_VNODE_FOREACH() in sys/mount.h 1481 */ 1482 1483struct vnode * 1484__mnt_vnode_next(struct vnode **nvp, struct mount *mp) 1485{ 1486 struct vnode *vp; 1487 1488 mtx_assert(&mp->mnt_mtx, MA_OWNED); 1489 1490 vp = *nvp; 1491 /* Check if we are done */ 1492 if (vp == NULL) 1493 return (NULL); 1494 /* If our next vnode is no longer ours, start over */ 1495 if (vp->v_mount != mp) 1496 vp = TAILQ_FIRST(&mp->mnt_nvnodelist); 1497 /* Save pointer to next vnode in list */ 1498 if (vp != NULL) 1499 *nvp = TAILQ_NEXT(vp, v_nmntvnodes); 1500 else 1501 *nvp = NULL; 1502 return (vp); 1503} 1504 1505int 1506__vfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td) 1507{ 1508 int error; 1509 1510 error = mp->mnt_op->vfs_statfs(mp, &mp->mnt_stat, td); 1511 if (sbp != &mp->mnt_stat) 1512 *sbp = mp->mnt_stat; 1513 return (error); 1514} 1515 1516void 1517vfs_mountedfrom(struct mount *mp, const char *from) 1518{ 1519 1520 bzero(mp->mnt_stat.f_mntfromname, sizeof mp->mnt_stat.f_mntfromname); 1521 strlcpy(mp->mnt_stat.f_mntfromname, from, 1522 sizeof mp->mnt_stat.f_mntfromname); 1523} 1524 1525/* 1526 * --------------------------------------------------------------------- 1527 * This is the api for building mount args and mounting filesystems from 1528 * inside the kernel. 1529 * 1530 * The API works by accumulation of individual args. First error is 1531 * latched. 1532 * 1533 * XXX: should be documented in new manpage kernel_mount(9) 1534 */ 1535 1536/* A memory allocation which must be freed when we are done */ 1537struct mntaarg { 1538 SLIST_ENTRY(mntaarg) next; 1539}; 1540 1541/* The header for the mount arguments */ 1542struct mntarg { 1543 struct iovec *v; 1544 int len; 1545 int error; 1546 SLIST_HEAD(, mntaarg) list; 1547}; 1548 1549/* 1550 * Add a boolean argument. 1551 * 1552 * flag is the boolean value. 1553 * name must start with "no". 1554 */ 1555struct mntarg * 1556mount_argb(struct mntarg *ma, int flag, const char *name) 1557{ 1558 1559 KASSERT(name[0] == 'n' && name[1] == 'o', 1560 ("mount_argb(...,%s): name must start with 'no'", name)); 1561 1562 return (mount_arg(ma, name + (flag ? 2 : 0), NULL, 0)); 1563} 1564 1565/* 1566 * Add an argument printf style 1567 */ 1568struct mntarg * 1569mount_argf(struct mntarg *ma, const char *name, const char *fmt, ...) 1570{ 1571 va_list ap; 1572 struct mntaarg *maa; 1573 struct sbuf *sb; 1574 int len; 1575 1576 if (ma == NULL) { 1577 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO); 1578 SLIST_INIT(&ma->list); 1579 } 1580 if (ma->error) 1581 return (ma); 1582 1583 ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2), 1584 M_MOUNT, M_WAITOK); 1585 ma->v[ma->len].iov_base = (void *)(uintptr_t)name; 1586 ma->v[ma->len].iov_len = strlen(name) + 1; 1587 ma->len++; 1588 1589 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND); 1590 va_start(ap, fmt); 1591 sbuf_vprintf(sb, fmt, ap); 1592 va_end(ap); 1593 sbuf_finish(sb); 1594 len = sbuf_len(sb) + 1; 1595 maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO); 1596 SLIST_INSERT_HEAD(&ma->list, maa, next); 1597 bcopy(sbuf_data(sb), maa + 1, len); 1598 sbuf_delete(sb); 1599 1600 ma->v[ma->len].iov_base = maa + 1; 1601 ma->v[ma->len].iov_len = len; 1602 ma->len++; 1603 1604 return (ma); 1605} 1606 1607/* 1608 * Add an argument which is a userland string. 1609 */ 1610struct mntarg * 1611mount_argsu(struct mntarg *ma, const char *name, const void *val, int len) 1612{ 1613 struct mntaarg *maa; 1614 char *tbuf; 1615 1616 if (val == NULL) 1617 return (ma); 1618 if (ma == NULL) { 1619 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO); 1620 SLIST_INIT(&ma->list); 1621 } 1622 if (ma->error) 1623 return (ma); 1624 maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO); 1625 SLIST_INSERT_HEAD(&ma->list, maa, next); 1626 tbuf = (void *)(maa + 1); 1627 ma->error = copyinstr(val, tbuf, len, NULL); 1628 return (mount_arg(ma, name, tbuf, -1)); 1629} 1630 1631/* 1632 * Plain argument. 1633 * 1634 * If length is -1, use printf. 1635 */ 1636struct mntarg * 1637mount_arg(struct mntarg *ma, const char *name, const void *val, int len) 1638{ 1639 1640 if (ma == NULL) { 1641 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO); 1642 SLIST_INIT(&ma->list); 1643 } 1644 if (ma->error) 1645 return (ma); 1646 1647 ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2), 1648 M_MOUNT, M_WAITOK); 1649 ma->v[ma->len].iov_base = (void *)(uintptr_t)name; 1650 ma->v[ma->len].iov_len = strlen(name) + 1; 1651 ma->len++; 1652 1653 ma->v[ma->len].iov_base = (void *)(uintptr_t)val; 1654 if (len < 0) 1655 ma->v[ma->len].iov_len = strlen(val) + 1; 1656 else 1657 ma->v[ma->len].iov_len = len; 1658 ma->len++; 1659 return (ma); 1660} 1661 1662/* 1663 * Free a mntarg structure 1664 */ 1665static void 1666free_mntarg(struct mntarg *ma) 1667{ 1668 struct mntaarg *maa; 1669 1670 while (!SLIST_EMPTY(&ma->list)) { 1671 maa = SLIST_FIRST(&ma->list); 1672 SLIST_REMOVE_HEAD(&ma->list, next); 1673 free(maa, M_MOUNT); 1674 } 1675 free(ma->v, M_MOUNT); 1676 free(ma, M_MOUNT); 1677} 1678 1679/* 1680 * Mount a filesystem 1681 */ 1682int 1683kernel_mount(struct mntarg *ma, int flags) 1684{ 1685 struct uio auio; 1686 int error; 1687 1688 KASSERT(ma != NULL, ("kernel_mount NULL ma")); 1689 KASSERT(ma->v != NULL, ("kernel_mount NULL ma->v")); 1690 KASSERT(!(ma->len & 1), ("kernel_mount odd ma->len (%d)", ma->len)); 1691 1692 auio.uio_iov = ma->v; 1693 auio.uio_iovcnt = ma->len; 1694 auio.uio_segflg = UIO_SYSSPACE; 1695 1696 error = ma->error; 1697 if (!error) 1698 error = vfs_donmount(curthread, flags, &auio); 1699 free_mntarg(ma); 1700 return (error); 1701} 1702 1703/* 1704 * A printflike function to mount a filesystem. 1705 */ 1706int 1707kernel_vmount(int flags, ...) 1708{ 1709 struct mntarg *ma = NULL; 1710 va_list ap; 1711 const char *cp; 1712 const void *vp; 1713 int error; 1714 1715 va_start(ap, flags); 1716 for (;;) { 1717 cp = va_arg(ap, const char *); 1718 if (cp == NULL) 1719 break; 1720 vp = va_arg(ap, const void *); 1721 ma = mount_arg(ma, cp, vp, -1); 1722 } 1723 va_end(ap); 1724 1725 error = kernel_mount(ma, flags); 1726 return (error); 1727} 1728