csum.c revision 0eeec8ac61bf1eaa31533b2be825cd75580829c9
1/* 2 * csum.c --- checksumming of ext3 structures 3 * 4 * Copyright (C) 2006 Cluster File Systems, Inc. 5 * 6 * %Begin-Header% 7 * This file may be redistributed under the terms of the GNU Public 8 * License. 9 * %End-Header% 10 */ 11 12#if HAVE_SYS_TYPES_H 13#include <sys/types.h> 14#endif 15 16#include "ext2_fs.h" 17#include "ext2fs.h" 18#include "crc16.h" 19#include <assert.h> 20 21#ifndef offsetof 22#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER) 23#endif 24 25#ifdef DEBUG 26#define STATIC 27#else 28#define STATIC static 29#endif 30 31STATIC __u16 ext2fs_group_desc_csum(ext2_filsys fs, dgrp_t group) 32{ 33 __u16 crc = 0; 34 struct ext2_group_desc *desc; 35 36 desc = &fs->group_desc[group]; 37 38 if (fs->super->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_GDT_CSUM) { 39 int offset = offsetof(struct ext2_group_desc, bg_checksum); 40 41#ifdef WORDS_BIGENDIAN 42 struct ext2_group_desc swabdesc = *desc; 43 44 /* Have to swab back to little-endian to do the checksum */ 45 ext2fs_swap_group_desc(&swabdesc); 46 desc = &swabdesc; 47 48 group = ext2fs_swab32(group); 49#endif 50 crc = ext2fs_crc16(~0, fs->super->s_uuid, 51 sizeof(fs->super->s_uuid)); 52 crc = ext2fs_crc16(crc, &group, sizeof(group)); 53 crc = ext2fs_crc16(crc, desc, offset); 54 offset += sizeof(desc->bg_checksum); /* skip checksum */ 55 assert(offset == sizeof(*desc)); 56 /* for checksum of struct ext4_group_desc do the rest...*/ 57 if (offset < fs->super->s_desc_size) { 58 crc = ext2fs_crc16(crc, (char *)desc + offset, 59 fs->super->s_desc_size - offset); 60 } 61 } 62 63 return crc; 64} 65 66int ext2fs_group_desc_csum_verify(ext2_filsys fs, dgrp_t group) 67{ 68 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super, 69 EXT4_FEATURE_RO_COMPAT_GDT_CSUM) && 70 (fs->group_desc[group].bg_checksum != 71 ext2fs_group_desc_csum(fs, group))) 72 return 0; 73 74 return 1; 75} 76 77void ext2fs_group_desc_csum_set(ext2_filsys fs, dgrp_t group) 78{ 79 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super, 80 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) 81 fs->group_desc[group].bg_checksum = 82 ext2fs_group_desc_csum(fs, group); 83} 84 85static __u32 find_last_inode_ingrp(ext2fs_inode_bitmap bitmap, 86 __u32 inodes_per_grp, dgrp_t grp_no) 87{ 88 ext2_ino_t i, start_ino, end_ino; 89 90 start_ino = grp_no * inodes_per_grp + 1; 91 end_ino = start_ino + inodes_per_grp - 1; 92 93 for (i = end_ino; i >= start_ino; i--) { 94 if (ext2fs_fast_test_inode_bitmap(bitmap, i)) 95 return i - start_ino + 1; 96 } 97 return inodes_per_grp; 98} 99 100/* update the bitmap flags, set the itable high watermark, and calculate 101 * checksums for the group descriptors */ 102errcode_t ext2fs_set_gdt_csum(ext2_filsys fs) 103{ 104 struct ext2_super_block *sb = fs->super; 105 struct ext2_group_desc *bg = fs->group_desc; 106 int dirty = 0; 107 dgrp_t i; 108 109 if (!fs->inode_map) 110 return EXT2_ET_NO_INODE_BITMAP; 111 112 if (!EXT2_HAS_RO_COMPAT_FEATURE(fs->super, 113 EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) 114 return 0; 115 116 for (i = 0; i < fs->group_desc_count; i++, bg++) { 117 int old_csum = bg->bg_checksum; 118 int old_unused = bg->bg_itable_unused; 119 int old_flags = bg->bg_flags; 120 121 if (bg->bg_free_inodes_count == sb->s_inodes_per_group) { 122 bg->bg_flags |= EXT2_BG_INODE_UNINIT; 123 bg->bg_itable_unused = sb->s_inodes_per_group; 124 } else { 125 bg->bg_flags &= ~EXT2_BG_INODE_UNINIT; 126 bg->bg_itable_unused = sb->s_inodes_per_group - 127 find_last_inode_ingrp(fs->inode_map, 128 sb->s_inodes_per_group,i); 129 } 130 131 ext2fs_group_desc_csum_set(fs, i); 132 if (old_flags != bg->bg_flags) 133 dirty = 1; 134 if (old_unused != bg->bg_itable_unused) 135 dirty = 1; 136 if (old_csum != bg->bg_checksum) 137 dirty = 1; 138 } 139 if (dirty) 140 ext2fs_mark_super_dirty(fs); 141 return 0; 142} 143