1 /* 2 * Copyright (c) 1989, 1993 3 * The Regents of the University of California. All rights reserved. 4 * (c) UNIX System Laboratories, Inc. 5 * All or some portions of this file are derived from material licensed 6 * to the University of California by American Telephone and Telegraph 7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with 8 * the permission of UNIX System Laboratories, Inc. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the University of 21 * California, Berkeley and its contributors. 22 * 4. Neither the name of the University nor the names of its contributors 23 * may be used to endorse or promote products derived from this software 24 * without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 36 * SUCH DAMAGE. 37 * 38 * Copyright (c) 1999 Michael Smith 39 * All rights reserved. 40 * Copyright (c) 1999 Poul-Henning Kamp 41 * All rights reserved. 42 * 43 * Redistribution and use in source and binary forms, with or without 44 * modification, are permitted provided that the following conditions 45 * are met: 46 * 1. Redistributions of source code must retain the above copyright 47 * notice, this list of conditions and the following disclaimer. 48 * 2. Redistributions in binary form must reproduce the above copyright 49 * notice, this list of conditions and the following disclaimer in the 50 * documentation and/or other materials provided with the distribution. 51 * 52 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 53 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 54 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 55 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 56 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 57 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 58 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 59 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 60 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 61 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 62 * SUCH DAMAGE. 63 * 64 * $FreeBSD$ 65 */ 66 67 #include <sys/param.h> 68 #include <sys/conf.h> 69 #include <sys/cons.h> 70 #include <sys/kernel.h> 71 #include <sys/linker.h> 72 #include <sys/mac.h> 73 #include <sys/malloc.h> 74 #include <sys/mount.h> 75 #include <sys/mutex.h> 76 #include <sys/namei.h> 77 #include <sys/proc.h> 78 #include <sys/reboot.h> 79 #include <sys/sysproto.h> 80 #include <sys/sx.h> 81 #include <sys/sysctl.h> 82 #include <sys/sysent.h> 83 #include <sys/systm.h> 84 #include <sys/vnode.h> 85 86 #include <machine/stdarg.h> 87 88 #include "opt_rootdevname.h" 89 #include "opt_ddb.h" 90 #include "opt_mac.h" 91 92 #ifdef DDB 93 #include <ddb/ddb.h> 94 #endif 95 96 #define ROOTNAME "root_device" 97 98 static void checkdirs(struct vnode *olddp, struct vnode *newdp); 99 static int vfs_nmount(struct thread *td, int, struct uio *); 100 static int vfs_mountroot_try(char *mountfrom); 101 static int vfs_mountroot_ask(void); 102 static void gets(char *cp); 103 104 static int usermount = 0; /* if 1, non-root can mount fs. */ 105 SYSCTL_INT(_vfs, OID_AUTO, usermount, CTLFLAG_RW, &usermount, 0, ""); 106 107 MALLOC_DEFINE(M_MOUNT, "mount", "vfs mount structure"); 108 109 /* List of mounted filesystems. */ 110 struct mntlist mountlist = TAILQ_HEAD_INITIALIZER(mountlist); 111 112 /* For any iteration/modification of mountlist */ 113 struct mtx mountlist_mtx; 114 115 /* For any iteration/modification of mnt_vnodelist */ 116 struct mtx mntvnode_mtx; 117 118 /* 119 * The vnode of the system's root (/ in the filesystem, without chroot 120 * active.) 121 */ 122 struct vnode *rootvnode; 123 124 /* 125 * The root filesystem is detailed in the kernel environment variable 126 * vfs.root.mountfrom, which is expected to be in the general format 127 * 128 * <vfsname>:[<path>] 129 * vfsname := the name of a VFS known to the kernel and capable 130 * of being mounted as root 131 * path := disk device name or other data used by the filesystem 132 * to locate its physical store 133 */ 134 135 /* 136 * The root specifiers we will try if RB_CDROM is specified. 137 */ 138 static char *cdrom_rootdevnames[] = { 139 "cd9660:cd0a", 140 "cd9660:acd0a", 141 "cd9660:wcd0a", 142 NULL 143 }; 144 145 /* legacy find-root code */ 146 char *rootdevnames[2] = {NULL, NULL}; 147 static int setrootbyname(char *name); 148 dev_t rootdev = NODEV; 149 150 /* Remove one mount option. */ 151 static void 152 vfs_freeopt(struct vfsoptlist *opts, struct vfsopt *opt) 153 { 154 155 TAILQ_REMOVE(opts, opt, link); 156 free(opt->name, M_MOUNT); 157 if (opt->value != NULL) 158 free(opt->value, M_MOUNT); 159 #ifdef INVARIANTS 160 else if (opt->len != 0) 161 panic("%s: mount option with NULL value but length != 0", 162 __func__); 163 #endif 164 free(opt, M_MOUNT); 165 } 166 167 /* Release all resources related to the mount options. */ 168 static void 169 vfs_freeopts(struct vfsoptlist *opts) 170 { 171 struct vfsopt *opt; 172 173 while (!TAILQ_EMPTY(opts)) { 174 opt = TAILQ_FIRST(opts); 175 vfs_freeopt(opts, opt); 176 } 177 free(opts, M_MOUNT); 178 } 179 180 /* 181 * If a mount option is specified several times, 182 * (with or without the "no" prefix) only keep 183 * the last occurence of it. 184 */ 185 static void 186 vfs_sanitizeopts(struct vfsoptlist *opts) 187 { 188 struct vfsopt *opt, *opt2, *tmp; 189 int noopt; 190 191 TAILQ_FOREACH_REVERSE(opt, opts, vfsoptlist, link) { 192 if (strncmp(opt->name, "no", 2) == 0) 193 noopt = 1; 194 else 195 noopt = 0; 196 opt2 = TAILQ_PREV(opt, vfsoptlist, link); 197 while (opt2 != NULL) { 198 if (strcmp(opt2->name, opt->name) == 0 || 199 (noopt && strcmp(opt->name + 2, opt2->name) == 0) || 200 (!noopt && strncmp(opt2->name, "no", 2) == 0 && 201 strcmp(opt2->name + 2, opt->name) == 0)) { 202 tmp = TAILQ_PREV(opt2, vfsoptlist, link); 203 vfs_freeopt(opts, opt2); 204 opt2 = tmp; 205 } else { 206 opt2 = TAILQ_PREV(opt2, vfsoptlist, link); 207 } 208 } 209 } 210 } 211 212 /* 213 * Build a linked list of mount options from a struct uio. 214 */ 215 static int 216 vfs_buildopts(struct uio *auio, struct vfsoptlist **options) 217 { 218 struct vfsoptlist *opts; 219 struct vfsopt *opt; 220 unsigned int i, iovcnt; 221 int error, namelen, optlen; 222 223 iovcnt = auio->uio_iovcnt; 224 opts = malloc(sizeof(struct vfsoptlist), M_MOUNT, M_WAITOK); 225 TAILQ_INIT(opts); 226 for (i = 0; i < iovcnt; i += 2) { 227 opt = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK); 228 namelen = auio->uio_iov[i].iov_len; 229 optlen = auio->uio_iov[i + 1].iov_len; 230 opt->name = malloc(namelen, M_MOUNT, M_WAITOK); 231 opt->value = NULL; 232 if (auio->uio_segflg == UIO_SYSSPACE) { 233 bcopy(auio->uio_iov[i].iov_base, opt->name, namelen); 234 } else { 235 error = copyin(auio->uio_iov[i].iov_base, opt->name, 236 namelen); 237 if (error) 238 goto bad; 239 } 240 opt->len = optlen; 241 if (optlen != 0) { 242 opt->value = malloc(optlen, M_MOUNT, M_WAITOK); 243 if (auio->uio_segflg == UIO_SYSSPACE) { 244 bcopy(auio->uio_iov[i + 1].iov_base, opt->value, 245 optlen); 246 } else { 247 error = copyin(auio->uio_iov[i + 1].iov_base, 248 opt->value, optlen); 249 if (error) 250 goto bad; 251 } 252 } 253 TAILQ_INSERT_TAIL(opts, opt, link); 254 } 255 vfs_sanitizeopts(opts); 256 *options = opts; 257 return (0); 258 bad: 259 vfs_freeopts(opts); 260 return (error); 261 } 262 263 /* 264 * Merge the old mount options with the new ones passed 265 * in the MNT_UPDATE case. 266 */ 267 static void 268 vfs_mergeopts(struct vfsoptlist *toopts, struct vfsoptlist *opts) 269 { 270 struct vfsopt *opt, *opt2, *new; 271 272 TAILQ_FOREACH(opt, opts, link) { 273 /* 274 * Check that this option hasn't been redefined 275 * nor cancelled with a "no" mount option. 276 */ 277 opt2 = TAILQ_FIRST(toopts); 278 while (opt2 != NULL) { 279 if (strcmp(opt2->name, opt->name) == 0) 280 goto next; 281 if (strncmp(opt2->name, "no", 2) == 0 && 282 strcmp(opt2->name + 2, opt->name) == 0) { 283 vfs_freeopt(toopts, opt2); 284 goto next; 285 } 286 opt2 = TAILQ_NEXT(opt2, link); 287 } 288 /* We want this option, duplicate it. */ 289 new = malloc(sizeof(struct vfsopt), M_MOUNT, M_WAITOK); 290 new->name = malloc(strlen(opt->name) + 1, M_MOUNT, M_WAITOK); 291 strcpy(new->name, opt->name); 292 if (opt->len != 0) { 293 new->value = malloc(opt->len, M_MOUNT, M_WAITOK); 294 bcopy(opt->value, new->value, opt->len); 295 } else { 296 new->value = NULL; 297 } 298 new->len = opt->len; 299 TAILQ_INSERT_TAIL(toopts, new, link); 300 next: 301 continue; 302 } 303 } 304 305 /* 306 * New mount API. 307 */ 308 int 309 nmount(td, uap) 310 struct thread *td; 311 struct nmount_args /* { 312 syscallarg(struct iovec *) iovp; 313 syscallarg(unsigned int) iovcnt; 314 syscallarg(int) flags; 315 } */ *uap; 316 { 317 struct uio auio; 318 struct iovec *iov, *needfree; 319 struct iovec aiov[UIO_SMALLIOV]; 320 unsigned int i; 321 int error; 322 u_int iovlen, iovcnt; 323 324 iovcnt = SCARG(uap, iovcnt); 325 iovlen = iovcnt * sizeof (struct iovec); 326 /* 327 * Check that we have an even number of iovec's 328 * and that we have at least two options. 329 */ 330 if ((iovcnt & 1) || (iovcnt < 4) || (iovcnt > UIO_MAXIOV)) 331 return (EINVAL); 332 333 if (iovcnt > UIO_SMALLIOV) { 334 MALLOC(iov, struct iovec *, iovlen, M_IOV, M_WAITOK); 335 needfree = iov; 336 } else { 337 iov = aiov; 338 needfree = NULL; 339 } 340 auio.uio_iov = iov; 341 auio.uio_iovcnt = iovcnt; 342 auio.uio_segflg = UIO_USERSPACE; 343 if ((error = copyin(uap->iovp, iov, iovlen))) 344 goto finish; 345 346 for (i = 0; i < iovcnt; i++) { 347 if (iov->iov_len > MMAXOPTIONLEN) { 348 error = EINVAL; 349 goto finish; 350 } 351 iov++; 352 } 353 error = vfs_nmount(td, SCARG(uap, flags), &auio); 354 finish: 355 if (needfree != NULL) 356 free(needfree, M_TEMP); 357 return (error); 358 } 359 360 int 361 kernel_mount(iovp, iovcnt, flags) 362 struct iovec *iovp; 363 unsigned int iovcnt; 364 int flags; 365 { 366 struct uio auio; 367 int error; 368 369 /* 370 * Check that we have an even number of iovec's 371 * and that we have at least two options. 372 */ 373 if ((iovcnt & 1) || (iovcnt < 4)) 374 return (EINVAL); 375 376 auio.uio_iov = iovp; 377 auio.uio_iovcnt = iovcnt; 378 auio.uio_segflg = UIO_SYSSPACE; 379 380 error = vfs_nmount(curthread, flags, &auio); 381 return (error); 382 } 383 384 int 385 kernel_vmount(int flags, ...) 386 { 387 struct iovec *iovp; 388 struct uio auio; 389 va_list ap; 390 unsigned int iovcnt, iovlen, len; 391 const char *cp; 392 char *buf, *pos; 393 size_t n; 394 int error, i; 395 396 len = 0; 397 va_start(ap, flags); 398 for (iovcnt = 0; (cp = va_arg(ap, const char *)) != NULL; iovcnt++) 399 len += strlen(cp) + 1; 400 va_end(ap); 401 402 if (iovcnt < 4 || iovcnt & 1) 403 return (EINVAL); 404 405 iovlen = iovcnt * sizeof (struct iovec); 406 MALLOC(iovp, struct iovec *, iovlen, M_MOUNT, M_WAITOK); 407 MALLOC(buf, char *, len, M_MOUNT, M_WAITOK); 408 pos = buf; 409 va_start(ap, flags); 410 for (i = 0; i < iovcnt; i++) { 411 cp = va_arg(ap, const char *); 412 copystr(cp, pos, len - (pos - buf), &n); 413 iovp[i].iov_base = pos; 414 iovp[i].iov_len = n; 415 pos += n; 416 } 417 va_end(ap); 418 419 auio.uio_iov = iovp; 420 auio.uio_iovcnt = iovcnt; 421 auio.uio_segflg = UIO_SYSSPACE; 422 423 error = vfs_nmount(curthread, flags, &auio); 424 FREE(iovp, M_MOUNT); 425 FREE(buf, M_MOUNT); 426 return (error); 427 } 428 429 /* 430 * vfs_nmount(): actually attempt a filesystem mount. 431 */ 432 static int 433 vfs_nmount(td, fsflags, fsoptions) 434 struct thread *td; 435 int fsflags; /* Flags common to all filesystems. */ 436 struct uio *fsoptions; /* Options local to the filesystem. */ 437 { 438 linker_file_t lf; 439 struct vnode *vp; 440 struct mount *mp; 441 struct vfsconf *vfsp; 442 struct vfsoptlist *optlist; 443 char *fstype, *fspath; 444 int error, flag = 0, kern_flag = 0; 445 int fstypelen, fspathlen; 446 struct vattr va; 447 struct nameidata nd; 448 449 error = vfs_buildopts(fsoptions, &optlist); 450 if (error) 451 return (error); 452 453 /* 454 * We need these two options before the others, 455 * and they are mandatory for any filesystem. 456 * Ensure they are NUL terminated as well. 457 */ 458 fstypelen = 0; 459 error = vfs_getopt(optlist, "fstype", (void **)&fstype, &fstypelen); 460 if (error || fstype[fstypelen - 1] != '\0') { 461 error = EINVAL; 462 goto bad; 463 } 464 fspathlen = 0; 465 error = vfs_getopt(optlist, "fspath", (void **)&fspath, &fspathlen); 466 if (error || fspath[fspathlen - 1] != '\0') { 467 error = EINVAL; 468 goto bad; 469 } 470 471 /* 472 * Be ultra-paranoid about making sure the type and fspath 473 * variables will fit in our mp buffers, including the 474 * terminating NUL. 475 */ 476 if (fstypelen >= MFSNAMELEN - 1 || fspathlen >= MNAMELEN - 1) { 477 error = ENAMETOOLONG; 478 goto bad; 479 } 480 481 if (usermount == 0) { 482 error = suser(td); 483 if (error) 484 goto bad; 485 } 486 /* 487 * Do not allow NFS export by non-root users. 488 */ 489 if (fsflags & MNT_EXPORTED) { 490 error = suser(td); 491 if (error) 492 goto bad; 493 } 494 /* 495 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users. 496 */ 497 if (suser(td)) 498 fsflags |= MNT_NOSUID | MNT_NODEV; 499 /* 500 * Get vnode to be covered 501 */ 502 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspath, td); 503 if ((error = namei(&nd)) != 0) 504 goto bad; 505 NDFREE(&nd, NDF_ONLY_PNBUF); 506 vp = nd.ni_vp; 507 if (fsflags & MNT_UPDATE) { 508 if ((vp->v_vflag & VV_ROOT) == 0) { 509 vput(vp); 510 error = EINVAL; 511 goto bad; 512 } 513 mp = vp->v_mount; 514 flag = mp->mnt_flag; 515 kern_flag = mp->mnt_kern_flag; 516 /* 517 * We only allow the filesystem to be reloaded if it 518 * is currently mounted read-only. 519 */ 520 if ((fsflags & MNT_RELOAD) && 521 ((mp->mnt_flag & MNT_RDONLY) == 0)) { 522 vput(vp); 523 error = EOPNOTSUPP; /* Needs translation */ 524 goto bad; 525 } 526 /* 527 * Only root, or the user that did the original mount is 528 * permitted to update it. 529 */ 530 if (mp->mnt_stat.f_owner != td->td_ucred->cr_uid) { 531 error = suser(td); 532 if (error) { 533 vput(vp); 534 goto bad; 535 } 536 } 537 if (vfs_busy(mp, LK_NOWAIT, 0, td)) { 538 vput(vp); 539 error = EBUSY; 540 goto bad; 541 } 542 VI_LOCK(vp); 543 if ((vp->v_iflag & VI_MOUNT) != 0 || 544 vp->v_mountedhere != NULL) { 545 VI_UNLOCK(vp); 546 vfs_unbusy(mp, td); 547 vput(vp); 548 error = EBUSY; 549 goto bad; 550 } 551 vp->v_iflag |= VI_MOUNT; 552 VI_UNLOCK(vp); 553 mp->mnt_flag |= fsflags & 554 (MNT_RELOAD | MNT_FORCE | MNT_UPDATE | MNT_SNAPSHOT); 555 VOP_UNLOCK(vp, 0, td); 556 mp->mnt_optnew = optlist; 557 vfs_mergeopts(mp->mnt_optnew, mp->mnt_opt); 558 goto update; 559 } 560 /* 561 * If the user is not root, ensure that they own the directory 562 * onto which we are attempting to mount. 563 */ 564 error = VOP_GETATTR(vp, &va, td->td_ucred, td); 565 if (error) { 566 vput(vp); 567 goto bad; 568 } 569 if (va.va_uid != td->td_ucred->cr_uid) { 570 error = suser(td); 571 if (error) { 572 vput(vp); 573 goto bad; 574 } 575 } 576 if ((error = vinvalbuf(vp, V_SAVE, td->td_ucred, td, 0, 0)) != 0) { 577 vput(vp); 578 goto bad; 579 } 580 if (vp->v_type != VDIR) { 581 vput(vp); 582 error = ENOTDIR; 583 goto bad; 584 } 585 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) 586 if (!strcmp(vfsp->vfc_name, fstype)) 587 break; 588 if (vfsp == NULL) { 589 /* Only load modules for root (very important!). */ 590 error = suser(td); 591 if (error) { 592 vput(vp); 593 goto bad; 594 } 595 error = securelevel_gt(td->td_ucred, 0); 596 if (error) { 597 vput(vp); 598 goto bad; 599 } 600 error = linker_load_module(NULL, fstype, NULL, NULL, &lf); 601 if (error || lf == NULL) { 602 vput(vp); 603 if (lf == NULL) 604 error = ENODEV; 605 goto bad; 606 } 607 lf->userrefs++; 608 /* Look up again to see if the VFS was loaded. */ 609 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) 610 if (!strcmp(vfsp->vfc_name, fstype)) 611 break; 612 if (vfsp == NULL) { 613 lf->userrefs--; 614 linker_file_unload(lf); 615 vput(vp); 616 error = ENODEV; 617 goto bad; 618 } 619 } 620 VI_LOCK(vp); 621 if ((vp->v_iflag & VI_MOUNT) != 0 || 622 vp->v_mountedhere != NULL) { 623 VI_UNLOCK(vp); 624 vput(vp); 625 error = EBUSY; 626 goto bad; 627 } 628 vp->v_iflag |= VI_MOUNT; 629 VI_UNLOCK(vp); 630 631 /* 632 * Allocate and initialize the filesystem. 633 */ 634 mp = malloc(sizeof(struct mount), M_MOUNT, M_WAITOK | M_ZERO); 635 TAILQ_INIT(&mp->mnt_nvnodelist); 636 TAILQ_INIT(&mp->mnt_reservedvnlist); 637 lockinit(&mp->mnt_lock, PVFS, "vfslock", 0, LK_NOPAUSE); 638 (void)vfs_busy(mp, LK_NOWAIT, 0, td); 639 mp->mnt_op = vfsp->vfc_vfsops; 640 mp->mnt_vfc = vfsp; 641 vfsp->vfc_refcount++; 642 mp->mnt_stat.f_type = vfsp->vfc_typenum; 643 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK; 644 strncpy(mp->mnt_stat.f_fstypename, fstype, MFSNAMELEN); 645 mp->mnt_vnodecovered = vp; 646 mp->mnt_stat.f_owner = td->td_ucred->cr_uid; 647 strncpy(mp->mnt_stat.f_mntonname, fspath, MNAMELEN); 648 mp->mnt_iosize_max = DFLTPHYS; 649 #ifdef MAC 650 mac_init_mount(mp); 651 mac_create_mount(td->td_ucred, mp); 652 #endif 653 VOP_UNLOCK(vp, 0, td); 654 mp->mnt_optnew = optlist; /* XXXMAC: should this be above? */ 655 656 update: 657 /* 658 * Check if the fs implements the new VFS_NMOUNT() 659 * function, since the new system call was used. 660 */ 661 if (mp->mnt_op->vfs_mount != NULL) { 662 printf("%s doesn't support the new mount syscall\n", 663 mp->mnt_vfc->vfc_name); 664 VI_LOCK(vp); 665 vp->v_iflag &= ~VI_MOUNT; 666 VI_UNLOCK(vp); 667 if (mp->mnt_flag & MNT_UPDATE) 668 vfs_unbusy(mp, td); 669 else { 670 mp->mnt_vfc->vfc_refcount--; 671 vfs_unbusy(mp, td); 672 #ifdef MAC 673 mac_destroy_mount(mp); 674 #endif 675 free(mp, M_MOUNT); 676 } 677 vrele(vp); 678 error = EOPNOTSUPP; 679 goto bad; 680 } 681 682 /* 683 * Set the mount level flags. 684 */ 685 if (fsflags & MNT_RDONLY) 686 mp->mnt_flag |= MNT_RDONLY; 687 else if (mp->mnt_flag & MNT_RDONLY) 688 mp->mnt_kern_flag |= MNTK_WANTRDWR; 689 mp->mnt_flag &=~ MNT_UPDATEMASK; 690 mp->mnt_flag |= fsflags & (MNT_UPDATEMASK | MNT_FORCE); 691 /* 692 * Mount the filesystem. 693 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they 694 * get. No freeing of cn_pnbuf. 695 */ 696 error = VFS_NMOUNT(mp, &nd, td); 697 if (!error) { 698 if (mp->mnt_opt != NULL) 699 vfs_freeopts(mp->mnt_opt); 700 mp->mnt_opt = mp->mnt_optnew; 701 } 702 /* 703 * Prevent external consumers of mount 704 * options to read mnt_optnew. 705 */ 706 mp->mnt_optnew = NULL; 707 if (mp->mnt_flag & MNT_UPDATE) { 708 if (mp->mnt_kern_flag & MNTK_WANTRDWR) 709 mp->mnt_flag &= ~MNT_RDONLY; 710 mp->mnt_flag &=~ 711 (MNT_UPDATE | MNT_RELOAD | MNT_FORCE | MNT_SNAPSHOT); 712 mp->mnt_kern_flag &=~ MNTK_WANTRDWR; 713 if (error) { 714 mp->mnt_flag = flag; 715 mp->mnt_kern_flag = kern_flag; 716 } 717 if ((mp->mnt_flag & MNT_RDONLY) == 0) { 718 if (mp->mnt_syncer == NULL) 719 error = vfs_allocate_syncvnode(mp); 720 } else { 721 if (mp->mnt_syncer != NULL) 722 vrele(mp->mnt_syncer); 723 mp->mnt_syncer = NULL; 724 } 725 vfs_unbusy(mp, td); 726 VI_LOCK(vp); 727 vp->v_iflag &= ~VI_MOUNT; 728 VI_UNLOCK(vp); 729 vrele(vp); 730 return (error); 731 } 732 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 733 /* 734 * Put the new filesystem on the mount list after root. 735 */ 736 cache_purge(vp); 737 if (!error) { 738 struct vnode *newdp; 739 740 VI_LOCK(vp); 741 vp->v_iflag &= ~VI_MOUNT; 742 vp->v_mountedhere = mp; 743 VI_UNLOCK(vp); 744 mtx_lock(&mountlist_mtx); 745 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list); 746 mtx_unlock(&mountlist_mtx); 747 if (VFS_ROOT(mp, &newdp)) 748 panic("mount: lost mount"); 749 checkdirs(vp, newdp); 750 vput(newdp); 751 VOP_UNLOCK(vp, 0, td); 752 if ((mp->mnt_flag & MNT_RDONLY) == 0) 753 error = vfs_allocate_syncvnode(mp); 754 vfs_unbusy(mp, td); 755 if ((error = VFS_START(mp, 0, td)) != 0) { 756 vrele(vp); 757 goto bad; 758 } 759 } else { 760 VI_LOCK(vp); 761 vp->v_iflag &= ~VI_MOUNT; 762 VI_UNLOCK(vp); 763 mp->mnt_vfc->vfc_refcount--; 764 vfs_unbusy(mp, td); 765 #ifdef MAC 766 mac_destroy_mount(mp); 767 #endif 768 free(mp, M_MOUNT); 769 vput(vp); 770 goto bad; 771 } 772 return (0); 773 bad: 774 vfs_freeopts(optlist); 775 return (error); 776 } 777 778 /* 779 * Old mount API. 780 */ 781 #ifndef _SYS_SYSPROTO_H_ 782 struct mount_args { 783 char *type; 784 char *path; 785 int flags; 786 caddr_t data; 787 }; 788 #endif 789 /* ARGSUSED */ 790 int 791 mount(td, uap) 792 struct thread *td; 793 struct mount_args /* { 794 syscallarg(char *) type; 795 syscallarg(char *) path; 796 syscallarg(int) flags; 797 syscallarg(caddr_t) data; 798 } */ *uap; 799 { 800 char *fstype; 801 char *fspath; 802 int error; 803 804 fstype = malloc(MFSNAMELEN, M_TEMP, M_WAITOK); 805 fspath = malloc(MNAMELEN, M_TEMP, M_WAITOK); 806 807 /* 808 * vfs_mount() actually takes a kernel string for `type' and 809 * `path' now, so extract them. 810 */ 811 error = copyinstr(SCARG(uap, type), fstype, MFSNAMELEN, NULL); 812 if (error) 813 goto finish; 814 error = copyinstr(SCARG(uap, path), fspath, MNAMELEN, NULL); 815 if (error) 816 goto finish; 817 error = vfs_mount(td, fstype, fspath, SCARG(uap, flags), 818 SCARG(uap, data)); 819 finish: 820 free(fstype, M_TEMP); 821 free(fspath, M_TEMP); 822 return (error); 823 } 824 825 /* 826 * vfs_mount(): actually attempt a filesystem mount. 827 * 828 * This routine is designed to be a "generic" entry point for routines 829 * that wish to mount a filesystem. All parameters except `fsdata' are 830 * pointers into kernel space. `fsdata' is currently still a pointer 831 * into userspace. 832 */ 833 int 834 vfs_mount(td, fstype, fspath, fsflags, fsdata) 835 struct thread *td; 836 const char *fstype; 837 char *fspath; 838 int fsflags; 839 void *fsdata; 840 { 841 linker_file_t lf; 842 struct vnode *vp; 843 struct mount *mp; 844 struct vfsconf *vfsp; 845 int error, flag = 0, kern_flag = 0; 846 struct vattr va; 847 struct nameidata nd; 848 849 /* 850 * Be ultra-paranoid about making sure the type and fspath 851 * variables will fit in our mp buffers, including the 852 * terminating NUL. 853 */ 854 if (strlen(fstype) >= MFSNAMELEN || strlen(fspath) >= MNAMELEN) 855 return (ENAMETOOLONG); 856 857 if (usermount == 0) { 858 error = suser(td); 859 if (error) 860 return (error); 861 } 862 /* 863 * Do not allow NFS export by non-root users. 864 */ 865 if (fsflags & MNT_EXPORTED) { 866 error = suser(td); 867 if (error) 868 return (error); 869 } 870 /* 871 * Silently enforce MNT_NOSUID and MNT_NODEV for non-root users. 872 */ 873 if (suser(td)) 874 fsflags |= MNT_NOSUID | MNT_NODEV; 875 /* 876 * Get vnode to be covered 877 */ 878 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_SYSSPACE, fspath, td); 879 if ((error = namei(&nd)) != 0) 880 return (error); 881 NDFREE(&nd, NDF_ONLY_PNBUF); 882 vp = nd.ni_vp; 883 if (fsflags & MNT_UPDATE) { 884 if ((vp->v_vflag & VV_ROOT) == 0) { 885 vput(vp); 886 return (EINVAL); 887 } 888 mp = vp->v_mount; 889 flag = mp->mnt_flag; 890 kern_flag = mp->mnt_kern_flag; 891 /* 892 * We only allow the filesystem to be reloaded if it 893 * is currently mounted read-only. 894 */ 895 if ((fsflags & MNT_RELOAD) && 896 ((mp->mnt_flag & MNT_RDONLY) == 0)) { 897 vput(vp); 898 return (EOPNOTSUPP); /* Needs translation */ 899 } 900 /* 901 * Only root, or the user that did the original mount is 902 * permitted to update it. 903 */ 904 if (mp->mnt_stat.f_owner != td->td_ucred->cr_uid) { 905 error = suser(td); 906 if (error) { 907 vput(vp); 908 return (error); 909 } 910 } 911 if (vfs_busy(mp, LK_NOWAIT, 0, td)) { 912 vput(vp); 913 return (EBUSY); 914 } 915 VI_LOCK(vp); 916 if ((vp->v_iflag & VI_MOUNT) != 0 || 917 vp->v_mountedhere != NULL) { 918 VI_UNLOCK(vp); 919 vfs_unbusy(mp, td); 920 vput(vp); 921 return (EBUSY); 922 } 923 vp->v_iflag |= VI_MOUNT; 924 VI_UNLOCK(vp); 925 mp->mnt_flag |= fsflags & 926 (MNT_RELOAD | MNT_FORCE | MNT_UPDATE | MNT_SNAPSHOT); 927 VOP_UNLOCK(vp, 0, td); 928 goto update; 929 } 930 /* 931 * If the user is not root, ensure that they own the directory 932 * onto which we are attempting to mount. 933 */ 934 error = VOP_GETATTR(vp, &va, td->td_ucred, td); 935 if (error) { 936 vput(vp); 937 return (error); 938 } 939 if (va.va_uid != td->td_ucred->cr_uid) { 940 error = suser(td); 941 if (error) { 942 vput(vp); 943 return (error); 944 } 945 } 946 if ((error = vinvalbuf(vp, V_SAVE, td->td_ucred, td, 0, 0)) != 0) { 947 vput(vp); 948 return (error); 949 } 950 if (vp->v_type != VDIR) { 951 vput(vp); 952 return (ENOTDIR); 953 } 954 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) 955 if (!strcmp(vfsp->vfc_name, fstype)) 956 break; 957 if (vfsp == NULL) { 958 /* Only load modules for root (very important!). */ 959 error = suser(td); 960 if (error) { 961 vput(vp); 962 return (error); 963 } 964 error = securelevel_gt(td->td_ucred, 0); 965 if (error) { 966 vput(vp); 967 return (error); 968 } 969 error = linker_load_module(NULL, fstype, NULL, NULL, &lf); 970 if (error || lf == NULL) { 971 vput(vp); 972 if (lf == NULL) 973 error = ENODEV; 974 return (error); 975 } 976 lf->userrefs++; 977 /* Look up again to see if the VFS was loaded. */ 978 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) 979 if (!strcmp(vfsp->vfc_name, fstype)) 980 break; 981 if (vfsp == NULL) { 982 lf->userrefs--; 983 linker_file_unload(lf); 984 vput(vp); 985 return (ENODEV); 986 } 987 } 988 VI_LOCK(vp); 989 if ((vp->v_iflag & VI_MOUNT) != 0 || 990 vp->v_mountedhere != NULL) { 991 VI_UNLOCK(vp); 992 vput(vp); 993 return (EBUSY); 994 } 995 vp->v_iflag |= VI_MOUNT; 996 VI_UNLOCK(vp); 997 998 /* 999 * Allocate and initialize the filesystem. 1000 */ 1001 mp = malloc(sizeof(struct mount), M_MOUNT, M_WAITOK | M_ZERO); 1002 TAILQ_INIT(&mp->mnt_nvnodelist); 1003 TAILQ_INIT(&mp->mnt_reservedvnlist); 1004 lockinit(&mp->mnt_lock, PVFS, "vfslock", 0, LK_NOPAUSE); 1005 (void)vfs_busy(mp, LK_NOWAIT, 0, td); 1006 mp->mnt_op = vfsp->vfc_vfsops; 1007 mp->mnt_vfc = vfsp; 1008 vfsp->vfc_refcount++; 1009 mp->mnt_stat.f_type = vfsp->vfc_typenum; 1010 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK; 1011 strncpy(mp->mnt_stat.f_fstypename, fstype, MFSNAMELEN); 1012 mp->mnt_vnodecovered = vp; 1013 mp->mnt_stat.f_owner = td->td_ucred->cr_uid; 1014 strncpy(mp->mnt_stat.f_mntonname, fspath, MNAMELEN); 1015 mp->mnt_iosize_max = DFLTPHYS; 1016 #ifdef MAC 1017 mac_init_mount(mp); 1018 mac_create_mount(td->td_ucred, mp); 1019 #endif 1020 VOP_UNLOCK(vp, 0, td); 1021 update: 1022 /* 1023 * Check if the fs implements the old VFS_MOUNT() 1024 * function, since the old system call was used. 1025 */ 1026 if (mp->mnt_op->vfs_mount == NULL) { 1027 printf("%s doesn't support the old mount syscall\n", 1028 mp->mnt_vfc->vfc_name); 1029 VI_LOCK(vp); 1030 vp->v_iflag &= ~VI_MOUNT; 1031 VI_UNLOCK(vp); 1032 if (mp->mnt_flag & MNT_UPDATE) 1033 vfs_unbusy(mp, td); 1034 else { 1035 mp->mnt_vfc->vfc_refcount--; 1036 vfs_unbusy(mp, td); 1037 #ifdef MAC 1038 mac_destroy_mount(mp); 1039 #endif 1040 free(mp, M_MOUNT); 1041 } 1042 vrele(vp); 1043 return (EOPNOTSUPP); 1044 } 1045 1046 /* 1047 * Set the mount level flags. 1048 */ 1049 if (fsflags & MNT_RDONLY) 1050 mp->mnt_flag |= MNT_RDONLY; 1051 else if (mp->mnt_flag & MNT_RDONLY) 1052 mp->mnt_kern_flag |= MNTK_WANTRDWR; 1053 mp->mnt_flag &=~ MNT_UPDATEMASK; 1054 mp->mnt_flag |= fsflags & (MNT_UPDATEMASK | MNT_FORCE); 1055 /* 1056 * Mount the filesystem. 1057 * XXX The final recipients of VFS_MOUNT just overwrite the ndp they 1058 * get. No freeing of cn_pnbuf. 1059 */ 1060 error = VFS_MOUNT(mp, fspath, fsdata, &nd, td); 1061 if (mp->mnt_flag & MNT_UPDATE) { 1062 if (mp->mnt_kern_flag & MNTK_WANTRDWR) 1063 mp->mnt_flag &= ~MNT_RDONLY; 1064 mp->mnt_flag &=~ 1065 (MNT_UPDATE | MNT_RELOAD | MNT_FORCE | MNT_SNAPSHOT); 1066 mp->mnt_kern_flag &=~ MNTK_WANTRDWR; 1067 if (error) { 1068 mp->mnt_flag = flag; 1069 mp->mnt_kern_flag = kern_flag; 1070 } 1071 if ((mp->mnt_flag & MNT_RDONLY) == 0) { 1072 if (mp->mnt_syncer == NULL) 1073 error = vfs_allocate_syncvnode(mp); 1074 } else { 1075 if (mp->mnt_syncer != NULL) 1076 vrele(mp->mnt_syncer); 1077 mp->mnt_syncer = NULL; 1078 } 1079 vfs_unbusy(mp, td); 1080 VI_LOCK(vp); 1081 vp->v_iflag &= ~VI_MOUNT; 1082 VI_UNLOCK(vp); 1083 vrele(vp); 1084 return (error); 1085 } 1086 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, td); 1087 /* 1088 * Put the new filesystem on the mount list after root. 1089 */ 1090 cache_purge(vp); 1091 if (!error) { 1092 struct vnode *newdp; 1093 1094 mp_fixme("Does interlock protect mounted here or not?"); 1095 VI_LOCK(vp); 1096 vp->v_iflag &= ~VI_MOUNT; 1097 vp->v_mountedhere = mp; 1098 VI_UNLOCK(vp); 1099 mtx_lock(&mountlist_mtx); 1100 TAILQ_INSERT_TAIL(&mountlist, mp, mnt_list); 1101 mtx_unlock(&mountlist_mtx); 1102 if (VFS_ROOT(mp, &newdp)) 1103 panic("mount: lost mount"); 1104 checkdirs(vp, newdp); 1105 vput(newdp); 1106 VOP_UNLOCK(vp, 0, td); 1107 if ((mp->mnt_flag & MNT_RDONLY) == 0) 1108 error = vfs_allocate_syncvnode(mp); 1109 vfs_unbusy(mp, td); 1110 if ((error = VFS_START(mp, 0, td)) != 0) 1111 vrele(vp); 1112 } else { 1113 VI_LOCK(vp); 1114 vp->v_iflag &= ~VI_MOUNT; 1115 VI_UNLOCK(vp); 1116 mp->mnt_vfc->vfc_refcount--; 1117 vfs_unbusy(mp, td); 1118 #ifdef MAC 1119 mac_destroy_mount(mp); 1120 #endif 1121 free(mp, M_MOUNT); 1122 vput(vp); 1123 } 1124 return (error); 1125 } 1126 1127 /* 1128 * Scan all active processes to see if any of them have a current 1129 * or root directory of `olddp'. If so, replace them with the new 1130 * mount point. 1131 */ 1132 static void 1133 checkdirs(olddp, newdp) 1134 struct vnode *olddp, *newdp; 1135 { 1136 struct filedesc *fdp; 1137 struct proc *p; 1138 int nrele; 1139 1140 if (olddp->v_usecount == 1) 1141 return; 1142 sx_slock(&allproc_lock); 1143 LIST_FOREACH(p, &allproc, p_list) { 1144 PROC_LOCK(p); 1145 fdp = p->p_fd; 1146 if (fdp == NULL) { 1147 PROC_UNLOCK(p); 1148 continue; 1149 } 1150 nrele = 0; 1151 FILEDESC_LOCK(fdp); 1152 if (fdp->fd_cdir == olddp) { 1153 VREF(newdp); 1154 fdp->fd_cdir = newdp; 1155 nrele++; 1156 } 1157 if (fdp->fd_rdir == olddp) { 1158 VREF(newdp); 1159 fdp->fd_rdir = newdp; 1160 nrele++; 1161 } 1162 FILEDESC_UNLOCK(fdp); 1163 PROC_UNLOCK(p); 1164 while (nrele--) 1165 vrele(olddp); 1166 } 1167 sx_sunlock(&allproc_lock); 1168 if (rootvnode == olddp) { 1169 vrele(rootvnode); 1170 VREF(newdp); 1171 rootvnode = newdp; 1172 } 1173 } 1174 1175 /* 1176 * Unmount a filesystem. 1177 * 1178 * Note: unmount takes a path to the vnode mounted on as argument, 1179 * not special file (as before). 1180 */ 1181 #ifndef _SYS_SYSPROTO_H_ 1182 struct unmount_args { 1183 char *path; 1184 int flags; 1185 }; 1186 #endif 1187 /* ARGSUSED */ 1188 int 1189 unmount(td, uap) 1190 struct thread *td; 1191 register struct unmount_args /* { 1192 syscallarg(char *) path; 1193 syscallarg(int) flags; 1194 } */ *uap; 1195 { 1196 register struct vnode *vp; 1197 struct mount *mp; 1198 int error; 1199 struct nameidata nd; 1200 1201 NDINIT(&nd, LOOKUP, FOLLOW | LOCKLEAF, UIO_USERSPACE, 1202 SCARG(uap, path), td); 1203 if ((error = namei(&nd)) != 0) 1204 return (error); 1205 vp = nd.ni_vp; 1206 NDFREE(&nd, NDF_ONLY_PNBUF); 1207 mp = vp->v_mount; 1208 1209 /* 1210 * Only root, or the user that did the original mount is 1211 * permitted to unmount this filesystem. 1212 */ 1213 if (mp->mnt_stat.f_owner != td->td_ucred->cr_uid) { 1214 error = suser(td); 1215 if (error) { 1216 vput(vp); 1217 return (error); 1218 } 1219 } 1220 1221 /* 1222 * Don't allow unmounting the root filesystem. 1223 */ 1224 if (mp->mnt_flag & MNT_ROOTFS) { 1225 vput(vp); 1226 return (EINVAL); 1227 } 1228 1229 /* 1230 * Must be the root of the filesystem 1231 */ 1232 if ((vp->v_vflag & VV_ROOT) == 0) { 1233 vput(vp); 1234 return (EINVAL); 1235 } 1236 vput(vp); 1237 return (dounmount(mp, SCARG(uap, flags), td)); 1238 } 1239 1240 /* 1241 * Do the actual filesystem unmount. 1242 */ 1243 int 1244 dounmount(mp, flags, td) 1245 struct mount *mp; 1246 int flags; 1247 struct thread *td; 1248 { 1249 struct vnode *coveredvp, *fsrootvp; 1250 int error; 1251 int async_flag; 1252 1253 mtx_lock(&mountlist_mtx); 1254 if (mp->mnt_kern_flag & MNTK_UNMOUNT) { 1255 mtx_unlock(&mountlist_mtx); 1256 return (EBUSY); 1257 } 1258 mp->mnt_kern_flag |= MNTK_UNMOUNT; 1259 /* Allow filesystems to detect that a forced unmount is in progress. */ 1260 if (flags & MNT_FORCE) 1261 mp->mnt_kern_flag |= MNTK_UNMOUNTF; 1262 error = lockmgr(&mp->mnt_lock, LK_DRAIN | LK_INTERLOCK | 1263 ((flags & MNT_FORCE) ? 0 : LK_NOWAIT), &mountlist_mtx, td); 1264 if (error) { 1265 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF); 1266 if (mp->mnt_kern_flag & MNTK_MWAIT) 1267 wakeup(mp); 1268 return (error); 1269 } 1270 vn_start_write(NULL, &mp, V_WAIT); 1271 1272 if (mp->mnt_flag & MNT_EXPUBLIC) 1273 vfs_setpublicfs(NULL, NULL, NULL); 1274 1275 vfs_msync(mp, MNT_WAIT); 1276 async_flag = mp->mnt_flag & MNT_ASYNC; 1277 mp->mnt_flag &=~ MNT_ASYNC; 1278 cache_purgevfs(mp); /* remove cache entries for this file sys */ 1279 if (mp->mnt_syncer != NULL) 1280 vrele(mp->mnt_syncer); 1281 /* Move process cdir/rdir refs on fs root to underlying vnode. */ 1282 if (VFS_ROOT(mp, &fsrootvp) == 0) { 1283 if (mp->mnt_vnodecovered != NULL) 1284 checkdirs(fsrootvp, mp->mnt_vnodecovered); 1285 if (fsrootvp == rootvnode) { 1286 vrele(rootvnode); 1287 rootvnode = NULL; 1288 } 1289 vput(fsrootvp); 1290 } 1291 if (((mp->mnt_flag & MNT_RDONLY) || 1292 (error = VFS_SYNC(mp, MNT_WAIT, td->td_ucred, td)) == 0) || 1293 (flags & MNT_FORCE)) { 1294 error = VFS_UNMOUNT(mp, flags, td); 1295 } 1296 vn_finished_write(mp); 1297 if (error) { 1298 /* Undo cdir/rdir and rootvnode changes made above. */ 1299 if (VFS_ROOT(mp, &fsrootvp) == 0) { 1300 if (mp->mnt_vnodecovered != NULL) 1301 checkdirs(mp->mnt_vnodecovered, fsrootvp); 1302 if (rootvnode == NULL) { 1303 rootvnode = fsrootvp; 1304 vref(rootvnode); 1305 } 1306 vput(fsrootvp); 1307 } 1308 if ((mp->mnt_flag & MNT_RDONLY) == 0 && mp->mnt_syncer == NULL) 1309 (void) vfs_allocate_syncvnode(mp); 1310 mtx_lock(&mountlist_mtx); 1311 mp->mnt_kern_flag &= ~(MNTK_UNMOUNT | MNTK_UNMOUNTF); 1312 mp->mnt_flag |= async_flag; 1313 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK, 1314 &mountlist_mtx, td); 1315 if (mp->mnt_kern_flag & MNTK_MWAIT) 1316 wakeup(mp); 1317 return (error); 1318 } 1319 mtx_lock(&mountlist_mtx); 1320 TAILQ_REMOVE(&mountlist, mp, mnt_list); 1321 if ((coveredvp = mp->mnt_vnodecovered) != NULL) 1322 coveredvp->v_mountedhere = NULL; 1323 mp->mnt_vfc->vfc_refcount--; 1324 if (!TAILQ_EMPTY(&mp->mnt_nvnodelist)) 1325 panic("unmount: dangling vnode"); 1326 lockmgr(&mp->mnt_lock, LK_RELEASE | LK_INTERLOCK, &mountlist_mtx, td); 1327 lockdestroy(&mp->mnt_lock); 1328 if (coveredvp != NULL) 1329 vrele(coveredvp); 1330 if (mp->mnt_kern_flag & MNTK_MWAIT) 1331 wakeup(mp); 1332 #ifdef MAC 1333 mac_destroy_mount(mp); 1334 #endif 1335 if (mp->mnt_op->vfs_mount == NULL) 1336 vfs_freeopts(mp->mnt_opt); 1337 free(mp, M_MOUNT); 1338 return (0); 1339 } 1340 1341 /* 1342 * Lookup a filesystem type, and if found allocate and initialize 1343 * a mount structure for it. 1344 * 1345 * Devname is usually updated by mount(8) after booting. 1346 */ 1347 int 1348 vfs_rootmountalloc(fstypename, devname, mpp) 1349 char *fstypename; 1350 char *devname; 1351 struct mount **mpp; 1352 { 1353 struct thread *td = curthread; /* XXX */ 1354 struct vfsconf *vfsp; 1355 struct mount *mp; 1356 1357 if (fstypename == NULL) 1358 return (ENODEV); 1359 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) 1360 if (!strcmp(vfsp->vfc_name, fstypename)) 1361 break; 1362 if (vfsp == NULL) 1363 return (ENODEV); 1364 mp = malloc((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK | M_ZERO); 1365 lockinit(&mp->mnt_lock, PVFS, "vfslock", 0, LK_NOPAUSE); 1366 (void)vfs_busy(mp, LK_NOWAIT, 0, td); 1367 TAILQ_INIT(&mp->mnt_nvnodelist); 1368 TAILQ_INIT(&mp->mnt_reservedvnlist); 1369 mp->mnt_vfc = vfsp; 1370 mp->mnt_op = vfsp->vfc_vfsops; 1371 mp->mnt_flag = MNT_RDONLY; 1372 mp->mnt_vnodecovered = NULLVP; 1373 vfsp->vfc_refcount++; 1374 mp->mnt_iosize_max = DFLTPHYS; 1375 mp->mnt_stat.f_type = vfsp->vfc_typenum; 1376 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK; 1377 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN); 1378 mp->mnt_stat.f_mntonname[0] = '/'; 1379 mp->mnt_stat.f_mntonname[1] = 0; 1380 (void) copystr(devname, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, 0); 1381 #ifdef MAC 1382 mac_init_mount(mp); 1383 mac_create_mount(td->td_ucred, mp); 1384 #endif 1385 *mpp = mp; 1386 return (0); 1387 } 1388 1389 /* 1390 * Find and mount the root filesystem 1391 */ 1392 void 1393 vfs_mountroot(void) 1394 { 1395 char *cp; 1396 int i, error; 1397 1398 /* 1399 * The root filesystem information is compiled in, and we are 1400 * booted with instructions to use it. 1401 */ 1402 #ifdef ROOTDEVNAME 1403 if ((boothowto & RB_DFLTROOT) && 1404 !vfs_mountroot_try(ROOTDEVNAME)) 1405 return; 1406 #endif 1407 /* 1408 * We are booted with instructions to prompt for the root filesystem, 1409 * or to use the compiled-in default when it doesn't exist. 1410 */ 1411 if (boothowto & (RB_DFLTROOT | RB_ASKNAME)) { 1412 if (!vfs_mountroot_ask()) 1413 return; 1414 } 1415 1416 /* 1417 * We've been given the generic "use CDROM as root" flag. This is 1418 * necessary because one media may be used in many different 1419 * devices, so we need to search for them. 1420 */ 1421 if (boothowto & RB_CDROM) { 1422 for (i = 0; cdrom_rootdevnames[i] != NULL; i++) { 1423 if (!vfs_mountroot_try(cdrom_rootdevnames[i])) 1424 return; 1425 } 1426 } 1427 1428 /* 1429 * Try to use the value read by the loader from /etc/fstab, or 1430 * supplied via some other means. This is the preferred 1431 * mechanism. 1432 */ 1433 if ((cp = getenv("vfs.root.mountfrom")) != NULL) { 1434 error = vfs_mountroot_try(cp); 1435 freeenv(cp); 1436 if (!error) 1437 return; 1438 } 1439 1440 /* 1441 * Try values that may have been computed by the machine-dependant 1442 * legacy code. 1443 */ 1444 if (!vfs_mountroot_try(rootdevnames[0])) 1445 return; 1446 if (!vfs_mountroot_try(rootdevnames[1])) 1447 return; 1448 1449 /* 1450 * If we have a compiled-in default, and haven't already tried it, try 1451 * it now. 1452 */ 1453 #ifdef ROOTDEVNAME 1454 if (!(boothowto & RB_DFLTROOT)) 1455 if (!vfs_mountroot_try(ROOTDEVNAME)) 1456 return; 1457 #endif 1458 1459 /* 1460 * Everything so far has failed, prompt on the console if we haven't 1461 * already tried that. 1462 */ 1463 if (!(boothowto & (RB_DFLTROOT | RB_ASKNAME)) && !vfs_mountroot_ask()) 1464 return; 1465 panic("Root mount failed, startup aborted."); 1466 } 1467 1468 /* 1469 * Mount (mountfrom) as the root filesystem. 1470 */ 1471 static int 1472 vfs_mountroot_try(char *mountfrom) 1473 { 1474 struct mount *mp; 1475 char *vfsname, *path; 1476 int error; 1477 char patt[32]; 1478 int s; 1479 1480 vfsname = NULL; 1481 path = NULL; 1482 mp = NULL; 1483 error = EINVAL; 1484 1485 if (mountfrom == NULL) 1486 return(error); /* don't complain */ 1487 1488 s = splcam(); /* Overkill, but annoying without it */ 1489 printf("Mounting root from %s\n", mountfrom); 1490 splx(s); 1491 1492 /* parse vfs name and path */ 1493 vfsname = malloc(MFSNAMELEN, M_MOUNT, M_WAITOK); 1494 path = malloc(MNAMELEN, M_MOUNT, M_WAITOK); 1495 vfsname[0] = path[0] = 0; 1496 sprintf(patt, "%%%d[a-z0-9]:%%%ds", MFSNAMELEN, MNAMELEN); 1497 if (sscanf(mountfrom, patt, vfsname, path) < 1) 1498 goto done; 1499 1500 /* allocate a root mount */ 1501 error = vfs_rootmountalloc(vfsname, path[0] != 0 ? path : ROOTNAME, 1502 &mp); 1503 if (error != 0) { 1504 printf("Can't allocate root mount for filesystem '%s': %d\n", 1505 vfsname, error); 1506 goto done; 1507 } 1508 mp->mnt_flag |= MNT_ROOTFS; 1509 1510 /* do our best to set rootdev */ 1511 if ((path[0] != 0) && setrootbyname(path)) 1512 printf("setrootbyname failed\n"); 1513 1514 /* If the root device is a type "memory disk", mount RW */ 1515 if (rootdev != NODEV && devsw(rootdev) && 1516 (devsw(rootdev)->d_flags & D_MEMDISK)) 1517 mp->mnt_flag &= ~MNT_RDONLY; 1518 1519 /* 1520 * Set the mount path to be something useful, because the 1521 * filesystem code isn't responsible now for initialising 1522 * f_mntonname unless they want to override the default 1523 * (which is `path'.) 1524 */ 1525 strncpy(mp->mnt_stat.f_mntonname, "/", MNAMELEN); 1526 1527 error = VFS_MOUNT(mp, NULL, NULL, NULL, curthread); 1528 1529 done: 1530 if (vfsname != NULL) 1531 free(vfsname, M_MOUNT); 1532 if (path != NULL) 1533 free(path, M_MOUNT); 1534 if (error != 0) { 1535 if (mp != NULL) { 1536 vfs_unbusy(mp, curthread); 1537 #ifdef MAC 1538 mac_destroy_mount(mp); 1539 #endif 1540 free(mp, M_MOUNT); 1541 } 1542 printf("Root mount failed: %d\n", error); 1543 } else { 1544 1545 /* register with list of mounted filesystems */ 1546 mtx_lock(&mountlist_mtx); 1547 TAILQ_INSERT_HEAD(&mountlist, mp, mnt_list); 1548 mtx_unlock(&mountlist_mtx); 1549 1550 /* sanity check system clock against root fs timestamp */ 1551 inittodr(mp->mnt_time); 1552 vfs_unbusy(mp, curthread); 1553 error = VFS_START(mp, 0, curthread); 1554 } 1555 return(error); 1556 } 1557 1558 /* 1559 * Spin prompting on the console for a suitable root filesystem 1560 */ 1561 static int 1562 vfs_mountroot_ask(void) 1563 { 1564 char name[128]; 1565 int i; 1566 dev_t dev; 1567 1568 for(;;) { 1569 printf("\nManual root filesystem specification:\n"); 1570 printf(" <fstype>:<device> Mount <device> using filesystem <fstype>\n"); 1571 #if defined(__i386__) || defined(__ia64__) 1572 printf(" eg. ufs:da0s1a\n"); 1573 #else 1574 printf(" eg. ufs:da0a\n"); 1575 #endif 1576 printf(" ? List valid disk boot devices\n"); 1577 printf(" <empty line> Abort manual input\n"); 1578 printf("\nmountroot> "); 1579 gets(name); 1580 if (name[0] == 0) 1581 return(1); 1582 if (name[0] == '?') { 1583 printf("Possibly valid devices for 'ufs' root:\n"); 1584 for (i = 0; i < NUMCDEVSW; i++) { 1585 dev = makedev(i, 0); 1586 if (devsw(dev) != NULL) 1587 printf(" \"%s\"", devsw(dev)->d_name); 1588 } 1589 printf("\n"); 1590 continue; 1591 } 1592 if (!vfs_mountroot_try(name)) 1593 return(0); 1594 } 1595 } 1596 1597 /* 1598 * Local helper function for vfs_mountroot_ask. 1599 */ 1600 static void 1601 gets(char *cp) 1602 { 1603 char *lp; 1604 int c; 1605 1606 lp = cp; 1607 for (;;) { 1608 printf("%c", c = cngetc() & 0177); 1609 switch (c) { 1610 case -1: 1611 case '\n': 1612 case '\r': 1613 *lp++ = '\0'; 1614 return; 1615 case '\b': 1616 case '\177': 1617 if (lp > cp) { 1618 printf(" \b"); 1619 lp--; 1620 } 1621 continue; 1622 case '#': 1623 lp--; 1624 if (lp < cp) 1625 lp = cp; 1626 continue; 1627 case '@': 1628 case 'u' & 037: 1629 lp = cp; 1630 printf("%c", '\n'); 1631 continue; 1632 default: 1633 *lp++ = c; 1634 } 1635 } 1636 } 1637 1638 /* 1639 * Convert a given name to the dev_t of the disk-like device 1640 * it refers to. 1641 */ 1642 dev_t 1643 getdiskbyname(char *name) { 1644 char *cp; 1645 dev_t dev; 1646 1647 cp = name; 1648 if (!bcmp(cp, "/dev/", 5)) 1649 cp += 5; 1650 1651 dev = NODEV; 1652 EVENTHANDLER_INVOKE(dev_clone, cp, strlen(cp), &dev); 1653 return (dev); 1654 } 1655 1656 /* 1657 * Set rootdev to match (name), given that we expect it to 1658 * refer to a disk-like device. 1659 */ 1660 static int 1661 setrootbyname(char *name) 1662 { 1663 dev_t diskdev; 1664 1665 diskdev = getdiskbyname(name); 1666 if (diskdev != NODEV) { 1667 rootdev = diskdev; 1668 return (0); 1669 } 1670 1671 return (1); 1672 } 1673 1674 /* Show the dev_t for a disk specified by name */ 1675 #ifdef DDB 1676 DB_SHOW_COMMAND(disk, db_getdiskbyname) 1677 { 1678 dev_t dev; 1679 1680 if (modif[0] == '\0') { 1681 db_error("usage: show disk/devicename"); 1682 return; 1683 } 1684 dev = getdiskbyname(modif); 1685 if (dev != NODEV) 1686 db_printf("dev_t = %p\n", dev); 1687 else 1688 db_printf("No disk device matched.\n"); 1689 } 1690 #endif 1691 1692 /* 1693 * Get a mount option by its name. 1694 * 1695 * Return 0 if the option was found, ENOENT otherwise. 1696 * If len is non-NULL it will be filled with the length 1697 * of the option. If buf is non-NULL, it will be filled 1698 * with the address of the option. 1699 */ 1700 int 1701 vfs_getopt(opts, name, buf, len) 1702 struct vfsoptlist *opts; 1703 const char *name; 1704 void **buf; 1705 int *len; 1706 { 1707 struct vfsopt *opt; 1708 1709 TAILQ_FOREACH(opt, opts, link) { 1710 if (strcmp(name, opt->name) == 0) { 1711 if (len != NULL) 1712 *len = opt->len; 1713 if (buf != NULL) 1714 *buf = opt->value; 1715 return (0); 1716 } 1717 } 1718 return (ENOENT); 1719 } 1720 1721 /* 1722 * Find and copy a mount option. 1723 * 1724 * The size of the buffer has to be specified 1725 * in len, if it is not the same length as the 1726 * mount option, EINVAL is returned. 1727 * Returns ENOENT if the option is not found. 1728 */ 1729 int 1730 vfs_copyopt(opts, name, dest, len) 1731 struct vfsoptlist *opts; 1732 const char *name; 1733 void *dest; 1734 int len; 1735 { 1736 struct vfsopt *opt; 1737 1738 TAILQ_FOREACH(opt, opts, link) { 1739 if (strcmp(name, opt->name) == 0) { 1740 if (len != opt->len) 1741 return (EINVAL); 1742 bcopy(opt->value, dest, opt->len); 1743 return (0); 1744 } 1745 } 1746 return (ENOENT); 1747 } 1748