]>
Commit | Line | Data |
---|---|---|
ca2634a4 JS |
1 | /* |
2 | * csum.c --- checksumming of ext3 structures | |
3 | * | |
4 | * Copyright (C) 2006 Cluster File Systems, Inc. | |
470e737a | 5 | * Copyright (C) 2006, 2007 by Andreas Dilger <adilger@clusterfs.com> |
ca2634a4 JS |
6 | * |
7 | * %Begin-Header% | |
543547a5 TT |
8 | * This file may be redistributed under the terms of the GNU Library |
9 | * General Public License, version 2. | |
ca2634a4 JS |
10 | * %End-Header% |
11 | */ | |
12 | ||
d1154eb4 | 13 | #include "config.h" |
0eeec8ac TT |
14 | #if HAVE_SYS_TYPES_H |
15 | #include <sys/types.h> | |
16 | #endif | |
17 | ||
ca2634a4 JS |
18 | #include "ext2_fs.h" |
19 | #include "ext2fs.h" | |
20 | #include "crc16.h" | |
21 | #include <assert.h> | |
22 | ||
23 | #ifndef offsetof | |
24 | #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) | |
25 | #endif | |
26 | ||
27 | #ifdef DEBUG | |
28 | #define STATIC | |
29 | #else | |
30 | #define STATIC static | |
31 | #endif | |
32 | ||
2ed0adbc DW |
33 | void ext2fs_init_csum_seed(ext2_filsys fs) |
34 | { | |
35 | if (ext2fs_has_feature_csum_seed(fs->super)) | |
36 | fs->csum_seed = fs->super->s_checksum_seed; | |
216d6b76 TE |
37 | else if (ext2fs_has_feature_metadata_csum(fs->super) || |
38 | ext2fs_has_feature_ea_inode(fs->super)) | |
2ed0adbc DW |
39 | fs->csum_seed = ext2fs_crc32c_le(~0, fs->super->s_uuid, |
40 | sizeof(fs->super->s_uuid)); | |
41 | } | |
42 | ||
a9620d8b DW |
43 | static __u32 ext2fs_mmp_csum(ext2_filsys fs, struct mmp_struct *mmp) |
44 | { | |
45 | int offset = offsetof(struct mmp_struct, mmp_checksum); | |
46 | ||
47 | return ext2fs_crc32c_le(fs->csum_seed, (unsigned char *)mmp, offset); | |
48 | } | |
49 | ||
50 | int ext2fs_mmp_csum_verify(ext2_filsys fs, struct mmp_struct *mmp) | |
51 | { | |
52 | __u32 calculated; | |
53 | ||
77b3e987 | 54 | if (!ext2fs_has_feature_metadata_csum(fs->super)) |
a9620d8b DW |
55 | return 1; |
56 | ||
57 | calculated = ext2fs_mmp_csum(fs, mmp); | |
58 | ||
59 | return ext2fs_le32_to_cpu(mmp->mmp_checksum) == calculated; | |
60 | } | |
61 | ||
62 | errcode_t ext2fs_mmp_csum_set(ext2_filsys fs, struct mmp_struct *mmp) | |
63 | { | |
64 | __u32 crc; | |
65 | ||
77b3e987 | 66 | if (!ext2fs_has_feature_metadata_csum(fs->super)) |
a9620d8b DW |
67 | return 0; |
68 | ||
69 | crc = ext2fs_mmp_csum(fs, mmp); | |
70 | mmp->mmp_checksum = ext2fs_cpu_to_le32(crc); | |
71 | ||
72 | return 0; | |
73 | } | |
74 | ||
1eedf3e5 DW |
75 | int ext2fs_verify_csum_type(ext2_filsys fs, struct ext2_super_block *sb) |
76 | { | |
77b3e987 | 77 | if (!ext2fs_has_feature_metadata_csum(fs->super)) |
1eedf3e5 DW |
78 | return 1; |
79 | ||
80 | return sb->s_checksum_type == EXT2_CRC32C_CHKSUM; | |
81 | } | |
82 | ||
894eaf85 TT |
83 | static __u32 ext2fs_superblock_csum(ext2_filsys fs EXT2FS_ATTR((unused)), |
84 | struct ext2_super_block *sb) | |
d4ca3e40 DW |
85 | { |
86 | int offset = offsetof(struct ext2_super_block, s_checksum); | |
87 | ||
88 | return ext2fs_crc32c_le(~0, (unsigned char *)sb, offset); | |
89 | } | |
90 | ||
2432a41a | 91 | /* NOTE: The input to this function MUST be in LE order */ |
d4ca3e40 DW |
92 | int ext2fs_superblock_csum_verify(ext2_filsys fs, struct ext2_super_block *sb) |
93 | { | |
2432a41a | 94 | __u32 flag, calculated; |
d4ca3e40 | 95 | |
2432a41a DW |
96 | if (fs->flags & EXT2_FLAG_SWAP_BYTES) |
97 | flag = EXT4_FEATURE_RO_COMPAT_METADATA_CSUM; | |
98 | else | |
99 | flag = ext2fs_cpu_to_le32(EXT4_FEATURE_RO_COMPAT_METADATA_CSUM); | |
100 | ||
101 | if (!EXT2_HAS_RO_COMPAT_FEATURE(fs->super, flag)) | |
d4ca3e40 DW |
102 | return 1; |
103 | ||
104 | calculated = ext2fs_superblock_csum(fs, sb); | |
105 | ||
106 | return ext2fs_le32_to_cpu(sb->s_checksum) == calculated; | |
107 | } | |
108 | ||
2432a41a | 109 | /* NOTE: The input to this function MUST be in LE order */ |
d4ca3e40 DW |
110 | errcode_t ext2fs_superblock_csum_set(ext2_filsys fs, |
111 | struct ext2_super_block *sb) | |
112 | { | |
2432a41a | 113 | __u32 flag, crc; |
d4ca3e40 | 114 | |
2432a41a DW |
115 | if (fs->flags & EXT2_FLAG_SWAP_BYTES) |
116 | flag = EXT4_FEATURE_RO_COMPAT_METADATA_CSUM; | |
117 | else | |
118 | flag = ext2fs_cpu_to_le32(EXT4_FEATURE_RO_COMPAT_METADATA_CSUM); | |
119 | ||
120 | if (!EXT2_HAS_RO_COMPAT_FEATURE(fs->super, flag)) | |
d4ca3e40 DW |
121 | return 0; |
122 | ||
123 | crc = ext2fs_superblock_csum(fs, sb); | |
124 | sb->s_checksum = ext2fs_cpu_to_le32(crc); | |
125 | ||
126 | return 0; | |
127 | } | |
128 | ||
894eaf85 TT |
129 | static errcode_t ext2fs_ext_attr_block_csum(ext2_filsys fs, |
130 | ext2_ino_t inum EXT2FS_ATTR((unused)), | |
39f5659a DW |
131 | blk64_t block, |
132 | struct ext2_ext_attr_header *hdr, | |
133 | __u32 *crc) | |
134 | { | |
39f5659a | 135 | char *buf = (char *)hdr; |
1b8c4c1b | 136 | __u32 old_crc = hdr->h_checksum; |
39f5659a DW |
137 | |
138 | hdr->h_checksum = 0; | |
139 | block = ext2fs_cpu_to_le64(block); | |
140 | *crc = ext2fs_crc32c_le(fs->csum_seed, (unsigned char *)&block, | |
141 | sizeof(block)); | |
142 | *crc = ext2fs_crc32c_le(*crc, (unsigned char *)buf, fs->blocksize); | |
143 | hdr->h_checksum = old_crc; | |
144 | ||
145 | return 0; | |
146 | } | |
147 | ||
148 | int ext2fs_ext_attr_block_csum_verify(ext2_filsys fs, ext2_ino_t inum, | |
149 | blk64_t block, | |
150 | struct ext2_ext_attr_header *hdr) | |
151 | { | |
152 | __u32 calculated; | |
153 | errcode_t retval; | |
154 | ||
77b3e987 | 155 | if (!ext2fs_has_feature_metadata_csum(fs->super)) |
39f5659a DW |
156 | return 1; |
157 | ||
158 | retval = ext2fs_ext_attr_block_csum(fs, inum, block, hdr, &calculated); | |
159 | if (retval) | |
160 | return 0; | |
161 | ||
162 | return ext2fs_le32_to_cpu(hdr->h_checksum) == calculated; | |
163 | } | |
164 | ||
165 | errcode_t ext2fs_ext_attr_block_csum_set(ext2_filsys fs, ext2_ino_t inum, | |
166 | blk64_t block, | |
167 | struct ext2_ext_attr_header *hdr) | |
168 | { | |
169 | errcode_t retval; | |
170 | __u32 crc; | |
171 | ||
77b3e987 | 172 | if (!ext2fs_has_feature_metadata_csum(fs->super)) |
39f5659a DW |
173 | return 0; |
174 | ||
175 | retval = ext2fs_ext_attr_block_csum(fs, inum, block, hdr, &crc); | |
176 | if (retval) | |
177 | return retval; | |
178 | hdr->h_checksum = ext2fs_cpu_to_le32(crc); | |
179 | return 0; | |
180 | } | |
181 | ||
dc96de09 DW |
182 | static __u16 do_nothing16(__u16 x) |
183 | { | |
184 | return x; | |
185 | } | |
186 | ||
187 | static __u16 disk_to_host16(__u16 x) | |
188 | { | |
189 | return ext2fs_le16_to_cpu(x); | |
190 | } | |
191 | ||
192 | static errcode_t __get_dx_countlimit(ext2_filsys fs, | |
193 | struct ext2_dir_entry *dirent, | |
194 | struct ext2_dx_countlimit **cc, | |
195 | int *offset, | |
196 | int need_swab) | |
197 | { | |
198 | struct ext2_dir_entry *dp; | |
199 | struct ext2_dx_root_info *root; | |
200 | struct ext2_dx_countlimit *c; | |
201 | int count_offset, max_sane_entries; | |
202 | unsigned int rec_len; | |
203 | __u16 (*translate)(__u16) = (need_swab ? disk_to_host16 : do_nothing16); | |
204 | ||
205 | rec_len = translate(dirent->rec_len); | |
206 | ||
207 | if (rec_len == fs->blocksize && translate(dirent->name_len) == 0) | |
208 | count_offset = 8; | |
209 | else if (rec_len == 12) { | |
894eaf85 | 210 | dp = (struct ext2_dir_entry *)(((char *)dirent) + rec_len); |
dc96de09 DW |
211 | rec_len = translate(dp->rec_len); |
212 | if (rec_len != fs->blocksize - 12) | |
213 | return EXT2_ET_DB_NOT_FOUND; | |
894eaf85 | 214 | root = (struct ext2_dx_root_info *)(((char *)dp + 12)); |
dc96de09 DW |
215 | if (root->reserved_zero || |
216 | root->info_length != sizeof(struct ext2_dx_root_info)) | |
217 | return EXT2_ET_DB_NOT_FOUND; | |
218 | count_offset = 32; | |
219 | } else | |
220 | return EXT2_ET_DB_NOT_FOUND; | |
221 | ||
894eaf85 | 222 | c = (struct ext2_dx_countlimit *)(((char *)dirent) + count_offset); |
dc96de09 DW |
223 | max_sane_entries = (fs->blocksize - count_offset) / |
224 | sizeof(struct ext2_dx_entry); | |
225 | if (ext2fs_le16_to_cpu(c->limit) > max_sane_entries || | |
226 | ext2fs_le16_to_cpu(c->count) > max_sane_entries) | |
227 | return EXT2_ET_DIR_NO_SPACE_FOR_CSUM; | |
228 | ||
229 | if (offset) | |
230 | *offset = count_offset; | |
231 | if (cc) | |
232 | *cc = c; | |
233 | ||
234 | return 0; | |
235 | } | |
236 | ||
237 | errcode_t ext2fs_get_dx_countlimit(ext2_filsys fs, | |
238 | struct ext2_dir_entry *dirent, | |
239 | struct ext2_dx_countlimit **cc, | |
240 | int *offset) | |
241 | { | |
242 | return __get_dx_countlimit(fs, dirent, cc, offset, 0); | |
243 | } | |
244 | ||
81683c6a DW |
245 | void ext2fs_initialize_dirent_tail(ext2_filsys fs, |
246 | struct ext2_dir_entry_tail *t) | |
247 | { | |
248 | memset(t, 0, sizeof(struct ext2_dir_entry_tail)); | |
249 | ext2fs_set_rec_len(fs, sizeof(struct ext2_dir_entry_tail), | |
250 | (struct ext2_dir_entry *)t); | |
251 | t->det_reserved_name_len = EXT2_DIR_NAME_LEN_CSUM; | |
252 | } | |
253 | ||
254 | static errcode_t __get_dirent_tail(ext2_filsys fs, | |
255 | struct ext2_dir_entry *dirent, | |
256 | struct ext2_dir_entry_tail **tt, | |
257 | int need_swab) | |
258 | { | |
259 | struct ext2_dir_entry *d; | |
260 | void *top; | |
261 | struct ext2_dir_entry_tail *t; | |
262 | unsigned int rec_len; | |
263 | errcode_t retval = 0; | |
264 | __u16 (*translate)(__u16) = (need_swab ? disk_to_host16 : do_nothing16); | |
265 | ||
03130cc2 TT |
266 | if (fs->blocksize < 1024) |
267 | return EXT2_FILSYS_CORRUPTED; /* Should never happen */ | |
268 | ||
81683c6a DW |
269 | d = dirent; |
270 | top = EXT2_DIRENT_TAIL(dirent, fs->blocksize); | |
271 | ||
eb88b751 | 272 | while ((void *) d < top) { |
7b63656d | 273 | rec_len = translate(d->rec_len); |
eb88b751 TT |
274 | if ((rec_len < 8) || (rec_len & 0x03)) |
275 | return EXT2_ET_DIR_CORRUPTED; | |
894eaf85 | 276 | d = (struct ext2_dir_entry *)(((char *)d) + rec_len); |
81683c6a DW |
277 | } |
278 | ||
1bc7feae | 279 | if ((char *)d > ((char *)dirent + fs->blocksize)) |
eb88b751 | 280 | return EXT2_ET_DIR_CORRUPTED; |
81683c6a DW |
281 | if (d != top) |
282 | return EXT2_ET_DIR_NO_SPACE_FOR_CSUM; | |
283 | ||
284 | t = (struct ext2_dir_entry_tail *)d; | |
285 | if (t->det_reserved_zero1 || | |
286 | translate(t->det_rec_len) != sizeof(struct ext2_dir_entry_tail) || | |
287 | translate(t->det_reserved_name_len) != EXT2_DIR_NAME_LEN_CSUM) | |
288 | return EXT2_ET_DIR_NO_SPACE_FOR_CSUM; | |
289 | ||
290 | if (tt) | |
291 | *tt = t; | |
292 | return retval; | |
293 | } | |
294 | ||
295 | int ext2fs_dirent_has_tail(ext2_filsys fs, struct ext2_dir_entry *dirent) | |
296 | { | |
eb88b751 TT |
297 | return __get_dirent_tail(fs, dirent, NULL, 0) != |
298 | EXT2_ET_DIR_NO_SPACE_FOR_CSUM; | |
81683c6a DW |
299 | } |
300 | ||
301 | static errcode_t ext2fs_dirent_csum(ext2_filsys fs, ext2_ino_t inum, | |
302 | struct ext2_dir_entry *dirent, __u32 *crc, | |
303 | int size) | |
304 | { | |
305 | errcode_t retval; | |
306 | char *buf = (char *)dirent; | |
307 | __u32 gen; | |
308 | struct ext2_inode inode; | |
309 | ||
310 | retval = ext2fs_read_inode(fs, inum, &inode); | |
311 | if (retval) | |
312 | return retval; | |
313 | ||
314 | inum = ext2fs_cpu_to_le32(inum); | |
315 | gen = ext2fs_cpu_to_le32(inode.i_generation); | |
316 | *crc = ext2fs_crc32c_le(fs->csum_seed, (unsigned char *)&inum, | |
317 | sizeof(inum)); | |
318 | *crc = ext2fs_crc32c_le(*crc, (unsigned char *)&gen, sizeof(gen)); | |
319 | *crc = ext2fs_crc32c_le(*crc, (unsigned char *)buf, size); | |
320 | ||
321 | return 0; | |
322 | } | |
323 | ||
324 | int ext2fs_dirent_csum_verify(ext2_filsys fs, ext2_ino_t inum, | |
325 | struct ext2_dir_entry *dirent) | |
326 | { | |
327 | errcode_t retval; | |
328 | __u32 calculated; | |
329 | struct ext2_dir_entry_tail *t; | |
330 | ||
331 | retval = __get_dirent_tail(fs, dirent, &t, 1); | |
332 | if (retval) | |
333 | return 1; | |
334 | ||
335 | /* | |
336 | * The checksum field is overlaid with the dirent->name field | |
337 | * so the swapfs.c functions won't change the endianness. | |
338 | */ | |
339 | retval = ext2fs_dirent_csum(fs, inum, dirent, &calculated, | |
894eaf85 | 340 | (char *)t - (char *)dirent); |
81683c6a DW |
341 | if (retval) |
342 | return 0; | |
343 | return ext2fs_le32_to_cpu(t->det_checksum) == calculated; | |
344 | } | |
345 | ||
346 | static errcode_t ext2fs_dirent_csum_set(ext2_filsys fs, ext2_ino_t inum, | |
347 | struct ext2_dir_entry *dirent) | |
348 | { | |
349 | errcode_t retval; | |
350 | __u32 crc; | |
351 | struct ext2_dir_entry_tail *t; | |
352 | ||
353 | retval = __get_dirent_tail(fs, dirent, &t, 1); | |
354 | if (retval) | |
355 | return retval; | |
356 | ||
357 | /* swapfs.c functions don't change the checksum endianness */ | |
358 | retval = ext2fs_dirent_csum(fs, inum, dirent, &crc, | |
894eaf85 | 359 | (char *)t - (char *)dirent); |
81683c6a DW |
360 | if (retval) |
361 | return retval; | |
362 | t->det_checksum = ext2fs_cpu_to_le32(crc); | |
363 | return 0; | |
364 | } | |
365 | ||
c73eb706 TT |
366 | errcode_t ext2fs_dx_csum(ext2_filsys fs, ext2_ino_t inum, |
367 | struct ext2_dir_entry *dirent, | |
368 | __u32 *crc, struct ext2_dx_tail **ret_t) | |
dc96de09 DW |
369 | { |
370 | errcode_t retval; | |
371 | char *buf = (char *)dirent; | |
372 | int size; | |
cd2dc70b | 373 | __u32 gen, dummy_csum = 0; |
dc96de09 | 374 | struct ext2_inode inode; |
cd2dc70b TT |
375 | struct ext2_dx_tail *t; |
376 | struct ext2_dx_countlimit *c; | |
377 | int count_offset, limit, count; | |
378 | ||
379 | retval = __get_dx_countlimit(fs, dirent, &c, &count_offset, 1); | |
380 | if (retval) | |
381 | return retval; | |
382 | limit = ext2fs_le16_to_cpu(c->limit); | |
383 | count = ext2fs_le16_to_cpu(c->count); | |
384 | if (count_offset + (limit * sizeof(struct ext2_dx_entry)) > | |
385 | fs->blocksize - sizeof(struct ext2_dx_tail)) | |
386 | return EXT2_ET_DIR_NO_SPACE_FOR_CSUM; | |
387 | /* htree structs are accessed in LE order */ | |
388 | t = (struct ext2_dx_tail *)(((struct ext2_dx_entry *)c) + limit); | |
dc96de09 DW |
389 | |
390 | size = count_offset + (count * sizeof(struct ext2_dx_entry)); | |
dc96de09 DW |
391 | |
392 | retval = ext2fs_read_inode(fs, inum, &inode); | |
393 | if (retval) | |
394 | return retval; | |
395 | ||
396 | inum = ext2fs_cpu_to_le32(inum); | |
397 | gen = ext2fs_cpu_to_le32(inode.i_generation); | |
398 | *crc = ext2fs_crc32c_le(fs->csum_seed, (unsigned char *)&inum, | |
399 | sizeof(inum)); | |
400 | *crc = ext2fs_crc32c_le(*crc, (unsigned char *)&gen, sizeof(gen)); | |
401 | *crc = ext2fs_crc32c_le(*crc, (unsigned char *)buf, size); | |
cd2dc70b TT |
402 | *crc = ext2fs_crc32c_le(*crc, (unsigned char *)t, 4); |
403 | *crc = ext2fs_crc32c_le(*crc, (unsigned char *)&dummy_csum, 4); | |
dc96de09 | 404 | |
cd2dc70b TT |
405 | if (ret_t) |
406 | *ret_t = t; | |
dc96de09 DW |
407 | return 0; |
408 | } | |
409 | ||
410 | static int ext2fs_dx_csum_verify(ext2_filsys fs, ext2_ino_t inum, | |
411 | struct ext2_dir_entry *dirent) | |
412 | { | |
413 | __u32 calculated; | |
414 | errcode_t retval; | |
dc96de09 | 415 | struct ext2_dx_tail *t; |
dc96de09 | 416 | |
cd2dc70b | 417 | retval = ext2fs_dx_csum(fs, inum, dirent, &calculated, &t); |
dc96de09 DW |
418 | if (retval) |
419 | return 0; | |
420 | ||
421 | return ext2fs_le32_to_cpu(t->dt_checksum) == calculated; | |
422 | } | |
423 | ||
424 | static errcode_t ext2fs_dx_csum_set(ext2_filsys fs, ext2_ino_t inum, | |
425 | struct ext2_dir_entry *dirent) | |
426 | { | |
427 | __u32 crc; | |
428 | errcode_t retval = 0; | |
dc96de09 | 429 | struct ext2_dx_tail *t; |
dc96de09 | 430 | |
cd2dc70b | 431 | retval = ext2fs_dx_csum(fs, inum, dirent, &crc, &t); |
dc96de09 DW |
432 | if (retval) |
433 | return retval; | |
434 | t->dt_checksum = ext2fs_cpu_to_le32(crc); | |
435 | return retval; | |
436 | } | |
437 | ||
81683c6a DW |
438 | int ext2fs_dir_block_csum_verify(ext2_filsys fs, ext2_ino_t inum, |
439 | struct ext2_dir_entry *dirent) | |
440 | { | |
77b3e987 | 441 | if (!ext2fs_has_feature_metadata_csum(fs->super)) |
81683c6a DW |
442 | return 1; |
443 | ||
444 | if (__get_dirent_tail(fs, dirent, NULL, 1) == 0) | |
445 | return ext2fs_dirent_csum_verify(fs, inum, dirent); | |
446 | if (__get_dx_countlimit(fs, dirent, NULL, NULL, 1) == 0) | |
447 | return ext2fs_dx_csum_verify(fs, inum, dirent); | |
448 | ||
449 | return 0; | |
450 | } | |
451 | ||
452 | errcode_t ext2fs_dir_block_csum_set(ext2_filsys fs, ext2_ino_t inum, | |
453 | struct ext2_dir_entry *dirent) | |
454 | { | |
77b3e987 | 455 | if (!ext2fs_has_feature_metadata_csum(fs->super)) |
81683c6a DW |
456 | return 0; |
457 | ||
458 | if (__get_dirent_tail(fs, dirent, NULL, 1) == 0) | |
459 | return ext2fs_dirent_csum_set(fs, inum, dirent); | |
460 | if (__get_dx_countlimit(fs, dirent, NULL, NULL, 1) == 0) | |
461 | return ext2fs_dx_csum_set(fs, inum, dirent); | |
462 | ||
463 | if (fs->flags & EXT2_FLAG_IGNORE_CSUM_ERRORS) | |
464 | return 0; | |
465 | return EXT2_ET_DIR_NO_SPACE_FOR_CSUM; | |
466 | } | |
467 | ||
41275efe DW |
468 | #define EXT3_EXTENT_TAIL_OFFSET(hdr) (sizeof(struct ext3_extent_header) + \ |
469 | (sizeof(struct ext3_extent) * ext2fs_le16_to_cpu((hdr)->eh_max))) | |
470 | ||
471 | static struct ext3_extent_tail *get_extent_tail(struct ext3_extent_header *h) | |
472 | { | |
894eaf85 | 473 | return (struct ext3_extent_tail *)(((char *)h) + |
41275efe DW |
474 | EXT3_EXTENT_TAIL_OFFSET(h)); |
475 | } | |
476 | ||
477 | static errcode_t ext2fs_extent_block_csum(ext2_filsys fs, ext2_ino_t inum, | |
478 | struct ext3_extent_header *eh, | |
479 | __u32 *crc) | |
480 | { | |
481 | int size; | |
482 | __u32 gen; | |
483 | errcode_t retval; | |
484 | struct ext2_inode inode; | |
485 | ||
486 | size = EXT3_EXTENT_TAIL_OFFSET(eh) + offsetof(struct ext3_extent_tail, | |
487 | et_checksum); | |
488 | ||
489 | retval = ext2fs_read_inode(fs, inum, &inode); | |
490 | if (retval) | |
491 | return retval; | |
492 | inum = ext2fs_cpu_to_le32(inum); | |
493 | gen = ext2fs_cpu_to_le32(inode.i_generation); | |
494 | *crc = ext2fs_crc32c_le(fs->csum_seed, (unsigned char *)&inum, | |
495 | sizeof(inum)); | |
496 | *crc = ext2fs_crc32c_le(*crc, (unsigned char *)&gen, sizeof(gen)); | |
497 | *crc = ext2fs_crc32c_le(*crc, (unsigned char *)eh, size); | |
498 | ||
499 | return 0; | |
500 | } | |
501 | ||
502 | int ext2fs_extent_block_csum_verify(ext2_filsys fs, ext2_ino_t inum, | |
503 | struct ext3_extent_header *eh) | |
504 | { | |
505 | errcode_t retval; | |
506 | __u32 provided, calculated; | |
507 | struct ext3_extent_tail *t = get_extent_tail(eh); | |
508 | ||
509 | /* | |
510 | * The extent tree structures are accessed in LE order, so we must | |
511 | * swap the checksum bytes here. | |
512 | */ | |
77b3e987 | 513 | if (!ext2fs_has_feature_metadata_csum(fs->super)) |
41275efe DW |
514 | return 1; |
515 | ||
516 | provided = ext2fs_le32_to_cpu(t->et_checksum); | |
517 | retval = ext2fs_extent_block_csum(fs, inum, eh, &calculated); | |
518 | if (retval) | |
519 | return 0; | |
520 | ||
521 | return provided == calculated; | |
522 | } | |
523 | ||
524 | errcode_t ext2fs_extent_block_csum_set(ext2_filsys fs, ext2_ino_t inum, | |
525 | struct ext3_extent_header *eh) | |
526 | { | |
527 | errcode_t retval; | |
528 | __u32 crc; | |
529 | struct ext3_extent_tail *t = get_extent_tail(eh); | |
530 | ||
77b3e987 | 531 | if (!ext2fs_has_feature_metadata_csum(fs->super)) |
41275efe DW |
532 | return 0; |
533 | ||
534 | /* | |
535 | * The extent tree structures are accessed in LE order, so we must | |
536 | * swap the checksum bytes here. | |
537 | */ | |
538 | retval = ext2fs_extent_block_csum(fs, inum, eh, &crc); | |
539 | if (retval) | |
540 | return retval; | |
541 | t->et_checksum = ext2fs_cpu_to_le32(crc); | |
542 | return retval; | |
543 | } | |
544 | ||
a80ea340 DW |
545 | int ext2fs_inode_bitmap_csum_verify(ext2_filsys fs, dgrp_t group, |
546 | char *bitmap, int size) | |
547 | { | |
548 | struct ext4_group_desc *gdp = (struct ext4_group_desc *) | |
549 | ext2fs_group_desc(fs, fs->group_desc, group); | |
550 | __u32 provided, calculated; | |
551 | ||
77b3e987 | 552 | if (!ext2fs_has_feature_metadata_csum(fs->super)) |
a80ea340 DW |
553 | return 1; |
554 | provided = gdp->bg_inode_bitmap_csum_lo; | |
555 | calculated = ext2fs_crc32c_le(fs->csum_seed, (unsigned char *)bitmap, | |
556 | size); | |
cedfcf2a | 557 | if (EXT2_DESC_SIZE(fs->super) >= EXT4_BG_INODE_BITMAP_CSUM_HI_END) |
a80ea340 DW |
558 | provided |= (__u32)gdp->bg_inode_bitmap_csum_hi << 16; |
559 | else | |
560 | calculated &= 0xFFFF; | |
561 | ||
562 | return provided == calculated; | |
563 | } | |
564 | ||
565 | errcode_t ext2fs_inode_bitmap_csum_set(ext2_filsys fs, dgrp_t group, | |
566 | char *bitmap, int size) | |
567 | { | |
568 | __u32 crc; | |
569 | struct ext4_group_desc *gdp = (struct ext4_group_desc *) | |
570 | ext2fs_group_desc(fs, fs->group_desc, group); | |
571 | ||
77b3e987 | 572 | if (!ext2fs_has_feature_metadata_csum(fs->super)) |
a80ea340 DW |
573 | return 0; |
574 | ||
575 | crc = ext2fs_crc32c_le(fs->csum_seed, (unsigned char *)bitmap, size); | |
576 | gdp->bg_inode_bitmap_csum_lo = crc & 0xFFFF; | |
cedfcf2a | 577 | if (EXT2_DESC_SIZE(fs->super) >= EXT4_BG_INODE_BITMAP_CSUM_HI_END) |
a80ea340 DW |
578 | gdp->bg_inode_bitmap_csum_hi = crc >> 16; |
579 | ||
580 | return 0; | |
581 | } | |
582 | ||
e7dc95c6 DW |
583 | int ext2fs_block_bitmap_csum_verify(ext2_filsys fs, dgrp_t group, |
584 | char *bitmap, int size) | |
585 | { | |
586 | struct ext4_group_desc *gdp = (struct ext4_group_desc *) | |
587 | ext2fs_group_desc(fs, fs->group_desc, group); | |
588 | __u32 provided, calculated; | |
589 | ||
77b3e987 | 590 | if (!ext2fs_has_feature_metadata_csum(fs->super)) |
e7dc95c6 DW |
591 | return 1; |
592 | provided = gdp->bg_block_bitmap_csum_lo; | |
593 | calculated = ext2fs_crc32c_le(fs->csum_seed, (unsigned char *)bitmap, | |
594 | size); | |
cedfcf2a | 595 | if (EXT2_DESC_SIZE(fs->super) >= EXT4_BG_BLOCK_BITMAP_CSUM_HI_LOCATION) |
e7dc95c6 DW |
596 | provided |= (__u32)gdp->bg_block_bitmap_csum_hi << 16; |
597 | else | |
598 | calculated &= 0xFFFF; | |
599 | ||
600 | return provided == calculated; | |
601 | } | |
602 | ||
603 | errcode_t ext2fs_block_bitmap_csum_set(ext2_filsys fs, dgrp_t group, | |
604 | char *bitmap, int size) | |
605 | { | |
606 | __u32 crc; | |
607 | struct ext4_group_desc *gdp = (struct ext4_group_desc *) | |
608 | ext2fs_group_desc(fs, fs->group_desc, group); | |
609 | ||
77b3e987 | 610 | if (!ext2fs_has_feature_metadata_csum(fs->super)) |
e7dc95c6 DW |
611 | return 0; |
612 | ||
613 | crc = ext2fs_crc32c_le(fs->csum_seed, (unsigned char *)bitmap, size); | |
614 | gdp->bg_block_bitmap_csum_lo = crc & 0xFFFF; | |
cedfcf2a | 615 | if (EXT2_DESC_SIZE(fs->super) >= EXT4_BG_BLOCK_BITMAP_CSUM_HI_LOCATION) |
e7dc95c6 DW |
616 | gdp->bg_block_bitmap_csum_hi = crc >> 16; |
617 | ||
618 | return 0; | |
619 | } | |
620 | ||
37d82b6a DW |
621 | static errcode_t ext2fs_inode_csum(ext2_filsys fs, ext2_ino_t inum, |
622 | struct ext2_inode_large *inode, | |
623 | __u32 *crc, int has_hi) | |
624 | { | |
625 | __u32 gen; | |
626 | struct ext2_inode_large *desc = inode; | |
d1ccc6e5 | 627 | size_t size = EXT2_INODE_SIZE(fs->super); |
37d82b6a DW |
628 | __u16 old_lo; |
629 | __u16 old_hi = 0; | |
630 | ||
631 | old_lo = inode->i_checksum_lo; | |
632 | inode->i_checksum_lo = 0; | |
633 | if (has_hi) { | |
634 | old_hi = inode->i_checksum_hi; | |
635 | inode->i_checksum_hi = 0; | |
636 | } | |
637 | ||
638 | inum = ext2fs_cpu_to_le32(inum); | |
639 | gen = inode->i_generation; | |
640 | *crc = ext2fs_crc32c_le(fs->csum_seed, (unsigned char *)&inum, | |
641 | sizeof(inum)); | |
642 | *crc = ext2fs_crc32c_le(*crc, (unsigned char *)&gen, sizeof(gen)); | |
643 | *crc = ext2fs_crc32c_le(*crc, (unsigned char *)desc, size); | |
644 | ||
645 | inode->i_checksum_lo = old_lo; | |
646 | if (has_hi) | |
647 | inode->i_checksum_hi = old_hi; | |
648 | return 0; | |
649 | } | |
650 | ||
651 | int ext2fs_inode_csum_verify(ext2_filsys fs, ext2_ino_t inum, | |
652 | struct ext2_inode_large *inode) | |
653 | { | |
654 | errcode_t retval; | |
655 | __u32 provided, calculated; | |
894eaf85 | 656 | unsigned int i, has_hi; |
97fac350 | 657 | char *cp; |
37d82b6a | 658 | |
c0495d96 | 659 | if (!ext2fs_has_feature_metadata_csum(fs->super)) |
37d82b6a DW |
660 | return 1; |
661 | ||
662 | has_hi = (EXT2_INODE_SIZE(fs->super) > EXT2_GOOD_OLD_INODE_SIZE && | |
663 | inode->i_extra_isize >= EXT4_INODE_CSUM_HI_EXTRA_END); | |
664 | ||
665 | provided = ext2fs_le16_to_cpu(inode->i_checksum_lo); | |
666 | retval = ext2fs_inode_csum(fs, inum, inode, &calculated, has_hi); | |
667 | if (retval) | |
668 | return 0; | |
669 | if (has_hi) { | |
670 | __u32 hi = ext2fs_le16_to_cpu(inode->i_checksum_hi); | |
671 | provided |= hi << 16; | |
672 | } else | |
673 | calculated &= 0xFFFF; | |
674 | ||
97fac350 TT |
675 | if (provided == calculated) |
676 | return 1; | |
677 | ||
678 | /* | |
679 | * If the checksum didn't match, it's possible it was due to | |
680 | * the inode being all zero's. It's unlikely this is the | |
681 | * case, but it can happen. So check for it here. (We only | |
682 | * check the base inode since that's good enough, and it's not | |
683 | * worth the bother to figure out how much of the extended | |
684 | * inode, if any, is present.) | |
685 | */ | |
686 | for (cp = (char *) inode, i = 0; | |
687 | i < sizeof(struct ext2_inode); | |
688 | cp++, i++) | |
689 | if (*cp) | |
690 | return 0; | |
691 | return 1; /* Inode must have been all zero's */ | |
37d82b6a DW |
692 | } |
693 | ||
694 | errcode_t ext2fs_inode_csum_set(ext2_filsys fs, ext2_ino_t inum, | |
695 | struct ext2_inode_large *inode) | |
696 | { | |
697 | errcode_t retval; | |
698 | __u32 crc; | |
699 | int has_hi; | |
700 | ||
c0495d96 | 701 | if (!ext2fs_has_feature_metadata_csum(fs->super)) |
37d82b6a DW |
702 | return 0; |
703 | ||
704 | has_hi = (EXT2_INODE_SIZE(fs->super) > EXT2_GOOD_OLD_INODE_SIZE && | |
705 | inode->i_extra_isize >= EXT4_INODE_CSUM_HI_EXTRA_END); | |
706 | ||
707 | retval = ext2fs_inode_csum(fs, inum, inode, &crc, has_hi); | |
708 | if (retval) | |
709 | return retval; | |
710 | inode->i_checksum_lo = ext2fs_cpu_to_le16(crc & 0xFFFF); | |
711 | if (has_hi) | |
712 | inode->i_checksum_hi = ext2fs_cpu_to_le16(crc >> 16); | |
713 | return 0; | |
714 | } | |
715 | ||
87141781 | 716 | __u16 ext2fs_group_desc_csum(ext2_filsys fs, dgrp_t group) |
ca2634a4 | 717 | { |
2bc30417 AD |
718 | struct ext2_group_desc *desc = ext2fs_group_desc(fs, fs->group_desc, |
719 | group); | |
71715a5a | 720 | size_t offset, size = EXT2_DESC_SIZE(fs->super); |
ca2634a4 | 721 | __u16 crc = 0; |
ca2634a4 | 722 | #ifdef WORDS_BIGENDIAN |
5b58dc23 | 723 | struct ext4_group_desc swabdesc; |
2ffccc82 TT |
724 | size_t save_size = size; |
725 | const size_t ext4_bg_size = sizeof(struct ext4_group_desc); | |
726 | struct ext2_group_desc *save_desc = desc; | |
ca2634a4 | 727 | |
5b58dc23 | 728 | /* Have to swab back to little-endian to do the checksum */ |
2ffccc82 TT |
729 | if (size > ext4_bg_size) |
730 | size = ext4_bg_size; | |
5b58dc23 | 731 | memcpy(&swabdesc, desc, size); |
2ffccc82 | 732 | ext2fs_swap_group_desc2(fs, (struct ext2_group_desc *) &swabdesc); |
5b58dc23 | 733 | desc = (struct ext2_group_desc *) &swabdesc; |
5b58dc23 | 734 | group = ext2fs_swab32(group); |
ca2634a4 | 735 | #endif |
5b58dc23 | 736 | |
77b3e987 | 737 | if (ext2fs_has_feature_metadata_csum(fs->super)) { |
5b58dc23 DW |
738 | /* new metadata csum code */ |
739 | __u16 old_crc; | |
740 | __u32 crc32; | |
741 | ||
742 | old_crc = desc->bg_checksum; | |
743 | desc->bg_checksum = 0; | |
744 | crc32 = ext2fs_crc32c_le(fs->csum_seed, (unsigned char *)&group, | |
745 | sizeof(group)); | |
746 | crc32 = ext2fs_crc32c_le(crc32, (unsigned char *)desc, | |
747 | size); | |
748 | desc->bg_checksum = old_crc; | |
b3c2a638 | 749 | #ifdef WORDS_BIGENDIAN |
b3c2a638 | 750 | if (save_size > ext4_bg_size) |
2ffccc82 | 751 | crc32 = ext2fs_crc32c_le(crc32, |
2432a41a DW |
752 | (unsigned char *)save_desc + ext4_bg_size, |
753 | save_size - ext4_bg_size); | |
b3c2a638 | 754 | #endif |
5b58dc23 DW |
755 | crc = crc32 & 0xFFFF; |
756 | goto out; | |
ca2634a4 JS |
757 | } |
758 | ||
5b58dc23 | 759 | /* old crc16 code */ |
2ffccc82 | 760 | offset = offsetof(struct ext2_group_desc, bg_checksum); |
5b58dc23 DW |
761 | crc = ext2fs_crc16(~0, fs->super->s_uuid, |
762 | sizeof(fs->super->s_uuid)); | |
763 | crc = ext2fs_crc16(crc, &group, sizeof(group)); | |
764 | crc = ext2fs_crc16(crc, desc, offset); | |
765 | offset += sizeof(desc->bg_checksum); /* skip checksum */ | |
766 | /* for checksum of struct ext4_group_desc do the rest...*/ | |
767 | if (offset < size) { | |
768 | crc = ext2fs_crc16(crc, (char *)desc + offset, | |
769 | size - offset); | |
770 | } | |
2ffccc82 TT |
771 | #ifdef WORDS_BIGENDIAN |
772 | /* | |
773 | * If the size of the bg descriptor is greater than 64 | |
774 | * bytes, which is the size of the traditional ext4 bg | |
775 | * descriptor, checksum the rest of the descriptor here | |
776 | */ | |
777 | if (save_size > ext4_bg_size) | |
778 | crc = ext2fs_crc16(crc, (char *)save_desc + ext4_bg_size, | |
779 | save_size - ext4_bg_size); | |
780 | #endif | |
5b58dc23 DW |
781 | |
782 | out: | |
ca2634a4 JS |
783 | return crc; |
784 | } | |
785 | ||
786 | int ext2fs_group_desc_csum_verify(ext2_filsys fs, dgrp_t group) | |
787 | { | |
5b58dc23 | 788 | if (ext2fs_has_group_desc_csum(fs) && |
d7cca6b0 | 789 | (ext2fs_bg_checksum(fs, group) != |
4729455f | 790 | ext2fs_group_desc_csum(fs, group))) |
ca2634a4 JS |
791 | return 0; |
792 | ||
793 | return 1; | |
794 | } | |
795 | ||
796 | void ext2fs_group_desc_csum_set(ext2_filsys fs, dgrp_t group) | |
797 | { | |
5b58dc23 | 798 | if (!ext2fs_has_group_desc_csum(fs)) |
d7cca6b0 VAH |
799 | return; |
800 | ||
801 | /* ext2fs_bg_checksum_set() sets the actual checksum field but | |
802 | * does not calculate the checksum itself. */ | |
803 | ext2fs_bg_checksum_set(fs, group, ext2fs_group_desc_csum(fs, group)); | |
ca2634a4 JS |
804 | } |
805 | ||
806 | static __u32 find_last_inode_ingrp(ext2fs_inode_bitmap bitmap, | |
807 | __u32 inodes_per_grp, dgrp_t grp_no) | |
808 | { | |
809 | ext2_ino_t i, start_ino, end_ino; | |
810 | ||
811 | start_ino = grp_no * inodes_per_grp + 1; | |
812 | end_ino = start_ino + inodes_per_grp - 1; | |
813 | ||
814 | for (i = end_ino; i >= start_ino; i--) { | |
8f82ef98 | 815 | if (ext2fs_fast_test_inode_bitmap2(bitmap, i)) |
ca2634a4 JS |
816 | return i - start_ino + 1; |
817 | } | |
818 | return inodes_per_grp; | |
819 | } | |
820 | ||
821 | /* update the bitmap flags, set the itable high watermark, and calculate | |
822 | * checksums for the group descriptors */ | |
f628acea | 823 | errcode_t ext2fs_set_gdt_csum(ext2_filsys fs) |
ca2634a4 JS |
824 | { |
825 | struct ext2_super_block *sb = fs->super; | |
8895f43a | 826 | int dirty = 0; |
ca2634a4 JS |
827 | dgrp_t i; |
828 | ||
f628acea AD |
829 | if (!fs->inode_map) |
830 | return EXT2_ET_NO_INODE_BITMAP; | |
831 | ||
5b58dc23 | 832 | if (!ext2fs_has_group_desc_csum(fs)) |
f628acea | 833 | return 0; |
ca2634a4 | 834 | |
d7cca6b0 | 835 | for (i = 0; i < fs->group_desc_count; i++) { |
d32c915a TT |
836 | __u32 old_csum = ext2fs_bg_checksum(fs, i); |
837 | __u32 old_unused = ext2fs_bg_itable_unused(fs, i); | |
838 | __u32 old_flags = ext2fs_bg_flags(fs, i); | |
839 | __u32 old_free_inodes_count = ext2fs_bg_free_inodes_count(fs, i); | |
c0ff3a21 DW |
840 | __u32 old_free_blocks_count = ext2fs_bg_free_blocks_count(fs, i); |
841 | ||
842 | if (old_free_blocks_count == sb->s_blocks_per_group && | |
843 | i != fs->group_desc_count - 1) | |
844 | ext2fs_bg_flags_set(fs, i, EXT2_BG_BLOCK_UNINIT); | |
ca2634a4 | 845 | |
d7cca6b0 VAH |
846 | if (old_free_inodes_count == sb->s_inodes_per_group) { |
847 | ext2fs_bg_flags_set(fs, i, EXT2_BG_INODE_UNINIT); | |
848 | ext2fs_bg_itable_unused_set(fs, i, sb->s_inodes_per_group); | |
16b851cd | 849 | } else { |
d7cca6b0 VAH |
850 | int unused = |
851 | sb->s_inodes_per_group - | |
ca2634a4 | 852 | find_last_inode_ingrp(fs->inode_map, |
d7cca6b0 VAH |
853 | sb->s_inodes_per_group, i); |
854 | ||
855 | ext2fs_bg_flags_clear(fs, i, EXT2_BG_INODE_UNINIT); | |
856 | ext2fs_bg_itable_unused_set(fs, i, unused); | |
ca2634a4 JS |
857 | } |
858 | ||
ca2634a4 | 859 | ext2fs_group_desc_csum_set(fs, i); |
d7cca6b0 | 860 | if (old_flags != ext2fs_bg_flags(fs, i)) |
80fc4e69 | 861 | dirty = 1; |
d7cca6b0 | 862 | if (old_unused != ext2fs_bg_itable_unused(fs, i)) |
80fc4e69 | 863 | dirty = 1; |
d7cca6b0 | 864 | if (old_csum != ext2fs_bg_checksum(fs, i)) |
ca2634a4 JS |
865 | dirty = 1; |
866 | } | |
867 | if (dirty) | |
868 | ext2fs_mark_super_dirty(fs); | |
f628acea | 869 | return 0; |
ca2634a4 | 870 | } |
470e737a TT |
871 | |
872 | #ifdef DEBUG | |
c816ecb2 ES |
873 | #include "e2p/e2p.h" |
874 | ||
470e737a TT |
875 | void print_csum(const char *msg, ext2_filsys fs, dgrp_t group) |
876 | { | |
877 | __u16 crc1, crc2, crc3; | |
878 | dgrp_t swabgroup; | |
b3c2a638 TT |
879 | struct ext2_group_desc *desc = ext2fs_group_desc(fs, fs->group_desc, |
880 | group); | |
881 | size_t size = EXT2_DESC_SIZE(fs->super); | |
470e737a | 882 | struct ext2_super_block *sb = fs->super; |
1d18a55c | 883 | int offset = offsetof(struct ext2_group_desc, bg_checksum); |
470e737a | 884 | #ifdef WORDS_BIGENDIAN |
1d18a55c | 885 | struct ext4_group_desc swabdesc; |
b3c2a638 TT |
886 | struct ext2_group_desc *save_desc = desc; |
887 | const size_t ext4_bg_size = sizeof(struct ext4_group_desc); | |
888 | size_t save_size = size; | |
1d18a55c | 889 | #endif |
470e737a | 890 | |
1d18a55c | 891 | #ifdef WORDS_BIGENDIAN |
470e737a | 892 | /* Have to swab back to little-endian to do the checksum */ |
b3c2a638 TT |
893 | if (size > ext4_bg_size) |
894 | size = ext4_bg_size; | |
1d18a55c TT |
895 | memcpy(&swabdesc, desc, size); |
896 | ext2fs_swap_group_desc2(fs, (struct ext2_group_desc *) &swabdesc); | |
897 | desc = (struct ext2_group_desc *) &swabdesc; | |
470e737a TT |
898 | |
899 | swabgroup = ext2fs_swab32(group); | |
900 | #else | |
901 | swabgroup = group; | |
902 | #endif | |
903 | ||
904 | crc1 = ext2fs_crc16(~0, sb->s_uuid, sizeof(fs->super->s_uuid)); | |
905 | crc2 = ext2fs_crc16(crc1, &swabgroup, sizeof(swabgroup)); | |
1d18a55c TT |
906 | crc3 = ext2fs_crc16(crc2, desc, offset); |
907 | offset += sizeof(desc->bg_checksum); /* skip checksum */ | |
908 | /* for checksum of struct ext4_group_desc do the rest...*/ | |
909 | if (offset < size) | |
910 | crc3 = ext2fs_crc16(crc3, (char *)desc + offset, size - offset); | |
b3c2a638 TT |
911 | #ifdef WORDS_BIGENDIAN |
912 | if (save_size > ext4_bg_size) | |
913 | crc3 = ext2fs_crc16(crc3, (char *)save_desc + ext4_bg_size, | |
914 | save_size - ext4_bg_size); | |
915 | #endif | |
1d18a55c | 916 | |
f797cf3e | 917 | printf("%s UUID %s=%04x, grp %u=%04x: %04x=%04x\n", |
562f2642 | 918 | msg, e2p_uuid2str(sb->s_uuid), crc1, group, crc2, crc3, |
c816ecb2 | 919 | ext2fs_group_desc_csum(fs, group)); |
470e737a TT |
920 | } |
921 | ||
922 | unsigned char sb_uuid[16] = { 0x4f, 0x25, 0xe8, 0xcf, 0xe7, 0x97, 0x48, 0x23, | |
923 | 0xbe, 0xfa, 0xa7, 0x88, 0x4b, 0xae, 0xec, 0xdb }; | |
924 | ||
925 | int main(int argc, char **argv) | |
926 | { | |
927 | struct ext2_super_block param; | |
928 | errcode_t retval; | |
929 | ext2_filsys fs; | |
930 | int i; | |
931 | __u16 csum1, csum2, csum_known = 0xd3a4; | |
932 | ||
933 | memset(¶m, 0, sizeof(param)); | |
4efbac6f | 934 | ext2fs_blocks_count_set(¶m, 32768); |
b3c2a638 TT |
935 | #if 0 |
936 | param.s_feature_incompat |= EXT4_FEATURE_INCOMPAT_64BIT; | |
937 | param.s_desc_size = 128; | |
938 | csum_known = 0x5b6e; | |
939 | #endif | |
470e737a | 940 | |
8f82ef98 | 941 | retval = ext2fs_initialize("test fs", EXT2_FLAG_64BITS, ¶m, |
470e737a TT |
942 | test_io_manager, &fs); |
943 | if (retval) { | |
944 | com_err("setup", retval, | |
945 | "While initializing filesystem"); | |
946 | exit(1); | |
947 | } | |
948 | memcpy(fs->super->s_uuid, sb_uuid, 16); | |
949 | fs->super->s_feature_ro_compat = EXT4_FEATURE_RO_COMPAT_GDT_CSUM; | |
950 | ||
951 | for (i=0; i < fs->group_desc_count; i++) { | |
d7cca6b0 VAH |
952 | ext2fs_block_bitmap_loc_set(fs, i, 124); |
953 | ext2fs_inode_bitmap_loc_set(fs, i, 125); | |
954 | ext2fs_inode_table_loc_set(fs, i, 126); | |
955 | ext2fs_bg_free_blocks_count_set(fs, i, 31119); | |
956 | ext2fs_bg_free_inodes_count_set(fs, i, 15701); | |
957 | ext2fs_bg_used_dirs_count_set(fs, i, 2); | |
e633b58a | 958 | ext2fs_bg_flags_zap(fs, i); |
470e737a TT |
959 | }; |
960 | ||
961 | csum1 = ext2fs_group_desc_csum(fs, 0); | |
962 | print_csum("csum0000", fs, 0); | |
963 | ||
964 | if (csum1 != csum_known) { | |
965 | printf("checksum for group 0 should be %04x\n", csum_known); | |
966 | exit(1); | |
967 | } | |
968 | csum2 = ext2fs_group_desc_csum(fs, 1); | |
969 | print_csum("csum0001", fs, 1); | |
970 | if (csum1 == csum2) { | |
971 | printf("checksums for different groups shouldn't match\n"); | |
972 | exit(1); | |
973 | } | |
974 | csum2 = ext2fs_group_desc_csum(fs, 2); | |
975 | print_csum("csumffff", fs, 2); | |
976 | if (csum1 == csum2) { | |
977 | printf("checksums for different groups shouldn't match\n"); | |
978 | exit(1); | |
979 | } | |
d7cca6b0 | 980 | ext2fs_bg_checksum_set(fs, 0, csum1); |
470e737a TT |
981 | csum2 = ext2fs_group_desc_csum(fs, 0); |
982 | print_csum("csum_set", fs, 0); | |
983 | if (csum1 != csum2) { | |
984 | printf("checksums should not depend on checksum field\n"); | |
985 | exit(1); | |
986 | } | |
987 | if (!ext2fs_group_desc_csum_verify(fs, 0)) { | |
988 | printf("checksums should verify against gd_checksum\n"); | |
989 | exit(1); | |
990 | } | |
991 | memset(fs->super->s_uuid, 0x30, sizeof(fs->super->s_uuid)); | |
992 | print_csum("new_uuid", fs, 0); | |
993 | if (ext2fs_group_desc_csum_verify(fs, 0) != 0) { | |
994 | printf("checksums for different filesystems shouldn't match\n"); | |
995 | exit(1); | |
996 | } | |
d7cca6b0 VAH |
997 | csum1 = ext2fs_group_desc_csum(fs, 0); |
998 | ext2fs_bg_checksum_set(fs, 0, csum1); | |
470e737a | 999 | print_csum("csum_new", fs, 0); |
d7cca6b0 | 1000 | ext2fs_bg_free_blocks_count_set(fs, 0, 1); |
470e737a TT |
1001 | csum2 = ext2fs_group_desc_csum(fs, 0); |
1002 | print_csum("csum_blk", fs, 0); | |
1003 | if (csum1 == csum2) { | |
1004 | printf("checksums for different data shouldn't match\n"); | |
1005 | exit(1); | |
1006 | } | |
151c49bc | 1007 | ext2fs_free(fs); |
470e737a TT |
1008 | |
1009 | return 0; | |
1010 | } | |
1011 | #endif |