2 * Copyright (c) 2011-2012 The DragonFly Project. All rights reserved.
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>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
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
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.
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
37 * This header file contains structures used internally by the HAMMER2
38 * implementation. See hammer2_disk.h for on-disk structures.
41 #ifndef _VFS_HAMMER2_HAMMER2_H_
42 #define _VFS_HAMMER2_HAMMER2_H_
44 #include <sys/param.h>
45 #include <sys/types.h>
46 #include <sys/kernel.h>
48 #include <sys/systm.h>
50 #include <sys/malloc.h>
51 #include <sys/mount.h>
52 #include <sys/vnode.h>
54 #include <sys/mountctl.h>
57 #include <sys/thread.h>
58 #include <sys/globaldata.h>
59 #include <sys/lockf.h>
61 #include <sys/queue.h>
62 #include <sys/limits.h>
64 #include <sys/signal2.h>
68 #include "hammer2_disk.h"
69 #include "hammer2_mount.h"
70 #include "hammer2_ioctl.h"
71 #include "hammer2_ccms.h"
76 struct hammer2_pfsmount;
82 * The chain structure tracks blockref recursions all the way to
83 * the root volume. These consist of indirect blocks, inodes,
84 * and eventually the volume header.
86 * The chain structure is embedded in the hammer2_mount, hammer2_inode,
87 * and other system memory structures. The chain structure typically
88 * implements the reference count and busy flag for the larger structure.
90 * It is always possible to track a chain element all the way back to the
91 * root by following the (parent) links. (index) is a type-dependent index
92 * in the parent indicating where in the parent the chain element resides.
94 * When a blockref is added or deleted the related chain element is marked
95 * modified and all of its parents are marked SUBMODIFIED (the parent
96 * recursion can stop once we hit a node that is already marked SUBMODIFIED).
97 * A deleted chain element must remain intact until synchronized against
100 * The blockref at (parent, index) is not adjusted until the modified chain
101 * element is flushed and unmarked. Until then the child's blockref may
102 * not match the blockref at (parent, index).
104 RB_HEAD(hammer2_chain_tree, hammer2_chain);
105 TAILQ_HEAD(flush_deferral_list, hammer2_chain);
107 struct hammer2_chain {
108 ccms_cst_t cst; /* attr or data cst */
109 struct hammer2_blockref bref;
110 struct hammer2_blockref bref_flush; /* synchronized w/MOVED bit */
111 struct hammer2_chain *parent; /* return chain to root */
112 struct hammer2_chain_tree rbhead;
113 struct hammer2_state *state; /* if active cache msg */
114 RB_ENTRY(hammer2_chain) rbnode;
115 TAILQ_ENTRY(hammer2_chain) flush_node; /* flush deferral list */
117 void *mem; /* generic */
118 struct hammer2_inode *ip; /* high-level h2 structure */
121 struct buf *bp; /* buffer cache (ro) */
122 hammer2_media_data_t *data; /* modified copy of data (rw) */
123 u_int bytes; /* physical size of data */
124 int index; /* index in parent */
125 u_int flushing; /* element undergoing flush (count) */
130 typedef struct hammer2_chain hammer2_chain_t;
132 int hammer2_chain_cmp(hammer2_chain_t *chain1, hammer2_chain_t *chain2);
133 RB_PROTOTYPE(hammer2_chain_tree, hammer2_chain, rbnode, hammer2_chain_cmp);
136 * MOVED - This bit is set during the flush when the MODIFIED bit is cleared,
137 * indicating that the parent's blocktable must inherit a change to
138 * the bref (typically a block reallocation)
140 * It must also be set in situations where a chain is not MODIFIED
141 * but whos bref has changed (typically due to fields other than
142 * a block reallocation).
144 #define HAMMER2_CHAIN_MODIFIED 0x00000001 /* active mods */
145 #define HAMMER2_CHAIN_UNUSED0002 0x00000002
146 #define HAMMER2_CHAIN_DIRTYBP 0x00000004 /* dirty on unlock */
147 #define HAMMER2_CHAIN_SUBMODIFIED 0x00000008 /* 1+ subs modified */
148 #define HAMMER2_CHAIN_DELETED 0x00000010 /* deleted chain */
149 #define HAMMER2_CHAIN_INITIAL 0x00000020 /* initial create */
150 #define HAMMER2_CHAIN_FLUSHED 0x00000040 /* flush on unlock */
151 #define HAMMER2_CHAIN_MOVED 0x00000080 /* bref changed */
152 #define HAMMER2_CHAIN_IOFLUSH 0x00000100 /* bawrite on put */
153 #define HAMMER2_CHAIN_DEFERRED 0x00000200 /* on a deferral list*/
154 #define HAMMER2_CHAIN_DESTROYED 0x00000400 /* destroying inode */
155 #define HAMMER2_CHAIN_MODIFIED_AUX 0x00000800 /* hmp->vchain only */
156 #define HAMMER2_CHAIN_MODIFY_TID 0x00001000 /* mod updates field */
157 #define HAMMER2_CHAIN_MOUNTED 0x00002000 /* PFS is mounted */
158 #define HAMMER2_CHAIN_ONRBTREE 0x00004000 /* on parent RB tree */
161 * Flags passed to hammer2_chain_lookup() and hammer2_chain_next()
163 #define HAMMER2_LOOKUP_NOLOCK 0x00000001 /* ref only */
164 #define HAMMER2_LOOKUP_NODATA 0x00000002 /* data left NULL */
165 #define HAMMER2_LOOKUP_SHARED 0x00000100
168 * Flags passed to hammer2_chain_modify() and hammer2_chain_resize()
170 * NOTE: OPTDATA allows us to avoid instantiating buffers for INDIRECT
171 * blocks in the INITIAL-create state.
173 * NOTE: NO_MODIFY_TID tells the function to not set HAMMER2_CHAIN_MODIFY_TID
174 * when marking the chain modified (used when a sub-chain modification
175 * propagates upward).
177 #define HAMMER2_MODIFY_NOSUB 0x00000001 /* do not set SUBMOD */
178 #define HAMMER2_MODIFY_OPTDATA 0x00000002 /* data can be NULL */
179 #define HAMMER2_MODIFY_NO_MODIFY_TID 0x00000004
182 * Flags passed to hammer2_chain_lock()
184 #define HAMMER2_RESOLVE_NEVER 1
185 #define HAMMER2_RESOLVE_MAYBE 2
186 #define HAMMER2_RESOLVE_ALWAYS 3
187 #define HAMMER2_RESOLVE_MASK 0x0F
189 #define HAMMER2_RESOLVE_SHARED 0x10
192 * Cluster different types of storage together for allocations
194 #define HAMMER2_FREECACHE_INODE 0
195 #define HAMMER2_FREECACHE_INDIR 1
196 #define HAMMER2_FREECACHE_DATA 2
197 #define HAMMER2_FREECACHE_UNUSED3 3
198 #define HAMMER2_FREECACHE_TYPES 4
201 * BMAP read-ahead maximum parameters
203 #define HAMMER2_BMAP_COUNT 16 /* max bmap read-ahead */
204 #define HAMMER2_BMAP_BYTES (HAMMER2_PBUFSIZE * HAMMER2_BMAP_COUNT)
209 #define HAMMER2_FLUSH_DEPTH_LIMIT 40 /* stack recursion limit */
212 * HAMMER2 IN-MEMORY CACHE OF MEDIA STRUCTURES
214 * There is an in-memory representation of all on-media data structure.
216 * When accessed read-only the data will be mapped to the related buffer
219 * When accessed read-write (marked modified) a kmalloc()'d copy of the
220 * is created which can then be modified. The copy is destroyed when a
221 * filesystem block is allocated to replace it.
223 * Active inodes (those with vnodes attached) will maintain the kmalloc()'d
224 * copy for both the read-only and the read-write case. The combination of
225 * (bp) and (data) determines whether (data) was allocated or not.
227 * The in-memory representation may remain cached (for example in order to
228 * placemark clustering locks) even after the related data has been
235 * NOTE: The inode's attribute CST which is also used to lock the inode
236 * is embedded in the chain (chain.cst) and aliased w/ attr_cst.
238 struct hammer2_inode {
239 ccms_cst_t topo_cst; /* directory topology cst */
240 struct hammer2_mount *hmp; /* Global mount */
241 struct hammer2_pfsmount *pmp; /* PFS mount */
242 struct hammer2_inode *pip; /* parent inode */
244 hammer2_chain_t *chain;
245 struct lockf advlock;
248 u_int refs; /* +vpref, +flushref */
251 typedef struct hammer2_inode hammer2_inode_t;
253 #define HAMMER2_INODE_MODIFIED 0x0001
254 #define HAMMER2_INODE_DIRTYEMBED 0x0002
255 #define HAMMER2_INODE_DELETED 0x0004
260 struct hammer2_freecache {
262 hammer2_off_t single;
265 typedef struct hammer2_freecache hammer2_freecache_t;
268 * Global (per device) mount structure for device (aka vp->v_mount->hmp)
270 struct hammer2_mount {
271 struct vnode *devvp; /* device vnode */
272 int ronly; /* read-only mount */
273 int pmp_count; /* PFS mounts backed by us */
274 TAILQ_ENTRY(hammer2_mount) mntentry; /* hammer2_mntlist */
276 struct malloc_type *minode;
280 struct malloc_type *mchain;
283 hammer2_chain_t vchain; /* anchor chain */
284 hammer2_chain_t *schain; /* super-root */
285 struct lock alloclk; /* lockmgr lock */
286 struct lock voldatalk; /* lockmgr lock */
288 int volhdrno; /* last volhdrno written */
289 hammer2_volume_data_t voldata;
290 hammer2_volume_data_t volsync; /* synchronized voldata */
291 hammer2_freecache_t freecache[HAMMER2_FREECACHE_TYPES]
292 [HAMMER2_MAX_RADIX+1];
295 typedef struct hammer2_mount hammer2_mount_t;
298 * Per-PFS mount structure for device (aka vp->v_mount)
300 struct hammer2_pfsmount {
301 struct mount *mp; /* kernel mount */
302 struct hammer2_mount *hmp; /* device global mount */
303 hammer2_chain_t *rchain; /* PFS root chain */
304 hammer2_inode_t *iroot; /* PFS root inode */
305 hammer2_off_t inode_count; /* copy of inode_count */
306 ccms_domain_t ccms_dom;
307 struct netexport export; /* nfs export */
308 int ronly; /* read-only mount */
309 struct malloc_type *mmsg;
313 typedef struct hammer2_pfsmount hammer2_pfsmount_t;
317 MALLOC_DECLARE(M_HAMMER2);
319 #define VTOI(vp) ((hammer2_inode_t *)(vp)->v_data)
320 #define ITOV(ip) ((ip)->vp)
324 MPTOPMP(struct mount *mp)
326 return ((hammer2_pfsmount_t *)mp->mnt_data);
331 MPTOHMP(struct mount *mp)
333 return (((hammer2_pfsmount_t *)mp->mnt_data)->hmp);
336 extern struct vop_ops hammer2_vnode_vops;
337 extern struct vop_ops hammer2_spec_vops;
338 extern struct vop_ops hammer2_fifo_vops;
340 extern int hammer2_debug;
341 extern int hammer2_cluster_enable;
342 extern int hammer2_hardlink_enable;
343 extern long hammer2_iod_file_read;
344 extern long hammer2_iod_meta_read;
345 extern long hammer2_iod_indr_read;
346 extern long hammer2_iod_file_write;
347 extern long hammer2_iod_meta_write;
348 extern long hammer2_iod_indr_write;
349 extern long hammer2_iod_fmap_write;
350 extern long hammer2_iod_volu_write;
351 extern long hammer2_ioa_file_read;
352 extern long hammer2_ioa_meta_read;
353 extern long hammer2_ioa_indr_read;
354 extern long hammer2_ioa_file_write;
355 extern long hammer2_ioa_meta_write;
356 extern long hammer2_ioa_indr_write;
357 extern long hammer2_ioa_fmap_write;
358 extern long hammer2_ioa_volu_write;
363 #define hammer2_icrc32(buf, size) iscsi_crc32((buf), (size))
364 #define hammer2_icrc32c(buf, size, crc) iscsi_crc32_ext((buf), (size), (crc))
366 hammer2_chain_t *hammer2_inode_lock_ex(hammer2_inode_t *ip);
367 hammer2_chain_t *hammer2_inode_lock_sh(hammer2_inode_t *ip);
368 void hammer2_inode_unlock_ex(hammer2_inode_t *ip, hammer2_chain_t *chain);
369 void hammer2_inode_unlock_sh(hammer2_inode_t *ip, hammer2_chain_t *chain);
370 void hammer2_voldata_lock(hammer2_mount_t *hmp);
371 void hammer2_voldata_unlock(hammer2_mount_t *hmp);
372 ccms_state_t hammer2_inode_lock_temp_release(hammer2_inode_t *ip);
373 ccms_state_t hammer2_inode_lock_upgrade(hammer2_inode_t *ip);
374 void hammer2_inode_lock_restore(hammer2_inode_t *ip, ccms_state_t ostate);
376 void hammer2_mount_exlock(hammer2_mount_t *hmp);
377 void hammer2_mount_shlock(hammer2_mount_t *hmp);
378 void hammer2_mount_unlock(hammer2_mount_t *hmp);
380 int hammer2_get_dtype(hammer2_chain_t *chain);
381 int hammer2_get_vtype(hammer2_chain_t *chain);
382 u_int8_t hammer2_get_obj_type(enum vtype vtype);
383 void hammer2_time_to_timespec(u_int64_t xtime, struct timespec *ts);
384 u_int64_t hammer2_timespec_to_time(struct timespec *ts);
385 u_int32_t hammer2_to_unix_xid(uuid_t *uuid);
386 void hammer2_guid_to_uuid(uuid_t *uuid, u_int32_t guid);
388 hammer2_key_t hammer2_dirhash(const unsigned char *name, size_t len);
389 int hammer2_allocsize(size_t bytes);
391 int hammer2_calc_logical(hammer2_inode_t *ip, hammer2_off_t uoff,
392 hammer2_key_t *lbasep, hammer2_key_t *leofp);
393 void hammer2_update_time(uint64_t *timep);
398 struct vnode *hammer2_igetv(hammer2_inode_t *ip, int *errorp);
400 void hammer2_inode_lock_nlinks(hammer2_inode_t *ip);
401 void hammer2_inode_unlock_nlinks(hammer2_inode_t *ip);
402 hammer2_inode_t *hammer2_inode_get(hammer2_pfsmount_t *pmp,
403 hammer2_inode_t *dip, hammer2_chain_t *chain);
404 void hammer2_inode_put(hammer2_inode_t *ip, hammer2_chain_t *chain);
405 void hammer2_inode_free(hammer2_inode_t *ip);
406 void hammer2_inode_ref(hammer2_inode_t *ip);
407 void hammer2_inode_drop(hammer2_inode_t *ip);
408 int hammer2_inode_calc_alloc(hammer2_key_t filesize);
410 int hammer2_inode_create(hammer2_inode_t *dip,
411 struct vattr *vap, struct ucred *cred,
412 const uint8_t *name, size_t name_len,
413 hammer2_inode_t **nipp, hammer2_chain_t **nchainp);
415 int hammer2_inode_duplicate(hammer2_inode_t *dip, hammer2_inode_t *oip,
416 hammer2_inode_t **nipp, hammer2_chain_t **nchainp,
417 const uint8_t *name, size_t name_len);
418 int hammer2_inode_connect(hammer2_inode_t *dip, hammer2_inode_t *oip,
419 const uint8_t *name, size_t name_len);
420 hammer2_inode_t *hammer2_inode_common_parent(hammer2_mount_t *hmp,
421 hammer2_inode_t *fdip, hammer2_inode_t *tdip);
423 int hammer2_unlink_file(hammer2_inode_t *dip,
424 const uint8_t *name, size_t name_len,
425 int isdir, hammer2_inode_t *retain_ip);
426 int hammer2_hardlink_consolidate(hammer2_inode_t **ipp, hammer2_inode_t *tdip);
427 int hammer2_hardlink_deconsolidate(hammer2_inode_t *dip,
428 hammer2_chain_t **chainp, hammer2_chain_t **ochainp);
429 int hammer2_hardlink_find(hammer2_inode_t *dip,
430 hammer2_chain_t **chainp, hammer2_chain_t **ochainp);
435 void hammer2_modify_volume(hammer2_mount_t *hmp);
436 hammer2_chain_t *hammer2_chain_alloc(hammer2_mount_t *hmp,
437 hammer2_blockref_t *bref);
438 void hammer2_chain_free(hammer2_mount_t *hmp, hammer2_chain_t *chain);
439 void hammer2_chain_ref(hammer2_mount_t *hmp, hammer2_chain_t *chain);
440 void hammer2_chain_drop(hammer2_mount_t *hmp, hammer2_chain_t *chain);
441 int hammer2_chain_lock(hammer2_mount_t *hmp, hammer2_chain_t *chain, int how);
442 void hammer2_chain_moved(hammer2_mount_t *hmp, hammer2_chain_t *chain);
443 void hammer2_chain_modify(hammer2_mount_t *hmp, hammer2_chain_t *chain,
445 void hammer2_chain_resize(hammer2_inode_t *ip, hammer2_chain_t *chain,
446 int nradix, int flags);
447 void hammer2_chain_unlock(hammer2_mount_t *hmp, hammer2_chain_t *chain);
448 void hammer2_chain_wait(hammer2_mount_t *hmp, hammer2_chain_t *chain);
449 hammer2_chain_t *hammer2_chain_find(hammer2_mount_t *hmp,
450 hammer2_chain_t *parent, int index);
451 hammer2_chain_t *hammer2_chain_get(hammer2_mount_t *hmp,
452 hammer2_chain_t *parent,
453 int index, int flags);
454 hammer2_chain_t *hammer2_chain_lookup(hammer2_mount_t *hmp,
455 hammer2_chain_t **parentp,
456 hammer2_key_t key_beg, hammer2_key_t key_end,
458 hammer2_chain_t *hammer2_chain_next(hammer2_mount_t *hmp,
459 hammer2_chain_t **parentp,
460 hammer2_chain_t *chain,
461 hammer2_key_t key_beg, hammer2_key_t key_end,
463 hammer2_chain_t *hammer2_chain_create(hammer2_mount_t *hmp,
464 hammer2_chain_t *parent,
465 hammer2_chain_t *chain,
466 hammer2_key_t key, int keybits,
467 int type, size_t bytes,
469 void hammer2_chain_delete(hammer2_mount_t *hmp, hammer2_chain_t *parent,
470 hammer2_chain_t *chain, int retain);
471 void hammer2_chain_flush(hammer2_mount_t *hmp, hammer2_chain_t *chain,
472 hammer2_tid_t modify_tid);
473 void hammer2_chain_commit(hammer2_mount_t *hmp, hammer2_chain_t *chain);
474 void hammer2_chain_parent_setsubmod(hammer2_mount_t *hmp,
475 hammer2_chain_t *chain);
480 int hammer2_ioctl(hammer2_inode_t *ip, u_long com, void *data,
481 int fflag, struct ucred *cred);
486 int hammer2_msg_dbg_rcvmsg(kdmsg_msg_t *msg);
487 int hammer2_msg_adhoc_input(kdmsg_msg_t *msg);
492 void hammer2_clusterctl_wakeup(kdmsg_iocom_t *iocom);
493 void hammer2_volconf_update(hammer2_pfsmount_t *pmp, int index);
494 void hammer2_cluster_reconnect(hammer2_pfsmount_t *pmp, struct file *fp);
499 hammer2_off_t hammer2_freemap_alloc(hammer2_mount_t *hmp,
500 int type, size_t bytes);
501 void hammer2_freemap_free(hammer2_mount_t *hmp, hammer2_off_t data_off,
504 #endif /* !_KERNEL */
505 #endif /* !_VFS_HAMMER2_HAMMER2_H_ */