Merge branches 'hammer2' and 'master' of ssh://crater.dragonflybsd.org/repository...
[dragonfly.git] / sys / vfs / hammer2 / hammer2.h
1 /*
2  * Copyright (c) 2011-2012 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/globaldata.h>
58 #include <sys/lockf.h>
59 #include <sys/buf.h>
60 #include <sys/queue.h>
61 #include <sys/limits.h>
62 #include <sys/buf2.h>
63 #include <sys/signal2.h>
64 #include <sys/tree.h>
65
66 #include "hammer2_disk.h"
67 #include "hammer2_mount.h"
68
69 struct hammer2_chain;
70 struct hammer2_inode;
71 struct hammer2_mount;
72
73 /*
74  * The chain structure tracks blockref recursions all the way to
75  * the root volume.  These consist of indirect blocks, inodes,
76  * and eventually the volume header.
77  *
78  * The chain structure is embedded in the hammer2_mount, hammer2_inode,
79  * and other system memory structures.  The chain structure typically
80  * implements the reference count and busy flag for the larger structure.
81  *
82  * It is always possible to track a chain element all the way back to the
83  * root by following the (parent) links.  (index) is a type-dependent index
84  * in the parent indicating where in the parent the chain element resides.
85  *
86  * When a blockref is added or deleted the related chain element is marked
87  * modified and all of its parents are marked SUBMODIFIED (the parent
88  * recursion can stop once we hit a node that is already marked SUBMODIFIED).
89  * A deleted chain element must remain intact until synchronized against
90  * its parent.
91  *
92  * The blockref at (parent, index) is not adjusted until the modified chain
93  * element is flushed and unmarked.  Until then the child's blockref may
94  * not match the blockref at (parent, index).
95  */
96 SPLAY_HEAD(hammer2_chain_splay, hammer2_chain);
97
98 struct hammer2_chain {
99         struct hammer2_blockref bref;
100         struct hammer2_chain *parent;   /* return chain to root */
101         struct hammer2_chain_splay shead;
102         SPLAY_ENTRY(hammer2_chain) snode;
103         union {
104                 struct hammer2_inode *ip;
105                 struct hammer2_indblock *np;
106                 struct hammer2_data *dp;
107                 void *mem;
108         } u;
109
110         struct buf      *bp;            /* buffer cache (ro) */
111         hammer2_media_data_t *data;     /* modified copy of data (rw) */
112
113         struct lock     lk;             /* lockmgr lock */
114         int             index;          /* index in parent */
115         u_int           refs;
116         u_int           busy;           /* soft-busy */
117         u_int           flags;
118 };
119
120 typedef struct hammer2_chain hammer2_chain_t;
121
122 int hammer2_chain_cmp(hammer2_chain_t *chain1, hammer2_chain_t *chain2);
123 SPLAY_PROTOTYPE(hammer2_chain_splay, hammer2_chain, snode, hammer2_chain_cmp);
124
125 #define HAMMER2_CHAIN_MODIFIED          0x00000001      /* this elm modified */
126 #define HAMMER2_CHAIN_SUBMODIFIED       0x00000002      /* 1+ subs modified */
127 #define HAMMER2_CHAIN_DELETED           0x00000004
128 #define HAMMER2_CHAIN_INITIAL           0x00000008      /* initial write */
129
130 /*
131  * HAMMER2 IN-MEMORY CACHE OF MEDIA STRUCTURES
132  *
133  * There is an in-memory representation of all on-media data structure.
134  *
135  * When accessed read-only the data will be mapped to the related buffer
136  * cache buffer.
137  *
138  * When accessed read-write (marked modified) a kmalloc()'d copy of the
139  * is created which can then be modified.  The copy is destroyed when a
140  * filesystem block is allocated to replace it.
141  *
142  * Active inodes (those with vnodes attached) will maintain the kmalloc()'d
143  * copy for both the read-only and the read-write case.  The combination of
144  * (bp) and (data) determines whether (data) was allocated or not.
145  *
146  * The in-memory representation may remain cached (for example in order to
147  * placemark clustering locks) even after the related data has been
148  * detached.
149  */
150
151 /*
152  * A hammer2 inode.
153  */
154 struct hammer2_inode {
155         struct hammer2_mount    *hmp;
156         struct vnode            *vp;
157         hammer2_chain_t         chain;
158         struct hammer2_inode_data ip_data;
159 };
160
161 typedef struct hammer2_inode hammer2_inode_t;
162
163 /*
164  * A hammer2 indirect block
165  */
166 struct hammer2_indblock {
167         hammer2_chain_t         chain;
168 };
169
170 typedef struct hammer2_indblock hammer2_indblock_t;
171
172 #define np_data         chain.data->npdata
173
174 /*
175  * A hammer2 data block
176  */
177 struct hammer2_data {
178         hammer2_chain_t         chain;
179 };
180
181 #define dp_data         chain.data->buf
182
183 typedef struct hammer2_data hammer2_data_t;
184
185 /*
186  * Governing mount structure for filesystem (aka vp->v_mount)
187  */
188 struct hammer2_mount {
189         struct mount    *mp;            /* kernel mount */
190         struct vnode    *devvp;         /* device vnode */
191         struct netexport export;        /* nfs export */
192         int             ronly;          /* read-only mount */
193
194         struct malloc_type *minode;
195         int             ninodes;
196         int             maxinodes;
197
198         struct malloc_type *mchain;
199         int             nipstacks;
200         int             maxipstacks;
201         hammer2_chain_t vchain;         /* anchor chain */
202         hammer2_chain_t *schain;        /* super-root */
203         hammer2_chain_t *rchain;        /* label-root */
204         struct hammer2_inode *iroot;
205
206         hammer2_volume_data_t voldata;
207         hammer2_off_t   freecache[HAMMER2_MAX_RADIX];
208 };
209
210 typedef struct hammer2_mount hammer2_mount_t;
211
212 #if defined(_KERNEL)
213
214 MALLOC_DECLARE(M_HAMMER2);
215
216 static __inline
217 struct mount *
218 H2TOMP(struct hammer2_mount *hmp)
219 {
220         return (struct mount *) hmp->mp;
221 }
222
223 #define VTOI(vp)        ((hammer2_inode_t *)(vp)->v_data)
224 #define ITOV(ip)        ((ip)->vp)
225
226 static __inline
227 struct hammer2_mount *
228 MPTOH2(struct mount *mp)
229 {
230         return (hammer2_mount_t *) mp->mnt_data;
231 }
232
233 extern struct vop_ops hammer2_vnode_vops;
234 extern struct vop_ops hammer2_spec_vops;
235 extern struct vop_ops hammer2_fifo_vops;
236
237 /*
238  * hammer2_subr.c
239  */
240 void hammer2_inode_lock_ex(hammer2_inode_t *ip);
241 void hammer2_inode_unlock_ex(hammer2_inode_t *ip);
242 void hammer2_inode_lock_sh(hammer2_inode_t *ip);
243 void hammer2_inode_unlock_sh(hammer2_inode_t *ip);
244 void hammer2_inode_busy(hammer2_inode_t *ip);
245 void hammer2_inode_unbusy(hammer2_inode_t *ip);
246
247 void hammer2_mount_exlock(hammer2_mount_t *hmp);
248 void hammer2_mount_shlock(hammer2_mount_t *hmp);
249 void hammer2_mount_unlock(hammer2_mount_t *hmp);
250
251 hammer2_key_t hammer2_dirhash(const unsigned char *name, size_t len);
252
253 /*
254  * hammer2_inode.c
255  */
256 struct vnode *hammer2_igetv(hammer2_inode_t *ip, int *errorp);
257 hammer2_inode_t *hammer2_inode_alloc(hammer2_mount_t *hmp, void *data);
258 void hammer2_inode_free(hammer2_inode_t *ip);
259 void hammer2_inode_ref(hammer2_inode_t *ip);
260 void hammer2_inode_drop(hammer2_inode_t *ip);
261
262 /*
263  * hammer2_chain.c
264  */
265 hammer2_chain_t *hammer2_chain_alloc(hammer2_mount_t *hmp,
266                                 hammer2_blockref_t *bref);
267 void hammer2_chain_free(hammer2_mount_t *hmp, hammer2_chain_t *chain);
268 void hammer2_chain_ref(hammer2_mount_t *hmp, hammer2_chain_t *chain);
269 void hammer2_chain_drop(hammer2_mount_t *hmp, hammer2_chain_t *chain);
270 int hammer2_chain_lock(hammer2_mount_t *hmp, hammer2_chain_t *chain);
271 void hammer2_chain_modify(hammer2_mount_t *hmp, hammer2_chain_t *chain);
272 void hammer2_chain_unlock(hammer2_mount_t *hmp, hammer2_chain_t *chain);
273 hammer2_chain_t *hammer2_chain_get(hammer2_mount_t *hmp,
274                                 hammer2_chain_t *parent, int index);
275 void hammer2_chain_put(hammer2_mount_t *hmp, hammer2_chain_t *chain);
276 hammer2_chain_t *hammer2_chain_lookup(hammer2_mount_t *hmp,
277                                 hammer2_chain_t **parentp, hammer2_key_t key,
278                                 hammer2_key_t mask);
279 hammer2_chain_t *hammer2_chain_next(hammer2_mount_t *hmp,
280                                 hammer2_chain_t **parentp,
281                                 hammer2_chain_t *chain,
282                                 hammer2_key_t key, hammer2_key_t mask);
283 hammer2_chain_t *hammer2_chain_create(hammer2_mount_t *hmp,
284                                 hammer2_chain_t *parent,
285                                 hammer2_key_t key, int keybits,
286                                 int type, size_t bytes);
287 void hammer2_chain_delete(hammer2_mount_t *hmp, hammer2_chain_t **parentp,
288                                 hammer2_chain_t *chain);
289
290
291 /*
292  * hammer2_freemap.c
293  */
294 hammer2_off_t hammer2_freemap_alloc(hammer2_mount_t *hmp, size_t bytes);
295
296 #endif /* !_KERNEL */
297 #endif /* !_VFS_HAMMER2_HAMMER2_H_ */