hammer2 - Initial CCMS locking tie-in
[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/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/tree.h>
66
67 #include "hammer2_disk.h"
68 #include "hammer2_mount.h"
69 #include "hammer2_ioctl.h"
70 #include "hammer2_ccms.h"
71
72 struct hammer2_chain;
73 struct hammer2_inode;
74 struct hammer2_mount;
75 struct hammer2_pfsmount;
76
77 /*
78  * The chain structure tracks blockref recursions all the way to
79  * the root volume.  These consist of indirect blocks, inodes,
80  * and eventually the volume header.
81  *
82  * The chain structure is embedded in the hammer2_mount, hammer2_inode,
83  * and other system memory structures.  The chain structure typically
84  * implements the reference count and busy flag for the larger structure.
85  *
86  * It is always possible to track a chain element all the way back to the
87  * root by following the (parent) links.  (index) is a type-dependent index
88  * in the parent indicating where in the parent the chain element resides.
89  *
90  * When a blockref is added or deleted the related chain element is marked
91  * modified and all of its parents are marked SUBMODIFIED (the parent
92  * recursion can stop once we hit a node that is already marked SUBMODIFIED).
93  * A deleted chain element must remain intact until synchronized against
94  * its parent.
95  *
96  * The blockref at (parent, index) is not adjusted until the modified chain
97  * element is flushed and unmarked.  Until then the child's blockref may
98  * not match the blockref at (parent, index).
99  */
100 SPLAY_HEAD(hammer2_chain_splay, hammer2_chain);
101
102 struct hammer2_chain {
103         ccms_cst_t      cst;                    /* attr or data cst */
104         struct hammer2_blockref bref;
105         struct hammer2_blockref bref_flush;     /* synchronized w/MOVED bit */
106         struct hammer2_chain *parent;           /* return chain to root */
107         struct hammer2_chain_splay shead;
108         SPLAY_ENTRY(hammer2_chain) snode;
109         TAILQ_ENTRY(hammer2_chain) flush_node;  /* flush deferral list */
110         union {
111                 struct hammer2_inode *ip;
112                 struct hammer2_indblock *np;
113                 struct hammer2_data *dp;
114                 void *mem;
115         } u;
116
117         struct buf      *bp;            /* buffer cache (ro) */
118         hammer2_media_data_t *data;     /* modified copy of data (rw) */
119         u_int           bytes;          /* physical size of data */
120         int             index;          /* index in parent */
121         u_int           refs;
122         u_int           busy;           /* soft-busy */
123         u_int           flags;
124 };
125
126 typedef struct hammer2_chain hammer2_chain_t;
127
128 int hammer2_chain_cmp(hammer2_chain_t *chain1, hammer2_chain_t *chain2);
129 SPLAY_PROTOTYPE(hammer2_chain_splay, hammer2_chain, snode, hammer2_chain_cmp);
130
131 /*
132  * MOVED - This bit is set during the flush when the MODIFIED bit is cleared,
133  *         indicating that the parent's blocktable must inherit a change to
134  *         the bref (typically a block reallocation)
135  *
136  *         It must also be set in situations where a chain is not MODIFIED
137  *         but whos bref has changed (typically due to fields other than
138  *         a block reallocation).
139  */
140 #define HAMMER2_CHAIN_MODIFIED          0x00000001      /* active mods */
141 #define HAMMER2_CHAIN_DIRTYEMBED        0x00000002      /* inode embedded */
142 #define HAMMER2_CHAIN_DIRTYBP           0x00000004      /* dirty on unlock */
143 #define HAMMER2_CHAIN_SUBMODIFIED       0x00000008      /* 1+ subs modified */
144 #define HAMMER2_CHAIN_DELETED           0x00000010
145 #define HAMMER2_CHAIN_INITIAL           0x00000020      /* initial create */
146 #define HAMMER2_CHAIN_FLUSHED           0x00000040      /* flush on unlock */
147 #define HAMMER2_CHAIN_MOVED             0x00000080      /* bref changed */
148 #define HAMMER2_CHAIN_IOFLUSH           0x00000100      /* bawrite on put */
149 #define HAMMER2_CHAIN_DEFERRED          0x00000200      /* on a deferral list*/
150 #define HAMMER2_CHAIN_DESTROYED         0x00000400      /* destroying */
151 #define HAMMER2_CHAIN_MODIFIED_AUX      0x00000800      /* hmp->vchain only */
152 #define HAMMER2_CHAIN_MODIFY_TID        0x00001000      /* mod updates field */
153 #define HAMMER2_CHAIN_MOUNTED           0x00002000      /* PFS is mounted */
154
155 /*
156  * Flags passed to hammer2_chain_lookup() and hammer2_chain_next()
157  */
158 #define HAMMER2_LOOKUP_NOLOCK           0x00000001      /* ref only */
159 #define HAMMER2_LOOKUP_NODATA           0x00000002      /* data left NULL */
160
161 /*
162  * Flags passed to hammer2_chain_modify() and hammer2_chain_resize()
163  *
164  * NOTE: OPTDATA allows us to avoid instantiating buffers for INDIRECT
165  *       blocks in the INITIAL-create state.
166  *
167  * NOTE: NO_MODIFY_TID tells the function to not set HAMMER2_CHAIN_MODIFY_TID
168  *       when marking the chain modified (used when a sub-chain modification
169  *       propagates upward).
170  */
171 #define HAMMER2_MODIFY_NOSUB            0x00000001      /* do not set SUBMOD */
172 #define HAMMER2_MODIFY_OPTDATA          0x00000002      /* data can be NULL */
173 #define HAMMER2_MODIFY_NO_MODIFY_TID    0x00000004
174
175 /*
176  * Flags passed to hammer2_chain_lock()
177  */
178 #define HAMMER2_RESOLVE_NEVER           1
179 #define HAMMER2_RESOLVE_MAYBE           2
180 #define HAMMER2_RESOLVE_ALWAYS          3
181
182 /*
183  * Cluster different types of storage together for allocations
184  */
185 #define HAMMER2_FREECACHE_INODE         0
186 #define HAMMER2_FREECACHE_INDIR         1
187 #define HAMMER2_FREECACHE_DATA          2
188 #define HAMMER2_FREECACHE_UNUSED3       3
189 #define HAMMER2_FREECACHE_TYPES         4
190
191 /*
192  * BMAP read-ahead maximum parameters
193  */
194 #define HAMMER2_BMAP_COUNT              16      /* max bmap read-ahead */
195 #define HAMMER2_BMAP_BYTES              (HAMMER2_PBUFSIZE * HAMMER2_BMAP_COUNT)
196
197 /*
198  * Misc
199  */
200 #define HAMMER2_FLUSH_DEPTH_LIMIT       40      /* stack recursion limit */
201
202 /*
203  * HAMMER2 IN-MEMORY CACHE OF MEDIA STRUCTURES
204  *
205  * There is an in-memory representation of all on-media data structure.
206  *
207  * When accessed read-only the data will be mapped to the related buffer
208  * cache buffer.
209  *
210  * When accessed read-write (marked modified) a kmalloc()'d copy of the
211  * is created which can then be modified.  The copy is destroyed when a
212  * filesystem block is allocated to replace it.
213  *
214  * Active inodes (those with vnodes attached) will maintain the kmalloc()'d
215  * copy for both the read-only and the read-write case.  The combination of
216  * (bp) and (data) determines whether (data) was allocated or not.
217  *
218  * The in-memory representation may remain cached (for example in order to
219  * placemark clustering locks) even after the related data has been
220  * detached.
221  */
222
223 /*
224  * A hammer2 inode.
225  *
226  * NOTE: The inode's attribute CST which is also used to lock the inode
227  *       is embedded in the chain (chain.cst) and aliased w/ attr_cst.
228  */
229 struct hammer2_inode {
230         struct hammer2_mount    *hmp;           /* Global mount */
231         struct hammer2_pfsmount *pmp;           /* PFS mount */
232         struct hammer2_inode    *pip;           /* parent inode */
233         struct vnode            *vp;
234         ccms_cst_t              topo_cst;       /* directory topology cst */
235         hammer2_chain_t         chain;
236         struct hammer2_inode_data ip_data;
237         struct lockf            advlock;
238         u_int                   depth;          /* directory depth */
239         hammer2_off_t           delta_dcount;   /* adjust data_count */
240         hammer2_off_t           delta_icount;   /* adjust inode_count */
241 };
242
243 typedef struct hammer2_inode hammer2_inode_t;
244
245 #if defined(_KERNEL)
246
247 #define attr_cst        chain.cst
248
249 #endif
250
251 /*
252  * A hammer2 indirect block
253  */
254 struct hammer2_indblock {
255         hammer2_chain_t         chain;
256 };
257
258 typedef struct hammer2_indblock hammer2_indblock_t;
259
260 /*
261  * A hammer2 data block
262  */
263 struct hammer2_data {
264         hammer2_chain_t         chain;
265 };
266
267 typedef struct hammer2_data hammer2_data_t;
268
269 struct hammer2_freecache {
270         hammer2_off_t   bulk;
271         hammer2_off_t   single;
272 };
273
274 typedef struct hammer2_freecache hammer2_freecache_t;
275
276 /*
277  * Global (per device) mount structure for device (aka vp->v_mount->hmp)
278  */
279 struct hammer2_mount {
280         struct vnode    *devvp;         /* device vnode */
281         int             ronly;          /* read-only mount */
282         int             pmp_count;      /* PFS mounts backed by us */
283         TAILQ_ENTRY(hammer2_mount) mntentry; /* hammer2_mntlist */
284
285         struct malloc_type *minode;
286         int             ninodes;
287         int             maxinodes;
288
289         struct malloc_type *mchain;
290         int             nipstacks;
291         int             maxipstacks;
292         hammer2_chain_t vchain;         /* anchor chain */
293         hammer2_chain_t *schain;        /* super-root */
294         struct lock     alloclk;        /* lockmgr lock */
295         struct lock     voldatalk;      /* lockmgr lock */
296
297         hammer2_volume_data_t voldata;
298         hammer2_freecache_t freecache[HAMMER2_FREECACHE_TYPES][HAMMER2_MAX_RADIX+1];
299 };
300
301 typedef struct hammer2_mount hammer2_mount_t;
302
303 /*
304  * Per-PFS mount structure for device (aka vp->v_mount)
305  */
306 struct hammer2_pfsmount {
307         struct mount            *mp;            /* kernel mount */
308         struct hammer2_mount    *hmp;           /* device global mount */
309         hammer2_chain_t         *rchain;        /* PFS root chain */
310         hammer2_inode_t         *iroot;         /* PFS root inode */
311         ccms_domain_t           ccms_dom;
312         struct netexport        export;         /* nfs export */
313         int                     ronly;          /* read-only mount */
314 };
315
316 typedef struct hammer2_pfsmount hammer2_pfsmount_t;
317
318 #if defined(_KERNEL)
319
320 MALLOC_DECLARE(M_HAMMER2);
321
322 #define VTOI(vp)        ((hammer2_inode_t *)(vp)->v_data)
323 #define ITOV(ip)        ((ip)->vp)
324
325 static __inline
326 hammer2_pfsmount_t *
327 MPTOPMP(struct mount *mp)
328 {
329         return ((hammer2_pfsmount_t *)mp->mnt_data);
330 }
331
332 static __inline
333 hammer2_mount_t *
334 MPTOHMP(struct mount *mp)
335 {
336         return (((hammer2_pfsmount_t *)mp->mnt_data)->hmp);
337 }
338
339 extern struct vop_ops hammer2_vnode_vops;
340 extern struct vop_ops hammer2_spec_vops;
341 extern struct vop_ops hammer2_fifo_vops;
342
343 extern int hammer2_debug;
344 extern int hammer2_cluster_enable;
345 extern int hammer2_hardlink_enable;
346 extern long hammer2_iod_file_read;
347 extern long hammer2_iod_meta_read;
348 extern long hammer2_iod_indr_read;
349 extern long hammer2_iod_file_write;
350 extern long hammer2_iod_meta_write;
351 extern long hammer2_iod_indr_write;
352 extern long hammer2_iod_volu_write;
353 extern long hammer2_ioa_file_read;
354 extern long hammer2_ioa_meta_read;
355 extern long hammer2_ioa_indr_read;
356 extern long hammer2_ioa_file_write;
357 extern long hammer2_ioa_meta_write;
358 extern long hammer2_ioa_indr_write;
359 extern long hammer2_ioa_volu_write;
360
361 /*
362  * hammer2_subr.c
363  */
364 void hammer2_inode_lock_ex(hammer2_inode_t *ip);
365 void hammer2_inode_unlock_ex(hammer2_inode_t *ip);
366 void hammer2_inode_lock_sh(hammer2_inode_t *ip);
367 void hammer2_inode_unlock_sh(hammer2_inode_t *ip);
368 void hammer2_inode_busy(hammer2_inode_t *ip);
369 void hammer2_inode_unbusy(hammer2_inode_t *ip);
370 void hammer2_voldata_lock(hammer2_mount_t *hmp);
371 void hammer2_voldata_unlock(hammer2_mount_t *hmp);
372
373 void hammer2_mount_exlock(hammer2_mount_t *hmp);
374 void hammer2_mount_shlock(hammer2_mount_t *hmp);
375 void hammer2_mount_unlock(hammer2_mount_t *hmp);
376
377 int hammer2_get_dtype(hammer2_inode_t *ip);
378 int hammer2_get_vtype(hammer2_inode_t *ip);
379 u_int8_t hammer2_get_obj_type(enum vtype vtype);
380 void hammer2_time_to_timespec(u_int64_t xtime, struct timespec *ts);
381 u_int64_t hammer2_timespec_to_time(struct timespec *ts);
382 u_int32_t hammer2_to_unix_xid(uuid_t *uuid);
383 void hammer2_guid_to_uuid(uuid_t *uuid, u_int32_t guid);
384
385 hammer2_key_t hammer2_dirhash(const unsigned char *name, size_t len);
386 int hammer2_bytes_to_radix(size_t bytes);
387
388 int hammer2_calc_logical(hammer2_inode_t *ip, hammer2_off_t uoff,
389                          hammer2_key_t *lbasep, hammer2_key_t *leofp);
390 void hammer2_update_time(uint64_t *timep);
391
392 /*
393  * hammer2_inode.c
394  */
395 struct vnode *hammer2_igetv(hammer2_inode_t *ip, int *errorp);
396
397 void hammer2_inode_lock_nlinks(hammer2_inode_t *ip);
398 void hammer2_inode_unlock_nlinks(hammer2_inode_t *ip);
399 hammer2_inode_t *hammer2_inode_alloc(hammer2_pfsmount_t *pmp, void *data);
400 void hammer2_inode_free(hammer2_inode_t *ip);
401 void hammer2_inode_ref(hammer2_inode_t *ip);
402 void hammer2_inode_drop(hammer2_inode_t *ip);
403 int hammer2_inode_calc_alloc(hammer2_key_t filesize);
404
405 int hammer2_inode_create(hammer2_inode_t *dip,
406                         struct vattr *vap, struct ucred *cred,
407                         const uint8_t *name, size_t name_len,
408                         hammer2_inode_t **nipp);
409
410 int hammer2_inode_duplicate(hammer2_inode_t *dip,
411                         hammer2_inode_t *oip, hammer2_inode_t **nipp,
412                         const uint8_t *name, size_t name_len);
413 int hammer2_inode_connect(hammer2_inode_t *dip, hammer2_inode_t *oip,
414                         const uint8_t *name, size_t name_len);
415
416 int hammer2_unlink_file(hammer2_inode_t *dip,
417                         const uint8_t *name, size_t name_len,
418                         int isdir, hammer2_inode_t *retain_ip);
419 int hammer2_hardlink_consolidate(hammer2_inode_t **ipp, hammer2_inode_t *tdip);
420 int hammer2_hardlink_deconsolidate(hammer2_inode_t *dip,
421                         hammer2_chain_t **chainp, hammer2_inode_t **ipp);
422 int hammer2_hardlink_find(hammer2_inode_t *dip, hammer2_chain_t **chainp,
423                         hammer2_inode_t **ipp);
424
425 /*
426  * hammer2_chain.c
427  */
428 void hammer2_modify_volume(hammer2_mount_t *hmp);
429 hammer2_chain_t *hammer2_chain_alloc(hammer2_mount_t *hmp,
430                                 hammer2_blockref_t *bref);
431 void hammer2_chain_free(hammer2_mount_t *hmp, hammer2_chain_t *chain);
432 void hammer2_chain_ref(hammer2_mount_t *hmp, hammer2_chain_t *chain);
433 void hammer2_chain_drop(hammer2_mount_t *hmp, hammer2_chain_t *chain);
434 int hammer2_chain_lock(hammer2_mount_t *hmp, hammer2_chain_t *chain, int how);
435 void hammer2_chain_moved(hammer2_mount_t *hmp, hammer2_chain_t *chain);
436 void hammer2_chain_modify(hammer2_mount_t *hmp, hammer2_chain_t *chain,
437                                 int flags);
438 void hammer2_chain_resize(hammer2_inode_t *ip, hammer2_chain_t *chain,
439                                 int nradix, int flags);
440 void hammer2_chain_unlock(hammer2_mount_t *hmp, hammer2_chain_t *chain);
441 hammer2_chain_t *hammer2_chain_find(hammer2_mount_t *hmp,
442                                 hammer2_chain_t *parent, int index);
443 hammer2_chain_t *hammer2_chain_get(hammer2_mount_t *hmp,
444                                 hammer2_chain_t *parent,
445                                 int index, int flags);
446 hammer2_chain_t *hammer2_chain_lookup(hammer2_mount_t *hmp,
447                                 hammer2_chain_t **parentp,
448                                 hammer2_key_t key_beg, hammer2_key_t key_end,
449                                 int flags);
450 hammer2_chain_t *hammer2_chain_next(hammer2_mount_t *hmp,
451                                 hammer2_chain_t **parentp,
452                                 hammer2_chain_t *chain,
453                                 hammer2_key_t key_beg, hammer2_key_t key_end,
454                                 int flags);
455 hammer2_chain_t *hammer2_chain_create(hammer2_mount_t *hmp,
456                                 hammer2_chain_t *parent,
457                                 hammer2_chain_t *chain,
458                                 hammer2_key_t key, int keybits,
459                                 int type, size_t bytes);
460 void hammer2_chain_delete(hammer2_mount_t *hmp, hammer2_chain_t *parent,
461                                 hammer2_chain_t *chain, int retain);
462 void hammer2_chain_flush(hammer2_mount_t *hmp, hammer2_chain_t *chain,
463                                 hammer2_tid_t modify_tid);
464 void hammer2_chain_commit(hammer2_mount_t *hmp, hammer2_chain_t *chain);
465
466 /*
467  * hammer2_ioctl.c
468  */
469 int hammer2_ioctl(hammer2_inode_t *ip, u_long com, void *data,
470                                 int fflag, struct ucred *cred);
471
472 /*
473  * hammer2_freemap.c
474  */
475 hammer2_off_t hammer2_freemap_alloc(hammer2_mount_t *hmp,
476                                 int type, size_t bytes);
477 void hammer2_freemap_free(hammer2_mount_t *hmp, hammer2_off_t data_off,
478                                 int type);
479
480 #endif /* !_KERNEL */
481 #endif /* !_VFS_HAMMER2_HAMMER2_H_ */