3 fs/ext4/mballoc.c | 109 ++++++++++++++++++++++++++++++++++++++++++++++--------
4 fs/ext4/mballoc.h | 2 -
5 3 files changed, 96 insertions(+), 16 deletions(-)
9 @@ -2098,6 +2098,7 @@ struct ext4_group_info {
10 ext4_grpblk_t bb_fragments; /* nr of freespace fragments */
11 ext4_grpblk_t bb_largest_free_order;/* order of largest frag in BG */
12 struct list_head bb_prealloc_list;
13 + unsigned long bb_prealloc_nr;
17 --- a/fs/ext4/mballoc.c
18 +++ b/fs/ext4/mballoc.c
19 @@ -352,7 +352,7 @@ static const char *ext4_groupinfo_slab_n
20 "ext4_groupinfo_64k", "ext4_groupinfo_128k"
23 -static void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
24 +static int ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
26 static void ext4_mb_generate_from_freelist(struct super_block *sb, void *bitmap,
28 @@ -702,7 +702,7 @@ mb_set_largest_free_order(struct super_b
31 static noinline_for_stack
32 -void ext4_mb_generate_buddy(struct super_block *sb,
33 +int ext4_mb_generate_buddy(struct super_block *sb,
34 void *buddy, void *bitmap, ext4_group_t group)
36 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
37 @@ -734,14 +734,19 @@ void ext4_mb_generate_buddy(struct super
38 grp->bb_fragments = fragments;
40 if (free != grp->bb_free) {
41 + struct ext4_group_desc *gdp;
42 + gdp = ext4_get_group_desc (sb, group, NULL);
43 ext4_grp_locked_error(sb, group, 0, 0,
44 - "%u blocks in bitmap, %u in gd",
45 - free, grp->bb_free);
46 + "%u blocks in bitmap, %u in bb, %u in gd",
48 + ext4_free_blks_count(sb, gdp));
51 * If we intent to continue, we consider group descritor
52 * corrupt and update bb_free using bitmap value
57 mb_set_largest_free_order(sb, grp);
59 @@ -752,6 +757,8 @@ void ext4_mb_generate_buddy(struct super
60 EXT4_SB(sb)->s_mb_buddies_generated++;
61 EXT4_SB(sb)->s_mb_generation_time += period;
62 spin_unlock(&EXT4_SB(sb)->s_bal_lock);
67 /* The buddy information is attached the buddy cache inode
68 @@ -898,7 +905,7 @@ static int ext4_mb_init_cache(struct pag
71 first_block = page->index * blocks_per_page;
72 - for (i = 0; i < blocks_per_page; i++) {
73 + for (i = 0; i < blocks_per_page && err == 0; i++) {
76 group = (first_block + i) >> 1;
77 @@ -939,7 +946,7 @@ static int ext4_mb_init_cache(struct pag
78 ext4_lock_group(sb, group);
80 memset(data, 0xff, blocksize);
81 - ext4_mb_generate_buddy(sb, data, incore, group);
82 + err = ext4_mb_generate_buddy(sb, data, incore, group);
83 ext4_unlock_group(sb, group);
86 @@ -954,7 +961,7 @@ static int ext4_mb_init_cache(struct pag
87 memcpy(data, bitmap, blocksize);
89 /* mark all preallocated blks used in in-core bitmap */
90 - ext4_mb_generate_from_pa(sb, data, group);
91 + err = ext4_mb_generate_from_pa(sb, data, group);
92 ext4_mb_generate_from_freelist(sb, data, group);
93 ext4_unlock_group(sb, group);
95 @@ -964,7 +971,8 @@ static int ext4_mb_init_cache(struct pag
99 - SetPageUptodate(page);
100 + if (likely(err == 0))
101 + SetPageUptodate(page);
105 @@ -2148,9 +2156,11 @@ static void *ext4_mb_seq_groups_next(str
106 static int ext4_mb_seq_groups_show(struct seq_file *seq, void *v)
108 struct super_block *sb = seq->private;
109 + struct ext4_group_desc *gdp;
110 ext4_group_t group = (ext4_group_t) ((unsigned long) v);
114 struct ext4_buddy e4b;
116 struct ext4_group_info info;
117 @@ -2159,10 +2169,10 @@ static int ext4_mb_seq_groups_show(struc
121 - seq_printf(seq, "#%-5s: %-5s %-5s %-5s "
122 + seq_printf(seq, "#%-5s: %-5s %-5s %-5s %-5s %-5s"
123 "[ %-5s %-5s %-5s %-5s %-5s %-5s %-5s "
124 "%-5s %-5s %-5s %-5s %-5s %-5s %-5s ]\n",
125 - "group", "free", "frags", "first",
126 + "group", "free", "free", "frags", "first", "pa",
127 "2^0", "2^1", "2^2", "2^3", "2^4", "2^5", "2^6",
128 "2^7", "2^8", "2^9", "2^10", "2^11", "2^12", "2^13");
130 @@ -2173,13 +2183,20 @@ static int ext4_mb_seq_groups_show(struc
131 seq_printf(seq, "#%-5u: I/O error\n", group);
135 + gdp = ext4_get_group_desc(sb, group, NULL);
137 + free = ext4_free_blks_count(sb, gdp);
139 ext4_lock_group(sb, group);
140 memcpy(&sg, ext4_get_group_info(sb, group), i);
141 ext4_unlock_group(sb, group);
142 ext4_mb_unload_buddy(&e4b);
144 - seq_printf(seq, "#%-5u: %-5u %-5u %-5u [", group, sg.info.bb_free,
145 - sg.info.bb_fragments, sg.info.bb_first_free);
146 + seq_printf(seq, "#%-5lu: %-5u %-5u %-5u %-5u %-5lu [",
147 + (long unsigned int)group, sg.info.bb_free, free,
148 + sg.info.bb_fragments, sg.info.bb_first_free,
149 + sg.info.bb_prealloc_nr);
150 for (i = 0; i <= 13; i++)
151 seq_printf(seq, " %-5u", i <= sb->s_blocksize_bits + 1 ?
152 sg.info.bb_counters[i] : 0);
153 @@ -3417,23 +3434,68 @@ static void ext4_mb_generate_from_freeli
157 + * check free blocks in bitmap match free block in group descriptor
158 + * do this before taking preallocated blocks into account to be able
159 + * to detect on-disk corruptions. The group lock should be hold by the
162 +int ext4_mb_check_ondisk_bitmap(struct super_block *sb, void *bitmap,
163 + struct ext4_group_desc *gdp, int group)
165 + unsigned short max = EXT4_BLOCKS_PER_GROUP(sb);
166 + unsigned short i, first, free = 0;
168 + i = mb_find_next_zero_bit(bitmap, max, 0);
172 + i = mb_find_next_bit(bitmap, max, i);
177 + i = mb_find_next_zero_bit(bitmap, max, i);
180 + if (free != ext4_free_blks_count(sb, gdp)) {
181 + ext4_error(sb, "on-disk bitmap for group %d"
182 + "corrupted: %u blocks free in bitmap, %u - in gd\n",
183 + group, free, ext4_free_blks_count(sb, gdp));
190 * the function goes through all preallocation in this group and marks them
191 * used in in-core bitmap. buddy must be generated from this bitmap
192 * Need to be called with ext4 group lock held
194 static noinline_for_stack
195 -void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
196 +int ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
199 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
200 struct ext4_prealloc_space *pa;
201 + struct ext4_group_desc *gdp;
202 struct list_head *cur;
203 ext4_group_t groupnr;
205 int preallocated = 0;
211 + gdp = ext4_get_group_desc (sb, group, NULL);
215 + /* before applying preallocations, check bitmap consistency */
216 + err = ext4_mb_check_ondisk_bitmap(sb, bitmap, gdp, group);
220 /* all form of preallocation discards first load group,
221 * so the only competing code is preallocation use.
222 * we don't need any locking here
223 @@ -3449,14 +3511,23 @@ void ext4_mb_generate_from_pa(struct sup
226 spin_unlock(&pa->pa_lock);
227 - if (unlikely(len == 0))
228 + if (unlikely(len == 0)) {
232 BUG_ON(groupnr != group);
233 mb_set_bits(bitmap, start, len);
237 + if (count + skip != grp->bb_prealloc_nr) {
238 + ext4_error(sb, "lost preallocations: "
239 + "count %d, bb_prealloc_nr %lu, skip %d\n",
240 + count, grp->bb_prealloc_nr, skip);
243 mb_debug(1, "prellocated %u for group %u\n", preallocated, group);
247 static void ext4_mb_pa_callback(struct rcu_head *head)
248 @@ -3515,6 +3586,7 @@ static void ext4_mb_put_pa(struct ext4_a
250 ext4_lock_group(sb, grp);
251 list_del(&pa->pa_group_list);
252 + ext4_get_group_info(sb, grp)->bb_prealloc_nr--;
253 ext4_unlock_group(sb, grp);
255 spin_lock(pa->pa_obj_lock);
256 @@ -3606,6 +3678,7 @@ ext4_mb_new_inode_pa(struct ext4_allocat
258 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
259 list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
260 + grp->bb_prealloc_nr++;
261 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
263 spin_lock(pa->pa_obj_lock);
264 @@ -3667,6 +3740,7 @@ ext4_mb_new_group_pa(struct ext4_allocat
266 ext4_lock_group(sb, ac->ac_b_ex.fe_group);
267 list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
268 + grp->bb_prealloc_nr++;
269 ext4_unlock_group(sb, ac->ac_b_ex.fe_group);
272 @@ -3835,6 +3909,8 @@ repeat:
274 spin_unlock(&pa->pa_lock);
276 + BUG_ON(grp->bb_prealloc_nr == 0);
277 + grp->bb_prealloc_nr--;
278 list_del(&pa->pa_group_list);
279 list_add(&pa->u.pa_tmp_list, &list);
281 @@ -3968,7 +4044,7 @@ repeat:
283 ext4_error(sb, "Error loading buddy information for %u",
289 bitmap_bh = ext4_read_block_bitmap(sb, group);
290 @@ -3980,6 +4056,8 @@ repeat:
293 ext4_lock_group(sb, group);
294 + BUG_ON(e4b.bd_info->bb_prealloc_nr == 0);
295 + e4b.bd_info->bb_prealloc_nr--;
296 list_del(&pa->pa_group_list);
297 ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa);
298 ext4_unlock_group(sb, group);
299 @@ -4240,6 +4318,7 @@ ext4_mb_discard_lg_preallocations(struct
301 ext4_lock_group(sb, group);
302 list_del(&pa->pa_group_list);
303 + ext4_get_group_info(sb, group)->bb_prealloc_nr--;
304 ext4_mb_release_group_pa(&e4b, pa);
305 ext4_unlock_group(sb, group);
307 --- a/fs/ext4/mballoc.h
308 +++ b/fs/ext4/mballoc.h
309 @@ -87,7 +87,7 @@ extern u8 mb_enable_debug;
311 * for which requests use 2^N search using buddies
313 -#define MB_DEFAULT_ORDER2_REQS 2
314 +#define MB_DEFAULT_ORDER2_REQS 8
317 * default group prealloc size 512 blocks