vfs_mount.c revision 138480
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 138480 2004-12-06 19:53:32Z 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 VFS_STATFS(mp, &mp->mnt_stat, td); 765 } 766 /* 767 * Prevent external consumers of mount options from reading 768 * mnt_optnew. 769 */ 770 mp->mnt_optnew = NULL; 771 if (mp->mnt_flag & MNT_UPDATE) { 772 if (mp->mnt_kern_flag & MNTK_WANTRDWR) 773 mp->mnt_flag &= ~MNT_RDONLY; 774 mp->mnt_flag &= 775 ~(MNT_UPDATE | MNT_RELOAD | MNT_FORCE | MNT_SNAPSHOT); 776 mp->mnt_kern_flag &= ~MNTK_WANTRDWR; 777 if (error) { 778 mp->mnt_flag = flag; 779 mp->mnt_kern_flag = kern_flag; 780 } 781 if ((mp->mnt_flag & MNT_RDONLY) == 0) { 782 if (mp->mnt_syncer == NULL) 783 error = vfs_allocate_syncvnode(mp); 784 } else { 785 if (mp->mnt_syncer != NULL) 786 vrele(mp->mnt_syncer); 787 mp->mnt_syncer = NULL; 788 } 789 vfs_unbusy(mp, td); 790 VI_LOCK(vp); 791 vp->v_iflag &= ~VI_MOUNT; 792 VI_UNLOCK(vp); 793 vrele(vp); 794 return (error); 795 } 796 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 797 /* 798 * Put the new filesystem on the mount list after root. 799 */ 800 cache_purge(vp); 801 if (!error) { 802 struct vnode *newdp; 803 804 VI_LOCK(vp); 805 vp->v_iflag &= ~VI_MOUNT; 806 VI_UNLOCK(vp); 807 vp->v_mountedhere = mp; 808 mtx_lock(&mountlist_mtx); 809 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list); 810 mtx_unlock(&mountlist_mtx); 811 vfs_event_signal(NULL, VQ_MOUNT, 0); 812 if (VFS_ROOT(mp, &newdp, td)) 813 panic("mount: lost mount"); 814 checkdirs(vp, newdp); 815 vput(newdp); 816 VOP_UNLOCK(vp, 0, td); 817 if ((mp->mnt_flag & MNT_RDONLY) == 0) 818 error = vfs_allocate_syncvnode(mp); 819 vfs_unbusy(mp, td); 820 if (error || (error = VFS_START(mp, 0, td)) != 0) 821 vrele(vp); 822 } else { 823 VI_LOCK(vp); 824 vp->v_iflag &= ~VI_MOUNT; 825 VI_UNLOCK(vp); 826 vfs_mount_destroy(mp, td); 827 vput(vp); 828 } 829 return (error); 830} 831 832/* 833 * Scan all active processes to see if any of them have a current 834 * or root directory of `olddp'. If so, replace them with the new 835 * mount point. 836 */ 837static void 838checkdirs(olddp, newdp) 839 struct vnode *olddp, *newdp; 840{ 841 struct filedesc *fdp; 842 struct proc *p; 843 int nrele; 844 845 if (vrefcnt(olddp) == 1) 846 return; 847 sx_slock(&allproc_lock); 848 LIST_FOREACH(p, &allproc, p_list) { 849 mtx_lock(&fdesc_mtx); 850 fdp = p->p_fd; 851 if (fdp == NULL) { 852 mtx_unlock(&fdesc_mtx); 853 continue; 854 } 855 nrele = 0; 856 FILEDESC_LOCK_FAST(fdp); 857 if (fdp->fd_cdir == olddp) { 858 vref(newdp); 859 fdp->fd_cdir = newdp; 860 nrele++; 861 } 862 if (fdp->fd_rdir == olddp) { 863 vref(newdp); 864 fdp->fd_rdir = newdp; 865 nrele++; 866 } 867 FILEDESC_UNLOCK_FAST(fdp); 868 mtx_unlock(&fdesc_mtx); 869 while (nrele--) 870 vrele(olddp); 871 } 872 sx_sunlock(&allproc_lock); 873 if (rootvnode == olddp) { 874 vrele(rootvnode); 875 vref(newdp); 876 rootvnode = newdp; 877 } 878} 879 880/* 881 * Unmount a filesystem. 882 * 883 * Note: unmount takes a path to the vnode mounted on as argument, 884 * not special file (as before). 885 */ 886#ifndef _SYS_SYSPROTO_H_ 887struct unmount_args { 888 char *path; 889 int flags; 890}; 891#endif 892/* ARGSUSED */ 893int 894unmount(td, uap) 895 struct thread *td; 896 register struct unmount_args /* { 897 char *path; 898 int flags; 899 } */ *uap; 900{ 901 struct mount *mp; 902 char *pathbuf; 903 int error, id0, id1; 904 905 if (jailed(td->td_ucred)) 906 return (EPERM); 907 if (usermount == 0) { 908 if ((error = suser(td)) != 0) 909 return (error); 910 } 911 912 pathbuf = malloc(MNAMELEN, M_TEMP, M_WAITOK); 913 error = copyinstr(uap->path, pathbuf, MNAMELEN, NULL); 914 if (error) { 915 free(pathbuf, M_TEMP); 916 return (error); 917 } 918 if (uap->flags & MNT_BYFSID) { 919 /* Decode the filesystem ID. */ 920 if (sscanf(pathbuf, "FSID:%d:%d", &id0, &id1) != 2) { 921 free(pathbuf, M_TEMP); 922 return (EINVAL); 923 } 924 925 mtx_lock(&mountlist_mtx); 926 TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) { 927 if (mp->mnt_stat.f_fsid.val[0] == id0 && 928 mp->mnt_stat.f_fsid.val[1] == id1) 929 break; 930 } 931 mtx_unlock(&mountlist_mtx); 932 } else { 933 mtx_lock(&mountlist_mtx); 934 TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) { 935 if (strcmp(mp->mnt_stat.f_mntonname, pathbuf) == 0) 936 break; 937 } 938 mtx_unlock(&mountlist_mtx); 939 } 940 free(pathbuf, M_TEMP); 941 if (mp == NULL) { 942 /* 943 * Previously we returned ENOENT for a nonexistent path and 944 * EINVAL for a non-mountpoint. We cannot tell these apart 945 * now, so in the !MNT_BYFSID case return the more likely 946 * EINVAL for compatibility. 947 */ 948 return ((uap->flags & MNT_BYFSID) ? ENOENT : EINVAL); 949 } 950 951 /* 952 * Only privileged root, or (if MNT_USER is set) the user that did the 953 * original mount is permitted to unmount this filesystem. 954 */ 955 error = vfs_suser(mp, td); 956 if (error) 957 return (error); 958 959 /* 960 * Don't allow unmounting the root filesystem. 961 */ 962 if (mp->mnt_flag & MNT_ROOTFS) 963 return (EINVAL); 964 mtx_lock(&Giant); 965 error = dounmount(mp, uap->flags, td); 966 mtx_unlock(&Giant); 967 return (error); 968} 969 970/* 971 * Do the actual filesystem unmount. 972 */ 973int 974dounmount(mp, flags, td) 975 struct mount *mp; 976 int flags; 977 struct thread *td; 978{ 979 struct vnode *coveredvp, *fsrootvp; 980 int error; 981 int async_flag; 982 983 mtx_assert(&Giant, MA_OWNED); 984 985 mtx_lock(&mountlist_mtx); 986 if (mp->mnt_kern_flag & MNTK_UNMOUNT) { 987 mtx_unlock(&mountlist_mtx); 988 return (EBUSY); 989 } 990 mp->mnt_kern_flag |= MNTK_UNMOUNT; 991 /* Allow filesystems to detect that a forced unmount is in progress. */ 992 if (flags & MNT_FORCE) 993 mp->mnt_kern_flag |= MNTK_UNMOUNTF; 994 error = lockmgr(&mp->mnt_lock, LK_DRAIN | LK_INTERLOCK | 995 ((flags & MNT_FORCE) ? 0 : LK_NOWAIT), &mountlist_mtx, td); 996 if (error) { 997 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF); 998 if (mp->mnt_kern_flag & MNTK_MWAIT) 999 wakeup(mp); 1000 return (error); 1001 } 1002 vn_start_write(NULL, &mp, V_WAIT); 1003 1004 if (mp->mnt_flag & MNT_EXPUBLIC) 1005 vfs_setpublicfs(NULL, NULL, NULL); 1006 1007 vfs_msync(mp, MNT_WAIT); 1008 async_flag = mp->mnt_flag & MNT_ASYNC; 1009 mp->mnt_flag &= ~MNT_ASYNC; 1010 cache_purgevfs(mp); /* remove cache entries for this file sys */ 1011 if (mp->mnt_syncer != NULL) 1012 vrele(mp->mnt_syncer); 1013 /* 1014 * For forced unmounts, move process cdir/rdir refs on the fs root 1015 * vnode to the covered vnode. For non-forced unmounts we want 1016 * such references to cause an EBUSY error. 1017 */ 1018 if ((flags & MNT_FORCE) && VFS_ROOT(mp, &fsrootvp, td) == 0) { 1019 if (mp->mnt_vnodecovered != NULL) 1020 checkdirs(fsrootvp, mp->mnt_vnodecovered); 1021 if (fsrootvp == rootvnode) { 1022 vrele(rootvnode); 1023 rootvnode = NULL; 1024 } 1025 vput(fsrootvp); 1026 } 1027 if (((mp->mnt_flag & MNT_RDONLY) || 1028 (error = VFS_SYNC(mp, MNT_WAIT, td->td_ucred, td)) == 0) || 1029 (flags & MNT_FORCE)) { 1030 error = VFS_UNMOUNT(mp, flags, td); 1031 } 1032 vn_finished_write(mp); 1033 if (error) { 1034 /* Undo cdir/rdir and rootvnode changes made above. */ 1035 if ((flags & MNT_FORCE) && VFS_ROOT(mp, &fsrootvp, td) == 0) { 1036 if (mp->mnt_vnodecovered != NULL) 1037 checkdirs(mp->mnt_vnodecovered, fsrootvp); 1038 if (rootvnode == NULL) { 1039 rootvnode = fsrootvp; 1040 vref(rootvnode); 1041 } 1042 vput(fsrootvp); 1043 } 1044 if ((mp->mnt_flag & MNT_RDONLY) == 0 && mp->mnt_syncer == NULL) 1045 (void) vfs_allocate_syncvnode(mp); 1046 mtx_lock(&mountlist_mtx); 1047 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF); 1048 mp->mnt_flag |= async_flag; 1049 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK, 1050 &mountlist_mtx, td); 1051 if (mp->mnt_kern_flag & MNTK_MWAIT) 1052 wakeup(mp); 1053 return (error); 1054 } 1055 mtx_lock(&mountlist_mtx); 1056 TAILQ_REMOVE(&mountlist, mp, mnt_list); 1057 if ((coveredvp = mp->mnt_vnodecovered) != NULL) 1058 coveredvp->v_mountedhere = NULL; 1059 mtx_unlock(&mountlist_mtx); 1060 vfs_event_signal(NULL, VQ_UNMOUNT, 0); 1061 vfs_mount_destroy(mp, td); 1062 if (coveredvp != NULL) 1063 vrele(coveredvp); 1064 return (0); 1065} 1066 1067/* 1068 * Find and mount the root filesystem 1069 */ 1070void 1071vfs_mountroot(void) 1072{ 1073 char *cp; 1074 int error, i, asked = 0; 1075 1076 1077 /* 1078 * Wait for GEOM to settle down 1079 */ 1080 DROP_GIANT(); 1081 g_waitidle(); 1082 PICKUP_GIANT(); 1083 1084 /* 1085 * We are booted with instructions to prompt for the root filesystem. 1086 */ 1087 if (boothowto & RB_ASKNAME) { 1088 if (!vfs_mountroot_ask()) 1089 return; 1090 asked = 1; 1091 } 1092 1093 /* 1094 * The root filesystem information is compiled in, and we are 1095 * booted with instructions to use it. 1096 */ 1097 if (ctrootdevname != NULL && (boothowto & RB_DFLTROOT)) { 1098 if (!vfs_mountroot_try(ctrootdevname)) 1099 return; 1100 ctrootdevname = NULL; 1101 } 1102 1103 /* 1104 * We've been given the generic "use CDROM as root" flag. This is 1105 * necessary because one media may be used in many different 1106 * devices, so we need to search for them. 1107 */ 1108 if (boothowto & RB_CDROM) { 1109 for (i = 0; cdrom_rootdevnames[i] != NULL; i++) { 1110 if (!vfs_mountroot_try(cdrom_rootdevnames[i])) 1111 return; 1112 } 1113 } 1114 1115 /* 1116 * Try to use the value read by the loader from /etc/fstab, or 1117 * supplied via some other means. This is the preferred 1118 * mechanism. 1119 */ 1120 cp = getenv("vfs.root.mountfrom"); 1121 if (cp != NULL) { 1122 error = vfs_mountroot_try(cp); 1123 freeenv(cp); 1124 if (!error) 1125 return; 1126 } 1127 1128 /* 1129 * Try values that may have been computed by code during boot 1130 */ 1131 if (!vfs_mountroot_try(rootdevnames[0])) 1132 return; 1133 if (!vfs_mountroot_try(rootdevnames[1])) 1134 return; 1135 1136 /* 1137 * If we (still) have a compiled-in default, try it. 1138 */ 1139 if (ctrootdevname != NULL) 1140 if (!vfs_mountroot_try(ctrootdevname)) 1141 return; 1142 1143 /* 1144 * Everything so far has failed, prompt on the console if we haven't 1145 * already tried that. 1146 */ 1147 if (!asked) 1148 if (!vfs_mountroot_ask()) 1149 return; 1150 panic("Root mount failed, startup aborted."); 1151} 1152 1153/* 1154 * Mount (mountfrom) as the root filesystem. 1155 */ 1156static int 1157vfs_mountroot_try(const char *mountfrom) 1158{ 1159 struct mount *mp; 1160 struct thread *td = curthread; 1161 struct vfsconf *vfsp; 1162 char *vfsname, *path; 1163 int error; 1164 char patt[32]; 1165 int s; 1166 1167 vfsname = NULL; 1168 path = NULL; 1169 mp = NULL; 1170 error = EINVAL; 1171 1172 if (mountfrom == NULL) 1173 return (error); /* don't complain */ 1174 1175 s = splcam(); /* Overkill, but annoying without it */ 1176 printf("Trying to mount root from %s\n", mountfrom); 1177 splx(s); 1178 1179 /* parse vfs name and path */ 1180 vfsname = malloc(MFSNAMELEN, M_MOUNT, M_WAITOK); 1181 path = malloc(MNAMELEN, M_MOUNT, M_WAITOK); 1182 vfsname[0] = path[0] = 0; 1183 sprintf(patt, "%%%d[a-z0-9]:%%%ds", MFSNAMELEN, MNAMELEN); 1184 if (sscanf(mountfrom, patt, vfsname, path) < 1) 1185 goto done; 1186 1187 if (path[0] == '\0') 1188 strcpy(path, ROOTNAME); 1189 1190 vfsp = vfs_byname(vfsname); 1191 if (vfsp == NULL) { 1192 printf("Can't find filesystem \"%s\"\n", vfsname); 1193 goto done; 1194 } 1195 error = vfs_mount_alloc(NULLVP, vfsp, "/", td, &mp); 1196 if (error) { 1197 printf("Could not alloc mountpoint\n"); 1198 goto done; 1199 } 1200 1201 mp->mnt_flag |= MNT_RDONLY | MNT_ROOTFS; 1202 1203 strlcpy(mp->mnt_stat.f_mntfromname, path, MNAMELEN); 1204 1205 /* 1206 * do our best to set rootdev 1207 * XXX: This does not belong here! 1208 */ 1209 if (path[0] != '\0') { 1210 struct cdev *diskdev; 1211 diskdev = getdiskbyname(path); 1212 if (diskdev != NULL) 1213 rootdev = diskdev; 1214 else 1215 printf("setrootbyname failed\n"); 1216 } 1217 1218 error = VFS_OMOUNT(mp, path, NULL, curthread); 1219 1220done: 1221 if (vfsname != NULL) 1222 free(vfsname, M_MOUNT); 1223 if (path != NULL) 1224 free(path, M_MOUNT); 1225 if (error != 0) { 1226 if (mp != NULL) 1227 vfs_mount_destroy(mp, curthread); 1228 printf("Root mount failed: %d\n", error); 1229 } else { 1230 1231 /* register with list of mounted filesystems */ 1232 mtx_lock(&mountlist_mtx); 1233 TAILQ_INSERT_HEAD(&mountlist, mp, mnt_list); 1234 mtx_unlock(&mountlist_mtx); 1235 1236 /* sanity check system clock against root fs timestamp */ 1237 inittodr(mp->mnt_time); 1238 vfs_unbusy(mp, curthread); 1239 error = VFS_START(mp, 0, curthread); 1240 } 1241 return (error); 1242} 1243 1244/* 1245 * Spin prompting on the console for a suitable root filesystem 1246 */ 1247static int 1248vfs_mountroot_ask(void) 1249{ 1250 char name[128]; 1251 1252 for(;;) { 1253 printf("\nManual root filesystem specification:\n"); 1254 printf(" <fstype>:<device> Mount <device> using filesystem <fstype>\n"); 1255#if defined(__i386__) || defined(__ia64__) 1256 printf(" eg. ufs:da0s1a\n"); 1257#else 1258 printf(" eg. ufs:/dev/da0a\n"); 1259#endif 1260 printf(" ? List valid disk boot devices\n"); 1261 printf(" <empty line> Abort manual input\n"); 1262 printf("\nmountroot> "); 1263 gets(name); 1264 if (name[0] == '\0') 1265 return (1); 1266 if (name[0] == '?') { 1267 printf("\nList of GEOM managed disk devices:\n "); 1268 g_dev_print(); 1269 continue; 1270 } 1271 if (!vfs_mountroot_try(name)) 1272 return (0); 1273 } 1274} 1275 1276/* 1277 * Local helper function for vfs_mountroot_ask. 1278 */ 1279static void 1280gets(char *cp) 1281{ 1282 char *lp; 1283 int c; 1284 1285 lp = cp; 1286 for (;;) { 1287 printf("%c", c = cngetc() & 0177); 1288 switch (c) { 1289 case -1: 1290 case '\n': 1291 case '\r': 1292 *lp++ = '\0'; 1293 return; 1294 case '\b': 1295 case '\177': 1296 if (lp > cp) { 1297 printf(" \b"); 1298 lp--; 1299 } 1300 continue; 1301 case '#': 1302 lp--; 1303 if (lp < cp) 1304 lp = cp; 1305 continue; 1306 case '@': 1307 case 'u' & 037: 1308 lp = cp; 1309 printf("%c", '\n'); 1310 continue; 1311 default: 1312 *lp++ = c; 1313 } 1314 } 1315} 1316 1317/* 1318 * Convert a given name to the cdev pointer of the device, which is probably 1319 * but not by definition, a disk. Mount a DEVFS (on nothing), look the name 1320 * up, extract the cdev from the vnode and unmount it again. Unfortunately 1321 * we cannot use the vnode directly (because we unmount the DEVFS again) 1322 * so the filesystems still have to do the bdevvp() stunt. 1323 */ 1324static struct cdev * 1325getdiskbyname(char *name) 1326{ 1327 char *cp = name; 1328 struct cdev *dev = NULL; 1329 struct thread *td = curthread; 1330 struct vfsconf *vfsp; 1331 struct mount *mp = NULL; 1332 struct vnode *vroot = NULL; 1333 struct nameidata nid; 1334 int error; 1335 1336 if (!bcmp(cp, "/dev/", 5)) 1337 cp += 5; 1338 1339 do { 1340 vfsp = vfs_byname("devfs"); 1341 if (vfsp == NULL) 1342 break; 1343 error = vfs_mount_alloc(NULLVP, vfsp, "/dev", td, &mp); 1344 if (error) 1345 break; 1346 mp->mnt_flag |= MNT_RDONLY; 1347 1348 error = VFS_MOUNT(mp, curthread); 1349 if (error) 1350 break; 1351 VFS_START(mp, 0, td); 1352 VFS_ROOT(mp, &vroot, td); 1353 VOP_UNLOCK(vroot, 0, td); 1354 1355 NDINIT(&nid, LOOKUP, NOCACHE|FOLLOW, 1356 UIO_SYSSPACE, cp, curthread); 1357 nid.ni_startdir = vroot; 1358 nid.ni_pathlen = strlen(cp); 1359 nid.ni_cnd.cn_cred = curthread->td_ucred; 1360 nid.ni_cnd.cn_nameptr = cp; 1361 1362 error = lookup(&nid); 1363 if (error) 1364 break; 1365 if (nid.ni_vp->v_type != VCHR) 1366 dev = NULL; 1367 else 1368 dev = nid.ni_vp->v_rdev; 1369 NDFREE(&nid, 0); 1370 } while (0); 1371 1372 if (vroot != NULL) 1373 VFS_UNMOUNT(mp, 0, td); 1374 if (mp != NULL) 1375 vfs_mount_destroy(mp, td); 1376 return (dev); 1377} 1378 1379/* Show the struct cdev *for a disk specified by name */ 1380#ifdef DDB 1381DB_SHOW_COMMAND(disk, db_getdiskbyname) 1382{ 1383 struct cdev *dev; 1384 1385 if (modif[0] == '\0') { 1386 db_error("usage: show disk/devicename"); 1387 return; 1388 } 1389 dev = getdiskbyname(modif); 1390 if (dev != NULL) 1391 db_printf("struct cdev *= %p\n", dev); 1392 else 1393 db_printf("No disk device matched.\n"); 1394} 1395#endif 1396 1397/* 1398 * --------------------------------------------------------------------- 1399 * Functions for querying mount options/arguments from filesystems. 1400 */ 1401 1402/* 1403 * Check that no unknown options are given 1404 */ 1405int 1406vfs_filteropt(struct vfsoptlist *opts, const char **legal) 1407{ 1408 struct vfsopt *opt; 1409 const char **t, *p; 1410 1411 1412 TAILQ_FOREACH(opt, opts, link) { 1413 p = opt->name; 1414 if (p[0] == 'n' && p[1] == 'o') 1415 p += 2; 1416 for(t = global_opts; *t != NULL; t++) 1417 if (!strcmp(*t, p)) 1418 break; 1419 if (*t != NULL) 1420 continue; 1421 for(t = legal; *t != NULL; t++) 1422 if (!strcmp(*t, p)) 1423 break; 1424 if (*t != NULL) 1425 continue; 1426 printf("mount option <%s> is unknown\n", p); 1427 return (EINVAL); 1428 } 1429 return (0); 1430} 1431 1432/* 1433 * Get a mount option by its name. 1434 * 1435 * Return 0 if the option was found, ENOENT otherwise. 1436 * If len is non-NULL it will be filled with the length 1437 * of the option. If buf is non-NULL, it will be filled 1438 * with the address of the option. 1439 */ 1440int 1441vfs_getopt(opts, name, buf, len) 1442 struct vfsoptlist *opts; 1443 const char *name; 1444 void **buf; 1445 int *len; 1446{ 1447 struct vfsopt *opt; 1448 1449 KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL")); 1450 1451 TAILQ_FOREACH(opt, opts, link) { 1452 if (strcmp(name, opt->name) == 0) { 1453 if (len != NULL) 1454 *len = opt->len; 1455 if (buf != NULL) 1456 *buf = opt->value; 1457 return (0); 1458 } 1459 } 1460 return (ENOENT); 1461} 1462 1463char * 1464vfs_getopts(struct vfsoptlist *opts, const char *name, int *error) 1465{ 1466 struct vfsopt *opt; 1467 1468 *error = 0; 1469 TAILQ_FOREACH(opt, opts, link) { 1470 if (strcmp(name, opt->name) != 0) 1471 continue; 1472 if (((char *)opt->value)[opt->len - 1] != '\0') { 1473 *error = EINVAL; 1474 return (NULL); 1475 } 1476 return (opt->value); 1477 } 1478 return (NULL); 1479} 1480 1481int 1482vfs_flagopt(struct vfsoptlist *opts, const char *name, u_int *w, u_int val) 1483{ 1484 struct vfsopt *opt; 1485 1486 TAILQ_FOREACH(opt, opts, link) { 1487 if (strcmp(name, opt->name) == 0) { 1488 if (w != NULL) 1489 *w |= val; 1490 return (1); 1491 } 1492 } 1493 if (w != NULL) 1494 *w &= ~val; 1495 return (0); 1496} 1497 1498int 1499vfs_scanopt(struct vfsoptlist *opts, const char *name, const char *fmt, ...) 1500{ 1501 va_list ap; 1502 struct vfsopt *opt; 1503 int ret; 1504 1505 KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL")); 1506 1507 TAILQ_FOREACH(opt, opts, link) { 1508 if (strcmp(name, opt->name) != 0) 1509 continue; 1510 if (((char *)opt->value)[opt->len - 1] != '\0') 1511 return (0); 1512 va_start(ap, fmt); 1513 ret = vsscanf(opt->value, fmt, ap); 1514 va_end(ap); 1515 return (ret); 1516 } 1517 return (0); 1518} 1519/* 1520 * Find and copy a mount option. 1521 * 1522 * The size of the buffer has to be specified 1523 * in len, if it is not the same length as the 1524 * mount option, EINVAL is returned. 1525 * Returns ENOENT if the option is not found. 1526 */ 1527int 1528vfs_copyopt(opts, name, dest, len) 1529 struct vfsoptlist *opts; 1530 const char *name; 1531 void *dest; 1532 int len; 1533{ 1534 struct vfsopt *opt; 1535 1536 KASSERT(opts != NULL, ("vfs_copyopt: caller passed 'opts' as NULL")); 1537 1538 TAILQ_FOREACH(opt, opts, link) { 1539 if (strcmp(name, opt->name) == 0) { 1540 if (len != opt->len) 1541 return (EINVAL); 1542 bcopy(opt->value, dest, opt->len); 1543 return (0); 1544 } 1545 } 1546 return (ENOENT); 1547} 1548 1549 1550/* 1551 * This is a helper function for filesystems to traverse their 1552 * vnodes. See MNT_VNODE_FOREACH() in sys/mount.h 1553 */ 1554 1555struct vnode * 1556__mnt_vnode_next(struct vnode **nvp, struct mount *mp) 1557{ 1558 struct vnode *vp; 1559 1560 mtx_assert(&mp->mnt_mtx, MA_OWNED); 1561 1562 vp = *nvp; 1563 /* Check if we are done */ 1564 if (vp == NULL) 1565 return (NULL); 1566 /* If our next vnode is no longer ours, start over */ 1567 if (vp->v_mount != mp) 1568 vp = TAILQ_FIRST(&mp->mnt_nvnodelist); 1569 /* Save pointer to next vnode in list */ 1570 if (vp != NULL) 1571 *nvp = TAILQ_NEXT(vp, v_nmntvnodes); 1572 else 1573 *nvp = NULL; 1574 return (vp); 1575} 1576 1577int 1578__vfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td) 1579{ 1580 int error; 1581 1582 error = mp->mnt_op->vfs_statfs(mp, &mp->mnt_stat, td); 1583 if (sbp != &mp->mnt_stat) 1584 memcpy(sbp, &mp->mnt_stat, sizeof sbp); 1585 return (error); 1586} 1587 1588void 1589vfs_mountedfrom(struct mount *mp, const char *from) 1590{ 1591 1592 bzero(mp->mnt_stat.f_mntfromname, sizeof mp->mnt_stat.f_mntfromname); 1593 strlcpy(mp->mnt_stat.f_mntfromname, from, 1594 sizeof mp->mnt_stat.f_mntfromname); 1595} 1596 1597/* 1598 * --------------------------------------------------------------------- 1599 * This is the api for building mount args and mounting filesystems from 1600 * inside the kernel. 1601 * 1602 * The API works by accumulation of individual args. First error is 1603 * latched. 1604 * 1605 * XXX: should be documented in new manpage kernel_mount(9) 1606 */ 1607 1608/* A memory allocation which must be freed when we are done */ 1609struct mntaarg { 1610 SLIST_ENTRY(mntaarg) next; 1611}; 1612 1613/* The header for the mount arguments */ 1614struct mntarg { 1615 struct iovec *v; 1616 int len; 1617 int error; 1618 SLIST_HEAD(, mntaarg) list; 1619}; 1620 1621/* 1622 * Add a boolean argument. 1623 * 1624 * flag is the boolean value. 1625 * name must start with "no". 1626 */ 1627struct mntarg * 1628mount_argb(struct mntarg *ma, int flag, const char *name) 1629{ 1630 1631 KASSERT(name[0] == 'n' && name[1] == 'o', 1632 ("mount_argb(...,%s): name must start with 'no'", name)); 1633 1634 return (mount_arg(ma, name + (flag ? 2 : 0), NULL, 0)); 1635} 1636 1637/* 1638 * Add an argument printf style 1639 */ 1640struct mntarg * 1641mount_argf(struct mntarg *ma, const char *name, const char *fmt, ...) 1642{ 1643 va_list ap; 1644 struct mntaarg *maa; 1645 struct sbuf *sb; 1646 int len; 1647 1648 if (ma == NULL) { 1649 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO); 1650 SLIST_INIT(&ma->list); 1651 } 1652 if (ma->error) 1653 return (ma); 1654 1655 ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2), 1656 M_MOUNT, M_WAITOK); 1657 ma->v[ma->len].iov_base = (void *)(uintptr_t)name; 1658 ma->v[ma->len].iov_len = strlen(name) + 1; 1659 ma->len++; 1660 1661 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND); 1662 va_start(ap, fmt); 1663 sbuf_vprintf(sb, fmt, ap); 1664 va_end(ap); 1665 sbuf_finish(sb); 1666 len = sbuf_len(sb) + 1; 1667 maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO); 1668 SLIST_INSERT_HEAD(&ma->list, maa, next); 1669 bcopy(sbuf_data(sb), maa + 1, len); 1670 sbuf_delete(sb); 1671 1672 ma->v[ma->len].iov_base = maa + 1; 1673 ma->v[ma->len].iov_len = len; 1674 ma->len++; 1675 1676 return (ma); 1677} 1678 1679/* 1680 * Add an argument which is a userland string. 1681 */ 1682struct mntarg * 1683mount_argsu(struct mntarg *ma, const char *name, const void *val, int len) 1684{ 1685 struct mntaarg *maa; 1686 char *tbuf; 1687 1688 if (val == NULL) 1689 return (ma); 1690 if (ma == NULL) { 1691 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO); 1692 SLIST_INIT(&ma->list); 1693 } 1694 if (ma->error) 1695 return (ma); 1696 maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO); 1697 SLIST_INSERT_HEAD(&ma->list, maa, next); 1698 tbuf = (void *)(maa + 1); 1699 ma->error = copyinstr(val, tbuf, len, NULL); 1700 return (mount_arg(ma, name, tbuf, -1)); 1701} 1702 1703/* 1704 * Plain argument. 1705 * 1706 * If length is -1, use printf. 1707 */ 1708struct mntarg * 1709mount_arg(struct mntarg *ma, const char *name, const void *val, int len) 1710{ 1711 1712 if (ma == NULL) { 1713 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO); 1714 SLIST_INIT(&ma->list); 1715 } 1716 if (ma->error) 1717 return (ma); 1718 1719 ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2), 1720 M_MOUNT, M_WAITOK); 1721 ma->v[ma->len].iov_base = (void *)(uintptr_t)name; 1722 ma->v[ma->len].iov_len = strlen(name) + 1; 1723 ma->len++; 1724 1725 ma->v[ma->len].iov_base = (void *)(uintptr_t)val; 1726 if (len < 0) 1727 ma->v[ma->len].iov_len = strlen(val) + 1; 1728 else 1729 ma->v[ma->len].iov_len = len; 1730 ma->len++; 1731 return (ma); 1732} 1733 1734/* 1735 * Free a mntarg structure 1736 */ 1737void 1738free_mntarg(struct mntarg *ma) 1739{ 1740 struct mntaarg *maa; 1741 1742 while (!SLIST_EMPTY(&ma->list)) { 1743 maa = SLIST_FIRST(&ma->list); 1744 SLIST_REMOVE_HEAD(&ma->list, next); 1745 free(maa, M_MOUNT); 1746 } 1747 free(ma->v, M_MOUNT); 1748 free(ma, M_MOUNT); 1749} 1750 1751/* 1752 * Mount a filesystem 1753 */ 1754int 1755kernel_mount(struct mntarg *ma, int flags) 1756{ 1757 struct uio auio; 1758 int error; 1759 1760 KASSERT(ma != NULL, ("kernel_mount NULL ma")); 1761 KASSERT(ma->v != NULL, ("kernel_mount NULL ma->v")); 1762 KASSERT(!(ma->len & 1), ("kernel_mount odd ma->len (%d)", ma->len)); 1763 1764 auio.uio_iov = ma->v; 1765 auio.uio_iovcnt = ma->len; 1766 auio.uio_segflg = UIO_SYSSPACE; 1767 1768 error = ma->error; 1769 if (!error) 1770 error = vfs_donmount(curthread, flags, &auio); 1771 free_mntarg(ma); 1772 return (error); 1773} 1774 1775/* 1776 * A printflike function to mount a filesystem. 1777 */ 1778int 1779kernel_vmount(int flags, ...) 1780{ 1781 struct mntarg *ma = NULL; 1782 va_list ap; 1783 const char *cp; 1784 const void *vp; 1785 int error; 1786 1787 va_start(ap, flags); 1788 for (;;) { 1789 cp = va_arg(ap, const char *); 1790 if (cp == NULL) 1791 break; 1792 vp = va_arg(ap, const void *); 1793 ma = mount_arg(ma, cp, vp, -1); 1794 } 1795 va_end(ap); 1796 1797 error = kernel_mount(ma, flags); 1798 return (error); 1799} 1800