Whamcloud - gitweb
b=17569
[fs/lustre-release.git] / ldiskfs / kernel_patches / patches / ext3-handle-directory-corruption-better.patch
1 Date: Mon, 23 Oct 2006 15:45:05 -0500
2 From: Eric Sandeen <sandeen@redhat.com>
3 Subject: [PATCH RHEL5] - handle ext3 directory corruption better
4
5 This is for BZ 209907 <https://bugzilla.redhat.com/bugzilla/show_bug.cgi?id=209907>: kernel Soft lockup detected on corrupted ext3 filesystem.
6
7 This patch is now in -mm.
8
9 I've been using Steve Grubb's purely evil "fsfuzzer" tool, at
10 http://people.redhat.com/sgrubb/files/fsfuzzer-0.4.tar.gz
11
12 Basically it makes a filesystem, splats some random bits over it, then
13 tries to mount it and do some simple filesystem actions.
14
15 At best, the filesystem catches the corruption gracefully.  At worst,
16 things spin out of control.
17
18 As you might guess, we found a couple places in ext3 where things spin out
19 of control :)
20
21 First, we had a corrupted directory that was never checked for
22 consistency...  it was corrupt, and pointed to another bad "entry" of
23 length 0.  The for() loop looped forever, since the length of
24 ext3_next_entry(de) was 0, and we kept looking at the same pointer over and
25 over and over and over...  I modeled this check and subsequent action on
26 what is done for other directory types in ext3_readdir...
27
28 (adding this check adds some computational expense; I am testing a followup
29 patch to reduce the number of times we check and re-check these directory
30 entries, in all cases.  Thanks for the idea, Andreas).
31
32 Next we had a root directory inode which had a corrupted size, claimed to
33 be > 200M on a 4M filesystem.  There was only really 1 block in the
34 directory, but because the size was so large, readdir kept coming back for
35 more, spewing thousands of printk's along the way.
36
37 Per Andreas' suggestion, if we're in this read error condition and we're
38 trying to read an offset which is greater than i_blocks worth of bytes,
39 stop trying, and break out of the loop.
40
41 With these two changes fsfuzz test survives quite well on ext3.
42
43 Signed-off-by: Eric Sandeen <sandeen@redhat.com>
44 Cc: <linux-ext4@vger.kernel.org>
45 Signed-off-by: Andrew Morton <akpm@osdl.org>
46 ---
47
48  fs/ext3/dir.c   |    3 +++
49  fs/ext3/namei.c |    9 +++++++++
50  2 files changed, 12 insertions(+)
51
52 Index: linux-2.6.18-1.2732.el5/fs/ext3/dir.c
53 ===================================================================
54 --- linux-2.6.18-1.2732.el5.orig/fs/ext3/dir.c
55 +++ linux-2.6.18-1.2732.el5/fs/ext3/dir.c
56 @@ -151,6 +151,9 @@ static int ext3_readdir(struct file * fi
57                         ext3_error (sb, "ext3_readdir",
58                                 "directory #%lu contains a hole at offset %lu",
59                                 inode->i_ino, (unsigned long)filp->f_pos);
60 +                       /* corrupt size?  Maybe no more blocks to read */
61 +                       if (filp->f_pos > inode->i_blocks << 9)
62 +                               break;
63                         filp->f_pos += sb->s_blocksize - offset;
64                         continue;
65                 }
66 Index: linux-2.6.18-1.2732.el5/fs/ext3/namei.c
67 ===================================================================
68 --- linux-2.6.18-1.2732.el5.orig/fs/ext3/namei.c
69 +++ linux-2.6.18-1.2732.el5/fs/ext3/namei.c
70 @@ -551,6 +551,15 @@ static int htree_dirblock_to_tree(struct
71                                            dir->i_sb->s_blocksize -
72                                            EXT3_DIR_REC_LEN(0));
73         for (; de < top; de = ext3_next_entry(de)) {
74 +               if (!ext3_check_dir_entry("htree_dirblock_to_tree", dir, de, bh,
75 +                                       (block<<EXT3_BLOCK_SIZE_BITS(dir->i_sb))
76 +                                               +((char *)de - bh->b_data))) {
77 +                       /* On error, skip the f_pos to the next block. */
78 +                       dir_file->f_pos = (dir_file->f_pos |
79 +                                       (dir->i_sb->s_blocksize - 1)) + 1;
80 +                       brelse (bh);
81 +                       return count;
82 +               }
83                 ext3fs_dirhash(de->name, de->name_len, hinfo);
84                 if ((hinfo->hash < start_hash) ||
85                     ((hinfo->hash == start_hash) &&
86