hammer2 - Refactor frontend part 4/many
[dragonfly.git] / sys / vfs / hammer2 / hammer2.h
1 /*
2  * Copyright (c) 2011-2015 The DragonFly Project.  All rights reserved.
3  *
4  * This code is derived from software contributed to The DragonFly Project
5  * by Matthew Dillon <dillon@dragonflybsd.org>
6  * by Venkatesh Srinivas <vsrinivas@dragonflybsd.org>
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the
17  *    distribution.
18  * 3. Neither the name of The DragonFly Project nor the names of its
19  *    contributors may be used to endorse or promote products derived
20  *    from this software without specific, prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
26  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
28  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
30  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
31  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
32  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33  * SUCH DAMAGE.
34  */
35
36 /*
37  * HAMMER2 IN-MEMORY CACHE OF MEDIA STRUCTURES
38  *
39  * This header file contains structures used internally by the HAMMER2
40  * implementation.  See hammer2_disk.h for on-disk structures.
41  *
42  * There is an in-memory representation of all on-media data structure.
43  * Almost everything is represented by a hammer2_chain structure in-memory.
44  * Other higher-level structures typically map to chains.
45  *
46  * A great deal of data is accessed simply via its buffer cache buffer,
47  * which is mapped for the duration of the chain's lock.  Hammer2 must
48  * implement its own buffer cache layer on top of the system layer to
49  * allow for different threads to lock different sub-block-sized buffers.
50  *
51  * When modifications are made to a chain a new filesystem block must be
52  * allocated.  Multiple modifications do not typically allocate new blocks
53  * until the current block has been flushed.  Flushes do not block the
54  * front-end unless the front-end operation crosses the current inode being
55  * flushed.
56  *
57  * The in-memory representation may remain cached (for example in order to
58  * placemark clustering locks) even after the related data has been
59  * detached.
60  */
61
62 #ifndef _VFS_HAMMER2_HAMMER2_H_
63 #define _VFS_HAMMER2_HAMMER2_H_
64
65 #include <sys/param.h>
66 #include <sys/types.h>
67 #include <sys/kernel.h>
68 #include <sys/conf.h>
69 #include <sys/systm.h>
70 #include <sys/tree.h>
71 #include <sys/malloc.h>
72 #include <sys/mount.h>
73 #include <sys/vnode.h>
74 #include <sys/proc.h>
75 #include <sys/mountctl.h>
76 #include <sys/priv.h>
77 #include <sys/stat.h>
78 #include <sys/thread.h>
79 #include <sys/globaldata.h>
80 #include <sys/lockf.h>
81 #include <sys/buf.h>
82 #include <sys/queue.h>
83 #include <sys/limits.h>
84 #include <sys/dmsg.h>
85 #include <sys/mutex.h>
86 #include <sys/kern_syscall.h>
87
88 #include <sys/signal2.h>
89 #include <sys/buf2.h>
90 #include <sys/mutex2.h>
91 #include <sys/thread2.h>
92
93 #include "hammer2_disk.h"
94 #include "hammer2_mount.h"
95 #include "hammer2_ioctl.h"
96
97 struct hammer2_io;
98 struct hammer2_iocb;
99 struct hammer2_chain;
100 struct hammer2_cluster;
101 struct hammer2_inode;
102 struct hammer2_dev;
103 struct hammer2_pfs;
104 struct hammer2_span;
105 struct hammer2_state;
106 struct hammer2_msg;
107 struct hammer2_syncthr;
108 struct hammer2_vop_info;
109
110 /*
111  * Mutex and lock shims.  Hammer2 requires support for asynchronous and
112  * abortable locks, and both exclusive and shared spinlocks.  Normal
113  * synchronous non-abortable locks can be substituted for spinlocks.
114  */
115 typedef mtx_t                           hammer2_mtx_t;
116 typedef mtx_link_t                      hammer2_mtx_link_t;
117 typedef mtx_state_t                     hammer2_mtx_state_t;
118
119 typedef struct spinlock                 hammer2_spin_t;
120
121 #define hammer2_mtx_ex                  mtx_lock_ex_quick
122 #define hammer2_mtx_sh                  mtx_lock_sh_quick
123 #define hammer2_mtx_unlock              mtx_unlock
124 #define hammer2_mtx_owned               mtx_owned
125 #define hammer2_mtx_init                mtx_init
126 #define hammer2_mtx_temp_release        mtx_lock_temp_release
127 #define hammer2_mtx_temp_restore        mtx_lock_temp_restore
128 #define hammer2_mtx_refs                mtx_lockrefs
129
130 #define hammer2_spin_init               spin_init
131 #define hammer2_spin_sh                 spin_lock_shared
132 #define hammer2_spin_ex                 spin_lock
133 #define hammer2_spin_unsh               spin_unlock_shared
134 #define hammer2_spin_unex               spin_unlock
135
136 /*
137  * General lock support
138  */
139 static __inline
140 int
141 hammer2_mtx_upgrade(hammer2_mtx_t *mtx)
142 {
143         int wasexclusive;
144
145         if (mtx_islocked_ex(mtx)) {
146                 wasexclusive = 1;
147         } else {
148                 mtx_unlock(mtx);
149                 mtx_lock_ex_quick(mtx);
150                 wasexclusive = 0;
151         }
152         return wasexclusive;
153 }
154
155 /*
156  * Downgrade an inode lock from exclusive to shared only if the inode
157  * lock was previously shared.  If the inode lock was previously exclusive,
158  * this is a NOP.
159  */
160 static __inline
161 void
162 hammer2_mtx_downgrade(hammer2_mtx_t *mtx, int wasexclusive)
163 {
164         if (wasexclusive == 0)
165                 mtx_downgrade(mtx);
166 }
167
168 /*
169  * The xid tracks internal transactional updates.
170  *
171  * XXX fix-me, really needs to be 64-bits
172  */
173 typedef uint32_t hammer2_xid_t;
174
175 #define HAMMER2_XID_MIN 0x00000000U
176 #define HAMMER2_XID_MAX 0x7FFFFFFFU
177
178 /*
179  * The chain structure tracks a portion of the media topology from the
180  * root (volume) down.  Chains represent volumes, inodes, indirect blocks,
181  * data blocks, and freemap nodes and leafs.
182  *
183  * The chain structure utilizes a simple singly-homed topology and the
184  * chain's in-memory topology will move around as the chains do, due mainly
185  * to renames and indirect block creation.
186  *
187  * Block Table Updates
188  *
189  *      Block table updates for insertions and updates are delayed until the
190  *      flush.  This allows us to avoid having to modify the parent chain
191  *      all the way to the root.
192  *
193  *      Block table deletions are performed immediately (modifying the parent
194  *      in the process) because the flush code uses the chain structure to
195  *      track delayed updates and the chain will be (likely) gone or moved to
196  *      another location in the topology after a deletion.
197  *
198  *      A prior iteration of the code tried to keep the relationship intact
199  *      on deletes by doing a delete-duplicate operation on the chain, but
200  *      it added way too much complexity to the codebase.
201  *
202  * Flush Synchronization
203  *
204  *      The flush code must flush modified chains bottom-up.  Because chain
205  *      structures can shift around and are NOT topologically stable,
206  *      modified chains are independently indexed for the flush.  As the flush
207  *      runs it modifies (or further modifies) and updates the parents,
208  *      propagating the flush all the way to the volume root.
209  *
210  *      Modifying front-end operations can occur during a flush but will block
211  *      in two cases: (1) when the front-end tries to operate on the inode
212  *      currently in the midst of being flushed and (2) if the front-end
213  *      crosses an inode currently being flushed (such as during a rename).
214  *      So, for example, if you rename directory "x" to "a/b/c/d/e/f/g/x" and
215  *      the flusher is currently working on "a/b/c", the rename will block
216  *      temporarily in order to ensure that "x" exists in one place or the
217  *      other.
218  *
219  *      Meta-data statistics are updated by the flusher.  The front-end will
220  *      make estimates but meta-data must be fully synchronized only during a
221  *      flush in order to ensure that it remains correct across a crash.
222  *
223  *      Multiple flush synchronizations can theoretically be in-flight at the
224  *      same time but the implementation is not coded to handle the case and
225  *      currently serializes them.
226  *
227  * Snapshots:
228  *
229  *      Snapshots currently require the subdirectory tree being snapshotted
230  *      to be flushed.  The snapshot then creates a new super-root inode which
231  *      copies the flushed blockdata of the directory or file that was
232  *      snapshotted.
233  *
234  * RBTREE NOTES:
235  *
236  *      - Note that the radix tree runs in powers of 2 only so sub-trees
237  *        cannot straddle edges.
238  */
239 RB_HEAD(hammer2_chain_tree, hammer2_chain);
240 TAILQ_HEAD(h2_flush_list, hammer2_chain);
241 TAILQ_HEAD(h2_core_list, hammer2_chain);
242 TAILQ_HEAD(h2_iocb_list, hammer2_iocb);
243
244 #define CHAIN_CORE_DELETE_BMAP_ENTRIES  \
245         (HAMMER2_PBUFSIZE / sizeof(hammer2_blockref_t) / sizeof(uint32_t))
246
247 /*
248  * Core topology for chain (embedded in chain).  Protected by a spinlock.
249  */
250 struct hammer2_chain_core {
251         hammer2_spin_t  spin;
252         struct hammer2_chain_tree rbtree; /* sub-chains */
253         int             live_zero;      /* blockref array opt */
254         u_int           live_count;     /* live (not deleted) chains in tree */
255         u_int           chain_count;    /* live + deleted chains under core */
256         int             generation;     /* generation number (inserts only) */
257 };
258
259 typedef struct hammer2_chain_core hammer2_chain_core_t;
260
261 RB_HEAD(hammer2_io_tree, hammer2_io);
262
263 /*
264  * IOCB - IO callback (into chain, cluster, or manual request)
265  */
266 struct hammer2_iocb {
267         TAILQ_ENTRY(hammer2_iocb) entry;
268         void (*callback)(struct hammer2_iocb *iocb);
269         struct hammer2_io       *dio;
270         struct hammer2_cluster  *cluster;
271         struct hammer2_chain    *chain;
272         void                    *ptr;
273         off_t                   lbase;
274         int                     lsize;
275         uint32_t                flags;
276         int                     error;
277 };
278
279 typedef struct hammer2_iocb hammer2_iocb_t;
280
281 #define HAMMER2_IOCB_INTERLOCK  0x00000001
282 #define HAMMER2_IOCB_ONQ        0x00000002
283 #define HAMMER2_IOCB_DONE       0x00000004
284 #define HAMMER2_IOCB_INPROG     0x00000008
285 #define HAMMER2_IOCB_UNUSED10   0x00000010
286 #define HAMMER2_IOCB_QUICK      0x00010000
287 #define HAMMER2_IOCB_ZERO       0x00020000
288 #define HAMMER2_IOCB_READ       0x00040000
289 #define HAMMER2_IOCB_WAKEUP     0x00080000
290
291 /*
292  * DIO - Management structure wrapping system buffer cache.
293  *
294  *       Used for multiple purposes including concurrent management
295  *       if small requests by chains into larger DIOs.
296  */
297 struct hammer2_io {
298         RB_ENTRY(hammer2_io) rbnode;    /* indexed by device offset */
299         struct h2_iocb_list iocbq;
300         struct spinlock spin;
301         struct hammer2_dev *hmp;
302         struct buf      *bp;
303         off_t           pbase;
304         int             psize;
305         int             refs;
306         int             act;                    /* activity */
307 };
308
309 typedef struct hammer2_io hammer2_io_t;
310
311 #define HAMMER2_DIO_INPROG      0x80000000      /* bio in progress */
312 #define HAMMER2_DIO_GOOD        0x40000000      /* dio->bp is stable */
313 #define HAMMER2_DIO_WAITING     0x20000000      /* (old) */
314 #define HAMMER2_DIO_DIRTY       0x10000000      /* flush on last drop */
315
316 #define HAMMER2_DIO_MASK        0x0FFFFFFF
317
318 /*
319  * Primary chain structure keeps track of the topology in-memory.
320  */
321 struct hammer2_chain {
322         hammer2_mtx_t           lock;
323         hammer2_chain_core_t    core;
324         RB_ENTRY(hammer2_chain) rbnode;         /* live chain(s) */
325         hammer2_blockref_t      bref;
326         struct hammer2_chain    *parent;
327         struct hammer2_state    *state;         /* if active cache msg */
328         struct hammer2_dev      *hmp;
329         struct hammer2_pfs      *pmp;           /* A PFS or super-root (spmp) */
330
331         hammer2_xid_t   flush_xid;              /* flush sequencing */
332         hammer2_io_t    *dio;                   /* physical data buffer */
333         u_int           bytes;                  /* physical data size */
334         u_int           flags;
335         u_int           refs;
336         u_int           lockcnt;
337         int             error;                  /* on-lock data error state */
338
339         hammer2_media_data_t *data;             /* data pointer shortcut */
340         TAILQ_ENTRY(hammer2_chain) flush_node;  /* flush list */
341 };
342
343 typedef struct hammer2_chain hammer2_chain_t;
344
345 int hammer2_chain_cmp(hammer2_chain_t *chain1, hammer2_chain_t *chain2);
346 RB_PROTOTYPE(hammer2_chain_tree, hammer2_chain, rbnode, hammer2_chain_cmp);
347
348 /*
349  * Special notes on flags:
350  *
351  * INITIAL      - This flag allows a chain to be created and for storage to
352  *                be allocated without having to immediately instantiate the
353  *                related buffer.  The data is assumed to be all-zeros.  It
354  *                is primarily used for indirect blocks.
355  *
356  * MODIFIED     - The chain's media data has been modified.
357  *
358  * UPDATE       - Chain might not be modified but parent blocktable needs update
359  *
360  * FICTITIOUS   - Faked chain as a placeholder for an error condition.  This
361  *                chain is unsuitable for I/O.
362  *
363  * BMAPPED      - Indicates that the chain is present in the parent blockmap.
364  *
365  * BMAPUPD      - Indicates that the chain is present but needs to be updated
366  *                in the parent blockmap.
367  */
368 #define HAMMER2_CHAIN_MODIFIED          0x00000001      /* dirty chain data */
369 #define HAMMER2_CHAIN_ALLOCATED         0x00000002      /* kmalloc'd chain */
370 #define HAMMER2_CHAIN_DESTROY           0x00000004
371 #define HAMMER2_CHAIN_UNUSED0008        0x00000008
372 #define HAMMER2_CHAIN_DELETED           0x00000010      /* deleted chain */
373 #define HAMMER2_CHAIN_INITIAL           0x00000020      /* initial create */
374 #define HAMMER2_CHAIN_UPDATE            0x00000040      /* need parent update */
375 #define HAMMER2_CHAIN_DEFERRED          0x00000080      /* flush depth defer */
376 #define HAMMER2_CHAIN_IOFLUSH           0x00000100      /* bawrite on put */
377 #define HAMMER2_CHAIN_ONFLUSH           0x00000200      /* on a flush list */
378 #define HAMMER2_CHAIN_FICTITIOUS        0x00000400      /* unsuitable for I/O */
379 #define HAMMER2_CHAIN_VOLUMESYNC        0x00000800      /* needs volume sync */
380 #define HAMMER2_CHAIN_DELAYED           0x00001000      /* delayed flush */
381 #define HAMMER2_CHAIN_COUNTEDBREFS      0x00002000      /* block table stats */
382 #define HAMMER2_CHAIN_ONRBTREE          0x00004000      /* on parent RB tree */
383 #define HAMMER2_CHAIN_UNUSED00008000    0x00008000
384 #define HAMMER2_CHAIN_EMBEDDED          0x00010000      /* embedded data */
385 #define HAMMER2_CHAIN_RELEASE           0x00020000      /* don't keep around */
386 #define HAMMER2_CHAIN_BMAPPED           0x00040000      /* present in blkmap */
387 #define HAMMER2_CHAIN_BMAPUPD           0x00080000      /* +needs updating */
388 #define HAMMER2_CHAIN_IOINPROG          0x00100000      /* I/O interlock */
389 #define HAMMER2_CHAIN_IOSIGNAL          0x00200000      /* I/O interlock */
390 #define HAMMER2_CHAIN_PFSBOUNDARY       0x00400000      /* super->pfs inode */
391
392 #define HAMMER2_CHAIN_FLUSH_MASK        (HAMMER2_CHAIN_MODIFIED |       \
393                                          HAMMER2_CHAIN_UPDATE |         \
394                                          HAMMER2_CHAIN_ONFLUSH)
395
396 /*
397  * Hammer2 error codes, used by chain->error and cluster->error.  The error
398  * code is typically set on-lock unless no I/O was requested, and set on
399  * I/O otherwise.  If set for a cluster it generally means that the cluster
400  * code could not find a valid copy to present.
401  *
402  * IO           - An I/O error occurred
403  * CHECK        - I/O succeeded but did not match the check code
404  * INCOMPLETE   - A cluster is not complete enough to use, or
405  *                a chain cannot be loaded because its parent has an error.
406  *
407  * NOTE: API allows callers to check zero/non-zero to determine if an error
408  *       condition exists.
409  *
410  * NOTE: Chain's data field is usually NULL on an IO error but not necessarily
411  *       NULL on other errors.  Check chain->error, not chain->data.
412  */
413 #define HAMMER2_ERROR_NONE              0
414 #define HAMMER2_ERROR_IO                1       /* device I/O error */
415 #define HAMMER2_ERROR_CHECK             2       /* check code mismatch */
416 #define HAMMER2_ERROR_INCOMPLETE        3       /* incomplete cluster */
417 #define HAMMER2_ERROR_DEPTH             4       /* temporary depth limit */
418
419 /*
420  * Flags passed to hammer2_chain_lookup() and hammer2_chain_next()
421  *
422  * NOTE: MATCHIND allows an indirect block / freemap node to be returned
423  *       when the passed key range matches the radix.  Remember that key_end
424  *       is inclusive (e.g. {0x000,0xFFF}, not {0x000,0x1000}).
425  *
426  * NOTE: NODIRECT prevents a lookup of offset 0 in an inode from returning
427  *       the inode itself if the inode is in DIRECTDATA mode (i.e. file is
428  *       <= 512 bytes).
429  */
430 #define HAMMER2_LOOKUP_NOLOCK           0x00000001      /* ref only */
431 #define HAMMER2_LOOKUP_NODATA           0x00000002      /* data left NULL */
432 #define HAMMER2_LOOKUP_NODIRECT         0x00000004      /* no offset=0 DD */
433 #define HAMMER2_LOOKUP_SHARED           0x00000100
434 #define HAMMER2_LOOKUP_MATCHIND         0x00000200      /* return all chains */
435 #define HAMMER2_LOOKUP_ALLNODES         0x00000400      /* allow NULL focus */
436 #define HAMMER2_LOOKUP_ALWAYS           0x00000800      /* resolve data */
437
438 /*
439  * Flags passed to hammer2_chain_modify() and hammer2_chain_resize()
440  *
441  * NOTE: OPTDATA allows us to avoid instantiating buffers for INDIRECT
442  *       blocks in the INITIAL-create state.
443  */
444 #define HAMMER2_MODIFY_OPTDATA          0x00000002      /* data can be NULL */
445 #define HAMMER2_MODIFY_NO_MODIFY_TID    0x00000004
446 #define HAMMER2_MODIFY_UNUSED0008       0x00000008
447 #define HAMMER2_MODIFY_NOREALLOC        0x00000010
448
449 /*
450  * Flags passed to hammer2_chain_lock()
451  *
452  * NOTE: RDONLY is set to optimize cluster operations when *no* modifications
453  *       will be made to either the cluster being locked or any underlying
454  *       cluster.  It allows the cluster to lock and access data for a subset
455  *       of available nodes instead of all available nodes.
456  */
457 #define HAMMER2_RESOLVE_NEVER           1
458 #define HAMMER2_RESOLVE_MAYBE           2
459 #define HAMMER2_RESOLVE_ALWAYS          3
460 #define HAMMER2_RESOLVE_MASK            0x0F
461
462 #define HAMMER2_RESOLVE_SHARED          0x10    /* request shared lock */
463 #define HAMMER2_RESOLVE_UNUSED20        0x20
464 #define HAMMER2_RESOLVE_RDONLY          0x40    /* higher level op flag */
465
466 /*
467  * Flags passed to hammer2_chain_delete()
468  */
469 #define HAMMER2_DELETE_PERMANENT        0x0001
470 #define HAMMER2_DELETE_NOSTATS          0x0002
471
472 #define HAMMER2_INSERT_NOSTATS          0x0002
473 #define HAMMER2_INSERT_PFSROOT          0x0004
474
475 /*
476  * Flags passed to hammer2_chain_delete_duplicate()
477  */
478 #define HAMMER2_DELDUP_RECORE           0x0001
479
480 /*
481  * Cluster different types of storage together for allocations
482  */
483 #define HAMMER2_FREECACHE_INODE         0
484 #define HAMMER2_FREECACHE_INDIR         1
485 #define HAMMER2_FREECACHE_DATA          2
486 #define HAMMER2_FREECACHE_UNUSED3       3
487 #define HAMMER2_FREECACHE_TYPES         4
488
489 /*
490  * hammer2_freemap_alloc() block preference
491  */
492 #define HAMMER2_OFF_NOPREF              ((hammer2_off_t)-1)
493
494 /*
495  * BMAP read-ahead maximum parameters
496  */
497 #define HAMMER2_BMAP_COUNT              16      /* max bmap read-ahead */
498 #define HAMMER2_BMAP_BYTES              (HAMMER2_PBUFSIZE * HAMMER2_BMAP_COUNT)
499
500 /*
501  * hammer2_freemap_adjust()
502  */
503 #define HAMMER2_FREEMAP_DORECOVER       1
504 #define HAMMER2_FREEMAP_DOMAYFREE       2
505 #define HAMMER2_FREEMAP_DOREALFREE      3
506
507 /*
508  * HAMMER2 cluster - A set of chains representing the same entity.
509  *
510  * hammer2_cluster typically represents a temporary set of representitive
511  * chains.  The one exception is that a hammer2_cluster is embedded in
512  * hammer2_inode.  This embedded cluster is ONLY used to track the
513  * representitive chains and cannot be directly locked.
514  *
515  * A cluster is usually temporary (and thus per-thread) for locking purposes,
516  * allowing us to embed the asynchronous storage required for cluster
517  * operations in the cluster itself and adjust the state and status without
518  * having to worry too much about SMP issues.
519  *
520  * The exception is the cluster embedded in the hammer2_inode structure.
521  * This is used to cache the cluster state on an inode-by-inode basis.
522  * Individual hammer2_chain structures not incorporated into clusters might
523  * also stick around to cache miscellanious elements.
524  *
525  * Because the cluster is a 'working copy' and is usually subject to cluster
526  * quorum rules, it is quite possible for us to end up with an insufficient
527  * number of live chains to execute an operation.  If an insufficient number
528  * of chains remain in a working copy, the operation may have to be
529  * downgraded, retried, stall until the requisit number of chains are
530  * available, or possibly even error out depending on the mount type.
531  *
532  * A cluster's focus is set when it is locked.  The focus can only be set
533  * to a chain still part of the synchronized set.
534  */
535 #define HAMMER2_MAXCLUSTER      8
536
537 struct hammer2_cluster_item {
538 #if 0
539         hammer2_mtx_link_t      async_link;
540 #endif
541         hammer2_chain_t         *chain;
542 #if 0
543         struct hammer2_cluster  *cluster;       /* link back to cluster */
544 #endif
545         int                     cache_index;
546         uint32_t                flags;
547 };
548
549 typedef struct hammer2_cluster_item hammer2_cluster_item_t;
550
551 /*
552  * INVALID      - Invalid for focus, i.e. not part of synchronized set.
553  *                Once set, this bit is sticky across operations.
554  *
555  * FEMOD        - Indicates that front-end modifying operations can
556  *                mess with this entry and MODSYNC will copy also
557  *                effect it.
558  */
559 #define HAMMER2_CITEM_INVALID   0x00000001
560 #define HAMMER2_CITEM_FEMOD     0x00000002
561
562 struct hammer2_cluster {
563         int                     refs;           /* track for deallocation */
564         int                     ddflag;
565         struct hammer2_pfs      *pmp;
566         uint32_t                flags;
567         int                     nchains;
568         int                     error;          /* error code valid on lock */
569         int                     focus_index;
570         hammer2_iocb_t          iocb;
571         hammer2_chain_t         *focus;         /* current focus (or mod) */
572         hammer2_cluster_item_t  array[HAMMER2_MAXCLUSTER];
573 };
574
575 typedef struct hammer2_cluster  hammer2_cluster_t;
576
577 /*
578  * WRHARD       - Hard mounts can write fully synchronized
579  * RDHARD       - Hard mounts can read fully synchronized
580  * UNHARD       - Unsynchronized masters present
581  * NOHARD       - No masters visible
582  * WRSOFT       - Soft mounts can write to at least the SOFT_MASTER
583  * RDSOFT       - Soft mounts can read from at least a SOFT_SLAVE
584  * UNSOFT       - Unsynchronized slaves present
585  * NOSOFT       - No slaves visible
586  * RDSLAVE      - slaves are accessible (possibly unsynchronized or remote).
587  * MSYNCED      - All masters are fully synchronized
588  * SSYNCED      - All known local slaves are fully synchronized to masters
589  *
590  * All available masters are always incorporated.  All PFSs belonging to a
591  * cluster (master, slave, copy, whatever) always try to synchronize the
592  * total number of known masters in the PFSs root inode.
593  *
594  * A cluster might have access to many slaves, copies, or caches, but we
595  * have a limited number of cluster slots.  Any such elements which are
596  * directly mounted from block device(s) will always be incorporated.   Note
597  * that SSYNCED only applies to such elements which are directly mounted,
598  * not to any remote slaves, copies, or caches that could be available.  These
599  * bits are used to monitor and drive our synchronization threads.
600  *
601  * When asking the question 'is any data accessible at all', then a simple
602  * test against (RDHARD|RDSOFT|RDSLAVE) gives you the answer.  If any of
603  * these bits are set the object can be read with certain caveats:
604  * RDHARD - no caveats.  RDSOFT - authoritative but might not be synchronized.
605  * and RDSLAVE - not authoritative, has some data but it could be old or
606  * incomplete.
607  *
608  * When both soft and hard mounts are available, data will be read and written
609  * via the soft mount only.  But all might be in the cluster because
610  * background synchronization threads still need to do their work.
611  */
612 #define HAMMER2_CLUSTER_INODE   0x00000001      /* embedded in inode struct */
613 #define HAMMER2_CLUSTER_UNUSED2 0x00000002
614 #define HAMMER2_CLUSTER_LOCKED  0x00000004      /* cluster lks not recursive */
615 #define HAMMER2_CLUSTER_WRHARD  0x00000100      /* hard-mount can write */
616 #define HAMMER2_CLUSTER_RDHARD  0x00000200      /* hard-mount can read */
617 #define HAMMER2_CLUSTER_UNHARD  0x00000400      /* unsynchronized masters */
618 #define HAMMER2_CLUSTER_NOHARD  0x00000800      /* no masters visible */
619 #define HAMMER2_CLUSTER_WRSOFT  0x00001000      /* soft-mount can write */
620 #define HAMMER2_CLUSTER_RDSOFT  0x00002000      /* soft-mount can read */
621 #define HAMMER2_CLUSTER_UNSOFT  0x00004000      /* unsynchronized slaves */
622 #define HAMMER2_CLUSTER_NOSOFT  0x00008000      /* no slaves visible */
623 #define HAMMER2_CLUSTER_MSYNCED 0x00010000      /* all masters synchronized */
624 #define HAMMER2_CLUSTER_SSYNCED 0x00020000      /* known slaves synchronized */
625
626 #define HAMMER2_CLUSTER_ANYDATA ( HAMMER2_CLUSTER_RDHARD |      \
627                                   HAMMER2_CLUSTER_RDSOFT |      \
628                                   HAMMER2_CLUSTER_RDSLAVE)
629
630 #define HAMMER2_CLUSTER_RDOK    ( HAMMER2_CLUSTER_RDHARD |      \
631                                   HAMMER2_CLUSTER_RDSOFT)
632
633 #define HAMMER2_CLUSTER_WROK    ( HAMMER2_CLUSTER_WRHARD |      \
634                                   HAMMER2_CLUSTER_WRSOFT)
635
636 #define HAMMER2_CLUSTER_ZFLAGS  ( HAMMER2_CLUSTER_WRHARD |      \
637                                   HAMMER2_CLUSTER_RDHARD |      \
638                                   HAMMER2_CLUSTER_WRSOFT |      \
639                                   HAMMER2_CLUSTER_RDSOFT |      \
640                                   HAMMER2_CLUSTER_MSYNCED |     \
641                                   HAMMER2_CLUSTER_SSYNCED)
642
643 /*
644  * Helper functions (cluster must be locked for flags to be valid).
645  */
646 static __inline
647 int
648 hammer2_cluster_rdok(hammer2_cluster_t *cluster)
649 {
650         return (cluster->flags & HAMMER2_CLUSTER_RDOK);
651 }
652
653 static __inline
654 int
655 hammer2_cluster_wrok(hammer2_cluster_t *cluster)
656 {
657         return (cluster->flags & HAMMER2_CLUSTER_WROK);
658 }
659
660 RB_HEAD(hammer2_inode_tree, hammer2_inode);
661
662 /*
663  * A hammer2 inode.
664  *
665  * NOTE: The inode-embedded cluster is never used directly for I/O (since
666  *       it may be shared).  Instead it will be replicated-in and synchronized
667  *       back out if changed.
668  */
669 struct hammer2_inode {
670         RB_ENTRY(hammer2_inode) rbnode;         /* inumber lookup (HL) */
671         hammer2_mtx_t           lock;           /* inode lock */
672         struct hammer2_pfs      *pmp;           /* PFS mount */
673         struct hammer2_inode    *pip;           /* parent inode */
674         struct vnode            *vp;
675         struct spinlock         cluster_spin;   /* update cluster */
676         hammer2_cluster_t       cluster;
677         struct lockf            advlock;
678         u_int                   flags;
679         u_int                   refs;           /* +vpref, +flushref */
680         uint8_t                 comp_heuristic;
681         hammer2_inode_meta_t    meta;           /* copy of meta-data */
682 };
683
684 typedef struct hammer2_inode hammer2_inode_t;
685
686 #define HAMMER2_INODE_MODIFIED          0x0001
687 #define HAMMER2_INODE_SROOT             0x0002  /* kmalloc special case */
688 #define HAMMER2_INODE_RENAME_INPROG     0x0004
689 #define HAMMER2_INODE_ONRBTREE          0x0008
690 #define HAMMER2_INODE_RESIZED           0x0010
691 #define HAMMER2_INODE_MTIME             0x0020
692 #define HAMMER2_INODE_ISUNLINKED        0x0040
693 #define HAMMER2_INODE_METAGOOD          0x0080  /* inode meta-data good */
694
695 int hammer2_inode_cmp(hammer2_inode_t *ip1, hammer2_inode_t *ip2);
696 RB_PROTOTYPE2(hammer2_inode_tree, hammer2_inode, rbnode, hammer2_inode_cmp,
697                 hammer2_tid_t);
698
699 /*
700  * inode-unlink side-structure
701  */
702 struct hammer2_inode_unlink {
703         TAILQ_ENTRY(hammer2_inode_unlink) entry;
704         hammer2_inode_t *ip;
705 };
706 TAILQ_HEAD(h2_unlk_list, hammer2_inode_unlink);
707
708 typedef struct hammer2_inode_unlink hammer2_inode_unlink_t;
709
710 /*
711  * A hammer2 transaction and flush sequencing structure.
712  *
713  * This global structure is tied into hammer2_dev and is used
714  * to sequence modifying operations and flushes.  These operations
715  * run on whole cluster PFSs, not individual nodes (at this level),
716  * so we do not record mirror_tid here.
717  */
718 struct hammer2_trans {
719         TAILQ_ENTRY(hammer2_trans) entry;
720         struct hammer2_pfs      *pmp;
721         hammer2_xid_t           sync_xid;       /* transaction sequencer */
722         hammer2_tid_t           inode_tid;      /* inode number assignment */
723         hammer2_tid_t           modify_tid;     /* modify transaction id */
724         thread_t                td;             /* pointer */
725         int                     flags;
726         int                     blocked;
727         uint8_t                 inodes_created;
728         uint8_t                 dummy[7];
729 };
730
731 typedef struct hammer2_trans hammer2_trans_t;
732
733 #define HAMMER2_TRANS_ISFLUSH           0x0001  /* formal flush */
734 #define HAMMER2_TRANS_CONCURRENT        0x0002  /* concurrent w/flush */
735 #define HAMMER2_TRANS_BUFCACHE          0x0004  /* from bioq strategy write */
736 #define HAMMER2_TRANS_NEWINODE          0x0008  /* caller allocating inode */
737 #define HAMMER2_TRANS_KEEPMODIFY        0x0010  /* do not change bref.modify */
738 #define HAMMER2_TRANS_PREFLUSH          0x0020  /* preflush state */
739
740 #define HAMMER2_FREEMAP_HEUR_NRADIX     4       /* pwr 2 PBUFRADIX-MINIORADIX */
741 #define HAMMER2_FREEMAP_HEUR_TYPES      8
742 #define HAMMER2_FREEMAP_HEUR            (HAMMER2_FREEMAP_HEUR_NRADIX * \
743                                          HAMMER2_FREEMAP_HEUR_TYPES)
744
745 /*
746  * Transaction Rendezvous
747  */
748 TAILQ_HEAD(hammer2_trans_queue, hammer2_trans);
749
750 struct hammer2_trans_manage {
751         hammer2_xid_t           flush_xid;      /* last flush transaction */
752         hammer2_xid_t           alloc_xid;
753         struct lock             translk;        /* lockmgr lock */
754         struct hammer2_trans_queue transq;      /* modifying transactions */
755         int                     flushcnt;       /* track flush trans */
756 };
757
758 typedef struct hammer2_trans_manage hammer2_trans_manage_t;
759
760 /*
761  * hammer2_vop_info - container for VOP operation.
762  *
763  * This structure is used to distribute a VOP operation across multiple
764  * nodes.  It provides a rendezvous for concurrent node execution and
765  * can be detached from the frontend operation to allow the frontend to
766  * return early.
767  */
768 struct hammer2_vop_info {
769         hammer2_inode_t *dip;
770         hammer2_inode_t *ip;
771         struct uio      *uio;
772         int             clidx;
773         void            (*xio_func)(struct hammer2_syncthr *thr,
774                                     struct hammer2_vop_info *info);
775 };
776
777 typedef struct hammer2_vop_info hammer2_vop_info_t;
778
779 /*
780  * Cluster node synchronization and operation thread element.
781  *
782  * Multiple syncthr's can hang off of a hammer2_pfs structure, typically one
783  * for each block device that is part of the PFS.  Synchronization threads
784  * for PFSs accessed over the network are handled by their respective hosts.
785  *
786  * Synchronization threads are responsible for keeping a local node
787  * synchronized to the greater cluster.
788  *
789  * A syncthr can also hang off each hammer2_dev's super-root PFS (spmp).
790  * This thread is responsible for automatic bulkfree and dedup scans.
791  */
792 struct hammer2_syncthr {
793         struct hammer2_pfs *pmp;
794         thread_t        td;
795         uint32_t        flags;
796         int             depth;
797         int             clindex;        /* sync_thrs[] array index */
798         hammer2_trans_t trans;
799         struct lock     lk;
800 };
801
802 typedef struct hammer2_syncthr hammer2_syncthr_t;
803
804 #define HAMMER2_SYNCTHR_UNMOUNTING      0x0001  /* unmount request */
805 #define HAMMER2_SYNCTHR_DEV             0x0002  /* related to dev, not pfs */
806 #define HAMMER2_SYNCTHR_UNUSED04        0x0004
807 #define HAMMER2_SYNCTHR_REMASTER        0x0008  /* remaster request */
808 #define HAMMER2_SYNCTHR_STOP            0x0010  /* exit request */
809 #define HAMMER2_SYNCTHR_FREEZE          0x0020  /* force idle */
810 #define HAMMER2_SYNCTHR_FROZEN          0x0040  /* restart */
811
812
813 /*
814  * Global (per partition) management structure, represents a hard block
815  * device.  Typically referenced by hammer2_chain structures when applicable.
816  * Typically not used for network-managed elements.
817  *
818  * Note that a single hammer2_dev can be indirectly tied to multiple system
819  * mount points.  There is no direct relationship.  System mounts are
820  * per-cluster-id, not per-block-device, and a single hard mount might contain
821  * many PFSs and those PFSs might combine together in various ways to form
822  * the set of available clusters.
823  */
824 struct hammer2_dev {
825         struct vnode    *devvp;         /* device vnode */
826         int             ronly;          /* read-only mount */
827         int             mount_count;    /* number of actively mounted PFSs */
828         TAILQ_ENTRY(hammer2_dev) mntentry; /* hammer2_mntlist */
829
830         struct malloc_type *mchain;
831         int             nipstacks;
832         int             maxipstacks;
833         kdmsg_iocom_t   iocom;          /* volume-level dmsg interface */
834         struct spinlock io_spin;        /* iotree access */
835         struct hammer2_io_tree iotree;
836         int             iofree_count;
837         hammer2_chain_t vchain;         /* anchor chain (topology) */
838         hammer2_chain_t fchain;         /* anchor chain (freemap) */
839         struct spinlock list_spin;
840         struct h2_flush_list    flushq; /* flush seeds */
841         struct hammer2_pfs *spmp;       /* super-root pmp for transactions */
842         struct lock     vollk;          /* lockmgr lock */
843         hammer2_off_t   heur_freemap[HAMMER2_FREEMAP_HEUR];
844         int             volhdrno;       /* last volhdrno written */
845         char            devrepname[64]; /* for kprintf */
846         hammer2_volume_data_t voldata;
847         hammer2_volume_data_t volsync;  /* synchronized voldata */
848 };
849
850 typedef struct hammer2_dev hammer2_dev_t;
851
852 /*
853  * Helper functions (cluster must be locked for flags to be valid).
854  */
855 static __inline
856 int
857 hammer2_chain_rdok(hammer2_chain_t *chain)
858 {
859         return (chain->error == 0);
860 }
861
862 static __inline
863 int
864 hammer2_chain_wrok(hammer2_chain_t *chain)
865 {
866         return (chain->error == 0 && chain->hmp->ronly == 0);
867 }
868
869 /*
870  * Per-cluster management structure.  This structure will be tied to a
871  * system mount point if the system is mounting the PFS, but is also used
872  * to manage clusters encountered during the super-root scan or received
873  * via LNK_SPANs that might not be mounted.
874  *
875  * This structure is also used to represent the super-root that hangs off
876  * of a hard mount point.  The super-root is not really a cluster element.
877  * In this case the spmp_hmp field will be non-NULL.  It's just easier to do
878  * this than to special case super-root manipulation in the hammer2_chain*
879  * code as being only hammer2_dev-related.
880  *
881  * pfs_mode and pfs_nmasters are rollup fields which critically describes
882  * how elements of the cluster act on the cluster.  pfs_mode is only applicable
883  * when a PFS is mounted by the system.  pfs_nmasters is our best guess as to
884  * how many masters have been configured for a cluster and is always
885  * applicable.  pfs_types[] is an array with 1:1 correspondance to the
886  * iroot cluster and describes the PFS types of the nodes making up the
887  * cluster.
888  *
889  * WARNING! Portions of this structure have deferred initialization.  In
890  *          particular, if not mounted there will be no ihidden or wthread.
891  *          umounted network PFSs will also be missing iroot and numerous
892  *          other fields will not be initialized prior to mount.
893  *
894  *          Synchronization threads are chain-specific and only applicable
895  *          to local hard PFS entries.  A hammer2_pfs structure may contain
896  *          more than one when multiple hard PFSs are present on the local
897  *          machine which require synchronization monitoring.  Most PFSs
898  *          (such as snapshots) are 1xMASTER PFSs which do not need a
899  *          synchronization thread.
900  *
901  * WARNING! The chains making up pfs->iroot's cluster are accounted for in
902  *          hammer2_dev->mount_count when the pfs is associated with a mount
903  *          point.
904  */
905 struct hammer2_pfs {
906         struct mount            *mp;
907         TAILQ_ENTRY(hammer2_pfs) mntentry;      /* hammer2_pfslist */
908         uuid_t                  pfs_clid;
909         hammer2_dev_t           *spmp_hmp;      /* only if super-root pmp */
910         hammer2_inode_t         *iroot;         /* PFS root inode */
911         hammer2_inode_t         *ihidden;       /* PFS hidden directory */
912         uint8_t                 pfs_types[HAMMER2_MAXCLUSTER];
913         char                    *pfs_names[HAMMER2_MAXCLUSTER];
914         hammer2_trans_manage_t  tmanage;        /* transaction management */
915         struct lock             lock;           /* PFS lock for certain ops */
916         struct netexport        export;         /* nfs export */
917         int                     ronly;          /* read-only mount */
918         struct malloc_type      *minode;
919         struct malloc_type      *mmsg;
920         struct spinlock         inum_spin;      /* inumber lookup */
921         struct hammer2_inode_tree inum_tree;    /* (not applicable to spmp) */
922         hammer2_tid_t           modify_tid;     /* modify transaction id */
923         hammer2_tid_t           inode_tid;      /* inode allocator */
924         uint8_t                 pfs_nmasters;   /* total masters */
925         uint8_t                 pfs_mode;       /* operating mode PFSMODE */
926         uint8_t                 unused01;
927         uint8_t                 unused02;
928         uint32_t                unused03;
929         long                    inmem_inodes;
930         uint32_t                inmem_dirty_chains;
931         int                     count_lwinprog; /* logical write in prog */
932         struct spinlock         list_spin;
933         struct h2_unlk_list     unlinkq;        /* last-close unlink */
934         hammer2_syncthr_t       sync_thrs[HAMMER2_MAXCLUSTER];
935         thread_t                wthread_td;     /* write thread td */
936         struct bio_queue_head   wthread_bioq;   /* logical buffer bioq */
937         hammer2_mtx_t           wthread_mtx;    /* interlock */
938         int                     wthread_destroy;/* termination sequencing */
939         uint32_t                flags;          /* cached cluster flags */
940 };
941
942 typedef struct hammer2_pfs hammer2_pfs_t;
943
944 #define HAMMER2_DIRTYCHAIN_WAITING      0x80000000
945 #define HAMMER2_DIRTYCHAIN_MASK         0x7FFFFFFF
946
947 #define HAMMER2_LWINPROG_WAITING        0x80000000
948 #define HAMMER2_LWINPROG_MASK           0x7FFFFFFF
949
950 /*
951  * Bulkscan
952  */
953 #define HAMMER2_BULK_ABORT      0x00000001
954
955 /*
956  * Misc
957  */
958 #if defined(_KERNEL)
959
960 MALLOC_DECLARE(M_HAMMER2);
961
962 #define VTOI(vp)        ((hammer2_inode_t *)(vp)->v_data)
963 #define ITOV(ip)        ((ip)->vp)
964
965 /*
966  * Currently locked chains retain the locked buffer cache buffer for
967  * indirect blocks, and indirect blocks can be one of two sizes.  The
968  * device buffer has to match the case to avoid deadlocking recursive
969  * chains that might otherwise try to access different offsets within
970  * the same device buffer.
971  */
972 static __inline
973 int
974 hammer2_devblkradix(int radix)
975 {
976 #if 0
977         if (radix <= HAMMER2_LBUFRADIX) {
978                 return (HAMMER2_LBUFRADIX);
979         } else {
980                 return (HAMMER2_PBUFRADIX);
981         }
982 #endif
983         return (HAMMER2_PBUFRADIX);
984 }
985
986 /*
987  * XXX almost time to remove this.  DIO uses PBUFSIZE exclusively now.
988  */
989 static __inline
990 size_t
991 hammer2_devblksize(size_t bytes)
992 {
993 #if 0
994         if (bytes <= HAMMER2_LBUFSIZE) {
995                 return(HAMMER2_LBUFSIZE);
996         } else {
997                 KKASSERT(bytes <= HAMMER2_PBUFSIZE &&
998                          (bytes ^ (bytes - 1)) == ((bytes << 1) - 1));
999                 return (HAMMER2_PBUFSIZE);
1000         }
1001 #endif
1002         return (HAMMER2_PBUFSIZE);
1003 }
1004
1005
1006 static __inline
1007 hammer2_pfs_t *
1008 MPTOPMP(struct mount *mp)
1009 {
1010         return ((hammer2_pfs_t *)mp->mnt_data);
1011 }
1012
1013 #define LOCKSTART       int __nlocks = curthread->td_locks
1014 #define LOCKENTER       (++curthread->td_locks)
1015 #define LOCKEXIT        (--curthread->td_locks)
1016 #define LOCKSTOP        KKASSERT(curthread->td_locks == __nlocks)
1017
1018 extern struct vop_ops hammer2_vnode_vops;
1019 extern struct vop_ops hammer2_spec_vops;
1020 extern struct vop_ops hammer2_fifo_vops;
1021
1022 extern int hammer2_debug;
1023 extern int hammer2_cluster_enable;
1024 extern int hammer2_hardlink_enable;
1025 extern int hammer2_flush_pipe;
1026 extern int hammer2_synchronous_flush;
1027 extern int hammer2_dio_count;
1028 extern long hammer2_limit_dirty_chains;
1029 extern long hammer2_iod_file_read;
1030 extern long hammer2_iod_meta_read;
1031 extern long hammer2_iod_indr_read;
1032 extern long hammer2_iod_fmap_read;
1033 extern long hammer2_iod_volu_read;
1034 extern long hammer2_iod_file_write;
1035 extern long hammer2_iod_meta_write;
1036 extern long hammer2_iod_indr_write;
1037 extern long hammer2_iod_fmap_write;
1038 extern long hammer2_iod_volu_write;
1039 extern long hammer2_ioa_file_read;
1040 extern long hammer2_ioa_meta_read;
1041 extern long hammer2_ioa_indr_read;
1042 extern long hammer2_ioa_fmap_read;
1043 extern long hammer2_ioa_volu_read;
1044 extern long hammer2_ioa_file_write;
1045 extern long hammer2_ioa_meta_write;
1046 extern long hammer2_ioa_indr_write;
1047 extern long hammer2_ioa_fmap_write;
1048 extern long hammer2_ioa_volu_write;
1049
1050 extern struct objcache *cache_buffer_read;
1051 extern struct objcache *cache_buffer_write;
1052 extern struct objcache *cache_vop_info;
1053
1054 extern int destroy;
1055 extern int write_thread_wakeup;
1056
1057 /*
1058  * hammer2_subr.c
1059  */
1060 #define hammer2_icrc32(buf, size)       iscsi_crc32((buf), (size))
1061 #define hammer2_icrc32c(buf, size, crc) iscsi_crc32_ext((buf), (size), (crc))
1062
1063 int hammer2_signal_check(time_t *timep);
1064 const char *hammer2_error_str(int error);
1065
1066 hammer2_cluster_t *hammer2_inode_lock(hammer2_inode_t *ip, int how);
1067 void hammer2_inode_unlock(hammer2_inode_t *ip, hammer2_cluster_t *cluster);
1068 hammer2_mtx_state_t hammer2_inode_lock_temp_release(hammer2_inode_t *ip);
1069 void hammer2_inode_lock_temp_restore(hammer2_inode_t *ip,
1070                         hammer2_mtx_state_t ostate);
1071 int hammer2_inode_lock_upgrade(hammer2_inode_t *ip);
1072 void hammer2_inode_lock_downgrade(hammer2_inode_t *ip, int);
1073
1074 void hammer2_dev_exlock(hammer2_dev_t *hmp);
1075 void hammer2_dev_shlock(hammer2_dev_t *hmp);
1076 void hammer2_dev_unlock(hammer2_dev_t *hmp);
1077
1078 int hammer2_get_dtype(const hammer2_inode_data_t *ipdata);
1079 int hammer2_get_vtype(const hammer2_inode_data_t *ipdata);
1080 u_int8_t hammer2_get_obj_type(enum vtype vtype);
1081 void hammer2_time_to_timespec(u_int64_t xtime, struct timespec *ts);
1082 u_int64_t hammer2_timespec_to_time(const struct timespec *ts);
1083 u_int32_t hammer2_to_unix_xid(const uuid_t *uuid);
1084 void hammer2_guid_to_uuid(uuid_t *uuid, u_int32_t guid);
1085 hammer2_xid_t hammer2_trans_newxid(hammer2_pfs_t *pmp);
1086 void hammer2_trans_manage_init(hammer2_trans_manage_t *tman);
1087
1088 hammer2_key_t hammer2_dirhash(const unsigned char *name, size_t len);
1089 int hammer2_getradix(size_t bytes);
1090
1091 int hammer2_calc_logical(hammer2_inode_t *ip, hammer2_off_t uoff,
1092                         hammer2_key_t *lbasep, hammer2_key_t *leofp);
1093 int hammer2_calc_physical(hammer2_inode_t *ip, hammer2_key_t lbase);
1094 void hammer2_update_time(uint64_t *timep);
1095 void hammer2_adjreadcounter(hammer2_blockref_t *bref, size_t bytes);
1096
1097 /*
1098  * hammer2_inode.c
1099  */
1100 struct vnode *hammer2_igetv(hammer2_inode_t *ip, hammer2_cluster_t *cparent,
1101                         int *errorp);
1102 hammer2_inode_t *hammer2_inode_lookup(hammer2_pfs_t *pmp,
1103                         hammer2_tid_t inum);
1104 hammer2_inode_t *hammer2_inode_get(hammer2_pfs_t *pmp,
1105                         hammer2_inode_t *dip, hammer2_cluster_t *cluster);
1106 void hammer2_inode_free(hammer2_inode_t *ip);
1107 void hammer2_inode_ref(hammer2_inode_t *ip);
1108 void hammer2_inode_drop(hammer2_inode_t *ip);
1109 void hammer2_inode_repoint(hammer2_inode_t *ip, hammer2_inode_t *pip,
1110                         hammer2_cluster_t *cluster);
1111 void hammer2_inode_repoint_one(hammer2_inode_t *ip, hammer2_cluster_t *cluster,
1112                         int idx);
1113
1114 void hammer2_run_unlinkq(hammer2_trans_t *trans, hammer2_pfs_t *pmp);
1115
1116 hammer2_inode_t *hammer2_inode_create(hammer2_trans_t *trans,
1117                         hammer2_inode_t *dip,
1118                         struct vattr *vap, struct ucred *cred,
1119                         const uint8_t *name, size_t name_len,
1120                         hammer2_cluster_t **clusterp,
1121                         int flags, int *errorp);
1122 int hammer2_inode_connect(hammer2_trans_t *trans,
1123                         hammer2_inode_t *ip, hammer2_cluster_t **clusterp,
1124                         int hlink,
1125                         hammer2_inode_t *dip, hammer2_cluster_t *dcluster,
1126                         const uint8_t *name, size_t name_len,
1127                         hammer2_key_t key);
1128 hammer2_inode_t *hammer2_inode_common_parent(hammer2_inode_t *fdip,
1129                         hammer2_inode_t *tdip);
1130 void hammer2_inode_fsync(hammer2_trans_t *trans, hammer2_inode_t *ip,
1131                         hammer2_cluster_t *cparent);
1132 int hammer2_unlink_file(hammer2_trans_t *trans, hammer2_inode_t *dip,
1133                         const uint8_t *name, size_t name_len, int isdir,
1134                         int *hlinkp, struct nchandle *nch, int nlinks);
1135 int hammer2_hardlink_consolidate(hammer2_trans_t *trans,
1136                         hammer2_inode_t *ip, hammer2_cluster_t **clusterp,
1137                         hammer2_inode_t *cdip, hammer2_cluster_t *cdcluster,
1138                         int nlinks);
1139 int hammer2_hardlink_deconsolidate(hammer2_trans_t *trans, hammer2_inode_t *dip,
1140                         hammer2_chain_t **chainp, hammer2_chain_t **ochainp);
1141 int hammer2_hardlink_find(hammer2_inode_t *dip, hammer2_cluster_t **cparentp,
1142                         hammer2_cluster_t **clusterp);
1143 int hammer2_parent_find(hammer2_cluster_t **cparentp,
1144                         hammer2_cluster_t *cluster);
1145 void hammer2_inode_install_hidden(hammer2_pfs_t *pmp);
1146
1147 /*
1148  * hammer2_chain.c
1149  */
1150 void hammer2_voldata_lock(hammer2_dev_t *hmp);
1151 void hammer2_voldata_unlock(hammer2_dev_t *hmp);
1152 void hammer2_voldata_modify(hammer2_dev_t *hmp);
1153 hammer2_chain_t *hammer2_chain_alloc(hammer2_dev_t *hmp,
1154                                 hammer2_pfs_t *pmp,
1155                                 hammer2_trans_t *trans,
1156                                 hammer2_blockref_t *bref);
1157 void hammer2_chain_core_init(hammer2_chain_t *chain);
1158 void hammer2_chain_ref(hammer2_chain_t *chain);
1159 void hammer2_chain_drop(hammer2_chain_t *chain);
1160 void hammer2_chain_lock(hammer2_chain_t *chain, int how);
1161 void hammer2_chain_load_data(hammer2_chain_t *chain);
1162 const hammer2_media_data_t *hammer2_chain_rdata(hammer2_chain_t *chain);
1163 hammer2_media_data_t *hammer2_chain_wdata(hammer2_chain_t *chain);
1164
1165 /*
1166  * hammer2_cluster.c
1167  */
1168 void hammer2_cluster_load_async(hammer2_cluster_t *cluster,
1169                                 void (*callback)(hammer2_iocb_t *iocb),
1170                                 void *ptr);
1171 void hammer2_chain_moved(hammer2_chain_t *chain);
1172 void hammer2_chain_modify(hammer2_trans_t *trans,
1173                                 hammer2_chain_t *chain, int flags);
1174 void hammer2_chain_resize(hammer2_trans_t *trans, hammer2_inode_t *ip,
1175                                 hammer2_chain_t *parent,
1176                                 hammer2_chain_t *chain,
1177                                 int nradix, int flags);
1178 void hammer2_chain_unlock(hammer2_chain_t *chain);
1179 void hammer2_chain_wait(hammer2_chain_t *chain);
1180 hammer2_chain_t *hammer2_chain_get(hammer2_chain_t *parent, int generation,
1181                                 hammer2_blockref_t *bref);
1182 hammer2_chain_t *hammer2_chain_lookup_init(hammer2_chain_t *parent, int flags);
1183 void hammer2_chain_lookup_done(hammer2_chain_t *parent);
1184 hammer2_chain_t *hammer2_chain_lookup(hammer2_chain_t **parentp,
1185                                 hammer2_key_t *key_nextp,
1186                                 hammer2_key_t key_beg, hammer2_key_t key_end,
1187                                 int *cache_indexp, int flags);
1188 hammer2_chain_t *hammer2_chain_next(hammer2_chain_t **parentp,
1189                                 hammer2_chain_t *chain,
1190                                 hammer2_key_t *key_nextp,
1191                                 hammer2_key_t key_beg, hammer2_key_t key_end,
1192                                 int *cache_indexp, int flags);
1193 hammer2_chain_t *hammer2_chain_scan(hammer2_chain_t *parent,
1194                                 hammer2_chain_t *chain,
1195                                 int *cache_indexp, int flags);
1196
1197 int hammer2_chain_create(hammer2_trans_t *trans, hammer2_chain_t **parentp,
1198                                 hammer2_chain_t **chainp,
1199                                 hammer2_pfs_t *pmp,
1200                                 hammer2_key_t key, int keybits,
1201                                 int type, size_t bytes, int flags);
1202 void hammer2_chain_rename(hammer2_trans_t *trans, hammer2_blockref_t *bref,
1203                                 hammer2_chain_t **parentp,
1204                                 hammer2_chain_t *chain, int flags);
1205 int hammer2_chain_snapshot(hammer2_trans_t *trans, hammer2_chain_t **chainp,
1206                                 hammer2_ioc_pfs_t *pfs);
1207 void hammer2_chain_delete(hammer2_trans_t *trans, hammer2_chain_t *parent,
1208                                 hammer2_chain_t *chain, int flags);
1209 void hammer2_chain_delete_duplicate(hammer2_trans_t *trans,
1210                                 hammer2_chain_t **chainp, int flags);
1211 void hammer2_flush(hammer2_trans_t *trans, hammer2_chain_t *chain, int istop);
1212 void hammer2_delayed_flush(hammer2_trans_t *trans, hammer2_chain_t *chain);
1213 void hammer2_chain_commit(hammer2_trans_t *trans, hammer2_chain_t *chain);
1214 void hammer2_chain_setflush(hammer2_trans_t *trans, hammer2_chain_t *chain);
1215 void hammer2_chain_countbrefs(hammer2_chain_t *chain,
1216                                 hammer2_blockref_t *base, int count);
1217
1218 void hammer2_chain_setcheck(hammer2_chain_t *chain, void *bdata);
1219 int hammer2_chain_testcheck(hammer2_chain_t *chain, void *bdata);
1220
1221
1222 void hammer2_pfs_memory_wait(hammer2_pfs_t *pmp);
1223 void hammer2_pfs_memory_inc(hammer2_pfs_t *pmp);
1224 void hammer2_pfs_memory_wakeup(hammer2_pfs_t *pmp);
1225
1226 void hammer2_base_delete(hammer2_trans_t *trans, hammer2_chain_t *chain,
1227                                 hammer2_blockref_t *base, int count,
1228                                 int *cache_indexp, hammer2_chain_t *child);
1229 void hammer2_base_insert(hammer2_trans_t *trans, hammer2_chain_t *chain,
1230                                 hammer2_blockref_t *base, int count,
1231                                 int *cache_indexp, hammer2_chain_t *child);
1232
1233 /*
1234  * hammer2_trans.c
1235  */
1236 void hammer2_trans_init(hammer2_trans_t *trans, hammer2_pfs_t *pmp,
1237                                 int flags);
1238 void hammer2_trans_done(hammer2_trans_t *trans);
1239 void hammer2_trans_assert_strategy(hammer2_pfs_t *pmp);
1240
1241 /*
1242  * hammer2_ioctl.c
1243  */
1244 int hammer2_ioctl(hammer2_inode_t *ip, u_long com, void *data,
1245                                 int fflag, struct ucred *cred);
1246
1247 /*
1248  * hammer2_io.c
1249  */
1250 void hammer2_io_putblk(hammer2_io_t **diop);
1251 void hammer2_io_cleanup(hammer2_dev_t *hmp, struct hammer2_io_tree *tree);
1252 char *hammer2_io_data(hammer2_io_t *dio, off_t lbase);
1253 void hammer2_io_getblk(hammer2_dev_t *hmp, off_t lbase, int lsize,
1254                                 hammer2_iocb_t *iocb);
1255 void hammer2_io_complete(hammer2_iocb_t *iocb);
1256 void hammer2_io_callback(struct bio *bio);
1257 void hammer2_iocb_wait(hammer2_iocb_t *iocb);
1258 int hammer2_io_new(hammer2_dev_t *hmp, off_t lbase, int lsize,
1259                                 hammer2_io_t **diop);
1260 int hammer2_io_newnz(hammer2_dev_t *hmp, off_t lbase, int lsize,
1261                                 hammer2_io_t **diop);
1262 int hammer2_io_newq(hammer2_dev_t *hmp, off_t lbase, int lsize,
1263                                 hammer2_io_t **diop);
1264 int hammer2_io_bread(hammer2_dev_t *hmp, off_t lbase, int lsize,
1265                                 hammer2_io_t **diop);
1266 void hammer2_io_bawrite(hammer2_io_t **diop);
1267 void hammer2_io_bdwrite(hammer2_io_t **diop);
1268 int hammer2_io_bwrite(hammer2_io_t **diop);
1269 int hammer2_io_isdirty(hammer2_io_t *dio);
1270 void hammer2_io_setdirty(hammer2_io_t *dio);
1271 void hammer2_io_setinval(hammer2_io_t *dio, u_int bytes);
1272 void hammer2_io_brelse(hammer2_io_t **diop);
1273 void hammer2_io_bqrelse(hammer2_io_t **diop);
1274
1275 /*
1276  * hammer2_msgops.c
1277  */
1278 int hammer2_msg_dbg_rcvmsg(kdmsg_msg_t *msg);
1279 int hammer2_msg_adhoc_input(kdmsg_msg_t *msg);
1280
1281 /*
1282  * hammer2_vfsops.c
1283  */
1284 void hammer2_clusterctl_wakeup(kdmsg_iocom_t *iocom);
1285 void hammer2_volconf_update(hammer2_dev_t *hmp, int index);
1286 void hammer2_dump_chain(hammer2_chain_t *chain, int tab, int *countp, char pfx);
1287 void hammer2_bioq_sync(hammer2_pfs_t *pmp);
1288 int hammer2_vfs_sync(struct mount *mp, int waitflags);
1289 hammer2_pfs_t *hammer2_pfsalloc(hammer2_cluster_t *cluster,
1290                                 const hammer2_inode_data_t *ripdata,
1291                                 hammer2_tid_t modify_tid);
1292
1293 void hammer2_lwinprog_ref(hammer2_pfs_t *pmp);
1294 void hammer2_lwinprog_drop(hammer2_pfs_t *pmp);
1295 void hammer2_lwinprog_wait(hammer2_pfs_t *pmp);
1296
1297 /*
1298  * hammer2_freemap.c
1299  */
1300 int hammer2_freemap_alloc(hammer2_trans_t *trans, hammer2_chain_t *chain,
1301                                 size_t bytes);
1302 void hammer2_freemap_adjust(hammer2_trans_t *trans, hammer2_dev_t *hmp,
1303                                 hammer2_blockref_t *bref, int how);
1304
1305 /*
1306  * hammer2_cluster.c
1307  */
1308 int hammer2_cluster_need_resize(hammer2_cluster_t *cluster, int bytes);
1309 uint8_t hammer2_cluster_type(hammer2_cluster_t *cluster);
1310 const hammer2_media_data_t *hammer2_cluster_rdata(hammer2_cluster_t *cluster);
1311 hammer2_media_data_t *hammer2_cluster_wdata(hammer2_cluster_t *cluster);
1312 hammer2_cluster_t *hammer2_cluster_from_chain(hammer2_chain_t *chain);
1313 int hammer2_cluster_modified(hammer2_cluster_t *cluster);
1314 int hammer2_cluster_duplicated(hammer2_cluster_t *cluster);
1315 void hammer2_cluster_bref(hammer2_cluster_t *cluster, hammer2_blockref_t *bref);
1316 void hammer2_cluster_setflush(hammer2_trans_t *trans,
1317                         hammer2_cluster_t *cluster);
1318 void hammer2_cluster_setmethod_check(hammer2_trans_t *trans,
1319                         hammer2_cluster_t *cluster, int check_algo);
1320 hammer2_cluster_t *hammer2_cluster_alloc(hammer2_pfs_t *pmp,
1321                         hammer2_trans_t *trans,
1322                         hammer2_blockref_t *bref);
1323 void hammer2_cluster_ref(hammer2_cluster_t *cluster);
1324 void hammer2_cluster_drop(hammer2_cluster_t *cluster);
1325 void hammer2_cluster_wait(hammer2_cluster_t *cluster);
1326 void hammer2_cluster_lock(hammer2_cluster_t *cluster, int how);
1327 void hammer2_cluster_lock_except(hammer2_cluster_t *cluster, int idx, int how);
1328 void hammer2_cluster_resolve(hammer2_cluster_t *cluster);
1329 void hammer2_cluster_forcegood(hammer2_cluster_t *cluster);
1330 hammer2_cluster_t *hammer2_cluster_copy(hammer2_cluster_t *ocluster);
1331 void hammer2_cluster_unlock(hammer2_cluster_t *cluster);
1332 void hammer2_cluster_unlock_except(hammer2_cluster_t *cluster, int idx);
1333 void hammer2_cluster_resize(hammer2_trans_t *trans, hammer2_inode_t *ip,
1334                         hammer2_cluster_t *cparent, hammer2_cluster_t *cluster,
1335                         int nradix, int flags);
1336 hammer2_inode_data_t *hammer2_cluster_modify_ip(hammer2_trans_t *trans,
1337                         hammer2_inode_t *ip, hammer2_cluster_t *cluster,
1338                         int flags);
1339 void hammer2_cluster_modify(hammer2_trans_t *trans, hammer2_cluster_t *cluster,
1340                         int flags);
1341 void hammer2_cluster_modsync(hammer2_cluster_t *cluster);
1342 hammer2_cluster_t *hammer2_cluster_lookup_init(hammer2_cluster_t *cparent,
1343                         int flags);
1344 void hammer2_cluster_lookup_done(hammer2_cluster_t *cparent);
1345 hammer2_cluster_t *hammer2_cluster_lookup(hammer2_cluster_t *cparent,
1346                         hammer2_key_t *key_nextp,
1347                         hammer2_key_t key_beg, hammer2_key_t key_end,
1348                         int flags);
1349 hammer2_cluster_t *hammer2_cluster_next(hammer2_cluster_t *cparent,
1350                         hammer2_cluster_t *cluster,
1351                         hammer2_key_t *key_nextp,
1352                         hammer2_key_t key_beg, hammer2_key_t key_end,
1353                         int flags);
1354 void hammer2_cluster_next_single_chain(hammer2_cluster_t *cparent,
1355                         hammer2_cluster_t *cluster,
1356                         hammer2_key_t *key_nextp,
1357                         hammer2_key_t key_beg,
1358                         hammer2_key_t key_end,
1359                         int i, int flags);
1360 hammer2_cluster_t *hammer2_cluster_scan(hammer2_cluster_t *cparent,
1361                         hammer2_cluster_t *cluster, int flags);
1362 int hammer2_cluster_create(hammer2_trans_t *trans, hammer2_cluster_t *cparent,
1363                         hammer2_cluster_t **clusterp,
1364                         hammer2_key_t key, int keybits,
1365                         int type, size_t bytes, int flags);
1366 void hammer2_cluster_rename(hammer2_trans_t *trans, hammer2_blockref_t *bref,
1367                         hammer2_cluster_t *cparent, hammer2_cluster_t *cluster,
1368                         int flags);
1369 void hammer2_cluster_delete(hammer2_trans_t *trans, hammer2_cluster_t *pcluster,
1370                         hammer2_cluster_t *cluster, int flags);
1371 int hammer2_cluster_snapshot(hammer2_trans_t *trans,
1372                         hammer2_cluster_t *ocluster, hammer2_ioc_pfs_t *pfs);
1373 hammer2_cluster_t *hammer2_cluster_parent(hammer2_cluster_t *cluster);
1374
1375 int hammer2_bulk_scan(hammer2_trans_t *trans, hammer2_chain_t *parent,
1376                         int (*func)(hammer2_chain_t *chain, void *info),
1377                         void *info);
1378 int hammer2_bulkfree_pass(hammer2_dev_t *hmp,
1379                         struct hammer2_ioc_bulkfree *bfi);
1380
1381 /*
1382  * hammer2_iocom.c
1383  */
1384 void hammer2_iocom_init(hammer2_dev_t *hmp);
1385 void hammer2_iocom_uninit(hammer2_dev_t *hmp);
1386 void hammer2_cluster_reconnect(hammer2_dev_t *hmp, struct file *fp);
1387
1388 /*
1389  * hammer2_syncthr.c
1390  */
1391 void hammer2_syncthr_create(hammer2_syncthr_t *thr, hammer2_pfs_t *pmp,
1392                         int clindex, void (*func)(void *arg));
1393 void hammer2_syncthr_delete(hammer2_syncthr_t *thr);
1394 void hammer2_syncthr_remaster(hammer2_syncthr_t *thr);
1395 void hammer2_syncthr_freeze(hammer2_syncthr_t *thr);
1396 void hammer2_syncthr_unfreeze(hammer2_syncthr_t *thr);
1397 void hammer2_syncthr_primary(void *arg);
1398
1399 /*
1400  * hammer2_strategy.c
1401  */
1402 int hammer2_vop_strategy(struct vop_strategy_args *ap);
1403 int hammer2_vop_bmap(struct vop_bmap_args *ap);
1404
1405 #endif /* !_KERNEL */
1406 #endif /* !_VFS_HAMMER2_HAMMER2_H_ */