Add fix-uninit-bitmap-blocks patch
[ext4-patch-queue.git] / ext4-online-defrag-alloc-contiguous-blks.patch
blobd13a85371d44301443c26f76863b10bd3e348417
1 ext4: online defrag-- Allocate new contiguous blocks with mballoc
3 From: Akira Fujita <a-fujita@rs.jp.nec.com>
5 Search contiguous free blocks with mutil-block allocation
6 and allocate them for the temporary inode.
8 Signed-off-by: Akira Fujita <a-fujita@rs.jp.nec.com>
9 Signed-off-by: Takashi Sato <t-sato@yk.jp.nec.com>
10 ---
11 fs/ext4/defrag.c | 279 ++++++++++++++++++++++++++++++++++++++++++++++++-
12 fs/ext4/ext4.h | 4
13 fs/ext4/ext4_extents.h | 3
14 fs/ext4/extents.c | 6 -
15 4 files changed, 287 insertions(+), 5 deletions(-)
17 Index: linux-2.6.26-rc9/fs/ext4/defrag.c
18 ===================================================================
19 --- linux-2.6.26-rc9.orig/fs/ext4/defrag.c 2008-07-11 16:05:18.000000000 -0700
20 +++ linux-2.6.26-rc9/fs/ext4/defrag.c 2008-07-11 16:05:18.000000000 -0700
21 @@ -34,7 +34,60 @@ static int
22 ext4_defrag_next_extent(struct inode *inode, struct ext4_ext_path *path,
23 struct ext4_extent **extent)
25 - return 0;
26 + int ppos, leaf_ppos = path->p_depth;
28 + ppos = leaf_ppos;
29 + if (EXT_LAST_EXTENT(path[ppos].p_hdr) > path[ppos].p_ext) {
30 + /* leaf block */
31 + *extent = ++path[ppos].p_ext;
32 + return 0;
33 + }
35 + while (--ppos >= 0) {
36 + if (EXT_LAST_INDEX(path[ppos].p_hdr) >
37 + path[ppos].p_idx) {
38 + int cur_ppos = ppos;
40 + /* index block */
41 + path[ppos].p_idx++;
42 + path[ppos].p_block = idx_pblock(path[ppos].p_idx);
43 + if (path[ppos+1].p_bh)
44 + brelse(path[ppos+1].p_bh);
45 + path[ppos+1].p_bh =
46 + sb_bread(inode->i_sb, path[ppos].p_block);
47 + if (!path[ppos+1].p_bh)
48 + goto err;
49 + path[ppos+1].p_hdr =
50 + ext_block_hdr(path[ppos+1].p_bh);
52 + /* Halfway index block */
53 + while (++cur_ppos < leaf_ppos) {
54 + path[cur_ppos].p_idx =
55 + EXT_FIRST_INDEX(path[cur_ppos].p_hdr);
56 + path[cur_ppos].p_block =
57 + idx_pblock(path[cur_ppos].p_idx);
58 + if (path[cur_ppos+1].p_bh)
59 + brelse(path[cur_ppos+1].p_bh);
60 + path[cur_ppos+1].p_bh = sb_bread(inode->i_sb,
61 + path[cur_ppos].p_block);
62 + if (!path[cur_ppos+1].p_bh)
63 + goto err;
64 + path[cur_ppos+1].p_hdr =
65 + ext_block_hdr(path[cur_ppos+1].p_bh);
66 + }
68 + /* leaf block */
69 + path[leaf_ppos].p_ext = *extent =
70 + EXT_FIRST_EXTENT(path[leaf_ppos].p_hdr);
71 + return 0;
72 + }
73 + }
74 + /* We found the last extent */
75 + return 1;
76 +err:
77 + if (path)
78 + ext4_ext_drop_refs(path);
79 + return -EIO;
82 int ext4_defrag_ioctl(struct inode *inode, struct file *filp, unsigned int cmd,
83 @@ -81,6 +134,84 @@ int ext4_defrag_ioctl(struct inode *inod
86 /**
87 + * ext4_defrag_fill_ar - Prepare to multiple block allocate for tmp inode
88 + *
89 + * @org_inode: original inode
90 + * @dest_inode: temporary inode
91 + * @ar: allocation request for multiple block allocation
92 + * @org_path: indicating the original inode's extent
93 + * @dest_path: indicating the temporary inode's extent
94 + * @req_blocks: contiguous blocks count we need
95 + * @iblock: target file offset
96 + *
97 + */
98 +static void
99 +ext4_defrag_fill_ar(struct inode *org_inode, struct inode *dest_inode,
100 + struct ext4_allocation_request *ar,
101 + struct ext4_ext_path *org_path,
102 + struct ext4_ext_path *dest_path,
103 + ext4_fsblk_t req_blocks, ext4_lblk_t iblock)
105 + ar->inode = dest_inode;
106 + ar->len = req_blocks;
107 + ar->logical = iblock;
108 + ar->flags = EXT4_MB_HINT_DATA | EXT4_MB_HINT_RESERVED
109 + | EXT4_MB_HINT_NOPREALLOC;
110 + ar->lleft = 0;
111 + ar->pleft = 0;
112 + ar->lright = 0;
113 + ar->pright = 0;
115 + ar->goal = ext4_ext_find_goal(dest_inode, dest_path, iblock);
118 +/**
119 + * ext4_defrag_alloc_blocks - Allocate contiguous blocks to temporary inode
121 + * @handle: journal handle
122 + * @org_inode: original inode
123 + * @dest_inode: temporary inode for multiple block allocation
124 + * @ar: allocation request for multiple block allocation
125 + * @dest_path: indicating the temporary inode's extent
126 + * @newblock: start offset of contiguous blocks
128 + * This function returns 0 if succeeed, otherwise returns error value.
129 + */
130 +static int
131 +ext4_defrag_alloc_blocks(handle_t *handle, struct inode *org_inode,
132 + struct inode *dest_inode, struct ext4_allocation_request *ar,
133 + struct ext4_ext_path *dest_path, ext4_fsblk_t *newblock)
135 + struct super_block *sb = org_inode->i_sb;
136 + struct buffer_head *bh = NULL;
137 + int err, i, credits = 0;
139 + credits = ext4_ext_calc_credits_for_insert(dest_inode, dest_path);
140 + err = ext4_ext_journal_restart(handle,
141 + credits + EXT4_TRANS_META_BLOCKS);
142 + if (err)
143 + return err;
145 + *newblock = ext4_mb_new_blocks(handle, ar, &err);
146 + if (err)
147 + return err;
149 + /*
150 + * Dirty buffer_head causes the overwriting
151 + * if ext4_mb_new_blocks() allocates the block
152 + * which used to be the metadata block.
153 + * We should call unmap_underlying_metadata()
154 + * to clear the dirty flag.
155 + */
156 + for (i = 0; i < ar->len; i++) {
157 + bh = sb_find_get_block(sb, *newblock + i);
158 + unmap_underlying_metadata(sb->s_bdev, *newblock + i);
161 + return err;
164 +/**
165 * ext4_defrag_partial - Defrag a file per page
167 * @tmp_inode: temporary inode
168 @@ -99,6 +230,70 @@ ext4_defrag_partial(struct inode *tmp_in
172 + * ext4_defrag_comp_ext_count- Check whether fragments are improved or not
174 + * @org_inode: original inode
175 + * @path: the structure holding some info about
176 + * original extent tree
177 + * @tar_end: the last block number of the allocated blocks
178 + * @sum_tmp: the extents count in the allocated blocks
181 + * This function returns the values as below.
182 + * 0 (improved)
183 + * 1 (not improved)
184 + * negative value (error case)
185 + */
186 +static int
187 +ext4_defrag_comp_ext_count(struct inode *org_inode,
188 + struct ext4_ext_path *org_path, ext4_lblk_t tar_end,
189 + int sum_tmp)
191 + struct ext4_extent *ext = NULL;
192 + int depth = ext_depth(org_inode);
193 + int last_extent = 0;
194 + int sum_org = 0;
195 + int ret = 0;
197 + ext = org_path[depth].p_ext;
199 + /*
200 + * Compare the number of the newly allocated extents to
201 + * that of existing one.
202 + */
203 + while (1) {
204 + if (!last_extent)
205 + ++sum_org;
206 + if (tar_end <= (le32_to_cpu(ext->ee_block) +
207 + le16_to_cpu(ext->ee_len) - 1) ||
208 + last_extent) {
209 + /*
210 + * Fail if goal is not set and the fragmentation
211 + * is not improved.
212 + */
213 + if (sum_org == sum_tmp) {
214 + /* Not improved */
215 + ret = 1;
216 + } else if (sum_org < sum_tmp) {
217 + /* Fragment increased */
218 + ret = -ENOSPC;
219 + printk(KERN_ERR "ext4 defrag: "
220 + "Insufficient free blocks\n");
222 + break;
224 + last_extent =
225 + ext4_defrag_next_extent(org_inode, org_path, &ext);
226 + if (last_extent < 0) {
227 + ret = last_extent;
228 + break;
232 + return ret;
235 +/**
236 * ext4_defrag_new_extent_tree - Get contiguous blocks and build an extent tree
238 * @org_inode: original inode
239 @@ -119,7 +314,87 @@ ext4_defrag_new_extent_tree(struct inode
240 struct ext4_ext_path *org_path, ext4_lblk_t tar_start,
241 ext4_lblk_t tar_blocks, ext4_lblk_t iblock)
243 - return 0;
244 + handle_t *handle;
245 + struct ext4_extent_header *eh = NULL;
246 + struct ext4_allocation_request ar;
247 + struct ext4_ext_path *dest_path = NULL;
248 + struct ext4_extent newex;
249 + ext4_fsblk_t alloc_total = 0;
250 + ext4_fsblk_t newblock = 0;
251 + ext4_lblk_t tar_end = tar_start + tar_blocks - 1;
252 + int sum_tmp = 0;
253 + int metadata = 1;
254 + int ret, ret2;
256 + eh = ext_inode_hdr(tmp_inode);
257 + eh->eh_depth = 0;
259 + dest_path = ext4_ext_find_extent(tmp_inode, iblock, NULL);
260 + if (IS_ERR(dest_path)) {
261 + ret = PTR_ERR(dest_path);
262 + dest_path = NULL;
263 + goto out2;
266 + /* Fill struct ext4_allocation_request with necessary info */
267 + ext4_defrag_fill_ar(org_inode, tmp_inode, &ar, org_path,
268 + dest_path, tar_blocks, iblock);
270 + handle = ext4_journal_start(tmp_inode, 0);
271 + if (IS_ERR(handle)) {
272 + ret = PTR_ERR(handle);
273 + goto out2;
276 + while (alloc_total != tar_blocks) {
277 + /* Allocate blocks */
278 + ret = ext4_defrag_alloc_blocks(handle, org_inode, tmp_inode,
279 + &ar, dest_path, &newblock);
280 + if (ret < 0)
281 + goto out;
283 + alloc_total += ar.len;
285 + newex.ee_block = cpu_to_le32(alloc_total - ar.len);
286 + ext4_ext_store_pblock(&newex, newblock);
287 + newex.ee_len = cpu_to_le16(ar.len);
289 + ret = ext4_ext_insert_extent(handle, tmp_inode,
290 + dest_path, &newex);
291 + if (ret < 0)
292 + goto out;
294 + ar.goal = newblock + ar.len;
295 + ar.len = tar_blocks - alloc_total;
296 + sum_tmp++;
299 + ret = ext4_defrag_comp_ext_count(org_inode, org_path, tar_end,
300 + sum_tmp);
302 +out:
303 + if (ret < 0 || ret == 1) {
304 + if (ar.len)
305 + ext4_free_blocks(handle, tmp_inode, newblock, ar.len,
306 + metadata);
307 + /* Faild case: We have to remove halfway blocks */
308 + ret2 = ext4_ext_remove_space(tmp_inode, 0);
309 + if (ret2) {
310 + printk(KERN_ERR "ext4 defrag: "
311 + "Failed to remove temporary inode blocks\n");
312 + ret = ret2;
316 + ext4_journal_stop(handle);
318 +out2:
319 + if (dest_path) {
320 + ext4_ext_drop_refs(dest_path);
321 + kfree(dest_path);
324 + return ret;
328 Index: linux-2.6.26-rc9/fs/ext4/ext4.h
329 ===================================================================
330 --- linux-2.6.26-rc9.orig/fs/ext4/ext4.h 2008-07-11 16:05:18.000000000 -0700
331 +++ linux-2.6.26-rc9/fs/ext4/ext4.h 2008-07-11 16:05:18.000000000 -0700
332 @@ -331,6 +331,7 @@ struct ext4_ext_defrag_data {
333 ext4_fsblk_t goal; /* block offset for allocation */
336 +#define EXT4_TRANS_META_BLOCKS 4 /* bitmap + group desc + sb + inode */
339 * Mount options
340 @@ -1137,6 +1138,8 @@ extern void ext4_inode_bitmap_set(struct
341 struct ext4_group_desc *bg, ext4_fsblk_t blk);
342 extern void ext4_inode_table_set(struct super_block *sb,
343 struct ext4_group_desc *bg, ext4_fsblk_t blk);
344 +/* extents.c */
345 +extern int ext4_ext_journal_restart(handle_t *handle, int needed);
346 /* defrag.c */
347 extern int ext4_defrag(struct file *filp, ext4_lblk_t block_start,
348 ext4_lblk_t defrag_size);
349 @@ -1259,6 +1262,7 @@ extern int ext4_get_blocks_wrap(handle_t
350 sector_t block, unsigned long max_blocks,
351 struct buffer_head *bh, int create,
352 int extend_disksize, int flag);
353 +extern int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start);
354 #endif /* __KERNEL__ */
356 #endif /* _EXT4_H */
357 Index: linux-2.6.26-rc9/fs/ext4/ext4_extents.h
358 ===================================================================
359 --- linux-2.6.26-rc9.orig/fs/ext4/ext4_extents.h 2008-07-11 16:05:18.000000000 -0700
360 +++ linux-2.6.26-rc9/fs/ext4/ext4_extents.h 2008-07-11 16:05:18.000000000 -0700
361 @@ -231,5 +231,8 @@ extern int ext4_ext_search_right(struct
362 extern void ext4_ext_drop_refs(struct ext4_ext_path *);
363 extern ext4_fsblk_t ext_pblock(struct ext4_extent *ex);
364 extern void ext4_ext_drop_refs(struct ext4_ext_path *path);
365 +extern ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
366 + struct ext4_ext_path *path,
367 + ext4_lblk_t block);
368 #endif /* _EXT4_EXTENTS */
370 Index: linux-2.6.26-rc9/fs/ext4/extents.c
371 ===================================================================
372 --- linux-2.6.26-rc9.orig/fs/ext4/extents.c 2008-07-11 16:05:18.000000000 -0700
373 +++ linux-2.6.26-rc9/fs/ext4/extents.c 2008-07-11 16:05:18.000000000 -0700
374 @@ -92,7 +92,7 @@ static void ext4_idx_store_pblock(struct
375 ix->ei_leaf_hi = cpu_to_le16((unsigned long) ((pb >> 31) >> 1) & 0xffff);
378 -static int ext4_ext_journal_restart(handle_t *handle, int needed)
379 +int ext4_ext_journal_restart(handle_t *handle, int needed)
381 int err;
383 @@ -141,7 +141,7 @@ static int ext4_ext_dirty(handle_t *hand
384 return err;
387 -static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
388 +ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
389 struct ext4_ext_path *path,
390 ext4_lblk_t block)
392 @@ -1992,7 +1992,7 @@ ext4_ext_more_to_rm(struct ext4_ext_path
393 return 1;
396 -static int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start)
397 +int ext4_ext_remove_space(struct inode *inode, ext4_lblk_t start)
399 struct super_block *sb = inode->i_sb;
400 int depth = ext_depth(inode);