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 "opt_inet6.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
50 #include <sys/dirent.h>
51 #include <sys/endian.h>
52 #include <sys/eventhandler.h>
53 #include <sys/fcntl.h>
55 #include <sys/kernel.h>
56 #include <sys/kthread.h>
57 #include <sys/malloc.h>
59 #include <sys/mount.h>
62 #include <sys/reboot.h>
63 #include <sys/socket.h>
65 #include <sys/sysctl.h>
66 #include <sys/syslog.h>
67 #include <sys/unistd.h>
68 #include <sys/vmmeter.h>
69 #include <sys/vnode.h>
71 #include <machine/limits.h>
74 #include <vm/vm_object.h>
75 #include <vm/vm_extern.h>
76 #include <vm/vm_kern.h>
78 #include <vm/vm_map.h>
79 #include <vm/vm_page.h>
80 #include <vm/vm_pager.h>
81 #include <vm/vnode_pager.h>
82 #include <vm/vm_zone.h>
85 #include <vm/vm_page2.h>
87 #include <netinet/in.h>
89 static MALLOC_DEFINE(M_NETCRED
, "Export Host", "Export host address structure");
91 __read_mostly
int numvnodes
;
92 SYSCTL_INT(_debug
, OID_AUTO
, numvnodes
, CTLFLAG_RD
, &numvnodes
, 0,
93 "Number of vnodes allocated");
94 __read_mostly
int verbose_reclaims
;
95 SYSCTL_INT(_debug
, OID_AUTO
, verbose_reclaims
, CTLFLAG_RD
, &verbose_reclaims
, 0,
96 "Output filename of reclaimed vnode(s)");
98 __read_mostly
enum vtype iftovt_tab
[16] = {
99 VNON
, VFIFO
, VCHR
, VNON
, VDIR
, VNON
, VBLK
, VNON
,
100 VREG
, VNON
, VLNK
, VNON
, VSOCK
, VNON
, VNON
, VBAD
,
102 __read_mostly
int vttoif_tab
[9] = {
103 0, S_IFREG
, S_IFDIR
, S_IFBLK
, S_IFCHR
, S_IFLNK
,
104 S_IFSOCK
, S_IFIFO
, S_IFMT
,
107 static int reassignbufcalls
;
108 SYSCTL_INT(_vfs
, OID_AUTO
, reassignbufcalls
, CTLFLAG_RW
, &reassignbufcalls
,
109 0, "Number of times buffers have been reassigned to the proper list");
111 __read_mostly
static int check_buf_overlap
= 2; /* invasive check */
112 SYSCTL_INT(_vfs
, OID_AUTO
, check_buf_overlap
, CTLFLAG_RW
, &check_buf_overlap
,
113 0, "Enable overlapping buffer checks");
115 int nfs_mount_type
= -1;
116 static struct lwkt_token spechash_token
;
117 struct nfs_public nfs_pub
; /* publicly exported FS */
119 __read_mostly
int maxvnodes
;
120 SYSCTL_INT(_kern
, KERN_MAXVNODES
, maxvnodes
, CTLFLAG_RW
,
121 &maxvnodes
, 0, "Maximum number of vnodes");
123 static struct radix_node_head
*vfs_create_addrlist_af(int af
,
124 struct netexport
*nep
);
125 static void vfs_free_addrlist (struct netexport
*nep
);
126 static int vfs_free_netcred (struct radix_node
*rn
, void *w
);
127 static void vfs_free_addrlist_af (struct radix_node_head
**prnh
);
128 static int vfs_hang_addrlist (struct mount
*mp
, struct netexport
*nep
,
129 const struct export_args
*argp
);
130 static void vclean_vxlocked(struct vnode
*vp
, int flags
);
132 __read_mostly
int prtactive
= 0; /* 1 => print out reclaim of active vnodes */
135 * Red black tree functions
137 static int rb_buf_compare(struct buf
*b1
, struct buf
*b2
);
138 RB_GENERATE2(buf_rb_tree
, buf
, b_rbnode
, rb_buf_compare
, off_t
, b_loffset
);
139 RB_GENERATE2(buf_rb_hash
, buf
, b_rbhash
, rb_buf_compare
, off_t
, b_loffset
);
142 rb_buf_compare(struct buf
*b1
, struct buf
*b2
)
144 if (b1
->b_loffset
< b2
->b_loffset
)
146 if (b1
->b_loffset
> b2
->b_loffset
)
152 * Initialize the vnode management data structures.
154 * Called from vfsinit()
156 #define VNBREAKMEM1 (1L * 1024 * 1024 * 1024)
157 #define VNBREAKMEM2 (7L * 1024 * 1024 * 1024)
158 #define MINVNODES 2000
159 #define MAXVNODES 4000000
164 int factor1
; /* Limit based on ram (x 2 above 1GB) */
168 * Size maxvnodes non-linearly to available memory. Don't bloat
169 * the count on low-memory systems. Scale up for systems with
170 * more than 1G and more than 8G of ram, but do so non-linearly
171 * because the value of a large maxvnodes count diminishes
172 * significantly beyond a certain point.
174 * The general minimum is maxproc * 8 (we want someone pushing
175 * up maxproc a lot to also get more vnodes). Usually maxproc
176 * does not affect this calculation. The KvaSize limitation also
177 * typically does not affect this calculation (it is just in case
178 * the kernel VM space is made much smaller than main memory, which
179 * should no longer happen on 64-bit systems).
181 * There isn't much of a point allowing maxvnodes to exceed a
182 * few million as modern filesystems cache pages in the
183 * underlying block device and not so much hanging off of VM
186 * Also, VM objects, vnodes, and filesystem inode and other related
187 * structures have gotten a lot larger in recent years and the kernel
188 * memory use tends to scale with maxvnodes, so we don't want to bloat
189 * it too much. But neither do we want the max set too low because
190 * systems with large amounts of memory and cores are capable of
191 * doing a hell of a lot.
193 factor1
= 80 * (sizeof(struct vm_object
) + sizeof(struct vnode
));
195 freemem
= (int64_t)vmstats
.v_page_count
* PAGE_SIZE
;
197 maxvnodes
= freemem
/ factor1
;
198 if (freemem
> VNBREAKMEM1
) {
199 freemem
-= VNBREAKMEM1
;
200 if (freemem
< VNBREAKMEM2
) {
201 maxvnodes
+= freemem
/ factor1
/ 2;
203 maxvnodes
+= VNBREAKMEM2
/ factor1
/ 2;
204 freemem
-= VNBREAKMEM2
;
205 maxvnodes
+= freemem
/ factor1
/ 4;
208 maxvnodes
= imax(maxvnodes
, maxproc
* 8);
209 maxvnodes
= imin(maxvnodes
, KvaSize
/ factor1
);
210 maxvnodes
= imin(maxvnodes
, MAXVNODES
);
211 maxvnodes
= imax(maxvnodes
, MINVNODES
);
213 lwkt_token_init(&spechash_token
, "spechash");
217 * Knob to control the precision of file timestamps:
219 * 0 = seconds only; nanoseconds zeroed.
220 * 1 = microseconds accurate to tick precision
221 * 2 = microseconds accurate to tick precision (default, hz >= 100)
222 * 3 = nanoseconds accurate to tick precision
223 * 4 = microseconds, maximum precision (default, hz < 100)
224 * 5 = nanoseconds, maximum precision
226 * Note that utimes() precision is microseconds because it takes a timeval
227 * structure, so its probably best to default to USEC or USEC_PRECISE, and
230 enum { TSP_SEC
, TSP_HZ
, TSP_USEC
, TSP_NSEC
,
231 TSP_USEC_PRECISE
, TSP_NSEC_PRECISE
};
233 __read_mostly
static int timestamp_precision
= -1;
234 SYSCTL_INT(_vfs
, OID_AUTO
, timestamp_precision
, CTLFLAG_RW
,
235 ×tamp_precision
, 0, "Precision of file timestamps");
238 * Get a current timestamp.
243 vfs_timestamp(struct timespec
*tsp
)
245 switch (timestamp_precision
) {
246 case TSP_SEC
: /* seconds precision */
250 case TSP_HZ
: /* ticks precision (limit to microseconds) */
252 tsp
->tv_nsec
-= tsp
->tv_nsec
% 1000;
255 case TSP_USEC
: /* microseconds (ticks precision) */
257 tsp
->tv_nsec
-= tsp
->tv_nsec
% 1000;
259 case TSP_NSEC
: /* nanoseconds (ticks precision) */
262 case TSP_USEC_PRECISE
: /* microseconds (high preceision) */
264 tsp
->tv_nsec
-= tsp
->tv_nsec
% 1000;
266 case TSP_NSEC_PRECISE
: /* nanoseconds (high precision) */
273 * Set vnode attributes to VNOVAL
276 vattr_null(struct vattr
*vap
)
279 vap
->va_size
= VNOVAL
;
280 vap
->va_bytes
= VNOVAL
;
281 vap
->va_mode
= VNOVAL
;
282 vap
->va_nlink
= VNOVAL
;
283 vap
->va_uid
= VNOVAL
;
284 vap
->va_gid
= VNOVAL
;
285 vap
->va_fsid
= VNOVAL
;
286 vap
->va_fileid
= VNOVAL
;
287 vap
->va_blocksize
= VNOVAL
;
288 vap
->va_rmajor
= VNOVAL
;
289 vap
->va_rminor
= VNOVAL
;
290 vap
->va_atime
.tv_sec
= VNOVAL
;
291 vap
->va_atime
.tv_nsec
= VNOVAL
;
292 vap
->va_mtime
.tv_sec
= VNOVAL
;
293 vap
->va_mtime
.tv_nsec
= VNOVAL
;
294 vap
->va_ctime
.tv_sec
= VNOVAL
;
295 vap
->va_ctime
.tv_nsec
= VNOVAL
;
296 vap
->va_flags
= VNOVAL
;
297 vap
->va_gen
= VNOVAL
;
299 /* va_*_uuid fields are only valid if related flags are set */
303 * Flush out and invalidate all buffers associated with a vnode.
307 static int vinvalbuf_bp(struct buf
*bp
, void *data
);
309 struct vinvalbuf_bp_info
{
318 vinvalbuf(struct vnode
*vp
, int flags
, int slpflag
, int slptimeo
)
320 struct vinvalbuf_bp_info info
;
324 lwkt_gettoken(&vp
->v_token
);
327 * If we are being asked to save, call fsync to ensure that the inode
330 if (flags
& V_SAVE
) {
331 error
= bio_track_wait(&vp
->v_track_write
, slpflag
, slptimeo
);
334 if (!RB_EMPTY(&vp
->v_rbdirty_tree
)) {
335 if ((error
= VOP_FSYNC(vp
, MNT_WAIT
, 0)) != 0)
339 * Dirty bufs may be left or generated via races
340 * in circumstances where vinvalbuf() is called on
341 * a vnode not undergoing reclamation. Only
342 * panic if we are trying to reclaim the vnode.
344 if ((vp
->v_flag
& VRECLAIMED
) &&
345 (bio_track_active(&vp
->v_track_write
) ||
346 !RB_EMPTY(&vp
->v_rbdirty_tree
))) {
347 panic("vinvalbuf: dirty bufs");
352 info
.slptimeo
= slptimeo
;
353 info
.lkflags
= LK_EXCLUSIVE
| LK_SLEEPFAIL
;
354 if (slpflag
& PCATCH
)
355 info
.lkflags
|= LK_PCATCH
;
360 * Flush the buffer cache until nothing is left, wait for all I/O
361 * to complete. At least one pass is required. We might block
362 * in the pip code so we have to re-check. Order is important.
368 if (!RB_EMPTY(&vp
->v_rbclean_tree
)) {
370 error
= RB_SCAN(buf_rb_tree
, &vp
->v_rbclean_tree
,
371 NULL
, vinvalbuf_bp
, &info
);
373 if (!RB_EMPTY(&vp
->v_rbdirty_tree
)) {
375 error
= RB_SCAN(buf_rb_tree
, &vp
->v_rbdirty_tree
,
376 NULL
, vinvalbuf_bp
, &info
);
380 * Wait for I/O completion.
382 bio_track_wait(&vp
->v_track_write
, 0, 0);
383 if ((object
= vp
->v_object
) != NULL
)
384 refcount_wait(&object
->paging_in_progress
, "vnvlbx");
385 } while (bio_track_active(&vp
->v_track_write
) ||
386 !RB_EMPTY(&vp
->v_rbclean_tree
) ||
387 !RB_EMPTY(&vp
->v_rbdirty_tree
));
390 * Destroy the copy in the VM cache, too.
392 if ((object
= vp
->v_object
) != NULL
) {
393 vm_object_page_remove(object
, 0, 0,
394 (flags
& V_SAVE
) ? TRUE
: FALSE
);
397 if (!RB_EMPTY(&vp
->v_rbdirty_tree
) || !RB_EMPTY(&vp
->v_rbclean_tree
))
398 panic("vinvalbuf: flush failed");
399 if (!RB_EMPTY(&vp
->v_rbhash_tree
))
400 panic("vinvalbuf: flush failed, buffers still present");
403 lwkt_reltoken(&vp
->v_token
);
408 vinvalbuf_bp(struct buf
*bp
, void *data
)
410 struct vinvalbuf_bp_info
*info
= data
;
413 if (BUF_LOCK(bp
, LK_EXCLUSIVE
| LK_NOWAIT
)) {
414 atomic_add_int(&bp
->b_refs
, 1);
415 error
= BUF_TIMELOCK(bp
, info
->lkflags
,
416 "vinvalbuf", info
->slptimeo
);
417 atomic_subtract_int(&bp
->b_refs
, 1);
426 KKASSERT(bp
->b_vp
== info
->vp
);
429 * Must check clean/dirty status after successfully locking as
432 if ((info
->clean
&& (bp
->b_flags
& B_DELWRI
)) ||
433 (info
->clean
== 0 && (bp
->b_flags
& B_DELWRI
) == 0)) {
439 * NOTE: NO B_LOCKED CHECK. Also no buf_checkwrite()
440 * check. This code will write out the buffer, period.
443 if (((bp
->b_flags
& (B_DELWRI
| B_INVAL
)) == B_DELWRI
) &&
444 (info
->flags
& V_SAVE
)) {
446 } else if (info
->flags
& V_SAVE
) {
448 * Cannot set B_NOCACHE on a clean buffer as this will
449 * destroy the VM backing store which might actually
450 * be dirty (and unsynchronized).
452 bp
->b_flags
|= (B_INVAL
| B_RELBUF
);
455 bp
->b_flags
|= (B_INVAL
| B_NOCACHE
| B_RELBUF
);
462 * Truncate a file's buffer and pages to a specified length. This
463 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
466 * The vnode must be locked.
468 static int vtruncbuf_bp_trunc_cmp(struct buf
*bp
, void *data
);
469 static int vtruncbuf_bp_trunc(struct buf
*bp
, void *data
);
470 static int vtruncbuf_bp_metasync_cmp(struct buf
*bp
, void *data
);
471 static int vtruncbuf_bp_metasync(struct buf
*bp
, void *data
);
473 struct vtruncbuf_info
{
480 vtruncbuf(struct vnode
*vp
, off_t length
, int blksize
)
482 struct vtruncbuf_info info
;
483 const char *filename
;
487 * Round up to the *next* block, then destroy the buffers in question.
488 * Since we are only removing some of the buffers we must rely on the
489 * scan count to determine whether a loop is necessary.
491 if ((count
= (int)(length
% blksize
)) != 0)
492 info
.truncloffset
= length
+ (blksize
- count
);
494 info
.truncloffset
= length
;
497 lwkt_gettoken(&vp
->v_token
);
500 count
= RB_SCAN(buf_rb_tree
, &vp
->v_rbclean_tree
,
501 vtruncbuf_bp_trunc_cmp
,
502 vtruncbuf_bp_trunc
, &info
);
504 count
+= RB_SCAN(buf_rb_tree
, &vp
->v_rbdirty_tree
,
505 vtruncbuf_bp_trunc_cmp
,
506 vtruncbuf_bp_trunc
, &info
);
510 * For safety, fsync any remaining metadata if the file is not being
511 * truncated to 0. Since the metadata does not represent the entire
512 * dirty list we have to rely on the hit count to ensure that we get
517 count
= RB_SCAN(buf_rb_tree
, &vp
->v_rbdirty_tree
,
518 vtruncbuf_bp_metasync_cmp
,
519 vtruncbuf_bp_metasync
, &info
);
524 * Clean out any left over VM backing store.
526 * It is possible to have in-progress I/O from buffers that were
527 * not part of the truncation. This should not happen if we
528 * are truncating to 0-length.
530 vnode_pager_setsize(vp
, length
);
531 bio_track_wait(&vp
->v_track_write
, 0, 0);
536 spin_lock(&vp
->v_spin
);
537 filename
= TAILQ_FIRST(&vp
->v_namecache
) ?
538 TAILQ_FIRST(&vp
->v_namecache
)->nc_name
: "?";
539 spin_unlock(&vp
->v_spin
);
542 * Make sure no buffers were instantiated while we were trying
543 * to clean out the remaining VM pages. This could occur due
544 * to busy dirty VM pages being flushed out to disk.
548 count
= RB_SCAN(buf_rb_tree
, &vp
->v_rbclean_tree
,
549 vtruncbuf_bp_trunc_cmp
,
550 vtruncbuf_bp_trunc
, &info
);
552 count
+= RB_SCAN(buf_rb_tree
, &vp
->v_rbdirty_tree
,
553 vtruncbuf_bp_trunc_cmp
,
554 vtruncbuf_bp_trunc
, &info
);
556 kprintf("Warning: vtruncbuf(): Had to re-clean %d "
557 "left over buffers in %s\n", count
, filename
);
561 lwkt_reltoken(&vp
->v_token
);
567 * The callback buffer is beyond the new file EOF and must be destroyed.
568 * Note that the compare function must conform to the RB_SCAN's requirements.
572 vtruncbuf_bp_trunc_cmp(struct buf
*bp
, void *data
)
574 struct vtruncbuf_info
*info
= data
;
576 if (bp
->b_loffset
>= info
->truncloffset
)
583 vtruncbuf_bp_trunc(struct buf
*bp
, void *data
)
585 struct vtruncbuf_info
*info
= data
;
588 * Do not try to use a buffer we cannot immediately lock, but sleep
589 * anyway to prevent a livelock. The code will loop until all buffers
592 * We must always revalidate the buffer after locking it to deal
595 if (BUF_LOCK(bp
, LK_EXCLUSIVE
| LK_NOWAIT
)) {
596 atomic_add_int(&bp
->b_refs
, 1);
597 if (BUF_LOCK(bp
, LK_EXCLUSIVE
|LK_SLEEPFAIL
) == 0)
599 atomic_subtract_int(&bp
->b_refs
, 1);
600 } else if ((info
->clean
&& (bp
->b_flags
& B_DELWRI
)) ||
601 (info
->clean
== 0 && (bp
->b_flags
& B_DELWRI
) == 0) ||
602 bp
->b_vp
!= info
->vp
||
603 vtruncbuf_bp_trunc_cmp(bp
, data
)) {
607 bp
->b_flags
|= (B_INVAL
| B_RELBUF
| B_NOCACHE
);
614 * Fsync all meta-data after truncating a file to be non-zero. Only metadata
615 * blocks (with a negative loffset) are scanned.
616 * Note that the compare function must conform to the RB_SCAN's requirements.
619 vtruncbuf_bp_metasync_cmp(struct buf
*bp
, void *data __unused
)
621 if (bp
->b_loffset
< 0)
627 vtruncbuf_bp_metasync(struct buf
*bp
, void *data
)
629 struct vtruncbuf_info
*info
= data
;
631 if (BUF_LOCK(bp
, LK_EXCLUSIVE
| LK_NOWAIT
)) {
632 atomic_add_int(&bp
->b_refs
, 1);
633 if (BUF_LOCK(bp
, LK_EXCLUSIVE
|LK_SLEEPFAIL
) == 0)
635 atomic_subtract_int(&bp
->b_refs
, 1);
636 } else if ((bp
->b_flags
& B_DELWRI
) == 0 ||
637 bp
->b_vp
!= info
->vp
||
638 vtruncbuf_bp_metasync_cmp(bp
, data
)) {
642 if (bp
->b_vp
== info
->vp
)
651 * vfsync - implements a multipass fsync on a file which understands
652 * dependancies and meta-data. The passed vnode must be locked. The
653 * waitfor argument may be MNT_WAIT or MNT_NOWAIT, or MNT_LAZY.
655 * When fsyncing data asynchronously just do one consolidated pass starting
656 * with the most negative block number. This may not get all the data due
659 * When fsyncing data synchronously do a data pass, then a metadata pass,
660 * then do additional data+metadata passes to try to get all the data out.
662 * Caller must ref the vnode but does not have to lock it.
664 static int vfsync_wait_output(struct vnode
*vp
,
665 int (*waitoutput
)(struct vnode
*, struct thread
*));
666 static int vfsync_dummy_cmp(struct buf
*bp __unused
, void *data __unused
);
667 static int vfsync_data_only_cmp(struct buf
*bp
, void *data
);
668 static int vfsync_meta_only_cmp(struct buf
*bp
, void *data
);
669 static int vfsync_lazy_range_cmp(struct buf
*bp
, void *data
);
670 static int vfsync_bp(struct buf
*bp
, void *data
);
680 int (*checkdef
)(struct buf
*);
681 int (*cmpfunc
)(struct buf
*, void *);
685 vfsync(struct vnode
*vp
, int waitfor
, int passes
,
686 int (*checkdef
)(struct buf
*),
687 int (*waitoutput
)(struct vnode
*, struct thread
*))
689 struct vfsync_info info
;
692 bzero(&info
, sizeof(info
));
694 if ((info
.checkdef
= checkdef
) == NULL
)
697 lwkt_gettoken(&vp
->v_token
);
700 case MNT_LAZY
| MNT_NOWAIT
:
703 * Lazy (filesystem syncer typ) Asynchronous plus limit the
704 * number of data (not meta) pages we try to flush to 1MB.
705 * A non-zero return means that lazy limit was reached.
707 info
.lazylimit
= 1024 * 1024;
709 info
.cmpfunc
= vfsync_lazy_range_cmp
;
710 error
= RB_SCAN(buf_rb_tree
, &vp
->v_rbdirty_tree
,
711 vfsync_lazy_range_cmp
, vfsync_bp
, &info
);
712 info
.cmpfunc
= vfsync_meta_only_cmp
;
713 RB_SCAN(buf_rb_tree
, &vp
->v_rbdirty_tree
,
714 vfsync_meta_only_cmp
, vfsync_bp
, &info
);
717 else if (!RB_EMPTY(&vp
->v_rbdirty_tree
))
718 vn_syncer_add(vp
, 1);
723 * Asynchronous. Do a data-only pass and a meta-only pass.
726 info
.cmpfunc
= vfsync_data_only_cmp
;
727 RB_SCAN(buf_rb_tree
, &vp
->v_rbdirty_tree
, vfsync_data_only_cmp
,
729 info
.cmpfunc
= vfsync_meta_only_cmp
;
730 RB_SCAN(buf_rb_tree
, &vp
->v_rbdirty_tree
, vfsync_meta_only_cmp
,
736 * Synchronous. Do a data-only pass, then a meta-data+data
737 * pass, then additional integrated passes to try to get
738 * all the dependancies flushed.
740 info
.cmpfunc
= vfsync_data_only_cmp
;
742 RB_SCAN(buf_rb_tree
, &vp
->v_rbdirty_tree
, vfsync_data_only_cmp
,
745 error
= vfsync_wait_output(vp
, waitoutput
);
747 info
.skippedbufs
= 0;
748 info
.cmpfunc
= vfsync_dummy_cmp
;
749 RB_SCAN(buf_rb_tree
, &vp
->v_rbdirty_tree
, NULL
,
751 error
= vfsync_wait_output(vp
, waitoutput
);
752 if (info
.skippedbufs
) {
753 kprintf("Warning: vfsync skipped %d dirty "
756 ((info
.skippedbufs
> 1) ? "s" : ""));
759 while (error
== 0 && passes
> 0 &&
760 !RB_EMPTY(&vp
->v_rbdirty_tree
)
762 info
.skippedbufs
= 0;
764 info
.synchronous
= 1;
767 info
.cmpfunc
= vfsync_dummy_cmp
;
768 error
= RB_SCAN(buf_rb_tree
, &vp
->v_rbdirty_tree
, NULL
,
774 error
= vfsync_wait_output(vp
, waitoutput
);
775 if (info
.skippedbufs
&& passes
== 0) {
776 kprintf("Warning: vfsync skipped %d dirty "
777 "buf%s in final pass!\n",
779 ((info
.skippedbufs
> 1) ? "s" : ""));
784 * This case can occur normally because vnode lock might
787 if (!RB_EMPTY(&vp
->v_rbdirty_tree
))
788 kprintf("dirty bufs left after final pass\n");
792 lwkt_reltoken(&vp
->v_token
);
798 vfsync_wait_output(struct vnode
*vp
,
799 int (*waitoutput
)(struct vnode
*, struct thread
*))
803 error
= bio_track_wait(&vp
->v_track_write
, 0, 0);
805 error
= waitoutput(vp
, curthread
);
810 vfsync_dummy_cmp(struct buf
*bp __unused
, void *data __unused
)
816 vfsync_data_only_cmp(struct buf
*bp
, void *data
)
818 if (bp
->b_loffset
< 0)
824 vfsync_meta_only_cmp(struct buf
*bp
, void *data
)
826 if (bp
->b_loffset
< 0)
832 vfsync_lazy_range_cmp(struct buf
*bp
, void *data
)
834 struct vfsync_info
*info
= data
;
836 if (bp
->b_loffset
< info
->vp
->v_lazyw
)
842 vfsync_bp(struct buf
*bp
, void *data
)
844 struct vfsync_info
*info
= data
;
845 struct vnode
*vp
= info
->vp
;
848 if (info
->fastpass
) {
850 * Ignore buffers that we cannot immediately lock.
852 if (BUF_LOCK(bp
, LK_EXCLUSIVE
| LK_NOWAIT
)) {
854 * Removed BUF_TIMELOCK(..., 1), even a 1-tick
855 * delay can mess up performance
857 * Another reason is that during a dirty-buffer
858 * scan a clustered write can start I/O on buffers
859 * ahead of the scan, causing the scan to not
860 * get a lock here. Usually this means the write
861 * is already in progress so, in fact, we *want*
862 * to skip the buffer.
867 } else if (info
->synchronous
== 0) {
869 * Normal pass, give the buffer a little time to become
872 if (BUF_TIMELOCK(bp
, LK_EXCLUSIVE
, "bflst2", hz
/ 10)) {
878 * Synchronous pass, give the buffer a lot of time before
881 if (BUF_TIMELOCK(bp
, LK_EXCLUSIVE
, "bflst3", hz
* 10)) {
888 * We must revalidate the buffer after locking.
890 if ((bp
->b_flags
& B_DELWRI
) == 0 ||
891 bp
->b_vp
!= info
->vp
||
892 info
->cmpfunc(bp
, data
)) {
898 * If syncdeps is not set we do not try to write buffers which have
901 if (!info
->synchronous
&& info
->syncdeps
== 0 && info
->checkdef(bp
)) {
907 * B_NEEDCOMMIT (primarily used by NFS) is a state where the buffer
908 * has been written but an additional handshake with the device
909 * is required before we can dispose of the buffer. We have no idea
910 * how to do this so we have to skip these buffers.
912 if (bp
->b_flags
& B_NEEDCOMMIT
) {
918 * Ask bioops if it is ok to sync. If not the VFS may have
919 * set B_LOCKED so we have to cycle the buffer.
921 if (LIST_FIRST(&bp
->b_dep
) != NULL
&& buf_checkwrite(bp
)) {
927 if (info
->synchronous
) {
929 * Synchronous flush. An error may be returned and will
936 * Asynchronous flush. We use the error return to support
939 * In low-memory situations we revert to synchronous
940 * operation. This should theoretically prevent the I/O
941 * path from exhausting memory in a non-recoverable way.
943 vp
->v_lazyw
= bp
->b_loffset
;
945 if (vm_paging_min()) {
947 info
->lazycount
+= bp
->b_bufsize
;
951 info
->lazycount
+= cluster_awrite(bp
);
952 waitrunningbufspace();
953 /*vm_wait_nominal();*/
955 if (info
->lazylimit
&& info
->lazycount
>= info
->lazylimit
)
964 * Associate a buffer with a vnode.
969 bgetvp(struct vnode
*vp
, struct buf
*bp
, int testsize
)
971 KASSERT(bp
->b_vp
== NULL
, ("bgetvp: not free"));
972 KKASSERT((bp
->b_flags
& (B_HASHED
|B_DELWRI
|B_VNCLEAN
|B_VNDIRTY
)) == 0);
975 * Insert onto list for new vnode.
977 lwkt_gettoken(&vp
->v_token
);
979 if (buf_rb_hash_RB_INSERT(&vp
->v_rbhash_tree
, bp
)) {
980 lwkt_reltoken(&vp
->v_token
);
985 * Diagnostics (mainly for HAMMER debugging). Check for
986 * overlapping buffers.
988 if (check_buf_overlap
) {
990 bx
= buf_rb_hash_RB_PREV(bp
);
992 if (bx
->b_loffset
+ bx
->b_bufsize
> bp
->b_loffset
) {
993 kprintf("bgetvp: overlapl %016jx/%d %016jx "
995 (intmax_t)bx
->b_loffset
,
997 (intmax_t)bp
->b_loffset
,
999 if (check_buf_overlap
> 1)
1000 panic("bgetvp - overlapping buffer");
1003 bx
= buf_rb_hash_RB_NEXT(bp
);
1005 if (bp
->b_loffset
+ testsize
> bx
->b_loffset
) {
1006 kprintf("bgetvp: overlapr %016jx/%d %016jx "
1008 (intmax_t)bp
->b_loffset
,
1010 (intmax_t)bx
->b_loffset
,
1012 if (check_buf_overlap
> 1)
1013 panic("bgetvp - overlapping buffer");
1018 bp
->b_flags
|= B_HASHED
;
1019 bp
->b_flags
|= B_VNCLEAN
;
1020 if (buf_rb_tree_RB_INSERT(&vp
->v_rbclean_tree
, bp
))
1021 panic("reassignbuf: dup lblk/clean vp %p bp %p", vp
, bp
);
1023 lwkt_reltoken(&vp
->v_token
);
1028 * Disassociate a buffer from a vnode.
1033 brelvp(struct buf
*bp
)
1037 KASSERT(bp
->b_vp
!= NULL
, ("brelvp: NULL"));
1040 * Delete from old vnode list, if on one.
1043 lwkt_gettoken(&vp
->v_token
);
1044 if (bp
->b_flags
& (B_VNDIRTY
| B_VNCLEAN
)) {
1045 if (bp
->b_flags
& B_VNDIRTY
)
1046 buf_rb_tree_RB_REMOVE(&vp
->v_rbdirty_tree
, bp
);
1048 buf_rb_tree_RB_REMOVE(&vp
->v_rbclean_tree
, bp
);
1049 bp
->b_flags
&= ~(B_VNDIRTY
| B_VNCLEAN
);
1051 if (bp
->b_flags
& B_HASHED
) {
1052 buf_rb_hash_RB_REMOVE(&vp
->v_rbhash_tree
, bp
);
1053 bp
->b_flags
&= ~B_HASHED
;
1057 * Only remove from synclist when no dirty buffers are left AND
1058 * the VFS has not flagged the vnode's inode as being dirty.
1060 if ((vp
->v_flag
& (VONWORKLST
| VISDIRTY
| VOBJDIRTY
)) == VONWORKLST
&&
1061 RB_EMPTY(&vp
->v_rbdirty_tree
)) {
1062 vn_syncer_remove(vp
, 0);
1066 lwkt_reltoken(&vp
->v_token
);
1072 * Reassign the buffer to the proper clean/dirty list based on B_DELWRI.
1073 * This routine is called when the state of the B_DELWRI bit is changed.
1075 * Must be called with vp->v_token held.
1079 reassignbuf(struct buf
*bp
)
1081 struct vnode
*vp
= bp
->b_vp
;
1084 ASSERT_LWKT_TOKEN_HELD(&vp
->v_token
);
1088 * B_PAGING flagged buffers cannot be reassigned because their vp
1089 * is not fully linked in.
1091 if (bp
->b_flags
& B_PAGING
)
1092 panic("cannot reassign paging buffer");
1094 if (bp
->b_flags
& B_DELWRI
) {
1096 * Move to the dirty list, add the vnode to the worklist
1098 if (bp
->b_flags
& B_VNCLEAN
) {
1099 buf_rb_tree_RB_REMOVE(&vp
->v_rbclean_tree
, bp
);
1100 bp
->b_flags
&= ~B_VNCLEAN
;
1102 if ((bp
->b_flags
& B_VNDIRTY
) == 0) {
1103 if (buf_rb_tree_RB_INSERT(&vp
->v_rbdirty_tree
, bp
)) {
1104 panic("reassignbuf: dup lblk vp %p bp %p",
1107 bp
->b_flags
|= B_VNDIRTY
;
1109 if ((vp
->v_flag
& VONWORKLST
) == 0) {
1110 switch (vp
->v_type
) {
1117 vp
->v_rdev
->si_mountpoint
!= NULL
) {
1125 vn_syncer_add(vp
, delay
);
1129 * Move to the clean list, remove the vnode from the worklist
1130 * if no dirty blocks remain.
1132 if (bp
->b_flags
& B_VNDIRTY
) {
1133 buf_rb_tree_RB_REMOVE(&vp
->v_rbdirty_tree
, bp
);
1134 bp
->b_flags
&= ~B_VNDIRTY
;
1136 if ((bp
->b_flags
& B_VNCLEAN
) == 0) {
1137 if (buf_rb_tree_RB_INSERT(&vp
->v_rbclean_tree
, bp
)) {
1138 panic("reassignbuf: dup lblk vp %p bp %p",
1141 bp
->b_flags
|= B_VNCLEAN
;
1145 * Only remove from synclist when no dirty buffers are left
1146 * AND the VFS has not flagged the vnode's inode as being
1149 if ((vp
->v_flag
& (VONWORKLST
| VISDIRTY
| VOBJDIRTY
)) ==
1151 RB_EMPTY(&vp
->v_rbdirty_tree
)) {
1152 vn_syncer_remove(vp
, 0);
1158 * Create a vnode for a block device. Used for mounting the root file
1161 * A vref()'d vnode is returned.
1163 extern struct vop_ops
*devfs_vnode_dev_vops_p
;
1165 bdevvp(cdev_t dev
, struct vnode
**vpp
)
1175 error
= getspecialvnode(VT_NON
, NULL
, &devfs_vnode_dev_vops_p
,
1186 v_associate_rdev(vp
, dev
);
1187 vp
->v_umajor
= dev
->si_umajor
;
1188 vp
->v_uminor
= dev
->si_uminor
;
1195 v_associate_rdev(struct vnode
*vp
, cdev_t dev
)
1199 if (dev_is_good(dev
) == 0)
1201 KKASSERT(vp
->v_rdev
== NULL
);
1202 vp
->v_rdev
= reference_dev(dev
);
1203 lwkt_gettoken(&spechash_token
);
1204 SLIST_INSERT_HEAD(&dev
->si_hlist
, vp
, v_cdevnext
);
1205 lwkt_reltoken(&spechash_token
);
1210 v_release_rdev(struct vnode
*vp
)
1214 if ((dev
= vp
->v_rdev
) != NULL
) {
1215 lwkt_gettoken(&spechash_token
);
1216 SLIST_REMOVE(&dev
->si_hlist
, vp
, vnode
, v_cdevnext
);
1219 lwkt_reltoken(&spechash_token
);
1224 * Add a vnode to the alias list hung off the cdev_t. We only associate
1225 * the device number with the vnode. The actual device is not associated
1226 * until the vnode is opened (usually in spec_open()), and will be
1227 * disassociated on last close.
1230 addaliasu(struct vnode
*nvp
, int x
, int y
)
1232 if (nvp
->v_type
!= VBLK
&& nvp
->v_type
!= VCHR
)
1233 panic("addaliasu on non-special vnode");
1239 * Simple call that a filesystem can make to try to get rid of a
1240 * vnode. It will fail if anyone is referencing the vnode (including
1243 * The filesystem can check whether its in-memory inode structure still
1244 * references the vp on return.
1246 * May only be called if the vnode is in a known state (i.e. being prevented
1247 * from being deallocated by some other condition such as a vfs inode hold).
1249 * This call might not succeed.
1252 vclean_unlocked(struct vnode
*vp
)
1255 if (VREFCNT(vp
) <= 1)
1261 * Disassociate a vnode from its underlying filesystem.
1263 * The vnode must be VX locked and referenced. In all normal situations
1264 * there are no active references. If vclean_vxlocked() is called while
1265 * there are active references, the vnode is being ripped out and we have
1266 * to call VOP_CLOSE() as appropriate before we can reclaim it.
1269 vclean_vxlocked(struct vnode
*vp
, int flags
)
1274 struct namecache
*ncp
;
1277 * If the vnode has already been reclaimed we have nothing to do.
1279 if (vp
->v_flag
& VRECLAIMED
)
1283 * Set flag to interlock operation, flag finalization to ensure
1284 * that the vnode winds up on the inactive list, and set v_act to 0.
1286 vsetflags(vp
, VRECLAIMED
);
1287 atomic_set_int(&vp
->v_refcnt
, VREF_FINALIZE
);
1290 if (verbose_reclaims
) {
1291 if ((ncp
= TAILQ_FIRST(&vp
->v_namecache
)) != NULL
)
1292 kprintf("Debug: reclaim %p %s\n", vp
, ncp
->nc_name
);
1296 * Scrap the vfs cache
1298 while (cache_inval_vp(vp
, 0) != 0) {
1299 kprintf("Warning: vnode %p clean/cache_resolution "
1300 "race detected\n", vp
);
1301 tsleep(vp
, 0, "vclninv", 2);
1305 * Check to see if the vnode is in use. If so we have to reference it
1306 * before we clean it out so that its count cannot fall to zero and
1307 * generate a race against ourselves to recycle it.
1309 active
= (VREFCNT(vp
) > 0);
1312 * Clean out any buffers associated with the vnode and destroy its
1313 * object, if it has one.
1315 vinvalbuf(vp
, V_SAVE
, 0, 0);
1318 * If purging an active vnode (typically during a forced unmount
1319 * or reboot), it must be closed and deactivated before being
1320 * reclaimed. This isn't really all that safe, but what can
1323 * Note that neither of these routines unlocks the vnode.
1325 if (active
&& (flags
& DOCLOSE
)) {
1326 while ((n
= vp
->v_opencount
) != 0) {
1327 if (vp
->v_writecount
)
1328 VOP_CLOSE(vp
, FWRITE
|FNONBLOCK
, NULL
);
1330 VOP_CLOSE(vp
, FNONBLOCK
, NULL
);
1331 if (vp
->v_opencount
== n
) {
1332 kprintf("Warning: unable to force-close"
1340 * If the vnode has not been deactivated, deactivated it. Deactivation
1341 * can create new buffers and VM pages so we have to call vinvalbuf()
1342 * again to make sure they all get flushed.
1344 * This can occur if a file with a link count of 0 needs to be
1347 * If the vnode is already dead don't try to deactivate it.
1349 if ((vp
->v_flag
& VINACTIVE
) == 0) {
1350 vsetflags(vp
, VINACTIVE
);
1353 vinvalbuf(vp
, V_SAVE
, 0, 0);
1357 * If the vnode has an object, destroy it.
1359 while ((object
= vp
->v_object
) != NULL
) {
1360 vm_object_hold(object
);
1361 if (object
== vp
->v_object
)
1363 vm_object_drop(object
);
1366 if (object
!= NULL
) {
1367 if (object
->ref_count
== 0) {
1368 if ((object
->flags
& OBJ_DEAD
) == 0)
1369 vm_object_terminate(object
);
1370 vm_object_drop(object
);
1371 vclrflags(vp
, VOBJBUF
);
1373 vm_pager_deallocate(object
);
1374 vclrflags(vp
, VOBJBUF
);
1375 vm_object_drop(object
);
1378 KKASSERT((vp
->v_flag
& VOBJBUF
) == 0);
1380 if (vp
->v_flag
& VOBJDIRTY
)
1384 * Reclaim the vnode if not already dead.
1386 if (vp
->v_mount
&& VOP_RECLAIM(vp
))
1387 panic("vclean: cannot reclaim");
1390 * Done with purge, notify sleepers of the grim news.
1392 vp
->v_ops
= &dead_vnode_vops_p
;
1397 * If we are destroying an active vnode, reactivate it now that
1398 * we have reassociated it with deadfs. This prevents the system
1399 * from crashing on the vnode due to it being unexpectedly marked
1400 * as inactive or reclaimed.
1402 if (active
&& (flags
& DOCLOSE
)) {
1403 vclrflags(vp
, VINACTIVE
| VRECLAIMED
);
1408 * Eliminate all activity associated with the requested vnode
1409 * and with all vnodes aliased to the requested vnode.
1411 * The vnode must be referenced but should not be locked.
1414 vrevoke(struct vnode
*vp
, struct ucred
*cred
)
1422 * If the vnode has a device association, scrap all vnodes associated
1423 * with the device. Don't let the device disappear on us while we
1424 * are scrapping the vnodes.
1426 * The passed vp will probably show up in the list, do not VX lock
1429 * Releasing the vnode's rdev here can mess up specfs's call to
1430 * device close, so don't do it. The vnode has been disassociated
1431 * and the device will be closed after the last ref on the related
1432 * fp goes away (if not still open by e.g. the kernel).
1434 if (vp
->v_type
!= VCHR
) {
1435 error
= fdrevoke(vp
, DTYPE_VNODE
, cred
);
1438 if ((dev
= vp
->v_rdev
) == NULL
) {
1442 lwkt_gettoken(&spechash_token
);
1445 vqn
= SLIST_FIRST(&dev
->si_hlist
);
1448 while ((vq
= vqn
) != NULL
) {
1449 if (VREFCNT(vq
) > 0) {
1451 fdrevoke(vq
, DTYPE_VNODE
, cred
);
1452 /*v_release_rdev(vq);*/
1454 if (vq
->v_rdev
!= dev
) {
1459 vqn
= SLIST_NEXT(vq
, v_cdevnext
);
1464 lwkt_reltoken(&spechash_token
);
1471 * This is called when the object underlying a vnode is being destroyed,
1472 * such as in a remove(). Try to recycle the vnode immediately if the
1473 * only active reference is our reference.
1475 * Directory vnodes in the namecache with children cannot be immediately
1476 * recycled because numerous VOP_N*() ops require them to be stable.
1478 * To avoid recursive recycling from VOP_INACTIVE implemenetations this
1479 * function is a NOP if VRECLAIMED is already set.
1482 vrecycle(struct vnode
*vp
)
1484 if (VREFCNT(vp
) <= 1 && (vp
->v_flag
& VRECLAIMED
) == 0) {
1485 if (cache_inval_vp_nonblock(vp
))
1494 * Return the maximum I/O size allowed for strategy calls on VP.
1496 * If vp is VCHR or VBLK we dive the device, otherwise we use
1497 * the vp's mount info.
1499 * The returned value is clamped at MAXPHYS as most callers cannot use
1500 * buffers larger than that size.
1503 vmaxiosize(struct vnode
*vp
)
1507 if (vp
->v_type
== VBLK
|| vp
->v_type
== VCHR
)
1508 maxiosize
= vp
->v_rdev
->si_iosize_max
;
1510 maxiosize
= vp
->v_mount
->mnt_iosize_max
;
1512 if (maxiosize
> MAXPHYS
)
1513 maxiosize
= MAXPHYS
;
1518 * Eliminate all activity associated with a vnode in preparation for
1521 * The vnode must be VX locked and refd and will remain VX locked and refd
1522 * on return. This routine may be called with the vnode in any state, as
1523 * long as it is VX locked. The vnode will be cleaned out and marked
1524 * VRECLAIMED but will not actually be reused until all existing refs and
1527 * NOTE: This routine may be called on a vnode which has not yet been
1528 * already been deactivated (VOP_INACTIVE), or on a vnode which has
1529 * already been reclaimed.
1531 * This routine is not responsible for placing us back on the freelist.
1532 * Instead, it happens automatically when the caller releases the VX lock
1533 * (assuming there aren't any other references).
1536 vgone_vxlocked(struct vnode
*vp
)
1539 * assert that the VX lock is held. This is an absolute requirement
1540 * now for vgone_vxlocked() to be called.
1542 KKASSERT(lockinuse(&vp
->v_lock
));
1545 * Clean out the filesystem specific data and set the VRECLAIMED
1546 * bit. Also deactivate the vnode if necessary.
1548 * The vnode should have automatically been removed from the syncer
1549 * list as syncer/dirty flags cleared during the cleaning.
1551 vclean_vxlocked(vp
, DOCLOSE
);
1554 * Normally panic if the vnode is still dirty, unless we are doing
1555 * a forced unmount (tmpfs typically).
1557 if (vp
->v_flag
& VONWORKLST
) {
1558 if (vp
->v_mount
->mnt_kern_flag
& MNTK_UNMOUNTF
) {
1560 vn_syncer_remove(vp
, 1);
1562 panic("vp %p still dirty in vgone after flush", vp
);
1567 * Delete from old mount point vnode list, if on one.
1569 if (vp
->v_mount
!= NULL
) {
1570 KKASSERT(vp
->v_data
== NULL
);
1571 insmntque(vp
, NULL
);
1575 * If special device, remove it from special device alias list
1576 * if it is on one. This should normally only occur if a vnode is
1577 * being revoked as the device should otherwise have been released
1580 if ((vp
->v_type
== VBLK
|| vp
->v_type
== VCHR
) && vp
->v_rdev
!= NULL
) {
1591 * Calculate the total number of references to a special device. This
1592 * routine may only be called for VBLK and VCHR vnodes since v_rdev is
1593 * an overloaded field. Since dev_from_devid() can now return NULL, we
1594 * have to check for a NULL v_rdev.
1597 count_dev(cdev_t dev
)
1602 if (SLIST_FIRST(&dev
->si_hlist
)) {
1603 lwkt_gettoken(&spechash_token
);
1604 SLIST_FOREACH(vp
, &dev
->si_hlist
, v_cdevnext
) {
1605 count
+= vp
->v_opencount
;
1607 lwkt_reltoken(&spechash_token
);
1613 vcount(struct vnode
*vp
)
1615 if (vp
->v_rdev
== NULL
)
1617 return(count_dev(vp
->v_rdev
));
1621 * Initialize VMIO for a vnode. This routine MUST be called before a
1622 * VFS can issue buffer cache ops on a vnode. It is typically called
1623 * when a vnode is initialized from its inode.
1626 vinitvmio(struct vnode
*vp
, off_t filesize
, int blksize
, int boff
)
1631 object
= vp
->v_object
;
1633 vm_object_hold(object
);
1634 KKASSERT(vp
->v_object
== object
);
1637 if (object
== NULL
) {
1638 object
= vnode_pager_alloc(vp
, filesize
, 0, 0, blksize
, boff
);
1641 * Dereference the reference we just created. This assumes
1642 * that the object is associated with the vp. Allow it to
1643 * have zero refs. It cannot be destroyed as long as it
1644 * is associated with the vnode.
1646 vm_object_hold(object
);
1647 atomic_add_int(&object
->ref_count
, -1);
1650 KKASSERT((object
->flags
& OBJ_DEAD
) == 0);
1652 KASSERT(vp
->v_object
!= NULL
, ("vinitvmio: NULL object"));
1653 vsetflags(vp
, VOBJBUF
);
1654 vm_object_drop(object
);
1661 * Print out a description of a vnode.
1663 static char *typename
[] =
1664 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD"};
1667 vprint(char *label
, struct vnode
*vp
)
1672 kprintf("%s: %p: ", label
, (void *)vp
);
1674 kprintf("%p: ", (void *)vp
);
1675 kprintf("type %s, refcnt %08x, writecount %d, holdcnt %d,",
1676 typename
[vp
->v_type
],
1677 vp
->v_refcnt
, vp
->v_writecount
, vp
->v_auxrefs
);
1679 if (vp
->v_flag
& VROOT
)
1680 strcat(buf
, "|VROOT");
1681 if (vp
->v_flag
& VPFSROOT
)
1682 strcat(buf
, "|VPFSROOT");
1683 if (vp
->v_flag
& VTEXT
)
1684 strcat(buf
, "|VTEXT");
1685 if (vp
->v_flag
& VSYSTEM
)
1686 strcat(buf
, "|VSYSTEM");
1687 if (vp
->v_flag
& VOBJBUF
)
1688 strcat(buf
, "|VOBJBUF");
1690 kprintf(" flags (%s)", &buf
[1]);
1691 if (vp
->v_data
== NULL
) {
1700 * Do the usual access checking.
1701 * file_mode, uid and gid are from the vnode in question,
1702 * while acc_mode and cred are from the VOP_ACCESS parameter list
1705 vaccess(enum vtype type
, mode_t file_mode
, uid_t uid
, gid_t gid
,
1706 mode_t acc_mode
, struct ucred
*cred
)
1712 * Super-user always gets read/write access, but execute access depends
1713 * on at least one execute bit being set.
1715 if (priv_check_cred(cred
, PRIV_ROOT
, 0) == 0) {
1716 if ((acc_mode
& VEXEC
) && type
!= VDIR
&&
1717 (file_mode
& (S_IXUSR
|S_IXGRP
|S_IXOTH
)) == 0)
1724 /* Otherwise, check the owner. */
1725 if (cred
->cr_uid
== uid
) {
1726 if (acc_mode
& VEXEC
)
1728 if (acc_mode
& VREAD
)
1730 if (acc_mode
& VWRITE
)
1732 return ((file_mode
& mask
) == mask
? 0 : EACCES
);
1735 /* Otherwise, check the groups. */
1736 ismember
= groupmember(gid
, cred
);
1737 if (cred
->cr_svgid
== gid
|| ismember
) {
1738 if (acc_mode
& VEXEC
)
1740 if (acc_mode
& VREAD
)
1742 if (acc_mode
& VWRITE
)
1744 return ((file_mode
& mask
) == mask
? 0 : EACCES
);
1747 /* Otherwise, check everyone else. */
1748 if (acc_mode
& VEXEC
)
1750 if (acc_mode
& VREAD
)
1752 if (acc_mode
& VWRITE
)
1754 return ((file_mode
& mask
) == mask
? 0 : EACCES
);
1758 #include <ddb/ddb.h>
1760 static int db_show_locked_vnodes(struct mount
*mp
, void *data
);
1763 * List all of the locked vnodes in the system.
1764 * Called when debugging the kernel.
1766 DB_SHOW_COMMAND(lockedvnodes
, lockedvnodes
)
1768 kprintf("Locked vnodes\n");
1769 mountlist_scan(db_show_locked_vnodes
, NULL
,
1770 MNTSCAN_FORWARD
|MNTSCAN_NOBUSY
);
1774 db_show_locked_vnodes(struct mount
*mp
, void *data __unused
)
1778 TAILQ_FOREACH(vp
, &mp
->mnt_nvnodelist
, v_nmntvnodes
) {
1779 if (vn_islocked(vp
))
1787 * Top level filesystem related information gathering.
1789 static int sysctl_ovfs_conf (SYSCTL_HANDLER_ARGS
);
1792 vfs_sysctl(SYSCTL_HANDLER_ARGS
)
1794 int *name
= (int *)arg1
- 1; /* XXX */
1795 u_int namelen
= arg2
+ 1; /* XXX */
1796 struct vfsconf
*vfsp
;
1799 #if 1 || defined(COMPAT_PRELITE2)
1800 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
1802 return (sysctl_ovfs_conf(oidp
, arg1
, arg2
, req
));
1806 /* all sysctl names at this level are at least name and field */
1808 return (ENOTDIR
); /* overloaded */
1809 if (name
[0] != VFS_GENERIC
) {
1810 vfsp
= vfsconf_find_by_typenum(name
[0]);
1812 return (EOPNOTSUPP
);
1813 return ((*vfsp
->vfc_vfsops
->vfs_sysctl
)(&name
[1], namelen
- 1,
1814 oldp
, oldlenp
, newp
, newlen
, p
));
1818 case VFS_MAXTYPENUM
:
1821 maxtypenum
= vfsconf_get_maxtypenum();
1822 return (SYSCTL_OUT(req
, &maxtypenum
, sizeof(maxtypenum
)));
1825 return (ENOTDIR
); /* overloaded */
1826 vfsp
= vfsconf_find_by_typenum(name
[2]);
1828 return (EOPNOTSUPP
);
1829 return (SYSCTL_OUT(req
, vfsp
, sizeof *vfsp
));
1831 return (EOPNOTSUPP
);
1834 SYSCTL_NODE(_vfs
, VFS_GENERIC
, generic
, CTLFLAG_RD
, vfs_sysctl
,
1835 "Generic filesystem");
1837 #if 1 || defined(COMPAT_PRELITE2)
1840 sysctl_ovfs_conf_iter(struct vfsconf
*vfsp
, void *data
)
1843 struct ovfsconf ovfs
;
1844 struct sysctl_req
*req
= (struct sysctl_req
*) data
;
1846 bzero(&ovfs
, sizeof(ovfs
));
1847 ovfs
.vfc_vfsops
= vfsp
->vfc_vfsops
; /* XXX used as flag */
1848 strcpy(ovfs
.vfc_name
, vfsp
->vfc_name
);
1849 ovfs
.vfc_index
= vfsp
->vfc_typenum
;
1850 ovfs
.vfc_refcount
= vfsp
->vfc_refcount
;
1851 ovfs
.vfc_flags
= vfsp
->vfc_flags
;
1852 error
= SYSCTL_OUT(req
, &ovfs
, sizeof ovfs
);
1854 return error
; /* abort iteration with error code */
1856 return 0; /* continue iterating with next element */
1860 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS
)
1862 return vfsconf_each(sysctl_ovfs_conf_iter
, (void*)req
);
1865 #endif /* 1 || COMPAT_PRELITE2 */
1868 * Check to see if a filesystem is mounted on a block device.
1871 vfs_mountedon(struct vnode
*vp
)
1876 if (dev
!= NULL
&& dev
->si_mountpoint
)
1882 * Unmount all filesystems. The list is traversed in reverse order
1883 * of mounting to avoid dependencies.
1885 * We want the umountall to be able to break out of its loop if a
1886 * failure occurs, after scanning all possible mounts, so the callback
1887 * returns 0 on error.
1889 * NOTE: Do not call mountlist_remove(mp) on error any more, this will
1890 * confuse mountlist_scan()'s unbusy check.
1892 static int vfs_umountall_callback(struct mount
*mp
, void *data
);
1895 vfs_unmountall(int halting
)
1900 count
= mountlist_scan(vfs_umountall_callback
, &halting
,
1901 MNTSCAN_REVERSE
|MNTSCAN_NOBUSY
);
1907 vfs_umountall_callback(struct mount
*mp
, void *data
)
1910 int halting
= *(int *)data
;
1913 * NOTE: When halting, dounmount will disconnect but leave
1914 * certain mount points intact. e.g. devfs.
1916 error
= dounmount(mp
, MNT_FORCE
, halting
);
1918 kprintf("unmount of filesystem mounted from %s failed (",
1919 mp
->mnt_stat
.f_mntfromname
);
1923 kprintf("%d)\n", error
);
1931 * Checks the mount flags for parameter mp and put the names comma-separated
1932 * into a string buffer buf with a size limit specified by len.
1934 * It returns the number of bytes written into buf, and (*errorp) will be
1935 * set to 0, EINVAL (if passed length is 0), or ENOSPC (supplied buffer was
1936 * not large enough). The buffer will be 0-terminated if len was not 0.
1939 vfs_flagstostr(int flags
, const struct mountctl_opt
*optp
,
1940 char *buf
, size_t len
, int *errorp
)
1942 static const struct mountctl_opt optnames
[] = {
1943 { MNT_RDONLY
, "read-only" },
1944 { MNT_SYNCHRONOUS
, "synchronous" },
1945 { MNT_NOEXEC
, "noexec" },
1946 { MNT_NOSUID
, "nosuid" },
1947 { MNT_NODEV
, "nodev" },
1948 { MNT_AUTOMOUNTED
, "automounted" },
1949 { MNT_ASYNC
, "asynchronous" },
1950 { MNT_SUIDDIR
, "suiddir" },
1951 { MNT_SOFTDEP
, "soft-updates" },
1952 { MNT_NOSYMFOLLOW
, "nosymfollow" },
1953 { MNT_TRIM
, "trim" },
1954 { MNT_NOATIME
, "noatime" },
1955 { MNT_NOCLUSTERR
, "noclusterr" },
1956 { MNT_NOCLUSTERW
, "noclusterw" },
1957 { MNT_EXRDONLY
, "NFS read-only" },
1958 { MNT_EXPORTED
, "NFS exported" },
1959 /* Remaining NFS flags could come here */
1960 { MNT_LOCAL
, "local" },
1961 { MNT_QUOTA
, "with-quotas" },
1962 /* { MNT_ROOTFS, "rootfs" }, */
1963 /* { MNT_IGNORE, "ignore" }, */
1973 bleft
= len
- 1; /* leave room for trailing \0 */
1976 * Checks the size of the string. If it contains
1977 * any data, then we will append the new flags to
1980 actsize
= strlen(buf
);
1984 /* Default flags if no flags passed */
1988 if (bleft
< 0) { /* degenerate case, 0-length buffer */
1993 for (; flags
&& optp
->o_opt
; ++optp
) {
1994 if ((flags
& optp
->o_opt
) == 0)
1996 optlen
= strlen(optp
->o_name
);
1997 if (bwritten
|| actsize
> 0) {
2002 buf
[bwritten
++] = ',';
2003 buf
[bwritten
++] = ' ';
2006 if (bleft
< optlen
) {
2010 bcopy(optp
->o_name
, buf
+ bwritten
, optlen
);
2013 flags
&= ~optp
->o_opt
;
2017 * Space already reserved for trailing \0
2024 * Build hash lists of net addresses and hang them off the mount point.
2025 * Called by ufs_mount() to set up the lists of export addresses.
2028 vfs_hang_addrlist(struct mount
*mp
, struct netexport
*nep
,
2029 const struct export_args
*argp
)
2032 struct radix_node_head
*rnh
;
2034 struct radix_node
*rn
;
2035 struct sockaddr
*saddr
, *smask
= NULL
;
2038 if (argp
->ex_addrlen
== 0) {
2039 if (mp
->mnt_flag
& MNT_DEFEXPORTED
)
2041 np
= &nep
->ne_defexported
;
2042 np
->netc_exflags
= argp
->ex_flags
;
2043 np
->netc_anon
= argp
->ex_anon
;
2044 np
->netc_anon
.cr_ref
= 1;
2045 mp
->mnt_flag
|= MNT_DEFEXPORTED
;
2049 if (argp
->ex_addrlen
< 0 || argp
->ex_addrlen
> MLEN
)
2051 if (argp
->ex_masklen
< 0 || argp
->ex_masklen
> MLEN
)
2054 i
= sizeof(struct netcred
) + argp
->ex_addrlen
+ argp
->ex_masklen
;
2055 np
= (struct netcred
*)kmalloc(i
, M_NETCRED
, M_WAITOK
| M_ZERO
);
2056 saddr
= (struct sockaddr
*) (np
+ 1);
2057 if ((error
= copyin(argp
->ex_addr
, (caddr_t
) saddr
, argp
->ex_addrlen
)))
2059 if (saddr
->sa_len
> argp
->ex_addrlen
)
2060 saddr
->sa_len
= argp
->ex_addrlen
;
2061 if (argp
->ex_masklen
) {
2062 smask
= (struct sockaddr
*)((caddr_t
)saddr
+ argp
->ex_addrlen
);
2063 error
= copyin(argp
->ex_mask
, (caddr_t
)smask
, argp
->ex_masklen
);
2066 if (smask
->sa_len
> argp
->ex_masklen
)
2067 smask
->sa_len
= argp
->ex_masklen
;
2070 if (nep
->ne_maskhead
== NULL
) {
2071 if (!rn_inithead((void **)&nep
->ne_maskhead
, NULL
, 0)) {
2076 if ((rnh
= vfs_create_addrlist_af(saddr
->sa_family
, nep
)) == NULL
) {
2080 rn
= (*rnh
->rnh_addaddr
)((char *)saddr
, (char *)smask
, rnh
,
2083 if (rn
== NULL
|| np
!= (struct netcred
*)rn
) { /* already exists */
2087 np
->netc_exflags
= argp
->ex_flags
;
2088 np
->netc_anon
= argp
->ex_anon
;
2089 np
->netc_anon
.cr_ref
= 1;
2093 kfree(np
, M_NETCRED
);
2098 * Free netcred structures installed in the netexport
2101 vfs_free_netcred(struct radix_node
*rn
, void *w
)
2103 struct radix_node_head
*rnh
= (struct radix_node_head
*)w
;
2105 (*rnh
->rnh_deladdr
) (rn
->rn_key
, rn
->rn_mask
, rnh
);
2106 kfree(rn
, M_NETCRED
);
2112 * callback to free an element of the mask table installed in the
2113 * netexport. These may be created indirectly and are not netcred
2117 vfs_free_netcred_mask(struct radix_node
*rn
, void *w
)
2119 struct radix_node_head
*rnh
= (struct radix_node_head
*)w
;
2121 (*rnh
->rnh_deladdr
) (rn
->rn_key
, rn
->rn_mask
, rnh
);
2122 kfree(rn
, M_RTABLE
);
2127 static struct radix_node_head
*
2128 vfs_create_addrlist_af(int af
, struct netexport
*nep
)
2130 struct radix_node_head
*rnh
= NULL
;
2131 #if defined(INET) || defined(INET6)
2132 struct radix_node_head
*maskhead
= nep
->ne_maskhead
;
2136 NE_ASSERT_LOCKED(nep
);
2137 #if defined(INET) || defined(INET6)
2138 KKASSERT(maskhead
!= NULL
);
2143 if ((rnh
= nep
->ne_inethead
) == NULL
) {
2144 off
= offsetof(struct sockaddr_in
, sin_addr
) << 3;
2145 if (!rn_inithead((void **)&rnh
, maskhead
, off
))
2147 nep
->ne_inethead
= rnh
;
2153 if ((rnh
= nep
->ne_inet6head
) == NULL
) {
2154 off
= offsetof(struct sockaddr_in6
, sin6_addr
) << 3;
2155 if (!rn_inithead((void **)&rnh
, maskhead
, off
))
2157 nep
->ne_inet6head
= rnh
;
2166 * helper function for freeing netcred elements
2169 vfs_free_addrlist_af(struct radix_node_head
**prnh
)
2171 struct radix_node_head
*rnh
= *prnh
;
2173 (*rnh
->rnh_walktree
) (rnh
, vfs_free_netcred
, rnh
);
2174 kfree(rnh
, M_RTABLE
);
2179 * helper function for freeing mask elements
2182 vfs_free_addrlist_masks(struct radix_node_head
**prnh
)
2184 struct radix_node_head
*rnh
= *prnh
;
2186 (*rnh
->rnh_walktree
) (rnh
, vfs_free_netcred_mask
, rnh
);
2187 kfree(rnh
, M_RTABLE
);
2192 * Free the net address hash lists that are hanging off the mount points.
2195 vfs_free_addrlist(struct netexport
*nep
)
2198 if (nep
->ne_inethead
!= NULL
)
2199 vfs_free_addrlist_af(&nep
->ne_inethead
);
2200 if (nep
->ne_inet6head
!= NULL
)
2201 vfs_free_addrlist_af(&nep
->ne_inet6head
);
2202 if (nep
->ne_maskhead
)
2203 vfs_free_addrlist_masks(&nep
->ne_maskhead
);
2208 vfs_export(struct mount
*mp
, struct netexport
*nep
,
2209 const struct export_args
*argp
)
2213 if (argp
->ex_flags
& MNT_DELEXPORT
) {
2214 if (mp
->mnt_flag
& MNT_EXPUBLIC
) {
2215 vfs_setpublicfs(NULL
, NULL
, NULL
);
2216 mp
->mnt_flag
&= ~MNT_EXPUBLIC
;
2218 vfs_free_addrlist(nep
);
2219 mp
->mnt_flag
&= ~(MNT_EXPORTED
| MNT_DEFEXPORTED
);
2221 if (argp
->ex_flags
& MNT_EXPORTED
) {
2222 if (argp
->ex_flags
& MNT_EXPUBLIC
) {
2223 if ((error
= vfs_setpublicfs(mp
, nep
, argp
)) != 0)
2225 mp
->mnt_flag
|= MNT_EXPUBLIC
;
2227 if ((error
= vfs_hang_addrlist(mp
, nep
, argp
)))
2229 mp
->mnt_flag
|= MNT_EXPORTED
;
2236 * Set the publicly exported filesystem (WebNFS). Currently, only
2237 * one public filesystem is possible in the spec (RFC 2054 and 2055)
2240 vfs_setpublicfs(struct mount
*mp
, struct netexport
*nep
,
2241 const struct export_args
*argp
)
2248 * mp == NULL -> invalidate the current info, the FS is
2249 * no longer exported. May be called from either vfs_export
2250 * or unmount, so check if it hasn't already been done.
2253 if (nfs_pub
.np_valid
) {
2254 nfs_pub
.np_valid
= 0;
2255 if (nfs_pub
.np_index
!= NULL
) {
2256 kfree(nfs_pub
.np_index
, M_TEMP
);
2257 nfs_pub
.np_index
= NULL
;
2264 * Only one allowed at a time.
2266 if (nfs_pub
.np_valid
!= 0 && mp
!= nfs_pub
.np_mount
)
2270 * Get real filehandle for root of exported FS.
2272 bzero((caddr_t
)&nfs_pub
.np_handle
, sizeof(nfs_pub
.np_handle
));
2273 nfs_pub
.np_handle
.fh_fsid
= mp
->mnt_stat
.f_fsid
;
2275 if ((error
= VFS_ROOT(mp
, &rvp
)))
2278 if ((error
= VFS_VPTOFH(rvp
, &nfs_pub
.np_handle
.fh_fid
)))
2284 * If an indexfile was specified, pull it in.
2286 if (argp
->ex_indexfile
!= NULL
) {
2289 error
= vn_get_namelen(rvp
, &namelen
);
2292 nfs_pub
.np_index
= kmalloc(namelen
, M_TEMP
, M_WAITOK
);
2293 error
= copyinstr(argp
->ex_indexfile
, nfs_pub
.np_index
,
2297 * Check for illegal filenames.
2299 for (cp
= nfs_pub
.np_index
; *cp
; cp
++) {
2307 kfree(nfs_pub
.np_index
, M_TEMP
);
2312 nfs_pub
.np_mount
= mp
;
2313 nfs_pub
.np_valid
= 1;
2318 vfs_export_lookup(struct mount
*mp
, struct netexport
*nep
,
2319 struct sockaddr
*nam
)
2322 struct radix_node_head
*rnh
;
2323 struct sockaddr
*saddr
;
2326 if (mp
->mnt_flag
& MNT_EXPORTED
) {
2328 * Lookup in the export list first.
2333 switch (saddr
->sa_family
) {
2336 rnh
= nep
->ne_inethead
;
2341 rnh
= nep
->ne_inet6head
;
2348 np
= (struct netcred
*)
2349 (*rnh
->rnh_matchaddr
)((char *)saddr
,
2351 if (np
&& np
->netc_rnodes
->rn_flags
& RNF_ROOT
)
2357 * If no address match, use the default if it exists.
2359 if (np
== NULL
&& mp
->mnt_flag
& MNT_DEFEXPORTED
)
2360 np
= &nep
->ne_defexported
;
2366 * perform msync on all vnodes under a mount point. The mount point must
2367 * be locked. This code is also responsible for lazy-freeing unreferenced
2368 * vnodes whos VM objects no longer contain pages.
2370 * NOTE: MNT_WAIT still skips vnodes in the VXLOCK state.
2372 * NOTE: XXX VOP_PUTPAGES and friends requires that the vnode be locked,
2373 * but vnode_pager_putpages() doesn't lock the vnode. We have to do it
2374 * way up in this high level function.
2376 static int vfs_msync_scan1(struct mount
*mp
, struct vnode
*vp
, void *data
);
2377 static int vfs_msync_scan2(struct mount
*mp
, struct vnode
*vp
, void *data
);
2380 vfs_msync(struct mount
*mp
, int flags
)
2385 * tmpfs sets this flag to prevent msync(), sync, and the
2386 * filesystem periodic syncer from trying to flush VM pages
2387 * to swap. Only pure memory pressure flushes tmpfs VM pages
2390 if (mp
->mnt_kern_flag
& MNTK_NOMSYNC
)
2394 * Ok, scan the vnodes for work. If the filesystem is using the
2395 * syncer thread feature we can use vsyncscan() instead of
2396 * vmntvnodescan(), which is much faster.
2398 vmsc_flags
= VMSC_GETVP
;
2399 if (flags
!= MNT_WAIT
)
2400 vmsc_flags
|= VMSC_NOWAIT
;
2402 if (mp
->mnt_kern_flag
& MNTK_THR_SYNC
) {
2403 vsyncscan(mp
, vmsc_flags
, vfs_msync_scan2
,
2404 (void *)(intptr_t)flags
);
2406 vmntvnodescan(mp
, vmsc_flags
,
2407 vfs_msync_scan1
, vfs_msync_scan2
,
2408 (void *)(intptr_t)flags
);
2413 * scan1 is a fast pre-check. There could be hundreds of thousands of
2414 * vnodes, we cannot afford to do anything heavy weight until we have a
2415 * fairly good indication that there is work to do.
2417 * The new namecache holds the vnode for each v_namecache association
2418 * so allow these refs.
2422 vfs_msync_scan1(struct mount
*mp
, struct vnode
*vp
, void *data
)
2424 int flags
= (int)(intptr_t)data
;
2426 if ((vp
->v_flag
& VRECLAIMED
) == 0) {
2427 if (vp
->v_auxrefs
== vp
->v_namecache_count
&&
2428 VREFCNT(vp
) <= 0 && vp
->v_object
) {
2429 return(0); /* call scan2 */
2431 if ((mp
->mnt_flag
& MNT_RDONLY
) == 0 &&
2432 (vp
->v_flag
& VOBJDIRTY
) &&
2433 (flags
== MNT_WAIT
|| vn_islocked(vp
) == 0)) {
2434 return(0); /* call scan2 */
2439 * do not call scan2, continue the loop
2445 * This callback is handed a locked vnode.
2449 vfs_msync_scan2(struct mount
*mp
, struct vnode
*vp
, void *data
)
2452 int flags
= (int)(intptr_t)data
;
2455 if (vp
->v_flag
& VRECLAIMED
)
2458 if ((mp
->mnt_flag
& MNT_RDONLY
) == 0 && (vp
->v_flag
& VOBJDIRTY
)) {
2459 if ((obj
= vp
->v_object
) != NULL
) {
2460 if (flags
== MNT_WAIT
) {
2462 * VFS_MSYNC is called with MNT_WAIT when
2465 opcflags
= OBJPC_SYNC
;
2466 } else if (vp
->v_writecount
|| obj
->ref_count
) {
2468 * VFS_MSYNC is otherwise called via the
2469 * periodic filesystem sync or the 'sync'
2470 * command. Honor MADV_NOSYNC / MAP_NOSYNC
2471 * if the file is open for writing or memory
2472 * mapped. Pages flagged PG_NOSYNC will not
2473 * be automatically flushed at this time.
2475 * The obj->ref_count test is not perfect
2476 * since temporary refs may be present, but
2477 * the periodic filesystem sync will ultimately
2478 * catch it if the file is not open and not
2481 opcflags
= OBJPC_NOSYNC
;
2484 * If the file is no longer open for writing
2485 * and also no longer mapped, do not honor
2486 * MAP_NOSYNC. That is, fully synchronize
2489 * This still occurs on the periodic fs sync,
2490 * so frontend programs which turn the file
2491 * over quickly enough can still avoid the
2492 * sync, but ultimately we do want to flush
2493 * even MADV_NOSYNC pages once it is no longer
2494 * mapped or open for writing.
2498 vm_object_page_clean(obj
, 0, 0, opcflags
);
2505 * Wake up anyone interested in vp because it is being revoked.
2508 vn_gone(struct vnode
*vp
)
2510 lwkt_gettoken(&vp
->v_token
);
2511 KNOTE(&vp
->v_pollinfo
.vpi_kqinfo
.ki_note
, NOTE_REVOKE
);
2512 lwkt_reltoken(&vp
->v_token
);
2516 * extract the cdev_t from a VBLK or VCHR. The vnode must have been opened
2517 * (or v_rdev might be NULL).
2520 vn_todev(struct vnode
*vp
)
2522 if (vp
->v_type
!= VBLK
&& vp
->v_type
!= VCHR
)
2524 KKASSERT(vp
->v_rdev
!= NULL
);
2525 return (vp
->v_rdev
);
2529 * Check if vnode represents a disk device. The vnode does not need to be
2535 vn_isdisk(struct vnode
*vp
, int *errp
)
2539 if (vp
->v_type
!= VCHR
) {
2552 if (dev_is_good(dev
) == 0) {
2557 if ((dev_dflags(dev
) & D_DISK
) == 0) {
2568 vn_get_namelen(struct vnode
*vp
, int *namelen
)
2571 register_t retval
[2];
2573 error
= VOP_PATHCONF(vp
, _PC_NAME_MAX
, retval
);
2576 *namelen
= (int)retval
[0];
2581 vop_write_dirent(int *error
, struct uio
*uio
, ino_t d_ino
, uint8_t d_type
,
2582 uint16_t d_namlen
, const char *d_name
)
2587 len
= _DIRENT_RECLEN(d_namlen
);
2588 if (len
> uio
->uio_resid
)
2591 dp
= kmalloc(len
, M_TEMP
, M_WAITOK
| M_ZERO
);
2594 dp
->d_namlen
= d_namlen
;
2595 dp
->d_type
= d_type
;
2596 bcopy(d_name
, dp
->d_name
, d_namlen
);
2598 *error
= uiomove((caddr_t
)dp
, len
, uio
);
2606 vn_mark_atime(struct vnode
*vp
, struct thread
*td
)
2608 struct proc
*p
= td
->td_proc
;
2609 struct ucred
*cred
= p
? p
->p_ucred
: proc0
.p_ucred
;
2611 if ((vp
->v_mount
->mnt_flag
& (MNT_NOATIME
| MNT_RDONLY
)) == 0) {
2612 VOP_MARKATIME(vp
, cred
);
2617 * Calculate the number of entries in an inode-related chained hash table.
2618 * With today's memory sizes, maxvnodes can wind up being a very large
2619 * number. There is no reason to waste memory, so tolerate some stacking.
2622 vfs_inodehashsize(void)
2627 while (hsize
< maxvnodes
)
2629 while (hsize
> maxvnodes
* 2)
2630 hsize
>>= 1; /* nominal 2x stacking */
2632 if (maxvnodes
> 1024 * 1024)
2633 hsize
>>= 1; /* nominal 8x stacking */
2635 if (maxvnodes
> 128 * 1024)
2636 hsize
>>= 1; /* nominal 4x stacking */
2649 #define SETHIGH(q, h) { \
2652 tmp.val[_QUAD_HIGHWORD] = (h); \
2655 #define SETLOW(q, l) { \
2658 tmp.val[_QUAD_LOWWORD] = (l); \
2663 init_va_filerev(void)
2668 getmicrouptime(&tv
);
2669 SETHIGH(ret
, tv
.tv_sec
);
2670 SETLOW(ret
, tv
.tv_usec
* 4294);
2676 * Set default timestamp_precision. If hz is reasonably high we go for
2677 * performance and limit vfs timestamps to microseconds with tick resolution.
2678 * If hz is too low, however, we lose a bit of performance to get a more
2679 * precise timestamp, because the mtime/ctime granularity might just be too
2680 * rough otherwise (for make and Makefile's, for example).
2683 vfs_ts_prec_init(void *dummy
)
2685 if (timestamp_precision
< 0) {
2687 timestamp_precision
= TSP_USEC
;
2689 timestamp_precision
= TSP_USEC_PRECISE
;
2692 SYSINIT(vfs_ts_prec_init
, SI_SUB_VFS
, SI_ORDER_ANY
, vfs_ts_prec_init
, NULL
);