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