xref: /dragonfly/sys/kern/vfs_subr.c (revision 493fd20c)
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.
9  *
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.
21  *
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.
33  *
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 $
36  */
37 
38 /*
39  * External virtual filesystem routines
40  */
41 #include "opt_ddb.h"
42 #include "opt_inet.h"
43 #include "opt_inet6.h"
44 
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>
68 
69 #include <machine/limits.h>
70 
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>
81 
82 #include <sys/buf2.h>
83 #include <sys/thread2.h>
84 #include <sys/mplock2.h>
85 #include <vm/vm_page2.h>
86 
87 #include <netinet/in.h>
88 
89 static MALLOC_DEFINE(M_NETCRED, "Export Host", "Export host address structure");
90 
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)");
97 
98 enum vtype iftovt_tab[16] = {
99 	VNON, VFIFO, VCHR, VNON, VDIR, VNON, VBLK, VNON,
100 	VREG, VNON, VLNK, VNON, VSOCK, VNON, VNON, VBAD,
101 };
102 int vttoif_tab[9] = {
103 	0, S_IFREG, S_IFDIR, S_IFBLK, S_IFCHR, S_IFLNK,
104 	S_IFSOCK, S_IFIFO, S_IFMT,
105 };
106 
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");
110 
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");
114 
115 int	nfs_mount_type = -1;
116 static struct lwkt_token spechash_token;
117 struct nfs_public nfs_pub;	/* publicly exported FS */
118 
119 int maxvnodes;
120 SYSCTL_INT(_kern, KERN_MAXVNODES, maxvnodes, CTLFLAG_RW,
121 	   &maxvnodes, 0, "Maximum number of vnodes");
122 
123 static struct radix_node_head *vfs_create_addrlist_af(int af,
124 		    struct netexport *nep);
125 static void	vfs_free_addrlist (struct netexport *nep);
126 static int	vfs_free_netcred (struct radix_node *rn, void *w);
127 static void	vfs_free_addrlist_af (struct radix_node_head **prnh);
128 static int	vfs_hang_addrlist (struct mount *mp, struct netexport *nep,
129 	            const struct export_args *argp);
130 
131 int	prtactive = 0;		/* 1 => print out reclaim of active vnodes */
132 
133 /*
134  * Red black tree functions
135  */
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);
139 
140 static int
141 rb_buf_compare(struct buf *b1, struct buf *b2)
142 {
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);
148 }
149 
150 /*
151  * Initialize the vnode management data structures.
152  *
153  * Called from vfsinit()
154  */
155 void
156 vfs_subr_init(void)
157 {
158 	int factor1;
159 	int factor2;
160 
161 	/*
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.
165 	 *
166 	 * WARNING!  For machines with 64-256M of ram we have to be sure
167 	 *	     that the default limit scales down well due to HAMMER
168 	 *	     taking up significantly more memory per-vnode vs UFS.
169 	 *	     We want around ~5800 on a 128M machine.
170 	 *
171 	 * WARNING!  Now that KVM is substantially larger (e.g. 8TB+),
172 	 *	     also limit maxvnodes based on a 128GB metric.  This
173 	 *	     gives us something like ~3 millon vnodes.  sysctl
174 	 *	     can be used to increase it further if desired.
175 	 *
176 	 *	     For disk cachhing purposes, filesystems like HAMMER1
177 	 *	     and HAMMER2 will or can be told to cache file data
178 	 *	     via the block device instead of excessively in vnodes.
179 	 */
180 	factor1 = 25 * (sizeof(struct vm_object) + sizeof(struct vnode));
181 	factor2 = 30 * (sizeof(struct vm_object) + sizeof(struct vnode));
182 	maxvnodes = imin((int64_t)vmstats.v_page_count * PAGE_SIZE / factor1,
183 			 KvaSize / factor2);
184 	maxvnodes = imax(maxvnodes, maxproc * 8);
185 	maxvnodes = imin(maxvnodes, 64LL*1024*1024*1024 / factor2);
186 
187 	lwkt_token_init(&spechash_token, "spechash");
188 }
189 
190 /*
191  * Knob to control the precision of file timestamps:
192  *
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.
197  */
198 enum { TSP_SEC, TSP_HZ, TSP_USEC, TSP_NSEC };
199 
200 static int timestamp_precision = TSP_SEC;
201 SYSCTL_INT(_vfs, OID_AUTO, timestamp_precision, CTLFLAG_RW,
202 		&timestamp_precision, 0, "Precision of file timestamps");
203 
204 /*
205  * Get a current timestamp.
206  *
207  * MPSAFE
208  */
209 void
210 vfs_timestamp(struct timespec *tsp)
211 {
212 	struct timeval tv;
213 
214 	switch (timestamp_precision) {
215 	case TSP_SEC:
216 		tsp->tv_sec = time_second;
217 		tsp->tv_nsec = 0;
218 		break;
219 	case TSP_HZ:
220 		getnanotime(tsp);
221 		break;
222 	case TSP_USEC:
223 		microtime(&tv);
224 		TIMEVAL_TO_TIMESPEC(&tv, tsp);
225 		break;
226 	case TSP_NSEC:
227 	default:
228 		nanotime(tsp);
229 		break;
230 	}
231 }
232 
233 /*
234  * Set vnode attributes to VNOVAL
235  */
236 void
237 vattr_null(struct vattr *vap)
238 {
239 	vap->va_type = VNON;
240 	vap->va_size = VNOVAL;
241 	vap->va_bytes = VNOVAL;
242 	vap->va_mode = VNOVAL;
243 	vap->va_nlink = VNOVAL;
244 	vap->va_uid = VNOVAL;
245 	vap->va_gid = VNOVAL;
246 	vap->va_fsid = VNOVAL;
247 	vap->va_fileid = VNOVAL;
248 	vap->va_blocksize = VNOVAL;
249 	vap->va_rmajor = VNOVAL;
250 	vap->va_rminor = VNOVAL;
251 	vap->va_atime.tv_sec = VNOVAL;
252 	vap->va_atime.tv_nsec = VNOVAL;
253 	vap->va_mtime.tv_sec = VNOVAL;
254 	vap->va_mtime.tv_nsec = VNOVAL;
255 	vap->va_ctime.tv_sec = VNOVAL;
256 	vap->va_ctime.tv_nsec = VNOVAL;
257 	vap->va_flags = VNOVAL;
258 	vap->va_gen = VNOVAL;
259 	vap->va_vaflags = 0;
260 	/* va_*_uuid fields are only valid if related flags are set */
261 }
262 
263 /*
264  * Flush out and invalidate all buffers associated with a vnode.
265  *
266  * vp must be locked.
267  */
268 static int vinvalbuf_bp(struct buf *bp, void *data);
269 
270 struct vinvalbuf_bp_info {
271 	struct vnode *vp;
272 	int slptimeo;
273 	int lkflags;
274 	int flags;
275 	int clean;
276 };
277 
278 int
279 vinvalbuf(struct vnode *vp, int flags, int slpflag, int slptimeo)
280 {
281 	struct vinvalbuf_bp_info info;
282 	vm_object_t object;
283 	int error;
284 
285 	lwkt_gettoken(&vp->v_token);
286 
287 	/*
288 	 * If we are being asked to save, call fsync to ensure that the inode
289 	 * is updated.
290 	 */
291 	if (flags & V_SAVE) {
292 		error = bio_track_wait(&vp->v_track_write, slpflag, slptimeo);
293 		if (error)
294 			goto done;
295 		if (!RB_EMPTY(&vp->v_rbdirty_tree)) {
296 			if ((error = VOP_FSYNC(vp, MNT_WAIT, 0)) != 0)
297 				goto done;
298 #if 0
299 			/*
300 			 * Dirty bufs may be left or generated via races
301 			 * in circumstances where vinvalbuf() is called on
302 			 * a vnode not undergoing reclamation.   Only
303 			 * panic if we are trying to reclaim the vnode.
304 			 */
305 			if ((vp->v_flag & VRECLAIMED) &&
306 			    (bio_track_active(&vp->v_track_write) ||
307 			    !RB_EMPTY(&vp->v_rbdirty_tree))) {
308 				panic("vinvalbuf: dirty bufs");
309 			}
310 #endif
311 		}
312   	}
313 	info.slptimeo = slptimeo;
314 	info.lkflags = LK_EXCLUSIVE | LK_SLEEPFAIL;
315 	if (slpflag & PCATCH)
316 		info.lkflags |= LK_PCATCH;
317 	info.flags = flags;
318 	info.vp = vp;
319 
320 	/*
321 	 * Flush the buffer cache until nothing is left, wait for all I/O
322 	 * to complete.  At least one pass is required.  We might block
323 	 * in the pip code so we have to re-check.  Order is important.
324 	 */
325 	do {
326 		/*
327 		 * Flush buffer cache
328 		 */
329 		if (!RB_EMPTY(&vp->v_rbclean_tree)) {
330 			info.clean = 1;
331 			error = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree,
332 					NULL, vinvalbuf_bp, &info);
333 		}
334 		if (!RB_EMPTY(&vp->v_rbdirty_tree)) {
335 			info.clean = 0;
336 			error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
337 					NULL, vinvalbuf_bp, &info);
338 		}
339 
340 		/*
341 		 * Wait for I/O completion.
342 		 */
343 		bio_track_wait(&vp->v_track_write, 0, 0);
344 		if ((object = vp->v_object) != NULL)
345 			refcount_wait(&object->paging_in_progress, "vnvlbx");
346 	} while (bio_track_active(&vp->v_track_write) ||
347 		 !RB_EMPTY(&vp->v_rbclean_tree) ||
348 		 !RB_EMPTY(&vp->v_rbdirty_tree));
349 
350 	/*
351 	 * Destroy the copy in the VM cache, too.
352 	 */
353 	if ((object = vp->v_object) != NULL) {
354 		vm_object_page_remove(object, 0, 0,
355 			(flags & V_SAVE) ? TRUE : FALSE);
356 	}
357 
358 	if (!RB_EMPTY(&vp->v_rbdirty_tree) || !RB_EMPTY(&vp->v_rbclean_tree))
359 		panic("vinvalbuf: flush failed");
360 	if (!RB_EMPTY(&vp->v_rbhash_tree))
361 		panic("vinvalbuf: flush failed, buffers still present");
362 	error = 0;
363 done:
364 	lwkt_reltoken(&vp->v_token);
365 	return (error);
366 }
367 
368 static int
369 vinvalbuf_bp(struct buf *bp, void *data)
370 {
371 	struct vinvalbuf_bp_info *info = data;
372 	int error;
373 
374 	if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
375 		atomic_add_int(&bp->b_refs, 1);
376 		error = BUF_TIMELOCK(bp, info->lkflags,
377 				     "vinvalbuf", info->slptimeo);
378 		atomic_subtract_int(&bp->b_refs, 1);
379 		if (error == 0) {
380 			BUF_UNLOCK(bp);
381 			error = ENOLCK;
382 		}
383 		if (error == ENOLCK)
384 			return(0);
385 		return (-error);
386 	}
387 	KKASSERT(bp->b_vp == info->vp);
388 
389 	/*
390 	 * Must check clean/dirty status after successfully locking as
391 	 * it may race.
392 	 */
393 	if ((info->clean && (bp->b_flags & B_DELWRI)) ||
394 	    (info->clean == 0 && (bp->b_flags & B_DELWRI) == 0)) {
395 		BUF_UNLOCK(bp);
396 		return(0);
397 	}
398 
399 	/*
400 	 * NOTE:  NO B_LOCKED CHECK.  Also no buf_checkwrite()
401 	 * check.  This code will write out the buffer, period.
402 	 */
403 	bremfree(bp);
404 	if (((bp->b_flags & (B_DELWRI | B_INVAL)) == B_DELWRI) &&
405 	    (info->flags & V_SAVE)) {
406 		cluster_awrite(bp);
407 	} else if (info->flags & V_SAVE) {
408 		/*
409 		 * Cannot set B_NOCACHE on a clean buffer as this will
410 		 * destroy the VM backing store which might actually
411 		 * be dirty (and unsynchronized).
412 		 */
413 		bp->b_flags |= (B_INVAL | B_RELBUF);
414 		brelse(bp);
415 	} else {
416 		bp->b_flags |= (B_INVAL | B_NOCACHE | B_RELBUF);
417 		brelse(bp);
418 	}
419 	return(0);
420 }
421 
422 /*
423  * Truncate a file's buffer and pages to a specified length.  This
424  * is in lieu of the old vinvalbuf mechanism, which performed unneeded
425  * sync activity.
426  *
427  * The vnode must be locked.
428  */
429 static int vtruncbuf_bp_trunc_cmp(struct buf *bp, void *data);
430 static int vtruncbuf_bp_trunc(struct buf *bp, void *data);
431 static int vtruncbuf_bp_metasync_cmp(struct buf *bp, void *data);
432 static int vtruncbuf_bp_metasync(struct buf *bp, void *data);
433 
434 struct vtruncbuf_info {
435 	struct vnode *vp;
436 	off_t	truncloffset;
437 	int	clean;
438 };
439 
440 int
441 vtruncbuf(struct vnode *vp, off_t length, int blksize)
442 {
443 	struct vtruncbuf_info info;
444 	const char *filename;
445 	int count;
446 
447 	/*
448 	 * Round up to the *next* block, then destroy the buffers in question.
449 	 * Since we are only removing some of the buffers we must rely on the
450 	 * scan count to determine whether a loop is necessary.
451 	 */
452 	if ((count = (int)(length % blksize)) != 0)
453 		info.truncloffset = length + (blksize - count);
454 	else
455 		info.truncloffset = length;
456 	info.vp = vp;
457 
458 	lwkt_gettoken(&vp->v_token);
459 	do {
460 		info.clean = 1;
461 		count = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree,
462 				vtruncbuf_bp_trunc_cmp,
463 				vtruncbuf_bp_trunc, &info);
464 		info.clean = 0;
465 		count += RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
466 				vtruncbuf_bp_trunc_cmp,
467 				vtruncbuf_bp_trunc, &info);
468 	} while(count);
469 
470 	/*
471 	 * For safety, fsync any remaining metadata if the file is not being
472 	 * truncated to 0.  Since the metadata does not represent the entire
473 	 * dirty list we have to rely on the hit count to ensure that we get
474 	 * all of it.
475 	 */
476 	if (length > 0) {
477 		do {
478 			count = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
479 					vtruncbuf_bp_metasync_cmp,
480 					vtruncbuf_bp_metasync, &info);
481 		} while (count);
482 	}
483 
484 	/*
485 	 * Clean out any left over VM backing store.
486 	 *
487 	 * It is possible to have in-progress I/O from buffers that were
488 	 * not part of the truncation.  This should not happen if we
489 	 * are truncating to 0-length.
490 	 */
491 	vnode_pager_setsize(vp, length);
492 	bio_track_wait(&vp->v_track_write, 0, 0);
493 
494 	/*
495 	 * Debugging only
496 	 */
497 	spin_lock(&vp->v_spin);
498 	filename = TAILQ_FIRST(&vp->v_namecache) ?
499 		   TAILQ_FIRST(&vp->v_namecache)->nc_name : "?";
500 	spin_unlock(&vp->v_spin);
501 
502 	/*
503 	 * Make sure no buffers were instantiated while we were trying
504 	 * to clean out the remaining VM pages.  This could occur due
505 	 * to busy dirty VM pages being flushed out to disk.
506 	 */
507 	do {
508 		info.clean = 1;
509 		count = RB_SCAN(buf_rb_tree, &vp->v_rbclean_tree,
510 				vtruncbuf_bp_trunc_cmp,
511 				vtruncbuf_bp_trunc, &info);
512 		info.clean = 0;
513 		count += RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
514 				vtruncbuf_bp_trunc_cmp,
515 				vtruncbuf_bp_trunc, &info);
516 		if (count) {
517 			kprintf("Warning: vtruncbuf():  Had to re-clean %d "
518 			       "left over buffers in %s\n", count, filename);
519 		}
520 	} while(count);
521 
522 	lwkt_reltoken(&vp->v_token);
523 
524 	return (0);
525 }
526 
527 /*
528  * The callback buffer is beyond the new file EOF and must be destroyed.
529  * Note that the compare function must conform to the RB_SCAN's requirements.
530  */
531 static
532 int
533 vtruncbuf_bp_trunc_cmp(struct buf *bp, void *data)
534 {
535 	struct vtruncbuf_info *info = data;
536 
537 	if (bp->b_loffset >= info->truncloffset)
538 		return(0);
539 	return(-1);
540 }
541 
542 static
543 int
544 vtruncbuf_bp_trunc(struct buf *bp, void *data)
545 {
546 	struct vtruncbuf_info *info = data;
547 
548 	/*
549 	 * Do not try to use a buffer we cannot immediately lock, but sleep
550 	 * anyway to prevent a livelock.  The code will loop until all buffers
551 	 * can be acted upon.
552 	 *
553 	 * We must always revalidate the buffer after locking it to deal
554 	 * with MP races.
555 	 */
556 	if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
557 		atomic_add_int(&bp->b_refs, 1);
558 		if (BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL) == 0)
559 			BUF_UNLOCK(bp);
560 		atomic_subtract_int(&bp->b_refs, 1);
561 	} else if ((info->clean && (bp->b_flags & B_DELWRI)) ||
562 		   (info->clean == 0 && (bp->b_flags & B_DELWRI) == 0) ||
563 		   bp->b_vp != info->vp ||
564 		   vtruncbuf_bp_trunc_cmp(bp, data)) {
565 		BUF_UNLOCK(bp);
566 	} else {
567 		bremfree(bp);
568 		bp->b_flags |= (B_INVAL | B_RELBUF | B_NOCACHE);
569 		brelse(bp);
570 	}
571 	return(1);
572 }
573 
574 /*
575  * Fsync all meta-data after truncating a file to be non-zero.  Only metadata
576  * blocks (with a negative loffset) are scanned.
577  * Note that the compare function must conform to the RB_SCAN's requirements.
578  */
579 static int
580 vtruncbuf_bp_metasync_cmp(struct buf *bp, void *data __unused)
581 {
582 	if (bp->b_loffset < 0)
583 		return(0);
584 	return(1);
585 }
586 
587 static int
588 vtruncbuf_bp_metasync(struct buf *bp, void *data)
589 {
590 	struct vtruncbuf_info *info = data;
591 
592 	if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
593 		atomic_add_int(&bp->b_refs, 1);
594 		if (BUF_LOCK(bp, LK_EXCLUSIVE|LK_SLEEPFAIL) == 0)
595 			BUF_UNLOCK(bp);
596 		atomic_subtract_int(&bp->b_refs, 1);
597 	} else if ((bp->b_flags & B_DELWRI) == 0 ||
598 		   bp->b_vp != info->vp ||
599 		   vtruncbuf_bp_metasync_cmp(bp, data)) {
600 		BUF_UNLOCK(bp);
601 	} else {
602 		bremfree(bp);
603 		if (bp->b_vp == info->vp)
604 			bawrite(bp);
605 		else
606 			bwrite(bp);
607 	}
608 	return(1);
609 }
610 
611 /*
612  * vfsync - implements a multipass fsync on a file which understands
613  * dependancies and meta-data.  The passed vnode must be locked.  The
614  * waitfor argument may be MNT_WAIT or MNT_NOWAIT, or MNT_LAZY.
615  *
616  * When fsyncing data asynchronously just do one consolidated pass starting
617  * with the most negative block number.  This may not get all the data due
618  * to dependancies.
619  *
620  * When fsyncing data synchronously do a data pass, then a metadata pass,
621  * then do additional data+metadata passes to try to get all the data out.
622  *
623  * Caller must ref the vnode but does not have to lock it.
624  */
625 static int vfsync_wait_output(struct vnode *vp,
626 			    int (*waitoutput)(struct vnode *, struct thread *));
627 static int vfsync_dummy_cmp(struct buf *bp __unused, void *data __unused);
628 static int vfsync_data_only_cmp(struct buf *bp, void *data);
629 static int vfsync_meta_only_cmp(struct buf *bp, void *data);
630 static int vfsync_lazy_range_cmp(struct buf *bp, void *data);
631 static int vfsync_bp(struct buf *bp, void *data);
632 
633 struct vfsync_info {
634 	struct vnode *vp;
635 	int fastpass;
636 	int synchronous;
637 	int syncdeps;
638 	int lazycount;
639 	int lazylimit;
640 	int skippedbufs;
641 	int (*checkdef)(struct buf *);
642 	int (*cmpfunc)(struct buf *, void *);
643 };
644 
645 int
646 vfsync(struct vnode *vp, int waitfor, int passes,
647 	int (*checkdef)(struct buf *),
648 	int (*waitoutput)(struct vnode *, struct thread *))
649 {
650 	struct vfsync_info info;
651 	int error;
652 
653 	bzero(&info, sizeof(info));
654 	info.vp = vp;
655 	if ((info.checkdef = checkdef) == NULL)
656 		info.syncdeps = 1;
657 
658 	lwkt_gettoken(&vp->v_token);
659 
660 	switch(waitfor) {
661 	case MNT_LAZY | MNT_NOWAIT:
662 	case MNT_LAZY:
663 		/*
664 		 * Lazy (filesystem syncer typ) Asynchronous plus limit the
665 		 * number of data (not meta) pages we try to flush to 1MB.
666 		 * A non-zero return means that lazy limit was reached.
667 		 */
668 		info.lazylimit = 1024 * 1024;
669 		info.syncdeps = 1;
670 		info.cmpfunc = vfsync_lazy_range_cmp;
671 		error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
672 				vfsync_lazy_range_cmp, vfsync_bp, &info);
673 		info.cmpfunc = vfsync_meta_only_cmp;
674 		RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree,
675 			vfsync_meta_only_cmp, vfsync_bp, &info);
676 		if (error == 0)
677 			vp->v_lazyw = 0;
678 		else if (!RB_EMPTY(&vp->v_rbdirty_tree))
679 			vn_syncer_add(vp, 1);
680 		error = 0;
681 		break;
682 	case MNT_NOWAIT:
683 		/*
684 		 * Asynchronous.  Do a data-only pass and a meta-only pass.
685 		 */
686 		info.syncdeps = 1;
687 		info.cmpfunc = vfsync_data_only_cmp;
688 		RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_data_only_cmp,
689 			vfsync_bp, &info);
690 		info.cmpfunc = vfsync_meta_only_cmp;
691 		RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_meta_only_cmp,
692 			vfsync_bp, &info);
693 		error = 0;
694 		break;
695 	default:
696 		/*
697 		 * Synchronous.  Do a data-only pass, then a meta-data+data
698 		 * pass, then additional integrated passes to try to get
699 		 * all the dependancies flushed.
700 		 */
701 		info.cmpfunc = vfsync_data_only_cmp;
702 		info.fastpass = 1;
703 		RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, vfsync_data_only_cmp,
704 			vfsync_bp, &info);
705 		info.fastpass = 0;
706 		error = vfsync_wait_output(vp, waitoutput);
707 		if (error == 0) {
708 			info.skippedbufs = 0;
709 			info.cmpfunc = vfsync_dummy_cmp;
710 			RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
711 				vfsync_bp, &info);
712 			error = vfsync_wait_output(vp, waitoutput);
713 			if (info.skippedbufs) {
714 				kprintf("Warning: vfsync skipped %d dirty "
715 					"buf%s in pass2!\n",
716 					info.skippedbufs,
717 					((info.skippedbufs > 1) ? "s" : ""));
718 			}
719 		}
720 		while (error == 0 && passes > 0 &&
721 		       !RB_EMPTY(&vp->v_rbdirty_tree)
722 		) {
723 			info.skippedbufs = 0;
724 			if (--passes == 0) {
725 				info.synchronous = 1;
726 				info.syncdeps = 1;
727 			}
728 			info.cmpfunc = vfsync_dummy_cmp;
729 			error = RB_SCAN(buf_rb_tree, &vp->v_rbdirty_tree, NULL,
730 					vfsync_bp, &info);
731 			if (error < 0)
732 				error = -error;
733 			info.syncdeps = 1;
734 			if (error == 0)
735 				error = vfsync_wait_output(vp, waitoutput);
736 			if (info.skippedbufs && passes == 0) {
737 				kprintf("Warning: vfsync skipped %d dirty "
738 					"buf%s in final pass!\n",
739 					info.skippedbufs,
740 					((info.skippedbufs > 1) ? "s" : ""));
741 			}
742 		}
743 #if 0
744 		/*
745 		 * This case can occur normally because vnode lock might
746 		 * not be held.
747 		 */
748 		if (!RB_EMPTY(&vp->v_rbdirty_tree))
749 			kprintf("dirty bufs left after final pass\n");
750 #endif
751 		break;
752 	}
753 	lwkt_reltoken(&vp->v_token);
754 
755 	return(error);
756 }
757 
758 static int
759 vfsync_wait_output(struct vnode *vp,
760 		   int (*waitoutput)(struct vnode *, struct thread *))
761 {
762 	int error;
763 
764 	error = bio_track_wait(&vp->v_track_write, 0, 0);
765 	if (waitoutput)
766 		error = waitoutput(vp, curthread);
767 	return(error);
768 }
769 
770 static int
771 vfsync_dummy_cmp(struct buf *bp __unused, void *data __unused)
772 {
773 	return(0);
774 }
775 
776 static int
777 vfsync_data_only_cmp(struct buf *bp, void *data)
778 {
779 	if (bp->b_loffset < 0)
780 		return(-1);
781 	return(0);
782 }
783 
784 static int
785 vfsync_meta_only_cmp(struct buf *bp, void *data)
786 {
787 	if (bp->b_loffset < 0)
788 		return(0);
789 	return(1);
790 }
791 
792 static int
793 vfsync_lazy_range_cmp(struct buf *bp, void *data)
794 {
795 	struct vfsync_info *info = data;
796 
797 	if (bp->b_loffset < info->vp->v_lazyw)
798 		return(-1);
799 	return(0);
800 }
801 
802 static int
803 vfsync_bp(struct buf *bp, void *data)
804 {
805 	struct vfsync_info *info = data;
806 	struct vnode *vp = info->vp;
807 	int error;
808 
809 	if (info->fastpass) {
810 		/*
811 		 * Ignore buffers that we cannot immediately lock.
812 		 */
813 		if (BUF_LOCK(bp, LK_EXCLUSIVE | LK_NOWAIT)) {
814 			/*
815 			 * Removed BUF_TIMELOCK(..., 1), even a 1-tick
816 			 * delay can mess up performance
817 			 *
818 			 * Another reason is that during a dirty-buffer
819 			 * scan a clustered write can start I/O on buffers
820 			 * ahead of the scan, causing the scan to not
821 			 * get a lock here.  Usually this means the write
822 			 * is already in progress so, in fact, we *want*
823 			 * to skip the buffer.
824 			 */
825 			++info->skippedbufs;
826 			return(0);
827 		}
828 	} else if (info->synchronous == 0) {
829 		/*
830 		 * Normal pass, give the buffer a little time to become
831 		 * available to us.
832 		 */
833 		if (BUF_TIMELOCK(bp, LK_EXCLUSIVE, "bflst2", hz / 10)) {
834 			++info->skippedbufs;
835 			return(0);
836 		}
837 	} else {
838 		/*
839 		 * Synchronous pass, give the buffer a lot of time before
840 		 * giving up.
841 		 */
842 		if (BUF_TIMELOCK(bp, LK_EXCLUSIVE, "bflst3", hz * 10)) {
843 			++info->skippedbufs;
844 			return(0);
845 		}
846 	}
847 
848 	/*
849 	 * We must revalidate the buffer after locking.
850 	 */
851 	if ((bp->b_flags & B_DELWRI) == 0 ||
852 	    bp->b_vp != info->vp ||
853 	    info->cmpfunc(bp, data)) {
854 		BUF_UNLOCK(bp);
855 		return(0);
856 	}
857 
858 	/*
859 	 * If syncdeps is not set we do not try to write buffers which have
860 	 * dependancies.
861 	 */
862 	if (!info->synchronous && info->syncdeps == 0 && info->checkdef(bp)) {
863 		BUF_UNLOCK(bp);
864 		return(0);
865 	}
866 
867 	/*
868 	 * B_NEEDCOMMIT (primarily used by NFS) is a state where the buffer
869 	 * has been written but an additional handshake with the device
870 	 * is required before we can dispose of the buffer.  We have no idea
871 	 * how to do this so we have to skip these buffers.
872 	 */
873 	if (bp->b_flags & B_NEEDCOMMIT) {
874 		BUF_UNLOCK(bp);
875 		return(0);
876 	}
877 
878 	/*
879 	 * Ask bioops if it is ok to sync.  If not the VFS may have
880 	 * set B_LOCKED so we have to cycle the buffer.
881 	 */
882 	if (LIST_FIRST(&bp->b_dep) != NULL && buf_checkwrite(bp)) {
883 		bremfree(bp);
884 		brelse(bp);
885 		return(0);
886 	}
887 
888 	if (info->synchronous) {
889 		/*
890 		 * Synchronous flush.  An error may be returned and will
891 		 * stop the scan.
892 		 */
893 		bremfree(bp);
894 		error = bwrite(bp);
895 	} else {
896 		/*
897 		 * Asynchronous flush.  We use the error return to support
898 		 * MNT_LAZY flushes.
899 		 *
900 		 * In low-memory situations we revert to synchronous
901 		 * operation.  This should theoretically prevent the I/O
902 		 * path from exhausting memory in a non-recoverable way.
903 		 */
904 		vp->v_lazyw = bp->b_loffset;
905 		bremfree(bp);
906 		if (vm_page_count_min(0)) {
907 			/* low memory */
908 			info->lazycount += bp->b_bufsize;
909 			bwrite(bp);
910 		} else {
911 			/* normal */
912 			info->lazycount += cluster_awrite(bp);
913 			waitrunningbufspace();
914 			/*vm_wait_nominal();*/
915 		}
916 		if (info->lazylimit && info->lazycount >= info->lazylimit)
917 			error = 1;
918 		else
919 			error = 0;
920 	}
921 	return(-error);
922 }
923 
924 /*
925  * Associate a buffer with a vnode.
926  *
927  * MPSAFE
928  */
929 int
930 bgetvp(struct vnode *vp, struct buf *bp, int testsize)
931 {
932 	KASSERT(bp->b_vp == NULL, ("bgetvp: not free"));
933 	KKASSERT((bp->b_flags & (B_HASHED|B_DELWRI|B_VNCLEAN|B_VNDIRTY)) == 0);
934 
935 	/*
936 	 * Insert onto list for new vnode.
937 	 */
938 	lwkt_gettoken(&vp->v_token);
939 
940 	if (buf_rb_hash_RB_INSERT(&vp->v_rbhash_tree, bp)) {
941 		lwkt_reltoken(&vp->v_token);
942 		return (EEXIST);
943 	}
944 
945 	/*
946 	 * Diagnostics (mainly for HAMMER debugging).  Check for
947 	 * overlapping buffers.
948 	 */
949 	if (check_buf_overlap) {
950 		struct buf *bx;
951 		bx = buf_rb_hash_RB_PREV(bp);
952 		if (bx) {
953 			if (bx->b_loffset + bx->b_bufsize > bp->b_loffset) {
954 				kprintf("bgetvp: overlapl %016jx/%d %016jx "
955 					"bx %p bp %p\n",
956 					(intmax_t)bx->b_loffset,
957 					bx->b_bufsize,
958 					(intmax_t)bp->b_loffset,
959 					bx, bp);
960 				if (check_buf_overlap > 1)
961 					panic("bgetvp - overlapping buffer");
962 			}
963 		}
964 		bx = buf_rb_hash_RB_NEXT(bp);
965 		if (bx) {
966 			if (bp->b_loffset + testsize > bx->b_loffset) {
967 				kprintf("bgetvp: overlapr %016jx/%d %016jx "
968 					"bp %p bx %p\n",
969 					(intmax_t)bp->b_loffset,
970 					testsize,
971 					(intmax_t)bx->b_loffset,
972 					bp, bx);
973 				if (check_buf_overlap > 1)
974 					panic("bgetvp - overlapping buffer");
975 			}
976 		}
977 	}
978 	bp->b_vp = vp;
979 	bp->b_flags |= B_HASHED;
980 	bp->b_flags |= B_VNCLEAN;
981 	if (buf_rb_tree_RB_INSERT(&vp->v_rbclean_tree, bp))
982 		panic("reassignbuf: dup lblk/clean vp %p bp %p", vp, bp);
983 	/*vhold(vp);*/
984 	lwkt_reltoken(&vp->v_token);
985 	return(0);
986 }
987 
988 /*
989  * Disassociate a buffer from a vnode.
990  *
991  * MPSAFE
992  */
993 void
994 brelvp(struct buf *bp)
995 {
996 	struct vnode *vp;
997 
998 	KASSERT(bp->b_vp != NULL, ("brelvp: NULL"));
999 
1000 	/*
1001 	 * Delete from old vnode list, if on one.
1002 	 */
1003 	vp = bp->b_vp;
1004 	lwkt_gettoken(&vp->v_token);
1005 	if (bp->b_flags & (B_VNDIRTY | B_VNCLEAN)) {
1006 		if (bp->b_flags & B_VNDIRTY)
1007 			buf_rb_tree_RB_REMOVE(&vp->v_rbdirty_tree, bp);
1008 		else
1009 			buf_rb_tree_RB_REMOVE(&vp->v_rbclean_tree, bp);
1010 		bp->b_flags &= ~(B_VNDIRTY | B_VNCLEAN);
1011 	}
1012 	if (bp->b_flags & B_HASHED) {
1013 		buf_rb_hash_RB_REMOVE(&vp->v_rbhash_tree, bp);
1014 		bp->b_flags &= ~B_HASHED;
1015 	}
1016 
1017 	/*
1018 	 * Only remove from synclist when no dirty buffers are left AND
1019 	 * the VFS has not flagged the vnode's inode as being dirty.
1020 	 */
1021 	if ((vp->v_flag & (VONWORKLST | VISDIRTY | VOBJDIRTY)) == VONWORKLST &&
1022 	    RB_EMPTY(&vp->v_rbdirty_tree)) {
1023 		vn_syncer_remove(vp, 0);
1024 	}
1025 	bp->b_vp = NULL;
1026 
1027 	lwkt_reltoken(&vp->v_token);
1028 
1029 	/*vdrop(vp);*/
1030 }
1031 
1032 /*
1033  * Reassign the buffer to the proper clean/dirty list based on B_DELWRI.
1034  * This routine is called when the state of the B_DELWRI bit is changed.
1035  *
1036  * Must be called with vp->v_token held.
1037  * MPSAFE
1038  */
1039 void
1040 reassignbuf(struct buf *bp)
1041 {
1042 	struct vnode *vp = bp->b_vp;
1043 	int delay;
1044 
1045 	ASSERT_LWKT_TOKEN_HELD(&vp->v_token);
1046 	++reassignbufcalls;
1047 
1048 	/*
1049 	 * B_PAGING flagged buffers cannot be reassigned because their vp
1050 	 * is not fully linked in.
1051 	 */
1052 	if (bp->b_flags & B_PAGING)
1053 		panic("cannot reassign paging buffer");
1054 
1055 	if (bp->b_flags & B_DELWRI) {
1056 		/*
1057 		 * Move to the dirty list, add the vnode to the worklist
1058 		 */
1059 		if (bp->b_flags & B_VNCLEAN) {
1060 			buf_rb_tree_RB_REMOVE(&vp->v_rbclean_tree, bp);
1061 			bp->b_flags &= ~B_VNCLEAN;
1062 		}
1063 		if ((bp->b_flags & B_VNDIRTY) == 0) {
1064 			if (buf_rb_tree_RB_INSERT(&vp->v_rbdirty_tree, bp)) {
1065 				panic("reassignbuf: dup lblk vp %p bp %p",
1066 				      vp, bp);
1067 			}
1068 			bp->b_flags |= B_VNDIRTY;
1069 		}
1070 		if ((vp->v_flag & VONWORKLST) == 0) {
1071 			switch (vp->v_type) {
1072 			case VDIR:
1073 				delay = dirdelay;
1074 				break;
1075 			case VCHR:
1076 			case VBLK:
1077 				if (vp->v_rdev &&
1078 				    vp->v_rdev->si_mountpoint != NULL) {
1079 					delay = metadelay;
1080 					break;
1081 				}
1082 				/* fall through */
1083 			default:
1084 				delay = filedelay;
1085 			}
1086 			vn_syncer_add(vp, delay);
1087 		}
1088 	} else {
1089 		/*
1090 		 * Move to the clean list, remove the vnode from the worklist
1091 		 * if no dirty blocks remain.
1092 		 */
1093 		if (bp->b_flags & B_VNDIRTY) {
1094 			buf_rb_tree_RB_REMOVE(&vp->v_rbdirty_tree, bp);
1095 			bp->b_flags &= ~B_VNDIRTY;
1096 		}
1097 		if ((bp->b_flags & B_VNCLEAN) == 0) {
1098 			if (buf_rb_tree_RB_INSERT(&vp->v_rbclean_tree, bp)) {
1099 				panic("reassignbuf: dup lblk vp %p bp %p",
1100 				      vp, bp);
1101 			}
1102 			bp->b_flags |= B_VNCLEAN;
1103 		}
1104 
1105 		/*
1106 		 * Only remove from synclist when no dirty buffers are left
1107 		 * AND the VFS has not flagged the vnode's inode as being
1108 		 * dirty.
1109 		 */
1110 		if ((vp->v_flag & (VONWORKLST | VISDIRTY | VOBJDIRTY)) ==
1111 		     VONWORKLST &&
1112 		    RB_EMPTY(&vp->v_rbdirty_tree)) {
1113 			vn_syncer_remove(vp, 0);
1114 		}
1115 	}
1116 }
1117 
1118 /*
1119  * Create a vnode for a block device.  Used for mounting the root file
1120  * system.
1121  *
1122  * A vref()'d vnode is returned.
1123  */
1124 extern struct vop_ops *devfs_vnode_dev_vops_p;
1125 int
1126 bdevvp(cdev_t dev, struct vnode **vpp)
1127 {
1128 	struct vnode *vp;
1129 	struct vnode *nvp;
1130 	int error;
1131 
1132 	if (dev == NULL) {
1133 		*vpp = NULLVP;
1134 		return (ENXIO);
1135 	}
1136 	error = getspecialvnode(VT_NON, NULL, &devfs_vnode_dev_vops_p,
1137 				&nvp, 0, 0);
1138 	if (error) {
1139 		*vpp = NULLVP;
1140 		return (error);
1141 	}
1142 	vp = nvp;
1143 	vp->v_type = VCHR;
1144 #if 0
1145 	vp->v_rdev = dev;
1146 #endif
1147 	v_associate_rdev(vp, dev);
1148 	vp->v_umajor = dev->si_umajor;
1149 	vp->v_uminor = dev->si_uminor;
1150 	vx_unlock(vp);
1151 	*vpp = vp;
1152 	return (0);
1153 }
1154 
1155 int
1156 v_associate_rdev(struct vnode *vp, cdev_t dev)
1157 {
1158 	if (dev == NULL)
1159 		return(ENXIO);
1160 	if (dev_is_good(dev) == 0)
1161 		return(ENXIO);
1162 	KKASSERT(vp->v_rdev == NULL);
1163 	vp->v_rdev = reference_dev(dev);
1164 	lwkt_gettoken(&spechash_token);
1165 	SLIST_INSERT_HEAD(&dev->si_hlist, vp, v_cdevnext);
1166 	lwkt_reltoken(&spechash_token);
1167 	return(0);
1168 }
1169 
1170 void
1171 v_release_rdev(struct vnode *vp)
1172 {
1173 	cdev_t dev;
1174 
1175 	if ((dev = vp->v_rdev) != NULL) {
1176 		lwkt_gettoken(&spechash_token);
1177 		SLIST_REMOVE(&dev->si_hlist, vp, vnode, v_cdevnext);
1178 		vp->v_rdev = NULL;
1179 		release_dev(dev);
1180 		lwkt_reltoken(&spechash_token);
1181 	}
1182 }
1183 
1184 /*
1185  * Add a vnode to the alias list hung off the cdev_t.  We only associate
1186  * the device number with the vnode.  The actual device is not associated
1187  * until the vnode is opened (usually in spec_open()), and will be
1188  * disassociated on last close.
1189  */
1190 void
1191 addaliasu(struct vnode *nvp, int x, int y)
1192 {
1193 	if (nvp->v_type != VBLK && nvp->v_type != VCHR)
1194 		panic("addaliasu on non-special vnode");
1195 	nvp->v_umajor = x;
1196 	nvp->v_uminor = y;
1197 }
1198 
1199 /*
1200  * Simple call that a filesystem can make to try to get rid of a
1201  * vnode.  It will fail if anyone is referencing the vnode (including
1202  * the caller).
1203  *
1204  * The filesystem can check whether its in-memory inode structure still
1205  * references the vp on return.
1206  *
1207  * May only be called if the vnode is in a known state (i.e. being prevented
1208  * from being deallocated by some other condition such as a vfs inode hold).
1209  */
1210 void
1211 vclean_unlocked(struct vnode *vp)
1212 {
1213 	vx_get(vp);
1214 	if (VREFCNT(vp) <= 1)
1215 		vgone_vxlocked(vp);
1216 	vx_put(vp);
1217 }
1218 
1219 /*
1220  * Disassociate a vnode from its underlying filesystem.
1221  *
1222  * The vnode must be VX locked and referenced.  In all normal situations
1223  * there are no active references.  If vclean_vxlocked() is called while
1224  * there are active references, the vnode is being ripped out and we have
1225  * to call VOP_CLOSE() as appropriate before we can reclaim it.
1226  */
1227 void
1228 vclean_vxlocked(struct vnode *vp, int flags)
1229 {
1230 	int active;
1231 	int n;
1232 	vm_object_t object;
1233 	struct namecache *ncp;
1234 
1235 	/*
1236 	 * If the vnode has already been reclaimed we have nothing to do.
1237 	 */
1238 	if (vp->v_flag & VRECLAIMED)
1239 		return;
1240 
1241 	/*
1242 	 * Set flag to interlock operation, flag finalization to ensure
1243 	 * that the vnode winds up on the inactive list, and set v_act to 0.
1244 	 */
1245 	vsetflags(vp, VRECLAIMED);
1246 	atomic_set_int(&vp->v_refcnt, VREF_FINALIZE);
1247 	vp->v_act = 0;
1248 
1249 	if (verbose_reclaims) {
1250 		if ((ncp = TAILQ_FIRST(&vp->v_namecache)) != NULL)
1251 			kprintf("Debug: reclaim %p %s\n", vp, ncp->nc_name);
1252 	}
1253 
1254 	/*
1255 	 * Scrap the vfs cache
1256 	 */
1257 	while (cache_inval_vp(vp, 0) != 0) {
1258 		kprintf("Warning: vnode %p clean/cache_resolution "
1259 			"race detected\n", vp);
1260 		tsleep(vp, 0, "vclninv", 2);
1261 	}
1262 
1263 	/*
1264 	 * Check to see if the vnode is in use. If so we have to reference it
1265 	 * before we clean it out so that its count cannot fall to zero and
1266 	 * generate a race against ourselves to recycle it.
1267 	 */
1268 	active = (VREFCNT(vp) > 0);
1269 
1270 	/*
1271 	 * Clean out any buffers associated with the vnode and destroy its
1272 	 * object, if it has one.
1273 	 */
1274 	vinvalbuf(vp, V_SAVE, 0, 0);
1275 
1276 	/*
1277 	 * If purging an active vnode (typically during a forced unmount
1278 	 * or reboot), it must be closed and deactivated before being
1279 	 * reclaimed.  This isn't really all that safe, but what can
1280 	 * we do? XXX.
1281 	 *
1282 	 * Note that neither of these routines unlocks the vnode.
1283 	 */
1284 	if (active && (flags & DOCLOSE)) {
1285 		while ((n = vp->v_opencount) != 0) {
1286 			if (vp->v_writecount)
1287 				VOP_CLOSE(vp, FWRITE|FNONBLOCK, NULL);
1288 			else
1289 				VOP_CLOSE(vp, FNONBLOCK, NULL);
1290 			if (vp->v_opencount == n) {
1291 				kprintf("Warning: unable to force-close"
1292 				       " vnode %p\n", vp);
1293 				break;
1294 			}
1295 		}
1296 	}
1297 
1298 	/*
1299 	 * If the vnode has not been deactivated, deactivated it.  Deactivation
1300 	 * can create new buffers and VM pages so we have to call vinvalbuf()
1301 	 * again to make sure they all get flushed.
1302 	 *
1303 	 * This can occur if a file with a link count of 0 needs to be
1304 	 * truncated.
1305 	 *
1306 	 * If the vnode is already dead don't try to deactivate it.
1307 	 */
1308 	if ((vp->v_flag & VINACTIVE) == 0) {
1309 		vsetflags(vp, VINACTIVE);
1310 		if (vp->v_mount)
1311 			VOP_INACTIVE(vp);
1312 		vinvalbuf(vp, V_SAVE, 0, 0);
1313 	}
1314 
1315 	/*
1316 	 * If the vnode has an object, destroy it.
1317 	 */
1318 	while ((object = vp->v_object) != NULL) {
1319 		vm_object_hold(object);
1320 		if (object == vp->v_object)
1321 			break;
1322 		vm_object_drop(object);
1323 	}
1324 
1325 	if (object != NULL) {
1326 		if (object->ref_count == 0) {
1327 			if ((object->flags & OBJ_DEAD) == 0)
1328 				vm_object_terminate(object);
1329 			vm_object_drop(object);
1330 			vclrflags(vp, VOBJBUF);
1331 		} else {
1332 			vm_pager_deallocate(object);
1333 			vclrflags(vp, VOBJBUF);
1334 			vm_object_drop(object);
1335 		}
1336 	}
1337 	KKASSERT((vp->v_flag & VOBJBUF) == 0);
1338 
1339 	if (vp->v_flag & VOBJDIRTY)
1340 		vclrobjdirty(vp);
1341 
1342 	/*
1343 	 * Reclaim the vnode if not already dead.
1344 	 */
1345 	if (vp->v_mount && VOP_RECLAIM(vp))
1346 		panic("vclean: cannot reclaim");
1347 
1348 	/*
1349 	 * Done with purge, notify sleepers of the grim news.
1350 	 */
1351 	vp->v_ops = &dead_vnode_vops_p;
1352 	vn_gone(vp);
1353 	vp->v_tag = VT_NON;
1354 
1355 	/*
1356 	 * If we are destroying an active vnode, reactivate it now that
1357 	 * we have reassociated it with deadfs.  This prevents the system
1358 	 * from crashing on the vnode due to it being unexpectedly marked
1359 	 * as inactive or reclaimed.
1360 	 */
1361 	if (active && (flags & DOCLOSE)) {
1362 		vclrflags(vp, VINACTIVE | VRECLAIMED);
1363 	}
1364 }
1365 
1366 /*
1367  * Eliminate all activity associated with the requested vnode
1368  * and with all vnodes aliased to the requested vnode.
1369  *
1370  * The vnode must be referenced but should not be locked.
1371  */
1372 int
1373 vrevoke(struct vnode *vp, struct ucred *cred)
1374 {
1375 	struct vnode *vq;
1376 	struct vnode *vqn;
1377 	cdev_t dev;
1378 	int error;
1379 
1380 	/*
1381 	 * If the vnode has a device association, scrap all vnodes associated
1382 	 * with the device.  Don't let the device disappear on us while we
1383 	 * are scrapping the vnodes.
1384 	 *
1385 	 * The passed vp will probably show up in the list, do not VX lock
1386 	 * it twice!
1387 	 *
1388 	 * Releasing the vnode's rdev here can mess up specfs's call to
1389 	 * device close, so don't do it.  The vnode has been disassociated
1390 	 * and the device will be closed after the last ref on the related
1391 	 * fp goes away (if not still open by e.g. the kernel).
1392 	 */
1393 	if (vp->v_type != VCHR) {
1394 		error = fdrevoke(vp, DTYPE_VNODE, cred);
1395 		return (error);
1396 	}
1397 	if ((dev = vp->v_rdev) == NULL) {
1398 		return(0);
1399 	}
1400 	reference_dev(dev);
1401 	lwkt_gettoken(&spechash_token);
1402 
1403 restart:
1404 	vqn = SLIST_FIRST(&dev->si_hlist);
1405 	if (vqn)
1406 		vhold(vqn);
1407 	while ((vq = vqn) != NULL) {
1408 		if (VREFCNT(vq) > 0) {
1409 			vref(vq);
1410 			fdrevoke(vq, DTYPE_VNODE, cred);
1411 			/*v_release_rdev(vq);*/
1412 			vrele(vq);
1413 			if (vq->v_rdev != dev) {
1414 				vdrop(vq);
1415 				goto restart;
1416 			}
1417 		}
1418 		vqn = SLIST_NEXT(vq, v_cdevnext);
1419 		if (vqn)
1420 			vhold(vqn);
1421 		vdrop(vq);
1422 	}
1423 	lwkt_reltoken(&spechash_token);
1424 	dev_drevoke(dev);
1425 	release_dev(dev);
1426 	return (0);
1427 }
1428 
1429 /*
1430  * This is called when the object underlying a vnode is being destroyed,
1431  * such as in a remove().  Try to recycle the vnode immediately if the
1432  * only active reference is our reference.
1433  *
1434  * Directory vnodes in the namecache with children cannot be immediately
1435  * recycled because numerous VOP_N*() ops require them to be stable.
1436  *
1437  * To avoid recursive recycling from VOP_INACTIVE implemenetations this
1438  * function is a NOP if VRECLAIMED is already set.
1439  */
1440 int
1441 vrecycle(struct vnode *vp)
1442 {
1443 	if (VREFCNT(vp) <= 1 && (vp->v_flag & VRECLAIMED) == 0) {
1444 		if (cache_inval_vp_nonblock(vp))
1445 			return(0);
1446 		vgone_vxlocked(vp);
1447 		return (1);
1448 	}
1449 	return (0);
1450 }
1451 
1452 /*
1453  * Return the maximum I/O size allowed for strategy calls on VP.
1454  *
1455  * If vp is VCHR or VBLK we dive the device, otherwise we use
1456  * the vp's mount info.
1457  *
1458  * The returned value is clamped at MAXPHYS as most callers cannot use
1459  * buffers larger than that size.
1460  */
1461 int
1462 vmaxiosize(struct vnode *vp)
1463 {
1464 	int maxiosize;
1465 
1466 	if (vp->v_type == VBLK || vp->v_type == VCHR)
1467 		maxiosize = vp->v_rdev->si_iosize_max;
1468 	else
1469 		maxiosize = vp->v_mount->mnt_iosize_max;
1470 
1471 	if (maxiosize > MAXPHYS)
1472 		maxiosize = MAXPHYS;
1473 	return (maxiosize);
1474 }
1475 
1476 /*
1477  * Eliminate all activity associated with a vnode in preparation for
1478  * destruction.
1479  *
1480  * The vnode must be VX locked and refd and will remain VX locked and refd
1481  * on return.  This routine may be called with the vnode in any state, as
1482  * long as it is VX locked.  The vnode will be cleaned out and marked
1483  * VRECLAIMED but will not actually be reused until all existing refs and
1484  * holds go away.
1485  *
1486  * NOTE: This routine may be called on a vnode which has not yet been
1487  * already been deactivated (VOP_INACTIVE), or on a vnode which has
1488  * already been reclaimed.
1489  *
1490  * This routine is not responsible for placing us back on the freelist.
1491  * Instead, it happens automatically when the caller releases the VX lock
1492  * (assuming there aren't any other references).
1493  */
1494 void
1495 vgone_vxlocked(struct vnode *vp)
1496 {
1497 	/*
1498 	 * assert that the VX lock is held.  This is an absolute requirement
1499 	 * now for vgone_vxlocked() to be called.
1500 	 */
1501 	KKASSERT(lockinuse(&vp->v_lock));
1502 
1503 	/*
1504 	 * Clean out the filesystem specific data and set the VRECLAIMED
1505 	 * bit.  Also deactivate the vnode if necessary.
1506 	 *
1507 	 * The vnode should have automatically been removed from the syncer
1508 	 * list as syncer/dirty flags cleared during the cleaning.
1509 	 */
1510 	vclean_vxlocked(vp, DOCLOSE);
1511 
1512 	/*
1513 	 * Normally panic if the vnode is still dirty, unless we are doing
1514 	 * a forced unmount (tmpfs typically).
1515 	 */
1516 	if (vp->v_flag & VONWORKLST) {
1517 		if (vp->v_mount->mnt_kern_flag & MNTK_UNMOUNTF) {
1518 			/* force removal */
1519 			vn_syncer_remove(vp, 1);
1520 		} else {
1521 			panic("vp %p still dirty in vgone after flush", vp);
1522 		}
1523 	}
1524 
1525 	/*
1526 	 * Delete from old mount point vnode list, if on one.
1527 	 */
1528 	if (vp->v_mount != NULL) {
1529 		KKASSERT(vp->v_data == NULL);
1530 		insmntque(vp, NULL);
1531 	}
1532 
1533 	/*
1534 	 * If special device, remove it from special device alias list
1535 	 * if it is on one.  This should normally only occur if a vnode is
1536 	 * being revoked as the device should otherwise have been released
1537 	 * naturally.
1538 	 */
1539 	if ((vp->v_type == VBLK || vp->v_type == VCHR) && vp->v_rdev != NULL) {
1540 		v_release_rdev(vp);
1541 	}
1542 
1543 	/*
1544 	 * Set us to VBAD
1545 	 */
1546 	vp->v_type = VBAD;
1547 }
1548 
1549 /*
1550  * Lookup a vnode by device number.
1551  *
1552  * Returns non-zero and *vpp set to a vref'd vnode on success.
1553  * Returns zero on failure.
1554  */
1555 int
1556 vfinddev(cdev_t dev, enum vtype type, struct vnode **vpp)
1557 {
1558 	struct vnode *vp;
1559 
1560 	lwkt_gettoken(&spechash_token);
1561 	SLIST_FOREACH(vp, &dev->si_hlist, v_cdevnext) {
1562 		if (type == vp->v_type) {
1563 			*vpp = vp;
1564 			vref(vp);
1565 			lwkt_reltoken(&spechash_token);
1566 			return (1);
1567 		}
1568 	}
1569 	lwkt_reltoken(&spechash_token);
1570 	return (0);
1571 }
1572 
1573 /*
1574  * Calculate the total number of references to a special device.  This
1575  * routine may only be called for VBLK and VCHR vnodes since v_rdev is
1576  * an overloaded field.  Since udev2dev can now return NULL, we have
1577  * to check for a NULL v_rdev.
1578  */
1579 int
1580 count_dev(cdev_t dev)
1581 {
1582 	struct vnode *vp;
1583 	int count = 0;
1584 
1585 	if (SLIST_FIRST(&dev->si_hlist)) {
1586 		lwkt_gettoken(&spechash_token);
1587 		SLIST_FOREACH(vp, &dev->si_hlist, v_cdevnext) {
1588 			count += vp->v_opencount;
1589 		}
1590 		lwkt_reltoken(&spechash_token);
1591 	}
1592 	return(count);
1593 }
1594 
1595 int
1596 vcount(struct vnode *vp)
1597 {
1598 	if (vp->v_rdev == NULL)
1599 		return(0);
1600 	return(count_dev(vp->v_rdev));
1601 }
1602 
1603 /*
1604  * Initialize VMIO for a vnode.  This routine MUST be called before a
1605  * VFS can issue buffer cache ops on a vnode.  It is typically called
1606  * when a vnode is initialized from its inode.
1607  */
1608 int
1609 vinitvmio(struct vnode *vp, off_t filesize, int blksize, int boff)
1610 {
1611 	vm_object_t object;
1612 	int error = 0;
1613 
1614 	object = vp->v_object;
1615 	if (object) {
1616 		vm_object_hold(object);
1617 		KKASSERT(vp->v_object == object);
1618 	}
1619 
1620 	if (object == NULL) {
1621 		object = vnode_pager_alloc(vp, filesize, 0, 0, blksize, boff);
1622 
1623 		/*
1624 		 * Dereference the reference we just created.  This assumes
1625 		 * that the object is associated with the vp.  Allow it to
1626 		 * have zero refs.  It cannot be destroyed as long as it
1627 		 * is associated with the vnode.
1628 		 */
1629 		vm_object_hold(object);
1630 		atomic_add_int(&object->ref_count, -1);
1631 		vrele(vp);
1632 	} else {
1633 		KKASSERT((object->flags & OBJ_DEAD) == 0);
1634 	}
1635 	KASSERT(vp->v_object != NULL, ("vinitvmio: NULL object"));
1636 	vsetflags(vp, VOBJBUF);
1637 	vm_object_drop(object);
1638 
1639 	return (error);
1640 }
1641 
1642 
1643 /*
1644  * Print out a description of a vnode.
1645  */
1646 static char *typename[] =
1647 {"VNON", "VREG", "VDIR", "VBLK", "VCHR", "VLNK", "VSOCK", "VFIFO", "VBAD"};
1648 
1649 void
1650 vprint(char *label, struct vnode *vp)
1651 {
1652 	char buf[96];
1653 
1654 	if (label != NULL)
1655 		kprintf("%s: %p: ", label, (void *)vp);
1656 	else
1657 		kprintf("%p: ", (void *)vp);
1658 	kprintf("type %s, refcnt %08x, writecount %d, holdcnt %d,",
1659 		typename[vp->v_type],
1660 		vp->v_refcnt, vp->v_writecount, vp->v_auxrefs);
1661 	buf[0] = '\0';
1662 	if (vp->v_flag & VROOT)
1663 		strcat(buf, "|VROOT");
1664 	if (vp->v_flag & VPFSROOT)
1665 		strcat(buf, "|VPFSROOT");
1666 	if (vp->v_flag & VTEXT)
1667 		strcat(buf, "|VTEXT");
1668 	if (vp->v_flag & VSYSTEM)
1669 		strcat(buf, "|VSYSTEM");
1670 	if (vp->v_flag & VOBJBUF)
1671 		strcat(buf, "|VOBJBUF");
1672 	if (buf[0] != '\0')
1673 		kprintf(" flags (%s)", &buf[1]);
1674 	if (vp->v_data == NULL) {
1675 		kprintf("\n");
1676 	} else {
1677 		kprintf("\n\t");
1678 		VOP_PRINT(vp);
1679 	}
1680 }
1681 
1682 /*
1683  * Do the usual access checking.
1684  * file_mode, uid and gid are from the vnode in question,
1685  * while acc_mode and cred are from the VOP_ACCESS parameter list
1686  */
1687 int
1688 vaccess(enum vtype type, mode_t file_mode, uid_t uid, gid_t gid,
1689     mode_t acc_mode, struct ucred *cred)
1690 {
1691 	mode_t mask;
1692 	int ismember;
1693 
1694 	/*
1695 	 * Super-user always gets read/write access, but execute access depends
1696 	 * on at least one execute bit being set.
1697 	 */
1698 	if (priv_check_cred(cred, PRIV_ROOT, 0) == 0) {
1699 		if ((acc_mode & VEXEC) && type != VDIR &&
1700 		    (file_mode & (S_IXUSR|S_IXGRP|S_IXOTH)) == 0)
1701 			return (EACCES);
1702 		return (0);
1703 	}
1704 
1705 	mask = 0;
1706 
1707 	/* Otherwise, check the owner. */
1708 	if (cred->cr_uid == uid) {
1709 		if (acc_mode & VEXEC)
1710 			mask |= S_IXUSR;
1711 		if (acc_mode & VREAD)
1712 			mask |= S_IRUSR;
1713 		if (acc_mode & VWRITE)
1714 			mask |= S_IWUSR;
1715 		return ((file_mode & mask) == mask ? 0 : EACCES);
1716 	}
1717 
1718 	/* Otherwise, check the groups. */
1719 	ismember = groupmember(gid, cred);
1720 	if (cred->cr_svgid == gid || ismember) {
1721 		if (acc_mode & VEXEC)
1722 			mask |= S_IXGRP;
1723 		if (acc_mode & VREAD)
1724 			mask |= S_IRGRP;
1725 		if (acc_mode & VWRITE)
1726 			mask |= S_IWGRP;
1727 		return ((file_mode & mask) == mask ? 0 : EACCES);
1728 	}
1729 
1730 	/* Otherwise, check everyone else. */
1731 	if (acc_mode & VEXEC)
1732 		mask |= S_IXOTH;
1733 	if (acc_mode & VREAD)
1734 		mask |= S_IROTH;
1735 	if (acc_mode & VWRITE)
1736 		mask |= S_IWOTH;
1737 	return ((file_mode & mask) == mask ? 0 : EACCES);
1738 }
1739 
1740 #ifdef DDB
1741 #include <ddb/ddb.h>
1742 
1743 static int db_show_locked_vnodes(struct mount *mp, void *data);
1744 
1745 /*
1746  * List all of the locked vnodes in the system.
1747  * Called when debugging the kernel.
1748  */
1749 DB_SHOW_COMMAND(lockedvnodes, lockedvnodes)
1750 {
1751 	kprintf("Locked vnodes\n");
1752 	mountlist_scan(db_show_locked_vnodes, NULL,
1753 			MNTSCAN_FORWARD|MNTSCAN_NOBUSY);
1754 }
1755 
1756 static int
1757 db_show_locked_vnodes(struct mount *mp, void *data __unused)
1758 {
1759 	struct vnode *vp;
1760 
1761 	TAILQ_FOREACH(vp, &mp->mnt_nvnodelist, v_nmntvnodes) {
1762 		if (vn_islocked(vp))
1763 			vprint(NULL, vp);
1764 	}
1765 	return(0);
1766 }
1767 #endif
1768 
1769 /*
1770  * Top level filesystem related information gathering.
1771  */
1772 static int	sysctl_ovfs_conf (SYSCTL_HANDLER_ARGS);
1773 
1774 static int
1775 vfs_sysctl(SYSCTL_HANDLER_ARGS)
1776 {
1777 	int *name = (int *)arg1 - 1;	/* XXX */
1778 	u_int namelen = arg2 + 1;	/* XXX */
1779 	struct vfsconf *vfsp;
1780 	int maxtypenum;
1781 
1782 #if 1 || defined(COMPAT_PRELITE2)
1783 	/* Resolve ambiguity between VFS_VFSCONF and VFS_GENERIC. */
1784 	if (namelen == 1)
1785 		return (sysctl_ovfs_conf(oidp, arg1, arg2, req));
1786 #endif
1787 
1788 #ifdef notyet
1789 	/* all sysctl names at this level are at least name and field */
1790 	if (namelen < 2)
1791 		return (ENOTDIR);		/* overloaded */
1792 	if (name[0] != VFS_GENERIC) {
1793 		vfsp = vfsconf_find_by_typenum(name[0]);
1794 		if (vfsp == NULL)
1795 			return (EOPNOTSUPP);
1796 		return ((*vfsp->vfc_vfsops->vfs_sysctl)(&name[1], namelen - 1,
1797 		    oldp, oldlenp, newp, newlen, p));
1798 	}
1799 #endif
1800 	switch (name[1]) {
1801 	case VFS_MAXTYPENUM:
1802 		if (namelen != 2)
1803 			return (ENOTDIR);
1804 		maxtypenum = vfsconf_get_maxtypenum();
1805 		return (SYSCTL_OUT(req, &maxtypenum, sizeof(maxtypenum)));
1806 	case VFS_CONF:
1807 		if (namelen != 3)
1808 			return (ENOTDIR);	/* overloaded */
1809 		vfsp = vfsconf_find_by_typenum(name[2]);
1810 		if (vfsp == NULL)
1811 			return (EOPNOTSUPP);
1812 		return (SYSCTL_OUT(req, vfsp, sizeof *vfsp));
1813 	}
1814 	return (EOPNOTSUPP);
1815 }
1816 
1817 SYSCTL_NODE(_vfs, VFS_GENERIC, generic, CTLFLAG_RD, vfs_sysctl,
1818 	"Generic filesystem");
1819 
1820 #if 1 || defined(COMPAT_PRELITE2)
1821 
1822 static int
1823 sysctl_ovfs_conf_iter(struct vfsconf *vfsp, void *data)
1824 {
1825 	int error;
1826 	struct ovfsconf ovfs;
1827 	struct sysctl_req *req = (struct sysctl_req*) data;
1828 
1829 	bzero(&ovfs, sizeof(ovfs));
1830 	ovfs.vfc_vfsops = vfsp->vfc_vfsops;	/* XXX used as flag */
1831 	strcpy(ovfs.vfc_name, vfsp->vfc_name);
1832 	ovfs.vfc_index = vfsp->vfc_typenum;
1833 	ovfs.vfc_refcount = vfsp->vfc_refcount;
1834 	ovfs.vfc_flags = vfsp->vfc_flags;
1835 	error = SYSCTL_OUT(req, &ovfs, sizeof ovfs);
1836 	if (error)
1837 		return error; /* abort iteration with error code */
1838 	else
1839 		return 0; /* continue iterating with next element */
1840 }
1841 
1842 static int
1843 sysctl_ovfs_conf(SYSCTL_HANDLER_ARGS)
1844 {
1845 	return vfsconf_each(sysctl_ovfs_conf_iter, (void*)req);
1846 }
1847 
1848 #endif /* 1 || COMPAT_PRELITE2 */
1849 
1850 /*
1851  * Check to see if a filesystem is mounted on a block device.
1852  */
1853 int
1854 vfs_mountedon(struct vnode *vp)
1855 {
1856 	cdev_t dev;
1857 
1858 	if ((dev = vp->v_rdev) == NULL) {
1859 /*		if (vp->v_type != VBLK)
1860 			dev = get_dev(vp->v_uminor, vp->v_umajor); */
1861 	}
1862 	if (dev != NULL && dev->si_mountpoint)
1863 		return (EBUSY);
1864 	return (0);
1865 }
1866 
1867 /*
1868  * Unmount all filesystems. The list is traversed in reverse order
1869  * of mounting to avoid dependencies.
1870  *
1871  * We want the umountall to be able to break out of its loop if a
1872  * failure occurs, after scanning all possible mounts, so the callback
1873  * returns 0 on error.
1874  *
1875  * NOTE: Do not call mountlist_remove(mp) on error any more, this will
1876  *	 confuse mountlist_scan()'s unbusy check.
1877  */
1878 static int vfs_umountall_callback(struct mount *mp, void *data);
1879 
1880 void
1881 vfs_unmountall(int halting)
1882 {
1883 	int count;
1884 
1885 	do {
1886 		count = mountlist_scan(vfs_umountall_callback, &halting,
1887 				       MNTSCAN_REVERSE|MNTSCAN_NOBUSY);
1888 	} while (count);
1889 }
1890 
1891 static
1892 int
1893 vfs_umountall_callback(struct mount *mp, void *data)
1894 {
1895 	int error;
1896 	int halting = *(int *)data;
1897 
1898 	/*
1899 	 * NOTE: When halting, dounmount will disconnect but leave
1900 	 *	 certain mount points intact.  e.g. devfs.
1901 	 */
1902 	error = dounmount(mp, MNT_FORCE, halting);
1903 	if (error) {
1904 		kprintf("unmount of filesystem mounted from %s failed (",
1905 			mp->mnt_stat.f_mntfromname);
1906 		if (error == EBUSY)
1907 			kprintf("BUSY)\n");
1908 		else
1909 			kprintf("%d)\n", error);
1910 		return 0;
1911 	} else {
1912 		return 1;
1913 	}
1914 }
1915 
1916 /*
1917  * Checks the mount flags for parameter mp and put the names comma-separated
1918  * into a string buffer buf with a size limit specified by len.
1919  *
1920  * It returns the number of bytes written into buf, and (*errorp) will be
1921  * set to 0, EINVAL (if passed length is 0), or ENOSPC (supplied buffer was
1922  * not large enough).  The buffer will be 0-terminated if len was not 0.
1923  */
1924 size_t
1925 vfs_flagstostr(int flags, const struct mountctl_opt *optp,
1926 	       char *buf, size_t len, int *errorp)
1927 {
1928 	static const struct mountctl_opt optnames[] = {
1929 		{ MNT_RDONLY,           "read-only" },
1930 		{ MNT_SYNCHRONOUS,      "synchronous" },
1931 		{ MNT_NOEXEC,           "noexec" },
1932 		{ MNT_NOSUID,           "nosuid" },
1933 		{ MNT_NODEV,            "nodev" },
1934 		{ MNT_AUTOMOUNTED,      "automounted" },
1935 		{ MNT_ASYNC,            "asynchronous" },
1936 		{ MNT_SUIDDIR,          "suiddir" },
1937 		{ MNT_SOFTDEP,          "soft-updates" },
1938 		{ MNT_NOSYMFOLLOW,      "nosymfollow" },
1939 		{ MNT_TRIM,             "trim" },
1940 		{ MNT_NOATIME,          "noatime" },
1941 		{ MNT_NOCLUSTERR,       "noclusterr" },
1942 		{ MNT_NOCLUSTERW,       "noclusterw" },
1943 		{ MNT_EXRDONLY,         "NFS read-only" },
1944 		{ MNT_EXPORTED,         "NFS exported" },
1945 		/* Remaining NFS flags could come here */
1946 		{ MNT_LOCAL,            "local" },
1947 		{ MNT_QUOTA,            "with-quotas" },
1948 		/* { MNT_ROOTFS,           "rootfs" }, */
1949 		/* { MNT_IGNORE,           "ignore" }, */
1950 		{ 0,			NULL}
1951 	};
1952 	int bwritten;
1953 	int bleft;
1954 	int optlen;
1955 	int actsize;
1956 
1957 	*errorp = 0;
1958 	bwritten = 0;
1959 	bleft = len - 1;	/* leave room for trailing \0 */
1960 
1961 	/*
1962 	 * Checks the size of the string. If it contains
1963 	 * any data, then we will append the new flags to
1964 	 * it.
1965 	 */
1966 	actsize = strlen(buf);
1967 	if (actsize > 0)
1968 		buf += actsize;
1969 
1970 	/* Default flags if no flags passed */
1971 	if (optp == NULL)
1972 		optp = optnames;
1973 
1974 	if (bleft < 0) {	/* degenerate case, 0-length buffer */
1975 		*errorp = EINVAL;
1976 		return(0);
1977 	}
1978 
1979 	for (; flags && optp->o_opt; ++optp) {
1980 		if ((flags & optp->o_opt) == 0)
1981 			continue;
1982 		optlen = strlen(optp->o_name);
1983 		if (bwritten || actsize > 0) {
1984 			if (bleft < 2) {
1985 				*errorp = ENOSPC;
1986 				break;
1987 			}
1988 			buf[bwritten++] = ',';
1989 			buf[bwritten++] = ' ';
1990 			bleft -= 2;
1991 		}
1992 		if (bleft < optlen) {
1993 			*errorp = ENOSPC;
1994 			break;
1995 		}
1996 		bcopy(optp->o_name, buf + bwritten, optlen);
1997 		bwritten += optlen;
1998 		bleft -= optlen;
1999 		flags &= ~optp->o_opt;
2000 	}
2001 
2002 	/*
2003 	 * Space already reserved for trailing \0
2004 	 */
2005 	buf[bwritten] = 0;
2006 	return (bwritten);
2007 }
2008 
2009 /*
2010  * Build hash lists of net addresses and hang them off the mount point.
2011  * Called by ufs_mount() to set up the lists of export addresses.
2012  */
2013 static int
2014 vfs_hang_addrlist(struct mount *mp, struct netexport *nep,
2015 		const struct export_args *argp)
2016 {
2017 	struct netcred *np;
2018 	struct radix_node_head *rnh;
2019 	int i;
2020 	struct radix_node *rn;
2021 	struct sockaddr *saddr, *smask = NULL;
2022 	int error;
2023 
2024 	if (argp->ex_addrlen == 0) {
2025 		if (mp->mnt_flag & MNT_DEFEXPORTED)
2026 			return (EPERM);
2027 		np = &nep->ne_defexported;
2028 		np->netc_exflags = argp->ex_flags;
2029 		np->netc_anon = argp->ex_anon;
2030 		np->netc_anon.cr_ref = 1;
2031 		mp->mnt_flag |= MNT_DEFEXPORTED;
2032 		return (0);
2033 	}
2034 
2035 	if (argp->ex_addrlen < 0 || argp->ex_addrlen > MLEN)
2036 		return (EINVAL);
2037 	if (argp->ex_masklen < 0 || argp->ex_masklen > MLEN)
2038 		return (EINVAL);
2039 
2040 	i = sizeof(struct netcred) + argp->ex_addrlen + argp->ex_masklen;
2041 	np = (struct netcred *)kmalloc(i, M_NETCRED, M_WAITOK | M_ZERO);
2042 	saddr = (struct sockaddr *) (np + 1);
2043 	if ((error = copyin(argp->ex_addr, (caddr_t) saddr, argp->ex_addrlen)))
2044 		goto out;
2045 	if (saddr->sa_len > argp->ex_addrlen)
2046 		saddr->sa_len = argp->ex_addrlen;
2047 	if (argp->ex_masklen) {
2048 		smask = (struct sockaddr *)((caddr_t)saddr + argp->ex_addrlen);
2049 		error = copyin(argp->ex_mask, (caddr_t)smask, argp->ex_masklen);
2050 		if (error)
2051 			goto out;
2052 		if (smask->sa_len > argp->ex_masklen)
2053 			smask->sa_len = argp->ex_masklen;
2054 	}
2055 	NE_LOCK(nep);
2056 	if (nep->ne_maskhead == NULL) {
2057 		if (!rn_inithead((void **)&nep->ne_maskhead, NULL, 0)) {
2058 			error = ENOBUFS;
2059 			goto out;
2060 		}
2061 	}
2062 	if ((rnh = vfs_create_addrlist_af(saddr->sa_family, nep)) == NULL) {
2063 		error = ENOBUFS;
2064 		goto out;
2065 	}
2066 	rn = (*rnh->rnh_addaddr)((char *)saddr, (char *)smask, rnh,
2067 				 np->netc_rnodes);
2068 	NE_UNLOCK(nep);
2069 	if (rn == NULL || np != (struct netcred *)rn) {	/* already exists */
2070 		error = EPERM;
2071 		goto out;
2072 	}
2073 	np->netc_exflags = argp->ex_flags;
2074 	np->netc_anon = argp->ex_anon;
2075 	np->netc_anon.cr_ref = 1;
2076 	return (0);
2077 
2078 out:
2079 	kfree(np, M_NETCRED);
2080 	return (error);
2081 }
2082 
2083 /*
2084  * Free netcred structures installed in the netexport
2085  */
2086 static int
2087 vfs_free_netcred(struct radix_node *rn, void *w)
2088 {
2089 	struct radix_node_head *rnh = (struct radix_node_head *)w;
2090 
2091 	(*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, rnh);
2092 	kfree(rn, M_NETCRED);
2093 
2094 	return (0);
2095 }
2096 
2097 /*
2098  * callback to free an element of the mask table installed in the
2099  * netexport.  These may be created indirectly and are not netcred
2100  * structures.
2101  */
2102 static int
2103 vfs_free_netcred_mask(struct radix_node *rn, void *w)
2104 {
2105 	struct radix_node_head *rnh = (struct radix_node_head *)w;
2106 
2107 	(*rnh->rnh_deladdr) (rn->rn_key, rn->rn_mask, rnh);
2108 	kfree(rn, M_RTABLE);
2109 
2110 	return (0);
2111 }
2112 
2113 static struct radix_node_head *
2114 vfs_create_addrlist_af(int af, struct netexport *nep)
2115 {
2116 	struct radix_node_head *rnh = NULL;
2117 #if defined(INET) || defined(INET6)
2118 	struct radix_node_head *maskhead = nep->ne_maskhead;
2119 	int off;
2120 #endif
2121 
2122 	NE_ASSERT_LOCKED(nep);
2123 #if defined(INET) || defined(INET6)
2124 	KKASSERT(maskhead != NULL);
2125 #endif
2126 	switch (af) {
2127 #ifdef INET
2128 	case AF_INET:
2129 		if ((rnh = nep->ne_inethead) == NULL) {
2130 			off = offsetof(struct sockaddr_in, sin_addr) << 3;
2131 			if (!rn_inithead((void **)&rnh, maskhead, off))
2132 				return (NULL);
2133 			nep->ne_inethead = rnh;
2134 		}
2135 		break;
2136 #endif
2137 #ifdef INET6
2138 	case AF_INET6:
2139 		if ((rnh = nep->ne_inet6head) == NULL) {
2140 			off = offsetof(struct sockaddr_in6, sin6_addr) << 3;
2141 			if (!rn_inithead((void **)&rnh, maskhead, off))
2142 				return (NULL);
2143 			nep->ne_inet6head = rnh;
2144 		}
2145 		break;
2146 #endif
2147 	}
2148 	return (rnh);
2149 }
2150 
2151 /*
2152  * helper function for freeing netcred elements
2153  */
2154 static void
2155 vfs_free_addrlist_af(struct radix_node_head **prnh)
2156 {
2157 	struct radix_node_head *rnh = *prnh;
2158 
2159 	(*rnh->rnh_walktree) (rnh, vfs_free_netcred, rnh);
2160 	kfree(rnh, M_RTABLE);
2161 	*prnh = NULL;
2162 }
2163 
2164 /*
2165  * helper function for freeing mask elements
2166  */
2167 static void
2168 vfs_free_addrlist_masks(struct radix_node_head **prnh)
2169 {
2170 	struct radix_node_head *rnh = *prnh;
2171 
2172 	(*rnh->rnh_walktree) (rnh, vfs_free_netcred_mask, rnh);
2173 	kfree(rnh, M_RTABLE);
2174 	*prnh = NULL;
2175 }
2176 
2177 /*
2178  * Free the net address hash lists that are hanging off the mount points.
2179  */
2180 static void
2181 vfs_free_addrlist(struct netexport *nep)
2182 {
2183 	NE_LOCK(nep);
2184 	if (nep->ne_inethead != NULL)
2185 		vfs_free_addrlist_af(&nep->ne_inethead);
2186 	if (nep->ne_inet6head != NULL)
2187 		vfs_free_addrlist_af(&nep->ne_inet6head);
2188 	if (nep->ne_maskhead)
2189 		vfs_free_addrlist_masks(&nep->ne_maskhead);
2190 	NE_UNLOCK(nep);
2191 }
2192 
2193 int
2194 vfs_export(struct mount *mp, struct netexport *nep,
2195 	   const struct export_args *argp)
2196 {
2197 	int error;
2198 
2199 	if (argp->ex_flags & MNT_DELEXPORT) {
2200 		if (mp->mnt_flag & MNT_EXPUBLIC) {
2201 			vfs_setpublicfs(NULL, NULL, NULL);
2202 			mp->mnt_flag &= ~MNT_EXPUBLIC;
2203 		}
2204 		vfs_free_addrlist(nep);
2205 		mp->mnt_flag &= ~(MNT_EXPORTED | MNT_DEFEXPORTED);
2206 	}
2207 	if (argp->ex_flags & MNT_EXPORTED) {
2208 		if (argp->ex_flags & MNT_EXPUBLIC) {
2209 			if ((error = vfs_setpublicfs(mp, nep, argp)) != 0)
2210 				return (error);
2211 			mp->mnt_flag |= MNT_EXPUBLIC;
2212 		}
2213 		if ((error = vfs_hang_addrlist(mp, nep, argp)))
2214 			return (error);
2215 		mp->mnt_flag |= MNT_EXPORTED;
2216 	}
2217 	return (0);
2218 }
2219 
2220 
2221 /*
2222  * Set the publicly exported filesystem (WebNFS). Currently, only
2223  * one public filesystem is possible in the spec (RFC 2054 and 2055)
2224  */
2225 int
2226 vfs_setpublicfs(struct mount *mp, struct netexport *nep,
2227 		const struct export_args *argp)
2228 {
2229 	int error;
2230 	struct vnode *rvp;
2231 	char *cp;
2232 
2233 	/*
2234 	 * mp == NULL -> invalidate the current info, the FS is
2235 	 * no longer exported. May be called from either vfs_export
2236 	 * or unmount, so check if it hasn't already been done.
2237 	 */
2238 	if (mp == NULL) {
2239 		if (nfs_pub.np_valid) {
2240 			nfs_pub.np_valid = 0;
2241 			if (nfs_pub.np_index != NULL) {
2242 				kfree(nfs_pub.np_index, M_TEMP);
2243 				nfs_pub.np_index = NULL;
2244 			}
2245 		}
2246 		return (0);
2247 	}
2248 
2249 	/*
2250 	 * Only one allowed at a time.
2251 	 */
2252 	if (nfs_pub.np_valid != 0 && mp != nfs_pub.np_mount)
2253 		return (EBUSY);
2254 
2255 	/*
2256 	 * Get real filehandle for root of exported FS.
2257 	 */
2258 	bzero((caddr_t)&nfs_pub.np_handle, sizeof(nfs_pub.np_handle));
2259 	nfs_pub.np_handle.fh_fsid = mp->mnt_stat.f_fsid;
2260 
2261 	if ((error = VFS_ROOT(mp, &rvp)))
2262 		return (error);
2263 
2264 	if ((error = VFS_VPTOFH(rvp, &nfs_pub.np_handle.fh_fid)))
2265 		return (error);
2266 
2267 	vput(rvp);
2268 
2269 	/*
2270 	 * If an indexfile was specified, pull it in.
2271 	 */
2272 	if (argp->ex_indexfile != NULL) {
2273 		int namelen;
2274 
2275 		error = vn_get_namelen(rvp, &namelen);
2276 		if (error)
2277 			return (error);
2278 		nfs_pub.np_index = kmalloc(namelen, M_TEMP, M_WAITOK);
2279 		error = copyinstr(argp->ex_indexfile, nfs_pub.np_index,
2280 		    namelen, NULL);
2281 		if (!error) {
2282 			/*
2283 			 * Check for illegal filenames.
2284 			 */
2285 			for (cp = nfs_pub.np_index; *cp; cp++) {
2286 				if (*cp == '/') {
2287 					error = EINVAL;
2288 					break;
2289 				}
2290 			}
2291 		}
2292 		if (error) {
2293 			kfree(nfs_pub.np_index, M_TEMP);
2294 			return (error);
2295 		}
2296 	}
2297 
2298 	nfs_pub.np_mount = mp;
2299 	nfs_pub.np_valid = 1;
2300 	return (0);
2301 }
2302 
2303 struct netcred *
2304 vfs_export_lookup(struct mount *mp, struct netexport *nep,
2305 		struct sockaddr *nam)
2306 {
2307 	struct netcred *np;
2308 	struct radix_node_head *rnh;
2309 	struct sockaddr *saddr;
2310 
2311 	np = NULL;
2312 	if (mp->mnt_flag & MNT_EXPORTED) {
2313 		/*
2314 		 * Lookup in the export list first.
2315 		 */
2316 		NE_LOCK(nep);
2317 		if (nam != NULL) {
2318 			saddr = nam;
2319 			switch (saddr->sa_family) {
2320 #ifdef INET
2321 			case AF_INET:
2322 				rnh = nep->ne_inethead;
2323 				break;
2324 #endif
2325 #ifdef INET6
2326 			case AF_INET6:
2327 				rnh = nep->ne_inet6head;
2328 				break;
2329 #endif
2330 			default:
2331 				rnh = NULL;
2332 			}
2333 			if (rnh != NULL) {
2334 				np = (struct netcred *)
2335 					(*rnh->rnh_matchaddr)((char *)saddr,
2336 							      rnh);
2337 				if (np && np->netc_rnodes->rn_flags & RNF_ROOT)
2338 					np = NULL;
2339 			}
2340 		}
2341 		NE_UNLOCK(nep);
2342 		/*
2343 		 * If no address match, use the default if it exists.
2344 		 */
2345 		if (np == NULL && mp->mnt_flag & MNT_DEFEXPORTED)
2346 			np = &nep->ne_defexported;
2347 	}
2348 	return (np);
2349 }
2350 
2351 /*
2352  * perform msync on all vnodes under a mount point.  The mount point must
2353  * be locked.  This code is also responsible for lazy-freeing unreferenced
2354  * vnodes whos VM objects no longer contain pages.
2355  *
2356  * NOTE: MNT_WAIT still skips vnodes in the VXLOCK state.
2357  *
2358  * NOTE: XXX VOP_PUTPAGES and friends requires that the vnode be locked,
2359  * but vnode_pager_putpages() doesn't lock the vnode.  We have to do it
2360  * way up in this high level function.
2361  */
2362 static int vfs_msync_scan1(struct mount *mp, struct vnode *vp, void *data);
2363 static int vfs_msync_scan2(struct mount *mp, struct vnode *vp, void *data);
2364 
2365 void
2366 vfs_msync(struct mount *mp, int flags)
2367 {
2368 	int vmsc_flags;
2369 
2370 	/*
2371 	 * tmpfs sets this flag to prevent msync(), sync, and the
2372 	 * filesystem periodic syncer from trying to flush VM pages
2373 	 * to swap.  Only pure memory pressure flushes tmpfs VM pages
2374 	 * to swap.
2375 	 */
2376 	if (mp->mnt_kern_flag & MNTK_NOMSYNC)
2377 		return;
2378 
2379 	/*
2380 	 * Ok, scan the vnodes for work.  If the filesystem is using the
2381 	 * syncer thread feature we can use vsyncscan() instead of
2382 	 * vmntvnodescan(), which is much faster.
2383 	 */
2384 	vmsc_flags = VMSC_GETVP;
2385 	if (flags != MNT_WAIT)
2386 		vmsc_flags |= VMSC_NOWAIT;
2387 
2388 	if (mp->mnt_kern_flag & MNTK_THR_SYNC) {
2389 		vsyncscan(mp, vmsc_flags, vfs_msync_scan2,
2390 			  (void *)(intptr_t)flags);
2391 	} else {
2392 		vmntvnodescan(mp, vmsc_flags,
2393 			      vfs_msync_scan1, vfs_msync_scan2,
2394 			      (void *)(intptr_t)flags);
2395 	}
2396 }
2397 
2398 /*
2399  * scan1 is a fast pre-check.  There could be hundreds of thousands of
2400  * vnodes, we cannot afford to do anything heavy weight until we have a
2401  * fairly good indication that there is work to do.
2402  */
2403 static
2404 int
2405 vfs_msync_scan1(struct mount *mp, struct vnode *vp, void *data)
2406 {
2407 	int flags = (int)(intptr_t)data;
2408 
2409 	if ((vp->v_flag & VRECLAIMED) == 0) {
2410 		if (vp->v_auxrefs == 0 && VREFCNT(vp) <= 0 &&
2411 		    vp->v_object) {
2412 			return(0);	/* call scan2 */
2413 		}
2414 		if ((mp->mnt_flag & MNT_RDONLY) == 0 &&
2415 		    (vp->v_flag & VOBJDIRTY) &&
2416 		    (flags == MNT_WAIT || vn_islocked(vp) == 0)) {
2417 			return(0);	/* call scan2 */
2418 		}
2419 	}
2420 
2421 	/*
2422 	 * do not call scan2, continue the loop
2423 	 */
2424 	return(-1);
2425 }
2426 
2427 /*
2428  * This callback is handed a locked vnode.
2429  */
2430 static
2431 int
2432 vfs_msync_scan2(struct mount *mp, struct vnode *vp, void *data)
2433 {
2434 	vm_object_t obj;
2435 	int flags = (int)(intptr_t)data;
2436 
2437 	if (vp->v_flag & VRECLAIMED)
2438 		return(0);
2439 
2440 	if ((mp->mnt_flag & MNT_RDONLY) == 0 && (vp->v_flag & VOBJDIRTY)) {
2441 		if ((obj = vp->v_object) != NULL) {
2442 			vm_object_page_clean(obj, 0, 0,
2443 			 flags == MNT_WAIT ? OBJPC_SYNC : OBJPC_NOSYNC);
2444 		}
2445 	}
2446 	return(0);
2447 }
2448 
2449 /*
2450  * Wake up anyone interested in vp because it is being revoked.
2451  */
2452 void
2453 vn_gone(struct vnode *vp)
2454 {
2455 	lwkt_gettoken(&vp->v_token);
2456 	KNOTE(&vp->v_pollinfo.vpi_kqinfo.ki_note, NOTE_REVOKE);
2457 	lwkt_reltoken(&vp->v_token);
2458 }
2459 
2460 /*
2461  * extract the cdev_t from a VBLK or VCHR.  The vnode must have been opened
2462  * (or v_rdev might be NULL).
2463  */
2464 cdev_t
2465 vn_todev(struct vnode *vp)
2466 {
2467 	if (vp->v_type != VBLK && vp->v_type != VCHR)
2468 		return (NULL);
2469 	KKASSERT(vp->v_rdev != NULL);
2470 	return (vp->v_rdev);
2471 }
2472 
2473 /*
2474  * Check if vnode represents a disk device.  The vnode does not need to be
2475  * opened.
2476  *
2477  * MPALMOSTSAFE
2478  */
2479 int
2480 vn_isdisk(struct vnode *vp, int *errp)
2481 {
2482 	cdev_t dev;
2483 
2484 	if (vp->v_type != VCHR) {
2485 		if (errp != NULL)
2486 			*errp = ENOTBLK;
2487 		return (0);
2488 	}
2489 
2490 	dev = vp->v_rdev;
2491 
2492 	if (dev == NULL) {
2493 		if (errp != NULL)
2494 			*errp = ENXIO;
2495 		return (0);
2496 	}
2497 	if (dev_is_good(dev) == 0) {
2498 		if (errp != NULL)
2499 			*errp = ENXIO;
2500 		return (0);
2501 	}
2502 	if ((dev_dflags(dev) & D_DISK) == 0) {
2503 		if (errp != NULL)
2504 			*errp = ENOTBLK;
2505 		return (0);
2506 	}
2507 	if (errp != NULL)
2508 		*errp = 0;
2509 	return (1);
2510 }
2511 
2512 int
2513 vn_get_namelen(struct vnode *vp, int *namelen)
2514 {
2515 	int error;
2516 	register_t retval[2];
2517 
2518 	error = VOP_PATHCONF(vp, _PC_NAME_MAX, retval);
2519 	if (error)
2520 		return (error);
2521 	*namelen = (int)retval[0];
2522 	return (0);
2523 }
2524 
2525 int
2526 vop_write_dirent(int *error, struct uio *uio, ino_t d_ino, uint8_t d_type,
2527 		uint16_t d_namlen, const char *d_name)
2528 {
2529 	struct dirent *dp;
2530 	size_t len;
2531 
2532 	len = _DIRENT_RECLEN(d_namlen);
2533 	if (len > uio->uio_resid)
2534 		return(1);
2535 
2536 	dp = kmalloc(len, M_TEMP, M_WAITOK | M_ZERO);
2537 
2538 	dp->d_ino = d_ino;
2539 	dp->d_namlen = d_namlen;
2540 	dp->d_type = d_type;
2541 	bcopy(d_name, dp->d_name, d_namlen);
2542 
2543 	*error = uiomove((caddr_t)dp, len, uio);
2544 
2545 	kfree(dp, M_TEMP);
2546 
2547 	return(0);
2548 }
2549 
2550 void
2551 vn_mark_atime(struct vnode *vp, struct thread *td)
2552 {
2553 	struct proc *p = td->td_proc;
2554 	struct ucred *cred = p ? p->p_ucred : proc0.p_ucred;
2555 
2556 	if ((vp->v_mount->mnt_flag & (MNT_NOATIME | MNT_RDONLY)) == 0) {
2557 		VOP_MARKATIME(vp, cred);
2558 	}
2559 }
2560 
2561 /*
2562  * Calculate the number of entries in an inode-related chained hash table.
2563  * With today's memory sizes, maxvnodes can wind up being a very large
2564  * number.  There is no reason to waste memory, so tolerate some stacking.
2565  */
2566 int
2567 vfs_inodehashsize(void)
2568 {
2569 	int hsize;
2570 
2571 	hsize = 32;
2572 	while (hsize < maxvnodes)
2573 		hsize <<= 1;
2574 	while (hsize > maxvnodes * 2)
2575 		hsize >>= 1;		/* nominal 2x stacking */
2576 
2577 	if (maxvnodes > 1024 * 1024)
2578 		hsize >>= 1;		/* nominal 8x stacking */
2579 
2580 	if (maxvnodes > 128 * 1024)
2581 		hsize >>= 1;		/* nominal 4x stacking */
2582 
2583 	if (hsize < 16)
2584 		hsize = 16;
2585 
2586 	return hsize;
2587 }
2588