1 Index: linux-2.6.18-128.1.6/fs/ext4/mballoc.c
2 ===================================================================
3 --- linux-2.6.18-128.1.6.orig/fs/ext4/mballoc.c
4 +++ linux-2.6.18-128.1.6/fs/ext4/mballoc.c
5 @@ -660,7 +660,7 @@ static void ext4_mb_mark_free_simple(str
9 -static void ext4_mb_generate_buddy(struct super_block *sb,
10 +static int ext4_mb_generate_buddy(struct super_block *sb,
11 void *buddy, void *bitmap, ext4_group_t group)
13 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
14 @@ -692,14 +692,14 @@ static void ext4_mb_generate_buddy(struc
15 grp->bb_fragments = fragments;
17 if (free != grp->bb_free) {
18 - ext4_error(sb, __func__,
19 - "EXT4-fs: group %lu: %u blocks in bitmap, %u in gd\n",
20 - group, free, grp->bb_free);
22 - * If we intent to continue, we consider group descritor
23 - * corrupt and update bb_free using bitmap value
25 - grp->bb_free = free;
26 + struct ext4_group_desc *gdp;
27 + gdp = ext4_get_group_desc (sb, group, NULL);
28 + ext4_error(sb, __FUNCTION__,
29 + "group %lu: %u blocks in bitmap, %u in bb, "
30 + "%u in gd, %lu pa's\n", group, free, grp->bb_free,
31 + le16_to_cpu(gdp->bg_free_blocks_count),
32 + grp->bb_prealloc_nr);
36 clear_bit(EXT4_GROUP_INFO_NEED_INIT_BIT, &(grp->bb_state));
37 @@ -709,6 +709,8 @@ static void ext4_mb_generate_buddy(struc
38 EXT4_SB(sb)->s_mb_buddies_generated++;
39 EXT4_SB(sb)->s_mb_generation_time += period;
40 spin_unlock(&EXT4_SB(sb)->s_bal_lock);
45 /* The buddy information is attached the buddy cache inode
46 @@ -814,7 +816,7 @@ static int ext4_mb_init_cache(struct pag
49 first_block = page->index * blocks_per_page;
50 - for (i = 0; i < blocks_per_page; i++) {
51 + for (i = 0; i < blocks_per_page && err == 0; i++) {
53 struct ext4_group_info *grinfo;
55 @@ -848,7 +850,7 @@ static int ext4_mb_init_cache(struct pag
57 * incore got set to the group block bitmap below
59 - ext4_mb_generate_buddy(sb, data, incore, group);
60 + err = ext4_mb_generate_buddy(sb, data, incore, group);
63 /* this is block of bitmap */
64 @@ -861,7 +863,7 @@ static int ext4_mb_init_cache(struct pag
65 memcpy(data, bitmap, blocksize);
67 /* mark all preallocated blks used in in-core bitmap */
68 - ext4_mb_generate_from_pa(sb, data, group);
69 + err = ext4_mb_generate_from_pa(sb, data, group);
70 ext4_unlock_group(sb, group);
72 /* set incore so that the buddy information can be
73 @@ -870,6 +872,7 @@ static int ext4_mb_init_cache(struct pag
77 + if (likely(err == 0))
78 SetPageUptodate(page);
81 @@ -1964,7 +1967,10 @@ static int ext4_mb_seq_history_show(stru
82 hs->result.fe_start, hs->result.fe_len);
83 seq_printf(seq, "%-5u %-8u %-23s free\n",
84 hs->pid, hs->ino, buf2);
86 + seq_printf(seq, "unknown op %d\n", hs->op);
92 @@ -2092,9 +2098,11 @@ static void *ext4_mb_seq_groups_next(str
93 static int ext4_mb_seq_groups_show(struct seq_file *seq, void *v)
95 struct super_block *sb = seq->private;
96 + struct ext4_group_desc *gdp;
97 long group = (long) v;
101 struct ext4_buddy e4b;
103 struct ext4_group_info info;
104 @@ -2103,10 +2111,10 @@ static int ext4_mb_seq_groups_show(struc
108 - seq_printf(seq, "#%-5s: %-5s %-5s %-5s "
109 + seq_printf(seq, "#%-5s: %-5s %-5s %-5s %-5s %-5s"
110 "[ %-5s %-5s %-5s %-5s %-5s %-5s %-5s "
111 "%-5s %-5s %-5s %-5s %-5s %-5s %-5s ]\n",
112 - "group", "free", "frags", "first",
113 + "group", "free", "frags", "first", "first", "pa",
114 "2^0", "2^1", "2^2", "2^3", "2^4", "2^5", "2^6",
115 "2^7", "2^8", "2^9", "2^10", "2^11", "2^12", "2^13");
117 @@ -2117,13 +2125,20 @@ static int ext4_mb_seq_groups_show(struc
118 seq_printf(seq, "#%-5lu: I/O error\n", group);
122 + gdp = ext4_get_group_desc(sb, group, NULL);
124 + free = le16_to_cpu(gdp->bg_free_blocks_count);
126 ext4_lock_group(sb, group);
127 memcpy(&sg, ext4_get_group_info(sb, group), i);
128 ext4_unlock_group(sb, group);
129 ext4_mb_release_desc(&e4b);
131 - seq_printf(seq, "#%-5lu: %-5u %-5u %-5u [", group, sg.info.bb_free,
132 - sg.info.bb_fragments, sg.info.bb_first_free);
133 + seq_printf(seq, "#%-5lu: %-5u %-5u %-5u %-5u %-5lu [", group,
134 + sg.info.bb_free, free,
135 + sg.info.bb_fragments, sg.info.bb_first_free,
136 + sg.info.bb_prealloc_nr);
137 for (i = 0; i <= 13; i++)
138 seq_printf(seq, " %-5u", i <= sb->s_blocksize_bits + 1 ?
139 sg.info.bb_counters[i] : 0);
140 @@ -2226,6 +2241,7 @@ ext4_mb_store_history(struct ext4_alloca
141 h.tail = ac->ac_tail;
142 h.buddy = ac->ac_buddy;
144 + h.cr = ac->ac_criteria;
145 if (ac->ac_op == EXT4_MB_HISTORY_ALLOC) {
146 if (ac->ac_g_ex.fe_start == ac->ac_b_ex.fe_start &&
147 ac->ac_g_ex.fe_group == ac->ac_b_ex.fe_group)
148 @@ -3539,22 +3555,67 @@ ext4_mb_use_preallocated(struct ext4_all
152 + * check free blocks in bitmap match free block in group descriptor
153 + * do this before taking preallocated blocks into account to be able
154 + * to detect on-disk corruptions. The group lock should be hold by the
157 +int ext4_mb_check_ondisk_bitmap(struct super_block *sb, void *bitmap,
158 + struct ext4_group_desc *gdp, int group)
160 + unsigned short max = EXT4_BLOCKS_PER_GROUP(sb);
161 + unsigned short i, first, free = 0;
163 + i = mb_find_next_zero_bit(bitmap, max, 0);
167 + i = mb_find_next_bit(bitmap, max, i);
172 + i = mb_find_next_zero_bit(bitmap, max, i);
175 + if (free != le16_to_cpu(gdp->bg_free_blocks_count)) {
176 + ext4_error(sb, __FUNCTION__, "on-disk bitmap for group %d"
177 + "corrupted: %u blocks free in bitmap, %u - in gd\n",
178 + group, free, le16_to_cpu(gdp->bg_free_blocks_count));
185 * the function goes through all preallocation in this group and marks them
186 * used in in-core bitmap. buddy must be generated from this bitmap
187 * Need to be called with ext4 group lock (ext4_lock_group)
189 -static void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
190 +static int ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
193 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
194 struct ext4_prealloc_space *pa;
195 + struct ext4_group_desc *gdp;
196 struct list_head *cur;
197 ext4_group_t groupnr;
199 int preallocated = 0;
205 + gdp = ext4_get_group_desc (sb, group, NULL);
209 + /* before applying preallocations, check bitmap consistency */
210 + err = ext4_mb_check_ondisk_bitmap(sb, bitmap, gdp, group);
214 /* all form of preallocation discards first load group,
215 * so the only competing code is preallocation use.
216 * we don't need any locking here
217 @@ -3570,15 +3631,24 @@ static void ext4_mb_generate_from_pa(str
220 spin_unlock(&pa->pa_lock);
221 - if (unlikely(len == 0))
222 + if (unlikely(len == 0)) {
226 BUG_ON(groupnr != group);
227 mb_set_bits(sb_bgl_lock(EXT4_SB(sb), group),
232 + if (count + skip != grp->bb_prealloc_nr) {
233 + ext4_error(sb, __FUNCTION__, "lost preallocations: "
234 + "count %d, bb_prealloc_nr %lu, skip %d\n",
235 + count, grp->bb_prealloc_nr, skip);
238 mb_debug("prellocated %u for group %lu\n", preallocated, group);
242 static void ext4_mb_pa_callback(struct rcu_head *head)
243 @@ -3629,6 +3699,7 @@ static void ext4_mb_put_pa(struct ext4_a
245 ext4_lock_group(sb, grp);
246 list_del(&pa->pa_group_list);
247 + ext4_get_group_info(sb, grp)->bb_prealloc_nr--;
248 ext4_unlock_group(sb, grp);
250 spin_lock(pa->pa_obj_lock);
251 @@ -3717,6 +3788,7 @@ ext4_mb_new_inode_pa(struct ext4_allocat
253 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
254 list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
255 + grp->bb_prealloc_nr++;
256 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
258 spin_lock(pa->pa_obj_lock);
259 @@ -3776,6 +3848,7 @@ ext4_mb_new_group_pa(struct ext4_allocat
261 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
262 list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
263 + grp->bb_prealloc_nr++;
264 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
267 @@ -3828,6 +3901,7 @@ ext4_mb_release_inode_pa(struct ext4_bud
269 ac->ac_inode = pa->pa_inode;
270 ac->ac_op = EXT4_MB_HISTORY_DISCARD;
271 + ac->ac_o_ex.fe_len = 1;
275 @@ -3972,6 +4046,8 @@ repeat:
277 spin_unlock(&pa->pa_lock);
279 + BUG_ON(grp->bb_prealloc_nr == 0);
280 + grp->bb_prealloc_nr--;
281 list_del(&pa->pa_group_list);
282 list_add(&pa->u.pa_tmp_list, &list);
284 @@ -4107,7 +4183,7 @@ repeat:
286 ext4_error(sb, __func__, "Error in loading buddy "
287 "information for %lu\n", group);
292 bitmap_bh = ext4_read_block_bitmap(sb, group);
293 @@ -4119,6 +4195,8 @@ repeat:
296 ext4_lock_group(sb, group);
297 + BUG_ON(e4b.bd_info->bb_prealloc_nr == 0);
298 + e4b.bd_info->bb_prealloc_nr--;
299 list_del(&pa->pa_group_list);
300 ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa, ac);
301 ext4_unlock_group(sb, group);
302 Index: linux-2.6.18-128.1.6/fs/ext4/mballoc.h
303 ===================================================================
304 --- linux-2.6.18-128.1.6.orig/fs/ext4/mballoc.h
305 +++ linux-2.6.18-128.1.6/fs/ext4/mballoc.h
306 @@ -119,6 +119,7 @@ struct ext4_group_info {
307 unsigned short bb_free;
308 unsigned short bb_fragments;
309 struct list_head bb_prealloc_list;
310 + unsigned long bb_prealloc_nr;
314 @@ -228,7 +229,7 @@ struct ext4_mb_history {
315 __u16 tail; /* what tail broke some buddy */
316 __u16 buddy; /* buddy the tail ^^^ broke */
318 - __u8 cr:3; /* which phase the result extent was found at */
319 + __u8 cr:8; /* which phase the result extent was found at */
323 @@ -259,7 +260,7 @@ static void ext4_mb_store_history(struct
325 struct buffer_head *read_block_bitmap(struct super_block *, ext4_group_t);
327 -static void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
328 +static int ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
330 static void ext4_mb_poll_new_transaction(struct super_block *, handle_t *);
331 static void ext4_mb_free_committed_blocks(struct super_block *);