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.94 2006/08/12 00:26:20 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>
56 #include <sys/kernel.h>
57 #include <sys/kthread.h>
58 #include <sys/malloc.h>
60 #include <sys/mount.h>
62 #include <sys/reboot.h>
63 #include <sys/socket.h>
65 #include <sys/sysctl.h>
66 #include <sys/syslog.h>
67 #include <sys/unistd.h>
68 #include <sys/vmmeter.h>
69 #include <sys/vnode.h>
71 #include <machine/limits.h>
74 #include <vm/vm_object.h>
75 #include <vm/vm_extern.h>
76 #include <vm/vm_kern.h>
78 #include <vm/vm_map.h>
79 #include <vm/vm_page.h>
80 #include <vm/vm_pager.h>
81 #include <vm/vnode_pager.h>
82 #include <vm/vm_zone.h>
85 #include <sys/thread2.h>
87 static MALLOC_DEFINE(M_NETADDR, "Export Host", "Export host address structure");
90 SYSCTL_INT(_debug, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0, "");
92 SYSCTL_INT(_vfs, OID_AUTO, fastdev, CTLFLAG_RW, &vfs_fastdev, 0, "");
94 enum vtype iftovt_tab[16] = {
95 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
96 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
99 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
100 S_IFSOCK, S_IFIFO, S_IFMT,
103 static int reassignbufcalls;
104 SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW,
105 &reassignbufcalls, 0, "");
106 static int reassignbufloops;
107 SYSCTL_INT(_vfs, OID_AUTO, reassignbufloops, CTLFLAG_RW,
108 &reassignbufloops, 0, "");
109 static int reassignbufsortgood;
110 SYSCTL_INT(_vfs, OID_AUTO, reassignbufsortgood, CTLFLAG_RW,
111 &reassignbufsortgood, 0, "");
112 static int reassignbufsortbad;
113 SYSCTL_INT(_vfs, OID_AUTO, reassignbufsortbad, CTLFLAG_RW,
114 &reassignbufsortbad, 0, "");
115 static int reassignbufmethod = 1;
116 SYSCTL_INT(_vfs, OID_AUTO, reassignbufmethod, CTLFLAG_RW,
117 &reassignbufmethod, 0, "");
119 int nfs_mount_type = -1;
120 static struct lwkt_token spechash_token;
121 struct nfs_public nfs_pub; /* publicly exported FS */
124 SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RW,
125 &desiredvnodes, 0, "Maximum number of vnodes");
127 static void vfs_free_addrlist (struct netexport *nep);
128 static int vfs_free_netcred (struct radix_node *rn, void *w);
129 static int vfs_hang_addrlist (struct mount *mp, struct netexport *nep,
130 struct export_args *argp);
132 extern int dev_ref_debug;
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, int usecount)
157 if (vp->v_holdcnt != 0 || vp->v_usecount != usecount)
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 * Desired vnodes is a result of the physical page count
176 * and the size of kernel's heap. It scales in proportion
177 * to the amount of available physical memory. This can
178 * cause trouble on 64-bit and large memory platforms.
180 /* desiredvnodes = maxproc + vmstats.v_page_count / 4; */
182 min(maxproc + vmstats.v_page_count /4,
183 2 * (VM_MAX_KERNEL_ADDRESS - VM_MIN_KERNEL_ADDRESS) /
184 (5 * (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.
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_rdev = VNOVAL;
247 vap->va_atime.tv_sec = VNOVAL;
248 vap->va_atime.tv_nsec = VNOVAL;
249 vap->va_mtime.tv_sec = VNOVAL;
250 vap->va_mtime.tv_nsec = VNOVAL;
251 vap->va_ctime.tv_sec = VNOVAL;
252 vap->va_ctime.tv_nsec = VNOVAL;
253 vap->va_flags = VNOVAL;
254 vap->va_gen = VNOVAL;
256 vap->va_fsmid = VNOVAL;
260 * Flush out and invalidate all buffers associated with a vnode.
264 static int vinvalbuf_bp(struct buf *bp, void *data);
266 struct vinvalbuf_bp_info {
274 vupdatefsmid(struct vnode *vp)
276 atomic_set_int(&vp->v_flag, VFSMID);
280 vinvalbuf(struct vnode *vp, int flags, int slpflag, int slptimeo)
282 struct vinvalbuf_bp_info info;
287 * If we are being asked to save, call fsync to ensure that the inode
290 if (flags & V_SAVE) {
292 while (vp->v_track_write.bk_active) {
293 vp->v_track_write.bk_waitflag = 1;
294 error = tsleep(&vp->v_track_write, slpflag,
295 "vinvlbuf", slptimeo);
301 if (!RB_EMPTY(&vp->v_rbdirty_tree)) {
303 if ((error = VOP_FSYNC(vp, MNT_WAIT)) != 0)
306 if (vp->v_track_write.bk_active > 0 ||
307 !RB_EMPTY(&vp->v_rbdirty_tree))
308 panic("vinvalbuf: dirty bufs");
313 info.slptimeo = slptimeo;
314 info.lkflags = LK_EXCLUSIVE | LK_SLEEPFAIL;
315 if (slpflag & PCATCH)
316 info.lkflags |= LK_PCATCH;
321 * Flush the buffer cache until nothing is left.
323 while (!RB_EMPTY(&vp->v_rbclean_tree) ||
324 !RB_EMPTY(&vp->v_rbdirty_tree)) {
325 error = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree, NULL,
326 vinvalbuf_bp, &info);
328 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
329 vinvalbuf_bp, &info);
334 * Wait for I/O to complete. XXX needs cleaning up. The vnode can
335 * have write I/O in-progress but if there is a VM object then the
336 * VM object can also have read-I/O in-progress.
339 while (vp->v_track_write.bk_active > 0) {
340 vp->v_track_write.bk_waitflag = 1;
341 tsleep(&vp->v_track_write, 0, "vnvlbv", 0);
343 if ((object = vp->v_object) != NULL) {
344 while (object->paging_in_progress)
345 vm_object_pip_sleep(object, "vnvlbx");
347 } while (vp->v_track_write.bk_active > 0);
352 * Destroy the copy in the VM cache, too.
354 if ((object = vp->v_object) != NULL) {
355 vm_object_page_remove(object, 0, 0,
356 (flags & V_SAVE) ? TRUE : FALSE);
359 if (!RB_EMPTY(&vp->v_rbdirty_tree) || !RB_EMPTY(&vp->v_rbclean_tree))
360 panic("vinvalbuf: flush failed");
361 if (!RB_EMPTY(&vp->v_rbhash_tree))
362 panic("vinvalbuf: flush failed, buffers still present");
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 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
395 (info->flags & V_SAVE)) {
396 if (bp->b_vp == info->vp) {
397 if (bp->b_flags & B_CLUSTEROK) {
401 bp->b_flags |= B_ASYNC;
408 } else if (info->flags & V_SAVE) {
410 * Cannot set B_NOCACHE on a clean buffer as this will
411 * destroy the VM backing store which might actually
412 * be dirty (and unsynchronized).
415 bp->b_flags |= (B_INVAL | B_RELBUF);
416 bp->b_flags &= ~B_ASYNC;
420 bp->b_flags |= (B_INVAL | B_NOCACHE | B_RELBUF);
421 bp->b_flags &= ~B_ASYNC;
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)
444 const char *filename;
447 * Round up to the *next* block, then destroy the buffers in question.
448 * Since we are only removing some of the buffers we must rely on the
449 * scan count to determine whether a loop is necessary.
451 if ((count = (int)(length % blksize)) != 0)
452 truncloffset = length + (blksize - count);
454 truncloffset = length;
458 count = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree,
459 vtruncbuf_bp_trunc_cmp,
460 vtruncbuf_bp_trunc, &truncloffset);
461 count += RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
462 vtruncbuf_bp_trunc_cmp,
463 vtruncbuf_bp_trunc, &truncloffset);
467 * For safety, fsync any remaining metadata if the file is not being
468 * truncated to 0. Since the metadata does not represent the entire
469 * dirty list we have to rely on the hit count to ensure that we get
474 count = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
475 vtruncbuf_bp_metasync_cmp,
476 vtruncbuf_bp_metasync, vp);
481 * Clean out any left over VM backing store.
485 vnode_pager_setsize(vp, length);
490 * It is possible to have in-progress I/O from buffers that were
491 * not part of the truncation. This should not happen if we
492 * are truncating to 0-length.
494 filename = TAILQ_FIRST(&vp->v_namecache) ?
495 TAILQ_FIRST(&vp->v_namecache)->nc_name : "?";
497 while ((count = vp->v_track_write.bk_active) > 0) {
498 vp->v_track_write.bk_waitflag = 1;
499 tsleep(&vp->v_track_write, 0, "vbtrunc", 0);
501 printf("Warning: vtruncbuf(): Had to wait for "
502 "%d buffer I/Os to finish in %s\n",
508 * Make sure no buffers were instantiated while we were trying
509 * to clean out the remaining VM pages. This could occur due
510 * to busy dirty VM pages being flushed out to disk.
513 count = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree,
514 vtruncbuf_bp_trunc_cmp,
515 vtruncbuf_bp_trunc, &truncloffset);
516 count += RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
517 vtruncbuf_bp_trunc_cmp,
518 vtruncbuf_bp_trunc, &truncloffset);
520 printf("Warning: vtruncbuf(): Had to re-clean %d "
521 "left over buffers in %s\n", count, filename);
531 * The callback buffer is beyond the new file EOF and must be destroyed.
532 * Note that the compare function must conform to the RB_SCAN's requirements.
536 vtruncbuf_bp_trunc_cmp(struct buf *bp, void *data)
538 if (bp->b_loffset >= *(off_t *)data)
545 vtruncbuf_bp_trunc(struct buf *bp, void *data)
548 * Do not try to use a buffer we cannot immediately lock, but sleep
549 * anyway to prevent a livelock. The code will loop until all buffers
552 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
553 if (BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL) == 0)
557 bp->b_flags |= (B_INVAL | B_RELBUF | B_NOCACHE);
558 bp->b_flags &= ~B_ASYNC;
565 * Fsync all meta-data after truncating a file to be non-zero. Only metadata
566 * blocks (with a negative loffset) are scanned.
567 * Note that the compare function must conform to the RB_SCAN's requirements.
570 vtruncbuf_bp_metasync_cmp(struct buf *bp, void *data)
572 if (bp->b_loffset < 0)
578 vtruncbuf_bp_metasync(struct buf *bp, void *data)
580 struct vnode *vp = data;
582 if (bp->b_flags & B_DELWRI) {
584 * Do not try to use a buffer we cannot immediately lock,
585 * but sleep anyway to prevent a livelock. The code will
586 * loop until all buffers can be acted upon.
588 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
589 if (BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL) == 0)
593 if (bp->b_vp == vp) {
594 bp->b_flags |= B_ASYNC;
596 bp->b_flags &= ~B_ASYNC;
607 * vfsync - implements a multipass fsync on a file which understands
608 * dependancies and meta-data. The passed vnode must be locked. The
609 * waitfor argument may be MNT_WAIT or MNT_NOWAIT, or MNT_LAZY.
611 * When fsyncing data asynchronously just do one consolidated pass starting
612 * with the most negative block number. This may not get all the data due
615 * When fsyncing data synchronously do a data pass, then a metadata pass,
616 * then do additional data+metadata passes to try to get all the data out.
618 static int vfsync_wait_output(struct vnode *vp,
619 int (*waitoutput)(struct vnode *, struct thread *));
620 static int vfsync_data_only_cmp(struct buf *bp, void *data);
621 static int vfsync_meta_only_cmp(struct buf *bp, void *data);
622 static int vfsync_lazy_range_cmp(struct buf *bp, void *data);
623 static int vfsync_bp(struct buf *bp, void *data);
632 int (*checkdef)(struct buf *);
636 vfsync(struct vnode *vp, int waitfor, int passes,
637 int (*checkdef)(struct buf *),
638 int (*waitoutput)(struct vnode *, struct thread *))
640 struct vfsync_info info;
643 bzero(&info, sizeof(info));
645 if ((info.checkdef = checkdef) == NULL)
648 crit_enter_id("vfsync");
653 * Lazy (filesystem syncer typ) Asynchronous plus limit the
654 * number of data (not meta) pages we try to flush to 1MB.
655 * A non-zero return means that lazy limit was reached.
657 info.lazylimit = 1024 * 1024;
659 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
660 vfsync_lazy_range_cmp, vfsync_bp, &info);
661 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
662 vfsync_meta_only_cmp, vfsync_bp, &info);
665 else if (!RB_EMPTY(&vp->v_rbdirty_tree))
666 vn_syncer_add_to_worklist(vp, 1);
671 * Asynchronous. Do a data-only pass and a meta-only pass.
674 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_data_only_cmp,
676 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_meta_only_cmp,
682 * Synchronous. Do a data-only pass, then a meta-data+data
683 * pass, then additional integrated passes to try to get
684 * all the dependancies flushed.
686 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_data_only_cmp,
688 error = vfsync_wait_output(vp, waitoutput);
690 info.skippedbufs = 0;
691 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
693 error = vfsync_wait_output(vp, waitoutput);
694 if (info.skippedbufs)
695 printf("Warning: vfsync skipped %d dirty bufs in pass2!\n", info.skippedbufs);
697 while (error == 0 && passes > 0 &&
698 !RB_EMPTY(&vp->v_rbdirty_tree)) {
700 info.synchronous = 1;
703 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
709 error = vfsync_wait_output(vp, waitoutput);
713 crit_exit_id("vfsync");
718 vfsync_wait_output(struct vnode *vp, int (*waitoutput)(struct vnode *, struct thread *))
722 while (vp->v_track_write.bk_active) {
723 vp->v_track_write.bk_waitflag = 1;
724 tsleep(&vp->v_track_write, 0, "fsfsn", 0);
727 error = waitoutput(vp, curthread);
732 vfsync_data_only_cmp(struct buf *bp, void *data)
734 if (bp->b_loffset < 0)
740 vfsync_meta_only_cmp(struct buf *bp, void *data)
742 if (bp->b_loffset < 0)
748 vfsync_lazy_range_cmp(struct buf *bp, void *data)
750 struct vfsync_info *info = data;
751 if (bp->b_loffset < info->vp->v_lazyw)
757 vfsync_bp(struct buf *bp, void *data)
759 struct vfsync_info *info = data;
760 struct vnode *vp = info->vp;
764 * if syncdeps is not set we do not try to write buffers which have
767 if (!info->synchronous && info->syncdeps == 0 && info->checkdef(bp))
771 * Ignore buffers that we cannot immediately lock. XXX
773 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
774 printf("Warning: vfsync_bp skipping dirty buffer %p\n", bp);
778 if ((bp->b_flags & B_DELWRI) == 0)
779 panic("vfsync_bp: buffer not dirty");
781 panic("vfsync_bp: buffer vp mismatch");
784 * B_NEEDCOMMIT (primarily used by NFS) is a state where the buffer
785 * has been written but an additional handshake with the device
786 * is required before we can dispose of the buffer. We have no idea
787 * how to do this so we have to skip these buffers.
789 if (bp->b_flags & B_NEEDCOMMIT) {
794 if (info->synchronous) {
796 * Synchronous flushing. An error may be returned.
799 crit_exit_id("vfsync");
801 crit_enter_id("vfsync");
804 * Asynchronous flushing. A negative return value simply
805 * stops the scan and is not considered an error. We use
806 * this to support limited MNT_LAZY flushes.
808 vp->v_lazyw = bp->b_loffset;
809 if ((vp->v_flag & VOBJBUF) && (bp->b_flags & B_CLUSTEROK)) {
810 info->lazycount += vfs_bio_awrite(bp);
812 info->lazycount += bp->b_bufsize;
814 crit_exit_id("vfsync");
816 crit_enter_id("vfsync");
818 if (info->lazylimit && info->lazycount >= info->lazylimit)
827 * Associate a buffer with a vnode.
830 bgetvp(struct vnode *vp, struct buf *bp)
832 KASSERT(bp->b_vp == NULL, ("bgetvp: not free"));
833 KKASSERT((bp->b_flags & (B_HASHED|B_DELWRI|B_VNCLEAN|B_VNDIRTY)) == 0);
837 * Insert onto list for new vnode.
841 bp->b_flags |= B_HASHED;
842 if (buf_rb_hash_RB_INSERT(&vp->v_rbhash_tree, bp))
843 panic("reassignbuf: dup lblk vp %p bp %p", vp, bp);
845 bp->b_flags |= B_VNCLEAN;
846 if (buf_rb_tree_RB_INSERT(&vp->v_rbclean_tree, bp))
847 panic("reassignbuf: dup lblk/clean vp %p bp %p", vp, bp);
852 * Disassociate a buffer from a vnode.
855 brelvp(struct buf *bp)
859 KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
862 * Delete from old vnode list, if on one.
866 if (bp->b_flags & (B_VNDIRTY | B_VNCLEAN)) {
867 if (bp->b_flags & B_VNDIRTY)
868 buf_rb_tree_RB_REMOVE(&vp->v_rbdirty_tree, bp);
870 buf_rb_tree_RB_REMOVE(&vp->v_rbclean_tree, bp);
871 bp->b_flags &= ~(B_VNDIRTY | B_VNCLEAN);
873 if (bp->b_flags & B_HASHED) {
874 buf_rb_hash_RB_REMOVE(&vp->v_rbhash_tree, bp);
875 bp->b_flags &= ~B_HASHED;
877 if ((vp->v_flag & VONWORKLST) && RB_EMPTY(&vp->v_rbdirty_tree)) {
878 vp->v_flag &= ~VONWORKLST;
879 LIST_REMOVE(vp, v_synclist);
887 * Reassign the buffer to the proper clean/dirty list based on B_DELWRI.
888 * This routine is called when the state of the B_DELWRI bit is changed.
891 reassignbuf(struct buf *bp)
893 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");
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);
966 * Create a vnode for a block device.
967 * Used for mounting the root file system.
970 bdevvp(dev_t dev, struct vnode **vpp)
980 error = getspecialvnode(VT_NON, NULL, &spec_vnode_vops_p, &nvp, 0, 0);
987 vp->v_udev = dev->si_udev;
994 v_associate_rdev(struct vnode *vp, dev_t dev)
998 if (dev == NULL || dev == NODEV)
1000 if (dev_is_good(dev) == 0)
1002 KKASSERT(vp->v_rdev == NULL);
1005 vp->v_rdev = reference_dev(dev);
1006 lwkt_gettoken(&ilock, &spechash_token);
1007 SLIST_INSERT_HEAD(&dev->si_hlist, vp, v_specnext);
1008 lwkt_reltoken(&ilock);
1013 v_release_rdev(struct vnode *vp)
1018 if ((dev = vp->v_rdev) != NULL) {
1019 lwkt_gettoken(&ilock, &spechash_token);
1020 SLIST_REMOVE(&dev->si_hlist, vp, vnode, v_specnext);
1023 lwkt_reltoken(&ilock);
1028 * Add a vnode to the alias list hung off the dev_t. We only associate
1029 * the device number with the vnode. The actual device is not associated
1030 * until the vnode is opened (usually in spec_open()), and will be
1031 * disassociated on last close.
1034 addaliasu(struct vnode *nvp, udev_t nvp_udev)
1036 if (nvp->v_type != VBLK && nvp->v_type != VCHR)
1037 panic("addaliasu on non-special vnode");
1038 nvp->v_udev = nvp_udev;
1042 * Disassociate a vnode from its underlying filesystem.
1044 * The vnode must be VX locked, referenced, and v_spinlock must be held.
1045 * This routine releases v_spinlock.
1047 * If there are v_usecount references to the vnode other then ours we have
1048 * to VOP_CLOSE the vnode before we can deactivate and reclaim it.
1051 vclean_interlocked(struct vnode *vp, int flags)
1058 * If the vnode has already been reclaimed we have nothing to do.
1059 * VRECLAIMED must be interlocked with the vnode's spinlock.
1061 if (vp->v_flag & VRECLAIMED) {
1062 spin_unlock_wr(&vp->v_spinlock);
1065 vp->v_flag |= VRECLAIMED;
1066 spin_unlock_wr(&vp->v_spinlock);
1069 * Scrap the vfs cache
1071 while (cache_inval_vp(vp, 0) != 0) {
1072 printf("Warning: vnode %p clean/cache_resolution race detected\n", vp);
1073 tsleep(vp, 0, "vclninv", 2);
1077 * Check to see if the vnode is in use. If so we have to reference it
1078 * before we clean it out so that its count cannot fall to zero and
1079 * generate a race against ourselves to recycle it.
1081 active = (vp->v_usecount > 1);
1084 * Clean out any buffers associated with the vnode and destroy its
1085 * object, if it has one.
1087 vinvalbuf(vp, V_SAVE, 0, 0);
1090 * If purging an active vnode (typically during a forced unmount
1091 * or reboot), it must be closed and deactivated before being
1092 * reclaimed. This isn't really all that safe, but what can
1095 * Note that neither of these routines unlocks the vnode.
1097 if (active && (flags & DOCLOSE)) {
1098 while ((n = vp->v_opencount) != 0) {
1099 if (vp->v_writecount)
1100 VOP_CLOSE(vp, FWRITE|FNONBLOCK);
1102 VOP_CLOSE(vp, FNONBLOCK);
1103 if (vp->v_opencount == n) {
1104 printf("Warning: unable to force-close"
1112 * If the vnode has not be deactivated, deactivated it. Deactivation
1113 * can create new buffers and VM pages so we have to call vinvalbuf()
1114 * again to make sure they all get flushed.
1116 * This can occur if a file with a link count of 0 needs to be
1119 if ((vp->v_flag & VINACTIVE) == 0) {
1120 vp->v_flag |= VINACTIVE;
1122 vinvalbuf(vp, V_SAVE, 0, 0);
1126 * If the vnode has an object, destroy it.
1128 if ((object = vp->v_object) != NULL) {
1129 if (object->ref_count == 0) {
1130 if ((object->flags & OBJ_DEAD) == 0)
1131 vm_object_terminate(object);
1133 vm_pager_deallocate(object);
1135 vp->v_flag &= ~VOBJBUF;
1137 KKASSERT((vp->v_flag & VOBJBUF) == 0);
1141 * Reclaim the vnode.
1143 if (VOP_RECLAIM(vp))
1144 panic("vclean: cannot reclaim");
1147 * Done with purge, notify sleepers of the grim news.
1149 vp->v_ops = &dead_vnode_vops_p;
1155 * Eliminate all activity associated with the requested vnode
1156 * and with all vnodes aliased to the requested vnode.
1158 * The vnode must be referenced and vx_lock()'d
1160 * revoke { struct vnode *a_vp, int a_flags }
1163 vop_stdrevoke(struct vop_revoke_args *ap)
1165 struct vnode *vp, *vq;
1169 KASSERT((ap->a_flags & REVOKEALL) != 0, ("vop_revoke"));
1174 * If the vnode is already dead don't try to revoke it
1176 if (vp->v_flag & VRECLAIMED)
1180 * If the vnode has a device association, scrap all vnodes associated
1181 * with the device. Don't let the device disappear on us while we
1182 * are scrapping the vnodes.
1184 * The passed vp will probably show up in the list, do not VX lock
1187 if (vp->v_type != VCHR && vp->v_type != VBLK)
1189 if ((dev = vp->v_rdev) == NULL) {
1190 if ((dev = udev2dev(vp->v_udev, vp->v_type == VBLK)) == NODEV)
1194 lwkt_gettoken(&ilock, &spechash_token);
1195 while ((vq = SLIST_FIRST(&dev->si_hlist)) != NULL) {
1198 if (vq == SLIST_FIRST(&dev->si_hlist))
1203 lwkt_reltoken(&ilock);
1209 * Recycle an unused vnode to the front of the free list.
1211 * Returns 1 if we were successfully able to recycle the vnode,
1215 vrecycle(struct vnode *vp)
1217 if (vp->v_usecount == 1) {
1225 * Eliminate all activity associated with a vnode in preparation for reuse.
1227 * The vnode must be VX locked and refd and will remain VX locked and refd
1228 * on return. This routine may be called with the vnode in any state, as
1229 * long as it is VX locked. The vnode will be cleaned out and marked
1230 * VRECLAIMED but will not actually be reused until all existing refs and
1233 * NOTE: This routine may be called on a vnode which has not yet been
1234 * already been deactivated (VOP_INACTIVE), or on a vnode which has
1235 * already been reclaimed.
1237 * This routine is not responsible for placing us back on the freelist.
1238 * Instead, it happens automatically when the caller releases the VX lock
1239 * (assuming there aren't any other references).
1242 vgone(struct vnode *vp)
1244 spin_lock_wr(&vp->v_spinlock);
1245 vgone_interlocked(vp);
1249 vgone_interlocked(struct vnode *vp)
1252 * assert that the VX lock is held. This is an absolute requirement
1253 * now for vgone() to be called.
1255 KKASSERT(vp->v_lock.lk_exclusivecount == 1);
1258 * Clean out the filesystem specific data and set the VRECLAIMED
1259 * bit. Also deactivate the vnode if necessary.
1261 vclean_interlocked(vp, DOCLOSE);
1262 /* spinlock unlocked */
1265 * Delete from old mount point vnode list, if on one.
1267 if (vp->v_mount != NULL)
1268 insmntque(vp, NULL);
1271 * If special device, remove it from special device alias list
1272 * if it is on one. This should normally only occur if a vnode is
1273 * being revoked as the device should otherwise have been released
1276 if ((vp->v_type == VBLK || vp->v_type == VCHR) && vp->v_rdev != NULL) {
1287 * Lookup a vnode by device number.
1290 vfinddev(dev_t dev, enum vtype type, struct vnode **vpp)
1295 lwkt_gettoken(&ilock, &spechash_token);
1296 SLIST_FOREACH(vp, &dev->si_hlist, v_specnext) {
1297 if (type == vp->v_type) {
1299 lwkt_reltoken(&ilock);
1303 lwkt_reltoken(&ilock);
1308 * Calculate the total number of references to a special device. This
1309 * routine may only be called for VBLK and VCHR vnodes since v_rdev is
1310 * an overloaded field. Since udev2dev can now return NODEV, we have
1311 * to check for a NULL v_rdev.
1314 count_dev(dev_t dev)
1320 if (SLIST_FIRST(&dev->si_hlist)) {
1321 lwkt_gettoken(&ilock, &spechash_token);
1322 SLIST_FOREACH(vp, &dev->si_hlist, v_specnext) {
1323 count += vp->v_usecount;
1325 lwkt_reltoken(&ilock);
1331 count_udev(udev_t udev)
1335 if ((dev = udev2dev(udev, 0)) == NODEV)
1337 return(count_dev(dev));
1341 vcount(struct vnode *vp)
1343 if (vp->v_rdev == NULL)
1345 return(count_dev(vp->v_rdev));
1349 * Initialize VMIO for a vnode. This routine MUST be called before a
1350 * VFS can issue buffer cache ops on a vnode. It is typically called
1351 * when a vnode is initialized from its inode.
1354 vinitvmio(struct vnode *vp, off_t filesize)
1360 if ((object = vp->v_object) == NULL) {
1361 object = vnode_pager_alloc(vp, filesize, 0, 0);
1363 * Dereference the reference we just created. This assumes
1364 * that the object is associated with the vp.
1366 object->ref_count--;
1369 if (object->flags & OBJ_DEAD) {
1371 tsleep(object, 0, "vodead", 0);
1372 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1376 KASSERT(vp->v_object != NULL, ("vinitvmio: NULL object"));
1377 vp->v_flag |= VOBJBUF;
1383 * Print out a description of a vnode.
1385 static char *typename[] =
1386 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD"};
1389 vprint(char *label, struct vnode *vp)
1394 printf("%s: %p: ", label, (void *)vp);
1396 printf("%p: ", (void *)vp);
1397 printf("type %s, usecount %d, writecount %d, refcount %d,",
1398 typename[vp->v_type], vp->v_usecount, vp->v_writecount,
1401 if (vp->v_flag & VROOT)
1402 strcat(buf, "|VROOT");
1403 if (vp->v_flag & VTEXT)
1404 strcat(buf, "|VTEXT");
1405 if (vp->v_flag & VSYSTEM)
1406 strcat(buf, "|VSYSTEM");
1407 if (vp->v_flag & VFREE)
1408 strcat(buf, "|VFREE");
1409 if (vp->v_flag & VOBJBUF)
1410 strcat(buf, "|VOBJBUF");
1412 printf(" flags (%s)", &buf[1]);
1413 if (vp->v_data == NULL) {
1422 #include <ddb/ddb.h>
1424 static int db_show_locked_vnodes(struct mount *mp, void *data);
1427 * List all of the locked vnodes in the system.
1428 * Called when debugging the kernel.
1430 DB_SHOW_COMMAND(lockedvnodes, lockedvnodes)
1432 printf("Locked vnodes\n");
1433 mountlist_scan(db_show_locked_vnodes, NULL,
1434 MNTSCAN_FORWARD|MNTSCAN_NOBUSY);
1438 db_show_locked_vnodes(struct mount *mp, void *data __unused)
1442 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
1443 if (vn_islocked(vp))
1444 vprint((char *)0, vp);
1451 * Top level filesystem related information gathering.
1453 static int sysctl_ovfs_conf (SYSCTL_HANDLER_ARGS);
1456 vfs_sysctl(SYSCTL_HANDLER_ARGS)
1458 int *name = (int *)arg1 - 1; /* XXX */
1459 u_int namelen = arg2 + 1; /* XXX */
1460 struct vfsconf *vfsp;
1462 #if 1 || defined(COMPAT_PRELITE2)
1463 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
1465 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
1469 /* all sysctl names at this level are at least name and field */
1471 return (ENOTDIR); /* overloaded */
1472 if (name[0] != VFS_GENERIC) {
1473 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
1474 if (vfsp->vfc_typenum == name[0])
1477 return (EOPNOTSUPP);
1478 return ((*vfsp->vfc_vfsops->vfs_sysctl)(&name[1], namelen - 1,
1479 oldp, oldlenp, newp, newlen, p));
1483 case VFS_MAXTYPENUM:
1486 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int)));
1489 return (ENOTDIR); /* overloaded */
1490 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
1491 if (vfsp->vfc_typenum == name[2])
1494 return (EOPNOTSUPP);
1495 return (SYSCTL_OUT(req, vfsp, sizeof *vfsp));
1497 return (EOPNOTSUPP);
1500 SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD, vfs_sysctl,
1501 "Generic filesystem");
1503 #if 1 || defined(COMPAT_PRELITE2)
1506 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
1509 struct vfsconf *vfsp;
1510 struct ovfsconf ovfs;
1512 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
1513 bzero(&ovfs, sizeof(ovfs));
1514 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
1515 strcpy(ovfs.vfc_name, vfsp->vfc_name);
1516 ovfs.vfc_index = vfsp->vfc_typenum;
1517 ovfs.vfc_refcount = vfsp->vfc_refcount;
1518 ovfs.vfc_flags = vfsp->vfc_flags;
1519 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
1526 #endif /* 1 || COMPAT_PRELITE2 */
1529 * Check to see if a filesystem is mounted on a block device.
1532 vfs_mountedon(struct vnode *vp)
1536 if ((dev = vp->v_rdev) == NULL)
1537 dev = udev2dev(vp->v_udev, (vp->v_type == VBLK));
1538 if (dev != NODEV && dev->si_mountpoint)
1544 * Unmount all filesystems. The list is traversed in reverse order
1545 * of mounting to avoid dependencies.
1548 static int vfs_umountall_callback(struct mount *mp, void *data);
1551 vfs_unmountall(void)
1553 struct thread *td = curthread;
1556 if (td->td_proc == NULL)
1557 td = initproc->p_thread; /* XXX XXX use proc0 instead? */
1560 count = mountlist_scan(vfs_umountall_callback,
1561 NULL, MNTSCAN_REVERSE|MNTSCAN_NOBUSY);
1567 vfs_umountall_callback(struct mount *mp, void *data)
1571 error = dounmount(mp, MNT_FORCE);
1573 mountlist_remove(mp);
1574 printf("unmount of filesystem mounted from %s failed (",
1575 mp->mnt_stat.f_mntfromname);
1579 printf("%d)\n", error);
1585 * Build hash lists of net addresses and hang them off the mount point.
1586 * Called by ufs_mount() to set up the lists of export addresses.
1589 vfs_hang_addrlist(struct mount *mp, struct netexport *nep,
1590 struct export_args *argp)
1593 struct radix_node_head *rnh;
1595 struct radix_node *rn;
1596 struct sockaddr *saddr, *smask = 0;
1600 if (argp->ex_addrlen == 0) {
1601 if (mp->mnt_flag & MNT_DEFEXPORTED)
1603 np = &nep->ne_defexported;
1604 np->netc_exflags = argp->ex_flags;
1605 np->netc_anon = argp->ex_anon;
1606 np->netc_anon.cr_ref = 1;
1607 mp->mnt_flag |= MNT_DEFEXPORTED;
1611 if (argp->ex_addrlen < 0 || argp->ex_addrlen > MLEN)
1613 if (argp->ex_masklen < 0 || argp->ex_masklen > MLEN)
1616 i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
1617 np = (struct netcred *) malloc(i, M_NETADDR, M_WAITOK);
1618 bzero((caddr_t) np, i);
1619 saddr = (struct sockaddr *) (np + 1);
1620 if ((error = copyin(argp->ex_addr, (caddr_t) saddr, argp->ex_addrlen)))
1622 if (saddr->sa_len > argp->ex_addrlen)
1623 saddr->sa_len = argp->ex_addrlen;
1624 if (argp->ex_masklen) {
1625 smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen);
1626 error = copyin(argp->ex_mask, (caddr_t)smask, argp->ex_masklen);
1629 if (smask->sa_len > argp->ex_masklen)
1630 smask->sa_len = argp->ex_masklen;
1632 i = saddr->sa_family;
1633 if ((rnh = nep->ne_rtable[i]) == 0) {
1635 * Seems silly to initialize every AF when most are not used,
1636 * do so on demand here
1638 SLIST_FOREACH(dom, &domains, dom_next)
1639 if (dom->dom_family == i && dom->dom_rtattach) {
1640 dom->dom_rtattach((void **) &nep->ne_rtable[i],
1644 if ((rnh = nep->ne_rtable[i]) == 0) {
1649 rn = (*rnh->rnh_addaddr) ((char *) saddr, (char *) smask, rnh,
1651 if (rn == 0 || np != (struct netcred *) rn) { /* already exists */
1655 np->netc_exflags = argp->ex_flags;
1656 np->netc_anon = argp->ex_anon;
1657 np->netc_anon.cr_ref = 1;
1660 free(np, M_NETADDR);
1666 vfs_free_netcred(struct radix_node *rn, void *w)
1668 struct radix_node_head *rnh = (struct radix_node_head *) w;
1670 (*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, rnh);
1671 free((caddr_t) rn, M_NETADDR);
1676 * Free the net address hash lists that are hanging off the mount points.
1679 vfs_free_addrlist(struct netexport *nep)
1682 struct radix_node_head *rnh;
1684 for (i = 0; i <= AF_MAX; i++)
1685 if ((rnh = nep->ne_rtable[i])) {
1686 (*rnh->rnh_walktree) (rnh, vfs_free_netcred,
1688 free((caddr_t) rnh, M_RTABLE);
1689 nep->ne_rtable[i] = 0;
1694 vfs_export(struct mount *mp, struct netexport *nep, struct export_args *argp)
1698 if (argp->ex_flags & MNT_DELEXPORT) {
1699 if (mp->mnt_flag & MNT_EXPUBLIC) {
1700 vfs_setpublicfs(NULL, NULL, NULL);
1701 mp->mnt_flag &= ~MNT_EXPUBLIC;
1703 vfs_free_addrlist(nep);
1704 mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
1706 if (argp->ex_flags & MNT_EXPORTED) {
1707 if (argp->ex_flags & MNT_EXPUBLIC) {
1708 if ((error = vfs_setpublicfs(mp, nep, argp)) != 0)
1710 mp->mnt_flag |= MNT_EXPUBLIC;
1712 if ((error = vfs_hang_addrlist(mp, nep, argp)))
1714 mp->mnt_flag |= MNT_EXPORTED;
1721 * Set the publicly exported filesystem (WebNFS). Currently, only
1722 * one public filesystem is possible in the spec (RFC 2054 and 2055)
1725 vfs_setpublicfs(struct mount *mp, struct netexport *nep,
1726 struct export_args *argp)
1733 * mp == NULL -> invalidate the current info, the FS is
1734 * no longer exported. May be called from either vfs_export
1735 * or unmount, so check if it hasn't already been done.
1738 if (nfs_pub.np_valid) {
1739 nfs_pub.np_valid = 0;
1740 if (nfs_pub.np_index != NULL) {
1741 FREE(nfs_pub.np_index, M_TEMP);
1742 nfs_pub.np_index = NULL;
1749 * Only one allowed at a time.
1751 if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
1755 * Get real filehandle for root of exported FS.
1757 bzero((caddr_t)&nfs_pub.np_handle, sizeof(nfs_pub.np_handle));
1758 nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid;
1760 if ((error = VFS_ROOT(mp, &rvp)))
1763 if ((error = VFS_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid)))
1769 * If an indexfile was specified, pull it in.
1771 if (argp->ex_indexfile != NULL) {
1774 error = vn_get_namelen(rvp, &namelen);
1777 MALLOC(nfs_pub.np_index, char *, namelen, M_TEMP,
1779 error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
1780 namelen, (size_t *)0);
1783 * Check for illegal filenames.
1785 for (cp = nfs_pub.np_index; *cp; cp++) {
1793 FREE(nfs_pub.np_index, M_TEMP);
1798 nfs_pub.np_mount = mp;
1799 nfs_pub.np_valid = 1;
1804 vfs_export_lookup(struct mount *mp, struct netexport *nep,
1805 struct sockaddr *nam)
1808 struct radix_node_head *rnh;
1809 struct sockaddr *saddr;
1812 if (mp->mnt_flag & MNT_EXPORTED) {
1814 * Lookup in the export list first.
1818 rnh = nep->ne_rtable[saddr->sa_family];
1820 np = (struct netcred *)
1821 (*rnh->rnh_matchaddr)((char *)saddr,
1823 if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
1828 * If no address match, use the default if it exists.
1830 if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
1831 np = &nep->ne_defexported;
1837 * perform msync on all vnodes under a mount point. The mount point must
1838 * be locked. This code is also responsible for lazy-freeing unreferenced
1839 * vnodes whos VM objects no longer contain pages.
1841 * NOTE: MNT_WAIT still skips vnodes in the VXLOCK state.
1843 * NOTE: XXX VOP_PUTPAGES and friends requires that the vnode be locked,
1844 * but vnode_pager_putpages() doesn't lock the vnode. We have to do it
1845 * way up in this high level function.
1847 static int vfs_msync_scan1(struct mount *mp, struct vnode *vp, void *data);
1848 static int vfs_msync_scan2(struct mount *mp, struct vnode *vp, void *data);
1851 vfs_msync(struct mount *mp, int flags)
1855 vmsc_flags = VMSC_GETVP;
1856 if (flags != MNT_WAIT)
1857 vmsc_flags |= VMSC_NOWAIT;
1858 vmntvnodescan(mp, vmsc_flags, vfs_msync_scan1, vfs_msync_scan2,
1863 * scan1 is a fast pre-check. There could be hundreds of thousands of
1864 * vnodes, we cannot afford to do anything heavy weight until we have a
1865 * fairly good indication that there is work to do.
1869 vfs_msync_scan1(struct mount *mp, struct vnode *vp, void *data)
1871 int flags = (int)data;
1873 if ((vp->v_flag & VRECLAIMED) == 0) {
1874 if (vshouldmsync(vp, 0))
1875 return(0); /* call scan2 */
1876 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
1877 (vp->v_flag & VOBJDIRTY) &&
1878 (flags == MNT_WAIT || vn_islocked(vp) == 0)) {
1879 return(0); /* call scan2 */
1884 * do not call scan2, continue the loop
1890 * This callback is handed a locked vnode.
1894 vfs_msync_scan2(struct mount *mp, struct vnode *vp, void *data)
1897 int flags = (int)data;
1899 if (vp->v_flag & VRECLAIMED)
1902 if ((mp->mnt_flag & MNT_RDONLY) == 0 && (vp->v_flag & VOBJDIRTY)) {
1903 if ((obj = vp->v_object) != NULL) {
1904 vm_object_page_clean(obj, 0, 0,
1905 flags == MNT_WAIT ? OBJPC_SYNC : OBJPC_NOSYNC);
1912 * Record a process's interest in events which might happen to
1913 * a vnode. Because poll uses the historic select-style interface
1914 * internally, this routine serves as both the ``check for any
1915 * pending events'' and the ``record my interest in future events''
1916 * functions. (These are done together, while the lock is held,
1917 * to avoid race conditions.)
1920 vn_pollrecord(struct vnode *vp, int events)
1924 KKASSERT(curthread->td_proc != NULL);
1926 lwkt_gettoken(&ilock, &vp->v_pollinfo.vpi_token);
1927 if (vp->v_pollinfo.vpi_revents & events) {
1929 * This leaves events we are not interested
1930 * in available for the other process which
1931 * which presumably had requested them
1932 * (otherwise they would never have been
1935 events &= vp->v_pollinfo.vpi_revents;
1936 vp->v_pollinfo.vpi_revents &= ~events;
1938 lwkt_reltoken(&ilock);
1941 vp->v_pollinfo.vpi_events |= events;
1942 selrecord(curthread, &vp->v_pollinfo.vpi_selinfo);
1943 lwkt_reltoken(&ilock);
1948 * Note the occurrence of an event. If the VN_POLLEVENT macro is used,
1949 * it is possible for us to miss an event due to race conditions, but
1950 * that condition is expected to be rare, so for the moment it is the
1951 * preferred interface.
1954 vn_pollevent(struct vnode *vp, int events)
1958 lwkt_gettoken(&ilock, &vp->v_pollinfo.vpi_token);
1959 if (vp->v_pollinfo.vpi_events & events) {
1961 * We clear vpi_events so that we don't
1962 * call selwakeup() twice if two events are
1963 * posted before the polling process(es) is
1964 * awakened. This also ensures that we take at
1965 * most one selwakeup() if the polling process
1966 * is no longer interested. However, it does
1967 * mean that only one event can be noticed at
1968 * a time. (Perhaps we should only clear those
1969 * event bits which we note?) XXX
1971 vp->v_pollinfo.vpi_events = 0; /* &= ~events ??? */
1972 vp->v_pollinfo.vpi_revents |= events;
1973 selwakeup(&vp->v_pollinfo.vpi_selinfo);
1975 lwkt_reltoken(&ilock);
1979 * Wake up anyone polling on vp because it is being revoked.
1980 * This depends on dead_poll() returning POLLHUP for correct
1984 vn_pollgone(struct vnode *vp)
1988 lwkt_gettoken(&ilock, &vp->v_pollinfo.vpi_token);
1989 if (vp->v_pollinfo.vpi_events) {
1990 vp->v_pollinfo.vpi_events = 0;
1991 selwakeup(&vp->v_pollinfo.vpi_selinfo);
1993 lwkt_reltoken(&ilock);
1997 * extract the dev_t from a VBLK or VCHR. The vnode must have been opened
1998 * (or v_rdev might be NULL).
2001 vn_todev(struct vnode *vp)
2003 if (vp->v_type != VBLK && vp->v_type != VCHR)
2005 KKASSERT(vp->v_rdev != NULL);
2006 return (vp->v_rdev);
2010 * Check if vnode represents a disk device. The vnode does not need to be
2014 vn_isdisk(struct vnode *vp, int *errp)
2018 if (vp->v_type != VBLK && vp->v_type != VCHR) {
2024 if ((dev = vp->v_rdev) == NULL)
2025 dev = udev2dev(vp->v_udev, (vp->v_type == VBLK));
2026 if (dev == NULL || dev == NODEV) {
2031 if (dev_is_good(dev) == 0) {
2036 if ((dev_dflags(dev) & D_DISK) == 0) {
2047 vn_get_namelen(struct vnode *vp, int *namelen)
2049 int error, retval[2];
2051 error = VOP_PATHCONF(vp, _PC_NAME_MAX, retval);
2059 vop_write_dirent(int *error, struct uio *uio, ino_t d_ino, uint8_t d_type,
2060 uint16_t d_namlen, const char *d_name)
2065 len = _DIRENT_RECLEN(d_namlen);
2066 if (len > uio->uio_resid)
2069 dp = malloc(len, M_TEMP, M_WAITOK | M_ZERO);
2072 dp->d_namlen = d_namlen;
2073 dp->d_type = d_type;
2074 bcopy(d_name, dp->d_name, d_namlen);
2076 *error = uiomove((caddr_t)dp, len, uio);