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.92 2006/08/09 22:47:32 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;
135 * Red black tree functions
137 static int rb_buf_compare(struct buf *b1, struct buf *b2);
138 RB_GENERATE2(buf_rb_tree, buf, b_rbnode, rb_buf_compare, off_t, b_loffset);
139 RB_GENERATE2(buf_rb_hash, buf, b_rbhash, rb_buf_compare, off_t, b_loffset);
142 rb_buf_compare(struct buf *b1, struct buf *b2)
144 if (b1->b_loffset < b2->b_loffset)
146 if (b1->b_loffset > b2->b_loffset)
152 * Returns non-zero if the vnode is a candidate for lazy msyncing.
155 vshouldmsync(struct vnode *vp, int usecount)
157 if (vp->v_holdcnt != 0 || vp->v_usecount != usecount)
158 return (0); /* other holders */
160 (vp->v_object->ref_count || vp->v_object->resident_page_count)) {
167 * Initialize the vnode management data structures.
169 * Called from vfsinit()
175 * Desired vnodes is a result of the physical page count
176 * and the size of kernel's heap. It scales in proportion
177 * to the amount of available physical memory. This can
178 * cause trouble on 64-bit and large memory platforms.
180 /* desiredvnodes = maxproc + vmstats.v_page_count / 4; */
182 min(maxproc + vmstats.v_page_count /4,
183 2 * (VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS) /
184 (5 * (sizeof(struct vm_object) + sizeof(struct vnode))));
186 lwkt_token_init(&spechash_token);
190 * Knob to control the precision of file timestamps:
192 * 0 = seconds only; nanoseconds zeroed.
193 * 1 = seconds and nanoseconds, accurate within 1/HZ.
194 * 2 = seconds and nanoseconds, truncated to microseconds.
195 * >=3 = seconds and nanoseconds, maximum precision.
197 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC };
199 static int timestamp_precision = TSP_SEC;
200 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW,
201 ×tamp_precision, 0, "");
204 * Get a current timestamp.
207 vfs_timestamp(struct timespec *tsp)
211 switch (timestamp_precision) {
213 tsp->tv_sec = time_second;
221 TIMEVAL_TO_TIMESPEC(&tv, tsp);
231 * Set vnode attributes to VNOVAL
234 vattr_null(struct vattr *vap)
237 vap->va_size = VNOVAL;
238 vap->va_bytes = VNOVAL;
239 vap->va_mode = VNOVAL;
240 vap->va_nlink = VNOVAL;
241 vap->va_uid = VNOVAL;
242 vap->va_gid = VNOVAL;
243 vap->va_fsid = VNOVAL;
244 vap->va_fileid = VNOVAL;
245 vap->va_blocksize = VNOVAL;
246 vap->va_rdev = VNOVAL;
247 vap->va_atime.tv_sec = VNOVAL;
248 vap->va_atime.tv_nsec = VNOVAL;
249 vap->va_mtime.tv_sec = VNOVAL;
250 vap->va_mtime.tv_nsec = VNOVAL;
251 vap->va_ctime.tv_sec = VNOVAL;
252 vap->va_ctime.tv_nsec = VNOVAL;
253 vap->va_flags = VNOVAL;
254 vap->va_gen = VNOVAL;
256 vap->va_fsmid = VNOVAL;
260 * Flush out and invalidate all buffers associated with a vnode.
264 static int vinvalbuf_bp(struct buf *bp, void *data);
266 struct vinvalbuf_bp_info {
274 vupdatefsmid(struct vnode *vp)
276 atomic_set_int(&vp->v_flag, VFSMID);
280 vinvalbuf(struct vnode *vp, int flags, int slpflag, int slptimeo)
282 struct vinvalbuf_bp_info info;
287 * If we are being asked to save, call fsync to ensure that the inode
290 if (flags & V_SAVE) {
292 while (vp->v_track_write.bk_active) {
293 vp->v_track_write.bk_waitflag = 1;
294 error = tsleep(&vp->v_track_write, slpflag,
295 "vinvlbuf", slptimeo);
301 if (!RB_EMPTY(&vp->v_rbdirty_tree)) {
303 if ((error = VOP_FSYNC(vp, MNT_WAIT)) != 0)
306 if (vp->v_track_write.bk_active > 0 ||
307 !RB_EMPTY(&vp->v_rbdirty_tree))
308 panic("vinvalbuf: dirty bufs");
313 info.slptimeo = slptimeo;
314 info.lkflags = LK_EXCLUSIVE | LK_SLEEPFAIL;
315 if (slpflag & PCATCH)
316 info.lkflags |= LK_PCATCH;
321 * Flush the buffer cache until nothing is left.
323 while (!RB_EMPTY(&vp->v_rbclean_tree) ||
324 !RB_EMPTY(&vp->v_rbdirty_tree)) {
325 error = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree, NULL,
326 vinvalbuf_bp, &info);
328 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
329 vinvalbuf_bp, &info);
334 * Wait for I/O to complete. XXX needs cleaning up. The vnode can
335 * have write I/O in-progress but if there is a VM object then the
336 * VM object can also have read-I/O in-progress.
339 while (vp->v_track_write.bk_active > 0) {
340 vp->v_track_write.bk_waitflag = 1;
341 tsleep(&vp->v_track_write, 0, "vnvlbv", 0);
343 if ((object = vp->v_object) != NULL) {
344 while (object->paging_in_progress)
345 vm_object_pip_sleep(object, "vnvlbx");
347 } while (vp->v_track_write.bk_active > 0);
352 * Destroy the copy in the VM cache, too.
354 if ((object = vp->v_object) != NULL) {
355 vm_object_page_remove(object, 0, 0,
356 (flags & V_SAVE) ? TRUE : FALSE);
359 if (!RB_EMPTY(&vp->v_rbdirty_tree) || !RB_EMPTY(&vp->v_rbclean_tree))
360 panic("vinvalbuf: flush failed");
361 if (!RB_EMPTY(&vp->v_rbhash_tree))
362 panic("vinvalbuf: flush failed, buffers still present");
367 vinvalbuf_bp(struct buf *bp, void *data)
369 struct vinvalbuf_bp_info *info = data;
372 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
373 error = BUF_TIMELOCK(bp, info->lkflags,
374 "vinvalbuf", info->slptimeo);
384 KKASSERT(bp->b_vp == info->vp);
387 * XXX Since there are no node locks for NFS, I
388 * believe there is a slight chance that a delayed
389 * write will occur while sleeping just above, so
390 * check for it. Note that vfs_bio_awrite expects
391 * buffers to reside on a queue, while bwrite() and
394 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
395 (info->flags & V_SAVE)) {
396 if (bp->b_vp == info->vp) {
397 if (bp->b_flags & B_CLUSTEROK) {
401 bp->b_flags |= B_ASYNC;
408 } else if (info->flags & V_SAVE) {
410 * Cannot set B_NOCACHE on a clean buffer as this will
411 * destroy the VM backing store which might actually
412 * be dirty (and unsynchronized).
415 bp->b_flags |= (B_INVAL | B_RELBUF);
416 bp->b_flags &= ~B_ASYNC;
420 bp->b_flags |= (B_INVAL | B_NOCACHE | B_RELBUF);
421 bp->b_flags &= ~B_ASYNC;
428 * Truncate a file's buffer and pages to a specified length. This
429 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
432 * The vnode must be locked.
434 static int vtruncbuf_bp_trunc_cmp(struct buf *bp, void *data);
435 static int vtruncbuf_bp_trunc(struct buf *bp, void *data);
436 static int vtruncbuf_bp_metasync_cmp(struct buf *bp, void *data);
437 static int vtruncbuf_bp_metasync(struct buf *bp, void *data);
440 vtruncbuf(struct vnode *vp, off_t length, int blksize)
444 const char *filename;
447 * Round up to the *next* block, then destroy the buffers in question.
448 * Since we are only removing some of the buffers we must rely on the
449 * scan count to determine whether a loop is necessary.
451 if ((count = (int)(length % blksize)) != 0)
452 truncloffset = length + (blksize - count);
454 truncloffset = length;
458 count = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree,
459 vtruncbuf_bp_trunc_cmp,
460 vtruncbuf_bp_trunc, &truncloffset);
461 count += RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
462 vtruncbuf_bp_trunc_cmp,
463 vtruncbuf_bp_trunc, &truncloffset);
467 * For safety, fsync any remaining metadata if the file is not being
468 * truncated to 0. Since the metadata does not represent the entire
469 * dirty list we have to rely on the hit count to ensure that we get
474 count = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
475 vtruncbuf_bp_metasync_cmp,
476 vtruncbuf_bp_metasync, vp);
481 * Clean out any left over VM backing store.
485 vnode_pager_setsize(vp, length);
490 * It is possible to have in-progress I/O from buffers that were
491 * not part of the truncation. This should not happen if we
492 * are truncating to 0-length.
494 filename = TAILQ_FIRST(&vp->v_namecache) ?
495 TAILQ_FIRST(&vp->v_namecache)->nc_name : "?";
497 while ((count = vp->v_track_write.bk_active) > 0) {
498 vp->v_track_write.bk_waitflag = 1;
499 tsleep(&vp->v_track_write, 0, "vbtrunc", 0);
501 printf("Warning: vtruncbuf(): Had to wait for "
502 "%d buffer I/Os to finish in %s\n",
508 * Make sure no buffers were instantiated while we were trying
509 * to clean out the remaining VM pages. This could occur due
510 * to busy dirty VM pages being flushed out to disk.
513 count = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree,
514 vtruncbuf_bp_trunc_cmp,
515 vtruncbuf_bp_trunc, &truncloffset);
516 count += RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
517 vtruncbuf_bp_trunc_cmp,
518 vtruncbuf_bp_trunc, &truncloffset);
520 printf("Warning: vtruncbuf(): Had to re-clean %d "
521 "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)
648 crit_enter_id("vfsync");
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);
713 crit_exit_id("vfsync");
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.
799 crit_exit_id("vfsync");
801 crit_enter_id("vfsync");
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;
814 crit_exit_id("vfsync");
816 crit_enter_id("vfsync");
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_p, &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);
1085 * If purging an active vnode (typically during a forced unmount
1086 * or reboot), it must be closed and deactivated before being
1087 * reclaimed. This isn't really all that safe, but what can
1090 * Note that neither of these routines unlocks the vnode.
1092 if (active && (flags & DOCLOSE)) {
1093 while ((n = vp->v_opencount) != 0) {
1094 if (vp->v_writecount)
1095 VOP_CLOSE(vp, FWRITE|FNONBLOCK);
1097 VOP_CLOSE(vp, FNONBLOCK);
1098 if (vp->v_opencount == n) {
1099 printf("Warning: unable to force-close"
1107 * If the vnode has not be deactivated, deactivated it. Deactivation
1108 * can create new buffers and VM pages so we have to call vinvalbuf()
1109 * again to make sure they all get flushed.
1111 * This can occur if a file with a link count of 0 needs to be
1114 if ((vp->v_flag & VINACTIVE) == 0) {
1115 vp->v_flag |= VINACTIVE;
1117 vinvalbuf(vp, V_SAVE, 0, 0);
1121 * If the vnode has an object, destroy it.
1123 if ((object = vp->v_object) != NULL) {
1124 if (object->ref_count == 0) {
1125 if ((object->flags & OBJ_DEAD) == 0)
1126 vm_object_terminate(object);
1128 vm_pager_deallocate(object);
1130 vp->v_flag &= ~VOBJBUF;
1132 KKASSERT((vp->v_flag & VOBJBUF) == 0);
1136 * Reclaim the vnode.
1138 if (VOP_RECLAIM(vp))
1139 panic("vclean: cannot reclaim");
1142 * Done with purge, notify sleepers of the grim news.
1144 vp->v_ops = &dead_vnode_vops_p;
1150 * Eliminate all activity associated with the requested vnode
1151 * and with all vnodes aliased to the requested vnode.
1153 * The vnode must be referenced and vx_lock()'d
1155 * revoke { struct vnode *a_vp, int a_flags }
1158 vop_stdrevoke(struct vop_revoke_args *ap)
1160 struct vnode *vp, *vq;
1164 KASSERT((ap->a_flags & REVOKEALL) != 0, ("vop_revoke"));
1169 * If the vnode is already dead don't try to revoke it
1171 if (vp->v_flag & VRECLAIMED)
1175 * If the vnode has a device association, scrap all vnodes associated
1176 * with the device. Don't let the device disappear on us while we
1177 * are scrapping the vnodes.
1179 * The passed vp will probably show up in the list, do not VX lock
1182 if (vp->v_type != VCHR && vp->v_type != VBLK)
1184 if ((dev = vp->v_rdev) == NULL) {
1185 if ((dev = udev2dev(vp->v_udev, vp->v_type == VBLK)) == NODEV)
1189 lwkt_gettoken(&ilock, &spechash_token);
1190 while ((vq = SLIST_FIRST(&dev->si_hlist)) != NULL) {
1191 if (vp == vq || vx_get(vq) == 0) {
1192 if (vq == SLIST_FIRST(&dev->si_hlist))
1198 lwkt_reltoken(&ilock);
1204 * Recycle an unused vnode to the front of the free list.
1206 * Returns 1 if we were successfully able to recycle the vnode,
1210 vrecycle(struct vnode *vp)
1212 if (vp->v_usecount == 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).
1237 vgone(struct vnode *vp)
1240 * assert that the VX lock is held. This is an absolute requirement
1241 * now for vgone() to be called.
1243 KKASSERT(vp->v_lock.lk_exclusivecount == 1);
1246 * Clean out the filesystem specific data and set the VRECLAIMED
1247 * bit. Also deactivate the vnode if necessary.
1249 vclean(vp, DOCLOSE);
1252 * Delete from old mount point vnode list, if on one.
1254 if (vp->v_mount != NULL)
1255 insmntque(vp, NULL);
1258 * If special device, remove it from special device alias list
1259 * if it is on one. This should normally only occur if a vnode is
1260 * being revoked as the device should otherwise have been released
1263 if ((vp->v_type == VBLK || vp->v_type == VCHR) && vp->v_rdev != NULL) {
1274 * Lookup a vnode by device number.
1277 vfinddev(dev_t dev, enum vtype type, struct vnode **vpp)
1282 lwkt_gettoken(&ilock, &spechash_token);
1283 SLIST_FOREACH(vp, &dev->si_hlist, v_specnext) {
1284 if (type == vp->v_type) {
1286 lwkt_reltoken(&ilock);
1290 lwkt_reltoken(&ilock);
1295 * Calculate the total number of references to a special device. This
1296 * routine may only be called for VBLK and VCHR vnodes since v_rdev is
1297 * an overloaded field. Since udev2dev can now return NODEV, we have
1298 * to check for a NULL v_rdev.
1301 count_dev(dev_t dev)
1307 if (SLIST_FIRST(&dev->si_hlist)) {
1308 lwkt_gettoken(&ilock, &spechash_token);
1309 SLIST_FOREACH(vp, &dev->si_hlist, v_specnext) {
1310 count += vp->v_usecount;
1312 lwkt_reltoken(&ilock);
1318 count_udev(udev_t udev)
1322 if ((dev = udev2dev(udev, 0)) == NODEV)
1324 return(count_dev(dev));
1328 vcount(struct vnode *vp)
1330 if (vp->v_rdev == NULL)
1332 return(count_dev(vp->v_rdev));
1336 * Initialize VMIO for a vnode. This routine MUST be called before a
1337 * VFS can issue buffer cache ops on a vnode. It is typically called
1338 * when a vnode is initialized from its inode.
1341 vinitvmio(struct vnode *vp, off_t filesize)
1347 if ((object = vp->v_object) == NULL) {
1348 object = vnode_pager_alloc(vp, filesize, 0, 0);
1350 * Dereference the reference we just created. This assumes
1351 * that the object is associated with the vp.
1353 object->ref_count--;
1356 if (object->flags & OBJ_DEAD) {
1358 tsleep(object, 0, "vodead", 0);
1359 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1363 KASSERT(vp->v_object != NULL, ("vinitvmio: NULL object"));
1364 vp->v_flag |= VOBJBUF;
1370 * Print out a description of a vnode.
1372 static char *typename[] =
1373 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD"};
1376 vprint(char *label, struct vnode *vp)
1381 printf("%s: %p: ", label, (void *)vp);
1383 printf("%p: ", (void *)vp);
1384 printf("type %s, usecount %d, writecount %d, refcount %d,",
1385 typename[vp->v_type], vp->v_usecount, vp->v_writecount,
1388 if (vp->v_flag & VROOT)
1389 strcat(buf, "|VROOT");
1390 if (vp->v_flag & VTEXT)
1391 strcat(buf, "|VTEXT");
1392 if (vp->v_flag & VSYSTEM)
1393 strcat(buf, "|VSYSTEM");
1394 if (vp->v_flag & VFREE)
1395 strcat(buf, "|VFREE");
1396 if (vp->v_flag & VOBJBUF)
1397 strcat(buf, "|VOBJBUF");
1399 printf(" flags (%s)", &buf[1]);
1400 if (vp->v_data == NULL) {
1409 #include <ddb/ddb.h>
1411 static int db_show_locked_vnodes(struct mount *mp, void *data);
1414 * List all of the locked vnodes in the system.
1415 * Called when debugging the kernel.
1417 DB_SHOW_COMMAND(lockedvnodes, lockedvnodes)
1419 printf("Locked vnodes\n");
1420 mountlist_scan(db_show_locked_vnodes, NULL,
1421 MNTSCAN_FORWARD|MNTSCAN_NOBUSY);
1425 db_show_locked_vnodes(struct mount *mp, void *data __unused)
1429 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
1430 if (VOP_ISLOCKED(vp, NULL))
1431 vprint((char *)0, vp);
1438 * Top level filesystem related information gathering.
1440 static int sysctl_ovfs_conf (SYSCTL_HANDLER_ARGS);
1443 vfs_sysctl(SYSCTL_HANDLER_ARGS)
1445 int *name = (int *)arg1 - 1; /* XXX */
1446 u_int namelen = arg2 + 1; /* XXX */
1447 struct vfsconf *vfsp;
1449 #if 1 || defined(COMPAT_PRELITE2)
1450 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
1452 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
1456 /* all sysctl names at this level are at least name and field */
1458 return (ENOTDIR); /* overloaded */
1459 if (name[0] != VFS_GENERIC) {
1460 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
1461 if (vfsp->vfc_typenum == name[0])
1464 return (EOPNOTSUPP);
1465 return ((*vfsp->vfc_vfsops->vfs_sysctl)(&name[1], namelen - 1,
1466 oldp, oldlenp, newp, newlen, p));
1470 case VFS_MAXTYPENUM:
1473 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int)));
1476 return (ENOTDIR); /* overloaded */
1477 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
1478 if (vfsp->vfc_typenum == name[2])
1481 return (EOPNOTSUPP);
1482 return (SYSCTL_OUT(req, vfsp, sizeof *vfsp));
1484 return (EOPNOTSUPP);
1487 SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD, vfs_sysctl,
1488 "Generic filesystem");
1490 #if 1 || defined(COMPAT_PRELITE2)
1493 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
1496 struct vfsconf *vfsp;
1497 struct ovfsconf ovfs;
1499 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
1500 bzero(&ovfs, sizeof(ovfs));
1501 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
1502 strcpy(ovfs.vfc_name, vfsp->vfc_name);
1503 ovfs.vfc_index = vfsp->vfc_typenum;
1504 ovfs.vfc_refcount = vfsp->vfc_refcount;
1505 ovfs.vfc_flags = vfsp->vfc_flags;
1506 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
1513 #endif /* 1 || COMPAT_PRELITE2 */
1516 * Check to see if a filesystem is mounted on a block device.
1519 vfs_mountedon(struct vnode *vp)
1523 if ((dev = vp->v_rdev) == NULL)
1524 dev = udev2dev(vp->v_udev, (vp->v_type == VBLK));
1525 if (dev != NODEV && dev->si_mountpoint)
1531 * Unmount all filesystems. The list is traversed in reverse order
1532 * of mounting to avoid dependencies.
1535 static int vfs_umountall_callback(struct mount *mp, void *data);
1538 vfs_unmountall(void)
1540 struct thread *td = curthread;
1543 if (td->td_proc == NULL)
1544 td = initproc->p_thread; /* XXX XXX use proc0 instead? */
1547 count = mountlist_scan(vfs_umountall_callback,
1548 NULL, MNTSCAN_REVERSE|MNTSCAN_NOBUSY);
1554 vfs_umountall_callback(struct mount *mp, void *data)
1558 error = dounmount(mp, MNT_FORCE);
1560 mountlist_remove(mp);
1561 printf("unmount of filesystem mounted from %s failed (",
1562 mp->mnt_stat.f_mntfromname);
1566 printf("%d)\n", error);
1572 * Build hash lists of net addresses and hang them off the mount point.
1573 * Called by ufs_mount() to set up the lists of export addresses.
1576 vfs_hang_addrlist(struct mount *mp, struct netexport *nep,
1577 struct export_args *argp)
1580 struct radix_node_head *rnh;
1582 struct radix_node *rn;
1583 struct sockaddr *saddr, *smask = 0;
1587 if (argp->ex_addrlen == 0) {
1588 if (mp->mnt_flag & MNT_DEFEXPORTED)
1590 np = &nep->ne_defexported;
1591 np->netc_exflags = argp->ex_flags;
1592 np->netc_anon = argp->ex_anon;
1593 np->netc_anon.cr_ref = 1;
1594 mp->mnt_flag |= MNT_DEFEXPORTED;
1598 if (argp->ex_addrlen < 0 || argp->ex_addrlen > MLEN)
1600 if (argp->ex_masklen < 0 || argp->ex_masklen > MLEN)
1603 i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
1604 np = (struct netcred *) malloc(i, M_NETADDR, M_WAITOK);
1605 bzero((caddr_t) np, i);
1606 saddr = (struct sockaddr *) (np + 1);
1607 if ((error = copyin(argp->ex_addr, (caddr_t) saddr, argp->ex_addrlen)))
1609 if (saddr->sa_len > argp->ex_addrlen)
1610 saddr->sa_len = argp->ex_addrlen;
1611 if (argp->ex_masklen) {
1612 smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen);
1613 error = copyin(argp->ex_mask, (caddr_t)smask, argp->ex_masklen);
1616 if (smask->sa_len > argp->ex_masklen)
1617 smask->sa_len = argp->ex_masklen;
1619 i = saddr->sa_family;
1620 if ((rnh = nep->ne_rtable[i]) == 0) {
1622 * Seems silly to initialize every AF when most are not used,
1623 * do so on demand here
1625 SLIST_FOREACH(dom, &domains, dom_next)
1626 if (dom->dom_family == i && dom->dom_rtattach) {
1627 dom->dom_rtattach((void **) &nep->ne_rtable[i],
1631 if ((rnh = nep->ne_rtable[i]) == 0) {
1636 rn = (*rnh->rnh_addaddr) ((char *) saddr, (char *) smask, rnh,
1638 if (rn == 0 || np != (struct netcred *) rn) { /* already exists */
1642 np->netc_exflags = argp->ex_flags;
1643 np->netc_anon = argp->ex_anon;
1644 np->netc_anon.cr_ref = 1;
1647 free(np, M_NETADDR);
1653 vfs_free_netcred(struct radix_node *rn, void *w)
1655 struct radix_node_head *rnh = (struct radix_node_head *) w;
1657 (*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, rnh);
1658 free((caddr_t) rn, M_NETADDR);
1663 * Free the net address hash lists that are hanging off the mount points.
1666 vfs_free_addrlist(struct netexport *nep)
1669 struct radix_node_head *rnh;
1671 for (i = 0; i <= AF_MAX; i++)
1672 if ((rnh = nep->ne_rtable[i])) {
1673 (*rnh->rnh_walktree) (rnh, vfs_free_netcred,
1675 free((caddr_t) rnh, M_RTABLE);
1676 nep->ne_rtable[i] = 0;
1681 vfs_export(struct mount *mp, struct netexport *nep, struct export_args *argp)
1685 if (argp->ex_flags & MNT_DELEXPORT) {
1686 if (mp->mnt_flag & MNT_EXPUBLIC) {
1687 vfs_setpublicfs(NULL, NULL, NULL);
1688 mp->mnt_flag &= ~MNT_EXPUBLIC;
1690 vfs_free_addrlist(nep);
1691 mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
1693 if (argp->ex_flags & MNT_EXPORTED) {
1694 if (argp->ex_flags & MNT_EXPUBLIC) {
1695 if ((error = vfs_setpublicfs(mp, nep, argp)) != 0)
1697 mp->mnt_flag |= MNT_EXPUBLIC;
1699 if ((error = vfs_hang_addrlist(mp, nep, argp)))
1701 mp->mnt_flag |= MNT_EXPORTED;
1708 * Set the publicly exported filesystem (WebNFS). Currently, only
1709 * one public filesystem is possible in the spec (RFC 2054 and 2055)
1712 vfs_setpublicfs(struct mount *mp, struct netexport *nep,
1713 struct export_args *argp)
1720 * mp == NULL -> invalidate the current info, the FS is
1721 * no longer exported. May be called from either vfs_export
1722 * or unmount, so check if it hasn't already been done.
1725 if (nfs_pub.np_valid) {
1726 nfs_pub.np_valid = 0;
1727 if (nfs_pub.np_index != NULL) {
1728 FREE(nfs_pub.np_index, M_TEMP);
1729 nfs_pub.np_index = NULL;
1736 * Only one allowed at a time.
1738 if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
1742 * Get real filehandle for root of exported FS.
1744 bzero((caddr_t)&nfs_pub.np_handle, sizeof(nfs_pub.np_handle));
1745 nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid;
1747 if ((error = VFS_ROOT(mp, &rvp)))
1750 if ((error = VFS_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid)))
1756 * If an indexfile was specified, pull it in.
1758 if (argp->ex_indexfile != NULL) {
1761 error = vn_get_namelen(rvp, &namelen);
1764 MALLOC(nfs_pub.np_index, char *, namelen, M_TEMP,
1766 error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
1767 namelen, (size_t *)0);
1770 * Check for illegal filenames.
1772 for (cp = nfs_pub.np_index; *cp; cp++) {
1780 FREE(nfs_pub.np_index, M_TEMP);
1785 nfs_pub.np_mount = mp;
1786 nfs_pub.np_valid = 1;
1791 vfs_export_lookup(struct mount *mp, struct netexport *nep,
1792 struct sockaddr *nam)
1795 struct radix_node_head *rnh;
1796 struct sockaddr *saddr;
1799 if (mp->mnt_flag & MNT_EXPORTED) {
1801 * Lookup in the export list first.
1805 rnh = nep->ne_rtable[saddr->sa_family];
1807 np = (struct netcred *)
1808 (*rnh->rnh_matchaddr)((char *)saddr,
1810 if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
1815 * If no address match, use the default if it exists.
1817 if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
1818 np = &nep->ne_defexported;
1824 * perform msync on all vnodes under a mount point. The mount point must
1825 * be locked. This code is also responsible for lazy-freeing unreferenced
1826 * vnodes whos VM objects no longer contain pages.
1828 * NOTE: MNT_WAIT still skips vnodes in the VXLOCK state.
1830 * NOTE: XXX VOP_PUTPAGES and friends requires that the vnode be locked,
1831 * but vnode_pager_putpages() doesn't lock the vnode. We have to do it
1832 * way up in this high level function.
1834 static int vfs_msync_scan1(struct mount *mp, struct vnode *vp, void *data);
1835 static int vfs_msync_scan2(struct mount *mp, struct vnode *vp, void *data);
1838 vfs_msync(struct mount *mp, int flags)
1842 vmsc_flags = VMSC_GETVP;
1843 if (flags != MNT_WAIT)
1844 vmsc_flags |= VMSC_NOWAIT;
1845 vmntvnodescan(mp, vmsc_flags, vfs_msync_scan1, vfs_msync_scan2,
1850 * scan1 is a fast pre-check. There could be hundreds of thousands of
1851 * vnodes, we cannot afford to do anything heavy weight until we have a
1852 * fairly good indication that there is work to do.
1856 vfs_msync_scan1(struct mount *mp, struct vnode *vp, void *data)
1858 int flags = (int)data;
1860 if ((vp->v_flag & VRECLAIMED) == 0) {
1861 if (vshouldmsync(vp, 0))
1862 return(0); /* call scan2 */
1863 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
1864 (vp->v_flag & VOBJDIRTY) &&
1865 (flags == MNT_WAIT || VOP_ISLOCKED(vp, NULL) == 0)) {
1866 return(0); /* call scan2 */
1871 * do not call scan2, continue the loop
1877 * This callback is handed a locked vnode.
1881 vfs_msync_scan2(struct mount *mp, struct vnode *vp, void *data)
1884 int flags = (int)data;
1886 if (vp->v_flag & VRECLAIMED)
1889 if ((mp->mnt_flag & MNT_RDONLY) == 0 && (vp->v_flag & VOBJDIRTY)) {
1890 if ((obj = vp->v_object) != NULL) {
1891 vm_object_page_clean(obj, 0, 0,
1892 flags == MNT_WAIT ? OBJPC_SYNC : OBJPC_NOSYNC);
1899 * Record a process's interest in events which might happen to
1900 * a vnode. Because poll uses the historic select-style interface
1901 * internally, this routine serves as both the ``check for any
1902 * pending events'' and the ``record my interest in future events''
1903 * functions. (These are done together, while the lock is held,
1904 * to avoid race conditions.)
1907 vn_pollrecord(struct vnode *vp, int events)
1911 KKASSERT(curthread->td_proc != NULL);
1913 lwkt_gettoken(&ilock, &vp->v_pollinfo.vpi_token);
1914 if (vp->v_pollinfo.vpi_revents & events) {
1916 * This leaves events we are not interested
1917 * in available for the other process which
1918 * which presumably had requested them
1919 * (otherwise they would never have been
1922 events &= vp->v_pollinfo.vpi_revents;
1923 vp->v_pollinfo.vpi_revents &= ~events;
1925 lwkt_reltoken(&ilock);
1928 vp->v_pollinfo.vpi_events |= events;
1929 selrecord(curthread, &vp->v_pollinfo.vpi_selinfo);
1930 lwkt_reltoken(&ilock);
1935 * Note the occurrence of an event. If the VN_POLLEVENT macro is used,
1936 * it is possible for us to miss an event due to race conditions, but
1937 * that condition is expected to be rare, so for the moment it is the
1938 * preferred interface.
1941 vn_pollevent(struct vnode *vp, int events)
1945 lwkt_gettoken(&ilock, &vp->v_pollinfo.vpi_token);
1946 if (vp->v_pollinfo.vpi_events & events) {
1948 * We clear vpi_events so that we don't
1949 * call selwakeup() twice if two events are
1950 * posted before the polling process(es) is
1951 * awakened. This also ensures that we take at
1952 * most one selwakeup() if the polling process
1953 * is no longer interested. However, it does
1954 * mean that only one event can be noticed at
1955 * a time. (Perhaps we should only clear those
1956 * event bits which we note?) XXX
1958 vp->v_pollinfo.vpi_events = 0; /* &= ~events ??? */
1959 vp->v_pollinfo.vpi_revents |= events;
1960 selwakeup(&vp->v_pollinfo.vpi_selinfo);
1962 lwkt_reltoken(&ilock);
1966 * Wake up anyone polling on vp because it is being revoked.
1967 * This depends on dead_poll() returning POLLHUP for correct
1971 vn_pollgone(struct vnode *vp)
1975 lwkt_gettoken(&ilock, &vp->v_pollinfo.vpi_token);
1976 if (vp->v_pollinfo.vpi_events) {
1977 vp->v_pollinfo.vpi_events = 0;
1978 selwakeup(&vp->v_pollinfo.vpi_selinfo);
1980 lwkt_reltoken(&ilock);
1984 * extract the dev_t from a VBLK or VCHR. The vnode must have been opened
1985 * (or v_rdev might be NULL).
1988 vn_todev(struct vnode *vp)
1990 if (vp->v_type != VBLK && vp->v_type != VCHR)
1992 KKASSERT(vp->v_rdev != NULL);
1993 return (vp->v_rdev);
1997 * Check if vnode represents a disk device. The vnode does not need to be
2001 vn_isdisk(struct vnode *vp, int *errp)
2005 if (vp->v_type != VBLK && vp->v_type != VCHR) {
2011 if ((dev = vp->v_rdev) == NULL)
2012 dev = udev2dev(vp->v_udev, (vp->v_type == VBLK));
2013 if (dev == NULL || dev == NODEV) {
2018 if (dev_is_good(dev) == 0) {
2023 if ((dev_dflags(dev) & D_DISK) == 0) {
2033 #ifdef DEBUG_VFS_LOCKS
2036 assert_vop_locked(struct vnode *vp, const char *str)
2038 if (vp && !VOP_ISLOCKED(vp, NULL)) {
2039 panic("%s: %p is not locked shared but should be", str, vp);
2044 assert_vop_unlocked(struct vnode *vp, const char *str)
2047 if (VOP_ISLOCKED(vp, curthread) == LK_EXCLUSIVE) {
2048 panic("%s: %p is locked but should not be", str, vp);
2056 vn_get_namelen(struct vnode *vp, int *namelen)
2058 int error, retval[2];
2060 error = VOP_PATHCONF(vp, _PC_NAME_MAX, retval);
2068 vop_write_dirent(int *error, struct uio *uio, ino_t d_ino, uint8_t d_type,
2069 uint16_t d_namlen, const char *d_name)
2074 len = _DIRENT_RECLEN(d_namlen);
2075 if (len > uio->uio_resid)
2078 dp = malloc(len, M_TEMP, M_WAITOK | M_ZERO);
2081 dp->d_namlen = d_namlen;
2082 dp->d_type = d_type;
2083 bcopy(d_name, dp->d_name, d_namlen);
2085 *error = uiomove((caddr_t)dp, len, uio);