53737d3839cd163b5da97a5efde8852f70f043f6
[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         TAILQ_ENTRY(hammer2_chain) flush_node;  /* flush deferral list */
104         union {
105                 struct hammer2_inode *ip;
106                 struct hammer2_indblock *np;
107                 struct hammer2_data *dp;
108                 void *mem;
109         } u;
110
111         struct buf      *bp;            /* buffer cache (ro) */
112         hammer2_media_data_t *data;     /* modified copy of data (rw) */
113         u_int           bytes;          /* physical size of data */
114         struct lock     lk;             /* lockmgr lock */
115         int             index;          /* index in parent */
116         u_int           refs;
117         u_int           busy;           /* soft-busy */
118         u_int           flags;
119 };
120
121 typedef struct hammer2_chain hammer2_chain_t;
122
123 int hammer2_chain_cmp(hammer2_chain_t *chain1, hammer2_chain_t *chain2);
124 SPLAY_PROTOTYPE(hammer2_chain_splay, hammer2_chain, snode, hammer2_chain_cmp);
125
126 #define HAMMER2_CHAIN_MODIFIED1         0x00000001      /* active mods */
127 #define HAMMER2_CHAIN_DIRTYEMBED        0x00000002      /* inode embedded */
128 #define HAMMER2_CHAIN_DIRTYBP           0x00000004      /* dirty on unlock */
129 #define HAMMER2_CHAIN_SUBMODIFIED       0x00000008      /* 1+ subs modified */
130 #define HAMMER2_CHAIN_DELETED           0x00000010
131 #define HAMMER2_CHAIN_INITIAL           0x00000020      /* initial create */
132 #define HAMMER2_CHAIN_FLUSHED           0x00000040      /* flush on unlock */
133 #define HAMMER2_CHAIN_MOVED             0x00000080      /* moved */
134 #define HAMMER2_CHAIN_IOFLUSH           0x00000100      /* bawrite on put */
135 #define HAMMER2_CHAIN_DEFERRED          0x00000200      /* on a deferral list*/
136 #define HAMMER2_CHAIN_DESTROYED         0x00000400      /* destroying */
137
138 /*
139  * Flags passed to hammer2_chain_lookup() and hammer2_chain_next()
140  */
141 #define HAMMER2_LOOKUP_NOLOCK           0x00000001      /* ref only */
142 #define HAMMER2_LOOKUP_NODATA           0x00000002      /* data left NULL */
143
144 /*
145  * Flags passed to hammer2_chain_modify() and hammer2_chain_resize()
146  *
147  * NOTE: OPTDATA allows us to avoid instantiating buffers for INDIRECT
148  *       blocks in the INITIAL-create state.
149  */
150 #define HAMMER2_MODIFY_NOSUB            0x00000001      /* do not set SUBMOD */
151 #define HAMMER2_MODIFY_OPTDATA          0x00000002      /* data can be NULL */
152
153 /*
154  * Flags passed to hammer2_chain_lock()
155  */
156 #define HAMMER2_RESOLVE_NEVER           1
157 #define HAMMER2_RESOLVE_MAYBE           2
158 #define HAMMER2_RESOLVE_ALWAYS          3
159
160 /*
161  * Cluster different types of storage together for allocations
162  */
163 #define HAMMER2_FREECACHE_INODE         0
164 #define HAMMER2_FREECACHE_INDIR         1
165 #define HAMMER2_FREECACHE_DATA          2
166 #define HAMMER2_FREECACHE_UNUSED3       3
167 #define HAMMER2_FREECACHE_TYPES         4
168
169 /*
170  * BMAP read-ahead maximum parameters
171  */
172 #define HAMMER2_BMAP_COUNT              16      /* max bmap read-ahead */
173 #define HAMMER2_BMAP_BYTES              (HAMMER2_PBUFSIZE * HAMMER2_BMAP_COUNT)
174
175 /*
176  * Misc
177  */
178 #define HAMMER2_FLUSH_DEPTH_LIMIT       40      /* stack recursion limit */
179
180 /*
181  * HAMMER2 IN-MEMORY CACHE OF MEDIA STRUCTURES
182  *
183  * There is an in-memory representation of all on-media data structure.
184  *
185  * When accessed read-only the data will be mapped to the related buffer
186  * cache buffer.
187  *
188  * When accessed read-write (marked modified) a kmalloc()'d copy of the
189  * is created which can then be modified.  The copy is destroyed when a
190  * filesystem block is allocated to replace it.
191  *
192  * Active inodes (those with vnodes attached) will maintain the kmalloc()'d
193  * copy for both the read-only and the read-write case.  The combination of
194  * (bp) and (data) determines whether (data) was allocated or not.
195  *
196  * The in-memory representation may remain cached (for example in order to
197  * placemark clustering locks) even after the related data has been
198  * detached.
199  */
200
201 /*
202  * A hammer2 inode.
203  */
204 struct hammer2_inode {
205         struct hammer2_mount    *hmp;
206         struct hammer2_inode    *pip;           /* parent inode */
207         struct vnode            *vp;
208         hammer2_chain_t         chain;
209         struct hammer2_inode_data ip_data;
210         struct lockf            advlock;
211 };
212
213 typedef struct hammer2_inode hammer2_inode_t;
214
215 /*
216  * A hammer2 indirect block
217  */
218 struct hammer2_indblock {
219         hammer2_chain_t         chain;
220 };
221
222 typedef struct hammer2_indblock hammer2_indblock_t;
223
224 /*
225  * A hammer2 data block
226  */
227 struct hammer2_data {
228         hammer2_chain_t         chain;
229 };
230
231 typedef struct hammer2_data hammer2_data_t;
232
233 /*
234  * Governing mount structure for filesystem (aka vp->v_mount)
235  */
236 struct hammer2_mount {
237         struct mount    *mp;            /* kernel mount */
238         struct vnode    *devvp;         /* device vnode */
239         struct netexport export;        /* nfs export */
240         int             ronly;          /* read-only mount */
241
242         struct malloc_type *minode;
243         int             ninodes;
244         int             maxinodes;
245
246         struct malloc_type *mchain;
247         int             nipstacks;
248         int             maxipstacks;
249         hammer2_chain_t vchain;         /* anchor chain */
250         hammer2_chain_t *schain;        /* super-root */
251         hammer2_chain_t *rchain;        /* label-root */
252         struct hammer2_inode *iroot;
253         struct lock     alloclk;        /* lockmgr lock */
254
255         hammer2_volume_data_t voldata;
256         hammer2_off_t   freecache[HAMMER2_FREECACHE_TYPES][HAMMER2_MAX_RADIX+1];
257 };
258
259 typedef struct hammer2_mount hammer2_mount_t;
260
261 #if defined(_KERNEL)
262
263 MALLOC_DECLARE(M_HAMMER2);
264
265 static __inline
266 struct mount *
267 H2TOMP(struct hammer2_mount *hmp)
268 {
269         return (struct mount *) hmp->mp;
270 }
271
272 #define VTOI(vp)        ((hammer2_inode_t *)(vp)->v_data)
273 #define ITOV(ip)        ((ip)->vp)
274
275 static __inline
276 struct hammer2_mount *
277 MPTOH2(struct mount *mp)
278 {
279         return (hammer2_mount_t *) mp->mnt_data;
280 }
281
282 extern struct vop_ops hammer2_vnode_vops;
283 extern struct vop_ops hammer2_spec_vops;
284 extern struct vop_ops hammer2_fifo_vops;
285
286 extern int hammer2_debug;
287 extern int hammer2_cluster_enable;
288 extern long hammer2_iod_file_read;
289 extern long hammer2_iod_meta_read;
290 extern long hammer2_iod_indr_read;
291 extern long hammer2_iod_file_write;
292 extern long hammer2_iod_meta_write;
293 extern long hammer2_iod_indr_write;
294 extern long hammer2_iod_volu_write;
295 extern long hammer2_ioa_file_read;
296 extern long hammer2_ioa_meta_read;
297 extern long hammer2_ioa_indr_read;
298 extern long hammer2_ioa_file_write;
299 extern long hammer2_ioa_meta_write;
300 extern long hammer2_ioa_indr_write;
301 extern long hammer2_ioa_volu_write;
302
303 /*
304  * hammer2_subr.c
305  */
306 void hammer2_inode_lock_ex(hammer2_inode_t *ip);
307 void hammer2_inode_unlock_ex(hammer2_inode_t *ip);
308 void hammer2_inode_lock_sh(hammer2_inode_t *ip);
309 void hammer2_inode_unlock_sh(hammer2_inode_t *ip);
310 void hammer2_inode_busy(hammer2_inode_t *ip);
311 void hammer2_inode_unbusy(hammer2_inode_t *ip);
312
313 void hammer2_mount_exlock(hammer2_mount_t *hmp);
314 void hammer2_mount_shlock(hammer2_mount_t *hmp);
315 void hammer2_mount_unlock(hammer2_mount_t *hmp);
316
317 int hammer2_get_dtype(hammer2_inode_t *ip);
318 int hammer2_get_vtype(hammer2_inode_t *ip);
319 u_int8_t hammer2_get_obj_type(enum vtype vtype);
320 void hammer2_time_to_timespec(u_int64_t xtime, struct timespec *ts);
321 u_int32_t hammer2_to_unix_xid(uuid_t *uuid);
322
323 hammer2_key_t hammer2_dirhash(const unsigned char *name, size_t len);
324 int hammer2_bytes_to_radix(size_t bytes);
325
326 int hammer2_calc_logical(hammer2_inode_t *ip, hammer2_off_t uoff,
327                          hammer2_key_t *lbasep, hammer2_key_t *leofp);
328
329 /*
330  * hammer2_inode.c
331  */
332 struct vnode *hammer2_igetv(hammer2_inode_t *ip, int *errorp);
333 hammer2_inode_t *hammer2_inode_alloc(hammer2_mount_t *hmp, void *data);
334 void hammer2_inode_free(hammer2_inode_t *ip);
335 void hammer2_inode_ref(hammer2_inode_t *ip);
336 void hammer2_inode_drop(hammer2_inode_t *ip);
337 int hammer2_inode_calc_alloc(hammer2_key_t filesize);
338
339 int hammer2_inode_create(hammer2_mount_t *hmp,
340                         struct vattr *vap, struct ucred *cred,
341                         hammer2_inode_t *dip,
342                         const uint8_t *name, size_t name_len,
343                         hammer2_inode_t **nipp);
344
345 int hammer2_inode_connect(hammer2_inode_t *dip, hammer2_inode_t *nip,
346                         const uint8_t *name, size_t name_len);
347
348 int hammer2_hardlink_create(hammer2_inode_t *ip, hammer2_inode_t *dip,
349                         const uint8_t *name, size_t name_len);
350
351 /*
352  * hammer2_chain.c
353  */
354 hammer2_chain_t *hammer2_chain_alloc(hammer2_mount_t *hmp,
355                                 hammer2_blockref_t *bref);
356 void hammer2_chain_free(hammer2_mount_t *hmp, hammer2_chain_t *chain);
357 void hammer2_chain_ref(hammer2_mount_t *hmp, hammer2_chain_t *chain);
358 void hammer2_chain_drop(hammer2_mount_t *hmp, hammer2_chain_t *chain);
359 int hammer2_chain_lock(hammer2_mount_t *hmp, hammer2_chain_t *chain, int how);
360 void hammer2_chain_moved(hammer2_mount_t *hmp, hammer2_chain_t *chain);
361 void hammer2_chain_modify(hammer2_mount_t *hmp, hammer2_chain_t *chain,
362                                 int flags);
363 void hammer2_chain_resize(hammer2_mount_t *hmp, hammer2_chain_t *chain,
364                                 int nradix, int flags);
365 void hammer2_chain_unlock(hammer2_mount_t *hmp, hammer2_chain_t *chain);
366 hammer2_chain_t *hammer2_chain_find(hammer2_mount_t *hmp,
367                                 hammer2_chain_t *parent, int index);
368 hammer2_chain_t *hammer2_chain_get(hammer2_mount_t *hmp,
369                                 hammer2_chain_t *parent,
370                                 int index, int flags);
371 hammer2_chain_t *hammer2_chain_lookup(hammer2_mount_t *hmp,
372                                 hammer2_chain_t **parentp,
373                                 hammer2_key_t key_beg, hammer2_key_t key_end,
374                                 int flags);
375 hammer2_chain_t *hammer2_chain_next(hammer2_mount_t *hmp,
376                                 hammer2_chain_t **parentp,
377                                 hammer2_chain_t *chain,
378                                 hammer2_key_t key_beg, hammer2_key_t key_end,
379                                 int flags);
380 hammer2_chain_t *hammer2_chain_create(hammer2_mount_t *hmp,
381                                 hammer2_chain_t *parent,
382                                 hammer2_chain_t *chain,
383                                 hammer2_key_t key, int keybits,
384                                 int type, size_t bytes);
385 void hammer2_chain_delete(hammer2_mount_t *hmp, hammer2_chain_t *parent,
386                                 hammer2_chain_t *chain);
387 void hammer2_chain_flush(hammer2_mount_t *hmp, hammer2_chain_t *chain);
388 void hammer2_chain_commit(hammer2_mount_t *hmp, hammer2_chain_t *chain);
389
390 /*
391  * hammer2_freemap.c
392  */
393 hammer2_off_t hammer2_freemap_alloc(hammer2_mount_t *hmp,
394                                 int type, size_t bytes);
395
396 #endif /* !_KERNEL */
397 #endif /* !_VFS_HAMMER2_HAMMER2_H_ */