3 fs/ext4/mballoc.c | 103 +++++++++++++++++++++++++++++++++++++++++++++++-------
4 fs/ext4/mballoc.h | 2 -
5 3 files changed, 93 insertions(+), 13 deletions(-)
9 @@ -3156,6 +3156,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 @@ -345,7 +345,7 @@ static const char * const ext4_groupinfo
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 @@ -769,7 +769,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 @@ -813,6 +813,7 @@ void ext4_mb_generate_buddy(struct super
39 ext4_mark_group_bitmap_corrupted(sb, group,
40 EXT4_GROUP_INFO_BBITMAP_CORRUPT);
43 mb_set_largest_free_order(sb, grp);
45 @@ -823,6 +824,8 @@ void ext4_mb_generate_buddy(struct super
46 sbi->s_mb_buddies_generated++;
47 sbi->s_mb_generation_time += period;
48 spin_unlock(&sbi->s_bal_lock);
53 static void mb_regenerate_buddy(struct ext4_buddy *e4b)
54 @@ -943,7 +946,7 @@ static int ext4_mb_init_cache(struct pag
57 first_block = page->index * blocks_per_page;
58 - for (i = 0; i < blocks_per_page; i++) {
59 + for (i = 0; i < blocks_per_page && err == 0; i++) {
60 group = (first_block + i) >> 1;
63 @@ -987,7 +990,7 @@ static int ext4_mb_init_cache(struct pag
64 ext4_lock_group(sb, group);
66 memset(data, 0xff, blocksize);
67 - ext4_mb_generate_buddy(sb, data, incore, group);
68 + err = ext4_mb_generate_buddy(sb, data, incore, group);
69 ext4_unlock_group(sb, group);
72 @@ -1002,7 +1005,7 @@ static int ext4_mb_init_cache(struct pag
73 memcpy(data, bitmap, blocksize);
75 /* mark all preallocated blks used in in-core bitmap */
76 - ext4_mb_generate_from_pa(sb, data, group);
77 + err = ext4_mb_generate_from_pa(sb, data, group);
78 ext4_mb_generate_from_freelist(sb, data, group);
79 ext4_unlock_group(sb, group);
81 @@ -1012,7 +1015,8 @@ static int ext4_mb_init_cache(struct pag
85 - SetPageUptodate(page);
86 + if (likely(err == 0))
87 + SetPageUptodate(page);
91 @@ -2396,9 +2400,11 @@ static void *ext4_mb_seq_groups_next(str
92 static int ext4_mb_seq_groups_show(struct seq_file *seq, void *v)
94 struct super_block *sb = PDE_DATA(file_inode(seq->file));
95 + struct ext4_group_desc *gdp;
96 ext4_group_t group = (ext4_group_t) ((unsigned long) v);
98 int err, buddy_loaded = 0;
100 struct ext4_buddy e4b;
101 struct ext4_group_info *grinfo;
102 unsigned char blocksize_bits = min_t(unsigned char,
103 @@ -2411,7 +2417,7 @@ static int ext4_mb_seq_groups_show(struc
107 - seq_puts(seq, "#group: free frags first ["
108 + seq_puts(seq, "#group: bfree gfree frags first pa ["
109 " 2^0 2^1 2^2 2^3 2^4 2^5 2^6 "
110 " 2^7 2^8 2^9 2^10 2^11 2^12 2^13 ]\n");
112 @@ -2429,13 +2435,19 @@ static int ext4_mb_seq_groups_show(struc
116 + gdp = ext4_get_group_desc(sb, group, NULL);
118 + free = ext4_free_group_clusters(sb, gdp);
120 memcpy(&sg, ext4_get_group_info(sb, group), i);
123 ext4_mb_unload_buddy(&e4b);
125 - seq_printf(seq, "#%-5u: %-5u %-5u %-5u [", group, sg.info.bb_free,
126 - sg.info.bb_fragments, sg.info.bb_first_free);
127 + seq_printf(seq, "#%-5lu: %-5u %-5u %-5u %-5u %-5lu [",
128 + (long unsigned int)group, sg.info.bb_free, free,
129 + sg.info.bb_fragments, sg.info.bb_first_free,
130 + sg.info.bb_prealloc_nr);
131 for (i = 0; i <= 13; i++)
132 seq_printf(seq, " %-5u", i <= blocksize_bits + 1 ?
133 sg.info.bb_counters[i] : 0);
134 @@ -3742,22 +3754,71 @@ static void ext4_mb_generate_from_freeli
138 + * check free blocks in bitmap match free block in group descriptor
139 + * do this before taking preallocated blocks into account to be able
140 + * to detect on-disk corruptions. The group lock should be hold by the
143 +int ext4_mb_check_ondisk_bitmap(struct super_block *sb, void *bitmap,
144 + struct ext4_group_desc *gdp, int group)
146 + unsigned short max = EXT4_CLUSTERS_PER_GROUP(sb);
147 + unsigned short i, first, free = 0;
148 + unsigned short free_in_gdp = ext4_free_group_clusters(sb, gdp);
150 + if (free_in_gdp == 0 && gdp->bg_flags & cpu_to_le16(EXT4_BG_BLOCK_UNINIT))
153 + i = mb_find_next_zero_bit(bitmap, max, 0);
157 + i = mb_find_next_bit(bitmap, max, i);
162 + i = mb_find_next_zero_bit(bitmap, max, i);
165 + if (free != free_in_gdp) {
166 + ext4_error(sb, "on-disk bitmap for group %d"
167 + "corrupted: %u blocks free in bitmap, %u - in gd\n",
168 + group, free, free_in_gdp);
175 * the function goes through all preallocation in this group and marks them
176 * used in in-core bitmap. buddy must be generated from this bitmap
177 * Need to be called with ext4 group lock held
179 static noinline_for_stack
180 -void ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
181 +int ext4_mb_generate_from_pa(struct super_block *sb, void *bitmap,
184 struct ext4_group_info *grp = ext4_get_group_info(sb, group);
185 struct ext4_prealloc_space *pa;
186 + struct ext4_group_desc *gdp;
187 struct list_head *cur;
188 ext4_group_t groupnr;
190 int preallocated = 0;
191 + int skip = 0, count = 0;
195 + gdp = ext4_get_group_desc(sb, group, NULL);
199 + /* before applying preallocations, check bitmap consistency */
200 + err = ext4_mb_check_ondisk_bitmap(sb, bitmap, gdp, group);
204 /* all form of preallocation discards first load group,
205 * so the only competing code is preallocation use.
206 * we don't need any locking here
207 @@ -3773,13 +3834,23 @@ void ext4_mb_generate_from_pa(struct sup
210 spin_unlock(&pa->pa_lock);
211 - if (unlikely(len == 0))
212 + if (unlikely(len == 0)) {
216 BUG_ON(groupnr != group);
217 ext4_set_bits(bitmap, start, len);
221 + if (count + skip != grp->bb_prealloc_nr) {
222 + ext4_error(sb, "lost preallocations: "
223 + "count %d, bb_prealloc_nr %lu, skip %d\n",
224 + count, grp->bb_prealloc_nr, skip);
227 mb_debug(sb, "preallocated %d for group %u\n", preallocated, group);
231 static void ext4_mb_pa_callback(struct rcu_head *head)
232 @@ -3843,6 +3914,7 @@ static void ext4_mb_put_pa(struct ext4_a
234 ext4_lock_group(sb, grp);
235 list_del(&pa->pa_group_list);
236 + ext4_get_group_info(sb, grp)->bb_prealloc_nr--;
237 ext4_unlock_group(sb, grp);
239 spin_lock(pa->pa_obj_lock);
240 @@ -3934,6 +4006,7 @@ ext4_mb_new_inode_pa(struct ext4_allocat
241 pa->pa_inode = ac->ac_inode;
243 list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
244 + grp->bb_prealloc_nr++;
246 spin_lock(pa->pa_obj_lock);
247 list_add_rcu(&pa->pa_inode_list, &ei->i_prealloc_list);
248 @@ -3988,6 +4061,7 @@ ext4_mb_new_group_pa(struct ext4_allocat
251 list_add(&pa->pa_group_list, &grp->bb_prealloc_list);
252 + grp->bb_prealloc_nr++;
255 * We will later add the new pa to the right bucket
256 @@ -4155,6 +4229,8 @@ repeat:
258 spin_unlock(&pa->pa_lock);
260 + BUG_ON(grp->bb_prealloc_nr == 0);
261 + grp->bb_prealloc_nr--;
262 list_del(&pa->pa_group_list);
263 list_add(&pa->u.pa_tmp_list, &list);
265 @@ -4291,7 +4367,7 @@ repeat:
267 ext4_error_err(sb, -err, "Error %d loading buddy information for %u",
273 bitmap_bh = ext4_read_block_bitmap(sb, group);
274 @@ -4304,6 +4380,8 @@ repeat:
277 ext4_lock_group(sb, group);
278 + BUG_ON(e4b.bd_info->bb_prealloc_nr == 0);
279 + e4b.bd_info->bb_prealloc_nr--;
280 list_del(&pa->pa_group_list);
281 ext4_mb_release_inode_pa(&e4b, bitmap_bh, pa);
282 ext4_unlock_group(sb, group);
283 @@ -4598,6 +4676,7 @@ ext4_mb_discard_lg_preallocations(struct
285 ext4_lock_group(sb, group);
286 list_del(&pa->pa_group_list);
287 + ext4_get_group_info(sb, group)->bb_prealloc_nr--;
288 ext4_mb_release_group_pa(&e4b, pa);
289 ext4_unlock_group(sb, group);
291 --- a/fs/ext4/mballoc.h
292 +++ b/fs/ext4/mballoc.h
295 * for which requests use 2^N search using buddies
297 -#define MB_DEFAULT_ORDER2_REQS 2
298 +#define MB_DEFAULT_ORDER2_REQS 8
301 * default group prealloc size 512 blocks