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>
50 #include <sys/dirent.h>
51 #include <sys/endian.h>
52 #include <sys/eventhandler.h>
53 #include <sys/fcntl.h>
55 #include <sys/kernel.h>
56 #include <sys/kthread.h>
57 #include <sys/malloc.h>
59 #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 <vm/vm_page2.h>
87 #include <netinet/in.h>
89 static MALLOC_DEFINE(M_NETCRED, "Export Host", "Export host address structure");
91 __read_mostly int numvnodes;
92 SYSCTL_INT(_debug, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0,
93 "Number of vnodes allocated");
94 __read_mostly int verbose_reclaims;
95 SYSCTL_INT(_debug, OID_AUTO, verbose_reclaims, CTLFLAG_RD, &verbose_reclaims, 0,
96 "Output filename of reclaimed vnode(s)");
98 __read_mostly enum vtype iftovt_tab[16] = {
99 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
100 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
102 __read_mostly int vttoif_tab[9] = {
103 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
104 S_IFSOCK, S_IFIFO, S_IFMT,
107 static int reassignbufcalls;
108 SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW, &reassignbufcalls,
109 0, "Number of times buffers have been reassigned to the proper list");
111 __read_mostly static int check_buf_overlap = 2; /* invasive check */
112 SYSCTL_INT(_vfs, OID_AUTO, check_buf_overlap, CTLFLAG_RW, &check_buf_overlap,
113 0, "Enable overlapping buffer checks");
115 int nfs_mount_type = -1;
116 static struct lwkt_token spechash_token;
117 struct nfs_public nfs_pub; /* publicly exported FS */
119 __read_mostly int maxvnodes;
120 SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RW,
121 &maxvnodes, 0, "Maximum number of vnodes");
123 static struct radix_node_head *vfs_create_addrlist_af(int af,
124 struct netexport *nep);
125 static void vclean_vxlocked(struct vnode *vp, int flags);
127 __read_mostly int prtactive = 0; /* 1 => print out reclaim of active vnodes */
130 * Red black tree functions
132 static int rb_buf_compare(struct buf *b1, struct buf *b2);
133 RB_GENERATE2(buf_rb_tree, buf, b_rbnode, rb_buf_compare, off_t, b_loffset);
134 RB_GENERATE2(buf_rb_hash, buf, b_rbhash, rb_buf_compare, off_t, b_loffset);
137 rb_buf_compare(struct buf *b1, struct buf *b2)
139 if (b1->b_loffset < b2->b_loffset)
141 if (b1->b_loffset > b2->b_loffset)
147 * Initialize the vnode management data structures.
149 * Called from vfsinit()
151 #define VNBREAKMEM1 (1L * 1024 * 1024 * 1024)
152 #define VNBREAKMEM2 (7L * 1024 * 1024 * 1024)
153 #define MINVNODES 2000
154 #define MAXVNODES 4000000
159 int factor1; /* Limit based on ram (x 2 above 1GB) */
163 * Size maxvnodes non-linearly to available memory. Don't bloat
164 * the count on low-memory systems. Scale up for systems with
165 * more than 1G and more than 8G of ram, but do so non-linearly
166 * because the value of a large maxvnodes count diminishes
167 * significantly beyond a certain point.
169 * The general minimum is maxproc * 8 (we want someone pushing
170 * up maxproc a lot to also get more vnodes). Usually maxproc
171 * does not affect this calculation. The KvaSize limitation also
172 * typically does not affect this calculation (it is just in case
173 * the kernel VM space is made much smaller than main memory, which
174 * should no longer happen on 64-bit systems).
176 * There isn't much of a point allowing maxvnodes to exceed a
177 * few million as modern filesystems cache pages in the
178 * underlying block device and not so much hanging off of VM
181 * Also, VM objects, vnodes, and filesystem inode and other related
182 * structures have gotten a lot larger in recent years and the kernel
183 * memory use tends to scale with maxvnodes, so we don't want to bloat
184 * it too much. But neither do we want the max set too low because
185 * systems with large amounts of memory and cores are capable of
186 * doing a hell of a lot.
188 factor1 = 80 * (sizeof(struct vm_object) + sizeof(struct vnode));
190 freemem = (int64_t)vmstats.v_page_count * PAGE_SIZE;
192 maxvnodes = freemem / factor1;
193 if (freemem > VNBREAKMEM1) {
194 freemem -= VNBREAKMEM1;
195 if (freemem < VNBREAKMEM2) {
196 maxvnodes += freemem / factor1 / 2;
198 maxvnodes += VNBREAKMEM2 / factor1 / 2;
199 freemem -= VNBREAKMEM2;
200 maxvnodes += freemem / factor1 / 4;
203 maxvnodes = imax(maxvnodes, maxproc * 8);
204 maxvnodes = imin(maxvnodes, KvaSize / factor1);
205 maxvnodes = imin(maxvnodes, MAXVNODES);
206 maxvnodes = imax(maxvnodes, MINVNODES);
208 lwkt_token_init(&spechash_token, "spechash");
212 * Knob to control the precision of file timestamps:
214 * 0 = seconds only; nanoseconds zeroed.
215 * 1 = microseconds accurate to tick precision
216 * 2 = microseconds accurate to tick precision (default, hz >= 100)
217 * 3 = nanoseconds accurate to tick precision
218 * 4 = microseconds, maximum precision (default, hz < 100)
219 * 5 = nanoseconds, maximum precision
221 * Note that utimes() precision is microseconds because it takes a timeval
222 * structure, so its probably best to default to USEC or USEC_PRECISE, and
225 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC,
226 TSP_USEC_PRECISE, TSP_NSEC_PRECISE };
228 __read_mostly static int timestamp_precision = -1;
229 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW,
230 ×tamp_precision, 0, "Precision of file timestamps");
233 * Get a current timestamp.
238 vfs_timestamp(struct timespec *tsp)
240 switch (timestamp_precision) {
241 case TSP_SEC: /* seconds precision */
245 case TSP_HZ: /* ticks precision (limit to microseconds) */
247 tsp->tv_nsec -= tsp->tv_nsec % 1000;
250 case TSP_USEC: /* microseconds (ticks precision) */
252 tsp->tv_nsec -= tsp->tv_nsec % 1000;
254 case TSP_NSEC: /* nanoseconds (ticks precision) */
257 case TSP_USEC_PRECISE: /* microseconds (high preceision) */
259 tsp->tv_nsec -= tsp->tv_nsec % 1000;
261 case TSP_NSEC_PRECISE: /* nanoseconds (high precision) */
268 * Set vnode attributes to VNOVAL
271 vattr_null(struct vattr *vap)
274 vap->va_size = VNOVAL;
275 vap->va_bytes = VNOVAL;
276 vap->va_mode = VNOVAL;
277 vap->va_nlink = VNOVAL;
278 vap->va_uid = VNOVAL;
279 vap->va_gid = VNOVAL;
280 vap->va_fsid = VNOVAL;
281 vap->va_fileid = VNOVAL;
282 vap->va_blocksize = VNOVAL;
283 vap->va_rmajor = VNOVAL;
284 vap->va_rminor = VNOVAL;
285 vap->va_atime.tv_sec = VNOVAL;
286 vap->va_atime.tv_nsec = VNOVAL;
287 vap->va_mtime.tv_sec = VNOVAL;
288 vap->va_mtime.tv_nsec = VNOVAL;
289 vap->va_ctime.tv_sec = VNOVAL;
290 vap->va_ctime.tv_nsec = VNOVAL;
291 vap->va_flags = VNOVAL;
292 vap->va_gen = VNOVAL;
294 /* va_*_uuid fields are only valid if related flags are set */
298 * Flush out and invalidate all buffers associated with a vnode.
302 static int vinvalbuf_bp(struct buf *bp, void *data);
304 struct vinvalbuf_bp_info {
313 vinvalbuf(struct vnode *vp, int flags, int slpflag, int slptimeo)
315 struct vinvalbuf_bp_info info;
319 lwkt_gettoken(&vp->v_token);
322 * If we are being asked to save, call fsync to ensure that the inode
325 if (flags & V_SAVE) {
326 error = bio_track_wait(&vp->v_track_write, slpflag, slptimeo);
329 if (!RB_EMPTY(&vp->v_rbdirty_tree)) {
330 if ((error = VOP_FSYNC(vp, MNT_WAIT, 0)) != 0)
334 * Dirty bufs may be left or generated via races
335 * in circumstances where vinvalbuf() is called on
336 * a vnode not undergoing reclamation. Only
337 * panic if we are trying to reclaim the vnode.
339 if ((vp->v_flag & VRECLAIMED) &&
340 (bio_track_active(&vp->v_track_write) ||
341 !RB_EMPTY(&vp->v_rbdirty_tree))) {
342 panic("vinvalbuf: dirty bufs");
347 info.slptimeo = slptimeo;
348 info.lkflags = LK_EXCLUSIVE | LK_SLEEPFAIL;
349 if (slpflag & PCATCH)
350 info.lkflags |= LK_PCATCH;
355 * Flush the buffer cache until nothing is left, wait for all I/O
356 * to complete. At least one pass is required. We might block
357 * in the pip code so we have to re-check. Order is important.
363 if (!RB_EMPTY(&vp->v_rbclean_tree)) {
365 error = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree,
366 NULL, vinvalbuf_bp, &info);
368 if (!RB_EMPTY(&vp->v_rbdirty_tree)) {
370 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
371 NULL, vinvalbuf_bp, &info);
375 * Wait for I/O completion.
377 bio_track_wait(&vp->v_track_write, 0, 0);
378 if ((object = vp->v_object) != NULL)
379 refcount_wait(&object->paging_in_progress, "vnvlbx");
380 } while (bio_track_active(&vp->v_track_write) ||
381 !RB_EMPTY(&vp->v_rbclean_tree) ||
382 !RB_EMPTY(&vp->v_rbdirty_tree));
385 * Destroy the copy in the VM cache, too.
387 if ((object = vp->v_object) != NULL) {
388 vm_object_page_remove(object, 0, 0,
389 (flags & V_SAVE) ? TRUE : FALSE);
392 if (!RB_EMPTY(&vp->v_rbdirty_tree) || !RB_EMPTY(&vp->v_rbclean_tree))
393 panic("vinvalbuf: flush failed");
394 if (!RB_EMPTY(&vp->v_rbhash_tree))
395 panic("vinvalbuf: flush failed, buffers still present");
398 lwkt_reltoken(&vp->v_token);
403 vinvalbuf_bp(struct buf *bp, void *data)
405 struct vinvalbuf_bp_info *info = data;
408 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
409 atomic_add_int(&bp->b_refs, 1);
410 error = BUF_TIMELOCK(bp, info->lkflags,
411 "vinvalbuf", info->slptimeo);
412 atomic_subtract_int(&bp->b_refs, 1);
421 KKASSERT(bp->b_vp == info->vp);
424 * Must check clean/dirty status after successfully locking as
427 if ((info->clean && (bp->b_flags & B_DELWRI)) ||
428 (info->clean == 0 && (bp->b_flags & B_DELWRI) == 0)) {
434 * NOTE: NO B_LOCKED CHECK. Also no buf_checkwrite()
435 * check. This code will write out the buffer, period.
438 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
439 (info->flags & V_SAVE)) {
441 } else if (info->flags & V_SAVE) {
443 * Cannot set B_NOCACHE on a clean buffer as this will
444 * destroy the VM backing store which might actually
445 * be dirty (and unsynchronized).
447 bp->b_flags |= (B_INVAL | B_RELBUF);
450 bp->b_flags |= (B_INVAL | B_NOCACHE | B_RELBUF);
457 * Truncate a file's buffer and pages to a specified length. This
458 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
461 * The vnode must be locked.
463 static int vtruncbuf_bp_trunc_cmp(struct buf *bp, void *data);
464 static int vtruncbuf_bp_trunc(struct buf *bp, void *data);
465 static int vtruncbuf_bp_metasync_cmp(struct buf *bp, void *data);
466 static int vtruncbuf_bp_metasync(struct buf *bp, void *data);
468 struct vtruncbuf_info {
475 vtruncbuf(struct vnode *vp, off_t length, int blksize)
477 struct vtruncbuf_info info;
478 const char *filename;
482 * Round up to the *next* block, then destroy the buffers in question.
483 * Since we are only removing some of the buffers we must rely on the
484 * scan count to determine whether a loop is necessary.
486 if ((count = (int)(length % blksize)) != 0)
487 info.truncloffset = length + (blksize - count);
489 info.truncloffset = length;
492 lwkt_gettoken(&vp->v_token);
495 count = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree,
496 vtruncbuf_bp_trunc_cmp,
497 vtruncbuf_bp_trunc, &info);
499 count += RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
500 vtruncbuf_bp_trunc_cmp,
501 vtruncbuf_bp_trunc, &info);
505 * For safety, fsync any remaining metadata if the file is not being
506 * truncated to 0. Since the metadata does not represent the entire
507 * dirty list we have to rely on the hit count to ensure that we get
512 count = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
513 vtruncbuf_bp_metasync_cmp,
514 vtruncbuf_bp_metasync, &info);
519 * Clean out any left over VM backing store.
521 * It is possible to have in-progress I/O from buffers that were
522 * not part of the truncation. This should not happen if we
523 * are truncating to 0-length.
525 vnode_pager_setsize(vp, length);
526 bio_track_wait(&vp->v_track_write, 0, 0);
531 spin_lock(&vp->v_spin);
532 filename = TAILQ_FIRST(&vp->v_namecache) ?
533 TAILQ_FIRST(&vp->v_namecache)->nc_name : "?";
534 spin_unlock(&vp->v_spin);
537 * Make sure no buffers were instantiated while we were trying
538 * to clean out the remaining VM pages. This could occur due
539 * to busy dirty VM pages being flushed out to disk.
543 count = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree,
544 vtruncbuf_bp_trunc_cmp,
545 vtruncbuf_bp_trunc, &info);
547 count += RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
548 vtruncbuf_bp_trunc_cmp,
549 vtruncbuf_bp_trunc, &info);
551 kprintf("Warning: vtruncbuf(): Had to re-clean %d "
552 "left over buffers in %s\n", count, filename);
556 lwkt_reltoken(&vp->v_token);
562 * The callback buffer is beyond the new file EOF and must be destroyed.
563 * Note that the compare function must conform to the RB_SCAN's requirements.
567 vtruncbuf_bp_trunc_cmp(struct buf *bp, void *data)
569 struct vtruncbuf_info *info = data;
571 if (bp->b_loffset >= info->truncloffset)
578 vtruncbuf_bp_trunc(struct buf *bp, void *data)
580 struct vtruncbuf_info *info = data;
583 * Do not try to use a buffer we cannot immediately lock, but sleep
584 * anyway to prevent a livelock. The code will loop until all buffers
587 * We must always revalidate the buffer after locking it to deal
590 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
591 atomic_add_int(&bp->b_refs, 1);
592 if (BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL) == 0)
594 atomic_subtract_int(&bp->b_refs, 1);
595 } else if ((info->clean && (bp->b_flags & B_DELWRI)) ||
596 (info->clean == 0 && (bp->b_flags & B_DELWRI) == 0) ||
597 bp->b_vp != info->vp ||
598 vtruncbuf_bp_trunc_cmp(bp, data)) {
602 bp->b_flags |= (B_INVAL | B_RELBUF | B_NOCACHE);
609 * Fsync all meta-data after truncating a file to be non-zero. Only metadata
610 * blocks (with a negative loffset) are scanned.
611 * Note that the compare function must conform to the RB_SCAN's requirements.
614 vtruncbuf_bp_metasync_cmp(struct buf *bp, void *data __unused)
616 if (bp->b_loffset < 0)
622 vtruncbuf_bp_metasync(struct buf *bp, void *data)
624 struct vtruncbuf_info *info = data;
626 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
627 atomic_add_int(&bp->b_refs, 1);
628 if (BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL) == 0)
630 atomic_subtract_int(&bp->b_refs, 1);
631 } else if ((bp->b_flags & B_DELWRI) == 0 ||
632 bp->b_vp != info->vp ||
633 vtruncbuf_bp_metasync_cmp(bp, data)) {
637 if (bp->b_vp == info->vp)
646 * vfsync - implements a multipass fsync on a file which understands
647 * dependancies and meta-data. The passed vnode must be locked. The
648 * waitfor argument may be MNT_WAIT or MNT_NOWAIT, or MNT_LAZY.
650 * When fsyncing data asynchronously just do one consolidated pass starting
651 * with the most negative block number. This may not get all the data due
654 * When fsyncing data synchronously do a data pass, then a metadata pass,
655 * then do additional data+metadata passes to try to get all the data out.
657 * Caller must ref the vnode but does not have to lock it.
659 static int vfsync_wait_output(struct vnode *vp,
660 int (*waitoutput)(struct vnode *, struct thread *));
661 static int vfsync_dummy_cmp(struct buf *bp __unused, void *data __unused);
662 static int vfsync_data_only_cmp(struct buf *bp, void *data);
663 static int vfsync_meta_only_cmp(struct buf *bp, void *data);
664 static int vfsync_lazy_range_cmp(struct buf *bp, void *data);
665 static int vfsync_bp(struct buf *bp, void *data);
675 int (*checkdef)(struct buf *);
676 int (*cmpfunc)(struct buf *, void *);
680 vfsync(struct vnode *vp, int waitfor, int passes,
681 int (*checkdef)(struct buf *),
682 int (*waitoutput)(struct vnode *, struct thread *))
684 struct vfsync_info info;
687 bzero(&info, sizeof(info));
689 if ((info.checkdef = checkdef) == NULL)
692 lwkt_gettoken(&vp->v_token);
695 case MNT_LAZY | MNT_NOWAIT:
698 * Lazy (filesystem syncer typ) Asynchronous plus limit the
699 * number of data (not meta) pages we try to flush to 1MB.
700 * A non-zero return means that lazy limit was reached.
702 info.lazylimit = 1024 * 1024;
704 info.cmpfunc = vfsync_lazy_range_cmp;
705 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
706 vfsync_lazy_range_cmp, vfsync_bp, &info);
707 info.cmpfunc = vfsync_meta_only_cmp;
708 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
709 vfsync_meta_only_cmp, vfsync_bp, &info);
712 else if (!RB_EMPTY(&vp->v_rbdirty_tree))
713 vn_syncer_add(vp, 1);
718 * Asynchronous. Do a data-only pass and a meta-only pass.
721 info.cmpfunc = vfsync_data_only_cmp;
722 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_data_only_cmp,
724 info.cmpfunc = vfsync_meta_only_cmp;
725 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_meta_only_cmp,
731 * Synchronous. Do a data-only pass, then a meta-data+data
732 * pass, then additional integrated passes to try to get
733 * all the dependancies flushed.
735 info.cmpfunc = vfsync_data_only_cmp;
737 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_data_only_cmp,
740 error = vfsync_wait_output(vp, waitoutput);
742 info.skippedbufs = 0;
743 info.cmpfunc = vfsync_dummy_cmp;
744 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
746 error = vfsync_wait_output(vp, waitoutput);
747 if (info.skippedbufs) {
748 kprintf("Warning: vfsync skipped %d dirty "
751 ((info.skippedbufs > 1) ? "s" : ""));
754 while (error == 0 && passes > 0 &&
755 !RB_EMPTY(&vp->v_rbdirty_tree)
757 info.skippedbufs = 0;
759 info.synchronous = 1;
762 info.cmpfunc = vfsync_dummy_cmp;
763 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
769 error = vfsync_wait_output(vp, waitoutput);
770 if (info.skippedbufs && passes == 0) {
771 kprintf("Warning: vfsync skipped %d dirty "
772 "buf%s in final pass!\n",
774 ((info.skippedbufs > 1) ? "s" : ""));
779 * This case can occur normally because vnode lock might
782 if (!RB_EMPTY(&vp->v_rbdirty_tree))
783 kprintf("dirty bufs left after final pass\n");
787 lwkt_reltoken(&vp->v_token);
793 vfsync_wait_output(struct vnode *vp,
794 int (*waitoutput)(struct vnode *, struct thread *))
798 error = bio_track_wait(&vp->v_track_write, 0, 0);
800 error = waitoutput(vp, curthread);
805 vfsync_dummy_cmp(struct buf *bp __unused, void *data __unused)
811 vfsync_data_only_cmp(struct buf *bp, void *data)
813 if (bp->b_loffset < 0)
819 vfsync_meta_only_cmp(struct buf *bp, void *data)
821 if (bp->b_loffset < 0)
827 vfsync_lazy_range_cmp(struct buf *bp, void *data)
829 struct vfsync_info *info = data;
831 if (bp->b_loffset < info->vp->v_lazyw)
837 vfsync_bp(struct buf *bp, void *data)
839 struct vfsync_info *info = data;
840 struct vnode *vp = info->vp;
843 if (info->fastpass) {
845 * Ignore buffers that we cannot immediately lock.
847 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
849 * Removed BUF_TIMELOCK(..., 1), even a 1-tick
850 * delay can mess up performance
852 * Another reason is that during a dirty-buffer
853 * scan a clustered write can start I/O on buffers
854 * ahead of the scan, causing the scan to not
855 * get a lock here. Usually this means the write
856 * is already in progress so, in fact, we *want*
857 * to skip the buffer.
862 } else if (info->synchronous == 0) {
864 * Normal pass, give the buffer a little time to become
867 if (BUF_TIMELOCK(bp, LK_EXCLUSIVE, "bflst2", hz / 10)) {
873 * Synchronous pass, give the buffer a lot of time before
876 if (BUF_TIMELOCK(bp, LK_EXCLUSIVE, "bflst3", hz * 10)) {
883 * We must revalidate the buffer after locking.
885 if ((bp->b_flags & B_DELWRI) == 0 ||
886 bp->b_vp != info->vp ||
887 info->cmpfunc(bp, data)) {
893 * If syncdeps is not set we do not try to write buffers which have
896 if (!info->synchronous && info->syncdeps == 0 && info->checkdef(bp)) {
902 * B_NEEDCOMMIT (primarily used by NFS) is a state where the buffer
903 * has been written but an additional handshake with the device
904 * is required before we can dispose of the buffer. We have no idea
905 * how to do this so we have to skip these buffers.
907 if (bp->b_flags & B_NEEDCOMMIT) {
913 * Ask bioops if it is ok to sync. If not the VFS may have
914 * set B_LOCKED so we have to cycle the buffer.
916 if (LIST_FIRST(&bp->b_dep) != NULL && buf_checkwrite(bp)) {
922 if (info->synchronous) {
924 * Synchronous flush. An error may be returned and will
931 * Asynchronous flush. We use the error return to support
934 * In low-memory situations we revert to synchronous
935 * operation. This should theoretically prevent the I/O
936 * path from exhausting memory in a non-recoverable way.
938 vp->v_lazyw = bp->b_loffset;
940 if (vm_paging_min()) {
942 info->lazycount += bp->b_bufsize;
946 info->lazycount += cluster_awrite(bp);
947 waitrunningbufspace();
948 /*vm_wait_nominal();*/
950 if (info->lazylimit && info->lazycount >= info->lazylimit)
959 * Associate a buffer with a vnode.
964 bgetvp(struct vnode *vp, struct buf *bp, int testsize)
966 KASSERT(bp->b_vp == NULL, ("bgetvp: not free"));
967 KKASSERT((bp->b_flags & (B_HASHED|B_DELWRI|B_VNCLEAN|B_VNDIRTY)) == 0);
970 * Insert onto list for new vnode.
972 lwkt_gettoken(&vp->v_token);
974 if (buf_rb_hash_RB_INSERT(&vp->v_rbhash_tree, bp)) {
975 lwkt_reltoken(&vp->v_token);
980 * Diagnostics (mainly for HAMMER debugging). Check for
981 * overlapping buffers.
983 if (check_buf_overlap) {
985 bx = buf_rb_hash_RB_PREV(bp);
987 if (bx->b_loffset + bx->b_bufsize > bp->b_loffset) {
988 kprintf("bgetvp: overlapl %016jx/%d %016jx "
990 (intmax_t)bx->b_loffset,
992 (intmax_t)bp->b_loffset,
994 if (check_buf_overlap > 1)
995 panic("bgetvp - overlapping buffer");
998 bx = buf_rb_hash_RB_NEXT(bp);
1000 if (bp->b_loffset + testsize > bx->b_loffset) {
1001 kprintf("bgetvp: overlapr %016jx/%d %016jx "
1003 (intmax_t)bp->b_loffset,
1005 (intmax_t)bx->b_loffset,
1007 if (check_buf_overlap > 1)
1008 panic("bgetvp - overlapping buffer");
1013 bp->b_flags |= B_HASHED;
1014 bp->b_flags |= B_VNCLEAN;
1015 if (buf_rb_tree_RB_INSERT(&vp->v_rbclean_tree, bp))
1016 panic("reassignbuf: dup lblk/clean vp %p bp %p", vp, bp);
1018 lwkt_reltoken(&vp->v_token);
1023 * Disassociate a buffer from a vnode.
1028 brelvp(struct buf *bp)
1032 KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
1035 * Delete from old vnode list, if on one.
1038 lwkt_gettoken(&vp->v_token);
1039 if (bp->b_flags & (B_VNDIRTY | B_VNCLEAN)) {
1040 if (bp->b_flags & B_VNDIRTY)
1041 buf_rb_tree_RB_REMOVE(&vp->v_rbdirty_tree, bp);
1043 buf_rb_tree_RB_REMOVE(&vp->v_rbclean_tree, bp);
1044 bp->b_flags &= ~(B_VNDIRTY | B_VNCLEAN);
1046 if (bp->b_flags & B_HASHED) {
1047 buf_rb_hash_RB_REMOVE(&vp->v_rbhash_tree, bp);
1048 bp->b_flags &= ~B_HASHED;
1052 * Only remove from synclist when no dirty buffers are left AND
1053 * the VFS has not flagged the vnode's inode as being dirty.
1055 if ((vp->v_flag & (VONWORKLST | VISDIRTY | VOBJDIRTY)) == VONWORKLST &&
1056 RB_EMPTY(&vp->v_rbdirty_tree)) {
1057 vn_syncer_remove(vp, 0);
1061 lwkt_reltoken(&vp->v_token);
1067 * Reassign the buffer to the proper clean/dirty list based on B_DELWRI.
1068 * This routine is called when the state of the B_DELWRI bit is changed.
1070 * Must be called with vp->v_token held.
1074 reassignbuf(struct buf *bp)
1076 struct vnode *vp = bp->b_vp;
1079 ASSERT_LWKT_TOKEN_HELD(&vp->v_token);
1083 * B_PAGING flagged buffers cannot be reassigned because their vp
1084 * is not fully linked in.
1086 if (bp->b_flags & B_PAGING)
1087 panic("cannot reassign paging buffer");
1089 if (bp->b_flags & B_DELWRI) {
1091 * Move to the dirty list, add the vnode to the worklist
1093 if (bp->b_flags & B_VNCLEAN) {
1094 buf_rb_tree_RB_REMOVE(&vp->v_rbclean_tree, bp);
1095 bp->b_flags &= ~B_VNCLEAN;
1097 if ((bp->b_flags & B_VNDIRTY) == 0) {
1098 if (buf_rb_tree_RB_INSERT(&vp->v_rbdirty_tree, bp)) {
1099 panic("reassignbuf: dup lblk vp %p bp %p",
1102 bp->b_flags |= B_VNDIRTY;
1104 if ((vp->v_flag & VONWORKLST) == 0) {
1105 switch (vp->v_type) {
1112 vp->v_rdev->si_mountpoint != NULL) {
1120 vn_syncer_add(vp, delay);
1124 * Move to the clean list, remove the vnode from the worklist
1125 * if no dirty blocks remain.
1127 if (bp->b_flags & B_VNDIRTY) {
1128 buf_rb_tree_RB_REMOVE(&vp->v_rbdirty_tree, bp);
1129 bp->b_flags &= ~B_VNDIRTY;
1131 if ((bp->b_flags & B_VNCLEAN) == 0) {
1132 if (buf_rb_tree_RB_INSERT(&vp->v_rbclean_tree, bp)) {
1133 panic("reassignbuf: dup lblk vp %p bp %p",
1136 bp->b_flags |= B_VNCLEAN;
1140 * Only remove from synclist when no dirty buffers are left
1141 * AND the VFS has not flagged the vnode's inode as being
1144 if ((vp->v_flag & (VONWORKLST | VISDIRTY | VOBJDIRTY)) ==
1146 RB_EMPTY(&vp->v_rbdirty_tree)) {
1147 vn_syncer_remove(vp, 0);
1153 * Create a vnode for a block device. Used for mounting the root file
1156 * A vref()'d vnode is returned.
1158 extern struct vop_ops *devfs_vnode_dev_vops_p;
1160 bdevvp(cdev_t dev, struct vnode **vpp)
1170 error = getspecialvnode(VT_NON, NULL, &devfs_vnode_dev_vops_p,
1181 v_associate_rdev(vp, dev);
1182 vp->v_umajor = dev->si_umajor;
1183 vp->v_uminor = dev->si_uminor;
1190 v_associate_rdev(struct vnode *vp, cdev_t dev)
1194 if (dev_is_good(dev) == 0)
1196 KKASSERT(vp->v_rdev == NULL);
1197 vp->v_rdev = reference_dev(dev);
1198 lwkt_gettoken(&spechash_token);
1199 SLIST_INSERT_HEAD(&dev->si_hlist, vp, v_cdevnext);
1200 lwkt_reltoken(&spechash_token);
1205 v_release_rdev(struct vnode *vp)
1209 if ((dev = vp->v_rdev) != NULL) {
1210 lwkt_gettoken(&spechash_token);
1211 SLIST_REMOVE(&dev->si_hlist, vp, vnode, v_cdevnext);
1214 lwkt_reltoken(&spechash_token);
1219 * Add a vnode to the alias list hung off the cdev_t. We only associate
1220 * the device number with the vnode. The actual device is not associated
1221 * until the vnode is opened (usually in spec_open()), and will be
1222 * disassociated on last close.
1225 addaliasu(struct vnode *nvp, int x, int y)
1227 if (nvp->v_type != VBLK && nvp->v_type != VCHR)
1228 panic("addaliasu on non-special vnode");
1234 * Simple call that a filesystem can make to try to get rid of a
1235 * vnode. It will fail if anyone is referencing the vnode (including
1238 * The filesystem can check whether its in-memory inode structure still
1239 * references the vp on return.
1241 * May only be called if the vnode is in a known state (i.e. being prevented
1242 * from being deallocated by some other condition such as a vfs inode hold).
1244 * This call might not succeed.
1247 vclean_unlocked(struct vnode *vp)
1250 if (VREFCNT(vp) <= 1)
1256 * Disassociate a vnode from its underlying filesystem.
1258 * The vnode must be VX locked and referenced. In all normal situations
1259 * there are no active references. If vclean_vxlocked() is called while
1260 * there are active references, the vnode is being ripped out and we have
1261 * to call VOP_CLOSE() as appropriate before we can reclaim it.
1264 vclean_vxlocked(struct vnode *vp, int flags)
1269 struct namecache *ncp;
1272 * If the vnode has already been reclaimed we have nothing to do.
1274 if (vp->v_flag & VRECLAIMED)
1278 * Set flag to interlock operation, flag finalization to ensure
1279 * that the vnode winds up on the inactive list, and set v_act to 0.
1281 vsetflags(vp, VRECLAIMED);
1282 atomic_set_int(&vp->v_refcnt, VREF_FINALIZE);
1285 if (verbose_reclaims) {
1286 if ((ncp = TAILQ_FIRST(&vp->v_namecache)) != NULL)
1287 kprintf("Debug: reclaim %p %s\n", vp, ncp->nc_name);
1291 * Scrap the vfs cache
1293 while (cache_inval_vp(vp, 0) != 0) {
1294 kprintf("Warning: vnode %p clean/cache_resolution "
1295 "race detected\n", vp);
1296 tsleep(vp, 0, "vclninv", 2);
1300 * Check to see if the vnode is in use. If so we have to reference it
1301 * before we clean it out so that its count cannot fall to zero and
1302 * generate a race against ourselves to recycle it.
1304 active = (VREFCNT(vp) > 0);
1307 * Clean out any buffers associated with the vnode and destroy its
1308 * object, if it has one.
1310 vinvalbuf(vp, V_SAVE, 0, 0);
1313 * If purging an active vnode (typically during a forced unmount
1314 * or reboot), it must be closed and deactivated before being
1315 * reclaimed. This isn't really all that safe, but what can
1318 * Note that neither of these routines unlocks the vnode.
1320 if (active && (flags & DOCLOSE)) {
1321 while ((n = vp->v_opencount) != 0) {
1322 if (vp->v_writecount)
1323 VOP_CLOSE(vp, FWRITE|FNONBLOCK, NULL);
1325 VOP_CLOSE(vp, FNONBLOCK, NULL);
1326 if (vp->v_opencount == n) {
1327 kprintf("Warning: unable to force-close"
1335 * If the vnode has not been deactivated, deactivated it. Deactivation
1336 * can create new buffers and VM pages so we have to call vinvalbuf()
1337 * again to make sure they all get flushed.
1339 * This can occur if a file with a link count of 0 needs to be
1342 * If the vnode is already dead don't try to deactivate it.
1344 if ((vp->v_flag & VINACTIVE) == 0) {
1345 vsetflags(vp, VINACTIVE);
1348 vinvalbuf(vp, V_SAVE, 0, 0);
1352 * If the vnode has an object, destroy it.
1354 while ((object = vp->v_object) != NULL) {
1355 vm_object_hold(object);
1356 if (object == vp->v_object)
1358 vm_object_drop(object);
1361 if (object != NULL) {
1362 if (object->ref_count == 0) {
1363 if ((object->flags & OBJ_DEAD) == 0)
1364 vm_object_terminate(object);
1365 vm_object_drop(object);
1366 vclrflags(vp, VOBJBUF);
1368 vm_pager_deallocate(object);
1369 vclrflags(vp, VOBJBUF);
1370 vm_object_drop(object);
1373 KKASSERT((vp->v_flag & VOBJBUF) == 0);
1375 if (vp->v_flag & VOBJDIRTY)
1379 * Reclaim the vnode if not already dead.
1381 if (vp->v_mount && VOP_RECLAIM(vp))
1382 panic("vclean: cannot reclaim");
1385 * Done with purge, notify sleepers of the grim news.
1387 vp->v_ops = &dead_vnode_vops_p;
1392 * If we are destroying an active vnode, reactivate it now that
1393 * we have reassociated it with deadfs. This prevents the system
1394 * from crashing on the vnode due to it being unexpectedly marked
1395 * as inactive or reclaimed.
1397 if (active && (flags & DOCLOSE)) {
1398 vclrflags(vp, VINACTIVE | VRECLAIMED);
1403 * Eliminate all activity associated with the requested vnode
1404 * and with all vnodes aliased to the requested vnode.
1406 * The vnode must be referenced but should not be locked.
1409 vrevoke(struct vnode *vp, struct ucred *cred)
1417 * If the vnode has a device association, scrap all vnodes associated
1418 * with the device. Don't let the device disappear on us while we
1419 * are scrapping the vnodes.
1421 * The passed vp will probably show up in the list, do not VX lock
1424 * Releasing the vnode's rdev here can mess up specfs's call to
1425 * device close, so don't do it. The vnode has been disassociated
1426 * and the device will be closed after the last ref on the related
1427 * fp goes away (if not still open by e.g. the kernel).
1429 if (vp->v_type != VCHR) {
1430 error = fdrevoke(vp, DTYPE_VNODE, cred);
1433 if ((dev = vp->v_rdev) == NULL) {
1437 lwkt_gettoken(&spechash_token);
1440 vqn = SLIST_FIRST(&dev->si_hlist);
1443 while ((vq = vqn) != NULL) {
1444 if (VREFCNT(vq) > 0) {
1446 fdrevoke(vq, DTYPE_VNODE, cred);
1447 /*v_release_rdev(vq);*/
1449 if (vq->v_rdev != dev) {
1454 vqn = SLIST_NEXT(vq, v_cdevnext);
1459 lwkt_reltoken(&spechash_token);
1466 * This is called when the object underlying a vnode is being destroyed,
1467 * such as in a remove(). Try to recycle the vnode immediately if the
1468 * only active reference is our reference.
1470 * Directory vnodes in the namecache with children cannot be immediately
1471 * recycled because numerous VOP_N*() ops require them to be stable.
1473 * To avoid recursive recycling from VOP_INACTIVE implemenetations this
1474 * function is a NOP if VRECLAIMED is already set.
1477 vrecycle(struct vnode *vp)
1479 if (VREFCNT(vp) <= 1 && (vp->v_flag & VRECLAIMED) == 0) {
1480 if (cache_inval_vp_nonblock(vp))
1489 * Return the maximum I/O size allowed for strategy calls on VP.
1491 * If vp is VCHR or VBLK we dive the device, otherwise we use
1492 * the vp's mount info.
1494 * The returned value is clamped at MAXPHYS as most callers cannot use
1495 * buffers larger than that size.
1498 vmaxiosize(struct vnode *vp)
1502 if (vp->v_type == VBLK || vp->v_type == VCHR)
1503 maxiosize = vp->v_rdev->si_iosize_max;
1505 maxiosize = vp->v_mount->mnt_iosize_max;
1507 if (maxiosize > MAXPHYS)
1508 maxiosize = MAXPHYS;
1513 * Eliminate all activity associated with a vnode in preparation for
1516 * The vnode must be VX locked and refd and will remain VX locked and refd
1517 * on return. This routine may be called with the vnode in any state, as
1518 * long as it is VX locked. The vnode will be cleaned out and marked
1519 * VRECLAIMED but will not actually be reused until all existing refs and
1522 * NOTE: This routine may be called on a vnode which has not yet been
1523 * already been deactivated (VOP_INACTIVE), or on a vnode which has
1524 * already been reclaimed.
1526 * This routine is not responsible for placing us back on the freelist.
1527 * Instead, it happens automatically when the caller releases the VX lock
1528 * (assuming there aren't any other references).
1531 vgone_vxlocked(struct vnode *vp)
1534 * assert that the VX lock is held. This is an absolute requirement
1535 * now for vgone_vxlocked() to be called.
1537 KKASSERT(lockinuse(&vp->v_lock));
1540 * Clean out the filesystem specific data and set the VRECLAIMED
1541 * bit. Also deactivate the vnode if necessary.
1543 * The vnode should have automatically been removed from the syncer
1544 * list as syncer/dirty flags cleared during the cleaning.
1546 vclean_vxlocked(vp, DOCLOSE);
1549 * Normally panic if the vnode is still dirty, unless we are doing
1550 * a forced unmount (tmpfs typically).
1552 if (vp->v_flag & VONWORKLST) {
1553 if (vp->v_mount->mnt_kern_flag & MNTK_UNMOUNTF) {
1555 vn_syncer_remove(vp, 1);
1557 panic("vp %p still dirty in vgone after flush", vp);
1562 * Delete from old mount point vnode list, if on one.
1564 if (vp->v_mount != NULL) {
1565 KKASSERT(vp->v_data == NULL);
1566 insmntque(vp, NULL);
1570 * If special device, remove it from special device alias list
1571 * if it is on one. This should normally only occur if a vnode is
1572 * being revoked as the device should otherwise have been released
1575 if ((vp->v_type == VBLK || vp->v_type == VCHR) && vp->v_rdev != NULL) {
1586 * Calculate the total number of references to a special device. This
1587 * routine may only be called for VBLK and VCHR vnodes since v_rdev is
1588 * an overloaded field. Since dev_from_devid() can now return NULL, we
1589 * have to check for a NULL v_rdev.
1592 count_dev(cdev_t dev)
1597 if (SLIST_FIRST(&dev->si_hlist)) {
1598 lwkt_gettoken(&spechash_token);
1599 SLIST_FOREACH(vp, &dev->si_hlist, v_cdevnext) {
1600 count += vp->v_opencount;
1602 lwkt_reltoken(&spechash_token);
1608 vcount(struct vnode *vp)
1610 if (vp->v_rdev == NULL)
1612 return(count_dev(vp->v_rdev));
1616 * Initialize VMIO for a vnode. This routine MUST be called before a
1617 * VFS can issue buffer cache ops on a vnode. It is typically called
1618 * when a vnode is initialized from its inode.
1621 vinitvmio(struct vnode *vp, off_t filesize, int blksize, int boff)
1626 object = vp->v_object;
1628 vm_object_hold(object);
1629 KKASSERT(vp->v_object == object);
1632 if (object == NULL) {
1633 object = vnode_pager_alloc(vp, filesize, 0, 0, blksize, boff);
1636 * Dereference the reference we just created. This assumes
1637 * that the object is associated with the vp. Allow it to
1638 * have zero refs. It cannot be destroyed as long as it
1639 * is associated with the vnode.
1641 vm_object_hold(object);
1642 atomic_add_int(&object->ref_count, -1);
1645 KKASSERT((object->flags & OBJ_DEAD) == 0);
1647 KASSERT(vp->v_object != NULL, ("vinitvmio: NULL object"));
1648 vsetflags(vp, VOBJBUF);
1649 vm_object_drop(object);
1656 * Print out a description of a vnode.
1658 static char *typename[] =
1659 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD"};
1662 vprint(char *label, struct vnode *vp)
1667 kprintf("%s: %p: ", label, (void *)vp);
1669 kprintf("%p: ", (void *)vp);
1670 kprintf("type %s, refcnt %08x, writecount %d, holdcnt %d,",
1671 typename[vp->v_type],
1672 vp->v_refcnt, vp->v_writecount, vp->v_auxrefs);
1674 if (vp->v_flag & VROOT)
1675 strcat(buf, "|VROOT");
1676 if (vp->v_flag & VPFSROOT)
1677 strcat(buf, "|VPFSROOT");
1678 if (vp->v_flag & VTEXT)
1679 strcat(buf, "|VTEXT");
1680 if (vp->v_flag & VSYSTEM)
1681 strcat(buf, "|VSYSTEM");
1682 if (vp->v_flag & VOBJBUF)
1683 strcat(buf, "|VOBJBUF");
1685 kprintf(" flags (%s)", &buf[1]);
1686 if (vp->v_data == NULL) {
1695 * Do the usual access checking.
1696 * file_mode, uid and gid are from the vnode in question,
1697 * while acc_mode and cred are from the VOP_ACCESS parameter list
1700 vaccess(enum vtype type, mode_t file_mode, uid_t uid, gid_t gid,
1701 mode_t acc_mode, struct ucred *cred)
1707 * Super-user always gets read/write access, but execute access depends
1708 * on at least one execute bit being set.
1710 if (caps_priv_check(cred, SYSCAP_RESTRICTEDROOT) == 0) {
1711 if ((acc_mode & VEXEC) && type != VDIR &&
1712 (file_mode & (S_IXUSR|S_IXGRP|S_IXOTH)) == 0)
1719 /* Otherwise, check the owner. */
1720 if (cred->cr_uid == uid) {
1721 if (acc_mode & VEXEC)
1723 if (acc_mode & VREAD)
1725 if (acc_mode & VWRITE)
1727 return ((file_mode & mask) == mask ? 0 : EACCES);
1730 /* Otherwise, check the groups. */
1731 ismember = groupmember(gid, cred);
1732 if (cred->cr_svgid == gid || ismember) {
1733 if (acc_mode & VEXEC)
1735 if (acc_mode & VREAD)
1737 if (acc_mode & VWRITE)
1739 return ((file_mode & mask) == mask ? 0 : EACCES);
1742 /* Otherwise, check everyone else. */
1743 if (acc_mode & VEXEC)
1745 if (acc_mode & VREAD)
1747 if (acc_mode & VWRITE)
1749 return ((file_mode & mask) == mask ? 0 : EACCES);
1753 #include <ddb/ddb.h>
1755 static int db_show_locked_vnodes(struct mount *mp, void *data);
1758 * List all of the locked vnodes in the system.
1759 * Called when debugging the kernel.
1761 DB_SHOW_COMMAND(lockedvnodes, lockedvnodes)
1763 kprintf("Locked vnodes\n");
1764 mountlist_scan(db_show_locked_vnodes, NULL,
1765 MNTSCAN_FORWARD|MNTSCAN_NOBUSY);
1769 db_show_locked_vnodes(struct mount *mp, void *data __unused)
1773 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
1774 if (vn_islocked(vp))
1782 * Top level filesystem related information gathering.
1784 static int sysctl_ovfs_conf (SYSCTL_HANDLER_ARGS);
1787 vfs_sysctl(SYSCTL_HANDLER_ARGS)
1789 int *name = (int *)arg1 - 1; /* XXX */
1790 u_int namelen = arg2 + 1; /* XXX */
1791 struct vfsconf *vfsp;
1794 #if 1 || defined(COMPAT_PRELITE2)
1795 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
1797 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
1801 /* all sysctl names at this level are at least name and field */
1803 return (ENOTDIR); /* overloaded */
1804 if (name[0] != VFS_GENERIC) {
1805 vfsp = vfsconf_find_by_typenum(name[0]);
1807 return (EOPNOTSUPP);
1808 return ((*vfsp->vfc_vfsops->vfs_sysctl)(&name[1], namelen - 1,
1809 oldp, oldlenp, newp, newlen, p));
1813 case VFS_MAXTYPENUM:
1816 maxtypenum = vfsconf_get_maxtypenum();
1817 return (SYSCTL_OUT(req, &maxtypenum, sizeof(maxtypenum)));
1820 return (ENOTDIR); /* overloaded */
1821 vfsp = vfsconf_find_by_typenum(name[2]);
1823 return (EOPNOTSUPP);
1824 return (SYSCTL_OUT(req, vfsp, sizeof *vfsp));
1826 return (EOPNOTSUPP);
1829 SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD, vfs_sysctl,
1830 "Generic filesystem");
1832 #if 1 || defined(COMPAT_PRELITE2)
1835 sysctl_ovfs_conf_iter(struct vfsconf *vfsp, void *data)
1838 struct ovfsconf ovfs;
1839 struct sysctl_req *req = (struct sysctl_req*) data;
1841 bzero(&ovfs, sizeof(ovfs));
1842 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
1843 strcpy(ovfs.vfc_name, vfsp->vfc_name);
1844 ovfs.vfc_index = vfsp->vfc_typenum;
1845 ovfs.vfc_refcount = vfsp->vfc_refcount;
1846 ovfs.vfc_flags = vfsp->vfc_flags;
1847 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
1849 return error; /* abort iteration with error code */
1851 return 0; /* continue iterating with next element */
1855 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
1857 return vfsconf_each(sysctl_ovfs_conf_iter, (void*)req);
1860 #endif /* 1 || COMPAT_PRELITE2 */
1863 * Check to see if a filesystem is mounted on a block device.
1866 vfs_mountedon(struct vnode *vp)
1871 if (dev != NULL && dev->si_mountpoint)
1877 * Unmount all filesystems. The list is traversed in reverse order
1878 * of mounting to avoid dependencies.
1880 * We want the umountall to be able to break out of its loop if a
1881 * failure occurs, after scanning all possible mounts, so the callback
1882 * returns 0 on error.
1884 * NOTE: Do not call mountlist_remove(mp) on error any more, this will
1885 * confuse mountlist_scan()'s unbusy check.
1887 static int vfs_umountall_callback(struct mount *mp, void *data);
1890 vfs_unmountall(int halting)
1895 count = mountlist_scan(vfs_umountall_callback, &halting,
1896 MNTSCAN_REVERSE|MNTSCAN_NOBUSY);
1902 vfs_umountall_callback(struct mount *mp, void *data)
1905 int halting = *(int *)data;
1908 * NOTE: When halting, dounmount will disconnect but leave
1909 * certain mount points intact. e.g. devfs.
1911 error = dounmount(mp, MNT_FORCE, halting);
1913 kprintf("unmount of filesystem mounted from %s failed (",
1914 mp->mnt_stat.f_mntfromname);
1918 kprintf("%d)\n", error);
1926 * Checks the mount flags for parameter mp and put the names comma-separated
1927 * into a string buffer buf with a size limit specified by len.
1929 * It returns the number of bytes written into buf, and (*errorp) will be
1930 * set to 0, EINVAL (if passed length is 0), or ENOSPC (supplied buffer was
1931 * not large enough). The buffer will be 0-terminated if len was not 0.
1934 vfs_flagstostr(int flags, const struct mountctl_opt *optp,
1935 char *buf, size_t len, int *errorp)
1937 static const struct mountctl_opt optnames[] = {
1938 { MNT_RDONLY, "read-only" },
1939 { MNT_SYNCHRONOUS, "synchronous" },
1940 { MNT_NOEXEC, "noexec" },
1941 { MNT_NOSUID, "nosuid" },
1942 { MNT_NODEV, "nodev" },
1943 { MNT_AUTOMOUNTED, "automounted" },
1944 { MNT_ASYNC, "asynchronous" },
1945 { MNT_SUIDDIR, "suiddir" },
1946 { MNT_SOFTDEP, "soft-updates" },
1947 { MNT_NOSYMFOLLOW, "nosymfollow" },
1948 { MNT_TRIM, "trim" },
1949 { MNT_NOATIME, "noatime" },
1950 { MNT_NOCLUSTERR, "noclusterr" },
1951 { MNT_NOCLUSTERW, "noclusterw" },
1952 { MNT_EXRDONLY, "NFS read-only" },
1953 { MNT_EXPORTED, "NFS exported" },
1954 /* Remaining NFS flags could come here */
1955 { MNT_LOCAL, "local" },
1956 { MNT_QUOTA, "with-quotas" },
1957 /* { MNT_ROOTFS, "rootfs" }, */
1958 /* { MNT_IGNORE, "ignore" }, */
1968 bleft = len - 1; /* leave room for trailing \0 */
1971 * Checks the size of the string. If it contains
1972 * any data, then we will append the new flags to
1975 actsize = strlen(buf);
1979 /* Default flags if no flags passed */
1983 if (bleft < 0) { /* degenerate case, 0-length buffer */
1988 for (; flags && optp->o_opt; ++optp) {
1989 if ((flags & optp->o_opt) == 0)
1991 optlen = strlen(optp->o_name);
1992 if (bwritten || actsize > 0) {
1997 buf[bwritten++] = ',';
1998 buf[bwritten++] = ' ';
2001 if (bleft < optlen) {
2005 bcopy(optp->o_name, buf + bwritten, optlen);
2008 flags &= ~optp->o_opt;
2012 * Space already reserved for trailing \0
2019 * Build hash lists of net addresses and hang them off the mount point.
2020 * Called by ufs_mount() to set up the lists of export addresses.
2023 vfs_hang_addrlist(struct mount *mp, struct netexport *nep,
2024 const struct export_args *argp)
2027 struct radix_node_head *rnh;
2029 struct radix_node *rn;
2030 struct sockaddr *saddr, *smask = NULL;
2033 if (argp->ex_addrlen == 0) {
2034 if (mp->mnt_flag & MNT_DEFEXPORTED)
2036 np = &nep->ne_defexported;
2037 np->netc_exflags = argp->ex_flags;
2038 np->netc_anon = argp->ex_anon;
2039 np->netc_anon.cr_ref = 1;
2040 mp->mnt_flag |= MNT_DEFEXPORTED;
2044 if (argp->ex_addrlen < 0 || argp->ex_addrlen > MLEN)
2046 if (argp->ex_masklen < 0 || argp->ex_masklen > MLEN)
2049 i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
2050 np = (struct netcred *)kmalloc(i, M_NETCRED, M_WAITOK | M_ZERO);
2051 saddr = (struct sockaddr *) (np + 1);
2052 if ((error = copyin(argp->ex_addr, (caddr_t) saddr, argp->ex_addrlen)))
2054 if (saddr->sa_len > argp->ex_addrlen)
2055 saddr->sa_len = argp->ex_addrlen;
2056 if (argp->ex_masklen) {
2057 smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen);
2058 error = copyin(argp->ex_mask, (caddr_t)smask, argp->ex_masklen);
2061 if (smask->sa_len > argp->ex_masklen)
2062 smask->sa_len = argp->ex_masklen;
2065 if (nep->ne_maskhead == NULL) {
2066 if (!rn_inithead(&nep->ne_maskhead, NULL, 0)) {
2071 if ((rnh = vfs_create_addrlist_af(saddr->sa_family, nep)) == NULL) {
2075 rn = rnh->rnh_addaddr(saddr, smask, rnh, np->netc_rnodes);
2077 if (rn == NULL || np != (struct netcred *)rn) { /* already exists */
2081 np->netc_exflags = argp->ex_flags;
2082 np->netc_anon = argp->ex_anon;
2083 np->netc_anon.cr_ref = 1;
2087 kfree(np, M_NETCRED);
2092 * Free netcred structures installed in the netexport
2095 vfs_free_netcred(struct radix_node *rn)
2099 np = (struct netcred *)rn;
2100 kfree(np, M_NETCRED);
2103 static struct radix_node_head *
2104 vfs_create_addrlist_af(int af, struct netexport *nep)
2106 struct radix_node_head *rnh = NULL;
2107 #if defined(INET) || defined(INET6)
2108 struct radix_node_head *maskhead = nep->ne_maskhead;
2112 NE_ASSERT_LOCKED(nep);
2113 #if defined(INET) || defined(INET6)
2114 KKASSERT(maskhead != NULL);
2119 if ((rnh = nep->ne_inethead) == NULL) {
2120 off = offsetof(struct sockaddr_in, sin_addr);
2121 if (!rn_inithead(&rnh, maskhead, off))
2123 nep->ne_inethead = rnh;
2129 if ((rnh = nep->ne_inet6head) == NULL) {
2130 off = offsetof(struct sockaddr_in6, sin6_addr);
2131 if (!rn_inithead(&rnh, maskhead, off))
2133 nep->ne_inet6head = rnh;
2142 * Free the net address hash lists that are hanging off the mount points.
2145 vfs_free_addrlist(struct netexport *nep)
2148 if (nep->ne_inethead != NULL) {
2149 rn_flush(nep->ne_inethead, vfs_free_netcred);
2150 rn_freehead(nep->ne_inethead);
2151 nep->ne_inethead = NULL;
2153 if (nep->ne_inet6head != NULL) {
2154 rn_flush(nep->ne_inet6head, vfs_free_netcred);
2155 rn_freehead(nep->ne_inet6head);
2156 nep->ne_inet6head = NULL;
2158 if (nep->ne_maskhead != NULL) {
2159 rn_flush(nep->ne_maskhead, rn_freemask);
2160 rn_freehead(nep->ne_maskhead);
2161 nep->ne_maskhead = NULL;
2167 vfs_export(struct mount *mp, struct netexport *nep,
2168 const struct export_args *argp)
2172 if (argp->ex_flags & MNT_DELEXPORT) {
2173 if (mp->mnt_flag & MNT_EXPUBLIC) {
2174 vfs_setpublicfs(NULL, NULL, NULL);
2175 mp->mnt_flag &= ~MNT_EXPUBLIC;
2177 vfs_free_addrlist(nep);
2178 mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
2180 if (argp->ex_flags & MNT_EXPORTED) {
2181 if (argp->ex_flags & MNT_EXPUBLIC) {
2182 if ((error = vfs_setpublicfs(mp, nep, argp)) != 0)
2184 mp->mnt_flag |= MNT_EXPUBLIC;
2186 if ((error = vfs_hang_addrlist(mp, nep, argp)))
2188 mp->mnt_flag |= MNT_EXPORTED;
2195 * Set the publicly exported filesystem (WebNFS). Currently, only
2196 * one public filesystem is possible in the spec (RFC 2054 and 2055)
2199 vfs_setpublicfs(struct mount *mp, struct netexport *nep,
2200 const struct export_args *argp)
2207 * mp == NULL -> invalidate the current info, the FS is
2208 * no longer exported. May be called from either vfs_export
2209 * or unmount, so check if it hasn't already been done.
2212 if (nfs_pub.np_valid) {
2213 nfs_pub.np_valid = 0;
2214 if (nfs_pub.np_index != NULL) {
2215 kfree(nfs_pub.np_index, M_TEMP);
2216 nfs_pub.np_index = NULL;
2223 * Only one allowed at a time.
2225 if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
2229 * Get real filehandle for root of exported FS.
2231 bzero((caddr_t)&nfs_pub.np_handle, sizeof(nfs_pub.np_handle));
2232 nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid;
2234 if ((error = VFS_ROOT(mp, &rvp)))
2237 if ((error = VFS_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid)))
2243 * If an indexfile was specified, pull it in.
2245 if (argp->ex_indexfile != NULL) {
2248 error = vn_get_namelen(rvp, &namelen);
2251 nfs_pub.np_index = kmalloc(namelen, M_TEMP, M_WAITOK);
2252 error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
2256 * Check for illegal filenames.
2258 for (cp = nfs_pub.np_index; *cp; cp++) {
2266 kfree(nfs_pub.np_index, M_TEMP);
2271 nfs_pub.np_mount = mp;
2272 nfs_pub.np_valid = 1;
2277 vfs_export_lookup(struct mount *mp, struct netexport *nep,
2278 struct sockaddr *nam)
2281 struct radix_node_head *rnh;
2282 struct sockaddr *saddr;
2285 if (mp->mnt_flag & MNT_EXPORTED) {
2287 * Lookup in the export list first.
2292 switch (saddr->sa_family) {
2295 rnh = nep->ne_inethead;
2300 rnh = nep->ne_inet6head;
2307 np = (struct netcred *)
2308 rnh->rnh_matchaddr(saddr, rnh);
2309 if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
2315 * If no address match, use the default if it exists.
2317 if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
2318 np = &nep->ne_defexported;
2324 * perform msync on all vnodes under a mount point. The mount point must
2325 * be locked. This code is also responsible for lazy-freeing unreferenced
2326 * vnodes whos VM objects no longer contain pages.
2328 * NOTE: MNT_WAIT still skips vnodes in the VXLOCK state.
2330 * NOTE: XXX VOP_PUTPAGES and friends requires that the vnode be locked,
2331 * but vnode_pager_putpages() doesn't lock the vnode. We have to do it
2332 * way up in this high level function.
2334 static int vfs_msync_scan1(struct mount *mp, struct vnode *vp, void *data);
2335 static int vfs_msync_scan2(struct mount *mp, struct vnode *vp, void *data);
2338 vfs_msync(struct mount *mp, int flags)
2343 * tmpfs sets this flag to prevent msync(), sync, and the
2344 * filesystem periodic syncer from trying to flush VM pages
2345 * to swap. Only pure memory pressure flushes tmpfs VM pages
2348 if (mp->mnt_kern_flag & MNTK_NOMSYNC)
2352 * Ok, scan the vnodes for work. If the filesystem is using the
2353 * syncer thread feature we can use vsyncscan() instead of
2354 * vmntvnodescan(), which is much faster.
2356 vmsc_flags = VMSC_GETVP;
2357 if (flags != MNT_WAIT)
2358 vmsc_flags |= VMSC_NOWAIT;
2360 if (mp->mnt_kern_flag & MNTK_THR_SYNC) {
2361 vsyncscan(mp, vmsc_flags, vfs_msync_scan2,
2362 (void *)(intptr_t)flags);
2364 vmntvnodescan(mp, vmsc_flags,
2365 vfs_msync_scan1, vfs_msync_scan2,
2366 (void *)(intptr_t)flags);
2371 * scan1 is a fast pre-check. There could be hundreds of thousands of
2372 * vnodes, we cannot afford to do anything heavy weight until we have a
2373 * fairly good indication that there is work to do.
2375 * The new namecache holds the vnode for each v_namecache association
2376 * so allow these refs.
2380 vfs_msync_scan1(struct mount *mp, struct vnode *vp, void *data)
2382 int flags = (int)(intptr_t)data;
2384 if ((vp->v_flag & VRECLAIMED) == 0) {
2385 if (vp->v_auxrefs == vp->v_namecache_count &&
2386 VREFCNT(vp) <= 0 && vp->v_object) {
2387 return(0); /* call scan2 */
2389 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
2390 (vp->v_flag & VOBJDIRTY) &&
2391 (flags == MNT_WAIT || vn_islocked(vp) == 0)) {
2392 return(0); /* call scan2 */
2397 * do not call scan2, continue the loop
2403 * This callback is handed a locked vnode.
2407 vfs_msync_scan2(struct mount *mp, struct vnode *vp, void *data)
2410 int flags = (int)(intptr_t)data;
2413 if (vp->v_flag & VRECLAIMED)
2416 if ((mp->mnt_flag & MNT_RDONLY) == 0 && (vp->v_flag & VOBJDIRTY)) {
2417 if ((obj = vp->v_object) != NULL) {
2418 if (flags == MNT_WAIT) {
2420 * VFS_MSYNC is called with MNT_WAIT when
2423 opcflags = OBJPC_SYNC;
2424 } else if (vp->v_writecount || obj->ref_count) {
2426 * VFS_MSYNC is otherwise called via the
2427 * periodic filesystem sync or the 'sync'
2428 * command. Honor MADV_NOSYNC / MAP_NOSYNC
2429 * if the file is open for writing or memory
2430 * mapped. Pages flagged PG_NOSYNC will not
2431 * be automatically flushed at this time.
2433 * The obj->ref_count test is not perfect
2434 * since temporary refs may be present, but
2435 * the periodic filesystem sync will ultimately
2436 * catch it if the file is not open and not
2439 opcflags = OBJPC_NOSYNC;
2442 * If the file is no longer open for writing
2443 * and also no longer mapped, do not honor
2444 * MAP_NOSYNC. That is, fully synchronize
2447 * This still occurs on the periodic fs sync,
2448 * so frontend programs which turn the file
2449 * over quickly enough can still avoid the
2450 * sync, but ultimately we do want to flush
2451 * even MADV_NOSYNC pages once it is no longer
2452 * mapped or open for writing.
2456 vm_object_page_clean(obj, 0, 0, opcflags);
2463 * Wake up anyone interested in vp because it is being revoked.
2466 vn_gone(struct vnode *vp)
2468 lwkt_gettoken(&vp->v_token);
2469 KNOTE(&vp->v_pollinfo.vpi_kqinfo.ki_note, NOTE_REVOKE);
2470 lwkt_reltoken(&vp->v_token);
2474 * extract the cdev_t from a VBLK or VCHR. The vnode must have been opened
2475 * (or v_rdev might be NULL).
2478 vn_todev(struct vnode *vp)
2480 if (vp->v_type != VBLK && vp->v_type != VCHR)
2482 KKASSERT(vp->v_rdev != NULL);
2483 return (vp->v_rdev);
2487 * Check if vnode represents a disk device. The vnode does not need to be
2493 vn_isdisk(struct vnode *vp, int *errp)
2497 if (vp->v_type != VCHR) {
2510 if (dev_is_good(dev) == 0) {
2515 if ((dev_dflags(dev) & D_DISK) == 0) {
2526 vn_get_namelen(struct vnode *vp, int *namelen)
2529 register_t retval[2];
2531 error = VOP_PATHCONF(vp, _PC_NAME_MAX, retval);
2534 *namelen = (int)retval[0];
2539 vop_write_dirent(int *error, struct uio *uio, ino_t d_ino, uint8_t d_type,
2540 uint16_t d_namlen, const char *d_name)
2545 len = _DIRENT_RECLEN(d_namlen);
2546 if (len > uio->uio_resid)
2549 dp = kmalloc(len, M_TEMP, M_WAITOK | M_ZERO);
2552 dp->d_namlen = d_namlen;
2553 dp->d_type = d_type;
2554 bcopy(d_name, dp->d_name, d_namlen);
2556 *error = uiomove((caddr_t)dp, len, uio);
2564 vn_mark_atime(struct vnode *vp, struct thread *td)
2566 struct proc *p = td->td_proc;
2567 struct ucred *cred = p ? p->p_ucred : proc0.p_ucred;
2569 if ((vp->v_mount->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0) {
2570 VOP_MARKATIME(vp, cred);
2575 * Calculate the number of entries in an inode-related chained hash table.
2576 * With today's memory sizes, maxvnodes can wind up being a very large
2577 * number. There is no reason to waste memory, so tolerate some stacking.
2580 vfs_inodehashsize(void)
2585 while (hsize < maxvnodes)
2587 while (hsize > maxvnodes * 2)
2588 hsize >>= 1; /* nominal 2x stacking */
2590 if (maxvnodes > 1024 * 1024)
2591 hsize >>= 1; /* nominal 8x stacking */
2593 if (maxvnodes > 128 * 1024)
2594 hsize >>= 1; /* nominal 4x stacking */
2607 #define SETHIGH(q, h) { \
2610 tmp.val[_QUAD_HIGHWORD] = (h); \
2613 #define SETLOW(q, l) { \
2616 tmp.val[_QUAD_LOWWORD] = (l); \
2621 init_va_filerev(void)
2626 getmicrouptime(&tv);
2627 SETHIGH(ret, tv.tv_sec);
2628 SETLOW(ret, tv.tv_usec * 4294);
2634 * Set default timestamp_precision. If hz is reasonably high we go for
2635 * performance and limit vfs timestamps to microseconds with tick resolution.
2636 * If hz is too low, however, we lose a bit of performance to get a more
2637 * precise timestamp, because the mtime/ctime granularity might just be too
2638 * rough otherwise (for make and Makefile's, for example).
2641 vfs_ts_prec_init(void *dummy)
2643 if (timestamp_precision < 0) {
2645 timestamp_precision = TSP_USEC;
2647 timestamp_precision = TSP_USEC_PRECISE;
2650 SYSINIT(vfs_ts_prec_init, SI_SUB_VFS, SI_ORDER_ANY, vfs_ts_prec_init, NULL);