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