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.87 2006/05/25 19:31:13 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.
488 vnode_pager_setsize(vp, length);
492 * It is possible to have in-progress I/O from buffers that were
493 * not part of the truncation. This should not happen if we
494 * are truncating to 0-length.
496 filename = TAILQ_FIRST(&vp->v_namecache) ?
497 TAILQ_FIRST(&vp->v_namecache)->nc_name : "?";
499 while ((count = vp->v_track_write.bk_active) > 0) {
500 vp->v_track_write.bk_waitflag = 1;
501 tsleep(&vp->v_track_write, 0, "vbtrunc", 0);
503 printf("Warning: vtruncbuf(): Had to wait for "
504 "%d buffer I/Os to finish in %s\n",
510 * Make sure no buffers were instantiated while we were trying
511 * to clean out the remaining VM pages. This could occur due
512 * to busy dirty VM pages being flushed out to disk.
515 count = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree,
516 vtruncbuf_bp_trunc_cmp,
517 vtruncbuf_bp_trunc, &truncloffset);
518 count += RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
519 vtruncbuf_bp_trunc_cmp,
520 vtruncbuf_bp_trunc, &truncloffset);
522 printf("Warning: vtruncbuf(): Had to re-clean %d "
523 "left over buffers in %s\n", count, filename);
531 * The callback buffer is beyond the new file EOF and must be destroyed.
532 * Note that the compare function must conform to the RB_SCAN's requirements.
536 vtruncbuf_bp_trunc_cmp(struct buf *bp, void *data)
538 if (bp->b_loffset >= *(off_t *)data)
545 vtruncbuf_bp_trunc(struct buf *bp, void *data)
548 * Do not try to use a buffer we cannot immediately lock, but sleep
549 * anyway to prevent a livelock. The code will loop until all buffers
552 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
553 if (BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL) == 0)
557 bp->b_flags |= (B_INVAL | B_RELBUF | B_NOCACHE);
558 bp->b_flags &= ~B_ASYNC;
565 * Fsync all meta-data after truncating a file to be non-zero. Only metadata
566 * blocks (with a negative loffset) are scanned.
567 * Note that the compare function must conform to the RB_SCAN's requirements.
570 vtruncbuf_bp_metasync_cmp(struct buf *bp, void *data)
572 if (bp->b_loffset < 0)
578 vtruncbuf_bp_metasync(struct buf *bp, void *data)
580 struct vnode *vp = data;
582 if (bp->b_flags & B_DELWRI) {
584 * Do not try to use a buffer we cannot immediately lock,
585 * but sleep anyway to prevent a livelock. The code will
586 * loop until all buffers can be acted upon.
588 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
589 if (BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL) == 0)
593 if (bp->b_vp == vp) {
594 bp->b_flags |= B_ASYNC;
596 bp->b_flags &= ~B_ASYNC;
607 * vfsync - implements a multipass fsync on a file which understands
608 * dependancies and meta-data. The passed vnode must be locked. The
609 * waitfor argument may be MNT_WAIT or MNT_NOWAIT, or MNT_LAZY.
611 * When fsyncing data asynchronously just do one consolidated pass starting
612 * with the most negative block number. This may not get all the data due
615 * When fsyncing data synchronously do a data pass, then a metadata pass,
616 * then do additional data+metadata passes to try to get all the data out.
618 static int vfsync_wait_output(struct vnode *vp,
619 int (*waitoutput)(struct vnode *, struct thread *));
620 static int vfsync_data_only_cmp(struct buf *bp, void *data);
621 static int vfsync_meta_only_cmp(struct buf *bp, void *data);
622 static int vfsync_lazy_range_cmp(struct buf *bp, void *data);
623 static int vfsync_bp(struct buf *bp, void *data);
632 int (*checkdef)(struct buf *);
636 vfsync(struct vnode *vp, int waitfor, int passes,
637 int (*checkdef)(struct buf *),
638 int (*waitoutput)(struct vnode *, struct thread *))
640 struct vfsync_info info;
643 bzero(&info, sizeof(info));
645 if ((info.checkdef = checkdef) == NULL)
653 * Lazy (filesystem syncer typ) Asynchronous plus limit the
654 * number of data (not meta) pages we try to flush to 1MB.
655 * A non-zero return means that lazy limit was reached.
657 info.lazylimit = 1024 * 1024;
659 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
660 vfsync_lazy_range_cmp, vfsync_bp, &info);
661 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
662 vfsync_meta_only_cmp, vfsync_bp, &info);
665 else if (!RB_EMPTY(&vp->v_rbdirty_tree))
666 vn_syncer_add_to_worklist(vp, 1);
671 * Asynchronous. Do a data-only pass and a meta-only pass.
674 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_data_only_cmp,
676 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_meta_only_cmp,
682 * Synchronous. Do a data-only pass, then a meta-data+data
683 * pass, then additional integrated passes to try to get
684 * all the dependancies flushed.
686 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_data_only_cmp,
688 error = vfsync_wait_output(vp, waitoutput);
690 info.skippedbufs = 0;
691 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
693 error = vfsync_wait_output(vp, waitoutput);
694 if (info.skippedbufs)
695 printf("Warning: vfsync skipped %d dirty bufs in pass2!\n", info.skippedbufs);
697 while (error == 0 && passes > 0 &&
698 !RB_EMPTY(&vp->v_rbdirty_tree)) {
700 info.synchronous = 1;
703 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
709 error = vfsync_wait_output(vp, waitoutput);
718 vfsync_wait_output(struct vnode *vp, int (*waitoutput)(struct vnode *, struct thread *))
722 while (vp->v_track_write.bk_active) {
723 vp->v_track_write.bk_waitflag = 1;
724 tsleep(&vp->v_track_write, 0, "fsfsn", 0);
727 error = waitoutput(vp, curthread);
732 vfsync_data_only_cmp(struct buf *bp, void *data)
734 if (bp->b_loffset < 0)
740 vfsync_meta_only_cmp(struct buf *bp, void *data)
742 if (bp->b_loffset < 0)
748 vfsync_lazy_range_cmp(struct buf *bp, void *data)
750 struct vfsync_info *info = data;
751 if (bp->b_loffset < info->vp->v_lazyw)
757 vfsync_bp(struct buf *bp, void *data)
759 struct vfsync_info *info = data;
760 struct vnode *vp = info->vp;
764 * if syncdeps is not set we do not try to write buffers which have
767 if (!info->synchronous && info->syncdeps == 0 && info->checkdef(bp))
771 * Ignore buffers that we cannot immediately lock. XXX
773 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
774 printf("Warning: vfsync_bp skipping dirty buffer %p\n", bp);
778 if ((bp->b_flags & B_DELWRI) == 0)
779 panic("vfsync_bp: buffer not dirty");
781 panic("vfsync_bp: buffer vp mismatch");
784 * B_NEEDCOMMIT (primarily used by NFS) is a state where the buffer
785 * has been written but an additional handshake with the device
786 * is required before we can dispose of the buffer. We have no idea
787 * how to do this so we have to skip these buffers.
789 if (bp->b_flags & B_NEEDCOMMIT) {
794 if (info->synchronous) {
796 * Synchronous flushing. An error may be returned.
804 * Asynchronous flushing. A negative return value simply
805 * stops the scan and is not considered an error. We use
806 * this to support limited MNT_LAZY flushes.
808 vp->v_lazyw = bp->b_loffset;
809 if ((vp->v_flag & VOBJBUF) && (bp->b_flags & B_CLUSTEROK)) {
810 info->lazycount += vfs_bio_awrite(bp);
812 info->lazycount += bp->b_bufsize;
818 if (info->lazylimit && info->lazycount >= info->lazylimit)
827 * Associate a buffer with a vnode.
830 bgetvp(struct vnode *vp, struct buf *bp)
832 KASSERT(bp->b_vp == NULL, ("bgetvp: not free"));
833 KKASSERT((bp->b_flags & (B_HASHED|B_DELWRI|B_VNCLEAN|B_VNDIRTY)) == 0);
837 * Insert onto list for new vnode.
841 bp->b_flags |= B_HASHED;
842 if (buf_rb_hash_RB_INSERT(&vp->v_rbhash_tree, bp))
843 panic("reassignbuf: dup lblk vp %p bp %p", vp, bp);
845 bp->b_flags |= B_VNCLEAN;
846 if (buf_rb_tree_RB_INSERT(&vp->v_rbclean_tree, bp))
847 panic("reassignbuf: dup lblk/clean vp %p bp %p", vp, bp);
852 * Disassociate a buffer from a vnode.
855 brelvp(struct buf *bp)
859 KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
862 * Delete from old vnode list, if on one.
866 if (bp->b_flags & (B_VNDIRTY | B_VNCLEAN)) {
867 if (bp->b_flags & B_VNDIRTY)
868 buf_rb_tree_RB_REMOVE(&vp->v_rbdirty_tree, bp);
870 buf_rb_tree_RB_REMOVE(&vp->v_rbclean_tree, bp);
871 bp->b_flags &= ~(B_VNDIRTY | B_VNCLEAN);
873 if (bp->b_flags & B_HASHED) {
874 buf_rb_hash_RB_REMOVE(&vp->v_rbhash_tree, bp);
875 bp->b_flags &= ~B_HASHED;
877 if ((vp->v_flag & VONWORKLST) && RB_EMPTY(&vp->v_rbdirty_tree)) {
878 vp->v_flag &= ~VONWORKLST;
879 LIST_REMOVE(vp, v_synclist);
887 * Reassign the buffer to the proper clean/dirty list based on B_DELWRI.
888 * This routine is called when the state of the B_DELWRI bit is changed.
891 reassignbuf(struct buf *bp)
893 struct vnode *vp = bp->b_vp;
896 KKASSERT(vp != NULL);
900 * B_PAGING flagged buffers cannot be reassigned because their vp
901 * is not fully linked in.
903 if (bp->b_flags & B_PAGING)
904 panic("cannot reassign paging buffer");
907 if (bp->b_flags & B_DELWRI) {
909 * Move to the dirty list, add the vnode to the worklist
911 if (bp->b_flags & B_VNCLEAN) {
912 buf_rb_tree_RB_REMOVE(&vp->v_rbclean_tree, bp);
913 bp->b_flags &= ~B_VNCLEAN;
915 if ((bp->b_flags & B_VNDIRTY) == 0) {
916 if (buf_rb_tree_RB_INSERT(&vp->v_rbdirty_tree, bp)) {
917 panic("reassignbuf: dup lblk vp %p bp %p",
920 bp->b_flags |= B_VNDIRTY;
922 if ((vp->v_flag & VONWORKLST) == 0) {
923 switch (vp->v_type) {
930 vp->v_rdev->si_mountpoint != NULL) {
938 vn_syncer_add_to_worklist(vp, delay);
942 * Move to the clean list, remove the vnode from the worklist
943 * if no dirty blocks remain.
945 if (bp->b_flags & B_VNDIRTY) {
946 buf_rb_tree_RB_REMOVE(&vp->v_rbdirty_tree, bp);
947 bp->b_flags &= ~B_VNDIRTY;
949 if ((bp->b_flags & B_VNCLEAN) == 0) {
950 if (buf_rb_tree_RB_INSERT(&vp->v_rbclean_tree, bp)) {
951 panic("reassignbuf: dup lblk vp %p bp %p",
954 bp->b_flags |= B_VNCLEAN;
956 if ((vp->v_flag & VONWORKLST) &&
957 RB_EMPTY(&vp->v_rbdirty_tree)) {
958 vp->v_flag &= ~VONWORKLST;
959 LIST_REMOVE(vp, v_synclist);
966 * Create a vnode for a block device.
967 * Used for mounting the root file system.
970 bdevvp(dev_t dev, struct vnode **vpp)
980 error = getspecialvnode(VT_NON, NULL, &spec_vnode_vops, &nvp, 0, 0);
987 vp->v_udev = dev->si_udev;
994 v_associate_rdev(struct vnode *vp, dev_t dev)
998 if (dev == NULL || dev == NODEV)
1000 if (dev_is_good(dev) == 0)
1002 KKASSERT(vp->v_rdev == NULL);
1005 vp->v_rdev = reference_dev(dev);
1006 lwkt_gettoken(&ilock, &spechash_token);
1007 SLIST_INSERT_HEAD(&dev->si_hlist, vp, v_specnext);
1008 lwkt_reltoken(&ilock);
1013 v_release_rdev(struct vnode *vp)
1018 if ((dev = vp->v_rdev) != NULL) {
1019 lwkt_gettoken(&ilock, &spechash_token);
1020 SLIST_REMOVE(&dev->si_hlist, vp, vnode, v_specnext);
1023 lwkt_reltoken(&ilock);
1028 * Add a vnode to the alias list hung off the dev_t. We only associate
1029 * the device number with the vnode. The actual device is not associated
1030 * until the vnode is opened (usually in spec_open()), and will be
1031 * disassociated on last close.
1034 addaliasu(struct vnode *nvp, udev_t nvp_udev)
1036 if (nvp->v_type != VBLK && nvp->v_type != VCHR)
1037 panic("addaliasu on non-special vnode");
1038 nvp->v_udev = nvp_udev;
1042 * Disassociate a vnode from its underlying filesystem.
1044 * The vnode must be VX locked and refd
1046 * If there are v_usecount references to the vnode other then ours we have
1047 * to VOP_CLOSE the vnode before we can deactivate and reclaim it.
1050 vclean(struct vnode *vp, int flags)
1057 * If the vnode has already been reclaimed we have nothing to do.
1059 if (vp->v_flag & VRECLAIMED)
1061 vp->v_flag |= VRECLAIMED;
1064 * Scrap the vfs cache
1066 while (cache_inval_vp(vp, 0) != 0) {
1067 printf("Warning: vnode %p clean/cache_resolution race detected\n", vp);
1068 tsleep(vp, 0, "vclninv", 2);
1072 * Check to see if the vnode is in use. If so we have to reference it
1073 * before we clean it out so that its count cannot fall to zero and
1074 * generate a race against ourselves to recycle it.
1076 active = (vp->v_usecount > 1);
1079 * Clean out any buffers associated with the vnode and destroy its
1080 * object, if it has one.
1082 vinvalbuf(vp, V_SAVE, 0, 0);
1084 if ((object = vp->v_object) != NULL) {
1085 if (object->ref_count == 0) {
1086 if ((object->flags & OBJ_DEAD) == 0)
1087 vm_object_terminate(object);
1089 vm_pager_deallocate(object);
1091 vp->v_flag &= ~VOBJBUF;
1093 KKASSERT((vp->v_flag & VOBJBUF) == 0);
1096 * If purging an active vnode (typically during a forced unmount
1097 * or reboot), it must be closed and deactivated before being
1098 * reclaimed. This isn't really all that safe, but what can
1101 * Note that neither of these routines unlocks the vnode.
1103 if (active && (flags & DOCLOSE)) {
1104 while ((n = vp->v_opencount) != 0) {
1105 if (vp->v_writecount)
1106 VOP_CLOSE(vp, FWRITE|FNONBLOCK);
1108 VOP_CLOSE(vp, FNONBLOCK);
1109 if (vp->v_opencount == n) {
1110 printf("Warning: unable to force-close"
1118 * If the vnode has not be deactivated, deactivated it.
1120 if ((vp->v_flag & VINACTIVE) == 0) {
1121 vp->v_flag |= VINACTIVE;
1126 * Reclaim the vnode.
1128 if (VOP_RECLAIM(vp))
1129 panic("vclean: cannot reclaim");
1132 * Done with purge, notify sleepers of the grim news.
1134 vp->v_ops = &dead_vnode_vops;
1140 * Eliminate all activity associated with the requested vnode
1141 * and with all vnodes aliased to the requested vnode.
1143 * The vnode must be referenced and vx_lock()'d
1145 * revoke { struct vnode *a_vp, int a_flags }
1148 vop_stdrevoke(struct vop_revoke_args *ap)
1150 struct vnode *vp, *vq;
1154 KASSERT((ap->a_flags & REVOKEALL) != 0, ("vop_revoke"));
1159 * If the vnode is already dead don't try to revoke it
1161 if (vp->v_flag & VRECLAIMED)
1165 * If the vnode has a device association, scrap all vnodes associated
1166 * with the device. Don't let the device disappear on us while we
1167 * are scrapping the vnodes.
1169 * The passed vp will probably show up in the list, do not VX lock
1172 if (vp->v_type != VCHR && vp->v_type != VBLK)
1174 if ((dev = vp->v_rdev) == NULL) {
1175 if ((dev = udev2dev(vp->v_udev, vp->v_type == VBLK)) == NODEV)
1179 lwkt_gettoken(&ilock, &spechash_token);
1180 while ((vq = SLIST_FIRST(&dev->si_hlist)) != NULL) {
1181 if (vp == vq || vx_get(vq) == 0) {
1182 if (vq == SLIST_FIRST(&dev->si_hlist))
1188 lwkt_reltoken(&ilock);
1194 * Recycle an unused vnode to the front of the free list.
1196 * Returns 1 if we were successfully able to recycle the vnode,
1200 vrecycle(struct vnode *vp)
1202 if (vp->v_usecount == 1) {
1210 * Eliminate all activity associated with a vnode in preparation for reuse.
1212 * The vnode must be VX locked and refd and will remain VX locked and refd
1213 * on return. This routine may be called with the vnode in any state, as
1214 * long as it is VX locked. The vnode will be cleaned out and marked
1215 * VRECLAIMED but will not actually be reused until all existing refs and
1218 * NOTE: This routine may be called on a vnode which has not yet been
1219 * already been deactivated (VOP_INACTIVE), or on a vnode which has
1220 * already been reclaimed.
1222 * This routine is not responsible for placing us back on the freelist.
1223 * Instead, it happens automatically when the caller releases the VX lock
1224 * (assuming there aren't any other references).
1227 vgone(struct vnode *vp)
1230 * assert that the VX lock is held. This is an absolute requirement
1231 * now for vgone() to be called.
1233 KKASSERT(vp->v_lock.lk_exclusivecount == 1);
1236 * Clean out the filesystem specific data and set the VRECLAIMED
1237 * bit. Also deactivate the vnode if necessary.
1239 vclean(vp, DOCLOSE);
1242 * Delete from old mount point vnode list, if on one.
1244 if (vp->v_mount != NULL)
1245 insmntque(vp, NULL);
1248 * If special device, remove it from special device alias list
1249 * if it is on one. This should normally only occur if a vnode is
1250 * being revoked as the device should otherwise have been released
1253 if ((vp->v_type == VBLK || vp->v_type == VCHR) && vp->v_rdev != NULL) {
1264 * Lookup a vnode by device number.
1267 vfinddev(dev_t dev, enum vtype type, struct vnode **vpp)
1272 lwkt_gettoken(&ilock, &spechash_token);
1273 SLIST_FOREACH(vp, &dev->si_hlist, v_specnext) {
1274 if (type == vp->v_type) {
1276 lwkt_reltoken(&ilock);
1280 lwkt_reltoken(&ilock);
1285 * Calculate the total number of references to a special device. This
1286 * routine may only be called for VBLK and VCHR vnodes since v_rdev is
1287 * an overloaded field. Since udev2dev can now return NODEV, we have
1288 * to check for a NULL v_rdev.
1291 count_dev(dev_t dev)
1297 if (SLIST_FIRST(&dev->si_hlist)) {
1298 lwkt_gettoken(&ilock, &spechash_token);
1299 SLIST_FOREACH(vp, &dev->si_hlist, v_specnext) {
1300 count += vp->v_usecount;
1302 lwkt_reltoken(&ilock);
1308 count_udev(udev_t udev)
1312 if ((dev = udev2dev(udev, 0)) == NODEV)
1314 return(count_dev(dev));
1318 vcount(struct vnode *vp)
1320 if (vp->v_rdev == NULL)
1322 return(count_dev(vp->v_rdev));
1326 * Initialize VMIO for a vnode. This routine MUST be called before a
1327 * VFS can issue buffer cache ops on a vnode. It is typically called
1328 * when a vnode is initialized from its inode.
1331 vinitvmio(struct vnode *vp, off_t filesize)
1337 if ((object = vp->v_object) == NULL) {
1338 object = vnode_pager_alloc(vp, filesize, 0, 0);
1340 * Dereference the reference we just created. This assumes
1341 * that the object is associated with the vp.
1343 object->ref_count--;
1346 if (object->flags & OBJ_DEAD) {
1348 tsleep(object, 0, "vodead", 0);
1349 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1353 KASSERT(vp->v_object != NULL, ("vinitvmio: NULL object"));
1354 vp->v_flag |= VOBJBUF;
1360 * Print out a description of a vnode.
1362 static char *typename[] =
1363 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD"};
1366 vprint(char *label, struct vnode *vp)
1371 printf("%s: %p: ", label, (void *)vp);
1373 printf("%p: ", (void *)vp);
1374 printf("type %s, usecount %d, writecount %d, refcount %d,",
1375 typename[vp->v_type], vp->v_usecount, vp->v_writecount,
1378 if (vp->v_flag & VROOT)
1379 strcat(buf, "|VROOT");
1380 if (vp->v_flag & VTEXT)
1381 strcat(buf, "|VTEXT");
1382 if (vp->v_flag & VSYSTEM)
1383 strcat(buf, "|VSYSTEM");
1384 if (vp->v_flag & VFREE)
1385 strcat(buf, "|VFREE");
1386 if (vp->v_flag & VOBJBUF)
1387 strcat(buf, "|VOBJBUF");
1389 printf(" flags (%s)", &buf[1]);
1390 if (vp->v_data == NULL) {
1399 #include <ddb/ddb.h>
1401 static int db_show_locked_vnodes(struct mount *mp, void *data);
1404 * List all of the locked vnodes in the system.
1405 * Called when debugging the kernel.
1407 DB_SHOW_COMMAND(lockedvnodes, lockedvnodes)
1409 printf("Locked vnodes\n");
1410 mountlist_scan(db_show_locked_vnodes, NULL,
1411 MNTSCAN_FORWARD|MNTSCAN_NOBUSY);
1415 db_show_locked_vnodes(struct mount *mp, void *data __unused)
1419 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
1420 if (VOP_ISLOCKED(vp, NULL))
1421 vprint((char *)0, vp);
1428 * Top level filesystem related information gathering.
1430 static int sysctl_ovfs_conf (SYSCTL_HANDLER_ARGS);
1433 vfs_sysctl(SYSCTL_HANDLER_ARGS)
1435 int *name = (int *)arg1 - 1; /* XXX */
1436 u_int namelen = arg2 + 1; /* XXX */
1437 struct vfsconf *vfsp;
1439 #if 1 || defined(COMPAT_PRELITE2)
1440 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
1442 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
1446 /* all sysctl names at this level are at least name and field */
1448 return (ENOTDIR); /* overloaded */
1449 if (name[0] != VFS_GENERIC) {
1450 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
1451 if (vfsp->vfc_typenum == name[0])
1454 return (EOPNOTSUPP);
1455 return ((*vfsp->vfc_vfsops->vfs_sysctl)(&name[1], namelen - 1,
1456 oldp, oldlenp, newp, newlen, p));
1460 case VFS_MAXTYPENUM:
1463 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int)));
1466 return (ENOTDIR); /* overloaded */
1467 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
1468 if (vfsp->vfc_typenum == name[2])
1471 return (EOPNOTSUPP);
1472 return (SYSCTL_OUT(req, vfsp, sizeof *vfsp));
1474 return (EOPNOTSUPP);
1477 SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD, vfs_sysctl,
1478 "Generic filesystem");
1480 #if 1 || defined(COMPAT_PRELITE2)
1483 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
1486 struct vfsconf *vfsp;
1487 struct ovfsconf ovfs;
1489 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
1490 bzero(&ovfs, sizeof(ovfs));
1491 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
1492 strcpy(ovfs.vfc_name, vfsp->vfc_name);
1493 ovfs.vfc_index = vfsp->vfc_typenum;
1494 ovfs.vfc_refcount = vfsp->vfc_refcount;
1495 ovfs.vfc_flags = vfsp->vfc_flags;
1496 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
1503 #endif /* 1 || COMPAT_PRELITE2 */
1506 * Check to see if a filesystem is mounted on a block device.
1509 vfs_mountedon(struct vnode *vp)
1513 if ((dev = vp->v_rdev) == NULL)
1514 dev = udev2dev(vp->v_udev, (vp->v_type == VBLK));
1515 if (dev != NODEV && dev->si_mountpoint)
1521 * Unmount all filesystems. The list is traversed in reverse order
1522 * of mounting to avoid dependencies.
1525 static int vfs_umountall_callback(struct mount *mp, void *data);
1528 vfs_unmountall(void)
1530 struct thread *td = curthread;
1533 if (td->td_proc == NULL)
1534 td = initproc->p_thread; /* XXX XXX use proc0 instead? */
1537 count = mountlist_scan(vfs_umountall_callback,
1538 NULL, MNTSCAN_REVERSE|MNTSCAN_NOBUSY);
1544 vfs_umountall_callback(struct mount *mp, void *data)
1548 error = dounmount(mp, MNT_FORCE);
1550 mountlist_remove(mp);
1551 printf("unmount of filesystem mounted from %s failed (",
1552 mp->mnt_stat.f_mntfromname);
1556 printf("%d)\n", error);
1562 * Build hash lists of net addresses and hang them off the mount point.
1563 * Called by ufs_mount() to set up the lists of export addresses.
1566 vfs_hang_addrlist(struct mount *mp, struct netexport *nep,
1567 struct export_args *argp)
1570 struct radix_node_head *rnh;
1572 struct radix_node *rn;
1573 struct sockaddr *saddr, *smask = 0;
1577 if (argp->ex_addrlen == 0) {
1578 if (mp->mnt_flag & MNT_DEFEXPORTED)
1580 np = &nep->ne_defexported;
1581 np->netc_exflags = argp->ex_flags;
1582 np->netc_anon = argp->ex_anon;
1583 np->netc_anon.cr_ref = 1;
1584 mp->mnt_flag |= MNT_DEFEXPORTED;
1588 if (argp->ex_addrlen < 0 || argp->ex_addrlen > MLEN)
1590 if (argp->ex_masklen < 0 || argp->ex_masklen > MLEN)
1593 i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
1594 np = (struct netcred *) malloc(i, M_NETADDR, M_WAITOK);
1595 bzero((caddr_t) np, i);
1596 saddr = (struct sockaddr *) (np + 1);
1597 if ((error = copyin(argp->ex_addr, (caddr_t) saddr, argp->ex_addrlen)))
1599 if (saddr->sa_len > argp->ex_addrlen)
1600 saddr->sa_len = argp->ex_addrlen;
1601 if (argp->ex_masklen) {
1602 smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen);
1603 error = copyin(argp->ex_mask, (caddr_t)smask, argp->ex_masklen);
1606 if (smask->sa_len > argp->ex_masklen)
1607 smask->sa_len = argp->ex_masklen;
1609 i = saddr->sa_family;
1610 if ((rnh = nep->ne_rtable[i]) == 0) {
1612 * Seems silly to initialize every AF when most are not used,
1613 * do so on demand here
1615 SLIST_FOREACH(dom, &domains, dom_next)
1616 if (dom->dom_family == i && dom->dom_rtattach) {
1617 dom->dom_rtattach((void **) &nep->ne_rtable[i],
1621 if ((rnh = nep->ne_rtable[i]) == 0) {
1626 rn = (*rnh->rnh_addaddr) ((char *) saddr, (char *) smask, rnh,
1628 if (rn == 0 || np != (struct netcred *) rn) { /* already exists */
1632 np->netc_exflags = argp->ex_flags;
1633 np->netc_anon = argp->ex_anon;
1634 np->netc_anon.cr_ref = 1;
1637 free(np, M_NETADDR);
1643 vfs_free_netcred(struct radix_node *rn, void *w)
1645 struct radix_node_head *rnh = (struct radix_node_head *) w;
1647 (*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, rnh);
1648 free((caddr_t) rn, M_NETADDR);
1653 * Free the net address hash lists that are hanging off the mount points.
1656 vfs_free_addrlist(struct netexport *nep)
1659 struct radix_node_head *rnh;
1661 for (i = 0; i <= AF_MAX; i++)
1662 if ((rnh = nep->ne_rtable[i])) {
1663 (*rnh->rnh_walktree) (rnh, vfs_free_netcred,
1665 free((caddr_t) rnh, M_RTABLE);
1666 nep->ne_rtable[i] = 0;
1671 vfs_export(struct mount *mp, struct netexport *nep, struct export_args *argp)
1675 if (argp->ex_flags & MNT_DELEXPORT) {
1676 if (mp->mnt_flag & MNT_EXPUBLIC) {
1677 vfs_setpublicfs(NULL, NULL, NULL);
1678 mp->mnt_flag &= ~MNT_EXPUBLIC;
1680 vfs_free_addrlist(nep);
1681 mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
1683 if (argp->ex_flags & MNT_EXPORTED) {
1684 if (argp->ex_flags & MNT_EXPUBLIC) {
1685 if ((error = vfs_setpublicfs(mp, nep, argp)) != 0)
1687 mp->mnt_flag |= MNT_EXPUBLIC;
1689 if ((error = vfs_hang_addrlist(mp, nep, argp)))
1691 mp->mnt_flag |= MNT_EXPORTED;
1698 * Set the publicly exported filesystem (WebNFS). Currently, only
1699 * one public filesystem is possible in the spec (RFC 2054 and 2055)
1702 vfs_setpublicfs(struct mount *mp, struct netexport *nep,
1703 struct export_args *argp)
1710 * mp == NULL -> invalidate the current info, the FS is
1711 * no longer exported. May be called from either vfs_export
1712 * or unmount, so check if it hasn't already been done.
1715 if (nfs_pub.np_valid) {
1716 nfs_pub.np_valid = 0;
1717 if (nfs_pub.np_index != NULL) {
1718 FREE(nfs_pub.np_index, M_TEMP);
1719 nfs_pub.np_index = NULL;
1726 * Only one allowed at a time.
1728 if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
1732 * Get real filehandle for root of exported FS.
1734 bzero((caddr_t)&nfs_pub.np_handle, sizeof(nfs_pub.np_handle));
1735 nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid;
1737 if ((error = VFS_ROOT(mp, &rvp)))
1740 if ((error = VFS_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid)))
1746 * If an indexfile was specified, pull it in.
1748 if (argp->ex_indexfile != NULL) {
1751 error = vn_get_namelen(rvp, &namelen);
1754 MALLOC(nfs_pub.np_index, char *, namelen, M_TEMP,
1756 error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
1757 namelen, (size_t *)0);
1760 * Check for illegal filenames.
1762 for (cp = nfs_pub.np_index; *cp; cp++) {
1770 FREE(nfs_pub.np_index, M_TEMP);
1775 nfs_pub.np_mount = mp;
1776 nfs_pub.np_valid = 1;
1781 vfs_export_lookup(struct mount *mp, struct netexport *nep,
1782 struct sockaddr *nam)
1785 struct radix_node_head *rnh;
1786 struct sockaddr *saddr;
1789 if (mp->mnt_flag & MNT_EXPORTED) {
1791 * Lookup in the export list first.
1795 rnh = nep->ne_rtable[saddr->sa_family];
1797 np = (struct netcred *)
1798 (*rnh->rnh_matchaddr)((char *)saddr,
1800 if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
1805 * If no address match, use the default if it exists.
1807 if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
1808 np = &nep->ne_defexported;
1814 * perform msync on all vnodes under a mount point. The mount point must
1815 * be locked. This code is also responsible for lazy-freeing unreferenced
1816 * vnodes whos VM objects no longer contain pages.
1818 * NOTE: MNT_WAIT still skips vnodes in the VXLOCK state.
1820 * NOTE: XXX VOP_PUTPAGES and friends requires that the vnode be locked,
1821 * but vnode_pager_putpages() doesn't lock the vnode. We have to do it
1822 * way up in this high level function.
1824 static int vfs_msync_scan1(struct mount *mp, struct vnode *vp, void *data);
1825 static int vfs_msync_scan2(struct mount *mp, struct vnode *vp, void *data);
1828 vfs_msync(struct mount *mp, int flags)
1832 vmsc_flags = VMSC_GETVP;
1833 if (flags != MNT_WAIT)
1834 vmsc_flags |= VMSC_NOWAIT;
1835 vmntvnodescan(mp, vmsc_flags, vfs_msync_scan1, vfs_msync_scan2,
1840 * scan1 is a fast pre-check. There could be hundreds of thousands of
1841 * vnodes, we cannot afford to do anything heavy weight until we have a
1842 * fairly good indication that there is work to do.
1846 vfs_msync_scan1(struct mount *mp, struct vnode *vp, void *data)
1848 int flags = (int)data;
1850 if ((vp->v_flag & VRECLAIMED) == 0) {
1851 if (vshouldfree(vp, 0))
1852 return(0); /* call scan2 */
1853 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
1854 (vp->v_flag & VOBJDIRTY) &&
1855 (flags == MNT_WAIT || VOP_ISLOCKED(vp, NULL) == 0)) {
1856 return(0); /* call scan2 */
1861 * do not call scan2, continue the loop
1867 * This callback is handed a locked vnode.
1871 vfs_msync_scan2(struct mount *mp, struct vnode *vp, void *data)
1874 int flags = (int)data;
1876 if (vp->v_flag & VRECLAIMED)
1879 if ((mp->mnt_flag & MNT_RDONLY) == 0 && (vp->v_flag & VOBJDIRTY)) {
1880 if ((obj = vp->v_object) != NULL) {
1881 vm_object_page_clean(obj, 0, 0,
1882 flags == MNT_WAIT ? OBJPC_SYNC : OBJPC_NOSYNC);
1889 * Record a process's interest in events which might happen to
1890 * a vnode. Because poll uses the historic select-style interface
1891 * internally, this routine serves as both the ``check for any
1892 * pending events'' and the ``record my interest in future events''
1893 * functions. (These are done together, while the lock is held,
1894 * to avoid race conditions.)
1897 vn_pollrecord(struct vnode *vp, int events)
1901 KKASSERT(curthread->td_proc != NULL);
1903 lwkt_gettoken(&ilock, &vp->v_pollinfo.vpi_token);
1904 if (vp->v_pollinfo.vpi_revents & events) {
1906 * This leaves events we are not interested
1907 * in available for the other process which
1908 * which presumably had requested them
1909 * (otherwise they would never have been
1912 events &= vp->v_pollinfo.vpi_revents;
1913 vp->v_pollinfo.vpi_revents &= ~events;
1915 lwkt_reltoken(&ilock);
1918 vp->v_pollinfo.vpi_events |= events;
1919 selrecord(curthread, &vp->v_pollinfo.vpi_selinfo);
1920 lwkt_reltoken(&ilock);
1925 * Note the occurrence of an event. If the VN_POLLEVENT macro is used,
1926 * it is possible for us to miss an event due to race conditions, but
1927 * that condition is expected to be rare, so for the moment it is the
1928 * preferred interface.
1931 vn_pollevent(struct vnode *vp, int events)
1935 lwkt_gettoken(&ilock, &vp->v_pollinfo.vpi_token);
1936 if (vp->v_pollinfo.vpi_events & events) {
1938 * We clear vpi_events so that we don't
1939 * call selwakeup() twice if two events are
1940 * posted before the polling process(es) is
1941 * awakened. This also ensures that we take at
1942 * most one selwakeup() if the polling process
1943 * is no longer interested. However, it does
1944 * mean that only one event can be noticed at
1945 * a time. (Perhaps we should only clear those
1946 * event bits which we note?) XXX
1948 vp->v_pollinfo.vpi_events = 0; /* &= ~events ??? */
1949 vp->v_pollinfo.vpi_revents |= events;
1950 selwakeup(&vp->v_pollinfo.vpi_selinfo);
1952 lwkt_reltoken(&ilock);
1956 * Wake up anyone polling on vp because it is being revoked.
1957 * This depends on dead_poll() returning POLLHUP for correct
1961 vn_pollgone(struct vnode *vp)
1965 lwkt_gettoken(&ilock, &vp->v_pollinfo.vpi_token);
1966 if (vp->v_pollinfo.vpi_events) {
1967 vp->v_pollinfo.vpi_events = 0;
1968 selwakeup(&vp->v_pollinfo.vpi_selinfo);
1970 lwkt_reltoken(&ilock);
1974 * extract the dev_t from a VBLK or VCHR. The vnode must have been opened
1975 * (or v_rdev might be NULL).
1978 vn_todev(struct vnode *vp)
1980 if (vp->v_type != VBLK && vp->v_type != VCHR)
1982 KKASSERT(vp->v_rdev != NULL);
1983 return (vp->v_rdev);
1987 * Check if vnode represents a disk device. The vnode does not need to be
1991 vn_isdisk(struct vnode *vp, int *errp)
1995 if (vp->v_type != VBLK && vp->v_type != VCHR) {
2001 if ((dev = vp->v_rdev) == NULL)
2002 dev = udev2dev(vp->v_udev, (vp->v_type == VBLK));
2003 if (dev == NULL || dev == NODEV) {
2008 if (dev_is_good(dev) == 0) {
2013 if ((dev_dflags(dev) & D_DISK) == 0) {
2023 #ifdef DEBUG_VFS_LOCKS
2026 assert_vop_locked(struct vnode *vp, const char *str)
2028 if (vp && !VOP_ISLOCKED(vp, NULL)) {
2029 panic("%s: %p is not locked shared but should be", str, vp);
2034 assert_vop_unlocked(struct vnode *vp, const char *str)
2037 if (VOP_ISLOCKED(vp, curthread) == LK_EXCLUSIVE) {
2038 panic("%s: %p is locked but should not be", str, vp);
2046 vn_get_namelen(struct vnode *vp, int *namelen)
2048 int error, retval[2];
2050 error = VOP_PATHCONF(vp, _PC_NAME_MAX, retval);
2058 vop_write_dirent(int *error, struct uio *uio, ino_t d_ino, uint8_t d_type,
2059 uint16_t d_namlen, const char *d_name)
2064 len = _DIRENT_RECLEN(d_namlen);
2065 if (len > uio->uio_resid)
2068 dp = malloc(len, M_TEMP, M_WAITOK | M_ZERO);
2071 dp->d_namlen = d_namlen;
2072 dp->d_type = d_type;
2073 bcopy(d_name, dp->d_name, d_namlen);
2075 *error = uiomove((caddr_t)dp, len, uio);