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 * 4. 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 * Returns non-zero if the vnode is a candidate for lazy msyncing.
145 * NOTE: v_object is not stable (this scan can race), however the
146 * mntvnodescan code holds vmobj_token so any VM object we
147 * do find will remain stable storage.
150 vshouldmsync(struct vnode
*vp
)
154 if (vp
->v_auxrefs
!= 0 || vp
->v_sysref
.refcnt
> 0)
155 return (0); /* other holders */
156 object
= vp
->v_object
;
158 if (object
&& (object
->ref_count
|| object
->resident_page_count
))
164 * Initialize the vnode management data structures.
166 * Called from vfsinit()
175 * Desiredvnodes is kern.maxvnodes. We want to scale it
176 * according to available system memory but we may also have
177 * to limit it based on available KVM, which is capped on 32 bit
178 * systems, to ~80K vnodes or so.
180 * WARNING! For machines with 64-256M of ram we have to be sure
181 * that the default limit scales down well due to HAMMER
182 * taking up significantly more memory per-vnode vs UFS.
183 * We want around ~5800 on a 128M machine.
185 factor1
= 20 * (sizeof(struct vm_object
) + sizeof(struct vnode
));
186 factor2
= 25 * (sizeof(struct vm_object
) + sizeof(struct vnode
));
188 imin((int64_t)vmstats
.v_page_count
* PAGE_SIZE
/ factor1
,
190 desiredvnodes
= imax(desiredvnodes
, maxproc
* 8);
192 lwkt_token_init(&spechash_token
, "spechash");
196 * Knob to control the precision of file timestamps:
198 * 0 = seconds only; nanoseconds zeroed.
199 * 1 = seconds and nanoseconds, accurate within 1/HZ.
200 * 2 = seconds and nanoseconds, truncated to microseconds.
201 * >=3 = seconds and nanoseconds, maximum precision.
203 enum { TSP_SEC
, TSP_HZ
, TSP_USEC
, TSP_NSEC
};
205 static int timestamp_precision
= TSP_SEC
;
206 SYSCTL_INT(_vfs
, OID_AUTO
, timestamp_precision
, CTLFLAG_RW
,
207 ×tamp_precision
, 0, "Precision of file timestamps");
210 * Get a current timestamp.
215 vfs_timestamp(struct timespec
*tsp
)
219 switch (timestamp_precision
) {
221 tsp
->tv_sec
= time_second
;
229 TIMEVAL_TO_TIMESPEC(&tv
, tsp
);
239 * Set vnode attributes to VNOVAL
242 vattr_null(struct vattr
*vap
)
245 vap
->va_size
= VNOVAL
;
246 vap
->va_bytes
= VNOVAL
;
247 vap
->va_mode
= VNOVAL
;
248 vap
->va_nlink
= VNOVAL
;
249 vap
->va_uid
= VNOVAL
;
250 vap
->va_gid
= VNOVAL
;
251 vap
->va_fsid
= VNOVAL
;
252 vap
->va_fileid
= VNOVAL
;
253 vap
->va_blocksize
= VNOVAL
;
254 vap
->va_rmajor
= VNOVAL
;
255 vap
->va_rminor
= VNOVAL
;
256 vap
->va_atime
.tv_sec
= VNOVAL
;
257 vap
->va_atime
.tv_nsec
= VNOVAL
;
258 vap
->va_mtime
.tv_sec
= VNOVAL
;
259 vap
->va_mtime
.tv_nsec
= VNOVAL
;
260 vap
->va_ctime
.tv_sec
= VNOVAL
;
261 vap
->va_ctime
.tv_nsec
= VNOVAL
;
262 vap
->va_flags
= VNOVAL
;
263 vap
->va_gen
= VNOVAL
;
265 /* va_*_uuid fields are only valid if related flags are set */
269 * Flush out and invalidate all buffers associated with a vnode.
273 static int vinvalbuf_bp(struct buf
*bp
, void *data
);
275 struct vinvalbuf_bp_info
{
284 vinvalbuf(struct vnode
*vp
, int flags
, int slpflag
, int slptimeo
)
286 struct vinvalbuf_bp_info info
;
290 lwkt_gettoken(&vp
->v_token
);
293 * If we are being asked to save, call fsync to ensure that the inode
296 if (flags
& V_SAVE
) {
297 error
= bio_track_wait(&vp
->v_track_write
, slpflag
, slptimeo
);
300 if (!RB_EMPTY(&vp
->v_rbdirty_tree
)) {
301 if ((error
= VOP_FSYNC(vp
, MNT_WAIT
, 0)) != 0)
305 * Dirty bufs may be left or generated via races
306 * in circumstances where vinvalbuf() is called on
307 * a vnode not undergoing reclamation. Only
308 * panic if we are trying to reclaim the vnode.
310 if ((vp
->v_flag
& VRECLAIMED
) &&
311 (bio_track_active(&vp
->v_track_write
) ||
312 !RB_EMPTY(&vp
->v_rbdirty_tree
))) {
313 panic("vinvalbuf: dirty bufs");
318 info
.slptimeo
= slptimeo
;
319 info
.lkflags
= LK_EXCLUSIVE
| LK_SLEEPFAIL
;
320 if (slpflag
& PCATCH
)
321 info
.lkflags
|= LK_PCATCH
;
326 * Flush the buffer cache until nothing is left, wait for all I/O
327 * to complete. At least one pass is required. We might block
328 * in the pip code so we have to re-check. Order is important.
334 if (!RB_EMPTY(&vp
->v_rbclean_tree
)) {
336 error
= RB_SCAN(buf_rb_tree
, &vp
->v_rbclean_tree
,
337 NULL
, vinvalbuf_bp
, &info
);
339 if (!RB_EMPTY(&vp
->v_rbdirty_tree
)) {
341 error
= RB_SCAN(buf_rb_tree
, &vp
->v_rbdirty_tree
,
342 NULL
, vinvalbuf_bp
, &info
);
346 * Wait for I/O completion.
348 bio_track_wait(&vp
->v_track_write
, 0, 0);
349 if ((object
= vp
->v_object
) != NULL
)
350 refcount_wait(&object
->paging_in_progress
, "vnvlbx");
351 } while (bio_track_active(&vp
->v_track_write
) ||
352 !RB_EMPTY(&vp
->v_rbclean_tree
) ||
353 !RB_EMPTY(&vp
->v_rbdirty_tree
));
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: NO B_LOCKED CHECK. Also no buf_checkwrite()
406 * check. This code will write out the buffer, period.
409 if (((bp
->b_flags
& (B_DELWRI
| B_INVAL
)) == B_DELWRI
) &&
410 (info
->flags
& V_SAVE
)) {
412 } else if (info
->flags
& V_SAVE
) {
414 * Cannot set B_NOCACHE on a clean buffer as this will
415 * destroy the VM backing store which might actually
416 * be dirty (and unsynchronized).
418 bp
->b_flags
|= (B_INVAL
| B_RELBUF
);
421 bp
->b_flags
|= (B_INVAL
| B_NOCACHE
| B_RELBUF
);
428 * Truncate a file's buffer and pages to a specified length. This
429 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
432 * The vnode must be locked.
434 static int vtruncbuf_bp_trunc_cmp(struct buf
*bp
, void *data
);
435 static int vtruncbuf_bp_trunc(struct buf
*bp
, void *data
);
436 static int vtruncbuf_bp_metasync_cmp(struct buf
*bp
, void *data
);
437 static int vtruncbuf_bp_metasync(struct buf
*bp
, void *data
);
439 struct vtruncbuf_info
{
446 vtruncbuf(struct vnode
*vp
, off_t length
, int blksize
)
448 struct vtruncbuf_info info
;
449 const char *filename
;
453 * Round up to the *next* block, then destroy the buffers in question.
454 * Since we are only removing some of the buffers we must rely on the
455 * scan count to determine whether a loop is necessary.
457 if ((count
= (int)(length
% blksize
)) != 0)
458 info
.truncloffset
= length
+ (blksize
- count
);
460 info
.truncloffset
= length
;
463 lwkt_gettoken(&vp
->v_token
);
466 count
= RB_SCAN(buf_rb_tree
, &vp
->v_rbclean_tree
,
467 vtruncbuf_bp_trunc_cmp
,
468 vtruncbuf_bp_trunc
, &info
);
470 count
+= RB_SCAN(buf_rb_tree
, &vp
->v_rbdirty_tree
,
471 vtruncbuf_bp_trunc_cmp
,
472 vtruncbuf_bp_trunc
, &info
);
476 * For safety, fsync any remaining metadata if the file is not being
477 * truncated to 0. Since the metadata does not represent the entire
478 * dirty list we have to rely on the hit count to ensure that we get
483 count
= RB_SCAN(buf_rb_tree
, &vp
->v_rbdirty_tree
,
484 vtruncbuf_bp_metasync_cmp
,
485 vtruncbuf_bp_metasync
, &info
);
490 * Clean out any left over VM backing store.
492 * It is possible to have in-progress I/O from buffers that were
493 * not part of the truncation. This should not happen if we
494 * are truncating to 0-length.
496 vnode_pager_setsize(vp
, length
);
497 bio_track_wait(&vp
->v_track_write
, 0, 0);
502 spin_lock(&vp
->v_spin
);
503 filename
= TAILQ_FIRST(&vp
->v_namecache
) ?
504 TAILQ_FIRST(&vp
->v_namecache
)->nc_name
: "?";
505 spin_unlock(&vp
->v_spin
);
508 * Make sure no buffers were instantiated while we were trying
509 * to clean out the remaining VM pages. This could occur due
510 * to busy dirty VM pages being flushed out to disk.
514 count
= RB_SCAN(buf_rb_tree
, &vp
->v_rbclean_tree
,
515 vtruncbuf_bp_trunc_cmp
,
516 vtruncbuf_bp_trunc
, &info
);
518 count
+= RB_SCAN(buf_rb_tree
, &vp
->v_rbdirty_tree
,
519 vtruncbuf_bp_trunc_cmp
,
520 vtruncbuf_bp_trunc
, &info
);
522 kprintf("Warning: vtruncbuf(): Had to re-clean %d "
523 "left over buffers in %s\n", count
, filename
);
527 lwkt_reltoken(&vp
->v_token
);
533 * The callback buffer is beyond the new file EOF and must be destroyed.
534 * Note that the compare function must conform to the RB_SCAN's requirements.
538 vtruncbuf_bp_trunc_cmp(struct buf
*bp
, void *data
)
540 struct vtruncbuf_info
*info
= data
;
542 if (bp
->b_loffset
>= info
->truncloffset
)
549 vtruncbuf_bp_trunc(struct buf
*bp
, void *data
)
551 struct vtruncbuf_info
*info
= data
;
554 * Do not try to use a buffer we cannot immediately lock, but sleep
555 * anyway to prevent a livelock. The code will loop until all buffers
558 * We must always revalidate the buffer after locking it to deal
561 if (BUF_LOCK(bp
, LK_EXCLUSIVE
| LK_NOWAIT
)) {
562 atomic_add_int(&bp
->b_refs
, 1);
563 if (BUF_LOCK(bp
, LK_EXCLUSIVE
|LK_SLEEPFAIL
) == 0)
565 atomic_subtract_int(&bp
->b_refs
, 1);
566 } else if ((info
->clean
&& (bp
->b_flags
& B_DELWRI
)) ||
567 (info
->clean
== 0 && (bp
->b_flags
& B_DELWRI
) == 0) ||
568 bp
->b_vp
!= info
->vp
||
569 vtruncbuf_bp_trunc_cmp(bp
, data
)) {
573 bp
->b_flags
|= (B_INVAL
| B_RELBUF
| B_NOCACHE
);
580 * Fsync all meta-data after truncating a file to be non-zero. Only metadata
581 * blocks (with a negative loffset) are scanned.
582 * Note that the compare function must conform to the RB_SCAN's requirements.
585 vtruncbuf_bp_metasync_cmp(struct buf
*bp
, void *data __unused
)
587 if (bp
->b_loffset
< 0)
593 vtruncbuf_bp_metasync(struct buf
*bp
, void *data
)
595 struct vtruncbuf_info
*info
= data
;
597 if (BUF_LOCK(bp
, LK_EXCLUSIVE
| LK_NOWAIT
)) {
598 atomic_add_int(&bp
->b_refs
, 1);
599 if (BUF_LOCK(bp
, LK_EXCLUSIVE
|LK_SLEEPFAIL
) == 0)
601 atomic_subtract_int(&bp
->b_refs
, 1);
602 } else if ((bp
->b_flags
& B_DELWRI
) == 0 ||
603 bp
->b_vp
!= info
->vp
||
604 vtruncbuf_bp_metasync_cmp(bp
, data
)) {
608 if (bp
->b_vp
== info
->vp
)
617 * vfsync - implements a multipass fsync on a file which understands
618 * dependancies and meta-data. The passed vnode must be locked. The
619 * waitfor argument may be MNT_WAIT or MNT_NOWAIT, or MNT_LAZY.
621 * When fsyncing data asynchronously just do one consolidated pass starting
622 * with the most negative block number. This may not get all the data due
625 * When fsyncing data synchronously do a data pass, then a metadata pass,
626 * then do additional data+metadata passes to try to get all the data out.
628 static int vfsync_wait_output(struct vnode
*vp
,
629 int (*waitoutput
)(struct vnode
*, struct thread
*));
630 static int vfsync_dummy_cmp(struct buf
*bp __unused
, void *data __unused
);
631 static int vfsync_data_only_cmp(struct buf
*bp
, void *data
);
632 static int vfsync_meta_only_cmp(struct buf
*bp
, void *data
);
633 static int vfsync_lazy_range_cmp(struct buf
*bp
, void *data
);
634 static int vfsync_bp(struct buf
*bp
, void *data
);
643 int (*checkdef
)(struct buf
*);
644 int (*cmpfunc
)(struct buf
*, void *);
648 vfsync(struct vnode
*vp
, int waitfor
, int passes
,
649 int (*checkdef
)(struct buf
*),
650 int (*waitoutput
)(struct vnode
*, struct thread
*))
652 struct vfsync_info info
;
655 bzero(&info
, sizeof(info
));
657 if ((info
.checkdef
= checkdef
) == NULL
)
660 lwkt_gettoken(&vp
->v_token
);
663 case MNT_LAZY
| MNT_NOWAIT
:
666 * Lazy (filesystem syncer typ) Asynchronous plus limit the
667 * number of data (not meta) pages we try to flush to 1MB.
668 * A non-zero return means that lazy limit was reached.
670 info
.lazylimit
= 1024 * 1024;
672 info
.cmpfunc
= vfsync_lazy_range_cmp
;
673 error
= RB_SCAN(buf_rb_tree
, &vp
->v_rbdirty_tree
,
674 vfsync_lazy_range_cmp
, vfsync_bp
, &info
);
675 info
.cmpfunc
= vfsync_meta_only_cmp
;
676 RB_SCAN(buf_rb_tree
, &vp
->v_rbdirty_tree
,
677 vfsync_meta_only_cmp
, vfsync_bp
, &info
);
680 else if (!RB_EMPTY(&vp
->v_rbdirty_tree
))
681 vn_syncer_add(vp
, 1);
686 * Asynchronous. Do a data-only pass and a meta-only pass.
689 info
.cmpfunc
= vfsync_data_only_cmp
;
690 RB_SCAN(buf_rb_tree
, &vp
->v_rbdirty_tree
, vfsync_data_only_cmp
,
692 info
.cmpfunc
= vfsync_meta_only_cmp
;
693 RB_SCAN(buf_rb_tree
, &vp
->v_rbdirty_tree
, vfsync_meta_only_cmp
,
699 * Synchronous. Do a data-only pass, then a meta-data+data
700 * pass, then additional integrated passes to try to get
701 * all the dependancies flushed.
703 info
.cmpfunc
= vfsync_data_only_cmp
;
704 RB_SCAN(buf_rb_tree
, &vp
->v_rbdirty_tree
, vfsync_data_only_cmp
,
706 error
= vfsync_wait_output(vp
, waitoutput
);
708 info
.skippedbufs
= 0;
709 info
.cmpfunc
= vfsync_dummy_cmp
;
710 RB_SCAN(buf_rb_tree
, &vp
->v_rbdirty_tree
, NULL
,
712 error
= vfsync_wait_output(vp
, waitoutput
);
713 if (info
.skippedbufs
) {
714 kprintf("Warning: vfsync skipped %d dirty "
715 "bufs in pass2!\n", info
.skippedbufs
);
718 while (error
== 0 && passes
> 0 &&
719 !RB_EMPTY(&vp
->v_rbdirty_tree
)
722 info
.synchronous
= 1;
725 info
.cmpfunc
= vfsync_dummy_cmp
;
726 error
= RB_SCAN(buf_rb_tree
, &vp
->v_rbdirty_tree
, NULL
,
732 error
= vfsync_wait_output(vp
, waitoutput
);
736 lwkt_reltoken(&vp
->v_token
);
741 vfsync_wait_output(struct vnode
*vp
,
742 int (*waitoutput
)(struct vnode
*, struct thread
*))
746 error
= bio_track_wait(&vp
->v_track_write
, 0, 0);
748 error
= waitoutput(vp
, curthread
);
753 vfsync_dummy_cmp(struct buf
*bp __unused
, void *data __unused
)
759 vfsync_data_only_cmp(struct buf
*bp
, void *data
)
761 if (bp
->b_loffset
< 0)
767 vfsync_meta_only_cmp(struct buf
*bp
, void *data
)
769 if (bp
->b_loffset
< 0)
775 vfsync_lazy_range_cmp(struct buf
*bp
, void *data
)
777 struct vfsync_info
*info
= data
;
779 if (bp
->b_loffset
< info
->vp
->v_lazyw
)
785 vfsync_bp(struct buf
*bp
, void *data
)
787 struct vfsync_info
*info
= data
;
788 struct vnode
*vp
= info
->vp
;
792 * Ignore buffers that we cannot immediately lock.
794 if (BUF_LOCK(bp
, LK_EXCLUSIVE
| LK_NOWAIT
)) {
800 * We must revalidate the buffer after locking.
802 if ((bp
->b_flags
& B_DELWRI
) == 0 ||
803 bp
->b_vp
!= info
->vp
||
804 info
->cmpfunc(bp
, data
)) {
810 * If syncdeps is not set we do not try to write buffers which have
813 if (!info
->synchronous
&& info
->syncdeps
== 0 && info
->checkdef(bp
)) {
819 * B_NEEDCOMMIT (primarily used by NFS) is a state where the buffer
820 * has been written but an additional handshake with the device
821 * is required before we can dispose of the buffer. We have no idea
822 * how to do this so we have to skip these buffers.
824 if (bp
->b_flags
& B_NEEDCOMMIT
) {
830 * Ask bioops if it is ok to sync. If not the VFS may have
831 * set B_LOCKED so we have to cycle the buffer.
833 if (LIST_FIRST(&bp
->b_dep
) != NULL
&& buf_checkwrite(bp
)) {
839 if (info
->synchronous
) {
841 * Synchronous flushing. An error may be returned.
847 * Asynchronous flushing. A negative return value simply
848 * stops the scan and is not considered an error. We use
849 * this to support limited MNT_LAZY flushes.
851 vp
->v_lazyw
= bp
->b_loffset
;
853 info
->lazycount
+= cluster_awrite(bp
);
854 waitrunningbufspace();
856 if (info
->lazylimit
&& info
->lazycount
>= info
->lazylimit
)
865 * Associate a buffer with a vnode.
870 bgetvp(struct vnode
*vp
, struct buf
*bp
, int testsize
)
872 KASSERT(bp
->b_vp
== NULL
, ("bgetvp: not free"));
873 KKASSERT((bp
->b_flags
& (B_HASHED
|B_DELWRI
|B_VNCLEAN
|B_VNDIRTY
)) == 0);
876 * Insert onto list for new vnode.
878 lwkt_gettoken(&vp
->v_token
);
880 if (buf_rb_hash_RB_INSERT(&vp
->v_rbhash_tree
, bp
)) {
881 lwkt_reltoken(&vp
->v_token
);
886 * Diagnostics (mainly for HAMMER debugging). Check for
887 * overlapping buffers.
889 if (check_buf_overlap
) {
891 bx
= buf_rb_hash_RB_PREV(bp
);
893 if (bx
->b_loffset
+ bx
->b_bufsize
> bp
->b_loffset
) {
894 kprintf("bgetvp: overlapl %016jx/%d %016jx "
896 (intmax_t)bx
->b_loffset
,
898 (intmax_t)bp
->b_loffset
,
900 if (check_buf_overlap
> 1)
901 panic("bgetvp - overlapping buffer");
904 bx
= buf_rb_hash_RB_NEXT(bp
);
906 if (bp
->b_loffset
+ testsize
> bx
->b_loffset
) {
907 kprintf("bgetvp: overlapr %016jx/%d %016jx "
909 (intmax_t)bp
->b_loffset
,
911 (intmax_t)bx
->b_loffset
,
913 if (check_buf_overlap
> 1)
914 panic("bgetvp - overlapping buffer");
919 bp
->b_flags
|= B_HASHED
;
920 bp
->b_flags
|= B_VNCLEAN
;
921 if (buf_rb_tree_RB_INSERT(&vp
->v_rbclean_tree
, bp
))
922 panic("reassignbuf: dup lblk/clean vp %p bp %p", vp
, bp
);
924 lwkt_reltoken(&vp
->v_token
);
929 * Disassociate a buffer from a vnode.
934 brelvp(struct buf
*bp
)
938 KASSERT(bp
->b_vp
!= NULL
, ("brelvp: NULL"));
941 * Delete from old vnode list, if on one.
944 lwkt_gettoken(&vp
->v_token
);
945 if (bp
->b_flags
& (B_VNDIRTY
| B_VNCLEAN
)) {
946 if (bp
->b_flags
& B_VNDIRTY
)
947 buf_rb_tree_RB_REMOVE(&vp
->v_rbdirty_tree
, bp
);
949 buf_rb_tree_RB_REMOVE(&vp
->v_rbclean_tree
, bp
);
950 bp
->b_flags
&= ~(B_VNDIRTY
| B_VNCLEAN
);
952 if (bp
->b_flags
& B_HASHED
) {
953 buf_rb_hash_RB_REMOVE(&vp
->v_rbhash_tree
, bp
);
954 bp
->b_flags
&= ~B_HASHED
;
956 if ((vp
->v_flag
& VONWORKLST
) && RB_EMPTY(&vp
->v_rbdirty_tree
))
957 vn_syncer_remove(vp
);
960 lwkt_reltoken(&vp
->v_token
);
966 * Reassign the buffer to the proper clean/dirty list based on B_DELWRI.
967 * This routine is called when the state of the B_DELWRI bit is changed.
969 * Must be called with vp->v_token held.
973 reassignbuf(struct buf
*bp
)
975 struct vnode
*vp
= bp
->b_vp
;
978 ASSERT_LWKT_TOKEN_HELD(&vp
->v_token
);
982 * B_PAGING flagged buffers cannot be reassigned because their vp
983 * is not fully linked in.
985 if (bp
->b_flags
& B_PAGING
)
986 panic("cannot reassign paging buffer");
988 if (bp
->b_flags
& B_DELWRI
) {
990 * Move to the dirty list, add the vnode to the worklist
992 if (bp
->b_flags
& B_VNCLEAN
) {
993 buf_rb_tree_RB_REMOVE(&vp
->v_rbclean_tree
, bp
);
994 bp
->b_flags
&= ~B_VNCLEAN
;
996 if ((bp
->b_flags
& B_VNDIRTY
) == 0) {
997 if (buf_rb_tree_RB_INSERT(&vp
->v_rbdirty_tree
, bp
)) {
998 panic("reassignbuf: dup lblk vp %p bp %p",
1001 bp
->b_flags
|= B_VNDIRTY
;
1003 if ((vp
->v_flag
& VONWORKLST
) == 0) {
1004 switch (vp
->v_type
) {
1011 vp
->v_rdev
->si_mountpoint
!= NULL
) {
1019 vn_syncer_add(vp
, delay
);
1023 * Move to the clean list, remove the vnode from the worklist
1024 * if no dirty blocks remain.
1026 if (bp
->b_flags
& B_VNDIRTY
) {
1027 buf_rb_tree_RB_REMOVE(&vp
->v_rbdirty_tree
, bp
);
1028 bp
->b_flags
&= ~B_VNDIRTY
;
1030 if ((bp
->b_flags
& B_VNCLEAN
) == 0) {
1031 if (buf_rb_tree_RB_INSERT(&vp
->v_rbclean_tree
, bp
)) {
1032 panic("reassignbuf: dup lblk vp %p bp %p",
1035 bp
->b_flags
|= B_VNCLEAN
;
1037 if ((vp
->v_flag
& VONWORKLST
) &&
1038 RB_EMPTY(&vp
->v_rbdirty_tree
)) {
1039 vn_syncer_remove(vp
);
1045 * Create a vnode for a block device. Used for mounting the root file
1048 * A vref()'d vnode is returned.
1050 extern struct vop_ops
*devfs_vnode_dev_vops_p
;
1052 bdevvp(cdev_t dev
, struct vnode
**vpp
)
1062 error
= getspecialvnode(VT_NON
, NULL
, &devfs_vnode_dev_vops_p
,
1073 v_associate_rdev(vp
, dev
);
1074 vp
->v_umajor
= dev
->si_umajor
;
1075 vp
->v_uminor
= dev
->si_uminor
;
1082 v_associate_rdev(struct vnode
*vp
, cdev_t dev
)
1086 if (dev_is_good(dev
) == 0)
1088 KKASSERT(vp
->v_rdev
== NULL
);
1089 vp
->v_rdev
= reference_dev(dev
);
1090 lwkt_gettoken(&spechash_token
);
1091 SLIST_INSERT_HEAD(&dev
->si_hlist
, vp
, v_cdevnext
);
1092 lwkt_reltoken(&spechash_token
);
1097 v_release_rdev(struct vnode
*vp
)
1101 if ((dev
= vp
->v_rdev
) != NULL
) {
1102 lwkt_gettoken(&spechash_token
);
1103 SLIST_REMOVE(&dev
->si_hlist
, vp
, vnode
, v_cdevnext
);
1106 lwkt_reltoken(&spechash_token
);
1111 * Add a vnode to the alias list hung off the cdev_t. We only associate
1112 * the device number with the vnode. The actual device is not associated
1113 * until the vnode is opened (usually in spec_open()), and will be
1114 * disassociated on last close.
1117 addaliasu(struct vnode
*nvp
, int x
, int y
)
1119 if (nvp
->v_type
!= VBLK
&& nvp
->v_type
!= VCHR
)
1120 panic("addaliasu on non-special vnode");
1126 * Simple call that a filesystem can make to try to get rid of a
1127 * vnode. It will fail if anyone is referencing the vnode (including
1130 * The filesystem can check whether its in-memory inode structure still
1131 * references the vp on return.
1134 vclean_unlocked(struct vnode
*vp
)
1137 if (sysref_isactive(&vp
->v_sysref
) == 0)
1143 * Disassociate a vnode from its underlying filesystem.
1145 * The vnode must be VX locked and referenced. In all normal situations
1146 * there are no active references. If vclean_vxlocked() is called while
1147 * there are active references, the vnode is being ripped out and we have
1148 * to call VOP_CLOSE() as appropriate before we can reclaim it.
1151 vclean_vxlocked(struct vnode
*vp
, int flags
)
1156 struct namecache
*ncp
;
1159 * If the vnode has already been reclaimed we have nothing to do.
1161 if (vp
->v_flag
& VRECLAIMED
)
1163 vsetflags(vp
, VRECLAIMED
);
1165 if (verbose_reclaims
) {
1166 if ((ncp
= TAILQ_FIRST(&vp
->v_namecache
)) != NULL
)
1167 kprintf("Debug: reclaim %p %s\n", vp
, ncp
->nc_name
);
1171 * Scrap the vfs cache
1173 while (cache_inval_vp(vp
, 0) != 0) {
1174 kprintf("Warning: vnode %p clean/cache_resolution "
1175 "race detected\n", vp
);
1176 tsleep(vp
, 0, "vclninv", 2);
1180 * Check to see if the vnode is in use. If so we have to reference it
1181 * before we clean it out so that its count cannot fall to zero and
1182 * generate a race against ourselves to recycle it.
1184 active
= sysref_isactive(&vp
->v_sysref
);
1187 * Clean out any buffers associated with the vnode and destroy its
1188 * object, if it has one.
1190 vinvalbuf(vp
, V_SAVE
, 0, 0);
1193 * If purging an active vnode (typically during a forced unmount
1194 * or reboot), it must be closed and deactivated before being
1195 * reclaimed. This isn't really all that safe, but what can
1198 * Note that neither of these routines unlocks the vnode.
1200 if (active
&& (flags
& DOCLOSE
)) {
1201 while ((n
= vp
->v_opencount
) != 0) {
1202 if (vp
->v_writecount
)
1203 VOP_CLOSE(vp
, FWRITE
|FNONBLOCK
);
1205 VOP_CLOSE(vp
, FNONBLOCK
);
1206 if (vp
->v_opencount
== n
) {
1207 kprintf("Warning: unable to force-close"
1215 * If the vnode has not been deactivated, deactivated it. Deactivation
1216 * can create new buffers and VM pages so we have to call vinvalbuf()
1217 * again to make sure they all get flushed.
1219 * This can occur if a file with a link count of 0 needs to be
1222 * If the vnode is already dead don't try to deactivate it.
1224 if ((vp
->v_flag
& VINACTIVE
) == 0) {
1225 vsetflags(vp
, VINACTIVE
);
1228 vinvalbuf(vp
, V_SAVE
, 0, 0);
1232 * If the vnode has an object, destroy it.
1234 while ((object
= vp
->v_object
) != NULL
) {
1235 vm_object_hold(object
);
1236 if (object
== vp
->v_object
)
1238 vm_object_drop(object
);
1241 if (object
!= NULL
) {
1242 if (object
->ref_count
== 0) {
1243 if ((object
->flags
& OBJ_DEAD
) == 0)
1244 vm_object_terminate(object
);
1245 vm_object_drop(object
);
1246 vclrflags(vp
, VOBJBUF
);
1248 vm_pager_deallocate(object
);
1249 vclrflags(vp
, VOBJBUF
);
1250 vm_object_drop(object
);
1253 KKASSERT((vp
->v_flag
& VOBJBUF
) == 0);
1256 * Reclaim the vnode if not already dead.
1258 if (vp
->v_mount
&& VOP_RECLAIM(vp
))
1259 panic("vclean: cannot reclaim");
1262 * Done with purge, notify sleepers of the grim news.
1264 vp
->v_ops
= &dead_vnode_vops_p
;
1269 * If we are destroying an active vnode, reactivate it now that
1270 * we have reassociated it with deadfs. This prevents the system
1271 * from crashing on the vnode due to it being unexpectedly marked
1272 * as inactive or reclaimed.
1274 if (active
&& (flags
& DOCLOSE
)) {
1275 vclrflags(vp
, VINACTIVE
| VRECLAIMED
);
1280 * Eliminate all activity associated with the requested vnode
1281 * and with all vnodes aliased to the requested vnode.
1283 * The vnode must be referenced but should not be locked.
1286 vrevoke(struct vnode
*vp
, struct ucred
*cred
)
1294 * If the vnode has a device association, scrap all vnodes associated
1295 * with the device. Don't let the device disappear on us while we
1296 * are scrapping the vnodes.
1298 * The passed vp will probably show up in the list, do not VX lock
1301 * Releasing the vnode's rdev here can mess up specfs's call to
1302 * device close, so don't do it. The vnode has been disassociated
1303 * and the device will be closed after the last ref on the related
1304 * fp goes away (if not still open by e.g. the kernel).
1306 if (vp
->v_type
!= VCHR
) {
1307 error
= fdrevoke(vp
, DTYPE_VNODE
, cred
);
1310 if ((dev
= vp
->v_rdev
) == NULL
) {
1314 lwkt_gettoken(&spechash_token
);
1317 vqn
= SLIST_FIRST(&dev
->si_hlist
);
1320 while ((vq
= vqn
) != NULL
) {
1321 if (sysref_isactive(&vq
->v_sysref
)) {
1323 fdrevoke(vq
, DTYPE_VNODE
, cred
);
1324 /*v_release_rdev(vq);*/
1326 if (vq
->v_rdev
!= dev
) {
1331 vqn
= SLIST_NEXT(vq
, v_cdevnext
);
1336 lwkt_reltoken(&spechash_token
);
1343 * This is called when the object underlying a vnode is being destroyed,
1344 * such as in a remove(). Try to recycle the vnode immediately if the
1345 * only active reference is our reference.
1347 * Directory vnodes in the namecache with children cannot be immediately
1348 * recycled because numerous VOP_N*() ops require them to be stable.
1350 * To avoid recursive recycling from VOP_INACTIVE implemenetations this
1351 * function is a NOP if VRECLAIMED is already set.
1354 vrecycle(struct vnode
*vp
)
1356 if (vp
->v_sysref
.refcnt
<= 1 && (vp
->v_flag
& VRECLAIMED
) == 0) {
1357 if (cache_inval_vp_nonblock(vp
))
1366 * Return the maximum I/O size allowed for strategy calls on VP.
1368 * If vp is VCHR or VBLK we dive the device, otherwise we use
1369 * the vp's mount info.
1371 * The returned value is clamped at MAXPHYS as most callers cannot use
1372 * buffers larger than that size.
1375 vmaxiosize(struct vnode
*vp
)
1379 if (vp
->v_type
== VBLK
|| vp
->v_type
== VCHR
)
1380 maxiosize
= vp
->v_rdev
->si_iosize_max
;
1382 maxiosize
= vp
->v_mount
->mnt_iosize_max
;
1384 if (maxiosize
> MAXPHYS
)
1385 maxiosize
= MAXPHYS
;
1390 * Eliminate all activity associated with a vnode in preparation for reuse.
1392 * The vnode must be VX locked and refd and will remain VX locked and refd
1393 * on return. This routine may be called with the vnode in any state, as
1394 * long as it is VX locked. The vnode will be cleaned out and marked
1395 * VRECLAIMED but will not actually be reused until all existing refs and
1398 * NOTE: This routine may be called on a vnode which has not yet been
1399 * already been deactivated (VOP_INACTIVE), or on a vnode which has
1400 * already been reclaimed.
1402 * This routine is not responsible for placing us back on the freelist.
1403 * Instead, it happens automatically when the caller releases the VX lock
1404 * (assuming there aren't any other references).
1407 vgone_vxlocked(struct vnode
*vp
)
1410 * assert that the VX lock is held. This is an absolute requirement
1411 * now for vgone_vxlocked() to be called.
1413 KKASSERT(vp
->v_lock
.lk_exclusivecount
== 1);
1416 * Clean out the filesystem specific data and set the VRECLAIMED
1417 * bit. Also deactivate the vnode if necessary.
1419 vclean_vxlocked(vp
, DOCLOSE
);
1422 * Delete from old mount point vnode list, if on one.
1424 if (vp
->v_mount
!= NULL
) {
1425 KKASSERT(vp
->v_data
== NULL
);
1426 insmntque(vp
, NULL
);
1430 * If special device, remove it from special device alias list
1431 * if it is on one. This should normally only occur if a vnode is
1432 * being revoked as the device should otherwise have been released
1435 if ((vp
->v_type
== VBLK
|| vp
->v_type
== VCHR
) && vp
->v_rdev
!= NULL
) {
1446 * Lookup a vnode by device number.
1448 * Returns non-zero and *vpp set to a vref'd vnode on success.
1449 * Returns zero on failure.
1452 vfinddev(cdev_t dev
, enum vtype type
, struct vnode
**vpp
)
1456 lwkt_gettoken(&spechash_token
);
1457 SLIST_FOREACH(vp
, &dev
->si_hlist
, v_cdevnext
) {
1458 if (type
== vp
->v_type
) {
1461 lwkt_reltoken(&spechash_token
);
1465 lwkt_reltoken(&spechash_token
);
1470 * Calculate the total number of references to a special device. This
1471 * routine may only be called for VBLK and VCHR vnodes since v_rdev is
1472 * an overloaded field. Since udev2dev can now return NULL, we have
1473 * to check for a NULL v_rdev.
1476 count_dev(cdev_t dev
)
1481 if (SLIST_FIRST(&dev
->si_hlist
)) {
1482 lwkt_gettoken(&spechash_token
);
1483 SLIST_FOREACH(vp
, &dev
->si_hlist
, v_cdevnext
) {
1484 count
+= vp
->v_opencount
;
1486 lwkt_reltoken(&spechash_token
);
1492 vcount(struct vnode
*vp
)
1494 if (vp
->v_rdev
== NULL
)
1496 return(count_dev(vp
->v_rdev
));
1500 * Initialize VMIO for a vnode. This routine MUST be called before a
1501 * VFS can issue buffer cache ops on a vnode. It is typically called
1502 * when a vnode is initialized from its inode.
1505 vinitvmio(struct vnode
*vp
, off_t filesize
, int blksize
, int boff
)
1511 while ((object
= vp
->v_object
) != NULL
) {
1512 vm_object_hold(object
);
1513 if (object
== vp
->v_object
)
1515 vm_object_drop(object
);
1518 if (object
== NULL
) {
1519 object
= vnode_pager_alloc(vp
, filesize
, 0, 0, blksize
, boff
);
1522 * Dereference the reference we just created. This assumes
1523 * that the object is associated with the vp.
1525 vm_object_hold(object
);
1526 object
->ref_count
--;
1529 if (object
->flags
& OBJ_DEAD
) {
1531 if (vp
->v_object
== object
)
1532 vm_object_dead_sleep(object
, "vodead");
1533 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
1534 vm_object_drop(object
);
1538 KASSERT(vp
->v_object
!= NULL
, ("vinitvmio: NULL object"));
1539 vsetflags(vp
, VOBJBUF
);
1540 vm_object_drop(object
);
1547 * Print out a description of a vnode.
1549 static char *typename
[] =
1550 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD"};
1553 vprint(char *label
, struct vnode
*vp
)
1558 kprintf("%s: %p: ", label
, (void *)vp
);
1560 kprintf("%p: ", (void *)vp
);
1561 kprintf("type %s, sysrefs %d, writecount %d, holdcnt %d,",
1562 typename
[vp
->v_type
],
1563 vp
->v_sysref
.refcnt
, vp
->v_writecount
, vp
->v_auxrefs
);
1565 if (vp
->v_flag
& VROOT
)
1566 strcat(buf
, "|VROOT");
1567 if (vp
->v_flag
& VPFSROOT
)
1568 strcat(buf
, "|VPFSROOT");
1569 if (vp
->v_flag
& VTEXT
)
1570 strcat(buf
, "|VTEXT");
1571 if (vp
->v_flag
& VSYSTEM
)
1572 strcat(buf
, "|VSYSTEM");
1573 if (vp
->v_flag
& VFREE
)
1574 strcat(buf
, "|VFREE");
1575 if (vp
->v_flag
& VOBJBUF
)
1576 strcat(buf
, "|VOBJBUF");
1578 kprintf(" flags (%s)", &buf
[1]);
1579 if (vp
->v_data
== NULL
) {
1588 * Do the usual access checking.
1589 * file_mode, uid and gid are from the vnode in question,
1590 * while acc_mode and cred are from the VOP_ACCESS parameter list
1593 vaccess(enum vtype type
, mode_t file_mode
, uid_t uid
, gid_t gid
,
1594 mode_t acc_mode
, struct ucred
*cred
)
1600 * Super-user always gets read/write access, but execute access depends
1601 * on at least one execute bit being set.
1603 if (priv_check_cred(cred
, PRIV_ROOT
, 0) == 0) {
1604 if ((acc_mode
& VEXEC
) && type
!= VDIR
&&
1605 (file_mode
& (S_IXUSR
|S_IXGRP
|S_IXOTH
)) == 0)
1612 /* Otherwise, check the owner. */
1613 if (cred
->cr_uid
== uid
) {
1614 if (acc_mode
& VEXEC
)
1616 if (acc_mode
& VREAD
)
1618 if (acc_mode
& VWRITE
)
1620 return ((file_mode
& mask
) == mask
? 0 : EACCES
);
1623 /* Otherwise, check the groups. */
1624 ismember
= groupmember(gid
, cred
);
1625 if (cred
->cr_svgid
== gid
|| ismember
) {
1626 if (acc_mode
& VEXEC
)
1628 if (acc_mode
& VREAD
)
1630 if (acc_mode
& VWRITE
)
1632 return ((file_mode
& mask
) == mask
? 0 : EACCES
);
1635 /* Otherwise, check everyone else. */
1636 if (acc_mode
& VEXEC
)
1638 if (acc_mode
& VREAD
)
1640 if (acc_mode
& VWRITE
)
1642 return ((file_mode
& mask
) == mask
? 0 : EACCES
);
1646 #include <ddb/ddb.h>
1648 static int db_show_locked_vnodes(struct mount
*mp
, void *data
);
1651 * List all of the locked vnodes in the system.
1652 * Called when debugging the kernel.
1654 DB_SHOW_COMMAND(lockedvnodes
, lockedvnodes
)
1656 kprintf("Locked vnodes\n");
1657 mountlist_scan(db_show_locked_vnodes
, NULL
,
1658 MNTSCAN_FORWARD
|MNTSCAN_NOBUSY
);
1662 db_show_locked_vnodes(struct mount
*mp
, void *data __unused
)
1666 TAILQ_FOREACH(vp
, &mp
->mnt_nvnodelist
, v_nmntvnodes
) {
1667 if (vn_islocked(vp
))
1675 * Top level filesystem related information gathering.
1677 static int sysctl_ovfs_conf (SYSCTL_HANDLER_ARGS
);
1680 vfs_sysctl(SYSCTL_HANDLER_ARGS
)
1682 int *name
= (int *)arg1
- 1; /* XXX */
1683 u_int namelen
= arg2
+ 1; /* XXX */
1684 struct vfsconf
*vfsp
;
1687 #if 1 || defined(COMPAT_PRELITE2)
1688 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
1690 return (sysctl_ovfs_conf(oidp
, arg1
, arg2
, req
));
1694 /* all sysctl names at this level are at least name and field */
1696 return (ENOTDIR
); /* overloaded */
1697 if (name
[0] != VFS_GENERIC
) {
1698 vfsp
= vfsconf_find_by_typenum(name
[0]);
1700 return (EOPNOTSUPP
);
1701 return ((*vfsp
->vfc_vfsops
->vfs_sysctl
)(&name
[1], namelen
- 1,
1702 oldp
, oldlenp
, newp
, newlen
, p
));
1706 case VFS_MAXTYPENUM
:
1709 maxtypenum
= vfsconf_get_maxtypenum();
1710 return (SYSCTL_OUT(req
, &maxtypenum
, sizeof(maxtypenum
)));
1713 return (ENOTDIR
); /* overloaded */
1714 vfsp
= vfsconf_find_by_typenum(name
[2]);
1716 return (EOPNOTSUPP
);
1717 return (SYSCTL_OUT(req
, vfsp
, sizeof *vfsp
));
1719 return (EOPNOTSUPP
);
1722 SYSCTL_NODE(_vfs
, VFS_GENERIC
, generic
, CTLFLAG_RD
, vfs_sysctl
,
1723 "Generic filesystem");
1725 #if 1 || defined(COMPAT_PRELITE2)
1728 sysctl_ovfs_conf_iter(struct vfsconf
*vfsp
, void *data
)
1731 struct ovfsconf ovfs
;
1732 struct sysctl_req
*req
= (struct sysctl_req
*) data
;
1734 bzero(&ovfs
, sizeof(ovfs
));
1735 ovfs
.vfc_vfsops
= vfsp
->vfc_vfsops
; /* XXX used as flag */
1736 strcpy(ovfs
.vfc_name
, vfsp
->vfc_name
);
1737 ovfs
.vfc_index
= vfsp
->vfc_typenum
;
1738 ovfs
.vfc_refcount
= vfsp
->vfc_refcount
;
1739 ovfs
.vfc_flags
= vfsp
->vfc_flags
;
1740 error
= SYSCTL_OUT(req
, &ovfs
, sizeof ovfs
);
1742 return error
; /* abort iteration with error code */
1744 return 0; /* continue iterating with next element */
1748 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS
)
1750 return vfsconf_each(sysctl_ovfs_conf_iter
, (void*)req
);
1753 #endif /* 1 || COMPAT_PRELITE2 */
1756 * Check to see if a filesystem is mounted on a block device.
1759 vfs_mountedon(struct vnode
*vp
)
1763 if ((dev
= vp
->v_rdev
) == NULL
) {
1764 /* if (vp->v_type != VBLK)
1765 dev = get_dev(vp->v_uminor, vp->v_umajor); */
1767 if (dev
!= NULL
&& dev
->si_mountpoint
)
1773 * Unmount all filesystems. The list is traversed in reverse order
1774 * of mounting to avoid dependencies.
1777 static int vfs_umountall_callback(struct mount
*mp
, void *data
);
1780 vfs_unmountall(void)
1785 count
= mountlist_scan(vfs_umountall_callback
,
1786 NULL
, MNTSCAN_REVERSE
|MNTSCAN_NOBUSY
);
1792 vfs_umountall_callback(struct mount
*mp
, void *data
)
1796 error
= dounmount(mp
, MNT_FORCE
);
1798 mountlist_remove(mp
);
1799 kprintf("unmount of filesystem mounted from %s failed (",
1800 mp
->mnt_stat
.f_mntfromname
);
1804 kprintf("%d)\n", error
);
1810 * Checks the mount flags for parameter mp and put the names comma-separated
1811 * into a string buffer buf with a size limit specified by len.
1813 * It returns the number of bytes written into buf, and (*errorp) will be
1814 * set to 0, EINVAL (if passed length is 0), or ENOSPC (supplied buffer was
1815 * not large enough). The buffer will be 0-terminated if len was not 0.
1818 vfs_flagstostr(int flags
, const struct mountctl_opt
*optp
,
1819 char *buf
, size_t len
, int *errorp
)
1821 static const struct mountctl_opt optnames
[] = {
1822 { MNT_ASYNC
, "asynchronous" },
1823 { MNT_EXPORTED
, "NFS exported" },
1824 { MNT_LOCAL
, "local" },
1825 { MNT_NOATIME
, "noatime" },
1826 { MNT_NODEV
, "nodev" },
1827 { MNT_NOEXEC
, "noexec" },
1828 { MNT_NOSUID
, "nosuid" },
1829 { MNT_NOSYMFOLLOW
, "nosymfollow" },
1830 { MNT_QUOTA
, "with-quotas" },
1831 { MNT_RDONLY
, "read-only" },
1832 { MNT_SYNCHRONOUS
, "synchronous" },
1833 { MNT_UNION
, "union" },
1834 { MNT_NOCLUSTERR
, "noclusterr" },
1835 { MNT_NOCLUSTERW
, "noclusterw" },
1836 { MNT_SUIDDIR
, "suiddir" },
1837 { MNT_SOFTDEP
, "soft-updates" },
1838 { MNT_IGNORE
, "ignore" },
1848 bleft
= len
- 1; /* leave room for trailing \0 */
1851 * Checks the size of the string. If it contains
1852 * any data, then we will append the new flags to
1855 actsize
= strlen(buf
);
1859 /* Default flags if no flags passed */
1863 if (bleft
< 0) { /* degenerate case, 0-length buffer */
1868 for (; flags
&& optp
->o_opt
; ++optp
) {
1869 if ((flags
& optp
->o_opt
) == 0)
1871 optlen
= strlen(optp
->o_name
);
1872 if (bwritten
|| actsize
> 0) {
1877 buf
[bwritten
++] = ',';
1878 buf
[bwritten
++] = ' ';
1881 if (bleft
< optlen
) {
1885 bcopy(optp
->o_name
, buf
+ bwritten
, optlen
);
1888 flags
&= ~optp
->o_opt
;
1892 * Space already reserved for trailing \0
1899 * Build hash lists of net addresses and hang them off the mount point.
1900 * Called by ufs_mount() to set up the lists of export addresses.
1903 vfs_hang_addrlist(struct mount
*mp
, struct netexport
*nep
,
1904 const struct export_args
*argp
)
1907 struct radix_node_head
*rnh
;
1909 struct radix_node
*rn
;
1910 struct sockaddr
*saddr
, *smask
= NULL
;
1914 if (argp
->ex_addrlen
== 0) {
1915 if (mp
->mnt_flag
& MNT_DEFEXPORTED
)
1917 np
= &nep
->ne_defexported
;
1918 np
->netc_exflags
= argp
->ex_flags
;
1919 np
->netc_anon
= argp
->ex_anon
;
1920 np
->netc_anon
.cr_ref
= 1;
1921 mp
->mnt_flag
|= MNT_DEFEXPORTED
;
1925 if (argp
->ex_addrlen
< 0 || argp
->ex_addrlen
> MLEN
)
1927 if (argp
->ex_masklen
< 0 || argp
->ex_masklen
> MLEN
)
1930 i
= sizeof(struct netcred
) + argp
->ex_addrlen
+ argp
->ex_masklen
;
1931 np
= (struct netcred
*) kmalloc(i
, M_NETADDR
, M_WAITOK
| M_ZERO
);
1932 saddr
= (struct sockaddr
*) (np
+ 1);
1933 if ((error
= copyin(argp
->ex_addr
, (caddr_t
) saddr
, argp
->ex_addrlen
)))
1935 if (saddr
->sa_len
> argp
->ex_addrlen
)
1936 saddr
->sa_len
= argp
->ex_addrlen
;
1937 if (argp
->ex_masklen
) {
1938 smask
= (struct sockaddr
*)((caddr_t
)saddr
+ argp
->ex_addrlen
);
1939 error
= copyin(argp
->ex_mask
, (caddr_t
)smask
, argp
->ex_masklen
);
1942 if (smask
->sa_len
> argp
->ex_masklen
)
1943 smask
->sa_len
= argp
->ex_masklen
;
1945 i
= saddr
->sa_family
;
1946 if ((rnh
= nep
->ne_rtable
[i
]) == NULL
) {
1948 * Seems silly to initialize every AF when most are not used,
1949 * do so on demand here
1951 SLIST_FOREACH(dom
, &domains
, dom_next
)
1952 if (dom
->dom_family
== i
&& dom
->dom_rtattach
) {
1953 dom
->dom_rtattach((void **) &nep
->ne_rtable
[i
],
1957 if ((rnh
= nep
->ne_rtable
[i
]) == NULL
) {
1962 rn
= (*rnh
->rnh_addaddr
) ((char *) saddr
, (char *) smask
, rnh
,
1964 if (rn
== NULL
|| np
!= (struct netcred
*) rn
) { /* already exists */
1968 np
->netc_exflags
= argp
->ex_flags
;
1969 np
->netc_anon
= argp
->ex_anon
;
1970 np
->netc_anon
.cr_ref
= 1;
1973 kfree(np
, M_NETADDR
);
1979 vfs_free_netcred(struct radix_node
*rn
, void *w
)
1981 struct radix_node_head
*rnh
= (struct radix_node_head
*) w
;
1983 (*rnh
->rnh_deladdr
) (rn
->rn_key
, rn
->rn_mask
, rnh
);
1984 kfree((caddr_t
) rn
, M_NETADDR
);
1989 * Free the net address hash lists that are hanging off the mount points.
1992 vfs_free_addrlist(struct netexport
*nep
)
1995 struct radix_node_head
*rnh
;
1997 for (i
= 0; i
<= AF_MAX
; i
++)
1998 if ((rnh
= nep
->ne_rtable
[i
])) {
1999 (*rnh
->rnh_walktree
) (rnh
, vfs_free_netcred
,
2001 kfree((caddr_t
) rnh
, M_RTABLE
);
2002 nep
->ne_rtable
[i
] = 0;
2007 vfs_export(struct mount
*mp
, struct netexport
*nep
,
2008 const struct export_args
*argp
)
2012 if (argp
->ex_flags
& MNT_DELEXPORT
) {
2013 if (mp
->mnt_flag
& MNT_EXPUBLIC
) {
2014 vfs_setpublicfs(NULL
, NULL
, NULL
);
2015 mp
->mnt_flag
&= ~MNT_EXPUBLIC
;
2017 vfs_free_addrlist(nep
);
2018 mp
->mnt_flag
&= ~(MNT_EXPORTED
| MNT_DEFEXPORTED
);
2020 if (argp
->ex_flags
& MNT_EXPORTED
) {
2021 if (argp
->ex_flags
& MNT_EXPUBLIC
) {
2022 if ((error
= vfs_setpublicfs(mp
, nep
, argp
)) != 0)
2024 mp
->mnt_flag
|= MNT_EXPUBLIC
;
2026 if ((error
= vfs_hang_addrlist(mp
, nep
, argp
)))
2028 mp
->mnt_flag
|= MNT_EXPORTED
;
2035 * Set the publicly exported filesystem (WebNFS). Currently, only
2036 * one public filesystem is possible in the spec (RFC 2054 and 2055)
2039 vfs_setpublicfs(struct mount
*mp
, struct netexport
*nep
,
2040 const struct export_args
*argp
)
2047 * mp == NULL -> invalidate the current info, the FS is
2048 * no longer exported. May be called from either vfs_export
2049 * or unmount, so check if it hasn't already been done.
2052 if (nfs_pub
.np_valid
) {
2053 nfs_pub
.np_valid
= 0;
2054 if (nfs_pub
.np_index
!= NULL
) {
2055 kfree(nfs_pub
.np_index
, M_TEMP
);
2056 nfs_pub
.np_index
= NULL
;
2063 * Only one allowed at a time.
2065 if (nfs_pub
.np_valid
!= 0 && mp
!= nfs_pub
.np_mount
)
2069 * Get real filehandle for root of exported FS.
2071 bzero((caddr_t
)&nfs_pub
.np_handle
, sizeof(nfs_pub
.np_handle
));
2072 nfs_pub
.np_handle
.fh_fsid
= mp
->mnt_stat
.f_fsid
;
2074 if ((error
= VFS_ROOT(mp
, &rvp
)))
2077 if ((error
= VFS_VPTOFH(rvp
, &nfs_pub
.np_handle
.fh_fid
)))
2083 * If an indexfile was specified, pull it in.
2085 if (argp
->ex_indexfile
!= NULL
) {
2088 error
= vn_get_namelen(rvp
, &namelen
);
2091 nfs_pub
.np_index
= kmalloc(namelen
, M_TEMP
, M_WAITOK
);
2092 error
= copyinstr(argp
->ex_indexfile
, nfs_pub
.np_index
,
2096 * Check for illegal filenames.
2098 for (cp
= nfs_pub
.np_index
; *cp
; cp
++) {
2106 kfree(nfs_pub
.np_index
, M_TEMP
);
2111 nfs_pub
.np_mount
= mp
;
2112 nfs_pub
.np_valid
= 1;
2117 vfs_export_lookup(struct mount
*mp
, struct netexport
*nep
,
2118 struct sockaddr
*nam
)
2121 struct radix_node_head
*rnh
;
2122 struct sockaddr
*saddr
;
2125 if (mp
->mnt_flag
& MNT_EXPORTED
) {
2127 * Lookup in the export list first.
2131 rnh
= nep
->ne_rtable
[saddr
->sa_family
];
2133 np
= (struct netcred
*)
2134 (*rnh
->rnh_matchaddr
)((char *)saddr
,
2136 if (np
&& np
->netc_rnodes
->rn_flags
& RNF_ROOT
)
2141 * If no address match, use the default if it exists.
2143 if (np
== NULL
&& mp
->mnt_flag
& MNT_DEFEXPORTED
)
2144 np
= &nep
->ne_defexported
;
2150 * perform msync on all vnodes under a mount point. The mount point must
2151 * be locked. This code is also responsible for lazy-freeing unreferenced
2152 * vnodes whos VM objects no longer contain pages.
2154 * NOTE: MNT_WAIT still skips vnodes in the VXLOCK state.
2156 * NOTE: XXX VOP_PUTPAGES and friends requires that the vnode be locked,
2157 * but vnode_pager_putpages() doesn't lock the vnode. We have to do it
2158 * way up in this high level function.
2160 static int vfs_msync_scan1(struct mount
*mp
, struct vnode
*vp
, void *data
);
2161 static int vfs_msync_scan2(struct mount
*mp
, struct vnode
*vp
, void *data
);
2164 vfs_msync(struct mount
*mp
, int flags
)
2169 * tmpfs sets this flag to prevent msync(), sync, and the
2170 * filesystem periodic syncer from trying to flush VM pages
2171 * to swap. Only pure memory pressure flushes tmpfs VM pages
2174 if (mp
->mnt_kern_flag
& MNTK_NOMSYNC
)
2178 * Ok, scan the vnodes for work.
2180 vmsc_flags
= VMSC_GETVP
;
2181 if (flags
!= MNT_WAIT
)
2182 vmsc_flags
|= VMSC_NOWAIT
;
2183 vmntvnodescan(mp
, vmsc_flags
,
2184 vfs_msync_scan1
, vfs_msync_scan2
,
2185 (void *)(intptr_t)flags
);
2189 * scan1 is a fast pre-check. There could be hundreds of thousands of
2190 * vnodes, we cannot afford to do anything heavy weight until we have a
2191 * fairly good indication that there is work to do.
2195 vfs_msync_scan1(struct mount
*mp
, struct vnode
*vp
, void *data
)
2197 int flags
= (int)(intptr_t)data
;
2199 if ((vp
->v_flag
& VRECLAIMED
) == 0) {
2200 if (vshouldmsync(vp
))
2201 return(0); /* call scan2 */
2202 if ((mp
->mnt_flag
& MNT_RDONLY
) == 0 &&
2203 (vp
->v_flag
& VOBJDIRTY
) &&
2204 (flags
== MNT_WAIT
|| vn_islocked(vp
) == 0)) {
2205 return(0); /* call scan2 */
2210 * do not call scan2, continue the loop
2216 * This callback is handed a locked vnode.
2220 vfs_msync_scan2(struct mount
*mp
, struct vnode
*vp
, void *data
)
2223 int flags
= (int)(intptr_t)data
;
2225 if (vp
->v_flag
& VRECLAIMED
)
2228 if ((mp
->mnt_flag
& MNT_RDONLY
) == 0 && (vp
->v_flag
& VOBJDIRTY
)) {
2229 if ((obj
= vp
->v_object
) != NULL
) {
2230 vm_object_page_clean(obj
, 0, 0,
2231 flags
== MNT_WAIT
? OBJPC_SYNC
: OBJPC_NOSYNC
);
2238 * Wake up anyone interested in vp because it is being revoked.
2241 vn_gone(struct vnode
*vp
)
2243 lwkt_gettoken(&vp
->v_token
);
2244 KNOTE(&vp
->v_pollinfo
.vpi_kqinfo
.ki_note
, NOTE_REVOKE
);
2245 lwkt_reltoken(&vp
->v_token
);
2249 * extract the cdev_t from a VBLK or VCHR. The vnode must have been opened
2250 * (or v_rdev might be NULL).
2253 vn_todev(struct vnode
*vp
)
2255 if (vp
->v_type
!= VBLK
&& vp
->v_type
!= VCHR
)
2257 KKASSERT(vp
->v_rdev
!= NULL
);
2258 return (vp
->v_rdev
);
2262 * Check if vnode represents a disk device. The vnode does not need to be
2268 vn_isdisk(struct vnode
*vp
, int *errp
)
2272 if (vp
->v_type
!= VCHR
) {
2285 if (dev_is_good(dev
) == 0) {
2290 if ((dev_dflags(dev
) & D_DISK
) == 0) {
2301 vn_get_namelen(struct vnode
*vp
, int *namelen
)
2304 register_t retval
[2];
2306 error
= VOP_PATHCONF(vp
, _PC_NAME_MAX
, retval
);
2309 *namelen
= (int)retval
[0];
2314 vop_write_dirent(int *error
, struct uio
*uio
, ino_t d_ino
, uint8_t d_type
,
2315 uint16_t d_namlen
, const char *d_name
)
2320 len
= _DIRENT_RECLEN(d_namlen
);
2321 if (len
> uio
->uio_resid
)
2324 dp
= kmalloc(len
, M_TEMP
, M_WAITOK
| M_ZERO
);
2327 dp
->d_namlen
= d_namlen
;
2328 dp
->d_type
= d_type
;
2329 bcopy(d_name
, dp
->d_name
, d_namlen
);
2331 *error
= uiomove((caddr_t
)dp
, len
, uio
);
2339 vn_mark_atime(struct vnode
*vp
, struct thread
*td
)
2341 struct proc
*p
= td
->td_proc
;
2342 struct ucred
*cred
= p
? p
->p_ucred
: proc0
.p_ucred
;
2344 if ((vp
->v_mount
->mnt_flag
& (MNT_NOATIME
| MNT_RDONLY
)) == 0) {
2345 VOP_MARKATIME(vp
, cred
);