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.68 2006/03/02 19:26:14 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_GENERATE(buf_rb_tree, buf, b_rbnode, rb_buf_compare);
142 rb_buf_compare(struct buf *b1, struct buf *b2)
144 if (b1->b_lblkno < b2->b_lblkno)
146 if (b1->b_lblkno > b2->b_lblkno)
152 * Return 0 if the vnode is already on the free list or cannot be placed
153 * on the free list. Return 1 if the vnode can be placed on the free list.
156 vshouldfree(struct vnode *vp, int usecount)
158 if (vp->v_flag & VFREE)
159 return (0); /* already free */
160 if (vp->v_holdcnt != 0 || vp->v_usecount != usecount)
161 return (0); /* other holderse */
163 (vp->v_object->ref_count || vp->v_object->resident_page_count)) {
170 * Initialize the vnode management data structures.
172 * Called from vfsinit()
178 * Desired vnodes is a result of the physical page count
179 * and the size of kernel's heap. It scales in proportion
180 * to the amount of available physical memory. This can
181 * cause trouble on 64-bit and large memory platforms.
183 /* desiredvnodes = maxproc + vmstats.v_page_count / 4; */
185 min(maxproc + vmstats.v_page_count /4,
186 2 * (VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS) /
187 (5 * (sizeof(struct vm_object) + sizeof(struct vnode))));
189 lwkt_token_init(&spechash_token);
193 * Knob to control the precision of file timestamps:
195 * 0 = seconds only; nanoseconds zeroed.
196 * 1 = seconds and nanoseconds, accurate within 1/HZ.
197 * 2 = seconds and nanoseconds, truncated to microseconds.
198 * >=3 = seconds and nanoseconds, maximum precision.
200 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC };
202 static int timestamp_precision = TSP_SEC;
203 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW,
204 ×tamp_precision, 0, "");
207 * Get a current timestamp.
210 vfs_timestamp(struct timespec *tsp)
214 switch (timestamp_precision) {
216 tsp->tv_sec = time_second;
224 TIMEVAL_TO_TIMESPEC(&tv, tsp);
234 * Set vnode attributes to VNOVAL
237 vattr_null(struct vattr *vap)
240 vap->va_size = VNOVAL;
241 vap->va_bytes = VNOVAL;
242 vap->va_mode = VNOVAL;
243 vap->va_nlink = VNOVAL;
244 vap->va_uid = VNOVAL;
245 vap->va_gid = VNOVAL;
246 vap->va_fsid = VNOVAL;
247 vap->va_fileid = VNOVAL;
248 vap->va_blocksize = VNOVAL;
249 vap->va_rdev = VNOVAL;
250 vap->va_atime.tv_sec = VNOVAL;
251 vap->va_atime.tv_nsec = VNOVAL;
252 vap->va_mtime.tv_sec = VNOVAL;
253 vap->va_mtime.tv_nsec = VNOVAL;
254 vap->va_ctime.tv_sec = VNOVAL;
255 vap->va_ctime.tv_nsec = VNOVAL;
256 vap->va_flags = VNOVAL;
257 vap->va_gen = VNOVAL;
259 vap->va_fsmid = VNOVAL;
263 * Flush out and invalidate all buffers associated with a vnode.
267 static int vinvalbuf_bp(struct buf *bp, void *data);
269 struct vinvalbuf_bp_info {
277 vinvalbuf(struct vnode *vp, int flags, struct thread *td,
278 int slpflag, int slptimeo)
280 struct vinvalbuf_bp_info info;
285 * If we are being asked to save, call fsync to ensure that the inode
288 if (flags & V_SAVE) {
290 while (vp->v_track_write.bk_active) {
291 vp->v_track_write.bk_waitflag = 1;
292 error = tsleep(&vp->v_track_write, slpflag,
293 "vinvlbuf", slptimeo);
299 if (!RB_EMPTY(&vp->v_rbdirty_tree)) {
301 if ((error = VOP_FSYNC(vp, MNT_WAIT, td)) != 0)
304 if (vp->v_track_write.bk_active > 0 ||
305 !RB_EMPTY(&vp->v_rbdirty_tree))
306 panic("vinvalbuf: dirty bufs");
311 info.slptimeo = slptimeo;
312 info.lkflags = LK_EXCLUSIVE | LK_SLEEPFAIL;
313 if (slpflag & PCATCH)
314 info.lkflags |= LK_PCATCH;
319 * Flush the buffer cache until nothing is left.
321 while (!RB_EMPTY(&vp->v_rbclean_tree) ||
322 !RB_EMPTY(&vp->v_rbdirty_tree)) {
323 error = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree, NULL,
324 vinvalbuf_bp, &info);
326 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
327 vinvalbuf_bp, &info);
332 * Wait for I/O to complete. XXX needs cleaning up. The vnode can
333 * have write I/O in-progress but if there is a VM object then the
334 * VM object can also have read-I/O in-progress.
337 while (vp->v_track_write.bk_active > 0) {
338 vp->v_track_write.bk_waitflag = 1;
339 tsleep(&vp->v_track_write, 0, "vnvlbv", 0);
341 if (VOP_GETVOBJECT(vp, &object) == 0) {
342 while (object->paging_in_progress)
343 vm_object_pip_sleep(object, "vnvlbx");
345 } while (vp->v_track_write.bk_active > 0);
350 * Destroy the copy in the VM cache, too.
352 if (VOP_GETVOBJECT(vp, &object) == 0) {
353 vm_object_page_remove(object, 0, 0,
354 (flags & V_SAVE) ? TRUE : FALSE);
357 if (!RB_EMPTY(&vp->v_rbdirty_tree) || !RB_EMPTY(&vp->v_rbclean_tree))
358 panic("vinvalbuf: flush failed");
363 vinvalbuf_bp(struct buf *bp, void *data)
365 struct vinvalbuf_bp_info *info = data;
368 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
369 error = BUF_TIMELOCK(bp, info->lkflags,
370 "vinvalbuf", info->slptimeo);
380 KKASSERT(bp->b_vp == info->vp);
383 * XXX Since there are no node locks for NFS, I
384 * believe there is a slight chance that a delayed
385 * write will occur while sleeping just above, so
386 * check for it. Note that vfs_bio_awrite expects
387 * buffers to reside on a queue, while VOP_BWRITE and
390 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
391 (info->flags & V_SAVE)) {
392 if (bp->b_vp == info->vp) {
393 if (bp->b_flags & B_CLUSTEROK) {
398 bp->b_flags |= B_ASYNC;
399 VOP_BWRITE(bp->b_vp, bp);
403 VOP_BWRITE(bp->b_vp, bp);
407 bp->b_flags |= (B_INVAL | B_NOCACHE | B_RELBUF);
408 bp->b_flags &= ~B_ASYNC;
415 * Truncate a file's buffer and pages to a specified length. This
416 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
419 * The vnode must be locked.
421 static int vtruncbuf_bp_trunc_cmp(struct buf *bp, void *data);
422 static int vtruncbuf_bp_trunc(struct buf *bp, void *data);
423 static int vtruncbuf_bp_metasync_cmp(struct buf *bp, void *data);
424 static int vtruncbuf_bp_metasync(struct buf *bp, void *data);
427 vtruncbuf(struct vnode *vp, struct thread *td, off_t length, int blksize)
433 * Round up to the *next* lbn, then destroy the buffers in question.
434 * Since we are only removing some of the buffers we must rely on the
435 * scan count to determine whether a loop is necessary.
437 trunclbn = (length + blksize - 1) / blksize;
441 count = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree,
442 vtruncbuf_bp_trunc_cmp,
443 vtruncbuf_bp_trunc, &trunclbn);
444 count += RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
445 vtruncbuf_bp_trunc_cmp,
446 vtruncbuf_bp_trunc, &trunclbn);
450 * For safety, fsync any remaining metadata if the file is not being
451 * truncated to 0. Since the metadata does not represent the entire
452 * dirty list we have to rely on the hit count to ensure that we get
457 count = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
458 vtruncbuf_bp_metasync_cmp,
459 vtruncbuf_bp_metasync, vp);
464 * Wait for any in-progress I/O to complete before returning (why?)
466 while (vp->v_track_write.bk_active > 0) {
467 vp->v_track_write.bk_waitflag = 1;
468 tsleep(&vp->v_track_write, 0, "vbtrunc", 0);
473 vnode_pager_setsize(vp, length);
479 * The callback buffer is beyond the new file EOF and must be destroyed.
480 * Note that the compare function must conform to the RB_SCAN's requirements.
484 vtruncbuf_bp_trunc_cmp(struct buf *bp, void *data)
486 if (bp->b_lblkno >= *(daddr_t *)data)
493 vtruncbuf_bp_trunc(struct buf *bp, void *data)
496 * Do not try to use a buffer we cannot immediately lock, but sleep
497 * anyway to prevent a livelock. The code will loop until all buffers
500 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
501 if (BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL) == 0)
505 bp->b_flags |= (B_INVAL | B_RELBUF);
506 bp->b_flags &= ~B_ASYNC;
513 * Fsync all meta-data after truncating a file to be non-zero. Only metadata
514 * blocks (with a negative lblkno) are scanned.
515 * Note that the compare function must conform to the RB_SCAN's requirements.
518 vtruncbuf_bp_metasync_cmp(struct buf *bp, void *data)
520 if (bp->b_lblkno < 0)
526 vtruncbuf_bp_metasync(struct buf *bp, void *data)
528 struct vnode *vp = data;
530 if (bp->b_flags & B_DELWRI) {
532 * Do not try to use a buffer we cannot immediately lock,
533 * but sleep anyway to prevent a livelock. The code will
534 * loop until all buffers can be acted upon.
536 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
537 if (BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL) == 0)
541 if (bp->b_vp == vp) {
542 bp->b_flags |= B_ASYNC;
544 bp->b_flags &= ~B_ASYNC;
546 VOP_BWRITE(bp->b_vp, bp);
555 * vfsync - implements a multipass fsync on a file which understands
556 * dependancies and meta-data. The passed vnode must be locked. The
557 * waitfor argument may be MNT_WAIT or MNT_NOWAIT, or MNT_LAZY.
559 * When fsyncing data asynchronously just do one consolidated pass starting
560 * with the most negative block number. This may not get all the data due
563 * When fsyncing data synchronously do a data pass, then a metadata pass,
564 * then do additional data+metadata passes to try to get all the data out.
566 static int vfsync_wait_output(struct vnode *vp,
567 int (*waitoutput)(struct vnode *, struct thread *));
568 static int vfsync_data_only_cmp(struct buf *bp, void *data);
569 static int vfsync_meta_only_cmp(struct buf *bp, void *data);
570 static int vfsync_lazy_range_cmp(struct buf *bp, void *data);
571 static int vfsync_bp(struct buf *bp, void *data);
580 int (*checkdef)(struct buf *);
584 vfsync(struct vnode *vp, int waitfor, int passes, daddr_t lbn,
585 int (*checkdef)(struct buf *),
586 int (*waitoutput)(struct vnode *, struct thread *))
588 struct vfsync_info info;
591 bzero(&info, sizeof(info));
594 if ((info.checkdef = checkdef) == NULL)
602 * Lazy (filesystem syncer typ) Asynchronous plus limit the
603 * number of data (not meta) pages we try to flush to 1MB.
604 * A non-zero return means that lazy limit was reached.
606 info.lazylimit = 1024 * 1024;
608 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
609 vfsync_lazy_range_cmp, vfsync_bp, &info);
610 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
611 vfsync_meta_only_cmp, vfsync_bp, &info);
614 else if (!RB_EMPTY(&vp->v_rbdirty_tree))
615 vn_syncer_add_to_worklist(vp, 1);
620 * Asynchronous. Do a data-only pass and a meta-only pass.
623 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_data_only_cmp,
625 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_meta_only_cmp,
631 * Synchronous. Do a data-only pass, then a meta-data+data
632 * pass, then additional integrated passes to try to get
633 * all the dependancies flushed.
635 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_data_only_cmp,
637 error = vfsync_wait_output(vp, waitoutput);
639 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
641 error = vfsync_wait_output(vp, waitoutput);
643 while (error == 0 && passes > 0 &&
644 !RB_EMPTY(&vp->v_rbdirty_tree)) {
646 info.synchronous = 1;
649 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
655 error = vfsync_wait_output(vp, waitoutput);
664 vfsync_wait_output(struct vnode *vp, int (*waitoutput)(struct vnode *, struct thread *))
668 while (vp->v_track_write.bk_active) {
669 vp->v_track_write.bk_waitflag = 1;
670 tsleep(&vp->v_track_write, 0, "fsfsn", 0);
673 error = waitoutput(vp, curthread);
678 vfsync_data_only_cmp(struct buf *bp, void *data)
680 if (bp->b_lblkno < 0)
686 vfsync_meta_only_cmp(struct buf *bp, void *data)
688 if (bp->b_lblkno < 0)
694 vfsync_lazy_range_cmp(struct buf *bp, void *data)
696 struct vfsync_info *info = data;
697 if (bp->b_lblkno < info->vp->v_lazyw)
703 vfsync_bp(struct buf *bp, void *data)
705 struct vfsync_info *info = data;
706 struct vnode *vp = info->vp;
710 * if syncdeps is not set we do not try to write buffers which have
713 if (!info->synchronous && info->syncdeps == 0 && info->checkdef(bp))
717 * Ignore buffers that we cannot immediately lock. XXX
719 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT))
721 if ((bp->b_flags & B_DELWRI) == 0)
722 panic("vfsync_bp: buffer not dirty");
724 panic("vfsync_bp: buffer vp mismatch");
727 * B_NEEDCOMMIT (primarily used by NFS) is a state where the buffer
728 * has been written but an additional handshake with the device
729 * is required before we can dispose of the buffer. We have no idea
730 * how to do this so we have to skip these buffers.
732 if (bp->b_flags & B_NEEDCOMMIT) {
738 * (LEGACY FROM UFS, REMOVE WHEN POSSIBLE) - invalidate any dirty
739 * buffers beyond the file EOF.
741 if (info->lbn != (daddr_t)-1 && vp->v_type == VREG &&
742 bp->b_lblkno >= info->lbn) {
744 bp->b_flags |= B_INVAL | B_NOCACHE;
750 if (info->synchronous) {
752 * Synchronous flushing. An error may be returned.
760 * Asynchronous flushing. A negative return value simply
761 * stops the scan and is not considered an error. We use
762 * this to support limited MNT_LAZY flushes.
764 vp->v_lazyw = bp->b_lblkno;
765 if ((vp->v_flag & VOBJBUF) && (bp->b_flags & B_CLUSTEROK)) {
767 info->lazycount += vfs_bio_awrite(bp);
769 info->lazycount += bp->b_bufsize;
775 if (info->lazylimit && info->lazycount >= info->lazylimit)
784 * Associate a buffer with a vnode.
787 bgetvp(struct vnode *vp, struct buf *bp)
789 KASSERT(bp->b_vp == NULL, ("bgetvp: not free"));
794 * Insert onto list for new vnode.
797 bp->b_xflags |= BX_VNCLEAN;
798 bp->b_xflags &= ~BX_VNDIRTY;
799 if (buf_rb_tree_RB_INSERT(&vp->v_rbclean_tree, bp))
800 panic("reassignbuf: dup lblk vp %p bp %p", vp, bp);
805 * Disassociate a buffer from a vnode.
808 brelvp(struct buf *bp)
812 KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
815 * Delete from old vnode list, if on one.
819 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) {
820 if (bp->b_xflags & BX_VNDIRTY)
821 buf_rb_tree_RB_REMOVE(&vp->v_rbdirty_tree, bp);
823 buf_rb_tree_RB_REMOVE(&vp->v_rbclean_tree, bp);
824 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN);
826 if ((vp->v_flag & VONWORKLST) && RB_EMPTY(&vp->v_rbdirty_tree)) {
827 vp->v_flag &= ~VONWORKLST;
828 LIST_REMOVE(vp, v_synclist);
836 * Associate a p-buffer with a vnode.
838 * Also sets B_PAGING flag to indicate that vnode is not fully associated
839 * with the buffer. i.e. the bp has not been linked into the vnode or
843 pbgetvp(struct vnode *vp, struct buf *bp)
845 KASSERT(bp->b_vp == NULL, ("pbgetvp: not free"));
848 bp->b_flags |= B_PAGING;
852 * Disassociate a p-buffer from a vnode.
855 pbrelvp(struct buf *bp)
857 KASSERT(bp->b_vp != NULL, ("pbrelvp: NULL"));
860 bp->b_flags &= ~B_PAGING;
864 pbreassignbuf(struct buf *bp, struct vnode *newvp)
866 if ((bp->b_flags & B_PAGING) == 0) {
868 "pbreassignbuf() on non phys bp %p",
876 * Reassign a buffer from one vnode to another.
877 * Used to assign file specific control information
878 * (indirect blocks) to the vnode to which they belong.
881 reassignbuf(struct buf *bp, struct vnode *newvp)
886 printf("reassignbuf: NULL");
892 * B_PAGING flagged buffers cannot be reassigned because their vp
893 * is not fully linked in.
895 if (bp->b_flags & B_PAGING)
896 panic("cannot reassign paging buffer");
900 * Delete from old vnode list, if on one.
902 if (bp->b_xflags & (BX_VNDIRTY | BX_VNCLEAN)) {
903 if (bp->b_xflags & BX_VNDIRTY)
904 buf_rb_tree_RB_REMOVE(&bp->b_vp->v_rbdirty_tree, bp);
906 buf_rb_tree_RB_REMOVE(&bp->b_vp->v_rbclean_tree, bp);
907 bp->b_xflags &= ~(BX_VNDIRTY | BX_VNCLEAN);
908 if (bp->b_vp != newvp) {
910 bp->b_vp = NULL; /* for clarification */
914 * If dirty, put on list of dirty buffers; otherwise insert onto list
917 if (bp->b_flags & B_DELWRI) {
918 if ((newvp->v_flag & VONWORKLST) == 0) {
919 switch (newvp->v_type) {
926 newvp->v_rdev->si_mountpoint != NULL) {
934 vn_syncer_add_to_worklist(newvp, delay);
936 bp->b_xflags |= BX_VNDIRTY;
937 if (buf_rb_tree_RB_INSERT(&newvp->v_rbdirty_tree, bp))
938 panic("reassignbuf: dup lblk vp %p bp %p", newvp, bp);
940 bp->b_xflags |= BX_VNCLEAN;
941 if (buf_rb_tree_RB_INSERT(&newvp->v_rbclean_tree, bp))
942 panic("reassignbuf: dup lblk vp %p bp %p", newvp, bp);
943 if ((newvp->v_flag & VONWORKLST) &&
944 RB_EMPTY(&newvp->v_rbdirty_tree)) {
945 newvp->v_flag &= ~VONWORKLST;
946 LIST_REMOVE(newvp, v_synclist);
949 if (bp->b_vp != newvp) {
957 * Create a vnode for a block device.
958 * Used for mounting the root file system.
961 bdevvp(dev_t dev, struct vnode **vpp)
971 error = getspecialvnode(VT_NON, NULL, &spec_vnode_vops, &nvp, 0, 0);
978 vp->v_udev = dev->si_udev;
985 v_associate_rdev(struct vnode *vp, dev_t dev)
989 if (dev == NULL || dev == NODEV)
991 if (dev_is_good(dev) == 0)
993 KKASSERT(vp->v_rdev == NULL);
996 vp->v_rdev = reference_dev(dev);
997 lwkt_gettoken(&ilock, &spechash_token);
998 SLIST_INSERT_HEAD(&dev->si_hlist, vp, v_specnext);
999 lwkt_reltoken(&ilock);
1004 v_release_rdev(struct vnode *vp)
1009 if ((dev = vp->v_rdev) != NULL) {
1010 lwkt_gettoken(&ilock, &spechash_token);
1011 SLIST_REMOVE(&dev->si_hlist, vp, vnode, v_specnext);
1012 if (dev_ref_debug && vp->v_opencount != 0) {
1013 printf("releasing rdev with non-0 "
1014 "v_opencount(%d) (revoked?)\n",
1018 vp->v_opencount = 0;
1020 lwkt_reltoken(&ilock);
1025 * Add a vnode to the alias list hung off the dev_t. We only associate
1026 * the device number with the vnode. The actual device is not associated
1027 * until the vnode is opened (usually in spec_open()), and will be
1028 * disassociated on last close.
1031 addaliasu(struct vnode *nvp, udev_t nvp_udev)
1033 if (nvp->v_type != VBLK && nvp->v_type != VCHR)
1034 panic("addaliasu on non-special vnode");
1035 nvp->v_udev = nvp_udev;
1039 * Disassociate a vnode from its underlying filesystem.
1041 * The vnode must be VX locked and refd
1043 * If there are v_usecount references to the vnode other then ours we have
1044 * to VOP_CLOSE the vnode before we can deactivate and reclaim it.
1047 vclean(struct vnode *vp, int flags, struct thread *td)
1053 * If the vnode has already been reclaimed we have nothing to do.
1055 if (vp->v_flag & VRECLAIMED)
1057 vp->v_flag |= VRECLAIMED;
1060 * Scrap the vfs cache
1062 while (cache_inval_vp(vp, 0, &retflags) != 0) {
1063 printf("Warning: vnode %p clean/cache_resolution race detected\n", vp);
1064 tsleep(vp, 0, "vclninv", 2);
1068 * Check to see if the vnode is in use. If so we have to reference it
1069 * before we clean it out so that its count cannot fall to zero and
1070 * generate a race against ourselves to recycle it.
1072 active = (vp->v_usecount > 1);
1075 * Clean out any buffers associated with the vnode and destroy its
1076 * object, if it has one.
1078 vinvalbuf(vp, V_SAVE, td, 0, 0);
1079 VOP_DESTROYVOBJECT(vp);
1082 * If purging an active vnode, it must be closed and
1083 * deactivated before being reclaimed. XXX
1085 * Note that neither of these routines unlocks the vnode.
1088 if (flags & DOCLOSE)
1089 VOP_CLOSE(vp, FNONBLOCK, td);
1093 * If the vnode has not be deactivated, deactivated it.
1095 if ((vp->v_flag & VINACTIVE) == 0) {
1096 vp->v_flag |= VINACTIVE;
1097 VOP_INACTIVE(vp, td);
1101 * Reclaim the vnode.
1103 if (VOP_RECLAIM(vp, retflags, td))
1104 panic("vclean: cannot reclaim");
1107 * Done with purge, notify sleepers of the grim news.
1109 vp->v_ops = &dead_vnode_vops;
1115 * Eliminate all activity associated with the requested vnode
1116 * and with all vnodes aliased to the requested vnode.
1118 * The vnode must be referenced and vx_lock()'d
1120 * revoke { struct vnode *a_vp, int a_flags }
1123 vop_stdrevoke(struct vop_revoke_args *ap)
1125 struct vnode *vp, *vq;
1129 KASSERT((ap->a_flags & REVOKEALL) != 0, ("vop_revoke"));
1134 * If the vnode is already dead don't try to revoke it
1136 if (vp->v_flag & VRECLAIMED)
1140 * If the vnode has a device association, scrap all vnodes associated
1141 * with the device. Don't let the device disappear on us while we
1142 * are scrapping the vnodes.
1144 * The passed vp will probably show up in the list, do not VX lock
1147 if (vp->v_type != VCHR && vp->v_type != VBLK)
1149 if ((dev = vp->v_rdev) == NULL) {
1150 if ((dev = udev2dev(vp->v_udev, vp->v_type == VBLK)) == NODEV)
1154 lwkt_gettoken(&ilock, &spechash_token);
1155 while ((vq = SLIST_FIRST(&dev->si_hlist)) != NULL) {
1156 if (vp == vq || vx_get(vq) == 0) {
1157 if (vq == SLIST_FIRST(&dev->si_hlist))
1163 lwkt_reltoken(&ilock);
1169 * Recycle an unused vnode to the front of the free list.
1171 * Returns 1 if we were successfully able to recycle the vnode,
1175 vrecycle(struct vnode *vp, struct thread *td)
1177 if (vp->v_usecount == 1) {
1185 * Eliminate all activity associated with a vnode in preparation for reuse.
1187 * The vnode must be VX locked and refd and will remain VX locked and refd
1188 * on return. This routine may be called with the vnode in any state, as
1189 * long as it is VX locked. The vnode will be cleaned out and marked
1190 * VRECLAIMED but will not actually be reused until all existing refs and
1193 * NOTE: This routine may be called on a vnode which has not yet been
1194 * already been deactivated (VOP_INACTIVE), or on a vnode which has
1195 * already been reclaimed.
1197 * This routine is not responsible for placing us back on the freelist.
1198 * Instead, it happens automatically when the caller releases the VX lock
1199 * (assuming there aren't any other references).
1202 vgone(struct vnode *vp)
1205 * assert that the VX lock is held. This is an absolute requirement
1206 * now for vgone() to be called.
1208 KKASSERT(vp->v_lock.lk_exclusivecount == 1);
1211 * Clean out the filesystem specific data and set the VRECLAIMED
1212 * bit. Also deactivate the vnode if necessary.
1214 vclean(vp, DOCLOSE, curthread);
1217 * Delete from old mount point vnode list, if on one.
1219 if (vp->v_mount != NULL)
1220 insmntque(vp, NULL);
1223 * If special device, remove it from special device alias list
1224 * if it is on one. This should normally only occur if a vnode is
1225 * being revoked as the device should otherwise have been released
1228 if ((vp->v_type == VBLK || vp->v_type == VCHR) && vp->v_rdev != NULL) {
1239 * Lookup a vnode by device number.
1242 vfinddev(dev_t dev, enum vtype type, struct vnode **vpp)
1247 lwkt_gettoken(&ilock, &spechash_token);
1248 SLIST_FOREACH(vp, &dev->si_hlist, v_specnext) {
1249 if (type == vp->v_type) {
1251 lwkt_reltoken(&ilock);
1255 lwkt_reltoken(&ilock);
1260 * Calculate the total number of references to a special device. This
1261 * routine may only be called for VBLK and VCHR vnodes since v_rdev is
1262 * an overloaded field. Since udev2dev can now return NODEV, we have
1263 * to check for a NULL v_rdev.
1266 count_dev(dev_t dev)
1272 if (SLIST_FIRST(&dev->si_hlist)) {
1273 lwkt_gettoken(&ilock, &spechash_token);
1274 SLIST_FOREACH(vp, &dev->si_hlist, v_specnext) {
1275 count += vp->v_usecount;
1277 lwkt_reltoken(&ilock);
1283 count_udev(udev_t udev)
1287 if ((dev = udev2dev(udev, 0)) == NODEV)
1289 return(count_dev(dev));
1293 vcount(struct vnode *vp)
1295 if (vp->v_rdev == NULL)
1297 return(count_dev(vp->v_rdev));
1301 * Print out a description of a vnode.
1303 static char *typename[] =
1304 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD"};
1307 vprint(char *label, struct vnode *vp)
1312 printf("%s: %p: ", label, (void *)vp);
1314 printf("%p: ", (void *)vp);
1315 printf("type %s, usecount %d, writecount %d, refcount %d,",
1316 typename[vp->v_type], vp->v_usecount, vp->v_writecount,
1319 if (vp->v_flag & VROOT)
1320 strcat(buf, "|VROOT");
1321 if (vp->v_flag & VTEXT)
1322 strcat(buf, "|VTEXT");
1323 if (vp->v_flag & VSYSTEM)
1324 strcat(buf, "|VSYSTEM");
1325 if (vp->v_flag & VFREE)
1326 strcat(buf, "|VFREE");
1327 if (vp->v_flag & VOBJBUF)
1328 strcat(buf, "|VOBJBUF");
1330 printf(" flags (%s)", &buf[1]);
1331 if (vp->v_data == NULL) {
1340 #include <ddb/ddb.h>
1342 static int db_show_locked_vnodes(struct mount *mp, void *data);
1345 * List all of the locked vnodes in the system.
1346 * Called when debugging the kernel.
1348 DB_SHOW_COMMAND(lockedvnodes, lockedvnodes)
1350 printf("Locked vnodes\n");
1351 mountlist_scan(db_show_locked_vnodes, NULL,
1352 MNTSCAN_FORWARD|MNTSCAN_NOBUSY);
1356 db_show_locked_vnodes(struct mount *mp, void *data __unused)
1360 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
1361 if (VOP_ISLOCKED(vp, NULL))
1362 vprint((char *)0, vp);
1369 * Top level filesystem related information gathering.
1371 static int sysctl_ovfs_conf (SYSCTL_HANDLER_ARGS);
1374 vfs_sysctl(SYSCTL_HANDLER_ARGS)
1376 int *name = (int *)arg1 - 1; /* XXX */
1377 u_int namelen = arg2 + 1; /* XXX */
1378 struct vfsconf *vfsp;
1380 #if 1 || defined(COMPAT_PRELITE2)
1381 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
1383 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
1387 /* all sysctl names at this level are at least name and field */
1389 return (ENOTDIR); /* overloaded */
1390 if (name[0] != VFS_GENERIC) {
1391 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
1392 if (vfsp->vfc_typenum == name[0])
1395 return (EOPNOTSUPP);
1396 return ((*vfsp->vfc_vfsops->vfs_sysctl)(&name[1], namelen - 1,
1397 oldp, oldlenp, newp, newlen, p));
1401 case VFS_MAXTYPENUM:
1404 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int)));
1407 return (ENOTDIR); /* overloaded */
1408 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
1409 if (vfsp->vfc_typenum == name[2])
1412 return (EOPNOTSUPP);
1413 return (SYSCTL_OUT(req, vfsp, sizeof *vfsp));
1415 return (EOPNOTSUPP);
1418 SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD, vfs_sysctl,
1419 "Generic filesystem");
1421 #if 1 || defined(COMPAT_PRELITE2)
1424 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
1427 struct vfsconf *vfsp;
1428 struct ovfsconf ovfs;
1430 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
1431 bzero(&ovfs, sizeof(ovfs));
1432 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
1433 strcpy(ovfs.vfc_name, vfsp->vfc_name);
1434 ovfs.vfc_index = vfsp->vfc_typenum;
1435 ovfs.vfc_refcount = vfsp->vfc_refcount;
1436 ovfs.vfc_flags = vfsp->vfc_flags;
1437 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
1444 #endif /* 1 || COMPAT_PRELITE2 */
1447 * Check to see if a filesystem is mounted on a block device.
1450 vfs_mountedon(struct vnode *vp)
1454 if ((dev = vp->v_rdev) == NULL)
1455 dev = udev2dev(vp->v_udev, (vp->v_type == VBLK));
1456 if (dev != NODEV && dev->si_mountpoint)
1462 * Unmount all filesystems. The list is traversed in reverse order
1463 * of mounting to avoid dependencies.
1466 static int vfs_umountall_callback(struct mount *mp, void *data);
1469 vfs_unmountall(void)
1471 struct thread *td = curthread;
1474 if (td->td_proc == NULL)
1475 td = initproc->p_thread; /* XXX XXX use proc0 instead? */
1478 count = mountlist_scan(vfs_umountall_callback,
1479 &td, MNTSCAN_REVERSE|MNTSCAN_NOBUSY);
1485 vfs_umountall_callback(struct mount *mp, void *data)
1487 struct thread *td = *(struct thread **)data;
1490 error = dounmount(mp, MNT_FORCE, td);
1492 mountlist_remove(mp);
1493 printf("unmount of filesystem mounted from %s failed (",
1494 mp->mnt_stat.f_mntfromname);
1498 printf("%d)\n", error);
1504 * Build hash lists of net addresses and hang them off the mount point.
1505 * Called by ufs_mount() to set up the lists of export addresses.
1508 vfs_hang_addrlist(struct mount *mp, struct netexport *nep,
1509 struct export_args *argp)
1512 struct radix_node_head *rnh;
1514 struct radix_node *rn;
1515 struct sockaddr *saddr, *smask = 0;
1519 if (argp->ex_addrlen == 0) {
1520 if (mp->mnt_flag & MNT_DEFEXPORTED)
1522 np = &nep->ne_defexported;
1523 np->netc_exflags = argp->ex_flags;
1524 np->netc_anon = argp->ex_anon;
1525 np->netc_anon.cr_ref = 1;
1526 mp->mnt_flag |= MNT_DEFEXPORTED;
1530 if (argp->ex_addrlen < 0 || argp->ex_addrlen > MLEN)
1532 if (argp->ex_masklen < 0 || argp->ex_masklen > MLEN)
1535 i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
1536 np = (struct netcred *) malloc(i, M_NETADDR, M_WAITOK);
1537 bzero((caddr_t) np, i);
1538 saddr = (struct sockaddr *) (np + 1);
1539 if ((error = copyin(argp->ex_addr, (caddr_t) saddr, argp->ex_addrlen)))
1541 if (saddr->sa_len > argp->ex_addrlen)
1542 saddr->sa_len = argp->ex_addrlen;
1543 if (argp->ex_masklen) {
1544 smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen);
1545 error = copyin(argp->ex_mask, (caddr_t)smask, argp->ex_masklen);
1548 if (smask->sa_len > argp->ex_masklen)
1549 smask->sa_len = argp->ex_masklen;
1551 i = saddr->sa_family;
1552 if ((rnh = nep->ne_rtable[i]) == 0) {
1554 * Seems silly to initialize every AF when most are not used,
1555 * do so on demand here
1557 SLIST_FOREACH(dom, &domains, dom_next)
1558 if (dom->dom_family == i && dom->dom_rtattach) {
1559 dom->dom_rtattach((void **) &nep->ne_rtable[i],
1563 if ((rnh = nep->ne_rtable[i]) == 0) {
1568 rn = (*rnh->rnh_addaddr) ((char *) saddr, (char *) smask, rnh,
1570 if (rn == 0 || np != (struct netcred *) rn) { /* already exists */
1574 np->netc_exflags = argp->ex_flags;
1575 np->netc_anon = argp->ex_anon;
1576 np->netc_anon.cr_ref = 1;
1579 free(np, M_NETADDR);
1585 vfs_free_netcred(struct radix_node *rn, void *w)
1587 struct radix_node_head *rnh = (struct radix_node_head *) w;
1589 (*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, rnh);
1590 free((caddr_t) rn, M_NETADDR);
1595 * Free the net address hash lists that are hanging off the mount points.
1598 vfs_free_addrlist(struct netexport *nep)
1601 struct radix_node_head *rnh;
1603 for (i = 0; i <= AF_MAX; i++)
1604 if ((rnh = nep->ne_rtable[i])) {
1605 (*rnh->rnh_walktree) (rnh, vfs_free_netcred,
1607 free((caddr_t) rnh, M_RTABLE);
1608 nep->ne_rtable[i] = 0;
1613 vfs_export(struct mount *mp, struct netexport *nep, struct export_args *argp)
1617 if (argp->ex_flags & MNT_DELEXPORT) {
1618 if (mp->mnt_flag & MNT_EXPUBLIC) {
1619 vfs_setpublicfs(NULL, NULL, NULL);
1620 mp->mnt_flag &= ~MNT_EXPUBLIC;
1622 vfs_free_addrlist(nep);
1623 mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
1625 if (argp->ex_flags & MNT_EXPORTED) {
1626 if (argp->ex_flags & MNT_EXPUBLIC) {
1627 if ((error = vfs_setpublicfs(mp, nep, argp)) != 0)
1629 mp->mnt_flag |= MNT_EXPUBLIC;
1631 if ((error = vfs_hang_addrlist(mp, nep, argp)))
1633 mp->mnt_flag |= MNT_EXPORTED;
1640 * Set the publicly exported filesystem (WebNFS). Currently, only
1641 * one public filesystem is possible in the spec (RFC 2054 and 2055)
1644 vfs_setpublicfs(struct mount *mp, struct netexport *nep,
1645 struct export_args *argp)
1652 * mp == NULL -> invalidate the current info, the FS is
1653 * no longer exported. May be called from either vfs_export
1654 * or unmount, so check if it hasn't already been done.
1657 if (nfs_pub.np_valid) {
1658 nfs_pub.np_valid = 0;
1659 if (nfs_pub.np_index != NULL) {
1660 FREE(nfs_pub.np_index, M_TEMP);
1661 nfs_pub.np_index = NULL;
1668 * Only one allowed at a time.
1670 if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
1674 * Get real filehandle for root of exported FS.
1676 bzero((caddr_t)&nfs_pub.np_handle, sizeof(nfs_pub.np_handle));
1677 nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid;
1679 if ((error = VFS_ROOT(mp, &rvp)))
1682 if ((error = VFS_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid)))
1688 * If an indexfile was specified, pull it in.
1690 if (argp->ex_indexfile != NULL) {
1693 error = vn_get_namelen(rvp, &namelen);
1696 MALLOC(nfs_pub.np_index, char *, namelen, M_TEMP,
1698 error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
1699 namelen, (size_t *)0);
1702 * Check for illegal filenames.
1704 for (cp = nfs_pub.np_index; *cp; cp++) {
1712 FREE(nfs_pub.np_index, M_TEMP);
1717 nfs_pub.np_mount = mp;
1718 nfs_pub.np_valid = 1;
1723 vfs_export_lookup(struct mount *mp, struct netexport *nep,
1724 struct sockaddr *nam)
1727 struct radix_node_head *rnh;
1728 struct sockaddr *saddr;
1731 if (mp->mnt_flag & MNT_EXPORTED) {
1733 * Lookup in the export list first.
1737 rnh = nep->ne_rtable[saddr->sa_family];
1739 np = (struct netcred *)
1740 (*rnh->rnh_matchaddr)((char *)saddr,
1742 if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
1747 * If no address match, use the default if it exists.
1749 if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
1750 np = &nep->ne_defexported;
1756 * perform msync on all vnodes under a mount point. The mount point must
1757 * be locked. This code is also responsible for lazy-freeing unreferenced
1758 * vnodes whos VM objects no longer contain pages.
1760 * NOTE: MNT_WAIT still skips vnodes in the VXLOCK state.
1762 * NOTE: XXX VOP_PUTPAGES and friends requires that the vnode be locked,
1763 * but vnode_pager_putpages() doesn't lock the vnode. We have to do it
1764 * way up in this high level function.
1766 static int vfs_msync_scan1(struct mount *mp, struct vnode *vp, void *data);
1767 static int vfs_msync_scan2(struct mount *mp, struct vnode *vp, void *data);
1770 vfs_msync(struct mount *mp, int flags)
1774 vmsc_flags = VMSC_GETVP;
1775 if (flags != MNT_WAIT)
1776 vmsc_flags |= VMSC_NOWAIT;
1777 vmntvnodescan(mp, vmsc_flags, vfs_msync_scan1, vfs_msync_scan2,
1782 * scan1 is a fast pre-check. There could be hundreds of thousands of
1783 * vnodes, we cannot afford to do anything heavy weight until we have a
1784 * fairly good indication that there is work to do.
1788 vfs_msync_scan1(struct mount *mp, struct vnode *vp, void *data)
1790 int flags = (int)data;
1792 if ((vp->v_flag & VRECLAIMED) == 0) {
1793 if (vshouldfree(vp, 0))
1794 return(0); /* call scan2 */
1795 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
1796 (vp->v_flag & VOBJDIRTY) &&
1797 (flags == MNT_WAIT || VOP_ISLOCKED(vp, NULL) == 0)) {
1798 return(0); /* call scan2 */
1803 * do not call scan2, continue the loop
1809 * This callback is handed a locked vnode.
1813 vfs_msync_scan2(struct mount *mp, struct vnode *vp, void *data)
1816 int flags = (int)data;
1818 if (vp->v_flag & VRECLAIMED)
1821 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
1822 (vp->v_flag & VOBJDIRTY)) {
1823 if (VOP_GETVOBJECT(vp, &obj) == 0) {
1824 vm_object_page_clean(obj, 0, 0,
1825 flags == MNT_WAIT ? OBJPC_SYNC : OBJPC_NOSYNC);
1832 * Create the VM object needed for VMIO and mmap support. This
1833 * is done for all VREG files in the system. Some filesystems might
1834 * afford the additional metadata buffering capability of the
1835 * VMIO code by making the device node be VMIO mode also.
1837 * vp must be locked when vfs_object_create is called.
1840 vfs_object_create(struct vnode *vp, struct thread *td)
1842 return (VOP_CREATEVOBJECT(vp, td));
1846 * Record a process's interest in events which might happen to
1847 * a vnode. Because poll uses the historic select-style interface
1848 * internally, this routine serves as both the ``check for any
1849 * pending events'' and the ``record my interest in future events''
1850 * functions. (These are done together, while the lock is held,
1851 * to avoid race conditions.)
1854 vn_pollrecord(struct vnode *vp, struct thread *td, int events)
1858 lwkt_gettoken(&ilock, &vp->v_pollinfo.vpi_token);
1859 if (vp->v_pollinfo.vpi_revents & events) {
1861 * This leaves events we are not interested
1862 * in available for the other process which
1863 * which presumably had requested them
1864 * (otherwise they would never have been
1867 events &= vp->v_pollinfo.vpi_revents;
1868 vp->v_pollinfo.vpi_revents &= ~events;
1870 lwkt_reltoken(&ilock);
1873 vp->v_pollinfo.vpi_events |= events;
1874 selrecord(td, &vp->v_pollinfo.vpi_selinfo);
1875 lwkt_reltoken(&ilock);
1880 * Note the occurrence of an event. If the VN_POLLEVENT macro is used,
1881 * it is possible for us to miss an event due to race conditions, but
1882 * that condition is expected to be rare, so for the moment it is the
1883 * preferred interface.
1886 vn_pollevent(struct vnode *vp, int events)
1890 lwkt_gettoken(&ilock, &vp->v_pollinfo.vpi_token);
1891 if (vp->v_pollinfo.vpi_events & events) {
1893 * We clear vpi_events so that we don't
1894 * call selwakeup() twice if two events are
1895 * posted before the polling process(es) is
1896 * awakened. This also ensures that we take at
1897 * most one selwakeup() if the polling process
1898 * is no longer interested. However, it does
1899 * mean that only one event can be noticed at
1900 * a time. (Perhaps we should only clear those
1901 * event bits which we note?) XXX
1903 vp->v_pollinfo.vpi_events = 0; /* &= ~events ??? */
1904 vp->v_pollinfo.vpi_revents |= events;
1905 selwakeup(&vp->v_pollinfo.vpi_selinfo);
1907 lwkt_reltoken(&ilock);
1911 * Wake up anyone polling on vp because it is being revoked.
1912 * This depends on dead_poll() returning POLLHUP for correct
1916 vn_pollgone(struct vnode *vp)
1920 lwkt_gettoken(&ilock, &vp->v_pollinfo.vpi_token);
1921 if (vp->v_pollinfo.vpi_events) {
1922 vp->v_pollinfo.vpi_events = 0;
1923 selwakeup(&vp->v_pollinfo.vpi_selinfo);
1925 lwkt_reltoken(&ilock);
1929 * extract the dev_t from a VBLK or VCHR. The vnode must have been opened
1930 * (or v_rdev might be NULL).
1933 vn_todev(struct vnode *vp)
1935 if (vp->v_type != VBLK && vp->v_type != VCHR)
1937 KKASSERT(vp->v_rdev != NULL);
1938 return (vp->v_rdev);
1942 * Check if vnode represents a disk device. The vnode does not need to be
1946 vn_isdisk(struct vnode *vp, int *errp)
1950 if (vp->v_type != VBLK && vp->v_type != VCHR) {
1956 if ((dev = vp->v_rdev) == NULL)
1957 dev = udev2dev(vp->v_udev, (vp->v_type == VBLK));
1958 if (dev == NULL || dev == NODEV) {
1963 if (dev_is_good(dev) == 0) {
1968 if ((dev_dflags(dev) & D_DISK) == 0) {
1978 #ifdef DEBUG_VFS_LOCKS
1981 assert_vop_locked(struct vnode *vp, const char *str)
1983 if (vp && IS_LOCKING_VFS(vp) && !VOP_ISLOCKED(vp, NULL)) {
1984 panic("%s: %p is not locked shared but should be", str, vp);
1989 assert_vop_unlocked(struct vnode *vp, const char *str)
1991 if (vp && IS_LOCKING_VFS(vp)) {
1992 if (VOP_ISLOCKED(vp, curthread) == LK_EXCLUSIVE) {
1993 panic("%s: %p is locked but should not be", str, vp);
2001 vn_get_namelen(struct vnode *vp, int *namelen)
2003 int error, retval[2];
2005 error = VOP_PATHCONF(vp, _PC_NAME_MAX, retval);
2013 vop_write_dirent(int *error, struct uio *uio, ino_t d_ino, uint8_t d_type,
2014 uint16_t d_namlen, const char *d_name)
2019 len = _DIRENT_RECLEN(d_namlen);
2020 if (len > uio->uio_resid)
2023 dp = malloc(len, M_TEMP, M_WAITOK | M_ZERO);
2026 dp->d_namlen = d_namlen;
2027 dp->d_type = d_type;
2028 bcopy(d_name, dp->d_name, d_namlen);
2030 *error = uiomove((caddr_t)dp, len, uio);