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.118 2008/09/17 21:44:18 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>
57 #include <sys/kernel.h>
58 #include <sys/kthread.h>
59 #include <sys/malloc.h>
61 #include <sys/mount.h>
63 #include <sys/reboot.h>
64 #include <sys/socket.h>
66 #include <sys/sysctl.h>
67 #include <sys/syslog.h>
68 #include <sys/unistd.h>
69 #include <sys/vmmeter.h>
70 #include <sys/vnode.h>
72 #include <machine/limits.h>
75 #include <vm/vm_object.h>
76 #include <vm/vm_extern.h>
77 #include <vm/vm_kern.h>
79 #include <vm/vm_map.h>
80 #include <vm/vm_page.h>
81 #include <vm/vm_pager.h>
82 #include <vm/vnode_pager.h>
83 #include <vm/vm_zone.h>
86 #include <sys/thread2.h>
87 #include <sys/sysref2.h>
89 static MALLOC_DEFINE(M_NETADDR, "Export Host", "Export host address structure");
92 SYSCTL_INT(_debug, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0, "");
94 SYSCTL_INT(_vfs, OID_AUTO, fastdev, CTLFLAG_RW, &vfs_fastdev, 0, "");
96 enum vtype iftovt_tab[16] = {
97 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
98 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
100 int vttoif_tab[9] = {
101 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
102 S_IFSOCK, S_IFIFO, S_IFMT,
105 static int reassignbufcalls;
106 SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW,
107 &reassignbufcalls, 0, "");
108 static int reassignbufloops;
109 SYSCTL_INT(_vfs, OID_AUTO, reassignbufloops, CTLFLAG_RW,
110 &reassignbufloops, 0, "");
111 static int reassignbufsortgood;
112 SYSCTL_INT(_vfs, OID_AUTO, reassignbufsortgood, CTLFLAG_RW,
113 &reassignbufsortgood, 0, "");
114 static int reassignbufsortbad;
115 SYSCTL_INT(_vfs, OID_AUTO, reassignbufsortbad, CTLFLAG_RW,
116 &reassignbufsortbad, 0, "");
117 static int reassignbufmethod = 1;
118 SYSCTL_INT(_vfs, OID_AUTO, reassignbufmethod, CTLFLAG_RW,
119 &reassignbufmethod, 0, "");
121 int nfs_mount_type = -1;
122 static struct lwkt_token spechash_token;
123 struct nfs_public nfs_pub; /* publicly exported FS */
126 SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RW,
127 &desiredvnodes, 0, "Maximum number of vnodes");
129 static void vfs_free_addrlist (struct netexport *nep);
130 static int vfs_free_netcred (struct radix_node *rn, void *w);
131 static int vfs_hang_addrlist (struct mount *mp, struct netexport *nep,
132 const struct export_args *argp);
134 extern int dev_ref_debug;
137 * Red black tree functions
139 static int rb_buf_compare(struct buf *b1, struct buf *b2);
140 RB_GENERATE2(buf_rb_tree, buf, b_rbnode, rb_buf_compare, off_t, b_loffset);
141 RB_GENERATE2(buf_rb_hash, buf, b_rbhash, rb_buf_compare, off_t, b_loffset);
144 rb_buf_compare(struct buf *b1, struct buf *b2)
146 if (b1->b_loffset < b2->b_loffset)
148 if (b1->b_loffset > b2->b_loffset)
154 * Returns non-zero if the vnode is a candidate for lazy msyncing.
157 vshouldmsync(struct vnode *vp)
159 if (vp->v_auxrefs != 0 || vp->v_sysref.refcnt > 0)
160 return (0); /* other holders */
162 (vp->v_object->ref_count || vp->v_object->resident_page_count)) {
169 * Initialize the vnode management data structures.
171 * Called from vfsinit()
177 * Desiredvnodes is kern.maxvnodes. We want to scale it
178 * according to available system memory but we may also have
179 * to limit it based on available KVM, which is capped on 32 bit
182 desiredvnodes = min(maxproc + vmstats.v_page_count / 4,
184 (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.
209 vfs_timestamp(struct timespec *tsp)
213 switch (timestamp_precision) {
215 tsp->tv_sec = time_second;
223 TIMEVAL_TO_TIMESPEC(&tv, tsp);
233 * Set vnode attributes to VNOVAL
236 vattr_null(struct vattr *vap)
239 vap->va_size = VNOVAL;
240 vap->va_bytes = VNOVAL;
241 vap->va_mode = VNOVAL;
242 vap->va_nlink = VNOVAL;
243 vap->va_uid = VNOVAL;
244 vap->va_gid = VNOVAL;
245 vap->va_fsid = VNOVAL;
246 vap->va_fileid = VNOVAL;
247 vap->va_blocksize = VNOVAL;
248 vap->va_rmajor = VNOVAL;
249 vap->va_rminor = 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;
260 /* va_*_uuid fields are only valid if related flags are set */
264 * Flush out and invalidate all buffers associated with a vnode.
268 static int vinvalbuf_bp(struct buf *bp, void *data);
270 struct vinvalbuf_bp_info {
278 vupdatefsmid(struct vnode *vp)
280 atomic_set_int(&vp->v_flag, VFSMID);
284 vinvalbuf(struct vnode *vp, int flags, int slpflag, int slptimeo)
286 struct vinvalbuf_bp_info info;
291 lwkt_gettoken(&vlock, &vp->v_token);
294 * If we are being asked to save, call fsync to ensure that the inode
297 if (flags & V_SAVE) {
298 error = bio_track_wait(&vp->v_track_write, slpflag, slptimeo);
301 if (!RB_EMPTY(&vp->v_rbdirty_tree)) {
302 if ((error = VOP_FSYNC(vp, MNT_WAIT)) != 0)
306 * Dirty bufs may be left or generated via races
307 * in circumstances where vinvalbuf() is called on
308 * a vnode not undergoing reclamation. Only
309 * panic if we are trying to reclaim the vnode.
311 if ((vp->v_flag & VRECLAIMED) &&
312 (bio_track_active(&vp->v_track_write) ||
313 !RB_EMPTY(&vp->v_rbdirty_tree))) {
314 panic("vinvalbuf: dirty bufs");
318 info.slptimeo = slptimeo;
319 info.lkflags = LK_EXCLUSIVE | LK_SLEEPFAIL;
320 if (slpflag & PCATCH)
321 info.lkflags |= LK_PCATCH;
326 * Flush the buffer cache until nothing is left.
328 while (!RB_EMPTY(&vp->v_rbclean_tree) ||
329 !RB_EMPTY(&vp->v_rbdirty_tree)) {
330 error = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree, NULL,
331 vinvalbuf_bp, &info);
333 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
334 vinvalbuf_bp, &info);
339 * Wait for I/O completion. We may block in the pip code so we have
343 bio_track_wait(&vp->v_track_write, 0, 0);
344 if ((object = vp->v_object) != NULL) {
345 while (object->paging_in_progress)
346 vm_object_pip_sleep(object, "vnvlbx");
348 } while (bio_track_active(&vp->v_track_write));
351 * Destroy the copy in the VM cache, too.
353 if ((object = vp->v_object) != NULL) {
354 vm_object_page_remove(object, 0, 0,
355 (flags & V_SAVE) ? TRUE : FALSE);
358 if (!RB_EMPTY(&vp->v_rbdirty_tree) || !RB_EMPTY(&vp->v_rbclean_tree))
359 panic("vinvalbuf: flush failed");
360 if (!RB_EMPTY(&vp->v_rbhash_tree))
361 panic("vinvalbuf: flush failed, buffers still present");
364 lwkt_reltoken(&vlock);
369 vinvalbuf_bp(struct buf *bp, void *data)
371 struct vinvalbuf_bp_info *info = data;
374 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
375 error = BUF_TIMELOCK(bp, info->lkflags,
376 "vinvalbuf", info->slptimeo);
386 KKASSERT(bp->b_vp == info->vp);
389 * XXX Since there are no node locks for NFS, I
390 * believe there is a slight chance that a delayed
391 * write will occur while sleeping just above, so
392 * check for it. Note that vfs_bio_awrite expects
393 * buffers to reside on a queue, while bwrite() and
396 * NOTE: NO B_LOCKED CHECK. Also no buf_checkwrite()
397 * check. This code will write out the buffer, period.
399 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
400 (info->flags & V_SAVE)) {
401 if (bp->b_vp == info->vp) {
402 if (bp->b_flags & B_CLUSTEROK) {
412 } else if (info->flags & V_SAVE) {
414 * Cannot set B_NOCACHE on a clean buffer as this will
415 * destroy the VM backing store which might actually
416 * be dirty (and unsynchronized).
419 bp->b_flags |= (B_INVAL | B_RELBUF);
423 bp->b_flags |= (B_INVAL | B_NOCACHE | B_RELBUF);
430 * Truncate a file's buffer and pages to a specified length. This
431 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
434 * The vnode must be locked.
436 static int vtruncbuf_bp_trunc_cmp(struct buf *bp, void *data);
437 static int vtruncbuf_bp_trunc(struct buf *bp, void *data);
438 static int vtruncbuf_bp_metasync_cmp(struct buf *bp, void *data);
439 static int vtruncbuf_bp_metasync(struct buf *bp, void *data);
442 vtruncbuf(struct vnode *vp, off_t length, int blksize)
445 const char *filename;
450 * Round up to the *next* block, then destroy the buffers in question.
451 * Since we are only removing some of the buffers we must rely on the
452 * scan count to determine whether a loop is necessary.
454 if ((count = (int)(length % blksize)) != 0)
455 truncloffset = length + (blksize - count);
457 truncloffset = length;
459 lwkt_gettoken(&vlock, &vp->v_token);
461 count = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree,
462 vtruncbuf_bp_trunc_cmp,
463 vtruncbuf_bp_trunc, &truncloffset);
464 count += RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
465 vtruncbuf_bp_trunc_cmp,
466 vtruncbuf_bp_trunc, &truncloffset);
470 * For safety, fsync any remaining metadata if the file is not being
471 * truncated to 0. Since the metadata does not represent the entire
472 * dirty list we have to rely on the hit count to ensure that we get
477 count = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
478 vtruncbuf_bp_metasync_cmp,
479 vtruncbuf_bp_metasync, vp);
484 * Clean out any left over VM backing store.
486 * It is possible to have in-progress I/O from buffers that were
487 * not part of the truncation. This should not happen if we
488 * are truncating to 0-length.
490 vnode_pager_setsize(vp, length);
491 bio_track_wait(&vp->v_track_write, 0, 0);
493 filename = TAILQ_FIRST(&vp->v_namecache) ?
494 TAILQ_FIRST(&vp->v_namecache)->nc_name : "?";
497 * Make sure no buffers were instantiated while we were trying
498 * to clean out the remaining VM pages. This could occur due
499 * to busy dirty VM pages being flushed out to disk.
502 count = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree,
503 vtruncbuf_bp_trunc_cmp,
504 vtruncbuf_bp_trunc, &truncloffset);
505 count += RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
506 vtruncbuf_bp_trunc_cmp,
507 vtruncbuf_bp_trunc, &truncloffset);
509 kprintf("Warning: vtruncbuf(): Had to re-clean %d "
510 "left over buffers in %s\n", count, filename);
514 lwkt_reltoken(&vlock);
520 * The callback buffer is beyond the new file EOF and must be destroyed.
521 * Note that the compare function must conform to the RB_SCAN's requirements.
525 vtruncbuf_bp_trunc_cmp(struct buf *bp, void *data)
527 if (bp->b_loffset >= *(off_t *)data)
534 vtruncbuf_bp_trunc(struct buf *bp, void *data)
537 * Do not try to use a buffer we cannot immediately lock, but sleep
538 * anyway to prevent a livelock. The code will loop until all buffers
541 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
542 if (BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL) == 0)
546 bp->b_flags |= (B_INVAL | B_RELBUF | B_NOCACHE);
553 * Fsync all meta-data after truncating a file to be non-zero. Only metadata
554 * blocks (with a negative loffset) are scanned.
555 * Note that the compare function must conform to the RB_SCAN's requirements.
558 vtruncbuf_bp_metasync_cmp(struct buf *bp, void *data)
560 if (bp->b_loffset < 0)
566 vtruncbuf_bp_metasync(struct buf *bp, void *data)
568 struct vnode *vp = data;
570 if (bp->b_flags & B_DELWRI) {
572 * Do not try to use a buffer we cannot immediately lock,
573 * but sleep anyway to prevent a livelock. The code will
574 * loop until all buffers can be acted upon.
576 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
577 if (BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL) == 0)
593 * vfsync - implements a multipass fsync on a file which understands
594 * dependancies and meta-data. The passed vnode must be locked. The
595 * waitfor argument may be MNT_WAIT or MNT_NOWAIT, or MNT_LAZY.
597 * When fsyncing data asynchronously just do one consolidated pass starting
598 * with the most negative block number. This may not get all the data due
601 * When fsyncing data synchronously do a data pass, then a metadata pass,
602 * then do additional data+metadata passes to try to get all the data out.
604 static int vfsync_wait_output(struct vnode *vp,
605 int (*waitoutput)(struct vnode *, struct thread *));
606 static int vfsync_data_only_cmp(struct buf *bp, void *data);
607 static int vfsync_meta_only_cmp(struct buf *bp, void *data);
608 static int vfsync_lazy_range_cmp(struct buf *bp, void *data);
609 static int vfsync_bp(struct buf *bp, void *data);
618 int (*checkdef)(struct buf *);
622 vfsync(struct vnode *vp, int waitfor, int passes,
623 int (*checkdef)(struct buf *),
624 int (*waitoutput)(struct vnode *, struct thread *))
626 struct vfsync_info info;
630 bzero(&info, sizeof(info));
632 if ((info.checkdef = checkdef) == NULL)
635 lwkt_gettoken(&vlock, &vp->v_token);
640 * Lazy (filesystem syncer typ) Asynchronous plus limit the
641 * number of data (not meta) pages we try to flush to 1MB.
642 * A non-zero return means that lazy limit was reached.
644 info.lazylimit = 1024 * 1024;
646 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
647 vfsync_lazy_range_cmp, vfsync_bp, &info);
648 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
649 vfsync_meta_only_cmp, vfsync_bp, &info);
652 else if (!RB_EMPTY(&vp->v_rbdirty_tree))
653 vn_syncer_add_to_worklist(vp, 1);
658 * Asynchronous. Do a data-only pass and a meta-only pass.
661 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_data_only_cmp,
663 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_meta_only_cmp,
669 * Synchronous. Do a data-only pass, then a meta-data+data
670 * pass, then additional integrated passes to try to get
671 * all the dependancies flushed.
673 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_data_only_cmp,
675 error = vfsync_wait_output(vp, waitoutput);
677 info.skippedbufs = 0;
678 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
680 error = vfsync_wait_output(vp, waitoutput);
681 if (info.skippedbufs)
682 kprintf("Warning: vfsync skipped %d dirty bufs in pass2!\n", info.skippedbufs);
684 while (error == 0 && passes > 0 &&
685 !RB_EMPTY(&vp->v_rbdirty_tree)
688 info.synchronous = 1;
691 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
697 error = vfsync_wait_output(vp, waitoutput);
701 lwkt_reltoken(&vlock);
706 vfsync_wait_output(struct vnode *vp,
707 int (*waitoutput)(struct vnode *, struct thread *))
711 error = bio_track_wait(&vp->v_track_write, 0, 0);
713 error = waitoutput(vp, curthread);
718 vfsync_data_only_cmp(struct buf *bp, void *data)
720 if (bp->b_loffset < 0)
726 vfsync_meta_only_cmp(struct buf *bp, void *data)
728 if (bp->b_loffset < 0)
734 vfsync_lazy_range_cmp(struct buf *bp, void *data)
736 struct vfsync_info *info = data;
737 if (bp->b_loffset < info->vp->v_lazyw)
743 vfsync_bp(struct buf *bp, void *data)
745 struct vfsync_info *info = data;
746 struct vnode *vp = info->vp;
750 * if syncdeps is not set we do not try to write buffers which have
753 if (!info->synchronous && info->syncdeps == 0 && info->checkdef(bp))
757 * Ignore buffers that we cannot immediately lock. XXX
759 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
760 kprintf("Warning: vfsync_bp skipping dirty buffer %p\n", bp);
764 if ((bp->b_flags & B_DELWRI) == 0)
765 panic("vfsync_bp: buffer not dirty");
767 panic("vfsync_bp: buffer vp mismatch");
770 * B_NEEDCOMMIT (primarily used by NFS) is a state where the buffer
771 * has been written but an additional handshake with the device
772 * is required before we can dispose of the buffer. We have no idea
773 * how to do this so we have to skip these buffers.
775 if (bp->b_flags & B_NEEDCOMMIT) {
781 * Ask bioops if it is ok to sync
783 if (LIST_FIRST(&bp->b_dep) != NULL && buf_checkwrite(bp)) {
789 if (info->synchronous) {
791 * Synchronous flushing. An error may be returned.
797 * Asynchronous flushing. A negative return value simply
798 * stops the scan and is not considered an error. We use
799 * this to support limited MNT_LAZY flushes.
801 vp->v_lazyw = bp->b_loffset;
802 if ((vp->v_flag & VOBJBUF) && (bp->b_flags & B_CLUSTEROK)) {
803 info->lazycount += vfs_bio_awrite(bp);
805 info->lazycount += bp->b_bufsize;
809 if (info->lazylimit && info->lazycount >= info->lazylimit)
818 * Associate a buffer with a vnode.
823 bgetvp(struct vnode *vp, struct buf *bp)
827 KASSERT(bp->b_vp == NULL, ("bgetvp: not free"));
828 KKASSERT((bp->b_flags & (B_HASHED|B_DELWRI|B_VNCLEAN|B_VNDIRTY)) == 0);
831 * Insert onto list for new vnode.
833 lwkt_gettoken(&vlock, &vp->v_token);
834 if (buf_rb_hash_RB_INSERT(&vp->v_rbhash_tree, bp)) {
835 lwkt_reltoken(&vlock);
839 bp->b_flags |= B_HASHED;
840 bp->b_flags |= B_VNCLEAN;
841 if (buf_rb_tree_RB_INSERT(&vp->v_rbclean_tree, bp))
842 panic("reassignbuf: dup lblk/clean vp %p bp %p", vp, bp);
844 lwkt_reltoken(&vlock);
849 * Disassociate a buffer from a vnode.
852 brelvp(struct buf *bp)
857 KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
860 * Delete from old vnode list, if on one.
863 lwkt_gettoken(&vlock, &vp->v_token);
864 if (bp->b_flags & (B_VNDIRTY | B_VNCLEAN)) {
865 if (bp->b_flags & B_VNDIRTY)
866 buf_rb_tree_RB_REMOVE(&vp->v_rbdirty_tree, bp);
868 buf_rb_tree_RB_REMOVE(&vp->v_rbclean_tree, bp);
869 bp->b_flags &= ~(B_VNDIRTY | B_VNCLEAN);
871 if (bp->b_flags & B_HASHED) {
872 buf_rb_hash_RB_REMOVE(&vp->v_rbhash_tree, bp);
873 bp->b_flags &= ~B_HASHED;
875 if ((vp->v_flag & VONWORKLST) && RB_EMPTY(&vp->v_rbdirty_tree)) {
876 vp->v_flag &= ~VONWORKLST;
877 LIST_REMOVE(vp, v_synclist);
880 lwkt_reltoken(&vlock);
886 * Reassign the buffer to the proper clean/dirty list based on B_DELWRI.
887 * 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;
898 KKASSERT(vp != NULL);
902 * B_PAGING flagged buffers cannot be reassigned because their vp
903 * is not fully linked in.
905 if (bp->b_flags & B_PAGING)
906 panic("cannot reassign paging buffer");
908 lwkt_gettoken(&vlock, &vp->v_token);
909 if (bp->b_flags & B_DELWRI) {
911 * Move to the dirty list, add the vnode to the worklist
913 if (bp->b_flags & B_VNCLEAN) {
914 buf_rb_tree_RB_REMOVE(&vp->v_rbclean_tree, bp);
915 bp->b_flags &= ~B_VNCLEAN;
917 if ((bp->b_flags & B_VNDIRTY) == 0) {
918 if (buf_rb_tree_RB_INSERT(&vp->v_rbdirty_tree, bp)) {
919 panic("reassignbuf: dup lblk vp %p bp %p",
922 bp->b_flags |= B_VNDIRTY;
924 if ((vp->v_flag & VONWORKLST) == 0) {
925 switch (vp->v_type) {
932 vp->v_rdev->si_mountpoint != NULL) {
940 vn_syncer_add_to_worklist(vp, delay);
944 * Move to the clean list, remove the vnode from the worklist
945 * if no dirty blocks remain.
947 if (bp->b_flags & B_VNDIRTY) {
948 buf_rb_tree_RB_REMOVE(&vp->v_rbdirty_tree, bp);
949 bp->b_flags &= ~B_VNDIRTY;
951 if ((bp->b_flags & B_VNCLEAN) == 0) {
952 if (buf_rb_tree_RB_INSERT(&vp->v_rbclean_tree, bp)) {
953 panic("reassignbuf: dup lblk vp %p bp %p",
956 bp->b_flags |= B_VNCLEAN;
958 if ((vp->v_flag & VONWORKLST) &&
959 RB_EMPTY(&vp->v_rbdirty_tree)) {
960 vp->v_flag &= ~VONWORKLST;
961 LIST_REMOVE(vp, v_synclist);
964 lwkt_reltoken(&vlock);
968 * Create a vnode for a block device.
969 * Used for mounting the root file system.
971 extern struct vop_ops *devfs_vnode_dev_vops_p;
973 bdevvp(cdev_t dev, struct vnode **vpp)
983 error = getspecialvnode(VT_NON, NULL, &devfs_vnode_dev_vops_p/*&spec_vnode_vops_p*/, &nvp, 0, 0);
991 vp->v_umajor = dev->si_umajor;
992 vp->v_uminor = dev->si_uminor;
999 v_associate_rdev(struct vnode *vp, cdev_t dev)
1005 if (dev_is_good(dev) == 0)
1007 KKASSERT(vp->v_rdev == NULL);
1010 vp->v_rdev = reference_dev(dev);
1011 lwkt_gettoken(&ilock, &spechash_token);
1012 SLIST_INSERT_HEAD(&dev->si_hlist, vp, v_cdevnext);
1013 lwkt_reltoken(&ilock);
1018 v_release_rdev(struct vnode *vp)
1023 if ((dev = vp->v_rdev) != NULL) {
1024 lwkt_gettoken(&ilock, &spechash_token);
1025 SLIST_REMOVE(&dev->si_hlist, vp, vnode, v_cdevnext);
1028 lwkt_reltoken(&ilock);
1033 * Add a vnode to the alias list hung off the cdev_t. We only associate
1034 * the device number with the vnode. The actual device is not associated
1035 * until the vnode is opened (usually in spec_open()), and will be
1036 * disassociated on last close.
1039 addaliasu(struct vnode *nvp, int x, int y)
1041 if (nvp->v_type != VBLK && nvp->v_type != VCHR)
1042 panic("addaliasu on non-special vnode");
1048 * Simple call that a filesystem can make to try to get rid of a
1049 * vnode. It will fail if anyone is referencing the vnode (including
1052 * The filesystem can check whether its in-memory inode structure still
1053 * references the vp on return.
1056 vclean_unlocked(struct vnode *vp)
1059 if (sysref_isactive(&vp->v_sysref) == 0)
1065 * Disassociate a vnode from its underlying filesystem.
1067 * The vnode must be VX locked and referenced. In all normal situations
1068 * there are no active references. If vclean_vxlocked() is called while
1069 * there are active references, the vnode is being ripped out and we have
1070 * to call VOP_CLOSE() as appropriate before we can reclaim it.
1073 vclean_vxlocked(struct vnode *vp, int flags)
1080 * If the vnode has already been reclaimed we have nothing to do.
1082 if (vp->v_flag & VRECLAIMED)
1084 vp->v_flag |= VRECLAIMED;
1087 * Scrap the vfs cache
1089 while (cache_inval_vp(vp, 0) != 0) {
1090 kprintf("Warning: vnode %p clean/cache_resolution race detected\n", vp);
1091 tsleep(vp, 0, "vclninv", 2);
1095 * Check to see if the vnode is in use. If so we have to reference it
1096 * before we clean it out so that its count cannot fall to zero and
1097 * generate a race against ourselves to recycle it.
1099 active = sysref_isactive(&vp->v_sysref);
1102 * Clean out any buffers associated with the vnode and destroy its
1103 * object, if it has one.
1105 vinvalbuf(vp, V_SAVE, 0, 0);
1108 * If purging an active vnode (typically during a forced unmount
1109 * or reboot), it must be closed and deactivated before being
1110 * reclaimed. This isn't really all that safe, but what can
1113 * Note that neither of these routines unlocks the vnode.
1115 if (active && (flags & DOCLOSE)) {
1116 while ((n = vp->v_opencount) != 0) {
1117 if (vp->v_writecount)
1118 VOP_CLOSE(vp, FWRITE|FNONBLOCK);
1120 VOP_CLOSE(vp, FNONBLOCK);
1121 if (vp->v_opencount == n) {
1122 kprintf("Warning: unable to force-close"
1130 * If the vnode has not been deactivated, deactivated it. Deactivation
1131 * can create new buffers and VM pages so we have to call vinvalbuf()
1132 * again to make sure they all get flushed.
1134 * This can occur if a file with a link count of 0 needs to be
1137 if ((vp->v_flag & VINACTIVE) == 0) {
1138 vp->v_flag |= VINACTIVE;
1140 vinvalbuf(vp, V_SAVE, 0, 0);
1144 * If the vnode has an object, destroy it.
1146 if ((object = vp->v_object) != NULL) {
1147 if (object->ref_count == 0) {
1148 if ((object->flags & OBJ_DEAD) == 0)
1149 vm_object_terminate(object);
1151 vm_pager_deallocate(object);
1153 vp->v_flag &= ~VOBJBUF;
1155 KKASSERT((vp->v_flag & VOBJBUF) == 0);
1158 * Reclaim the vnode.
1160 if (VOP_RECLAIM(vp))
1161 panic("vclean: cannot reclaim");
1164 * Done with purge, notify sleepers of the grim news.
1166 vp->v_ops = &dead_vnode_vops_p;
1171 * If we are destroying an active vnode, reactivate it now that
1172 * we have reassociated it with deadfs. This prevents the system
1173 * from crashing on the vnode due to it being unexpectedly marked
1174 * as inactive or reclaimed.
1176 if (active && (flags & DOCLOSE)) {
1177 vp->v_flag &= ~(VINACTIVE|VRECLAIMED);
1182 * Eliminate all activity associated with the requested vnode
1183 * and with all vnodes aliased to the requested vnode.
1185 * The vnode must be referenced but should not be locked.
1188 vrevoke(struct vnode *vp, struct ucred *cred)
1197 * If the vnode has a device association, scrap all vnodes associated
1198 * with the device. Don't let the device disappear on us while we
1199 * are scrapping the vnodes.
1201 * The passed vp will probably show up in the list, do not VX lock
1204 * Releasing the vnode's rdev here can mess up specfs's call to
1205 * device close, so don't do it. The vnode has been disassociated
1206 * and the device will be closed after the last ref on the related
1207 * fp goes away (if not still open by e.g. the kernel).
1209 if (vp->v_type != VCHR) {
1210 error = fdrevoke(vp, DTYPE_VNODE, cred);
1213 if ((dev = vp->v_rdev) == NULL) {
1217 lwkt_gettoken(&ilock, &spechash_token);
1219 vqn = SLIST_FIRST(&dev->si_hlist);
1222 while ((vq = vqn) != NULL) {
1223 vqn = SLIST_NEXT(vqn, v_cdevnext);
1226 fdrevoke(vq, DTYPE_VNODE, cred);
1227 /*v_release_rdev(vq);*/
1230 lwkt_reltoken(&ilock);
1237 * This is called when the object underlying a vnode is being destroyed,
1238 * such as in a remove(). Try to recycle the vnode immediately if the
1239 * only active reference is our reference.
1241 * Directory vnodes in the namecache with children cannot be immediately
1242 * recycled because numerous VOP_N*() ops require them to be stable.
1245 vrecycle(struct vnode *vp)
1247 if (vp->v_sysref.refcnt <= 1) {
1248 if (cache_inval_vp_nonblock(vp))
1257 * Return the maximum I/O size allowed for strategy calls on VP.
1259 * If vp is VCHR or VBLK we dive the device, otherwise we use
1260 * the vp's mount info.
1263 vmaxiosize(struct vnode *vp)
1265 if (vp->v_type == VBLK || vp->v_type == VCHR) {
1266 return(vp->v_rdev->si_iosize_max);
1268 return(vp->v_mount->mnt_iosize_max);
1273 * Eliminate all activity associated with a vnode in preparation for reuse.
1275 * The vnode must be VX locked and refd and will remain VX locked and refd
1276 * on return. This routine may be called with the vnode in any state, as
1277 * long as it is VX locked. The vnode will be cleaned out and marked
1278 * VRECLAIMED but will not actually be reused until all existing refs and
1281 * NOTE: This routine may be called on a vnode which has not yet been
1282 * already been deactivated (VOP_INACTIVE), or on a vnode which has
1283 * already been reclaimed.
1285 * This routine is not responsible for placing us back on the freelist.
1286 * Instead, it happens automatically when the caller releases the VX lock
1287 * (assuming there aren't any other references).
1291 vgone_vxlocked(struct vnode *vp)
1294 * assert that the VX lock is held. This is an absolute requirement
1295 * now for vgone_vxlocked() to be called.
1297 KKASSERT(vp->v_lock.lk_exclusivecount == 1);
1300 * Clean out the filesystem specific data and set the VRECLAIMED
1301 * bit. Also deactivate the vnode if necessary.
1303 vclean_vxlocked(vp, DOCLOSE);
1306 * Delete from old mount point vnode list, if on one.
1308 if (vp->v_mount != NULL)
1309 insmntque(vp, NULL);
1312 * If special device, remove it from special device alias list
1313 * if it is on one. This should normally only occur if a vnode is
1314 * being revoked as the device should otherwise have been released
1317 if ((vp->v_type == VBLK || vp->v_type == VCHR) && vp->v_rdev != NULL) {
1328 * Lookup a vnode by device number.
1330 * Returns non-zero and *vpp set to a vref'd vnode on success.
1331 * Returns zero on failure.
1334 vfinddev(cdev_t dev, enum vtype type, struct vnode **vpp)
1339 lwkt_gettoken(&ilock, &spechash_token);
1340 SLIST_FOREACH(vp, &dev->si_hlist, v_cdevnext) {
1341 if (type == vp->v_type) {
1344 lwkt_reltoken(&ilock);
1348 lwkt_reltoken(&ilock);
1353 * Calculate the total number of references to a special device. This
1354 * routine may only be called for VBLK and VCHR vnodes since v_rdev is
1355 * an overloaded field. Since udev2dev can now return NULL, we have
1356 * to check for a NULL v_rdev.
1359 count_dev(cdev_t dev)
1365 if (SLIST_FIRST(&dev->si_hlist)) {
1366 lwkt_gettoken(&ilock, &spechash_token);
1367 SLIST_FOREACH(vp, &dev->si_hlist, v_cdevnext) {
1368 if (vp->v_sysref.refcnt > 0)
1369 count += vp->v_sysref.refcnt;
1371 lwkt_reltoken(&ilock);
1377 count_udev(int x, int y)
1381 if ((dev = get_dev(x, y)) == NULL)
1383 return(count_dev(dev));
1387 vcount(struct vnode *vp)
1389 if (vp->v_rdev == NULL)
1391 return(count_dev(vp->v_rdev));
1395 * Initialize VMIO for a vnode. This routine MUST be called before a
1396 * VFS can issue buffer cache ops on a vnode. It is typically called
1397 * when a vnode is initialized from its inode.
1400 vinitvmio(struct vnode *vp, off_t filesize)
1406 if ((object = vp->v_object) == NULL) {
1407 object = vnode_pager_alloc(vp, filesize, 0, 0);
1409 * Dereference the reference we just created. This assumes
1410 * that the object is associated with the vp.
1412 object->ref_count--;
1415 if (object->flags & OBJ_DEAD) {
1417 vm_object_dead_sleep(object, "vodead");
1418 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1422 KASSERT(vp->v_object != NULL, ("vinitvmio: NULL object"));
1423 vp->v_flag |= VOBJBUF;
1429 * Print out a description of a vnode.
1431 static char *typename[] =
1432 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD"};
1435 vprint(char *label, struct vnode *vp)
1440 kprintf("%s: %p: ", label, (void *)vp);
1442 kprintf("%p: ", (void *)vp);
1443 kprintf("type %s, sysrefs %d, writecount %d, holdcnt %d,",
1444 typename[vp->v_type],
1445 vp->v_sysref.refcnt, vp->v_writecount, vp->v_auxrefs);
1447 if (vp->v_flag & VROOT)
1448 strcat(buf, "|VROOT");
1449 if (vp->v_flag & VPFSROOT)
1450 strcat(buf, "|VPFSROOT");
1451 if (vp->v_flag & VTEXT)
1452 strcat(buf, "|VTEXT");
1453 if (vp->v_flag & VSYSTEM)
1454 strcat(buf, "|VSYSTEM");
1455 if (vp->v_flag & VFREE)
1456 strcat(buf, "|VFREE");
1457 if (vp->v_flag & VOBJBUF)
1458 strcat(buf, "|VOBJBUF");
1460 kprintf(" flags (%s)", &buf[1]);
1461 if (vp->v_data == NULL) {
1470 #include <ddb/ddb.h>
1472 static int db_show_locked_vnodes(struct mount *mp, void *data);
1475 * List all of the locked vnodes in the system.
1476 * Called when debugging the kernel.
1478 DB_SHOW_COMMAND(lockedvnodes, lockedvnodes)
1480 kprintf("Locked vnodes\n");
1481 mountlist_scan(db_show_locked_vnodes, NULL,
1482 MNTSCAN_FORWARD|MNTSCAN_NOBUSY);
1486 db_show_locked_vnodes(struct mount *mp, void *data __unused)
1490 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
1491 if (vn_islocked(vp))
1499 * Top level filesystem related information gathering.
1501 static int sysctl_ovfs_conf (SYSCTL_HANDLER_ARGS);
1504 vfs_sysctl(SYSCTL_HANDLER_ARGS)
1506 int *name = (int *)arg1 - 1; /* XXX */
1507 u_int namelen = arg2 + 1; /* XXX */
1508 struct vfsconf *vfsp;
1511 #if 1 || defined(COMPAT_PRELITE2)
1512 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
1514 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
1518 /* all sysctl names at this level are at least name and field */
1520 return (ENOTDIR); /* overloaded */
1521 if (name[0] != VFS_GENERIC) {
1522 vfsp = vfsconf_find_by_typenum(name[0]);
1524 return (EOPNOTSUPP);
1525 return ((*vfsp->vfc_vfsops->vfs_sysctl)(&name[1], namelen - 1,
1526 oldp, oldlenp, newp, newlen, p));
1530 case VFS_MAXTYPENUM:
1533 maxtypenum = vfsconf_get_maxtypenum();
1534 return (SYSCTL_OUT(req, &maxtypenum, sizeof(maxtypenum)));
1537 return (ENOTDIR); /* overloaded */
1538 vfsp = vfsconf_find_by_typenum(name[2]);
1540 return (EOPNOTSUPP);
1541 return (SYSCTL_OUT(req, vfsp, sizeof *vfsp));
1543 return (EOPNOTSUPP);
1546 SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD, vfs_sysctl,
1547 "Generic filesystem");
1549 #if 1 || defined(COMPAT_PRELITE2)
1552 sysctl_ovfs_conf_iter(struct vfsconf *vfsp, void *data)
1555 struct ovfsconf ovfs;
1556 struct sysctl_req *req = (struct sysctl_req*) data;
1558 bzero(&ovfs, sizeof(ovfs));
1559 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
1560 strcpy(ovfs.vfc_name, vfsp->vfc_name);
1561 ovfs.vfc_index = vfsp->vfc_typenum;
1562 ovfs.vfc_refcount = vfsp->vfc_refcount;
1563 ovfs.vfc_flags = vfsp->vfc_flags;
1564 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
1566 return error; /* abort iteration with error code */
1568 return 0; /* continue iterating with next element */
1572 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
1574 return vfsconf_each(sysctl_ovfs_conf_iter, (void*)req);
1577 #endif /* 1 || COMPAT_PRELITE2 */
1580 * Check to see if a filesystem is mounted on a block device.
1583 vfs_mountedon(struct vnode *vp)
1587 if ((dev = vp->v_rdev) == NULL) {
1588 /* if (vp->v_type != VBLK)
1589 dev = get_dev(vp->v_uminor, vp->v_umajor); */
1591 if (dev != NULL && dev->si_mountpoint)
1597 * Unmount all filesystems. The list is traversed in reverse order
1598 * of mounting to avoid dependencies.
1601 static int vfs_umountall_callback(struct mount *mp, void *data);
1604 vfs_unmountall(void)
1609 count = mountlist_scan(vfs_umountall_callback,
1610 NULL, MNTSCAN_REVERSE|MNTSCAN_NOBUSY);
1616 vfs_umountall_callback(struct mount *mp, void *data)
1620 error = dounmount(mp, MNT_FORCE);
1622 mountlist_remove(mp);
1623 kprintf("unmount of filesystem mounted from %s failed (",
1624 mp->mnt_stat.f_mntfromname);
1628 kprintf("%d)\n", error);
1634 * Build hash lists of net addresses and hang them off the mount point.
1635 * Called by ufs_mount() to set up the lists of export addresses.
1638 vfs_hang_addrlist(struct mount *mp, struct netexport *nep,
1639 const struct export_args *argp)
1642 struct radix_node_head *rnh;
1644 struct radix_node *rn;
1645 struct sockaddr *saddr, *smask = 0;
1649 if (argp->ex_addrlen == 0) {
1650 if (mp->mnt_flag & MNT_DEFEXPORTED)
1652 np = &nep->ne_defexported;
1653 np->netc_exflags = argp->ex_flags;
1654 np->netc_anon = argp->ex_anon;
1655 np->netc_anon.cr_ref = 1;
1656 mp->mnt_flag |= MNT_DEFEXPORTED;
1660 if (argp->ex_addrlen < 0 || argp->ex_addrlen > MLEN)
1662 if (argp->ex_masklen < 0 || argp->ex_masklen > MLEN)
1665 i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
1666 np = (struct netcred *) kmalloc(i, M_NETADDR, M_WAITOK | M_ZERO);
1667 saddr = (struct sockaddr *) (np + 1);
1668 if ((error = copyin(argp->ex_addr, (caddr_t) saddr, argp->ex_addrlen)))
1670 if (saddr->sa_len > argp->ex_addrlen)
1671 saddr->sa_len = argp->ex_addrlen;
1672 if (argp->ex_masklen) {
1673 smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen);
1674 error = copyin(argp->ex_mask, (caddr_t)smask, argp->ex_masklen);
1677 if (smask->sa_len > argp->ex_masklen)
1678 smask->sa_len = argp->ex_masklen;
1680 i = saddr->sa_family;
1681 if ((rnh = nep->ne_rtable[i]) == 0) {
1683 * Seems silly to initialize every AF when most are not used,
1684 * do so on demand here
1686 SLIST_FOREACH(dom, &domains, dom_next)
1687 if (dom->dom_family == i && dom->dom_rtattach) {
1688 dom->dom_rtattach((void **) &nep->ne_rtable[i],
1692 if ((rnh = nep->ne_rtable[i]) == 0) {
1697 rn = (*rnh->rnh_addaddr) ((char *) saddr, (char *) smask, rnh,
1699 if (rn == 0 || np != (struct netcred *) rn) { /* already exists */
1703 np->netc_exflags = argp->ex_flags;
1704 np->netc_anon = argp->ex_anon;
1705 np->netc_anon.cr_ref = 1;
1708 kfree(np, M_NETADDR);
1714 vfs_free_netcred(struct radix_node *rn, void *w)
1716 struct radix_node_head *rnh = (struct radix_node_head *) w;
1718 (*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, rnh);
1719 kfree((caddr_t) rn, M_NETADDR);
1724 * Free the net address hash lists that are hanging off the mount points.
1727 vfs_free_addrlist(struct netexport *nep)
1730 struct radix_node_head *rnh;
1732 for (i = 0; i <= AF_MAX; i++)
1733 if ((rnh = nep->ne_rtable[i])) {
1734 (*rnh->rnh_walktree) (rnh, vfs_free_netcred,
1736 kfree((caddr_t) rnh, M_RTABLE);
1737 nep->ne_rtable[i] = 0;
1742 vfs_export(struct mount *mp, struct netexport *nep,
1743 const struct export_args *argp)
1747 if (argp->ex_flags & MNT_DELEXPORT) {
1748 if (mp->mnt_flag & MNT_EXPUBLIC) {
1749 vfs_setpublicfs(NULL, NULL, NULL);
1750 mp->mnt_flag &= ~MNT_EXPUBLIC;
1752 vfs_free_addrlist(nep);
1753 mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
1755 if (argp->ex_flags & MNT_EXPORTED) {
1756 if (argp->ex_flags & MNT_EXPUBLIC) {
1757 if ((error = vfs_setpublicfs(mp, nep, argp)) != 0)
1759 mp->mnt_flag |= MNT_EXPUBLIC;
1761 if ((error = vfs_hang_addrlist(mp, nep, argp)))
1763 mp->mnt_flag |= MNT_EXPORTED;
1770 * Set the publicly exported filesystem (WebNFS). Currently, only
1771 * one public filesystem is possible in the spec (RFC 2054 and 2055)
1774 vfs_setpublicfs(struct mount *mp, struct netexport *nep,
1775 const struct export_args *argp)
1782 * mp == NULL -> invalidate the current info, the FS is
1783 * no longer exported. May be called from either vfs_export
1784 * or unmount, so check if it hasn't already been done.
1787 if (nfs_pub.np_valid) {
1788 nfs_pub.np_valid = 0;
1789 if (nfs_pub.np_index != NULL) {
1790 FREE(nfs_pub.np_index, M_TEMP);
1791 nfs_pub.np_index = NULL;
1798 * Only one allowed at a time.
1800 if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
1804 * Get real filehandle for root of exported FS.
1806 bzero((caddr_t)&nfs_pub.np_handle, sizeof(nfs_pub.np_handle));
1807 nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid;
1809 if ((error = VFS_ROOT(mp, &rvp)))
1812 if ((error = VFS_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid)))
1818 * If an indexfile was specified, pull it in.
1820 if (argp->ex_indexfile != NULL) {
1823 error = vn_get_namelen(rvp, &namelen);
1826 MALLOC(nfs_pub.np_index, char *, namelen, M_TEMP,
1828 error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
1832 * Check for illegal filenames.
1834 for (cp = nfs_pub.np_index; *cp; cp++) {
1842 FREE(nfs_pub.np_index, M_TEMP);
1847 nfs_pub.np_mount = mp;
1848 nfs_pub.np_valid = 1;
1853 vfs_export_lookup(struct mount *mp, struct netexport *nep,
1854 struct sockaddr *nam)
1857 struct radix_node_head *rnh;
1858 struct sockaddr *saddr;
1861 if (mp->mnt_flag & MNT_EXPORTED) {
1863 * Lookup in the export list first.
1867 rnh = nep->ne_rtable[saddr->sa_family];
1869 np = (struct netcred *)
1870 (*rnh->rnh_matchaddr)((char *)saddr,
1872 if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
1877 * If no address match, use the default if it exists.
1879 if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
1880 np = &nep->ne_defexported;
1886 * perform msync on all vnodes under a mount point. The mount point must
1887 * be locked. This code is also responsible for lazy-freeing unreferenced
1888 * vnodes whos VM objects no longer contain pages.
1890 * NOTE: MNT_WAIT still skips vnodes in the VXLOCK state.
1892 * NOTE: XXX VOP_PUTPAGES and friends requires that the vnode be locked,
1893 * but vnode_pager_putpages() doesn't lock the vnode. We have to do it
1894 * way up in this high level function.
1896 static int vfs_msync_scan1(struct mount *mp, struct vnode *vp, void *data);
1897 static int vfs_msync_scan2(struct mount *mp, struct vnode *vp, void *data);
1900 vfs_msync(struct mount *mp, int flags)
1904 vmsc_flags = VMSC_GETVP;
1905 if (flags != MNT_WAIT)
1906 vmsc_flags |= VMSC_NOWAIT;
1907 vmntvnodescan(mp, vmsc_flags, vfs_msync_scan1, vfs_msync_scan2,
1908 (void *)(intptr_t)flags);
1912 * scan1 is a fast pre-check. There could be hundreds of thousands of
1913 * vnodes, we cannot afford to do anything heavy weight until we have a
1914 * fairly good indication that there is work to do.
1918 vfs_msync_scan1(struct mount *mp, struct vnode *vp, void *data)
1920 int flags = (int)(intptr_t)data;
1922 if ((vp->v_flag & VRECLAIMED) == 0) {
1923 if (vshouldmsync(vp))
1924 return(0); /* call scan2 */
1925 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
1926 (vp->v_flag & VOBJDIRTY) &&
1927 (flags == MNT_WAIT || vn_islocked(vp) == 0)) {
1928 return(0); /* call scan2 */
1933 * do not call scan2, continue the loop
1939 * This callback is handed a locked vnode.
1943 vfs_msync_scan2(struct mount *mp, struct vnode *vp, void *data)
1946 int flags = (int)(intptr_t)data;
1948 if (vp->v_flag & VRECLAIMED)
1951 if ((mp->mnt_flag & MNT_RDONLY) == 0 && (vp->v_flag & VOBJDIRTY)) {
1952 if ((obj = vp->v_object) != NULL) {
1953 vm_object_page_clean(obj, 0, 0,
1954 flags == MNT_WAIT ? OBJPC_SYNC : OBJPC_NOSYNC);
1961 * Record a process's interest in events which might happen to
1962 * a vnode. Because poll uses the historic select-style interface
1963 * internally, this routine serves as both the ``check for any
1964 * pending events'' and the ``record my interest in future events''
1965 * functions. (These are done together, while the lock is held,
1966 * to avoid race conditions.)
1969 vn_pollrecord(struct vnode *vp, int events)
1973 KKASSERT(curthread->td_proc != NULL);
1975 lwkt_gettoken(&vlock, &vp->v_token);
1976 if (vp->v_pollinfo.vpi_revents & events) {
1978 * This leaves events we are not interested
1979 * in available for the other process which
1980 * which presumably had requested them
1981 * (otherwise they would never have been
1984 events &= vp->v_pollinfo.vpi_revents;
1985 vp->v_pollinfo.vpi_revents &= ~events;
1987 lwkt_reltoken(&vlock);
1990 vp->v_pollinfo.vpi_events |= events;
1991 selrecord(curthread, &vp->v_pollinfo.vpi_selinfo);
1992 lwkt_reltoken(&vlock);
1997 * Note the occurrence of an event. If the VN_POLLEVENT macro is used,
1998 * it is possible for us to miss an event due to race conditions, but
1999 * that condition is expected to be rare, so for the moment it is the
2000 * preferred interface.
2003 vn_pollevent(struct vnode *vp, int events)
2007 lwkt_gettoken(&vlock, &vp->v_token);
2008 if (vp->v_pollinfo.vpi_events & events) {
2010 * We clear vpi_events so that we don't
2011 * call selwakeup() twice if two events are
2012 * posted before the polling process(es) is
2013 * awakened. This also ensures that we take at
2014 * most one selwakeup() if the polling process
2015 * is no longer interested. However, it does
2016 * mean that only one event can be noticed at
2017 * a time. (Perhaps we should only clear those
2018 * event bits which we note?) XXX
2020 vp->v_pollinfo.vpi_events = 0; /* &= ~events ??? */
2021 vp->v_pollinfo.vpi_revents |= events;
2022 selwakeup(&vp->v_pollinfo.vpi_selinfo);
2024 lwkt_reltoken(&vlock);
2028 * Wake up anyone polling on vp because it is being revoked.
2029 * This depends on dead_poll() returning POLLHUP for correct
2033 vn_pollgone(struct vnode *vp)
2037 lwkt_gettoken(&vlock, &vp->v_token);
2038 if (vp->v_pollinfo.vpi_events) {
2039 vp->v_pollinfo.vpi_events = 0;
2040 selwakeup(&vp->v_pollinfo.vpi_selinfo);
2042 lwkt_reltoken(&vlock);
2046 * extract the cdev_t from a VBLK or VCHR. The vnode must have been opened
2047 * (or v_rdev might be NULL).
2050 vn_todev(struct vnode *vp)
2052 if (vp->v_type != VBLK && vp->v_type != VCHR)
2054 KKASSERT(vp->v_rdev != NULL);
2055 return (vp->v_rdev);
2059 * Check if vnode represents a disk device. The vnode does not need to be
2065 vn_isdisk(struct vnode *vp, int *errp)
2069 if (vp->v_type != VCHR) {
2082 if (dev_is_good(dev) == 0) {
2087 if ((dev_dflags(dev) & D_DISK) == 0) {
2098 vn_get_namelen(struct vnode *vp, int *namelen)
2101 register_t retval[2];
2103 error = VOP_PATHCONF(vp, _PC_NAME_MAX, retval);
2106 *namelen = (int)retval[0];
2111 vop_write_dirent(int *error, struct uio *uio, ino_t d_ino, uint8_t d_type,
2112 uint16_t d_namlen, const char *d_name)
2117 len = _DIRENT_RECLEN(d_namlen);
2118 if (len > uio->uio_resid)
2121 dp = kmalloc(len, M_TEMP, M_WAITOK | M_ZERO);
2124 dp->d_namlen = d_namlen;
2125 dp->d_type = d_type;
2126 bcopy(d_name, dp->d_name, d_namlen);
2128 *error = uiomove((caddr_t)dp, len, uio);
2136 vn_mark_atime(struct vnode *vp, struct thread *td)
2138 struct proc *p = td->td_proc;
2139 struct ucred *cred = p ? p->p_ucred : proc0.p_ucred;
2141 if ((vp->v_mount->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0) {
2142 VOP_MARKATIME(vp, cred);