update to rc6, and verified pass fsx/dbench
[ext4-patch-queue.git] / ext4_uninit_blockgroup.patch
blobd41f89d71d20a69b77838dba28a17f9d59c956af
1 Signed-off-by: Avantika Mathur <mathur@us.ibm.com>
2 ---
3 diff -uprN linux-2.6.22-rc4/fs/ext4/balloc.c linux-2.6.22-rc4-patch/fs/ext4/balloc.c
4 --- linux-2.6.22-rc4/fs/ext4/balloc.c 2007-06-15 17:05:06.000000000 -0700
5 +++ linux-2.6.22-rc4-patch/fs/ext4/balloc.c 2007-06-15 16:53:58.000000000 -0700
6 @@ -20,6 +20,7 @@
7 #include <linux/quotaops.h>
8 #include <linux/buffer_head.h>
10 +#include "group.h"
12 * balloc.c contains the blocks allocation and deallocation routines
14 @@ -42,6 +43,75 @@ void ext4_get_group_no_and_offset(struct
18 +/* Initializes an uninitialized block bitmap if given, and returns the
19 + * number of blocks free in the group. */
20 +unsigned ext4_init_block_bitmap(struct super_block *sb, struct buffer_head *bh,
21 + int block_group, struct ext4_group_desc *gdp)
23 + unsigned long start;
24 + int bit, bit_max;
25 + unsigned free_blocks;
26 + struct ext4_sb_info *sbi = EXT4_SB(sb);
28 + if (bh) {
29 + J_ASSERT_BH(bh, buffer_locked(bh));
31 + /* If checksum is bad mark all blocks use to prevent allocation,
32 + * essentially implementing a per-group read-only flag. */
33 + if (!ext4_group_desc_csum_verify(sbi, block_group, gdp)) {
34 + ext4_error(sb, __FUNCTION__,
35 + "Checksum bad for group %u\n", block_group);
36 + gdp->bg_free_blocks_count = 0;
37 + gdp->bg_free_inodes_count = 0;
38 + gdp->bg_itable_unused = 0;
39 + memset(bh->b_data, 0xff, sb->s_blocksize);
40 + return 0;
41 + }
42 + memset(bh->b_data, 0, sb->s_blocksize);
43 + }
45 + /* Check for superblock and gdt backups in this group */
46 + bit_max = ext4_bg_has_super(sb, block_group);
48 + if (!EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_META_BG) ||
49 + block_group < le32_to_cpu(sbi->s_es->s_first_meta_bg) *
50 + sbi->s_desc_per_block) {
51 + if (bit_max) {
52 + bit_max += ext4_bg_num_gdb(sb, block_group);
53 + bit_max +=le16_to_cpu(sbi->s_es->s_reserved_gdt_blocks);
54 + }
55 + } else { /* For META_BG_BLOCK_GROUPS */
56 + int group_rel = (block_group -
57 + le32_to_cpu(sbi->s_es->s_first_meta_bg)) %
58 + EXT4_DESC_PER_BLOCK(sb);
59 + if (group_rel == 0 || group_rel == 1 ||
60 + (group_rel == EXT4_DESC_PER_BLOCK(sb) - 1))
61 + bit_max += 1;
62 + }
64 + /* Last and first groups are always initialized */
65 + free_blocks = EXT4_BLOCKS_PER_GROUP(sb) - bit_max;
67 + if (bh) {
68 + for (bit = 0; bit < bit_max; bit++)
69 + ext4_set_bit(bit, bh->b_data);
71 + start = block_group * EXT4_BLOCKS_PER_GROUP(sb) +
72 + le32_to_cpu(sbi->s_es->s_first_data_block);
74 + /* Set bits for block and inode bitmaps, and inode table */
75 + ext4_set_bit(le32_to_cpu(gdp->bg_block_bitmap) - start,
76 + bh->b_data);
77 + ext4_set_bit(le32_to_cpu(gdp->bg_inode_bitmap) - start,
78 + bh->b_data);
79 + for (bit = le32_to_cpu(gdp->bg_inode_table) - start,
80 + bit_max = bit + sbi->s_itb_per_group; bit < bit_max; bit++)
81 + ext4_set_bit(bit, bh->b_data);
82 + }
84 + return free_blocks - sbi->s_itb_per_group - 2;
88 * The free blocks are managed by bitmaps. A file system contains several
89 * blocks groups. Each group contains 1 bitmap block for blocks, 1 bitmap
90 @@ -110,16 +180,28 @@ struct ext4_group_desc * ext4_get_group_
92 * Return buffer_head on success or NULL in case of failure.
94 -static struct buffer_head *
95 +struct buffer_head *
96 read_block_bitmap(struct super_block *sb, unsigned int block_group)
98 struct ext4_group_desc * desc;
99 struct buffer_head * bh = NULL;
101 - desc = ext4_get_group_desc (sb, block_group, NULL);
102 + desc = ext4_get_group_desc(sb, block_group, NULL);
103 if (!desc)
104 goto error_out;
105 - bh = sb_bread(sb, ext4_block_bitmap(sb, desc));
106 + if (desc->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
107 + bh = sb_getblk(sb, le32_to_cpu(desc->bg_block_bitmap));
108 + if (!buffer_uptodate(bh)) {
109 + lock_buffer(bh);
110 + if (!buffer_uptodate(bh)) {
111 + ext4_init_block_bitmap(sb, bh,block_group,desc);
112 + set_buffer_uptodate(bh);
114 + unlock_buffer(bh);
116 + } else {
117 + bh = sb_bread(sb, le32_to_cpu(desc->bg_block_bitmap));
119 if (!bh)
120 ext4_error (sb, "read_block_bitmap",
121 "Cannot read block bitmap - "
122 @@ -586,6 +668,7 @@ do_more:
123 desc->bg_free_blocks_count =
124 cpu_to_le16(le16_to_cpu(desc->bg_free_blocks_count) +
125 group_freed);
126 + desc->bg_checksum = ext4_group_desc_csum(sbi, block_group, desc);
127 spin_unlock(sb_bgl_lock(sbi, block_group));
128 percpu_counter_mod(&sbi->s_freeblocks_counter, count);
130 @@ -1644,8 +1727,11 @@ allocated:
131 ret_block, goal_hits, goal_attempts);
133 spin_lock(sb_bgl_lock(sbi, group_no));
134 + if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))
135 + gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
136 gdp->bg_free_blocks_count =
137 cpu_to_le16(le16_to_cpu(gdp->bg_free_blocks_count)-num);
138 + gdp->bg_checksum = ext4_group_desc_csum(sbi, group_no, gdp);
139 spin_unlock(sb_bgl_lock(sbi, group_no));
140 percpu_counter_mod(&sbi->s_freeblocks_counter, -num);
142 diff -uprN linux-2.6.22-rc4/fs/ext4/group.h linux-2.6.22-rc4-patch/fs/ext4/group.h
143 --- linux-2.6.22-rc4/fs/ext4/group.h 1969-12-31 16:00:00.000000000 -0800
144 +++ linux-2.6.22-rc4-patch/fs/ext4/group.h 2007-06-15 16:53:58.000000000 -0700
145 @@ -0,0 +1,29 @@
147 + * linux/fs/ext4/group.h
149 + * Copyright (C) 2007 Cluster File Systems, Inc
151 + * Author: Andreas Dilger <adilger@clusterfs.com>
152 + */
154 +#ifndef _LINUX_EXT4_GROUP_H
155 +#define _LINUX_EXT4_GROUP_H
156 +#if defined(CONFIG_CRC16) || defined(CONFIG_CRC16_MODULE)
157 +#include <linux/crc16.h>
158 +#endif
160 +extern __le16 ext4_group_desc_csum(struct ext4_sb_info *sbi, __u32 group,
161 + struct ext4_group_desc *gdp);
162 +extern int ext4_group_desc_csum_verify(struct ext4_sb_info *sbi, __u32 group,
163 + struct ext4_group_desc *gdp);
164 +struct buffer_head *read_block_bitmap(struct super_block *sb,
165 + unsigned int block_group);
166 +extern unsigned ext4_init_block_bitmap(struct super_block *sb,
167 + struct buffer_head *bh, int group,
168 + struct ext4_group_desc *desc);
169 +#define ext4_free_blocks_after_init(sb, group, desc) \
170 + ext4_init_block_bitmap(sb, NULL, group, desc)
171 +extern unsigned ext4_init_inode_bitmap(struct super_block *sb,
172 + struct buffer_head *bh, int group,
173 + struct ext4_group_desc *desc);
174 +#endif /* _LINUX_EXT4_GROUP_H */
175 diff -uprN linux-2.6.22-rc4/fs/ext4/ialloc.c linux-2.6.22-rc4-patch/fs/ext4/ialloc.c
176 --- linux-2.6.22-rc4/fs/ext4/ialloc.c 2007-06-15 17:05:17.000000000 -0700
177 +++ linux-2.6.22-rc4-patch/fs/ext4/ialloc.c 2007-06-15 16:53:58.000000000 -0700
178 @@ -28,6 +28,7 @@
180 #include "xattr.h"
181 #include "acl.h"
182 +#include "group.h"
185 * ialloc.c contains the inodes allocation and deallocation routines
186 @@ -43,6 +44,52 @@
187 * the free blocks count in the block.
191 + * To avoid calling the atomic setbit hundreds or thousands of times, we only
192 + * need to use it within a single byte (to ensure we get endianness right).
193 + * We can use memset for the rest of the bitmap as there are no other users.
194 + */
195 +static void mark_bitmap_end(int start_bit, int end_bit, char *bitmap)
197 + int i;
199 + if (start_bit >= end_bit)
200 + return;
202 + ext4_debug("mark end bits +%d through +%d used\n", start_bit, end_bit);
203 + for (i = start_bit; i < ((start_bit + 7) & ~7UL); i++)
204 + ext4_set_bit(i, bitmap);
205 + if (i < end_bit)
206 + memset(bitmap + (i >> 3), 0xff, (end_bit - i) >> 3);
209 +/* Initializes an uninitialized inode bitmap */
210 +unsigned ext4_init_inode_bitmap(struct super_block *sb,
211 + struct buffer_head *bh, int block_group,
212 + struct ext4_group_desc *gdp)
214 + struct ext4_sb_info *sbi = EXT4_SB(sb);
216 + J_ASSERT_BH(bh, buffer_locked(bh));
218 + /* If checksum is bad mark all blocks and inodes use to prevent
219 + * allocation, essentially implementing a per-group read-only flag. */
220 + if (!ext4_group_desc_csum_verify(sbi, block_group, gdp)) {
221 + ext4_error(sb, __FUNCTION__, "Checksum bad for group %u\n",
222 + block_group);
223 + gdp->bg_free_blocks_count = 0;
224 + gdp->bg_free_inodes_count = 0;
225 + gdp->bg_itable_unused = 0;
226 + memset(bh->b_data, 0xff, sb->s_blocksize);
227 + return 0;
230 + memset(bh->b_data, 0, (EXT4_INODES_PER_GROUP(sb) + 7) / 8);
231 + mark_bitmap_end(EXT4_INODES_PER_GROUP(sb), EXT4_BLOCKS_PER_GROUP(sb),
232 + bh->b_data);
234 + return EXT4_INODES_PER_GROUP(sb);
238 * Read the inode allocation bitmap for a given block_group, reading
239 @@ -59,8 +106,19 @@ read_inode_bitmap(struct super_block * s
240 desc = ext4_get_group_desc(sb, block_group, NULL);
241 if (!desc)
242 goto error_out;
244 - bh = sb_bread(sb, ext4_inode_bitmap(sb, desc));
245 + if (desc->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
246 + bh = sb_getblk(sb, le32_to_cpu(desc->bg_inode_bitmap));
247 + if (!buffer_uptodate(bh)) {
248 + lock_buffer(bh);
249 + if (!buffer_uptodate(bh)) {
250 + ext4_init_inode_bitmap(sb, bh,block_group,desc);
251 + set_buffer_uptodate(bh);
253 + unlock_buffer(bh);
255 + } else {
256 + bh = sb_bread(sb, le32_to_cpu(desc->bg_inode_bitmap));
258 if (!bh)
259 ext4_error(sb, "read_inode_bitmap",
260 "Cannot read inode bitmap - "
261 @@ -169,6 +227,8 @@ void ext4_free_inode (handle_t *handle,
262 if (is_directory)
263 gdp->bg_used_dirs_count = cpu_to_le16(
264 le16_to_cpu(gdp->bg_used_dirs_count) - 1);
265 + gdp->bg_checksum = ext4_group_desc_csum(sbi,block_group,
266 + gdp);
267 spin_unlock(sb_bgl_lock(sbi, block_group));
268 percpu_counter_inc(&sbi->s_freeinodes_counter);
269 if (is_directory)
270 @@ -438,7 +498,7 @@ struct inode *ext4_new_inode(handle_t *h
271 struct ext4_sb_info *sbi;
272 int err = 0;
273 struct inode *ret;
274 - int i;
275 + int i, free = 0;
277 /* Cannot create files in a deleted directory */
278 if (!dir || !dir->i_nlink)
279 @@ -520,11 +580,13 @@ repeat_in_this_group:
280 goto out;
282 got:
283 - ino += group * EXT4_INODES_PER_GROUP(sb) + 1;
284 - if (ino < EXT4_FIRST_INO(sb) || ino > le32_to_cpu(es->s_inodes_count)) {
285 - ext4_error (sb, "ext4_new_inode",
286 - "reserved inode or inode > inodes count - "
287 - "block_group = %d, inode=%lu", group, ino);
288 + ino++;
289 + if ((group == 0 && ino < EXT4_FIRST_INO(sb)) ||
290 + ino > EXT4_INODES_PER_GROUP(sb)) {
291 + ext4_error(sb, __FUNCTION__,
292 + "reserved inode or inode > inodes count - "
293 + "block_group = %d, inode=%lu", group,
294 + ino + group * EXT4_INODES_PER_GROUP(sb));
295 err = -EIO;
296 goto fail;
298 @@ -532,13 +594,65 @@ got:
299 BUFFER_TRACE(bh2, "get_write_access");
300 err = ext4_journal_get_write_access(handle, bh2);
301 if (err) goto fail;
303 + /* We may have to initialize the block bitmap if it isn't already */
304 + if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM) &&
305 + gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
306 + struct buffer_head *block_bh = read_block_bitmap(sb, group);
308 + BUFFER_TRACE(block_bh, "get block bitmap access");
309 + err = ext4_journal_get_write_access(handle, block_bh);
310 + if (err) {
311 + brelse(block_bh);
312 + goto fail;
315 + free = 0;
316 + spin_lock(sb_bgl_lock(sbi, group));
317 + /* recheck and clear flag under lock if we still need to */
318 + if (gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT)) {
319 + gdp->bg_flags &= cpu_to_le16(~EXT4_BG_BLOCK_UNINIT);
320 + free = ext4_free_blocks_after_init(sb, group, gdp);
321 + gdp->bg_free_blocks_count = cpu_to_le16(free);
323 + spin_unlock(sb_bgl_lock(sbi, group));
325 + /* Don't need to dirty bitmap block if we didn't change it */
326 + if (free) {
327 + BUFFER_TRACE(block_bh, "dirty block bitmap");
328 + err = ext4_journal_dirty_metadata(handle, block_bh);
331 + brelse(block_bh);
332 + if (err)
333 + goto fail;
336 spin_lock(sb_bgl_lock(sbi, group));
337 + /* If we didn't allocate from within the initialized part of the inode
338 + * table then we need to initialize up to this inode. */
339 + if (EXT4_HAS_RO_COMPAT_FEATURE(sb, EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
340 + if (gdp->bg_flags & cpu_to_le16(EXT4_BG_INODE_UNINIT)) {
341 + gdp->bg_flags &= cpu_to_le16(~EXT4_BG_INODE_UNINIT);
342 + free = EXT4_INODES_PER_GROUP(sb);
343 + } else {
344 + free = EXT4_INODES_PER_GROUP(sb) -
345 + le16_to_cpu(gdp->bg_itable_unused);
348 + if (ino > free) {
349 + gdp->bg_itable_unused =
350 + cpu_to_le16(EXT4_INODES_PER_GROUP(sb) - ino);
354 gdp->bg_free_inodes_count =
355 cpu_to_le16(le16_to_cpu(gdp->bg_free_inodes_count) - 1);
356 if (S_ISDIR(mode)) {
357 gdp->bg_used_dirs_count =
358 cpu_to_le16(le16_to_cpu(gdp->bg_used_dirs_count) + 1);
360 + gdp->bg_checksum = ext4_group_desc_csum(sbi, group, gdp);
361 spin_unlock(sb_bgl_lock(sbi, group));
362 BUFFER_TRACE(bh2, "call ext4_journal_dirty_metadata");
363 err = ext4_journal_dirty_metadata(handle, bh2);
364 @@ -560,7 +674,7 @@ got:
365 inode->i_gid = current->fsgid;
366 inode->i_mode = mode;
368 - inode->i_ino = ino;
369 + inode->i_ino = ino + group * EXT4_INODES_PER_GROUP(sb);
370 /* This is the optimal IO size (for stat), not the fs block size */
371 inode->i_blocks = 0;
372 inode->i_mtime = inode->i_atime = inode->i_ctime = ei->i_crtime =
373 diff -uprN linux-2.6.22-rc4/fs/ext4/resize.c linux-2.6.22-rc4-patch/fs/ext4/resize.c
374 --- linux-2.6.22-rc4/fs/ext4/resize.c 2007-06-04 17:57:25.000000000 -0700
375 +++ linux-2.6.22-rc4-patch/fs/ext4/resize.c 2007-06-15 16:53:58.000000000 -0700
376 @@ -16,6 +16,7 @@
377 #include <linux/errno.h>
378 #include <linux/slab.h>
380 +#include "group.h"
382 #define outside(b, first, last) ((b) < (first) || (b) >= (last))
383 #define inside(b, first, last) ((b) >= (first) && (b) < (last))
384 @@ -842,6 +843,7 @@ int ext4_group_add(struct super_block *s
385 ext4_inode_table_set(sb, gdp, input->inode_table); /* LV FIXME */
386 gdp->bg_free_blocks_count = cpu_to_le16(input->free_blocks_count);
387 gdp->bg_free_inodes_count = cpu_to_le16(EXT4_INODES_PER_GROUP(sb));
388 + gdp->bg_checksum = ext4_group_desc_csum(sbi, input->group, gdp);
391 * Make the new blocks and inodes valid next. We do this before
392 diff -uprN linux-2.6.22-rc4/fs/ext4/super.c linux-2.6.22-rc4-patch/fs/ext4/super.c
393 --- linux-2.6.22-rc4/fs/ext4/super.c 2007-06-15 17:05:18.000000000 -0700
394 +++ linux-2.6.22-rc4-patch/fs/ext4/super.c 2007-06-15 16:53:58.000000000 -0700
395 @@ -41,6 +41,7 @@
396 #include "xattr.h"
397 #include "acl.h"
398 #include "namei.h"
399 +#include "group.h"
401 static int ext4_load_journal(struct super_block *, struct ext4_super_block *,
402 unsigned long journal_devnum);
403 @@ -1231,6 +1232,90 @@ static int ext4_setup_super(struct super
404 return res;
407 +#if !defined(CONFIG_CRC16) && !defined(CONFIG_CRC16_MODULE)
408 +/** CRC table for the CRC-16. The poly is 0x8005 (x16 + x15 + x2 + 1) */
409 +__u16 const crc16_table[256] = {
410 + 0x0000, 0xC0C1, 0xC181, 0x0140, 0xC301, 0x03C0, 0x0280, 0xC241,
411 + 0xC601, 0x06C0, 0x0780, 0xC741, 0x0500, 0xC5C1, 0xC481, 0x0440,
412 + 0xCC01, 0x0CC0, 0x0D80, 0xCD41, 0x0F00, 0xCFC1, 0xCE81, 0x0E40,
413 + 0x0A00, 0xCAC1, 0xCB81, 0x0B40, 0xC901, 0x09C0, 0x0880, 0xC841,
414 + 0xD801, 0x18C0, 0x1980, 0xD941, 0x1B00, 0xDBC1, 0xDA81, 0x1A40,
415 + 0x1E00, 0xDEC1, 0xDF81, 0x1F40, 0xDD01, 0x1DC0, 0x1C80, 0xDC41,
416 + 0x1400, 0xD4C1, 0xD581, 0x1540, 0xD701, 0x17C0, 0x1680, 0xD641,
417 + 0xD201, 0x12C0, 0x1380, 0xD341, 0x1100, 0xD1C1, 0xD081, 0x1040,
418 + 0xF001, 0x30C0, 0x3180, 0xF141, 0x3300, 0xF3C1, 0xF281, 0x3240,
419 + 0x3600, 0xF6C1, 0xF781, 0x3740, 0xF501, 0x35C0, 0x3480, 0xF441,
420 + 0x3C00, 0xFCC1, 0xFD81, 0x3D40, 0xFF01, 0x3FC0, 0x3E80, 0xFE41,
421 + 0xFA01, 0x3AC0, 0x3B80, 0xFB41, 0x3900, 0xF9C1, 0xF881, 0x3840,
422 + 0x2800, 0xE8C1, 0xE981, 0x2940, 0xEB01, 0x2BC0, 0x2A80, 0xEA41,
423 + 0xEE01, 0x2EC0, 0x2F80, 0xEF41, 0x2D00, 0xEDC1, 0xEC81, 0x2C40,
424 + 0xE401, 0x24C0, 0x2580, 0xE541, 0x2700, 0xE7C1, 0xE681, 0x2640,
425 + 0x2200, 0xE2C1, 0xE381, 0x2340, 0xE101, 0x21C0, 0x2080, 0xE041,
426 + 0xA001, 0x60C0, 0x6180, 0xA141, 0x6300, 0xA3C1, 0xA281, 0x6240,
427 + 0x6600, 0xA6C1, 0xA781, 0x6740, 0xA501, 0x65C0, 0x6480, 0xA441,
428 + 0x6C00, 0xACC1, 0xAD81, 0x6D40, 0xAF01, 0x6FC0, 0x6E80, 0xAE41,
429 + 0xAA01, 0x6AC0, 0x6B80, 0xAB41, 0x6900, 0xA9C1, 0xA881, 0x6840,
430 + 0x7800, 0xB8C1, 0xB981, 0x7940, 0xBB01, 0x7BC0, 0x7A80, 0xBA41,
431 + 0xBE01, 0x7EC0, 0x7F80, 0xBF41, 0x7D00, 0xBDC1, 0xBC81, 0x7C40,
432 + 0xB401, 0x74C0, 0x7580, 0xB541, 0x7700, 0xB7C1, 0xB681, 0x7640,
433 + 0x7200, 0xB2C1, 0xB381, 0x7340, 0xB101, 0x71C0, 0x7080, 0xB041,
434 + 0x5000, 0x90C1, 0x9181, 0x5140, 0x9301, 0x53C0, 0x5280, 0x9241,
435 + 0x9601, 0x56C0, 0x5780, 0x9741, 0x5500, 0x95C1, 0x9481, 0x5440,
436 + 0x9C01, 0x5CC0, 0x5D80, 0x9D41, 0x5F00, 0x9FC1, 0x9E81, 0x5E40,
437 + 0x5A00, 0x9AC1, 0x9B81, 0x5B40, 0x9901, 0x59C0, 0x5880, 0x9841,
438 + 0x8801, 0x48C0, 0x4980, 0x8941, 0x4B00, 0x8BC1, 0x8A81, 0x4A40,
439 + 0x4E00, 0x8EC1, 0x8F81, 0x4F40, 0x8D01, 0x4DC0, 0x4C80, 0x8C41,
440 + 0x4400, 0x84C1, 0x8581, 0x4540, 0x8701, 0x47C0, 0x4680, 0x8641,
441 + 0x8201, 0x42C0, 0x4380, 0x8341, 0x4100, 0x81C1, 0x8081, 0x4040
444 +static inline __u16 crc16_byte(__u16 crc, const __u8 data)
446 + return (crc >> 8) ^ crc16_table[(crc ^ data) & 0xff];
449 +__u16 crc16(__u16 crc, __u8 const *buffer, size_t len)
451 + while (len--)
452 + crc = crc16_byte(crc, *buffer++);
453 + return crc;
455 +#endif
457 +__le16 ext4_group_desc_csum(struct ext4_sb_info *sbi, __u32 block_group,
458 + struct ext4_group_desc *gdp)
460 + __u16 crc = 0;
462 + if (sbi->s_es->s_feature_ro_compat &
463 + cpu_to_le32(EXT4_FEATURE_RO_COMPAT_GDT_CSUM)) {
464 + int offset = offsetof(struct ext4_group_desc, bg_checksum);
465 + __le32 le_group = cpu_to_le32(block_group);
467 + crc = crc16(~0, sbi->s_es->s_uuid, sizeof(sbi->s_es->s_uuid));
468 + crc = crc16(crc, (__u8 *)&le_group, sizeof(le_group));
469 + crc = crc16(crc, (__u8 *)gdp, offset);
470 + offset += sizeof(gdp->bg_checksum); /* skip checksum */
471 + /*BUG_ON(offset != sizeof(*gdp)); /* XXX handle s_desc_size */
472 + /* for checksum of struct ext4_group_desc do the rest...*/
473 + if (offset < sbi->s_es->s_desc_size) {
474 + crc = crc16(crc, (__u8 *)gdp + offset,
475 + sbi->s_es->s_desc_size - offset);
479 + return cpu_to_le16(crc);
482 +int ext4_group_desc_csum_verify(struct ext4_sb_info *sbi, __u32 block_group,
483 + struct ext4_group_desc *gdp)
485 + if (gdp->bg_checksum != ext4_group_desc_csum(sbi, block_group, gdp))
486 + return 0;
488 + return 1;
491 /* Called at mount-time, super-block is locked */
492 static int ext4_check_descriptors (struct super_block * sb)
494 @@ -1285,6 +1370,13 @@ static int ext4_check_descriptors (struc
495 i, inode_table);
496 return 0;
498 + if (!ext4_group_desc_csum_verify(sbi, i, gdp)) {
499 + ext4_error(sb, __FUNCTION__,
500 + "Checksum for group %d failed (%u!=%u)\n", i,
501 + le16_to_cpu(ext4_group_desc_csum(sbi,i,gdp)),
502 + le16_to_cpu(gdp->bg_checksum));
503 + return 0;
505 first_block += EXT4_BLOCKS_PER_GROUP(sb);
506 gdp = (struct ext4_group_desc *)
507 ((__u8 *)gdp + EXT4_DESC_SIZE(sb));
508 diff -uprN linux-2.6.22-rc4/include/linux/ext4_fs.h linux-2.6.22-rc4-patch/include/linux/ext4_fs.h
509 --- linux-2.6.22-rc4/include/linux/ext4_fs.h 2007-06-15 17:05:24.000000000 -0700
510 +++ linux-2.6.22-rc4-patch/include/linux/ext4_fs.h 2007-06-15 16:57:27.000000000 -0700
511 @@ -123,19 +123,25 @@
513 struct ext4_group_desc
515 - __le32 bg_block_bitmap; /* Blocks bitmap block */
516 - __le32 bg_inode_bitmap; /* Inodes bitmap block */
517 + __le32 bg_block_bitmap; /* Blocks bitmap block */
518 + __le32 bg_inode_bitmap; /* Inodes bitmap block */
519 __le32 bg_inode_table; /* Inodes table block */
520 __le16 bg_free_blocks_count; /* Free blocks count */
521 __le16 bg_free_inodes_count; /* Free inodes count */
522 __le16 bg_used_dirs_count; /* Directories count */
523 __u16 bg_flags;
524 - __u32 bg_reserved[3];
525 + __u32 bg_reserved[2];
526 __le32 bg_block_bitmap_hi; /* Blocks bitmap block MSB */
527 __le32 bg_inode_bitmap_hi; /* Inodes bitmap block MSB */
528 __le32 bg_inode_table_hi; /* Inodes table block MSB */
529 + __le16 bg_itable_unused; /* Unused inodes count */
530 + __le16 bg_checksum; /* crc16(sb_uuid+group+desc) */
533 +#define EXT4_BG_INODE_UNINIT 0x0001 /* Inode table/bitmap not in use */
534 +#define EXT4_BG_BLOCK_UNINIT 0x0002 /* Block bitmap not in use */
535 +#define EXT4_BG_INODE_ZEROED 0x0004 /* On-disk itable initialized to zero */
537 #ifdef __KERNEL__
538 #include <linux/ext4_fs_i.h>
539 #include <linux/ext4_fs_sb.h>
540 @@ -681,6 +687,7 @@ static inline int ext4_valid_inum(struct
541 #define EXT4_FEATURE_COMPAT_EXT_ATTR 0x0008
542 #define EXT4_FEATURE_COMPAT_RESIZE_INODE 0x0010
543 #define EXT4_FEATURE_COMPAT_DIR_INDEX 0x0020
544 +#define EXT4_FEATURE_RO_COMPAT_GDT_CSUM 0x0040
546 #define EXT4_FEATURE_RO_COMPAT_SPARSE_SUPER 0x0001
547 #define EXT4_FEATURE_RO_COMPAT_LARGE_FILE 0x0002
548 @@ -706,6 +713,7 @@ static inline int ext4_valid_inum(struct
549 EXT4_FEATURE_RO_COMPAT_LARGE_FILE| \
550 EXT4_FEATURE_RO_COMPAT_DIR_NLINK | \
551 EXT4_FEATURE_RO_COMPAT_EXTRA_ISIZE | \
552 + EXT4_FEATURE_RO_COMPAT_GDT_CSUM| \
553 EXT4_FEATURE_RO_COMPAT_BTREE_DIR)