blob: 5ee34457e845a35616b68f895c8681e9db62c1f4 [file] [log] [blame]
Jose R. Santosca2634a2007-10-21 21:03:19 -05001/*
2 * csum.c --- checksumming of ext3 structures
3 *
4 * Copyright (C) 2006 Cluster File Systems, Inc.
Theodore Ts'o470e7372009-05-29 11:01:22 -04005 * Copyright (C) 2006, 2007 by Andreas Dilger <adilger@clusterfs.com>
Jose R. Santosca2634a2007-10-21 21:03:19 -05006 *
7 * %Begin-Header%
Theodore Ts'o543547a2010-05-17 21:31:56 -04008 * This file may be redistributed under the terms of the GNU Library
9 * General Public License, version 2.
Jose R. Santosca2634a2007-10-21 21:03:19 -050010 * %End-Header%
11 */
12
Theodore Ts'od1154eb2011-09-18 17:34:37 -040013#include "config.h"
Theodore Ts'o0eeec8a2008-09-12 09:10:39 -040014#if HAVE_SYS_TYPES_H
15#include <sys/types.h>
16#endif
17
Jose R. Santosca2634a2007-10-21 21:03:19 -050018#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
Theodore Ts'o87141782012-03-15 12:13:25 -040033__u16 ext2fs_group_desc_csum(ext2_filsys fs, dgrp_t group)
Jose R. Santosca2634a2007-10-21 21:03:19 -050034{
35 __u16 crc = 0;
36 struct ext2_group_desc *desc;
Theodore Ts'o1d18a552010-02-15 09:51:28 -050037 size_t size;
38
39 size = fs->super->s_desc_size;
40 if (size < EXT2_MIN_DESC_SIZE)
41 size = EXT2_MIN_DESC_SIZE;
42 if (size > sizeof(struct ext4_group_desc)) {
43 printf("%s: illegal s_desc_size(%zd)\n", __func__, size);
44 size = sizeof(struct ext4_group_desc);
45 }
Jose R. Santosca2634a2007-10-21 21:03:19 -050046
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -040047 desc = ext2fs_group_desc(fs, fs->group_desc, group);
Jose R. Santosca2634a2007-10-21 21:03:19 -050048
49 if (fs->super->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
Theodore Ts'od32c9152011-07-07 13:50:22 -040050 size_t offset = offsetof(struct ext2_group_desc, bg_checksum);
Jose R. Santosca2634a2007-10-21 21:03:19 -050051
52#ifdef WORDS_BIGENDIAN
Theodore Ts'o1d18a552010-02-15 09:51:28 -050053 struct ext4_group_desc swabdesc;
Jose R. Santosca2634a2007-10-21 21:03:19 -050054
55 /* Have to swab back to little-endian to do the checksum */
Theodore Ts'o1d18a552010-02-15 09:51:28 -050056 memcpy(&swabdesc, desc, size);
57 ext2fs_swap_group_desc2(fs,
58 (struct ext2_group_desc *) &swabdesc);
59 desc = (struct ext2_group_desc *) &swabdesc;
Jose R. Santosca2634a2007-10-21 21:03:19 -050060
61 group = ext2fs_swab32(group);
62#endif
Theodore Ts'oc4dcb1c2008-08-24 22:44:33 -040063 crc = ext2fs_crc16(~0, fs->super->s_uuid,
64 sizeof(fs->super->s_uuid));
65 crc = ext2fs_crc16(crc, &group, sizeof(group));
66 crc = ext2fs_crc16(crc, desc, offset);
Jose R. Santosca2634a2007-10-21 21:03:19 -050067 offset += sizeof(desc->bg_checksum); /* skip checksum */
Jose R. Santosca2634a2007-10-21 21:03:19 -050068 /* for checksum of struct ext4_group_desc do the rest...*/
Theodore Ts'o1d18a552010-02-15 09:51:28 -050069 if (offset < size) {
Theodore Ts'oc4dcb1c2008-08-24 22:44:33 -040070 crc = ext2fs_crc16(crc, (char *)desc + offset,
Theodore Ts'o1d18a552010-02-15 09:51:28 -050071 size - offset);
Jose R. Santosca2634a2007-10-21 21:03:19 -050072 }
73 }
74
75 return crc;
76}
77
78int ext2fs_group_desc_csum_verify(ext2_filsys fs, dgrp_t group)
79{
Theodore Ts'o47294552008-07-13 19:03:59 -040080 if (EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
81 EXT4_FEATURE_RO_COMPAT_GDT_CSUM) &&
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -040082 (ext2fs_bg_checksum(fs, group) !=
Theodore Ts'o47294552008-07-13 19:03:59 -040083 ext2fs_group_desc_csum(fs, group)))
Jose R. Santosca2634a2007-10-21 21:03:19 -050084 return 0;
85
86 return 1;
87}
88
89void ext2fs_group_desc_csum_set(ext2_filsys fs, dgrp_t group)
90{
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -040091 if (!EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
92 EXT4_FEATURE_RO_COMPAT_GDT_CSUM))
93 return;
94
95 /* ext2fs_bg_checksum_set() sets the actual checksum field but
96 * does not calculate the checksum itself. */
97 ext2fs_bg_checksum_set(fs, group, ext2fs_group_desc_csum(fs, group));
Jose R. Santosca2634a2007-10-21 21:03:19 -050098}
99
100static __u32 find_last_inode_ingrp(ext2fs_inode_bitmap bitmap,
101 __u32 inodes_per_grp, dgrp_t grp_no)
102{
103 ext2_ino_t i, start_ino, end_ino;
104
105 start_ino = grp_no * inodes_per_grp + 1;
106 end_ino = start_ino + inodes_per_grp - 1;
107
108 for (i = end_ino; i >= start_ino; i--) {
Valerie Aurora Henson8f82ef92009-08-05 00:27:10 -0400109 if (ext2fs_fast_test_inode_bitmap2(bitmap, i))
Jose R. Santosca2634a2007-10-21 21:03:19 -0500110 return i - start_ino + 1;
111 }
112 return inodes_per_grp;
113}
114
115/* update the bitmap flags, set the itable high watermark, and calculate
116 * checksums for the group descriptors */
Andreas Dilgerf628ace2008-03-31 10:50:19 -0400117errcode_t ext2fs_set_gdt_csum(ext2_filsys fs)
Jose R. Santosca2634a2007-10-21 21:03:19 -0500118{
119 struct ext2_super_block *sb = fs->super;
Theodore Ts'o8895f432008-06-07 11:53:56 -0400120 int dirty = 0;
Jose R. Santosca2634a2007-10-21 21:03:19 -0500121 dgrp_t i;
122
Andreas Dilgerf628ace2008-03-31 10:50:19 -0400123 if (!fs->inode_map)
124 return EXT2_ET_NO_INODE_BITMAP;
125
Theodore Ts'o16b851c2008-04-20 23:33:34 -0400126 if (!EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
127 EXT4_FEATURE_RO_COMPAT_GDT_CSUM))
Andreas Dilgerf628ace2008-03-31 10:50:19 -0400128 return 0;
Jose R. Santosca2634a2007-10-21 21:03:19 -0500129
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400130 for (i = 0; i < fs->group_desc_count; i++) {
Theodore Ts'od32c9152011-07-07 13:50:22 -0400131 __u32 old_csum = ext2fs_bg_checksum(fs, i);
132 __u32 old_unused = ext2fs_bg_itable_unused(fs, i);
133 __u32 old_flags = ext2fs_bg_flags(fs, i);
134 __u32 old_free_inodes_count = ext2fs_bg_free_inodes_count(fs, i);
Jose R. Santosca2634a2007-10-21 21:03:19 -0500135
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400136 if (old_free_inodes_count == sb->s_inodes_per_group) {
137 ext2fs_bg_flags_set(fs, i, EXT2_BG_INODE_UNINIT);
138 ext2fs_bg_itable_unused_set(fs, i, sb->s_inodes_per_group);
Theodore Ts'o16b851c2008-04-20 23:33:34 -0400139 } else {
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400140 int unused =
141 sb->s_inodes_per_group -
Jose R. Santosca2634a2007-10-21 21:03:19 -0500142 find_last_inode_ingrp(fs->inode_map,
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400143 sb->s_inodes_per_group, i);
144
145 ext2fs_bg_flags_clear(fs, i, EXT2_BG_INODE_UNINIT);
146 ext2fs_bg_itable_unused_set(fs, i, unused);
Jose R. Santosca2634a2007-10-21 21:03:19 -0500147 }
148
Jose R. Santosca2634a2007-10-21 21:03:19 -0500149 ext2fs_group_desc_csum_set(fs, i);
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400150 if (old_flags != ext2fs_bg_flags(fs, i))
Andreas Dilger80fc4e62008-03-31 00:40:51 -0400151 dirty = 1;
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400152 if (old_unused != ext2fs_bg_itable_unused(fs, i))
Andreas Dilger80fc4e62008-03-31 00:40:51 -0400153 dirty = 1;
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400154 if (old_csum != ext2fs_bg_checksum(fs, i))
Jose R. Santosca2634a2007-10-21 21:03:19 -0500155 dirty = 1;
156 }
157 if (dirty)
158 ext2fs_mark_super_dirty(fs);
Andreas Dilgerf628ace2008-03-31 10:50:19 -0400159 return 0;
Jose R. Santosca2634a2007-10-21 21:03:19 -0500160}
Theodore Ts'o470e7372009-05-29 11:01:22 -0400161
162#ifdef DEBUG
Eric Sandeenc816ecb2010-12-14 13:00:01 -0600163#include "e2p/e2p.h"
164
Theodore Ts'o470e7372009-05-29 11:01:22 -0400165void print_csum(const char *msg, ext2_filsys fs, dgrp_t group)
166{
167 __u16 crc1, crc2, crc3;
168 dgrp_t swabgroup;
Andreas Dilgerf797cf32012-11-29 05:47:51 -0700169 struct ext2_group_desc *desc;
Theodore Ts'o1d18a552010-02-15 09:51:28 -0500170 size_t size;
Theodore Ts'o470e7372009-05-29 11:01:22 -0400171 struct ext2_super_block *sb = fs->super;
Theodore Ts'o1d18a552010-02-15 09:51:28 -0500172 int offset = offsetof(struct ext2_group_desc, bg_checksum);
Theodore Ts'o470e7372009-05-29 11:01:22 -0400173#ifdef WORDS_BIGENDIAN
Theodore Ts'o1d18a552010-02-15 09:51:28 -0500174 struct ext4_group_desc swabdesc;
175#endif
Theodore Ts'o470e7372009-05-29 11:01:22 -0400176
Andreas Dilgerf797cf32012-11-29 05:47:51 -0700177 desc = ext2fs_group_desc(fs, fs->group_desc, group);
Theodore Ts'o1d18a552010-02-15 09:51:28 -0500178 size = fs->super->s_desc_size;
179 if (size < EXT2_MIN_DESC_SIZE)
180 size = EXT2_MIN_DESC_SIZE;
181 if (size > sizeof(struct ext4_group_desc))
182 size = sizeof(struct ext4_group_desc);
183#ifdef WORDS_BIGENDIAN
Theodore Ts'o470e7372009-05-29 11:01:22 -0400184 /* Have to swab back to little-endian to do the checksum */
Theodore Ts'o1d18a552010-02-15 09:51:28 -0500185 memcpy(&swabdesc, desc, size);
186 ext2fs_swap_group_desc2(fs, (struct ext2_group_desc *) &swabdesc);
187 desc = (struct ext2_group_desc *) &swabdesc;
Theodore Ts'o470e7372009-05-29 11:01:22 -0400188
189 swabgroup = ext2fs_swab32(group);
190#else
191 swabgroup = group;
192#endif
193
194 crc1 = ext2fs_crc16(~0, sb->s_uuid, sizeof(fs->super->s_uuid));
195 crc2 = ext2fs_crc16(crc1, &swabgroup, sizeof(swabgroup));
Theodore Ts'o1d18a552010-02-15 09:51:28 -0500196 crc3 = ext2fs_crc16(crc2, desc, offset);
197 offset += sizeof(desc->bg_checksum); /* skip checksum */
198 /* for checksum of struct ext4_group_desc do the rest...*/
199 if (offset < size)
200 crc3 = ext2fs_crc16(crc3, (char *)desc + offset, size - offset);
201
Andreas Dilgerf797cf32012-11-29 05:47:51 -0700202 printf("%s UUID %s=%04x, grp %u=%04x: %04x=%04x\n",
Theodore Ts'o562f2642010-12-20 10:06:58 -0500203 msg, e2p_uuid2str(sb->s_uuid), crc1, group, crc2, crc3,
Eric Sandeenc816ecb2010-12-14 13:00:01 -0600204 ext2fs_group_desc_csum(fs, group));
Theodore Ts'o470e7372009-05-29 11:01:22 -0400205}
206
207unsigned char sb_uuid[16] = { 0x4f, 0x25, 0xe8, 0xcf, 0xe7, 0x97, 0x48, 0x23,
208 0xbe, 0xfa, 0xa7, 0x88, 0x4b, 0xae, 0xec, 0xdb };
209
210int main(int argc, char **argv)
211{
212 struct ext2_super_block param;
213 errcode_t retval;
214 ext2_filsys fs;
215 int i;
216 __u16 csum1, csum2, csum_known = 0xd3a4;
217
218 memset(&param, 0, sizeof(param));
Valerie Aurora Henson4efbac62009-09-07 20:46:34 -0400219 ext2fs_blocks_count_set(&param, 32768);
Theodore Ts'o470e7372009-05-29 11:01:22 -0400220
Valerie Aurora Henson8f82ef92009-08-05 00:27:10 -0400221 retval = ext2fs_initialize("test fs", EXT2_FLAG_64BITS, &param,
Theodore Ts'o470e7372009-05-29 11:01:22 -0400222 test_io_manager, &fs);
223 if (retval) {
224 com_err("setup", retval,
225 "While initializing filesystem");
226 exit(1);
227 }
228 memcpy(fs->super->s_uuid, sb_uuid, 16);
229 fs->super->s_feature_ro_compat = EXT4_FEATURE_RO_COMPAT_GDT_CSUM;
230
231 for (i=0; i < fs->group_desc_count; i++) {
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400232 ext2fs_block_bitmap_loc_set(fs, i, 124);
233 ext2fs_inode_bitmap_loc_set(fs, i, 125);
234 ext2fs_inode_table_loc_set(fs, i, 126);
235 ext2fs_bg_free_blocks_count_set(fs, i, 31119);
236 ext2fs_bg_free_inodes_count_set(fs, i, 15701);
237 ext2fs_bg_used_dirs_count_set(fs, i, 2);
Eric Sandeene633b582009-10-25 21:41:32 -0400238 ext2fs_bg_flags_zap(fs, i);
Theodore Ts'o470e7372009-05-29 11:01:22 -0400239 };
240
241 csum1 = ext2fs_group_desc_csum(fs, 0);
242 print_csum("csum0000", fs, 0);
243
244 if (csum1 != csum_known) {
245 printf("checksum for group 0 should be %04x\n", csum_known);
246 exit(1);
247 }
248 csum2 = ext2fs_group_desc_csum(fs, 1);
249 print_csum("csum0001", fs, 1);
250 if (csum1 == csum2) {
251 printf("checksums for different groups shouldn't match\n");
252 exit(1);
253 }
254 csum2 = ext2fs_group_desc_csum(fs, 2);
255 print_csum("csumffff", fs, 2);
256 if (csum1 == csum2) {
257 printf("checksums for different groups shouldn't match\n");
258 exit(1);
259 }
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400260 ext2fs_bg_checksum_set(fs, 0, csum1);
Theodore Ts'o470e7372009-05-29 11:01:22 -0400261 csum2 = ext2fs_group_desc_csum(fs, 0);
262 print_csum("csum_set", fs, 0);
263 if (csum1 != csum2) {
264 printf("checksums should not depend on checksum field\n");
265 exit(1);
266 }
267 if (!ext2fs_group_desc_csum_verify(fs, 0)) {
268 printf("checksums should verify against gd_checksum\n");
269 exit(1);
270 }
271 memset(fs->super->s_uuid, 0x30, sizeof(fs->super->s_uuid));
272 print_csum("new_uuid", fs, 0);
273 if (ext2fs_group_desc_csum_verify(fs, 0) != 0) {
274 printf("checksums for different filesystems shouldn't match\n");
275 exit(1);
276 }
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400277 csum1 = ext2fs_group_desc_csum(fs, 0);
278 ext2fs_bg_checksum_set(fs, 0, csum1);
Theodore Ts'o470e7372009-05-29 11:01:22 -0400279 print_csum("csum_new", fs, 0);
Valerie Aurora Hensond7cca6b2009-10-25 21:43:47 -0400280 ext2fs_bg_free_blocks_count_set(fs, 0, 1);
Theodore Ts'o470e7372009-05-29 11:01:22 -0400281 csum2 = ext2fs_group_desc_csum(fs, 0);
282 print_csum("csum_blk", fs, 0);
283 if (csum1 == csum2) {
284 printf("checksums for different data shouldn't match\n");
285 exit(1);
286 }
287
288 return 0;
289}
290#endif