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