kernel - Modestly reduce default maxvnodes calculation
[dragonfly.git] / sys / kern / vfs_subr.c
blobadc5c8b2cf6b9bc25cf4b916b090e9a87326f875
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. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
34 * @(#)vfs_subr.c 8.31 (Berkeley) 5/26/95
35 * $FreeBSD: src/sys/kern/vfs_subr.c,v 1.249.2.30 2003/04/04 20:35:57 tegge Exp $
39 * External virtual filesystem routines
41 #include "opt_ddb.h"
42 #include "opt_inet.h"
43 #include "opt_inet6.h"
45 #include <sys/param.h>
46 #include <sys/systm.h>
47 #include <sys/buf.h>
48 #include <sys/conf.h>
49 #include <sys/dirent.h>
50 #include <sys/eventhandler.h>
51 #include <sys/fcntl.h>
52 #include <sys/file.h>
53 #include <sys/kernel.h>
54 #include <sys/kthread.h>
55 #include <sys/malloc.h>
56 #include <sys/mbuf.h>
57 #include <sys/mount.h>
58 #include <sys/priv.h>
59 #include <sys/proc.h>
60 #include <sys/reboot.h>
61 #include <sys/socket.h>
62 #include <sys/stat.h>
63 #include <sys/sysctl.h>
64 #include <sys/syslog.h>
65 #include <sys/unistd.h>
66 #include <sys/vmmeter.h>
67 #include <sys/vnode.h>
69 #include <machine/limits.h>
71 #include <vm/vm.h>
72 #include <vm/vm_object.h>
73 #include <vm/vm_extern.h>
74 #include <vm/vm_kern.h>
75 #include <vm/pmap.h>
76 #include <vm/vm_map.h>
77 #include <vm/vm_page.h>
78 #include <vm/vm_pager.h>
79 #include <vm/vnode_pager.h>
80 #include <vm/vm_zone.h>
82 #include <sys/buf2.h>
83 #include <sys/thread2.h>
84 #include <sys/sysref2.h>
85 #include <sys/mplock2.h>
87 #include <netinet/in.h>
89 static MALLOC_DEFINE(M_NETADDR, "Export Host", "Export host address structure");
91 int numvnodes;
92 SYSCTL_INT(_debug, OID_AUTO, numvnodes, CTLFLAG_RD, &numvnodes, 0,
93 "Number of vnodes allocated");
94 int verbose_reclaims;
95 SYSCTL_INT(_debug, OID_AUTO, verbose_reclaims, CTLFLAG_RD, &verbose_reclaims, 0,
96 "Output filename of reclaimed vnode(s)");
98 enum vtype iftovt_tab[16] = {
99 VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
100 VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
102 int vttoif_tab[9] = {
103 0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
104 S_IFSOCK, S_IFIFO, S_IFMT,
107 static int reassignbufcalls;
108 SYSCTL_INT(_vfs, OID_AUTO, reassignbufcalls, CTLFLAG_RW, &reassignbufcalls,
109 0, "Number of times buffers have been reassigned to the proper list");
111 static int check_buf_overlap = 2; /* invasive check */
112 SYSCTL_INT(_vfs, OID_AUTO, check_buf_overlap, CTLFLAG_RW, &check_buf_overlap,
113 0, "Enable overlapping buffer checks");
115 int nfs_mount_type = -1;
116 static struct lwkt_token spechash_token;
117 struct nfs_public nfs_pub; /* publicly exported FS */
119 int maxvnodes;
120 SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RW,
121 &maxvnodes, 0, "Maximum number of vnodes");
123 static struct radix_node_head *vfs_create_addrlist_af(int af,
124 struct netexport *nep);
125 static void vfs_free_addrlist (struct netexport *nep);
126 static int vfs_free_netcred (struct radix_node *rn, void *w);
127 static void vfs_free_addrlist_af (struct radix_node_head **prnh);
128 static int vfs_hang_addrlist (struct mount *mp, struct netexport *nep,
129 const struct export_args *argp);
131 int prtactive = 0; /* 1 => print out reclaim of active vnodes */
134 * Red black tree functions
136 static int rb_buf_compare(struct buf *b1, struct buf *b2);
137 RB_GENERATE2(buf_rb_tree, buf, b_rbnode, rb_buf_compare, off_t, b_loffset);
138 RB_GENERATE2(buf_rb_hash, buf, b_rbhash, rb_buf_compare, off_t, b_loffset);
140 static int
141 rb_buf_compare(struct buf *b1, struct buf *b2)
143 if (b1->b_loffset < b2->b_loffset)
144 return(-1);
145 if (b1->b_loffset > b2->b_loffset)
146 return(1);
147 return(0);
151 * Initialize the vnode management data structures.
153 * Called from vfsinit()
155 void
156 vfs_subr_init(void)
158 int factor1;
159 int factor2;
162 * Desiredvnodes is kern.maxvnodes. We want to scale it
163 * according to available system memory but we may also have
164 * to limit it based on available KVM, which is capped on 32 bit
165 * systems, to ~80K vnodes or so.
167 * WARNING! For machines with 64-256M of ram we have to be sure
168 * that the default limit scales down well due to HAMMER
169 * taking up significantly more memory per-vnode vs UFS.
170 * We want around ~5800 on a 128M machine.
172 factor1 = 25 * (sizeof(struct vm_object) + sizeof(struct vnode));
173 factor2 = 30 * (sizeof(struct vm_object) + sizeof(struct vnode));
174 maxvnodes = imin((int64_t)vmstats.v_page_count * PAGE_SIZE / factor1,
175 KvaSize / factor2);
176 maxvnodes = imax(maxvnodes, maxproc * 8);
178 lwkt_token_init(&spechash_token, "spechash");
182 * Knob to control the precision of file timestamps:
184 * 0 = seconds only; nanoseconds zeroed.
185 * 1 = seconds and nanoseconds, accurate within 1/HZ.
186 * 2 = seconds and nanoseconds, truncated to microseconds.
187 * >=3 = seconds and nanoseconds, maximum precision.
189 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC };
191 static int timestamp_precision = TSP_SEC;
192 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW,
193 &timestamp_precision, 0, "Precision of file timestamps");
196 * Get a current timestamp.
198 * MPSAFE
200 void
201 vfs_timestamp(struct timespec *tsp)
203 struct timeval tv;
205 switch (timestamp_precision) {
206 case TSP_SEC:
207 tsp->tv_sec = time_second;
208 tsp->tv_nsec = 0;
209 break;
210 case TSP_HZ:
211 getnanotime(tsp);
212 break;
213 case TSP_USEC:
214 microtime(&tv);
215 TIMEVAL_TO_TIMESPEC(&tv, tsp);
216 break;
217 case TSP_NSEC:
218 default:
219 nanotime(tsp);
220 break;
225 * Set vnode attributes to VNOVAL
227 void
228 vattr_null(struct vattr *vap)
230 vap->va_type = VNON;
231 vap->va_size = VNOVAL;
232 vap->va_bytes = VNOVAL;
233 vap->va_mode = VNOVAL;
234 vap->va_nlink = VNOVAL;
235 vap->va_uid = VNOVAL;
236 vap->va_gid = VNOVAL;
237 vap->va_fsid = VNOVAL;
238 vap->va_fileid = VNOVAL;
239 vap->va_blocksize = VNOVAL;
240 vap->va_rmajor = VNOVAL;
241 vap->va_rminor = VNOVAL;
242 vap->va_atime.tv_sec = VNOVAL;
243 vap->va_atime.tv_nsec = VNOVAL;
244 vap->va_mtime.tv_sec = VNOVAL;
245 vap->va_mtime.tv_nsec = VNOVAL;
246 vap->va_ctime.tv_sec = VNOVAL;
247 vap->va_ctime.tv_nsec = VNOVAL;
248 vap->va_flags = VNOVAL;
249 vap->va_gen = VNOVAL;
250 vap->va_vaflags = 0;
251 /* va_*_uuid fields are only valid if related flags are set */
255 * Flush out and invalidate all buffers associated with a vnode.
257 * vp must be locked.
259 static int vinvalbuf_bp(struct buf *bp, void *data);
261 struct vinvalbuf_bp_info {
262 struct vnode *vp;
263 int slptimeo;
264 int lkflags;
265 int flags;
266 int clean;
270 vinvalbuf(struct vnode *vp, int flags, int slpflag, int slptimeo)
272 struct vinvalbuf_bp_info info;
273 vm_object_t object;
274 int error;
276 lwkt_gettoken(&vp->v_token);
279 * If we are being asked to save, call fsync to ensure that the inode
280 * is updated.
282 if (flags & V_SAVE) {
283 error = bio_track_wait(&vp->v_track_write, slpflag, slptimeo);
284 if (error)
285 goto done;
286 if (!RB_EMPTY(&vp->v_rbdirty_tree)) {
287 if ((error = VOP_FSYNC(vp, MNT_WAIT, 0)) != 0)
288 goto done;
289 #if 0
291 * Dirty bufs may be left or generated via races
292 * in circumstances where vinvalbuf() is called on
293 * a vnode not undergoing reclamation. Only
294 * panic if we are trying to reclaim the vnode.
296 if ((vp->v_flag & VRECLAIMED) &&
297 (bio_track_active(&vp->v_track_write) ||
298 !RB_EMPTY(&vp->v_rbdirty_tree))) {
299 panic("vinvalbuf: dirty bufs");
301 #endif
304 info.slptimeo = slptimeo;
305 info.lkflags = LK_EXCLUSIVE | LK_SLEEPFAIL;
306 if (slpflag & PCATCH)
307 info.lkflags |= LK_PCATCH;
308 info.flags = flags;
309 info.vp = vp;
312 * Flush the buffer cache until nothing is left, wait for all I/O
313 * to complete. At least one pass is required. We might block
314 * in the pip code so we have to re-check. Order is important.
316 do {
318 * Flush buffer cache
320 if (!RB_EMPTY(&vp->v_rbclean_tree)) {
321 info.clean = 1;
322 error = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree,
323 NULL, vinvalbuf_bp, &info);
325 if (!RB_EMPTY(&vp->v_rbdirty_tree)) {
326 info.clean = 0;
327 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
328 NULL, vinvalbuf_bp, &info);
332 * Wait for I/O completion.
334 bio_track_wait(&vp->v_track_write, 0, 0);
335 if ((object = vp->v_object) != NULL)
336 refcount_wait(&object->paging_in_progress, "vnvlbx");
337 } while (bio_track_active(&vp->v_track_write) ||
338 !RB_EMPTY(&vp->v_rbclean_tree) ||
339 !RB_EMPTY(&vp->v_rbdirty_tree));
342 * Destroy the copy in the VM cache, too.
344 if ((object = vp->v_object) != NULL) {
345 vm_object_page_remove(object, 0, 0,
346 (flags & V_SAVE) ? TRUE : FALSE);
349 if (!RB_EMPTY(&vp->v_rbdirty_tree) || !RB_EMPTY(&vp->v_rbclean_tree))
350 panic("vinvalbuf: flush failed");
351 if (!RB_EMPTY(&vp->v_rbhash_tree))
352 panic("vinvalbuf: flush failed, buffers still present");
353 error = 0;
354 done:
355 lwkt_reltoken(&vp->v_token);
356 return (error);
359 static int
360 vinvalbuf_bp(struct buf *bp, void *data)
362 struct vinvalbuf_bp_info *info = data;
363 int error;
365 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
366 atomic_add_int(&bp->b_refs, 1);
367 error = BUF_TIMELOCK(bp, info->lkflags,
368 "vinvalbuf", info->slptimeo);
369 atomic_subtract_int(&bp->b_refs, 1);
370 if (error == 0) {
371 BUF_UNLOCK(bp);
372 error = ENOLCK;
374 if (error == ENOLCK)
375 return(0);
376 return (-error);
378 KKASSERT(bp->b_vp == info->vp);
381 * Must check clean/dirty status after successfully locking as
382 * it may race.
384 if ((info->clean && (bp->b_flags & B_DELWRI)) ||
385 (info->clean == 0 && (bp->b_flags & B_DELWRI) == 0)) {
386 BUF_UNLOCK(bp);
387 return(0);
391 * NOTE: NO B_LOCKED CHECK. Also no buf_checkwrite()
392 * check. This code will write out the buffer, period.
394 bremfree(bp);
395 if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
396 (info->flags & V_SAVE)) {
397 cluster_awrite(bp);
398 } else if (info->flags & V_SAVE) {
400 * Cannot set B_NOCACHE on a clean buffer as this will
401 * destroy the VM backing store which might actually
402 * be dirty (and unsynchronized).
404 bp->b_flags |= (B_INVAL | B_RELBUF);
405 brelse(bp);
406 } else {
407 bp->b_flags |= (B_INVAL | B_NOCACHE | B_RELBUF);
408 brelse(bp);
410 return(0);
414 * Truncate a file's buffer and pages to a specified length. This
415 * is in lieu of the old vinvalbuf mechanism, which performed unneeded
416 * sync activity.
418 * The vnode must be locked.
420 static int vtruncbuf_bp_trunc_cmp(struct buf *bp, void *data);
421 static int vtruncbuf_bp_trunc(struct buf *bp, void *data);
422 static int vtruncbuf_bp_metasync_cmp(struct buf *bp, void *data);
423 static int vtruncbuf_bp_metasync(struct buf *bp, void *data);
425 struct vtruncbuf_info {
426 struct vnode *vp;
427 off_t truncloffset;
428 int clean;
432 vtruncbuf(struct vnode *vp, off_t length, int blksize)
434 struct vtruncbuf_info info;
435 const char *filename;
436 int count;
439 * Round up to the *next* block, then destroy the buffers in question.
440 * Since we are only removing some of the buffers we must rely on the
441 * scan count to determine whether a loop is necessary.
443 if ((count = (int)(length % blksize)) != 0)
444 info.truncloffset = length + (blksize - count);
445 else
446 info.truncloffset = length;
447 info.vp = vp;
449 lwkt_gettoken(&vp->v_token);
450 do {
451 info.clean = 1;
452 count = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree,
453 vtruncbuf_bp_trunc_cmp,
454 vtruncbuf_bp_trunc, &info);
455 info.clean = 0;
456 count += RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
457 vtruncbuf_bp_trunc_cmp,
458 vtruncbuf_bp_trunc, &info);
459 } while(count);
462 * For safety, fsync any remaining metadata if the file is not being
463 * truncated to 0. Since the metadata does not represent the entire
464 * dirty list we have to rely on the hit count to ensure that we get
465 * all of it.
467 if (length > 0) {
468 do {
469 count = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
470 vtruncbuf_bp_metasync_cmp,
471 vtruncbuf_bp_metasync, &info);
472 } while (count);
476 * Clean out any left over VM backing store.
478 * It is possible to have in-progress I/O from buffers that were
479 * not part of the truncation. This should not happen if we
480 * are truncating to 0-length.
482 vnode_pager_setsize(vp, length);
483 bio_track_wait(&vp->v_track_write, 0, 0);
486 * Debugging only
488 spin_lock(&vp->v_spin);
489 filename = TAILQ_FIRST(&vp->v_namecache) ?
490 TAILQ_FIRST(&vp->v_namecache)->nc_name : "?";
491 spin_unlock(&vp->v_spin);
494 * Make sure no buffers were instantiated while we were trying
495 * to clean out the remaining VM pages. This could occur due
496 * to busy dirty VM pages being flushed out to disk.
498 do {
499 info.clean = 1;
500 count = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree,
501 vtruncbuf_bp_trunc_cmp,
502 vtruncbuf_bp_trunc, &info);
503 info.clean = 0;
504 count += RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
505 vtruncbuf_bp_trunc_cmp,
506 vtruncbuf_bp_trunc, &info);
507 if (count) {
508 kprintf("Warning: vtruncbuf(): Had to re-clean %d "
509 "left over buffers in %s\n", count, filename);
511 } while(count);
513 lwkt_reltoken(&vp->v_token);
515 return (0);
519 * The callback buffer is beyond the new file EOF and must be destroyed.
520 * Note that the compare function must conform to the RB_SCAN's requirements.
522 static
524 vtruncbuf_bp_trunc_cmp(struct buf *bp, void *data)
526 struct vtruncbuf_info *info = data;
528 if (bp->b_loffset >= info->truncloffset)
529 return(0);
530 return(-1);
533 static
534 int
535 vtruncbuf_bp_trunc(struct buf *bp, void *data)
537 struct vtruncbuf_info *info = data;
540 * Do not try to use a buffer we cannot immediately lock, but sleep
541 * anyway to prevent a livelock. The code will loop until all buffers
542 * can be acted upon.
544 * We must always revalidate the buffer after locking it to deal
545 * with MP races.
547 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
548 atomic_add_int(&bp->b_refs, 1);
549 if (BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL) == 0)
550 BUF_UNLOCK(bp);
551 atomic_subtract_int(&bp->b_refs, 1);
552 } else if ((info->clean && (bp->b_flags & B_DELWRI)) ||
553 (info->clean == 0 && (bp->b_flags & B_DELWRI) == 0) ||
554 bp->b_vp != info->vp ||
555 vtruncbuf_bp_trunc_cmp(bp, data)) {
556 BUF_UNLOCK(bp);
557 } else {
558 bremfree(bp);
559 bp->b_flags |= (B_INVAL | B_RELBUF | B_NOCACHE);
560 brelse(bp);
562 return(1);
566 * Fsync all meta-data after truncating a file to be non-zero. Only metadata
567 * blocks (with a negative loffset) are scanned.
568 * Note that the compare function must conform to the RB_SCAN's requirements.
570 static int
571 vtruncbuf_bp_metasync_cmp(struct buf *bp, void *data __unused)
573 if (bp->b_loffset < 0)
574 return(0);
575 return(1);
578 static int
579 vtruncbuf_bp_metasync(struct buf *bp, void *data)
581 struct vtruncbuf_info *info = data;
583 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
584 atomic_add_int(&bp->b_refs, 1);
585 if (BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL) == 0)
586 BUF_UNLOCK(bp);
587 atomic_subtract_int(&bp->b_refs, 1);
588 } else if ((bp->b_flags & B_DELWRI) == 0 ||
589 bp->b_vp != info->vp ||
590 vtruncbuf_bp_metasync_cmp(bp, data)) {
591 BUF_UNLOCK(bp);
592 } else {
593 bremfree(bp);
594 if (bp->b_vp == info->vp)
595 bawrite(bp);
596 else
597 bwrite(bp);
599 return(1);
603 * vfsync - implements a multipass fsync on a file which understands
604 * dependancies and meta-data. The passed vnode must be locked. The
605 * waitfor argument may be MNT_WAIT or MNT_NOWAIT, or MNT_LAZY.
607 * When fsyncing data asynchronously just do one consolidated pass starting
608 * with the most negative block number. This may not get all the data due
609 * to dependancies.
611 * When fsyncing data synchronously do a data pass, then a metadata pass,
612 * then do additional data+metadata passes to try to get all the data out.
614 * Caller must ref the vnode but does not have to lock it.
616 static int vfsync_wait_output(struct vnode *vp,
617 int (*waitoutput)(struct vnode *, struct thread *));
618 static int vfsync_dummy_cmp(struct buf *bp __unused, void *data __unused);
619 static int vfsync_data_only_cmp(struct buf *bp, void *data);
620 static int vfsync_meta_only_cmp(struct buf *bp, void *data);
621 static int vfsync_lazy_range_cmp(struct buf *bp, void *data);
622 static int vfsync_bp(struct buf *bp, void *data);
624 struct vfsync_info {
625 struct vnode *vp;
626 int fastpass;
627 int synchronous;
628 int syncdeps;
629 int lazycount;
630 int lazylimit;
631 int skippedbufs;
632 int (*checkdef)(struct buf *);
633 int (*cmpfunc)(struct buf *, void *);
637 vfsync(struct vnode *vp, int waitfor, int passes,
638 int (*checkdef)(struct buf *),
639 int (*waitoutput)(struct vnode *, struct thread *))
641 struct vfsync_info info;
642 int error;
644 bzero(&info, sizeof(info));
645 info.vp = vp;
646 if ((info.checkdef = checkdef) == NULL)
647 info.syncdeps = 1;
649 lwkt_gettoken(&vp->v_token);
651 switch(waitfor) {
652 case MNT_LAZY | MNT_NOWAIT:
653 case MNT_LAZY:
655 * Lazy (filesystem syncer typ) Asynchronous plus limit the
656 * number of data (not meta) pages we try to flush to 1MB.
657 * A non-zero return means that lazy limit was reached.
659 info.lazylimit = 1024 * 1024;
660 info.syncdeps = 1;
661 info.cmpfunc = vfsync_lazy_range_cmp;
662 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
663 vfsync_lazy_range_cmp, vfsync_bp, &info);
664 info.cmpfunc = vfsync_meta_only_cmp;
665 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
666 vfsync_meta_only_cmp, vfsync_bp, &info);
667 if (error == 0)
668 vp->v_lazyw = 0;
669 else if (!RB_EMPTY(&vp->v_rbdirty_tree))
670 vn_syncer_add(vp, 1);
671 error = 0;
672 break;
673 case MNT_NOWAIT:
675 * Asynchronous. Do a data-only pass and a meta-only pass.
677 info.syncdeps = 1;
678 info.cmpfunc = vfsync_data_only_cmp;
679 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_data_only_cmp,
680 vfsync_bp, &info);
681 info.cmpfunc = vfsync_meta_only_cmp;
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 info.cmpfunc = vfsync_data_only_cmp;
693 info.fastpass = 1;
694 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_data_only_cmp,
695 vfsync_bp, &info);
696 info.fastpass = 0;
697 error = vfsync_wait_output(vp, waitoutput);
698 if (error == 0) {
699 info.skippedbufs = 0;
700 info.cmpfunc = vfsync_dummy_cmp;
701 RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
702 vfsync_bp, &info);
703 error = vfsync_wait_output(vp, waitoutput);
704 if (info.skippedbufs) {
705 kprintf("Warning: vfsync skipped %d dirty "
706 "buf%s in pass2!\n",
707 info.skippedbufs,
708 ((info.skippedbufs > 1) ? "s" : ""));
711 while (error == 0 && passes > 0 &&
712 !RB_EMPTY(&vp->v_rbdirty_tree)
714 info.skippedbufs = 0;
715 if (--passes == 0) {
716 info.synchronous = 1;
717 info.syncdeps = 1;
719 info.cmpfunc = vfsync_dummy_cmp;
720 error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
721 vfsync_bp, &info);
722 if (error < 0)
723 error = -error;
724 info.syncdeps = 1;
725 if (error == 0)
726 error = vfsync_wait_output(vp, waitoutput);
727 if (info.skippedbufs && passes == 0) {
728 kprintf("Warning: vfsync skipped %d dirty "
729 "buf%s in final pass!\n",
730 info.skippedbufs,
731 ((info.skippedbufs > 1) ? "s" : ""));
734 if (!RB_EMPTY(&vp->v_rbdirty_tree))
735 kprintf("dirty bufs left after final pass\n");
736 break;
738 lwkt_reltoken(&vp->v_token);
740 return(error);
743 static int
744 vfsync_wait_output(struct vnode *vp,
745 int (*waitoutput)(struct vnode *, struct thread *))
747 int error;
749 error = bio_track_wait(&vp->v_track_write, 0, 0);
750 if (waitoutput)
751 error = waitoutput(vp, curthread);
752 return(error);
755 static int
756 vfsync_dummy_cmp(struct buf *bp __unused, void *data __unused)
758 return(0);
761 static int
762 vfsync_data_only_cmp(struct buf *bp, void *data)
764 if (bp->b_loffset < 0)
765 return(-1);
766 return(0);
769 static int
770 vfsync_meta_only_cmp(struct buf *bp, void *data)
772 if (bp->b_loffset < 0)
773 return(0);
774 return(1);
777 static int
778 vfsync_lazy_range_cmp(struct buf *bp, void *data)
780 struct vfsync_info *info = data;
782 if (bp->b_loffset < info->vp->v_lazyw)
783 return(-1);
784 return(0);
787 static int
788 vfsync_bp(struct buf *bp, void *data)
790 struct vfsync_info *info = data;
791 struct vnode *vp = info->vp;
792 int error;
794 if (info->fastpass) {
796 * Ignore buffers that we cannot immediately lock.
798 if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
799 if (BUF_TIMELOCK(bp, LK_EXCLUSIVE, "bflst1", 1)) {
800 ++info->skippedbufs;
801 return(0);
804 } else if (info->synchronous == 0) {
806 * Normal pass, give the buffer a little time to become
807 * available to us.
809 if (BUF_TIMELOCK(bp, LK_EXCLUSIVE, "bflst2", hz / 10)) {
810 ++info->skippedbufs;
811 return(0);
813 } else {
815 * Synchronous pass, give the buffer a lot of time before
816 * giving up.
818 if (BUF_TIMELOCK(bp, LK_EXCLUSIVE, "bflst3", hz * 10)) {
819 ++info->skippedbufs;
820 return(0);
825 * We must revalidate the buffer after locking.
827 if ((bp->b_flags & B_DELWRI) == 0 ||
828 bp->b_vp != info->vp ||
829 info->cmpfunc(bp, data)) {
830 BUF_UNLOCK(bp);
831 return(0);
835 * If syncdeps is not set we do not try to write buffers which have
836 * dependancies.
838 if (!info->synchronous && info->syncdeps == 0 && info->checkdef(bp)) {
839 BUF_UNLOCK(bp);
840 return(0);
844 * B_NEEDCOMMIT (primarily used by NFS) is a state where the buffer
845 * has been written but an additional handshake with the device
846 * is required before we can dispose of the buffer. We have no idea
847 * how to do this so we have to skip these buffers.
849 if (bp->b_flags & B_NEEDCOMMIT) {
850 BUF_UNLOCK(bp);
851 return(0);
855 * Ask bioops if it is ok to sync. If not the VFS may have
856 * set B_LOCKED so we have to cycle the buffer.
858 if (LIST_FIRST(&bp->b_dep) != NULL && buf_checkwrite(bp)) {
859 bremfree(bp);
860 brelse(bp);
861 return(0);
864 if (info->synchronous) {
866 * Synchronous flushing. An error may be returned.
868 bremfree(bp);
869 error = bwrite(bp);
870 } else {
872 * Asynchronous flushing. A negative return value simply
873 * stops the scan and is not considered an error. We use
874 * this to support limited MNT_LAZY flushes.
876 vp->v_lazyw = bp->b_loffset;
877 bremfree(bp);
878 info->lazycount += cluster_awrite(bp);
879 waitrunningbufspace();
880 vm_wait_nominal();
881 if (info->lazylimit && info->lazycount >= info->lazylimit)
882 error = 1;
883 else
884 error = 0;
886 return(-error);
890 * Associate a buffer with a vnode.
892 * MPSAFE
895 bgetvp(struct vnode *vp, struct buf *bp, int testsize)
897 KASSERT(bp->b_vp == NULL, ("bgetvp: not free"));
898 KKASSERT((bp->b_flags & (B_HASHED|B_DELWRI|B_VNCLEAN|B_VNDIRTY)) == 0);
901 * Insert onto list for new vnode.
903 lwkt_gettoken(&vp->v_token);
905 if (buf_rb_hash_RB_INSERT(&vp->v_rbhash_tree, bp)) {
906 lwkt_reltoken(&vp->v_token);
907 return (EEXIST);
911 * Diagnostics (mainly for HAMMER debugging). Check for
912 * overlapping buffers.
914 if (check_buf_overlap) {
915 struct buf *bx;
916 bx = buf_rb_hash_RB_PREV(bp);
917 if (bx) {
918 if (bx->b_loffset + bx->b_bufsize > bp->b_loffset) {
919 kprintf("bgetvp: overlapl %016jx/%d %016jx "
920 "bx %p bp %p\n",
921 (intmax_t)bx->b_loffset,
922 bx->b_bufsize,
923 (intmax_t)bp->b_loffset,
924 bx, bp);
925 if (check_buf_overlap > 1)
926 panic("bgetvp - overlapping buffer");
929 bx = buf_rb_hash_RB_NEXT(bp);
930 if (bx) {
931 if (bp->b_loffset + testsize > bx->b_loffset) {
932 kprintf("bgetvp: overlapr %016jx/%d %016jx "
933 "bp %p bx %p\n",
934 (intmax_t)bp->b_loffset,
935 testsize,
936 (intmax_t)bx->b_loffset,
937 bp, bx);
938 if (check_buf_overlap > 1)
939 panic("bgetvp - overlapping buffer");
943 bp->b_vp = vp;
944 bp->b_flags |= B_HASHED;
945 bp->b_flags |= B_VNCLEAN;
946 if (buf_rb_tree_RB_INSERT(&vp->v_rbclean_tree, bp))
947 panic("reassignbuf: dup lblk/clean vp %p bp %p", vp, bp);
948 /*vhold(vp);*/
949 lwkt_reltoken(&vp->v_token);
950 return(0);
954 * Disassociate a buffer from a vnode.
956 * MPSAFE
958 void
959 brelvp(struct buf *bp)
961 struct vnode *vp;
963 KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
966 * Delete from old vnode list, if on one.
968 vp = bp->b_vp;
969 lwkt_gettoken(&vp->v_token);
970 if (bp->b_flags & (B_VNDIRTY | B_VNCLEAN)) {
971 if (bp->b_flags & B_VNDIRTY)
972 buf_rb_tree_RB_REMOVE(&vp->v_rbdirty_tree, bp);
973 else
974 buf_rb_tree_RB_REMOVE(&vp->v_rbclean_tree, bp);
975 bp->b_flags &= ~(B_VNDIRTY | B_VNCLEAN);
977 if (bp->b_flags & B_HASHED) {
978 buf_rb_hash_RB_REMOVE(&vp->v_rbhash_tree, bp);
979 bp->b_flags &= ~B_HASHED;
983 * Only remove from synclist when no dirty buffers are left AND
984 * the VFS has not flagged the vnode's inode as being dirty.
986 if ((vp->v_flag & (VONWORKLST | VISDIRTY | VOBJDIRTY)) == VONWORKLST &&
987 RB_EMPTY(&vp->v_rbdirty_tree)) {
988 vn_syncer_remove(vp, 0);
990 bp->b_vp = NULL;
992 lwkt_reltoken(&vp->v_token);
994 /*vdrop(vp);*/
998 * Reassign the buffer to the proper clean/dirty list based on B_DELWRI.
999 * This routine is called when the state of the B_DELWRI bit is changed.
1001 * Must be called with vp->v_token held.
1002 * MPSAFE
1004 void
1005 reassignbuf(struct buf *bp)
1007 struct vnode *vp = bp->b_vp;
1008 int delay;
1010 ASSERT_LWKT_TOKEN_HELD(&vp->v_token);
1011 ++reassignbufcalls;
1014 * B_PAGING flagged buffers cannot be reassigned because their vp
1015 * is not fully linked in.
1017 if (bp->b_flags & B_PAGING)
1018 panic("cannot reassign paging buffer");
1020 if (bp->b_flags & B_DELWRI) {
1022 * Move to the dirty list, add the vnode to the worklist
1024 if (bp->b_flags & B_VNCLEAN) {
1025 buf_rb_tree_RB_REMOVE(&vp->v_rbclean_tree, bp);
1026 bp->b_flags &= ~B_VNCLEAN;
1028 if ((bp->b_flags & B_VNDIRTY) == 0) {
1029 if (buf_rb_tree_RB_INSERT(&vp->v_rbdirty_tree, bp)) {
1030 panic("reassignbuf: dup lblk vp %p bp %p",
1031 vp, bp);
1033 bp->b_flags |= B_VNDIRTY;
1035 if ((vp->v_flag & VONWORKLST) == 0) {
1036 switch (vp->v_type) {
1037 case VDIR:
1038 delay = dirdelay;
1039 break;
1040 case VCHR:
1041 case VBLK:
1042 if (vp->v_rdev &&
1043 vp->v_rdev->si_mountpoint != NULL) {
1044 delay = metadelay;
1045 break;
1047 /* fall through */
1048 default:
1049 delay = filedelay;
1051 vn_syncer_add(vp, delay);
1053 } else {
1055 * Move to the clean list, remove the vnode from the worklist
1056 * if no dirty blocks remain.
1058 if (bp->b_flags & B_VNDIRTY) {
1059 buf_rb_tree_RB_REMOVE(&vp->v_rbdirty_tree, bp);
1060 bp->b_flags &= ~B_VNDIRTY;
1062 if ((bp->b_flags & B_VNCLEAN) == 0) {
1063 if (buf_rb_tree_RB_INSERT(&vp->v_rbclean_tree, bp)) {
1064 panic("reassignbuf: dup lblk vp %p bp %p",
1065 vp, bp);
1067 bp->b_flags |= B_VNCLEAN;
1071 * Only remove from synclist when no dirty buffers are left
1072 * AND the VFS has not flagged the vnode's inode as being
1073 * dirty.
1075 if ((vp->v_flag & (VONWORKLST | VISDIRTY | VOBJDIRTY)) ==
1076 VONWORKLST &&
1077 RB_EMPTY(&vp->v_rbdirty_tree)) {
1078 vn_syncer_remove(vp, 0);
1084 * Create a vnode for a block device. Used for mounting the root file
1085 * system.
1087 * A vref()'d vnode is returned.
1089 extern struct vop_ops *devfs_vnode_dev_vops_p;
1091 bdevvp(cdev_t dev, struct vnode **vpp)
1093 struct vnode *vp;
1094 struct vnode *nvp;
1095 int error;
1097 if (dev == NULL) {
1098 *vpp = NULLVP;
1099 return (ENXIO);
1101 error = getspecialvnode(VT_NON, NULL, &devfs_vnode_dev_vops_p,
1102 &nvp, 0, 0);
1103 if (error) {
1104 *vpp = NULLVP;
1105 return (error);
1107 vp = nvp;
1108 vp->v_type = VCHR;
1109 #if 0
1110 vp->v_rdev = dev;
1111 #endif
1112 v_associate_rdev(vp, dev);
1113 vp->v_umajor = dev->si_umajor;
1114 vp->v_uminor = dev->si_uminor;
1115 vx_unlock(vp);
1116 *vpp = vp;
1117 return (0);
1121 v_associate_rdev(struct vnode *vp, cdev_t dev)
1123 if (dev == NULL)
1124 return(ENXIO);
1125 if (dev_is_good(dev) == 0)
1126 return(ENXIO);
1127 KKASSERT(vp->v_rdev == NULL);
1128 vp->v_rdev = reference_dev(dev);
1129 lwkt_gettoken(&spechash_token);
1130 SLIST_INSERT_HEAD(&dev->si_hlist, vp, v_cdevnext);
1131 lwkt_reltoken(&spechash_token);
1132 return(0);
1135 void
1136 v_release_rdev(struct vnode *vp)
1138 cdev_t dev;
1140 if ((dev = vp->v_rdev) != NULL) {
1141 lwkt_gettoken(&spechash_token);
1142 SLIST_REMOVE(&dev->si_hlist, vp, vnode, v_cdevnext);
1143 vp->v_rdev = NULL;
1144 release_dev(dev);
1145 lwkt_reltoken(&spechash_token);
1150 * Add a vnode to the alias list hung off the cdev_t. We only associate
1151 * the device number with the vnode. The actual device is not associated
1152 * until the vnode is opened (usually in spec_open()), and will be
1153 * disassociated on last close.
1155 void
1156 addaliasu(struct vnode *nvp, int x, int y)
1158 if (nvp->v_type != VBLK && nvp->v_type != VCHR)
1159 panic("addaliasu on non-special vnode");
1160 nvp->v_umajor = x;
1161 nvp->v_uminor = y;
1165 * Simple call that a filesystem can make to try to get rid of a
1166 * vnode. It will fail if anyone is referencing the vnode (including
1167 * the caller).
1169 * The filesystem can check whether its in-memory inode structure still
1170 * references the vp on return.
1172 * May only be called if the vnode is in a known state (i.e. being prevented
1173 * from being deallocated by some other condition such as a vfs inode hold).
1175 void
1176 vclean_unlocked(struct vnode *vp)
1178 vx_get(vp);
1179 if (VREFCNT(vp) <= 1)
1180 vgone_vxlocked(vp);
1181 vx_put(vp);
1185 * Disassociate a vnode from its underlying filesystem.
1187 * The vnode must be VX locked and referenced. In all normal situations
1188 * there are no active references. If vclean_vxlocked() is called while
1189 * there are active references, the vnode is being ripped out and we have
1190 * to call VOP_CLOSE() as appropriate before we can reclaim it.
1192 void
1193 vclean_vxlocked(struct vnode *vp, int flags)
1195 int active;
1196 int n;
1197 vm_object_t object;
1198 struct namecache *ncp;
1201 * If the vnode has already been reclaimed we have nothing to do.
1203 if (vp->v_flag & VRECLAIMED)
1204 return;
1207 * Set flag to interlock operation, flag finalization to ensure
1208 * that the vnode winds up on the inactive list, and set v_act to 0.
1210 vsetflags(vp, VRECLAIMED);
1211 atomic_set_int(&vp->v_refcnt, VREF_FINALIZE);
1212 vp->v_act = 0;
1214 if (verbose_reclaims) {
1215 if ((ncp = TAILQ_FIRST(&vp->v_namecache)) != NULL)
1216 kprintf("Debug: reclaim %p %s\n", vp, ncp->nc_name);
1220 * Scrap the vfs cache
1222 while (cache_inval_vp(vp, 0) != 0) {
1223 kprintf("Warning: vnode %p clean/cache_resolution "
1224 "race detected\n", vp);
1225 tsleep(vp, 0, "vclninv", 2);
1229 * Check to see if the vnode is in use. If so we have to reference it
1230 * before we clean it out so that its count cannot fall to zero and
1231 * generate a race against ourselves to recycle it.
1233 active = (VREFCNT(vp) > 0);
1236 * Clean out any buffers associated with the vnode and destroy its
1237 * object, if it has one.
1239 vinvalbuf(vp, V_SAVE, 0, 0);
1240 KKASSERT(lockcountnb(&vp->v_lock) == 1);
1243 * If purging an active vnode (typically during a forced unmount
1244 * or reboot), it must be closed and deactivated before being
1245 * reclaimed. This isn't really all that safe, but what can
1246 * we do? XXX.
1248 * Note that neither of these routines unlocks the vnode.
1250 if (active && (flags & DOCLOSE)) {
1251 while ((n = vp->v_opencount) != 0) {
1252 if (vp->v_writecount)
1253 VOP_CLOSE(vp, FWRITE|FNONBLOCK, NULL);
1254 else
1255 VOP_CLOSE(vp, FNONBLOCK, NULL);
1256 if (vp->v_opencount == n) {
1257 kprintf("Warning: unable to force-close"
1258 " vnode %p\n", vp);
1259 break;
1265 * If the vnode has not been deactivated, deactivated it. Deactivation
1266 * can create new buffers and VM pages so we have to call vinvalbuf()
1267 * again to make sure they all get flushed.
1269 * This can occur if a file with a link count of 0 needs to be
1270 * truncated.
1272 * If the vnode is already dead don't try to deactivate it.
1274 if ((vp->v_flag & VINACTIVE) == 0) {
1275 vsetflags(vp, VINACTIVE);
1276 if (vp->v_mount)
1277 VOP_INACTIVE(vp);
1278 vinvalbuf(vp, V_SAVE, 0, 0);
1280 KKASSERT(lockcountnb(&vp->v_lock) == 1);
1283 * If the vnode has an object, destroy it.
1285 while ((object = vp->v_object) != NULL) {
1286 vm_object_hold(object);
1287 if (object == vp->v_object)
1288 break;
1289 vm_object_drop(object);
1292 if (object != NULL) {
1293 if (object->ref_count == 0) {
1294 if ((object->flags & OBJ_DEAD) == 0)
1295 vm_object_terminate(object);
1296 vm_object_drop(object);
1297 vclrflags(vp, VOBJBUF);
1298 } else {
1299 vm_pager_deallocate(object);
1300 vclrflags(vp, VOBJBUF);
1301 vm_object_drop(object);
1304 KKASSERT((vp->v_flag & VOBJBUF) == 0);
1307 * Reclaim the vnode if not already dead.
1309 if (vp->v_mount && VOP_RECLAIM(vp))
1310 panic("vclean: cannot reclaim");
1313 * Done with purge, notify sleepers of the grim news.
1315 vp->v_ops = &dead_vnode_vops_p;
1316 vn_gone(vp);
1317 vp->v_tag = VT_NON;
1320 * If we are destroying an active vnode, reactivate it now that
1321 * we have reassociated it with deadfs. This prevents the system
1322 * from crashing on the vnode due to it being unexpectedly marked
1323 * as inactive or reclaimed.
1325 if (active && (flags & DOCLOSE)) {
1326 vclrflags(vp, VINACTIVE | VRECLAIMED);
1331 * Eliminate all activity associated with the requested vnode
1332 * and with all vnodes aliased to the requested vnode.
1334 * The vnode must be referenced but should not be locked.
1337 vrevoke(struct vnode *vp, struct ucred *cred)
1339 struct vnode *vq;
1340 struct vnode *vqn;
1341 cdev_t dev;
1342 int error;
1345 * If the vnode has a device association, scrap all vnodes associated
1346 * with the device. Don't let the device disappear on us while we
1347 * are scrapping the vnodes.
1349 * The passed vp will probably show up in the list, do not VX lock
1350 * it twice!
1352 * Releasing the vnode's rdev here can mess up specfs's call to
1353 * device close, so don't do it. The vnode has been disassociated
1354 * and the device will be closed after the last ref on the related
1355 * fp goes away (if not still open by e.g. the kernel).
1357 if (vp->v_type != VCHR) {
1358 error = fdrevoke(vp, DTYPE_VNODE, cred);
1359 return (error);
1361 if ((dev = vp->v_rdev) == NULL) {
1362 return(0);
1364 reference_dev(dev);
1365 lwkt_gettoken(&spechash_token);
1367 restart:
1368 vqn = SLIST_FIRST(&dev->si_hlist);
1369 if (vqn)
1370 vhold(vqn);
1371 while ((vq = vqn) != NULL) {
1372 if (VREFCNT(vq) > 0) {
1373 vref(vq);
1374 fdrevoke(vq, DTYPE_VNODE, cred);
1375 /*v_release_rdev(vq);*/
1376 vrele(vq);
1377 if (vq->v_rdev != dev) {
1378 vdrop(vq);
1379 goto restart;
1382 vqn = SLIST_NEXT(vq, v_cdevnext);
1383 if (vqn)
1384 vhold(vqn);
1385 vdrop(vq);
1387 lwkt_reltoken(&spechash_token);
1388 dev_drevoke(dev);
1389 release_dev(dev);
1390 return (0);
1394 * This is called when the object underlying a vnode is being destroyed,
1395 * such as in a remove(). Try to recycle the vnode immediately if the
1396 * only active reference is our reference.
1398 * Directory vnodes in the namecache with children cannot be immediately
1399 * recycled because numerous VOP_N*() ops require them to be stable.
1401 * To avoid recursive recycling from VOP_INACTIVE implemenetations this
1402 * function is a NOP if VRECLAIMED is already set.
1405 vrecycle(struct vnode *vp)
1407 if (VREFCNT(vp) <= 1 && (vp->v_flag & VRECLAIMED) == 0) {
1408 if (cache_inval_vp_nonblock(vp))
1409 return(0);
1410 vgone_vxlocked(vp);
1411 return (1);
1413 return (0);
1417 * Return the maximum I/O size allowed for strategy calls on VP.
1419 * If vp is VCHR or VBLK we dive the device, otherwise we use
1420 * the vp's mount info.
1422 * The returned value is clamped at MAXPHYS as most callers cannot use
1423 * buffers larger than that size.
1426 vmaxiosize(struct vnode *vp)
1428 int maxiosize;
1430 if (vp->v_type == VBLK || vp->v_type == VCHR)
1431 maxiosize = vp->v_rdev->si_iosize_max;
1432 else
1433 maxiosize = vp->v_mount->mnt_iosize_max;
1435 if (maxiosize > MAXPHYS)
1436 maxiosize = MAXPHYS;
1437 return (maxiosize);
1441 * Eliminate all activity associated with a vnode in preparation for
1442 * destruction.
1444 * The vnode must be VX locked and refd and will remain VX locked and refd
1445 * on return. This routine may be called with the vnode in any state, as
1446 * long as it is VX locked. The vnode will be cleaned out and marked
1447 * VRECLAIMED but will not actually be reused until all existing refs and
1448 * holds go away.
1450 * NOTE: This routine may be called on a vnode which has not yet been
1451 * already been deactivated (VOP_INACTIVE), or on a vnode which has
1452 * already been reclaimed.
1454 * This routine is not responsible for placing us back on the freelist.
1455 * Instead, it happens automatically when the caller releases the VX lock
1456 * (assuming there aren't any other references).
1458 void
1459 vgone_vxlocked(struct vnode *vp)
1462 * assert that the VX lock is held. This is an absolute requirement
1463 * now for vgone_vxlocked() to be called.
1465 KKASSERT(lockcountnb(&vp->v_lock) == 1);
1468 * Clean out the filesystem specific data and set the VRECLAIMED
1469 * bit. Also deactivate the vnode if necessary.
1471 * The vnode should have automatically been removed from the syncer
1472 * list as syncer/dirty flags cleared during the cleaning.
1474 vclean_vxlocked(vp, DOCLOSE);
1477 * Normally panic if the vnode is still dirty, unless we are doing
1478 * a forced unmount (tmpfs typically).
1480 if (vp->v_flag & VONWORKLST) {
1481 if (vp->v_mount->mnt_kern_flag & MNTK_UNMOUNTF) {
1482 /* force removal */
1483 vn_syncer_remove(vp, 1);
1484 } else {
1485 panic("vp %p still dirty in vgone after flush", vp);
1490 * Delete from old mount point vnode list, if on one.
1492 if (vp->v_mount != NULL) {
1493 KKASSERT(vp->v_data == NULL);
1494 insmntque(vp, NULL);
1498 * If special device, remove it from special device alias list
1499 * if it is on one. This should normally only occur if a vnode is
1500 * being revoked as the device should otherwise have been released
1501 * naturally.
1503 if ((vp->v_type == VBLK || vp->v_type == VCHR) && vp->v_rdev != NULL) {
1504 v_release_rdev(vp);
1508 * Set us to VBAD
1510 vp->v_type = VBAD;
1514 * Lookup a vnode by device number.
1516 * Returns non-zero and *vpp set to a vref'd vnode on success.
1517 * Returns zero on failure.
1520 vfinddev(cdev_t dev, enum vtype type, struct vnode **vpp)
1522 struct vnode *vp;
1524 lwkt_gettoken(&spechash_token);
1525 SLIST_FOREACH(vp, &dev->si_hlist, v_cdevnext) {
1526 if (type == vp->v_type) {
1527 *vpp = vp;
1528 vref(vp);
1529 lwkt_reltoken(&spechash_token);
1530 return (1);
1533 lwkt_reltoken(&spechash_token);
1534 return (0);
1538 * Calculate the total number of references to a special device. This
1539 * routine may only be called for VBLK and VCHR vnodes since v_rdev is
1540 * an overloaded field. Since udev2dev can now return NULL, we have
1541 * to check for a NULL v_rdev.
1544 count_dev(cdev_t dev)
1546 struct vnode *vp;
1547 int count = 0;
1549 if (SLIST_FIRST(&dev->si_hlist)) {
1550 lwkt_gettoken(&spechash_token);
1551 SLIST_FOREACH(vp, &dev->si_hlist, v_cdevnext) {
1552 count += vp->v_opencount;
1554 lwkt_reltoken(&spechash_token);
1556 return(count);
1560 vcount(struct vnode *vp)
1562 if (vp->v_rdev == NULL)
1563 return(0);
1564 return(count_dev(vp->v_rdev));
1568 * Initialize VMIO for a vnode. This routine MUST be called before a
1569 * VFS can issue buffer cache ops on a vnode. It is typically called
1570 * when a vnode is initialized from its inode.
1573 vinitvmio(struct vnode *vp, off_t filesize, int blksize, int boff)
1575 vm_object_t object;
1576 int error = 0;
1578 object = vp->v_object;
1579 if (object) {
1580 vm_object_hold(object);
1581 KKASSERT(vp->v_object == object);
1584 if (object == NULL) {
1585 object = vnode_pager_alloc(vp, filesize, 0, 0, blksize, boff);
1588 * Dereference the reference we just created. This assumes
1589 * that the object is associated with the vp. Allow it to
1590 * have zero refs. It cannot be destroyed as long as it
1591 * is associated with the vnode.
1593 vm_object_hold(object);
1594 atomic_add_int(&object->ref_count, -1);
1595 vrele(vp);
1596 } else {
1597 KKASSERT((object->flags & OBJ_DEAD) == 0);
1599 KASSERT(vp->v_object != NULL, ("vinitvmio: NULL object"));
1600 vsetflags(vp, VOBJBUF);
1601 vm_object_drop(object);
1603 return (error);
1608 * Print out a description of a vnode.
1610 static char *typename[] =
1611 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD"};
1613 void
1614 vprint(char *label, struct vnode *vp)
1616 char buf[96];
1618 if (label != NULL)
1619 kprintf("%s: %p: ", label, (void *)vp);
1620 else
1621 kprintf("%p: ", (void *)vp);
1622 kprintf("type %s, refcnt %08x, writecount %d, holdcnt %d,",
1623 typename[vp->v_type],
1624 vp->v_refcnt, vp->v_writecount, vp->v_auxrefs);
1625 buf[0] = '\0';
1626 if (vp->v_flag & VROOT)
1627 strcat(buf, "|VROOT");
1628 if (vp->v_flag & VPFSROOT)
1629 strcat(buf, "|VPFSROOT");
1630 if (vp->v_flag & VTEXT)
1631 strcat(buf, "|VTEXT");
1632 if (vp->v_flag & VSYSTEM)
1633 strcat(buf, "|VSYSTEM");
1634 if (vp->v_flag & VOBJBUF)
1635 strcat(buf, "|VOBJBUF");
1636 if (buf[0] != '\0')
1637 kprintf(" flags (%s)", &buf[1]);
1638 if (vp->v_data == NULL) {
1639 kprintf("\n");
1640 } else {
1641 kprintf("\n\t");
1642 VOP_PRINT(vp);
1647 * Do the usual access checking.
1648 * file_mode, uid and gid are from the vnode in question,
1649 * while acc_mode and cred are from the VOP_ACCESS parameter list
1652 vaccess(enum vtype type, mode_t file_mode, uid_t uid, gid_t gid,
1653 mode_t acc_mode, struct ucred *cred)
1655 mode_t mask;
1656 int ismember;
1659 * Super-user always gets read/write access, but execute access depends
1660 * on at least one execute bit being set.
1662 if (priv_check_cred(cred, PRIV_ROOT, 0) == 0) {
1663 if ((acc_mode & VEXEC) && type != VDIR &&
1664 (file_mode & (S_IXUSR|S_IXGRP|S_IXOTH)) == 0)
1665 return (EACCES);
1666 return (0);
1669 mask = 0;
1671 /* Otherwise, check the owner. */
1672 if (cred->cr_uid == uid) {
1673 if (acc_mode & VEXEC)
1674 mask |= S_IXUSR;
1675 if (acc_mode & VREAD)
1676 mask |= S_IRUSR;
1677 if (acc_mode & VWRITE)
1678 mask |= S_IWUSR;
1679 return ((file_mode & mask) == mask ? 0 : EACCES);
1682 /* Otherwise, check the groups. */
1683 ismember = groupmember(gid, cred);
1684 if (cred->cr_svgid == gid || ismember) {
1685 if (acc_mode & VEXEC)
1686 mask |= S_IXGRP;
1687 if (acc_mode & VREAD)
1688 mask |= S_IRGRP;
1689 if (acc_mode & VWRITE)
1690 mask |= S_IWGRP;
1691 return ((file_mode & mask) == mask ? 0 : EACCES);
1694 /* Otherwise, check everyone else. */
1695 if (acc_mode & VEXEC)
1696 mask |= S_IXOTH;
1697 if (acc_mode & VREAD)
1698 mask |= S_IROTH;
1699 if (acc_mode & VWRITE)
1700 mask |= S_IWOTH;
1701 return ((file_mode & mask) == mask ? 0 : EACCES);
1704 #ifdef DDB
1705 #include <ddb/ddb.h>
1707 static int db_show_locked_vnodes(struct mount *mp, void *data);
1710 * List all of the locked vnodes in the system.
1711 * Called when debugging the kernel.
1713 DB_SHOW_COMMAND(lockedvnodes, lockedvnodes)
1715 kprintf("Locked vnodes\n");
1716 mountlist_scan(db_show_locked_vnodes, NULL,
1717 MNTSCAN_FORWARD|MNTSCAN_NOBUSY);
1720 static int
1721 db_show_locked_vnodes(struct mount *mp, void *data __unused)
1723 struct vnode *vp;
1725 TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
1726 if (vn_islocked(vp))
1727 vprint(NULL, vp);
1729 return(0);
1731 #endif
1734 * Top level filesystem related information gathering.
1736 static int sysctl_ovfs_conf (SYSCTL_HANDLER_ARGS);
1738 static int
1739 vfs_sysctl(SYSCTL_HANDLER_ARGS)
1741 int *name = (int *)arg1 - 1; /* XXX */
1742 u_int namelen = arg2 + 1; /* XXX */
1743 struct vfsconf *vfsp;
1744 int maxtypenum;
1746 #if 1 || defined(COMPAT_PRELITE2)
1747 /* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
1748 if (namelen == 1)
1749 return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
1750 #endif
1752 #ifdef notyet
1753 /* all sysctl names at this level are at least name and field */
1754 if (namelen < 2)
1755 return (ENOTDIR); /* overloaded */
1756 if (name[0] != VFS_GENERIC) {
1757 vfsp = vfsconf_find_by_typenum(name[0]);
1758 if (vfsp == NULL)
1759 return (EOPNOTSUPP);
1760 return ((*vfsp->vfc_vfsops->vfs_sysctl)(&name[1], namelen - 1,
1761 oldp, oldlenp, newp, newlen, p));
1763 #endif
1764 switch (name[1]) {
1765 case VFS_MAXTYPENUM:
1766 if (namelen != 2)
1767 return (ENOTDIR);
1768 maxtypenum = vfsconf_get_maxtypenum();
1769 return (SYSCTL_OUT(req, &maxtypenum, sizeof(maxtypenum)));
1770 case VFS_CONF:
1771 if (namelen != 3)
1772 return (ENOTDIR); /* overloaded */
1773 vfsp = vfsconf_find_by_typenum(name[2]);
1774 if (vfsp == NULL)
1775 return (EOPNOTSUPP);
1776 return (SYSCTL_OUT(req, vfsp, sizeof *vfsp));
1778 return (EOPNOTSUPP);
1781 SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD, vfs_sysctl,
1782 "Generic filesystem");
1784 #if 1 || defined(COMPAT_PRELITE2)
1786 static int
1787 sysctl_ovfs_conf_iter(struct vfsconf *vfsp, void *data)
1789 int error;
1790 struct ovfsconf ovfs;
1791 struct sysctl_req *req = (struct sysctl_req*) data;
1793 bzero(&ovfs, sizeof(ovfs));
1794 ovfs.vfc_vfsops = vfsp->vfc_vfsops; /* XXX used as flag */
1795 strcpy(ovfs.vfc_name, vfsp->vfc_name);
1796 ovfs.vfc_index = vfsp->vfc_typenum;
1797 ovfs.vfc_refcount = vfsp->vfc_refcount;
1798 ovfs.vfc_flags = vfsp->vfc_flags;
1799 error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
1800 if (error)
1801 return error; /* abort iteration with error code */
1802 else
1803 return 0; /* continue iterating with next element */
1806 static int
1807 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
1809 return vfsconf_each(sysctl_ovfs_conf_iter, (void*)req);
1812 #endif /* 1 || COMPAT_PRELITE2 */
1815 * Check to see if a filesystem is mounted on a block device.
1818 vfs_mountedon(struct vnode *vp)
1820 cdev_t dev;
1822 if ((dev = vp->v_rdev) == NULL) {
1823 /* if (vp->v_type != VBLK)
1824 dev = get_dev(vp->v_uminor, vp->v_umajor); */
1826 if (dev != NULL && dev->si_mountpoint)
1827 return (EBUSY);
1828 return (0);
1832 * Unmount all filesystems. The list is traversed in reverse order
1833 * of mounting to avoid dependencies.
1835 * We want the umountall to be able to break out of its loop if a
1836 * failure occurs, after scanning all possible mounts, so the callback
1837 * returns 0 on error.
1839 * NOTE: Do not call mountlist_remove(mp) on error any more, this will
1840 * confuse mountlist_scan()'s unbusy check.
1842 static int vfs_umountall_callback(struct mount *mp, void *data);
1844 void
1845 vfs_unmountall(void)
1847 int count;
1849 do {
1850 count = mountlist_scan(vfs_umountall_callback,
1851 NULL, MNTSCAN_REVERSE|MNTSCAN_NOBUSY);
1852 } while (count);
1855 static
1857 vfs_umountall_callback(struct mount *mp, void *data)
1859 int error;
1861 error = dounmount(mp, MNT_FORCE);
1862 if (error) {
1863 kprintf("unmount of filesystem mounted from %s failed (",
1864 mp->mnt_stat.f_mntfromname);
1865 if (error == EBUSY)
1866 kprintf("BUSY)\n");
1867 else
1868 kprintf("%d)\n", error);
1869 return 0;
1870 } else {
1871 return 1;
1876 * Checks the mount flags for parameter mp and put the names comma-separated
1877 * into a string buffer buf with a size limit specified by len.
1879 * It returns the number of bytes written into buf, and (*errorp) will be
1880 * set to 0, EINVAL (if passed length is 0), or ENOSPC (supplied buffer was
1881 * not large enough). The buffer will be 0-terminated if len was not 0.
1883 size_t
1884 vfs_flagstostr(int flags, const struct mountctl_opt *optp,
1885 char *buf, size_t len, int *errorp)
1887 static const struct mountctl_opt optnames[] = {
1888 { MNT_RDONLY, "read-only" },
1889 { MNT_SYNCHRONOUS, "synchronous" },
1890 { MNT_NOEXEC, "noexec" },
1891 { MNT_NOSUID, "nosuid" },
1892 { MNT_NODEV, "nodev" },
1893 { MNT_AUTOMOUNTED, "automounted" },
1894 { MNT_ASYNC, "asynchronous" },
1895 { MNT_SUIDDIR, "suiddir" },
1896 { MNT_SOFTDEP, "soft-updates" },
1897 { MNT_NOSYMFOLLOW, "nosymfollow" },
1898 { MNT_TRIM, "trim" },
1899 { MNT_NOATIME, "noatime" },
1900 { MNT_NOCLUSTERR, "noclusterr" },
1901 { MNT_NOCLUSTERW, "noclusterw" },
1902 { MNT_EXRDONLY, "NFS read-only" },
1903 { MNT_EXPORTED, "NFS exported" },
1904 /* Remaining NFS flags could come here */
1905 { MNT_LOCAL, "local" },
1906 { MNT_QUOTA, "with-quotas" },
1907 /* { MNT_ROOTFS, "rootfs" }, */
1908 /* { MNT_IGNORE, "ignore" }, */
1909 { 0, NULL}
1911 int bwritten;
1912 int bleft;
1913 int optlen;
1914 int actsize;
1916 *errorp = 0;
1917 bwritten = 0;
1918 bleft = len - 1; /* leave room for trailing \0 */
1921 * Checks the size of the string. If it contains
1922 * any data, then we will append the new flags to
1923 * it.
1925 actsize = strlen(buf);
1926 if (actsize > 0)
1927 buf += actsize;
1929 /* Default flags if no flags passed */
1930 if (optp == NULL)
1931 optp = optnames;
1933 if (bleft < 0) { /* degenerate case, 0-length buffer */
1934 *errorp = EINVAL;
1935 return(0);
1938 for (; flags && optp->o_opt; ++optp) {
1939 if ((flags & optp->o_opt) == 0)
1940 continue;
1941 optlen = strlen(optp->o_name);
1942 if (bwritten || actsize > 0) {
1943 if (bleft < 2) {
1944 *errorp = ENOSPC;
1945 break;
1947 buf[bwritten++] = ',';
1948 buf[bwritten++] = ' ';
1949 bleft -= 2;
1951 if (bleft < optlen) {
1952 *errorp = ENOSPC;
1953 break;
1955 bcopy(optp->o_name, buf + bwritten, optlen);
1956 bwritten += optlen;
1957 bleft -= optlen;
1958 flags &= ~optp->o_opt;
1962 * Space already reserved for trailing \0
1964 buf[bwritten] = 0;
1965 return (bwritten);
1969 * Build hash lists of net addresses and hang them off the mount point.
1970 * Called by ufs_mount() to set up the lists of export addresses.
1972 static int
1973 vfs_hang_addrlist(struct mount *mp, struct netexport *nep,
1974 const struct export_args *argp)
1976 struct netcred *np;
1977 struct radix_node_head *rnh;
1978 int i;
1979 struct radix_node *rn;
1980 struct sockaddr *saddr, *smask = NULL;
1981 int error;
1983 if (argp->ex_addrlen == 0) {
1984 if (mp->mnt_flag & MNT_DEFEXPORTED)
1985 return (EPERM);
1986 np = &nep->ne_defexported;
1987 np->netc_exflags = argp->ex_flags;
1988 np->netc_anon = argp->ex_anon;
1989 np->netc_anon.cr_ref = 1;
1990 mp->mnt_flag |= MNT_DEFEXPORTED;
1991 return (0);
1994 if (argp->ex_addrlen < 0 || argp->ex_addrlen > MLEN)
1995 return (EINVAL);
1996 if (argp->ex_masklen < 0 || argp->ex_masklen > MLEN)
1997 return (EINVAL);
1999 i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
2000 np = (struct netcred *) kmalloc(i, M_NETADDR, M_WAITOK | M_ZERO);
2001 saddr = (struct sockaddr *) (np + 1);
2002 if ((error = copyin(argp->ex_addr, (caddr_t) saddr, argp->ex_addrlen)))
2003 goto out;
2004 if (saddr->sa_len > argp->ex_addrlen)
2005 saddr->sa_len = argp->ex_addrlen;
2006 if (argp->ex_masklen) {
2007 smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen);
2008 error = copyin(argp->ex_mask, (caddr_t)smask, argp->ex_masklen);
2009 if (error)
2010 goto out;
2011 if (smask->sa_len > argp->ex_masklen)
2012 smask->sa_len = argp->ex_masklen;
2014 NE_LOCK(nep);
2015 if (nep->ne_maskhead == NULL) {
2016 if (!rn_inithead((void **)&nep->ne_maskhead, NULL, 0)) {
2017 error = ENOBUFS;
2018 goto out;
2021 if((rnh = vfs_create_addrlist_af(saddr->sa_family, nep)) == NULL) {
2022 error = ENOBUFS;
2023 goto out;
2025 rn = (*rnh->rnh_addaddr) ((char *) saddr, (char *) smask, rnh,
2026 np->netc_rnodes);
2027 NE_UNLOCK(nep);
2028 if (rn == NULL || np != (struct netcred *) rn) { /* already exists */
2029 error = EPERM;
2030 goto out;
2032 np->netc_exflags = argp->ex_flags;
2033 np->netc_anon = argp->ex_anon;
2034 np->netc_anon.cr_ref = 1;
2035 return (0);
2036 out:
2037 kfree(np, M_NETADDR);
2038 return (error);
2041 /* ARGSUSED */
2042 static int
2043 vfs_free_netcred(struct radix_node *rn, void *w)
2045 struct radix_node_head *rnh = (struct radix_node_head *) w;
2047 (*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, rnh);
2048 kfree((caddr_t) rn, M_NETADDR);
2049 return (0);
2052 static struct radix_node_head *
2053 vfs_create_addrlist_af(int af, struct netexport *nep)
2055 struct radix_node_head *rnh = NULL;
2056 #if defined(INET) || defined(INET6)
2057 struct radix_node_head *maskhead = nep->ne_maskhead;
2058 int off;
2059 #endif
2061 NE_ASSERT_LOCKED(nep);
2062 KKASSERT(maskhead != NULL);
2063 switch (af) {
2064 #ifdef INET
2065 case AF_INET:
2066 if ((rnh = nep->ne_inethead) == NULL) {
2067 off = offsetof(struct sockaddr_in, sin_addr) << 3;
2068 if (!rn_inithead((void **)&rnh, maskhead, off))
2069 return (NULL);
2070 nep->ne_inethead = rnh;
2072 break;
2073 #endif
2074 #ifdef INET6
2075 case AF_INET6:
2076 if ((rnh = nep->ne_inet6head) == NULL) {
2077 off = offsetof(struct sockaddr_in6, sin6_addr) << 3;
2078 if (!rn_inithead((void **)&rnh, maskhead, off))
2079 return (NULL);
2080 nep->ne_inet6head = rnh;
2082 break;
2083 #endif
2085 return (rnh);
2088 static void
2089 vfs_free_addrlist_af(struct radix_node_head **prnh)
2091 struct radix_node_head *rnh = *prnh;
2093 (*rnh->rnh_walktree) (rnh, vfs_free_netcred, rnh);
2094 kfree(rnh, M_RTABLE);
2095 *prnh = NULL;
2099 * Free the net address hash lists that are hanging off the mount points.
2101 static void
2102 vfs_free_addrlist(struct netexport *nep)
2104 NE_LOCK(nep);
2105 if (nep->ne_inethead != NULL)
2106 vfs_free_addrlist_af(&nep->ne_inethead);
2107 if (nep->ne_inet6head != NULL)
2108 vfs_free_addrlist_af(&nep->ne_inet6head);
2109 if (nep->ne_maskhead)
2110 vfs_free_addrlist_af(&nep->ne_maskhead);
2111 NE_UNLOCK(nep);
2115 vfs_export(struct mount *mp, struct netexport *nep,
2116 const struct export_args *argp)
2118 int error;
2120 if (argp->ex_flags & MNT_DELEXPORT) {
2121 if (mp->mnt_flag & MNT_EXPUBLIC) {
2122 vfs_setpublicfs(NULL, NULL, NULL);
2123 mp->mnt_flag &= ~MNT_EXPUBLIC;
2125 vfs_free_addrlist(nep);
2126 mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
2128 if (argp->ex_flags & MNT_EXPORTED) {
2129 if (argp->ex_flags & MNT_EXPUBLIC) {
2130 if ((error = vfs_setpublicfs(mp, nep, argp)) != 0)
2131 return (error);
2132 mp->mnt_flag |= MNT_EXPUBLIC;
2134 if ((error = vfs_hang_addrlist(mp, nep, argp)))
2135 return (error);
2136 mp->mnt_flag |= MNT_EXPORTED;
2138 return (0);
2143 * Set the publicly exported filesystem (WebNFS). Currently, only
2144 * one public filesystem is possible in the spec (RFC 2054 and 2055)
2147 vfs_setpublicfs(struct mount *mp, struct netexport *nep,
2148 const struct export_args *argp)
2150 int error;
2151 struct vnode *rvp;
2152 char *cp;
2155 * mp == NULL -> invalidate the current info, the FS is
2156 * no longer exported. May be called from either vfs_export
2157 * or unmount, so check if it hasn't already been done.
2159 if (mp == NULL) {
2160 if (nfs_pub.np_valid) {
2161 nfs_pub.np_valid = 0;
2162 if (nfs_pub.np_index != NULL) {
2163 kfree(nfs_pub.np_index, M_TEMP);
2164 nfs_pub.np_index = NULL;
2167 return (0);
2171 * Only one allowed at a time.
2173 if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
2174 return (EBUSY);
2177 * Get real filehandle for root of exported FS.
2179 bzero((caddr_t)&nfs_pub.np_handle, sizeof(nfs_pub.np_handle));
2180 nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid;
2182 if ((error = VFS_ROOT(mp, &rvp)))
2183 return (error);
2185 if ((error = VFS_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid)))
2186 return (error);
2188 vput(rvp);
2191 * If an indexfile was specified, pull it in.
2193 if (argp->ex_indexfile != NULL) {
2194 int namelen;
2196 error = vn_get_namelen(rvp, &namelen);
2197 if (error)
2198 return (error);
2199 nfs_pub.np_index = kmalloc(namelen, M_TEMP, M_WAITOK);
2200 error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
2201 namelen, NULL);
2202 if (!error) {
2204 * Check for illegal filenames.
2206 for (cp = nfs_pub.np_index; *cp; cp++) {
2207 if (*cp == '/') {
2208 error = EINVAL;
2209 break;
2213 if (error) {
2214 kfree(nfs_pub.np_index, M_TEMP);
2215 return (error);
2219 nfs_pub.np_mount = mp;
2220 nfs_pub.np_valid = 1;
2221 return (0);
2224 struct netcred *
2225 vfs_export_lookup(struct mount *mp, struct netexport *nep,
2226 struct sockaddr *nam)
2228 struct netcred *np;
2229 struct radix_node_head *rnh;
2230 struct sockaddr *saddr;
2232 np = NULL;
2233 if (mp->mnt_flag & MNT_EXPORTED) {
2235 * Lookup in the export list first.
2237 NE_LOCK(nep);
2238 if (nam != NULL) {
2239 saddr = nam;
2240 switch (saddr->sa_family) {
2241 #ifdef INET
2242 case AF_INET:
2243 rnh = nep->ne_inethead;
2244 break;
2245 #endif
2246 #ifdef INET6
2247 case AF_INET6:
2248 rnh = nep->ne_inet6head;
2249 break;
2250 #endif
2251 default:
2252 rnh = NULL;
2254 if (rnh != NULL) {
2255 np = (struct netcred *)
2256 (*rnh->rnh_matchaddr)((char *)saddr,
2257 rnh);
2258 if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
2259 np = NULL;
2262 NE_UNLOCK(nep);
2264 * If no address match, use the default if it exists.
2266 if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
2267 np = &nep->ne_defexported;
2269 return (np);
2273 * perform msync on all vnodes under a mount point. The mount point must
2274 * be locked. This code is also responsible for lazy-freeing unreferenced
2275 * vnodes whos VM objects no longer contain pages.
2277 * NOTE: MNT_WAIT still skips vnodes in the VXLOCK state.
2279 * NOTE: XXX VOP_PUTPAGES and friends requires that the vnode be locked,
2280 * but vnode_pager_putpages() doesn't lock the vnode. We have to do it
2281 * way up in this high level function.
2283 static int vfs_msync_scan1(struct mount *mp, struct vnode *vp, void *data);
2284 static int vfs_msync_scan2(struct mount *mp, struct vnode *vp, void *data);
2286 void
2287 vfs_msync(struct mount *mp, int flags)
2289 int vmsc_flags;
2292 * tmpfs sets this flag to prevent msync(), sync, and the
2293 * filesystem periodic syncer from trying to flush VM pages
2294 * to swap. Only pure memory pressure flushes tmpfs VM pages
2295 * to swap.
2297 if (mp->mnt_kern_flag & MNTK_NOMSYNC)
2298 return;
2301 * Ok, scan the vnodes for work. If the filesystem is using the
2302 * syncer thread feature we can use vsyncscan() instead of
2303 * vmntvnodescan(), which is much faster.
2305 vmsc_flags = VMSC_GETVP;
2306 if (flags != MNT_WAIT)
2307 vmsc_flags |= VMSC_NOWAIT;
2309 if (mp->mnt_kern_flag & MNTK_THR_SYNC) {
2310 vsyncscan(mp, vmsc_flags, vfs_msync_scan2,
2311 (void *)(intptr_t)flags);
2312 } else {
2313 vmntvnodescan(mp, vmsc_flags,
2314 vfs_msync_scan1, vfs_msync_scan2,
2315 (void *)(intptr_t)flags);
2320 * scan1 is a fast pre-check. There could be hundreds of thousands of
2321 * vnodes, we cannot afford to do anything heavy weight until we have a
2322 * fairly good indication that there is work to do.
2324 static
2326 vfs_msync_scan1(struct mount *mp, struct vnode *vp, void *data)
2328 int flags = (int)(intptr_t)data;
2330 if ((vp->v_flag & VRECLAIMED) == 0) {
2331 if (vp->v_auxrefs == 0 && VREFCNT(vp) <= 0 &&
2332 vp->v_object) {
2333 return(0); /* call scan2 */
2335 if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
2336 (vp->v_flag & VOBJDIRTY) &&
2337 (flags == MNT_WAIT || vn_islocked(vp) == 0)) {
2338 return(0); /* call scan2 */
2343 * do not call scan2, continue the loop
2345 return(-1);
2349 * This callback is handed a locked vnode.
2351 static
2353 vfs_msync_scan2(struct mount *mp, struct vnode *vp, void *data)
2355 vm_object_t obj;
2356 int flags = (int)(intptr_t)data;
2358 if (vp->v_flag & VRECLAIMED)
2359 return(0);
2361 if ((mp->mnt_flag & MNT_RDONLY) == 0 && (vp->v_flag & VOBJDIRTY)) {
2362 if ((obj = vp->v_object) != NULL) {
2363 vm_object_page_clean(obj, 0, 0,
2364 flags == MNT_WAIT ? OBJPC_SYNC : OBJPC_NOSYNC);
2367 return(0);
2371 * Wake up anyone interested in vp because it is being revoked.
2373 void
2374 vn_gone(struct vnode *vp)
2376 lwkt_gettoken(&vp->v_token);
2377 KNOTE(&vp->v_pollinfo.vpi_kqinfo.ki_note, NOTE_REVOKE);
2378 lwkt_reltoken(&vp->v_token);
2382 * extract the cdev_t from a VBLK or VCHR. The vnode must have been opened
2383 * (or v_rdev might be NULL).
2385 cdev_t
2386 vn_todev(struct vnode *vp)
2388 if (vp->v_type != VBLK && vp->v_type != VCHR)
2389 return (NULL);
2390 KKASSERT(vp->v_rdev != NULL);
2391 return (vp->v_rdev);
2395 * Check if vnode represents a disk device. The vnode does not need to be
2396 * opened.
2398 * MPALMOSTSAFE
2401 vn_isdisk(struct vnode *vp, int *errp)
2403 cdev_t dev;
2405 if (vp->v_type != VCHR) {
2406 if (errp != NULL)
2407 *errp = ENOTBLK;
2408 return (0);
2411 dev = vp->v_rdev;
2413 if (dev == NULL) {
2414 if (errp != NULL)
2415 *errp = ENXIO;
2416 return (0);
2418 if (dev_is_good(dev) == 0) {
2419 if (errp != NULL)
2420 *errp = ENXIO;
2421 return (0);
2423 if ((dev_dflags(dev) & D_DISK) == 0) {
2424 if (errp != NULL)
2425 *errp = ENOTBLK;
2426 return (0);
2428 if (errp != NULL)
2429 *errp = 0;
2430 return (1);
2434 vn_get_namelen(struct vnode *vp, int *namelen)
2436 int error;
2437 register_t retval[2];
2439 error = VOP_PATHCONF(vp, _PC_NAME_MAX, retval);
2440 if (error)
2441 return (error);
2442 *namelen = (int)retval[0];
2443 return (0);
2447 vop_write_dirent(int *error, struct uio *uio, ino_t d_ino, uint8_t d_type,
2448 uint16_t d_namlen, const char *d_name)
2450 struct dirent *dp;
2451 size_t len;
2453 len = _DIRENT_RECLEN(d_namlen);
2454 if (len > uio->uio_resid)
2455 return(1);
2457 dp = kmalloc(len, M_TEMP, M_WAITOK | M_ZERO);
2459 dp->d_ino = d_ino;
2460 dp->d_namlen = d_namlen;
2461 dp->d_type = d_type;
2462 bcopy(d_name, dp->d_name, d_namlen);
2464 *error = uiomove((caddr_t)dp, len, uio);
2466 kfree(dp, M_TEMP);
2468 return(0);
2471 void
2472 vn_mark_atime(struct vnode *vp, struct thread *td)
2474 struct proc *p = td->td_proc;
2475 struct ucred *cred = p ? p->p_ucred : proc0.p_ucred;
2477 if ((vp->v_mount->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0) {
2478 VOP_MARKATIME(vp, cred);
2483 * Calculate the number of entries in an inode-related chained hash table.
2484 * With today's memory sizes, maxvnodes can wind up being a very large
2485 * number. There is no reason to waste memory, so tolerate some stacking.
2488 vfs_inodehashsize(void)
2490 int hsize;
2492 hsize = 32;
2493 while (hsize < maxvnodes)
2494 hsize <<= 1;
2495 while (hsize > maxvnodes * 2)
2496 hsize >>= 1; /* nominal 2x stacking */
2498 if (maxvnodes > 1024 * 1024)
2499 hsize >>= 1; /* nominal 8x stacking */
2501 if (maxvnodes > 128 * 1024)
2502 hsize >>= 1; /* nominal 4x stacking */
2504 if (hsize < 16)
2505 hsize = 16;
2507 return hsize;