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