1 /*- 2 * Copyright (c) 1999-2004 Poul-Henning Kamp 3 * Copyright (c) 1999 Michael Smith 4 * Copyright (c) 1989, 1993 5 * The Regents of the University of California. All rights reserved. 6 * (c) UNIX System Laboratories, Inc. 7 * All or some portions of this file are derived from material licensed 8 * to the University of California by American Telephone and Telegraph 9 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 10 * the permission of UNIX System Laboratories, Inc. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 15 * 1. Redistributions of source code must retain the above copyright 16 * notice, this list of conditions and the following disclaimer. 17 * 2. Redistributions in binary form must reproduce the above copyright 18 * notice, this list of conditions and the following disclaimer in the 19 * documentation and/or other materials provided with the distribution. 20 * 4. Neither the name of the University nor the names of its contributors 21 * may be used to endorse or promote products derived from this software 22 * without specific prior written permission. 23 * 24 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 34 * SUCH DAMAGE. 35 */ 36 37 #include <sys/cdefs.h> 38 __FBSDID("$FreeBSD$"); 39 40 #include <sys/param.h> 41 #include <sys/conf.h> 42 #include <sys/clock.h> 43 #include <sys/jail.h> 44 #include <sys/kernel.h> 45 #include <sys/libkern.h> 46 #include <sys/malloc.h> 47 #include <sys/mount.h> 48 #include <sys/mutex.h> 49 #include <sys/namei.h> 50 #include <sys/priv.h> 51 #include <sys/proc.h> 52 #include <sys/filedesc.h> 53 #include <sys/reboot.h> 54 #include <sys/syscallsubr.h> 55 #include <sys/sysproto.h> 56 #include <sys/sx.h> 57 #include <sys/sysctl.h> 58 #include <sys/sysent.h> 59 #include <sys/systm.h> 60 #include <sys/vnode.h> 61 #include <vm/uma.h> 62 63 #include <geom/geom.h> 64 65 #include <machine/stdarg.h> 66 67 #include <security/audit/audit.h> 68 #include <security/mac/mac_framework.h> 69 70 #include "opt_rootdevname.h" 71 #include "opt_ddb.h" 72 #include "opt_mac.h" 73 74 #ifdef DDB 75 #include <ddb/ddb.h> 76 #endif 77 78 #define ROOTNAME "root_device" 79 #define VFS_MOUNTARG_SIZE_MAX (1024 * 64) 80 81 static int vfs_domount(struct thread *td, const char *fstype, 82 char *fspath, int fsflags, void *fsdata); 83 static struct mount *vfs_mount_alloc(struct vnode *dvp, struct vfsconf *vfsp, 84 const char *fspath, struct thread *td); 85 static int vfs_mountroot_ask(void); 86 static int vfs_mountroot_try(const char *mountfrom); 87 static int vfs_donmount(struct thread *td, int fsflags, 88 struct uio *fsoptions); 89 static void free_mntarg(struct mntarg *ma); 90 static void vfs_mount_destroy(struct mount *); 91 static int vfs_getopt_pos(struct vfsoptlist *opts, const char *name); 92 93 static int usermount = 0; 94 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0, 95 "Unprivileged users may mount and unmount file systems"); 96 97 MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount structure"); 98 MALLOC_DEFINE(M_VNODE_MARKER, "vnodemarker", "vnode marker"); 99 static uma_zone_t mount_zone; 100 101 /* List of mounted filesystems. */ 102 struct mntlist mountlist = TAILQ_HEAD_INITIALIZER(mountlist); 103 104 /* For any iteration/modification of mountlist */ 105 struct mtx mountlist_mtx; 106 MTX_SYSINIT(mountlist, &mountlist_mtx, "mountlist", MTX_DEF); 107 108 TAILQ_HEAD(vfsoptlist, vfsopt); 109 struct vfsopt { 110 TAILQ_ENTRY(vfsopt) link; 111 char *name; 112 void *value; 113 int len; 114 }; 115 116 /* 117 * The vnode of the system's root (/ in the filesystem, without chroot 118 * active.) 119 */ 120 struct vnode *rootvnode; 121 122 /* 123 * The root filesystem is detailed in the kernel environment variable 124 * vfs.root.mountfrom, which is expected to be in the general format 125 * 126 * <vfsname>:[<path>] 127 * vfsname := the name of a VFS known to the kernel and capable 128 * of being mounted as root 129 * path := disk device name or other data used by the filesystem 130 * to locate its physical store 131 */ 132 133 /* 134 * Global opts, taken by all filesystems 135 */ 136 static const char *global_opts[] = { 137 "errmsg", 138 "fstype", 139 "fspath", 140 "rdonly", 141 "ro", 142 "rw", 143 "suid", 144 "exec", 145 "update", 146 NULL 147 }; 148 149 /* 150 * The root specifiers we will try if RB_CDROM is specified. 151 */ 152 static char *cdrom_rootdevnames[] = { 153 "cd9660:cd0", 154 "cd9660:acd0", 155 NULL 156 }; 157 158 /* legacy find-root code */ 159 char *rootdevnames[2] = {NULL, NULL}; 160 #ifndef ROOTDEVNAME 161 # define ROOTDEVNAME NULL 162 #endif 163 static const char *ctrootdevname = ROOTDEVNAME; 164 165 /* 166 * --------------------------------------------------------------------- 167 * Functions for building and sanitizing the mount options 168 */ 169 170 /* Remove one mount option. */ 171 static void 172 vfs_freeopt(struct vfsoptlist *opts, struct vfsopt *opt) 173 { 174 175 TAILQ_REMOVE(opts, opt, link); 176 free(opt->name, M_MOUNT); 177 if (opt->value != NULL) 178 free(opt->value, M_MOUNT); 179 #ifdef INVARIANTS 180 else if (opt->len != 0) 181 panic("%s: mount option with NULL value but length != 0", 182 __func__); 183 #endif 184 free(opt, M_MOUNT); 185 } 186 187 /* Release all resources related to the mount options. */ 188 static void 189 vfs_freeopts(struct vfsoptlist *opts) 190 { 191 struct vfsopt *opt; 192 193 while (!TAILQ_EMPTY(opts)) { 194 opt = TAILQ_FIRST(opts); 195 vfs_freeopt(opts, opt); 196 } 197 free(opts, M_MOUNT); 198 } 199 200 void 201 vfs_deleteopt(struct vfsoptlist *opts, const char *name) 202 { 203 struct vfsopt *opt, *temp; 204 205 TAILQ_FOREACH_SAFE(opt, opts, link, temp) { 206 if (strcmp(opt->name, name) == 0) 207 vfs_freeopt(opts, opt); 208 } 209 } 210 211 /* 212 * Check if options are equal (with or without the "no" prefix). 213 */ 214 static int 215 vfs_equalopts(const char *opt1, const char *opt2) 216 { 217 218 /* "opt" vs. "opt" or "noopt" vs. "noopt" */ 219 if (strcmp(opt1, opt2) == 0) 220 return (1); 221 /* "noopt" vs. "opt" */ 222 if (strncmp(opt1, "no", 2) == 0 && strcmp(opt1 + 2, opt2) == 0) 223 return (1); 224 /* "opt" vs. "noopt" */ 225 if (strncmp(opt2, "no", 2) == 0 && strcmp(opt1, opt2 + 2) == 0) 226 return (1); 227 return (0); 228 } 229 230 /* 231 * If a mount option is specified several times, 232 * (with or without the "no" prefix) only keep 233 * the last occurence of it. 234 */ 235 static void 236 vfs_sanitizeopts(struct vfsoptlist *opts) 237 { 238 struct vfsopt *opt, *opt2, *tmp; 239 240 TAILQ_FOREACH_REVERSE(opt, opts, vfsoptlist, link) { 241 opt2 = TAILQ_PREV(opt, vfsoptlist, link); 242 while (opt2 != NULL) { 243 if (vfs_equalopts(opt->name, opt2->name)) { 244 tmp = TAILQ_PREV(opt2, vfsoptlist, link); 245 vfs_freeopt(opts, opt2); 246 opt2 = tmp; 247 } else { 248 opt2 = TAILQ_PREV(opt2, vfsoptlist, link); 249 } 250 } 251 } 252 } 253 254 /* 255 * Build a linked list of mount options from a struct uio. 256 */ 257 static int 258 vfs_buildopts(struct uio *auio, struct vfsoptlist **options) 259 { 260 struct vfsoptlist *opts; 261 struct vfsopt *opt; 262 size_t memused; 263 unsigned int i, iovcnt; 264 int error, namelen, optlen; 265 266 opts = malloc(sizeof(struct vfsoptlist), M_MOUNT, M_WAITOK); 267 TAILQ_INIT(opts); 268 memused = 0; 269 iovcnt = auio->uio_iovcnt; 270 for (i = 0; i < iovcnt; i += 2) { 271 opt = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK); 272 namelen = auio->uio_iov[i].iov_len; 273 optlen = auio->uio_iov[i + 1].iov_len; 274 opt->name = malloc(namelen, M_MOUNT, M_WAITOK); 275 opt->value = NULL; 276 opt->len = 0; 277 278 /* 279 * Do this early, so jumps to "bad" will free the current 280 * option. 281 */ 282 TAILQ_INSERT_TAIL(opts, opt, link); 283 memused += sizeof(struct vfsopt) + optlen + namelen; 284 285 /* 286 * Avoid consuming too much memory, and attempts to overflow 287 * memused. 288 */ 289 if (memused > VFS_MOUNTARG_SIZE_MAX || 290 optlen > VFS_MOUNTARG_SIZE_MAX || 291 namelen > VFS_MOUNTARG_SIZE_MAX) { 292 error = EINVAL; 293 goto bad; 294 } 295 296 if (auio->uio_segflg == UIO_SYSSPACE) { 297 bcopy(auio->uio_iov[i].iov_base, opt->name, namelen); 298 } else { 299 error = copyin(auio->uio_iov[i].iov_base, opt->name, 300 namelen); 301 if (error) 302 goto bad; 303 } 304 /* Ensure names are null-terminated strings. */ 305 if (opt->name[namelen - 1] != '\0') { 306 error = EINVAL; 307 goto bad; 308 } 309 if (optlen != 0) { 310 opt->len = optlen; 311 opt->value = malloc(optlen, M_MOUNT, M_WAITOK); 312 if (auio->uio_segflg == UIO_SYSSPACE) { 313 bcopy(auio->uio_iov[i + 1].iov_base, opt->value, 314 optlen); 315 } else { 316 error = copyin(auio->uio_iov[i + 1].iov_base, 317 opt->value, optlen); 318 if (error) 319 goto bad; 320 } 321 } 322 } 323 vfs_sanitizeopts(opts); 324 *options = opts; 325 return (0); 326 bad: 327 vfs_freeopts(opts); 328 return (error); 329 } 330 331 /* 332 * Merge the old mount options with the new ones passed 333 * in the MNT_UPDATE case. 334 */ 335 static void 336 vfs_mergeopts(struct vfsoptlist *toopts, struct vfsoptlist *opts) 337 { 338 struct vfsopt *opt, *opt2, *new; 339 340 TAILQ_FOREACH(opt, opts, link) { 341 /* 342 * Check that this option hasn't been redefined 343 * nor cancelled with a "no" mount option. 344 */ 345 opt2 = TAILQ_FIRST(toopts); 346 while (opt2 != NULL) { 347 if (strcmp(opt2->name, opt->name) == 0) 348 goto next; 349 if (strncmp(opt2->name, "no", 2) == 0 && 350 strcmp(opt2->name + 2, opt->name) == 0) { 351 vfs_freeopt(toopts, opt2); 352 goto next; 353 } 354 opt2 = TAILQ_NEXT(opt2, link); 355 } 356 /* We want this option, duplicate it. */ 357 new = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK); 358 new->name = malloc(strlen(opt->name) + 1, M_MOUNT, M_WAITOK); 359 strcpy(new->name, opt->name); 360 if (opt->len != 0) { 361 new->value = malloc(opt->len, M_MOUNT, M_WAITOK); 362 bcopy(opt->value, new->value, opt->len); 363 } else { 364 new->value = NULL; 365 } 366 new->len = opt->len; 367 TAILQ_INSERT_TAIL(toopts, new, link); 368 next: 369 continue; 370 } 371 } 372 373 /* 374 * Mount a filesystem. 375 */ 376 int 377 nmount(td, uap) 378 struct thread *td; 379 struct nmount_args /* { 380 struct iovec *iovp; 381 unsigned int iovcnt; 382 int flags; 383 } */ *uap; 384 { 385 struct uio *auio; 386 struct iovec *iov; 387 unsigned int i; 388 int error; 389 u_int iovcnt; 390 391 AUDIT_ARG(fflags, uap->flags); 392 393 /* Kick out MNT_ROOTFS early as it is legal internally */ 394 if (uap->flags & MNT_ROOTFS) 395 return (EINVAL); 396 397 iovcnt = uap->iovcnt; 398 /* 399 * Check that we have an even number of iovec's 400 * and that we have at least two options. 401 */ 402 if ((iovcnt & 1) || (iovcnt < 4)) 403 return (EINVAL); 404 405 error = copyinuio(uap->iovp, iovcnt, &auio); 406 if (error) 407 return (error); 408 iov = auio->uio_iov; 409 for (i = 0; i < iovcnt; i++) { 410 if (iov->iov_len > MMAXOPTIONLEN) { 411 free(auio, M_IOV); 412 return (EINVAL); 413 } 414 iov++; 415 } 416 error = vfs_donmount(td, uap->flags, auio); 417 418 free(auio, M_IOV); 419 return (error); 420 } 421 422 /* 423 * --------------------------------------------------------------------- 424 * Various utility functions 425 */ 426 427 void 428 vfs_ref(struct mount *mp) 429 { 430 431 MNT_ILOCK(mp); 432 MNT_REF(mp); 433 MNT_IUNLOCK(mp); 434 } 435 436 void 437 vfs_rel(struct mount *mp) 438 { 439 440 MNT_ILOCK(mp); 441 MNT_REL(mp); 442 MNT_IUNLOCK(mp); 443 } 444 445 static int 446 mount_init(void *mem, int size, int flags) 447 { 448 struct mount *mp; 449 450 mp = (struct mount *)mem; 451 mtx_init(&mp->mnt_mtx, "struct mount mtx", NULL, MTX_DEF); 452 lockinit(&mp->mnt_lock, PVFS, "vfslock", 0, 0); 453 return (0); 454 } 455 456 static void 457 mount_fini(void *mem, int size) 458 { 459 struct mount *mp; 460 461 mp = (struct mount *)mem; 462 lockdestroy(&mp->mnt_lock); 463 mtx_destroy(&mp->mnt_mtx); 464 } 465 466 /* 467 * Allocate and initialize the mount point struct. 468 */ 469 static struct mount * 470 vfs_mount_alloc(struct vnode *vp, struct vfsconf *vfsp, 471 const char *fspath, struct thread *td) 472 { 473 struct mount *mp; 474 475 mp = uma_zalloc(mount_zone, M_WAITOK); 476 bzero(&mp->mnt_startzero, 477 __rangeof(struct mount, mnt_startzero, mnt_endzero)); 478 TAILQ_INIT(&mp->mnt_nvnodelist); 479 mp->mnt_nvnodelistsize = 0; 480 mp->mnt_ref = 0; 481 (void) vfs_busy(mp, LK_NOWAIT, 0, td); 482 mp->mnt_op = vfsp->vfc_vfsops; 483 mp->mnt_vfc = vfsp; 484 vfsp->vfc_refcount++; /* XXX Unlocked */ 485 mp->mnt_stat.f_type = vfsp->vfc_typenum; 486 MNT_ILOCK(mp); 487 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK; 488 MNT_IUNLOCK(mp); 489 mp->mnt_gen++; 490 strlcpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN); 491 mp->mnt_vnodecovered = vp; 492 mp->mnt_cred = crdup(td->td_ucred); 493 mp->mnt_stat.f_owner = td->td_ucred->cr_uid; 494 strlcpy(mp->mnt_stat.f_mntonname, fspath, MNAMELEN); 495 mp->mnt_iosize_max = DFLTPHYS; 496 #ifdef MAC 497 mac_init_mount(mp); 498 mac_create_mount(td->td_ucred, mp); 499 #endif 500 arc4rand(&mp->mnt_hashseed, sizeof mp->mnt_hashseed, 0); 501 return (mp); 502 } 503 504 /* 505 * Destroy the mount struct previously allocated by vfs_mount_alloc(). 506 */ 507 static void 508 vfs_mount_destroy(struct mount *mp) 509 { 510 int i; 511 512 MNT_ILOCK(mp); 513 for (i = 0; mp->mnt_ref && i < 3; i++) 514 msleep(mp, MNT_MTX(mp), PVFS, "mntref", hz); 515 /* 516 * This will always cause a 3 second delay in rebooting due to 517 * refs on the root mountpoint that never go away. Most of these 518 * are held by init which never exits. 519 */ 520 if (i == 3 && (!rebooting || bootverbose)) 521 printf("Mount point %s had %d dangling refs\n", 522 mp->mnt_stat.f_mntonname, mp->mnt_ref); 523 if (mp->mnt_holdcnt != 0) { 524 printf("Waiting for mount point to be unheld\n"); 525 while (mp->mnt_holdcnt != 0) { 526 mp->mnt_holdcntwaiters++; 527 msleep(&mp->mnt_holdcnt, MNT_MTX(mp), 528 PZERO, "mntdestroy", 0); 529 mp->mnt_holdcntwaiters--; 530 } 531 printf("mount point unheld\n"); 532 } 533 if (mp->mnt_writeopcount > 0) { 534 printf("Waiting for mount point write ops\n"); 535 while (mp->mnt_writeopcount > 0) { 536 mp->mnt_kern_flag |= MNTK_SUSPEND; 537 msleep(&mp->mnt_writeopcount, 538 MNT_MTX(mp), 539 PZERO, "mntdestroy2", 0); 540 } 541 printf("mount point write ops completed\n"); 542 } 543 if (mp->mnt_secondary_writes > 0) { 544 printf("Waiting for mount point secondary write ops\n"); 545 while (mp->mnt_secondary_writes > 0) { 546 mp->mnt_kern_flag |= MNTK_SUSPEND; 547 msleep(&mp->mnt_secondary_writes, 548 MNT_MTX(mp), 549 PZERO, "mntdestroy3", 0); 550 } 551 printf("mount point secondary write ops completed\n"); 552 } 553 MNT_IUNLOCK(mp); 554 mp->mnt_vfc->vfc_refcount--; 555 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist)) { 556 struct vnode *vp; 557 558 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) 559 vprint("", vp); 560 panic("unmount: dangling vnode"); 561 } 562 MNT_ILOCK(mp); 563 if (mp->mnt_kern_flag & MNTK_MWAIT) 564 wakeup(mp); 565 if (mp->mnt_writeopcount != 0) 566 panic("vfs_mount_destroy: nonzero writeopcount"); 567 if (mp->mnt_secondary_writes != 0) 568 panic("vfs_mount_destroy: nonzero secondary_writes"); 569 if (mp->mnt_nvnodelistsize != 0) 570 panic("vfs_mount_destroy: nonzero nvnodelistsize"); 571 mp->mnt_writeopcount = -1000; 572 mp->mnt_nvnodelistsize = -1000; 573 mp->mnt_secondary_writes = -1000; 574 MNT_IUNLOCK(mp); 575 #ifdef MAC 576 mac_destroy_mount(mp); 577 #endif 578 if (mp->mnt_opt != NULL) 579 vfs_freeopts(mp->mnt_opt); 580 crfree(mp->mnt_cred); 581 uma_zfree(mount_zone, mp); 582 } 583 584 static int 585 vfs_donmount(struct thread *td, int fsflags, struct uio *fsoptions) 586 { 587 struct vfsoptlist *optlist; 588 struct vfsopt *opt, *noro_opt; 589 char *fstype, *fspath, *errmsg; 590 int error, fstypelen, fspathlen, errmsg_len, errmsg_pos; 591 int has_rw, has_noro; 592 593 errmsg = NULL; 594 errmsg_len = 0; 595 errmsg_pos = -1; 596 has_rw = 0; 597 has_noro = 0; 598 599 error = vfs_buildopts(fsoptions, &optlist); 600 if (error) 601 return (error); 602 603 if (vfs_getopt(optlist, "errmsg", (void **)&errmsg, &errmsg_len) == 0) 604 errmsg_pos = vfs_getopt_pos(optlist, "errmsg"); 605 606 /* 607 * We need these two options before the others, 608 * and they are mandatory for any filesystem. 609 * Ensure they are NUL terminated as well. 610 */ 611 fstypelen = 0; 612 error = vfs_getopt(optlist, "fstype", (void **)&fstype, &fstypelen); 613 if (error || fstype[fstypelen - 1] != '\0') { 614 error = EINVAL; 615 if (errmsg != NULL) 616 strncpy(errmsg, "Invalid fstype", errmsg_len); 617 goto bail; 618 } 619 fspathlen = 0; 620 error = vfs_getopt(optlist, "fspath", (void **)&fspath, &fspathlen); 621 if (error || fspath[fspathlen - 1] != '\0') { 622 error = EINVAL; 623 if (errmsg != NULL) 624 strncpy(errmsg, "Invalid fspath", errmsg_len); 625 goto bail; 626 } 627 628 /* 629 * We need to see if we have the "update" option 630 * before we call vfs_domount(), since vfs_domount() has special 631 * logic based on MNT_UPDATE. This is very important 632 * when we want to update the root filesystem. 633 */ 634 TAILQ_FOREACH(opt, optlist, link) { 635 if (strcmp(opt->name, "update") == 0) 636 fsflags |= MNT_UPDATE; 637 else if (strcmp(opt->name, "async") == 0) 638 fsflags |= MNT_ASYNC; 639 else if (strcmp(opt->name, "force") == 0) 640 fsflags |= MNT_FORCE; 641 else if (strcmp(opt->name, "multilabel") == 0) 642 fsflags |= MNT_MULTILABEL; 643 else if (strcmp(opt->name, "noasync") == 0) 644 fsflags &= ~MNT_ASYNC; 645 else if (strcmp(opt->name, "noatime") == 0) 646 fsflags |= MNT_NOATIME; 647 else if (strcmp(opt->name, "noclusterr") == 0) 648 fsflags |= MNT_NOCLUSTERR; 649 else if (strcmp(opt->name, "noclusterw") == 0) 650 fsflags |= MNT_NOCLUSTERW; 651 else if (strcmp(opt->name, "noexec") == 0) 652 fsflags |= MNT_NOEXEC; 653 else if (strcmp(opt->name, "nosuid") == 0) 654 fsflags |= MNT_NOSUID; 655 else if (strcmp(opt->name, "nosymfollow") == 0) 656 fsflags |= MNT_NOSYMFOLLOW; 657 else if (strcmp(opt->name, "noro") == 0) { 658 fsflags &= ~MNT_RDONLY; 659 has_noro = 1; 660 } 661 else if (strcmp(opt->name, "rw") == 0) { 662 fsflags &= ~MNT_RDONLY; 663 has_rw = 1; 664 } 665 else if (strcmp(opt->name, "ro") == 0 || 666 strcmp(opt->name, "rdonly") == 0) 667 fsflags |= MNT_RDONLY; 668 else if (strcmp(opt->name, "snapshot") == 0) 669 fsflags |= MNT_SNAPSHOT; 670 else if (strcmp(opt->name, "suiddir") == 0) 671 fsflags |= MNT_SUIDDIR; 672 else if (strcmp(opt->name, "sync") == 0) 673 fsflags |= MNT_SYNCHRONOUS; 674 else if (strcmp(opt->name, "union") == 0) 675 fsflags |= MNT_UNION; 676 } 677 678 /* 679 * If "rw" was specified as a mount option, and we 680 * are trying to update a mount-point from "ro" to "rw", 681 * we need a mount option "noro", since in vfs_mergeopts(), 682 * "noro" will cancel "ro", but "rw" will not do anything. 683 */ 684 if (has_rw && !has_noro) { 685 noro_opt = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK); 686 noro_opt->name = strdup("noro", M_MOUNT); 687 noro_opt->value = NULL; 688 noro_opt->len = 0; 689 TAILQ_INSERT_TAIL(optlist, noro_opt, link); 690 } 691 692 /* 693 * Be ultra-paranoid about making sure the type and fspath 694 * variables will fit in our mp buffers, including the 695 * terminating NUL. 696 */ 697 if (fstypelen >= MFSNAMELEN - 1 || fspathlen >= MNAMELEN - 1) { 698 error = ENAMETOOLONG; 699 goto bail; 700 } 701 702 mtx_lock(&Giant); 703 error = vfs_domount(td, fstype, fspath, fsflags, optlist); 704 mtx_unlock(&Giant); 705 bail: 706 /* copyout the errmsg */ 707 if (errmsg_pos != -1 && ((2 * errmsg_pos + 1) < fsoptions->uio_iovcnt) 708 && errmsg_len > 0 && errmsg != NULL) { 709 if (fsoptions->uio_segflg == UIO_SYSSPACE) { 710 bcopy(errmsg, 711 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_base, 712 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_len); 713 } else { 714 copyout(errmsg, 715 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_base, 716 fsoptions->uio_iov[2 * errmsg_pos + 1].iov_len); 717 } 718 } 719 720 if (error != 0) 721 vfs_freeopts(optlist); 722 return (error); 723 } 724 725 /* 726 * Old mount API. 727 */ 728 #ifndef _SYS_SYSPROTO_H_ 729 struct mount_args { 730 char *type; 731 char *path; 732 int flags; 733 caddr_t data; 734 }; 735 #endif 736 /* ARGSUSED */ 737 int 738 mount(td, uap) 739 struct thread *td; 740 struct mount_args /* { 741 char *type; 742 char *path; 743 int flags; 744 caddr_t data; 745 } */ *uap; 746 { 747 char *fstype; 748 struct vfsconf *vfsp = NULL; 749 struct mntarg *ma = NULL; 750 int error; 751 752 AUDIT_ARG(fflags, uap->flags); 753 754 /* Kick out MNT_ROOTFS early as it is legal internally */ 755 uap->flags &= ~MNT_ROOTFS; 756 757 fstype = malloc(MFSNAMELEN, M_TEMP, M_WAITOK); 758 error = copyinstr(uap->type, fstype, MFSNAMELEN, NULL); 759 if (error) { 760 free(fstype, M_TEMP); 761 return (error); 762 } 763 764 AUDIT_ARG(text, fstype); 765 mtx_lock(&Giant); 766 vfsp = vfs_byname_kld(fstype, td, &error); 767 free(fstype, M_TEMP); 768 if (vfsp == NULL) { 769 mtx_unlock(&Giant); 770 return (ENOENT); 771 } 772 if (vfsp->vfc_vfsops->vfs_cmount == NULL) { 773 mtx_unlock(&Giant); 774 return (EOPNOTSUPP); 775 } 776 777 ma = mount_argsu(ma, "fstype", uap->type, MNAMELEN); 778 ma = mount_argsu(ma, "fspath", uap->path, MNAMELEN); 779 ma = mount_argb(ma, uap->flags & MNT_RDONLY, "noro"); 780 ma = mount_argb(ma, !(uap->flags & MNT_NOSUID), "nosuid"); 781 ma = mount_argb(ma, !(uap->flags & MNT_NOEXEC), "noexec"); 782 783 error = vfsp->vfc_vfsops->vfs_cmount(ma, uap->data, uap->flags, td); 784 mtx_unlock(&Giant); 785 return (error); 786 } 787 788 789 /* 790 * vfs_domount(): actually attempt a filesystem mount. 791 */ 792 static int 793 vfs_domount( 794 struct thread *td, /* Calling thread. */ 795 const char *fstype, /* Filesystem type. */ 796 char *fspath, /* Mount path. */ 797 int fsflags, /* Flags common to all filesystems. */ 798 void *fsdata /* Options local to the filesystem. */ 799 ) 800 { 801 struct vnode *vp; 802 struct mount *mp; 803 struct vfsconf *vfsp; 804 struct export_args export; 805 int error, flag = 0; 806 struct vattr va; 807 struct nameidata nd; 808 809 mtx_assert(&Giant, MA_OWNED); 810 /* 811 * Be ultra-paranoid about making sure the type and fspath 812 * variables will fit in our mp buffers, including the 813 * terminating NUL. 814 */ 815 if (strlen(fstype) >= MFSNAMELEN || strlen(fspath) >= MNAMELEN) 816 return (ENAMETOOLONG); 817 818 if (jailed(td->td_ucred)) 819 return (EPERM); 820 if (usermount == 0) { 821 if ((error = priv_check(td, PRIV_VFS_MOUNT)) != 0) 822 return (error); 823 } 824 825 /* 826 * Do not allow NFS export or MNT_SUIDDIR by unprivileged users. 827 */ 828 if (fsflags & MNT_EXPORTED) { 829 error = priv_check(td, PRIV_VFS_MOUNT_EXPORTED); 830 if (error) 831 return (error); 832 } 833 if (fsflags & MNT_SUIDDIR) { 834 error = priv_check(td, PRIV_VFS_MOUNT_SUIDDIR); 835 if (error) 836 return (error); 837 838 } 839 /* 840 * Silently enforce MNT_NOSUID and MNT_USER for unprivileged users. 841 */ 842 if ((fsflags & (MNT_NOSUID | MNT_USER)) != (MNT_NOSUID | MNT_USER)) { 843 if (priv_check(td, PRIV_VFS_MOUNT_NONUSER) != 0) 844 fsflags |= MNT_NOSUID | MNT_USER; 845 } 846 847 /* Load KLDs before we lock the covered vnode to avoid reversals. */ 848 vfsp = NULL; 849 if ((fsflags & MNT_UPDATE) == 0) { 850 /* Don't try to load KLDs if we're mounting the root. */ 851 if (fsflags & MNT_ROOTFS) 852 vfsp = vfs_byname(fstype); 853 else 854 vfsp = vfs_byname_kld(fstype, td, &error); 855 if (vfsp == NULL) 856 return (ENODEV); 857 } 858 /* 859 * Get vnode to be covered 860 */ 861 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF | AUDITVNODE1, UIO_SYSSPACE, 862 fspath, td); 863 if ((error = namei(&nd)) != 0) 864 return (error); 865 NDFREE(&nd, NDF_ONLY_PNBUF); 866 vp = nd.ni_vp; 867 if (fsflags & MNT_UPDATE) { 868 if ((vp->v_vflag & VV_ROOT) == 0) { 869 vput(vp); 870 return (EINVAL); 871 } 872 mp = vp->v_mount; 873 MNT_ILOCK(mp); 874 flag = mp->mnt_flag; 875 /* 876 * We only allow the filesystem to be reloaded if it 877 * is currently mounted read-only. 878 */ 879 if ((fsflags & MNT_RELOAD) && 880 ((mp->mnt_flag & MNT_RDONLY) == 0)) { 881 MNT_IUNLOCK(mp); 882 vput(vp); 883 return (EOPNOTSUPP); /* Needs translation */ 884 } 885 MNT_IUNLOCK(mp); 886 /* 887 * Only privileged root, or (if MNT_USER is set) the user that 888 * did the original mount is permitted to update it. 889 */ 890 error = vfs_suser(mp, td); 891 if (error) { 892 vput(vp); 893 return (error); 894 } 895 if (vfs_busy(mp, LK_NOWAIT, 0, td)) { 896 vput(vp); 897 return (EBUSY); 898 } 899 VI_LOCK(vp); 900 if ((vp->v_iflag & VI_MOUNT) != 0 || 901 vp->v_mountedhere != NULL) { 902 VI_UNLOCK(vp); 903 vfs_unbusy(mp, td); 904 vput(vp); 905 return (EBUSY); 906 } 907 vp->v_iflag |= VI_MOUNT; 908 VI_UNLOCK(vp); 909 MNT_ILOCK(mp); 910 mp->mnt_flag |= fsflags & 911 (MNT_RELOAD | MNT_FORCE | MNT_UPDATE | MNT_SNAPSHOT | MNT_ROOTFS); 912 MNT_IUNLOCK(mp); 913 VOP_UNLOCK(vp, 0, td); 914 mp->mnt_optnew = fsdata; 915 vfs_mergeopts(mp->mnt_optnew, mp->mnt_opt); 916 } else { 917 /* 918 * If the user is not root, ensure that they own the directory 919 * onto which we are attempting to mount. 920 */ 921 error = VOP_GETATTR(vp, &va, td->td_ucred, td); 922 if (error) { 923 vput(vp); 924 return (error); 925 } 926 if (va.va_uid != td->td_ucred->cr_uid) { 927 error = priv_check_cred(td->td_ucred, PRIV_VFS_ADMIN, 928 SUSER_ALLOWJAIL); 929 if (error) { 930 vput(vp); 931 return (error); 932 } 933 } 934 error = vinvalbuf(vp, V_SAVE, td, 0, 0); 935 if (error != 0) { 936 vput(vp); 937 return (error); 938 } 939 if (vp->v_type != VDIR) { 940 vput(vp); 941 return (ENOTDIR); 942 } 943 VI_LOCK(vp); 944 if ((vp->v_iflag & VI_MOUNT) != 0 || 945 vp->v_mountedhere != NULL) { 946 VI_UNLOCK(vp); 947 vput(vp); 948 return (EBUSY); 949 } 950 vp->v_iflag |= VI_MOUNT; 951 VI_UNLOCK(vp); 952 953 /* 954 * Allocate and initialize the filesystem. 955 */ 956 mp = vfs_mount_alloc(vp, vfsp, fspath, td); 957 VOP_UNLOCK(vp, 0, td); 958 959 /* XXXMAC: pass to vfs_mount_alloc? */ 960 mp->mnt_optnew = fsdata; 961 } 962 963 /* 964 * Set the mount level flags. 965 */ 966 MNT_ILOCK(mp); 967 mp->mnt_flag = (mp->mnt_flag & ~MNT_UPDATEMASK) | 968 (fsflags & (MNT_UPDATEMASK | MNT_FORCE | MNT_ROOTFS | 969 MNT_RDONLY)); 970 if ((mp->mnt_flag & MNT_ASYNC) == 0) 971 mp->mnt_kern_flag &= ~MNTK_ASYNC; 972 MNT_IUNLOCK(mp); 973 /* 974 * Mount the filesystem. 975 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they 976 * get. No freeing of cn_pnbuf. 977 */ 978 error = VFS_MOUNT(mp, td); 979 980 /* 981 * Process the export option only if we are 982 * updating mount options. 983 */ 984 if (!error && (fsflags & MNT_UPDATE)) { 985 if (vfs_copyopt(mp->mnt_optnew, "export", &export, 986 sizeof(export)) == 0) 987 error = vfs_export(mp, &export); 988 } 989 990 if (!error) { 991 if (mp->mnt_opt != NULL) 992 vfs_freeopts(mp->mnt_opt); 993 mp->mnt_opt = mp->mnt_optnew; 994 (void)VFS_STATFS(mp, &mp->mnt_stat, td); 995 } 996 /* 997 * Prevent external consumers of mount options from reading 998 * mnt_optnew. 999 */ 1000 mp->mnt_optnew = NULL; 1001 if (mp->mnt_flag & MNT_UPDATE) { 1002 MNT_ILOCK(mp); 1003 if (error) 1004 mp->mnt_flag = (mp->mnt_flag & MNT_QUOTA) | 1005 (flag & ~MNT_QUOTA); 1006 else 1007 mp->mnt_flag &= ~(MNT_UPDATE | MNT_RELOAD | 1008 MNT_FORCE | MNT_SNAPSHOT); 1009 if ((mp->mnt_flag & MNT_ASYNC) != 0 && mp->mnt_noasync == 0) 1010 mp->mnt_kern_flag |= MNTK_ASYNC; 1011 else 1012 mp->mnt_kern_flag &= ~MNTK_ASYNC; 1013 MNT_IUNLOCK(mp); 1014 if ((mp->mnt_flag & MNT_RDONLY) == 0) { 1015 if (mp->mnt_syncer == NULL) 1016 error = vfs_allocate_syncvnode(mp); 1017 } else { 1018 if (mp->mnt_syncer != NULL) 1019 vrele(mp->mnt_syncer); 1020 mp->mnt_syncer = NULL; 1021 } 1022 vfs_unbusy(mp, td); 1023 VI_LOCK(vp); 1024 vp->v_iflag &= ~VI_MOUNT; 1025 VI_UNLOCK(vp); 1026 vrele(vp); 1027 return (error); 1028 } 1029 MNT_ILOCK(mp); 1030 if ((mp->mnt_flag & MNT_ASYNC) != 0 && mp->mnt_noasync == 0) 1031 mp->mnt_kern_flag |= MNTK_ASYNC; 1032 else 1033 mp->mnt_kern_flag &= ~MNTK_ASYNC; 1034 MNT_IUNLOCK(mp); 1035 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 1036 /* 1037 * Put the new filesystem on the mount list after root. 1038 */ 1039 cache_purge(vp); 1040 if (!error) { 1041 struct vnode *newdp; 1042 1043 VI_LOCK(vp); 1044 vp->v_iflag &= ~VI_MOUNT; 1045 VI_UNLOCK(vp); 1046 vp->v_mountedhere = mp; 1047 mtx_lock(&mountlist_mtx); 1048 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list); 1049 mtx_unlock(&mountlist_mtx); 1050 vfs_event_signal(NULL, VQ_MOUNT, 0); 1051 if (VFS_ROOT(mp, LK_EXCLUSIVE, &newdp, td)) 1052 panic("mount: lost mount"); 1053 mountcheckdirs(vp, newdp); 1054 vput(newdp); 1055 VOP_UNLOCK(vp, 0, td); 1056 if ((mp->mnt_flag & MNT_RDONLY) == 0) 1057 error = vfs_allocate_syncvnode(mp); 1058 vfs_unbusy(mp, td); 1059 if (error) 1060 vrele(vp); 1061 } else { 1062 VI_LOCK(vp); 1063 vp->v_iflag &= ~VI_MOUNT; 1064 VI_UNLOCK(vp); 1065 vfs_unbusy(mp, td); 1066 vfs_mount_destroy(mp); 1067 vput(vp); 1068 } 1069 return (error); 1070 } 1071 1072 /* 1073 * Unmount a filesystem. 1074 * 1075 * Note: unmount takes a path to the vnode mounted on as argument, not 1076 * special file (as before). 1077 */ 1078 #ifndef _SYS_SYSPROTO_H_ 1079 struct unmount_args { 1080 char *path; 1081 int flags; 1082 }; 1083 #endif 1084 /* ARGSUSED */ 1085 int 1086 unmount(td, uap) 1087 struct thread *td; 1088 register struct unmount_args /* { 1089 char *path; 1090 int flags; 1091 } */ *uap; 1092 { 1093 struct mount *mp; 1094 char *pathbuf; 1095 int error, id0, id1; 1096 1097 if (jailed(td->td_ucred)) 1098 return (EPERM); 1099 if (usermount == 0) { 1100 error = priv_check(td, PRIV_VFS_UNMOUNT); 1101 if (error) 1102 return (error); 1103 } 1104 1105 pathbuf = malloc(MNAMELEN, M_TEMP, M_WAITOK); 1106 error = copyinstr(uap->path, pathbuf, MNAMELEN, NULL); 1107 if (error) { 1108 free(pathbuf, M_TEMP); 1109 return (error); 1110 } 1111 AUDIT_ARG(upath, td, pathbuf, ARG_UPATH1); 1112 mtx_lock(&Giant); 1113 if (uap->flags & MNT_BYFSID) { 1114 /* Decode the filesystem ID. */ 1115 if (sscanf(pathbuf, "FSID:%d:%d", &id0, &id1) != 2) { 1116 mtx_unlock(&Giant); 1117 free(pathbuf, M_TEMP); 1118 return (EINVAL); 1119 } 1120 1121 mtx_lock(&mountlist_mtx); 1122 TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) { 1123 if (mp->mnt_stat.f_fsid.val[0] == id0 && 1124 mp->mnt_stat.f_fsid.val[1] == id1) 1125 break; 1126 } 1127 mtx_unlock(&mountlist_mtx); 1128 } else { 1129 mtx_lock(&mountlist_mtx); 1130 TAILQ_FOREACH_REVERSE(mp, &mountlist, mntlist, mnt_list) { 1131 if (strcmp(mp->mnt_stat.f_mntonname, pathbuf) == 0) 1132 break; 1133 } 1134 mtx_unlock(&mountlist_mtx); 1135 } 1136 free(pathbuf, M_TEMP); 1137 if (mp == NULL) { 1138 /* 1139 * Previously we returned ENOENT for a nonexistent path and 1140 * EINVAL for a non-mountpoint. We cannot tell these apart 1141 * now, so in the !MNT_BYFSID case return the more likely 1142 * EINVAL for compatibility. 1143 */ 1144 mtx_unlock(&Giant); 1145 return ((uap->flags & MNT_BYFSID) ? ENOENT : EINVAL); 1146 } 1147 1148 /* 1149 * Don't allow unmounting the root filesystem. 1150 */ 1151 if (mp->mnt_flag & MNT_ROOTFS) { 1152 mtx_unlock(&Giant); 1153 return (EINVAL); 1154 } 1155 error = dounmount(mp, uap->flags, td); 1156 mtx_unlock(&Giant); 1157 return (error); 1158 } 1159 1160 /* 1161 * Do the actual filesystem unmount. 1162 */ 1163 int 1164 dounmount(mp, flags, td) 1165 struct mount *mp; 1166 int flags; 1167 struct thread *td; 1168 { 1169 struct vnode *coveredvp, *fsrootvp; 1170 int error; 1171 int async_flag; 1172 int mnt_gen_r; 1173 1174 mtx_assert(&Giant, MA_OWNED); 1175 1176 if ((coveredvp = mp->mnt_vnodecovered) != NULL) { 1177 mnt_gen_r = mp->mnt_gen; 1178 VI_LOCK(coveredvp); 1179 vholdl(coveredvp); 1180 error = vn_lock(coveredvp, LK_EXCLUSIVE | LK_INTERLOCK, td); 1181 vdrop(coveredvp); 1182 /* 1183 * Check for mp being unmounted while waiting for the 1184 * covered vnode lock. 1185 */ 1186 if (error) 1187 return (error); 1188 if (coveredvp->v_mountedhere != mp || 1189 coveredvp->v_mountedhere->mnt_gen != mnt_gen_r) { 1190 VOP_UNLOCK(coveredvp, 0, td); 1191 return (EBUSY); 1192 } 1193 } 1194 /* 1195 * Only privileged root, or (if MNT_USER is set) the user that did the 1196 * original mount is permitted to unmount this filesystem. 1197 */ 1198 error = vfs_suser(mp, td); 1199 if (error) { 1200 if (coveredvp) 1201 VOP_UNLOCK(coveredvp, 0, td); 1202 return (error); 1203 } 1204 1205 MNT_ILOCK(mp); 1206 if (mp->mnt_kern_flag & MNTK_UNMOUNT) { 1207 MNT_IUNLOCK(mp); 1208 if (coveredvp) 1209 VOP_UNLOCK(coveredvp, 0, td); 1210 return (EBUSY); 1211 } 1212 mp->mnt_kern_flag |= MNTK_UNMOUNT; 1213 /* Allow filesystems to detect that a forced unmount is in progress. */ 1214 if (flags & MNT_FORCE) 1215 mp->mnt_kern_flag |= MNTK_UNMOUNTF; 1216 error = lockmgr(&mp->mnt_lock, LK_DRAIN | LK_INTERLOCK | 1217 ((flags & MNT_FORCE) ? 0 : LK_NOWAIT), MNT_MTX(mp), td); 1218 if (error) { 1219 MNT_ILOCK(mp); 1220 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF); 1221 if (mp->mnt_kern_flag & MNTK_MWAIT) 1222 wakeup(mp); 1223 MNT_IUNLOCK(mp); 1224 if (coveredvp) 1225 VOP_UNLOCK(coveredvp, 0, td); 1226 return (error); 1227 } 1228 vn_start_write(NULL, &mp, V_WAIT); 1229 1230 if (mp->mnt_flag & MNT_EXPUBLIC) 1231 vfs_setpublicfs(NULL, NULL, NULL); 1232 1233 vfs_msync(mp, MNT_WAIT); 1234 MNT_ILOCK(mp); 1235 async_flag = mp->mnt_flag & MNT_ASYNC; 1236 mp->mnt_flag &= ~MNT_ASYNC; 1237 mp->mnt_kern_flag &= ~MNTK_ASYNC; 1238 MNT_IUNLOCK(mp); 1239 cache_purgevfs(mp); /* remove cache entries for this file sys */ 1240 if (mp->mnt_syncer != NULL) 1241 vrele(mp->mnt_syncer); 1242 /* 1243 * For forced unmounts, move process cdir/rdir refs on the fs root 1244 * vnode to the covered vnode. For non-forced unmounts we want 1245 * such references to cause an EBUSY error. 1246 */ 1247 if ((flags & MNT_FORCE) && 1248 VFS_ROOT(mp, LK_EXCLUSIVE, &fsrootvp, td) == 0) { 1249 if (mp->mnt_vnodecovered != NULL) 1250 mountcheckdirs(fsrootvp, mp->mnt_vnodecovered); 1251 if (fsrootvp == rootvnode) { 1252 vrele(rootvnode); 1253 rootvnode = NULL; 1254 } 1255 vput(fsrootvp); 1256 } 1257 if (((mp->mnt_flag & MNT_RDONLY) || 1258 (error = VFS_SYNC(mp, MNT_WAIT, td)) == 0) || 1259 (flags & MNT_FORCE)) { 1260 error = VFS_UNMOUNT(mp, flags, td); 1261 } 1262 vn_finished_write(mp); 1263 if (error) { 1264 /* Undo cdir/rdir and rootvnode changes made above. */ 1265 if ((flags & MNT_FORCE) && 1266 VFS_ROOT(mp, LK_EXCLUSIVE, &fsrootvp, td) == 0) { 1267 if (mp->mnt_vnodecovered != NULL) 1268 mountcheckdirs(mp->mnt_vnodecovered, fsrootvp); 1269 if (rootvnode == NULL) { 1270 rootvnode = fsrootvp; 1271 vref(rootvnode); 1272 } 1273 vput(fsrootvp); 1274 } 1275 if ((mp->mnt_flag & MNT_RDONLY) == 0 && mp->mnt_syncer == NULL) 1276 (void) vfs_allocate_syncvnode(mp); 1277 MNT_ILOCK(mp); 1278 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF); 1279 mp->mnt_flag |= async_flag; 1280 if ((mp->mnt_flag & MNT_ASYNC) != 0 && mp->mnt_noasync == 0) 1281 mp->mnt_kern_flag |= MNTK_ASYNC; 1282 lockmgr(&mp->mnt_lock, LK_RELEASE, NULL, td); 1283 if (mp->mnt_kern_flag & MNTK_MWAIT) 1284 wakeup(mp); 1285 MNT_IUNLOCK(mp); 1286 if (coveredvp) 1287 VOP_UNLOCK(coveredvp, 0, td); 1288 return (error); 1289 } 1290 mtx_lock(&mountlist_mtx); 1291 TAILQ_REMOVE(&mountlist, mp, mnt_list); 1292 mtx_unlock(&mountlist_mtx); 1293 if (coveredvp != NULL) { 1294 coveredvp->v_mountedhere = NULL; 1295 vput(coveredvp); 1296 } 1297 vfs_event_signal(NULL, VQ_UNMOUNT, 0); 1298 lockmgr(&mp->mnt_lock, LK_RELEASE, NULL, td); 1299 vfs_mount_destroy(mp); 1300 return (0); 1301 } 1302 1303 /* 1304 * --------------------------------------------------------------------- 1305 * Mounting of root filesystem 1306 * 1307 */ 1308 1309 struct root_hold_token { 1310 const char *who; 1311 LIST_ENTRY(root_hold_token) list; 1312 }; 1313 1314 static LIST_HEAD(, root_hold_token) root_holds = 1315 LIST_HEAD_INITIALIZER(&root_holds); 1316 1317 struct root_hold_token * 1318 root_mount_hold(const char *identifier) 1319 { 1320 struct root_hold_token *h; 1321 1322 h = malloc(sizeof *h, M_DEVBUF, M_ZERO | M_WAITOK); 1323 h->who = identifier; 1324 mtx_lock(&mountlist_mtx); 1325 LIST_INSERT_HEAD(&root_holds, h, list); 1326 mtx_unlock(&mountlist_mtx); 1327 return (h); 1328 } 1329 1330 void 1331 root_mount_rel(struct root_hold_token *h) 1332 { 1333 1334 mtx_lock(&mountlist_mtx); 1335 LIST_REMOVE(h, list); 1336 wakeup(&root_holds); 1337 mtx_unlock(&mountlist_mtx); 1338 free(h, M_DEVBUF); 1339 } 1340 1341 static void 1342 root_mount_wait(void) 1343 { 1344 struct root_hold_token *h; 1345 1346 for (;;) { 1347 DROP_GIANT(); 1348 g_waitidle(); 1349 PICKUP_GIANT(); 1350 mtx_lock(&mountlist_mtx); 1351 if (LIST_EMPTY(&root_holds)) { 1352 mtx_unlock(&mountlist_mtx); 1353 break; 1354 } 1355 printf("Root mount waiting for:"); 1356 LIST_FOREACH(h, &root_holds, list) 1357 printf(" %s", h->who); 1358 printf("\n"); 1359 msleep(&root_holds, &mountlist_mtx, PZERO | PDROP, "roothold", 1360 hz); 1361 } 1362 } 1363 1364 static void 1365 set_rootvnode(struct thread *td) 1366 { 1367 struct proc *p; 1368 1369 if (VFS_ROOT(TAILQ_FIRST(&mountlist), LK_EXCLUSIVE, &rootvnode, td)) 1370 panic("Cannot find root vnode"); 1371 1372 p = td->td_proc; 1373 FILEDESC_LOCK(p->p_fd); 1374 1375 if (p->p_fd->fd_cdir != NULL) 1376 vrele(p->p_fd->fd_cdir); 1377 p->p_fd->fd_cdir = rootvnode; 1378 VREF(rootvnode); 1379 1380 if (p->p_fd->fd_rdir != NULL) 1381 vrele(p->p_fd->fd_rdir); 1382 p->p_fd->fd_rdir = rootvnode; 1383 VREF(rootvnode); 1384 1385 FILEDESC_UNLOCK(p->p_fd); 1386 1387 VOP_UNLOCK(rootvnode, 0, td); 1388 } 1389 1390 /* 1391 * Mount /devfs as our root filesystem, but do not put it on the mountlist 1392 * yet. Create a /dev -> / symlink so that absolute pathnames will lookup. 1393 */ 1394 1395 static void 1396 devfs_first(void) 1397 { 1398 struct thread *td = curthread; 1399 struct vfsoptlist *opts; 1400 struct vfsconf *vfsp; 1401 struct mount *mp = NULL; 1402 int error; 1403 1404 vfsp = vfs_byname("devfs"); 1405 KASSERT(vfsp != NULL, ("Could not find devfs by name")); 1406 if (vfsp == NULL) 1407 return; 1408 1409 mp = vfs_mount_alloc(NULLVP, vfsp, "/dev", td); 1410 1411 error = VFS_MOUNT(mp, td); 1412 KASSERT(error == 0, ("VFS_MOUNT(devfs) failed %d", error)); 1413 if (error) 1414 return; 1415 1416 opts = malloc(sizeof(struct vfsoptlist), M_MOUNT, M_WAITOK); 1417 TAILQ_INIT(opts); 1418 mp->mnt_opt = opts; 1419 1420 mtx_lock(&mountlist_mtx); 1421 TAILQ_INSERT_HEAD(&mountlist, mp, mnt_list); 1422 mtx_unlock(&mountlist_mtx); 1423 1424 set_rootvnode(td); 1425 1426 error = kern_symlink(td, "/", "dev", UIO_SYSSPACE); 1427 if (error) 1428 printf("kern_symlink /dev -> / returns %d\n", error); 1429 } 1430 1431 /* 1432 * Surgically move our devfs to be mounted on /dev. 1433 */ 1434 1435 static void 1436 devfs_fixup(struct thread *td) 1437 { 1438 struct nameidata nd; 1439 int error; 1440 struct vnode *vp, *dvp; 1441 struct mount *mp; 1442 1443 /* Remove our devfs mount from the mountlist and purge the cache */ 1444 mtx_lock(&mountlist_mtx); 1445 mp = TAILQ_FIRST(&mountlist); 1446 TAILQ_REMOVE(&mountlist, mp, mnt_list); 1447 mtx_unlock(&mountlist_mtx); 1448 cache_purgevfs(mp); 1449 1450 VFS_ROOT(mp, LK_EXCLUSIVE, &dvp, td); 1451 VI_LOCK(dvp); 1452 dvp->v_iflag &= ~VI_MOUNT; 1453 dvp->v_mountedhere = NULL; 1454 VI_UNLOCK(dvp); 1455 1456 /* Set up the real rootvnode, and purge the cache */ 1457 TAILQ_FIRST(&mountlist)->mnt_vnodecovered = NULL; 1458 set_rootvnode(td); 1459 cache_purgevfs(rootvnode->v_mount); 1460 1461 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, "/dev", td); 1462 error = namei(&nd); 1463 if (error) { 1464 printf("Lookup of /dev for devfs, error: %d\n", error); 1465 return; 1466 } 1467 NDFREE(&nd, NDF_ONLY_PNBUF); 1468 vp = nd.ni_vp; 1469 if (vp->v_type != VDIR) { 1470 vput(vp); 1471 } 1472 error = vinvalbuf(vp, V_SAVE, td, 0, 0); 1473 if (error) { 1474 vput(vp); 1475 } 1476 cache_purge(vp); 1477 mp->mnt_vnodecovered = vp; 1478 vp->v_mountedhere = mp; 1479 mtx_lock(&mountlist_mtx); 1480 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list); 1481 mtx_unlock(&mountlist_mtx); 1482 VOP_UNLOCK(vp, 0, td); 1483 vput(dvp); 1484 vfs_unbusy(mp, td); 1485 1486 /* Unlink the no longer needed /dev/dev -> / symlink */ 1487 kern_unlink(td, "/dev/dev", UIO_SYSSPACE); 1488 } 1489 1490 /* 1491 * Report errors during filesystem mounting. 1492 */ 1493 void 1494 vfs_mount_error(struct mount *mp, const char *fmt, ...) 1495 { 1496 struct vfsoptlist *moptlist = mp->mnt_optnew; 1497 va_list ap; 1498 int error, len; 1499 char *errmsg; 1500 1501 error = vfs_getopt(moptlist, "errmsg", (void **)&errmsg, &len); 1502 if (error || errmsg == NULL || len <= 0) 1503 return; 1504 1505 va_start(ap, fmt); 1506 vsnprintf(errmsg, (size_t)len, fmt, ap); 1507 va_end(ap); 1508 } 1509 1510 /* 1511 * Find and mount the root filesystem 1512 */ 1513 void 1514 vfs_mountroot(void) 1515 { 1516 char *cp; 1517 int error, i, asked = 0; 1518 1519 root_mount_wait(); 1520 1521 mount_zone = uma_zcreate("Mountpoints", sizeof(struct mount), 1522 NULL, NULL, mount_init, mount_fini, 1523 UMA_ALIGN_PTR, UMA_ZONE_NOFREE); 1524 devfs_first(); 1525 1526 /* 1527 * We are booted with instructions to prompt for the root filesystem. 1528 */ 1529 if (boothowto & RB_ASKNAME) { 1530 if (!vfs_mountroot_ask()) 1531 return; 1532 asked = 1; 1533 } 1534 1535 /* 1536 * The root filesystem information is compiled in, and we are 1537 * booted with instructions to use it. 1538 */ 1539 if (ctrootdevname != NULL && (boothowto & RB_DFLTROOT)) { 1540 if (!vfs_mountroot_try(ctrootdevname)) 1541 return; 1542 ctrootdevname = NULL; 1543 } 1544 1545 /* 1546 * We've been given the generic "use CDROM as root" flag. This is 1547 * necessary because one media may be used in many different 1548 * devices, so we need to search for them. 1549 */ 1550 if (boothowto & RB_CDROM) { 1551 for (i = 0; cdrom_rootdevnames[i] != NULL; i++) { 1552 if (!vfs_mountroot_try(cdrom_rootdevnames[i])) 1553 return; 1554 } 1555 } 1556 1557 /* 1558 * Try to use the value read by the loader from /etc/fstab, or 1559 * supplied via some other means. This is the preferred 1560 * mechanism. 1561 */ 1562 cp = getenv("vfs.root.mountfrom"); 1563 if (cp != NULL) { 1564 error = vfs_mountroot_try(cp); 1565 freeenv(cp); 1566 if (!error) 1567 return; 1568 } 1569 1570 /* 1571 * Try values that may have been computed by code during boot 1572 */ 1573 if (!vfs_mountroot_try(rootdevnames[0])) 1574 return; 1575 if (!vfs_mountroot_try(rootdevnames[1])) 1576 return; 1577 1578 /* 1579 * If we (still) have a compiled-in default, try it. 1580 */ 1581 if (ctrootdevname != NULL) 1582 if (!vfs_mountroot_try(ctrootdevname)) 1583 return; 1584 /* 1585 * Everything so far has failed, prompt on the console if we haven't 1586 * already tried that. 1587 */ 1588 if (!asked) 1589 if (!vfs_mountroot_ask()) 1590 return; 1591 1592 panic("Root mount failed, startup aborted."); 1593 } 1594 1595 /* 1596 * Mount (mountfrom) as the root filesystem. 1597 */ 1598 static int 1599 vfs_mountroot_try(const char *mountfrom) 1600 { 1601 struct mount *mp; 1602 char *vfsname, *path; 1603 time_t timebase; 1604 int error; 1605 char patt[32]; 1606 1607 vfsname = NULL; 1608 path = NULL; 1609 mp = NULL; 1610 error = EINVAL; 1611 1612 if (mountfrom == NULL) 1613 return (error); /* don't complain */ 1614 printf("Trying to mount root from %s\n", mountfrom); 1615 1616 /* parse vfs name and path */ 1617 vfsname = malloc(MFSNAMELEN, M_MOUNT, M_WAITOK); 1618 path = malloc(MNAMELEN, M_MOUNT, M_WAITOK); 1619 vfsname[0] = path[0] = 0; 1620 sprintf(patt, "%%%d[a-z0-9]:%%%ds", MFSNAMELEN, MNAMELEN); 1621 if (sscanf(mountfrom, patt, vfsname, path) < 1) 1622 goto out; 1623 1624 if (path[0] == '\0') 1625 strcpy(path, ROOTNAME); 1626 1627 error = kernel_vmount( 1628 MNT_RDONLY | MNT_ROOTFS, 1629 "fstype", vfsname, 1630 "fspath", "/", 1631 "from", path, 1632 NULL); 1633 if (error == 0) { 1634 /* 1635 * We mount devfs prior to mounting the / FS, so the first 1636 * entry will typically be devfs. 1637 */ 1638 mp = TAILQ_FIRST(&mountlist); 1639 KASSERT(mp != NULL, ("%s: mountlist is empty", __func__)); 1640 1641 /* 1642 * Iterate over all currently mounted file systems and use 1643 * the time stamp found to check and/or initialize the RTC. 1644 * Typically devfs has no time stamp and the only other FS 1645 * is the actual / FS. 1646 * Call inittodr() only once and pass it the largest of the 1647 * timestamps we encounter. 1648 */ 1649 timebase = 0; 1650 do { 1651 if (mp->mnt_time > timebase) 1652 timebase = mp->mnt_time; 1653 mp = TAILQ_NEXT(mp, mnt_list); 1654 } while (mp != NULL); 1655 inittodr(timebase); 1656 1657 devfs_fixup(curthread); 1658 } 1659 out: 1660 free(path, M_MOUNT); 1661 free(vfsname, M_MOUNT); 1662 return (error); 1663 } 1664 1665 /* 1666 * --------------------------------------------------------------------- 1667 * Interactive root filesystem selection code. 1668 */ 1669 1670 static int 1671 vfs_mountroot_ask(void) 1672 { 1673 char name[128]; 1674 1675 for(;;) { 1676 printf("\nManual root filesystem specification:\n"); 1677 printf(" <fstype>:<device> Mount <device> using filesystem <fstype>\n"); 1678 #if defined(__amd64__) || defined(__i386__) || defined(__ia64__) 1679 printf(" eg. ufs:da0s1a\n"); 1680 #else 1681 printf(" eg. ufs:/dev/da0a\n"); 1682 #endif 1683 printf(" ? List valid disk boot devices\n"); 1684 printf(" <empty line> Abort manual input\n"); 1685 printf("\nmountroot> "); 1686 gets(name, sizeof(name), 1); 1687 if (name[0] == '\0') 1688 return (1); 1689 if (name[0] == '?') { 1690 printf("\nList of GEOM managed disk devices:\n "); 1691 g_dev_print(); 1692 continue; 1693 } 1694 if (!vfs_mountroot_try(name)) 1695 return (0); 1696 } 1697 } 1698 1699 /* 1700 * --------------------------------------------------------------------- 1701 * Functions for querying mount options/arguments from filesystems. 1702 */ 1703 1704 /* 1705 * Check that no unknown options are given 1706 */ 1707 int 1708 vfs_filteropt(struct vfsoptlist *opts, const char **legal) 1709 { 1710 struct vfsopt *opt; 1711 const char **t, *p; 1712 1713 1714 TAILQ_FOREACH(opt, opts, link) { 1715 p = opt->name; 1716 if (p[0] == 'n' && p[1] == 'o') 1717 p += 2; 1718 for(t = global_opts; *t != NULL; t++) 1719 if (!strcmp(*t, p)) 1720 break; 1721 if (*t != NULL) 1722 continue; 1723 for(t = legal; *t != NULL; t++) 1724 if (!strcmp(*t, p)) 1725 break; 1726 if (*t != NULL) 1727 continue; 1728 printf("mount option <%s> is unknown\n", p); 1729 return (EINVAL); 1730 } 1731 return (0); 1732 } 1733 1734 /* 1735 * Get a mount option by its name. 1736 * 1737 * Return 0 if the option was found, ENOENT otherwise. 1738 * If len is non-NULL it will be filled with the length 1739 * of the option. If buf is non-NULL, it will be filled 1740 * with the address of the option. 1741 */ 1742 int 1743 vfs_getopt(opts, name, buf, len) 1744 struct vfsoptlist *opts; 1745 const char *name; 1746 void **buf; 1747 int *len; 1748 { 1749 struct vfsopt *opt; 1750 1751 KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL")); 1752 1753 TAILQ_FOREACH(opt, opts, link) { 1754 if (strcmp(name, opt->name) == 0) { 1755 if (len != NULL) 1756 *len = opt->len; 1757 if (buf != NULL) 1758 *buf = opt->value; 1759 return (0); 1760 } 1761 } 1762 return (ENOENT); 1763 } 1764 1765 static int 1766 vfs_getopt_pos(struct vfsoptlist *opts, const char *name) 1767 { 1768 struct vfsopt *opt; 1769 int i; 1770 1771 if (opts == NULL) 1772 return (-1); 1773 1774 i = 0; 1775 TAILQ_FOREACH(opt, opts, link) { 1776 if (strcmp(name, opt->name) == 0) 1777 return (i); 1778 ++i; 1779 } 1780 return (-1); 1781 } 1782 1783 char * 1784 vfs_getopts(struct vfsoptlist *opts, const char *name, int *error) 1785 { 1786 struct vfsopt *opt; 1787 1788 *error = 0; 1789 TAILQ_FOREACH(opt, opts, link) { 1790 if (strcmp(name, opt->name) != 0) 1791 continue; 1792 if (((char *)opt->value)[opt->len - 1] != '\0') { 1793 *error = EINVAL; 1794 return (NULL); 1795 } 1796 return (opt->value); 1797 } 1798 *error = ENOENT; 1799 return (NULL); 1800 } 1801 1802 int 1803 vfs_flagopt(struct vfsoptlist *opts, const char *name, u_int *w, u_int val) 1804 { 1805 struct vfsopt *opt; 1806 1807 TAILQ_FOREACH(opt, opts, link) { 1808 if (strcmp(name, opt->name) == 0) { 1809 if (w != NULL) 1810 *w |= val; 1811 return (1); 1812 } 1813 } 1814 if (w != NULL) 1815 *w &= ~val; 1816 return (0); 1817 } 1818 1819 int 1820 vfs_scanopt(struct vfsoptlist *opts, const char *name, const char *fmt, ...) 1821 { 1822 va_list ap; 1823 struct vfsopt *opt; 1824 int ret; 1825 1826 KASSERT(opts != NULL, ("vfs_getopt: caller passed 'opts' as NULL")); 1827 1828 TAILQ_FOREACH(opt, opts, link) { 1829 if (strcmp(name, opt->name) != 0) 1830 continue; 1831 if (((char *)opt->value)[opt->len - 1] != '\0') 1832 return (0); 1833 va_start(ap, fmt); 1834 ret = vsscanf(opt->value, fmt, ap); 1835 va_end(ap); 1836 return (ret); 1837 } 1838 return (0); 1839 } 1840 1841 /* 1842 * Find and copy a mount option. 1843 * 1844 * The size of the buffer has to be specified 1845 * in len, if it is not the same length as the 1846 * mount option, EINVAL is returned. 1847 * Returns ENOENT if the option is not found. 1848 */ 1849 int 1850 vfs_copyopt(opts, name, dest, len) 1851 struct vfsoptlist *opts; 1852 const char *name; 1853 void *dest; 1854 int len; 1855 { 1856 struct vfsopt *opt; 1857 1858 KASSERT(opts != NULL, ("vfs_copyopt: caller passed 'opts' as NULL")); 1859 1860 TAILQ_FOREACH(opt, opts, link) { 1861 if (strcmp(name, opt->name) == 0) { 1862 if (len != opt->len) 1863 return (EINVAL); 1864 bcopy(opt->value, dest, opt->len); 1865 return (0); 1866 } 1867 } 1868 return (ENOENT); 1869 } 1870 1871 /* 1872 * This is a helper function for filesystems to traverse their 1873 * vnodes. See MNT_VNODE_FOREACH() in sys/mount.h 1874 */ 1875 1876 struct vnode * 1877 __mnt_vnode_next(struct vnode **mvp, struct mount *mp) 1878 { 1879 struct vnode *vp; 1880 1881 mtx_assert(MNT_MTX(mp), MA_OWNED); 1882 1883 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch")); 1884 vp = TAILQ_NEXT(*mvp, v_nmntvnodes); 1885 while (vp != NULL && vp->v_type == VMARKER) 1886 vp = TAILQ_NEXT(vp, v_nmntvnodes); 1887 1888 /* Check if we are done */ 1889 if (vp == NULL) { 1890 __mnt_vnode_markerfree(mvp, mp); 1891 return (NULL); 1892 } 1893 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes); 1894 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes); 1895 return (vp); 1896 } 1897 1898 struct vnode * 1899 __mnt_vnode_first(struct vnode **mvp, struct mount *mp) 1900 { 1901 struct vnode *vp; 1902 1903 mtx_assert(MNT_MTX(mp), MA_OWNED); 1904 1905 vp = TAILQ_FIRST(&mp->mnt_nvnodelist); 1906 while (vp != NULL && vp->v_type == VMARKER) 1907 vp = TAILQ_NEXT(vp, v_nmntvnodes); 1908 1909 /* Check if we are done */ 1910 if (vp == NULL) { 1911 *mvp = NULL; 1912 return (NULL); 1913 } 1914 mp->mnt_holdcnt++; 1915 MNT_IUNLOCK(mp); 1916 *mvp = (struct vnode *) malloc(sizeof(struct vnode), 1917 M_VNODE_MARKER, 1918 M_WAITOK | M_ZERO); 1919 MNT_ILOCK(mp); 1920 (*mvp)->v_type = VMARKER; 1921 1922 vp = TAILQ_FIRST(&mp->mnt_nvnodelist); 1923 while (vp != NULL && vp->v_type == VMARKER) 1924 vp = TAILQ_NEXT(vp, v_nmntvnodes); 1925 1926 /* Check if we are done */ 1927 if (vp == NULL) { 1928 MNT_IUNLOCK(mp); 1929 free(*mvp, M_VNODE_MARKER); 1930 MNT_ILOCK(mp); 1931 *mvp = NULL; 1932 mp->mnt_holdcnt--; 1933 if (mp->mnt_holdcnt == 0 && mp->mnt_holdcntwaiters != 0) 1934 wakeup(&mp->mnt_holdcnt); 1935 return (NULL); 1936 } 1937 mp->mnt_markercnt++; 1938 (*mvp)->v_mount = mp; 1939 TAILQ_INSERT_AFTER(&mp->mnt_nvnodelist, vp, *mvp, v_nmntvnodes); 1940 return (vp); 1941 } 1942 1943 1944 void 1945 __mnt_vnode_markerfree(struct vnode **mvp, struct mount *mp) 1946 { 1947 1948 if (*mvp == NULL) 1949 return; 1950 1951 mtx_assert(MNT_MTX(mp), MA_OWNED); 1952 1953 KASSERT((*mvp)->v_mount == mp, ("marker vnode mount list mismatch")); 1954 TAILQ_REMOVE(&mp->mnt_nvnodelist, *mvp, v_nmntvnodes); 1955 MNT_IUNLOCK(mp); 1956 free(*mvp, M_VNODE_MARKER); 1957 MNT_ILOCK(mp); 1958 *mvp = NULL; 1959 1960 mp->mnt_markercnt--; 1961 mp->mnt_holdcnt--; 1962 if (mp->mnt_holdcnt == 0 && mp->mnt_holdcntwaiters != 0) 1963 wakeup(&mp->mnt_holdcnt); 1964 } 1965 1966 1967 int 1968 __vfs_statfs(struct mount *mp, struct statfs *sbp, struct thread *td) 1969 { 1970 int error; 1971 1972 error = mp->mnt_op->vfs_statfs(mp, &mp->mnt_stat, td); 1973 if (sbp != &mp->mnt_stat) 1974 *sbp = mp->mnt_stat; 1975 return (error); 1976 } 1977 1978 void 1979 vfs_mountedfrom(struct mount *mp, const char *from) 1980 { 1981 1982 bzero(mp->mnt_stat.f_mntfromname, sizeof mp->mnt_stat.f_mntfromname); 1983 strlcpy(mp->mnt_stat.f_mntfromname, from, 1984 sizeof mp->mnt_stat.f_mntfromname); 1985 } 1986 1987 /* 1988 * --------------------------------------------------------------------- 1989 * This is the api for building mount args and mounting filesystems from 1990 * inside the kernel. 1991 * 1992 * The API works by accumulation of individual args. First error is 1993 * latched. 1994 * 1995 * XXX: should be documented in new manpage kernel_mount(9) 1996 */ 1997 1998 /* A memory allocation which must be freed when we are done */ 1999 struct mntaarg { 2000 SLIST_ENTRY(mntaarg) next; 2001 }; 2002 2003 /* The header for the mount arguments */ 2004 struct mntarg { 2005 struct iovec *v; 2006 int len; 2007 int error; 2008 SLIST_HEAD(, mntaarg) list; 2009 }; 2010 2011 /* 2012 * Add a boolean argument. 2013 * 2014 * flag is the boolean value. 2015 * name must start with "no". 2016 */ 2017 struct mntarg * 2018 mount_argb(struct mntarg *ma, int flag, const char *name) 2019 { 2020 2021 KASSERT(name[0] == 'n' && name[1] == 'o', 2022 ("mount_argb(...,%s): name must start with 'no'", name)); 2023 2024 return (mount_arg(ma, name + (flag ? 2 : 0), NULL, 0)); 2025 } 2026 2027 /* 2028 * Add an argument printf style 2029 */ 2030 struct mntarg * 2031 mount_argf(struct mntarg *ma, const char *name, const char *fmt, ...) 2032 { 2033 va_list ap; 2034 struct mntaarg *maa; 2035 struct sbuf *sb; 2036 int len; 2037 2038 if (ma == NULL) { 2039 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO); 2040 SLIST_INIT(&ma->list); 2041 } 2042 if (ma->error) 2043 return (ma); 2044 2045 ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2), 2046 M_MOUNT, M_WAITOK); 2047 ma->v[ma->len].iov_base = (void *)(uintptr_t)name; 2048 ma->v[ma->len].iov_len = strlen(name) + 1; 2049 ma->len++; 2050 2051 sb = sbuf_new(NULL, NULL, 0, SBUF_AUTOEXTEND); 2052 va_start(ap, fmt); 2053 sbuf_vprintf(sb, fmt, ap); 2054 va_end(ap); 2055 sbuf_finish(sb); 2056 len = sbuf_len(sb) + 1; 2057 maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO); 2058 SLIST_INSERT_HEAD(&ma->list, maa, next); 2059 bcopy(sbuf_data(sb), maa + 1, len); 2060 sbuf_delete(sb); 2061 2062 ma->v[ma->len].iov_base = maa + 1; 2063 ma->v[ma->len].iov_len = len; 2064 ma->len++; 2065 2066 return (ma); 2067 } 2068 2069 /* 2070 * Add an argument which is a userland string. 2071 */ 2072 struct mntarg * 2073 mount_argsu(struct mntarg *ma, const char *name, const void *val, int len) 2074 { 2075 struct mntaarg *maa; 2076 char *tbuf; 2077 2078 if (val == NULL) 2079 return (ma); 2080 if (ma == NULL) { 2081 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO); 2082 SLIST_INIT(&ma->list); 2083 } 2084 if (ma->error) 2085 return (ma); 2086 maa = malloc(sizeof *maa + len, M_MOUNT, M_WAITOK | M_ZERO); 2087 SLIST_INSERT_HEAD(&ma->list, maa, next); 2088 tbuf = (void *)(maa + 1); 2089 ma->error = copyinstr(val, tbuf, len, NULL); 2090 return (mount_arg(ma, name, tbuf, -1)); 2091 } 2092 2093 /* 2094 * Plain argument. 2095 * 2096 * If length is -1, use printf. 2097 */ 2098 struct mntarg * 2099 mount_arg(struct mntarg *ma, const char *name, const void *val, int len) 2100 { 2101 2102 if (ma == NULL) { 2103 ma = malloc(sizeof *ma, M_MOUNT, M_WAITOK | M_ZERO); 2104 SLIST_INIT(&ma->list); 2105 } 2106 if (ma->error) 2107 return (ma); 2108 2109 ma->v = realloc(ma->v, sizeof *ma->v * (ma->len + 2), 2110 M_MOUNT, M_WAITOK); 2111 ma->v[ma->len].iov_base = (void *)(uintptr_t)name; 2112 ma->v[ma->len].iov_len = strlen(name) + 1; 2113 ma->len++; 2114 2115 ma->v[ma->len].iov_base = (void *)(uintptr_t)val; 2116 if (len < 0) 2117 ma->v[ma->len].iov_len = strlen(val) + 1; 2118 else 2119 ma->v[ma->len].iov_len = len; 2120 ma->len++; 2121 return (ma); 2122 } 2123 2124 /* 2125 * Free a mntarg structure 2126 */ 2127 static void 2128 free_mntarg(struct mntarg *ma) 2129 { 2130 struct mntaarg *maa; 2131 2132 while (!SLIST_EMPTY(&ma->list)) { 2133 maa = SLIST_FIRST(&ma->list); 2134 SLIST_REMOVE_HEAD(&ma->list, next); 2135 free(maa, M_MOUNT); 2136 } 2137 free(ma->v, M_MOUNT); 2138 free(ma, M_MOUNT); 2139 } 2140 2141 /* 2142 * Mount a filesystem 2143 */ 2144 int 2145 kernel_mount(struct mntarg *ma, int flags) 2146 { 2147 struct uio auio; 2148 int error; 2149 2150 KASSERT(ma != NULL, ("kernel_mount NULL ma")); 2151 KASSERT(ma->v != NULL, ("kernel_mount NULL ma->v")); 2152 KASSERT(!(ma->len & 1), ("kernel_mount odd ma->len (%d)", ma->len)); 2153 2154 auio.uio_iov = ma->v; 2155 auio.uio_iovcnt = ma->len; 2156 auio.uio_segflg = UIO_SYSSPACE; 2157 2158 error = ma->error; 2159 if (!error) 2160 error = vfs_donmount(curthread, flags, &auio); 2161 free_mntarg(ma); 2162 return (error); 2163 } 2164 2165 /* 2166 * A printflike function to mount a filesystem. 2167 */ 2168 int 2169 kernel_vmount(int flags, ...) 2170 { 2171 struct mntarg *ma = NULL; 2172 va_list ap; 2173 const char *cp; 2174 const void *vp; 2175 int error; 2176 2177 va_start(ap, flags); 2178 for (;;) { 2179 cp = va_arg(ap, const char *); 2180 if (cp == NULL) 2181 break; 2182 vp = va_arg(ap, const void *); 2183 ma = mount_arg(ma, cp, vp, -1); 2184 } 2185 va_end(ap); 2186 2187 error = kernel_mount(ma, flags); 2188 return (error); 2189 } 2190