<sys/cdefs.h>: Use _Static_assert for CTASSERT.
[dragonfly.git] / sys / vfs / hammer2 / hammer2_vfsops.c
CommitLineData
58e43599 1/*
8138a154 2 * Copyright (c) 2011-2014 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 77TAILQ_HEAD(hammer2_mntlist, hammer2_mount);
58e43599 78TAILQ_HEAD(hammer2_pfslist, hammer2_pfsmount);
e4e20f48 79static struct hammer2_mntlist hammer2_mntlist;
58e43599 80static struct hammer2_pfslist hammer2_pfslist;
e4e20f48
MD
81static struct lock hammer2_mntlk;
82
37aa19df 83int hammer2_debug;
a98aa0b0 84int hammer2_cluster_enable = 1;
99535653 85int hammer2_hardlink_enable = 1;
3f5b8b3b 86int hammer2_flush_pipe = 100;
052e0aa0 87int hammer2_synchronous_flush = 1;
0924b3f8 88long hammer2_limit_dirty_chains;
01eabad4
MD
89long hammer2_iod_file_read;
90long hammer2_iod_meta_read;
91long hammer2_iod_indr_read;
a98aa0b0
MD
92long hammer2_iod_fmap_read;
93long hammer2_iod_volu_read;
01eabad4
MD
94long hammer2_iod_file_write;
95long hammer2_iod_meta_write;
96long hammer2_iod_indr_write;
9061bde5 97long hammer2_iod_fmap_write;
01eabad4
MD
98long hammer2_iod_volu_write;
99long hammer2_ioa_file_read;
100long hammer2_ioa_meta_read;
101long hammer2_ioa_indr_read;
a98aa0b0
MD
102long hammer2_ioa_fmap_read;
103long hammer2_ioa_volu_read;
9061bde5 104long hammer2_ioa_fmap_write;
01eabad4
MD
105long hammer2_ioa_file_write;
106long hammer2_ioa_meta_write;
107long hammer2_ioa_indr_write;
108long hammer2_ioa_volu_write;
37aa19df 109
355d67fc
MD
110MALLOC_DECLARE(C_BUFFER);
111MALLOC_DEFINE(C_BUFFER, "compbuffer", "Buffer used for compression.");
112
113MALLOC_DECLARE(D_BUFFER);
114MALLOC_DEFINE(D_BUFFER, "decompbuffer", "Buffer used for decompression.");
115
37aa19df
MD
116SYSCTL_NODE(_vfs, OID_AUTO, hammer2, CTLFLAG_RW, 0, "HAMMER2 filesystem");
117
118SYSCTL_INT(_vfs_hammer2, OID_AUTO, debug, CTLFLAG_RW,
119 &hammer2_debug, 0, "");
01eabad4
MD
120SYSCTL_INT(_vfs_hammer2, OID_AUTO, cluster_enable, CTLFLAG_RW,
121 &hammer2_cluster_enable, 0, "");
e708f8b9
MD
122SYSCTL_INT(_vfs_hammer2, OID_AUTO, hardlink_enable, CTLFLAG_RW,
123 &hammer2_hardlink_enable, 0, "");
3f5b8b3b
MD
124SYSCTL_INT(_vfs_hammer2, OID_AUTO, flush_pipe, CTLFLAG_RW,
125 &hammer2_flush_pipe, 0, "");
8138a154
MD
126SYSCTL_INT(_vfs_hammer2, OID_AUTO, synchronous_flush, CTLFLAG_RW,
127 &hammer2_synchronous_flush, 0, "");
0924b3f8
MD
128SYSCTL_LONG(_vfs_hammer2, OID_AUTO, limit_dirty_chains, CTLFLAG_RW,
129 &hammer2_limit_dirty_chains, 0, "");
a98aa0b0 130
01eabad4
MD
131SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_read, CTLFLAG_RW,
132 &hammer2_iod_file_read, 0, "");
133SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_meta_read, CTLFLAG_RW,
134 &hammer2_iod_meta_read, 0, "");
135SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_indr_read, CTLFLAG_RW,
136 &hammer2_iod_indr_read, 0, "");
a98aa0b0
MD
137SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_fmap_read, CTLFLAG_RW,
138 &hammer2_iod_fmap_read, 0, "");
139SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_volu_read, CTLFLAG_RW,
140 &hammer2_iod_volu_read, 0, "");
141
01eabad4
MD
142SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_file_write, CTLFLAG_RW,
143 &hammer2_iod_file_write, 0, "");
144SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_meta_write, CTLFLAG_RW,
145 &hammer2_iod_meta_write, 0, "");
146SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_indr_write, CTLFLAG_RW,
147 &hammer2_iod_indr_write, 0, "");
a98aa0b0
MD
148SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_fmap_write, CTLFLAG_RW,
149 &hammer2_iod_fmap_write, 0, "");
01eabad4
MD
150SYSCTL_LONG(_vfs_hammer2, OID_AUTO, iod_volu_write, CTLFLAG_RW,
151 &hammer2_iod_volu_write, 0, "");
a98aa0b0 152
01eabad4
MD
153SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_file_read, CTLFLAG_RW,
154 &hammer2_ioa_file_read, 0, "");
155SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_meta_read, CTLFLAG_RW,
156 &hammer2_ioa_meta_read, 0, "");
157SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_indr_read, CTLFLAG_RW,
158 &hammer2_ioa_indr_read, 0, "");
a98aa0b0
MD
159SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_fmap_read, CTLFLAG_RW,
160 &hammer2_ioa_fmap_read, 0, "");
161SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_volu_read, CTLFLAG_RW,
162 &hammer2_ioa_volu_read, 0, "");
163
01eabad4
MD
164SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_file_write, CTLFLAG_RW,
165 &hammer2_ioa_file_write, 0, "");
166SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_meta_write, CTLFLAG_RW,
167 &hammer2_ioa_meta_write, 0, "");
168SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_indr_write, CTLFLAG_RW,
169 &hammer2_ioa_indr_write, 0, "");
a98aa0b0
MD
170SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_fmap_write, CTLFLAG_RW,
171 &hammer2_ioa_fmap_write, 0, "");
01eabad4
MD
172SYSCTL_LONG(_vfs_hammer2, OID_AUTO, ioa_volu_write, CTLFLAG_RW,
173 &hammer2_ioa_volu_write, 0, "");
37aa19df 174
b7926f31 175static int hammer2_vfs_init(struct vfsconf *conf);
355d67fc 176static int hammer2_vfs_uninit(struct vfsconf *vfsp);
b7926f31
MD
177static int hammer2_vfs_mount(struct mount *mp, char *path, caddr_t data,
178 struct ucred *cred);
10136ab6
MD
179static int hammer2_remount(hammer2_mount_t *, struct mount *, char *,
180 struct vnode *, struct ucred *);
181static int hammer2_recovery(hammer2_mount_t *hmp);
b7926f31
MD
182static int hammer2_vfs_unmount(struct mount *mp, int mntflags);
183static int hammer2_vfs_root(struct mount *mp, struct vnode **vpp);
184static int hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp,
703720e4 185 struct ucred *cred);
b7926f31
MD
186static int hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp,
187 struct ucred *cred);
b7926f31
MD
188static int hammer2_vfs_vget(struct mount *mp, struct vnode *dvp,
189 ino_t ino, struct vnode **vpp);
190static int hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
191 struct fid *fhp, struct vnode **vpp);
192static int hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp);
193static int hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
194 int *exflagsp, struct ucred **credanonp);
195
196static int hammer2_install_volume_header(hammer2_mount_t *hmp);
b7926f31 197static int hammer2_sync_scan2(struct mount *mp, struct vnode *vp, void *data);
50e4f8f4 198
355d67fc
MD
199static void hammer2_write_thread(void *arg);
200
623d43d4
MD
201static void hammer2_vfs_unmount_hmp1(struct mount *mp, hammer2_mount_t *hmp);
202static void hammer2_vfs_unmount_hmp2(struct mount *mp, hammer2_mount_t *hmp);
203
355d67fc
MD
204/*
205 * Functions for compression in threads,
206 * from hammer2_vnops.c
207 */
1e5c08ba 208static void hammer2_write_file_core(struct buf *bp, hammer2_trans_t *trans,
355d67fc
MD
209 hammer2_inode_t *ip,
210 hammer2_inode_data_t *ipdata,
278ab2b2 211 hammer2_cluster_t *cparent,
355d67fc
MD
212 hammer2_key_t lbase, int ioflag, int pblksize,
213 int *errorp);
1e5c08ba 214static void hammer2_compress_and_write(struct buf *bp, hammer2_trans_t *trans,
355d67fc 215 hammer2_inode_t *ip,
6a5f4fe6 216 const hammer2_inode_data_t *ipdata,
278ab2b2 217 hammer2_cluster_t *cparent,
355d67fc 218 hammer2_key_t lbase, int ioflag,
f481450f 219 int pblksize, int *errorp, int comp_algo);
1e5c08ba 220static void hammer2_zero_check_and_write(struct buf *bp,
355d67fc 221 hammer2_trans_t *trans, hammer2_inode_t *ip,
6a5f4fe6 222 const hammer2_inode_data_t *ipdata,
278ab2b2 223 hammer2_cluster_t *cparent,
355d67fc 224 hammer2_key_t lbase,
1e5c08ba 225 int ioflag, int pblksize, int *errorp);
f481450f 226static int test_block_zeros(const char *buf, size_t bytes);
1e5c08ba 227static void zero_write(struct buf *bp, hammer2_trans_t *trans,
355d67fc 228 hammer2_inode_t *ip,
6a5f4fe6 229 const hammer2_inode_data_t *ipdata,
278ab2b2 230 hammer2_cluster_t *cparent,
1e5c08ba
MD
231 hammer2_key_t lbase,
232 int *errorp);
278ab2b2 233static void hammer2_write_bp(hammer2_cluster_t *cluster, struct buf *bp,
1e5c08ba 234 int ioflag, int pblksize, int *errorp);
355d67fc 235
537d97bc
MD
236static int hammer2_rcvdmsg(kdmsg_msg_t *msg);
237static void hammer2_autodmsg(kdmsg_msg_t *msg);
bfc3a7b1 238
355d67fc 239
703720e4
MD
240/*
241 * HAMMER2 vfs operations.
242 */
243static struct vfsops hammer2_vfsops = {
066e00cc 244 .vfs_init = hammer2_vfs_init,
58e43599 245 .vfs_uninit = hammer2_vfs_uninit,
066e00cc
MD
246 .vfs_sync = hammer2_vfs_sync,
247 .vfs_mount = hammer2_vfs_mount,
248 .vfs_unmount = hammer2_vfs_unmount,
249 .vfs_root = hammer2_vfs_root,
250 .vfs_statfs = hammer2_vfs_statfs,
251 .vfs_statvfs = hammer2_vfs_statvfs,
252 .vfs_vget = hammer2_vfs_vget,
253 .vfs_vptofh = hammer2_vfs_vptofh,
254 .vfs_fhtovp = hammer2_vfs_fhtovp,
255 .vfs_checkexp = hammer2_vfs_checkexp
703720e4
MD
256};
257
703720e4
MD
258MALLOC_DEFINE(M_HAMMER2, "HAMMER2-mount", "");
259
260VFS_SET(hammer2_vfsops, hammer2, 0);
261MODULE_VERSION(hammer2, 1);
262
e118c14f
MD
263static
264int
066e00cc 265hammer2_vfs_init(struct vfsconf *conf)
703720e4 266{
355d67fc
MD
267 static struct objcache_malloc_args margs_read;
268 static struct objcache_malloc_args margs_write;
269
703720e4
MD
270 int error;
271
272 error = 0;
273
0e92b724 274 if (HAMMER2_BLOCKREF_BYTES != sizeof(struct hammer2_blockref))
703720e4 275 error = EINVAL;
0e92b724 276 if (HAMMER2_INODE_BYTES != sizeof(struct hammer2_inode_data))
703720e4 277 error = EINVAL;
0e92b724 278 if (HAMMER2_VOLUME_BYTES != sizeof(struct hammer2_volume_data))
703720e4
MD
279 error = EINVAL;
280
281 if (error)
282 kprintf("HAMMER2 structure size mismatch; cannot continue.\n");
355d67fc
MD
283
284 margs_read.objsize = 65536;
285 margs_read.mtype = D_BUFFER;
286
287 margs_write.objsize = 32768;
288 margs_write.mtype = C_BUFFER;
289
290 cache_buffer_read = objcache_create(margs_read.mtype->ks_shortdesc,
291 0, 1, NULL, NULL, NULL, objcache_malloc_alloc,
292 objcache_malloc_free, &margs_read);
293 cache_buffer_write = objcache_create(margs_write.mtype->ks_shortdesc,
294 0, 1, NULL, NULL, NULL, objcache_malloc_alloc,
295 objcache_malloc_free, &margs_write);
703720e4 296
e4e20f48
MD
297 lockinit(&hammer2_mntlk, "mntlk", 0, 0);
298 TAILQ_INIT(&hammer2_mntlist);
58e43599 299 TAILQ_INIT(&hammer2_pfslist);
e4e20f48 300
0924b3f8
MD
301 hammer2_limit_dirty_chains = desiredvnodes / 10;
302
703720e4
MD
303 return (error);
304}
305
355d67fc
MD
306static
307int
308hammer2_vfs_uninit(struct vfsconf *vfsp __unused)
309{
310 objcache_destroy(cache_buffer_read);
311 objcache_destroy(cache_buffer_write);
312 return 0;
313}
314
703720e4
MD
315/*
316 * Mount or remount HAMMER2 fileystem from physical media
317 *
318 * mountroot
319 * mp mount point structure
320 * path NULL
321 * data <unused>
322 * cred <unused>
323 *
324 * mount
325 * mp mount point structure
326 * path path to mount point
327 * data pointer to argument structure in user space
328 * volume volume path (device@LABEL form)
329 * hflags user mount flags
330 * cred user credentials
331 *
332 * RETURNS: 0 Success
333 * !0 error number
334 */
e118c14f
MD
335static
336int
066e00cc 337hammer2_vfs_mount(struct mount *mp, char *path, caddr_t data,
bfc3a7b1 338 struct ucred *cred)
703720e4
MD
339{
340 struct hammer2_mount_info info;
e4e20f48 341 hammer2_pfsmount_t *pmp;
54eb943b 342 hammer2_mount_t *hmp;
1897c66e
MD
343 hammer2_key_t key_next;
344 hammer2_key_t key_dummy;
7cfa8da5 345 hammer2_key_t lhc;
703720e4
MD
346 struct vnode *devvp;
347 struct nlookupdata nd;
5c23d7f1 348 hammer2_chain_t *parent;
7cfa8da5 349 hammer2_chain_t *rchain;
278ab2b2
MD
350 hammer2_chain_t *schain;
351 hammer2_cluster_t *cluster;
352 hammer2_cluster_t *cparent;
58e43599 353 const hammer2_inode_data_t *ipdata;
3a5aa68f 354 struct file *fp;
703720e4
MD
355 char devstr[MNAMELEN];
356 size_t size;
357 size_t done;
50e4f8f4
MD
358 char *dev;
359 char *label;
54eb943b 360 int ronly = 1;
703720e4 361 int error;
1897c66e 362 int cache_index;
278ab2b2 363 int ddflag;
7bed8d7e 364 int i;
703720e4
MD
365
366 hmp = NULL;
e4e20f48 367 pmp = NULL;
50e4f8f4
MD
368 dev = NULL;
369 label = NULL;
703720e4 370 devvp = NULL;
1897c66e 371 cache_index = -1;
703720e4
MD
372
373 kprintf("hammer2_mount\n");
374
375 if (path == NULL) {
376 /*
377 * Root mount
378 */
bfc3a7b1
MD
379 bzero(&info, sizeof(info));
380 info.cluster_fd = -1;
703720e4
MD
381 return (EOPNOTSUPP);
382 } else {
383 /*
384 * Non-root mount or updating a mount
385 */
703720e4
MD
386 error = copyin(data, &info, sizeof(info));
387 if (error)
388 return (error);
389
390 error = copyinstr(info.volume, devstr, MNAMELEN - 1, &done);
391 if (error)
392 return (error);
393
394 /* Extract device and label */
395 dev = devstr;
396 label = strchr(devstr, '@');
397 if (label == NULL ||
50e4f8f4 398 ((label + 1) - dev) > done) {
703720e4 399 return (EINVAL);
50e4f8f4 400 }
703720e4
MD
401 *label = '\0';
402 label++;
403 if (*label == '\0')
404 return (EINVAL);
405
406 if (mp->mnt_flag & MNT_UPDATE) {
407 /* Update mount */
408 /* HAMMER2 implements NFS export via mountctl */
7bed8d7e 409 pmp = MPTOPMP(mp);
58e43599
MD
410 for (i = 0; i < pmp->iroot->cluster.nchains; ++i) {
411 hmp = pmp->iroot->cluster.array[i]->hmp;
7bed8d7e 412 devvp = hmp->devvp;
10136ab6
MD
413 error = hammer2_remount(hmp, mp, path,
414 devvp, cred);
7bed8d7e
MD
415 if (error)
416 break;
417 }
044541cd
MD
418 hammer2_inode_install_hidden(pmp);
419
54eb943b 420 return error;
703720e4
MD
421 }
422 }
423
424 /*
58e43599 425 * HMP device mount
bfc3a7b1
MD
426 *
427 * Lookup name and verify it refers to a block device.
703720e4 428 */
703720e4 429 error = nlookup_init(&nd, dev, UIO_SYSSPACE, NLC_FOLLOW);
54eb943b
MD
430 if (error == 0)
431 error = nlookup(&nd);
432 if (error == 0)
433 error = cache_vref(&nd.nl_nch, nd.nl_cred, &devvp);
703720e4
MD
434 nlookup_done(&nd);
435
54eb943b
MD
436 if (error == 0) {
437 if (vn_isdisk(devvp, &error))
438 error = vfs_mountedon(devvp);
703720e4
MD
439 }
440
441 /*
e4e20f48
MD
442 * Determine if the device has already been mounted. After this
443 * check hmp will be non-NULL if we are doing the second or more
444 * hammer2 mounts from the same device.
703720e4 445 */
e4e20f48
MD
446 lockmgr(&hammer2_mntlk, LK_EXCLUSIVE);
447 TAILQ_FOREACH(hmp, &hammer2_mntlist, mntentry) {
448 if (hmp->devvp == devvp)
449 break;
450 }
451
452 /*
a5913bdf
MD
453 * Open the device if this isn't a secondary mount and construct
454 * the H2 device mount (hmp).
e4e20f48 455 */
a5913bdf 456 if (hmp == NULL) {
e4e20f48
MD
457 if (error == 0 && vcount(devvp) > 0)
458 error = EBUSY;
459
460 /*
461 * Now open the device
462 */
54eb943b 463 if (error == 0) {
e4e20f48
MD
464 ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
465 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
466 error = vinvalbuf(devvp, V_SAVE, 0, 0);
467 if (error == 0) {
468 error = VOP_OPEN(devvp,
469 ronly ? FREAD : FREAD | FWRITE,
470 FSCRED, NULL);
471 }
472 vn_unlock(devvp);
473 }
474 if (error && devvp) {
475 vrele(devvp);
476 devvp = NULL;
477 }
478 if (error) {
479 lockmgr(&hammer2_mntlk, LK_RELEASE);
480 return error;
54eb943b 481 }
e4e20f48
MD
482 hmp = kmalloc(sizeof(*hmp), M_HAMMER2, M_WAITOK | M_ZERO);
483 hmp->ronly = ronly;
484 hmp->devvp = devvp;
e4e20f48
MD
485 kmalloc_create(&hmp->mchain, "HAMMER2-chains");
486 TAILQ_INSERT_TAIL(&hammer2_mntlist, hmp, mntentry);
fdf62707 487 RB_INIT(&hmp->iotree);
50e4f8f4 488
0dea3156
MD
489 lockinit(&hmp->alloclk, "h2alloc", 0, 0);
490 lockinit(&hmp->voldatalk, "voldata", 0, LK_CANRECURSE);
d001f460 491 TAILQ_INIT(&hmp->transq);
0dea3156 492
e4e20f48 493 /*
1a7cfe5a 494 * vchain setup. vchain.data is embedded.
e4e20f48 495 * vchain.refs is initialized and will never drop to 0.
10136ab6
MD
496 *
497 * NOTE! voldata is not yet loaded.
e4e20f48 498 */
0dea3156 499 hmp->vchain.hmp = hmp;
e4e20f48
MD
500 hmp->vchain.refs = 1;
501 hmp->vchain.data = (void *)&hmp->voldata;
004f88b4 502 hmp->vchain.bref.type = HAMMER2_BREF_TYPE_VOLUME;
e4e20f48 503 hmp->vchain.bref.data_off = 0 | HAMMER2_PBUFRADIX;
731b2a84 504 hmp->vchain.delete_tid = HAMMER2_MAX_TID;
10136ab6 505
1897c66e 506 hammer2_chain_core_alloc(NULL, &hmp->vchain, NULL);
e4e20f48 507 /* hmp->vchain.u.xxx is left NULL */
232a50f9 508
1a7cfe5a
MD
509 /*
510 * fchain setup. fchain.data is embedded.
511 * fchain.refs is initialized and will never drop to 0.
512 *
513 * The data is not used but needs to be initialized to
514 * pass assertion muster. We use this chain primarily
515 * as a placeholder for the freemap's top-level RBTREE
516 * so it does not interfere with the volume's topology
517 * RBTREE.
518 */
519 hmp->fchain.hmp = hmp;
520 hmp->fchain.refs = 1;
521 hmp->fchain.data = (void *)&hmp->voldata.freemap_blockset;
522 hmp->fchain.bref.type = HAMMER2_BREF_TYPE_FREEMAP;
523 hmp->fchain.bref.data_off = 0 | HAMMER2_PBUFRADIX;
512beabd
MD
524 hmp->fchain.bref.methods =
525 HAMMER2_ENC_CHECK(HAMMER2_CHECK_FREEMAP) |
526 HAMMER2_ENC_COMP(HAMMER2_COMP_NONE);
1a7cfe5a 527 hmp->fchain.delete_tid = HAMMER2_MAX_TID;
512beabd 528
1897c66e 529 hammer2_chain_core_alloc(NULL, &hmp->fchain, NULL);
1a7cfe5a
MD
530 /* hmp->fchain.u.xxx is left NULL */
531
e4e20f48 532 /*
10136ab6
MD
533 * Install the volume header and initialize fields from
534 * voldata.
e4e20f48
MD
535 */
536 error = hammer2_install_volume_header(hmp);
537 if (error) {
623d43d4
MD
538 ++hmp->pmp_count;
539 hammer2_vfs_unmount_hmp1(mp, hmp);
540 hammer2_vfs_unmount_hmp2(mp, hmp);
e4e20f48
MD
541 hammer2_vfs_unmount(mp, MNT_FORCE);
542 return error;
543 }
a5913bdf 544
8138a154
MD
545 /*
546 * Really important to get these right or flush will get
547 * confused.
548 */
a4dc31e0 549 hmp->vchain.bref.mirror_tid = hmp->voldata.mirror_tid;
10136ab6 550 hmp->vchain.modify_tid = hmp->voldata.mirror_tid;
8138a154 551 hmp->vchain.update_lo = hmp->voldata.mirror_tid;
a4dc31e0 552 hmp->fchain.bref.mirror_tid = hmp->voldata.freemap_tid;
10136ab6 553 hmp->fchain.modify_tid = hmp->voldata.freemap_tid;
8138a154 554 hmp->fchain.update_lo = hmp->voldata.freemap_tid;
10136ab6 555
a5913bdf
MD
556 /*
557 * First locate the super-root inode, which is key 0
558 * relative to the volume header's blockset.
559 *
560 * Then locate the root inode by scanning the directory keyspace
561 * represented by the label.
562 */
563 parent = hammer2_chain_lookup_init(&hmp->vchain, 0);
1897c66e
MD
564 schain = hammer2_chain_lookup(&parent, &key_dummy,
565 HAMMER2_SROOT_KEY, HAMMER2_SROOT_KEY,
278ab2b2 566 &cache_index, 0, &ddflag);
a5913bdf
MD
567 hammer2_chain_lookup_done(parent);
568 if (schain == NULL) {
569 kprintf("hammer2_mount: invalid super-root\n");
623d43d4
MD
570 ++hmp->pmp_count;
571 hammer2_vfs_unmount_hmp1(mp, hmp);
572 hammer2_vfs_unmount_hmp2(mp, hmp);
a5913bdf
MD
573 hammer2_vfs_unmount(mp, MNT_FORCE);
574 return EINVAL;
575 }
7bed8d7e
MD
576
577 /*
578 * NOTE: inode_get sucks up schain's lock.
579 */
580 atomic_set_int(&schain->flags, HAMMER2_CHAIN_PFSROOT);
278ab2b2
MD
581 cluster = hammer2_cluster_from_chain(schain);
582 hmp->sroot = hammer2_inode_get(NULL, NULL, cluster);
7bed8d7e 583 hammer2_inode_ref(hmp->sroot);
278ab2b2 584 hammer2_inode_unlock_ex(hmp->sroot, cluster);
a5913bdf 585 schain = NULL;
7bed8d7e 586 /* leave hmp->sroot with one ref */
10136ab6
MD
587
588 if ((mp->mnt_flag & MNT_RDONLY) == 0) {
589 error = hammer2_recovery(hmp);
590 /* XXX do something with error */
591 }
50e4f8f4 592 }
a5913bdf 593 ++hmp->pmp_count;
5c23d7f1 594
10252dc7 595 /*
7bed8d7e 596 * Lookup mount point under the media-localized super-root.
10252dc7 597 */
278ab2b2 598 cparent = hammer2_inode_lock_ex(hmp->sroot);
e4e20f48 599 lhc = hammer2_dirhash(label, strlen(label));
278ab2b2 600 cluster = hammer2_cluster_lookup(cparent, &key_next,
c667909f 601 lhc, lhc + HAMMER2_DIRHASH_LOMASK,
278ab2b2
MD
602 0, &ddflag);
603 while (cluster) {
604 if (hammer2_cluster_type(cluster) == HAMMER2_BREF_TYPE_INODE &&
605 strcmp(label,
606 hammer2_cluster_data(cluster)->ipdata.filename) == 0) {
7cfa8da5
MD
607 break;
608 }
278ab2b2 609 cluster = hammer2_cluster_next(cparent, cluster, &key_next,
1897c66e 610 key_next,
278ab2b2 611 lhc + HAMMER2_DIRHASH_LOMASK, 0);
7cfa8da5 612 }
278ab2b2 613 hammer2_inode_unlock_ex(hmp->sroot, cparent);
7bed8d7e 614
278ab2b2 615 if (cluster == NULL) {
e4e20f48 616 kprintf("hammer2_mount: PFS label not found\n");
623d43d4
MD
617 hammer2_vfs_unmount_hmp1(mp, hmp);
618 hammer2_vfs_unmount_hmp2(mp, hmp);
58e43599 619 lockmgr(&hammer2_mntlk, LK_RELEASE);
9c2e0de0 620 hammer2_vfs_unmount(mp, MNT_FORCE);
7cfa8da5
MD
621 return EINVAL;
622 }
278ab2b2
MD
623
624 for (i = 0; i < cluster->nchains; ++i) {
625 rchain = cluster->array[i];
626 if (rchain->flags & HAMMER2_CHAIN_MOUNTED) {
627 kprintf("hammer2_mount: PFS label already mounted!\n");
628 hammer2_cluster_unlock(cluster);
629 hammer2_vfs_unmount_hmp1(mp, hmp);
630 hammer2_vfs_unmount_hmp2(mp, hmp);
58e43599 631 lockmgr(&hammer2_mntlk, LK_RELEASE);
278ab2b2
MD
632 hammer2_vfs_unmount(mp, MNT_FORCE);
633 return EBUSY;
634 }
9b6b3df4 635#if 0
278ab2b2
MD
636 if (rchain->flags & HAMMER2_CHAIN_RECYCLE) {
637 kprintf("hammer2_mount: PFS label is recycling\n");
638 hammer2_cluster_unlock(cluster);
639 hammer2_vfs_unmount_hmp1(mp, hmp);
640 hammer2_vfs_unmount_hmp2(mp, hmp);
58e43599 641 lockmgr(&hammer2_mntlk, LK_RELEASE);
278ab2b2
MD
642 hammer2_vfs_unmount(mp, MNT_FORCE);
643 return EBUSY;
644 }
9b6b3df4 645#endif
278ab2b2
MD
646 }
647
58e43599
MD
648 /*
649 * Check to see if the cluster id is already mounted at the mount
650 * point. If it is, add us to the cluster.
651 */
652 ipdata = &hammer2_cluster_data(cluster)->ipdata;
653 TAILQ_FOREACH(pmp, &hammer2_pfslist, mntentry) {
654 if (bcmp(&pmp->pfs_clid, &ipdata->pfs_clid,
655 sizeof(pmp->pfs_clid)) == 0)
656 break;
657 }
658 if (pmp) {
659 int i;
660 int j;
661
662 hammer2_inode_ref(pmp->iroot);
663 ccms_thread_lock(&pmp->iroot->topo_cst, CCMS_STATE_EXCLUSIVE);
664
665 if (pmp->iroot->cluster.nchains + cluster->nchains >
666 HAMMER2_MAXCLUSTER) {
667 kprintf("hammer2_mount: cluster full!\n");
668
669 ccms_thread_unlock(&pmp->iroot->topo_cst);
670 hammer2_inode_drop(pmp->iroot);
671
672 hammer2_cluster_unlock(cluster);
673 hammer2_vfs_unmount_hmp1(mp, hmp);
674 hammer2_vfs_unmount_hmp2(mp, hmp);
675 lockmgr(&hammer2_mntlk, LK_RELEASE);
676 hammer2_vfs_unmount(mp, MNT_FORCE);
677 return EBUSY;
678 }
679 kprintf("hammer2_vfs_mount: Adding pfs to existing cluster\n");
680 j = pmp->iroot->cluster.nchains;
681 for (i = 0; i < cluster->nchains; ++i) {
682 hammer2_chain_ref(cluster->array[i]);
683 pmp->iroot->cluster.array[j] = cluster->array[i];
684 ++j;
685 }
686 pmp->iroot->cluster.nchains = j;
687 ccms_thread_unlock(&pmp->iroot->topo_cst);
688 hammer2_inode_drop(pmp->iroot);
689 hammer2_cluster_unlock(cluster);
690 lockmgr(&hammer2_mntlk, LK_RELEASE);
691
692 kprintf("ok\n");
693
694 return ERANGE;
695 }
696
697 /*
698 * Block device opened successfully, finish initializing the
699 * mount structure.
700 *
701 * From this point on we have to call hammer2_unmount() on failure.
702 */
703 pmp = kmalloc(sizeof(*pmp), M_HAMMER2, M_WAITOK | M_ZERO);
704
705 kmalloc_create(&pmp->minode, "HAMMER2-inodes");
706 kmalloc_create(&pmp->mmsg, "HAMMER2-pfsmsg");
707 lockinit(&pmp->lock, "pfslk", 0, 0);
708 spin_init(&pmp->inum_spin);
709 RB_INIT(&pmp->inum_tree);
710 TAILQ_INIT(&pmp->unlinkq);
711 spin_init(&pmp->unlinkq_spin);
712 pmp->pfs_clid = ipdata->pfs_clid;
713
714 kdmsg_iocom_init(&pmp->iocom, pmp,
715 KDMSG_IOCOMF_AUTOCONN |
716 KDMSG_IOCOMF_AUTOSPAN |
717 KDMSG_IOCOMF_AUTOCIRC,
718 pmp->mmsg, hammer2_rcvdmsg);
719
720 ccms_domain_init(&pmp->ccms_dom);
721 TAILQ_INSERT_TAIL(&hammer2_pfslist, pmp, mntentry);
722 lockmgr(&hammer2_mntlk, LK_RELEASE);
723
724 kprintf("hammer2_mount hmp=%p pmp=%p pmpcnt=%d\n",
725 hmp, pmp, hmp->pmp_count);
726
727 mp->mnt_flag = MNT_LOCAL;
728 mp->mnt_kern_flag |= MNTK_ALL_MPSAFE; /* all entry pts are SMP */
729 mp->mnt_kern_flag |= MNTK_THR_SYNC; /* new vsyncscan semantics */
730
731 /*
732 * required mount structure initializations
733 */
734 mp->mnt_stat.f_iosize = HAMMER2_PBUFSIZE;
735 mp->mnt_stat.f_bsize = HAMMER2_PBUFSIZE;
736
737 mp->mnt_vstat.f_frsize = HAMMER2_PBUFSIZE;
738 mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE;
739
740 /*
741 * Optional fields
742 */
743 mp->mnt_iosize_max = MAXPHYS;
744 mp->mnt_data = (qaddr_t)pmp;
745 pmp->mp = mp;
746
623d43d4
MD
747 /*
748 * After this point hammer2_vfs_unmount() has visibility on hmp
749 * and manual hmp1/hmp2 calls are not needed on fatal errors.
750 */
278ab2b2
MD
751 for (i = 0; i < cluster->nchains; ++i) {
752 rchain = cluster->array[i];
753 KKASSERT(rchain->pmp == NULL); /* tracking pmp for rchain */
754 rchain->pmp = pmp;
755 atomic_set_int(&rchain->flags, HAMMER2_CHAIN_MOUNTED);
278ab2b2
MD
756 }
757 pmp->iroot = hammer2_inode_get(pmp, NULL, cluster);
476d2aad 758 hammer2_inode_ref(pmp->iroot); /* ref for pmp->iroot */
278ab2b2 759 hammer2_inode_unlock_ex(pmp->iroot, cluster);
609a8021 760
065f4046
MD
761 /*
762 * The logical file buffer bio write thread handles things
763 * like physical block assignment and compression.
764 */
765 mtx_init(&pmp->wthread_mtx);
766 bioq_init(&pmp->wthread_bioq);
767 pmp->wthread_destroy = 0;
768 lwkt_create(hammer2_write_thread, pmp,
769 &pmp->wthread_td, NULL, 0, -1, "hwrite-%s", label);
770
bfc3a7b1
MD
771 /*
772 * Ref the cluster management messaging descriptor. The mount
773 * program deals with the other end of the communications pipe.
774 */
3a5aa68f
MD
775 fp = holdfp(curproc->p_fd, info.cluster_fd, -1);
776 if (fp == NULL) {
bfc3a7b1
MD
777 kprintf("hammer2_mount: bad cluster_fd!\n");
778 hammer2_vfs_unmount(mp, MNT_FORCE);
779 return EBADF;
780 }
3a5aa68f 781 hammer2_cluster_reconnect(pmp, fp);
bfc3a7b1 782
044541cd
MD
783 /*
784 * With the cluster operational install ihidden.
785 */
786 hammer2_inode_install_hidden(pmp);
787
bfc3a7b1
MD
788 /*
789 * Finish setup
790 */
f0206a67
VS
791 vfs_getnewfsid(mp);
792 vfs_add_vnodeops(mp, &hammer2_vnode_vops, &mp->mnt_vn_norm_ops);
793 vfs_add_vnodeops(mp, &hammer2_spec_vops, &mp->mnt_vn_spec_ops);
794 vfs_add_vnodeops(mp, &hammer2_fifo_vops, &mp->mnt_vn_fifo_ops);
703720e4 795
54f522df 796 copyinstr(info.volume, mp->mnt_stat.f_mntfromname, MNAMELEN - 1, &size);
703720e4
MD
797 bzero(mp->mnt_stat.f_mntfromname + size, MNAMELEN - size);
798 bzero(mp->mnt_stat.f_mntonname, sizeof(mp->mnt_stat.f_mntonname));
799 copyinstr(path, mp->mnt_stat.f_mntonname,
800 sizeof(mp->mnt_stat.f_mntonname) - 1,
801 &size);
802
bfc3a7b1
MD
803 /*
804 * Initial statfs to prime mnt_stat.
805 */
066e00cc 806 hammer2_vfs_statfs(mp, &mp->mnt_stat, cred);
355d67fc 807
47902fef 808 return 0;
703720e4
MD
809}
810
355d67fc
MD
811/*
812 * Handle bioq for strategy write
813 */
814static
815void
816hammer2_write_thread(void *arg)
817{
065f4046 818 hammer2_pfsmount_t *pmp;
355d67fc
MD
819 struct bio *bio;
820 struct buf *bp;
821 hammer2_trans_t trans;
822 struct vnode *vp;
355d67fc 823 hammer2_inode_t *ip;
278ab2b2 824 hammer2_cluster_t *cparent;
6a5f4fe6 825 hammer2_inode_data_t *wipdata;
355d67fc
MD
826 hammer2_key_t lbase;
827 int lblksize;
828 int pblksize;
829 int error;
830
065f4046 831 pmp = arg;
355d67fc 832
065f4046
MD
833 mtx_lock(&pmp->wthread_mtx);
834 while (pmp->wthread_destroy == 0) {
835 if (bioq_first(&pmp->wthread_bioq) == NULL) {
836 mtxsleep(&pmp->wthread_bioq, &pmp->wthread_mtx,
355d67fc
MD
837 0, "h2bioqw", 0);
838 }
278ab2b2 839 cparent = NULL;
355d67fc 840
10136ab6 841 hammer2_trans_init(&trans, pmp, NULL, HAMMER2_TRANS_BUFCACHE);
a7720be7 842
065f4046 843 while ((bio = bioq_takefirst(&pmp->wthread_bioq)) != NULL) {
a7720be7 844 /*
a4dc31e0
MD
845 * dummy bio for synchronization. The transaction
846 * must be reinitialized.
a7720be7
MD
847 */
848 if (bio->bio_buf == NULL) {
849 bio->bio_flags |= BIO_DONE;
850 wakeup(bio);
a4dc31e0 851 hammer2_trans_done(&trans);
10136ab6 852 hammer2_trans_init(&trans, pmp, NULL,
a4dc31e0 853 HAMMER2_TRANS_BUFCACHE);
a7720be7
MD
854 continue;
855 }
856
857 /*
858 * else normal bio processing
859 */
065f4046 860 mtx_unlock(&pmp->wthread_mtx);
3f5b8b3b
MD
861
862 hammer2_lwinprog_drop(pmp);
355d67fc
MD
863
864 error = 0;
865 bp = bio->bio_buf;
866 vp = bp->b_vp;
867 ip = VTOI(vp);
868
355d67fc
MD
869 /*
870 * Inode is modified, flush size and mtime changes
871 * to ensure that the file size remains consistent
872 * with the buffers being flushed.
72ebfa75
MD
873 *
874 * NOTE: The inode_fsync() call only flushes the
875 * inode's meta-data state, it doesn't try
876 * to flush underlying buffers or chains.
355d67fc 877 */
278ab2b2 878 cparent = hammer2_inode_lock_ex(ip);
355d67fc
MD
879 if (ip->flags & (HAMMER2_INODE_RESIZED |
880 HAMMER2_INODE_MTIME)) {
278ab2b2 881 hammer2_inode_fsync(&trans, ip, cparent);
355d67fc 882 }
6a5f4fe6 883 wipdata = hammer2_cluster_modify_ip(&trans, ip,
278ab2b2 884 cparent, 0);
355d67fc
MD
885 lblksize = hammer2_calc_logical(ip, bio->bio_offset,
886 &lbase, NULL);
6a5f4fe6
MD
887 pblksize = hammer2_calc_physical(ip, wipdata, lbase);
888 hammer2_write_file_core(bp, &trans, ip, wipdata,
278ab2b2 889 cparent,
355d67fc
MD
890 lbase, IO_ASYNC,
891 pblksize, &error);
6a5f4fe6 892 hammer2_cluster_modsync(cparent);
278ab2b2 893 hammer2_inode_unlock_ex(ip, cparent);
355d67fc 894 if (error) {
1e5c08ba
MD
895 kprintf("hammer2: error in buffer write\n");
896 bp->b_flags |= B_ERROR;
897 bp->b_error = EIO;
355d67fc
MD
898 }
899 biodone(bio);
065f4046 900 mtx_lock(&pmp->wthread_mtx);
355d67fc 901 }
a7720be7 902 hammer2_trans_done(&trans);
355d67fc 903 }
065f4046
MD
904 pmp->wthread_destroy = -1;
905 wakeup(&pmp->wthread_destroy);
355d67fc 906
065f4046 907 mtx_unlock(&pmp->wthread_mtx);
355d67fc
MD
908}
909
a7720be7
MD
910void
911hammer2_bioq_sync(hammer2_pfsmount_t *pmp)
912{
913 struct bio sync_bio;
914
915 bzero(&sync_bio, sizeof(sync_bio)); /* dummy with no bio_buf */
916 mtx_lock(&pmp->wthread_mtx);
72ebfa75
MD
917 if (pmp->wthread_destroy == 0 &&
918 TAILQ_FIRST(&pmp->wthread_bioq.queue)) {
919 bioq_insert_tail(&pmp->wthread_bioq, &sync_bio);
a7720be7
MD
920 while ((sync_bio.bio_flags & BIO_DONE) == 0)
921 mtxsleep(&sync_bio, &pmp->wthread_mtx, 0, "h2bioq", 0);
922 }
923 mtx_unlock(&pmp->wthread_mtx);
924}
925
355d67fc 926/*
51a0d27c
MD
927 * Return a chain suitable for I/O, creating the chain if necessary
928 * and assigning its physical block.
355d67fc
MD
929 */
930static
278ab2b2 931hammer2_cluster_t *
355d67fc 932hammer2_assign_physical(hammer2_trans_t *trans,
278ab2b2 933 hammer2_inode_t *ip, hammer2_cluster_t *cparent,
355d67fc
MD
934 hammer2_key_t lbase, int pblksize, int *errorp)
935{
278ab2b2 936 hammer2_cluster_t *cluster;
84e47819 937 hammer2_cluster_t *dparent;
1897c66e 938 hammer2_key_t key_dummy;
355d67fc 939 int pradix = hammer2_getradix(pblksize);
278ab2b2 940 int ddflag;
355d67fc
MD
941
942 /*
943 * Locate the chain associated with lbase, return a locked chain.
944 * However, do not instantiate any data reference (which utilizes a
945 * device buffer) because we will be using direct IO via the
946 * logical buffer cache buffer.
947 */
948 *errorp = 0;
51a0d27c 949 KKASSERT(pblksize >= HAMMER2_MIN_ALLOC);
355d67fc 950retry:
84e47819
MD
951 dparent = hammer2_cluster_lookup_init(cparent, 0);
952 cluster = hammer2_cluster_lookup(dparent, &key_dummy,
355d67fc 953 lbase, lbase,
278ab2b2 954 HAMMER2_LOOKUP_NODATA, &ddflag);
355d67fc 955
278ab2b2 956 if (cluster == NULL) {
355d67fc
MD
957 /*
958 * We found a hole, create a new chain entry.
959 *
960 * NOTE: DATA chains are created without device backing
961 * store (nor do we want any).
962 */
84e47819 963 *errorp = hammer2_cluster_create(trans, dparent, &cluster,
355d67fc
MD
964 lbase, HAMMER2_PBUFRADIX,
965 HAMMER2_BREF_TYPE_DATA,
966 pblksize);
278ab2b2 967 if (cluster == NULL) {
84e47819 968 hammer2_cluster_lookup_done(dparent);
278ab2b2 969 panic("hammer2_cluster_create: par=%p error=%d\n",
84e47819 970 dparent->focus, *errorp);
355d67fc
MD
971 goto retry;
972 }
355d67fc
MD
973 /*ip->delta_dcount += pblksize;*/
974 } else {
278ab2b2 975 switch (hammer2_cluster_type(cluster)) {
355d67fc
MD
976 case HAMMER2_BREF_TYPE_INODE:
977 /*
978 * The data is embedded in the inode. The
979 * caller is responsible for marking the inode
980 * modified and copying the data to the embedded
981 * area.
982 */
355d67fc
MD
983 break;
984 case HAMMER2_BREF_TYPE_DATA:
278ab2b2
MD
985 if (hammer2_cluster_bytes(cluster) != pblksize) {
986 hammer2_cluster_resize(trans, ip,
84e47819 987 dparent, cluster,
355d67fc
MD
988 pradix,
989 HAMMER2_MODIFY_OPTDATA);
990 }
278ab2b2 991 hammer2_cluster_modify(trans, cluster,
355d67fc 992 HAMMER2_MODIFY_OPTDATA);
355d67fc
MD
993 break;
994 default:
995 panic("hammer2_assign_physical: bad type");
996 /* NOT REACHED */
355d67fc
MD
997 break;
998 }
999 }
1000
1001 /*
84e47819
MD
1002 * Cleanup. If cluster wound up being the inode itself, i.e.
1003 * the DIRECTDATA case for offset 0, then we need to update cparent.
1004 * The caller expects cparent to not become stale.
355d67fc 1005 */
84e47819
MD
1006 hammer2_cluster_lookup_done(dparent);
1007 /* dparent = NULL; safety */
1008 if (cluster && ddflag)
278ab2b2 1009 hammer2_cluster_replace_locked(cparent, cluster);
278ab2b2 1010 return (cluster);
355d67fc
MD
1011}
1012
1013/*
1014 * From hammer2_vnops.c.
1015 * The core write function which determines which path to take
1016 * depending on compression settings.
1017 */
1018static
1019void
1e5c08ba 1020hammer2_write_file_core(struct buf *bp, hammer2_trans_t *trans,
355d67fc 1021 hammer2_inode_t *ip, hammer2_inode_data_t *ipdata,
278ab2b2 1022 hammer2_cluster_t *cparent,
355d67fc
MD
1023 hammer2_key_t lbase, int ioflag, int pblksize,
1024 int *errorp)
1025{
278ab2b2 1026 hammer2_cluster_t *cluster;
f481450f
MD
1027
1028 switch(HAMMER2_DEC_COMP(ipdata->comp_algo)) {
1029 case HAMMER2_COMP_NONE:
355d67fc
MD
1030 /*
1031 * We have to assign physical storage to the buffer
1032 * we intend to dirty or write now to avoid deadlocks
1033 * in the strategy code later.
1034 *
1035 * This can return NOOFFSET for inode-embedded data.
1036 * The strategy code will take care of it in that case.
1037 */
278ab2b2 1038 cluster = hammer2_assign_physical(trans, ip, cparent,
355d67fc
MD
1039 lbase, pblksize,
1040 errorp);
278ab2b2
MD
1041 hammer2_write_bp(cluster, bp, ioflag, pblksize, errorp);
1042 if (cluster)
1043 hammer2_cluster_unlock(cluster);
f481450f
MD
1044 break;
1045 case HAMMER2_COMP_AUTOZERO:
1046 /*
1047 * Check for zero-fill only
1048 */
1049 hammer2_zero_check_and_write(bp, trans, ip,
278ab2b2 1050 ipdata, cparent, lbase,
f481450f
MD
1051 ioflag, pblksize, errorp);
1052 break;
1053 case HAMMER2_COMP_LZ4:
1054 case HAMMER2_COMP_ZLIB:
1055 default:
1056 /*
1057 * Check for zero-fill and attempt compression.
1058 */
1059 hammer2_compress_and_write(bp, trans, ip,
278ab2b2 1060 ipdata, cparent,
f481450f
MD
1061 lbase, ioflag,
1062 pblksize, errorp,
1063 ipdata->comp_algo);
1064 break;
355d67fc 1065 }
355d67fc
MD
1066}
1067
1068/*
355d67fc
MD
1069 * Generic function that will perform the compression in compression
1070 * write path. The compression algorithm is determined by the settings
1071 * obtained from inode.
1072 */
1073static
1074void
1e5c08ba 1075hammer2_compress_and_write(struct buf *bp, hammer2_trans_t *trans,
6a5f4fe6 1076 hammer2_inode_t *ip, const hammer2_inode_data_t *ipdata,
278ab2b2 1077 hammer2_cluster_t *cparent,
355d67fc 1078 hammer2_key_t lbase, int ioflag, int pblksize,
f481450f 1079 int *errorp, int comp_algo)
355d67fc 1080{
278ab2b2 1081 hammer2_cluster_t *cluster;
355d67fc 1082 hammer2_chain_t *chain;
f481450f
MD
1083 int comp_size;
1084 int comp_block_size;
278ab2b2 1085 int i;
f481450f 1086 char *comp_buffer;
355d67fc 1087
f481450f 1088 if (test_block_zeros(bp->b_data, pblksize)) {
278ab2b2 1089 zero_write(bp, trans, ip, ipdata, cparent, lbase, errorp);
f481450f
MD
1090 return;
1091 }
1e5c08ba 1092
f481450f
MD
1093 comp_size = 0;
1094 comp_buffer = NULL;
355d67fc 1095
f481450f 1096 KKASSERT(pblksize / 2 <= 32768);
355d67fc 1097
f481450f
MD
1098 if (ip->comp_heuristic < 8 || (ip->comp_heuristic & 7) == 0) {
1099 z_stream strm_compress;
1100 int comp_level;
1101 int ret;
1102
1103 switch(HAMMER2_DEC_COMP(comp_algo)) {
1104 case HAMMER2_COMP_LZ4:
1105 comp_buffer = objcache_get(cache_buffer_write,
1106 M_INTWAIT);
1107 comp_size = LZ4_compress_limitedOutput(
1108 bp->b_data,
1109 &comp_buffer[sizeof(int)],
1110 pblksize,
1111 pblksize / 2 - sizeof(int));
1112 /*
1113 * We need to prefix with the size, LZ4
1114 * doesn't do it for us. Add the related
1115 * overhead.
1116 */
1117 *(int *)comp_buffer = comp_size;
1118 if (comp_size)
1119 comp_size += sizeof(int);
1120 break;
1121 case HAMMER2_COMP_ZLIB:
1122 comp_level = HAMMER2_DEC_LEVEL(comp_algo);
1123 if (comp_level == 0)
1124 comp_level = 6; /* default zlib compression */
1125 else if (comp_level < 6)
1126 comp_level = 6;
1127 else if (comp_level > 9)
1128 comp_level = 9;
1129 ret = deflateInit(&strm_compress, comp_level);
1130 if (ret != Z_OK) {
1131 kprintf("HAMMER2 ZLIB: fatal error "
1132 "on deflateInit.\n");
1133 }
1134
1135 comp_buffer = objcache_get(cache_buffer_write,
1136 M_INTWAIT);
1137 strm_compress.next_in = bp->b_data;
1138 strm_compress.avail_in = pblksize;
1139 strm_compress.next_out = comp_buffer;
1140 strm_compress.avail_out = pblksize / 2;
1141 ret = deflate(&strm_compress, Z_FINISH);
1142 if (ret == Z_STREAM_END) {
1143 comp_size = pblksize / 2 -
1144 strm_compress.avail_out;
1e5c08ba 1145 } else {
f481450f 1146 comp_size = 0;
355d67fc 1147 }
f481450f
MD
1148 ret = deflateEnd(&strm_compress);
1149 break;
1150 default:
1151 kprintf("Error: Unknown compression method.\n");
1152 kprintf("Comp_method = %d.\n", comp_algo);
1153 break;
355d67fc 1154 }
f481450f 1155 }
1e5c08ba 1156
f481450f
MD
1157 if (comp_size == 0) {
1158 /*
1159 * compression failed or turned off
1160 */
1161 comp_block_size = pblksize; /* safety */
1162 if (++ip->comp_heuristic > 128)
1163 ip->comp_heuristic = 8;
1164 } else {
1165 /*
1166 * compression succeeded
1167 */
1168 ip->comp_heuristic = 0;
1169 if (comp_size <= 1024) {
1170 comp_block_size = 1024;
1171 } else if (comp_size <= 2048) {
1172 comp_block_size = 2048;
1173 } else if (comp_size <= 4096) {
1174 comp_block_size = 4096;
1175 } else if (comp_size <= 8192) {
1176 comp_block_size = 8192;
1177 } else if (comp_size <= 16384) {
1178 comp_block_size = 16384;
1179 } else if (comp_size <= 32768) {
1180 comp_block_size = 32768;
355d67fc 1181 } else {
f481450f
MD
1182 panic("hammer2: WRITE PATH: "
1183 "Weird comp_size value.");
1184 /* NOT REACHED */
1185 comp_block_size = pblksize;
1186 }
1187 }
1188
278ab2b2
MD
1189 cluster = hammer2_assign_physical(trans, ip, cparent,
1190 lbase, comp_block_size,
1191 errorp);
84e47819 1192 ipdata = &hammer2_cluster_data(cparent)->ipdata;
f481450f
MD
1193
1194 if (*errorp) {
1195 kprintf("WRITE PATH: An error occurred while "
1196 "assigning physical space.\n");
278ab2b2
MD
1197 KKASSERT(cluster == NULL);
1198 goto done;
1199 }
1200
1201 for (i = 0; i < cluster->nchains; ++i) {
fdf62707
MD
1202 hammer2_io_t *dio;
1203 char *bdata;
f481450f
MD
1204 int temp_check;
1205
278ab2b2 1206 chain = cluster->array[i];
f481450f
MD
1207 KKASSERT(chain->flags & HAMMER2_CHAIN_MODIFIED);
1208
1209 switch(chain->bref.type) {
1210 case HAMMER2_BREF_TYPE_INODE:
1211 KKASSERT(chain->data->ipdata.op_flags &
1212 HAMMER2_OPFLAG_DIRECTDATA);
1213 KKASSERT(bp->b_loffset == 0);
1214 bcopy(bp->b_data, chain->data->ipdata.u.data,
1215 HAMMER2_EMBEDDED_BYTES);
1216 break;
1217 case HAMMER2_BREF_TYPE_DATA:
f481450f
MD
1218 temp_check = HAMMER2_DEC_CHECK(chain->bref.methods);
1219
1e5c08ba 1220 /*
f481450f
MD
1221 * Optimize out the read-before-write
1222 * if possible.
1e5c08ba 1223 */
fdf62707
MD
1224 *errorp = hammer2_io_newnz(chain->hmp,
1225 chain->bref.data_off,
1226 chain->bytes,
1227 &dio);
1228 if (*errorp) {
1229 hammer2_io_brelse(&dio);
1230 kprintf("hammer2: WRITE PATH: "
1231 "dbp bread error\n");
1232 break;
355d67fc 1233 }
fdf62707 1234 bdata = hammer2_io_data(dio, chain->bref.data_off);
355d67fc 1235
f481450f
MD
1236 /*
1237 * When loading the block make sure we don't
1238 * leave garbage after the compressed data.
1239 */
1240 if (comp_size) {
1241 chain->bref.methods =
1242 HAMMER2_ENC_COMP(comp_algo) +
1243 HAMMER2_ENC_CHECK(temp_check);
fdf62707 1244 bcopy(comp_buffer, bdata, comp_size);
f481450f 1245 if (comp_size != comp_block_size) {
fdf62707
MD
1246 bzero(bdata + comp_size,
1247 comp_block_size - comp_size);
355d67fc 1248 }
f481450f
MD
1249 } else {
1250 chain->bref.methods =
1251 HAMMER2_ENC_COMP(
1252 HAMMER2_COMP_NONE) +
1253 HAMMER2_ENC_CHECK(temp_check);
fdf62707 1254 bcopy(bp->b_data, bdata, pblksize);
f481450f 1255 }
355d67fc 1256
f481450f
MD
1257 /*
1258 * Device buffer is now valid, chain is no
1259 * longer in the initial state.
1260 */
fdf62707 1261 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
355d67fc 1262
f481450f
MD
1263 /* Now write the related bdp. */
1264 if (ioflag & IO_SYNC) {
355d67fc 1265 /*
f481450f 1266 * Synchronous I/O requested.
355d67fc 1267 */
fdf62707 1268 hammer2_io_bwrite(&dio);
f481450f
MD
1269 /*
1270 } else if ((ioflag & IO_DIRECT) &&
1271 loff + n == pblksize) {
fdf62707 1272 hammer2_io_bdwrite(&dio);
f481450f
MD
1273 */
1274 } else if (ioflag & IO_ASYNC) {
fdf62707 1275 hammer2_io_bawrite(&dio);
f481450f 1276 } else {
fdf62707 1277 hammer2_io_bdwrite(&dio);
355d67fc 1278 }
f481450f
MD
1279 break;
1280 default:
1281 panic("hammer2_write_bp: bad chain type %d\n",
1282 chain->bref.type);
1283 /* NOT REACHED */
1284 break;
355d67fc 1285 }
f481450f
MD
1286
1287 hammer2_chain_unlock(chain);
355d67fc 1288 }
278ab2b2 1289done:
f481450f
MD
1290 if (comp_buffer)
1291 objcache_put(cache_buffer_write, comp_buffer);
355d67fc
MD
1292}
1293
1294/*
1295 * Function that performs zero-checking and writing without compression,
1296 * it corresponds to default zero-checking path.
1297 */
1298static
1299void
1e5c08ba 1300hammer2_zero_check_and_write(struct buf *bp, hammer2_trans_t *trans,
6a5f4fe6 1301 hammer2_inode_t *ip, const hammer2_inode_data_t *ipdata,
278ab2b2 1302 hammer2_cluster_t *cparent,
355d67fc
MD
1303 hammer2_key_t lbase, int ioflag, int pblksize, int *errorp)
1304{
278ab2b2 1305 hammer2_cluster_t *cluster;
355d67fc 1306
f481450f 1307 if (test_block_zeros(bp->b_data, pblksize)) {
278ab2b2 1308 zero_write(bp, trans, ip, ipdata, cparent, lbase, errorp);
f481450f 1309 } else {
278ab2b2
MD
1310 cluster = hammer2_assign_physical(trans, ip, cparent,
1311 lbase, pblksize, errorp);
1312 hammer2_write_bp(cluster, bp, ioflag, pblksize, errorp);
1313 if (cluster)
1314 hammer2_cluster_unlock(cluster);
355d67fc
MD
1315 }
1316}
1317
1318/*
1319 * A function to test whether a block of data contains only zeros,
f481450f 1320 * returns TRUE (non-zero) if the block is all zeros.
355d67fc
MD
1321 */
1322static
1323int
f481450f 1324test_block_zeros(const char *buf, size_t bytes)
355d67fc
MD
1325{
1326 size_t i;
1327
1328 for (i = 0; i < bytes; i += sizeof(long)) {
f481450f
MD
1329 if (*(const long *)(buf + i) != 0)
1330 return (0);
355d67fc 1331 }
f481450f 1332 return (1);
355d67fc
MD
1333}
1334
1335/*
1336 * Function to "write" a block that contains only zeros.
1337 */
1338static
1339void
6a5f4fe6
MD
1340zero_write(struct buf *bp, hammer2_trans_t *trans,
1341 hammer2_inode_t *ip, const hammer2_inode_data_t *ipdata,
1342 hammer2_cluster_t *cparent,
1e5c08ba 1343 hammer2_key_t lbase, int *errorp __unused)
355d67fc 1344{
278ab2b2
MD
1345 hammer2_cluster_t *cluster;
1346 hammer2_media_data_t *data;
1897c66e 1347 hammer2_key_t key_dummy;
278ab2b2 1348 int ddflag;
355d67fc 1349
278ab2b2
MD
1350 cparent = hammer2_cluster_lookup_init(cparent, 0);
1351 cluster = hammer2_cluster_lookup(cparent, &key_dummy, lbase, lbase,
1352 HAMMER2_LOOKUP_NODATA, &ddflag);
1353 if (cluster) {
6a5f4fe6 1354 data = hammer2_cluster_wdata(cluster);
355d67fc 1355
278ab2b2 1356 if (ddflag) {
6a5f4fe6
MD
1357 KKASSERT(cluster->focus->flags &
1358 HAMMER2_CHAIN_MODIFIED);
278ab2b2 1359 bzero(data->ipdata.u.data, HAMMER2_EMBEDDED_BYTES);
6a5f4fe6 1360 hammer2_cluster_modsync(cluster);
355d67fc 1361 } else {
278ab2b2 1362 hammer2_cluster_delete(trans, cluster, 0);
355d67fc 1363 }
278ab2b2 1364 hammer2_cluster_unlock(cluster);
355d67fc 1365 }
278ab2b2 1366 hammer2_cluster_lookup_done(cparent);
355d67fc
MD
1367}
1368
1369/*
1370 * Function to write the data as it is, without performing any sort of
1371 * compression. This function is used in path without compression and
1372 * default zero-checking path.
1373 */
1374static
1375void
278ab2b2 1376hammer2_write_bp(hammer2_cluster_t *cluster, struct buf *bp, int ioflag,
1e5c08ba 1377 int pblksize, int *errorp)
355d67fc 1378{
278ab2b2 1379 hammer2_chain_t *chain;
fdf62707
MD
1380 hammer2_io_t *dio;
1381 char *bdata;
355d67fc 1382 int error;
278ab2b2
MD
1383 int i;
1384 int temp_check;
355d67fc 1385
278ab2b2 1386 error = 0; /* XXX TODO below */
355d67fc 1387
278ab2b2
MD
1388 for (i = 0; i < cluster->nchains; ++i) {
1389 chain = cluster->array[i];
1390
1391 temp_check = HAMMER2_DEC_CHECK(chain->bref.methods);
1392
1393 KKASSERT(chain->flags & HAMMER2_CHAIN_MODIFIED);
1394
1395 switch(chain->bref.type) {
1396 case HAMMER2_BREF_TYPE_INODE:
1397 KKASSERT(chain->data->ipdata.op_flags &
1398 HAMMER2_OPFLAG_DIRECTDATA);
1399 KKASSERT(bp->b_loffset == 0);
1400 bcopy(bp->b_data, chain->data->ipdata.u.data,
1401 HAMMER2_EMBEDDED_BYTES);
1402 error = 0;
fdf62707 1403 break;
278ab2b2
MD
1404 case HAMMER2_BREF_TYPE_DATA:
1405 error = hammer2_io_newnz(chain->hmp,
1406 chain->bref.data_off,
1407 chain->bytes, &dio);
1408 if (error) {
1409 hammer2_io_bqrelse(&dio);
1410 kprintf("hammer2: WRITE PATH: "
1411 "dbp bread error\n");
1412 break;
1413 }
1414 bdata = hammer2_io_data(dio, chain->bref.data_off);
355d67fc 1415
278ab2b2
MD
1416 chain->bref.methods = HAMMER2_ENC_COMP(
1417 HAMMER2_COMP_NONE) +
1418 HAMMER2_ENC_CHECK(temp_check);
1419 bcopy(bp->b_data, bdata, chain->bytes);
355d67fc 1420
355d67fc 1421 /*
278ab2b2
MD
1422 * Device buffer is now valid, chain is no
1423 * longer in the initial state.
355d67fc 1424 */
278ab2b2
MD
1425 atomic_clear_int(&chain->flags, HAMMER2_CHAIN_INITIAL);
1426
1427 if (ioflag & IO_SYNC) {
1428 /*
1429 * Synchronous I/O requested.
1430 */
1431 hammer2_io_bwrite(&dio);
1432 /*
1433 } else if ((ioflag & IO_DIRECT) &&
1434 loff + n == pblksize) {
1435 hammer2_io_bdwrite(&dio);
1436 */
1437 } else if (ioflag & IO_ASYNC) {
1438 hammer2_io_bawrite(&dio);
1439 } else {
1440 hammer2_io_bdwrite(&dio);
1441 }
1442 break;
1443 default:
1444 panic("hammer2_write_bp: bad chain type %d\n",
1445 chain->bref.type);
1446 /* NOT REACHED */
1447 error = 0;
1448 break;
355d67fc 1449 }
278ab2b2 1450 KKASSERT(error == 0); /* XXX TODO */
355d67fc 1451 }
1e5c08ba 1452 *errorp = error;
355d67fc
MD
1453}
1454
e118c14f
MD
1455static
1456int
10136ab6
MD
1457hammer2_remount(hammer2_mount_t *hmp, struct mount *mp, char *path,
1458 struct vnode *devvp, struct ucred *cred)
703720e4 1459{
10136ab6
MD
1460 int error;
1461
1462 if (hmp->ronly && (mp->mnt_kern_flag & MNTK_WANTRDWR)) {
1463 error = hammer2_recovery(hmp);
1464 } else {
1465 error = 0;
1466 }
1467 return error;
703720e4
MD
1468}
1469
e118c14f
MD
1470static
1471int
066e00cc 1472hammer2_vfs_unmount(struct mount *mp, int mntflags)
703720e4 1473{
e4e20f48 1474 hammer2_pfsmount_t *pmp;
54eb943b 1475 hammer2_mount_t *hmp;
7bed8d7e 1476 hammer2_chain_t *rchain;
58e43599 1477 hammer2_cluster_t *cluster;
703720e4 1478 int flags;
50e4f8f4 1479 int error = 0;
7bed8d7e 1480 int i;
703720e4 1481
e4e20f48 1482 pmp = MPTOPMP(mp);
703720e4 1483
623d43d4
MD
1484 if (pmp == NULL)
1485 return(0);
1486
9b6b3df4 1487 lockmgr(&hammer2_mntlk, LK_EXCLUSIVE);
58e43599 1488 TAILQ_REMOVE(&hammer2_pfslist, pmp, mntentry);
9b6b3df4 1489
065f4046
MD
1490 /*
1491 * If mount initialization proceeded far enough we must flush
1492 * its vnodes.
1493 */
1494 if (mntflags & MNT_FORCE)
1495 flags = FORCECLOSE;
1496 else
7bed8d7e 1497 flags = 0;
065f4046
MD
1498 if (pmp->iroot) {
1499 error = vflush(mp, 0, flags);
1500 if (error)
1501 goto failed;
1502 }
50e4f8f4 1503
eae2ed61
MD
1504 ccms_domain_uninit(&pmp->ccms_dom);
1505 kdmsg_iocom_uninit(&pmp->iocom); /* XXX chain dependency */
1506
065f4046
MD
1507 if (pmp->wthread_td) {
1508 mtx_lock(&pmp->wthread_mtx);
1509 pmp->wthread_destroy = 1;
1510 wakeup(&pmp->wthread_bioq);
1511 while (pmp->wthread_destroy != -1) {
1512 mtxsleep(&pmp->wthread_destroy,
1513 &pmp->wthread_mtx, 0,
1514 "umount-sleep", 0);
1515 }
1516 mtx_unlock(&pmp->wthread_mtx);
1517 pmp->wthread_td = NULL;
1518 }
e4e20f48 1519
044541cd
MD
1520 /*
1521 * Cleanup our reference on ihidden.
1522 */
1523 if (pmp->ihidden) {
1524 hammer2_inode_drop(pmp->ihidden);
1525 pmp->ihidden = NULL;
1526 }
7bed8d7e 1527
044541cd
MD
1528 /*
1529 * Cleanup our reference on iroot. iroot is (should) not be needed
1530 * by the flush code.
1531 */
1532 if (pmp->iroot) {
58e43599
MD
1533 cluster = &pmp->iroot->cluster;
1534 for (i = 0; i < pmp->iroot->cluster.nchains; ++i) {
1535 rchain = pmp->iroot->cluster.array[i];
1536 if (rchain == NULL)
1537 continue;
1538 hmp = rchain->hmp;
1539 hammer2_vfs_unmount_hmp1(mp, hmp);
7bed8d7e 1540
7bed8d7e 1541 atomic_clear_int(&rchain->flags, HAMMER2_CHAIN_MOUNTED);
0dea3156 1542#if REPORT_REFS_ERRORS
7bed8d7e
MD
1543 if (rchain->refs != 1)
1544 kprintf("PMP->RCHAIN %p REFS WRONG %d\n",
1545 rchain, rchain->refs);
0dea3156 1546#else
7bed8d7e 1547 KKASSERT(rchain->refs == 1);
0dea3156 1548#endif
7bed8d7e 1549 hammer2_chain_drop(rchain);
58e43599
MD
1550 cluster->array[i] = NULL;
1551 hammer2_vfs_unmount_hmp2(mp, hmp);
e4e20f48 1552 }
58e43599 1553 cluster->focus = NULL;
222d9e22 1554
58e43599
MD
1555#if REPORT_REFS_ERRORS
1556 if (pmp->iroot->refs != 1)
1557 kprintf("PMP->IROOT %p REFS WRONG %d\n",
1558 pmp->iroot, pmp->iroot->refs);
1559#else
1560 KKASSERT(pmp->iroot->refs == 1);
1561#endif
1562 /* ref for pmp->iroot */
1563 hammer2_inode_drop(pmp->iroot);
1564 pmp->iroot = NULL;
54eb943b 1565 }
703720e4 1566
e4e20f48 1567 pmp->mp = NULL;
54eb943b 1568 mp->mnt_data = NULL;
703720e4 1569
26bf1a36 1570 kmalloc_destroy(&pmp->mmsg);
99da41ea 1571 kmalloc_destroy(&pmp->minode);
26bf1a36 1572
e4e20f48 1573 kfree(pmp, M_HAMMER2);
9b6b3df4
MD
1574 error = 0;
1575
1576failed:
e4e20f48 1577 lockmgr(&hammer2_mntlk, LK_RELEASE);
a5913bdf 1578
703720e4
MD
1579 return (error);
1580}
1581
623d43d4
MD
1582static
1583void
1584hammer2_vfs_unmount_hmp1(struct mount *mp, hammer2_mount_t *hmp)
1585{
1586 hammer2_mount_exlock(hmp);
1587 --hmp->pmp_count;
1588
1589 kprintf("hammer2_unmount hmp=%p pmpcnt=%d\n", hmp, hmp->pmp_count);
1590
1591 /*
1592 * Flush any left over chains. The voldata lock is only used
1593 * to synchronize against HAMMER2_CHAIN_MODIFIED_AUX.
1594 *
1595 * Flush twice to ensure that the freemap is completely
1596 * synchronized. If we only do it once the next mount's
1597 * recovery scan will have to do some fixups (which isn't
1598 * bad, but we don't want it to have to do it except when
1599 * recovering from a crash).
1600 */
1601 hammer2_voldata_lock(hmp);
1602 if (((hmp->vchain.flags | hmp->fchain.flags) &
1603 HAMMER2_CHAIN_MODIFIED) ||
8138a154
MD
1604 hmp->vchain.update_hi > hmp->voldata.mirror_tid ||
1605 hmp->fchain.update_hi > hmp->voldata.freemap_tid) {
623d43d4
MD
1606 hammer2_voldata_unlock(hmp, 0);
1607 hammer2_vfs_sync(mp, MNT_WAIT);
1608 /*hammer2_vfs_sync(mp, MNT_WAIT);*/
1609 } else {
1610 hammer2_voldata_unlock(hmp, 0);
1611 }
1612 if (hmp->pmp_count == 0) {
1613 if (((hmp->vchain.flags | hmp->fchain.flags) &
1614 HAMMER2_CHAIN_MODIFIED) ||
8138a154 1615 (hmp->vchain.update_hi >
623d43d4 1616 hmp->voldata.mirror_tid) ||
8138a154 1617 (hmp->fchain.update_hi >
623d43d4
MD
1618 hmp->voldata.freemap_tid)) {
1619 kprintf("hammer2_unmount: chains left over "
1620 "after final sync\n");
0924b3f8
MD
1621 kprintf(" vchain %08x update_hi %jx/%jx\n",
1622 hmp->vchain.flags,
1623 hmp->voldata.mirror_tid,
8138a154 1624 hmp->vchain.update_hi);
0924b3f8
MD
1625 kprintf(" fchain %08x update_hi %jx/%jx\n",
1626 hmp->fchain.flags,
1627 hmp->voldata.freemap_tid,
8138a154 1628 hmp->fchain.update_hi);
0924b3f8 1629
623d43d4
MD
1630 if (hammer2_debug & 0x0010)
1631 Debugger("entered debugger");
1632 }
1633 }
1634}
1635
1636static
1637void
1638hammer2_vfs_unmount_hmp2(struct mount *mp, hammer2_mount_t *hmp)
1639{
1640 struct vnode *devvp;
1641 int dumpcnt;
1642 int ronly = ((mp->mnt_flag & MNT_RDONLY) != 0);
1643
1644 /*
1645 * If no PFS's left drop the master hammer2_mount for the
1646 * device.
1647 */
1648 if (hmp->pmp_count == 0) {
1649 if (hmp->sroot) {
1650 hammer2_inode_drop(hmp->sroot);
1651 hmp->sroot = NULL;
1652 }
1653
1654 /*
1655 * Finish up with the device vnode
1656 */
1657 if ((devvp = hmp->devvp) != NULL) {
1658 vn_lock(devvp, LK_EXCLUSIVE | LK_RETRY);
1659 vinvalbuf(devvp, (ronly ? 0 : V_SAVE), 0, 0);
1660 hmp->devvp = NULL;
8138a154 1661 VOP_CLOSE(devvp, (ronly ? FREAD : FREAD|FWRITE), NULL);
623d43d4
MD
1662 vn_unlock(devvp);
1663 vrele(devvp);
1664 devvp = NULL;
1665 }
1666
1667 /*
1668 * Final drop of embedded freemap root chain to
1669 * clean up fchain.core (fchain structure is not
1670 * flagged ALLOCATED so it is cleaned out and then
1671 * left to rot).
1672 */
1673 hammer2_chain_drop(&hmp->fchain);
1674
1675 /*
1676 * Final drop of embedded volume root chain to clean
1677 * up vchain.core (vchain structure is not flagged
1678 * ALLOCATED so it is cleaned out and then left to
1679 * rot).
1680 */
1681 dumpcnt = 50;
8138a154 1682 hammer2_dump_chain(&hmp->vchain, 0, &dumpcnt, 'v');
623d43d4 1683 dumpcnt = 50;
8138a154 1684 hammer2_dump_chain(&hmp->fchain, 0, &dumpcnt, 'f');
623d43d4
MD
1685 hammer2_mount_unlock(hmp);
1686 hammer2_chain_drop(&hmp->vchain);
1687
1688 hammer2_io_cleanup(hmp, &hmp->iotree);
1689 if (hmp->iofree_count) {
1690 kprintf("io_cleanup: %d I/O's left hanging\n",
1691 hmp->iofree_count);
1692 }
1693
1694 TAILQ_REMOVE(&hammer2_mntlist, hmp, mntentry);
1695 kmalloc_destroy(&hmp->mchain);
1696 kfree(hmp, M_HAMMER2);
1697 } else {
1698 hammer2_mount_unlock(hmp);
1699 }
1700}
1701
e118c14f
MD
1702static
1703int
066e00cc 1704hammer2_vfs_vget(struct mount *mp, struct vnode *dvp,
703720e4
MD
1705 ino_t ino, struct vnode **vpp)
1706{
1707 kprintf("hammer2_vget\n");
1708 return (EOPNOTSUPP);
1709}
1710
e118c14f
MD
1711static
1712int
066e00cc 1713hammer2_vfs_root(struct mount *mp, struct vnode **vpp)
703720e4 1714{
e4e20f48 1715 hammer2_pfsmount_t *pmp;
278ab2b2 1716 hammer2_cluster_t *cparent;
703720e4
MD
1717 int error;
1718 struct vnode *vp;
1719
e4e20f48 1720 pmp = MPTOPMP(mp);
e4e20f48 1721 if (pmp->iroot == NULL) {
703720e4
MD
1722 *vpp = NULL;
1723 error = EINVAL;
1724 } else {
278ab2b2 1725 cparent = hammer2_inode_lock_sh(pmp->iroot);
84e47819 1726 vp = hammer2_igetv(pmp->iroot, cparent, &error);
278ab2b2 1727 hammer2_inode_unlock_sh(pmp->iroot, cparent);
703720e4
MD
1728 *vpp = vp;
1729 if (vp == NULL)
1730 kprintf("vnodefail\n");
1731 }
703720e4
MD
1732
1733 return (error);
1734}
1735
28ee5f14
MD
1736/*
1737 * Filesystem status
1738 *
476d2aad 1739 * XXX incorporate ipdata->inode_quota and data_quota
28ee5f14 1740 */
e118c14f
MD
1741static
1742int
066e00cc 1743hammer2_vfs_statfs(struct mount *mp, struct statfs *sbp, struct ucred *cred)
703720e4 1744{
28ee5f14 1745 hammer2_pfsmount_t *pmp;
54eb943b 1746 hammer2_mount_t *hmp;
703720e4 1747
28ee5f14 1748 pmp = MPTOPMP(mp);
58e43599
MD
1749 KKASSERT(pmp->iroot->cluster.nchains >= 1);
1750 hmp = pmp->iroot->cluster.focus->hmp; /* XXX */
703720e4 1751
476d2aad 1752 mp->mnt_stat.f_files = pmp->inode_count;
28ee5f14
MD
1753 mp->mnt_stat.f_ffree = 0;
1754 mp->mnt_stat.f_blocks = hmp->voldata.allocator_size / HAMMER2_PBUFSIZE;
9f604b01 1755 mp->mnt_stat.f_bfree = hmp->voldata.allocator_free / HAMMER2_PBUFSIZE;
50e4f8f4 1756 mp->mnt_stat.f_bavail = mp->mnt_stat.f_bfree;
703720e4 1757
50e4f8f4 1758 *sbp = mp->mnt_stat;
703720e4
MD
1759 return (0);
1760}
1761
e118c14f
MD
1762static
1763int
066e00cc 1764hammer2_vfs_statvfs(struct mount *mp, struct statvfs *sbp, struct ucred *cred)
703720e4 1765{
28ee5f14 1766 hammer2_pfsmount_t *pmp;
50e4f8f4
MD
1767 hammer2_mount_t *hmp;
1768
28ee5f14 1769 pmp = MPTOPMP(mp);
58e43599
MD
1770 KKASSERT(pmp->iroot->cluster.nchains >= 1);
1771 hmp = pmp->iroot->cluster.focus->hmp; /* XXX */
50e4f8f4 1772
88a032af 1773 mp->mnt_vstat.f_bsize = HAMMER2_PBUFSIZE;
476d2aad 1774 mp->mnt_vstat.f_files = pmp->inode_count;
28ee5f14
MD
1775 mp->mnt_vstat.f_ffree = 0;
1776 mp->mnt_vstat.f_blocks = hmp->voldata.allocator_size / HAMMER2_PBUFSIZE;
51f0b4b7 1777 mp->mnt_vstat.f_bfree = hmp->voldata.allocator_free / HAMMER2_PBUFSIZE;
28ee5f14 1778 mp->mnt_vstat.f_bavail = mp->mnt_vstat.f_bfree;
50e4f8f4
MD
1779
1780 *sbp = mp->mnt_vstat;
1781 return (0);
703720e4
MD
1782}
1783
10136ab6
MD
1784/*
1785 * Mount-time recovery (RW mounts)
1786 *
1787 * Updates to the free block table are allowed to lag flushes by one
1788 * transaction. In case of a crash, then on a fresh mount we must do an
1789 * incremental scan of transaction id voldata.mirror_tid and make sure the
1790 * related blocks have been marked allocated.
1791 *
1792 */
1793struct hammer2_recovery_elm {
1794 TAILQ_ENTRY(hammer2_recovery_elm) entry;
1795 hammer2_chain_t *chain;
1796};
1797
1798TAILQ_HEAD(hammer2_recovery_list, hammer2_recovery_elm);
1799
1800static int hammer2_recovery_scan(hammer2_trans_t *trans, hammer2_mount_t *hmp,
1801 hammer2_chain_t *parent,
1802 struct hammer2_recovery_list *list, int depth);
1803
1804#define HAMMER2_RECOVERY_MAXDEPTH 10
1805
1806static
1807int
1808hammer2_recovery(hammer2_mount_t *hmp)
1809{
1810 hammer2_trans_t trans;
1811 struct hammer2_recovery_list list;
1812 struct hammer2_recovery_elm *elm;
1813 hammer2_chain_t *parent;
1814 int error;
1815 int cumulative_error = 0;
1816
1817 hammer2_trans_init(&trans, NULL, hmp, 0);
1818
1819 TAILQ_INIT(&list);
1820 parent = hammer2_chain_lookup_init(&hmp->vchain, 0);
1821 cumulative_error = hammer2_recovery_scan(&trans, hmp, parent, &list, 0);
1822 hammer2_chain_lookup_done(parent);
1823
1824 while ((elm = TAILQ_FIRST(&list)) != NULL) {
1825 TAILQ_REMOVE(&list, elm, entry);
1826 parent = elm->chain;
1827 kfree(elm, M_HAMMER2);
1828
1829 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS |
1830 HAMMER2_RESOLVE_NOREF);
1831 error = hammer2_recovery_scan(&trans, hmp, parent, &list, 0);
1832 hammer2_chain_unlock(parent);
1833 if (error)
1834 cumulative_error = error;
1835 }
1836 hammer2_trans_done(&trans);
1837
1838 return cumulative_error;
1839}
1840
1841static
1842int
1843hammer2_recovery_scan(hammer2_trans_t *trans, hammer2_mount_t *hmp,
1844 hammer2_chain_t *parent,
1845 struct hammer2_recovery_list *list, int depth)
1846{
1847 hammer2_chain_t *chain;
1848 int cache_index;
1849 int cumulative_error = 0;
1850 int error;
1851
1852 /*
1853 * Defer operation if depth limit reached.
1854 */
1855 if (depth >= HAMMER2_RECOVERY_MAXDEPTH) {
1856 struct hammer2_recovery_elm *elm;
1857
1858 elm = kmalloc(sizeof(*elm), M_HAMMER2, M_ZERO | M_WAITOK);
1859 elm->chain = parent;
1860 hammer2_chain_ref(parent);
1861 TAILQ_INSERT_TAIL(list, elm, entry);
1862 /* unlocked by caller */
1863
1864 return(0);
1865 }
1866
1867 /*
1868 * Adjust freemap to ensure that the block(s) are marked allocated.
1869 */
1870 if (parent->bref.type != HAMMER2_BREF_TYPE_VOLUME) {
1871 hammer2_freemap_adjust(trans, hmp, &parent->bref,
1872 HAMMER2_FREEMAP_DORECOVER);
1873 }
1874
1875 /*
1876 * Check type for recursive scan
1877 */
1878 switch(parent->bref.type) {
1879 case HAMMER2_BREF_TYPE_VOLUME:
1880 /* data already instantiated */
1881 break;
1882 case HAMMER2_BREF_TYPE_INODE:
1883 /*
1884 * Must instantiate data for DIRECTDATA test and also
1885 * for recursion.
1886 */
1887 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS);
1888 hammer2_chain_unlock(parent);
1889 if (parent->data->ipdata.op_flags & HAMMER2_OPFLAG_DIRECTDATA) {
1890 /* not applicable to recovery scan */
1891 return 0;
1892 }
1893 break;
1894 case HAMMER2_BREF_TYPE_INDIRECT:
1895 /*
1896 * Must instantiate data for recursion
1897 */
1898 hammer2_chain_lock(parent, HAMMER2_RESOLVE_ALWAYS);
1899 hammer2_chain_unlock(parent);
1900 break;
1901 case HAMMER2_BREF_TYPE_DATA:
1902 case HAMMER2_BREF_TYPE_FREEMAP:
1903 case HAMMER2_BREF_TYPE_FREEMAP_NODE:
1904 case HAMMER2_BREF_TYPE_FREEMAP_LEAF:
1905 /* not applicable to recovery scan */
1906 return 0;
1907 break;
1908 default:
1909 return EDOM;
1910 }
1911
1912 /*
1913 * Recursive scan of the last flushed transaction only. We are
1914 * doing this without pmp assignments so don't leave the chains
1915 * hanging around after we are done with them.
1916 */
1917 cache_index = 0;
1918 chain = hammer2_chain_scan(parent, NULL, &cache_index,
1919 HAMMER2_LOOKUP_NODATA);
1920 while (chain) {
1921 atomic_set_int(&chain->flags, HAMMER2_CHAIN_RELEASE);
8138a154 1922 if (chain->bref.mirror_tid >= hmp->voldata.alloc_tid - 1) {
10136ab6
MD
1923 error = hammer2_recovery_scan(trans, hmp, chain,
1924 list, depth + 1);
1925 if (error)
1926 cumulative_error = error;
1927 }
1928 chain = hammer2_chain_scan(parent, chain, &cache_index,
1929 HAMMER2_LOOKUP_NODATA);
1930 }
1931
1932 return cumulative_error;
1933}
1934
703720e4
MD
1935/*
1936 * Sync the entire filesystem; this is called from the filesystem syncer
1937 * process periodically and whenever a user calls sync(1) on the hammer
1938 * mountpoint.
1939 *
1940 * Currently is actually called from the syncer! \o/
1941 *
1942 * This task will have to snapshot the state of the dirty inode chain.
1943 * From that, it will have to make sure all of the inodes on the dirty
1944 * chain have IO initiated. We make sure that io is initiated for the root
1945 * block.
1946 *
1947 * If waitfor is set, we wait for media to acknowledge the new rootblock.
1948 *
1949 * THINKS: side A vs side B, to have sync not stall all I/O?
1950 */
e118c14f 1951int
066e00cc 1952hammer2_vfs_sync(struct mount *mp, int waitfor)
703720e4 1953{
b7926f31 1954 struct hammer2_sync_info info;
a7720be7 1955 hammer2_chain_t *chain;
a5913bdf 1956 hammer2_pfsmount_t *pmp;
54eb943b 1957 hammer2_mount_t *hmp;
b7926f31
MD
1958 int flags;
1959 int error;
7bed8d7e 1960 int total_error;
a7720be7 1961 int force_fchain;
3fc4c63d 1962 int i;
703720e4 1963
a5913bdf 1964 pmp = MPTOPMP(mp);
703720e4 1965
355d67fc
MD
1966 /*
1967 * We can't acquire locks on existing vnodes while in a transaction
1968 * without risking a deadlock. This assumes that vfsync() can be
1969 * called without the vnode locked (which it can in DragonFly).
1970 * Otherwise we'd have to implement a multi-pass or flag the lock
1971 * failures and retry.
c057466c
MD
1972 *
1973 * The reclamation code interlocks with the sync list's token
1974 * (by removing the vnode from the scan list) before unlocking
1975 * the inode, giving us time to ref the inode.
355d67fc
MD
1976 */
1977 /*flags = VMSC_GETVP;*/
1978 flags = 0;
b7926f31
MD
1979 if (waitfor & MNT_LAZY)
1980 flags |= VMSC_ONEPASS;
1981
d7bfb2cb 1982 /*
052e0aa0
MD
1983 * Start our flush transaction. This does not return until all
1984 * concurrent transactions have completed and will prevent any
1985 * new transactions from running concurrently, except for the
1986 * buffer cache transactions.
1987 *
1988 * For efficiency do an async pass before making sure with a
1989 * synchronous pass on all related buffer cache buffers. It
1990 * should theoretically not be possible for any new file buffers
1991 * to be instantiated during this sequence.
d7bfb2cb 1992 */
052e0aa0
MD
1993 hammer2_trans_init(&info.trans, pmp, NULL, HAMMER2_TRANS_ISFLUSH |
1994 HAMMER2_TRANS_PREFLUSH);
eae2ed61 1995 hammer2_run_unlinkq(&info.trans, pmp);
b7926f31
MD
1996 info.error = 0;
1997 info.waitfor = MNT_NOWAIT;
eddc656a 1998 vsyncscan(mp, flags | VMSC_NOWAIT, hammer2_sync_scan2, &info);
052e0aa0
MD
1999 info.waitfor = MNT_WAIT;
2000 vsyncscan(mp, flags, hammer2_sync_scan2, &info);
a7720be7 2001
052e0aa0
MD
2002 /*
2003 * Clear PREFLUSH. This prevents (or asserts on) any new logical
2004 * buffer cache flushes which occur during the flush. Device buffers
2005 * are not affected.
2006 */
2007
2008#if 0
b7926f31
MD
2009 if (info.error == 0 && (waitfor & MNT_WAIT)) {
2010 info.waitfor = waitfor;
eddc656a 2011 vsyncscan(mp, flags, hammer2_sync_scan2, &info);
b7926f31
MD
2012
2013 }
052e0aa0 2014#endif
a4dc31e0 2015 hammer2_bioq_sync(info.trans.pmp);
052e0aa0 2016 atomic_clear_int(&info.trans.flags, HAMMER2_TRANS_PREFLUSH);
a7720be7 2017
052e0aa0 2018#if 0
a7720be7 2019 /*
a4dc31e0 2020 * Start the flush transaction and flush all meta-data.
a7720be7 2021 */
10136ab6 2022 hammer2_trans_init(&info.trans, pmp, NULL, HAMMER2_TRANS_ISFLUSH);
052e0aa0 2023#endif
a5913bdf 2024
7bed8d7e 2025 total_error = 0;
58e43599
MD
2026 for (i = 0; pmp->iroot && i < pmp->iroot->cluster.nchains; ++i) {
2027 hmp = pmp->iroot->cluster.array[i]->hmp;
a5913bdf 2028
d7bfb2cb
MD
2029 /*
2030 * Media mounts have two 'roots', vchain for the topology
2031 * and fchain for the free block table. Flush both.
2032 *
2033 * Note that the topology and free block table are handled
2034 * independently, so the free block table can wind up being
2035 * ahead of the topology. We depend on the bulk free scan
2036 * code to deal with any loose ends.
2037 */
10136ab6
MD
2038#if 1
2039 hammer2_chain_lock(&hmp->fchain, HAMMER2_RESOLVE_ALWAYS);
8138a154
MD
2040 kprintf("sync tid test fmap %016jx %016jx\n",
2041 hmp->fchain.update_hi, hmp->voldata.freemap_tid);
10136ab6 2042 if ((hmp->fchain.flags & HAMMER2_CHAIN_MODIFIED) ||
8138a154 2043 hmp->fchain.update_hi > hmp->voldata.freemap_tid) {
10136ab6
MD
2044 /* this will also modify vchain as a side effect */
2045 chain = &hmp->fchain;
8138a154 2046 hammer2_flush(&info.trans, &chain);
10136ab6
MD
2047 KKASSERT(chain == &hmp->fchain);
2048 }
2049 hammer2_chain_unlock(&hmp->fchain);
2050#endif
2051
7bed8d7e 2052 hammer2_chain_lock(&hmp->vchain, HAMMER2_RESOLVE_ALWAYS);
8138a154
MD
2053 kprintf("sync tid test vmap %016jx %016jx\n",
2054 hmp->vchain.update_hi, hmp->voldata.mirror_tid);
a7720be7 2055 if ((hmp->vchain.flags & HAMMER2_CHAIN_MODIFIED) ||
8138a154 2056 hmp->vchain.update_hi > hmp->voldata.mirror_tid) {
a7720be7 2057 chain = &hmp->vchain;
8138a154 2058 hammer2_flush(&info.trans, &chain);
a7720be7
MD
2059 KKASSERT(chain == &hmp->vchain);
2060 force_fchain = 1;
2061 } else {
2062 force_fchain = 0;
7bed8d7e
MD
2063 }
2064 hammer2_chain_unlock(&hmp->vchain);
1a7cfe5a 2065
10136ab6 2066#if 0
7bed8d7e 2067 hammer2_chain_lock(&hmp->fchain, HAMMER2_RESOLVE_ALWAYS);
a7720be7 2068 if ((hmp->fchain.flags & HAMMER2_CHAIN_MODIFIED) ||
8138a154 2069 hmp->fchain.update_hi > hmp->voldata.freemap_tid ||
a7720be7 2070 force_fchain) {
d7bfb2cb 2071 /* this will also modify vchain as a side effect */
a7720be7 2072 chain = &hmp->fchain;
8138a154 2073 hammer2_flush(&info.trans, &chain);
a7720be7 2074 KKASSERT(chain == &hmp->fchain);
7bed8d7e
MD
2075 }
2076 hammer2_chain_unlock(&hmp->fchain);
10136ab6 2077#endif
1a7cfe5a 2078
7bed8d7e 2079 error = 0;
b7926f31 2080
2910a90c 2081 /*
7bed8d7e
MD
2082 * We can't safely flush the volume header until we have
2083 * flushed any device buffers which have built up.
2084 *
2085 * XXX this isn't being incremental
2910a90c 2086 */
7bed8d7e
MD
2087 vn_lock(hmp->devvp, LK_EXCLUSIVE | LK_RETRY);
2088 error = VOP_FSYNC(hmp->devvp, MNT_WAIT, 0);
2089 vn_unlock(hmp->devvp);
b7926f31 2090
2910a90c 2091 /*
7bed8d7e
MD
2092 * The flush code sets CHAIN_VOLUMESYNC to indicate that the
2093 * volume header needs synchronization via hmp->volsync.
2094 *
2095 * XXX synchronize the flag & data with only this flush XXX
2910a90c 2096 */
7bed8d7e
MD
2097 if (error == 0 &&
2098 (hmp->vchain.flags & HAMMER2_CHAIN_VOLUMESYNC)) {
2099 struct buf *bp;
2100
2101 /*
2102 * Synchronize the disk before flushing the volume
2103 * header.
2104 */
2105 bp = getpbuf(NULL);
2106 bp->b_bio1.bio_offset = 0;
2107 bp->b_bufsize = 0;
2108 bp->b_bcount = 0;
2109 bp->b_cmd = BUF_CMD_FLUSH;
2110 bp->b_bio1.bio_done = biodone_sync;
2111 bp->b_bio1.bio_flags |= BIO_SYNC;
2112 vn_strategy(hmp->devvp, &bp->b_bio1);
2113 biowait(&bp->b_bio1, "h2vol");
2114 relpbuf(bp, NULL);
2115
2116 /*
2117 * Then we can safely flush the version of the
2118 * volume header synchronized by the flush code.
2119 */
2120 i = hmp->volhdrno + 1;
2121 if (i >= HAMMER2_NUM_VOLHDRS)
2122 i = 0;
2123 if (i * HAMMER2_ZONE_BYTES64 + HAMMER2_SEGSIZE >
2124 hmp->volsync.volu_size) {
2125 i = 0;
2126 }
2127 kprintf("sync volhdr %d %jd\n",
2128 i, (intmax_t)hmp->volsync.volu_size);
2129 bp = getblk(hmp->devvp, i * HAMMER2_ZONE_BYTES64,
2130 HAMMER2_PBUFSIZE, 0, 0);
2131 atomic_clear_int(&hmp->vchain.flags,
2132 HAMMER2_CHAIN_VOLUMESYNC);
2133 bcopy(&hmp->volsync, bp->b_data, HAMMER2_PBUFSIZE);
2134 bawrite(bp);
2135 hmp->volhdrno = i;
3fc4c63d 2136 }
7bed8d7e
MD
2137 if (error)
2138 total_error = error;
b7926f31 2139 }
d001f460 2140 hammer2_trans_done(&info.trans);
a4dc31e0 2141
7bed8d7e 2142 return (total_error);
b7926f31 2143}
703720e4 2144
214f4a77
MD
2145/*
2146 * Sync passes.
214f4a77 2147 */
b7926f31
MD
2148static int
2149hammer2_sync_scan2(struct mount *mp, struct vnode *vp, void *data)
2150{
2151 struct hammer2_sync_info *info = data;
2152 hammer2_inode_t *ip;
2153 int error;
2154
c057466c
MD
2155 /*
2156 *
2157 */
b7926f31 2158 ip = VTOI(vp);
eddc656a
MD
2159 if (ip == NULL)
2160 return(0);
2161 if (vp->v_type == VNON || vp->v_type == VBAD) {
2162 vclrisdirty(vp);
2163 return(0);
2164 }
2165 if ((ip->flags & HAMMER2_INODE_MODIFIED) == 0 &&
2166 RB_EMPTY(&vp->v_rbdirty_tree)) {
2167 vclrisdirty(vp);
b7926f31
MD
2168 return(0);
2169 }
d001f460
MD
2170
2171 /*
2172 * VOP_FSYNC will start a new transaction so replicate some code
2173 * here to do it inline (see hammer2_vop_fsync()).
355d67fc
MD
2174 *
2175 * WARNING: The vfsync interacts with the buffer cache and might
2176 * block, we can't hold the inode lock at that time.
c057466c
MD
2177 * However, we MUST ref ip before blocking to ensure that
2178 * it isn't ripped out from under us (since we do not
2179 * hold a lock on the vnode).
d001f460 2180 */
c057466c 2181 hammer2_inode_ref(ip);
9596b8c4 2182 atomic_clear_int(&ip->flags, HAMMER2_INODE_MODIFIED);
c057466c
MD
2183 if (vp)
2184 vfsync(vp, MNT_NOWAIT, 1, NULL, NULL);
a7720be7 2185
c057466c 2186 hammer2_inode_drop(ip);
052e0aa0 2187#if 1
d001f460 2188 error = 0;
b7926f31
MD
2189 if (error)
2190 info->error = error;
052e0aa0 2191#endif
b7926f31 2192 return(0);
703720e4
MD
2193}
2194
e118c14f
MD
2195static
2196int
066e00cc 2197hammer2_vfs_vptofh(struct vnode *vp, struct fid *fhp)
703720e4
MD
2198{
2199 return (0);
2200}
2201
e118c14f
MD
2202static
2203int
066e00cc 2204hammer2_vfs_fhtovp(struct mount *mp, struct vnode *rootvp,
703720e4
MD
2205 struct fid *fhp, struct vnode **vpp)
2206{
2207 return (0);
2208}
2209
e118c14f
MD
2210static
2211int
066e00cc 2212hammer2_vfs_checkexp(struct mount *mp, struct sockaddr *nam,
703720e4
MD
2213 int *exflagsp, struct ucred **credanonp)
2214{
2215 return (0);
2216}
50e4f8f4
MD
2217
2218/*
2219 * Support code for hammer2_mount(). Read, verify, and install the volume
2220 * header into the HMP
2221 *
2222 * XXX read four volhdrs and use the one with the highest TID whos CRC
2223 * matches.
2224 *
2225 * XXX check iCRCs.
57381c9e
VS
2226 *
2227 * XXX For filesystems w/ less than 4 volhdrs, make sure to not write to
2228 * nonexistant locations.
2229 *
2230 * XXX Record selected volhdr and ring updates to each of 4 volhdrs
50e4f8f4
MD
2231 */
2232static
2233int
2234hammer2_install_volume_header(hammer2_mount_t *hmp)
2235{
2236 hammer2_volume_data_t *vd;
99924359 2237 struct buf *bp;
60fbd5f4 2238 hammer2_crc32_t crc0, crc, bcrc0, bcrc;
99924359
MD
2239 int error_reported;
2240 int error;
0b3147ba
VS
2241 int valid;
2242 int i;
50e4f8f4 2243
99924359 2244 error_reported = 0;
0b3147ba
VS
2245 error = 0;
2246 valid = 0;
99924359 2247 bp = NULL;
0b3147ba 2248
99924359
MD
2249 /*
2250 * There are up to 4 copies of the volume header (syncs iterate
2251 * between them so there is no single master). We don't trust the
2252 * volu_size field so we don't know precisely how large the filesystem
2253 * is, so depend on the OS to return an error if we go beyond the
2254 * block device's EOF.
2255 */
0b3147ba 2256 for (i = 0; i < HAMMER2_NUM_VOLHDRS; i++) {
62efe6ec 2257 error = bread(hmp->devvp, i * HAMMER2_ZONE_BYTES64,
99924359 2258 HAMMER2_VOLUME_BYTES, &bp);
0b3147ba 2259 if (error) {
99924359
MD
2260 brelse(bp);
2261 bp = NULL;
0b3147ba
VS
2262 continue;
2263 }
2264
88a032af
VS
2265 vd = (struct hammer2_volume_data *) bp->b_data;
2266 if ((vd->magic != HAMMER2_VOLUME_ID_HBO) &&
2267 (vd->magic != HAMMER2_VOLUME_ID_ABO)) {
2268 brelse(bp);
2269 bp = NULL;
0b3147ba 2270 continue;
88a032af
VS
2271 }
2272
2273 if (vd->magic == HAMMER2_VOLUME_ID_ABO) {
2274 /* XXX: Reversed-endianness filesystem */
2275 kprintf("hammer2: reverse-endian filesystem detected");
2276 brelse(bp);
2277 bp = NULL;
2278 continue;
2279 }
0b3147ba
VS
2280
2281 crc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT0];
60fbd5f4 2282 crc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC0_OFF,
99924359 2283 HAMMER2_VOLUME_ICRC0_SIZE);
60fbd5f4
VS
2284 bcrc = vd->icrc_sects[HAMMER2_VOL_ICRC_SECT1];
2285 bcrc0 = hammer2_icrc32(bp->b_data + HAMMER2_VOLUME_ICRC1_OFF,
2286 HAMMER2_VOLUME_ICRC1_SIZE);
2287 if ((crc0 != crc) || (bcrc0 != bcrc)) {
99924359 2288 kprintf("hammer2 volume header crc "
3fc4c63d 2289 "mismatch copy #%d %08x/%08x\n",
60fbd5f4 2290 i, crc0, crc);
99924359
MD
2291 error_reported = 1;
2292 brelse(bp);
2293 bp = NULL;
0b3147ba
VS
2294 continue;
2295 }
4d5318eb 2296 if (valid == 0 || hmp->voldata.mirror_tid < vd->mirror_tid) {
99924359
MD
2297 valid = 1;
2298 hmp->voldata = *vd;
3fc4c63d 2299 hmp->volhdrno = i;
0b3147ba 2300 }
99924359
MD
2301 brelse(bp);
2302 bp = NULL;
50e4f8f4 2303 }
0b3147ba 2304 if (valid) {
3fc4c63d 2305 hmp->volsync = hmp->voldata;
57381c9e 2306 error = 0;
3fc4c63d
MD
2307 if (error_reported || bootverbose || 1) { /* 1/DEBUG */
2308 kprintf("hammer2: using volume header #%d\n",
2309 hmp->volhdrno);
2310 }
0b3147ba
VS
2311 } else {
2312 error = EINVAL;
99924359 2313 kprintf("hammer2: no valid volume headers found!\n");
0b3147ba 2314 }
0b3147ba 2315 return (error);
50e4f8f4 2316}
0b3147ba 2317
1a34728c
MD
2318/*
2319 * Reconnect using the passed file pointer. The caller must ref the
2320 * fp for us.
2321 */
2322void
2323hammer2_cluster_reconnect(hammer2_pfsmount_t *pmp, struct file *fp)
2324{
6a5f4fe6 2325 const hammer2_inode_data_t *ipdata;
278ab2b2 2326 hammer2_cluster_t *cparent;
a5913bdf 2327 hammer2_mount_t *hmp;
3a5aa68f 2328 size_t name_len;
70c3c3b7 2329
58e43599 2330 hmp = pmp->iroot->cluster.focus->hmp; /* XXX */
a5913bdf 2331
70c3c3b7 2332 /*
3a5aa68f
MD
2333 * Closes old comm descriptor, kills threads, cleans up
2334 * states, then installs the new descriptor and creates
2335 * new threads.
1a34728c 2336 */
3a5aa68f 2337 kdmsg_iocom_reconnect(&pmp->iocom, fp, "hammer2");
bfc3a7b1 2338
9b8b748f 2339 /*
537d97bc
MD
2340 * Setup LNK_CONN fields for autoinitiated state machine
2341 */
278ab2b2
MD
2342 cparent = hammer2_inode_lock_ex(pmp->iroot);
2343 ipdata = &hammer2_cluster_data(cparent)->ipdata;
476d2aad
MD
2344 pmp->iocom.auto_lnk_conn.pfs_clid = ipdata->pfs_clid;
2345 pmp->iocom.auto_lnk_conn.pfs_fsid = ipdata->pfs_fsid;
2346 pmp->iocom.auto_lnk_conn.pfs_type = ipdata->pfs_type;
537d97bc 2347 pmp->iocom.auto_lnk_conn.proto_version = DMSG_SPAN_PROTO_1;
a5913bdf 2348 pmp->iocom.auto_lnk_conn.peer_type = hmp->voldata.peer_type;
537d97bc
MD
2349
2350 /*
2351 * Filter adjustment. Clients do not need visibility into other
2352 * clients (otherwise millions of clients would present a serious
2353 * problem). The fs_label also serves to restrict the namespace.
9b8b748f 2354 */
537d97bc
MD
2355 pmp->iocom.auto_lnk_conn.peer_mask = 1LLU << HAMMER2_PEER_HAMMER2;
2356 pmp->iocom.auto_lnk_conn.pfs_mask = (uint64_t)-1;
476d2aad 2357 switch (ipdata->pfs_type) {
537d97bc
MD
2358 case DMSG_PFSTYPE_CLIENT:
2359 pmp->iocom.auto_lnk_conn.peer_mask &=
2360 ~(1LLU << DMSG_PFSTYPE_CLIENT);
2361 break;
2362 default:
2363 break;
2364 }
2365
476d2aad 2366 name_len = ipdata->name_len;
537d97bc
MD
2367 if (name_len >= sizeof(pmp->iocom.auto_lnk_conn.fs_label))
2368 name_len = sizeof(pmp->iocom.auto_lnk_conn.fs_label) - 1;
476d2aad 2369 bcopy(ipdata->filename,
537d97bc 2370 pmp->iocom.auto_lnk_conn.fs_label,
ddfbb283 2371 name_len);
537d97bc
MD
2372 pmp->iocom.auto_lnk_conn.fs_label[name_len] = 0;
2373
2374 /*
2375 * Setup LNK_SPAN fields for autoinitiated state machine
2376 */
476d2aad
MD
2377 pmp->iocom.auto_lnk_span.pfs_clid = ipdata->pfs_clid;
2378 pmp->iocom.auto_lnk_span.pfs_fsid = ipdata->pfs_fsid;
2379 pmp->iocom.auto_lnk_span.pfs_type = ipdata->pfs_type;
a5913bdf 2380 pmp->iocom.auto_lnk_span.peer_type = hmp->voldata.peer_type;
537d97bc 2381 pmp->iocom.auto_lnk_span.proto_version = DMSG_SPAN_PROTO_1;
476d2aad 2382 name_len = ipdata->name_len;
537d97bc
MD
2383 if (name_len >= sizeof(pmp->iocom.auto_lnk_span.fs_label))
2384 name_len = sizeof(pmp->iocom.auto_lnk_span.fs_label) - 1;
476d2aad 2385 bcopy(ipdata->filename,
537d97bc
MD
2386 pmp->iocom.auto_lnk_span.fs_label,
2387 name_len);
2388 pmp->iocom.auto_lnk_span.fs_label[name_len] = 0;
278ab2b2 2389 hammer2_inode_unlock_ex(pmp->iroot, cparent);
537d97bc
MD
2390
2391 kdmsg_iocom_autoinitiate(&pmp->iocom, hammer2_autodmsg);
70c3c3b7
MD
2392}
2393
9b8b748f 2394static int
537d97bc 2395hammer2_rcvdmsg(kdmsg_msg_t *msg)
9b8b748f 2396{
5bc5bca2 2397 switch(msg->any.head.cmd & DMSGF_TRANSMASK) {
537d97bc 2398 case DMSG_DBG_SHELL:
10c86c4e 2399 /*
ea7c725f 2400 * (non-transaction)
537d97bc 2401 * Execute shell command (not supported atm)
10c86c4e 2402 */
537d97bc 2403 kdmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
8c280d5d 2404 break;
537d97bc 2405 case DMSG_DBG_SHELL | DMSGF_REPLY:
ea7c725f
MD
2406 /*
2407 * (non-transaction)
2408 */
537d97bc
MD
2409 if (msg->aux_data) {
2410 msg->aux_data[msg->aux_size - 1] = 0;
2411 kprintf("HAMMER2 DBG: %s\n", msg->aux_data);
2412 }
8c280d5d
MD
2413 break;
2414 default:
3b76886b 2415 /*
ea7c725f 2416 * Unsupported message received. We only need to
3b76886b
MD
2417 * reply if it's a transaction in order to close our end.
2418 * Ignore any one-way messages are any further messages
2419 * associated with the transaction.
2420 *
2421 * NOTE: This case also includes DMSG_LNK_ERROR messages
2422 * which might be one-way, replying to those would
2423 * cause an infinite ping-pong.
2424 */
2425 if (msg->any.head.cmd & DMSGF_CREATE)
2426 kdmsg_msg_reply(msg, DMSG_ERR_NOSUPP);
8c280d5d
MD
2427 break;
2428 }
2429 return(0);
2430}
2431
2432/*
537d97bc
MD
2433 * This function is called after KDMSG has automatically handled processing
2434 * of a LNK layer message (typically CONN, SPAN, or CIRC).
8c280d5d 2435 *
537d97bc
MD
2436 * We tag off the LNK_CONN to trigger our LNK_VOLCONF messages which
2437 * advertises all available hammer2 super-root volumes.
8c280d5d 2438 */
537d97bc
MD
2439static void
2440hammer2_autodmsg(kdmsg_msg_t *msg)
8c280d5d 2441{
537d97bc 2442 hammer2_pfsmount_t *pmp = msg->iocom->handle;
58e43599 2443 hammer2_mount_t *hmp = pmp->iroot->cluster.focus->hmp; /* XXX */
1a34728c 2444 int copyid;
8c280d5d 2445
537d97bc
MD
2446 /*
2447 * We only care about replies to our LNK_CONN auto-request. kdmsg
2448 * has already processed the reply, we use this calback as a shim
2449 * to know when we can advertise available super-root volumes.
2450 */
2451 if ((msg->any.head.cmd & DMSGF_TRANSMASK) !=
2452 (DMSG_LNK_CONN | DMSGF_CREATE | DMSGF_REPLY) ||
2453 msg->state == NULL) {
2454 return;
2455 }
2456
70c3c3b7
MD
2457 kprintf("LNK_CONN REPLY RECEIVED CMD %08x\n", msg->any.head.cmd);
2458
5bc5bca2 2459 if (msg->any.head.cmd & DMSGF_CREATE) {
537d97bc 2460 kprintf("HAMMER2: VOLDATA DUMP\n");
1a34728c
MD
2461
2462 /*
2463 * Dump the configuration stored in the volume header
2464 */
2465 hammer2_voldata_lock(hmp);
2466 for (copyid = 0; copyid < HAMMER2_COPYID_COUNT; ++copyid) {
2467 if (hmp->voldata.copyinfo[copyid].copyid == 0)
2468 continue;
2469 hammer2_volconf_update(pmp, copyid);
2470 }
0dea3156 2471 hammer2_voldata_unlock(hmp, 0);
8c280d5d 2472 }
537d97bc
MD
2473 if ((msg->any.head.cmd & DMSGF_DELETE) &&
2474 msg->state && (msg->state->txcmd & DMSGF_DELETE) == 0) {
2475 kprintf("HAMMER2: CONN WAS TERMINATED\n");
8c280d5d 2476 }
9b8b748f 2477}
1a34728c
MD
2478
2479/*
2480 * Volume configuration updates are passed onto the userland service
2481 * daemon via the open LNK_CONN transaction.
2482 */
2483void
2484hammer2_volconf_update(hammer2_pfsmount_t *pmp, int index)
2485{
58e43599 2486 hammer2_mount_t *hmp = pmp->iroot->cluster.focus->hmp; /* XXX */
3a5aa68f 2487 kdmsg_msg_t *msg;
1a34728c
MD
2488
2489 /* XXX interlock against connection state termination */
3a5aa68f
MD
2490 kprintf("volconf update %p\n", pmp->iocom.conn_state);
2491 if (pmp->iocom.conn_state) {
1a34728c 2492 kprintf("TRANSMIT VOLCONF VIA OPEN CONN TRANSACTION\n");
ea7c725f
MD
2493 msg = kdmsg_msg_alloc_state(pmp->iocom.conn_state,
2494 DMSG_LNK_VOLCONF, NULL, NULL);
1a34728c
MD
2495 msg->any.lnk_volconf.copy = hmp->voldata.copyinfo[index];
2496 msg->any.lnk_volconf.mediaid = hmp->voldata.fsid;
2497 msg->any.lnk_volconf.index = index;
3a5aa68f 2498 kdmsg_msg_write(msg);
1a34728c
MD
2499 }
2500}
9797e933 2501
3f5b8b3b
MD
2502/*
2503 * This handles hysteresis on regular file flushes. Because the BIOs are
2504 * routed to a thread it is possible for an excessive number to build up
2505 * and cause long front-end stalls long before the runningbuffspace limit
2506 * is hit, so we implement hammer2_flush_pipe to control the
2507 * hysteresis.
2508 *
2509 * This is a particular problem when compression is used.
2510 */
2511void
2512hammer2_lwinprog_ref(hammer2_pfsmount_t *pmp)
2513{
2514 atomic_add_int(&pmp->count_lwinprog, 1);
2515}
2516
2517void
2518hammer2_lwinprog_drop(hammer2_pfsmount_t *pmp)
2519{
2520 int lwinprog;
2521
2522 lwinprog = atomic_fetchadd_int(&pmp->count_lwinprog, -1);
2523 if ((lwinprog & HAMMER2_LWINPROG_WAITING) &&
2524 (lwinprog & HAMMER2_LWINPROG_MASK) <= hammer2_flush_pipe * 2 / 3) {
2525 atomic_clear_int(&pmp->count_lwinprog,
2526 HAMMER2_LWINPROG_WAITING);
2527 wakeup(&pmp->count_lwinprog);
2528 }
2529}
2530
2531void
2532hammer2_lwinprog_wait(hammer2_pfsmount_t *pmp)
2533{
2534 int lwinprog;
2535
2536 for (;;) {
2537 lwinprog = pmp->count_lwinprog;
2538 cpu_ccfence();
2539 if ((lwinprog & HAMMER2_LWINPROG_MASK) < hammer2_flush_pipe)
2540 break;
2541 tsleep_interlock(&pmp->count_lwinprog, 0);
2542 atomic_set_int(&pmp->count_lwinprog, HAMMER2_LWINPROG_WAITING);
2543 lwinprog = pmp->count_lwinprog;
2544 if ((lwinprog & HAMMER2_LWINPROG_MASK) < hammer2_flush_pipe)
2545 break;
2546 tsleep(&pmp->count_lwinprog, PINTERLOCKED, "h2wpipe", hz);
2547 }
2548}
2549
278ab2b2
MD
2550/*
2551 * Manage excessive memory resource use for chain and related
2552 * structures.
2553 */
2554void
2555hammer2_pfs_memory_wait(hammer2_pfsmount_t *pmp)
2556{
2557 long waiting;
2558 long count;
2559 long limit;
2560#if 0
2561 static int zzticks;
2562#endif
2563
2564 /*
2565 * Atomic check condition and wait. Also do an early speedup of
2566 * the syncer to try to avoid hitting the wait.
2567 */
2568 for (;;) {
2569 waiting = pmp->inmem_dirty_chains;
2570 cpu_ccfence();
2571 count = waiting & HAMMER2_DIRTYCHAIN_MASK;
2572
2573 limit = pmp->mp->mnt_nvnodelistsize / 10;
2574 if (limit < hammer2_limit_dirty_chains)
2575 limit = hammer2_limit_dirty_chains;
2576 if (limit < 1000)
2577 limit = 1000;
2578
2579#if 0
2580 if ((int)(ticks - zzticks) > hz) {
2581 zzticks = ticks;
2582 kprintf("count %ld %ld\n", count, limit);
2583 }
2584#endif
2585
2586 /*
2587 * Block if there are too many dirty chains present, wait
2588 * for the flush to clean some out.
2589 */
2590 if (count > limit) {
2591 tsleep_interlock(&pmp->inmem_dirty_chains, 0);
2592 if (atomic_cmpset_long(&pmp->inmem_dirty_chains,
2593 waiting,
2594 waiting | HAMMER2_DIRTYCHAIN_WAITING)) {
2595 speedup_syncer(pmp->mp);
2596 tsleep(&pmp->inmem_dirty_chains, PINTERLOCKED,
2597 "chnmem", hz);
2598 }
2599 continue; /* loop on success or fail */
2600 }
2601
2602 /*
2603 * Try to start an early flush before we are forced to block.
2604 */
2605 if (count > limit * 7 / 10)
2606 speedup_syncer(pmp->mp);
2607 break;
2608 }
2609}
2610
2611void
2612hammer2_pfs_memory_inc(hammer2_pfsmount_t *pmp)
2613{
2614 if (pmp)
2615 atomic_add_long(&pmp->inmem_dirty_chains, 1);
2616}
2617
2618void
2619hammer2_pfs_memory_wakeup(hammer2_pfsmount_t *pmp)
2620{
2621 long waiting;
2622
2623 if (pmp == NULL)
2624 return;
2625
2626 for (;;) {
2627 waiting = pmp->inmem_dirty_chains;
2628 cpu_ccfence();
2629 if (atomic_cmpset_long(&pmp->inmem_dirty_chains,
2630 waiting,
2631 (waiting - 1) &
2632 ~HAMMER2_DIRTYCHAIN_WAITING)) {
2633 break;
2634 }
2635 }
2636
2637 if (waiting & HAMMER2_DIRTYCHAIN_WAITING)
2638 wakeup(&pmp->inmem_dirty_chains);
2639}
2640
2641/*
2642 * Debugging
2643 */
9797e933 2644void
8138a154 2645hammer2_dump_chain(hammer2_chain_t *chain, int tab, int *countp, char pfx)
9797e933
MD
2646{
2647 hammer2_chain_t *scan;
1897c66e 2648 hammer2_chain_t *first_parent;
9797e933 2649
a02dfba1
MD
2650 --*countp;
2651 if (*countp == 0) {
2652 kprintf("%*.*s...\n", tab, tab, "");
2653 return;
2654 }
2655 if (*countp < 0)
2656 return;
1897c66e 2657 first_parent = chain->core ? TAILQ_FIRST(&chain->core->ownerq) : NULL;
8138a154
MD
2658 kprintf("%*.*s%c-chain %p.%d %016jx/%d mir=%016jx\n",
2659 tab, tab, "", pfx,
a7720be7
MD
2660 chain, chain->bref.type,
2661 chain->bref.key, chain->bref.keybits,
2662 chain->bref.mirror_tid);
2663
8138a154
MD
2664 kprintf("%*.*s [%08x] (%s) mod=%016jx del=%016jx "
2665 "lo=%08jx hi=%08jx refs=%d\n",
a7720be7
MD
2666 tab, tab, "",
2667 chain->flags,
9797e933
MD
2668 ((chain->bref.type == HAMMER2_BREF_TYPE_INODE &&
2669 chain->data) ? (char *)chain->data->ipdata.filename : "?"),
8138a154 2670 chain->modify_tid,
a7720be7 2671 chain->delete_tid,
8138a154
MD
2672 chain->update_lo,
2673 chain->update_hi,
8853dfb5 2674 chain->refs);
a7720be7 2675
8138a154 2676 kprintf("%*.*s core %p [%08x]",
a7720be7 2677 tab, tab, "",
8138a154 2678 chain->core, (chain->core ? chain->core->flags : 0));
a7720be7 2679
1897c66e 2680 if (first_parent)
8138a154
MD
2681 kprintf("\n%*.*s fp=%p np=%p [fpflags %08x fprefs %d",
2682 tab, tab, "",
2683 first_parent,
2684 (first_parent ? TAILQ_NEXT(first_parent, core_entry) :
2685 NULL),
1897c66e
MD
2686 first_parent->flags,
2687 first_parent->refs);
8138a154 2688 if (chain->core == NULL || RB_EMPTY(&chain->core->rbtree))
9797e933
MD
2689 kprintf("\n");
2690 else
2691 kprintf(" {\n");
a7720be7 2692 if (chain->core) {
8138a154
MD
2693 RB_FOREACH(scan, hammer2_chain_tree, &chain->core->rbtree)
2694 hammer2_dump_chain(scan, tab + 4, countp, 'a');
2695 RB_FOREACH(scan, hammer2_chain_tree, &chain->core->dbtree)
2696 hammer2_dump_chain(scan, tab + 4, countp, 'r');
2697 TAILQ_FOREACH(scan, &chain->core->dbq, db_entry)
2698 hammer2_dump_chain(scan, tab + 4, countp, 'd');
9797e933 2699 }
8138a154 2700 if (chain->core && !RB_EMPTY(&chain->core->rbtree)) {
9797e933
MD
2701 if (chain->bref.type == HAMMER2_BREF_TYPE_INODE && chain->data)
2702 kprintf("%*.*s}(%s)\n", tab, tab, "",
2703 chain->data->ipdata.filename);
2704 else
2705 kprintf("%*.*s}\n", tab, tab, "");
2706 }
2707}