2 * csum.c --- checksumming of ext3 structures
4 * Copyright (C) 2006 Cluster File Systems, Inc.
5 * Copyright (C) 2006, 2007 by Andreas Dilger <adilger@clusterfs.com>
8 * This file may be redistributed under the terms of the GNU Public
14 #include <sys/types.h>
23 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
32 STATIC __u16 ext2fs_group_desc_csum(ext2_filsys fs, dgrp_t group)
35 struct ext2_group_desc *desc;
37 desc = &fs->group_desc[group];
39 if (fs->super->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
40 int offset = offsetof(struct ext2_group_desc, bg_checksum);
42 #ifdef WORDS_BIGENDIAN
43 struct ext2_group_desc swabdesc = *desc;
45 /* Have to swab back to little-endian to do the checksum */
46 ext2fs_swap_group_desc(&swabdesc);
49 group = ext2fs_swab32(group);
51 crc = ext2fs_crc16(~0, fs->super->s_uuid,
52 sizeof(fs->super->s_uuid));
53 crc = ext2fs_crc16(crc, &group, sizeof(group));
54 crc = ext2fs_crc16(crc, desc, offset);
55 offset += sizeof(desc->bg_checksum); /* skip checksum */
56 assert(offset == sizeof(*desc));
57 /* for checksum of struct ext4_group_desc do the rest...*/
58 if (offset < fs->super->s_desc_size) {
59 crc = ext2fs_crc16(crc, (char *)desc + offset,
60 fs->super->s_desc_size - offset);
67 int ext2fs_group_desc_csum_verify(ext2_filsys fs, dgrp_t group)
69 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
70 EXT4_FEATURE_RO_COMPAT_GDT_CSUM) &&
71 (fs->group_desc[group].bg_checksum !=
72 ext2fs_group_desc_csum(fs, group)))
78 void ext2fs_group_desc_csum_set(ext2_filsys fs, dgrp_t group)
80 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
81 EXT4_FEATURE_RO_COMPAT_GDT_CSUM))
82 fs->group_desc[group].bg_checksum =
83 ext2fs_group_desc_csum(fs, group);
86 static __u32 find_last_inode_ingrp(ext2fs_inode_bitmap bitmap,
87 __u32 inodes_per_grp, dgrp_t grp_no)
89 ext2_ino_t i, start_ino, end_ino;
91 start_ino = grp_no * inodes_per_grp + 1;
92 end_ino = start_ino + inodes_per_grp - 1;
94 for (i = end_ino; i >= start_ino; i--) {
95 if (ext2fs_fast_test_inode_bitmap(bitmap, i))
96 return i - start_ino + 1;
98 return inodes_per_grp;
101 /* update the bitmap flags, set the itable high watermark, and calculate
102 * checksums for the group descriptors */
103 errcode_t ext2fs_set_gdt_csum(ext2_filsys fs)
105 struct ext2_super_block *sb = fs->super;
106 struct ext2_group_desc *bg = fs->group_desc;
111 return EXT2_ET_NO_INODE_BITMAP;
113 if (!EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
114 EXT4_FEATURE_RO_COMPAT_GDT_CSUM))
117 for (i = 0; i < fs->group_desc_count; i++, bg++) {
118 int old_csum = bg->bg_checksum;
119 int old_unused = bg->bg_itable_unused;
120 int old_flags = bg->bg_flags;
122 if (bg->bg_free_inodes_count == sb->s_inodes_per_group) {
123 bg->bg_flags |= EXT2_BG_INODE_UNINIT;
124 bg->bg_itable_unused = sb->s_inodes_per_group;
126 bg->bg_flags &= ~EXT2_BG_INODE_UNINIT;
127 bg->bg_itable_unused = sb->s_inodes_per_group -
128 find_last_inode_ingrp(fs->inode_map,
129 sb->s_inodes_per_group,i);
132 ext2fs_group_desc_csum_set(fs, i);
133 if (old_flags != bg->bg_flags)
135 if (old_unused != bg->bg_itable_unused)
137 if (old_csum != bg->bg_checksum)
141 ext2fs_mark_super_dirty(fs);
146 void print_csum(const char *msg, ext2_filsys fs, dgrp_t group)
148 __u16 crc1, crc2, crc3;
150 struct ext2_group_desc *desc = &fs->group_desc[group];
151 struct ext2_super_block *sb = fs->super;
153 #ifdef WORDS_BIGENDIAN
154 struct ext2_group_desc swabdesc = fs->group_desc[group];
156 /* Have to swab back to little-endian to do the checksum */
157 ext2fs_swap_group_desc(&swabdesc);
160 swabgroup = ext2fs_swab32(group);
165 crc1 = ext2fs_crc16(~0, sb->s_uuid, sizeof(fs->super->s_uuid));
166 crc2 = ext2fs_crc16(crc1, &swabgroup, sizeof(swabgroup));
167 crc3 = ext2fs_crc16(crc2, desc,
168 offsetof(struct ext2_group_desc, bg_checksum));
169 printf("%s: UUID %016Lx%016Lx(%04x), grp %u(%04x): %04x=%04x\n",
170 msg, *(long long *)&sb->s_uuid, *(long long *)&sb->s_uuid[8],
171 crc1, group, crc2, crc3, ext2fs_group_desc_csum(fs, group));
174 unsigned char sb_uuid[16] = { 0x4f, 0x25, 0xe8, 0xcf, 0xe7, 0x97, 0x48, 0x23,
175 0xbe, 0xfa, 0xa7, 0x88, 0x4b, 0xae, 0xec, 0xdb };
177 int main(int argc, char **argv)
179 struct ext2_super_block param;
183 __u16 csum1, csum2, csum_known = 0xd3a4;
185 memset(¶m, 0, sizeof(param));
186 param.s_blocks_count = 32768;
188 retval = ext2fs_initialize("test fs", 0, ¶m,
189 test_io_manager, &fs);
191 com_err("setup", retval,
192 "While initializing filesystem");
195 memcpy(fs->super->s_uuid, sb_uuid, 16);
196 fs->super->s_feature_ro_compat = EXT4_FEATURE_RO_COMPAT_GDT_CSUM;
198 for (i=0; i < fs->group_desc_count; i++) {
199 fs->group_desc[i].bg_block_bitmap = 124;
200 fs->group_desc[i].bg_inode_bitmap = 125;
201 fs->group_desc[i].bg_inode_table = 126;
202 fs->group_desc[i].bg_free_blocks_count = 31119;
203 fs->group_desc[i].bg_free_inodes_count = 15701;
204 fs->group_desc[i].bg_used_dirs_count = 2;
205 fs->group_desc[i].bg_flags = 0;
208 csum1 = ext2fs_group_desc_csum(fs, 0);
209 print_csum("csum0000", fs, 0);
211 if (csum1 != csum_known) {
212 printf("checksum for group 0 should be %04x\n", csum_known);
215 csum2 = ext2fs_group_desc_csum(fs, 1);
216 print_csum("csum0001", fs, 1);
217 if (csum1 == csum2) {
218 printf("checksums for different groups shouldn't match\n");
221 csum2 = ext2fs_group_desc_csum(fs, 2);
222 print_csum("csumffff", fs, 2);
223 if (csum1 == csum2) {
224 printf("checksums for different groups shouldn't match\n");
227 fs->group_desc[0].bg_checksum = csum1;
228 csum2 = ext2fs_group_desc_csum(fs, 0);
229 print_csum("csum_set", fs, 0);
230 if (csum1 != csum2) {
231 printf("checksums should not depend on checksum field\n");
234 if (!ext2fs_group_desc_csum_verify(fs, 0)) {
235 printf("checksums should verify against gd_checksum\n");
238 memset(fs->super->s_uuid, 0x30, sizeof(fs->super->s_uuid));
239 print_csum("new_uuid", fs, 0);
240 if (ext2fs_group_desc_csum_verify(fs, 0) != 0) {
241 printf("checksums for different filesystems shouldn't match\n");
244 csum1 = fs->group_desc[0].bg_checksum = ext2fs_group_desc_csum(fs, 0);
245 print_csum("csum_new", fs, 0);
246 fs->group_desc[0].bg_free_blocks_count = 1;
247 csum2 = ext2fs_group_desc_csum(fs, 0);
248 print_csum("csum_blk", fs, 0);
249 if (csum1 == csum2) {
250 printf("checksums for different data shouldn't match\n");