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