2 * Copyright (c) 1989, 1993
3 * The Regents of the University of California. All rights reserved.
4 * (c) UNIX System Laboratories, Inc.
5 * All or some portions of this file are derived from material licensed
6 * to the University of California by American Telephone and Telegraph
7 * Co. or Unix System Laboratories, Inc. and are reproduced herein with
8 * the permission of UNIX System Laboratories, Inc.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
38 * @(#)vfs_subr.c 8.31 (Berkeley) 5/26/95
39 * $FreeBSD: src/sys/kern/vfs_subr.c,v 1.249.2.30 2003/04/04 20:35:57 tegge Exp $
40 * $DragonFly: src/sys/kern/vfs_subr.c,v 1.116 2008/07/12 02:44:59 dillon Exp $
44 * External virtual filesystem routines
48 #include <sys/param.h>
49 #include <sys/systm.h>
52 #include <sys/dirent.h>
53 #include <sys/domain.h>
54 #include <sys/eventhandler.h>
55 #include <sys/fcntl.h>
56 #include <sys/kernel.h>
57 #include <sys/kthread.h>
58 #include <sys/malloc.h>
60 #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 <sys/thread2.h>
86 #include <sys/sysref2.h>
88 static MALLOC_DEFINE(M_NETADDR
, "Export Host", "Export host address structure");
91 SYSCTL_INT(_debug
, OID_AUTO
, numvnodes
, CTLFLAG_RD
, &numvnodes
, 0, "");
93 SYSCTL_INT(_vfs
, OID_AUTO
, fastdev
, CTLFLAG_RW
, &vfs_fastdev
, 0, "");
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
,
106 &reassignbufcalls
, 0, "");
107 static int reassignbufloops
;
108 SYSCTL_INT(_vfs
, OID_AUTO
, reassignbufloops
, CTLFLAG_RW
,
109 &reassignbufloops
, 0, "");
110 static int reassignbufsortgood
;
111 SYSCTL_INT(_vfs
, OID_AUTO
, reassignbufsortgood
, CTLFLAG_RW
,
112 &reassignbufsortgood
, 0, "");
113 static int reassignbufsortbad
;
114 SYSCTL_INT(_vfs
, OID_AUTO
, reassignbufsortbad
, CTLFLAG_RW
,
115 &reassignbufsortbad
, 0, "");
116 static int reassignbufmethod
= 1;
117 SYSCTL_INT(_vfs
, OID_AUTO
, reassignbufmethod
, CTLFLAG_RW
,
118 &reassignbufmethod
, 0, "");
120 int nfs_mount_type
= -1;
121 static struct lwkt_token spechash_token
;
122 struct nfs_public nfs_pub
; /* publicly exported FS */
125 SYSCTL_INT(_kern
, KERN_MAXVNODES
, maxvnodes
, CTLFLAG_RW
,
126 &desiredvnodes
, 0, "Maximum number of vnodes");
128 static void vfs_free_addrlist (struct netexport
*nep
);
129 static int vfs_free_netcred (struct radix_node
*rn
, void *w
);
130 static int vfs_hang_addrlist (struct mount
*mp
, struct netexport
*nep
,
131 const struct export_args
*argp
);
133 extern int dev_ref_debug
;
136 * Red black tree functions
138 static int rb_buf_compare(struct buf
*b1
, struct buf
*b2
);
139 RB_GENERATE2(buf_rb_tree
, buf
, b_rbnode
, rb_buf_compare
, off_t
, b_loffset
);
140 RB_GENERATE2(buf_rb_hash
, buf
, b_rbhash
, rb_buf_compare
, off_t
, b_loffset
);
143 rb_buf_compare(struct buf
*b1
, struct buf
*b2
)
145 if (b1
->b_loffset
< b2
->b_loffset
)
147 if (b1
->b_loffset
> b2
->b_loffset
)
153 * Returns non-zero if the vnode is a candidate for lazy msyncing.
156 vshouldmsync(struct vnode
*vp
)
158 if (vp
->v_auxrefs
!= 0 || vp
->v_sysref
.refcnt
> 0)
159 return (0); /* other holders */
161 (vp
->v_object
->ref_count
|| vp
->v_object
->resident_page_count
)) {
168 * Initialize the vnode management data structures.
170 * Called from vfsinit()
176 * Desired vnodes is a result of the physical page count
177 * and the size of kernel's heap. It scales in proportion
178 * to the amount of available physical memory. This can
179 * cause trouble on 64-bit and large memory platforms.
181 /* desiredvnodes = maxproc + vmstats.v_page_count / 4; */
183 min(maxproc
+ vmstats
.v_page_count
/ 4,
185 (5 * (sizeof(struct vm_object
) + sizeof(struct vnode
))));
187 lwkt_token_init(&spechash_token
);
191 * Knob to control the precision of file timestamps:
193 * 0 = seconds only; nanoseconds zeroed.
194 * 1 = seconds and nanoseconds, accurate within 1/HZ.
195 * 2 = seconds and nanoseconds, truncated to microseconds.
196 * >=3 = seconds and nanoseconds, maximum precision.
198 enum { TSP_SEC
, TSP_HZ
, TSP_USEC
, TSP_NSEC
};
200 static int timestamp_precision
= TSP_SEC
;
201 SYSCTL_INT(_vfs
, OID_AUTO
, timestamp_precision
, CTLFLAG_RW
,
202 ×tamp_precision
, 0, "");
205 * Get a current timestamp.
208 vfs_timestamp(struct timespec
*tsp
)
212 switch (timestamp_precision
) {
214 tsp
->tv_sec
= time_second
;
222 TIMEVAL_TO_TIMESPEC(&tv
, tsp
);
232 * Set vnode attributes to VNOVAL
235 vattr_null(struct vattr
*vap
)
238 vap
->va_size
= VNOVAL
;
239 vap
->va_bytes
= VNOVAL
;
240 vap
->va_mode
= VNOVAL
;
241 vap
->va_nlink
= VNOVAL
;
242 vap
->va_uid
= VNOVAL
;
243 vap
->va_gid
= VNOVAL
;
244 vap
->va_fsid
= VNOVAL
;
245 vap
->va_fileid
= VNOVAL
;
246 vap
->va_blocksize
= VNOVAL
;
247 vap
->va_rmajor
= VNOVAL
;
248 vap
->va_rminor
= VNOVAL
;
249 vap
->va_atime
.tv_sec
= VNOVAL
;
250 vap
->va_atime
.tv_nsec
= VNOVAL
;
251 vap
->va_mtime
.tv_sec
= VNOVAL
;
252 vap
->va_mtime
.tv_nsec
= VNOVAL
;
253 vap
->va_ctime
.tv_sec
= VNOVAL
;
254 vap
->va_ctime
.tv_nsec
= VNOVAL
;
255 vap
->va_flags
= VNOVAL
;
256 vap
->va_gen
= VNOVAL
;
258 vap
->va_fsmid
= VNOVAL
;
259 /* va_*_uuid fields are only valid if related flags are set */
263 * Flush out and invalidate all buffers associated with a vnode.
267 static int vinvalbuf_bp(struct buf
*bp
, void *data
);
269 struct vinvalbuf_bp_info
{
277 vupdatefsmid(struct vnode
*vp
)
279 atomic_set_int(&vp
->v_flag
, VFSMID
);
283 vinvalbuf(struct vnode
*vp
, int flags
, int slpflag
, int slptimeo
)
285 struct vinvalbuf_bp_info info
;
290 * If we are being asked to save, call fsync to ensure that the inode
293 if (flags
& V_SAVE
) {
295 while (vp
->v_track_write
.bk_active
) {
296 vp
->v_track_write
.bk_waitflag
= 1;
297 error
= tsleep(&vp
->v_track_write
, slpflag
,
298 "vinvlbuf", slptimeo
);
304 if (!RB_EMPTY(&vp
->v_rbdirty_tree
)) {
306 if ((error
= VOP_FSYNC(vp
, MNT_WAIT
)) != 0)
309 if (vp
->v_track_write
.bk_active
> 0 ||
310 !RB_EMPTY(&vp
->v_rbdirty_tree
))
311 panic("vinvalbuf: dirty bufs");
316 info
.slptimeo
= slptimeo
;
317 info
.lkflags
= LK_EXCLUSIVE
| LK_SLEEPFAIL
;
318 if (slpflag
& PCATCH
)
319 info
.lkflags
|= LK_PCATCH
;
324 * Flush the buffer cache until nothing is left.
326 while (!RB_EMPTY(&vp
->v_rbclean_tree
) ||
327 !RB_EMPTY(&vp
->v_rbdirty_tree
)) {
328 error
= RB_SCAN(buf_rb_tree
, &vp
->v_rbclean_tree
, NULL
,
329 vinvalbuf_bp
, &info
);
331 error
= RB_SCAN(buf_rb_tree
, &vp
->v_rbdirty_tree
, NULL
,
332 vinvalbuf_bp
, &info
);
337 * Wait for I/O to complete. XXX needs cleaning up. The vnode can
338 * have write I/O in-progress but if there is a VM object then the
339 * VM object can also have read-I/O in-progress.
342 while (vp
->v_track_write
.bk_active
> 0) {
343 vp
->v_track_write
.bk_waitflag
= 1;
344 tsleep(&vp
->v_track_write
, 0, "vnvlbv", 0);
346 if ((object
= vp
->v_object
) != NULL
) {
347 while (object
->paging_in_progress
)
348 vm_object_pip_sleep(object
, "vnvlbx");
350 } while (vp
->v_track_write
.bk_active
> 0);
355 * Destroy the copy in the VM cache, too.
357 if ((object
= vp
->v_object
) != NULL
) {
358 vm_object_page_remove(object
, 0, 0,
359 (flags
& V_SAVE
) ? TRUE
: FALSE
);
362 if (!RB_EMPTY(&vp
->v_rbdirty_tree
) || !RB_EMPTY(&vp
->v_rbclean_tree
))
363 panic("vinvalbuf: flush failed");
364 if (!RB_EMPTY(&vp
->v_rbhash_tree
))
365 panic("vinvalbuf: flush failed, buffers still present");
370 vinvalbuf_bp(struct buf
*bp
, void *data
)
372 struct vinvalbuf_bp_info
*info
= data
;
375 if (BUF_LOCK(bp
, LK_EXCLUSIVE
| LK_NOWAIT
)) {
376 error
= BUF_TIMELOCK(bp
, info
->lkflags
,
377 "vinvalbuf", info
->slptimeo
);
387 KKASSERT(bp
->b_vp
== info
->vp
);
390 * XXX Since there are no node locks for NFS, I
391 * believe there is a slight chance that a delayed
392 * write will occur while sleeping just above, so
393 * check for it. Note that vfs_bio_awrite expects
394 * buffers to reside on a queue, while bwrite() and
397 * NOTE: NO B_LOCKED CHECK. Also no buf_checkwrite()
398 * check. This code will write out the buffer, period.
400 if (((bp
->b_flags
& (B_DELWRI
| B_INVAL
)) == B_DELWRI
) &&
401 (info
->flags
& V_SAVE
)) {
402 if (bp
->b_vp
== info
->vp
) {
403 if (bp
->b_flags
& B_CLUSTEROK
) {
407 bp
->b_flags
|= B_ASYNC
;
414 } else if (info
->flags
& V_SAVE
) {
416 * Cannot set B_NOCACHE on a clean buffer as this will
417 * destroy the VM backing store which might actually
418 * be dirty (and unsynchronized).
421 bp
->b_flags
|= (B_INVAL
| B_RELBUF
);
422 bp
->b_flags
&= ~B_ASYNC
;
426 bp
->b_flags
|= (B_INVAL
| B_NOCACHE
| B_RELBUF
);
427 bp
->b_flags
&= ~B_ASYNC
;
434 * Truncate a file's buffer and pages to a specified length. This
435 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
438 * The vnode must be locked.
440 static int vtruncbuf_bp_trunc_cmp(struct buf
*bp
, void *data
);
441 static int vtruncbuf_bp_trunc(struct buf
*bp
, void *data
);
442 static int vtruncbuf_bp_metasync_cmp(struct buf
*bp
, void *data
);
443 static int vtruncbuf_bp_metasync(struct buf
*bp
, void *data
);
446 vtruncbuf(struct vnode
*vp
, off_t length
, int blksize
)
450 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 truncloffset
= length
+ (blksize
- count
);
460 truncloffset
= length
;
464 count
= RB_SCAN(buf_rb_tree
, &vp
->v_rbclean_tree
,
465 vtruncbuf_bp_trunc_cmp
,
466 vtruncbuf_bp_trunc
, &truncloffset
);
467 count
+= RB_SCAN(buf_rb_tree
, &vp
->v_rbdirty_tree
,
468 vtruncbuf_bp_trunc_cmp
,
469 vtruncbuf_bp_trunc
, &truncloffset
);
473 * For safety, fsync any remaining metadata if the file is not being
474 * truncated to 0. Since the metadata does not represent the entire
475 * dirty list we have to rely on the hit count to ensure that we get
480 count
= RB_SCAN(buf_rb_tree
, &vp
->v_rbdirty_tree
,
481 vtruncbuf_bp_metasync_cmp
,
482 vtruncbuf_bp_metasync
, vp
);
487 * Clean out any left over VM backing store.
491 vnode_pager_setsize(vp
, length
);
496 * It is possible to have in-progress I/O from buffers that were
497 * not part of the truncation. This should not happen if we
498 * are truncating to 0-length.
500 filename
= TAILQ_FIRST(&vp
->v_namecache
) ?
501 TAILQ_FIRST(&vp
->v_namecache
)->nc_name
: "?";
503 while ((count
= vp
->v_track_write
.bk_active
) > 0) {
504 vp
->v_track_write
.bk_waitflag
= 1;
505 tsleep(&vp
->v_track_write
, 0, "vbtrunc", 0);
507 kprintf("Warning: vtruncbuf(): Had to wait for "
508 "%d buffer I/Os to finish in %s\n",
514 * Make sure no buffers were instantiated while we were trying
515 * to clean out the remaining VM pages. This could occur due
516 * to busy dirty VM pages being flushed out to disk.
519 count
= RB_SCAN(buf_rb_tree
, &vp
->v_rbclean_tree
,
520 vtruncbuf_bp_trunc_cmp
,
521 vtruncbuf_bp_trunc
, &truncloffset
);
522 count
+= RB_SCAN(buf_rb_tree
, &vp
->v_rbdirty_tree
,
523 vtruncbuf_bp_trunc_cmp
,
524 vtruncbuf_bp_trunc
, &truncloffset
);
526 kprintf("Warning: vtruncbuf(): Had to re-clean %d "
527 "left over buffers in %s\n", count
, filename
);
537 * The callback buffer is beyond the new file EOF and must be destroyed.
538 * Note that the compare function must conform to the RB_SCAN's requirements.
542 vtruncbuf_bp_trunc_cmp(struct buf
*bp
, void *data
)
544 if (bp
->b_loffset
>= *(off_t
*)data
)
551 vtruncbuf_bp_trunc(struct buf
*bp
, void *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 if (BUF_LOCK(bp
, LK_EXCLUSIVE
| LK_NOWAIT
)) {
559 if (BUF_LOCK(bp
, LK_EXCLUSIVE
|LK_SLEEPFAIL
) == 0)
563 bp
->b_flags
|= (B_INVAL
| B_RELBUF
| B_NOCACHE
);
564 bp
->b_flags
&= ~B_ASYNC
;
571 * Fsync all meta-data after truncating a file to be non-zero. Only metadata
572 * blocks (with a negative loffset) are scanned.
573 * Note that the compare function must conform to the RB_SCAN's requirements.
576 vtruncbuf_bp_metasync_cmp(struct buf
*bp
, void *data
)
578 if (bp
->b_loffset
< 0)
584 vtruncbuf_bp_metasync(struct buf
*bp
, void *data
)
586 struct vnode
*vp
= data
;
588 if (bp
->b_flags
& B_DELWRI
) {
590 * Do not try to use a buffer we cannot immediately lock,
591 * but sleep anyway to prevent a livelock. The code will
592 * loop until all buffers can be acted upon.
594 if (BUF_LOCK(bp
, LK_EXCLUSIVE
| LK_NOWAIT
)) {
595 if (BUF_LOCK(bp
, LK_EXCLUSIVE
|LK_SLEEPFAIL
) == 0)
599 if (bp
->b_vp
== vp
) {
600 bp
->b_flags
|= B_ASYNC
;
602 bp
->b_flags
&= ~B_ASYNC
;
613 * vfsync - implements a multipass fsync on a file which understands
614 * dependancies and meta-data. The passed vnode must be locked. The
615 * waitfor argument may be MNT_WAIT or MNT_NOWAIT, or MNT_LAZY.
617 * When fsyncing data asynchronously just do one consolidated pass starting
618 * with the most negative block number. This may not get all the data due
621 * When fsyncing data synchronously do a data pass, then a metadata pass,
622 * then do additional data+metadata passes to try to get all the data out.
624 static int vfsync_wait_output(struct vnode
*vp
,
625 int (*waitoutput
)(struct vnode
*, struct thread
*));
626 static int vfsync_data_only_cmp(struct buf
*bp
, void *data
);
627 static int vfsync_meta_only_cmp(struct buf
*bp
, void *data
);
628 static int vfsync_lazy_range_cmp(struct buf
*bp
, void *data
);
629 static int vfsync_bp(struct buf
*bp
, void *data
);
638 int (*checkdef
)(struct buf
*);
642 vfsync(struct vnode
*vp
, int waitfor
, int passes
,
643 int (*checkdef
)(struct buf
*),
644 int (*waitoutput
)(struct vnode
*, struct thread
*))
646 struct vfsync_info info
;
649 bzero(&info
, sizeof(info
));
651 if ((info
.checkdef
= checkdef
) == NULL
)
654 crit_enter_id("vfsync");
659 * Lazy (filesystem syncer typ) Asynchronous plus limit the
660 * number of data (not meta) pages we try to flush to 1MB.
661 * A non-zero return means that lazy limit was reached.
663 info
.lazylimit
= 1024 * 1024;
665 error
= RB_SCAN(buf_rb_tree
, &vp
->v_rbdirty_tree
,
666 vfsync_lazy_range_cmp
, vfsync_bp
, &info
);
667 RB_SCAN(buf_rb_tree
, &vp
->v_rbdirty_tree
,
668 vfsync_meta_only_cmp
, vfsync_bp
, &info
);
671 else if (!RB_EMPTY(&vp
->v_rbdirty_tree
))
672 vn_syncer_add_to_worklist(vp
, 1);
677 * Asynchronous. Do a data-only pass and a meta-only pass.
680 RB_SCAN(buf_rb_tree
, &vp
->v_rbdirty_tree
, vfsync_data_only_cmp
,
682 RB_SCAN(buf_rb_tree
, &vp
->v_rbdirty_tree
, vfsync_meta_only_cmp
,
688 * Synchronous. Do a data-only pass, then a meta-data+data
689 * pass, then additional integrated passes to try to get
690 * all the dependancies flushed.
692 RB_SCAN(buf_rb_tree
, &vp
->v_rbdirty_tree
, vfsync_data_only_cmp
,
694 error
= vfsync_wait_output(vp
, waitoutput
);
696 info
.skippedbufs
= 0;
697 RB_SCAN(buf_rb_tree
, &vp
->v_rbdirty_tree
, NULL
,
699 error
= vfsync_wait_output(vp
, waitoutput
);
700 if (info
.skippedbufs
)
701 kprintf("Warning: vfsync skipped %d dirty bufs in pass2!\n", info
.skippedbufs
);
703 while (error
== 0 && passes
> 0 &&
704 !RB_EMPTY(&vp
->v_rbdirty_tree
)) {
706 info
.synchronous
= 1;
709 error
= RB_SCAN(buf_rb_tree
, &vp
->v_rbdirty_tree
, NULL
,
715 error
= vfsync_wait_output(vp
, waitoutput
);
719 crit_exit_id("vfsync");
724 vfsync_wait_output(struct vnode
*vp
, int (*waitoutput
)(struct vnode
*, struct thread
*))
728 while (vp
->v_track_write
.bk_active
) {
729 vp
->v_track_write
.bk_waitflag
= 1;
730 tsleep(&vp
->v_track_write
, 0, "fsfsn", 0);
733 error
= waitoutput(vp
, curthread
);
738 vfsync_data_only_cmp(struct buf
*bp
, void *data
)
740 if (bp
->b_loffset
< 0)
746 vfsync_meta_only_cmp(struct buf
*bp
, void *data
)
748 if (bp
->b_loffset
< 0)
754 vfsync_lazy_range_cmp(struct buf
*bp
, void *data
)
756 struct vfsync_info
*info
= data
;
757 if (bp
->b_loffset
< info
->vp
->v_lazyw
)
763 vfsync_bp(struct buf
*bp
, void *data
)
765 struct vfsync_info
*info
= data
;
766 struct vnode
*vp
= info
->vp
;
770 * if syncdeps is not set we do not try to write buffers which have
773 if (!info
->synchronous
&& info
->syncdeps
== 0 && info
->checkdef(bp
))
777 * Ignore buffers that we cannot immediately lock. XXX
779 if (BUF_LOCK(bp
, LK_EXCLUSIVE
| LK_NOWAIT
)) {
780 kprintf("Warning: vfsync_bp skipping dirty buffer %p\n", bp
);
784 if ((bp
->b_flags
& B_DELWRI
) == 0)
785 panic("vfsync_bp: buffer not dirty");
787 panic("vfsync_bp: buffer vp mismatch");
790 * B_NEEDCOMMIT (primarily used by NFS) is a state where the buffer
791 * has been written but an additional handshake with the device
792 * is required before we can dispose of the buffer. We have no idea
793 * how to do this so we have to skip these buffers.
795 if (bp
->b_flags
& B_NEEDCOMMIT
) {
801 * Ask bioops if it is ok to sync
803 if (LIST_FIRST(&bp
->b_dep
) != NULL
&& buf_checkwrite(bp
)) {
809 if (info
->synchronous
) {
811 * Synchronous flushing. An error may be returned.
814 crit_exit_id("vfsync");
816 crit_enter_id("vfsync");
819 * Asynchronous flushing. A negative return value simply
820 * stops the scan and is not considered an error. We use
821 * this to support limited MNT_LAZY flushes.
823 vp
->v_lazyw
= bp
->b_loffset
;
824 if ((vp
->v_flag
& VOBJBUF
) && (bp
->b_flags
& B_CLUSTEROK
)) {
825 info
->lazycount
+= vfs_bio_awrite(bp
);
827 info
->lazycount
+= bp
->b_bufsize
;
829 crit_exit_id("vfsync");
831 crit_enter_id("vfsync");
833 if (info
->lazylimit
&& info
->lazycount
>= info
->lazylimit
)
842 * Associate a buffer with a vnode.
845 bgetvp(struct vnode
*vp
, struct buf
*bp
)
847 KASSERT(bp
->b_vp
== NULL
, ("bgetvp: not free"));
848 KKASSERT((bp
->b_flags
& (B_HASHED
|B_DELWRI
|B_VNCLEAN
|B_VNDIRTY
)) == 0);
852 * Insert onto list for new vnode.
856 bp
->b_flags
|= B_HASHED
;
857 if (buf_rb_hash_RB_INSERT(&vp
->v_rbhash_tree
, bp
))
858 panic("reassignbuf: dup lblk vp %p bp %p", vp
, bp
);
860 bp
->b_flags
|= B_VNCLEAN
;
861 if (buf_rb_tree_RB_INSERT(&vp
->v_rbclean_tree
, bp
))
862 panic("reassignbuf: dup lblk/clean vp %p bp %p", vp
, bp
);
867 * Disassociate a buffer from a vnode.
870 brelvp(struct buf
*bp
)
874 KASSERT(bp
->b_vp
!= NULL
, ("brelvp: NULL"));
877 * Delete from old vnode list, if on one.
881 if (bp
->b_flags
& (B_VNDIRTY
| B_VNCLEAN
)) {
882 if (bp
->b_flags
& B_VNDIRTY
)
883 buf_rb_tree_RB_REMOVE(&vp
->v_rbdirty_tree
, bp
);
885 buf_rb_tree_RB_REMOVE(&vp
->v_rbclean_tree
, bp
);
886 bp
->b_flags
&= ~(B_VNDIRTY
| B_VNCLEAN
);
888 if (bp
->b_flags
& B_HASHED
) {
889 buf_rb_hash_RB_REMOVE(&vp
->v_rbhash_tree
, bp
);
890 bp
->b_flags
&= ~B_HASHED
;
892 if ((vp
->v_flag
& VONWORKLST
) && RB_EMPTY(&vp
->v_rbdirty_tree
)) {
893 vp
->v_flag
&= ~VONWORKLST
;
894 LIST_REMOVE(vp
, v_synclist
);
902 * Reassign the buffer to the proper clean/dirty list based on B_DELWRI.
903 * This routine is called when the state of the B_DELWRI bit is changed.
906 reassignbuf(struct buf
*bp
)
908 struct vnode
*vp
= bp
->b_vp
;
911 KKASSERT(vp
!= NULL
);
915 * B_PAGING flagged buffers cannot be reassigned because their vp
916 * is not fully linked in.
918 if (bp
->b_flags
& B_PAGING
)
919 panic("cannot reassign paging buffer");
922 if (bp
->b_flags
& B_DELWRI
) {
924 * Move to the dirty list, add the vnode to the worklist
926 if (bp
->b_flags
& B_VNCLEAN
) {
927 buf_rb_tree_RB_REMOVE(&vp
->v_rbclean_tree
, bp
);
928 bp
->b_flags
&= ~B_VNCLEAN
;
930 if ((bp
->b_flags
& B_VNDIRTY
) == 0) {
931 if (buf_rb_tree_RB_INSERT(&vp
->v_rbdirty_tree
, bp
)) {
932 panic("reassignbuf: dup lblk vp %p bp %p",
935 bp
->b_flags
|= B_VNDIRTY
;
937 if ((vp
->v_flag
& VONWORKLST
) == 0) {
938 switch (vp
->v_type
) {
945 vp
->v_rdev
->si_mountpoint
!= NULL
) {
953 vn_syncer_add_to_worklist(vp
, delay
);
957 * Move to the clean list, remove the vnode from the worklist
958 * if no dirty blocks remain.
960 if (bp
->b_flags
& B_VNDIRTY
) {
961 buf_rb_tree_RB_REMOVE(&vp
->v_rbdirty_tree
, bp
);
962 bp
->b_flags
&= ~B_VNDIRTY
;
964 if ((bp
->b_flags
& B_VNCLEAN
) == 0) {
965 if (buf_rb_tree_RB_INSERT(&vp
->v_rbclean_tree
, bp
)) {
966 panic("reassignbuf: dup lblk vp %p bp %p",
969 bp
->b_flags
|= B_VNCLEAN
;
971 if ((vp
->v_flag
& VONWORKLST
) &&
972 RB_EMPTY(&vp
->v_rbdirty_tree
)) {
973 vp
->v_flag
&= ~VONWORKLST
;
974 LIST_REMOVE(vp
, v_synclist
);
981 * Create a vnode for a block device.
982 * Used for mounting the root file system.
985 bdevvp(cdev_t dev
, struct vnode
**vpp
)
995 error
= getspecialvnode(VT_NON
, NULL
, &spec_vnode_vops_p
, &nvp
, 0, 0);
1002 vp
->v_umajor
= dev
->si_umajor
;
1003 vp
->v_uminor
= dev
->si_uminor
;
1010 v_associate_rdev(struct vnode
*vp
, cdev_t dev
)
1016 if (dev_is_good(dev
) == 0)
1018 KKASSERT(vp
->v_rdev
== NULL
);
1021 vp
->v_rdev
= reference_dev(dev
);
1022 lwkt_gettoken(&ilock
, &spechash_token
);
1023 SLIST_INSERT_HEAD(&dev
->si_hlist
, vp
, v_cdevnext
);
1024 lwkt_reltoken(&ilock
);
1029 v_release_rdev(struct vnode
*vp
)
1034 if ((dev
= vp
->v_rdev
) != NULL
) {
1035 lwkt_gettoken(&ilock
, &spechash_token
);
1036 SLIST_REMOVE(&dev
->si_hlist
, vp
, vnode
, v_cdevnext
);
1039 lwkt_reltoken(&ilock
);
1044 * Add a vnode to the alias list hung off the cdev_t. We only associate
1045 * the device number with the vnode. The actual device is not associated
1046 * until the vnode is opened (usually in spec_open()), and will be
1047 * disassociated on last close.
1050 addaliasu(struct vnode
*nvp
, int x
, int y
)
1052 if (nvp
->v_type
!= VBLK
&& nvp
->v_type
!= VCHR
)
1053 panic("addaliasu on non-special vnode");
1059 * Simple call that a filesystem can make to try to get rid of a
1060 * vnode. It will fail if anyone is referencing the vnode (including
1063 * The filesystem can check whether its in-memory inode structure still
1064 * references the vp on return.
1067 vclean_unlocked(struct vnode
*vp
)
1070 if (sysref_isactive(&vp
->v_sysref
) == 0)
1076 * Disassociate a vnode from its underlying filesystem.
1078 * The vnode must be VX locked and referenced. In all normal situations
1079 * there are no active references. If vclean_vxlocked() is called while
1080 * there are active references, the vnode is being ripped out and we have
1081 * to call VOP_CLOSE() as appropriate before we can reclaim it.
1084 vclean_vxlocked(struct vnode
*vp
, int flags
)
1091 * If the vnode has already been reclaimed we have nothing to do.
1093 if (vp
->v_flag
& VRECLAIMED
)
1095 vp
->v_flag
|= VRECLAIMED
;
1098 * Scrap the vfs cache
1100 while (cache_inval_vp(vp
, 0) != 0) {
1101 kprintf("Warning: vnode %p clean/cache_resolution race detected\n", vp
);
1102 tsleep(vp
, 0, "vclninv", 2);
1106 * Check to see if the vnode is in use. If so we have to reference it
1107 * before we clean it out so that its count cannot fall to zero and
1108 * generate a race against ourselves to recycle it.
1110 active
= sysref_isactive(&vp
->v_sysref
);
1113 * Clean out any buffers associated with the vnode and destroy its
1114 * object, if it has one.
1116 vinvalbuf(vp
, V_SAVE
, 0, 0);
1119 * If purging an active vnode (typically during a forced unmount
1120 * or reboot), it must be closed and deactivated before being
1121 * reclaimed. This isn't really all that safe, but what can
1124 * Note that neither of these routines unlocks the vnode.
1126 if (active
&& (flags
& DOCLOSE
)) {
1127 while ((n
= vp
->v_opencount
) != 0) {
1128 if (vp
->v_writecount
)
1129 VOP_CLOSE(vp
, FWRITE
|FNONBLOCK
);
1131 VOP_CLOSE(vp
, FNONBLOCK
);
1132 if (vp
->v_opencount
== n
) {
1133 kprintf("Warning: unable to force-close"
1141 * If the vnode has not been deactivated, deactivated it. Deactivation
1142 * can create new buffers and VM pages so we have to call vinvalbuf()
1143 * again to make sure they all get flushed.
1145 * This can occur if a file with a link count of 0 needs to be
1148 if ((vp
->v_flag
& VINACTIVE
) == 0) {
1149 vp
->v_flag
|= VINACTIVE
;
1151 vinvalbuf(vp
, V_SAVE
, 0, 0);
1155 * If the vnode has an object, destroy it.
1157 if ((object
= vp
->v_object
) != NULL
) {
1158 if (object
->ref_count
== 0) {
1159 if ((object
->flags
& OBJ_DEAD
) == 0)
1160 vm_object_terminate(object
);
1162 vm_pager_deallocate(object
);
1164 vp
->v_flag
&= ~VOBJBUF
;
1166 KKASSERT((vp
->v_flag
& VOBJBUF
) == 0);
1169 * Reclaim the vnode.
1171 if (VOP_RECLAIM(vp
))
1172 panic("vclean: cannot reclaim");
1175 * Done with purge, notify sleepers of the grim news.
1177 vp
->v_ops
= &dead_vnode_vops_p
;
1182 * If we are destroying an active vnode, reactivate it now that
1183 * we have reassociated it with deadfs. This prevents the system
1184 * from crashing on the vnode due to it being unexpectedly marked
1185 * as inactive or reclaimed.
1187 if (active
&& (flags
& DOCLOSE
)) {
1188 vp
->v_flag
&= ~(VINACTIVE
|VRECLAIMED
);
1193 * Eliminate all activity associated with the requested vnode
1194 * and with all vnodes aliased to the requested vnode.
1196 * The vnode must be referenced and vx_lock()'d
1198 * revoke { struct vnode *a_vp, int a_flags }
1201 vop_stdrevoke(struct vop_revoke_args
*ap
)
1203 struct vnode
*vp
, *vq
;
1207 KASSERT((ap
->a_flags
& REVOKEALL
) != 0, ("vop_revoke"));
1212 * If the vnode is already dead don't try to revoke it
1214 if (vp
->v_flag
& VRECLAIMED
)
1218 * If the vnode has a device association, scrap all vnodes associated
1219 * with the device. Don't let the device disappear on us while we
1220 * are scrapping the vnodes.
1222 * The passed vp will probably show up in the list, do not VX lock
1225 if (vp
->v_type
!= VCHR
)
1227 if ((dev
= vp
->v_rdev
) == NULL
) {
1228 if ((dev
= get_dev(vp
->v_umajor
, vp
->v_uminor
)) == NULL
)
1232 lwkt_gettoken(&ilock
, &spechash_token
);
1233 while ((vq
= SLIST_FIRST(&dev
->si_hlist
)) != NULL
) {
1236 if (vq
== SLIST_FIRST(&dev
->si_hlist
))
1241 lwkt_reltoken(&ilock
);
1247 * This is called when the object underlying a vnode is being destroyed,
1248 * such as in a remove(). Try to recycle the vnode immediately if the
1249 * only active reference is our reference.
1251 * Directory vnodes in the namecache with children cannot be immediately
1252 * recycled because numerous VOP_N*() ops require them to be stable.
1255 vrecycle(struct vnode
*vp
)
1257 if (vp
->v_sysref
.refcnt
<= 1) {
1258 if (cache_inval_vp_nonblock(vp
))
1267 * Return the maximum I/O size allowed for strategy calls on VP.
1269 * If vp is VCHR or VBLK we dive the device, otherwise we use
1270 * the vp's mount info.
1273 vmaxiosize(struct vnode
*vp
)
1275 if (vp
->v_type
== VBLK
|| vp
->v_type
== VCHR
) {
1276 return(vp
->v_rdev
->si_iosize_max
);
1278 return(vp
->v_mount
->mnt_iosize_max
);
1283 * Eliminate all activity associated with a vnode in preparation for reuse.
1285 * The vnode must be VX locked and refd and will remain VX locked and refd
1286 * on return. This routine may be called with the vnode in any state, as
1287 * long as it is VX locked. The vnode will be cleaned out and marked
1288 * VRECLAIMED but will not actually be reused until all existing refs and
1291 * NOTE: This routine may be called on a vnode which has not yet been
1292 * already been deactivated (VOP_INACTIVE), or on a vnode which has
1293 * already been reclaimed.
1295 * This routine is not responsible for placing us back on the freelist.
1296 * Instead, it happens automatically when the caller releases the VX lock
1297 * (assuming there aren't any other references).
1301 vgone_vxlocked(struct vnode
*vp
)
1304 * assert that the VX lock is held. This is an absolute requirement
1305 * now for vgone_vxlocked() to be called.
1307 KKASSERT(vp
->v_lock
.lk_exclusivecount
== 1);
1310 * Clean out the filesystem specific data and set the VRECLAIMED
1311 * bit. Also deactivate the vnode if necessary.
1313 vclean_vxlocked(vp
, DOCLOSE
);
1316 * Delete from old mount point vnode list, if on one.
1318 if (vp
->v_mount
!= NULL
)
1319 insmntque(vp
, NULL
);
1322 * If special device, remove it from special device alias list
1323 * if it is on one. This should normally only occur if a vnode is
1324 * being revoked as the device should otherwise have been released
1327 if ((vp
->v_type
== VBLK
|| vp
->v_type
== VCHR
) && vp
->v_rdev
!= NULL
) {
1338 * Lookup a vnode by device number.
1341 vfinddev(cdev_t dev
, enum vtype type
, struct vnode
**vpp
)
1346 lwkt_gettoken(&ilock
, &spechash_token
);
1347 SLIST_FOREACH(vp
, &dev
->si_hlist
, v_cdevnext
) {
1348 if (type
== vp
->v_type
) {
1350 lwkt_reltoken(&ilock
);
1354 lwkt_reltoken(&ilock
);
1359 * Calculate the total number of references to a special device. This
1360 * routine may only be called for VBLK and VCHR vnodes since v_rdev is
1361 * an overloaded field. Since udev2dev can now return NULL, we have
1362 * to check for a NULL v_rdev.
1365 count_dev(cdev_t dev
)
1371 if (SLIST_FIRST(&dev
->si_hlist
)) {
1372 lwkt_gettoken(&ilock
, &spechash_token
);
1373 SLIST_FOREACH(vp
, &dev
->si_hlist
, v_cdevnext
) {
1374 if (vp
->v_sysref
.refcnt
> 0)
1375 count
+= vp
->v_sysref
.refcnt
;
1377 lwkt_reltoken(&ilock
);
1383 count_udev(int x
, int y
)
1387 if ((dev
= get_dev(x
, y
)) == NULL
)
1389 return(count_dev(dev
));
1393 vcount(struct vnode
*vp
)
1395 if (vp
->v_rdev
== NULL
)
1397 return(count_dev(vp
->v_rdev
));
1401 * Initialize VMIO for a vnode. This routine MUST be called before a
1402 * VFS can issue buffer cache ops on a vnode. It is typically called
1403 * when a vnode is initialized from its inode.
1406 vinitvmio(struct vnode
*vp
, off_t filesize
)
1412 if ((object
= vp
->v_object
) == NULL
) {
1413 object
= vnode_pager_alloc(vp
, filesize
, 0, 0);
1415 * Dereference the reference we just created. This assumes
1416 * that the object is associated with the vp.
1418 object
->ref_count
--;
1421 if (object
->flags
& OBJ_DEAD
) {
1423 vm_object_dead_sleep(object
, "vodead");
1424 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
1428 KASSERT(vp
->v_object
!= NULL
, ("vinitvmio: NULL object"));
1429 vp
->v_flag
|= VOBJBUF
;
1435 * Print out a description of a vnode.
1437 static char *typename
[] =
1438 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD"};
1441 vprint(char *label
, struct vnode
*vp
)
1446 kprintf("%s: %p: ", label
, (void *)vp
);
1448 kprintf("%p: ", (void *)vp
);
1449 kprintf("type %s, sysrefs %d, writecount %d, holdcnt %d,",
1450 typename
[vp
->v_type
],
1451 vp
->v_sysref
.refcnt
, vp
->v_writecount
, vp
->v_auxrefs
);
1453 if (vp
->v_flag
& VROOT
)
1454 strcat(buf
, "|VROOT");
1455 if (vp
->v_flag
& VTEXT
)
1456 strcat(buf
, "|VTEXT");
1457 if (vp
->v_flag
& VSYSTEM
)
1458 strcat(buf
, "|VSYSTEM");
1459 if (vp
->v_flag
& VFREE
)
1460 strcat(buf
, "|VFREE");
1461 if (vp
->v_flag
& VOBJBUF
)
1462 strcat(buf
, "|VOBJBUF");
1464 kprintf(" flags (%s)", &buf
[1]);
1465 if (vp
->v_data
== NULL
) {
1474 #include <ddb/ddb.h>
1476 static int db_show_locked_vnodes(struct mount
*mp
, void *data
);
1479 * List all of the locked vnodes in the system.
1480 * Called when debugging the kernel.
1482 DB_SHOW_COMMAND(lockedvnodes
, lockedvnodes
)
1484 kprintf("Locked vnodes\n");
1485 mountlist_scan(db_show_locked_vnodes
, NULL
,
1486 MNTSCAN_FORWARD
|MNTSCAN_NOBUSY
);
1490 db_show_locked_vnodes(struct mount
*mp
, void *data __unused
)
1494 TAILQ_FOREACH(vp
, &mp
->mnt_nvnodelist
, v_nmntvnodes
) {
1495 if (vn_islocked(vp
))
1496 vprint((char *)0, vp
);
1503 * Top level filesystem related information gathering.
1505 static int sysctl_ovfs_conf (SYSCTL_HANDLER_ARGS
);
1508 vfs_sysctl(SYSCTL_HANDLER_ARGS
)
1510 int *name
= (int *)arg1
- 1; /* XXX */
1511 u_int namelen
= arg2
+ 1; /* XXX */
1512 struct vfsconf
*vfsp
;
1514 #if 1 || defined(COMPAT_PRELITE2)
1515 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
1517 return (sysctl_ovfs_conf(oidp
, arg1
, arg2
, req
));
1521 /* all sysctl names at this level are at least name and field */
1523 return (ENOTDIR
); /* overloaded */
1524 if (name
[0] != VFS_GENERIC
) {
1525 for (vfsp
= vfsconf
; vfsp
; vfsp
= vfsp
->vfc_next
)
1526 if (vfsp
->vfc_typenum
== name
[0])
1529 return (EOPNOTSUPP
);
1530 return ((*vfsp
->vfc_vfsops
->vfs_sysctl
)(&name
[1], namelen
- 1,
1531 oldp
, oldlenp
, newp
, newlen
, p
));
1535 case VFS_MAXTYPENUM
:
1538 return (SYSCTL_OUT(req
, &maxvfsconf
, sizeof(int)));
1541 return (ENOTDIR
); /* overloaded */
1542 for (vfsp
= vfsconf
; vfsp
; vfsp
= vfsp
->vfc_next
)
1543 if (vfsp
->vfc_typenum
== name
[2])
1546 return (EOPNOTSUPP
);
1547 return (SYSCTL_OUT(req
, vfsp
, sizeof *vfsp
));
1549 return (EOPNOTSUPP
);
1552 SYSCTL_NODE(_vfs
, VFS_GENERIC
, generic
, CTLFLAG_RD
, vfs_sysctl
,
1553 "Generic filesystem");
1555 #if 1 || defined(COMPAT_PRELITE2)
1558 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS
)
1561 struct vfsconf
*vfsp
;
1562 struct ovfsconf ovfs
;
1564 for (vfsp
= vfsconf
; vfsp
; vfsp
= vfsp
->vfc_next
) {
1565 bzero(&ovfs
, sizeof(ovfs
));
1566 ovfs
.vfc_vfsops
= vfsp
->vfc_vfsops
; /* XXX used as flag */
1567 strcpy(ovfs
.vfc_name
, vfsp
->vfc_name
);
1568 ovfs
.vfc_index
= vfsp
->vfc_typenum
;
1569 ovfs
.vfc_refcount
= vfsp
->vfc_refcount
;
1570 ovfs
.vfc_flags
= vfsp
->vfc_flags
;
1571 error
= SYSCTL_OUT(req
, &ovfs
, sizeof ovfs
);
1578 #endif /* 1 || COMPAT_PRELITE2 */
1581 * Check to see if a filesystem is mounted on a block device.
1584 vfs_mountedon(struct vnode
*vp
)
1588 if ((dev
= vp
->v_rdev
) == NULL
) {
1589 if (vp
->v_type
!= VBLK
)
1590 dev
= get_dev(vp
->v_uminor
, vp
->v_umajor
);
1592 if (dev
!= NULL
&& dev
->si_mountpoint
)
1598 * Unmount all filesystems. The list is traversed in reverse order
1599 * of mounting to avoid dependencies.
1602 static int vfs_umountall_callback(struct mount
*mp
, void *data
);
1605 vfs_unmountall(void)
1610 count
= mountlist_scan(vfs_umountall_callback
,
1611 NULL
, MNTSCAN_REVERSE
|MNTSCAN_NOBUSY
);
1617 vfs_umountall_callback(struct mount
*mp
, void *data
)
1621 error
= dounmount(mp
, MNT_FORCE
);
1623 mountlist_remove(mp
);
1624 kprintf("unmount of filesystem mounted from %s failed (",
1625 mp
->mnt_stat
.f_mntfromname
);
1629 kprintf("%d)\n", error
);
1635 * Build hash lists of net addresses and hang them off the mount point.
1636 * Called by ufs_mount() to set up the lists of export addresses.
1639 vfs_hang_addrlist(struct mount
*mp
, struct netexport
*nep
,
1640 const struct export_args
*argp
)
1643 struct radix_node_head
*rnh
;
1645 struct radix_node
*rn
;
1646 struct sockaddr
*saddr
, *smask
= 0;
1650 if (argp
->ex_addrlen
== 0) {
1651 if (mp
->mnt_flag
& MNT_DEFEXPORTED
)
1653 np
= &nep
->ne_defexported
;
1654 np
->netc_exflags
= argp
->ex_flags
;
1655 np
->netc_anon
= argp
->ex_anon
;
1656 np
->netc_anon
.cr_ref
= 1;
1657 mp
->mnt_flag
|= MNT_DEFEXPORTED
;
1661 if (argp
->ex_addrlen
< 0 || argp
->ex_addrlen
> MLEN
)
1663 if (argp
->ex_masklen
< 0 || argp
->ex_masklen
> MLEN
)
1666 i
= sizeof(struct netcred
) + argp
->ex_addrlen
+ argp
->ex_masklen
;
1667 np
= (struct netcred
*) kmalloc(i
, M_NETADDR
, M_WAITOK
| M_ZERO
);
1668 saddr
= (struct sockaddr
*) (np
+ 1);
1669 if ((error
= copyin(argp
->ex_addr
, (caddr_t
) saddr
, argp
->ex_addrlen
)))
1671 if (saddr
->sa_len
> argp
->ex_addrlen
)
1672 saddr
->sa_len
= argp
->ex_addrlen
;
1673 if (argp
->ex_masklen
) {
1674 smask
= (struct sockaddr
*)((caddr_t
)saddr
+ argp
->ex_addrlen
);
1675 error
= copyin(argp
->ex_mask
, (caddr_t
)smask
, argp
->ex_masklen
);
1678 if (smask
->sa_len
> argp
->ex_masklen
)
1679 smask
->sa_len
= argp
->ex_masklen
;
1681 i
= saddr
->sa_family
;
1682 if ((rnh
= nep
->ne_rtable
[i
]) == 0) {
1684 * Seems silly to initialize every AF when most are not used,
1685 * do so on demand here
1687 SLIST_FOREACH(dom
, &domains
, dom_next
)
1688 if (dom
->dom_family
== i
&& dom
->dom_rtattach
) {
1689 dom
->dom_rtattach((void **) &nep
->ne_rtable
[i
],
1693 if ((rnh
= nep
->ne_rtable
[i
]) == 0) {
1698 rn
= (*rnh
->rnh_addaddr
) ((char *) saddr
, (char *) smask
, rnh
,
1700 if (rn
== 0 || np
!= (struct netcred
*) rn
) { /* already exists */
1704 np
->netc_exflags
= argp
->ex_flags
;
1705 np
->netc_anon
= argp
->ex_anon
;
1706 np
->netc_anon
.cr_ref
= 1;
1709 kfree(np
, M_NETADDR
);
1715 vfs_free_netcred(struct radix_node
*rn
, void *w
)
1717 struct radix_node_head
*rnh
= (struct radix_node_head
*) w
;
1719 (*rnh
->rnh_deladdr
) (rn
->rn_key
, rn
->rn_mask
, rnh
);
1720 kfree((caddr_t
) rn
, M_NETADDR
);
1725 * Free the net address hash lists that are hanging off the mount points.
1728 vfs_free_addrlist(struct netexport
*nep
)
1731 struct radix_node_head
*rnh
;
1733 for (i
= 0; i
<= AF_MAX
; i
++)
1734 if ((rnh
= nep
->ne_rtable
[i
])) {
1735 (*rnh
->rnh_walktree
) (rnh
, vfs_free_netcred
,
1737 kfree((caddr_t
) rnh
, M_RTABLE
);
1738 nep
->ne_rtable
[i
] = 0;
1743 vfs_export(struct mount
*mp
, struct netexport
*nep
,
1744 const struct export_args
*argp
)
1748 if (argp
->ex_flags
& MNT_DELEXPORT
) {
1749 if (mp
->mnt_flag
& MNT_EXPUBLIC
) {
1750 vfs_setpublicfs(NULL
, NULL
, NULL
);
1751 mp
->mnt_flag
&= ~MNT_EXPUBLIC
;
1753 vfs_free_addrlist(nep
);
1754 mp
->mnt_flag
&= ~(MNT_EXPORTED
| MNT_DEFEXPORTED
);
1756 if (argp
->ex_flags
& MNT_EXPORTED
) {
1757 if (argp
->ex_flags
& MNT_EXPUBLIC
) {
1758 if ((error
= vfs_setpublicfs(mp
, nep
, argp
)) != 0)
1760 mp
->mnt_flag
|= MNT_EXPUBLIC
;
1762 if ((error
= vfs_hang_addrlist(mp
, nep
, argp
)))
1764 mp
->mnt_flag
|= MNT_EXPORTED
;
1771 * Set the publicly exported filesystem (WebNFS). Currently, only
1772 * one public filesystem is possible in the spec (RFC 2054 and 2055)
1775 vfs_setpublicfs(struct mount
*mp
, struct netexport
*nep
,
1776 const struct export_args
*argp
)
1783 * mp == NULL -> invalidate the current info, the FS is
1784 * no longer exported. May be called from either vfs_export
1785 * or unmount, so check if it hasn't already been done.
1788 if (nfs_pub
.np_valid
) {
1789 nfs_pub
.np_valid
= 0;
1790 if (nfs_pub
.np_index
!= NULL
) {
1791 FREE(nfs_pub
.np_index
, M_TEMP
);
1792 nfs_pub
.np_index
= NULL
;
1799 * Only one allowed at a time.
1801 if (nfs_pub
.np_valid
!= 0 && mp
!= nfs_pub
.np_mount
)
1805 * Get real filehandle for root of exported FS.
1807 bzero((caddr_t
)&nfs_pub
.np_handle
, sizeof(nfs_pub
.np_handle
));
1808 nfs_pub
.np_handle
.fh_fsid
= mp
->mnt_stat
.f_fsid
;
1810 if ((error
= VFS_ROOT(mp
, &rvp
)))
1813 if ((error
= VFS_VPTOFH(rvp
, &nfs_pub
.np_handle
.fh_fid
)))
1819 * If an indexfile was specified, pull it in.
1821 if (argp
->ex_indexfile
!= NULL
) {
1824 error
= vn_get_namelen(rvp
, &namelen
);
1827 MALLOC(nfs_pub
.np_index
, char *, namelen
, M_TEMP
,
1829 error
= copyinstr(argp
->ex_indexfile
, nfs_pub
.np_index
,
1830 namelen
, (size_t *)0);
1833 * Check for illegal filenames.
1835 for (cp
= nfs_pub
.np_index
; *cp
; cp
++) {
1843 FREE(nfs_pub
.np_index
, M_TEMP
);
1848 nfs_pub
.np_mount
= mp
;
1849 nfs_pub
.np_valid
= 1;
1854 vfs_export_lookup(struct mount
*mp
, struct netexport
*nep
,
1855 struct sockaddr
*nam
)
1858 struct radix_node_head
*rnh
;
1859 struct sockaddr
*saddr
;
1862 if (mp
->mnt_flag
& MNT_EXPORTED
) {
1864 * Lookup in the export list first.
1868 rnh
= nep
->ne_rtable
[saddr
->sa_family
];
1870 np
= (struct netcred
*)
1871 (*rnh
->rnh_matchaddr
)((char *)saddr
,
1873 if (np
&& np
->netc_rnodes
->rn_flags
& RNF_ROOT
)
1878 * If no address match, use the default if it exists.
1880 if (np
== NULL
&& mp
->mnt_flag
& MNT_DEFEXPORTED
)
1881 np
= &nep
->ne_defexported
;
1887 * perform msync on all vnodes under a mount point. The mount point must
1888 * be locked. This code is also responsible for lazy-freeing unreferenced
1889 * vnodes whos VM objects no longer contain pages.
1891 * NOTE: MNT_WAIT still skips vnodes in the VXLOCK state.
1893 * NOTE: XXX VOP_PUTPAGES and friends requires that the vnode be locked,
1894 * but vnode_pager_putpages() doesn't lock the vnode. We have to do it
1895 * way up in this high level function.
1897 static int vfs_msync_scan1(struct mount
*mp
, struct vnode
*vp
, void *data
);
1898 static int vfs_msync_scan2(struct mount
*mp
, struct vnode
*vp
, void *data
);
1901 vfs_msync(struct mount
*mp
, int flags
)
1905 vmsc_flags
= VMSC_GETVP
;
1906 if (flags
!= MNT_WAIT
)
1907 vmsc_flags
|= VMSC_NOWAIT
;
1908 vmntvnodescan(mp
, vmsc_flags
, vfs_msync_scan1
, vfs_msync_scan2
,
1913 * scan1 is a fast pre-check. There could be hundreds of thousands of
1914 * vnodes, we cannot afford to do anything heavy weight until we have a
1915 * fairly good indication that there is work to do.
1919 vfs_msync_scan1(struct mount
*mp
, struct vnode
*vp
, void *data
)
1921 int flags
= (int)data
;
1923 if ((vp
->v_flag
& VRECLAIMED
) == 0) {
1924 if (vshouldmsync(vp
))
1925 return(0); /* call scan2 */
1926 if ((mp
->mnt_flag
& MNT_RDONLY
) == 0 &&
1927 (vp
->v_flag
& VOBJDIRTY
) &&
1928 (flags
== MNT_WAIT
|| vn_islocked(vp
) == 0)) {
1929 return(0); /* call scan2 */
1934 * do not call scan2, continue the loop
1940 * This callback is handed a locked vnode.
1944 vfs_msync_scan2(struct mount
*mp
, struct vnode
*vp
, void *data
)
1947 int flags
= (int)data
;
1949 if (vp
->v_flag
& VRECLAIMED
)
1952 if ((mp
->mnt_flag
& MNT_RDONLY
) == 0 && (vp
->v_flag
& VOBJDIRTY
)) {
1953 if ((obj
= vp
->v_object
) != NULL
) {
1954 vm_object_page_clean(obj
, 0, 0,
1955 flags
== MNT_WAIT
? OBJPC_SYNC
: OBJPC_NOSYNC
);
1962 * Record a process's interest in events which might happen to
1963 * a vnode. Because poll uses the historic select-style interface
1964 * internally, this routine serves as both the ``check for any
1965 * pending events'' and the ``record my interest in future events''
1966 * functions. (These are done together, while the lock is held,
1967 * to avoid race conditions.)
1970 vn_pollrecord(struct vnode
*vp
, int events
)
1974 KKASSERT(curthread
->td_proc
!= NULL
);
1976 lwkt_gettoken(&ilock
, &vp
->v_pollinfo
.vpi_token
);
1977 if (vp
->v_pollinfo
.vpi_revents
& events
) {
1979 * This leaves events we are not interested
1980 * in available for the other process which
1981 * which presumably had requested them
1982 * (otherwise they would never have been
1985 events
&= vp
->v_pollinfo
.vpi_revents
;
1986 vp
->v_pollinfo
.vpi_revents
&= ~events
;
1988 lwkt_reltoken(&ilock
);
1991 vp
->v_pollinfo
.vpi_events
|= events
;
1992 selrecord(curthread
, &vp
->v_pollinfo
.vpi_selinfo
);
1993 lwkt_reltoken(&ilock
);
1998 * Note the occurrence of an event. If the VN_POLLEVENT macro is used,
1999 * it is possible for us to miss an event due to race conditions, but
2000 * that condition is expected to be rare, so for the moment it is the
2001 * preferred interface.
2004 vn_pollevent(struct vnode
*vp
, int events
)
2008 lwkt_gettoken(&ilock
, &vp
->v_pollinfo
.vpi_token
);
2009 if (vp
->v_pollinfo
.vpi_events
& events
) {
2011 * We clear vpi_events so that we don't
2012 * call selwakeup() twice if two events are
2013 * posted before the polling process(es) is
2014 * awakened. This also ensures that we take at
2015 * most one selwakeup() if the polling process
2016 * is no longer interested. However, it does
2017 * mean that only one event can be noticed at
2018 * a time. (Perhaps we should only clear those
2019 * event bits which we note?) XXX
2021 vp
->v_pollinfo
.vpi_events
= 0; /* &= ~events ??? */
2022 vp
->v_pollinfo
.vpi_revents
|= events
;
2023 selwakeup(&vp
->v_pollinfo
.vpi_selinfo
);
2025 lwkt_reltoken(&ilock
);
2029 * Wake up anyone polling on vp because it is being revoked.
2030 * This depends on dead_poll() returning POLLHUP for correct
2034 vn_pollgone(struct vnode
*vp
)
2038 lwkt_gettoken(&ilock
, &vp
->v_pollinfo
.vpi_token
);
2039 if (vp
->v_pollinfo
.vpi_events
) {
2040 vp
->v_pollinfo
.vpi_events
= 0;
2041 selwakeup(&vp
->v_pollinfo
.vpi_selinfo
);
2043 lwkt_reltoken(&ilock
);
2047 * extract the cdev_t from a VBLK or VCHR. The vnode must have been opened
2048 * (or v_rdev might be NULL).
2051 vn_todev(struct vnode
*vp
)
2053 if (vp
->v_type
!= VBLK
&& vp
->v_type
!= VCHR
)
2055 KKASSERT(vp
->v_rdev
!= NULL
);
2056 return (vp
->v_rdev
);
2060 * Check if vnode represents a disk device. The vnode does not need to be
2064 vn_isdisk(struct vnode
*vp
, int *errp
)
2068 if (vp
->v_type
!= VCHR
) {
2074 if ((dev
= vp
->v_rdev
) == NULL
)
2075 dev
= get_dev(vp
->v_umajor
, vp
->v_uminor
);
2082 if (dev_is_good(dev
) == 0) {
2087 if ((dev_dflags(dev
) & D_DISK
) == 0) {
2098 vn_get_namelen(struct vnode
*vp
, int *namelen
)
2100 int error
, retval
[2];
2102 error
= VOP_PATHCONF(vp
, _PC_NAME_MAX
, retval
);
2110 vop_write_dirent(int *error
, struct uio
*uio
, ino_t d_ino
, uint8_t d_type
,
2111 uint16_t d_namlen
, const char *d_name
)
2116 len
= _DIRENT_RECLEN(d_namlen
);
2117 if (len
> uio
->uio_resid
)
2120 dp
= kmalloc(len
, M_TEMP
, M_WAITOK
| M_ZERO
);
2123 dp
->d_namlen
= d_namlen
;
2124 dp
->d_type
= d_type
;
2125 bcopy(d_name
, dp
->d_name
, d_namlen
);
2127 *error
= uiomove((caddr_t
)dp
, len
, uio
);