xref: /dragonfly/sys/vfs/hammer2/hammer2_vfsops.c (revision 5a08817b)
1 /*
2  * Copyright (c) 2011-2015 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@backplane.com>
6  * by Daniel Flores (GSOC 2013 - mentored by Matthew Dillon, compression)
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the
17  *    distribution.
18  * 3. Neither the name of The DragonFly Project nor the names of its
19  *    contributors may be used to endorse or promote products derived
20  *    from this software without specific, prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
26  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/nlookup.h>
39 #include <sys/vnode.h>
40 #include <sys/mount.h>
41 #include <sys/fcntl.h>
42 #include <sys/buf.h>
43 #include <sys/uuid.h>
44 #include <sys/vfsops.h>
45 #include <sys/sysctl.h>
46 #include <sys/socket.h>
47 #include <sys/objcache.h>
48 
49 #include <sys/proc.h>
50 #include <sys/namei.h>
51 #include <sys/mountctl.h>
52 #include <sys/dirent.h>
53 #include <sys/uio.h>
54 
55 #include <sys/mutex.h>
56 #include <sys/mutex2.h>
57 
58 #include "hammer2.h"
59 #include "hammer2_disk.h"
60 #include "hammer2_mount.h"
61 #include "hammer2_lz4.h"
62 
63 #include "zlib/hammer2_zlib.h"
64 
65 #define REPORT_REFS_ERRORS 1	/* XXX remove me */
66 
67 MALLOC_DEFINE(M_OBJCACHE, "objcache", "Object Cache");
68 
69 struct hammer2_sync_info {
70 	int error;
71 	int waitfor;
72 };
73 
74 TAILQ_HEAD(hammer2_mntlist, hammer2_dev);
75 TAILQ_HEAD(hammer2_pfslist, hammer2_pfs);
76 static struct hammer2_mntlist hammer2_mntlist;
77 static struct hammer2_pfslist hammer2_pfslist;
78 static struct lock hammer2_mntlk;
79 
80 int hammer2_debug;
81 int hammer2_cluster_enable = 1;
82 int hammer2_hardlink_enable = 1;
83 int hammer2_flush_pipe = 100;
84 int hammer2_synchronous_flush = 1;
85 int hammer2_dio_count;
86 long hammer2_limit_dirty_chains;
87 long hammer2_iod_file_read;
88 long hammer2_iod_meta_read;
89 long hammer2_iod_indr_read;
90 long hammer2_iod_fmap_read;
91 long hammer2_iod_volu_read;
92 long hammer2_iod_file_write;
93 long hammer2_iod_file_wembed;
94 long hammer2_iod_file_wzero;
95 long hammer2_iod_file_wdedup;
96 long hammer2_iod_meta_write;
97 long hammer2_iod_indr_write;
98 long hammer2_iod_fmap_write;
99 long hammer2_iod_volu_write;
100 long hammer2_ioa_file_read;
101 long hammer2_ioa_meta_read;
102 long hammer2_ioa_indr_read;
103 long hammer2_ioa_fmap_read;
104 long hammer2_ioa_volu_read;
105 long hammer2_ioa_fmap_write;
106 long hammer2_ioa_file_write;
107 long hammer2_ioa_meta_write;
108 long hammer2_ioa_indr_write;
109 long hammer2_ioa_volu_write;
110 
111 MALLOC_DECLARE(M_HAMMER2_CBUFFER);
112 MALLOC_DEFINE(M_HAMMER2_CBUFFER, "HAMMER2-compbuffer",
113 		"Buffer used for compression.");
114 
115 MALLOC_DECLARE(M_HAMMER2_DEBUFFER);
116 MALLOC_DEFINE(M_HAMMER2_DEBUFFER, "HAMMER2-decompbuffer",
117 		"Buffer used for decompression.");
118 
119 SYSCTL_NODE(_vfs, OID_AUTO, hammer2, CTLFLAG_RW, 0, "HAMMER2 filesystem");
120 
121 SYSCTL_INT(_vfs_hammer2, OID_AUTO, debug, CTLFLAG_RW,
122 	   &hammer2_debug, 0, "");
123 SYSCTL_INT(_vfs_hammer2, OID_AUTO, cluster_enable, CTLFLAG_RW,
124 	   &hammer2_cluster_enable, 0, "");
125 SYSCTL_INT(_vfs_hammer2, OID_AUTO, hardlink_enable, CTLFLAG_RW,
126 	   &hammer2_hardlink_enable, 0, "");
127 SYSCTL_INT(_vfs_hammer2, OID_AUTO, flush_pipe, CTLFLAG_RW,
128 	   &hammer2_flush_pipe, 0, "");
129 SYSCTL_INT(_vfs_hammer2, OID_AUTO, synchronous_flush, CTLFLAG_RW,
130 	   &hammer2_synchronous_flush, 0, "");
131 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, limit_dirty_chains, CTLFLAG_RW,
132 	   &hammer2_limit_dirty_chains, 0, "");
133 SYSCTL_INT(_vfs_hammer2, OID_AUTO, dio_count, CTLFLAG_RD,
134 	   &hammer2_dio_count, 0, "");
135 
136 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_read, CTLFLAG_RW,
137 	   &hammer2_iod_file_read, 0, "");
138 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_meta_read, CTLFLAG_RW,
139 	   &hammer2_iod_meta_read, 0, "");
140 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_indr_read, CTLFLAG_RW,
141 	   &hammer2_iod_indr_read, 0, "");
142 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_fmap_read, CTLFLAG_RW,
143 	   &hammer2_iod_fmap_read, 0, "");
144 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_volu_read, CTLFLAG_RW,
145 	   &hammer2_iod_volu_read, 0, "");
146 
147 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_write, CTLFLAG_RW,
148 	   &hammer2_iod_file_write, 0, "");
149 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_wembed, CTLFLAG_RW,
150 	   &hammer2_iod_file_wembed, 0, "");
151 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_wzero, CTLFLAG_RW,
152 	   &hammer2_iod_file_wzero, 0, "");
153 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_wdedup, CTLFLAG_RW,
154 	   &hammer2_iod_file_wdedup, 0, "");
155 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_meta_write, CTLFLAG_RW,
156 	   &hammer2_iod_meta_write, 0, "");
157 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_indr_write, CTLFLAG_RW,
158 	   &hammer2_iod_indr_write, 0, "");
159 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_fmap_write, CTLFLAG_RW,
160 	   &hammer2_iod_fmap_write, 0, "");
161 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_volu_write, CTLFLAG_RW,
162 	   &hammer2_iod_volu_write, 0, "");
163 
164 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_file_read, CTLFLAG_RW,
165 	   &hammer2_ioa_file_read, 0, "");
166 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_meta_read, CTLFLAG_RW,
167 	   &hammer2_ioa_meta_read, 0, "");
168 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_indr_read, CTLFLAG_RW,
169 	   &hammer2_ioa_indr_read, 0, "");
170 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_fmap_read, CTLFLAG_RW,
171 	   &hammer2_ioa_fmap_read, 0, "");
172 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_volu_read, CTLFLAG_RW,
173 	   &hammer2_ioa_volu_read, 0, "");
174 
175 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_file_write, CTLFLAG_RW,
176 	   &hammer2_ioa_file_write, 0, "");
177 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_meta_write, CTLFLAG_RW,
178 	   &hammer2_ioa_meta_write, 0, "");
179 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_indr_write, CTLFLAG_RW,
180 	   &hammer2_ioa_indr_write, 0, "");
181 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_fmap_write, CTLFLAG_RW,
182 	   &hammer2_ioa_fmap_write, 0, "");
183 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_volu_write, CTLFLAG_RW,
184 	   &hammer2_ioa_volu_write, 0, "");
185 
186 static int hammer2_vfs_init(struct vfsconf *conf);
187 static int hammer2_vfs_uninit(struct vfsconf *vfsp);
188 static int hammer2_vfs_mount(struct mount *mp, char *path, caddr_t data,
189 				struct ucred *cred);
190 static int hammer2_remount(hammer2_dev_t *, struct mount *, char *,
191 				struct vnode *, struct ucred *);
192 static int hammer2_recovery(hammer2_dev_t *hmp);
193 static int hammer2_vfs_unmount(struct mount *mp, int mntflags);
194 static int hammer2_vfs_root(struct mount *mp, struct vnode **vpp);
195 static int hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp,
196 				struct ucred *cred);
197 static int hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp,
198 				struct ucred *cred);
199 static int hammer2_vfs_vget(struct mount *mp, struct vnode *dvp,
200 				ino_t ino, struct vnode **vpp);
201 static int hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
202 				struct fid *fhp, struct vnode **vpp);
203 static int hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp);
204 static int hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
205 				int *exflagsp, struct ucred **credanonp);
206 
207 static int hammer2_install_volume_header(hammer2_dev_t *hmp);
208 static int hammer2_sync_scan2(struct mount *mp, struct vnode *vp, void *data);
209 
210 static void hammer2_update_pmps(hammer2_dev_t *hmp);
211 
212 static void hammer2_mount_helper(struct mount *mp, hammer2_pfs_t *pmp);
213 static void hammer2_unmount_helper(struct mount *mp, hammer2_pfs_t *pmp,
214 				hammer2_dev_t *hmp);
215 
216 /*
217  * HAMMER2 vfs operations.
218  */
219 static struct vfsops hammer2_vfsops = {
220 	.vfs_init	= hammer2_vfs_init,
221 	.vfs_uninit	= hammer2_vfs_uninit,
222 	.vfs_sync	= hammer2_vfs_sync,
223 	.vfs_mount	= hammer2_vfs_mount,
224 	.vfs_unmount	= hammer2_vfs_unmount,
225 	.vfs_root 	= hammer2_vfs_root,
226 	.vfs_statfs	= hammer2_vfs_statfs,
227 	.vfs_statvfs	= hammer2_vfs_statvfs,
228 	.vfs_vget	= hammer2_vfs_vget,
229 	.vfs_vptofh	= hammer2_vfs_vptofh,
230 	.vfs_fhtovp	= hammer2_vfs_fhtovp,
231 	.vfs_checkexp	= hammer2_vfs_checkexp
232 };
233 
234 MALLOC_DEFINE(M_HAMMER2, "HAMMER2-mount", "");
235 
236 VFS_SET(hammer2_vfsops, hammer2, 0);
237 MODULE_VERSION(hammer2, 1);
238 
239 static
240 int
241 hammer2_vfs_init(struct vfsconf *conf)
242 {
243 	static struct objcache_malloc_args margs_read;
244 	static struct objcache_malloc_args margs_write;
245 	static struct objcache_malloc_args margs_vop;
246 
247 	int error;
248 
249 	error = 0;
250 
251 	if (HAMMER2_BLOCKREF_BYTES != sizeof(struct hammer2_blockref))
252 		error = EINVAL;
253 	if (HAMMER2_INODE_BYTES != sizeof(struct hammer2_inode_data))
254 		error = EINVAL;
255 	if (HAMMER2_VOLUME_BYTES != sizeof(struct hammer2_volume_data))
256 		error = EINVAL;
257 
258 	if (error)
259 		kprintf("HAMMER2 structure size mismatch; cannot continue.\n");
260 
261 	margs_read.objsize = 65536;
262 	margs_read.mtype = M_HAMMER2_DEBUFFER;
263 
264 	margs_write.objsize = 32768;
265 	margs_write.mtype = M_HAMMER2_CBUFFER;
266 
267 	margs_vop.objsize = sizeof(hammer2_xop_t);
268 	margs_vop.mtype = M_HAMMER2;
269 
270 	/*
271 	 * Note thaht for the XOPS cache we want backing store allocations
272 	 * to use M_ZERO.  This is not allowed in objcache_get() (to avoid
273 	 * confusion), so use the backing store function that does it.  This
274 	 * means that initial XOPS objects are zerod but REUSED objects are
275 	 * not.  So we are responsible for cleaning the object up sufficiently
276 	 * for our needs before objcache_put()ing it back (typically just the
277 	 * FIFO indices).
278 	 */
279 	cache_buffer_read = objcache_create(margs_read.mtype->ks_shortdesc,
280 				0, 1, NULL, NULL, NULL,
281 				objcache_malloc_alloc,
282 				objcache_malloc_free,
283 				&margs_read);
284 	cache_buffer_write = objcache_create(margs_write.mtype->ks_shortdesc,
285 				0, 1, NULL, NULL, NULL,
286 				objcache_malloc_alloc,
287 				objcache_malloc_free,
288 				&margs_write);
289 	cache_xops = objcache_create(margs_vop.mtype->ks_shortdesc,
290 				0, 1, NULL, NULL, NULL,
291 				objcache_malloc_alloc_zero,
292 				objcache_malloc_free,
293 				&margs_vop);
294 
295 
296 	lockinit(&hammer2_mntlk, "mntlk", 0, 0);
297 	TAILQ_INIT(&hammer2_mntlist);
298 	TAILQ_INIT(&hammer2_pfslist);
299 
300 	hammer2_limit_dirty_chains = desiredvnodes / 10;
301 
302 	return (error);
303 }
304 
305 static
306 int
307 hammer2_vfs_uninit(struct vfsconf *vfsp __unused)
308 {
309 	objcache_destroy(cache_buffer_read);
310 	objcache_destroy(cache_buffer_write);
311 	objcache_destroy(cache_xops);
312 	return 0;
313 }
314 
315 /*
316  * Core PFS allocator.  Used to allocate the pmp structure for PFS cluster
317  * mounts and the spmp structure for media (hmp) structures.
318  *
319  * pmp->modify_tid tracks new modify_tid transaction ids for front-end
320  * transactions.  Note that synchronization does not use this field.
321  * (typically frontend operations and synchronization cannot run on the
322  * same PFS node at the same time).
323  *
324  * XXX check locking
325  */
326 hammer2_pfs_t *
327 hammer2_pfsalloc(hammer2_chain_t *chain, const hammer2_inode_data_t *ripdata,
328 		 hammer2_tid_t modify_tid)
329 {
330 	hammer2_inode_t *iroot;
331 	hammer2_pfs_t *pmp;
332 	int count;
333 	int i;
334 	int j;
335 
336 	/*
337 	 * Locate or create the PFS based on the cluster id.  If ripdata
338 	 * is NULL this is a spmp which is unique and is always allocated.
339 	 */
340 	if (ripdata) {
341 		TAILQ_FOREACH(pmp, &hammer2_pfslist, mntentry) {
342 			if (bcmp(&pmp->pfs_clid, &ripdata->meta.pfs_clid,
343 				 sizeof(pmp->pfs_clid)) == 0) {
344 					break;
345 			}
346 		}
347 	} else {
348 		pmp = NULL;
349 	}
350 
351 	if (pmp == NULL) {
352 		pmp = kmalloc(sizeof(*pmp), M_HAMMER2, M_WAITOK | M_ZERO);
353 		hammer2_trans_manage_init(pmp);
354 		kmalloc_create(&pmp->minode, "HAMMER2-inodes");
355 		kmalloc_create(&pmp->mmsg, "HAMMER2-pfsmsg");
356 		lockinit(&pmp->lock, "pfslk", 0, 0);
357 		lockinit(&pmp->lock_nlink, "h2nlink", 0, 0);
358 		spin_init(&pmp->inum_spin, "hm2pfsalloc_inum");
359 		spin_init(&pmp->xop_spin, "h2xop");
360 		RB_INIT(&pmp->inum_tree);
361 		TAILQ_INIT(&pmp->sideq);
362 		spin_init(&pmp->list_spin, "hm2pfsalloc_list");
363 
364 		/*
365 		 * Distribute backend operations to threads
366 		 */
367 		for (j = 0; j < HAMMER2_MAXCLUSTER; ++j)
368 			TAILQ_INIT(&pmp->xopq[j]);
369 		for (j = 0; j < HAMMER2_XOPGROUPS; ++j)
370 			hammer2_xop_group_init(pmp, &pmp->xop_groups[j]);
371 
372 		/*
373 		 * Save the last media transaction id for the flusher.  Set
374 		 * initial
375 		 */
376 		if (ripdata)
377 			pmp->pfs_clid = ripdata->meta.pfs_clid;
378 		TAILQ_INSERT_TAIL(&hammer2_pfslist, pmp, mntentry);
379 
380 		/*
381 		 * The synchronization thread may start too early, make
382 		 * sure it stays frozen until we are ready to let it go.
383 		 * XXX
384 		 */
385 		/*
386 		pmp->primary_thr.flags = HAMMER2_THREAD_FROZEN |
387 					 HAMMER2_THREAD_REMASTER;
388 		*/
389 	}
390 
391 	/*
392 	 * Create the PFS's root inode.
393 	 */
394 	if ((iroot = pmp->iroot) == NULL) {
395 		iroot = hammer2_inode_get(pmp, NULL, NULL, -1);
396 		pmp->iroot = iroot;
397 		hammer2_inode_ref(iroot);
398 		hammer2_inode_unlock(iroot);
399 	}
400 
401 	/*
402 	 * Stop here if no chain is passed in.
403 	 */
404 	if (chain == NULL)
405 		goto done;
406 
407 	/*
408 	 * When a chain is passed in we must add it to the PFS's root
409 	 * inode, update pmp->pfs_types[], and update the syncronization
410 	 * threads.
411 	 *
412 	 * At the moment empty spots can develop due to removals or failures.
413 	 * Ultimately we want to re-fill these spots but doing so might
414 	 * confused running code. XXX
415 	 */
416 	hammer2_inode_ref(iroot);
417 	hammer2_mtx_ex(&iroot->lock);
418 	j = iroot->cluster.nchains;
419 
420 	kprintf("add PFS to pmp %p[%d]\n", pmp, j);
421 
422 	if (j == HAMMER2_MAXCLUSTER) {
423 		kprintf("hammer2_mount: cluster full!\n");
424 		/* XXX fatal error? */
425 	} else {
426 		KKASSERT(chain->pmp == NULL);
427 		chain->pmp = pmp;
428 		hammer2_chain_ref(chain);
429 		iroot->cluster.array[j].chain = chain;
430 		pmp->pfs_types[j] = ripdata->meta.pfs_type;
431 		pmp->pfs_names[j] = kstrdup(ripdata->filename, M_HAMMER2);
432 		pmp->pfs_hmps[j] = chain->hmp;
433 
434 		/*
435 		 * If the PFS is already mounted we must account
436 		 * for the mount_count here.
437 		 */
438 		if (pmp->mp)
439 			++chain->hmp->mount_count;
440 
441 		/*
442 		 * May have to fixup dirty chain tracking.  Previous
443 		 * pmp was NULL so nothing to undo.
444 		 */
445 		if (chain->flags & HAMMER2_CHAIN_MODIFIED)
446 			hammer2_pfs_memory_inc(pmp);
447 		++j;
448 	}
449 	iroot->cluster.nchains = j;
450 
451 	/*
452 	 * Update nmasters from any PFS inode which is part of the cluster.
453 	 * It is possible that this will result in a value which is too
454 	 * high.  MASTER PFSs are authoritative for pfs_nmasters and will
455 	 * override this value later on.
456 	 *
457 	 * (This informs us of masters that might not currently be
458 	 *  discoverable by this mount).
459 	 */
460 	if (ripdata && pmp->pfs_nmasters < ripdata->meta.pfs_nmasters) {
461 		pmp->pfs_nmasters = ripdata->meta.pfs_nmasters;
462 	}
463 
464 	/*
465 	 * Count visible masters.  Masters are usually added with
466 	 * ripdata->meta.pfs_nmasters set to 1.  This detects when there
467 	 * are more (XXX and must update the master inodes).
468 	 */
469 	count = 0;
470 	for (i = 0; i < iroot->cluster.nchains; ++i) {
471 		if (pmp->pfs_types[i] == HAMMER2_PFSTYPE_MASTER)
472 			++count;
473 	}
474 	if (pmp->pfs_nmasters < count)
475 		pmp->pfs_nmasters = count;
476 
477 	/*
478 	 * Create missing synchronization and support threads.
479 	 *
480 	 * Single-node masters (including snapshots) have nothing to
481 	 * synchronize and do not require this thread.
482 	 *
483 	 * Multi-node masters or any number of soft masters, slaves, copy,
484 	 * or other PFS types need the thread.
485 	 *
486 	 * Each thread is responsible for its particular cluster index.
487 	 * We use independent threads so stalls or mismatches related to
488 	 * any given target do not affect other targets.
489 	 */
490 	for (i = 0; i < iroot->cluster.nchains; ++i) {
491 		/*
492 		 * Single-node masters (including snapshots) have nothing
493 		 * to synchronize and will make direct xops support calls,
494 		 * thus they do not require this thread.
495 		 *
496 		 * Note that there can be thousands of snapshots.  We do not
497 		 * want to create thousands of threads.
498 		 */
499 		if (pmp->pfs_nmasters <= 1 &&
500 		    pmp->pfs_types[i] == HAMMER2_PFSTYPE_MASTER) {
501 			continue;
502 		}
503 
504 		/*
505 		 * Sync support thread
506 		 */
507 		if (pmp->sync_thrs[i].td == NULL) {
508 			hammer2_thr_create(&pmp->sync_thrs[i], pmp,
509 					   "h2nod", i, -1,
510 					   hammer2_primary_sync_thread);
511 		}
512 	}
513 
514 	/*
515 	 * Create missing Xop threads
516 	 */
517 	if (pmp->mp)
518 		hammer2_xop_helper_create(pmp);
519 
520 	hammer2_mtx_unlock(&iroot->lock);
521 	hammer2_inode_drop(iroot);
522 done:
523 	return pmp;
524 }
525 
526 /*
527  * Destroy a PFS, typically only occurs after the last mount on a device
528  * has gone away.
529  */
530 static void
531 hammer2_pfsfree(hammer2_pfs_t *pmp)
532 {
533 	hammer2_inode_t *iroot;
534 	int i;
535 	int j;
536 
537 	/*
538 	 * Cleanup our reference on iroot.  iroot is (should) not be needed
539 	 * by the flush code.
540 	 */
541 	TAILQ_REMOVE(&hammer2_pfslist, pmp, mntentry);
542 
543 	iroot = pmp->iroot;
544 	if (iroot) {
545 		for (i = 0; i < iroot->cluster.nchains; ++i) {
546 			hammer2_thr_delete(&pmp->sync_thrs[i]);
547 			for (j = 0; j < HAMMER2_XOPGROUPS; ++j)
548 				hammer2_thr_delete(&pmp->xop_groups[j].thrs[i]);
549 		}
550 #if REPORT_REFS_ERRORS
551 		if (pmp->iroot->refs != 1)
552 			kprintf("PMP->IROOT %p REFS WRONG %d\n",
553 				pmp->iroot, pmp->iroot->refs);
554 #else
555 		KKASSERT(pmp->iroot->refs == 1);
556 #endif
557 		/* ref for pmp->iroot */
558 		hammer2_inode_drop(pmp->iroot);
559 		pmp->iroot = NULL;
560 	}
561 
562 	kmalloc_destroy(&pmp->mmsg);
563 	kmalloc_destroy(&pmp->minode);
564 
565 	kfree(pmp, M_HAMMER2);
566 }
567 
568 /*
569  * Remove all references to hmp from the pfs list.  Any PFS which becomes
570  * empty is terminated and freed.
571  *
572  * XXX inefficient.
573  */
574 static void
575 hammer2_pfsfree_scan(hammer2_dev_t *hmp)
576 {
577 	hammer2_pfs_t *pmp;
578 	hammer2_inode_t *iroot;
579 	hammer2_chain_t *rchain;
580 	int didfreeze;
581 	int i;
582 	int j;
583 
584 again:
585 	TAILQ_FOREACH(pmp, &hammer2_pfslist, mntentry) {
586 		if ((iroot = pmp->iroot) == NULL)
587 			continue;
588 		if (hmp->spmp == pmp) {
589 			kprintf("unmount hmp %p remove spmp %p\n",
590 				hmp, pmp);
591 			hmp->spmp = NULL;
592 		}
593 
594 		/*
595 		 * Determine if this PFS is affected.  If it is we must
596 		 * freeze all management threads and lock its iroot.
597 		 *
598 		 * Freezing a management thread forces it idle, operations
599 		 * in-progress will be aborted and it will have to start
600 		 * over again when unfrozen, or exit if told to exit.
601 		 */
602 		for (i = 0; i < HAMMER2_MAXCLUSTER; ++i) {
603 			if (pmp->pfs_hmps[i] == hmp)
604 				break;
605 		}
606 		if (i != HAMMER2_MAXCLUSTER) {
607 			/*
608 			 * Make sure all synchronization threads are locked
609 			 * down.
610 			 */
611 			for (i = 0; i < HAMMER2_MAXCLUSTER; ++i) {
612 				if (pmp->pfs_hmps[i] == NULL)
613 					continue;
614 				hammer2_thr_freeze_async(&pmp->sync_thrs[i]);
615 				for (j = 0; j < HAMMER2_XOPGROUPS; ++j) {
616 					hammer2_thr_freeze_async(
617 						&pmp->xop_groups[j].thrs[i]);
618 				}
619 			}
620 			for (i = 0; i < HAMMER2_MAXCLUSTER; ++i) {
621 				if (pmp->pfs_hmps[i] == NULL)
622 					continue;
623 				hammer2_thr_freeze(&pmp->sync_thrs[i]);
624 				for (j = 0; j < HAMMER2_XOPGROUPS; ++j) {
625 					hammer2_thr_freeze(
626 						&pmp->xop_groups[j].thrs[i]);
627 				}
628 			}
629 
630 			/*
631 			 * Lock the inode and clean out matching chains.
632 			 * Note that we cannot use hammer2_inode_lock_*()
633 			 * here because that would attempt to validate the
634 			 * cluster that we are in the middle of ripping
635 			 * apart.
636 			 *
637 			 * WARNING! We are working directly on the inodes
638 			 *	    embedded cluster.
639 			 */
640 			hammer2_mtx_ex(&iroot->lock);
641 
642 			/*
643 			 * Remove the chain from matching elements of the PFS.
644 			 */
645 			for (i = 0; i < HAMMER2_MAXCLUSTER; ++i) {
646 				if (pmp->pfs_hmps[i] != hmp)
647 					continue;
648 				hammer2_thr_delete(&pmp->sync_thrs[i]);
649 				for (j = 0; j < HAMMER2_XOPGROUPS; ++j) {
650 					hammer2_thr_delete(
651 						&pmp->xop_groups[j].thrs[i]);
652 				}
653 				rchain = iroot->cluster.array[i].chain;
654 				iroot->cluster.array[i].chain = NULL;
655 				pmp->pfs_types[i] = 0;
656 				if (pmp->pfs_names[i]) {
657 					kfree(pmp->pfs_names[i], M_HAMMER2);
658 					pmp->pfs_names[i] = NULL;
659 				}
660 				if (rchain) {
661 					hammer2_chain_drop(rchain);
662 					/* focus hint */
663 					if (iroot->cluster.focus == rchain)
664 						iroot->cluster.focus = NULL;
665 				}
666 				pmp->pfs_hmps[i] = NULL;
667 			}
668 			hammer2_mtx_unlock(&iroot->lock);
669 			didfreeze = 1;	/* remaster, unfreeze down below */
670 		} else {
671 			didfreeze = 0;
672 		}
673 
674 		/*
675 		 * Cleanup trailing chains.  Gaps may remain.
676 		 */
677 		for (i = HAMMER2_MAXCLUSTER - 1; i >= 0; --i) {
678 			if (pmp->pfs_hmps[i])
679 				break;
680 		}
681 		iroot->cluster.nchains = i + 1;
682 
683 		/*
684 		 * If the PMP has no elements remaining we can destroy it.
685 		 * (this will transition management threads from frozen->exit).
686 		 */
687 		if (iroot->cluster.nchains == 0) {
688 			kprintf("unmount hmp %p last ref to PMP=%p\n",
689 				hmp, pmp);
690 			hammer2_pfsfree(pmp);
691 			goto again;
692 		}
693 
694 		/*
695 		 * If elements still remain we need to set the REMASTER
696 		 * flag and unfreeze it.
697 		 */
698 		if (didfreeze) {
699 			for (i = 0; i < HAMMER2_MAXCLUSTER; ++i) {
700 				if (pmp->pfs_hmps[i] == NULL)
701 					continue;
702 				hammer2_thr_remaster(&pmp->sync_thrs[i]);
703 				hammer2_thr_unfreeze(&pmp->sync_thrs[i]);
704 				for (j = 0; j < HAMMER2_XOPGROUPS; ++j) {
705 					hammer2_thr_remaster(
706 						&pmp->xop_groups[j].thrs[i]);
707 					hammer2_thr_unfreeze(
708 						&pmp->xop_groups[j].thrs[i]);
709 				}
710 			}
711 		}
712 	}
713 }
714 
715 /*
716  * Mount or remount HAMMER2 fileystem from physical media
717  *
718  *	mountroot
719  *		mp		mount point structure
720  *		path		NULL
721  *		data		<unused>
722  *		cred		<unused>
723  *
724  *	mount
725  *		mp		mount point structure
726  *		path		path to mount point
727  *		data		pointer to argument structure in user space
728  *			volume	volume path (device@LABEL form)
729  *			hflags	user mount flags
730  *		cred		user credentials
731  *
732  * RETURNS:	0	Success
733  *		!0	error number
734  */
735 static
736 int
737 hammer2_vfs_mount(struct mount *mp, char *path, caddr_t data,
738 		  struct ucred *cred)
739 {
740 	struct hammer2_mount_info info;
741 	hammer2_pfs_t *pmp;
742 	hammer2_pfs_t *spmp;
743 	hammer2_dev_t *hmp;
744 	hammer2_key_t key_next;
745 	hammer2_key_t key_dummy;
746 	hammer2_key_t lhc;
747 	struct vnode *devvp;
748 	struct nlookupdata nd;
749 	hammer2_chain_t *parent;
750 	hammer2_chain_t *chain;
751 	hammer2_cluster_t *cluster;
752 	const hammer2_inode_data_t *ripdata;
753 	hammer2_blockref_t bref;
754 	struct file *fp;
755 	char devstr[MNAMELEN];
756 	size_t size;
757 	size_t done;
758 	char *dev;
759 	char *label;
760 	int ronly = 1;
761 	int error;
762 	int cache_index;
763 	int i;
764 
765 	hmp = NULL;
766 	pmp = NULL;
767 	dev = NULL;
768 	label = NULL;
769 	devvp = NULL;
770 	cache_index = -1;
771 
772 	kprintf("hammer2_mount\n");
773 
774 	if (path == NULL) {
775 		/*
776 		 * Root mount
777 		 */
778 		bzero(&info, sizeof(info));
779 		info.cluster_fd = -1;
780 		ksnprintf(devstr, sizeof(devstr), "%s",
781 			  mp->mnt_stat.f_mntfromname);
782 		kprintf("hammer2_mount: root '%s'\n", devstr);
783 	} else {
784 		/*
785 		 * Non-root mount or updating a mount
786 		 */
787 		error = copyin(data, &info, sizeof(info));
788 		if (error)
789 			return (error);
790 
791 		error = copyinstr(info.volume, devstr, MNAMELEN - 1, &done);
792 		if (error)
793 			return (error);
794 	}
795 
796 	/* Extract device and label */
797 	dev = devstr;
798 	label = strchr(devstr, '@');
799 	if (label == NULL ||
800 	    ((label + 1) - dev) > done) {
801 		return (EINVAL);
802 	}
803 	*label = '\0';
804 	label++;
805 	if (*label == '\0')
806 		return (EINVAL);
807 
808 	if (mp->mnt_flag & MNT_UPDATE) {
809 		/*
810 		 * Update mount.  Note that pmp->iroot->cluster is
811 		 * an inode-embedded cluster and thus cannot be
812 		 * directly locked.
813 		 *
814 		 * XXX HAMMER2 needs to implement NFS export via
815 		 *     mountctl.
816 		 */
817 		pmp = MPTOPMP(mp);
818 		cluster = &pmp->iroot->cluster;
819 		for (i = 0; i < cluster->nchains; ++i) {
820 			if (cluster->array[i].chain == NULL)
821 				continue;
822 			hmp = cluster->array[i].chain->hmp;
823 			devvp = hmp->devvp;
824 			error = hammer2_remount(hmp, mp, path,
825 						devvp, cred);
826 			if (error)
827 				break;
828 		}
829 
830 		return error;
831 	}
832 
833 	/*
834 	 * HMP device mount
835 	 *
836 	 * Lookup name and verify it refers to a block device.
837 	 */
838 	if (path) {
839 		error = nlookup_init(&nd, dev, UIO_SYSSPACE, NLC_FOLLOW);
840 		if (error == 0)
841 			error = nlookup(&nd);
842 		if (error == 0)
843 			error = cache_vref(&nd.nl_nch, nd.nl_cred, &devvp);
844 		nlookup_done(&nd);
845 	} else {
846 		/* root mount */
847 		cdev_t cdev = kgetdiskbyname(dev);
848 		error = bdevvp(cdev, &devvp);
849 		if (error)
850 			kprintf("hammer2: cannot find '%s'\n", dev);
851 	}
852 
853 	if (error == 0) {
854 		if (vn_isdisk(devvp, &error))
855 			error = vfs_mountedon(devvp);
856 	}
857 
858 	/*
859 	 * Determine if the device has already been mounted.  After this
860 	 * check hmp will be non-NULL if we are doing the second or more
861 	 * hammer2 mounts from the same device.
862 	 */
863 	lockmgr(&hammer2_mntlk, LK_EXCLUSIVE);
864 	TAILQ_FOREACH(hmp, &hammer2_mntlist, mntentry) {
865 		if (hmp->devvp == devvp)
866 			break;
867 	}
868 
869 	/*
870 	 * Open the device if this isn't a secondary mount and construct
871 	 * the H2 device mount (hmp).
872 	 */
873 	if (hmp == NULL) {
874 		hammer2_chain_t *schain;
875 		hammer2_xid_t xid;
876 
877 		if (error == 0 && vcount(devvp) > 0)
878 			error = EBUSY;
879 
880 		/*
881 		 * Now open the device
882 		 */
883 		if (error == 0) {
884 			ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
885 			vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
886 			error = vinvalbuf(devvp, V_SAVE, 0, 0);
887 			if (error == 0) {
888 				error = VOP_OPEN(devvp,
889 						 ronly ? FREAD : FREAD | FWRITE,
890 						 FSCRED, NULL);
891 			}
892 			vn_unlock(devvp);
893 		}
894 		if (error && devvp) {
895 			vrele(devvp);
896 			devvp = NULL;
897 		}
898 		if (error) {
899 			lockmgr(&hammer2_mntlk, LK_RELEASE);
900 			return error;
901 		}
902 		hmp = kmalloc(sizeof(*hmp), M_HAMMER2, M_WAITOK | M_ZERO);
903 		ksnprintf(hmp->devrepname, sizeof(hmp->devrepname), "%s", dev);
904 		hmp->ronly = ronly;
905 		hmp->devvp = devvp;
906 		kmalloc_create(&hmp->mchain, "HAMMER2-chains");
907 		TAILQ_INSERT_TAIL(&hammer2_mntlist, hmp, mntentry);
908 		RB_INIT(&hmp->iotree);
909 		spin_init(&hmp->io_spin, "hm2mount_io");
910 		spin_init(&hmp->list_spin, "hm2mount_list");
911 		TAILQ_INIT(&hmp->flushq);
912 
913 		lockinit(&hmp->vollk, "h2vol", 0, 0);
914 		lockinit(&hmp->bulklk, "h2bulk", 0, 0);
915 
916 		/*
917 		 * vchain setup. vchain.data is embedded.
918 		 * vchain.refs is initialized and will never drop to 0.
919 		 *
920 		 * NOTE! voldata is not yet loaded.
921 		 */
922 		hmp->vchain.hmp = hmp;
923 		hmp->vchain.refs = 1;
924 		hmp->vchain.data = (void *)&hmp->voldata;
925 		hmp->vchain.bref.type = HAMMER2_BREF_TYPE_VOLUME;
926 		hmp->vchain.bref.data_off = 0 | HAMMER2_PBUFRADIX;
927 		hmp->vchain.bref.mirror_tid = hmp->voldata.mirror_tid;
928 
929 		hammer2_chain_core_init(&hmp->vchain);
930 		/* hmp->vchain.u.xxx is left NULL */
931 
932 		/*
933 		 * fchain setup.  fchain.data is embedded.
934 		 * fchain.refs is initialized and will never drop to 0.
935 		 *
936 		 * The data is not used but needs to be initialized to
937 		 * pass assertion muster.  We use this chain primarily
938 		 * as a placeholder for the freemap's top-level RBTREE
939 		 * so it does not interfere with the volume's topology
940 		 * RBTREE.
941 		 */
942 		hmp->fchain.hmp = hmp;
943 		hmp->fchain.refs = 1;
944 		hmp->fchain.data = (void *)&hmp->voldata.freemap_blockset;
945 		hmp->fchain.bref.type = HAMMER2_BREF_TYPE_FREEMAP;
946 		hmp->fchain.bref.data_off = 0 | HAMMER2_PBUFRADIX;
947 		hmp->fchain.bref.mirror_tid = hmp->voldata.freemap_tid;
948 		hmp->fchain.bref.methods =
949 			HAMMER2_ENC_CHECK(HAMMER2_CHECK_FREEMAP) |
950 			HAMMER2_ENC_COMP(HAMMER2_COMP_NONE);
951 
952 		hammer2_chain_core_init(&hmp->fchain);
953 		/* hmp->fchain.u.xxx is left NULL */
954 
955 		/*
956 		 * Install the volume header and initialize fields from
957 		 * voldata.
958 		 */
959 		error = hammer2_install_volume_header(hmp);
960 		if (error) {
961 			hammer2_unmount_helper(mp, NULL, hmp);
962 			lockmgr(&hammer2_mntlk, LK_RELEASE);
963 			hammer2_vfs_unmount(mp, MNT_FORCE);
964 			return error;
965 		}
966 
967 		/*
968 		 * Really important to get these right or flush will get
969 		 * confused.
970 		 */
971 		hmp->spmp = hammer2_pfsalloc(NULL, NULL, 0);
972 		kprintf("alloc spmp %p tid %016jx\n",
973 			hmp->spmp, hmp->voldata.mirror_tid);
974 		spmp = hmp->spmp;
975 
976 		/*
977 		 * Dummy-up vchain and fchain's modify_tid.  mirror_tid
978 		 * is inherited from the volume header.
979 		 */
980 		xid = 0;
981 		hmp->vchain.bref.mirror_tid = hmp->voldata.mirror_tid;
982 		hmp->vchain.bref.modify_tid = hmp->vchain.bref.mirror_tid;
983 		hmp->vchain.pmp = spmp;
984 		hmp->fchain.bref.mirror_tid = hmp->voldata.freemap_tid;
985 		hmp->fchain.bref.modify_tid = hmp->fchain.bref.mirror_tid;
986 		hmp->fchain.pmp = spmp;
987 
988 		/*
989 		 * First locate the super-root inode, which is key 0
990 		 * relative to the volume header's blockset.
991 		 *
992 		 * Then locate the root inode by scanning the directory keyspace
993 		 * represented by the label.
994 		 */
995 		parent = hammer2_chain_lookup_init(&hmp->vchain, 0);
996 		schain = hammer2_chain_lookup(&parent, &key_dummy,
997 				      HAMMER2_SROOT_KEY, HAMMER2_SROOT_KEY,
998 				      &cache_index, 0);
999 		hammer2_chain_lookup_done(parent);
1000 		if (schain == NULL) {
1001 			kprintf("hammer2_mount: invalid super-root\n");
1002 			hammer2_unmount_helper(mp, NULL, hmp);
1003 			lockmgr(&hammer2_mntlk, LK_RELEASE);
1004 			hammer2_vfs_unmount(mp, MNT_FORCE);
1005 			return EINVAL;
1006 		}
1007 		if (schain->error) {
1008 			kprintf("hammer2_mount: error %s reading super-root\n",
1009 				hammer2_error_str(schain->error));
1010 			hammer2_chain_unlock(schain);
1011 			hammer2_chain_drop(schain);
1012 			schain = NULL;
1013 			hammer2_unmount_helper(mp, NULL, hmp);
1014 			lockmgr(&hammer2_mntlk, LK_RELEASE);
1015 			hammer2_vfs_unmount(mp, MNT_FORCE);
1016 			return EINVAL;
1017 		}
1018 
1019 		/*
1020 		 * The super-root always uses an inode_tid of 1 when
1021 		 * creating PFSs.
1022 		 */
1023 		spmp->inode_tid = 1;
1024 		spmp->modify_tid = schain->bref.modify_tid + 1;
1025 
1026 		/*
1027 		 * Sanity-check schain's pmp and finish initialization.
1028 		 * Any chain belonging to the super-root topology should
1029 		 * have a NULL pmp (not even set to spmp).
1030 		 */
1031 		ripdata = &hammer2_chain_rdata(schain)->ipdata;
1032 		KKASSERT(schain->pmp == NULL);
1033 		spmp->pfs_clid = ripdata->meta.pfs_clid;
1034 
1035 		/*
1036 		 * Replace the dummy spmp->iroot with a real one.  It's
1037 		 * easier to just do a wholesale replacement than to try
1038 		 * to update the chain and fixup the iroot fields.
1039 		 *
1040 		 * The returned inode is locked with the supplied cluster.
1041 		 */
1042 		cluster = hammer2_cluster_from_chain(schain);
1043 		hammer2_inode_drop(spmp->iroot);
1044 		spmp->iroot = NULL;
1045 		spmp->iroot = hammer2_inode_get(spmp, NULL, cluster, -1);
1046 		spmp->spmp_hmp = hmp;
1047 		spmp->pfs_types[0] = ripdata->meta.pfs_type;
1048 		spmp->pfs_hmps[0] = hmp;
1049 		hammer2_inode_ref(spmp->iroot);
1050 		hammer2_inode_unlock(spmp->iroot);
1051 		hammer2_cluster_unlock(cluster);
1052 		hammer2_cluster_drop(cluster);
1053 		schain = NULL;
1054 		/* leave spmp->iroot with one ref */
1055 
1056 		if ((mp->mnt_flag & MNT_RDONLY) == 0) {
1057 			error = hammer2_recovery(hmp);
1058 			/* XXX do something with error */
1059 		}
1060 		hammer2_update_pmps(hmp);
1061 		hammer2_iocom_init(hmp);
1062 
1063 		/*
1064 		 * Ref the cluster management messaging descriptor.  The mount
1065 		 * program deals with the other end of the communications pipe.
1066 		 *
1067 		 * Root mounts typically do not supply one.
1068 		 */
1069 		if (info.cluster_fd >= 0) {
1070 			fp = holdfp(curproc->p_fd, info.cluster_fd, -1);
1071 			if (fp) {
1072 				hammer2_cluster_reconnect(hmp, fp);
1073 			} else {
1074 				kprintf("hammer2_mount: bad cluster_fd!\n");
1075 			}
1076 		}
1077 	} else {
1078 		spmp = hmp->spmp;
1079 	}
1080 
1081 	/*
1082 	 * Lookup the mount point under the media-localized super-root.
1083 	 * Scanning hammer2_pfslist doesn't help us because it represents
1084 	 * PFS cluster ids which can aggregate several named PFSs together.
1085 	 *
1086 	 * cluster->pmp will incorrectly point to spmp and must be fixed
1087 	 * up later on.
1088 	 */
1089 	hammer2_inode_lock(spmp->iroot, 0);
1090 	parent = hammer2_inode_chain(spmp->iroot, 0, HAMMER2_RESOLVE_ALWAYS);
1091 	lhc = hammer2_dirhash(label, strlen(label));
1092 	chain = hammer2_chain_lookup(&parent, &key_next,
1093 				     lhc, lhc + HAMMER2_DIRHASH_LOMASK,
1094 				     &cache_index, 0);
1095 	while (chain) {
1096 		if (chain->bref.type == HAMMER2_BREF_TYPE_INODE &&
1097 		    strcmp(label, chain->data->ipdata.filename) == 0) {
1098 			break;
1099 		}
1100 		chain = hammer2_chain_next(&parent, chain, &key_next,
1101 					    key_next,
1102 					    lhc + HAMMER2_DIRHASH_LOMASK,
1103 					    &cache_index, 0);
1104 	}
1105 	if (parent) {
1106 		hammer2_chain_unlock(parent);
1107 		hammer2_chain_drop(parent);
1108 	}
1109 	hammer2_inode_unlock(spmp->iroot);
1110 
1111 	/*
1112 	 * PFS could not be found?
1113 	 */
1114 	if (chain == NULL) {
1115 		kprintf("hammer2_mount: PFS label not found\n");
1116 		hammer2_unmount_helper(mp, NULL, hmp);
1117 		lockmgr(&hammer2_mntlk, LK_RELEASE);
1118 		hammer2_vfs_unmount(mp, MNT_FORCE);
1119 
1120 		return EINVAL;
1121 	}
1122 
1123 	/*
1124 	 * Acquire the pmp structure (it should have already been allocated
1125 	 * via hammer2_update_pmps() so do not pass cluster in to add to
1126 	 * available chains).
1127 	 *
1128 	 * Check if the cluster has already been mounted.  A cluster can
1129 	 * only be mounted once, use null mounts to mount additional copies.
1130 	 */
1131 	ripdata = &chain->data->ipdata;
1132 	bref = chain->bref;
1133 	pmp = hammer2_pfsalloc(NULL, ripdata, bref.modify_tid);
1134 	hammer2_chain_unlock(chain);
1135 	hammer2_chain_drop(chain);
1136 
1137 	if (pmp->mp) {
1138 		kprintf("hammer2_mount: PFS already mounted!\n");
1139 		hammer2_unmount_helper(mp, NULL, hmp);
1140 		lockmgr(&hammer2_mntlk, LK_RELEASE);
1141 		hammer2_vfs_unmount(mp, MNT_FORCE);
1142 
1143 		return EBUSY;
1144 	}
1145 
1146 	/*
1147 	 * Finish the mount
1148 	 */
1149         kprintf("hammer2_mount hmp=%p pmp=%p\n", hmp, pmp);
1150 
1151         mp->mnt_flag = MNT_LOCAL;
1152         mp->mnt_kern_flag |= MNTK_ALL_MPSAFE;   /* all entry pts are SMP */
1153         mp->mnt_kern_flag |= MNTK_THR_SYNC;     /* new vsyncscan semantics */
1154 
1155         /*
1156          * required mount structure initializations
1157          */
1158         mp->mnt_stat.f_iosize = HAMMER2_PBUFSIZE;
1159         mp->mnt_stat.f_bsize = HAMMER2_PBUFSIZE;
1160 
1161         mp->mnt_vstat.f_frsize = HAMMER2_PBUFSIZE;
1162         mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE;
1163 
1164         /*
1165          * Optional fields
1166          */
1167         mp->mnt_iosize_max = MAXPHYS;
1168 
1169 	/*
1170 	 * Connect up mount pointers.
1171 	 */
1172 	hammer2_mount_helper(mp, pmp);
1173 
1174         lockmgr(&hammer2_mntlk, LK_RELEASE);
1175 
1176 	/*
1177 	 * Finish setup
1178 	 */
1179 	vfs_getnewfsid(mp);
1180 	vfs_add_vnodeops(mp, &hammer2_vnode_vops, &mp->mnt_vn_norm_ops);
1181 	vfs_add_vnodeops(mp, &hammer2_spec_vops, &mp->mnt_vn_spec_ops);
1182 	vfs_add_vnodeops(mp, &hammer2_fifo_vops, &mp->mnt_vn_fifo_ops);
1183 
1184 	if (path) {
1185 		copyinstr(info.volume, mp->mnt_stat.f_mntfromname,
1186 			  MNAMELEN - 1, &size);
1187 		bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
1188 	} /* else root mount, already in there */
1189 
1190 	bzero(mp->mnt_stat.f_mntonname, sizeof(mp->mnt_stat.f_mntonname));
1191 	if (path) {
1192 		copyinstr(path, mp->mnt_stat.f_mntonname,
1193 			  sizeof(mp->mnt_stat.f_mntonname) - 1,
1194 			  &size);
1195 	} else {
1196 		/* root mount */
1197 		mp->mnt_stat.f_mntonname[0] = '/';
1198 	}
1199 
1200 	/*
1201 	 * Initial statfs to prime mnt_stat.
1202 	 */
1203 	hammer2_vfs_statfs(mp, &mp->mnt_stat, cred);
1204 
1205 	return 0;
1206 }
1207 
1208 /*
1209  * Scan PFSs under the super-root and create hammer2_pfs structures.
1210  */
1211 static
1212 void
1213 hammer2_update_pmps(hammer2_dev_t *hmp)
1214 {
1215 	const hammer2_inode_data_t *ripdata;
1216 	hammer2_chain_t *parent;
1217 	hammer2_chain_t *chain;
1218 	hammer2_blockref_t bref;
1219 	hammer2_pfs_t *spmp;
1220 	hammer2_pfs_t *pmp;
1221 	hammer2_key_t key_next;
1222 	int cache_index = -1;
1223 
1224 	/*
1225 	 * Lookup mount point under the media-localized super-root.
1226 	 *
1227 	 * cluster->pmp will incorrectly point to spmp and must be fixed
1228 	 * up later on.
1229 	 */
1230 	spmp = hmp->spmp;
1231 	hammer2_inode_lock(spmp->iroot, 0);
1232 	parent = hammer2_inode_chain(spmp->iroot, 0, HAMMER2_RESOLVE_ALWAYS);
1233 	chain = hammer2_chain_lookup(&parent, &key_next,
1234 					 HAMMER2_KEY_MIN, HAMMER2_KEY_MAX,
1235 					 &cache_index, 0);
1236 	while (chain) {
1237 		if (chain->bref.type != HAMMER2_BREF_TYPE_INODE)
1238 			continue;
1239 		ripdata = &chain->data->ipdata;
1240 		bref = chain->bref;
1241 		kprintf("ADD LOCAL PFS: %s\n", ripdata->filename);
1242 
1243 		pmp = hammer2_pfsalloc(chain, ripdata, bref.modify_tid);
1244 		chain = hammer2_chain_next(&parent, chain, &key_next,
1245 					   key_next, HAMMER2_KEY_MAX,
1246 					   &cache_index, 0);
1247 	}
1248 	if (parent) {
1249 		hammer2_chain_unlock(parent);
1250 		hammer2_chain_drop(parent);
1251 	}
1252 	hammer2_inode_unlock(spmp->iroot);
1253 }
1254 
1255 static
1256 int
1257 hammer2_remount(hammer2_dev_t *hmp, struct mount *mp, char *path __unused,
1258 		struct vnode *devvp, struct ucred *cred)
1259 {
1260 	int error;
1261 
1262 	if (hmp->ronly && (mp->mnt_kern_flag & MNTK_WANTRDWR)) {
1263 		error = hammer2_recovery(hmp);
1264 	} else {
1265 		error = 0;
1266 	}
1267 	return error;
1268 }
1269 
1270 static
1271 int
1272 hammer2_vfs_unmount(struct mount *mp, int mntflags)
1273 {
1274 	hammer2_pfs_t *pmp;
1275 	int flags;
1276 	int error = 0;
1277 
1278 	pmp = MPTOPMP(mp);
1279 
1280 	if (pmp == NULL)
1281 		return(0);
1282 
1283 	lockmgr(&hammer2_mntlk, LK_EXCLUSIVE);
1284 
1285 	/*
1286 	 * If mount initialization proceeded far enough we must flush
1287 	 * its vnodes and sync the underlying mount points.  Three syncs
1288 	 * are required to fully flush the filesystem (freemap updates lag
1289 	 * by one flush, and one extra for safety).
1290 	 */
1291 	if (mntflags & MNT_FORCE)
1292 		flags = FORCECLOSE;
1293 	else
1294 		flags = 0;
1295 	if (pmp->iroot) {
1296 		error = vflush(mp, 0, flags);
1297 		if (error)
1298 			goto failed;
1299 		hammer2_vfs_sync(mp, MNT_WAIT);
1300 		hammer2_vfs_sync(mp, MNT_WAIT);
1301 		hammer2_vfs_sync(mp, MNT_WAIT);
1302 	}
1303 
1304 	/*
1305 	 * Cleanup the frontend support XOPS threads
1306 	 */
1307 	hammer2_xop_helper_cleanup(pmp);
1308 
1309 	/*
1310 	 * Cleanup our reference on ihidden.
1311 	 */
1312 	if (pmp->ihidden) {
1313 		hammer2_inode_drop(pmp->ihidden);
1314 		pmp->ihidden = NULL;
1315 	}
1316 	if (pmp->mp)
1317 		hammer2_unmount_helper(mp, pmp, NULL);
1318 
1319 	error = 0;
1320 failed:
1321 	lockmgr(&hammer2_mntlk, LK_RELEASE);
1322 
1323 	return (error);
1324 }
1325 
1326 /*
1327  * Mount helper, hook the system mount into our PFS.
1328  * The mount lock is held.
1329  *
1330  * We must bump the mount_count on related devices for any
1331  * mounted PFSs.
1332  */
1333 static
1334 void
1335 hammer2_mount_helper(struct mount *mp, hammer2_pfs_t *pmp)
1336 {
1337 	hammer2_cluster_t *cluster;
1338 	hammer2_chain_t *rchain;
1339 	int i;
1340 
1341         mp->mnt_data = (qaddr_t)pmp;
1342 	pmp->mp = mp;
1343 
1344 	/*
1345 	 * After pmp->mp is set we have to adjust hmp->mount_count.
1346 	 */
1347 	cluster = &pmp->iroot->cluster;
1348 	for (i = 0; i < cluster->nchains; ++i) {
1349 		rchain = cluster->array[i].chain;
1350 		if (rchain == NULL)
1351 			continue;
1352 		++rchain->hmp->mount_count;
1353 		kprintf("hammer2_mount hmp=%p ++mount_count=%d\n",
1354 			rchain->hmp, rchain->hmp->mount_count);
1355 	}
1356 
1357 	/*
1358 	 * Create missing Xop threads
1359 	 */
1360 	hammer2_xop_helper_create(pmp);
1361 }
1362 
1363 /*
1364  * Mount helper, unhook the system mount from our PFS.
1365  * The mount lock is held.
1366  *
1367  * If hmp is supplied a mount responsible for being the first to open
1368  * the block device failed and the block device and all PFSs using the
1369  * block device must be cleaned up.
1370  *
1371  * If pmp is supplied multiple devices might be backing the PFS and each
1372  * must be disconnect.  This might not be the last PFS using some of the
1373  * underlying devices.  Also, we have to adjust our hmp->mount_count
1374  * accounting for the devices backing the pmp which is now undergoing an
1375  * unmount.
1376  */
1377 static
1378 void
1379 hammer2_unmount_helper(struct mount *mp, hammer2_pfs_t *pmp, hammer2_dev_t *hmp)
1380 {
1381 	hammer2_cluster_t *cluster;
1382 	hammer2_chain_t *rchain;
1383 	struct vnode *devvp;
1384 	int dumpcnt;
1385 	int ronly = 0;
1386 	int i;
1387 
1388 	/*
1389 	 * If no device supplied this is a high-level unmount and we have to
1390 	 * to disconnect the mount, adjust mount_count, and locate devices
1391 	 * that might now have no mounts.
1392 	 */
1393 	if (pmp) {
1394 		KKASSERT(hmp == NULL);
1395 		KKASSERT((void *)(intptr_t)mp->mnt_data == pmp);
1396 		pmp->mp = NULL;
1397 		mp->mnt_data = NULL;
1398 
1399 		/*
1400 		 * After pmp->mp is cleared we have to account for
1401 		 * mount_count.
1402 		 */
1403 		cluster = &pmp->iroot->cluster;
1404 		for (i = 0; i < cluster->nchains; ++i) {
1405 			rchain = cluster->array[i].chain;
1406 			if (rchain == NULL)
1407 				continue;
1408 			--rchain->hmp->mount_count;
1409 			kprintf("hammer2_unmount hmp=%p --mount_count=%d\n",
1410 				rchain->hmp, rchain->hmp->mount_count);
1411 			/* scrapping hmp now may invalidate the pmp */
1412 		}
1413 again:
1414 		TAILQ_FOREACH(hmp, &hammer2_mntlist, mntentry) {
1415 			if (hmp->mount_count == 0) {
1416 				hammer2_unmount_helper(NULL, NULL, hmp);
1417 				goto again;
1418 			}
1419 		}
1420 		return;
1421 	}
1422 
1423 	/*
1424 	 * Try to terminate the block device.  We can't terminate it if
1425 	 * there are still PFSs referencing it.
1426 	 */
1427 	kprintf("hammer2_unmount hmp=%p mount_count=%d\n",
1428 		hmp, hmp->mount_count);
1429 	if (hmp->mount_count)
1430 		return;
1431 
1432 	hammer2_pfsfree_scan(hmp);
1433 	hammer2_dev_exlock(hmp);	/* XXX order */
1434 
1435 	/*
1436 	 * Cycle the volume data lock as a safety (probably not needed any
1437 	 * more).  To ensure everything is out we need to flush at least
1438 	 * three times.  (1) The running of the sideq can dirty the
1439 	 * filesystem, (2) A normal flush can dirty the freemap, and
1440 	 * (3) ensure that the freemap is fully synchronized.
1441 	 *
1442 	 * The next mount's recovery scan can clean everything up but we want
1443 	 * to leave the filesystem in a 100% clean state on a normal unmount.
1444 	 */
1445 #if 0
1446 	hammer2_voldata_lock(hmp);
1447 	hammer2_voldata_unlock(hmp);
1448 #endif
1449 	hammer2_iocom_uninit(hmp);
1450 
1451 	if ((hmp->vchain.flags | hmp->fchain.flags) &
1452 	    HAMMER2_CHAIN_FLUSH_MASK) {
1453 		kprintf("hammer2_unmount: chains left over "
1454 			"after final sync\n");
1455 		kprintf("    vchain %08x\n", hmp->vchain.flags);
1456 		kprintf("    fchain %08x\n", hmp->fchain.flags);
1457 
1458 		if (hammer2_debug & 0x0010)
1459 			Debugger("entered debugger");
1460 	}
1461 
1462 	KKASSERT(hmp->spmp == NULL);
1463 
1464 	/*
1465 	 * Finish up with the device vnode
1466 	 */
1467 	if ((devvp = hmp->devvp) != NULL) {
1468 		vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1469 		vinvalbuf(devvp, (ronly ? 0 : V_SAVE), 0, 0);
1470 		hmp->devvp = NULL;
1471 		VOP_CLOSE(devvp, (ronly ? FREAD : FREAD|FWRITE), NULL);
1472 		vn_unlock(devvp);
1473 		vrele(devvp);
1474 		devvp = NULL;
1475 	}
1476 
1477 	/*
1478 	 * Clear vchain/fchain flags that might prevent final cleanup
1479 	 * of these chains.
1480 	 */
1481 	if (hmp->vchain.flags & HAMMER2_CHAIN_MODIFIED) {
1482 		atomic_clear_int(&hmp->vchain.flags,
1483 				 HAMMER2_CHAIN_MODIFIED);
1484 		hammer2_pfs_memory_wakeup(hmp->vchain.pmp);
1485 		hammer2_chain_drop(&hmp->vchain);
1486 	}
1487 	if (hmp->vchain.flags & HAMMER2_CHAIN_UPDATE) {
1488 		atomic_clear_int(&hmp->vchain.flags,
1489 				 HAMMER2_CHAIN_UPDATE);
1490 		hammer2_chain_drop(&hmp->vchain);
1491 	}
1492 
1493 	if (hmp->fchain.flags & HAMMER2_CHAIN_MODIFIED) {
1494 		atomic_clear_int(&hmp->fchain.flags,
1495 				 HAMMER2_CHAIN_MODIFIED);
1496 		hammer2_pfs_memory_wakeup(hmp->fchain.pmp);
1497 		hammer2_chain_drop(&hmp->fchain);
1498 	}
1499 	if (hmp->fchain.flags & HAMMER2_CHAIN_UPDATE) {
1500 		atomic_clear_int(&hmp->fchain.flags,
1501 				 HAMMER2_CHAIN_UPDATE);
1502 		hammer2_chain_drop(&hmp->fchain);
1503 	}
1504 
1505 	/*
1506 	 * Final drop of embedded freemap root chain to
1507 	 * clean up fchain.core (fchain structure is not
1508 	 * flagged ALLOCATED so it is cleaned out and then
1509 	 * left to rot).
1510 	 */
1511 	hammer2_chain_drop(&hmp->fchain);
1512 
1513 	/*
1514 	 * Final drop of embedded volume root chain to clean
1515 	 * up vchain.core (vchain structure is not flagged
1516 	 * ALLOCATED so it is cleaned out and then left to
1517 	 * rot).
1518 	 */
1519 	dumpcnt = 50;
1520 	hammer2_dump_chain(&hmp->vchain, 0, &dumpcnt, 'v');
1521 	dumpcnt = 50;
1522 	hammer2_dump_chain(&hmp->fchain, 0, &dumpcnt, 'f');
1523 	hammer2_dev_unlock(hmp);
1524 	hammer2_chain_drop(&hmp->vchain);
1525 
1526 	hammer2_io_cleanup(hmp, &hmp->iotree);
1527 	if (hmp->iofree_count) {
1528 		kprintf("io_cleanup: %d I/O's left hanging\n",
1529 			hmp->iofree_count);
1530 	}
1531 
1532 	TAILQ_REMOVE(&hammer2_mntlist, hmp, mntentry);
1533 	kmalloc_destroy(&hmp->mchain);
1534 	kfree(hmp, M_HAMMER2);
1535 }
1536 
1537 static
1538 int
1539 hammer2_vfs_vget(struct mount *mp, struct vnode *dvp,
1540 	     ino_t ino, struct vnode **vpp)
1541 {
1542 	kprintf("hammer2_vget\n");
1543 	return (EOPNOTSUPP);
1544 }
1545 
1546 static
1547 int
1548 hammer2_vfs_root(struct mount *mp, struct vnode **vpp)
1549 {
1550 	hammer2_pfs_t *pmp;
1551 	int error;
1552 	struct vnode *vp;
1553 
1554 	pmp = MPTOPMP(mp);
1555 	if (pmp->iroot == NULL) {
1556 		*vpp = NULL;
1557 		return EINVAL;
1558 	}
1559 
1560 	error = 0;
1561 	hammer2_inode_lock(pmp->iroot, HAMMER2_RESOLVE_SHARED);
1562 
1563 	while (pmp->inode_tid == 0) {
1564 		hammer2_xop_ipcluster_t *xop;
1565 		hammer2_inode_meta_t *meta;
1566 
1567 		xop = hammer2_xop_alloc(pmp->iroot, HAMMER2_XOP_MODIFYING);
1568 		hammer2_xop_start(&xop->head, hammer2_xop_ipcluster);
1569 		error = hammer2_xop_collect(&xop->head, 0);
1570 
1571 		if (error == 0) {
1572 			meta = &xop->head.cluster.focus->data->ipdata.meta;
1573 			pmp->iroot->meta = *meta;
1574 			pmp->inode_tid = meta->pfs_inum + 1;
1575 			if (pmp->inode_tid < HAMMER2_INODE_START)
1576 				pmp->inode_tid = HAMMER2_INODE_START;
1577 			pmp->modify_tid =
1578 				xop->head.cluster.focus->bref.modify_tid + 1;
1579 			kprintf("PFS: Starting inode %jd\n",
1580 				(intmax_t)pmp->inode_tid);
1581 			kprintf("PMP focus good set nextino=%ld mod=%016jx\n",
1582 				pmp->inode_tid, pmp->modify_tid);
1583 			wakeup(&pmp->iroot);
1584 
1585 			hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1586 
1587 			/*
1588 			 * Prime the mount info.
1589 			 */
1590 			hammer2_vfs_statfs(mp, &mp->mnt_stat, NULL);
1591 
1592 			/*
1593 			 * With the cluster operational, check for and
1594 			 * install ihidden if needed.  The install_hidden
1595 			 * code needs to get a transaction so we must unlock
1596 			 * iroot around it.
1597 			 *
1598 			 * This is only applicable PFS mounts, there is no
1599 			 * hidden directory in the spmp.
1600 			 */
1601 			hammer2_inode_unlock(pmp->iroot);
1602 			hammer2_inode_install_hidden(pmp);
1603 			hammer2_inode_lock(pmp->iroot, HAMMER2_RESOLVE_SHARED);
1604 
1605 			break;
1606 		}
1607 
1608 		/*
1609 		 * Loop, try again
1610 		 */
1611 		hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
1612 		hammer2_inode_unlock(pmp->iroot);
1613 		error = tsleep(&pmp->iroot, PCATCH, "h2root", hz);
1614 		hammer2_inode_lock(pmp->iroot, HAMMER2_RESOLVE_SHARED);
1615 		if (error == EINTR)
1616 			break;
1617 	}
1618 
1619 	if (error) {
1620 		hammer2_inode_unlock(pmp->iroot);
1621 		*vpp = NULL;
1622 	} else {
1623 		vp = hammer2_igetv(pmp->iroot, &error);
1624 		hammer2_inode_unlock(pmp->iroot);
1625 		*vpp = vp;
1626 	}
1627 
1628 	return (error);
1629 }
1630 
1631 /*
1632  * Filesystem status
1633  *
1634  * XXX incorporate ipdata->meta.inode_quota and data_quota
1635  */
1636 static
1637 int
1638 hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
1639 {
1640 	hammer2_pfs_t *pmp;
1641 	hammer2_dev_t *hmp;
1642 	hammer2_blockref_t bref;
1643 	int i;
1644 
1645 	/*
1646 	 * NOTE: iroot might not have validated the cluster yet.
1647 	 */
1648 	pmp = MPTOPMP(mp);
1649 
1650 	mp->mnt_stat.f_files = 0;
1651 	mp->mnt_stat.f_ffree = 0;
1652 	mp->mnt_stat.f_blocks = 0;
1653 	mp->mnt_stat.f_bfree = 0;
1654 	mp->mnt_stat.f_bavail = 0;
1655 
1656 	for (i = 0; i < pmp->iroot->cluster.nchains; ++i) {
1657 		hmp = pmp->pfs_hmps[i];
1658 		if (hmp == NULL)
1659 			continue;
1660 		if (pmp->iroot->cluster.array[i].chain)
1661 			bref = pmp->iroot->cluster.array[i].chain->bref;
1662 		else
1663 			bzero(&bref, sizeof(bref));
1664 
1665 		mp->mnt_stat.f_files = bref.inode_count;
1666 		mp->mnt_stat.f_ffree = 0;
1667 		mp->mnt_stat.f_blocks = (bref.data_count +
1668 					 hmp->voldata.allocator_free) /
1669 					mp->mnt_vstat.f_bsize;
1670 		mp->mnt_stat.f_bfree =  hmp->voldata.allocator_free /
1671 					mp->mnt_vstat.f_bsize;
1672 		mp->mnt_stat.f_bavail = mp->mnt_stat.f_bfree;
1673 
1674 		*sbp = mp->mnt_stat;
1675 	}
1676 	return (0);
1677 }
1678 
1679 static
1680 int
1681 hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred)
1682 {
1683 	hammer2_pfs_t *pmp;
1684 	hammer2_dev_t *hmp;
1685 	hammer2_blockref_t bref;
1686 	int i;
1687 
1688 	/*
1689 	 * NOTE: iroot might not have validated the cluster yet.
1690 	 */
1691 	pmp = MPTOPMP(mp);
1692 
1693 	mp->mnt_vstat.f_bsize = 0;
1694 	mp->mnt_vstat.f_files = 0;
1695 	mp->mnt_vstat.f_ffree = 0;
1696 	mp->mnt_vstat.f_blocks = 0;
1697 	mp->mnt_vstat.f_bfree = 0;
1698 	mp->mnt_vstat.f_bavail = 0;
1699 
1700 	for (i = 0; i < pmp->iroot->cluster.nchains; ++i) {
1701 		hmp = pmp->pfs_hmps[i];
1702 		if (hmp == NULL)
1703 			continue;
1704 		if (pmp->iroot->cluster.array[i].chain)
1705 			bref = pmp->iroot->cluster.array[i].chain->bref;
1706 		else
1707 			bzero(&bref, sizeof(bref));
1708 
1709 		mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE;
1710 		mp->mnt_vstat.f_files = bref.inode_count;
1711 		mp->mnt_vstat.f_ffree = 0;
1712 		mp->mnt_vstat.f_blocks = (bref.data_count +
1713 					 hmp->voldata.allocator_free) /
1714 					mp->mnt_vstat.f_bsize;
1715 		mp->mnt_vstat.f_bfree = hmp->voldata.allocator_free /
1716 					mp->mnt_vstat.f_bsize;
1717 		mp->mnt_vstat.f_bavail = mp->mnt_vstat.f_bfree;
1718 
1719 		*sbp = mp->mnt_vstat;
1720 	}
1721 	return (0);
1722 }
1723 
1724 /*
1725  * Mount-time recovery (RW mounts)
1726  *
1727  * Updates to the free block table are allowed to lag flushes by one
1728  * transaction.  In case of a crash, then on a fresh mount we must do an
1729  * incremental scan of the last committed transaction id and make sure that
1730  * all related blocks have been marked allocated.
1731  *
1732  * The super-root topology and each PFS has its own transaction id domain,
1733  * so we must track PFS boundary transitions.
1734  */
1735 struct hammer2_recovery_elm {
1736 	TAILQ_ENTRY(hammer2_recovery_elm) entry;
1737 	hammer2_chain_t *chain;
1738 	hammer2_tid_t sync_tid;
1739 };
1740 
1741 TAILQ_HEAD(hammer2_recovery_list, hammer2_recovery_elm);
1742 
1743 struct hammer2_recovery_info {
1744 	struct hammer2_recovery_list list;
1745 	hammer2_tid_t	mtid;
1746 	int	depth;
1747 };
1748 
1749 static int hammer2_recovery_scan(hammer2_dev_t *hmp,
1750 			hammer2_chain_t *parent,
1751 			struct hammer2_recovery_info *info,
1752 			hammer2_tid_t sync_tid);
1753 
1754 #define HAMMER2_RECOVERY_MAXDEPTH	10
1755 
1756 static
1757 int
1758 hammer2_recovery(hammer2_dev_t *hmp)
1759 {
1760 	struct hammer2_recovery_info info;
1761 	struct hammer2_recovery_elm *elm;
1762 	hammer2_chain_t *parent;
1763 	hammer2_tid_t sync_tid;
1764 	hammer2_tid_t mirror_tid;
1765 	int error;
1766 	int cumulative_error = 0;
1767 
1768 	hammer2_trans_init(hmp->spmp, 0);
1769 
1770 	sync_tid = hmp->voldata.freemap_tid;
1771 	mirror_tid = hmp->voldata.mirror_tid;
1772 
1773 	kprintf("hammer2 mount \"%s\": ", hmp->devrepname);
1774 	if (sync_tid >= mirror_tid) {
1775 		kprintf(" no recovery needed\n");
1776 	} else {
1777 		kprintf(" freemap recovery %016jx-%016jx\n",
1778 			sync_tid + 1, mirror_tid);
1779 	}
1780 
1781 	TAILQ_INIT(&info.list);
1782 	info.depth = 0;
1783 	parent = hammer2_chain_lookup_init(&hmp->vchain, 0);
1784 	cumulative_error = hammer2_recovery_scan(hmp, parent, &info, sync_tid);
1785 	hammer2_chain_lookup_done(parent);
1786 
1787 	while ((elm = TAILQ_FIRST(&info.list)) != NULL) {
1788 		TAILQ_REMOVE(&info.list, elm, entry);
1789 		parent = elm->chain;
1790 		sync_tid = elm->sync_tid;
1791 		kfree(elm, M_HAMMER2);
1792 
1793 		hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS);
1794 		error = hammer2_recovery_scan(hmp, parent, &info,
1795 					      hmp->voldata.freemap_tid);
1796 		hammer2_chain_unlock(parent);
1797 		hammer2_chain_drop(parent);	/* drop elm->chain ref */
1798 		if (error)
1799 			cumulative_error = error;
1800 	}
1801 	hammer2_trans_done(hmp->spmp);
1802 
1803 	return cumulative_error;
1804 }
1805 
1806 static
1807 int
1808 hammer2_recovery_scan(hammer2_dev_t *hmp, hammer2_chain_t *parent,
1809 		      struct hammer2_recovery_info *info,
1810 		      hammer2_tid_t sync_tid)
1811 {
1812 	const hammer2_inode_data_t *ripdata;
1813 	hammer2_chain_t *chain;
1814 	hammer2_blockref_t bref;
1815 	int cache_index;
1816 	int cumulative_error = 0;
1817 	int error;
1818 	int first;
1819 
1820 	/*
1821 	 * Adjust freemap to ensure that the block(s) are marked allocated.
1822 	 */
1823 	if (parent->bref.type != HAMMER2_BREF_TYPE_VOLUME) {
1824 		hammer2_freemap_adjust(hmp, &parent->bref,
1825 				       HAMMER2_FREEMAP_DORECOVER);
1826 	}
1827 
1828 	/*
1829 	 * Check type for recursive scan
1830 	 */
1831 	switch(parent->bref.type) {
1832 	case HAMMER2_BREF_TYPE_VOLUME:
1833 		/* data already instantiated */
1834 		break;
1835 	case HAMMER2_BREF_TYPE_INODE:
1836 		/*
1837 		 * Must instantiate data for DIRECTDATA test and also
1838 		 * for recursion.
1839 		 */
1840 		hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS);
1841 		ripdata = &hammer2_chain_rdata(parent)->ipdata;
1842 		if (ripdata->meta.op_flags & HAMMER2_OPFLAG_DIRECTDATA) {
1843 			/* not applicable to recovery scan */
1844 			hammer2_chain_unlock(parent);
1845 			return 0;
1846 		}
1847 		hammer2_chain_unlock(parent);
1848 		break;
1849 	case HAMMER2_BREF_TYPE_INDIRECT:
1850 		/*
1851 		 * Must instantiate data for recursion
1852 		 */
1853 		hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS);
1854 		hammer2_chain_unlock(parent);
1855 		break;
1856 	case HAMMER2_BREF_TYPE_DATA:
1857 	case HAMMER2_BREF_TYPE_FREEMAP:
1858 	case HAMMER2_BREF_TYPE_FREEMAP_NODE:
1859 	case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
1860 		/* not applicable to recovery scan */
1861 		return 0;
1862 		break;
1863 	default:
1864 		return EDOM;
1865 	}
1866 
1867 	/*
1868 	 * Defer operation if depth limit reached or if we are crossing a
1869 	 * PFS boundary.
1870 	 */
1871 	if (info->depth >= HAMMER2_RECOVERY_MAXDEPTH) {
1872 		struct hammer2_recovery_elm *elm;
1873 
1874 		elm = kmalloc(sizeof(*elm), M_HAMMER2, M_ZERO | M_WAITOK);
1875 		elm->chain = parent;
1876 		elm->sync_tid = sync_tid;
1877 		hammer2_chain_ref(parent);
1878 		TAILQ_INSERT_TAIL(&info->list, elm, entry);
1879 		/* unlocked by caller */
1880 
1881 		return(0);
1882 	}
1883 
1884 
1885 	/*
1886 	 * Recursive scan of the last flushed transaction only.  We are
1887 	 * doing this without pmp assignments so don't leave the chains
1888 	 * hanging around after we are done with them.
1889 	 */
1890 	cache_index = 0;
1891 	chain = NULL;
1892 	first = 1;
1893 
1894 	while (hammer2_chain_scan(parent, &chain, &bref,
1895 				  &first, &cache_index,
1896 				  HAMMER2_LOOKUP_NODATA) != NULL) {
1897 		/*
1898 		 * If this is a leaf
1899 		 */
1900 		if (chain == NULL) {
1901 			if (bref.mirror_tid > sync_tid) {
1902 				hammer2_freemap_adjust(hmp, &bref,
1903 						     HAMMER2_FREEMAP_DORECOVER);
1904 			}
1905 			continue;
1906 		}
1907 
1908 		/*
1909 		 * This may or may not be a recursive node.
1910 		 */
1911 		atomic_set_int(&chain->flags, HAMMER2_CHAIN_RELEASE);
1912 		if (bref.mirror_tid > sync_tid) {
1913 			++info->depth;
1914 			error = hammer2_recovery_scan(hmp, chain,
1915 						      info, sync_tid);
1916 			--info->depth;
1917 			if (error)
1918 				cumulative_error = error;
1919 		}
1920 
1921 		/*
1922 		 * Flush the recovery at the PFS boundary to stage it for
1923 		 * the final flush of the super-root topology.
1924 		 */
1925 		if ((bref.flags & HAMMER2_BREF_FLAG_PFSROOT) &&
1926 		    (chain->flags & HAMMER2_CHAIN_ONFLUSH)) {
1927 			hammer2_flush(chain, HAMMER2_FLUSH_TOP);
1928 		}
1929 	}
1930 
1931 	return cumulative_error;
1932 }
1933 
1934 /*
1935  * Sync a mount point; this is called on a per-mount basis from the
1936  * filesystem syncer process periodically and whenever a user issues
1937  * a sync.
1938  */
1939 int
1940 hammer2_vfs_sync(struct mount *mp, int waitfor)
1941 {
1942 	hammer2_xop_flush_t *xop;
1943 	struct hammer2_sync_info info;
1944 	hammer2_inode_t *iroot;
1945 	hammer2_pfs_t *pmp;
1946 	int flags;
1947 	int error;
1948 
1949 	pmp = MPTOPMP(mp);
1950 	iroot = pmp->iroot;
1951 	KKASSERT(iroot);
1952 	KKASSERT(iroot->pmp == pmp);
1953 
1954 	/*
1955 	 * We can't acquire locks on existing vnodes while in a transaction
1956 	 * without risking a deadlock.  This assumes that vfsync() can be
1957 	 * called without the vnode locked (which it can in DragonFly).
1958 	 * Otherwise we'd have to implement a multi-pass or flag the lock
1959 	 * failures and retry.
1960 	 *
1961 	 * The reclamation code interlocks with the sync list's token
1962 	 * (by removing the vnode from the scan list) before unlocking
1963 	 * the inode, giving us time to ref the inode.
1964 	 */
1965 	/*flags = VMSC_GETVP;*/
1966 	flags = 0;
1967 	if (waitfor & MNT_LAZY)
1968 		flags |= VMSC_ONEPASS;
1969 
1970 #if 0
1971 	/*
1972 	 * Preflush the vnodes using a normal transaction before interlocking
1973 	 * with a flush transaction.
1974 	 */
1975 	hammer2_trans_init(pmp, 0);
1976 	info.error = 0;
1977 	info.waitfor = MNT_NOWAIT;
1978 	vsyncscan(mp, flags | VMSC_NOWAIT, hammer2_sync_scan2, &info);
1979 	hammer2_trans_done(pmp);
1980 #endif
1981 
1982 	/*
1983 	 * Start our flush transaction.  This does not return until all
1984 	 * concurrent transactions have completed and will prevent any
1985 	 * new transactions from running concurrently, except for the
1986 	 * buffer cache transactions.
1987 	 *
1988 	 * For efficiency do an async pass before making sure with a
1989 	 * synchronous pass on all related buffer cache buffers.  It
1990 	 * should theoretically not be possible for any new file buffers
1991 	 * to be instantiated during this sequence.
1992 	 */
1993 	hammer2_trans_init(pmp, HAMMER2_TRANS_ISFLUSH |
1994 			        HAMMER2_TRANS_PREFLUSH);
1995 	hammer2_inode_run_sideq(pmp);
1996 
1997 	info.error = 0;
1998 	info.waitfor = MNT_NOWAIT;
1999 	vsyncscan(mp, flags | VMSC_NOWAIT, hammer2_sync_scan2, &info);
2000 	info.waitfor = MNT_WAIT;
2001 	vsyncscan(mp, flags, hammer2_sync_scan2, &info);
2002 
2003 	/*
2004 	 * Clear PREFLUSH.  This prevents (or asserts on) any new logical
2005 	 * buffer cache flushes which occur during the flush.  Device buffers
2006 	 * are not affected.
2007 	 */
2008 	hammer2_bioq_sync(pmp);
2009 	hammer2_trans_clear_preflush(pmp);
2010 
2011 	/*
2012 	 * Use the XOP interface to concurrently flush all nodes to
2013 	 * synchronize the PFSROOT subtopology to the media.  A standard
2014 	 * end-of-scan ENOENT error indicates cluster sufficiency.
2015 	 *
2016 	 * Note that this flush will not be visible on crash recovery until
2017 	 * we flush the super-root topology in the next loop.
2018 	 *
2019 	 * XXX For now wait for all flushes to complete.
2020 	 */
2021 	if (iroot) {
2022 		xop = hammer2_xop_alloc(iroot, HAMMER2_XOP_MODIFYING);
2023 		hammer2_xop_start(&xop->head, hammer2_inode_xop_flush);
2024 		error = hammer2_xop_collect(&xop->head,
2025 					    HAMMER2_XOP_COLLECT_WAITALL);
2026 		hammer2_xop_retire(&xop->head, HAMMER2_XOPMASK_VOP);
2027 		if (error == ENOENT)
2028 			error = 0;
2029 	} else {
2030 		error = 0;
2031 	}
2032 	hammer2_trans_done(pmp);
2033 
2034 	return (error);
2035 }
2036 
2037 /*
2038  * Sync passes.
2039  *
2040  * Note that we ignore the tranasction mtid we got above.  Instead,
2041  * each vfsync below will ultimately get its own via TRANS_BUFCACHE
2042  * transactions.
2043  */
2044 static int
2045 hammer2_sync_scan2(struct mount *mp, struct vnode *vp, void *data)
2046 {
2047 	struct hammer2_sync_info *info = data;
2048 	hammer2_inode_t *ip;
2049 	int error;
2050 
2051 	/*
2052 	 * Degenerate cases.  Note that ip == NULL typically means the
2053 	 * syncer vnode itself and we don't want to vclrisdirty() in that
2054 	 * situation.
2055 	 */
2056 	ip = VTOI(vp);
2057 	if (ip == NULL) {
2058 		return(0);
2059 	}
2060 	if (vp->v_type == VNON || vp->v_type == VBAD) {
2061 		vclrisdirty(vp);
2062 		return(0);
2063 	}
2064 
2065 	/*
2066 	 * VOP_FSYNC will start a new transaction so replicate some code
2067 	 * here to do it inline (see hammer2_vop_fsync()).
2068 	 *
2069 	 * WARNING: The vfsync interacts with the buffer cache and might
2070 	 *          block, we can't hold the inode lock at that time.
2071 	 *	    However, we MUST ref ip before blocking to ensure that
2072 	 *	    it isn't ripped out from under us (since we do not
2073 	 *	    hold a lock on the vnode).
2074 	 */
2075 	hammer2_inode_ref(ip);
2076 	if ((ip->flags & HAMMER2_INODE_MODIFIED) ||
2077 	    !RB_EMPTY(&vp->v_rbdirty_tree)) {
2078 		vfsync(vp, info->waitfor, 1, NULL, NULL);
2079 		if (ip->flags & (HAMMER2_INODE_RESIZED |
2080 				 HAMMER2_INODE_MODIFIED)) {
2081 			hammer2_inode_lock(ip, 0);
2082 			hammer2_inode_chain_sync(ip);
2083 			hammer2_inode_unlock(ip);
2084 		}
2085 	}
2086 	if ((ip->flags & HAMMER2_INODE_MODIFIED) == 0 &&
2087 	    RB_EMPTY(&vp->v_rbdirty_tree)) {
2088 		vclrisdirty(vp);
2089 	}
2090 
2091 	hammer2_inode_drop(ip);
2092 #if 1
2093 	error = 0;
2094 	if (error)
2095 		info->error = error;
2096 #endif
2097 	return(0);
2098 }
2099 
2100 static
2101 int
2102 hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp)
2103 {
2104 	return (0);
2105 }
2106 
2107 static
2108 int
2109 hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
2110 	       struct fid *fhp, struct vnode **vpp)
2111 {
2112 	return (0);
2113 }
2114 
2115 static
2116 int
2117 hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
2118 		 int *exflagsp, struct ucred **credanonp)
2119 {
2120 	return (0);
2121 }
2122 
2123 /*
2124  * Support code for hammer2_vfs_mount().  Read, verify, and install the volume
2125  * header into the HMP
2126  *
2127  * XXX read four volhdrs and use the one with the highest TID whos CRC
2128  *     matches.
2129  *
2130  * XXX check iCRCs.
2131  *
2132  * XXX For filesystems w/ less than 4 volhdrs, make sure to not write to
2133  *     nonexistant locations.
2134  *
2135  * XXX Record selected volhdr and ring updates to each of 4 volhdrs
2136  */
2137 static
2138 int
2139 hammer2_install_volume_header(hammer2_dev_t *hmp)
2140 {
2141 	hammer2_volume_data_t *vd;
2142 	struct buf *bp;
2143 	hammer2_crc32_t crc0, crc, bcrc0, bcrc;
2144 	int error_reported;
2145 	int error;
2146 	int valid;
2147 	int i;
2148 
2149 	error_reported = 0;
2150 	error = 0;
2151 	valid = 0;
2152 	bp = NULL;
2153 
2154 	/*
2155 	 * There are up to 4 copies of the volume header (syncs iterate
2156 	 * between them so there is no single master).  We don't trust the
2157 	 * volu_size field so we don't know precisely how large the filesystem
2158 	 * is, so depend on the OS to return an error if we go beyond the
2159 	 * block device's EOF.
2160 	 */
2161 	for (i = 0; i < HAMMER2_NUM_VOLHDRS; i++) {
2162 		error = bread(hmp->devvp, i * HAMMER2_ZONE_BYTES64,
2163 			      HAMMER2_VOLUME_BYTES, &bp);
2164 		if (error) {
2165 			brelse(bp);
2166 			bp = NULL;
2167 			continue;
2168 		}
2169 
2170 		vd = (struct hammer2_volume_data *) bp->b_data;
2171 		if ((vd->magic != HAMMER2_VOLUME_ID_HBO) &&
2172 		    (vd->magic != HAMMER2_VOLUME_ID_ABO)) {
2173 			brelse(bp);
2174 			bp = NULL;
2175 			continue;
2176 		}
2177 
2178 		if (vd->magic == HAMMER2_VOLUME_ID_ABO) {
2179 			/* XXX: Reversed-endianness filesystem */
2180 			kprintf("hammer2: reverse-endian filesystem detected");
2181 			brelse(bp);
2182 			bp = NULL;
2183 			continue;
2184 		}
2185 
2186 		crc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT0];
2187 		crc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC0_OFF,
2188 				      HAMMER2_VOLUME_ICRC0_SIZE);
2189 		bcrc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT1];
2190 		bcrc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC1_OFF,
2191 				       HAMMER2_VOLUME_ICRC1_SIZE);
2192 		if ((crc0 != crc) || (bcrc0 != bcrc)) {
2193 			kprintf("hammer2 volume header crc "
2194 				"mismatch copy #%d %08x/%08x\n",
2195 				i, crc0, crc);
2196 			error_reported = 1;
2197 			brelse(bp);
2198 			bp = NULL;
2199 			continue;
2200 		}
2201 		if (valid == 0 || hmp->voldata.mirror_tid < vd->mirror_tid) {
2202 			valid = 1;
2203 			hmp->voldata = *vd;
2204 			hmp->volhdrno = i;
2205 		}
2206 		brelse(bp);
2207 		bp = NULL;
2208 	}
2209 	if (valid) {
2210 		hmp->volsync = hmp->voldata;
2211 		error = 0;
2212 		if (error_reported || bootverbose || 1) { /* 1/DEBUG */
2213 			kprintf("hammer2: using volume header #%d\n",
2214 				hmp->volhdrno);
2215 		}
2216 	} else {
2217 		error = EINVAL;
2218 		kprintf("hammer2: no valid volume headers found!\n");
2219 	}
2220 	return (error);
2221 }
2222 
2223 /*
2224  * This handles hysteresis on regular file flushes.  Because the BIOs are
2225  * routed to a thread it is possible for an excessive number to build up
2226  * and cause long front-end stalls long before the runningbuffspace limit
2227  * is hit, so we implement hammer2_flush_pipe to control the
2228  * hysteresis.
2229  *
2230  * This is a particular problem when compression is used.
2231  */
2232 void
2233 hammer2_lwinprog_ref(hammer2_pfs_t *pmp)
2234 {
2235 	atomic_add_int(&pmp->count_lwinprog, 1);
2236 }
2237 
2238 void
2239 hammer2_lwinprog_drop(hammer2_pfs_t *pmp)
2240 {
2241 	int lwinprog;
2242 
2243 	lwinprog = atomic_fetchadd_int(&pmp->count_lwinprog, -1);
2244 	if ((lwinprog & HAMMER2_LWINPROG_WAITING) &&
2245 	    (lwinprog & HAMMER2_LWINPROG_MASK) <= hammer2_flush_pipe * 2 / 3) {
2246 		atomic_clear_int(&pmp->count_lwinprog,
2247 				 HAMMER2_LWINPROG_WAITING);
2248 		wakeup(&pmp->count_lwinprog);
2249 	}
2250 	if ((lwinprog & HAMMER2_LWINPROG_WAITING0) &&
2251 	    (lwinprog & HAMMER2_LWINPROG_MASK) <= 0) {
2252 		atomic_clear_int(&pmp->count_lwinprog,
2253 				 HAMMER2_LWINPROG_WAITING0);
2254 		wakeup(&pmp->count_lwinprog);
2255 	}
2256 }
2257 
2258 void
2259 hammer2_lwinprog_wait(hammer2_pfs_t *pmp, int flush_pipe)
2260 {
2261 	int lwinprog;
2262 	int lwflag = (flush_pipe) ? HAMMER2_LWINPROG_WAITING :
2263 				    HAMMER2_LWINPROG_WAITING0;
2264 
2265 	for (;;) {
2266 		lwinprog = pmp->count_lwinprog;
2267 		cpu_ccfence();
2268 		if ((lwinprog & HAMMER2_LWINPROG_MASK) <= flush_pipe)
2269 			break;
2270 		tsleep_interlock(&pmp->count_lwinprog, 0);
2271 		atomic_set_int(&pmp->count_lwinprog, lwflag);
2272 		lwinprog = pmp->count_lwinprog;
2273 		if ((lwinprog & HAMMER2_LWINPROG_MASK) <= flush_pipe)
2274 			break;
2275 		tsleep(&pmp->count_lwinprog, PINTERLOCKED, "h2wpipe", hz);
2276 	}
2277 }
2278 
2279 /*
2280  * Manage excessive memory resource use for chain and related
2281  * structures.
2282  */
2283 void
2284 hammer2_pfs_memory_wait(hammer2_pfs_t *pmp)
2285 {
2286 	uint32_t waiting;
2287 	uint32_t count;
2288 	uint32_t limit;
2289 #if 0
2290 	static int zzticks;
2291 #endif
2292 
2293 	/*
2294 	 * Atomic check condition and wait.  Also do an early speedup of
2295 	 * the syncer to try to avoid hitting the wait.
2296 	 */
2297 	for (;;) {
2298 		waiting = pmp->inmem_dirty_chains;
2299 		cpu_ccfence();
2300 		count = waiting & HAMMER2_DIRTYCHAIN_MASK;
2301 
2302 		limit = pmp->mp->mnt_nvnodelistsize / 10;
2303 		if (limit < hammer2_limit_dirty_chains)
2304 			limit = hammer2_limit_dirty_chains;
2305 		if (limit < 1000)
2306 			limit = 1000;
2307 
2308 #if 0
2309 		if ((int)(ticks - zzticks) > hz) {
2310 			zzticks = ticks;
2311 			kprintf("count %ld %ld\n", count, limit);
2312 		}
2313 #endif
2314 
2315 		/*
2316 		 * Block if there are too many dirty chains present, wait
2317 		 * for the flush to clean some out.
2318 		 */
2319 		if (count > limit) {
2320 			tsleep_interlock(&pmp->inmem_dirty_chains, 0);
2321 			if (atomic_cmpset_int(&pmp->inmem_dirty_chains,
2322 					       waiting,
2323 				       waiting | HAMMER2_DIRTYCHAIN_WAITING)) {
2324 				speedup_syncer(pmp->mp);
2325 				tsleep(&pmp->inmem_dirty_chains, PINTERLOCKED,
2326 				       "chnmem", hz);
2327 			}
2328 			continue;	/* loop on success or fail */
2329 		}
2330 
2331 		/*
2332 		 * Try to start an early flush before we are forced to block.
2333 		 */
2334 		if (count > limit * 7 / 10)
2335 			speedup_syncer(pmp->mp);
2336 		break;
2337 	}
2338 }
2339 
2340 void
2341 hammer2_pfs_memory_inc(hammer2_pfs_t *pmp)
2342 {
2343 	if (pmp) {
2344 		atomic_add_int(&pmp->inmem_dirty_chains, 1);
2345 	}
2346 }
2347 
2348 void
2349 hammer2_pfs_memory_wakeup(hammer2_pfs_t *pmp)
2350 {
2351 	uint32_t waiting;
2352 
2353 	if (pmp == NULL)
2354 		return;
2355 
2356 	for (;;) {
2357 		waiting = pmp->inmem_dirty_chains;
2358 		cpu_ccfence();
2359 		if (atomic_cmpset_int(&pmp->inmem_dirty_chains,
2360 				       waiting,
2361 				       (waiting - 1) &
2362 					~HAMMER2_DIRTYCHAIN_WAITING)) {
2363 			break;
2364 		}
2365 	}
2366 
2367 	if (waiting & HAMMER2_DIRTYCHAIN_WAITING)
2368 		wakeup(&pmp->inmem_dirty_chains);
2369 }
2370 
2371 /*
2372  * Debugging
2373  */
2374 void
2375 hammer2_dump_chain(hammer2_chain_t *chain, int tab, int *countp, char pfx)
2376 {
2377 	hammer2_chain_t *scan;
2378 	hammer2_chain_t *parent;
2379 
2380 	--*countp;
2381 	if (*countp == 0) {
2382 		kprintf("%*.*s...\n", tab, tab, "");
2383 		return;
2384 	}
2385 	if (*countp < 0)
2386 		return;
2387 	kprintf("%*.*s%c-chain %p.%d %016jx/%d mir=%016jx\n",
2388 		tab, tab, "", pfx,
2389 		chain, chain->bref.type,
2390 		chain->bref.key, chain->bref.keybits,
2391 		chain->bref.mirror_tid);
2392 
2393 	kprintf("%*.*s      [%08x] (%s) refs=%d",
2394 		tab, tab, "",
2395 		chain->flags,
2396 		((chain->bref.type == HAMMER2_BREF_TYPE_INODE &&
2397 		chain->data) ?  (char *)chain->data->ipdata.filename : "?"),
2398 		chain->refs);
2399 
2400 	parent = chain->parent;
2401 	if (parent)
2402 		kprintf("\n%*.*s      p=%p [pflags %08x prefs %d",
2403 			tab, tab, "",
2404 			parent, parent->flags, parent->refs);
2405 	if (RB_EMPTY(&chain->core.rbtree)) {
2406 		kprintf("\n");
2407 	} else {
2408 		kprintf(" {\n");
2409 		RB_FOREACH(scan, hammer2_chain_tree, &chain->core.rbtree)
2410 			hammer2_dump_chain(scan, tab + 4, countp, 'a');
2411 		if (chain->bref.type == HAMMER2_BREF_TYPE_INODE && chain->data)
2412 			kprintf("%*.*s}(%s)\n", tab, tab, "",
2413 				chain->data->ipdata.filename);
2414 		else
2415 			kprintf("%*.*s}\n", tab, tab, "");
2416 	}
2417 }
2418