blob: b681d342e919f99d34c21c866f0e44fd82cde3f5 [file] [log] [blame]
lh9ed821d2023-04-07 01:36:19 -07001/*
2 * csum.c --- checksumming of ext3 structures
3 *
4 * Copyright (C) 2006 Cluster File Systems, Inc.
5 * Copyright (C) 2006, 2007 by Andreas Dilger <adilger@clusterfs.com>
6 *
7 * %Begin-Header%
8 * This file may be redistributed under the terms of the GNU Library
9 * General Public License, version 2.
10 * %End-Header%
11 */
12
13#include "config.h"
14#if HAVE_SYS_TYPES_H
15#include <sys/types.h>
16#endif
17
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
33__u16 ext2fs_group_desc_csum(ext2_filsys fs, dgrp_t group)
34{
35 struct ext2_group_desc *desc = ext2fs_group_desc(fs, fs->group_desc,
36 group);
37 size_t size = EXT2_DESC_SIZE(fs->super);
38 size_t offset;
39 __u16 crc;
40
41 if (fs->super->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
42 size_t offset = offsetof(struct ext2_group_desc, bg_checksum);
43
44#ifdef WORDS_BIGENDIAN
45 struct ext4_group_desc swabdesc;
46 size_t save_size = size;
47 const size_t ext4_bg_size = sizeof(struct ext4_group_desc);
48 struct ext2_group_desc *save_desc = desc;
49
50 /* Have to swab back to little-endian to do the checksum */
51 if (size > ext4_bg_size)
52 size = ext4_bg_size;
53 memcpy(&swabdesc, desc, size);
54 ext2fs_swap_group_desc2(fs,
55 (struct ext2_group_desc *) &swabdesc);
56 desc = (struct ext2_group_desc *) &swabdesc;
57
58 group = ext2fs_swab32(group);
59#endif
60 crc = ext2fs_crc16(~0, fs->super->s_uuid,
61 sizeof(fs->super->s_uuid));
62 crc = ext2fs_crc16(crc, &group, sizeof(group));
63 crc = ext2fs_crc16(crc, desc, offset);
64 offset += sizeof(desc->bg_checksum); /* skip checksum */
65 /* for checksum of struct ext4_group_desc do the rest...*/
66 if (offset < size) {
67 crc = ext2fs_crc16(crc, (char *)desc + offset,
68 size - offset);
69 }
70#ifdef WORDS_BIGENDIAN
71 /*
72 * If the size of the bg descriptor is greater than 64
73 * bytes, which is the size of the traditional ext4 bg
74 * descriptor, checksum the rest of the descriptor here
75 */
76 if (save_size > ext4_bg_size)
77 crc = ext2fs_crc16(crc,
78 (char *)save_desc + ext4_bg_size,
79 save_size - ext4_bg_size);
80#endif
81 }
82
83 return crc;
84}
85
86int ext2fs_group_desc_csum_verify(ext2_filsys fs, dgrp_t group)
87{
88 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
89 EXT4_FEATURE_RO_COMPAT_GDT_CSUM) &&
90 (ext2fs_bg_checksum(fs, group) !=
91 ext2fs_group_desc_csum(fs, group)))
92 return 0;
93
94 return 1;
95}
96
97void ext2fs_group_desc_csum_set(ext2_filsys fs, dgrp_t group)
98{
99 if (!EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
100 EXT4_FEATURE_RO_COMPAT_GDT_CSUM))
101 return;
102
103 /* ext2fs_bg_checksum_set() sets the actual checksum field but
104 * does not calculate the checksum itself. */
105 ext2fs_bg_checksum_set(fs, group, ext2fs_group_desc_csum(fs, group));
106}
107
108static __u32 find_last_inode_ingrp(ext2fs_inode_bitmap bitmap,
109 __u32 inodes_per_grp, dgrp_t grp_no)
110{
111 ext2_ino_t i, start_ino, end_ino;
112
113 start_ino = grp_no * inodes_per_grp + 1;
114 end_ino = start_ino + inodes_per_grp - 1;
115
116 for (i = end_ino; i >= start_ino; i--) {
117 if (ext2fs_fast_test_inode_bitmap2(bitmap, i))
118 return i - start_ino + 1;
119 }
120 return inodes_per_grp;
121}
122
123/* update the bitmap flags, set the itable high watermark, and calculate
124 * checksums for the group descriptors */
125errcode_t ext2fs_set_gdt_csum(ext2_filsys fs)
126{
127 struct ext2_super_block *sb = fs->super;
128 int dirty = 0;
129 dgrp_t i;
130
131 if (!fs->inode_map)
132 return EXT2_ET_NO_INODE_BITMAP;
133
134 if (!EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
135 EXT4_FEATURE_RO_COMPAT_GDT_CSUM))
136 return 0;
137
138 for (i = 0; i < fs->group_desc_count; i++) {
139 __u32 old_csum = ext2fs_bg_checksum(fs, i);
140 __u32 old_unused = ext2fs_bg_itable_unused(fs, i);
141 __u32 old_flags = ext2fs_bg_flags(fs, i);
142 __u32 old_free_inodes_count = ext2fs_bg_free_inodes_count(fs, i);
143
144 if (old_free_inodes_count == sb->s_inodes_per_group) {
145 ext2fs_bg_flags_set(fs, i, EXT2_BG_INODE_UNINIT);
146 ext2fs_bg_itable_unused_set(fs, i, sb->s_inodes_per_group);
147 } else {
148 int unused =
149 sb->s_inodes_per_group -
150 find_last_inode_ingrp(fs->inode_map,
151 sb->s_inodes_per_group, i);
152
153 ext2fs_bg_flags_clear(fs, i, EXT2_BG_INODE_UNINIT);
154 ext2fs_bg_itable_unused_set(fs, i, unused);
155 }
156
157 ext2fs_group_desc_csum_set(fs, i);
158 if (old_flags != ext2fs_bg_flags(fs, i))
159 dirty = 1;
160 if (old_unused != ext2fs_bg_itable_unused(fs, i))
161 dirty = 1;
162 if (old_csum != ext2fs_bg_checksum(fs, i))
163 dirty = 1;
164 }
165 if (dirty)
166 ext2fs_mark_super_dirty(fs);
167 return 0;
168}
169
170#ifdef DEBUG
171#include "e2p/e2p.h"
172
173void print_csum(const char *msg, ext2_filsys fs, dgrp_t group)
174{
175 __u16 crc1, crc2, crc3;
176 dgrp_t swabgroup;
177 struct ext2_group_desc *desc = ext2fs_group_desc(fs, fs->group_desc,
178 group);
179 size_t size = EXT2_DESC_SIZE(fs->super);
180 struct ext2_super_block *sb = fs->super;
181 int offset = offsetof(struct ext2_group_desc, bg_checksum);
182#ifdef WORDS_BIGENDIAN
183 struct ext4_group_desc swabdesc;
184 struct ext2_group_desc *save_desc = desc;
185 const size_t ext4_bg_size = sizeof(struct ext4_group_desc);
186 size_t save_size = size;
187#endif
188
189#ifdef WORDS_BIGENDIAN
190 /* Have to swab back to little-endian to do the checksum */
191 if (size > ext4_bg_size)
192 size = ext4_bg_size;
193 memcpy(&swabdesc, desc, size);
194 ext2fs_swap_group_desc2(fs, (struct ext2_group_desc *) &swabdesc);
195 desc = (struct ext2_group_desc *) &swabdesc;
196
197 swabgroup = ext2fs_swab32(group);
198#else
199 swabgroup = group;
200#endif
201
202 crc1 = ext2fs_crc16(~0, sb->s_uuid, sizeof(fs->super->s_uuid));
203 crc2 = ext2fs_crc16(crc1, &swabgroup, sizeof(swabgroup));
204 crc3 = ext2fs_crc16(crc2, desc, offset);
205 offset += sizeof(desc->bg_checksum); /* skip checksum */
206 /* for checksum of struct ext4_group_desc do the rest...*/
207 if (offset < size)
208 crc3 = ext2fs_crc16(crc3, (char *)desc + offset, size - offset);
209#ifdef WORDS_BIGENDIAN
210 if (save_size > ext4_bg_size)
211 crc3 = ext2fs_crc16(crc3, (char *)save_desc + ext4_bg_size,
212 save_size - ext4_bg_size);
213#endif
214
215 printf("%s UUID %s=%04x, grp %u=%04x: %04x=%04x\n",
216 msg, e2p_uuid2str(sb->s_uuid), crc1, group, crc2, crc3,
217 ext2fs_group_desc_csum(fs, group));
218}
219
220unsigned char sb_uuid[16] = { 0x4f, 0x25, 0xe8, 0xcf, 0xe7, 0x97, 0x48, 0x23,
221 0xbe, 0xfa, 0xa7, 0x88, 0x4b, 0xae, 0xec, 0xdb };
222
223int main(int argc, char **argv)
224{
225 struct ext2_super_block param;
226 errcode_t retval;
227 ext2_filsys fs;
228 int i;
229 __u16 csum1, csum2, csum_known = 0xd3a4;
230
231 memset(&param, 0, sizeof(param));
232 ext2fs_blocks_count_set(&param, 32768);
233#if 0
234 param.s_feature_incompat |= EXT4_FEATURE_INCOMPAT_64BIT;
235 param.s_desc_size = 128;
236 csum_known = 0x5b6e;
237#endif
238
239 retval = ext2fs_initialize("test fs", EXT2_FLAG_64BITS, &param,
240 test_io_manager, &fs);
241 if (retval) {
242 com_err("setup", retval,
243 "While initializing filesystem");
244 exit(1);
245 }
246 memcpy(fs->super->s_uuid, sb_uuid, 16);
247 fs->super->s_feature_ro_compat = EXT4_FEATURE_RO_COMPAT_GDT_CSUM;
248
249 for (i=0; i < fs->group_desc_count; i++) {
250 ext2fs_block_bitmap_loc_set(fs, i, 124);
251 ext2fs_inode_bitmap_loc_set(fs, i, 125);
252 ext2fs_inode_table_loc_set(fs, i, 126);
253 ext2fs_bg_free_blocks_count_set(fs, i, 31119);
254 ext2fs_bg_free_inodes_count_set(fs, i, 15701);
255 ext2fs_bg_used_dirs_count_set(fs, i, 2);
256 ext2fs_bg_flags_zap(fs, i);
257 };
258
259 csum1 = ext2fs_group_desc_csum(fs, 0);
260 print_csum("csum0000", fs, 0);
261
262 if (csum1 != csum_known) {
263 printf("checksum for group 0 should be %04x\n", csum_known);
264 exit(1);
265 }
266 csum2 = ext2fs_group_desc_csum(fs, 1);
267 print_csum("csum0001", fs, 1);
268 if (csum1 == csum2) {
269 printf("checksums for different groups shouldn't match\n");
270 exit(1);
271 }
272 csum2 = ext2fs_group_desc_csum(fs, 2);
273 print_csum("csumffff", fs, 2);
274 if (csum1 == csum2) {
275 printf("checksums for different groups shouldn't match\n");
276 exit(1);
277 }
278 ext2fs_bg_checksum_set(fs, 0, csum1);
279 csum2 = ext2fs_group_desc_csum(fs, 0);
280 print_csum("csum_set", fs, 0);
281 if (csum1 != csum2) {
282 printf("checksums should not depend on checksum field\n");
283 exit(1);
284 }
285 if (!ext2fs_group_desc_csum_verify(fs, 0)) {
286 printf("checksums should verify against gd_checksum\n");
287 exit(1);
288 }
289 memset(fs->super->s_uuid, 0x30, sizeof(fs->super->s_uuid));
290 print_csum("new_uuid", fs, 0);
291 if (ext2fs_group_desc_csum_verify(fs, 0) != 0) {
292 printf("checksums for different filesystems shouldn't match\n");
293 exit(1);
294 }
295 csum1 = ext2fs_group_desc_csum(fs, 0);
296 ext2fs_bg_checksum_set(fs, 0, csum1);
297 print_csum("csum_new", fs, 0);
298 ext2fs_bg_free_blocks_count_set(fs, 0, 1);
299 csum2 = ext2fs_group_desc_csum(fs, 0);
300 print_csum("csum_blk", fs, 0);
301 if (csum1 == csum2) {
302 printf("checksums for different data shouldn't match\n");
303 exit(1);
304 }
305
306 return 0;
307}
308#endif