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