2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * @(#)vfs_subr.c 8.31 (Berkeley) 5/26/95
39 * $FreeBSD: src/sys/kern/vfs_subr.c,v 1.249.2.30 2003/04/04 20:35:57 tegge Exp $
40 * $DragonFly: src/sys/kern/vfs_subr.c,v 1.118 2008/09/17 21:44:18 dillon Exp $
44 * External virtual filesystem routines
48 #include <sys/param.h>
49 #include <sys/systm.h>
52 #include <sys/dirent.h>
53 #include <sys/domain.h>
54 #include <sys/eventhandler.h>
55 #include <sys/fcntl.h>
57 #include <sys/kernel.h>
58 #include <sys/kthread.h>
59 #include <sys/malloc.h>
61 #include <sys/mount.h>
64 #include <sys/reboot.h>
65 #include <sys/socket.h>
67 #include <sys/sysctl.h>
68 #include <sys/syslog.h>
69 #include <sys/unistd.h>
70 #include <sys/vmmeter.h>
71 #include <sys/vnode.h>
73 #include <machine/limits.h>
76 #include <vm/vm_object.h>
77 #include <vm/vm_extern.h>
78 #include <vm/vm_kern.h>
80 #include <vm/vm_map.h>
81 #include <vm/vm_page.h>
82 #include <vm/vm_pager.h>
83 #include <vm/vnode_pager.h>
84 #include <vm/vm_zone.h>
87 #include <sys/thread2.h>
88 #include <sys/sysref2.h>
89 #include <sys/mplock2.h>
91 static MALLOC_DEFINE(M_NETADDR, "Export Host", "Export host address structure");
94 SYSCTL_INT(_debug, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0,
95 "Number of vnodes allocated");
97 SYSCTL_INT(_debug, OID_AUTO, verbose_reclaims, CTLFLAG_RD, &verbose_reclaims, 0,
98 "Output filename of reclaimed vnode(s)");
100 enum vtype iftovt_tab[16] = {
101 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
102 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
104 int vttoif_tab[9] = {
105 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
106 S_IFSOCK, S_IFIFO, S_IFMT,
109 static int reassignbufcalls;
110 SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW, &reassignbufcalls,
111 0, "Number of times buffers have been reassigned to the proper list");
113 static int check_buf_overlap = 2; /* invasive check */
114 SYSCTL_INT(_vfs, OID_AUTO, check_buf_overlap, CTLFLAG_RW, &check_buf_overlap,
115 0, "Enable overlapping buffer checks");
117 int nfs_mount_type = -1;
118 static struct lwkt_token spechash_token;
119 struct nfs_public nfs_pub; /* publicly exported FS */
122 SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RW,
123 &desiredvnodes, 0, "Maximum number of vnodes");
125 static void vfs_free_addrlist (struct netexport *nep);
126 static int vfs_free_netcred (struct radix_node *rn, void *w);
127 static int vfs_hang_addrlist (struct mount *mp, struct netexport *nep,
128 const struct export_args *argp);
131 * Red black tree functions
133 static int rb_buf_compare(struct buf *b1, struct buf *b2);
134 RB_GENERATE2(buf_rb_tree, buf, b_rbnode, rb_buf_compare, off_t, b_loffset);
135 RB_GENERATE2(buf_rb_hash, buf, b_rbhash, rb_buf_compare, off_t, b_loffset);
138 rb_buf_compare(struct buf *b1, struct buf *b2)
140 if (b1->b_loffset < b2->b_loffset)
142 if (b1->b_loffset > b2->b_loffset)
148 * Returns non-zero if the vnode is a candidate for lazy msyncing.
150 * NOTE: v_object is not stable (this scan can race), however the
151 * mntvnodescan code holds vmobj_token so any VM object we
152 * do find will remain stable storage.
155 vshouldmsync(struct vnode *vp)
159 if (vp->v_auxrefs != 0 || vp->v_sysref.refcnt > 0)
160 return (0); /* other holders */
161 object = vp->v_object;
163 if (object && (object->ref_count || object->resident_page_count))
169 * Initialize the vnode management data structures.
171 * Called from vfsinit()
180 * Desiredvnodes is kern.maxvnodes. We want to scale it
181 * according to available system memory but we may also have
182 * to limit it based on available KVM, which is capped on 32 bit
185 * WARNING! For machines with 64-256M of ram we have to be sure
186 * that the default limit scales down well due to HAMMER
187 * taking up significantly more memory per-vnode vs UFS.
188 * We want around ~5800 on a 128M machine.
190 factor1 = 20 * (sizeof(struct vm_object) + sizeof(struct vnode));
191 factor2 = 22 * (sizeof(struct vm_object) + sizeof(struct vnode));
193 imin((int64_t)vmstats.v_page_count * PAGE_SIZE / factor1,
195 desiredvnodes = imax(desiredvnodes, maxproc * 8);
197 lwkt_token_init(&spechash_token, 1, "spechash");
201 * Knob to control the precision of file timestamps:
203 * 0 = seconds only; nanoseconds zeroed.
204 * 1 = seconds and nanoseconds, accurate within 1/HZ.
205 * 2 = seconds and nanoseconds, truncated to microseconds.
206 * >=3 = seconds and nanoseconds, maximum precision.
208 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC };
210 static int timestamp_precision = TSP_SEC;
211 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW,
212 ×tamp_precision, 0, "Precision of file timestamps");
215 * Get a current timestamp.
220 vfs_timestamp(struct timespec *tsp)
224 switch (timestamp_precision) {
226 tsp->tv_sec = time_second;
234 TIMEVAL_TO_TIMESPEC(&tv, tsp);
244 * Set vnode attributes to VNOVAL
247 vattr_null(struct vattr *vap)
250 vap->va_size = VNOVAL;
251 vap->va_bytes = VNOVAL;
252 vap->va_mode = VNOVAL;
253 vap->va_nlink = VNOVAL;
254 vap->va_uid = VNOVAL;
255 vap->va_gid = VNOVAL;
256 vap->va_fsid = VNOVAL;
257 vap->va_fileid = VNOVAL;
258 vap->va_blocksize = VNOVAL;
259 vap->va_rmajor = VNOVAL;
260 vap->va_rminor = VNOVAL;
261 vap->va_atime.tv_sec = VNOVAL;
262 vap->va_atime.tv_nsec = VNOVAL;
263 vap->va_mtime.tv_sec = VNOVAL;
264 vap->va_mtime.tv_nsec = VNOVAL;
265 vap->va_ctime.tv_sec = VNOVAL;
266 vap->va_ctime.tv_nsec = VNOVAL;
267 vap->va_flags = VNOVAL;
268 vap->va_gen = VNOVAL;
270 /* va_*_uuid fields are only valid if related flags are set */
274 * Flush out and invalidate all buffers associated with a vnode.
278 static int vinvalbuf_bp(struct buf *bp, void *data);
280 struct vinvalbuf_bp_info {
289 vinvalbuf(struct vnode *vp, int flags, int slpflag, int slptimeo)
291 struct vinvalbuf_bp_info info;
295 lwkt_gettoken(&vp->v_token);
298 * If we are being asked to save, call fsync to ensure that the inode
301 if (flags & V_SAVE) {
302 error = bio_track_wait(&vp->v_track_write, slpflag, slptimeo);
305 if (!RB_EMPTY(&vp->v_rbdirty_tree)) {
306 if ((error = VOP_FSYNC(vp, MNT_WAIT, 0)) != 0)
310 * Dirty bufs may be left or generated via races
311 * in circumstances where vinvalbuf() is called on
312 * a vnode not undergoing reclamation. Only
313 * panic if we are trying to reclaim the vnode.
315 if ((vp->v_flag & VRECLAIMED) &&
316 (bio_track_active(&vp->v_track_write) ||
317 !RB_EMPTY(&vp->v_rbdirty_tree))) {
318 panic("vinvalbuf: dirty bufs");
322 info.slptimeo = slptimeo;
323 info.lkflags = LK_EXCLUSIVE | LK_SLEEPFAIL;
324 if (slpflag & PCATCH)
325 info.lkflags |= LK_PCATCH;
330 * Flush the buffer cache until nothing is left.
332 while (!RB_EMPTY(&vp->v_rbclean_tree) ||
333 !RB_EMPTY(&vp->v_rbdirty_tree)) {
335 error = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree, NULL,
336 vinvalbuf_bp, &info);
339 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
340 vinvalbuf_bp, &info);
345 * Wait for I/O completion. We may block in the pip code so we have
349 bio_track_wait(&vp->v_track_write, 0, 0);
350 if ((object = vp->v_object) != NULL) {
351 while (object->paging_in_progress)
352 vm_object_pip_sleep(object, "vnvlbx");
354 } while (bio_track_active(&vp->v_track_write));
357 * Destroy the copy in the VM cache, too.
359 if ((object = vp->v_object) != NULL) {
360 vm_object_page_remove(object, 0, 0,
361 (flags & V_SAVE) ? TRUE : FALSE);
364 if (!RB_EMPTY(&vp->v_rbdirty_tree) || !RB_EMPTY(&vp->v_rbclean_tree))
365 panic("vinvalbuf: flush failed");
366 if (!RB_EMPTY(&vp->v_rbhash_tree))
367 panic("vinvalbuf: flush failed, buffers still present");
370 lwkt_reltoken(&vp->v_token);
375 vinvalbuf_bp(struct buf *bp, void *data)
377 struct vinvalbuf_bp_info *info = data;
380 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
381 atomic_add_int(&bp->b_refs, 1);
382 error = BUF_TIMELOCK(bp, info->lkflags,
383 "vinvalbuf", info->slptimeo);
384 atomic_subtract_int(&bp->b_refs, 1);
393 KKASSERT(bp->b_vp == info->vp);
396 * Must check clean/dirty status after successfully locking as
399 if ((info->clean && (bp->b_flags & B_DELWRI)) ||
400 (info->clean == 0 && (bp->b_flags & B_DELWRI) == 0)) {
406 * Note that vfs_bio_awrite expects buffers to reside
407 * on a queue, while bwrite() and brelse() do not.
409 * NOTE: NO B_LOCKED CHECK. Also no buf_checkwrite()
410 * check. This code will write out the buffer, period.
412 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
413 (info->flags & V_SAVE)) {
414 if (bp->b_flags & B_CLUSTEROK) {
420 } else if (info->flags & V_SAVE) {
422 * Cannot set B_NOCACHE on a clean buffer as this will
423 * destroy the VM backing store which might actually
424 * be dirty (and unsynchronized).
427 bp->b_flags |= (B_INVAL | B_RELBUF);
431 bp->b_flags |= (B_INVAL | B_NOCACHE | B_RELBUF);
438 * Truncate a file's buffer and pages to a specified length. This
439 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
442 * The vnode must be locked.
444 static int vtruncbuf_bp_trunc_cmp(struct buf *bp, void *data);
445 static int vtruncbuf_bp_trunc(struct buf *bp, void *data);
446 static int vtruncbuf_bp_metasync_cmp(struct buf *bp, void *data);
447 static int vtruncbuf_bp_metasync(struct buf *bp, void *data);
449 struct vtruncbuf_info {
456 vtruncbuf(struct vnode *vp, off_t length, int blksize)
458 struct vtruncbuf_info info;
459 const char *filename;
463 * Round up to the *next* block, then destroy the buffers in question.
464 * Since we are only removing some of the buffers we must rely on the
465 * scan count to determine whether a loop is necessary.
467 if ((count = (int)(length % blksize)) != 0)
468 info.truncloffset = length + (blksize - count);
470 info.truncloffset = length;
473 lwkt_gettoken(&vp->v_token);
476 count = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree,
477 vtruncbuf_bp_trunc_cmp,
478 vtruncbuf_bp_trunc, &info);
480 count += RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
481 vtruncbuf_bp_trunc_cmp,
482 vtruncbuf_bp_trunc, &info);
486 * For safety, fsync any remaining metadata if the file is not being
487 * truncated to 0. Since the metadata does not represent the entire
488 * dirty list we have to rely on the hit count to ensure that we get
493 count = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
494 vtruncbuf_bp_metasync_cmp,
495 vtruncbuf_bp_metasync, &info);
500 * Clean out any left over VM backing store.
502 * It is possible to have in-progress I/O from buffers that were
503 * not part of the truncation. This should not happen if we
504 * are truncating to 0-length.
506 vnode_pager_setsize(vp, length);
507 bio_track_wait(&vp->v_track_write, 0, 0);
512 spin_lock(&vp->v_spinlock);
513 filename = TAILQ_FIRST(&vp->v_namecache) ?
514 TAILQ_FIRST(&vp->v_namecache)->nc_name : "?";
515 spin_unlock(&vp->v_spinlock);
518 * Make sure no buffers were instantiated while we were trying
519 * to clean out the remaining VM pages. This could occur due
520 * to busy dirty VM pages being flushed out to disk.
524 count = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree,
525 vtruncbuf_bp_trunc_cmp,
526 vtruncbuf_bp_trunc, &info);
528 count += RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
529 vtruncbuf_bp_trunc_cmp,
530 vtruncbuf_bp_trunc, &info);
532 kprintf("Warning: vtruncbuf(): Had to re-clean %d "
533 "left over buffers in %s\n", count, filename);
537 lwkt_reltoken(&vp->v_token);
543 * The callback buffer is beyond the new file EOF and must be destroyed.
544 * Note that the compare function must conform to the RB_SCAN's requirements.
548 vtruncbuf_bp_trunc_cmp(struct buf *bp, void *data)
550 struct vtruncbuf_info *info = data;
552 if (bp->b_loffset >= info->truncloffset)
559 vtruncbuf_bp_trunc(struct buf *bp, void *data)
561 struct vtruncbuf_info *info = data;
564 * Do not try to use a buffer we cannot immediately lock, but sleep
565 * anyway to prevent a livelock. The code will loop until all buffers
568 * We must always revalidate the buffer after locking it to deal
571 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
572 atomic_add_int(&bp->b_refs, 1);
573 if (BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL) == 0)
575 atomic_subtract_int(&bp->b_refs, 1);
576 } else if ((info->clean && (bp->b_flags & B_DELWRI)) ||
577 (info->clean == 0 && (bp->b_flags & B_DELWRI) == 0) ||
578 bp->b_vp != info->vp ||
579 vtruncbuf_bp_trunc_cmp(bp, data)) {
583 bp->b_flags |= (B_INVAL | B_RELBUF | B_NOCACHE);
590 * Fsync all meta-data after truncating a file to be non-zero. Only metadata
591 * blocks (with a negative loffset) are scanned.
592 * Note that the compare function must conform to the RB_SCAN's requirements.
595 vtruncbuf_bp_metasync_cmp(struct buf *bp, void *data __unused)
597 if (bp->b_loffset < 0)
603 vtruncbuf_bp_metasync(struct buf *bp, void *data)
605 struct vtruncbuf_info *info = data;
607 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
608 atomic_add_int(&bp->b_refs, 1);
609 if (BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL) == 0)
611 atomic_subtract_int(&bp->b_refs, 1);
612 } else if ((bp->b_flags & B_DELWRI) == 0 ||
613 bp->b_vp != info->vp ||
614 vtruncbuf_bp_metasync_cmp(bp, data)) {
618 if (bp->b_vp == info->vp)
627 * vfsync - implements a multipass fsync on a file which understands
628 * dependancies and meta-data. The passed vnode must be locked. The
629 * waitfor argument may be MNT_WAIT or MNT_NOWAIT, or MNT_LAZY.
631 * When fsyncing data asynchronously just do one consolidated pass starting
632 * with the most negative block number. This may not get all the data due
635 * When fsyncing data synchronously do a data pass, then a metadata pass,
636 * then do additional data+metadata passes to try to get all the data out.
638 static int vfsync_wait_output(struct vnode *vp,
639 int (*waitoutput)(struct vnode *, struct thread *));
640 static int vfsync_dummy_cmp(struct buf *bp __unused, void *data __unused);
641 static int vfsync_data_only_cmp(struct buf *bp, void *data);
642 static int vfsync_meta_only_cmp(struct buf *bp, void *data);
643 static int vfsync_lazy_range_cmp(struct buf *bp, void *data);
644 static int vfsync_bp(struct buf *bp, void *data);
653 int (*checkdef)(struct buf *);
654 int (*cmpfunc)(struct buf *, void *);
658 vfsync(struct vnode *vp, int waitfor, int passes,
659 int (*checkdef)(struct buf *),
660 int (*waitoutput)(struct vnode *, struct thread *))
662 struct vfsync_info info;
665 bzero(&info, sizeof(info));
667 if ((info.checkdef = checkdef) == NULL)
670 lwkt_gettoken(&vp->v_token);
673 case MNT_LAZY | MNT_NOWAIT:
676 * Lazy (filesystem syncer typ) Asynchronous plus limit the
677 * number of data (not meta) pages we try to flush to 1MB.
678 * A non-zero return means that lazy limit was reached.
680 info.lazylimit = 1024 * 1024;
682 info.cmpfunc = vfsync_lazy_range_cmp;
683 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
684 vfsync_lazy_range_cmp, vfsync_bp, &info);
685 info.cmpfunc = vfsync_meta_only_cmp;
686 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
687 vfsync_meta_only_cmp, vfsync_bp, &info);
690 else if (!RB_EMPTY(&vp->v_rbdirty_tree))
691 vn_syncer_add(vp, 1);
696 * Asynchronous. Do a data-only pass and a meta-only pass.
699 info.cmpfunc = vfsync_data_only_cmp;
700 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_data_only_cmp,
702 info.cmpfunc = vfsync_meta_only_cmp;
703 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_meta_only_cmp,
709 * Synchronous. Do a data-only pass, then a meta-data+data
710 * pass, then additional integrated passes to try to get
711 * all the dependancies flushed.
713 info.cmpfunc = vfsync_data_only_cmp;
714 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_data_only_cmp,
716 error = vfsync_wait_output(vp, waitoutput);
718 info.skippedbufs = 0;
719 info.cmpfunc = vfsync_dummy_cmp;
720 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
722 error = vfsync_wait_output(vp, waitoutput);
723 if (info.skippedbufs) {
724 kprintf("Warning: vfsync skipped %d dirty "
725 "bufs in pass2!\n", info.skippedbufs);
728 while (error == 0 && passes > 0 &&
729 !RB_EMPTY(&vp->v_rbdirty_tree)
732 info.synchronous = 1;
735 info.cmpfunc = vfsync_dummy_cmp;
736 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
742 error = vfsync_wait_output(vp, waitoutput);
746 lwkt_reltoken(&vp->v_token);
751 vfsync_wait_output(struct vnode *vp,
752 int (*waitoutput)(struct vnode *, struct thread *))
756 error = bio_track_wait(&vp->v_track_write, 0, 0);
758 error = waitoutput(vp, curthread);
763 vfsync_dummy_cmp(struct buf *bp __unused, void *data __unused)
769 vfsync_data_only_cmp(struct buf *bp, void *data)
771 if (bp->b_loffset < 0)
777 vfsync_meta_only_cmp(struct buf *bp, void *data)
779 if (bp->b_loffset < 0)
785 vfsync_lazy_range_cmp(struct buf *bp, void *data)
787 struct vfsync_info *info = data;
789 if (bp->b_loffset < info->vp->v_lazyw)
795 vfsync_bp(struct buf *bp, void *data)
797 struct vfsync_info *info = data;
798 struct vnode *vp = info->vp;
802 * Ignore buffers that we cannot immediately lock.
804 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
810 * We must revalidate the buffer after locking.
812 if ((bp->b_flags & B_DELWRI) == 0 ||
813 bp->b_vp != info->vp ||
814 info->cmpfunc(bp, data)) {
820 * If syncdeps is not set we do not try to write buffers which have
823 if (!info->synchronous && info->syncdeps == 0 && info->checkdef(bp)) {
829 * B_NEEDCOMMIT (primarily used by NFS) is a state where the buffer
830 * has been written but an additional handshake with the device
831 * is required before we can dispose of the buffer. We have no idea
832 * how to do this so we have to skip these buffers.
834 if (bp->b_flags & B_NEEDCOMMIT) {
840 * Ask bioops if it is ok to sync. If not the VFS may have
841 * set B_LOCKED so we have to cycle the buffer.
843 if (LIST_FIRST(&bp->b_dep) != NULL && buf_checkwrite(bp)) {
849 if (info->synchronous) {
851 * Synchronous flushing. An error may be returned.
857 * Asynchronous flushing. A negative return value simply
858 * stops the scan and is not considered an error. We use
859 * this to support limited MNT_LAZY flushes.
861 vp->v_lazyw = bp->b_loffset;
862 if ((vp->v_flag & VOBJBUF) && (bp->b_flags & B_CLUSTEROK)) {
863 info->lazycount += vfs_bio_awrite(bp);
865 info->lazycount += bp->b_bufsize;
869 waitrunningbufspace();
870 if (info->lazylimit && info->lazycount >= info->lazylimit)
879 * Associate a buffer with a vnode.
884 bgetvp(struct vnode *vp, struct buf *bp, int testsize)
886 KASSERT(bp->b_vp == NULL, ("bgetvp: not free"));
887 KKASSERT((bp->b_flags & (B_HASHED|B_DELWRI|B_VNCLEAN|B_VNDIRTY)) == 0);
890 * Insert onto list for new vnode.
892 lwkt_gettoken(&vp->v_token);
894 if (buf_rb_hash_RB_INSERT(&vp->v_rbhash_tree, bp)) {
895 lwkt_reltoken(&vp->v_token);
900 * Diagnostics (mainly for HAMMER debugging). Check for
901 * overlapping buffers.
903 if (check_buf_overlap) {
905 bx = buf_rb_hash_RB_PREV(bp);
907 if (bx->b_loffset + bx->b_bufsize > bp->b_loffset) {
908 kprintf("bgetvp: overlapl %016jx/%d %016jx "
910 (intmax_t)bx->b_loffset,
912 (intmax_t)bp->b_loffset,
914 if (check_buf_overlap > 1)
915 panic("bgetvp - overlapping buffer");
918 bx = buf_rb_hash_RB_NEXT(bp);
920 if (bp->b_loffset + testsize > bx->b_loffset) {
921 kprintf("bgetvp: overlapr %016jx/%d %016jx "
923 (intmax_t)bp->b_loffset,
925 (intmax_t)bx->b_loffset,
927 if (check_buf_overlap > 1)
928 panic("bgetvp - overlapping buffer");
933 bp->b_flags |= B_HASHED;
934 bp->b_flags |= B_VNCLEAN;
935 if (buf_rb_tree_RB_INSERT(&vp->v_rbclean_tree, bp))
936 panic("reassignbuf: dup lblk/clean vp %p bp %p", vp, bp);
938 lwkt_reltoken(&vp->v_token);
943 * Disassociate a buffer from a vnode.
948 brelvp(struct buf *bp)
952 KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
955 * Delete from old vnode list, if on one.
958 lwkt_gettoken(&vp->v_token);
959 if (bp->b_flags & (B_VNDIRTY | B_VNCLEAN)) {
960 if (bp->b_flags & B_VNDIRTY)
961 buf_rb_tree_RB_REMOVE(&vp->v_rbdirty_tree, bp);
963 buf_rb_tree_RB_REMOVE(&vp->v_rbclean_tree, bp);
964 bp->b_flags &= ~(B_VNDIRTY | B_VNCLEAN);
966 if (bp->b_flags & B_HASHED) {
967 buf_rb_hash_RB_REMOVE(&vp->v_rbhash_tree, bp);
968 bp->b_flags &= ~B_HASHED;
970 if ((vp->v_flag & VONWORKLST) && RB_EMPTY(&vp->v_rbdirty_tree))
971 vn_syncer_remove(vp);
974 lwkt_reltoken(&vp->v_token);
980 * Reassign the buffer to the proper clean/dirty list based on B_DELWRI.
981 * This routine is called when the state of the B_DELWRI bit is changed.
983 * Must be called with vp->v_token held.
987 reassignbuf(struct buf *bp)
989 struct vnode *vp = bp->b_vp;
992 ASSERT_LWKT_TOKEN_HELD(&vp->v_token);
996 * B_PAGING flagged buffers cannot be reassigned because their vp
997 * is not fully linked in.
999 if (bp->b_flags & B_PAGING)
1000 panic("cannot reassign paging buffer");
1002 if (bp->b_flags & B_DELWRI) {
1004 * Move to the dirty list, add the vnode to the worklist
1006 if (bp->b_flags & B_VNCLEAN) {
1007 buf_rb_tree_RB_REMOVE(&vp->v_rbclean_tree, bp);
1008 bp->b_flags &= ~B_VNCLEAN;
1010 if ((bp->b_flags & B_VNDIRTY) == 0) {
1011 if (buf_rb_tree_RB_INSERT(&vp->v_rbdirty_tree, bp)) {
1012 panic("reassignbuf: dup lblk vp %p bp %p",
1015 bp->b_flags |= B_VNDIRTY;
1017 if ((vp->v_flag & VONWORKLST) == 0) {
1018 switch (vp->v_type) {
1025 vp->v_rdev->si_mountpoint != NULL) {
1033 vn_syncer_add(vp, delay);
1037 * Move to the clean list, remove the vnode from the worklist
1038 * if no dirty blocks remain.
1040 if (bp->b_flags & B_VNDIRTY) {
1041 buf_rb_tree_RB_REMOVE(&vp->v_rbdirty_tree, bp);
1042 bp->b_flags &= ~B_VNDIRTY;
1044 if ((bp->b_flags & B_VNCLEAN) == 0) {
1045 if (buf_rb_tree_RB_INSERT(&vp->v_rbclean_tree, bp)) {
1046 panic("reassignbuf: dup lblk vp %p bp %p",
1049 bp->b_flags |= B_VNCLEAN;
1051 if ((vp->v_flag & VONWORKLST) &&
1052 RB_EMPTY(&vp->v_rbdirty_tree)) {
1053 vn_syncer_remove(vp);
1059 * Create a vnode for a block device.
1060 * Used for mounting the root file system.
1062 extern struct vop_ops *devfs_vnode_dev_vops_p;
1064 bdevvp(cdev_t dev, struct vnode **vpp)
1074 error = getspecialvnode(VT_NON, NULL, &devfs_vnode_dev_vops_p,
1085 v_associate_rdev(vp, dev);
1086 vp->v_umajor = dev->si_umajor;
1087 vp->v_uminor = dev->si_uminor;
1094 v_associate_rdev(struct vnode *vp, cdev_t dev)
1098 if (dev_is_good(dev) == 0)
1100 KKASSERT(vp->v_rdev == NULL);
1101 vp->v_rdev = reference_dev(dev);
1102 lwkt_gettoken(&spechash_token);
1103 SLIST_INSERT_HEAD(&dev->si_hlist, vp, v_cdevnext);
1104 lwkt_reltoken(&spechash_token);
1109 v_release_rdev(struct vnode *vp)
1113 if ((dev = vp->v_rdev) != NULL) {
1114 lwkt_gettoken(&spechash_token);
1115 SLIST_REMOVE(&dev->si_hlist, vp, vnode, v_cdevnext);
1118 lwkt_reltoken(&spechash_token);
1123 * Add a vnode to the alias list hung off the cdev_t. We only associate
1124 * the device number with the vnode. The actual device is not associated
1125 * until the vnode is opened (usually in spec_open()), and will be
1126 * disassociated on last close.
1129 addaliasu(struct vnode *nvp, int x, int y)
1131 if (nvp->v_type != VBLK && nvp->v_type != VCHR)
1132 panic("addaliasu on non-special vnode");
1138 * Simple call that a filesystem can make to try to get rid of a
1139 * vnode. It will fail if anyone is referencing the vnode (including
1142 * The filesystem can check whether its in-memory inode structure still
1143 * references the vp on return.
1146 vclean_unlocked(struct vnode *vp)
1149 if (sysref_isactive(&vp->v_sysref) == 0)
1155 * Disassociate a vnode from its underlying filesystem.
1157 * The vnode must be VX locked and referenced. In all normal situations
1158 * there are no active references. If vclean_vxlocked() is called while
1159 * there are active references, the vnode is being ripped out and we have
1160 * to call VOP_CLOSE() as appropriate before we can reclaim it.
1163 vclean_vxlocked(struct vnode *vp, int flags)
1168 struct namecache *ncp;
1171 * If the vnode has already been reclaimed we have nothing to do.
1173 if (vp->v_flag & VRECLAIMED)
1175 vsetflags(vp, VRECLAIMED);
1177 if (verbose_reclaims) {
1178 if ((ncp = TAILQ_FIRST(&vp->v_namecache)) != NULL)
1179 kprintf("Debug: reclaim %p %s\n", vp, ncp->nc_name);
1183 * Scrap the vfs cache
1185 while (cache_inval_vp(vp, 0) != 0) {
1186 kprintf("Warning: vnode %p clean/cache_resolution "
1187 "race detected\n", vp);
1188 tsleep(vp, 0, "vclninv", 2);
1192 * Check to see if the vnode is in use. If so we have to reference it
1193 * before we clean it out so that its count cannot fall to zero and
1194 * generate a race against ourselves to recycle it.
1196 active = sysref_isactive(&vp->v_sysref);
1199 * Clean out any buffers associated with the vnode and destroy its
1200 * object, if it has one.
1202 vinvalbuf(vp, V_SAVE, 0, 0);
1205 * If purging an active vnode (typically during a forced unmount
1206 * or reboot), it must be closed and deactivated before being
1207 * reclaimed. This isn't really all that safe, but what can
1210 * Note that neither of these routines unlocks the vnode.
1212 if (active && (flags & DOCLOSE)) {
1213 while ((n = vp->v_opencount) != 0) {
1214 if (vp->v_writecount)
1215 VOP_CLOSE(vp, FWRITE|FNONBLOCK);
1217 VOP_CLOSE(vp, FNONBLOCK);
1218 if (vp->v_opencount == n) {
1219 kprintf("Warning: unable to force-close"
1227 * If the vnode has not been deactivated, deactivated it. Deactivation
1228 * can create new buffers and VM pages so we have to call vinvalbuf()
1229 * again to make sure they all get flushed.
1231 * This can occur if a file with a link count of 0 needs to be
1234 * If the vnode is already dead don't try to deactivate it.
1236 if ((vp->v_flag & VINACTIVE) == 0) {
1237 vsetflags(vp, VINACTIVE);
1240 vinvalbuf(vp, V_SAVE, 0, 0);
1244 * If the vnode has an object, destroy it.
1246 lwkt_gettoken(&vmobj_token);
1247 if ((object = vp->v_object) != NULL) {
1248 KKASSERT(object == vp->v_object);
1249 if (object->ref_count == 0) {
1250 if ((object->flags & OBJ_DEAD) == 0)
1251 vm_object_terminate(object);
1253 vm_pager_deallocate(object);
1255 vclrflags(vp, VOBJBUF);
1257 lwkt_reltoken(&vmobj_token);
1258 KKASSERT((vp->v_flag & VOBJBUF) == 0);
1261 * Reclaim the vnode if not already dead.
1263 if (vp->v_mount && VOP_RECLAIM(vp))
1264 panic("vclean: cannot reclaim");
1267 * Done with purge, notify sleepers of the grim news.
1269 vp->v_ops = &dead_vnode_vops_p;
1274 * If we are destroying an active vnode, reactivate it now that
1275 * we have reassociated it with deadfs. This prevents the system
1276 * from crashing on the vnode due to it being unexpectedly marked
1277 * as inactive or reclaimed.
1279 if (active && (flags & DOCLOSE)) {
1280 vclrflags(vp, VINACTIVE | VRECLAIMED);
1285 * Eliminate all activity associated with the requested vnode
1286 * and with all vnodes aliased to the requested vnode.
1288 * The vnode must be referenced but should not be locked.
1291 vrevoke(struct vnode *vp, struct ucred *cred)
1299 * If the vnode has a device association, scrap all vnodes associated
1300 * with the device. Don't let the device disappear on us while we
1301 * are scrapping the vnodes.
1303 * The passed vp will probably show up in the list, do not VX lock
1306 * Releasing the vnode's rdev here can mess up specfs's call to
1307 * device close, so don't do it. The vnode has been disassociated
1308 * and the device will be closed after the last ref on the related
1309 * fp goes away (if not still open by e.g. the kernel).
1311 if (vp->v_type != VCHR) {
1312 error = fdrevoke(vp, DTYPE_VNODE, cred);
1315 if ((dev = vp->v_rdev) == NULL) {
1319 lwkt_gettoken(&spechash_token);
1321 vqn = SLIST_FIRST(&dev->si_hlist);
1324 while ((vq = vqn) != NULL) {
1325 vqn = SLIST_NEXT(vqn, v_cdevnext);
1328 fdrevoke(vq, DTYPE_VNODE, cred);
1329 /*v_release_rdev(vq);*/
1332 lwkt_reltoken(&spechash_token);
1339 * This is called when the object underlying a vnode is being destroyed,
1340 * such as in a remove(). Try to recycle the vnode immediately if the
1341 * only active reference is our reference.
1343 * Directory vnodes in the namecache with children cannot be immediately
1344 * recycled because numerous VOP_N*() ops require them to be stable.
1346 * To avoid recursive recycling from VOP_INACTIVE implemenetations this
1347 * function is a NOP if VRECLAIMED is already set.
1350 vrecycle(struct vnode *vp)
1352 if (vp->v_sysref.refcnt <= 1 && (vp->v_flag & VRECLAIMED) == 0) {
1353 if (cache_inval_vp_nonblock(vp))
1362 * Return the maximum I/O size allowed for strategy calls on VP.
1364 * If vp is VCHR or VBLK we dive the device, otherwise we use
1365 * the vp's mount info.
1368 vmaxiosize(struct vnode *vp)
1370 if (vp->v_type == VBLK || vp->v_type == VCHR) {
1371 return(vp->v_rdev->si_iosize_max);
1373 return(vp->v_mount->mnt_iosize_max);
1378 * Eliminate all activity associated with a vnode in preparation for reuse.
1380 * The vnode must be VX locked and refd and will remain VX locked and refd
1381 * on return. This routine may be called with the vnode in any state, as
1382 * long as it is VX locked. The vnode will be cleaned out and marked
1383 * VRECLAIMED but will not actually be reused until all existing refs and
1386 * NOTE: This routine may be called on a vnode which has not yet been
1387 * already been deactivated (VOP_INACTIVE), or on a vnode which has
1388 * already been reclaimed.
1390 * This routine is not responsible for placing us back on the freelist.
1391 * Instead, it happens automatically when the caller releases the VX lock
1392 * (assuming there aren't any other references).
1395 vgone_vxlocked(struct vnode *vp)
1398 * assert that the VX lock is held. This is an absolute requirement
1399 * now for vgone_vxlocked() to be called.
1401 KKASSERT(vp->v_lock.lk_exclusivecount == 1);
1406 * Clean out the filesystem specific data and set the VRECLAIMED
1407 * bit. Also deactivate the vnode if necessary.
1409 vclean_vxlocked(vp, DOCLOSE);
1412 * Delete from old mount point vnode list, if on one.
1414 if (vp->v_mount != NULL) {
1415 KKASSERT(vp->v_data == NULL);
1416 insmntque(vp, NULL);
1420 * If special device, remove it from special device alias list
1421 * if it is on one. This should normally only occur if a vnode is
1422 * being revoked as the device should otherwise have been released
1425 if ((vp->v_type == VBLK || vp->v_type == VCHR) && vp->v_rdev != NULL) {
1437 * Lookup a vnode by device number.
1439 * Returns non-zero and *vpp set to a vref'd vnode on success.
1440 * Returns zero on failure.
1443 vfinddev(cdev_t dev, enum vtype type, struct vnode **vpp)
1447 lwkt_gettoken(&spechash_token);
1448 SLIST_FOREACH(vp, &dev->si_hlist, v_cdevnext) {
1449 if (type == vp->v_type) {
1452 lwkt_reltoken(&spechash_token);
1456 lwkt_reltoken(&spechash_token);
1461 * Calculate the total number of references to a special device. This
1462 * routine may only be called for VBLK and VCHR vnodes since v_rdev is
1463 * an overloaded field. Since udev2dev can now return NULL, we have
1464 * to check for a NULL v_rdev.
1467 count_dev(cdev_t dev)
1472 if (SLIST_FIRST(&dev->si_hlist)) {
1473 lwkt_gettoken(&spechash_token);
1474 SLIST_FOREACH(vp, &dev->si_hlist, v_cdevnext) {
1475 count += vp->v_opencount;
1477 lwkt_reltoken(&spechash_token);
1483 vcount(struct vnode *vp)
1485 if (vp->v_rdev == NULL)
1487 return(count_dev(vp->v_rdev));
1491 * Initialize VMIO for a vnode. This routine MUST be called before a
1492 * VFS can issue buffer cache ops on a vnode. It is typically called
1493 * when a vnode is initialized from its inode.
1496 vinitvmio(struct vnode *vp, off_t filesize, int blksize, int boff)
1501 lwkt_gettoken(&vmobj_token);
1503 if ((object = vp->v_object) == NULL) {
1504 object = vnode_pager_alloc(vp, filesize, 0, 0, blksize, boff);
1506 * Dereference the reference we just created. This assumes
1507 * that the object is associated with the vp.
1509 object->ref_count--;
1512 if (object->flags & OBJ_DEAD) {
1514 if (vp->v_object == object)
1515 vm_object_dead_sleep(object, "vodead");
1516 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1520 KASSERT(vp->v_object != NULL, ("vinitvmio: NULL object"));
1521 vsetflags(vp, VOBJBUF);
1522 lwkt_reltoken(&vmobj_token);
1529 * Print out a description of a vnode.
1531 static char *typename[] =
1532 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD"};
1535 vprint(char *label, struct vnode *vp)
1540 kprintf("%s: %p: ", label, (void *)vp);
1542 kprintf("%p: ", (void *)vp);
1543 kprintf("type %s, sysrefs %d, writecount %d, holdcnt %d,",
1544 typename[vp->v_type],
1545 vp->v_sysref.refcnt, vp->v_writecount, vp->v_auxrefs);
1547 if (vp->v_flag & VROOT)
1548 strcat(buf, "|VROOT");
1549 if (vp->v_flag & VPFSROOT)
1550 strcat(buf, "|VPFSROOT");
1551 if (vp->v_flag & VTEXT)
1552 strcat(buf, "|VTEXT");
1553 if (vp->v_flag & VSYSTEM)
1554 strcat(buf, "|VSYSTEM");
1555 if (vp->v_flag & VFREE)
1556 strcat(buf, "|VFREE");
1557 if (vp->v_flag & VOBJBUF)
1558 strcat(buf, "|VOBJBUF");
1560 kprintf(" flags (%s)", &buf[1]);
1561 if (vp->v_data == NULL) {
1570 * Do the usual access checking.
1571 * file_mode, uid and gid are from the vnode in question,
1572 * while acc_mode and cred are from the VOP_ACCESS parameter list
1575 vaccess(enum vtype type, mode_t file_mode, uid_t uid, gid_t gid,
1576 mode_t acc_mode, struct ucred *cred)
1582 * Super-user always gets read/write access, but execute access depends
1583 * on at least one execute bit being set.
1585 if (priv_check_cred(cred, PRIV_ROOT, 0) == 0) {
1586 if ((acc_mode & VEXEC) && type != VDIR &&
1587 (file_mode & (S_IXUSR|S_IXGRP|S_IXOTH)) == 0)
1594 /* Otherwise, check the owner. */
1595 if (cred->cr_uid == uid) {
1596 if (acc_mode & VEXEC)
1598 if (acc_mode & VREAD)
1600 if (acc_mode & VWRITE)
1602 return ((file_mode & mask) == mask ? 0 : EACCES);
1605 /* Otherwise, check the groups. */
1606 ismember = groupmember(gid, cred);
1607 if (cred->cr_svgid == gid || ismember) {
1608 if (acc_mode & VEXEC)
1610 if (acc_mode & VREAD)
1612 if (acc_mode & VWRITE)
1614 return ((file_mode & mask) == mask ? 0 : EACCES);
1617 /* Otherwise, check everyone else. */
1618 if (acc_mode & VEXEC)
1620 if (acc_mode & VREAD)
1622 if (acc_mode & VWRITE)
1624 return ((file_mode & mask) == mask ? 0 : EACCES);
1628 #include <ddb/ddb.h>
1630 static int db_show_locked_vnodes(struct mount *mp, void *data);
1633 * List all of the locked vnodes in the system.
1634 * Called when debugging the kernel.
1636 DB_SHOW_COMMAND(lockedvnodes, lockedvnodes)
1638 kprintf("Locked vnodes\n");
1639 mountlist_scan(db_show_locked_vnodes, NULL,
1640 MNTSCAN_FORWARD|MNTSCAN_NOBUSY);
1644 db_show_locked_vnodes(struct mount *mp, void *data __unused)
1648 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
1649 if (vn_islocked(vp))
1657 * Top level filesystem related information gathering.
1659 static int sysctl_ovfs_conf (SYSCTL_HANDLER_ARGS);
1662 vfs_sysctl(SYSCTL_HANDLER_ARGS)
1664 int *name = (int *)arg1 - 1; /* XXX */
1665 u_int namelen = arg2 + 1; /* XXX */
1666 struct vfsconf *vfsp;
1669 #if 1 || defined(COMPAT_PRELITE2)
1670 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
1672 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
1676 /* all sysctl names at this level are at least name and field */
1678 return (ENOTDIR); /* overloaded */
1679 if (name[0] != VFS_GENERIC) {
1680 vfsp = vfsconf_find_by_typenum(name[0]);
1682 return (EOPNOTSUPP);
1683 return ((*vfsp->vfc_vfsops->vfs_sysctl)(&name[1], namelen - 1,
1684 oldp, oldlenp, newp, newlen, p));
1688 case VFS_MAXTYPENUM:
1691 maxtypenum = vfsconf_get_maxtypenum();
1692 return (SYSCTL_OUT(req, &maxtypenum, sizeof(maxtypenum)));
1695 return (ENOTDIR); /* overloaded */
1696 vfsp = vfsconf_find_by_typenum(name[2]);
1698 return (EOPNOTSUPP);
1699 return (SYSCTL_OUT(req, vfsp, sizeof *vfsp));
1701 return (EOPNOTSUPP);
1704 SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD, vfs_sysctl,
1705 "Generic filesystem");
1707 #if 1 || defined(COMPAT_PRELITE2)
1710 sysctl_ovfs_conf_iter(struct vfsconf *vfsp, void *data)
1713 struct ovfsconf ovfs;
1714 struct sysctl_req *req = (struct sysctl_req*) data;
1716 bzero(&ovfs, sizeof(ovfs));
1717 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
1718 strcpy(ovfs.vfc_name, vfsp->vfc_name);
1719 ovfs.vfc_index = vfsp->vfc_typenum;
1720 ovfs.vfc_refcount = vfsp->vfc_refcount;
1721 ovfs.vfc_flags = vfsp->vfc_flags;
1722 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
1724 return error; /* abort iteration with error code */
1726 return 0; /* continue iterating with next element */
1730 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
1732 return vfsconf_each(sysctl_ovfs_conf_iter, (void*)req);
1735 #endif /* 1 || COMPAT_PRELITE2 */
1738 * Check to see if a filesystem is mounted on a block device.
1741 vfs_mountedon(struct vnode *vp)
1745 if ((dev = vp->v_rdev) == NULL) {
1746 /* if (vp->v_type != VBLK)
1747 dev = get_dev(vp->v_uminor, vp->v_umajor); */
1749 if (dev != NULL && dev->si_mountpoint)
1755 * Unmount all filesystems. The list is traversed in reverse order
1756 * of mounting to avoid dependencies.
1759 static int vfs_umountall_callback(struct mount *mp, void *data);
1762 vfs_unmountall(void)
1767 count = mountlist_scan(vfs_umountall_callback,
1768 NULL, MNTSCAN_REVERSE|MNTSCAN_NOBUSY);
1774 vfs_umountall_callback(struct mount *mp, void *data)
1778 error = dounmount(mp, MNT_FORCE);
1780 mountlist_remove(mp);
1781 kprintf("unmount of filesystem mounted from %s failed (",
1782 mp->mnt_stat.f_mntfromname);
1786 kprintf("%d)\n", error);
1792 * Checks the mount flags for parameter mp and put the names comma-separated
1793 * into a string buffer buf with a size limit specified by len.
1795 * It returns the number of bytes written into buf, and (*errorp) will be
1796 * set to 0, EINVAL (if passed length is 0), or ENOSPC (supplied buffer was
1797 * not large enough). The buffer will be 0-terminated if len was not 0.
1800 vfs_flagstostr(int flags, const struct mountctl_opt *optp,
1801 char *buf, size_t len, int *errorp)
1803 static const struct mountctl_opt optnames[] = {
1804 { MNT_ASYNC, "asynchronous" },
1805 { MNT_EXPORTED, "NFS exported" },
1806 { MNT_LOCAL, "local" },
1807 { MNT_NOATIME, "noatime" },
1808 { MNT_NODEV, "nodev" },
1809 { MNT_NOEXEC, "noexec" },
1810 { MNT_NOSUID, "nosuid" },
1811 { MNT_NOSYMFOLLOW, "nosymfollow" },
1812 { MNT_QUOTA, "with-quotas" },
1813 { MNT_RDONLY, "read-only" },
1814 { MNT_SYNCHRONOUS, "synchronous" },
1815 { MNT_UNION, "union" },
1816 { MNT_NOCLUSTERR, "noclusterr" },
1817 { MNT_NOCLUSTERW, "noclusterw" },
1818 { MNT_SUIDDIR, "suiddir" },
1819 { MNT_SOFTDEP, "soft-updates" },
1820 { MNT_IGNORE, "ignore" },
1830 bleft = len - 1; /* leave room for trailing \0 */
1833 * Checks the size of the string. If it contains
1834 * any data, then we will append the new flags to
1837 actsize = strlen(buf);
1841 /* Default flags if no flags passed */
1845 if (bleft < 0) { /* degenerate case, 0-length buffer */
1850 for (; flags && optp->o_opt; ++optp) {
1851 if ((flags & optp->o_opt) == 0)
1853 optlen = strlen(optp->o_name);
1854 if (bwritten || actsize > 0) {
1859 buf[bwritten++] = ',';
1860 buf[bwritten++] = ' ';
1863 if (bleft < optlen) {
1867 bcopy(optp->o_name, buf + bwritten, optlen);
1870 flags &= ~optp->o_opt;
1874 * Space already reserved for trailing \0
1881 * Build hash lists of net addresses and hang them off the mount point.
1882 * Called by ufs_mount() to set up the lists of export addresses.
1885 vfs_hang_addrlist(struct mount *mp, struct netexport *nep,
1886 const struct export_args *argp)
1889 struct radix_node_head *rnh;
1891 struct radix_node *rn;
1892 struct sockaddr *saddr, *smask = 0;
1896 if (argp->ex_addrlen == 0) {
1897 if (mp->mnt_flag & MNT_DEFEXPORTED)
1899 np = &nep->ne_defexported;
1900 np->netc_exflags = argp->ex_flags;
1901 np->netc_anon = argp->ex_anon;
1902 np->netc_anon.cr_ref = 1;
1903 mp->mnt_flag |= MNT_DEFEXPORTED;
1907 if (argp->ex_addrlen < 0 || argp->ex_addrlen > MLEN)
1909 if (argp->ex_masklen < 0 || argp->ex_masklen > MLEN)
1912 i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
1913 np = (struct netcred *) kmalloc(i, M_NETADDR, M_WAITOK | M_ZERO);
1914 saddr = (struct sockaddr *) (np + 1);
1915 if ((error = copyin(argp->ex_addr, (caddr_t) saddr, argp->ex_addrlen)))
1917 if (saddr->sa_len > argp->ex_addrlen)
1918 saddr->sa_len = argp->ex_addrlen;
1919 if (argp->ex_masklen) {
1920 smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen);
1921 error = copyin(argp->ex_mask, (caddr_t)smask, argp->ex_masklen);
1924 if (smask->sa_len > argp->ex_masklen)
1925 smask->sa_len = argp->ex_masklen;
1927 i = saddr->sa_family;
1928 if ((rnh = nep->ne_rtable[i]) == 0) {
1930 * Seems silly to initialize every AF when most are not used,
1931 * do so on demand here
1933 SLIST_FOREACH(dom, &domains, dom_next)
1934 if (dom->dom_family == i && dom->dom_rtattach) {
1935 dom->dom_rtattach((void **) &nep->ne_rtable[i],
1939 if ((rnh = nep->ne_rtable[i]) == 0) {
1944 rn = (*rnh->rnh_addaddr) ((char *) saddr, (char *) smask, rnh,
1946 if (rn == 0 || np != (struct netcred *) rn) { /* already exists */
1950 np->netc_exflags = argp->ex_flags;
1951 np->netc_anon = argp->ex_anon;
1952 np->netc_anon.cr_ref = 1;
1955 kfree(np, M_NETADDR);
1961 vfs_free_netcred(struct radix_node *rn, void *w)
1963 struct radix_node_head *rnh = (struct radix_node_head *) w;
1965 (*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, rnh);
1966 kfree((caddr_t) rn, M_NETADDR);
1971 * Free the net address hash lists that are hanging off the mount points.
1974 vfs_free_addrlist(struct netexport *nep)
1977 struct radix_node_head *rnh;
1979 for (i = 0; i <= AF_MAX; i++)
1980 if ((rnh = nep->ne_rtable[i])) {
1981 (*rnh->rnh_walktree) (rnh, vfs_free_netcred,
1983 kfree((caddr_t) rnh, M_RTABLE);
1984 nep->ne_rtable[i] = 0;
1989 vfs_export(struct mount *mp, struct netexport *nep,
1990 const struct export_args *argp)
1994 if (argp->ex_flags & MNT_DELEXPORT) {
1995 if (mp->mnt_flag & MNT_EXPUBLIC) {
1996 vfs_setpublicfs(NULL, NULL, NULL);
1997 mp->mnt_flag &= ~MNT_EXPUBLIC;
1999 vfs_free_addrlist(nep);
2000 mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
2002 if (argp->ex_flags & MNT_EXPORTED) {
2003 if (argp->ex_flags & MNT_EXPUBLIC) {
2004 if ((error = vfs_setpublicfs(mp, nep, argp)) != 0)
2006 mp->mnt_flag |= MNT_EXPUBLIC;
2008 if ((error = vfs_hang_addrlist(mp, nep, argp)))
2010 mp->mnt_flag |= MNT_EXPORTED;
2017 * Set the publicly exported filesystem (WebNFS). Currently, only
2018 * one public filesystem is possible in the spec (RFC 2054 and 2055)
2021 vfs_setpublicfs(struct mount *mp, struct netexport *nep,
2022 const struct export_args *argp)
2029 * mp == NULL -> invalidate the current info, the FS is
2030 * no longer exported. May be called from either vfs_export
2031 * or unmount, so check if it hasn't already been done.
2034 if (nfs_pub.np_valid) {
2035 nfs_pub.np_valid = 0;
2036 if (nfs_pub.np_index != NULL) {
2037 FREE(nfs_pub.np_index, M_TEMP);
2038 nfs_pub.np_index = NULL;
2045 * Only one allowed at a time.
2047 if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
2051 * Get real filehandle for root of exported FS.
2053 bzero((caddr_t)&nfs_pub.np_handle, sizeof(nfs_pub.np_handle));
2054 nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid;
2056 if ((error = VFS_ROOT(mp, &rvp)))
2059 if ((error = VFS_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid)))
2065 * If an indexfile was specified, pull it in.
2067 if (argp->ex_indexfile != NULL) {
2070 error = vn_get_namelen(rvp, &namelen);
2073 MALLOC(nfs_pub.np_index, char *, namelen, M_TEMP,
2075 error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
2079 * Check for illegal filenames.
2081 for (cp = nfs_pub.np_index; *cp; cp++) {
2089 FREE(nfs_pub.np_index, M_TEMP);
2094 nfs_pub.np_mount = mp;
2095 nfs_pub.np_valid = 1;
2100 vfs_export_lookup(struct mount *mp, struct netexport *nep,
2101 struct sockaddr *nam)
2104 struct radix_node_head *rnh;
2105 struct sockaddr *saddr;
2108 if (mp->mnt_flag & MNT_EXPORTED) {
2110 * Lookup in the export list first.
2114 rnh = nep->ne_rtable[saddr->sa_family];
2116 np = (struct netcred *)
2117 (*rnh->rnh_matchaddr)((char *)saddr,
2119 if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
2124 * If no address match, use the default if it exists.
2126 if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
2127 np = &nep->ne_defexported;
2133 * perform msync on all vnodes under a mount point. The mount point must
2134 * be locked. This code is also responsible for lazy-freeing unreferenced
2135 * vnodes whos VM objects no longer contain pages.
2137 * NOTE: MNT_WAIT still skips vnodes in the VXLOCK state.
2139 * NOTE: XXX VOP_PUTPAGES and friends requires that the vnode be locked,
2140 * but vnode_pager_putpages() doesn't lock the vnode. We have to do it
2141 * way up in this high level function.
2143 static int vfs_msync_scan1(struct mount *mp, struct vnode *vp, void *data);
2144 static int vfs_msync_scan2(struct mount *mp, struct vnode *vp, void *data);
2147 vfs_msync(struct mount *mp, int flags)
2152 * tmpfs sets this flag to prevent msync(), sync, and the
2153 * filesystem periodic syncer from trying to flush VM pages
2154 * to swap. Only pure memory pressure flushes tmpfs VM pages
2157 if (mp->mnt_kern_flag & MNTK_NOMSYNC)
2161 * Ok, scan the vnodes for work.
2163 vmsc_flags = VMSC_GETVP;
2164 if (flags != MNT_WAIT)
2165 vmsc_flags |= VMSC_NOWAIT;
2166 vmntvnodescan(mp, vmsc_flags, vfs_msync_scan1, vfs_msync_scan2,
2167 (void *)(intptr_t)flags);
2171 * scan1 is a fast pre-check. There could be hundreds of thousands of
2172 * vnodes, we cannot afford to do anything heavy weight until we have a
2173 * fairly good indication that there is work to do.
2177 vfs_msync_scan1(struct mount *mp, struct vnode *vp, void *data)
2179 int flags = (int)(intptr_t)data;
2181 if ((vp->v_flag & VRECLAIMED) == 0) {
2182 if (vshouldmsync(vp))
2183 return(0); /* call scan2 */
2184 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
2185 (vp->v_flag & VOBJDIRTY) &&
2186 (flags == MNT_WAIT || vn_islocked(vp) == 0)) {
2187 return(0); /* call scan2 */
2192 * do not call scan2, continue the loop
2198 * This callback is handed a locked vnode.
2202 vfs_msync_scan2(struct mount *mp, struct vnode *vp, void *data)
2205 int flags = (int)(intptr_t)data;
2207 if (vp->v_flag & VRECLAIMED)
2210 if ((mp->mnt_flag & MNT_RDONLY) == 0 && (vp->v_flag & VOBJDIRTY)) {
2211 if ((obj = vp->v_object) != NULL) {
2212 vm_object_page_clean(obj, 0, 0,
2213 flags == MNT_WAIT ? OBJPC_SYNC : OBJPC_NOSYNC);
2220 * Wake up anyone interested in vp because it is being revoked.
2223 vn_gone(struct vnode *vp)
2225 lwkt_gettoken(&vp->v_token);
2226 KNOTE(&vp->v_pollinfo.vpi_kqinfo.ki_note, NOTE_REVOKE);
2227 lwkt_reltoken(&vp->v_token);
2231 * extract the cdev_t from a VBLK or VCHR. The vnode must have been opened
2232 * (or v_rdev might be NULL).
2235 vn_todev(struct vnode *vp)
2237 if (vp->v_type != VBLK && vp->v_type != VCHR)
2239 KKASSERT(vp->v_rdev != NULL);
2240 return (vp->v_rdev);
2244 * Check if vnode represents a disk device. The vnode does not need to be
2250 vn_isdisk(struct vnode *vp, int *errp)
2254 if (vp->v_type != VCHR) {
2267 if (dev_is_good(dev) == 0) {
2272 if ((dev_dflags(dev) & D_DISK) == 0) {
2283 vn_get_namelen(struct vnode *vp, int *namelen)
2286 register_t retval[2];
2288 error = VOP_PATHCONF(vp, _PC_NAME_MAX, retval);
2291 *namelen = (int)retval[0];
2296 vop_write_dirent(int *error, struct uio *uio, ino_t d_ino, uint8_t d_type,
2297 uint16_t d_namlen, const char *d_name)
2302 len = _DIRENT_RECLEN(d_namlen);
2303 if (len > uio->uio_resid)
2306 dp = kmalloc(len, M_TEMP, M_WAITOK | M_ZERO);
2309 dp->d_namlen = d_namlen;
2310 dp->d_type = d_type;
2311 bcopy(d_name, dp->d_name, d_namlen);
2313 *error = uiomove((caddr_t)dp, len, uio);
2321 vn_mark_atime(struct vnode *vp, struct thread *td)
2323 struct proc *p = td->td_proc;
2324 struct ucred *cred = p ? p->p_ucred : proc0.p_ucred;
2326 if ((vp->v_mount->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0) {
2327 VOP_MARKATIME(vp, cred);