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.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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
38 * @(#)vfs_subr.c 8.31 (Berkeley) 5/26/95
39 * $FreeBSD: src/sys/kern/vfs_subr.c,v 1.249.2.30 2003/04/04 20:35:57 tegge Exp $
40 * $DragonFly: src/sys/kern/vfs_subr.c,v 1.89 2006/06/05 21:03:02 dillon Exp $
44 * External virtual filesystem routines
48 #include <sys/param.h>
49 #include <sys/systm.h>
52 #include <sys/dirent.h>
53 #include <sys/domain.h>
54 #include <sys/eventhandler.h>
55 #include <sys/fcntl.h>
56 #include <sys/kernel.h>
57 #include <sys/kthread.h>
58 #include <sys/malloc.h>
60 #include <sys/mount.h>
62 #include <sys/reboot.h>
63 #include <sys/socket.h>
65 #include <sys/sysctl.h>
66 #include <sys/syslog.h>
67 #include <sys/unistd.h>
68 #include <sys/vmmeter.h>
69 #include <sys/vnode.h>
71 #include <machine/limits.h>
74 #include <vm/vm_object.h>
75 #include <vm/vm_extern.h>
76 #include <vm/vm_kern.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>
85 #include <sys/thread2.h>
87 static MALLOC_DEFINE(M_NETADDR, "Export Host", "Export host address structure");
90 SYSCTL_INT(_debug, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0, "");
92 SYSCTL_INT(_vfs, OID_AUTO, fastdev, CTLFLAG_RW, &vfs_fastdev, 0, "");
94 enum vtype iftovt_tab[16] = {
95 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
96 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
99 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
100 S_IFSOCK, S_IFIFO, S_IFMT,
103 static int reassignbufcalls;
104 SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW,
105 &reassignbufcalls, 0, "");
106 static int reassignbufloops;
107 SYSCTL_INT(_vfs, OID_AUTO, reassignbufloops, CTLFLAG_RW,
108 &reassignbufloops, 0, "");
109 static int reassignbufsortgood;
110 SYSCTL_INT(_vfs, OID_AUTO, reassignbufsortgood, CTLFLAG_RW,
111 &reassignbufsortgood, 0, "");
112 static int reassignbufsortbad;
113 SYSCTL_INT(_vfs, OID_AUTO, reassignbufsortbad, CTLFLAG_RW,
114 &reassignbufsortbad, 0, "");
115 static int reassignbufmethod = 1;
116 SYSCTL_INT(_vfs, OID_AUTO, reassignbufmethod, CTLFLAG_RW,
117 &reassignbufmethod, 0, "");
119 int nfs_mount_type = -1;
120 static struct lwkt_token spechash_token;
121 struct nfs_public nfs_pub; /* publicly exported FS */
124 SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RW,
125 &desiredvnodes, 0, "Maximum number of vnodes");
127 static void vfs_free_addrlist (struct netexport *nep);
128 static int vfs_free_netcred (struct radix_node *rn, void *w);
129 static int vfs_hang_addrlist (struct mount *mp, struct netexport *nep,
130 struct export_args *argp);
132 extern int dev_ref_debug;
133 extern struct vnodeopv_entry_desc spec_vnodeop_entries[];
136 * Red black tree functions
138 static int rb_buf_compare(struct buf *b1, struct buf *b2);
139 RB_GENERATE2(buf_rb_tree, buf, b_rbnode, rb_buf_compare, off_t, b_loffset);
140 RB_GENERATE2(buf_rb_hash, buf, b_rbhash, rb_buf_compare, off_t, b_loffset);
143 rb_buf_compare(struct buf *b1, struct buf *b2)
145 if (b1->b_loffset < b2->b_loffset)
147 if (b1->b_loffset > b2->b_loffset)
153 * Return 0 if the vnode is already on the free list or cannot be placed
154 * on the free list. Return 1 if the vnode can be placed on the free list.
157 vshouldfree(struct vnode *vp, int usecount)
159 if (vp->v_flag & VFREE)
160 return (0); /* already free */
161 if (vp->v_holdcnt != 0 || vp->v_usecount != usecount)
162 return (0); /* other holderse */
164 (vp->v_object->ref_count || vp->v_object->resident_page_count)) {
171 * Initialize the vnode management data structures.
173 * Called from vfsinit()
179 * Desired vnodes is a result of the physical page count
180 * and the size of kernel's heap. It scales in proportion
181 * to the amount of available physical memory. This can
182 * cause trouble on 64-bit and large memory platforms.
184 /* desiredvnodes = maxproc + vmstats.v_page_count / 4; */
186 min(maxproc + vmstats.v_page_count /4,
187 2 * (VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS) /
188 (5 * (sizeof(struct vm_object) + sizeof(struct vnode))));
190 lwkt_token_init(&spechash_token);
194 * Knob to control the precision of file timestamps:
196 * 0 = seconds only; nanoseconds zeroed.
197 * 1 = seconds and nanoseconds, accurate within 1/HZ.
198 * 2 = seconds and nanoseconds, truncated to microseconds.
199 * >=3 = seconds and nanoseconds, maximum precision.
201 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC };
203 static int timestamp_precision = TSP_SEC;
204 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW,
205 ×tamp_precision, 0, "");
208 * Get a current timestamp.
211 vfs_timestamp(struct timespec *tsp)
215 switch (timestamp_precision) {
217 tsp->tv_sec = time_second;
225 TIMEVAL_TO_TIMESPEC(&tv, tsp);
235 * Set vnode attributes to VNOVAL
238 vattr_null(struct vattr *vap)
241 vap->va_size = VNOVAL;
242 vap->va_bytes = VNOVAL;
243 vap->va_mode = VNOVAL;
244 vap->va_nlink = VNOVAL;
245 vap->va_uid = VNOVAL;
246 vap->va_gid = VNOVAL;
247 vap->va_fsid = VNOVAL;
248 vap->va_fileid = VNOVAL;
249 vap->va_blocksize = VNOVAL;
250 vap->va_rdev = VNOVAL;
251 vap->va_atime.tv_sec = VNOVAL;
252 vap->va_atime.tv_nsec = VNOVAL;
253 vap->va_mtime.tv_sec = VNOVAL;
254 vap->va_mtime.tv_nsec = VNOVAL;
255 vap->va_ctime.tv_sec = VNOVAL;
256 vap->va_ctime.tv_nsec = VNOVAL;
257 vap->va_flags = VNOVAL;
258 vap->va_gen = VNOVAL;
260 vap->va_fsmid = VNOVAL;
264 * Flush out and invalidate all buffers associated with a vnode.
268 static int vinvalbuf_bp(struct buf *bp, void *data);
270 struct vinvalbuf_bp_info {
278 vupdatefsmid(struct vnode *vp)
280 atomic_set_int(&vp->v_flag, VFSMID);
284 vinvalbuf(struct vnode *vp, int flags, int slpflag, int slptimeo)
286 struct vinvalbuf_bp_info info;
291 * If we are being asked to save, call fsync to ensure that the inode
294 if (flags & V_SAVE) {
296 while (vp->v_track_write.bk_active) {
297 vp->v_track_write.bk_waitflag = 1;
298 error = tsleep(&vp->v_track_write, slpflag,
299 "vinvlbuf", slptimeo);
305 if (!RB_EMPTY(&vp->v_rbdirty_tree)) {
307 if ((error = VOP_FSYNC(vp, MNT_WAIT)) != 0)
310 if (vp->v_track_write.bk_active > 0 ||
311 !RB_EMPTY(&vp->v_rbdirty_tree))
312 panic("vinvalbuf: dirty bufs");
317 info.slptimeo = slptimeo;
318 info.lkflags = LK_EXCLUSIVE | LK_SLEEPFAIL;
319 if (slpflag & PCATCH)
320 info.lkflags |= LK_PCATCH;
325 * Flush the buffer cache until nothing is left.
327 while (!RB_EMPTY(&vp->v_rbclean_tree) ||
328 !RB_EMPTY(&vp->v_rbdirty_tree)) {
329 error = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree, NULL,
330 vinvalbuf_bp, &info);
332 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
333 vinvalbuf_bp, &info);
338 * Wait for I/O to complete. XXX needs cleaning up. The vnode can
339 * have write I/O in-progress but if there is a VM object then the
340 * VM object can also have read-I/O in-progress.
343 while (vp->v_track_write.bk_active > 0) {
344 vp->v_track_write.bk_waitflag = 1;
345 tsleep(&vp->v_track_write, 0, "vnvlbv", 0);
347 if ((object = vp->v_object) != NULL) {
348 while (object->paging_in_progress)
349 vm_object_pip_sleep(object, "vnvlbx");
351 } while (vp->v_track_write.bk_active > 0);
356 * Destroy the copy in the VM cache, too.
358 if ((object = vp->v_object) != NULL) {
359 vm_object_page_remove(object, 0, 0,
360 (flags & V_SAVE) ? TRUE : FALSE);
363 if (!RB_EMPTY(&vp->v_rbdirty_tree) || !RB_EMPTY(&vp->v_rbclean_tree))
364 panic("vinvalbuf: flush failed");
365 if (!RB_EMPTY(&vp->v_rbhash_tree))
366 panic("vinvalbuf: flush failed, buffers still present");
371 vinvalbuf_bp(struct buf *bp, void *data)
373 struct vinvalbuf_bp_info *info = data;
376 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
377 error = BUF_TIMELOCK(bp, info->lkflags,
378 "vinvalbuf", info->slptimeo);
388 KKASSERT(bp->b_vp == info->vp);
391 * XXX Since there are no node locks for NFS, I
392 * believe there is a slight chance that a delayed
393 * write will occur while sleeping just above, so
394 * check for it. Note that vfs_bio_awrite expects
395 * buffers to reside on a queue, while bwrite() and
398 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
399 (info->flags & V_SAVE)) {
400 if (bp->b_vp == info->vp) {
401 if (bp->b_flags & B_CLUSTEROK) {
405 bp->b_flags |= B_ASYNC;
412 } else if (info->flags & V_SAVE) {
414 * Cannot set B_NOCACHE on a clean buffer as this will
415 * destroy the VM backing store which might actually
416 * be dirty (and unsynchronized).
419 bp->b_flags |= (B_INVAL | B_RELBUF);
420 bp->b_flags &= ~B_ASYNC;
424 bp->b_flags |= (B_INVAL | B_NOCACHE | B_RELBUF);
425 bp->b_flags &= ~B_ASYNC;
432 * Truncate a file's buffer and pages to a specified length. This
433 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
436 * The vnode must be locked.
438 static int vtruncbuf_bp_trunc_cmp(struct buf *bp, void *data);
439 static int vtruncbuf_bp_trunc(struct buf *bp, void *data);
440 static int vtruncbuf_bp_metasync_cmp(struct buf *bp, void *data);
441 static int vtruncbuf_bp_metasync(struct buf *bp, void *data);
444 vtruncbuf(struct vnode *vp, off_t length, int blksize)
448 const char *filename;
451 * Round up to the *next* block, then destroy the buffers in question.
452 * Since we are only removing some of the buffers we must rely on the
453 * scan count to determine whether a loop is necessary.
455 if ((count = (int)(length % blksize)) != 0)
456 truncloffset = length + (blksize - count);
458 truncloffset = length;
462 count = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree,
463 vtruncbuf_bp_trunc_cmp,
464 vtruncbuf_bp_trunc, &truncloffset);
465 count += RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
466 vtruncbuf_bp_trunc_cmp,
467 vtruncbuf_bp_trunc, &truncloffset);
471 * For safety, fsync any remaining metadata if the file is not being
472 * truncated to 0. Since the metadata does not represent the entire
473 * dirty list we have to rely on the hit count to ensure that we get
478 count = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
479 vtruncbuf_bp_metasync_cmp,
480 vtruncbuf_bp_metasync, vp);
485 * Clean out any left over VM backing store.
489 vnode_pager_setsize(vp, length);
494 * It is possible to have in-progress I/O from buffers that were
495 * not part of the truncation. This should not happen if we
496 * are truncating to 0-length.
498 filename = TAILQ_FIRST(&vp->v_namecache) ?
499 TAILQ_FIRST(&vp->v_namecache)->nc_name : "?";
501 while ((count = vp->v_track_write.bk_active) > 0) {
502 vp->v_track_write.bk_waitflag = 1;
503 tsleep(&vp->v_track_write, 0, "vbtrunc", 0);
505 printf("Warning: vtruncbuf(): Had to wait for "
506 "%d buffer I/Os to finish in %s\n",
512 * Make sure no buffers were instantiated while we were trying
513 * to clean out the remaining VM pages. This could occur due
514 * to busy dirty VM pages being flushed out to disk.
517 count = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree,
518 vtruncbuf_bp_trunc_cmp,
519 vtruncbuf_bp_trunc, &truncloffset);
520 count += RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
521 vtruncbuf_bp_trunc_cmp,
522 vtruncbuf_bp_trunc, &truncloffset);
524 printf("Warning: vtruncbuf(): Had to re-clean %d "
525 "left over buffers in %s\n", count, filename);
535 * The callback buffer is beyond the new file EOF and must be destroyed.
536 * Note that the compare function must conform to the RB_SCAN's requirements.
540 vtruncbuf_bp_trunc_cmp(struct buf *bp, void *data)
542 if (bp->b_loffset >= *(off_t *)data)
549 vtruncbuf_bp_trunc(struct buf *bp, void *data)
552 * Do not try to use a buffer we cannot immediately lock, but sleep
553 * anyway to prevent a livelock. The code will loop until all buffers
556 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
557 if (BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL) == 0)
561 bp->b_flags |= (B_INVAL | B_RELBUF | B_NOCACHE);
562 bp->b_flags &= ~B_ASYNC;
569 * Fsync all meta-data after truncating a file to be non-zero. Only metadata
570 * blocks (with a negative loffset) are scanned.
571 * Note that the compare function must conform to the RB_SCAN's requirements.
574 vtruncbuf_bp_metasync_cmp(struct buf *bp, void *data)
576 if (bp->b_loffset < 0)
582 vtruncbuf_bp_metasync(struct buf *bp, void *data)
584 struct vnode *vp = data;
586 if (bp->b_flags & B_DELWRI) {
588 * Do not try to use a buffer we cannot immediately lock,
589 * but sleep anyway to prevent a livelock. The code will
590 * loop until all buffers can be acted upon.
592 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
593 if (BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL) == 0)
597 if (bp->b_vp == vp) {
598 bp->b_flags |= B_ASYNC;
600 bp->b_flags &= ~B_ASYNC;
611 * vfsync - implements a multipass fsync on a file which understands
612 * dependancies and meta-data. The passed vnode must be locked. The
613 * waitfor argument may be MNT_WAIT or MNT_NOWAIT, or MNT_LAZY.
615 * When fsyncing data asynchronously just do one consolidated pass starting
616 * with the most negative block number. This may not get all the data due
619 * When fsyncing data synchronously do a data pass, then a metadata pass,
620 * then do additional data+metadata passes to try to get all the data out.
622 static int vfsync_wait_output(struct vnode *vp,
623 int (*waitoutput)(struct vnode *, struct thread *));
624 static int vfsync_data_only_cmp(struct buf *bp, void *data);
625 static int vfsync_meta_only_cmp(struct buf *bp, void *data);
626 static int vfsync_lazy_range_cmp(struct buf *bp, void *data);
627 static int vfsync_bp(struct buf *bp, void *data);
636 int (*checkdef)(struct buf *);
640 vfsync(struct vnode *vp, int waitfor, int passes,
641 int (*checkdef)(struct buf *),
642 int (*waitoutput)(struct vnode *, struct thread *))
644 struct vfsync_info info;
647 bzero(&info, sizeof(info));
649 if ((info.checkdef = checkdef) == NULL)
652 crit_enter_id("vfsync");
657 * Lazy (filesystem syncer typ) Asynchronous plus limit the
658 * number of data (not meta) pages we try to flush to 1MB.
659 * A non-zero return means that lazy limit was reached.
661 info.lazylimit = 1024 * 1024;
663 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
664 vfsync_lazy_range_cmp, vfsync_bp, &info);
665 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
666 vfsync_meta_only_cmp, vfsync_bp, &info);
669 else if (!RB_EMPTY(&vp->v_rbdirty_tree))
670 vn_syncer_add_to_worklist(vp, 1);
675 * Asynchronous. Do a data-only pass and a meta-only pass.
678 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_data_only_cmp,
680 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_meta_only_cmp,
686 * Synchronous. Do a data-only pass, then a meta-data+data
687 * pass, then additional integrated passes to try to get
688 * all the dependancies flushed.
690 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_data_only_cmp,
692 error = vfsync_wait_output(vp, waitoutput);
694 info.skippedbufs = 0;
695 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
697 error = vfsync_wait_output(vp, waitoutput);
698 if (info.skippedbufs)
699 printf("Warning: vfsync skipped %d dirty bufs in pass2!\n", info.skippedbufs);
701 while (error == 0 && passes > 0 &&
702 !RB_EMPTY(&vp->v_rbdirty_tree)) {
704 info.synchronous = 1;
707 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
713 error = vfsync_wait_output(vp, waitoutput);
717 crit_exit_id("vfsync");
722 vfsync_wait_output(struct vnode *vp, int (*waitoutput)(struct vnode *, struct thread *))
726 while (vp->v_track_write.bk_active) {
727 vp->v_track_write.bk_waitflag = 1;
728 tsleep(&vp->v_track_write, 0, "fsfsn", 0);
731 error = waitoutput(vp, curthread);
736 vfsync_data_only_cmp(struct buf *bp, void *data)
738 if (bp->b_loffset < 0)
744 vfsync_meta_only_cmp(struct buf *bp, void *data)
746 if (bp->b_loffset < 0)
752 vfsync_lazy_range_cmp(struct buf *bp, void *data)
754 struct vfsync_info *info = data;
755 if (bp->b_loffset < info->vp->v_lazyw)
761 vfsync_bp(struct buf *bp, void *data)
763 struct vfsync_info *info = data;
764 struct vnode *vp = info->vp;
768 * if syncdeps is not set we do not try to write buffers which have
771 if (!info->synchronous && info->syncdeps == 0 && info->checkdef(bp))
775 * Ignore buffers that we cannot immediately lock. XXX
777 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
778 printf("Warning: vfsync_bp skipping dirty buffer %p\n", bp);
782 if ((bp->b_flags & B_DELWRI) == 0)
783 panic("vfsync_bp: buffer not dirty");
785 panic("vfsync_bp: buffer vp mismatch");
788 * B_NEEDCOMMIT (primarily used by NFS) is a state where the buffer
789 * has been written but an additional handshake with the device
790 * is required before we can dispose of the buffer. We have no idea
791 * how to do this so we have to skip these buffers.
793 if (bp->b_flags & B_NEEDCOMMIT) {
798 if (info->synchronous) {
800 * Synchronous flushing. An error may be returned.
803 crit_exit_id("vfsync");
805 crit_enter_id("vfsync");
808 * Asynchronous flushing. A negative return value simply
809 * stops the scan and is not considered an error. We use
810 * this to support limited MNT_LAZY flushes.
812 vp->v_lazyw = bp->b_loffset;
813 if ((vp->v_flag & VOBJBUF) && (bp->b_flags & B_CLUSTEROK)) {
814 info->lazycount += vfs_bio_awrite(bp);
816 info->lazycount += bp->b_bufsize;
818 crit_exit_id("vfsync");
820 crit_enter_id("vfsync");
822 if (info->lazylimit && info->lazycount >= info->lazylimit)
831 * Associate a buffer with a vnode.
834 bgetvp(struct vnode *vp, struct buf *bp)
836 KASSERT(bp->b_vp == NULL, ("bgetvp: not free"));
837 KKASSERT((bp->b_flags & (B_HASHED|B_DELWRI|B_VNCLEAN|B_VNDIRTY)) == 0);
841 * Insert onto list for new vnode.
845 bp->b_flags |= B_HASHED;
846 if (buf_rb_hash_RB_INSERT(&vp->v_rbhash_tree, bp))
847 panic("reassignbuf: dup lblk vp %p bp %p", vp, bp);
849 bp->b_flags |= B_VNCLEAN;
850 if (buf_rb_tree_RB_INSERT(&vp->v_rbclean_tree, bp))
851 panic("reassignbuf: dup lblk/clean vp %p bp %p", vp, bp);
856 * Disassociate a buffer from a vnode.
859 brelvp(struct buf *bp)
863 KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
866 * Delete from old vnode list, if on one.
870 if (bp->b_flags & (B_VNDIRTY | B_VNCLEAN)) {
871 if (bp->b_flags & B_VNDIRTY)
872 buf_rb_tree_RB_REMOVE(&vp->v_rbdirty_tree, bp);
874 buf_rb_tree_RB_REMOVE(&vp->v_rbclean_tree, bp);
875 bp->b_flags &= ~(B_VNDIRTY | B_VNCLEAN);
877 if (bp->b_flags & B_HASHED) {
878 buf_rb_hash_RB_REMOVE(&vp->v_rbhash_tree, bp);
879 bp->b_flags &= ~B_HASHED;
881 if ((vp->v_flag & VONWORKLST) && RB_EMPTY(&vp->v_rbdirty_tree)) {
882 vp->v_flag &= ~VONWORKLST;
883 LIST_REMOVE(vp, v_synclist);
891 * Reassign the buffer to the proper clean/dirty list based on B_DELWRI.
892 * This routine is called when the state of the B_DELWRI bit is changed.
895 reassignbuf(struct buf *bp)
897 struct vnode *vp = bp->b_vp;
900 KKASSERT(vp != NULL);
904 * B_PAGING flagged buffers cannot be reassigned because their vp
905 * is not fully linked in.
907 if (bp->b_flags & B_PAGING)
908 panic("cannot reassign paging buffer");
911 if (bp->b_flags & B_DELWRI) {
913 * Move to the dirty list, add the vnode to the worklist
915 if (bp->b_flags & B_VNCLEAN) {
916 buf_rb_tree_RB_REMOVE(&vp->v_rbclean_tree, bp);
917 bp->b_flags &= ~B_VNCLEAN;
919 if ((bp->b_flags & B_VNDIRTY) == 0) {
920 if (buf_rb_tree_RB_INSERT(&vp->v_rbdirty_tree, bp)) {
921 panic("reassignbuf: dup lblk vp %p bp %p",
924 bp->b_flags |= B_VNDIRTY;
926 if ((vp->v_flag & VONWORKLST) == 0) {
927 switch (vp->v_type) {
934 vp->v_rdev->si_mountpoint != NULL) {
942 vn_syncer_add_to_worklist(vp, delay);
946 * Move to the clean list, remove the vnode from the worklist
947 * if no dirty blocks remain.
949 if (bp->b_flags & B_VNDIRTY) {
950 buf_rb_tree_RB_REMOVE(&vp->v_rbdirty_tree, bp);
951 bp->b_flags &= ~B_VNDIRTY;
953 if ((bp->b_flags & B_VNCLEAN) == 0) {
954 if (buf_rb_tree_RB_INSERT(&vp->v_rbclean_tree, bp)) {
955 panic("reassignbuf: dup lblk vp %p bp %p",
958 bp->b_flags |= B_VNCLEAN;
960 if ((vp->v_flag & VONWORKLST) &&
961 RB_EMPTY(&vp->v_rbdirty_tree)) {
962 vp->v_flag &= ~VONWORKLST;
963 LIST_REMOVE(vp, v_synclist);
970 * Create a vnode for a block device.
971 * Used for mounting the root file system.
974 bdevvp(dev_t dev, struct vnode **vpp)
984 error = getspecialvnode(VT_NON, NULL, &spec_vnode_vops, &nvp, 0, 0);
991 vp->v_udev = dev->si_udev;
998 v_associate_rdev(struct vnode *vp, dev_t dev)
1002 if (dev == NULL || dev == NODEV)
1004 if (dev_is_good(dev) == 0)
1006 KKASSERT(vp->v_rdev == NULL);
1009 vp->v_rdev = reference_dev(dev);
1010 lwkt_gettoken(&ilock, &spechash_token);
1011 SLIST_INSERT_HEAD(&dev->si_hlist, vp, v_specnext);
1012 lwkt_reltoken(&ilock);
1017 v_release_rdev(struct vnode *vp)
1022 if ((dev = vp->v_rdev) != NULL) {
1023 lwkt_gettoken(&ilock, &spechash_token);
1024 SLIST_REMOVE(&dev->si_hlist, vp, vnode, v_specnext);
1027 lwkt_reltoken(&ilock);
1032 * Add a vnode to the alias list hung off the dev_t. We only associate
1033 * the device number with the vnode. The actual device is not associated
1034 * until the vnode is opened (usually in spec_open()), and will be
1035 * disassociated on last close.
1038 addaliasu(struct vnode *nvp, udev_t nvp_udev)
1040 if (nvp->v_type != VBLK && nvp->v_type != VCHR)
1041 panic("addaliasu on non-special vnode");
1042 nvp->v_udev = nvp_udev;
1046 * Disassociate a vnode from its underlying filesystem.
1048 * The vnode must be VX locked and refd
1050 * If there are v_usecount references to the vnode other then ours we have
1051 * to VOP_CLOSE the vnode before we can deactivate and reclaim it.
1054 vclean(struct vnode *vp, int flags)
1061 * If the vnode has already been reclaimed we have nothing to do.
1063 if (vp->v_flag & VRECLAIMED)
1065 vp->v_flag |= VRECLAIMED;
1068 * Scrap the vfs cache
1070 while (cache_inval_vp(vp, 0) != 0) {
1071 printf("Warning: vnode %p clean/cache_resolution race detected\n", vp);
1072 tsleep(vp, 0, "vclninv", 2);
1076 * Check to see if the vnode is in use. If so we have to reference it
1077 * before we clean it out so that its count cannot fall to zero and
1078 * generate a race against ourselves to recycle it.
1080 active = (vp->v_usecount > 1);
1083 * Clean out any buffers associated with the vnode and destroy its
1084 * object, if it has one.
1086 vinvalbuf(vp, V_SAVE, 0, 0);
1088 if ((object = vp->v_object) != NULL) {
1089 if (object->ref_count == 0) {
1090 if ((object->flags & OBJ_DEAD) == 0)
1091 vm_object_terminate(object);
1093 vm_pager_deallocate(object);
1095 vp->v_flag &= ~VOBJBUF;
1097 KKASSERT((vp->v_flag & VOBJBUF) == 0);
1100 * If purging an active vnode (typically during a forced unmount
1101 * or reboot), it must be closed and deactivated before being
1102 * reclaimed. This isn't really all that safe, but what can
1105 * Note that neither of these routines unlocks the vnode.
1107 if (active && (flags & DOCLOSE)) {
1108 while ((n = vp->v_opencount) != 0) {
1109 if (vp->v_writecount)
1110 VOP_CLOSE(vp, FWRITE|FNONBLOCK);
1112 VOP_CLOSE(vp, FNONBLOCK);
1113 if (vp->v_opencount == n) {
1114 printf("Warning: unable to force-close"
1122 * If the vnode has not be deactivated, deactivated it.
1124 if ((vp->v_flag & VINACTIVE) == 0) {
1125 vp->v_flag |= VINACTIVE;
1130 * Reclaim the vnode.
1132 if (VOP_RECLAIM(vp))
1133 panic("vclean: cannot reclaim");
1136 * Done with purge, notify sleepers of the grim news.
1138 vp->v_ops = &dead_vnode_vops;
1144 * Eliminate all activity associated with the requested vnode
1145 * and with all vnodes aliased to the requested vnode.
1147 * The vnode must be referenced and vx_lock()'d
1149 * revoke { struct vnode *a_vp, int a_flags }
1152 vop_stdrevoke(struct vop_revoke_args *ap)
1154 struct vnode *vp, *vq;
1158 KASSERT((ap->a_flags & REVOKEALL) != 0, ("vop_revoke"));
1163 * If the vnode is already dead don't try to revoke it
1165 if (vp->v_flag & VRECLAIMED)
1169 * If the vnode has a device association, scrap all vnodes associated
1170 * with the device. Don't let the device disappear on us while we
1171 * are scrapping the vnodes.
1173 * The passed vp will probably show up in the list, do not VX lock
1176 if (vp->v_type != VCHR && vp->v_type != VBLK)
1178 if ((dev = vp->v_rdev) == NULL) {
1179 if ((dev = udev2dev(vp->v_udev, vp->v_type == VBLK)) == NODEV)
1183 lwkt_gettoken(&ilock, &spechash_token);
1184 while ((vq = SLIST_FIRST(&dev->si_hlist)) != NULL) {
1185 if (vp == vq || vx_get(vq) == 0) {
1186 if (vq == SLIST_FIRST(&dev->si_hlist))
1192 lwkt_reltoken(&ilock);
1198 * Recycle an unused vnode to the front of the free list.
1200 * Returns 1 if we were successfully able to recycle the vnode,
1204 vrecycle(struct vnode *vp)
1206 if (vp->v_usecount == 1) {
1214 * Eliminate all activity associated with a vnode in preparation for reuse.
1216 * The vnode must be VX locked and refd and will remain VX locked and refd
1217 * on return. This routine may be called with the vnode in any state, as
1218 * long as it is VX locked. The vnode will be cleaned out and marked
1219 * VRECLAIMED but will not actually be reused until all existing refs and
1222 * NOTE: This routine may be called on a vnode which has not yet been
1223 * already been deactivated (VOP_INACTIVE), or on a vnode which has
1224 * already been reclaimed.
1226 * This routine is not responsible for placing us back on the freelist.
1227 * Instead, it happens automatically when the caller releases the VX lock
1228 * (assuming there aren't any other references).
1231 vgone(struct vnode *vp)
1234 * assert that the VX lock is held. This is an absolute requirement
1235 * now for vgone() to be called.
1237 KKASSERT(vp->v_lock.lk_exclusivecount == 1);
1240 * Clean out the filesystem specific data and set the VRECLAIMED
1241 * bit. Also deactivate the vnode if necessary.
1243 vclean(vp, DOCLOSE);
1246 * Delete from old mount point vnode list, if on one.
1248 if (vp->v_mount != NULL)
1249 insmntque(vp, NULL);
1252 * If special device, remove it from special device alias list
1253 * if it is on one. This should normally only occur if a vnode is
1254 * being revoked as the device should otherwise have been released
1257 if ((vp->v_type == VBLK || vp->v_type == VCHR) && vp->v_rdev != NULL) {
1268 * Lookup a vnode by device number.
1271 vfinddev(dev_t dev, enum vtype type, struct vnode **vpp)
1276 lwkt_gettoken(&ilock, &spechash_token);
1277 SLIST_FOREACH(vp, &dev->si_hlist, v_specnext) {
1278 if (type == vp->v_type) {
1280 lwkt_reltoken(&ilock);
1284 lwkt_reltoken(&ilock);
1289 * Calculate the total number of references to a special device. This
1290 * routine may only be called for VBLK and VCHR vnodes since v_rdev is
1291 * an overloaded field. Since udev2dev can now return NODEV, we have
1292 * to check for a NULL v_rdev.
1295 count_dev(dev_t dev)
1301 if (SLIST_FIRST(&dev->si_hlist)) {
1302 lwkt_gettoken(&ilock, &spechash_token);
1303 SLIST_FOREACH(vp, &dev->si_hlist, v_specnext) {
1304 count += vp->v_usecount;
1306 lwkt_reltoken(&ilock);
1312 count_udev(udev_t udev)
1316 if ((dev = udev2dev(udev, 0)) == NODEV)
1318 return(count_dev(dev));
1322 vcount(struct vnode *vp)
1324 if (vp->v_rdev == NULL)
1326 return(count_dev(vp->v_rdev));
1330 * Initialize VMIO for a vnode. This routine MUST be called before a
1331 * VFS can issue buffer cache ops on a vnode. It is typically called
1332 * when a vnode is initialized from its inode.
1335 vinitvmio(struct vnode *vp, off_t filesize)
1341 if ((object = vp->v_object) == NULL) {
1342 object = vnode_pager_alloc(vp, filesize, 0, 0);
1344 * Dereference the reference we just created. This assumes
1345 * that the object is associated with the vp.
1347 object->ref_count--;
1350 if (object->flags & OBJ_DEAD) {
1352 tsleep(object, 0, "vodead", 0);
1353 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1357 KASSERT(vp->v_object != NULL, ("vinitvmio: NULL object"));
1358 vp->v_flag |= VOBJBUF;
1364 * Print out a description of a vnode.
1366 static char *typename[] =
1367 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD"};
1370 vprint(char *label, struct vnode *vp)
1375 printf("%s: %p: ", label, (void *)vp);
1377 printf("%p: ", (void *)vp);
1378 printf("type %s, usecount %d, writecount %d, refcount %d,",
1379 typename[vp->v_type], vp->v_usecount, vp->v_writecount,
1382 if (vp->v_flag & VROOT)
1383 strcat(buf, "|VROOT");
1384 if (vp->v_flag & VTEXT)
1385 strcat(buf, "|VTEXT");
1386 if (vp->v_flag & VSYSTEM)
1387 strcat(buf, "|VSYSTEM");
1388 if (vp->v_flag & VFREE)
1389 strcat(buf, "|VFREE");
1390 if (vp->v_flag & VOBJBUF)
1391 strcat(buf, "|VOBJBUF");
1393 printf(" flags (%s)", &buf[1]);
1394 if (vp->v_data == NULL) {
1403 #include <ddb/ddb.h>
1405 static int db_show_locked_vnodes(struct mount *mp, void *data);
1408 * List all of the locked vnodes in the system.
1409 * Called when debugging the kernel.
1411 DB_SHOW_COMMAND(lockedvnodes, lockedvnodes)
1413 printf("Locked vnodes\n");
1414 mountlist_scan(db_show_locked_vnodes, NULL,
1415 MNTSCAN_FORWARD|MNTSCAN_NOBUSY);
1419 db_show_locked_vnodes(struct mount *mp, void *data __unused)
1423 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
1424 if (VOP_ISLOCKED(vp, NULL))
1425 vprint((char *)0, vp);
1432 * Top level filesystem related information gathering.
1434 static int sysctl_ovfs_conf (SYSCTL_HANDLER_ARGS);
1437 vfs_sysctl(SYSCTL_HANDLER_ARGS)
1439 int *name = (int *)arg1 - 1; /* XXX */
1440 u_int namelen = arg2 + 1; /* XXX */
1441 struct vfsconf *vfsp;
1443 #if 1 || defined(COMPAT_PRELITE2)
1444 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
1446 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
1450 /* all sysctl names at this level are at least name and field */
1452 return (ENOTDIR); /* overloaded */
1453 if (name[0] != VFS_GENERIC) {
1454 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
1455 if (vfsp->vfc_typenum == name[0])
1458 return (EOPNOTSUPP);
1459 return ((*vfsp->vfc_vfsops->vfs_sysctl)(&name[1], namelen - 1,
1460 oldp, oldlenp, newp, newlen, p));
1464 case VFS_MAXTYPENUM:
1467 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int)));
1470 return (ENOTDIR); /* overloaded */
1471 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
1472 if (vfsp->vfc_typenum == name[2])
1475 return (EOPNOTSUPP);
1476 return (SYSCTL_OUT(req, vfsp, sizeof *vfsp));
1478 return (EOPNOTSUPP);
1481 SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD, vfs_sysctl,
1482 "Generic filesystem");
1484 #if 1 || defined(COMPAT_PRELITE2)
1487 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
1490 struct vfsconf *vfsp;
1491 struct ovfsconf ovfs;
1493 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
1494 bzero(&ovfs, sizeof(ovfs));
1495 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
1496 strcpy(ovfs.vfc_name, vfsp->vfc_name);
1497 ovfs.vfc_index = vfsp->vfc_typenum;
1498 ovfs.vfc_refcount = vfsp->vfc_refcount;
1499 ovfs.vfc_flags = vfsp->vfc_flags;
1500 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
1507 #endif /* 1 || COMPAT_PRELITE2 */
1510 * Check to see if a filesystem is mounted on a block device.
1513 vfs_mountedon(struct vnode *vp)
1517 if ((dev = vp->v_rdev) == NULL)
1518 dev = udev2dev(vp->v_udev, (vp->v_type == VBLK));
1519 if (dev != NODEV && dev->si_mountpoint)
1525 * Unmount all filesystems. The list is traversed in reverse order
1526 * of mounting to avoid dependencies.
1529 static int vfs_umountall_callback(struct mount *mp, void *data);
1532 vfs_unmountall(void)
1534 struct thread *td = curthread;
1537 if (td->td_proc == NULL)
1538 td = initproc->p_thread; /* XXX XXX use proc0 instead? */
1541 count = mountlist_scan(vfs_umountall_callback,
1542 NULL, MNTSCAN_REVERSE|MNTSCAN_NOBUSY);
1548 vfs_umountall_callback(struct mount *mp, void *data)
1552 error = dounmount(mp, MNT_FORCE);
1554 mountlist_remove(mp);
1555 printf("unmount of filesystem mounted from %s failed (",
1556 mp->mnt_stat.f_mntfromname);
1560 printf("%d)\n", error);
1566 * Build hash lists of net addresses and hang them off the mount point.
1567 * Called by ufs_mount() to set up the lists of export addresses.
1570 vfs_hang_addrlist(struct mount *mp, struct netexport *nep,
1571 struct export_args *argp)
1574 struct radix_node_head *rnh;
1576 struct radix_node *rn;
1577 struct sockaddr *saddr, *smask = 0;
1581 if (argp->ex_addrlen == 0) {
1582 if (mp->mnt_flag & MNT_DEFEXPORTED)
1584 np = &nep->ne_defexported;
1585 np->netc_exflags = argp->ex_flags;
1586 np->netc_anon = argp->ex_anon;
1587 np->netc_anon.cr_ref = 1;
1588 mp->mnt_flag |= MNT_DEFEXPORTED;
1592 if (argp->ex_addrlen < 0 || argp->ex_addrlen > MLEN)
1594 if (argp->ex_masklen < 0 || argp->ex_masklen > MLEN)
1597 i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
1598 np = (struct netcred *) malloc(i, M_NETADDR, M_WAITOK);
1599 bzero((caddr_t) np, i);
1600 saddr = (struct sockaddr *) (np + 1);
1601 if ((error = copyin(argp->ex_addr, (caddr_t) saddr, argp->ex_addrlen)))
1603 if (saddr->sa_len > argp->ex_addrlen)
1604 saddr->sa_len = argp->ex_addrlen;
1605 if (argp->ex_masklen) {
1606 smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen);
1607 error = copyin(argp->ex_mask, (caddr_t)smask, argp->ex_masklen);
1610 if (smask->sa_len > argp->ex_masklen)
1611 smask->sa_len = argp->ex_masklen;
1613 i = saddr->sa_family;
1614 if ((rnh = nep->ne_rtable[i]) == 0) {
1616 * Seems silly to initialize every AF when most are not used,
1617 * do so on demand here
1619 SLIST_FOREACH(dom, &domains, dom_next)
1620 if (dom->dom_family == i && dom->dom_rtattach) {
1621 dom->dom_rtattach((void **) &nep->ne_rtable[i],
1625 if ((rnh = nep->ne_rtable[i]) == 0) {
1630 rn = (*rnh->rnh_addaddr) ((char *) saddr, (char *) smask, rnh,
1632 if (rn == 0 || np != (struct netcred *) rn) { /* already exists */
1636 np->netc_exflags = argp->ex_flags;
1637 np->netc_anon = argp->ex_anon;
1638 np->netc_anon.cr_ref = 1;
1641 free(np, M_NETADDR);
1647 vfs_free_netcred(struct radix_node *rn, void *w)
1649 struct radix_node_head *rnh = (struct radix_node_head *) w;
1651 (*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, rnh);
1652 free((caddr_t) rn, M_NETADDR);
1657 * Free the net address hash lists that are hanging off the mount points.
1660 vfs_free_addrlist(struct netexport *nep)
1663 struct radix_node_head *rnh;
1665 for (i = 0; i <= AF_MAX; i++)
1666 if ((rnh = nep->ne_rtable[i])) {
1667 (*rnh->rnh_walktree) (rnh, vfs_free_netcred,
1669 free((caddr_t) rnh, M_RTABLE);
1670 nep->ne_rtable[i] = 0;
1675 vfs_export(struct mount *mp, struct netexport *nep, struct export_args *argp)
1679 if (argp->ex_flags & MNT_DELEXPORT) {
1680 if (mp->mnt_flag & MNT_EXPUBLIC) {
1681 vfs_setpublicfs(NULL, NULL, NULL);
1682 mp->mnt_flag &= ~MNT_EXPUBLIC;
1684 vfs_free_addrlist(nep);
1685 mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
1687 if (argp->ex_flags & MNT_EXPORTED) {
1688 if (argp->ex_flags & MNT_EXPUBLIC) {
1689 if ((error = vfs_setpublicfs(mp, nep, argp)) != 0)
1691 mp->mnt_flag |= MNT_EXPUBLIC;
1693 if ((error = vfs_hang_addrlist(mp, nep, argp)))
1695 mp->mnt_flag |= MNT_EXPORTED;
1702 * Set the publicly exported filesystem (WebNFS). Currently, only
1703 * one public filesystem is possible in the spec (RFC 2054 and 2055)
1706 vfs_setpublicfs(struct mount *mp, struct netexport *nep,
1707 struct export_args *argp)
1714 * mp == NULL -> invalidate the current info, the FS is
1715 * no longer exported. May be called from either vfs_export
1716 * or unmount, so check if it hasn't already been done.
1719 if (nfs_pub.np_valid) {
1720 nfs_pub.np_valid = 0;
1721 if (nfs_pub.np_index != NULL) {
1722 FREE(nfs_pub.np_index, M_TEMP);
1723 nfs_pub.np_index = NULL;
1730 * Only one allowed at a time.
1732 if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
1736 * Get real filehandle for root of exported FS.
1738 bzero((caddr_t)&nfs_pub.np_handle, sizeof(nfs_pub.np_handle));
1739 nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid;
1741 if ((error = VFS_ROOT(mp, &rvp)))
1744 if ((error = VFS_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid)))
1750 * If an indexfile was specified, pull it in.
1752 if (argp->ex_indexfile != NULL) {
1755 error = vn_get_namelen(rvp, &namelen);
1758 MALLOC(nfs_pub.np_index, char *, namelen, M_TEMP,
1760 error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
1761 namelen, (size_t *)0);
1764 * Check for illegal filenames.
1766 for (cp = nfs_pub.np_index; *cp; cp++) {
1774 FREE(nfs_pub.np_index, M_TEMP);
1779 nfs_pub.np_mount = mp;
1780 nfs_pub.np_valid = 1;
1785 vfs_export_lookup(struct mount *mp, struct netexport *nep,
1786 struct sockaddr *nam)
1789 struct radix_node_head *rnh;
1790 struct sockaddr *saddr;
1793 if (mp->mnt_flag & MNT_EXPORTED) {
1795 * Lookup in the export list first.
1799 rnh = nep->ne_rtable[saddr->sa_family];
1801 np = (struct netcred *)
1802 (*rnh->rnh_matchaddr)((char *)saddr,
1804 if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
1809 * If no address match, use the default if it exists.
1811 if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
1812 np = &nep->ne_defexported;
1818 * perform msync on all vnodes under a mount point. The mount point must
1819 * be locked. This code is also responsible for lazy-freeing unreferenced
1820 * vnodes whos VM objects no longer contain pages.
1822 * NOTE: MNT_WAIT still skips vnodes in the VXLOCK state.
1824 * NOTE: XXX VOP_PUTPAGES and friends requires that the vnode be locked,
1825 * but vnode_pager_putpages() doesn't lock the vnode. We have to do it
1826 * way up in this high level function.
1828 static int vfs_msync_scan1(struct mount *mp, struct vnode *vp, void *data);
1829 static int vfs_msync_scan2(struct mount *mp, struct vnode *vp, void *data);
1832 vfs_msync(struct mount *mp, int flags)
1836 vmsc_flags = VMSC_GETVP;
1837 if (flags != MNT_WAIT)
1838 vmsc_flags |= VMSC_NOWAIT;
1839 vmntvnodescan(mp, vmsc_flags, vfs_msync_scan1, vfs_msync_scan2,
1844 * scan1 is a fast pre-check. There could be hundreds of thousands of
1845 * vnodes, we cannot afford to do anything heavy weight until we have a
1846 * fairly good indication that there is work to do.
1850 vfs_msync_scan1(struct mount *mp, struct vnode *vp, void *data)
1852 int flags = (int)data;
1854 if ((vp->v_flag & VRECLAIMED) == 0) {
1855 if (vshouldfree(vp, 0))
1856 return(0); /* call scan2 */
1857 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
1858 (vp->v_flag & VOBJDIRTY) &&
1859 (flags == MNT_WAIT || VOP_ISLOCKED(vp, NULL) == 0)) {
1860 return(0); /* call scan2 */
1865 * do not call scan2, continue the loop
1871 * This callback is handed a locked vnode.
1875 vfs_msync_scan2(struct mount *mp, struct vnode *vp, void *data)
1878 int flags = (int)data;
1880 if (vp->v_flag & VRECLAIMED)
1883 if ((mp->mnt_flag & MNT_RDONLY) == 0 && (vp->v_flag & VOBJDIRTY)) {
1884 if ((obj = vp->v_object) != NULL) {
1885 vm_object_page_clean(obj, 0, 0,
1886 flags == MNT_WAIT ? OBJPC_SYNC : OBJPC_NOSYNC);
1893 * Record a process's interest in events which might happen to
1894 * a vnode. Because poll uses the historic select-style interface
1895 * internally, this routine serves as both the ``check for any
1896 * pending events'' and the ``record my interest in future events''
1897 * functions. (These are done together, while the lock is held,
1898 * to avoid race conditions.)
1901 vn_pollrecord(struct vnode *vp, int events)
1905 KKASSERT(curthread->td_proc != NULL);
1907 lwkt_gettoken(&ilock, &vp->v_pollinfo.vpi_token);
1908 if (vp->v_pollinfo.vpi_revents & events) {
1910 * This leaves events we are not interested
1911 * in available for the other process which
1912 * which presumably had requested them
1913 * (otherwise they would never have been
1916 events &= vp->v_pollinfo.vpi_revents;
1917 vp->v_pollinfo.vpi_revents &= ~events;
1919 lwkt_reltoken(&ilock);
1922 vp->v_pollinfo.vpi_events |= events;
1923 selrecord(curthread, &vp->v_pollinfo.vpi_selinfo);
1924 lwkt_reltoken(&ilock);
1929 * Note the occurrence of an event. If the VN_POLLEVENT macro is used,
1930 * it is possible for us to miss an event due to race conditions, but
1931 * that condition is expected to be rare, so for the moment it is the
1932 * preferred interface.
1935 vn_pollevent(struct vnode *vp, int events)
1939 lwkt_gettoken(&ilock, &vp->v_pollinfo.vpi_token);
1940 if (vp->v_pollinfo.vpi_events & events) {
1942 * We clear vpi_events so that we don't
1943 * call selwakeup() twice if two events are
1944 * posted before the polling process(es) is
1945 * awakened. This also ensures that we take at
1946 * most one selwakeup() if the polling process
1947 * is no longer interested. However, it does
1948 * mean that only one event can be noticed at
1949 * a time. (Perhaps we should only clear those
1950 * event bits which we note?) XXX
1952 vp->v_pollinfo.vpi_events = 0; /* &= ~events ??? */
1953 vp->v_pollinfo.vpi_revents |= events;
1954 selwakeup(&vp->v_pollinfo.vpi_selinfo);
1956 lwkt_reltoken(&ilock);
1960 * Wake up anyone polling on vp because it is being revoked.
1961 * This depends on dead_poll() returning POLLHUP for correct
1965 vn_pollgone(struct vnode *vp)
1969 lwkt_gettoken(&ilock, &vp->v_pollinfo.vpi_token);
1970 if (vp->v_pollinfo.vpi_events) {
1971 vp->v_pollinfo.vpi_events = 0;
1972 selwakeup(&vp->v_pollinfo.vpi_selinfo);
1974 lwkt_reltoken(&ilock);
1978 * extract the dev_t from a VBLK or VCHR. The vnode must have been opened
1979 * (or v_rdev might be NULL).
1982 vn_todev(struct vnode *vp)
1984 if (vp->v_type != VBLK && vp->v_type != VCHR)
1986 KKASSERT(vp->v_rdev != NULL);
1987 return (vp->v_rdev);
1991 * Check if vnode represents a disk device. The vnode does not need to be
1995 vn_isdisk(struct vnode *vp, int *errp)
1999 if (vp->v_type != VBLK && vp->v_type != VCHR) {
2005 if ((dev = vp->v_rdev) == NULL)
2006 dev = udev2dev(vp->v_udev, (vp->v_type == VBLK));
2007 if (dev == NULL || dev == NODEV) {
2012 if (dev_is_good(dev) == 0) {
2017 if ((dev_dflags(dev) & D_DISK) == 0) {
2027 #ifdef DEBUG_VFS_LOCKS
2030 assert_vop_locked(struct vnode *vp, const char *str)
2032 if (vp && !VOP_ISLOCKED(vp, NULL)) {
2033 panic("%s: %p is not locked shared but should be", str, vp);
2038 assert_vop_unlocked(struct vnode *vp, const char *str)
2041 if (VOP_ISLOCKED(vp, curthread) == LK_EXCLUSIVE) {
2042 panic("%s: %p is locked but should not be", str, vp);
2050 vn_get_namelen(struct vnode *vp, int *namelen)
2052 int error, retval[2];
2054 error = VOP_PATHCONF(vp, _PC_NAME_MAX, retval);
2062 vop_write_dirent(int *error, struct uio *uio, ino_t d_ino, uint8_t d_type,
2063 uint16_t d_namlen, const char *d_name)
2068 len = _DIRENT_RECLEN(d_namlen);
2069 if (len > uio->uio_resid)
2072 dp = malloc(len, M_TEMP, M_WAITOK | M_ZERO);
2075 dp->d_namlen = d_namlen;
2076 dp->d_type = d_type;
2077 bcopy(d_name, dp->d_name, d_namlen);
2079 *error = uiomove((caddr_t)dp, len, uio);