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, "");
96 enum vtype iftovt_tab[16] = {
97 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
98 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
100 int vttoif_tab[9] = {
101 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
102 S_IFSOCK, S_IFIFO, S_IFMT,
105 static int reassignbufcalls;
106 SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW,
107 &reassignbufcalls, 0, "");
108 static int reassignbufloops;
109 SYSCTL_INT(_vfs, OID_AUTO, reassignbufloops, CTLFLAG_RW,
110 &reassignbufloops, 0, "");
111 static int reassignbufsortgood;
112 SYSCTL_INT(_vfs, OID_AUTO, reassignbufsortgood, CTLFLAG_RW,
113 &reassignbufsortgood, 0, "");
114 static int reassignbufsortbad;
115 SYSCTL_INT(_vfs, OID_AUTO, reassignbufsortbad, CTLFLAG_RW,
116 &reassignbufsortbad, 0, "");
117 static int reassignbufmethod = 1;
118 SYSCTL_INT(_vfs, OID_AUTO, reassignbufmethod, CTLFLAG_RW,
119 &reassignbufmethod, 0, "");
120 static int check_buf_overlap = 2; /* invasive check */
121 SYSCTL_INT(_vfs, OID_AUTO, check_buf_overlap, CTLFLAG_RW,
122 &check_buf_overlap, 0, "");
124 int nfs_mount_type = -1;
125 static struct lwkt_token spechash_token;
126 struct nfs_public nfs_pub; /* publicly exported FS */
129 SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RW,
130 &desiredvnodes, 0, "Maximum number of vnodes");
132 static void vfs_free_addrlist (struct netexport *nep);
133 static int vfs_free_netcred (struct radix_node *rn, void *w);
134 static int vfs_hang_addrlist (struct mount *mp, struct netexport *nep,
135 const struct export_args *argp);
138 * Red black tree functions
140 static int rb_buf_compare(struct buf *b1, struct buf *b2);
141 RB_GENERATE2(buf_rb_tree, buf, b_rbnode, rb_buf_compare, off_t, b_loffset);
142 RB_GENERATE2(buf_rb_hash, buf, b_rbhash, rb_buf_compare, off_t, b_loffset);
145 rb_buf_compare(struct buf *b1, struct buf *b2)
147 if (b1->b_loffset < b2->b_loffset)
149 if (b1->b_loffset > b2->b_loffset)
155 * Returns non-zero if the vnode is a candidate for lazy msyncing.
158 vshouldmsync(struct vnode *vp)
160 if (vp->v_auxrefs != 0 || vp->v_sysref.refcnt > 0)
161 return (0); /* other holders */
163 (vp->v_object->ref_count || vp->v_object->resident_page_count)) {
170 * Initialize the vnode management data structures.
172 * Called from vfsinit()
181 * Desiredvnodes is kern.maxvnodes. We want to scale it
182 * according to available system memory but we may also have
183 * to limit it based on available KVM, which is capped on 32 bit
186 * WARNING! For machines with 64-256M of ram we have to be sure
187 * that the default limit scales down well due to HAMMER
188 * taking up significantly more memory per-vnode vs UFS.
189 * We want around ~5800 on a 128M machine.
191 factor1 = 20 * (sizeof(struct vm_object) + sizeof(struct vnode));
192 factor2 = 22 * (sizeof(struct vm_object) + sizeof(struct vnode));
194 imin((int64_t)vmstats.v_page_count * PAGE_SIZE / factor1,
196 desiredvnodes = imax(desiredvnodes, maxproc * 8);
198 lwkt_token_init(&spechash_token, 1, "spechash");
202 * Knob to control the precision of file timestamps:
204 * 0 = seconds only; nanoseconds zeroed.
205 * 1 = seconds and nanoseconds, accurate within 1/HZ.
206 * 2 = seconds and nanoseconds, truncated to microseconds.
207 * >=3 = seconds and nanoseconds, maximum precision.
209 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC };
211 static int timestamp_precision = TSP_SEC;
212 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW,
213 ×tamp_precision, 0, "");
216 * Get a current timestamp.
221 vfs_timestamp(struct timespec *tsp)
225 switch (timestamp_precision) {
227 tsp->tv_sec = time_second;
235 TIMEVAL_TO_TIMESPEC(&tv, tsp);
245 * Set vnode attributes to VNOVAL
248 vattr_null(struct vattr *vap)
251 vap->va_size = VNOVAL;
252 vap->va_bytes = VNOVAL;
253 vap->va_mode = VNOVAL;
254 vap->va_nlink = VNOVAL;
255 vap->va_uid = VNOVAL;
256 vap->va_gid = VNOVAL;
257 vap->va_fsid = VNOVAL;
258 vap->va_fileid = VNOVAL;
259 vap->va_blocksize = VNOVAL;
260 vap->va_rmajor = VNOVAL;
261 vap->va_rminor = VNOVAL;
262 vap->va_atime.tv_sec = VNOVAL;
263 vap->va_atime.tv_nsec = VNOVAL;
264 vap->va_mtime.tv_sec = VNOVAL;
265 vap->va_mtime.tv_nsec = VNOVAL;
266 vap->va_ctime.tv_sec = VNOVAL;
267 vap->va_ctime.tv_nsec = VNOVAL;
268 vap->va_flags = VNOVAL;
269 vap->va_gen = VNOVAL;
271 /* va_*_uuid fields are only valid if related flags are set */
275 * Flush out and invalidate all buffers associated with a vnode.
279 static int vinvalbuf_bp(struct buf *bp, void *data);
281 struct vinvalbuf_bp_info {
290 vinvalbuf(struct vnode *vp, int flags, int slpflag, int slptimeo)
292 struct vinvalbuf_bp_info info;
296 lwkt_gettoken(&vp->v_token);
299 * If we are being asked to save, call fsync to ensure that the inode
302 if (flags & V_SAVE) {
303 error = bio_track_wait(&vp->v_track_write, slpflag, slptimeo);
306 if (!RB_EMPTY(&vp->v_rbdirty_tree)) {
307 if ((error = VOP_FSYNC(vp, MNT_WAIT, 0)) != 0)
311 * Dirty bufs may be left or generated via races
312 * in circumstances where vinvalbuf() is called on
313 * a vnode not undergoing reclamation. Only
314 * panic if we are trying to reclaim the vnode.
316 if ((vp->v_flag & VRECLAIMED) &&
317 (bio_track_active(&vp->v_track_write) ||
318 !RB_EMPTY(&vp->v_rbdirty_tree))) {
319 panic("vinvalbuf: dirty bufs");
323 info.slptimeo = slptimeo;
324 info.lkflags = LK_EXCLUSIVE | LK_SLEEPFAIL;
325 if (slpflag & PCATCH)
326 info.lkflags |= LK_PCATCH;
331 * Flush the buffer cache until nothing is left.
333 while (!RB_EMPTY(&vp->v_rbclean_tree) ||
334 !RB_EMPTY(&vp->v_rbdirty_tree)) {
336 error = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree, NULL,
337 vinvalbuf_bp, &info);
340 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
341 vinvalbuf_bp, &info);
346 * Wait for I/O completion. We may block in the pip code so we have
350 bio_track_wait(&vp->v_track_write, 0, 0);
351 if ((object = vp->v_object) != NULL) {
352 while (object->paging_in_progress)
353 vm_object_pip_sleep(object, "vnvlbx");
355 } while (bio_track_active(&vp->v_track_write));
358 * Destroy the copy in the VM cache, too.
360 if ((object = vp->v_object) != NULL) {
361 vm_object_page_remove(object, 0, 0,
362 (flags & V_SAVE) ? TRUE : FALSE);
365 if (!RB_EMPTY(&vp->v_rbdirty_tree) || !RB_EMPTY(&vp->v_rbclean_tree))
366 panic("vinvalbuf: flush failed");
367 if (!RB_EMPTY(&vp->v_rbhash_tree))
368 panic("vinvalbuf: flush failed, buffers still present");
371 lwkt_reltoken(&vp->v_token);
376 vinvalbuf_bp(struct buf *bp, void *data)
378 struct vinvalbuf_bp_info *info = data;
381 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
382 atomic_add_int(&bp->b_refs, 1);
383 error = BUF_TIMELOCK(bp, info->lkflags,
384 "vinvalbuf", info->slptimeo);
385 atomic_subtract_int(&bp->b_refs, 1);
394 KKASSERT(bp->b_vp == info->vp);
397 * Must check clean/dirty status after successfully locking as
400 if ((info->clean && (bp->b_flags & B_DELWRI)) ||
401 (info->clean == 0 && (bp->b_flags & B_DELWRI) == 0)) {
407 * Note that vfs_bio_awrite expects buffers to reside
408 * on a queue, while bwrite() and brelse() do not.
410 * NOTE: NO B_LOCKED CHECK. Also no buf_checkwrite()
411 * check. This code will write out the buffer, period.
413 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
414 (info->flags & V_SAVE)) {
415 if (bp->b_flags & B_CLUSTEROK) {
421 } else if (info->flags & V_SAVE) {
423 * Cannot set B_NOCACHE on a clean buffer as this will
424 * destroy the VM backing store which might actually
425 * be dirty (and unsynchronized).
428 bp->b_flags |= (B_INVAL | B_RELBUF);
432 bp->b_flags |= (B_INVAL | B_NOCACHE | B_RELBUF);
439 * Truncate a file's buffer and pages to a specified length. This
440 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
443 * The vnode must be locked.
445 static int vtruncbuf_bp_trunc_cmp(struct buf *bp, void *data);
446 static int vtruncbuf_bp_trunc(struct buf *bp, void *data);
447 static int vtruncbuf_bp_metasync_cmp(struct buf *bp, void *data);
448 static int vtruncbuf_bp_metasync(struct buf *bp, void *data);
450 struct vtruncbuf_info {
457 vtruncbuf(struct vnode *vp, off_t length, int blksize)
459 struct vtruncbuf_info info;
460 const char *filename;
464 * Round up to the *next* block, then destroy the buffers in question.
465 * Since we are only removing some of the buffers we must rely on the
466 * scan count to determine whether a loop is necessary.
468 if ((count = (int)(length % blksize)) != 0)
469 info.truncloffset = length + (blksize - count);
471 info.truncloffset = length;
474 lwkt_gettoken(&vp->v_token);
477 count = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree,
478 vtruncbuf_bp_trunc_cmp,
479 vtruncbuf_bp_trunc, &info);
481 count += RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
482 vtruncbuf_bp_trunc_cmp,
483 vtruncbuf_bp_trunc, &info);
487 * For safety, fsync any remaining metadata if the file is not being
488 * truncated to 0. Since the metadata does not represent the entire
489 * dirty list we have to rely on the hit count to ensure that we get
494 count = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
495 vtruncbuf_bp_metasync_cmp,
496 vtruncbuf_bp_metasync, &info);
501 * Clean out any left over VM backing store.
503 * It is possible to have in-progress I/O from buffers that were
504 * not part of the truncation. This should not happen if we
505 * are truncating to 0-length.
507 vnode_pager_setsize(vp, length);
508 bio_track_wait(&vp->v_track_write, 0, 0);
513 spin_lock_wr(&vp->v_spinlock);
514 filename = TAILQ_FIRST(&vp->v_namecache) ?
515 TAILQ_FIRST(&vp->v_namecache)->nc_name : "?";
516 spin_unlock_wr(&vp->v_spinlock);
519 * Make sure no buffers were instantiated while we were trying
520 * to clean out the remaining VM pages. This could occur due
521 * to busy dirty VM pages being flushed out to disk.
525 count = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree,
526 vtruncbuf_bp_trunc_cmp,
527 vtruncbuf_bp_trunc, &info);
529 count += RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
530 vtruncbuf_bp_trunc_cmp,
531 vtruncbuf_bp_trunc, &info);
533 kprintf("Warning: vtruncbuf(): Had to re-clean %d "
534 "left over buffers in %s\n", count, filename);
538 lwkt_reltoken(&vp->v_token);
544 * The callback buffer is beyond the new file EOF and must be destroyed.
545 * Note that the compare function must conform to the RB_SCAN's requirements.
549 vtruncbuf_bp_trunc_cmp(struct buf *bp, void *data)
551 struct vtruncbuf_info *info = data;
553 if (bp->b_loffset >= info->truncloffset)
560 vtruncbuf_bp_trunc(struct buf *bp, void *data)
562 struct vtruncbuf_info *info = data;
565 * Do not try to use a buffer we cannot immediately lock, but sleep
566 * anyway to prevent a livelock. The code will loop until all buffers
569 * We must always revalidate the buffer after locking it to deal
572 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
573 atomic_add_int(&bp->b_refs, 1);
574 if (BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL) == 0)
576 atomic_subtract_int(&bp->b_refs, 1);
577 } else if ((info->clean && (bp->b_flags & B_DELWRI)) ||
578 (info->clean == 0 && (bp->b_flags & B_DELWRI) == 0) ||
579 bp->b_vp != info->vp ||
580 vtruncbuf_bp_trunc_cmp(bp, data)) {
584 bp->b_flags |= (B_INVAL | B_RELBUF | B_NOCACHE);
591 * Fsync all meta-data after truncating a file to be non-zero. Only metadata
592 * blocks (with a negative loffset) are scanned.
593 * Note that the compare function must conform to the RB_SCAN's requirements.
596 vtruncbuf_bp_metasync_cmp(struct buf *bp, void *data __unused)
598 if (bp->b_loffset < 0)
604 vtruncbuf_bp_metasync(struct buf *bp, void *data)
606 struct vtruncbuf_info *info = data;
608 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
609 atomic_add_int(&bp->b_refs, 1);
610 if (BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL) == 0)
612 atomic_subtract_int(&bp->b_refs, 1);
613 } else if ((bp->b_flags & B_DELWRI) == 0 ||
614 bp->b_vp != info->vp ||
615 vtruncbuf_bp_metasync_cmp(bp, data)) {
619 if (bp->b_vp == info->vp)
628 * vfsync - implements a multipass fsync on a file which understands
629 * dependancies and meta-data. The passed vnode must be locked. The
630 * waitfor argument may be MNT_WAIT or MNT_NOWAIT, or MNT_LAZY.
632 * When fsyncing data asynchronously just do one consolidated pass starting
633 * with the most negative block number. This may not get all the data due
636 * When fsyncing data synchronously do a data pass, then a metadata pass,
637 * then do additional data+metadata passes to try to get all the data out.
639 static int vfsync_wait_output(struct vnode *vp,
640 int (*waitoutput)(struct vnode *, struct thread *));
641 static int vfsync_dummy_cmp(struct buf *bp __unused, void *data __unused);
642 static int vfsync_data_only_cmp(struct buf *bp, void *data);
643 static int vfsync_meta_only_cmp(struct buf *bp, void *data);
644 static int vfsync_lazy_range_cmp(struct buf *bp, void *data);
645 static int vfsync_bp(struct buf *bp, void *data);
654 int (*checkdef)(struct buf *);
655 int (*cmpfunc)(struct buf *, void *);
659 vfsync(struct vnode *vp, int waitfor, int passes,
660 int (*checkdef)(struct buf *),
661 int (*waitoutput)(struct vnode *, struct thread *))
663 struct vfsync_info info;
666 bzero(&info, sizeof(info));
668 if ((info.checkdef = checkdef) == NULL)
671 lwkt_gettoken(&vp->v_token);
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 bufs in pass2!\n", info.skippedbufs);
726 while (error == 0 && passes > 0 &&
727 !RB_EMPTY(&vp->v_rbdirty_tree)
730 info.synchronous = 1;
733 info.cmpfunc = vfsync_dummy_cmp;
734 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
740 error = vfsync_wait_output(vp, waitoutput);
744 lwkt_reltoken(&vp->v_token);
749 vfsync_wait_output(struct vnode *vp,
750 int (*waitoutput)(struct vnode *, struct thread *))
754 error = bio_track_wait(&vp->v_track_write, 0, 0);
756 error = waitoutput(vp, curthread);
761 vfsync_dummy_cmp(struct buf *bp __unused, void *data __unused)
767 vfsync_data_only_cmp(struct buf *bp, void *data)
769 if (bp->b_loffset < 0)
775 vfsync_meta_only_cmp(struct buf *bp, void *data)
777 if (bp->b_loffset < 0)
783 vfsync_lazy_range_cmp(struct buf *bp, void *data)
785 struct vfsync_info *info = data;
787 if (bp->b_loffset < info->vp->v_lazyw)
793 vfsync_bp(struct buf *bp, void *data)
795 struct vfsync_info *info = data;
796 struct vnode *vp = info->vp;
800 * Ignore buffers that we cannot immediately lock.
802 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
803 kprintf("Warning: vfsync_bp skipping dirty buffer %p\n", bp);
809 * We must revalidate the buffer after locking.
811 if ((bp->b_flags & B_DELWRI) == 0 ||
812 bp->b_vp != info->vp ||
813 info->cmpfunc(bp, data)) {
819 * If syncdeps is not set we do not try to write buffers which have
822 if (!info->synchronous && info->syncdeps == 0 && info->checkdef(bp)) {
828 * B_NEEDCOMMIT (primarily used by NFS) is a state where the buffer
829 * has been written but an additional handshake with the device
830 * is required before we can dispose of the buffer. We have no idea
831 * how to do this so we have to skip these buffers.
833 if (bp->b_flags & B_NEEDCOMMIT) {
839 * Ask bioops if it is ok to sync. If not the VFS may have
840 * set B_LOCKED so we have to cycle the buffer.
842 if (LIST_FIRST(&bp->b_dep) != NULL && buf_checkwrite(bp)) {
848 if (info->synchronous) {
850 * Synchronous flushing. An error may be returned.
856 * Asynchronous flushing. A negative return value simply
857 * stops the scan and is not considered an error. We use
858 * this to support limited MNT_LAZY flushes.
860 vp->v_lazyw = bp->b_loffset;
861 if ((vp->v_flag & VOBJBUF) && (bp->b_flags & B_CLUSTEROK)) {
862 info->lazycount += vfs_bio_awrite(bp);
864 info->lazycount += bp->b_bufsize;
868 waitrunningbufspace();
869 if (info->lazylimit && info->lazycount >= info->lazylimit)
878 * Associate a buffer with a vnode.
883 bgetvp(struct vnode *vp, struct buf *bp, int testsize)
885 KASSERT(bp->b_vp == NULL, ("bgetvp: not free"));
886 KKASSERT((bp->b_flags & (B_HASHED|B_DELWRI|B_VNCLEAN|B_VNDIRTY)) == 0);
889 * Insert onto list for new vnode.
891 lwkt_gettoken(&vp->v_token);
893 if (buf_rb_hash_RB_INSERT(&vp->v_rbhash_tree, bp)) {
894 lwkt_reltoken(&vp->v_token);
899 * Diagnostics (mainly for HAMMER debugging). Check for
900 * overlapping buffers.
902 if (check_buf_overlap) {
904 bx = buf_rb_hash_RB_PREV(bp);
906 if (bx->b_loffset + bx->b_bufsize > bp->b_loffset) {
907 kprintf("bgetvp: overlapl %016jx/%d %016jx "
909 (intmax_t)bx->b_loffset,
911 (intmax_t)bp->b_loffset,
913 if (check_buf_overlap > 1)
914 panic("bgetvp - overlapping buffer");
917 bx = buf_rb_hash_RB_NEXT(bp);
919 if (bp->b_loffset + testsize > bx->b_loffset) {
920 kprintf("bgetvp: overlapr %016jx/%d %016jx "
922 (intmax_t)bp->b_loffset,
924 (intmax_t)bx->b_loffset,
926 if (check_buf_overlap > 1)
927 panic("bgetvp - overlapping buffer");
932 bp->b_flags |= B_HASHED;
933 bp->b_flags |= B_VNCLEAN;
934 if (buf_rb_tree_RB_INSERT(&vp->v_rbclean_tree, bp))
935 panic("reassignbuf: dup lblk/clean vp %p bp %p", vp, bp);
937 lwkt_reltoken(&vp->v_token);
942 * Disassociate a buffer from a vnode.
947 brelvp(struct buf *bp)
951 KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
954 * Delete from old vnode list, if on one.
957 lwkt_gettoken(&vp->v_token);
958 if (bp->b_flags & (B_VNDIRTY | B_VNCLEAN)) {
959 if (bp->b_flags & B_VNDIRTY)
960 buf_rb_tree_RB_REMOVE(&vp->v_rbdirty_tree, bp);
962 buf_rb_tree_RB_REMOVE(&vp->v_rbclean_tree, bp);
963 bp->b_flags &= ~(B_VNDIRTY | B_VNCLEAN);
965 if (bp->b_flags & B_HASHED) {
966 buf_rb_hash_RB_REMOVE(&vp->v_rbhash_tree, bp);
967 bp->b_flags &= ~B_HASHED;
969 if ((vp->v_flag & VONWORKLST) && RB_EMPTY(&vp->v_rbdirty_tree))
970 vn_syncer_remove(vp);
973 lwkt_reltoken(&vp->v_token);
979 * Reassign the buffer to the proper clean/dirty list based on B_DELWRI.
980 * This routine is called when the state of the B_DELWRI bit is changed.
982 * Must be called with vp->v_token held.
986 reassignbuf(struct buf *bp)
988 struct vnode *vp = bp->b_vp;
991 ASSERT_LWKT_TOKEN_HELD(&vp->v_token);
995 * B_PAGING flagged buffers cannot be reassigned because their vp
996 * is not fully linked in.
998 if (bp->b_flags & B_PAGING)
999 panic("cannot reassign paging buffer");
1001 if (bp->b_flags & B_DELWRI) {
1003 * Move to the dirty list, add the vnode to the worklist
1005 if (bp->b_flags & B_VNCLEAN) {
1006 buf_rb_tree_RB_REMOVE(&vp->v_rbclean_tree, bp);
1007 bp->b_flags &= ~B_VNCLEAN;
1009 if ((bp->b_flags & B_VNDIRTY) == 0) {
1010 if (buf_rb_tree_RB_INSERT(&vp->v_rbdirty_tree, bp)) {
1011 panic("reassignbuf: dup lblk vp %p bp %p",
1014 bp->b_flags |= B_VNDIRTY;
1016 if ((vp->v_flag & VONWORKLST) == 0) {
1017 switch (vp->v_type) {
1024 vp->v_rdev->si_mountpoint != NULL) {
1032 vn_syncer_add(vp, delay);
1036 * Move to the clean list, remove the vnode from the worklist
1037 * if no dirty blocks remain.
1039 if (bp->b_flags & B_VNDIRTY) {
1040 buf_rb_tree_RB_REMOVE(&vp->v_rbdirty_tree, bp);
1041 bp->b_flags &= ~B_VNDIRTY;
1043 if ((bp->b_flags & B_VNCLEAN) == 0) {
1044 if (buf_rb_tree_RB_INSERT(&vp->v_rbclean_tree, bp)) {
1045 panic("reassignbuf: dup lblk vp %p bp %p",
1048 bp->b_flags |= B_VNCLEAN;
1050 if ((vp->v_flag & VONWORKLST) &&
1051 RB_EMPTY(&vp->v_rbdirty_tree)) {
1052 vn_syncer_remove(vp);
1058 * Create a vnode for a block device.
1059 * Used for mounting the root file system.
1061 extern struct vop_ops *devfs_vnode_dev_vops_p;
1063 bdevvp(cdev_t dev, struct vnode **vpp)
1073 error = getspecialvnode(VT_NON, NULL, &devfs_vnode_dev_vops_p,
1084 v_associate_rdev(vp, dev);
1085 vp->v_umajor = dev->si_umajor;
1086 vp->v_uminor = dev->si_uminor;
1093 v_associate_rdev(struct vnode *vp, cdev_t dev)
1097 if (dev_is_good(dev) == 0)
1099 KKASSERT(vp->v_rdev == NULL);
1100 vp->v_rdev = reference_dev(dev);
1101 lwkt_gettoken(&spechash_token);
1102 SLIST_INSERT_HEAD(&dev->si_hlist, vp, v_cdevnext);
1103 lwkt_reltoken(&spechash_token);
1108 v_release_rdev(struct vnode *vp)
1112 if ((dev = vp->v_rdev) != NULL) {
1113 lwkt_gettoken(&spechash_token);
1114 SLIST_REMOVE(&dev->si_hlist, vp, vnode, v_cdevnext);
1117 lwkt_reltoken(&spechash_token);
1122 * Add a vnode to the alias list hung off the cdev_t. We only associate
1123 * the device number with the vnode. The actual device is not associated
1124 * until the vnode is opened (usually in spec_open()), and will be
1125 * disassociated on last close.
1128 addaliasu(struct vnode *nvp, int x, int y)
1130 if (nvp->v_type != VBLK && nvp->v_type != VCHR)
1131 panic("addaliasu on non-special vnode");
1137 * Simple call that a filesystem can make to try to get rid of a
1138 * vnode. It will fail if anyone is referencing the vnode (including
1141 * The filesystem can check whether its in-memory inode structure still
1142 * references the vp on return.
1145 vclean_unlocked(struct vnode *vp)
1148 if (sysref_isactive(&vp->v_sysref) == 0)
1154 * Disassociate a vnode from its underlying filesystem.
1156 * The vnode must be VX locked and referenced. In all normal situations
1157 * there are no active references. If vclean_vxlocked() is called while
1158 * there are active references, the vnode is being ripped out and we have
1159 * to call VOP_CLOSE() as appropriate before we can reclaim it.
1162 vclean_vxlocked(struct vnode *vp, int flags)
1169 * If the vnode has already been reclaimed we have nothing to do.
1171 if (vp->v_flag & VRECLAIMED)
1173 vsetflags(vp, VRECLAIMED);
1176 * Scrap the vfs cache
1178 while (cache_inval_vp(vp, 0) != 0) {
1179 kprintf("Warning: vnode %p clean/cache_resolution race detected\n", vp);
1180 tsleep(vp, 0, "vclninv", 2);
1184 * Check to see if the vnode is in use. If so we have to reference it
1185 * before we clean it out so that its count cannot fall to zero and
1186 * generate a race against ourselves to recycle it.
1188 active = sysref_isactive(&vp->v_sysref);
1191 * Clean out any buffers associated with the vnode and destroy its
1192 * object, if it has one.
1194 vinvalbuf(vp, V_SAVE, 0, 0);
1197 * If purging an active vnode (typically during a forced unmount
1198 * or reboot), it must be closed and deactivated before being
1199 * reclaimed. This isn't really all that safe, but what can
1202 * Note that neither of these routines unlocks the vnode.
1204 if (active && (flags & DOCLOSE)) {
1205 while ((n = vp->v_opencount) != 0) {
1206 if (vp->v_writecount)
1207 VOP_CLOSE(vp, FWRITE|FNONBLOCK);
1209 VOP_CLOSE(vp, FNONBLOCK);
1210 if (vp->v_opencount == n) {
1211 kprintf("Warning: unable to force-close"
1219 * If the vnode has not been deactivated, deactivated it. Deactivation
1220 * can create new buffers and VM pages so we have to call vinvalbuf()
1221 * again to make sure they all get flushed.
1223 * This can occur if a file with a link count of 0 needs to be
1226 * If the vnode is already dead don't try to deactivate it.
1228 if ((vp->v_flag & VINACTIVE) == 0) {
1229 vsetflags(vp, VINACTIVE);
1232 vinvalbuf(vp, V_SAVE, 0, 0);
1236 * If the vnode has an object, destroy it.
1238 if ((object = vp->v_object) != NULL) {
1239 lwkt_gettoken(&vm_token);
1240 KKASSERT(object == vp->v_object);
1241 if (object->ref_count == 0) {
1242 if ((object->flags & OBJ_DEAD) == 0)
1243 vm_object_terminate(object);
1245 vm_pager_deallocate(object);
1247 vclrflags(vp, VOBJBUF);
1248 lwkt_reltoken(&vm_token);
1250 KKASSERT((vp->v_flag & VOBJBUF) == 0);
1253 * Reclaim the vnode if not already dead.
1255 if (vp->v_mount && VOP_RECLAIM(vp))
1256 panic("vclean: cannot reclaim");
1259 * Done with purge, notify sleepers of the grim news.
1261 vp->v_ops = &dead_vnode_vops_p;
1266 * If we are destroying an active vnode, reactivate it now that
1267 * we have reassociated it with deadfs. This prevents the system
1268 * from crashing on the vnode due to it being unexpectedly marked
1269 * as inactive or reclaimed.
1271 if (active && (flags & DOCLOSE)) {
1272 vclrflags(vp, VINACTIVE | VRECLAIMED);
1277 * Eliminate all activity associated with the requested vnode
1278 * and with all vnodes aliased to the requested vnode.
1280 * The vnode must be referenced but should not be locked.
1283 vrevoke(struct vnode *vp, struct ucred *cred)
1291 * If the vnode has a device association, scrap all vnodes associated
1292 * with the device. Don't let the device disappear on us while we
1293 * are scrapping the vnodes.
1295 * The passed vp will probably show up in the list, do not VX lock
1298 * Releasing the vnode's rdev here can mess up specfs's call to
1299 * device close, so don't do it. The vnode has been disassociated
1300 * and the device will be closed after the last ref on the related
1301 * fp goes away (if not still open by e.g. the kernel).
1303 if (vp->v_type != VCHR) {
1304 error = fdrevoke(vp, DTYPE_VNODE, cred);
1307 if ((dev = vp->v_rdev) == NULL) {
1311 lwkt_gettoken(&spechash_token);
1313 vqn = SLIST_FIRST(&dev->si_hlist);
1316 while ((vq = vqn) != NULL) {
1317 vqn = SLIST_NEXT(vqn, v_cdevnext);
1320 fdrevoke(vq, DTYPE_VNODE, cred);
1321 /*v_release_rdev(vq);*/
1324 lwkt_reltoken(&spechash_token);
1331 * This is called when the object underlying a vnode is being destroyed,
1332 * such as in a remove(). Try to recycle the vnode immediately if the
1333 * only active reference is our reference.
1335 * Directory vnodes in the namecache with children cannot be immediately
1336 * recycled because numerous VOP_N*() ops require them to be stable.
1338 * To avoid recursive recycling from VOP_INACTIVE implemenetations this
1339 * function is a NOP if VRECLAIMED is already set.
1342 vrecycle(struct vnode *vp)
1344 if (vp->v_sysref.refcnt <= 1 && (vp->v_flag & VRECLAIMED) == 0) {
1345 if (cache_inval_vp_nonblock(vp))
1354 * Return the maximum I/O size allowed for strategy calls on VP.
1356 * If vp is VCHR or VBLK we dive the device, otherwise we use
1357 * the vp's mount info.
1360 vmaxiosize(struct vnode *vp)
1362 if (vp->v_type == VBLK || vp->v_type == VCHR) {
1363 return(vp->v_rdev->si_iosize_max);
1365 return(vp->v_mount->mnt_iosize_max);
1370 * Eliminate all activity associated with a vnode in preparation for reuse.
1372 * The vnode must be VX locked and refd and will remain VX locked and refd
1373 * on return. This routine may be called with the vnode in any state, as
1374 * long as it is VX locked. The vnode will be cleaned out and marked
1375 * VRECLAIMED but will not actually be reused until all existing refs and
1378 * NOTE: This routine may be called on a vnode which has not yet been
1379 * already been deactivated (VOP_INACTIVE), or on a vnode which has
1380 * already been reclaimed.
1382 * This routine is not responsible for placing us back on the freelist.
1383 * Instead, it happens automatically when the caller releases the VX lock
1384 * (assuming there aren't any other references).
1387 vgone_vxlocked(struct vnode *vp)
1390 * assert that the VX lock is held. This is an absolute requirement
1391 * now for vgone_vxlocked() to be called.
1393 KKASSERT(vp->v_lock.lk_exclusivecount == 1);
1398 * Clean out the filesystem specific data and set the VRECLAIMED
1399 * bit. Also deactivate the vnode if necessary.
1401 vclean_vxlocked(vp, DOCLOSE);
1404 * Delete from old mount point vnode list, if on one.
1406 if (vp->v_mount != NULL) {
1407 KKASSERT(vp->v_data == NULL);
1408 insmntque(vp, NULL);
1412 * If special device, remove it from special device alias list
1413 * if it is on one. This should normally only occur if a vnode is
1414 * being revoked as the device should otherwise have been released
1417 if ((vp->v_type == VBLK || vp->v_type == VCHR) && vp->v_rdev != NULL) {
1429 * Lookup a vnode by device number.
1431 * Returns non-zero and *vpp set to a vref'd vnode on success.
1432 * Returns zero on failure.
1435 vfinddev(cdev_t dev, enum vtype type, struct vnode **vpp)
1439 lwkt_gettoken(&spechash_token);
1440 SLIST_FOREACH(vp, &dev->si_hlist, v_cdevnext) {
1441 if (type == vp->v_type) {
1444 lwkt_reltoken(&spechash_token);
1448 lwkt_reltoken(&spechash_token);
1453 * Calculate the total number of references to a special device. This
1454 * routine may only be called for VBLK and VCHR vnodes since v_rdev is
1455 * an overloaded field. Since udev2dev can now return NULL, we have
1456 * to check for a NULL v_rdev.
1459 count_dev(cdev_t dev)
1464 if (SLIST_FIRST(&dev->si_hlist)) {
1465 lwkt_gettoken(&spechash_token);
1466 SLIST_FOREACH(vp, &dev->si_hlist, v_cdevnext) {
1467 count += vp->v_opencount;
1469 lwkt_reltoken(&spechash_token);
1475 vcount(struct vnode *vp)
1477 if (vp->v_rdev == NULL)
1479 return(count_dev(vp->v_rdev));
1483 * Initialize VMIO for a vnode. This routine MUST be called before a
1484 * VFS can issue buffer cache ops on a vnode. It is typically called
1485 * when a vnode is initialized from its inode.
1488 vinitvmio(struct vnode *vp, off_t filesize, int blksize, int boff)
1494 if ((object = vp->v_object) == NULL) {
1495 lwkt_gettoken(&vm_token);
1496 object = vnode_pager_alloc(vp, filesize, 0, 0, blksize, boff);
1498 * Dereference the reference we just created. This assumes
1499 * that the object is associated with the vp.
1501 object->ref_count--;
1503 lwkt_reltoken(&vm_token);
1505 if (object->flags & OBJ_DEAD) {
1507 vm_object_dead_sleep(object, "vodead");
1508 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1512 KASSERT(vp->v_object != NULL, ("vinitvmio: NULL object"));
1513 vsetflags(vp, VOBJBUF);
1519 * Print out a description of a vnode.
1521 static char *typename[] =
1522 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD"};
1525 vprint(char *label, struct vnode *vp)
1530 kprintf("%s: %p: ", label, (void *)vp);
1532 kprintf("%p: ", (void *)vp);
1533 kprintf("type %s, sysrefs %d, writecount %d, holdcnt %d,",
1534 typename[vp->v_type],
1535 vp->v_sysref.refcnt, vp->v_writecount, vp->v_auxrefs);
1537 if (vp->v_flag & VROOT)
1538 strcat(buf, "|VROOT");
1539 if (vp->v_flag & VPFSROOT)
1540 strcat(buf, "|VPFSROOT");
1541 if (vp->v_flag & VTEXT)
1542 strcat(buf, "|VTEXT");
1543 if (vp->v_flag & VSYSTEM)
1544 strcat(buf, "|VSYSTEM");
1545 if (vp->v_flag & VFREE)
1546 strcat(buf, "|VFREE");
1547 if (vp->v_flag & VOBJBUF)
1548 strcat(buf, "|VOBJBUF");
1550 kprintf(" flags (%s)", &buf[1]);
1551 if (vp->v_data == NULL) {
1560 * Do the usual access checking.
1561 * file_mode, uid and gid are from the vnode in question,
1562 * while acc_mode and cred are from the VOP_ACCESS parameter list
1565 vaccess(enum vtype type, mode_t file_mode, uid_t uid, gid_t gid,
1566 mode_t acc_mode, struct ucred *cred)
1572 * Super-user always gets read/write access, but execute access depends
1573 * on at least one execute bit being set.
1575 if (priv_check_cred(cred, PRIV_ROOT, 0) == 0) {
1576 if ((acc_mode & VEXEC) && type != VDIR &&
1577 (file_mode & (S_IXUSR|S_IXGRP|S_IXOTH)) == 0)
1584 /* Otherwise, check the owner. */
1585 if (cred->cr_uid == uid) {
1586 if (acc_mode & VEXEC)
1588 if (acc_mode & VREAD)
1590 if (acc_mode & VWRITE)
1592 return ((file_mode & mask) == mask ? 0 : EACCES);
1595 /* Otherwise, check the groups. */
1596 ismember = groupmember(gid, cred);
1597 if (cred->cr_svgid == gid || ismember) {
1598 if (acc_mode & VEXEC)
1600 if (acc_mode & VREAD)
1602 if (acc_mode & VWRITE)
1604 return ((file_mode & mask) == mask ? 0 : EACCES);
1607 /* Otherwise, check everyone else. */
1608 if (acc_mode & VEXEC)
1610 if (acc_mode & VREAD)
1612 if (acc_mode & VWRITE)
1614 return ((file_mode & mask) == mask ? 0 : EACCES);
1618 #include <ddb/ddb.h>
1620 static int db_show_locked_vnodes(struct mount *mp, void *data);
1623 * List all of the locked vnodes in the system.
1624 * Called when debugging the kernel.
1626 DB_SHOW_COMMAND(lockedvnodes, lockedvnodes)
1628 kprintf("Locked vnodes\n");
1629 mountlist_scan(db_show_locked_vnodes, NULL,
1630 MNTSCAN_FORWARD|MNTSCAN_NOBUSY);
1634 db_show_locked_vnodes(struct mount *mp, void *data __unused)
1638 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
1639 if (vn_islocked(vp))
1647 * Top level filesystem related information gathering.
1649 static int sysctl_ovfs_conf (SYSCTL_HANDLER_ARGS);
1652 vfs_sysctl(SYSCTL_HANDLER_ARGS)
1654 int *name = (int *)arg1 - 1; /* XXX */
1655 u_int namelen = arg2 + 1; /* XXX */
1656 struct vfsconf *vfsp;
1659 #if 1 || defined(COMPAT_PRELITE2)
1660 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
1662 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
1666 /* all sysctl names at this level are at least name and field */
1668 return (ENOTDIR); /* overloaded */
1669 if (name[0] != VFS_GENERIC) {
1670 vfsp = vfsconf_find_by_typenum(name[0]);
1672 return (EOPNOTSUPP);
1673 return ((*vfsp->vfc_vfsops->vfs_sysctl)(&name[1], namelen - 1,
1674 oldp, oldlenp, newp, newlen, p));
1678 case VFS_MAXTYPENUM:
1681 maxtypenum = vfsconf_get_maxtypenum();
1682 return (SYSCTL_OUT(req, &maxtypenum, sizeof(maxtypenum)));
1685 return (ENOTDIR); /* overloaded */
1686 vfsp = vfsconf_find_by_typenum(name[2]);
1688 return (EOPNOTSUPP);
1689 return (SYSCTL_OUT(req, vfsp, sizeof *vfsp));
1691 return (EOPNOTSUPP);
1694 SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD, vfs_sysctl,
1695 "Generic filesystem");
1697 #if 1 || defined(COMPAT_PRELITE2)
1700 sysctl_ovfs_conf_iter(struct vfsconf *vfsp, void *data)
1703 struct ovfsconf ovfs;
1704 struct sysctl_req *req = (struct sysctl_req*) data;
1706 bzero(&ovfs, sizeof(ovfs));
1707 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
1708 strcpy(ovfs.vfc_name, vfsp->vfc_name);
1709 ovfs.vfc_index = vfsp->vfc_typenum;
1710 ovfs.vfc_refcount = vfsp->vfc_refcount;
1711 ovfs.vfc_flags = vfsp->vfc_flags;
1712 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
1714 return error; /* abort iteration with error code */
1716 return 0; /* continue iterating with next element */
1720 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
1722 return vfsconf_each(sysctl_ovfs_conf_iter, (void*)req);
1725 #endif /* 1 || COMPAT_PRELITE2 */
1728 * Check to see if a filesystem is mounted on a block device.
1731 vfs_mountedon(struct vnode *vp)
1735 if ((dev = vp->v_rdev) == NULL) {
1736 /* if (vp->v_type != VBLK)
1737 dev = get_dev(vp->v_uminor, vp->v_umajor); */
1739 if (dev != NULL && dev->si_mountpoint)
1745 * Unmount all filesystems. The list is traversed in reverse order
1746 * of mounting to avoid dependencies.
1749 static int vfs_umountall_callback(struct mount *mp, void *data);
1752 vfs_unmountall(void)
1757 count = mountlist_scan(vfs_umountall_callback,
1758 NULL, MNTSCAN_REVERSE|MNTSCAN_NOBUSY);
1764 vfs_umountall_callback(struct mount *mp, void *data)
1768 error = dounmount(mp, MNT_FORCE);
1770 mountlist_remove(mp);
1771 kprintf("unmount of filesystem mounted from %s failed (",
1772 mp->mnt_stat.f_mntfromname);
1776 kprintf("%d)\n", error);
1782 * Checks the mount flags for parameter mp and put the names comma-separated
1783 * into a string buffer buf with a size limit specified by len.
1785 * It returns the number of bytes written into buf, and (*errorp) will be
1786 * set to 0, EINVAL (if passed length is 0), or ENOSPC (supplied buffer was
1787 * not large enough). The buffer will be 0-terminated if len was not 0.
1790 vfs_flagstostr(int flags, const struct mountctl_opt *optp,
1791 char *buf, size_t len, int *errorp)
1793 static const struct mountctl_opt optnames[] = {
1794 { MNT_ASYNC, "asynchronous" },
1795 { MNT_EXPORTED, "NFS exported" },
1796 { MNT_LOCAL, "local" },
1797 { MNT_NOATIME, "noatime" },
1798 { MNT_NODEV, "nodev" },
1799 { MNT_NOEXEC, "noexec" },
1800 { MNT_NOSUID, "nosuid" },
1801 { MNT_NOSYMFOLLOW, "nosymfollow" },
1802 { MNT_QUOTA, "with-quotas" },
1803 { MNT_RDONLY, "read-only" },
1804 { MNT_SYNCHRONOUS, "synchronous" },
1805 { MNT_UNION, "union" },
1806 { MNT_NOCLUSTERR, "noclusterr" },
1807 { MNT_NOCLUSTERW, "noclusterw" },
1808 { MNT_SUIDDIR, "suiddir" },
1809 { MNT_SOFTDEP, "soft-updates" },
1810 { MNT_IGNORE, "ignore" },
1820 bleft = len - 1; /* leave room for trailing \0 */
1823 * Checks the size of the string. If it contains
1824 * any data, then we will append the new flags to
1827 actsize = strlen(buf);
1831 /* Default flags if no flags passed */
1835 if (bleft < 0) { /* degenerate case, 0-length buffer */
1840 for (; flags && optp->o_opt; ++optp) {
1841 if ((flags & optp->o_opt) == 0)
1843 optlen = strlen(optp->o_name);
1844 if (bwritten || actsize > 0) {
1849 buf[bwritten++] = ',';
1850 buf[bwritten++] = ' ';
1853 if (bleft < optlen) {
1857 bcopy(optp->o_name, buf + bwritten, optlen);
1860 flags &= ~optp->o_opt;
1864 * Space already reserved for trailing \0
1871 * Build hash lists of net addresses and hang them off the mount point.
1872 * Called by ufs_mount() to set up the lists of export addresses.
1875 vfs_hang_addrlist(struct mount *mp, struct netexport *nep,
1876 const struct export_args *argp)
1879 struct radix_node_head *rnh;
1881 struct radix_node *rn;
1882 struct sockaddr *saddr, *smask = 0;
1886 if (argp->ex_addrlen == 0) {
1887 if (mp->mnt_flag & MNT_DEFEXPORTED)
1889 np = &nep->ne_defexported;
1890 np->netc_exflags = argp->ex_flags;
1891 np->netc_anon = argp->ex_anon;
1892 np->netc_anon.cr_ref = 1;
1893 mp->mnt_flag |= MNT_DEFEXPORTED;
1897 if (argp->ex_addrlen < 0 || argp->ex_addrlen > MLEN)
1899 if (argp->ex_masklen < 0 || argp->ex_masklen > MLEN)
1902 i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
1903 np = (struct netcred *) kmalloc(i, M_NETADDR, M_WAITOK | M_ZERO);
1904 saddr = (struct sockaddr *) (np + 1);
1905 if ((error = copyin(argp->ex_addr, (caddr_t) saddr, argp->ex_addrlen)))
1907 if (saddr->sa_len > argp->ex_addrlen)
1908 saddr->sa_len = argp->ex_addrlen;
1909 if (argp->ex_masklen) {
1910 smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen);
1911 error = copyin(argp->ex_mask, (caddr_t)smask, argp->ex_masklen);
1914 if (smask->sa_len > argp->ex_masklen)
1915 smask->sa_len = argp->ex_masklen;
1917 i = saddr->sa_family;
1918 if ((rnh = nep->ne_rtable[i]) == 0) {
1920 * Seems silly to initialize every AF when most are not used,
1921 * do so on demand here
1923 SLIST_FOREACH(dom, &domains, dom_next)
1924 if (dom->dom_family == i && dom->dom_rtattach) {
1925 dom->dom_rtattach((void **) &nep->ne_rtable[i],
1929 if ((rnh = nep->ne_rtable[i]) == 0) {
1934 rn = (*rnh->rnh_addaddr) ((char *) saddr, (char *) smask, rnh,
1936 if (rn == 0 || np != (struct netcred *) rn) { /* already exists */
1940 np->netc_exflags = argp->ex_flags;
1941 np->netc_anon = argp->ex_anon;
1942 np->netc_anon.cr_ref = 1;
1945 kfree(np, M_NETADDR);
1951 vfs_free_netcred(struct radix_node *rn, void *w)
1953 struct radix_node_head *rnh = (struct radix_node_head *) w;
1955 (*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, rnh);
1956 kfree((caddr_t) rn, M_NETADDR);
1961 * Free the net address hash lists that are hanging off the mount points.
1964 vfs_free_addrlist(struct netexport *nep)
1967 struct radix_node_head *rnh;
1969 for (i = 0; i <= AF_MAX; i++)
1970 if ((rnh = nep->ne_rtable[i])) {
1971 (*rnh->rnh_walktree) (rnh, vfs_free_netcred,
1973 kfree((caddr_t) rnh, M_RTABLE);
1974 nep->ne_rtable[i] = 0;
1979 vfs_export(struct mount *mp, struct netexport *nep,
1980 const struct export_args *argp)
1984 if (argp->ex_flags & MNT_DELEXPORT) {
1985 if (mp->mnt_flag & MNT_EXPUBLIC) {
1986 vfs_setpublicfs(NULL, NULL, NULL);
1987 mp->mnt_flag &= ~MNT_EXPUBLIC;
1989 vfs_free_addrlist(nep);
1990 mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
1992 if (argp->ex_flags & MNT_EXPORTED) {
1993 if (argp->ex_flags & MNT_EXPUBLIC) {
1994 if ((error = vfs_setpublicfs(mp, nep, argp)) != 0)
1996 mp->mnt_flag |= MNT_EXPUBLIC;
1998 if ((error = vfs_hang_addrlist(mp, nep, argp)))
2000 mp->mnt_flag |= MNT_EXPORTED;
2007 * Set the publicly exported filesystem (WebNFS). Currently, only
2008 * one public filesystem is possible in the spec (RFC 2054 and 2055)
2011 vfs_setpublicfs(struct mount *mp, struct netexport *nep,
2012 const struct export_args *argp)
2019 * mp == NULL -> invalidate the current info, the FS is
2020 * no longer exported. May be called from either vfs_export
2021 * or unmount, so check if it hasn't already been done.
2024 if (nfs_pub.np_valid) {
2025 nfs_pub.np_valid = 0;
2026 if (nfs_pub.np_index != NULL) {
2027 FREE(nfs_pub.np_index, M_TEMP);
2028 nfs_pub.np_index = NULL;
2035 * Only one allowed at a time.
2037 if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
2041 * Get real filehandle for root of exported FS.
2043 bzero((caddr_t)&nfs_pub.np_handle, sizeof(nfs_pub.np_handle));
2044 nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid;
2046 if ((error = VFS_ROOT(mp, &rvp)))
2049 if ((error = VFS_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid)))
2055 * If an indexfile was specified, pull it in.
2057 if (argp->ex_indexfile != NULL) {
2060 error = vn_get_namelen(rvp, &namelen);
2063 MALLOC(nfs_pub.np_index, char *, namelen, M_TEMP,
2065 error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
2069 * Check for illegal filenames.
2071 for (cp = nfs_pub.np_index; *cp; cp++) {
2079 FREE(nfs_pub.np_index, M_TEMP);
2084 nfs_pub.np_mount = mp;
2085 nfs_pub.np_valid = 1;
2090 vfs_export_lookup(struct mount *mp, struct netexport *nep,
2091 struct sockaddr *nam)
2094 struct radix_node_head *rnh;
2095 struct sockaddr *saddr;
2098 if (mp->mnt_flag & MNT_EXPORTED) {
2100 * Lookup in the export list first.
2104 rnh = nep->ne_rtable[saddr->sa_family];
2106 np = (struct netcred *)
2107 (*rnh->rnh_matchaddr)((char *)saddr,
2109 if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
2114 * If no address match, use the default if it exists.
2116 if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
2117 np = &nep->ne_defexported;
2123 * perform msync on all vnodes under a mount point. The mount point must
2124 * be locked. This code is also responsible for lazy-freeing unreferenced
2125 * vnodes whos VM objects no longer contain pages.
2127 * NOTE: MNT_WAIT still skips vnodes in the VXLOCK state.
2129 * NOTE: XXX VOP_PUTPAGES and friends requires that the vnode be locked,
2130 * but vnode_pager_putpages() doesn't lock the vnode. We have to do it
2131 * way up in this high level function.
2133 static int vfs_msync_scan1(struct mount *mp, struct vnode *vp, void *data);
2134 static int vfs_msync_scan2(struct mount *mp, struct vnode *vp, void *data);
2137 vfs_msync(struct mount *mp, int flags)
2142 * tmpfs sets this flag to prevent msync(), sync, and the
2143 * filesystem periodic syncer from trying to flush VM pages
2144 * to swap. Only pure memory pressure flushes tmpfs VM pages
2147 if (mp->mnt_kern_flag & MNTK_NOMSYNC)
2151 * Ok, scan the vnodes for work.
2153 vmsc_flags = VMSC_GETVP;
2154 if (flags != MNT_WAIT)
2155 vmsc_flags |= VMSC_NOWAIT;
2156 vmntvnodescan(mp, vmsc_flags, vfs_msync_scan1, vfs_msync_scan2,
2157 (void *)(intptr_t)flags);
2161 * scan1 is a fast pre-check. There could be hundreds of thousands of
2162 * vnodes, we cannot afford to do anything heavy weight until we have a
2163 * fairly good indication that there is work to do.
2167 vfs_msync_scan1(struct mount *mp, struct vnode *vp, void *data)
2169 int flags = (int)(intptr_t)data;
2171 if ((vp->v_flag & VRECLAIMED) == 0) {
2172 if (vshouldmsync(vp))
2173 return(0); /* call scan2 */
2174 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
2175 (vp->v_flag & VOBJDIRTY) &&
2176 (flags == MNT_WAIT || vn_islocked(vp) == 0)) {
2177 return(0); /* call scan2 */
2182 * do not call scan2, continue the loop
2188 * This callback is handed a locked vnode.
2192 vfs_msync_scan2(struct mount *mp, struct vnode *vp, void *data)
2195 int flags = (int)(intptr_t)data;
2197 if (vp->v_flag & VRECLAIMED)
2200 if ((mp->mnt_flag & MNT_RDONLY) == 0 && (vp->v_flag & VOBJDIRTY)) {
2201 if ((obj = vp->v_object) != NULL) {
2202 vm_object_page_clean(obj, 0, 0,
2203 flags == MNT_WAIT ? OBJPC_SYNC : OBJPC_NOSYNC);
2210 * Wake up anyone interested in vp because it is being revoked.
2213 vn_gone(struct vnode *vp)
2215 lwkt_gettoken(&vp->v_token);
2216 KNOTE(&vp->v_pollinfo.vpi_kqinfo.ki_note, NOTE_REVOKE);
2217 lwkt_reltoken(&vp->v_token);
2221 * extract the cdev_t from a VBLK or VCHR. The vnode must have been opened
2222 * (or v_rdev might be NULL).
2225 vn_todev(struct vnode *vp)
2227 if (vp->v_type != VBLK && vp->v_type != VCHR)
2229 KKASSERT(vp->v_rdev != NULL);
2230 return (vp->v_rdev);
2234 * Check if vnode represents a disk device. The vnode does not need to be
2240 vn_isdisk(struct vnode *vp, int *errp)
2244 if (vp->v_type != VCHR) {
2257 if (dev_is_good(dev) == 0) {
2262 if ((dev_dflags(dev) & D_DISK) == 0) {
2273 vn_get_namelen(struct vnode *vp, int *namelen)
2276 register_t retval[2];
2278 error = VOP_PATHCONF(vp, _PC_NAME_MAX, retval);
2281 *namelen = (int)retval[0];
2286 vop_write_dirent(int *error, struct uio *uio, ino_t d_ino, uint8_t d_type,
2287 uint16_t d_namlen, const char *d_name)
2292 len = _DIRENT_RECLEN(d_namlen);
2293 if (len > uio->uio_resid)
2296 dp = kmalloc(len, M_TEMP, M_WAITOK | M_ZERO);
2299 dp->d_namlen = d_namlen;
2300 dp->d_type = d_type;
2301 bcopy(d_name, dp->d_name, d_namlen);
2303 *error = uiomove((caddr_t)dp, len, uio);
2311 vn_mark_atime(struct vnode *vp, struct thread *td)
2313 struct proc *p = td->td_proc;
2314 struct ucred *cred = p ? p->p_ucred : proc0.p_ucred;
2316 if ((vp->v_mount->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0) {
2317 VOP_MARKATIME(vp, cred);