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. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)vfs_subr.c 8.31 (Berkeley) 5/26/95
35 * $FreeBSD: src/sys/kern/vfs_subr.c,v 1.249.2.30 2003/04/04 20:35:57 tegge Exp $
39 * External virtual filesystem routines
43 #include "opt_inet6.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
49 #include <sys/dirent.h>
50 #include <sys/eventhandler.h>
51 #include <sys/fcntl.h>
53 #include <sys/kernel.h>
54 #include <sys/kthread.h>
55 #include <sys/malloc.h>
57 #include <sys/mount.h>
60 #include <sys/reboot.h>
61 #include <sys/socket.h>
63 #include <sys/sysctl.h>
64 #include <sys/syslog.h>
65 #include <sys/unistd.h>
66 #include <sys/vmmeter.h>
67 #include <sys/vnode.h>
69 #include <machine/limits.h>
72 #include <vm/vm_object.h>
73 #include <vm/vm_extern.h>
74 #include <vm/vm_kern.h>
76 #include <vm/vm_map.h>
77 #include <vm/vm_page.h>
78 #include <vm/vm_pager.h>
79 #include <vm/vnode_pager.h>
80 #include <vm/vm_zone.h>
83 #include <sys/mplock2.h>
84 #include <vm/vm_page2.h>
86 #include <netinet/in.h>
88 static MALLOC_DEFINE(M_NETCRED, "Export Host", "Export host address structure");
91 SYSCTL_INT(_debug, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0,
92 "Number of vnodes allocated");
94 SYSCTL_INT(_debug, OID_AUTO, verbose_reclaims, CTLFLAG_RD, &verbose_reclaims, 0,
95 "Output filename of reclaimed vnode(s)");
97 enum vtype iftovt_tab[16] = {
98 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
99 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
101 int vttoif_tab[9] = {
102 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
103 S_IFSOCK, S_IFIFO, S_IFMT,
106 static int reassignbufcalls;
107 SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW, &reassignbufcalls,
108 0, "Number of times buffers have been reassigned to the proper list");
110 static int check_buf_overlap = 2; /* invasive check */
111 SYSCTL_INT(_vfs, OID_AUTO, check_buf_overlap, CTLFLAG_RW, &check_buf_overlap,
112 0, "Enable overlapping buffer checks");
114 int nfs_mount_type = -1;
115 static struct lwkt_token spechash_token;
116 struct nfs_public nfs_pub; /* publicly exported FS */
119 SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RW,
120 &maxvnodes, 0, "Maximum number of vnodes");
122 static struct radix_node_head *vfs_create_addrlist_af(int af,
123 struct netexport *nep);
124 static void vfs_free_addrlist (struct netexport *nep);
125 static int vfs_free_netcred (struct radix_node *rn, void *w);
126 static void vfs_free_addrlist_af (struct radix_node_head **prnh);
127 static int vfs_hang_addrlist (struct mount *mp, struct netexport *nep,
128 const struct export_args *argp);
130 int prtactive = 0; /* 1 => print out reclaim of active vnodes */
133 * Red black tree functions
135 static int rb_buf_compare(struct buf *b1, struct buf *b2);
136 RB_GENERATE2(buf_rb_tree, buf, b_rbnode, rb_buf_compare, off_t, b_loffset);
137 RB_GENERATE2(buf_rb_hash, buf, b_rbhash, rb_buf_compare, off_t, b_loffset);
140 rb_buf_compare(struct buf *b1, struct buf *b2)
142 if (b1->b_loffset < b2->b_loffset)
144 if (b1->b_loffset > b2->b_loffset)
150 * Initialize the vnode management data structures.
152 * Called from vfsinit()
161 * Desiredvnodes is kern.maxvnodes. We want to scale it
162 * according to available system memory but we may also have
163 * to limit it based on available KVM.
165 * WARNING! For machines with 64-256M of ram we have to be sure
166 * that the default limit scales down well due to HAMMER
167 * taking up significantly more memory per-vnode vs UFS.
168 * We want around ~5800 on a 128M machine.
170 * WARNING! Now that KVM is substantially larger (e.g. 8TB+),
171 * also limit maxvnodes based on a 128GB metric. This
172 * gives us something like ~3 millon vnodes. sysctl
173 * can be used to increase it further if desired.
175 * For disk cachhing purposes, filesystems like HAMMER1
176 * and HAMMER2 will or can be told to cache file data
177 * via the block device instead of excessively in vnodes.
179 factor1 = 25 * (sizeof(struct vm_object) + sizeof(struct vnode));
180 factor2 = 30 * (sizeof(struct vm_object) + sizeof(struct vnode));
181 maxvnodes = imin((int64_t)vmstats.v_page_count * PAGE_SIZE / factor1,
183 maxvnodes = imax(maxvnodes, maxproc * 8);
184 maxvnodes = imin(maxvnodes, 64LL*1024*1024*1024 / factor2);
186 lwkt_token_init(&spechash_token, "spechash");
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, "Precision of file timestamps");
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 /* va_*_uuid fields are only valid if related flags are set */
263 * Flush out and invalidate all buffers associated with a vnode.
267 static int vinvalbuf_bp(struct buf *bp, void *data);
269 struct vinvalbuf_bp_info {
278 vinvalbuf(struct vnode *vp, int flags, int slpflag, int slptimeo)
280 struct vinvalbuf_bp_info info;
284 lwkt_gettoken(&vp->v_token);
287 * If we are being asked to save, call fsync to ensure that the inode
290 if (flags & V_SAVE) {
291 error = bio_track_wait(&vp->v_track_write, slpflag, slptimeo);
294 if (!RB_EMPTY(&vp->v_rbdirty_tree)) {
295 if ((error = VOP_FSYNC(vp, MNT_WAIT, 0)) != 0)
299 * Dirty bufs may be left or generated via races
300 * in circumstances where vinvalbuf() is called on
301 * a vnode not undergoing reclamation. Only
302 * panic if we are trying to reclaim the vnode.
304 if ((vp->v_flag & VRECLAIMED) &&
305 (bio_track_active(&vp->v_track_write) ||
306 !RB_EMPTY(&vp->v_rbdirty_tree))) {
307 panic("vinvalbuf: dirty bufs");
312 info.slptimeo = slptimeo;
313 info.lkflags = LK_EXCLUSIVE | LK_SLEEPFAIL;
314 if (slpflag & PCATCH)
315 info.lkflags |= LK_PCATCH;
320 * Flush the buffer cache until nothing is left, wait for all I/O
321 * to complete. At least one pass is required. We might block
322 * in the pip code so we have to re-check. Order is important.
328 if (!RB_EMPTY(&vp->v_rbclean_tree)) {
330 error = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree,
331 NULL, vinvalbuf_bp, &info);
333 if (!RB_EMPTY(&vp->v_rbdirty_tree)) {
335 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
336 NULL, vinvalbuf_bp, &info);
340 * Wait for I/O completion.
342 bio_track_wait(&vp->v_track_write, 0, 0);
343 if ((object = vp->v_object) != NULL)
344 refcount_wait(&object->paging_in_progress, "vnvlbx");
345 } while (bio_track_active(&vp->v_track_write) ||
346 !RB_EMPTY(&vp->v_rbclean_tree) ||
347 !RB_EMPTY(&vp->v_rbdirty_tree));
350 * Destroy the copy in the VM cache, too.
352 if ((object = vp->v_object) != NULL) {
353 vm_object_page_remove(object, 0, 0,
354 (flags & V_SAVE) ? TRUE : FALSE);
357 if (!RB_EMPTY(&vp->v_rbdirty_tree) || !RB_EMPTY(&vp->v_rbclean_tree))
358 panic("vinvalbuf: flush failed");
359 if (!RB_EMPTY(&vp->v_rbhash_tree))
360 panic("vinvalbuf: flush failed, buffers still present");
363 lwkt_reltoken(&vp->v_token);
368 vinvalbuf_bp(struct buf *bp, void *data)
370 struct vinvalbuf_bp_info *info = data;
373 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
374 atomic_add_int(&bp->b_refs, 1);
375 error = BUF_TIMELOCK(bp, info->lkflags,
376 "vinvalbuf", info->slptimeo);
377 atomic_subtract_int(&bp->b_refs, 1);
386 KKASSERT(bp->b_vp == info->vp);
389 * Must check clean/dirty status after successfully locking as
392 if ((info->clean && (bp->b_flags & B_DELWRI)) ||
393 (info->clean == 0 && (bp->b_flags & B_DELWRI) == 0)) {
399 * NOTE: NO B_LOCKED CHECK. Also no buf_checkwrite()
400 * check. This code will write out the buffer, period.
403 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
404 (info->flags & V_SAVE)) {
406 } else if (info->flags & V_SAVE) {
408 * Cannot set B_NOCACHE on a clean buffer as this will
409 * destroy the VM backing store which might actually
410 * be dirty (and unsynchronized).
412 bp->b_flags |= (B_INVAL | B_RELBUF);
415 bp->b_flags |= (B_INVAL | B_NOCACHE | B_RELBUF);
422 * Truncate a file's buffer and pages to a specified length. This
423 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
426 * The vnode must be locked.
428 static int vtruncbuf_bp_trunc_cmp(struct buf *bp, void *data);
429 static int vtruncbuf_bp_trunc(struct buf *bp, void *data);
430 static int vtruncbuf_bp_metasync_cmp(struct buf *bp, void *data);
431 static int vtruncbuf_bp_metasync(struct buf *bp, void *data);
433 struct vtruncbuf_info {
440 vtruncbuf(struct vnode *vp, off_t length, int blksize)
442 struct vtruncbuf_info info;
443 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 info.truncloffset = length + (blksize - count);
454 info.truncloffset = length;
457 lwkt_gettoken(&vp->v_token);
460 count = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree,
461 vtruncbuf_bp_trunc_cmp,
462 vtruncbuf_bp_trunc, &info);
464 count += RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
465 vtruncbuf_bp_trunc_cmp,
466 vtruncbuf_bp_trunc, &info);
470 * For safety, fsync any remaining metadata if the file is not being
471 * truncated to 0. Since the metadata does not represent the entire
472 * dirty list we have to rely on the hit count to ensure that we get
477 count = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
478 vtruncbuf_bp_metasync_cmp,
479 vtruncbuf_bp_metasync, &info);
484 * Clean out any left over VM backing store.
486 * It is possible to have in-progress I/O from buffers that were
487 * not part of the truncation. This should not happen if we
488 * are truncating to 0-length.
490 vnode_pager_setsize(vp, length);
491 bio_track_wait(&vp->v_track_write, 0, 0);
496 spin_lock(&vp->v_spin);
497 filename = TAILQ_FIRST(&vp->v_namecache) ?
498 TAILQ_FIRST(&vp->v_namecache)->nc_name : "?";
499 spin_unlock(&vp->v_spin);
502 * Make sure no buffers were instantiated while we were trying
503 * to clean out the remaining VM pages. This could occur due
504 * to busy dirty VM pages being flushed out to disk.
508 count = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree,
509 vtruncbuf_bp_trunc_cmp,
510 vtruncbuf_bp_trunc, &info);
512 count += RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
513 vtruncbuf_bp_trunc_cmp,
514 vtruncbuf_bp_trunc, &info);
516 kprintf("Warning: vtruncbuf(): Had to re-clean %d "
517 "left over buffers in %s\n", count, filename);
521 lwkt_reltoken(&vp->v_token);
527 * The callback buffer is beyond the new file EOF and must be destroyed.
528 * Note that the compare function must conform to the RB_SCAN's requirements.
532 vtruncbuf_bp_trunc_cmp(struct buf *bp, void *data)
534 struct vtruncbuf_info *info = data;
536 if (bp->b_loffset >= info->truncloffset)
543 vtruncbuf_bp_trunc(struct buf *bp, void *data)
545 struct vtruncbuf_info *info = 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 * We must always revalidate the buffer after locking it to deal
555 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
556 atomic_add_int(&bp->b_refs, 1);
557 if (BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL) == 0)
559 atomic_subtract_int(&bp->b_refs, 1);
560 } else if ((info->clean && (bp->b_flags & B_DELWRI)) ||
561 (info->clean == 0 && (bp->b_flags & B_DELWRI) == 0) ||
562 bp->b_vp != info->vp ||
563 vtruncbuf_bp_trunc_cmp(bp, data)) {
567 bp->b_flags |= (B_INVAL | B_RELBUF | B_NOCACHE);
574 * Fsync all meta-data after truncating a file to be non-zero. Only metadata
575 * blocks (with a negative loffset) are scanned.
576 * Note that the compare function must conform to the RB_SCAN's requirements.
579 vtruncbuf_bp_metasync_cmp(struct buf *bp, void *data __unused)
581 if (bp->b_loffset < 0)
587 vtruncbuf_bp_metasync(struct buf *bp, void *data)
589 struct vtruncbuf_info *info = data;
591 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
592 atomic_add_int(&bp->b_refs, 1);
593 if (BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL) == 0)
595 atomic_subtract_int(&bp->b_refs, 1);
596 } else if ((bp->b_flags & B_DELWRI) == 0 ||
597 bp->b_vp != info->vp ||
598 vtruncbuf_bp_metasync_cmp(bp, data)) {
602 if (bp->b_vp == info->vp)
611 * vfsync - implements a multipass fsync on a file which understands
612 * dependancies and meta-data. The passed vnode must be locked. The
613 * waitfor argument may be MNT_WAIT or MNT_NOWAIT, or MNT_LAZY.
615 * When fsyncing data asynchronously just do one consolidated pass starting
616 * with the most negative block number. This may not get all the data due
619 * When fsyncing data synchronously do a data pass, then a metadata pass,
620 * then do additional data+metadata passes to try to get all the data out.
622 * Caller must ref the vnode but does not have to lock it.
624 static int vfsync_wait_output(struct vnode *vp,
625 int (*waitoutput)(struct vnode *, struct thread *));
626 static int vfsync_dummy_cmp(struct buf *bp __unused, void *data __unused);
627 static int vfsync_data_only_cmp(struct buf *bp, void *data);
628 static int vfsync_meta_only_cmp(struct buf *bp, void *data);
629 static int vfsync_lazy_range_cmp(struct buf *bp, void *data);
630 static int vfsync_bp(struct buf *bp, void *data);
640 int (*checkdef)(struct buf *);
641 int (*cmpfunc)(struct buf *, void *);
645 vfsync(struct vnode *vp, int waitfor, int passes,
646 int (*checkdef)(struct buf *),
647 int (*waitoutput)(struct vnode *, struct thread *))
649 struct vfsync_info info;
652 bzero(&info, sizeof(info));
654 if ((info.checkdef = checkdef) == NULL)
657 lwkt_gettoken(&vp->v_token);
660 case MNT_LAZY | MNT_NOWAIT:
663 * Lazy (filesystem syncer typ) Asynchronous plus limit the
664 * number of data (not meta) pages we try to flush to 1MB.
665 * A non-zero return means that lazy limit was reached.
667 info.lazylimit = 1024 * 1024;
669 info.cmpfunc = vfsync_lazy_range_cmp;
670 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
671 vfsync_lazy_range_cmp, vfsync_bp, &info);
672 info.cmpfunc = vfsync_meta_only_cmp;
673 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
674 vfsync_meta_only_cmp, vfsync_bp, &info);
677 else if (!RB_EMPTY(&vp->v_rbdirty_tree))
678 vn_syncer_add(vp, 1);
683 * Asynchronous. Do a data-only pass and a meta-only pass.
686 info.cmpfunc = vfsync_data_only_cmp;
687 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_data_only_cmp,
689 info.cmpfunc = vfsync_meta_only_cmp;
690 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_meta_only_cmp,
696 * Synchronous. Do a data-only pass, then a meta-data+data
697 * pass, then additional integrated passes to try to get
698 * all the dependancies flushed.
700 info.cmpfunc = vfsync_data_only_cmp;
702 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_data_only_cmp,
705 error = vfsync_wait_output(vp, waitoutput);
707 info.skippedbufs = 0;
708 info.cmpfunc = vfsync_dummy_cmp;
709 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
711 error = vfsync_wait_output(vp, waitoutput);
712 if (info.skippedbufs) {
713 kprintf("Warning: vfsync skipped %d dirty "
716 ((info.skippedbufs > 1) ? "s" : ""));
719 while (error == 0 && passes > 0 &&
720 !RB_EMPTY(&vp->v_rbdirty_tree)
722 info.skippedbufs = 0;
724 info.synchronous = 1;
727 info.cmpfunc = vfsync_dummy_cmp;
728 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
734 error = vfsync_wait_output(vp, waitoutput);
735 if (info.skippedbufs && passes == 0) {
736 kprintf("Warning: vfsync skipped %d dirty "
737 "buf%s in final pass!\n",
739 ((info.skippedbufs > 1) ? "s" : ""));
744 * This case can occur normally because vnode lock might
747 if (!RB_EMPTY(&vp->v_rbdirty_tree))
748 kprintf("dirty bufs left after final pass\n");
752 lwkt_reltoken(&vp->v_token);
758 vfsync_wait_output(struct vnode *vp,
759 int (*waitoutput)(struct vnode *, struct thread *))
763 error = bio_track_wait(&vp->v_track_write, 0, 0);
765 error = waitoutput(vp, curthread);
770 vfsync_dummy_cmp(struct buf *bp __unused, void *data __unused)
776 vfsync_data_only_cmp(struct buf *bp, void *data)
778 if (bp->b_loffset < 0)
784 vfsync_meta_only_cmp(struct buf *bp, void *data)
786 if (bp->b_loffset < 0)
792 vfsync_lazy_range_cmp(struct buf *bp, void *data)
794 struct vfsync_info *info = data;
796 if (bp->b_loffset < info->vp->v_lazyw)
802 vfsync_bp(struct buf *bp, void *data)
804 struct vfsync_info *info = data;
805 struct vnode *vp = info->vp;
808 if (info->fastpass) {
810 * Ignore buffers that we cannot immediately lock.
812 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
814 * Removed BUF_TIMELOCK(..., 1), even a 1-tick
815 * delay can mess up performance
817 * Another reason is that during a dirty-buffer
818 * scan a clustered write can start I/O on buffers
819 * ahead of the scan, causing the scan to not
820 * get a lock here. Usually this means the write
821 * is already in progress so, in fact, we *want*
822 * to skip the buffer.
827 } else if (info->synchronous == 0) {
829 * Normal pass, give the buffer a little time to become
832 if (BUF_TIMELOCK(bp, LK_EXCLUSIVE, "bflst2", hz / 10)) {
838 * Synchronous pass, give the buffer a lot of time before
841 if (BUF_TIMELOCK(bp, LK_EXCLUSIVE, "bflst3", hz * 10)) {
848 * We must revalidate the buffer after locking.
850 if ((bp->b_flags & B_DELWRI) == 0 ||
851 bp->b_vp != info->vp ||
852 info->cmpfunc(bp, data)) {
858 * If syncdeps is not set we do not try to write buffers which have
861 if (!info->synchronous && info->syncdeps == 0 && info->checkdef(bp)) {
867 * B_NEEDCOMMIT (primarily used by NFS) is a state where the buffer
868 * has been written but an additional handshake with the device
869 * is required before we can dispose of the buffer. We have no idea
870 * how to do this so we have to skip these buffers.
872 if (bp->b_flags & B_NEEDCOMMIT) {
878 * Ask bioops if it is ok to sync. If not the VFS may have
879 * set B_LOCKED so we have to cycle the buffer.
881 if (LIST_FIRST(&bp->b_dep) != NULL && buf_checkwrite(bp)) {
887 if (info->synchronous) {
889 * Synchronous flush. An error may be returned and will
896 * Asynchronous flush. We use the error return to support
899 * In low-memory situations we revert to synchronous
900 * operation. This should theoretically prevent the I/O
901 * path from exhausting memory in a non-recoverable way.
903 vp->v_lazyw = bp->b_loffset;
905 if (vm_page_count_min(0)) {
907 info->lazycount += bp->b_bufsize;
911 info->lazycount += cluster_awrite(bp);
912 waitrunningbufspace();
913 /*vm_wait_nominal();*/
915 if (info->lazylimit && info->lazycount >= info->lazylimit)
924 * Associate a buffer with a vnode.
929 bgetvp(struct vnode *vp, struct buf *bp, int testsize)
931 KASSERT(bp->b_vp == NULL, ("bgetvp: not free"));
932 KKASSERT((bp->b_flags & (B_HASHED|B_DELWRI|B_VNCLEAN|B_VNDIRTY)) == 0);
935 * Insert onto list for new vnode.
937 lwkt_gettoken(&vp->v_token);
939 if (buf_rb_hash_RB_INSERT(&vp->v_rbhash_tree, bp)) {
940 lwkt_reltoken(&vp->v_token);
945 * Diagnostics (mainly for HAMMER debugging). Check for
946 * overlapping buffers.
948 if (check_buf_overlap) {
950 bx = buf_rb_hash_RB_PREV(bp);
952 if (bx->b_loffset + bx->b_bufsize > bp->b_loffset) {
953 kprintf("bgetvp: overlapl %016jx/%d %016jx "
955 (intmax_t)bx->b_loffset,
957 (intmax_t)bp->b_loffset,
959 if (check_buf_overlap > 1)
960 panic("bgetvp - overlapping buffer");
963 bx = buf_rb_hash_RB_NEXT(bp);
965 if (bp->b_loffset + testsize > bx->b_loffset) {
966 kprintf("bgetvp: overlapr %016jx/%d %016jx "
968 (intmax_t)bp->b_loffset,
970 (intmax_t)bx->b_loffset,
972 if (check_buf_overlap > 1)
973 panic("bgetvp - overlapping buffer");
978 bp->b_flags |= B_HASHED;
979 bp->b_flags |= B_VNCLEAN;
980 if (buf_rb_tree_RB_INSERT(&vp->v_rbclean_tree, bp))
981 panic("reassignbuf: dup lblk/clean vp %p bp %p", vp, bp);
983 lwkt_reltoken(&vp->v_token);
988 * Disassociate a buffer from a vnode.
993 brelvp(struct buf *bp)
997 KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
1000 * Delete from old vnode list, if on one.
1003 lwkt_gettoken(&vp->v_token);
1004 if (bp->b_flags & (B_VNDIRTY | B_VNCLEAN)) {
1005 if (bp->b_flags & B_VNDIRTY)
1006 buf_rb_tree_RB_REMOVE(&vp->v_rbdirty_tree, bp);
1008 buf_rb_tree_RB_REMOVE(&vp->v_rbclean_tree, bp);
1009 bp->b_flags &= ~(B_VNDIRTY | B_VNCLEAN);
1011 if (bp->b_flags & B_HASHED) {
1012 buf_rb_hash_RB_REMOVE(&vp->v_rbhash_tree, bp);
1013 bp->b_flags &= ~B_HASHED;
1017 * Only remove from synclist when no dirty buffers are left AND
1018 * the VFS has not flagged the vnode's inode as being dirty.
1020 if ((vp->v_flag & (VONWORKLST | VISDIRTY | VOBJDIRTY)) == VONWORKLST &&
1021 RB_EMPTY(&vp->v_rbdirty_tree)) {
1022 vn_syncer_remove(vp, 0);
1026 lwkt_reltoken(&vp->v_token);
1032 * Reassign the buffer to the proper clean/dirty list based on B_DELWRI.
1033 * This routine is called when the state of the B_DELWRI bit is changed.
1035 * Must be called with vp->v_token held.
1039 reassignbuf(struct buf *bp)
1041 struct vnode *vp = bp->b_vp;
1044 ASSERT_LWKT_TOKEN_HELD(&vp->v_token);
1048 * B_PAGING flagged buffers cannot be reassigned because their vp
1049 * is not fully linked in.
1051 if (bp->b_flags & B_PAGING)
1052 panic("cannot reassign paging buffer");
1054 if (bp->b_flags & B_DELWRI) {
1056 * Move to the dirty list, add the vnode to the worklist
1058 if (bp->b_flags & B_VNCLEAN) {
1059 buf_rb_tree_RB_REMOVE(&vp->v_rbclean_tree, bp);
1060 bp->b_flags &= ~B_VNCLEAN;
1062 if ((bp->b_flags & B_VNDIRTY) == 0) {
1063 if (buf_rb_tree_RB_INSERT(&vp->v_rbdirty_tree, bp)) {
1064 panic("reassignbuf: dup lblk vp %p bp %p",
1067 bp->b_flags |= B_VNDIRTY;
1069 if ((vp->v_flag & VONWORKLST) == 0) {
1070 switch (vp->v_type) {
1077 vp->v_rdev->si_mountpoint != NULL) {
1085 vn_syncer_add(vp, delay);
1089 * Move to the clean list, remove the vnode from the worklist
1090 * if no dirty blocks remain.
1092 if (bp->b_flags & B_VNDIRTY) {
1093 buf_rb_tree_RB_REMOVE(&vp->v_rbdirty_tree, bp);
1094 bp->b_flags &= ~B_VNDIRTY;
1096 if ((bp->b_flags & B_VNCLEAN) == 0) {
1097 if (buf_rb_tree_RB_INSERT(&vp->v_rbclean_tree, bp)) {
1098 panic("reassignbuf: dup lblk vp %p bp %p",
1101 bp->b_flags |= B_VNCLEAN;
1105 * Only remove from synclist when no dirty buffers are left
1106 * AND the VFS has not flagged the vnode's inode as being
1109 if ((vp->v_flag & (VONWORKLST | VISDIRTY | VOBJDIRTY)) ==
1111 RB_EMPTY(&vp->v_rbdirty_tree)) {
1112 vn_syncer_remove(vp, 0);
1118 * Create a vnode for a block device. Used for mounting the root file
1121 * A vref()'d vnode is returned.
1123 extern struct vop_ops *devfs_vnode_dev_vops_p;
1125 bdevvp(cdev_t dev, struct vnode **vpp)
1135 error = getspecialvnode(VT_NON, NULL, &devfs_vnode_dev_vops_p,
1146 v_associate_rdev(vp, dev);
1147 vp->v_umajor = dev->si_umajor;
1148 vp->v_uminor = dev->si_uminor;
1155 v_associate_rdev(struct vnode *vp, cdev_t dev)
1159 if (dev_is_good(dev) == 0)
1161 KKASSERT(vp->v_rdev == NULL);
1162 vp->v_rdev = reference_dev(dev);
1163 lwkt_gettoken(&spechash_token);
1164 SLIST_INSERT_HEAD(&dev->si_hlist, vp, v_cdevnext);
1165 lwkt_reltoken(&spechash_token);
1170 v_release_rdev(struct vnode *vp)
1174 if ((dev = vp->v_rdev) != NULL) {
1175 lwkt_gettoken(&spechash_token);
1176 SLIST_REMOVE(&dev->si_hlist, vp, vnode, v_cdevnext);
1179 lwkt_reltoken(&spechash_token);
1184 * Add a vnode to the alias list hung off the cdev_t. We only associate
1185 * the device number with the vnode. The actual device is not associated
1186 * until the vnode is opened (usually in spec_open()), and will be
1187 * disassociated on last close.
1190 addaliasu(struct vnode *nvp, int x, int y)
1192 if (nvp->v_type != VBLK && nvp->v_type != VCHR)
1193 panic("addaliasu on non-special vnode");
1199 * Simple call that a filesystem can make to try to get rid of a
1200 * vnode. It will fail if anyone is referencing the vnode (including
1203 * The filesystem can check whether its in-memory inode structure still
1204 * references the vp on return.
1206 * May only be called if the vnode is in a known state (i.e. being prevented
1207 * from being deallocated by some other condition such as a vfs inode hold).
1210 vclean_unlocked(struct vnode *vp)
1213 if (VREFCNT(vp) <= 1)
1219 * Disassociate a vnode from its underlying filesystem.
1221 * The vnode must be VX locked and referenced. In all normal situations
1222 * there are no active references. If vclean_vxlocked() is called while
1223 * there are active references, the vnode is being ripped out and we have
1224 * to call VOP_CLOSE() as appropriate before we can reclaim it.
1227 vclean_vxlocked(struct vnode *vp, int flags)
1232 struct namecache *ncp;
1235 * If the vnode has already been reclaimed we have nothing to do.
1237 if (vp->v_flag & VRECLAIMED)
1241 * Set flag to interlock operation, flag finalization to ensure
1242 * that the vnode winds up on the inactive list, and set v_act to 0.
1244 vsetflags(vp, VRECLAIMED);
1245 atomic_set_int(&vp->v_refcnt, VREF_FINALIZE);
1248 if (verbose_reclaims) {
1249 if ((ncp = TAILQ_FIRST(&vp->v_namecache)) != NULL)
1250 kprintf("Debug: reclaim %p %s\n", vp, ncp->nc_name);
1254 * Scrap the vfs cache
1256 while (cache_inval_vp(vp, 0) != 0) {
1257 kprintf("Warning: vnode %p clean/cache_resolution "
1258 "race detected\n", vp);
1259 tsleep(vp, 0, "vclninv", 2);
1263 * Check to see if the vnode is in use. If so we have to reference it
1264 * before we clean it out so that its count cannot fall to zero and
1265 * generate a race against ourselves to recycle it.
1267 active = (VREFCNT(vp) > 0);
1270 * Clean out any buffers associated with the vnode and destroy its
1271 * object, if it has one.
1273 vinvalbuf(vp, V_SAVE, 0, 0);
1276 * If purging an active vnode (typically during a forced unmount
1277 * or reboot), it must be closed and deactivated before being
1278 * reclaimed. This isn't really all that safe, but what can
1281 * Note that neither of these routines unlocks the vnode.
1283 if (active && (flags & DOCLOSE)) {
1284 while ((n = vp->v_opencount) != 0) {
1285 if (vp->v_writecount)
1286 VOP_CLOSE(vp, FWRITE|FNONBLOCK, NULL);
1288 VOP_CLOSE(vp, FNONBLOCK, NULL);
1289 if (vp->v_opencount == n) {
1290 kprintf("Warning: unable to force-close"
1298 * If the vnode has not been deactivated, deactivated it. Deactivation
1299 * can create new buffers and VM pages so we have to call vinvalbuf()
1300 * again to make sure they all get flushed.
1302 * This can occur if a file with a link count of 0 needs to be
1305 * If the vnode is already dead don't try to deactivate it.
1307 if ((vp->v_flag & VINACTIVE) == 0) {
1308 vsetflags(vp, VINACTIVE);
1311 vinvalbuf(vp, V_SAVE, 0, 0);
1315 * If the vnode has an object, destroy it.
1317 while ((object = vp->v_object) != NULL) {
1318 vm_object_hold(object);
1319 if (object == vp->v_object)
1321 vm_object_drop(object);
1324 if (object != NULL) {
1325 if (object->ref_count == 0) {
1326 if ((object->flags & OBJ_DEAD) == 0)
1327 vm_object_terminate(object);
1328 vm_object_drop(object);
1329 vclrflags(vp, VOBJBUF);
1331 vm_pager_deallocate(object);
1332 vclrflags(vp, VOBJBUF);
1333 vm_object_drop(object);
1336 KKASSERT((vp->v_flag & VOBJBUF) == 0);
1338 if (vp->v_flag & VOBJDIRTY)
1342 * Reclaim the vnode if not already dead.
1344 if (vp->v_mount && VOP_RECLAIM(vp))
1345 panic("vclean: cannot reclaim");
1348 * Done with purge, notify sleepers of the grim news.
1350 vp->v_ops = &dead_vnode_vops_p;
1355 * If we are destroying an active vnode, reactivate it now that
1356 * we have reassociated it with deadfs. This prevents the system
1357 * from crashing on the vnode due to it being unexpectedly marked
1358 * as inactive or reclaimed.
1360 if (active && (flags & DOCLOSE)) {
1361 vclrflags(vp, VINACTIVE | VRECLAIMED);
1366 * Eliminate all activity associated with the requested vnode
1367 * and with all vnodes aliased to the requested vnode.
1369 * The vnode must be referenced but should not be locked.
1372 vrevoke(struct vnode *vp, struct ucred *cred)
1380 * If the vnode has a device association, scrap all vnodes associated
1381 * with the device. Don't let the device disappear on us while we
1382 * are scrapping the vnodes.
1384 * The passed vp will probably show up in the list, do not VX lock
1387 * Releasing the vnode's rdev here can mess up specfs's call to
1388 * device close, so don't do it. The vnode has been disassociated
1389 * and the device will be closed after the last ref on the related
1390 * fp goes away (if not still open by e.g. the kernel).
1392 if (vp->v_type != VCHR) {
1393 error = fdrevoke(vp, DTYPE_VNODE, cred);
1396 if ((dev = vp->v_rdev) == NULL) {
1400 lwkt_gettoken(&spechash_token);
1403 vqn = SLIST_FIRST(&dev->si_hlist);
1406 while ((vq = vqn) != NULL) {
1407 if (VREFCNT(vq) > 0) {
1409 fdrevoke(vq, DTYPE_VNODE, cred);
1410 /*v_release_rdev(vq);*/
1412 if (vq->v_rdev != dev) {
1417 vqn = SLIST_NEXT(vq, v_cdevnext);
1422 lwkt_reltoken(&spechash_token);
1429 * This is called when the object underlying a vnode is being destroyed,
1430 * such as in a remove(). Try to recycle the vnode immediately if the
1431 * only active reference is our reference.
1433 * Directory vnodes in the namecache with children cannot be immediately
1434 * recycled because numerous VOP_N*() ops require them to be stable.
1436 * To avoid recursive recycling from VOP_INACTIVE implemenetations this
1437 * function is a NOP if VRECLAIMED is already set.
1440 vrecycle(struct vnode *vp)
1442 if (VREFCNT(vp) <= 1 && (vp->v_flag & VRECLAIMED) == 0) {
1443 if (cache_inval_vp_nonblock(vp))
1452 * Return the maximum I/O size allowed for strategy calls on VP.
1454 * If vp is VCHR or VBLK we dive the device, otherwise we use
1455 * the vp's mount info.
1457 * The returned value is clamped at MAXPHYS as most callers cannot use
1458 * buffers larger than that size.
1461 vmaxiosize(struct vnode *vp)
1465 if (vp->v_type == VBLK || vp->v_type == VCHR)
1466 maxiosize = vp->v_rdev->si_iosize_max;
1468 maxiosize = vp->v_mount->mnt_iosize_max;
1470 if (maxiosize > MAXPHYS)
1471 maxiosize = MAXPHYS;
1476 * Eliminate all activity associated with a vnode in preparation for
1479 * The vnode must be VX locked and refd and will remain VX locked and refd
1480 * on return. This routine may be called with the vnode in any state, as
1481 * long as it is VX locked. The vnode will be cleaned out and marked
1482 * VRECLAIMED but will not actually be reused until all existing refs and
1485 * NOTE: This routine may be called on a vnode which has not yet been
1486 * already been deactivated (VOP_INACTIVE), or on a vnode which has
1487 * already been reclaimed.
1489 * This routine is not responsible for placing us back on the freelist.
1490 * Instead, it happens automatically when the caller releases the VX lock
1491 * (assuming there aren't any other references).
1494 vgone_vxlocked(struct vnode *vp)
1497 * assert that the VX lock is held. This is an absolute requirement
1498 * now for vgone_vxlocked() to be called.
1500 KKASSERT(lockinuse(&vp->v_lock));
1503 * Clean out the filesystem specific data and set the VRECLAIMED
1504 * bit. Also deactivate the vnode if necessary.
1506 * The vnode should have automatically been removed from the syncer
1507 * list as syncer/dirty flags cleared during the cleaning.
1509 vclean_vxlocked(vp, DOCLOSE);
1512 * Normally panic if the vnode is still dirty, unless we are doing
1513 * a forced unmount (tmpfs typically).
1515 if (vp->v_flag & VONWORKLST) {
1516 if (vp->v_mount->mnt_kern_flag & MNTK_UNMOUNTF) {
1518 vn_syncer_remove(vp, 1);
1520 panic("vp %p still dirty in vgone after flush", vp);
1525 * Delete from old mount point vnode list, if on one.
1527 if (vp->v_mount != NULL) {
1528 KKASSERT(vp->v_data == NULL);
1529 insmntque(vp, NULL);
1533 * If special device, remove it from special device alias list
1534 * if it is on one. This should normally only occur if a vnode is
1535 * being revoked as the device should otherwise have been released
1538 if ((vp->v_type == VBLK || vp->v_type == VCHR) && vp->v_rdev != NULL) {
1549 * Lookup a vnode by device number.
1551 * Returns non-zero and *vpp set to a vref'd vnode on success.
1552 * Returns zero on failure.
1555 vfinddev(cdev_t dev, enum vtype type, struct vnode **vpp)
1559 lwkt_gettoken(&spechash_token);
1560 SLIST_FOREACH(vp, &dev->si_hlist, v_cdevnext) {
1561 if (type == vp->v_type) {
1564 lwkt_reltoken(&spechash_token);
1568 lwkt_reltoken(&spechash_token);
1573 * Calculate the total number of references to a special device. This
1574 * routine may only be called for VBLK and VCHR vnodes since v_rdev is
1575 * an overloaded field. Since udev2dev can now return NULL, we have
1576 * to check for a NULL v_rdev.
1579 count_dev(cdev_t dev)
1584 if (SLIST_FIRST(&dev->si_hlist)) {
1585 lwkt_gettoken(&spechash_token);
1586 SLIST_FOREACH(vp, &dev->si_hlist, v_cdevnext) {
1587 count += vp->v_opencount;
1589 lwkt_reltoken(&spechash_token);
1595 vcount(struct vnode *vp)
1597 if (vp->v_rdev == NULL)
1599 return(count_dev(vp->v_rdev));
1603 * Initialize VMIO for a vnode. This routine MUST be called before a
1604 * VFS can issue buffer cache ops on a vnode. It is typically called
1605 * when a vnode is initialized from its inode.
1608 vinitvmio(struct vnode *vp, off_t filesize, int blksize, int boff)
1613 object = vp->v_object;
1615 vm_object_hold(object);
1616 KKASSERT(vp->v_object == object);
1619 if (object == NULL) {
1620 object = vnode_pager_alloc(vp, filesize, 0, 0, blksize, boff);
1623 * Dereference the reference we just created. This assumes
1624 * that the object is associated with the vp. Allow it to
1625 * have zero refs. It cannot be destroyed as long as it
1626 * is associated with the vnode.
1628 vm_object_hold(object);
1629 atomic_add_int(&object->ref_count, -1);
1632 KKASSERT((object->flags & OBJ_DEAD) == 0);
1634 KASSERT(vp->v_object != NULL, ("vinitvmio: NULL object"));
1635 vsetflags(vp, VOBJBUF);
1636 vm_object_drop(object);
1643 * Print out a description of a vnode.
1645 static char *typename[] =
1646 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD"};
1649 vprint(char *label, struct vnode *vp)
1654 kprintf("%s: %p: ", label, (void *)vp);
1656 kprintf("%p: ", (void *)vp);
1657 kprintf("type %s, refcnt %08x, writecount %d, holdcnt %d,",
1658 typename[vp->v_type],
1659 vp->v_refcnt, vp->v_writecount, vp->v_auxrefs);
1661 if (vp->v_flag & VROOT)
1662 strcat(buf, "|VROOT");
1663 if (vp->v_flag & VPFSROOT)
1664 strcat(buf, "|VPFSROOT");
1665 if (vp->v_flag & VTEXT)
1666 strcat(buf, "|VTEXT");
1667 if (vp->v_flag & VSYSTEM)
1668 strcat(buf, "|VSYSTEM");
1669 if (vp->v_flag & VOBJBUF)
1670 strcat(buf, "|VOBJBUF");
1672 kprintf(" flags (%s)", &buf[1]);
1673 if (vp->v_data == NULL) {
1682 * Do the usual access checking.
1683 * file_mode, uid and gid are from the vnode in question,
1684 * while acc_mode and cred are from the VOP_ACCESS parameter list
1687 vaccess(enum vtype type, mode_t file_mode, uid_t uid, gid_t gid,
1688 mode_t acc_mode, struct ucred *cred)
1694 * Super-user always gets read/write access, but execute access depends
1695 * on at least one execute bit being set.
1697 if (priv_check_cred(cred, PRIV_ROOT, 0) == 0) {
1698 if ((acc_mode & VEXEC) && type != VDIR &&
1699 (file_mode & (S_IXUSR|S_IXGRP|S_IXOTH)) == 0)
1706 /* Otherwise, check the owner. */
1707 if (cred->cr_uid == uid) {
1708 if (acc_mode & VEXEC)
1710 if (acc_mode & VREAD)
1712 if (acc_mode & VWRITE)
1714 return ((file_mode & mask) == mask ? 0 : EACCES);
1717 /* Otherwise, check the groups. */
1718 ismember = groupmember(gid, cred);
1719 if (cred->cr_svgid == gid || ismember) {
1720 if (acc_mode & VEXEC)
1722 if (acc_mode & VREAD)
1724 if (acc_mode & VWRITE)
1726 return ((file_mode & mask) == mask ? 0 : EACCES);
1729 /* Otherwise, check everyone else. */
1730 if (acc_mode & VEXEC)
1732 if (acc_mode & VREAD)
1734 if (acc_mode & VWRITE)
1736 return ((file_mode & mask) == mask ? 0 : EACCES);
1740 #include <ddb/ddb.h>
1742 static int db_show_locked_vnodes(struct mount *mp, void *data);
1745 * List all of the locked vnodes in the system.
1746 * Called when debugging the kernel.
1748 DB_SHOW_COMMAND(lockedvnodes, lockedvnodes)
1750 kprintf("Locked vnodes\n");
1751 mountlist_scan(db_show_locked_vnodes, NULL,
1752 MNTSCAN_FORWARD|MNTSCAN_NOBUSY);
1756 db_show_locked_vnodes(struct mount *mp, void *data __unused)
1760 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
1761 if (vn_islocked(vp))
1769 * Top level filesystem related information gathering.
1771 static int sysctl_ovfs_conf (SYSCTL_HANDLER_ARGS);
1774 vfs_sysctl(SYSCTL_HANDLER_ARGS)
1776 int *name = (int *)arg1 - 1; /* XXX */
1777 u_int namelen = arg2 + 1; /* XXX */
1778 struct vfsconf *vfsp;
1781 #if 1 || defined(COMPAT_PRELITE2)
1782 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
1784 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
1788 /* all sysctl names at this level are at least name and field */
1790 return (ENOTDIR); /* overloaded */
1791 if (name[0] != VFS_GENERIC) {
1792 vfsp = vfsconf_find_by_typenum(name[0]);
1794 return (EOPNOTSUPP);
1795 return ((*vfsp->vfc_vfsops->vfs_sysctl)(&name[1], namelen - 1,
1796 oldp, oldlenp, newp, newlen, p));
1800 case VFS_MAXTYPENUM:
1803 maxtypenum = vfsconf_get_maxtypenum();
1804 return (SYSCTL_OUT(req, &maxtypenum, sizeof(maxtypenum)));
1807 return (ENOTDIR); /* overloaded */
1808 vfsp = vfsconf_find_by_typenum(name[2]);
1810 return (EOPNOTSUPP);
1811 return (SYSCTL_OUT(req, vfsp, sizeof *vfsp));
1813 return (EOPNOTSUPP);
1816 SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD, vfs_sysctl,
1817 "Generic filesystem");
1819 #if 1 || defined(COMPAT_PRELITE2)
1822 sysctl_ovfs_conf_iter(struct vfsconf *vfsp, void *data)
1825 struct ovfsconf ovfs;
1826 struct sysctl_req *req = (struct sysctl_req*) data;
1828 bzero(&ovfs, sizeof(ovfs));
1829 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
1830 strcpy(ovfs.vfc_name, vfsp->vfc_name);
1831 ovfs.vfc_index = vfsp->vfc_typenum;
1832 ovfs.vfc_refcount = vfsp->vfc_refcount;
1833 ovfs.vfc_flags = vfsp->vfc_flags;
1834 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
1836 return error; /* abort iteration with error code */
1838 return 0; /* continue iterating with next element */
1842 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
1844 return vfsconf_each(sysctl_ovfs_conf_iter, (void*)req);
1847 #endif /* 1 || COMPAT_PRELITE2 */
1850 * Check to see if a filesystem is mounted on a block device.
1853 vfs_mountedon(struct vnode *vp)
1857 if ((dev = vp->v_rdev) == NULL) {
1858 /* if (vp->v_type != VBLK)
1859 dev = get_dev(vp->v_uminor, vp->v_umajor); */
1861 if (dev != NULL && dev->si_mountpoint)
1867 * Unmount all filesystems. The list is traversed in reverse order
1868 * of mounting to avoid dependencies.
1870 * We want the umountall to be able to break out of its loop if a
1871 * failure occurs, after scanning all possible mounts, so the callback
1872 * returns 0 on error.
1874 * NOTE: Do not call mountlist_remove(mp) on error any more, this will
1875 * confuse mountlist_scan()'s unbusy check.
1877 static int vfs_umountall_callback(struct mount *mp, void *data);
1880 vfs_unmountall(int halting)
1885 count = mountlist_scan(vfs_umountall_callback, &halting,
1886 MNTSCAN_REVERSE|MNTSCAN_NOBUSY);
1892 vfs_umountall_callback(struct mount *mp, void *data)
1895 int halting = *(int *)data;
1898 * NOTE: When halting, dounmount will disconnect but leave
1899 * certain mount points intact. e.g. devfs.
1901 error = dounmount(mp, MNT_FORCE, halting);
1903 kprintf("unmount of filesystem mounted from %s failed (",
1904 mp->mnt_stat.f_mntfromname);
1908 kprintf("%d)\n", error);
1916 * Checks the mount flags for parameter mp and put the names comma-separated
1917 * into a string buffer buf with a size limit specified by len.
1919 * It returns the number of bytes written into buf, and (*errorp) will be
1920 * set to 0, EINVAL (if passed length is 0), or ENOSPC (supplied buffer was
1921 * not large enough). The buffer will be 0-terminated if len was not 0.
1924 vfs_flagstostr(int flags, const struct mountctl_opt *optp,
1925 char *buf, size_t len, int *errorp)
1927 static const struct mountctl_opt optnames[] = {
1928 { MNT_RDONLY, "read-only" },
1929 { MNT_SYNCHRONOUS, "synchronous" },
1930 { MNT_NOEXEC, "noexec" },
1931 { MNT_NOSUID, "nosuid" },
1932 { MNT_NODEV, "nodev" },
1933 { MNT_AUTOMOUNTED, "automounted" },
1934 { MNT_ASYNC, "asynchronous" },
1935 { MNT_SUIDDIR, "suiddir" },
1936 { MNT_SOFTDEP, "soft-updates" },
1937 { MNT_NOSYMFOLLOW, "nosymfollow" },
1938 { MNT_TRIM, "trim" },
1939 { MNT_NOATIME, "noatime" },
1940 { MNT_NOCLUSTERR, "noclusterr" },
1941 { MNT_NOCLUSTERW, "noclusterw" },
1942 { MNT_EXRDONLY, "NFS read-only" },
1943 { MNT_EXPORTED, "NFS exported" },
1944 /* Remaining NFS flags could come here */
1945 { MNT_LOCAL, "local" },
1946 { MNT_QUOTA, "with-quotas" },
1947 /* { MNT_ROOTFS, "rootfs" }, */
1948 /* { MNT_IGNORE, "ignore" }, */
1958 bleft = len - 1; /* leave room for trailing \0 */
1961 * Checks the size of the string. If it contains
1962 * any data, then we will append the new flags to
1965 actsize = strlen(buf);
1969 /* Default flags if no flags passed */
1973 if (bleft < 0) { /* degenerate case, 0-length buffer */
1978 for (; flags && optp->o_opt; ++optp) {
1979 if ((flags & optp->o_opt) == 0)
1981 optlen = strlen(optp->o_name);
1982 if (bwritten || actsize > 0) {
1987 buf[bwritten++] = ',';
1988 buf[bwritten++] = ' ';
1991 if (bleft < optlen) {
1995 bcopy(optp->o_name, buf + bwritten, optlen);
1998 flags &= ~optp->o_opt;
2002 * Space already reserved for trailing \0
2009 * Build hash lists of net addresses and hang them off the mount point.
2010 * Called by ufs_mount() to set up the lists of export addresses.
2013 vfs_hang_addrlist(struct mount *mp, struct netexport *nep,
2014 const struct export_args *argp)
2017 struct radix_node_head *rnh;
2019 struct radix_node *rn;
2020 struct sockaddr *saddr, *smask = NULL;
2023 if (argp->ex_addrlen == 0) {
2024 if (mp->mnt_flag & MNT_DEFEXPORTED)
2026 np = &nep->ne_defexported;
2027 np->netc_exflags = argp->ex_flags;
2028 np->netc_anon = argp->ex_anon;
2029 np->netc_anon.cr_ref = 1;
2030 mp->mnt_flag |= MNT_DEFEXPORTED;
2034 if (argp->ex_addrlen < 0 || argp->ex_addrlen > MLEN)
2036 if (argp->ex_masklen < 0 || argp->ex_masklen > MLEN)
2039 i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
2040 np = (struct netcred *)kmalloc(i, M_NETCRED, M_WAITOK | M_ZERO);
2041 saddr = (struct sockaddr *) (np + 1);
2042 if ((error = copyin(argp->ex_addr, (caddr_t) saddr, argp->ex_addrlen)))
2044 if (saddr->sa_len > argp->ex_addrlen)
2045 saddr->sa_len = argp->ex_addrlen;
2046 if (argp->ex_masklen) {
2047 smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen);
2048 error = copyin(argp->ex_mask, (caddr_t)smask, argp->ex_masklen);
2051 if (smask->sa_len > argp->ex_masklen)
2052 smask->sa_len = argp->ex_masklen;
2055 if (nep->ne_maskhead == NULL) {
2056 if (!rn_inithead((void **)&nep->ne_maskhead, NULL, 0)) {
2061 if ((rnh = vfs_create_addrlist_af(saddr->sa_family, nep)) == NULL) {
2065 rn = (*rnh->rnh_addaddr)((char *)saddr, (char *)smask, rnh,
2068 if (rn == NULL || np != (struct netcred *)rn) { /* already exists */
2072 np->netc_exflags = argp->ex_flags;
2073 np->netc_anon = argp->ex_anon;
2074 np->netc_anon.cr_ref = 1;
2078 kfree(np, M_NETCRED);
2083 * Free netcred structures installed in the netexport
2086 vfs_free_netcred(struct radix_node *rn, void *w)
2088 struct radix_node_head *rnh = (struct radix_node_head *)w;
2090 (*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, rnh);
2091 kfree(rn, M_NETCRED);
2097 * callback to free an element of the mask table installed in the
2098 * netexport. These may be created indirectly and are not netcred
2102 vfs_free_netcred_mask(struct radix_node *rn, void *w)
2104 struct radix_node_head *rnh = (struct radix_node_head *)w;
2106 (*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, rnh);
2107 kfree(rn, M_RTABLE);
2112 static struct radix_node_head *
2113 vfs_create_addrlist_af(int af, struct netexport *nep)
2115 struct radix_node_head *rnh = NULL;
2116 #if defined(INET) || defined(INET6)
2117 struct radix_node_head *maskhead = nep->ne_maskhead;
2121 NE_ASSERT_LOCKED(nep);
2122 #if defined(INET) || defined(INET6)
2123 KKASSERT(maskhead != NULL);
2128 if ((rnh = nep->ne_inethead) == NULL) {
2129 off = offsetof(struct sockaddr_in, sin_addr) << 3;
2130 if (!rn_inithead((void **)&rnh, maskhead, off))
2132 nep->ne_inethead = rnh;
2138 if ((rnh = nep->ne_inet6head) == NULL) {
2139 off = offsetof(struct sockaddr_in6, sin6_addr) << 3;
2140 if (!rn_inithead((void **)&rnh, maskhead, off))
2142 nep->ne_inet6head = rnh;
2151 * helper function for freeing netcred elements
2154 vfs_free_addrlist_af(struct radix_node_head **prnh)
2156 struct radix_node_head *rnh = *prnh;
2158 (*rnh->rnh_walktree) (rnh, vfs_free_netcred, rnh);
2159 kfree(rnh, M_RTABLE);
2164 * helper function for freeing mask elements
2167 vfs_free_addrlist_masks(struct radix_node_head **prnh)
2169 struct radix_node_head *rnh = *prnh;
2171 (*rnh->rnh_walktree) (rnh, vfs_free_netcred_mask, rnh);
2172 kfree(rnh, M_RTABLE);
2177 * Free the net address hash lists that are hanging off the mount points.
2180 vfs_free_addrlist(struct netexport *nep)
2183 if (nep->ne_inethead != NULL)
2184 vfs_free_addrlist_af(&nep->ne_inethead);
2185 if (nep->ne_inet6head != NULL)
2186 vfs_free_addrlist_af(&nep->ne_inet6head);
2187 if (nep->ne_maskhead)
2188 vfs_free_addrlist_masks(&nep->ne_maskhead);
2193 vfs_export(struct mount *mp, struct netexport *nep,
2194 const struct export_args *argp)
2198 if (argp->ex_flags & MNT_DELEXPORT) {
2199 if (mp->mnt_flag & MNT_EXPUBLIC) {
2200 vfs_setpublicfs(NULL, NULL, NULL);
2201 mp->mnt_flag &= ~MNT_EXPUBLIC;
2203 vfs_free_addrlist(nep);
2204 mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
2206 if (argp->ex_flags & MNT_EXPORTED) {
2207 if (argp->ex_flags & MNT_EXPUBLIC) {
2208 if ((error = vfs_setpublicfs(mp, nep, argp)) != 0)
2210 mp->mnt_flag |= MNT_EXPUBLIC;
2212 if ((error = vfs_hang_addrlist(mp, nep, argp)))
2214 mp->mnt_flag |= MNT_EXPORTED;
2221 * Set the publicly exported filesystem (WebNFS). Currently, only
2222 * one public filesystem is possible in the spec (RFC 2054 and 2055)
2225 vfs_setpublicfs(struct mount *mp, struct netexport *nep,
2226 const struct export_args *argp)
2233 * mp == NULL -> invalidate the current info, the FS is
2234 * no longer exported. May be called from either vfs_export
2235 * or unmount, so check if it hasn't already been done.
2238 if (nfs_pub.np_valid) {
2239 nfs_pub.np_valid = 0;
2240 if (nfs_pub.np_index != NULL) {
2241 kfree(nfs_pub.np_index, M_TEMP);
2242 nfs_pub.np_index = NULL;
2249 * Only one allowed at a time.
2251 if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
2255 * Get real filehandle for root of exported FS.
2257 bzero((caddr_t)&nfs_pub.np_handle, sizeof(nfs_pub.np_handle));
2258 nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid;
2260 if ((error = VFS_ROOT(mp, &rvp)))
2263 if ((error = VFS_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid)))
2269 * If an indexfile was specified, pull it in.
2271 if (argp->ex_indexfile != NULL) {
2274 error = vn_get_namelen(rvp, &namelen);
2277 nfs_pub.np_index = kmalloc(namelen, M_TEMP, M_WAITOK);
2278 error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
2282 * Check for illegal filenames.
2284 for (cp = nfs_pub.np_index; *cp; cp++) {
2292 kfree(nfs_pub.np_index, M_TEMP);
2297 nfs_pub.np_mount = mp;
2298 nfs_pub.np_valid = 1;
2303 vfs_export_lookup(struct mount *mp, struct netexport *nep,
2304 struct sockaddr *nam)
2307 struct radix_node_head *rnh;
2308 struct sockaddr *saddr;
2311 if (mp->mnt_flag & MNT_EXPORTED) {
2313 * Lookup in the export list first.
2318 switch (saddr->sa_family) {
2321 rnh = nep->ne_inethead;
2326 rnh = nep->ne_inet6head;
2333 np = (struct netcred *)
2334 (*rnh->rnh_matchaddr)((char *)saddr,
2336 if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
2342 * If no address match, use the default if it exists.
2344 if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
2345 np = &nep->ne_defexported;
2351 * perform msync on all vnodes under a mount point. The mount point must
2352 * be locked. This code is also responsible for lazy-freeing unreferenced
2353 * vnodes whos VM objects no longer contain pages.
2355 * NOTE: MNT_WAIT still skips vnodes in the VXLOCK state.
2357 * NOTE: XXX VOP_PUTPAGES and friends requires that the vnode be locked,
2358 * but vnode_pager_putpages() doesn't lock the vnode. We have to do it
2359 * way up in this high level function.
2361 static int vfs_msync_scan1(struct mount *mp, struct vnode *vp, void *data);
2362 static int vfs_msync_scan2(struct mount *mp, struct vnode *vp, void *data);
2365 vfs_msync(struct mount *mp, int flags)
2370 * tmpfs sets this flag to prevent msync(), sync, and the
2371 * filesystem periodic syncer from trying to flush VM pages
2372 * to swap. Only pure memory pressure flushes tmpfs VM pages
2375 if (mp->mnt_kern_flag & MNTK_NOMSYNC)
2379 * Ok, scan the vnodes for work. If the filesystem is using the
2380 * syncer thread feature we can use vsyncscan() instead of
2381 * vmntvnodescan(), which is much faster.
2383 vmsc_flags = VMSC_GETVP;
2384 if (flags != MNT_WAIT)
2385 vmsc_flags |= VMSC_NOWAIT;
2387 if (mp->mnt_kern_flag & MNTK_THR_SYNC) {
2388 vsyncscan(mp, vmsc_flags, vfs_msync_scan2,
2389 (void *)(intptr_t)flags);
2391 vmntvnodescan(mp, vmsc_flags,
2392 vfs_msync_scan1, vfs_msync_scan2,
2393 (void *)(intptr_t)flags);
2398 * scan1 is a fast pre-check. There could be hundreds of thousands of
2399 * vnodes, we cannot afford to do anything heavy weight until we have a
2400 * fairly good indication that there is work to do.
2404 vfs_msync_scan1(struct mount *mp, struct vnode *vp, void *data)
2406 int flags = (int)(intptr_t)data;
2408 if ((vp->v_flag & VRECLAIMED) == 0) {
2409 if (vp->v_auxrefs == 0 && VREFCNT(vp) <= 0 &&
2411 return(0); /* call scan2 */
2413 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
2414 (vp->v_flag & VOBJDIRTY) &&
2415 (flags == MNT_WAIT || vn_islocked(vp) == 0)) {
2416 return(0); /* call scan2 */
2421 * do not call scan2, continue the loop
2427 * This callback is handed a locked vnode.
2431 vfs_msync_scan2(struct mount *mp, struct vnode *vp, void *data)
2434 int flags = (int)(intptr_t)data;
2437 if (vp->v_flag & VRECLAIMED)
2440 if ((mp->mnt_flag & MNT_RDONLY) == 0 && (vp->v_flag & VOBJDIRTY)) {
2441 if ((obj = vp->v_object) != NULL) {
2442 if (flags == MNT_WAIT) {
2444 * VFS_MSYNC is called with MNT_WAIT when
2447 opcflags = OBJPC_SYNC;
2448 } else if (vp->v_writecount || obj->ref_count) {
2450 * VFS_MSYNC is otherwise called via the
2451 * periodic filesystem sync or the 'sync'
2452 * command. Honor MADV_NOSYNC / MAP_NOSYNC
2453 * if the file is open for writing or memory
2454 * mapped. Pages flagged PG_NOSYNC will not
2455 * be automatically flushed at this time.
2457 * The obj->ref_count test is not perfect
2458 * since temporary refs may be present, but
2459 * the periodic filesystem sync will ultimately
2460 * catch it if the file is not open and not
2463 opcflags = OBJPC_NOSYNC;
2466 * If the file is no longer open for writing
2467 * and also no longer mapped, do not honor
2468 * MAP_NOSYNC. That is, fully synchronize
2471 * This still occurs on the periodic fs sync,
2472 * so frontend programs which turn the file
2473 * over quickly enough can still avoid the
2474 * sync, but ultimately we do want to flush
2475 * even MADV_NOSYNC pages once it is no longer
2476 * mapped or open for writing.
2480 vm_object_page_clean(obj, 0, 0, opcflags);
2487 * Wake up anyone interested in vp because it is being revoked.
2490 vn_gone(struct vnode *vp)
2492 lwkt_gettoken(&vp->v_token);
2493 KNOTE(&vp->v_pollinfo.vpi_kqinfo.ki_note, NOTE_REVOKE);
2494 lwkt_reltoken(&vp->v_token);
2498 * extract the cdev_t from a VBLK or VCHR. The vnode must have been opened
2499 * (or v_rdev might be NULL).
2502 vn_todev(struct vnode *vp)
2504 if (vp->v_type != VBLK && vp->v_type != VCHR)
2506 KKASSERT(vp->v_rdev != NULL);
2507 return (vp->v_rdev);
2511 * Check if vnode represents a disk device. The vnode does not need to be
2517 vn_isdisk(struct vnode *vp, int *errp)
2521 if (vp->v_type != VCHR) {
2534 if (dev_is_good(dev) == 0) {
2539 if ((dev_dflags(dev) & D_DISK) == 0) {
2550 vn_get_namelen(struct vnode *vp, int *namelen)
2553 register_t retval[2];
2555 error = VOP_PATHCONF(vp, _PC_NAME_MAX, retval);
2558 *namelen = (int)retval[0];
2563 vop_write_dirent(int *error, struct uio *uio, ino_t d_ino, uint8_t d_type,
2564 uint16_t d_namlen, const char *d_name)
2569 len = _DIRENT_RECLEN(d_namlen);
2570 if (len > uio->uio_resid)
2573 dp = kmalloc(len, M_TEMP, M_WAITOK | M_ZERO);
2576 dp->d_namlen = d_namlen;
2577 dp->d_type = d_type;
2578 bcopy(d_name, dp->d_name, d_namlen);
2580 *error = uiomove((caddr_t)dp, len, uio);
2588 vn_mark_atime(struct vnode *vp, struct thread *td)
2590 struct proc *p = td->td_proc;
2591 struct ucred *cred = p ? p->p_ucred : proc0.p_ucred;
2593 if ((vp->v_mount->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0) {
2594 VOP_MARKATIME(vp, cred);
2599 * Calculate the number of entries in an inode-related chained hash table.
2600 * With today's memory sizes, maxvnodes can wind up being a very large
2601 * number. There is no reason to waste memory, so tolerate some stacking.
2604 vfs_inodehashsize(void)
2609 while (hsize < maxvnodes)
2611 while (hsize > maxvnodes * 2)
2612 hsize >>= 1; /* nominal 2x stacking */
2614 if (maxvnodes > 1024 * 1024)
2615 hsize >>= 1; /* nominal 8x stacking */
2617 if (maxvnodes > 128 * 1024)
2618 hsize >>= 1; /* nominal 4x stacking */