1 Index: linux-stage/fs/ext3/extents.c
2 ===================================================================
3 --- linux-stage.orig/fs/ext3/extents.c 2007-04-17 22:09:19.000000000 -0700
4 +++ linux-stage/fs/ext3/extents.c 2007-04-17 22:12:05.000000000 -0700
6 #include <asm/uaccess.h>
9 -static inline int ext3_ext_check_header(struct ext3_extent_header *eh)
11 - if (eh->eh_magic != EXT3_EXT_MAGIC) {
12 - printk(KERN_ERR "EXT3-fs: invalid magic = 0x%x\n",
13 - (unsigned)eh->eh_magic);
16 - if (eh->eh_max == 0) {
17 - printk(KERN_ERR "EXT3-fs: invalid eh_max = %u\n",
18 - (unsigned)eh->eh_max);
21 - if (eh->eh_entries > eh->eh_max) {
22 - printk(KERN_ERR "EXT3-fs: invalid eh_entries = %u\n",
23 - (unsigned)eh->eh_entries);
25 +static int __ext3_ext_check_header(const char *function, struct inode *inode,
26 + struct ext3_extent_header *eh, int depth,
29 + const char *error_msg = NULL;
31 + if (unlikely(eh->eh_magic != EXT3_EXT_MAGIC)) {
32 + error_msg = "invalid magic";
35 + if (unlikely(eh->eh_depth != depth)) {
36 + error_msg = "unexpected eh_depth";
39 + if (unlikely(eh->eh_max == 0)) {
40 + error_msg = "too small eh_max";
43 + if (unlikely(eh->eh_max > max)) {
44 + error_msg = "too large eh_max";
47 + if (unlikely(eh->eh_entries > eh->eh_max)) {
48 + error_msg = "invalid eh_entries";
54 + ext3_error(inode->i_sb, function,
55 + "bad header in inode #%lu: %s - magic %x, "
56 + "entries %u, max %u(%u), depth %u(%u)",
57 + inode->i_ino, error_msg, eh->eh_magic,
58 + eh->eh_entries, eh->eh_max, max,
59 + eh->eh_depth, depth);
64 +#define ext3_ext_check_header(inode,eh,depth,max) \
65 + __ext3_ext_check_header(__FUNCTION__,inode,eh,depth,max)
68 static handle_t *ext3_ext_journal_restart(handle_t *handle, int needed)
76 +ext3_ext_max_entries(struct ext3_extents_tree *tree, int root, int depth)
82 + max = ext3_ext_space_root(tree);
84 + max = ext3_ext_space_root_idx(tree);
87 + max = ext3_ext_space_block(tree);
89 + max = ext3_ext_space_block_idx(tree);
95 static void ext3_ext_show_path(struct ext3_extents_tree *tree,
96 struct ext3_ext_path *path)
99 struct ext3_extent_idx *ix;
102 - EXT_ASSERT(eh->eh_magic == EXT3_EXT_MAGIC);
103 - EXT_ASSERT(eh->eh_entries <= eh->eh_max);
104 - EXT_ASSERT(eh->eh_entries > 0);
106 ext_debug(tree, "binsearch for %d(idx): ", block);
108 path->p_idx = ix = EXT_FIRST_INDEX(eh);
110 struct ext3_extent *ex;
113 - EXT_ASSERT(eh->eh_magic == EXT3_EXT_MAGIC);
114 - EXT_ASSERT(eh->eh_entries <= eh->eh_max);
116 if (eh->eh_entries == 0) {
118 * this leaf is empty yet:
120 struct ext3_extent_header *eh;
121 struct buffer_head *bh;
122 int depth, i, ppos = 0;
126 EXT_ASSERT(tree->inode);
127 @@ -443,17 +480,15 @@
129 eh = EXT_ROOT_HDR(tree);
131 - if (ext3_ext_check_header(eh)) {
132 + i = depth = EXT_DEPTH(tree);
133 + max = ext3_ext_max_entries(tree, 1, i);
134 + if (ext3_ext_check_header(tree->inode, eh, i, max)) {
135 /* don't free previously allocated path
136 * -- caller should take care */
141 - i = depth = EXT_DEPTH(tree);
142 - EXT_ASSERT(eh->eh_max);
143 - EXT_ASSERT(eh->eh_magic == EXT3_EXT_MAGIC);
145 /* account possible depth increase */
147 path = kmalloc(sizeof(struct ext3_ext_path) * (depth + 2),
149 path[ppos].p_hdr = eh;
152 - if (ext3_ext_check_header(eh))
153 + max = ext3_ext_max_entries(tree, 0, i);
154 + if (ext3_ext_check_header(tree->inode, eh, i, max))
159 path[ppos].p_depth = i;
161 path[ppos].p_ext = NULL;
162 path[ppos].p_idx = NULL;
164 - if (ext3_ext_check_header(eh))
168 ext3_ext_binsearch(tree, path + ppos, block);
170 @@ -1545,6 +1579,8 @@
171 ext_debug(tree, "remove [%lu:%lu] in leaf\n", start, end);
172 if (!path[depth].p_hdr)
173 path[depth].p_hdr = EXT_BLOCK_HDR(path[depth].p_bh);
175 + /* the header must be checked already in ext3_ext_remove_space() */
176 eh = path[depth].p_hdr;
178 EXT_ASSERT(eh->eh_entries <= eh->eh_max);
179 @@ -1707,7 +1743,7 @@
180 int depth = EXT_DEPTH(tree);
181 struct ext3_ext_path *path;
183 - int i = 0, err = 0;
184 + int i = 0, err = 0, max;
186 ext_debug(tree, "space to be removed: %lu:%lu\n", start, end);
188 @@ -1730,7 +1766,13 @@
190 memset(path, 0, sizeof(struct ext3_ext_path) * (depth + 1));
191 path[i].p_hdr = EXT_ROOT_HDR(tree);
194 + max = ext3_ext_max_entries(tree, 1, depth);
195 + if (ext3_ext_check_header(inode, path[i].p_hdr, depth, max)) {
200 while (i >= 0 && err == 0) {
202 /* this is leaf block */
203 @@ -1740,16 +1782,13 @@
209 /* this is index block */
210 if (!path[i].p_hdr) {
211 ext_debug(tree, "initialize header\n");
212 path[i].p_hdr = EXT_BLOCK_HDR(path[i].p_bh);
215 - EXT_ASSERT(path[i].p_hdr->eh_entries <= path[i].p_hdr->eh_max);
216 - EXT_ASSERT(path[i].p_hdr->eh_magic == EXT3_EXT_MAGIC);
218 if (!path[i].p_idx) {
219 /* this level hasn't touched yet */
221 @@ -1776,6 +1815,14 @@
225 + BUG_ON(i + 1 > depth);
226 + max = ext3_ext_max_entries(tree, 0, depth - i - 1);
227 + if (ext3_ext_check_header(inode,
228 + EXT_BLOCK_HDR(path[i+1].p_bh),
229 + depth - i - 1, max)) {
233 /* put actual number of indexes to know is this
234 * number got changed at the next iteration */
235 path[i].p_block = path[i].p_hdr->eh_entries;
236 @@ -1796,7 +1843,7 @@
239 /* TODO: flexible tree reduction should be here */
240 - if (path->p_hdr->eh_entries == 0) {
241 + if (err == 0 && path->p_hdr->eh_entries == 0) {
243 * truncate to zero freed all the tree
244 * so, we need to correct eh_depth
245 @@ -1810,6 +1857,7 @@
247 ext3_ext_tree_changed(tree);
251 ext3_journal_stop(handle);