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>
64 #include <sys/reboot.h>
65 #include <sys/socket.h>
67 #include <sys/sysctl.h>
68 #include <sys/syslog.h>
69 #include <sys/unistd.h>
70 #include <sys/vmmeter.h>
71 #include <sys/vnode.h>
73 #include <machine/limits.h>
76 #include <vm/vm_object.h>
77 #include <vm/vm_extern.h>
78 #include <vm/vm_kern.h>
80 #include <vm/vm_map.h>
81 #include <vm/vm_page.h>
82 #include <vm/vm_pager.h>
83 #include <vm/vnode_pager.h>
84 #include <vm/vm_zone.h>
87 #include <sys/thread2.h>
88 #include <sys/sysref2.h>
90 static MALLOC_DEFINE(M_NETADDR, "Export Host", "Export host address structure");
93 SYSCTL_INT(_debug, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0, "");
95 SYSCTL_INT(_vfs, OID_AUTO, fastdev, CTLFLAG_RW, &vfs_fastdev, 0, "");
97 enum vtype iftovt_tab[16] = {
98 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
99 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
101 int vttoif_tab[9] = {
102 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
103 S_IFSOCK, S_IFIFO, S_IFMT,
106 static int reassignbufcalls;
107 SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW,
108 &reassignbufcalls, 0, "");
109 static int reassignbufloops;
110 SYSCTL_INT(_vfs, OID_AUTO, reassignbufloops, CTLFLAG_RW,
111 &reassignbufloops, 0, "");
112 static int reassignbufsortgood;
113 SYSCTL_INT(_vfs, OID_AUTO, reassignbufsortgood, CTLFLAG_RW,
114 &reassignbufsortgood, 0, "");
115 static int reassignbufsortbad;
116 SYSCTL_INT(_vfs, OID_AUTO, reassignbufsortbad, CTLFLAG_RW,
117 &reassignbufsortbad, 0, "");
118 static int reassignbufmethod = 1;
119 SYSCTL_INT(_vfs, OID_AUTO, reassignbufmethod, CTLFLAG_RW,
120 &reassignbufmethod, 0, "");
122 int nfs_mount_type = -1;
123 static struct lwkt_token spechash_token;
124 struct nfs_public nfs_pub; /* publicly exported FS */
127 SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RW,
128 &desiredvnodes, 0, "Maximum number of vnodes");
130 static void vfs_free_addrlist (struct netexport *nep);
131 static int vfs_free_netcred (struct radix_node *rn, void *w);
132 static int vfs_hang_addrlist (struct mount *mp, struct netexport *nep,
133 const struct export_args *argp);
136 * Red black tree functions
138 static int rb_buf_compare(struct buf *b1, struct buf *b2);
139 RB_GENERATE2(buf_rb_tree, buf, b_rbnode, rb_buf_compare, off_t, b_loffset);
140 RB_GENERATE2(buf_rb_hash, buf, b_rbhash, rb_buf_compare, off_t, b_loffset);
143 rb_buf_compare(struct buf *b1, struct buf *b2)
145 if (b1->b_loffset < b2->b_loffset)
147 if (b1->b_loffset > b2->b_loffset)
153 * Returns non-zero if the vnode is a candidate for lazy msyncing.
156 vshouldmsync(struct vnode *vp)
158 if (vp->v_auxrefs != 0 || vp->v_sysref.refcnt > 0)
159 return (0); /* other holders */
161 (vp->v_object->ref_count || vp->v_object->resident_page_count)) {
168 * Initialize the vnode management data structures.
170 * Called from vfsinit()
176 * Desiredvnodes is kern.maxvnodes. We want to scale it
177 * according to available system memory but we may also have
178 * to limit it based on available KVM, which is capped on 32 bit
181 desiredvnodes = min(maxproc + vmstats.v_page_count / 4,
183 (sizeof(struct vm_object) + sizeof(struct vnode))));
185 lwkt_token_init(&spechash_token);
189 * Knob to control the precision of file timestamps:
191 * 0 = seconds only; nanoseconds zeroed.
192 * 1 = seconds and nanoseconds, accurate within 1/HZ.
193 * 2 = seconds and nanoseconds, truncated to microseconds.
194 * >=3 = seconds and nanoseconds, maximum precision.
196 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC };
198 static int timestamp_precision = TSP_SEC;
199 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW,
200 ×tamp_precision, 0, "");
203 * Get a current timestamp.
208 vfs_timestamp(struct timespec *tsp)
212 switch (timestamp_precision) {
214 tsp->tv_sec = time_second;
222 TIMEVAL_TO_TIMESPEC(&tv, tsp);
232 * Set vnode attributes to VNOVAL
235 vattr_null(struct vattr *vap)
238 vap->va_size = VNOVAL;
239 vap->va_bytes = VNOVAL;
240 vap->va_mode = VNOVAL;
241 vap->va_nlink = VNOVAL;
242 vap->va_uid = VNOVAL;
243 vap->va_gid = VNOVAL;
244 vap->va_fsid = VNOVAL;
245 vap->va_fileid = VNOVAL;
246 vap->va_blocksize = VNOVAL;
247 vap->va_rmajor = VNOVAL;
248 vap->va_rminor = VNOVAL;
249 vap->va_atime.tv_sec = VNOVAL;
250 vap->va_atime.tv_nsec = VNOVAL;
251 vap->va_mtime.tv_sec = VNOVAL;
252 vap->va_mtime.tv_nsec = VNOVAL;
253 vap->va_ctime.tv_sec = VNOVAL;
254 vap->va_ctime.tv_nsec = VNOVAL;
255 vap->va_flags = VNOVAL;
256 vap->va_gen = VNOVAL;
258 /* va_*_uuid fields are only valid if related flags are set */
262 * Flush out and invalidate all buffers associated with a vnode.
266 static int vinvalbuf_bp(struct buf *bp, void *data);
268 struct vinvalbuf_bp_info {
276 vinvalbuf(struct vnode *vp, int flags, int slpflag, int slptimeo)
278 struct vinvalbuf_bp_info info;
283 lwkt_gettoken(&vlock, &vp->v_token);
286 * If we are being asked to save, call fsync to ensure that the inode
289 if (flags & V_SAVE) {
290 error = bio_track_wait(&vp->v_track_write, slpflag, slptimeo);
293 if (!RB_EMPTY(&vp->v_rbdirty_tree)) {
294 if ((error = VOP_FSYNC(vp, MNT_WAIT, 0)) != 0)
298 * Dirty bufs may be left or generated via races
299 * in circumstances where vinvalbuf() is called on
300 * a vnode not undergoing reclamation. Only
301 * panic if we are trying to reclaim the vnode.
303 if ((vp->v_flag & VRECLAIMED) &&
304 (bio_track_active(&vp->v_track_write) ||
305 !RB_EMPTY(&vp->v_rbdirty_tree))) {
306 panic("vinvalbuf: dirty bufs");
310 info.slptimeo = slptimeo;
311 info.lkflags = LK_EXCLUSIVE | LK_SLEEPFAIL;
312 if (slpflag & PCATCH)
313 info.lkflags |= LK_PCATCH;
318 * Flush the buffer cache until nothing is left.
320 while (!RB_EMPTY(&vp->v_rbclean_tree) ||
321 !RB_EMPTY(&vp->v_rbdirty_tree)) {
322 error = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree, NULL,
323 vinvalbuf_bp, &info);
325 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
326 vinvalbuf_bp, &info);
331 * Wait for I/O completion. We may block in the pip code so we have
335 bio_track_wait(&vp->v_track_write, 0, 0);
336 if ((object = vp->v_object) != NULL) {
337 while (object->paging_in_progress)
338 vm_object_pip_sleep(object, "vnvlbx");
340 } while (bio_track_active(&vp->v_track_write));
343 * Destroy the copy in the VM cache, too.
345 if ((object = vp->v_object) != NULL) {
346 vm_object_page_remove(object, 0, 0,
347 (flags & V_SAVE) ? TRUE : FALSE);
350 if (!RB_EMPTY(&vp->v_rbdirty_tree) || !RB_EMPTY(&vp->v_rbclean_tree))
351 panic("vinvalbuf: flush failed");
352 if (!RB_EMPTY(&vp->v_rbhash_tree))
353 panic("vinvalbuf: flush failed, buffers still present");
356 lwkt_reltoken(&vlock);
361 vinvalbuf_bp(struct buf *bp, void *data)
363 struct vinvalbuf_bp_info *info = data;
366 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
367 error = BUF_TIMELOCK(bp, info->lkflags,
368 "vinvalbuf", info->slptimeo);
378 KKASSERT(bp->b_vp == info->vp);
381 * XXX Since there are no node locks for NFS, I
382 * believe there is a slight chance that a delayed
383 * write will occur while sleeping just above, so
384 * check for it. Note that vfs_bio_awrite expects
385 * buffers to reside on a queue, while bwrite() and
388 * NOTE: NO B_LOCKED CHECK. Also no buf_checkwrite()
389 * check. This code will write out the buffer, period.
391 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
392 (info->flags & V_SAVE)) {
393 if (bp->b_vp == info->vp) {
394 if (bp->b_flags & B_CLUSTEROK) {
404 } else if (info->flags & V_SAVE) {
406 * Cannot set B_NOCACHE on a clean buffer as this will
407 * destroy the VM backing store which might actually
408 * be dirty (and unsynchronized).
411 bp->b_flags |= (B_INVAL | B_RELBUF);
415 bp->b_flags |= (B_INVAL | B_NOCACHE | B_RELBUF);
422 * Truncate a file's buffer and pages to a specified length. This
423 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
426 * The vnode must be locked.
428 static int vtruncbuf_bp_trunc_cmp(struct buf *bp, void *data);
429 static int vtruncbuf_bp_trunc(struct buf *bp, void *data);
430 static int vtruncbuf_bp_metasync_cmp(struct buf *bp, void *data);
431 static int vtruncbuf_bp_metasync(struct buf *bp, void *data);
434 vtruncbuf(struct vnode *vp, off_t length, int blksize)
437 const char *filename;
442 * Round up to the *next* block, then destroy the buffers in question.
443 * Since we are only removing some of the buffers we must rely on the
444 * scan count to determine whether a loop is necessary.
446 if ((count = (int)(length % blksize)) != 0)
447 truncloffset = length + (blksize - count);
449 truncloffset = length;
451 lwkt_gettoken(&vlock, &vp->v_token);
453 count = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree,
454 vtruncbuf_bp_trunc_cmp,
455 vtruncbuf_bp_trunc, &truncloffset);
456 count += RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
457 vtruncbuf_bp_trunc_cmp,
458 vtruncbuf_bp_trunc, &truncloffset);
462 * For safety, fsync any remaining metadata if the file is not being
463 * truncated to 0. Since the metadata does not represent the entire
464 * dirty list we have to rely on the hit count to ensure that we get
469 count = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
470 vtruncbuf_bp_metasync_cmp,
471 vtruncbuf_bp_metasync, vp);
476 * Clean out any left over VM backing store.
478 * It is possible to have in-progress I/O from buffers that were
479 * not part of the truncation. This should not happen if we
480 * are truncating to 0-length.
482 vnode_pager_setsize(vp, length);
483 bio_track_wait(&vp->v_track_write, 0, 0);
488 spin_lock_wr(&vp->v_spinlock);
489 filename = TAILQ_FIRST(&vp->v_namecache) ?
490 TAILQ_FIRST(&vp->v_namecache)->nc_name : "?";
491 spin_unlock_wr(&vp->v_spinlock);
494 * Make sure no buffers were instantiated while we were trying
495 * to clean out the remaining VM pages. This could occur due
496 * to busy dirty VM pages being flushed out to disk.
499 count = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree,
500 vtruncbuf_bp_trunc_cmp,
501 vtruncbuf_bp_trunc, &truncloffset);
502 count += RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
503 vtruncbuf_bp_trunc_cmp,
504 vtruncbuf_bp_trunc, &truncloffset);
506 kprintf("Warning: vtruncbuf(): Had to re-clean %d "
507 "left over buffers in %s\n", count, filename);
511 lwkt_reltoken(&vlock);
517 * The callback buffer is beyond the new file EOF and must be destroyed.
518 * Note that the compare function must conform to the RB_SCAN's requirements.
522 vtruncbuf_bp_trunc_cmp(struct buf *bp, void *data)
524 if (bp->b_loffset >= *(off_t *)data)
531 vtruncbuf_bp_trunc(struct buf *bp, void *data)
534 * Do not try to use a buffer we cannot immediately lock, but sleep
535 * anyway to prevent a livelock. The code will loop until all buffers
538 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
539 if (BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL) == 0)
543 bp->b_flags |= (B_INVAL | B_RELBUF | B_NOCACHE);
550 * Fsync all meta-data after truncating a file to be non-zero. Only metadata
551 * blocks (with a negative loffset) are scanned.
552 * Note that the compare function must conform to the RB_SCAN's requirements.
555 vtruncbuf_bp_metasync_cmp(struct buf *bp, void *data)
557 if (bp->b_loffset < 0)
563 vtruncbuf_bp_metasync(struct buf *bp, void *data)
565 struct vnode *vp = data;
567 if (bp->b_flags & B_DELWRI) {
569 * Do not try to use a buffer we cannot immediately lock,
570 * but sleep anyway to prevent a livelock. The code will
571 * loop until all buffers can be acted upon.
573 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
574 if (BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL) == 0)
590 * vfsync - implements a multipass fsync on a file which understands
591 * dependancies and meta-data. The passed vnode must be locked. The
592 * waitfor argument may be MNT_WAIT or MNT_NOWAIT, or MNT_LAZY.
594 * When fsyncing data asynchronously just do one consolidated pass starting
595 * with the most negative block number. This may not get all the data due
598 * When fsyncing data synchronously do a data pass, then a metadata pass,
599 * then do additional data+metadata passes to try to get all the data out.
601 static int vfsync_wait_output(struct vnode *vp,
602 int (*waitoutput)(struct vnode *, struct thread *));
603 static int vfsync_data_only_cmp(struct buf *bp, void *data);
604 static int vfsync_meta_only_cmp(struct buf *bp, void *data);
605 static int vfsync_lazy_range_cmp(struct buf *bp, void *data);
606 static int vfsync_bp(struct buf *bp, void *data);
615 int (*checkdef)(struct buf *);
619 vfsync(struct vnode *vp, int waitfor, int passes,
620 int (*checkdef)(struct buf *),
621 int (*waitoutput)(struct vnode *, struct thread *))
623 struct vfsync_info info;
627 bzero(&info, sizeof(info));
629 if ((info.checkdef = checkdef) == NULL)
632 lwkt_gettoken(&vlock, &vp->v_token);
637 * Lazy (filesystem syncer typ) Asynchronous plus limit the
638 * number of data (not meta) pages we try to flush to 1MB.
639 * A non-zero return means that lazy limit was reached.
641 info.lazylimit = 1024 * 1024;
643 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
644 vfsync_lazy_range_cmp, vfsync_bp, &info);
645 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
646 vfsync_meta_only_cmp, vfsync_bp, &info);
649 else if (!RB_EMPTY(&vp->v_rbdirty_tree))
650 vn_syncer_add_to_worklist(vp, 1);
655 * Asynchronous. Do a data-only pass and a meta-only pass.
658 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_data_only_cmp,
660 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_meta_only_cmp,
666 * Synchronous. Do a data-only pass, then a meta-data+data
667 * pass, then additional integrated passes to try to get
668 * all the dependancies flushed.
670 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_data_only_cmp,
672 error = vfsync_wait_output(vp, waitoutput);
674 info.skippedbufs = 0;
675 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
677 error = vfsync_wait_output(vp, waitoutput);
678 if (info.skippedbufs)
679 kprintf("Warning: vfsync skipped %d dirty bufs in pass2!\n", info.skippedbufs);
681 while (error == 0 && passes > 0 &&
682 !RB_EMPTY(&vp->v_rbdirty_tree)
685 info.synchronous = 1;
688 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
694 error = vfsync_wait_output(vp, waitoutput);
698 lwkt_reltoken(&vlock);
703 vfsync_wait_output(struct vnode *vp,
704 int (*waitoutput)(struct vnode *, struct thread *))
708 error = bio_track_wait(&vp->v_track_write, 0, 0);
710 error = waitoutput(vp, curthread);
715 vfsync_data_only_cmp(struct buf *bp, void *data)
717 if (bp->b_loffset < 0)
723 vfsync_meta_only_cmp(struct buf *bp, void *data)
725 if (bp->b_loffset < 0)
731 vfsync_lazy_range_cmp(struct buf *bp, void *data)
733 struct vfsync_info *info = data;
734 if (bp->b_loffset < info->vp->v_lazyw)
740 vfsync_bp(struct buf *bp, void *data)
742 struct vfsync_info *info = data;
743 struct vnode *vp = info->vp;
747 * if syncdeps is not set we do not try to write buffers which have
750 if (!info->synchronous && info->syncdeps == 0 && info->checkdef(bp))
754 * Ignore buffers that we cannot immediately lock. XXX
756 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
757 kprintf("Warning: vfsync_bp skipping dirty buffer %p\n", bp);
761 if ((bp->b_flags & B_DELWRI) == 0)
762 panic("vfsync_bp: buffer not dirty");
764 panic("vfsync_bp: buffer vp mismatch");
767 * B_NEEDCOMMIT (primarily used by NFS) is a state where the buffer
768 * has been written but an additional handshake with the device
769 * is required before we can dispose of the buffer. We have no idea
770 * how to do this so we have to skip these buffers.
772 if (bp->b_flags & B_NEEDCOMMIT) {
778 * Ask bioops if it is ok to sync
780 if (LIST_FIRST(&bp->b_dep) != NULL && buf_checkwrite(bp)) {
786 if (info->synchronous) {
788 * Synchronous flushing. An error may be returned.
794 * Asynchronous flushing. A negative return value simply
795 * stops the scan and is not considered an error. We use
796 * this to support limited MNT_LAZY flushes.
798 vp->v_lazyw = bp->b_loffset;
799 if ((vp->v_flag & VOBJBUF) && (bp->b_flags & B_CLUSTEROK)) {
800 info->lazycount += vfs_bio_awrite(bp);
802 info->lazycount += bp->b_bufsize;
806 if (info->lazylimit && info->lazycount >= info->lazylimit)
815 * Associate a buffer with a vnode.
820 bgetvp(struct vnode *vp, struct buf *bp)
824 KASSERT(bp->b_vp == NULL, ("bgetvp: not free"));
825 KKASSERT((bp->b_flags & (B_HASHED|B_DELWRI|B_VNCLEAN|B_VNDIRTY)) == 0);
828 * Insert onto list for new vnode.
830 lwkt_gettoken(&vlock, &vp->v_token);
831 if (buf_rb_hash_RB_INSERT(&vp->v_rbhash_tree, bp)) {
832 lwkt_reltoken(&vlock);
836 bp->b_flags |= B_HASHED;
837 bp->b_flags |= B_VNCLEAN;
838 if (buf_rb_tree_RB_INSERT(&vp->v_rbclean_tree, bp))
839 panic("reassignbuf: dup lblk/clean vp %p bp %p", vp, bp);
841 lwkt_reltoken(&vlock);
846 * Disassociate a buffer from a vnode.
849 brelvp(struct buf *bp)
854 KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
857 * Delete from old vnode list, if on one.
860 lwkt_gettoken(&vlock, &vp->v_token);
861 if (bp->b_flags & (B_VNDIRTY | B_VNCLEAN)) {
862 if (bp->b_flags & B_VNDIRTY)
863 buf_rb_tree_RB_REMOVE(&vp->v_rbdirty_tree, bp);
865 buf_rb_tree_RB_REMOVE(&vp->v_rbclean_tree, bp);
866 bp->b_flags &= ~(B_VNDIRTY | B_VNCLEAN);
868 if (bp->b_flags & B_HASHED) {
869 buf_rb_hash_RB_REMOVE(&vp->v_rbhash_tree, bp);
870 bp->b_flags &= ~B_HASHED;
872 if ((vp->v_flag & VONWORKLST) && RB_EMPTY(&vp->v_rbdirty_tree)) {
873 vp->v_flag &= ~VONWORKLST;
874 LIST_REMOVE(vp, v_synclist);
877 lwkt_reltoken(&vlock);
883 * Reassign the buffer to the proper clean/dirty list based on B_DELWRI.
884 * This routine is called when the state of the B_DELWRI bit is changed.
889 reassignbuf(struct buf *bp)
891 struct vnode *vp = bp->b_vp;
895 KKASSERT(vp != NULL);
899 * B_PAGING flagged buffers cannot be reassigned because their vp
900 * is not fully linked in.
902 if (bp->b_flags & B_PAGING)
903 panic("cannot reassign paging buffer");
905 lwkt_gettoken(&vlock, &vp->v_token);
906 if (bp->b_flags & B_DELWRI) {
908 * Move to the dirty list, add the vnode to the worklist
910 if (bp->b_flags & B_VNCLEAN) {
911 buf_rb_tree_RB_REMOVE(&vp->v_rbclean_tree, bp);
912 bp->b_flags &= ~B_VNCLEAN;
914 if ((bp->b_flags & B_VNDIRTY) == 0) {
915 if (buf_rb_tree_RB_INSERT(&vp->v_rbdirty_tree, bp)) {
916 panic("reassignbuf: dup lblk vp %p bp %p",
919 bp->b_flags |= B_VNDIRTY;
921 if ((vp->v_flag & VONWORKLST) == 0) {
922 switch (vp->v_type) {
929 vp->v_rdev->si_mountpoint != NULL) {
937 vn_syncer_add_to_worklist(vp, delay);
941 * Move to the clean list, remove the vnode from the worklist
942 * if no dirty blocks remain.
944 if (bp->b_flags & B_VNDIRTY) {
945 buf_rb_tree_RB_REMOVE(&vp->v_rbdirty_tree, bp);
946 bp->b_flags &= ~B_VNDIRTY;
948 if ((bp->b_flags & B_VNCLEAN) == 0) {
949 if (buf_rb_tree_RB_INSERT(&vp->v_rbclean_tree, bp)) {
950 panic("reassignbuf: dup lblk vp %p bp %p",
953 bp->b_flags |= B_VNCLEAN;
955 if ((vp->v_flag & VONWORKLST) &&
956 RB_EMPTY(&vp->v_rbdirty_tree)) {
957 vp->v_flag &= ~VONWORKLST;
958 LIST_REMOVE(vp, v_synclist);
961 lwkt_reltoken(&vlock);
965 * Create a vnode for a block device.
966 * Used for mounting the root file system.
968 extern struct vop_ops *devfs_vnode_dev_vops_p;
970 bdevvp(cdev_t dev, struct vnode **vpp)
980 error = getspecialvnode(VT_NON, NULL, &devfs_vnode_dev_vops_p,
991 v_associate_rdev(vp, dev);
992 vp->v_umajor = dev->si_umajor;
993 vp->v_uminor = dev->si_uminor;
1000 v_associate_rdev(struct vnode *vp, cdev_t dev)
1006 if (dev_is_good(dev) == 0)
1008 KKASSERT(vp->v_rdev == NULL);
1009 vp->v_rdev = reference_dev(dev);
1010 lwkt_gettoken(&ilock, &spechash_token);
1011 SLIST_INSERT_HEAD(&dev->si_hlist, vp, v_cdevnext);
1012 lwkt_reltoken(&ilock);
1017 v_release_rdev(struct vnode *vp)
1022 if ((dev = vp->v_rdev) != NULL) {
1023 lwkt_gettoken(&ilock, &spechash_token);
1024 SLIST_REMOVE(&dev->si_hlist, vp, vnode, v_cdevnext);
1027 lwkt_reltoken(&ilock);
1032 * Add a vnode to the alias list hung off the cdev_t. We only associate
1033 * the device number with the vnode. The actual device is not associated
1034 * until the vnode is opened (usually in spec_open()), and will be
1035 * disassociated on last close.
1038 addaliasu(struct vnode *nvp, int x, int y)
1040 if (nvp->v_type != VBLK && nvp->v_type != VCHR)
1041 panic("addaliasu on non-special vnode");
1047 * Simple call that a filesystem can make to try to get rid of a
1048 * vnode. It will fail if anyone is referencing the vnode (including
1051 * The filesystem can check whether its in-memory inode structure still
1052 * references the vp on return.
1055 vclean_unlocked(struct vnode *vp)
1058 if (sysref_isactive(&vp->v_sysref) == 0)
1064 * Disassociate a vnode from its underlying filesystem.
1066 * The vnode must be VX locked and referenced. In all normal situations
1067 * there are no active references. If vclean_vxlocked() is called while
1068 * there are active references, the vnode is being ripped out and we have
1069 * to call VOP_CLOSE() as appropriate before we can reclaim it.
1072 vclean_vxlocked(struct vnode *vp, int flags)
1079 * If the vnode has already been reclaimed we have nothing to do.
1081 if (vp->v_flag & VRECLAIMED)
1083 vp->v_flag |= VRECLAIMED;
1086 * Scrap the vfs cache
1088 while (cache_inval_vp(vp, 0) != 0) {
1089 kprintf("Warning: vnode %p clean/cache_resolution race detected\n", vp);
1090 tsleep(vp, 0, "vclninv", 2);
1094 * Check to see if the vnode is in use. If so we have to reference it
1095 * before we clean it out so that its count cannot fall to zero and
1096 * generate a race against ourselves to recycle it.
1098 active = sysref_isactive(&vp->v_sysref);
1101 * Clean out any buffers associated with the vnode and destroy its
1102 * object, if it has one.
1104 vinvalbuf(vp, V_SAVE, 0, 0);
1107 * If purging an active vnode (typically during a forced unmount
1108 * or reboot), it must be closed and deactivated before being
1109 * reclaimed. This isn't really all that safe, but what can
1112 * Note that neither of these routines unlocks the vnode.
1114 if (active && (flags & DOCLOSE)) {
1115 while ((n = vp->v_opencount) != 0) {
1116 if (vp->v_writecount)
1117 VOP_CLOSE(vp, FWRITE|FNONBLOCK);
1119 VOP_CLOSE(vp, FNONBLOCK);
1120 if (vp->v_opencount == n) {
1121 kprintf("Warning: unable to force-close"
1129 * If the vnode has not been deactivated, deactivated it. Deactivation
1130 * can create new buffers and VM pages so we have to call vinvalbuf()
1131 * again to make sure they all get flushed.
1133 * This can occur if a file with a link count of 0 needs to be
1136 if ((vp->v_flag & VINACTIVE) == 0) {
1137 vp->v_flag |= VINACTIVE;
1139 vinvalbuf(vp, V_SAVE, 0, 0);
1143 * If the vnode has an object, destroy it.
1145 if ((object = vp->v_object) != NULL) {
1146 if (object->ref_count == 0) {
1147 if ((object->flags & OBJ_DEAD) == 0)
1148 vm_object_terminate(object);
1150 vm_pager_deallocate(object);
1152 vp->v_flag &= ~VOBJBUF;
1154 KKASSERT((vp->v_flag & VOBJBUF) == 0);
1157 * Reclaim the vnode.
1159 if (VOP_RECLAIM(vp))
1160 panic("vclean: cannot reclaim");
1163 * Done with purge, notify sleepers of the grim news.
1165 vp->v_ops = &dead_vnode_vops_p;
1170 * If we are destroying an active vnode, reactivate it now that
1171 * we have reassociated it with deadfs. This prevents the system
1172 * from crashing on the vnode due to it being unexpectedly marked
1173 * as inactive or reclaimed.
1175 if (active && (flags & DOCLOSE)) {
1176 vp->v_flag &= ~(VINACTIVE|VRECLAIMED);
1181 * Eliminate all activity associated with the requested vnode
1182 * and with all vnodes aliased to the requested vnode.
1184 * The vnode must be referenced but should not be locked.
1187 vrevoke(struct vnode *vp, struct ucred *cred)
1196 * If the vnode has a device association, scrap all vnodes associated
1197 * with the device. Don't let the device disappear on us while we
1198 * are scrapping the vnodes.
1200 * The passed vp will probably show up in the list, do not VX lock
1203 * Releasing the vnode's rdev here can mess up specfs's call to
1204 * device close, so don't do it. The vnode has been disassociated
1205 * and the device will be closed after the last ref on the related
1206 * fp goes away (if not still open by e.g. the kernel).
1208 if (vp->v_type != VCHR) {
1209 error = fdrevoke(vp, DTYPE_VNODE, cred);
1212 if ((dev = vp->v_rdev) == NULL) {
1216 lwkt_gettoken(&ilock, &spechash_token);
1218 vqn = SLIST_FIRST(&dev->si_hlist);
1221 while ((vq = vqn) != NULL) {
1222 vqn = SLIST_NEXT(vqn, v_cdevnext);
1225 fdrevoke(vq, DTYPE_VNODE, cred);
1226 /*v_release_rdev(vq);*/
1229 lwkt_reltoken(&ilock);
1236 * This is called when the object underlying a vnode is being destroyed,
1237 * such as in a remove(). Try to recycle the vnode immediately if the
1238 * only active reference is our reference.
1240 * Directory vnodes in the namecache with children cannot be immediately
1241 * recycled because numerous VOP_N*() ops require them to be stable.
1244 vrecycle(struct vnode *vp)
1246 if (vp->v_sysref.refcnt <= 1) {
1247 if (cache_inval_vp_nonblock(vp))
1256 * Return the maximum I/O size allowed for strategy calls on VP.
1258 * If vp is VCHR or VBLK we dive the device, otherwise we use
1259 * the vp's mount info.
1262 vmaxiosize(struct vnode *vp)
1264 if (vp->v_type == VBLK || vp->v_type == VCHR) {
1265 return(vp->v_rdev->si_iosize_max);
1267 return(vp->v_mount->mnt_iosize_max);
1272 * Eliminate all activity associated with a vnode in preparation for reuse.
1274 * The vnode must be VX locked and refd and will remain VX locked and refd
1275 * on return. This routine may be called with the vnode in any state, as
1276 * long as it is VX locked. The vnode will be cleaned out and marked
1277 * VRECLAIMED but will not actually be reused until all existing refs and
1280 * NOTE: This routine may be called on a vnode which has not yet been
1281 * already been deactivated (VOP_INACTIVE), or on a vnode which has
1282 * already been reclaimed.
1284 * This routine is not responsible for placing us back on the freelist.
1285 * Instead, it happens automatically when the caller releases the VX lock
1286 * (assuming there aren't any other references).
1290 vgone_vxlocked(struct vnode *vp)
1293 * assert that the VX lock is held. This is an absolute requirement
1294 * now for vgone_vxlocked() to be called.
1296 KKASSERT(vp->v_lock.lk_exclusivecount == 1);
1299 * Clean out the filesystem specific data and set the VRECLAIMED
1300 * bit. Also deactivate the vnode if necessary.
1302 vclean_vxlocked(vp, DOCLOSE);
1305 * Delete from old mount point vnode list, if on one.
1307 if (vp->v_mount != NULL)
1308 insmntque(vp, NULL);
1311 * If special device, remove it from special device alias list
1312 * if it is on one. This should normally only occur if a vnode is
1313 * being revoked as the device should otherwise have been released
1316 if ((vp->v_type == VBLK || vp->v_type == VCHR) && vp->v_rdev != NULL) {
1327 * Lookup a vnode by device number.
1329 * Returns non-zero and *vpp set to a vref'd vnode on success.
1330 * Returns zero on failure.
1333 vfinddev(cdev_t dev, enum vtype type, struct vnode **vpp)
1338 lwkt_gettoken(&ilock, &spechash_token);
1339 SLIST_FOREACH(vp, &dev->si_hlist, v_cdevnext) {
1340 if (type == vp->v_type) {
1343 lwkt_reltoken(&ilock);
1347 lwkt_reltoken(&ilock);
1352 * Calculate the total number of references to a special device. This
1353 * routine may only be called for VBLK and VCHR vnodes since v_rdev is
1354 * an overloaded field. Since udev2dev can now return NULL, we have
1355 * to check for a NULL v_rdev.
1358 count_dev(cdev_t dev)
1364 if (SLIST_FIRST(&dev->si_hlist)) {
1365 lwkt_gettoken(&ilock, &spechash_token);
1366 SLIST_FOREACH(vp, &dev->si_hlist, v_cdevnext) {
1367 count += vp->v_opencount;
1369 lwkt_reltoken(&ilock);
1375 vcount(struct vnode *vp)
1377 if (vp->v_rdev == NULL)
1379 return(count_dev(vp->v_rdev));
1383 * Initialize VMIO for a vnode. This routine MUST be called before a
1384 * VFS can issue buffer cache ops on a vnode. It is typically called
1385 * when a vnode is initialized from its inode.
1388 vinitvmio(struct vnode *vp, off_t filesize)
1394 if ((object = vp->v_object) == NULL) {
1395 object = vnode_pager_alloc(vp, filesize, 0, 0);
1397 * Dereference the reference we just created. This assumes
1398 * that the object is associated with the vp.
1400 object->ref_count--;
1403 if (object->flags & OBJ_DEAD) {
1405 vm_object_dead_sleep(object, "vodead");
1406 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1410 KASSERT(vp->v_object != NULL, ("vinitvmio: NULL object"));
1411 vp->v_flag |= VOBJBUF;
1417 * Print out a description of a vnode.
1419 static char *typename[] =
1420 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD"};
1423 vprint(char *label, struct vnode *vp)
1428 kprintf("%s: %p: ", label, (void *)vp);
1430 kprintf("%p: ", (void *)vp);
1431 kprintf("type %s, sysrefs %d, writecount %d, holdcnt %d,",
1432 typename[vp->v_type],
1433 vp->v_sysref.refcnt, vp->v_writecount, vp->v_auxrefs);
1435 if (vp->v_flag & VROOT)
1436 strcat(buf, "|VROOT");
1437 if (vp->v_flag & VPFSROOT)
1438 strcat(buf, "|VPFSROOT");
1439 if (vp->v_flag & VTEXT)
1440 strcat(buf, "|VTEXT");
1441 if (vp->v_flag & VSYSTEM)
1442 strcat(buf, "|VSYSTEM");
1443 if (vp->v_flag & VFREE)
1444 strcat(buf, "|VFREE");
1445 if (vp->v_flag & VOBJBUF)
1446 strcat(buf, "|VOBJBUF");
1448 kprintf(" flags (%s)", &buf[1]);
1449 if (vp->v_data == NULL) {
1458 * Do the usual access checking.
1459 * file_mode, uid and gid are from the vnode in question,
1460 * while acc_mode and cred are from the VOP_ACCESS parameter list
1463 vaccess(enum vtype type, mode_t file_mode, uid_t uid, gid_t gid,
1464 mode_t acc_mode, struct ucred *cred)
1470 * Super-user always gets read/write access, but execute access depends
1471 * on at least one execute bit being set.
1473 if (priv_check_cred(cred, PRIV_ROOT, 0) == 0) {
1474 if ((acc_mode & VEXEC) && type != VDIR &&
1475 (file_mode & (S_IXUSR|S_IXGRP|S_IXOTH)) == 0)
1482 /* Otherwise, check the owner. */
1483 if (cred->cr_uid == uid) {
1484 if (acc_mode & VEXEC)
1486 if (acc_mode & VREAD)
1488 if (acc_mode & VWRITE)
1490 return ((file_mode & mask) == mask ? 0 : EACCES);
1493 /* Otherwise, check the groups. */
1494 ismember = groupmember(gid, cred);
1495 if (cred->cr_svgid == gid || ismember) {
1496 if (acc_mode & VEXEC)
1498 if (acc_mode & VREAD)
1500 if (acc_mode & VWRITE)
1502 return ((file_mode & mask) == mask ? 0 : EACCES);
1505 /* Otherwise, check everyone else. */
1506 if (acc_mode & VEXEC)
1508 if (acc_mode & VREAD)
1510 if (acc_mode & VWRITE)
1512 return ((file_mode & mask) == mask ? 0 : EACCES);
1516 #include <ddb/ddb.h>
1518 static int db_show_locked_vnodes(struct mount *mp, void *data);
1521 * List all of the locked vnodes in the system.
1522 * Called when debugging the kernel.
1524 DB_SHOW_COMMAND(lockedvnodes, lockedvnodes)
1526 kprintf("Locked vnodes\n");
1527 mountlist_scan(db_show_locked_vnodes, NULL,
1528 MNTSCAN_FORWARD|MNTSCAN_NOBUSY);
1532 db_show_locked_vnodes(struct mount *mp, void *data __unused)
1536 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
1537 if (vn_islocked(vp))
1545 * Top level filesystem related information gathering.
1547 static int sysctl_ovfs_conf (SYSCTL_HANDLER_ARGS);
1550 vfs_sysctl(SYSCTL_HANDLER_ARGS)
1552 int *name = (int *)arg1 - 1; /* XXX */
1553 u_int namelen = arg2 + 1; /* XXX */
1554 struct vfsconf *vfsp;
1557 #if 1 || defined(COMPAT_PRELITE2)
1558 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
1560 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
1564 /* all sysctl names at this level are at least name and field */
1566 return (ENOTDIR); /* overloaded */
1567 if (name[0] != VFS_GENERIC) {
1568 vfsp = vfsconf_find_by_typenum(name[0]);
1570 return (EOPNOTSUPP);
1571 return ((*vfsp->vfc_vfsops->vfs_sysctl)(&name[1], namelen - 1,
1572 oldp, oldlenp, newp, newlen, p));
1576 case VFS_MAXTYPENUM:
1579 maxtypenum = vfsconf_get_maxtypenum();
1580 return (SYSCTL_OUT(req, &maxtypenum, sizeof(maxtypenum)));
1583 return (ENOTDIR); /* overloaded */
1584 vfsp = vfsconf_find_by_typenum(name[2]);
1586 return (EOPNOTSUPP);
1587 return (SYSCTL_OUT(req, vfsp, sizeof *vfsp));
1589 return (EOPNOTSUPP);
1592 SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD, vfs_sysctl,
1593 "Generic filesystem");
1595 #if 1 || defined(COMPAT_PRELITE2)
1598 sysctl_ovfs_conf_iter(struct vfsconf *vfsp, void *data)
1601 struct ovfsconf ovfs;
1602 struct sysctl_req *req = (struct sysctl_req*) data;
1604 bzero(&ovfs, sizeof(ovfs));
1605 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
1606 strcpy(ovfs.vfc_name, vfsp->vfc_name);
1607 ovfs.vfc_index = vfsp->vfc_typenum;
1608 ovfs.vfc_refcount = vfsp->vfc_refcount;
1609 ovfs.vfc_flags = vfsp->vfc_flags;
1610 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
1612 return error; /* abort iteration with error code */
1614 return 0; /* continue iterating with next element */
1618 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
1620 return vfsconf_each(sysctl_ovfs_conf_iter, (void*)req);
1623 #endif /* 1 || COMPAT_PRELITE2 */
1626 * Check to see if a filesystem is mounted on a block device.
1629 vfs_mountedon(struct vnode *vp)
1633 if ((dev = vp->v_rdev) == NULL) {
1634 /* if (vp->v_type != VBLK)
1635 dev = get_dev(vp->v_uminor, vp->v_umajor); */
1637 if (dev != NULL && dev->si_mountpoint)
1643 * Unmount all filesystems. The list is traversed in reverse order
1644 * of mounting to avoid dependencies.
1647 static int vfs_umountall_callback(struct mount *mp, void *data);
1650 vfs_unmountall(void)
1655 count = mountlist_scan(vfs_umountall_callback,
1656 NULL, MNTSCAN_REVERSE|MNTSCAN_NOBUSY);
1662 vfs_umountall_callback(struct mount *mp, void *data)
1666 error = dounmount(mp, MNT_FORCE);
1668 mountlist_remove(mp);
1669 kprintf("unmount of filesystem mounted from %s failed (",
1670 mp->mnt_stat.f_mntfromname);
1674 kprintf("%d)\n", error);
1680 * Checks the mount flags for parameter mp and put the names comma-separated
1681 * into a string buffer buf with a size limit specified by len.
1683 * It returns the number of bytes written into buf, and (*errorp) will be
1684 * set to 0, EINVAL (if passed length is 0), or ENOSPC (supplied buffer was
1685 * not large enough). The buffer will be 0-terminated if len was not 0.
1688 vfs_flagstostr(int flags, const struct mountctl_opt *optp,
1689 char *buf, size_t len, int *errorp)
1691 static const struct mountctl_opt optnames[] = {
1692 { MNT_ASYNC, "asynchronous" },
1693 { MNT_EXPORTED, "NFS exported" },
1694 { MNT_LOCAL, "local" },
1695 { MNT_NOATIME, "noatime" },
1696 { MNT_NODEV, "nodev" },
1697 { MNT_NOEXEC, "noexec" },
1698 { MNT_NOSUID, "nosuid" },
1699 { MNT_NOSYMFOLLOW, "nosymfollow" },
1700 { MNT_QUOTA, "with-quotas" },
1701 { MNT_RDONLY, "read-only" },
1702 { MNT_SYNCHRONOUS, "synchronous" },
1703 { MNT_UNION, "union" },
1704 { MNT_NOCLUSTERR, "noclusterr" },
1705 { MNT_NOCLUSTERW, "noclusterw" },
1706 { MNT_SUIDDIR, "suiddir" },
1707 { MNT_SOFTDEP, "soft-updates" },
1708 { MNT_IGNORE, "ignore" },
1718 bleft = len - 1; /* leave room for trailing \0 */
1721 * Checks the size of the string. If it contains
1722 * any data, then we will append the new flags to
1725 actsize = strlen(buf);
1729 /* Default flags if no flags passed */
1733 if (bleft < 0) { /* degenerate case, 0-length buffer */
1738 for (; flags && optp->o_opt; ++optp) {
1739 if ((flags & optp->o_opt) == 0)
1741 optlen = strlen(optp->o_name);
1742 if (bwritten || actsize > 0) {
1747 buf[bwritten++] = ',';
1748 buf[bwritten++] = ' ';
1751 if (bleft < optlen) {
1755 bcopy(optp->o_name, buf + bwritten, optlen);
1758 flags &= ~optp->o_opt;
1762 * Space already reserved for trailing \0
1769 * Build hash lists of net addresses and hang them off the mount point.
1770 * Called by ufs_mount() to set up the lists of export addresses.
1773 vfs_hang_addrlist(struct mount *mp, struct netexport *nep,
1774 const struct export_args *argp)
1777 struct radix_node_head *rnh;
1779 struct radix_node *rn;
1780 struct sockaddr *saddr, *smask = 0;
1784 if (argp->ex_addrlen == 0) {
1785 if (mp->mnt_flag & MNT_DEFEXPORTED)
1787 np = &nep->ne_defexported;
1788 np->netc_exflags = argp->ex_flags;
1789 np->netc_anon = argp->ex_anon;
1790 np->netc_anon.cr_ref = 1;
1791 mp->mnt_flag |= MNT_DEFEXPORTED;
1795 if (argp->ex_addrlen < 0 || argp->ex_addrlen > MLEN)
1797 if (argp->ex_masklen < 0 || argp->ex_masklen > MLEN)
1800 i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
1801 np = (struct netcred *) kmalloc(i, M_NETADDR, M_WAITOK | M_ZERO);
1802 saddr = (struct sockaddr *) (np + 1);
1803 if ((error = copyin(argp->ex_addr, (caddr_t) saddr, argp->ex_addrlen)))
1805 if (saddr->sa_len > argp->ex_addrlen)
1806 saddr->sa_len = argp->ex_addrlen;
1807 if (argp->ex_masklen) {
1808 smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen);
1809 error = copyin(argp->ex_mask, (caddr_t)smask, argp->ex_masklen);
1812 if (smask->sa_len > argp->ex_masklen)
1813 smask->sa_len = argp->ex_masklen;
1815 i = saddr->sa_family;
1816 if ((rnh = nep->ne_rtable[i]) == 0) {
1818 * Seems silly to initialize every AF when most are not used,
1819 * do so on demand here
1821 SLIST_FOREACH(dom, &domains, dom_next)
1822 if (dom->dom_family == i && dom->dom_rtattach) {
1823 dom->dom_rtattach((void **) &nep->ne_rtable[i],
1827 if ((rnh = nep->ne_rtable[i]) == 0) {
1832 rn = (*rnh->rnh_addaddr) ((char *) saddr, (char *) smask, rnh,
1834 if (rn == 0 || np != (struct netcred *) rn) { /* already exists */
1838 np->netc_exflags = argp->ex_flags;
1839 np->netc_anon = argp->ex_anon;
1840 np->netc_anon.cr_ref = 1;
1843 kfree(np, M_NETADDR);
1849 vfs_free_netcred(struct radix_node *rn, void *w)
1851 struct radix_node_head *rnh = (struct radix_node_head *) w;
1853 (*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, rnh);
1854 kfree((caddr_t) rn, M_NETADDR);
1859 * Free the net address hash lists that are hanging off the mount points.
1862 vfs_free_addrlist(struct netexport *nep)
1865 struct radix_node_head *rnh;
1867 for (i = 0; i <= AF_MAX; i++)
1868 if ((rnh = nep->ne_rtable[i])) {
1869 (*rnh->rnh_walktree) (rnh, vfs_free_netcred,
1871 kfree((caddr_t) rnh, M_RTABLE);
1872 nep->ne_rtable[i] = 0;
1877 vfs_export(struct mount *mp, struct netexport *nep,
1878 const struct export_args *argp)
1882 if (argp->ex_flags & MNT_DELEXPORT) {
1883 if (mp->mnt_flag & MNT_EXPUBLIC) {
1884 vfs_setpublicfs(NULL, NULL, NULL);
1885 mp->mnt_flag &= ~MNT_EXPUBLIC;
1887 vfs_free_addrlist(nep);
1888 mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
1890 if (argp->ex_flags & MNT_EXPORTED) {
1891 if (argp->ex_flags & MNT_EXPUBLIC) {
1892 if ((error = vfs_setpublicfs(mp, nep, argp)) != 0)
1894 mp->mnt_flag |= MNT_EXPUBLIC;
1896 if ((error = vfs_hang_addrlist(mp, nep, argp)))
1898 mp->mnt_flag |= MNT_EXPORTED;
1905 * Set the publicly exported filesystem (WebNFS). Currently, only
1906 * one public filesystem is possible in the spec (RFC 2054 and 2055)
1909 vfs_setpublicfs(struct mount *mp, struct netexport *nep,
1910 const struct export_args *argp)
1917 * mp == NULL -> invalidate the current info, the FS is
1918 * no longer exported. May be called from either vfs_export
1919 * or unmount, so check if it hasn't already been done.
1922 if (nfs_pub.np_valid) {
1923 nfs_pub.np_valid = 0;
1924 if (nfs_pub.np_index != NULL) {
1925 FREE(nfs_pub.np_index, M_TEMP);
1926 nfs_pub.np_index = NULL;
1933 * Only one allowed at a time.
1935 if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
1939 * Get real filehandle for root of exported FS.
1941 bzero((caddr_t)&nfs_pub.np_handle, sizeof(nfs_pub.np_handle));
1942 nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid;
1944 if ((error = VFS_ROOT(mp, &rvp)))
1947 if ((error = VFS_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid)))
1953 * If an indexfile was specified, pull it in.
1955 if (argp->ex_indexfile != NULL) {
1958 error = vn_get_namelen(rvp, &namelen);
1961 MALLOC(nfs_pub.np_index, char *, namelen, M_TEMP,
1963 error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
1967 * Check for illegal filenames.
1969 for (cp = nfs_pub.np_index; *cp; cp++) {
1977 FREE(nfs_pub.np_index, M_TEMP);
1982 nfs_pub.np_mount = mp;
1983 nfs_pub.np_valid = 1;
1988 vfs_export_lookup(struct mount *mp, struct netexport *nep,
1989 struct sockaddr *nam)
1992 struct radix_node_head *rnh;
1993 struct sockaddr *saddr;
1996 if (mp->mnt_flag & MNT_EXPORTED) {
1998 * Lookup in the export list first.
2002 rnh = nep->ne_rtable[saddr->sa_family];
2004 np = (struct netcred *)
2005 (*rnh->rnh_matchaddr)((char *)saddr,
2007 if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
2012 * If no address match, use the default if it exists.
2014 if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
2015 np = &nep->ne_defexported;
2021 * perform msync on all vnodes under a mount point. The mount point must
2022 * be locked. This code is also responsible for lazy-freeing unreferenced
2023 * vnodes whos VM objects no longer contain pages.
2025 * NOTE: MNT_WAIT still skips vnodes in the VXLOCK state.
2027 * NOTE: XXX VOP_PUTPAGES and friends requires that the vnode be locked,
2028 * but vnode_pager_putpages() doesn't lock the vnode. We have to do it
2029 * way up in this high level function.
2031 static int vfs_msync_scan1(struct mount *mp, struct vnode *vp, void *data);
2032 static int vfs_msync_scan2(struct mount *mp, struct vnode *vp, void *data);
2035 vfs_msync(struct mount *mp, int flags)
2039 vmsc_flags = VMSC_GETVP;
2040 if (flags != MNT_WAIT)
2041 vmsc_flags |= VMSC_NOWAIT;
2042 vmntvnodescan(mp, vmsc_flags, vfs_msync_scan1, vfs_msync_scan2,
2043 (void *)(intptr_t)flags);
2047 * scan1 is a fast pre-check. There could be hundreds of thousands of
2048 * vnodes, we cannot afford to do anything heavy weight until we have a
2049 * fairly good indication that there is work to do.
2053 vfs_msync_scan1(struct mount *mp, struct vnode *vp, void *data)
2055 int flags = (int)(intptr_t)data;
2057 if ((vp->v_flag & VRECLAIMED) == 0) {
2058 if (vshouldmsync(vp))
2059 return(0); /* call scan2 */
2060 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
2061 (vp->v_flag & VOBJDIRTY) &&
2062 (flags == MNT_WAIT || vn_islocked(vp) == 0)) {
2063 return(0); /* call scan2 */
2068 * do not call scan2, continue the loop
2074 * This callback is handed a locked vnode.
2078 vfs_msync_scan2(struct mount *mp, struct vnode *vp, void *data)
2081 int flags = (int)(intptr_t)data;
2083 if (vp->v_flag & VRECLAIMED)
2086 if ((mp->mnt_flag & MNT_RDONLY) == 0 && (vp->v_flag & VOBJDIRTY)) {
2087 if ((obj = vp->v_object) != NULL) {
2088 vm_object_page_clean(obj, 0, 0,
2089 flags == MNT_WAIT ? OBJPC_SYNC : OBJPC_NOSYNC);
2096 * Record a process's interest in events which might happen to
2097 * a vnode. Because poll uses the historic select-style interface
2098 * internally, this routine serves as both the ``check for any
2099 * pending events'' and the ``record my interest in future events''
2100 * functions. (These are done together, while the lock is held,
2101 * to avoid race conditions.)
2104 vn_pollrecord(struct vnode *vp, int events)
2108 KKASSERT(curthread->td_proc != NULL);
2110 lwkt_gettoken(&vlock, &vp->v_token);
2111 if (vp->v_pollinfo.vpi_revents & events) {
2113 * This leaves events we are not interested
2114 * in available for the other process which
2115 * which presumably had requested them
2116 * (otherwise they would never have been
2119 events &= vp->v_pollinfo.vpi_revents;
2120 vp->v_pollinfo.vpi_revents &= ~events;
2122 lwkt_reltoken(&vlock);
2125 vp->v_pollinfo.vpi_events |= events;
2126 selrecord(curthread, &vp->v_pollinfo.vpi_selinfo);
2127 lwkt_reltoken(&vlock);
2132 * Note the occurrence of an event. If the VN_POLLEVENT macro is used,
2133 * it is possible for us to miss an event due to race conditions, but
2134 * that condition is expected to be rare, so for the moment it is the
2135 * preferred interface.
2138 vn_pollevent(struct vnode *vp, int events)
2142 lwkt_gettoken(&vlock, &vp->v_token);
2143 if (vp->v_pollinfo.vpi_events & events) {
2145 * We clear vpi_events so that we don't
2146 * call selwakeup() twice if two events are
2147 * posted before the polling process(es) is
2148 * awakened. This also ensures that we take at
2149 * most one selwakeup() if the polling process
2150 * is no longer interested. However, it does
2151 * mean that only one event can be noticed at
2152 * a time. (Perhaps we should only clear those
2153 * event bits which we note?) XXX
2155 vp->v_pollinfo.vpi_events = 0; /* &= ~events ??? */
2156 vp->v_pollinfo.vpi_revents |= events;
2157 selwakeup(&vp->v_pollinfo.vpi_selinfo);
2159 lwkt_reltoken(&vlock);
2163 * Wake up anyone polling on vp because it is being revoked.
2164 * This depends on dead_poll() returning POLLHUP for correct
2168 vn_pollgone(struct vnode *vp)
2172 lwkt_gettoken(&vlock, &vp->v_token);
2173 if (vp->v_pollinfo.vpi_events) {
2174 vp->v_pollinfo.vpi_events = 0;
2175 selwakeup(&vp->v_pollinfo.vpi_selinfo);
2177 lwkt_reltoken(&vlock);
2181 * extract the cdev_t from a VBLK or VCHR. The vnode must have been opened
2182 * (or v_rdev might be NULL).
2185 vn_todev(struct vnode *vp)
2187 if (vp->v_type != VBLK && vp->v_type != VCHR)
2189 KKASSERT(vp->v_rdev != NULL);
2190 return (vp->v_rdev);
2194 * Check if vnode represents a disk device. The vnode does not need to be
2200 vn_isdisk(struct vnode *vp, int *errp)
2204 if (vp->v_type != VCHR) {
2217 if (dev_is_good(dev) == 0) {
2222 if ((dev_dflags(dev) & D_DISK) == 0) {
2233 vn_get_namelen(struct vnode *vp, int *namelen)
2236 register_t retval[2];
2238 error = VOP_PATHCONF(vp, _PC_NAME_MAX, retval);
2241 *namelen = (int)retval[0];
2246 vop_write_dirent(int *error, struct uio *uio, ino_t d_ino, uint8_t d_type,
2247 uint16_t d_namlen, const char *d_name)
2252 len = _DIRENT_RECLEN(d_namlen);
2253 if (len > uio->uio_resid)
2256 dp = kmalloc(len, M_TEMP, M_WAITOK | M_ZERO);
2259 dp->d_namlen = d_namlen;
2260 dp->d_type = d_type;
2261 bcopy(d_name, dp->d_name, d_namlen);
2263 *error = uiomove((caddr_t)dp, len, uio);
2271 vn_mark_atime(struct vnode *vp, struct thread *td)
2273 struct proc *p = td->td_proc;
2274 struct ucred *cred = p ? p->p_ucred : proc0.p_ucred;
2276 if ((vp->v_mount->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0) {
2277 VOP_MARKATIME(vp, cred);