2 * csum.c --- checksumming of ext3 structures
4 * Copyright (C) 2006 Cluster File Systems, Inc.
7 * This file may be redistributed under the terms of the GNU Public
18 #define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
27 STATIC __u16 ext2fs_group_desc_csum(ext2_filsys fs, dgrp_t group)
30 struct ext2_group_desc *desc;
32 desc = &fs->group_desc[group];
34 if (fs->super->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
35 int offset = offsetof(struct ext2_group_desc, bg_checksum);
37 #ifdef WORDS_BIGENDIAN
38 struct ext2_group_desc swabdesc = *desc;
40 /* Have to swab back to little-endian to do the checksum */
41 ext2fs_swap_group_desc(&swabdesc);
44 group = ext2fs_swab32(group);
46 crc = crc16(~0, fs->super->s_uuid, sizeof(fs->super->s_uuid));
47 crc = crc16(crc, &group, sizeof(group));
48 crc = crc16(crc, desc, offset);
49 offset += sizeof(desc->bg_checksum); /* skip checksum */
50 assert(offset == sizeof(*desc));
51 /* for checksum of struct ext4_group_desc do the rest...*/
52 if (offset < fs->super->s_desc_size) {
53 crc = crc16(crc, (char *)desc + offset,
54 fs->super->s_desc_size - offset);
61 int ext2fs_group_desc_csum_verify(ext2_filsys fs, dgrp_t group)
63 if (fs->group_desc[group].bg_checksum !=
64 ext2fs_group_desc_csum(fs, group))
70 void ext2fs_group_desc_csum_set(ext2_filsys fs, dgrp_t group)
72 fs->group_desc[group].bg_checksum = ext2fs_group_desc_csum(fs, group);
75 static __u32 find_last_inode_ingrp(ext2fs_inode_bitmap bitmap,
76 __u32 inodes_per_grp, dgrp_t grp_no)
78 ext2_ino_t i, start_ino, end_ino;
80 start_ino = grp_no * inodes_per_grp + 1;
81 end_ino = start_ino + inodes_per_grp - 1;
83 for (i = end_ino; i >= start_ino; i--) {
84 if (ext2fs_fast_test_inode_bitmap(bitmap, i))
85 return i - start_ino + 1;
87 return inodes_per_grp;
90 /* update the bitmap flags, set the itable high watermark, and calculate
91 * checksums for the group descriptors */
92 errcode_t ext2fs_set_gdt_csum(ext2_filsys fs)
94 struct ext2_super_block *sb = fs->super;
95 struct ext2_group_desc *bg = fs->group_desc;
100 return EXT2_ET_NO_INODE_BITMAP;
102 if (!EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
103 EXT4_FEATURE_RO_COMPAT_GDT_CSUM))
106 for (i = 0; i < fs->group_desc_count; i++, bg++) {
107 int old_csum = bg->bg_checksum;
108 int old_unused = bg->bg_itable_unused;
109 int old_flags = bg->bg_flags;
111 if (bg->bg_free_inodes_count == sb->s_inodes_per_group) {
112 bg->bg_flags |= EXT2_BG_INODE_UNINIT;
113 bg->bg_itable_unused = sb->s_inodes_per_group;
115 bg->bg_flags &= ~EXT2_BG_INODE_UNINIT;
116 bg->bg_itable_unused = sb->s_inodes_per_group -
117 find_last_inode_ingrp(fs->inode_map,
118 sb->s_inodes_per_group,i);
121 ext2fs_group_desc_csum_set(fs, i);
122 if (old_flags != bg->bg_flags)
124 if (old_unused != bg->bg_itable_unused)
126 if (old_csum != bg->bg_checksum)
130 ext2fs_mark_super_dirty(fs);