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