Properly yield to userland processes.
[dragonfly.git] / sys / vfs / hammer / hammer.h
CommitLineData
8750964d
MD
1/*
2 * Copyright (c) 2007 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
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.
20 *
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.
33 *
0729c8c8 34 * $DragonFly: src/sys/vfs/hammer/hammer.h,v 1.54 2008/04/29 01:10:37 dillon Exp $
8750964d
MD
35 */
36/*
37 * This header file contains structures used internally by the HAMMERFS
c60bb2c5 38 * implementation. See hammer_disk.h for on-disk structures.
8750964d
MD
39 */
40
427e5fc6
MD
41#include <sys/param.h>
42#include <sys/types.h>
43#include <sys/kernel.h>
42c7d26b 44#include <sys/conf.h>
427e5fc6 45#include <sys/systm.h>
8750964d
MD
46#include <sys/tree.h>
47#include <sys/malloc.h>
427e5fc6 48#include <sys/mount.h>
513ca7d7 49#include <sys/mountctl.h>
427e5fc6 50#include <sys/vnode.h>
42c7d26b 51#include <sys/proc.h>
427e5fc6 52#include <sys/globaldata.h>
66325755
MD
53#include <sys/lockf.h>
54#include <sys/buf.h>
8cd0a023 55#include <sys/queue.h>
66325755
MD
56#include <sys/globaldata.h>
57
58#include <sys/buf2.h>
855942b6 59#include <sys/signal2.h>
427e5fc6 60#include "hammer_disk.h"
8750964d 61#include "hammer_mount.h"
7dc57964 62#include "hammer_ioctl.h"
8750964d
MD
63
64#if defined(_KERNEL) || defined(_KERNEL_STRUCTURES)
65
66MALLOC_DECLARE(M_HAMMER);
67
66325755
MD
68struct hammer_mount;
69
8750964d
MD
70/*
71 * Key structure used for custom RB tree inode lookups. This prototypes
72 * the function hammer_ino_rb_tree_RB_LOOKUP_INFO(root, info).
73 */
74typedef struct hammer_inode_info {
513ca7d7 75 int64_t obj_id; /* (key) object identifier */
8750964d
MD
76 hammer_tid_t obj_asof; /* (key) snapshot transid or 0 */
77} *hammer_inode_info_t;
78
b84de5af
MD
79typedef enum hammer_transaction_type {
80 HAMMER_TRANS_RO,
81 HAMMER_TRANS_STD,
82 HAMMER_TRANS_FLS
83} hammer_transaction_type_t;
84
66325755
MD
85/*
86 * HAMMER Transaction tracking
87 */
88struct hammer_transaction {
b84de5af 89 hammer_transaction_type_t type;
66325755
MD
90 struct hammer_mount *hmp;
91 hammer_tid_t tid;
b84de5af 92 hammer_tid_t time;
a89aec1b 93 struct hammer_volume *rootvol;
66325755
MD
94};
95
8cd0a023
MD
96typedef struct hammer_transaction *hammer_transaction_t;
97
66325755
MD
98/*
99 * HAMMER locks
100 */
427e5fc6 101struct hammer_lock {
0b075555 102 int refs; /* active references delay writes */
0b075555 103 int lockcount; /* lock count for exclusive/shared access */
427e5fc6
MD
104 int wanted;
105 struct thread *locktd;
8750964d
MD
106};
107
427e5fc6
MD
108static __inline int
109hammer_islocked(struct hammer_lock *lock)
110{
8cd0a023 111 return(lock->lockcount != 0);
427e5fc6
MD
112}
113
0b075555
MD
114static __inline int
115hammer_isactive(struct hammer_lock *lock)
116{
117 return(lock->refs != 0);
118}
119
427e5fc6
MD
120static __inline int
121hammer_islastref(struct hammer_lock *lock)
122{
123 return(lock->refs == 1);
124}
c60bb2c5 125
6a37e7e4 126/*
7aa3b8a6 127 * Return if we specifically own the lock exclusively.
6a37e7e4
MD
128 */
129static __inline int
7aa3b8a6 130hammer_lock_excl_owned(struct hammer_lock *lock, thread_t td)
6a37e7e4 131{
7aa3b8a6
MD
132 if (lock->lockcount > 0 && lock->locktd == td)
133 return(1);
134 return(0);
6a37e7e4
MD
135}
136
ec4e8497
MD
137/*
138 * inode->inode dependancy
139 */
140typedef struct hammer_depend {
141 TAILQ_ENTRY(hammer_depend) ip_entry;
142 TAILQ_ENTRY(hammer_depend) rec_entry;
143 struct hammer_inode *ip;
144 struct hammer_record *record;
145} *hammer_depend_t;
146
147TAILQ_HEAD(hammer_depend_list, hammer_depend);
148
0729c8c8
MD
149/*
150 * Cache object ids. A fixed number of objid cache structures are
151 * created to reserve object id's for newly created files in multiples
152 * of 100,000, localized to a particular directory, and recycled as
153 * needed. This allows parallel create operations in different
154 * directories to retain fairly localized object ids which in turn
155 * improves reblocking performance and layout.
156 */
157#define OBJID_CACHE_SIZE 128
158#define OBJID_CACHE_BULK 100000
159
160typedef struct hammer_objid_cache {
161 TAILQ_ENTRY(hammer_objid_cache) entry;
162 struct hammer_inode *dip;
163 hammer_tid_t next_tid;
164 int count;
165} *hammer_objid_cache_t;
166
8750964d 167/*
8cd0a023
MD
168 * Structure used to represent an inode in-memory.
169 *
170 * The record and data associated with an inode may be out of sync with
171 * the disk (xDIRTY flags), or not even on the disk at all (ONDISK flag
172 * clear).
173 *
174 * An inode may also hold a cache of unsynchronized records, used for
175 * database and directories only. Unsynchronized regular file data is
176 * stored in the buffer cache.
177 *
178 * NOTE: A file which is created and destroyed within the initial
179 * synchronization period can wind up not doing any disk I/O at all.
180 *
181 * Finally, an inode may cache numerous disk-referencing B-Tree cursors.
8750964d
MD
182 */
183struct hammer_ino_rb_tree;
184struct hammer_inode;
185RB_HEAD(hammer_ino_rb_tree, hammer_inode);
186RB_PROTOTYPEX(hammer_ino_rb_tree, INFO, hammer_inode, rb_node,
8cd0a023
MD
187 hammer_ino_rb_compare, hammer_inode_info_t);
188
189struct hammer_rec_rb_tree;
190struct hammer_record;
191RB_HEAD(hammer_rec_rb_tree, hammer_record);
192RB_PROTOTYPEX(hammer_rec_rb_tree, INFO, hammer_record, rb_node,
193 hammer_rec_rb_compare, hammer_base_elm_t);
194
195TAILQ_HEAD(hammer_node_list, hammer_node);
8750964d 196
b84de5af
MD
197typedef enum hammer_inode_state {
198 HAMMER_FST_IDLE,
199 HAMMER_FST_SETUP,
200 HAMMER_FST_FLUSH
201} hammer_inode_state_t;
202
8750964d
MD
203struct hammer_inode {
204 RB_ENTRY(hammer_inode) rb_node;
b84de5af 205 hammer_inode_state_t flush_state;
059819e3 206 TAILQ_ENTRY(hammer_inode) flush_entry;
ec4e8497 207 struct hammer_depend_list depend_list;
b84de5af
MD
208 u_int64_t obj_id; /* (key) object identifier */
209 hammer_tid_t obj_asof; /* (key) snapshot or 0 */
210 struct hammer_mount *hmp;
0729c8c8 211 hammer_objid_cache_t objid_cache;
b84de5af
MD
212 int flags;
213 int error; /* flush error */
ec4e8497 214 int depend_count;
b84de5af
MD
215 int cursor_ip_refs; /* sanity */
216 struct vnode *vp;
217 struct lockf advlock;
218 struct hammer_lock lock; /* sync copy interlock */
219 TAILQ_HEAD(, bio) bio_list; /* BIOs to flush out */
220 TAILQ_HEAD(, bio) bio_alt_list; /* BIOs to flush out */
221 off_t trunc_off;
222 struct hammer_inode_record ino_rec; /* in-memory cache */
223 struct hammer_inode_data ino_data; /* in-memory cache */
224 struct hammer_rec_rb_tree rec_tree; /* in-memory cache */
61aeeb33 225 struct hammer_node *cache[2]; /* search initiate cache */
b84de5af
MD
226
227 /*
228 * When a demark is created to synchronize an inode to
229 * disk, certain fields are copied so the front-end VOPs
230 * can continue to run in parallel with the synchronization
231 * occuring in the background.
232 */
233 int sync_flags; /* to-sync flags cache */
234 off_t sync_trunc_off; /* to-sync truncation */
235 struct hammer_inode_record sync_ino_rec;/* to-sync cache */
236 struct hammer_inode_data sync_ino_data; /* to-sync cache */
8750964d
MD
237};
238
8cd0a023
MD
239typedef struct hammer_inode *hammer_inode_t;
240
66325755
MD
241#define VTOI(vp) ((struct hammer_inode *)(vp)->v_data)
242
8cd0a023
MD
243#define HAMMER_INODE_DDIRTY 0x0001 /* in-memory ino_data is dirty */
244#define HAMMER_INODE_RDIRTY 0x0002 /* in-memory ino_rec is dirty */
245#define HAMMER_INODE_ITIMES 0x0004 /* in-memory mtime/atime modified */
059819e3 246#define HAMMER_INODE_XDIRTY 0x0008 /* in-memory records/flsbufs present */
8cd0a023 247#define HAMMER_INODE_ONDISK 0x0010 /* inode is on-disk (else not yet) */
a89aec1b 248#define HAMMER_INODE_FLUSH 0x0020 /* flush on last ref */
c0ade690 249#define HAMMER_INODE_DELETED 0x0080 /* inode ready for deletion */
76376933 250#define HAMMER_INODE_DELONDISK 0x0100 /* delete synchronized to disk */
d113fda1
MD
251#define HAMMER_INODE_RO 0x0200 /* read-only (because of as-of) */
252#define HAMMER_INODE_GONE 0x0400 /* delete flushed out */
0a72edae 253#define HAMMER_INODE_DONDISK 0x0800 /* data records may be on disk */
f3b0f382 254#define HAMMER_INODE_BUFS 0x1000 /* dirty high level bps present */
b84de5af
MD
255#define HAMMER_INODE_REFLUSH 0x2000 /* pipelined flush during flush */
256#define HAMMER_INODE_UNUSED4000 0x4000
059819e3 257#define HAMMER_INODE_FLUSHW 0x8000 /* Someone waiting for flush */
8cd0a023 258
b84de5af 259#define HAMMER_INODE_TRUNCATED 0x00010000
9480ff55 260#define HAMMER_INODE_NEW 0x00020000
b84de5af 261
0b075555 262#define HAMMER_INODE_MODMASK (HAMMER_INODE_DDIRTY|HAMMER_INODE_RDIRTY| \
f3b0f382 263 HAMMER_INODE_XDIRTY|HAMMER_INODE_BUFS| \
b84de5af 264 HAMMER_INODE_ITIMES|HAMMER_INODE_TRUNCATED)
0b075555 265
8cd0a023 266#define HAMMER_MAX_INODE_CURSORS 4
66325755 267
f90dde4c
MD
268#define HAMMER_FLUSH_SIGNAL 0x0001
269#define HAMMER_FLUSH_FORCE 0x0002
ec4e8497 270#define HAMMER_FLUSH_RELEASE 0x0004
f90dde4c 271
8750964d 272/*
8cd0a023
MD
273 * Structure used to represent an unsynchronized record in-memory. This
274 * structure is orgranized in a per-inode RB-tree. If the inode is not
275 * on disk then neither are any records and the in-memory record tree
276 * represents the entire contents of the inode. If the inode is on disk
277 * then the on-disk B-Tree is scanned in parallel with the in-memory
278 * RB-Tree to synthesize the current state of the file.
279 *
280 * Only current (delete_tid == 0) unsynchronized records are kept in-memory.
b33e2cc0
MD
281 *
282 * blocked is the count of the number of cursors (ip_first/ip_next) blocked
283 * on the record waiting for a synchronization to complete.
8750964d 284 */
8cd0a023
MD
285struct hammer_record {
286 RB_ENTRY(hammer_record) rb_node;
b84de5af 287 hammer_inode_state_t state;
a89aec1b 288 struct hammer_lock lock;
8cd0a023
MD
289 struct hammer_inode *ip;
290 union hammer_record_ondisk rec;
291 union hammer_data_ondisk *data;
8cd0a023 292 int flags;
ec4e8497 293 struct hammer_depend_list depend_list;
8cd0a023 294};
8750964d 295
8cd0a023
MD
296typedef struct hammer_record *hammer_record_t;
297
d36ec43b
MD
298/*
299 * Record flags. Note that FE can only be set by the frontend if the
300 * record has not been interlocked by the backend w/ BE.
301 */
8cd0a023
MD
302#define HAMMER_RECF_ALLOCDATA 0x0001
303#define HAMMER_RECF_ONRBTREE 0x0002
b84de5af 304#define HAMMER_RECF_DELETED_FE 0x0004 /* deleted (frontend) */
d36ec43b
MD
305#define HAMMER_RECF_DELETED_BE 0x0008 /* deleted (backend) */
306#define HAMMER_RECF_INBAND 0x0010
307#define HAMMER_RECF_INTERLOCK_BE 0x0020 /* backend interlock */
308#define HAMMER_RECF_WANTED 0x0040
309#define HAMMER_RECF_DELETE_ONDISK 0x0080
310#define HAMMER_RECF_CONVERT_DELETE_ONDISK 0x0100 /* special case */
8cd0a023
MD
311
312/*
47197d71 313 * In-memory structures representing on-disk structures.
8cd0a023 314 */
8750964d 315struct hammer_volume;
427e5fc6 316struct hammer_buffer;
8cd0a023 317struct hammer_node;
8750964d 318RB_HEAD(hammer_vol_rb_tree, hammer_volume);
427e5fc6 319RB_HEAD(hammer_buf_rb_tree, hammer_buffer);
8cd0a023 320RB_HEAD(hammer_nod_rb_tree, hammer_node);
8750964d
MD
321
322RB_PROTOTYPE2(hammer_vol_rb_tree, hammer_volume, rb_node,
323 hammer_vol_rb_compare, int32_t);
427e5fc6 324RB_PROTOTYPE2(hammer_buf_rb_tree, hammer_buffer, rb_node,
47197d71 325 hammer_buf_rb_compare, hammer_off_t);
8cd0a023 326RB_PROTOTYPE2(hammer_nod_rb_tree, hammer_node, rb_node,
47197d71 327 hammer_nod_rb_compare, hammer_off_t);
8750964d 328
66325755
MD
329/*
330 * IO management - embedded at the head of various in-memory structures
10a5d1ba
MD
331 *
332 * VOLUME - hammer_volume containing meta-data
333 * META_BUFFER - hammer_buffer containing meta-data
334 * DATA_BUFFER - hammer_buffer containing pure-data
335 *
336 * Dirty volume headers and dirty meta-data buffers are locked until the
337 * flusher can sequence them out. Dirty pure-data buffers can be written.
338 * Clean buffers can be passively released.
66325755 339 */
10a5d1ba
MD
340typedef enum hammer_io_type {
341 HAMMER_STRUCTURE_VOLUME,
342 HAMMER_STRUCTURE_META_BUFFER,
343 HAMMER_STRUCTURE_UNDO_BUFFER,
344 HAMMER_STRUCTURE_DATA_BUFFER
345} hammer_io_type_t;
66325755
MD
346
347union hammer_io_structure;
055f5ff8 348struct hammer_io;
66325755
MD
349
350struct worklist {
351 LIST_ENTRY(worklist) node;
352};
353
10a5d1ba
MD
354TAILQ_HEAD(hammer_io_list, hammer_io);
355typedef struct hammer_io_list *hammer_io_list_t;
055f5ff8 356
66325755 357struct hammer_io {
10a5d1ba
MD
358 struct worklist worklist;
359 struct hammer_lock lock;
360 enum hammer_io_type type;
361 struct hammer_mount *hmp;
362 TAILQ_ENTRY(hammer_io) mod_entry; /* list entry if modified */
363 hammer_io_list_t mod_list;
364 struct buf *bp;
365 int64_t offset;
366 int loading; /* loading/unloading interlock */
367 int modify_refs;
368
66325755
MD
369 u_int modified : 1; /* bp's data was modified */
370 u_int released : 1; /* bp released (w/ B_LOCKED set) */
055f5ff8
MD
371 u_int running : 1; /* bp write IO in progress */
372 u_int waiting : 1; /* someone is waiting on us */
b33e2cc0 373 u_int validated : 1; /* ondisk has been validated */
b58c6388
MD
374 u_int flush : 1; /* flush on last release */
375 u_int waitdep : 1; /* flush waits for dependancies */
66325755
MD
376};
377
8cd0a023
MD
378typedef struct hammer_io *hammer_io_t;
379
66325755 380/*
8cd0a023 381 * In-memory volume representing on-disk buffer
66325755 382 */
8750964d 383struct hammer_volume {
66325755 384 struct hammer_io io;
8750964d 385 RB_ENTRY(hammer_volume) rb_node;
47197d71 386 struct hammer_buf_rb_tree rb_bufs_root;
8750964d
MD
387 struct hammer_volume_ondisk *ondisk;
388 int32_t vol_no;
fbc6e32a 389 int64_t nblocks; /* note: special calculation for statfs */
47197d71
MD
390 int64_t buffer_base; /* base offset of buffer 0 */
391 hammer_off_t maxbuf_off; /* Maximum buffer offset */
c60bb2c5 392 char *vol_name;
8750964d 393 struct vnode *devvp;
427e5fc6 394 int vol_flags;
8750964d
MD
395};
396
8cd0a023
MD
397typedef struct hammer_volume *hammer_volume_t;
398
66325755 399/*
8cd0a023
MD
400 * In-memory buffer (other then volume, super-cluster, or cluster),
401 * representing an on-disk buffer.
66325755 402 */
427e5fc6 403struct hammer_buffer {
66325755 404 struct hammer_io io;
427e5fc6 405 RB_ENTRY(hammer_buffer) rb_node;
47197d71 406 void *ondisk;
427e5fc6 407 struct hammer_volume *volume;
34d829f7
MD
408 hammer_off_t zone2_offset;
409 hammer_off_t zoneX_offset;
8cd0a023 410 struct hammer_node_list clist;
66325755
MD
411};
412
8cd0a023
MD
413typedef struct hammer_buffer *hammer_buffer_t;
414
415/*
416 * In-memory B-Tree node, representing an on-disk B-Tree node.
417 *
418 * This is a hang-on structure which is backed by a hammer_buffer,
419 * indexed by a hammer_cluster, and used for fine-grained locking of
420 * B-Tree nodes in order to properly control lock ordering. A hammer_buffer
421 * can contain multiple nodes representing wildly disassociated portions
422 * of the B-Tree so locking cannot be done on a buffer-by-buffer basis.
423 *
424 * This structure uses a cluster-relative index to reduce the number
425 * of layers required to access it, and also because all on-disk B-Tree
426 * references are cluster-relative offsets.
427 */
428struct hammer_node {
429 struct hammer_lock lock; /* node-by-node lock */
430 TAILQ_ENTRY(hammer_node) entry; /* per-buffer linkage */
431 RB_ENTRY(hammer_node) rb_node; /* per-cluster linkage */
47197d71 432 hammer_off_t node_offset; /* full offset spec */
40043e7f 433 struct hammer_mount *hmp;
8cd0a023
MD
434 struct hammer_buffer *buffer; /* backing buffer */
435 hammer_node_ondisk_t ondisk; /* ptr to on-disk structure */
436 struct hammer_node **cache1; /* passive cache(s) */
437 struct hammer_node **cache2;
b3deaf57 438 int flags;
740d8317 439 int loading; /* load interlock */
8cd0a023
MD
440};
441
b3deaf57
MD
442#define HAMMER_NODE_DELETED 0x0001
443#define HAMMER_NODE_FLUSH 0x0002
b3deaf57 444
8cd0a023
MD
445typedef struct hammer_node *hammer_node_t;
446
b33e2cc0
MD
447/*
448 * List of locked nodes.
449 */
450struct hammer_node_locklist {
451 struct hammer_node_locklist *next;
452 hammer_node_t node;
453};
454
455typedef struct hammer_node_locklist *hammer_node_locklist_t;
456
457
8cd0a023
MD
458/*
459 * Common I/O management structure - embedded in in-memory structures
460 * which are backed by filesystem buffers.
461 */
66325755
MD
462union hammer_io_structure {
463 struct hammer_io io;
464 struct hammer_volume volume;
66325755 465 struct hammer_buffer buffer;
8750964d
MD
466};
467
055f5ff8
MD
468typedef union hammer_io_structure *hammer_io_structure_t;
469
bf686dbe
MD
470/*
471 * Allocation holes are recorded for a short period of time in an attempt
472 * to use up the space.
473 */
474
475#define HAMMER_MAX_HOLES 8
476
477struct hammer_hole;
478
479struct hammer_holes {
480 TAILQ_HEAD(, hammer_hole) list;
481 int count;
482};
483
484typedef struct hammer_holes *hammer_holes_t;
485
486struct hammer_hole {
487 TAILQ_ENTRY(hammer_hole) entry;
488 hammer_off_t offset;
489 int bytes;
490};
491
492typedef struct hammer_hole *hammer_hole_t;
427e5fc6 493
8cd0a023
MD
494#include "hammer_cursor.h"
495
8750964d
MD
496/*
497 * Internal hammer mount data structure
498 */
499struct hammer_mount {
500 struct mount *mp;
27ea2398 501 /*struct vnode *rootvp;*/
8750964d
MD
502 struct hammer_ino_rb_tree rb_inos_root;
503 struct hammer_vol_rb_tree rb_vols_root;
40043e7f 504 struct hammer_nod_rb_tree rb_nods_root;
8750964d 505 struct hammer_volume *rootvol;
47197d71
MD
506 struct hammer_base_elm root_btree_beg;
507 struct hammer_base_elm root_btree_end;
d26d0ae9 508 char *zbuf; /* HAMMER_BUFSIZE bytes worth of all-zeros */
195c19a1
MD
509 int hflags;
510 int ronly;
511 int nvolumes;
d26d0ae9 512 int volume_iterator;
059819e3
MD
513 int flusher_seq;
514 int flusher_act;
515 int flusher_exiting;
f90dde4c 516 int reclaim_count;
059819e3 517 thread_t flusher_td;
855942b6 518 u_int check_interrupt;
8750964d 519 uuid_t fsid;
66325755 520 udev_t fsid_udev;
10a5d1ba
MD
521 struct hammer_io_list volu_list; /* dirty undo buffers */
522 struct hammer_io_list undo_list; /* dirty undo buffers */
523 struct hammer_io_list data_list; /* dirty data buffers */
524 struct hammer_io_list meta_list; /* dirty meta bufs */
525 struct hammer_io_list lose_list; /* loose buffers */
526 int locked_dirty_count; /* meta/volu count */
f90dde4c 527 int io_running_count;
0729c8c8 528 int objid_cache_count;
7f7c1f84 529 hammer_tid_t asof;
b84de5af 530 hammer_off_t next_tid;
66325755 531 u_int32_t namekey_iterator;
f03c9cf4 532 hammer_off_t zone_limits[HAMMER_MAX_ZONES];
513ca7d7 533 struct netexport export;
9480ff55 534 struct hammer_lock sync_lock;
40043e7f 535 struct lock blockmap_lock;
0729c8c8
MD
536 hammer_inode_t flusher_demark;
537 struct hammer_blockmap blockmap[HAMMER_MAX_ZONES];
bf686dbe 538 struct hammer_holes holes[HAMMER_MAX_ZONES];
059819e3 539 TAILQ_HEAD(, hammer_inode) flush_list;
0729c8c8
MD
540 TAILQ_HEAD(, hammer_inode) flush_alt_list;
541 TAILQ_HEAD(, hammer_objid_cache) objid_cache_list;
8750964d
MD
542};
543
8cd0a023 544typedef struct hammer_mount *hammer_mount_t;
8750964d 545
fbc6e32a
MD
546struct hammer_sync_info {
547 int error;
548 int waitfor;
549};
550
8750964d
MD
551#endif
552
553#if defined(_KERNEL)
554
555extern struct vop_ops hammer_vnode_vops;
7a04d74f
MD
556extern struct vop_ops hammer_spec_vops;
557extern struct vop_ops hammer_fifo_vops;
66325755 558extern struct bio_ops hammer_bioops;
427e5fc6 559
d5ef456e 560extern int hammer_debug_general;
7d683b0f 561extern int hammer_debug_locks;
b3deaf57 562extern int hammer_debug_btree;
d113fda1 563extern int hammer_debug_tid;
b33e2cc0 564extern int hammer_debug_recover;
46fe7ae1 565extern int hammer_debug_recover_faults;
b3deaf57
MD
566extern int hammer_count_inodes;
567extern int hammer_count_records;
568extern int hammer_count_record_datas;
569extern int hammer_count_volumes;
b3deaf57
MD
570extern int hammer_count_buffers;
571extern int hammer_count_nodes;
9480ff55
MD
572extern int hammer_count_dirtybufs;
573extern int hammer_limit_dirtybufs;
7d683b0f 574extern int64_t hammer_contention_count;
b3deaf57 575
8750964d
MD
576int hammer_vop_inactive(struct vop_inactive_args *);
577int hammer_vop_reclaim(struct vop_reclaim_args *);
66325755
MD
578int hammer_get_vnode(struct hammer_inode *ip, int lktype,
579 struct vnode **vpp);
36f82b23 580struct hammer_inode *hammer_get_inode(hammer_transaction_t trans,
61aeeb33 581 struct hammer_node **cache,
d113fda1
MD
582 u_int64_t obj_id, hammer_tid_t asof, int flags,
583 int *errorp);
66325755
MD
584void hammer_put_inode(struct hammer_inode *ip);
585void hammer_put_inode_ref(struct hammer_inode *ip);
586
8cd0a023 587int hammer_unload_volume(hammer_volume_t volume, void *data __unused);
a89aec1b 588int hammer_unload_buffer(hammer_buffer_t buffer, void *data __unused);
8cd0a023
MD
589int hammer_install_volume(hammer_mount_t hmp, const char *volname);
590
a89aec1b
MD
591int hammer_ip_lookup(hammer_cursor_t cursor, hammer_inode_t ip);
592int hammer_ip_first(hammer_cursor_t cursor, hammer_inode_t ip);
593int hammer_ip_next(hammer_cursor_t cursor);
40043e7f 594int hammer_ip_resolve_record_and_data(hammer_cursor_t cursor);
8cd0a023 595int hammer_ip_resolve_data(hammer_cursor_t cursor);
195c19a1 596int hammer_ip_delete_record(hammer_cursor_t cursor, hammer_tid_t tid);
7dc57964 597int hammer_delete_at_cursor(hammer_cursor_t cursor, int64_t *stat_bytes);
b3deaf57
MD
598int hammer_ip_check_directory_empty(hammer_transaction_t trans,
599 hammer_inode_t ip);
fbc6e32a 600int hammer_sync_hmp(hammer_mount_t hmp, int waitfor);
fbc6e32a 601
8cd0a023 602hammer_record_t
40043e7f 603 hammer_alloc_mem_record(hammer_inode_t ip);
d36ec43b 604void hammer_flush_record_done(hammer_record_t record, int error);
b84de5af 605void hammer_wait_mem_record(hammer_record_t record);
b3deaf57 606void hammer_rel_mem_record(hammer_record_t record);
d36ec43b 607void hammer_cleardep_mem_record(struct hammer_record *record);
b84de5af 608
6a37e7e4 609int hammer_cursor_up(hammer_cursor_t cursor);
8cd0a023 610int hammer_cursor_down(hammer_cursor_t cursor);
6a37e7e4
MD
611int hammer_cursor_upgrade(hammer_cursor_t cursor);
612void hammer_cursor_downgrade(hammer_cursor_t cursor);
32c90105
MD
613int hammer_cursor_seek(hammer_cursor_t cursor, hammer_node_t node,
614 int index);
8cd0a023
MD
615void hammer_lock_ex(struct hammer_lock *lock);
616int hammer_lock_ex_try(struct hammer_lock *lock);
617void hammer_lock_sh(struct hammer_lock *lock);
6a37e7e4
MD
618int hammer_lock_upgrade(struct hammer_lock *lock);
619void hammer_lock_downgrade(struct hammer_lock *lock);
427e5fc6 620void hammer_unlock(struct hammer_lock *lock);
66325755
MD
621void hammer_ref(struct hammer_lock *lock);
622void hammer_unref(struct hammer_lock *lock);
8cd0a023 623
66325755 624u_int32_t hammer_to_unix_xid(uuid_t *uuid);
8cd0a023
MD
625void hammer_guid_to_uuid(uuid_t *uuid, u_int32_t guid);
626void hammer_to_timespec(hammer_tid_t tid, struct timespec *ts);
627hammer_tid_t hammer_timespec_to_transid(struct timespec *ts);
7f7c1f84 628hammer_tid_t hammer_now_tid(void);
d113fda1 629hammer_tid_t hammer_str_to_tid(const char *str);
0729c8c8
MD
630hammer_tid_t hammer_alloc_objid(hammer_transaction_t trans, hammer_inode_t dip);
631void hammer_clear_objid(hammer_inode_t dip);
632void hammer_destroy_objid_cache(hammer_mount_t hmp);
8cd0a023 633
66325755 634enum vtype hammer_get_vnode_type(u_int8_t obj_type);
6b4f890b 635int hammer_get_dtype(u_int8_t obj_type);
66325755
MD
636u_int8_t hammer_get_obj_type(enum vtype vtype);
637int64_t hammer_directory_namekey(void *name, int len);
427e5fc6 638
36f82b23
MD
639int hammer_init_cursor(hammer_transaction_t trans, hammer_cursor_t cursor,
640 struct hammer_node **cache);
195c19a1 641
8cd0a023 642void hammer_done_cursor(hammer_cursor_t cursor);
6b4f890b 643void hammer_mem_done(hammer_cursor_t cursor);
8cd0a023
MD
644
645int hammer_btree_lookup(hammer_cursor_t cursor);
d26d0ae9 646int hammer_btree_first(hammer_cursor_t cursor);
32c90105 647int hammer_btree_last(hammer_cursor_t cursor);
8cd0a023
MD
648int hammer_btree_extract(hammer_cursor_t cursor, int flags);
649int hammer_btree_iterate(hammer_cursor_t cursor);
32c90105 650int hammer_btree_iterate_reverse(hammer_cursor_t cursor);
8cd0a023
MD
651int hammer_btree_insert(hammer_cursor_t cursor, hammer_btree_elm_t elm);
652int hammer_btree_delete(hammer_cursor_t cursor);
653int hammer_btree_cmp(hammer_base_elm_t key1, hammer_base_elm_t key2);
d26d0ae9 654int hammer_btree_chkts(hammer_tid_t ts, hammer_base_elm_t key);
32c90105
MD
655int hammer_btree_correct_rhb(hammer_cursor_t cursor, hammer_tid_t tid);
656int hammer_btree_correct_lhb(hammer_cursor_t cursor, hammer_tid_t tid);
657
fe7678ee 658
b33e2cc0
MD
659int hammer_btree_lock_children(hammer_cursor_t cursor,
660 struct hammer_node_locklist **locklistp);
b33e2cc0 661
c0ade690
MD
662void hammer_print_btree_node(hammer_node_ondisk_t ondisk);
663void hammer_print_btree_elm(hammer_btree_elm_t elm, u_int8_t type, int i);
8750964d 664
47197d71
MD
665void *hammer_bread(struct hammer_mount *hmp, hammer_off_t off,
666 int *errorp, struct hammer_buffer **bufferp);
667void *hammer_bnew(struct hammer_mount *hmp, hammer_off_t off,
668 int *errorp, struct hammer_buffer **bufferp);
8cd0a023
MD
669
670hammer_volume_t hammer_get_root_volume(hammer_mount_t hmp, int *errorp);
059819e3
MD
671int hammer_dowrite(hammer_transaction_t trans, hammer_inode_t ip,
672 struct bio *bio);
427e5fc6 673
8cd0a023 674hammer_volume_t hammer_get_volume(hammer_mount_t hmp,
427e5fc6 675 int32_t vol_no, int *errorp);
47197d71
MD
676hammer_buffer_t hammer_get_buffer(hammer_mount_t hmp,
677 hammer_off_t buf_offset, int isnew, int *errorp);
b58c6388 678void hammer_uncache_buffer(struct hammer_mount *hmp, hammer_off_t off);
8cd0a023 679
fbc6e32a 680int hammer_ref_volume(hammer_volume_t volume);
8cd0a023
MD
681int hammer_ref_buffer(hammer_buffer_t buffer);
682void hammer_flush_buffer_nodes(hammer_buffer_t buffer);
683
8cd0a023 684void hammer_rel_volume(hammer_volume_t volume, int flush);
8cd0a023
MD
685void hammer_rel_buffer(hammer_buffer_t buffer, int flush);
686
513ca7d7
MD
687int hammer_vfs_export(struct mount *mp, int op,
688 const struct export_args *export);
47197d71
MD
689hammer_node_t hammer_get_node(hammer_mount_t hmp,
690 hammer_off_t node_offset, int *errorp);
740d8317 691void hammer_ref_node(hammer_node_t node);
055f5ff8
MD
692hammer_node_t hammer_ref_node_safe(struct hammer_mount *hmp,
693 struct hammer_node **cache, int *errorp);
8cd0a023 694void hammer_rel_node(hammer_node_t node);
36f82b23
MD
695void hammer_delete_node(hammer_transaction_t trans,
696 hammer_node_t node);
8cd0a023
MD
697void hammer_cache_node(hammer_node_t node,
698 struct hammer_node **cache);
699void hammer_uncache_node(struct hammer_node **cache);
700void hammer_flush_node(hammer_node_t node);
701
427e5fc6
MD
702void hammer_dup_buffer(struct hammer_buffer **bufferp,
703 struct hammer_buffer *buffer);
36f82b23
MD
704hammer_node_t hammer_alloc_btree(hammer_transaction_t trans, int *errorp);
705void *hammer_alloc_record(hammer_transaction_t trans,
bf686dbe 706 hammer_off_t *rec_offp, u_int16_t rec_type,
40043e7f
MD
707 struct hammer_buffer **rec_bufferp,
708 int32_t data_len, void **datap,
709 struct hammer_buffer **data_bufferp, int *errorp);
36f82b23 710void *hammer_alloc_data(hammer_transaction_t trans, int32_t data_len,
bf686dbe
MD
711 hammer_off_t *data_offsetp,
712 struct hammer_buffer **data_bufferp, int *errorp);
713
059819e3
MD
714int hammer_generate_undo(hammer_transaction_t trans, hammer_io_t io,
715 hammer_off_t zone1_offset, void *base, int len);
427e5fc6 716
66325755 717void hammer_put_volume(struct hammer_volume *volume, int flush);
66325755 718void hammer_put_buffer(struct hammer_buffer *buffer, int flush);
427e5fc6 719
36f82b23
MD
720hammer_off_t hammer_freemap_alloc(hammer_transaction_t trans,
721 hammer_off_t owner, int *errorp);
722void hammer_freemap_free(hammer_transaction_t trans, hammer_off_t phys_offset,
c3be93f2 723 hammer_off_t owner, int *errorp);
36f82b23 724hammer_off_t hammer_blockmap_alloc(hammer_transaction_t trans, int zone,
40043e7f 725 int bytes, int *errorp);
36f82b23
MD
726void hammer_blockmap_free(hammer_transaction_t trans,
727 hammer_off_t bmap_off, int bytes);
bf686dbe
MD
728int hammer_blockmap_getfree(hammer_mount_t hmp, hammer_off_t bmap_off,
729 int *curp, int *errorp);
40043e7f
MD
730hammer_off_t hammer_blockmap_lookup(hammer_mount_t hmp, hammer_off_t bmap_off,
731 int *errorp);
bf686dbe
MD
732hammer_off_t hammer_undo_lookup(hammer_mount_t hmp, hammer_off_t bmap_off,
733 int *errorp);
40043e7f 734
8cd0a023
MD
735void hammer_start_transaction(struct hammer_transaction *trans,
736 struct hammer_mount *hmp);
36f82b23
MD
737void hammer_simple_transaction(struct hammer_transaction *trans,
738 struct hammer_mount *hmp);
b84de5af
MD
739void hammer_start_transaction_fls(struct hammer_transaction *trans,
740 struct hammer_mount *hmp);
741void hammer_done_transaction(struct hammer_transaction *trans);
66325755
MD
742
743void hammer_modify_inode(struct hammer_transaction *trans,
8cd0a023 744 hammer_inode_t ip, int flags);
f90dde4c 745void hammer_flush_inode(hammer_inode_t ip, int flags);
b84de5af
MD
746void hammer_flush_inode_done(hammer_inode_t ip);
747void hammer_wait_inode(hammer_inode_t ip);
748
8cd0a023
MD
749int hammer_create_inode(struct hammer_transaction *trans, struct vattr *vap,
750 struct ucred *cred, struct hammer_inode *dip,
751 struct hammer_inode **ipp);
9480ff55
MD
752void hammer_finalize_inode(hammer_transaction_t trans, hammer_inode_t ip,
753 int error);
a89aec1b 754void hammer_rel_inode(hammer_inode_t ip, int flush);
b84de5af 755int hammer_sync_inode(hammer_inode_t ip, int handle_delete);
8cd0a023 756
a89aec1b 757int hammer_ip_add_directory(struct hammer_transaction *trans,
8cd0a023
MD
758 hammer_inode_t dip, struct namecache *ncp,
759 hammer_inode_t nip);
a89aec1b 760int hammer_ip_del_directory(struct hammer_transaction *trans,
8cd0a023
MD
761 hammer_cursor_t cursor, hammer_inode_t dip,
762 hammer_inode_t ip);
7a04d74f
MD
763int hammer_ip_add_record(struct hammer_transaction *trans,
764 hammer_record_t record);
a89aec1b 765int hammer_ip_delete_range(struct hammer_transaction *trans,
47197d71 766 hammer_inode_t ip, int64_t ran_beg, int64_t ran_end);
7a04d74f
MD
767int hammer_ip_delete_range_all(struct hammer_transaction *trans,
768 hammer_inode_t ip);
c0ade690 769int hammer_ip_sync_data(struct hammer_transaction *trans,
8cd0a023 770 hammer_inode_t ip, int64_t offset,
47197d71 771 void *data, int bytes);
36f82b23 772int hammer_ip_sync_record(hammer_transaction_t trans, hammer_record_t rec);
66325755 773
7dc57964
MD
774int hammer_ioctl(hammer_inode_t ip, u_long com, caddr_t data, int fflag,
775 struct ucred *cred);
776
10a5d1ba
MD
777void hammer_io_init(hammer_io_t io, hammer_mount_t hmp,
778 enum hammer_io_type type);
779void hammer_io_reinit(hammer_io_t io, enum hammer_io_type type);
66325755
MD
780int hammer_io_read(struct vnode *devvp, struct hammer_io *io);
781int hammer_io_new(struct vnode *devvp, struct hammer_io *io);
b58c6388 782void hammer_io_release(struct hammer_io *io);
055f5ff8 783void hammer_io_flush(struct hammer_io *io);
8cd0a023 784int hammer_io_checkflush(hammer_io_t io);
61aeeb33 785void hammer_io_clear_modify(struct hammer_io *io);
055f5ff8 786void hammer_io_waitdep(struct hammer_io *io);
66325755 787
36f82b23
MD
788void hammer_modify_volume(hammer_transaction_t trans, hammer_volume_t volume,
789 void *base, int len);
790void hammer_modify_buffer(hammer_transaction_t trans, hammer_buffer_t buffer,
791 void *base, int len);
10a5d1ba
MD
792void hammer_modify_volume_done(hammer_volume_t volume);
793void hammer_modify_buffer_done(hammer_buffer_t buffer);
0b075555 794
36f82b23
MD
795int hammer_ioc_reblock(hammer_transaction_t trans, hammer_inode_t ip,
796 struct hammer_ioc_reblock *reblock);
bf686dbe
MD
797
798void hammer_init_holes(hammer_mount_t hmp, hammer_holes_t holes);
799void hammer_free_holes(hammer_mount_t hmp, hammer_holes_t holes);
855942b6 800int hammer_signal_check(hammer_mount_t hmp);
bf686dbe 801
059819e3
MD
802void hammer_flusher_create(hammer_mount_t hmp);
803void hammer_flusher_destroy(hammer_mount_t hmp);
804void hammer_flusher_sync(hammer_mount_t hmp);
805void hammer_flusher_async(hammer_mount_t hmp);
806
f90dde4c
MD
807int hammer_recover(hammer_mount_t hmp, hammer_volume_t rootvol);
808
055f5ff8 809#endif
427e5fc6 810
8cd0a023 811static __inline void
36f82b23 812hammer_modify_node_noundo(hammer_transaction_t trans, hammer_node_t node)
bf686dbe 813{
36f82b23 814 hammer_modify_buffer(trans, node->buffer, NULL, 0);
bf686dbe
MD
815}
816
817static __inline void
36f82b23 818hammer_modify_node_all(hammer_transaction_t trans, struct hammer_node *node)
8cd0a023 819{
36f82b23
MD
820 hammer_modify_buffer(trans, node->buffer,
821 node->ondisk, sizeof(*node->ondisk));
427e5fc6
MD
822}
823
bf686dbe 824static __inline void
36f82b23
MD
825hammer_modify_node(hammer_transaction_t trans, hammer_node_t node,
826 void *base, int len)
bf686dbe
MD
827{
828 KKASSERT((char *)base >= (char *)node->ondisk &&
829 (char *)base + len <=
830 (char *)node->ondisk + sizeof(*node->ondisk));
36f82b23 831 hammer_modify_buffer(trans, node->buffer, base, len);
bf686dbe
MD
832}
833
10a5d1ba
MD
834static __inline void
835hammer_modify_node_done(hammer_node_t node)
836{
837 hammer_modify_buffer_done(node->buffer);
838}
839
bf686dbe 840static __inline void
36f82b23
MD
841hammer_modify_record(hammer_transaction_t trans, hammer_buffer_t buffer,
842 void *base, int len)
bf686dbe
MD
843{
844 KKASSERT((char *)base >= (char *)buffer->ondisk &&
845 (char *)base + len <= (char *)buffer->ondisk + HAMMER_BUFSIZE);
36f82b23 846 hammer_modify_buffer(trans, buffer, base, len);
bf686dbe
MD
847}
848
10a5d1ba
MD
849static __inline void
850hammer_modify_record_done(hammer_buffer_t buffer)
851{
852 hammer_modify_buffer_done(buffer);
853}
854