csum.c revision cf828f1a72ec1eb0c1e819307137879447c909b7
1/*
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 Public
9 * License.
10 * %End-Header%
11 */
12
13#if HAVE_SYS_TYPES_H
14#include <sys/types.h>
15#endif
16
17#include "ext2_fs.h"
18#include "ext2fs.h"
19#include "crc16.h"
20#include <assert.h>
21
22#ifndef offsetof
23#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
24#endif
25
26#ifdef DEBUG
27#define STATIC
28#else
29#define STATIC static
30#endif
31
32STATIC __u16 ext2fs_group_desc_csum(ext2_filsys fs, dgrp_t group)
33{
34	__u16 crc = 0;
35	struct ext2_group_desc *desc;
36
37	desc = ext2fs_group_desc(fs, fs->group_desc, group);
38
39	if (fs->super->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
40		int offset = offsetof(struct ext2_group_desc, bg_checksum);
41
42#ifdef WORDS_BIGENDIAN
43		struct ext2_group_desc swabdesc = *desc;
44
45		/* Have to swab back to little-endian to do the checksum */
46		ext2fs_swap_group_desc2(fs, &swabdesc);
47		desc = &swabdesc;
48
49		group = ext2fs_swab32(group);
50#endif
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		/* 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	    (ext2fs_bg_checksum(fs, group) !=
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		return;
82
83	/* ext2fs_bg_checksum_set() sets the actual checksum field but
84	 * does not calculate the checksum itself. */
85	ext2fs_bg_checksum_set(fs, group, ext2fs_group_desc_csum(fs, group));
86}
87
88static __u32 find_last_inode_ingrp(ext2fs_inode_bitmap bitmap,
89				   __u32 inodes_per_grp, dgrp_t grp_no)
90{
91	ext2_ino_t i, start_ino, end_ino;
92
93	start_ino = grp_no * inodes_per_grp + 1;
94	end_ino = start_ino + inodes_per_grp - 1;
95
96	for (i = end_ino; i >= start_ino; i--) {
97		if (ext2fs_fast_test_inode_bitmap2(bitmap, i))
98			return i - start_ino + 1;
99	}
100	return inodes_per_grp;
101}
102
103/* update the bitmap flags, set the itable high watermark, and calculate
104 * checksums for the group descriptors */
105errcode_t ext2fs_set_gdt_csum(ext2_filsys fs)
106{
107	struct ext2_super_block *sb = fs->super;
108	int dirty = 0;
109	dgrp_t i;
110
111	if (!fs->inode_map)
112		return EXT2_ET_NO_INODE_BITMAP;
113
114	if (!EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
115					EXT4_FEATURE_RO_COMPAT_GDT_CSUM))
116		return 0;
117
118	for (i = 0; i < fs->group_desc_count; i++) {
119		unsigned int old_csum = ext2fs_bg_checksum(fs, i);
120		int old_unused = ext2fs_bg_itable_unused(fs, i);
121		unsigned int old_flags = ext2fs_bg_flags(fs, i);
122		int old_free_inodes_count = ext2fs_bg_free_inodes_count(fs, i);
123
124		if (old_free_inodes_count == sb->s_inodes_per_group) {
125			ext2fs_bg_flags_set(fs, i, EXT2_BG_INODE_UNINIT);
126			ext2fs_bg_itable_unused_set(fs, i, sb->s_inodes_per_group);
127		} else {
128			int unused =
129				sb->s_inodes_per_group -
130				find_last_inode_ingrp(fs->inode_map,
131						      sb->s_inodes_per_group, i);
132
133			ext2fs_bg_flags_clear(fs, i, EXT2_BG_INODE_UNINIT);
134			ext2fs_bg_itable_unused_set(fs, i, unused);
135		}
136
137		ext2fs_group_desc_csum_set(fs, i);
138		if (old_flags != ext2fs_bg_flags(fs, i))
139			dirty = 1;
140		if (old_unused != ext2fs_bg_itable_unused(fs, i))
141			dirty = 1;
142		if (old_csum != ext2fs_bg_checksum(fs, i))
143			dirty = 1;
144	}
145	if (dirty)
146		ext2fs_mark_super_dirty(fs);
147	return 0;
148}
149
150#ifdef DEBUG
151void print_csum(const char *msg, ext2_filsys fs, dgrp_t group)
152{
153	__u16 crc1, crc2, crc3;
154	dgrp_t swabgroup;
155 	struct ext2_group_desc *desc = ext2fs_group_desc(fs, fs->group_desc, group);
156	struct ext2_super_block *sb = fs->super;
157
158#ifdef WORDS_BIGENDIAN
159	struct ext2_group_desc swabdesc = fs->group_desc[group];
160
161	/* Have to swab back to little-endian to do the checksum */
162	ext2fs_swap_group_desc2(fs, fs->group_desc, &swabdesc);
163	desc = &swabdesc;
164
165	swabgroup = ext2fs_swab32(group);
166#else
167	swabgroup = group;
168#endif
169
170	crc1 = ext2fs_crc16(~0, sb->s_uuid, sizeof(fs->super->s_uuid));
171	crc2 = ext2fs_crc16(crc1, &swabgroup, sizeof(swabgroup));
172	crc3 = ext2fs_crc16(crc2, desc,
173			    offsetof(struct ext2_group_desc, bg_checksum));
174	printf("%s: UUID %016Lx%016Lx(%04x), grp %u(%04x): %04x=%04x\n",
175	       msg, *(long long *)&sb->s_uuid, *(long long *)&sb->s_uuid[8],
176	       crc1, group, crc2, crc3, ext2fs_group_desc_csum(fs, group));
177}
178
179unsigned char sb_uuid[16] = { 0x4f, 0x25, 0xe8, 0xcf, 0xe7, 0x97, 0x48, 0x23,
180			      0xbe, 0xfa, 0xa7, 0x88, 0x4b, 0xae, 0xec, 0xdb };
181
182int main(int argc, char **argv)
183{
184	struct ext2_super_block param;
185	errcode_t		retval;
186	ext2_filsys		fs;
187	int			i;
188	__u16 csum1, csum2, csum_known = 0xd3a4;
189
190	memset(&param, 0, sizeof(param));
191	ext2fs_blocks_count_set(&param, 32768);
192
193	retval = ext2fs_initialize("test fs", EXT2_FLAG_64BITS, &param,
194				   test_io_manager, &fs);
195	if (retval) {
196		com_err("setup", retval,
197			"While initializing filesystem");
198		exit(1);
199	}
200	memcpy(fs->super->s_uuid, sb_uuid, 16);
201	fs->super->s_feature_ro_compat = EXT4_FEATURE_RO_COMPAT_GDT_CSUM;
202
203	for (i=0; i < fs->group_desc_count; i++) {
204		ext2fs_block_bitmap_loc_set(fs, i, 124);
205		ext2fs_inode_bitmap_loc_set(fs, i, 125);
206		ext2fs_inode_table_loc_set(fs, i, 126);
207		ext2fs_bg_free_blocks_count_set(fs, i, 31119);
208		ext2fs_bg_free_inodes_count_set(fs, i, 15701);
209		ext2fs_bg_used_dirs_count_set(fs, i, 2);
210		ext2fs_bg_flags_zap(fs, i);
211	};
212
213	csum1 = ext2fs_group_desc_csum(fs, 0);
214	print_csum("csum0000", fs, 0);
215
216	if (csum1 != csum_known) {
217		printf("checksum for group 0 should be %04x\n", csum_known);
218		exit(1);
219	}
220	csum2 = ext2fs_group_desc_csum(fs, 1);
221	print_csum("csum0001", fs, 1);
222	if (csum1 == csum2) {
223		printf("checksums for different groups shouldn't match\n");
224		exit(1);
225	}
226	csum2 = ext2fs_group_desc_csum(fs, 2);
227	print_csum("csumffff", fs, 2);
228	if (csum1 == csum2) {
229		printf("checksums for different groups shouldn't match\n");
230		exit(1);
231	}
232	ext2fs_bg_checksum_set(fs, 0, csum1);
233	csum2 = ext2fs_group_desc_csum(fs, 0);
234	print_csum("csum_set", fs, 0);
235	if (csum1 != csum2) {
236		printf("checksums should not depend on checksum field\n");
237		exit(1);
238	}
239	if (!ext2fs_group_desc_csum_verify(fs, 0)) {
240		printf("checksums should verify against gd_checksum\n");
241		exit(1);
242	}
243	memset(fs->super->s_uuid, 0x30, sizeof(fs->super->s_uuid));
244	print_csum("new_uuid", fs, 0);
245	if (ext2fs_group_desc_csum_verify(fs, 0) != 0) {
246		printf("checksums for different filesystems shouldn't match\n");
247		exit(1);
248	}
249	csum1 = ext2fs_group_desc_csum(fs, 0);
250	ext2fs_bg_checksum_set(fs, 0, csum1);
251	print_csum("csum_new", fs, 0);
252	ext2fs_bg_free_blocks_count_set(fs, 0, 1);
253	csum2 = ext2fs_group_desc_csum(fs, 0);
254	print_csum("csum_blk", fs, 0);
255	if (csum1 == csum2) {
256		printf("checksums for different data shouldn't match\n");
257		exit(1);
258	}
259
260	return 0;
261}
262#endif
263