vfs_export.c revision 67309
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 * @(#)vfs_subr.c 8.31 (Berkeley) 5/26/95 39 * $FreeBSD: head/sys/kern/vfs_export.c 67309 2000-10-19 07:53:59Z rwatson $ 40 */ 41 42/* 43 * External virtual filesystem routines 44 */ 45#include "opt_ddb.h" 46#include "opt_ffs.h" 47 48#include <sys/param.h> 49#include <sys/systm.h> 50#include <sys/bio.h> 51#include <sys/buf.h> 52#include <sys/conf.h> 53#include <sys/dirent.h> 54#include <sys/domain.h> 55#include <sys/eventhandler.h> 56#include <sys/fcntl.h> 57#include <sys/kernel.h> 58#include <sys/kthread.h> 59#include <sys/ktr.h> 60#include <sys/malloc.h> 61#include <sys/mount.h> 62#include <sys/namei.h> 63#include <sys/proc.h> 64#include <sys/reboot.h> 65#include <sys/socket.h> 66#include <sys/stat.h> 67#include <sys/sysctl.h> 68#include <sys/vmmeter.h> 69#include <sys/vnode.h> 70 71#include <machine/limits.h> 72#include <machine/mutex.h> 73 74#include <vm/vm.h> 75#include <vm/vm_object.h> 76#include <vm/vm_extern.h> 77#include <vm/pmap.h> 78#include <vm/vm_map.h> 79#include <vm/vm_page.h> 80#include <vm/vm_pager.h> 81#include <vm/vnode_pager.h> 82#include <vm/vm_zone.h> 83 84static MALLOC_DEFINE(M_NETADDR, "Export Host", "Export host address structure"); 85 86static void addalias __P((struct vnode *vp, dev_t nvp_rdev)); 87static void insmntque __P((struct vnode *vp, struct mount *mp)); 88static void vclean __P((struct vnode *vp, int flags, struct proc *p)); 89 90/* 91 * Number of vnodes in existence. Increased whenever getnewvnode() 92 * allocates a new vnode, never decreased. 93 */ 94static unsigned long numvnodes; 95SYSCTL_INT(_debug, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0, ""); 96 97/* 98 * Conversion tables for conversion from vnode types to inode formats 99 * and back. 100 */ 101enum vtype iftovt_tab[16] = { 102 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON, 103 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD, 104}; 105int vttoif_tab[9] = { 106 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK, 107 S_IFSOCK, S_IFIFO, S_IFMT, 108}; 109 110/* 111 * List of vnodes that are ready for recycling. 112 */ 113static TAILQ_HEAD(freelst, vnode) vnode_free_list; 114 115/* 116 * Minimum number of free vnodes. If there are fewer than this free vnodes, 117 * getnewvnode() will return a newly allocated vnode. 118 */ 119static u_long wantfreevnodes = 25; 120SYSCTL_INT(_debug, OID_AUTO, wantfreevnodes, CTLFLAG_RW, &wantfreevnodes, 0, ""); 121/* Number of vnodes in the free list. */ 122static u_long freevnodes = 0; 123SYSCTL_INT(_debug, OID_AUTO, freevnodes, CTLFLAG_RD, &freevnodes, 0, ""); 124 125/* 126 * Various variables used for debugging the new implementation of 127 * reassignbuf(). 128 * XXX these are probably of (very) limited utility now. 129 */ 130static int reassignbufcalls; 131SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW, &reassignbufcalls, 0, ""); 132static int reassignbufloops; 133SYSCTL_INT(_vfs, OID_AUTO, reassignbufloops, CTLFLAG_RW, &reassignbufloops, 0, ""); 134static int reassignbufsortgood; 135SYSCTL_INT(_vfs, OID_AUTO, reassignbufsortgood, CTLFLAG_RW, &reassignbufsortgood, 0, ""); 136static int reassignbufsortbad; 137SYSCTL_INT(_vfs, OID_AUTO, reassignbufsortbad, CTLFLAG_RW, &reassignbufsortbad, 0, ""); 138/* Set to 0 for old insertion-sort based reassignbuf, 1 for modern method. */ 139static int reassignbufmethod = 1; 140SYSCTL_INT(_vfs, OID_AUTO, reassignbufmethod, CTLFLAG_RW, &reassignbufmethod, 0, ""); 141 142#ifdef ENABLE_VFS_IOOPT 143/* See NOTES for a description of this setting. */ 144int vfs_ioopt = 0; 145SYSCTL_INT(_vfs, OID_AUTO, ioopt, CTLFLAG_RW, &vfs_ioopt, 0, ""); 146#endif 147 148/* List of mounted filesystems. */ 149struct mntlist mountlist = TAILQ_HEAD_INITIALIZER(mountlist); 150 151/* For any iteration/modification of mountlist */ 152struct mtx mountlist_mtx; 153 154/* For any iteration/modification of mnt_vnodelist */ 155struct simplelock mntvnode_slock; 156/* 157 * Cache for the mount type id assigned to NFS. This is used for 158 * special checks in nfs/nfs_nqlease.c and vm/vnode_pager.c. 159 */ 160int nfs_mount_type = -1; 161 162#ifndef NULL_SIMPLELOCKS 163/* To keep more than one thread at a time from running vfs_getnewfsid */ 164static struct simplelock mntid_slock; 165 166/* For any iteration/modification of vnode_free_list */ 167static struct simplelock vnode_free_list_slock; 168 169/* 170 * For any iteration/modification of dev->si_hlist (linked through 171 * v_specnext) 172 */ 173static struct simplelock spechash_slock; 174#endif 175 176/* Publicly exported FS */ 177struct nfs_public nfs_pub; 178 179/* Zone for allocation of new vnodes - used exclusively by getnewvnode() */ 180static vm_zone_t vnode_zone; 181 182/* Set to 1 to print out reclaim of active vnodes */ 183int prtactive = 0; 184 185/* 186 * The workitem queue. 187 * 188 * It is useful to delay writes of file data and filesystem metadata 189 * for tens of seconds so that quickly created and deleted files need 190 * not waste disk bandwidth being created and removed. To realize this, 191 * we append vnodes to a "workitem" queue. When running with a soft 192 * updates implementation, most pending metadata dependencies should 193 * not wait for more than a few seconds. Thus, mounted on block devices 194 * are delayed only about a half the time that file data is delayed. 195 * Similarly, directory updates are more critical, so are only delayed 196 * about a third the time that file data is delayed. Thus, there are 197 * SYNCER_MAXDELAY queues that are processed round-robin at a rate of 198 * one each second (driven off the filesystem syncer process). The 199 * syncer_delayno variable indicates the next queue that is to be processed. 200 * Items that need to be processed soon are placed in this queue: 201 * 202 * syncer_workitem_pending[syncer_delayno] 203 * 204 * A delay of fifteen seconds is done by placing the request fifteen 205 * entries later in the queue: 206 * 207 * syncer_workitem_pending[(syncer_delayno + 15) & syncer_mask] 208 * 209 */ 210static int syncer_delayno = 0; 211static long syncer_mask; 212LIST_HEAD(synclist, vnode); 213static struct synclist *syncer_workitem_pending; 214 215#define SYNCER_MAXDELAY 32 216static int syncer_maxdelay = SYNCER_MAXDELAY; /* maximum delay time */ 217time_t syncdelay = 30; /* max time to delay syncing data */ 218time_t filedelay = 30; /* time to delay syncing files */ 219SYSCTL_INT(_kern, OID_AUTO, filedelay, CTLFLAG_RW, &filedelay, 0, ""); 220time_t dirdelay = 29; /* time to delay syncing directories */ 221SYSCTL_INT(_kern, OID_AUTO, dirdelay, CTLFLAG_RW, &dirdelay, 0, ""); 222time_t metadelay = 28; /* time to delay syncing metadata */ 223SYSCTL_INT(_kern, OID_AUTO, metadelay, CTLFLAG_RW, &metadelay, 0, ""); 224static int rushjob; /* number of slots to run ASAP */ 225static int stat_rush_requests; /* number of times I/O speeded up */ 226SYSCTL_INT(_debug, OID_AUTO, rush_requests, CTLFLAG_RW, &stat_rush_requests, 0, ""); 227 228/* 229 * Number of vnodes we want to exist at any one time. This is mostly used 230 * to size hash tables in vnode-related code. It is normally not used in 231 * getnewvnode(), as wantfreevnodes is normally nonzero.) 232 * 233 * XXX desiredvnodes is historical cruft and should not exist. 234 */ 235int desiredvnodes; 236SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RW, 237 &desiredvnodes, 0, "Maximum number of vnodes"); 238 239static void vfs_free_addrlist __P((struct netexport *nep)); 240static int vfs_free_netcred __P((struct radix_node *rn, void *w)); 241static int vfs_hang_addrlist __P((struct mount *mp, struct netexport *nep, 242 struct export_args *argp)); 243 244/* 245 * Initialize the vnode management data structures. 246 */ 247void 248vntblinit() 249{ 250 251 desiredvnodes = maxproc + cnt.v_page_count / 4; 252 mtx_init(&mountlist_mtx, "mountlist", MTX_DEF); 253 simple_lock_init(&mntvnode_slock); 254 simple_lock_init(&mntid_slock); 255 simple_lock_init(&spechash_slock); 256 TAILQ_INIT(&vnode_free_list); 257 simple_lock_init(&vnode_free_list_slock); 258 vnode_zone = zinit("VNODE", sizeof (struct vnode), 0, 0, 5); 259 /* 260 * Initialize the filesystem syncer. 261 */ 262 syncer_workitem_pending = hashinit(syncer_maxdelay, M_VNODE, 263 &syncer_mask); 264 syncer_maxdelay = syncer_mask + 1; 265} 266 267/* 268 * Mark a mount point as busy. Used to synchronize access and to delay 269 * unmounting. Interlock is not released on failure. 270 */ 271int 272vfs_busy(mp, flags, interlkp, p) 273 struct mount *mp; 274 int flags; 275 struct mtx *interlkp; 276 struct proc *p; 277{ 278 int lkflags; 279 280 if (mp->mnt_kern_flag & MNTK_UNMOUNT) { 281 if (flags & LK_NOWAIT) 282 return (ENOENT); 283 mp->mnt_kern_flag |= MNTK_MWAIT; 284 if (interlkp) { 285 mtx_exit(interlkp, MTX_DEF); 286 } 287 /* 288 * Since all busy locks are shared except the exclusive 289 * lock granted when unmounting, the only place that a 290 * wakeup needs to be done is at the release of the 291 * exclusive lock at the end of dounmount. 292 */ 293 tsleep((caddr_t)mp, PVFS, "vfs_busy", 0); 294 if (interlkp) { 295 mtx_enter(interlkp, MTX_DEF); 296 } 297 return (ENOENT); 298 } 299 lkflags = LK_SHARED | LK_NOPAUSE; 300 if (interlkp) 301 lkflags |= LK_INTERLOCK; 302 if (lockmgr(&mp->mnt_lock, lkflags, interlkp, p)) 303 panic("vfs_busy: unexpected lock failure"); 304 return (0); 305} 306 307/* 308 * Free a busy filesystem. 309 */ 310void 311vfs_unbusy(mp, p) 312 struct mount *mp; 313 struct proc *p; 314{ 315 316 lockmgr(&mp->mnt_lock, LK_RELEASE, NULL, p); 317} 318 319/* 320 * Lookup a filesystem type, and if found allocate and initialize 321 * a mount structure for it. 322 * 323 * Devname is usually updated by mount(8) after booting. 324 */ 325int 326vfs_rootmountalloc(fstypename, devname, mpp) 327 char *fstypename; 328 char *devname; 329 struct mount **mpp; 330{ 331 struct proc *p = curproc; /* XXX */ 332 struct vfsconf *vfsp; 333 struct mount *mp; 334 335 if (fstypename == NULL) 336 return (ENODEV); 337 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) 338 if (!strcmp(vfsp->vfc_name, fstypename)) 339 break; 340 if (vfsp == NULL) 341 return (ENODEV); 342 mp = malloc((u_long)sizeof(struct mount), M_MOUNT, M_WAITOK); 343 bzero((char *)mp, (u_long)sizeof(struct mount)); 344 lockinit(&mp->mnt_lock, PVFS, "vfslock", 0, LK_NOPAUSE); 345 (void)vfs_busy(mp, LK_NOWAIT, 0, p); 346 LIST_INIT(&mp->mnt_vnodelist); 347 mp->mnt_vfc = vfsp; 348 mp->mnt_op = vfsp->vfc_vfsops; 349 mp->mnt_flag = MNT_RDONLY; 350 mp->mnt_vnodecovered = NULLVP; 351 vfsp->vfc_refcount++; 352 mp->mnt_iosize_max = DFLTPHYS; 353 mp->mnt_stat.f_type = vfsp->vfc_typenum; 354 mp->mnt_flag |= vfsp->vfc_flags & MNT_VISFLAGMASK; 355 strncpy(mp->mnt_stat.f_fstypename, vfsp->vfc_name, MFSNAMELEN); 356 mp->mnt_stat.f_mntonname[0] = '/'; 357 mp->mnt_stat.f_mntonname[1] = 0; 358 (void) copystr(devname, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, 0); 359 *mpp = mp; 360 return (0); 361} 362 363/* 364 * Find an appropriate filesystem to use for the root. If a filesystem 365 * has not been preselected, walk through the list of known filesystems 366 * trying those that have mountroot routines, and try them until one 367 * works or we have tried them all. 368 */ 369#ifdef notdef /* XXX JH */ 370int 371lite2_vfs_mountroot() 372{ 373 struct vfsconf *vfsp; 374 extern int (*lite2_mountroot) __P((void)); 375 int error; 376 377 if (lite2_mountroot != NULL) 378 return ((*lite2_mountroot)()); 379 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) { 380 if (vfsp->vfc_mountroot == NULL) 381 continue; 382 if ((error = (*vfsp->vfc_mountroot)()) == 0) 383 return (0); 384 printf("%s_mountroot failed: %d\n", vfsp->vfc_name, error); 385 } 386 return (ENODEV); 387} 388#endif 389 390/* 391 * Lookup a mount point by filesystem identifier. 392 */ 393struct mount * 394vfs_getvfs(fsid) 395 fsid_t *fsid; 396{ 397 register struct mount *mp; 398 399 mtx_enter(&mountlist_mtx, MTX_DEF); 400 TAILQ_FOREACH(mp, &mountlist, mnt_list) { 401 if (mp->mnt_stat.f_fsid.val[0] == fsid->val[0] && 402 mp->mnt_stat.f_fsid.val[1] == fsid->val[1]) { 403 mtx_exit(&mountlist_mtx, MTX_DEF); 404 return (mp); 405 } 406 } 407 mtx_exit(&mountlist_mtx, MTX_DEF); 408 return ((struct mount *) 0); 409} 410 411/* 412 * Get a new unique fsid. Try to make its val[0] unique, since this value 413 * will be used to create fake device numbers for stat(). Also try (but 414 * not so hard) make its val[0] unique mod 2^16, since some emulators only 415 * support 16-bit device numbers. We end up with unique val[0]'s for the 416 * first 2^16 calls and unique val[0]'s mod 2^16 for the first 2^8 calls. 417 * 418 * Keep in mind that several mounts may be running in parallel. Starting 419 * the search one past where the previous search terminated is both a 420 * micro-optimization and a defense against returning the same fsid to 421 * different mounts. 422 */ 423void 424vfs_getnewfsid(mp) 425 struct mount *mp; 426{ 427 static u_int16_t mntid_base; 428 fsid_t tfsid; 429 int mtype; 430 431 simple_lock(&mntid_slock); 432 mtype = mp->mnt_vfc->vfc_typenum; 433 tfsid.val[1] = mtype; 434 mtype = (mtype & 0xFF) << 24; 435 for (;;) { 436 tfsid.val[0] = makeudev(255, 437 mtype | ((mntid_base & 0xFF00) << 8) | (mntid_base & 0xFF)); 438 mntid_base++; 439 if (vfs_getvfs(&tfsid) == NULL) 440 break; 441 } 442 mp->mnt_stat.f_fsid.val[0] = tfsid.val[0]; 443 mp->mnt_stat.f_fsid.val[1] = tfsid.val[1]; 444 simple_unlock(&mntid_slock); 445} 446 447/* 448 * Knob to control the precision of file timestamps: 449 * 450 * 0 = seconds only; nanoseconds zeroed. 451 * 1 = seconds and nanoseconds, accurate within 1/HZ. 452 * 2 = seconds and nanoseconds, truncated to microseconds. 453 * >=3 = seconds and nanoseconds, maximum precision. 454 */ 455enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC }; 456 457static int timestamp_precision = TSP_SEC; 458SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW, 459 ×tamp_precision, 0, ""); 460 461/* 462 * Get a current timestamp. 463 */ 464void 465vfs_timestamp(tsp) 466 struct timespec *tsp; 467{ 468 struct timeval tv; 469 470 switch (timestamp_precision) { 471 case TSP_SEC: 472 tsp->tv_sec = time_second; 473 tsp->tv_nsec = 0; 474 break; 475 case TSP_HZ: 476 getnanotime(tsp); 477 break; 478 case TSP_USEC: 479 microtime(&tv); 480 TIMEVAL_TO_TIMESPEC(&tv, tsp); 481 break; 482 case TSP_NSEC: 483 default: 484 nanotime(tsp); 485 break; 486 } 487} 488 489/* 490 * Set vnode attributes to VNOVAL 491 */ 492void 493vattr_null(vap) 494 register struct vattr *vap; 495{ 496 497 vap->va_type = VNON; 498 vap->va_size = VNOVAL; 499 vap->va_bytes = VNOVAL; 500 vap->va_mode = VNOVAL; 501 vap->va_nlink = VNOVAL; 502 vap->va_uid = VNOVAL; 503 vap->va_gid = VNOVAL; 504 vap->va_fsid = VNOVAL; 505 vap->va_fileid = VNOVAL; 506 vap->va_blocksize = VNOVAL; 507 vap->va_rdev = VNOVAL; 508 vap->va_atime.tv_sec = VNOVAL; 509 vap->va_atime.tv_nsec = VNOVAL; 510 vap->va_mtime.tv_sec = VNOVAL; 511 vap->va_mtime.tv_nsec = VNOVAL; 512 vap->va_ctime.tv_sec = VNOVAL; 513 vap->va_ctime.tv_nsec = VNOVAL; 514 vap->va_flags = VNOVAL; 515 vap->va_gen = VNOVAL; 516 vap->va_vaflags = 0; 517} 518 519/* 520 * Routines having to do with the management of the vnode table. 521 */ 522 523/* 524 * Return the next vnode from the free list. 525 */ 526int 527getnewvnode(tag, mp, vops, vpp) 528 enum vtagtype tag; 529 struct mount *mp; 530 vop_t **vops; 531 struct vnode **vpp; 532{ 533 int s, count; 534 struct proc *p = curproc; /* XXX */ 535 struct vnode *vp = NULL; 536 struct mount *vnmp; 537 vm_object_t object; 538 539 /* 540 * We take the least recently used vnode from the freelist 541 * if we can get it and it has no cached pages, and no 542 * namecache entries are relative to it. 543 * Otherwise we allocate a new vnode 544 */ 545 546 s = splbio(); 547 simple_lock(&vnode_free_list_slock); 548 549 if (wantfreevnodes && freevnodes < wantfreevnodes) { 550 vp = NULL; 551 } else if (!wantfreevnodes && freevnodes <= desiredvnodes) { 552 /* 553 * XXX: this is only here to be backwards compatible 554 */ 555 vp = NULL; 556 } else for (count = 0; count < freevnodes; count++) { 557 vp = TAILQ_FIRST(&vnode_free_list); 558 if (vp == NULL || vp->v_usecount) 559 panic("getnewvnode: free vnode isn't"); 560 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist); 561 /* 562 * Don't recycle if active in the namecache or 563 * if it still has cached pages or we cannot get 564 * its interlock. 565 */ 566 if (LIST_FIRST(&vp->v_cache_src) != NULL || 567 (VOP_GETVOBJECT(vp, &object) == 0 && 568 (object->resident_page_count || object->ref_count)) || 569 !mtx_try_enter(&vp->v_interlock, MTX_DEF)) { 570 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist); 571 vp = NULL; 572 continue; 573 } 574 /* 575 * Skip over it if its filesystem is being suspended. 576 */ 577 if (vn_start_write(vp, &vnmp, V_NOWAIT) == 0) 578 break; 579 mtx_exit(&vp->v_interlock, MTX_DEF); 580 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist); 581 vp = NULL; 582 } 583 if (vp) { 584 vp->v_flag |= VDOOMED; 585 freevnodes--; 586 simple_unlock(&vnode_free_list_slock); 587 cache_purge(vp); 588 vp->v_lease = NULL; 589 if (vp->v_type != VBAD) { 590 vgonel(vp, p); 591 } else { 592 mtx_exit(&vp->v_interlock, MTX_DEF); 593 } 594 vn_finished_write(vnmp); 595 596#ifdef INVARIANTS 597 { 598 int s; 599 600 if (vp->v_data) 601 panic("cleaned vnode isn't"); 602 s = splbio(); 603 if (vp->v_numoutput) 604 panic("Clean vnode has pending I/O's"); 605 splx(s); 606 if (vp->v_writecount != 0) 607 panic("Non-zero write count"); 608 } 609#endif 610 vp->v_flag = 0; 611 vp->v_lastw = 0; 612 vp->v_lasta = 0; 613 vp->v_cstart = 0; 614 vp->v_clen = 0; 615 vp->v_socket = 0; 616 } else { 617 simple_unlock(&vnode_free_list_slock); 618 vp = (struct vnode *) zalloc(vnode_zone); 619 bzero((char *) vp, sizeof *vp); 620 mtx_init(&vp->v_interlock, "vnode interlock", MTX_DEF); 621 vp->v_dd = vp; 622 cache_purge(vp); 623 LIST_INIT(&vp->v_cache_src); 624 TAILQ_INIT(&vp->v_cache_dst); 625 numvnodes++; 626 } 627 628 TAILQ_INIT(&vp->v_cleanblkhd); 629 TAILQ_INIT(&vp->v_dirtyblkhd); 630 vp->v_type = VNON; 631 vp->v_tag = tag; 632 vp->v_op = vops; 633 lockinit(&vp->v_lock, PVFS, "vnlock", 0, LK_NOPAUSE); 634 insmntque(vp, mp); 635 *vpp = vp; 636 vp->v_usecount = 1; 637 vp->v_data = 0; 638 splx(s); 639 640 vfs_object_create(vp, p, p->p_ucred); 641 return (0); 642} 643 644/* 645 * Move a vnode from one mount queue to another. 646 */ 647static void 648insmntque(vp, mp) 649 register struct vnode *vp; 650 register struct mount *mp; 651{ 652 653 simple_lock(&mntvnode_slock); 654 /* 655 * Delete from old mount point vnode list, if on one. 656 */ 657 if (vp->v_mount != NULL) 658 LIST_REMOVE(vp, v_mntvnodes); 659 /* 660 * Insert into list of vnodes for the new mount point, if available. 661 */ 662 if ((vp->v_mount = mp) == NULL) { 663 simple_unlock(&mntvnode_slock); 664 return; 665 } 666 LIST_INSERT_HEAD(&mp->mnt_vnodelist, vp, v_mntvnodes); 667 simple_unlock(&mntvnode_slock); 668} 669 670/* 671 * Update outstanding I/O count and do wakeup if requested. 672 */ 673void 674vwakeup(bp) 675 register struct buf *bp; 676{ 677 register struct vnode *vp; 678 679 bp->b_flags &= ~B_WRITEINPROG; 680 if ((vp = bp->b_vp)) { 681 vp->v_numoutput--; 682 if (vp->v_numoutput < 0) 683 panic("vwakeup: neg numoutput"); 684 if ((vp->v_numoutput == 0) && (vp->v_flag & VBWAIT)) { 685 vp->v_flag &= ~VBWAIT; 686 wakeup((caddr_t) &vp->v_numoutput); 687 } 688 } 689} 690 691/* 692 * Flush out and invalidate all buffers associated with a vnode. 693 * Called with the underlying object locked. 694 */ 695int 696vinvalbuf(vp, flags, cred, p, slpflag, slptimeo) 697 register struct vnode *vp; 698 int flags; 699 struct ucred *cred; 700 struct proc *p; 701 int slpflag, slptimeo; 702{ 703 register struct buf *bp; 704 struct buf *nbp, *blist; 705 int s, error; 706 vm_object_t object; 707 708 if (flags & V_SAVE) { 709 s = splbio(); 710 while (vp->v_numoutput) { 711 vp->v_flag |= VBWAIT; 712 error = tsleep((caddr_t)&vp->v_numoutput, 713 slpflag | (PRIBIO + 1), "vinvlbuf", slptimeo); 714 if (error) { 715 splx(s); 716 return (error); 717 } 718 } 719 if (!TAILQ_EMPTY(&vp->v_dirtyblkhd)) { 720 splx(s); 721 if ((error = VOP_FSYNC(vp, cred, MNT_WAIT, p)) != 0) 722 return (error); 723 s = splbio(); 724 if (vp->v_numoutput > 0 || 725 !TAILQ_EMPTY(&vp->v_dirtyblkhd)) 726 panic("vinvalbuf: dirty bufs"); 727 } 728 splx(s); 729 } 730 s = splbio(); 731 for (;;) { 732 blist = TAILQ_FIRST(&vp->v_cleanblkhd); 733 if (!blist) 734 blist = TAILQ_FIRST(&vp->v_dirtyblkhd); 735 if (!blist) 736 break; 737 738 for (bp = blist; bp; bp = nbp) { 739 nbp = TAILQ_NEXT(bp, b_vnbufs); 740 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) { 741 error = BUF_TIMELOCK(bp, 742 LK_EXCLUSIVE | LK_SLEEPFAIL, 743 "vinvalbuf", slpflag, slptimeo); 744 if (error == ENOLCK) 745 break; 746 splx(s); 747 return (error); 748 } 749 /* 750 * XXX Since there are no node locks for NFS, I 751 * believe there is a slight chance that a delayed 752 * write will occur while sleeping just above, so 753 * check for it. Note that vfs_bio_awrite expects 754 * buffers to reside on a queue, while VOP_BWRITE and 755 * brelse do not. 756 */ 757 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) && 758 (flags & V_SAVE)) { 759 760 if (bp->b_vp == vp) { 761 if (bp->b_flags & B_CLUSTEROK) { 762 BUF_UNLOCK(bp); 763 vfs_bio_awrite(bp); 764 } else { 765 bremfree(bp); 766 bp->b_flags |= B_ASYNC; 767 BUF_WRITE(bp); 768 } 769 } else { 770 bremfree(bp); 771 (void) BUF_WRITE(bp); 772 } 773 break; 774 } 775 bremfree(bp); 776 bp->b_flags |= (B_INVAL | B_NOCACHE | B_RELBUF); 777 bp->b_flags &= ~B_ASYNC; 778 brelse(bp); 779 } 780 } 781 782 while (vp->v_numoutput > 0) { 783 vp->v_flag |= VBWAIT; 784 tsleep(&vp->v_numoutput, PVM, "vnvlbv", 0); 785 } 786 787 splx(s); 788 789 /* 790 * Destroy the copy in the VM cache, too. 791 */ 792 mtx_enter(&vp->v_interlock, MTX_DEF); 793 if (VOP_GETVOBJECT(vp, &object) == 0) { 794 vm_object_page_remove(object, 0, 0, 795 (flags & V_SAVE) ? TRUE : FALSE); 796 } 797 mtx_exit(&vp->v_interlock, MTX_DEF); 798 799 if (!TAILQ_EMPTY(&vp->v_dirtyblkhd) || !TAILQ_EMPTY(&vp->v_cleanblkhd)) 800 panic("vinvalbuf: flush failed"); 801 return (0); 802} 803 804/* 805 * Truncate a file's buffer and pages to a specified length. This 806 * is in lieu of the old vinvalbuf mechanism, which performed unneeded 807 * sync activity. 808 */ 809int 810vtruncbuf(vp, cred, p, length, blksize) 811 register struct vnode *vp; 812 struct ucred *cred; 813 struct proc *p; 814 off_t length; 815 int blksize; 816{ 817 register struct buf *bp; 818 struct buf *nbp; 819 int s, anyfreed; 820 int trunclbn; 821 822 /* 823 * Round up to the *next* lbn. 824 */ 825 trunclbn = (length + blksize - 1) / blksize; 826 827 s = splbio(); 828restart: 829 anyfreed = 1; 830 for (;anyfreed;) { 831 anyfreed = 0; 832 for (bp = TAILQ_FIRST(&vp->v_cleanblkhd); bp; bp = nbp) { 833 nbp = TAILQ_NEXT(bp, b_vnbufs); 834 if (bp->b_lblkno >= trunclbn) { 835 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) { 836 BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL); 837 goto restart; 838 } else { 839 bremfree(bp); 840 bp->b_flags |= (B_INVAL | B_RELBUF); 841 bp->b_flags &= ~B_ASYNC; 842 brelse(bp); 843 anyfreed = 1; 844 } 845 if (nbp && 846 (((nbp->b_xflags & BX_VNCLEAN) == 0) || 847 (nbp->b_vp != vp) || 848 (nbp->b_flags & B_DELWRI))) { 849 goto restart; 850 } 851 } 852 } 853 854 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) { 855 nbp = TAILQ_NEXT(bp, b_vnbufs); 856 if (bp->b_lblkno >= trunclbn) { 857 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) { 858 BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL); 859 goto restart; 860 } else { 861 bremfree(bp); 862 bp->b_flags |= (B_INVAL | B_RELBUF); 863 bp->b_flags &= ~B_ASYNC; 864 brelse(bp); 865 anyfreed = 1; 866 } 867 if (nbp && 868 (((nbp->b_xflags & BX_VNDIRTY) == 0) || 869 (nbp->b_vp != vp) || 870 (nbp->b_flags & B_DELWRI) == 0)) { 871 goto restart; 872 } 873 } 874 } 875 } 876 877 if (length > 0) { 878restartsync: 879 for (bp = TAILQ_FIRST(&vp->v_dirtyblkhd); bp; bp = nbp) { 880 nbp = TAILQ_NEXT(bp, b_vnbufs); 881 if ((bp->b_flags & B_DELWRI) && (bp->b_lblkno < 0)) { 882 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) { 883 BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL); 884 goto restart; 885 } else { 886 bremfree(bp); 887 if (bp->b_vp == vp) { 888 bp->b_flags |= B_ASYNC; 889 } else { 890 bp->b_flags &= ~B_ASYNC; 891 } 892 BUF_WRITE(bp); 893 } 894 goto restartsync; 895 } 896 897 } 898 } 899 900 while (vp->v_numoutput > 0) { 901 vp->v_flag |= VBWAIT; 902 tsleep(&vp->v_numoutput, PVM, "vbtrunc", 0); 903 } 904 905 splx(s); 906 907 vnode_pager_setsize(vp, length); 908 909 return (0); 910} 911 912/* 913 * Associate a buffer with a vnode. 914 */ 915void 916bgetvp(vp, bp) 917 register struct vnode *vp; 918 register struct buf *bp; 919{ 920 int s; 921 922 KASSERT(bp->b_vp == NULL, ("bgetvp: not free")); 923 924 vhold(vp); 925 bp->b_vp = vp; 926 bp->b_dev = vn_todev(vp); 927 /* 928 * Insert onto list for new vnode. 929 */ 930 s = splbio(); 931 bp->b_xflags |= BX_VNCLEAN; 932 bp->b_xflags &= ~BX_VNDIRTY; 933 TAILQ_INSERT_TAIL(&vp->v_cleanblkhd, bp, b_vnbufs); 934 splx(s); 935} 936 937/* 938 * Disassociate a buffer from a vnode. 939 */ 940void 941brelvp(bp) 942 register struct buf *bp; 943{ 944 struct vnode *vp; 945 struct buflists *listheadp; 946 int s; 947 948 KASSERT(bp->b_vp != NULL, ("brelvp: NULL")); 949 950 /* 951 * Delete from old vnode list, if on one. 952 */ 953 vp = bp->b_vp; 954 s = splbio(); 955 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) { 956 if (bp->b_xflags & BX_VNDIRTY) 957 listheadp = &vp->v_dirtyblkhd; 958 else 959 listheadp = &vp->v_cleanblkhd; 960 TAILQ_REMOVE(listheadp, bp, b_vnbufs); 961 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN); 962 } 963 if ((vp->v_flag & VONWORKLST) && TAILQ_EMPTY(&vp->v_dirtyblkhd)) { 964 vp->v_flag &= ~VONWORKLST; 965 LIST_REMOVE(vp, v_synclist); 966 } 967 splx(s); 968 bp->b_vp = (struct vnode *) 0; 969 vdrop(vp); 970} 971 972/* 973 * Add an item to the syncer work queue. 974 */ 975static void 976vn_syncer_add_to_worklist(struct vnode *vp, int delay) 977{ 978 int s, slot; 979 980 s = splbio(); 981 982 if (vp->v_flag & VONWORKLST) { 983 LIST_REMOVE(vp, v_synclist); 984 } 985 986 if (delay > syncer_maxdelay - 2) 987 delay = syncer_maxdelay - 2; 988 slot = (syncer_delayno + delay) & syncer_mask; 989 990 LIST_INSERT_HEAD(&syncer_workitem_pending[slot], vp, v_synclist); 991 vp->v_flag |= VONWORKLST; 992 splx(s); 993} 994 995struct proc *updateproc; 996static void sched_sync __P((void)); 997static struct kproc_desc up_kp = { 998 "syncer", 999 sched_sync, 1000 &updateproc 1001}; 1002SYSINIT(syncer, SI_SUB_KTHREAD_UPDATE, SI_ORDER_FIRST, kproc_start, &up_kp) 1003 1004/* 1005 * System filesystem synchronizer daemon. 1006 */ 1007void 1008sched_sync(void) 1009{ 1010 struct synclist *slp; 1011 struct vnode *vp; 1012 struct mount *mp; 1013 long starttime; 1014 int s; 1015 struct proc *p = updateproc; 1016 1017 mtx_enter(&Giant, MTX_DEF); 1018 1019 EVENTHANDLER_REGISTER(shutdown_pre_sync, shutdown_kproc, p, 1020 SHUTDOWN_PRI_LAST); 1021 1022 for (;;) { 1023 kproc_suspend_loop(p); 1024 1025 starttime = time_second; 1026 1027 /* 1028 * Push files whose dirty time has expired. Be careful 1029 * of interrupt race on slp queue. 1030 */ 1031 s = splbio(); 1032 slp = &syncer_workitem_pending[syncer_delayno]; 1033 syncer_delayno += 1; 1034 if (syncer_delayno == syncer_maxdelay) 1035 syncer_delayno = 0; 1036 splx(s); 1037 1038 while ((vp = LIST_FIRST(slp)) != NULL) { 1039 if (VOP_ISLOCKED(vp, NULL) == 0 && 1040 vn_start_write(vp, &mp, V_NOWAIT) == 0) { 1041 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY, p); 1042 (void) VOP_FSYNC(vp, p->p_ucred, MNT_LAZY, p); 1043 VOP_UNLOCK(vp, 0, p); 1044 vn_finished_write(mp); 1045 } 1046 s = splbio(); 1047 if (LIST_FIRST(slp) == vp) { 1048 /* 1049 * Note: v_tag VT_VFS vps can remain on the 1050 * worklist too with no dirty blocks, but 1051 * since sync_fsync() moves it to a different 1052 * slot we are safe. 1053 */ 1054 if (TAILQ_EMPTY(&vp->v_dirtyblkhd) && 1055 !vn_isdisk(vp, NULL)) 1056 panic("sched_sync: fsync failed vp %p tag %d", vp, vp->v_tag); 1057 /* 1058 * Put us back on the worklist. The worklist 1059 * routine will remove us from our current 1060 * position and then add us back in at a later 1061 * position. 1062 */ 1063 vn_syncer_add_to_worklist(vp, syncdelay); 1064 } 1065 splx(s); 1066 } 1067 1068 /* 1069 * Do soft update processing. 1070 */ 1071#ifdef SOFTUPDATES 1072 softdep_process_worklist(NULL); 1073#endif 1074 1075 /* 1076 * The variable rushjob allows the kernel to speed up the 1077 * processing of the filesystem syncer process. A rushjob 1078 * value of N tells the filesystem syncer to process the next 1079 * N seconds worth of work on its queue ASAP. Currently rushjob 1080 * is used by the soft update code to speed up the filesystem 1081 * syncer process when the incore state is getting so far 1082 * ahead of the disk that the kernel memory pool is being 1083 * threatened with exhaustion. 1084 */ 1085 if (rushjob > 0) { 1086 rushjob -= 1; 1087 continue; 1088 } 1089 /* 1090 * If it has taken us less than a second to process the 1091 * current work, then wait. Otherwise start right over 1092 * again. We can still lose time if any single round 1093 * takes more than two seconds, but it does not really 1094 * matter as we are just trying to generally pace the 1095 * filesystem activity. 1096 */ 1097 if (time_second == starttime) 1098 tsleep(&lbolt, PPAUSE, "syncer", 0); 1099 } 1100} 1101 1102/* 1103 * Request the syncer daemon to speed up its work. 1104 * We never push it to speed up more than half of its 1105 * normal turn time, otherwise it could take over the cpu. 1106 */ 1107int 1108speedup_syncer() 1109{ 1110 int s; 1111 1112 s = splhigh(); 1113 if (updateproc->p_wchan == &lbolt) 1114 setrunnable(updateproc); 1115 splx(s); 1116 if (rushjob < syncdelay / 2) { 1117 rushjob += 1; 1118 stat_rush_requests += 1; 1119 return (1); 1120 } 1121 return(0); 1122} 1123 1124/* 1125 * Associate a p-buffer with a vnode. 1126 * 1127 * Also sets B_PAGING flag to indicate that vnode is not fully associated 1128 * with the buffer. i.e. the bp has not been linked into the vnode or 1129 * ref-counted. 1130 */ 1131void 1132pbgetvp(vp, bp) 1133 register struct vnode *vp; 1134 register struct buf *bp; 1135{ 1136 1137 KASSERT(bp->b_vp == NULL, ("pbgetvp: not free")); 1138 1139 bp->b_vp = vp; 1140 bp->b_flags |= B_PAGING; 1141 bp->b_dev = vn_todev(vp); 1142} 1143 1144/* 1145 * Disassociate a p-buffer from a vnode. 1146 */ 1147void 1148pbrelvp(bp) 1149 register struct buf *bp; 1150{ 1151 1152 KASSERT(bp->b_vp != NULL, ("pbrelvp: NULL")); 1153 1154 /* XXX REMOVE ME */ 1155 if (bp->b_vnbufs.tqe_next != NULL) { 1156 panic( 1157 "relpbuf(): b_vp was probably reassignbuf()d %p %x", 1158 bp, 1159 (int)bp->b_flags 1160 ); 1161 } 1162 bp->b_vp = (struct vnode *) 0; 1163 bp->b_flags &= ~B_PAGING; 1164} 1165 1166/* 1167 * Change the vnode a pager buffer is associated with. 1168 */ 1169void 1170pbreassignbuf(bp, newvp) 1171 struct buf *bp; 1172 struct vnode *newvp; 1173{ 1174 1175 KASSERT(bp->b_flags & B_PAGING, 1176 ("pbreassignbuf() on non phys bp %p", bp)); 1177 bp->b_vp = newvp; 1178} 1179 1180/* 1181 * Reassign a buffer from one vnode to another. 1182 * Used to assign file specific control information 1183 * (indirect blocks) to the vnode to which they belong. 1184 */ 1185void 1186reassignbuf(bp, newvp) 1187 register struct buf *bp; 1188 register struct vnode *newvp; 1189{ 1190 struct buflists *listheadp; 1191 int delay; 1192 int s; 1193 1194 if (newvp == NULL) { 1195 printf("reassignbuf: NULL"); 1196 return; 1197 } 1198 ++reassignbufcalls; 1199 1200 /* 1201 * B_PAGING flagged buffers cannot be reassigned because their vp 1202 * is not fully linked in. 1203 */ 1204 if (bp->b_flags & B_PAGING) 1205 panic("cannot reassign paging buffer"); 1206 1207 s = splbio(); 1208 /* 1209 * Delete from old vnode list, if on one. 1210 */ 1211 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) { 1212 if (bp->b_xflags & BX_VNDIRTY) 1213 listheadp = &bp->b_vp->v_dirtyblkhd; 1214 else 1215 listheadp = &bp->b_vp->v_cleanblkhd; 1216 TAILQ_REMOVE(listheadp, bp, b_vnbufs); 1217 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN); 1218 if (bp->b_vp != newvp) { 1219 vdrop(bp->b_vp); 1220 bp->b_vp = NULL; /* for clarification */ 1221 } 1222 } 1223 /* 1224 * If dirty, put on list of dirty buffers; otherwise insert onto list 1225 * of clean buffers. 1226 */ 1227 if (bp->b_flags & B_DELWRI) { 1228 struct buf *tbp; 1229 1230 listheadp = &newvp->v_dirtyblkhd; 1231 if ((newvp->v_flag & VONWORKLST) == 0) { 1232 switch (newvp->v_type) { 1233 case VDIR: 1234 delay = dirdelay; 1235 break; 1236 case VCHR: 1237 case VBLK: 1238 if (newvp->v_rdev->si_mountpoint != NULL) { 1239 delay = metadelay; 1240 break; 1241 } 1242 /* fall through */ 1243 default: 1244 delay = filedelay; 1245 } 1246 vn_syncer_add_to_worklist(newvp, delay); 1247 } 1248 bp->b_xflags |= BX_VNDIRTY; 1249 tbp = TAILQ_FIRST(listheadp); 1250 if (tbp == NULL || 1251 bp->b_lblkno == 0 || 1252 (bp->b_lblkno > 0 && tbp->b_lblkno < 0) || 1253 (bp->b_lblkno > 0 && bp->b_lblkno < tbp->b_lblkno)) { 1254 TAILQ_INSERT_HEAD(listheadp, bp, b_vnbufs); 1255 ++reassignbufsortgood; 1256 } else if (bp->b_lblkno < 0) { 1257 TAILQ_INSERT_TAIL(listheadp, bp, b_vnbufs); 1258 ++reassignbufsortgood; 1259 } else if (reassignbufmethod == 1) { 1260 /* 1261 * New sorting algorithm, only handle sequential case, 1262 * otherwise append to end (but before metadata) 1263 */ 1264 if ((tbp = gbincore(newvp, bp->b_lblkno - 1)) != NULL && 1265 (tbp->b_xflags & BX_VNDIRTY)) { 1266 /* 1267 * Found the best place to insert the buffer 1268 */ 1269 TAILQ_INSERT_AFTER(listheadp, tbp, bp, b_vnbufs); 1270 ++reassignbufsortgood; 1271 } else { 1272 /* 1273 * Missed, append to end, but before meta-data. 1274 * We know that the head buffer in the list is 1275 * not meta-data due to prior conditionals. 1276 * 1277 * Indirect effects: NFS second stage write 1278 * tends to wind up here, giving maximum 1279 * distance between the unstable write and the 1280 * commit rpc. 1281 */ 1282 tbp = TAILQ_LAST(listheadp, buflists); 1283 while (tbp && tbp->b_lblkno < 0) 1284 tbp = TAILQ_PREV(tbp, buflists, b_vnbufs); 1285 TAILQ_INSERT_AFTER(listheadp, tbp, bp, b_vnbufs); 1286 ++reassignbufsortbad; 1287 } 1288 } else { 1289 /* 1290 * Old sorting algorithm, scan queue and insert 1291 */ 1292 struct buf *ttbp; 1293 while ((ttbp = TAILQ_NEXT(tbp, b_vnbufs)) && 1294 (ttbp->b_lblkno < bp->b_lblkno)) { 1295 ++reassignbufloops; 1296 tbp = ttbp; 1297 } 1298 TAILQ_INSERT_AFTER(listheadp, tbp, bp, b_vnbufs); 1299 } 1300 } else { 1301 bp->b_xflags |= BX_VNCLEAN; 1302 TAILQ_INSERT_TAIL(&newvp->v_cleanblkhd, bp, b_vnbufs); 1303 if ((newvp->v_flag & VONWORKLST) && 1304 TAILQ_EMPTY(&newvp->v_dirtyblkhd)) { 1305 newvp->v_flag &= ~VONWORKLST; 1306 LIST_REMOVE(newvp, v_synclist); 1307 } 1308 } 1309 if (bp->b_vp != newvp) { 1310 bp->b_vp = newvp; 1311 vhold(bp->b_vp); 1312 } 1313 splx(s); 1314} 1315 1316/* 1317 * Create a vnode for a block device. 1318 * Used for mounting the root file system. 1319 * XXX: This now changed to a VCHR due to the block/char merging. 1320 */ 1321int 1322bdevvp(dev, vpp) 1323 dev_t dev; 1324 struct vnode **vpp; 1325{ 1326 register struct vnode *vp; 1327 struct vnode *nvp; 1328 int error; 1329 1330 if (dev == NODEV) { 1331 *vpp = NULLVP; 1332 return (ENXIO); 1333 } 1334 if (vfinddev(dev, VCHR, vpp)) 1335 return (0); 1336 error = getnewvnode(VT_NON, (struct mount *)0, spec_vnodeop_p, &nvp); 1337 if (error) { 1338 *vpp = NULLVP; 1339 return (error); 1340 } 1341 vp = nvp; 1342 vp->v_type = VCHR; 1343 addalias(vp, dev); 1344 *vpp = vp; 1345 return (0); 1346} 1347 1348/* 1349 * Add vnode to the alias list hung off the dev_t. 1350 * 1351 * The reason for this gunk is that multiple vnodes can reference 1352 * the same physical device, so checking vp->v_usecount to see 1353 * how many users there are is inadequate; the v_usecount for 1354 * the vnodes need to be accumulated. vcount() does that. 1355 */ 1356struct vnode * 1357addaliasu(nvp, nvp_rdev) 1358 struct vnode *nvp; 1359 udev_t nvp_rdev; 1360{ 1361 struct vnode *ovp; 1362 vop_t **ops; 1363 dev_t dev; 1364 1365 if (nvp->v_type != VBLK && nvp->v_type != VCHR) 1366 panic("addaliasu on non-special vnode"); 1367 dev = udev2dev(nvp_rdev, nvp->v_type == VBLK ? 1 : 0); 1368 /* 1369 * Check to see if we have a bdevvp vnode with no associated 1370 * filesystem. If so, we want to associate the filesystem of 1371 * the new newly instigated vnode with the bdevvp vnode and 1372 * discard the newly created vnode rather than leaving the 1373 * bdevvp vnode lying around with no associated filesystem. 1374 */ 1375 if (vfinddev(dev, nvp->v_type, &ovp) == 0 || ovp->v_data != NULL) { 1376 addalias(nvp, dev); 1377 return (nvp); 1378 } 1379 /* 1380 * Discard unneeded vnode, but save its node specific data. 1381 * Note that if there is a lock, it is carried over in the 1382 * node specific data to the replacement vnode. 1383 */ 1384 vref(ovp); 1385 ovp->v_data = nvp->v_data; 1386 ovp->v_tag = nvp->v_tag; 1387 nvp->v_data = NULL; 1388 ops = nvp->v_op; 1389 nvp->v_op = ovp->v_op; 1390 ovp->v_op = ops; 1391 lockinit(&ovp->v_lock, PVFS, "vnlock", 0, LK_NOPAUSE); 1392 if (nvp->v_vnlock) 1393 ovp->v_vnlock = &ovp->v_lock; 1394 insmntque(ovp, nvp->v_mount); 1395 vrele(nvp); 1396 vgone(nvp); 1397 return (ovp); 1398} 1399 1400/* This is a local helper function that do the same as addaliasu, but for a 1401 * dev_t instead of an udev_t. */ 1402static void 1403addalias(nvp, dev) 1404 struct vnode *nvp; 1405 dev_t dev; 1406{ 1407 1408 KASSERT(nvp->v_type == VBLK || nvp->v_type == VCHR, 1409 ("addalias on non-special vnode")); 1410 nvp->v_rdev = dev; 1411 simple_lock(&spechash_slock); 1412 SLIST_INSERT_HEAD(&dev->si_hlist, nvp, v_specnext); 1413 simple_unlock(&spechash_slock); 1414} 1415 1416/* 1417 * Grab a particular vnode from the free list, increment its 1418 * reference count and lock it. The vnode lock bit is set if the 1419 * vnode is being eliminated in vgone. The process is awakened 1420 * when the transition is completed, and an error returned to 1421 * indicate that the vnode is no longer usable (possibly having 1422 * been changed to a new file system type). 1423 */ 1424int 1425vget(vp, flags, p) 1426 register struct vnode *vp; 1427 int flags; 1428 struct proc *p; 1429{ 1430 int error; 1431 1432 /* 1433 * If the vnode is in the process of being cleaned out for 1434 * another use, we wait for the cleaning to finish and then 1435 * return failure. Cleaning is determined by checking that 1436 * the VXLOCK flag is set. 1437 */ 1438 if ((flags & LK_INTERLOCK) == 0) 1439 mtx_enter(&vp->v_interlock, MTX_DEF); 1440 if (vp->v_flag & VXLOCK) { 1441 vp->v_flag |= VXWANT; 1442 mtx_exit(&vp->v_interlock, MTX_DEF); 1443 tsleep((caddr_t)vp, PINOD, "vget", 0); 1444 return (ENOENT); 1445 } 1446 1447 vp->v_usecount++; 1448 1449 if (VSHOULDBUSY(vp)) 1450 vbusy(vp); 1451 if (flags & LK_TYPE_MASK) { 1452 if ((error = vn_lock(vp, flags | LK_INTERLOCK, p)) != 0) { 1453 /* 1454 * must expand vrele here because we do not want 1455 * to call VOP_INACTIVE if the reference count 1456 * drops back to zero since it was never really 1457 * active. We must remove it from the free list 1458 * before sleeping so that multiple processes do 1459 * not try to recycle it. 1460 */ 1461 mtx_enter(&vp->v_interlock, MTX_DEF); 1462 vp->v_usecount--; 1463 if (VSHOULDFREE(vp)) 1464 vfree(vp); 1465 mtx_exit(&vp->v_interlock, MTX_DEF); 1466 } 1467 return (error); 1468 } 1469 mtx_exit(&vp->v_interlock, MTX_DEF); 1470 return (0); 1471} 1472 1473/* 1474 * Increase the reference count of a vnode. 1475 */ 1476void 1477vref(struct vnode *vp) 1478{ 1479 mtx_enter(&vp->v_interlock, MTX_DEF); 1480 vp->v_usecount++; 1481 mtx_exit(&vp->v_interlock, MTX_DEF); 1482} 1483 1484/* 1485 * Vnode put/release. 1486 * If count drops to zero, call inactive routine and return to freelist. 1487 */ 1488void 1489vrele(vp) 1490 struct vnode *vp; 1491{ 1492 struct proc *p = curproc; /* XXX */ 1493 1494 KASSERT(vp != NULL, ("vrele: null vp")); 1495 1496 mtx_enter(&vp->v_interlock, MTX_DEF); 1497 1498 KASSERT(vp->v_writecount < vp->v_usecount, ("vrele: missed vn_close")); 1499 1500 if (vp->v_usecount > 1) { 1501 1502 vp->v_usecount--; 1503 mtx_exit(&vp->v_interlock, MTX_DEF); 1504 1505 return; 1506 } 1507 1508 if (vp->v_usecount == 1) { 1509 1510 vp->v_usecount--; 1511 if (VSHOULDFREE(vp)) 1512 vfree(vp); 1513 /* 1514 * If we are doing a vput, the node is already locked, and we must 1515 * call VOP_INACTIVE with the node locked. So, in the case of 1516 * vrele, we explicitly lock the vnode before calling VOP_INACTIVE. 1517 */ 1518 if (vn_lock(vp, LK_EXCLUSIVE | LK_INTERLOCK, p) == 0) { 1519 VOP_INACTIVE(vp, p); 1520 } 1521 1522 } else { 1523#ifdef DIAGNOSTIC 1524 vprint("vrele: negative ref count", vp); 1525 mtx_exit(&vp->v_interlock, MTX_DEF); 1526#endif 1527 panic("vrele: negative ref cnt"); 1528 } 1529} 1530 1531/* 1532 * Release an already locked vnode. This give the same effects as 1533 * unlock+vrele(), but takes less time and avoids releasing and 1534 * re-aquiring the lock (as vrele() aquires the lock internally.) 1535 */ 1536void 1537vput(vp) 1538 struct vnode *vp; 1539{ 1540 struct proc *p = curproc; /* XXX */ 1541 1542 KASSERT(vp != NULL, ("vput: null vp")); 1543 mtx_enter(&vp->v_interlock, MTX_DEF); 1544 KASSERT(vp->v_writecount < vp->v_usecount, ("vput: missed vn_close")); 1545 1546 if (vp->v_usecount > 1) { 1547 1548 vp->v_usecount--; 1549 VOP_UNLOCK(vp, LK_INTERLOCK, p); 1550 return; 1551 1552 } 1553 1554 if (vp->v_usecount == 1) { 1555 1556 vp->v_usecount--; 1557 if (VSHOULDFREE(vp)) 1558 vfree(vp); 1559 /* 1560 * If we are doing a vput, the node is already locked, and we must 1561 * call VOP_INACTIVE with the node locked. So, in the case of 1562 * vrele, we explicitly lock the vnode before calling VOP_INACTIVE. 1563 */ 1564 mtx_exit(&vp->v_interlock, MTX_DEF); 1565 VOP_INACTIVE(vp, p); 1566 1567 } else { 1568#ifdef DIAGNOSTIC 1569 vprint("vput: negative ref count", vp); 1570#endif 1571 panic("vput: negative ref cnt"); 1572 } 1573} 1574 1575/* 1576 * Somebody doesn't want the vnode recycled. 1577 */ 1578void 1579vhold(vp) 1580 register struct vnode *vp; 1581{ 1582 int s; 1583 1584 s = splbio(); 1585 vp->v_holdcnt++; 1586 if (VSHOULDBUSY(vp)) 1587 vbusy(vp); 1588 splx(s); 1589} 1590 1591/* 1592 * Note that there is one less who cares about this vnode. vdrop() is the 1593 * opposite of vhold(). 1594 */ 1595void 1596vdrop(vp) 1597 register struct vnode *vp; 1598{ 1599 int s; 1600 1601 s = splbio(); 1602 if (vp->v_holdcnt <= 0) 1603 panic("vdrop: holdcnt"); 1604 vp->v_holdcnt--; 1605 if (VSHOULDFREE(vp)) 1606 vfree(vp); 1607 splx(s); 1608} 1609 1610/* 1611 * Remove any vnodes in the vnode table belonging to mount point mp. 1612 * 1613 * If MNT_NOFORCE is specified, there should not be any active ones, 1614 * return error if any are found (nb: this is a user error, not a 1615 * system error). If MNT_FORCE is specified, detach any active vnodes 1616 * that are found. 1617 */ 1618#ifdef DIAGNOSTIC 1619static int busyprt = 0; /* print out busy vnodes */ 1620SYSCTL_INT(_debug, OID_AUTO, busyprt, CTLFLAG_RW, &busyprt, 0, ""); 1621#endif 1622 1623int 1624vflush(mp, skipvp, flags) 1625 struct mount *mp; 1626 struct vnode *skipvp; 1627 int flags; 1628{ 1629 struct proc *p = curproc; /* XXX */ 1630 struct vnode *vp, *nvp; 1631 int busy = 0; 1632 1633 simple_lock(&mntvnode_slock); 1634loop: 1635 for (vp = LIST_FIRST(&mp->mnt_vnodelist); vp; vp = nvp) { 1636 /* 1637 * Make sure this vnode wasn't reclaimed in getnewvnode(). 1638 * Start over if it has (it won't be on the list anymore). 1639 */ 1640 if (vp->v_mount != mp) 1641 goto loop; 1642 nvp = LIST_NEXT(vp, v_mntvnodes); 1643 /* 1644 * Skip over a selected vnode. 1645 */ 1646 if (vp == skipvp) 1647 continue; 1648 1649 mtx_enter(&vp->v_interlock, MTX_DEF); 1650 /* 1651 * Skip over a vnodes marked VSYSTEM. 1652 */ 1653 if ((flags & SKIPSYSTEM) && (vp->v_flag & VSYSTEM)) { 1654 mtx_exit(&vp->v_interlock, MTX_DEF); 1655 continue; 1656 } 1657 /* 1658 * If WRITECLOSE is set, only flush out regular file vnodes 1659 * open for writing. 1660 */ 1661 if ((flags & WRITECLOSE) && 1662 (vp->v_writecount == 0 || vp->v_type != VREG)) { 1663 mtx_exit(&vp->v_interlock, MTX_DEF); 1664 continue; 1665 } 1666 1667 /* 1668 * With v_usecount == 0, all we need to do is clear out the 1669 * vnode data structures and we are done. 1670 */ 1671 if (vp->v_usecount == 0) { 1672 simple_unlock(&mntvnode_slock); 1673 vgonel(vp, p); 1674 simple_lock(&mntvnode_slock); 1675 continue; 1676 } 1677 1678 /* 1679 * If FORCECLOSE is set, forcibly close the vnode. For block 1680 * or character devices, revert to an anonymous device. For 1681 * all other files, just kill them. 1682 */ 1683 if (flags & FORCECLOSE) { 1684 simple_unlock(&mntvnode_slock); 1685 if (vp->v_type != VBLK && vp->v_type != VCHR) { 1686 vgonel(vp, p); 1687 } else { 1688 vclean(vp, 0, p); 1689 vp->v_op = spec_vnodeop_p; 1690 insmntque(vp, (struct mount *) 0); 1691 } 1692 simple_lock(&mntvnode_slock); 1693 continue; 1694 } 1695#ifdef DIAGNOSTIC 1696 if (busyprt) 1697 vprint("vflush: busy vnode", vp); 1698#endif 1699 mtx_exit(&vp->v_interlock, MTX_DEF); 1700 busy++; 1701 } 1702 simple_unlock(&mntvnode_slock); 1703 if (busy) 1704 return (EBUSY); 1705 return (0); 1706} 1707 1708/* 1709 * Disassociate the underlying file system from a vnode. 1710 */ 1711static void 1712vclean(vp, flags, p) 1713 struct vnode *vp; 1714 int flags; 1715 struct proc *p; 1716{ 1717 int active; 1718 1719 /* 1720 * Check to see if the vnode is in use. If so we have to reference it 1721 * before we clean it out so that its count cannot fall to zero and 1722 * generate a race against ourselves to recycle it. 1723 */ 1724 if ((active = vp->v_usecount)) 1725 vp->v_usecount++; 1726 1727 /* 1728 * Prevent the vnode from being recycled or brought into use while we 1729 * clean it out. 1730 */ 1731 if (vp->v_flag & VXLOCK) 1732 panic("vclean: deadlock"); 1733 vp->v_flag |= VXLOCK; 1734 /* 1735 * Even if the count is zero, the VOP_INACTIVE routine may still 1736 * have the object locked while it cleans it out. The VOP_LOCK 1737 * ensures that the VOP_INACTIVE routine is done with its work. 1738 * For active vnodes, it ensures that no other activity can 1739 * occur while the underlying object is being cleaned out. 1740 */ 1741 VOP_LOCK(vp, LK_DRAIN | LK_INTERLOCK, p); 1742 1743 /* 1744 * Clean out any buffers associated with the vnode. 1745 * If the flush fails, just toss the buffers. 1746 */ 1747 if (flags & DOCLOSE) { 1748 if (TAILQ_FIRST(&vp->v_dirtyblkhd) != NULL) 1749 (void) vn_write_suspend_wait(vp, NULL, V_WAIT); 1750 if (vinvalbuf(vp, V_SAVE, NOCRED, p, 0, 0) != 0) 1751 vinvalbuf(vp, 0, NOCRED, p, 0, 0); 1752 } 1753 1754 VOP_DESTROYVOBJECT(vp); 1755 1756 /* 1757 * If purging an active vnode, it must be closed and 1758 * deactivated before being reclaimed. Note that the 1759 * VOP_INACTIVE will unlock the vnode. 1760 */ 1761 if (active) { 1762 if (flags & DOCLOSE) 1763 VOP_CLOSE(vp, FNONBLOCK, NOCRED, p); 1764 VOP_INACTIVE(vp, p); 1765 } else { 1766 /* 1767 * Any other processes trying to obtain this lock must first 1768 * wait for VXLOCK to clear, then call the new lock operation. 1769 */ 1770 VOP_UNLOCK(vp, 0, p); 1771 } 1772 /* 1773 * Reclaim the vnode. 1774 */ 1775 if (VOP_RECLAIM(vp, p)) 1776 panic("vclean: cannot reclaim"); 1777 1778 if (active) { 1779 /* 1780 * Inline copy of vrele() since VOP_INACTIVE 1781 * has already been called. 1782 */ 1783 mtx_enter(&vp->v_interlock, MTX_DEF); 1784 if (--vp->v_usecount <= 0) { 1785#ifdef DIAGNOSTIC 1786 if (vp->v_usecount < 0 || vp->v_writecount != 0) { 1787 vprint("vclean: bad ref count", vp); 1788 panic("vclean: ref cnt"); 1789 } 1790#endif 1791 vfree(vp); 1792 } 1793 mtx_exit(&vp->v_interlock, MTX_DEF); 1794 } 1795 1796 cache_purge(vp); 1797 vp->v_vnlock = NULL; 1798 lockdestroy(&vp->v_lock); 1799 1800 if (VSHOULDFREE(vp)) 1801 vfree(vp); 1802 1803 /* 1804 * Done with purge, notify sleepers of the grim news. 1805 */ 1806 vp->v_op = dead_vnodeop_p; 1807 vn_pollgone(vp); 1808 vp->v_tag = VT_NON; 1809 vp->v_flag &= ~VXLOCK; 1810 if (vp->v_flag & VXWANT) { 1811 vp->v_flag &= ~VXWANT; 1812 wakeup((caddr_t) vp); 1813 } 1814} 1815 1816/* 1817 * Eliminate all activity associated with the requested vnode 1818 * and with all vnodes aliased to the requested vnode. 1819 */ 1820int 1821vop_revoke(ap) 1822 struct vop_revoke_args /* { 1823 struct vnode *a_vp; 1824 int a_flags; 1825 } */ *ap; 1826{ 1827 struct vnode *vp, *vq; 1828 dev_t dev; 1829 1830 KASSERT((ap->a_flags & REVOKEALL) != 0, ("vop_revoke")); 1831 1832 vp = ap->a_vp; 1833 /* 1834 * If a vgone (or vclean) is already in progress, 1835 * wait until it is done and return. 1836 */ 1837 if (vp->v_flag & VXLOCK) { 1838 vp->v_flag |= VXWANT; 1839 mtx_exit(&vp->v_interlock, MTX_DEF); 1840 tsleep((caddr_t)vp, PINOD, "vop_revokeall", 0); 1841 return (0); 1842 } 1843 dev = vp->v_rdev; 1844 for (;;) { 1845 simple_lock(&spechash_slock); 1846 vq = SLIST_FIRST(&dev->si_hlist); 1847 simple_unlock(&spechash_slock); 1848 if (!vq) 1849 break; 1850 vgone(vq); 1851 } 1852 return (0); 1853} 1854 1855/* 1856 * Recycle an unused vnode to the front of the free list. 1857 * Release the passed interlock if the vnode will be recycled. 1858 */ 1859int 1860vrecycle(vp, inter_lkp, p) 1861 struct vnode *vp; 1862 struct simplelock *inter_lkp; 1863 struct proc *p; 1864{ 1865 1866 mtx_enter(&vp->v_interlock, MTX_DEF); 1867 if (vp->v_usecount == 0) { 1868 if (inter_lkp) { 1869 simple_unlock(inter_lkp); 1870 } 1871 vgonel(vp, p); 1872 return (1); 1873 } 1874 mtx_exit(&vp->v_interlock, MTX_DEF); 1875 return (0); 1876} 1877 1878/* 1879 * Eliminate all activity associated with a vnode 1880 * in preparation for reuse. 1881 */ 1882void 1883vgone(vp) 1884 register struct vnode *vp; 1885{ 1886 struct proc *p = curproc; /* XXX */ 1887 1888 mtx_enter(&vp->v_interlock, MTX_DEF); 1889 vgonel(vp, p); 1890} 1891 1892/* 1893 * vgone, with the vp interlock held. 1894 */ 1895void 1896vgonel(vp, p) 1897 struct vnode *vp; 1898 struct proc *p; 1899{ 1900 int s; 1901 1902 /* 1903 * If a vgone (or vclean) is already in progress, 1904 * wait until it is done and return. 1905 */ 1906 if (vp->v_flag & VXLOCK) { 1907 vp->v_flag |= VXWANT; 1908 mtx_exit(&vp->v_interlock, MTX_DEF); 1909 tsleep((caddr_t)vp, PINOD, "vgone", 0); 1910 return; 1911 } 1912 1913 /* 1914 * Clean out the filesystem specific data. 1915 */ 1916 vclean(vp, DOCLOSE, p); 1917 mtx_enter(&vp->v_interlock, MTX_DEF); 1918 1919 /* 1920 * Delete from old mount point vnode list, if on one. 1921 */ 1922 if (vp->v_mount != NULL) 1923 insmntque(vp, (struct mount *)0); 1924 /* 1925 * If special device, remove it from special device alias list 1926 * if it is on one. 1927 */ 1928 if ((vp->v_type == VBLK || vp->v_type == VCHR) && vp->v_rdev != NULL) { 1929 simple_lock(&spechash_slock); 1930 SLIST_REMOVE(&vp->v_rdev->si_hlist, vp, vnode, v_specnext); 1931 freedev(vp->v_rdev); 1932 simple_unlock(&spechash_slock); 1933 vp->v_rdev = NULL; 1934 } 1935 1936 /* 1937 * If it is on the freelist and not already at the head, 1938 * move it to the head of the list. The test of the 1939 * VDOOMED flag and the reference count of zero is because 1940 * it will be removed from the free list by getnewvnode, 1941 * but will not have its reference count incremented until 1942 * after calling vgone. If the reference count were 1943 * incremented first, vgone would (incorrectly) try to 1944 * close the previous instance of the underlying object. 1945 */ 1946 if (vp->v_usecount == 0 && !(vp->v_flag & VDOOMED)) { 1947 s = splbio(); 1948 simple_lock(&vnode_free_list_slock); 1949 if (vp->v_flag & VFREE) 1950 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist); 1951 else 1952 freevnodes++; 1953 vp->v_flag |= VFREE; 1954 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist); 1955 simple_unlock(&vnode_free_list_slock); 1956 splx(s); 1957 } 1958 1959 vp->v_type = VBAD; 1960 mtx_exit(&vp->v_interlock, MTX_DEF); 1961} 1962 1963/* 1964 * Lookup a vnode by device number. 1965 */ 1966int 1967vfinddev(dev, type, vpp) 1968 dev_t dev; 1969 enum vtype type; 1970 struct vnode **vpp; 1971{ 1972 struct vnode *vp; 1973 1974 simple_lock(&spechash_slock); 1975 SLIST_FOREACH(vp, &dev->si_hlist, v_specnext) { 1976 if (type == vp->v_type) { 1977 *vpp = vp; 1978 simple_unlock(&spechash_slock); 1979 return (1); 1980 } 1981 } 1982 simple_unlock(&spechash_slock); 1983 return (0); 1984} 1985 1986/* 1987 * Calculate the total number of references to a special device. 1988 */ 1989int 1990vcount(vp) 1991 struct vnode *vp; 1992{ 1993 struct vnode *vq; 1994 int count; 1995 1996 count = 0; 1997 simple_lock(&spechash_slock); 1998 SLIST_FOREACH(vq, &vp->v_rdev->si_hlist, v_specnext) 1999 count += vq->v_usecount; 2000 simple_unlock(&spechash_slock); 2001 return (count); 2002} 2003 2004/* 2005 * Same as above, but using the dev_t as argument 2006 */ 2007int 2008count_dev(dev) 2009 dev_t dev; 2010{ 2011 struct vnode *vp; 2012 2013 vp = SLIST_FIRST(&dev->si_hlist); 2014 if (vp == NULL) 2015 return (0); 2016 return(vcount(vp)); 2017} 2018 2019/* 2020 * Print out a description of a vnode. 2021 */ 2022static char *typename[] = 2023{"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD"}; 2024 2025void 2026vprint(label, vp) 2027 char *label; 2028 struct vnode *vp; 2029{ 2030 char buf[96]; 2031 2032 if (label != NULL) 2033 printf("%s: %p: ", label, (void *)vp); 2034 else 2035 printf("%p: ", (void *)vp); 2036 printf("type %s, usecount %d, writecount %d, refcount %d,", 2037 typename[vp->v_type], vp->v_usecount, vp->v_writecount, 2038 vp->v_holdcnt); 2039 buf[0] = '\0'; 2040 if (vp->v_flag & VROOT) 2041 strcat(buf, "|VROOT"); 2042 if (vp->v_flag & VTEXT) 2043 strcat(buf, "|VTEXT"); 2044 if (vp->v_flag & VSYSTEM) 2045 strcat(buf, "|VSYSTEM"); 2046 if (vp->v_flag & VXLOCK) 2047 strcat(buf, "|VXLOCK"); 2048 if (vp->v_flag & VXWANT) 2049 strcat(buf, "|VXWANT"); 2050 if (vp->v_flag & VBWAIT) 2051 strcat(buf, "|VBWAIT"); 2052 if (vp->v_flag & VDOOMED) 2053 strcat(buf, "|VDOOMED"); 2054 if (vp->v_flag & VFREE) 2055 strcat(buf, "|VFREE"); 2056 if (vp->v_flag & VOBJBUF) 2057 strcat(buf, "|VOBJBUF"); 2058 if (buf[0] != '\0') 2059 printf(" flags (%s)", &buf[1]); 2060 if (vp->v_data == NULL) { 2061 printf("\n"); 2062 } else { 2063 printf("\n\t"); 2064 VOP_PRINT(vp); 2065 } 2066} 2067 2068#ifdef DDB 2069#include <ddb/ddb.h> 2070/* 2071 * List all of the locked vnodes in the system. 2072 * Called when debugging the kernel. 2073 */ 2074DB_SHOW_COMMAND(lockedvnodes, lockedvnodes) 2075{ 2076 struct proc *p = curproc; /* XXX */ 2077 struct mount *mp, *nmp; 2078 struct vnode *vp; 2079 2080 printf("Locked vnodes\n"); 2081 mtx_enter(&mountlist_mtx, MTX_DEF); 2082 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) { 2083 if (vfs_busy(mp, LK_NOWAIT, &mountlist_mtx, p)) { 2084 nmp = TAILQ_NEXT(mp, mnt_list); 2085 continue; 2086 } 2087 LIST_FOREACH(vp, &mp->mnt_vnodelist, v_mntvnodes) { 2088 if (VOP_ISLOCKED(vp, NULL)) 2089 vprint((char *)0, vp); 2090 } 2091 mtx_enter(&mountlist_mtx, MTX_DEF); 2092 nmp = TAILQ_NEXT(mp, mnt_list); 2093 vfs_unbusy(mp, p); 2094 } 2095 mtx_exit(&mountlist_mtx, MTX_DEF); 2096} 2097#endif 2098 2099/* 2100 * Top level filesystem related information gathering. 2101 */ 2102static int sysctl_ovfs_conf __P((SYSCTL_HANDLER_ARGS)); 2103 2104static int 2105vfs_sysctl(SYSCTL_HANDLER_ARGS) 2106{ 2107 int *name = (int *)arg1 - 1; /* XXX */ 2108 u_int namelen = arg2 + 1; /* XXX */ 2109 struct vfsconf *vfsp; 2110 2111#if 1 || defined(COMPAT_PRELITE2) 2112 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */ 2113 if (namelen == 1) 2114 return (sysctl_ovfs_conf(oidp, arg1, arg2, req)); 2115#endif 2116 2117 /* XXX the below code does not compile; vfs_sysctl does not exist. */ 2118#ifdef notyet 2119 /* all sysctl names at this level are at least name and field */ 2120 if (namelen < 2) 2121 return (ENOTDIR); /* overloaded */ 2122 if (name[0] != VFS_GENERIC) { 2123 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) 2124 if (vfsp->vfc_typenum == name[0]) 2125 break; 2126 if (vfsp == NULL) 2127 return (EOPNOTSUPP); 2128 return ((*vfsp->vfc_vfsops->vfs_sysctl)(&name[1], namelen - 1, 2129 oldp, oldlenp, newp, newlen, p)); 2130 } 2131#endif 2132 switch (name[1]) { 2133 case VFS_MAXTYPENUM: 2134 if (namelen != 2) 2135 return (ENOTDIR); 2136 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int))); 2137 case VFS_CONF: 2138 if (namelen != 3) 2139 return (ENOTDIR); /* overloaded */ 2140 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) 2141 if (vfsp->vfc_typenum == name[2]) 2142 break; 2143 if (vfsp == NULL) 2144 return (EOPNOTSUPP); 2145 return (SYSCTL_OUT(req, vfsp, sizeof *vfsp)); 2146 } 2147 return (EOPNOTSUPP); 2148} 2149 2150SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD, vfs_sysctl, 2151 "Generic filesystem"); 2152 2153#if 1 || defined(COMPAT_PRELITE2) 2154 2155static int 2156sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS) 2157{ 2158 int error; 2159 struct vfsconf *vfsp; 2160 struct ovfsconf ovfs; 2161 2162 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) { 2163 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */ 2164 strcpy(ovfs.vfc_name, vfsp->vfc_name); 2165 ovfs.vfc_index = vfsp->vfc_typenum; 2166 ovfs.vfc_refcount = vfsp->vfc_refcount; 2167 ovfs.vfc_flags = vfsp->vfc_flags; 2168 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs); 2169 if (error) 2170 return error; 2171 } 2172 return 0; 2173} 2174 2175#endif /* 1 || COMPAT_PRELITE2 */ 2176 2177#if COMPILING_LINT 2178#define KINFO_VNODESLOP 10 2179/* 2180 * Dump vnode list (via sysctl). 2181 * Copyout address of vnode followed by vnode. 2182 */ 2183/* ARGSUSED */ 2184static int 2185sysctl_vnode(SYSCTL_HANDLER_ARGS) 2186{ 2187 struct proc *p = curproc; /* XXX */ 2188 struct mount *mp, *nmp; 2189 struct vnode *nvp, *vp; 2190 int error; 2191 2192#define VPTRSZ sizeof (struct vnode *) 2193#define VNODESZ sizeof (struct vnode) 2194 2195 req->lock = 0; 2196 if (!req->oldptr) /* Make an estimate */ 2197 return (SYSCTL_OUT(req, 0, 2198 (numvnodes + KINFO_VNODESLOP) * (VPTRSZ + VNODESZ))); 2199 2200 mtx_enter(&mountlist_mtx, MTX_DEF); 2201 for (mp = TAILQ_FIRST(&mountlist); mp != NULL; mp = nmp) { 2202 if (vfs_busy(mp, LK_NOWAIT, &mountlist_mtx, p)) { 2203 nmp = TAILQ_NEXT(mp, mnt_list); 2204 continue; 2205 } 2206again: 2207 simple_lock(&mntvnode_slock); 2208 for (vp = LIST_FIRST(&mp->mnt_vnodelist); 2209 vp != NULL; 2210 vp = nvp) { 2211 /* 2212 * Check that the vp is still associated with 2213 * this filesystem. RACE: could have been 2214 * recycled onto the same filesystem. 2215 */ 2216 if (vp->v_mount != mp) { 2217 simple_unlock(&mntvnode_slock); 2218 goto again; 2219 } 2220 nvp = LIST_NEXT(vp, v_mntvnodes); 2221 simple_unlock(&mntvnode_slock); 2222 if ((error = SYSCTL_OUT(req, &vp, VPTRSZ)) || 2223 (error = SYSCTL_OUT(req, vp, VNODESZ))) 2224 return (error); 2225 simple_lock(&mntvnode_slock); 2226 } 2227 simple_unlock(&mntvnode_slock); 2228 mtx_enter(&mountlist_mtx, MTX_DEF); 2229 nmp = TAILQ_NEXT(mp, mnt_list); 2230 vfs_unbusy(mp, p); 2231 } 2232 mtx_exit(&mountlist_mtx, MTX_DEF); 2233 2234 return (0); 2235} 2236 2237/* 2238 * XXX 2239 * Exporting the vnode list on large systems causes them to crash. 2240 * Exporting the vnode list on medium systems causes sysctl to coredump. 2241 */ 2242SYSCTL_PROC(_kern, KERN_VNODE, vnode, CTLTYPE_OPAQUE|CTLFLAG_RD, 2243 0, 0, sysctl_vnode, "S,vnode", ""); 2244#endif 2245 2246/* 2247 * Check to see if a filesystem is mounted on a block device. 2248 */ 2249int 2250vfs_mountedon(vp) 2251 struct vnode *vp; 2252{ 2253 2254 if (vp->v_rdev->si_mountpoint != NULL) 2255 return (EBUSY); 2256 return (0); 2257} 2258 2259/* 2260 * Unmount all filesystems. The list is traversed in reverse order 2261 * of mounting to avoid dependencies. 2262 */ 2263void 2264vfs_unmountall() 2265{ 2266 struct mount *mp; 2267 struct proc *p; 2268 int error; 2269 2270 if (curproc != NULL) 2271 p = curproc; 2272 else 2273 p = initproc; /* XXX XXX should this be proc0? */ 2274 /* 2275 * Since this only runs when rebooting, it is not interlocked. 2276 */ 2277 while(!TAILQ_EMPTY(&mountlist)) { 2278 mp = TAILQ_LAST(&mountlist, mntlist); 2279 error = dounmount(mp, MNT_FORCE, p); 2280 if (error) { 2281 TAILQ_REMOVE(&mountlist, mp, mnt_list); 2282 printf("unmount of %s failed (", 2283 mp->mnt_stat.f_mntonname); 2284 if (error == EBUSY) 2285 printf("BUSY)\n"); 2286 else 2287 printf("%d)\n", error); 2288 } else { 2289 /* The unmount has removed mp from the mountlist */ 2290 } 2291 } 2292} 2293 2294/* 2295 * Build hash lists of net addresses and hang them off the mount point. 2296 * Called by ufs_mount() to set up the lists of export addresses. 2297 */ 2298static int 2299vfs_hang_addrlist(mp, nep, argp) 2300 struct mount *mp; 2301 struct netexport *nep; 2302 struct export_args *argp; 2303{ 2304 register struct netcred *np; 2305 register struct radix_node_head *rnh; 2306 register int i; 2307 struct radix_node *rn; 2308 struct sockaddr *saddr, *smask = 0; 2309 struct domain *dom; 2310 int error; 2311 2312 if (argp->ex_addrlen == 0) { 2313 if (mp->mnt_flag & MNT_DEFEXPORTED) 2314 return (EPERM); 2315 np = &nep->ne_defexported; 2316 np->netc_exflags = argp->ex_flags; 2317 np->netc_anon = argp->ex_anon; 2318 np->netc_anon.cr_ref = 1; 2319 mp->mnt_flag |= MNT_DEFEXPORTED; 2320 return (0); 2321 } 2322 i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen; 2323 np = (struct netcred *) malloc(i, M_NETADDR, M_WAITOK); 2324 bzero((caddr_t) np, i); 2325 saddr = (struct sockaddr *) (np + 1); 2326 if ((error = copyin(argp->ex_addr, (caddr_t) saddr, argp->ex_addrlen))) 2327 goto out; 2328 if (saddr->sa_len > argp->ex_addrlen) 2329 saddr->sa_len = argp->ex_addrlen; 2330 if (argp->ex_masklen) { 2331 smask = (struct sockaddr *) ((caddr_t) saddr + argp->ex_addrlen); 2332 error = copyin(argp->ex_mask, (caddr_t) smask, argp->ex_masklen); 2333 if (error) 2334 goto out; 2335 if (smask->sa_len > argp->ex_masklen) 2336 smask->sa_len = argp->ex_masklen; 2337 } 2338 i = saddr->sa_family; 2339 if ((rnh = nep->ne_rtable[i]) == 0) { 2340 /* 2341 * Seems silly to initialize every AF when most are not used, 2342 * do so on demand here 2343 */ 2344 for (dom = domains; dom; dom = dom->dom_next) 2345 if (dom->dom_family == i && dom->dom_rtattach) { 2346 dom->dom_rtattach((void **) &nep->ne_rtable[i], 2347 dom->dom_rtoffset); 2348 break; 2349 } 2350 if ((rnh = nep->ne_rtable[i]) == 0) { 2351 error = ENOBUFS; 2352 goto out; 2353 } 2354 } 2355 rn = (*rnh->rnh_addaddr) ((caddr_t) saddr, (caddr_t) smask, rnh, 2356 np->netc_rnodes); 2357 if (rn == 0 || np != (struct netcred *) rn) { /* already exists */ 2358 error = EPERM; 2359 goto out; 2360 } 2361 np->netc_exflags = argp->ex_flags; 2362 np->netc_anon = argp->ex_anon; 2363 np->netc_anon.cr_ref = 1; 2364 return (0); 2365out: 2366 free(np, M_NETADDR); 2367 return (error); 2368} 2369 2370/* Helper for vfs_free_addrlist. */ 2371/* ARGSUSED */ 2372static int 2373vfs_free_netcred(rn, w) 2374 struct radix_node *rn; 2375 void *w; 2376{ 2377 register struct radix_node_head *rnh = (struct radix_node_head *) w; 2378 2379 (*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, rnh); 2380 free((caddr_t) rn, M_NETADDR); 2381 return (0); 2382} 2383 2384/* 2385 * Free the net address hash lists that are hanging off the mount points. 2386 */ 2387static void 2388vfs_free_addrlist(nep) 2389 struct netexport *nep; 2390{ 2391 register int i; 2392 register struct radix_node_head *rnh; 2393 2394 for (i = 0; i <= AF_MAX; i++) 2395 if ((rnh = nep->ne_rtable[i])) { 2396 (*rnh->rnh_walktree) (rnh, vfs_free_netcred, 2397 (caddr_t) rnh); 2398 free((caddr_t) rnh, M_RTABLE); 2399 nep->ne_rtable[i] = 0; 2400 } 2401} 2402 2403/* 2404 * High level function to manipulate export options on a mount point 2405 * and the passed in netexport. 2406 * Struct export_args *argp is the variable used to twiddle options, 2407 * the structure is described in sys/mount.h 2408 */ 2409int 2410vfs_export(mp, nep, argp) 2411 struct mount *mp; 2412 struct netexport *nep; 2413 struct export_args *argp; 2414{ 2415 int error; 2416 2417 if (argp->ex_flags & MNT_DELEXPORT) { 2418 if (mp->mnt_flag & MNT_EXPUBLIC) { 2419 vfs_setpublicfs(NULL, NULL, NULL); 2420 mp->mnt_flag &= ~MNT_EXPUBLIC; 2421 } 2422 vfs_free_addrlist(nep); 2423 mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED); 2424 } 2425 if (argp->ex_flags & MNT_EXPORTED) { 2426 if (argp->ex_flags & MNT_EXPUBLIC) { 2427 if ((error = vfs_setpublicfs(mp, nep, argp)) != 0) 2428 return (error); 2429 mp->mnt_flag |= MNT_EXPUBLIC; 2430 } 2431 if ((error = vfs_hang_addrlist(mp, nep, argp))) 2432 return (error); 2433 mp->mnt_flag |= MNT_EXPORTED; 2434 } 2435 return (0); 2436} 2437 2438/* 2439 * Set the publicly exported filesystem (WebNFS). Currently, only 2440 * one public filesystem is possible in the spec (RFC 2054 and 2055) 2441 */ 2442int 2443vfs_setpublicfs(mp, nep, argp) 2444 struct mount *mp; 2445 struct netexport *nep; 2446 struct export_args *argp; 2447{ 2448 int error; 2449 struct vnode *rvp; 2450 char *cp; 2451 2452 /* 2453 * mp == NULL -> invalidate the current info, the FS is 2454 * no longer exported. May be called from either vfs_export 2455 * or unmount, so check if it hasn't already been done. 2456 */ 2457 if (mp == NULL) { 2458 if (nfs_pub.np_valid) { 2459 nfs_pub.np_valid = 0; 2460 if (nfs_pub.np_index != NULL) { 2461 FREE(nfs_pub.np_index, M_TEMP); 2462 nfs_pub.np_index = NULL; 2463 } 2464 } 2465 return (0); 2466 } 2467 2468 /* 2469 * Only one allowed at a time. 2470 */ 2471 if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount) 2472 return (EBUSY); 2473 2474 /* 2475 * Get real filehandle for root of exported FS. 2476 */ 2477 bzero((caddr_t)&nfs_pub.np_handle, sizeof(nfs_pub.np_handle)); 2478 nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid; 2479 2480 if ((error = VFS_ROOT(mp, &rvp))) 2481 return (error); 2482 2483 if ((error = VFS_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid))) 2484 return (error); 2485 2486 vput(rvp); 2487 2488 /* 2489 * If an indexfile was specified, pull it in. 2490 */ 2491 if (argp->ex_indexfile != NULL) { 2492 MALLOC(nfs_pub.np_index, char *, MAXNAMLEN + 1, M_TEMP, 2493 M_WAITOK); 2494 error = copyinstr(argp->ex_indexfile, nfs_pub.np_index, 2495 MAXNAMLEN, (size_t *)0); 2496 if (!error) { 2497 /* 2498 * Check for illegal filenames. 2499 */ 2500 for (cp = nfs_pub.np_index; *cp; cp++) { 2501 if (*cp == '/') { 2502 error = EINVAL; 2503 break; 2504 } 2505 } 2506 } 2507 if (error) { 2508 FREE(nfs_pub.np_index, M_TEMP); 2509 return (error); 2510 } 2511 } 2512 2513 nfs_pub.np_mount = mp; 2514 nfs_pub.np_valid = 1; 2515 return (0); 2516} 2517 2518/* 2519 * Used by the filesystems to determine if a given network address 2520 * (passed in 'nam') is present in thier exports list, returns a pointer 2521 * to struct netcred so that the filesystem can examine it for 2522 * access rights (read/write/etc). 2523 */ 2524struct netcred * 2525vfs_export_lookup(mp, nep, nam) 2526 register struct mount *mp; 2527 struct netexport *nep; 2528 struct sockaddr *nam; 2529{ 2530 register struct netcred *np; 2531 register struct radix_node_head *rnh; 2532 struct sockaddr *saddr; 2533 2534 np = NULL; 2535 if (mp->mnt_flag & MNT_EXPORTED) { 2536 /* 2537 * Lookup in the export list first. 2538 */ 2539 if (nam != NULL) { 2540 saddr = nam; 2541 rnh = nep->ne_rtable[saddr->sa_family]; 2542 if (rnh != NULL) { 2543 np = (struct netcred *) 2544 (*rnh->rnh_matchaddr)((caddr_t)saddr, 2545 rnh); 2546 if (np && np->netc_rnodes->rn_flags & RNF_ROOT) 2547 np = NULL; 2548 } 2549 } 2550 /* 2551 * If no address match, use the default if it exists. 2552 */ 2553 if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED) 2554 np = &nep->ne_defexported; 2555 } 2556 return (np); 2557} 2558 2559/* 2560 * perform msync on all vnodes under a mount point 2561 * the mount point must be locked. 2562 */ 2563void 2564vfs_msync(struct mount *mp, int flags) { 2565 struct vnode *vp, *nvp; 2566 struct vm_object *obj; 2567 int anyio, tries; 2568 2569 tries = 5; 2570loop: 2571 anyio = 0; 2572 for (vp = LIST_FIRST(&mp->mnt_vnodelist); vp != NULL; vp = nvp) { 2573 2574 nvp = LIST_NEXT(vp, v_mntvnodes); 2575 2576 if (vp->v_mount != mp) { 2577 goto loop; 2578 } 2579 2580 if (vp->v_flag & VXLOCK) /* XXX: what if MNT_WAIT? */ 2581 continue; 2582 2583 if (flags != MNT_WAIT) { 2584 if (VOP_GETVOBJECT(vp, &obj) != 0 || 2585 (obj->flags & OBJ_MIGHTBEDIRTY) == 0) 2586 continue; 2587 if (VOP_ISLOCKED(vp, NULL)) 2588 continue; 2589 } 2590 2591 mtx_enter(&vp->v_interlock, MTX_DEF); 2592 if (VOP_GETVOBJECT(vp, &obj) == 0 && 2593 (obj->flags & OBJ_MIGHTBEDIRTY)) { 2594 if (!vget(vp, 2595 LK_INTERLOCK | LK_EXCLUSIVE | LK_RETRY | LK_NOOBJ, curproc)) { 2596 if (VOP_GETVOBJECT(vp, &obj) == 0) { 2597 vm_object_page_clean(obj, 0, 0, flags == MNT_WAIT ? OBJPC_SYNC : OBJPC_NOSYNC); 2598 anyio = 1; 2599 } 2600 vput(vp); 2601 } 2602 } else { 2603 mtx_exit(&vp->v_interlock, MTX_DEF); 2604 } 2605 } 2606 if (anyio && (--tries > 0)) 2607 goto loop; 2608} 2609 2610/* 2611 * Create the VM object needed for VMIO and mmap support. This 2612 * is done for all VREG files in the system. Some filesystems might 2613 * afford the additional metadata buffering capability of the 2614 * VMIO code by making the device node be VMIO mode also. 2615 * 2616 * vp must be locked when vfs_object_create is called. 2617 */ 2618int 2619vfs_object_create(vp, p, cred) 2620 struct vnode *vp; 2621 struct proc *p; 2622 struct ucred *cred; 2623{ 2624 return (VOP_CREATEVOBJECT(vp, cred, p)); 2625} 2626 2627/* 2628 * Mark a vnode as free, putting it up for recycling. 2629 */ 2630void 2631vfree(vp) 2632 struct vnode *vp; 2633{ 2634 int s; 2635 2636 s = splbio(); 2637 simple_lock(&vnode_free_list_slock); 2638 KASSERT((vp->v_flag & VFREE) == 0, ("vnode already free")); 2639 if (vp->v_flag & VAGE) { 2640 TAILQ_INSERT_HEAD(&vnode_free_list, vp, v_freelist); 2641 } else { 2642 TAILQ_INSERT_TAIL(&vnode_free_list, vp, v_freelist); 2643 } 2644 freevnodes++; 2645 simple_unlock(&vnode_free_list_slock); 2646 vp->v_flag &= ~VAGE; 2647 vp->v_flag |= VFREE; 2648 splx(s); 2649} 2650 2651/* 2652 * Opposite of vfree() - mark a vnode as in use. 2653 */ 2654void 2655vbusy(vp) 2656 struct vnode *vp; 2657{ 2658 int s; 2659 2660 s = splbio(); 2661 simple_lock(&vnode_free_list_slock); 2662 KASSERT((vp->v_flag & VFREE) != 0, ("vnode not free")); 2663 TAILQ_REMOVE(&vnode_free_list, vp, v_freelist); 2664 freevnodes--; 2665 simple_unlock(&vnode_free_list_slock); 2666 vp->v_flag &= ~(VFREE|VAGE); 2667 splx(s); 2668} 2669 2670/* 2671 * Record a process's interest in events which might happen to 2672 * a vnode. Because poll uses the historic select-style interface 2673 * internally, this routine serves as both the ``check for any 2674 * pending events'' and the ``record my interest in future events'' 2675 * functions. (These are done together, while the lock is held, 2676 * to avoid race conditions.) 2677 */ 2678int 2679vn_pollrecord(vp, p, events) 2680 struct vnode *vp; 2681 struct proc *p; 2682 short events; 2683{ 2684 simple_lock(&vp->v_pollinfo.vpi_lock); 2685 if (vp->v_pollinfo.vpi_revents & events) { 2686 /* 2687 * This leaves events we are not interested 2688 * in available for the other process which 2689 * which presumably had requested them 2690 * (otherwise they would never have been 2691 * recorded). 2692 */ 2693 events &= vp->v_pollinfo.vpi_revents; 2694 vp->v_pollinfo.vpi_revents &= ~events; 2695 2696 simple_unlock(&vp->v_pollinfo.vpi_lock); 2697 return events; 2698 } 2699 vp->v_pollinfo.vpi_events |= events; 2700 selrecord(p, &vp->v_pollinfo.vpi_selinfo); 2701 simple_unlock(&vp->v_pollinfo.vpi_lock); 2702 return 0; 2703} 2704 2705/* 2706 * Note the occurrence of an event. If the VN_POLLEVENT macro is used, 2707 * it is possible for us to miss an event due to race conditions, but 2708 * that condition is expected to be rare, so for the moment it is the 2709 * preferred interface. 2710 */ 2711void 2712vn_pollevent(vp, events) 2713 struct vnode *vp; 2714 short events; 2715{ 2716 simple_lock(&vp->v_pollinfo.vpi_lock); 2717 if (vp->v_pollinfo.vpi_events & events) { 2718 /* 2719 * We clear vpi_events so that we don't 2720 * call selwakeup() twice if two events are 2721 * posted before the polling process(es) is 2722 * awakened. This also ensures that we take at 2723 * most one selwakeup() if the polling process 2724 * is no longer interested. However, it does 2725 * mean that only one event can be noticed at 2726 * a time. (Perhaps we should only clear those 2727 * event bits which we note?) XXX 2728 */ 2729 vp->v_pollinfo.vpi_events = 0; /* &= ~events ??? */ 2730 vp->v_pollinfo.vpi_revents |= events; 2731 selwakeup(&vp->v_pollinfo.vpi_selinfo); 2732 } 2733 simple_unlock(&vp->v_pollinfo.vpi_lock); 2734} 2735 2736/* 2737 * Wake up anyone polling on vp because it is being revoked. 2738 * This depends on dead_poll() returning POLLHUP for correct 2739 * behavior. 2740 */ 2741void 2742vn_pollgone(vp) 2743 struct vnode *vp; 2744{ 2745 simple_lock(&vp->v_pollinfo.vpi_lock); 2746 if (vp->v_pollinfo.vpi_events) { 2747 vp->v_pollinfo.vpi_events = 0; 2748 selwakeup(&vp->v_pollinfo.vpi_selinfo); 2749 } 2750 simple_unlock(&vp->v_pollinfo.vpi_lock); 2751} 2752 2753 2754 2755/* 2756 * Routine to create and manage a filesystem syncer vnode. 2757 */ 2758#define sync_close ((int (*) __P((struct vop_close_args *)))nullop) 2759static int sync_fsync __P((struct vop_fsync_args *)); 2760static int sync_inactive __P((struct vop_inactive_args *)); 2761static int sync_reclaim __P((struct vop_reclaim_args *)); 2762#define sync_lock ((int (*) __P((struct vop_lock_args *)))vop_nolock) 2763#define sync_unlock ((int (*) __P((struct vop_unlock_args *)))vop_nounlock) 2764static int sync_print __P((struct vop_print_args *)); 2765#define sync_islocked ((int(*) __P((struct vop_islocked_args *)))vop_noislocked) 2766 2767static vop_t **sync_vnodeop_p; 2768static struct vnodeopv_entry_desc sync_vnodeop_entries[] = { 2769 { &vop_default_desc, (vop_t *) vop_eopnotsupp }, 2770 { &vop_close_desc, (vop_t *) sync_close }, /* close */ 2771 { &vop_fsync_desc, (vop_t *) sync_fsync }, /* fsync */ 2772 { &vop_inactive_desc, (vop_t *) sync_inactive }, /* inactive */ 2773 { &vop_reclaim_desc, (vop_t *) sync_reclaim }, /* reclaim */ 2774 { &vop_lock_desc, (vop_t *) sync_lock }, /* lock */ 2775 { &vop_unlock_desc, (vop_t *) sync_unlock }, /* unlock */ 2776 { &vop_print_desc, (vop_t *) sync_print }, /* print */ 2777 { &vop_islocked_desc, (vop_t *) sync_islocked }, /* islocked */ 2778 { NULL, NULL } 2779}; 2780static struct vnodeopv_desc sync_vnodeop_opv_desc = 2781 { &sync_vnodeop_p, sync_vnodeop_entries }; 2782 2783VNODEOP_SET(sync_vnodeop_opv_desc); 2784 2785/* 2786 * Create a new filesystem syncer vnode for the specified mount point. 2787 */ 2788int 2789vfs_allocate_syncvnode(mp) 2790 struct mount *mp; 2791{ 2792 struct vnode *vp; 2793 static long start, incr, next; 2794 int error; 2795 2796 /* Allocate a new vnode */ 2797 if ((error = getnewvnode(VT_VFS, mp, sync_vnodeop_p, &vp)) != 0) { 2798 mp->mnt_syncer = NULL; 2799 return (error); 2800 } 2801 vp->v_type = VNON; 2802 /* 2803 * Place the vnode onto the syncer worklist. We attempt to 2804 * scatter them about on the list so that they will go off 2805 * at evenly distributed times even if all the filesystems 2806 * are mounted at once. 2807 */ 2808 next += incr; 2809 if (next == 0 || next > syncer_maxdelay) { 2810 start /= 2; 2811 incr /= 2; 2812 if (start == 0) { 2813 start = syncer_maxdelay / 2; 2814 incr = syncer_maxdelay; 2815 } 2816 next = start; 2817 } 2818 vn_syncer_add_to_worklist(vp, syncdelay > 0 ? next % syncdelay : 0); 2819 mp->mnt_syncer = vp; 2820 return (0); 2821} 2822 2823/* 2824 * Do a lazy sync of the filesystem. 2825 */ 2826static int 2827sync_fsync(ap) 2828 struct vop_fsync_args /* { 2829 struct vnode *a_vp; 2830 struct ucred *a_cred; 2831 int a_waitfor; 2832 struct proc *a_p; 2833 } */ *ap; 2834{ 2835 struct vnode *syncvp = ap->a_vp; 2836 struct mount *mp = syncvp->v_mount; 2837 struct proc *p = ap->a_p; 2838 int asyncflag; 2839 2840 /* 2841 * We only need to do something if this is a lazy evaluation. 2842 */ 2843 if (ap->a_waitfor != MNT_LAZY) 2844 return (0); 2845 2846 /* 2847 * Move ourselves to the back of the sync list. 2848 */ 2849 vn_syncer_add_to_worklist(syncvp, syncdelay); 2850 2851 /* 2852 * Walk the list of vnodes pushing all that are dirty and 2853 * not already on the sync list. 2854 */ 2855 mtx_enter(&mountlist_mtx, MTX_DEF); 2856 if (vfs_busy(mp, LK_EXCLUSIVE | LK_NOWAIT, &mountlist_mtx, p) != 0) { 2857 mtx_exit(&mountlist_mtx, MTX_DEF); 2858 return (0); 2859 } 2860 if (vn_start_write(NULL, &mp, V_NOWAIT) != 0) { 2861 vfs_unbusy(mp, p); 2862 mtx_exit(&mountlist_mtx, MTX_DEF); 2863 return (0); 2864 } 2865 asyncflag = mp->mnt_flag & MNT_ASYNC; 2866 mp->mnt_flag &= ~MNT_ASYNC; 2867 vfs_msync(mp, MNT_NOWAIT); 2868 VFS_SYNC(mp, MNT_LAZY, ap->a_cred, p); 2869 if (asyncflag) 2870 mp->mnt_flag |= MNT_ASYNC; 2871 vn_finished_write(mp); 2872 vfs_unbusy(mp, p); 2873 return (0); 2874} 2875 2876/* 2877 * The syncer vnode is no referenced. 2878 */ 2879static int 2880sync_inactive(ap) 2881 struct vop_inactive_args /* { 2882 struct vnode *a_vp; 2883 struct proc *a_p; 2884 } */ *ap; 2885{ 2886 2887 vgone(ap->a_vp); 2888 return (0); 2889} 2890 2891/* 2892 * The syncer vnode is no longer needed and is being decommissioned. 2893 * 2894 * Modifications to the worklist must be protected at splbio(). 2895 */ 2896static int 2897sync_reclaim(ap) 2898 struct vop_reclaim_args /* { 2899 struct vnode *a_vp; 2900 } */ *ap; 2901{ 2902 struct vnode *vp = ap->a_vp; 2903 int s; 2904 2905 s = splbio(); 2906 vp->v_mount->mnt_syncer = NULL; 2907 if (vp->v_flag & VONWORKLST) { 2908 LIST_REMOVE(vp, v_synclist); 2909 vp->v_flag &= ~VONWORKLST; 2910 } 2911 splx(s); 2912 2913 return (0); 2914} 2915 2916/* 2917 * Print out a syncer vnode. 2918 */ 2919static int 2920sync_print(ap) 2921 struct vop_print_args /* { 2922 struct vnode *a_vp; 2923 } */ *ap; 2924{ 2925 struct vnode *vp = ap->a_vp; 2926 2927 printf("syncer vnode"); 2928 if (vp->v_vnlock != NULL) 2929 lockmgr_printinfo(vp->v_vnlock); 2930 printf("\n"); 2931 return (0); 2932} 2933 2934/* 2935 * extract the dev_t from a VBLK or VCHR 2936 */ 2937dev_t 2938vn_todev(vp) 2939 struct vnode *vp; 2940{ 2941 if (vp->v_type != VBLK && vp->v_type != VCHR) 2942 return (NODEV); 2943 return (vp->v_rdev); 2944} 2945 2946/* 2947 * Check if vnode represents a disk device 2948 */ 2949int 2950vn_isdisk(vp, errp) 2951 struct vnode *vp; 2952 int *errp; 2953{ 2954 struct cdevsw *cdevsw; 2955 2956 if (vp->v_type != VBLK && vp->v_type != VCHR) { 2957 if (errp != NULL) 2958 *errp = ENOTBLK; 2959 return (0); 2960 } 2961 if (vp->v_rdev == NULL) { 2962 if (errp != NULL) 2963 *errp = ENXIO; 2964 return (0); 2965 } 2966 cdevsw = devsw(vp->v_rdev); 2967 if (cdevsw == NULL) { 2968 if (errp != NULL) 2969 *errp = ENXIO; 2970 return (0); 2971 } 2972 if (!(cdevsw->d_flags & D_DISK)) { 2973 if (errp != NULL) 2974 *errp = ENOTBLK; 2975 return (0); 2976 } 2977 if (errp != NULL) 2978 *errp = 0; 2979 return (1); 2980} 2981 2982/* 2983 * Free data allocated by namei(); see namei(9) for details. 2984 */ 2985void 2986NDFREE(ndp, flags) 2987 struct nameidata *ndp; 2988 const uint flags; 2989{ 2990 if (!(flags & NDF_NO_FREE_PNBUF) && 2991 (ndp->ni_cnd.cn_flags & HASBUF)) { 2992 zfree(namei_zone, ndp->ni_cnd.cn_pnbuf); 2993 ndp->ni_cnd.cn_flags &= ~HASBUF; 2994 } 2995 if (!(flags & NDF_NO_DVP_UNLOCK) && 2996 (ndp->ni_cnd.cn_flags & LOCKPARENT) && 2997 ndp->ni_dvp != ndp->ni_vp) 2998 VOP_UNLOCK(ndp->ni_dvp, 0, ndp->ni_cnd.cn_proc); 2999 if (!(flags & NDF_NO_DVP_RELE) && 3000 (ndp->ni_cnd.cn_flags & (LOCKPARENT|WANTPARENT))) { 3001 vrele(ndp->ni_dvp); 3002 ndp->ni_dvp = NULL; 3003 } 3004 if (!(flags & NDF_NO_VP_UNLOCK) && 3005 (ndp->ni_cnd.cn_flags & LOCKLEAF) && ndp->ni_vp) 3006 VOP_UNLOCK(ndp->ni_vp, 0, ndp->ni_cnd.cn_proc); 3007 if (!(flags & NDF_NO_VP_RELE) && 3008 ndp->ni_vp) { 3009 vrele(ndp->ni_vp); 3010 ndp->ni_vp = NULL; 3011 } 3012 if (!(flags & NDF_NO_STARTDIR_RELE) && 3013 (ndp->ni_cnd.cn_flags & SAVESTART)) { 3014 vrele(ndp->ni_startdir); 3015 ndp->ni_startdir = NULL; 3016 } 3017} 3018 3019/* 3020 * Common file system object access control check routine. Accepts a 3021 * vnode's type, "mode", uid and gid, requested access mode, credentials, 3022 * and optional call-by-reference privused argument allowing vaccess() 3023 * to indicate to the caller whether privilege was used to satisfy the 3024 * request. Returns 0 on success, or an errno on failure. 3025 */ 3026int 3027vaccess(type, file_mode, file_uid, file_gid, acc_mode, cred, privused) 3028 enum vtype type; 3029 mode_t file_mode; 3030 uid_t file_uid; 3031 gid_t file_gid; 3032 mode_t acc_mode; 3033 struct ucred *cred; 3034 int *privused; 3035{ 3036 mode_t dac_granted; 3037#ifdef CAPABILITIES 3038 mode_t cap_granted; 3039#endif 3040 3041 /* 3042 * Look for a normal, non-privileged way to access the file/directory 3043 * as requested. If it exists, go with that. 3044 */ 3045 3046 if (privused != NULL) 3047 *privused = 0; 3048 3049 dac_granted = 0; 3050 3051 /* Check the owner. */ 3052 if (cred->cr_uid == file_uid) { 3053 dac_granted |= VADMIN; 3054 if (file_mode & S_IXUSR) 3055 dac_granted |= VEXEC; 3056 if (file_mode & S_IRUSR) 3057 dac_granted |= VREAD; 3058 if (file_mode & S_IWUSR) 3059 dac_granted |= VWRITE; 3060 3061 if ((acc_mode & dac_granted) == acc_mode) 3062 return (0); 3063 3064 goto privcheck; 3065 } 3066 3067 /* Otherwise, check the groups (first match) */ 3068 if (groupmember(file_gid, cred)) { 3069 if (file_mode & S_IXGRP) 3070 dac_granted |= VEXEC; 3071 if (file_mode & S_IRGRP) 3072 dac_granted |= VREAD; 3073 if (file_mode & S_IWGRP) 3074 dac_granted |= VWRITE; 3075 3076 if ((acc_mode & dac_granted) == acc_mode) 3077 return (0); 3078 3079 goto privcheck; 3080 } 3081 3082 /* Otherwise, check everyone else. */ 3083 if (file_mode & S_IXOTH) 3084 dac_granted |= VEXEC; 3085 if (file_mode & S_IROTH) 3086 dac_granted |= VREAD; 3087 if (file_mode & S_IWOTH) 3088 dac_granted |= VWRITE; 3089 if ((acc_mode & dac_granted) == acc_mode) 3090 return (0); 3091 3092privcheck: 3093 if (!suser_xxx(cred, NULL, PRISON_ROOT)) { 3094 /* XXX audit: privilege used */ 3095 if (privused != NULL) 3096 *privused = 1; 3097 return (0); 3098 } 3099 3100#ifdef CAPABILITIES 3101 /* 3102 * Build a capability mask to determine if the set of capabilities 3103 * satisfies the requirements when combined with the granted mask 3104 * from above. 3105 * For each capability, if the capability is required, bitwise 3106 * or the request type onto the cap_granted mask. 3107 */ 3108 cap_granted = 0; 3109 if ((acc_mode & VEXEC) && ((dac_granted & VEXEC) == 0) && 3110 !cap_check_xxx(cred, NULL, CAP_DAC_EXECUTE, PRISON_ROOT)) 3111 cap_granted |= VEXEC; 3112 3113 if ((acc_mode & VREAD) && ((dac_granted & VREAD) == 0) && 3114 !cap_check_xxx(cred, NULL, CAP_DAC_READ_SEARCH, PRISON_ROOT)) 3115 cap_granted |= VREAD; 3116 3117 if ((acc_mode & VWRITE) && ((dac_granted & VWRITE) == 0) && 3118 !cap_check_xxx(cred, NULL, CAP_DAC_WRITE, PRISON_ROOT)) 3119 cap_granted |= VWRITE; 3120 3121 if ((acc_mode & VADMIN) && ((dac_granted & VADMIN) == 0) && 3122 !cap_check_xxx(cred, NULL, CAP_FOWNER, PRISON_ROOT)) 3123 cap_granted |= VADMIN; 3124 3125 if ((acc_mode & (cap_granted | dac_granted)) == acc_mode) { 3126 /* XXX audit: privilege used */ 3127 if (privused != NULL) 3128 *privused = 1; 3129 return (0); 3130 } 3131#endif 3132 3133 return (EACCES); 3134} 3135