xref: /freebsd/usr.sbin/fstyp/hammer_disk.h (revision 42b38843)
1509798eaSPedro F. Giffuni /*-
2509798eaSPedro F. Giffuni  * Copyright (c) 2007 The DragonFly Project.  All rights reserved.
3509798eaSPedro F. Giffuni  *
4509798eaSPedro F. Giffuni  * This code is derived from software contributed to The DragonFly Project
5509798eaSPedro F. Giffuni  * by Matthew Dillon <dillon@backplane.com>
6509798eaSPedro F. Giffuni  *
7509798eaSPedro F. Giffuni  * Redistribution and use in source and binary forms, with or without
8509798eaSPedro F. Giffuni  * modification, are permitted provided that the following conditions
9509798eaSPedro F. Giffuni  * are met:
10509798eaSPedro F. Giffuni  *
11509798eaSPedro F. Giffuni  * 1. Redistributions of source code must retain the above copyright
12509798eaSPedro F. Giffuni  *    notice, this list of conditions and the following disclaimer.
13509798eaSPedro F. Giffuni  * 2. Redistributions in binary form must reproduce the above copyright
14509798eaSPedro F. Giffuni  *    notice, this list of conditions and the following disclaimer in
15509798eaSPedro F. Giffuni  *    the documentation and/or other materials provided with the
16509798eaSPedro F. Giffuni  *    distribution.
17509798eaSPedro F. Giffuni  * 3. Neither the name of The DragonFly Project nor the names of its
18509798eaSPedro F. Giffuni  *    contributors may be used to endorse or promote products derived
19509798eaSPedro F. Giffuni  *    from this software without specific, prior written permission.
20509798eaSPedro F. Giffuni  *
21509798eaSPedro F. Giffuni  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22509798eaSPedro F. Giffuni  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23509798eaSPedro F. Giffuni  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24509798eaSPedro F. Giffuni  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25509798eaSPedro F. Giffuni  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26509798eaSPedro F. Giffuni  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27509798eaSPedro F. Giffuni  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28509798eaSPedro F. Giffuni  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29509798eaSPedro F. Giffuni  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30509798eaSPedro F. Giffuni  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31509798eaSPedro F. Giffuni  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32509798eaSPedro F. Giffuni  * SUCH DAMAGE.
33509798eaSPedro F. Giffuni  *
34509798eaSPedro F. Giffuni  * $DragonFly: src/sys/vfs/hammer/hammer_disk.h,v 1.55 2008/11/13 02:18:43 dillon Exp $
35509798eaSPedro F. Giffuni  */
36509798eaSPedro F. Giffuni 
37509798eaSPedro F. Giffuni #ifndef VFS_HAMMER_DISK_H_
38509798eaSPedro F. Giffuni #define VFS_HAMMER_DISK_H_
39509798eaSPedro F. Giffuni 
40509798eaSPedro F. Giffuni #include <sys/endian.h>
41509798eaSPedro F. Giffuni 
42509798eaSPedro F. Giffuni #ifndef _SYS_UUID_H_
43509798eaSPedro F. Giffuni #include <sys/uuid.h>
44509798eaSPedro F. Giffuni #endif
45509798eaSPedro F. Giffuni 
46509798eaSPedro F. Giffuni /*
47509798eaSPedro F. Giffuni  * The structures below represent the on-disk format for a HAMMER
48509798eaSPedro F. Giffuni  * filesystem.  Note that all fields for on-disk structures are naturally
49509798eaSPedro F. Giffuni  * aligned.  HAMMER uses little endian for fields in on-disk structures.
50509798eaSPedro F. Giffuni  * HAMMER doesn't support big endian arch, but is planned.
51509798eaSPedro F. Giffuni  *
52509798eaSPedro F. Giffuni  * Most of HAMMER revolves around the concept of an object identifier.  An
53509798eaSPedro F. Giffuni  * obj_id is a 64 bit quantity which uniquely identifies a filesystem object
54509798eaSPedro F. Giffuni  * FOR THE ENTIRE LIFE OF THE FILESYSTEM.  This uniqueness allows backups
55509798eaSPedro F. Giffuni  * and mirrors to retain varying amounts of filesystem history by removing
56509798eaSPedro F. Giffuni  * any possibility of conflict through identifier reuse.
57509798eaSPedro F. Giffuni  *
58509798eaSPedro F. Giffuni  * A HAMMER filesystem may span multiple volumes.
59509798eaSPedro F. Giffuni  *
60509798eaSPedro F. Giffuni  * A HAMMER filesystem uses a 16K filesystem buffer size.  All filesystem
61509798eaSPedro F. Giffuni  * I/O is done in multiples of 16K.
62509798eaSPedro F. Giffuni  *
63509798eaSPedro F. Giffuni  * 64K X-bufs are used for blocks >= a file's 1MB mark.
64509798eaSPedro F. Giffuni  *
65509798eaSPedro F. Giffuni  * Per-volume storage limit: 52 bits		4096 TB
66509798eaSPedro F. Giffuni  * Per-Zone storage limit: 60 bits		1 MTB
67509798eaSPedro F. Giffuni  * Per-filesystem storage limit: 60 bits	1 MTB
68509798eaSPedro F. Giffuni  */
69509798eaSPedro F. Giffuni #define HAMMER_BUFSIZE		16384
70509798eaSPedro F. Giffuni #define HAMMER_XBUFSIZE		65536
71509798eaSPedro F. Giffuni #define HAMMER_HBUFSIZE		(HAMMER_BUFSIZE / 2)
72509798eaSPedro F. Giffuni #define HAMMER_XDEMARC		(1024 * 1024)
73509798eaSPedro F. Giffuni #define HAMMER_BUFMASK		(HAMMER_BUFSIZE - 1)
74509798eaSPedro F. Giffuni #define HAMMER_XBUFMASK		(HAMMER_XBUFSIZE - 1)
75509798eaSPedro F. Giffuni 
76509798eaSPedro F. Giffuni #define HAMMER_BUFSIZE64	((uint64_t)HAMMER_BUFSIZE)
77509798eaSPedro F. Giffuni #define HAMMER_BUFMASK64	((uint64_t)HAMMER_BUFMASK)
78509798eaSPedro F. Giffuni 
79509798eaSPedro F. Giffuni #define HAMMER_XBUFSIZE64	((uint64_t)HAMMER_XBUFSIZE)
80509798eaSPedro F. Giffuni #define HAMMER_XBUFMASK64	((uint64_t)HAMMER_XBUFMASK)
81509798eaSPedro F. Giffuni 
82509798eaSPedro F. Giffuni #define HAMMER_OFF_ZONE_MASK	0xF000000000000000ULL /* zone portion */
83509798eaSPedro F. Giffuni #define HAMMER_OFF_VOL_MASK	0x0FF0000000000000ULL /* volume portion */
84509798eaSPedro F. Giffuni #define HAMMER_OFF_SHORT_MASK	0x000FFFFFFFFFFFFFULL /* offset portion */
85509798eaSPedro F. Giffuni #define HAMMER_OFF_LONG_MASK	0x0FFFFFFFFFFFFFFFULL /* offset portion */
86509798eaSPedro F. Giffuni 
87509798eaSPedro F. Giffuni #define HAMMER_OFF_BAD		((hammer_off_t)-1)
88509798eaSPedro F. Giffuni 
89509798eaSPedro F. Giffuni #define HAMMER_BUFSIZE_DOALIGN(offset)				\
90509798eaSPedro F. Giffuni 	(((offset) + HAMMER_BUFMASK) & ~HAMMER_BUFMASK)
91509798eaSPedro F. Giffuni #define HAMMER_BUFSIZE64_DOALIGN(offset)			\
92509798eaSPedro F. Giffuni 	(((offset) + HAMMER_BUFMASK64) & ~HAMMER_BUFMASK64)
93509798eaSPedro F. Giffuni 
94509798eaSPedro F. Giffuni #define HAMMER_XBUFSIZE_DOALIGN(offset)				\
95509798eaSPedro F. Giffuni 	(((offset) + HAMMER_XBUFMASK) & ~HAMMER_XBUFMASK)
96509798eaSPedro F. Giffuni #define HAMMER_XBUFSIZE64_DOALIGN(offset)			\
97509798eaSPedro F. Giffuni 	(((offset) + HAMMER_XBUFMASK64) & ~HAMMER_XBUFMASK64)
98509798eaSPedro F. Giffuni 
99509798eaSPedro F. Giffuni /*
100509798eaSPedro F. Giffuni  * The current limit of volumes that can make up a HAMMER FS
101509798eaSPedro F. Giffuni  */
102509798eaSPedro F. Giffuni #define HAMMER_MAX_VOLUMES	256
103509798eaSPedro F. Giffuni 
104509798eaSPedro F. Giffuni /*
105509798eaSPedro F. Giffuni  * Reserved space for (future) header junk after the volume header.
106509798eaSPedro F. Giffuni  */
107509798eaSPedro F. Giffuni #define HAMMER_MIN_VOL_JUNK	(HAMMER_BUFSIZE * 16)	/* 256 KB */
108509798eaSPedro F. Giffuni #define HAMMER_MAX_VOL_JUNK	HAMMER_MIN_VOL_JUNK
109509798eaSPedro F. Giffuni #define HAMMER_VOL_JUNK_SIZE	HAMMER_MIN_VOL_JUNK
110509798eaSPedro F. Giffuni 
111509798eaSPedro F. Giffuni /*
112509798eaSPedro F. Giffuni  * Hammer transaction ids are 64 bit unsigned integers and are usually
113509798eaSPedro F. Giffuni  * synchronized with the time of day in nanoseconds.
114509798eaSPedro F. Giffuni  *
115509798eaSPedro F. Giffuni  * Hammer offsets are used for FIFO indexing and embed a cycle counter
116509798eaSPedro F. Giffuni  * and volume number in addition to the offset.  Most offsets are required
117509798eaSPedro F. Giffuni  * to be 16 KB aligned.
118509798eaSPedro F. Giffuni  */
119509798eaSPedro F. Giffuni typedef uint64_t hammer_tid_t;
120509798eaSPedro F. Giffuni typedef uint64_t hammer_off_t;
121509798eaSPedro F. Giffuni typedef uint32_t hammer_crc_t;
122509798eaSPedro F. Giffuni typedef uuid_t hammer_uuid_t;
123509798eaSPedro F. Giffuni 
124509798eaSPedro F. Giffuni #define HAMMER_MIN_TID		0ULL			/* unsigned */
125509798eaSPedro F. Giffuni #define HAMMER_MAX_TID		0xFFFFFFFFFFFFFFFFULL	/* unsigned */
126509798eaSPedro F. Giffuni #define HAMMER_MIN_KEY		-0x8000000000000000LL	/* signed */
127509798eaSPedro F. Giffuni #define HAMMER_MAX_KEY		0x7FFFFFFFFFFFFFFFLL	/* signed */
128509798eaSPedro F. Giffuni #define HAMMER_MIN_OBJID	HAMMER_MIN_KEY		/* signed */
129509798eaSPedro F. Giffuni #define HAMMER_MAX_OBJID	HAMMER_MAX_KEY		/* signed */
130509798eaSPedro F. Giffuni #define HAMMER_MIN_RECTYPE	0x0U			/* unsigned */
131509798eaSPedro F. Giffuni #define HAMMER_MAX_RECTYPE	0xFFFFU			/* unsigned */
132509798eaSPedro F. Giffuni #define HAMMER_MIN_OFFSET	0ULL			/* unsigned */
133509798eaSPedro F. Giffuni #define HAMMER_MAX_OFFSET	0xFFFFFFFFFFFFFFFFULL	/* unsigned */
134509798eaSPedro F. Giffuni 
135509798eaSPedro F. Giffuni /*
136509798eaSPedro F. Giffuni  * hammer_off_t has several different encodings.  Note that not all zones
137509798eaSPedro F. Giffuni  * encode a vol_no.  Zone bits are not a part of filesystem capacity as
138509798eaSPedro F. Giffuni  * the zone bits aren't directly or indirectly mapped to physical volumes.
139509798eaSPedro F. Giffuni  *
140509798eaSPedro F. Giffuni  * In other words, HAMMER's logical filesystem offset consists of 64 bits,
141509798eaSPedro F. Giffuni  * but the filesystem is considered 60 bits filesystem, not 64 bits.
142509798eaSPedro F. Giffuni  * The maximum filesystem capacity is 1EB, not 16EB.
143509798eaSPedro F. Giffuni  *
144509798eaSPedro F. Giffuni  * zone 0:		available, a big-block that contains the offset is unused
145509798eaSPedro F. Giffuni  * zone 1 (z,v,o):	raw volume relative (offset 0 is the volume header)
146509798eaSPedro F. Giffuni  * zone 2 (z,v,o):	raw buffer relative (offset 0 is the first buffer)
147509798eaSPedro F. Giffuni  * zone 3 (z,o):	undo/redo fifo	- fixed zone-2 offset array in volume header
148509798eaSPedro F. Giffuni  * zone 4 (z,v,o):	freemap		- only real blockmap
149509798eaSPedro F. Giffuni  * zone 8 (z,v,o):	B-Tree		- actually zone-2 address
150509798eaSPedro F. Giffuni  * zone 9 (z,v,o):	meta		- actually zone-2 address
151509798eaSPedro F. Giffuni  * zone 10 (z,v,o):	large-data	- actually zone-2 address
152509798eaSPedro F. Giffuni  * zone 11 (z,v,o):	small-data	- actually zone-2 address
153509798eaSPedro F. Giffuni  * zone 15:		unavailable, usually the offset is beyond volume size
154509798eaSPedro F. Giffuni  *
155509798eaSPedro F. Giffuni  * layer1/layer2 direct map:
156509798eaSPedro F. Giffuni  *	     Maximum HAMMER filesystem capacity from volume aspect
157509798eaSPedro F. Giffuni  *	     2^8(max volumes) * 2^52(max volume size) = 2^60 = 1EB (long offset)
158509798eaSPedro F. Giffuni  *	    <------------------------------------------------------------->
159509798eaSPedro F. Giffuni  *	     8bits   52bits (short offset)
160509798eaSPedro F. Giffuni  *	    <------><----------------------------------------------------->
161509798eaSPedro F. Giffuni  *	zzzzvvvvvvvvoooo oooooooooooooooo oooooooooooooooo oooooooooooooooo
162509798eaSPedro F. Giffuni  *	----111111111111 1111112222222222 222222222ooooooo oooooooooooooooo
163509798eaSPedro F. Giffuni  *	    <-----------------><------------------><---------------------->
164509798eaSPedro F. Giffuni  *	     18bits             19bits              23bits
165509798eaSPedro F. Giffuni  *	    <------------------------------------------------------------->
166509798eaSPedro F. Giffuni  *	     2^18(layer1) * 2^19(layer2) * 2^23(big-block) = 2^60 = 1EB
167509798eaSPedro F. Giffuni  *	     Maximum HAMMER filesystem capacity from blockmap aspect
168509798eaSPedro F. Giffuni  *
169509798eaSPedro F. Giffuni  * volume#0 layout
170509798eaSPedro F. Giffuni  *	+-------------------------> offset 0 of a device/partition
171509798eaSPedro F. Giffuni  *	| volume header (1928 bytes)
172509798eaSPedro F. Giffuni  *	| the rest of header junk space (HAMMER_BUFSIZE aligned)
173509798eaSPedro F. Giffuni  *	+-------------------------> vol_bot_beg
174509798eaSPedro F. Giffuni  *	| boot area (HAMMER_BUFSIZE aligned)
175509798eaSPedro F. Giffuni  *	+-------------------------> vol_mem_beg
176509798eaSPedro F. Giffuni  *	| memory log (HAMMER_BUFSIZE aligned)
177509798eaSPedro F. Giffuni  *	+-------------------------> vol_buf_beg (physical offset of zone-2)
178509798eaSPedro F. Giffuni  *	| zone-4 big-block for layer1
179509798eaSPedro F. Giffuni  *	+-------------------------> vol_buf_beg + HAMMER_BIGBLOCK_SIZE
180509798eaSPedro F. Giffuni  *	| zone-4 big-blocks for layer2
181509798eaSPedro F. Giffuni  *	| ... (1 big-block per 4TB space)
182509798eaSPedro F. Giffuni  *	+-------------------------> vol_buf_beg + HAMMER_BIGBLOCK_SIZE * ...
183509798eaSPedro F. Giffuni  *	| zone-3 big-blocks for UNDO/REDO FIFO
184509798eaSPedro F. Giffuni  *	| ... (max 128 big-blocks)
185509798eaSPedro F. Giffuni  *	+-------------------------> vol_buf_beg + HAMMER_BIGBLOCK_SIZE * ...
186509798eaSPedro F. Giffuni  *	| zone-8 big-block for root B-Tree node/etc
187509798eaSPedro F. Giffuni  *	+-------------------------> vol_buf_beg + HAMMER_BIGBLOCK_SIZE * ...
188509798eaSPedro F. Giffuni  *	| zone-9 big-block for root inode/PFS/etc
189509798eaSPedro F. Giffuni  *	+-------------------------> vol_buf_beg + HAMMER_BIGBLOCK_SIZE * ...
190509798eaSPedro F. Giffuni  *	| zone-X big-blocks
191509798eaSPedro F. Giffuni  *	| ... (big-blocks for new zones after newfs_hammer)
192509798eaSPedro F. Giffuni  *	| ...
193509798eaSPedro F. Giffuni  *	| ...
194509798eaSPedro F. Giffuni  *	| ...
195509798eaSPedro F. Giffuni  *	| ...
196509798eaSPedro F. Giffuni  *	+-------------------------> vol_buf_end (HAMMER_BUFSIZE aligned)
197509798eaSPedro F. Giffuni  *	+-------------------------> end of a device/partition
198509798eaSPedro F. Giffuni  *
199509798eaSPedro F. Giffuni  * volume#N layout (0<N<256)
200509798eaSPedro F. Giffuni  *	+-------------------------> offset 0 of a device/partition
201509798eaSPedro F. Giffuni  *	| volume header (1928 bytes)
202509798eaSPedro F. Giffuni  *	| the rest of header junk space (HAMMER_BUFSIZE aligned)
203509798eaSPedro F. Giffuni  *	+-------------------------> vol_bot_beg
204509798eaSPedro F. Giffuni  *	| boot area (HAMMER_BUFSIZE aligned)
205509798eaSPedro F. Giffuni  *	+-------------------------> vol_mem_beg
206509798eaSPedro F. Giffuni  *	| memory log (HAMMER_BUFSIZE aligned)
207509798eaSPedro F. Giffuni  *	+-------------------------> vol_buf_beg (physical offset of zone-2)
208509798eaSPedro F. Giffuni  *	| zone-4 big-blocks for layer2
209509798eaSPedro F. Giffuni  *	| ... (1 big-block per 4TB space)
210509798eaSPedro F. Giffuni  *	+-------------------------> vol_buf_beg + HAMMER_BIGBLOCK_SIZE * ...
211509798eaSPedro F. Giffuni  *	| zone-X big-blocks
212509798eaSPedro F. Giffuni  *	| ... (unused until volume#(N-1) runs out of space)
213509798eaSPedro F. Giffuni  *	| ...
214509798eaSPedro F. Giffuni  *	| ...
215509798eaSPedro F. Giffuni  *	| ...
216509798eaSPedro F. Giffuni  *	| ...
217509798eaSPedro F. Giffuni  *	+-------------------------> vol_buf_end (HAMMER_BUFSIZE aligned)
218509798eaSPedro F. Giffuni  *	+-------------------------> end of a device/partition
219509798eaSPedro F. Giffuni  */
220509798eaSPedro F. Giffuni 
221509798eaSPedro F. Giffuni #define HAMMER_ZONE_RAW_VOLUME		0x1000000000000000ULL
222509798eaSPedro F. Giffuni #define HAMMER_ZONE_RAW_BUFFER		0x2000000000000000ULL
223509798eaSPedro F. Giffuni #define HAMMER_ZONE_UNDO		0x3000000000000000ULL
224509798eaSPedro F. Giffuni #define HAMMER_ZONE_FREEMAP		0x4000000000000000ULL
225509798eaSPedro F. Giffuni #define HAMMER_ZONE_RESERVED05		0x5000000000000000ULL  /* not used */
226509798eaSPedro F. Giffuni #define HAMMER_ZONE_RESERVED06		0x6000000000000000ULL  /* not used */
227509798eaSPedro F. Giffuni #define HAMMER_ZONE_RESERVED07		0x7000000000000000ULL  /* not used */
228509798eaSPedro F. Giffuni #define HAMMER_ZONE_BTREE		0x8000000000000000ULL
229509798eaSPedro F. Giffuni #define HAMMER_ZONE_META		0x9000000000000000ULL
230509798eaSPedro F. Giffuni #define HAMMER_ZONE_LARGE_DATA		0xA000000000000000ULL
231509798eaSPedro F. Giffuni #define HAMMER_ZONE_SMALL_DATA		0xB000000000000000ULL
232509798eaSPedro F. Giffuni #define HAMMER_ZONE_RESERVED0C		0xC000000000000000ULL  /* not used */
233509798eaSPedro F. Giffuni #define HAMMER_ZONE_RESERVED0D		0xD000000000000000ULL  /* not used */
234509798eaSPedro F. Giffuni #define HAMMER_ZONE_RESERVED0E		0xE000000000000000ULL  /* not used */
235509798eaSPedro F. Giffuni #define HAMMER_ZONE_UNAVAIL		0xF000000000000000ULL
236509798eaSPedro F. Giffuni 
237509798eaSPedro F. Giffuni #define HAMMER_ZONE_RAW_VOLUME_INDEX	1
238509798eaSPedro F. Giffuni #define HAMMER_ZONE_RAW_BUFFER_INDEX	2
239509798eaSPedro F. Giffuni #define HAMMER_ZONE_UNDO_INDEX		3
240509798eaSPedro F. Giffuni #define HAMMER_ZONE_FREEMAP_INDEX	4
241509798eaSPedro F. Giffuni #define HAMMER_ZONE_BTREE_INDEX		8
242509798eaSPedro F. Giffuni #define HAMMER_ZONE_META_INDEX		9
243509798eaSPedro F. Giffuni #define HAMMER_ZONE_LARGE_DATA_INDEX	10
244509798eaSPedro F. Giffuni #define HAMMER_ZONE_SMALL_DATA_INDEX	11
245509798eaSPedro F. Giffuni #define HAMMER_ZONE_UNAVAIL_INDEX	15
246509798eaSPedro F. Giffuni 
247509798eaSPedro F. Giffuni #define HAMMER_MAX_ZONES		16
248509798eaSPedro F. Giffuni 
249509798eaSPedro F. Giffuni #define HAMMER_ZONE(offset)		((offset) & HAMMER_OFF_ZONE_MASK)
250509798eaSPedro F. Giffuni 
251509798eaSPedro F. Giffuni #define hammer_is_zone_raw_volume(offset)		\
252509798eaSPedro F. Giffuni 	(HAMMER_ZONE(offset) == HAMMER_ZONE_RAW_VOLUME)
253509798eaSPedro F. Giffuni #define hammer_is_zone_raw_buffer(offset)		\
254509798eaSPedro F. Giffuni 	(HAMMER_ZONE(offset) == HAMMER_ZONE_RAW_BUFFER)
255509798eaSPedro F. Giffuni #define hammer_is_zone_undo(offset)			\
256509798eaSPedro F. Giffuni 	(HAMMER_ZONE(offset) == HAMMER_ZONE_UNDO)
257509798eaSPedro F. Giffuni #define hammer_is_zone_freemap(offset)			\
258509798eaSPedro F. Giffuni 	(HAMMER_ZONE(offset) == HAMMER_ZONE_FREEMAP)
259509798eaSPedro F. Giffuni #define hammer_is_zone_btree(offset)			\
260509798eaSPedro F. Giffuni 	(HAMMER_ZONE(offset) == HAMMER_ZONE_BTREE)
261509798eaSPedro F. Giffuni #define hammer_is_zone_meta(offset)			\
262509798eaSPedro F. Giffuni 	(HAMMER_ZONE(offset) == HAMMER_ZONE_META)
263509798eaSPedro F. Giffuni #define hammer_is_zone_large_data(offset)		\
264509798eaSPedro F. Giffuni 	(HAMMER_ZONE(offset) == HAMMER_ZONE_LARGE_DATA)
265509798eaSPedro F. Giffuni #define hammer_is_zone_small_data(offset)		\
266509798eaSPedro F. Giffuni 	(HAMMER_ZONE(offset) == HAMMER_ZONE_SMALL_DATA)
267509798eaSPedro F. Giffuni #define hammer_is_zone_unavail(offset)			\
268509798eaSPedro F. Giffuni 	(HAMMER_ZONE(offset) == HAMMER_ZONE_UNAVAIL)
269509798eaSPedro F. Giffuni #define hammer_is_zone_data(offset)			\
270509798eaSPedro F. Giffuni 	(hammer_is_zone_large_data(offset) || hammer_is_zone_small_data(offset))
271509798eaSPedro F. Giffuni 
272509798eaSPedro F. Giffuni #define hammer_is_index_record(zone)			\
273509798eaSPedro F. Giffuni 	((zone) >= HAMMER_ZONE_BTREE_INDEX &&		\
274509798eaSPedro F. Giffuni 	 (zone) < HAMMER_MAX_ZONES)
275509798eaSPedro F. Giffuni 
276509798eaSPedro F. Giffuni #define hammer_is_zone_record(offset)			\
277509798eaSPedro F. Giffuni 	hammer_is_index_record(HAMMER_ZONE_DECODE(offset))
278509798eaSPedro F. Giffuni 
279509798eaSPedro F. Giffuni #define hammer_is_index_direct_xlated(zone)		\
280509798eaSPedro F. Giffuni 	(((zone) == HAMMER_ZONE_RAW_BUFFER_INDEX) ||	\
281509798eaSPedro F. Giffuni 	 ((zone) == HAMMER_ZONE_FREEMAP_INDEX) ||	\
282509798eaSPedro F. Giffuni 	 hammer_is_index_record(zone))
283509798eaSPedro F. Giffuni 
284509798eaSPedro F. Giffuni #define hammer_is_zone_direct_xlated(offset)		\
285509798eaSPedro F. Giffuni 	hammer_is_index_direct_xlated(HAMMER_ZONE_DECODE(offset))
286509798eaSPedro F. Giffuni 
287509798eaSPedro F. Giffuni #define HAMMER_ZONE_ENCODE(zone, ham_off)		\
288509798eaSPedro F. Giffuni 	(((hammer_off_t)(zone) << 60) | (ham_off))
289509798eaSPedro F. Giffuni #define HAMMER_ZONE_DECODE(ham_off)			\
290509798eaSPedro F. Giffuni 	((int)(((hammer_off_t)(ham_off) >> 60)))
291509798eaSPedro F. Giffuni 
292509798eaSPedro F. Giffuni #define HAMMER_VOL_ENCODE(vol_no)			\
293509798eaSPedro F. Giffuni 	((hammer_off_t)((vol_no) & 255) << 52)
294509798eaSPedro F. Giffuni #define HAMMER_VOL_DECODE(ham_off)			\
295509798eaSPedro F. Giffuni 	((int)(((hammer_off_t)(ham_off) >> 52) & 255))
296509798eaSPedro F. Giffuni 
297509798eaSPedro F. Giffuni #define HAMMER_OFF_SHORT_ENCODE(offset)			\
298509798eaSPedro F. Giffuni 	((hammer_off_t)(offset) & HAMMER_OFF_SHORT_MASK)
299509798eaSPedro F. Giffuni #define HAMMER_OFF_LONG_ENCODE(offset)			\
300509798eaSPedro F. Giffuni 	((hammer_off_t)(offset) & HAMMER_OFF_LONG_MASK)
301509798eaSPedro F. Giffuni 
302509798eaSPedro F. Giffuni #define HAMMER_ENCODE(zone, vol_no, offset)		\
303509798eaSPedro F. Giffuni 	(((hammer_off_t)(zone) << 60) |			\
304509798eaSPedro F. Giffuni 	HAMMER_VOL_ENCODE(vol_no) |			\
305509798eaSPedro F. Giffuni 	HAMMER_OFF_SHORT_ENCODE(offset))
306509798eaSPedro F. Giffuni #define HAMMER_ENCODE_RAW_VOLUME(vol_no, offset)	\
307509798eaSPedro F. Giffuni 	HAMMER_ENCODE(HAMMER_ZONE_RAW_VOLUME_INDEX, vol_no, offset)
308509798eaSPedro F. Giffuni #define HAMMER_ENCODE_RAW_BUFFER(vol_no, offset)	\
309509798eaSPedro F. Giffuni 	HAMMER_ENCODE(HAMMER_ZONE_RAW_BUFFER_INDEX, vol_no, offset)
310509798eaSPedro F. Giffuni #define HAMMER_ENCODE_UNDO(offset)			\
311509798eaSPedro F. Giffuni 	HAMMER_ENCODE(HAMMER_ZONE_UNDO_INDEX, HAMMER_ROOT_VOLNO, offset)
312509798eaSPedro F. Giffuni #define HAMMER_ENCODE_FREEMAP(vol_no, offset)		\
313509798eaSPedro F. Giffuni 	HAMMER_ENCODE(HAMMER_ZONE_FREEMAP_INDEX, vol_no, offset)
314509798eaSPedro F. Giffuni 
315509798eaSPedro F. Giffuni /*
316509798eaSPedro F. Giffuni  * Translate a zone address to zone-X address.
317509798eaSPedro F. Giffuni  */
318509798eaSPedro F. Giffuni #define hammer_xlate_to_zoneX(zone, offset)		\
319509798eaSPedro F. Giffuni 	HAMMER_ZONE_ENCODE((zone), (offset) & ~HAMMER_OFF_ZONE_MASK)
320509798eaSPedro F. Giffuni #define hammer_xlate_to_zone2(offset)			\
321509798eaSPedro F. Giffuni 	hammer_xlate_to_zoneX(HAMMER_ZONE_RAW_BUFFER_INDEX, (offset))
322509798eaSPedro F. Giffuni 
323509798eaSPedro F. Giffuni #define hammer_data_zone(data_len)			\
324509798eaSPedro F. Giffuni 	(((data_len) >= HAMMER_BUFSIZE) ?		\
325509798eaSPedro F. Giffuni 	 HAMMER_ZONE_LARGE_DATA :			\
326509798eaSPedro F. Giffuni 	 HAMMER_ZONE_SMALL_DATA)
327509798eaSPedro F. Giffuni #define hammer_data_zone_index(data_len)		\
328509798eaSPedro F. Giffuni 	(((data_len) >= HAMMER_BUFSIZE) ?		\
329509798eaSPedro F. Giffuni 	 HAMMER_ZONE_LARGE_DATA_INDEX :			\
330509798eaSPedro F. Giffuni 	 HAMMER_ZONE_SMALL_DATA_INDEX)
331509798eaSPedro F. Giffuni 
332509798eaSPedro F. Giffuni /*
333509798eaSPedro F. Giffuni  * Big-Block backing store
334509798eaSPedro F. Giffuni  *
335509798eaSPedro F. Giffuni  * A blockmap is a two-level map which translates a blockmap-backed zone
336509798eaSPedro F. Giffuni  * offset into a raw zone 2 offset.  The layer 1 handles 18 bits and the
337509798eaSPedro F. Giffuni  * layer 2 handles 19 bits.  The 8M big-block size is 23 bits so two
338509798eaSPedro F. Giffuni  * layers gives us 18+19+23 = 60 bits of address space.
339509798eaSPedro F. Giffuni  *
340509798eaSPedro F. Giffuni  * When using hinting for a blockmap lookup, the hint is lost when the
341509798eaSPedro F. Giffuni  * scan leaves the HINTBLOCK, which is typically several BIGBLOCK's.
342509798eaSPedro F. Giffuni  * HINTBLOCK is a heuristic.
343509798eaSPedro F. Giffuni  */
344509798eaSPedro F. Giffuni #define HAMMER_HINTBLOCK_SIZE		(HAMMER_BIGBLOCK_SIZE * 4)
345509798eaSPedro F. Giffuni #define HAMMER_HINTBLOCK_MASK64		((uint64_t)HAMMER_HINTBLOCK_SIZE - 1)
346509798eaSPedro F. Giffuni #define HAMMER_BIGBLOCK_SIZE		(8192 * 1024)
347509798eaSPedro F. Giffuni #define HAMMER_BIGBLOCK_SIZE64		((uint64_t)HAMMER_BIGBLOCK_SIZE)
348509798eaSPedro F. Giffuni #define HAMMER_BIGBLOCK_MASK		(HAMMER_BIGBLOCK_SIZE - 1)
349509798eaSPedro F. Giffuni #define HAMMER_BIGBLOCK_MASK64		((uint64_t)HAMMER_BIGBLOCK_SIZE - 1)
350509798eaSPedro F. Giffuni #define HAMMER_BIGBLOCK_BITS		23
351509798eaSPedro F. Giffuni #if 0
352509798eaSPedro F. Giffuni #define HAMMER_BIGBLOCK_OVERFILL	(6144 * 1024)
353509798eaSPedro F. Giffuni #endif
354509798eaSPedro F. Giffuni #if (1 << HAMMER_BIGBLOCK_BITS) != HAMMER_BIGBLOCK_SIZE
355509798eaSPedro F. Giffuni #error "HAMMER_BIGBLOCK_BITS BROKEN"
356509798eaSPedro F. Giffuni #endif
357509798eaSPedro F. Giffuni 
358509798eaSPedro F. Giffuni #define HAMMER_BUFFERS_PER_BIGBLOCK			\
359509798eaSPedro F. Giffuni 	(HAMMER_BIGBLOCK_SIZE / HAMMER_BUFSIZE)
360509798eaSPedro F. Giffuni #define HAMMER_BUFFERS_PER_BIGBLOCK_MASK		\
361509798eaSPedro F. Giffuni 	(HAMMER_BUFFERS_PER_BIGBLOCK - 1)
362509798eaSPedro F. Giffuni #define HAMMER_BUFFERS_PER_BIGBLOCK_MASK64		\
363509798eaSPedro F. Giffuni 	((hammer_off_t)HAMMER_BUFFERS_PER_BIGBLOCK_MASK)
364509798eaSPedro F. Giffuni 
365509798eaSPedro F. Giffuni #define HAMMER_BIGBLOCK_DOALIGN(offset)				\
366509798eaSPedro F. Giffuni 	(((offset) + HAMMER_BIGBLOCK_MASK64) & ~HAMMER_BIGBLOCK_MASK64)
367509798eaSPedro F. Giffuni 
368509798eaSPedro F. Giffuni /*
369509798eaSPedro F. Giffuni  * Maximum number of mirrors operating in master mode (multi-master
370509798eaSPedro F. Giffuni  * clustering and mirroring). Note that HAMMER1 does not support
371509798eaSPedro F. Giffuni  * multi-master clustering as of 2015.
372509798eaSPedro F. Giffuni  */
373509798eaSPedro F. Giffuni #define HAMMER_MAX_MASTERS		16
374509798eaSPedro F. Giffuni 
375509798eaSPedro F. Giffuni /*
376509798eaSPedro F. Giffuni  * The blockmap is somewhat of a degenerate structure.  HAMMER only actually
377509798eaSPedro F. Giffuni  * uses it in its original incarnation to implement the freemap.
378509798eaSPedro F. Giffuni  *
379509798eaSPedro F. Giffuni  * zone:1	raw volume (no blockmap)
380509798eaSPedro F. Giffuni  * zone:2	raw buffer (no blockmap)
381509798eaSPedro F. Giffuni  * zone:3	undomap    (direct layer2 array in volume header)
382509798eaSPedro F. Giffuni  * zone:4	freemap    (the only real blockmap)
383509798eaSPedro F. Giffuni  * zone:8-15	zone id used to classify big-block only, address is actually
384509798eaSPedro F. Giffuni  *		a zone-2 address.
385509798eaSPedro F. Giffuni  */
386509798eaSPedro F. Giffuni typedef struct hammer_blockmap {
387509798eaSPedro F. Giffuni 	hammer_off_t	phys_offset;  /* zone-2 offset only used by zone-4 */
388509798eaSPedro F. Giffuni 	hammer_off_t	first_offset; /* zone-X offset only used by zone-3 */
389509798eaSPedro F. Giffuni 	hammer_off_t	next_offset;  /* zone-X offset for allocation */
390509798eaSPedro F. Giffuni 	hammer_off_t	alloc_offset; /* zone-X offset only used by zone-3 */
391509798eaSPedro F. Giffuni 	uint32_t	reserved01;
392509798eaSPedro F. Giffuni 	hammer_crc_t	entry_crc;
393509798eaSPedro F. Giffuni } *hammer_blockmap_t;
394509798eaSPedro F. Giffuni 
395509798eaSPedro F. Giffuni #define HAMMER_BLOCKMAP_CRCSIZE	\
396509798eaSPedro F. Giffuni 	offsetof(struct hammer_blockmap, entry_crc)
397509798eaSPedro F. Giffuni 
398509798eaSPedro F. Giffuni /*
399509798eaSPedro F. Giffuni  * The blockmap is a 2-layer entity made up of big-blocks.  The first layer
400509798eaSPedro F. Giffuni  * contains 262144 32-byte entries (18 bits), the second layer contains
401509798eaSPedro F. Giffuni  * 524288 16-byte entries (19 bits), representing 8MB (23 bit) blockmaps.
402509798eaSPedro F. Giffuni  * 18+19+23 = 60 bits.  The top four bits are the zone id.
403509798eaSPedro F. Giffuni  *
404509798eaSPedro F. Giffuni  * Currently only the freemap utilizes both layers in all their glory.
405509798eaSPedro F. Giffuni  * All primary data/meta-data zones actually encode a zone-2 address
406509798eaSPedro F. Giffuni  * requiring no real blockmap translation.
407509798eaSPedro F. Giffuni  *
408509798eaSPedro F. Giffuni  * The freemap uses the upper 8 bits of layer-1 to identify the volume,
409509798eaSPedro F. Giffuni  * thus any space allocated via the freemap can be directly translated
410509798eaSPedro F. Giffuni  * to a zone:2 (or zone:8-15) address.
411509798eaSPedro F. Giffuni  *
412509798eaSPedro F. Giffuni  * zone-X blockmap offset: [zone:4][layer1:18][layer2:19][big-block:23]
413509798eaSPedro F. Giffuni  */
414509798eaSPedro F. Giffuni 
415509798eaSPedro F. Giffuni /*
416509798eaSPedro F. Giffuni  * 32 bytes layer1 entry for 8MB big-block.
417509798eaSPedro F. Giffuni  * A big-block can hold 2^23 / 2^5 = 2^18 layer1 entries,
418509798eaSPedro F. Giffuni  * which equals bits assigned for layer1 in zone-2 address.
419509798eaSPedro F. Giffuni  */
420509798eaSPedro F. Giffuni typedef struct hammer_blockmap_layer1 {
421509798eaSPedro F. Giffuni 	hammer_off_t	blocks_free;	/* big-blocks free */
422509798eaSPedro F. Giffuni 	hammer_off_t	phys_offset;	/* UNAVAIL or zone-2 */
423509798eaSPedro F. Giffuni 	hammer_off_t	reserved01;
424509798eaSPedro F. Giffuni 	hammer_crc_t	layer2_crc;	/* xor'd crc's of HAMMER_BLOCKSIZE */
425509798eaSPedro F. Giffuni 					/* (not yet used) */
426509798eaSPedro F. Giffuni 	hammer_crc_t	layer1_crc;	/* MUST BE LAST FIELD OF STRUCTURE*/
427509798eaSPedro F. Giffuni } *hammer_blockmap_layer1_t;
428509798eaSPedro F. Giffuni 
429509798eaSPedro F. Giffuni #define HAMMER_LAYER1_CRCSIZE	\
430509798eaSPedro F. Giffuni 	offsetof(struct hammer_blockmap_layer1, layer1_crc)
431509798eaSPedro F. Giffuni 
432509798eaSPedro F. Giffuni /*
433509798eaSPedro F. Giffuni  * 16 bytes layer2 entry for 8MB big-blocks.
434509798eaSPedro F. Giffuni  * A big-block can hold 2^23 / 2^4 = 2^19 layer2 entries,
435509798eaSPedro F. Giffuni  * which equals bits assigned for layer2 in zone-2 address.
436509798eaSPedro F. Giffuni  *
437509798eaSPedro F. Giffuni  * NOTE: bytes_free is signed and can legally go negative if/when data
438509798eaSPedro F. Giffuni  *	 de-dup occurs.  This field will never go higher than
439509798eaSPedro F. Giffuni  *	 HAMMER_BIGBLOCK_SIZE.  If exactly HAMMER_BIGBLOCK_SIZE
440509798eaSPedro F. Giffuni  *	 the big-block is completely free.
441509798eaSPedro F. Giffuni  */
442509798eaSPedro F. Giffuni typedef struct hammer_blockmap_layer2 {
443509798eaSPedro F. Giffuni 	uint8_t		zone;		/* typed allocation zone */
444509798eaSPedro F. Giffuni 	uint8_t		reserved01;
445509798eaSPedro F. Giffuni 	uint16_t	reserved02;
446509798eaSPedro F. Giffuni 	uint32_t	append_off;	/* allocatable space index */
447509798eaSPedro F. Giffuni 	int32_t		bytes_free;	/* bytes free within this big-block */
448509798eaSPedro F. Giffuni 	hammer_crc_t	entry_crc;
449509798eaSPedro F. Giffuni } *hammer_blockmap_layer2_t;
450509798eaSPedro F. Giffuni 
451509798eaSPedro F. Giffuni #define HAMMER_LAYER2_CRCSIZE	\
452509798eaSPedro F. Giffuni 	offsetof(struct hammer_blockmap_layer2, entry_crc)
453509798eaSPedro F. Giffuni 
454509798eaSPedro F. Giffuni #define HAMMER_BLOCKMAP_UNAVAIL	((hammer_off_t)-1LL)
455509798eaSPedro F. Giffuni 
456509798eaSPedro F. Giffuni #define HAMMER_BLOCKMAP_RADIX1	/* 2^18 = 262144 */	\
457509798eaSPedro F. Giffuni 	((int)(HAMMER_BIGBLOCK_SIZE / sizeof(struct hammer_blockmap_layer1)))
458509798eaSPedro F. Giffuni #define HAMMER_BLOCKMAP_RADIX2	/* 2^19 = 524288 */	\
459509798eaSPedro F. Giffuni 	((int)(HAMMER_BIGBLOCK_SIZE / sizeof(struct hammer_blockmap_layer2)))
460509798eaSPedro F. Giffuni 
461509798eaSPedro F. Giffuni #define HAMMER_BLOCKMAP_LAYER1	/* 2^(18+19+23) = 1EB */	\
462509798eaSPedro F. Giffuni 	(HAMMER_BLOCKMAP_RADIX1 * HAMMER_BLOCKMAP_LAYER2)
463509798eaSPedro F. Giffuni #define HAMMER_BLOCKMAP_LAYER2	/* 2^(19+23) = 4TB */		\
464509798eaSPedro F. Giffuni 	(HAMMER_BLOCKMAP_RADIX2 * HAMMER_BIGBLOCK_SIZE64)
465509798eaSPedro F. Giffuni 
466509798eaSPedro F. Giffuni #define HAMMER_BLOCKMAP_LAYER1_MASK	(HAMMER_BLOCKMAP_LAYER1 - 1)
467509798eaSPedro F. Giffuni #define HAMMER_BLOCKMAP_LAYER2_MASK	(HAMMER_BLOCKMAP_LAYER2 - 1)
468509798eaSPedro F. Giffuni 
469509798eaSPedro F. Giffuni #define HAMMER_BLOCKMAP_LAYER2_DOALIGN(offset)			\
470509798eaSPedro F. Giffuni 	(((offset) + HAMMER_BLOCKMAP_LAYER2_MASK) &		\
471509798eaSPedro F. Giffuni 	 ~HAMMER_BLOCKMAP_LAYER2_MASK)
472509798eaSPedro F. Giffuni 
473509798eaSPedro F. Giffuni /*
474509798eaSPedro F. Giffuni  * Index within layer1 or layer2 big-block for the entry representing
475509798eaSPedro F. Giffuni  * a zone-2 physical offset.
476509798eaSPedro F. Giffuni  */
477509798eaSPedro F. Giffuni #define HAMMER_BLOCKMAP_LAYER1_INDEX(zone2_offset)		\
478509798eaSPedro F. Giffuni 	((int)(((zone2_offset) & HAMMER_BLOCKMAP_LAYER1_MASK) /	\
479509798eaSPedro F. Giffuni 	 HAMMER_BLOCKMAP_LAYER2))
480509798eaSPedro F. Giffuni 
481509798eaSPedro F. Giffuni #define HAMMER_BLOCKMAP_LAYER2_INDEX(zone2_offset)		\
482509798eaSPedro F. Giffuni 	((int)(((zone2_offset) & HAMMER_BLOCKMAP_LAYER2_MASK) /	\
483509798eaSPedro F. Giffuni 	HAMMER_BIGBLOCK_SIZE64))
484509798eaSPedro F. Giffuni 
485509798eaSPedro F. Giffuni /*
486509798eaSPedro F. Giffuni  * Byte offset within layer1 or layer2 big-block for the entry representing
487509798eaSPedro F. Giffuni  * a zone-2 physical offset.  Multiply the index by sizeof(blockmap_layer).
488509798eaSPedro F. Giffuni  */
489509798eaSPedro F. Giffuni #define HAMMER_BLOCKMAP_LAYER1_OFFSET(zone2_offset)		\
490509798eaSPedro F. Giffuni 	(HAMMER_BLOCKMAP_LAYER1_INDEX(zone2_offset) *		\
491509798eaSPedro F. Giffuni 	 sizeof(struct hammer_blockmap_layer1))
492509798eaSPedro F. Giffuni 
493509798eaSPedro F. Giffuni #define HAMMER_BLOCKMAP_LAYER2_OFFSET(zone2_offset)		\
494509798eaSPedro F. Giffuni 	(HAMMER_BLOCKMAP_LAYER2_INDEX(zone2_offset) *		\
495509798eaSPedro F. Giffuni 	 sizeof(struct hammer_blockmap_layer2))
496509798eaSPedro F. Giffuni 
497509798eaSPedro F. Giffuni /*
498509798eaSPedro F. Giffuni  * Move on to offset 0 of the next layer1 or layer2.
499509798eaSPedro F. Giffuni  */
500509798eaSPedro F. Giffuni #define HAMMER_ZONE_LAYER1_NEXT_OFFSET(offset)			\
501509798eaSPedro F. Giffuni 	(((offset) + HAMMER_BLOCKMAP_LAYER2) & ~HAMMER_BLOCKMAP_LAYER2_MASK)
502509798eaSPedro F. Giffuni 
503509798eaSPedro F. Giffuni #define HAMMER_ZONE_LAYER2_NEXT_OFFSET(offset)			\
504509798eaSPedro F. Giffuni 	(((offset) + HAMMER_BIGBLOCK_SIZE) & ~HAMMER_BIGBLOCK_MASK64)
505509798eaSPedro F. Giffuni 
506509798eaSPedro F. Giffuni /*
507509798eaSPedro F. Giffuni  * HAMMER UNDO parameters.  The UNDO fifo is mapped directly in the volume
508509798eaSPedro F. Giffuni  * header with an array of zone-2 offsets.  A maximum of (128x8MB) = 1GB,
509509798eaSPedro F. Giffuni  * and minimum of (64x8MB) = 512MB may be reserved.  The size of the undo
510509798eaSPedro F. Giffuni  * fifo is usually set a newfs time.
511509798eaSPedro F. Giffuni  */
512509798eaSPedro F. Giffuni #define HAMMER_MIN_UNDO_BIGBLOCKS		64
513509798eaSPedro F. Giffuni #define HAMMER_MAX_UNDO_BIGBLOCKS		128
514509798eaSPedro F. Giffuni 
515509798eaSPedro F. Giffuni /*
516509798eaSPedro F. Giffuni  * All on-disk HAMMER structures which make up elements of the UNDO FIFO
517509798eaSPedro F. Giffuni  * contain a hammer_fifo_head and hammer_fifo_tail structure.  This structure
518509798eaSPedro F. Giffuni  * contains all the information required to validate the fifo element
519509798eaSPedro F. Giffuni  * and to scan the fifo in either direction.  The head is typically embedded
520509798eaSPedro F. Giffuni  * in higher level hammer on-disk structures while the tail is typically
521509798eaSPedro F. Giffuni  * out-of-band.  hdr_size is the size of the whole mess, including the tail.
522509798eaSPedro F. Giffuni  *
523509798eaSPedro F. Giffuni  * All undo structures are guaranteed to not cross a 16K filesystem
524509798eaSPedro F. Giffuni  * buffer boundary.  Most undo structures are fairly small.  Data spaces
525509798eaSPedro F. Giffuni  * are not immediately reused by HAMMER so file data is not usually recorded
526509798eaSPedro F. Giffuni  * as part of an UNDO.
527509798eaSPedro F. Giffuni  *
528509798eaSPedro F. Giffuni  * PAD elements are allowed to take up only 8 bytes of space as a special
529509798eaSPedro F. Giffuni  * case, containing only hdr_signature, hdr_type, and hdr_size fields,
530509798eaSPedro F. Giffuni  * and with the tail overloaded onto the head structure for 8 bytes total.
531509798eaSPedro F. Giffuni  *
532509798eaSPedro F. Giffuni  * Every undo record has a sequence number.  This number is unrelated to
533509798eaSPedro F. Giffuni  * transaction ids and instead collects the undo transactions associated
534509798eaSPedro F. Giffuni  * with a single atomic operation.  A larger transactional operation, such
535509798eaSPedro F. Giffuni  * as a remove(), may consist of several smaller atomic operations
536509798eaSPedro F. Giffuni  * representing raw meta-data operations.
537509798eaSPedro F. Giffuni  *
538509798eaSPedro F. Giffuni  *				HAMMER VERSION 4 CHANGES
539509798eaSPedro F. Giffuni  *
540509798eaSPedro F. Giffuni  * In HAMMER version 4 the undo structure alignment is reduced from 16384
541509798eaSPedro F. Giffuni  * to 512 bytes in order to ensure that each 512 byte sector begins with
542509798eaSPedro F. Giffuni  * a header.  The hdr_seq field in the header is a 32 bit sequence number
543509798eaSPedro F. Giffuni  * which allows the recovery code to detect missing sectors
544509798eaSPedro F. Giffuni  * without relying on the 32-bit crc and to definitively identify the current
545509798eaSPedro F. Giffuni  * undo sequence space without having to rely on information from the volume
546509798eaSPedro F. Giffuni  * header.  In addition, new REDO entries in the undo space are used to
547509798eaSPedro F. Giffuni  * record write, write/extend, and transaction id updates.
548509798eaSPedro F. Giffuni  *
549509798eaSPedro F. Giffuni  * The grand result is:
550509798eaSPedro F. Giffuni  *
551509798eaSPedro F. Giffuni  * (1) The volume header no longer needs to be synchronized for most
552509798eaSPedro F. Giffuni  *     flush and fsync operations.
553509798eaSPedro F. Giffuni  *
554509798eaSPedro F. Giffuni  * (2) Most fsync operations need only lay down REDO records
555509798eaSPedro F. Giffuni  *
556509798eaSPedro F. Giffuni  * (3) Data overwrite for nohistory operations covered by REDO records
557509798eaSPedro F. Giffuni  *     can be supported (instead of rolling a new block allocation),
558509798eaSPedro F. Giffuni  *     by rolling UNDO for the prior contents of the data.
559509798eaSPedro F. Giffuni  *
560509798eaSPedro F. Giffuni  *				HAMMER VERSION 5 CHANGES
561509798eaSPedro F. Giffuni  *
562509798eaSPedro F. Giffuni  * Hammer version 5 contains a minor adjustment making layer2's bytes_free
563509798eaSPedro F. Giffuni  * field signed, allowing dedup to push it into the negative domain.
564509798eaSPedro F. Giffuni  */
565509798eaSPedro F. Giffuni #define HAMMER_HEAD_ALIGN		8
566509798eaSPedro F. Giffuni #define HAMMER_HEAD_ALIGN_MASK		(HAMMER_HEAD_ALIGN - 1)
567509798eaSPedro F. Giffuni #define HAMMER_HEAD_DOALIGN(bytes)	\
568509798eaSPedro F. Giffuni 	(((bytes) + HAMMER_HEAD_ALIGN_MASK) & ~HAMMER_HEAD_ALIGN_MASK)
569509798eaSPedro F. Giffuni 
570509798eaSPedro F. Giffuni #define HAMMER_UNDO_ALIGN		512
571509798eaSPedro F. Giffuni #define HAMMER_UNDO_ALIGN64		((uint64_t)512)
572509798eaSPedro F. Giffuni #define HAMMER_UNDO_MASK		(HAMMER_UNDO_ALIGN - 1)
573509798eaSPedro F. Giffuni #define HAMMER_UNDO_MASK64		(HAMMER_UNDO_ALIGN64 - 1)
574509798eaSPedro F. Giffuni #define HAMMER_UNDO_DOALIGN(offset)	\
575509798eaSPedro F. Giffuni 	(((offset) + HAMMER_UNDO_MASK) & ~HAMMER_UNDO_MASK64)
576509798eaSPedro F. Giffuni 
577509798eaSPedro F. Giffuni typedef struct hammer_fifo_head {
578509798eaSPedro F. Giffuni 	uint16_t hdr_signature;
579509798eaSPedro F. Giffuni 	uint16_t hdr_type;
580509798eaSPedro F. Giffuni 	uint32_t hdr_size;	/* Aligned size of the whole mess */
581509798eaSPedro F. Giffuni 	uint32_t hdr_seq;	/* Sequence number */
582509798eaSPedro F. Giffuni 	hammer_crc_t hdr_crc;	/* XOR crc up to field w/ crc after field */
583509798eaSPedro F. Giffuni } *hammer_fifo_head_t;
584509798eaSPedro F. Giffuni 
585509798eaSPedro F. Giffuni #define HAMMER_FIFO_HEAD_CRCOFF	offsetof(struct hammer_fifo_head, hdr_crc)
586509798eaSPedro F. Giffuni 
587509798eaSPedro F. Giffuni typedef struct hammer_fifo_tail {
588509798eaSPedro F. Giffuni 	uint16_t tail_signature;
589509798eaSPedro F. Giffuni 	uint16_t tail_type;
590509798eaSPedro F. Giffuni 	uint32_t tail_size;	/* aligned size of the whole mess */
591509798eaSPedro F. Giffuni } *hammer_fifo_tail_t;
592509798eaSPedro F. Giffuni 
593509798eaSPedro F. Giffuni /*
594509798eaSPedro F. Giffuni  * Fifo header types.
595509798eaSPedro F. Giffuni  *
596509798eaSPedro F. Giffuni  * NOTE: 0x8000U part of HAMMER_HEAD_TYPE_PAD can be removed if the HAMMER
597509798eaSPedro F. Giffuni  * version ever gets bumped again. It exists only to keep compatibility with
598509798eaSPedro F. Giffuni  * older versions.
599509798eaSPedro F. Giffuni  */
600509798eaSPedro F. Giffuni #define HAMMER_HEAD_TYPE_PAD	(0x0040U | 0x8000U)
601509798eaSPedro F. Giffuni #define HAMMER_HEAD_TYPE_DUMMY	0x0041U		/* dummy entry w/seqno */
602509798eaSPedro F. Giffuni #define HAMMER_HEAD_TYPE_UNDO	0x0043U		/* random UNDO information */
603509798eaSPedro F. Giffuni #define HAMMER_HEAD_TYPE_REDO	0x0044U		/* data REDO / fast fsync */
604509798eaSPedro F. Giffuni 
605509798eaSPedro F. Giffuni #define HAMMER_HEAD_SIGNATURE	0xC84EU
606509798eaSPedro F. Giffuni #define HAMMER_TAIL_SIGNATURE	0xC74FU
607509798eaSPedro F. Giffuni 
608509798eaSPedro F. Giffuni /*
609509798eaSPedro F. Giffuni  * Misc FIFO structures.
610509798eaSPedro F. Giffuni  *
611509798eaSPedro F. Giffuni  * UNDO - Raw meta-data media updates.
612509798eaSPedro F. Giffuni  */
613509798eaSPedro F. Giffuni typedef struct hammer_fifo_undo {
614509798eaSPedro F. Giffuni 	struct hammer_fifo_head	head;
615509798eaSPedro F. Giffuni 	hammer_off_t		undo_offset;	/* zone-1,2 offset */
616509798eaSPedro F. Giffuni 	int32_t			undo_data_bytes;
617509798eaSPedro F. Giffuni 	int32_t			undo_reserved01;
618509798eaSPedro F. Giffuni 	/* followed by data */
619509798eaSPedro F. Giffuni } *hammer_fifo_undo_t;
620509798eaSPedro F. Giffuni 
621509798eaSPedro F. Giffuni /*
622509798eaSPedro F. Giffuni  * REDO (HAMMER version 4+) - Logical file writes/truncates.
623509798eaSPedro F. Giffuni  *
624509798eaSPedro F. Giffuni  * REDOs contain information which will be duplicated in a later meta-data
625509798eaSPedro F. Giffuni  * update, allowing fast write()+fsync() operations.  REDOs can be ignored
626509798eaSPedro F. Giffuni  * without harming filesystem integrity but must be processed if fsync()
627509798eaSPedro F. Giffuni  * semantics are desired.
628509798eaSPedro F. Giffuni  *
629509798eaSPedro F. Giffuni  * Unlike UNDOs which are processed backwards within the recovery span,
630509798eaSPedro F. Giffuni  * REDOs must be processed forwards starting further back (starting outside
631509798eaSPedro F. Giffuni  * the recovery span).
632509798eaSPedro F. Giffuni  *
633509798eaSPedro F. Giffuni  *	WRITE	- Write logical file (with payload).  Executed both
634509798eaSPedro F. Giffuni  *		  out-of-span and in-span.  Out-of-span WRITEs may be
635509798eaSPedro F. Giffuni  *		  filtered out by TERMs.
636509798eaSPedro F. Giffuni  *
637509798eaSPedro F. Giffuni  *	TRUNC	- Truncate logical file (no payload).  Executed both
638509798eaSPedro F. Giffuni  *		  out-of-span and in-span.  Out-of-span WRITEs may be
639509798eaSPedro F. Giffuni  *		  filtered out by TERMs.
640509798eaSPedro F. Giffuni  *
641509798eaSPedro F. Giffuni  *	TERM_*	- Indicates meta-data was committed (if out-of-span) or
642509798eaSPedro F. Giffuni  *		  will be rolled-back (in-span).  Any out-of-span TERMs
643509798eaSPedro F. Giffuni  *		  matching earlier WRITEs remove those WRITEs from
644509798eaSPedro F. Giffuni  *		  consideration as they might conflict with a later data
645509798eaSPedro F. Giffuni  *		  commit (which is not being rolled-back).
646509798eaSPedro F. Giffuni  *
647509798eaSPedro F. Giffuni  *	SYNC	- The earliest in-span SYNC (the last one when scanning
648509798eaSPedro F. Giffuni  *		  backwards) tells the recovery code how far out-of-span
649509798eaSPedro F. Giffuni  *		  it must go to run REDOs.
650509798eaSPedro F. Giffuni  *
651509798eaSPedro F. Giffuni  * NOTE: WRITEs do not always have matching TERMs even under
652509798eaSPedro F. Giffuni  *	 perfect conditions because truncations might remove the
653509798eaSPedro F. Giffuni  *	 buffers from consideration.  I/O problems can also remove
654509798eaSPedro F. Giffuni  *	 buffers from consideration.
655509798eaSPedro F. Giffuni  *
656509798eaSPedro F. Giffuni  *	 TRUNCSs do not always have matching TERMs because several
657509798eaSPedro F. Giffuni  *	 truncations may be aggregated together into a single TERM.
658509798eaSPedro F. Giffuni  */
659509798eaSPedro F. Giffuni typedef struct hammer_fifo_redo {
660509798eaSPedro F. Giffuni 	struct hammer_fifo_head	head;
661509798eaSPedro F. Giffuni 	int64_t			redo_objid;	/* file being written */
662509798eaSPedro F. Giffuni 	hammer_off_t		redo_offset;	/* logical offset in file */
663509798eaSPedro F. Giffuni 	int32_t			redo_data_bytes;
664509798eaSPedro F. Giffuni 	uint32_t		redo_flags;
665509798eaSPedro F. Giffuni 	uint32_t		redo_localization;
666509798eaSPedro F. Giffuni 	uint32_t		redo_reserved01;
667509798eaSPedro F. Giffuni 	uint64_t		redo_reserved02;
668509798eaSPedro F. Giffuni 	/* followed by data */
669509798eaSPedro F. Giffuni } *hammer_fifo_redo_t;
670509798eaSPedro F. Giffuni 
671509798eaSPedro F. Giffuni #define HAMMER_REDO_WRITE	0x00000001
672509798eaSPedro F. Giffuni #define HAMMER_REDO_TRUNC	0x00000002
673509798eaSPedro F. Giffuni #define HAMMER_REDO_TERM_WRITE	0x00000004
674509798eaSPedro F. Giffuni #define HAMMER_REDO_TERM_TRUNC	0x00000008
675509798eaSPedro F. Giffuni #define HAMMER_REDO_SYNC	0x00000010
676509798eaSPedro F. Giffuni 
677509798eaSPedro F. Giffuni typedef union hammer_fifo_any {
678509798eaSPedro F. Giffuni 	struct hammer_fifo_head	head;
679509798eaSPedro F. Giffuni 	struct hammer_fifo_undo	undo;
680509798eaSPedro F. Giffuni 	struct hammer_fifo_redo	redo;
681509798eaSPedro F. Giffuni } *hammer_fifo_any_t;
682509798eaSPedro F. Giffuni 
683509798eaSPedro F. Giffuni /*
684509798eaSPedro F. Giffuni  * Volume header types
685509798eaSPedro F. Giffuni  */
686509798eaSPedro F. Giffuni #define HAMMER_FSBUF_VOLUME	0xC8414D4DC5523031ULL	/* HAMMER01 */
687509798eaSPedro F. Giffuni #define HAMMER_FSBUF_VOLUME_REV	0x313052C54D4D41C8ULL	/* (reverse endian) */
688509798eaSPedro F. Giffuni 
689509798eaSPedro F. Giffuni /*
690509798eaSPedro F. Giffuni  * HAMMER Volume header
691509798eaSPedro F. Giffuni  *
692509798eaSPedro F. Giffuni  * A HAMMER filesystem can be built from 1-256 block devices, each block
693509798eaSPedro F. Giffuni  * device contains a volume header followed by however many buffers fit
694509798eaSPedro F. Giffuni  * into the volume.
695509798eaSPedro F. Giffuni  *
696509798eaSPedro F. Giffuni  * One of the volumes making up a HAMMER filesystem is the root volume.
697509798eaSPedro F. Giffuni  * The root volume is always volume #0 which is the first block device path
698509798eaSPedro F. Giffuni  * specified by newfs_hammer(8).  All HAMMER volumes have a volume header,
699509798eaSPedro F. Giffuni  * however the root volume may be the only volume that has valid values for
700509798eaSPedro F. Giffuni  * some fields in the header.
701509798eaSPedro F. Giffuni  *
702509798eaSPedro F. Giffuni  * Special field notes:
703509798eaSPedro F. Giffuni  *
704509798eaSPedro F. Giffuni  *	vol_bot_beg - offset of boot area (mem_beg - bot_beg bytes)
705509798eaSPedro F. Giffuni  *	vol_mem_beg - offset of memory log (buf_beg - mem_beg bytes)
706509798eaSPedro F. Giffuni  *	vol_buf_beg - offset of the first buffer in volume
707509798eaSPedro F. Giffuni  *	vol_buf_end - offset of volume EOF (on buffer boundary)
708509798eaSPedro F. Giffuni  *
709509798eaSPedro F. Giffuni  *	The memory log area allows a kernel to cache new records and data
710509798eaSPedro F. Giffuni  *	in memory without allocating space in the actual filesystem to hold
711509798eaSPedro F. Giffuni  *	the records and data.  In the event that a filesystem becomes full,
712509798eaSPedro F. Giffuni  *	any records remaining in memory can be flushed to the memory log
713509798eaSPedro F. Giffuni  *	area.  This allows the kernel to immediately return success.
714509798eaSPedro F. Giffuni  *
715509798eaSPedro F. Giffuni  *	The buffer offset is a physical offset of zone-2 offset. The lower
716509798eaSPedro F. Giffuni  *	52 bits of the zone-2 offset is added to the buffer offset of each
717509798eaSPedro F. Giffuni  *	volume to generate an actual I/O offset within the block device.
718509798eaSPedro F. Giffuni  *
719509798eaSPedro F. Giffuni  *	NOTE: boot area and memory log are currently not used.
720509798eaSPedro F. Giffuni  */
721509798eaSPedro F. Giffuni 
722509798eaSPedro F. Giffuni /*
723509798eaSPedro F. Giffuni  * Filesystem type string
724509798eaSPedro F. Giffuni  */
725509798eaSPedro F. Giffuni #define HAMMER_FSTYPE_STRING		"DragonFly HAMMER"
726509798eaSPedro F. Giffuni 
727509798eaSPedro F. Giffuni /*
728509798eaSPedro F. Giffuni  * These macros are only used by userspace when userspace commands either
729509798eaSPedro F. Giffuni  * initialize or add a new HAMMER volume.
730509798eaSPedro F. Giffuni  */
731509798eaSPedro F. Giffuni #define HAMMER_BOOT_MINBYTES		(32*1024)
732509798eaSPedro F. Giffuni #define HAMMER_BOOT_NOMBYTES		(64LL*1024*1024)
733509798eaSPedro F. Giffuni #define HAMMER_BOOT_MAXBYTES		(256LL*1024*1024)
734509798eaSPedro F. Giffuni 
735509798eaSPedro F. Giffuni #define HAMMER_MEM_MINBYTES		(256*1024)
736509798eaSPedro F. Giffuni #define HAMMER_MEM_NOMBYTES		(1LL*1024*1024*1024)
737509798eaSPedro F. Giffuni #define HAMMER_MEM_MAXBYTES		(64LL*1024*1024*1024)
738509798eaSPedro F. Giffuni 
739509798eaSPedro F. Giffuni typedef struct hammer_volume_ondisk {
740509798eaSPedro F. Giffuni 	uint64_t vol_signature;	/* HAMMER_FSBUF_VOLUME for a valid header */
741509798eaSPedro F. Giffuni 
742509798eaSPedro F. Giffuni 	/*
743509798eaSPedro F. Giffuni 	 * These are relative to block device offset, not zone offsets.
744509798eaSPedro F. Giffuni 	 */
745509798eaSPedro F. Giffuni 	int64_t vol_bot_beg;	/* offset of boot area */
746509798eaSPedro F. Giffuni 	int64_t vol_mem_beg;	/* offset of memory log */
747509798eaSPedro F. Giffuni 	int64_t vol_buf_beg;	/* offset of the first buffer in volume */
748509798eaSPedro F. Giffuni 	int64_t vol_buf_end;	/* offset of volume EOF (on buffer boundary) */
749509798eaSPedro F. Giffuni 	int64_t vol_reserved01;
750509798eaSPedro F. Giffuni 
751509798eaSPedro F. Giffuni 	hammer_uuid_t vol_fsid;	/* identify filesystem */
752509798eaSPedro F. Giffuni 	hammer_uuid_t vol_fstype; /* identify filesystem type */
753509798eaSPedro F. Giffuni 	char vol_label[64];	/* filesystem label */
754509798eaSPedro F. Giffuni 
755509798eaSPedro F. Giffuni 	int32_t vol_no;		/* volume number within filesystem */
756509798eaSPedro F. Giffuni 	int32_t vol_count;	/* number of volumes making up filesystem */
757509798eaSPedro F. Giffuni 
758509798eaSPedro F. Giffuni 	uint32_t vol_version;	/* version control information */
759509798eaSPedro F. Giffuni 	hammer_crc_t vol_crc;	/* header crc */
760509798eaSPedro F. Giffuni 	uint32_t vol_flags;	/* volume flags */
761509798eaSPedro F. Giffuni 	uint32_t vol_rootvol;	/* the root volume number (must be 0) */
762509798eaSPedro F. Giffuni 
763509798eaSPedro F. Giffuni 	uint32_t vol_reserved[8];
764509798eaSPedro F. Giffuni 
765509798eaSPedro F. Giffuni 	/*
766509798eaSPedro F. Giffuni 	 * These fields are initialized and space is reserved in every
7674e12c7c5SGordon Bergling 	 * volume making up a HAMMER filesystem, but only the root volume
768509798eaSPedro F. Giffuni 	 * contains valid data.  Note that vol0_stat_bigblocks does not
769509798eaSPedro F. Giffuni 	 * include big-blocks for freemap and undomap initially allocated
770509798eaSPedro F. Giffuni 	 * by newfs_hammer(8).
771509798eaSPedro F. Giffuni 	 */
772509798eaSPedro F. Giffuni 	int64_t vol0_stat_bigblocks;	/* total big-blocks when fs is empty */
773509798eaSPedro F. Giffuni 	int64_t vol0_stat_freebigblocks;/* number of free big-blocks */
774509798eaSPedro F. Giffuni 	int64_t	vol0_reserved01;
775509798eaSPedro F. Giffuni 	int64_t vol0_stat_inodes;	/* for statfs only */
776509798eaSPedro F. Giffuni 	int64_t vol0_reserved02;
777509798eaSPedro F. Giffuni 	hammer_off_t vol0_btree_root;	/* B-Tree root offset in zone-8 */
778509798eaSPedro F. Giffuni 	hammer_tid_t vol0_next_tid;	/* highest partially synchronized TID */
779509798eaSPedro F. Giffuni 	hammer_off_t vol0_reserved03;
780509798eaSPedro F. Giffuni 
781509798eaSPedro F. Giffuni 	/*
782509798eaSPedro F. Giffuni 	 * Blockmaps for zones.  Not all zones use a blockmap.  Note that
783509798eaSPedro F. Giffuni 	 * the entire root blockmap is cached in the hammer_mount structure.
784509798eaSPedro F. Giffuni 	 */
785509798eaSPedro F. Giffuni 	struct hammer_blockmap	vol0_blockmap[HAMMER_MAX_ZONES];
786509798eaSPedro F. Giffuni 
787509798eaSPedro F. Giffuni 	/*
788509798eaSPedro F. Giffuni 	 * Array of zone-2 addresses for undo FIFO.
789509798eaSPedro F. Giffuni 	 */
790509798eaSPedro F. Giffuni 	hammer_off_t		vol0_undo_array[HAMMER_MAX_UNDO_BIGBLOCKS];
791509798eaSPedro F. Giffuni } *hammer_volume_ondisk_t;
792509798eaSPedro F. Giffuni 
793509798eaSPedro F. Giffuni #define HAMMER_ROOT_VOLNO		0
794509798eaSPedro F. Giffuni 
795509798eaSPedro F. Giffuni #define HAMMER_VOLF_NEEDFLUSH		0x0004	/* volume needs flush */
796509798eaSPedro F. Giffuni 
797509798eaSPedro F. Giffuni #define HAMMER_VOL_CRCSIZE1	\
798509798eaSPedro F. Giffuni 	offsetof(struct hammer_volume_ondisk, vol_crc)
799509798eaSPedro F. Giffuni #define HAMMER_VOL_CRCSIZE2	\
800509798eaSPedro F. Giffuni 	(sizeof(struct hammer_volume_ondisk) - HAMMER_VOL_CRCSIZE1 -	\
801509798eaSPedro F. Giffuni 	 sizeof(hammer_crc_t))
802509798eaSPedro F. Giffuni 
803509798eaSPedro F. Giffuni #define HAMMER_VOL_VERSION_MIN		1	/* minimum supported version */
804509798eaSPedro F. Giffuni #define HAMMER_VOL_VERSION_DEFAULT	7	/* newfs default version */
805509798eaSPedro F. Giffuni #define HAMMER_VOL_VERSION_WIP		8	/* version >= this is WIP */
806509798eaSPedro F. Giffuni #define HAMMER_VOL_VERSION_MAX		7	/* maximum supported version */
807509798eaSPedro F. Giffuni 
808509798eaSPedro F. Giffuni #define HAMMER_VOL_VERSION_ONE		1
809509798eaSPedro F. Giffuni #define HAMMER_VOL_VERSION_TWO		2	/* new dirent layout (2.3+) */
810509798eaSPedro F. Giffuni #define HAMMER_VOL_VERSION_THREE	3	/* new snapshot layout (2.5+) */
811509798eaSPedro F. Giffuni #define HAMMER_VOL_VERSION_FOUR		4	/* new undo/flush (2.5+) */
812509798eaSPedro F. Giffuni #define HAMMER_VOL_VERSION_FIVE		5	/* dedup (2.9+) */
813509798eaSPedro F. Giffuni #define HAMMER_VOL_VERSION_SIX		6	/* DIRHASH_ALG1 */
814509798eaSPedro F. Giffuni #define HAMMER_VOL_VERSION_SEVEN	7	/* use the faster iscsi_crc */
815509798eaSPedro F. Giffuni 
816509798eaSPedro F. Giffuni /*
817509798eaSPedro F. Giffuni  * Translate a zone-2 address to physical address
818509798eaSPedro F. Giffuni  */
819509798eaSPedro F. Giffuni #define hammer_xlate_to_phys(volume, zone2_offset)	\
820509798eaSPedro F. Giffuni 	((volume)->vol_buf_beg + HAMMER_OFF_SHORT_ENCODE(zone2_offset))
821509798eaSPedro F. Giffuni 
822509798eaSPedro F. Giffuni /*
823509798eaSPedro F. Giffuni  * Translate a zone-3 address to zone-2 address
824509798eaSPedro F. Giffuni  */
825509798eaSPedro F. Giffuni #define HAMMER_UNDO_INDEX(zone3_offset)			\
826509798eaSPedro F. Giffuni 	(HAMMER_OFF_SHORT_ENCODE(zone3_offset) / HAMMER_BIGBLOCK_SIZE)
827509798eaSPedro F. Giffuni 
828509798eaSPedro F. Giffuni #define hammer_xlate_to_undo(volume, zone3_offset)			\
829509798eaSPedro F. Giffuni 	((volume)->vol0_undo_array[HAMMER_UNDO_INDEX(zone3_offset)] +	\
830509798eaSPedro F. Giffuni 	 (zone3_offset & HAMMER_BIGBLOCK_MASK64))
831509798eaSPedro F. Giffuni 
832509798eaSPedro F. Giffuni /*
833509798eaSPedro F. Giffuni  * Effective per-volume filesystem capacity including big-blocks for layer1/2
834509798eaSPedro F. Giffuni  */
835509798eaSPedro F. Giffuni #define HAMMER_VOL_BUF_SIZE(volume)			\
836509798eaSPedro F. Giffuni 	((volume)->vol_buf_end - (volume)->vol_buf_beg)
837509798eaSPedro F. Giffuni 
838509798eaSPedro F. Giffuni /*
839509798eaSPedro F. Giffuni  * Record types are fairly straightforward.  The B-Tree includes the record
840509798eaSPedro F. Giffuni  * type in its index sort.
841509798eaSPedro F. Giffuni  */
842509798eaSPedro F. Giffuni #define HAMMER_RECTYPE_UNKNOWN		0x0000
843509798eaSPedro F. Giffuni #define HAMMER_RECTYPE_INODE		0x0001	/* inode in obj_id space */
844509798eaSPedro F. Giffuni #define HAMMER_RECTYPE_DATA		0x0010
845509798eaSPedro F. Giffuni #define HAMMER_RECTYPE_DIRENTRY		0x0011
846509798eaSPedro F. Giffuni #define HAMMER_RECTYPE_DB		0x0012
847509798eaSPedro F. Giffuni #define HAMMER_RECTYPE_EXT		0x0013	/* ext attributes */
848509798eaSPedro F. Giffuni #define HAMMER_RECTYPE_FIX		0x0014	/* fixed attribute */
849509798eaSPedro F. Giffuni #define HAMMER_RECTYPE_PFS		0x0015	/* PFS management */
850509798eaSPedro F. Giffuni #define HAMMER_RECTYPE_SNAPSHOT		0x0016	/* Snapshot management */
851509798eaSPedro F. Giffuni #define HAMMER_RECTYPE_CONFIG		0x0017	/* hammer cleanup config */
852509798eaSPedro F. Giffuni #define HAMMER_RECTYPE_MAX		0xFFFF
853509798eaSPedro F. Giffuni 
854509798eaSPedro F. Giffuni #define HAMMER_RECTYPE_ENTRY_START	(HAMMER_RECTYPE_INODE + 1)
855509798eaSPedro F. Giffuni #define HAMMER_RECTYPE_CLEAN_START	HAMMER_RECTYPE_EXT
856509798eaSPedro F. Giffuni 
857509798eaSPedro F. Giffuni #define HAMMER_FIXKEY_SYMLINK		1
858509798eaSPedro F. Giffuni 
859509798eaSPedro F. Giffuni #define HAMMER_OBJTYPE_UNKNOWN		0	/* never exists on-disk as unknown */
860509798eaSPedro F. Giffuni #define HAMMER_OBJTYPE_DIRECTORY	1
861509798eaSPedro F. Giffuni #define HAMMER_OBJTYPE_REGFILE		2
862509798eaSPedro F. Giffuni #define HAMMER_OBJTYPE_DBFILE		3
863509798eaSPedro F. Giffuni #define HAMMER_OBJTYPE_FIFO		4
864509798eaSPedro F. Giffuni #define HAMMER_OBJTYPE_CDEV		5
865509798eaSPedro F. Giffuni #define HAMMER_OBJTYPE_BDEV		6
866509798eaSPedro F. Giffuni #define HAMMER_OBJTYPE_SOFTLINK		7
867509798eaSPedro F. Giffuni #define HAMMER_OBJTYPE_PSEUDOFS		8	/* pseudo filesystem obj */
868509798eaSPedro F. Giffuni #define HAMMER_OBJTYPE_SOCKET		9
869509798eaSPedro F. Giffuni 
870509798eaSPedro F. Giffuni /*
871509798eaSPedro F. Giffuni  * HAMMER inode attribute data
872509798eaSPedro F. Giffuni  *
873509798eaSPedro F. Giffuni  * The data reference for a HAMMER inode points to this structure.  Any
874509798eaSPedro F. Giffuni  * modifications to the contents of this structure will result in a
875509798eaSPedro F. Giffuni  * replacement operation.
876509798eaSPedro F. Giffuni  *
877509798eaSPedro F. Giffuni  * parent_obj_id is only valid for directories (which cannot be hard-linked),
878509798eaSPedro F. Giffuni  * and specifies the parent directory obj_id.  This field will also be set
879509798eaSPedro F. Giffuni  * for non-directory inodes as a recovery aid, but can wind up holding
880509798eaSPedro F. Giffuni  * stale information.  However, since object id's are not reused, the worse
881509798eaSPedro F. Giffuni  * that happens is that the recovery code is unable to use it.
882509798eaSPedro F. Giffuni  * A parent_obj_id of 0 means it's a root inode of root or non-root PFS.
883509798eaSPedro F. Giffuni  *
884509798eaSPedro F. Giffuni  * NOTE: Future note on directory hardlinks.  We can implement a record type
885509798eaSPedro F. Giffuni  * which allows us to point to multiple parent directories.
886509798eaSPedro F. Giffuni  */
887509798eaSPedro F. Giffuni typedef struct hammer_inode_data {
888509798eaSPedro F. Giffuni 	uint16_t version;	/* inode data version */
889509798eaSPedro F. Giffuni 	uint16_t mode;		/* basic unix permissions */
890509798eaSPedro F. Giffuni 	uint32_t uflags;	/* chflags */
891509798eaSPedro F. Giffuni 	uint32_t rmajor;	/* used by device nodes */
892509798eaSPedro F. Giffuni 	uint32_t rminor;	/* used by device nodes */
893509798eaSPedro F. Giffuni 	uint64_t ctime;
894509798eaSPedro F. Giffuni 	int64_t parent_obj_id;	/* parent directory obj_id */
895509798eaSPedro F. Giffuni 	hammer_uuid_t uid;
896509798eaSPedro F. Giffuni 	hammer_uuid_t gid;
897509798eaSPedro F. Giffuni 
898509798eaSPedro F. Giffuni 	uint8_t obj_type;
899509798eaSPedro F. Giffuni 	uint8_t cap_flags;	/* capability support flags (extension) */
900509798eaSPedro F. Giffuni 	uint16_t reserved01;
901509798eaSPedro F. Giffuni 	uint32_t reserved02;
902509798eaSPedro F. Giffuni 	uint64_t nlinks;	/* hard links */
903509798eaSPedro F. Giffuni 	uint64_t size;		/* filesystem object size */
904509798eaSPedro F. Giffuni 	union {
905509798eaSPedro F. Giffuni 		char	symlink[24];	/* HAMMER_INODE_BASESYMLEN */
906509798eaSPedro F. Giffuni 	} ext;
907509798eaSPedro F. Giffuni 	uint64_t mtime;	/* mtime must be second-to-last */
908509798eaSPedro F. Giffuni 	uint64_t atime;	/* atime must be last */
909509798eaSPedro F. Giffuni } *hammer_inode_data_t;
910509798eaSPedro F. Giffuni 
911509798eaSPedro F. Giffuni /*
912fe4fa932SElyes Haouas  * Neither mtime nor atime updates are CRCd by the B-Tree element.
913509798eaSPedro F. Giffuni  * mtime updates have UNDO, atime updates do not.
914509798eaSPedro F. Giffuni  */
915509798eaSPedro F. Giffuni #define HAMMER_INODE_CRCSIZE	\
916509798eaSPedro F. Giffuni 	offsetof(struct hammer_inode_data, mtime)
917509798eaSPedro F. Giffuni 
918509798eaSPedro F. Giffuni #define HAMMER_INODE_DATA_VERSION	1
919509798eaSPedro F. Giffuni #define HAMMER_OBJID_ROOT		1	/* root inodes # */
920509798eaSPedro F. Giffuni #define HAMMER_INODE_BASESYMLEN		24	/* see ext.symlink */
921509798eaSPedro F. Giffuni 
922509798eaSPedro F. Giffuni /*
923509798eaSPedro F. Giffuni  * Capability & implementation flags.
924509798eaSPedro F. Giffuni  *
925509798eaSPedro F. Giffuni  * HAMMER_INODE_CAP_DIR_LOCAL_INO - Use inode B-Tree localization
926509798eaSPedro F. Giffuni  * for directory entries.  Also see HAMMER_DIR_INODE_LOCALIZATION().
927509798eaSPedro F. Giffuni  */
928509798eaSPedro F. Giffuni #define HAMMER_INODE_CAP_DIRHASH_MASK	0x03	/* directory: hash algorithm */
929509798eaSPedro F. Giffuni #define HAMMER_INODE_CAP_DIRHASH_ALG0	0x00
930509798eaSPedro F. Giffuni #define HAMMER_INODE_CAP_DIRHASH_ALG1	0x01
931509798eaSPedro F. Giffuni #define HAMMER_INODE_CAP_DIRHASH_ALG2	0x02
932509798eaSPedro F. Giffuni #define HAMMER_INODE_CAP_DIRHASH_ALG3	0x03
933509798eaSPedro F. Giffuni #define HAMMER_INODE_CAP_DIR_LOCAL_INO	0x04	/* use inode localization */
934509798eaSPedro F. Giffuni 
935509798eaSPedro F. Giffuni #define HAMMER_DATA_DOALIGN(offset)				\
936509798eaSPedro F. Giffuni 	(((offset) + 15) & ~15)
937509798eaSPedro F. Giffuni #define HAMMER_DATA_DOALIGN_WITH(type, offset)			\
938509798eaSPedro F. Giffuni 	(((type)(offset) + 15) & (~(type)15))
939509798eaSPedro F. Giffuni 
940509798eaSPedro F. Giffuni /*
941509798eaSPedro F. Giffuni  * A HAMMER directory entry associates a HAMMER filesystem object with a
942509798eaSPedro F. Giffuni  * namespace.  It is hooked into a pseudo-filesystem (with its own inode
943509798eaSPedro F. Giffuni  * numbering space) in the filesystem by setting the high 16 bits of the
944509798eaSPedro F. Giffuni  * localization field.  The low 16 bits must be 0 and are reserved for
945509798eaSPedro F. Giffuni  * future use.
946509798eaSPedro F. Giffuni  *
947509798eaSPedro F. Giffuni  * Directory entries are indexed with a 128 bit namekey rather then an
948509798eaSPedro F. Giffuni  * offset.  A portion of the namekey is an iterator/randomizer to deal
949509798eaSPedro F. Giffuni  * with collisions.
950509798eaSPedro F. Giffuni  *
951509798eaSPedro F. Giffuni  * NOTE: leaf.base.obj_type from the related B-Tree leaf entry holds
952509798eaSPedro F. Giffuni  * the filesystem object type of obj_id, e.g. a den_type equivalent.
953509798eaSPedro F. Giffuni  * It is not stored in hammer_direntry_data.
954509798eaSPedro F. Giffuni  *
955509798eaSPedro F. Giffuni  * NOTE: name field / the filename data reference is NOT terminated with \0.
956509798eaSPedro F. Giffuni  */
957509798eaSPedro F. Giffuni typedef struct hammer_direntry_data {
958509798eaSPedro F. Giffuni 	int64_t obj_id;			/* object being referenced */
959509798eaSPedro F. Giffuni 	uint32_t localization;		/* identify pseudo-filesystem */
960509798eaSPedro F. Giffuni 	uint32_t reserved01;
961509798eaSPedro F. Giffuni 	char	name[16];		/* name (extended) */
962509798eaSPedro F. Giffuni } *hammer_direntry_data_t;
963509798eaSPedro F. Giffuni 
964509798eaSPedro F. Giffuni #define HAMMER_ENTRY_NAME_OFF	offsetof(struct hammer_direntry_data, name[0])
965509798eaSPedro F. Giffuni #define HAMMER_ENTRY_SIZE(nlen)	offsetof(struct hammer_direntry_data, name[nlen])
966509798eaSPedro F. Giffuni 
967509798eaSPedro F. Giffuni /*
968509798eaSPedro F. Giffuni  * Symlink data which does not fit in the inode is stored in a separate
969509798eaSPedro F. Giffuni  * FIX type record.
970509798eaSPedro F. Giffuni  */
971509798eaSPedro F. Giffuni typedef struct hammer_symlink_data {
972509798eaSPedro F. Giffuni 	char	name[16];		/* name (extended) */
973509798eaSPedro F. Giffuni } *hammer_symlink_data_t;
974509798eaSPedro F. Giffuni 
975509798eaSPedro F. Giffuni #define HAMMER_SYMLINK_NAME_OFF	offsetof(struct hammer_symlink_data, name[0])
976509798eaSPedro F. Giffuni 
977509798eaSPedro F. Giffuni /*
978509798eaSPedro F. Giffuni  * The root inode for the primary filesystem and root inode for any
979509798eaSPedro F. Giffuni  * pseudo-fs may be tagged with an optional data structure using
980509798eaSPedro F. Giffuni  * HAMMER_RECTYPE_PFS and localization id.  This structure allows
981509798eaSPedro F. Giffuni  * the node to be used as a mirroring master or slave.
982509798eaSPedro F. Giffuni  *
983509798eaSPedro F. Giffuni  * When operating as a slave CD's into the node automatically become read-only
984509798eaSPedro F. Giffuni  * and as-of sync_end_tid.
985509798eaSPedro F. Giffuni  *
986509798eaSPedro F. Giffuni  * When operating as a master the read PFSD info sets sync_end_tid to
987509798eaSPedro F. Giffuni  * the most recently flushed TID.
988509798eaSPedro F. Giffuni  *
989509798eaSPedro F. Giffuni  * sync_low_tid is not yet used but will represent the highest pruning
990509798eaSPedro F. Giffuni  * end-point, after which full history is available.
991509798eaSPedro F. Giffuni  *
992509798eaSPedro F. Giffuni  * We need to pack this structure making it equally sized on both 32-bit and
993509798eaSPedro F. Giffuni  * 64-bit machines as it is part of struct hammer_ioc_mrecord_pfs which is
994509798eaSPedro F. Giffuni  * send over the wire in hammer mirror operations. Only on 64-bit machines
995509798eaSPedro F. Giffuni  * the size of this struct differ when packed or not. This leads us to the
996509798eaSPedro F. Giffuni  * situation where old 64-bit systems (using the non-packed structure),
997509798eaSPedro F. Giffuni  * which were never able to mirror to/from 32-bit systems, are now no longer
998509798eaSPedro F. Giffuni  * able to mirror to/from newer 64-bit systems (using the packed structure).
999509798eaSPedro F. Giffuni  */
1000509798eaSPedro F. Giffuni struct hammer_pseudofs_data {
1001509798eaSPedro F. Giffuni 	hammer_tid_t	sync_low_tid;	/* full history beyond this point */
1002509798eaSPedro F. Giffuni 	hammer_tid_t	sync_beg_tid;	/* earliest tid w/ full history avail */
1003509798eaSPedro F. Giffuni 	hammer_tid_t	sync_end_tid;	/* current synchronizatoin point */
1004509798eaSPedro F. Giffuni 	uint64_t	sync_beg_ts;	/* real-time of last completed sync */
1005509798eaSPedro F. Giffuni 	uint64_t	sync_end_ts;	/* initiation of current sync cycle */
1006509798eaSPedro F. Giffuni 	hammer_uuid_t	shared_uuid;	/* shared uuid (match required) */
1007509798eaSPedro F. Giffuni 	hammer_uuid_t	unique_uuid;	/* unique uuid of this master/slave */
1008509798eaSPedro F. Giffuni 	int32_t		reserved01;	/* reserved for future master_id */
1009509798eaSPedro F. Giffuni 	int32_t		mirror_flags;	/* misc flags */
1010509798eaSPedro F. Giffuni 	char		label[64];	/* filesystem space label */
1011509798eaSPedro F. Giffuni 	char		snapshots[64];	/* softlink dir for pruning */
1012509798eaSPedro F. Giffuni 	int32_t		reserved02;	/* was prune_{time,freq} */
1013509798eaSPedro F. Giffuni 	int32_t		reserved03;	/* was reblock_{time,freq} */
1014509798eaSPedro F. Giffuni 	int32_t		reserved04;	/* was snapshot_freq */
1015509798eaSPedro F. Giffuni 	int32_t		prune_min;	/* do not prune recent history */
1016509798eaSPedro F. Giffuni 	int32_t		prune_max;	/* do not retain history beyond here */
1017509798eaSPedro F. Giffuni 	int32_t		reserved[16];
1018509798eaSPedro F. Giffuni } __packed;
1019509798eaSPedro F. Giffuni 
1020509798eaSPedro F. Giffuni typedef struct hammer_pseudofs_data *hammer_pseudofs_data_t;
1021509798eaSPedro F. Giffuni 
1022509798eaSPedro F. Giffuni #define HAMMER_PFSD_SLAVE	0x00000001
1023509798eaSPedro F. Giffuni #define HAMMER_PFSD_DELETED	0x80000000
1024509798eaSPedro F. Giffuni 
1025509798eaSPedro F. Giffuni #define hammer_is_pfs_slave(pfsd)			\
1026509798eaSPedro F. Giffuni 	(((pfsd)->mirror_flags & HAMMER_PFSD_SLAVE) != 0)
1027509798eaSPedro F. Giffuni #define hammer_is_pfs_master(pfsd)			\
1028509798eaSPedro F. Giffuni 	(!hammer_is_pfs_slave(pfsd))
1029509798eaSPedro F. Giffuni #define hammer_is_pfs_deleted(pfsd)			\
1030509798eaSPedro F. Giffuni 	(((pfsd)->mirror_flags & HAMMER_PFSD_DELETED) != 0)
1031509798eaSPedro F. Giffuni 
1032509798eaSPedro F. Giffuni #define HAMMER_MAX_PFS		65536
1033509798eaSPedro F. Giffuni #define HAMMER_MAX_PFSID	(HAMMER_MAX_PFS - 1)
1034509798eaSPedro F. Giffuni #define HAMMER_ROOT_PFSID	0
1035509798eaSPedro F. Giffuni 
1036509798eaSPedro F. Giffuni /*
1037509798eaSPedro F. Giffuni  * Snapshot meta-data { Objid = HAMMER_OBJID_ROOT, Key = tid, rectype = SNAPSHOT }.
1038509798eaSPedro F. Giffuni  *
1039509798eaSPedro F. Giffuni  * Snapshot records replace the old <fs>/snapshots/<softlink> methodology.  Snapshot
1040fe4fa932SElyes Haouas  * records are mirrored but may be independently managed once they are laid down on
1041509798eaSPedro F. Giffuni  * a slave.
1042509798eaSPedro F. Giffuni  *
1043509798eaSPedro F. Giffuni  * NOTE: The b-tree key is signed, the tid is not, so callers must still sort the
1044509798eaSPedro F. Giffuni  *	 results.
1045509798eaSPedro F. Giffuni  *
1046509798eaSPedro F. Giffuni  * NOTE: Reserved fields must be zero (as usual)
1047509798eaSPedro F. Giffuni  */
1048509798eaSPedro F. Giffuni typedef struct hammer_snapshot_data {
1049509798eaSPedro F. Giffuni 	hammer_tid_t	tid;		/* the snapshot TID itself (== key) */
1050509798eaSPedro F. Giffuni 	uint64_t	ts;		/* real-time when snapshot was made */
1051509798eaSPedro F. Giffuni 	uint64_t	reserved01;
1052509798eaSPedro F. Giffuni 	uint64_t	reserved02;
1053509798eaSPedro F. Giffuni 	char		label[64];	/* user-supplied description */
1054509798eaSPedro F. Giffuni 	uint64_t	reserved03[4];
1055509798eaSPedro F. Giffuni } *hammer_snapshot_data_t;
1056509798eaSPedro F. Giffuni 
1057509798eaSPedro F. Giffuni /*
1058509798eaSPedro F. Giffuni  * Config meta-data { ObjId = HAMMER_OBJID_ROOT, Key = 0, rectype = CONFIG }.
1059509798eaSPedro F. Giffuni  *
1060509798eaSPedro F. Giffuni  * Used to store the hammer cleanup config.  This data is not mirrored.
1061509798eaSPedro F. Giffuni  */
1062509798eaSPedro F. Giffuni typedef struct hammer_config_data {
1063509798eaSPedro F. Giffuni 	char		text[1024];
1064509798eaSPedro F. Giffuni } *hammer_config_data_t;
1065509798eaSPedro F. Giffuni 
1066509798eaSPedro F. Giffuni /*
1067509798eaSPedro F. Giffuni  * Rollup various structures embedded as record data
1068509798eaSPedro F. Giffuni  */
1069509798eaSPedro F. Giffuni typedef union hammer_data_ondisk {
1070509798eaSPedro F. Giffuni 	struct hammer_direntry_data entry;
1071509798eaSPedro F. Giffuni 	struct hammer_inode_data inode;
1072509798eaSPedro F. Giffuni 	struct hammer_symlink_data symlink;
1073509798eaSPedro F. Giffuni 	struct hammer_pseudofs_data pfsd;
1074509798eaSPedro F. Giffuni 	struct hammer_snapshot_data snap;
1075509798eaSPedro F. Giffuni 	struct hammer_config_data config;
1076509798eaSPedro F. Giffuni } *hammer_data_ondisk_t;
1077509798eaSPedro F. Giffuni 
1078509798eaSPedro F. Giffuni /*
1079509798eaSPedro F. Giffuni  * Ondisk layout of B-Tree related structures
1080509798eaSPedro F. Giffuni  */
1081509798eaSPedro F. Giffuni #if 0	 /* Not needed for fstype(8) */
1082509798eaSPedro F. Giffuni #include "hammer_btree.h"
1083509798eaSPedro F. Giffuni #endif
1084509798eaSPedro F. Giffuni 
1085509798eaSPedro F. Giffuni #define HAMMER_DIR_INODE_LOCALIZATION(ino_data)				\
1086509798eaSPedro F. Giffuni 	(((ino_data)->cap_flags & HAMMER_INODE_CAP_DIR_LOCAL_INO) ?	\
1087509798eaSPedro F. Giffuni 	 HAMMER_LOCALIZE_INODE :					\
1088509798eaSPedro F. Giffuni 	 HAMMER_LOCALIZE_MISC)
1089509798eaSPedro F. Giffuni 
1090509798eaSPedro F. Giffuni #endif /* !VFS_HAMMER_DISK_H_ */
1091