@@ -61,10 +61,9 @@ static int finish_range(handle_t *handle, struct inode *inode,
6161 retval = ext4_journal_restart (handle , needed );
6262 if (retval )
6363 goto err_out ;
64- }
65- if (needed ) {
64+ } else if (needed ) {
6665 retval = ext4_journal_extend (handle , needed );
67- if (retval != 0 ) {
66+ if (retval ) {
6867 /*
6968 * IF not able to extend the journal restart the journal
7069 */
@@ -220,6 +219,26 @@ static int update_tind_extent_range(handle_t *handle, struct inode *inode,
220219
221220}
222221
222+ static int extend_credit_for_blkdel (handle_t * handle , struct inode * inode )
223+ {
224+ int retval = 0 , needed ;
225+
226+ if (handle -> h_buffer_credits > EXT4_RESERVE_TRANS_BLOCKS )
227+ return 0 ;
228+ /*
229+ * We are freeing a blocks. During this we touch
230+ * superblock, group descriptor and block bitmap.
231+ * So allocate a credit of 3. We may update
232+ * quota (user and group).
233+ */
234+ needed = 3 + 2 * EXT4_QUOTA_TRANS_BLOCKS (inode -> i_sb );
235+
236+ if (ext4_journal_extend (handle , needed ) != 0 )
237+ retval = ext4_journal_restart (handle , needed );
238+
239+ return retval ;
240+ }
241+
223242static int free_dind_blocks (handle_t * handle ,
224243 struct inode * inode , __le32 i_data )
225244{
@@ -234,11 +253,14 @@ static int free_dind_blocks(handle_t *handle,
234253
235254 tmp_idata = (__le32 * )bh -> b_data ;
236255 for (i = 0 ; i < max_entries ; i ++ ) {
237- if (tmp_idata [i ])
256+ if (tmp_idata [i ]) {
257+ extend_credit_for_blkdel (handle , inode );
238258 ext4_free_blocks (handle , inode ,
239259 le32_to_cpu (tmp_idata [i ]), 1 , 1 );
260+ }
240261 }
241262 put_bh (bh );
263+ extend_credit_for_blkdel (handle , inode );
242264 ext4_free_blocks (handle , inode , le32_to_cpu (i_data ), 1 , 1 );
243265 return 0 ;
244266}
@@ -267,45 +289,46 @@ static int free_tind_blocks(handle_t *handle,
267289 }
268290 }
269291 put_bh (bh );
292+ extend_credit_for_blkdel (handle , inode );
270293 ext4_free_blocks (handle , inode , le32_to_cpu (i_data ), 1 , 1 );
271294 return 0 ;
272295}
273296
274- static int free_ind_block (handle_t * handle , struct inode * inode )
297+ static int free_ind_block (handle_t * handle , struct inode * inode , __le32 * i_data )
275298{
276299 int retval ;
277- struct ext4_inode_info * ei = EXT4_I (inode );
278300
279- if (ei -> i_data [EXT4_IND_BLOCK ])
301+ /* ei->i_data[EXT4_IND_BLOCK] */
302+ if (i_data [0 ]) {
303+ extend_credit_for_blkdel (handle , inode );
280304 ext4_free_blocks (handle , inode ,
281- le32_to_cpu (ei -> i_data [EXT4_IND_BLOCK ]), 1 , 1 );
305+ le32_to_cpu (i_data [0 ]), 1 , 1 );
306+ }
282307
283- if ( ei -> i_data [EXT4_DIND_BLOCK ]) {
284- retval = free_dind_blocks ( handle , inode ,
285- ei -> i_data [EXT4_DIND_BLOCK ]);
308+ /* ei->i_data[EXT4_DIND_BLOCK] */
309+ if ( i_data [ 1 ]) {
310+ retval = free_dind_blocks ( handle , inode , i_data [1 ]);
286311 if (retval )
287312 return retval ;
288313 }
289314
290- if ( ei -> i_data [EXT4_TIND_BLOCK ]) {
291- retval = free_tind_blocks ( handle , inode ,
292- ei -> i_data [EXT4_TIND_BLOCK ]);
315+ /* ei->i_data[EXT4_TIND_BLOCK] */
316+ if ( i_data [ 2 ]) {
317+ retval = free_tind_blocks ( handle , inode , i_data [2 ]);
293318 if (retval )
294319 return retval ;
295320 }
296321 return 0 ;
297322}
298323
299324static int ext4_ext_swap_inode_data (handle_t * handle , struct inode * inode ,
300- struct inode * tmp_inode , int retval )
325+ struct inode * tmp_inode )
301326{
327+ int retval ;
328+ __le32 i_data [3 ];
302329 struct ext4_inode_info * ei = EXT4_I (inode );
303330 struct ext4_inode_info * tmp_ei = EXT4_I (tmp_inode );
304331
305- retval = free_ind_block (handle , inode );
306- if (retval )
307- goto err_out ;
308-
309332 /*
310333 * One credit accounted for writing the
311334 * i_data field of the original inode
@@ -317,6 +340,11 @@ static int ext4_ext_swap_inode_data(handle_t *handle, struct inode *inode,
317340 goto err_out ;
318341 }
319342
343+ i_data [0 ] = ei -> i_data [EXT4_IND_BLOCK ];
344+ i_data [1 ] = ei -> i_data [EXT4_DIND_BLOCK ];
345+ i_data [2 ] = ei -> i_data [EXT4_TIND_BLOCK ];
346+
347+ down_write (& EXT4_I (inode )-> i_data_sem );
320348 /*
321349 * We have the extent map build with the tmp inode.
322350 * Now copy the i_data across
@@ -336,8 +364,15 @@ static int ext4_ext_swap_inode_data(handle_t *handle, struct inode *inode,
336364 spin_lock (& inode -> i_lock );
337365 inode -> i_blocks += tmp_inode -> i_blocks ;
338366 spin_unlock (& inode -> i_lock );
367+ up_write (& EXT4_I (inode )-> i_data_sem );
339368
369+ /*
370+ * We mark the inode dirty after, because we decrement the
371+ * i_blocks when freeing the indirect meta-data blocks
372+ */
373+ retval = free_ind_block (handle , inode , i_data );
340374 ext4_mark_inode_dirty (handle , inode );
375+
341376err_out :
342377 return retval ;
343378}
@@ -365,6 +400,7 @@ static int free_ext_idx(handle_t *handle, struct inode *inode,
365400 }
366401 }
367402 put_bh (bh );
403+ extend_credit_for_blkdel (handle , inode );
368404 ext4_free_blocks (handle , inode , block , 1 , 1 );
369405 return retval ;
370406}
@@ -420,7 +456,6 @@ int ext4_ext_migrate(struct inode *inode, struct file *filp,
420456 */
421457 return retval ;
422458
423- down_write (& EXT4_I (inode )-> i_data_sem );
424459 handle = ext4_journal_start (inode ,
425460 EXT4_DATA_TRANS_BLOCKS (inode -> i_sb ) +
426461 EXT4_INDEX_EXTRA_TRANS_BLOCKS + 3 +
@@ -454,13 +489,6 @@ int ext4_ext_migrate(struct inode *inode, struct file *filp,
454489 ext4_orphan_add (handle , tmp_inode );
455490 ext4_journal_stop (handle );
456491
457- ei = EXT4_I (inode );
458- i_data = ei -> i_data ;
459- memset (& lb , 0 , sizeof (lb ));
460-
461- /* 32 bit block address 4 bytes */
462- max_entries = inode -> i_sb -> s_blocksize >> 2 ;
463-
464492 /*
465493 * start with one credit accounted for
466494 * superblock modification.
@@ -469,7 +497,20 @@ int ext4_ext_migrate(struct inode *inode, struct file *filp,
469497 * trascation that created the inode. Later as and
470498 * when we add extents we extent the journal
471499 */
500+ /*
501+ * inode_mutex prevent write and truncate on the file. Read still goes
502+ * through. We take i_data_sem in ext4_ext_swap_inode_data before we
503+ * switch the inode format to prevent read.
504+ */
505+ mutex_lock (& (inode -> i_mutex ));
472506 handle = ext4_journal_start (inode , 1 );
507+
508+ ei = EXT4_I (inode );
509+ i_data = ei -> i_data ;
510+ memset (& lb , 0 , sizeof (lb ));
511+
512+ /* 32 bit block address 4 bytes */
513+ max_entries = inode -> i_sb -> s_blocksize >> 2 ;
473514 for (i = 0 ; i < EXT4_NDIR_BLOCKS ; i ++ , blk_count ++ ) {
474515 if (i_data [i ]) {
475516 retval = update_extent_range (handle , tmp_inode ,
@@ -507,19 +548,6 @@ int ext4_ext_migrate(struct inode *inode, struct file *filp,
507548 */
508549 retval = finish_range (handle , tmp_inode , & lb );
509550err_out :
510- /*
511- * We are either freeing extent information or indirect
512- * blocks. During this we touch superblock, group descriptor
513- * and block bitmap. Later we mark the tmp_inode dirty
514- * via ext4_ext_tree_init. So allocate a credit of 4
515- * We may update quota (user and group).
516- *
517- * FIXME!! we may be touching bitmaps in different block groups.
518- */
519- if (ext4_journal_extend (handle ,
520- 4 + 2 * EXT4_QUOTA_TRANS_BLOCKS (inode -> i_sb )) != 0 )
521- ext4_journal_restart (handle ,
522- 4 + 2 * EXT4_QUOTA_TRANS_BLOCKS (inode -> i_sb ));
523551 if (retval )
524552 /*
525553 * Failure case delete the extent information with the
@@ -528,7 +556,11 @@ int ext4_ext_migrate(struct inode *inode, struct file *filp,
528556 free_ext_block (handle , tmp_inode );
529557 else
530558 retval = ext4_ext_swap_inode_data (handle , inode ,
531- tmp_inode , retval );
559+ tmp_inode );
560+
561+ /* We mark the tmp_inode dirty via ext4_ext_tree_init. */
562+ if (ext4_journal_extend (handle , 1 ) != 0 )
563+ ext4_journal_restart (handle , 1 );
532564
533565 /*
534566 * Mark the tmp_inode as of size zero
@@ -556,8 +588,7 @@ int ext4_ext_migrate(struct inode *inode, struct file *filp,
556588 tmp_inode -> i_nlink = 0 ;
557589
558590 ext4_journal_stop (handle );
559-
560- up_write (& EXT4_I (inode )-> i_data_sem );
591+ mutex_unlock (& (inode -> i_mutex ));
561592
562593 if (tmp_inode )
563594 iput (tmp_inode );
0 commit comments