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