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>
90 * Struct for mount options to printable formats.
97 static MALLOC_DEFINE(M_NETADDR, "Export Host", "Export host address structure");
100 SYSCTL_INT(_debug, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0, "");
102 SYSCTL_INT(_vfs, OID_AUTO, fastdev, CTLFLAG_RW, &vfs_fastdev, 0, "");
104 enum vtype iftovt_tab[16] = {
105 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
106 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
108 int vttoif_tab[9] = {
109 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
110 S_IFSOCK, S_IFIFO, S_IFMT,
113 static int reassignbufcalls;
114 SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW,
115 &reassignbufcalls, 0, "");
116 static int reassignbufloops;
117 SYSCTL_INT(_vfs, OID_AUTO, reassignbufloops, CTLFLAG_RW,
118 &reassignbufloops, 0, "");
119 static int reassignbufsortgood;
120 SYSCTL_INT(_vfs, OID_AUTO, reassignbufsortgood, CTLFLAG_RW,
121 &reassignbufsortgood, 0, "");
122 static int reassignbufsortbad;
123 SYSCTL_INT(_vfs, OID_AUTO, reassignbufsortbad, CTLFLAG_RW,
124 &reassignbufsortbad, 0, "");
125 static int reassignbufmethod = 1;
126 SYSCTL_INT(_vfs, OID_AUTO, reassignbufmethod, CTLFLAG_RW,
127 &reassignbufmethod, 0, "");
129 int nfs_mount_type = -1;
130 static struct lwkt_token spechash_token;
131 struct nfs_public nfs_pub; /* publicly exported FS */
134 SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RW,
135 &desiredvnodes, 0, "Maximum number of vnodes");
137 static void vfs_free_addrlist (struct netexport *nep);
138 static int vfs_free_netcred (struct radix_node *rn, void *w);
139 static int vfs_hang_addrlist (struct mount *mp, struct netexport *nep,
140 const struct export_args *argp);
143 * Red black tree functions
145 static int rb_buf_compare(struct buf *b1, struct buf *b2);
146 RB_GENERATE2(buf_rb_tree, buf, b_rbnode, rb_buf_compare, off_t, b_loffset);
147 RB_GENERATE2(buf_rb_hash, buf, b_rbhash, rb_buf_compare, off_t, b_loffset);
150 rb_buf_compare(struct buf *b1, struct buf *b2)
152 if (b1->b_loffset < b2->b_loffset)
154 if (b1->b_loffset > b2->b_loffset)
160 * Returns non-zero if the vnode is a candidate for lazy msyncing.
163 vshouldmsync(struct vnode *vp)
165 if (vp->v_auxrefs != 0 || vp->v_sysref.refcnt > 0)
166 return (0); /* other holders */
168 (vp->v_object->ref_count || vp->v_object->resident_page_count)) {
175 * Initialize the vnode management data structures.
177 * Called from vfsinit()
183 * Desiredvnodes is kern.maxvnodes. We want to scale it
184 * according to available system memory but we may also have
185 * to limit it based on available KVM, which is capped on 32 bit
188 desiredvnodes = min(maxproc + vmstats.v_page_count / 4,
190 (sizeof(struct vm_object) + sizeof(struct vnode))));
192 lwkt_token_init(&spechash_token);
196 * Knob to control the precision of file timestamps:
198 * 0 = seconds only; nanoseconds zeroed.
199 * 1 = seconds and nanoseconds, accurate within 1/HZ.
200 * 2 = seconds and nanoseconds, truncated to microseconds.
201 * >=3 = seconds and nanoseconds, maximum precision.
203 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC };
205 static int timestamp_precision = TSP_SEC;
206 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW,
207 ×tamp_precision, 0, "");
210 * Get a current timestamp.
215 vfs_timestamp(struct timespec *tsp)
219 switch (timestamp_precision) {
221 tsp->tv_sec = time_second;
229 TIMEVAL_TO_TIMESPEC(&tv, tsp);
239 * Set vnode attributes to VNOVAL
242 vattr_null(struct vattr *vap)
245 vap->va_size = VNOVAL;
246 vap->va_bytes = VNOVAL;
247 vap->va_mode = VNOVAL;
248 vap->va_nlink = VNOVAL;
249 vap->va_uid = VNOVAL;
250 vap->va_gid = VNOVAL;
251 vap->va_fsid = VNOVAL;
252 vap->va_fileid = VNOVAL;
253 vap->va_blocksize = VNOVAL;
254 vap->va_rmajor = VNOVAL;
255 vap->va_rminor = VNOVAL;
256 vap->va_atime.tv_sec = VNOVAL;
257 vap->va_atime.tv_nsec = VNOVAL;
258 vap->va_mtime.tv_sec = VNOVAL;
259 vap->va_mtime.tv_nsec = VNOVAL;
260 vap->va_ctime.tv_sec = VNOVAL;
261 vap->va_ctime.tv_nsec = VNOVAL;
262 vap->va_flags = VNOVAL;
263 vap->va_gen = VNOVAL;
265 vap->va_fsmid = VNOVAL;
266 /* va_*_uuid fields are only valid if related flags are set */
270 * Flush out and invalidate all buffers associated with a vnode.
274 static int vinvalbuf_bp(struct buf *bp, void *data);
276 struct vinvalbuf_bp_info {
284 vupdatefsmid(struct vnode *vp)
286 atomic_set_int(&vp->v_flag, VFSMID);
290 vinvalbuf(struct vnode *vp, int flags, int slpflag, int slptimeo)
292 struct vinvalbuf_bp_info info;
297 lwkt_gettoken(&vlock, &vp->v_token);
300 * If we are being asked to save, call fsync to ensure that the inode
303 if (flags & V_SAVE) {
304 error = bio_track_wait(&vp->v_track_write, slpflag, slptimeo);
307 if (!RB_EMPTY(&vp->v_rbdirty_tree)) {
308 if ((error = VOP_FSYNC(vp, MNT_WAIT)) != 0)
312 * Dirty bufs may be left or generated via races
313 * in circumstances where vinvalbuf() is called on
314 * a vnode not undergoing reclamation. Only
315 * panic if we are trying to reclaim the vnode.
317 if ((vp->v_flag & VRECLAIMED) &&
318 (bio_track_active(&vp->v_track_write) ||
319 !RB_EMPTY(&vp->v_rbdirty_tree))) {
320 panic("vinvalbuf: dirty bufs");
324 info.slptimeo = slptimeo;
325 info.lkflags = LK_EXCLUSIVE | LK_SLEEPFAIL;
326 if (slpflag & PCATCH)
327 info.lkflags |= LK_PCATCH;
332 * Flush the buffer cache until nothing is left.
334 while (!RB_EMPTY(&vp->v_rbclean_tree) ||
335 !RB_EMPTY(&vp->v_rbdirty_tree)) {
336 error = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree, NULL,
337 vinvalbuf_bp, &info);
339 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
340 vinvalbuf_bp, &info);
345 * Wait for I/O completion. We may block in the pip code so we have
349 bio_track_wait(&vp->v_track_write, 0, 0);
350 if ((object = vp->v_object) != NULL) {
351 while (object->paging_in_progress)
352 vm_object_pip_sleep(object, "vnvlbx");
354 } while (bio_track_active(&vp->v_track_write));
357 * Destroy the copy in the VM cache, too.
359 if ((object = vp->v_object) != NULL) {
360 vm_object_page_remove(object, 0, 0,
361 (flags & V_SAVE) ? TRUE : FALSE);
364 if (!RB_EMPTY(&vp->v_rbdirty_tree) || !RB_EMPTY(&vp->v_rbclean_tree))
365 panic("vinvalbuf: flush failed");
366 if (!RB_EMPTY(&vp->v_rbhash_tree))
367 panic("vinvalbuf: flush failed, buffers still present");
370 lwkt_reltoken(&vlock);
375 vinvalbuf_bp(struct buf *bp, void *data)
377 struct vinvalbuf_bp_info *info = data;
380 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
381 error = BUF_TIMELOCK(bp, info->lkflags,
382 "vinvalbuf", info->slptimeo);
392 KKASSERT(bp->b_vp == info->vp);
395 * XXX Since there are no node locks for NFS, I
396 * believe there is a slight chance that a delayed
397 * write will occur while sleeping just above, so
398 * check for it. Note that vfs_bio_awrite expects
399 * buffers to reside on a queue, while bwrite() and
402 * NOTE: NO B_LOCKED CHECK. Also no buf_checkwrite()
403 * check. This code will write out the buffer, period.
405 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
406 (info->flags & V_SAVE)) {
407 if (bp->b_vp == info->vp) {
408 if (bp->b_flags & B_CLUSTEROK) {
418 } else if (info->flags & V_SAVE) {
420 * Cannot set B_NOCACHE on a clean buffer as this will
421 * destroy the VM backing store which might actually
422 * be dirty (and unsynchronized).
425 bp->b_flags |= (B_INVAL | B_RELBUF);
429 bp->b_flags |= (B_INVAL | B_NOCACHE | B_RELBUF);
436 * Truncate a file's buffer and pages to a specified length. This
437 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
440 * The vnode must be locked.
442 static int vtruncbuf_bp_trunc_cmp(struct buf *bp, void *data);
443 static int vtruncbuf_bp_trunc(struct buf *bp, void *data);
444 static int vtruncbuf_bp_metasync_cmp(struct buf *bp, void *data);
445 static int vtruncbuf_bp_metasync(struct buf *bp, void *data);
448 vtruncbuf(struct vnode *vp, off_t length, int blksize)
451 const char *filename;
456 * Round up to the *next* block, then destroy the buffers in question.
457 * Since we are only removing some of the buffers we must rely on the
458 * scan count to determine whether a loop is necessary.
460 if ((count = (int)(length % blksize)) != 0)
461 truncloffset = length + (blksize - count);
463 truncloffset = length;
465 lwkt_gettoken(&vlock, &vp->v_token);
467 count = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree,
468 vtruncbuf_bp_trunc_cmp,
469 vtruncbuf_bp_trunc, &truncloffset);
470 count += RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
471 vtruncbuf_bp_trunc_cmp,
472 vtruncbuf_bp_trunc, &truncloffset);
476 * For safety, fsync any remaining metadata if the file is not being
477 * truncated to 0. Since the metadata does not represent the entire
478 * dirty list we have to rely on the hit count to ensure that we get
483 count = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
484 vtruncbuf_bp_metasync_cmp,
485 vtruncbuf_bp_metasync, vp);
490 * Clean out any left over VM backing store.
492 * It is possible to have in-progress I/O from buffers that were
493 * not part of the truncation. This should not happen if we
494 * are truncating to 0-length.
496 vnode_pager_setsize(vp, length);
497 bio_track_wait(&vp->v_track_write, 0, 0);
499 filename = TAILQ_FIRST(&vp->v_namecache) ?
500 TAILQ_FIRST(&vp->v_namecache)->nc_name : "?";
503 * Make sure no buffers were instantiated while we were trying
504 * to clean out the remaining VM pages. This could occur due
505 * to busy dirty VM pages being flushed out to disk.
508 count = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree,
509 vtruncbuf_bp_trunc_cmp,
510 vtruncbuf_bp_trunc, &truncloffset);
511 count += RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
512 vtruncbuf_bp_trunc_cmp,
513 vtruncbuf_bp_trunc, &truncloffset);
515 kprintf("Warning: vtruncbuf(): Had to re-clean %d "
516 "left over buffers in %s\n", count, filename);
520 lwkt_reltoken(&vlock);
526 * The callback buffer is beyond the new file EOF and must be destroyed.
527 * Note that the compare function must conform to the RB_SCAN's requirements.
531 vtruncbuf_bp_trunc_cmp(struct buf *bp, void *data)
533 if (bp->b_loffset >= *(off_t *)data)
540 vtruncbuf_bp_trunc(struct buf *bp, void *data)
543 * Do not try to use a buffer we cannot immediately lock, but sleep
544 * anyway to prevent a livelock. The code will loop until all buffers
547 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
548 if (BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL) == 0)
552 bp->b_flags |= (B_INVAL | B_RELBUF | B_NOCACHE);
559 * Fsync all meta-data after truncating a file to be non-zero. Only metadata
560 * blocks (with a negative loffset) are scanned.
561 * Note that the compare function must conform to the RB_SCAN's requirements.
564 vtruncbuf_bp_metasync_cmp(struct buf *bp, void *data)
566 if (bp->b_loffset < 0)
572 vtruncbuf_bp_metasync(struct buf *bp, void *data)
574 struct vnode *vp = data;
576 if (bp->b_flags & B_DELWRI) {
578 * Do not try to use a buffer we cannot immediately lock,
579 * but sleep anyway to prevent a livelock. The code will
580 * loop until all buffers can be acted upon.
582 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
583 if (BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL) == 0)
599 * vfsync - implements a multipass fsync on a file which understands
600 * dependancies and meta-data. The passed vnode must be locked. The
601 * waitfor argument may be MNT_WAIT or MNT_NOWAIT, or MNT_LAZY.
603 * When fsyncing data asynchronously just do one consolidated pass starting
604 * with the most negative block number. This may not get all the data due
607 * When fsyncing data synchronously do a data pass, then a metadata pass,
608 * then do additional data+metadata passes to try to get all the data out.
610 static int vfsync_wait_output(struct vnode *vp,
611 int (*waitoutput)(struct vnode *, struct thread *));
612 static int vfsync_data_only_cmp(struct buf *bp, void *data);
613 static int vfsync_meta_only_cmp(struct buf *bp, void *data);
614 static int vfsync_lazy_range_cmp(struct buf *bp, void *data);
615 static int vfsync_bp(struct buf *bp, void *data);
624 int (*checkdef)(struct buf *);
628 vfsync(struct vnode *vp, int waitfor, int passes,
629 int (*checkdef)(struct buf *),
630 int (*waitoutput)(struct vnode *, struct thread *))
632 struct vfsync_info info;
636 bzero(&info, sizeof(info));
638 if ((info.checkdef = checkdef) == NULL)
641 lwkt_gettoken(&vlock, &vp->v_token);
646 * Lazy (filesystem syncer typ) Asynchronous plus limit the
647 * number of data (not meta) pages we try to flush to 1MB.
648 * A non-zero return means that lazy limit was reached.
650 info.lazylimit = 1024 * 1024;
652 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
653 vfsync_lazy_range_cmp, vfsync_bp, &info);
654 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
655 vfsync_meta_only_cmp, vfsync_bp, &info);
658 else if (!RB_EMPTY(&vp->v_rbdirty_tree))
659 vn_syncer_add_to_worklist(vp, 1);
664 * Asynchronous. Do a data-only pass and a meta-only pass.
667 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_data_only_cmp,
669 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_meta_only_cmp,
675 * Synchronous. Do a data-only pass, then a meta-data+data
676 * pass, then additional integrated passes to try to get
677 * all the dependancies flushed.
679 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_data_only_cmp,
681 error = vfsync_wait_output(vp, waitoutput);
683 info.skippedbufs = 0;
684 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
686 error = vfsync_wait_output(vp, waitoutput);
687 if (info.skippedbufs)
688 kprintf("Warning: vfsync skipped %d dirty bufs in pass2!\n", info.skippedbufs);
690 while (error == 0 && passes > 0 &&
691 !RB_EMPTY(&vp->v_rbdirty_tree)
694 info.synchronous = 1;
697 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
703 error = vfsync_wait_output(vp, waitoutput);
707 lwkt_reltoken(&vlock);
712 vfsync_wait_output(struct vnode *vp,
713 int (*waitoutput)(struct vnode *, struct thread *))
717 error = bio_track_wait(&vp->v_track_write, 0, 0);
719 error = waitoutput(vp, curthread);
724 vfsync_data_only_cmp(struct buf *bp, void *data)
726 if (bp->b_loffset < 0)
732 vfsync_meta_only_cmp(struct buf *bp, void *data)
734 if (bp->b_loffset < 0)
740 vfsync_lazy_range_cmp(struct buf *bp, void *data)
742 struct vfsync_info *info = data;
743 if (bp->b_loffset < info->vp->v_lazyw)
749 vfsync_bp(struct buf *bp, void *data)
751 struct vfsync_info *info = data;
752 struct vnode *vp = info->vp;
756 * if syncdeps is not set we do not try to write buffers which have
759 if (!info->synchronous && info->syncdeps == 0 && info->checkdef(bp))
763 * Ignore buffers that we cannot immediately lock. XXX
765 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
766 kprintf("Warning: vfsync_bp skipping dirty buffer %p\n", bp);
770 if ((bp->b_flags & B_DELWRI) == 0)
771 panic("vfsync_bp: buffer not dirty");
773 panic("vfsync_bp: buffer vp mismatch");
776 * B_NEEDCOMMIT (primarily used by NFS) is a state where the buffer
777 * has been written but an additional handshake with the device
778 * is required before we can dispose of the buffer. We have no idea
779 * how to do this so we have to skip these buffers.
781 if (bp->b_flags & B_NEEDCOMMIT) {
787 * Ask bioops if it is ok to sync
789 if (LIST_FIRST(&bp->b_dep) != NULL && buf_checkwrite(bp)) {
795 if (info->synchronous) {
797 * Synchronous flushing. An error may be returned.
803 * Asynchronous flushing. A negative return value simply
804 * stops the scan and is not considered an error. We use
805 * this to support limited MNT_LAZY flushes.
807 vp->v_lazyw = bp->b_loffset;
808 if ((vp->v_flag & VOBJBUF) && (bp->b_flags & B_CLUSTEROK)) {
809 info->lazycount += vfs_bio_awrite(bp);
811 info->lazycount += bp->b_bufsize;
815 if (info->lazylimit && info->lazycount >= info->lazylimit)
824 * Associate a buffer with a vnode.
829 bgetvp(struct vnode *vp, struct buf *bp)
833 KASSERT(bp->b_vp == NULL, ("bgetvp: not free"));
834 KKASSERT((bp->b_flags & (B_HASHED|B_DELWRI|B_VNCLEAN|B_VNDIRTY)) == 0);
837 * Insert onto list for new vnode.
839 lwkt_gettoken(&vlock, &vp->v_token);
840 if (buf_rb_hash_RB_INSERT(&vp->v_rbhash_tree, bp)) {
841 lwkt_reltoken(&vlock);
845 bp->b_flags |= B_HASHED;
846 bp->b_flags |= B_VNCLEAN;
847 if (buf_rb_tree_RB_INSERT(&vp->v_rbclean_tree, bp))
848 panic("reassignbuf: dup lblk/clean vp %p bp %p", vp, bp);
850 lwkt_reltoken(&vlock);
855 * Disassociate a buffer from a vnode.
858 brelvp(struct buf *bp)
863 KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
866 * Delete from old vnode list, if on one.
869 lwkt_gettoken(&vlock, &vp->v_token);
870 if (bp->b_flags & (B_VNDIRTY | B_VNCLEAN)) {
871 if (bp->b_flags & B_VNDIRTY)
872 buf_rb_tree_RB_REMOVE(&vp->v_rbdirty_tree, bp);
874 buf_rb_tree_RB_REMOVE(&vp->v_rbclean_tree, bp);
875 bp->b_flags &= ~(B_VNDIRTY | B_VNCLEAN);
877 if (bp->b_flags & B_HASHED) {
878 buf_rb_hash_RB_REMOVE(&vp->v_rbhash_tree, bp);
879 bp->b_flags &= ~B_HASHED;
881 if ((vp->v_flag & VONWORKLST) && RB_EMPTY(&vp->v_rbdirty_tree)) {
882 vp->v_flag &= ~VONWORKLST;
883 LIST_REMOVE(vp, v_synclist);
886 lwkt_reltoken(&vlock);
892 * Reassign the buffer to the proper clean/dirty list based on B_DELWRI.
893 * This routine is called when the state of the B_DELWRI bit is changed.
898 reassignbuf(struct buf *bp)
900 struct vnode *vp = bp->b_vp;
904 KKASSERT(vp != NULL);
908 * B_PAGING flagged buffers cannot be reassigned because their vp
909 * is not fully linked in.
911 if (bp->b_flags & B_PAGING)
912 panic("cannot reassign paging buffer");
914 lwkt_gettoken(&vlock, &vp->v_token);
915 if (bp->b_flags & B_DELWRI) {
917 * Move to the dirty list, add the vnode to the worklist
919 if (bp->b_flags & B_VNCLEAN) {
920 buf_rb_tree_RB_REMOVE(&vp->v_rbclean_tree, bp);
921 bp->b_flags &= ~B_VNCLEAN;
923 if ((bp->b_flags & B_VNDIRTY) == 0) {
924 if (buf_rb_tree_RB_INSERT(&vp->v_rbdirty_tree, bp)) {
925 panic("reassignbuf: dup lblk vp %p bp %p",
928 bp->b_flags |= B_VNDIRTY;
930 if ((vp->v_flag & VONWORKLST) == 0) {
931 switch (vp->v_type) {
938 vp->v_rdev->si_mountpoint != NULL) {
946 vn_syncer_add_to_worklist(vp, delay);
950 * Move to the clean list, remove the vnode from the worklist
951 * if no dirty blocks remain.
953 if (bp->b_flags & B_VNDIRTY) {
954 buf_rb_tree_RB_REMOVE(&vp->v_rbdirty_tree, bp);
955 bp->b_flags &= ~B_VNDIRTY;
957 if ((bp->b_flags & B_VNCLEAN) == 0) {
958 if (buf_rb_tree_RB_INSERT(&vp->v_rbclean_tree, bp)) {
959 panic("reassignbuf: dup lblk vp %p bp %p",
962 bp->b_flags |= B_VNCLEAN;
964 if ((vp->v_flag & VONWORKLST) &&
965 RB_EMPTY(&vp->v_rbdirty_tree)) {
966 vp->v_flag &= ~VONWORKLST;
967 LIST_REMOVE(vp, v_synclist);
970 lwkt_reltoken(&vlock);
974 * Create a vnode for a block device.
975 * Used for mounting the root file system.
977 extern struct vop_ops *devfs_vnode_dev_vops_p;
979 bdevvp(cdev_t dev, struct vnode **vpp)
989 error = getspecialvnode(VT_NON, NULL, &devfs_vnode_dev_vops_p,
1000 v_associate_rdev(vp, dev);
1001 vp->v_umajor = dev->si_umajor;
1002 vp->v_uminor = dev->si_uminor;
1009 v_associate_rdev(struct vnode *vp, cdev_t dev)
1015 if (dev_is_good(dev) == 0)
1017 KKASSERT(vp->v_rdev == NULL);
1018 vp->v_rdev = reference_dev(dev);
1019 lwkt_gettoken(&ilock, &spechash_token);
1020 SLIST_INSERT_HEAD(&dev->si_hlist, vp, v_cdevnext);
1021 lwkt_reltoken(&ilock);
1026 v_release_rdev(struct vnode *vp)
1031 if ((dev = vp->v_rdev) != NULL) {
1032 lwkt_gettoken(&ilock, &spechash_token);
1033 SLIST_REMOVE(&dev->si_hlist, vp, vnode, v_cdevnext);
1036 lwkt_reltoken(&ilock);
1041 * Add a vnode to the alias list hung off the cdev_t. We only associate
1042 * the device number with the vnode. The actual device is not associated
1043 * until the vnode is opened (usually in spec_open()), and will be
1044 * disassociated on last close.
1047 addaliasu(struct vnode *nvp, int x, int y)
1049 if (nvp->v_type != VBLK && nvp->v_type != VCHR)
1050 panic("addaliasu on non-special vnode");
1056 * Simple call that a filesystem can make to try to get rid of a
1057 * vnode. It will fail if anyone is referencing the vnode (including
1060 * The filesystem can check whether its in-memory inode structure still
1061 * references the vp on return.
1064 vclean_unlocked(struct vnode *vp)
1067 if (sysref_isactive(&vp->v_sysref) == 0)
1073 * Disassociate a vnode from its underlying filesystem.
1075 * The vnode must be VX locked and referenced. In all normal situations
1076 * there are no active references. If vclean_vxlocked() is called while
1077 * there are active references, the vnode is being ripped out and we have
1078 * to call VOP_CLOSE() as appropriate before we can reclaim it.
1081 vclean_vxlocked(struct vnode *vp, int flags)
1088 * If the vnode has already been reclaimed we have nothing to do.
1090 if (vp->v_flag & VRECLAIMED)
1092 vp->v_flag |= VRECLAIMED;
1095 * Scrap the vfs cache
1097 while (cache_inval_vp(vp, 0) != 0) {
1098 kprintf("Warning: vnode %p clean/cache_resolution race detected\n", vp);
1099 tsleep(vp, 0, "vclninv", 2);
1103 * Check to see if the vnode is in use. If so we have to reference it
1104 * before we clean it out so that its count cannot fall to zero and
1105 * generate a race against ourselves to recycle it.
1107 active = sysref_isactive(&vp->v_sysref);
1110 * Clean out any buffers associated with the vnode and destroy its
1111 * object, if it has one.
1113 vinvalbuf(vp, V_SAVE, 0, 0);
1116 * If purging an active vnode (typically during a forced unmount
1117 * or reboot), it must be closed and deactivated before being
1118 * reclaimed. This isn't really all that safe, but what can
1121 * Note that neither of these routines unlocks the vnode.
1123 if (active && (flags & DOCLOSE)) {
1124 while ((n = vp->v_opencount) != 0) {
1125 if (vp->v_writecount)
1126 VOP_CLOSE(vp, FWRITE|FNONBLOCK);
1128 VOP_CLOSE(vp, FNONBLOCK);
1129 if (vp->v_opencount == n) {
1130 kprintf("Warning: unable to force-close"
1138 * If the vnode has not been deactivated, deactivated it. Deactivation
1139 * can create new buffers and VM pages so we have to call vinvalbuf()
1140 * again to make sure they all get flushed.
1142 * This can occur if a file with a link count of 0 needs to be
1145 if ((vp->v_flag & VINACTIVE) == 0) {
1146 vp->v_flag |= VINACTIVE;
1148 vinvalbuf(vp, V_SAVE, 0, 0);
1152 * If the vnode has an object, destroy it.
1154 if ((object = vp->v_object) != NULL) {
1155 if (object->ref_count == 0) {
1156 if ((object->flags & OBJ_DEAD) == 0)
1157 vm_object_terminate(object);
1159 vm_pager_deallocate(object);
1161 vp->v_flag &= ~VOBJBUF;
1163 KKASSERT((vp->v_flag & VOBJBUF) == 0);
1166 * Reclaim the vnode.
1168 if (VOP_RECLAIM(vp))
1169 panic("vclean: cannot reclaim");
1172 * Done with purge, notify sleepers of the grim news.
1174 vp->v_ops = &dead_vnode_vops_p;
1179 * If we are destroying an active vnode, reactivate it now that
1180 * we have reassociated it with deadfs. This prevents the system
1181 * from crashing on the vnode due to it being unexpectedly marked
1182 * as inactive or reclaimed.
1184 if (active && (flags & DOCLOSE)) {
1185 vp->v_flag &= ~(VINACTIVE|VRECLAIMED);
1190 * Eliminate all activity associated with the requested vnode
1191 * and with all vnodes aliased to the requested vnode.
1193 * The vnode must be referenced but should not be locked.
1196 vrevoke(struct vnode *vp, struct ucred *cred)
1205 * If the vnode has a device association, scrap all vnodes associated
1206 * with the device. Don't let the device disappear on us while we
1207 * are scrapping the vnodes.
1209 * The passed vp will probably show up in the list, do not VX lock
1212 * Releasing the vnode's rdev here can mess up specfs's call to
1213 * device close, so don't do it. The vnode has been disassociated
1214 * and the device will be closed after the last ref on the related
1215 * fp goes away (if not still open by e.g. the kernel).
1217 if (vp->v_type != VCHR) {
1218 error = fdrevoke(vp, DTYPE_VNODE, cred);
1221 if ((dev = vp->v_rdev) == NULL) {
1225 lwkt_gettoken(&ilock, &spechash_token);
1227 vqn = SLIST_FIRST(&dev->si_hlist);
1230 while ((vq = vqn) != NULL) {
1231 vqn = SLIST_NEXT(vqn, v_cdevnext);
1234 fdrevoke(vq, DTYPE_VNODE, cred);
1235 /*v_release_rdev(vq);*/
1238 lwkt_reltoken(&ilock);
1245 * This is called when the object underlying a vnode is being destroyed,
1246 * such as in a remove(). Try to recycle the vnode immediately if the
1247 * only active reference is our reference.
1249 * Directory vnodes in the namecache with children cannot be immediately
1250 * recycled because numerous VOP_N*() ops require them to be stable.
1253 vrecycle(struct vnode *vp)
1255 if (vp->v_sysref.refcnt <= 1) {
1256 if (cache_inval_vp_nonblock(vp))
1265 * Return the maximum I/O size allowed for strategy calls on VP.
1267 * If vp is VCHR or VBLK we dive the device, otherwise we use
1268 * the vp's mount info.
1271 vmaxiosize(struct vnode *vp)
1273 if (vp->v_type == VBLK || vp->v_type == VCHR) {
1274 return(vp->v_rdev->si_iosize_max);
1276 return(vp->v_mount->mnt_iosize_max);
1281 * Eliminate all activity associated with a vnode in preparation for reuse.
1283 * The vnode must be VX locked and refd and will remain VX locked and refd
1284 * on return. This routine may be called with the vnode in any state, as
1285 * long as it is VX locked. The vnode will be cleaned out and marked
1286 * VRECLAIMED but will not actually be reused until all existing refs and
1289 * NOTE: This routine may be called on a vnode which has not yet been
1290 * already been deactivated (VOP_INACTIVE), or on a vnode which has
1291 * already been reclaimed.
1293 * This routine is not responsible for placing us back on the freelist.
1294 * Instead, it happens automatically when the caller releases the VX lock
1295 * (assuming there aren't any other references).
1299 vgone_vxlocked(struct vnode *vp)
1302 * assert that the VX lock is held. This is an absolute requirement
1303 * now for vgone_vxlocked() to be called.
1305 KKASSERT(vp->v_lock.lk_exclusivecount == 1);
1308 * Clean out the filesystem specific data and set the VRECLAIMED
1309 * bit. Also deactivate the vnode if necessary.
1311 vclean_vxlocked(vp, DOCLOSE);
1314 * Delete from old mount point vnode list, if on one.
1316 if (vp->v_mount != NULL)
1317 insmntque(vp, NULL);
1320 * If special device, remove it from special device alias list
1321 * if it is on one. This should normally only occur if a vnode is
1322 * being revoked as the device should otherwise have been released
1325 if ((vp->v_type == VBLK || vp->v_type == VCHR) && vp->v_rdev != NULL) {
1336 * Lookup a vnode by device number.
1338 * Returns non-zero and *vpp set to a vref'd vnode on success.
1339 * Returns zero on failure.
1342 vfinddev(cdev_t dev, enum vtype type, struct vnode **vpp)
1347 lwkt_gettoken(&ilock, &spechash_token);
1348 SLIST_FOREACH(vp, &dev->si_hlist, v_cdevnext) {
1349 if (type == vp->v_type) {
1352 lwkt_reltoken(&ilock);
1356 lwkt_reltoken(&ilock);
1361 * Calculate the total number of references to a special device. This
1362 * routine may only be called for VBLK and VCHR vnodes since v_rdev is
1363 * an overloaded field. Since udev2dev can now return NULL, we have
1364 * to check for a NULL v_rdev.
1367 count_dev(cdev_t dev)
1373 if (SLIST_FIRST(&dev->si_hlist)) {
1374 lwkt_gettoken(&ilock, &spechash_token);
1375 SLIST_FOREACH(vp, &dev->si_hlist, v_cdevnext) {
1376 count += vp->v_opencount;
1378 lwkt_reltoken(&ilock);
1384 vcount(struct vnode *vp)
1386 if (vp->v_rdev == NULL)
1388 return(count_dev(vp->v_rdev));
1392 * Initialize VMIO for a vnode. This routine MUST be called before a
1393 * VFS can issue buffer cache ops on a vnode. It is typically called
1394 * when a vnode is initialized from its inode.
1397 vinitvmio(struct vnode *vp, off_t filesize)
1403 if ((object = vp->v_object) == NULL) {
1404 object = vnode_pager_alloc(vp, filesize, 0, 0);
1406 * Dereference the reference we just created. This assumes
1407 * that the object is associated with the vp.
1409 object->ref_count--;
1412 if (object->flags & OBJ_DEAD) {
1414 vm_object_dead_sleep(object, "vodead");
1415 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1419 KASSERT(vp->v_object != NULL, ("vinitvmio: NULL object"));
1420 vp->v_flag |= VOBJBUF;
1426 * Print out a description of a vnode.
1428 static char *typename[] =
1429 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD"};
1432 vprint(char *label, struct vnode *vp)
1437 kprintf("%s: %p: ", label, (void *)vp);
1439 kprintf("%p: ", (void *)vp);
1440 kprintf("type %s, sysrefs %d, writecount %d, holdcnt %d,",
1441 typename[vp->v_type],
1442 vp->v_sysref.refcnt, vp->v_writecount, vp->v_auxrefs);
1444 if (vp->v_flag & VROOT)
1445 strcat(buf, "|VROOT");
1446 if (vp->v_flag & VPFSROOT)
1447 strcat(buf, "|VPFSROOT");
1448 if (vp->v_flag & VTEXT)
1449 strcat(buf, "|VTEXT");
1450 if (vp->v_flag & VSYSTEM)
1451 strcat(buf, "|VSYSTEM");
1452 if (vp->v_flag & VFREE)
1453 strcat(buf, "|VFREE");
1454 if (vp->v_flag & VOBJBUF)
1455 strcat(buf, "|VOBJBUF");
1457 kprintf(" flags (%s)", &buf[1]);
1458 if (vp->v_data == NULL) {
1467 #include <ddb/ddb.h>
1469 static int db_show_locked_vnodes(struct mount *mp, void *data);
1472 * List all of the locked vnodes in the system.
1473 * Called when debugging the kernel.
1475 DB_SHOW_COMMAND(lockedvnodes, lockedvnodes)
1477 kprintf("Locked vnodes\n");
1478 mountlist_scan(db_show_locked_vnodes, NULL,
1479 MNTSCAN_FORWARD|MNTSCAN_NOBUSY);
1483 db_show_locked_vnodes(struct mount *mp, void *data __unused)
1487 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
1488 if (vn_islocked(vp))
1496 * Top level filesystem related information gathering.
1498 static int sysctl_ovfs_conf (SYSCTL_HANDLER_ARGS);
1501 vfs_sysctl(SYSCTL_HANDLER_ARGS)
1503 int *name = (int *)arg1 - 1; /* XXX */
1504 u_int namelen = arg2 + 1; /* XXX */
1505 struct vfsconf *vfsp;
1508 #if 1 || defined(COMPAT_PRELITE2)
1509 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
1511 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
1515 /* all sysctl names at this level are at least name and field */
1517 return (ENOTDIR); /* overloaded */
1518 if (name[0] != VFS_GENERIC) {
1519 vfsp = vfsconf_find_by_typenum(name[0]);
1521 return (EOPNOTSUPP);
1522 return ((*vfsp->vfc_vfsops->vfs_sysctl)(&name[1], namelen - 1,
1523 oldp, oldlenp, newp, newlen, p));
1527 case VFS_MAXTYPENUM:
1530 maxtypenum = vfsconf_get_maxtypenum();
1531 return (SYSCTL_OUT(req, &maxtypenum, sizeof(maxtypenum)));
1534 return (ENOTDIR); /* overloaded */
1535 vfsp = vfsconf_find_by_typenum(name[2]);
1537 return (EOPNOTSUPP);
1538 return (SYSCTL_OUT(req, vfsp, sizeof *vfsp));
1540 return (EOPNOTSUPP);
1543 SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD, vfs_sysctl,
1544 "Generic filesystem");
1546 #if 1 || defined(COMPAT_PRELITE2)
1549 sysctl_ovfs_conf_iter(struct vfsconf *vfsp, void *data)
1552 struct ovfsconf ovfs;
1553 struct sysctl_req *req = (struct sysctl_req*) data;
1555 bzero(&ovfs, sizeof(ovfs));
1556 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
1557 strcpy(ovfs.vfc_name, vfsp->vfc_name);
1558 ovfs.vfc_index = vfsp->vfc_typenum;
1559 ovfs.vfc_refcount = vfsp->vfc_refcount;
1560 ovfs.vfc_flags = vfsp->vfc_flags;
1561 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
1563 return error; /* abort iteration with error code */
1565 return 0; /* continue iterating with next element */
1569 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
1571 return vfsconf_each(sysctl_ovfs_conf_iter, (void*)req);
1574 #endif /* 1 || COMPAT_PRELITE2 */
1577 * Check to see if a filesystem is mounted on a block device.
1580 vfs_mountedon(struct vnode *vp)
1584 if ((dev = vp->v_rdev) == NULL) {
1585 /* if (vp->v_type != VBLK)
1586 dev = get_dev(vp->v_uminor, vp->v_umajor); */
1588 if (dev != NULL && dev->si_mountpoint)
1594 * Unmount all filesystems. The list is traversed in reverse order
1595 * of mounting to avoid dependencies.
1598 static int vfs_umountall_callback(struct mount *mp, void *data);
1601 vfs_unmountall(void)
1606 count = mountlist_scan(vfs_umountall_callback,
1607 NULL, MNTSCAN_REVERSE|MNTSCAN_NOBUSY);
1613 vfs_umountall_callback(struct mount *mp, void *data)
1617 error = dounmount(mp, MNT_FORCE);
1619 mountlist_remove(mp);
1620 kprintf("unmount of filesystem mounted from %s failed (",
1621 mp->mnt_stat.f_mntfromname);
1625 kprintf("%d)\n", error);
1631 * Checks the mount flags for parameter mp and put the names comma-separated
1632 * into a string buffer buf with a size limit specified by len.
1634 * It returns the number of bytes written into buf, and (*errorp) will be
1635 * set to 0, EINVAL (if passed length is 0), or ENOSPC (supplied buffer was
1636 * not large enough). The buffer will be 0-terminated if len was not 0.
1638 #define OPTARYSIZE (sizeof(optnames) / sizeof(optnames[0]))
1641 vfs_flagstostr(struct mount *mp, char *buf, size_t len, int *errorp)
1643 static const struct mountctl_opt optnames[] = {
1644 { MNT_ASYNC, "asynchronous" },
1645 { MNT_EXPORTED, "NFS exported" },
1646 { MNT_LOCAL, "local" },
1647 { MNT_NOATIME, "noatime" },
1648 { MNT_NODEV, "nodev" },
1649 { MNT_NOEXEC, "noexec" },
1650 { MNT_NOSUID, "nosuid" },
1651 { MNT_NOSYMFOLLOW, "nosymfollow" },
1652 { MNT_QUOTA, "with-quotas" },
1653 { MNT_RDONLY, "read-only" },
1654 { MNT_SYNCHRONOUS, "synchronous" },
1655 { MNT_UNION, "union" },
1656 { MNT_NOCLUSTERR, "noclusterr" },
1657 { MNT_NOCLUSTERW, "noclusterw" },
1658 { MNT_SUIDDIR, "suiddir" },
1659 { MNT_SOFTDEP, "soft-updates" },
1660 { MNT_IGNORE, "ignore" }
1662 const struct mountctl_opt *opt;
1669 flags = mp->mnt_flag & MNT_VISFLAGMASK;
1671 bleft = len - 1; /* leave room for trailing \0 */
1672 if (bleft < 0) { /* degenerate case, 0-length buffer */
1677 for (opt = optnames; flags && opt < &optnames[OPTARYSIZE]; ++opt) {
1678 if ((flags & opt->o_opt) == 0)
1680 optlen = strlen(opt->o_name);
1686 buf[bwritten++] = ',';
1689 if (bleft < optlen) {
1693 bcopy(opt->o_name, buf + bwritten, optlen);
1699 * Space already reserved for trailing \0
1708 * Build hash lists of net addresses and hang them off the mount point.
1709 * Called by ufs_mount() to set up the lists of export addresses.
1712 vfs_hang_addrlist(struct mount *mp, struct netexport *nep,
1713 const struct export_args *argp)
1716 struct radix_node_head *rnh;
1718 struct radix_node *rn;
1719 struct sockaddr *saddr, *smask = 0;
1723 if (argp->ex_addrlen == 0) {
1724 if (mp->mnt_flag & MNT_DEFEXPORTED)
1726 np = &nep->ne_defexported;
1727 np->netc_exflags = argp->ex_flags;
1728 np->netc_anon = argp->ex_anon;
1729 np->netc_anon.cr_ref = 1;
1730 mp->mnt_flag |= MNT_DEFEXPORTED;
1734 if (argp->ex_addrlen < 0 || argp->ex_addrlen > MLEN)
1736 if (argp->ex_masklen < 0 || argp->ex_masklen > MLEN)
1739 i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
1740 np = (struct netcred *) kmalloc(i, M_NETADDR, M_WAITOK | M_ZERO);
1741 saddr = (struct sockaddr *) (np + 1);
1742 if ((error = copyin(argp->ex_addr, (caddr_t) saddr, argp->ex_addrlen)))
1744 if (saddr->sa_len > argp->ex_addrlen)
1745 saddr->sa_len = argp->ex_addrlen;
1746 if (argp->ex_masklen) {
1747 smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen);
1748 error = copyin(argp->ex_mask, (caddr_t)smask, argp->ex_masklen);
1751 if (smask->sa_len > argp->ex_masklen)
1752 smask->sa_len = argp->ex_masklen;
1754 i = saddr->sa_family;
1755 if ((rnh = nep->ne_rtable[i]) == 0) {
1757 * Seems silly to initialize every AF when most are not used,
1758 * do so on demand here
1760 SLIST_FOREACH(dom, &domains, dom_next)
1761 if (dom->dom_family == i && dom->dom_rtattach) {
1762 dom->dom_rtattach((void **) &nep->ne_rtable[i],
1766 if ((rnh = nep->ne_rtable[i]) == 0) {
1771 rn = (*rnh->rnh_addaddr) ((char *) saddr, (char *) smask, rnh,
1773 if (rn == 0 || np != (struct netcred *) rn) { /* already exists */
1777 np->netc_exflags = argp->ex_flags;
1778 np->netc_anon = argp->ex_anon;
1779 np->netc_anon.cr_ref = 1;
1782 kfree(np, M_NETADDR);
1788 vfs_free_netcred(struct radix_node *rn, void *w)
1790 struct radix_node_head *rnh = (struct radix_node_head *) w;
1792 (*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, rnh);
1793 kfree((caddr_t) rn, M_NETADDR);
1798 * Free the net address hash lists that are hanging off the mount points.
1801 vfs_free_addrlist(struct netexport *nep)
1804 struct radix_node_head *rnh;
1806 for (i = 0; i <= AF_MAX; i++)
1807 if ((rnh = nep->ne_rtable[i])) {
1808 (*rnh->rnh_walktree) (rnh, vfs_free_netcred,
1810 kfree((caddr_t) rnh, M_RTABLE);
1811 nep->ne_rtable[i] = 0;
1816 vfs_export(struct mount *mp, struct netexport *nep,
1817 const struct export_args *argp)
1821 if (argp->ex_flags & MNT_DELEXPORT) {
1822 if (mp->mnt_flag & MNT_EXPUBLIC) {
1823 vfs_setpublicfs(NULL, NULL, NULL);
1824 mp->mnt_flag &= ~MNT_EXPUBLIC;
1826 vfs_free_addrlist(nep);
1827 mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
1829 if (argp->ex_flags & MNT_EXPORTED) {
1830 if (argp->ex_flags & MNT_EXPUBLIC) {
1831 if ((error = vfs_setpublicfs(mp, nep, argp)) != 0)
1833 mp->mnt_flag |= MNT_EXPUBLIC;
1835 if ((error = vfs_hang_addrlist(mp, nep, argp)))
1837 mp->mnt_flag |= MNT_EXPORTED;
1844 * Set the publicly exported filesystem (WebNFS). Currently, only
1845 * one public filesystem is possible in the spec (RFC 2054 and 2055)
1848 vfs_setpublicfs(struct mount *mp, struct netexport *nep,
1849 const struct export_args *argp)
1856 * mp == NULL -> invalidate the current info, the FS is
1857 * no longer exported. May be called from either vfs_export
1858 * or unmount, so check if it hasn't already been done.
1861 if (nfs_pub.np_valid) {
1862 nfs_pub.np_valid = 0;
1863 if (nfs_pub.np_index != NULL) {
1864 FREE(nfs_pub.np_index, M_TEMP);
1865 nfs_pub.np_index = NULL;
1872 * Only one allowed at a time.
1874 if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
1878 * Get real filehandle for root of exported FS.
1880 bzero((caddr_t)&nfs_pub.np_handle, sizeof(nfs_pub.np_handle));
1881 nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid;
1883 if ((error = VFS_ROOT(mp, &rvp)))
1886 if ((error = VFS_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid)))
1892 * If an indexfile was specified, pull it in.
1894 if (argp->ex_indexfile != NULL) {
1897 error = vn_get_namelen(rvp, &namelen);
1900 MALLOC(nfs_pub.np_index, char *, namelen, M_TEMP,
1902 error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
1906 * Check for illegal filenames.
1908 for (cp = nfs_pub.np_index; *cp; cp++) {
1916 FREE(nfs_pub.np_index, M_TEMP);
1921 nfs_pub.np_mount = mp;
1922 nfs_pub.np_valid = 1;
1927 vfs_export_lookup(struct mount *mp, struct netexport *nep,
1928 struct sockaddr *nam)
1931 struct radix_node_head *rnh;
1932 struct sockaddr *saddr;
1935 if (mp->mnt_flag & MNT_EXPORTED) {
1937 * Lookup in the export list first.
1941 rnh = nep->ne_rtable[saddr->sa_family];
1943 np = (struct netcred *)
1944 (*rnh->rnh_matchaddr)((char *)saddr,
1946 if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
1951 * If no address match, use the default if it exists.
1953 if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
1954 np = &nep->ne_defexported;
1960 * perform msync on all vnodes under a mount point. The mount point must
1961 * be locked. This code is also responsible for lazy-freeing unreferenced
1962 * vnodes whos VM objects no longer contain pages.
1964 * NOTE: MNT_WAIT still skips vnodes in the VXLOCK state.
1966 * NOTE: XXX VOP_PUTPAGES and friends requires that the vnode be locked,
1967 * but vnode_pager_putpages() doesn't lock the vnode. We have to do it
1968 * way up in this high level function.
1970 static int vfs_msync_scan1(struct mount *mp, struct vnode *vp, void *data);
1971 static int vfs_msync_scan2(struct mount *mp, struct vnode *vp, void *data);
1974 vfs_msync(struct mount *mp, int flags)
1978 vmsc_flags = VMSC_GETVP;
1979 if (flags != MNT_WAIT)
1980 vmsc_flags |= VMSC_NOWAIT;
1981 vmntvnodescan(mp, vmsc_flags, vfs_msync_scan1, vfs_msync_scan2,
1982 (void *)(intptr_t)flags);
1986 * scan1 is a fast pre-check. There could be hundreds of thousands of
1987 * vnodes, we cannot afford to do anything heavy weight until we have a
1988 * fairly good indication that there is work to do.
1992 vfs_msync_scan1(struct mount *mp, struct vnode *vp, void *data)
1994 int flags = (int)(intptr_t)data;
1996 if ((vp->v_flag & VRECLAIMED) == 0) {
1997 if (vshouldmsync(vp))
1998 return(0); /* call scan2 */
1999 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
2000 (vp->v_flag & VOBJDIRTY) &&
2001 (flags == MNT_WAIT || vn_islocked(vp) == 0)) {
2002 return(0); /* call scan2 */
2007 * do not call scan2, continue the loop
2013 * This callback is handed a locked vnode.
2017 vfs_msync_scan2(struct mount *mp, struct vnode *vp, void *data)
2020 int flags = (int)(intptr_t)data;
2022 if (vp->v_flag & VRECLAIMED)
2025 if ((mp->mnt_flag & MNT_RDONLY) == 0 && (vp->v_flag & VOBJDIRTY)) {
2026 if ((obj = vp->v_object) != NULL) {
2027 vm_object_page_clean(obj, 0, 0,
2028 flags == MNT_WAIT ? OBJPC_SYNC : OBJPC_NOSYNC);
2035 * Record a process's interest in events which might happen to
2036 * a vnode. Because poll uses the historic select-style interface
2037 * internally, this routine serves as both the ``check for any
2038 * pending events'' and the ``record my interest in future events''
2039 * functions. (These are done together, while the lock is held,
2040 * to avoid race conditions.)
2043 vn_pollrecord(struct vnode *vp, int events)
2047 KKASSERT(curthread->td_proc != NULL);
2049 lwkt_gettoken(&vlock, &vp->v_token);
2050 if (vp->v_pollinfo.vpi_revents & events) {
2052 * This leaves events we are not interested
2053 * in available for the other process which
2054 * which presumably had requested them
2055 * (otherwise they would never have been
2058 events &= vp->v_pollinfo.vpi_revents;
2059 vp->v_pollinfo.vpi_revents &= ~events;
2061 lwkt_reltoken(&vlock);
2064 vp->v_pollinfo.vpi_events |= events;
2065 selrecord(curthread, &vp->v_pollinfo.vpi_selinfo);
2066 lwkt_reltoken(&vlock);
2071 * Note the occurrence of an event. If the VN_POLLEVENT macro is used,
2072 * it is possible for us to miss an event due to race conditions, but
2073 * that condition is expected to be rare, so for the moment it is the
2074 * preferred interface.
2077 vn_pollevent(struct vnode *vp, int events)
2081 lwkt_gettoken(&vlock, &vp->v_token);
2082 if (vp->v_pollinfo.vpi_events & events) {
2084 * We clear vpi_events so that we don't
2085 * call selwakeup() twice if two events are
2086 * posted before the polling process(es) is
2087 * awakened. This also ensures that we take at
2088 * most one selwakeup() if the polling process
2089 * is no longer interested. However, it does
2090 * mean that only one event can be noticed at
2091 * a time. (Perhaps we should only clear those
2092 * event bits which we note?) XXX
2094 vp->v_pollinfo.vpi_events = 0; /* &= ~events ??? */
2095 vp->v_pollinfo.vpi_revents |= events;
2096 selwakeup(&vp->v_pollinfo.vpi_selinfo);
2098 lwkt_reltoken(&vlock);
2102 * Wake up anyone polling on vp because it is being revoked.
2103 * This depends on dead_poll() returning POLLHUP for correct
2107 vn_pollgone(struct vnode *vp)
2111 lwkt_gettoken(&vlock, &vp->v_token);
2112 if (vp->v_pollinfo.vpi_events) {
2113 vp->v_pollinfo.vpi_events = 0;
2114 selwakeup(&vp->v_pollinfo.vpi_selinfo);
2116 lwkt_reltoken(&vlock);
2120 * extract the cdev_t from a VBLK or VCHR. The vnode must have been opened
2121 * (or v_rdev might be NULL).
2124 vn_todev(struct vnode *vp)
2126 if (vp->v_type != VBLK && vp->v_type != VCHR)
2128 KKASSERT(vp->v_rdev != NULL);
2129 return (vp->v_rdev);
2133 * Check if vnode represents a disk device. The vnode does not need to be
2139 vn_isdisk(struct vnode *vp, int *errp)
2143 if (vp->v_type != VCHR) {
2156 if (dev_is_good(dev) == 0) {
2161 if ((dev_dflags(dev) & D_DISK) == 0) {
2172 vn_get_namelen(struct vnode *vp, int *namelen)
2175 register_t retval[2];
2177 error = VOP_PATHCONF(vp, _PC_NAME_MAX, retval);
2180 *namelen = (int)retval[0];
2185 vop_write_dirent(int *error, struct uio *uio, ino_t d_ino, uint8_t d_type,
2186 uint16_t d_namlen, const char *d_name)
2191 len = _DIRENT_RECLEN(d_namlen);
2192 if (len > uio->uio_resid)
2195 dp = kmalloc(len, M_TEMP, M_WAITOK | M_ZERO);
2198 dp->d_namlen = d_namlen;
2199 dp->d_type = d_type;
2200 bcopy(d_name, dp->d_name, d_namlen);
2202 *error = uiomove((caddr_t)dp, len, uio);
2210 vn_mark_atime(struct vnode *vp, struct thread *td)
2212 struct proc *p = td->td_proc;
2213 struct ucred *cred = p ? p->p_ucred : proc0.p_ucred;
2215 if ((vp->v_mount->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0) {
2216 VOP_MARKATIME(vp, cred);