hammer2 - Performance work
[dragonfly.git] / sys / vfs / hammer2 / hammer2.h
CommitLineData
703720e4 1/*
40498d1c 2 * Copyright (c) 2011-2018 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@dragonflybsd.org>
6 * by Venkatesh Srinivas <vsrinivas@dragonflybsd.org>
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 */
35
36/*
278ab2b2
MD
37 * HAMMER2 IN-MEMORY CACHE OF MEDIA STRUCTURES
38 *
703720e4
MD
39 * This header file contains structures used internally by the HAMMER2
40 * implementation. See hammer2_disk.h for on-disk structures.
278ab2b2
MD
41 *
42 * There is an in-memory representation of all on-media data structure.
da6f36f4
MD
43 * Almost everything is represented by a hammer2_chain structure in-memory.
44 * Other higher-level structures typically map to chains.
278ab2b2
MD
45 *
46 * A great deal of data is accessed simply via its buffer cache buffer,
da6f36f4
MD
47 * which is mapped for the duration of the chain's lock. Hammer2 must
48 * implement its own buffer cache layer on top of the system layer to
49 * allow for different threads to lock different sub-block-sized buffers.
278ab2b2
MD
50 *
51 * When modifications are made to a chain a new filesystem block must be
da6f36f4
MD
52 * allocated. Multiple modifications do not typically allocate new blocks
53 * until the current block has been flushed. Flushes do not block the
54 * front-end unless the front-end operation crosses the current inode being
55 * flushed.
278ab2b2
MD
56 *
57 * The in-memory representation may remain cached (for example in order to
58 * placemark clustering locks) even after the related data has been
59 * detached.
703720e4
MD
60 */
61
62#ifndef _VFS_HAMMER2_HAMMER2_H_
63#define _VFS_HAMMER2_HAMMER2_H_
64
fae225dc 65#ifdef _KERNEL
703720e4 66#include <sys/param.h>
fae225dc 67#endif
703720e4 68#include <sys/types.h>
fae225dc 69#ifdef _KERNEL
703720e4 70#include <sys/kernel.h>
fae225dc 71#endif
703720e4 72#include <sys/conf.h>
fae225dc 73#ifdef _KERNEL
703720e4 74#include <sys/systm.h>
fae225dc 75#endif
703720e4
MD
76#include <sys/tree.h>
77#include <sys/malloc.h>
78#include <sys/mount.h>
79#include <sys/vnode.h>
80#include <sys/proc.h>
81#include <sys/mountctl.h>
82#include <sys/priv.h>
83#include <sys/stat.h>
46558838 84#include <sys/thread.h>
703720e4
MD
85#include <sys/globaldata.h>
86#include <sys/lockf.h>
87#include <sys/buf.h>
88#include <sys/queue.h>
89#include <sys/limits.h>
5bc5bca2 90#include <sys/dmsg.h>
355d67fc 91#include <sys/mutex.h>
fae225dc 92#ifdef _KERNEL
278ab2b2 93#include <sys/kern_syscall.h>
fae225dc 94#endif
278ab2b2 95
fae225dc 96#ifdef _KERNEL
94491fa0 97#include <sys/signal2.h>
278ab2b2 98#include <sys/buf2.h>
355d67fc 99#include <sys/mutex2.h>
fae225dc 100#endif
703720e4 101
7d565a4f 102#include "hammer2_xxhash.h"
703720e4
MD
103#include "hammer2_disk.h"
104#include "hammer2_mount.h"
2910a90c 105#include "hammer2_ioctl.h"
703720e4 106
bca9f8e6 107struct hammer2_io;
5c23d7f1 108struct hammer2_chain;
278ab2b2 109struct hammer2_cluster;
703720e4 110struct hammer2_inode;
d0755e6d 111struct hammer2_depend;
506bd6d1
MD
112struct hammer2_dev;
113struct hammer2_pfs;
10c86c4e 114struct hammer2_span;
26bf1a36 115struct hammer2_msg;
2ed4fece 116struct hammer2_thread;
c847e838 117union hammer2_xop;
703720e4 118
94491fa0
MD
119/*
120 * Mutex and lock shims. Hammer2 requires support for asynchronous and
121 * abortable locks, and both exclusive and shared spinlocks. Normal
122 * synchronous non-abortable locks can be substituted for spinlocks.
123 */
124typedef mtx_t hammer2_mtx_t;
125typedef mtx_link_t hammer2_mtx_link_t;
126typedef mtx_state_t hammer2_mtx_state_t;
127
128typedef struct spinlock hammer2_spin_t;
129
130#define hammer2_mtx_ex mtx_lock_ex_quick
68b321c1 131#define hammer2_mtx_ex_try mtx_lock_ex_try
94491fa0 132#define hammer2_mtx_sh mtx_lock_sh_quick
2efe493b 133#define hammer2_mtx_sh_again mtx_lock_sh_again
68b321c1 134#define hammer2_mtx_sh_try mtx_lock_sh_try
94491fa0 135#define hammer2_mtx_unlock mtx_unlock
2efe493b 136#define hammer2_mtx_downgrade mtx_downgrade
94491fa0
MD
137#define hammer2_mtx_owned mtx_owned
138#define hammer2_mtx_init mtx_init
139#define hammer2_mtx_temp_release mtx_lock_temp_release
140#define hammer2_mtx_temp_restore mtx_lock_temp_restore
141#define hammer2_mtx_refs mtx_lockrefs
142
143#define hammer2_spin_init spin_init
144#define hammer2_spin_sh spin_lock_shared
145#define hammer2_spin_ex spin_lock
146#define hammer2_spin_unsh spin_unlock_shared
147#define hammer2_spin_unex spin_unlock
148
c847e838 149TAILQ_HEAD(hammer2_xop_list, hammer2_xop_head);
a7b81974 150TAILQ_HEAD(hammer2_chain_list, hammer2_chain);
c847e838
MD
151
152typedef struct hammer2_xop_list hammer2_xop_list_t;
153
fae225dc 154#ifdef _KERNEL
94491fa0
MD
155/*
156 * General lock support
157 */
158static __inline
159int
b6b260f2 160hammer2_mtx_upgrade_try(hammer2_mtx_t *mtx)
94491fa0 161{
b6b260f2 162 return mtx_upgrade_try(mtx);
94491fa0
MD
163}
164
fae225dc
MD
165#endif
166
50456506
MD
167/*
168 * The xid tracks internal transactional updates.
169 *
170 * XXX fix-me, really needs to be 64-bits
171 */
172typedef uint32_t hammer2_xid_t;
173
0fe46dc6
MD
174#define HAMMER2_XID_MIN 0x00000000U
175#define HAMMER2_XID_MAX 0x7FFFFFFFU
176
d2a41023
MD
177/*
178 * Cap the dynamic calculation for the maximum number of dirty
179 * chains and dirty inodes allowed.
180 */
181#define HAMMER2_LIMIT_DIRTY_CHAINS (1024*1024)
182#define HAMMER2_LIMIT_DIRTY_INODES (65536)
50456506 183
50e4f8f4 184/*
1897c66e
MD
185 * The chain structure tracks a portion of the media topology from the
186 * root (volume) down. Chains represent volumes, inodes, indirect blocks,
187 * data blocks, and freemap nodes and leafs.
188 *
da6f36f4
MD
189 * The chain structure utilizes a simple singly-homed topology and the
190 * chain's in-memory topology will move around as the chains do, due mainly
191 * to renames and indirect block creation.
192 *
193 * Block Table Updates
1897c66e 194 *
da6f36f4
MD
195 * Block table updates for insertions and updates are delayed until the
196 * flush. This allows us to avoid having to modify the parent chain
197 * all the way to the root.
1897c66e 198 *
da6f36f4
MD
199 * Block table deletions are performed immediately (modifying the parent
200 * in the process) because the flush code uses the chain structure to
201 * track delayed updates and the chain will be (likely) gone or moved to
202 * another location in the topology after a deletion.
8138a154 203 *
da6f36f4
MD
204 * A prior iteration of the code tried to keep the relationship intact
205 * on deletes by doing a delete-duplicate operation on the chain, but
206 * it added way too much complexity to the codebase.
8138a154 207 *
da6f36f4 208 * Flush Synchronization
8138a154 209 *
da6f36f4
MD
210 * The flush code must flush modified chains bottom-up. Because chain
211 * structures can shift around and are NOT topologically stable,
212 * modified chains are independently indexed for the flush. As the flush
213 * runs it modifies (or further modifies) and updates the parents,
214 * propagating the flush all the way to the volume root.
5c23d7f1 215 *
da6f36f4
MD
216 * Modifying front-end operations can occur during a flush but will block
217 * in two cases: (1) when the front-end tries to operate on the inode
218 * currently in the midst of being flushed and (2) if the front-end
219 * crosses an inode currently being flushed (such as during a rename).
220 * So, for example, if you rename directory "x" to "a/b/c/d/e/f/g/x" and
221 * the flusher is currently working on "a/b/c", the rename will block
222 * temporarily in order to ensure that "x" exists in one place or the
223 * other.
0dea3156 224 *
da6f36f4
MD
225 * Meta-data statistics are updated by the flusher. The front-end will
226 * make estimates but meta-data must be fully synchronized only during a
227 * flush in order to ensure that it remains correct across a crash.
0dea3156 228 *
da6f36f4
MD
229 * Multiple flush synchronizations can theoretically be in-flight at the
230 * same time but the implementation is not coded to handle the case and
231 * currently serializes them.
0dea3156 232 *
da6f36f4 233 * Snapshots:
0dea3156 234 *
da6f36f4
MD
235 * Snapshots currently require the subdirectory tree being snapshotted
236 * to be flushed. The snapshot then creates a new super-root inode which
237 * copies the flushed blockdata of the directory or file that was
238 * snapshotted.
0dea3156 239 *
da6f36f4 240 * RBTREE NOTES:
0dea3156 241 *
da6f36f4
MD
242 * - Note that the radix tree runs in powers of 2 only so sub-trees
243 * cannot straddle edges.
50e4f8f4 244 */
ecc33e71 245RB_HEAD(hammer2_chain_tree, hammer2_chain);
da6f36f4 246TAILQ_HEAD(h2_flush_list, hammer2_chain);
1897c66e 247TAILQ_HEAD(h2_core_list, hammer2_chain);
1897c66e 248
8138a154
MD
249#define CHAIN_CORE_DELETE_BMAP_ENTRIES \
250 (HAMMER2_PBUFSIZE / sizeof(hammer2_blockref_t) / sizeof(uint32_t))
5c23d7f1 251
68b321c1
MD
252struct hammer2_reptrack {
253 hammer2_spin_t spin;
254 struct hammer2_reptrack *next;
255 struct hammer2_chain *chain;
256};
257
f1c7c224
MD
258/*
259 * Core topology for chain (embedded in chain). Protected by a spinlock.
260 */
0dea3156 261struct hammer2_chain_core {
94491fa0 262 hammer2_spin_t spin;
68b321c1 263 struct hammer2_reptrack *reptrack;
da6f36f4 264 struct hammer2_chain_tree rbtree; /* sub-chains */
c057466c 265 int live_zero; /* blockref array opt */
1897c66e 266 u_int live_count; /* live (not deleted) chains in tree */
8138a154 267 u_int chain_count; /* live + deleted chains under core */
a4dc31e0 268 int generation; /* generation number (inserts only) */
0dea3156
MD
269};
270
271typedef struct hammer2_chain_core hammer2_chain_core_t;
272
bca9f8e6
MD
273RB_HEAD(hammer2_io_tree, hammer2_io);
274
bca9f8e6
MD
275/*
276 * DIO - Management structure wrapping system buffer cache.
277 *
3d4f397a
MD
278 * HAMMER2 uses an I/O abstraction that allows it to cache and manipulate
279 * fixed-sized filesystem buffers frontend by variable-sized hammer2_chain
280 * structures.
bca9f8e6 281 */
3e3e52cc
MD
282/* #define HAMMER2_IO_DEBUG */
283
284#ifdef HAMMER2_IO_DEBUG
285#define HAMMER2_IO_DEBUG_ARGS , const char *file, int line
286#define HAMMER2_IO_DEBUG_CALL , file, line
287#define HAMMER2_IO_DEBUG_COUNT 2048
288#define HAMMER2_IO_DEBUG_MASK (HAMMER2_IO_DEBUG_COUNT - 1)
289#else
290#define HAMMER2_IO_DEBUG_ARGS
291#define HAMMER2_IO_DEBUG_CALL
292#endif
293
fdf62707 294struct hammer2_io {
0924b3f8 295 RB_ENTRY(hammer2_io) rbnode; /* indexed by device offset */
506bd6d1 296 struct hammer2_dev *hmp;
fdf62707 297 struct buf *bp;
fdf62707 298 off_t pbase;
7767d389 299 uint64_t refs;
fdf62707 300 int psize;
6c8a5902
MD
301 int act; /* activity */
302 int btype; /* approximate BREF_TYPE_* */
3d4f397a 303 int ticks;
85b1f267 304 int error;
3e3e52cc
MD
305#ifdef HAMMER2_IO_DEBUG
306 int debug_index;
307#else
85b1f267 308 int unused01;
3e3e52cc 309#endif
f48e8306
MD
310 uint64_t dedup_valid; /* valid for dedup operation */
311 uint64_t dedup_alloc; /* allocated / de-dupable */
3e3e52cc
MD
312#ifdef HAMMER2_IO_DEBUG
313 const char *debug_file[HAMMER2_IO_DEBUG_COUNT];
314 void *debug_td[HAMMER2_IO_DEBUG_COUNT];
315 int debug_line[HAMMER2_IO_DEBUG_COUNT];
316 uint64_t debug_refs[HAMMER2_IO_DEBUG_COUNT];
317#endif
fdf62707
MD
318};
319
320typedef struct hammer2_io hammer2_io_t;
321
7767d389
MD
322#define HAMMER2_DIO_INPROG 0x8000000000000000LLU /* bio in progress */
323#define HAMMER2_DIO_GOOD 0x4000000000000000LLU /* dio->bp is stable */
324#define HAMMER2_DIO_WAITING 0x2000000000000000LLU /* wait on INPROG */
325#define HAMMER2_DIO_DIRTY 0x1000000000000000LLU /* flush last drop */
1159c75c 326#define HAMMER2_DIO_FLUSH 0x0800000000000000LLU /* immediate flush */
bca9f8e6 327
7767d389
MD
328#define HAMMER2_DIO_MASK 0x00FFFFFFFFFFFFFFLLU
329
fdf62707
MD
330/*
331 * Primary chain structure keeps track of the topology in-memory.
332 */
50e4f8f4 333struct hammer2_chain {
f1c7c224 334 hammer2_mtx_t lock;
da6f36f4 335 hammer2_chain_core_t core;
8138a154 336 RB_ENTRY(hammer2_chain) rbnode; /* live chain(s) */
0dea3156 337 hammer2_blockref_t bref;
da6f36f4 338 struct hammer2_chain *parent;
506bd6d1
MD
339 struct hammer2_dev *hmp;
340 struct hammer2_pfs *pmp; /* A PFS or super-root (spmp) */
50456506 341
fda30e02 342 struct lock diolk; /* xop focus interlock */
fdf62707 343 hammer2_io_t *dio; /* physical data buffer */
fda30e02 344 hammer2_media_data_t *data; /* data pointer shortcut */
0dea3156 345 u_int bytes; /* physical data size */
5c23d7f1 346 u_int flags;
0dea3156 347 u_int refs;
731b2a84 348 u_int lockcnt;
23c7c7dd 349 int error; /* on-lock data error state */
2b8f3e7e 350 int cache_index; /* heur speeds up lookup */
23c7c7dd 351
da6f36f4 352 TAILQ_ENTRY(hammer2_chain) flush_node; /* flush list */
a7b81974 353 TAILQ_ENTRY(hammer2_chain) lru_node; /* 0-refs LRU */
50e4f8f4
MD
354};
355
356typedef struct hammer2_chain hammer2_chain_t;
357
5c23d7f1 358int hammer2_chain_cmp(hammer2_chain_t *chain1, hammer2_chain_t *chain2);
ecc33e71 359RB_PROTOTYPE(hammer2_chain_tree, hammer2_chain, rbnode, hammer2_chain_cmp);
5c23d7f1 360
1a7cfe5a
MD
361/*
362 * Special notes on flags:
363 *
b93cc2e0
MD
364 * INITIAL - This flag allows a chain to be created and for storage to
365 * be allocated without having to immediately instantiate the
366 * related buffer. The data is assumed to be all-zeros. It
367 * is primarily used for indirect blocks.
1a7cfe5a 368 *
3f4ec3cf
MD
369 * MODIFIED - The chain's media data has been modified. Prevents chain
370 * free on lastdrop if still in the topology.
da6f36f4 371 *
3f4ec3cf
MD
372 * UPDATE - Chain might not be modified but parent blocktable needs
373 * an update. Prevents chain free on lastdrop if still in
374 * the topology.
b93cc2e0
MD
375 *
376 * FICTITIOUS - Faked chain as a placeholder for an error condition. This
377 * chain is unsuitable for I/O.
378 *
379 * BMAPPED - Indicates that the chain is present in the parent blockmap.
380 *
381 * BMAPUPD - Indicates that the chain is present but needs to be updated
382 * in the parent blockmap.
1a7cfe5a 383 */
0dea3156
MD
384#define HAMMER2_CHAIN_MODIFIED 0x00000001 /* dirty chain data */
385#define HAMMER2_CHAIN_ALLOCATED 0x00000002 /* kmalloc'd chain */
da6f36f4 386#define HAMMER2_CHAIN_DESTROY 0x00000004
b49f72c3 387#define HAMMER2_CHAIN_DEDUPABLE 0x00000008 /* registered w/dedup */
5f6853df 388#define HAMMER2_CHAIN_DELETED 0x00000010 /* deleted chain */
01eabad4 389#define HAMMER2_CHAIN_INITIAL 0x00000020 /* initial create */
da6f36f4 390#define HAMMER2_CHAIN_UPDATE 0x00000040 /* need parent update */
ecfe89b8 391#define HAMMER2_CHAIN_UNUSED0080 0x00000080
7d565a4f 392#define HAMMER2_CHAIN_TESTEDGOOD 0x00000100 /* crc tested good */
da6f36f4 393#define HAMMER2_CHAIN_ONFLUSH 0x00000200 /* on a flush list */
b93cc2e0 394#define HAMMER2_CHAIN_FICTITIOUS 0x00000400 /* unsuitable for I/O */
0dea3156 395#define HAMMER2_CHAIN_VOLUMESYNC 0x00000800 /* needs volume sync */
ecfe89b8 396#define HAMMER2_CHAIN_UNUSED1000 0x00001000
f1c7c224 397#define HAMMER2_CHAIN_COUNTEDBREFS 0x00002000 /* block table stats */
5f6853df 398#define HAMMER2_CHAIN_ONRBTREE 0x00004000 /* on parent RB tree */
a7b81974 399#define HAMMER2_CHAIN_ONLRU 0x00008000 /* on LRU list */
512beabd 400#define HAMMER2_CHAIN_EMBEDDED 0x00010000 /* embedded data */
10136ab6 401#define HAMMER2_CHAIN_RELEASE 0x00020000 /* don't keep around */
da6f36f4
MD
402#define HAMMER2_CHAIN_BMAPPED 0x00040000 /* present in blkmap */
403#define HAMMER2_CHAIN_BMAPUPD 0x00080000 /* +needs updating */
f1c7c224
MD
404#define HAMMER2_CHAIN_IOINPROG 0x00100000 /* I/O interlock */
405#define HAMMER2_CHAIN_IOSIGNAL 0x00200000 /* I/O interlock */
50456506 406#define HAMMER2_CHAIN_PFSBOUNDARY 0x00400000 /* super->pfs inode */
4e0d25fd 407#define HAMMER2_CHAIN_HINT_LEAF_COUNT 0x00800000 /* redo leaf count */
f94a23f5 408#define HAMMER2_CHAIN_LRUHINT 0x01000000 /* was reused */
c667909f 409
da6f36f4
MD
410#define HAMMER2_CHAIN_FLUSH_MASK (HAMMER2_CHAIN_MODIFIED | \
411 HAMMER2_CHAIN_UPDATE | \
8bbe5025
MD
412 HAMMER2_CHAIN_ONFLUSH | \
413 HAMMER2_CHAIN_DESTROY)
da6f36f4 414
23c7c7dd
MD
415/*
416 * Hammer2 error codes, used by chain->error and cluster->error. The error
417 * code is typically set on-lock unless no I/O was requested, and set on
418 * I/O otherwise. If set for a cluster it generally means that the cluster
419 * code could not find a valid copy to present.
420 *
1e2c8208
MD
421 * All H2 error codes are flags and can be accumulated by ORing them
422 * together.
423 *
b93cc2e0
MD
424 * IO - An I/O error occurred
425 * CHECK - I/O succeeded but did not match the check code
426 * INCOMPLETE - A cluster is not complete enough to use, or
427 * a chain cannot be loaded because its parent has an error.
428 *
23c7c7dd
MD
429 * NOTE: API allows callers to check zero/non-zero to determine if an error
430 * condition exists.
431 *
432 * NOTE: Chain's data field is usually NULL on an IO error but not necessarily
433 * NULL on other errors. Check chain->error, not chain->data.
434 */
1e2c8208 435#define HAMMER2_ERROR_NONE 0 /* no error (must be 0) */
65cacacf
MD
436#define HAMMER2_ERROR_EIO 0x00000001 /* device I/O error */
437#define HAMMER2_ERROR_CHECK 0x00000002 /* check code error */
438#define HAMMER2_ERROR_INCOMPLETE 0x00000004 /* incomplete cluster */
439#define HAMMER2_ERROR_DEPTH 0x00000008 /* tmp depth limit */
440#define HAMMER2_ERROR_BADBREF 0x00000010 /* illegal bref */
441#define HAMMER2_ERROR_ENOSPC 0x00000020 /* allocation failure */
442#define HAMMER2_ERROR_ENOENT 0x00000040 /* entry not found */
443#define HAMMER2_ERROR_ENOTEMPTY 0x00000080 /* dir not empty */
444#define HAMMER2_ERROR_EAGAIN 0x00000100 /* retry */
445#define HAMMER2_ERROR_ENOTDIR 0x00000200 /* not directory */
446#define HAMMER2_ERROR_EISDIR 0x00000400 /* is directory */
447#define HAMMER2_ERROR_EINPROGRESS 0x00000800 /* already running */
448#define HAMMER2_ERROR_ABORTED 0x00001000 /* aborted operation */
449#define HAMMER2_ERROR_EOF 0x00002000 /* end of scan */
450#define HAMMER2_ERROR_EINVAL 0x00004000 /* catch-all */
451#define HAMMER2_ERROR_EEXIST 0x00008000 /* entry exists */
452#define HAMMER2_ERROR_EDEADLK 0x00010000
453#define HAMMER2_ERROR_ESRCH 0x00020000
454#define HAMMER2_ERROR_ETIMEDOUT 0x00040000
23c7c7dd 455
c667909f
MD
456/*
457 * Flags passed to hammer2_chain_lookup() and hammer2_chain_next()
1a7cfe5a 458 *
c847e838 459 * NOTES:
c847e838
MD
460 * NODATA - Asks that the chain->data not be resolved in order
461 * to avoid I/O.
462 *
463 * NODIRECT - Prevents a lookup of offset 0 in an inode from returning
464 * the inode itself if the inode is in DIRECTDATA mode
465 * (i.e. file is <= 512 bytes). Used by the synchronization
466 * code to prevent confusion.
467 *
468 * SHARED - The input chain is expected to be locked shared,
469 * and the output chain is locked shared.
470 *
471 * MATCHIND - Allows an indirect block / freemap node to be returned
472 * when the passed key range matches the radix. Remember
473 * that key_end is inclusive (e.g. {0x000,0xFFF},
474 * not {0x000,0x1000}).
475 *
476 * (Cannot be used for remote or cluster ops).
477 *
478 * ALLNODES - Allows NULL focus.
479 *
480 * ALWAYS - Always resolve the data. If ALWAYS and NODATA are both
481 * missing, bulk file data is not resolved but inodes and
482 * other meta-data will.
c667909f 483 */
68b321c1 484#define HAMMER2_LOOKUP_UNUSED0001 0x00000001
8cce658d 485#define HAMMER2_LOOKUP_NODATA 0x00000002 /* data left NULL */
f7712c43 486#define HAMMER2_LOOKUP_NODIRECT 0x00000004 /* no offset=0 DD */
a0ed3c24 487#define HAMMER2_LOOKUP_SHARED 0x00000100
10136ab6 488#define HAMMER2_LOOKUP_MATCHIND 0x00000200 /* return all chains */
a6cf1052 489#define HAMMER2_LOOKUP_ALLNODES 0x00000400 /* allow NULL focus */
1a7cfe5a 490#define HAMMER2_LOOKUP_ALWAYS 0x00000800 /* resolve data */
68b321c1 491#define HAMMER2_LOOKUP_UNUSED1000 0x00001000
5c23d7f1 492
01eabad4
MD
493/*
494 * Flags passed to hammer2_chain_modify() and hammer2_chain_resize()
495 *
496 * NOTE: OPTDATA allows us to avoid instantiating buffers for INDIRECT
497 * blocks in the INITIAL-create state.
498 */
01eabad4 499#define HAMMER2_MODIFY_OPTDATA 0x00000002 /* data can be NULL */
4d5318eb 500#define HAMMER2_MODIFY_NO_MODIFY_TID 0x00000004
e2163f5b 501#define HAMMER2_MODIFY_UNUSED0008 0x00000008
01eabad4
MD
502
503/*
504 * Flags passed to hammer2_chain_lock()
b8ba9690
MD
505 *
506 * NOTE: RDONLY is set to optimize cluster operations when *no* modifications
507 * will be made to either the cluster being locked or any underlying
508 * cluster. It allows the cluster to lock and access data for a subset
509 * of available nodes instead of all available nodes.
68b321c1
MD
510 *
511 * NOTE: NONBLOCK is only used for hammer2_chain_repparent() and getparent(),
512 * other functions (e.g. hammer2_chain_lookup(), etc) can't handle its
513 * operation.
01eabad4
MD
514 */
515#define HAMMER2_RESOLVE_NEVER 1
516#define HAMMER2_RESOLVE_MAYBE 2
517#define HAMMER2_RESOLVE_ALWAYS 3
a0ed3c24
MD
518#define HAMMER2_RESOLVE_MASK 0x0F
519
1a7cfe5a 520#define HAMMER2_RESOLVE_SHARED 0x10 /* request shared lock */
b8b0be74 521#define HAMMER2_RESOLVE_LOCKAGAIN 0x20 /* another shared lock */
d2a41023 522#define HAMMER2_RESOLVE_UNUSED40 0x40
68b321c1 523#define HAMMER2_RESOLVE_NONBLOCK 0x80 /* non-blocking */
01eabad4 524
91abd410
MD
525/*
526 * Flags passed to hammer2_chain_delete()
527 */
da6f36f4 528#define HAMMER2_DELETE_PERMANENT 0x0001
b3659de2 529
01d71aa5
MD
530/*
531 * Flags passed to hammer2_chain_insert() or hammer2_chain_rename()
850d3f60 532 * or hammer2_chain_create().
01d71aa5 533 */
506bd6d1 534#define HAMMER2_INSERT_PFSROOT 0x0004
850d3f60 535#define HAMMER2_INSERT_SAMEPARENT 0x0008
91abd410 536
09dd2dfe
MD
537/*
538 * Flags passed to hammer2_chain_delete_duplicate()
539 */
540#define HAMMER2_DELDUP_RECORE 0x0001
541
6ba3b984
MD
542/*
543 * Cluster different types of storage together for allocations
544 */
545#define HAMMER2_FREECACHE_INODE 0
546#define HAMMER2_FREECACHE_INDIR 1
547#define HAMMER2_FREECACHE_DATA 2
548#define HAMMER2_FREECACHE_UNUSED3 3
549#define HAMMER2_FREECACHE_TYPES 4
550
1a7cfe5a
MD
551/*
552 * hammer2_freemap_alloc() block preference
553 */
554#define HAMMER2_OFF_NOPREF ((hammer2_off_t)-1)
555
6ba3b984
MD
556/*
557 * BMAP read-ahead maximum parameters
558 */
559#define HAMMER2_BMAP_COUNT 16 /* max bmap read-ahead */
560#define HAMMER2_BMAP_BYTES (HAMMER2_PBUFSIZE * HAMMER2_BMAP_COUNT)
561
10136ab6
MD
562/*
563 * hammer2_freemap_adjust()
564 */
565#define HAMMER2_FREEMAP_DORECOVER 1
566#define HAMMER2_FREEMAP_DOMAYFREE 2
567#define HAMMER2_FREEMAP_DOREALFREE 3
568
703720e4 569/*
278ab2b2 570 * HAMMER2 cluster - A set of chains representing the same entity.
5c23d7f1 571 *
94491fa0
MD
572 * hammer2_cluster typically represents a temporary set of representitive
573 * chains. The one exception is that a hammer2_cluster is embedded in
574 * hammer2_inode. This embedded cluster is ONLY used to track the
575 * representitive chains and cannot be directly locked.
576 *
5ceaaa82
MD
577 * A cluster is usually temporary (and thus per-thread) for locking purposes,
578 * allowing us to embed the asynchronous storage required for cluster
579 * operations in the cluster itself and adjust the state and status without
580 * having to worry too much about SMP issues.
581 *
582 * The exception is the cluster embedded in the hammer2_inode structure.
583 * This is used to cache the cluster state on an inode-by-inode basis.
584 * Individual hammer2_chain structures not incorporated into clusters might
585 * also stick around to cache miscellanious elements.
94491fa0
MD
586 *
587 * Because the cluster is a 'working copy' and is usually subject to cluster
588 * quorum rules, it is quite possible for us to end up with an insufficient
589 * number of live chains to execute an operation. If an insufficient number
590 * of chains remain in a working copy, the operation may have to be
5ceaaa82
MD
591 * downgraded, retried, stall until the requisit number of chains are
592 * available, or possibly even error out depending on the mount type.
8db69c9f
MD
593 *
594 * A cluster's focus is set when it is locked. The focus can only be set
595 * to a chain still part of the synchronized set.
5c23d7f1 596 */
c847e838
MD
597#define HAMMER2_XOPFIFO 16
598#define HAMMER2_XOPFIFO_MASK (HAMMER2_XOPFIFO - 1)
dc6a6bd2 599#define HAMMER2_XOPGROUPS 32
c847e838 600#define HAMMER2_XOPGROUPS_MASK (HAMMER2_XOPGROUPS - 1)
19808ac9
MD
601
602#define HAMMER2_MAXCLUSTER 8
603#define HAMMER2_XOPMASK_CLUSTER (uint64_t)((1LLU << HAMMER2_MAXCLUSTER) - 1)
604#define HAMMER2_XOPMASK_VOP (uint64_t)0x0000000080000000LLU
605#define HAMMER2_XOPMASK_FIFOW (uint64_t)0x0000000040000000LLU
606#define HAMMER2_XOPMASK_WAIT (uint64_t)0x0000000020000000LLU
607#define HAMMER2_XOPMASK_FEED (uint64_t)0x0000000100000000LLU
a7b81974
MD
608
609#define HAMMER2_XOPMASK_ALLDONE (HAMMER2_XOPMASK_VOP | HAMMER2_XOPMASK_CLUSTER)
278ab2b2 610
7d565a4f
MD
611#define HAMMER2_SPECTHREADS 1 /* sync */
612
4b7e61e0 613struct hammer2_cluster_item {
4b7e61e0 614 hammer2_chain_t *chain;
3f4ec3cf 615 int error;
b8ba9690 616 uint32_t flags;
4b7e61e0
MD
617};
618
619typedef struct hammer2_cluster_item hammer2_cluster_item_t;
620
8db69c9f
MD
621/*
622 * INVALID - Invalid for focus, i.e. not part of synchronized set.
623 * Once set, this bit is sticky across operations.
e513e77e
MD
624 *
625 * FEMOD - Indicates that front-end modifying operations can
626 * mess with this entry and MODSYNC will copy also
627 * effect it.
8db69c9f
MD
628 */
629#define HAMMER2_CITEM_INVALID 0x00000001
e513e77e 630#define HAMMER2_CITEM_FEMOD 0x00000002
c847e838 631#define HAMMER2_CITEM_NULL 0x00000004
8db69c9f 632
278ab2b2 633struct hammer2_cluster {
278ab2b2 634 int refs; /* track for deallocation */
b8ba9690 635 int ddflag;
506bd6d1 636 struct hammer2_pfs *pmp;
278ab2b2
MD
637 uint32_t flags;
638 int nchains;
23c7c7dd 639 int error; /* error code valid on lock */
8db69c9f 640 int focus_index;
278ab2b2 641 hammer2_chain_t *focus; /* current focus (or mod) */
4b7e61e0 642 hammer2_cluster_item_t array[HAMMER2_MAXCLUSTER];
278ab2b2
MD
643};
644
94491fa0 645typedef struct hammer2_cluster hammer2_cluster_t;
278ab2b2 646
5ceaaa82
MD
647/*
648 * WRHARD - Hard mounts can write fully synchronized
649 * RDHARD - Hard mounts can read fully synchronized
b93cc2e0
MD
650 * UNHARD - Unsynchronized masters present
651 * NOHARD - No masters visible
5ceaaa82
MD
652 * WRSOFT - Soft mounts can write to at least the SOFT_MASTER
653 * RDSOFT - Soft mounts can read from at least a SOFT_SLAVE
b93cc2e0
MD
654 * UNSOFT - Unsynchronized slaves present
655 * NOSOFT - No slaves visible
5ceaaa82
MD
656 * RDSLAVE - slaves are accessible (possibly unsynchronized or remote).
657 * MSYNCED - All masters are fully synchronized
658 * SSYNCED - All known local slaves are fully synchronized to masters
659 *
660 * All available masters are always incorporated. All PFSs belonging to a
661 * cluster (master, slave, copy, whatever) always try to synchronize the
662 * total number of known masters in the PFSs root inode.
663 *
664 * A cluster might have access to many slaves, copies, or caches, but we
665 * have a limited number of cluster slots. Any such elements which are
666 * directly mounted from block device(s) will always be incorporated. Note
667 * that SSYNCED only applies to such elements which are directly mounted,
668 * not to any remote slaves, copies, or caches that could be available. These
669 * bits are used to monitor and drive our synchronization threads.
670 *
671 * When asking the question 'is any data accessible at all', then a simple
672 * test against (RDHARD|RDSOFT|RDSLAVE) gives you the answer. If any of
673 * these bits are set the object can be read with certain caveats:
674 * RDHARD - no caveats. RDSOFT - authoritative but might not be synchronized.
675 * and RDSLAVE - not authoritative, has some data but it could be old or
676 * incomplete.
677 *
678 * When both soft and hard mounts are available, data will be read and written
679 * via the soft mount only. But all might be in the cluster because
680 * background synchronization threads still need to do their work.
681 */
a6cf1052
MD
682#define HAMMER2_CLUSTER_INODE 0x00000001 /* embedded in inode struct */
683#define HAMMER2_CLUSTER_UNUSED2 0x00000002
b8ba9690 684#define HAMMER2_CLUSTER_LOCKED 0x00000004 /* cluster lks not recursive */
5ceaaa82
MD
685#define HAMMER2_CLUSTER_WRHARD 0x00000100 /* hard-mount can write */
686#define HAMMER2_CLUSTER_RDHARD 0x00000200 /* hard-mount can read */
b93cc2e0
MD
687#define HAMMER2_CLUSTER_UNHARD 0x00000400 /* unsynchronized masters */
688#define HAMMER2_CLUSTER_NOHARD 0x00000800 /* no masters visible */
689#define HAMMER2_CLUSTER_WRSOFT 0x00001000 /* soft-mount can write */
690#define HAMMER2_CLUSTER_RDSOFT 0x00002000 /* soft-mount can read */
691#define HAMMER2_CLUSTER_UNSOFT 0x00004000 /* unsynchronized slaves */
692#define HAMMER2_CLUSTER_NOSOFT 0x00008000 /* no slaves visible */
693#define HAMMER2_CLUSTER_MSYNCED 0x00010000 /* all masters synchronized */
694#define HAMMER2_CLUSTER_SSYNCED 0x00020000 /* known slaves synchronized */
5ceaaa82
MD
695
696#define HAMMER2_CLUSTER_ANYDATA ( HAMMER2_CLUSTER_RDHARD | \
697 HAMMER2_CLUSTER_RDSOFT | \
698 HAMMER2_CLUSTER_RDSLAVE)
699
700#define HAMMER2_CLUSTER_RDOK ( HAMMER2_CLUSTER_RDHARD | \
701 HAMMER2_CLUSTER_RDSOFT)
702
703#define HAMMER2_CLUSTER_WROK ( HAMMER2_CLUSTER_WRHARD | \
704 HAMMER2_CLUSTER_WRSOFT)
278ab2b2 705
b8ba9690
MD
706#define HAMMER2_CLUSTER_ZFLAGS ( HAMMER2_CLUSTER_WRHARD | \
707 HAMMER2_CLUSTER_RDHARD | \
708 HAMMER2_CLUSTER_WRSOFT | \
709 HAMMER2_CLUSTER_RDSOFT | \
710 HAMMER2_CLUSTER_MSYNCED | \
711 HAMMER2_CLUSTER_SSYNCED)
5c23d7f1 712
23c7c7dd
MD
713/*
714 * Helper functions (cluster must be locked for flags to be valid).
715 */
716static __inline
717int
718hammer2_cluster_rdok(hammer2_cluster_t *cluster)
719{
720 return (cluster->flags & HAMMER2_CLUSTER_RDOK);
721}
722
723static __inline
724int
725hammer2_cluster_wrok(hammer2_cluster_t *cluster)
726{
727 return (cluster->flags & HAMMER2_CLUSTER_WROK);
728}
729
5afbe9d8 730RB_HEAD(hammer2_inode_tree, hammer2_inode); /* ip->rbnode */
d0755e6d
MD
731TAILQ_HEAD(inoq_head, hammer2_inode); /* ip->entry */
732TAILQ_HEAD(depq_head, hammer2_depend); /* depend->entry */
733
734struct hammer2_depend {
735 TAILQ_ENTRY(hammer2_depend) entry;
736 struct inoq_head sideq;
737 long count;
738 int pass2;
739 int unused01;
740};
741
742typedef struct hammer2_depend hammer2_depend_t;
9797e933 743
5c23d7f1
MD
744/*
745 * A hammer2 inode.
46558838 746 *
bca9f8e6
MD
747 * NOTE: The inode-embedded cluster is never used directly for I/O (since
748 * it may be shared). Instead it will be replicated-in and synchronized
749 * back out if changed.
703720e4
MD
750 */
751struct hammer2_inode {
9797e933 752 RB_ENTRY(hammer2_inode) rbnode; /* inumber lookup (HL) */
d0755e6d
MD
753 TAILQ_ENTRY(hammer2_inode) entry; /* SYNCQ/SIDEQ */
754 hammer2_depend_t *depend; /* non-NULL if SIDEQ */
755 hammer2_depend_t depend_static; /* (in-place allocation) */
94491fa0 756 hammer2_mtx_t lock; /* inode lock */
63be6fed 757 hammer2_mtx_t truncate_lock; /* prevent truncates */
506bd6d1 758 struct hammer2_pfs *pmp; /* PFS mount */
703720e4 759 struct vnode *vp;
eedd52a3 760 struct spinlock cluster_spin; /* update cluster */
278ab2b2 761 hammer2_cluster_t cluster;
37aa19df 762 struct lockf advlock;
476d2aad
MD
763 u_int flags;
764 u_int refs; /* +vpref, +flushref */
1e5c08ba 765 uint8_t comp_heuristic;
7a9b14a0 766 hammer2_inode_meta_t meta; /* copy of meta-data */
2121ef11 767 hammer2_off_t osize;
703720e4
MD
768};
769
54eb943b
MD
770typedef struct hammer2_inode hammer2_inode_t;
771
2121ef11
MD
772/*
773 * MODIFIED - Inode is in a modified state, ip->meta may have changes.
774 * RESIZED - Inode truncated (any) or inode extended beyond
775 * EMBEDDED_BYTES.
5afbe9d8
MD
776 *
777 * SYNCQ - Inode is included in the current filesystem sync. The
778 * DELETING and CREATING flags will be acted upon.
779 *
780 * SIDEQ - Inode has likely been disconnected from the vnode topology
781 * and so is not visible to the vnode-based filesystem syncer
782 * code, but is dirty and must be included in the next
783 * filesystem sync. These inodes are moved to the SYNCQ at
784 * the time the sync occurs.
785 *
786 * Inodes are not placed on this queue simply because they have
787 * become dirty, if a vnode is attached.
788 *
789 * DELETING - Inode is flagged for deletion during the next filesystem
790 * sync. That is, the inode's chain is currently connected
791 * and must be deleting during the current or next fs sync.
792 *
793 * CREATING - Inode is flagged for creation during the next filesystem
794 * sync. That is, the inode's chain topology exists (so
795 * kernel buffer flushes can occur), but is currently
796 * disconnected and must be inserted during the current or
797 * next fs sync. If the DELETING flag is also set, the
798 * topology can be thrown away instead.
799 *
800 * If an inode that is already part of the current filesystem sync is
801 * modified by the frontend, including by buffer flushes, the inode lock
802 * code detects the SYNCQ flag and moves the inode to the head of the
803 * flush-in-progress, then blocks until the flush has gotten past it.
2121ef11 804 */
476d2aad 805#define HAMMER2_INODE_MODIFIED 0x0001
99da41ea 806#define HAMMER2_INODE_SROOT 0x0002 /* kmalloc special case */
51bf8e9b 807#define HAMMER2_INODE_RENAME_INPROG 0x0004
9797e933 808#define HAMMER2_INODE_ONRBTREE 0x0008
2121ef11 809#define HAMMER2_INODE_RESIZED 0x0010 /* requires inode_fsync */
6f445d15 810#define HAMMER2_INODE_UNUSED0020 0x0020
1f6671f8 811#define HAMMER2_INODE_ISUNLINKED 0x0040
7a9b14a0 812#define HAMMER2_INODE_METAGOOD 0x0080 /* inode meta-data good */
5afbe9d8 813#define HAMMER2_INODE_SIDEQ 0x0100 /* on side processing queue */
c42feed6 814#define HAMMER2_INODE_NOSIDEQ 0x0200 /* disable sideq operation */
a3868284 815#define HAMMER2_INODE_DIRTYDATA 0x0400 /* interlocks inode flush */
5afbe9d8
MD
816#define HAMMER2_INODE_SYNCQ 0x0800 /* sync interlock, sequenced */
817#define HAMMER2_INODE_DELETING 0x1000 /* sync interlock, chain topo */
818#define HAMMER2_INODE_CREATING 0x2000 /* sync interlock, chain topo */
819#define HAMMER2_INODE_SYNCQ_WAKEUP 0x4000 /* sync interlock wakeup */
ecfe89b8
MD
820#define HAMMER2_INODE_SYNCQ_PASS2 0x8000 /* force retry delay */
821
822#define HAMMER2_INODE_DIRTY (HAMMER2_INODE_MODIFIED | \
823 HAMMER2_INODE_DIRTYDATA | \
824 HAMMER2_INODE_DELETING | \
825 HAMMER2_INODE_CREATING)
9797e933
MD
826
827int hammer2_inode_cmp(hammer2_inode_t *ip1, hammer2_inode_t *ip2);
828RB_PROTOTYPE2(hammer2_inode_tree, hammer2_inode, rbnode, hammer2_inode_cmp,
829 hammer2_tid_t);
5c23d7f1 830
0dea3156 831/*
c603b86b 832 * Transaction management sub-structure under hammer2_pfs
0dea3156
MD
833 */
834struct hammer2_trans {
c603b86b
MD
835 uint32_t flags;
836 uint32_t sync_wait;
0dea3156
MD
837};
838
839typedef struct hammer2_trans hammer2_trans_t;
840
c603b86b
MD
841#define HAMMER2_TRANS_ISFLUSH 0x80000000 /* flush code */
842#define HAMMER2_TRANS_BUFCACHE 0x40000000 /* bio strategy */
ecfe89b8 843#define HAMMER2_TRANS_SIDEQ 0x20000000 /* run sideq */
d0755e6d 844#define HAMMER2_TRANS_UNUSED10 0x10000000
c603b86b 845#define HAMMER2_TRANS_WAITING 0x08000000 /* someone waiting */
6f445d15 846#define HAMMER2_TRANS_RESCAN 0x04000000 /* rescan sideq */
c603b86b 847#define HAMMER2_TRANS_MASK 0x00FFFFFF /* count mask */
d001f460 848
91abd410
MD
849#define HAMMER2_FREEMAP_HEUR_NRADIX 4 /* pwr 2 PBUFRADIX-MINIORADIX */
850#define HAMMER2_FREEMAP_HEUR_TYPES 8
3f01ebaa 851#define HAMMER2_FREEMAP_HEUR_SIZE (HAMMER2_FREEMAP_HEUR_NRADIX * \
91abd410 852 HAMMER2_FREEMAP_HEUR_TYPES)
004f88b4 853
3d4f397a 854#define HAMMER2_DEDUP_HEUR_SIZE (65536 * 4)
3f01ebaa
MD
855#define HAMMER2_DEDUP_HEUR_MASK (HAMMER2_DEDUP_HEUR_SIZE - 1)
856
53f84d31
MD
857#define HAMMER2_FLUSH_TOP 0x0001
858#define HAMMER2_FLUSH_ALL 0x0002
40498d1c 859#define HAMMER2_FLUSH_INODE_STOP 0x0004 /* stop at sub-inode */
ecfe89b8 860#define HAMMER2_FLUSH_FSSYNC 0x0008 /* part of filesystem sync */
53f84d31
MD
861
862
1f6671f8 863/*
2ed4fece 864 * Hammer2 support thread element.
b0f58de8 865 *
2ed4fece
MD
866 * Potentially many support threads can hang off of hammer2, primarily
867 * off the hammer2_pfs structure. Typically:
8db69c9f 868 *
2ed4fece
MD
869 * td x Nodes A synchronization thread for each node.
870 * td x Nodes x workers Worker threads for frontend operations.
871 * td x 1 Bioq thread for logical buffer writes.
8db69c9f 872 *
2ed4fece
MD
873 * In addition, the synchronization thread(s) associated with the
874 * super-root PFS (spmp) for a node is responsible for automatic bulkfree
875 * and dedup scans.
8db69c9f 876 */
2ed4fece 877struct hammer2_thread {
8db69c9f 878 struct hammer2_pfs *pmp;
9dca9515 879 struct hammer2_dev *hmp;
20852157 880 hammer2_xop_list_t xopq;
8db69c9f
MD
881 thread_t td;
882 uint32_t flags;
f7712c43 883 int depth;
2ed4fece
MD
884 int clindex; /* cluster element index */
885 int repidx;
05a3c4ec 886 char *scratch; /* MAXPHYS */
2ed4fece
MD
887};
888
889typedef struct hammer2_thread hammer2_thread_t;
890
891#define HAMMER2_THREAD_UNMOUNTING 0x0001 /* unmount request */
892#define HAMMER2_THREAD_DEV 0x0002 /* related to dev, not pfs */
660d007e 893#define HAMMER2_THREAD_WAITING 0x0004 /* thread in idle tsleep */
2ed4fece
MD
894#define HAMMER2_THREAD_REMASTER 0x0008 /* remaster request */
895#define HAMMER2_THREAD_STOP 0x0010 /* exit request */
896#define HAMMER2_THREAD_FREEZE 0x0020 /* force idle */
660d007e
MD
897#define HAMMER2_THREAD_FROZEN 0x0040 /* thread is frozen */
898#define HAMMER2_THREAD_XOPQ 0x0080 /* work pending */
899#define HAMMER2_THREAD_STOPPED 0x0100 /* thread has stopped */
900#define HAMMER2_THREAD_UNFREEZE 0x0200
660d007e
MD
901
902#define HAMMER2_THREAD_WAKEUP_MASK (HAMMER2_THREAD_UNMOUNTING | \
903 HAMMER2_THREAD_REMASTER | \
904 HAMMER2_THREAD_STOP | \
905 HAMMER2_THREAD_FREEZE | \
906 HAMMER2_THREAD_XOPQ)
2ed4fece 907
3f01ebaa
MD
908/*
909 * Support structure for dedup heuristic.
910 */
911struct hammer2_dedup {
912 hammer2_off_t data_off;
7d565a4f 913 uint64_t data_crc;
3f01ebaa 914 uint32_t ticks;
7d565a4f 915 uint32_t unused03;
3f01ebaa
MD
916};
917
918typedef struct hammer2_dedup hammer2_dedup_t;
2ed4fece
MD
919
920/*
c847e838 921 * hammer2_xop - container for VOP/XOP operation (allocated, not on stack).
2ed4fece
MD
922 *
923 * This structure is used to distribute a VOP operation across multiple
924 * nodes. It provides a rendezvous for concurrent node execution and
925 * can be detached from the frontend operation to allow the frontend to
926 * return early.
21a90458
MD
927 *
928 * This structure also sequences operations on up to three inodes.
2ed4fece 929 */
c4421f07
MD
930typedef void (*hammer2_xop_func_t)(union hammer2_xop *xop, void *scratch,
931 int clindex);
932
933struct hammer2_xop_desc {
934 hammer2_xop_func_t storage_func; /* local storage function */
935 hammer2_xop_func_t dmsg_dispatch; /* dmsg dispatch function */
936 hammer2_xop_func_t dmsg_process; /* dmsg processing function */
937 const char *id;
938};
939
940typedef struct hammer2_xop_desc hammer2_xop_desc_t;
c847e838 941
3f01ebaa 942struct hammer2_xop_fifo {
c847e838
MD
943 TAILQ_ENTRY(hammer2_xop_head) entry;
944 hammer2_chain_t *array[HAMMER2_XOPFIFO];
945 int errors[HAMMER2_XOPFIFO];
946 int ri;
947 int wi;
21a90458 948 int flags;
05a3c4ec 949 hammer2_thread_t *thr;
3f01ebaa
MD
950};
951
952typedef struct hammer2_xop_fifo hammer2_xop_fifo_t;
c847e838 953
21a90458 954#define HAMMER2_XOP_FIFO_RUN 0x0001
2efe493b 955#define HAMMER2_XOP_FIFO_STALL 0x0002
21a90458 956
c847e838 957struct hammer2_xop_head {
c4421f07 958 hammer2_xop_desc_t *desc;
e2163f5b 959 hammer2_tid_t mtid;
21a90458 960 struct hammer2_inode *ip1;
e12ae3a5
MD
961 struct hammer2_inode *ip2;
962 struct hammer2_inode *ip3;
19808ac9
MD
963 uint64_t run_mask;
964 uint64_t chk_mask;
640406cb 965 int flags;
c847e838
MD
966 int state;
967 int error;
01d71aa5 968 hammer2_key_t collect_key;
21a90458
MD
969 char *name1;
970 size_t name1_len;
e12ae3a5
MD
971 char *name2;
972 size_t name2_len;
c847e838
MD
973 hammer2_xop_fifo_t collect[HAMMER2_MAXCLUSTER];
974 hammer2_cluster_t cluster; /* help collections */
fda30e02 975 hammer2_io_t *focus_dio;
8db69c9f
MD
976};
977
c847e838
MD
978typedef struct hammer2_xop_head hammer2_xop_head_t;
979
01d71aa5 980struct hammer2_xop_ipcluster {
b7add675
MD
981 hammer2_xop_head_t head;
982};
983
c603b86b
MD
984struct hammer2_xop_strategy {
985 hammer2_xop_head_t head;
986 hammer2_key_t lbase;
987 int finished;
21a90458 988 hammer2_mtx_t lock;
c603b86b
MD
989 struct bio *bio;
990};
991
c847e838
MD
992struct hammer2_xop_readdir {
993 hammer2_xop_head_t head;
01d71aa5 994 hammer2_key_t lkey;
c847e838
MD
995};
996
c603b86b
MD
997struct hammer2_xop_nresolve {
998 hammer2_xop_head_t head;
b7add675 999 hammer2_key_t lhc; /* if name is NULL used lhc */
c603b86b
MD
1000};
1001
e12ae3a5
MD
1002struct hammer2_xop_unlink {
1003 hammer2_xop_head_t head;
1004 int isdir;
1005 int dopermanent;
1006};
1007
f6aebb44
MD
1008#define H2DOPERM_PERMANENT 0x01
1009#define H2DOPERM_FORCE 0x02
1010#define H2DOPERM_IGNINO 0x04
1011
e12ae3a5
MD
1012struct hammer2_xop_nrename {
1013 hammer2_xop_head_t head;
1014 hammer2_tid_t lhc;
1015 int ip_key;
1016};
1017
c603b86b
MD
1018struct hammer2_xop_scanlhc {
1019 hammer2_xop_head_t head;
1020 hammer2_key_t lhc;
1021};
1022
01d71aa5
MD
1023struct hammer2_xop_scanall {
1024 hammer2_xop_head_t head;
1025 hammer2_key_t key_beg; /* inclusive */
1026 hammer2_key_t key_end; /* inclusive */
b02c0ae6
MD
1027 int resolve_flags;
1028 int lookup_flags;
01d71aa5
MD
1029};
1030
b7add675
MD
1031struct hammer2_xop_lookup {
1032 hammer2_xop_head_t head;
1033 hammer2_key_t lhc;
1034};
1035
da0cdd33
MD
1036struct hammer2_xop_mkdirent {
1037 hammer2_xop_head_t head;
1038 hammer2_dirent_head_t dirent;
1039 hammer2_key_t lhc;
1040};
1041
c603b86b
MD
1042struct hammer2_xop_create {
1043 hammer2_xop_head_t head;
1044 hammer2_inode_meta_t meta; /* initial metadata */
1045 hammer2_key_t lhc;
1046 int flags;
c603b86b
MD
1047};
1048
b7add675 1049struct hammer2_xop_destroy {
e12ae3a5 1050 hammer2_xop_head_t head;
e12ae3a5
MD
1051};
1052
01d71aa5
MD
1053struct hammer2_xop_fsync {
1054 hammer2_xop_head_t head;
1055 hammer2_inode_meta_t meta;
1056 hammer2_off_t osize;
1057 u_int ipflags;
1058 int clear_directdata;
1059};
1060
b7add675
MD
1061struct hammer2_xop_unlinkall {
1062 hammer2_xop_head_t head;
01d71aa5
MD
1063 hammer2_key_t key_beg;
1064 hammer2_key_t key_end;
b7add675
MD
1065};
1066
1067struct hammer2_xop_connect {
c603b86b
MD
1068 hammer2_xop_head_t head;
1069 hammer2_key_t lhc;
c603b86b
MD
1070};
1071
12ff971c
MD
1072struct hammer2_xop_flush {
1073 hammer2_xop_head_t head;
1074};
1075
c847e838 1076typedef struct hammer2_xop_readdir hammer2_xop_readdir_t;
c603b86b 1077typedef struct hammer2_xop_nresolve hammer2_xop_nresolve_t;
e12ae3a5
MD
1078typedef struct hammer2_xop_unlink hammer2_xop_unlink_t;
1079typedef struct hammer2_xop_nrename hammer2_xop_nrename_t;
01d71aa5 1080typedef struct hammer2_xop_ipcluster hammer2_xop_ipcluster_t;
c603b86b 1081typedef struct hammer2_xop_strategy hammer2_xop_strategy_t;
da0cdd33 1082typedef struct hammer2_xop_mkdirent hammer2_xop_mkdirent_t;
c603b86b
MD
1083typedef struct hammer2_xop_create hammer2_xop_create_t;
1084typedef struct hammer2_xop_destroy hammer2_xop_destroy_t;
01d71aa5 1085typedef struct hammer2_xop_fsync hammer2_xop_fsync_t;
b7add675 1086typedef struct hammer2_xop_unlinkall hammer2_xop_unlinkall_t;
c603b86b 1087typedef struct hammer2_xop_scanlhc hammer2_xop_scanlhc_t;
01d71aa5 1088typedef struct hammer2_xop_scanall hammer2_xop_scanall_t;
b7add675 1089typedef struct hammer2_xop_lookup hammer2_xop_lookup_t;
e12ae3a5 1090typedef struct hammer2_xop_connect hammer2_xop_connect_t;
12ff971c 1091typedef struct hammer2_xop_flush hammer2_xop_flush_t;
c847e838
MD
1092
1093union hammer2_xop {
1094 hammer2_xop_head_t head;
01d71aa5 1095 hammer2_xop_ipcluster_t xop_ipcluster;
c847e838 1096 hammer2_xop_readdir_t xop_readdir;
c603b86b 1097 hammer2_xop_nresolve_t xop_nresolve;
e12ae3a5
MD
1098 hammer2_xop_unlink_t xop_unlink;
1099 hammer2_xop_nrename_t xop_nrename;
c603b86b 1100 hammer2_xop_strategy_t xop_strategy;
da0cdd33 1101 hammer2_xop_mkdirent_t xop_mkdirent;
c603b86b
MD
1102 hammer2_xop_create_t xop_create;
1103 hammer2_xop_destroy_t xop_destroy;
01d71aa5 1104 hammer2_xop_fsync_t xop_fsync;
b7add675 1105 hammer2_xop_unlinkall_t xop_unlinkall;
c603b86b 1106 hammer2_xop_scanlhc_t xop_scanlhc;
01d71aa5 1107 hammer2_xop_scanall_t xop_scanall;
b7add675 1108 hammer2_xop_lookup_t xop_lookup;
12ff971c 1109 hammer2_xop_flush_t xop_flush;
e12ae3a5 1110 hammer2_xop_connect_t xop_connect;
c847e838 1111};
2ed4fece 1112
c847e838 1113typedef union hammer2_xop hammer2_xop_t;
8db69c9f 1114
2ed4fece
MD
1115/*
1116 * hammer2_xop_group - Manage XOP support threads.
1117 */
1118struct hammer2_xop_group {
1119 hammer2_thread_t thrs[HAMMER2_MAXCLUSTER];
2ed4fece 1120};
8db69c9f 1121
2ed4fece 1122typedef struct hammer2_xop_group hammer2_xop_group_t;
8db69c9f 1123
c603b86b
MD
1124/*
1125 * flags to hammer2_xop_collect()
1126 */
1127#define HAMMER2_XOP_COLLECT_NOWAIT 0x00000001
12ff971c 1128#define HAMMER2_XOP_COLLECT_WAITALL 0x00000002
c603b86b
MD
1129
1130/*
1131 * flags to hammer2_xop_alloc()
1132 *
e2163f5b 1133 * MODIFYING - This is a modifying transaction, allocate a mtid.
40498d1c 1134 * RECURSE - Recurse top-level inode (for root flushes)
c603b86b 1135 */
e2163f5b 1136#define HAMMER2_XOP_MODIFYING 0x00000001
20852157 1137#define HAMMER2_XOP_STRATEGY 0x00000002
40498d1c 1138#define HAMMER2_XOP_INODE_STOP 0x00000004
65c894ff 1139#define HAMMER2_XOP_VOLHDR 0x00000008
ecfe89b8 1140#define HAMMER2_XOP_FSSYNC 0x00000010
6f445d15 1141#define HAMMER2_XOP_IROOT 0x00000020
c603b86b 1142
50456506 1143/*
506bd6d1
MD
1144 * Global (per partition) management structure, represents a hard block
1145 * device. Typically referenced by hammer2_chain structures when applicable.
1146 * Typically not used for network-managed elements.
1147 *
1148 * Note that a single hammer2_dev can be indirectly tied to multiple system
1149 * mount points. There is no direct relationship. System mounts are
1150 * per-cluster-id, not per-block-device, and a single hard mount might contain
1151 * many PFSs and those PFSs might combine together in various ways to form
1152 * the set of available clusters.
50456506 1153 */
506bd6d1 1154struct hammer2_dev {
50e4f8f4 1155 struct vnode *devvp; /* device vnode */
50e4f8f4 1156 int ronly; /* read-only mount */
f7712c43 1157 int mount_count; /* number of actively mounted PFSs */
506bd6d1 1158 TAILQ_ENTRY(hammer2_dev) mntentry; /* hammer2_mntlist */
703720e4 1159
5c23d7f1 1160 struct malloc_type *mchain;
54eb943b
MD
1161 int nipstacks;
1162 int maxipstacks;
063639ba 1163 kdmsg_iocom_t iocom; /* volume-level dmsg interface */
3d4f397a 1164 struct spinlock io_spin; /* iotree, iolruq access */
fdf62707
MD
1165 struct hammer2_io_tree iotree;
1166 int iofree_count;
65cacacf 1167 int freemap_relaxed;
7bed8d7e
MD
1168 hammer2_chain_t vchain; /* anchor chain (topology) */
1169 hammer2_chain_t fchain; /* anchor chain (freemap) */
da6f36f4 1170 struct spinlock list_spin;
506bd6d1 1171 struct hammer2_pfs *spmp; /* super-root pmp for transactions */
50456506 1172 struct lock vollk; /* lockmgr lock */
9dca9515
MD
1173 struct lock bulklk; /* bulkfree operation lock */
1174 struct lock bflock; /* bulk-free manual function lock */
3f01ebaa
MD
1175 hammer2_off_t heur_freemap[HAMMER2_FREEMAP_HEUR_SIZE];
1176 hammer2_dedup_t heur_dedup[HAMMER2_DEDUP_HEUR_SIZE];
3fc4c63d 1177 int volhdrno; /* last volhdrno written */
9dca9515 1178 uint32_t hflags; /* HMNT2 flags applicable to device */
65cacacf 1179 hammer2_off_t free_reserved; /* nominal free reserved */
9dca9515 1180 hammer2_thread_t bfthr; /* bulk-free thread */
e513e77e 1181 char devrepname[64]; /* for kprintf */
9dca9515 1182 hammer2_ioc_bulkfree_t bflast; /* stats for last bulkfree run */
54eb943b 1183 hammer2_volume_data_t voldata;
3fc4c63d 1184 hammer2_volume_data_t volsync; /* synchronized voldata */
703720e4
MD
1185};
1186
506bd6d1 1187typedef struct hammer2_dev hammer2_dev_t;
54eb943b 1188
23c7c7dd
MD
1189/*
1190 * Helper functions (cluster must be locked for flags to be valid).
1191 */
1192static __inline
1193int
1194hammer2_chain_rdok(hammer2_chain_t *chain)
1195{
1196 return (chain->error == 0);
1197}
1198
1199static __inline
1200int
1201hammer2_chain_wrok(hammer2_chain_t *chain)
1202{
1203 return (chain->error == 0 && chain->hmp->ronly == 0);
1204}
1205
e4e20f48 1206/*
506bd6d1
MD
1207 * Per-cluster management structure. This structure will be tied to a
1208 * system mount point if the system is mounting the PFS, but is also used
1209 * to manage clusters encountered during the super-root scan or received
1210 * via LNK_SPANs that might not be mounted.
7750fd72 1211 *
506bd6d1
MD
1212 * This structure is also used to represent the super-root that hangs off
1213 * of a hard mount point. The super-root is not really a cluster element.
1214 * In this case the spmp_hmp field will be non-NULL. It's just easier to do
1215 * this than to special case super-root manipulation in the hammer2_chain*
1216 * code as being only hammer2_dev-related.
7750fd72 1217 *
506bd6d1
MD
1218 * pfs_mode and pfs_nmasters are rollup fields which critically describes
1219 * how elements of the cluster act on the cluster. pfs_mode is only applicable
1220 * when a PFS is mounted by the system. pfs_nmasters is our best guess as to
1221 * how many masters have been configured for a cluster and is always
22211834
MD
1222 * applicable. pfs_types[] is an array with 1:1 correspondance to the
1223 * iroot cluster and describes the PFS types of the nodes making up the
1224 * cluster.
7750fd72 1225 *
506bd6d1 1226 * WARNING! Portions of this structure have deferred initialization. In
b652edda 1227 * particular, if not mounted there will be no wthread.
506bd6d1
MD
1228 * umounted network PFSs will also be missing iroot and numerous
1229 * other fields will not be initialized prior to mount.
7750fd72 1230 *
506bd6d1
MD
1231 * Synchronization threads are chain-specific and only applicable
1232 * to local hard PFS entries. A hammer2_pfs structure may contain
1233 * more than one when multiple hard PFSs are present on the local
1234 * machine which require synchronization monitoring. Most PFSs
1235 * (such as snapshots) are 1xMASTER PFSs which do not need a
1236 * synchronization thread.
7750fd72 1237 *
506bd6d1 1238 * WARNING! The chains making up pfs->iroot's cluster are accounted for in
f7712c43 1239 * hammer2_dev->mount_count when the pfs is associated with a mount
506bd6d1 1240 * point.
e4e20f48 1241 */
506bd6d1 1242struct hammer2_pfs {
7bed8d7e 1243 struct mount *mp;
506bd6d1 1244 TAILQ_ENTRY(hammer2_pfs) mntentry; /* hammer2_pfslist */
58e43599 1245 uuid_t pfs_clid;
506bd6d1 1246 hammer2_dev_t *spmp_hmp; /* only if super-root pmp */
b02c0ae6 1247 hammer2_dev_t *force_local; /* only if 'local' mount */
e4e20f48 1248 hammer2_inode_t *iroot; /* PFS root inode */
22211834 1249 uint8_t pfs_types[HAMMER2_MAXCLUSTER];
c7916d0b 1250 char *pfs_names[HAMMER2_MAXCLUSTER];
01d71aa5 1251 hammer2_dev_t *pfs_hmps[HAMMER2_MAXCLUSTER];
6f445d15 1252 hammer2_blockset_t pfs_iroot_blocksets[HAMMER2_MAXCLUSTER];
c603b86b 1253 hammer2_trans_t trans;
044541cd 1254 struct lock lock; /* PFS lock for certain ops */
76f85faa 1255 struct lock lock_nlink; /* rename and nlink lock */
e4e20f48 1256 struct netexport export; /* nfs export */
d2a41023 1257 int unused00;
e4e20f48 1258 int ronly; /* read-only mount */
b02c0ae6 1259 int hflags; /* pfs-specific mount flags */
99da41ea 1260 struct malloc_type *minode;
3a5aa68f 1261 struct malloc_type *mmsg;
9797e933 1262 struct spinlock inum_spin; /* inumber lookup */
50456506 1263 struct hammer2_inode_tree inum_tree; /* (not applicable to spmp) */
19808ac9 1264 long inum_count; /* #of inodes in inum_tree */
a7b81974 1265 struct spinlock lru_spin; /* inumber lookup */
f94a23f5 1266 struct hammer2_chain_list lru_list; /* basis for LRU tests */
a7b81974 1267 int lru_count; /* #of chains on LRU */
65c894ff 1268 int flags;
e513e77e
MD
1269 hammer2_tid_t modify_tid; /* modify transaction id */
1270 hammer2_tid_t inode_tid; /* inode allocator */
7750fd72
MD
1271 uint8_t pfs_nmasters; /* total masters */
1272 uint8_t pfs_mode; /* operating mode PFSMODE */
1273 uint8_t unused01;
1274 uint8_t unused02;
65cacacf 1275 int free_ticks; /* free_* calculations */
355d67fc 1276 long inmem_inodes;
65cacacf
MD
1277 hammer2_off_t free_reserved;
1278 hammer2_off_t free_nominal;
da6f36f4 1279 uint32_t inmem_dirty_chains;
3f5b8b3b 1280 int count_lwinprog; /* logical write in prog */
da6f36f4 1281 struct spinlock list_spin;
d0755e6d
MD
1282 struct inoq_head syncq; /* SYNCQ flagged inodes */
1283 struct depq_head depq; /* SIDEQ flagged inodes */
1284 long sideq_count; /* total inodes on depq */
2ed4fece 1285 hammer2_thread_t sync_thrs[HAMMER2_MAXCLUSTER];
01d71aa5 1286 uint32_t cluster_flags; /* cached cluster flags */
8cd26e36 1287 int has_xop_threads;
21a90458 1288 struct spinlock xop_spin; /* xop sequencer */
2ed4fece 1289 hammer2_xop_group_t xop_groups[HAMMER2_XOPGROUPS];
e4e20f48
MD
1290};
1291
506bd6d1 1292typedef struct hammer2_pfs hammer2_pfs_t;
e4e20f48 1293
0d66a712
MD
1294TAILQ_HEAD(hammer2_pfslist, hammer2_pfs);
1295
65c894ff
MD
1296#define HAMMER2_PMPF_SPMP 0x00000001
1297
f94a23f5
MD
1298/*
1299 * NOTE: The LRU list contains at least all the chains with refs == 0
1300 * that can be recycled, and may contain additional chains which
1301 * cannot.
1302 */
1303#define HAMMER2_LRU_LIMIT 4096
a7b81974 1304
0924b3f8
MD
1305#define HAMMER2_DIRTYCHAIN_WAITING 0x80000000
1306#define HAMMER2_DIRTYCHAIN_MASK 0x7FFFFFFF
1307
3f5b8b3b 1308#define HAMMER2_LWINPROG_WAITING 0x80000000
d34788ef
MD
1309#define HAMMER2_LWINPROG_WAITING0 0x40000000
1310#define HAMMER2_LWINPROG_MASK 0x3FFFFFFF
3f5b8b3b 1311
c847e838
MD
1312/*
1313 * hammer2_cluster_check
1314 */
1315#define HAMMER2_CHECK_NULL 0x00000001
1316
3148f677
MD
1317/*
1318 * Misc
1319 */
0a4a8eab
SW
1320#if defined(_KERNEL) || defined(_KERNEL_STRUCTURES)
1321#define VTOI(vp) ((hammer2_inode_t *)(vp)->v_data)
1322#endif
1323
703720e4
MD
1324#if defined(_KERNEL)
1325
1326MALLOC_DECLARE(M_HAMMER2);
1327
e4e20f48
MD
1328#define ITOV(ip) ((ip)->vp)
1329
91abd410
MD
1330/*
1331 * Currently locked chains retain the locked buffer cache buffer for
1332 * indirect blocks, and indirect blocks can be one of two sizes. The
1333 * device buffer has to match the case to avoid deadlocking recursive
1334 * chains that might otherwise try to access different offsets within
1335 * the same device buffer.
1336 */
a98aa0b0
MD
1337static __inline
1338int
1339hammer2_devblkradix(int radix)
1340{
f89b6495 1341#if 0
91abd410
MD
1342 if (radix <= HAMMER2_LBUFRADIX) {
1343 return (HAMMER2_LBUFRADIX);
1344 } else {
1345 return (HAMMER2_PBUFRADIX);
1346 }
f89b6495
MD
1347#endif
1348 return (HAMMER2_PBUFRADIX);
a98aa0b0
MD
1349}
1350
71008001
MD
1351/*
1352 * XXX almost time to remove this. DIO uses PBUFSIZE exclusively now.
1353 */
a98aa0b0
MD
1354static __inline
1355size_t
1356hammer2_devblksize(size_t bytes)
1357{
71008001 1358#if 0
a5913bdf 1359 if (bytes <= HAMMER2_LBUFSIZE) {
a98aa0b0 1360 return(HAMMER2_LBUFSIZE);
a5913bdf
MD
1361 } else {
1362 KKASSERT(bytes <= HAMMER2_PBUFSIZE &&
1363 (bytes ^ (bytes - 1)) == ((bytes << 1) - 1));
91abd410 1364 return (HAMMER2_PBUFSIZE);
a5913bdf 1365 }
71008001
MD
1366#endif
1367 return (HAMMER2_PBUFSIZE);
a98aa0b0
MD
1368}
1369
1370
e118c14f 1371static __inline
506bd6d1 1372hammer2_pfs_t *
e4e20f48 1373MPTOPMP(struct mount *mp)
703720e4 1374{
506bd6d1 1375 return ((hammer2_pfs_t *)mp->mnt_data);
703720e4
MD
1376}
1377
0b8efeb7
MD
1378#define HAMMER2_DEDUP_FRAG (HAMMER2_PBUFSIZE / 64)
1379#define HAMMER2_DEDUP_FRAGRADIX (HAMMER2_PBUFRADIX - 6)
1380
1381static __inline
1382uint64_t
1383hammer2_dedup_mask(hammer2_io_t *dio, hammer2_off_t data_off, u_int bytes)
1384{
1385 int bbeg;
1386 int bits;
1387 uint64_t mask;
1388
1389 bbeg = (int)((data_off & ~HAMMER2_OFF_MASK_RADIX) - dio->pbase) >>
1390 HAMMER2_DEDUP_FRAGRADIX;
1391 bits = (int)((bytes + (HAMMER2_DEDUP_FRAG - 1)) >>
1392 HAMMER2_DEDUP_FRAGRADIX);
1393 mask = ((uint64_t)1 << bbeg) - 1;
1394 if (bbeg + bits == 64)
1395 mask = (uint64_t)-1;
1396 else
1397 mask = ((uint64_t)1 << (bbeg + bits)) - 1;
1398
1399 mask &= ~(((uint64_t)1 << bbeg) - 1);
1400
1401 return mask;
1402}
1403
1e2c8208
MD
1404static __inline
1405int
1406hammer2_error_to_errno(int error)
1407{
1408 if (error) {
65cacacf 1409 if (error & HAMMER2_ERROR_EIO)
1e2c8208
MD
1410 error = EIO;
1411 else if (error & HAMMER2_ERROR_CHECK)
1412 error = EDOM;
1413 else if (error & HAMMER2_ERROR_ABORTED)
1414 error = EINTR;
65cacacf
MD
1415 else if (error & HAMMER2_ERROR_BADBREF)
1416 error = EIO;
1417 else if (error & HAMMER2_ERROR_ENOSPC)
1418 error = ENOSPC;
1419 else if (error & HAMMER2_ERROR_ENOENT)
1420 error = ENOENT;
1421 else if (error & HAMMER2_ERROR_ENOTEMPTY)
1422 error = ENOTEMPTY;
1423 else if (error & HAMMER2_ERROR_EAGAIN)
1424 error = EAGAIN;
1425 else if (error & HAMMER2_ERROR_ENOTDIR)
1426 error = ENOTDIR;
1427 else if (error & HAMMER2_ERROR_EISDIR)
1428 error = EISDIR;
1429 else if (error & HAMMER2_ERROR_EINPROGRESS)
1430 error = EINPROGRESS;
412a77ff
MD
1431 else if (error & HAMMER2_ERROR_EEXIST)
1432 error = EEXIST;
1e2c8208
MD
1433 else
1434 error = EDOM;
1435 }
1436 return error;
1437}
1438
65cacacf
MD
1439static __inline
1440int
1441hammer2_errno_to_error(int error)
1442{
1443 switch(error) {
1444 case 0:
1445 return 0;
1446 case EIO:
1447 return HAMMER2_ERROR_EIO;
1448 case EINVAL:
1449 default:
1450 return HAMMER2_ERROR_EINVAL;
1451 }
1452}
1453
fda30e02 1454
703720e4
MD
1455extern struct vop_ops hammer2_vnode_vops;
1456extern struct vop_ops hammer2_spec_vops;
1457extern struct vop_ops hammer2_fifo_vops;
0d66a712
MD
1458extern struct hammer2_pfslist hammer2_pfslist;
1459extern struct lock hammer2_mntlk;
1460
703720e4 1461
37aa19df 1462extern int hammer2_debug;
5071e670 1463extern long hammer2_debug_inode;
c152fb8d
MD
1464extern int hammer2_cluster_meta_read;
1465extern int hammer2_cluster_data_read;
f66cf371 1466extern int hammer2_cluster_write;
c57fa38c 1467extern int hammer2_dedup_enable;
3d4f397a 1468extern int hammer2_always_compress;
c57fa38c 1469extern int hammer2_inval_enable;
3f5b8b3b 1470extern int hammer2_flush_pipe;
837bd39b 1471extern int hammer2_dio_count;
40498d1c 1472extern int hammer2_dio_limit;
4ff0b408 1473extern int hammer2_bulkfree_tps;
1159c75c 1474extern int hammer2_worker_rmask;
7d565a4f
MD
1475extern long hammer2_chain_allocs;
1476extern long hammer2_chain_frees;
0924b3f8 1477extern long hammer2_limit_dirty_chains;
d0755e6d 1478extern long hammer2_limit_dirty_inodes;
f9f4459e 1479extern long hammer2_count_modified_chains;
c57fa38c 1480extern long hammer2_iod_invals;
01eabad4
MD
1481extern long hammer2_iod_file_read;
1482extern long hammer2_iod_meta_read;
1483extern long hammer2_iod_indr_read;
a98aa0b0
MD
1484extern long hammer2_iod_fmap_read;
1485extern long hammer2_iod_volu_read;
01eabad4 1486extern long hammer2_iod_file_write;
3f01ebaa
MD
1487extern long hammer2_iod_file_wembed;
1488extern long hammer2_iod_file_wzero;
1489extern long hammer2_iod_file_wdedup;
01eabad4
MD
1490extern long hammer2_iod_meta_write;
1491extern long hammer2_iod_indr_write;
9061bde5 1492extern long hammer2_iod_fmap_write;
01eabad4 1493extern long hammer2_iod_volu_write;
37aa19df 1494
40498d1c
MD
1495extern long hammer2_process_xxhash64;
1496extern long hammer2_process_icrc32;
7d565a4f 1497
355d67fc
MD
1498extern struct objcache *cache_buffer_read;
1499extern struct objcache *cache_buffer_write;
2ed4fece 1500extern struct objcache *cache_xops;
355d67fc 1501
7cfa8da5
MD
1502/*
1503 * hammer2_subr.c
1504 */
3a5aa68f
MD
1505#define hammer2_icrc32(buf, size) iscsi_crc32((buf), (size))
1506#define hammer2_icrc32c(buf, size, crc) iscsi_crc32_ext((buf), (size), (crc))
0c3a8cd0 1507
d662271e 1508int hammer2_signal_check(time_t *timep);
b93cc2e0
MD
1509const char *hammer2_error_str(int error);
1510
d0755e6d 1511void hammer2_inode_delayed_sideq(hammer2_inode_t *ip);
159c3ca2 1512void hammer2_inode_lock(hammer2_inode_t *ip, int how);
ecfe89b8
MD
1513void hammer2_inode_lock4(hammer2_inode_t *ip1, hammer2_inode_t *ip2,
1514 hammer2_inode_t *ip3, hammer2_inode_t *ip4);
b7add675 1515void hammer2_inode_unlock(hammer2_inode_t *ip);
3e8408db 1516void hammer2_inode_depend(hammer2_inode_t *ip1, hammer2_inode_t *ip2);
c847e838 1517hammer2_chain_t *hammer2_inode_chain(hammer2_inode_t *ip, int clindex, int how);
01d71aa5
MD
1518hammer2_chain_t *hammer2_inode_chain_and_parent(hammer2_inode_t *ip,
1519 int clindex, hammer2_chain_t **parentp, int how);
94491fa0
MD
1520hammer2_mtx_state_t hammer2_inode_lock_temp_release(hammer2_inode_t *ip);
1521void hammer2_inode_lock_temp_restore(hammer2_inode_t *ip,
1522 hammer2_mtx_state_t ostate);
1523int hammer2_inode_lock_upgrade(hammer2_inode_t *ip);
1524void hammer2_inode_lock_downgrade(hammer2_inode_t *ip, int);
e118c14f 1525
506bd6d1
MD
1526void hammer2_dev_exlock(hammer2_dev_t *hmp);
1527void hammer2_dev_shlock(hammer2_dev_t *hmp);
1528void hammer2_dev_unlock(hammer2_dev_t *hmp);
703720e4 1529
da0cdd33 1530int hammer2_get_dtype(uint8_t type);
159c3ca2 1531int hammer2_get_vtype(uint8_t type);
2f70512b
TK
1532uint8_t hammer2_get_obj_type(enum vtype vtype);
1533void hammer2_time_to_timespec(uint64_t xtime, struct timespec *ts);
1534uint64_t hammer2_timespec_to_time(const struct timespec *ts);
1535uint32_t hammer2_to_unix_xid(const uuid_t *uuid);
1536void hammer2_guid_to_uuid(uuid_t *uuid, uint32_t guid);
c603b86b 1537void hammer2_trans_manage_init(hammer2_pfs_t *pmp);
e028fa74 1538
7cfa8da5 1539hammer2_key_t hammer2_dirhash(const unsigned char *name, size_t len);
1a7cfe5a 1540int hammer2_getradix(size_t bytes);
7cfa8da5 1541
8cce658d 1542int hammer2_calc_logical(hammer2_inode_t *ip, hammer2_off_t uoff,
1897c66e 1543 hammer2_key_t *lbasep, hammer2_key_t *leofp);
7a9b14a0 1544int hammer2_calc_physical(hammer2_inode_t *ip, hammer2_key_t lbase);
b2b78aaa 1545void hammer2_update_time(uint64_t *timep);
278ab2b2 1546void hammer2_adjreadcounter(hammer2_blockref_t *bref, size_t bytes);
8cce658d 1547
7cfa8da5
MD
1548/*
1549 * hammer2_inode.c
1550 */
c603b86b 1551struct vnode *hammer2_igetv(hammer2_inode_t *ip, int *errorp);
506bd6d1 1552hammer2_inode_t *hammer2_inode_lookup(hammer2_pfs_t *pmp,
9797e933 1553 hammer2_tid_t inum);
5afbe9d8
MD
1554hammer2_inode_t *hammer2_inode_get(hammer2_pfs_t *pmp,
1555 hammer2_xop_head_t *xop, hammer2_tid_t inum, int idx);
7cfa8da5
MD
1556void hammer2_inode_free(hammer2_inode_t *ip);
1557void hammer2_inode_ref(hammer2_inode_t *ip);
1558void hammer2_inode_drop(hammer2_inode_t *ip);
ea155208 1559void hammer2_inode_repoint(hammer2_inode_t *ip, hammer2_inode_t *pip,
278ab2b2 1560 hammer2_cluster_t *cluster);
a6cf1052
MD
1561void hammer2_inode_repoint_one(hammer2_inode_t *ip, hammer2_cluster_t *cluster,
1562 int idx);
c603b86b 1563void hammer2_inode_modify(hammer2_inode_t *ip);
19808ac9 1564void hammer2_inode_run_sideq(hammer2_pfs_t *pmp, int doall);
6934ae32 1565
5afbe9d8 1566hammer2_inode_t *hammer2_inode_create_normal(hammer2_inode_t *pip,
e708f8b9 1567 struct vattr *vap, struct ucred *cred,
5afbe9d8
MD
1568 hammer2_key_t inum, int *errorp);
1569hammer2_inode_t *hammer2_inode_create_pfs(hammer2_pfs_t *spmp,
1570 const uint8_t *name, size_t name_len,
1571 int *errorp);
ecfe89b8 1572int hammer2_inode_chain_ins(hammer2_inode_t *ip);
6f445d15 1573int hammer2_inode_chain_des(hammer2_inode_t *ip);
40498d1c 1574int hammer2_inode_chain_sync(hammer2_inode_t *ip);
ecfe89b8 1575int hammer2_inode_chain_flush(hammer2_inode_t *ip, int flags);
e12ae3a5 1576int hammer2_inode_unlink_finisher(hammer2_inode_t *ip, int isopen);
da0cdd33
MD
1577int hammer2_dirent_create(hammer2_inode_t *dip, const char *name,
1578 size_t name_len, hammer2_key_t inum, uint8_t type);
ae183399 1579
7cfa8da5
MD
1580/*
1581 * hammer2_chain.c
1582 */
506bd6d1
MD
1583void hammer2_voldata_lock(hammer2_dev_t *hmp);
1584void hammer2_voldata_unlock(hammer2_dev_t *hmp);
1585void hammer2_voldata_modify(hammer2_dev_t *hmp);
1586hammer2_chain_t *hammer2_chain_alloc(hammer2_dev_t *hmp,
1587 hammer2_pfs_t *pmp,
fdf62707 1588 hammer2_blockref_t *bref);
23c7c7dd 1589void hammer2_chain_core_init(hammer2_chain_t *chain);
0dea3156 1590void hammer2_chain_ref(hammer2_chain_t *chain);
6d51e13a 1591void hammer2_chain_ref_hold(hammer2_chain_t *chain);
0dea3156 1592void hammer2_chain_drop(hammer2_chain_t *chain);
6d51e13a 1593void hammer2_chain_drop_unhold(hammer2_chain_t *chain);
3ad7c35f
MD
1594void hammer2_chain_unhold(hammer2_chain_t *chain);
1595void hammer2_chain_rehold(hammer2_chain_t *chain);
68b321c1 1596int hammer2_chain_lock(hammer2_chain_t *chain, int how);
6d51e13a 1597void hammer2_chain_lock_unhold(hammer2_chain_t *chain, int how);
f1c7c224 1598void hammer2_chain_load_data(hammer2_chain_t *chain);
bca9f8e6
MD
1599const hammer2_media_data_t *hammer2_chain_rdata(hammer2_chain_t *chain);
1600hammer2_media_data_t *hammer2_chain_wdata(hammer2_chain_t *chain);
1601
da0cdd33
MD
1602int hammer2_chain_inode_find(hammer2_pfs_t *pmp, hammer2_key_t inum,
1603 int clindex, int flags,
1604 hammer2_chain_t **parentp,
1605 hammer2_chain_t **chainp);
65cacacf 1606int hammer2_chain_modify(hammer2_chain_t *chain, hammer2_tid_t mtid,
3f01ebaa 1607 hammer2_off_t dedup_off, int flags);
65cacacf 1608int hammer2_chain_modify_ip(hammer2_inode_t *ip, hammer2_chain_t *chain,
e2163f5b 1609 hammer2_tid_t mtid, int flags);
65cacacf 1610int hammer2_chain_resize(hammer2_chain_t *chain,
3f01ebaa
MD
1611 hammer2_tid_t mtid, hammer2_off_t dedup_off,
1612 int nradix, int flags);
0dea3156 1613void hammer2_chain_unlock(hammer2_chain_t *chain);
6d51e13a 1614void hammer2_chain_unlock_hold(hammer2_chain_t *chain);
0dea3156 1615void hammer2_chain_wait(hammer2_chain_t *chain);
8138a154 1616hammer2_chain_t *hammer2_chain_get(hammer2_chain_t *parent, int generation,
40498d1c 1617 hammer2_blockref_t *bref, int how);
0dea3156
MD
1618hammer2_chain_t *hammer2_chain_lookup_init(hammer2_chain_t *parent, int flags);
1619void hammer2_chain_lookup_done(hammer2_chain_t *parent);
e0bfa1ed
MD
1620hammer2_chain_t *hammer2_chain_getparent(hammer2_chain_t *chain, int flags);
1621hammer2_chain_t *hammer2_chain_repparent(hammer2_chain_t **chainp, int flags);
0dea3156 1622hammer2_chain_t *hammer2_chain_lookup(hammer2_chain_t **parentp,
1897c66e 1623 hammer2_key_t *key_nextp,
c667909f 1624 hammer2_key_t key_beg, hammer2_key_t key_end,
c8c0a18a 1625 int *errorp, int flags);
0dea3156 1626hammer2_chain_t *hammer2_chain_next(hammer2_chain_t **parentp,
5c23d7f1 1627 hammer2_chain_t *chain,
1897c66e 1628 hammer2_key_t *key_nextp,
c667909f 1629 hammer2_key_t key_beg, hammer2_key_t key_end,
c8c0a18a 1630 int *errorp, int flags);
1e2c8208 1631int hammer2_chain_scan(hammer2_chain_t *parent,
125966e8
MD
1632 hammer2_chain_t **chainp,
1633 hammer2_blockref_t *bref,
2b8f3e7e 1634 int *firstp, int flags);
91abd410 1635
7fece146 1636int hammer2_chain_create(hammer2_chain_t **parentp, hammer2_chain_t **chainp,
ecfe89b8
MD
1637 hammer2_dev_t *hmp, hammer2_pfs_t *pmp,
1638 int methods, hammer2_key_t key, int keybits,
3f01ebaa
MD
1639 int type, size_t bytes, hammer2_tid_t mtid,
1640 hammer2_off_t dedup_off, int flags);
40498d1c 1641void hammer2_chain_rename(hammer2_chain_t **parentp,
e2163f5b
MD
1642 hammer2_chain_t *chain,
1643 hammer2_tid_t mtid, int flags);
65cacacf 1644int hammer2_chain_delete(hammer2_chain_t *parent, hammer2_chain_t *chain,
e2163f5b 1645 hammer2_tid_t mtid, int flags);
850d3f60
MD
1646int hammer2_chain_indirect_maintenance(hammer2_chain_t *parent,
1647 hammer2_chain_t *chain);
c603b86b 1648void hammer2_chain_setflush(hammer2_chain_t *chain);
51a0d27c 1649void hammer2_chain_countbrefs(hammer2_chain_t *chain,
1897c66e 1650 hammer2_blockref_t *base, int count);
f079a0a8 1651hammer2_chain_t *hammer2_chain_bulksnap(hammer2_dev_t *hmp);
21a90458 1652void hammer2_chain_bulkdrop(hammer2_chain_t *copy);
1897c66e 1653
a71db85d
MD
1654void hammer2_chain_setcheck(hammer2_chain_t *chain, void *bdata);
1655int hammer2_chain_testcheck(hammer2_chain_t *chain, void *bdata);
da0cdd33
MD
1656int hammer2_chain_dirent_test(hammer2_chain_t *chain, const char *name,
1657 size_t name_len);
a71db85d 1658
d2a41023 1659void hammer2_pfs_memory_wait(hammer2_pfs_t *pmp);
506bd6d1
MD
1660void hammer2_pfs_memory_inc(hammer2_pfs_t *pmp);
1661void hammer2_pfs_memory_wakeup(hammer2_pfs_t *pmp);
278ab2b2 1662
71d348d0 1663void hammer2_base_delete(hammer2_chain_t *parent,
51a0d27c 1664 hammer2_blockref_t *base, int count,
ecfe89b8
MD
1665 hammer2_chain_t *chain,
1666 hammer2_blockref_t *obref);
71d348d0 1667void hammer2_base_insert(hammer2_chain_t *parent,
51a0d27c 1668 hammer2_blockref_t *base, int count,
71d348d0 1669 hammer2_chain_t *chain,
850d3f60 1670 hammer2_blockref_t *elm);
0dea3156 1671
53f84d31
MD
1672/*
1673 * hammer2_flush.c
1674 */
65cacacf 1675int hammer2_flush(hammer2_chain_t *chain, int istop);
53f84d31
MD
1676void hammer2_delayed_flush(hammer2_chain_t *chain);
1677
0dea3156
MD
1678/*
1679 * hammer2_trans.c
1680 */
c603b86b 1681void hammer2_trans_init(hammer2_pfs_t *pmp, uint32_t flags);
ecfe89b8
MD
1682void hammer2_trans_setflags(hammer2_pfs_t *pmp, uint32_t flags);
1683void hammer2_trans_clearflags(hammer2_pfs_t *pmp, uint32_t flags);
e2163f5b 1684hammer2_tid_t hammer2_trans_sub(hammer2_pfs_t *pmp);
ecfe89b8 1685void hammer2_trans_done(hammer2_pfs_t *pmp, uint32_t flags);
c603b86b 1686hammer2_tid_t hammer2_trans_newinum(hammer2_pfs_t *pmp);
c7916d0b 1687void hammer2_trans_assert_strategy(hammer2_pfs_t *pmp);
3d4f397a 1688void hammer2_dedup_record(hammer2_chain_t *chain, hammer2_io_t *dio,
fda30e02 1689 const char *data);
50e4f8f4 1690
2910a90c
MD
1691/*
1692 * hammer2_ioctl.c
1693 */
1694int hammer2_ioctl(hammer2_inode_t *ip, u_long com, void *data,
1695 int fflag, struct ucred *cred);
1696
fdf62707
MD
1697/*
1698 * hammer2_io.c
1699 */
3d4f397a 1700void hammer2_io_inval(hammer2_io_t *dio, hammer2_off_t data_off, u_int bytes);
506bd6d1 1701void hammer2_io_cleanup(hammer2_dev_t *hmp, struct hammer2_io_tree *tree);
fdf62707 1702char *hammer2_io_data(hammer2_io_t *dio, off_t lbase);
04b8e839 1703void hammer2_io_bkvasync(hammer2_io_t *dio);
0b8efeb7
MD
1704void hammer2_io_dedup_set(hammer2_dev_t *hmp, hammer2_blockref_t *bref);
1705void hammer2_io_dedup_delete(hammer2_dev_t *hmp, uint8_t btype,
1706 hammer2_off_t data_off, u_int bytes);
1707void hammer2_io_dedup_assert(hammer2_dev_t *hmp, hammer2_off_t data_off,
1708 u_int bytes);
bca9f8e6 1709void hammer2_io_callback(struct bio *bio);
6c8a5902 1710int hammer2_io_new(hammer2_dev_t *hmp, int btype, off_t lbase, int lsize,
fdf62707 1711 hammer2_io_t **diop);
6c8a5902 1712int hammer2_io_newnz(hammer2_dev_t *hmp, int btype, off_t lbase, int lsize,
fdf62707 1713 hammer2_io_t **diop);
3e3e52cc
MD
1714int _hammer2_io_bread(hammer2_dev_t *hmp, int btype, off_t lbase, int lsize,
1715 hammer2_io_t **diop HAMMER2_IO_DEBUG_ARGS);
fdf62707 1716void hammer2_io_setdirty(hammer2_io_t *dio);
3e3e52cc
MD
1717
1718hammer2_io_t *_hammer2_io_getblk(hammer2_dev_t *hmp, int btype, off_t lbase,
1719 int lsize, int op HAMMER2_IO_DEBUG_ARGS);
1720hammer2_io_t *_hammer2_io_getquick(hammer2_dev_t *hmp, off_t lbase,
1721 int lsize HAMMER2_IO_DEBUG_ARGS);
1722void _hammer2_io_putblk(hammer2_io_t **diop HAMMER2_IO_DEBUG_ARGS);
1723int _hammer2_io_bwrite(hammer2_io_t **diop HAMMER2_IO_DEBUG_ARGS);
1724void _hammer2_io_bawrite(hammer2_io_t **diop HAMMER2_IO_DEBUG_ARGS);
1725void _hammer2_io_bdwrite(hammer2_io_t **diop HAMMER2_IO_DEBUG_ARGS);
1726void _hammer2_io_brelse(hammer2_io_t **diop HAMMER2_IO_DEBUG_ARGS);
1727void _hammer2_io_bqrelse(hammer2_io_t **diop HAMMER2_IO_DEBUG_ARGS);
1728void _hammer2_io_ref(hammer2_io_t *dio HAMMER2_IO_DEBUG_ARGS);
1729
1730#ifndef HAMMER2_IO_DEBUG
1731
1732#define hammer2_io_getblk(hmp, btype, lbase, lsize, op) \
1733 _hammer2_io_getblk((hmp), (btype), (lbase), (lsize), (op))
1734#define hammer2_io_getquick(hmp, lbase, lsize) \
1735 _hammer2_io_getquick((hmp), (lbase), (lsize))
1736#define hammer2_io_putblk(diop) \
1737 _hammer2_io_putblk(diop)
1738#define hammer2_io_bwrite(diop) \
1739 _hammer2_io_bwrite((diop))
1740#define hammer2_io_bawrite(diop) \
1741 _hammer2_io_bawrite((diop))
1742#define hammer2_io_bdwrite(diop) \
1743 _hammer2_io_bdwrite((diop))
1744#define hammer2_io_brelse(diop) \
1745 _hammer2_io_brelse((diop))
1746#define hammer2_io_bqrelse(diop) \
1747 _hammer2_io_bqrelse((diop))
1748#define hammer2_io_ref(dio) \
1749 _hammer2_io_ref((dio))
1750
1751#define hammer2_io_bread(hmp, btype, lbase, lsize, diop) \
1752 _hammer2_io_bread((hmp), (btype), (lbase), (lsize), (diop))
1753
1754#else
1755
1756#define hammer2_io_getblk(hmp, btype, lbase, lsize, op) \
1757 _hammer2_io_getblk((hmp), (btype), (lbase), (lsize), (op), \
1758 __FILE__, __LINE__)
1759
1760#define hammer2_io_getquick(hmp, lbase, lsize) \
1761 _hammer2_io_getquick((hmp), (lbase), (lsize), __FILE__, __LINE__)
1762
1763#define hammer2_io_putblk(diop) \
1764 _hammer2_io_putblk(diop, __FILE__, __LINE__)
1765
1766#define hammer2_io_bwrite(diop) \
1767 _hammer2_io_bwrite((diop), __FILE__, __LINE__)
1768#define hammer2_io_bawrite(diop) \
1769 _hammer2_io_bawrite((diop), __FILE__, __LINE__)
1770#define hammer2_io_bdwrite(diop) \
1771 _hammer2_io_bdwrite((diop), __FILE__, __LINE__)
1772#define hammer2_io_brelse(diop) \
1773 _hammer2_io_brelse((diop), __FILE__, __LINE__)
1774#define hammer2_io_bqrelse(diop) \
1775 _hammer2_io_bqrelse((diop), __FILE__, __LINE__)
1776#define hammer2_io_ref(dio) \
1777 _hammer2_io_ref((dio), __FILE__, __LINE__)
1778
1779#define hammer2_io_bread(hmp, btype, lbase, lsize, diop) \
1780 _hammer2_io_bread((hmp), (btype), (lbase), (lsize), (diop), \
1781 __FILE__, __LINE__)
1782
1783#endif
fdf62707 1784
2ed4fece 1785/*
3f01ebaa
MD
1786 * hammer2_thread.c
1787 */
660d007e 1788void hammer2_thr_signal(hammer2_thread_t *thr, uint32_t flags);
9dca9515
MD
1789void hammer2_thr_signal2(hammer2_thread_t *thr,
1790 uint32_t pflags, uint32_t nflags);
660d007e
MD
1791void hammer2_thr_wait(hammer2_thread_t *thr, uint32_t flags);
1792void hammer2_thr_wait_neg(hammer2_thread_t *thr, uint32_t flags);
9dca9515
MD
1793int hammer2_thr_wait_any(hammer2_thread_t *thr, uint32_t flags, int timo);
1794void hammer2_thr_create(hammer2_thread_t *thr,
1795 hammer2_pfs_t *pmp, hammer2_dev_t *hmp,
3f01ebaa
MD
1796 const char *id, int clindex, int repidx,
1797 void (*func)(void *arg));
1798void hammer2_thr_delete(hammer2_thread_t *thr);
1799void hammer2_thr_remaster(hammer2_thread_t *thr);
1800void hammer2_thr_freeze_async(hammer2_thread_t *thr);
1801void hammer2_thr_freeze(hammer2_thread_t *thr);
1802void hammer2_thr_unfreeze(hammer2_thread_t *thr);
1803int hammer2_thr_break(hammer2_thread_t *thr);
1804void hammer2_primary_xops_thread(void *arg);
1805
1806/*
1807 * hammer2_thread.c (XOP API)
2ed4fece 1808 */
c847e838 1809void hammer2_xop_group_init(hammer2_pfs_t *pmp, hammer2_xop_group_t *xgrp);
e2163f5b 1810void *hammer2_xop_alloc(hammer2_inode_t *ip, int flags);
e12ae3a5
MD
1811void hammer2_xop_setname(hammer2_xop_head_t *xop,
1812 const char *name, size_t name_len);
1813void hammer2_xop_setname2(hammer2_xop_head_t *xop,
1814 const char *name, size_t name_len);
cf1b3faf 1815size_t hammer2_xop_setname_inum(hammer2_xop_head_t *xop, hammer2_key_t inum);
e12ae3a5
MD
1816void hammer2_xop_setip2(hammer2_xop_head_t *xop, hammer2_inode_t *ip2);
1817void hammer2_xop_setip3(hammer2_xop_head_t *xop, hammer2_inode_t *ip3);
c603b86b 1818void hammer2_xop_reinit(hammer2_xop_head_t *xop);
c847e838
MD
1819void hammer2_xop_helper_create(hammer2_pfs_t *pmp);
1820void hammer2_xop_helper_cleanup(hammer2_pfs_t *pmp);
c4421f07
MD
1821void hammer2_xop_start(hammer2_xop_head_t *xop, hammer2_xop_desc_t *desc);
1822void hammer2_xop_start_except(hammer2_xop_head_t *xop, hammer2_xop_desc_t *desc,
01d71aa5 1823 int notidx);
c603b86b 1824int hammer2_xop_collect(hammer2_xop_head_t *xop, int flags);
19808ac9 1825void hammer2_xop_retire(hammer2_xop_head_t *xop, uint64_t mask);
c847e838
MD
1826int hammer2_xop_active(hammer2_xop_head_t *xop);
1827int hammer2_xop_feed(hammer2_xop_head_t *xop, hammer2_chain_t *chain,
1828 int clindex, int error);
1829
b7add675 1830/*
3f01ebaa
MD
1831 * hammer2_synchro.c
1832 */
1833void hammer2_primary_sync_thread(void *arg);
1834
1835/*
1836 * XOP backends in hammer2_xops.c, primarily for VNOPS. Other XOP backends
1837 * may be integrated into other source files.
b7add675 1838 */
c4421f07
MD
1839void hammer2_xop_ipcluster(hammer2_xop_t *xop, void *scratch, int clindex);
1840void hammer2_xop_readdir(hammer2_xop_t *xop, void *scratch, int clindex);
1841void hammer2_xop_nresolve(hammer2_xop_t *xop, void *scratch, int clindex);
1842void hammer2_xop_unlink(hammer2_xop_t *xop, void *scratch, int clindex);
1843void hammer2_xop_nrename(hammer2_xop_t *xop, void *scratch, int clindex);
1844void hammer2_xop_scanlhc(hammer2_xop_t *xop, void *scratch, int clindex);
1845void hammer2_xop_scanall(hammer2_xop_t *xop, void *scratch, int clindex);
1846void hammer2_xop_lookup(hammer2_xop_t *xop, void *scratch, int clindex);
1847void hammer2_xop_delete(hammer2_xop_t *xop, void *scratch, int clindex);
1848void hammer2_xop_inode_mkdirent(hammer2_xop_t *xop, void *scratch, int clindex);
1849void hammer2_xop_inode_create(hammer2_xop_t *xop, void *scratch, int clindex);
ecfe89b8
MD
1850void hammer2_xop_inode_create_det(hammer2_xop_t *xop,
1851 void *scratch, int clindex);
1852void hammer2_xop_inode_create_ins(hammer2_xop_t *xop,
1853 void *scratch, int clindex);
c4421f07
MD
1854void hammer2_xop_inode_destroy(hammer2_xop_t *xop, void *scratch, int clindex);
1855void hammer2_xop_inode_chain_sync(hammer2_xop_t *xop, void *scratch,
1856 int clindex);
1857void hammer2_xop_inode_unlinkall(hammer2_xop_t *xop, void *scratch,
1858 int clindex);
1859void hammer2_xop_inode_connect(hammer2_xop_t *xop, void *scratch, int clindex);
1860void hammer2_xop_inode_flush(hammer2_xop_t *xop, void *scratch, int clindex);
1861void hammer2_xop_strategy_read(hammer2_xop_t *xop, void *scratch, int clindex);
1862void hammer2_xop_strategy_write(hammer2_xop_t *xop, void *scratch, int clindex);
1863
1864void hammer2_dmsg_ipcluster(hammer2_xop_t *xop, void *scratch, int clindex);
1865void hammer2_dmsg_readdir(hammer2_xop_t *xop, void *scratch, int clindex);
1866void hammer2_dmsg_nresolve(hammer2_xop_t *xop, void *scratch, int clindex);
1867void hammer2_dmsg_unlink(hammer2_xop_t *xop, void *scratch, int clindex);
1868void hammer2_dmsg_nrename(hammer2_xop_t *xop, void *scratch, int clindex);
1869void hammer2_dmsg_scanlhc(hammer2_xop_t *xop, void *scratch, int clindex);
1870void hammer2_dmsg_scanall(hammer2_xop_t *xop, void *scratch, int clindex);
1871void hammer2_dmsg_lookup(hammer2_xop_t *xop, void *scratch, int clindex);
1872void hammer2_dmsg_inode_mkdirent(hammer2_xop_t *xop, void *scratch,
1873 int clindex);
1874void hammer2_dmsg_inode_create(hammer2_xop_t *xop, void *scratch, int clindex);
1875void hammer2_dmsg_inode_destroy(hammer2_xop_t *xop, void *scratch, int clindex);
1876void hammer2_dmsg_inode_chain_sync(hammer2_xop_t *xop, void *scratch,
1877 int clindex);
1878void hammer2_dmsg_inode_unlinkall(hammer2_xop_t *xop, void *scratch,
1879 int clindex);
1880void hammer2_dmsg_inode_connect(hammer2_xop_t *xop, void *scratch, int clindex);
1881void hammer2_dmsg_inode_flush(hammer2_xop_t *xop, void *scratch, int clindex);
1882void hammer2_dmsg_strategy_read(hammer2_xop_t *xop, void *scratch, int clindex);
1883void hammer2_dmsg_strategy_write(hammer2_xop_t *xop, void *scratch,
1884 int clindex);
1885
1886void hammer2_rmsg_ipcluster(hammer2_xop_t *xop, void *scratch, int clindex);
1887void hammer2_rmsg_readdir(hammer2_xop_t *xop, void *scratch, int clindex);
1888void hammer2_rmsg_nresolve(hammer2_xop_t *xop, void *scratch, int clindex);
1889void hammer2_rmsg_unlink(hammer2_xop_t *xop, void *scratch, int clindex);
1890void hammer2_rmsg_nrename(hammer2_xop_t *xop, void *scratch, int clindex);
1891void hammer2_rmsg_scanlhc(hammer2_xop_t *xop, void *scratch, int clindex);
1892void hammer2_rmsg_scanall(hammer2_xop_t *xop, void *scratch, int clindex);
1893void hammer2_rmsg_lookup(hammer2_xop_t *xop, void *scratch, int clindex);
1894void hammer2_rmsg_inode_mkdirent(hammer2_xop_t *xop, void *scratch,
1895 int clindex);
1896void hammer2_rmsg_inode_create(hammer2_xop_t *xop, void *scratch, int clindex);
1897void hammer2_rmsg_inode_destroy(hammer2_xop_t *xop, void *scratch, int clindex);
1898void hammer2_rmsg_inode_chain_sync(hammer2_xop_t *xop, void *scratch,
1899 int clindex);
1900void hammer2_rmsg_inode_unlinkall(hammer2_xop_t *xop, void *scratch,
1901 int clindex);
1902void hammer2_rmsg_inode_connect(hammer2_xop_t *xop, void *scratch, int clindex);
1903void hammer2_rmsg_inode_flush(hammer2_xop_t *xop, void *scratch, int clindex);
1904void hammer2_rmsg_strategy_read(hammer2_xop_t *xop, void *scratch, int clindex);
1905void hammer2_rmsg_strategy_write(hammer2_xop_t *xop, void *scratch,
1906 int clindex);
1907
1908extern hammer2_xop_desc_t hammer2_ipcluster_desc;
1909extern hammer2_xop_desc_t hammer2_readdir_desc;
1910extern hammer2_xop_desc_t hammer2_nresolve_desc;
1911extern hammer2_xop_desc_t hammer2_unlink_desc;
1912extern hammer2_xop_desc_t hammer2_nrename_desc;
1913extern hammer2_xop_desc_t hammer2_scanlhc_desc;
1914extern hammer2_xop_desc_t hammer2_scanall_desc;
1915extern hammer2_xop_desc_t hammer2_lookup_desc;
1916extern hammer2_xop_desc_t hammer2_delete_desc;
1917extern hammer2_xop_desc_t hammer2_inode_mkdirent_desc;
1918extern hammer2_xop_desc_t hammer2_inode_create_desc;
ecfe89b8
MD
1919extern hammer2_xop_desc_t hammer2_inode_create_det_desc;
1920extern hammer2_xop_desc_t hammer2_inode_create_ins_desc;
c4421f07
MD
1921extern hammer2_xop_desc_t hammer2_inode_destroy_desc;
1922extern hammer2_xop_desc_t hammer2_inode_chain_sync_desc;
1923extern hammer2_xop_desc_t hammer2_inode_unlinkall_desc;
1924extern hammer2_xop_desc_t hammer2_inode_connect_desc;
1925extern hammer2_xop_desc_t hammer2_inode_flush_desc;
1926extern hammer2_xop_desc_t hammer2_strategy_read_desc;
1927extern hammer2_xop_desc_t hammer2_strategy_write_desc;
2ed4fece 1928
9b8b748f
MD
1929/*
1930 * hammer2_msgops.c
1931 */
3a5aa68f
MD
1932int hammer2_msg_dbg_rcvmsg(kdmsg_msg_t *msg);
1933int hammer2_msg_adhoc_input(kdmsg_msg_t *msg);
26bf1a36 1934
1a34728c
MD
1935/*
1936 * hammer2_vfsops.c
1937 */
506bd6d1 1938void hammer2_volconf_update(hammer2_dev_t *hmp, int index);
40498d1c
MD
1939void hammer2_dump_chain(hammer2_chain_t *chain, int tab, int *countp, char pfx,
1940 u_int flags);
a7720be7 1941int hammer2_vfs_sync(struct mount *mp, int waitflags);
5afbe9d8 1942int hammer2_vfs_sync_pmp(hammer2_pfs_t *pmp, int waitfor);
65cacacf
MD
1943int hammer2_vfs_enospace(hammer2_inode_t *ip, off_t bytes, struct ucred *cred);
1944
b7add675 1945hammer2_pfs_t *hammer2_pfsalloc(hammer2_chain_t *chain,
506bd6d1 1946 const hammer2_inode_data_t *ripdata,
b02c0ae6
MD
1947 hammer2_tid_t modify_tid,
1948 hammer2_dev_t *force_local);
0d66a712 1949void hammer2_pfsdealloc(hammer2_pfs_t *pmp, int clindex, int destroying);
92e2ca6e
MD
1950int hammer2_vfs_vget(struct mount *mp, struct vnode *dvp,
1951 ino_t ino, struct vnode **vpp);
506bd6d1
MD
1952
1953void hammer2_lwinprog_ref(hammer2_pfs_t *pmp);
1954void hammer2_lwinprog_drop(hammer2_pfs_t *pmp);
d34788ef 1955void hammer2_lwinprog_wait(hammer2_pfs_t *pmp, int pipe);
1a34728c 1956
df9ea374
MD
1957/*
1958 * hammer2_freemap.c
1959 */
c603b86b
MD
1960int hammer2_freemap_alloc(hammer2_chain_t *chain, size_t bytes);
1961void hammer2_freemap_adjust(hammer2_dev_t *hmp,
91abd410
MD
1962 hammer2_blockref_t *bref, int how);
1963
278ab2b2
MD
1964/*
1965 * hammer2_cluster.c
1966 */
278ab2b2 1967uint8_t hammer2_cluster_type(hammer2_cluster_t *cluster);
278ab2b2 1968void hammer2_cluster_bref(hammer2_cluster_t *cluster, hammer2_blockref_t *bref);
506bd6d1 1969hammer2_cluster_t *hammer2_cluster_alloc(hammer2_pfs_t *pmp,
c603b86b 1970 hammer2_blockref_t *bref);
278ab2b2
MD
1971void hammer2_cluster_ref(hammer2_cluster_t *cluster);
1972void hammer2_cluster_drop(hammer2_cluster_t *cluster);
3ad7c35f
MD
1973void hammer2_cluster_unhold(hammer2_cluster_t *cluster);
1974void hammer2_cluster_rehold(hammer2_cluster_t *cluster);
23c7c7dd 1975void hammer2_cluster_lock(hammer2_cluster_t *cluster, int how);
c847e838
MD
1976int hammer2_cluster_check(hammer2_cluster_t *cluster, hammer2_key_t lokey,
1977 int flags);
23c7c7dd 1978void hammer2_cluster_resolve(hammer2_cluster_t *cluster);
0057d3b8 1979void hammer2_cluster_forcegood(hammer2_cluster_t *cluster);
278ab2b2 1980void hammer2_cluster_unlock(hammer2_cluster_t *cluster);
da6f36f4 1981
9dca9515
MD
1982void hammer2_bulkfree_init(hammer2_dev_t *hmp);
1983void hammer2_bulkfree_uninit(hammer2_dev_t *hmp);
f079a0a8
MD
1984int hammer2_bulkfree_pass(hammer2_dev_t *hmp, hammer2_chain_t *vchain,
1985 struct hammer2_ioc_bulkfree *bfi);
fda30e02
MD
1986void hammer2_dummy_xop_from_chain(hammer2_xop_head_t *xop,
1987 hammer2_chain_t *chain);
df9ea374 1988
d157d4dc
MD
1989/*
1990 * hammer2_iocom.c
1991 */
506bd6d1
MD
1992void hammer2_iocom_init(hammer2_dev_t *hmp);
1993void hammer2_iocom_uninit(hammer2_dev_t *hmp);
1994void hammer2_cluster_reconnect(hammer2_dev_t *hmp, struct file *fp);
d157d4dc 1995
b2900845
MD
1996/*
1997 * hammer2_strategy.c
1998 */
1999int hammer2_vop_strategy(struct vop_strategy_args *ap);
2000int hammer2_vop_bmap(struct vop_bmap_args *ap);
3c8b994d 2001void hammer2_write_thread(void *arg);
2ed4fece 2002void hammer2_bioq_sync(hammer2_pfs_t *pmp);
125966e8 2003void hammer2_dedup_clear(hammer2_dev_t *hmp);
b2900845 2004
fda30e02
MD
2005/*
2006 * More complex inlines
2007 */
3e3e52cc
MD
2008
2009#define hammer2_xop_gdata(xop) _hammer2_xop_gdata((xop), __FILE__, __LINE__)
2010
fda30e02
MD
2011static __inline
2012const hammer2_media_data_t *
3e3e52cc 2013_hammer2_xop_gdata(hammer2_xop_head_t *xop, const char *file, int line)
fda30e02
MD
2014{
2015 hammer2_chain_t *focus;
2016 const void *data;
2017
2018 focus = xop->cluster.focus;
2019 if (focus->dio) {
2020 lockmgr(&focus->diolk, LK_SHARED);
2021 if ((xop->focus_dio = focus->dio) != NULL) {
3e3e52cc 2022 _hammer2_io_ref(xop->focus_dio HAMMER2_IO_DEBUG_CALL);
fda30e02
MD
2023 hammer2_io_bkvasync(xop->focus_dio);
2024 }
2025 data = focus->data;
2026 lockmgr(&focus->diolk, LK_RELEASE);
2027 } else {
2028 data = focus->data;
2029 }
2030
2031 return data;
2032}
2033
3e3e52cc
MD
2034#define hammer2_xop_pdata(xop) _hammer2_xop_pdata((xop), __FILE__, __LINE__)
2035
fda30e02
MD
2036static __inline
2037void
3e3e52cc 2038_hammer2_xop_pdata(hammer2_xop_head_t *xop, const char *file, int line)
fda30e02
MD
2039{
2040 if (xop->focus_dio)
3e3e52cc 2041 _hammer2_io_putblk(&xop->focus_dio HAMMER2_IO_DEBUG_CALL);
fda30e02
MD
2042}
2043
47902fef
VS
2044#endif /* !_KERNEL */
2045#endif /* !_VFS_HAMMER2_HAMMER2_H_ */