2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * @(#)vfs_subr.c 8.31 (Berkeley) 5/26/95
35 * $FreeBSD: src/sys/kern/vfs_subr.c,v 1.249.2.30 2003/04/04 20:35:57 tegge Exp $
39 * External virtual filesystem routines
43 #include <sys/param.h>
44 #include <sys/systm.h>
47 #include <sys/dirent.h>
48 #include <sys/domain.h>
49 #include <sys/eventhandler.h>
50 #include <sys/fcntl.h>
52 #include <sys/kernel.h>
53 #include <sys/kthread.h>
54 #include <sys/malloc.h>
56 #include <sys/mount.h>
59 #include <sys/reboot.h>
60 #include <sys/socket.h>
62 #include <sys/sysctl.h>
63 #include <sys/syslog.h>
64 #include <sys/unistd.h>
65 #include <sys/vmmeter.h>
66 #include <sys/vnode.h>
68 #include <machine/limits.h>
71 #include <vm/vm_object.h>
72 #include <vm/vm_extern.h>
73 #include <vm/vm_kern.h>
75 #include <vm/vm_map.h>
76 #include <vm/vm_page.h>
77 #include <vm/vm_pager.h>
78 #include <vm/vnode_pager.h>
79 #include <vm/vm_zone.h>
82 #include <sys/thread2.h>
83 #include <sys/sysref2.h>
84 #include <sys/mplock2.h>
86 static MALLOC_DEFINE(M_NETADDR, "Export Host", "Export host address structure");
89 SYSCTL_INT(_debug, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0,
90 "Number of vnodes allocated");
92 SYSCTL_INT(_debug, OID_AUTO, verbose_reclaims, CTLFLAG_RD, &verbose_reclaims, 0,
93 "Output filename of reclaimed vnode(s)");
95 enum vtype iftovt_tab[16] = {
96 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
97 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
100 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
101 S_IFSOCK, S_IFIFO, S_IFMT,
104 static int reassignbufcalls;
105 SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW, &reassignbufcalls,
106 0, "Number of times buffers have been reassigned to the proper list");
108 static int check_buf_overlap = 2; /* invasive check */
109 SYSCTL_INT(_vfs, OID_AUTO, check_buf_overlap, CTLFLAG_RW, &check_buf_overlap,
110 0, "Enable overlapping buffer checks");
112 int nfs_mount_type = -1;
113 static struct lwkt_token spechash_token;
114 struct nfs_public nfs_pub; /* publicly exported FS */
117 SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RW,
118 &desiredvnodes, 0, "Maximum number of vnodes");
120 static void vfs_free_addrlist (struct netexport *nep);
121 static int vfs_free_netcred (struct radix_node *rn, void *w);
122 static int vfs_hang_addrlist (struct mount *mp, struct netexport *nep,
123 const struct export_args *argp);
126 * Red black tree functions
128 static int rb_buf_compare(struct buf *b1, struct buf *b2);
129 RB_GENERATE2(buf_rb_tree, buf, b_rbnode, rb_buf_compare, off_t, b_loffset);
130 RB_GENERATE2(buf_rb_hash, buf, b_rbhash, rb_buf_compare, off_t, b_loffset);
133 rb_buf_compare(struct buf *b1, struct buf *b2)
135 if (b1->b_loffset < b2->b_loffset)
137 if (b1->b_loffset > b2->b_loffset)
143 * Initialize the vnode management data structures.
145 * Called from vfsinit()
154 * Desiredvnodes is kern.maxvnodes. We want to scale it
155 * according to available system memory but we may also have
156 * to limit it based on available KVM, which is capped on 32 bit
157 * systems, to ~80K vnodes or so.
159 * WARNING! For machines with 64-256M of ram we have to be sure
160 * that the default limit scales down well due to HAMMER
161 * taking up significantly more memory per-vnode vs UFS.
162 * We want around ~5800 on a 128M machine.
164 factor1 = 20 * (sizeof(struct vm_object) + sizeof(struct vnode));
165 factor2 = 25 * (sizeof(struct vm_object) + sizeof(struct vnode));
167 imin((int64_t)vmstats.v_page_count * PAGE_SIZE / factor1,
169 desiredvnodes = imax(desiredvnodes, maxproc * 8);
171 lwkt_token_init(&spechash_token, "spechash");
175 * Knob to control the precision of file timestamps:
177 * 0 = seconds only; nanoseconds zeroed.
178 * 1 = seconds and nanoseconds, accurate within 1/HZ.
179 * 2 = seconds and nanoseconds, truncated to microseconds.
180 * >=3 = seconds and nanoseconds, maximum precision.
182 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC };
184 static int timestamp_precision = TSP_SEC;
185 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW,
186 ×tamp_precision, 0, "Precision of file timestamps");
189 * Get a current timestamp.
194 vfs_timestamp(struct timespec *tsp)
198 switch (timestamp_precision) {
200 tsp->tv_sec = time_second;
208 TIMEVAL_TO_TIMESPEC(&tv, tsp);
218 * Set vnode attributes to VNOVAL
221 vattr_null(struct vattr *vap)
224 vap->va_size = VNOVAL;
225 vap->va_bytes = VNOVAL;
226 vap->va_mode = VNOVAL;
227 vap->va_nlink = VNOVAL;
228 vap->va_uid = VNOVAL;
229 vap->va_gid = VNOVAL;
230 vap->va_fsid = VNOVAL;
231 vap->va_fileid = VNOVAL;
232 vap->va_blocksize = VNOVAL;
233 vap->va_rmajor = VNOVAL;
234 vap->va_rminor = VNOVAL;
235 vap->va_atime.tv_sec = VNOVAL;
236 vap->va_atime.tv_nsec = VNOVAL;
237 vap->va_mtime.tv_sec = VNOVAL;
238 vap->va_mtime.tv_nsec = VNOVAL;
239 vap->va_ctime.tv_sec = VNOVAL;
240 vap->va_ctime.tv_nsec = VNOVAL;
241 vap->va_flags = VNOVAL;
242 vap->va_gen = VNOVAL;
244 /* va_*_uuid fields are only valid if related flags are set */
248 * Flush out and invalidate all buffers associated with a vnode.
252 static int vinvalbuf_bp(struct buf *bp, void *data);
254 struct vinvalbuf_bp_info {
263 vinvalbuf(struct vnode *vp, int flags, int slpflag, int slptimeo)
265 struct vinvalbuf_bp_info info;
269 lwkt_gettoken(&vp->v_token);
272 * If we are being asked to save, call fsync to ensure that the inode
275 if (flags & V_SAVE) {
276 error = bio_track_wait(&vp->v_track_write, slpflag, slptimeo);
279 if (!RB_EMPTY(&vp->v_rbdirty_tree)) {
280 if ((error = VOP_FSYNC(vp, MNT_WAIT, 0)) != 0)
284 * Dirty bufs may be left or generated via races
285 * in circumstances where vinvalbuf() is called on
286 * a vnode not undergoing reclamation. Only
287 * panic if we are trying to reclaim the vnode.
289 if ((vp->v_flag & VRECLAIMED) &&
290 (bio_track_active(&vp->v_track_write) ||
291 !RB_EMPTY(&vp->v_rbdirty_tree))) {
292 panic("vinvalbuf: dirty bufs");
297 info.slptimeo = slptimeo;
298 info.lkflags = LK_EXCLUSIVE | LK_SLEEPFAIL;
299 if (slpflag & PCATCH)
300 info.lkflags |= LK_PCATCH;
305 * Flush the buffer cache until nothing is left, wait for all I/O
306 * to complete. At least one pass is required. We might block
307 * in the pip code so we have to re-check. Order is important.
313 if (!RB_EMPTY(&vp->v_rbclean_tree)) {
315 error = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree,
316 NULL, vinvalbuf_bp, &info);
318 if (!RB_EMPTY(&vp->v_rbdirty_tree)) {
320 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
321 NULL, vinvalbuf_bp, &info);
325 * Wait for I/O completion.
327 bio_track_wait(&vp->v_track_write, 0, 0);
328 if ((object = vp->v_object) != NULL)
329 refcount_wait(&object->paging_in_progress, "vnvlbx");
330 } while (bio_track_active(&vp->v_track_write) ||
331 !RB_EMPTY(&vp->v_rbclean_tree) ||
332 !RB_EMPTY(&vp->v_rbdirty_tree));
335 * Destroy the copy in the VM cache, too.
337 if ((object = vp->v_object) != NULL) {
338 vm_object_page_remove(object, 0, 0,
339 (flags & V_SAVE) ? TRUE : FALSE);
342 if (!RB_EMPTY(&vp->v_rbdirty_tree) || !RB_EMPTY(&vp->v_rbclean_tree))
343 panic("vinvalbuf: flush failed");
344 if (!RB_EMPTY(&vp->v_rbhash_tree))
345 panic("vinvalbuf: flush failed, buffers still present");
348 lwkt_reltoken(&vp->v_token);
353 vinvalbuf_bp(struct buf *bp, void *data)
355 struct vinvalbuf_bp_info *info = data;
358 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
359 atomic_add_int(&bp->b_refs, 1);
360 error = BUF_TIMELOCK(bp, info->lkflags,
361 "vinvalbuf", info->slptimeo);
362 atomic_subtract_int(&bp->b_refs, 1);
371 KKASSERT(bp->b_vp == info->vp);
374 * Must check clean/dirty status after successfully locking as
377 if ((info->clean && (bp->b_flags & B_DELWRI)) ||
378 (info->clean == 0 && (bp->b_flags & B_DELWRI) == 0)) {
384 * NOTE: NO B_LOCKED CHECK. Also no buf_checkwrite()
385 * check. This code will write out the buffer, period.
388 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
389 (info->flags & V_SAVE)) {
391 } else if (info->flags & V_SAVE) {
393 * Cannot set B_NOCACHE on a clean buffer as this will
394 * destroy the VM backing store which might actually
395 * be dirty (and unsynchronized).
397 bp->b_flags |= (B_INVAL | B_RELBUF);
400 bp->b_flags |= (B_INVAL | B_NOCACHE | B_RELBUF);
407 * Truncate a file's buffer and pages to a specified length. This
408 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
411 * The vnode must be locked.
413 static int vtruncbuf_bp_trunc_cmp(struct buf *bp, void *data);
414 static int vtruncbuf_bp_trunc(struct buf *bp, void *data);
415 static int vtruncbuf_bp_metasync_cmp(struct buf *bp, void *data);
416 static int vtruncbuf_bp_metasync(struct buf *bp, void *data);
418 struct vtruncbuf_info {
425 vtruncbuf(struct vnode *vp, off_t length, int blksize)
427 struct vtruncbuf_info info;
428 const char *filename;
432 * Round up to the *next* block, then destroy the buffers in question.
433 * Since we are only removing some of the buffers we must rely on the
434 * scan count to determine whether a loop is necessary.
436 if ((count = (int)(length % blksize)) != 0)
437 info.truncloffset = length + (blksize - count);
439 info.truncloffset = length;
442 lwkt_gettoken(&vp->v_token);
445 count = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree,
446 vtruncbuf_bp_trunc_cmp,
447 vtruncbuf_bp_trunc, &info);
449 count += RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
450 vtruncbuf_bp_trunc_cmp,
451 vtruncbuf_bp_trunc, &info);
455 * For safety, fsync any remaining metadata if the file is not being
456 * truncated to 0. Since the metadata does not represent the entire
457 * dirty list we have to rely on the hit count to ensure that we get
462 count = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
463 vtruncbuf_bp_metasync_cmp,
464 vtruncbuf_bp_metasync, &info);
469 * Clean out any left over VM backing store.
471 * It is possible to have in-progress I/O from buffers that were
472 * not part of the truncation. This should not happen if we
473 * are truncating to 0-length.
475 vnode_pager_setsize(vp, length);
476 bio_track_wait(&vp->v_track_write, 0, 0);
481 spin_lock(&vp->v_spin);
482 filename = TAILQ_FIRST(&vp->v_namecache) ?
483 TAILQ_FIRST(&vp->v_namecache)->nc_name : "?";
484 spin_unlock(&vp->v_spin);
487 * Make sure no buffers were instantiated while we were trying
488 * to clean out the remaining VM pages. This could occur due
489 * to busy dirty VM pages being flushed out to disk.
493 count = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree,
494 vtruncbuf_bp_trunc_cmp,
495 vtruncbuf_bp_trunc, &info);
497 count += RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
498 vtruncbuf_bp_trunc_cmp,
499 vtruncbuf_bp_trunc, &info);
501 kprintf("Warning: vtruncbuf(): Had to re-clean %d "
502 "left over buffers in %s\n", count, filename);
506 lwkt_reltoken(&vp->v_token);
512 * The callback buffer is beyond the new file EOF and must be destroyed.
513 * Note that the compare function must conform to the RB_SCAN's requirements.
517 vtruncbuf_bp_trunc_cmp(struct buf *bp, void *data)
519 struct vtruncbuf_info *info = data;
521 if (bp->b_loffset >= info->truncloffset)
528 vtruncbuf_bp_trunc(struct buf *bp, void *data)
530 struct vtruncbuf_info *info = data;
533 * Do not try to use a buffer we cannot immediately lock, but sleep
534 * anyway to prevent a livelock. The code will loop until all buffers
537 * We must always revalidate the buffer after locking it to deal
540 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
541 atomic_add_int(&bp->b_refs, 1);
542 if (BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL) == 0)
544 atomic_subtract_int(&bp->b_refs, 1);
545 } else if ((info->clean && (bp->b_flags & B_DELWRI)) ||
546 (info->clean == 0 && (bp->b_flags & B_DELWRI) == 0) ||
547 bp->b_vp != info->vp ||
548 vtruncbuf_bp_trunc_cmp(bp, data)) {
552 bp->b_flags |= (B_INVAL | B_RELBUF | B_NOCACHE);
559 * Fsync all meta-data after truncating a file to be non-zero. Only metadata
560 * blocks (with a negative loffset) are scanned.
561 * Note that the compare function must conform to the RB_SCAN's requirements.
564 vtruncbuf_bp_metasync_cmp(struct buf *bp, void *data __unused)
566 if (bp->b_loffset < 0)
572 vtruncbuf_bp_metasync(struct buf *bp, void *data)
574 struct vtruncbuf_info *info = data;
576 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
577 atomic_add_int(&bp->b_refs, 1);
578 if (BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL) == 0)
580 atomic_subtract_int(&bp->b_refs, 1);
581 } else if ((bp->b_flags & B_DELWRI) == 0 ||
582 bp->b_vp != info->vp ||
583 vtruncbuf_bp_metasync_cmp(bp, data)) {
587 if (bp->b_vp == info->vp)
596 * vfsync - implements a multipass fsync on a file which understands
597 * dependancies and meta-data. The passed vnode must be locked. The
598 * waitfor argument may be MNT_WAIT or MNT_NOWAIT, or MNT_LAZY.
600 * When fsyncing data asynchronously just do one consolidated pass starting
601 * with the most negative block number. This may not get all the data due
604 * When fsyncing data synchronously do a data pass, then a metadata pass,
605 * then do additional data+metadata passes to try to get all the data out.
607 static int vfsync_wait_output(struct vnode *vp,
608 int (*waitoutput)(struct vnode *, struct thread *));
609 static int vfsync_dummy_cmp(struct buf *bp __unused, void *data __unused);
610 static int vfsync_data_only_cmp(struct buf *bp, void *data);
611 static int vfsync_meta_only_cmp(struct buf *bp, void *data);
612 static int vfsync_lazy_range_cmp(struct buf *bp, void *data);
613 static int vfsync_bp(struct buf *bp, void *data);
622 int (*checkdef)(struct buf *);
623 int (*cmpfunc)(struct buf *, void *);
627 vfsync(struct vnode *vp, int waitfor, int passes,
628 int (*checkdef)(struct buf *),
629 int (*waitoutput)(struct vnode *, struct thread *))
631 struct vfsync_info info;
634 bzero(&info, sizeof(info));
636 if ((info.checkdef = checkdef) == NULL)
639 lwkt_gettoken(&vp->v_token);
642 case MNT_LAZY | MNT_NOWAIT:
645 * Lazy (filesystem syncer typ) Asynchronous plus limit the
646 * number of data (not meta) pages we try to flush to 1MB.
647 * A non-zero return means that lazy limit was reached.
649 info.lazylimit = 1024 * 1024;
651 info.cmpfunc = vfsync_lazy_range_cmp;
652 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
653 vfsync_lazy_range_cmp, vfsync_bp, &info);
654 info.cmpfunc = vfsync_meta_only_cmp;
655 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
656 vfsync_meta_only_cmp, vfsync_bp, &info);
659 else if (!RB_EMPTY(&vp->v_rbdirty_tree))
660 vn_syncer_add(vp, 1);
665 * Asynchronous. Do a data-only pass and a meta-only pass.
668 info.cmpfunc = vfsync_data_only_cmp;
669 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_data_only_cmp,
671 info.cmpfunc = vfsync_meta_only_cmp;
672 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_meta_only_cmp,
678 * Synchronous. Do a data-only pass, then a meta-data+data
679 * pass, then additional integrated passes to try to get
680 * all the dependancies flushed.
682 info.cmpfunc = vfsync_data_only_cmp;
683 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_data_only_cmp,
685 error = vfsync_wait_output(vp, waitoutput);
687 info.skippedbufs = 0;
688 info.cmpfunc = vfsync_dummy_cmp;
689 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
691 error = vfsync_wait_output(vp, waitoutput);
692 if (info.skippedbufs) {
693 kprintf("Warning: vfsync skipped %d dirty "
694 "bufs in pass2!\n", info.skippedbufs);
697 while (error == 0 && passes > 0 &&
698 !RB_EMPTY(&vp->v_rbdirty_tree)
701 info.synchronous = 1;
704 info.cmpfunc = vfsync_dummy_cmp;
705 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
711 error = vfsync_wait_output(vp, waitoutput);
715 lwkt_reltoken(&vp->v_token);
720 vfsync_wait_output(struct vnode *vp,
721 int (*waitoutput)(struct vnode *, struct thread *))
725 error = bio_track_wait(&vp->v_track_write, 0, 0);
727 error = waitoutput(vp, curthread);
732 vfsync_dummy_cmp(struct buf *bp __unused, void *data __unused)
738 vfsync_data_only_cmp(struct buf *bp, void *data)
740 if (bp->b_loffset < 0)
746 vfsync_meta_only_cmp(struct buf *bp, void *data)
748 if (bp->b_loffset < 0)
754 vfsync_lazy_range_cmp(struct buf *bp, void *data)
756 struct vfsync_info *info = data;
758 if (bp->b_loffset < info->vp->v_lazyw)
764 vfsync_bp(struct buf *bp, void *data)
766 struct vfsync_info *info = data;
767 struct vnode *vp = info->vp;
771 * Ignore buffers that we cannot immediately lock.
773 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
779 * We must revalidate the buffer after locking.
781 if ((bp->b_flags & B_DELWRI) == 0 ||
782 bp->b_vp != info->vp ||
783 info->cmpfunc(bp, data)) {
789 * If syncdeps is not set we do not try to write buffers which have
792 if (!info->synchronous && info->syncdeps == 0 && info->checkdef(bp)) {
798 * B_NEEDCOMMIT (primarily used by NFS) is a state where the buffer
799 * has been written but an additional handshake with the device
800 * is required before we can dispose of the buffer. We have no idea
801 * how to do this so we have to skip these buffers.
803 if (bp->b_flags & B_NEEDCOMMIT) {
809 * Ask bioops if it is ok to sync. If not the VFS may have
810 * set B_LOCKED so we have to cycle the buffer.
812 if (LIST_FIRST(&bp->b_dep) != NULL && buf_checkwrite(bp)) {
818 if (info->synchronous) {
820 * Synchronous flushing. An error may be returned.
826 * Asynchronous flushing. A negative return value simply
827 * stops the scan and is not considered an error. We use
828 * this to support limited MNT_LAZY flushes.
830 vp->v_lazyw = bp->b_loffset;
832 info->lazycount += cluster_awrite(bp);
833 waitrunningbufspace();
835 if (info->lazylimit && info->lazycount >= info->lazylimit)
844 * Associate a buffer with a vnode.
849 bgetvp(struct vnode *vp, struct buf *bp, int testsize)
851 KASSERT(bp->b_vp == NULL, ("bgetvp: not free"));
852 KKASSERT((bp->b_flags & (B_HASHED|B_DELWRI|B_VNCLEAN|B_VNDIRTY)) == 0);
855 * Insert onto list for new vnode.
857 lwkt_gettoken(&vp->v_token);
859 if (buf_rb_hash_RB_INSERT(&vp->v_rbhash_tree, bp)) {
860 lwkt_reltoken(&vp->v_token);
865 * Diagnostics (mainly for HAMMER debugging). Check for
866 * overlapping buffers.
868 if (check_buf_overlap) {
870 bx = buf_rb_hash_RB_PREV(bp);
872 if (bx->b_loffset + bx->b_bufsize > bp->b_loffset) {
873 kprintf("bgetvp: overlapl %016jx/%d %016jx "
875 (intmax_t)bx->b_loffset,
877 (intmax_t)bp->b_loffset,
879 if (check_buf_overlap > 1)
880 panic("bgetvp - overlapping buffer");
883 bx = buf_rb_hash_RB_NEXT(bp);
885 if (bp->b_loffset + testsize > bx->b_loffset) {
886 kprintf("bgetvp: overlapr %016jx/%d %016jx "
888 (intmax_t)bp->b_loffset,
890 (intmax_t)bx->b_loffset,
892 if (check_buf_overlap > 1)
893 panic("bgetvp - overlapping buffer");
898 bp->b_flags |= B_HASHED;
899 bp->b_flags |= B_VNCLEAN;
900 if (buf_rb_tree_RB_INSERT(&vp->v_rbclean_tree, bp))
901 panic("reassignbuf: dup lblk/clean vp %p bp %p", vp, bp);
903 lwkt_reltoken(&vp->v_token);
908 * Disassociate a buffer from a vnode.
913 brelvp(struct buf *bp)
917 KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
920 * Delete from old vnode list, if on one.
923 lwkt_gettoken(&vp->v_token);
924 if (bp->b_flags & (B_VNDIRTY | B_VNCLEAN)) {
925 if (bp->b_flags & B_VNDIRTY)
926 buf_rb_tree_RB_REMOVE(&vp->v_rbdirty_tree, bp);
928 buf_rb_tree_RB_REMOVE(&vp->v_rbclean_tree, bp);
929 bp->b_flags &= ~(B_VNDIRTY | B_VNCLEAN);
931 if (bp->b_flags & B_HASHED) {
932 buf_rb_hash_RB_REMOVE(&vp->v_rbhash_tree, bp);
933 bp->b_flags &= ~B_HASHED;
937 * Only remove from synclist when no dirty buffers are left AND
938 * the VFS has not flagged the vnode's inode as being dirty.
940 if ((vp->v_flag & (VONWORKLST | VISDIRTY | VOBJDIRTY)) == VONWORKLST &&
941 RB_EMPTY(&vp->v_rbdirty_tree)) {
942 vn_syncer_remove(vp);
946 lwkt_reltoken(&vp->v_token);
952 * Reassign the buffer to the proper clean/dirty list based on B_DELWRI.
953 * This routine is called when the state of the B_DELWRI bit is changed.
955 * Must be called with vp->v_token held.
959 reassignbuf(struct buf *bp)
961 struct vnode *vp = bp->b_vp;
964 ASSERT_LWKT_TOKEN_HELD(&vp->v_token);
968 * B_PAGING flagged buffers cannot be reassigned because their vp
969 * is not fully linked in.
971 if (bp->b_flags & B_PAGING)
972 panic("cannot reassign paging buffer");
974 if (bp->b_flags & B_DELWRI) {
976 * Move to the dirty list, add the vnode to the worklist
978 if (bp->b_flags & B_VNCLEAN) {
979 buf_rb_tree_RB_REMOVE(&vp->v_rbclean_tree, bp);
980 bp->b_flags &= ~B_VNCLEAN;
982 if ((bp->b_flags & B_VNDIRTY) == 0) {
983 if (buf_rb_tree_RB_INSERT(&vp->v_rbdirty_tree, bp)) {
984 panic("reassignbuf: dup lblk vp %p bp %p",
987 bp->b_flags |= B_VNDIRTY;
989 if ((vp->v_flag & VONWORKLST) == 0) {
990 switch (vp->v_type) {
997 vp->v_rdev->si_mountpoint != NULL) {
1005 vn_syncer_add(vp, delay);
1009 * Move to the clean list, remove the vnode from the worklist
1010 * if no dirty blocks remain.
1012 if (bp->b_flags & B_VNDIRTY) {
1013 buf_rb_tree_RB_REMOVE(&vp->v_rbdirty_tree, bp);
1014 bp->b_flags &= ~B_VNDIRTY;
1016 if ((bp->b_flags & B_VNCLEAN) == 0) {
1017 if (buf_rb_tree_RB_INSERT(&vp->v_rbclean_tree, bp)) {
1018 panic("reassignbuf: dup lblk vp %p bp %p",
1021 bp->b_flags |= B_VNCLEAN;
1025 * Only remove from synclist when no dirty buffers are left
1026 * AND the VFS has not flagged the vnode's inode as being
1029 if ((vp->v_flag & (VONWORKLST | VISDIRTY | VOBJDIRTY)) ==
1031 RB_EMPTY(&vp->v_rbdirty_tree)) {
1032 vn_syncer_remove(vp);
1038 * Create a vnode for a block device. Used for mounting the root file
1041 * A vref()'d vnode is returned.
1043 extern struct vop_ops *devfs_vnode_dev_vops_p;
1045 bdevvp(cdev_t dev, struct vnode **vpp)
1055 error = getspecialvnode(VT_NON, NULL, &devfs_vnode_dev_vops_p,
1066 v_associate_rdev(vp, dev);
1067 vp->v_umajor = dev->si_umajor;
1068 vp->v_uminor = dev->si_uminor;
1075 v_associate_rdev(struct vnode *vp, cdev_t dev)
1079 if (dev_is_good(dev) == 0)
1081 KKASSERT(vp->v_rdev == NULL);
1082 vp->v_rdev = reference_dev(dev);
1083 lwkt_gettoken(&spechash_token);
1084 SLIST_INSERT_HEAD(&dev->si_hlist, vp, v_cdevnext);
1085 lwkt_reltoken(&spechash_token);
1090 v_release_rdev(struct vnode *vp)
1094 if ((dev = vp->v_rdev) != NULL) {
1095 lwkt_gettoken(&spechash_token);
1096 SLIST_REMOVE(&dev->si_hlist, vp, vnode, v_cdevnext);
1099 lwkt_reltoken(&spechash_token);
1104 * Add a vnode to the alias list hung off the cdev_t. We only associate
1105 * the device number with the vnode. The actual device is not associated
1106 * until the vnode is opened (usually in spec_open()), and will be
1107 * disassociated on last close.
1110 addaliasu(struct vnode *nvp, int x, int y)
1112 if (nvp->v_type != VBLK && nvp->v_type != VCHR)
1113 panic("addaliasu on non-special vnode");
1119 * Simple call that a filesystem can make to try to get rid of a
1120 * vnode. It will fail if anyone is referencing the vnode (including
1123 * The filesystem can check whether its in-memory inode structure still
1124 * references the vp on return.
1126 * May only be called if the vnode is in a known state (i.e. being prevented
1127 * from being deallocated by some other condition such as a vfs inode hold).
1130 vclean_unlocked(struct vnode *vp)
1133 if (VREFCNT(vp) <= 1)
1139 * Disassociate a vnode from its underlying filesystem.
1141 * The vnode must be VX locked and referenced. In all normal situations
1142 * there are no active references. If vclean_vxlocked() is called while
1143 * there are active references, the vnode is being ripped out and we have
1144 * to call VOP_CLOSE() as appropriate before we can reclaim it.
1147 vclean_vxlocked(struct vnode *vp, int flags)
1152 struct namecache *ncp;
1155 * If the vnode has already been reclaimed we have nothing to do.
1157 if (vp->v_flag & VRECLAIMED)
1161 * Set flag to interlock operation, flag finalization to ensure
1162 * that the vnode winds up on the inactive list, and set v_act to 0.
1164 vsetflags(vp, VRECLAIMED);
1165 atomic_set_int(&vp->v_refcnt, VREF_FINALIZE);
1168 if (verbose_reclaims) {
1169 if ((ncp = TAILQ_FIRST(&vp->v_namecache)) != NULL)
1170 kprintf("Debug: reclaim %p %s\n", vp, ncp->nc_name);
1174 * Scrap the vfs cache
1176 while (cache_inval_vp(vp, 0) != 0) {
1177 kprintf("Warning: vnode %p clean/cache_resolution "
1178 "race detected\n", vp);
1179 tsleep(vp, 0, "vclninv", 2);
1183 * Check to see if the vnode is in use. If so we have to reference it
1184 * before we clean it out so that its count cannot fall to zero and
1185 * generate a race against ourselves to recycle it.
1187 active = (VREFCNT(vp) > 0);
1190 * Clean out any buffers associated with the vnode and destroy its
1191 * object, if it has one.
1193 vinvalbuf(vp, V_SAVE, 0, 0);
1194 KKASSERT(lockcountnb(&vp->v_lock) == 1);
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, NULL);
1209 VOP_CLOSE(vp, FNONBLOCK, NULL);
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);
1234 KKASSERT(lockcountnb(&vp->v_lock) == 1);
1237 * If the vnode has an object, destroy it.
1239 while ((object = vp->v_object) != NULL) {
1240 vm_object_hold(object);
1241 if (object == vp->v_object)
1243 vm_object_drop(object);
1246 if (object != NULL) {
1247 if (object->ref_count == 0) {
1248 if ((object->flags & OBJ_DEAD) == 0)
1249 vm_object_terminate(object);
1250 vm_object_drop(object);
1251 vclrflags(vp, VOBJBUF);
1253 vm_pager_deallocate(object);
1254 vclrflags(vp, VOBJBUF);
1255 vm_object_drop(object);
1258 KKASSERT((vp->v_flag & VOBJBUF) == 0);
1261 * Reclaim the vnode if not already dead.
1263 if (vp->v_mount && VOP_RECLAIM(vp))
1264 panic("vclean: cannot reclaim");
1267 * Done with purge, notify sleepers of the grim news.
1269 vp->v_ops = &dead_vnode_vops_p;
1274 * If we are destroying an active vnode, reactivate it now that
1275 * we have reassociated it with deadfs. This prevents the system
1276 * from crashing on the vnode due to it being unexpectedly marked
1277 * as inactive or reclaimed.
1279 if (active && (flags & DOCLOSE)) {
1280 vclrflags(vp, VINACTIVE | VRECLAIMED);
1285 * Eliminate all activity associated with the requested vnode
1286 * and with all vnodes aliased to the requested vnode.
1288 * The vnode must be referenced but should not be locked.
1291 vrevoke(struct vnode *vp, struct ucred *cred)
1299 * If the vnode has a device association, scrap all vnodes associated
1300 * with the device. Don't let the device disappear on us while we
1301 * are scrapping the vnodes.
1303 * The passed vp will probably show up in the list, do not VX lock
1306 * Releasing the vnode's rdev here can mess up specfs's call to
1307 * device close, so don't do it. The vnode has been disassociated
1308 * and the device will be closed after the last ref on the related
1309 * fp goes away (if not still open by e.g. the kernel).
1311 if (vp->v_type != VCHR) {
1312 error = fdrevoke(vp, DTYPE_VNODE, cred);
1315 if ((dev = vp->v_rdev) == NULL) {
1319 lwkt_gettoken(&spechash_token);
1322 vqn = SLIST_FIRST(&dev->si_hlist);
1325 while ((vq = vqn) != NULL) {
1326 if (VREFCNT(vq) > 0) {
1328 fdrevoke(vq, DTYPE_VNODE, cred);
1329 /*v_release_rdev(vq);*/
1331 if (vq->v_rdev != dev) {
1336 vqn = SLIST_NEXT(vq, v_cdevnext);
1341 lwkt_reltoken(&spechash_token);
1348 * This is called when the object underlying a vnode is being destroyed,
1349 * such as in a remove(). Try to recycle the vnode immediately if the
1350 * only active reference is our reference.
1352 * Directory vnodes in the namecache with children cannot be immediately
1353 * recycled because numerous VOP_N*() ops require them to be stable.
1355 * To avoid recursive recycling from VOP_INACTIVE implemenetations this
1356 * function is a NOP if VRECLAIMED is already set.
1359 vrecycle(struct vnode *vp)
1361 if (VREFCNT(vp) <= 1 && (vp->v_flag & VRECLAIMED) == 0) {
1362 if (cache_inval_vp_nonblock(vp))
1371 * Return the maximum I/O size allowed for strategy calls on VP.
1373 * If vp is VCHR or VBLK we dive the device, otherwise we use
1374 * the vp's mount info.
1376 * The returned value is clamped at MAXPHYS as most callers cannot use
1377 * buffers larger than that size.
1380 vmaxiosize(struct vnode *vp)
1384 if (vp->v_type == VBLK || vp->v_type == VCHR)
1385 maxiosize = vp->v_rdev->si_iosize_max;
1387 maxiosize = vp->v_mount->mnt_iosize_max;
1389 if (maxiosize > MAXPHYS)
1390 maxiosize = MAXPHYS;
1395 * Eliminate all activity associated with a vnode in preparation for
1398 * The vnode must be VX locked and refd and will remain VX locked and refd
1399 * on return. This routine may be called with the vnode in any state, as
1400 * long as it is VX locked. The vnode will be cleaned out and marked
1401 * VRECLAIMED but will not actually be reused until all existing refs and
1404 * NOTE: This routine may be called on a vnode which has not yet been
1405 * already been deactivated (VOP_INACTIVE), or on a vnode which has
1406 * already been reclaimed.
1408 * This routine is not responsible for placing us back on the freelist.
1409 * Instead, it happens automatically when the caller releases the VX lock
1410 * (assuming there aren't any other references).
1413 vgone_vxlocked(struct vnode *vp)
1416 * assert that the VX lock is held. This is an absolute requirement
1417 * now for vgone_vxlocked() to be called.
1419 KKASSERT(lockcountnb(&vp->v_lock) == 1);
1422 * Clean out the filesystem specific data and set the VRECLAIMED
1423 * bit. Also deactivate the vnode if necessary.
1425 * The vnode should have automatically been removed from the syncer
1426 * list as syncer/dirty flags cleared during the cleaning.
1428 vclean_vxlocked(vp, DOCLOSE);
1429 KKASSERT((vp->v_flag & VONWORKLST) == 0);
1432 * Delete from old mount point vnode list, if on one.
1434 if (vp->v_mount != NULL) {
1435 KKASSERT(vp->v_data == NULL);
1436 insmntque(vp, NULL);
1440 * If special device, remove it from special device alias list
1441 * if it is on one. This should normally only occur if a vnode is
1442 * being revoked as the device should otherwise have been released
1445 if ((vp->v_type == VBLK || vp->v_type == VCHR) && vp->v_rdev != NULL) {
1456 * Lookup a vnode by device number.
1458 * Returns non-zero and *vpp set to a vref'd vnode on success.
1459 * Returns zero on failure.
1462 vfinddev(cdev_t dev, enum vtype type, struct vnode **vpp)
1466 lwkt_gettoken(&spechash_token);
1467 SLIST_FOREACH(vp, &dev->si_hlist, v_cdevnext) {
1468 if (type == vp->v_type) {
1471 lwkt_reltoken(&spechash_token);
1475 lwkt_reltoken(&spechash_token);
1480 * Calculate the total number of references to a special device. This
1481 * routine may only be called for VBLK and VCHR vnodes since v_rdev is
1482 * an overloaded field. Since udev2dev can now return NULL, we have
1483 * to check for a NULL v_rdev.
1486 count_dev(cdev_t dev)
1491 if (SLIST_FIRST(&dev->si_hlist)) {
1492 lwkt_gettoken(&spechash_token);
1493 SLIST_FOREACH(vp, &dev->si_hlist, v_cdevnext) {
1494 count += vp->v_opencount;
1496 lwkt_reltoken(&spechash_token);
1502 vcount(struct vnode *vp)
1504 if (vp->v_rdev == NULL)
1506 return(count_dev(vp->v_rdev));
1510 * Initialize VMIO for a vnode. This routine MUST be called before a
1511 * VFS can issue buffer cache ops on a vnode. It is typically called
1512 * when a vnode is initialized from its inode.
1515 vinitvmio(struct vnode *vp, off_t filesize, int blksize, int boff)
1520 object = vp->v_object;
1522 vm_object_hold(object);
1523 KKASSERT(vp->v_object == object);
1526 if (object == NULL) {
1527 object = vnode_pager_alloc(vp, filesize, 0, 0, blksize, boff);
1530 * Dereference the reference we just created. This assumes
1531 * that the object is associated with the vp. Allow it to
1532 * have zero refs. It cannot be destroyed as long as it
1533 * is associated with the vnode.
1535 vm_object_hold(object);
1536 atomic_add_int(&object->ref_count, -1);
1539 KKASSERT((object->flags & OBJ_DEAD) == 0);
1541 KASSERT(vp->v_object != NULL, ("vinitvmio: NULL object"));
1542 vsetflags(vp, VOBJBUF);
1543 vm_object_drop(object);
1550 * Print out a description of a vnode.
1552 static char *typename[] =
1553 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD"};
1556 vprint(char *label, struct vnode *vp)
1561 kprintf("%s: %p: ", label, (void *)vp);
1563 kprintf("%p: ", (void *)vp);
1564 kprintf("type %s, refcnt %08x, writecount %d, holdcnt %d,",
1565 typename[vp->v_type],
1566 vp->v_refcnt, vp->v_writecount, vp->v_auxrefs);
1568 if (vp->v_flag & VROOT)
1569 strcat(buf, "|VROOT");
1570 if (vp->v_flag & VPFSROOT)
1571 strcat(buf, "|VPFSROOT");
1572 if (vp->v_flag & VTEXT)
1573 strcat(buf, "|VTEXT");
1574 if (vp->v_flag & VSYSTEM)
1575 strcat(buf, "|VSYSTEM");
1576 if (vp->v_flag & VOBJBUF)
1577 strcat(buf, "|VOBJBUF");
1579 kprintf(" flags (%s)", &buf[1]);
1580 if (vp->v_data == NULL) {
1589 * Do the usual access checking.
1590 * file_mode, uid and gid are from the vnode in question,
1591 * while acc_mode and cred are from the VOP_ACCESS parameter list
1594 vaccess(enum vtype type, mode_t file_mode, uid_t uid, gid_t gid,
1595 mode_t acc_mode, struct ucred *cred)
1601 * Super-user always gets read/write access, but execute access depends
1602 * on at least one execute bit being set.
1604 if (priv_check_cred(cred, PRIV_ROOT, 0) == 0) {
1605 if ((acc_mode & VEXEC) && type != VDIR &&
1606 (file_mode & (S_IXUSR|S_IXGRP|S_IXOTH)) == 0)
1613 /* Otherwise, check the owner. */
1614 if (cred->cr_uid == uid) {
1615 if (acc_mode & VEXEC)
1617 if (acc_mode & VREAD)
1619 if (acc_mode & VWRITE)
1621 return ((file_mode & mask) == mask ? 0 : EACCES);
1624 /* Otherwise, check the groups. */
1625 ismember = groupmember(gid, cred);
1626 if (cred->cr_svgid == gid || ismember) {
1627 if (acc_mode & VEXEC)
1629 if (acc_mode & VREAD)
1631 if (acc_mode & VWRITE)
1633 return ((file_mode & mask) == mask ? 0 : EACCES);
1636 /* Otherwise, check everyone else. */
1637 if (acc_mode & VEXEC)
1639 if (acc_mode & VREAD)
1641 if (acc_mode & VWRITE)
1643 return ((file_mode & mask) == mask ? 0 : EACCES);
1647 #include <ddb/ddb.h>
1649 static int db_show_locked_vnodes(struct mount *mp, void *data);
1652 * List all of the locked vnodes in the system.
1653 * Called when debugging the kernel.
1655 DB_SHOW_COMMAND(lockedvnodes, lockedvnodes)
1657 kprintf("Locked vnodes\n");
1658 mountlist_scan(db_show_locked_vnodes, NULL,
1659 MNTSCAN_FORWARD|MNTSCAN_NOBUSY);
1663 db_show_locked_vnodes(struct mount *mp, void *data __unused)
1667 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
1668 if (vn_islocked(vp))
1676 * Top level filesystem related information gathering.
1678 static int sysctl_ovfs_conf (SYSCTL_HANDLER_ARGS);
1681 vfs_sysctl(SYSCTL_HANDLER_ARGS)
1683 int *name = (int *)arg1 - 1; /* XXX */
1684 u_int namelen = arg2 + 1; /* XXX */
1685 struct vfsconf *vfsp;
1688 #if 1 || defined(COMPAT_PRELITE2)
1689 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
1691 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
1695 /* all sysctl names at this level are at least name and field */
1697 return (ENOTDIR); /* overloaded */
1698 if (name[0] != VFS_GENERIC) {
1699 vfsp = vfsconf_find_by_typenum(name[0]);
1701 return (EOPNOTSUPP);
1702 return ((*vfsp->vfc_vfsops->vfs_sysctl)(&name[1], namelen - 1,
1703 oldp, oldlenp, newp, newlen, p));
1707 case VFS_MAXTYPENUM:
1710 maxtypenum = vfsconf_get_maxtypenum();
1711 return (SYSCTL_OUT(req, &maxtypenum, sizeof(maxtypenum)));
1714 return (ENOTDIR); /* overloaded */
1715 vfsp = vfsconf_find_by_typenum(name[2]);
1717 return (EOPNOTSUPP);
1718 return (SYSCTL_OUT(req, vfsp, sizeof *vfsp));
1720 return (EOPNOTSUPP);
1723 SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD, vfs_sysctl,
1724 "Generic filesystem");
1726 #if 1 || defined(COMPAT_PRELITE2)
1729 sysctl_ovfs_conf_iter(struct vfsconf *vfsp, void *data)
1732 struct ovfsconf ovfs;
1733 struct sysctl_req *req = (struct sysctl_req*) data;
1735 bzero(&ovfs, sizeof(ovfs));
1736 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
1737 strcpy(ovfs.vfc_name, vfsp->vfc_name);
1738 ovfs.vfc_index = vfsp->vfc_typenum;
1739 ovfs.vfc_refcount = vfsp->vfc_refcount;
1740 ovfs.vfc_flags = vfsp->vfc_flags;
1741 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
1743 return error; /* abort iteration with error code */
1745 return 0; /* continue iterating with next element */
1749 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
1751 return vfsconf_each(sysctl_ovfs_conf_iter, (void*)req);
1754 #endif /* 1 || COMPAT_PRELITE2 */
1757 * Check to see if a filesystem is mounted on a block device.
1760 vfs_mountedon(struct vnode *vp)
1764 if ((dev = vp->v_rdev) == NULL) {
1765 /* if (vp->v_type != VBLK)
1766 dev = get_dev(vp->v_uminor, vp->v_umajor); */
1768 if (dev != NULL && dev->si_mountpoint)
1774 * Unmount all filesystems. The list is traversed in reverse order
1775 * of mounting to avoid dependencies.
1778 static int vfs_umountall_callback(struct mount *mp, void *data);
1781 vfs_unmountall(void)
1786 count = mountlist_scan(vfs_umountall_callback,
1787 NULL, MNTSCAN_REVERSE|MNTSCAN_NOBUSY);
1793 vfs_umountall_callback(struct mount *mp, void *data)
1797 error = dounmount(mp, MNT_FORCE);
1799 mountlist_remove(mp);
1800 kprintf("unmount of filesystem mounted from %s failed (",
1801 mp->mnt_stat.f_mntfromname);
1805 kprintf("%d)\n", error);
1811 * Checks the mount flags for parameter mp and put the names comma-separated
1812 * into a string buffer buf with a size limit specified by len.
1814 * It returns the number of bytes written into buf, and (*errorp) will be
1815 * set to 0, EINVAL (if passed length is 0), or ENOSPC (supplied buffer was
1816 * not large enough). The buffer will be 0-terminated if len was not 0.
1819 vfs_flagstostr(int flags, const struct mountctl_opt *optp,
1820 char *buf, size_t len, int *errorp)
1822 static const struct mountctl_opt optnames[] = {
1823 { MNT_ASYNC, "asynchronous" },
1824 { MNT_EXPORTED, "NFS exported" },
1825 { MNT_LOCAL, "local" },
1826 { MNT_NOATIME, "noatime" },
1827 { MNT_NODEV, "nodev" },
1828 { MNT_NOEXEC, "noexec" },
1829 { MNT_NOSUID, "nosuid" },
1830 { MNT_NOSYMFOLLOW, "nosymfollow" },
1831 { MNT_QUOTA, "with-quotas" },
1832 { MNT_RDONLY, "read-only" },
1833 { MNT_SYNCHRONOUS, "synchronous" },
1834 { MNT_UNION, "union" },
1835 { MNT_NOCLUSTERR, "noclusterr" },
1836 { MNT_NOCLUSTERW, "noclusterw" },
1837 { MNT_SUIDDIR, "suiddir" },
1838 { MNT_SOFTDEP, "soft-updates" },
1839 { MNT_IGNORE, "ignore" },
1849 bleft = len - 1; /* leave room for trailing \0 */
1852 * Checks the size of the string. If it contains
1853 * any data, then we will append the new flags to
1856 actsize = strlen(buf);
1860 /* Default flags if no flags passed */
1864 if (bleft < 0) { /* degenerate case, 0-length buffer */
1869 for (; flags && optp->o_opt; ++optp) {
1870 if ((flags & optp->o_opt) == 0)
1872 optlen = strlen(optp->o_name);
1873 if (bwritten || actsize > 0) {
1878 buf[bwritten++] = ',';
1879 buf[bwritten++] = ' ';
1882 if (bleft < optlen) {
1886 bcopy(optp->o_name, buf + bwritten, optlen);
1889 flags &= ~optp->o_opt;
1893 * Space already reserved for trailing \0
1900 * Build hash lists of net addresses and hang them off the mount point.
1901 * Called by ufs_mount() to set up the lists of export addresses.
1904 vfs_hang_addrlist(struct mount *mp, struct netexport *nep,
1905 const struct export_args *argp)
1908 struct radix_node_head *rnh;
1910 struct radix_node *rn;
1911 struct sockaddr *saddr, *smask = NULL;
1915 if (argp->ex_addrlen == 0) {
1916 if (mp->mnt_flag & MNT_DEFEXPORTED)
1918 np = &nep->ne_defexported;
1919 np->netc_exflags = argp->ex_flags;
1920 np->netc_anon = argp->ex_anon;
1921 np->netc_anon.cr_ref = 1;
1922 mp->mnt_flag |= MNT_DEFEXPORTED;
1926 if (argp->ex_addrlen < 0 || argp->ex_addrlen > MLEN)
1928 if (argp->ex_masklen < 0 || argp->ex_masklen > MLEN)
1931 i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
1932 np = (struct netcred *) kmalloc(i, M_NETADDR, M_WAITOK | M_ZERO);
1933 saddr = (struct sockaddr *) (np + 1);
1934 if ((error = copyin(argp->ex_addr, (caddr_t) saddr, argp->ex_addrlen)))
1936 if (saddr->sa_len > argp->ex_addrlen)
1937 saddr->sa_len = argp->ex_addrlen;
1938 if (argp->ex_masklen) {
1939 smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen);
1940 error = copyin(argp->ex_mask, (caddr_t)smask, argp->ex_masklen);
1943 if (smask->sa_len > argp->ex_masklen)
1944 smask->sa_len = argp->ex_masklen;
1946 i = saddr->sa_family;
1947 if ((rnh = nep->ne_rtable[i]) == NULL) {
1949 * Seems silly to initialize every AF when most are not used,
1950 * do so on demand here
1952 SLIST_FOREACH(dom, &domains, dom_next)
1953 if (dom->dom_family == i && dom->dom_rtattach) {
1954 dom->dom_rtattach((void **) &nep->ne_rtable[i],
1958 if ((rnh = nep->ne_rtable[i]) == NULL) {
1963 rn = (*rnh->rnh_addaddr) ((char *) saddr, (char *) smask, rnh,
1965 if (rn == NULL || np != (struct netcred *) rn) { /* already exists */
1969 np->netc_exflags = argp->ex_flags;
1970 np->netc_anon = argp->ex_anon;
1971 np->netc_anon.cr_ref = 1;
1974 kfree(np, M_NETADDR);
1980 vfs_free_netcred(struct radix_node *rn, void *w)
1982 struct radix_node_head *rnh = (struct radix_node_head *) w;
1984 (*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, rnh);
1985 kfree((caddr_t) rn, M_NETADDR);
1990 * Free the net address hash lists that are hanging off the mount points.
1993 vfs_free_addrlist(struct netexport *nep)
1996 struct radix_node_head *rnh;
1998 for (i = 0; i <= AF_MAX; i++)
1999 if ((rnh = nep->ne_rtable[i])) {
2000 (*rnh->rnh_walktree) (rnh, vfs_free_netcred,
2002 kfree((caddr_t) rnh, M_RTABLE);
2003 nep->ne_rtable[i] = 0;
2008 vfs_export(struct mount *mp, struct netexport *nep,
2009 const struct export_args *argp)
2013 if (argp->ex_flags & MNT_DELEXPORT) {
2014 if (mp->mnt_flag & MNT_EXPUBLIC) {
2015 vfs_setpublicfs(NULL, NULL, NULL);
2016 mp->mnt_flag &= ~MNT_EXPUBLIC;
2018 vfs_free_addrlist(nep);
2019 mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
2021 if (argp->ex_flags & MNT_EXPORTED) {
2022 if (argp->ex_flags & MNT_EXPUBLIC) {
2023 if ((error = vfs_setpublicfs(mp, nep, argp)) != 0)
2025 mp->mnt_flag |= MNT_EXPUBLIC;
2027 if ((error = vfs_hang_addrlist(mp, nep, argp)))
2029 mp->mnt_flag |= MNT_EXPORTED;
2036 * Set the publicly exported filesystem (WebNFS). Currently, only
2037 * one public filesystem is possible in the spec (RFC 2054 and 2055)
2040 vfs_setpublicfs(struct mount *mp, struct netexport *nep,
2041 const struct export_args *argp)
2048 * mp == NULL -> invalidate the current info, the FS is
2049 * no longer exported. May be called from either vfs_export
2050 * or unmount, so check if it hasn't already been done.
2053 if (nfs_pub.np_valid) {
2054 nfs_pub.np_valid = 0;
2055 if (nfs_pub.np_index != NULL) {
2056 kfree(nfs_pub.np_index, M_TEMP);
2057 nfs_pub.np_index = NULL;
2064 * Only one allowed at a time.
2066 if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
2070 * Get real filehandle for root of exported FS.
2072 bzero((caddr_t)&nfs_pub.np_handle, sizeof(nfs_pub.np_handle));
2073 nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid;
2075 if ((error = VFS_ROOT(mp, &rvp)))
2078 if ((error = VFS_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid)))
2084 * If an indexfile was specified, pull it in.
2086 if (argp->ex_indexfile != NULL) {
2089 error = vn_get_namelen(rvp, &namelen);
2092 nfs_pub.np_index = kmalloc(namelen, M_TEMP, M_WAITOK);
2093 error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
2097 * Check for illegal filenames.
2099 for (cp = nfs_pub.np_index; *cp; cp++) {
2107 kfree(nfs_pub.np_index, M_TEMP);
2112 nfs_pub.np_mount = mp;
2113 nfs_pub.np_valid = 1;
2118 vfs_export_lookup(struct mount *mp, struct netexport *nep,
2119 struct sockaddr *nam)
2122 struct radix_node_head *rnh;
2123 struct sockaddr *saddr;
2126 if (mp->mnt_flag & MNT_EXPORTED) {
2128 * Lookup in the export list first.
2132 rnh = nep->ne_rtable[saddr->sa_family];
2134 np = (struct netcred *)
2135 (*rnh->rnh_matchaddr)((char *)saddr,
2137 if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
2142 * If no address match, use the default if it exists.
2144 if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
2145 np = &nep->ne_defexported;
2151 * perform msync on all vnodes under a mount point. The mount point must
2152 * be locked. This code is also responsible for lazy-freeing unreferenced
2153 * vnodes whos VM objects no longer contain pages.
2155 * NOTE: MNT_WAIT still skips vnodes in the VXLOCK state.
2157 * NOTE: XXX VOP_PUTPAGES and friends requires that the vnode be locked,
2158 * but vnode_pager_putpages() doesn't lock the vnode. We have to do it
2159 * way up in this high level function.
2161 static int vfs_msync_scan1(struct mount *mp, struct vnode *vp, void *data);
2162 static int vfs_msync_scan2(struct mount *mp, struct vnode *vp, void *data);
2165 vfs_msync(struct mount *mp, int flags)
2170 * tmpfs sets this flag to prevent msync(), sync, and the
2171 * filesystem periodic syncer from trying to flush VM pages
2172 * to swap. Only pure memory pressure flushes tmpfs VM pages
2175 if (mp->mnt_kern_flag & MNTK_NOMSYNC)
2179 * Ok, scan the vnodes for work. If the filesystem is using the
2180 * syncer thread feature we can use vsyncscan() instead of
2181 * vmntvnodescan(), which is much faster.
2183 vmsc_flags = VMSC_GETVP;
2184 if (flags != MNT_WAIT)
2185 vmsc_flags |= VMSC_NOWAIT;
2187 if (mp->mnt_kern_flag & MNTK_THR_SYNC) {
2188 vsyncscan(mp, vmsc_flags, vfs_msync_scan2,
2189 (void *)(intptr_t)flags);
2191 vmntvnodescan(mp, vmsc_flags,
2192 vfs_msync_scan1, vfs_msync_scan2,
2193 (void *)(intptr_t)flags);
2198 * scan1 is a fast pre-check. There could be hundreds of thousands of
2199 * vnodes, we cannot afford to do anything heavy weight until we have a
2200 * fairly good indication that there is work to do.
2204 vfs_msync_scan1(struct mount *mp, struct vnode *vp, void *data)
2206 int flags = (int)(intptr_t)data;
2208 if ((vp->v_flag & VRECLAIMED) == 0) {
2209 if (vp->v_auxrefs == 0 && VREFCNT(vp) <= 0 &&
2211 return(0); /* call scan2 */
2213 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
2214 (vp->v_flag & VOBJDIRTY) &&
2215 (flags == MNT_WAIT || vn_islocked(vp) == 0)) {
2216 return(0); /* call scan2 */
2221 * do not call scan2, continue the loop
2227 * This callback is handed a locked vnode.
2231 vfs_msync_scan2(struct mount *mp, struct vnode *vp, void *data)
2234 int flags = (int)(intptr_t)data;
2236 if (vp->v_flag & VRECLAIMED)
2239 if ((mp->mnt_flag & MNT_RDONLY) == 0 && (vp->v_flag & VOBJDIRTY)) {
2240 if ((obj = vp->v_object) != NULL) {
2241 vm_object_page_clean(obj, 0, 0,
2242 flags == MNT_WAIT ? OBJPC_SYNC : OBJPC_NOSYNC);
2249 * Wake up anyone interested in vp because it is being revoked.
2252 vn_gone(struct vnode *vp)
2254 lwkt_gettoken(&vp->v_token);
2255 KNOTE(&vp->v_pollinfo.vpi_kqinfo.ki_note, NOTE_REVOKE);
2256 lwkt_reltoken(&vp->v_token);
2260 * extract the cdev_t from a VBLK or VCHR. The vnode must have been opened
2261 * (or v_rdev might be NULL).
2264 vn_todev(struct vnode *vp)
2266 if (vp->v_type != VBLK && vp->v_type != VCHR)
2268 KKASSERT(vp->v_rdev != NULL);
2269 return (vp->v_rdev);
2273 * Check if vnode represents a disk device. The vnode does not need to be
2279 vn_isdisk(struct vnode *vp, int *errp)
2283 if (vp->v_type != VCHR) {
2296 if (dev_is_good(dev) == 0) {
2301 if ((dev_dflags(dev) & D_DISK) == 0) {
2312 vn_get_namelen(struct vnode *vp, int *namelen)
2315 register_t retval[2];
2317 error = VOP_PATHCONF(vp, _PC_NAME_MAX, retval);
2320 *namelen = (int)retval[0];
2325 vop_write_dirent(int *error, struct uio *uio, ino_t d_ino, uint8_t d_type,
2326 uint16_t d_namlen, const char *d_name)
2331 len = _DIRENT_RECLEN(d_namlen);
2332 if (len > uio->uio_resid)
2335 dp = kmalloc(len, M_TEMP, M_WAITOK | M_ZERO);
2338 dp->d_namlen = d_namlen;
2339 dp->d_type = d_type;
2340 bcopy(d_name, dp->d_name, d_namlen);
2342 *error = uiomove((caddr_t)dp, len, uio);
2350 vn_mark_atime(struct vnode *vp, struct thread *td)
2352 struct proc *p = td->td_proc;
2353 struct ucred *cred = p ? p->p_ucred : proc0.p_ucred;
2355 if ((vp->v_mount->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0) {
2356 VOP_MARKATIME(vp, cred);