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
;
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)
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
);
48 retval
= PTR_ERR(path
);
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
);
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
);
81 retval
= ext4_ext_insert_extent(handle
, inode
, path
, &newext
);
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
)
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
;
106 retval
= finish_range(handle
, inode
, lb
);
107 lb
->first_pblock
= lb
->last_pblock
= pblock
;
108 lb
->first_block
= lb
->last_block
= blk_num
;
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
;
121 ext4_lblk_t blk_count
= *blk_nump
;
122 unsigned long max_entries
= inode
->i_sb
->s_blocksize
>> 2;
125 /* Only update the file block number */
126 *blk_nump
+= max_entries
;
130 bh
= sb_bread(inode
->i_sb
, pblock
);
134 i_data
= (__le32
*)bh
->b_data
;
136 for (i
= 0; i
< max_entries
; i
++, blk_count
++) {
138 retval
= update_extent_range(handle
, inode
,
139 le32_to_cpu(i_data
[i
]),
146 /* Update the file block number */
147 *blk_nump
= blk_count
;
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
;
159 ext4_lblk_t blk_count
= *blk_nump
;
160 unsigned long max_entries
= inode
->i_sb
->s_blocksize
>> 2;
163 /* Only update the file block number */
164 *blk_nump
+= max_entries
* max_entries
;
168 bh
= sb_bread(inode
->i_sb
, pblock
);
172 i_data
= (__le32
*)bh
->b_data
;
174 for (i
= 0; i
< max_entries
; i
++) {
176 retval
= update_ind_extent_range(handle
, inode
,
177 le32_to_cpu(i_data
[i
]),
182 /* Only update the file block number */
183 blk_count
+= max_entries
;
187 /* Update the file block number */
188 *blk_nump
= blk_count
;
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
;
200 ext4_lblk_t blk_count
= *blk_nump
;
201 unsigned long max_entries
= inode
->i_sb
->s_blocksize
>> 2;
204 /* Only update the file block number */
205 *blk_nump
+= max_entries
* max_entries
* max_entries
;
209 bh
= sb_bread(inode
->i_sb
, pblock
);
213 i_data
= (__le32
*)bh
->b_data
;
215 for (i
= 0; i
< max_entries
; i
++) {
217 retval
= update_dind_extent_range(handle
, inode
,
218 le32_to_cpu(i_data
[i
]),
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
;
236 static int free_dind_blocks(handle_t
*handle
,
237 struct inode
*inode
, __le32 i_data
)
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
));
248 tmp_idata
= (__le32
*)bh
->b_data
;
249 for (i
= 0; i
< max_entries
; i
++) {
251 ext4_free_blocks(handle
, inode
,
252 le32_to_cpu(tmp_idata
[i
]), 1, 1);
256 ext4_free_blocks(handle
, inode
, le32_to_cpu(i_data
), 1, 1);
263 static int free_tind_blocks(handle_t
*handle
,
264 struct inode
*inode
, __le32 i_data
)
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
));
275 tmp_idata
= (__le32
*)bh
->b_data
;
277 for (i
= 0; i
< max_entries
; i
++) {
279 retval
= free_dind_blocks(handle
,
280 inode
, tmp_idata
[i
]);
288 ext4_free_blocks(handle
, inode
, le32_to_cpu(i_data
), 1, 1);
295 static int free_ind_block(handle_t
*handle
, struct inode
*inode
)
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
]);
314 if (ei
->i_data
[EXT4_TIND_BLOCK
]) {
315 retval
= free_tind_blocks(handle
, inode
,
316 ei
->i_data
[EXT4_TIND_BLOCK
]);
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
);
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);
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
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
);
374 static ext4_fsblk_t
idx_pblock(struct ext4_extent_idx
*ix
)
378 block
= le32_to_cpu(ix
->ei_leaf_lo
);
379 block
|= ((ext4_fsblk_t
) le16_to_cpu(ix
->ei_leaf_hi
) << 31) << 1;
383 static int free_ext_idx(handle_t
*handle
, struct inode
*inode
,
384 struct ext4_extent_idx
*ix
)
388 struct buffer_head
*bh
;
389 struct ext4_extent_header
*eh
;
392 block
= idx_pblock(ix
);
393 bh
= sb_bread(inode
->i_sb
, block
);
397 eh
= (struct ext4_extent_header
*)bh
->b_data
;
398 if (eh
->eh_depth
== 0) {
401 ext4_free_blocks(handle
, inode
, block
, 1, 1);
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
);
418 * Free the extent meta data blocks only
420 static int free_ext_block(handle_t
*handle
, struct inode
*inode
)
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
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
);
442 int ext4_ext_migrate(struct inode
* inode
, struct file
* filp
,
443 unsigned int cmd
, unsigned long arg
)
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
463 if ((EXT4_I(inode
)->i_flags
& EXT4_EXTENTS_FL
))
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
)
474 if (IS_ERR(handle
)) {
475 retval
= PTR_ERR(handle
);
479 tmp_inode
= ext4_new_inode(handle
,
480 inode
->i_sb
->s_root
->d_inode
,
483 if (IS_ERR(tmp_inode
)) {
485 ext4_journal_stop(handle
);
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
);
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
++) {
524 retval
= update_extent_range(handle
, tmp_inode
,
525 le32_to_cpu(i_data
[i
]),
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
]),
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
]),
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
]),
562 * Build the last extent
564 retval
= finish_range(handle
, tmp_inode
, &lb
);
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
));
586 * Failure case delete the extent information with the
589 free_ext_block(handle
, tmp_inode
);
593 retval
= ext4_ext_swap_inode_data(handle
, inode
,
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
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
620 tmp_inode
->i_nlink
= 0;
622 ext4_journal_stop(handle
);
624 mutex_unlock(&EXT4_I(inode
)->truncate_mutex
);