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