HAMMER 60I/Many: Mirroring
[dragonfly.git] / sys / kern / vfs_subr.c
blob30249bf52e2be507dedca7812cf9693eb72a67a4
1 /*
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
12 * are met:
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
36 * SUCH DAMAGE.
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.114 2008/05/18 05:54:25 dillon Exp $
44 * External virtual filesystem routines
46 #include "opt_ddb.h"
48 #include <sys/param.h>
49 #include <sys/systm.h>
50 #include <sys/buf.h>
51 #include <sys/conf.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>
59 #include <sys/mbuf.h>
60 #include <sys/mount.h>
61 #include <sys/proc.h>
62 #include <sys/reboot.h>
63 #include <sys/socket.h>
64 #include <sys/stat.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>
73 #include <vm/vm.h>
74 #include <vm/vm_object.h>
75 #include <vm/vm_extern.h>
76 #include <vm/vm_kern.h>
77 #include <vm/pmap.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>
84 #include <sys/buf2.h>
85 #include <sys/thread2.h>
86 #include <sys/sysref2.h>
88 static MALLOC_DEFINE(M_NETADDR, "Export Host", "Export host address structure");
90 int numvnodes;
91 SYSCTL_INT(_debug, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0, "");
92 int vfs_fastdev = 1;
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,
99 int vttoif_tab[9] = {
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 */
124 int desiredvnodes;
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);
142 static int
143 rb_buf_compare(struct buf *b1, struct buf *b2)
145 if (b1->b_loffset < b2->b_loffset)
146 return(-1);
147 if (b1->b_loffset > b2->b_loffset)
148 return(1);
149 return(0);
153 * Returns non-zero if the vnode is a candidate for lazy msyncing.
155 static __inline int
156 vshouldmsync(struct vnode *vp)
158 if (vp->v_auxrefs != 0 || vp->v_sysref.refcnt > 0)
159 return (0); /* other holders */
160 if (vp->v_object &&
161 (vp->v_object->ref_count || vp->v_object->resident_page_count)) {
162 return (0);
164 return (1);
168 * Initialize the vnode management data structures.
170 * Called from vfsinit()
172 void
173 vfs_subr_init(void)
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; */
182 desiredvnodes =
183 min(maxproc + vmstats.v_page_count / 4,
184 2 * KvaSize /
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 &timestamp_precision, 0, "");
205 * Get a current timestamp.
207 void
208 vfs_timestamp(struct timespec *tsp)
210 struct timeval tv;
212 switch (timestamp_precision) {
213 case TSP_SEC:
214 tsp->tv_sec = time_second;
215 tsp->tv_nsec = 0;
216 break;
217 case TSP_HZ:
218 getnanotime(tsp);
219 break;
220 case TSP_USEC:
221 microtime(&tv);
222 TIMEVAL_TO_TIMESPEC(&tv, tsp);
223 break;
224 case TSP_NSEC:
225 default:
226 nanotime(tsp);
227 break;
232 * Set vnode attributes to VNOVAL
234 void
235 vattr_null(struct vattr *vap)
237 vap->va_type = VNON;
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;
257 vap->va_vaflags = 0;
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.
265 * vp must be locked.
267 static int vinvalbuf_bp(struct buf *bp, void *data);
269 struct vinvalbuf_bp_info {
270 struct vnode *vp;
271 int slptimeo;
272 int lkflags;
273 int flags;
276 void
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;
286 int error;
287 vm_object_t object;
290 * If we are being asked to save, call fsync to ensure that the inode
291 * is updated.
293 if (flags & V_SAVE) {
294 crit_enter();
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);
299 if (error) {
300 crit_exit();
301 return (error);
304 if (!RB_EMPTY(&vp->v_rbdirty_tree)) {
305 crit_exit();
306 if ((error = VOP_FSYNC(vp, MNT_WAIT)) != 0)
307 return (error);
308 crit_enter();
309 if (vp->v_track_write.bk_active > 0 ||
310 !RB_EMPTY(&vp->v_rbdirty_tree))
311 panic("vinvalbuf: dirty bufs");
313 crit_exit();
315 crit_enter();
316 info.slptimeo = slptimeo;
317 info.lkflags = LK_EXCLUSIVE | LK_SLEEPFAIL;
318 if (slpflag & PCATCH)
319 info.lkflags |= LK_PCATCH;
320 info.flags = flags;
321 info.vp = vp;
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);
330 if (error == 0) {
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.
341 do {
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);
352 crit_exit();
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");
366 return (0);
369 static int
370 vinvalbuf_bp(struct buf *bp, void *data)
372 struct vinvalbuf_bp_info *info = data;
373 int error;
375 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
376 error = BUF_TIMELOCK(bp, info->lkflags,
377 "vinvalbuf", info->slptimeo);
378 if (error == 0) {
379 BUF_UNLOCK(bp);
380 error = ENOLCK;
382 if (error == ENOLCK)
383 return(0);
384 return (-error);
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
395 * brelse() do not.
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) {
404 vfs_bio_awrite(bp);
405 } else {
406 bremfree(bp);
407 bp->b_flags |= B_ASYNC;
408 bwrite(bp);
410 } else {
411 bremfree(bp);
412 bwrite(bp);
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).
420 bremfree(bp);
421 bp->b_flags |= (B_INVAL | B_RELBUF);
422 bp->b_flags &= ~B_ASYNC;
423 brelse(bp);
424 } else {
425 bremfree(bp);
426 bp->b_flags |= (B_INVAL | B_NOCACHE | B_RELBUF);
427 bp->b_flags &= ~B_ASYNC;
428 brelse(bp);
430 return(0);
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
436 * sync activity.
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)
448 off_t truncloffset;
449 int count;
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);
459 else
460 truncloffset = length;
462 crit_enter();
463 do {
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);
470 } while(count);
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
476 * all of it.
478 if (length > 0) {
479 do {
480 count = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
481 vtruncbuf_bp_metasync_cmp,
482 vtruncbuf_bp_metasync, vp);
483 } while (count);
487 * Clean out any left over VM backing store.
489 crit_exit();
491 vnode_pager_setsize(vp, length);
493 crit_enter();
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);
506 if (length == 0) {
507 kprintf("Warning: vtruncbuf(): Had to wait for "
508 "%d buffer I/Os to finish in %s\n",
509 count, filename);
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.
518 do {
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);
525 if (count) {
526 kprintf("Warning: vtruncbuf(): Had to re-clean %d "
527 "left over buffers in %s\n", count, filename);
529 } while(count);
531 crit_exit();
533 return (0);
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.
540 static
542 vtruncbuf_bp_trunc_cmp(struct buf *bp, void *data)
544 if (bp->b_loffset >= *(off_t *)data)
545 return(0);
546 return(-1);
549 static
550 int
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
556 * can be acted upon.
558 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
559 if (BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL) == 0)
560 BUF_UNLOCK(bp);
561 } else {
562 bremfree(bp);
563 bp->b_flags |= (B_INVAL | B_RELBUF | B_NOCACHE);
564 bp->b_flags &= ~B_ASYNC;
565 brelse(bp);
567 return(1);
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.
575 static int
576 vtruncbuf_bp_metasync_cmp(struct buf *bp, void *data)
578 if (bp->b_loffset < 0)
579 return(0);
580 return(1);
583 static int
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)
596 BUF_UNLOCK(bp);
597 } else {
598 bremfree(bp);
599 if (bp->b_vp == vp) {
600 bp->b_flags |= B_ASYNC;
601 } else {
602 bp->b_flags &= ~B_ASYNC;
604 bwrite(bp);
606 return(1);
607 } else {
608 return(0);
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
619 * to dependancies.
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);
631 struct vfsync_info {
632 struct vnode *vp;
633 int synchronous;
634 int syncdeps;
635 int lazycount;
636 int lazylimit;
637 int skippedbufs;
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;
647 int error;
649 bzero(&info, sizeof(info));
650 info.vp = vp;
651 if ((info.checkdef = checkdef) == NULL)
652 info.syncdeps = 1;
654 crit_enter_id("vfsync");
656 switch(waitfor) {
657 case MNT_LAZY:
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;
664 info.syncdeps = 1;
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);
669 if (error == 0)
670 vp->v_lazyw = 0;
671 else if (!RB_EMPTY(&vp->v_rbdirty_tree))
672 vn_syncer_add_to_worklist(vp, 1);
673 error = 0;
674 break;
675 case MNT_NOWAIT:
677 * Asynchronous. Do a data-only pass and a meta-only pass.
679 info.syncdeps = 1;
680 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_data_only_cmp,
681 vfsync_bp, &info);
682 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_meta_only_cmp,
683 vfsync_bp, &info);
684 error = 0;
685 break;
686 default:
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,
693 vfsync_bp, &info);
694 error = vfsync_wait_output(vp, waitoutput);
695 if (error == 0) {
696 info.skippedbufs = 0;
697 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
698 vfsync_bp, &info);
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)) {
705 if (--passes == 0) {
706 info.synchronous = 1;
707 info.syncdeps = 1;
709 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
710 vfsync_bp, &info);
711 if (error < 0)
712 error = -error;
713 info.syncdeps = 1;
714 if (error == 0)
715 error = vfsync_wait_output(vp, waitoutput);
717 break;
719 crit_exit_id("vfsync");
720 return(error);
723 static int
724 vfsync_wait_output(struct vnode *vp, int (*waitoutput)(struct vnode *, struct thread *))
726 int error = 0;
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);
732 if (waitoutput)
733 error = waitoutput(vp, curthread);
734 return(error);
737 static int
738 vfsync_data_only_cmp(struct buf *bp, void *data)
740 if (bp->b_loffset < 0)
741 return(-1);
742 return(0);
745 static int
746 vfsync_meta_only_cmp(struct buf *bp, void *data)
748 if (bp->b_loffset < 0)
749 return(0);
750 return(1);
753 static int
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)
758 return(-1);
759 return(0);
762 static int
763 vfsync_bp(struct buf *bp, void *data)
765 struct vfsync_info *info = data;
766 struct vnode *vp = info->vp;
767 int error;
770 * if syncdeps is not set we do not try to write buffers which have
771 * dependancies.
773 if (!info->synchronous && info->syncdeps == 0 && info->checkdef(bp))
774 return(0);
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);
781 ++info->skippedbufs;
782 return(0);
784 if ((bp->b_flags & B_DELWRI) == 0)
785 panic("vfsync_bp: buffer not dirty");
786 if (vp != bp->b_vp)
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) {
796 BUF_UNLOCK(bp);
797 return(0);
801 * Ask bioops if it is ok to sync
803 if (LIST_FIRST(&bp->b_dep) != NULL && buf_checkwrite(bp)) {
804 bremfree(bp);
805 brelse(bp);
806 return(0);
809 if (info->synchronous) {
811 * Synchronous flushing. An error may be returned.
813 bremfree(bp);
814 crit_exit_id("vfsync");
815 error = bwrite(bp);
816 crit_enter_id("vfsync");
817 } else {
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);
826 } else {
827 info->lazycount += bp->b_bufsize;
828 bremfree(bp);
829 crit_exit_id("vfsync");
830 bawrite(bp);
831 crit_enter_id("vfsync");
833 if (info->lazylimit && info->lazycount >= info->lazylimit)
834 error = 1;
835 else
836 error = 0;
838 return(-error);
842 * Associate a buffer with a vnode.
844 void
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);
850 vhold(vp);
852 * Insert onto list for new vnode.
854 crit_enter();
855 bp->b_vp = vp;
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);
863 crit_exit();
867 * Disassociate a buffer from a vnode.
869 void
870 brelvp(struct buf *bp)
872 struct vnode *vp;
874 KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
877 * Delete from old vnode list, if on one.
879 vp = bp->b_vp;
880 crit_enter();
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);
884 else
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);
896 crit_exit();
897 bp->b_vp = NULL;
898 vdrop(vp);
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.
905 void
906 reassignbuf(struct buf *bp)
908 struct vnode *vp = bp->b_vp;
909 int delay;
911 KKASSERT(vp != NULL);
912 ++reassignbufcalls;
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");
921 crit_enter();
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",
933 vp, bp);
935 bp->b_flags |= B_VNDIRTY;
937 if ((vp->v_flag & VONWORKLST) == 0) {
938 switch (vp->v_type) {
939 case VDIR:
940 delay = dirdelay;
941 break;
942 case VCHR:
943 case VBLK:
944 if (vp->v_rdev &&
945 vp->v_rdev->si_mountpoint != NULL) {
946 delay = metadelay;
947 break;
949 /* fall through */
950 default:
951 delay = filedelay;
953 vn_syncer_add_to_worklist(vp, delay);
955 } else {
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",
967 vp, bp);
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);
977 crit_exit();
981 * Create a vnode for a block device.
982 * Used for mounting the root file system.
985 bdevvp(cdev_t dev, struct vnode **vpp)
987 struct vnode *vp;
988 struct vnode *nvp;
989 int error;
991 if (dev == NULL) {
992 *vpp = NULLVP;
993 return (ENXIO);
995 error = getspecialvnode(VT_NON, NULL, &spec_vnode_vops_p, &nvp, 0, 0);
996 if (error) {
997 *vpp = NULLVP;
998 return (error);
1000 vp = nvp;
1001 vp->v_type = VCHR;
1002 vp->v_umajor = dev->si_umajor;
1003 vp->v_uminor = dev->si_uminor;
1004 vx_unlock(vp);
1005 *vpp = vp;
1006 return (0);
1010 v_associate_rdev(struct vnode *vp, cdev_t dev)
1012 lwkt_tokref ilock;
1014 if (dev == NULL)
1015 return(ENXIO);
1016 if (dev_is_good(dev) == 0)
1017 return(ENXIO);
1018 KKASSERT(vp->v_rdev == NULL);
1019 if (dev_ref_debug)
1020 kprintf("Z1");
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);
1025 return(0);
1028 void
1029 v_release_rdev(struct vnode *vp)
1031 lwkt_tokref ilock;
1032 cdev_t dev;
1034 if ((dev = vp->v_rdev) != NULL) {
1035 lwkt_gettoken(&ilock, &spechash_token);
1036 SLIST_REMOVE(&dev->si_hlist, vp, vnode, v_cdevnext);
1037 vp->v_rdev = NULL;
1038 release_dev(dev);
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.
1049 void
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");
1054 nvp->v_umajor = x;
1055 nvp->v_uminor = y;
1059 * Disassociate a vnode from its underlying filesystem.
1061 * The vnode must be VX locked and referenced. In all normal situations
1062 * there are no active references. If vclean_vxlocked() is called while
1063 * there are active references, the vnode is being ripped out and we have
1064 * to call VOP_CLOSE() as appropriate before we can reclaim it.
1066 void
1067 vclean_vxlocked(struct vnode *vp, int flags)
1069 int active;
1070 int n;
1071 vm_object_t object;
1074 * If the vnode has already been reclaimed we have nothing to do.
1076 if (vp->v_flag & VRECLAIMED)
1077 return;
1078 vp->v_flag |= VRECLAIMED;
1081 * Scrap the vfs cache
1083 while (cache_inval_vp(vp, 0) != 0) {
1084 kprintf("Warning: vnode %p clean/cache_resolution race detected\n", vp);
1085 tsleep(vp, 0, "vclninv", 2);
1089 * Check to see if the vnode is in use. If so we have to reference it
1090 * before we clean it out so that its count cannot fall to zero and
1091 * generate a race against ourselves to recycle it.
1093 active = sysref_isactive(&vp->v_sysref);
1096 * Clean out any buffers associated with the vnode and destroy its
1097 * object, if it has one.
1099 vinvalbuf(vp, V_SAVE, 0, 0);
1102 * If purging an active vnode (typically during a forced unmount
1103 * or reboot), it must be closed and deactivated before being
1104 * reclaimed. This isn't really all that safe, but what can
1105 * we do? XXX.
1107 * Note that neither of these routines unlocks the vnode.
1109 if (active && (flags & DOCLOSE)) {
1110 while ((n = vp->v_opencount) != 0) {
1111 if (vp->v_writecount)
1112 VOP_CLOSE(vp, FWRITE|FNONBLOCK);
1113 else
1114 VOP_CLOSE(vp, FNONBLOCK);
1115 if (vp->v_opencount == n) {
1116 kprintf("Warning: unable to force-close"
1117 " vnode %p\n", vp);
1118 break;
1124 * If the vnode has not been deactivated, deactivated it. Deactivation
1125 * can create new buffers and VM pages so we have to call vinvalbuf()
1126 * again to make sure they all get flushed.
1128 * This can occur if a file with a link count of 0 needs to be
1129 * truncated.
1131 if ((vp->v_flag & VINACTIVE) == 0) {
1132 vp->v_flag |= VINACTIVE;
1133 VOP_INACTIVE(vp);
1134 vinvalbuf(vp, V_SAVE, 0, 0);
1138 * If the vnode has an object, destroy it.
1140 if ((object = vp->v_object) != NULL) {
1141 if (object->ref_count == 0) {
1142 if ((object->flags & OBJ_DEAD) == 0)
1143 vm_object_terminate(object);
1144 } else {
1145 vm_pager_deallocate(object);
1147 vp->v_flag &= ~VOBJBUF;
1149 KKASSERT((vp->v_flag & VOBJBUF) == 0);
1152 * Reclaim the vnode.
1154 if (VOP_RECLAIM(vp))
1155 panic("vclean: cannot reclaim");
1158 * Done with purge, notify sleepers of the grim news.
1160 vp->v_ops = &dead_vnode_vops_p;
1161 vn_pollgone(vp);
1162 vp->v_tag = VT_NON;
1165 * If we are destroying an active vnode, reactivate it now that
1166 * we have reassociated it with deadfs. This prevents the system
1167 * from crashing on the vnode due to it being unexpectedly marked
1168 * as inactive or reclaimed.
1170 if (active && (flags & DOCLOSE)) {
1171 vp->v_flag &= ~(VINACTIVE|VRECLAIMED);
1176 * Eliminate all activity associated with the requested vnode
1177 * and with all vnodes aliased to the requested vnode.
1179 * The vnode must be referenced and vx_lock()'d
1181 * revoke { struct vnode *a_vp, int a_flags }
1184 vop_stdrevoke(struct vop_revoke_args *ap)
1186 struct vnode *vp, *vq;
1187 lwkt_tokref ilock;
1188 cdev_t dev;
1190 KASSERT((ap->a_flags & REVOKEALL) != 0, ("vop_revoke"));
1192 vp = ap->a_vp;
1195 * If the vnode is already dead don't try to revoke it
1197 if (vp->v_flag & VRECLAIMED)
1198 return (0);
1201 * If the vnode has a device association, scrap all vnodes associated
1202 * with the device. Don't let the device disappear on us while we
1203 * are scrapping the vnodes.
1205 * The passed vp will probably show up in the list, do not VX lock
1206 * it twice!
1208 if (vp->v_type != VCHR)
1209 return(0);
1210 if ((dev = vp->v_rdev) == NULL) {
1211 if ((dev = get_dev(vp->v_umajor, vp->v_uminor)) == NULL)
1212 return(0);
1214 reference_dev(dev);
1215 lwkt_gettoken(&ilock, &spechash_token);
1216 while ((vq = SLIST_FIRST(&dev->si_hlist)) != NULL) {
1217 if (vp != vq)
1218 vx_get(vq);
1219 if (vq == SLIST_FIRST(&dev->si_hlist))
1220 vgone_vxlocked(vq);
1221 if (vp != vq)
1222 vx_put(vq);
1224 lwkt_reltoken(&ilock);
1225 release_dev(dev);
1226 return (0);
1230 * This is called when the object underlying a vnode is being destroyed,
1231 * such as in a remove(). Try to recycle the vnode immediately if the
1232 * only active reference is our reference.
1234 * Directory vnodes in the namecache with children cannot be immediately
1235 * recycled because numerous VOP_N*() ops require them to be stable.
1238 vrecycle(struct vnode *vp)
1240 if (vp->v_sysref.refcnt <= 1) {
1241 if (cache_inval_vp_nonblock(vp))
1242 return(0);
1243 vgone_vxlocked(vp);
1244 return (1);
1246 return (0);
1250 * Return the maximum I/O size allowed for strategy calls on VP.
1252 * If vp is VCHR or VBLK we dive the device, otherwise we use
1253 * the vp's mount info.
1256 vmaxiosize(struct vnode *vp)
1258 if (vp->v_type == VBLK || vp->v_type == VCHR) {
1259 return(vp->v_rdev->si_iosize_max);
1260 } else {
1261 return(vp->v_mount->mnt_iosize_max);
1266 * Eliminate all activity associated with a vnode in preparation for reuse.
1268 * The vnode must be VX locked and refd and will remain VX locked and refd
1269 * on return. This routine may be called with the vnode in any state, as
1270 * long as it is VX locked. The vnode will be cleaned out and marked
1271 * VRECLAIMED but will not actually be reused until all existing refs and
1272 * holds go away.
1274 * NOTE: This routine may be called on a vnode which has not yet been
1275 * already been deactivated (VOP_INACTIVE), or on a vnode which has
1276 * already been reclaimed.
1278 * This routine is not responsible for placing us back on the freelist.
1279 * Instead, it happens automatically when the caller releases the VX lock
1280 * (assuming there aren't any other references).
1283 void
1284 vgone_vxlocked(struct vnode *vp)
1287 * assert that the VX lock is held. This is an absolute requirement
1288 * now for vgone_vxlocked() to be called.
1290 KKASSERT(vp->v_lock.lk_exclusivecount == 1);
1293 * Clean out the filesystem specific data and set the VRECLAIMED
1294 * bit. Also deactivate the vnode if necessary.
1296 vclean_vxlocked(vp, DOCLOSE);
1299 * Delete from old mount point vnode list, if on one.
1301 if (vp->v_mount != NULL)
1302 insmntque(vp, NULL);
1305 * If special device, remove it from special device alias list
1306 * if it is on one. This should normally only occur if a vnode is
1307 * being revoked as the device should otherwise have been released
1308 * naturally.
1310 if ((vp->v_type == VBLK || vp->v_type == VCHR) && vp->v_rdev != NULL) {
1311 v_release_rdev(vp);
1315 * Set us to VBAD
1317 vp->v_type = VBAD;
1321 * Lookup a vnode by device number.
1324 vfinddev(cdev_t dev, enum vtype type, struct vnode **vpp)
1326 lwkt_tokref ilock;
1327 struct vnode *vp;
1329 lwkt_gettoken(&ilock, &spechash_token);
1330 SLIST_FOREACH(vp, &dev->si_hlist, v_cdevnext) {
1331 if (type == vp->v_type) {
1332 *vpp = vp;
1333 lwkt_reltoken(&ilock);
1334 return (1);
1337 lwkt_reltoken(&ilock);
1338 return (0);
1342 * Calculate the total number of references to a special device. This
1343 * routine may only be called for VBLK and VCHR vnodes since v_rdev is
1344 * an overloaded field. Since udev2dev can now return NULL, we have
1345 * to check for a NULL v_rdev.
1348 count_dev(cdev_t dev)
1350 lwkt_tokref ilock;
1351 struct vnode *vp;
1352 int count = 0;
1354 if (SLIST_FIRST(&dev->si_hlist)) {
1355 lwkt_gettoken(&ilock, &spechash_token);
1356 SLIST_FOREACH(vp, &dev->si_hlist, v_cdevnext) {
1357 if (vp->v_sysref.refcnt > 0)
1358 count += vp->v_sysref.refcnt;
1360 lwkt_reltoken(&ilock);
1362 return(count);
1366 count_udev(int x, int y)
1368 cdev_t dev;
1370 if ((dev = get_dev(x, y)) == NULL)
1371 return(0);
1372 return(count_dev(dev));
1376 vcount(struct vnode *vp)
1378 if (vp->v_rdev == NULL)
1379 return(0);
1380 return(count_dev(vp->v_rdev));
1384 * Initialize VMIO for a vnode. This routine MUST be called before a
1385 * VFS can issue buffer cache ops on a vnode. It is typically called
1386 * when a vnode is initialized from its inode.
1389 vinitvmio(struct vnode *vp, off_t filesize)
1391 vm_object_t object;
1392 int error = 0;
1394 retry:
1395 if ((object = vp->v_object) == NULL) {
1396 object = vnode_pager_alloc(vp, filesize, 0, 0);
1398 * Dereference the reference we just created. This assumes
1399 * that the object is associated with the vp.
1401 object->ref_count--;
1402 vrele(vp);
1403 } else {
1404 if (object->flags & OBJ_DEAD) {
1405 vn_unlock(vp);
1406 vm_object_dead_sleep(object, "vodead");
1407 vn_lock(vp, LK_EXCLUSIVE | LK_RETRY);
1408 goto retry;
1411 KASSERT(vp->v_object != NULL, ("vinitvmio: NULL object"));
1412 vp->v_flag |= VOBJBUF;
1413 return (error);
1418 * Print out a description of a vnode.
1420 static char *typename[] =
1421 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD"};
1423 void
1424 vprint(char *label, struct vnode *vp)
1426 char buf[96];
1428 if (label != NULL)
1429 kprintf("%s: %p: ", label, (void *)vp);
1430 else
1431 kprintf("%p: ", (void *)vp);
1432 kprintf("type %s, sysrefs %d, writecount %d, holdcnt %d,",
1433 typename[vp->v_type],
1434 vp->v_sysref.refcnt, vp->v_writecount, vp->v_auxrefs);
1435 buf[0] = '\0';
1436 if (vp->v_flag & VROOT)
1437 strcat(buf, "|VROOT");
1438 if (vp->v_flag & VTEXT)
1439 strcat(buf, "|VTEXT");
1440 if (vp->v_flag & VSYSTEM)
1441 strcat(buf, "|VSYSTEM");
1442 if (vp->v_flag & VFREE)
1443 strcat(buf, "|VFREE");
1444 if (vp->v_flag & VOBJBUF)
1445 strcat(buf, "|VOBJBUF");
1446 if (buf[0] != '\0')
1447 kprintf(" flags (%s)", &buf[1]);
1448 if (vp->v_data == NULL) {
1449 kprintf("\n");
1450 } else {
1451 kprintf("\n\t");
1452 VOP_PRINT(vp);
1456 #ifdef DDB
1457 #include <ddb/ddb.h>
1459 static int db_show_locked_vnodes(struct mount *mp, void *data);
1462 * List all of the locked vnodes in the system.
1463 * Called when debugging the kernel.
1465 DB_SHOW_COMMAND(lockedvnodes, lockedvnodes)
1467 kprintf("Locked vnodes\n");
1468 mountlist_scan(db_show_locked_vnodes, NULL,
1469 MNTSCAN_FORWARD|MNTSCAN_NOBUSY);
1472 static int
1473 db_show_locked_vnodes(struct mount *mp, void *data __unused)
1475 struct vnode *vp;
1477 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
1478 if (vn_islocked(vp))
1479 vprint((char *)0, vp);
1481 return(0);
1483 #endif
1486 * Top level filesystem related information gathering.
1488 static int sysctl_ovfs_conf (SYSCTL_HANDLER_ARGS);
1490 static int
1491 vfs_sysctl(SYSCTL_HANDLER_ARGS)
1493 int *name = (int *)arg1 - 1; /* XXX */
1494 u_int namelen = arg2 + 1; /* XXX */
1495 struct vfsconf *vfsp;
1497 #if 1 || defined(COMPAT_PRELITE2)
1498 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
1499 if (namelen == 1)
1500 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
1501 #endif
1503 #ifdef notyet
1504 /* all sysctl names at this level are at least name and field */
1505 if (namelen < 2)
1506 return (ENOTDIR); /* overloaded */
1507 if (name[0] != VFS_GENERIC) {
1508 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
1509 if (vfsp->vfc_typenum == name[0])
1510 break;
1511 if (vfsp == NULL)
1512 return (EOPNOTSUPP);
1513 return ((*vfsp->vfc_vfsops->vfs_sysctl)(&name[1], namelen - 1,
1514 oldp, oldlenp, newp, newlen, p));
1516 #endif
1517 switch (name[1]) {
1518 case VFS_MAXTYPENUM:
1519 if (namelen != 2)
1520 return (ENOTDIR);
1521 return (SYSCTL_OUT(req, &maxvfsconf, sizeof(int)));
1522 case VFS_CONF:
1523 if (namelen != 3)
1524 return (ENOTDIR); /* overloaded */
1525 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next)
1526 if (vfsp->vfc_typenum == name[2])
1527 break;
1528 if (vfsp == NULL)
1529 return (EOPNOTSUPP);
1530 return (SYSCTL_OUT(req, vfsp, sizeof *vfsp));
1532 return (EOPNOTSUPP);
1535 SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD, vfs_sysctl,
1536 "Generic filesystem");
1538 #if 1 || defined(COMPAT_PRELITE2)
1540 static int
1541 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
1543 int error;
1544 struct vfsconf *vfsp;
1545 struct ovfsconf ovfs;
1547 for (vfsp = vfsconf; vfsp; vfsp = vfsp->vfc_next) {
1548 bzero(&ovfs, sizeof(ovfs));
1549 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
1550 strcpy(ovfs.vfc_name, vfsp->vfc_name);
1551 ovfs.vfc_index = vfsp->vfc_typenum;
1552 ovfs.vfc_refcount = vfsp->vfc_refcount;
1553 ovfs.vfc_flags = vfsp->vfc_flags;
1554 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
1555 if (error)
1556 return error;
1558 return 0;
1561 #endif /* 1 || COMPAT_PRELITE2 */
1564 * Check to see if a filesystem is mounted on a block device.
1567 vfs_mountedon(struct vnode *vp)
1569 cdev_t dev;
1571 if ((dev = vp->v_rdev) == NULL) {
1572 if (vp->v_type != VBLK)
1573 dev = get_dev(vp->v_uminor, vp->v_umajor);
1575 if (dev != NULL && dev->si_mountpoint)
1576 return (EBUSY);
1577 return (0);
1581 * Unmount all filesystems. The list is traversed in reverse order
1582 * of mounting to avoid dependencies.
1585 static int vfs_umountall_callback(struct mount *mp, void *data);
1587 void
1588 vfs_unmountall(void)
1590 int count;
1592 do {
1593 count = mountlist_scan(vfs_umountall_callback,
1594 NULL, MNTSCAN_REVERSE|MNTSCAN_NOBUSY);
1595 } while (count);
1598 static
1600 vfs_umountall_callback(struct mount *mp, void *data)
1602 int error;
1604 error = dounmount(mp, MNT_FORCE);
1605 if (error) {
1606 mountlist_remove(mp);
1607 kprintf("unmount of filesystem mounted from %s failed (",
1608 mp->mnt_stat.f_mntfromname);
1609 if (error == EBUSY)
1610 kprintf("BUSY)\n");
1611 else
1612 kprintf("%d)\n", error);
1614 return(1);
1618 * Build hash lists of net addresses and hang them off the mount point.
1619 * Called by ufs_mount() to set up the lists of export addresses.
1621 static int
1622 vfs_hang_addrlist(struct mount *mp, struct netexport *nep,
1623 const struct export_args *argp)
1625 struct netcred *np;
1626 struct radix_node_head *rnh;
1627 int i;
1628 struct radix_node *rn;
1629 struct sockaddr *saddr, *smask = 0;
1630 struct domain *dom;
1631 int error;
1633 if (argp->ex_addrlen == 0) {
1634 if (mp->mnt_flag & MNT_DEFEXPORTED)
1635 return (EPERM);
1636 np = &nep->ne_defexported;
1637 np->netc_exflags = argp->ex_flags;
1638 np->netc_anon = argp->ex_anon;
1639 np->netc_anon.cr_ref = 1;
1640 mp->mnt_flag |= MNT_DEFEXPORTED;
1641 return (0);
1644 if (argp->ex_addrlen < 0 || argp->ex_addrlen > MLEN)
1645 return (EINVAL);
1646 if (argp->ex_masklen < 0 || argp->ex_masklen > MLEN)
1647 return (EINVAL);
1649 i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
1650 np = (struct netcred *) kmalloc(i, M_NETADDR, M_WAITOK | M_ZERO);
1651 saddr = (struct sockaddr *) (np + 1);
1652 if ((error = copyin(argp->ex_addr, (caddr_t) saddr, argp->ex_addrlen)))
1653 goto out;
1654 if (saddr->sa_len > argp->ex_addrlen)
1655 saddr->sa_len = argp->ex_addrlen;
1656 if (argp->ex_masklen) {
1657 smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen);
1658 error = copyin(argp->ex_mask, (caddr_t)smask, argp->ex_masklen);
1659 if (error)
1660 goto out;
1661 if (smask->sa_len > argp->ex_masklen)
1662 smask->sa_len = argp->ex_masklen;
1664 i = saddr->sa_family;
1665 if ((rnh = nep->ne_rtable[i]) == 0) {
1667 * Seems silly to initialize every AF when most are not used,
1668 * do so on demand here
1670 SLIST_FOREACH(dom, &domains, dom_next)
1671 if (dom->dom_family == i && dom->dom_rtattach) {
1672 dom->dom_rtattach((void **) &nep->ne_rtable[i],
1673 dom->dom_rtoffset);
1674 break;
1676 if ((rnh = nep->ne_rtable[i]) == 0) {
1677 error = ENOBUFS;
1678 goto out;
1681 rn = (*rnh->rnh_addaddr) ((char *) saddr, (char *) smask, rnh,
1682 np->netc_rnodes);
1683 if (rn == 0 || np != (struct netcred *) rn) { /* already exists */
1684 error = EPERM;
1685 goto out;
1687 np->netc_exflags = argp->ex_flags;
1688 np->netc_anon = argp->ex_anon;
1689 np->netc_anon.cr_ref = 1;
1690 return (0);
1691 out:
1692 kfree(np, M_NETADDR);
1693 return (error);
1696 /* ARGSUSED */
1697 static int
1698 vfs_free_netcred(struct radix_node *rn, void *w)
1700 struct radix_node_head *rnh = (struct radix_node_head *) w;
1702 (*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, rnh);
1703 kfree((caddr_t) rn, M_NETADDR);
1704 return (0);
1708 * Free the net address hash lists that are hanging off the mount points.
1710 static void
1711 vfs_free_addrlist(struct netexport *nep)
1713 int i;
1714 struct radix_node_head *rnh;
1716 for (i = 0; i <= AF_MAX; i++)
1717 if ((rnh = nep->ne_rtable[i])) {
1718 (*rnh->rnh_walktree) (rnh, vfs_free_netcred,
1719 (caddr_t) rnh);
1720 kfree((caddr_t) rnh, M_RTABLE);
1721 nep->ne_rtable[i] = 0;
1726 vfs_export(struct mount *mp, struct netexport *nep,
1727 const struct export_args *argp)
1729 int error;
1731 if (argp->ex_flags & MNT_DELEXPORT) {
1732 if (mp->mnt_flag & MNT_EXPUBLIC) {
1733 vfs_setpublicfs(NULL, NULL, NULL);
1734 mp->mnt_flag &= ~MNT_EXPUBLIC;
1736 vfs_free_addrlist(nep);
1737 mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
1739 if (argp->ex_flags & MNT_EXPORTED) {
1740 if (argp->ex_flags & MNT_EXPUBLIC) {
1741 if ((error = vfs_setpublicfs(mp, nep, argp)) != 0)
1742 return (error);
1743 mp->mnt_flag |= MNT_EXPUBLIC;
1745 if ((error = vfs_hang_addrlist(mp, nep, argp)))
1746 return (error);
1747 mp->mnt_flag |= MNT_EXPORTED;
1749 return (0);
1754 * Set the publicly exported filesystem (WebNFS). Currently, only
1755 * one public filesystem is possible in the spec (RFC 2054 and 2055)
1758 vfs_setpublicfs(struct mount *mp, struct netexport *nep,
1759 const struct export_args *argp)
1761 int error;
1762 struct vnode *rvp;
1763 char *cp;
1766 * mp == NULL -> invalidate the current info, the FS is
1767 * no longer exported. May be called from either vfs_export
1768 * or unmount, so check if it hasn't already been done.
1770 if (mp == NULL) {
1771 if (nfs_pub.np_valid) {
1772 nfs_pub.np_valid = 0;
1773 if (nfs_pub.np_index != NULL) {
1774 FREE(nfs_pub.np_index, M_TEMP);
1775 nfs_pub.np_index = NULL;
1778 return (0);
1782 * Only one allowed at a time.
1784 if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
1785 return (EBUSY);
1788 * Get real filehandle for root of exported FS.
1790 bzero((caddr_t)&nfs_pub.np_handle, sizeof(nfs_pub.np_handle));
1791 nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid;
1793 if ((error = VFS_ROOT(mp, &rvp)))
1794 return (error);
1796 if ((error = VFS_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid)))
1797 return (error);
1799 vput(rvp);
1802 * If an indexfile was specified, pull it in.
1804 if (argp->ex_indexfile != NULL) {
1805 int namelen;
1807 error = vn_get_namelen(rvp, &namelen);
1808 if (error)
1809 return (error);
1810 MALLOC(nfs_pub.np_index, char *, namelen, M_TEMP,
1811 M_WAITOK);
1812 error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
1813 namelen, (size_t *)0);
1814 if (!error) {
1816 * Check for illegal filenames.
1818 for (cp = nfs_pub.np_index; *cp; cp++) {
1819 if (*cp == '/') {
1820 error = EINVAL;
1821 break;
1825 if (error) {
1826 FREE(nfs_pub.np_index, M_TEMP);
1827 return (error);
1831 nfs_pub.np_mount = mp;
1832 nfs_pub.np_valid = 1;
1833 return (0);
1836 struct netcred *
1837 vfs_export_lookup(struct mount *mp, struct netexport *nep,
1838 struct sockaddr *nam)
1840 struct netcred *np;
1841 struct radix_node_head *rnh;
1842 struct sockaddr *saddr;
1844 np = NULL;
1845 if (mp->mnt_flag & MNT_EXPORTED) {
1847 * Lookup in the export list first.
1849 if (nam != NULL) {
1850 saddr = nam;
1851 rnh = nep->ne_rtable[saddr->sa_family];
1852 if (rnh != NULL) {
1853 np = (struct netcred *)
1854 (*rnh->rnh_matchaddr)((char *)saddr,
1855 rnh);
1856 if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
1857 np = NULL;
1861 * If no address match, use the default if it exists.
1863 if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
1864 np = &nep->ne_defexported;
1866 return (np);
1870 * perform msync on all vnodes under a mount point. The mount point must
1871 * be locked. This code is also responsible for lazy-freeing unreferenced
1872 * vnodes whos VM objects no longer contain pages.
1874 * NOTE: MNT_WAIT still skips vnodes in the VXLOCK state.
1876 * NOTE: XXX VOP_PUTPAGES and friends requires that the vnode be locked,
1877 * but vnode_pager_putpages() doesn't lock the vnode. We have to do it
1878 * way up in this high level function.
1880 static int vfs_msync_scan1(struct mount *mp, struct vnode *vp, void *data);
1881 static int vfs_msync_scan2(struct mount *mp, struct vnode *vp, void *data);
1883 void
1884 vfs_msync(struct mount *mp, int flags)
1886 int vmsc_flags;
1888 vmsc_flags = VMSC_GETVP;
1889 if (flags != MNT_WAIT)
1890 vmsc_flags |= VMSC_NOWAIT;
1891 vmntvnodescan(mp, vmsc_flags, vfs_msync_scan1, vfs_msync_scan2,
1892 (void *)flags);
1896 * scan1 is a fast pre-check. There could be hundreds of thousands of
1897 * vnodes, we cannot afford to do anything heavy weight until we have a
1898 * fairly good indication that there is work to do.
1900 static
1902 vfs_msync_scan1(struct mount *mp, struct vnode *vp, void *data)
1904 int flags = (int)data;
1906 if ((vp->v_flag & VRECLAIMED) == 0) {
1907 if (vshouldmsync(vp))
1908 return(0); /* call scan2 */
1909 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
1910 (vp->v_flag & VOBJDIRTY) &&
1911 (flags == MNT_WAIT || vn_islocked(vp) == 0)) {
1912 return(0); /* call scan2 */
1917 * do not call scan2, continue the loop
1919 return(-1);
1923 * This callback is handed a locked vnode.
1925 static
1927 vfs_msync_scan2(struct mount *mp, struct vnode *vp, void *data)
1929 vm_object_t obj;
1930 int flags = (int)data;
1932 if (vp->v_flag & VRECLAIMED)
1933 return(0);
1935 if ((mp->mnt_flag & MNT_RDONLY) == 0 && (vp->v_flag & VOBJDIRTY)) {
1936 if ((obj = vp->v_object) != NULL) {
1937 vm_object_page_clean(obj, 0, 0,
1938 flags == MNT_WAIT ? OBJPC_SYNC : OBJPC_NOSYNC);
1941 return(0);
1945 * Record a process's interest in events which might happen to
1946 * a vnode. Because poll uses the historic select-style interface
1947 * internally, this routine serves as both the ``check for any
1948 * pending events'' and the ``record my interest in future events''
1949 * functions. (These are done together, while the lock is held,
1950 * to avoid race conditions.)
1953 vn_pollrecord(struct vnode *vp, int events)
1955 lwkt_tokref ilock;
1957 KKASSERT(curthread->td_proc != NULL);
1959 lwkt_gettoken(&ilock, &vp->v_pollinfo.vpi_token);
1960 if (vp->v_pollinfo.vpi_revents & events) {
1962 * This leaves events we are not interested
1963 * in available for the other process which
1964 * which presumably had requested them
1965 * (otherwise they would never have been
1966 * recorded).
1968 events &= vp->v_pollinfo.vpi_revents;
1969 vp->v_pollinfo.vpi_revents &= ~events;
1971 lwkt_reltoken(&ilock);
1972 return events;
1974 vp->v_pollinfo.vpi_events |= events;
1975 selrecord(curthread, &vp->v_pollinfo.vpi_selinfo);
1976 lwkt_reltoken(&ilock);
1977 return 0;
1981 * Note the occurrence of an event. If the VN_POLLEVENT macro is used,
1982 * it is possible for us to miss an event due to race conditions, but
1983 * that condition is expected to be rare, so for the moment it is the
1984 * preferred interface.
1986 void
1987 vn_pollevent(struct vnode *vp, int events)
1989 lwkt_tokref ilock;
1991 lwkt_gettoken(&ilock, &vp->v_pollinfo.vpi_token);
1992 if (vp->v_pollinfo.vpi_events & events) {
1994 * We clear vpi_events so that we don't
1995 * call selwakeup() twice if two events are
1996 * posted before the polling process(es) is
1997 * awakened. This also ensures that we take at
1998 * most one selwakeup() if the polling process
1999 * is no longer interested. However, it does
2000 * mean that only one event can be noticed at
2001 * a time. (Perhaps we should only clear those
2002 * event bits which we note?) XXX
2004 vp->v_pollinfo.vpi_events = 0; /* &= ~events ??? */
2005 vp->v_pollinfo.vpi_revents |= events;
2006 selwakeup(&vp->v_pollinfo.vpi_selinfo);
2008 lwkt_reltoken(&ilock);
2012 * Wake up anyone polling on vp because it is being revoked.
2013 * This depends on dead_poll() returning POLLHUP for correct
2014 * behavior.
2016 void
2017 vn_pollgone(struct vnode *vp)
2019 lwkt_tokref ilock;
2021 lwkt_gettoken(&ilock, &vp->v_pollinfo.vpi_token);
2022 if (vp->v_pollinfo.vpi_events) {
2023 vp->v_pollinfo.vpi_events = 0;
2024 selwakeup(&vp->v_pollinfo.vpi_selinfo);
2026 lwkt_reltoken(&ilock);
2030 * extract the cdev_t from a VBLK or VCHR. The vnode must have been opened
2031 * (or v_rdev might be NULL).
2033 cdev_t
2034 vn_todev(struct vnode *vp)
2036 if (vp->v_type != VBLK && vp->v_type != VCHR)
2037 return (NULL);
2038 KKASSERT(vp->v_rdev != NULL);
2039 return (vp->v_rdev);
2043 * Check if vnode represents a disk device. The vnode does not need to be
2044 * opened.
2047 vn_isdisk(struct vnode *vp, int *errp)
2049 cdev_t dev;
2051 if (vp->v_type != VCHR) {
2052 if (errp != NULL)
2053 *errp = ENOTBLK;
2054 return (0);
2057 if ((dev = vp->v_rdev) == NULL)
2058 dev = get_dev(vp->v_umajor, vp->v_uminor);
2060 if (dev == NULL) {
2061 if (errp != NULL)
2062 *errp = ENXIO;
2063 return (0);
2065 if (dev_is_good(dev) == 0) {
2066 if (errp != NULL)
2067 *errp = ENXIO;
2068 return (0);
2070 if ((dev_dflags(dev) & D_DISK) == 0) {
2071 if (errp != NULL)
2072 *errp = ENOTBLK;
2073 return (0);
2075 if (errp != NULL)
2076 *errp = 0;
2077 return (1);
2081 vn_get_namelen(struct vnode *vp, int *namelen)
2083 int error, retval[2];
2085 error = VOP_PATHCONF(vp, _PC_NAME_MAX, retval);
2086 if (error)
2087 return (error);
2088 *namelen = *retval;
2089 return (0);
2093 vop_write_dirent(int *error, struct uio *uio, ino_t d_ino, uint8_t d_type,
2094 uint16_t d_namlen, const char *d_name)
2096 struct dirent *dp;
2097 size_t len;
2099 len = _DIRENT_RECLEN(d_namlen);
2100 if (len > uio->uio_resid)
2101 return(1);
2103 dp = kmalloc(len, M_TEMP, M_WAITOK | M_ZERO);
2105 dp->d_ino = d_ino;
2106 dp->d_namlen = d_namlen;
2107 dp->d_type = d_type;
2108 bcopy(d_name, dp->d_name, d_namlen);
2110 *error = uiomove((caddr_t)dp, len, uio);
2112 kfree(dp, M_TEMP);
2114 return(0);