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