sys/vfs/hammer: Remove obsolete code and comments
[dragonfly.git] / sys / vfs / hammer / hammer.h
CommitLineData
8750964d 1/*
1f07f686 2 * Copyright (c) 2007-2008 The DragonFly Project. All rights reserved.
745703c7 3 *
8750964d
MD
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
745703c7 6 *
8750964d
MD
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
745703c7 10 *
8750964d
MD
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
745703c7 20 *
8750964d
MD
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
8750964d
MD
33 */
34/*
35 * This header file contains structures used internally by the HAMMERFS
c60bb2c5 36 * implementation. See hammer_disk.h for on-disk structures.
8750964d
MD
37 */
38
427e5fc6 39#include <sys/param.h>
872a7eee 40#ifdef _KERNEL
427e5fc6
MD
41#include <sys/kernel.h>
42#include <sys/systm.h>
872a7eee 43#endif
23e66b3b 44#include <sys/conf.h>
8750964d
MD
45#include <sys/tree.h>
46#include <sys/malloc.h>
427e5fc6 47#include <sys/mount.h>
513ca7d7 48#include <sys/mountctl.h>
427e5fc6 49#include <sys/vnode.h>
42c7d26b 50#include <sys/proc.h>
895c1f85 51#include <sys/priv.h>
e63644f0 52#include <sys/stat.h>
427e5fc6 53#include <sys/globaldata.h>
66325755
MD
54#include <sys/lockf.h>
55#include <sys/buf.h>
8cd0a023 56#include <sys/queue.h>
bcac4bbb 57#include <sys/ktr.h>
9192654c 58#include <sys/limits.h>
c652be54 59#include <vm/vm_extern.h>
66325755 60
872a7eee 61#ifdef _KERNEL
23e66b3b 62#include <sys/buf2.h>
855942b6 63#include <sys/signal2.h>
81e764f0 64#include <vm/vm_page2.h>
872a7eee 65#endif
81e764f0 66
427e5fc6 67#include "hammer_disk.h"
8750964d 68#include "hammer_mount.h"
7dc57964 69#include "hammer_ioctl.h"
8750964d
MD
70
71#if defined(_KERNEL) || defined(_KERNEL_STRUCTURES)
72
73MALLOC_DECLARE(M_HAMMER);
74
bcac4bbb
MD
75/*
76 * Kernel trace
77 */
78#if !defined(KTR_HAMMER)
79#define KTR_HAMMER KTR_ALL
80#endif
879a1b60 81/* KTR_INFO_MASTER_EXTERN(hammer); */
bcac4bbb
MD
82
83/*
84 * Misc structures
85 */
66325755
MD
86struct hammer_mount;
87
8750964d
MD
88/*
89 * Key structure used for custom RB tree inode lookups. This prototypes
90 * the function hammer_ino_rb_tree_RB_LOOKUP_INFO(root, info).
91 */
92typedef struct hammer_inode_info {
513ca7d7 93 int64_t obj_id; /* (key) object identifier */
8750964d 94 hammer_tid_t obj_asof; /* (key) snapshot transid or 0 */
ddfdf542 95 u_int32_t obj_localization; /* (key) pseudo-fs */
43c665ae
MD
96 union {
97 struct hammer_btree_leaf_elm *leaf;
98 } u;
8750964d
MD
99} *hammer_inode_info_t;
100
b84de5af
MD
101typedef enum hammer_transaction_type {
102 HAMMER_TRANS_RO,
103 HAMMER_TRANS_STD,
104 HAMMER_TRANS_FLS
105} hammer_transaction_type_t;
106
66325755
MD
107/*
108 * HAMMER Transaction tracking
109 */
110struct hammer_transaction {
b84de5af 111 hammer_transaction_type_t type;
66325755
MD
112 struct hammer_mount *hmp;
113 hammer_tid_t tid;
ddfdf542 114 u_int64_t time;
dd94f1b1 115 u_int32_t time32;
2f85fa4d 116 int sync_lock_refs;
21fde338 117 int flags;
a89aec1b 118 struct hammer_volume *rootvol;
66325755
MD
119};
120
8cd0a023
MD
121typedef struct hammer_transaction *hammer_transaction_t;
122
21fde338 123#define HAMMER_TRANSF_NEWINODE 0x0001
82010f9f 124#define HAMMER_TRANSF_DIDIO 0x0002
4c286c36 125#define HAMMER_TRANSF_CRCDOM 0x0004 /* EDOM on CRC error, less critical */
21fde338 126
66325755
MD
127/*
128 * HAMMER locks
129 */
427e5fc6 130struct hammer_lock {
250aec18 131 volatile u_int refs; /* active references */
899eb297 132 volatile u_int lockval; /* lock count and control bits */
250aec18
MD
133 struct thread *lowner; /* owner if exclusively held */
134 struct thread *rowner; /* owner if exclusively held */
8750964d
MD
135};
136
250aec18
MD
137#define HAMMER_REFS_LOCKED 0x40000000 /* transition check */
138#define HAMMER_REFS_WANTED 0x20000000 /* transition check */
139#define HAMMER_REFS_CHECK 0x10000000 /* transition check */
140
141#define HAMMER_REFS_FLAGS (HAMMER_REFS_LOCKED | \
142 HAMMER_REFS_WANTED | \
143 HAMMER_REFS_CHECK)
144
899eb297 145#define HAMMER_LOCKF_EXCLUSIVE 0x40000000
250aec18 146#define HAMMER_LOCKF_WANTED 0x20000000
899eb297
MD
147
148static __inline int
149hammer_notlocked(struct hammer_lock *lock)
150{
151 return(lock->lockval == 0);
152}
153
427e5fc6
MD
154static __inline int
155hammer_islocked(struct hammer_lock *lock)
156{
899eb297 157 return(lock->lockval != 0);
427e5fc6
MD
158}
159
250aec18
MD
160/*
161 * Returns the number of refs on the object.
162 */
0b075555
MD
163static __inline int
164hammer_isactive(struct hammer_lock *lock)
165{
250aec18
MD
166 return(lock->refs & ~HAMMER_REFS_FLAGS);
167}
168
169static __inline int
170hammer_oneref(struct hammer_lock *lock)
171{
172 return((lock->refs & ~HAMMER_REFS_FLAGS) == 1);
0b075555
MD
173}
174
427e5fc6 175static __inline int
250aec18 176hammer_norefs(struct hammer_lock *lock)
427e5fc6 177{
250aec18
MD
178 return((lock->refs & ~HAMMER_REFS_FLAGS) == 0);
179}
180
181static __inline int
182hammer_norefsorlock(struct hammer_lock *lock)
183{
184 return(lock->refs == 0);
185}
186
187static __inline int
188hammer_refsorlock(struct hammer_lock *lock)
189{
190 return(lock->refs != 0);
427e5fc6 191}
c60bb2c5 192
6a37e7e4 193/*
7aa3b8a6 194 * Return if we specifically own the lock exclusively.
6a37e7e4
MD
195 */
196static __inline int
7aa3b8a6 197hammer_lock_excl_owned(struct hammer_lock *lock, thread_t td)
6a37e7e4 198{
899eb297 199 if ((lock->lockval & HAMMER_LOCKF_EXCLUSIVE) &&
250aec18 200 lock->lowner == td) {
7aa3b8a6 201 return(1);
899eb297 202 }
7aa3b8a6 203 return(0);
6a37e7e4
MD
204}
205
ec4e8497 206/*
1f07f686 207 * Flush state, used by various structures
ec4e8497 208 */
1f07f686
MD
209typedef enum hammer_inode_state {
210 HAMMER_FST_IDLE,
211 HAMMER_FST_SETUP,
212 HAMMER_FST_FLUSH
213} hammer_inode_state_t;
ec4e8497 214
1f07f686 215TAILQ_HEAD(hammer_record_list, hammer_record);
ec4e8497 216
5fa5c92f
MD
217/*
218 * Pseudo-filesystem extended data tracking
219 */
220struct hammer_pfs_rb_tree;
221struct hammer_pseudofs_inmem;
222RB_HEAD(hammer_pfs_rb_tree, hammer_pseudofs_inmem);
223RB_PROTOTYPE2(hammer_pfs_rb_tree, hammer_pseudofs_inmem, rb_node,
224 hammer_pfs_rb_compare, u_int32_t);
225
226struct hammer_pseudofs_inmem {
227 RB_ENTRY(hammer_pseudofs_inmem) rb_node;
228 struct hammer_lock lock;
229 u_int32_t localization;
ea434b6f 230 hammer_tid_t create_tid;
842e7a70 231 int flags;
a56cb012 232 udev_t fsid_udev;
5fa5c92f
MD
233 struct hammer_pseudofs_data pfsd;
234};
235
236typedef struct hammer_pseudofs_inmem *hammer_pseudofs_inmem_t;
237
842e7a70
MD
238#define HAMMER_PFSM_DELETED 0x0001
239
0729c8c8
MD
240/*
241 * Cache object ids. A fixed number of objid cache structures are
242 * created to reserve object id's for newly created files in multiples
243 * of 100,000, localized to a particular directory, and recycled as
244 * needed. This allows parallel create operations in different
245 * directories to retain fairly localized object ids which in turn
246 * improves reblocking performance and layout.
247 */
7d29aec0 248#define OBJID_CACHE_SIZE 2048
5a64efa1
MD
249#define OBJID_CACHE_BULK_BITS 10 /* 10 bits (1024) */
250#define OBJID_CACHE_BULK (32 * 32) /* two level (1024) */
251#define OBJID_CACHE_BULK_MASK (OBJID_CACHE_BULK - 1)
252#define OBJID_CACHE_BULK_MASK64 ((u_int64_t)(OBJID_CACHE_BULK - 1))
0729c8c8
MD
253
254typedef struct hammer_objid_cache {
255 TAILQ_ENTRY(hammer_objid_cache) entry;
256 struct hammer_inode *dip;
5a64efa1 257 hammer_tid_t base_tid;
0729c8c8 258 int count;
5a64efa1
MD
259 u_int32_t bm0;
260 u_int32_t bm1[32];
0729c8c8
MD
261} *hammer_objid_cache_t;
262
bcac4bbb
MD
263/*
264 * Associate an inode with a B-Tree node to cache search start positions
265 */
266typedef struct hammer_node_cache {
7a61b85d 267 TAILQ_ENTRY(hammer_node_cache) entry;
bcac4bbb
MD
268 struct hammer_node *node;
269 struct hammer_inode *ip;
270} *hammer_node_cache_t;
271
272TAILQ_HEAD(hammer_node_cache_list, hammer_node_cache);
273
507df98a
ID
274/*
275 * Live dedup cache
276 */
277struct hammer_dedup_crc_rb_tree;
278RB_HEAD(hammer_dedup_crc_rb_tree, hammer_dedup_cache);
279RB_PROTOTYPE2(hammer_dedup_crc_rb_tree, hammer_dedup_cache, crc_entry,
280 hammer_dedup_crc_rb_compare, hammer_crc_t);
281
282struct hammer_dedup_off_rb_tree;
283RB_HEAD(hammer_dedup_off_rb_tree, hammer_dedup_cache);
284RB_PROTOTYPE2(hammer_dedup_off_rb_tree, hammer_dedup_cache, off_entry,
285 hammer_dedup_off_rb_compare, hammer_off_t);
286
287#define DEDUP_CACHE_SIZE 4096 /* XXX make it a dynamic tunable */
288
289typedef struct hammer_dedup_cache {
290 RB_ENTRY(hammer_dedup_cache) crc_entry;
291 RB_ENTRY(hammer_dedup_cache) off_entry;
292 TAILQ_ENTRY(hammer_dedup_cache) lru_entry;
293 struct hammer_mount *hmp;
294 int64_t obj_id;
295 u_int32_t localization;
296 off_t file_offset;
297 int bytes;
745703c7 298 hammer_off_t data_offset;
507df98a
ID
299 hammer_crc_t crc;
300} *hammer_dedup_cache_t;
301
7a61b85d
MD
302/*
303 * Structure used to organize flush groups. Flush groups must be
304 * organized into chunks in order to avoid blowing out the UNDO FIFO.
305 * Without this a 'sync' could end up flushing 50,000 inodes in a single
306 * transaction.
307 */
ff003b11
MD
308struct hammer_fls_rb_tree;
309RB_HEAD(hammer_fls_rb_tree, hammer_inode);
310RB_PROTOTYPE(hammer_fls_rb_tree, hammer_inode, rb_flsnode,
311 hammer_ino_rb_compare);
312
7a61b85d
MD
313struct hammer_flush_group {
314 TAILQ_ENTRY(hammer_flush_group) flush_entry;
ff003b11 315 struct hammer_fls_rb_tree flush_tree;
37646115 316 int seq; /* our seq no */
7a61b85d
MD
317 int total_count; /* record load */
318 int running; /* group is running */
319 int closed;
320 int refs;
321};
322
323typedef struct hammer_flush_group *hammer_flush_group_t;
324
325TAILQ_HEAD(hammer_flush_group_list, hammer_flush_group);
326
8750964d 327/*
8cd0a023
MD
328 * Structure used to represent an inode in-memory.
329 *
330 * The record and data associated with an inode may be out of sync with
331 * the disk (xDIRTY flags), or not even on the disk at all (ONDISK flag
332 * clear).
333 *
334 * An inode may also hold a cache of unsynchronized records, used for
335 * database and directories only. Unsynchronized regular file data is
336 * stored in the buffer cache.
337 *
338 * NOTE: A file which is created and destroyed within the initial
339 * synchronization period can wind up not doing any disk I/O at all.
340 *
341 * Finally, an inode may cache numerous disk-referencing B-Tree cursors.
8750964d
MD
342 */
343struct hammer_ino_rb_tree;
344struct hammer_inode;
345RB_HEAD(hammer_ino_rb_tree, hammer_inode);
346RB_PROTOTYPEX(hammer_ino_rb_tree, INFO, hammer_inode, rb_node,
8cd0a023
MD
347 hammer_ino_rb_compare, hammer_inode_info_t);
348
73896937
MD
349struct hammer_redo_rb_tree;
350RB_HEAD(hammer_redo_rb_tree, hammer_inode);
351RB_PROTOTYPE2(hammer_redo_rb_tree, hammer_inode, rb_redonode,
352 hammer_redo_rb_compare, hammer_off_t);
353
8cd0a023
MD
354struct hammer_rec_rb_tree;
355struct hammer_record;
356RB_HEAD(hammer_rec_rb_tree, hammer_record);
357RB_PROTOTYPEX(hammer_rec_rb_tree, INFO, hammer_record, rb_node,
0832c9bb 358 hammer_rec_rb_compare, hammer_btree_leaf_elm_t);
8cd0a023
MD
359
360TAILQ_HEAD(hammer_node_list, hammer_node);
8750964d
MD
361
362struct hammer_inode {
1f07f686
MD
363 RB_ENTRY(hammer_inode) rb_node;
364 hammer_inode_state_t flush_state;
7a61b85d 365 hammer_flush_group_t flush_group;
ff003b11 366 RB_ENTRY(hammer_inode) rb_flsnode; /* when on flush list */
73896937 367 RB_ENTRY(hammer_inode) rb_redonode; /* when INODE_RDIRTY is set */
1f07f686 368 struct hammer_record_list target_list; /* target of dependant recs */
adf01747 369 int64_t obj_id; /* (key) object identifier */
b84de5af 370 hammer_tid_t obj_asof; /* (key) snapshot or 0 */
ddfdf542 371 u_int32_t obj_localization; /* (key) pseudo-fs */
7866ea2a
TK
372 struct hammer_mount *hmp;
373 hammer_objid_cache_t objid_cache;
b84de5af
MD
374 int flags;
375 int error; /* flush error */
376 int cursor_ip_refs; /* sanity */
e2a02b72 377 int cursor_exclreq_count;
47637bff 378 int rsv_recs;
b84de5af 379 struct vnode *vp;
5fa5c92f 380 hammer_pseudofs_inmem_t pfsm;
b84de5af
MD
381 struct lockf advlock;
382 struct hammer_lock lock; /* sync copy interlock */
b84de5af 383 off_t trunc_off;
11ad5ade 384 struct hammer_btree_leaf_elm ino_leaf; /* in-memory cache */
b84de5af
MD
385 struct hammer_inode_data ino_data; /* in-memory cache */
386 struct hammer_rec_rb_tree rec_tree; /* in-memory cache */
3214ade6 387 int rec_generation;
39d8fd63 388 struct hammer_node_cache cache[4]; /* search initiate cache */
b84de5af
MD
389
390 /*
391 * When a demark is created to synchronize an inode to
392 * disk, certain fields are copied so the front-end VOPs
393 * can continue to run in parallel with the synchronization
394 * occuring in the background.
395 */
396 int sync_flags; /* to-sync flags cache */
397 off_t sync_trunc_off; /* to-sync truncation */
a9d52b76 398 off_t save_trunc_off; /* write optimization */
11ad5ade 399 struct hammer_btree_leaf_elm sync_ino_leaf; /* to-sync cache */
b84de5af 400 struct hammer_inode_data sync_ino_data; /* to-sync cache */
9192654c 401 size_t redo_count;
73896937
MD
402
403 /*
404 * Track the earliest offset in the UNDO/REDO FIFO containing
405 * REDO records. This is staged to the backend during flush
406 * sequences. While the inode is staged redo_fifo_next is used
407 * to track the earliest offset for rotation into redo_fifo_start
408 * on completion of the flush.
409 */
410 hammer_off_t redo_fifo_start;
411 hammer_off_t redo_fifo_next;
8750964d
MD
412};
413
8cd0a023
MD
414typedef struct hammer_inode *hammer_inode_t;
415
66325755
MD
416#define VTOI(vp) ((struct hammer_inode *)(vp)->v_data)
417
9192654c
MD
418/*
419 * NOTE: DDIRTY does not include atime or mtime and does not include
420 * write-append size changes. SDIRTY handles write-append size
421 * changes.
47f363f1
MD
422 *
423 * REDO indicates that REDO logging is active, creating a definitive
424 * stream of REDO records in the UNDO/REDO log for writes and
425 * truncations, including boundary records when/if REDO is turned off.
426 * REDO is typically enabled by fsync() and turned off if excessive
427 * writes without an fsync() occurs.
428 *
429 * RDIRTY indicates that REDO records were laid down in the UNDO/REDO
430 * FIFO (even if REDO is turned off some might still be active) and
431 * still being tracked for this inode. See hammer_redo.c
9192654c 432 */
ddfdf542 433 /* (not including atime/mtime) */
9192654c 434#define HAMMER_INODE_DDIRTY 0x0001 /* in-memory ino_data is dirty */
e63644f0 435#define HAMMER_INODE_RSV_INODES 0x0002 /* hmp->rsv_inodes bumped */
7b6ccb11 436#define HAMMER_INODE_CONN_DOWN 0x0004 /* include in downward recursion */
1f07f686 437#define HAMMER_INODE_XDIRTY 0x0008 /* in-memory records */
8cd0a023 438#define HAMMER_INODE_ONDISK 0x0010 /* inode is on-disk (else not yet) */
a89aec1b 439#define HAMMER_INODE_FLUSH 0x0020 /* flush on last ref */
869e8f55 440#define HAMMER_INODE_DELETED 0x0080 /* inode delete (backend) */
76376933 441#define HAMMER_INODE_DELONDISK 0x0100 /* delete synchronized to disk */
d113fda1 442#define HAMMER_INODE_RO 0x0200 /* read-only (because of as-of) */
a117fbeb 443#define HAMMER_INODE_RECSW 0x0400 /* waiting on data record flush */
0a72edae 444#define HAMMER_INODE_DONDISK 0x0800 /* data records may be on disk */
f3b0f382 445#define HAMMER_INODE_BUFS 0x1000 /* dirty high level bps present */
f153644d 446#define HAMMER_INODE_REFLUSH 0x2000 /* flush on dependancy / reflush */
9f5097dc 447#define HAMMER_INODE_RECLAIM 0x4000 /* trying to reclaim */
059819e3 448#define HAMMER_INODE_FLUSHW 0x8000 /* Someone waiting for flush */
8cd0a023 449
b84de5af 450#define HAMMER_INODE_TRUNCATED 0x00010000
869e8f55 451#define HAMMER_INODE_DELETING 0x00020000 /* inode delete request (frontend)*/
4e17f465 452#define HAMMER_INODE_RESIGNAL 0x00040000 /* re-signal on re-flush */
ddfdf542 453#define HAMMER_INODE_ATIME 0x00100000 /* in-memory atime modified */
06ad81ff
MD
454#define HAMMER_INODE_MTIME 0x00200000 /* in-memory mtime modified */
455#define HAMMER_INODE_WOULDBLOCK 0x00400000 /* re-issue to new flush group */
7866ea2a 456#define HAMMER_INODE_DUMMY 0x00800000 /* dummy inode covering bad file */
9192654c 457#define HAMMER_INODE_SDIRTY 0x01000000 /* in-memory ino_data.size is dirty*/
47f363f1
MD
458#define HAMMER_INODE_REDO 0x02000000 /* REDO logging active */
459#define HAMMER_INODE_RDIRTY 0x04000000 /* REDO records active in fifo */
e2a02b72 460#define HAMMER_INODE_SLAVEFLUSH 0x08000000 /* being flushed by slave */
b84de5af 461
9192654c 462#define HAMMER_INODE_MODMASK (HAMMER_INODE_DDIRTY|HAMMER_INODE_SDIRTY| \
11ad5ade 463 HAMMER_INODE_XDIRTY|HAMMER_INODE_BUFS| \
ddfdf542
MD
464 HAMMER_INODE_ATIME|HAMMER_INODE_MTIME| \
465 HAMMER_INODE_TRUNCATED|HAMMER_INODE_DELETING)
1f07f686 466
9192654c 467#define HAMMER_INODE_MODMASK_NOXDIRTY \
1f07f686 468 (HAMMER_INODE_MODMASK & ~HAMMER_INODE_XDIRTY)
0b075555 469
9192654c
MD
470#define HAMMER_INODE_MODMASK_NOREDO \
471 (HAMMER_INODE_DDIRTY| \
472 HAMMER_INODE_XDIRTY| \
473 HAMMER_INODE_TRUNCATED|HAMMER_INODE_DELETING)
474
f90dde4c 475#define HAMMER_FLUSH_SIGNAL 0x0001
4e17f465 476#define HAMMER_FLUSH_RECURSION 0x0002
f90dde4c 477
7bc5b8c2
MD
478/*
479 * Used by the inode reclaim code to pipeline reclaims and avoid
480 * blowing out kernel memory or letting the flusher get too far
82010f9f
MD
481 * behind. The reclaim wakes up when count reaches 0 or the
482 * timer expires.
7bc5b8c2
MD
483 */
484struct hammer_reclaim {
485 TAILQ_ENTRY(hammer_reclaim) entry;
82010f9f 486 int count;
7bc5b8c2
MD
487};
488
e98f1b96
MD
489/*
490 * Track who is creating the greatest burden on the
491 * inode cache.
492 */
493struct hammer_inostats {
494 pid_t pid; /* track user process */
495 int ltick; /* last tick */
496 int count; /* count (degenerates) */
497};
498
499#define HAMMER_INOSTATS_HSIZE 32
500#define HAMMER_INOSTATS_HMASK (HAMMER_INOSTATS_HSIZE - 1)
501
8750964d 502/*
1f07f686
MD
503 * Structure used to represent an unsynchronized record in-memory. These
504 * records typically represent directory entries. Only non-historical
505 * records are kept in-memory.
506 *
507 * Records are organized as a per-inode RB-Tree. If the inode is not
8cd0a023
MD
508 * on disk then neither are any records and the in-memory record tree
509 * represents the entire contents of the inode. If the inode is on disk
510 * then the on-disk B-Tree is scanned in parallel with the in-memory
511 * RB-Tree to synthesize the current state of the file.
512 *
1f07f686
MD
513 * Records are also used to enforce the ordering of directory create/delete
514 * operations. A new inode will not be flushed to disk unless its related
515 * directory entry is also being flushed at the same time. A directory entry
516 * will not be removed unless its related inode is also being removed at the
517 * same time.
8750964d 518 */
1f07f686 519typedef enum hammer_record_type {
e8599db1 520 HAMMER_MEM_RECORD_GENERAL, /* misc record */
930bf163 521 HAMMER_MEM_RECORD_INODE, /* inode record */
1f07f686 522 HAMMER_MEM_RECORD_ADD, /* positive memory cache record */
47637bff
MD
523 HAMMER_MEM_RECORD_DEL, /* negative delete-on-disk record */
524 HAMMER_MEM_RECORD_DATA /* bulk-data record w/on-disk ref */
1f07f686
MD
525} hammer_record_type_t;
526
8cd0a023
MD
527struct hammer_record {
528 RB_ENTRY(hammer_record) rb_node;
1f07f686
MD
529 TAILQ_ENTRY(hammer_record) target_entry;
530 hammer_inode_state_t flush_state;
7a61b85d 531 hammer_flush_group_t flush_group;
1f07f686 532 hammer_record_type_t type;
a89aec1b 533 struct hammer_lock lock;
0832c9bb 534 struct hammer_reserve *resv;
8cd0a023 535 struct hammer_inode *ip;
1f07f686 536 struct hammer_inode *target_ip;
11ad5ade 537 struct hammer_btree_leaf_elm leaf;
8cd0a023 538 union hammer_data_ondisk *data;
8cd0a023 539 int flags;
77912481 540 int gflags;
e469566b 541 hammer_off_t zone2_offset; /* direct-write only */
8cd0a023 542};
8750964d 543
8cd0a023
MD
544typedef struct hammer_record *hammer_record_t;
545
d36ec43b
MD
546/*
547 * Record flags. Note that FE can only be set by the frontend if the
548 * record has not been interlocked by the backend w/ BE.
549 */
8cd0a023
MD
550#define HAMMER_RECF_ALLOCDATA 0x0001
551#define HAMMER_RECF_ONRBTREE 0x0002
b84de5af 552#define HAMMER_RECF_DELETED_FE 0x0004 /* deleted (frontend) */
d36ec43b 553#define HAMMER_RECF_DELETED_BE 0x0008 /* deleted (backend) */
1b0ab2c3 554#define HAMMER_RECF_COMMITTED 0x0010 /* committed to the B-Tree */
d36ec43b 555#define HAMMER_RECF_INTERLOCK_BE 0x0020 /* backend interlock */
0832c9bb 556#define HAMMER_RECF_WANTED 0x0040 /* wanted by the frontend */
507df98a 557#define HAMMER_RECF_DEDUPED 0x0080 /* will be live-dedup'ed */
7866ea2a 558#define HAMMER_RECF_CONVERT_DELETE 0x0100 /* special case */
47f363f1 559#define HAMMER_RECF_REDO 0x1000 /* REDO was laid down */
8cd0a023 560
77912481
MD
561/*
562 * These flags must be separate to deal with SMP races
563 */
564#define HAMMER_RECG_DIRECT_IO 0x0001 /* related direct I/O running*/
565#define HAMMER_RECG_DIRECT_WAIT 0x0002 /* related direct I/O running*/
566#define HAMMER_RECG_DIRECT_INVAL 0x0004 /* buffer alias invalidation */
602c6cb8 567/*
83f2a3aa 568 * hammer_create_at_cursor() and hammer_delete_at_cursor() flags.
602c6cb8 569 */
83f2a3aa
MD
570#define HAMMER_CREATE_MODE_UMIRROR 0x0001
571#define HAMMER_CREATE_MODE_SYS 0x0002
572
602c6cb8
MD
573#define HAMMER_DELETE_ADJUST 0x0001
574#define HAMMER_DELETE_DESTROY 0x0002
575
8cd0a023 576/*
47197d71 577 * In-memory structures representing on-disk structures.
8cd0a023 578 */
8750964d 579struct hammer_volume;
427e5fc6 580struct hammer_buffer;
8cd0a023 581struct hammer_node;
e8599db1 582struct hammer_undo;
0832c9bb
MD
583struct hammer_reserve;
584
8750964d 585RB_HEAD(hammer_vol_rb_tree, hammer_volume);
427e5fc6 586RB_HEAD(hammer_buf_rb_tree, hammer_buffer);
8cd0a023 587RB_HEAD(hammer_nod_rb_tree, hammer_node);
e8599db1 588RB_HEAD(hammer_und_rb_tree, hammer_undo);
0832c9bb 589RB_HEAD(hammer_res_rb_tree, hammer_reserve);
1afb73cf 590RB_HEAD(hammer_mod_rb_tree, hammer_io);
8750964d
MD
591
592RB_PROTOTYPE2(hammer_vol_rb_tree, hammer_volume, rb_node,
593 hammer_vol_rb_compare, int32_t);
427e5fc6 594RB_PROTOTYPE2(hammer_buf_rb_tree, hammer_buffer, rb_node,
47197d71 595 hammer_buf_rb_compare, hammer_off_t);
8cd0a023 596RB_PROTOTYPE2(hammer_nod_rb_tree, hammer_node, rb_node,
47197d71 597 hammer_nod_rb_compare, hammer_off_t);
e8599db1
MD
598RB_PROTOTYPE2(hammer_und_rb_tree, hammer_undo, rb_node,
599 hammer_und_rb_compare, hammer_off_t);
0832c9bb
MD
600RB_PROTOTYPE2(hammer_res_rb_tree, hammer_reserve, rb_node,
601 hammer_res_rb_compare, hammer_off_t);
1afb73cf
MD
602RB_PROTOTYPE2(hammer_mod_rb_tree, hammer_io, rb_node,
603 hammer_mod_rb_compare, hammer_off_t);
8750964d 604
66325755
MD
605/*
606 * IO management - embedded at the head of various in-memory structures
10a5d1ba
MD
607 *
608 * VOLUME - hammer_volume containing meta-data
609 * META_BUFFER - hammer_buffer containing meta-data
88cecadc 610 * UNDO_BUFFER - hammer_buffer containing undo-data
10a5d1ba 611 * DATA_BUFFER - hammer_buffer containing pure-data
88cecadc 612 * DUMMY - hammer_buffer not containing valid data
10a5d1ba
MD
613 *
614 * Dirty volume headers and dirty meta-data buffers are locked until the
615 * flusher can sequence them out. Dirty pure-data buffers can be written.
616 * Clean buffers can be passively released.
66325755 617 */
10a5d1ba
MD
618typedef enum hammer_io_type {
619 HAMMER_STRUCTURE_VOLUME,
620 HAMMER_STRUCTURE_META_BUFFER,
621 HAMMER_STRUCTURE_UNDO_BUFFER,
eddadaee
MD
622 HAMMER_STRUCTURE_DATA_BUFFER,
623 HAMMER_STRUCTURE_DUMMY
10a5d1ba 624} hammer_io_type_t;
66325755
MD
625
626union hammer_io_structure;
055f5ff8 627struct hammer_io;
66325755
MD
628
629struct worklist {
630 LIST_ENTRY(worklist) node;
631};
632
10a5d1ba
MD
633TAILQ_HEAD(hammer_io_list, hammer_io);
634typedef struct hammer_io_list *hammer_io_list_t;
055f5ff8 635
66325755 636struct hammer_io {
10a5d1ba
MD
637 struct worklist worklist;
638 struct hammer_lock lock;
639 enum hammer_io_type type;
640 struct hammer_mount *hmp;
748efb59 641 struct hammer_volume *volume;
1afb73cf 642 RB_ENTRY(hammer_io) rb_node; /* if modified */
eddadaee 643 TAILQ_ENTRY(hammer_io) iorun_entry; /* iorun_list */
1afb73cf 644 struct hammer_mod_rb_tree *mod_root;
10a5d1ba 645 struct buf *bp;
4a2796f3
MD
646 int64_t offset; /* zone-2 offset */
647 int bytes; /* buffer cache buffer size */
10a5d1ba
MD
648 int modify_refs;
649
77912481
MD
650 /*
651 * These can be modified at any time by the backend while holding
652 * io_token, due to bio_done and hammer_io_complete() callbacks.
653 */
055f5ff8
MD
654 u_int running : 1; /* bp write IO in progress */
655 u_int waiting : 1; /* someone is waiting on us */
77912481
MD
656 u_int ioerror : 1; /* abort on io-error */
657 u_int unusedA : 29;
658
659 /*
660 * These can only be modified by the frontend while holding
661 * fs_token, or by the backend while holding the io interlocked
662 * with no references (which will block the frontend when it
663 * tries to reference it).
664 *
665 * WARNING! SMP RACES will create havoc if the callbacks ever tried
666 * to modify any of these outside the above restrictions.
667 */
668 u_int modified : 1; /* bp's data was modified */
669 u_int released : 1; /* bp released (w/ B_LOCKED set) */
b33e2cc0 670 u_int validated : 1; /* ondisk has been validated */
b58c6388 671 u_int waitdep : 1; /* flush waits for dependancies */
51c35492 672 u_int recovered : 1; /* has recovery ref */
9f5097dc 673 u_int waitmod : 1; /* waiting for modify_refs */
cebe9493 674 u_int reclaim : 1; /* reclaim requested */
bcac4bbb 675 u_int gencrc : 1; /* crc needs to be generated */
77912481 676 u_int unusedB : 24;
66325755
MD
677};
678
8cd0a023
MD
679typedef struct hammer_io *hammer_io_t;
680
af209b0f
MD
681#define HAMMER_CLUSTER_SIZE (64 * 1024)
682#if HAMMER_CLUSTER_SIZE > MAXBSIZE
683#undef HAMMER_CLUSTER_SIZE
684#define HAMMER_CLUSTER_SIZE MAXBSIZE
685#endif
686#define HAMMER_CLUSTER_BUFS (HAMMER_CLUSTER_SIZE / HAMMER_BUFSIZE)
687
66325755 688/*
8cd0a023 689 * In-memory volume representing on-disk buffer
66325755 690 */
8750964d 691struct hammer_volume {
66325755 692 struct hammer_io io;
8750964d 693 RB_ENTRY(hammer_volume) rb_node;
8750964d
MD
694 struct hammer_volume_ondisk *ondisk;
695 int32_t vol_no;
fbc6e32a 696 int64_t nblocks; /* note: special calculation for statfs */
47197d71 697 int64_t buffer_base; /* base offset of buffer 0 */
2f85fa4d
MD
698 hammer_off_t maxbuf_off; /* Maximum buffer offset (zone-2) */
699 hammer_off_t maxraw_off; /* Maximum raw offset for device */
c60bb2c5 700 char *vol_name;
8750964d 701 struct vnode *devvp;
427e5fc6 702 int vol_flags;
8750964d
MD
703};
704
8cd0a023
MD
705typedef struct hammer_volume *hammer_volume_t;
706
66325755 707/*
8cd0a023
MD
708 * In-memory buffer (other then volume, super-cluster, or cluster),
709 * representing an on-disk buffer.
66325755 710 */
427e5fc6 711struct hammer_buffer {
66325755 712 struct hammer_io io;
427e5fc6 713 RB_ENTRY(hammer_buffer) rb_node;
47197d71 714 void *ondisk;
34d829f7 715 hammer_off_t zoneX_offset;
0832c9bb 716 hammer_off_t zone2_offset;
cebe9493 717 struct hammer_reserve *resv;
8cd0a023 718 struct hammer_node_list clist;
66325755
MD
719};
720
8cd0a023
MD
721typedef struct hammer_buffer *hammer_buffer_t;
722
723/*
724 * In-memory B-Tree node, representing an on-disk B-Tree node.
725 *
726 * This is a hang-on structure which is backed by a hammer_buffer,
727 * indexed by a hammer_cluster, and used for fine-grained locking of
728 * B-Tree nodes in order to properly control lock ordering. A hammer_buffer
729 * can contain multiple nodes representing wildly disassociated portions
730 * of the B-Tree so locking cannot be done on a buffer-by-buffer basis.
731 *
732 * This structure uses a cluster-relative index to reduce the number
733 * of layers required to access it, and also because all on-disk B-Tree
734 * references are cluster-relative offsets.
735 */
736struct hammer_node {
737 struct hammer_lock lock; /* node-by-node lock */
738 TAILQ_ENTRY(hammer_node) entry; /* per-buffer linkage */
739 RB_ENTRY(hammer_node) rb_node; /* per-cluster linkage */
47197d71 740 hammer_off_t node_offset; /* full offset spec */
40043e7f 741 struct hammer_mount *hmp;
8cd0a023
MD
742 struct hammer_buffer *buffer; /* backing buffer */
743 hammer_node_ondisk_t ondisk; /* ptr to on-disk structure */
b3bad96f 744 TAILQ_HEAD(, hammer_cursor) cursor_list; /* deadlock recovery */
bcac4bbb 745 struct hammer_node_cache_list cache_list; /* passive caches */
b3deaf57 746 int flags;
e2a02b72 747 int cursor_exclreq_count;
8cd0a023
MD
748};
749
b3deaf57
MD
750#define HAMMER_NODE_DELETED 0x0001
751#define HAMMER_NODE_FLUSH 0x0002
bcac4bbb
MD
752#define HAMMER_NODE_CRCGOOD 0x0004
753#define HAMMER_NODE_NEEDSCRC 0x0008
c82af904 754#define HAMMER_NODE_NEEDSMIRROR 0x0010
4c286c36 755#define HAMMER_NODE_CRCBAD 0x0020
fa2b9a03 756#define HAMMER_NODE_NONLINEAR 0x0040 /* linear heuristic */
4c286c36
MD
757
758#define HAMMER_NODE_CRCANY (HAMMER_NODE_CRCGOOD | HAMMER_NODE_CRCBAD)
b3deaf57 759
8cd0a023
MD
760typedef struct hammer_node *hammer_node_t;
761
b33e2cc0 762/*
1775b6a0
MD
763 * List of locked nodes. This structure is used to lock potentially large
764 * numbers of nodes as an aid for complex B-Tree operations.
b33e2cc0 765 */
1775b6a0
MD
766struct hammer_node_lock;
767TAILQ_HEAD(hammer_node_lock_list, hammer_node_lock);
768
769struct hammer_node_lock {
770 TAILQ_ENTRY(hammer_node_lock) entry;
771 struct hammer_node_lock_list list;
772 struct hammer_node_lock *parent;
b33e2cc0 773 hammer_node_t node;
1775b6a0
MD
774 hammer_node_ondisk_t copy; /* copy of on-disk data */
775 int index; /* index of this node in parent */
776 int count; /* count children */
777 int flags;
b33e2cc0
MD
778};
779
1775b6a0 780typedef struct hammer_node_lock *hammer_node_lock_t;
b33e2cc0 781
1775b6a0 782#define HAMMER_NODE_LOCK_UPDATED 0x0001
24cf83d2 783#define HAMMER_NODE_LOCK_LCACHE 0x0002
b33e2cc0 784
8cd0a023
MD
785/*
786 * Common I/O management structure - embedded in in-memory structures
787 * which are backed by filesystem buffers.
788 */
66325755
MD
789union hammer_io_structure {
790 struct hammer_io io;
791 struct hammer_volume volume;
66325755 792 struct hammer_buffer buffer;
8750964d
MD
793};
794
055f5ff8
MD
795typedef union hammer_io_structure *hammer_io_structure_t;
796
cebe9493
MD
797/*
798 * The reserve structure prevents the blockmap from allocating
a981af19 799 * out of a reserved big-block. Such reservations are used by
cebe9493
MD
800 * the direct-write mechanism.
801 *
802 * The structure is also used to hold off on reallocations of
d165c90a 803 * big-blocks from the freemap until flush dependancies have
cebe9493
MD
804 * been dealt with.
805 */
0832c9bb
MD
806struct hammer_reserve {
807 RB_ENTRY(hammer_reserve) rb_node;
cebe9493
MD
808 TAILQ_ENTRY(hammer_reserve) delay_entry;
809 int flush_group;
cb51be26 810 int flags;
0832c9bb 811 int refs;
cb51be26 812 int zone;
df301614 813 int append_off;
507df98a 814 int32_t bytes_free;
cebe9493 815 hammer_off_t zone_offset;
0832c9bb
MD
816};
817
818typedef struct hammer_reserve *hammer_reserve_t;
819
cb51be26 820#define HAMMER_RESF_ONDELAY 0x0001
5e435c92 821#define HAMMER_RESF_LAYER2FREE 0x0002
cb51be26 822
8cd0a023
MD
823#include "hammer_cursor.h"
824
e8599db1 825/*
cebe9493
MD
826 * The undo structure tracks recent undos to avoid laying down duplicate
827 * undos within a flush group, saving us a significant amount of overhead.
828 *
829 * This is strictly a heuristic.
e8599db1 830 */
7a61b85d
MD
831#define HAMMER_MAX_UNDOS 1024
832#define HAMMER_MAX_FLUSHERS 4
e8599db1
MD
833
834struct hammer_undo {
835 RB_ENTRY(hammer_undo) rb_node;
836 TAILQ_ENTRY(hammer_undo) lru_entry;
837 hammer_off_t offset;
838 int bytes;
839};
840
841typedef struct hammer_undo *hammer_undo_t;
842
af209b0f 843struct hammer_flusher_info;
7a61b85d 844TAILQ_HEAD(hammer_flusher_info_list, hammer_flusher_info);
da2da375
MD
845
846struct hammer_flusher {
847 int signal; /* flusher thread sequencer */
e86903d8 848 int done; /* last completed flush group */
37646115 849 int next; /* next unallocated flg seqno */
da2da375
MD
850 int group_lock; /* lock sequencing of the next flush */
851 int exiting; /* request master exit */
da2da375
MD
852 thread_t td; /* master flusher thread */
853 hammer_tid_t tid; /* last flushed transaction id */
854 int finalize_want; /* serialize finalization */
855 struct hammer_lock finalize_lock; /* serialize finalization */
856 struct hammer_transaction trans; /* shared transaction */
7a61b85d
MD
857 struct hammer_flusher_info_list run_list;
858 struct hammer_flusher_info_list ready_list;
da2da375
MD
859};
860
9192654c
MD
861#define HAMMER_FLUSH_UNDOS_RELAXED 0
862#define HAMMER_FLUSH_UNDOS_FORCED 1
863#define HAMMER_FLUSH_UNDOS_AUTO 2
8750964d
MD
864/*
865 * Internal hammer mount data structure
866 */
867struct hammer_mount {
868 struct mount *mp;
8750964d 869 struct hammer_ino_rb_tree rb_inos_root;
73896937 870 struct hammer_redo_rb_tree rb_redo_root;
8750964d 871 struct hammer_vol_rb_tree rb_vols_root;
40043e7f 872 struct hammer_nod_rb_tree rb_nods_root;
e8599db1 873 struct hammer_und_rb_tree rb_undo_root;
0832c9bb
MD
874 struct hammer_res_rb_tree rb_resv_root;
875 struct hammer_buf_rb_tree rb_bufs_root;
5fa5c92f 876 struct hammer_pfs_rb_tree rb_pfsm_root;
507df98a
ID
877
878 struct hammer_dedup_crc_rb_tree rb_dedup_crc_root;
879 struct hammer_dedup_off_rb_tree rb_dedup_off_root;
880
8750964d 881 struct hammer_volume *rootvol;
47197d71
MD
882 struct hammer_base_elm root_btree_beg;
883 struct hammer_base_elm root_btree_end;
bac808fe
MD
884
885 struct malloc_type *m_misc;
886 struct malloc_type *m_inodes;
887
cdb6e4e6 888 int flags; /* HAMMER_MOUNT_xxx flags */
195c19a1
MD
889 int hflags;
890 int ronly;
891 int nvolumes;
d26d0ae9 892 int volume_iterator;
732a1697 893 int master_id; /* -1 or 0-15 - clustering and mirroring */
44a83111 894 int version; /* hammer filesystem version to use */
e63644f0 895 int rsv_inodes; /* reserved space due to dirty inodes */
a7e9bef1 896 int64_t rsv_databytes; /* reserved space due to record data */
e63644f0 897 int rsv_recs; /* reserved space due to dirty records */
a981af19 898 int rsv_fromdelay; /* big-blocks reserved due to flush delay */
7a61b85d 899 int undo_rec_limit; /* based on size of undo area */
4a2796f3
MD
900 int last_newrecords;
901 int count_newrecords;
da2da375 902
865c9609
MN
903 int volume_to_remove; /* volume that is currently being removed */
904
9f5097dc 905 int count_inodes; /* total number of inodes */
af209b0f 906 int count_iqueued; /* inodes queued to flusher */
e2a02b72 907 int count_reclaims; /* inodes pending reclaim by flusher */
da2da375
MD
908
909 struct hammer_flusher flusher;
910
855942b6 911 u_int check_interrupt;
3e583440 912 u_int check_yield;
8750964d 913 uuid_t fsid;
1afb73cf
MD
914 struct hammer_mod_rb_tree volu_root; /* dirty undo buffers */
915 struct hammer_mod_rb_tree undo_root; /* dirty undo buffers */
916 struct hammer_mod_rb_tree data_root; /* dirty data buffers */
917 struct hammer_mod_rb_tree meta_root; /* dirty meta bufs */
918 struct hammer_mod_rb_tree lose_root; /* loose buffers */
3583bbb4
MD
919 long locked_dirty_space; /* meta/volu count */
920 long io_running_space; /* io_token */
3038a8ca 921 int unused01;
0729c8c8 922 int objid_cache_count;
507df98a 923 int dedup_cache_count;
cdb6e4e6
MD
924 int error; /* critical I/O error */
925 struct krate krate; /* rate limited kprintf */
a8d31329 926 struct krate kdiag; /* rate limited kprintf */
ddfdf542 927 hammer_tid_t asof; /* snapshot mount */
4889cbd4
MD
928 hammer_tid_t next_tid;
929 hammer_tid_t flush_tid1; /* flusher tid sequencing */
930 hammer_tid_t flush_tid2; /* flusher tid sequencing */
a981af19 931 int64_t copy_stat_freebigblocks; /* number of free big-blocks */
02428fb6 932 u_int32_t undo_seqno; /* UNDO/REDO FIFO seqno */
c58123da
MD
933 u_int32_t recover_stage2_seqno; /* REDO recovery seqno */
934 hammer_off_t recover_stage2_offset; /* REDO recovery offset */
e63644f0 935
513ca7d7 936 struct netexport export;
9480ff55 937 struct hammer_lock sync_lock;
c9b9e29d 938 struct hammer_lock free_lock;
d99d6bf5
MD
939 struct hammer_lock undo_lock;
940 struct hammer_lock blkmap_lock;
83f2a3aa 941 struct hammer_lock snapshot_lock;
52e547e3 942 struct hammer_lock volume_lock;
0729c8c8 943 struct hammer_blockmap blockmap[HAMMER_MAX_ZONES];
e8599db1
MD
944 struct hammer_undo undos[HAMMER_MAX_UNDOS];
945 int undo_alloc;
946 TAILQ_HEAD(, hammer_undo) undo_lru_list;
cebe9493 947 TAILQ_HEAD(, hammer_reserve) delay_list;
7a61b85d 948 struct hammer_flush_group_list flush_group_list;
37646115 949 hammer_flush_group_t fill_flush_group;
7b6ccb11 950 hammer_flush_group_t next_flush_group;
0729c8c8 951 TAILQ_HEAD(, hammer_objid_cache) objid_cache_list;
507df98a
ID
952 TAILQ_HEAD(, hammer_dedup_cache) dedup_lru_list;
953 hammer_dedup_cache_t dedup_free_cache;
7bc5b8c2 954 TAILQ_HEAD(, hammer_reclaim) reclaim_list;
eddadaee 955 TAILQ_HEAD(, hammer_io) iorun_list;
e98f1b96 956
b0aab9b9
MD
957 struct lwkt_token fs_token; /* high level */
958 struct lwkt_token io_token; /* low level (IO callback) */
959
e98f1b96 960 struct hammer_inostats inostats[HAMMER_INOSTATS_HSIZE];
78249d7f 961 uint64_t volume_map[4]; /* 256 bits bitfield */
8750964d
MD
962};
963
8cd0a023 964typedef struct hammer_mount *hammer_mount_t;
8750964d 965
cdb6e4e6 966#define HAMMER_MOUNT_CRITICAL_ERROR 0x0001
82010f9f 967#define HAMMER_MOUNT_FLUSH_RECOVERY 0x0002
47f363f1 968#define HAMMER_MOUNT_REDO_SYNC 0x0004
c58123da
MD
969#define HAMMER_MOUNT_REDO_RECOVERY_REQ 0x0008
970#define HAMMER_MOUNT_REDO_RECOVERY_RUN 0x0010
9f5097dc 971
78249d7f
TK
972#define HAMMER_VOLUME_NUMBER_ADD(hmp, vol) \
973 (hmp)->volume_map[(vol)->vol_no >> 6] |= \
974 ((uint64_t)1 << ((vol)->vol_no & ((1 << 6) - 1)))
975
976#define HAMMER_VOLUME_NUMBER_DEL(hmp, vol) \
977 (hmp)->volume_map[(vol)->vol_no >> 6] &= \
978 ~((uint64_t)1 << ((vol)->vol_no & ((1 << 6) - 1)))
979
046789c4
TK
980#define HAMMER_VOLUME_NUMBER_IS_SET(hmp, n) \
981 (((hmp)->volume_map[(n) >> 6] & \
982 ((uint64_t)1 << ((n) & ((1 << 6) - 1)))) != 0)
983
78249d7f
TK
984#define HAMMER_VOLUME_NUMBER_FOREACH(hmp, n) \
985 for (n = 0; n < HAMMER_MAX_VOLUMES; n++) \
046789c4 986 if (HAMMER_VOLUME_NUMBER_IS_SET(hmp, n))
78249d7f 987
fbc6e32a
MD
988struct hammer_sync_info {
989 int error;
990 int waitfor;
991};
992
f6468e9e
MD
993/*
994 * Minium buffer cache bufs required to rebalance the B-Tree.
995 * This is because we must hold the children and the children's children
996 * locked. Even this might not be enough if things are horribly out
997 * of balance.
998 */
999#define HAMMER_REBALANCE_MIN_BUFS \
1000 (HAMMER_BTREE_LEAF_ELMS * HAMMER_BTREE_LEAF_ELMS)
1001
1caa2035 1002#endif /* _KERNEL || _KERNEL_STRUCTURES */
8750964d 1003
23e66b3b 1004#if defined(_KERNEL)
a7e9bef1
MD
1005/*
1006 * checkspace slop (8MB chunks), higher numbers are more conservative.
1007 */
93291532
MD
1008#define HAMMER_CHKSPC_REBLOCK 25
1009#define HAMMER_CHKSPC_MIRROR 20
1010#define HAMMER_CHKSPC_WRITE 20
1011#define HAMMER_CHKSPC_CREATE 20
1012#define HAMMER_CHKSPC_REMOVE 10
7b6ccb11 1013#define HAMMER_CHKSPC_EMERGENCY 0
a7e9bef1 1014
8750964d 1015extern struct vop_ops hammer_vnode_vops;
7a04d74f
MD
1016extern struct vop_ops hammer_spec_vops;
1017extern struct vop_ops hammer_fifo_vops;
66325755 1018extern struct bio_ops hammer_bioops;
427e5fc6 1019
2f85fa4d 1020extern int hammer_debug_io;
d5ef456e 1021extern int hammer_debug_general;
77062c8a 1022extern int hammer_debug_debug;
e8599db1 1023extern int hammer_debug_inode;
7d683b0f 1024extern int hammer_debug_locks;
b3deaf57 1025extern int hammer_debug_btree;
d113fda1 1026extern int hammer_debug_tid;
b33e2cc0 1027extern int hammer_debug_recover;
46fe7ae1 1028extern int hammer_debug_recover_faults;
fc73edd8 1029extern int hammer_debug_critical;
1b0ab2c3 1030extern int hammer_cluster_enable;
507df98a 1031extern int hammer_live_dedup;
e2a02b72 1032extern int hammer_tdmux_ticks;
7a61b85d 1033extern int hammer_count_fsyncs;
b3deaf57 1034extern int hammer_count_inodes;
af209b0f 1035extern int hammer_count_iqueued;
e2a02b72 1036extern int hammer_count_reclaims;
b3deaf57
MD
1037extern int hammer_count_records;
1038extern int hammer_count_record_datas;
1039extern int hammer_count_volumes;
b3deaf57
MD
1040extern int hammer_count_buffers;
1041extern int hammer_count_nodes;
a7e9bef1 1042extern int64_t hammer_count_extra_space_used;
cb51be26
MD
1043extern int64_t hammer_stats_btree_lookups;
1044extern int64_t hammer_stats_btree_searches;
1045extern int64_t hammer_stats_btree_inserts;
1046extern int64_t hammer_stats_btree_deletes;
1047extern int64_t hammer_stats_btree_elements;
1048extern int64_t hammer_stats_btree_splits;
1049extern int64_t hammer_stats_btree_iterations;
39d8fd63 1050extern int64_t hammer_stats_btree_root_iterations;
cb51be26 1051extern int64_t hammer_stats_record_iterations;
ce0138a6
MD
1052extern int64_t hammer_stats_file_read;
1053extern int64_t hammer_stats_file_write;
1054extern int64_t hammer_stats_file_iopsr;
1055extern int64_t hammer_stats_file_iopsw;
1056extern int64_t hammer_stats_disk_read;
1057extern int64_t hammer_stats_disk_write;
1058extern int64_t hammer_stats_inode_flushes;
1059extern int64_t hammer_stats_commits;
89e744ce 1060extern int64_t hammer_stats_undo;
6048b411 1061extern int64_t hammer_stats_redo;
3583bbb4 1062extern long hammer_count_dirtybufspace;
a99b9ea2 1063extern int hammer_count_refedbufs;
0832c9bb 1064extern int hammer_count_reservations;
3583bbb4
MD
1065extern long hammer_count_io_running_read;
1066extern long hammer_count_io_running_write;
a99b9ea2 1067extern int hammer_count_io_locked;
3583bbb4 1068extern long hammer_limit_dirtybufspace;
47637bff 1069extern int hammer_limit_recs;
de996e86 1070extern int hammer_limit_inode_recs;
e2a02b72 1071extern int hammer_limit_reclaims;
e2ef7a95 1072extern int hammer_live_dedup_cache_size;
9192654c 1073extern int hammer_limit_redo;
1f07f686 1074extern int hammer_bio_count;
cb51be26 1075extern int hammer_verify_zone;
1b0ab2c3 1076extern int hammer_verify_data;
cb51be26 1077extern int hammer_write_mode;
b4f86ea3 1078extern int hammer_double_buffer;
283a4a38 1079extern int hammer_btree_full_undo;
3e583440 1080extern int hammer_yield_check;
6f3d87c0 1081extern int hammer_fsync_mode;
21fde338 1082extern int hammer_autoflush;
7d683b0f 1083extern int64_t hammer_contention_count;
b3deaf57 1084
507df98a
ID
1085extern int64_t hammer_live_dedup_vnode_bcmps;
1086extern int64_t hammer_live_dedup_device_bcmps;
1087extern int64_t hammer_live_dedup_findblk_failures;
1088extern int64_t hammer_live_dedup_bmap_saves;
1089
cdb6e4e6
MD
1090void hammer_critical_error(hammer_mount_t hmp, hammer_inode_t ip,
1091 int error, const char *msg);
8750964d
MD
1092int hammer_vop_inactive(struct vop_inactive_args *);
1093int hammer_vop_reclaim(struct vop_reclaim_args *);
e8599db1 1094int hammer_get_vnode(struct hammer_inode *ip, struct vnode **vpp);
36f82b23 1095struct hammer_inode *hammer_get_inode(hammer_transaction_t trans,
adf01747 1096 hammer_inode_t dip, int64_t obj_id,
ddfdf542
MD
1097 hammer_tid_t asof, u_int32_t localization,
1098 int flags, int *errorp);
4c286c36
MD
1099struct hammer_inode *hammer_get_dummy_inode(hammer_transaction_t trans,
1100 hammer_inode_t dip, int64_t obj_id,
1101 hammer_tid_t asof, u_int32_t localization,
1102 int flags, int *errorp);
39d8fd63
MD
1103struct hammer_inode *hammer_find_inode(hammer_transaction_t trans,
1104 int64_t obj_id, hammer_tid_t asof,
1105 u_int32_t localization);
43c665ae
MD
1106void hammer_scan_inode_snapshots(hammer_mount_t hmp,
1107 hammer_inode_info_t iinfo,
1108 int (*callback)(hammer_inode_t ip, void *data),
1109 void *data);
66325755
MD
1110void hammer_put_inode(struct hammer_inode *ip);
1111void hammer_put_inode_ref(struct hammer_inode *ip);
e98f1b96 1112void hammer_inode_waitreclaims(hammer_transaction_t trans);
0d60b0ab 1113void hammer_inode_dirty(struct hammer_inode *ip);
66325755 1114
deabdbfb 1115int hammer_unload_volume(hammer_volume_t volume, void *data);
51c35492
MD
1116int hammer_adjust_volume_mode(hammer_volume_t volume, void *data __unused);
1117
07be83b8 1118int hammer_unload_buffer(hammer_buffer_t buffer, void *data);
7c19b529 1119int hammer_install_volume(hammer_mount_t hmp, const char *volname,
deabdbfb 1120 struct vnode *devvp, void *data);
1b0ab2c3 1121int hammer_mountcheck_volumes(hammer_mount_t hmp);
c302e844 1122int hammer_get_installed_volumes(hammer_mount_t hmp);
8cd0a023 1123
e469566b 1124int hammer_mem_add(hammer_record_t record);
45a014dc 1125int hammer_ip_lookup(hammer_cursor_t cursor);
4e17f465 1126int hammer_ip_first(hammer_cursor_t cursor);
a89aec1b 1127int hammer_ip_next(hammer_cursor_t cursor);
8cd0a023 1128int hammer_ip_resolve_data(hammer_cursor_t cursor);
e63644f0
MD
1129int hammer_ip_delete_record(hammer_cursor_t cursor, hammer_inode_t ip,
1130 hammer_tid_t tid);
83f2a3aa
MD
1131int hammer_create_at_cursor(hammer_cursor_t cursor,
1132 hammer_btree_leaf_elm_t leaf, void *udata, int mode);
602c6cb8 1133int hammer_delete_at_cursor(hammer_cursor_t cursor, int delete_flags,
842e7a70
MD
1134 hammer_tid_t delete_tid, u_int32_t delete_ts,
1135 int track, int64_t *stat_bytes);
b3deaf57 1136int hammer_ip_check_directory_empty(hammer_transaction_t trans,
98f7132d 1137 hammer_inode_t ip);
fbc6e32a 1138int hammer_sync_hmp(hammer_mount_t hmp, int waitfor);
f36a9737
MD
1139int hammer_queue_inodes_flusher(hammer_mount_t hmp, int waitfor);
1140
8cd0a023 1141hammer_record_t
11ad5ade 1142 hammer_alloc_mem_record(hammer_inode_t ip, int data_len);
d36ec43b 1143void hammer_flush_record_done(hammer_record_t record, int error);
af209b0f 1144void hammer_wait_mem_record_ident(hammer_record_t record, const char *ident);
b3deaf57 1145void hammer_rel_mem_record(hammer_record_t record);
b84de5af 1146
6a37e7e4 1147int hammer_cursor_up(hammer_cursor_t cursor);
f36a9737 1148int hammer_cursor_up_locked(hammer_cursor_t cursor);
8cd0a023 1149int hammer_cursor_down(hammer_cursor_t cursor);
6a37e7e4 1150int hammer_cursor_upgrade(hammer_cursor_t cursor);
7bc5b8c2 1151int hammer_cursor_upgrade_node(hammer_cursor_t cursor);
6a37e7e4 1152void hammer_cursor_downgrade(hammer_cursor_t cursor);
bb29b5d8
MD
1153int hammer_cursor_upgrade2(hammer_cursor_t c1, hammer_cursor_t c2);
1154void hammer_cursor_downgrade2(hammer_cursor_t c1, hammer_cursor_t c2);
32c90105
MD
1155int hammer_cursor_seek(hammer_cursor_t cursor, hammer_node_t node,
1156 int index);
af209b0f 1157void hammer_lock_ex_ident(struct hammer_lock *lock, const char *ident);
8cd0a023
MD
1158int hammer_lock_ex_try(struct hammer_lock *lock);
1159void hammer_lock_sh(struct hammer_lock *lock);
47637bff 1160int hammer_lock_sh_try(struct hammer_lock *lock);
bb29b5d8
MD
1161int hammer_lock_upgrade(struct hammer_lock *lock, int shcount);
1162void hammer_lock_downgrade(struct hammer_lock *lock, int shcount);
b3bad96f 1163int hammer_lock_status(struct hammer_lock *lock);
427e5fc6 1164void hammer_unlock(struct hammer_lock *lock);
66325755 1165void hammer_ref(struct hammer_lock *lock);
250aec18
MD
1166int hammer_ref_interlock(struct hammer_lock *lock);
1167int hammer_ref_interlock_true(struct hammer_lock *lock);
1168void hammer_ref_interlock_done(struct hammer_lock *lock);
1169void hammer_rel(struct hammer_lock *lock);
1170int hammer_rel_interlock(struct hammer_lock *lock, int locked);
1171void hammer_rel_interlock_done(struct hammer_lock *lock, int orig_locked);
1172int hammer_get_interlock(struct hammer_lock *lock);
1173int hammer_try_interlock_norefs(struct hammer_lock *lock);
1174void hammer_put_interlock(struct hammer_lock *lock, int error);
8cd0a023 1175
2f85fa4d
MD
1176void hammer_sync_lock_ex(hammer_transaction_t trans);
1177void hammer_sync_lock_sh(hammer_transaction_t trans);
47637bff 1178int hammer_sync_lock_sh_try(hammer_transaction_t trans);
2f85fa4d
MD
1179void hammer_sync_unlock(hammer_transaction_t trans);
1180
66325755 1181u_int32_t hammer_to_unix_xid(uuid_t *uuid);
8cd0a023 1182void hammer_guid_to_uuid(uuid_t *uuid, u_int32_t guid);
ddfdf542
MD
1183void hammer_time_to_timespec(u_int64_t xtime, struct timespec *ts);
1184u_int64_t hammer_timespec_to_time(struct timespec *ts);
bc6c1f13
MD
1185int hammer_str_to_tid(const char *str, int *ispfsp,
1186 hammer_tid_t *tidp, u_int32_t *localizationp);
1187int hammer_is_atatext(const char *name, int len);
5a64efa1
MD
1188hammer_tid_t hammer_alloc_objid(hammer_mount_t hmp, hammer_inode_t dip,
1189 int64_t namekey);
0729c8c8
MD
1190void hammer_clear_objid(hammer_inode_t dip);
1191void hammer_destroy_objid_cache(hammer_mount_t hmp);
8cd0a023 1192
507df98a
ID
1193int hammer_dedup_crc_rb_compare(hammer_dedup_cache_t dc1,
1194 hammer_dedup_cache_t dc2);
1195int hammer_dedup_off_rb_compare(hammer_dedup_cache_t dc1,
1196 hammer_dedup_cache_t dc2);
1197hammer_dedup_cache_t hammer_dedup_cache_add(hammer_inode_t ip,
1198 hammer_btree_leaf_elm_t leaf);
1199hammer_dedup_cache_t hammer_dedup_cache_lookup(hammer_mount_t hmp,
1200 hammer_crc_t crc);
1201void hammer_dedup_cache_inval(hammer_mount_t hmp, hammer_off_t base_offset);
1202void hammer_destroy_dedup_cache(hammer_mount_t hmp);
1203void hammer_dump_dedup_cache(hammer_mount_t hmp);
1204int hammer_dedup_validate(hammer_dedup_cache_t dcp, int zone, int bytes,
1205 void *data);
1206
e8599db1 1207int hammer_enter_undo_history(hammer_mount_t hmp, hammer_off_t offset,
b3bad96f 1208 int bytes);
e8599db1 1209void hammer_clear_undo_history(hammer_mount_t hmp);
66325755 1210enum vtype hammer_get_vnode_type(u_int8_t obj_type);
6b4f890b 1211int hammer_get_dtype(u_int8_t obj_type);
66325755 1212u_int8_t hammer_get_obj_type(enum vtype vtype);
5e435c92
MD
1213int64_t hammer_directory_namekey(hammer_inode_t dip, const void *name, int len,
1214 u_int32_t *max_iterationsp);
e63644f0 1215int hammer_nohistory(hammer_inode_t ip);
427e5fc6 1216
36f82b23 1217int hammer_init_cursor(hammer_transaction_t trans, hammer_cursor_t cursor,
b3bad96f 1218 hammer_node_cache_t cache, hammer_inode_t ip);
4e17f465 1219void hammer_normalize_cursor(hammer_cursor_t cursor);
8cd0a023 1220void hammer_done_cursor(hammer_cursor_t cursor);
b3bad96f 1221int hammer_recover_cursor(hammer_cursor_t cursor);
982be4bf
MD
1222void hammer_unlock_cursor(hammer_cursor_t cursor);
1223int hammer_lock_cursor(hammer_cursor_t cursor);
3f43fb33
MD
1224hammer_cursor_t hammer_push_cursor(hammer_cursor_t ocursor);
1225void hammer_pop_cursor(hammer_cursor_t ocursor, hammer_cursor_t ncursor);
b3bad96f
MD
1226
1227void hammer_cursor_replaced_node(hammer_node_t onode, hammer_node_t nnode);
1228void hammer_cursor_removed_node(hammer_node_t onode, hammer_node_t parent,
1229 int index);
1230void hammer_cursor_split_node(hammer_node_t onode, hammer_node_t nnode,
1231 int index);
bbb01e14
MD
1232void hammer_cursor_moved_element(hammer_node_t oparent, int pindex,
1233 hammer_node_t onode, int oindex,
1234 hammer_node_t nnode, int nindex);
1775b6a0
MD
1235void hammer_cursor_parent_changed(hammer_node_t node, hammer_node_t oparent,
1236 hammer_node_t nparent, int nindex);
b3bad96f
MD
1237void hammer_cursor_inserted_element(hammer_node_t node, int index);
1238void hammer_cursor_deleted_element(hammer_node_t node, int index);
b9107f58 1239void hammer_cursor_invalidate_cache(hammer_cursor_t cursor);
8cd0a023
MD
1240
1241int hammer_btree_lookup(hammer_cursor_t cursor);
d26d0ae9 1242int hammer_btree_first(hammer_cursor_t cursor);
32c90105 1243int hammer_btree_last(hammer_cursor_t cursor);
8cd0a023
MD
1244int hammer_btree_extract(hammer_cursor_t cursor, int flags);
1245int hammer_btree_iterate(hammer_cursor_t cursor);
32c90105 1246int hammer_btree_iterate_reverse(hammer_cursor_t cursor);
11ad5ade 1247int hammer_btree_insert(hammer_cursor_t cursor,
602c6cb8 1248 hammer_btree_leaf_elm_t elm, int *doprop);
03b6feea 1249int hammer_btree_delete(hammer_cursor_t cursor, int *ndelete);
4c038e17
MD
1250void hammer_btree_do_propagation(hammer_cursor_t cursor,
1251 hammer_pseudofs_inmem_t pfsm,
602c6cb8 1252 hammer_btree_leaf_elm_t leaf);
8cd0a023 1253int hammer_btree_cmp(hammer_base_elm_t key1, hammer_base_elm_t key2);
d26d0ae9 1254int hammer_btree_chkts(hammer_tid_t ts, hammer_base_elm_t key);
32c90105
MD
1255int hammer_btree_correct_rhb(hammer_cursor_t cursor, hammer_tid_t tid);
1256int hammer_btree_correct_lhb(hammer_cursor_t cursor, hammer_tid_t tid);
1257
2f85fa4d
MD
1258int btree_set_parent(hammer_transaction_t trans, hammer_node_t node,
1259 hammer_btree_elm_t elm);
1775b6a0 1260void hammer_node_lock_init(hammer_node_lock_t parent, hammer_node_t node);
24cf83d2
MD
1261void hammer_btree_lcache_init(hammer_mount_t hmp, hammer_node_lock_t lcache,
1262 int depth);
1263void hammer_btree_lcache_free(hammer_mount_t hmp, hammer_node_lock_t lcache);
1775b6a0 1264int hammer_btree_lock_children(hammer_cursor_t cursor, int depth,
24cf83d2
MD
1265 hammer_node_lock_t parent,
1266 hammer_node_lock_t lcache);
1775b6a0
MD
1267void hammer_btree_lock_copy(hammer_cursor_t cursor,
1268 hammer_node_lock_t parent);
7ddc70d1 1269int hammer_btree_sync_copy(hammer_cursor_t cursor,
1775b6a0 1270 hammer_node_lock_t parent);
24cf83d2
MD
1271void hammer_btree_unlock_children(hammer_mount_t hmp,
1272 hammer_node_lock_t parent,
1273 hammer_node_lock_t lcache);
bcac4bbb 1274int hammer_btree_search_node(hammer_base_elm_t elm, hammer_node_ondisk_t node);
82010f9f
MD
1275hammer_node_t hammer_btree_get_parent(hammer_transaction_t trans,
1276 hammer_node_t node, int *parent_indexp,
c82af904 1277 int *errorp, int try_exclusive);
b33e2cc0 1278
c0ade690
MD
1279void hammer_print_btree_node(hammer_node_ondisk_t ondisk);
1280void hammer_print_btree_elm(hammer_btree_elm_t elm, u_int8_t type, int i);
8750964d 1281
4af0f405 1282void *hammer_bread(hammer_mount_t hmp, hammer_off_t off,
47197d71 1283 int *errorp, struct hammer_buffer **bufferp);
4af0f405 1284void *hammer_bnew(hammer_mount_t hmp, hammer_off_t off,
47197d71 1285 int *errorp, struct hammer_buffer **bufferp);
4af0f405 1286void *hammer_bread_ext(hammer_mount_t hmp, hammer_off_t off, int bytes,
4a2796f3 1287 int *errorp, struct hammer_buffer **bufferp);
4af0f405 1288void *hammer_bnew_ext(hammer_mount_t hmp, hammer_off_t off, int bytes,
4a2796f3 1289 int *errorp, struct hammer_buffer **bufferp);
8cd0a023
MD
1290
1291hammer_volume_t hammer_get_root_volume(hammer_mount_t hmp, int *errorp);
427e5fc6 1292
8cd0a023 1293hammer_volume_t hammer_get_volume(hammer_mount_t hmp,
427e5fc6 1294 int32_t vol_no, int *errorp);
4a2796f3
MD
1295hammer_buffer_t hammer_get_buffer(hammer_mount_t hmp, hammer_off_t buf_offset,
1296 int bytes, int isnew, int *errorp);
1b0ab2c3
MD
1297void hammer_sync_buffers(hammer_mount_t hmp,
1298 hammer_off_t base_offset, int bytes);
362ec2dc 1299int hammer_del_buffers(hammer_mount_t hmp,
1b0ab2c3 1300 hammer_off_t base_offset,
362ec2dc
MD
1301 hammer_off_t zone2_offset, int bytes,
1302 int report_conflicts);
8cd0a023 1303
fbc6e32a 1304int hammer_ref_volume(hammer_volume_t volume);
8cd0a023
MD
1305int hammer_ref_buffer(hammer_buffer_t buffer);
1306void hammer_flush_buffer_nodes(hammer_buffer_t buffer);
1307
250aec18
MD
1308void hammer_rel_volume(hammer_volume_t volume, int locked);
1309void hammer_rel_buffer(hammer_buffer_t buffer, int locked);
8cd0a023 1310
513ca7d7
MD
1311int hammer_vfs_export(struct mount *mp, int op,
1312 const struct export_args *export);
82010f9f
MD
1313hammer_node_t hammer_get_node(hammer_transaction_t trans,
1314 hammer_off_t node_offset, int isnew, int *errorp);
740d8317 1315void hammer_ref_node(hammer_node_t node);
4c286c36 1316hammer_node_t hammer_ref_node_safe(hammer_transaction_t trans,
bcac4bbb 1317 hammer_node_cache_t cache, int *errorp);
8cd0a023 1318void hammer_rel_node(hammer_node_t node);
36f82b23
MD
1319void hammer_delete_node(hammer_transaction_t trans,
1320 hammer_node_t node);
bcac4bbb
MD
1321void hammer_cache_node(hammer_node_cache_t cache,
1322 hammer_node_t node);
1323void hammer_uncache_node(hammer_node_cache_t cache);
250aec18 1324void hammer_flush_node(hammer_node_t node, int locked);
8cd0a023 1325
427e5fc6
MD
1326void hammer_dup_buffer(struct hammer_buffer **bufferp,
1327 struct hammer_buffer *buffer);
df2ccbac
MD
1328hammer_node_t hammer_alloc_btree(hammer_transaction_t trans,
1329 hammer_off_t hint, int *errorp);
36f82b23 1330void *hammer_alloc_data(hammer_transaction_t trans, int32_t data_len,
bf3b416b 1331 u_int16_t rec_type, hammer_off_t *data_offsetp,
df2ccbac
MD
1332 struct hammer_buffer **data_bufferp,
1333 hammer_off_t hint, int *errorp);
bf686dbe 1334
02428fb6 1335int hammer_generate_undo(hammer_transaction_t trans,
2426f861 1336 hammer_off_t zone_offset, void *base, int len);
6048b411 1337int hammer_generate_redo(hammer_transaction_t trans, hammer_inode_t ip,
47f363f1
MD
1338 hammer_off_t file_offset, u_int32_t flags,
1339 void *base, int len);
1340void hammer_generate_redo_sync(hammer_transaction_t trans);
73896937
MD
1341void hammer_redo_fifo_start_flush(hammer_inode_t ip);
1342void hammer_redo_fifo_end_flush(hammer_inode_t ip);
1343
47f363f1 1344void hammer_format_undo(void *base, u_int32_t seqno);
02428fb6 1345int hammer_upgrade_undo_4(hammer_transaction_t trans);
427e5fc6 1346
66325755 1347void hammer_put_volume(struct hammer_volume *volume, int flush);
66325755 1348void hammer_put_buffer(struct hammer_buffer *buffer, int flush);
427e5fc6 1349
36f82b23
MD
1350hammer_off_t hammer_freemap_alloc(hammer_transaction_t trans,
1351 hammer_off_t owner, int *errorp);
1352void hammer_freemap_free(hammer_transaction_t trans, hammer_off_t phys_offset,
c3be93f2 1353 hammer_off_t owner, int *errorp);
0f65be10 1354int _hammer_checkspace(hammer_mount_t hmp, int slop, int64_t *resp);
36f82b23 1355hammer_off_t hammer_blockmap_alloc(hammer_transaction_t trans, int zone,
df2ccbac 1356 int bytes, hammer_off_t hint, int *errorp);
0832c9bb
MD
1357hammer_reserve_t hammer_blockmap_reserve(hammer_mount_t hmp, int zone,
1358 int bytes, hammer_off_t *zone_offp, int *errorp);
507df98a
ID
1359hammer_reserve_t hammer_blockmap_reserve_dedup(hammer_mount_t hmp, int zone,
1360 int bytes, hammer_off_t zone_offset, int *errorp);
0832c9bb
MD
1361void hammer_blockmap_reserve_complete(hammer_mount_t hmp,
1362 hammer_reserve_t resv);
cb51be26 1363void hammer_reserve_clrdelay(hammer_mount_t hmp, hammer_reserve_t resv);
36f82b23 1364void hammer_blockmap_free(hammer_transaction_t trans,
ae9ef1bf 1365 hammer_off_t zone_offset, int bytes);
bb29b5d8 1366int hammer_blockmap_dedup(hammer_transaction_t trans,
ae9ef1bf 1367 hammer_off_t zone_offset, int bytes);
cdb6e4e6 1368int hammer_blockmap_finalize(hammer_transaction_t trans,
5e435c92 1369 hammer_reserve_t resv,
ae9ef1bf
MD
1370 hammer_off_t zone_offset, int bytes);
1371int hammer_blockmap_getfree(hammer_mount_t hmp, hammer_off_t zone_offset,
bf686dbe 1372 int *curp, int *errorp);
f4fe61c2
MD
1373hammer_off_t hammer_blockmap_lookup_verify(hammer_mount_t hmp,
1374 hammer_off_t zone_offset, int *errorp);
1375
ae9ef1bf 1376hammer_off_t hammer_undo_lookup(hammer_mount_t hmp, hammer_off_t zone_offset,
bf686dbe 1377 int *errorp);
06ad81ff
MD
1378int64_t hammer_undo_used(hammer_transaction_t trans);
1379int64_t hammer_undo_space(hammer_transaction_t trans);
1f07f686 1380int64_t hammer_undo_max(hammer_mount_t hmp);
710733a6 1381int hammer_undo_reclaim(hammer_io_t io);
1f07f686 1382
8cd0a023
MD
1383void hammer_start_transaction(struct hammer_transaction *trans,
1384 struct hammer_mount *hmp);
36f82b23
MD
1385void hammer_simple_transaction(struct hammer_transaction *trans,
1386 struct hammer_mount *hmp);
b84de5af
MD
1387void hammer_start_transaction_fls(struct hammer_transaction *trans,
1388 struct hammer_mount *hmp);
1389void hammer_done_transaction(struct hammer_transaction *trans);
83f2a3aa 1390hammer_tid_t hammer_alloc_tid(hammer_mount_t hmp, int count);
66325755 1391
e98f1b96 1392void hammer_modify_inode(hammer_transaction_t trans, hammer_inode_t ip, int flags);
f90dde4c 1393void hammer_flush_inode(hammer_inode_t ip, int flags);
cdb6e4e6 1394void hammer_flush_inode_done(hammer_inode_t ip, int error);
b84de5af
MD
1395void hammer_wait_inode(hammer_inode_t ip);
1396
8cd0a023
MD
1397int hammer_create_inode(struct hammer_transaction *trans, struct vattr *vap,
1398 struct ucred *cred, struct hammer_inode *dip,
5a64efa1 1399 const char *name, int namelen,
ea434b6f
MD
1400 hammer_pseudofs_inmem_t pfsm,
1401 struct hammer_inode **ipp);
a89aec1b 1402void hammer_rel_inode(hammer_inode_t ip, int flush);
51c35492 1403int hammer_reload_inode(hammer_inode_t ip, void *arg __unused);
af209b0f 1404int hammer_ino_rb_compare(hammer_inode_t ip1, hammer_inode_t ip2);
73896937 1405int hammer_redo_rb_compare(hammer_inode_t ip1, hammer_inode_t ip2);
cdb6e4e6 1406int hammer_destroy_inode_callback(hammer_inode_t ip, void *data __unused);
51c35492 1407
02325004 1408int hammer_sync_inode(hammer_transaction_t trans, hammer_inode_t ip);
7b6ccb11 1409void hammer_test_inode(hammer_inode_t dip);
e8599db1 1410void hammer_inode_unloadable_check(hammer_inode_t ip, int getvp);
70125e78 1411int hammer_update_atime_quick(hammer_inode_t ip);
8cd0a023 1412
a89aec1b 1413int hammer_ip_add_directory(struct hammer_transaction *trans,
5a930e66 1414 hammer_inode_t dip, const char *name, int bytes,
8cd0a023 1415 hammer_inode_t nip);
a89aec1b 1416int hammer_ip_del_directory(struct hammer_transaction *trans,
8cd0a023
MD
1417 hammer_cursor_t cursor, hammer_inode_t dip,
1418 hammer_inode_t ip);
6362a262 1419void hammer_ip_replace_bulk(hammer_mount_t hmp, hammer_record_t record);
47637bff 1420hammer_record_t hammer_ip_add_bulk(hammer_inode_t ip, off_t file_offset,
0832c9bb 1421 void *data, int bytes, int *errorp);
47637bff 1422int hammer_ip_frontend_trunc(struct hammer_inode *ip, off_t file_size);
7a04d74f
MD
1423int hammer_ip_add_record(struct hammer_transaction *trans,
1424 hammer_record_t record);
4e17f465 1425int hammer_ip_delete_range(hammer_cursor_t cursor, hammer_inode_t ip,
47637bff 1426 int64_t ran_beg, int64_t ran_end, int truncating);
a9d52b76 1427int hammer_ip_delete_clean(hammer_cursor_t cursor, hammer_inode_t ip,
4e17f465
MD
1428 int *countp);
1429int hammer_ip_sync_data(hammer_cursor_t cursor, hammer_inode_t ip,
1430 int64_t offset, void *data, int bytes);
36f82b23 1431int hammer_ip_sync_record(hammer_transaction_t trans, hammer_record_t rec);
4e17f465 1432int hammer_ip_sync_record_cursor(hammer_cursor_t cursor, hammer_record_t rec);
ea434b6f
MD
1433hammer_pseudofs_inmem_t hammer_load_pseudofs(hammer_transaction_t trans,
1434 u_int32_t localization, int *errorp);
1435int hammer_mkroot_pseudofs(hammer_transaction_t trans, struct ucred *cred,
1436 hammer_pseudofs_inmem_t pfsm);
1437int hammer_save_pseudofs(hammer_transaction_t trans,
1438 hammer_pseudofs_inmem_t pfsm);
842e7a70 1439int hammer_unload_pseudofs(hammer_transaction_t trans, u_int32_t localization);
5fa5c92f 1440void hammer_rel_pseudofs(hammer_mount_t hmp, hammer_pseudofs_inmem_t pfsm);
7dc57964
MD
1441int hammer_ioctl(hammer_inode_t ip, u_long com, caddr_t data, int fflag,
1442 struct ucred *cred);
1443
748efb59 1444void hammer_io_init(hammer_io_t io, hammer_volume_t volume,
10a5d1ba 1445 enum hammer_io_type type);
b7de8aa5 1446int hammer_io_read(struct vnode *devvp, struct hammer_io *io, int limit);
0e8bd897 1447void hammer_io_advance(struct hammer_io *io);
66325755 1448int hammer_io_new(struct vnode *devvp, struct hammer_io *io);
362ec2dc 1449int hammer_io_inval(hammer_volume_t volume, hammer_off_t zone2_offset);
ecca949a 1450struct buf *hammer_io_release(struct hammer_io *io, int flush);
710733a6 1451void hammer_io_flush(struct hammer_io *io, int reclaim);
1b0ab2c3 1452void hammer_io_wait(struct hammer_io *io);
055f5ff8 1453void hammer_io_waitdep(struct hammer_io *io);
eddadaee 1454void hammer_io_wait_all(hammer_mount_t hmp, const char *ident, int doflush);
1b0ab2c3
MD
1455int hammer_io_direct_read(hammer_mount_t hmp, struct bio *bio,
1456 hammer_btree_leaf_elm_t leaf);
9a98f3cc
MD
1457int hammer_io_indirect_read(hammer_mount_t hmp, struct bio *bio,
1458 hammer_btree_leaf_elm_t leaf);
6362a262
MD
1459int hammer_io_direct_write(hammer_mount_t hmp, struct bio *bio,
1460 hammer_record_t record);
1b0ab2c3 1461void hammer_io_direct_wait(hammer_record_t record);
43c665ae 1462void hammer_io_direct_uncache(hammer_mount_t hmp, hammer_btree_leaf_elm_t leaf);
9f5097dc
MD
1463void hammer_io_write_interlock(hammer_io_t io);
1464void hammer_io_done_interlock(hammer_io_t io);
4a2796f3 1465void hammer_io_clear_modify(struct hammer_io *io, int inval);
cebe9493 1466void hammer_io_clear_modlist(struct hammer_io *io);
748efb59 1467void hammer_io_flush_sync(hammer_mount_t hmp);
2faf0737 1468void hammer_io_clear_error(struct hammer_io *io);
77912481 1469void hammer_io_clear_error_noassert(struct hammer_io *io);
b8a41159 1470void hammer_io_notmeta(hammer_buffer_t buffer);
ba298df1 1471void hammer_io_limit_backlog(hammer_mount_t hmp);
748efb59 1472
36f82b23
MD
1473void hammer_modify_volume(hammer_transaction_t trans, hammer_volume_t volume,
1474 void *base, int len);
1475void hammer_modify_buffer(hammer_transaction_t trans, hammer_buffer_t buffer,
1476 void *base, int len);
10a5d1ba
MD
1477void hammer_modify_volume_done(hammer_volume_t volume);
1478void hammer_modify_buffer_done(hammer_buffer_t buffer);
0b075555 1479
36f82b23
MD
1480int hammer_ioc_reblock(hammer_transaction_t trans, hammer_inode_t ip,
1481 struct hammer_ioc_reblock *reblock);
1775b6a0
MD
1482int hammer_ioc_rebalance(hammer_transaction_t trans, hammer_inode_t ip,
1483 struct hammer_ioc_rebalance *rebal);
11ad5ade
MD
1484int hammer_ioc_prune(hammer_transaction_t trans, hammer_inode_t ip,
1485 struct hammer_ioc_prune *prune);
c82af904
MD
1486int hammer_ioc_mirror_read(hammer_transaction_t trans, hammer_inode_t ip,
1487 struct hammer_ioc_mirror_rw *mirror);
1488int hammer_ioc_mirror_write(hammer_transaction_t trans, hammer_inode_t ip,
1489 struct hammer_ioc_mirror_rw *mirror);
5fa5c92f 1490int hammer_ioc_set_pseudofs(hammer_transaction_t trans, hammer_inode_t ip,
ea434b6f 1491 struct ucred *cred, struct hammer_ioc_pseudofs_rw *pfs);
5fa5c92f
MD
1492int hammer_ioc_get_pseudofs(hammer_transaction_t trans, hammer_inode_t ip,
1493 struct hammer_ioc_pseudofs_rw *pfs);
842e7a70
MD
1494int hammer_ioc_destroy_pseudofs(hammer_transaction_t trans, hammer_inode_t ip,
1495 struct hammer_ioc_pseudofs_rw *pfs);
1496int hammer_ioc_downgrade_pseudofs(hammer_transaction_t trans, hammer_inode_t ip,
1497 struct hammer_ioc_pseudofs_rw *pfs);
1498int hammer_ioc_upgrade_pseudofs(hammer_transaction_t trans, hammer_inode_t ip,
1499 struct hammer_ioc_pseudofs_rw *pfs);
4889cbd4
MD
1500int hammer_ioc_wait_pseudofs(hammer_transaction_t trans, hammer_inode_t ip,
1501 struct hammer_ioc_pseudofs_rw *pfs);
898f9540
TK
1502int hammer_ioc_iterate_pseudofs(hammer_transaction_t trans, hammer_inode_t ip,
1503 struct hammer_ioc_pfs_iterate *pi);
d121f61c 1504int hammer_ioc_volume_add(hammer_transaction_t trans, hammer_inode_t ip,
865c9609
MN
1505 struct hammer_ioc_volume *ioc);
1506int hammer_ioc_volume_del(hammer_transaction_t trans, hammer_inode_t ip,
1507 struct hammer_ioc_volume *ioc);
e914c91d
SK
1508int hammer_ioc_volume_list(hammer_transaction_t trans, hammer_inode_t ip,
1509 struct hammer_ioc_volume_list *ioc);
bb29b5d8
MD
1510int hammer_ioc_dedup(hammer_transaction_t trans, hammer_inode_t ip,
1511 struct hammer_ioc_dedup *dedup);
bf686dbe 1512
855942b6 1513int hammer_signal_check(hammer_mount_t hmp);
bf686dbe 1514
059819e3
MD
1515void hammer_flusher_create(hammer_mount_t hmp);
1516void hammer_flusher_destroy(hammer_mount_t hmp);
1517void hammer_flusher_sync(hammer_mount_t hmp);
7a61b85d 1518int hammer_flusher_async(hammer_mount_t hmp, hammer_flush_group_t flg);
15e75dab 1519int hammer_flusher_async_one(hammer_mount_t hmp);
e2a02b72 1520int hammer_flusher_running(hammer_mount_t hmp);
93291532 1521void hammer_flusher_wait(hammer_mount_t hmp, int seq);
82010f9f 1522void hammer_flusher_wait_next(hammer_mount_t hmp);
06ad81ff 1523int hammer_flusher_meta_limit(hammer_mount_t hmp);
93291532 1524int hammer_flusher_meta_halflimit(hammer_mount_t hmp);
06ad81ff 1525int hammer_flusher_undo_exhausted(hammer_transaction_t trans, int quarter);
525aad3a 1526void hammer_flusher_clean_loose_ios(hammer_mount_t hmp);
6c1f89f4 1527void hammer_flusher_finalize(hammer_transaction_t trans, int final);
1b0ab2c3 1528int hammer_flusher_haswork(hammer_mount_t hmp);
8bae937e 1529int hammer_flush_dirty(hammer_mount_t hmp, int max_count);
6048b411 1530void hammer_flusher_flush_undos(hammer_mount_t hmp, int already_flushed);
059819e3 1531
02428fb6
MD
1532int hammer_recover_stage1(hammer_mount_t hmp, hammer_volume_t rootvol);
1533int hammer_recover_stage2(hammer_mount_t hmp, hammer_volume_t rootvol);
51c35492 1534void hammer_recover_flush_buffers(hammer_mount_t hmp,
06ad81ff 1535 hammer_volume_t root_volume, int final);
f90dde4c 1536
19619882
MD
1537void hammer_crc_set_blockmap(hammer_blockmap_t blockmap);
1538void hammer_crc_set_volume(hammer_volume_ondisk_t ondisk);
ddfdf542 1539void hammer_crc_set_leaf(void *data, hammer_btree_leaf_elm_t leaf);
19619882
MD
1540
1541int hammer_crc_test_blockmap(hammer_blockmap_t blockmap);
1542int hammer_crc_test_volume(hammer_volume_ondisk_t ondisk);
19619882 1543int hammer_crc_test_btree(hammer_node_ondisk_t ondisk);
ddfdf542 1544int hammer_crc_test_leaf(void *data, hammer_btree_leaf_elm_t leaf);
c6b20585 1545void hkprintf(const char *ctl, ...) __printflike(1, 2);
a56cb012
MD
1546udev_t hammer_fsid_to_udev(uuid_t *uuid);
1547
19619882 1548
4a2796f3 1549int hammer_blocksize(int64_t file_offset);
6362a262 1550int hammer_blockoff(int64_t file_offset);
4a2796f3
MD
1551int64_t hammer_blockdemarc(int64_t file_offset1, int64_t file_offset2);
1552
0f65be10
MD
1553/*
1554 * Shortcut for _hammer_checkspace(), used all over the code.
1555 */
1556static __inline int
1557hammer_checkspace(hammer_mount_t hmp, int slop)
1558{
1559 return(_hammer_checkspace(hmp, slop, NULL));
1560}
1561
af209b0f
MD
1562static __inline void
1563hammer_wait_mem_record(hammer_record_t record)
1564{
1565 hammer_wait_mem_record_ident(record, "hmmwai");
1566}
1567
1568static __inline void
1569hammer_lock_ex(struct hammer_lock *lock)
1570{
1571 hammer_lock_ex_ident(lock, "hmrlck");
1572}
1573
f1c0ae53
TK
1574static __inline void
1575hammer_modify_volume_noundo(hammer_transaction_t trans, hammer_volume_t volume)
1576{
1577 hammer_modify_volume(trans, volume, NULL, 0);
1578}
1579
1580static __inline void
1581hammer_modify_buffer_noundo(hammer_transaction_t trans, hammer_buffer_t buffer)
1582{
1583 hammer_modify_buffer(trans, buffer, NULL, 0);
1584}
1585
bcac4bbb
MD
1586/*
1587 * Indicate that a B-Tree node is being modified.
1588 */
8cd0a023 1589static __inline void
36f82b23 1590hammer_modify_node_noundo(hammer_transaction_t trans, hammer_node_t node)
bf686dbe 1591{
4c286c36 1592 KKASSERT((node->flags & HAMMER_NODE_CRCBAD) == 0);
36f82b23 1593 hammer_modify_buffer(trans, node->buffer, NULL, 0);
bf686dbe
MD
1594}
1595
1596static __inline void
36f82b23 1597hammer_modify_node_all(hammer_transaction_t trans, struct hammer_node *node)
8cd0a023 1598{
4c286c36 1599 KKASSERT((node->flags & HAMMER_NODE_CRCBAD) == 0);
36f82b23
MD
1600 hammer_modify_buffer(trans, node->buffer,
1601 node->ondisk, sizeof(*node->ondisk));
427e5fc6
MD
1602}
1603
bf686dbe 1604static __inline void
36f82b23
MD
1605hammer_modify_node(hammer_transaction_t trans, hammer_node_t node,
1606 void *base, int len)
bf686dbe 1607{
19619882
MD
1608 hammer_crc_t *crcptr;
1609
bf686dbe
MD
1610 KKASSERT((char *)base >= (char *)node->ondisk &&
1611 (char *)base + len <=
1612 (char *)node->ondisk + sizeof(*node->ondisk));
4c286c36 1613 KKASSERT((node->flags & HAMMER_NODE_CRCBAD) == 0);
283a4a38
MD
1614
1615 if (hammer_btree_full_undo) {
1616 hammer_modify_node_all(trans, node);
1617 } else {
1618 hammer_modify_buffer(trans, node->buffer, base, len);
1619 crcptr = &node->ondisk->crc;
1620 hammer_modify_buffer(trans, node->buffer,
1621 crcptr, sizeof(hammer_crc_t));
1622 --node->buffer->io.modify_refs; /* only want one ref */
1623 }
bf686dbe
MD
1624}
1625
bcac4bbb
MD
1626/*
1627 * Indicate that the specified modifications have been completed.
1628 *
1629 * Do not try to generate the crc here, it's very expensive to do and a
1630 * sequence of insertions or deletions can result in many calls to this
1631 * function on the same node.
1632 */
10a5d1ba
MD
1633static __inline void
1634hammer_modify_node_done(hammer_node_t node)
1635{
bcac4bbb
MD
1636 node->flags |= HAMMER_NODE_CRCGOOD;
1637 if ((node->flags & HAMMER_NODE_NEEDSCRC) == 0) {
1638 node->flags |= HAMMER_NODE_NEEDSCRC;
1639 node->buffer->io.gencrc = 1;
1640 hammer_ref_node(node);
1641 }
10a5d1ba
MD
1642 hammer_modify_buffer_done(node->buffer);
1643}
f4fe61c2 1644
f4fe61c2
MD
1645/*
1646 * Lookup a blockmap offset.
1647 */
1648static __inline hammer_off_t
1649hammer_blockmap_lookup(hammer_mount_t hmp, hammer_off_t zone_offset,
1650 int *errorp)
1651{
1652#if defined INVARIANTS
1653 int zone = HAMMER_ZONE_DECODE(zone_offset);
ccdea233 1654 KKASSERT(zone >= HAMMER_ZONE2_MAPPED_INDEX && zone < HAMMER_MAX_ZONES);
f4fe61c2
MD
1655#endif
1656
1657 /*
1658 * We can actually skip blockmap verify by default,
1659 * as normal blockmaps are now direct-mapped onto the freemap
1660 * and so represent zone-2 addresses.
1661 */
1662 if (hammer_verify_zone == 0) {
1663 *errorp = 0;
1664 return hammer_xlate_to_zone2(zone_offset);
1665 }
1666
1667 return hammer_blockmap_lookup_verify(hmp, zone_offset, errorp);
1668}
10a5d1ba 1669
e8599db1
MD
1670#define hammer_modify_volume_field(trans, vol, field) \
1671 hammer_modify_volume(trans, vol, &(vol)->ondisk->field, \
1672 sizeof((vol)->ondisk->field))
1673
c9b9e29d
MD
1674#define hammer_modify_node_field(trans, node, field) \
1675 hammer_modify_node(trans, node, &(node)->ondisk->field, \
1676 sizeof((node)->ondisk->field))
1677
beec5dc4
MD
1678/*
1679 * The HAMMER_INODE_CAP_DIR_LOCAL_INO capability is set on newly
1680 * created directories for HAMMER version 2 or greater and causes
1681 * directory entries to be placed the inode localization zone in
1682 * the B-Tree instead of the misc zone.
1683 *
1684 * This greatly improves localization between directory entries and
1685 * inodes
1686 */
1687static __inline u_int32_t
1688hammer_dir_localization(hammer_inode_t dip)
1689{
1690 if (dip->ino_data.cap_flags & HAMMER_INODE_CAP_DIR_LOCAL_INO)
1691 return(HAMMER_LOCALIZE_INODE);
1692 else
1693 return(HAMMER_LOCALIZE_MISC);
1694}
1caa2035 1695#endif /* _KERNEL */