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);
135 * Red black tree functions
137 static int rb_buf_compare(struct buf *b1, struct buf *b2);
138 RB_GENERATE2(buf_rb_tree, buf, b_rbnode, rb_buf_compare, off_t, b_loffset);
139 RB_GENERATE2(buf_rb_hash, buf, b_rbhash, rb_buf_compare, off_t, b_loffset);
142 rb_buf_compare(struct buf *b1, struct buf *b2)
144 if (b1->b_loffset < b2->b_loffset)
146 if (b1->b_loffset > b2->b_loffset)
152 * Returns non-zero if the vnode is a candidate for lazy msyncing.
155 vshouldmsync(struct vnode *vp)
157 if (vp->v_auxrefs != 0 || vp->v_sysref.refcnt > 0)
158 return (0); /* other holders */
160 (vp->v_object->ref_count || vp->v_object->resident_page_count)) {
167 * Initialize the vnode management data structures.
169 * Called from vfsinit()
175 * Desiredvnodes is kern.maxvnodes. We want to scale it
176 * according to available system memory but we may also have
177 * to limit it based on available KVM, which is capped on 32 bit
180 desiredvnodes = min(maxproc + vmstats.v_page_count / 4,
182 (sizeof(struct vm_object) + sizeof(struct vnode))));
184 lwkt_token_init(&spechash_token);
188 * Knob to control the precision of file timestamps:
190 * 0 = seconds only; nanoseconds zeroed.
191 * 1 = seconds and nanoseconds, accurate within 1/HZ.
192 * 2 = seconds and nanoseconds, truncated to microseconds.
193 * >=3 = seconds and nanoseconds, maximum precision.
195 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC };
197 static int timestamp_precision = TSP_SEC;
198 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW,
199 ×tamp_precision, 0, "");
202 * Get a current timestamp.
207 vfs_timestamp(struct timespec *tsp)
211 switch (timestamp_precision) {
213 tsp->tv_sec = time_second;
221 TIMEVAL_TO_TIMESPEC(&tv, tsp);
231 * Set vnode attributes to VNOVAL
234 vattr_null(struct vattr *vap)
237 vap->va_size = VNOVAL;
238 vap->va_bytes = VNOVAL;
239 vap->va_mode = VNOVAL;
240 vap->va_nlink = VNOVAL;
241 vap->va_uid = VNOVAL;
242 vap->va_gid = VNOVAL;
243 vap->va_fsid = VNOVAL;
244 vap->va_fileid = VNOVAL;
245 vap->va_blocksize = VNOVAL;
246 vap->va_rmajor = VNOVAL;
247 vap->va_rminor = VNOVAL;
248 vap->va_atime.tv_sec = VNOVAL;
249 vap->va_atime.tv_nsec = VNOVAL;
250 vap->va_mtime.tv_sec = VNOVAL;
251 vap->va_mtime.tv_nsec = VNOVAL;
252 vap->va_ctime.tv_sec = VNOVAL;
253 vap->va_ctime.tv_nsec = VNOVAL;
254 vap->va_flags = VNOVAL;
255 vap->va_gen = VNOVAL;
257 vap->va_fsmid = 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 vupdatefsmid(struct vnode *vp)
278 atomic_set_int(&vp->v_flag, VFSMID);
282 vinvalbuf(struct vnode *vp, int flags, int slpflag, int slptimeo)
284 struct vinvalbuf_bp_info info;
289 lwkt_gettoken(&vlock, &vp->v_token);
292 * If we are being asked to save, call fsync to ensure that the inode
295 if (flags & V_SAVE) {
296 error = bio_track_wait(&vp->v_track_write, slpflag, slptimeo);
299 if (!RB_EMPTY(&vp->v_rbdirty_tree)) {
300 if ((error = VOP_FSYNC(vp, MNT_WAIT)) != 0)
304 * Dirty bufs may be left or generated via races
305 * in circumstances where vinvalbuf() is called on
306 * a vnode not undergoing reclamation. Only
307 * panic if we are trying to reclaim the vnode.
309 if ((vp->v_flag & VRECLAIMED) &&
310 (bio_track_active(&vp->v_track_write) ||
311 !RB_EMPTY(&vp->v_rbdirty_tree))) {
312 panic("vinvalbuf: dirty bufs");
316 info.slptimeo = slptimeo;
317 info.lkflags = LK_EXCLUSIVE | LK_SLEEPFAIL;
318 if (slpflag & PCATCH)
319 info.lkflags |= LK_PCATCH;
324 * Flush the buffer cache until nothing is left.
326 while (!RB_EMPTY(&vp->v_rbclean_tree) ||
327 !RB_EMPTY(&vp->v_rbdirty_tree)) {
328 error = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree, NULL,
329 vinvalbuf_bp, &info);
331 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
332 vinvalbuf_bp, &info);
337 * Wait for I/O completion. We may block in the pip code so we have
341 bio_track_wait(&vp->v_track_write, 0, 0);
342 if ((object = vp->v_object) != NULL) {
343 while (object->paging_in_progress)
344 vm_object_pip_sleep(object, "vnvlbx");
346 } while (bio_track_active(&vp->v_track_write));
349 * Destroy the copy in the VM cache, too.
351 if ((object = vp->v_object) != NULL) {
352 vm_object_page_remove(object, 0, 0,
353 (flags & V_SAVE) ? TRUE : FALSE);
356 if (!RB_EMPTY(&vp->v_rbdirty_tree) || !RB_EMPTY(&vp->v_rbclean_tree))
357 panic("vinvalbuf: flush failed");
358 if (!RB_EMPTY(&vp->v_rbhash_tree))
359 panic("vinvalbuf: flush failed, buffers still present");
362 lwkt_reltoken(&vlock);
367 vinvalbuf_bp(struct buf *bp, void *data)
369 struct vinvalbuf_bp_info *info = data;
372 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
373 error = BUF_TIMELOCK(bp, info->lkflags,
374 "vinvalbuf", info->slptimeo);
384 KKASSERT(bp->b_vp == info->vp);
387 * XXX Since there are no node locks for NFS, I
388 * believe there is a slight chance that a delayed
389 * write will occur while sleeping just above, so
390 * check for it. Note that vfs_bio_awrite expects
391 * buffers to reside on a queue, while bwrite() and
394 * NOTE: NO B_LOCKED CHECK. Also no buf_checkwrite()
395 * check. This code will write out the buffer, period.
397 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
398 (info->flags & V_SAVE)) {
399 if (bp->b_vp == info->vp) {
400 if (bp->b_flags & B_CLUSTEROK) {
410 } else if (info->flags & V_SAVE) {
412 * Cannot set B_NOCACHE on a clean buffer as this will
413 * destroy the VM backing store which might actually
414 * be dirty (and unsynchronized).
417 bp->b_flags |= (B_INVAL | B_RELBUF);
421 bp->b_flags |= (B_INVAL | B_NOCACHE | B_RELBUF);
428 * Truncate a file's buffer and pages to a specified length. This
429 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
432 * The vnode must be locked.
434 static int vtruncbuf_bp_trunc_cmp(struct buf *bp, void *data);
435 static int vtruncbuf_bp_trunc(struct buf *bp, void *data);
436 static int vtruncbuf_bp_metasync_cmp(struct buf *bp, void *data);
437 static int vtruncbuf_bp_metasync(struct buf *bp, void *data);
440 vtruncbuf(struct vnode *vp, off_t length, int blksize)
443 const char *filename;
448 * Round up to the *next* block, then destroy the buffers in question.
449 * Since we are only removing some of the buffers we must rely on the
450 * scan count to determine whether a loop is necessary.
452 if ((count = (int)(length % blksize)) != 0)
453 truncloffset = length + (blksize - count);
455 truncloffset = length;
457 lwkt_gettoken(&vlock, &vp->v_token);
459 count = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree,
460 vtruncbuf_bp_trunc_cmp,
461 vtruncbuf_bp_trunc, &truncloffset);
462 count += RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
463 vtruncbuf_bp_trunc_cmp,
464 vtruncbuf_bp_trunc, &truncloffset);
468 * For safety, fsync any remaining metadata if the file is not being
469 * truncated to 0. Since the metadata does not represent the entire
470 * dirty list we have to rely on the hit count to ensure that we get
475 count = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
476 vtruncbuf_bp_metasync_cmp,
477 vtruncbuf_bp_metasync, vp);
482 * Clean out any left over VM backing store.
484 * It is possible to have in-progress I/O from buffers that were
485 * not part of the truncation. This should not happen if we
486 * are truncating to 0-length.
488 vnode_pager_setsize(vp, length);
489 bio_track_wait(&vp->v_track_write, 0, 0);
491 filename = TAILQ_FIRST(&vp->v_namecache) ?
492 TAILQ_FIRST(&vp->v_namecache)->nc_name : "?";
495 * Make sure no buffers were instantiated while we were trying
496 * to clean out the remaining VM pages. This could occur due
497 * to busy dirty VM pages being flushed out to disk.
500 count = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree,
501 vtruncbuf_bp_trunc_cmp,
502 vtruncbuf_bp_trunc, &truncloffset);
503 count += RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
504 vtruncbuf_bp_trunc_cmp,
505 vtruncbuf_bp_trunc, &truncloffset);
507 kprintf("Warning: vtruncbuf(): Had to re-clean %d "
508 "left over buffers in %s\n", count, filename);
512 lwkt_reltoken(&vlock);
518 * The callback buffer is beyond the new file EOF and must be destroyed.
519 * Note that the compare function must conform to the RB_SCAN's requirements.
523 vtruncbuf_bp_trunc_cmp(struct buf *bp, void *data)
525 if (bp->b_loffset >= *(off_t *)data)
532 vtruncbuf_bp_trunc(struct buf *bp, void *data)
535 * Do not try to use a buffer we cannot immediately lock, but sleep
536 * anyway to prevent a livelock. The code will loop until all buffers
539 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
540 if (BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL) == 0)
544 bp->b_flags |= (B_INVAL | B_RELBUF | B_NOCACHE);
551 * Fsync all meta-data after truncating a file to be non-zero. Only metadata
552 * blocks (with a negative loffset) are scanned.
553 * Note that the compare function must conform to the RB_SCAN's requirements.
556 vtruncbuf_bp_metasync_cmp(struct buf *bp, void *data)
558 if (bp->b_loffset < 0)
564 vtruncbuf_bp_metasync(struct buf *bp, void *data)
566 struct vnode *vp = data;
568 if (bp->b_flags & B_DELWRI) {
570 * Do not try to use a buffer we cannot immediately lock,
571 * but sleep anyway to prevent a livelock. The code will
572 * loop until all buffers can be acted upon.
574 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
575 if (BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL) == 0)
591 * vfsync - implements a multipass fsync on a file which understands
592 * dependancies and meta-data. The passed vnode must be locked. The
593 * waitfor argument may be MNT_WAIT or MNT_NOWAIT, or MNT_LAZY.
595 * When fsyncing data asynchronously just do one consolidated pass starting
596 * with the most negative block number. This may not get all the data due
599 * When fsyncing data synchronously do a data pass, then a metadata pass,
600 * then do additional data+metadata passes to try to get all the data out.
602 static int vfsync_wait_output(struct vnode *vp,
603 int (*waitoutput)(struct vnode *, struct thread *));
604 static int vfsync_data_only_cmp(struct buf *bp, void *data);
605 static int vfsync_meta_only_cmp(struct buf *bp, void *data);
606 static int vfsync_lazy_range_cmp(struct buf *bp, void *data);
607 static int vfsync_bp(struct buf *bp, void *data);
616 int (*checkdef)(struct buf *);
620 vfsync(struct vnode *vp, int waitfor, int passes,
621 int (*checkdef)(struct buf *),
622 int (*waitoutput)(struct vnode *, struct thread *))
624 struct vfsync_info info;
628 bzero(&info, sizeof(info));
630 if ((info.checkdef = checkdef) == NULL)
633 lwkt_gettoken(&vlock, &vp->v_token);
638 * Lazy (filesystem syncer typ) Asynchronous plus limit the
639 * number of data (not meta) pages we try to flush to 1MB.
640 * A non-zero return means that lazy limit was reached.
642 info.lazylimit = 1024 * 1024;
644 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
645 vfsync_lazy_range_cmp, vfsync_bp, &info);
646 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
647 vfsync_meta_only_cmp, vfsync_bp, &info);
650 else if (!RB_EMPTY(&vp->v_rbdirty_tree))
651 vn_syncer_add_to_worklist(vp, 1);
656 * Asynchronous. Do a data-only pass and a meta-only pass.
659 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_data_only_cmp,
661 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_meta_only_cmp,
667 * Synchronous. Do a data-only pass, then a meta-data+data
668 * pass, then additional integrated passes to try to get
669 * all the dependancies flushed.
671 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_data_only_cmp,
673 error = vfsync_wait_output(vp, waitoutput);
675 info.skippedbufs = 0;
676 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
678 error = vfsync_wait_output(vp, waitoutput);
679 if (info.skippedbufs)
680 kprintf("Warning: vfsync skipped %d dirty bufs in pass2!\n", info.skippedbufs);
682 while (error == 0 && passes > 0 &&
683 !RB_EMPTY(&vp->v_rbdirty_tree)
686 info.synchronous = 1;
689 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
695 error = vfsync_wait_output(vp, waitoutput);
699 lwkt_reltoken(&vlock);
704 vfsync_wait_output(struct vnode *vp,
705 int (*waitoutput)(struct vnode *, struct thread *))
709 error = bio_track_wait(&vp->v_track_write, 0, 0);
711 error = waitoutput(vp, curthread);
716 vfsync_data_only_cmp(struct buf *bp, void *data)
718 if (bp->b_loffset < 0)
724 vfsync_meta_only_cmp(struct buf *bp, void *data)
726 if (bp->b_loffset < 0)
732 vfsync_lazy_range_cmp(struct buf *bp, void *data)
734 struct vfsync_info *info = data;
735 if (bp->b_loffset < info->vp->v_lazyw)
741 vfsync_bp(struct buf *bp, void *data)
743 struct vfsync_info *info = data;
744 struct vnode *vp = info->vp;
748 * if syncdeps is not set we do not try to write buffers which have
751 if (!info->synchronous && info->syncdeps == 0 && info->checkdef(bp))
755 * Ignore buffers that we cannot immediately lock. XXX
757 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
758 kprintf("Warning: vfsync_bp skipping dirty buffer %p\n", bp);
762 if ((bp->b_flags & B_DELWRI) == 0)
763 panic("vfsync_bp: buffer not dirty");
765 panic("vfsync_bp: buffer vp mismatch");
768 * B_NEEDCOMMIT (primarily used by NFS) is a state where the buffer
769 * has been written but an additional handshake with the device
770 * is required before we can dispose of the buffer. We have no idea
771 * how to do this so we have to skip these buffers.
773 if (bp->b_flags & B_NEEDCOMMIT) {
779 * Ask bioops if it is ok to sync
781 if (LIST_FIRST(&bp->b_dep) != NULL && buf_checkwrite(bp)) {
787 if (info->synchronous) {
789 * Synchronous flushing. An error may be returned.
795 * Asynchronous flushing. A negative return value simply
796 * stops the scan and is not considered an error. We use
797 * this to support limited MNT_LAZY flushes.
799 vp->v_lazyw = bp->b_loffset;
800 if ((vp->v_flag & VOBJBUF) && (bp->b_flags & B_CLUSTEROK)) {
801 info->lazycount += vfs_bio_awrite(bp);
803 info->lazycount += bp->b_bufsize;
807 if (info->lazylimit && info->lazycount >= info->lazylimit)
816 * Associate a buffer with a vnode.
821 bgetvp(struct vnode *vp, struct buf *bp)
825 KASSERT(bp->b_vp == NULL, ("bgetvp: not free"));
826 KKASSERT((bp->b_flags & (B_HASHED|B_DELWRI|B_VNCLEAN|B_VNDIRTY)) == 0);
829 * Insert onto list for new vnode.
831 lwkt_gettoken(&vlock, &vp->v_token);
832 if (buf_rb_hash_RB_INSERT(&vp->v_rbhash_tree, bp)) {
833 lwkt_reltoken(&vlock);
837 bp->b_flags |= B_HASHED;
838 bp->b_flags |= B_VNCLEAN;
839 if (buf_rb_tree_RB_INSERT(&vp->v_rbclean_tree, bp))
840 panic("reassignbuf: dup lblk/clean vp %p bp %p", vp, bp);
842 lwkt_reltoken(&vlock);
847 * Disassociate a buffer from a vnode.
850 brelvp(struct buf *bp)
855 KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
858 * Delete from old vnode list, if on one.
861 lwkt_gettoken(&vlock, &vp->v_token);
862 if (bp->b_flags & (B_VNDIRTY | B_VNCLEAN)) {
863 if (bp->b_flags & B_VNDIRTY)
864 buf_rb_tree_RB_REMOVE(&vp->v_rbdirty_tree, bp);
866 buf_rb_tree_RB_REMOVE(&vp->v_rbclean_tree, bp);
867 bp->b_flags &= ~(B_VNDIRTY | B_VNCLEAN);
869 if (bp->b_flags & B_HASHED) {
870 buf_rb_hash_RB_REMOVE(&vp->v_rbhash_tree, bp);
871 bp->b_flags &= ~B_HASHED;
873 if ((vp->v_flag & VONWORKLST) && RB_EMPTY(&vp->v_rbdirty_tree)) {
874 vp->v_flag &= ~VONWORKLST;
875 LIST_REMOVE(vp, v_synclist);
878 lwkt_reltoken(&vlock);
884 * Reassign the buffer to the proper clean/dirty list based on B_DELWRI.
885 * This routine is called when the state of the B_DELWRI bit is changed.
890 reassignbuf(struct buf *bp)
892 struct vnode *vp = bp->b_vp;
896 KKASSERT(vp != NULL);
900 * B_PAGING flagged buffers cannot be reassigned because their vp
901 * is not fully linked in.
903 if (bp->b_flags & B_PAGING)
904 panic("cannot reassign paging buffer");
906 lwkt_gettoken(&vlock, &vp->v_token);
907 if (bp->b_flags & B_DELWRI) {
909 * Move to the dirty list, add the vnode to the worklist
911 if (bp->b_flags & B_VNCLEAN) {
912 buf_rb_tree_RB_REMOVE(&vp->v_rbclean_tree, bp);
913 bp->b_flags &= ~B_VNCLEAN;
915 if ((bp->b_flags & B_VNDIRTY) == 0) {
916 if (buf_rb_tree_RB_INSERT(&vp->v_rbdirty_tree, bp)) {
917 panic("reassignbuf: dup lblk vp %p bp %p",
920 bp->b_flags |= B_VNDIRTY;
922 if ((vp->v_flag & VONWORKLST) == 0) {
923 switch (vp->v_type) {
930 vp->v_rdev->si_mountpoint != NULL) {
938 vn_syncer_add_to_worklist(vp, delay);
942 * Move to the clean list, remove the vnode from the worklist
943 * if no dirty blocks remain.
945 if (bp->b_flags & B_VNDIRTY) {
946 buf_rb_tree_RB_REMOVE(&vp->v_rbdirty_tree, bp);
947 bp->b_flags &= ~B_VNDIRTY;
949 if ((bp->b_flags & B_VNCLEAN) == 0) {
950 if (buf_rb_tree_RB_INSERT(&vp->v_rbclean_tree, bp)) {
951 panic("reassignbuf: dup lblk vp %p bp %p",
954 bp->b_flags |= B_VNCLEAN;
956 if ((vp->v_flag & VONWORKLST) &&
957 RB_EMPTY(&vp->v_rbdirty_tree)) {
958 vp->v_flag &= ~VONWORKLST;
959 LIST_REMOVE(vp, v_synclist);
962 lwkt_reltoken(&vlock);
966 * Create a vnode for a block device.
967 * Used for mounting the root file system.
969 extern struct vop_ops *devfs_vnode_dev_vops_p;
971 bdevvp(cdev_t dev, struct vnode **vpp)
981 error = getspecialvnode(VT_NON, NULL, &devfs_vnode_dev_vops_p,
990 vp->v_umajor = dev->si_umajor;
991 vp->v_uminor = dev->si_uminor;
998 v_associate_rdev(struct vnode *vp, cdev_t dev)
1004 if (dev_is_good(dev) == 0)
1006 KKASSERT(vp->v_rdev == NULL);
1007 vp->v_rdev = reference_dev(dev);
1008 lwkt_gettoken(&ilock, &spechash_token);
1009 SLIST_INSERT_HEAD(&dev->si_hlist, vp, v_cdevnext);
1010 lwkt_reltoken(&ilock);
1015 v_release_rdev(struct vnode *vp)
1020 if ((dev = vp->v_rdev) != NULL) {
1021 lwkt_gettoken(&ilock, &spechash_token);
1022 SLIST_REMOVE(&dev->si_hlist, vp, vnode, v_cdevnext);
1025 lwkt_reltoken(&ilock);
1030 * Add a vnode to the alias list hung off the cdev_t. We only associate
1031 * the device number with the vnode. The actual device is not associated
1032 * until the vnode is opened (usually in spec_open()), and will be
1033 * disassociated on last close.
1036 addaliasu(struct vnode *nvp, int x, int y)
1038 if (nvp->v_type != VBLK && nvp->v_type != VCHR)
1039 panic("addaliasu on non-special vnode");
1045 * Simple call that a filesystem can make to try to get rid of a
1046 * vnode. It will fail if anyone is referencing the vnode (including
1049 * The filesystem can check whether its in-memory inode structure still
1050 * references the vp on return.
1053 vclean_unlocked(struct vnode *vp)
1056 if (sysref_isactive(&vp->v_sysref) == 0)
1062 * Disassociate a vnode from its underlying filesystem.
1064 * The vnode must be VX locked and referenced. In all normal situations
1065 * there are no active references. If vclean_vxlocked() is called while
1066 * there are active references, the vnode is being ripped out and we have
1067 * to call VOP_CLOSE() as appropriate before we can reclaim it.
1070 vclean_vxlocked(struct vnode *vp, int flags)
1077 * If the vnode has already been reclaimed we have nothing to do.
1079 if (vp->v_flag & VRECLAIMED)
1081 vp->v_flag |= VRECLAIMED;
1084 * Scrap the vfs cache
1086 while (cache_inval_vp(vp, 0) != 0) {
1087 kprintf("Warning: vnode %p clean/cache_resolution race detected\n", vp);
1088 tsleep(vp, 0, "vclninv", 2);
1092 * Check to see if the vnode is in use. If so we have to reference it
1093 * before we clean it out so that its count cannot fall to zero and
1094 * generate a race against ourselves to recycle it.
1096 active = sysref_isactive(&vp->v_sysref);
1099 * Clean out any buffers associated with the vnode and destroy its
1100 * object, if it has one.
1102 vinvalbuf(vp, V_SAVE, 0, 0);
1105 * If purging an active vnode (typically during a forced unmount
1106 * or reboot), it must be closed and deactivated before being
1107 * reclaimed. This isn't really all that safe, but what can
1110 * Note that neither of these routines unlocks the vnode.
1112 if (active && (flags & DOCLOSE)) {
1113 while ((n = vp->v_opencount) != 0) {
1114 if (vp->v_writecount)
1115 VOP_CLOSE(vp, FWRITE|FNONBLOCK);
1117 VOP_CLOSE(vp, FNONBLOCK);
1118 if (vp->v_opencount == n) {
1119 kprintf("Warning: unable to force-close"
1127 * If the vnode has not been deactivated, deactivated it. Deactivation
1128 * can create new buffers and VM pages so we have to call vinvalbuf()
1129 * again to make sure they all get flushed.
1131 * This can occur if a file with a link count of 0 needs to be
1134 if ((vp->v_flag & VINACTIVE) == 0) {
1135 vp->v_flag |= VINACTIVE;
1137 vinvalbuf(vp, V_SAVE, 0, 0);
1141 * If the vnode has an object, destroy it.
1143 if ((object = vp->v_object) != NULL) {
1144 if (object->ref_count == 0) {
1145 if ((object->flags & OBJ_DEAD) == 0)
1146 vm_object_terminate(object);
1148 vm_pager_deallocate(object);
1150 vp->v_flag &= ~VOBJBUF;
1152 KKASSERT((vp->v_flag & VOBJBUF) == 0);
1155 * Reclaim the vnode.
1157 if (VOP_RECLAIM(vp))
1158 panic("vclean: cannot reclaim");
1161 * Done with purge, notify sleepers of the grim news.
1163 vp->v_ops = &dead_vnode_vops_p;
1168 * If we are destroying an active vnode, reactivate it now that
1169 * we have reassociated it with deadfs. This prevents the system
1170 * from crashing on the vnode due to it being unexpectedly marked
1171 * as inactive or reclaimed.
1173 if (active && (flags & DOCLOSE)) {
1174 vp->v_flag &= ~(VINACTIVE|VRECLAIMED);
1179 * Eliminate all activity associated with the requested vnode
1180 * and with all vnodes aliased to the requested vnode.
1182 * The vnode must be referenced but should not be locked.
1185 vrevoke(struct vnode *vp, struct ucred *cred)
1194 * If the vnode has a device association, scrap all vnodes associated
1195 * with the device. Don't let the device disappear on us while we
1196 * are scrapping the vnodes.
1198 * The passed vp will probably show up in the list, do not VX lock
1201 * Releasing the vnode's rdev here can mess up specfs's call to
1202 * device close, so don't do it. The vnode has been disassociated
1203 * and the device will be closed after the last ref on the related
1204 * fp goes away (if not still open by e.g. the kernel).
1206 if (vp->v_type != VCHR) {
1207 error = fdrevoke(vp, DTYPE_VNODE, cred);
1210 if ((dev = vp->v_rdev) == NULL) {
1214 lwkt_gettoken(&ilock, &spechash_token);
1216 vqn = SLIST_FIRST(&dev->si_hlist);
1219 while ((vq = vqn) != NULL) {
1220 vqn = SLIST_NEXT(vqn, v_cdevnext);
1223 fdrevoke(vq, DTYPE_VNODE, cred);
1224 /*v_release_rdev(vq);*/
1227 lwkt_reltoken(&ilock);
1234 * This is called when the object underlying a vnode is being destroyed,
1235 * such as in a remove(). Try to recycle the vnode immediately if the
1236 * only active reference is our reference.
1238 * Directory vnodes in the namecache with children cannot be immediately
1239 * recycled because numerous VOP_N*() ops require them to be stable.
1242 vrecycle(struct vnode *vp)
1244 if (vp->v_sysref.refcnt <= 1) {
1245 if (cache_inval_vp_nonblock(vp))
1254 * Return the maximum I/O size allowed for strategy calls on VP.
1256 * If vp is VCHR or VBLK we dive the device, otherwise we use
1257 * the vp's mount info.
1260 vmaxiosize(struct vnode *vp)
1262 if (vp->v_type == VBLK || vp->v_type == VCHR) {
1263 return(vp->v_rdev->si_iosize_max);
1265 return(vp->v_mount->mnt_iosize_max);
1270 * Eliminate all activity associated with a vnode in preparation for reuse.
1272 * The vnode must be VX locked and refd and will remain VX locked and refd
1273 * on return. This routine may be called with the vnode in any state, as
1274 * long as it is VX locked. The vnode will be cleaned out and marked
1275 * VRECLAIMED but will not actually be reused until all existing refs and
1278 * NOTE: This routine may be called on a vnode which has not yet been
1279 * already been deactivated (VOP_INACTIVE), or on a vnode which has
1280 * already been reclaimed.
1282 * This routine is not responsible for placing us back on the freelist.
1283 * Instead, it happens automatically when the caller releases the VX lock
1284 * (assuming there aren't any other references).
1288 vgone_vxlocked(struct vnode *vp)
1291 * assert that the VX lock is held. This is an absolute requirement
1292 * now for vgone_vxlocked() to be called.
1294 KKASSERT(vp->v_lock.lk_exclusivecount == 1);
1297 * Clean out the filesystem specific data and set the VRECLAIMED
1298 * bit. Also deactivate the vnode if necessary.
1300 vclean_vxlocked(vp, DOCLOSE);
1303 * Delete from old mount point vnode list, if on one.
1305 if (vp->v_mount != NULL)
1306 insmntque(vp, NULL);
1309 * If special device, remove it from special device alias list
1310 * if it is on one. This should normally only occur if a vnode is
1311 * being revoked as the device should otherwise have been released
1314 if ((vp->v_type == VBLK || vp->v_type == VCHR) && vp->v_rdev != NULL) {
1325 * Lookup a vnode by device number.
1327 * Returns non-zero and *vpp set to a vref'd vnode on success.
1328 * Returns zero on failure.
1331 vfinddev(cdev_t dev, enum vtype type, struct vnode **vpp)
1336 lwkt_gettoken(&ilock, &spechash_token);
1337 SLIST_FOREACH(vp, &dev->si_hlist, v_cdevnext) {
1338 if (type == vp->v_type) {
1341 lwkt_reltoken(&ilock);
1345 lwkt_reltoken(&ilock);
1350 * Calculate the total number of references to a special device. This
1351 * routine may only be called for VBLK and VCHR vnodes since v_rdev is
1352 * an overloaded field. Since udev2dev can now return NULL, we have
1353 * to check for a NULL v_rdev.
1356 count_dev(cdev_t dev)
1362 if (SLIST_FIRST(&dev->si_hlist)) {
1363 lwkt_gettoken(&ilock, &spechash_token);
1364 SLIST_FOREACH(vp, &dev->si_hlist, v_cdevnext) {
1365 if (vp->v_sysref.refcnt > 0)
1366 count += vp->v_sysref.refcnt;
1368 lwkt_reltoken(&ilock);
1374 vcount(struct vnode *vp)
1376 if (vp->v_rdev == NULL)
1378 return(count_dev(vp->v_rdev));
1382 * Initialize VMIO for a vnode. This routine MUST be called before a
1383 * VFS can issue buffer cache ops on a vnode. It is typically called
1384 * when a vnode is initialized from its inode.
1387 vinitvmio(struct vnode *vp, off_t filesize)
1393 if ((object = vp->v_object) == NULL) {
1394 object = vnode_pager_alloc(vp, filesize, 0, 0);
1396 * Dereference the reference we just created. This assumes
1397 * that the object is associated with the vp.
1399 object->ref_count--;
1402 if (object->flags & OBJ_DEAD) {
1404 vm_object_dead_sleep(object, "vodead");
1405 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1409 KASSERT(vp->v_object != NULL, ("vinitvmio: NULL object"));
1410 vp->v_flag |= VOBJBUF;
1416 * Print out a description of a vnode.
1418 static char *typename[] =
1419 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD"};
1422 vprint(char *label, struct vnode *vp)
1427 kprintf("%s: %p: ", label, (void *)vp);
1429 kprintf("%p: ", (void *)vp);
1430 kprintf("type %s, sysrefs %d, writecount %d, holdcnt %d,",
1431 typename[vp->v_type],
1432 vp->v_sysref.refcnt, vp->v_writecount, vp->v_auxrefs);
1434 if (vp->v_flag & VROOT)
1435 strcat(buf, "|VROOT");
1436 if (vp->v_flag & VPFSROOT)
1437 strcat(buf, "|VPFSROOT");
1438 if (vp->v_flag & VTEXT)
1439 strcat(buf, "|VTEXT");
1440 if (vp->v_flag & VSYSTEM)
1441 strcat(buf, "|VSYSTEM");
1442 if (vp->v_flag & VFREE)
1443 strcat(buf, "|VFREE");
1444 if (vp->v_flag & VOBJBUF)
1445 strcat(buf, "|VOBJBUF");
1447 kprintf(" flags (%s)", &buf[1]);
1448 if (vp->v_data == NULL) {
1457 #include <ddb/ddb.h>
1459 static int db_show_locked_vnodes(struct mount *mp, void *data);
1462 * List all of the locked vnodes in the system.
1463 * Called when debugging the kernel.
1465 DB_SHOW_COMMAND(lockedvnodes, lockedvnodes)
1467 kprintf("Locked vnodes\n");
1468 mountlist_scan(db_show_locked_vnodes, NULL,
1469 MNTSCAN_FORWARD|MNTSCAN_NOBUSY);
1473 db_show_locked_vnodes(struct mount *mp, void *data __unused)
1477 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
1478 if (vn_islocked(vp))
1486 * Top level filesystem related information gathering.
1488 static int sysctl_ovfs_conf (SYSCTL_HANDLER_ARGS);
1491 vfs_sysctl(SYSCTL_HANDLER_ARGS)
1493 int *name = (int *)arg1 - 1; /* XXX */
1494 u_int namelen = arg2 + 1; /* XXX */
1495 struct vfsconf *vfsp;
1498 #if 1 || defined(COMPAT_PRELITE2)
1499 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
1501 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
1505 /* all sysctl names at this level are at least name and field */
1507 return (ENOTDIR); /* overloaded */
1508 if (name[0] != VFS_GENERIC) {
1509 vfsp = vfsconf_find_by_typenum(name[0]);
1511 return (EOPNOTSUPP);
1512 return ((*vfsp->vfc_vfsops->vfs_sysctl)(&name[1], namelen - 1,
1513 oldp, oldlenp, newp, newlen, p));
1517 case VFS_MAXTYPENUM:
1520 maxtypenum = vfsconf_get_maxtypenum();
1521 return (SYSCTL_OUT(req, &maxtypenum, sizeof(maxtypenum)));
1524 return (ENOTDIR); /* overloaded */
1525 vfsp = vfsconf_find_by_typenum(name[2]);
1527 return (EOPNOTSUPP);
1528 return (SYSCTL_OUT(req, vfsp, sizeof *vfsp));
1530 return (EOPNOTSUPP);
1533 SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD, vfs_sysctl,
1534 "Generic filesystem");
1536 #if 1 || defined(COMPAT_PRELITE2)
1539 sysctl_ovfs_conf_iter(struct vfsconf *vfsp, void *data)
1542 struct ovfsconf ovfs;
1543 struct sysctl_req *req = (struct sysctl_req*) data;
1545 bzero(&ovfs, sizeof(ovfs));
1546 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
1547 strcpy(ovfs.vfc_name, vfsp->vfc_name);
1548 ovfs.vfc_index = vfsp->vfc_typenum;
1549 ovfs.vfc_refcount = vfsp->vfc_refcount;
1550 ovfs.vfc_flags = vfsp->vfc_flags;
1551 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
1553 return error; /* abort iteration with error code */
1555 return 0; /* continue iterating with next element */
1559 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
1561 return vfsconf_each(sysctl_ovfs_conf_iter, (void*)req);
1564 #endif /* 1 || COMPAT_PRELITE2 */
1567 * Check to see if a filesystem is mounted on a block device.
1570 vfs_mountedon(struct vnode *vp)
1574 if ((dev = vp->v_rdev) == NULL) {
1575 /* if (vp->v_type != VBLK)
1576 dev = get_dev(vp->v_uminor, vp->v_umajor); */
1578 if (dev != NULL && dev->si_mountpoint)
1584 * Unmount all filesystems. The list is traversed in reverse order
1585 * of mounting to avoid dependencies.
1588 static int vfs_umountall_callback(struct mount *mp, void *data);
1591 vfs_unmountall(void)
1596 count = mountlist_scan(vfs_umountall_callback,
1597 NULL, MNTSCAN_REVERSE|MNTSCAN_NOBUSY);
1603 vfs_umountall_callback(struct mount *mp, void *data)
1607 error = dounmount(mp, MNT_FORCE);
1609 mountlist_remove(mp);
1610 kprintf("unmount of filesystem mounted from %s failed (",
1611 mp->mnt_stat.f_mntfromname);
1615 kprintf("%d)\n", error);
1621 * Build hash lists of net addresses and hang them off the mount point.
1622 * Called by ufs_mount() to set up the lists of export addresses.
1625 vfs_hang_addrlist(struct mount *mp, struct netexport *nep,
1626 const struct export_args *argp)
1629 struct radix_node_head *rnh;
1631 struct radix_node *rn;
1632 struct sockaddr *saddr, *smask = 0;
1636 if (argp->ex_addrlen == 0) {
1637 if (mp->mnt_flag & MNT_DEFEXPORTED)
1639 np = &nep->ne_defexported;
1640 np->netc_exflags = argp->ex_flags;
1641 np->netc_anon = argp->ex_anon;
1642 np->netc_anon.cr_ref = 1;
1643 mp->mnt_flag |= MNT_DEFEXPORTED;
1647 if (argp->ex_addrlen < 0 || argp->ex_addrlen > MLEN)
1649 if (argp->ex_masklen < 0 || argp->ex_masklen > MLEN)
1652 i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
1653 np = (struct netcred *) kmalloc(i, M_NETADDR, M_WAITOK | M_ZERO);
1654 saddr = (struct sockaddr *) (np + 1);
1655 if ((error = copyin(argp->ex_addr, (caddr_t) saddr, argp->ex_addrlen)))
1657 if (saddr->sa_len > argp->ex_addrlen)
1658 saddr->sa_len = argp->ex_addrlen;
1659 if (argp->ex_masklen) {
1660 smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen);
1661 error = copyin(argp->ex_mask, (caddr_t)smask, argp->ex_masklen);
1664 if (smask->sa_len > argp->ex_masklen)
1665 smask->sa_len = argp->ex_masklen;
1667 i = saddr->sa_family;
1668 if ((rnh = nep->ne_rtable[i]) == 0) {
1670 * Seems silly to initialize every AF when most are not used,
1671 * do so on demand here
1673 SLIST_FOREACH(dom, &domains, dom_next)
1674 if (dom->dom_family == i && dom->dom_rtattach) {
1675 dom->dom_rtattach((void **) &nep->ne_rtable[i],
1679 if ((rnh = nep->ne_rtable[i]) == 0) {
1684 rn = (*rnh->rnh_addaddr) ((char *) saddr, (char *) smask, rnh,
1686 if (rn == 0 || np != (struct netcred *) rn) { /* already exists */
1690 np->netc_exflags = argp->ex_flags;
1691 np->netc_anon = argp->ex_anon;
1692 np->netc_anon.cr_ref = 1;
1695 kfree(np, M_NETADDR);
1701 vfs_free_netcred(struct radix_node *rn, void *w)
1703 struct radix_node_head *rnh = (struct radix_node_head *) w;
1705 (*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, rnh);
1706 kfree((caddr_t) rn, M_NETADDR);
1711 * Free the net address hash lists that are hanging off the mount points.
1714 vfs_free_addrlist(struct netexport *nep)
1717 struct radix_node_head *rnh;
1719 for (i = 0; i <= AF_MAX; i++)
1720 if ((rnh = nep->ne_rtable[i])) {
1721 (*rnh->rnh_walktree) (rnh, vfs_free_netcred,
1723 kfree((caddr_t) rnh, M_RTABLE);
1724 nep->ne_rtable[i] = 0;
1729 vfs_export(struct mount *mp, struct netexport *nep,
1730 const struct export_args *argp)
1734 if (argp->ex_flags & MNT_DELEXPORT) {
1735 if (mp->mnt_flag & MNT_EXPUBLIC) {
1736 vfs_setpublicfs(NULL, NULL, NULL);
1737 mp->mnt_flag &= ~MNT_EXPUBLIC;
1739 vfs_free_addrlist(nep);
1740 mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
1742 if (argp->ex_flags & MNT_EXPORTED) {
1743 if (argp->ex_flags & MNT_EXPUBLIC) {
1744 if ((error = vfs_setpublicfs(mp, nep, argp)) != 0)
1746 mp->mnt_flag |= MNT_EXPUBLIC;
1748 if ((error = vfs_hang_addrlist(mp, nep, argp)))
1750 mp->mnt_flag |= MNT_EXPORTED;
1757 * Set the publicly exported filesystem (WebNFS). Currently, only
1758 * one public filesystem is possible in the spec (RFC 2054 and 2055)
1761 vfs_setpublicfs(struct mount *mp, struct netexport *nep,
1762 const struct export_args *argp)
1769 * mp == NULL -> invalidate the current info, the FS is
1770 * no longer exported. May be called from either vfs_export
1771 * or unmount, so check if it hasn't already been done.
1774 if (nfs_pub.np_valid) {
1775 nfs_pub.np_valid = 0;
1776 if (nfs_pub.np_index != NULL) {
1777 FREE(nfs_pub.np_index, M_TEMP);
1778 nfs_pub.np_index = NULL;
1785 * Only one allowed at a time.
1787 if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
1791 * Get real filehandle for root of exported FS.
1793 bzero((caddr_t)&nfs_pub.np_handle, sizeof(nfs_pub.np_handle));
1794 nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid;
1796 if ((error = VFS_ROOT(mp, &rvp)))
1799 if ((error = VFS_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid)))
1805 * If an indexfile was specified, pull it in.
1807 if (argp->ex_indexfile != NULL) {
1810 error = vn_get_namelen(rvp, &namelen);
1813 MALLOC(nfs_pub.np_index, char *, namelen, M_TEMP,
1815 error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
1819 * Check for illegal filenames.
1821 for (cp = nfs_pub.np_index; *cp; cp++) {
1829 FREE(nfs_pub.np_index, M_TEMP);
1834 nfs_pub.np_mount = mp;
1835 nfs_pub.np_valid = 1;
1840 vfs_export_lookup(struct mount *mp, struct netexport *nep,
1841 struct sockaddr *nam)
1844 struct radix_node_head *rnh;
1845 struct sockaddr *saddr;
1848 if (mp->mnt_flag & MNT_EXPORTED) {
1850 * Lookup in the export list first.
1854 rnh = nep->ne_rtable[saddr->sa_family];
1856 np = (struct netcred *)
1857 (*rnh->rnh_matchaddr)((char *)saddr,
1859 if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
1864 * If no address match, use the default if it exists.
1866 if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
1867 np = &nep->ne_defexported;
1873 * perform msync on all vnodes under a mount point. The mount point must
1874 * be locked. This code is also responsible for lazy-freeing unreferenced
1875 * vnodes whos VM objects no longer contain pages.
1877 * NOTE: MNT_WAIT still skips vnodes in the VXLOCK state.
1879 * NOTE: XXX VOP_PUTPAGES and friends requires that the vnode be locked,
1880 * but vnode_pager_putpages() doesn't lock the vnode. We have to do it
1881 * way up in this high level function.
1883 static int vfs_msync_scan1(struct mount *mp, struct vnode *vp, void *data);
1884 static int vfs_msync_scan2(struct mount *mp, struct vnode *vp, void *data);
1887 vfs_msync(struct mount *mp, int flags)
1891 vmsc_flags = VMSC_GETVP;
1892 if (flags != MNT_WAIT)
1893 vmsc_flags |= VMSC_NOWAIT;
1894 vmntvnodescan(mp, vmsc_flags, vfs_msync_scan1, vfs_msync_scan2,
1895 (void *)(intptr_t)flags);
1899 * scan1 is a fast pre-check. There could be hundreds of thousands of
1900 * vnodes, we cannot afford to do anything heavy weight until we have a
1901 * fairly good indication that there is work to do.
1905 vfs_msync_scan1(struct mount *mp, struct vnode *vp, void *data)
1907 int flags = (int)(intptr_t)data;
1909 if ((vp->v_flag & VRECLAIMED) == 0) {
1910 if (vshouldmsync(vp))
1911 return(0); /* call scan2 */
1912 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
1913 (vp->v_flag & VOBJDIRTY) &&
1914 (flags == MNT_WAIT || vn_islocked(vp) == 0)) {
1915 return(0); /* call scan2 */
1920 * do not call scan2, continue the loop
1926 * This callback is handed a locked vnode.
1930 vfs_msync_scan2(struct mount *mp, struct vnode *vp, void *data)
1933 int flags = (int)(intptr_t)data;
1935 if (vp->v_flag & VRECLAIMED)
1938 if ((mp->mnt_flag & MNT_RDONLY) == 0 && (vp->v_flag & VOBJDIRTY)) {
1939 if ((obj = vp->v_object) != NULL) {
1940 vm_object_page_clean(obj, 0, 0,
1941 flags == MNT_WAIT ? OBJPC_SYNC : OBJPC_NOSYNC);
1948 * Record a process's interest in events which might happen to
1949 * a vnode. Because poll uses the historic select-style interface
1950 * internally, this routine serves as both the ``check for any
1951 * pending events'' and the ``record my interest in future events''
1952 * functions. (These are done together, while the lock is held,
1953 * to avoid race conditions.)
1956 vn_pollrecord(struct vnode *vp, int events)
1960 KKASSERT(curthread->td_proc != NULL);
1962 lwkt_gettoken(&vlock, &vp->v_token);
1963 if (vp->v_pollinfo.vpi_revents & events) {
1965 * This leaves events we are not interested
1966 * in available for the other process which
1967 * which presumably had requested them
1968 * (otherwise they would never have been
1971 events &= vp->v_pollinfo.vpi_revents;
1972 vp->v_pollinfo.vpi_revents &= ~events;
1974 lwkt_reltoken(&vlock);
1977 vp->v_pollinfo.vpi_events |= events;
1978 selrecord(curthread, &vp->v_pollinfo.vpi_selinfo);
1979 lwkt_reltoken(&vlock);
1984 * Note the occurrence of an event. If the VN_POLLEVENT macro is used,
1985 * it is possible for us to miss an event due to race conditions, but
1986 * that condition is expected to be rare, so for the moment it is the
1987 * preferred interface.
1990 vn_pollevent(struct vnode *vp, int events)
1994 lwkt_gettoken(&vlock, &vp->v_token);
1995 if (vp->v_pollinfo.vpi_events & events) {
1997 * We clear vpi_events so that we don't
1998 * call selwakeup() twice if two events are
1999 * posted before the polling process(es) is
2000 * awakened. This also ensures that we take at
2001 * most one selwakeup() if the polling process
2002 * is no longer interested. However, it does
2003 * mean that only one event can be noticed at
2004 * a time. (Perhaps we should only clear those
2005 * event bits which we note?) XXX
2007 vp->v_pollinfo.vpi_events = 0; /* &= ~events ??? */
2008 vp->v_pollinfo.vpi_revents |= events;
2009 selwakeup(&vp->v_pollinfo.vpi_selinfo);
2011 lwkt_reltoken(&vlock);
2015 * Wake up anyone polling on vp because it is being revoked.
2016 * This depends on dead_poll() returning POLLHUP for correct
2020 vn_pollgone(struct vnode *vp)
2024 lwkt_gettoken(&vlock, &vp->v_token);
2025 if (vp->v_pollinfo.vpi_events) {
2026 vp->v_pollinfo.vpi_events = 0;
2027 selwakeup(&vp->v_pollinfo.vpi_selinfo);
2029 lwkt_reltoken(&vlock);
2033 * extract the cdev_t from a VBLK or VCHR. The vnode must have been opened
2034 * (or v_rdev might be NULL).
2037 vn_todev(struct vnode *vp)
2039 if (vp->v_type != VBLK && vp->v_type != VCHR)
2041 KKASSERT(vp->v_rdev != NULL);
2042 return (vp->v_rdev);
2046 * Check if vnode represents a disk device. The vnode does not need to be
2052 vn_isdisk(struct vnode *vp, int *errp)
2056 if (vp->v_type != VCHR) {
2069 if (dev_is_good(dev) == 0) {
2074 if ((dev_dflags(dev) & D_DISK) == 0) {
2085 vn_get_namelen(struct vnode *vp, int *namelen)
2088 register_t retval[2];
2090 error = VOP_PATHCONF(vp, _PC_NAME_MAX, retval);
2093 *namelen = (int)retval[0];
2098 vop_write_dirent(int *error, struct uio *uio, ino_t d_ino, uint8_t d_type,
2099 uint16_t d_namlen, const char *d_name)
2104 len = _DIRENT_RECLEN(d_namlen);
2105 if (len > uio->uio_resid)
2108 dp = kmalloc(len, M_TEMP, M_WAITOK | M_ZERO);
2111 dp->d_namlen = d_namlen;
2112 dp->d_type = d_type;
2113 bcopy(d_name, dp->d_name, d_namlen);
2115 *error = uiomove((caddr_t)dp, len, uio);
2123 vn_mark_atime(struct vnode *vp, struct thread *td)
2125 struct proc *p = td->td_proc;
2126 struct ucred *cred = p ? p->p_ucred : proc0.p_ucred;
2128 if ((vp->v_mount->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0) {
2129 VOP_MARKATIME(vp, cred);