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 <sys/param.h>
44 #include <sys/systm.h>
47 #include <sys/dirent.h>
48 #include <sys/domain.h>
49 #include <sys/eventhandler.h>
50 #include <sys/fcntl.h>
52 #include <sys/kernel.h>
53 #include <sys/kthread.h>
54 #include <sys/malloc.h>
56 #include <sys/mount.h>
59 #include <sys/reboot.h>
60 #include <sys/socket.h>
62 #include <sys/sysctl.h>
63 #include <sys/syslog.h>
64 #include <sys/unistd.h>
65 #include <sys/vmmeter.h>
66 #include <sys/vnode.h>
68 #include <machine/limits.h>
71 #include <vm/vm_object.h>
72 #include <vm/vm_extern.h>
73 #include <vm/vm_kern.h>
75 #include <vm/vm_map.h>
76 #include <vm/vm_page.h>
77 #include <vm/vm_pager.h>
78 #include <vm/vnode_pager.h>
79 #include <vm/vm_zone.h>
82 #include <sys/thread2.h>
83 #include <sys/sysref2.h>
84 #include <sys/mplock2.h>
86 static MALLOC_DEFINE(M_NETADDR, "Export Host", "Export host address structure");
89 SYSCTL_INT(_debug, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0,
90 "Number of vnodes allocated");
92 SYSCTL_INT(_debug, OID_AUTO, verbose_reclaims, CTLFLAG_RD, &verbose_reclaims, 0,
93 "Output filename of reclaimed vnode(s)");
95 enum vtype iftovt_tab[16] = {
96 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
97 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
100 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
101 S_IFSOCK, S_IFIFO, S_IFMT,
104 static int reassignbufcalls;
105 SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW, &reassignbufcalls,
106 0, "Number of times buffers have been reassigned to the proper list");
108 static int check_buf_overlap = 2; /* invasive check */
109 SYSCTL_INT(_vfs, OID_AUTO, check_buf_overlap, CTLFLAG_RW, &check_buf_overlap,
110 0, "Enable overlapping buffer checks");
112 int nfs_mount_type = -1;
113 static struct lwkt_token spechash_token;
114 struct nfs_public nfs_pub; /* publicly exported FS */
117 SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RW,
118 &desiredvnodes, 0, "Maximum number of vnodes");
120 static void vfs_free_addrlist (struct netexport *nep);
121 static int vfs_free_netcred (struct radix_node *rn, void *w);
122 static int vfs_hang_addrlist (struct mount *mp, struct netexport *nep,
123 const struct export_args *argp);
125 int prtactive = 0; /* 1 => print out reclaim of active vnodes */
128 * Red black tree functions
130 static int rb_buf_compare(struct buf *b1, struct buf *b2);
131 RB_GENERATE2(buf_rb_tree, buf, b_rbnode, rb_buf_compare, off_t, b_loffset);
132 RB_GENERATE2(buf_rb_hash, buf, b_rbhash, rb_buf_compare, off_t, b_loffset);
135 rb_buf_compare(struct buf *b1, struct buf *b2)
137 if (b1->b_loffset < b2->b_loffset)
139 if (b1->b_loffset > b2->b_loffset)
145 * Initialize the vnode management data structures.
147 * Called from vfsinit()
156 * Desiredvnodes is kern.maxvnodes. We want to scale it
157 * according to available system memory but we may also have
158 * to limit it based on available KVM, which is capped on 32 bit
159 * systems, to ~80K vnodes or so.
161 * WARNING! For machines with 64-256M of ram we have to be sure
162 * that the default limit scales down well due to HAMMER
163 * taking up significantly more memory per-vnode vs UFS.
164 * We want around ~5800 on a 128M machine.
166 factor1 = 20 * (sizeof(struct vm_object) + sizeof(struct vnode));
167 factor2 = 25 * (sizeof(struct vm_object) + sizeof(struct vnode));
169 imin((int64_t)vmstats.v_page_count * PAGE_SIZE / factor1,
171 desiredvnodes = imax(desiredvnodes, maxproc * 8);
173 lwkt_token_init(&spechash_token, "spechash");
177 * Knob to control the precision of file timestamps:
179 * 0 = seconds only; nanoseconds zeroed.
180 * 1 = seconds and nanoseconds, accurate within 1/HZ.
181 * 2 = seconds and nanoseconds, truncated to microseconds.
182 * >=3 = seconds and nanoseconds, maximum precision.
184 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC };
186 static int timestamp_precision = TSP_SEC;
187 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW,
188 ×tamp_precision, 0, "Precision of file timestamps");
191 * Get a current timestamp.
196 vfs_timestamp(struct timespec *tsp)
200 switch (timestamp_precision) {
202 tsp->tv_sec = time_second;
210 TIMEVAL_TO_TIMESPEC(&tv, tsp);
220 * Set vnode attributes to VNOVAL
223 vattr_null(struct vattr *vap)
226 vap->va_size = VNOVAL;
227 vap->va_bytes = VNOVAL;
228 vap->va_mode = VNOVAL;
229 vap->va_nlink = VNOVAL;
230 vap->va_uid = VNOVAL;
231 vap->va_gid = VNOVAL;
232 vap->va_fsid = VNOVAL;
233 vap->va_fileid = VNOVAL;
234 vap->va_blocksize = VNOVAL;
235 vap->va_rmajor = VNOVAL;
236 vap->va_rminor = VNOVAL;
237 vap->va_atime.tv_sec = VNOVAL;
238 vap->va_atime.tv_nsec = VNOVAL;
239 vap->va_mtime.tv_sec = VNOVAL;
240 vap->va_mtime.tv_nsec = VNOVAL;
241 vap->va_ctime.tv_sec = VNOVAL;
242 vap->va_ctime.tv_nsec = VNOVAL;
243 vap->va_flags = VNOVAL;
244 vap->va_gen = VNOVAL;
246 /* va_*_uuid fields are only valid if related flags are set */
250 * Flush out and invalidate all buffers associated with a vnode.
254 static int vinvalbuf_bp(struct buf *bp, void *data);
256 struct vinvalbuf_bp_info {
265 vinvalbuf(struct vnode *vp, int flags, int slpflag, int slptimeo)
267 struct vinvalbuf_bp_info info;
271 lwkt_gettoken(&vp->v_token);
274 * If we are being asked to save, call fsync to ensure that the inode
277 if (flags & V_SAVE) {
278 error = bio_track_wait(&vp->v_track_write, slpflag, slptimeo);
281 if (!RB_EMPTY(&vp->v_rbdirty_tree)) {
282 if ((error = VOP_FSYNC(vp, MNT_WAIT, 0)) != 0)
286 * Dirty bufs may be left or generated via races
287 * in circumstances where vinvalbuf() is called on
288 * a vnode not undergoing reclamation. Only
289 * panic if we are trying to reclaim the vnode.
291 if ((vp->v_flag & VRECLAIMED) &&
292 (bio_track_active(&vp->v_track_write) ||
293 !RB_EMPTY(&vp->v_rbdirty_tree))) {
294 panic("vinvalbuf: dirty bufs");
299 info.slptimeo = slptimeo;
300 info.lkflags = LK_EXCLUSIVE | LK_SLEEPFAIL;
301 if (slpflag & PCATCH)
302 info.lkflags |= LK_PCATCH;
307 * Flush the buffer cache until nothing is left, wait for all I/O
308 * to complete. At least one pass is required. We might block
309 * in the pip code so we have to re-check. Order is important.
315 if (!RB_EMPTY(&vp->v_rbclean_tree)) {
317 error = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree,
318 NULL, vinvalbuf_bp, &info);
320 if (!RB_EMPTY(&vp->v_rbdirty_tree)) {
322 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
323 NULL, vinvalbuf_bp, &info);
327 * Wait for I/O completion.
329 bio_track_wait(&vp->v_track_write, 0, 0);
330 if ((object = vp->v_object) != NULL)
331 refcount_wait(&object->paging_in_progress, "vnvlbx");
332 } while (bio_track_active(&vp->v_track_write) ||
333 !RB_EMPTY(&vp->v_rbclean_tree) ||
334 !RB_EMPTY(&vp->v_rbdirty_tree));
337 * Destroy the copy in the VM cache, too.
339 if ((object = vp->v_object) != NULL) {
340 vm_object_page_remove(object, 0, 0,
341 (flags & V_SAVE) ? TRUE : FALSE);
344 if (!RB_EMPTY(&vp->v_rbdirty_tree) || !RB_EMPTY(&vp->v_rbclean_tree))
345 panic("vinvalbuf: flush failed");
346 if (!RB_EMPTY(&vp->v_rbhash_tree))
347 panic("vinvalbuf: flush failed, buffers still present");
350 lwkt_reltoken(&vp->v_token);
355 vinvalbuf_bp(struct buf *bp, void *data)
357 struct vinvalbuf_bp_info *info = data;
360 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
361 atomic_add_int(&bp->b_refs, 1);
362 error = BUF_TIMELOCK(bp, info->lkflags,
363 "vinvalbuf", info->slptimeo);
364 atomic_subtract_int(&bp->b_refs, 1);
373 KKASSERT(bp->b_vp == info->vp);
376 * Must check clean/dirty status after successfully locking as
379 if ((info->clean && (bp->b_flags & B_DELWRI)) ||
380 (info->clean == 0 && (bp->b_flags & B_DELWRI) == 0)) {
386 * NOTE: NO B_LOCKED CHECK. Also no buf_checkwrite()
387 * check. This code will write out the buffer, period.
390 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
391 (info->flags & V_SAVE)) {
393 } else if (info->flags & V_SAVE) {
395 * Cannot set B_NOCACHE on a clean buffer as this will
396 * destroy the VM backing store which might actually
397 * be dirty (and unsynchronized).
399 bp->b_flags |= (B_INVAL | B_RELBUF);
402 bp->b_flags |= (B_INVAL | B_NOCACHE | B_RELBUF);
409 * Truncate a file's buffer and pages to a specified length. This
410 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
413 * The vnode must be locked.
415 static int vtruncbuf_bp_trunc_cmp(struct buf *bp, void *data);
416 static int vtruncbuf_bp_trunc(struct buf *bp, void *data);
417 static int vtruncbuf_bp_metasync_cmp(struct buf *bp, void *data);
418 static int vtruncbuf_bp_metasync(struct buf *bp, void *data);
420 struct vtruncbuf_info {
427 vtruncbuf(struct vnode *vp, off_t length, int blksize)
429 struct vtruncbuf_info info;
430 const char *filename;
434 * Round up to the *next* block, then destroy the buffers in question.
435 * Since we are only removing some of the buffers we must rely on the
436 * scan count to determine whether a loop is necessary.
438 if ((count = (int)(length % blksize)) != 0)
439 info.truncloffset = length + (blksize - count);
441 info.truncloffset = length;
444 lwkt_gettoken(&vp->v_token);
447 count = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree,
448 vtruncbuf_bp_trunc_cmp,
449 vtruncbuf_bp_trunc, &info);
451 count += RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
452 vtruncbuf_bp_trunc_cmp,
453 vtruncbuf_bp_trunc, &info);
457 * For safety, fsync any remaining metadata if the file is not being
458 * truncated to 0. Since the metadata does not represent the entire
459 * dirty list we have to rely on the hit count to ensure that we get
464 count = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
465 vtruncbuf_bp_metasync_cmp,
466 vtruncbuf_bp_metasync, &info);
471 * Clean out any left over VM backing store.
473 * It is possible to have in-progress I/O from buffers that were
474 * not part of the truncation. This should not happen if we
475 * are truncating to 0-length.
477 vnode_pager_setsize(vp, length);
478 bio_track_wait(&vp->v_track_write, 0, 0);
483 spin_lock(&vp->v_spin);
484 filename = TAILQ_FIRST(&vp->v_namecache) ?
485 TAILQ_FIRST(&vp->v_namecache)->nc_name : "?";
486 spin_unlock(&vp->v_spin);
489 * Make sure no buffers were instantiated while we were trying
490 * to clean out the remaining VM pages. This could occur due
491 * to busy dirty VM pages being flushed out to disk.
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);
503 kprintf("Warning: vtruncbuf(): Had to re-clean %d "
504 "left over buffers in %s\n", count, filename);
508 lwkt_reltoken(&vp->v_token);
514 * The callback buffer is beyond the new file EOF and must be destroyed.
515 * Note that the compare function must conform to the RB_SCAN's requirements.
519 vtruncbuf_bp_trunc_cmp(struct buf *bp, void *data)
521 struct vtruncbuf_info *info = data;
523 if (bp->b_loffset >= info->truncloffset)
530 vtruncbuf_bp_trunc(struct buf *bp, void *data)
532 struct vtruncbuf_info *info = data;
535 * Do not try to use a buffer we cannot immediately lock, but sleep
536 * anyway to prevent a livelock. The code will loop until all buffers
539 * We must always revalidate the buffer after locking it to deal
542 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
543 atomic_add_int(&bp->b_refs, 1);
544 if (BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL) == 0)
546 atomic_subtract_int(&bp->b_refs, 1);
547 } else if ((info->clean && (bp->b_flags & B_DELWRI)) ||
548 (info->clean == 0 && (bp->b_flags & B_DELWRI) == 0) ||
549 bp->b_vp != info->vp ||
550 vtruncbuf_bp_trunc_cmp(bp, data)) {
554 bp->b_flags |= (B_INVAL | B_RELBUF | B_NOCACHE);
561 * Fsync all meta-data after truncating a file to be non-zero. Only metadata
562 * blocks (with a negative loffset) are scanned.
563 * Note that the compare function must conform to the RB_SCAN's requirements.
566 vtruncbuf_bp_metasync_cmp(struct buf *bp, void *data __unused)
568 if (bp->b_loffset < 0)
574 vtruncbuf_bp_metasync(struct buf *bp, void *data)
576 struct vtruncbuf_info *info = data;
578 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
579 atomic_add_int(&bp->b_refs, 1);
580 if (BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL) == 0)
582 atomic_subtract_int(&bp->b_refs, 1);
583 } else if ((bp->b_flags & B_DELWRI) == 0 ||
584 bp->b_vp != info->vp ||
585 vtruncbuf_bp_metasync_cmp(bp, data)) {
589 if (bp->b_vp == info->vp)
598 * vfsync - implements a multipass fsync on a file which understands
599 * dependancies and meta-data. The passed vnode must be locked. The
600 * waitfor argument may be MNT_WAIT or MNT_NOWAIT, or MNT_LAZY.
602 * When fsyncing data asynchronously just do one consolidated pass starting
603 * with the most negative block number. This may not get all the data due
606 * When fsyncing data synchronously do a data pass, then a metadata pass,
607 * then do additional data+metadata passes to try to get all the data out.
609 * Caller must ref the vnode but does not have to lock it.
611 static int vfsync_wait_output(struct vnode *vp,
612 int (*waitoutput)(struct vnode *, struct thread *));
613 static int vfsync_dummy_cmp(struct buf *bp __unused, void *data __unused);
614 static int vfsync_data_only_cmp(struct buf *bp, void *data);
615 static int vfsync_meta_only_cmp(struct buf *bp, void *data);
616 static int vfsync_lazy_range_cmp(struct buf *bp, void *data);
617 static int vfsync_bp(struct buf *bp, void *data);
626 int (*checkdef)(struct buf *);
627 int (*cmpfunc)(struct buf *, void *);
631 vfsync(struct vnode *vp, int waitfor, int passes,
632 int (*checkdef)(struct buf *),
633 int (*waitoutput)(struct vnode *, struct thread *))
635 struct vfsync_info info;
638 bzero(&info, sizeof(info));
640 if ((info.checkdef = checkdef) == NULL)
643 lwkt_gettoken(&vp->v_token);
646 case MNT_LAZY | MNT_NOWAIT:
649 * Lazy (filesystem syncer typ) Asynchronous plus limit the
650 * number of data (not meta) pages we try to flush to 1MB.
651 * A non-zero return means that lazy limit was reached.
653 info.lazylimit = 1024 * 1024;
655 info.cmpfunc = vfsync_lazy_range_cmp;
656 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
657 vfsync_lazy_range_cmp, vfsync_bp, &info);
658 info.cmpfunc = vfsync_meta_only_cmp;
659 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
660 vfsync_meta_only_cmp, vfsync_bp, &info);
663 else if (!RB_EMPTY(&vp->v_rbdirty_tree))
664 vn_syncer_add(vp, 1);
669 * Asynchronous. Do a data-only pass and a meta-only pass.
672 info.cmpfunc = vfsync_data_only_cmp;
673 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_data_only_cmp,
675 info.cmpfunc = vfsync_meta_only_cmp;
676 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_meta_only_cmp,
682 * Synchronous. Do a data-only pass, then a meta-data+data
683 * pass, then additional integrated passes to try to get
684 * all the dependancies flushed.
686 info.cmpfunc = vfsync_data_only_cmp;
687 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_data_only_cmp,
689 error = vfsync_wait_output(vp, waitoutput);
691 info.skippedbufs = 0;
692 info.cmpfunc = vfsync_dummy_cmp;
693 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
695 error = vfsync_wait_output(vp, waitoutput);
696 if (info.skippedbufs) {
697 kprintf("Warning: vfsync skipped %d dirty "
698 "bufs in pass2!\n", info.skippedbufs);
701 while (error == 0 && passes > 0 &&
702 !RB_EMPTY(&vp->v_rbdirty_tree)
705 info.synchronous = 1;
708 info.cmpfunc = vfsync_dummy_cmp;
709 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
715 error = vfsync_wait_output(vp, waitoutput);
719 lwkt_reltoken(&vp->v_token);
724 vfsync_wait_output(struct vnode *vp,
725 int (*waitoutput)(struct vnode *, struct thread *))
729 error = bio_track_wait(&vp->v_track_write, 0, 0);
731 error = waitoutput(vp, curthread);
736 vfsync_dummy_cmp(struct buf *bp __unused, void *data __unused)
742 vfsync_data_only_cmp(struct buf *bp, void *data)
744 if (bp->b_loffset < 0)
750 vfsync_meta_only_cmp(struct buf *bp, void *data)
752 if (bp->b_loffset < 0)
758 vfsync_lazy_range_cmp(struct buf *bp, void *data)
760 struct vfsync_info *info = data;
762 if (bp->b_loffset < info->vp->v_lazyw)
768 vfsync_bp(struct buf *bp, void *data)
770 struct vfsync_info *info = data;
771 struct vnode *vp = info->vp;
775 * Ignore buffers that we cannot immediately lock.
777 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
783 * We must revalidate the buffer after locking.
785 if ((bp->b_flags & B_DELWRI) == 0 ||
786 bp->b_vp != info->vp ||
787 info->cmpfunc(bp, data)) {
793 * If syncdeps is not set we do not try to write buffers which have
796 if (!info->synchronous && info->syncdeps == 0 && info->checkdef(bp)) {
802 * B_NEEDCOMMIT (primarily used by NFS) is a state where the buffer
803 * has been written but an additional handshake with the device
804 * is required before we can dispose of the buffer. We have no idea
805 * how to do this so we have to skip these buffers.
807 if (bp->b_flags & B_NEEDCOMMIT) {
813 * Ask bioops if it is ok to sync. If not the VFS may have
814 * set B_LOCKED so we have to cycle the buffer.
816 if (LIST_FIRST(&bp->b_dep) != NULL && buf_checkwrite(bp)) {
822 if (info->synchronous) {
824 * Synchronous flushing. An error may be returned.
830 * Asynchronous flushing. A negative return value simply
831 * stops the scan and is not considered an error. We use
832 * this to support limited MNT_LAZY flushes.
834 vp->v_lazyw = bp->b_loffset;
836 info->lazycount += cluster_awrite(bp);
837 waitrunningbufspace();
839 if (info->lazylimit && info->lazycount >= info->lazylimit)
848 * Associate a buffer with a vnode.
853 bgetvp(struct vnode *vp, struct buf *bp, int testsize)
855 KASSERT(bp->b_vp == NULL, ("bgetvp: not free"));
856 KKASSERT((bp->b_flags & (B_HASHED|B_DELWRI|B_VNCLEAN|B_VNDIRTY)) == 0);
859 * Insert onto list for new vnode.
861 lwkt_gettoken(&vp->v_token);
863 if (buf_rb_hash_RB_INSERT(&vp->v_rbhash_tree, bp)) {
864 lwkt_reltoken(&vp->v_token);
869 * Diagnostics (mainly for HAMMER debugging). Check for
870 * overlapping buffers.
872 if (check_buf_overlap) {
874 bx = buf_rb_hash_RB_PREV(bp);
876 if (bx->b_loffset + bx->b_bufsize > bp->b_loffset) {
877 kprintf("bgetvp: overlapl %016jx/%d %016jx "
879 (intmax_t)bx->b_loffset,
881 (intmax_t)bp->b_loffset,
883 if (check_buf_overlap > 1)
884 panic("bgetvp - overlapping buffer");
887 bx = buf_rb_hash_RB_NEXT(bp);
889 if (bp->b_loffset + testsize > bx->b_loffset) {
890 kprintf("bgetvp: overlapr %016jx/%d %016jx "
892 (intmax_t)bp->b_loffset,
894 (intmax_t)bx->b_loffset,
896 if (check_buf_overlap > 1)
897 panic("bgetvp - overlapping buffer");
902 bp->b_flags |= B_HASHED;
903 bp->b_flags |= B_VNCLEAN;
904 if (buf_rb_tree_RB_INSERT(&vp->v_rbclean_tree, bp))
905 panic("reassignbuf: dup lblk/clean vp %p bp %p", vp, bp);
907 lwkt_reltoken(&vp->v_token);
912 * Disassociate a buffer from a vnode.
917 brelvp(struct buf *bp)
921 KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
924 * Delete from old vnode list, if on one.
927 lwkt_gettoken(&vp->v_token);
928 if (bp->b_flags & (B_VNDIRTY | B_VNCLEAN)) {
929 if (bp->b_flags & B_VNDIRTY)
930 buf_rb_tree_RB_REMOVE(&vp->v_rbdirty_tree, bp);
932 buf_rb_tree_RB_REMOVE(&vp->v_rbclean_tree, bp);
933 bp->b_flags &= ~(B_VNDIRTY | B_VNCLEAN);
935 if (bp->b_flags & B_HASHED) {
936 buf_rb_hash_RB_REMOVE(&vp->v_rbhash_tree, bp);
937 bp->b_flags &= ~B_HASHED;
941 * Only remove from synclist when no dirty buffers are left AND
942 * the VFS has not flagged the vnode's inode as being dirty.
944 if ((vp->v_flag & (VONWORKLST | VISDIRTY | VOBJDIRTY)) == VONWORKLST &&
945 RB_EMPTY(&vp->v_rbdirty_tree)) {
946 vn_syncer_remove(vp);
950 lwkt_reltoken(&vp->v_token);
956 * Reassign the buffer to the proper clean/dirty list based on B_DELWRI.
957 * This routine is called when the state of the B_DELWRI bit is changed.
959 * Must be called with vp->v_token held.
963 reassignbuf(struct buf *bp)
965 struct vnode *vp = bp->b_vp;
968 ASSERT_LWKT_TOKEN_HELD(&vp->v_token);
972 * B_PAGING flagged buffers cannot be reassigned because their vp
973 * is not fully linked in.
975 if (bp->b_flags & B_PAGING)
976 panic("cannot reassign paging buffer");
978 if (bp->b_flags & B_DELWRI) {
980 * Move to the dirty list, add the vnode to the worklist
982 if (bp->b_flags & B_VNCLEAN) {
983 buf_rb_tree_RB_REMOVE(&vp->v_rbclean_tree, bp);
984 bp->b_flags &= ~B_VNCLEAN;
986 if ((bp->b_flags & B_VNDIRTY) == 0) {
987 if (buf_rb_tree_RB_INSERT(&vp->v_rbdirty_tree, bp)) {
988 panic("reassignbuf: dup lblk vp %p bp %p",
991 bp->b_flags |= B_VNDIRTY;
993 if ((vp->v_flag & VONWORKLST) == 0) {
994 switch (vp->v_type) {
1001 vp->v_rdev->si_mountpoint != NULL) {
1009 vn_syncer_add(vp, delay);
1013 * Move to the clean list, remove the vnode from the worklist
1014 * if no dirty blocks remain.
1016 if (bp->b_flags & B_VNDIRTY) {
1017 buf_rb_tree_RB_REMOVE(&vp->v_rbdirty_tree, bp);
1018 bp->b_flags &= ~B_VNDIRTY;
1020 if ((bp->b_flags & B_VNCLEAN) == 0) {
1021 if (buf_rb_tree_RB_INSERT(&vp->v_rbclean_tree, bp)) {
1022 panic("reassignbuf: dup lblk vp %p bp %p",
1025 bp->b_flags |= B_VNCLEAN;
1029 * Only remove from synclist when no dirty buffers are left
1030 * AND the VFS has not flagged the vnode's inode as being
1033 if ((vp->v_flag & (VONWORKLST | VISDIRTY | VOBJDIRTY)) ==
1035 RB_EMPTY(&vp->v_rbdirty_tree)) {
1036 vn_syncer_remove(vp);
1042 * Create a vnode for a block device. Used for mounting the root file
1045 * A vref()'d vnode is returned.
1047 extern struct vop_ops *devfs_vnode_dev_vops_p;
1049 bdevvp(cdev_t dev, struct vnode **vpp)
1059 error = getspecialvnode(VT_NON, NULL, &devfs_vnode_dev_vops_p,
1070 v_associate_rdev(vp, dev);
1071 vp->v_umajor = dev->si_umajor;
1072 vp->v_uminor = dev->si_uminor;
1079 v_associate_rdev(struct vnode *vp, cdev_t dev)
1083 if (dev_is_good(dev) == 0)
1085 KKASSERT(vp->v_rdev == NULL);
1086 vp->v_rdev = reference_dev(dev);
1087 lwkt_gettoken(&spechash_token);
1088 SLIST_INSERT_HEAD(&dev->si_hlist, vp, v_cdevnext);
1089 lwkt_reltoken(&spechash_token);
1094 v_release_rdev(struct vnode *vp)
1098 if ((dev = vp->v_rdev) != NULL) {
1099 lwkt_gettoken(&spechash_token);
1100 SLIST_REMOVE(&dev->si_hlist, vp, vnode, v_cdevnext);
1103 lwkt_reltoken(&spechash_token);
1108 * Add a vnode to the alias list hung off the cdev_t. We only associate
1109 * the device number with the vnode. The actual device is not associated
1110 * until the vnode is opened (usually in spec_open()), and will be
1111 * disassociated on last close.
1114 addaliasu(struct vnode *nvp, int x, int y)
1116 if (nvp->v_type != VBLK && nvp->v_type != VCHR)
1117 panic("addaliasu on non-special vnode");
1123 * Simple call that a filesystem can make to try to get rid of a
1124 * vnode. It will fail if anyone is referencing the vnode (including
1127 * The filesystem can check whether its in-memory inode structure still
1128 * references the vp on return.
1130 * May only be called if the vnode is in a known state (i.e. being prevented
1131 * from being deallocated by some other condition such as a vfs inode hold).
1134 vclean_unlocked(struct vnode *vp)
1137 if (VREFCNT(vp) <= 1)
1143 * Disassociate a vnode from its underlying filesystem.
1145 * The vnode must be VX locked and referenced. In all normal situations
1146 * there are no active references. If vclean_vxlocked() is called while
1147 * there are active references, the vnode is being ripped out and we have
1148 * to call VOP_CLOSE() as appropriate before we can reclaim it.
1151 vclean_vxlocked(struct vnode *vp, int flags)
1156 struct namecache *ncp;
1159 * If the vnode has already been reclaimed we have nothing to do.
1161 if (vp->v_flag & VRECLAIMED)
1165 * Set flag to interlock operation, flag finalization to ensure
1166 * that the vnode winds up on the inactive list, and set v_act to 0.
1168 vsetflags(vp, VRECLAIMED);
1169 atomic_set_int(&vp->v_refcnt, VREF_FINALIZE);
1172 if (verbose_reclaims) {
1173 if ((ncp = TAILQ_FIRST(&vp->v_namecache)) != NULL)
1174 kprintf("Debug: reclaim %p %s\n", vp, ncp->nc_name);
1178 * Scrap the vfs cache
1180 while (cache_inval_vp(vp, 0) != 0) {
1181 kprintf("Warning: vnode %p clean/cache_resolution "
1182 "race detected\n", vp);
1183 tsleep(vp, 0, "vclninv", 2);
1187 * Check to see if the vnode is in use. If so we have to reference it
1188 * before we clean it out so that its count cannot fall to zero and
1189 * generate a race against ourselves to recycle it.
1191 active = (VREFCNT(vp) > 0);
1194 * Clean out any buffers associated with the vnode and destroy its
1195 * object, if it has one.
1197 vinvalbuf(vp, V_SAVE, 0, 0);
1198 KKASSERT(lockcountnb(&vp->v_lock) == 1);
1201 * If purging an active vnode (typically during a forced unmount
1202 * or reboot), it must be closed and deactivated before being
1203 * reclaimed. This isn't really all that safe, but what can
1206 * Note that neither of these routines unlocks the vnode.
1208 if (active && (flags & DOCLOSE)) {
1209 while ((n = vp->v_opencount) != 0) {
1210 if (vp->v_writecount)
1211 VOP_CLOSE(vp, FWRITE|FNONBLOCK, NULL);
1213 VOP_CLOSE(vp, FNONBLOCK, NULL);
1214 if (vp->v_opencount == n) {
1215 kprintf("Warning: unable to force-close"
1223 * If the vnode has not been deactivated, deactivated it. Deactivation
1224 * can create new buffers and VM pages so we have to call vinvalbuf()
1225 * again to make sure they all get flushed.
1227 * This can occur if a file with a link count of 0 needs to be
1230 * If the vnode is already dead don't try to deactivate it.
1232 if ((vp->v_flag & VINACTIVE) == 0) {
1233 vsetflags(vp, VINACTIVE);
1236 vinvalbuf(vp, V_SAVE, 0, 0);
1238 KKASSERT(lockcountnb(&vp->v_lock) == 1);
1241 * If the vnode has an object, destroy it.
1243 while ((object = vp->v_object) != NULL) {
1244 vm_object_hold(object);
1245 if (object == vp->v_object)
1247 vm_object_drop(object);
1250 if (object != NULL) {
1251 if (object->ref_count == 0) {
1252 if ((object->flags & OBJ_DEAD) == 0)
1253 vm_object_terminate(object);
1254 vm_object_drop(object);
1255 vclrflags(vp, VOBJBUF);
1257 vm_pager_deallocate(object);
1258 vclrflags(vp, VOBJBUF);
1259 vm_object_drop(object);
1262 KKASSERT((vp->v_flag & VOBJBUF) == 0);
1265 * Reclaim the vnode if not already dead.
1267 if (vp->v_mount && VOP_RECLAIM(vp))
1268 panic("vclean: cannot reclaim");
1271 * Done with purge, notify sleepers of the grim news.
1273 vp->v_ops = &dead_vnode_vops_p;
1278 * If we are destroying an active vnode, reactivate it now that
1279 * we have reassociated it with deadfs. This prevents the system
1280 * from crashing on the vnode due to it being unexpectedly marked
1281 * as inactive or reclaimed.
1283 if (active && (flags & DOCLOSE)) {
1284 vclrflags(vp, VINACTIVE | VRECLAIMED);
1289 * Eliminate all activity associated with the requested vnode
1290 * and with all vnodes aliased to the requested vnode.
1292 * The vnode must be referenced but should not be locked.
1295 vrevoke(struct vnode *vp, struct ucred *cred)
1303 * If the vnode has a device association, scrap all vnodes associated
1304 * with the device. Don't let the device disappear on us while we
1305 * are scrapping the vnodes.
1307 * The passed vp will probably show up in the list, do not VX lock
1310 * Releasing the vnode's rdev here can mess up specfs's call to
1311 * device close, so don't do it. The vnode has been disassociated
1312 * and the device will be closed after the last ref on the related
1313 * fp goes away (if not still open by e.g. the kernel).
1315 if (vp->v_type != VCHR) {
1316 error = fdrevoke(vp, DTYPE_VNODE, cred);
1319 if ((dev = vp->v_rdev) == NULL) {
1323 lwkt_gettoken(&spechash_token);
1326 vqn = SLIST_FIRST(&dev->si_hlist);
1329 while ((vq = vqn) != NULL) {
1330 if (VREFCNT(vq) > 0) {
1332 fdrevoke(vq, DTYPE_VNODE, cred);
1333 /*v_release_rdev(vq);*/
1335 if (vq->v_rdev != dev) {
1340 vqn = SLIST_NEXT(vq, v_cdevnext);
1345 lwkt_reltoken(&spechash_token);
1352 * This is called when the object underlying a vnode is being destroyed,
1353 * such as in a remove(). Try to recycle the vnode immediately if the
1354 * only active reference is our reference.
1356 * Directory vnodes in the namecache with children cannot be immediately
1357 * recycled because numerous VOP_N*() ops require them to be stable.
1359 * To avoid recursive recycling from VOP_INACTIVE implemenetations this
1360 * function is a NOP if VRECLAIMED is already set.
1363 vrecycle(struct vnode *vp)
1365 if (VREFCNT(vp) <= 1 && (vp->v_flag & VRECLAIMED) == 0) {
1366 if (cache_inval_vp_nonblock(vp))
1375 * Return the maximum I/O size allowed for strategy calls on VP.
1377 * If vp is VCHR or VBLK we dive the device, otherwise we use
1378 * the vp's mount info.
1380 * The returned value is clamped at MAXPHYS as most callers cannot use
1381 * buffers larger than that size.
1384 vmaxiosize(struct vnode *vp)
1388 if (vp->v_type == VBLK || vp->v_type == VCHR)
1389 maxiosize = vp->v_rdev->si_iosize_max;
1391 maxiosize = vp->v_mount->mnt_iosize_max;
1393 if (maxiosize > MAXPHYS)
1394 maxiosize = MAXPHYS;
1399 * Eliminate all activity associated with a vnode in preparation for
1402 * The vnode must be VX locked and refd and will remain VX locked and refd
1403 * on return. This routine may be called with the vnode in any state, as
1404 * long as it is VX locked. The vnode will be cleaned out and marked
1405 * VRECLAIMED but will not actually be reused until all existing refs and
1408 * NOTE: This routine may be called on a vnode which has not yet been
1409 * already been deactivated (VOP_INACTIVE), or on a vnode which has
1410 * already been reclaimed.
1412 * This routine is not responsible for placing us back on the freelist.
1413 * Instead, it happens automatically when the caller releases the VX lock
1414 * (assuming there aren't any other references).
1417 vgone_vxlocked(struct vnode *vp)
1420 * assert that the VX lock is held. This is an absolute requirement
1421 * now for vgone_vxlocked() to be called.
1423 KKASSERT(lockcountnb(&vp->v_lock) == 1);
1426 * Clean out the filesystem specific data and set the VRECLAIMED
1427 * bit. Also deactivate the vnode if necessary.
1429 * The vnode should have automatically been removed from the syncer
1430 * list as syncer/dirty flags cleared during the cleaning.
1432 vclean_vxlocked(vp, DOCLOSE);
1433 KKASSERT((vp->v_flag & VONWORKLST) == 0);
1436 * Delete from old mount point vnode list, if on one.
1438 if (vp->v_mount != NULL) {
1439 KKASSERT(vp->v_data == NULL);
1440 insmntque(vp, NULL);
1444 * If special device, remove it from special device alias list
1445 * if it is on one. This should normally only occur if a vnode is
1446 * being revoked as the device should otherwise have been released
1449 if ((vp->v_type == VBLK || vp->v_type == VCHR) && vp->v_rdev != NULL) {
1460 * Lookup a vnode by device number.
1462 * Returns non-zero and *vpp set to a vref'd vnode on success.
1463 * Returns zero on failure.
1466 vfinddev(cdev_t dev, enum vtype type, struct vnode **vpp)
1470 lwkt_gettoken(&spechash_token);
1471 SLIST_FOREACH(vp, &dev->si_hlist, v_cdevnext) {
1472 if (type == vp->v_type) {
1475 lwkt_reltoken(&spechash_token);
1479 lwkt_reltoken(&spechash_token);
1484 * Calculate the total number of references to a special device. This
1485 * routine may only be called for VBLK and VCHR vnodes since v_rdev is
1486 * an overloaded field. Since udev2dev can now return NULL, we have
1487 * to check for a NULL v_rdev.
1490 count_dev(cdev_t dev)
1495 if (SLIST_FIRST(&dev->si_hlist)) {
1496 lwkt_gettoken(&spechash_token);
1497 SLIST_FOREACH(vp, &dev->si_hlist, v_cdevnext) {
1498 count += vp->v_opencount;
1500 lwkt_reltoken(&spechash_token);
1506 vcount(struct vnode *vp)
1508 if (vp->v_rdev == NULL)
1510 return(count_dev(vp->v_rdev));
1514 * Initialize VMIO for a vnode. This routine MUST be called before a
1515 * VFS can issue buffer cache ops on a vnode. It is typically called
1516 * when a vnode is initialized from its inode.
1519 vinitvmio(struct vnode *vp, off_t filesize, int blksize, int boff)
1524 object = vp->v_object;
1526 vm_object_hold(object);
1527 KKASSERT(vp->v_object == object);
1530 if (object == NULL) {
1531 object = vnode_pager_alloc(vp, filesize, 0, 0, blksize, boff);
1534 * Dereference the reference we just created. This assumes
1535 * that the object is associated with the vp. Allow it to
1536 * have zero refs. It cannot be destroyed as long as it
1537 * is associated with the vnode.
1539 vm_object_hold(object);
1540 atomic_add_int(&object->ref_count, -1);
1543 KKASSERT((object->flags & OBJ_DEAD) == 0);
1545 KASSERT(vp->v_object != NULL, ("vinitvmio: NULL object"));
1546 vsetflags(vp, VOBJBUF);
1547 vm_object_drop(object);
1554 * Print out a description of a vnode.
1556 static char *typename[] =
1557 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD"};
1560 vprint(char *label, struct vnode *vp)
1565 kprintf("%s: %p: ", label, (void *)vp);
1567 kprintf("%p: ", (void *)vp);
1568 kprintf("type %s, refcnt %08x, writecount %d, holdcnt %d,",
1569 typename[vp->v_type],
1570 vp->v_refcnt, vp->v_writecount, vp->v_auxrefs);
1572 if (vp->v_flag & VROOT)
1573 strcat(buf, "|VROOT");
1574 if (vp->v_flag & VPFSROOT)
1575 strcat(buf, "|VPFSROOT");
1576 if (vp->v_flag & VTEXT)
1577 strcat(buf, "|VTEXT");
1578 if (vp->v_flag & VSYSTEM)
1579 strcat(buf, "|VSYSTEM");
1580 if (vp->v_flag & VOBJBUF)
1581 strcat(buf, "|VOBJBUF");
1583 kprintf(" flags (%s)", &buf[1]);
1584 if (vp->v_data == NULL) {
1593 * Do the usual access checking.
1594 * file_mode, uid and gid are from the vnode in question,
1595 * while acc_mode and cred are from the VOP_ACCESS parameter list
1598 vaccess(enum vtype type, mode_t file_mode, uid_t uid, gid_t gid,
1599 mode_t acc_mode, struct ucred *cred)
1605 * Super-user always gets read/write access, but execute access depends
1606 * on at least one execute bit being set.
1608 if (priv_check_cred(cred, PRIV_ROOT, 0) == 0) {
1609 if ((acc_mode & VEXEC) && type != VDIR &&
1610 (file_mode & (S_IXUSR|S_IXGRP|S_IXOTH)) == 0)
1617 /* Otherwise, check the owner. */
1618 if (cred->cr_uid == uid) {
1619 if (acc_mode & VEXEC)
1621 if (acc_mode & VREAD)
1623 if (acc_mode & VWRITE)
1625 return ((file_mode & mask) == mask ? 0 : EACCES);
1628 /* Otherwise, check the groups. */
1629 ismember = groupmember(gid, cred);
1630 if (cred->cr_svgid == gid || ismember) {
1631 if (acc_mode & VEXEC)
1633 if (acc_mode & VREAD)
1635 if (acc_mode & VWRITE)
1637 return ((file_mode & mask) == mask ? 0 : EACCES);
1640 /* Otherwise, check everyone else. */
1641 if (acc_mode & VEXEC)
1643 if (acc_mode & VREAD)
1645 if (acc_mode & VWRITE)
1647 return ((file_mode & mask) == mask ? 0 : EACCES);
1651 #include <ddb/ddb.h>
1653 static int db_show_locked_vnodes(struct mount *mp, void *data);
1656 * List all of the locked vnodes in the system.
1657 * Called when debugging the kernel.
1659 DB_SHOW_COMMAND(lockedvnodes, lockedvnodes)
1661 kprintf("Locked vnodes\n");
1662 mountlist_scan(db_show_locked_vnodes, NULL,
1663 MNTSCAN_FORWARD|MNTSCAN_NOBUSY);
1667 db_show_locked_vnodes(struct mount *mp, void *data __unused)
1671 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
1672 if (vn_islocked(vp))
1680 * Top level filesystem related information gathering.
1682 static int sysctl_ovfs_conf (SYSCTL_HANDLER_ARGS);
1685 vfs_sysctl(SYSCTL_HANDLER_ARGS)
1687 int *name = (int *)arg1 - 1; /* XXX */
1688 u_int namelen = arg2 + 1; /* XXX */
1689 struct vfsconf *vfsp;
1692 #if 1 || defined(COMPAT_PRELITE2)
1693 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
1695 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
1699 /* all sysctl names at this level are at least name and field */
1701 return (ENOTDIR); /* overloaded */
1702 if (name[0] != VFS_GENERIC) {
1703 vfsp = vfsconf_find_by_typenum(name[0]);
1705 return (EOPNOTSUPP);
1706 return ((*vfsp->vfc_vfsops->vfs_sysctl)(&name[1], namelen - 1,
1707 oldp, oldlenp, newp, newlen, p));
1711 case VFS_MAXTYPENUM:
1714 maxtypenum = vfsconf_get_maxtypenum();
1715 return (SYSCTL_OUT(req, &maxtypenum, sizeof(maxtypenum)));
1718 return (ENOTDIR); /* overloaded */
1719 vfsp = vfsconf_find_by_typenum(name[2]);
1721 return (EOPNOTSUPP);
1722 return (SYSCTL_OUT(req, vfsp, sizeof *vfsp));
1724 return (EOPNOTSUPP);
1727 SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD, vfs_sysctl,
1728 "Generic filesystem");
1730 #if 1 || defined(COMPAT_PRELITE2)
1733 sysctl_ovfs_conf_iter(struct vfsconf *vfsp, void *data)
1736 struct ovfsconf ovfs;
1737 struct sysctl_req *req = (struct sysctl_req*) data;
1739 bzero(&ovfs, sizeof(ovfs));
1740 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
1741 strcpy(ovfs.vfc_name, vfsp->vfc_name);
1742 ovfs.vfc_index = vfsp->vfc_typenum;
1743 ovfs.vfc_refcount = vfsp->vfc_refcount;
1744 ovfs.vfc_flags = vfsp->vfc_flags;
1745 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
1747 return error; /* abort iteration with error code */
1749 return 0; /* continue iterating with next element */
1753 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
1755 return vfsconf_each(sysctl_ovfs_conf_iter, (void*)req);
1758 #endif /* 1 || COMPAT_PRELITE2 */
1761 * Check to see if a filesystem is mounted on a block device.
1764 vfs_mountedon(struct vnode *vp)
1768 if ((dev = vp->v_rdev) == NULL) {
1769 /* if (vp->v_type != VBLK)
1770 dev = get_dev(vp->v_uminor, vp->v_umajor); */
1772 if (dev != NULL && dev->si_mountpoint)
1778 * Unmount all filesystems. The list is traversed in reverse order
1779 * of mounting to avoid dependencies.
1782 static int vfs_umountall_callback(struct mount *mp, void *data);
1785 vfs_unmountall(void)
1790 count = mountlist_scan(vfs_umountall_callback,
1791 NULL, MNTSCAN_REVERSE|MNTSCAN_NOBUSY);
1797 vfs_umountall_callback(struct mount *mp, void *data)
1801 error = dounmount(mp, MNT_FORCE);
1803 mountlist_remove(mp);
1804 kprintf("unmount of filesystem mounted from %s failed (",
1805 mp->mnt_stat.f_mntfromname);
1809 kprintf("%d)\n", error);
1815 * Checks the mount flags for parameter mp and put the names comma-separated
1816 * into a string buffer buf with a size limit specified by len.
1818 * It returns the number of bytes written into buf, and (*errorp) will be
1819 * set to 0, EINVAL (if passed length is 0), or ENOSPC (supplied buffer was
1820 * not large enough). The buffer will be 0-terminated if len was not 0.
1823 vfs_flagstostr(int flags, const struct mountctl_opt *optp,
1824 char *buf, size_t len, int *errorp)
1826 static const struct mountctl_opt optnames[] = {
1827 { MNT_ASYNC, "asynchronous" },
1828 { MNT_EXPORTED, "NFS exported" },
1829 { MNT_LOCAL, "local" },
1830 { MNT_NOATIME, "noatime" },
1831 { MNT_NODEV, "nodev" },
1832 { MNT_NOEXEC, "noexec" },
1833 { MNT_NOSUID, "nosuid" },
1834 { MNT_NOSYMFOLLOW, "nosymfollow" },
1835 { MNT_QUOTA, "with-quotas" },
1836 { MNT_RDONLY, "read-only" },
1837 { MNT_SYNCHRONOUS, "synchronous" },
1838 { MNT_UNION, "union" },
1839 { MNT_NOCLUSTERR, "noclusterr" },
1840 { MNT_NOCLUSTERW, "noclusterw" },
1841 { MNT_SUIDDIR, "suiddir" },
1842 { MNT_SOFTDEP, "soft-updates" },
1843 { MNT_IGNORE, "ignore" },
1853 bleft = len - 1; /* leave room for trailing \0 */
1856 * Checks the size of the string. If it contains
1857 * any data, then we will append the new flags to
1860 actsize = strlen(buf);
1864 /* Default flags if no flags passed */
1868 if (bleft < 0) { /* degenerate case, 0-length buffer */
1873 for (; flags && optp->o_opt; ++optp) {
1874 if ((flags & optp->o_opt) == 0)
1876 optlen = strlen(optp->o_name);
1877 if (bwritten || actsize > 0) {
1882 buf[bwritten++] = ',';
1883 buf[bwritten++] = ' ';
1886 if (bleft < optlen) {
1890 bcopy(optp->o_name, buf + bwritten, optlen);
1893 flags &= ~optp->o_opt;
1897 * Space already reserved for trailing \0
1904 * Build hash lists of net addresses and hang them off the mount point.
1905 * Called by ufs_mount() to set up the lists of export addresses.
1908 vfs_hang_addrlist(struct mount *mp, struct netexport *nep,
1909 const struct export_args *argp)
1912 struct radix_node_head *rnh;
1914 struct radix_node *rn;
1915 struct sockaddr *saddr, *smask = NULL;
1919 if (argp->ex_addrlen == 0) {
1920 if (mp->mnt_flag & MNT_DEFEXPORTED)
1922 np = &nep->ne_defexported;
1923 np->netc_exflags = argp->ex_flags;
1924 np->netc_anon = argp->ex_anon;
1925 np->netc_anon.cr_ref = 1;
1926 mp->mnt_flag |= MNT_DEFEXPORTED;
1930 if (argp->ex_addrlen < 0 || argp->ex_addrlen > MLEN)
1932 if (argp->ex_masklen < 0 || argp->ex_masklen > MLEN)
1935 i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
1936 np = (struct netcred *) kmalloc(i, M_NETADDR, M_WAITOK | M_ZERO);
1937 saddr = (struct sockaddr *) (np + 1);
1938 if ((error = copyin(argp->ex_addr, (caddr_t) saddr, argp->ex_addrlen)))
1940 if (saddr->sa_len > argp->ex_addrlen)
1941 saddr->sa_len = argp->ex_addrlen;
1942 if (argp->ex_masklen) {
1943 smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen);
1944 error = copyin(argp->ex_mask, (caddr_t)smask, argp->ex_masklen);
1947 if (smask->sa_len > argp->ex_masklen)
1948 smask->sa_len = argp->ex_masklen;
1950 i = saddr->sa_family;
1951 if ((rnh = nep->ne_rtable[i]) == NULL) {
1953 * Seems silly to initialize every AF when most are not used,
1954 * do so on demand here
1956 SLIST_FOREACH(dom, &domains, dom_next)
1957 if (dom->dom_family == i && dom->dom_rtattach) {
1958 dom->dom_rtattach((void **) &nep->ne_rtable[i],
1962 if ((rnh = nep->ne_rtable[i]) == NULL) {
1967 rn = (*rnh->rnh_addaddr) ((char *) saddr, (char *) smask, rnh,
1969 if (rn == NULL || np != (struct netcred *) rn) { /* already exists */
1973 np->netc_exflags = argp->ex_flags;
1974 np->netc_anon = argp->ex_anon;
1975 np->netc_anon.cr_ref = 1;
1978 kfree(np, M_NETADDR);
1984 vfs_free_netcred(struct radix_node *rn, void *w)
1986 struct radix_node_head *rnh = (struct radix_node_head *) w;
1988 (*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, rnh);
1989 kfree((caddr_t) rn, M_NETADDR);
1994 * Free the net address hash lists that are hanging off the mount points.
1997 vfs_free_addrlist(struct netexport *nep)
2000 struct radix_node_head *rnh;
2002 for (i = 0; i <= AF_MAX; i++)
2003 if ((rnh = nep->ne_rtable[i])) {
2004 (*rnh->rnh_walktree) (rnh, vfs_free_netcred,
2006 kfree((caddr_t) rnh, M_RTABLE);
2007 nep->ne_rtable[i] = 0;
2012 vfs_export(struct mount *mp, struct netexport *nep,
2013 const struct export_args *argp)
2017 if (argp->ex_flags & MNT_DELEXPORT) {
2018 if (mp->mnt_flag & MNT_EXPUBLIC) {
2019 vfs_setpublicfs(NULL, NULL, NULL);
2020 mp->mnt_flag &= ~MNT_EXPUBLIC;
2022 vfs_free_addrlist(nep);
2023 mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
2025 if (argp->ex_flags & MNT_EXPORTED) {
2026 if (argp->ex_flags & MNT_EXPUBLIC) {
2027 if ((error = vfs_setpublicfs(mp, nep, argp)) != 0)
2029 mp->mnt_flag |= MNT_EXPUBLIC;
2031 if ((error = vfs_hang_addrlist(mp, nep, argp)))
2033 mp->mnt_flag |= MNT_EXPORTED;
2040 * Set the publicly exported filesystem (WebNFS). Currently, only
2041 * one public filesystem is possible in the spec (RFC 2054 and 2055)
2044 vfs_setpublicfs(struct mount *mp, struct netexport *nep,
2045 const struct export_args *argp)
2052 * mp == NULL -> invalidate the current info, the FS is
2053 * no longer exported. May be called from either vfs_export
2054 * or unmount, so check if it hasn't already been done.
2057 if (nfs_pub.np_valid) {
2058 nfs_pub.np_valid = 0;
2059 if (nfs_pub.np_index != NULL) {
2060 kfree(nfs_pub.np_index, M_TEMP);
2061 nfs_pub.np_index = NULL;
2068 * Only one allowed at a time.
2070 if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
2074 * Get real filehandle for root of exported FS.
2076 bzero((caddr_t)&nfs_pub.np_handle, sizeof(nfs_pub.np_handle));
2077 nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid;
2079 if ((error = VFS_ROOT(mp, &rvp)))
2082 if ((error = VFS_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid)))
2088 * If an indexfile was specified, pull it in.
2090 if (argp->ex_indexfile != NULL) {
2093 error = vn_get_namelen(rvp, &namelen);
2096 nfs_pub.np_index = kmalloc(namelen, M_TEMP, M_WAITOK);
2097 error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
2101 * Check for illegal filenames.
2103 for (cp = nfs_pub.np_index; *cp; cp++) {
2111 kfree(nfs_pub.np_index, M_TEMP);
2116 nfs_pub.np_mount = mp;
2117 nfs_pub.np_valid = 1;
2122 vfs_export_lookup(struct mount *mp, struct netexport *nep,
2123 struct sockaddr *nam)
2126 struct radix_node_head *rnh;
2127 struct sockaddr *saddr;
2130 if (mp->mnt_flag & MNT_EXPORTED) {
2132 * Lookup in the export list first.
2136 rnh = nep->ne_rtable[saddr->sa_family];
2138 np = (struct netcred *)
2139 (*rnh->rnh_matchaddr)((char *)saddr,
2141 if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
2146 * If no address match, use the default if it exists.
2148 if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
2149 np = &nep->ne_defexported;
2155 * perform msync on all vnodes under a mount point. The mount point must
2156 * be locked. This code is also responsible for lazy-freeing unreferenced
2157 * vnodes whos VM objects no longer contain pages.
2159 * NOTE: MNT_WAIT still skips vnodes in the VXLOCK state.
2161 * NOTE: XXX VOP_PUTPAGES and friends requires that the vnode be locked,
2162 * but vnode_pager_putpages() doesn't lock the vnode. We have to do it
2163 * way up in this high level function.
2165 static int vfs_msync_scan1(struct mount *mp, struct vnode *vp, void *data);
2166 static int vfs_msync_scan2(struct mount *mp, struct vnode *vp, void *data);
2169 vfs_msync(struct mount *mp, int flags)
2174 * tmpfs sets this flag to prevent msync(), sync, and the
2175 * filesystem periodic syncer from trying to flush VM pages
2176 * to swap. Only pure memory pressure flushes tmpfs VM pages
2179 if (mp->mnt_kern_flag & MNTK_NOMSYNC)
2183 * Ok, scan the vnodes for work. If the filesystem is using the
2184 * syncer thread feature we can use vsyncscan() instead of
2185 * vmntvnodescan(), which is much faster.
2187 vmsc_flags = VMSC_GETVP;
2188 if (flags != MNT_WAIT)
2189 vmsc_flags |= VMSC_NOWAIT;
2191 if (mp->mnt_kern_flag & MNTK_THR_SYNC) {
2192 vsyncscan(mp, vmsc_flags, vfs_msync_scan2,
2193 (void *)(intptr_t)flags);
2195 vmntvnodescan(mp, vmsc_flags,
2196 vfs_msync_scan1, vfs_msync_scan2,
2197 (void *)(intptr_t)flags);
2202 * scan1 is a fast pre-check. There could be hundreds of thousands of
2203 * vnodes, we cannot afford to do anything heavy weight until we have a
2204 * fairly good indication that there is work to do.
2208 vfs_msync_scan1(struct mount *mp, struct vnode *vp, void *data)
2210 int flags = (int)(intptr_t)data;
2212 if ((vp->v_flag & VRECLAIMED) == 0) {
2213 if (vp->v_auxrefs == 0 && VREFCNT(vp) <= 0 &&
2215 return(0); /* call scan2 */
2217 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
2218 (vp->v_flag & VOBJDIRTY) &&
2219 (flags == MNT_WAIT || vn_islocked(vp) == 0)) {
2220 return(0); /* call scan2 */
2225 * do not call scan2, continue the loop
2231 * This callback is handed a locked vnode.
2235 vfs_msync_scan2(struct mount *mp, struct vnode *vp, void *data)
2238 int flags = (int)(intptr_t)data;
2240 if (vp->v_flag & VRECLAIMED)
2243 if ((mp->mnt_flag & MNT_RDONLY) == 0 && (vp->v_flag & VOBJDIRTY)) {
2244 if ((obj = vp->v_object) != NULL) {
2245 vm_object_page_clean(obj, 0, 0,
2246 flags == MNT_WAIT ? OBJPC_SYNC : OBJPC_NOSYNC);
2253 * Wake up anyone interested in vp because it is being revoked.
2256 vn_gone(struct vnode *vp)
2258 lwkt_gettoken(&vp->v_token);
2259 KNOTE(&vp->v_pollinfo.vpi_kqinfo.ki_note, NOTE_REVOKE);
2260 lwkt_reltoken(&vp->v_token);
2264 * extract the cdev_t from a VBLK or VCHR. The vnode must have been opened
2265 * (or v_rdev might be NULL).
2268 vn_todev(struct vnode *vp)
2270 if (vp->v_type != VBLK && vp->v_type != VCHR)
2272 KKASSERT(vp->v_rdev != NULL);
2273 return (vp->v_rdev);
2277 * Check if vnode represents a disk device. The vnode does not need to be
2283 vn_isdisk(struct vnode *vp, int *errp)
2287 if (vp->v_type != VCHR) {
2300 if (dev_is_good(dev) == 0) {
2305 if ((dev_dflags(dev) & D_DISK) == 0) {
2316 vn_get_namelen(struct vnode *vp, int *namelen)
2319 register_t retval[2];
2321 error = VOP_PATHCONF(vp, _PC_NAME_MAX, retval);
2324 *namelen = (int)retval[0];
2329 vop_write_dirent(int *error, struct uio *uio, ino_t d_ino, uint8_t d_type,
2330 uint16_t d_namlen, const char *d_name)
2335 len = _DIRENT_RECLEN(d_namlen);
2336 if (len > uio->uio_resid)
2339 dp = kmalloc(len, M_TEMP, M_WAITOK | M_ZERO);
2342 dp->d_namlen = d_namlen;
2343 dp->d_type = d_type;
2344 bcopy(d_name, dp->d_name, d_namlen);
2346 *error = uiomove((caddr_t)dp, len, uio);
2354 vn_mark_atime(struct vnode *vp, struct thread *td)
2356 struct proc *p = td->td_proc;
2357 struct ucred *cred = p ? p->p_ucred : proc0.p_ucred;
2359 if ((vp->v_mount->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0) {
2360 VOP_MARKATIME(vp, cred);