revert-mm-fix-blkdev-size-calculation-in-generic_write_checks
[linux-2.6/linux-trees-mm.git] / fs / ext4 / migrate.c
blobb54c084b054b0c7536d9638ab50e71ea78430919
1 /*
2 * Copyright IBM Corporation, 2007
3 * Author Aneesh Kumar K.V <aneesh.kumar@linux.vnet.ibm.com>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of version 2.1 of the GNU Lesser General Public License
7 * as published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
15 #include <linux/module.h>
16 #include <linux/ext4_jbd2.h>
17 #include <linux/ext4_fs_extents.h>
19 struct list_blocks_struct {
20 ext4_lblk_t first_block, last_block;
21 ext4_fsblk_t first_pblock, last_pblock;
24 /* will go away */
25 static void ext4_ext_store_pblock(struct ext4_extent *ex, ext4_fsblk_t pb)
27 ex->ee_start_lo = cpu_to_le32((unsigned long) (pb & 0xffffffff));
28 ex->ee_start_hi = cpu_to_le16((unsigned long) ((pb >> 31) >> 1) & 0xffff);
31 static int finish_range(handle_t *handle, struct inode *inode,
32 struct list_blocks_struct *lb)
35 int retval = 0, needed;
36 struct ext4_extent newext;
37 struct ext4_ext_path *path;
38 if (lb->first_pblock == 0)
39 return 0;
41 /* Add the extent to temp inode*/
42 newext.ee_block = cpu_to_le32(lb->first_block);
43 newext.ee_len = cpu_to_le16(lb->last_block - lb->first_block +1);
44 ext4_ext_store_pblock(&newext, lb->first_pblock);
45 path = ext4_ext_find_extent(inode, lb->first_block, NULL);
47 if (IS_ERR(path)) {
48 retval = PTR_ERR(path);
49 goto err_out;
53 * Calculate the credit needed to inserting this extent
54 * Since we are doing this in loop we may accumalate extra
55 * credit. But below we try to not accumalate too much
56 * of them by restarting the journal.
58 needed = ext4_ext_calc_credits_for_insert(inode, path);
61 * Make sure the credit we accumalated is not really high
64 if (needed && handle->h_buffer_credits >= EXT4_RESERVE_TRANS_BLOCKS) {
66 retval = ext4_journal_restart(handle, needed);
67 if (retval)
68 goto err_out;
72 if (needed && (retval = ext4_journal_extend(handle, needed)) != 0) {
74 * IF not able to extend the journal restart the journal
76 retval = ext4_journal_restart(handle, needed);
77 if (retval)
78 goto err_out;
81 retval = ext4_ext_insert_extent(handle, inode, path, &newext);
83 err_out:
84 lb->first_pblock = 0;
85 return retval;
87 static int update_extent_range(handle_t *handle, struct inode *inode,
88 ext4_fsblk_t pblock, ext4_lblk_t blk_num,
89 struct list_blocks_struct *lb)
91 int retval;
94 * See if we can add on to the existing range (if it exists)
96 if (lb->first_pblock &&
97 (lb->last_pblock+1 == pblock) &&
98 (lb->last_block+1 == blk_num)) {
99 lb->last_pblock = pblock;
100 lb->last_block = blk_num;
101 return 0;
104 * Start a new range.
106 retval = finish_range(handle, inode, lb);
107 lb->first_pblock = lb->last_pblock = pblock;
108 lb->first_block = lb->last_block = blk_num;
110 return retval;
114 static int update_ind_extent_range(handle_t *handle, struct inode *inode,
115 ext4_fsblk_t pblock, ext4_lblk_t *blk_nump,
116 struct list_blocks_struct *lb)
118 struct buffer_head *bh;
119 __le32 *i_data;
120 int i, retval = 0;
121 ext4_lblk_t blk_count = *blk_nump;
122 unsigned long max_entries = inode->i_sb->s_blocksize >> 2;
124 if (!pblock) {
125 /* Only update the file block number */
126 *blk_nump += max_entries;
127 return 0;
130 bh = sb_bread(inode->i_sb, pblock);
131 if (!bh)
132 return -EIO;
134 i_data = (__le32 *)bh->b_data;
136 for (i = 0; i < max_entries; i++, blk_count++) {
137 if (i_data[i]) {
138 retval = update_extent_range(handle, inode,
139 le32_to_cpu(i_data[i]),
140 blk_count, lb);
141 if (retval)
142 break;
146 /* Update the file block number */
147 *blk_nump = blk_count;
148 brelse(bh);
149 return retval;
152 static int update_dind_extent_range(handle_t *handle, struct inode *inode,
153 ext4_fsblk_t pblock, ext4_lblk_t *blk_nump,
154 struct list_blocks_struct *lb)
156 struct buffer_head *bh;
157 __le32 *i_data;
158 int i, retval = 0;
159 ext4_lblk_t blk_count = *blk_nump;
160 unsigned long max_entries = inode->i_sb->s_blocksize >> 2;
162 if (!pblock) {
163 /* Only update the file block number */
164 *blk_nump += max_entries * max_entries;
165 return 0;
168 bh = sb_bread(inode->i_sb, pblock);
169 if (!bh)
170 return -EIO;
172 i_data = (__le32 *)bh->b_data;
174 for (i = 0; i < max_entries; i++) {
175 if (i_data[i]) {
176 retval = update_ind_extent_range(handle, inode,
177 le32_to_cpu(i_data[i]),
178 &blk_count, lb);
179 if (retval)
180 break;
181 } else {
182 /* Only update the file block number */
183 blk_count += max_entries;
187 /* Update the file block number */
188 *blk_nump = blk_count;
189 brelse(bh);
190 return retval;
193 static int update_tind_extent_range(handle_t *handle, struct inode *inode,
194 ext4_fsblk_t pblock, ext4_lblk_t *blk_nump,
195 struct list_blocks_struct *lb)
197 struct buffer_head *bh;
198 __le32 *i_data;
199 int i, retval = 0;
200 ext4_lblk_t blk_count = *blk_nump;
201 unsigned long max_entries = inode->i_sb->s_blocksize >> 2;
203 if (!pblock) {
204 /* Only update the file block number */
205 *blk_nump += max_entries * max_entries * max_entries;
206 return 0;
209 bh = sb_bread(inode->i_sb, pblock);
210 if (!bh)
211 return -EIO;
213 i_data = (__le32 *)bh->b_data;
215 for (i = 0; i < max_entries; i++) {
216 if (i_data[i]) {
217 retval = update_dind_extent_range(handle, inode,
218 le32_to_cpu(i_data[i]),
219 &blk_count, lb);
220 if (retval)
221 break;
222 } else {
223 /* Only update the file block number */
224 blk_count += max_entries * max_entries;
228 /* Update the file block number */
229 *blk_nump = blk_count;
230 brelse(bh);
231 return retval;
236 static int free_dind_blocks(handle_t *handle,
237 struct inode *inode, __le32 i_data)
239 int i;
240 __le32 *tmp_idata;
241 struct buffer_head *bh;
242 unsigned long max_entries = inode->i_sb->s_blocksize >> 2;
244 bh = sb_bread(inode->i_sb, le32_to_cpu(i_data));
245 if (!bh)
246 return -EIO;
248 tmp_idata = (__le32 *)bh->b_data;
249 for (i = 0; i < max_entries; i++) {
250 if (tmp_idata[i]) {
251 ext4_free_blocks(handle, inode,
252 le32_to_cpu(tmp_idata[i]), 1, 1);
255 brelse(bh);
256 ext4_free_blocks(handle, inode, le32_to_cpu(i_data), 1, 1);
258 return 0;
263 static int free_tind_blocks(handle_t *handle,
264 struct inode *inode, __le32 i_data)
266 int i, retval = 0;
267 __le32 *tmp_idata;
268 struct buffer_head *bh;
269 unsigned long max_entries = inode->i_sb->s_blocksize >> 2;
271 bh = sb_bread(inode->i_sb, le32_to_cpu(i_data));
272 if (!bh)
273 return -EIO;
275 tmp_idata = (__le32 *)bh->b_data;
277 for (i = 0; i < max_entries; i++) {
278 if (tmp_idata[i]) {
279 retval = free_dind_blocks(handle,
280 inode, tmp_idata[i]);
281 if (retval) {
282 brelse(bh);
283 return retval;
287 brelse(bh);
288 ext4_free_blocks(handle, inode, le32_to_cpu(i_data), 1, 1);
290 return 0;
295 static int free_ind_block(handle_t *handle, struct inode *inode)
297 int retval;
298 struct ext4_inode_info *ei = EXT4_I(inode);
300 if (ei->i_data[EXT4_IND_BLOCK]) {
302 ext4_free_blocks(handle, inode,
303 le32_to_cpu(ei->i_data[EXT4_IND_BLOCK]), 1, 1);
307 if (ei->i_data[EXT4_DIND_BLOCK]) {
308 retval = free_dind_blocks(handle, inode,
309 ei->i_data[EXT4_DIND_BLOCK]);
310 if (retval)
311 return retval;
314 if (ei->i_data[EXT4_TIND_BLOCK]) {
315 retval = free_tind_blocks(handle, inode,
316 ei->i_data[EXT4_TIND_BLOCK]);
317 if (retval)
318 return retval;
322 return 0;
324 static int ext4_ext_swap_inode_data(handle_t *handle, struct inode *inode,
325 struct inode *tmp_inode, int retval)
327 struct ext4_inode_info *ei = EXT4_I(inode);
328 struct ext4_inode_info *tmp_ei = EXT4_I(tmp_inode);
331 retval = free_ind_block(handle, inode);
332 if (retval)
333 goto err_out;
336 * One credit accounted for writing the
337 * i_data field of the original inode
339 if ((retval = ext4_journal_extend(handle, 1)) != 0) {
341 retval = ext4_journal_restart(handle, 1);
342 if (retval)
343 goto err_out;
347 * We have the extent map build with the tmp inode.
348 * Now copy the i_data across
350 ei->i_flags |= EXT4_EXTENTS_FL;
351 memcpy(ei->i_data, tmp_ei->i_data, sizeof(ei->i_data));
354 * Update i_blocks with the new blocks that got
355 * allocated while adding extents for extent index
356 * blocks.
358 * While converting to extents we need not
359 * update the orignal inode i_blocks for extent blocks
360 * via quota APIs. The quota update happened via tmp_inode already.
362 spin_lock(&inode->i_lock);
363 inode->i_blocks += tmp_inode->i_blocks;
364 spin_unlock(&inode->i_lock);
366 ext4_mark_inode_dirty(handle, inode);
368 err_out:
370 return retval;
373 /* Will go away */
374 static ext4_fsblk_t idx_pblock(struct ext4_extent_idx *ix)
376 ext4_fsblk_t block;
378 block = le32_to_cpu(ix->ei_leaf_lo);
379 block |= ((ext4_fsblk_t) le16_to_cpu(ix->ei_leaf_hi) << 31) << 1;
380 return block;
383 static int free_ext_idx(handle_t *handle, struct inode *inode,
384 struct ext4_extent_idx *ix)
386 int i, retval = 0;
387 ext4_fsblk_t block;
388 struct buffer_head *bh;
389 struct ext4_extent_header *eh;
392 block = idx_pblock(ix);
393 bh = sb_bread(inode->i_sb, block);
394 if (!bh)
395 return -EIO;
397 eh = (struct ext4_extent_header *)bh->b_data;
398 if (eh->eh_depth == 0) {
400 brelse(bh);
401 ext4_free_blocks(handle, inode, block, 1, 1);
403 } else {
405 ix = EXT_FIRST_INDEX(eh);
406 for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ix++) {
407 retval = free_ext_idx(handle, inode, ix);
408 if (retval)
409 return retval;
414 return retval;
418 * Free the extent meta data blocks only
420 static int free_ext_block(handle_t *handle, struct inode *inode)
422 int i, retval = 0;
423 struct ext4_inode_info *ei = EXT4_I(inode);
424 struct ext4_extent_header *eh = (struct ext4_extent_header *)ei->i_data;
425 struct ext4_extent_idx *ix;
426 if (eh->eh_depth == 0) {
428 * No extra blocks allocated for extent meta data
430 return 0;
432 ix = EXT_FIRST_INDEX(eh);
433 for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ix++) {
434 retval = free_ext_idx(handle, inode, ix);
435 if (retval)
436 return retval;
439 return retval;
442 int ext4_ext_migrate(struct inode * inode, struct file * filp,
443 unsigned int cmd, unsigned long arg)
445 handle_t *handle;
446 int retval = 0, i;
447 __le32 *i_data;
448 ext4_lblk_t blk_count = 0;
449 struct ext4_inode_info *ei;
450 struct inode *tmp_inode = NULL;
451 struct list_blocks_struct lb;
452 unsigned long max_entries;
455 if (!test_opt(inode->i_sb, EXTENTS)) {
457 * if mounted with noextents
458 * we don't allow the migrate
460 return -EINVAL;
463 if ((EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL))
464 return -EINVAL;
466 mutex_lock(&EXT4_I(inode)->truncate_mutex);
469 handle = ext4_journal_start(inode,
470 EXT4_DATA_TRANS_BLOCKS(inode->i_sb) +
471 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
472 2 * EXT4_QUOTA_INIT_BLOCKS(inode->i_sb)
473 + 1);
474 if (IS_ERR(handle)) {
475 retval = PTR_ERR(handle);
476 goto err_out;
479 tmp_inode = ext4_new_inode(handle,
480 inode->i_sb->s_root->d_inode,
481 S_IFREG);
483 if (IS_ERR(tmp_inode)) {
484 retval = -ENOMEM;
485 ext4_journal_stop(handle);
486 tmp_inode = NULL;
487 goto err_out;
490 i_size_write(tmp_inode, i_size_read(inode));
492 * We don't want the inode to be reclaimed
493 * if we got interrupted in between. We have
494 * this tmp inode carrying reference to the
495 * data blocks of the original file. We set
496 * the i_nlink to zero at the last stage after
497 * switching the original file to extent format
499 tmp_inode->i_nlink = 1;
501 ext4_ext_tree_init(handle, tmp_inode);
502 ext4_orphan_add(handle, tmp_inode);
503 ext4_journal_stop(handle);
505 ei = EXT4_I(inode);
506 i_data = ei->i_data;
507 memset(&lb, 0, sizeof(lb));
509 /* 32 bit block address 4 bytes */
510 max_entries = inode->i_sb->s_blocksize >> 2;
513 * start with one credit accounted for
514 * superblock modification.
516 * For the tmp_inode we already have commited the
517 * trascation that created the inode. Later as and
518 * when we add extents we extent the journal
520 handle = ext4_journal_start(inode, 1);
521 for (i = 0; i < EXT4_NDIR_BLOCKS; i++, blk_count++) {
523 if (i_data[i]) {
524 retval = update_extent_range(handle, tmp_inode,
525 le32_to_cpu(i_data[i]),
526 blk_count, &lb);
527 if (retval)
528 goto err_out;
532 if (i_data[EXT4_IND_BLOCK]) {
533 retval = update_ind_extent_range(handle, tmp_inode,
534 le32_to_cpu(i_data[EXT4_IND_BLOCK]),
535 &blk_count, &lb);
536 if (retval)
537 goto err_out;
538 } else {
539 blk_count += max_entries;
542 if (i_data[EXT4_DIND_BLOCK]) {
543 retval = update_dind_extent_range(handle, tmp_inode,
544 le32_to_cpu(i_data[EXT4_DIND_BLOCK]),
545 &blk_count, &lb);
546 if (retval)
547 goto err_out;
548 } else {
549 blk_count += max_entries * max_entries;
553 if (i_data[EXT4_TIND_BLOCK]) {
554 retval = update_tind_extent_range(handle, tmp_inode,
555 le32_to_cpu(i_data[EXT4_TIND_BLOCK]),
556 &blk_count, &lb);
557 if (retval)
558 goto err_out;
562 * Build the last extent
564 retval = finish_range(handle, tmp_inode, &lb);
566 err_out:
568 * We are either freeing extent information or indirect
569 * blocks. During this we touch superblock, group descriptor
570 * and block bitmap. Later we mark the tmp_inode dirty
571 * via ext4_ext_tree_init. So allocate a credit of 4
572 * We may update quota (user and group).
574 * FIXME!! we may be touching bitmaps in different block groups.
577 if (ext4_journal_extend(handle,
578 4 + 2*EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb)) != 0) {
580 ext4_journal_restart(handle,
581 4 + 2*EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb));
584 if (retval) {
586 * Failure case delete the extent information with the
587 * tmp_inode
589 free_ext_block(handle, tmp_inode);
591 } else {
593 retval = ext4_ext_swap_inode_data(handle, inode,
594 tmp_inode, retval);
598 * Mark the tmp_inode as of size zero
600 i_size_write(tmp_inode, 0);
603 * set the i_blocks count to zero
604 * so that the ext4_delete_inode does the
605 * right job
607 * We don't need to take the i_lock because
608 * the inode is not visible to user space.
610 tmp_inode->i_blocks = 0;
612 /* Reset the extent details */
613 ext4_ext_tree_init(handle, tmp_inode);
616 * Set the i_nlink to zero so that
617 * generic_drop_inode really deletes the
618 * inode
620 tmp_inode->i_nlink = 0;
622 ext4_journal_stop(handle);
624 mutex_unlock(&EXT4_I(inode)->truncate_mutex);
626 if (tmp_inode)
627 iput(tmp_inode);
629 return retval;