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