hammer2 - Stabilization pass, more flush refactoring
[dragonfly.git] / sys / vfs / hammer2 / hammer2.h
1 /*
2  * Copyright (c) 2011-2013 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  * This header file contains structures used internally by the HAMMER2
38  * implementation.  See hammer2_disk.h for on-disk structures.
39  */
40
41 #ifndef _VFS_HAMMER2_HAMMER2_H_
42 #define _VFS_HAMMER2_HAMMER2_H_
43
44 #include <sys/param.h>
45 #include <sys/types.h>
46 #include <sys/kernel.h>
47 #include <sys/conf.h>
48 #include <sys/systm.h>
49 #include <sys/tree.h>
50 #include <sys/malloc.h>
51 #include <sys/mount.h>
52 #include <sys/vnode.h>
53 #include <sys/proc.h>
54 #include <sys/mountctl.h>
55 #include <sys/priv.h>
56 #include <sys/stat.h>
57 #include <sys/thread.h>
58 #include <sys/globaldata.h>
59 #include <sys/lockf.h>
60 #include <sys/buf.h>
61 #include <sys/queue.h>
62 #include <sys/limits.h>
63 #include <sys/buf2.h>
64 #include <sys/signal2.h>
65 #include <sys/dmsg.h>
66 #include <sys/mutex.h>
67 #include <sys/mutex2.h>
68
69 #include "hammer2_disk.h"
70 #include "hammer2_mount.h"
71 #include "hammer2_ioctl.h"
72 #include "hammer2_ccms.h"
73
74 struct hammer2_chain;
75 struct hammer2_inode;
76 struct hammer2_mount;
77 struct hammer2_pfsmount;
78 struct hammer2_span;
79 struct hammer2_state;
80 struct hammer2_msg;
81
82 /*
83  * The chain structure tracks a portion of the media topology from the
84  * root (volume) down.  Chains represent volumes, inodes, indirect blocks,
85  * data blocks, and freemap nodes and leafs.
86  *
87  * The chain structure can be multi-homed and its topological recursion
88  * (chain->core) can be shared amongst several chains.  Chain structures
89  * are topologically stable once placed in the in-memory topology (they
90  * don't move around).  Modifications which cross flush synchronization
91  * boundaries, renames, resizing, or any move of the chain to elsewhere
92  * in the topology is accomplished via the DELETE-DUPLICATE mechanism.
93  *
94  * DELETE-DUPLICATE allows HAMMER2 to track work across flush synchronization
95  * points without stalling the filesystem or corrupting the flush
96  * sychronization point.  When necessary a chain will be marked DELETED
97  * and a new, duplicate chain will be allocated.
98  *
99  * This mechanism necessarily requires that we be able to overload chains
100  * at any given layer in the topology.  Overloading is accomplished via a
101  * RBTREE recursion through chain->rbtree.
102  *
103  * Advantages:
104  *
105  *      (1) Fully coherent snapshots can be taken without requiring
106  *          a pre-flush, resulting in extremely fast (sub-millisecond)
107  *          snapshots.
108  *
109  *      (2) Multiple synchronization points can be in-flight at the same
110  *          time, representing multiple snapshots or flushes.
111  *
112  *      (3) The algorithms needed to keep track of everything are actually
113  *          not that complex.
114  *
115  * Special Considerations:
116  *
117  *      A chain is ref-counted on a per-chain basis, but the chain's lock
118  *      is associated with the shared chain_core and is not per-chain.
119  *
120  *      The power-of-2 nature of the media radix tree ensures that there
121  *      will be no overlaps which straddle edges.
122  */
123 RB_HEAD(hammer2_chain_tree, hammer2_chain);
124 TAILQ_HEAD(h2_flush_deferral_list, hammer2_chain);
125 TAILQ_HEAD(h2_core_list, hammer2_chain);
126 TAILQ_HEAD(h2_layer_list, hammer2_chain_layer);
127
128 struct hammer2_chain_layer {
129         int             good;
130         TAILQ_ENTRY(hammer2_chain_layer) entry;
131         struct hammer2_chain_tree rbtree;
132         int             refs;           /* prevent destruction */
133 };
134
135 typedef struct hammer2_chain_layer hammer2_chain_layer_t;
136
137 struct hammer2_chain_core {
138         int             good;
139         struct ccms_cst cst;
140         struct h2_core_list ownerq;     /* all chains sharing this core */
141         struct h2_layer_list layerq;
142         int             live_zero;      /* blockref array opt */
143         hammer2_tid_t   update_tid;     /* check update against parent */
144         u_int           chain_count;    /* total chains in layers */
145         u_int           sharecnt;
146         u_int           flags;
147         u_int           live_count;     /* live (not deleted) chains in tree */
148         int             generation;     /* generation number (inserts only) */
149 };
150
151 typedef struct hammer2_chain_core hammer2_chain_core_t;
152
153 #define HAMMER2_CORE_UNUSED0001         0x0001
154 #define HAMMER2_CORE_COUNTEDBREFS       0x0002
155
156 struct hammer2_chain {
157         RB_ENTRY(hammer2_chain) rbnode;         /* node */
158         TAILQ_ENTRY(hammer2_chain) core_entry;  /* contemporary chains */
159         hammer2_chain_layer_t   *inlayer;
160         hammer2_blockref_t      bref;
161         hammer2_chain_core_t    *core;
162         hammer2_chain_core_t    *above;
163         struct hammer2_state    *state;         /* if active cache msg */
164         struct hammer2_mount    *hmp;
165         struct hammer2_pfsmount *pmp;           /* can be NULL */
166
167         hammer2_tid_t   modify_tid;             /* snapshot/flush filter */
168         hammer2_tid_t   delete_tid;
169         hammer2_key_t   data_count;             /* delta's to apply */
170         hammer2_key_t   inode_count;            /* delta's to apply */
171         struct buf      *bp;                    /* physical data buffer */
172         u_int           bytes;                  /* physical data size */
173         u_int           flags;
174         u_int           refs;
175         u_int           lockcnt;
176         int             debug_reason;
177         hammer2_media_data_t *data;             /* data pointer shortcut */
178         TAILQ_ENTRY(hammer2_chain) flush_node;  /* flush deferral list */
179 };
180
181 typedef struct hammer2_chain hammer2_chain_t;
182
183 int hammer2_chain_cmp(hammer2_chain_t *chain1, hammer2_chain_t *chain2);
184 RB_PROTOTYPE(hammer2_chain_tree, hammer2_chain, rbnode, hammer2_chain_cmp);
185
186 /*
187  * Special notes on flags:
188  *
189  * INITIAL - This flag allows a chain to be created and for storage to
190  *           be allocated without having to immediately instantiate the
191  *           related buffer.  The data is assumed to be all-zeros.  It
192  *           is primarily used for indirect blocks.
193  *
194  * MOVED   - A modified chain becomes MOVED after it flushes.  A chain
195  *           can also become MOVED if it is moved within the topology
196  *           (even if not modified).
197  */
198 #define HAMMER2_CHAIN_MODIFIED          0x00000001      /* dirty chain data */
199 #define HAMMER2_CHAIN_ALLOCATED         0x00000002      /* kmalloc'd chain */
200 #define HAMMER2_CHAIN_DIRTYBP           0x00000004      /* dirty on unlock */
201 #define HAMMER2_CHAIN_FORCECOW          0x00000008      /* force copy-on-wr */
202 #define HAMMER2_CHAIN_DELETED           0x00000010      /* deleted chain */
203 #define HAMMER2_CHAIN_INITIAL           0x00000020      /* initial create */
204 #define HAMMER2_CHAIN_FLUSHED           0x00000040      /* flush on unlock */
205 #define HAMMER2_CHAIN_MOVED             0x00000080      /* bref changed */
206 #define HAMMER2_CHAIN_IOFLUSH           0x00000100      /* bawrite on put */
207 #define HAMMER2_CHAIN_DEFERRED          0x00000200      /* on a deferral list */
208 #define HAMMER2_CHAIN_DESTROYED         0x00000400      /* destroying inode */
209 #define HAMMER2_CHAIN_VOLUMESYNC        0x00000800      /* needs volume sync */
210 #define HAMMER2_CHAIN_UNUSED1000        0x00001000
211 #define HAMMER2_CHAIN_MOUNTED           0x00002000      /* PFS is mounted */
212 #define HAMMER2_CHAIN_ONRBTREE          0x00004000      /* on parent RB tree */
213 #define HAMMER2_CHAIN_SNAPSHOT          0x00008000      /* snapshot special */
214 #define HAMMER2_CHAIN_EMBEDDED          0x00010000      /* embedded data */
215 #define HAMMER2_CHAIN_UNUSED20000       0x00020000
216 #define HAMMER2_CHAIN_UNUSED40000       0x00040000
217 #define HAMMER2_CHAIN_UNUSED80000       0x00080000
218 #define HAMMER2_CHAIN_DUPLICATED        0x00100000      /* fwd delete-dup */
219 #define HAMMER2_CHAIN_PFSROOT           0x00200000      /* in pfs->cluster */
220
221 /*
222  * Flags passed to hammer2_chain_lookup() and hammer2_chain_next()
223  *
224  * NOTE: MATCHIND allows an indirect block / freemap node to be returned
225  *       when the passed key range matches the radix.  Remember that key_end
226  *       is inclusive (e.g. {0x000,0xFFF}, not {0x000,0x1000}).
227  */
228 #define HAMMER2_LOOKUP_NOLOCK           0x00000001      /* ref only */
229 #define HAMMER2_LOOKUP_NODATA           0x00000002      /* data left NULL */
230 #define HAMMER2_LOOKUP_SHARED           0x00000100
231 #define HAMMER2_LOOKUP_MATCHIND         0x00000200
232 #define HAMMER2_LOOKUP_FREEMAP          0x00000400      /* freemap base */
233 #define HAMMER2_LOOKUP_ALWAYS           0x00000800      /* resolve data */
234
235 /*
236  * Flags passed to hammer2_chain_modify() and hammer2_chain_resize()
237  *
238  * NOTE: OPTDATA allows us to avoid instantiating buffers for INDIRECT
239  *       blocks in the INITIAL-create state.
240  */
241 #define HAMMER2_MODIFY_OPTDATA          0x00000002      /* data can be NULL */
242 #define HAMMER2_MODIFY_NO_MODIFY_TID    0x00000004
243 #define HAMMER2_MODIFY_ASSERTNOCOPY     0x00000008      /* assert no del-dup */
244 #define HAMMER2_MODIFY_NOREALLOC        0x00000010
245 #define HAMMER2_MODIFY_INPLACE          0x00000020      /* don't del-dup */
246
247 /*
248  * Flags passed to hammer2_chain_lock()
249  */
250 #define HAMMER2_RESOLVE_NEVER           1
251 #define HAMMER2_RESOLVE_MAYBE           2
252 #define HAMMER2_RESOLVE_ALWAYS          3
253 #define HAMMER2_RESOLVE_MASK            0x0F
254
255 #define HAMMER2_RESOLVE_SHARED          0x10    /* request shared lock */
256 #define HAMMER2_RESOLVE_NOREF           0x20    /* already ref'd on lock */
257
258 /*
259  * Flags passed to hammer2_chain_delete()
260  */
261 #define HAMMER2_DELETE_WILLDUP          0x0001  /* no blk free, will be dup */
262
263 /*
264  * Flags passed to hammer2_chain_delete_duplicate()
265  */
266 #define HAMMER2_DELDUP_RECORE           0x0001
267
268 /*
269  * Cluster different types of storage together for allocations
270  */
271 #define HAMMER2_FREECACHE_INODE         0
272 #define HAMMER2_FREECACHE_INDIR         1
273 #define HAMMER2_FREECACHE_DATA          2
274 #define HAMMER2_FREECACHE_UNUSED3       3
275 #define HAMMER2_FREECACHE_TYPES         4
276
277 /*
278  * hammer2_freemap_alloc() block preference
279  */
280 #define HAMMER2_OFF_NOPREF              ((hammer2_off_t)-1)
281
282 /*
283  * BMAP read-ahead maximum parameters
284  */
285 #define HAMMER2_BMAP_COUNT              16      /* max bmap read-ahead */
286 #define HAMMER2_BMAP_BYTES              (HAMMER2_PBUFSIZE * HAMMER2_BMAP_COUNT)
287
288 /*
289  * Misc
290  */
291 #define HAMMER2_FLUSH_DEPTH_LIMIT       40      /* stack recursion limit */
292
293 /*
294  * HAMMER2 IN-MEMORY CACHE OF MEDIA STRUCTURES
295  *
296  * There is an in-memory representation of all on-media data structure.
297  * Basically everything is represented by a hammer2_chain structure
298  * in-memory and other higher-level structures map to chains.
299  *
300  * A great deal of data is accessed simply via its buffer cache buffer,
301  * which is mapped for the duration of the chain's lock.  However, because
302  * chains may represent blocks smaller than the 16KB minimum we impose
303  * on buffer cache buffers, we cannot hold related buffer cache buffers
304  * locked for smaller blocks.  In these situations we kmalloc() a copy
305  * of the block.
306  *
307  * When modifications are made to a chain a new filesystem block must be
308  * allocated.  Multiple modifications do not necessarily allocate new
309  * blocks.  However, when a flush occurs a flush synchronization point
310  * is created and any new modifications made after this point will allocate
311  * a new block even if the chain is already in a modified state.
312  *
313  * The in-memory representation may remain cached (for example in order to
314  * placemark clustering locks) even after the related data has been
315  * detached.
316  *
317  *                              CORE SHARING
318  *
319  * In order to support concurrent flushes a flush synchronization point
320  * is created represented by a transaction id.  Among other things,
321  * operations may move filesystem objects from one part of the topology
322  * to another (for example, if you rename a file or when indirect blocks
323  * are created or destroyed, and a few other things).  When this occurs
324  * across a flush synchronization point the flusher needs to be able to
325  * recurse down BOTH the 'before' version of the topology and the 'after'
326  * version.
327  *
328  * To facilitate this modifications to chains do what is called a
329  * DELETE-DUPLICATE operation.  Chains are not actually moved in-memory.
330  * Instead the chain we wish to move is deleted and a new chain is created
331  * at the target location in the topology.  ANY SUBCHAINS PLACED UNDER THE
332  * CHAIN BEING MOVED HAVE TO EXIST IN BOTH PLACES.  To make this work
333  * all sub-chains are managed by the hammer2_chain_core structure.  This
334  * structure can be multi-homed, meaning that it can have more than one
335  * chain as its parent.  When a chain is delete-duplicated the chain's core
336  * becomes shared under both the old and new chain.
337  *
338  *                              STALE CHAINS
339  *
340  * When a chain is delete-duplicated the old chain typically becomes stale.
341  * This is detected via the HAMMER2_CHAIN_DUPLICATED flag in chain->flags.
342  * To avoid executing live filesystem operations on stale chains, the inode
343  * locking code will follow stale chains via core->ownerq until it finds
344  * the live chain.  The lock prevents ripups by other threads.  Lookups
345  * must properly order locking operations to prevent other threads from
346  * racing the lookup operation and will also follow stale chains when
347  * required.
348  */
349
350 RB_HEAD(hammer2_inode_tree, hammer2_inode);
351
352 /*
353  * A hammer2 inode.
354  *
355  * NOTE: The inode's attribute CST which is also used to lock the inode
356  *       is embedded in the chain (chain.cst) and aliased w/ attr_cst.
357  */
358 struct hammer2_inode {
359         RB_ENTRY(hammer2_inode) rbnode;         /* inumber lookup (HL) */
360         ccms_cst_t              topo_cst;       /* directory topology cst */
361         struct hammer2_pfsmount *pmp;           /* PFS mount */
362         struct hammer2_inode    *pip;           /* parent inode */
363         struct vnode            *vp;
364         hammer2_chain_t         *chain;         /* NOTE: rehomed on rename */
365         struct lockf            advlock;
366         hammer2_tid_t           inum;
367         u_int                   flags;
368         u_int                   refs;           /* +vpref, +flushref */
369         uint8_t                 comp_heuristic;
370         hammer2_off_t           size;
371         uint64_t                mtime;
372 };
373
374 typedef struct hammer2_inode hammer2_inode_t;
375
376 #define HAMMER2_INODE_MODIFIED          0x0001
377 #define HAMMER2_INODE_SROOT             0x0002  /* kmalloc special case */
378 #define HAMMER2_INODE_RENAME_INPROG     0x0004
379 #define HAMMER2_INODE_ONRBTREE          0x0008
380 #define HAMMER2_INODE_RESIZED           0x0010
381 #define HAMMER2_INODE_MTIME             0x0020
382
383 int hammer2_inode_cmp(hammer2_inode_t *ip1, hammer2_inode_t *ip2);
384 RB_PROTOTYPE2(hammer2_inode_tree, hammer2_inode, rbnode, hammer2_inode_cmp,
385                 hammer2_tid_t);
386
387 /*
388  * A hammer2 transaction and flush sequencing structure.
389  *
390  * This global structure is tied into hammer2_mount and is used
391  * to sequence modifying operations and flushes.
392  *
393  * (a) Any modifying operations with sync_tid >= flush_tid will stall until
394  *     all modifying operating with sync_tid < flush_tid complete.
395  *
396  *     The flush related to flush_tid stalls until all modifying operations
397  *     with sync_tid < flush_tid complete.
398  *
399  * (b) Once unstalled, modifying operations with sync_tid > flush_tid are
400  *     allowed to run.  All modifications cause modify/duplicate operations
401  *     to occur on the related chains.  Note that most INDIRECT blocks will
402  *     be unaffected because the modifications just overload the RBTREE
403  *     structurally instead of actually modifying the indirect blocks.
404  *
405  * (c) The actual flush unstalls and RUNS CONCURRENTLY with (b), but only
406  *     utilizes the chain structures with sync_tid <= flush_tid.  The
407  *     flush will modify related indirect blocks and inodes in-place
408  *     (rather than duplicate) since the adjustments are compatible with
409  *     (b)'s RBTREE overloading
410  *
411  *     SPECIAL NOTE:  Inode modifications have to also propagate along any
412  *                    modify/duplicate chains.  File writes detect the flush
413  *                    and force out the conflicting buffer cache buffer(s)
414  *                    before reusing them.
415  *
416  * (d) Snapshots can be made instantly but must be flushed and disconnected
417  *     from their duplicative source before they can be mounted.  This is
418  *     because while H2's on-media structure supports forks, its in-memory
419  *     structure only supports very simple forking for background flushing
420  *     purposes.
421  *
422  * TODO: Flush merging.  When fsync() is called on multiple discrete files
423  *       concurrently there is no reason to stall the second fsync.
424  *       The final flush that reaches to root can cover both fsync()s.
425  *
426  *     The chains typically terminate as they fly onto the disk.  The flush
427  *     ultimately reaches the volume header.
428  */
429 struct hammer2_trans {
430         TAILQ_ENTRY(hammer2_trans) entry;
431         struct hammer2_pfsmount *pmp;
432         hammer2_tid_t           real_tid;
433         hammer2_tid_t           sync_tid;
434         hammer2_tid_t           inode_tid;
435         thread_t                td;             /* pointer */
436         int                     flags;
437         int                     blocked;
438         uint8_t                 inodes_created;
439         uint8_t                 dummy[7];
440 };
441
442 typedef struct hammer2_trans hammer2_trans_t;
443
444 #define HAMMER2_TRANS_ISFLUSH           0x0001  /* formal flush */
445 #define HAMMER2_TRANS_RESTRICTED        0x0002  /* snapshot flush restrict */
446 #define HAMMER2_TRANS_BUFCACHE          0x0004  /* from bioq strategy write */
447 #define HAMMER2_TRANS_NEWINODE          0x0008  /* caller allocating inode */
448 #define HAMMER2_TRANS_ISALLOCATING      0x0010  /* in allocator */
449
450 #define HAMMER2_FREEMAP_HEUR_NRADIX     4       /* pwr 2 PBUFRADIX-MINIORADIX */
451 #define HAMMER2_FREEMAP_HEUR_TYPES      8
452 #define HAMMER2_FREEMAP_HEUR            (HAMMER2_FREEMAP_HEUR_NRADIX * \
453                                          HAMMER2_FREEMAP_HEUR_TYPES)
454
455 /*
456  * Global (per device) mount structure for device (aka vp->v_mount->hmp)
457  */
458 TAILQ_HEAD(hammer2_trans_queue, hammer2_trans);
459
460 struct hammer2_mount {
461         struct vnode    *devvp;         /* device vnode */
462         int             ronly;          /* read-only mount */
463         int             pmp_count;      /* PFS mounts backed by us */
464         TAILQ_ENTRY(hammer2_mount) mntentry; /* hammer2_mntlist */
465
466         struct malloc_type *mchain;
467         int             nipstacks;
468         int             maxipstacks;
469         hammer2_chain_t vchain;         /* anchor chain (topology) */
470         hammer2_chain_t fchain;         /* anchor chain (freemap) */
471         hammer2_inode_t *sroot;         /* super-root localized to media */
472         struct lock     alloclk;        /* lockmgr lock */
473         struct lock     voldatalk;      /* lockmgr lock */
474         struct hammer2_trans_queue transq; /* all in-progress transactions */
475         hammer2_off_t   heur_freemap[HAMMER2_FREEMAP_HEUR];
476         int             flushcnt;       /* #of flush trans on the list */
477
478         int             volhdrno;       /* last volhdrno written */
479         hammer2_volume_data_t voldata;
480         hammer2_volume_data_t volsync;  /* synchronized voldata */
481 };
482
483 typedef struct hammer2_mount hammer2_mount_t;
484
485 /*
486  * HAMMER2 cluster - a device/root associated with a PFS.
487  *
488  * A PFS may have several hammer2_cluster's associated with it.
489  */
490 #define HAMMER2_MAXCLUSTER      8
491
492 struct hammer2_cluster {
493         int                     nchains;
494         int                     status;
495         hammer2_chain_t         *chains[HAMMER2_MAXCLUSTER];
496 };
497
498 typedef struct hammer2_cluster hammer2_cluster_t;
499
500 /*
501  * HAMMER2 PFS mount point structure (aka vp->v_mount->mnt_data).
502  * This has a 1:1 correspondence to struct mount (note that the
503  * hammer2_mount structure has a N:1 correspondence).
504  *
505  * This structure represents a cluster mount and not necessarily a
506  * PFS under a specific device mount (HMP).  The distinction is important
507  * because the elements backing a cluster mount can change on the fly.
508  *
509  * Usually the first element under the cluster represents the original
510  * user-requested mount that bootstraps the whole mess.  In significant
511  * setups the original is usually just a read-only media image (or
512  * representitive file) that simply contains a bootstrap volume header
513  * listing the configuration.
514  */
515 struct hammer2_pfsmount {
516         struct mount            *mp;
517         hammer2_cluster_t       cluster;
518         hammer2_inode_t         *iroot;         /* PFS root inode */
519         hammer2_off_t           inode_count;    /* copy of inode_count */
520         ccms_domain_t           ccms_dom;
521         struct netexport        export;         /* nfs export */
522         int                     ronly;          /* read-only mount */
523         struct malloc_type      *minode;
524         struct malloc_type      *mmsg;
525         kdmsg_iocom_t           iocom;
526         struct spinlock         inum_spin;      /* inumber lookup */
527         struct hammer2_inode_tree inum_tree;
528         long                    inmem_inodes;
529         long                    inmem_chains;
530         int                     inmem_waiting;
531         thread_t                wthread_td;     /* write thread td */
532         struct bio_queue_head   wthread_bioq;   /* logical buffer bioq */
533         struct mtx              wthread_mtx;    /* interlock */
534         int                     wthread_destroy;/* termination sequencing */
535 };
536
537 typedef struct hammer2_pfsmount hammer2_pfsmount_t;
538
539 struct hammer2_cbinfo {
540         hammer2_chain_t *chain;
541         void (*func)(hammer2_chain_t *, struct buf *, char *, void *);
542         void *arg;
543         size_t boff;
544 };
545
546 typedef struct hammer2_cbinfo hammer2_cbinfo_t;
547
548 #if defined(_KERNEL)
549
550 MALLOC_DECLARE(M_HAMMER2);
551
552 #define VTOI(vp)        ((hammer2_inode_t *)(vp)->v_data)
553 #define ITOV(ip)        ((ip)->vp)
554
555 /*
556  * Currently locked chains retain the locked buffer cache buffer for
557  * indirect blocks, and indirect blocks can be one of two sizes.  The
558  * device buffer has to match the case to avoid deadlocking recursive
559  * chains that might otherwise try to access different offsets within
560  * the same device buffer.
561  */
562 static __inline
563 int
564 hammer2_devblkradix(int radix)
565 {
566 #if 1
567         if (radix <= HAMMER2_LBUFRADIX) {
568                 return (HAMMER2_LBUFRADIX);
569         } else {
570                 return (HAMMER2_PBUFRADIX);
571         }
572 #else
573         return (HAMMER2_PBUFRADIX);
574 #endif
575 }
576
577 static __inline
578 size_t
579 hammer2_devblksize(size_t bytes)
580 {
581 #if 1
582         if (bytes <= HAMMER2_LBUFSIZE) {
583                 return(HAMMER2_LBUFSIZE);
584         } else {
585                 KKASSERT(bytes <= HAMMER2_PBUFSIZE &&
586                          (bytes ^ (bytes - 1)) == ((bytes << 1) - 1));
587                 return (HAMMER2_PBUFSIZE);
588         }
589 #else
590         KKASSERT(bytes <= HAMMER2_PBUFSIZE &&
591                  (bytes ^ (bytes - 1)) == ((bytes << 1) - 1));
592         return(HAMMER2_PBUFSIZE);
593 #endif
594 }
595
596
597 static __inline
598 hammer2_pfsmount_t *
599 MPTOPMP(struct mount *mp)
600 {
601         return ((hammer2_pfsmount_t *)mp->mnt_data);
602 }
603
604 extern struct vop_ops hammer2_vnode_vops;
605 extern struct vop_ops hammer2_spec_vops;
606 extern struct vop_ops hammer2_fifo_vops;
607
608 extern int hammer2_debug;
609 extern int hammer2_cluster_enable;
610 extern int hammer2_hardlink_enable;
611 extern long hammer2_iod_file_read;
612 extern long hammer2_iod_meta_read;
613 extern long hammer2_iod_indr_read;
614 extern long hammer2_iod_fmap_read;
615 extern long hammer2_iod_volu_read;
616 extern long hammer2_iod_file_write;
617 extern long hammer2_iod_meta_write;
618 extern long hammer2_iod_indr_write;
619 extern long hammer2_iod_fmap_write;
620 extern long hammer2_iod_volu_write;
621 extern long hammer2_ioa_file_read;
622 extern long hammer2_ioa_meta_read;
623 extern long hammer2_ioa_indr_read;
624 extern long hammer2_ioa_fmap_read;
625 extern long hammer2_ioa_volu_read;
626 extern long hammer2_ioa_file_write;
627 extern long hammer2_ioa_meta_write;
628 extern long hammer2_ioa_indr_write;
629 extern long hammer2_ioa_fmap_write;
630 extern long hammer2_ioa_volu_write;
631
632 extern struct objcache *cache_buffer_read;
633 extern struct objcache *cache_buffer_write;
634
635 extern int destroy;
636 extern int write_thread_wakeup;
637
638 extern mtx_t thread_protect;
639
640 /*
641  * hammer2_subr.c
642  */
643 #define hammer2_icrc32(buf, size)       iscsi_crc32((buf), (size))
644 #define hammer2_icrc32c(buf, size, crc) iscsi_crc32_ext((buf), (size), (crc))
645
646 hammer2_chain_t *hammer2_inode_lock_ex(hammer2_inode_t *ip);
647 hammer2_chain_t *hammer2_inode_lock_sh(hammer2_inode_t *ip);
648 void hammer2_inode_unlock_ex(hammer2_inode_t *ip, hammer2_chain_t *chain);
649 void hammer2_inode_unlock_sh(hammer2_inode_t *ip, hammer2_chain_t *chain);
650 void hammer2_chain_refactor(hammer2_chain_t **chainp);
651 void hammer2_voldata_lock(hammer2_mount_t *hmp);
652 void hammer2_voldata_unlock(hammer2_mount_t *hmp, int modify);
653 ccms_state_t hammer2_inode_lock_temp_release(hammer2_inode_t *ip);
654 void hammer2_inode_lock_temp_restore(hammer2_inode_t *ip, ccms_state_t ostate);
655 ccms_state_t hammer2_inode_lock_upgrade(hammer2_inode_t *ip);
656 void hammer2_inode_lock_downgrade(hammer2_inode_t *ip, ccms_state_t ostate);
657
658 void hammer2_mount_exlock(hammer2_mount_t *hmp);
659 void hammer2_mount_shlock(hammer2_mount_t *hmp);
660 void hammer2_mount_unlock(hammer2_mount_t *hmp);
661
662 int hammer2_get_dtype(hammer2_chain_t *chain);
663 int hammer2_get_vtype(hammer2_chain_t *chain);
664 u_int8_t hammer2_get_obj_type(enum vtype vtype);
665 void hammer2_time_to_timespec(u_int64_t xtime, struct timespec *ts);
666 u_int64_t hammer2_timespec_to_time(struct timespec *ts);
667 u_int32_t hammer2_to_unix_xid(uuid_t *uuid);
668 void hammer2_guid_to_uuid(uuid_t *uuid, u_int32_t guid);
669
670 hammer2_key_t hammer2_dirhash(const unsigned char *name, size_t len);
671 int hammer2_getradix(size_t bytes);
672
673 int hammer2_calc_logical(hammer2_inode_t *ip, hammer2_off_t uoff,
674                         hammer2_key_t *lbasep, hammer2_key_t *leofp);
675 int hammer2_calc_physical(hammer2_inode_t *ip, hammer2_key_t lbase);
676 void hammer2_update_time(uint64_t *timep);
677
678 /*
679  * hammer2_inode.c
680  */
681 struct vnode *hammer2_igetv(hammer2_inode_t *ip, int *errorp);
682
683 void hammer2_inode_lock_nlinks(hammer2_inode_t *ip);
684 void hammer2_inode_unlock_nlinks(hammer2_inode_t *ip);
685 hammer2_inode_t *hammer2_inode_lookup(hammer2_pfsmount_t *pmp,
686                         hammer2_tid_t inum);
687 hammer2_inode_t *hammer2_inode_get(hammer2_pfsmount_t *pmp,
688                         hammer2_inode_t *dip, hammer2_chain_t *chain);
689 void hammer2_inode_free(hammer2_inode_t *ip);
690 void hammer2_inode_ref(hammer2_inode_t *ip);
691 void hammer2_inode_drop(hammer2_inode_t *ip);
692 void hammer2_inode_repoint(hammer2_inode_t *ip, hammer2_inode_t *pip,
693                         hammer2_chain_t *chain);
694
695 hammer2_inode_t *hammer2_inode_create(hammer2_trans_t *trans,
696                         hammer2_inode_t *dip,
697                         struct vattr *vap, struct ucred *cred,
698                         const uint8_t *name, size_t name_len,
699                         hammer2_chain_t **chainp, int *errorp);
700 int hammer2_inode_connect(hammer2_trans_t *trans, int hlink,
701                         hammer2_inode_t *dip, hammer2_chain_t **chainp,
702                         const uint8_t *name, size_t name_len);
703 hammer2_inode_t *hammer2_inode_common_parent(hammer2_inode_t *fdip,
704                         hammer2_inode_t *tdip);
705 void hammer2_inode_fsync(hammer2_trans_t *trans, hammer2_inode_t *ip,
706                         hammer2_chain_t **parentp);
707 int hammer2_unlink_file(hammer2_trans_t *trans, hammer2_inode_t *dip,
708                         const uint8_t *name, size_t name_len, int isdir,
709                         int *hlinkp);
710 int hammer2_hardlink_consolidate(hammer2_trans_t *trans, hammer2_inode_t *ip,
711                         hammer2_chain_t **chainp,
712                         hammer2_inode_t *tdip, int linkcnt);
713 int hammer2_hardlink_deconsolidate(hammer2_trans_t *trans, hammer2_inode_t *dip,
714                         hammer2_chain_t **chainp, hammer2_chain_t **ochainp);
715 int hammer2_hardlink_find(hammer2_inode_t *dip,
716                         hammer2_chain_t **chainp, hammer2_chain_t **ochainp);
717
718 /*
719  * hammer2_chain.c
720  */
721 void hammer2_modify_volume(hammer2_mount_t *hmp);
722 hammer2_chain_t *hammer2_chain_alloc(hammer2_mount_t *hmp, hammer2_pfsmount_t *pmp,
723                                 hammer2_trans_t *trans, hammer2_blockref_t *bref);
724 void hammer2_chain_core_alloc(hammer2_trans_t *trans, hammer2_chain_t *nchain,
725                                 hammer2_chain_t *ochain);
726 void hammer2_chain_ref(hammer2_chain_t *chain);
727 void hammer2_chain_drop(hammer2_chain_t *chain);
728 int hammer2_chain_lock(hammer2_chain_t *chain, int how);
729 void hammer2_chain_load_async(hammer2_chain_t *chain,
730                                 void (*func)(hammer2_chain_t *, struct buf *,
731                                              char *, void *),
732                                 void *arg);
733 void hammer2_chain_moved(hammer2_chain_t *chain);
734 void hammer2_chain_modify(hammer2_trans_t *trans,
735                                 hammer2_chain_t **chainp, int flags);
736 hammer2_inode_data_t *hammer2_chain_modify_ip(hammer2_trans_t *trans,
737                                 hammer2_inode_t *ip, hammer2_chain_t **chainp,
738                                 int flags);
739 void hammer2_chain_resize(hammer2_trans_t *trans, hammer2_inode_t *ip,
740                                 hammer2_chain_t *parent,
741                                 hammer2_chain_t **chainp,
742                                 int nradix, int flags);
743 void hammer2_chain_unlock(hammer2_chain_t *chain);
744 void hammer2_chain_wait(hammer2_chain_t *chain);
745 hammer2_chain_t *hammer2_chain_get(hammer2_chain_t *parent,
746                                 hammer2_blockref_t *bref);
747 hammer2_chain_t *hammer2_chain_lookup_init(hammer2_chain_t *parent, int flags);
748 void hammer2_chain_lookup_done(hammer2_chain_t *parent);
749 hammer2_chain_t *hammer2_chain_lookup(hammer2_chain_t **parentp,
750                                 hammer2_key_t *key_nextp,
751                                 hammer2_key_t key_beg, hammer2_key_t key_end,
752                                 int *cache_indexp, int flags);
753 hammer2_chain_t *hammer2_chain_next(hammer2_chain_t **parentp,
754                                 hammer2_chain_t *chain,
755                                 hammer2_key_t *key_nextp,
756                                 hammer2_key_t key_beg, hammer2_key_t key_end,
757                                 int *cache_indexp, int flags);
758
759 int hammer2_chain_create(hammer2_trans_t *trans,
760                                 hammer2_chain_t **parentp,
761                                 hammer2_chain_t **chainp,
762                                 hammer2_key_t key, int keybits,
763                                 int type, size_t bytes);
764 void hammer2_chain_duplicate(hammer2_trans_t *trans, hammer2_chain_t **parentp,
765                                 hammer2_chain_t **chainp,
766                                 hammer2_blockref_t *bref, int snapshot);
767 int hammer2_chain_snapshot(hammer2_trans_t *trans, hammer2_chain_t **chainp,
768                                 hammer2_ioc_pfs_t *pfs);
769 void hammer2_chain_delete(hammer2_trans_t *trans, hammer2_chain_t *chain,
770                                 int flags);
771 void hammer2_chain_delete_duplicate(hammer2_trans_t *trans,
772                                 hammer2_chain_t **chainp, int flags);
773 void hammer2_chain_flush(hammer2_trans_t *trans, hammer2_chain_t **chainp);
774 void hammer2_chain_commit(hammer2_trans_t *trans, hammer2_chain_t *chain);
775 void hammer2_chain_setsubmod(hammer2_trans_t *trans, hammer2_chain_t *chain);
776
777 void hammer2_chain_memory_wait(hammer2_pfsmount_t *pmp);
778 void hammer2_chain_memory_wakeup(hammer2_pfsmount_t *pmp);
779 void hammer2_chain_countbrefs(hammer2_chain_t *chain,
780                                 hammer2_blockref_t *base, int count);
781 void hammer2_chain_layer_check_locked(hammer2_mount_t *hmp,
782                                 hammer2_chain_core_t *core);
783
784 int hammer2_base_find(hammer2_chain_t *chain,
785                                 hammer2_blockref_t *base, int count,
786                                 int *cache_indexp, hammer2_key_t *key_nextp,
787                                 hammer2_key_t key_beg, hammer2_key_t key_end);
788 void hammer2_base_delete(hammer2_chain_t *chain,
789                                 hammer2_blockref_t *base, int count,
790                                 int *cache_indexp, hammer2_chain_t *child);
791 void hammer2_base_insert(hammer2_chain_t *chain,
792                                 hammer2_blockref_t *base, int count,
793                                 int *cache_indexp, hammer2_chain_t *child);
794
795 /*
796  * hammer2_trans.c
797  */
798 void hammer2_trans_init(hammer2_trans_t *trans,
799                         hammer2_pfsmount_t *pmp, int flags);
800 void hammer2_trans_clear_invfsync(hammer2_trans_t *trans);
801 void hammer2_trans_done(hammer2_trans_t *trans);
802
803 /*
804  * hammer2_ioctl.c
805  */
806 int hammer2_ioctl(hammer2_inode_t *ip, u_long com, void *data,
807                                 int fflag, struct ucred *cred);
808
809 /*
810  * hammer2_msgops.c
811  */
812 int hammer2_msg_dbg_rcvmsg(kdmsg_msg_t *msg);
813 int hammer2_msg_adhoc_input(kdmsg_msg_t *msg);
814
815 /*
816  * hammer2_vfsops.c
817  */
818 void hammer2_clusterctl_wakeup(kdmsg_iocom_t *iocom);
819 void hammer2_volconf_update(hammer2_pfsmount_t *pmp, int index);
820 void hammer2_cluster_reconnect(hammer2_pfsmount_t *pmp, struct file *fp);
821 void hammer2_dump_chain(hammer2_chain_t *chain, int tab, int *countp);
822 void hammer2_bioq_sync(hammer2_pfsmount_t *pmp);
823 int hammer2_vfs_sync(struct mount *mp, int waitflags);
824
825 /*
826  * hammer2_freemap.c
827  */
828 int hammer2_freemap_alloc(hammer2_trans_t *trans, hammer2_mount_t *hmp,
829                                 hammer2_blockref_t *bref, size_t bytes);
830 void hammer2_freemap_free(hammer2_trans_t *trans, hammer2_mount_t *hmp,
831                                 hammer2_blockref_t *bref, int how);
832
833
834 #endif /* !_KERNEL */
835 #endif /* !_VFS_HAMMER2_HAMMER2_H_ */