hammer2 - Refactor flush
[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  * by Daniel Flores (GSOC 2013 - mentored by Matthew Dillon, compression)
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the
17  *    distribution.
18  * 3. Neither the name of The DragonFly Project nor the names of its
19  *    contributors may be used to endorse or promote products derived
20  *    from this software without specific, prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
26  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/nlookup.h>
39 #include <sys/vnode.h>
40 #include <sys/mount.h>
41 #include <sys/fcntl.h>
42 #include <sys/buf.h>
43 #include <sys/uuid.h>
44 #include <sys/vfsops.h>
45 #include <sys/sysctl.h>
46 #include <sys/socket.h>
47 #include <sys/objcache.h>
48
49 #include <sys/proc.h>
50 #include <sys/namei.h>
51 #include <sys/mountctl.h>
52 #include <sys/dirent.h>
53 #include <sys/uio.h>
54
55 #include <sys/mutex.h>
56 #include <sys/mutex2.h>
57
58 #include "hammer2.h"
59 #include "hammer2_disk.h"
60 #include "hammer2_mount.h"
61
62 #include "hammer2.h"
63 #include "hammer2_lz4.h"
64
65 #include "zlib/hammer2_zlib.h"
66
67 #define REPORT_REFS_ERRORS 1    /* XXX remove me */
68
69 MALLOC_DEFINE(M_OBJCACHE, "objcache", "Object Cache");
70
71 struct hammer2_sync_info {
72         hammer2_trans_t trans;
73         int error;
74         int waitfor;
75 };
76
77 TAILQ_HEAD(hammer2_mntlist, hammer2_mount);
78 static struct hammer2_mntlist hammer2_mntlist;
79 static struct lock hammer2_mntlk;
80
81 int hammer2_debug;
82 int hammer2_cluster_enable = 1;
83 int hammer2_hardlink_enable = 1;
84 long hammer2_iod_file_read;
85 long hammer2_iod_meta_read;
86 long hammer2_iod_indr_read;
87 long hammer2_iod_fmap_read;
88 long hammer2_iod_volu_read;
89 long hammer2_iod_file_write;
90 long hammer2_iod_meta_write;
91 long hammer2_iod_indr_write;
92 long hammer2_iod_fmap_write;
93 long hammer2_iod_volu_write;
94 long hammer2_ioa_file_read;
95 long hammer2_ioa_meta_read;
96 long hammer2_ioa_indr_read;
97 long hammer2_ioa_fmap_read;
98 long hammer2_ioa_volu_read;
99 long hammer2_ioa_fmap_write;
100 long hammer2_ioa_file_write;
101 long hammer2_ioa_meta_write;
102 long hammer2_ioa_indr_write;
103 long hammer2_ioa_volu_write;
104
105 MALLOC_DECLARE(C_BUFFER);
106 MALLOC_DEFINE(C_BUFFER, "compbuffer", "Buffer used for compression.");
107
108 MALLOC_DECLARE(D_BUFFER);
109 MALLOC_DEFINE(D_BUFFER, "decompbuffer", "Buffer used for decompression.");
110
111 SYSCTL_NODE(_vfs, OID_AUTO, hammer2, CTLFLAG_RW, 0, "HAMMER2 filesystem");
112
113 SYSCTL_INT(_vfs_hammer2, OID_AUTO, debug, CTLFLAG_RW,
114            &hammer2_debug, 0, "");
115 SYSCTL_INT(_vfs_hammer2, OID_AUTO, cluster_enable, CTLFLAG_RW,
116            &hammer2_cluster_enable, 0, "");
117 SYSCTL_INT(_vfs_hammer2, OID_AUTO, hardlink_enable, CTLFLAG_RW,
118            &hammer2_hardlink_enable, 0, "");
119
120 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_read, CTLFLAG_RW,
121            &hammer2_iod_file_read, 0, "");
122 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_meta_read, CTLFLAG_RW,
123            &hammer2_iod_meta_read, 0, "");
124 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_indr_read, CTLFLAG_RW,
125            &hammer2_iod_indr_read, 0, "");
126 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_fmap_read, CTLFLAG_RW,
127            &hammer2_iod_fmap_read, 0, "");
128 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_volu_read, CTLFLAG_RW,
129            &hammer2_iod_volu_read, 0, "");
130
131 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_write, CTLFLAG_RW,
132            &hammer2_iod_file_write, 0, "");
133 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_meta_write, CTLFLAG_RW,
134            &hammer2_iod_meta_write, 0, "");
135 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_indr_write, CTLFLAG_RW,
136            &hammer2_iod_indr_write, 0, "");
137 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_fmap_write, CTLFLAG_RW,
138            &hammer2_iod_fmap_write, 0, "");
139 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_volu_write, CTLFLAG_RW,
140            &hammer2_iod_volu_write, 0, "");
141
142 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_file_read, CTLFLAG_RW,
143            &hammer2_ioa_file_read, 0, "");
144 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_meta_read, CTLFLAG_RW,
145            &hammer2_ioa_meta_read, 0, "");
146 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_indr_read, CTLFLAG_RW,
147            &hammer2_ioa_indr_read, 0, "");
148 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_fmap_read, CTLFLAG_RW,
149            &hammer2_ioa_fmap_read, 0, "");
150 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_volu_read, CTLFLAG_RW,
151            &hammer2_ioa_volu_read, 0, "");
152
153 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_file_write, CTLFLAG_RW,
154            &hammer2_ioa_file_write, 0, "");
155 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_meta_write, CTLFLAG_RW,
156            &hammer2_ioa_meta_write, 0, "");
157 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_indr_write, CTLFLAG_RW,
158            &hammer2_ioa_indr_write, 0, "");
159 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_fmap_write, CTLFLAG_RW,
160            &hammer2_ioa_fmap_write, 0, "");
161 SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_volu_write, CTLFLAG_RW,
162            &hammer2_ioa_volu_write, 0, "");
163
164 static int hammer2_vfs_init(struct vfsconf *conf);
165 static int hammer2_vfs_uninit(struct vfsconf *vfsp);
166 static int hammer2_vfs_mount(struct mount *mp, char *path, caddr_t data,
167                                 struct ucred *cred);
168 static int hammer2_remount(hammer2_mount_t *, char *, struct vnode *,
169                                 struct ucred *);
170 static int hammer2_vfs_unmount(struct mount *mp, int mntflags);
171 static int hammer2_vfs_root(struct mount *mp, struct vnode **vpp);
172 static int hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp,
173                                 struct ucred *cred);
174 static int hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp,
175                                 struct ucred *cred);
176 static int hammer2_vfs_vget(struct mount *mp, struct vnode *dvp,
177                                 ino_t ino, struct vnode **vpp);
178 static int hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
179                                 struct fid *fhp, struct vnode **vpp);
180 static int hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp);
181 static int hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
182                                 int *exflagsp, struct ucred **credanonp);
183
184 static int hammer2_install_volume_header(hammer2_mount_t *hmp);
185 static int hammer2_sync_scan2(struct mount *mp, struct vnode *vp, void *data);
186
187 static void hammer2_write_thread(void *arg);
188
189 /* 
190  * Functions for compression in threads,
191  * from hammer2_vnops.c
192  */
193 static void hammer2_write_file_core(struct buf *bp, hammer2_trans_t *trans,
194                                 hammer2_inode_t *ip,
195                                 hammer2_inode_data_t *ipdata,
196                                 hammer2_chain_t **parentp,
197                                 hammer2_key_t lbase, int ioflag, int pblksize,
198                                 int *errorp);
199 static void hammer2_compress_and_write(struct buf *bp, hammer2_trans_t *trans,
200                                 hammer2_inode_t *ip,
201                                 hammer2_inode_data_t *ipdata,
202                                 hammer2_chain_t **parentp,
203                                 hammer2_key_t lbase, int ioflag,
204                                 int pblksize, int *errorp, int comp_algo);
205 static void hammer2_zero_check_and_write(struct buf *bp,
206                                 hammer2_trans_t *trans, hammer2_inode_t *ip,
207                                 hammer2_inode_data_t *ipdata,
208                                 hammer2_chain_t **parentp,
209                                 hammer2_key_t lbase,
210                                 int ioflag, int pblksize, int *errorp);
211 static int test_block_zeros(const char *buf, size_t bytes);
212 static void zero_write(struct buf *bp, hammer2_trans_t *trans,
213                                 hammer2_inode_t *ip,
214                                 hammer2_inode_data_t *ipdata,
215                                 hammer2_chain_t **parentp, 
216                                 hammer2_key_t lbase,
217                                 int *errorp);
218 static void hammer2_write_bp(hammer2_chain_t *chain, struct buf *bp,
219                                 int ioflag, int pblksize, int *errorp);
220
221 static int hammer2_rcvdmsg(kdmsg_msg_t *msg);
222 static void hammer2_autodmsg(kdmsg_msg_t *msg);
223
224
225 /*
226  * HAMMER2 vfs operations.
227  */
228 static struct vfsops hammer2_vfsops = {
229         .vfs_init       = hammer2_vfs_init,
230         .vfs_uninit = hammer2_vfs_uninit,
231         .vfs_sync       = hammer2_vfs_sync,
232         .vfs_mount      = hammer2_vfs_mount,
233         .vfs_unmount    = hammer2_vfs_unmount,
234         .vfs_root       = hammer2_vfs_root,
235         .vfs_statfs     = hammer2_vfs_statfs,
236         .vfs_statvfs    = hammer2_vfs_statvfs,
237         .vfs_vget       = hammer2_vfs_vget,
238         .vfs_vptofh     = hammer2_vfs_vptofh,
239         .vfs_fhtovp     = hammer2_vfs_fhtovp,
240         .vfs_checkexp   = hammer2_vfs_checkexp
241 };
242
243 MALLOC_DEFINE(M_HAMMER2, "HAMMER2-mount", "");
244
245 VFS_SET(hammer2_vfsops, hammer2, 0);
246 MODULE_VERSION(hammer2, 1);
247
248 static
249 int
250 hammer2_vfs_init(struct vfsconf *conf)
251 {
252         static struct objcache_malloc_args margs_read;
253         static struct objcache_malloc_args margs_write;
254
255         int error;
256
257         error = 0;
258
259         if (HAMMER2_BLOCKREF_BYTES != sizeof(struct hammer2_blockref))
260                 error = EINVAL;
261         if (HAMMER2_INODE_BYTES != sizeof(struct hammer2_inode_data))
262                 error = EINVAL;
263         if (HAMMER2_VOLUME_BYTES != sizeof(struct hammer2_volume_data))
264                 error = EINVAL;
265
266         if (error)
267                 kprintf("HAMMER2 structure size mismatch; cannot continue.\n");
268         
269         margs_read.objsize = 65536;
270         margs_read.mtype = D_BUFFER;
271         
272         margs_write.objsize = 32768;
273         margs_write.mtype = C_BUFFER;
274         
275         cache_buffer_read = objcache_create(margs_read.mtype->ks_shortdesc,
276                                 0, 1, NULL, NULL, NULL, objcache_malloc_alloc,
277                                 objcache_malloc_free, &margs_read);
278         cache_buffer_write = objcache_create(margs_write.mtype->ks_shortdesc,
279                                 0, 1, NULL, NULL, NULL, objcache_malloc_alloc,
280                                 objcache_malloc_free, &margs_write);
281
282         lockinit(&hammer2_mntlk, "mntlk", 0, 0);
283         TAILQ_INIT(&hammer2_mntlist);
284
285         return (error);
286 }
287
288 static
289 int
290 hammer2_vfs_uninit(struct vfsconf *vfsp __unused)
291 {
292         objcache_destroy(cache_buffer_read);
293         objcache_destroy(cache_buffer_write);
294         return 0;
295 }
296
297 /*
298  * Mount or remount HAMMER2 fileystem from physical media
299  *
300  *      mountroot
301  *              mp              mount point structure
302  *              path            NULL
303  *              data            <unused>
304  *              cred            <unused>
305  *
306  *      mount
307  *              mp              mount point structure
308  *              path            path to mount point
309  *              data            pointer to argument structure in user space
310  *                      volume  volume path (device@LABEL form)
311  *                      hflags  user mount flags
312  *              cred            user credentials
313  *
314  * RETURNS:     0       Success
315  *              !0      error number
316  */
317 static
318 int
319 hammer2_vfs_mount(struct mount *mp, char *path, caddr_t data,
320                   struct ucred *cred)
321 {
322         struct hammer2_mount_info info;
323         hammer2_pfsmount_t *pmp;
324         hammer2_mount_t *hmp;
325         hammer2_key_t key_next;
326         hammer2_key_t key_dummy;
327         hammer2_key_t lhc;
328         struct vnode *devvp;
329         struct nlookupdata nd;
330         hammer2_chain_t *parent;
331         hammer2_chain_t *schain;
332         hammer2_chain_t *rchain;
333         struct file *fp;
334         char devstr[MNAMELEN];
335         size_t size;
336         size_t done;
337         char *dev;
338         char *label;
339         int ronly = 1;
340         int error;
341         int cache_index;
342         int i;
343
344         hmp = NULL;
345         pmp = NULL;
346         dev = NULL;
347         label = NULL;
348         devvp = NULL;
349         cache_index = -1;
350
351         kprintf("hammer2_mount\n");
352
353         if (path == NULL) {
354                 /*
355                  * Root mount
356                  */
357                 bzero(&info, sizeof(info));
358                 info.cluster_fd = -1;
359                 return (EOPNOTSUPP);
360         } else {
361                 /*
362                  * Non-root mount or updating a mount
363                  */
364                 error = copyin(data, &info, sizeof(info));
365                 if (error)
366                         return (error);
367
368                 error = copyinstr(info.volume, devstr, MNAMELEN - 1, &done);
369                 if (error)
370                         return (error);
371
372                 /* Extract device and label */
373                 dev = devstr;
374                 label = strchr(devstr, '@');
375                 if (label == NULL ||
376                     ((label + 1) - dev) > done) {
377                         return (EINVAL);
378                 }
379                 *label = '\0';
380                 label++;
381                 if (*label == '\0')
382                         return (EINVAL);
383
384                 if (mp->mnt_flag & MNT_UPDATE) {
385                         /* Update mount */
386                         /* HAMMER2 implements NFS export via mountctl */
387                         pmp = MPTOPMP(mp);
388                         for (i = 0; i < pmp->cluster.nchains; ++i) {
389                                 hmp = pmp->cluster.chains[i]->hmp;
390                                 devvp = hmp->devvp;
391                                 error = hammer2_remount(hmp, path, devvp, cred);
392                                 if (error)
393                                         break;
394                         }
395                         return error;
396                 }
397         }
398
399         /*
400          * PFS mount
401          *
402          * Lookup name and verify it refers to a block device.
403          */
404         error = nlookup_init(&nd, dev, UIO_SYSSPACE, NLC_FOLLOW);
405         if (error == 0)
406                 error = nlookup(&nd);
407         if (error == 0)
408                 error = cache_vref(&nd.nl_nch, nd.nl_cred, &devvp);
409         nlookup_done(&nd);
410
411         if (error == 0) {
412                 if (vn_isdisk(devvp, &error))
413                         error = vfs_mountedon(devvp);
414         }
415
416         /*
417          * Determine if the device has already been mounted.  After this
418          * check hmp will be non-NULL if we are doing the second or more
419          * hammer2 mounts from the same device.
420          */
421         lockmgr(&hammer2_mntlk, LK_EXCLUSIVE);
422         TAILQ_FOREACH(hmp, &hammer2_mntlist, mntentry) {
423                 if (hmp->devvp == devvp)
424                         break;
425         }
426
427         /*
428          * Open the device if this isn't a secondary mount and construct
429          * the H2 device mount (hmp).
430          */
431         if (hmp == NULL) {
432                 if (error == 0 && vcount(devvp) > 0)
433                         error = EBUSY;
434
435                 /*
436                  * Now open the device
437                  */
438                 if (error == 0) {
439                         ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
440                         vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
441                         error = vinvalbuf(devvp, V_SAVE, 0, 0);
442                         if (error == 0) {
443                                 error = VOP_OPEN(devvp,
444                                                  ronly ? FREAD : FREAD | FWRITE,
445                                                  FSCRED, NULL);
446                         }
447                         vn_unlock(devvp);
448                 }
449                 if (error && devvp) {
450                         vrele(devvp);
451                         devvp = NULL;
452                 }
453                 if (error) {
454                         lockmgr(&hammer2_mntlk, LK_RELEASE);
455                         return error;
456                 }
457                 hmp = kmalloc(sizeof(*hmp), M_HAMMER2, M_WAITOK | M_ZERO);
458                 hmp->ronly = ronly;
459                 hmp->devvp = devvp;
460                 hmp->last_flush_tid = 0;
461                 hmp->topo_flush_tid = HAMMER2_MAX_TID;
462                 kmalloc_create(&hmp->mchain, "HAMMER2-chains");
463                 TAILQ_INSERT_TAIL(&hammer2_mntlist, hmp, mntentry);
464
465                 lockinit(&hmp->alloclk, "h2alloc", 0, 0);
466                 lockinit(&hmp->voldatalk, "voldata", 0, LK_CANRECURSE);
467                 TAILQ_INIT(&hmp->transq);
468
469                 /*
470                  * vchain setup. vchain.data is embedded.
471                  * vchain.refs is initialized and will never drop to 0.
472                  */
473                 hmp->vchain.hmp = hmp;
474                 hmp->vchain.refs = 1;
475                 hmp->vchain.data = (void *)&hmp->voldata;
476                 hmp->vchain.bref.type = HAMMER2_BREF_TYPE_VOLUME;
477                 hmp->vchain.bref.data_off = 0 | HAMMER2_PBUFRADIX;
478                 hmp->vchain.delete_tid = HAMMER2_MAX_TID;
479                 hammer2_chain_core_alloc(NULL, &hmp->vchain, NULL);
480                 /* hmp->vchain.u.xxx is left NULL */
481
482                 /*
483                  * fchain setup.  fchain.data is embedded.
484                  * fchain.refs is initialized and will never drop to 0.
485                  *
486                  * The data is not used but needs to be initialized to
487                  * pass assertion muster.  We use this chain primarily
488                  * as a placeholder for the freemap's top-level RBTREE
489                  * so it does not interfere with the volume's topology
490                  * RBTREE.
491                  */
492                 hmp->fchain.hmp = hmp;
493                 hmp->fchain.refs = 1;
494                 hmp->fchain.data = (void *)&hmp->voldata.freemap_blockset;
495                 hmp->fchain.bref.type = HAMMER2_BREF_TYPE_FREEMAP;
496                 hmp->fchain.bref.data_off = 0 | HAMMER2_PBUFRADIX;
497                 hmp->fchain.bref.methods =
498                         HAMMER2_ENC_CHECK(HAMMER2_CHECK_FREEMAP) |
499                         HAMMER2_ENC_COMP(HAMMER2_COMP_NONE);
500                 hmp->fchain.delete_tid = HAMMER2_MAX_TID;
501
502                 hammer2_chain_core_alloc(NULL, &hmp->fchain, NULL);
503                 /* hmp->fchain.u.xxx is left NULL */
504
505                 /*
506                  * Install the volume header
507                  */
508                 error = hammer2_install_volume_header(hmp);
509                 if (error) {
510                         hammer2_vfs_unmount(mp, MNT_FORCE);
511                         return error;
512                 }
513
514                 /*
515                  * First locate the super-root inode, which is key 0
516                  * relative to the volume header's blockset.
517                  *
518                  * Then locate the root inode by scanning the directory keyspace
519                  * represented by the label.
520                  */
521                 parent = hammer2_chain_lookup_init(&hmp->vchain, 0);
522                 schain = hammer2_chain_lookup(&parent, &key_dummy,
523                                       HAMMER2_SROOT_KEY, HAMMER2_SROOT_KEY,
524                                       &cache_index, 0);
525                 hammer2_chain_lookup_done(parent);
526                 if (schain == NULL) {
527                         kprintf("hammer2_mount: invalid super-root\n");
528                         hammer2_vfs_unmount(mp, MNT_FORCE);
529                         return EINVAL;
530                 }
531
532                 /*
533                  * NOTE: inode_get sucks up schain's lock.
534                  */
535                 atomic_set_int(&schain->flags, HAMMER2_CHAIN_PFSROOT);
536                 hmp->sroot = hammer2_inode_get(NULL, NULL, schain);
537                 hammer2_inode_ref(hmp->sroot);
538                 hammer2_inode_unlock_ex(hmp->sroot, schain);
539                 schain = NULL;
540                 /* leave hmp->sroot with one ref */
541         }
542
543         /*
544          * Block device opened successfully, finish initializing the
545          * mount structure.
546          *
547          * From this point on we have to call hammer2_unmount() on failure.
548          */
549         pmp = kmalloc(sizeof(*pmp), M_HAMMER2, M_WAITOK | M_ZERO);
550
551         kmalloc_create(&pmp->minode, "HAMMER2-inodes");
552         kmalloc_create(&pmp->mmsg, "HAMMER2-pfsmsg");
553
554         spin_init(&pmp->inum_spin);
555         RB_INIT(&pmp->inum_tree);
556
557         kdmsg_iocom_init(&pmp->iocom, pmp,
558                          KDMSG_IOCOMF_AUTOCONN |
559                          KDMSG_IOCOMF_AUTOSPAN |
560                          KDMSG_IOCOMF_AUTOCIRC,
561                          pmp->mmsg, hammer2_rcvdmsg);
562
563         ccms_domain_init(&pmp->ccms_dom);
564         ++hmp->pmp_count;
565         lockmgr(&hammer2_mntlk, LK_RELEASE);
566         kprintf("hammer2_mount hmp=%p pmp=%p pmpcnt=%d\n",
567                 hmp, pmp, hmp->pmp_count);
568
569         mp->mnt_flag = MNT_LOCAL;
570         mp->mnt_kern_flag |= MNTK_ALL_MPSAFE;   /* all entry pts are SMP */
571         mp->mnt_kern_flag |= MNTK_THR_SYNC;     /* new vsyncscan semantics */
572
573         /*
574          * required mount structure initializations
575          */
576         mp->mnt_stat.f_iosize = HAMMER2_PBUFSIZE;
577         mp->mnt_stat.f_bsize = HAMMER2_PBUFSIZE;
578
579         mp->mnt_vstat.f_frsize = HAMMER2_PBUFSIZE;
580         mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE;
581
582         /*
583          * Optional fields
584          */
585         mp->mnt_iosize_max = MAXPHYS;
586         mp->mnt_data = (qaddr_t)pmp;
587         pmp->mp = mp;
588
589         /*
590          * Lookup mount point under the media-localized super-root.
591          */
592         parent = hammer2_inode_lock_ex(hmp->sroot);
593         lhc = hammer2_dirhash(label, strlen(label));
594         rchain = hammer2_chain_lookup(&parent, &key_next,
595                                       lhc, lhc + HAMMER2_DIRHASH_LOMASK,
596                                       &cache_index, 0);
597         while (rchain) {
598                 if (rchain->bref.type == HAMMER2_BREF_TYPE_INODE &&
599                     strcmp(label, rchain->data->ipdata.filename) == 0) {
600                         break;
601                 }
602                 rchain = hammer2_chain_next(&parent, rchain, &key_next,
603                                             key_next,
604                                             lhc + HAMMER2_DIRHASH_LOMASK,
605                                             &cache_index, 0);
606         }
607         hammer2_inode_unlock_ex(hmp->sroot, parent);
608
609         if (rchain == NULL) {
610                 kprintf("hammer2_mount: PFS label not found\n");
611                 --hmp->pmp_count;
612                 hammer2_vfs_unmount(mp, MNT_FORCE);
613                 return EINVAL;
614         }
615         if (rchain->flags & HAMMER2_CHAIN_MOUNTED) {
616                 hammer2_chain_unlock(rchain);
617                 kprintf("hammer2_mount: PFS label already mounted!\n");
618                 --hmp->pmp_count;
619                 hammer2_vfs_unmount(mp, MNT_FORCE);
620                 return EBUSY;
621         }
622 #if 0
623         if (rchain->flags & HAMMER2_CHAIN_RECYCLE) {
624                 kprintf("hammer2_mount: PFS label currently recycling\n");
625                 --hmp->pmp_count;
626                 hammer2_vfs_unmount(mp, MNT_FORCE);
627                 return EBUSY;
628         }
629 #endif
630
631         atomic_set_int(&rchain->flags, HAMMER2_CHAIN_MOUNTED);
632
633         /*
634          * NOTE: *_get() integrates chain's lock into the inode lock.
635          */
636         hammer2_chain_ref(rchain);              /* for pmp->rchain */
637         pmp->cluster.nchains = 1;
638         pmp->cluster.chains[0] = rchain;
639         pmp->iroot = hammer2_inode_get(pmp, NULL, rchain);
640         hammer2_inode_ref(pmp->iroot);          /* ref for pmp->iroot */
641
642         KKASSERT(rchain->pmp == NULL);          /* tracking pmp for rchain */
643         rchain->pmp = pmp;
644         atomic_add_long(&pmp->inmem_chains, 1);
645
646         hammer2_inode_unlock_ex(pmp->iroot, rchain);
647
648         kprintf("iroot %p\n", pmp->iroot);
649
650         /*
651          * The logical file buffer bio write thread handles things
652          * like physical block assignment and compression.
653          */
654         mtx_init(&pmp->wthread_mtx);
655         bioq_init(&pmp->wthread_bioq);
656         pmp->wthread_destroy = 0;
657         lwkt_create(hammer2_write_thread, pmp,
658                     &pmp->wthread_td, NULL, 0, -1, "hwrite-%s", label);
659
660         /*
661          * Ref the cluster management messaging descriptor.  The mount
662          * program deals with the other end of the communications pipe.
663          */
664         fp = holdfp(curproc->p_fd, info.cluster_fd, -1);
665         if (fp == NULL) {
666                 kprintf("hammer2_mount: bad cluster_fd!\n");
667                 hammer2_vfs_unmount(mp, MNT_FORCE);
668                 return EBADF;
669         }
670         hammer2_cluster_reconnect(pmp, fp);
671
672         /*
673          * Finish setup
674          */
675         vfs_getnewfsid(mp);
676         vfs_add_vnodeops(mp, &hammer2_vnode_vops, &mp->mnt_vn_norm_ops);
677         vfs_add_vnodeops(mp, &hammer2_spec_vops, &mp->mnt_vn_spec_ops);
678         vfs_add_vnodeops(mp, &hammer2_fifo_vops, &mp->mnt_vn_fifo_ops);
679
680         copyinstr(info.volume, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size);
681         bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
682         bzero(mp->mnt_stat.f_mntonname, sizeof(mp->mnt_stat.f_mntonname));
683         copyinstr(path, mp->mnt_stat.f_mntonname,
684                   sizeof(mp->mnt_stat.f_mntonname) - 1,
685                   &size);
686
687         /*
688          * Initial statfs to prime mnt_stat.
689          */
690         hammer2_vfs_statfs(mp, &mp->mnt_stat, cred);
691         
692         return 0;
693 }
694
695 /*
696  * Handle bioq for strategy write
697  */
698 static
699 void
700 hammer2_write_thread(void *arg)
701 {
702         hammer2_pfsmount_t *pmp;
703         struct bio *bio;
704         struct buf *bp;
705         hammer2_trans_t trans;
706         struct vnode *vp;
707         hammer2_inode_t *ip;
708         hammer2_chain_t *parent;
709         hammer2_chain_t **parentp;
710         hammer2_inode_data_t *ipdata;
711         hammer2_key_t lbase;
712         int lblksize;
713         int pblksize;
714         int error;
715         
716         pmp = arg;
717         
718         mtx_lock(&pmp->wthread_mtx);
719         while (pmp->wthread_destroy == 0) {
720                 if (bioq_first(&pmp->wthread_bioq) == NULL) {
721                         mtxsleep(&pmp->wthread_bioq, &pmp->wthread_mtx,
722                                  0, "h2bioqw", 0);
723                 }
724                 parent = NULL;
725                 parentp = &parent;
726
727                 hammer2_trans_init(&trans, pmp, HAMMER2_TRANS_BUFCACHE);
728
729                 while ((bio = bioq_takefirst(&pmp->wthread_bioq)) != NULL) {
730                         /*
731                          * dummy bio for synchronization
732                          */
733                         if (bio->bio_buf == NULL) {
734                                 bio->bio_flags |= BIO_DONE;
735                                 wakeup(bio);
736                                 continue;
737                         }
738
739                         /*
740                          * else normal bio processing
741                          */
742                         mtx_unlock(&pmp->wthread_mtx);
743                         
744                         error = 0;
745                         bp = bio->bio_buf;
746                         vp = bp->b_vp;
747                         ip = VTOI(vp);
748
749                         /*
750                          * Inode is modified, flush size and mtime changes
751                          * to ensure that the file size remains consistent
752                          * with the buffers being flushed.
753                          */
754                         parent = hammer2_inode_lock_ex(ip);
755                         if (ip->flags & (HAMMER2_INODE_RESIZED |
756                                          HAMMER2_INODE_MTIME)) {
757                                 hammer2_inode_fsync(&trans, ip, parentp);
758                         }
759                         ipdata = hammer2_chain_modify_ip(&trans, ip,
760                                                          parentp, 0);
761                         lblksize = hammer2_calc_logical(ip, bio->bio_offset,
762                                                         &lbase, NULL);
763                         pblksize = hammer2_calc_physical(ip, lbase);
764                         hammer2_write_file_core(bp, &trans, ip, ipdata,
765                                                 parentp,
766                                                 lbase, IO_ASYNC,
767                                                 pblksize, &error);
768                         hammer2_inode_unlock_ex(ip, parent);
769                         if (error) {
770                                 kprintf("hammer2: error in buffer write\n");
771                                 bp->b_flags |= B_ERROR;
772                                 bp->b_error = EIO;
773                         }
774                         biodone(bio);
775                         mtx_lock(&pmp->wthread_mtx);
776                 }
777                 hammer2_trans_done(&trans);
778         }
779         pmp->wthread_destroy = -1;
780         wakeup(&pmp->wthread_destroy);
781         
782         mtx_unlock(&pmp->wthread_mtx);
783 }
784
785 void
786 hammer2_bioq_sync(hammer2_pfsmount_t *pmp)
787 {
788         struct bio sync_bio;
789
790         bzero(&sync_bio, sizeof(sync_bio));     /* dummy with no bio_buf */
791         mtx_lock(&pmp->wthread_mtx);
792         if (pmp->wthread_destroy == 0) {
793                 if (TAILQ_EMPTY(&pmp->wthread_bioq.queue)) {
794                        bioq_insert_tail(&pmp->wthread_bioq, &sync_bio);
795                        wakeup(&pmp->wthread_bioq);
796                 } else {
797                        bioq_insert_tail(&pmp->wthread_bioq, &sync_bio);
798                 }
799                 while ((sync_bio.bio_flags & BIO_DONE) == 0)
800                         mtxsleep(&sync_bio, &pmp->wthread_mtx, 0, "h2bioq", 0);
801         }
802         mtx_unlock(&pmp->wthread_mtx);
803 }
804
805 /* 
806  * Return a chain suitable for I/O, creating the chain if necessary
807  * and assigning its physical block.
808  */
809 static
810 hammer2_chain_t *
811 hammer2_assign_physical(hammer2_trans_t *trans,
812                         hammer2_inode_t *ip, hammer2_chain_t **parentp,
813                         hammer2_key_t lbase, int pblksize, int *errorp)
814 {
815         hammer2_chain_t *parent;
816         hammer2_chain_t *chain;
817         hammer2_off_t pbase;
818         hammer2_key_t key_dummy;
819         int pradix = hammer2_getradix(pblksize);
820         int cache_index = -1;
821
822         /*
823          * Locate the chain associated with lbase, return a locked chain.
824          * However, do not instantiate any data reference (which utilizes a
825          * device buffer) because we will be using direct IO via the
826          * logical buffer cache buffer.
827          */
828         *errorp = 0;
829         KKASSERT(pblksize >= HAMMER2_MIN_ALLOC);
830 retry:
831         parent = *parentp;
832         hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS); /* extra lock */
833         chain = hammer2_chain_lookup(&parent, &key_dummy,
834                                      lbase, lbase,
835                                      &cache_index, HAMMER2_LOOKUP_NODATA);
836
837         if (chain == NULL) {
838                 /*
839                  * We found a hole, create a new chain entry.
840                  *
841                  * NOTE: DATA chains are created without device backing
842                  *       store (nor do we want any).
843                  */
844                 *errorp = hammer2_chain_create(trans, &parent, &chain,
845                                                lbase, HAMMER2_PBUFRADIX,
846                                                HAMMER2_BREF_TYPE_DATA,
847                                                pblksize);
848                 if (chain == NULL) {
849                         hammer2_chain_lookup_done(parent);
850                         panic("hammer2_chain_create: par=%p error=%d\n",
851                                 parent, *errorp);
852                         goto retry;
853                 }
854
855                 pbase = chain->bref.data_off & ~HAMMER2_OFF_MASK_RADIX;
856                 /*ip->delta_dcount += pblksize;*/
857         } else {
858                 switch (chain->bref.type) {
859                 case HAMMER2_BREF_TYPE_INODE:
860                         /*
861                          * The data is embedded in the inode.  The
862                          * caller is responsible for marking the inode
863                          * modified and copying the data to the embedded
864                          * area.
865                          */
866                         pbase = NOOFFSET;
867                         break;
868                 case HAMMER2_BREF_TYPE_DATA:
869                         if (chain->bytes != pblksize) {
870                                 hammer2_chain_resize(trans, ip,
871                                                      parent, &chain,
872                                                      pradix,
873                                                      HAMMER2_MODIFY_OPTDATA);
874                         }
875                         hammer2_chain_modify(trans, &chain,
876                                              HAMMER2_MODIFY_OPTDATA);
877                         pbase = chain->bref.data_off & ~HAMMER2_OFF_MASK_RADIX;
878                         break;
879                 default:
880                         panic("hammer2_assign_physical: bad type");
881                         /* NOT REACHED */
882                         pbase = NOOFFSET;
883                         break;
884                 }
885         }
886
887         /*
888          * Cleanup.  If chain wound up being the inode (i.e. DIRECTDATA),
889          * we might have to replace *parentp.
890          */
891         hammer2_chain_lookup_done(parent);
892         if (chain) {
893                 if (*parentp != chain &&
894                     (*parentp)->core == chain->core) {
895                         parent = *parentp;
896                         *parentp = chain;               /* eats lock */
897                         hammer2_chain_unlock(parent);
898                         hammer2_chain_lock(chain, 0);   /* need another */
899                 }
900                 /* else chain already locked for return */
901         }
902         return (chain);
903 }
904
905 /* 
906  * From hammer2_vnops.c.
907  * The core write function which determines which path to take
908  * depending on compression settings.
909  */
910 static
911 void
912 hammer2_write_file_core(struct buf *bp, hammer2_trans_t *trans,
913                         hammer2_inode_t *ip, hammer2_inode_data_t *ipdata,
914                         hammer2_chain_t **parentp,
915                         hammer2_key_t lbase, int ioflag, int pblksize,
916                         int *errorp)
917 {
918         hammer2_chain_t *chain;
919
920         switch(HAMMER2_DEC_COMP(ipdata->comp_algo)) {
921         case HAMMER2_COMP_NONE:
922                 /*
923                  * We have to assign physical storage to the buffer
924                  * we intend to dirty or write now to avoid deadlocks
925                  * in the strategy code later.
926                  *
927                  * This can return NOOFFSET for inode-embedded data.
928                  * The strategy code will take care of it in that case.
929                  */
930                 chain = hammer2_assign_physical(trans, ip, parentp,
931                                                 lbase, pblksize,
932                                                 errorp);
933                 hammer2_write_bp(chain, bp, ioflag, pblksize, errorp);
934                 if (chain)
935                         hammer2_chain_unlock(chain);
936                 break;
937         case HAMMER2_COMP_AUTOZERO:
938                 /*
939                  * Check for zero-fill only
940                  */
941                 hammer2_zero_check_and_write(bp, trans, ip,
942                                     ipdata, parentp, lbase,
943                                     ioflag, pblksize, errorp);
944                 break;
945         case HAMMER2_COMP_LZ4:
946         case HAMMER2_COMP_ZLIB:
947         default:
948                 /*
949                  * Check for zero-fill and attempt compression.
950                  */
951                 hammer2_compress_and_write(bp, trans, ip,
952                                            ipdata, parentp,
953                                            lbase, ioflag,
954                                            pblksize, errorp,
955                                            ipdata->comp_algo);
956                 break;
957         }
958         ipdata = &ip->chain->data->ipdata;      /* reload */
959 }
960
961 /*
962  * From hammer2_vnops.c
963  * Generic function that will perform the compression in compression
964  * write path. The compression algorithm is determined by the settings
965  * obtained from inode.
966  */
967 static
968 void
969 hammer2_compress_and_write(struct buf *bp, hammer2_trans_t *trans,
970         hammer2_inode_t *ip, hammer2_inode_data_t *ipdata,
971         hammer2_chain_t **parentp,
972         hammer2_key_t lbase, int ioflag, int pblksize,
973         int *errorp, int comp_algo)
974 {
975         hammer2_chain_t *chain;
976         int comp_size;
977         int comp_block_size;
978         char *comp_buffer;
979
980         if (test_block_zeros(bp->b_data, pblksize)) {
981                 zero_write(bp, trans, ip, ipdata, parentp, lbase, errorp);
982                 return;
983         }
984
985         comp_size = 0;
986         comp_buffer = NULL;
987
988         KKASSERT(pblksize / 2 <= 32768);
989                 
990         if (ip->comp_heuristic < 8 || (ip->comp_heuristic & 7) == 0) {
991                 z_stream strm_compress;
992                 int comp_level;
993                 int ret;
994
995                 switch(HAMMER2_DEC_COMP(comp_algo)) {
996                 case HAMMER2_COMP_LZ4:
997                         comp_buffer = objcache_get(cache_buffer_write,
998                                                    M_INTWAIT);
999                         comp_size = LZ4_compress_limitedOutput(
1000                                         bp->b_data,
1001                                         &comp_buffer[sizeof(int)],
1002                                         pblksize,
1003                                         pblksize / 2 - sizeof(int));
1004                         /*
1005                          * We need to prefix with the size, LZ4
1006                          * doesn't do it for us.  Add the related
1007                          * overhead.
1008                          */
1009                         *(int *)comp_buffer = comp_size;
1010                         if (comp_size)
1011                                 comp_size += sizeof(int);
1012                         break;
1013                 case HAMMER2_COMP_ZLIB:
1014                         comp_level = HAMMER2_DEC_LEVEL(comp_algo);
1015                         if (comp_level == 0)
1016                                 comp_level = 6; /* default zlib compression */
1017                         else if (comp_level < 6)
1018                                 comp_level = 6;
1019                         else if (comp_level > 9)
1020                                 comp_level = 9;
1021                         ret = deflateInit(&strm_compress, comp_level);
1022                         if (ret != Z_OK) {
1023                                 kprintf("HAMMER2 ZLIB: fatal error "
1024                                         "on deflateInit.\n");
1025                         }
1026
1027                         comp_buffer = objcache_get(cache_buffer_write,
1028                                                    M_INTWAIT);
1029                         strm_compress.next_in = bp->b_data;
1030                         strm_compress.avail_in = pblksize;
1031                         strm_compress.next_out = comp_buffer;
1032                         strm_compress.avail_out = pblksize / 2;
1033                         ret = deflate(&strm_compress, Z_FINISH);
1034                         if (ret == Z_STREAM_END) {
1035                                 comp_size = pblksize / 2 -
1036                                             strm_compress.avail_out;
1037                         } else {
1038                                 comp_size = 0;
1039                         }
1040                         ret = deflateEnd(&strm_compress);
1041                         break;
1042                 default:
1043                         kprintf("Error: Unknown compression method.\n");
1044                         kprintf("Comp_method = %d.\n", comp_algo);
1045                         break;
1046                 }
1047         }
1048
1049         if (comp_size == 0) {
1050                 /*
1051                  * compression failed or turned off
1052                  */
1053                 comp_block_size = pblksize;     /* safety */
1054                 if (++ip->comp_heuristic > 128)
1055                         ip->comp_heuristic = 8;
1056         } else {
1057                 /*
1058                  * compression succeeded
1059                  */
1060                 ip->comp_heuristic = 0;
1061                 if (comp_size <= 1024) {
1062                         comp_block_size = 1024;
1063                 } else if (comp_size <= 2048) {
1064                         comp_block_size = 2048;
1065                 } else if (comp_size <= 4096) {
1066                         comp_block_size = 4096;
1067                 } else if (comp_size <= 8192) {
1068                         comp_block_size = 8192;
1069                 } else if (comp_size <= 16384) {
1070                         comp_block_size = 16384;
1071                 } else if (comp_size <= 32768) {
1072                         comp_block_size = 32768;
1073                 } else {
1074                         panic("hammer2: WRITE PATH: "
1075                               "Weird comp_size value.");
1076                         /* NOT REACHED */
1077                         comp_block_size = pblksize;
1078                 }
1079         }
1080
1081         chain = hammer2_assign_physical(trans, ip, parentp,
1082                                         lbase, comp_block_size,
1083                                         errorp);
1084         ipdata = &ip->chain->data->ipdata;      /* RELOAD */
1085
1086         if (*errorp) {
1087                 kprintf("WRITE PATH: An error occurred while "
1088                         "assigning physical space.\n");
1089                 KKASSERT(chain == NULL);
1090         } else {
1091                 /* Get device offset */
1092                 hammer2_off_t pbase;
1093                 hammer2_off_t pmask;
1094                 hammer2_off_t peof;
1095                 size_t boff;
1096                 size_t psize;
1097                 struct buf *dbp;
1098                 int temp_check;
1099
1100                 KKASSERT(chain->flags & HAMMER2_CHAIN_MODIFIED);
1101
1102                 switch(chain->bref.type) {
1103                 case HAMMER2_BREF_TYPE_INODE:
1104                         KKASSERT(chain->data->ipdata.op_flags &
1105                                  HAMMER2_OPFLAG_DIRECTDATA);
1106                         KKASSERT(bp->b_loffset == 0);
1107                         bcopy(bp->b_data, chain->data->ipdata.u.data,
1108                               HAMMER2_EMBEDDED_BYTES);
1109                         break;
1110                 case HAMMER2_BREF_TYPE_DATA:
1111                         psize = hammer2_devblksize(chain->bytes);
1112                         pmask = (hammer2_off_t)psize - 1;
1113                         pbase = chain->bref.data_off & ~pmask;
1114                         boff = chain->bref.data_off &
1115                                (HAMMER2_OFF_MASK & pmask);
1116                         peof = (pbase + HAMMER2_SEGMASK64) &
1117                                ~HAMMER2_SEGMASK64;
1118                         temp_check = HAMMER2_DEC_CHECK(chain->bref.methods);
1119
1120                         /*
1121                          * Optimize out the read-before-write
1122                          * if possible.
1123                          */
1124                         if (comp_block_size == psize) {
1125                                 dbp = getblk(chain->hmp->devvp, pbase,
1126                                              psize, 0, 0);
1127                         } else {
1128                                 *errorp = bread(chain->hmp->devvp,
1129                                                 pbase, psize, &dbp);
1130                                 if (*errorp) {
1131                                         kprintf("hammer2: WRITE PATH: "
1132                                                 "dbp bread error\n");
1133                                         break;
1134                                 }
1135                         }
1136
1137                         /*
1138                          * When loading the block make sure we don't
1139                          * leave garbage after the compressed data.
1140                          */
1141                         if (comp_size) {
1142                                 chain->bref.methods =
1143                                         HAMMER2_ENC_COMP(comp_algo) +
1144                                         HAMMER2_ENC_CHECK(temp_check);
1145                                 bcopy(comp_buffer, dbp->b_data + boff,
1146                                       comp_size);
1147                                 if (comp_size != comp_block_size) {
1148                                         bzero(dbp->b_data + boff +
1149                                                 comp_size,
1150                                               comp_block_size -
1151                                                 comp_size);
1152                                 }
1153                         } else {
1154                                 chain->bref.methods =
1155                                         HAMMER2_ENC_COMP(
1156                                                 HAMMER2_COMP_NONE) +
1157                                         HAMMER2_ENC_CHECK(temp_check);
1158                                 bcopy(bp->b_data, dbp->b_data + boff,
1159                                       pblksize);
1160                         }
1161
1162                         /*
1163                          * Device buffer is now valid, chain is no
1164                          * longer in the initial state.
1165                          */
1166                         atomic_clear_int(&chain->flags,
1167                                          HAMMER2_CHAIN_INITIAL);
1168
1169                         /* Now write the related bdp. */
1170                         if (ioflag & IO_SYNC) {
1171                                 /*
1172                                  * Synchronous I/O requested.
1173                                  */
1174                                 bwrite(dbp);
1175                         /*
1176                         } else if ((ioflag & IO_DIRECT) &&
1177                                    loff + n == pblksize) {
1178                                 bdwrite(dbp);
1179                         */
1180                         } else if (ioflag & IO_ASYNC) {
1181                                 bawrite(dbp);
1182                         } else if (hammer2_cluster_enable) {
1183                                 cluster_write(dbp, peof,
1184                                               HAMMER2_PBUFSIZE,
1185                                               4/*XXX*/);
1186                         } else {
1187                                 bdwrite(dbp);
1188                         }
1189                         break;
1190                 default:
1191                         panic("hammer2_write_bp: bad chain type %d\n",
1192                                 chain->bref.type);
1193                         /* NOT REACHED */
1194                         break;
1195                 }
1196
1197                 hammer2_chain_unlock(chain);
1198         }
1199         if (comp_buffer)
1200                 objcache_put(cache_buffer_write, comp_buffer);
1201 }
1202
1203 /*
1204  * Function that performs zero-checking and writing without compression,
1205  * it corresponds to default zero-checking path.
1206  */
1207 static
1208 void
1209 hammer2_zero_check_and_write(struct buf *bp, hammer2_trans_t *trans,
1210         hammer2_inode_t *ip, hammer2_inode_data_t *ipdata,
1211         hammer2_chain_t **parentp,
1212         hammer2_key_t lbase, int ioflag, int pblksize, int *errorp)
1213 {
1214         hammer2_chain_t *chain;
1215
1216         if (test_block_zeros(bp->b_data, pblksize)) {
1217                 zero_write(bp, trans, ip, ipdata, parentp, lbase, errorp);
1218         } else {
1219                 chain = hammer2_assign_physical(trans, ip, parentp,
1220                                                 lbase, pblksize, errorp);
1221                 hammer2_write_bp(chain, bp, ioflag, pblksize, errorp);
1222                 if (chain)
1223                         hammer2_chain_unlock(chain);
1224         }
1225 }
1226
1227 /*
1228  * A function to test whether a block of data contains only zeros,
1229  * returns TRUE (non-zero) if the block is all zeros.
1230  */
1231 static
1232 int
1233 test_block_zeros(const char *buf, size_t bytes)
1234 {
1235         size_t i;
1236
1237         for (i = 0; i < bytes; i += sizeof(long)) {
1238                 if (*(const long *)(buf + i) != 0)
1239                         return (0);
1240         }
1241         return (1);
1242 }
1243
1244 /*
1245  * Function to "write" a block that contains only zeros.
1246  */
1247 static
1248 void
1249 zero_write(struct buf *bp, hammer2_trans_t *trans, hammer2_inode_t *ip,
1250         hammer2_inode_data_t *ipdata, hammer2_chain_t **parentp,
1251         hammer2_key_t lbase, int *errorp __unused)
1252 {
1253         hammer2_chain_t *parent;
1254         hammer2_chain_t *chain;
1255         hammer2_key_t key_dummy;
1256         int cache_index = -1;
1257
1258         parent = hammer2_chain_lookup_init(*parentp, 0);
1259
1260         chain = hammer2_chain_lookup(&parent, &key_dummy, lbase, lbase,
1261                                      &cache_index, HAMMER2_LOOKUP_NODATA);
1262         if (chain) {
1263                 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE) {
1264                         bzero(chain->data->ipdata.u.data,
1265                               HAMMER2_EMBEDDED_BYTES);
1266                 } else {
1267                         hammer2_chain_delete(trans, chain, 0);
1268                 }
1269                 hammer2_chain_unlock(chain);
1270         }
1271         hammer2_chain_lookup_done(parent);
1272 }
1273
1274 /*
1275  * Function to write the data as it is, without performing any sort of
1276  * compression. This function is used in path without compression and
1277  * default zero-checking path.
1278  */
1279 static
1280 void
1281 hammer2_write_bp(hammer2_chain_t *chain, struct buf *bp, int ioflag,
1282                                 int pblksize, int *errorp)
1283 {
1284         hammer2_off_t pbase;
1285         hammer2_off_t pmask;
1286         hammer2_off_t peof;
1287         struct buf *dbp;
1288         size_t boff;
1289         size_t psize;
1290         int error;
1291         int temp_check = HAMMER2_DEC_CHECK(chain->bref.methods);
1292
1293         KKASSERT(chain->flags & HAMMER2_CHAIN_MODIFIED);
1294
1295         switch(chain->bref.type) {
1296         case HAMMER2_BREF_TYPE_INODE:
1297                 KKASSERT(chain->data->ipdata.op_flags &
1298                          HAMMER2_OPFLAG_DIRECTDATA);
1299                 KKASSERT(bp->b_loffset == 0);
1300                 bcopy(bp->b_data, chain->data->ipdata.u.data,
1301                       HAMMER2_EMBEDDED_BYTES);
1302                 error = 0;
1303                 break;
1304         case HAMMER2_BREF_TYPE_DATA:
1305                 psize = hammer2_devblksize(chain->bytes);
1306                 pmask = (hammer2_off_t)psize - 1;
1307                 pbase = chain->bref.data_off & ~pmask;
1308                 boff = chain->bref.data_off & (HAMMER2_OFF_MASK & pmask);
1309                 peof = (pbase + HAMMER2_SEGMASK64) & ~HAMMER2_SEGMASK64;
1310
1311                 if (psize == pblksize) {
1312                         dbp = getblk(chain->hmp->devvp, pbase,
1313                                      psize, 0, 0);
1314                         error = 0;
1315                 } else {
1316                         error = bread(chain->hmp->devvp, pbase, psize, &dbp);
1317                         if (error) {
1318                                 kprintf("hammer2: WRITE PATH: "
1319                                         "dbp bread error\n");
1320                                 break;
1321                         }
1322                 }
1323
1324                 chain->bref.methods = HAMMER2_ENC_COMP(HAMMER2_COMP_NONE) +
1325                                       HAMMER2_ENC_CHECK(temp_check);
1326                 bcopy(bp->b_data, dbp->b_data + boff, chain->bytes);
1327                 
1328                 /*
1329                  * Device buffer is now valid, chain is no
1330                  * longer in the initial state.
1331                  */
1332                 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
1333
1334                 if (ioflag & IO_SYNC) {
1335                         /*
1336                          * Synchronous I/O requested.
1337                          */
1338                         bwrite(dbp);
1339                 /*
1340                 } else if ((ioflag & IO_DIRECT) && loff + n == pblksize) {
1341                         bdwrite(dbp);
1342                 */
1343                 } else if (ioflag & IO_ASYNC) {
1344                         bawrite(dbp);
1345                 } else if (hammer2_cluster_enable) {
1346                         cluster_write(dbp, peof, HAMMER2_PBUFSIZE, 4/*XXX*/);
1347                 } else {
1348                         bdwrite(dbp);
1349                 }
1350                 break;
1351         default:
1352                 panic("hammer2_write_bp: bad chain type %d\n",
1353                       chain->bref.type);
1354                 /* NOT REACHED */
1355                 error = 0;
1356                 break;
1357         }
1358         *errorp = error;
1359 }
1360
1361 static
1362 int
1363 hammer2_remount(hammer2_mount_t *hmp, char *path, struct vnode *devvp,
1364                 struct ucred *cred)
1365 {
1366         return (0);
1367 }
1368
1369 static
1370 int
1371 hammer2_vfs_unmount(struct mount *mp, int mntflags)
1372 {
1373         hammer2_pfsmount_t *pmp;
1374         hammer2_mount_t *hmp;
1375         hammer2_chain_t *rchain;
1376         int flags;
1377         int error = 0;
1378         int ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
1379         int dumpcnt;
1380         int i;
1381         struct vnode *devvp;
1382
1383         pmp = MPTOPMP(mp);
1384
1385         ccms_domain_uninit(&pmp->ccms_dom);
1386         kdmsg_iocom_uninit(&pmp->iocom);        /* XXX chain dependency */
1387
1388         lockmgr(&hammer2_mntlk, LK_EXCLUSIVE);
1389
1390         /*
1391          * If mount initialization proceeded far enough we must flush
1392          * its vnodes.
1393          */
1394         if (mntflags & MNT_FORCE)
1395                 flags = FORCECLOSE;
1396         else
1397                 flags = 0;
1398         if (pmp->iroot) {
1399                 error = vflush(mp, 0, flags);
1400                 if (error)
1401                         goto failed;
1402         }
1403
1404         if (pmp->wthread_td) {
1405                 mtx_lock(&pmp->wthread_mtx);
1406                 pmp->wthread_destroy = 1;
1407                 wakeup(&pmp->wthread_bioq);
1408                 while (pmp->wthread_destroy != -1) {
1409                         mtxsleep(&pmp->wthread_destroy,
1410                                 &pmp->wthread_mtx, 0,
1411                                 "umount-sleep", 0);
1412                 }
1413                 mtx_unlock(&pmp->wthread_mtx);
1414                 pmp->wthread_td = NULL;
1415         }
1416
1417         for (i = 0; i < pmp->cluster.nchains; ++i) {
1418                 hmp = pmp->cluster.chains[i]->hmp;
1419
1420                 hammer2_mount_exlock(hmp);
1421
1422                 --hmp->pmp_count;
1423                 kprintf("hammer2_unmount hmp=%p pmpcnt=%d\n",
1424                         hmp, hmp->pmp_count);
1425
1426                 /*
1427                  * Flush any left over chains.  The voldata lock is only used
1428                  * to synchronize against HAMMER2_CHAIN_MODIFIED_AUX.
1429                  */
1430                 hammer2_voldata_lock(hmp);
1431                 if (((hmp->vchain.flags | hmp->fchain.flags) &
1432                      HAMMER2_CHAIN_MODIFIED) ||
1433                     hmp->vchain.core->update_tid > hmp->voldata.mirror_tid) {
1434                         hammer2_voldata_unlock(hmp, 0);
1435                         hammer2_vfs_sync(mp, MNT_WAIT);
1436                         hammer2_vfs_sync(mp, MNT_WAIT);
1437                 } else {
1438                         hammer2_voldata_unlock(hmp, 0);
1439                 }
1440                 if (hmp->pmp_count == 0) {
1441                         if ((hmp->vchain.flags & HAMMER2_CHAIN_MODIFIED) ||
1442                             hmp->vchain.core->update_tid >
1443                              hmp->voldata.mirror_tid) {
1444                                 kprintf("hammer2_unmount: chains left over "
1445                                         "after final sync\n");
1446                                 if (hammer2_debug & 0x0010)
1447                                         Debugger("entered debugger");
1448                         }
1449                 }
1450
1451                 /*
1452                  * Cleanup the root and super-root chain elements
1453                  * (which should be clean).
1454                  */
1455                 if (pmp->iroot) {
1456 #if REPORT_REFS_ERRORS
1457                         if (pmp->iroot->refs != 1)
1458                                 kprintf("PMP->IROOT %p REFS WRONG %d\n",
1459                                         pmp->iroot, pmp->iroot->refs);
1460 #else
1461                         KKASSERT(pmp->iroot->refs == 1);
1462 #endif
1463                         /* ref for pmp->iroot */
1464                         hammer2_inode_drop(pmp->iroot);
1465                         pmp->iroot = NULL;
1466                 }
1467
1468                 rchain = pmp->cluster.chains[i];
1469                 if (rchain) {
1470                         atomic_clear_int(&rchain->flags, HAMMER2_CHAIN_MOUNTED);
1471 #if REPORT_REFS_ERRORS
1472                         if (rchain->refs != 1)
1473                                 kprintf("PMP->RCHAIN %p REFS WRONG %d\n",
1474                                         rchain, rchain->refs);
1475 #else
1476                         KKASSERT(rchain->refs == 1);
1477 #endif
1478                         hammer2_chain_drop(rchain);
1479                         pmp->cluster.chains[i] = NULL;
1480                 }
1481
1482                 /*
1483                  * If no PFS's left drop the master hammer2_mount for the
1484                  * device.
1485                  */
1486                 if (hmp->pmp_count == 0) {
1487                         if (hmp->sroot) {
1488                                 hammer2_inode_drop(hmp->sroot);
1489                                 hmp->sroot = NULL;
1490                         }
1491
1492                         /*
1493                          * Finish up with the device vnode
1494                          */
1495                         if ((devvp = hmp->devvp) != NULL) {
1496                                 vinvalbuf(devvp, (ronly ? 0 : V_SAVE), 0, 0);
1497                                 hmp->devvp = NULL;
1498                                 VOP_CLOSE(devvp,
1499                                           (ronly ? FREAD : FREAD|FWRITE));
1500                                 vrele(devvp);
1501                                 devvp = NULL;
1502                         }
1503
1504                         /*
1505                          * Final drop of embedded freemap root chain to
1506                          * clean up fchain.core (fchain structure is not
1507                          * flagged ALLOCATED so it is cleaned out and then
1508                          * left to rot).
1509                          */
1510                         hammer2_chain_drop(&hmp->fchain);
1511
1512                         /*
1513                          * Final drop of embedded volume root chain to clean
1514                          * up vchain.core (vchain structure is not flagged
1515                          * ALLOCATED so it is cleaned out and then left to
1516                          * rot).
1517                          */
1518                         dumpcnt = 50;
1519                         hammer2_dump_chain(&hmp->vchain, 0, &dumpcnt);
1520                         dumpcnt = 50;
1521                         hammer2_dump_chain(&hmp->fchain, 0, &dumpcnt);
1522                         hammer2_mount_unlock(hmp);
1523                         hammer2_chain_drop(&hmp->vchain);
1524
1525                         TAILQ_REMOVE(&hammer2_mntlist, hmp, mntentry);
1526                         kmalloc_destroy(&hmp->mchain);
1527                         kfree(hmp, M_HAMMER2);
1528                 } else {
1529                         hammer2_mount_unlock(hmp);
1530                 }
1531         }
1532
1533         pmp->mp = NULL;
1534         mp->mnt_data = NULL;
1535
1536         kmalloc_destroy(&pmp->mmsg);
1537         kmalloc_destroy(&pmp->minode);
1538
1539         kfree(pmp, M_HAMMER2);
1540         error = 0;
1541
1542 failed:
1543         lockmgr(&hammer2_mntlk, LK_RELEASE);
1544
1545         return (error);
1546 }
1547
1548 static
1549 int
1550 hammer2_vfs_vget(struct mount *mp, struct vnode *dvp,
1551              ino_t ino, struct vnode **vpp)
1552 {
1553         kprintf("hammer2_vget\n");
1554         return (EOPNOTSUPP);
1555 }
1556
1557 static
1558 int
1559 hammer2_vfs_root(struct mount *mp, struct vnode **vpp)
1560 {
1561         hammer2_pfsmount_t *pmp;
1562         hammer2_chain_t *parent;
1563         int error;
1564         struct vnode *vp;
1565
1566         pmp = MPTOPMP(mp);
1567         if (pmp->iroot == NULL) {
1568                 *vpp = NULL;
1569                 error = EINVAL;
1570         } else {
1571                 parent = hammer2_inode_lock_sh(pmp->iroot);
1572                 vp = hammer2_igetv(pmp->iroot, &error);
1573                 hammer2_inode_unlock_sh(pmp->iroot, parent);
1574                 *vpp = vp;
1575                 if (vp == NULL)
1576                         kprintf("vnodefail\n");
1577         }
1578
1579         return (error);
1580 }
1581
1582 /*
1583  * Filesystem status
1584  *
1585  * XXX incorporate ipdata->inode_quota and data_quota
1586  */
1587 static
1588 int
1589 hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
1590 {
1591         hammer2_pfsmount_t *pmp;
1592         hammer2_mount_t *hmp;
1593
1594         pmp = MPTOPMP(mp);
1595         KKASSERT(pmp->cluster.nchains >= 1);
1596         hmp = pmp->cluster.chains[0]->hmp;      /* XXX */
1597
1598         mp->mnt_stat.f_files = pmp->inode_count;
1599         mp->mnt_stat.f_ffree = 0;
1600         mp->mnt_stat.f_blocks = hmp->voldata.allocator_size / HAMMER2_PBUFSIZE;
1601         mp->mnt_stat.f_bfree =  hmp->voldata.allocator_free / HAMMER2_PBUFSIZE;
1602         mp->mnt_stat.f_bavail = mp->mnt_stat.f_bfree;
1603
1604         *sbp = mp->mnt_stat;
1605         return (0);
1606 }
1607
1608 static
1609 int
1610 hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred)
1611 {
1612         hammer2_pfsmount_t *pmp;
1613         hammer2_mount_t *hmp;
1614
1615         pmp = MPTOPMP(mp);
1616         KKASSERT(pmp->cluster.nchains >= 1);
1617         hmp = pmp->cluster.chains[0]->hmp;      /* XXX */
1618
1619         mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE;
1620         mp->mnt_vstat.f_files = pmp->inode_count;
1621         mp->mnt_vstat.f_ffree = 0;
1622         mp->mnt_vstat.f_blocks = hmp->voldata.allocator_size / HAMMER2_PBUFSIZE;
1623         mp->mnt_vstat.f_bfree =  hmp->voldata.allocator_free / HAMMER2_PBUFSIZE;
1624         mp->mnt_vstat.f_bavail = mp->mnt_vstat.f_bfree;
1625
1626         *sbp = mp->mnt_vstat;
1627         return (0);
1628 }
1629
1630 /*
1631  * Sync the entire filesystem; this is called from the filesystem syncer
1632  * process periodically and whenever a user calls sync(1) on the hammer
1633  * mountpoint.
1634  *
1635  * Currently is actually called from the syncer! \o/
1636  *
1637  * This task will have to snapshot the state of the dirty inode chain.
1638  * From that, it will have to make sure all of the inodes on the dirty
1639  * chain have IO initiated. We make sure that io is initiated for the root
1640  * block.
1641  *
1642  * If waitfor is set, we wait for media to acknowledge the new rootblock.
1643  *
1644  * THINKS: side A vs side B, to have sync not stall all I/O?
1645  */
1646 int
1647 hammer2_vfs_sync(struct mount *mp, int waitfor)
1648 {
1649         struct hammer2_sync_info info;
1650         hammer2_chain_t *chain;
1651         hammer2_pfsmount_t *pmp;
1652         hammer2_mount_t *hmp;
1653         int flags;
1654         int error;
1655         int total_error;
1656         int force_fchain;
1657         int i;
1658
1659         pmp = MPTOPMP(mp);
1660
1661         /*
1662          * We can't acquire locks on existing vnodes while in a transaction
1663          * without risking a deadlock.  This assumes that vfsync() can be
1664          * called without the vnode locked (which it can in DragonFly).
1665          * Otherwise we'd have to implement a multi-pass or flag the lock
1666          * failures and retry.
1667          *
1668          * The reclamation code interlocks with the sync list's token
1669          * (by removing the vnode from the scan list) before unlocking
1670          * the inode, giving us time to ref the inode.
1671          *
1672          * INVFSYNC allows the bioq to drain using the flush transaction's
1673          * TID while the ISFLUSH transaction is active.
1674          */
1675         /*flags = VMSC_GETVP;*/
1676         flags = 0;
1677         if (waitfor & MNT_LAZY)
1678                 flags |= VMSC_ONEPASS;
1679
1680         hammer2_trans_init(&info.trans, pmp, HAMMER2_TRANS_ISFLUSH |
1681                                              HAMMER2_TRANS_INVFSYNC);
1682
1683         /*
1684          * vfsync the vnodes.  XXX This will also catch writes for
1685          * transactions beyond the current flush.  XXX
1686          */
1687         info.error = 0;
1688         info.waitfor = MNT_NOWAIT;
1689         vsyncscan(mp, flags | VMSC_NOWAIT, hammer2_sync_scan2, &info);
1690
1691         if (info.error == 0 && (waitfor & MNT_WAIT)) {
1692                 info.waitfor = waitfor;
1693                     vsyncscan(mp, flags, hammer2_sync_scan2, &info);
1694
1695         }
1696
1697         /*
1698          * Wait for pending work to complete, then clear INVFSYNC.  Further
1699          * buffer cache synchronization is allowed to run concurrently but
1700          * will use a higher sync_tid and is not part of the normal flush.
1701          *
1702          * These waits are important because
1703          */
1704         hammer2_trans_clear_invfsync(&info.trans);
1705
1706 #if 0
1707         if (waitfor == MNT_WAIT) {
1708                 /* XXX */
1709         } else {
1710                 /* XXX */
1711         }
1712 #endif
1713
1714         total_error = 0;
1715         for (i = 0; i < pmp->cluster.nchains; ++i) {
1716                 hmp = pmp->cluster.chains[i]->hmp;
1717
1718                 /*
1719                  * Media mounts have two 'roots', vchain for the topology
1720                  * and fchain for the free block table.  Flush both.
1721                  *
1722                  * Note that the topology and free block table are handled
1723                  * independently, so the free block table can wind up being
1724                  * ahead of the topology.  We depend on the bulk free scan
1725                  * code to deal with any loose ends.
1726                  */
1727                 hammer2_chain_lock(&hmp->vchain, HAMMER2_RESOLVE_ALWAYS);
1728                 if ((hmp->vchain.flags & HAMMER2_CHAIN_MODIFIED) ||
1729                     hmp->vchain.core->update_tid > hmp->voldata.mirror_tid) {
1730                         chain = &hmp->vchain;
1731                         hammer2_chain_flush(&info.trans, &chain);
1732                         KKASSERT(chain == &hmp->vchain);
1733                         force_fchain = 1;
1734                 } else {
1735                         force_fchain = 0;
1736                 }
1737                 hammer2_chain_unlock(&hmp->vchain);
1738
1739                 hammer2_chain_lock(&hmp->fchain, HAMMER2_RESOLVE_ALWAYS);
1740                 if ((hmp->fchain.flags & HAMMER2_CHAIN_MODIFIED) ||
1741                     hmp->vchain.core->update_tid > hmp->voldata.mirror_tid ||
1742                     force_fchain) {
1743                         /* this will also modify vchain as a side effect */
1744                         chain = &hmp->fchain;
1745                         hammer2_chain_flush(&info.trans, &chain);
1746                         KKASSERT(chain == &hmp->fchain);
1747                 }
1748                 hammer2_chain_unlock(&hmp->fchain);
1749
1750                 error = 0;
1751
1752                 /*
1753                  * We can't safely flush the volume header until we have
1754                  * flushed any device buffers which have built up.
1755                  *
1756                  * XXX this isn't being incremental
1757                  */
1758                 vn_lock(hmp->devvp, LK_EXCLUSIVE | LK_RETRY);
1759                 error = VOP_FSYNC(hmp->devvp, MNT_WAIT, 0);
1760                 vn_unlock(hmp->devvp);
1761
1762                 /*
1763                  * The flush code sets CHAIN_VOLUMESYNC to indicate that the
1764                  * volume header needs synchronization via hmp->volsync.
1765                  *
1766                  * XXX synchronize the flag & data with only this flush XXX
1767                  */
1768                 if (error == 0 &&
1769                     (hmp->vchain.flags & HAMMER2_CHAIN_VOLUMESYNC)) {
1770                         struct buf *bp;
1771
1772                         /*
1773                          * Synchronize the disk before flushing the volume
1774                          * header.
1775                          */
1776                         bp = getpbuf(NULL);
1777                         bp->b_bio1.bio_offset = 0;
1778                         bp->b_bufsize = 0;
1779                         bp->b_bcount = 0;
1780                         bp->b_cmd = BUF_CMD_FLUSH;
1781                         bp->b_bio1.bio_done = biodone_sync;
1782                         bp->b_bio1.bio_flags |= BIO_SYNC;
1783                         vn_strategy(hmp->devvp, &bp->b_bio1);
1784                         biowait(&bp->b_bio1, "h2vol");
1785                         relpbuf(bp, NULL);
1786
1787                         /*
1788                          * Then we can safely flush the version of the
1789                          * volume header synchronized by the flush code.
1790                          */
1791                         i = hmp->volhdrno + 1;
1792                         if (i >= HAMMER2_NUM_VOLHDRS)
1793                                 i = 0;
1794                         if (i * HAMMER2_ZONE_BYTES64 + HAMMER2_SEGSIZE >
1795                             hmp->volsync.volu_size) {
1796                                 i = 0;
1797                         }
1798                         kprintf("sync volhdr %d %jd\n",
1799                                 i, (intmax_t)hmp->volsync.volu_size);
1800                         bp = getblk(hmp->devvp, i * HAMMER2_ZONE_BYTES64,
1801                                     HAMMER2_PBUFSIZE, 0, 0);
1802                         atomic_clear_int(&hmp->vchain.flags,
1803                                          HAMMER2_CHAIN_VOLUMESYNC);
1804                         bcopy(&hmp->volsync, bp->b_data, HAMMER2_PBUFSIZE);
1805                         bawrite(bp);
1806                         hmp->volhdrno = i;
1807                 }
1808                 if (error)
1809                         total_error = error;
1810         }
1811
1812         hammer2_trans_done(&info.trans);
1813         return (total_error);
1814 }
1815
1816 /*
1817  * Sync passes.
1818  *
1819  * NOTE: We don't test update_tid or MOVED here because the fsync code
1820  *       won't flush on those flags.  The syncer code above will do a
1821  *       general meta-data flush globally that will catch these flags.
1822  */
1823
1824 static int
1825 hammer2_sync_scan2(struct mount *mp, struct vnode *vp, void *data)
1826 {
1827         struct hammer2_sync_info *info = data;
1828         hammer2_inode_t *ip;
1829         int error;
1830
1831         /*
1832          *
1833          */
1834         ip = VTOI(vp);
1835         if (ip == NULL)
1836                 return(0);
1837         if (vp->v_type == VNON || vp->v_type == VBAD) {
1838                 vclrisdirty(vp);
1839                 return(0);
1840         }
1841         if ((ip->flags & HAMMER2_INODE_MODIFIED) == 0 &&
1842             RB_EMPTY(&vp->v_rbdirty_tree)) {
1843                 vclrisdirty(vp);
1844                 return(0);
1845         }
1846
1847         /*
1848          * VOP_FSYNC will start a new transaction so replicate some code
1849          * here to do it inline (see hammer2_vop_fsync()).
1850          *
1851          * WARNING: The vfsync interacts with the buffer cache and might
1852          *          block, we can't hold the inode lock at that time.
1853          *          However, we MUST ref ip before blocking to ensure that
1854          *          it isn't ripped out from under us (since we do not
1855          *          hold a lock on the vnode).
1856          */
1857         hammer2_inode_ref(ip);
1858         atomic_clear_int(&ip->flags, HAMMER2_INODE_MODIFIED);
1859         if (vp)
1860                 vfsync(vp, MNT_NOWAIT, 1, NULL, NULL);
1861
1862 #if 0
1863         /*
1864          * XXX this interferes with flush operations mainly because the
1865          *     same transaction id is being used by asynchronous buffer
1866          *     operations above and can be reordered after the flush
1867          *     below.
1868          */
1869         parent = hammer2_inode_lock_ex(ip);
1870         hammer2_chain_flush(&info->trans, &parent);
1871         hammer2_inode_unlock_ex(ip, parent);
1872 #endif
1873         hammer2_inode_drop(ip);
1874         error = 0;
1875 #if 0
1876         error = VOP_FSYNC(vp, MNT_NOWAIT, 0);
1877 #endif
1878         if (error)
1879                 info->error = error;
1880         return(0);
1881 }
1882
1883 static
1884 int
1885 hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp)
1886 {
1887         return (0);
1888 }
1889
1890 static
1891 int
1892 hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
1893                struct fid *fhp, struct vnode **vpp)
1894 {
1895         return (0);
1896 }
1897
1898 static
1899 int
1900 hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
1901                  int *exflagsp, struct ucred **credanonp)
1902 {
1903         return (0);
1904 }
1905
1906 /*
1907  * Support code for hammer2_mount().  Read, verify, and install the volume
1908  * header into the HMP
1909  *
1910  * XXX read four volhdrs and use the one with the highest TID whos CRC
1911  *     matches.
1912  *
1913  * XXX check iCRCs.
1914  *
1915  * XXX For filesystems w/ less than 4 volhdrs, make sure to not write to
1916  *     nonexistant locations.
1917  *
1918  * XXX Record selected volhdr and ring updates to each of 4 volhdrs
1919  */
1920 static
1921 int
1922 hammer2_install_volume_header(hammer2_mount_t *hmp)
1923 {
1924         hammer2_volume_data_t *vd;
1925         struct buf *bp;
1926         hammer2_crc32_t crc0, crc, bcrc0, bcrc;
1927         int error_reported;
1928         int error;
1929         int valid;
1930         int i;
1931
1932         error_reported = 0;
1933         error = 0;
1934         valid = 0;
1935         bp = NULL;
1936
1937         /*
1938          * There are up to 4 copies of the volume header (syncs iterate
1939          * between them so there is no single master).  We don't trust the
1940          * volu_size field so we don't know precisely how large the filesystem
1941          * is, so depend on the OS to return an error if we go beyond the
1942          * block device's EOF.
1943          */
1944         for (i = 0; i < HAMMER2_NUM_VOLHDRS; i++) {
1945                 error = bread(hmp->devvp, i * HAMMER2_ZONE_BYTES64,
1946                               HAMMER2_VOLUME_BYTES, &bp);
1947                 if (error) {
1948                         brelse(bp);
1949                         bp = NULL;
1950                         continue;
1951                 }
1952
1953                 vd = (struct hammer2_volume_data *) bp->b_data;
1954                 if ((vd->magic != HAMMER2_VOLUME_ID_HBO) &&
1955                     (vd->magic != HAMMER2_VOLUME_ID_ABO)) {
1956                         brelse(bp);
1957                         bp = NULL;
1958                         continue;
1959                 }
1960
1961                 if (vd->magic == HAMMER2_VOLUME_ID_ABO) {
1962                         /* XXX: Reversed-endianness filesystem */
1963                         kprintf("hammer2: reverse-endian filesystem detected");
1964                         brelse(bp);
1965                         bp = NULL;
1966                         continue;
1967                 }
1968
1969                 crc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT0];
1970                 crc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC0_OFF,
1971                                       HAMMER2_VOLUME_ICRC0_SIZE);
1972                 bcrc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT1];
1973                 bcrc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC1_OFF,
1974                                        HAMMER2_VOLUME_ICRC1_SIZE);
1975                 if ((crc0 != crc) || (bcrc0 != bcrc)) {
1976                         kprintf("hammer2 volume header crc "
1977                                 "mismatch copy #%d %08x/%08x\n",
1978                                 i, crc0, crc);
1979                         error_reported = 1;
1980                         brelse(bp);
1981                         bp = NULL;
1982                         continue;
1983                 }
1984                 if (valid == 0 || hmp->voldata.mirror_tid < vd->mirror_tid) {
1985                         valid = 1;
1986                         hmp->voldata = *vd;
1987                         hmp->volhdrno = i;
1988                 }
1989                 brelse(bp);
1990                 bp = NULL;
1991         }
1992         if (valid) {
1993                 hmp->volsync = hmp->voldata;
1994                 error = 0;
1995                 if (error_reported || bootverbose || 1) { /* 1/DEBUG */
1996                         kprintf("hammer2: using volume header #%d\n",
1997                                 hmp->volhdrno);
1998                 }
1999         } else {
2000                 error = EINVAL;
2001                 kprintf("hammer2: no valid volume headers found!\n");
2002         }
2003         return (error);
2004 }
2005
2006 /*
2007  * Reconnect using the passed file pointer.  The caller must ref the
2008  * fp for us.
2009  */
2010 void
2011 hammer2_cluster_reconnect(hammer2_pfsmount_t *pmp, struct file *fp)
2012 {
2013         hammer2_inode_data_t *ipdata;
2014         hammer2_chain_t *parent;
2015         hammer2_mount_t *hmp;
2016         size_t name_len;
2017
2018         hmp = pmp->cluster.chains[0]->hmp;      /* XXX */
2019
2020         /*
2021          * Closes old comm descriptor, kills threads, cleans up
2022          * states, then installs the new descriptor and creates
2023          * new threads.
2024          */
2025         kdmsg_iocom_reconnect(&pmp->iocom, fp, "hammer2");
2026
2027         /*
2028          * Setup LNK_CONN fields for autoinitiated state machine
2029          */
2030         parent = hammer2_inode_lock_ex(pmp->iroot);
2031         ipdata = &parent->data->ipdata;
2032         pmp->iocom.auto_lnk_conn.pfs_clid = ipdata->pfs_clid;
2033         pmp->iocom.auto_lnk_conn.pfs_fsid = ipdata->pfs_fsid;
2034         pmp->iocom.auto_lnk_conn.pfs_type = ipdata->pfs_type;
2035         pmp->iocom.auto_lnk_conn.proto_version = DMSG_SPAN_PROTO_1;
2036         pmp->iocom.auto_lnk_conn.peer_type = hmp->voldata.peer_type;
2037
2038         /*
2039          * Filter adjustment.  Clients do not need visibility into other
2040          * clients (otherwise millions of clients would present a serious
2041          * problem).  The fs_label also serves to restrict the namespace.
2042          */
2043         pmp->iocom.auto_lnk_conn.peer_mask = 1LLU << HAMMER2_PEER_HAMMER2;
2044         pmp->iocom.auto_lnk_conn.pfs_mask = (uint64_t)-1;
2045         switch (ipdata->pfs_type) {
2046         case DMSG_PFSTYPE_CLIENT:
2047                 pmp->iocom.auto_lnk_conn.peer_mask &=
2048                                 ~(1LLU << DMSG_PFSTYPE_CLIENT);
2049                 break;
2050         default:
2051                 break;
2052         }
2053
2054         name_len = ipdata->name_len;
2055         if (name_len >= sizeof(pmp->iocom.auto_lnk_conn.fs_label))
2056                 name_len = sizeof(pmp->iocom.auto_lnk_conn.fs_label) - 1;
2057         bcopy(ipdata->filename,
2058               pmp->iocom.auto_lnk_conn.fs_label,
2059               name_len);
2060         pmp->iocom.auto_lnk_conn.fs_label[name_len] = 0;
2061
2062         /*
2063          * Setup LNK_SPAN fields for autoinitiated state machine
2064          */
2065         pmp->iocom.auto_lnk_span.pfs_clid = ipdata->pfs_clid;
2066         pmp->iocom.auto_lnk_span.pfs_fsid = ipdata->pfs_fsid;
2067         pmp->iocom.auto_lnk_span.pfs_type = ipdata->pfs_type;
2068         pmp->iocom.auto_lnk_span.peer_type = hmp->voldata.peer_type;
2069         pmp->iocom.auto_lnk_span.proto_version = DMSG_SPAN_PROTO_1;
2070         name_len = ipdata->name_len;
2071         if (name_len >= sizeof(pmp->iocom.auto_lnk_span.fs_label))
2072                 name_len = sizeof(pmp->iocom.auto_lnk_span.fs_label) - 1;
2073         bcopy(ipdata->filename,
2074               pmp->iocom.auto_lnk_span.fs_label,
2075               name_len);
2076         pmp->iocom.auto_lnk_span.fs_label[name_len] = 0;
2077         hammer2_inode_unlock_ex(pmp->iroot, parent);
2078
2079         kdmsg_iocom_autoinitiate(&pmp->iocom, hammer2_autodmsg);
2080 }
2081
2082 static int
2083 hammer2_rcvdmsg(kdmsg_msg_t *msg)
2084 {
2085         switch(msg->any.head.cmd & DMSGF_TRANSMASK) {
2086         case DMSG_DBG_SHELL:
2087                 /*
2088                  * (non-transaction)
2089                  * Execute shell command (not supported atm)
2090                  */
2091                 kdmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
2092                 break;
2093         case DMSG_DBG_SHELL | DMSGF_REPLY:
2094                 /*
2095                  * (non-transaction)
2096                  */
2097                 if (msg->aux_data) {
2098                         msg->aux_data[msg->aux_size - 1] = 0;
2099                         kprintf("HAMMER2 DBG: %s\n", msg->aux_data);
2100                 }
2101                 break;
2102         default:
2103                 /*
2104                  * Unsupported message received.  We only need to
2105                  * reply if it's a transaction in order to close our end.
2106                  * Ignore any one-way messages are any further messages
2107                  * associated with the transaction.
2108                  *
2109                  * NOTE: This case also includes DMSG_LNK_ERROR messages
2110                  *       which might be one-way, replying to those would
2111                  *       cause an infinite ping-pong.
2112                  */
2113                 if (msg->any.head.cmd & DMSGF_CREATE)
2114                         kdmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
2115                 break;
2116         }
2117         return(0);
2118 }
2119
2120 /*
2121  * This function is called after KDMSG has automatically handled processing
2122  * of a LNK layer message (typically CONN, SPAN, or CIRC).
2123  *
2124  * We tag off the LNK_CONN to trigger our LNK_VOLCONF messages which
2125  * advertises all available hammer2 super-root volumes.
2126  */
2127 static void
2128 hammer2_autodmsg(kdmsg_msg_t *msg)
2129 {
2130         hammer2_pfsmount_t *pmp = msg->iocom->handle;
2131         hammer2_mount_t *hmp = pmp->cluster.chains[0]->hmp; /* XXX */
2132         int copyid;
2133
2134         /*
2135          * We only care about replies to our LNK_CONN auto-request.  kdmsg
2136          * has already processed the reply, we use this calback as a shim
2137          * to know when we can advertise available super-root volumes.
2138          */
2139         if ((msg->any.head.cmd & DMSGF_TRANSMASK) !=
2140             (DMSG_LNK_CONN | DMSGF_CREATE | DMSGF_REPLY) ||
2141             msg->state == NULL) {
2142                 return;
2143         }
2144
2145         kprintf("LNK_CONN REPLY RECEIVED CMD %08x\n", msg->any.head.cmd);
2146
2147         if (msg->any.head.cmd & DMSGF_CREATE) {
2148                 kprintf("HAMMER2: VOLDATA DUMP\n");
2149
2150                 /*
2151                  * Dump the configuration stored in the volume header
2152                  */
2153                 hammer2_voldata_lock(hmp);
2154                 for (copyid = 0; copyid < HAMMER2_COPYID_COUNT; ++copyid) {
2155                         if (hmp->voldata.copyinfo[copyid].copyid == 0)
2156                                 continue;
2157                         hammer2_volconf_update(pmp, copyid);
2158                 }
2159                 hammer2_voldata_unlock(hmp, 0);
2160         }
2161         if ((msg->any.head.cmd & DMSGF_DELETE) &&
2162             msg->state && (msg->state->txcmd & DMSGF_DELETE) == 0) {
2163                 kprintf("HAMMER2: CONN WAS TERMINATED\n");
2164         }
2165 }
2166
2167 /*
2168  * Volume configuration updates are passed onto the userland service
2169  * daemon via the open LNK_CONN transaction.
2170  */
2171 void
2172 hammer2_volconf_update(hammer2_pfsmount_t *pmp, int index)
2173 {
2174         hammer2_mount_t *hmp = pmp->cluster.chains[0]->hmp;     /* XXX */
2175         kdmsg_msg_t *msg;
2176
2177         /* XXX interlock against connection state termination */
2178         kprintf("volconf update %p\n", pmp->iocom.conn_state);
2179         if (pmp->iocom.conn_state) {
2180                 kprintf("TRANSMIT VOLCONF VIA OPEN CONN TRANSACTION\n");
2181                 msg = kdmsg_msg_alloc_state(pmp->iocom.conn_state,
2182                                             DMSG_LNK_VOLCONF, NULL, NULL);
2183                 msg->any.lnk_volconf.copy = hmp->voldata.copyinfo[index];
2184                 msg->any.lnk_volconf.mediaid = hmp->voldata.fsid;
2185                 msg->any.lnk_volconf.index = index;
2186                 kdmsg_msg_write(msg);
2187         }
2188 }
2189
2190 void
2191 hammer2_dump_chain(hammer2_chain_t *chain, int tab, int *countp)
2192 {
2193         hammer2_chain_layer_t *layer;
2194         hammer2_chain_t *scan;
2195         hammer2_chain_t *first_parent;
2196
2197         --*countp;
2198         if (*countp == 0) {
2199                 kprintf("%*.*s...\n", tab, tab, "");
2200                 return;
2201         }
2202         if (*countp < 0)
2203                 return;
2204         first_parent = chain->core ? TAILQ_FIRST(&chain->core->ownerq) : NULL;
2205         kprintf("%*.*schain %p.%d %016jx/%d mir=%016jx\n",
2206                 tab, tab, "",
2207                 chain, chain->bref.type,
2208                 chain->bref.key, chain->bref.keybits,
2209                 chain->bref.mirror_tid);
2210
2211         kprintf("%*.*s      [%08x] (%s) dt=%016jx refs=%d\n",
2212                 tab, tab, "",
2213                 chain->flags,
2214                 ((chain->bref.type == HAMMER2_BREF_TYPE_INODE &&
2215                 chain->data) ?  (char *)chain->data->ipdata.filename : "?"),
2216                 chain->delete_tid,
2217                 chain->refs);
2218
2219         kprintf("%*.*s      core %p [%08x] fp=%p np=%p",
2220                 tab, tab, "",
2221                 chain->core, (chain->core ? chain->core->flags : 0),
2222                 first_parent,
2223                 (first_parent ? TAILQ_NEXT(chain, core_entry) : NULL));
2224
2225         if (first_parent)
2226                 kprintf(" [fpflags %08x fprefs %d\n",
2227                         first_parent->flags,
2228                         first_parent->refs);
2229         if (chain->core == NULL || TAILQ_EMPTY(&chain->core->layerq))
2230                 kprintf("\n");
2231         else
2232                 kprintf(" {\n");
2233         if (chain->core) {
2234                 TAILQ_FOREACH(layer, &chain->core->layerq, entry) {
2235                         RB_FOREACH(scan, hammer2_chain_tree, &layer->rbtree) {
2236                                 hammer2_dump_chain(scan, tab + 4, countp);
2237                         }
2238                 }
2239         }
2240         if (chain->core && !TAILQ_EMPTY(&chain->core->layerq)) {
2241                 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE && chain->data)
2242                         kprintf("%*.*s}(%s)\n", tab, tab, "",
2243                                 chain->data->ipdata.filename);
2244                 else
2245                         kprintf("%*.*s}\n", tab, tab, "");
2246         }
2247 }