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$"); 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 76 static int vfs_domount(struct thread *td, const char *fstype, 77 char *fspath, int fsflags, void *fsdata); 78 static int vfs_mount_alloc(struct vnode *dvp, struct vfsconf *vfsp, 79 const char *fspath, struct thread *td, struct mount **mpp); 80 static int vfs_mountroot_ask(void); 81 static int vfs_mountroot_try(const char *mountfrom); 82 static int vfs_donmount(struct thread *td, int fsflags, 83 struct uio *fsoptions); 84 static void free_mntarg(struct mntarg *ma); 85 static void vfs_mount_destroy(struct mount *, struct thread *); 86 87 static int usermount = 0; 88 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0, 89 "Unprivileged users may mount and unmount file systems"); 90 91 MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount structure"); 92 93 /* List of mounted filesystems. */ 94 struct mntlist mountlist = TAILQ_HEAD_INITIALIZER(mountlist); 95 96 /* For any iteration/modification of mountlist */ 97 struct mtx mountlist_mtx; 98 99 TAILQ_HEAD(vfsoptlist, vfsopt); 100 struct 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 */ 111 struct 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 */ 127 static 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 */ 139 static char *cdrom_rootdevnames[] = { 140 "cd9660:cd0", 141 "cd9660:acd0", 142 NULL 143 }; 144 145 /* legacy find-root code */ 146 char *rootdevnames[2] = {NULL, NULL}; 147 #ifndef ROOTDEVNAME 148 # define ROOTDEVNAME NULL 149 #endif 150 static const char *ctrootdevname = ROOTDEVNAME; 151 152 /* 153 * --------------------------------------------------------------------- 154 * Functions for building and sanitizing the mount options 155 */ 156 157 /* Remove one mount option. */ 158 static void 159 vfs_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. */ 175 static void 176 vfs_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 */ 190 static int 191 vfs_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 */ 211 static void 212 vfs_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 */ 233 static int 234 vfs_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); 302 bad: 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 */ 311 static void 312 vfs_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); 344 next: 345 continue; 346 } 347 } 348 349 /* 350 * --------------------------------------------------------------------- 351 * Mount a filesystem 352 */ 353 int 354 nmount(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 */ 404 static int 405 vfs_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 arc4rand(&mp->mnt_hashseed, sizeof mp->mnt_hashseed, 0); 432 *mpp = mp; 433 return (0); 434 } 435 436 /* 437 * Destroy the mount struct previously allocated by vfs_mount_alloc(). 438 */ 439 static void 440 vfs_mount_destroy(struct mount *mp, struct thread *td) 441 { 442 443 mp->mnt_vfc->vfc_refcount--; 444 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist)) 445 panic("unmount: dangling vnode"); 446 vfs_unbusy(mp,td); 447 lockdestroy(&mp->mnt_lock); 448 MNT_ILOCK(mp); 449 if (mp->mnt_kern_flag & MNTK_MWAIT) 450 wakeup(mp); 451 MNT_IUNLOCK(mp); 452 mtx_destroy(&mp->mnt_mtx); 453 #ifdef MAC 454 mac_destroy_mount(mp); 455 #endif 456 if (mp->mnt_opt != NULL) 457 vfs_freeopts(mp->mnt_opt); 458 crfree(mp->mnt_cred); 459 free(mp, M_MOUNT); 460 } 461 462 static int 463 vfs_donmount(struct thread *td, int fsflags, struct uio *fsoptions) 464 { 465 struct vfsoptlist *optlist; 466 char *fstype, *fspath; 467 int error, fstypelen, fspathlen; 468 469 error = vfs_buildopts(fsoptions, &optlist); 470 if (error) 471 return (error); 472 473 /* 474 * We need these two options before the others, 475 * and they are mandatory for any filesystem. 476 * Ensure they are NUL terminated as well. 477 */ 478 fstypelen = 0; 479 error = vfs_getopt(optlist, "fstype", (void **)&fstype, &fstypelen); 480 if (error || fstype[fstypelen - 1] != '\0') { 481 error = EINVAL; 482 goto bail; 483 } 484 fspathlen = 0; 485 error = vfs_getopt(optlist, "fspath", (void **)&fspath, &fspathlen); 486 if (error || fspath[fspathlen - 1] != '\0') { 487 error = EINVAL; 488 goto bail; 489 } 490 491 /* 492 * Be ultra-paranoid about making sure the type and fspath 493 * variables will fit in our mp buffers, including the 494 * terminating NUL. 495 */ 496 if (fstypelen >= MFSNAMELEN - 1 || fspathlen >= MNAMELEN - 1) { 497 error = ENAMETOOLONG; 498 goto bail; 499 } 500 501 mtx_lock(&Giant); 502 error = vfs_domount(td, fstype, fspath, fsflags, optlist); 503 mtx_unlock(&Giant); 504 bail: 505 if (error) 506 vfs_freeopts(optlist); 507 return (error); 508 } 509 510 /* 511 * --------------------------------------------------------------------- 512 * Old mount API. 513 */ 514 #ifndef _SYS_SYSPROTO_H_ 515 struct mount_args { 516 char *type; 517 char *path; 518 int flags; 519 caddr_t data; 520 }; 521 #endif 522 /* ARGSUSED */ 523 int 524 mount(td, uap) 525 struct thread *td; 526 struct mount_args /* { 527 char *type; 528 char *path; 529 int flags; 530 caddr_t data; 531 } */ *uap; 532 { 533 char *fstype; 534 struct vfsconf *vfsp = NULL; 535 struct mntarg *ma = NULL; 536 int error; 537 538 /* Kick out MNT_ROOTFS early as it is legal internally */ 539 uap->flags &= ~MNT_ROOTFS; 540 541 if (uap->data == NULL) 542 return (EINVAL); 543 544 fstype = malloc(MFSNAMELEN, M_TEMP, M_WAITOK); 545 error = copyinstr(uap->type, fstype, MFSNAMELEN, NULL); 546 if (!error) { 547 mtx_lock(&Giant); /* XXX ? */ 548 vfsp = vfs_byname_kld(fstype, td, &error); 549 mtx_unlock(&Giant); 550 } 551 free(fstype, M_TEMP); 552 if (error) 553 return (error); 554 if (vfsp == NULL) 555 return (ENOENT); 556 if (vfsp->vfc_vfsops->vfs_cmount == NULL) 557 return (EOPNOTSUPP); 558 559 ma = mount_argsu(ma, "fstype", uap->type, MNAMELEN); 560 ma = mount_argsu(ma, "fspath", uap->path, MNAMELEN); 561 ma = mount_argb(ma, uap->flags & MNT_RDONLY, "noro"); 562 ma = mount_argb(ma, !(uap->flags & MNT_NOSUID), "nosuid"); 563 ma = mount_argb(ma, !(uap->flags & MNT_NOEXEC), "noexec"); 564 565 error = vfsp->vfc_vfsops->vfs_cmount(ma, uap->data, uap->flags, td); 566 return (error); 567 } 568 569 570 /* 571 * vfs_domount(): actually attempt a filesystem mount. 572 */ 573 static int 574 vfs_domount( 575 struct thread *td, /* Flags common to all filesystems. */ 576 const char *fstype, /* Filesystem type. */ 577 char *fspath, /* Mount path. */ 578 int fsflags, /* Flags common to all filesystems. */ 579 void *fsdata /* Options local to the filesystem. */ 580 ) 581 { 582 struct vnode *vp; 583 struct mount *mp; 584 struct vfsconf *vfsp; 585 int error, flag = 0, kern_flag = 0; 586 struct vattr va; 587 struct nameidata nd; 588 589 mtx_assert(&Giant, MA_OWNED); 590 591 /* 592 * Be ultra-paranoid about making sure the type and fspath 593 * variables will fit in our mp buffers, including the 594 * terminating NUL. 595 */ 596 if (strlen(fstype) >= MFSNAMELEN || strlen(fspath) >= MNAMELEN) 597 return (ENAMETOOLONG); 598 599 if (jailed(td->td_ucred)) 600 return (EPERM); 601 if (usermount == 0) { 602 if ((error = suser(td)) != 0) 603 return (error); 604 } 605 606 /* 607 * Do not allow NFS export or MNT_SUIDDIR by unprivileged users. 608 */ 609 if (fsflags & (MNT_EXPORTED | MNT_SUIDDIR)) { 610 if ((error = suser(td)) != 0) 611 return (error); 612 } 613 /* 614 * Silently enforce MNT_NOSUID and MNT_USER for 615 * unprivileged users. 616 */ 617 if (suser(td) != 0) 618 fsflags |= MNT_NOSUID | MNT_USER; 619 /* 620 * Get vnode to be covered 621 */ 622 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspath, td); 623 if ((error = namei(&nd)) != 0) 624 return (error); 625 NDFREE(&nd, NDF_ONLY_PNBUF); 626 vp = nd.ni_vp; 627 if (fsflags & MNT_UPDATE) { 628 if ((vp->v_vflag & VV_ROOT) == 0) { 629 vput(vp); 630 return (EINVAL); 631 } 632 mp = vp->v_mount; 633 flag = mp->mnt_flag; 634 kern_flag = mp->mnt_kern_flag; 635 /* 636 * We only allow the filesystem to be reloaded if it 637 * is currently mounted read-only. 638 */ 639 if ((fsflags & MNT_RELOAD) && 640 ((mp->mnt_flag & MNT_RDONLY) == 0)) { 641 vput(vp); 642 return (EOPNOTSUPP); /* Needs translation */ 643 } 644 /* 645 * Only privileged root, or (if MNT_USER is set) the user that 646 * did the original mount is permitted to update it. 647 */ 648 error = vfs_suser(mp, td); 649 if (error) { 650 vput(vp); 651 return (error); 652 } 653 if (vfs_busy(mp, LK_NOWAIT, 0, td)) { 654 vput(vp); 655 return (EBUSY); 656 } 657 VI_LOCK(vp); 658 if ((vp->v_iflag & VI_MOUNT) != 0 || 659 vp->v_mountedhere != NULL) { 660 VI_UNLOCK(vp); 661 vfs_unbusy(mp, td); 662 vput(vp); 663 return (EBUSY); 664 } 665 vp->v_iflag |= VI_MOUNT; 666 VI_UNLOCK(vp); 667 mp->mnt_flag |= fsflags & 668 (MNT_RELOAD | MNT_FORCE | MNT_UPDATE | MNT_SNAPSHOT | MNT_ROOTFS); 669 VOP_UNLOCK(vp, 0, td); 670 mp->mnt_optnew = fsdata; 671 vfs_mergeopts(mp->mnt_optnew, mp->mnt_opt); 672 } else { 673 /* 674 * If the user is not root, ensure that they own the directory 675 * onto which we are attempting to mount. 676 */ 677 error = VOP_GETATTR(vp, &va, td->td_ucred, td); 678 if (error) { 679 vput(vp); 680 return (error); 681 } 682 if (va.va_uid != td->td_ucred->cr_uid) { 683 if ((error = suser(td)) != 0) { 684 vput(vp); 685 return (error); 686 } 687 } 688 error = vinvalbuf(vp, V_SAVE, td, 0, 0); 689 if (error != 0) { 690 vput(vp); 691 return (error); 692 } 693 if (vp->v_type != VDIR) { 694 vput(vp); 695 return (ENOTDIR); 696 } 697 vfsp = vfs_byname_kld(fstype, td, &error); 698 if (vfsp == NULL) { 699 vput(vp); 700 return (error); 701 } 702 VI_LOCK(vp); 703 if ((vp->v_iflag & VI_MOUNT) != 0 || 704 vp->v_mountedhere != NULL) { 705 VI_UNLOCK(vp); 706 vput(vp); 707 return (EBUSY); 708 } 709 vp->v_iflag |= VI_MOUNT; 710 VI_UNLOCK(vp); 711 712 /* 713 * Allocate and initialize the filesystem. 714 */ 715 error = vfs_mount_alloc(vp, vfsp, fspath, td, &mp); 716 if (error) { 717 vput(vp); 718 return (error); 719 } 720 VOP_UNLOCK(vp, 0, td); 721 722 /* XXXMAC: pass to vfs_mount_alloc? */ 723 mp->mnt_optnew = fsdata; 724 } 725 726 /* 727 * Set the mount level flags. 728 */ 729 if (fsflags & MNT_RDONLY) 730 mp->mnt_flag |= MNT_RDONLY; 731 mp->mnt_flag &=~ MNT_UPDATEMASK; 732 mp->mnt_flag |= fsflags & (MNT_UPDATEMASK | MNT_FORCE | MNT_ROOTFS); 733 /* 734 * Mount the filesystem. 735 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they 736 * get. No freeing of cn_pnbuf. 737 */ 738 error = VFS_MOUNT(mp, td); 739 if (!error) { 740 if (mp->mnt_opt != NULL) 741 vfs_freeopts(mp->mnt_opt); 742 mp->mnt_opt = mp->mnt_optnew; 743 VFS_STATFS(mp, &mp->mnt_stat, td); 744 } 745 /* 746 * Prevent external consumers of mount options from reading 747 * mnt_optnew. 748 */ 749 mp->mnt_optnew = NULL; 750 if (mp->mnt_flag & MNT_UPDATE) { 751 mp->mnt_flag &= 752 ~(MNT_UPDATE | MNT_RELOAD | MNT_FORCE | MNT_SNAPSHOT); 753 if (error) { 754 mp->mnt_flag = flag; 755 mp->mnt_kern_flag = kern_flag; 756 } 757 if ((mp->mnt_flag & MNT_RDONLY) == 0) { 758 if (mp->mnt_syncer == NULL) 759 error = vfs_allocate_syncvnode(mp); 760 } else { 761 if (mp->mnt_syncer != NULL) 762 vrele(mp->mnt_syncer); 763 mp->mnt_syncer = NULL; 764 } 765 vfs_unbusy(mp, td); 766 VI_LOCK(vp); 767 vp->v_iflag &= ~VI_MOUNT; 768 VI_UNLOCK(vp); 769 vrele(vp); 770 return (error); 771 } 772 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 773 /* 774 * Put the new filesystem on the mount list after root. 775 */ 776 cache_purge(vp); 777 if (!error) { 778 struct vnode *newdp; 779 780 VI_LOCK(vp); 781 vp->v_iflag &= ~VI_MOUNT; 782 VI_UNLOCK(vp); 783 vp->v_mountedhere = mp; 784 mtx_lock(&mountlist_mtx); 785 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list); 786 mtx_unlock(&mountlist_mtx); 787 vfs_event_signal(NULL, VQ_MOUNT, 0); 788 if (VFS_ROOT(mp, &newdp, td)) 789 panic("mount: lost mount"); 790 mountcheckdirs(vp, newdp); 791 vput(newdp); 792 VOP_UNLOCK(vp, 0, td); 793 if ((mp->mnt_flag & MNT_RDONLY) == 0) 794 error = vfs_allocate_syncvnode(mp); 795 vfs_unbusy(mp, td); 796 if (error) 797 vrele(vp); 798 } else { 799 VI_LOCK(vp); 800 vp->v_iflag &= ~VI_MOUNT; 801 VI_UNLOCK(vp); 802 vfs_mount_destroy(mp, td); 803 vput(vp); 804 } 805 return (error); 806 } 807 808 /* 809 * --------------------------------------------------------------------- 810 * Unmount a filesystem. 811 * 812 * Note: unmount takes a path to the vnode mounted on as argument, 813 * not special file (as before). 814 */ 815 #ifndef _SYS_SYSPROTO_H_ 816 struct unmount_args { 817 char *path; 818 int flags; 819 }; 820 #endif 821 /* ARGSUSED */ 822 int 823 unmount(td, uap) 824 struct thread *td; 825 register struct unmount_args /* { 826 char *path; 827 int flags; 828 } */ *uap; 829 { 830 struct mount *mp; 831 char *pathbuf; 832 int error, id0, id1; 833 834 if (jailed(td->td_ucred)) 835 return (EPERM); 836 if (usermount == 0) { 837 if ((error = suser(td)) != 0) 838 return (error); 839 } 840 841 pathbuf = malloc(MNAMELEN, M_TEMP, M_WAITOK); 842 error = copyinstr(uap->path, pathbuf, MNAMELEN, NULL); 843 if (error) { 844 free(pathbuf, M_TEMP); 845 return (error); 846 } 847 if (uap->flags & MNT_BYFSID) { 848 /* Decode the filesystem ID. */ 849 if (sscanf(pathbuf, "FSID:%d:%d", &id0, &id1) != 2) { 850 free(pathbuf, M_TEMP); 851 return (EINVAL); 852 } 853 854 mtx_lock(&mountlist_mtx); 855 TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) { 856 if (mp->mnt_stat.f_fsid.val[0] == id0 && 857 mp->mnt_stat.f_fsid.val[1] == id1) 858 break; 859 } 860 mtx_unlock(&mountlist_mtx); 861 } else { 862 mtx_lock(&mountlist_mtx); 863 TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) { 864 if (strcmp(mp->mnt_stat.f_mntonname, pathbuf) == 0) 865 break; 866 } 867 mtx_unlock(&mountlist_mtx); 868 } 869 free(pathbuf, M_TEMP); 870 if (mp == NULL) { 871 /* 872 * Previously we returned ENOENT for a nonexistent path and 873 * EINVAL for a non-mountpoint. We cannot tell these apart 874 * now, so in the !MNT_BYFSID case return the more likely 875 * EINVAL for compatibility. 876 */ 877 return ((uap->flags & MNT_BYFSID) ? ENOENT : EINVAL); 878 } 879 880 /* 881 * Only privileged root, or (if MNT_USER is set) the user that did the 882 * original mount is permitted to unmount this filesystem. 883 */ 884 error = vfs_suser(mp, td); 885 if (error) 886 return (error); 887 888 /* 889 * Don't allow unmounting the root filesystem. 890 */ 891 if (mp->mnt_flag & MNT_ROOTFS) 892 return (EINVAL); 893 mtx_lock(&Giant); 894 error = dounmount(mp, uap->flags, td); 895 mtx_unlock(&Giant); 896 return (error); 897 } 898 899 /* 900 * Do the actual filesystem unmount. 901 */ 902 int 903 dounmount(mp, flags, td) 904 struct mount *mp; 905 int flags; 906 struct thread *td; 907 { 908 struct vnode *coveredvp, *fsrootvp; 909 int error; 910 int async_flag; 911 912 mtx_assert(&Giant, MA_OWNED); 913 914 MNT_ILOCK(mp); 915 if (mp->mnt_kern_flag & MNTK_UNMOUNT) { 916 MNT_IUNLOCK(mp); 917 return (EBUSY); 918 } 919 mp->mnt_kern_flag |= MNTK_UNMOUNT; 920 /* Allow filesystems to detect that a forced unmount is in progress. */ 921 if (flags & MNT_FORCE) 922 mp->mnt_kern_flag |= MNTK_UNMOUNTF; 923 error = lockmgr(&mp->mnt_lock, LK_DRAIN | LK_INTERLOCK | 924 ((flags & MNT_FORCE) ? 0 : LK_NOWAIT), MNT_MTX(mp), td); 925 if (error) { 926 MNT_ILOCK(mp); 927 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF); 928 if (mp->mnt_kern_flag & MNTK_MWAIT) 929 wakeup(mp); 930 MNT_IUNLOCK(mp); 931 return (error); 932 } 933 vn_start_write(NULL, &mp, V_WAIT); 934 935 if (mp->mnt_flag & MNT_EXPUBLIC) 936 vfs_setpublicfs(NULL, NULL, NULL); 937 938 vfs_msync(mp, MNT_WAIT); 939 async_flag = mp->mnt_flag & MNT_ASYNC; 940 mp->mnt_flag &= ~MNT_ASYNC; 941 cache_purgevfs(mp); /* remove cache entries for this file sys */ 942 if (mp->mnt_syncer != NULL) 943 vrele(mp->mnt_syncer); 944 /* 945 * For forced unmounts, move process cdir/rdir refs on the fs root 946 * vnode to the covered vnode. For non-forced unmounts we want 947 * such references to cause an EBUSY error. 948 */ 949 if ((flags & MNT_FORCE) && VFS_ROOT(mp, &fsrootvp, td) == 0) { 950 if (mp->mnt_vnodecovered != NULL) 951 mountcheckdirs(fsrootvp, mp->mnt_vnodecovered); 952 if (fsrootvp == rootvnode) { 953 vrele(rootvnode); 954 rootvnode = NULL; 955 } 956 vput(fsrootvp); 957 } 958 if (((mp->mnt_flag & MNT_RDONLY) || 959 (error = VFS_SYNC(mp, MNT_WAIT, td)) == 0) || 960 (flags & MNT_FORCE)) { 961 error = VFS_UNMOUNT(mp, flags, td); 962 } 963 vn_finished_write(mp); 964 if (error) { 965 /* Undo cdir/rdir and rootvnode changes made above. */ 966 if ((flags & MNT_FORCE) && VFS_ROOT(mp, &fsrootvp, td) == 0) { 967 if (mp->mnt_vnodecovered != NULL) 968 mountcheckdirs(mp->mnt_vnodecovered, fsrootvp); 969 if (rootvnode == NULL) { 970 rootvnode = fsrootvp; 971 vref(rootvnode); 972 } 973 vput(fsrootvp); 974 } 975 if ((mp->mnt_flag & MNT_RDONLY) == 0 && mp->mnt_syncer == NULL) 976 (void) vfs_allocate_syncvnode(mp); 977 MNT_ILOCK(mp); 978 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF); 979 mp->mnt_flag |= async_flag; 980 lockmgr(&mp->mnt_lock, LK_RELEASE, NULL, td); 981 if (mp->mnt_kern_flag & MNTK_MWAIT) 982 wakeup(mp); 983 MNT_IUNLOCK(mp); 984 return (error); 985 } 986 mtx_lock(&mountlist_mtx); 987 TAILQ_REMOVE(&mountlist, mp, mnt_list); 988 if ((coveredvp = mp->mnt_vnodecovered) != NULL) 989 coveredvp->v_mountedhere = NULL; 990 mtx_unlock(&mountlist_mtx); 991 vfs_event_signal(NULL, VQ_UNMOUNT, 0); 992 vfs_mount_destroy(mp, td); 993 if (coveredvp != NULL) 994 vrele(coveredvp); 995 return (0); 996 } 997 998 /* 999 * --------------------------------------------------------------------- 1000 * Mounting of root filesystem 1001 * 1002 */ 1003 1004 static void 1005 set_rootvnode(struct thread *td) 1006 { 1007 struct proc *p; 1008 1009 if (VFS_ROOT(TAILQ_FIRST(&mountlist), &rootvnode, td)) 1010 panic("Cannot find root vnode"); 1011 1012 p = td->td_proc; 1013 FILEDESC_LOCK(p->p_fd); 1014 1015 if (p->p_fd->fd_cdir != NULL) 1016 vrele(p->p_fd->fd_cdir); 1017 p->p_fd->fd_cdir = rootvnode; 1018 VREF(rootvnode); 1019 1020 if (p->p_fd->fd_rdir != NULL) 1021 vrele(p->p_fd->fd_rdir); 1022 p->p_fd->fd_rdir = rootvnode; 1023 VREF(rootvnode); 1024 1025 FILEDESC_UNLOCK(p->p_fd); 1026 1027 VOP_UNLOCK(rootvnode, 0, td); 1028 } 1029 1030 /* 1031 * Mount /devfs as our root filesystem, but do not put it on the mountlist 1032 * yet. Create a /dev -> / symlink so that absolute pathnames will lookup. 1033 */ 1034 1035 static struct mount * 1036 devfs_first(void) 1037 { 1038 struct thread *td = curthread; 1039 struct vfsconf *vfsp; 1040 struct mount *mp = NULL; 1041 int error; 1042 1043 vfsp = vfs_byname("devfs"); 1044 KASSERT(vfsp != NULL, ("Could not find devfs by name")); 1045 if (vfsp == NULL) 1046 return(NULL); 1047 1048 error = vfs_mount_alloc(NULLVP, vfsp, "/dev", td, &mp); 1049 KASSERT(error == 0, ("vfs_mount_alloc failed %d", error)); 1050 if (error) 1051 return (NULL); 1052 1053 error = VFS_MOUNT(mp, curthread); 1054 KASSERT(error == 0, ("VFS_MOUNT(devfs) failed %d", error)); 1055 if (error) 1056 return (NULL); 1057 1058 mtx_lock(&mountlist_mtx); 1059 TAILQ_INSERT_HEAD(&mountlist, mp, mnt_list); 1060 mtx_unlock(&mountlist_mtx); 1061 1062 set_rootvnode(td); 1063 1064 error = kern_symlink(td, "/", "dev", UIO_SYSSPACE); 1065 if (error) 1066 printf("kern_symlink /dev -> / returns %d\n", error); 1067 1068 return (mp); 1069 } 1070 1071 /* 1072 * Surgically move our devfs to be mounted on /dev. 1073 */ 1074 1075 static void 1076 devfs_fixup(struct thread *td) 1077 { 1078 struct nameidata nd; 1079 int error; 1080 struct vnode *vp, *dvp; 1081 struct mount *mp; 1082 1083 /* Remove our devfs mount from the mountlist and purge the cache */ 1084 mtx_lock(&mountlist_mtx); 1085 mp = TAILQ_FIRST(&mountlist); 1086 TAILQ_REMOVE(&mountlist, mp, mnt_list); 1087 mtx_unlock(&mountlist_mtx); 1088 cache_purgevfs(mp); 1089 1090 VFS_ROOT(mp, &dvp, td); 1091 VI_LOCK(dvp); 1092 dvp->v_iflag &= ~VI_MOUNT; 1093 dvp->v_mountedhere = NULL; 1094 VI_UNLOCK(dvp); 1095 1096 /* Set up the real rootvnode, and purge the cache */ 1097 TAILQ_FIRST(&mountlist)->mnt_vnodecovered = NULL; 1098 set_rootvnode(td); 1099 cache_purgevfs(rootvnode->v_mount); 1100 1101 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, "/dev", td); 1102 error = namei(&nd); 1103 if (error) { 1104 printf("Lookup of /dev for devfs, error: %d\n", error); 1105 return; 1106 } 1107 NDFREE(&nd, NDF_ONLY_PNBUF); 1108 vp = nd.ni_vp; 1109 if (vp->v_type != VDIR) { 1110 vput(vp); 1111 } 1112 error = vinvalbuf(vp, V_SAVE, td, 0, 0); 1113 if (error) { 1114 vput(vp); 1115 } 1116 cache_purge(vp); 1117 mp->mnt_vnodecovered = vp; 1118 vp->v_mountedhere = mp; 1119 mtx_lock(&mountlist_mtx); 1120 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list); 1121 mtx_unlock(&mountlist_mtx); 1122 VOP_UNLOCK(vp, 0, td); 1123 vfs_unbusy(mp, td); 1124 vput(dvp); 1125 1126 /* Unlink the no longer needed /dev/dev -> / symlink */ 1127 kern_unlink(td, "/dev/dev", UIO_SYSSPACE); 1128 } 1129 1130 /* 1131 * Find and mount the root filesystem 1132 */ 1133 void 1134 vfs_mountroot(void) 1135 { 1136 char *cp; 1137 int error, i, asked = 0; 1138 struct mount *mp; 1139 1140 /* 1141 * Wait for GEOM to settle down 1142 */ 1143 DROP_GIANT(); 1144 g_waitidle(); 1145 PICKUP_GIANT(); 1146 1147 mp = devfs_first(); 1148 1149 /* 1150 * We are booted with instructions to prompt for the root filesystem. 1151 */ 1152 if (boothowto & RB_ASKNAME) { 1153 if (!vfs_mountroot_ask()) 1154 return; 1155 asked = 1; 1156 } 1157 1158 /* 1159 * The root filesystem information is compiled in, and we are 1160 * booted with instructions to use it. 1161 */ 1162 if (ctrootdevname != NULL && (boothowto & RB_DFLTROOT)) { 1163 if (!vfs_mountroot_try(ctrootdevname)) 1164 return; 1165 ctrootdevname = NULL; 1166 } 1167 1168 /* 1169 * We've been given the generic "use CDROM as root" flag. This is 1170 * necessary because one media may be used in many different 1171 * devices, so we need to search for them. 1172 */ 1173 if (boothowto & RB_CDROM) { 1174 for (i = 0; cdrom_rootdevnames[i] != NULL; i++) { 1175 if (!vfs_mountroot_try(cdrom_rootdevnames[i])) 1176 return; 1177 } 1178 } 1179 1180 /* 1181 * Try to use the value read by the loader from /etc/fstab, or 1182 * supplied via some other means. This is the preferred 1183 * mechanism. 1184 */ 1185 cp = getenv("vfs.root.mountfrom"); 1186 if (cp != NULL) { 1187 error = vfs_mountroot_try(cp); 1188 freeenv(cp); 1189 if (!error) 1190 return; 1191 } 1192 1193 /* 1194 * Try values that may have been computed by code during boot 1195 */ 1196 if (!vfs_mountroot_try(rootdevnames[0])) 1197 return; 1198 if (!vfs_mountroot_try(rootdevnames[1])) 1199 return; 1200 1201 /* 1202 * If we (still) have a compiled-in default, try it. 1203 */ 1204 if (ctrootdevname != NULL) 1205 if (!vfs_mountroot_try(ctrootdevname)) 1206 return; 1207 /* 1208 * Everything so far has failed, prompt on the console if we haven't 1209 * already tried that. 1210 */ 1211 if (!asked) 1212 if (!vfs_mountroot_ask()) 1213 return; 1214 1215 panic("Root mount failed, startup aborted."); 1216 } 1217 1218 /* 1219 * Mount (mountfrom) as the root filesystem. 1220 */ 1221 static int 1222 vfs_mountroot_try(const char *mountfrom) 1223 { 1224 struct mount *mp; 1225 char *vfsname, *path; 1226 int error; 1227 char patt[32]; 1228 int s; 1229 1230 vfsname = NULL; 1231 path = NULL; 1232 mp = NULL; 1233 error = EINVAL; 1234 1235 if (mountfrom == NULL) 1236 return (error); /* don't complain */ 1237 1238 s = splcam(); /* Overkill, but annoying without it */ 1239 printf("Trying to mount root from %s\n", mountfrom); 1240 splx(s); 1241 1242 /* parse vfs name and path */ 1243 vfsname = malloc(MFSNAMELEN, M_MOUNT, M_WAITOK); 1244 path = malloc(MNAMELEN, M_MOUNT, M_WAITOK); 1245 vfsname[0] = path[0] = 0; 1246 sprintf(patt, "%%%d[a-z0-9]:%%%ds", MFSNAMELEN, MNAMELEN); 1247 if (sscanf(mountfrom, patt, vfsname, path) < 1) { 1248 free(path, M_MOUNT); 1249 free(vfsname, M_MOUNT); 1250 return (error); 1251 } 1252 1253 if (path[0] == '\0') 1254 strcpy(path, ROOTNAME); 1255 1256 error = kernel_vmount( 1257 MNT_RDONLY | MNT_ROOTFS, 1258 "fstype", vfsname, 1259 "fspath", "/", 1260 "from", path, 1261 NULL); 1262 if (error == 0) { 1263 mp = TAILQ_FIRST(&mountlist); 1264 1265 /* sanity check system clock against root fs timestamp */ 1266 inittodr(mp->mnt_time); 1267 vfs_unbusy(mp, curthread); 1268 1269 devfs_fixup(curthread); 1270 } 1271 return (error); 1272 } 1273 1274 /* 1275 * --------------------------------------------------------------------- 1276 * Interactive root filesystem selection code. 1277 */ 1278 1279 static int 1280 vfs_mountroot_ask(void) 1281 { 1282 char name[128]; 1283 1284 for(;;) { 1285 printf("\nManual root filesystem specification:\n"); 1286 printf(" <fstype>:<device> Mount <device> using filesystem <fstype>\n"); 1287 #if defined(__i386__) || defined(__ia64__) 1288 printf(" eg. ufs:da0s1a\n"); 1289 #else 1290 printf(" eg. ufs:/dev/da0a\n"); 1291 #endif 1292 printf(" ? List valid disk boot devices\n"); 1293 printf(" <empty line> Abort manual input\n"); 1294 printf("\nmountroot> "); 1295 gets(name, sizeof(name), 1); 1296 if (name[0] == '\0') 1297 return (1); 1298 if (name[0] == '?') { 1299 printf("\nList of GEOM managed disk devices:\n "); 1300 g_dev_print(); 1301 continue; 1302 } 1303 if (!vfs_mountroot_try(name)) 1304 return (0); 1305 } 1306 } 1307 1308 /* 1309 * --------------------------------------------------------------------- 1310 * Functions for querying mount options/arguments from filesystems. 1311 */ 1312 1313 /* 1314 * Check that no unknown options are given 1315 */ 1316 int 1317 vfs_filteropt(struct vfsoptlist *opts, const char **legal) 1318 { 1319 struct vfsopt *opt; 1320 const char **t, *p; 1321 1322 1323 TAILQ_FOREACH(opt, opts, link) { 1324 p = opt->name; 1325 if (p[0] == 'n' && p[1] == 'o') 1326 p += 2; 1327 for(t = global_opts; *t != NULL; t++) 1328 if (!strcmp(*t, p)) 1329 break; 1330 if (*t != NULL) 1331 continue; 1332 for(t = legal; *t != NULL; t++) 1333 if (!strcmp(*t, p)) 1334 break; 1335 if (*t != NULL) 1336 continue; 1337 printf("mount option <%s> is unknown\n", p); 1338 return (EINVAL); 1339 } 1340 return (0); 1341 } 1342 1343 /* 1344 * Get a mount option by its name. 1345 * 1346 * Return 0 if the option was found, ENOENT otherwise. 1347 * If len is non-NULL it will be filled with the length 1348 * of the option. If buf is non-NULL, it will be filled 1349 * with the address of the option. 1350 */ 1351 int 1352 vfs_getopt(opts, name, buf, len) 1353 struct vfsoptlist *opts; 1354 const char *name; 1355 void **buf; 1356 int *len; 1357 { 1358 struct vfsopt *opt; 1359 1360 KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL")); 1361 1362 TAILQ_FOREACH(opt, opts, link) { 1363 if (strcmp(name, opt->name) == 0) { 1364 if (len != NULL) 1365 *len = opt->len; 1366 if (buf != NULL) 1367 *buf = opt->value; 1368 return (0); 1369 } 1370 } 1371 return (ENOENT); 1372 } 1373 1374 char * 1375 vfs_getopts(struct vfsoptlist *opts, const char *name, int *error) 1376 { 1377 struct vfsopt *opt; 1378 1379 *error = 0; 1380 TAILQ_FOREACH(opt, opts, link) { 1381 if (strcmp(name, opt->name) != 0) 1382 continue; 1383 if (((char *)opt->value)[opt->len - 1] != '\0') { 1384 *error = EINVAL; 1385 return (NULL); 1386 } 1387 return (opt->value); 1388 } 1389 return (NULL); 1390 } 1391 1392 int 1393 vfs_flagopt(struct vfsoptlist *opts, const char *name, u_int *w, u_int val) 1394 { 1395 struct vfsopt *opt; 1396 1397 TAILQ_FOREACH(opt, opts, link) { 1398 if (strcmp(name, opt->name) == 0) { 1399 if (w != NULL) 1400 *w |= val; 1401 return (1); 1402 } 1403 } 1404 if (w != NULL) 1405 *w &= ~val; 1406 return (0); 1407 } 1408 1409 int 1410 vfs_scanopt(struct vfsoptlist *opts, const char *name, const char *fmt, ...) 1411 { 1412 va_list ap; 1413 struct vfsopt *opt; 1414 int ret; 1415 1416 KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL")); 1417 1418 TAILQ_FOREACH(opt, opts, link) { 1419 if (strcmp(name, opt->name) != 0) 1420 continue; 1421 if (((char *)opt->value)[opt->len - 1] != '\0') 1422 return (0); 1423 va_start(ap, fmt); 1424 ret = vsscanf(opt->value, fmt, ap); 1425 va_end(ap); 1426 return (ret); 1427 } 1428 return (0); 1429 } 1430 1431 /* 1432 * Find and copy a mount option. 1433 * 1434 * The size of the buffer has to be specified 1435 * in len, if it is not the same length as the 1436 * mount option, EINVAL is returned. 1437 * Returns ENOENT if the option is not found. 1438 */ 1439 int 1440 vfs_copyopt(opts, name, dest, len) 1441 struct vfsoptlist *opts; 1442 const char *name; 1443 void *dest; 1444 int len; 1445 { 1446 struct vfsopt *opt; 1447 1448 KASSERT(opts != NULL, ("vfs_copyopt: caller passed 'opts' as NULL")); 1449 1450 TAILQ_FOREACH(opt, opts, link) { 1451 if (strcmp(name, opt->name) == 0) { 1452 if (len != opt->len) 1453 return (EINVAL); 1454 bcopy(opt->value, dest, opt->len); 1455 return (0); 1456 } 1457 } 1458 return (ENOENT); 1459 } 1460 1461 /* 1462 * This is a helper function for filesystems to traverse their 1463 * vnodes. See MNT_VNODE_FOREACH() in sys/mount.h 1464 */ 1465 1466 struct vnode * 1467 __mnt_vnode_next(struct vnode **nvp, struct mount *mp) 1468 { 1469 struct vnode *vp; 1470 1471 mtx_assert(&mp->mnt_mtx, MA_OWNED); 1472 1473 vp = *nvp; 1474 /* Check if we are done */ 1475 if (vp == NULL) 1476 return (NULL); 1477 /* If our next vnode is no longer ours, start over */ 1478 if (vp->v_mount != mp) 1479 vp = TAILQ_FIRST(&mp->mnt_nvnodelist); 1480 /* Save pointer to next vnode in list */ 1481 if (vp != NULL) 1482 *nvp = TAILQ_NEXT(vp, v_nmntvnodes); 1483 else 1484 *nvp = NULL; 1485 return (vp); 1486 } 1487 1488 int 1489 __vfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td) 1490 { 1491 int error; 1492 1493 error = mp->mnt_op->vfs_statfs(mp, &mp->mnt_stat, td); 1494 if (sbp != &mp->mnt_stat) 1495 *sbp = mp->mnt_stat; 1496 return (error); 1497 } 1498 1499 void 1500 vfs_mountedfrom(struct mount *mp, const char *from) 1501 { 1502 1503 bzero(mp->mnt_stat.f_mntfromname, sizeof mp->mnt_stat.f_mntfromname); 1504 strlcpy(mp->mnt_stat.f_mntfromname, from, 1505 sizeof mp->mnt_stat.f_mntfromname); 1506 } 1507 1508 /* 1509 * --------------------------------------------------------------------- 1510 * This is the api for building mount args and mounting filesystems from 1511 * inside the kernel. 1512 * 1513 * The API works by accumulation of individual args. First error is 1514 * latched. 1515 * 1516 * XXX: should be documented in new manpage kernel_mount(9) 1517 */ 1518 1519 /* A memory allocation which must be freed when we are done */ 1520 struct mntaarg { 1521 SLIST_ENTRY(mntaarg) next; 1522 }; 1523 1524 /* The header for the mount arguments */ 1525 struct mntarg { 1526 struct iovec *v; 1527 int len; 1528 int error; 1529 SLIST_HEAD(, mntaarg) list; 1530 }; 1531 1532 /* 1533 * Add a boolean argument. 1534 * 1535 * flag is the boolean value. 1536 * name must start with "no". 1537 */ 1538 struct mntarg * 1539 mount_argb(struct mntarg *ma, int flag, const char *name) 1540 { 1541 1542 KASSERT(name[0] == 'n' && name[1] == 'o', 1543 ("mount_argb(...,%s): name must start with 'no'", name)); 1544 1545 return (mount_arg(ma, name + (flag ? 2 : 0), NULL, 0)); 1546 } 1547 1548 /* 1549 * Add an argument printf style 1550 */ 1551 struct mntarg * 1552 mount_argf(struct mntarg *ma, const char *name, const char *fmt, ...) 1553 { 1554 va_list ap; 1555 struct mntaarg *maa; 1556 struct sbuf *sb; 1557 int len; 1558 1559 if (ma == NULL) { 1560 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO); 1561 SLIST_INIT(&ma->list); 1562 } 1563 if (ma->error) 1564 return (ma); 1565 1566 ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2), 1567 M_MOUNT, M_WAITOK); 1568 ma->v[ma->len].iov_base = (void *)(uintptr_t)name; 1569 ma->v[ma->len].iov_len = strlen(name) + 1; 1570 ma->len++; 1571 1572 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND); 1573 va_start(ap, fmt); 1574 sbuf_vprintf(sb, fmt, ap); 1575 va_end(ap); 1576 sbuf_finish(sb); 1577 len = sbuf_len(sb) + 1; 1578 maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO); 1579 SLIST_INSERT_HEAD(&ma->list, maa, next); 1580 bcopy(sbuf_data(sb), maa + 1, len); 1581 sbuf_delete(sb); 1582 1583 ma->v[ma->len].iov_base = maa + 1; 1584 ma->v[ma->len].iov_len = len; 1585 ma->len++; 1586 1587 return (ma); 1588 } 1589 1590 /* 1591 * Add an argument which is a userland string. 1592 */ 1593 struct mntarg * 1594 mount_argsu(struct mntarg *ma, const char *name, const void *val, int len) 1595 { 1596 struct mntaarg *maa; 1597 char *tbuf; 1598 1599 if (val == NULL) 1600 return (ma); 1601 if (ma == NULL) { 1602 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO); 1603 SLIST_INIT(&ma->list); 1604 } 1605 if (ma->error) 1606 return (ma); 1607 maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO); 1608 SLIST_INSERT_HEAD(&ma->list, maa, next); 1609 tbuf = (void *)(maa + 1); 1610 ma->error = copyinstr(val, tbuf, len, NULL); 1611 return (mount_arg(ma, name, tbuf, -1)); 1612 } 1613 1614 /* 1615 * Plain argument. 1616 * 1617 * If length is -1, use printf. 1618 */ 1619 struct mntarg * 1620 mount_arg(struct mntarg *ma, const char *name, const void *val, int len) 1621 { 1622 1623 if (ma == NULL) { 1624 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO); 1625 SLIST_INIT(&ma->list); 1626 } 1627 if (ma->error) 1628 return (ma); 1629 1630 ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2), 1631 M_MOUNT, M_WAITOK); 1632 ma->v[ma->len].iov_base = (void *)(uintptr_t)name; 1633 ma->v[ma->len].iov_len = strlen(name) + 1; 1634 ma->len++; 1635 1636 ma->v[ma->len].iov_base = (void *)(uintptr_t)val; 1637 if (len < 0) 1638 ma->v[ma->len].iov_len = strlen(val) + 1; 1639 else 1640 ma->v[ma->len].iov_len = len; 1641 ma->len++; 1642 return (ma); 1643 } 1644 1645 /* 1646 * Free a mntarg structure 1647 */ 1648 static void 1649 free_mntarg(struct mntarg *ma) 1650 { 1651 struct mntaarg *maa; 1652 1653 while (!SLIST_EMPTY(&ma->list)) { 1654 maa = SLIST_FIRST(&ma->list); 1655 SLIST_REMOVE_HEAD(&ma->list, next); 1656 free(maa, M_MOUNT); 1657 } 1658 free(ma->v, M_MOUNT); 1659 free(ma, M_MOUNT); 1660 } 1661 1662 /* 1663 * Mount a filesystem 1664 */ 1665 int 1666 kernel_mount(struct mntarg *ma, int flags) 1667 { 1668 struct uio auio; 1669 int error; 1670 1671 KASSERT(ma != NULL, ("kernel_mount NULL ma")); 1672 KASSERT(ma->v != NULL, ("kernel_mount NULL ma->v")); 1673 KASSERT(!(ma->len & 1), ("kernel_mount odd ma->len (%d)", ma->len)); 1674 1675 auio.uio_iov = ma->v; 1676 auio.uio_iovcnt = ma->len; 1677 auio.uio_segflg = UIO_SYSSPACE; 1678 1679 error = ma->error; 1680 if (!error) 1681 error = vfs_donmount(curthread, flags, &auio); 1682 free_mntarg(ma); 1683 return (error); 1684 } 1685 1686 /* 1687 * A printflike function to mount a filesystem. 1688 */ 1689 int 1690 kernel_vmount(int flags, ...) 1691 { 1692 struct mntarg *ma = NULL; 1693 va_list ap; 1694 const char *cp; 1695 const void *vp; 1696 int error; 1697 1698 va_start(ap, flags); 1699 for (;;) { 1700 cp = va_arg(ap, const char *); 1701 if (cp == NULL) 1702 break; 1703 vp = va_arg(ap, const void *); 1704 ma = mount_arg(ma, cp, vp, -1); 1705 } 1706 va_end(ap); 1707 1708 error = kernel_mount(ma, flags); 1709 return (error); 1710 } 1711