44fa6e8dbe38e42a8ca323f5e2208f7e6fbcad00
[dragonfly.git] / sys / vfs / hammer2 / hammer2_vfsops.c
1 /*-
2  * Copyright (c) 2011-2013 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  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in
15  *    the documentation and/or other materials provided with the
16  *    distribution.
17  * 3. Neither the name of The DragonFly Project nor the names of its
18  *    contributors may be used to endorse or promote products derived
19  *    from this software without specific, prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32  * SUCH DAMAGE.
33  */
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/nlookup.h>
38 #include <sys/vnode.h>
39 #include <sys/mount.h>
40 #include <sys/fcntl.h>
41 #include <sys/buf.h>
42 #include <sys/uuid.h>
43 #include <sys/vfsops.h>
44 #include <sys/sysctl.h>
45 #include <sys/socket.h>
46
47 #include "hammer2.h"
48 #include "hammer2_disk.h"
49 #include "hammer2_mount.h"
50
51 #define REPORT_REFS_ERRORS 1    /* XXX remove me */
52
53 struct hammer2_sync_info {
54         hammer2_trans_t trans;
55         int error;
56         int waitfor;
57 };
58
59 TAILQ_HEAD(hammer2_mntlist, hammer2_mount);
60 static struct hammer2_mntlist hammer2_mntlist;
61 static struct lock hammer2_mntlk;
62
63 int hammer2_debug;
64 int hammer2_cluster_enable = 1;
65 int hammer2_hardlink_enable = 1;
66 long hammer2_iod_file_read;
67 long hammer2_iod_meta_read;
68 long hammer2_iod_indr_read;
69 long hammer2_iod_file_write;
70 long hammer2_iod_meta_write;
71 long hammer2_iod_indr_write;
72 long hammer2_iod_fmap_write;
73 long hammer2_iod_volu_write;
74 long hammer2_ioa_file_read;
75 long hammer2_ioa_meta_read;
76 long hammer2_ioa_indr_read;
77 long hammer2_ioa_fmap_write;
78 long hammer2_ioa_file_write;
79 long hammer2_ioa_meta_write;
80 long hammer2_ioa_indr_write;
81 long hammer2_ioa_volu_write;
82
83 SYSCTL_NODE(_vfs, OID_AUTO, hammer2, CTLFLAG_RW, 0, "HAMMER2 filesystem");
84
85 SYSCTL_INT(_vfs_hammer2, OID_AUTO, debug, CTLFLAG_RW,
86            &hammer2_debug, 0, "");
87 SYSCTL_INT(_vfs_hammer2, OID_AUTO, cluster_enable, CTLFLAG_RW,
88            &hammer2_cluster_enable, 0, "");
89 SYSCTL_INT(_vfs_hammer2, OID_AUTO, hardlink_enable, CTLFLAG_RW,
90            &hammer2_hardlink_enable, 0, "");
91 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_read, CTLFLAG_RW,
92            &hammer2_iod_file_read, 0, "");
93 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_meta_read, CTLFLAG_RW,
94            &hammer2_iod_meta_read, 0, "");
95 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_indr_read, CTLFLAG_RW,
96            &hammer2_iod_indr_read, 0, "");
97 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_write, CTLFLAG_RW,
98            &hammer2_iod_file_write, 0, "");
99 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_meta_write, CTLFLAG_RW,
100            &hammer2_iod_meta_write, 0, "");
101 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_indr_write, CTLFLAG_RW,
102            &hammer2_iod_indr_write, 0, "");
103 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_volu_write, CTLFLAG_RW,
104            &hammer2_iod_volu_write, 0, "");
105 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_file_read, CTLFLAG_RW,
106            &hammer2_ioa_file_read, 0, "");
107 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_meta_read, CTLFLAG_RW,
108            &hammer2_ioa_meta_read, 0, "");
109 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_indr_read, CTLFLAG_RW,
110            &hammer2_ioa_indr_read, 0, "");
111 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_file_write, CTLFLAG_RW,
112            &hammer2_ioa_file_write, 0, "");
113 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_meta_write, CTLFLAG_RW,
114            &hammer2_ioa_meta_write, 0, "");
115 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_indr_write, CTLFLAG_RW,
116            &hammer2_ioa_indr_write, 0, "");
117 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_volu_write, CTLFLAG_RW,
118            &hammer2_ioa_volu_write, 0, "");
119
120 static int hammer2_vfs_init(struct vfsconf *conf);
121 static int hammer2_vfs_mount(struct mount *mp, char *path, caddr_t data,
122                                 struct ucred *cred);
123 static int hammer2_remount(struct mount *, char *, struct vnode *,
124                                 struct ucred *);
125 static int hammer2_vfs_unmount(struct mount *mp, int mntflags);
126 static int hammer2_vfs_root(struct mount *mp, struct vnode **vpp);
127 static int hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp,
128                                 struct ucred *cred);
129 static int hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp,
130                                 struct ucred *cred);
131 static int hammer2_vfs_sync(struct mount *mp, int waitfor);
132 static int hammer2_vfs_vget(struct mount *mp, struct vnode *dvp,
133                                 ino_t ino, struct vnode **vpp);
134 static int hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
135                                 struct fid *fhp, struct vnode **vpp);
136 static int hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp);
137 static int hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
138                                 int *exflagsp, struct ucred **credanonp);
139
140 static int hammer2_install_volume_header(hammer2_mount_t *hmp);
141 static int hammer2_sync_scan1(struct mount *mp, struct vnode *vp, void *data);
142 static int hammer2_sync_scan2(struct mount *mp, struct vnode *vp, void *data);
143
144 static int hammer2_rcvdmsg(kdmsg_msg_t *msg);
145 static void hammer2_autodmsg(kdmsg_msg_t *msg);
146
147 /*
148  * HAMMER2 vfs operations.
149  */
150 static struct vfsops hammer2_vfsops = {
151         .vfs_init       = hammer2_vfs_init,
152         .vfs_sync       = hammer2_vfs_sync,
153         .vfs_mount      = hammer2_vfs_mount,
154         .vfs_unmount    = hammer2_vfs_unmount,
155         .vfs_root       = hammer2_vfs_root,
156         .vfs_statfs     = hammer2_vfs_statfs,
157         .vfs_statvfs    = hammer2_vfs_statvfs,
158         .vfs_vget       = hammer2_vfs_vget,
159         .vfs_vptofh     = hammer2_vfs_vptofh,
160         .vfs_fhtovp     = hammer2_vfs_fhtovp,
161         .vfs_checkexp   = hammer2_vfs_checkexp
162 };
163
164 MALLOC_DEFINE(M_HAMMER2, "HAMMER2-mount", "");
165
166 VFS_SET(hammer2_vfsops, hammer2, 0);
167 MODULE_VERSION(hammer2, 1);
168
169 static
170 int
171 hammer2_vfs_init(struct vfsconf *conf)
172 {
173         int error;
174
175         error = 0;
176
177         if (HAMMER2_BLOCKREF_BYTES != sizeof(struct hammer2_blockref))
178                 error = EINVAL;
179         if (HAMMER2_INODE_BYTES != sizeof(struct hammer2_inode_data))
180                 error = EINVAL;
181         if (HAMMER2_VOLUME_BYTES != sizeof(struct hammer2_volume_data))
182                 error = EINVAL;
183
184         if (error)
185                 kprintf("HAMMER2 structure size mismatch; cannot continue.\n");
186
187         lockinit(&hammer2_mntlk, "mntlk", 0, 0);
188         TAILQ_INIT(&hammer2_mntlist);
189
190         return (error);
191 }
192
193 /*
194  * Mount or remount HAMMER2 fileystem from physical media
195  *
196  *      mountroot
197  *              mp              mount point structure
198  *              path            NULL
199  *              data            <unused>
200  *              cred            <unused>
201  *
202  *      mount
203  *              mp              mount point structure
204  *              path            path to mount point
205  *              data            pointer to argument structure in user space
206  *                      volume  volume path (device@LABEL form)
207  *                      hflags  user mount flags
208  *              cred            user credentials
209  *
210  * RETURNS:     0       Success
211  *              !0      error number
212  */
213 static
214 int
215 hammer2_vfs_mount(struct mount *mp, char *path, caddr_t data,
216                   struct ucred *cred)
217 {
218         struct hammer2_mount_info info;
219         hammer2_pfsmount_t *pmp;
220         hammer2_mount_t *hmp;
221         hammer2_key_t lhc;
222         struct vnode *devvp;
223         struct nlookupdata nd;
224         hammer2_chain_t *parent;
225         hammer2_chain_t *schain;
226         hammer2_chain_t *rchain;
227         struct file *fp;
228         char devstr[MNAMELEN];
229         size_t size;
230         size_t done;
231         char *dev;
232         char *label;
233         int ronly = 1;
234         int create_hmp;
235         int error;
236
237         hmp = NULL;
238         pmp = NULL;
239         dev = NULL;
240         label = NULL;
241         devvp = NULL;
242
243         kprintf("hammer2_mount\n");
244
245         if (path == NULL) {
246                 /*
247                  * Root mount
248                  */
249                 bzero(&info, sizeof(info));
250                 info.cluster_fd = -1;
251                 return (EOPNOTSUPP);
252         } else {
253                 /*
254                  * Non-root mount or updating a mount
255                  */
256                 error = copyin(data, &info, sizeof(info));
257                 if (error)
258                         return (error);
259
260                 error = copyinstr(info.volume, devstr, MNAMELEN - 1, &done);
261                 if (error)
262                         return (error);
263
264                 /* Extract device and label */
265                 dev = devstr;
266                 label = strchr(devstr, '@');
267                 if (label == NULL ||
268                     ((label + 1) - dev) > done) {
269                         return (EINVAL);
270                 }
271                 *label = '\0';
272                 label++;
273                 if (*label == '\0')
274                         return (EINVAL);
275
276                 if (mp->mnt_flag & MNT_UPDATE) {
277                         /* Update mount */
278                         /* HAMMER2 implements NFS export via mountctl */
279                         hmp = MPTOHMP(mp);
280                         devvp = hmp->devvp;
281                         error = hammer2_remount(mp, path, devvp, cred);
282                         return error;
283                 }
284         }
285
286         /*
287          * PFS mount
288          *
289          * Lookup name and verify it refers to a block device.
290          */
291         error = nlookup_init(&nd, dev, UIO_SYSSPACE, NLC_FOLLOW);
292         if (error == 0)
293                 error = nlookup(&nd);
294         if (error == 0)
295                 error = cache_vref(&nd.nl_nch, nd.nl_cred, &devvp);
296         nlookup_done(&nd);
297
298         if (error == 0) {
299                 if (vn_isdisk(devvp, &error))
300                         error = vfs_mountedon(devvp);
301         }
302
303         /*
304          * Determine if the device has already been mounted.  After this
305          * check hmp will be non-NULL if we are doing the second or more
306          * hammer2 mounts from the same device.
307          */
308         lockmgr(&hammer2_mntlk, LK_EXCLUSIVE);
309         TAILQ_FOREACH(hmp, &hammer2_mntlist, mntentry) {
310                 if (hmp->devvp == devvp)
311                         break;
312         }
313
314         /*
315          * Open the device if this isn't a secondary mount
316          */
317         if (hmp) {
318                 create_hmp = 0;
319         } else {
320                 create_hmp = 1;
321                 if (error == 0 && vcount(devvp) > 0)
322                         error = EBUSY;
323
324                 /*
325                  * Now open the device
326                  */
327                 if (error == 0) {
328                         ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
329                         vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
330                         error = vinvalbuf(devvp, V_SAVE, 0, 0);
331                         if (error == 0) {
332                                 error = VOP_OPEN(devvp,
333                                                  ronly ? FREAD : FREAD | FWRITE,
334                                                  FSCRED, NULL);
335                         }
336                         vn_unlock(devvp);
337                 }
338                 if (error && devvp) {
339                         vrele(devvp);
340                         devvp = NULL;
341                 }
342                 if (error) {
343                         lockmgr(&hammer2_mntlk, LK_RELEASE);
344                         return error;
345                 }
346         }
347
348         /*
349          * Block device opened successfully, finish initializing the
350          * mount structure.
351          *
352          * From this point on we have to call hammer2_unmount() on failure.
353          */
354         pmp = kmalloc(sizeof(*pmp), M_HAMMER2, M_WAITOK | M_ZERO);
355         mp->mnt_data = (qaddr_t)pmp;
356         pmp->mp = mp;
357
358         spin_init(&pmp->inum_spin);
359         RB_INIT(&pmp->inum_tree);
360
361         kmalloc_create(&pmp->mmsg, "HAMMER2-pfsmsg");
362         kdmsg_iocom_init(&pmp->iocom, pmp,
363                          KDMSG_IOCOMF_AUTOCONN |
364                          KDMSG_IOCOMF_AUTOSPAN |
365                          KDMSG_IOCOMF_AUTOCIRC,
366                          pmp->mmsg, hammer2_rcvdmsg);
367
368         if (create_hmp) {
369                 hmp = kmalloc(sizeof(*hmp), M_HAMMER2, M_WAITOK | M_ZERO);
370                 hmp->ronly = ronly;
371                 hmp->devvp = devvp;
372                 kmalloc_create(&hmp->minode, "HAMMER2-inodes");
373                 kmalloc_create(&hmp->mchain, "HAMMER2-chains");
374                 TAILQ_INSERT_TAIL(&hammer2_mntlist, hmp, mntentry);
375         }
376         ccms_domain_init(&pmp->ccms_dom);
377         pmp->hmp = hmp;
378         ++hmp->pmp_count;
379         lockmgr(&hammer2_mntlk, LK_RELEASE);
380         kprintf("hammer2_mount hmp=%p pmpcnt=%d\n", hmp, hmp->pmp_count);
381         
382         mp->mnt_flag = MNT_LOCAL;
383         mp->mnt_kern_flag |= MNTK_ALL_MPSAFE;   /* all entry pts are SMP */
384
385         if (create_hmp) {
386                 /*
387                  * Presetup
388                  */
389                 lockinit(&hmp->alloclk, "h2alloc", 0, 0);
390                 lockinit(&hmp->voldatalk, "voldata", 0, LK_CANRECURSE);
391
392                 /*
393                  * vchain setup. vchain.data is special cased to NULL.
394                  * vchain.refs is initialized and will never drop to 0.
395                  */
396                 hmp->vchain.hmp = hmp;
397                 hmp->vchain.refs = 1;
398                 hmp->vchain.data = (void *)&hmp->voldata;
399                 hmp->vchain.bref.type = HAMMER2_BREF_TYPE_VOLUME;
400                 hmp->vchain.bref.data_off = 0 | HAMMER2_PBUFRADIX;
401                 hammer2_chain_core_alloc(&hmp->vchain, NULL);
402                 /* hmp->vchain.u.xxx is left NULL */
403
404                 /*
405                  * Install the volume header
406                  */
407                 error = hammer2_install_volume_header(hmp);
408                 if (error) {
409                         hammer2_vfs_unmount(mp, MNT_FORCE);
410                         return error;
411                 }
412         }
413
414         /*
415          * required mount structure initializations
416          */
417         mp->mnt_stat.f_iosize = HAMMER2_PBUFSIZE;
418         mp->mnt_stat.f_bsize = HAMMER2_PBUFSIZE;
419
420         mp->mnt_vstat.f_frsize = HAMMER2_PBUFSIZE;
421         mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE;
422
423         /*
424          * Optional fields
425          */
426         mp->mnt_iosize_max = MAXPHYS;
427
428         /*
429          * First locate the super-root inode, which is key 0 relative to the
430          * volume header's blockset.
431          *
432          * Then locate the root inode by scanning the directory keyspace
433          * represented by the label.
434          */
435         if (create_hmp) {
436                 parent = hammer2_chain_lookup_init(&hmp->vchain, 0);
437                 schain = hammer2_chain_lookup(&parent,
438                                       HAMMER2_SROOT_KEY, HAMMER2_SROOT_KEY, 0);
439                 hammer2_chain_lookup_done(parent);
440                 if (schain == NULL) {
441                         kprintf("hammer2_mount: invalid super-root\n");
442                         hammer2_vfs_unmount(mp, MNT_FORCE);
443                         return EINVAL;
444                 }
445                 hammer2_chain_ref(schain);      /* for hmp->schain */
446                 hmp->schain = schain;           /* left locked for inode_get */
447                 hmp->sroot = hammer2_inode_get(hmp, NULL, NULL, schain);
448                 hammer2_inode_ref(hmp->sroot);       /* for hmp->sroot */
449                 hammer2_inode_unlock_ex(hmp->sroot); /* eats schain lock */
450         } else {
451                 schain = hmp->schain;
452         }
453
454         /*
455          * schain only has 1 ref now for its hmp->schain assignment.
456          * Setup for lookup (which will lock it).
457          */
458         parent = hammer2_chain_lookup_init(schain, 0);
459         lhc = hammer2_dirhash(label, strlen(label));
460         rchain = hammer2_chain_lookup(&parent,
461                                       lhc, lhc + HAMMER2_DIRHASH_LOMASK,
462                                       0);
463         while (rchain) {
464                 if (rchain->bref.type == HAMMER2_BREF_TYPE_INODE &&
465                     strcmp(label, rchain->data->ipdata.filename) == 0) {
466                         break;
467                 }
468                 rchain = hammer2_chain_next(&parent, rchain,
469                                             lhc, lhc + HAMMER2_DIRHASH_LOMASK,
470                                             0);
471         }
472         hammer2_chain_lookup_done(parent);
473         if (rchain == NULL) {
474                 kprintf("hammer2_mount: PFS label not found\n");
475                 hammer2_vfs_unmount(mp, MNT_FORCE);
476                 return EINVAL;
477         }
478         if (rchain->flags & HAMMER2_CHAIN_MOUNTED) {
479                 hammer2_chain_unlock(rchain);
480                 kprintf("hammer2_mount: PFS label already mounted!\n");
481                 hammer2_vfs_unmount(mp, MNT_FORCE);
482                 return EBUSY;
483         }
484         atomic_set_int(&rchain->flags, HAMMER2_CHAIN_MOUNTED);
485
486         /*
487          * NOTE: *_get() integrates chain's lock into the inode lock.
488          */
489         hammer2_chain_ref(rchain);              /* for pmp->rchain */
490         pmp->rchain = rchain;                   /* left held & unlocked */
491         pmp->iroot = hammer2_inode_get(hmp, pmp, NULL, rchain);
492         hammer2_inode_ref(pmp->iroot);          /* ref for pmp->iroot */
493         hammer2_inode_unlock_ex(pmp->iroot);    /* iroot & its chain */
494
495         kprintf("iroot %p\n", pmp->iroot);
496
497         /*
498          * Ref the cluster management messaging descriptor.  The mount
499          * program deals with the other end of the communications pipe.
500          */
501         fp = holdfp(curproc->p_fd, info.cluster_fd, -1);
502         if (fp == NULL) {
503                 kprintf("hammer2_mount: bad cluster_fd!\n");
504                 hammer2_vfs_unmount(mp, MNT_FORCE);
505                 return EBADF;
506         }
507         hammer2_cluster_reconnect(pmp, fp);
508
509         /*
510          * Finish setup
511          */
512         vfs_getnewfsid(mp);
513         vfs_add_vnodeops(mp, &hammer2_vnode_vops, &mp->mnt_vn_norm_ops);
514         vfs_add_vnodeops(mp, &hammer2_spec_vops, &mp->mnt_vn_spec_ops);
515         vfs_add_vnodeops(mp, &hammer2_fifo_vops, &mp->mnt_vn_fifo_ops);
516
517         copyinstr(info.volume, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size);
518         bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
519         bzero(mp->mnt_stat.f_mntonname, sizeof(mp->mnt_stat.f_mntonname));
520         copyinstr(path, mp->mnt_stat.f_mntonname,
521                   sizeof(mp->mnt_stat.f_mntonname) - 1,
522                   &size);
523
524         /*
525          * Initial statfs to prime mnt_stat.
526          */
527         hammer2_vfs_statfs(mp, &mp->mnt_stat, cred);
528
529         return 0;
530 }
531
532 static
533 int
534 hammer2_remount(struct mount *mp, char *path, struct vnode *devvp,
535                 struct ucred *cred)
536 {
537         return (0);
538 }
539
540 static
541 int
542 hammer2_vfs_unmount(struct mount *mp, int mntflags)
543 {
544         hammer2_pfsmount_t *pmp;
545         hammer2_mount_t *hmp;
546         int flags;
547         int error = 0;
548         int ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
549         int dumpcnt;
550         struct vnode *devvp;
551
552         pmp = MPTOPMP(mp);
553         hmp = pmp->hmp;
554         flags = 0;
555
556         if (mntflags & MNT_FORCE)
557                 flags |= FORCECLOSE;
558
559         hammer2_mount_exlock(hmp);
560
561         /*
562          * If mount initialization proceeded far enough we must flush
563          * its vnodes.
564          */
565         if (pmp->iroot)
566                 error = vflush(mp, 0, flags);
567
568         if (error)
569                 return error;
570
571         lockmgr(&hammer2_mntlk, LK_EXCLUSIVE);
572         --hmp->pmp_count;
573         kprintf("hammer2_unmount hmp=%p pmpcnt=%d\n", hmp, hmp->pmp_count);
574
575         /*
576          * Flush any left over chains.  The voldata lock is only used
577          * to synchronize against HAMMER2_CHAIN_MODIFIED_AUX.
578          */
579         hammer2_voldata_lock(hmp);
580         if (hmp->vchain.flags & (HAMMER2_CHAIN_MODIFIED |
581                                  HAMMER2_CHAIN_SUBMODIFIED)) {
582                 hammer2_voldata_unlock(hmp, 0);
583                 hammer2_vfs_sync(mp, MNT_WAIT);
584         } else {
585                 hammer2_voldata_unlock(hmp, 0);
586         }
587         if (hmp->pmp_count == 0) {
588                 if (hmp->vchain.flags & (HAMMER2_CHAIN_MODIFIED |
589                                          HAMMER2_CHAIN_SUBMODIFIED)) {
590                         kprintf("hammer2_unmount: chains left over after "
591                                 "final sync\n");
592                         if (hammer2_debug & 0x0010)
593                                 Debugger("entered debugger");
594                 }
595         }
596
597         /*
598          * Cleanup the root and super-root chain elements (which should be
599          * clean).
600          */
601         if (pmp->iroot) {
602                 hammer2_inode_lock_ex(pmp->iroot);
603                 hammer2_inode_put(pmp->iroot);
604                 /* lock destroyed by the put */
605 #if REPORT_REFS_ERRORS
606                 if (pmp->iroot->refs != 1)
607                         kprintf("PMP->IROOT %p REFS WRONG %d\n",
608                                 pmp->iroot, pmp->iroot->refs);
609 #else
610                 KKASSERT(pmp->iroot->refs == 1);
611 #endif
612                 hammer2_inode_drop(pmp->iroot);     /* ref for pmp->iroot */
613                 pmp->iroot = NULL;
614         }
615         if (pmp->rchain) {
616                 atomic_clear_int(&pmp->rchain->flags, HAMMER2_CHAIN_MOUNTED);
617 #if REPORT_REFS_ERRORS
618                 if (pmp->rchain->refs != 1)
619                         kprintf("PMP->RCHAIN %p REFS WRONG %d\n",
620                                 pmp->rchain, pmp->rchain->refs);
621 #else
622                 KKASSERT(pmp->rchain->refs == 1);
623 #endif
624                 hammer2_chain_drop(pmp->rchain);
625                 pmp->rchain = NULL;
626         }
627         ccms_domain_uninit(&pmp->ccms_dom);
628
629         /*
630          * Kill cluster controller
631          */
632         kdmsg_iocom_uninit(&pmp->iocom);
633
634         /*
635          * If no PFS's left drop the master hammer2_mount for the device.
636          */
637         if (hmp->pmp_count == 0) {
638                 if (hmp->sroot) {
639                         hammer2_inode_drop(hmp->sroot);
640                         hmp->sroot = NULL;
641                 }
642                 if (hmp->schain) {
643 #if REPORT_REFS_ERRORS
644                         if (hmp->schain->refs != 1)
645                                 kprintf("HMP->SCHAIN %p REFS WRONG %d\n",
646                                         hmp->schain, hmp->schain->refs);
647 #else
648                         KKASSERT(hmp->schain->refs == 1);
649 #endif
650                         hammer2_chain_drop(hmp->schain);
651                         hmp->schain = NULL;
652                 }
653
654                 /*
655                  * Finish up with the device vnode
656                  */
657                 if ((devvp = hmp->devvp) != NULL) {
658                         vinvalbuf(devvp, (ronly ? 0 : V_SAVE), 0, 0);
659                         hmp->devvp = NULL;
660                         VOP_CLOSE(devvp, (ronly ? FREAD : FREAD|FWRITE));
661                         vrele(devvp);
662                         devvp = NULL;
663                 }
664         }
665         hammer2_mount_unlock(hmp);
666
667         dumpcnt = 200;
668         hammer2_dump_chain(&hmp->vchain, 0, &dumpcnt);
669
670         /*
671          * Final drop of embedded volume root chain to clean up
672          * vchain.core (vchain structure is not flagged ALLOCATED
673          * so it is cleaned out and then left).
674          */
675         hammer2_chain_drop(&hmp->vchain);
676
677         pmp->mp = NULL;
678         pmp->hmp = NULL;
679         mp->mnt_data = NULL;
680
681         kmalloc_destroy(&pmp->mmsg);
682
683         kfree(pmp, M_HAMMER2);
684         if (hmp->pmp_count == 0) {
685                 TAILQ_REMOVE(&hammer2_mntlist, hmp, mntentry);
686                 kmalloc_destroy(&hmp->minode);
687                 kmalloc_destroy(&hmp->mchain);
688                 kfree(hmp, M_HAMMER2);
689         }
690         lockmgr(&hammer2_mntlk, LK_RELEASE);
691         return (error);
692 }
693
694 static
695 int
696 hammer2_vfs_vget(struct mount *mp, struct vnode *dvp,
697              ino_t ino, struct vnode **vpp)
698 {
699         kprintf("hammer2_vget\n");
700         return (EOPNOTSUPP);
701 }
702
703 static
704 int
705 hammer2_vfs_root(struct mount *mp, struct vnode **vpp)
706 {
707         hammer2_pfsmount_t *pmp;
708         hammer2_mount_t *hmp;
709         int error;
710         struct vnode *vp;
711
712         pmp = MPTOPMP(mp);
713         hmp = pmp->hmp;
714         hammer2_mount_exlock(hmp);
715         if (pmp->iroot == NULL) {
716                 *vpp = NULL;
717                 error = EINVAL;
718         } else {
719                 hammer2_inode_lock_sh(pmp->iroot);
720                 vp = hammer2_igetv(pmp->iroot, &error);
721                 hammer2_inode_unlock_sh(pmp->iroot);
722                 *vpp = vp;
723                 if (vp == NULL)
724                         kprintf("vnodefail\n");
725         }
726         hammer2_mount_unlock(hmp);
727
728         return (error);
729 }
730
731 /*
732  * Filesystem status
733  *
734  * XXX incorporate ipdata->inode_quota and data_quota
735  */
736 static
737 int
738 hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
739 {
740         hammer2_pfsmount_t *pmp;
741         hammer2_mount_t *hmp;
742
743         pmp = MPTOPMP(mp);
744         hmp = MPTOHMP(mp);
745
746         mp->mnt_stat.f_files = pmp->inode_count;
747         mp->mnt_stat.f_ffree = 0;
748         mp->mnt_stat.f_blocks = hmp->voldata.allocator_size / HAMMER2_PBUFSIZE;
749         mp->mnt_stat.f_bfree = (hmp->voldata.allocator_size -
750                                 hmp->voldata.allocator_beg) / HAMMER2_PBUFSIZE;
751         mp->mnt_stat.f_bavail = mp->mnt_stat.f_bfree;
752
753         *sbp = mp->mnt_stat;
754         return (0);
755 }
756
757 static
758 int
759 hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred)
760 {
761         hammer2_pfsmount_t *pmp;
762         hammer2_mount_t *hmp;
763
764         pmp = MPTOPMP(mp);
765         hmp = MPTOHMP(mp);
766
767         mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE;
768         mp->mnt_vstat.f_files = pmp->inode_count;
769         mp->mnt_vstat.f_ffree = 0;
770         mp->mnt_vstat.f_blocks = hmp->voldata.allocator_size / HAMMER2_PBUFSIZE;
771         mp->mnt_vstat.f_bfree = (hmp->voldata.allocator_size -
772                                  hmp->voldata.allocator_beg) / HAMMER2_PBUFSIZE;
773         mp->mnt_vstat.f_bavail = mp->mnt_vstat.f_bfree;
774
775         *sbp = mp->mnt_vstat;
776         return (0);
777 }
778
779 /*
780  * Sync the entire filesystem; this is called from the filesystem syncer
781  * process periodically and whenever a user calls sync(1) on the hammer
782  * mountpoint.
783  *
784  * Currently is actually called from the syncer! \o/
785  *
786  * This task will have to snapshot the state of the dirty inode chain.
787  * From that, it will have to make sure all of the inodes on the dirty
788  * chain have IO initiated. We make sure that io is initiated for the root
789  * block.
790  *
791  * If waitfor is set, we wait for media to acknowledge the new rootblock.
792  *
793  * THINKS: side A vs side B, to have sync not stall all I/O?
794  */
795 static
796 int
797 hammer2_vfs_sync(struct mount *mp, int waitfor)
798 {
799         struct hammer2_sync_info info;
800         hammer2_mount_t *hmp;
801         int flags;
802         int error;
803         int i;
804 #if 0
805         int dumpcnt;
806 #endif
807
808         hmp = MPTOHMP(mp);
809 #if 0
810         if ((waitfor & MNT_LAZY) == 0) {
811                 dumpcnt = 50;
812                 hammer2_dump_chain(&hmp->vchain, 0, &dumpcnt);
813         }
814 #endif
815
816         flags = VMSC_GETVP;
817         if (waitfor & MNT_LAZY)
818                 flags |= VMSC_ONEPASS;
819
820         hammer2_trans_init_flush(hmp, &info.trans, 1);
821         info.error = 0;
822         info.waitfor = MNT_NOWAIT;
823         vmntvnodescan(mp, flags | VMSC_NOWAIT,
824                       hammer2_sync_scan1,
825                       hammer2_sync_scan2, &info);
826         if (info.error == 0 && (waitfor & MNT_WAIT)) {
827                 info.waitfor = waitfor;
828                     vmntvnodescan(mp, flags,
829                                   hammer2_sync_scan1,
830                                   hammer2_sync_scan2, &info);
831
832         }
833 #if 0
834         if (waitfor == MNT_WAIT) {
835                 /* XXX */
836         } else {
837                 /* XXX */
838         }
839 #endif
840         hammer2_chain_lock(&hmp->vchain, HAMMER2_RESOLVE_ALWAYS);
841         if (hmp->vchain.flags & (HAMMER2_CHAIN_MODIFIED |
842                                  HAMMER2_CHAIN_SUBMODIFIED)) {
843                 hammer2_chain_flush(&info.trans, &hmp->vchain);
844         }
845         hammer2_chain_unlock(&hmp->vchain);
846
847         error = 0;
848
849         /*
850          * We can't safely flush the volume header until we have
851          * flushed any device buffers which have built up.
852          */
853 #if 0
854         if ((waitfor & MNT_LAZY) == 0) {
855                 waitfor = MNT_NOWAIT;
856                 vn_lock(hmp->devvp, LK_EXCLUSIVE | LK_RETRY);
857                 error = VOP_FSYNC(hmp->devvp, waitfor, 0);
858                 vn_unlock(hmp->devvp);
859         }
860 #endif
861         vn_lock(hmp->devvp, LK_EXCLUSIVE | LK_RETRY);
862         error = VOP_FSYNC(hmp->devvp, MNT_WAIT, 0);
863         vn_unlock(hmp->devvp);
864
865         /*
866          * The flush code sets CHAIN_VOLUMESYNC to indicate that the
867          * volume header needs synchronization via hmp->volsync.
868          *
869          * XXX synchronize the flag & data with only this flush XXX
870          */
871         if (error == 0 && (hmp->vchain.flags & HAMMER2_CHAIN_VOLUMESYNC)) {
872                 struct buf *bp;
873
874                 /*
875                  * Synchronize the disk before flushing the volume
876                  * header.
877                  */
878                 bp = getpbuf(NULL);
879                 bp->b_bio1.bio_offset = 0;
880                 bp->b_bufsize = 0;
881                 bp->b_bcount = 0;
882                 bp->b_cmd = BUF_CMD_FLUSH;
883                 bp->b_bio1.bio_done = biodone_sync;
884                 bp->b_bio1.bio_flags |= BIO_SYNC;
885                 vn_strategy(hmp->devvp, &bp->b_bio1);
886                 biowait(&bp->b_bio1, "h2vol");
887                 relpbuf(bp, NULL);
888
889                 /*
890                  * Then we can safely flush the version of the volume header
891                  * synchronized by the flush code.
892                  */
893                 i = hmp->volhdrno + 1;
894                 if (i >= HAMMER2_NUM_VOLHDRS)
895                         i = 0;
896                 if (i * HAMMER2_ZONE_BYTES64 + HAMMER2_SEGSIZE >
897                     hmp->volsync.volu_size) {
898                         i = 0;
899                 }
900                 kprintf("sync volhdr %d %jd\n",
901                         i, (intmax_t)hmp->volsync.volu_size);
902                 bp = getblk(hmp->devvp, i * HAMMER2_ZONE_BYTES64,
903                             HAMMER2_PBUFSIZE, 0, 0);
904                 atomic_clear_int(&hmp->vchain.flags, HAMMER2_CHAIN_VOLUMESYNC);
905                 bcopy(&hmp->volsync, bp->b_data, HAMMER2_PBUFSIZE);
906                 bawrite(bp);
907                 hmp->volhdrno = i;
908         }
909         hammer2_trans_done_flush(&info.trans, 1);
910         return (error);
911 }
912
913 /*
914  * Sync passes.
915  *
916  * NOTE: We don't test SUBMODIFIED or MOVED here because the fsync code
917  *       won't flush on those flags.  The syncer code above will do a
918  *       general meta-data flush globally that will catch these flags.
919  */
920 static int
921 hammer2_sync_scan1(struct mount *mp, struct vnode *vp, void *data)
922 {
923         hammer2_inode_t *ip;
924
925         ip = VTOI(vp);
926         if (vp->v_type == VNON || ip == NULL ||
927             ((ip->flags & (HAMMER2_INODE_MODIFIED |
928                            HAMMER2_INODE_DIRTYEMBED)) == 0 &&
929              RB_EMPTY(&vp->v_rbdirty_tree))) {
930                 return(-1);
931         }
932         return(0);
933 }
934
935 static int
936 hammer2_sync_scan2(struct mount *mp, struct vnode *vp, void *data)
937 {
938         struct hammer2_sync_info *info = data;
939         hammer2_inode_t *ip;
940         int error;
941
942         ip = VTOI(vp);
943         if (vp->v_type == VNON || vp->v_type == VBAD ||
944             ((ip->flags & (HAMMER2_INODE_MODIFIED |
945                            HAMMER2_INODE_DIRTYEMBED)) == 0 &&
946              RB_EMPTY(&vp->v_rbdirty_tree))) {
947                 return(0);
948         }
949         error = VOP_FSYNC(vp, MNT_NOWAIT, 0);
950         if (error)
951                 info->error = error;
952         return(0);
953 }
954
955 static
956 int
957 hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp)
958 {
959         return (0);
960 }
961
962 static
963 int
964 hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
965                struct fid *fhp, struct vnode **vpp)
966 {
967         return (0);
968 }
969
970 static
971 int
972 hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
973                  int *exflagsp, struct ucred **credanonp)
974 {
975         return (0);
976 }
977
978 /*
979  * Support code for hammer2_mount().  Read, verify, and install the volume
980  * header into the HMP
981  *
982  * XXX read four volhdrs and use the one with the highest TID whos CRC
983  *     matches.
984  *
985  * XXX check iCRCs.
986  *
987  * XXX For filesystems w/ less than 4 volhdrs, make sure to not write to
988  *     nonexistant locations.
989  *
990  * XXX Record selected volhdr and ring updates to each of 4 volhdrs
991  */
992 static
993 int
994 hammer2_install_volume_header(hammer2_mount_t *hmp)
995 {
996         hammer2_volume_data_t *vd;
997         struct buf *bp;
998         hammer2_crc32_t crc0, crc, bcrc0, bcrc;
999         int error_reported;
1000         int error;
1001         int valid;
1002         int i;
1003
1004         error_reported = 0;
1005         error = 0;
1006         valid = 0;
1007         bp = NULL;
1008
1009         /*
1010          * There are up to 4 copies of the volume header (syncs iterate
1011          * between them so there is no single master).  We don't trust the
1012          * volu_size field so we don't know precisely how large the filesystem
1013          * is, so depend on the OS to return an error if we go beyond the
1014          * block device's EOF.
1015          */
1016         for (i = 0; i < HAMMER2_NUM_VOLHDRS; i++) {
1017                 error = bread(hmp->devvp, i * HAMMER2_ZONE_BYTES64,
1018                               HAMMER2_VOLUME_BYTES, &bp);
1019                 if (error) {
1020                         brelse(bp);
1021                         bp = NULL;
1022                         continue;
1023                 }
1024
1025                 vd = (struct hammer2_volume_data *) bp->b_data;
1026                 if ((vd->magic != HAMMER2_VOLUME_ID_HBO) &&
1027                     (vd->magic != HAMMER2_VOLUME_ID_ABO)) {
1028                         brelse(bp);
1029                         bp = NULL;
1030                         continue;
1031                 }
1032
1033                 if (vd->magic == HAMMER2_VOLUME_ID_ABO) {
1034                         /* XXX: Reversed-endianness filesystem */
1035                         kprintf("hammer2: reverse-endian filesystem detected");
1036                         brelse(bp);
1037                         bp = NULL;
1038                         continue;
1039                 }
1040
1041                 crc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT0];
1042                 crc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC0_OFF,
1043                                       HAMMER2_VOLUME_ICRC0_SIZE);
1044                 bcrc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT1];
1045                 bcrc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC1_OFF,
1046                                        HAMMER2_VOLUME_ICRC1_SIZE);
1047                 if ((crc0 != crc) || (bcrc0 != bcrc)) {
1048                         kprintf("hammer2 volume header crc "
1049                                 "mismatch copy #%d %08x/%08x\n",
1050                                 i, crc0, crc);
1051                         error_reported = 1;
1052                         brelse(bp);
1053                         bp = NULL;
1054                         continue;
1055                 }
1056                 if (valid == 0 || hmp->voldata.mirror_tid < vd->mirror_tid) {
1057                         valid = 1;
1058                         hmp->voldata = *vd;
1059                         hmp->volhdrno = i;
1060                 }
1061                 brelse(bp);
1062                 bp = NULL;
1063         }
1064         if (valid) {
1065                 hmp->volsync = hmp->voldata;
1066                 error = 0;
1067                 if (error_reported || bootverbose || 1) { /* 1/DEBUG */
1068                         kprintf("hammer2: using volume header #%d\n",
1069                                 hmp->volhdrno);
1070                 }
1071         } else {
1072                 error = EINVAL;
1073                 kprintf("hammer2: no valid volume headers found!\n");
1074         }
1075         return (error);
1076 }
1077
1078 /*
1079  * Reconnect using the passed file pointer.  The caller must ref the
1080  * fp for us.
1081  */
1082 void
1083 hammer2_cluster_reconnect(hammer2_pfsmount_t *pmp, struct file *fp)
1084 {
1085         hammer2_inode_data_t *ipdata;
1086         size_t name_len;
1087
1088         /*
1089          * Closes old comm descriptor, kills threads, cleans up
1090          * states, then installs the new descriptor and creates
1091          * new threads.
1092          */
1093         kdmsg_iocom_reconnect(&pmp->iocom, fp, "hammer2");
1094
1095         /*
1096          * Setup LNK_CONN fields for autoinitiated state machine
1097          */
1098         hammer2_inode_lock_ex(pmp->iroot);
1099         ipdata = &pmp->iroot->chain->data->ipdata;
1100         pmp->iocom.auto_lnk_conn.pfs_clid = ipdata->pfs_clid;
1101         pmp->iocom.auto_lnk_conn.pfs_fsid = ipdata->pfs_fsid;
1102         pmp->iocom.auto_lnk_conn.pfs_type = ipdata->pfs_type;
1103         pmp->iocom.auto_lnk_conn.proto_version = DMSG_SPAN_PROTO_1;
1104         pmp->iocom.auto_lnk_conn.peer_type = pmp->hmp->voldata.peer_type;
1105         hammer2_inode_unlock_ex(pmp->iroot);
1106
1107         /*
1108          * Filter adjustment.  Clients do not need visibility into other
1109          * clients (otherwise millions of clients would present a serious
1110          * problem).  The fs_label also serves to restrict the namespace.
1111          */
1112         pmp->iocom.auto_lnk_conn.peer_mask = 1LLU << HAMMER2_PEER_HAMMER2;
1113         pmp->iocom.auto_lnk_conn.pfs_mask = (uint64_t)-1;
1114         switch (ipdata->pfs_type) {
1115         case DMSG_PFSTYPE_CLIENT:
1116                 pmp->iocom.auto_lnk_conn.peer_mask &=
1117                                 ~(1LLU << DMSG_PFSTYPE_CLIENT);
1118                 break;
1119         default:
1120                 break;
1121         }
1122
1123         name_len = ipdata->name_len;
1124         if (name_len >= sizeof(pmp->iocom.auto_lnk_conn.fs_label))
1125                 name_len = sizeof(pmp->iocom.auto_lnk_conn.fs_label) - 1;
1126         bcopy(ipdata->filename,
1127               pmp->iocom.auto_lnk_conn.fs_label,
1128               name_len);
1129         pmp->iocom.auto_lnk_conn.fs_label[name_len] = 0;
1130
1131         /*
1132          * Setup LNK_SPAN fields for autoinitiated state machine
1133          */
1134         pmp->iocom.auto_lnk_span.pfs_clid = ipdata->pfs_clid;
1135         pmp->iocom.auto_lnk_span.pfs_fsid = ipdata->pfs_fsid;
1136         pmp->iocom.auto_lnk_span.pfs_type = ipdata->pfs_type;
1137         pmp->iocom.auto_lnk_span.peer_type = pmp->hmp->voldata.peer_type;
1138         pmp->iocom.auto_lnk_span.proto_version = DMSG_SPAN_PROTO_1;
1139         name_len = ipdata->name_len;
1140         if (name_len >= sizeof(pmp->iocom.auto_lnk_span.fs_label))
1141                 name_len = sizeof(pmp->iocom.auto_lnk_span.fs_label) - 1;
1142         bcopy(ipdata->filename,
1143               pmp->iocom.auto_lnk_span.fs_label,
1144               name_len);
1145         pmp->iocom.auto_lnk_span.fs_label[name_len] = 0;
1146
1147         kdmsg_iocom_autoinitiate(&pmp->iocom, hammer2_autodmsg);
1148 }
1149
1150 static int
1151 hammer2_rcvdmsg(kdmsg_msg_t *msg)
1152 {
1153         switch(msg->any.head.cmd & DMSGF_TRANSMASK) {
1154         case DMSG_DBG_SHELL:
1155                 /*
1156                  * (non-transaction)
1157                  * Execute shell command (not supported atm)
1158                  */
1159                 kdmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
1160                 break;
1161         case DMSG_DBG_SHELL | DMSGF_REPLY:
1162                 /*
1163                  * (non-transaction)
1164                  */
1165                 if (msg->aux_data) {
1166                         msg->aux_data[msg->aux_size - 1] = 0;
1167                         kprintf("HAMMER2 DBG: %s\n", msg->aux_data);
1168                 }
1169                 break;
1170         default:
1171                 /*
1172                  * Unsupported message received.  We only need to
1173                  * reply if it's a transaction in order to close our end.
1174                  * Ignore any one-way messages are any further messages
1175                  * associated with the transaction.
1176                  *
1177                  * NOTE: This case also includes DMSG_LNK_ERROR messages
1178                  *       which might be one-way, replying to those would
1179                  *       cause an infinite ping-pong.
1180                  */
1181                 if (msg->any.head.cmd & DMSGF_CREATE)
1182                         kdmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
1183                 break;
1184         }
1185         return(0);
1186 }
1187
1188 /*
1189  * This function is called after KDMSG has automatically handled processing
1190  * of a LNK layer message (typically CONN, SPAN, or CIRC).
1191  *
1192  * We tag off the LNK_CONN to trigger our LNK_VOLCONF messages which
1193  * advertises all available hammer2 super-root volumes.
1194  */
1195 static void
1196 hammer2_autodmsg(kdmsg_msg_t *msg)
1197 {
1198         hammer2_pfsmount_t *pmp = msg->iocom->handle;
1199         hammer2_mount_t *hmp = pmp->hmp;
1200         int copyid;
1201
1202         /*
1203          * We only care about replies to our LNK_CONN auto-request.  kdmsg
1204          * has already processed the reply, we use this calback as a shim
1205          * to know when we can advertise available super-root volumes.
1206          */
1207         if ((msg->any.head.cmd & DMSGF_TRANSMASK) !=
1208             (DMSG_LNK_CONN | DMSGF_CREATE | DMSGF_REPLY) ||
1209             msg->state == NULL) {
1210                 return;
1211         }
1212
1213         kprintf("LNK_CONN REPLY RECEIVED CMD %08x\n", msg->any.head.cmd);
1214
1215         if (msg->any.head.cmd & DMSGF_CREATE) {
1216                 kprintf("HAMMER2: VOLDATA DUMP\n");
1217
1218                 /*
1219                  * Dump the configuration stored in the volume header
1220                  */
1221                 hammer2_voldata_lock(hmp);
1222                 for (copyid = 0; copyid < HAMMER2_COPYID_COUNT; ++copyid) {
1223                         if (hmp->voldata.copyinfo[copyid].copyid == 0)
1224                                 continue;
1225                         hammer2_volconf_update(pmp, copyid);
1226                 }
1227                 hammer2_voldata_unlock(hmp, 0);
1228         }
1229         if ((msg->any.head.cmd & DMSGF_DELETE) &&
1230             msg->state && (msg->state->txcmd & DMSGF_DELETE) == 0) {
1231                 kprintf("HAMMER2: CONN WAS TERMINATED\n");
1232         }
1233 }
1234
1235 /*
1236  * Volume configuration updates are passed onto the userland service
1237  * daemon via the open LNK_CONN transaction.
1238  */
1239 void
1240 hammer2_volconf_update(hammer2_pfsmount_t *pmp, int index)
1241 {
1242         hammer2_mount_t *hmp = pmp->hmp;
1243         kdmsg_msg_t *msg;
1244
1245         /* XXX interlock against connection state termination */
1246         kprintf("volconf update %p\n", pmp->iocom.conn_state);
1247         if (pmp->iocom.conn_state) {
1248                 kprintf("TRANSMIT VOLCONF VIA OPEN CONN TRANSACTION\n");
1249                 msg = kdmsg_msg_alloc_state(pmp->iocom.conn_state,
1250                                             DMSG_LNK_VOLCONF, NULL, NULL);
1251                 msg->any.lnk_volconf.copy = hmp->voldata.copyinfo[index];
1252                 msg->any.lnk_volconf.mediaid = hmp->voldata.fsid;
1253                 msg->any.lnk_volconf.index = index;
1254                 kdmsg_msg_write(msg);
1255         }
1256 }
1257
1258 void
1259 hammer2_dump_chain(hammer2_chain_t *chain, int tab, int *countp)
1260 {
1261         hammer2_chain_t *scan;
1262
1263         --*countp;
1264         if (*countp == 0) {
1265                 kprintf("%*.*s...\n", tab, tab, "");
1266                 return;
1267         }
1268         if (*countp < 0)
1269                 return;
1270         kprintf("%*.*schain[%d] %p.%d [%08x][core=%p] (%s) dl=%p refs=%d",
1271                 tab, tab, "",
1272                 chain->index, chain, chain->bref.type, chain->flags,
1273                 chain->core,
1274                 ((chain->bref.type == HAMMER2_BREF_TYPE_INODE &&
1275                 chain->data) ?  (char *)chain->data->ipdata.filename : "?"),
1276                 chain->duplink, chain->refs);
1277         if (chain->core == NULL || RB_EMPTY(&chain->core->rbtree))
1278                 kprintf("\n");
1279         else
1280                 kprintf(" {\n");
1281         RB_FOREACH(scan, hammer2_chain_tree, &chain->core->rbtree) {
1282                 hammer2_dump_chain(scan, tab + 4, countp);
1283         }
1284         if (chain->core && !RB_EMPTY(&chain->core->rbtree)) {
1285                 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE && chain->data)
1286                         kprintf("%*.*s}(%s)\n", tab, tab, "",
1287                                 chain->data->ipdata.filename);
1288                 else
1289                         kprintf("%*.*s}\n", tab, tab, "");
1290         }
1291 }