hammer2 - flush sequencing part 5 - more flush synchronization work
[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                 TAILQ_INIT(&hmp->transq);
392
393                 /*
394                  * vchain setup. vchain.data is special cased to NULL.
395                  * vchain.refs is initialized and will never drop to 0.
396                  */
397                 hmp->vchain.hmp = hmp;
398                 hmp->vchain.refs = 1;
399                 hmp->vchain.data = (void *)&hmp->voldata;
400                 hmp->vchain.bref.type = HAMMER2_BREF_TYPE_VOLUME;
401                 hmp->vchain.bref.data_off = 0 | HAMMER2_PBUFRADIX;
402                 hmp->vchain.delete_tid = HAMMER2_MAX_TID;
403                 hammer2_chain_core_alloc(&hmp->vchain, NULL);
404                 /* hmp->vchain.u.xxx is left NULL */
405
406                 /*
407                  * Install the volume header
408                  */
409                 error = hammer2_install_volume_header(hmp);
410                 if (error) {
411                         hammer2_vfs_unmount(mp, MNT_FORCE);
412                         return error;
413                 }
414         }
415
416         /*
417          * required mount structure initializations
418          */
419         mp->mnt_stat.f_iosize = HAMMER2_PBUFSIZE;
420         mp->mnt_stat.f_bsize = HAMMER2_PBUFSIZE;
421
422         mp->mnt_vstat.f_frsize = HAMMER2_PBUFSIZE;
423         mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE;
424
425         /*
426          * Optional fields
427          */
428         mp->mnt_iosize_max = MAXPHYS;
429
430         /*
431          * First locate the super-root inode, which is key 0 relative to the
432          * volume header's blockset.
433          *
434          * Then locate the root inode by scanning the directory keyspace
435          * represented by the label.
436          */
437         if (create_hmp) {
438                 parent = hammer2_chain_lookup_init(&hmp->vchain, 0);
439                 schain = hammer2_chain_lookup(&parent,
440                                       HAMMER2_SROOT_KEY, HAMMER2_SROOT_KEY, 0);
441                 hammer2_chain_lookup_done(parent);
442                 if (schain == NULL) {
443                         kprintf("hammer2_mount: invalid super-root\n");
444                         hammer2_vfs_unmount(mp, MNT_FORCE);
445                         return EINVAL;
446                 }
447                 hammer2_chain_ref(schain);      /* for hmp->schain */
448                 hmp->schain = schain;           /* left locked for inode_get */
449                 hmp->sroot = hammer2_inode_get(hmp, NULL, NULL, schain);
450                 hammer2_inode_ref(hmp->sroot);       /* for hmp->sroot */
451                 hammer2_inode_unlock_ex(hmp->sroot, schain);
452         } else {
453                 schain = hmp->schain;
454         }
455
456         /*
457          * schain only has 1 ref now for its hmp->schain assignment.
458          * Setup for lookup (which will lock it).
459          */
460         parent = hammer2_chain_lookup_init(schain, 0);
461         lhc = hammer2_dirhash(label, strlen(label));
462         rchain = hammer2_chain_lookup(&parent,
463                                       lhc, lhc + HAMMER2_DIRHASH_LOMASK,
464                                       0);
465         while (rchain) {
466                 if (rchain->bref.type == HAMMER2_BREF_TYPE_INODE &&
467                     strcmp(label, rchain->data->ipdata.filename) == 0) {
468                         break;
469                 }
470                 rchain = hammer2_chain_next(&parent, rchain,
471                                             lhc, lhc + HAMMER2_DIRHASH_LOMASK,
472                                             0);
473         }
474         hammer2_chain_lookup_done(parent);
475         if (rchain == NULL) {
476                 kprintf("hammer2_mount: PFS label not found\n");
477                 hammer2_vfs_unmount(mp, MNT_FORCE);
478                 return EINVAL;
479         }
480         if (rchain->flags & HAMMER2_CHAIN_MOUNTED) {
481                 hammer2_chain_unlock(rchain);
482                 kprintf("hammer2_mount: PFS label already mounted!\n");
483                 hammer2_vfs_unmount(mp, MNT_FORCE);
484                 return EBUSY;
485         }
486         if (rchain->flags & HAMMER2_CHAIN_RECYCLE) {
487                 kprintf("hammer2_mount: PFS label currently recycling\n");
488                 hammer2_vfs_unmount(mp, MNT_FORCE);
489                 return EBUSY;
490         }
491
492         atomic_set_int(&rchain->flags, HAMMER2_CHAIN_MOUNTED);
493
494         /*
495          * NOTE: *_get() integrates chain's lock into the inode lock.
496          */
497         hammer2_chain_ref(rchain);              /* for pmp->rchain */
498         pmp->rchain = rchain;                   /* left held & unlocked */
499         pmp->iroot = hammer2_inode_get(hmp, pmp, NULL, rchain);
500         hammer2_inode_ref(pmp->iroot);          /* ref for pmp->iroot */
501         hammer2_inode_unlock_ex(pmp->iroot, rchain);
502
503         kprintf("iroot %p\n", pmp->iroot);
504
505         /*
506          * Ref the cluster management messaging descriptor.  The mount
507          * program deals with the other end of the communications pipe.
508          */
509         fp = holdfp(curproc->p_fd, info.cluster_fd, -1);
510         if (fp == NULL) {
511                 kprintf("hammer2_mount: bad cluster_fd!\n");
512                 hammer2_vfs_unmount(mp, MNT_FORCE);
513                 return EBADF;
514         }
515         hammer2_cluster_reconnect(pmp, fp);
516
517         /*
518          * Finish setup
519          */
520         vfs_getnewfsid(mp);
521         vfs_add_vnodeops(mp, &hammer2_vnode_vops, &mp->mnt_vn_norm_ops);
522         vfs_add_vnodeops(mp, &hammer2_spec_vops, &mp->mnt_vn_spec_ops);
523         vfs_add_vnodeops(mp, &hammer2_fifo_vops, &mp->mnt_vn_fifo_ops);
524
525         copyinstr(info.volume, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size);
526         bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
527         bzero(mp->mnt_stat.f_mntonname, sizeof(mp->mnt_stat.f_mntonname));
528         copyinstr(path, mp->mnt_stat.f_mntonname,
529                   sizeof(mp->mnt_stat.f_mntonname) - 1,
530                   &size);
531
532         /*
533          * Initial statfs to prime mnt_stat.
534          */
535         hammer2_vfs_statfs(mp, &mp->mnt_stat, cred);
536
537         return 0;
538 }
539
540 static
541 int
542 hammer2_remount(struct mount *mp, char *path, struct vnode *devvp,
543                 struct ucred *cred)
544 {
545         return (0);
546 }
547
548 static
549 int
550 hammer2_vfs_unmount(struct mount *mp, int mntflags)
551 {
552         hammer2_pfsmount_t *pmp;
553         hammer2_mount_t *hmp;
554         hammer2_chain_t *parent;
555         int flags;
556         int error = 0;
557         int ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
558         int dumpcnt;
559         struct vnode *devvp;
560
561         pmp = MPTOPMP(mp);
562         hmp = pmp->hmp;
563         flags = 0;
564
565         if (mntflags & MNT_FORCE)
566                 flags |= FORCECLOSE;
567
568         hammer2_mount_exlock(hmp);
569
570         /*
571          * If mount initialization proceeded far enough we must flush
572          * its vnodes.
573          */
574         if (pmp->iroot)
575                 error = vflush(mp, 0, flags);
576
577         if (error) {
578                 hammer2_mount_unlock(hmp);
579                 return error;
580         }
581
582         lockmgr(&hammer2_mntlk, LK_EXCLUSIVE);
583         --hmp->pmp_count;
584         kprintf("hammer2_unmount hmp=%p pmpcnt=%d\n", hmp, hmp->pmp_count);
585
586         /*
587          * Flush any left over chains.  The voldata lock is only used
588          * to synchronize against HAMMER2_CHAIN_MODIFIED_AUX.
589          */
590         hammer2_voldata_lock(hmp);
591         if (hmp->vchain.flags & (HAMMER2_CHAIN_MODIFIED |
592                                  HAMMER2_CHAIN_SUBMODIFIED)) {
593                 hammer2_voldata_unlock(hmp, 0);
594                 hammer2_vfs_sync(mp, MNT_WAIT);
595         } else {
596                 hammer2_voldata_unlock(hmp, 0);
597         }
598         if (hmp->pmp_count == 0) {
599                 if (hmp->vchain.flags & (HAMMER2_CHAIN_MODIFIED |
600                                          HAMMER2_CHAIN_SUBMODIFIED)) {
601                         kprintf("hammer2_unmount: chains left over after "
602                                 "final sync\n");
603                         if (hammer2_debug & 0x0010)
604                                 Debugger("entered debugger");
605                 }
606         }
607
608         /*
609          * Cleanup the root and super-root chain elements (which should be
610          * clean).
611          */
612         if (pmp->iroot) {
613                 parent = hammer2_inode_lock_ex(pmp->iroot);
614                 hammer2_inode_put(pmp->iroot, parent);
615                 /* lock destroyed by the put */
616 #if REPORT_REFS_ERRORS
617                 if (pmp->iroot->refs != 1)
618                         kprintf("PMP->IROOT %p REFS WRONG %d\n",
619                                 pmp->iroot, pmp->iroot->refs);
620 #else
621                 KKASSERT(pmp->iroot->refs == 1);
622 #endif
623                 hammer2_inode_drop(pmp->iroot);     /* ref for pmp->iroot */
624                 pmp->iroot = NULL;
625         }
626         if (pmp->rchain) {
627                 atomic_clear_int(&pmp->rchain->flags, HAMMER2_CHAIN_MOUNTED);
628 #if REPORT_REFS_ERRORS
629                 if (pmp->rchain->refs != 1)
630                         kprintf("PMP->RCHAIN %p REFS WRONG %d\n",
631                                 pmp->rchain, pmp->rchain->refs);
632 #else
633                 KKASSERT(pmp->rchain->refs == 1);
634 #endif
635                 hammer2_chain_drop(pmp->rchain);
636                 pmp->rchain = NULL;
637         }
638         ccms_domain_uninit(&pmp->ccms_dom);
639
640         /*
641          * Kill cluster controller
642          */
643         kdmsg_iocom_uninit(&pmp->iocom);
644
645         /*
646          * If no PFS's left drop the master hammer2_mount for the device.
647          */
648         if (hmp->pmp_count == 0) {
649                 if (hmp->sroot) {
650                         hammer2_inode_drop(hmp->sroot);
651                         hmp->sroot = NULL;
652                 }
653                 if (hmp->schain) {
654 #if REPORT_REFS_ERRORS
655                         if (hmp->schain->refs != 1)
656                                 kprintf("HMP->SCHAIN %p REFS WRONG %d\n",
657                                         hmp->schain, hmp->schain->refs);
658 #else
659                         KKASSERT(hmp->schain->refs == 1);
660 #endif
661                         hammer2_chain_drop(hmp->schain);
662                         hmp->schain = NULL;
663                 }
664
665                 /*
666                  * Finish up with the device vnode
667                  */
668                 if ((devvp = hmp->devvp) != NULL) {
669                         vinvalbuf(devvp, (ronly ? 0 : V_SAVE), 0, 0);
670                         hmp->devvp = NULL;
671                         VOP_CLOSE(devvp, (ronly ? FREAD : FREAD|FWRITE));
672                         vrele(devvp);
673                         devvp = NULL;
674                 }
675
676                 /*
677                  * Final drop of embedded volume root chain to clean up
678                  * vchain.core (vchain structure is not flagged ALLOCATED
679                  * so it is cleaned out and then left).
680                  */
681                 dumpcnt = 50;
682                 hammer2_dump_chain(&hmp->vchain, 0, &dumpcnt);
683                 hammer2_mount_unlock(hmp);
684                 hammer2_chain_drop(&hmp->vchain);
685         } else {
686                 hammer2_mount_unlock(hmp);
687         }
688
689         pmp->mp = NULL;
690         pmp->hmp = NULL;
691         mp->mnt_data = NULL;
692
693         kmalloc_destroy(&pmp->mmsg);
694
695         kfree(pmp, M_HAMMER2);
696         if (hmp->pmp_count == 0) {
697                 TAILQ_REMOVE(&hammer2_mntlist, hmp, mntentry);
698                 kmalloc_destroy(&hmp->minode);
699                 kmalloc_destroy(&hmp->mchain);
700                 kfree(hmp, M_HAMMER2);
701         }
702         lockmgr(&hammer2_mntlk, LK_RELEASE);
703         return (error);
704 }
705
706 static
707 int
708 hammer2_vfs_vget(struct mount *mp, struct vnode *dvp,
709              ino_t ino, struct vnode **vpp)
710 {
711         kprintf("hammer2_vget\n");
712         return (EOPNOTSUPP);
713 }
714
715 static
716 int
717 hammer2_vfs_root(struct mount *mp, struct vnode **vpp)
718 {
719         hammer2_pfsmount_t *pmp;
720         hammer2_mount_t *hmp;
721         hammer2_chain_t *parent;
722         int error;
723         struct vnode *vp;
724
725         pmp = MPTOPMP(mp);
726         hmp = pmp->hmp;
727         hammer2_mount_exlock(hmp);
728         if (pmp->iroot == NULL) {
729                 *vpp = NULL;
730                 error = EINVAL;
731         } else {
732                 parent = hammer2_inode_lock_sh(pmp->iroot);
733                 vp = hammer2_igetv(pmp->iroot, &error);
734                 hammer2_inode_unlock_sh(pmp->iroot, parent);
735                 *vpp = vp;
736                 if (vp == NULL)
737                         kprintf("vnodefail\n");
738         }
739         hammer2_mount_unlock(hmp);
740
741         return (error);
742 }
743
744 /*
745  * Filesystem status
746  *
747  * XXX incorporate ipdata->inode_quota and data_quota
748  */
749 static
750 int
751 hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
752 {
753         hammer2_pfsmount_t *pmp;
754         hammer2_mount_t *hmp;
755
756         pmp = MPTOPMP(mp);
757         hmp = MPTOHMP(mp);
758
759         mp->mnt_stat.f_files = pmp->inode_count;
760         mp->mnt_stat.f_ffree = 0;
761         mp->mnt_stat.f_blocks = hmp->voldata.allocator_size / HAMMER2_PBUFSIZE;
762         mp->mnt_stat.f_bfree = (hmp->voldata.allocator_size -
763                                 hmp->voldata.allocator_beg) / HAMMER2_PBUFSIZE;
764         mp->mnt_stat.f_bavail = mp->mnt_stat.f_bfree;
765
766         *sbp = mp->mnt_stat;
767         return (0);
768 }
769
770 static
771 int
772 hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred)
773 {
774         hammer2_pfsmount_t *pmp;
775         hammer2_mount_t *hmp;
776
777         pmp = MPTOPMP(mp);
778         hmp = MPTOHMP(mp);
779
780         mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE;
781         mp->mnt_vstat.f_files = pmp->inode_count;
782         mp->mnt_vstat.f_ffree = 0;
783         mp->mnt_vstat.f_blocks = hmp->voldata.allocator_size / HAMMER2_PBUFSIZE;
784         mp->mnt_vstat.f_bfree = (hmp->voldata.allocator_size -
785                                  hmp->voldata.allocator_beg) / HAMMER2_PBUFSIZE;
786         mp->mnt_vstat.f_bavail = mp->mnt_vstat.f_bfree;
787
788         *sbp = mp->mnt_vstat;
789         return (0);
790 }
791
792 /*
793  * Sync the entire filesystem; this is called from the filesystem syncer
794  * process periodically and whenever a user calls sync(1) on the hammer
795  * mountpoint.
796  *
797  * Currently is actually called from the syncer! \o/
798  *
799  * This task will have to snapshot the state of the dirty inode chain.
800  * From that, it will have to make sure all of the inodes on the dirty
801  * chain have IO initiated. We make sure that io is initiated for the root
802  * block.
803  *
804  * If waitfor is set, we wait for media to acknowledge the new rootblock.
805  *
806  * THINKS: side A vs side B, to have sync not stall all I/O?
807  */
808 static
809 int
810 hammer2_vfs_sync(struct mount *mp, int waitfor)
811 {
812         struct hammer2_sync_info info;
813         hammer2_mount_t *hmp;
814         int flags;
815         int error;
816         int i;
817 #if 0
818         int dumpcnt;
819 #endif
820
821         hmp = MPTOHMP(mp);
822 #if 0
823         if ((waitfor & MNT_LAZY) == 0) {
824                 dumpcnt = 50;
825                 hammer2_dump_chain(&hmp->vchain, 0, &dumpcnt);
826         }
827 #endif
828
829         flags = VMSC_GETVP;
830         if (waitfor & MNT_LAZY)
831                 flags |= VMSC_ONEPASS;
832
833         hammer2_trans_init(hmp, &info.trans, HAMMER2_TRANS_ISFLUSH);
834
835         info.error = 0;
836         info.waitfor = MNT_NOWAIT;
837         vmntvnodescan(mp, flags | VMSC_NOWAIT,
838                       hammer2_sync_scan1,
839                       hammer2_sync_scan2, &info);
840         if (info.error == 0 && (waitfor & MNT_WAIT)) {
841                 info.waitfor = waitfor;
842                     vmntvnodescan(mp, flags,
843                                   hammer2_sync_scan1,
844                                   hammer2_sync_scan2, &info);
845
846         }
847 #if 0
848         if (waitfor == MNT_WAIT) {
849                 /* XXX */
850         } else {
851                 /* XXX */
852         }
853 #endif
854         /*
855          * Rollup flush.  The fsyncs above basically just flushed
856          * data blocks.  The flush below gets all the meta-data.
857          */
858         hammer2_chain_lock(&hmp->vchain, HAMMER2_RESOLVE_ALWAYS);
859         if (hmp->vchain.flags & (HAMMER2_CHAIN_MODIFIED |
860                                  HAMMER2_CHAIN_SUBMODIFIED)) {
861                 hammer2_chain_flush(&info.trans, &hmp->vchain);
862         }
863         hammer2_chain_unlock(&hmp->vchain);
864
865         error = 0;
866
867         /*
868          * We can't safely flush the volume header until we have
869          * flushed any device buffers which have built up.
870          *
871          * XXX this isn't being incremental
872          */
873         vn_lock(hmp->devvp, LK_EXCLUSIVE | LK_RETRY);
874         error = VOP_FSYNC(hmp->devvp, MNT_WAIT, 0);
875         vn_unlock(hmp->devvp);
876
877         /*
878          * The flush code sets CHAIN_VOLUMESYNC to indicate that the
879          * volume header needs synchronization via hmp->volsync.
880          *
881          * XXX synchronize the flag & data with only this flush XXX
882          */
883         if (error == 0 && (hmp->vchain.flags & HAMMER2_CHAIN_VOLUMESYNC)) {
884                 struct buf *bp;
885
886                 /*
887                  * Synchronize the disk before flushing the volume
888                  * header.
889                  */
890                 bp = getpbuf(NULL);
891                 bp->b_bio1.bio_offset = 0;
892                 bp->b_bufsize = 0;
893                 bp->b_bcount = 0;
894                 bp->b_cmd = BUF_CMD_FLUSH;
895                 bp->b_bio1.bio_done = biodone_sync;
896                 bp->b_bio1.bio_flags |= BIO_SYNC;
897                 vn_strategy(hmp->devvp, &bp->b_bio1);
898                 biowait(&bp->b_bio1, "h2vol");
899                 relpbuf(bp, NULL);
900
901                 /*
902                  * Then we can safely flush the version of the volume header
903                  * synchronized by the flush code.
904                  */
905                 i = hmp->volhdrno + 1;
906                 if (i >= HAMMER2_NUM_VOLHDRS)
907                         i = 0;
908                 if (i * HAMMER2_ZONE_BYTES64 + HAMMER2_SEGSIZE >
909                     hmp->volsync.volu_size) {
910                         i = 0;
911                 }
912                 kprintf("sync volhdr %d %jd\n",
913                         i, (intmax_t)hmp->volsync.volu_size);
914                 bp = getblk(hmp->devvp, i * HAMMER2_ZONE_BYTES64,
915                             HAMMER2_PBUFSIZE, 0, 0);
916                 atomic_clear_int(&hmp->vchain.flags, HAMMER2_CHAIN_VOLUMESYNC);
917                 bcopy(&hmp->volsync, bp->b_data, HAMMER2_PBUFSIZE);
918                 bawrite(bp);
919                 hmp->volhdrno = i;
920         }
921         hammer2_trans_done(&info.trans);
922         return (error);
923 }
924
925 /*
926  * Sync passes.
927  *
928  * NOTE: We don't test SUBMODIFIED or MOVED here because the fsync code
929  *       won't flush on those flags.  The syncer code above will do a
930  *       general meta-data flush globally that will catch these flags.
931  */
932 static int
933 hammer2_sync_scan1(struct mount *mp, struct vnode *vp, void *data)
934 {
935         hammer2_inode_t *ip;
936
937         ip = VTOI(vp);
938         if (vp->v_type == VNON || ip == NULL ||
939             ((ip->flags & HAMMER2_INODE_MODIFIED) == 0 &&
940              RB_EMPTY(&vp->v_rbdirty_tree))) {
941                 return(-1);
942         }
943         return(0);
944 }
945
946 static int
947 hammer2_sync_scan2(struct mount *mp, struct vnode *vp, void *data)
948 {
949         struct hammer2_sync_info *info = data;
950         hammer2_inode_t *ip;
951         hammer2_chain_t *parent;
952         int error;
953
954         ip = VTOI(vp);
955         if (vp->v_type == VNON || vp->v_type == VBAD ||
956             ((ip->flags & HAMMER2_INODE_MODIFIED) == 0 &&
957              RB_EMPTY(&vp->v_rbdirty_tree))) {
958                 return(0);
959         }
960
961         /*
962          * VOP_FSYNC will start a new transaction so replicate some code
963          * here to do it inline (see hammer2_vop_fsync()).
964          */
965         parent = hammer2_inode_lock_ex(ip);
966         atomic_clear_int(&ip->flags, HAMMER2_INODE_MODIFIED);
967         if (ip->vp)
968                 vfsync(ip->vp, MNT_NOWAIT, 1, NULL, NULL);
969         hammer2_chain_flush(&info->trans, parent);
970         hammer2_inode_unlock_ex(ip, parent);
971         error = 0;
972 #if 0
973         error = VOP_FSYNC(vp, MNT_NOWAIT, 0);
974 #endif
975         if (error)
976                 info->error = error;
977         return(0);
978 }
979
980 static
981 int
982 hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp)
983 {
984         return (0);
985 }
986
987 static
988 int
989 hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
990                struct fid *fhp, struct vnode **vpp)
991 {
992         return (0);
993 }
994
995 static
996 int
997 hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
998                  int *exflagsp, struct ucred **credanonp)
999 {
1000         return (0);
1001 }
1002
1003 /*
1004  * Support code for hammer2_mount().  Read, verify, and install the volume
1005  * header into the HMP
1006  *
1007  * XXX read four volhdrs and use the one with the highest TID whos CRC
1008  *     matches.
1009  *
1010  * XXX check iCRCs.
1011  *
1012  * XXX For filesystems w/ less than 4 volhdrs, make sure to not write to
1013  *     nonexistant locations.
1014  *
1015  * XXX Record selected volhdr and ring updates to each of 4 volhdrs
1016  */
1017 static
1018 int
1019 hammer2_install_volume_header(hammer2_mount_t *hmp)
1020 {
1021         hammer2_volume_data_t *vd;
1022         struct buf *bp;
1023         hammer2_crc32_t crc0, crc, bcrc0, bcrc;
1024         int error_reported;
1025         int error;
1026         int valid;
1027         int i;
1028
1029         error_reported = 0;
1030         error = 0;
1031         valid = 0;
1032         bp = NULL;
1033
1034         /*
1035          * There are up to 4 copies of the volume header (syncs iterate
1036          * between them so there is no single master).  We don't trust the
1037          * volu_size field so we don't know precisely how large the filesystem
1038          * is, so depend on the OS to return an error if we go beyond the
1039          * block device's EOF.
1040          */
1041         for (i = 0; i < HAMMER2_NUM_VOLHDRS; i++) {
1042                 error = bread(hmp->devvp, i * HAMMER2_ZONE_BYTES64,
1043                               HAMMER2_VOLUME_BYTES, &bp);
1044                 if (error) {
1045                         brelse(bp);
1046                         bp = NULL;
1047                         continue;
1048                 }
1049
1050                 vd = (struct hammer2_volume_data *) bp->b_data;
1051                 if ((vd->magic != HAMMER2_VOLUME_ID_HBO) &&
1052                     (vd->magic != HAMMER2_VOLUME_ID_ABO)) {
1053                         brelse(bp);
1054                         bp = NULL;
1055                         continue;
1056                 }
1057
1058                 if (vd->magic == HAMMER2_VOLUME_ID_ABO) {
1059                         /* XXX: Reversed-endianness filesystem */
1060                         kprintf("hammer2: reverse-endian filesystem detected");
1061                         brelse(bp);
1062                         bp = NULL;
1063                         continue;
1064                 }
1065
1066                 crc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT0];
1067                 crc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC0_OFF,
1068                                       HAMMER2_VOLUME_ICRC0_SIZE);
1069                 bcrc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT1];
1070                 bcrc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC1_OFF,
1071                                        HAMMER2_VOLUME_ICRC1_SIZE);
1072                 if ((crc0 != crc) || (bcrc0 != bcrc)) {
1073                         kprintf("hammer2 volume header crc "
1074                                 "mismatch copy #%d %08x/%08x\n",
1075                                 i, crc0, crc);
1076                         error_reported = 1;
1077                         brelse(bp);
1078                         bp = NULL;
1079                         continue;
1080                 }
1081                 if (valid == 0 || hmp->voldata.mirror_tid < vd->mirror_tid) {
1082                         valid = 1;
1083                         hmp->voldata = *vd;
1084                         hmp->volhdrno = i;
1085                 }
1086                 brelse(bp);
1087                 bp = NULL;
1088         }
1089         if (valid) {
1090                 hmp->volsync = hmp->voldata;
1091                 error = 0;
1092                 if (error_reported || bootverbose || 1) { /* 1/DEBUG */
1093                         kprintf("hammer2: using volume header #%d\n",
1094                                 hmp->volhdrno);
1095                 }
1096         } else {
1097                 error = EINVAL;
1098                 kprintf("hammer2: no valid volume headers found!\n");
1099         }
1100         return (error);
1101 }
1102
1103 /*
1104  * Reconnect using the passed file pointer.  The caller must ref the
1105  * fp for us.
1106  */
1107 void
1108 hammer2_cluster_reconnect(hammer2_pfsmount_t *pmp, struct file *fp)
1109 {
1110         hammer2_inode_data_t *ipdata;
1111         hammer2_chain_t *parent;
1112         size_t name_len;
1113
1114         /*
1115          * Closes old comm descriptor, kills threads, cleans up
1116          * states, then installs the new descriptor and creates
1117          * new threads.
1118          */
1119         kdmsg_iocom_reconnect(&pmp->iocom, fp, "hammer2");
1120
1121         /*
1122          * Setup LNK_CONN fields for autoinitiated state machine
1123          */
1124         parent = hammer2_inode_lock_ex(pmp->iroot);
1125         ipdata = &parent->data->ipdata;
1126         pmp->iocom.auto_lnk_conn.pfs_clid = ipdata->pfs_clid;
1127         pmp->iocom.auto_lnk_conn.pfs_fsid = ipdata->pfs_fsid;
1128         pmp->iocom.auto_lnk_conn.pfs_type = ipdata->pfs_type;
1129         pmp->iocom.auto_lnk_conn.proto_version = DMSG_SPAN_PROTO_1;
1130         pmp->iocom.auto_lnk_conn.peer_type = pmp->hmp->voldata.peer_type;
1131
1132         /*
1133          * Filter adjustment.  Clients do not need visibility into other
1134          * clients (otherwise millions of clients would present a serious
1135          * problem).  The fs_label also serves to restrict the namespace.
1136          */
1137         pmp->iocom.auto_lnk_conn.peer_mask = 1LLU << HAMMER2_PEER_HAMMER2;
1138         pmp->iocom.auto_lnk_conn.pfs_mask = (uint64_t)-1;
1139         switch (ipdata->pfs_type) {
1140         case DMSG_PFSTYPE_CLIENT:
1141                 pmp->iocom.auto_lnk_conn.peer_mask &=
1142                                 ~(1LLU << DMSG_PFSTYPE_CLIENT);
1143                 break;
1144         default:
1145                 break;
1146         }
1147
1148         name_len = ipdata->name_len;
1149         if (name_len >= sizeof(pmp->iocom.auto_lnk_conn.fs_label))
1150                 name_len = sizeof(pmp->iocom.auto_lnk_conn.fs_label) - 1;
1151         bcopy(ipdata->filename,
1152               pmp->iocom.auto_lnk_conn.fs_label,
1153               name_len);
1154         pmp->iocom.auto_lnk_conn.fs_label[name_len] = 0;
1155
1156         /*
1157          * Setup LNK_SPAN fields for autoinitiated state machine
1158          */
1159         pmp->iocom.auto_lnk_span.pfs_clid = ipdata->pfs_clid;
1160         pmp->iocom.auto_lnk_span.pfs_fsid = ipdata->pfs_fsid;
1161         pmp->iocom.auto_lnk_span.pfs_type = ipdata->pfs_type;
1162         pmp->iocom.auto_lnk_span.peer_type = pmp->hmp->voldata.peer_type;
1163         pmp->iocom.auto_lnk_span.proto_version = DMSG_SPAN_PROTO_1;
1164         name_len = ipdata->name_len;
1165         if (name_len >= sizeof(pmp->iocom.auto_lnk_span.fs_label))
1166                 name_len = sizeof(pmp->iocom.auto_lnk_span.fs_label) - 1;
1167         bcopy(ipdata->filename,
1168               pmp->iocom.auto_lnk_span.fs_label,
1169               name_len);
1170         pmp->iocom.auto_lnk_span.fs_label[name_len] = 0;
1171         hammer2_inode_unlock_ex(pmp->iroot, parent);
1172
1173         kdmsg_iocom_autoinitiate(&pmp->iocom, hammer2_autodmsg);
1174 }
1175
1176 static int
1177 hammer2_rcvdmsg(kdmsg_msg_t *msg)
1178 {
1179         switch(msg->any.head.cmd & DMSGF_TRANSMASK) {
1180         case DMSG_DBG_SHELL:
1181                 /*
1182                  * (non-transaction)
1183                  * Execute shell command (not supported atm)
1184                  */
1185                 kdmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
1186                 break;
1187         case DMSG_DBG_SHELL | DMSGF_REPLY:
1188                 /*
1189                  * (non-transaction)
1190                  */
1191                 if (msg->aux_data) {
1192                         msg->aux_data[msg->aux_size - 1] = 0;
1193                         kprintf("HAMMER2 DBG: %s\n", msg->aux_data);
1194                 }
1195                 break;
1196         default:
1197                 /*
1198                  * Unsupported message received.  We only need to
1199                  * reply if it's a transaction in order to close our end.
1200                  * Ignore any one-way messages are any further messages
1201                  * associated with the transaction.
1202                  *
1203                  * NOTE: This case also includes DMSG_LNK_ERROR messages
1204                  *       which might be one-way, replying to those would
1205                  *       cause an infinite ping-pong.
1206                  */
1207                 if (msg->any.head.cmd & DMSGF_CREATE)
1208                         kdmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
1209                 break;
1210         }
1211         return(0);
1212 }
1213
1214 /*
1215  * This function is called after KDMSG has automatically handled processing
1216  * of a LNK layer message (typically CONN, SPAN, or CIRC).
1217  *
1218  * We tag off the LNK_CONN to trigger our LNK_VOLCONF messages which
1219  * advertises all available hammer2 super-root volumes.
1220  */
1221 static void
1222 hammer2_autodmsg(kdmsg_msg_t *msg)
1223 {
1224         hammer2_pfsmount_t *pmp = msg->iocom->handle;
1225         hammer2_mount_t *hmp = pmp->hmp;
1226         int copyid;
1227
1228         /*
1229          * We only care about replies to our LNK_CONN auto-request.  kdmsg
1230          * has already processed the reply, we use this calback as a shim
1231          * to know when we can advertise available super-root volumes.
1232          */
1233         if ((msg->any.head.cmd & DMSGF_TRANSMASK) !=
1234             (DMSG_LNK_CONN | DMSGF_CREATE | DMSGF_REPLY) ||
1235             msg->state == NULL) {
1236                 return;
1237         }
1238
1239         kprintf("LNK_CONN REPLY RECEIVED CMD %08x\n", msg->any.head.cmd);
1240
1241         if (msg->any.head.cmd & DMSGF_CREATE) {
1242                 kprintf("HAMMER2: VOLDATA DUMP\n");
1243
1244                 /*
1245                  * Dump the configuration stored in the volume header
1246                  */
1247                 hammer2_voldata_lock(hmp);
1248                 for (copyid = 0; copyid < HAMMER2_COPYID_COUNT; ++copyid) {
1249                         if (hmp->voldata.copyinfo[copyid].copyid == 0)
1250                                 continue;
1251                         hammer2_volconf_update(pmp, copyid);
1252                 }
1253                 hammer2_voldata_unlock(hmp, 0);
1254         }
1255         if ((msg->any.head.cmd & DMSGF_DELETE) &&
1256             msg->state && (msg->state->txcmd & DMSGF_DELETE) == 0) {
1257                 kprintf("HAMMER2: CONN WAS TERMINATED\n");
1258         }
1259 }
1260
1261 /*
1262  * Volume configuration updates are passed onto the userland service
1263  * daemon via the open LNK_CONN transaction.
1264  */
1265 void
1266 hammer2_volconf_update(hammer2_pfsmount_t *pmp, int index)
1267 {
1268         hammer2_mount_t *hmp = pmp->hmp;
1269         kdmsg_msg_t *msg;
1270
1271         /* XXX interlock against connection state termination */
1272         kprintf("volconf update %p\n", pmp->iocom.conn_state);
1273         if (pmp->iocom.conn_state) {
1274                 kprintf("TRANSMIT VOLCONF VIA OPEN CONN TRANSACTION\n");
1275                 msg = kdmsg_msg_alloc_state(pmp->iocom.conn_state,
1276                                             DMSG_LNK_VOLCONF, NULL, NULL);
1277                 msg->any.lnk_volconf.copy = hmp->voldata.copyinfo[index];
1278                 msg->any.lnk_volconf.mediaid = hmp->voldata.fsid;
1279                 msg->any.lnk_volconf.index = index;
1280                 kdmsg_msg_write(msg);
1281         }
1282 }
1283
1284 void
1285 hammer2_dump_chain(hammer2_chain_t *chain, int tab, int *countp)
1286 {
1287         hammer2_chain_t *scan;
1288
1289         --*countp;
1290         if (*countp == 0) {
1291                 kprintf("%*.*s...\n", tab, tab, "");
1292                 return;
1293         }
1294         if (*countp < 0)
1295                 return;
1296         kprintf("%*.*schain[%d] %p.%d [%08x][core=%p] (%s) dl=%p refs=%d",
1297                 tab, tab, "",
1298                 chain->index, chain, chain->bref.type, chain->flags,
1299                 chain->core,
1300                 ((chain->bref.type == HAMMER2_BREF_TYPE_INODE &&
1301                 chain->data) ?  (char *)chain->data->ipdata.filename : "?"),
1302                 chain->next_parent, chain->refs);
1303         if (chain->core == NULL || RB_EMPTY(&chain->core->rbtree))
1304                 kprintf("\n");
1305         else
1306                 kprintf(" {\n");
1307         RB_FOREACH(scan, hammer2_chain_tree, &chain->core->rbtree) {
1308                 hammer2_dump_chain(scan, tab + 4, countp);
1309         }
1310         if (chain->core && !RB_EMPTY(&chain->core->rbtree)) {
1311                 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE && chain->data)
1312                         kprintf("%*.*s}(%s)\n", tab, tab, "",
1313                                 chain->data->ipdata.filename);
1314                 else
1315                         kprintf("%*.*s}\n", tab, tab, "");
1316         }
1317 }