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 static int vfsync_wait_output(struct vnode *vp,
610 int (*waitoutput)(struct vnode *, struct thread *));
611 static int vfsync_dummy_cmp(struct buf *bp __unused, void *data __unused);
612 static int vfsync_data_only_cmp(struct buf *bp, void *data);
613 static int vfsync_meta_only_cmp(struct buf *bp, void *data);
614 static int vfsync_lazy_range_cmp(struct buf *bp, void *data);
615 static int vfsync_bp(struct buf *bp, void *data);
624 int (*checkdef)(struct buf *);
625 int (*cmpfunc)(struct buf *, void *);
629 vfsync(struct vnode *vp, int waitfor, int passes,
630 int (*checkdef)(struct buf *),
631 int (*waitoutput)(struct vnode *, struct thread *))
633 struct vfsync_info info;
636 bzero(&info, sizeof(info));
638 if ((info.checkdef = checkdef) == NULL)
641 lwkt_gettoken(&vp->v_token);
644 case MNT_LAZY | MNT_NOWAIT:
647 * Lazy (filesystem syncer typ) Asynchronous plus limit the
648 * number of data (not meta) pages we try to flush to 1MB.
649 * A non-zero return means that lazy limit was reached.
651 info.lazylimit = 1024 * 1024;
653 info.cmpfunc = vfsync_lazy_range_cmp;
654 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
655 vfsync_lazy_range_cmp, vfsync_bp, &info);
656 info.cmpfunc = vfsync_meta_only_cmp;
657 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
658 vfsync_meta_only_cmp, vfsync_bp, &info);
661 else if (!RB_EMPTY(&vp->v_rbdirty_tree))
662 vn_syncer_add(vp, 1);
667 * Asynchronous. Do a data-only pass and a meta-only pass.
670 info.cmpfunc = vfsync_data_only_cmp;
671 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_data_only_cmp,
673 info.cmpfunc = vfsync_meta_only_cmp;
674 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_meta_only_cmp,
680 * Synchronous. Do a data-only pass, then a meta-data+data
681 * pass, then additional integrated passes to try to get
682 * all the dependancies flushed.
684 info.cmpfunc = vfsync_data_only_cmp;
685 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_data_only_cmp,
687 error = vfsync_wait_output(vp, waitoutput);
689 info.skippedbufs = 0;
690 info.cmpfunc = vfsync_dummy_cmp;
691 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
693 error = vfsync_wait_output(vp, waitoutput);
694 if (info.skippedbufs) {
695 kprintf("Warning: vfsync skipped %d dirty "
696 "bufs in pass2!\n", info.skippedbufs);
699 while (error == 0 && passes > 0 &&
700 !RB_EMPTY(&vp->v_rbdirty_tree)
703 info.synchronous = 1;
706 info.cmpfunc = vfsync_dummy_cmp;
707 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
713 error = vfsync_wait_output(vp, waitoutput);
717 lwkt_reltoken(&vp->v_token);
722 vfsync_wait_output(struct vnode *vp,
723 int (*waitoutput)(struct vnode *, struct thread *))
727 error = bio_track_wait(&vp->v_track_write, 0, 0);
729 error = waitoutput(vp, curthread);
734 vfsync_dummy_cmp(struct buf *bp __unused, void *data __unused)
740 vfsync_data_only_cmp(struct buf *bp, void *data)
742 if (bp->b_loffset < 0)
748 vfsync_meta_only_cmp(struct buf *bp, void *data)
750 if (bp->b_loffset < 0)
756 vfsync_lazy_range_cmp(struct buf *bp, void *data)
758 struct vfsync_info *info = data;
760 if (bp->b_loffset < info->vp->v_lazyw)
766 vfsync_bp(struct buf *bp, void *data)
768 struct vfsync_info *info = data;
769 struct vnode *vp = info->vp;
773 * Ignore buffers that we cannot immediately lock.
775 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
781 * We must revalidate the buffer after locking.
783 if ((bp->b_flags & B_DELWRI) == 0 ||
784 bp->b_vp != info->vp ||
785 info->cmpfunc(bp, data)) {
791 * If syncdeps is not set we do not try to write buffers which have
794 if (!info->synchronous && info->syncdeps == 0 && info->checkdef(bp)) {
800 * B_NEEDCOMMIT (primarily used by NFS) is a state where the buffer
801 * has been written but an additional handshake with the device
802 * is required before we can dispose of the buffer. We have no idea
803 * how to do this so we have to skip these buffers.
805 if (bp->b_flags & B_NEEDCOMMIT) {
811 * Ask bioops if it is ok to sync. If not the VFS may have
812 * set B_LOCKED so we have to cycle the buffer.
814 if (LIST_FIRST(&bp->b_dep) != NULL && buf_checkwrite(bp)) {
820 if (info->synchronous) {
822 * Synchronous flushing. An error may be returned.
828 * Asynchronous flushing. A negative return value simply
829 * stops the scan and is not considered an error. We use
830 * this to support limited MNT_LAZY flushes.
832 vp->v_lazyw = bp->b_loffset;
834 info->lazycount += cluster_awrite(bp);
835 waitrunningbufspace();
837 if (info->lazylimit && info->lazycount >= info->lazylimit)
846 * Associate a buffer with a vnode.
851 bgetvp(struct vnode *vp, struct buf *bp, int testsize)
853 KASSERT(bp->b_vp == NULL, ("bgetvp: not free"));
854 KKASSERT((bp->b_flags & (B_HASHED|B_DELWRI|B_VNCLEAN|B_VNDIRTY)) == 0);
857 * Insert onto list for new vnode.
859 lwkt_gettoken(&vp->v_token);
861 if (buf_rb_hash_RB_INSERT(&vp->v_rbhash_tree, bp)) {
862 lwkt_reltoken(&vp->v_token);
867 * Diagnostics (mainly for HAMMER debugging). Check for
868 * overlapping buffers.
870 if (check_buf_overlap) {
872 bx = buf_rb_hash_RB_PREV(bp);
874 if (bx->b_loffset + bx->b_bufsize > bp->b_loffset) {
875 kprintf("bgetvp: overlapl %016jx/%d %016jx "
877 (intmax_t)bx->b_loffset,
879 (intmax_t)bp->b_loffset,
881 if (check_buf_overlap > 1)
882 panic("bgetvp - overlapping buffer");
885 bx = buf_rb_hash_RB_NEXT(bp);
887 if (bp->b_loffset + testsize > bx->b_loffset) {
888 kprintf("bgetvp: overlapr %016jx/%d %016jx "
890 (intmax_t)bp->b_loffset,
892 (intmax_t)bx->b_loffset,
894 if (check_buf_overlap > 1)
895 panic("bgetvp - overlapping buffer");
900 bp->b_flags |= B_HASHED;
901 bp->b_flags |= B_VNCLEAN;
902 if (buf_rb_tree_RB_INSERT(&vp->v_rbclean_tree, bp))
903 panic("reassignbuf: dup lblk/clean vp %p bp %p", vp, bp);
905 lwkt_reltoken(&vp->v_token);
910 * Disassociate a buffer from a vnode.
915 brelvp(struct buf *bp)
919 KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
922 * Delete from old vnode list, if on one.
925 lwkt_gettoken(&vp->v_token);
926 if (bp->b_flags & (B_VNDIRTY | B_VNCLEAN)) {
927 if (bp->b_flags & B_VNDIRTY)
928 buf_rb_tree_RB_REMOVE(&vp->v_rbdirty_tree, bp);
930 buf_rb_tree_RB_REMOVE(&vp->v_rbclean_tree, bp);
931 bp->b_flags &= ~(B_VNDIRTY | B_VNCLEAN);
933 if (bp->b_flags & B_HASHED) {
934 buf_rb_hash_RB_REMOVE(&vp->v_rbhash_tree, bp);
935 bp->b_flags &= ~B_HASHED;
939 * Only remove from synclist when no dirty buffers are left AND
940 * the VFS has not flagged the vnode's inode as being dirty.
942 if ((vp->v_flag & (VONWORKLST | VISDIRTY | VOBJDIRTY)) == VONWORKLST &&
943 RB_EMPTY(&vp->v_rbdirty_tree)) {
944 vn_syncer_remove(vp);
948 lwkt_reltoken(&vp->v_token);
954 * Reassign the buffer to the proper clean/dirty list based on B_DELWRI.
955 * This routine is called when the state of the B_DELWRI bit is changed.
957 * Must be called with vp->v_token held.
961 reassignbuf(struct buf *bp)
963 struct vnode *vp = bp->b_vp;
966 ASSERT_LWKT_TOKEN_HELD(&vp->v_token);
970 * B_PAGING flagged buffers cannot be reassigned because their vp
971 * is not fully linked in.
973 if (bp->b_flags & B_PAGING)
974 panic("cannot reassign paging buffer");
976 if (bp->b_flags & B_DELWRI) {
978 * Move to the dirty list, add the vnode to the worklist
980 if (bp->b_flags & B_VNCLEAN) {
981 buf_rb_tree_RB_REMOVE(&vp->v_rbclean_tree, bp);
982 bp->b_flags &= ~B_VNCLEAN;
984 if ((bp->b_flags & B_VNDIRTY) == 0) {
985 if (buf_rb_tree_RB_INSERT(&vp->v_rbdirty_tree, bp)) {
986 panic("reassignbuf: dup lblk vp %p bp %p",
989 bp->b_flags |= B_VNDIRTY;
991 if ((vp->v_flag & VONWORKLST) == 0) {
992 switch (vp->v_type) {
999 vp->v_rdev->si_mountpoint != NULL) {
1007 vn_syncer_add(vp, delay);
1011 * Move to the clean list, remove the vnode from the worklist
1012 * if no dirty blocks remain.
1014 if (bp->b_flags & B_VNDIRTY) {
1015 buf_rb_tree_RB_REMOVE(&vp->v_rbdirty_tree, bp);
1016 bp->b_flags &= ~B_VNDIRTY;
1018 if ((bp->b_flags & B_VNCLEAN) == 0) {
1019 if (buf_rb_tree_RB_INSERT(&vp->v_rbclean_tree, bp)) {
1020 panic("reassignbuf: dup lblk vp %p bp %p",
1023 bp->b_flags |= B_VNCLEAN;
1027 * Only remove from synclist when no dirty buffers are left
1028 * AND the VFS has not flagged the vnode's inode as being
1031 if ((vp->v_flag & (VONWORKLST | VISDIRTY | VOBJDIRTY)) ==
1033 RB_EMPTY(&vp->v_rbdirty_tree)) {
1034 vn_syncer_remove(vp);
1040 * Create a vnode for a block device. Used for mounting the root file
1043 * A vref()'d vnode is returned.
1045 extern struct vop_ops *devfs_vnode_dev_vops_p;
1047 bdevvp(cdev_t dev, struct vnode **vpp)
1057 error = getspecialvnode(VT_NON, NULL, &devfs_vnode_dev_vops_p,
1068 v_associate_rdev(vp, dev);
1069 vp->v_umajor = dev->si_umajor;
1070 vp->v_uminor = dev->si_uminor;
1077 v_associate_rdev(struct vnode *vp, cdev_t dev)
1081 if (dev_is_good(dev) == 0)
1083 KKASSERT(vp->v_rdev == NULL);
1084 vp->v_rdev = reference_dev(dev);
1085 lwkt_gettoken(&spechash_token);
1086 SLIST_INSERT_HEAD(&dev->si_hlist, vp, v_cdevnext);
1087 lwkt_reltoken(&spechash_token);
1092 v_release_rdev(struct vnode *vp)
1096 if ((dev = vp->v_rdev) != NULL) {
1097 lwkt_gettoken(&spechash_token);
1098 SLIST_REMOVE(&dev->si_hlist, vp, vnode, v_cdevnext);
1101 lwkt_reltoken(&spechash_token);
1106 * Add a vnode to the alias list hung off the cdev_t. We only associate
1107 * the device number with the vnode. The actual device is not associated
1108 * until the vnode is opened (usually in spec_open()), and will be
1109 * disassociated on last close.
1112 addaliasu(struct vnode *nvp, int x, int y)
1114 if (nvp->v_type != VBLK && nvp->v_type != VCHR)
1115 panic("addaliasu on non-special vnode");
1121 * Simple call that a filesystem can make to try to get rid of a
1122 * vnode. It will fail if anyone is referencing the vnode (including
1125 * The filesystem can check whether its in-memory inode structure still
1126 * references the vp on return.
1128 * May only be called if the vnode is in a known state (i.e. being prevented
1129 * from being deallocated by some other condition such as a vfs inode hold).
1132 vclean_unlocked(struct vnode *vp)
1135 if (VREFCNT(vp) <= 1)
1141 * Disassociate a vnode from its underlying filesystem.
1143 * The vnode must be VX locked and referenced. In all normal situations
1144 * there are no active references. If vclean_vxlocked() is called while
1145 * there are active references, the vnode is being ripped out and we have
1146 * to call VOP_CLOSE() as appropriate before we can reclaim it.
1149 vclean_vxlocked(struct vnode *vp, int flags)
1154 struct namecache *ncp;
1157 * If the vnode has already been reclaimed we have nothing to do.
1159 if (vp->v_flag & VRECLAIMED)
1163 * Set flag to interlock operation, flag finalization to ensure
1164 * that the vnode winds up on the inactive list, and set v_act to 0.
1166 vsetflags(vp, VRECLAIMED);
1167 atomic_set_int(&vp->v_refcnt, VREF_FINALIZE);
1170 if (verbose_reclaims) {
1171 if ((ncp = TAILQ_FIRST(&vp->v_namecache)) != NULL)
1172 kprintf("Debug: reclaim %p %s\n", vp, ncp->nc_name);
1176 * Scrap the vfs cache
1178 while (cache_inval_vp(vp, 0) != 0) {
1179 kprintf("Warning: vnode %p clean/cache_resolution "
1180 "race detected\n", vp);
1181 tsleep(vp, 0, "vclninv", 2);
1185 * Check to see if the vnode is in use. If so we have to reference it
1186 * before we clean it out so that its count cannot fall to zero and
1187 * generate a race against ourselves to recycle it.
1189 active = (VREFCNT(vp) > 0);
1192 * Clean out any buffers associated with the vnode and destroy its
1193 * object, if it has one.
1195 vinvalbuf(vp, V_SAVE, 0, 0);
1196 KKASSERT(lockcountnb(&vp->v_lock) == 1);
1199 * If purging an active vnode (typically during a forced unmount
1200 * or reboot), it must be closed and deactivated before being
1201 * reclaimed. This isn't really all that safe, but what can
1204 * Note that neither of these routines unlocks the vnode.
1206 if (active && (flags & DOCLOSE)) {
1207 while ((n = vp->v_opencount) != 0) {
1208 if (vp->v_writecount)
1209 VOP_CLOSE(vp, FWRITE|FNONBLOCK, NULL);
1211 VOP_CLOSE(vp, FNONBLOCK, NULL);
1212 if (vp->v_opencount == n) {
1213 kprintf("Warning: unable to force-close"
1221 * If the vnode has not been deactivated, deactivated it. Deactivation
1222 * can create new buffers and VM pages so we have to call vinvalbuf()
1223 * again to make sure they all get flushed.
1225 * This can occur if a file with a link count of 0 needs to be
1228 * If the vnode is already dead don't try to deactivate it.
1230 if ((vp->v_flag & VINACTIVE) == 0) {
1231 vsetflags(vp, VINACTIVE);
1234 vinvalbuf(vp, V_SAVE, 0, 0);
1236 KKASSERT(lockcountnb(&vp->v_lock) == 1);
1239 * If the vnode has an object, destroy it.
1241 while ((object = vp->v_object) != NULL) {
1242 vm_object_hold(object);
1243 if (object == vp->v_object)
1245 vm_object_drop(object);
1248 if (object != NULL) {
1249 if (object->ref_count == 0) {
1250 if ((object->flags & OBJ_DEAD) == 0)
1251 vm_object_terminate(object);
1252 vm_object_drop(object);
1253 vclrflags(vp, VOBJBUF);
1255 vm_pager_deallocate(object);
1256 vclrflags(vp, VOBJBUF);
1257 vm_object_drop(object);
1260 KKASSERT((vp->v_flag & VOBJBUF) == 0);
1263 * Reclaim the vnode if not already dead.
1265 if (vp->v_mount && VOP_RECLAIM(vp))
1266 panic("vclean: cannot reclaim");
1269 * Done with purge, notify sleepers of the grim news.
1271 vp->v_ops = &dead_vnode_vops_p;
1276 * If we are destroying an active vnode, reactivate it now that
1277 * we have reassociated it with deadfs. This prevents the system
1278 * from crashing on the vnode due to it being unexpectedly marked
1279 * as inactive or reclaimed.
1281 if (active && (flags & DOCLOSE)) {
1282 vclrflags(vp, VINACTIVE | VRECLAIMED);
1287 * Eliminate all activity associated with the requested vnode
1288 * and with all vnodes aliased to the requested vnode.
1290 * The vnode must be referenced but should not be locked.
1293 vrevoke(struct vnode *vp, struct ucred *cred)
1301 * If the vnode has a device association, scrap all vnodes associated
1302 * with the device. Don't let the device disappear on us while we
1303 * are scrapping the vnodes.
1305 * The passed vp will probably show up in the list, do not VX lock
1308 * Releasing the vnode's rdev here can mess up specfs's call to
1309 * device close, so don't do it. The vnode has been disassociated
1310 * and the device will be closed after the last ref on the related
1311 * fp goes away (if not still open by e.g. the kernel).
1313 if (vp->v_type != VCHR) {
1314 error = fdrevoke(vp, DTYPE_VNODE, cred);
1317 if ((dev = vp->v_rdev) == NULL) {
1321 lwkt_gettoken(&spechash_token);
1324 vqn = SLIST_FIRST(&dev->si_hlist);
1327 while ((vq = vqn) != NULL) {
1328 if (VREFCNT(vq) > 0) {
1330 fdrevoke(vq, DTYPE_VNODE, cred);
1331 /*v_release_rdev(vq);*/
1333 if (vq->v_rdev != dev) {
1338 vqn = SLIST_NEXT(vq, v_cdevnext);
1343 lwkt_reltoken(&spechash_token);
1350 * This is called when the object underlying a vnode is being destroyed,
1351 * such as in a remove(). Try to recycle the vnode immediately if the
1352 * only active reference is our reference.
1354 * Directory vnodes in the namecache with children cannot be immediately
1355 * recycled because numerous VOP_N*() ops require them to be stable.
1357 * To avoid recursive recycling from VOP_INACTIVE implemenetations this
1358 * function is a NOP if VRECLAIMED is already set.
1361 vrecycle(struct vnode *vp)
1363 if (VREFCNT(vp) <= 1 && (vp->v_flag & VRECLAIMED) == 0) {
1364 if (cache_inval_vp_nonblock(vp))
1373 * Return the maximum I/O size allowed for strategy calls on VP.
1375 * If vp is VCHR or VBLK we dive the device, otherwise we use
1376 * the vp's mount info.
1378 * The returned value is clamped at MAXPHYS as most callers cannot use
1379 * buffers larger than that size.
1382 vmaxiosize(struct vnode *vp)
1386 if (vp->v_type == VBLK || vp->v_type == VCHR)
1387 maxiosize = vp->v_rdev->si_iosize_max;
1389 maxiosize = vp->v_mount->mnt_iosize_max;
1391 if (maxiosize > MAXPHYS)
1392 maxiosize = MAXPHYS;
1397 * Eliminate all activity associated with a vnode in preparation for
1400 * The vnode must be VX locked and refd and will remain VX locked and refd
1401 * on return. This routine may be called with the vnode in any state, as
1402 * long as it is VX locked. The vnode will be cleaned out and marked
1403 * VRECLAIMED but will not actually be reused until all existing refs and
1406 * NOTE: This routine may be called on a vnode which has not yet been
1407 * already been deactivated (VOP_INACTIVE), or on a vnode which has
1408 * already been reclaimed.
1410 * This routine is not responsible for placing us back on the freelist.
1411 * Instead, it happens automatically when the caller releases the VX lock
1412 * (assuming there aren't any other references).
1415 vgone_vxlocked(struct vnode *vp)
1418 * assert that the VX lock is held. This is an absolute requirement
1419 * now for vgone_vxlocked() to be called.
1421 KKASSERT(lockcountnb(&vp->v_lock) == 1);
1424 * Clean out the filesystem specific data and set the VRECLAIMED
1425 * bit. Also deactivate the vnode if necessary.
1427 * The vnode should have automatically been removed from the syncer
1428 * list as syncer/dirty flags cleared during the cleaning.
1430 vclean_vxlocked(vp, DOCLOSE);
1431 KKASSERT((vp->v_flag & VONWORKLST) == 0);
1434 * Delete from old mount point vnode list, if on one.
1436 if (vp->v_mount != NULL) {
1437 KKASSERT(vp->v_data == NULL);
1438 insmntque(vp, NULL);
1442 * If special device, remove it from special device alias list
1443 * if it is on one. This should normally only occur if a vnode is
1444 * being revoked as the device should otherwise have been released
1447 if ((vp->v_type == VBLK || vp->v_type == VCHR) && vp->v_rdev != NULL) {
1458 * Lookup a vnode by device number.
1460 * Returns non-zero and *vpp set to a vref'd vnode on success.
1461 * Returns zero on failure.
1464 vfinddev(cdev_t dev, enum vtype type, struct vnode **vpp)
1468 lwkt_gettoken(&spechash_token);
1469 SLIST_FOREACH(vp, &dev->si_hlist, v_cdevnext) {
1470 if (type == vp->v_type) {
1473 lwkt_reltoken(&spechash_token);
1477 lwkt_reltoken(&spechash_token);
1482 * Calculate the total number of references to a special device. This
1483 * routine may only be called for VBLK and VCHR vnodes since v_rdev is
1484 * an overloaded field. Since udev2dev can now return NULL, we have
1485 * to check for a NULL v_rdev.
1488 count_dev(cdev_t dev)
1493 if (SLIST_FIRST(&dev->si_hlist)) {
1494 lwkt_gettoken(&spechash_token);
1495 SLIST_FOREACH(vp, &dev->si_hlist, v_cdevnext) {
1496 count += vp->v_opencount;
1498 lwkt_reltoken(&spechash_token);
1504 vcount(struct vnode *vp)
1506 if (vp->v_rdev == NULL)
1508 return(count_dev(vp->v_rdev));
1512 * Initialize VMIO for a vnode. This routine MUST be called before a
1513 * VFS can issue buffer cache ops on a vnode. It is typically called
1514 * when a vnode is initialized from its inode.
1517 vinitvmio(struct vnode *vp, off_t filesize, int blksize, int boff)
1522 object = vp->v_object;
1524 vm_object_hold(object);
1525 KKASSERT(vp->v_object == object);
1528 if (object == NULL) {
1529 object = vnode_pager_alloc(vp, filesize, 0, 0, blksize, boff);
1532 * Dereference the reference we just created. This assumes
1533 * that the object is associated with the vp. Allow it to
1534 * have zero refs. It cannot be destroyed as long as it
1535 * is associated with the vnode.
1537 vm_object_hold(object);
1538 atomic_add_int(&object->ref_count, -1);
1541 KKASSERT((object->flags & OBJ_DEAD) == 0);
1543 KASSERT(vp->v_object != NULL, ("vinitvmio: NULL object"));
1544 vsetflags(vp, VOBJBUF);
1545 vm_object_drop(object);
1552 * Print out a description of a vnode.
1554 static char *typename[] =
1555 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD"};
1558 vprint(char *label, struct vnode *vp)
1563 kprintf("%s: %p: ", label, (void *)vp);
1565 kprintf("%p: ", (void *)vp);
1566 kprintf("type %s, refcnt %08x, writecount %d, holdcnt %d,",
1567 typename[vp->v_type],
1568 vp->v_refcnt, vp->v_writecount, vp->v_auxrefs);
1570 if (vp->v_flag & VROOT)
1571 strcat(buf, "|VROOT");
1572 if (vp->v_flag & VPFSROOT)
1573 strcat(buf, "|VPFSROOT");
1574 if (vp->v_flag & VTEXT)
1575 strcat(buf, "|VTEXT");
1576 if (vp->v_flag & VSYSTEM)
1577 strcat(buf, "|VSYSTEM");
1578 if (vp->v_flag & VOBJBUF)
1579 strcat(buf, "|VOBJBUF");
1581 kprintf(" flags (%s)", &buf[1]);
1582 if (vp->v_data == NULL) {
1591 * Do the usual access checking.
1592 * file_mode, uid and gid are from the vnode in question,
1593 * while acc_mode and cred are from the VOP_ACCESS parameter list
1596 vaccess(enum vtype type, mode_t file_mode, uid_t uid, gid_t gid,
1597 mode_t acc_mode, struct ucred *cred)
1603 * Super-user always gets read/write access, but execute access depends
1604 * on at least one execute bit being set.
1606 if (priv_check_cred(cred, PRIV_ROOT, 0) == 0) {
1607 if ((acc_mode & VEXEC) && type != VDIR &&
1608 (file_mode & (S_IXUSR|S_IXGRP|S_IXOTH)) == 0)
1615 /* Otherwise, check the owner. */
1616 if (cred->cr_uid == uid) {
1617 if (acc_mode & VEXEC)
1619 if (acc_mode & VREAD)
1621 if (acc_mode & VWRITE)
1623 return ((file_mode & mask) == mask ? 0 : EACCES);
1626 /* Otherwise, check the groups. */
1627 ismember = groupmember(gid, cred);
1628 if (cred->cr_svgid == gid || ismember) {
1629 if (acc_mode & VEXEC)
1631 if (acc_mode & VREAD)
1633 if (acc_mode & VWRITE)
1635 return ((file_mode & mask) == mask ? 0 : EACCES);
1638 /* Otherwise, check everyone else. */
1639 if (acc_mode & VEXEC)
1641 if (acc_mode & VREAD)
1643 if (acc_mode & VWRITE)
1645 return ((file_mode & mask) == mask ? 0 : EACCES);
1649 #include <ddb/ddb.h>
1651 static int db_show_locked_vnodes(struct mount *mp, void *data);
1654 * List all of the locked vnodes in the system.
1655 * Called when debugging the kernel.
1657 DB_SHOW_COMMAND(lockedvnodes, lockedvnodes)
1659 kprintf("Locked vnodes\n");
1660 mountlist_scan(db_show_locked_vnodes, NULL,
1661 MNTSCAN_FORWARD|MNTSCAN_NOBUSY);
1665 db_show_locked_vnodes(struct mount *mp, void *data __unused)
1669 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
1670 if (vn_islocked(vp))
1678 * Top level filesystem related information gathering.
1680 static int sysctl_ovfs_conf (SYSCTL_HANDLER_ARGS);
1683 vfs_sysctl(SYSCTL_HANDLER_ARGS)
1685 int *name = (int *)arg1 - 1; /* XXX */
1686 u_int namelen = arg2 + 1; /* XXX */
1687 struct vfsconf *vfsp;
1690 #if 1 || defined(COMPAT_PRELITE2)
1691 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
1693 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
1697 /* all sysctl names at this level are at least name and field */
1699 return (ENOTDIR); /* overloaded */
1700 if (name[0] != VFS_GENERIC) {
1701 vfsp = vfsconf_find_by_typenum(name[0]);
1703 return (EOPNOTSUPP);
1704 return ((*vfsp->vfc_vfsops->vfs_sysctl)(&name[1], namelen - 1,
1705 oldp, oldlenp, newp, newlen, p));
1709 case VFS_MAXTYPENUM:
1712 maxtypenum = vfsconf_get_maxtypenum();
1713 return (SYSCTL_OUT(req, &maxtypenum, sizeof(maxtypenum)));
1716 return (ENOTDIR); /* overloaded */
1717 vfsp = vfsconf_find_by_typenum(name[2]);
1719 return (EOPNOTSUPP);
1720 return (SYSCTL_OUT(req, vfsp, sizeof *vfsp));
1722 return (EOPNOTSUPP);
1725 SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD, vfs_sysctl,
1726 "Generic filesystem");
1728 #if 1 || defined(COMPAT_PRELITE2)
1731 sysctl_ovfs_conf_iter(struct vfsconf *vfsp, void *data)
1734 struct ovfsconf ovfs;
1735 struct sysctl_req *req = (struct sysctl_req*) data;
1737 bzero(&ovfs, sizeof(ovfs));
1738 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
1739 strcpy(ovfs.vfc_name, vfsp->vfc_name);
1740 ovfs.vfc_index = vfsp->vfc_typenum;
1741 ovfs.vfc_refcount = vfsp->vfc_refcount;
1742 ovfs.vfc_flags = vfsp->vfc_flags;
1743 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
1745 return error; /* abort iteration with error code */
1747 return 0; /* continue iterating with next element */
1751 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
1753 return vfsconf_each(sysctl_ovfs_conf_iter, (void*)req);
1756 #endif /* 1 || COMPAT_PRELITE2 */
1759 * Check to see if a filesystem is mounted on a block device.
1762 vfs_mountedon(struct vnode *vp)
1766 if ((dev = vp->v_rdev) == NULL) {
1767 /* if (vp->v_type != VBLK)
1768 dev = get_dev(vp->v_uminor, vp->v_umajor); */
1770 if (dev != NULL && dev->si_mountpoint)
1776 * Unmount all filesystems. The list is traversed in reverse order
1777 * of mounting to avoid dependencies.
1780 static int vfs_umountall_callback(struct mount *mp, void *data);
1783 vfs_unmountall(void)
1788 count = mountlist_scan(vfs_umountall_callback,
1789 NULL, MNTSCAN_REVERSE|MNTSCAN_NOBUSY);
1795 vfs_umountall_callback(struct mount *mp, void *data)
1799 error = dounmount(mp, MNT_FORCE);
1801 mountlist_remove(mp);
1802 kprintf("unmount of filesystem mounted from %s failed (",
1803 mp->mnt_stat.f_mntfromname);
1807 kprintf("%d)\n", error);
1813 * Checks the mount flags for parameter mp and put the names comma-separated
1814 * into a string buffer buf with a size limit specified by len.
1816 * It returns the number of bytes written into buf, and (*errorp) will be
1817 * set to 0, EINVAL (if passed length is 0), or ENOSPC (supplied buffer was
1818 * not large enough). The buffer will be 0-terminated if len was not 0.
1821 vfs_flagstostr(int flags, const struct mountctl_opt *optp,
1822 char *buf, size_t len, int *errorp)
1824 static const struct mountctl_opt optnames[] = {
1825 { MNT_ASYNC, "asynchronous" },
1826 { MNT_EXPORTED, "NFS exported" },
1827 { MNT_LOCAL, "local" },
1828 { MNT_NOATIME, "noatime" },
1829 { MNT_NODEV, "nodev" },
1830 { MNT_NOEXEC, "noexec" },
1831 { MNT_NOSUID, "nosuid" },
1832 { MNT_NOSYMFOLLOW, "nosymfollow" },
1833 { MNT_QUOTA, "with-quotas" },
1834 { MNT_RDONLY, "read-only" },
1835 { MNT_SYNCHRONOUS, "synchronous" },
1836 { MNT_UNION, "union" },
1837 { MNT_NOCLUSTERR, "noclusterr" },
1838 { MNT_NOCLUSTERW, "noclusterw" },
1839 { MNT_SUIDDIR, "suiddir" },
1840 { MNT_SOFTDEP, "soft-updates" },
1841 { MNT_IGNORE, "ignore" },
1851 bleft = len - 1; /* leave room for trailing \0 */
1854 * Checks the size of the string. If it contains
1855 * any data, then we will append the new flags to
1858 actsize = strlen(buf);
1862 /* Default flags if no flags passed */
1866 if (bleft < 0) { /* degenerate case, 0-length buffer */
1871 for (; flags && optp->o_opt; ++optp) {
1872 if ((flags & optp->o_opt) == 0)
1874 optlen = strlen(optp->o_name);
1875 if (bwritten || actsize > 0) {
1880 buf[bwritten++] = ',';
1881 buf[bwritten++] = ' ';
1884 if (bleft < optlen) {
1888 bcopy(optp->o_name, buf + bwritten, optlen);
1891 flags &= ~optp->o_opt;
1895 * Space already reserved for trailing \0
1902 * Build hash lists of net addresses and hang them off the mount point.
1903 * Called by ufs_mount() to set up the lists of export addresses.
1906 vfs_hang_addrlist(struct mount *mp, struct netexport *nep,
1907 const struct export_args *argp)
1910 struct radix_node_head *rnh;
1912 struct radix_node *rn;
1913 struct sockaddr *saddr, *smask = NULL;
1917 if (argp->ex_addrlen == 0) {
1918 if (mp->mnt_flag & MNT_DEFEXPORTED)
1920 np = &nep->ne_defexported;
1921 np->netc_exflags = argp->ex_flags;
1922 np->netc_anon = argp->ex_anon;
1923 np->netc_anon.cr_ref = 1;
1924 mp->mnt_flag |= MNT_DEFEXPORTED;
1928 if (argp->ex_addrlen < 0 || argp->ex_addrlen > MLEN)
1930 if (argp->ex_masklen < 0 || argp->ex_masklen > MLEN)
1933 i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
1934 np = (struct netcred *) kmalloc(i, M_NETADDR, M_WAITOK | M_ZERO);
1935 saddr = (struct sockaddr *) (np + 1);
1936 if ((error = copyin(argp->ex_addr, (caddr_t) saddr, argp->ex_addrlen)))
1938 if (saddr->sa_len > argp->ex_addrlen)
1939 saddr->sa_len = argp->ex_addrlen;
1940 if (argp->ex_masklen) {
1941 smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen);
1942 error = copyin(argp->ex_mask, (caddr_t)smask, argp->ex_masklen);
1945 if (smask->sa_len > argp->ex_masklen)
1946 smask->sa_len = argp->ex_masklen;
1948 i = saddr->sa_family;
1949 if ((rnh = nep->ne_rtable[i]) == NULL) {
1951 * Seems silly to initialize every AF when most are not used,
1952 * do so on demand here
1954 SLIST_FOREACH(dom, &domains, dom_next)
1955 if (dom->dom_family == i && dom->dom_rtattach) {
1956 dom->dom_rtattach((void **) &nep->ne_rtable[i],
1960 if ((rnh = nep->ne_rtable[i]) == NULL) {
1965 rn = (*rnh->rnh_addaddr) ((char *) saddr, (char *) smask, rnh,
1967 if (rn == NULL || np != (struct netcred *) rn) { /* already exists */
1971 np->netc_exflags = argp->ex_flags;
1972 np->netc_anon = argp->ex_anon;
1973 np->netc_anon.cr_ref = 1;
1976 kfree(np, M_NETADDR);
1982 vfs_free_netcred(struct radix_node *rn, void *w)
1984 struct radix_node_head *rnh = (struct radix_node_head *) w;
1986 (*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, rnh);
1987 kfree((caddr_t) rn, M_NETADDR);
1992 * Free the net address hash lists that are hanging off the mount points.
1995 vfs_free_addrlist(struct netexport *nep)
1998 struct radix_node_head *rnh;
2000 for (i = 0; i <= AF_MAX; i++)
2001 if ((rnh = nep->ne_rtable[i])) {
2002 (*rnh->rnh_walktree) (rnh, vfs_free_netcred,
2004 kfree((caddr_t) rnh, M_RTABLE);
2005 nep->ne_rtable[i] = 0;
2010 vfs_export(struct mount *mp, struct netexport *nep,
2011 const struct export_args *argp)
2015 if (argp->ex_flags & MNT_DELEXPORT) {
2016 if (mp->mnt_flag & MNT_EXPUBLIC) {
2017 vfs_setpublicfs(NULL, NULL, NULL);
2018 mp->mnt_flag &= ~MNT_EXPUBLIC;
2020 vfs_free_addrlist(nep);
2021 mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
2023 if (argp->ex_flags & MNT_EXPORTED) {
2024 if (argp->ex_flags & MNT_EXPUBLIC) {
2025 if ((error = vfs_setpublicfs(mp, nep, argp)) != 0)
2027 mp->mnt_flag |= MNT_EXPUBLIC;
2029 if ((error = vfs_hang_addrlist(mp, nep, argp)))
2031 mp->mnt_flag |= MNT_EXPORTED;
2038 * Set the publicly exported filesystem (WebNFS). Currently, only
2039 * one public filesystem is possible in the spec (RFC 2054 and 2055)
2042 vfs_setpublicfs(struct mount *mp, struct netexport *nep,
2043 const struct export_args *argp)
2050 * mp == NULL -> invalidate the current info, the FS is
2051 * no longer exported. May be called from either vfs_export
2052 * or unmount, so check if it hasn't already been done.
2055 if (nfs_pub.np_valid) {
2056 nfs_pub.np_valid = 0;
2057 if (nfs_pub.np_index != NULL) {
2058 kfree(nfs_pub.np_index, M_TEMP);
2059 nfs_pub.np_index = NULL;
2066 * Only one allowed at a time.
2068 if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
2072 * Get real filehandle for root of exported FS.
2074 bzero((caddr_t)&nfs_pub.np_handle, sizeof(nfs_pub.np_handle));
2075 nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid;
2077 if ((error = VFS_ROOT(mp, &rvp)))
2080 if ((error = VFS_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid)))
2086 * If an indexfile was specified, pull it in.
2088 if (argp->ex_indexfile != NULL) {
2091 error = vn_get_namelen(rvp, &namelen);
2094 nfs_pub.np_index = kmalloc(namelen, M_TEMP, M_WAITOK);
2095 error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
2099 * Check for illegal filenames.
2101 for (cp = nfs_pub.np_index; *cp; cp++) {
2109 kfree(nfs_pub.np_index, M_TEMP);
2114 nfs_pub.np_mount = mp;
2115 nfs_pub.np_valid = 1;
2120 vfs_export_lookup(struct mount *mp, struct netexport *nep,
2121 struct sockaddr *nam)
2124 struct radix_node_head *rnh;
2125 struct sockaddr *saddr;
2128 if (mp->mnt_flag & MNT_EXPORTED) {
2130 * Lookup in the export list first.
2134 rnh = nep->ne_rtable[saddr->sa_family];
2136 np = (struct netcred *)
2137 (*rnh->rnh_matchaddr)((char *)saddr,
2139 if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
2144 * If no address match, use the default if it exists.
2146 if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
2147 np = &nep->ne_defexported;
2153 * perform msync on all vnodes under a mount point. The mount point must
2154 * be locked. This code is also responsible for lazy-freeing unreferenced
2155 * vnodes whos VM objects no longer contain pages.
2157 * NOTE: MNT_WAIT still skips vnodes in the VXLOCK state.
2159 * NOTE: XXX VOP_PUTPAGES and friends requires that the vnode be locked,
2160 * but vnode_pager_putpages() doesn't lock the vnode. We have to do it
2161 * way up in this high level function.
2163 static int vfs_msync_scan1(struct mount *mp, struct vnode *vp, void *data);
2164 static int vfs_msync_scan2(struct mount *mp, struct vnode *vp, void *data);
2167 vfs_msync(struct mount *mp, int flags)
2172 * tmpfs sets this flag to prevent msync(), sync, and the
2173 * filesystem periodic syncer from trying to flush VM pages
2174 * to swap. Only pure memory pressure flushes tmpfs VM pages
2177 if (mp->mnt_kern_flag & MNTK_NOMSYNC)
2181 * Ok, scan the vnodes for work. If the filesystem is using the
2182 * syncer thread feature we can use vsyncscan() instead of
2183 * vmntvnodescan(), which is much faster.
2185 vmsc_flags = VMSC_GETVP;
2186 if (flags != MNT_WAIT)
2187 vmsc_flags |= VMSC_NOWAIT;
2189 if (mp->mnt_kern_flag & MNTK_THR_SYNC) {
2190 vsyncscan(mp, vmsc_flags, vfs_msync_scan2,
2191 (void *)(intptr_t)flags);
2193 vmntvnodescan(mp, vmsc_flags,
2194 vfs_msync_scan1, vfs_msync_scan2,
2195 (void *)(intptr_t)flags);
2200 * scan1 is a fast pre-check. There could be hundreds of thousands of
2201 * vnodes, we cannot afford to do anything heavy weight until we have a
2202 * fairly good indication that there is work to do.
2206 vfs_msync_scan1(struct mount *mp, struct vnode *vp, void *data)
2208 int flags = (int)(intptr_t)data;
2210 if ((vp->v_flag & VRECLAIMED) == 0) {
2211 if (vp->v_auxrefs == 0 && VREFCNT(vp) <= 0 &&
2213 return(0); /* call scan2 */
2215 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
2216 (vp->v_flag & VOBJDIRTY) &&
2217 (flags == MNT_WAIT || vn_islocked(vp) == 0)) {
2218 return(0); /* call scan2 */
2223 * do not call scan2, continue the loop
2229 * This callback is handed a locked vnode.
2233 vfs_msync_scan2(struct mount *mp, struct vnode *vp, void *data)
2236 int flags = (int)(intptr_t)data;
2238 if (vp->v_flag & VRECLAIMED)
2241 if ((mp->mnt_flag & MNT_RDONLY) == 0 && (vp->v_flag & VOBJDIRTY)) {
2242 if ((obj = vp->v_object) != NULL) {
2243 vm_object_page_clean(obj, 0, 0,
2244 flags == MNT_WAIT ? OBJPC_SYNC : OBJPC_NOSYNC);
2251 * Wake up anyone interested in vp because it is being revoked.
2254 vn_gone(struct vnode *vp)
2256 lwkt_gettoken(&vp->v_token);
2257 KNOTE(&vp->v_pollinfo.vpi_kqinfo.ki_note, NOTE_REVOKE);
2258 lwkt_reltoken(&vp->v_token);
2262 * extract the cdev_t from a VBLK or VCHR. The vnode must have been opened
2263 * (or v_rdev might be NULL).
2266 vn_todev(struct vnode *vp)
2268 if (vp->v_type != VBLK && vp->v_type != VCHR)
2270 KKASSERT(vp->v_rdev != NULL);
2271 return (vp->v_rdev);
2275 * Check if vnode represents a disk device. The vnode does not need to be
2281 vn_isdisk(struct vnode *vp, int *errp)
2285 if (vp->v_type != VCHR) {
2298 if (dev_is_good(dev) == 0) {
2303 if ((dev_dflags(dev) & D_DISK) == 0) {
2314 vn_get_namelen(struct vnode *vp, int *namelen)
2317 register_t retval[2];
2319 error = VOP_PATHCONF(vp, _PC_NAME_MAX, retval);
2322 *namelen = (int)retval[0];
2327 vop_write_dirent(int *error, struct uio *uio, ino_t d_ino, uint8_t d_type,
2328 uint16_t d_namlen, const char *d_name)
2333 len = _DIRENT_RECLEN(d_namlen);
2334 if (len > uio->uio_resid)
2337 dp = kmalloc(len, M_TEMP, M_WAITOK | M_ZERO);
2340 dp->d_namlen = d_namlen;
2341 dp->d_type = d_type;
2342 bcopy(d_name, dp->d_name, d_namlen);
2344 *error = uiomove((caddr_t)dp, len, uio);
2352 vn_mark_atime(struct vnode *vp, struct thread *td)
2354 struct proc *p = td->td_proc;
2355 struct ucred *cred = p ? p->p_ucred : proc0.p_ucred;
2357 if ((vp->v_mount->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0) {
2358 VOP_MARKATIME(vp, cred);