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 $
43 * External virtual filesystem routines
47 #include <sys/param.h>
48 #include <sys/systm.h>
51 #include <sys/dirent.h>
52 #include <sys/domain.h>
53 #include <sys/eventhandler.h>
54 #include <sys/fcntl.h>
56 #include <sys/kernel.h>
57 #include <sys/kthread.h>
58 #include <sys/malloc.h>
60 #include <sys/mount.h>
63 #include <sys/reboot.h>
64 #include <sys/socket.h>
66 #include <sys/sysctl.h>
67 #include <sys/syslog.h>
68 #include <sys/unistd.h>
69 #include <sys/vmmeter.h>
70 #include <sys/vnode.h>
72 #include <machine/limits.h>
75 #include <vm/vm_object.h>
76 #include <vm/vm_extern.h>
77 #include <vm/vm_kern.h>
79 #include <vm/vm_map.h>
80 #include <vm/vm_page.h>
81 #include <vm/vm_pager.h>
82 #include <vm/vnode_pager.h>
83 #include <vm/vm_zone.h>
86 #include <sys/thread2.h>
87 #include <sys/sysref2.h>
88 #include <sys/mplock2.h>
90 static MALLOC_DEFINE(M_NETADDR, "Export Host", "Export host address structure");
93 SYSCTL_INT(_debug, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0,
94 "Number of vnodes allocated");
96 SYSCTL_INT(_debug, OID_AUTO, verbose_reclaims, CTLFLAG_RD, &verbose_reclaims, 0,
97 "Output filename of reclaimed vnode(s)");
99 enum vtype iftovt_tab[16] = {
100 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
101 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
103 int vttoif_tab[9] = {
104 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
105 S_IFSOCK, S_IFIFO, S_IFMT,
108 static int reassignbufcalls;
109 SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW, &reassignbufcalls,
110 0, "Number of times buffers have been reassigned to the proper list");
112 static int check_buf_overlap = 2; /* invasive check */
113 SYSCTL_INT(_vfs, OID_AUTO, check_buf_overlap, CTLFLAG_RW, &check_buf_overlap,
114 0, "Enable overlapping buffer checks");
116 int nfs_mount_type = -1;
117 static struct lwkt_token spechash_token;
118 struct nfs_public nfs_pub; /* publicly exported FS */
121 SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RW,
122 &desiredvnodes, 0, "Maximum number of vnodes");
124 static void vfs_free_addrlist (struct netexport *nep);
125 static int vfs_free_netcred (struct radix_node *rn, void *w);
126 static int vfs_hang_addrlist (struct mount *mp, struct netexport *nep,
127 const struct export_args *argp);
130 * Red black tree functions
132 static int rb_buf_compare(struct buf *b1, struct buf *b2);
133 RB_GENERATE2(buf_rb_tree, buf, b_rbnode, rb_buf_compare, off_t, b_loffset);
134 RB_GENERATE2(buf_rb_hash, buf, b_rbhash, rb_buf_compare, off_t, b_loffset);
137 rb_buf_compare(struct buf *b1, struct buf *b2)
139 if (b1->b_loffset < b2->b_loffset)
141 if (b1->b_loffset > b2->b_loffset)
147 * Returns non-zero if the vnode is a candidate for lazy msyncing.
149 * NOTE: v_object is not stable (this scan can race), however the
150 * mntvnodescan code holds vmobj_token so any VM object we
151 * do find will remain stable storage.
154 vshouldmsync(struct vnode *vp)
158 if (vp->v_auxrefs != 0 || vp->v_sysref.refcnt > 0)
159 return (0); /* other holders */
160 object = vp->v_object;
162 if (object && (object->ref_count || object->resident_page_count))
168 * Initialize the vnode management data structures.
170 * Called from vfsinit()
179 * Desiredvnodes is kern.maxvnodes. We want to scale it
180 * according to available system memory but we may also have
181 * to limit it based on available KVM, which is capped on 32 bit
184 * WARNING! For machines with 64-256M of ram we have to be sure
185 * that the default limit scales down well due to HAMMER
186 * taking up significantly more memory per-vnode vs UFS.
187 * We want around ~5800 on a 128M machine.
189 factor1 = 20 * (sizeof(struct vm_object) + sizeof(struct vnode));
190 factor2 = 22 * (sizeof(struct vm_object) + sizeof(struct vnode));
192 imin((int64_t)vmstats.v_page_count * PAGE_SIZE / factor1,
194 desiredvnodes = imax(desiredvnodes, maxproc * 8);
196 lwkt_token_init(&spechash_token, "spechash");
200 * Knob to control the precision of file timestamps:
202 * 0 = seconds only; nanoseconds zeroed.
203 * 1 = seconds and nanoseconds, accurate within 1/HZ.
204 * 2 = seconds and nanoseconds, truncated to microseconds.
205 * >=3 = seconds and nanoseconds, maximum precision.
207 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC };
209 static int timestamp_precision = TSP_SEC;
210 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW,
211 ×tamp_precision, 0, "Precision of file timestamps");
214 * Get a current timestamp.
219 vfs_timestamp(struct timespec *tsp)
223 switch (timestamp_precision) {
225 tsp->tv_sec = time_second;
233 TIMEVAL_TO_TIMESPEC(&tv, tsp);
243 * Set vnode attributes to VNOVAL
246 vattr_null(struct vattr *vap)
249 vap->va_size = VNOVAL;
250 vap->va_bytes = VNOVAL;
251 vap->va_mode = VNOVAL;
252 vap->va_nlink = VNOVAL;
253 vap->va_uid = VNOVAL;
254 vap->va_gid = VNOVAL;
255 vap->va_fsid = VNOVAL;
256 vap->va_fileid = VNOVAL;
257 vap->va_blocksize = VNOVAL;
258 vap->va_rmajor = VNOVAL;
259 vap->va_rminor = VNOVAL;
260 vap->va_atime.tv_sec = VNOVAL;
261 vap->va_atime.tv_nsec = VNOVAL;
262 vap->va_mtime.tv_sec = VNOVAL;
263 vap->va_mtime.tv_nsec = VNOVAL;
264 vap->va_ctime.tv_sec = VNOVAL;
265 vap->va_ctime.tv_nsec = VNOVAL;
266 vap->va_flags = VNOVAL;
267 vap->va_gen = VNOVAL;
269 /* va_*_uuid fields are only valid if related flags are set */
273 * Flush out and invalidate all buffers associated with a vnode.
277 static int vinvalbuf_bp(struct buf *bp, void *data);
279 struct vinvalbuf_bp_info {
288 vinvalbuf(struct vnode *vp, int flags, int slpflag, int slptimeo)
290 struct vinvalbuf_bp_info info;
294 lwkt_gettoken(&vp->v_token);
297 * If we are being asked to save, call fsync to ensure that the inode
300 if (flags & V_SAVE) {
301 error = bio_track_wait(&vp->v_track_write, slpflag, slptimeo);
304 if (!RB_EMPTY(&vp->v_rbdirty_tree)) {
305 if ((error = VOP_FSYNC(vp, MNT_WAIT, 0)) != 0)
309 * Dirty bufs may be left or generated via races
310 * in circumstances where vinvalbuf() is called on
311 * a vnode not undergoing reclamation. Only
312 * panic if we are trying to reclaim the vnode.
314 if ((vp->v_flag & VRECLAIMED) &&
315 (bio_track_active(&vp->v_track_write) ||
316 !RB_EMPTY(&vp->v_rbdirty_tree))) {
317 panic("vinvalbuf: dirty bufs");
321 info.slptimeo = slptimeo;
322 info.lkflags = LK_EXCLUSIVE | LK_SLEEPFAIL;
323 if (slpflag & PCATCH)
324 info.lkflags |= LK_PCATCH;
329 * Flush the buffer cache until nothing is left.
331 while (!RB_EMPTY(&vp->v_rbclean_tree) ||
332 !RB_EMPTY(&vp->v_rbdirty_tree)) {
334 error = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree, NULL,
335 vinvalbuf_bp, &info);
338 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
339 vinvalbuf_bp, &info);
344 * Wait for I/O completion. We may block in the pip code so we have
348 bio_track_wait(&vp->v_track_write, 0, 0);
349 if ((object = vp->v_object) != NULL) {
350 while (object->paging_in_progress)
351 vm_object_pip_sleep(object, "vnvlbx");
353 } while (bio_track_active(&vp->v_track_write));
356 * Destroy the copy in the VM cache, too.
358 if ((object = vp->v_object) != NULL) {
359 vm_object_page_remove(object, 0, 0,
360 (flags & V_SAVE) ? TRUE : FALSE);
363 if (!RB_EMPTY(&vp->v_rbdirty_tree) || !RB_EMPTY(&vp->v_rbclean_tree))
364 panic("vinvalbuf: flush failed");
365 if (!RB_EMPTY(&vp->v_rbhash_tree))
366 panic("vinvalbuf: flush failed, buffers still present");
369 lwkt_reltoken(&vp->v_token);
374 vinvalbuf_bp(struct buf *bp, void *data)
376 struct vinvalbuf_bp_info *info = data;
379 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
380 atomic_add_int(&bp->b_refs, 1);
381 error = BUF_TIMELOCK(bp, info->lkflags,
382 "vinvalbuf", info->slptimeo);
383 atomic_subtract_int(&bp->b_refs, 1);
392 KKASSERT(bp->b_vp == info->vp);
395 * Must check clean/dirty status after successfully locking as
398 if ((info->clean && (bp->b_flags & B_DELWRI)) ||
399 (info->clean == 0 && (bp->b_flags & B_DELWRI) == 0)) {
405 * Note that vfs_bio_awrite expects buffers to reside
406 * on a queue, while bwrite() and brelse() do not.
408 * NOTE: NO B_LOCKED CHECK. Also no buf_checkwrite()
409 * check. This code will write out the buffer, period.
411 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
412 (info->flags & V_SAVE)) {
413 if (bp->b_flags & B_CLUSTEROK) {
419 } else if (info->flags & V_SAVE) {
421 * Cannot set B_NOCACHE on a clean buffer as this will
422 * destroy the VM backing store which might actually
423 * be dirty (and unsynchronized).
426 bp->b_flags |= (B_INVAL | B_RELBUF);
430 bp->b_flags |= (B_INVAL | B_NOCACHE | B_RELBUF);
437 * Truncate a file's buffer and pages to a specified length. This
438 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
441 * The vnode must be locked.
443 static int vtruncbuf_bp_trunc_cmp(struct buf *bp, void *data);
444 static int vtruncbuf_bp_trunc(struct buf *bp, void *data);
445 static int vtruncbuf_bp_metasync_cmp(struct buf *bp, void *data);
446 static int vtruncbuf_bp_metasync(struct buf *bp, void *data);
448 struct vtruncbuf_info {
455 vtruncbuf(struct vnode *vp, off_t length, int blksize)
457 struct vtruncbuf_info info;
458 const char *filename;
462 * Round up to the *next* block, then destroy the buffers in question.
463 * Since we are only removing some of the buffers we must rely on the
464 * scan count to determine whether a loop is necessary.
466 if ((count = (int)(length % blksize)) != 0)
467 info.truncloffset = length + (blksize - count);
469 info.truncloffset = length;
472 lwkt_gettoken(&vp->v_token);
475 count = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree,
476 vtruncbuf_bp_trunc_cmp,
477 vtruncbuf_bp_trunc, &info);
479 count += RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
480 vtruncbuf_bp_trunc_cmp,
481 vtruncbuf_bp_trunc, &info);
485 * For safety, fsync any remaining metadata if the file is not being
486 * truncated to 0. Since the metadata does not represent the entire
487 * dirty list we have to rely on the hit count to ensure that we get
492 count = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
493 vtruncbuf_bp_metasync_cmp,
494 vtruncbuf_bp_metasync, &info);
499 * Clean out any left over VM backing store.
501 * It is possible to have in-progress I/O from buffers that were
502 * not part of the truncation. This should not happen if we
503 * are truncating to 0-length.
505 vnode_pager_setsize(vp, length);
506 bio_track_wait(&vp->v_track_write, 0, 0);
511 spin_lock(&vp->v_spinlock);
512 filename = TAILQ_FIRST(&vp->v_namecache) ?
513 TAILQ_FIRST(&vp->v_namecache)->nc_name : "?";
514 spin_unlock(&vp->v_spinlock);
517 * Make sure no buffers were instantiated while we were trying
518 * to clean out the remaining VM pages. This could occur due
519 * to busy dirty VM pages being flushed out to disk.
523 count = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree,
524 vtruncbuf_bp_trunc_cmp,
525 vtruncbuf_bp_trunc, &info);
527 count += RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
528 vtruncbuf_bp_trunc_cmp,
529 vtruncbuf_bp_trunc, &info);
531 kprintf("Warning: vtruncbuf(): Had to re-clean %d "
532 "left over buffers in %s\n", count, filename);
536 lwkt_reltoken(&vp->v_token);
542 * The callback buffer is beyond the new file EOF and must be destroyed.
543 * Note that the compare function must conform to the RB_SCAN's requirements.
547 vtruncbuf_bp_trunc_cmp(struct buf *bp, void *data)
549 struct vtruncbuf_info *info = data;
551 if (bp->b_loffset >= info->truncloffset)
558 vtruncbuf_bp_trunc(struct buf *bp, void *data)
560 struct vtruncbuf_info *info = data;
563 * Do not try to use a buffer we cannot immediately lock, but sleep
564 * anyway to prevent a livelock. The code will loop until all buffers
567 * We must always revalidate the buffer after locking it to deal
570 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
571 atomic_add_int(&bp->b_refs, 1);
572 if (BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL) == 0)
574 atomic_subtract_int(&bp->b_refs, 1);
575 } else if ((info->clean && (bp->b_flags & B_DELWRI)) ||
576 (info->clean == 0 && (bp->b_flags & B_DELWRI) == 0) ||
577 bp->b_vp != info->vp ||
578 vtruncbuf_bp_trunc_cmp(bp, data)) {
582 bp->b_flags |= (B_INVAL | B_RELBUF | B_NOCACHE);
589 * Fsync all meta-data after truncating a file to be non-zero. Only metadata
590 * blocks (with a negative loffset) are scanned.
591 * Note that the compare function must conform to the RB_SCAN's requirements.
594 vtruncbuf_bp_metasync_cmp(struct buf *bp, void *data __unused)
596 if (bp->b_loffset < 0)
602 vtruncbuf_bp_metasync(struct buf *bp, void *data)
604 struct vtruncbuf_info *info = data;
606 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
607 atomic_add_int(&bp->b_refs, 1);
608 if (BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL) == 0)
610 atomic_subtract_int(&bp->b_refs, 1);
611 } else if ((bp->b_flags & B_DELWRI) == 0 ||
612 bp->b_vp != info->vp ||
613 vtruncbuf_bp_metasync_cmp(bp, data)) {
617 if (bp->b_vp == info->vp)
626 * vfsync - implements a multipass fsync on a file which understands
627 * dependancies and meta-data. The passed vnode must be locked. The
628 * waitfor argument may be MNT_WAIT or MNT_NOWAIT, or MNT_LAZY.
630 * When fsyncing data asynchronously just do one consolidated pass starting
631 * with the most negative block number. This may not get all the data due
634 * When fsyncing data synchronously do a data pass, then a metadata pass,
635 * then do additional data+metadata passes to try to get all the data out.
637 static int vfsync_wait_output(struct vnode *vp,
638 int (*waitoutput)(struct vnode *, struct thread *));
639 static int vfsync_dummy_cmp(struct buf *bp __unused, void *data __unused);
640 static int vfsync_data_only_cmp(struct buf *bp, void *data);
641 static int vfsync_meta_only_cmp(struct buf *bp, void *data);
642 static int vfsync_lazy_range_cmp(struct buf *bp, void *data);
643 static int vfsync_bp(struct buf *bp, void *data);
652 int (*checkdef)(struct buf *);
653 int (*cmpfunc)(struct buf *, void *);
657 vfsync(struct vnode *vp, int waitfor, int passes,
658 int (*checkdef)(struct buf *),
659 int (*waitoutput)(struct vnode *, struct thread *))
661 struct vfsync_info info;
664 bzero(&info, sizeof(info));
666 if ((info.checkdef = checkdef) == NULL)
669 lwkt_gettoken(&vp->v_token);
672 case MNT_LAZY | MNT_NOWAIT:
675 * Lazy (filesystem syncer typ) Asynchronous plus limit the
676 * number of data (not meta) pages we try to flush to 1MB.
677 * A non-zero return means that lazy limit was reached.
679 info.lazylimit = 1024 * 1024;
681 info.cmpfunc = vfsync_lazy_range_cmp;
682 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
683 vfsync_lazy_range_cmp, vfsync_bp, &info);
684 info.cmpfunc = vfsync_meta_only_cmp;
685 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
686 vfsync_meta_only_cmp, vfsync_bp, &info);
689 else if (!RB_EMPTY(&vp->v_rbdirty_tree))
690 vn_syncer_add(vp, 1);
695 * Asynchronous. Do a data-only pass and a meta-only pass.
698 info.cmpfunc = vfsync_data_only_cmp;
699 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_data_only_cmp,
701 info.cmpfunc = vfsync_meta_only_cmp;
702 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_meta_only_cmp,
708 * Synchronous. Do a data-only pass, then a meta-data+data
709 * pass, then additional integrated passes to try to get
710 * all the dependancies flushed.
712 info.cmpfunc = vfsync_data_only_cmp;
713 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_data_only_cmp,
715 error = vfsync_wait_output(vp, waitoutput);
717 info.skippedbufs = 0;
718 info.cmpfunc = vfsync_dummy_cmp;
719 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
721 error = vfsync_wait_output(vp, waitoutput);
722 if (info.skippedbufs) {
723 kprintf("Warning: vfsync skipped %d dirty "
724 "bufs in pass2!\n", info.skippedbufs);
727 while (error == 0 && passes > 0 &&
728 !RB_EMPTY(&vp->v_rbdirty_tree)
731 info.synchronous = 1;
734 info.cmpfunc = vfsync_dummy_cmp;
735 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
741 error = vfsync_wait_output(vp, waitoutput);
745 lwkt_reltoken(&vp->v_token);
750 vfsync_wait_output(struct vnode *vp,
751 int (*waitoutput)(struct vnode *, struct thread *))
755 error = bio_track_wait(&vp->v_track_write, 0, 0);
757 error = waitoutput(vp, curthread);
762 vfsync_dummy_cmp(struct buf *bp __unused, void *data __unused)
768 vfsync_data_only_cmp(struct buf *bp, void *data)
770 if (bp->b_loffset < 0)
776 vfsync_meta_only_cmp(struct buf *bp, void *data)
778 if (bp->b_loffset < 0)
784 vfsync_lazy_range_cmp(struct buf *bp, void *data)
786 struct vfsync_info *info = data;
788 if (bp->b_loffset < info->vp->v_lazyw)
794 vfsync_bp(struct buf *bp, void *data)
796 struct vfsync_info *info = data;
797 struct vnode *vp = info->vp;
801 * Ignore buffers that we cannot immediately lock.
803 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
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. Used for mounting the root file
1061 * A vref()'d vnode is returned.
1063 extern struct vop_ops *devfs_vnode_dev_vops_p;
1065 bdevvp(cdev_t dev, struct vnode **vpp)
1075 error = getspecialvnode(VT_NON, NULL, &devfs_vnode_dev_vops_p,
1086 v_associate_rdev(vp, dev);
1087 vp->v_umajor = dev->si_umajor;
1088 vp->v_uminor = dev->si_uminor;
1095 v_associate_rdev(struct vnode *vp, cdev_t dev)
1099 if (dev_is_good(dev) == 0)
1101 KKASSERT(vp->v_rdev == NULL);
1102 vp->v_rdev = reference_dev(dev);
1103 lwkt_gettoken(&spechash_token);
1104 SLIST_INSERT_HEAD(&dev->si_hlist, vp, v_cdevnext);
1105 lwkt_reltoken(&spechash_token);
1110 v_release_rdev(struct vnode *vp)
1114 if ((dev = vp->v_rdev) != NULL) {
1115 lwkt_gettoken(&spechash_token);
1116 SLIST_REMOVE(&dev->si_hlist, vp, vnode, v_cdevnext);
1119 lwkt_reltoken(&spechash_token);
1124 * Add a vnode to the alias list hung off the cdev_t. We only associate
1125 * the device number with the vnode. The actual device is not associated
1126 * until the vnode is opened (usually in spec_open()), and will be
1127 * disassociated on last close.
1130 addaliasu(struct vnode *nvp, int x, int y)
1132 if (nvp->v_type != VBLK && nvp->v_type != VCHR)
1133 panic("addaliasu on non-special vnode");
1139 * Simple call that a filesystem can make to try to get rid of a
1140 * vnode. It will fail if anyone is referencing the vnode (including
1143 * The filesystem can check whether its in-memory inode structure still
1144 * references the vp on return.
1147 vclean_unlocked(struct vnode *vp)
1150 if (sysref_isactive(&vp->v_sysref) == 0)
1156 * Disassociate a vnode from its underlying filesystem.
1158 * The vnode must be VX locked and referenced. In all normal situations
1159 * there are no active references. If vclean_vxlocked() is called while
1160 * there are active references, the vnode is being ripped out and we have
1161 * to call VOP_CLOSE() as appropriate before we can reclaim it.
1164 vclean_vxlocked(struct vnode *vp, int flags)
1169 struct namecache *ncp;
1172 * If the vnode has already been reclaimed we have nothing to do.
1174 if (vp->v_flag & VRECLAIMED)
1176 vsetflags(vp, VRECLAIMED);
1178 if (verbose_reclaims) {
1179 if ((ncp = TAILQ_FIRST(&vp->v_namecache)) != NULL)
1180 kprintf("Debug: reclaim %p %s\n", vp, ncp->nc_name);
1184 * Scrap the vfs cache
1186 while (cache_inval_vp(vp, 0) != 0) {
1187 kprintf("Warning: vnode %p clean/cache_resolution "
1188 "race detected\n", vp);
1189 tsleep(vp, 0, "vclninv", 2);
1193 * Check to see if the vnode is in use. If so we have to reference it
1194 * before we clean it out so that its count cannot fall to zero and
1195 * generate a race against ourselves to recycle it.
1197 active = sysref_isactive(&vp->v_sysref);
1200 * Clean out any buffers associated with the vnode and destroy its
1201 * object, if it has one.
1203 vinvalbuf(vp, V_SAVE, 0, 0);
1206 * If purging an active vnode (typically during a forced unmount
1207 * or reboot), it must be closed and deactivated before being
1208 * reclaimed. This isn't really all that safe, but what can
1211 * Note that neither of these routines unlocks the vnode.
1213 if (active && (flags & DOCLOSE)) {
1214 while ((n = vp->v_opencount) != 0) {
1215 if (vp->v_writecount)
1216 VOP_CLOSE(vp, FWRITE|FNONBLOCK);
1218 VOP_CLOSE(vp, FNONBLOCK);
1219 if (vp->v_opencount == n) {
1220 kprintf("Warning: unable to force-close"
1228 * If the vnode has not been deactivated, deactivated it. Deactivation
1229 * can create new buffers and VM pages so we have to call vinvalbuf()
1230 * again to make sure they all get flushed.
1232 * This can occur if a file with a link count of 0 needs to be
1235 * If the vnode is already dead don't try to deactivate it.
1237 if ((vp->v_flag & VINACTIVE) == 0) {
1238 vsetflags(vp, VINACTIVE);
1241 vinvalbuf(vp, V_SAVE, 0, 0);
1245 * If the vnode has an object, destroy it.
1247 lwkt_gettoken(&vmobj_token);
1248 object = vp->v_object;
1249 if (object != NULL) {
1251 * Use vm_object_lock() rather than vm_object_hold to avoid
1252 * creating an extra (self-)hold on the object.
1254 vm_object_lock(object);
1255 KKASSERT(object == vp->v_object);
1256 if (object->ref_count == 0) {
1257 if ((object->flags & OBJ_DEAD) == 0)
1258 vm_object_terminate(object);
1260 vm_pager_deallocate(object);
1262 vclrflags(vp, VOBJBUF);
1263 vm_object_unlock(object);
1265 lwkt_reltoken(&vmobj_token);
1266 KKASSERT((vp->v_flag & VOBJBUF) == 0);
1269 * Reclaim the vnode if not already dead.
1271 if (vp->v_mount && VOP_RECLAIM(vp))
1272 panic("vclean: cannot reclaim");
1275 * Done with purge, notify sleepers of the grim news.
1277 vp->v_ops = &dead_vnode_vops_p;
1282 * If we are destroying an active vnode, reactivate it now that
1283 * we have reassociated it with deadfs. This prevents the system
1284 * from crashing on the vnode due to it being unexpectedly marked
1285 * as inactive or reclaimed.
1287 if (active && (flags & DOCLOSE)) {
1288 vclrflags(vp, VINACTIVE | VRECLAIMED);
1293 * Eliminate all activity associated with the requested vnode
1294 * and with all vnodes aliased to the requested vnode.
1296 * The vnode must be referenced but should not be locked.
1299 vrevoke(struct vnode *vp, struct ucred *cred)
1307 * If the vnode has a device association, scrap all vnodes associated
1308 * with the device. Don't let the device disappear on us while we
1309 * are scrapping the vnodes.
1311 * The passed vp will probably show up in the list, do not VX lock
1314 * Releasing the vnode's rdev here can mess up specfs's call to
1315 * device close, so don't do it. The vnode has been disassociated
1316 * and the device will be closed after the last ref on the related
1317 * fp goes away (if not still open by e.g. the kernel).
1319 if (vp->v_type != VCHR) {
1320 error = fdrevoke(vp, DTYPE_VNODE, cred);
1323 if ((dev = vp->v_rdev) == NULL) {
1327 lwkt_gettoken(&spechash_token);
1329 vqn = SLIST_FIRST(&dev->si_hlist);
1332 while ((vq = vqn) != NULL) {
1333 vqn = SLIST_NEXT(vqn, v_cdevnext);
1336 fdrevoke(vq, DTYPE_VNODE, cred);
1337 /*v_release_rdev(vq);*/
1340 lwkt_reltoken(&spechash_token);
1347 * This is called when the object underlying a vnode is being destroyed,
1348 * such as in a remove(). Try to recycle the vnode immediately if the
1349 * only active reference is our reference.
1351 * Directory vnodes in the namecache with children cannot be immediately
1352 * recycled because numerous VOP_N*() ops require them to be stable.
1354 * To avoid recursive recycling from VOP_INACTIVE implemenetations this
1355 * function is a NOP if VRECLAIMED is already set.
1358 vrecycle(struct vnode *vp)
1360 if (vp->v_sysref.refcnt <= 1 && (vp->v_flag & VRECLAIMED) == 0) {
1361 if (cache_inval_vp_nonblock(vp))
1370 * Return the maximum I/O size allowed for strategy calls on VP.
1372 * If vp is VCHR or VBLK we dive the device, otherwise we use
1373 * the vp's mount info.
1376 vmaxiosize(struct vnode *vp)
1378 if (vp->v_type == VBLK || vp->v_type == VCHR) {
1379 return(vp->v_rdev->si_iosize_max);
1381 return(vp->v_mount->mnt_iosize_max);
1386 * Eliminate all activity associated with a vnode in preparation for reuse.
1388 * The vnode must be VX locked and refd and will remain VX locked and refd
1389 * on return. This routine may be called with the vnode in any state, as
1390 * long as it is VX locked. The vnode will be cleaned out and marked
1391 * VRECLAIMED but will not actually be reused until all existing refs and
1394 * NOTE: This routine may be called on a vnode which has not yet been
1395 * already been deactivated (VOP_INACTIVE), or on a vnode which has
1396 * already been reclaimed.
1398 * This routine is not responsible for placing us back on the freelist.
1399 * Instead, it happens automatically when the caller releases the VX lock
1400 * (assuming there aren't any other references).
1403 vgone_vxlocked(struct vnode *vp)
1406 * assert that the VX lock is held. This is an absolute requirement
1407 * now for vgone_vxlocked() to be called.
1409 KKASSERT(vp->v_lock.lk_exclusivecount == 1);
1414 * Clean out the filesystem specific data and set the VRECLAIMED
1415 * bit. Also deactivate the vnode if necessary.
1417 vclean_vxlocked(vp, DOCLOSE);
1420 * Delete from old mount point vnode list, if on one.
1422 if (vp->v_mount != NULL) {
1423 KKASSERT(vp->v_data == NULL);
1424 insmntque(vp, NULL);
1428 * If special device, remove it from special device alias list
1429 * if it is on one. This should normally only occur if a vnode is
1430 * being revoked as the device should otherwise have been released
1433 if ((vp->v_type == VBLK || vp->v_type == VCHR) && vp->v_rdev != NULL) {
1445 * Lookup a vnode by device number.
1447 * Returns non-zero and *vpp set to a vref'd vnode on success.
1448 * Returns zero on failure.
1451 vfinddev(cdev_t dev, enum vtype type, struct vnode **vpp)
1455 lwkt_gettoken(&spechash_token);
1456 SLIST_FOREACH(vp, &dev->si_hlist, v_cdevnext) {
1457 if (type == vp->v_type) {
1460 lwkt_reltoken(&spechash_token);
1464 lwkt_reltoken(&spechash_token);
1469 * Calculate the total number of references to a special device. This
1470 * routine may only be called for VBLK and VCHR vnodes since v_rdev is
1471 * an overloaded field. Since udev2dev can now return NULL, we have
1472 * to check for a NULL v_rdev.
1475 count_dev(cdev_t dev)
1480 if (SLIST_FIRST(&dev->si_hlist)) {
1481 lwkt_gettoken(&spechash_token);
1482 SLIST_FOREACH(vp, &dev->si_hlist, v_cdevnext) {
1483 count += vp->v_opencount;
1485 lwkt_reltoken(&spechash_token);
1491 vcount(struct vnode *vp)
1493 if (vp->v_rdev == NULL)
1495 return(count_dev(vp->v_rdev));
1499 * Initialize VMIO for a vnode. This routine MUST be called before a
1500 * VFS can issue buffer cache ops on a vnode. It is typically called
1501 * when a vnode is initialized from its inode.
1504 vinitvmio(struct vnode *vp, off_t filesize, int blksize, int boff)
1509 lwkt_gettoken(&vmobj_token);
1511 if ((object = vp->v_object) == NULL) {
1512 object = vnode_pager_alloc(vp, filesize, 0, 0, blksize, boff);
1514 * Dereference the reference we just created. This assumes
1515 * that the object is associated with the vp.
1517 object->ref_count--;
1520 if (object->flags & OBJ_DEAD) {
1522 if (vp->v_object == object)
1523 vm_object_dead_sleep(object, "vodead");
1524 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1528 KASSERT(vp->v_object != NULL, ("vinitvmio: NULL object"));
1529 vsetflags(vp, VOBJBUF);
1530 lwkt_reltoken(&vmobj_token);
1537 * Print out a description of a vnode.
1539 static char *typename[] =
1540 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD"};
1543 vprint(char *label, struct vnode *vp)
1548 kprintf("%s: %p: ", label, (void *)vp);
1550 kprintf("%p: ", (void *)vp);
1551 kprintf("type %s, sysrefs %d, writecount %d, holdcnt %d,",
1552 typename[vp->v_type],
1553 vp->v_sysref.refcnt, vp->v_writecount, vp->v_auxrefs);
1555 if (vp->v_flag & VROOT)
1556 strcat(buf, "|VROOT");
1557 if (vp->v_flag & VPFSROOT)
1558 strcat(buf, "|VPFSROOT");
1559 if (vp->v_flag & VTEXT)
1560 strcat(buf, "|VTEXT");
1561 if (vp->v_flag & VSYSTEM)
1562 strcat(buf, "|VSYSTEM");
1563 if (vp->v_flag & VFREE)
1564 strcat(buf, "|VFREE");
1565 if (vp->v_flag & VOBJBUF)
1566 strcat(buf, "|VOBJBUF");
1568 kprintf(" flags (%s)", &buf[1]);
1569 if (vp->v_data == NULL) {
1578 * Do the usual access checking.
1579 * file_mode, uid and gid are from the vnode in question,
1580 * while acc_mode and cred are from the VOP_ACCESS parameter list
1583 vaccess(enum vtype type, mode_t file_mode, uid_t uid, gid_t gid,
1584 mode_t acc_mode, struct ucred *cred)
1590 * Super-user always gets read/write access, but execute access depends
1591 * on at least one execute bit being set.
1593 if (priv_check_cred(cred, PRIV_ROOT, 0) == 0) {
1594 if ((acc_mode & VEXEC) && type != VDIR &&
1595 (file_mode & (S_IXUSR|S_IXGRP|S_IXOTH)) == 0)
1602 /* Otherwise, check the owner. */
1603 if (cred->cr_uid == uid) {
1604 if (acc_mode & VEXEC)
1606 if (acc_mode & VREAD)
1608 if (acc_mode & VWRITE)
1610 return ((file_mode & mask) == mask ? 0 : EACCES);
1613 /* Otherwise, check the groups. */
1614 ismember = groupmember(gid, cred);
1615 if (cred->cr_svgid == gid || ismember) {
1616 if (acc_mode & VEXEC)
1618 if (acc_mode & VREAD)
1620 if (acc_mode & VWRITE)
1622 return ((file_mode & mask) == mask ? 0 : EACCES);
1625 /* Otherwise, check everyone else. */
1626 if (acc_mode & VEXEC)
1628 if (acc_mode & VREAD)
1630 if (acc_mode & VWRITE)
1632 return ((file_mode & mask) == mask ? 0 : EACCES);
1636 #include <ddb/ddb.h>
1638 static int db_show_locked_vnodes(struct mount *mp, void *data);
1641 * List all of the locked vnodes in the system.
1642 * Called when debugging the kernel.
1644 DB_SHOW_COMMAND(lockedvnodes, lockedvnodes)
1646 kprintf("Locked vnodes\n");
1647 mountlist_scan(db_show_locked_vnodes, NULL,
1648 MNTSCAN_FORWARD|MNTSCAN_NOBUSY);
1652 db_show_locked_vnodes(struct mount *mp, void *data __unused)
1656 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
1657 if (vn_islocked(vp))
1665 * Top level filesystem related information gathering.
1667 static int sysctl_ovfs_conf (SYSCTL_HANDLER_ARGS);
1670 vfs_sysctl(SYSCTL_HANDLER_ARGS)
1672 int *name = (int *)arg1 - 1; /* XXX */
1673 u_int namelen = arg2 + 1; /* XXX */
1674 struct vfsconf *vfsp;
1677 #if 1 || defined(COMPAT_PRELITE2)
1678 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
1680 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
1684 /* all sysctl names at this level are at least name and field */
1686 return (ENOTDIR); /* overloaded */
1687 if (name[0] != VFS_GENERIC) {
1688 vfsp = vfsconf_find_by_typenum(name[0]);
1690 return (EOPNOTSUPP);
1691 return ((*vfsp->vfc_vfsops->vfs_sysctl)(&name[1], namelen - 1,
1692 oldp, oldlenp, newp, newlen, p));
1696 case VFS_MAXTYPENUM:
1699 maxtypenum = vfsconf_get_maxtypenum();
1700 return (SYSCTL_OUT(req, &maxtypenum, sizeof(maxtypenum)));
1703 return (ENOTDIR); /* overloaded */
1704 vfsp = vfsconf_find_by_typenum(name[2]);
1706 return (EOPNOTSUPP);
1707 return (SYSCTL_OUT(req, vfsp, sizeof *vfsp));
1709 return (EOPNOTSUPP);
1712 SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD, vfs_sysctl,
1713 "Generic filesystem");
1715 #if 1 || defined(COMPAT_PRELITE2)
1718 sysctl_ovfs_conf_iter(struct vfsconf *vfsp, void *data)
1721 struct ovfsconf ovfs;
1722 struct sysctl_req *req = (struct sysctl_req*) data;
1724 bzero(&ovfs, sizeof(ovfs));
1725 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
1726 strcpy(ovfs.vfc_name, vfsp->vfc_name);
1727 ovfs.vfc_index = vfsp->vfc_typenum;
1728 ovfs.vfc_refcount = vfsp->vfc_refcount;
1729 ovfs.vfc_flags = vfsp->vfc_flags;
1730 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
1732 return error; /* abort iteration with error code */
1734 return 0; /* continue iterating with next element */
1738 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
1740 return vfsconf_each(sysctl_ovfs_conf_iter, (void*)req);
1743 #endif /* 1 || COMPAT_PRELITE2 */
1746 * Check to see if a filesystem is mounted on a block device.
1749 vfs_mountedon(struct vnode *vp)
1753 if ((dev = vp->v_rdev) == NULL) {
1754 /* if (vp->v_type != VBLK)
1755 dev = get_dev(vp->v_uminor, vp->v_umajor); */
1757 if (dev != NULL && dev->si_mountpoint)
1763 * Unmount all filesystems. The list is traversed in reverse order
1764 * of mounting to avoid dependencies.
1767 static int vfs_umountall_callback(struct mount *mp, void *data);
1770 vfs_unmountall(void)
1775 count = mountlist_scan(vfs_umountall_callback,
1776 NULL, MNTSCAN_REVERSE|MNTSCAN_NOBUSY);
1782 vfs_umountall_callback(struct mount *mp, void *data)
1786 error = dounmount(mp, MNT_FORCE);
1788 mountlist_remove(mp);
1789 kprintf("unmount of filesystem mounted from %s failed (",
1790 mp->mnt_stat.f_mntfromname);
1794 kprintf("%d)\n", error);
1800 * Checks the mount flags for parameter mp and put the names comma-separated
1801 * into a string buffer buf with a size limit specified by len.
1803 * It returns the number of bytes written into buf, and (*errorp) will be
1804 * set to 0, EINVAL (if passed length is 0), or ENOSPC (supplied buffer was
1805 * not large enough). The buffer will be 0-terminated if len was not 0.
1808 vfs_flagstostr(int flags, const struct mountctl_opt *optp,
1809 char *buf, size_t len, int *errorp)
1811 static const struct mountctl_opt optnames[] = {
1812 { MNT_ASYNC, "asynchronous" },
1813 { MNT_EXPORTED, "NFS exported" },
1814 { MNT_LOCAL, "local" },
1815 { MNT_NOATIME, "noatime" },
1816 { MNT_NODEV, "nodev" },
1817 { MNT_NOEXEC, "noexec" },
1818 { MNT_NOSUID, "nosuid" },
1819 { MNT_NOSYMFOLLOW, "nosymfollow" },
1820 { MNT_QUOTA, "with-quotas" },
1821 { MNT_RDONLY, "read-only" },
1822 { MNT_SYNCHRONOUS, "synchronous" },
1823 { MNT_UNION, "union" },
1824 { MNT_NOCLUSTERR, "noclusterr" },
1825 { MNT_NOCLUSTERW, "noclusterw" },
1826 { MNT_SUIDDIR, "suiddir" },
1827 { MNT_SOFTDEP, "soft-updates" },
1828 { MNT_IGNORE, "ignore" },
1838 bleft = len - 1; /* leave room for trailing \0 */
1841 * Checks the size of the string. If it contains
1842 * any data, then we will append the new flags to
1845 actsize = strlen(buf);
1849 /* Default flags if no flags passed */
1853 if (bleft < 0) { /* degenerate case, 0-length buffer */
1858 for (; flags && optp->o_opt; ++optp) {
1859 if ((flags & optp->o_opt) == 0)
1861 optlen = strlen(optp->o_name);
1862 if (bwritten || actsize > 0) {
1867 buf[bwritten++] = ',';
1868 buf[bwritten++] = ' ';
1871 if (bleft < optlen) {
1875 bcopy(optp->o_name, buf + bwritten, optlen);
1878 flags &= ~optp->o_opt;
1882 * Space already reserved for trailing \0
1889 * Build hash lists of net addresses and hang them off the mount point.
1890 * Called by ufs_mount() to set up the lists of export addresses.
1893 vfs_hang_addrlist(struct mount *mp, struct netexport *nep,
1894 const struct export_args *argp)
1897 struct radix_node_head *rnh;
1899 struct radix_node *rn;
1900 struct sockaddr *saddr, *smask = 0;
1904 if (argp->ex_addrlen == 0) {
1905 if (mp->mnt_flag & MNT_DEFEXPORTED)
1907 np = &nep->ne_defexported;
1908 np->netc_exflags = argp->ex_flags;
1909 np->netc_anon = argp->ex_anon;
1910 np->netc_anon.cr_ref = 1;
1911 mp->mnt_flag |= MNT_DEFEXPORTED;
1915 if (argp->ex_addrlen < 0 || argp->ex_addrlen > MLEN)
1917 if (argp->ex_masklen < 0 || argp->ex_masklen > MLEN)
1920 i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
1921 np = (struct netcred *) kmalloc(i, M_NETADDR, M_WAITOK | M_ZERO);
1922 saddr = (struct sockaddr *) (np + 1);
1923 if ((error = copyin(argp->ex_addr, (caddr_t) saddr, argp->ex_addrlen)))
1925 if (saddr->sa_len > argp->ex_addrlen)
1926 saddr->sa_len = argp->ex_addrlen;
1927 if (argp->ex_masklen) {
1928 smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen);
1929 error = copyin(argp->ex_mask, (caddr_t)smask, argp->ex_masklen);
1932 if (smask->sa_len > argp->ex_masklen)
1933 smask->sa_len = argp->ex_masklen;
1935 i = saddr->sa_family;
1936 if ((rnh = nep->ne_rtable[i]) == 0) {
1938 * Seems silly to initialize every AF when most are not used,
1939 * do so on demand here
1941 SLIST_FOREACH(dom, &domains, dom_next)
1942 if (dom->dom_family == i && dom->dom_rtattach) {
1943 dom->dom_rtattach((void **) &nep->ne_rtable[i],
1947 if ((rnh = nep->ne_rtable[i]) == 0) {
1952 rn = (*rnh->rnh_addaddr) ((char *) saddr, (char *) smask, rnh,
1954 if (rn == 0 || np != (struct netcred *) rn) { /* already exists */
1958 np->netc_exflags = argp->ex_flags;
1959 np->netc_anon = argp->ex_anon;
1960 np->netc_anon.cr_ref = 1;
1963 kfree(np, M_NETADDR);
1969 vfs_free_netcred(struct radix_node *rn, void *w)
1971 struct radix_node_head *rnh = (struct radix_node_head *) w;
1973 (*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, rnh);
1974 kfree((caddr_t) rn, M_NETADDR);
1979 * Free the net address hash lists that are hanging off the mount points.
1982 vfs_free_addrlist(struct netexport *nep)
1985 struct radix_node_head *rnh;
1987 for (i = 0; i <= AF_MAX; i++)
1988 if ((rnh = nep->ne_rtable[i])) {
1989 (*rnh->rnh_walktree) (rnh, vfs_free_netcred,
1991 kfree((caddr_t) rnh, M_RTABLE);
1992 nep->ne_rtable[i] = 0;
1997 vfs_export(struct mount *mp, struct netexport *nep,
1998 const struct export_args *argp)
2002 if (argp->ex_flags & MNT_DELEXPORT) {
2003 if (mp->mnt_flag & MNT_EXPUBLIC) {
2004 vfs_setpublicfs(NULL, NULL, NULL);
2005 mp->mnt_flag &= ~MNT_EXPUBLIC;
2007 vfs_free_addrlist(nep);
2008 mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
2010 if (argp->ex_flags & MNT_EXPORTED) {
2011 if (argp->ex_flags & MNT_EXPUBLIC) {
2012 if ((error = vfs_setpublicfs(mp, nep, argp)) != 0)
2014 mp->mnt_flag |= MNT_EXPUBLIC;
2016 if ((error = vfs_hang_addrlist(mp, nep, argp)))
2018 mp->mnt_flag |= MNT_EXPORTED;
2025 * Set the publicly exported filesystem (WebNFS). Currently, only
2026 * one public filesystem is possible in the spec (RFC 2054 and 2055)
2029 vfs_setpublicfs(struct mount *mp, struct netexport *nep,
2030 const struct export_args *argp)
2037 * mp == NULL -> invalidate the current info, the FS is
2038 * no longer exported. May be called from either vfs_export
2039 * or unmount, so check if it hasn't already been done.
2042 if (nfs_pub.np_valid) {
2043 nfs_pub.np_valid = 0;
2044 if (nfs_pub.np_index != NULL) {
2045 FREE(nfs_pub.np_index, M_TEMP);
2046 nfs_pub.np_index = NULL;
2053 * Only one allowed at a time.
2055 if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
2059 * Get real filehandle for root of exported FS.
2061 bzero((caddr_t)&nfs_pub.np_handle, sizeof(nfs_pub.np_handle));
2062 nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid;
2064 if ((error = VFS_ROOT(mp, &rvp)))
2067 if ((error = VFS_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid)))
2073 * If an indexfile was specified, pull it in.
2075 if (argp->ex_indexfile != NULL) {
2078 error = vn_get_namelen(rvp, &namelen);
2081 MALLOC(nfs_pub.np_index, char *, namelen, M_TEMP,
2083 error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
2087 * Check for illegal filenames.
2089 for (cp = nfs_pub.np_index; *cp; cp++) {
2097 FREE(nfs_pub.np_index, M_TEMP);
2102 nfs_pub.np_mount = mp;
2103 nfs_pub.np_valid = 1;
2108 vfs_export_lookup(struct mount *mp, struct netexport *nep,
2109 struct sockaddr *nam)
2112 struct radix_node_head *rnh;
2113 struct sockaddr *saddr;
2116 if (mp->mnt_flag & MNT_EXPORTED) {
2118 * Lookup in the export list first.
2122 rnh = nep->ne_rtable[saddr->sa_family];
2124 np = (struct netcred *)
2125 (*rnh->rnh_matchaddr)((char *)saddr,
2127 if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
2132 * If no address match, use the default if it exists.
2134 if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
2135 np = &nep->ne_defexported;
2141 * perform msync on all vnodes under a mount point. The mount point must
2142 * be locked. This code is also responsible for lazy-freeing unreferenced
2143 * vnodes whos VM objects no longer contain pages.
2145 * NOTE: MNT_WAIT still skips vnodes in the VXLOCK state.
2147 * NOTE: XXX VOP_PUTPAGES and friends requires that the vnode be locked,
2148 * but vnode_pager_putpages() doesn't lock the vnode. We have to do it
2149 * way up in this high level function.
2151 static int vfs_msync_scan1(struct mount *mp, struct vnode *vp, void *data);
2152 static int vfs_msync_scan2(struct mount *mp, struct vnode *vp, void *data);
2155 vfs_msync(struct mount *mp, int flags)
2160 * tmpfs sets this flag to prevent msync(), sync, and the
2161 * filesystem periodic syncer from trying to flush VM pages
2162 * to swap. Only pure memory pressure flushes tmpfs VM pages
2165 if (mp->mnt_kern_flag & MNTK_NOMSYNC)
2169 * Ok, scan the vnodes for work.
2171 vmsc_flags = VMSC_GETVP;
2172 if (flags != MNT_WAIT)
2173 vmsc_flags |= VMSC_NOWAIT;
2174 vmntvnodescan(mp, vmsc_flags, vfs_msync_scan1, vfs_msync_scan2,
2175 (void *)(intptr_t)flags);
2179 * scan1 is a fast pre-check. There could be hundreds of thousands of
2180 * vnodes, we cannot afford to do anything heavy weight until we have a
2181 * fairly good indication that there is work to do.
2185 vfs_msync_scan1(struct mount *mp, struct vnode *vp, void *data)
2187 int flags = (int)(intptr_t)data;
2189 if ((vp->v_flag & VRECLAIMED) == 0) {
2190 if (vshouldmsync(vp))
2191 return(0); /* call scan2 */
2192 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
2193 (vp->v_flag & VOBJDIRTY) &&
2194 (flags == MNT_WAIT || vn_islocked(vp) == 0)) {
2195 return(0); /* call scan2 */
2200 * do not call scan2, continue the loop
2206 * This callback is handed a locked vnode.
2210 vfs_msync_scan2(struct mount *mp, struct vnode *vp, void *data)
2213 int flags = (int)(intptr_t)data;
2215 if (vp->v_flag & VRECLAIMED)
2218 if ((mp->mnt_flag & MNT_RDONLY) == 0 && (vp->v_flag & VOBJDIRTY)) {
2219 if ((obj = vp->v_object) != NULL) {
2220 vm_object_page_clean(obj, 0, 0,
2221 flags == MNT_WAIT ? OBJPC_SYNC : OBJPC_NOSYNC);
2228 * Wake up anyone interested in vp because it is being revoked.
2231 vn_gone(struct vnode *vp)
2233 lwkt_gettoken(&vp->v_token);
2234 KNOTE(&vp->v_pollinfo.vpi_kqinfo.ki_note, NOTE_REVOKE);
2235 lwkt_reltoken(&vp->v_token);
2239 * extract the cdev_t from a VBLK or VCHR. The vnode must have been opened
2240 * (or v_rdev might be NULL).
2243 vn_todev(struct vnode *vp)
2245 if (vp->v_type != VBLK && vp->v_type != VCHR)
2247 KKASSERT(vp->v_rdev != NULL);
2248 return (vp->v_rdev);
2252 * Check if vnode represents a disk device. The vnode does not need to be
2258 vn_isdisk(struct vnode *vp, int *errp)
2262 if (vp->v_type != VCHR) {
2275 if (dev_is_good(dev) == 0) {
2280 if ((dev_dflags(dev) & D_DISK) == 0) {
2291 vn_get_namelen(struct vnode *vp, int *namelen)
2294 register_t retval[2];
2296 error = VOP_PATHCONF(vp, _PC_NAME_MAX, retval);
2299 *namelen = (int)retval[0];
2304 vop_write_dirent(int *error, struct uio *uio, ino_t d_ino, uint8_t d_type,
2305 uint16_t d_namlen, const char *d_name)
2310 len = _DIRENT_RECLEN(d_namlen);
2311 if (len > uio->uio_resid)
2314 dp = kmalloc(len, M_TEMP, M_WAITOK | M_ZERO);
2317 dp->d_namlen = d_namlen;
2318 dp->d_type = d_type;
2319 bcopy(d_name, dp->d_name, d_namlen);
2321 *error = uiomove((caddr_t)dp, len, uio);
2329 vn_mark_atime(struct vnode *vp, struct thread *td)
2331 struct proc *p = td->td_proc;
2332 struct ucred *cred = p ? p->p_ucred : proc0.p_ucred;
2334 if ((vp->v_mount->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0) {
2335 VOP_MARKATIME(vp, cred);