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