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