2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * @(#)vfs_subr.c 8.31 (Berkeley) 5/26/95
39 * $FreeBSD: src/sys/kern/vfs_subr.c,v 1.249.2.30 2003/04/04 20:35:57 tegge Exp $
40 * $DragonFly: src/sys/kern/vfs_subr.c,v 1.118 2008/09/17 21:44:18 dillon Exp $
44 * External virtual filesystem routines
48 #include <sys/param.h>
49 #include <sys/systm.h>
52 #include <sys/dirent.h>
53 #include <sys/domain.h>
54 #include <sys/eventhandler.h>
55 #include <sys/fcntl.h>
57 #include <sys/kernel.h>
58 #include <sys/kthread.h>
59 #include <sys/malloc.h>
61 #include <sys/mount.h>
63 #include <sys/reboot.h>
64 #include <sys/socket.h>
66 #include <sys/sysctl.h>
67 #include <sys/syslog.h>
68 #include <sys/unistd.h>
69 #include <sys/vmmeter.h>
70 #include <sys/vnode.h>
72 #include <machine/limits.h>
75 #include <vm/vm_object.h>
76 #include <vm/vm_extern.h>
77 #include <vm/vm_kern.h>
79 #include <vm/vm_map.h>
80 #include <vm/vm_page.h>
81 #include <vm/vm_pager.h>
82 #include <vm/vnode_pager.h>
83 #include <vm/vm_zone.h>
86 #include <sys/thread2.h>
87 #include <sys/sysref2.h>
89 static MALLOC_DEFINE(M_NETADDR, "Export Host", "Export host address structure");
92 SYSCTL_INT(_debug, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0, "");
94 SYSCTL_INT(_vfs, OID_AUTO, fastdev, CTLFLAG_RW, &vfs_fastdev, 0, "");
96 enum vtype iftovt_tab[16] = {
97 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
98 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
100 int vttoif_tab[9] = {
101 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
102 S_IFSOCK, S_IFIFO, S_IFMT,
105 static int reassignbufcalls;
106 SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW,
107 &reassignbufcalls, 0, "");
108 static int reassignbufloops;
109 SYSCTL_INT(_vfs, OID_AUTO, reassignbufloops, CTLFLAG_RW,
110 &reassignbufloops, 0, "");
111 static int reassignbufsortgood;
112 SYSCTL_INT(_vfs, OID_AUTO, reassignbufsortgood, CTLFLAG_RW,
113 &reassignbufsortgood, 0, "");
114 static int reassignbufsortbad;
115 SYSCTL_INT(_vfs, OID_AUTO, reassignbufsortbad, CTLFLAG_RW,
116 &reassignbufsortbad, 0, "");
117 static int reassignbufmethod = 1;
118 SYSCTL_INT(_vfs, OID_AUTO, reassignbufmethod, CTLFLAG_RW,
119 &reassignbufmethod, 0, "");
121 int nfs_mount_type = -1;
122 static struct lwkt_token spechash_token;
123 struct nfs_public nfs_pub; /* publicly exported FS */
126 SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RW,
127 &desiredvnodes, 0, "Maximum number of vnodes");
129 static void vfs_free_addrlist (struct netexport *nep);
130 static int vfs_free_netcred (struct radix_node *rn, void *w);
131 static int vfs_hang_addrlist (struct mount *mp, struct netexport *nep,
132 const struct export_args *argp);
134 extern int dev_ref_debug;
137 * Red black tree functions
139 static int rb_buf_compare(struct buf *b1, struct buf *b2);
140 RB_GENERATE2(buf_rb_tree, buf, b_rbnode, rb_buf_compare, off_t, b_loffset);
141 RB_GENERATE2(buf_rb_hash, buf, b_rbhash, rb_buf_compare, off_t, b_loffset);
144 rb_buf_compare(struct buf *b1, struct buf *b2)
146 if (b1->b_loffset < b2->b_loffset)
148 if (b1->b_loffset > b2->b_loffset)
154 * Returns non-zero if the vnode is a candidate for lazy msyncing.
157 vshouldmsync(struct vnode *vp)
159 if (vp->v_auxrefs != 0 || vp->v_sysref.refcnt > 0)
160 return (0); /* other holders */
162 (vp->v_object->ref_count || vp->v_object->resident_page_count)) {
169 * Initialize the vnode management data structures.
171 * Called from vfsinit()
177 * Desiredvnodes is kern.maxvnodes. We want to scale it
178 * according to available system memory but we may also have
179 * to limit it based on available KVM, which is capped on 32 bit
182 desiredvnodes = min(maxproc + vmstats.v_page_count / 4,
184 (sizeof(struct vm_object) + sizeof(struct vnode))));
186 lwkt_token_init(&spechash_token);
190 * Knob to control the precision of file timestamps:
192 * 0 = seconds only; nanoseconds zeroed.
193 * 1 = seconds and nanoseconds, accurate within 1/HZ.
194 * 2 = seconds and nanoseconds, truncated to microseconds.
195 * >=3 = seconds and nanoseconds, maximum precision.
197 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC };
199 static int timestamp_precision = TSP_SEC;
200 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW,
201 ×tamp_precision, 0, "");
204 * Get a current timestamp.
209 vfs_timestamp(struct timespec *tsp)
213 switch (timestamp_precision) {
215 tsp->tv_sec = time_second;
223 TIMEVAL_TO_TIMESPEC(&tv, tsp);
233 * Set vnode attributes to VNOVAL
236 vattr_null(struct vattr *vap)
239 vap->va_size = VNOVAL;
240 vap->va_bytes = VNOVAL;
241 vap->va_mode = VNOVAL;
242 vap->va_nlink = VNOVAL;
243 vap->va_uid = VNOVAL;
244 vap->va_gid = VNOVAL;
245 vap->va_fsid = VNOVAL;
246 vap->va_fileid = VNOVAL;
247 vap->va_blocksize = VNOVAL;
248 vap->va_rmajor = VNOVAL;
249 vap->va_rminor = VNOVAL;
250 vap->va_atime.tv_sec = VNOVAL;
251 vap->va_atime.tv_nsec = VNOVAL;
252 vap->va_mtime.tv_sec = VNOVAL;
253 vap->va_mtime.tv_nsec = VNOVAL;
254 vap->va_ctime.tv_sec = VNOVAL;
255 vap->va_ctime.tv_nsec = VNOVAL;
256 vap->va_flags = VNOVAL;
257 vap->va_gen = VNOVAL;
259 vap->va_fsmid = VNOVAL;
260 /* va_*_uuid fields are only valid if related flags are set */
264 * Flush out and invalidate all buffers associated with a vnode.
268 static int vinvalbuf_bp(struct buf *bp, void *data);
270 struct vinvalbuf_bp_info {
278 vupdatefsmid(struct vnode *vp)
280 atomic_set_int(&vp->v_flag, VFSMID);
284 vinvalbuf(struct vnode *vp, int flags, int slpflag, int slptimeo)
286 struct vinvalbuf_bp_info info;
291 lwkt_gettoken(&vlock, &vp->v_token);
294 * If we are being asked to save, call fsync to ensure that the inode
297 if (flags & V_SAVE) {
298 error = bio_track_wait(&vp->v_track_write, slpflag, slptimeo);
301 if (!RB_EMPTY(&vp->v_rbdirty_tree)) {
302 if ((error = VOP_FSYNC(vp, MNT_WAIT)) != 0)
306 * Dirty bufs may be left or generated via races
307 * in circumstances where vinvalbuf() is called on
308 * a vnode not undergoing reclamation. Only
309 * panic if we are trying to reclaim the vnode.
311 if ((vp->v_flag & VRECLAIMED) &&
312 (bio_track_active(&vp->v_track_write) ||
313 !RB_EMPTY(&vp->v_rbdirty_tree))) {
314 panic("vinvalbuf: dirty bufs");
318 info.slptimeo = slptimeo;
319 info.lkflags = LK_EXCLUSIVE | LK_SLEEPFAIL;
320 if (slpflag & PCATCH)
321 info.lkflags |= LK_PCATCH;
326 * Flush the buffer cache until nothing is left.
328 while (!RB_EMPTY(&vp->v_rbclean_tree) ||
329 !RB_EMPTY(&vp->v_rbdirty_tree)) {
330 error = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree, NULL,
331 vinvalbuf_bp, &info);
333 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
334 vinvalbuf_bp, &info);
339 * Wait for I/O completion. We may block in the pip code so we have
343 bio_track_wait(&vp->v_track_write, 0, 0);
344 if ((object = vp->v_object) != NULL) {
345 while (object->paging_in_progress)
346 vm_object_pip_sleep(object, "vnvlbx");
348 } while (bio_track_active(&vp->v_track_write));
351 * Destroy the copy in the VM cache, too.
353 if ((object = vp->v_object) != NULL) {
354 vm_object_page_remove(object, 0, 0,
355 (flags & V_SAVE) ? TRUE : FALSE);
358 if (!RB_EMPTY(&vp->v_rbdirty_tree) || !RB_EMPTY(&vp->v_rbclean_tree))
359 panic("vinvalbuf: flush failed");
360 if (!RB_EMPTY(&vp->v_rbhash_tree))
361 panic("vinvalbuf: flush failed, buffers still present");
364 lwkt_reltoken(&vlock);
369 vinvalbuf_bp(struct buf *bp, void *data)
371 struct vinvalbuf_bp_info *info = data;
374 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
375 error = BUF_TIMELOCK(bp, info->lkflags,
376 "vinvalbuf", info->slptimeo);
386 KKASSERT(bp->b_vp == info->vp);
389 * XXX Since there are no node locks for NFS, I
390 * believe there is a slight chance that a delayed
391 * write will occur while sleeping just above, so
392 * check for it. Note that vfs_bio_awrite expects
393 * buffers to reside on a queue, while bwrite() and
396 * NOTE: NO B_LOCKED CHECK. Also no buf_checkwrite()
397 * check. This code will write out the buffer, period.
399 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
400 (info->flags & V_SAVE)) {
401 if (bp->b_vp == info->vp) {
402 if (bp->b_flags & B_CLUSTEROK) {
406 bp->b_flags |= B_ASYNC;
413 } else if (info->flags & V_SAVE) {
415 * Cannot set B_NOCACHE on a clean buffer as this will
416 * destroy the VM backing store which might actually
417 * be dirty (and unsynchronized).
420 bp->b_flags |= (B_INVAL | B_RELBUF);
421 bp->b_flags &= ~B_ASYNC;
425 bp->b_flags |= (B_INVAL | B_NOCACHE | B_RELBUF);
426 bp->b_flags &= ~B_ASYNC;
433 * Truncate a file's buffer and pages to a specified length. This
434 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
437 * The vnode must be locked.
439 static int vtruncbuf_bp_trunc_cmp(struct buf *bp, void *data);
440 static int vtruncbuf_bp_trunc(struct buf *bp, void *data);
441 static int vtruncbuf_bp_metasync_cmp(struct buf *bp, void *data);
442 static int vtruncbuf_bp_metasync(struct buf *bp, void *data);
445 vtruncbuf(struct vnode *vp, off_t length, int blksize)
448 const char *filename;
453 * Round up to the *next* block, then destroy the buffers in question.
454 * Since we are only removing some of the buffers we must rely on the
455 * scan count to determine whether a loop is necessary.
457 if ((count = (int)(length % blksize)) != 0)
458 truncloffset = length + (blksize - count);
460 truncloffset = length;
462 lwkt_gettoken(&vlock, &vp->v_token);
464 count = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree,
465 vtruncbuf_bp_trunc_cmp,
466 vtruncbuf_bp_trunc, &truncloffset);
467 count += RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
468 vtruncbuf_bp_trunc_cmp,
469 vtruncbuf_bp_trunc, &truncloffset);
473 * For safety, fsync any remaining metadata if the file is not being
474 * truncated to 0. Since the metadata does not represent the entire
475 * dirty list we have to rely on the hit count to ensure that we get
480 count = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
481 vtruncbuf_bp_metasync_cmp,
482 vtruncbuf_bp_metasync, vp);
487 * Clean out any left over VM backing store.
489 * It is possible to have in-progress I/O from buffers that were
490 * not part of the truncation. This should not happen if we
491 * are truncating to 0-length.
493 vnode_pager_setsize(vp, length);
494 bio_track_wait(&vp->v_track_write, 0, 0);
496 filename = TAILQ_FIRST(&vp->v_namecache) ?
497 TAILQ_FIRST(&vp->v_namecache)->nc_name : "?";
500 * Make sure no buffers were instantiated while we were trying
501 * to clean out the remaining VM pages. This could occur due
502 * to busy dirty VM pages being flushed out to disk.
505 count = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree,
506 vtruncbuf_bp_trunc_cmp,
507 vtruncbuf_bp_trunc, &truncloffset);
508 count += RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
509 vtruncbuf_bp_trunc_cmp,
510 vtruncbuf_bp_trunc, &truncloffset);
512 kprintf("Warning: vtruncbuf(): Had to re-clean %d "
513 "left over buffers in %s\n", count, filename);
517 lwkt_reltoken(&vlock);
523 * The callback buffer is beyond the new file EOF and must be destroyed.
524 * Note that the compare function must conform to the RB_SCAN's requirements.
528 vtruncbuf_bp_trunc_cmp(struct buf *bp, void *data)
530 if (bp->b_loffset >= *(off_t *)data)
537 vtruncbuf_bp_trunc(struct buf *bp, void *data)
540 * Do not try to use a buffer we cannot immediately lock, but sleep
541 * anyway to prevent a livelock. The code will loop until all buffers
544 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
545 if (BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL) == 0)
549 bp->b_flags |= (B_INVAL | B_RELBUF | B_NOCACHE);
550 bp->b_flags &= ~B_ASYNC;
557 * Fsync all meta-data after truncating a file to be non-zero. Only metadata
558 * blocks (with a negative loffset) are scanned.
559 * Note that the compare function must conform to the RB_SCAN's requirements.
562 vtruncbuf_bp_metasync_cmp(struct buf *bp, void *data)
564 if (bp->b_loffset < 0)
570 vtruncbuf_bp_metasync(struct buf *bp, void *data)
572 struct vnode *vp = data;
574 if (bp->b_flags & B_DELWRI) {
576 * Do not try to use a buffer we cannot immediately lock,
577 * but sleep anyway to prevent a livelock. The code will
578 * loop until all buffers can be acted upon.
580 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
581 if (BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL) == 0)
585 if (bp->b_vp == vp) {
586 bp->b_flags |= B_ASYNC;
588 bp->b_flags &= ~B_ASYNC;
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.
978 bdevvp(cdev_t dev, struct vnode **vpp)
988 error = getspecialvnode(VT_NON, NULL, &spec_vnode_vops_p, &nvp, 0, 0);
995 vp->v_umajor = dev->si_umajor;
996 vp->v_uminor = dev->si_uminor;
1003 v_associate_rdev(struct vnode *vp, cdev_t dev)
1009 if (dev_is_good(dev) == 0)
1011 KKASSERT(vp->v_rdev == NULL);
1014 vp->v_rdev = reference_dev(dev);
1015 lwkt_gettoken(&ilock, &spechash_token);
1016 SLIST_INSERT_HEAD(&dev->si_hlist, vp, v_cdevnext);
1017 lwkt_reltoken(&ilock);
1022 v_release_rdev(struct vnode *vp)
1027 if ((dev = vp->v_rdev) != NULL) {
1028 lwkt_gettoken(&ilock, &spechash_token);
1029 SLIST_REMOVE(&dev->si_hlist, vp, vnode, v_cdevnext);
1032 lwkt_reltoken(&ilock);
1037 * Add a vnode to the alias list hung off the cdev_t. We only associate
1038 * the device number with the vnode. The actual device is not associated
1039 * until the vnode is opened (usually in spec_open()), and will be
1040 * disassociated on last close.
1043 addaliasu(struct vnode *nvp, int x, int y)
1045 if (nvp->v_type != VBLK && nvp->v_type != VCHR)
1046 panic("addaliasu on non-special vnode");
1052 * Simple call that a filesystem can make to try to get rid of a
1053 * vnode. It will fail if anyone is referencing the vnode (including
1056 * The filesystem can check whether its in-memory inode structure still
1057 * references the vp on return.
1060 vclean_unlocked(struct vnode *vp)
1063 if (sysref_isactive(&vp->v_sysref) == 0)
1069 * Disassociate a vnode from its underlying filesystem.
1071 * The vnode must be VX locked and referenced. In all normal situations
1072 * there are no active references. If vclean_vxlocked() is called while
1073 * there are active references, the vnode is being ripped out and we have
1074 * to call VOP_CLOSE() as appropriate before we can reclaim it.
1077 vclean_vxlocked(struct vnode *vp, int flags)
1084 * If the vnode has already been reclaimed we have nothing to do.
1086 if (vp->v_flag & VRECLAIMED)
1088 vp->v_flag |= VRECLAIMED;
1091 * Scrap the vfs cache
1093 while (cache_inval_vp(vp, 0) != 0) {
1094 kprintf("Warning: vnode %p clean/cache_resolution race detected\n", vp);
1095 tsleep(vp, 0, "vclninv", 2);
1099 * Check to see if the vnode is in use. If so we have to reference it
1100 * before we clean it out so that its count cannot fall to zero and
1101 * generate a race against ourselves to recycle it.
1103 active = sysref_isactive(&vp->v_sysref);
1106 * Clean out any buffers associated with the vnode and destroy its
1107 * object, if it has one.
1109 vinvalbuf(vp, V_SAVE, 0, 0);
1112 * If purging an active vnode (typically during a forced unmount
1113 * or reboot), it must be closed and deactivated before being
1114 * reclaimed. This isn't really all that safe, but what can
1117 * Note that neither of these routines unlocks the vnode.
1119 if (active && (flags & DOCLOSE)) {
1120 while ((n = vp->v_opencount) != 0) {
1121 if (vp->v_writecount)
1122 VOP_CLOSE(vp, FWRITE|FNONBLOCK);
1124 VOP_CLOSE(vp, FNONBLOCK);
1125 if (vp->v_opencount == n) {
1126 kprintf("Warning: unable to force-close"
1134 * If the vnode has not been deactivated, deactivated it. Deactivation
1135 * can create new buffers and VM pages so we have to call vinvalbuf()
1136 * again to make sure they all get flushed.
1138 * This can occur if a file with a link count of 0 needs to be
1141 if ((vp->v_flag & VINACTIVE) == 0) {
1142 vp->v_flag |= VINACTIVE;
1144 vinvalbuf(vp, V_SAVE, 0, 0);
1148 * If the vnode has an object, destroy it.
1150 if ((object = vp->v_object) != NULL) {
1151 if (object->ref_count == 0) {
1152 if ((object->flags & OBJ_DEAD) == 0)
1153 vm_object_terminate(object);
1155 vm_pager_deallocate(object);
1157 vp->v_flag &= ~VOBJBUF;
1159 KKASSERT((vp->v_flag & VOBJBUF) == 0);
1162 * Reclaim the vnode.
1164 if (VOP_RECLAIM(vp))
1165 panic("vclean: cannot reclaim");
1168 * Done with purge, notify sleepers of the grim news.
1170 vp->v_ops = &dead_vnode_vops_p;
1175 * If we are destroying an active vnode, reactivate it now that
1176 * we have reassociated it with deadfs. This prevents the system
1177 * from crashing on the vnode due to it being unexpectedly marked
1178 * as inactive or reclaimed.
1180 if (active && (flags & DOCLOSE)) {
1181 vp->v_flag &= ~(VINACTIVE|VRECLAIMED);
1186 * Eliminate all activity associated with the requested vnode
1187 * and with all vnodes aliased to the requested vnode.
1189 * The vnode must be referenced but should not be locked.
1192 vrevoke(struct vnode *vp, struct ucred *cred)
1201 * If the vnode has a device association, scrap all vnodes associated
1202 * with the device. Don't let the device disappear on us while we
1203 * are scrapping the vnodes.
1205 * The passed vp will probably show up in the list, do not VX lock
1208 * Releasing the vnode's rdev here can mess up specfs's call to
1209 * device close, so don't do it. The vnode has been disassociated
1210 * and the device will be closed after the last ref on the related
1211 * fp goes away (if not still open by e.g. the kernel).
1213 if (vp->v_type != VCHR) {
1214 error = fdrevoke(vp, DTYPE_VNODE, cred);
1217 if ((dev = vp->v_rdev) == NULL) {
1218 if ((dev = get_dev(vp->v_umajor, vp->v_uminor)) == NULL)
1222 lwkt_gettoken(&ilock, &spechash_token);
1224 vqn = SLIST_FIRST(&dev->si_hlist);
1227 while ((vq = vqn) != NULL) {
1228 vqn = SLIST_NEXT(vqn, v_cdevnext);
1231 fdrevoke(vq, DTYPE_VNODE, cred);
1232 /*v_release_rdev(vq);*/
1235 lwkt_reltoken(&ilock);
1242 * This is called when the object underlying a vnode is being destroyed,
1243 * such as in a remove(). Try to recycle the vnode immediately if the
1244 * only active reference is our reference.
1246 * Directory vnodes in the namecache with children cannot be immediately
1247 * recycled because numerous VOP_N*() ops require them to be stable.
1250 vrecycle(struct vnode *vp)
1252 if (vp->v_sysref.refcnt <= 1) {
1253 if (cache_inval_vp_nonblock(vp))
1262 * Return the maximum I/O size allowed for strategy calls on VP.
1264 * If vp is VCHR or VBLK we dive the device, otherwise we use
1265 * the vp's mount info.
1268 vmaxiosize(struct vnode *vp)
1270 if (vp->v_type == VBLK || vp->v_type == VCHR) {
1271 return(vp->v_rdev->si_iosize_max);
1273 return(vp->v_mount->mnt_iosize_max);
1278 * Eliminate all activity associated with a vnode in preparation for reuse.
1280 * The vnode must be VX locked and refd and will remain VX locked and refd
1281 * on return. This routine may be called with the vnode in any state, as
1282 * long as it is VX locked. The vnode will be cleaned out and marked
1283 * VRECLAIMED but will not actually be reused until all existing refs and
1286 * NOTE: This routine may be called on a vnode which has not yet been
1287 * already been deactivated (VOP_INACTIVE), or on a vnode which has
1288 * already been reclaimed.
1290 * This routine is not responsible for placing us back on the freelist.
1291 * Instead, it happens automatically when the caller releases the VX lock
1292 * (assuming there aren't any other references).
1296 vgone_vxlocked(struct vnode *vp)
1299 * assert that the VX lock is held. This is an absolute requirement
1300 * now for vgone_vxlocked() to be called.
1302 KKASSERT(vp->v_lock.lk_exclusivecount == 1);
1305 * Clean out the filesystem specific data and set the VRECLAIMED
1306 * bit. Also deactivate the vnode if necessary.
1308 vclean_vxlocked(vp, DOCLOSE);
1311 * Delete from old mount point vnode list, if on one.
1313 if (vp->v_mount != NULL)
1314 insmntque(vp, NULL);
1317 * If special device, remove it from special device alias list
1318 * if it is on one. This should normally only occur if a vnode is
1319 * being revoked as the device should otherwise have been released
1322 if ((vp->v_type == VBLK || vp->v_type == VCHR) && vp->v_rdev != NULL) {
1333 * Lookup a vnode by device number.
1335 * Returns non-zero and *vpp set to a vref'd vnode on success.
1336 * Returns zero on failure.
1339 vfinddev(cdev_t dev, enum vtype type, struct vnode **vpp)
1344 lwkt_gettoken(&ilock, &spechash_token);
1345 SLIST_FOREACH(vp, &dev->si_hlist, v_cdevnext) {
1346 if (type == vp->v_type) {
1349 lwkt_reltoken(&ilock);
1353 lwkt_reltoken(&ilock);
1358 * Calculate the total number of references to a special device. This
1359 * routine may only be called for VBLK and VCHR vnodes since v_rdev is
1360 * an overloaded field. Since udev2dev can now return NULL, we have
1361 * to check for a NULL v_rdev.
1364 count_dev(cdev_t dev)
1370 if (SLIST_FIRST(&dev->si_hlist)) {
1371 lwkt_gettoken(&ilock, &spechash_token);
1372 SLIST_FOREACH(vp, &dev->si_hlist, v_cdevnext) {
1373 if (vp->v_sysref.refcnt > 0)
1374 count += vp->v_sysref.refcnt;
1376 lwkt_reltoken(&ilock);
1382 count_udev(int x, int y)
1386 if ((dev = get_dev(x, y)) == NULL)
1388 return(count_dev(dev));
1392 vcount(struct vnode *vp)
1394 if (vp->v_rdev == NULL)
1396 return(count_dev(vp->v_rdev));
1400 * Initialize VMIO for a vnode. This routine MUST be called before a
1401 * VFS can issue buffer cache ops on a vnode. It is typically called
1402 * when a vnode is initialized from its inode.
1405 vinitvmio(struct vnode *vp, off_t filesize)
1411 if ((object = vp->v_object) == NULL) {
1412 object = vnode_pager_alloc(vp, filesize, 0, 0);
1414 * Dereference the reference we just created. This assumes
1415 * that the object is associated with the vp.
1417 object->ref_count--;
1420 if (object->flags & OBJ_DEAD) {
1422 vm_object_dead_sleep(object, "vodead");
1423 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1427 KASSERT(vp->v_object != NULL, ("vinitvmio: NULL object"));
1428 vp->v_flag |= VOBJBUF;
1434 * Print out a description of a vnode.
1436 static char *typename[] =
1437 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD"};
1440 vprint(char *label, struct vnode *vp)
1445 kprintf("%s: %p: ", label, (void *)vp);
1447 kprintf("%p: ", (void *)vp);
1448 kprintf("type %s, sysrefs %d, writecount %d, holdcnt %d,",
1449 typename[vp->v_type],
1450 vp->v_sysref.refcnt, vp->v_writecount, vp->v_auxrefs);
1452 if (vp->v_flag & VROOT)
1453 strcat(buf, "|VROOT");
1454 if (vp->v_flag & VPFSROOT)
1455 strcat(buf, "|VPFSROOT");
1456 if (vp->v_flag & VTEXT)
1457 strcat(buf, "|VTEXT");
1458 if (vp->v_flag & VSYSTEM)
1459 strcat(buf, "|VSYSTEM");
1460 if (vp->v_flag & VFREE)
1461 strcat(buf, "|VFREE");
1462 if (vp->v_flag & VOBJBUF)
1463 strcat(buf, "|VOBJBUF");
1465 kprintf(" flags (%s)", &buf[1]);
1466 if (vp->v_data == NULL) {
1475 #include <ddb/ddb.h>
1477 static int db_show_locked_vnodes(struct mount *mp, void *data);
1480 * List all of the locked vnodes in the system.
1481 * Called when debugging the kernel.
1483 DB_SHOW_COMMAND(lockedvnodes, lockedvnodes)
1485 kprintf("Locked vnodes\n");
1486 mountlist_scan(db_show_locked_vnodes, NULL,
1487 MNTSCAN_FORWARD|MNTSCAN_NOBUSY);
1491 db_show_locked_vnodes(struct mount *mp, void *data __unused)
1495 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
1496 if (vn_islocked(vp))
1504 * Top level filesystem related information gathering.
1506 static int sysctl_ovfs_conf (SYSCTL_HANDLER_ARGS);
1509 vfs_sysctl(SYSCTL_HANDLER_ARGS)
1511 int *name = (int *)arg1 - 1; /* XXX */
1512 u_int namelen = arg2 + 1; /* XXX */
1513 struct vfsconf *vfsp;
1516 #if 1 || defined(COMPAT_PRELITE2)
1517 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
1519 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
1523 /* all sysctl names at this level are at least name and field */
1525 return (ENOTDIR); /* overloaded */
1526 if (name[0] != VFS_GENERIC) {
1527 vfsp = vfsconf_find_by_typenum(name[0]);
1529 return (EOPNOTSUPP);
1530 return ((*vfsp->vfc_vfsops->vfs_sysctl)(&name[1], namelen - 1,
1531 oldp, oldlenp, newp, newlen, p));
1535 case VFS_MAXTYPENUM:
1538 maxtypenum = vfsconf_get_maxtypenum();
1539 return (SYSCTL_OUT(req, &maxtypenum, sizeof(maxtypenum)));
1542 return (ENOTDIR); /* overloaded */
1543 vfsp = vfsconf_find_by_typenum(name[2]);
1545 return (EOPNOTSUPP);
1546 return (SYSCTL_OUT(req, vfsp, sizeof *vfsp));
1548 return (EOPNOTSUPP);
1551 SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD, vfs_sysctl,
1552 "Generic filesystem");
1554 #if 1 || defined(COMPAT_PRELITE2)
1557 sysctl_ovfs_conf_iter(struct vfsconf *vfsp, void *data)
1560 struct ovfsconf ovfs;
1561 struct sysctl_req *req = (struct sysctl_req*) data;
1563 bzero(&ovfs, sizeof(ovfs));
1564 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
1565 strcpy(ovfs.vfc_name, vfsp->vfc_name);
1566 ovfs.vfc_index = vfsp->vfc_typenum;
1567 ovfs.vfc_refcount = vfsp->vfc_refcount;
1568 ovfs.vfc_flags = vfsp->vfc_flags;
1569 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
1571 return error; /* abort iteration with error code */
1573 return 0; /* continue iterating with next element */
1577 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
1579 return vfsconf_each(sysctl_ovfs_conf_iter, (void*)req);
1582 #endif /* 1 || COMPAT_PRELITE2 */
1585 * Check to see if a filesystem is mounted on a block device.
1588 vfs_mountedon(struct vnode *vp)
1592 if ((dev = vp->v_rdev) == NULL) {
1593 if (vp->v_type != VBLK)
1594 dev = get_dev(vp->v_uminor, vp->v_umajor);
1596 if (dev != NULL && dev->si_mountpoint)
1602 * Unmount all filesystems. The list is traversed in reverse order
1603 * of mounting to avoid dependencies.
1606 static int vfs_umountall_callback(struct mount *mp, void *data);
1609 vfs_unmountall(void)
1614 count = mountlist_scan(vfs_umountall_callback,
1615 NULL, MNTSCAN_REVERSE|MNTSCAN_NOBUSY);
1621 vfs_umountall_callback(struct mount *mp, void *data)
1625 error = dounmount(mp, MNT_FORCE);
1627 mountlist_remove(mp);
1628 kprintf("unmount of filesystem mounted from %s failed (",
1629 mp->mnt_stat.f_mntfromname);
1633 kprintf("%d)\n", error);
1639 * Build hash lists of net addresses and hang them off the mount point.
1640 * Called by ufs_mount() to set up the lists of export addresses.
1643 vfs_hang_addrlist(struct mount *mp, struct netexport *nep,
1644 const struct export_args *argp)
1647 struct radix_node_head *rnh;
1649 struct radix_node *rn;
1650 struct sockaddr *saddr, *smask = 0;
1654 if (argp->ex_addrlen == 0) {
1655 if (mp->mnt_flag & MNT_DEFEXPORTED)
1657 np = &nep->ne_defexported;
1658 np->netc_exflags = argp->ex_flags;
1659 np->netc_anon = argp->ex_anon;
1660 np->netc_anon.cr_ref = 1;
1661 mp->mnt_flag |= MNT_DEFEXPORTED;
1665 if (argp->ex_addrlen < 0 || argp->ex_addrlen > MLEN)
1667 if (argp->ex_masklen < 0 || argp->ex_masklen > MLEN)
1670 i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
1671 np = (struct netcred *) kmalloc(i, M_NETADDR, M_WAITOK | M_ZERO);
1672 saddr = (struct sockaddr *) (np + 1);
1673 if ((error = copyin(argp->ex_addr, (caddr_t) saddr, argp->ex_addrlen)))
1675 if (saddr->sa_len > argp->ex_addrlen)
1676 saddr->sa_len = argp->ex_addrlen;
1677 if (argp->ex_masklen) {
1678 smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen);
1679 error = copyin(argp->ex_mask, (caddr_t)smask, argp->ex_masklen);
1682 if (smask->sa_len > argp->ex_masklen)
1683 smask->sa_len = argp->ex_masklen;
1685 i = saddr->sa_family;
1686 if ((rnh = nep->ne_rtable[i]) == 0) {
1688 * Seems silly to initialize every AF when most are not used,
1689 * do so on demand here
1691 SLIST_FOREACH(dom, &domains, dom_next)
1692 if (dom->dom_family == i && dom->dom_rtattach) {
1693 dom->dom_rtattach((void **) &nep->ne_rtable[i],
1697 if ((rnh = nep->ne_rtable[i]) == 0) {
1702 rn = (*rnh->rnh_addaddr) ((char *) saddr, (char *) smask, rnh,
1704 if (rn == 0 || np != (struct netcred *) rn) { /* already exists */
1708 np->netc_exflags = argp->ex_flags;
1709 np->netc_anon = argp->ex_anon;
1710 np->netc_anon.cr_ref = 1;
1713 kfree(np, M_NETADDR);
1719 vfs_free_netcred(struct radix_node *rn, void *w)
1721 struct radix_node_head *rnh = (struct radix_node_head *) w;
1723 (*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, rnh);
1724 kfree((caddr_t) rn, M_NETADDR);
1729 * Free the net address hash lists that are hanging off the mount points.
1732 vfs_free_addrlist(struct netexport *nep)
1735 struct radix_node_head *rnh;
1737 for (i = 0; i <= AF_MAX; i++)
1738 if ((rnh = nep->ne_rtable[i])) {
1739 (*rnh->rnh_walktree) (rnh, vfs_free_netcred,
1741 kfree((caddr_t) rnh, M_RTABLE);
1742 nep->ne_rtable[i] = 0;
1747 vfs_export(struct mount *mp, struct netexport *nep,
1748 const struct export_args *argp)
1752 if (argp->ex_flags & MNT_DELEXPORT) {
1753 if (mp->mnt_flag & MNT_EXPUBLIC) {
1754 vfs_setpublicfs(NULL, NULL, NULL);
1755 mp->mnt_flag &= ~MNT_EXPUBLIC;
1757 vfs_free_addrlist(nep);
1758 mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
1760 if (argp->ex_flags & MNT_EXPORTED) {
1761 if (argp->ex_flags & MNT_EXPUBLIC) {
1762 if ((error = vfs_setpublicfs(mp, nep, argp)) != 0)
1764 mp->mnt_flag |= MNT_EXPUBLIC;
1766 if ((error = vfs_hang_addrlist(mp, nep, argp)))
1768 mp->mnt_flag |= MNT_EXPORTED;
1775 * Set the publicly exported filesystem (WebNFS). Currently, only
1776 * one public filesystem is possible in the spec (RFC 2054 and 2055)
1779 vfs_setpublicfs(struct mount *mp, struct netexport *nep,
1780 const struct export_args *argp)
1787 * mp == NULL -> invalidate the current info, the FS is
1788 * no longer exported. May be called from either vfs_export
1789 * or unmount, so check if it hasn't already been done.
1792 if (nfs_pub.np_valid) {
1793 nfs_pub.np_valid = 0;
1794 if (nfs_pub.np_index != NULL) {
1795 FREE(nfs_pub.np_index, M_TEMP);
1796 nfs_pub.np_index = NULL;
1803 * Only one allowed at a time.
1805 if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
1809 * Get real filehandle for root of exported FS.
1811 bzero((caddr_t)&nfs_pub.np_handle, sizeof(nfs_pub.np_handle));
1812 nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid;
1814 if ((error = VFS_ROOT(mp, &rvp)))
1817 if ((error = VFS_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid)))
1823 * If an indexfile was specified, pull it in.
1825 if (argp->ex_indexfile != NULL) {
1828 error = vn_get_namelen(rvp, &namelen);
1831 MALLOC(nfs_pub.np_index, char *, namelen, M_TEMP,
1833 error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
1837 * Check for illegal filenames.
1839 for (cp = nfs_pub.np_index; *cp; cp++) {
1847 FREE(nfs_pub.np_index, M_TEMP);
1852 nfs_pub.np_mount = mp;
1853 nfs_pub.np_valid = 1;
1858 vfs_export_lookup(struct mount *mp, struct netexport *nep,
1859 struct sockaddr *nam)
1862 struct radix_node_head *rnh;
1863 struct sockaddr *saddr;
1866 if (mp->mnt_flag & MNT_EXPORTED) {
1868 * Lookup in the export list first.
1872 rnh = nep->ne_rtable[saddr->sa_family];
1874 np = (struct netcred *)
1875 (*rnh->rnh_matchaddr)((char *)saddr,
1877 if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
1882 * If no address match, use the default if it exists.
1884 if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
1885 np = &nep->ne_defexported;
1891 * perform msync on all vnodes under a mount point. The mount point must
1892 * be locked. This code is also responsible for lazy-freeing unreferenced
1893 * vnodes whos VM objects no longer contain pages.
1895 * NOTE: MNT_WAIT still skips vnodes in the VXLOCK state.
1897 * NOTE: XXX VOP_PUTPAGES and friends requires that the vnode be locked,
1898 * but vnode_pager_putpages() doesn't lock the vnode. We have to do it
1899 * way up in this high level function.
1901 static int vfs_msync_scan1(struct mount *mp, struct vnode *vp, void *data);
1902 static int vfs_msync_scan2(struct mount *mp, struct vnode *vp, void *data);
1905 vfs_msync(struct mount *mp, int flags)
1909 vmsc_flags = VMSC_GETVP;
1910 if (flags != MNT_WAIT)
1911 vmsc_flags |= VMSC_NOWAIT;
1912 vmntvnodescan(mp, vmsc_flags, vfs_msync_scan1, vfs_msync_scan2,
1913 (void *)(intptr_t)flags);
1917 * scan1 is a fast pre-check. There could be hundreds of thousands of
1918 * vnodes, we cannot afford to do anything heavy weight until we have a
1919 * fairly good indication that there is work to do.
1923 vfs_msync_scan1(struct mount *mp, struct vnode *vp, void *data)
1925 int flags = (int)(intptr_t)data;
1927 if ((vp->v_flag & VRECLAIMED) == 0) {
1928 if (vshouldmsync(vp))
1929 return(0); /* call scan2 */
1930 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
1931 (vp->v_flag & VOBJDIRTY) &&
1932 (flags == MNT_WAIT || vn_islocked(vp) == 0)) {
1933 return(0); /* call scan2 */
1938 * do not call scan2, continue the loop
1944 * This callback is handed a locked vnode.
1948 vfs_msync_scan2(struct mount *mp, struct vnode *vp, void *data)
1951 int flags = (int)(intptr_t)data;
1953 if (vp->v_flag & VRECLAIMED)
1956 if ((mp->mnt_flag & MNT_RDONLY) == 0 && (vp->v_flag & VOBJDIRTY)) {
1957 if ((obj = vp->v_object) != NULL) {
1958 vm_object_page_clean(obj, 0, 0,
1959 flags == MNT_WAIT ? OBJPC_SYNC : OBJPC_NOSYNC);
1966 * Record a process's interest in events which might happen to
1967 * a vnode. Because poll uses the historic select-style interface
1968 * internally, this routine serves as both the ``check for any
1969 * pending events'' and the ``record my interest in future events''
1970 * functions. (These are done together, while the lock is held,
1971 * to avoid race conditions.)
1974 vn_pollrecord(struct vnode *vp, int events)
1978 KKASSERT(curthread->td_proc != NULL);
1980 lwkt_gettoken(&vlock, &vp->v_token);
1981 if (vp->v_pollinfo.vpi_revents & events) {
1983 * This leaves events we are not interested
1984 * in available for the other process which
1985 * which presumably had requested them
1986 * (otherwise they would never have been
1989 events &= vp->v_pollinfo.vpi_revents;
1990 vp->v_pollinfo.vpi_revents &= ~events;
1992 lwkt_reltoken(&vlock);
1995 vp->v_pollinfo.vpi_events |= events;
1996 selrecord(curthread, &vp->v_pollinfo.vpi_selinfo);
1997 lwkt_reltoken(&vlock);
2002 * Note the occurrence of an event. If the VN_POLLEVENT macro is used,
2003 * it is possible for us to miss an event due to race conditions, but
2004 * that condition is expected to be rare, so for the moment it is the
2005 * preferred interface.
2008 vn_pollevent(struct vnode *vp, int events)
2012 lwkt_gettoken(&vlock, &vp->v_token);
2013 if (vp->v_pollinfo.vpi_events & events) {
2015 * We clear vpi_events so that we don't
2016 * call selwakeup() twice if two events are
2017 * posted before the polling process(es) is
2018 * awakened. This also ensures that we take at
2019 * most one selwakeup() if the polling process
2020 * is no longer interested. However, it does
2021 * mean that only one event can be noticed at
2022 * a time. (Perhaps we should only clear those
2023 * event bits which we note?) XXX
2025 vp->v_pollinfo.vpi_events = 0; /* &= ~events ??? */
2026 vp->v_pollinfo.vpi_revents |= events;
2027 selwakeup(&vp->v_pollinfo.vpi_selinfo);
2029 lwkt_reltoken(&vlock);
2033 * Wake up anyone polling on vp because it is being revoked.
2034 * This depends on dead_poll() returning POLLHUP for correct
2038 vn_pollgone(struct vnode *vp)
2042 lwkt_gettoken(&vlock, &vp->v_token);
2043 if (vp->v_pollinfo.vpi_events) {
2044 vp->v_pollinfo.vpi_events = 0;
2045 selwakeup(&vp->v_pollinfo.vpi_selinfo);
2047 lwkt_reltoken(&vlock);
2051 * extract the cdev_t from a VBLK or VCHR. The vnode must have been opened
2052 * (or v_rdev might be NULL).
2055 vn_todev(struct vnode *vp)
2057 if (vp->v_type != VBLK && vp->v_type != VCHR)
2059 KKASSERT(vp->v_rdev != NULL);
2060 return (vp->v_rdev);
2064 * Check if vnode represents a disk device. The vnode does not need to be
2068 vn_isdisk(struct vnode *vp, int *errp)
2072 if (vp->v_type != VCHR) {
2078 if ((dev = vp->v_rdev) == NULL)
2079 dev = get_dev(vp->v_umajor, vp->v_uminor);
2086 if (dev_is_good(dev) == 0) {
2091 if ((dev_dflags(dev) & D_DISK) == 0) {
2102 vn_get_namelen(struct vnode *vp, int *namelen)
2105 register_t retval[2];
2107 error = VOP_PATHCONF(vp, _PC_NAME_MAX, retval);
2110 *namelen = (int)retval[0];
2115 vop_write_dirent(int *error, struct uio *uio, ino_t d_ino, uint8_t d_type,
2116 uint16_t d_namlen, const char *d_name)
2121 len = _DIRENT_RECLEN(d_namlen);
2122 if (len > uio->uio_resid)
2125 dp = kmalloc(len, M_TEMP, M_WAITOK | M_ZERO);
2128 dp->d_namlen = d_namlen;
2129 dp->d_type = d_type;
2130 bcopy(d_name, dp->d_name, d_namlen);
2132 *error = uiomove((caddr_t)dp, len, uio);
2140 vn_mark_atime(struct vnode *vp, struct thread *td)
2142 struct proc *p = td->td_proc;
2143 struct ucred *cred = p ? p->p_ucred : proc0.p_ucred;
2145 if ((vp->v_mount->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0) {
2146 VOP_MARKATIME(vp, cred);