sys/vfs/hammer: Add comments on volume version 7 CRC (4c09d9c4)
[dragonfly.git] / sys / vfs / hammer / hammer_crc.h
blob5fcf534d204a29fce35d4b92d17c9828879dc772
1 /*
2 * Copyright (c) 2007-2016 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * SUCH DAMAGE.
35 #ifndef VFS_HAMMER_CRC_H_
36 #define VFS_HAMMER_CRC_H_
38 #include "hammer_disk.h"
39 #include "hammer_ioctl.h"
41 #ifndef _KERNEL
43 * These are only for userspace.
44 * Userspace can't include sys/sys/systm.h.
46 uint32_t crc32(const void *buf, size_t size);
47 uint32_t crc32_ext(const void *buf, size_t size, uint32_t ocrc);
48 uint32_t iscsi_crc32(const void *buf, size_t size);
49 uint32_t iscsi_crc32_ext(const void *buf, size_t size, uint32_t ocrc);
50 #endif
53 * hammer_datacrc() expects vol_version to be the volume version of the
54 * currently mounted HAMMER filesystem to do either of the following.
55 * If vol_version is <= 6, do crc32() as it did in the past.
56 * If vol_version is >= 7, do iscsi_crc32().
58 * hammer_crc_test_xxx() first tests CRC using hammer_datacrc().
59 * If vol_version is <= 6, it's likely to match crc32().
60 * If vol_version is >= 7 and was created using the current newfs_hammer,
61 * it's likely to match iscsi_crc32(). If it's been upgraded, the CRC
62 * generated by old version(s) doesn't match iscsi_crc32(), and requires
63 * retry. This means there is overhead until CRCs for majority of data
64 * and metadata are upgraded.
66 * Once the volume is upgraded, all the newly created data and metadata
67 * are initialized with version 7 CRC. Reblocking the filesystem can
68 * upgrade CRCs upon moving data and metadata.
71 #define hammer_datacrc(vol_version, buf, size) \
72 (((vol_version) >= HAMMER_VOL_VERSION_SEVEN) ? \
73 iscsi_crc32(buf, size) : crc32(buf, size))
75 #define hammer_datacrc_ext(vol_version, buf, size, ocrc) \
76 (((vol_version) >= HAMMER_VOL_VERSION_SEVEN) ? \
77 iscsi_crc32_ext(buf, size, ocrc) : crc32_ext(buf, size, ocrc))
79 static __inline hammer_crc_t
80 hammer_crc_get_blockmap(uint32_t vol_version, hammer_blockmap_t blockmap)
82 return(hammer_datacrc(vol_version, blockmap, HAMMER_BLOCKMAP_CRCSIZE));
85 static __inline void
86 hammer_crc_set_blockmap(uint32_t vol_version, hammer_blockmap_t blockmap)
88 blockmap->entry_crc = hammer_crc_get_blockmap(vol_version, blockmap);
91 static __inline int
92 hammer_crc_test_blockmap(uint32_t vol_version, hammer_blockmap_t blockmap)
94 if (blockmap->entry_crc == hammer_crc_get_blockmap(vol_version, blockmap))
95 return 1;
96 if (vol_version >= HAMMER_VOL_VERSION_SEVEN) {
97 if (blockmap->entry_crc == hammer_crc_get_blockmap(HAMMER_VOL_VERSION_SIX,
98 blockmap)) {
99 return 1;
102 return 0;
105 static __inline hammer_crc_t
106 hammer_crc_get_layer1(uint32_t vol_version, hammer_blockmap_layer1_t layer1)
108 return(hammer_datacrc(vol_version, layer1, HAMMER_LAYER1_CRCSIZE));
111 static __inline void
112 hammer_crc_set_layer1(uint32_t vol_version, hammer_blockmap_layer1_t layer1)
114 layer1->layer1_crc = hammer_crc_get_layer1(vol_version, layer1);
117 static __inline int
118 hammer_crc_test_layer1(uint32_t vol_version, hammer_blockmap_layer1_t layer1)
120 if (layer1->layer1_crc == hammer_crc_get_layer1(vol_version, layer1))
121 return 1;
122 if (vol_version >= HAMMER_VOL_VERSION_SEVEN) {
123 if (layer1->layer1_crc == hammer_crc_get_layer1(HAMMER_VOL_VERSION_SIX, layer1))
124 return 1;
126 return 0;
129 static __inline hammer_crc_t
130 hammer_crc_get_layer2(uint32_t vol_version, hammer_blockmap_layer2_t layer2)
132 return(hammer_datacrc(vol_version, layer2, HAMMER_LAYER2_CRCSIZE));
135 static __inline void
136 hammer_crc_set_layer2(uint32_t vol_version, hammer_blockmap_layer2_t layer2)
138 layer2->entry_crc = hammer_crc_get_layer2(vol_version, layer2);
141 static __inline int
142 hammer_crc_test_layer2(uint32_t vol_version, hammer_blockmap_layer2_t layer2)
144 if (layer2->entry_crc == hammer_crc_get_layer2(vol_version, layer2))
145 return 1;
146 if (vol_version >= HAMMER_VOL_VERSION_SEVEN) {
147 if (layer2->entry_crc == hammer_crc_get_layer2(HAMMER_VOL_VERSION_SIX, layer2))
148 return 1;
150 return 0;
153 static __inline hammer_crc_t
154 hammer_crc_get_volume(uint32_t vol_version, hammer_volume_ondisk_t ondisk)
156 return (hammer_datacrc(vol_version, ondisk, HAMMER_VOL_CRCSIZE1) ^
157 hammer_datacrc(vol_version, &ondisk->vol_crc + 1, HAMMER_VOL_CRCSIZE2));
160 static __inline void
161 hammer_crc_set_volume(uint32_t vol_version, hammer_volume_ondisk_t ondisk)
163 ondisk->vol_crc = hammer_crc_get_volume(vol_version, ondisk);
166 static __inline int
167 hammer_crc_test_volume(uint32_t vol_version, hammer_volume_ondisk_t ondisk)
169 if (ondisk->vol_crc == hammer_crc_get_volume(vol_version, ondisk))
170 return 1;
171 if (vol_version >= HAMMER_VOL_VERSION_SEVEN) {
172 if (ondisk->vol_crc == hammer_crc_get_volume(HAMMER_VOL_VERSION_SIX, ondisk))
173 return 1;
175 return 0;
178 static __inline hammer_crc_t
179 hammer_crc_get_fifo_head(uint32_t vol_version, hammer_fifo_head_t head, int bytes)
181 return(hammer_datacrc(vol_version, head, HAMMER_FIFO_HEAD_CRCOFF) ^
182 hammer_datacrc(vol_version, head + 1, bytes - sizeof(*head)));
185 static __inline void
186 hammer_crc_set_fifo_head(uint32_t vol_version, hammer_fifo_head_t head, int bytes)
188 head->hdr_crc = hammer_crc_get_fifo_head(vol_version, head, bytes);
191 static __inline int
192 hammer_crc_test_fifo_head(uint32_t vol_version, hammer_fifo_head_t head, int bytes)
194 if (head->hdr_crc == hammer_crc_get_fifo_head(vol_version, head, bytes))
195 return 1;
196 if (vol_version >= HAMMER_VOL_VERSION_SEVEN) {
197 if (head->hdr_crc == hammer_crc_get_fifo_head(HAMMER_VOL_VERSION_SIX,
198 head, bytes)) {
199 return 1;
202 return 0;
205 static __inline hammer_crc_t
206 hammer_crc_get_btree(uint32_t vol_version, hammer_node_ondisk_t node)
208 return (hammer_datacrc(vol_version, &node->crc + 1, HAMMER_BTREE_CRCSIZE));
211 static __inline void
212 hammer_crc_set_btree(uint32_t vol_version, hammer_node_ondisk_t node)
214 node->crc = hammer_crc_get_btree(vol_version, node);
217 static __inline int
218 hammer_crc_test_btree(uint32_t vol_version, hammer_node_ondisk_t node)
220 if (node->crc == hammer_crc_get_btree(vol_version, node))
221 return 1;
222 if (vol_version >= HAMMER_VOL_VERSION_SEVEN) {
223 if (node->crc == hammer_crc_get_btree(HAMMER_VOL_VERSION_SIX, node))
224 return 1;
226 return 0;
230 * Get the leaf->data_crc field. Deal with any special cases given
231 * a generic B-Tree leaf element and its data.
233 * NOTE: Inode-data: the atime and mtime fields are not CRCd,
234 * allowing them to be updated in-place.
236 static __inline hammer_crc_t
237 hammer_crc_get_leaf(uint32_t vol_version, void *data, hammer_btree_leaf_elm_t leaf)
239 hammer_crc_t crc;
241 if (leaf->data_len == 0)
242 return(0);
244 switch(leaf->base.rec_type) {
245 case HAMMER_RECTYPE_INODE:
246 if (leaf->data_len != sizeof(struct hammer_inode_data))
247 return(0); /* This shouldn't happen */
248 crc = hammer_datacrc(vol_version, data, HAMMER_INODE_CRCSIZE);
249 break;
250 default:
251 crc = hammer_datacrc(vol_version, data, leaf->data_len);
252 break;
254 return(crc);
257 static __inline void
258 hammer_crc_set_leaf(uint32_t vol_version, void *data, hammer_btree_leaf_elm_t leaf)
260 #ifdef _KERNEL
261 #ifdef INVARIANTS
262 if (leaf->data_len && leaf->base.rec_type == HAMMER_RECTYPE_INODE)
263 KKASSERT(leaf->data_len == sizeof(struct hammer_inode_data));
264 #endif
265 #endif
266 leaf->data_crc = hammer_crc_get_leaf(vol_version, data, leaf);
269 static __inline int
270 hammer_crc_test_leaf(uint32_t vol_version, void *data, hammer_btree_leaf_elm_t leaf)
272 if (leaf->data_crc == hammer_crc_get_leaf(vol_version, data, leaf))
273 return 1;
274 if (vol_version >= HAMMER_VOL_VERSION_SEVEN) {
275 if (leaf->data_crc == hammer_crc_get_leaf(HAMMER_VOL_VERSION_SIX, data, leaf))
276 return 1;
278 return 0;
281 static __inline hammer_crc_t
282 hammer_crc_get_mrec_head(hammer_ioc_mrecord_head_t head, int bytes)
284 return(crc32(&head->rec_size, bytes - HAMMER_MREC_CRCOFF));
287 static __inline void
288 hammer_crc_set_mrec_head(hammer_ioc_mrecord_head_t head, int bytes)
290 head->rec_crc = hammer_crc_get_mrec_head(head, bytes);
293 static __inline int
294 hammer_crc_test_mrec_head(hammer_ioc_mrecord_head_t head, int bytes)
296 return(head->rec_crc == hammer_crc_get_mrec_head(head, bytes));
299 #endif /* !VFS_HAMMER_CRC_H_ */