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