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.118 2008/09/17 21:44:18 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>
57 #include <sys/kernel.h>
58 #include <sys/kthread.h>
59 #include <sys/malloc.h>
61 #include <sys/mount.h>
63 #include <sys/reboot.h>
64 #include <sys/socket.h>
66 #include <sys/sysctl.h>
67 #include <sys/syslog.h>
68 #include <sys/unistd.h>
69 #include <sys/vmmeter.h>
70 #include <sys/vnode.h>
72 #include <machine/limits.h>
75 #include <vm/vm_object.h>
76 #include <vm/vm_extern.h>
77 #include <vm/vm_kern.h>
79 #include <vm/vm_map.h>
80 #include <vm/vm_page.h>
81 #include <vm/vm_pager.h>
82 #include <vm/vnode_pager.h>
83 #include <vm/vm_zone.h>
86 #include <sys/thread2.h>
87 #include <sys/sysref2.h>
89 static MALLOC_DEFINE(M_NETADDR
, "Export Host", "Export host address structure");
92 SYSCTL_INT(_debug
, OID_AUTO
, numvnodes
, CTLFLAG_RD
, &numvnodes
, 0, "");
94 SYSCTL_INT(_vfs
, OID_AUTO
, fastdev
, CTLFLAG_RW
, &vfs_fastdev
, 0, "");
96 enum vtype iftovt_tab
[16] = {
97 VNON
, VFIFO
, VCHR
, VNON
, VDIR
, VNON
, VBLK
, VNON
,
98 VREG
, VNON
, VLNK
, VNON
, VSOCK
, VNON
, VNON
, VBAD
,
100 int vttoif_tab
[9] = {
101 0, S_IFREG
, S_IFDIR
, S_IFBLK
, S_IFCHR
, S_IFLNK
,
102 S_IFSOCK
, S_IFIFO
, S_IFMT
,
105 static int reassignbufcalls
;
106 SYSCTL_INT(_vfs
, OID_AUTO
, reassignbufcalls
, CTLFLAG_RW
,
107 &reassignbufcalls
, 0, "");
108 static int reassignbufloops
;
109 SYSCTL_INT(_vfs
, OID_AUTO
, reassignbufloops
, CTLFLAG_RW
,
110 &reassignbufloops
, 0, "");
111 static int reassignbufsortgood
;
112 SYSCTL_INT(_vfs
, OID_AUTO
, reassignbufsortgood
, CTLFLAG_RW
,
113 &reassignbufsortgood
, 0, "");
114 static int reassignbufsortbad
;
115 SYSCTL_INT(_vfs
, OID_AUTO
, reassignbufsortbad
, CTLFLAG_RW
,
116 &reassignbufsortbad
, 0, "");
117 static int reassignbufmethod
= 1;
118 SYSCTL_INT(_vfs
, OID_AUTO
, reassignbufmethod
, CTLFLAG_RW
,
119 &reassignbufmethod
, 0, "");
121 int nfs_mount_type
= -1;
122 static struct lwkt_token spechash_token
;
123 struct nfs_public nfs_pub
; /* publicly exported FS */
126 SYSCTL_INT(_kern
, KERN_MAXVNODES
, maxvnodes
, CTLFLAG_RW
,
127 &desiredvnodes
, 0, "Maximum number of vnodes");
129 static void vfs_free_addrlist (struct netexport
*nep
);
130 static int vfs_free_netcred (struct radix_node
*rn
, void *w
);
131 static int vfs_hang_addrlist (struct mount
*mp
, struct netexport
*nep
,
132 const struct export_args
*argp
);
134 extern int dev_ref_debug
;
137 * Red black tree functions
139 static int rb_buf_compare(struct buf
*b1
, struct buf
*b2
);
140 RB_GENERATE2(buf_rb_tree
, buf
, b_rbnode
, rb_buf_compare
, off_t
, b_loffset
);
141 RB_GENERATE2(buf_rb_hash
, buf
, b_rbhash
, rb_buf_compare
, off_t
, b_loffset
);
144 rb_buf_compare(struct buf
*b1
, struct buf
*b2
)
146 if (b1
->b_loffset
< b2
->b_loffset
)
148 if (b1
->b_loffset
> b2
->b_loffset
)
154 * Returns non-zero if the vnode is a candidate for lazy msyncing.
157 vshouldmsync(struct vnode
*vp
)
159 if (vp
->v_auxrefs
!= 0 || vp
->v_sysref
.refcnt
> 0)
160 return (0); /* other holders */
162 (vp
->v_object
->ref_count
|| vp
->v_object
->resident_page_count
)) {
169 * Initialize the vnode management data structures.
171 * Called from vfsinit()
177 * Desiredvnodes is kern.maxvnodes. We want to scale it
178 * according to available system memory but we may also have
179 * to limit it based on available KVM, which is capped on 32 bit
182 desiredvnodes
= min(maxproc
+ vmstats
.v_page_count
/ 4,
184 (sizeof(struct vm_object
) + sizeof(struct vnode
))));
186 lwkt_token_init(&spechash_token
);
190 * Knob to control the precision of file timestamps:
192 * 0 = seconds only; nanoseconds zeroed.
193 * 1 = seconds and nanoseconds, accurate within 1/HZ.
194 * 2 = seconds and nanoseconds, truncated to microseconds.
195 * >=3 = seconds and nanoseconds, maximum precision.
197 enum { TSP_SEC
, TSP_HZ
, TSP_USEC
, TSP_NSEC
};
199 static int timestamp_precision
= TSP_SEC
;
200 SYSCTL_INT(_vfs
, OID_AUTO
, timestamp_precision
, CTLFLAG_RW
,
201 ×tamp_precision
, 0, "");
204 * Get a current timestamp.
207 vfs_timestamp(struct timespec
*tsp
)
211 switch (timestamp_precision
) {
213 tsp
->tv_sec
= time_second
;
221 TIMEVAL_TO_TIMESPEC(&tv
, tsp
);
231 * Set vnode attributes to VNOVAL
234 vattr_null(struct vattr
*vap
)
237 vap
->va_size
= VNOVAL
;
238 vap
->va_bytes
= VNOVAL
;
239 vap
->va_mode
= VNOVAL
;
240 vap
->va_nlink
= VNOVAL
;
241 vap
->va_uid
= VNOVAL
;
242 vap
->va_gid
= VNOVAL
;
243 vap
->va_fsid
= VNOVAL
;
244 vap
->va_fileid
= VNOVAL
;
245 vap
->va_blocksize
= VNOVAL
;
246 vap
->va_rmajor
= VNOVAL
;
247 vap
->va_rminor
= VNOVAL
;
248 vap
->va_atime
.tv_sec
= VNOVAL
;
249 vap
->va_atime
.tv_nsec
= VNOVAL
;
250 vap
->va_mtime
.tv_sec
= VNOVAL
;
251 vap
->va_mtime
.tv_nsec
= VNOVAL
;
252 vap
->va_ctime
.tv_sec
= VNOVAL
;
253 vap
->va_ctime
.tv_nsec
= VNOVAL
;
254 vap
->va_flags
= VNOVAL
;
255 vap
->va_gen
= VNOVAL
;
257 vap
->va_fsmid
= VNOVAL
;
258 /* va_*_uuid fields are only valid if related flags are set */
262 * Flush out and invalidate all buffers associated with a vnode.
266 static int vinvalbuf_bp(struct buf
*bp
, void *data
);
268 struct vinvalbuf_bp_info
{
276 vupdatefsmid(struct vnode
*vp
)
278 atomic_set_int(&vp
->v_flag
, VFSMID
);
282 vinvalbuf(struct vnode
*vp
, int flags
, int slpflag
, int slptimeo
)
284 struct vinvalbuf_bp_info info
;
289 * If we are being asked to save, call fsync to ensure that the inode
292 if (flags
& V_SAVE
) {
294 while (vp
->v_track_write
.bk_active
) {
295 vp
->v_track_write
.bk_waitflag
= 1;
296 error
= tsleep(&vp
->v_track_write
, slpflag
,
297 "vinvlbuf", slptimeo
);
303 if (!RB_EMPTY(&vp
->v_rbdirty_tree
)) {
305 if ((error
= VOP_FSYNC(vp
, MNT_WAIT
)) != 0)
310 * Dirty bufs may be left or generated via races
311 * in circumstances where vinvalbuf() is called on
312 * a vnode not undergoing reclamation. Only
313 * panic if we are trying to reclaim the vnode.
315 if ((vp
->v_flag
& VRECLAIMED
) &&
316 (vp
->v_track_write
.bk_active
> 0 ||
317 !RB_EMPTY(&vp
->v_rbdirty_tree
))) {
318 panic("vinvalbuf: dirty bufs");
324 info
.slptimeo
= slptimeo
;
325 info
.lkflags
= LK_EXCLUSIVE
| LK_SLEEPFAIL
;
326 if (slpflag
& PCATCH
)
327 info
.lkflags
|= LK_PCATCH
;
332 * Flush the buffer cache until nothing is left.
334 while (!RB_EMPTY(&vp
->v_rbclean_tree
) ||
335 !RB_EMPTY(&vp
->v_rbdirty_tree
)) {
336 error
= RB_SCAN(buf_rb_tree
, &vp
->v_rbclean_tree
, NULL
,
337 vinvalbuf_bp
, &info
);
339 error
= RB_SCAN(buf_rb_tree
, &vp
->v_rbdirty_tree
, NULL
,
340 vinvalbuf_bp
, &info
);
345 * Wait for I/O to complete. XXX needs cleaning up. The vnode can
346 * have write I/O in-progress but if there is a VM object then the
347 * VM object can also have read-I/O in-progress.
350 while (vp
->v_track_write
.bk_active
> 0) {
351 vp
->v_track_write
.bk_waitflag
= 1;
352 tsleep(&vp
->v_track_write
, 0, "vnvlbv", 0);
354 if ((object
= vp
->v_object
) != NULL
) {
355 while (object
->paging_in_progress
)
356 vm_object_pip_sleep(object
, "vnvlbx");
358 } while (vp
->v_track_write
.bk_active
> 0);
363 * Destroy the copy in the VM cache, too.
365 if ((object
= vp
->v_object
) != NULL
) {
366 vm_object_page_remove(object
, 0, 0,
367 (flags
& V_SAVE
) ? TRUE
: FALSE
);
370 if (!RB_EMPTY(&vp
->v_rbdirty_tree
) || !RB_EMPTY(&vp
->v_rbclean_tree
))
371 panic("vinvalbuf: flush failed");
372 if (!RB_EMPTY(&vp
->v_rbhash_tree
))
373 panic("vinvalbuf: flush failed, buffers still present");
378 vinvalbuf_bp(struct buf
*bp
, void *data
)
380 struct vinvalbuf_bp_info
*info
= data
;
383 if (BUF_LOCK(bp
, LK_EXCLUSIVE
| LK_NOWAIT
)) {
384 error
= BUF_TIMELOCK(bp
, info
->lkflags
,
385 "vinvalbuf", info
->slptimeo
);
395 KKASSERT(bp
->b_vp
== info
->vp
);
398 * XXX Since there are no node locks for NFS, I
399 * believe there is a slight chance that a delayed
400 * write will occur while sleeping just above, so
401 * check for it. Note that vfs_bio_awrite expects
402 * buffers to reside on a queue, while bwrite() and
405 * NOTE: NO B_LOCKED CHECK. Also no buf_checkwrite()
406 * check. This code will write out the buffer, period.
408 if (((bp
->b_flags
& (B_DELWRI
| B_INVAL
)) == B_DELWRI
) &&
409 (info
->flags
& V_SAVE
)) {
410 if (bp
->b_vp
== info
->vp
) {
411 if (bp
->b_flags
& B_CLUSTEROK
) {
415 bp
->b_flags
|= B_ASYNC
;
422 } else if (info
->flags
& V_SAVE
) {
424 * Cannot set B_NOCACHE on a clean buffer as this will
425 * destroy the VM backing store which might actually
426 * be dirty (and unsynchronized).
429 bp
->b_flags
|= (B_INVAL
| B_RELBUF
);
430 bp
->b_flags
&= ~B_ASYNC
;
434 bp
->b_flags
|= (B_INVAL
| B_NOCACHE
| B_RELBUF
);
435 bp
->b_flags
&= ~B_ASYNC
;
442 * Truncate a file's buffer and pages to a specified length. This
443 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
446 * The vnode must be locked.
448 static int vtruncbuf_bp_trunc_cmp(struct buf
*bp
, void *data
);
449 static int vtruncbuf_bp_trunc(struct buf
*bp
, void *data
);
450 static int vtruncbuf_bp_metasync_cmp(struct buf
*bp
, void *data
);
451 static int vtruncbuf_bp_metasync(struct buf
*bp
, void *data
);
454 vtruncbuf(struct vnode
*vp
, off_t length
, int blksize
)
458 const char *filename
;
461 * Round up to the *next* block, then destroy the buffers in question.
462 * Since we are only removing some of the buffers we must rely on the
463 * scan count to determine whether a loop is necessary.
465 if ((count
= (int)(length
% blksize
)) != 0)
466 truncloffset
= length
+ (blksize
- count
);
468 truncloffset
= length
;
472 count
= RB_SCAN(buf_rb_tree
, &vp
->v_rbclean_tree
,
473 vtruncbuf_bp_trunc_cmp
,
474 vtruncbuf_bp_trunc
, &truncloffset
);
475 count
+= RB_SCAN(buf_rb_tree
, &vp
->v_rbdirty_tree
,
476 vtruncbuf_bp_trunc_cmp
,
477 vtruncbuf_bp_trunc
, &truncloffset
);
481 * For safety, fsync any remaining metadata if the file is not being
482 * truncated to 0. Since the metadata does not represent the entire
483 * dirty list we have to rely on the hit count to ensure that we get
488 count
= RB_SCAN(buf_rb_tree
, &vp
->v_rbdirty_tree
,
489 vtruncbuf_bp_metasync_cmp
,
490 vtruncbuf_bp_metasync
, vp
);
495 * Clean out any left over VM backing store.
499 vnode_pager_setsize(vp
, length
);
504 * It is possible to have in-progress I/O from buffers that were
505 * not part of the truncation. This should not happen if we
506 * are truncating to 0-length.
508 filename
= TAILQ_FIRST(&vp
->v_namecache
) ?
509 TAILQ_FIRST(&vp
->v_namecache
)->nc_name
: "?";
511 while ((count
= vp
->v_track_write
.bk_active
) > 0) {
512 vp
->v_track_write
.bk_waitflag
= 1;
513 tsleep(&vp
->v_track_write
, 0, "vbtrunc", 0);
515 kprintf("Warning: vtruncbuf(): Had to wait for "
516 "%d buffer I/Os to finish in %s\n",
522 * Make sure no buffers were instantiated while we were trying
523 * to clean out the remaining VM pages. This could occur due
524 * to busy dirty VM pages being flushed out to disk.
527 count
= RB_SCAN(buf_rb_tree
, &vp
->v_rbclean_tree
,
528 vtruncbuf_bp_trunc_cmp
,
529 vtruncbuf_bp_trunc
, &truncloffset
);
530 count
+= RB_SCAN(buf_rb_tree
, &vp
->v_rbdirty_tree
,
531 vtruncbuf_bp_trunc_cmp
,
532 vtruncbuf_bp_trunc
, &truncloffset
);
534 kprintf("Warning: vtruncbuf(): Had to re-clean %d "
535 "left over buffers in %s\n", count
, filename
);
545 * The callback buffer is beyond the new file EOF and must be destroyed.
546 * Note that the compare function must conform to the RB_SCAN's requirements.
550 vtruncbuf_bp_trunc_cmp(struct buf
*bp
, void *data
)
552 if (bp
->b_loffset
>= *(off_t
*)data
)
559 vtruncbuf_bp_trunc(struct buf
*bp
, void *data
)
562 * Do not try to use a buffer we cannot immediately lock, but sleep
563 * anyway to prevent a livelock. The code will loop until all buffers
566 if (BUF_LOCK(bp
, LK_EXCLUSIVE
| LK_NOWAIT
)) {
567 if (BUF_LOCK(bp
, LK_EXCLUSIVE
|LK_SLEEPFAIL
) == 0)
571 bp
->b_flags
|= (B_INVAL
| B_RELBUF
| B_NOCACHE
);
572 bp
->b_flags
&= ~B_ASYNC
;
579 * Fsync all meta-data after truncating a file to be non-zero. Only metadata
580 * blocks (with a negative loffset) are scanned.
581 * Note that the compare function must conform to the RB_SCAN's requirements.
584 vtruncbuf_bp_metasync_cmp(struct buf
*bp
, void *data
)
586 if (bp
->b_loffset
< 0)
592 vtruncbuf_bp_metasync(struct buf
*bp
, void *data
)
594 struct vnode
*vp
= data
;
596 if (bp
->b_flags
& B_DELWRI
) {
598 * Do not try to use a buffer we cannot immediately lock,
599 * but sleep anyway to prevent a livelock. The code will
600 * loop until all buffers can be acted upon.
602 if (BUF_LOCK(bp
, LK_EXCLUSIVE
| LK_NOWAIT
)) {
603 if (BUF_LOCK(bp
, LK_EXCLUSIVE
|LK_SLEEPFAIL
) == 0)
607 if (bp
->b_vp
== vp
) {
608 bp
->b_flags
|= B_ASYNC
;
610 bp
->b_flags
&= ~B_ASYNC
;
621 * vfsync - implements a multipass fsync on a file which understands
622 * dependancies and meta-data. The passed vnode must be locked. The
623 * waitfor argument may be MNT_WAIT or MNT_NOWAIT, or MNT_LAZY.
625 * When fsyncing data asynchronously just do one consolidated pass starting
626 * with the most negative block number. This may not get all the data due
629 * When fsyncing data synchronously do a data pass, then a metadata pass,
630 * then do additional data+metadata passes to try to get all the data out.
632 static int vfsync_wait_output(struct vnode
*vp
,
633 int (*waitoutput
)(struct vnode
*, struct thread
*));
634 static int vfsync_data_only_cmp(struct buf
*bp
, void *data
);
635 static int vfsync_meta_only_cmp(struct buf
*bp
, void *data
);
636 static int vfsync_lazy_range_cmp(struct buf
*bp
, void *data
);
637 static int vfsync_bp(struct buf
*bp
, void *data
);
646 int (*checkdef
)(struct buf
*);
650 vfsync(struct vnode
*vp
, int waitfor
, int passes
,
651 int (*checkdef
)(struct buf
*),
652 int (*waitoutput
)(struct vnode
*, struct thread
*))
654 struct vfsync_info info
;
657 bzero(&info
, sizeof(info
));
659 if ((info
.checkdef
= checkdef
) == NULL
)
662 crit_enter_id("vfsync");
667 * Lazy (filesystem syncer typ) Asynchronous plus limit the
668 * number of data (not meta) pages we try to flush to 1MB.
669 * A non-zero return means that lazy limit was reached.
671 info
.lazylimit
= 1024 * 1024;
673 error
= RB_SCAN(buf_rb_tree
, &vp
->v_rbdirty_tree
,
674 vfsync_lazy_range_cmp
, vfsync_bp
, &info
);
675 RB_SCAN(buf_rb_tree
, &vp
->v_rbdirty_tree
,
676 vfsync_meta_only_cmp
, vfsync_bp
, &info
);
679 else if (!RB_EMPTY(&vp
->v_rbdirty_tree
))
680 vn_syncer_add_to_worklist(vp
, 1);
685 * Asynchronous. Do a data-only pass and a meta-only pass.
688 RB_SCAN(buf_rb_tree
, &vp
->v_rbdirty_tree
, vfsync_data_only_cmp
,
690 RB_SCAN(buf_rb_tree
, &vp
->v_rbdirty_tree
, vfsync_meta_only_cmp
,
696 * Synchronous. Do a data-only pass, then a meta-data+data
697 * pass, then additional integrated passes to try to get
698 * all the dependancies flushed.
700 RB_SCAN(buf_rb_tree
, &vp
->v_rbdirty_tree
, vfsync_data_only_cmp
,
702 error
= vfsync_wait_output(vp
, waitoutput
);
704 info
.skippedbufs
= 0;
705 RB_SCAN(buf_rb_tree
, &vp
->v_rbdirty_tree
, NULL
,
707 error
= vfsync_wait_output(vp
, waitoutput
);
708 if (info
.skippedbufs
)
709 kprintf("Warning: vfsync skipped %d dirty bufs in pass2!\n", info
.skippedbufs
);
711 while (error
== 0 && passes
> 0 &&
712 !RB_EMPTY(&vp
->v_rbdirty_tree
)) {
714 info
.synchronous
= 1;
717 error
= RB_SCAN(buf_rb_tree
, &vp
->v_rbdirty_tree
, NULL
,
723 error
= vfsync_wait_output(vp
, waitoutput
);
727 crit_exit_id("vfsync");
732 vfsync_wait_output(struct vnode
*vp
, int (*waitoutput
)(struct vnode
*, struct thread
*))
736 while (vp
->v_track_write
.bk_active
) {
737 vp
->v_track_write
.bk_waitflag
= 1;
738 tsleep(&vp
->v_track_write
, 0, "fsfsn", 0);
741 error
= waitoutput(vp
, curthread
);
746 vfsync_data_only_cmp(struct buf
*bp
, void *data
)
748 if (bp
->b_loffset
< 0)
754 vfsync_meta_only_cmp(struct buf
*bp
, void *data
)
756 if (bp
->b_loffset
< 0)
762 vfsync_lazy_range_cmp(struct buf
*bp
, void *data
)
764 struct vfsync_info
*info
= data
;
765 if (bp
->b_loffset
< info
->vp
->v_lazyw
)
771 vfsync_bp(struct buf
*bp
, void *data
)
773 struct vfsync_info
*info
= data
;
774 struct vnode
*vp
= info
->vp
;
778 * if syncdeps is not set we do not try to write buffers which have
781 if (!info
->synchronous
&& info
->syncdeps
== 0 && info
->checkdef(bp
))
785 * Ignore buffers that we cannot immediately lock. XXX
787 if (BUF_LOCK(bp
, LK_EXCLUSIVE
| LK_NOWAIT
)) {
788 kprintf("Warning: vfsync_bp skipping dirty buffer %p\n", bp
);
792 if ((bp
->b_flags
& B_DELWRI
) == 0)
793 panic("vfsync_bp: buffer not dirty");
795 panic("vfsync_bp: buffer vp mismatch");
798 * B_NEEDCOMMIT (primarily used by NFS) is a state where the buffer
799 * has been written but an additional handshake with the device
800 * is required before we can dispose of the buffer. We have no idea
801 * how to do this so we have to skip these buffers.
803 if (bp
->b_flags
& B_NEEDCOMMIT
) {
809 * Ask bioops if it is ok to sync
811 if (LIST_FIRST(&bp
->b_dep
) != NULL
&& buf_checkwrite(bp
)) {
817 if (info
->synchronous
) {
819 * Synchronous flushing. An error may be returned.
822 crit_exit_id("vfsync");
824 crit_enter_id("vfsync");
827 * Asynchronous flushing. A negative return value simply
828 * stops the scan and is not considered an error. We use
829 * this to support limited MNT_LAZY flushes.
831 vp
->v_lazyw
= bp
->b_loffset
;
832 if ((vp
->v_flag
& VOBJBUF
) && (bp
->b_flags
& B_CLUSTEROK
)) {
833 info
->lazycount
+= vfs_bio_awrite(bp
);
835 info
->lazycount
+= bp
->b_bufsize
;
837 crit_exit_id("vfsync");
839 crit_enter_id("vfsync");
841 if (info
->lazylimit
&& info
->lazycount
>= info
->lazylimit
)
850 * Associate a buffer with a vnode.
853 bgetvp(struct vnode
*vp
, struct buf
*bp
)
855 KASSERT(bp
->b_vp
== NULL
, ("bgetvp: not free"));
856 KKASSERT((bp
->b_flags
& (B_HASHED
|B_DELWRI
|B_VNCLEAN
|B_VNDIRTY
)) == 0);
860 * Insert onto list for new vnode.
864 bp
->b_flags
|= B_HASHED
;
865 if (buf_rb_hash_RB_INSERT(&vp
->v_rbhash_tree
, bp
))
866 panic("reassignbuf: dup lblk vp %p bp %p", vp
, bp
);
868 bp
->b_flags
|= B_VNCLEAN
;
869 if (buf_rb_tree_RB_INSERT(&vp
->v_rbclean_tree
, bp
))
870 panic("reassignbuf: dup lblk/clean vp %p bp %p", vp
, bp
);
875 * Disassociate a buffer from a vnode.
878 brelvp(struct buf
*bp
)
882 KASSERT(bp
->b_vp
!= NULL
, ("brelvp: NULL"));
885 * Delete from old vnode list, if on one.
889 if (bp
->b_flags
& (B_VNDIRTY
| B_VNCLEAN
)) {
890 if (bp
->b_flags
& B_VNDIRTY
)
891 buf_rb_tree_RB_REMOVE(&vp
->v_rbdirty_tree
, bp
);
893 buf_rb_tree_RB_REMOVE(&vp
->v_rbclean_tree
, bp
);
894 bp
->b_flags
&= ~(B_VNDIRTY
| B_VNCLEAN
);
896 if (bp
->b_flags
& B_HASHED
) {
897 buf_rb_hash_RB_REMOVE(&vp
->v_rbhash_tree
, bp
);
898 bp
->b_flags
&= ~B_HASHED
;
900 if ((vp
->v_flag
& VONWORKLST
) && RB_EMPTY(&vp
->v_rbdirty_tree
)) {
901 vp
->v_flag
&= ~VONWORKLST
;
902 LIST_REMOVE(vp
, v_synclist
);
910 * Reassign the buffer to the proper clean/dirty list based on B_DELWRI.
911 * This routine is called when the state of the B_DELWRI bit is changed.
914 reassignbuf(struct buf
*bp
)
916 struct vnode
*vp
= bp
->b_vp
;
919 KKASSERT(vp
!= NULL
);
923 * B_PAGING flagged buffers cannot be reassigned because their vp
924 * is not fully linked in.
926 if (bp
->b_flags
& B_PAGING
)
927 panic("cannot reassign paging buffer");
930 if (bp
->b_flags
& B_DELWRI
) {
932 * Move to the dirty list, add the vnode to the worklist
934 if (bp
->b_flags
& B_VNCLEAN
) {
935 buf_rb_tree_RB_REMOVE(&vp
->v_rbclean_tree
, bp
);
936 bp
->b_flags
&= ~B_VNCLEAN
;
938 if ((bp
->b_flags
& B_VNDIRTY
) == 0) {
939 if (buf_rb_tree_RB_INSERT(&vp
->v_rbdirty_tree
, bp
)) {
940 panic("reassignbuf: dup lblk vp %p bp %p",
943 bp
->b_flags
|= B_VNDIRTY
;
945 if ((vp
->v_flag
& VONWORKLST
) == 0) {
946 switch (vp
->v_type
) {
953 vp
->v_rdev
->si_mountpoint
!= NULL
) {
961 vn_syncer_add_to_worklist(vp
, delay
);
965 * Move to the clean list, remove the vnode from the worklist
966 * if no dirty blocks remain.
968 if (bp
->b_flags
& B_VNDIRTY
) {
969 buf_rb_tree_RB_REMOVE(&vp
->v_rbdirty_tree
, bp
);
970 bp
->b_flags
&= ~B_VNDIRTY
;
972 if ((bp
->b_flags
& B_VNCLEAN
) == 0) {
973 if (buf_rb_tree_RB_INSERT(&vp
->v_rbclean_tree
, bp
)) {
974 panic("reassignbuf: dup lblk vp %p bp %p",
977 bp
->b_flags
|= B_VNCLEAN
;
979 if ((vp
->v_flag
& VONWORKLST
) &&
980 RB_EMPTY(&vp
->v_rbdirty_tree
)) {
981 vp
->v_flag
&= ~VONWORKLST
;
982 LIST_REMOVE(vp
, v_synclist
);
989 * Create a vnode for a block device.
990 * Used for mounting the root file system.
993 bdevvp(cdev_t dev
, struct vnode
**vpp
)
1003 error
= getspecialvnode(VT_NON
, NULL
, &spec_vnode_vops_p
, &nvp
, 0, 0);
1010 vp
->v_umajor
= dev
->si_umajor
;
1011 vp
->v_uminor
= dev
->si_uminor
;
1018 v_associate_rdev(struct vnode
*vp
, cdev_t dev
)
1024 if (dev_is_good(dev
) == 0)
1026 KKASSERT(vp
->v_rdev
== NULL
);
1029 vp
->v_rdev
= reference_dev(dev
);
1030 lwkt_gettoken(&ilock
, &spechash_token
);
1031 SLIST_INSERT_HEAD(&dev
->si_hlist
, vp
, v_cdevnext
);
1032 lwkt_reltoken(&ilock
);
1037 v_release_rdev(struct vnode
*vp
)
1042 if ((dev
= vp
->v_rdev
) != NULL
) {
1043 lwkt_gettoken(&ilock
, &spechash_token
);
1044 SLIST_REMOVE(&dev
->si_hlist
, vp
, vnode
, v_cdevnext
);
1047 lwkt_reltoken(&ilock
);
1052 * Add a vnode to the alias list hung off the cdev_t. We only associate
1053 * the device number with the vnode. The actual device is not associated
1054 * until the vnode is opened (usually in spec_open()), and will be
1055 * disassociated on last close.
1058 addaliasu(struct vnode
*nvp
, int x
, int y
)
1060 if (nvp
->v_type
!= VBLK
&& nvp
->v_type
!= VCHR
)
1061 panic("addaliasu on non-special vnode");
1067 * Simple call that a filesystem can make to try to get rid of a
1068 * vnode. It will fail if anyone is referencing the vnode (including
1071 * The filesystem can check whether its in-memory inode structure still
1072 * references the vp on return.
1075 vclean_unlocked(struct vnode
*vp
)
1078 if (sysref_isactive(&vp
->v_sysref
) == 0)
1084 * Disassociate a vnode from its underlying filesystem.
1086 * The vnode must be VX locked and referenced. In all normal situations
1087 * there are no active references. If vclean_vxlocked() is called while
1088 * there are active references, the vnode is being ripped out and we have
1089 * to call VOP_CLOSE() as appropriate before we can reclaim it.
1092 vclean_vxlocked(struct vnode
*vp
, int flags
)
1099 * If the vnode has already been reclaimed we have nothing to do.
1101 if (vp
->v_flag
& VRECLAIMED
)
1103 vp
->v_flag
|= VRECLAIMED
;
1106 * Scrap the vfs cache
1108 while (cache_inval_vp(vp
, 0) != 0) {
1109 kprintf("Warning: vnode %p clean/cache_resolution race detected\n", vp
);
1110 tsleep(vp
, 0, "vclninv", 2);
1114 * Check to see if the vnode is in use. If so we have to reference it
1115 * before we clean it out so that its count cannot fall to zero and
1116 * generate a race against ourselves to recycle it.
1118 active
= sysref_isactive(&vp
->v_sysref
);
1121 * Clean out any buffers associated with the vnode and destroy its
1122 * object, if it has one.
1124 vinvalbuf(vp
, V_SAVE
, 0, 0);
1127 * If purging an active vnode (typically during a forced unmount
1128 * or reboot), it must be closed and deactivated before being
1129 * reclaimed. This isn't really all that safe, but what can
1132 * Note that neither of these routines unlocks the vnode.
1134 if (active
&& (flags
& DOCLOSE
)) {
1135 while ((n
= vp
->v_opencount
) != 0) {
1136 if (vp
->v_writecount
)
1137 VOP_CLOSE(vp
, FWRITE
|FNONBLOCK
);
1139 VOP_CLOSE(vp
, FNONBLOCK
);
1140 if (vp
->v_opencount
== n
) {
1141 kprintf("Warning: unable to force-close"
1149 * If the vnode has not been deactivated, deactivated it. Deactivation
1150 * can create new buffers and VM pages so we have to call vinvalbuf()
1151 * again to make sure they all get flushed.
1153 * This can occur if a file with a link count of 0 needs to be
1156 if ((vp
->v_flag
& VINACTIVE
) == 0) {
1157 vp
->v_flag
|= VINACTIVE
;
1159 vinvalbuf(vp
, V_SAVE
, 0, 0);
1163 * If the vnode has an object, destroy it.
1165 if ((object
= vp
->v_object
) != NULL
) {
1166 if (object
->ref_count
== 0) {
1167 if ((object
->flags
& OBJ_DEAD
) == 0)
1168 vm_object_terminate(object
);
1170 vm_pager_deallocate(object
);
1172 vp
->v_flag
&= ~VOBJBUF
;
1174 KKASSERT((vp
->v_flag
& VOBJBUF
) == 0);
1177 * Reclaim the vnode.
1179 if (VOP_RECLAIM(vp
))
1180 panic("vclean: cannot reclaim");
1183 * Done with purge, notify sleepers of the grim news.
1185 vp
->v_ops
= &dead_vnode_vops_p
;
1190 * If we are destroying an active vnode, reactivate it now that
1191 * we have reassociated it with deadfs. This prevents the system
1192 * from crashing on the vnode due to it being unexpectedly marked
1193 * as inactive or reclaimed.
1195 if (active
&& (flags
& DOCLOSE
)) {
1196 vp
->v_flag
&= ~(VINACTIVE
|VRECLAIMED
);
1201 * Eliminate all activity associated with the requested vnode
1202 * and with all vnodes aliased to the requested vnode.
1204 * The vnode must be referenced but should not be locked.
1207 vrevoke(struct vnode
*vp
, struct ucred
*cred
)
1216 * If the vnode has a device association, scrap all vnodes associated
1217 * with the device. Don't let the device disappear on us while we
1218 * are scrapping the vnodes.
1220 * The passed vp will probably show up in the list, do not VX lock
1223 * Releasing the vnode's rdev here can mess up specfs's call to
1224 * device close, so don't do it. The vnode has been disassociated
1225 * and the device will be closed after the last ref on the related
1226 * fp goes away (if not still open by e.g. the kernel).
1228 if (vp
->v_type
!= VCHR
) {
1229 error
= fdrevoke(vp
, DTYPE_VNODE
, cred
);
1232 if ((dev
= vp
->v_rdev
) == NULL
) {
1233 if ((dev
= get_dev(vp
->v_umajor
, vp
->v_uminor
)) == NULL
)
1237 lwkt_gettoken(&ilock
, &spechash_token
);
1239 vqn
= SLIST_FIRST(&dev
->si_hlist
);
1242 while ((vq
= vqn
) != NULL
) {
1243 vqn
= SLIST_NEXT(vqn
, v_cdevnext
);
1246 fdrevoke(vq
, DTYPE_VNODE
, cred
);
1247 /*v_release_rdev(vq);*/
1250 lwkt_reltoken(&ilock
);
1257 * This is called when the object underlying a vnode is being destroyed,
1258 * such as in a remove(). Try to recycle the vnode immediately if the
1259 * only active reference is our reference.
1261 * Directory vnodes in the namecache with children cannot be immediately
1262 * recycled because numerous VOP_N*() ops require them to be stable.
1265 vrecycle(struct vnode
*vp
)
1267 if (vp
->v_sysref
.refcnt
<= 1) {
1268 if (cache_inval_vp_nonblock(vp
))
1277 * Return the maximum I/O size allowed for strategy calls on VP.
1279 * If vp is VCHR or VBLK we dive the device, otherwise we use
1280 * the vp's mount info.
1283 vmaxiosize(struct vnode
*vp
)
1285 if (vp
->v_type
== VBLK
|| vp
->v_type
== VCHR
) {
1286 return(vp
->v_rdev
->si_iosize_max
);
1288 return(vp
->v_mount
->mnt_iosize_max
);
1293 * Eliminate all activity associated with a vnode in preparation for reuse.
1295 * The vnode must be VX locked and refd and will remain VX locked and refd
1296 * on return. This routine may be called with the vnode in any state, as
1297 * long as it is VX locked. The vnode will be cleaned out and marked
1298 * VRECLAIMED but will not actually be reused until all existing refs and
1301 * NOTE: This routine may be called on a vnode which has not yet been
1302 * already been deactivated (VOP_INACTIVE), or on a vnode which has
1303 * already been reclaimed.
1305 * This routine is not responsible for placing us back on the freelist.
1306 * Instead, it happens automatically when the caller releases the VX lock
1307 * (assuming there aren't any other references).
1311 vgone_vxlocked(struct vnode
*vp
)
1314 * assert that the VX lock is held. This is an absolute requirement
1315 * now for vgone_vxlocked() to be called.
1317 KKASSERT(vp
->v_lock
.lk_exclusivecount
== 1);
1320 * Clean out the filesystem specific data and set the VRECLAIMED
1321 * bit. Also deactivate the vnode if necessary.
1323 vclean_vxlocked(vp
, DOCLOSE
);
1326 * Delete from old mount point vnode list, if on one.
1328 if (vp
->v_mount
!= NULL
)
1329 insmntque(vp
, NULL
);
1332 * If special device, remove it from special device alias list
1333 * if it is on one. This should normally only occur if a vnode is
1334 * being revoked as the device should otherwise have been released
1337 if ((vp
->v_type
== VBLK
|| vp
->v_type
== VCHR
) && vp
->v_rdev
!= NULL
) {
1348 * Lookup a vnode by device number.
1350 * Returns non-zero and *vpp set to a vref'd vnode on success.
1351 * Returns zero on failure.
1354 vfinddev(cdev_t dev
, enum vtype type
, struct vnode
**vpp
)
1359 lwkt_gettoken(&ilock
, &spechash_token
);
1360 SLIST_FOREACH(vp
, &dev
->si_hlist
, v_cdevnext
) {
1361 if (type
== vp
->v_type
) {
1364 lwkt_reltoken(&ilock
);
1368 lwkt_reltoken(&ilock
);
1373 * Calculate the total number of references to a special device. This
1374 * routine may only be called for VBLK and VCHR vnodes since v_rdev is
1375 * an overloaded field. Since udev2dev can now return NULL, we have
1376 * to check for a NULL v_rdev.
1379 count_dev(cdev_t dev
)
1385 if (SLIST_FIRST(&dev
->si_hlist
)) {
1386 lwkt_gettoken(&ilock
, &spechash_token
);
1387 SLIST_FOREACH(vp
, &dev
->si_hlist
, v_cdevnext
) {
1388 if (vp
->v_sysref
.refcnt
> 0)
1389 count
+= vp
->v_sysref
.refcnt
;
1391 lwkt_reltoken(&ilock
);
1397 count_udev(int x
, int y
)
1401 if ((dev
= get_dev(x
, y
)) == NULL
)
1403 return(count_dev(dev
));
1407 vcount(struct vnode
*vp
)
1409 if (vp
->v_rdev
== NULL
)
1411 return(count_dev(vp
->v_rdev
));
1415 * Initialize VMIO for a vnode. This routine MUST be called before a
1416 * VFS can issue buffer cache ops on a vnode. It is typically called
1417 * when a vnode is initialized from its inode.
1420 vinitvmio(struct vnode
*vp
, off_t filesize
)
1426 if ((object
= vp
->v_object
) == NULL
) {
1427 object
= vnode_pager_alloc(vp
, filesize
, 0, 0);
1429 * Dereference the reference we just created. This assumes
1430 * that the object is associated with the vp.
1432 object
->ref_count
--;
1435 if (object
->flags
& OBJ_DEAD
) {
1437 vm_object_dead_sleep(object
, "vodead");
1438 vn_lock(vp
, LK_EXCLUSIVE
| LK_RETRY
);
1442 KASSERT(vp
->v_object
!= NULL
, ("vinitvmio: NULL object"));
1443 vp
->v_flag
|= VOBJBUF
;
1449 * Print out a description of a vnode.
1451 static char *typename
[] =
1452 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD"};
1455 vprint(char *label
, struct vnode
*vp
)
1460 kprintf("%s: %p: ", label
, (void *)vp
);
1462 kprintf("%p: ", (void *)vp
);
1463 kprintf("type %s, sysrefs %d, writecount %d, holdcnt %d,",
1464 typename
[vp
->v_type
],
1465 vp
->v_sysref
.refcnt
, vp
->v_writecount
, vp
->v_auxrefs
);
1467 if (vp
->v_flag
& VROOT
)
1468 strcat(buf
, "|VROOT");
1469 if (vp
->v_flag
& VPFSROOT
)
1470 strcat(buf
, "|VPFSROOT");
1471 if (vp
->v_flag
& VTEXT
)
1472 strcat(buf
, "|VTEXT");
1473 if (vp
->v_flag
& VSYSTEM
)
1474 strcat(buf
, "|VSYSTEM");
1475 if (vp
->v_flag
& VFREE
)
1476 strcat(buf
, "|VFREE");
1477 if (vp
->v_flag
& VOBJBUF
)
1478 strcat(buf
, "|VOBJBUF");
1480 kprintf(" flags (%s)", &buf
[1]);
1481 if (vp
->v_data
== NULL
) {
1490 #include <ddb/ddb.h>
1492 static int db_show_locked_vnodes(struct mount
*mp
, void *data
);
1495 * List all of the locked vnodes in the system.
1496 * Called when debugging the kernel.
1498 DB_SHOW_COMMAND(lockedvnodes
, lockedvnodes
)
1500 kprintf("Locked vnodes\n");
1501 mountlist_scan(db_show_locked_vnodes
, NULL
,
1502 MNTSCAN_FORWARD
|MNTSCAN_NOBUSY
);
1506 db_show_locked_vnodes(struct mount
*mp
, void *data __unused
)
1510 TAILQ_FOREACH(vp
, &mp
->mnt_nvnodelist
, v_nmntvnodes
) {
1511 if (vn_islocked(vp
))
1519 * Top level filesystem related information gathering.
1521 static int sysctl_ovfs_conf (SYSCTL_HANDLER_ARGS
);
1524 vfs_sysctl(SYSCTL_HANDLER_ARGS
)
1526 int *name
= (int *)arg1
- 1; /* XXX */
1527 u_int namelen
= arg2
+ 1; /* XXX */
1528 struct vfsconf
*vfsp
;
1531 #if 1 || defined(COMPAT_PRELITE2)
1532 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
1534 return (sysctl_ovfs_conf(oidp
, arg1
, arg2
, req
));
1538 /* all sysctl names at this level are at least name and field */
1540 return (ENOTDIR
); /* overloaded */
1541 if (name
[0] != VFS_GENERIC
) {
1542 vfsp
= vfsconf_find_by_typenum(name
[0]);
1544 return (EOPNOTSUPP
);
1545 return ((*vfsp
->vfc_vfsops
->vfs_sysctl
)(&name
[1], namelen
- 1,
1546 oldp
, oldlenp
, newp
, newlen
, p
));
1550 case VFS_MAXTYPENUM
:
1553 maxtypenum
= vfsconf_get_maxtypenum();
1554 return (SYSCTL_OUT(req
, &maxtypenum
, sizeof(maxtypenum
)));
1557 return (ENOTDIR
); /* overloaded */
1558 vfsp
= vfsconf_find_by_typenum(name
[2]);
1560 return (EOPNOTSUPP
);
1561 return (SYSCTL_OUT(req
, vfsp
, sizeof *vfsp
));
1563 return (EOPNOTSUPP
);
1566 SYSCTL_NODE(_vfs
, VFS_GENERIC
, generic
, CTLFLAG_RD
, vfs_sysctl
,
1567 "Generic filesystem");
1569 #if 1 || defined(COMPAT_PRELITE2)
1572 sysctl_ovfs_conf_iter(struct vfsconf
*vfsp
, void *data
)
1575 struct ovfsconf ovfs
;
1576 struct sysctl_req
*req
= (struct sysctl_req
*) data
;
1578 bzero(&ovfs
, sizeof(ovfs
));
1579 ovfs
.vfc_vfsops
= vfsp
->vfc_vfsops
; /* XXX used as flag */
1580 strcpy(ovfs
.vfc_name
, vfsp
->vfc_name
);
1581 ovfs
.vfc_index
= vfsp
->vfc_typenum
;
1582 ovfs
.vfc_refcount
= vfsp
->vfc_refcount
;
1583 ovfs
.vfc_flags
= vfsp
->vfc_flags
;
1584 error
= SYSCTL_OUT(req
, &ovfs
, sizeof ovfs
);
1586 return error
; /* abort iteration with error code */
1588 return 0; /* continue iterating with next element */
1592 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS
)
1594 return vfsconf_each(sysctl_ovfs_conf_iter
, (void*)req
);
1597 #endif /* 1 || COMPAT_PRELITE2 */
1600 * Check to see if a filesystem is mounted on a block device.
1603 vfs_mountedon(struct vnode
*vp
)
1607 if ((dev
= vp
->v_rdev
) == NULL
) {
1608 if (vp
->v_type
!= VBLK
)
1609 dev
= get_dev(vp
->v_uminor
, vp
->v_umajor
);
1611 if (dev
!= NULL
&& dev
->si_mountpoint
)
1617 * Unmount all filesystems. The list is traversed in reverse order
1618 * of mounting to avoid dependencies.
1621 static int vfs_umountall_callback(struct mount
*mp
, void *data
);
1624 vfs_unmountall(void)
1629 count
= mountlist_scan(vfs_umountall_callback
,
1630 NULL
, MNTSCAN_REVERSE
|MNTSCAN_NOBUSY
);
1636 vfs_umountall_callback(struct mount
*mp
, void *data
)
1640 error
= dounmount(mp
, MNT_FORCE
);
1642 mountlist_remove(mp
);
1643 kprintf("unmount of filesystem mounted from %s failed (",
1644 mp
->mnt_stat
.f_mntfromname
);
1648 kprintf("%d)\n", error
);
1654 * Build hash lists of net addresses and hang them off the mount point.
1655 * Called by ufs_mount() to set up the lists of export addresses.
1658 vfs_hang_addrlist(struct mount
*mp
, struct netexport
*nep
,
1659 const struct export_args
*argp
)
1662 struct radix_node_head
*rnh
;
1664 struct radix_node
*rn
;
1665 struct sockaddr
*saddr
, *smask
= 0;
1669 if (argp
->ex_addrlen
== 0) {
1670 if (mp
->mnt_flag
& MNT_DEFEXPORTED
)
1672 np
= &nep
->ne_defexported
;
1673 np
->netc_exflags
= argp
->ex_flags
;
1674 np
->netc_anon
= argp
->ex_anon
;
1675 np
->netc_anon
.cr_ref
= 1;
1676 mp
->mnt_flag
|= MNT_DEFEXPORTED
;
1680 if (argp
->ex_addrlen
< 0 || argp
->ex_addrlen
> MLEN
)
1682 if (argp
->ex_masklen
< 0 || argp
->ex_masklen
> MLEN
)
1685 i
= sizeof(struct netcred
) + argp
->ex_addrlen
+ argp
->ex_masklen
;
1686 np
= (struct netcred
*) kmalloc(i
, M_NETADDR
, M_WAITOK
| M_ZERO
);
1687 saddr
= (struct sockaddr
*) (np
+ 1);
1688 if ((error
= copyin(argp
->ex_addr
, (caddr_t
) saddr
, argp
->ex_addrlen
)))
1690 if (saddr
->sa_len
> argp
->ex_addrlen
)
1691 saddr
->sa_len
= argp
->ex_addrlen
;
1692 if (argp
->ex_masklen
) {
1693 smask
= (struct sockaddr
*)((caddr_t
)saddr
+ argp
->ex_addrlen
);
1694 error
= copyin(argp
->ex_mask
, (caddr_t
)smask
, argp
->ex_masklen
);
1697 if (smask
->sa_len
> argp
->ex_masklen
)
1698 smask
->sa_len
= argp
->ex_masklen
;
1700 i
= saddr
->sa_family
;
1701 if ((rnh
= nep
->ne_rtable
[i
]) == 0) {
1703 * Seems silly to initialize every AF when most are not used,
1704 * do so on demand here
1706 SLIST_FOREACH(dom
, &domains
, dom_next
)
1707 if (dom
->dom_family
== i
&& dom
->dom_rtattach
) {
1708 dom
->dom_rtattach((void **) &nep
->ne_rtable
[i
],
1712 if ((rnh
= nep
->ne_rtable
[i
]) == 0) {
1717 rn
= (*rnh
->rnh_addaddr
) ((char *) saddr
, (char *) smask
, rnh
,
1719 if (rn
== 0 || np
!= (struct netcred
*) rn
) { /* already exists */
1723 np
->netc_exflags
= argp
->ex_flags
;
1724 np
->netc_anon
= argp
->ex_anon
;
1725 np
->netc_anon
.cr_ref
= 1;
1728 kfree(np
, M_NETADDR
);
1734 vfs_free_netcred(struct radix_node
*rn
, void *w
)
1736 struct radix_node_head
*rnh
= (struct radix_node_head
*) w
;
1738 (*rnh
->rnh_deladdr
) (rn
->rn_key
, rn
->rn_mask
, rnh
);
1739 kfree((caddr_t
) rn
, M_NETADDR
);
1744 * Free the net address hash lists that are hanging off the mount points.
1747 vfs_free_addrlist(struct netexport
*nep
)
1750 struct radix_node_head
*rnh
;
1752 for (i
= 0; i
<= AF_MAX
; i
++)
1753 if ((rnh
= nep
->ne_rtable
[i
])) {
1754 (*rnh
->rnh_walktree
) (rnh
, vfs_free_netcred
,
1756 kfree((caddr_t
) rnh
, M_RTABLE
);
1757 nep
->ne_rtable
[i
] = 0;
1762 vfs_export(struct mount
*mp
, struct netexport
*nep
,
1763 const struct export_args
*argp
)
1767 if (argp
->ex_flags
& MNT_DELEXPORT
) {
1768 if (mp
->mnt_flag
& MNT_EXPUBLIC
) {
1769 vfs_setpublicfs(NULL
, NULL
, NULL
);
1770 mp
->mnt_flag
&= ~MNT_EXPUBLIC
;
1772 vfs_free_addrlist(nep
);
1773 mp
->mnt_flag
&= ~(MNT_EXPORTED
| MNT_DEFEXPORTED
);
1775 if (argp
->ex_flags
& MNT_EXPORTED
) {
1776 if (argp
->ex_flags
& MNT_EXPUBLIC
) {
1777 if ((error
= vfs_setpublicfs(mp
, nep
, argp
)) != 0)
1779 mp
->mnt_flag
|= MNT_EXPUBLIC
;
1781 if ((error
= vfs_hang_addrlist(mp
, nep
, argp
)))
1783 mp
->mnt_flag
|= MNT_EXPORTED
;
1790 * Set the publicly exported filesystem (WebNFS). Currently, only
1791 * one public filesystem is possible in the spec (RFC 2054 and 2055)
1794 vfs_setpublicfs(struct mount
*mp
, struct netexport
*nep
,
1795 const struct export_args
*argp
)
1802 * mp == NULL -> invalidate the current info, the FS is
1803 * no longer exported. May be called from either vfs_export
1804 * or unmount, so check if it hasn't already been done.
1807 if (nfs_pub
.np_valid
) {
1808 nfs_pub
.np_valid
= 0;
1809 if (nfs_pub
.np_index
!= NULL
) {
1810 FREE(nfs_pub
.np_index
, M_TEMP
);
1811 nfs_pub
.np_index
= NULL
;
1818 * Only one allowed at a time.
1820 if (nfs_pub
.np_valid
!= 0 && mp
!= nfs_pub
.np_mount
)
1824 * Get real filehandle for root of exported FS.
1826 bzero((caddr_t
)&nfs_pub
.np_handle
, sizeof(nfs_pub
.np_handle
));
1827 nfs_pub
.np_handle
.fh_fsid
= mp
->mnt_stat
.f_fsid
;
1829 if ((error
= VFS_ROOT(mp
, &rvp
)))
1832 if ((error
= VFS_VPTOFH(rvp
, &nfs_pub
.np_handle
.fh_fid
)))
1838 * If an indexfile was specified, pull it in.
1840 if (argp
->ex_indexfile
!= NULL
) {
1843 error
= vn_get_namelen(rvp
, &namelen
);
1846 MALLOC(nfs_pub
.np_index
, char *, namelen
, M_TEMP
,
1848 error
= copyinstr(argp
->ex_indexfile
, nfs_pub
.np_index
,
1852 * Check for illegal filenames.
1854 for (cp
= nfs_pub
.np_index
; *cp
; cp
++) {
1862 FREE(nfs_pub
.np_index
, M_TEMP
);
1867 nfs_pub
.np_mount
= mp
;
1868 nfs_pub
.np_valid
= 1;
1873 vfs_export_lookup(struct mount
*mp
, struct netexport
*nep
,
1874 struct sockaddr
*nam
)
1877 struct radix_node_head
*rnh
;
1878 struct sockaddr
*saddr
;
1881 if (mp
->mnt_flag
& MNT_EXPORTED
) {
1883 * Lookup in the export list first.
1887 rnh
= nep
->ne_rtable
[saddr
->sa_family
];
1889 np
= (struct netcred
*)
1890 (*rnh
->rnh_matchaddr
)((char *)saddr
,
1892 if (np
&& np
->netc_rnodes
->rn_flags
& RNF_ROOT
)
1897 * If no address match, use the default if it exists.
1899 if (np
== NULL
&& mp
->mnt_flag
& MNT_DEFEXPORTED
)
1900 np
= &nep
->ne_defexported
;
1906 * perform msync on all vnodes under a mount point. The mount point must
1907 * be locked. This code is also responsible for lazy-freeing unreferenced
1908 * vnodes whos VM objects no longer contain pages.
1910 * NOTE: MNT_WAIT still skips vnodes in the VXLOCK state.
1912 * NOTE: XXX VOP_PUTPAGES and friends requires that the vnode be locked,
1913 * but vnode_pager_putpages() doesn't lock the vnode. We have to do it
1914 * way up in this high level function.
1916 static int vfs_msync_scan1(struct mount
*mp
, struct vnode
*vp
, void *data
);
1917 static int vfs_msync_scan2(struct mount
*mp
, struct vnode
*vp
, void *data
);
1920 vfs_msync(struct mount
*mp
, int flags
)
1924 vmsc_flags
= VMSC_GETVP
;
1925 if (flags
!= MNT_WAIT
)
1926 vmsc_flags
|= VMSC_NOWAIT
;
1927 vmntvnodescan(mp
, vmsc_flags
, vfs_msync_scan1
, vfs_msync_scan2
,
1928 (void *)(intptr_t)flags
);
1932 * scan1 is a fast pre-check. There could be hundreds of thousands of
1933 * vnodes, we cannot afford to do anything heavy weight until we have a
1934 * fairly good indication that there is work to do.
1938 vfs_msync_scan1(struct mount
*mp
, struct vnode
*vp
, void *data
)
1940 int flags
= (int)(intptr_t)data
;
1942 if ((vp
->v_flag
& VRECLAIMED
) == 0) {
1943 if (vshouldmsync(vp
))
1944 return(0); /* call scan2 */
1945 if ((mp
->mnt_flag
& MNT_RDONLY
) == 0 &&
1946 (vp
->v_flag
& VOBJDIRTY
) &&
1947 (flags
== MNT_WAIT
|| vn_islocked(vp
) == 0)) {
1948 return(0); /* call scan2 */
1953 * do not call scan2, continue the loop
1959 * This callback is handed a locked vnode.
1963 vfs_msync_scan2(struct mount
*mp
, struct vnode
*vp
, void *data
)
1966 int flags
= (int)(intptr_t)data
;
1968 if (vp
->v_flag
& VRECLAIMED
)
1971 if ((mp
->mnt_flag
& MNT_RDONLY
) == 0 && (vp
->v_flag
& VOBJDIRTY
)) {
1972 if ((obj
= vp
->v_object
) != NULL
) {
1973 vm_object_page_clean(obj
, 0, 0,
1974 flags
== MNT_WAIT
? OBJPC_SYNC
: OBJPC_NOSYNC
);
1981 * Record a process's interest in events which might happen to
1982 * a vnode. Because poll uses the historic select-style interface
1983 * internally, this routine serves as both the ``check for any
1984 * pending events'' and the ``record my interest in future events''
1985 * functions. (These are done together, while the lock is held,
1986 * to avoid race conditions.)
1989 vn_pollrecord(struct vnode
*vp
, int events
)
1993 KKASSERT(curthread
->td_proc
!= NULL
);
1995 lwkt_gettoken(&ilock
, &vp
->v_pollinfo
.vpi_token
);
1996 if (vp
->v_pollinfo
.vpi_revents
& events
) {
1998 * This leaves events we are not interested
1999 * in available for the other process which
2000 * which presumably had requested them
2001 * (otherwise they would never have been
2004 events
&= vp
->v_pollinfo
.vpi_revents
;
2005 vp
->v_pollinfo
.vpi_revents
&= ~events
;
2007 lwkt_reltoken(&ilock
);
2010 vp
->v_pollinfo
.vpi_events
|= events
;
2011 selrecord(curthread
, &vp
->v_pollinfo
.vpi_selinfo
);
2012 lwkt_reltoken(&ilock
);
2017 * Note the occurrence of an event. If the VN_POLLEVENT macro is used,
2018 * it is possible for us to miss an event due to race conditions, but
2019 * that condition is expected to be rare, so for the moment it is the
2020 * preferred interface.
2023 vn_pollevent(struct vnode
*vp
, int events
)
2027 lwkt_gettoken(&ilock
, &vp
->v_pollinfo
.vpi_token
);
2028 if (vp
->v_pollinfo
.vpi_events
& events
) {
2030 * We clear vpi_events so that we don't
2031 * call selwakeup() twice if two events are
2032 * posted before the polling process(es) is
2033 * awakened. This also ensures that we take at
2034 * most one selwakeup() if the polling process
2035 * is no longer interested. However, it does
2036 * mean that only one event can be noticed at
2037 * a time. (Perhaps we should only clear those
2038 * event bits which we note?) XXX
2040 vp
->v_pollinfo
.vpi_events
= 0; /* &= ~events ??? */
2041 vp
->v_pollinfo
.vpi_revents
|= events
;
2042 selwakeup(&vp
->v_pollinfo
.vpi_selinfo
);
2044 lwkt_reltoken(&ilock
);
2048 * Wake up anyone polling on vp because it is being revoked.
2049 * This depends on dead_poll() returning POLLHUP for correct
2053 vn_pollgone(struct vnode
*vp
)
2057 lwkt_gettoken(&ilock
, &vp
->v_pollinfo
.vpi_token
);
2058 if (vp
->v_pollinfo
.vpi_events
) {
2059 vp
->v_pollinfo
.vpi_events
= 0;
2060 selwakeup(&vp
->v_pollinfo
.vpi_selinfo
);
2062 lwkt_reltoken(&ilock
);
2066 * extract the cdev_t from a VBLK or VCHR. The vnode must have been opened
2067 * (or v_rdev might be NULL).
2070 vn_todev(struct vnode
*vp
)
2072 if (vp
->v_type
!= VBLK
&& vp
->v_type
!= VCHR
)
2074 KKASSERT(vp
->v_rdev
!= NULL
);
2075 return (vp
->v_rdev
);
2079 * Check if vnode represents a disk device. The vnode does not need to be
2083 vn_isdisk(struct vnode
*vp
, int *errp
)
2087 if (vp
->v_type
!= VCHR
) {
2093 if ((dev
= vp
->v_rdev
) == NULL
)
2094 dev
= get_dev(vp
->v_umajor
, vp
->v_uminor
);
2101 if (dev_is_good(dev
) == 0) {
2106 if ((dev_dflags(dev
) & D_DISK
) == 0) {
2117 vn_get_namelen(struct vnode
*vp
, int *namelen
)
2120 register_t retval
[2];
2122 error
= VOP_PATHCONF(vp
, _PC_NAME_MAX
, retval
);
2125 *namelen
= (int)retval
[0];
2130 vop_write_dirent(int *error
, struct uio
*uio
, ino_t d_ino
, uint8_t d_type
,
2131 uint16_t d_namlen
, const char *d_name
)
2136 len
= _DIRENT_RECLEN(d_namlen
);
2137 if (len
> uio
->uio_resid
)
2140 dp
= kmalloc(len
, M_TEMP
, M_WAITOK
| M_ZERO
);
2143 dp
->d_namlen
= d_namlen
;
2144 dp
->d_type
= d_type
;
2145 bcopy(d_name
, dp
->d_name
, d_namlen
);
2147 *error
= uiomove((caddr_t
)dp
, len
, uio
);
2155 vn_mark_atime(struct vnode
*vp
, struct thread
*td
)
2157 struct proc
*p
= td
->td_proc
;
2158 struct ucred
*cred
= p
? p
->p_ucred
: proc0
.p_ucred
;
2160 if ((vp
->v_mount
->mnt_flag
& (MNT_NOATIME
| MNT_RDONLY
)) == 0) {
2161 VOP_MARKATIME(vp
, cred
);