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>
20 * The contiguous blocks details which can be
21 * represented by a single extent
23 struct list_blocks_struct {
24 ext4_lblk_t first_block, last_block;
25 ext4_fsblk_t first_pblock, last_pblock;
28 static int finish_range(handle_t *handle, struct inode *inode,
29 struct list_blocks_struct *lb)
32 int retval = 0, needed;
33 struct ext4_extent newext;
34 struct ext4_ext_path *path;
35 if (lb->first_pblock == 0)
38 /* Add the extent to temp inode*/
39 newext.ee_block = cpu_to_le32(lb->first_block);
40 newext.ee_len = cpu_to_le16(lb->last_block - lb->first_block + 1);
41 ext4_ext_store_pblock(&newext, lb->first_pblock);
42 path = ext4_ext_find_extent(inode, lb->first_block, NULL);
45 retval = PTR_ERR(path);
51 * Calculate the credit needed to inserting this extent
52 * Since we are doing this in loop we may accumalate extra
53 * credit. But below we try to not accumalate too much
54 * of them by restarting the journal.
56 needed = ext4_ext_calc_credits_for_insert(inode, path);
59 * Make sure the credit we accumalated is not really high
61 if (needed && handle->h_buffer_credits >= EXT4_RESERVE_TRANS_BLOCKS) {
62 retval = ext4_journal_restart(handle, needed);
66 retval = ext4_journal_extend(handle, needed);
69 * IF not able to extend the journal restart the journal
71 retval = ext4_journal_restart(handle, needed);
76 retval = ext4_ext_insert_extent(handle, inode, path, &newext);
79 ext4_ext_drop_refs(path);
86 static int update_extent_range(handle_t *handle, struct inode *inode,
87 ext4_fsblk_t pblock, ext4_lblk_t blk_num,
88 struct list_blocks_struct *lb)
92 * See if we can add on to the existing range (if it exists)
94 if (lb->first_pblock &&
95 (lb->last_pblock+1 == pblock) &&
96 (lb->last_block+1 == blk_num)) {
97 lb->last_pblock = pblock;
98 lb->last_block = blk_num;
104 retval = finish_range(handle, inode, lb);
105 lb->first_pblock = lb->last_pblock = pblock;
106 lb->first_block = lb->last_block = blk_num;
111 static int update_ind_extent_range(handle_t *handle, struct inode *inode,
112 ext4_fsblk_t pblock, ext4_lblk_t *blk_nump,
113 struct list_blocks_struct *lb)
115 struct buffer_head *bh;
118 ext4_lblk_t blk_count = *blk_nump;
119 unsigned long max_entries = inode->i_sb->s_blocksize >> 2;
122 /* Only update the file block number */
123 *blk_nump += max_entries;
127 bh = sb_bread(inode->i_sb, pblock);
131 i_data = (__le32 *)bh->b_data;
132 for (i = 0; i < max_entries; i++, blk_count++) {
134 retval = update_extent_range(handle, inode,
135 le32_to_cpu(i_data[i]),
142 /* Update the file block number */
143 *blk_nump = blk_count;
149 static int update_dind_extent_range(handle_t *handle, struct inode *inode,
150 ext4_fsblk_t pblock, ext4_lblk_t *blk_nump,
151 struct list_blocks_struct *lb)
153 struct buffer_head *bh;
156 ext4_lblk_t blk_count = *blk_nump;
157 unsigned long max_entries = inode->i_sb->s_blocksize >> 2;
160 /* Only update the file block number */
161 *blk_nump += max_entries * max_entries;
164 bh = sb_bread(inode->i_sb, pblock);
168 i_data = (__le32 *)bh->b_data;
169 for (i = 0; i < max_entries; i++) {
171 retval = update_ind_extent_range(handle, inode,
172 le32_to_cpu(i_data[i]),
177 /* Only update the file block number */
178 blk_count += max_entries;
182 /* Update the file block number */
183 *blk_nump = blk_count;
189 static int update_tind_extent_range(handle_t *handle, struct inode *inode,
190 ext4_fsblk_t pblock, ext4_lblk_t *blk_nump,
191 struct list_blocks_struct *lb)
193 struct buffer_head *bh;
196 ext4_lblk_t blk_count = *blk_nump;
197 unsigned long max_entries = inode->i_sb->s_blocksize >> 2;
200 /* Only update the file block number */
201 *blk_nump += max_entries * max_entries * max_entries;
204 bh = sb_bread(inode->i_sb, pblock);
208 i_data = (__le32 *)bh->b_data;
209 for (i = 0; i < max_entries; i++) {
211 retval = update_dind_extent_range(handle, inode,
212 le32_to_cpu(i_data[i]),
217 /* Only update the file block number */
218 blk_count += max_entries * max_entries;
220 /* Update the file block number */
221 *blk_nump = blk_count;
227 static int extend_credit_for_blkdel(handle_t *handle, struct inode *inode)
229 int retval = 0, needed;
231 if (handle->h_buffer_credits > EXT4_RESERVE_TRANS_BLOCKS)
234 * We are freeing a blocks. During this we touch
235 * superblock, group descriptor and block bitmap.
236 * So allocate a credit of 3. We may update
237 * quota (user and group).
239 needed = 3 + 2*EXT4_QUOTA_TRANS_BLOCKS(inode->i_sb);
241 if (ext4_journal_extend(handle, needed) != 0)
242 retval = ext4_journal_restart(handle, needed);
247 static int free_dind_blocks(handle_t *handle,
248 struct inode *inode, __le32 i_data)
252 struct buffer_head *bh;
253 unsigned long max_entries = inode->i_sb->s_blocksize >> 2;
255 bh = sb_bread(inode->i_sb, le32_to_cpu(i_data));
259 tmp_idata = (__le32 *)bh->b_data;
260 for (i = 0; i < max_entries; i++) {
262 extend_credit_for_blkdel(handle, inode);
263 ext4_free_blocks(handle, inode,
264 le32_to_cpu(tmp_idata[i]), 1, 1);
268 extend_credit_for_blkdel(handle, inode);
269 ext4_free_blocks(handle, inode, le32_to_cpu(i_data), 1, 1);
273 static int free_tind_blocks(handle_t *handle,
274 struct inode *inode, __le32 i_data)
278 struct buffer_head *bh;
279 unsigned long max_entries = inode->i_sb->s_blocksize >> 2;
281 bh = sb_bread(inode->i_sb, le32_to_cpu(i_data));
285 tmp_idata = (__le32 *)bh->b_data;
286 for (i = 0; i < max_entries; i++) {
288 retval = free_dind_blocks(handle,
289 inode, tmp_idata[i]);
297 extend_credit_for_blkdel(handle, inode);
298 ext4_free_blocks(handle, inode, le32_to_cpu(i_data), 1, 1);
302 static int free_ind_block(handle_t *handle, struct inode *inode, __le32 *i_data)
306 /* ei->i_data[EXT4_IND_BLOCK] */
308 extend_credit_for_blkdel(handle, inode);
309 ext4_free_blocks(handle, inode,
310 le32_to_cpu(i_data[0]), 1, 1);
313 /* ei->i_data[EXT4_DIND_BLOCK] */
315 retval = free_dind_blocks(handle, inode, i_data[1]);
320 /* ei->i_data[EXT4_TIND_BLOCK] */
322 retval = free_tind_blocks(handle, inode, i_data[2]);
329 static int ext4_ext_swap_inode_data(handle_t *handle, struct inode *inode,
330 struct inode *tmp_inode)
334 struct ext4_inode_info *ei = EXT4_I(inode);
335 struct ext4_inode_info *tmp_ei = EXT4_I(tmp_inode);
338 * One credit accounted for writing the
339 * i_data field of the original inode
341 retval = ext4_journal_extend(handle, 1);
343 retval = ext4_journal_restart(handle, 1);
348 i_data[0] = ei->i_data[EXT4_IND_BLOCK];
349 i_data[1] = ei->i_data[EXT4_DIND_BLOCK];
350 i_data[2] = ei->i_data[EXT4_TIND_BLOCK];
352 down_write(&EXT4_I(inode)->i_data_sem);
354 * We have the extent map build with the tmp inode.
355 * Now copy the i_data across
357 ei->i_flags |= EXT4_EXTENTS_FL;
358 memcpy(ei->i_data, tmp_ei->i_data, sizeof(ei->i_data));
361 * Update i_blocks with the new blocks that got
362 * allocated while adding extents for extent index
365 * While converting to extents we need not
366 * update the orignal inode i_blocks for extent blocks
367 * via quota APIs. The quota update happened via tmp_inode already.
369 spin_lock(&inode->i_lock);
370 inode->i_blocks += tmp_inode->i_blocks;
371 spin_unlock(&inode->i_lock);
372 up_write(&EXT4_I(inode)->i_data_sem);
375 * We mark the inode dirty after, because we decrement the
376 * i_blocks when freeing the indirect meta-data blocks
378 retval = free_ind_block(handle, inode, i_data);
379 ext4_mark_inode_dirty(handle, inode);
385 static int free_ext_idx(handle_t *handle, struct inode *inode,
386 struct ext4_extent_idx *ix)
390 struct buffer_head *bh;
391 struct ext4_extent_header *eh;
393 block = idx_pblock(ix);
394 bh = sb_bread(inode->i_sb, block);
398 eh = (struct ext4_extent_header *)bh->b_data;
399 if (eh->eh_depth != 0) {
400 ix = EXT_FIRST_INDEX(eh);
401 for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ix++) {
402 retval = free_ext_idx(handle, inode, ix);
408 extend_credit_for_blkdel(handle, inode);
409 ext4_free_blocks(handle, inode, block, 1, 1);
414 * Free the extent meta data blocks only
416 static int free_ext_block(handle_t *handle, struct inode *inode)
419 struct ext4_inode_info *ei = EXT4_I(inode);
420 struct ext4_extent_header *eh = (struct ext4_extent_header *)ei->i_data;
421 struct ext4_extent_idx *ix;
422 if (eh->eh_depth == 0)
424 * No extra blocks allocated for extent meta data
427 ix = EXT_FIRST_INDEX(eh);
428 for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ix++) {
429 retval = free_ext_idx(handle, inode, ix);
437 int ext4_ext_migrate(struct inode *inode, struct file *filp,
438 unsigned int cmd, unsigned long arg)
443 ext4_lblk_t blk_count = 0;
444 struct ext4_inode_info *ei;
445 struct inode *tmp_inode = NULL;
446 struct list_blocks_struct lb;
447 unsigned long max_entries;
449 if (!test_opt(inode->i_sb, EXTENTS))
451 * if mounted with noextents we don't allow the migrate
455 if ((EXT4_I(inode)->i_flags & EXT4_EXTENTS_FL))
458 if (S_ISLNK(inode->i_mode) && inode->i_blocks == 0)
460 * don't migrate fast symlink
464 handle = ext4_journal_start(inode,
465 EXT4_DATA_TRANS_BLOCKS(inode->i_sb) +
466 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
467 2 * EXT4_QUOTA_INIT_BLOCKS(inode->i_sb)
469 if (IS_ERR(handle)) {
470 retval = PTR_ERR(handle);
473 tmp_inode = ext4_new_inode(handle,
474 inode->i_sb->s_root->d_inode,
476 if (IS_ERR(tmp_inode)) {
478 ext4_journal_stop(handle);
482 i_size_write(tmp_inode, i_size_read(inode));
484 * We don't want the inode to be reclaimed
485 * if we got interrupted in between. We have
486 * this tmp inode carrying reference to the
487 * data blocks of the original file. We set
488 * the i_nlink to zero at the last stage after
489 * switching the original file to extent format
491 tmp_inode->i_nlink = 1;
493 ext4_ext_tree_init(handle, tmp_inode);
494 ext4_orphan_add(handle, tmp_inode);
495 ext4_journal_stop(handle);
498 * start with one credit accounted for
499 * superblock modification.
501 * For the tmp_inode we already have commited the
502 * trascation that created the inode. Later as and
503 * when we add extents we extent the journal
506 * inode_mutex prevent write and truncate on the file. Read still goes
507 * through. We take i_data_sem in ext4_ext_swap_inode_data before we
508 * switch the inode format to prevent read.
510 mutex_lock(&(inode->i_mutex));
511 handle = ext4_journal_start(inode, 1);
515 memset(&lb, 0, sizeof(lb));
517 /* 32 bit block address 4 bytes */
518 max_entries = inode->i_sb->s_blocksize >> 2;
519 for (i = 0; i < EXT4_NDIR_BLOCKS; i++, blk_count++) {
521 retval = update_extent_range(handle, tmp_inode,
522 le32_to_cpu(i_data[i]),
528 if (i_data[EXT4_IND_BLOCK]) {
529 retval = update_ind_extent_range(handle, tmp_inode,
530 le32_to_cpu(i_data[EXT4_IND_BLOCK]),
535 blk_count += max_entries;
536 if (i_data[EXT4_DIND_BLOCK]) {
537 retval = update_dind_extent_range(handle, tmp_inode,
538 le32_to_cpu(i_data[EXT4_DIND_BLOCK]),
543 blk_count += max_entries * max_entries;
544 if (i_data[EXT4_TIND_BLOCK]) {
545 retval = update_tind_extent_range(handle, tmp_inode,
546 le32_to_cpu(i_data[EXT4_TIND_BLOCK]),
552 * Build the last extent
554 retval = finish_range(handle, tmp_inode, &lb);
558 * Failure case delete the extent information with the
561 free_ext_block(handle, tmp_inode);
563 retval = ext4_ext_swap_inode_data(handle, inode,
566 /* We mark the tmp_inode dirty via ext4_ext_tree_init. */
567 if (ext4_journal_extend(handle, 1) != 0)
568 ext4_journal_restart(handle, 1);
571 * Mark the tmp_inode as of size zero
573 i_size_write(tmp_inode, 0);
576 * set the i_blocks count to zero
577 * so that the ext4_delete_inode does the
580 * We don't need to take the i_lock because
581 * the inode is not visible to user space.
583 tmp_inode->i_blocks = 0;
585 /* Reset the extent details */
586 ext4_ext_tree_init(handle, tmp_inode);
589 * Set the i_nlink to zero so that
590 * generic_drop_inode really deletes the
593 tmp_inode->i_nlink = 0;
595 ext4_journal_stop(handle);
596 mutex_unlock(&(inode->i_mutex));