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