More patch description fixups. Standardize case.
[ext4-patch-queue.git] / ext4_ialloc-flexbg.patch
blob5b6b10cd66bbe19c41dabcc6f8e3c1ce51fa7bab
1 ext4: New inode allocation for FLEX_BG meta-data groups.
3 This patch mostly controls the way inode are allocated in order to
4 make ialloc aware of flex_bg block group grouping. It achieves this
5 by bypassing the Orlov allocator when block group meta-data are packed
6 toghether through mke2fs. Since the impact on the block allocator is
7 minimal, this patch should have little or no effect on other block
8 allocation algorithms. By controlling the inode allocation, it can
9 basically control where the initial search for new block begins and
10 thus indirectly manipulate the block allocator.
12 This allocator favors data and meta-data locality so the disk will
13 gradually be filled from block group zero upward. This helps improve
14 performance by reducing seek time. Since the group of inode tables
15 within one flex_bg are treated as one giant inode table, uninitialized
16 block groups would not need to partially initialize as many inode
17 table as with Orlov which would help fsck time as the filesystem usage
18 goes up.
20 Signed-off-by: Jose R. Santos <jrs@us.ibm.com>
21 Signed-off-by: Valerie Clement <valerie.clement@bull.net>
23 ---
24 fs/ext4/balloc.c | 16 +++++++
25 fs/ext4/ialloc.c | 96 +++++++++++++++++++++++++++++++++++++++++++++
26 fs/ext4/super.c | 57 ++++++++++++++++++++++++++
27 include/linux/ext4_fs.h | 25 +++++++++++
28 include/linux/ext4_fs_sb.h | 3 +
29 5 files changed, 196 insertions(+), 1 deletion(-)
31 Index: linux-2.6.24-rc7/fs/ext4/balloc.c
32 ===================================================================
33 --- linux-2.6.24-rc7.orig/fs/ext4/balloc.c 2008-01-16 13:52:19.000000000 -0800
34 +++ linux-2.6.24-rc7/fs/ext4/balloc.c 2008-01-16 13:52:51.000000000 -0800
35 @@ -127,6 +127,8 @@ unsigned ext4_init_block_bitmap(struct s
36 mark_bitmap_end(group_blocks, sb->s_blocksize * 8, bh->b_data);
39 + if (sbi->s_log_groups_per_flex)
40 + return free_blocks;
41 return free_blocks - sbi->s_itb_per_group - 2;
44 @@ -759,6 +761,13 @@ do_more:
45 spin_unlock(sb_bgl_lock(sbi, block_group));
46 percpu_counter_add(&sbi->s_freeblocks_counter, count);
48 + if (sbi->s_log_groups_per_flex) {
49 + ext4_group_t flex_group = ext4_flex_group(sbi, block_group);
50 + spin_lock(sb_bgl_lock(sbi, flex_group));
51 + sbi->s_flex_groups[flex_group].free_blocks += count;
52 + spin_unlock(sb_bgl_lock(sbi, flex_group));
53 + }
55 /* We dirtied the bitmap block */
56 BUFFER_TRACE(bitmap_bh, "dirtied bitmap block");
57 err = ext4_journal_dirty_metadata(handle, bitmap_bh);
58 @@ -1829,6 +1838,13 @@ allocated:
59 spin_unlock(sb_bgl_lock(sbi, group_no));
60 percpu_counter_sub(&sbi->s_freeblocks_counter, num);
62 + if (sbi->s_log_groups_per_flex) {
63 + ext4_group_t flex_group = ext4_flex_group(sbi, group_no);
64 + spin_lock(sb_bgl_lock(sbi, flex_group));
65 + sbi->s_flex_groups[flex_group].free_blocks -= num;
66 + spin_unlock(sb_bgl_lock(sbi, flex_group));
67 + }
69 BUFFER_TRACE(gdp_bh, "journal_dirty_metadata for group descriptor");
70 err = ext4_journal_dirty_metadata(handle, gdp_bh);
71 if (!fatal)
72 Index: linux-2.6.24-rc7/fs/ext4/ialloc.c
73 ===================================================================
74 --- linux-2.6.24-rc7.orig/fs/ext4/ialloc.c 2008-01-16 13:52:16.000000000 -0800
75 +++ linux-2.6.24-rc7/fs/ext4/ialloc.c 2008-01-16 13:52:51.000000000 -0800
76 @@ -158,6 +158,7 @@ void ext4_free_inode (handle_t *handle,
77 struct ext4_super_block * es;
78 struct ext4_sb_info *sbi;
79 int fatal = 0, err;
80 + ext4_group_t flex_group;
82 if (atomic_read(&inode->i_count) > 1) {
83 printk ("ext4_free_inode: inode has count=%d\n",
84 @@ -235,6 +236,12 @@ void ext4_free_inode (handle_t *handle,
85 if (is_directory)
86 percpu_counter_dec(&sbi->s_dirs_counter);
88 + if (sbi->s_log_groups_per_flex) {
89 + flex_group = ext4_flex_group(sbi, block_group);
90 + spin_lock(sb_bgl_lock(sbi, flex_group));
91 + sbi->s_flex_groups[flex_group].free_inodes++;
92 + spin_unlock(sb_bgl_lock(sbi, flex_group));
93 + }
95 BUFFER_TRACE(bh2, "call ext4_journal_dirty_metadata");
96 err = ext4_journal_dirty_metadata(handle, bh2);
97 @@ -289,6 +296,80 @@ static int find_group_dir(struct super_b
98 return ret;
101 +#define free_block_ratio 10
103 +static int find_group_flex(struct super_block *sb, struct inode *parent,
104 + ext4_group_t *best_group)
106 + struct ext4_sb_info *sbi = EXT4_SB(sb);
107 + struct ext4_group_desc *desc;
108 + struct buffer_head *bh;
109 + struct flex_groups *flex_group = sbi->s_flex_groups;
110 + ext4_group_t parent_group = EXT4_I(parent)->i_block_group;
111 + ext4_group_t parent_fbg_group = ext4_flex_group(sbi, parent_group);
112 + ext4_group_t ngroups = sbi->s_groups_count;
113 + int flex_size = ext4_flex_bg_size(sbi);
114 + ext4_group_t best_flex = parent_fbg_group;
115 + int blocks_per_flex = sbi->s_blocks_per_group * flex_size;
116 + int flexbg_free_blocks;
117 + int flex_freeb_ratio;
118 + ext4_group_t n_fbg_groups;
119 + ext4_group_t i;
121 + n_fbg_groups = (sbi->s_groups_count + flex_size - 1) >>
122 + sbi->s_log_groups_per_flex;
124 +find_close_to_parent:
125 + flexbg_free_blocks = flex_group[best_flex].free_blocks;
126 + flex_freeb_ratio = flexbg_free_blocks * 100 / blocks_per_flex;
127 + if (flex_group[best_flex].free_inodes &&
128 + flex_freeb_ratio > free_block_ratio)
129 + goto found_flexbg;
131 + if (best_flex && best_flex == parent_fbg_group) {
132 + best_flex--;
133 + goto find_close_to_parent;
136 + for (i = 0; i < n_fbg_groups; i++) {
137 + if (i == parent_fbg_group || i == parent_fbg_group - 1)
138 + continue;
140 + flexbg_free_blocks = flex_group[best_flex].free_blocks;
141 + flex_freeb_ratio = flexbg_free_blocks * 100 / blocks_per_flex;
143 + if (flex_freeb_ratio > free_block_ratio &&
144 + flex_group[i].free_inodes) {
145 + best_flex = i;
146 + goto found_flexbg;
149 + if (best_flex < 0 ||
150 + (flex_group[i].free_blocks >
151 + flex_group[best_flex].free_blocks &&
152 + flex_group[i].free_inodes))
153 + best_flex = i;
156 + if (!flex_group[best_flex].free_inodes ||
157 + !flex_group[best_flex].free_blocks)
158 + return -1;
160 +found_flexbg:
161 + for (i = best_flex * flex_size; i < ngroups &&
162 + i < (best_flex + 1) * flex_size; i++) {
163 + desc = ext4_get_group_desc(sb, i, &bh);
164 + if (le16_to_cpu(desc->bg_free_inodes_count)) {
165 + *best_group = i;
166 + goto out;
170 + return -1;
171 +out:
172 + return 0;
176 * Orlov's allocator for directories.
178 @@ -504,6 +585,7 @@ struct inode *ext4_new_inode(handle_t *h
179 struct inode *ret;
180 ext4_group_t i;
181 int free = 0;
182 + ext4_group_t flex_group;
184 /* Cannot create files in a deleted directory */
185 if (!dir || !dir->i_nlink)
186 @@ -517,6 +599,12 @@ struct inode *ext4_new_inode(handle_t *h
188 sbi = EXT4_SB(sb);
189 es = sbi->s_es;
191 + if (sbi->s_log_groups_per_flex) {
192 + ret2 = find_group_flex(sb, dir, &group);
193 + goto got_group;
196 if (S_ISDIR(mode)) {
197 if (test_opt (sb, OLDALLOC))
198 ret2 = find_group_dir(sb, dir, &group);
199 @@ -525,6 +613,7 @@ struct inode *ext4_new_inode(handle_t *h
200 } else
201 ret2 = find_group_other(sb, dir, &group);
203 +got_group:
204 err = -ENOSPC;
205 if (ret2 == -1)
206 goto out;
207 @@ -681,6 +770,13 @@ got:
208 percpu_counter_inc(&sbi->s_dirs_counter);
209 sb->s_dirt = 1;
211 + if (sbi->s_log_groups_per_flex) {
212 + flex_group = ext4_flex_group(sbi, group);
213 + spin_lock(sb_bgl_lock(sbi, flex_group));
214 + sbi->s_flex_groups[flex_group].free_inodes--;
215 + spin_unlock(sb_bgl_lock(sbi, flex_group));
218 inode->i_uid = current->fsuid;
219 if (test_opt (sb, GRPID))
220 inode->i_gid = dir->i_gid;
221 Index: linux-2.6.24-rc7/fs/ext4/super.c
222 ===================================================================
223 --- linux-2.6.24-rc7.orig/fs/ext4/super.c 2008-01-16 13:52:50.000000000 -0800
224 +++ linux-2.6.24-rc7/fs/ext4/super.c 2008-01-16 13:52:51.000000000 -0800
225 @@ -518,6 +518,7 @@ static void ext4_put_super (struct super
226 for (i = 0; i < sbi->s_gdb_count; i++)
227 brelse(sbi->s_group_desc[i]);
228 kfree(sbi->s_group_desc);
229 + kfree(sbi->s_flex_groups);
230 percpu_counter_destroy(&sbi->s_freeblocks_counter);
231 percpu_counter_destroy(&sbi->s_freeinodes_counter);
232 percpu_counter_destroy(&sbi->s_dirs_counter);
233 @@ -1429,6 +1430,54 @@ static int ext4_setup_super(struct super
234 return res;
237 +static int ext4_fill_flex_info(struct super_block *sb)
239 + struct ext4_sb_info *sbi = EXT4_SB(sb);
240 + struct ext4_group_desc *gdp = NULL;
241 + struct buffer_head *bh;
242 + ext4_group_t flex_group_count;
243 + ext4_group_t flex_group;
244 + int groups_per_flex = 0;
245 + __u64 block_bitmap = 0;
246 + int i;
248 + if (!sbi->s_es->s_log_groups_per_flex) {
249 + sbi->s_log_groups_per_flex = 0;
250 + return 1;
253 + sbi->s_log_groups_per_flex = sbi->s_es->s_log_groups_per_flex;
254 + groups_per_flex = 1 << sbi->s_log_groups_per_flex;
256 + flex_group_count = (sbi->s_groups_count + groups_per_flex - 1) /
257 + groups_per_flex;
258 + sbi->s_flex_groups = kmalloc(flex_group_count *
259 + sizeof(struct flex_groups), GFP_KERNEL);
260 + if (sbi->s_flex_groups == NULL) {
261 + printk(KERN_ERR "EXT4-fs: not enough memory\n");
262 + goto failed;
264 + memset(sbi->s_flex_groups, 0, flex_group_count *
265 + sizeof(struct flex_groups));
267 + gdp = ext4_get_group_desc(sb, 1, &bh);
268 + block_bitmap = ext4_block_bitmap(sb, gdp) - 1;
270 + for (i = 0; i < sbi->s_groups_count; i++) {
271 + gdp = ext4_get_group_desc(sb, i, &bh);
273 + flex_group = ext4_flex_group(sbi, i);
274 + sbi->s_flex_groups[flex_group].free_inodes +=
275 + le16_to_cpu(gdp->bg_free_inodes_count);
276 + sbi->s_flex_groups[flex_group].free_blocks +=
277 + le16_to_cpu(gdp->bg_free_blocks_count);
280 + return 1;
281 +failed:
282 + return 0;
285 __le16 ext4_group_desc_csum(struct ext4_sb_info *sbi, __u32 block_group,
286 struct ext4_group_desc *gdp)
288 @@ -2127,6 +2176,14 @@ static int ext4_fill_super (struct super
289 printk(KERN_ERR "EXT4-fs: group descriptors corrupted!\n");
290 goto failed_mount2;
292 + if (EXT4_HAS_INCOMPAT_FEATURE(sb, EXT4_FEATURE_INCOMPAT_FLEX_BG))
293 + if (!ext4_fill_flex_info(sb)) {
294 + printk(KERN_ERR
295 + "EXT4-fs: unable to initialize "
296 + "flex_bg meta info!\n");
297 + goto failed_mount2;
300 sbi->s_gdb_count = db_count;
301 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
302 spin_lock_init(&sbi->s_next_gen_lock);
303 Index: linux-2.6.24-rc7/include/linux/ext4_fs.h
304 ===================================================================
305 --- linux-2.6.24-rc7.orig/include/linux/ext4_fs.h 2008-01-16 13:52:43.000000000 -0800
306 +++ linux-2.6.24-rc7/include/linux/ext4_fs.h 2008-01-16 13:52:51.000000000 -0800
307 @@ -171,6 +171,15 @@ struct ext4_group_desc
308 __u32 bg_reserved2[3];
312 + * Structure of a flex block group info
313 + */
315 +struct flex_groups {
316 + __u32 free_inodes;
317 + __u32 free_blocks;
320 #define EXT4_BG_INODE_UNINIT 0x0001 /* Inode table/bitmap not in use */
321 #define EXT4_BG_BLOCK_UNINIT 0x0002 /* Block bitmap not in use */
322 #define EXT4_BG_INODE_ZEROED 0x0004 /* On-disk itable initialized to zero */
323 @@ -642,7 +651,10 @@ struct ext4_super_block {
324 __le16 s_mmp_interval; /* # seconds to wait in MMP checking */
325 __le64 s_mmp_block; /* Block for multi-mount protection */
326 __le32 s_raid_stripe_width; /* blocks on all data disks (N*stride)*/
327 - __u32 s_reserved[163]; /* Padding to the end of the block */
328 + __u8 s_log_groups_per_flex; /* FLEX_BG group size */
329 + __u8 s_reserved_char_pad2;
330 + __le16 s_reserved_pad;
331 + __u32 s_reserved[162]; /* Padding to the end of the block */
334 #ifdef __KERNEL__
335 @@ -1155,6 +1167,17 @@ struct ext4_group_info *ext4_get_group_i
339 +static inline ext4_group_t ext4_flex_group(struct ext4_sb_info *sbi,
340 + ext4_group_t block_group)
342 + return block_group >> sbi->s_log_groups_per_flex;
345 +static inline unsigned int ext4_flex_bg_size(struct ext4_sb_info *sbi)
347 + return 1 << sbi->s_log_groups_per_flex;
350 #define ext4_std_error(sb, errno) \
351 do { \
352 if ((errno)) \
353 Index: linux-2.6.24-rc7/include/linux/ext4_fs_sb.h
354 ===================================================================
355 --- linux-2.6.24-rc7.orig/include/linux/ext4_fs_sb.h 2008-01-16 13:52:19.000000000 -0800
356 +++ linux-2.6.24-rc7/include/linux/ext4_fs_sb.h 2008-01-16 13:52:51.000000000 -0800
357 @@ -143,6 +143,9 @@ struct ext4_sb_info {
359 /* locality groups */
360 struct ext4_locality_group *s_locality_groups;
362 + unsigned int s_log_groups_per_flex;
363 + struct flex_groups *s_flex_groups;
366 #endif /* _LINUX_EXT4_FS_SB */