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.103 2007/05/08 02:31:42 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>
86 #include <sys/sysref2.h>
88 static MALLOC_DEFINE(M_NETADDR, "Export Host", "Export host address structure");
91 SYSCTL_INT(_debug, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0, "");
93 SYSCTL_INT(_vfs, OID_AUTO, fastdev, CTLFLAG_RW, &vfs_fastdev, 0, "");
95 enum vtype iftovt_tab[16] = {
96 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
97 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
100 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
101 S_IFSOCK, S_IFIFO, S_IFMT,
104 static int reassignbufcalls;
105 SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW,
106 &reassignbufcalls, 0, "");
107 static int reassignbufloops;
108 SYSCTL_INT(_vfs, OID_AUTO, reassignbufloops, CTLFLAG_RW,
109 &reassignbufloops, 0, "");
110 static int reassignbufsortgood;
111 SYSCTL_INT(_vfs, OID_AUTO, reassignbufsortgood, CTLFLAG_RW,
112 &reassignbufsortgood, 0, "");
113 static int reassignbufsortbad;
114 SYSCTL_INT(_vfs, OID_AUTO, reassignbufsortbad, CTLFLAG_RW,
115 &reassignbufsortbad, 0, "");
116 static int reassignbufmethod = 1;
117 SYSCTL_INT(_vfs, OID_AUTO, reassignbufmethod, CTLFLAG_RW,
118 &reassignbufmethod, 0, "");
120 int nfs_mount_type = -1;
121 static struct lwkt_token spechash_token;
122 struct nfs_public nfs_pub; /* publicly exported FS */
125 SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RW,
126 &desiredvnodes, 0, "Maximum number of vnodes");
128 static void vfs_free_addrlist (struct netexport *nep);
129 static int vfs_free_netcred (struct radix_node *rn, void *w);
130 static int vfs_hang_addrlist (struct mount *mp, struct netexport *nep,
131 struct export_args *argp);
133 extern int dev_ref_debug;
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 * Returns non-zero if the vnode is a candidate for lazy msyncing.
156 vshouldmsync(struct vnode *vp)
158 if (vp->v_auxrefs != 0 || vp->v_sysref.refcnt > 0)
159 return (0); /* other holders */
161 (vp->v_object->ref_count || vp->v_object->resident_page_count)) {
168 * Initialize the vnode management data structures.
170 * Called from vfsinit()
176 * Desired vnodes is a result of the physical page count
177 * and the size of kernel's heap. It scales in proportion
178 * to the amount of available physical memory. This can
179 * cause trouble on 64-bit and large memory platforms.
181 /* desiredvnodes = maxproc + vmstats.v_page_count / 4; */
183 min(maxproc + vmstats.v_page_count / 4,
185 (5 * (sizeof(struct vm_object) + sizeof(struct vnode))));
187 lwkt_token_init(&spechash_token);
191 * Knob to control the precision of file timestamps:
193 * 0 = seconds only; nanoseconds zeroed.
194 * 1 = seconds and nanoseconds, accurate within 1/HZ.
195 * 2 = seconds and nanoseconds, truncated to microseconds.
196 * >=3 = seconds and nanoseconds, maximum precision.
198 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC };
200 static int timestamp_precision = TSP_SEC;
201 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW,
202 ×tamp_precision, 0, "");
205 * Get a current timestamp.
208 vfs_timestamp(struct timespec *tsp)
212 switch (timestamp_precision) {
214 tsp->tv_sec = time_second;
222 TIMEVAL_TO_TIMESPEC(&tv, tsp);
232 * Set vnode attributes to VNOVAL
235 vattr_null(struct vattr *vap)
238 vap->va_size = VNOVAL;
239 vap->va_bytes = VNOVAL;
240 vap->va_mode = VNOVAL;
241 vap->va_nlink = VNOVAL;
242 vap->va_uid = VNOVAL;
243 vap->va_gid = VNOVAL;
244 vap->va_fsid = VNOVAL;
245 vap->va_fileid = VNOVAL;
246 vap->va_blocksize = VNOVAL;
247 vap->va_rdev = VNOVAL;
248 vap->va_atime.tv_sec = VNOVAL;
249 vap->va_atime.tv_nsec = VNOVAL;
250 vap->va_mtime.tv_sec = VNOVAL;
251 vap->va_mtime.tv_nsec = VNOVAL;
252 vap->va_ctime.tv_sec = VNOVAL;
253 vap->va_ctime.tv_nsec = VNOVAL;
254 vap->va_flags = VNOVAL;
255 vap->va_gen = VNOVAL;
257 vap->va_fsmid = VNOVAL;
261 * Flush out and invalidate all buffers associated with a vnode.
265 static int vinvalbuf_bp(struct buf *bp, void *data);
267 struct vinvalbuf_bp_info {
275 vupdatefsmid(struct vnode *vp)
277 atomic_set_int(&vp->v_flag, VFSMID);
281 vinvalbuf(struct vnode *vp, int flags, int slpflag, int slptimeo)
283 struct vinvalbuf_bp_info info;
288 * If we are being asked to save, call fsync to ensure that the inode
291 if (flags & V_SAVE) {
293 while (vp->v_track_write.bk_active) {
294 vp->v_track_write.bk_waitflag = 1;
295 error = tsleep(&vp->v_track_write, slpflag,
296 "vinvlbuf", slptimeo);
302 if (!RB_EMPTY(&vp->v_rbdirty_tree)) {
304 if ((error = VOP_FSYNC(vp, MNT_WAIT)) != 0)
307 if (vp->v_track_write.bk_active > 0 ||
308 !RB_EMPTY(&vp->v_rbdirty_tree))
309 panic("vinvalbuf: dirty bufs");
314 info.slptimeo = slptimeo;
315 info.lkflags = LK_EXCLUSIVE | LK_SLEEPFAIL;
316 if (slpflag & PCATCH)
317 info.lkflags |= LK_PCATCH;
322 * Flush the buffer cache until nothing is left.
324 while (!RB_EMPTY(&vp->v_rbclean_tree) ||
325 !RB_EMPTY(&vp->v_rbdirty_tree)) {
326 error = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree, NULL,
327 vinvalbuf_bp, &info);
329 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
330 vinvalbuf_bp, &info);
335 * Wait for I/O to complete. XXX needs cleaning up. The vnode can
336 * have write I/O in-progress but if there is a VM object then the
337 * VM object can also have read-I/O in-progress.
340 while (vp->v_track_write.bk_active > 0) {
341 vp->v_track_write.bk_waitflag = 1;
342 tsleep(&vp->v_track_write, 0, "vnvlbv", 0);
344 if ((object = vp->v_object) != NULL) {
345 while (object->paging_in_progress)
346 vm_object_pip_sleep(object, "vnvlbx");
348 } while (vp->v_track_write.bk_active > 0);
353 * Destroy the copy in the VM cache, too.
355 if ((object = vp->v_object) != NULL) {
356 vm_object_page_remove(object, 0, 0,
357 (flags & V_SAVE) ? TRUE : FALSE);
360 if (!RB_EMPTY(&vp->v_rbdirty_tree) || !RB_EMPTY(&vp->v_rbclean_tree))
361 panic("vinvalbuf: flush failed");
362 if (!RB_EMPTY(&vp->v_rbhash_tree))
363 panic("vinvalbuf: flush failed, buffers still present");
368 vinvalbuf_bp(struct buf *bp, void *data)
370 struct vinvalbuf_bp_info *info = data;
373 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
374 error = BUF_TIMELOCK(bp, info->lkflags,
375 "vinvalbuf", info->slptimeo);
385 KKASSERT(bp->b_vp == info->vp);
388 * XXX Since there are no node locks for NFS, I
389 * believe there is a slight chance that a delayed
390 * write will occur while sleeping just above, so
391 * check for it. Note that vfs_bio_awrite expects
392 * buffers to reside on a queue, while bwrite() and
395 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
396 (info->flags & V_SAVE)) {
397 if (bp->b_vp == info->vp) {
398 if (bp->b_flags & B_CLUSTEROK) {
402 bp->b_flags |= B_ASYNC;
409 } else if (info->flags & V_SAVE) {
411 * Cannot set B_NOCACHE on a clean buffer as this will
412 * destroy the VM backing store which might actually
413 * be dirty (and unsynchronized).
416 bp->b_flags |= (B_INVAL | B_RELBUF);
417 bp->b_flags &= ~B_ASYNC;
421 bp->b_flags |= (B_INVAL | B_NOCACHE | B_RELBUF);
422 bp->b_flags &= ~B_ASYNC;
429 * Truncate a file's buffer and pages to a specified length. This
430 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
433 * The vnode must be locked.
435 static int vtruncbuf_bp_trunc_cmp(struct buf *bp, void *data);
436 static int vtruncbuf_bp_trunc(struct buf *bp, void *data);
437 static int vtruncbuf_bp_metasync_cmp(struct buf *bp, void *data);
438 static int vtruncbuf_bp_metasync(struct buf *bp, void *data);
441 vtruncbuf(struct vnode *vp, off_t length, int blksize)
445 const char *filename;
448 * Round up to the *next* block, then destroy the buffers in question.
449 * Since we are only removing some of the buffers we must rely on the
450 * scan count to determine whether a loop is necessary.
452 if ((count = (int)(length % blksize)) != 0)
453 truncloffset = length + (blksize - count);
455 truncloffset = length;
459 count = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree,
460 vtruncbuf_bp_trunc_cmp,
461 vtruncbuf_bp_trunc, &truncloffset);
462 count += RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
463 vtruncbuf_bp_trunc_cmp,
464 vtruncbuf_bp_trunc, &truncloffset);
468 * For safety, fsync any remaining metadata if the file is not being
469 * truncated to 0. Since the metadata does not represent the entire
470 * dirty list we have to rely on the hit count to ensure that we get
475 count = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
476 vtruncbuf_bp_metasync_cmp,
477 vtruncbuf_bp_metasync, vp);
482 * Clean out any left over VM backing store.
486 vnode_pager_setsize(vp, length);
491 * It is possible to have in-progress I/O from buffers that were
492 * not part of the truncation. This should not happen if we
493 * are truncating to 0-length.
495 filename = TAILQ_FIRST(&vp->v_namecache) ?
496 TAILQ_FIRST(&vp->v_namecache)->nc_name : "?";
498 while ((count = vp->v_track_write.bk_active) > 0) {
499 vp->v_track_write.bk_waitflag = 1;
500 tsleep(&vp->v_track_write, 0, "vbtrunc", 0);
502 kprintf("Warning: vtruncbuf(): Had to wait for "
503 "%d buffer I/Os to finish in %s\n",
509 * Make sure no buffers were instantiated while we were trying
510 * to clean out the remaining VM pages. This could occur due
511 * to busy dirty VM pages being flushed out to disk.
514 count = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree,
515 vtruncbuf_bp_trunc_cmp,
516 vtruncbuf_bp_trunc, &truncloffset);
517 count += RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
518 vtruncbuf_bp_trunc_cmp,
519 vtruncbuf_bp_trunc, &truncloffset);
521 kprintf("Warning: vtruncbuf(): Had to re-clean %d "
522 "left over buffers in %s\n", count, filename);
532 * The callback buffer is beyond the new file EOF and must be destroyed.
533 * Note that the compare function must conform to the RB_SCAN's requirements.
537 vtruncbuf_bp_trunc_cmp(struct buf *bp, void *data)
539 if (bp->b_loffset >= *(off_t *)data)
546 vtruncbuf_bp_trunc(struct buf *bp, void *data)
549 * Do not try to use a buffer we cannot immediately lock, but sleep
550 * anyway to prevent a livelock. The code will loop until all buffers
553 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
554 if (BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL) == 0)
558 bp->b_flags |= (B_INVAL | B_RELBUF | B_NOCACHE);
559 bp->b_flags &= ~B_ASYNC;
566 * Fsync all meta-data after truncating a file to be non-zero. Only metadata
567 * blocks (with a negative loffset) are scanned.
568 * Note that the compare function must conform to the RB_SCAN's requirements.
571 vtruncbuf_bp_metasync_cmp(struct buf *bp, void *data)
573 if (bp->b_loffset < 0)
579 vtruncbuf_bp_metasync(struct buf *bp, void *data)
581 struct vnode *vp = data;
583 if (bp->b_flags & B_DELWRI) {
585 * Do not try to use a buffer we cannot immediately lock,
586 * but sleep anyway to prevent a livelock. The code will
587 * loop until all buffers can be acted upon.
589 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
590 if (BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL) == 0)
594 if (bp->b_vp == vp) {
595 bp->b_flags |= B_ASYNC;
597 bp->b_flags &= ~B_ASYNC;
608 * vfsync - implements a multipass fsync on a file which understands
609 * dependancies and meta-data. The passed vnode must be locked. The
610 * waitfor argument may be MNT_WAIT or MNT_NOWAIT, or MNT_LAZY.
612 * When fsyncing data asynchronously just do one consolidated pass starting
613 * with the most negative block number. This may not get all the data due
616 * When fsyncing data synchronously do a data pass, then a metadata pass,
617 * then do additional data+metadata passes to try to get all the data out.
619 static int vfsync_wait_output(struct vnode *vp,
620 int (*waitoutput)(struct vnode *, struct thread *));
621 static int vfsync_data_only_cmp(struct buf *bp, void *data);
622 static int vfsync_meta_only_cmp(struct buf *bp, void *data);
623 static int vfsync_lazy_range_cmp(struct buf *bp, void *data);
624 static int vfsync_bp(struct buf *bp, void *data);
633 int (*checkdef)(struct buf *);
637 vfsync(struct vnode *vp, int waitfor, int passes,
638 int (*checkdef)(struct buf *),
639 int (*waitoutput)(struct vnode *, struct thread *))
641 struct vfsync_info info;
644 bzero(&info, sizeof(info));
646 if ((info.checkdef = checkdef) == NULL)
649 crit_enter_id("vfsync");
654 * Lazy (filesystem syncer typ) Asynchronous plus limit the
655 * number of data (not meta) pages we try to flush to 1MB.
656 * A non-zero return means that lazy limit was reached.
658 info.lazylimit = 1024 * 1024;
660 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
661 vfsync_lazy_range_cmp, vfsync_bp, &info);
662 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
663 vfsync_meta_only_cmp, vfsync_bp, &info);
666 else if (!RB_EMPTY(&vp->v_rbdirty_tree))
667 vn_syncer_add_to_worklist(vp, 1);
672 * Asynchronous. Do a data-only pass and a meta-only pass.
675 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_data_only_cmp,
677 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_meta_only_cmp,
683 * Synchronous. Do a data-only pass, then a meta-data+data
684 * pass, then additional integrated passes to try to get
685 * all the dependancies flushed.
687 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_data_only_cmp,
689 error = vfsync_wait_output(vp, waitoutput);
691 info.skippedbufs = 0;
692 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
694 error = vfsync_wait_output(vp, waitoutput);
695 if (info.skippedbufs)
696 kprintf("Warning: vfsync skipped %d dirty bufs in pass2!\n", info.skippedbufs);
698 while (error == 0 && passes > 0 &&
699 !RB_EMPTY(&vp->v_rbdirty_tree)) {
701 info.synchronous = 1;
704 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
710 error = vfsync_wait_output(vp, waitoutput);
714 crit_exit_id("vfsync");
719 vfsync_wait_output(struct vnode *vp, int (*waitoutput)(struct vnode *, struct thread *))
723 while (vp->v_track_write.bk_active) {
724 vp->v_track_write.bk_waitflag = 1;
725 tsleep(&vp->v_track_write, 0, "fsfsn", 0);
728 error = waitoutput(vp, curthread);
733 vfsync_data_only_cmp(struct buf *bp, void *data)
735 if (bp->b_loffset < 0)
741 vfsync_meta_only_cmp(struct buf *bp, void *data)
743 if (bp->b_loffset < 0)
749 vfsync_lazy_range_cmp(struct buf *bp, void *data)
751 struct vfsync_info *info = data;
752 if (bp->b_loffset < info->vp->v_lazyw)
758 vfsync_bp(struct buf *bp, void *data)
760 struct vfsync_info *info = data;
761 struct vnode *vp = info->vp;
765 * if syncdeps is not set we do not try to write buffers which have
768 if (!info->synchronous && info->syncdeps == 0 && info->checkdef(bp))
772 * Ignore buffers that we cannot immediately lock. XXX
774 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
775 kprintf("Warning: vfsync_bp skipping dirty buffer %p\n", bp);
779 if ((bp->b_flags & B_DELWRI) == 0)
780 panic("vfsync_bp: buffer not dirty");
782 panic("vfsync_bp: buffer vp mismatch");
785 * B_NEEDCOMMIT (primarily used by NFS) is a state where the buffer
786 * has been written but an additional handshake with the device
787 * is required before we can dispose of the buffer. We have no idea
788 * how to do this so we have to skip these buffers.
790 if (bp->b_flags & B_NEEDCOMMIT) {
795 if (info->synchronous) {
797 * Synchronous flushing. An error may be returned.
800 crit_exit_id("vfsync");
802 crit_enter_id("vfsync");
805 * Asynchronous flushing. A negative return value simply
806 * stops the scan and is not considered an error. We use
807 * this to support limited MNT_LAZY flushes.
809 vp->v_lazyw = bp->b_loffset;
810 if ((vp->v_flag & VOBJBUF) && (bp->b_flags & B_CLUSTEROK)) {
811 info->lazycount += vfs_bio_awrite(bp);
813 info->lazycount += bp->b_bufsize;
815 crit_exit_id("vfsync");
817 crit_enter_id("vfsync");
819 if (info->lazylimit && info->lazycount >= info->lazylimit)
828 * Associate a buffer with a vnode.
831 bgetvp(struct vnode *vp, struct buf *bp)
833 KASSERT(bp->b_vp == NULL, ("bgetvp: not free"));
834 KKASSERT((bp->b_flags & (B_HASHED|B_DELWRI|B_VNCLEAN|B_VNDIRTY)) == 0);
838 * Insert onto list for new vnode.
842 bp->b_flags |= B_HASHED;
843 if (buf_rb_hash_RB_INSERT(&vp->v_rbhash_tree, bp))
844 panic("reassignbuf: dup lblk vp %p bp %p", vp, bp);
846 bp->b_flags |= B_VNCLEAN;
847 if (buf_rb_tree_RB_INSERT(&vp->v_rbclean_tree, bp))
848 panic("reassignbuf: dup lblk/clean vp %p bp %p", vp, bp);
853 * Disassociate a buffer from a vnode.
856 brelvp(struct buf *bp)
860 KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
863 * Delete from old vnode list, if on one.
867 if (bp->b_flags & (B_VNDIRTY | B_VNCLEAN)) {
868 if (bp->b_flags & B_VNDIRTY)
869 buf_rb_tree_RB_REMOVE(&vp->v_rbdirty_tree, bp);
871 buf_rb_tree_RB_REMOVE(&vp->v_rbclean_tree, bp);
872 bp->b_flags &= ~(B_VNDIRTY | B_VNCLEAN);
874 if (bp->b_flags & B_HASHED) {
875 buf_rb_hash_RB_REMOVE(&vp->v_rbhash_tree, bp);
876 bp->b_flags &= ~B_HASHED;
878 if ((vp->v_flag & VONWORKLST) && RB_EMPTY(&vp->v_rbdirty_tree)) {
879 vp->v_flag &= ~VONWORKLST;
880 LIST_REMOVE(vp, v_synclist);
888 * Reassign the buffer to the proper clean/dirty list based on B_DELWRI.
889 * This routine is called when the state of the B_DELWRI bit is changed.
892 reassignbuf(struct buf *bp)
894 struct vnode *vp = bp->b_vp;
897 KKASSERT(vp != NULL);
901 * B_PAGING flagged buffers cannot be reassigned because their vp
902 * is not fully linked in.
904 if (bp->b_flags & B_PAGING)
905 panic("cannot reassign paging buffer");
908 if (bp->b_flags & B_DELWRI) {
910 * Move to the dirty list, add the vnode to the worklist
912 if (bp->b_flags & B_VNCLEAN) {
913 buf_rb_tree_RB_REMOVE(&vp->v_rbclean_tree, bp);
914 bp->b_flags &= ~B_VNCLEAN;
916 if ((bp->b_flags & B_VNDIRTY) == 0) {
917 if (buf_rb_tree_RB_INSERT(&vp->v_rbdirty_tree, bp)) {
918 panic("reassignbuf: dup lblk vp %p bp %p",
921 bp->b_flags |= B_VNDIRTY;
923 if ((vp->v_flag & VONWORKLST) == 0) {
924 switch (vp->v_type) {
931 vp->v_rdev->si_mountpoint != NULL) {
939 vn_syncer_add_to_worklist(vp, delay);
943 * Move to the clean list, remove the vnode from the worklist
944 * if no dirty blocks remain.
946 if (bp->b_flags & B_VNDIRTY) {
947 buf_rb_tree_RB_REMOVE(&vp->v_rbdirty_tree, bp);
948 bp->b_flags &= ~B_VNDIRTY;
950 if ((bp->b_flags & B_VNCLEAN) == 0) {
951 if (buf_rb_tree_RB_INSERT(&vp->v_rbclean_tree, bp)) {
952 panic("reassignbuf: dup lblk vp %p bp %p",
955 bp->b_flags |= B_VNCLEAN;
957 if ((vp->v_flag & VONWORKLST) &&
958 RB_EMPTY(&vp->v_rbdirty_tree)) {
959 vp->v_flag &= ~VONWORKLST;
960 LIST_REMOVE(vp, v_synclist);
967 * Create a vnode for a block device.
968 * Used for mounting the root file system.
971 bdevvp(cdev_t dev, struct vnode **vpp)
981 error = getspecialvnode(VT_NON, NULL, &spec_vnode_vops_p, &nvp, 0, 0);
988 vp->v_udev = dev->si_udev;
995 v_associate_rdev(struct vnode *vp, cdev_t dev)
999 if (dev == NULL || dev == NULL)
1001 if (dev_is_good(dev) == 0)
1003 KKASSERT(vp->v_rdev == NULL);
1006 vp->v_rdev = reference_dev(dev);
1007 lwkt_gettoken(&ilock, &spechash_token);
1008 SLIST_INSERT_HEAD(&dev->si_hlist, vp, v_cdevnext);
1009 lwkt_reltoken(&ilock);
1014 v_release_rdev(struct vnode *vp)
1019 if ((dev = vp->v_rdev) != NULL) {
1020 lwkt_gettoken(&ilock, &spechash_token);
1021 SLIST_REMOVE(&dev->si_hlist, vp, vnode, v_cdevnext);
1024 lwkt_reltoken(&ilock);
1029 * Add a vnode to the alias list hung off the cdev_t. We only associate
1030 * the device number with the vnode. The actual device is not associated
1031 * until the vnode is opened (usually in spec_open()), and will be
1032 * disassociated on last close.
1035 addaliasu(struct vnode *nvp, udev_t nvp_udev)
1037 if (nvp->v_type != VBLK && nvp->v_type != VCHR)
1038 panic("addaliasu on non-special vnode");
1039 nvp->v_udev = nvp_udev;
1043 * Disassociate a vnode from its underlying filesystem.
1045 * The vnode must be VX locked and referenced. In all normal situations
1046 * there are no active references. If vclean_vxlocked() is called while
1047 * there are active references, the vnode is being ripped out and we have
1048 * to call VOP_CLOSE() as appropriate before we can reclaim it.
1051 vclean_vxlocked(struct vnode *vp, int flags)
1058 * If the vnode has already been reclaimed we have nothing to do.
1060 if (vp->v_flag & VRECLAIMED)
1062 vp->v_flag |= VRECLAIMED;
1065 * Scrap the vfs cache
1067 while (cache_inval_vp(vp, 0) != 0) {
1068 kprintf("Warning: vnode %p clean/cache_resolution race detected\n", vp);
1069 tsleep(vp, 0, "vclninv", 2);
1073 * Check to see if the vnode is in use. If so we have to reference it
1074 * before we clean it out so that its count cannot fall to zero and
1075 * generate a race against ourselves to recycle it.
1077 active = sysref_isactive(&vp->v_sysref);
1080 * Clean out any buffers associated with the vnode and destroy its
1081 * object, if it has one.
1083 vinvalbuf(vp, V_SAVE, 0, 0);
1086 * If purging an active vnode (typically during a forced unmount
1087 * or reboot), it must be closed and deactivated before being
1088 * reclaimed. This isn't really all that safe, but what can
1091 * Note that neither of these routines unlocks the vnode.
1093 if (active && (flags & DOCLOSE)) {
1094 while ((n = vp->v_opencount) != 0) {
1095 if (vp->v_writecount)
1096 VOP_CLOSE(vp, FWRITE|FNONBLOCK);
1098 VOP_CLOSE(vp, FNONBLOCK);
1099 if (vp->v_opencount == n) {
1100 kprintf("Warning: unable to force-close"
1108 * If the vnode has not be deactivated, deactivated it. Deactivation
1109 * can create new buffers and VM pages so we have to call vinvalbuf()
1110 * again to make sure they all get flushed.
1112 * This can occur if a file with a link count of 0 needs to be
1115 if ((vp->v_flag & VINACTIVE) == 0) {
1116 vp->v_flag |= VINACTIVE;
1118 vinvalbuf(vp, V_SAVE, 0, 0);
1122 * If the vnode has an object, destroy it.
1124 if ((object = vp->v_object) != NULL) {
1125 if (object->ref_count == 0) {
1126 if ((object->flags & OBJ_DEAD) == 0)
1127 vm_object_terminate(object);
1129 vm_pager_deallocate(object);
1131 vp->v_flag &= ~VOBJBUF;
1133 KKASSERT((vp->v_flag & VOBJBUF) == 0);
1137 * Reclaim the vnode.
1139 if (VOP_RECLAIM(vp))
1140 panic("vclean: cannot reclaim");
1143 * Done with purge, notify sleepers of the grim news.
1145 vp->v_ops = &dead_vnode_vops_p;
1151 * Eliminate all activity associated with the requested vnode
1152 * and with all vnodes aliased to the requested vnode.
1154 * The vnode must be referenced and vx_lock()'d
1156 * revoke { struct vnode *a_vp, int a_flags }
1159 vop_stdrevoke(struct vop_revoke_args *ap)
1161 struct vnode *vp, *vq;
1165 KASSERT((ap->a_flags & REVOKEALL) != 0, ("vop_revoke"));
1170 * If the vnode is already dead don't try to revoke it
1172 if (vp->v_flag & VRECLAIMED)
1176 * If the vnode has a device association, scrap all vnodes associated
1177 * with the device. Don't let the device disappear on us while we
1178 * are scrapping the vnodes.
1180 * The passed vp will probably show up in the list, do not VX lock
1183 if (vp->v_type != VCHR && vp->v_type != VBLK)
1185 if ((dev = vp->v_rdev) == NULL) {
1186 if ((dev = udev2dev(vp->v_udev, vp->v_type == VBLK)) == NULL)
1190 lwkt_gettoken(&ilock, &spechash_token);
1191 while ((vq = SLIST_FIRST(&dev->si_hlist)) != NULL) {
1194 if (vq == SLIST_FIRST(&dev->si_hlist))
1199 lwkt_reltoken(&ilock);
1205 * This is called when the object underlying a vnode is being destroyed,
1206 * such as in a remove(). Try to recycle the vnode immediately if the
1207 * only active reference is our reference.
1210 vrecycle(struct vnode *vp)
1212 if (vp->v_sysref.refcnt == 1) {
1220 * Eliminate all activity associated with a vnode in preparation for reuse.
1222 * The vnode must be VX locked and refd and will remain VX locked and refd
1223 * on return. This routine may be called with the vnode in any state, as
1224 * long as it is VX locked. The vnode will be cleaned out and marked
1225 * VRECLAIMED but will not actually be reused until all existing refs and
1228 * NOTE: This routine may be called on a vnode which has not yet been
1229 * already been deactivated (VOP_INACTIVE), or on a vnode which has
1230 * already been reclaimed.
1232 * This routine is not responsible for placing us back on the freelist.
1233 * Instead, it happens automatically when the caller releases the VX lock
1234 * (assuming there aren't any other references).
1238 vgone_vxlocked(struct vnode *vp)
1241 * assert that the VX lock is held. This is an absolute requirement
1242 * now for vgone_vxlocked() to be called.
1244 KKASSERT(vp->v_lock.lk_exclusivecount == 1);
1247 * Clean out the filesystem specific data and set the VRECLAIMED
1248 * bit. Also deactivate the vnode if necessary.
1250 vclean_vxlocked(vp, DOCLOSE);
1253 * Delete from old mount point vnode list, if on one.
1255 if (vp->v_mount != NULL)
1256 insmntque(vp, NULL);
1259 * If special device, remove it from special device alias list
1260 * if it is on one. This should normally only occur if a vnode is
1261 * being revoked as the device should otherwise have been released
1264 if ((vp->v_type == VBLK || vp->v_type == VCHR) && vp->v_rdev != NULL) {
1275 * Lookup a vnode by device number.
1278 vfinddev(cdev_t dev, enum vtype type, struct vnode **vpp)
1283 lwkt_gettoken(&ilock, &spechash_token);
1284 SLIST_FOREACH(vp, &dev->si_hlist, v_cdevnext) {
1285 if (type == vp->v_type) {
1287 lwkt_reltoken(&ilock);
1291 lwkt_reltoken(&ilock);
1296 * Calculate the total number of references to a special device. This
1297 * routine may only be called for VBLK and VCHR vnodes since v_rdev is
1298 * an overloaded field. Since udev2dev can now return NULL, we have
1299 * to check for a NULL v_rdev.
1302 count_dev(cdev_t dev)
1308 if (SLIST_FIRST(&dev->si_hlist)) {
1309 lwkt_gettoken(&ilock, &spechash_token);
1310 SLIST_FOREACH(vp, &dev->si_hlist, v_cdevnext) {
1311 if (vp->v_sysref.refcnt > 0)
1312 count += vp->v_sysref.refcnt;
1314 lwkt_reltoken(&ilock);
1320 count_udev(udev_t udev)
1324 if ((dev = udev2dev(udev, 0)) == NULL)
1326 return(count_dev(dev));
1330 vcount(struct vnode *vp)
1332 if (vp->v_rdev == NULL)
1334 return(count_dev(vp->v_rdev));
1338 * Initialize VMIO for a vnode. This routine MUST be called before a
1339 * VFS can issue buffer cache ops on a vnode. It is typically called
1340 * when a vnode is initialized from its inode.
1343 vinitvmio(struct vnode *vp, off_t filesize)
1349 if ((object = vp->v_object) == NULL) {
1350 object = vnode_pager_alloc(vp, filesize, 0, 0);
1352 * Dereference the reference we just created. This assumes
1353 * that the object is associated with the vp.
1355 object->ref_count--;
1358 if (object->flags & OBJ_DEAD) {
1360 tsleep(object, 0, "vodead", 0);
1361 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1365 KASSERT(vp->v_object != NULL, ("vinitvmio: NULL object"));
1366 vp->v_flag |= VOBJBUF;
1372 * Print out a description of a vnode.
1374 static char *typename[] =
1375 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD"};
1378 vprint(char *label, struct vnode *vp)
1383 kprintf("%s: %p: ", label, (void *)vp);
1385 kprintf("%p: ", (void *)vp);
1386 kprintf("type %s, sysrefs %d, writecount %d, holdcnt %d,",
1387 typename[vp->v_type],
1388 vp->v_sysref.refcnt, vp->v_writecount, vp->v_auxrefs);
1390 if (vp->v_flag & VROOT)
1391 strcat(buf, "|VROOT");
1392 if (vp->v_flag & VTEXT)
1393 strcat(buf, "|VTEXT");
1394 if (vp->v_flag & VSYSTEM)
1395 strcat(buf, "|VSYSTEM");
1396 if (vp->v_flag & VFREE)
1397 strcat(buf, "|VFREE");
1398 if (vp->v_flag & VOBJBUF)
1399 strcat(buf, "|VOBJBUF");
1401 kprintf(" flags (%s)", &buf[1]);
1402 if (vp->v_data == NULL) {
1411 #include <ddb/ddb.h>
1413 static int db_show_locked_vnodes(struct mount *mp, void *data);
1416 * List all of the locked vnodes in the system.
1417 * Called when debugging the kernel.
1419 DB_SHOW_COMMAND(lockedvnodes, lockedvnodes)
1421 kprintf("Locked vnodes\n");
1422 mountlist_scan(db_show_locked_vnodes, NULL,
1423 MNTSCAN_FORWARD|MNTSCAN_NOBUSY);
1427 db_show_locked_vnodes(struct mount *mp, void *data __unused)
1431 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
1432 if (vn_islocked(vp))
1433 vprint((char *)0, vp);
1440 * Top level filesystem related information gathering.
1442 static int sysctl_ovfs_conf (SYSCTL_HANDLER_ARGS);
1445 vfs_sysctl(SYSCTL_HANDLER_ARGS)
1447 int *name = (int *)arg1 - 1; /* XXX */
1448 u_int namelen = arg2 + 1; /* XXX */
1449 struct vfsconf *vfsp;
1451 #if 1 || defined(COMPAT_PRELITE2)
1452 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
1454 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
1458 /* all sysctl names at this level are at least name and field */
1460 return (ENOTDIR); /* overloaded */
1461 if (name[0] != VFS_GENERIC) {
1462 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
1463 if (vfsp->vfc_typenum == name[0])
1466 return (EOPNOTSUPP);
1467 return ((*vfsp->vfc_vfsops->vfs_sysctl)(&name[1], namelen - 1,
1468 oldp, oldlenp, newp, newlen, p));
1472 case VFS_MAXTYPENUM:
1475 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int)));
1478 return (ENOTDIR); /* overloaded */
1479 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
1480 if (vfsp->vfc_typenum == name[2])
1483 return (EOPNOTSUPP);
1484 return (SYSCTL_OUT(req, vfsp, sizeof *vfsp));
1486 return (EOPNOTSUPP);
1489 SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD, vfs_sysctl,
1490 "Generic filesystem");
1492 #if 1 || defined(COMPAT_PRELITE2)
1495 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
1498 struct vfsconf *vfsp;
1499 struct ovfsconf ovfs;
1501 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
1502 bzero(&ovfs, sizeof(ovfs));
1503 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
1504 strcpy(ovfs.vfc_name, vfsp->vfc_name);
1505 ovfs.vfc_index = vfsp->vfc_typenum;
1506 ovfs.vfc_refcount = vfsp->vfc_refcount;
1507 ovfs.vfc_flags = vfsp->vfc_flags;
1508 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
1515 #endif /* 1 || COMPAT_PRELITE2 */
1518 * Check to see if a filesystem is mounted on a block device.
1521 vfs_mountedon(struct vnode *vp)
1525 if ((dev = vp->v_rdev) == NULL)
1526 dev = udev2dev(vp->v_udev, (vp->v_type == VBLK));
1527 if (dev != NULL && dev->si_mountpoint)
1533 * Unmount all filesystems. The list is traversed in reverse order
1534 * of mounting to avoid dependencies.
1537 static int vfs_umountall_callback(struct mount *mp, void *data);
1540 vfs_unmountall(void)
1545 count = mountlist_scan(vfs_umountall_callback,
1546 NULL, MNTSCAN_REVERSE|MNTSCAN_NOBUSY);
1552 vfs_umountall_callback(struct mount *mp, void *data)
1556 error = dounmount(mp, MNT_FORCE);
1558 mountlist_remove(mp);
1559 kprintf("unmount of filesystem mounted from %s failed (",
1560 mp->mnt_stat.f_mntfromname);
1564 kprintf("%d)\n", error);
1570 * Build hash lists of net addresses and hang them off the mount point.
1571 * Called by ufs_mount() to set up the lists of export addresses.
1574 vfs_hang_addrlist(struct mount *mp, struct netexport *nep,
1575 struct export_args *argp)
1578 struct radix_node_head *rnh;
1580 struct radix_node *rn;
1581 struct sockaddr *saddr, *smask = 0;
1585 if (argp->ex_addrlen == 0) {
1586 if (mp->mnt_flag & MNT_DEFEXPORTED)
1588 np = &nep->ne_defexported;
1589 np->netc_exflags = argp->ex_flags;
1590 np->netc_anon = argp->ex_anon;
1591 np->netc_anon.cr_ref = 1;
1592 mp->mnt_flag |= MNT_DEFEXPORTED;
1596 if (argp->ex_addrlen < 0 || argp->ex_addrlen > MLEN)
1598 if (argp->ex_masklen < 0 || argp->ex_masklen > MLEN)
1601 i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
1602 np = (struct netcred *) kmalloc(i, M_NETADDR, M_WAITOK);
1603 bzero((caddr_t) np, i);
1604 saddr = (struct sockaddr *) (np + 1);
1605 if ((error = copyin(argp->ex_addr, (caddr_t) saddr, argp->ex_addrlen)))
1607 if (saddr->sa_len > argp->ex_addrlen)
1608 saddr->sa_len = argp->ex_addrlen;
1609 if (argp->ex_masklen) {
1610 smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen);
1611 error = copyin(argp->ex_mask, (caddr_t)smask, argp->ex_masklen);
1614 if (smask->sa_len > argp->ex_masklen)
1615 smask->sa_len = argp->ex_masklen;
1617 i = saddr->sa_family;
1618 if ((rnh = nep->ne_rtable[i]) == 0) {
1620 * Seems silly to initialize every AF when most are not used,
1621 * do so on demand here
1623 SLIST_FOREACH(dom, &domains, dom_next)
1624 if (dom->dom_family == i && dom->dom_rtattach) {
1625 dom->dom_rtattach((void **) &nep->ne_rtable[i],
1629 if ((rnh = nep->ne_rtable[i]) == 0) {
1634 rn = (*rnh->rnh_addaddr) ((char *) saddr, (char *) smask, rnh,
1636 if (rn == 0 || np != (struct netcred *) rn) { /* already exists */
1640 np->netc_exflags = argp->ex_flags;
1641 np->netc_anon = argp->ex_anon;
1642 np->netc_anon.cr_ref = 1;
1645 kfree(np, M_NETADDR);
1651 vfs_free_netcred(struct radix_node *rn, void *w)
1653 struct radix_node_head *rnh = (struct radix_node_head *) w;
1655 (*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, rnh);
1656 kfree((caddr_t) rn, M_NETADDR);
1661 * Free the net address hash lists that are hanging off the mount points.
1664 vfs_free_addrlist(struct netexport *nep)
1667 struct radix_node_head *rnh;
1669 for (i = 0; i <= AF_MAX; i++)
1670 if ((rnh = nep->ne_rtable[i])) {
1671 (*rnh->rnh_walktree) (rnh, vfs_free_netcred,
1673 kfree((caddr_t) rnh, M_RTABLE);
1674 nep->ne_rtable[i] = 0;
1679 vfs_export(struct mount *mp, struct netexport *nep, struct export_args *argp)
1683 if (argp->ex_flags & MNT_DELEXPORT) {
1684 if (mp->mnt_flag & MNT_EXPUBLIC) {
1685 vfs_setpublicfs(NULL, NULL, NULL);
1686 mp->mnt_flag &= ~MNT_EXPUBLIC;
1688 vfs_free_addrlist(nep);
1689 mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
1691 if (argp->ex_flags & MNT_EXPORTED) {
1692 if (argp->ex_flags & MNT_EXPUBLIC) {
1693 if ((error = vfs_setpublicfs(mp, nep, argp)) != 0)
1695 mp->mnt_flag |= MNT_EXPUBLIC;
1697 if ((error = vfs_hang_addrlist(mp, nep, argp)))
1699 mp->mnt_flag |= MNT_EXPORTED;
1706 * Set the publicly exported filesystem (WebNFS). Currently, only
1707 * one public filesystem is possible in the spec (RFC 2054 and 2055)
1710 vfs_setpublicfs(struct mount *mp, struct netexport *nep,
1711 struct export_args *argp)
1718 * mp == NULL -> invalidate the current info, the FS is
1719 * no longer exported. May be called from either vfs_export
1720 * or unmount, so check if it hasn't already been done.
1723 if (nfs_pub.np_valid) {
1724 nfs_pub.np_valid = 0;
1725 if (nfs_pub.np_index != NULL) {
1726 FREE(nfs_pub.np_index, M_TEMP);
1727 nfs_pub.np_index = NULL;
1734 * Only one allowed at a time.
1736 if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
1740 * Get real filehandle for root of exported FS.
1742 bzero((caddr_t)&nfs_pub.np_handle, sizeof(nfs_pub.np_handle));
1743 nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid;
1745 if ((error = VFS_ROOT(mp, &rvp)))
1748 if ((error = VFS_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid)))
1754 * If an indexfile was specified, pull it in.
1756 if (argp->ex_indexfile != NULL) {
1759 error = vn_get_namelen(rvp, &namelen);
1762 MALLOC(nfs_pub.np_index, char *, namelen, M_TEMP,
1764 error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
1765 namelen, (size_t *)0);
1768 * Check for illegal filenames.
1770 for (cp = nfs_pub.np_index; *cp; cp++) {
1778 FREE(nfs_pub.np_index, M_TEMP);
1783 nfs_pub.np_mount = mp;
1784 nfs_pub.np_valid = 1;
1789 vfs_export_lookup(struct mount *mp, struct netexport *nep,
1790 struct sockaddr *nam)
1793 struct radix_node_head *rnh;
1794 struct sockaddr *saddr;
1797 if (mp->mnt_flag & MNT_EXPORTED) {
1799 * Lookup in the export list first.
1803 rnh = nep->ne_rtable[saddr->sa_family];
1805 np = (struct netcred *)
1806 (*rnh->rnh_matchaddr)((char *)saddr,
1808 if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
1813 * If no address match, use the default if it exists.
1815 if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
1816 np = &nep->ne_defexported;
1822 * perform msync on all vnodes under a mount point. The mount point must
1823 * be locked. This code is also responsible for lazy-freeing unreferenced
1824 * vnodes whos VM objects no longer contain pages.
1826 * NOTE: MNT_WAIT still skips vnodes in the VXLOCK state.
1828 * NOTE: XXX VOP_PUTPAGES and friends requires that the vnode be locked,
1829 * but vnode_pager_putpages() doesn't lock the vnode. We have to do it
1830 * way up in this high level function.
1832 static int vfs_msync_scan1(struct mount *mp, struct vnode *vp, void *data);
1833 static int vfs_msync_scan2(struct mount *mp, struct vnode *vp, void *data);
1836 vfs_msync(struct mount *mp, int flags)
1840 vmsc_flags = VMSC_GETVP;
1841 if (flags != MNT_WAIT)
1842 vmsc_flags |= VMSC_NOWAIT;
1843 vmntvnodescan(mp, vmsc_flags, vfs_msync_scan1, vfs_msync_scan2,
1848 * scan1 is a fast pre-check. There could be hundreds of thousands of
1849 * vnodes, we cannot afford to do anything heavy weight until we have a
1850 * fairly good indication that there is work to do.
1854 vfs_msync_scan1(struct mount *mp, struct vnode *vp, void *data)
1856 int flags = (int)data;
1858 if ((vp->v_flag & VRECLAIMED) == 0) {
1859 if (vshouldmsync(vp))
1860 return(0); /* call scan2 */
1861 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
1862 (vp->v_flag & VOBJDIRTY) &&
1863 (flags == MNT_WAIT || vn_islocked(vp) == 0)) {
1864 return(0); /* call scan2 */
1869 * do not call scan2, continue the loop
1875 * This callback is handed a locked vnode.
1879 vfs_msync_scan2(struct mount *mp, struct vnode *vp, void *data)
1882 int flags = (int)data;
1884 if (vp->v_flag & VRECLAIMED)
1887 if ((mp->mnt_flag & MNT_RDONLY) == 0 && (vp->v_flag & VOBJDIRTY)) {
1888 if ((obj = vp->v_object) != NULL) {
1889 vm_object_page_clean(obj, 0, 0,
1890 flags == MNT_WAIT ? OBJPC_SYNC : OBJPC_NOSYNC);
1897 * Record a process's interest in events which might happen to
1898 * a vnode. Because poll uses the historic select-style interface
1899 * internally, this routine serves as both the ``check for any
1900 * pending events'' and the ``record my interest in future events''
1901 * functions. (These are done together, while the lock is held,
1902 * to avoid race conditions.)
1905 vn_pollrecord(struct vnode *vp, int events)
1909 KKASSERT(curthread->td_proc != NULL);
1911 lwkt_gettoken(&ilock, &vp->v_pollinfo.vpi_token);
1912 if (vp->v_pollinfo.vpi_revents & events) {
1914 * This leaves events we are not interested
1915 * in available for the other process which
1916 * which presumably had requested them
1917 * (otherwise they would never have been
1920 events &= vp->v_pollinfo.vpi_revents;
1921 vp->v_pollinfo.vpi_revents &= ~events;
1923 lwkt_reltoken(&ilock);
1926 vp->v_pollinfo.vpi_events |= events;
1927 selrecord(curthread, &vp->v_pollinfo.vpi_selinfo);
1928 lwkt_reltoken(&ilock);
1933 * Note the occurrence of an event. If the VN_POLLEVENT macro is used,
1934 * it is possible for us to miss an event due to race conditions, but
1935 * that condition is expected to be rare, so for the moment it is the
1936 * preferred interface.
1939 vn_pollevent(struct vnode *vp, int events)
1943 lwkt_gettoken(&ilock, &vp->v_pollinfo.vpi_token);
1944 if (vp->v_pollinfo.vpi_events & events) {
1946 * We clear vpi_events so that we don't
1947 * call selwakeup() twice if two events are
1948 * posted before the polling process(es) is
1949 * awakened. This also ensures that we take at
1950 * most one selwakeup() if the polling process
1951 * is no longer interested. However, it does
1952 * mean that only one event can be noticed at
1953 * a time. (Perhaps we should only clear those
1954 * event bits which we note?) XXX
1956 vp->v_pollinfo.vpi_events = 0; /* &= ~events ??? */
1957 vp->v_pollinfo.vpi_revents |= events;
1958 selwakeup(&vp->v_pollinfo.vpi_selinfo);
1960 lwkt_reltoken(&ilock);
1964 * Wake up anyone polling on vp because it is being revoked.
1965 * This depends on dead_poll() returning POLLHUP for correct
1969 vn_pollgone(struct vnode *vp)
1973 lwkt_gettoken(&ilock, &vp->v_pollinfo.vpi_token);
1974 if (vp->v_pollinfo.vpi_events) {
1975 vp->v_pollinfo.vpi_events = 0;
1976 selwakeup(&vp->v_pollinfo.vpi_selinfo);
1978 lwkt_reltoken(&ilock);
1982 * extract the cdev_t from a VBLK or VCHR. The vnode must have been opened
1983 * (or v_rdev might be NULL).
1986 vn_todev(struct vnode *vp)
1988 if (vp->v_type != VBLK && vp->v_type != VCHR)
1990 KKASSERT(vp->v_rdev != NULL);
1991 return (vp->v_rdev);
1995 * Check if vnode represents a disk device. The vnode does not need to be
1999 vn_isdisk(struct vnode *vp, int *errp)
2003 if (vp->v_type != VBLK && vp->v_type != VCHR) {
2009 if ((dev = vp->v_rdev) == NULL)
2010 dev = udev2dev(vp->v_udev, (vp->v_type == VBLK));
2016 if (dev_is_good(dev) == 0) {
2021 if ((dev_dflags(dev) & D_DISK) == 0) {
2032 vn_get_namelen(struct vnode *vp, int *namelen)
2034 int error, retval[2];
2036 error = VOP_PATHCONF(vp, _PC_NAME_MAX, retval);
2044 vop_write_dirent(int *error, struct uio *uio, ino_t d_ino, uint8_t d_type,
2045 uint16_t d_namlen, const char *d_name)
2050 len = _DIRENT_RECLEN(d_namlen);
2051 if (len > uio->uio_resid)
2054 dp = kmalloc(len, M_TEMP, M_WAITOK | M_ZERO);
2057 dp->d_namlen = d_namlen;
2058 dp->d_type = d_type;
2059 bcopy(d_name, dp->d_name, d_namlen);
2061 *error = uiomove((caddr_t)dp, len, uio);