csum.c revision f628acea2671dda839fc086f1017718e41e34eca
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#include "ext2_fs.h"
13#include "ext2fs.h"
14#include "crc16.h"
15#include <assert.h>
16
17#ifndef offsetof
18#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)
19#endif
20
21#ifdef DEBUG
22#define STATIC
23#else
24#define STATIC static
25#endif
26
27STATIC __u16 ext2fs_group_desc_csum(ext2_filsys fs, dgrp_t group)
28{
29	__u16 crc = 0;
30	struct ext2_group_desc *desc;
31
32	desc = &fs->group_desc[group];
33
34	if (fs->super->s_feature_ro_compat & EXT4_FEATURE_RO_COMPAT_GDT_CSUM) {
35		int offset = offsetof(struct ext2_group_desc, bg_checksum);
36
37#ifdef WORDS_BIGENDIAN
38		struct ext2_group_desc swabdesc = *desc;
39
40		/* Have to swab back to little-endian to do the checksum */
41		ext2fs_swap_group_desc(&swabdesc);
42		desc = &swabdesc;
43
44		group = ext2fs_swab32(group);
45#endif
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);
55		}
56	}
57
58	return crc;
59}
60
61int ext2fs_group_desc_csum_verify(ext2_filsys fs, dgrp_t group)
62{
63	if (fs->group_desc[group].bg_checksum !=
64	    ext2fs_group_desc_csum(fs, group))
65		return 0;
66
67	return 1;
68}
69
70void ext2fs_group_desc_csum_set(ext2_filsys fs, dgrp_t group)
71{
72	fs->group_desc[group].bg_checksum = ext2fs_group_desc_csum(fs, group);
73}
74
75static __u32 find_last_inode_ingrp(ext2fs_inode_bitmap bitmap,
76				   __u32 inodes_per_grp, dgrp_t grp_no)
77{
78	ext2_ino_t i, start_ino, end_ino;
79
80	start_ino = grp_no * inodes_per_grp + 1;
81	end_ino = start_ino + inodes_per_grp - 1;
82
83	for (i = end_ino; i >= start_ino; i--) {
84		if (ext2fs_fast_test_inode_bitmap(bitmap, i))
85			return i - start_ino + 1;
86	}
87	return inodes_per_grp;
88}
89
90/* update the bitmap flags, set the itable high watermark, and calculate
91 * checksums for the group descriptors */
92errcode_t ext2fs_set_gdt_csum(ext2_filsys fs)
93{
94	struct ext2_super_block *sb = fs->super;
95	struct ext2_group_desc *bg = fs->group_desc;
96	int blks, csum_flag, dirty = 0;
97	dgrp_t i;
98
99	if (!fs->inode_map)
100		return EXT2_ET_NO_INODE_BITMAP;
101
102	csum_flag = EXT2_HAS_RO_COMPAT_FEATURE(fs->super,
103					       EXT4_FEATURE_RO_COMPAT_GDT_CSUM);
104	if (!EXT2_HAS_COMPAT_FEATURE(fs->super,
105				     EXT2_FEATURE_COMPAT_LAZY_BG) && !csum_flag)
106		return 0;
107
108	for (i = 0; i < fs->group_desc_count; i++, bg++) {
109		int old_csum = bg->bg_checksum;
110		int old_unused = bg->bg_itable_unused;
111		int old_flags = bg->bg_flags;
112
113		/* Even if it wasn't zeroed, by the time this function is
114		 * called by e2fsck we have already scanned and corrected
115		 * the whole inode table so we may as well not overwrite it.
116		 * This is just a hint to the kernel that it could do lazy
117		 * zeroing of the inode table if mke2fs didn't do it, to help
118		 * out if we need to do a full itable scan sometime later. */
119		if (!(bg->bg_flags & (EXT2_BG_INODE_UNINIT |
120				      EXT2_BG_INODE_ZEROED)))
121			fs->group_desc[i].bg_flags |= EXT2_BG_INODE_ZEROED;
122
123		if (bg->bg_free_inodes_count == sb->s_inodes_per_group &&
124		    i > 0 && (i < fs->group_desc_count - 1 || csum_flag)) {
125			if (!(bg->bg_flags & EXT2_BG_INODE_UNINIT))
126				bg->bg_flags |= EXT2_BG_INODE_UNINIT;
127
128			if (csum_flag)
129				bg->bg_itable_unused = sb->s_inodes_per_group;
130		} else if (csum_flag) {
131			bg->bg_flags &= ~EXT2_BG_INODE_UNINIT;
132			bg->bg_itable_unused = sb->s_inodes_per_group -
133				find_last_inode_ingrp(fs->inode_map,
134						      sb->s_inodes_per_group,i);
135		}
136
137		/* skip first and last groups, or groups with GDT backups
138		 * because the resize inode has blocks allocated in them. */
139		if (i == 0 || i == fs->group_desc_count - 1 ||
140		    (ext2fs_bg_has_super(fs, i) && sb->s_reserved_gdt_blocks))
141			goto checksum;
142
143		blks = ext2fs_super_and_bgd_loc(fs, i, 0, 0, 0, 0);
144		if (bg->bg_free_blocks_count == blks &&
145		    bg->bg_flags & EXT2_BG_INODE_UNINIT &&
146		    !(bg->bg_flags & EXT2_BG_BLOCK_UNINIT))
147			bg->bg_flags |= EXT2_BG_BLOCK_UNINIT;
148checksum:
149		ext2fs_group_desc_csum_set(fs, i);
150		if (old_flags != bg->bg_flags)
151			dirty = 1;
152		if (old_unused != bg->bg_itable_unused)
153			dirty = 1;
154		if (old_csum != bg->bg_checksum)
155			dirty = 1;
156	}
157	if (dirty)
158		ext2fs_mark_super_dirty(fs);
159	return 0;
160}
161