Skip to content

Commit e636260

Browse files
committed
ext4: call ext4_forget() from ext4_free_blocks()
Add the facility for ext4_forget() to be called from ext4_free_blocks(). This simplifies the code in a large number of places, and centralizes most of the work of calling ext4_forget() into a single place. Also fix a bug in the extents migration code; it wasn't calling ext4_forget() when releasing the indirect blocks during the conversion. As a result, if the system cashed during or shortly after the extents migration, and the released indirect blocks get reused as data blocks, the journal replay would corrupt the data blocks. With this new patch, fixing this bug was as simple as adding the EXT4_FREE_BLOCKS_FORGET flags to the call to ext4_free_blocks(). Signed-off-by: "Theodore Ts'o" <tytso@mit.edu> Cc: "Aneesh Kumar K.V" <aneesh.kumar@linux.vnet.ibm.com>
1 parent 4433871 commit e636260

7 files changed

Lines changed: 109 additions & 88 deletions

File tree

‎fs/ext4/ext4.h‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,12 @@ struct ext4_new_group_data {
375375
#define EXT4_GET_BLOCKS_DIO_CONVERT_EXT (EXT4_GET_BLOCKS_CONVERT|\
376376
EXT4_GET_BLOCKS_DIO_CREATE_EXT)
377377

378+
/*
379+
* Flags used by ext4_free_blocks
380+
*/
381+
#define EXT4_FREE_BLOCKS_METADATA 0x0001
382+
#define EXT4_FREE_BLOCKS_FORGET 0x0002
383+
378384
/*
379385
* ioctl commands
380386
*/
@@ -1384,8 +1390,8 @@ extern void ext4_discard_preallocations(struct inode *);
13841390
extern int __init init_ext4_mballoc(void);
13851391
extern void exit_ext4_mballoc(void);
13861392
extern void ext4_free_blocks(handle_t *handle, struct inode *inode,
1387-
ext4_fsblk_t block, unsigned long count,
1388-
int metadata);
1393+
struct buffer_head *bh, ext4_fsblk_t block,
1394+
unsigned long count, int flags);
13891395
extern int ext4_mb_add_groupinfo(struct super_block *sb,
13901396
ext4_group_t i, struct ext4_group_desc *desc);
13911397
extern int ext4_mb_get_buddy_cache_lock(struct super_block *, ext4_group_t);

‎fs/ext4/extents.c‎

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,7 +1007,8 @@ static int ext4_ext_split(handle_t *handle, struct inode *inode,
10071007
for (i = 0; i < depth; i++) {
10081008
if (!ablocks[i])
10091009
continue;
1010-
ext4_free_blocks(handle, inode, ablocks[i], 1, 1);
1010+
ext4_free_blocks(handle, inode, 0, ablocks[i], 1,
1011+
EXT4_FREE_BLOCKS_METADATA);
10111012
}
10121013
}
10131014
kfree(ablocks);
@@ -1957,7 +1958,6 @@ ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block,
19571958
static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
19581959
struct ext4_ext_path *path)
19591960
{
1960-
struct buffer_head *bh;
19611961
int err;
19621962
ext4_fsblk_t leaf;
19631963

@@ -1973,9 +1973,8 @@ static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
19731973
if (err)
19741974
return err;
19751975
ext_debug("index is empty, remove it, free block %llu\n", leaf);
1976-
bh = sb_find_get_block(inode->i_sb, leaf);
1977-
ext4_forget(handle, 1, inode, bh, leaf);
1978-
ext4_free_blocks(handle, inode, leaf, 1, 1);
1976+
ext4_free_blocks(handle, inode, 0, leaf, 1,
1977+
EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
19791978
return err;
19801979
}
19811980

@@ -2042,12 +2041,11 @@ static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
20422041
struct ext4_extent *ex,
20432042
ext4_lblk_t from, ext4_lblk_t to)
20442043
{
2045-
struct buffer_head *bh;
20462044
unsigned short ee_len = ext4_ext_get_actual_len(ex);
2047-
int i, metadata = 0;
2045+
int flags = EXT4_FREE_BLOCKS_FORGET;
20482046

20492047
if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
2050-
metadata = 1;
2048+
flags |= EXT4_FREE_BLOCKS_METADATA;
20512049
#ifdef EXTENTS_STATS
20522050
{
20532051
struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb);
@@ -2072,11 +2070,7 @@ static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
20722070
num = le32_to_cpu(ex->ee_block) + ee_len - from;
20732071
start = ext_pblock(ex) + ee_len - num;
20742072
ext_debug("free last %u blocks starting %llu\n", num, start);
2075-
for (i = 0; i < num; i++) {
2076-
bh = sb_find_get_block(inode->i_sb, start + i);
2077-
ext4_forget(handle, metadata, inode, bh, start + i);
2078-
}
2079-
ext4_free_blocks(handle, inode, start, num, metadata);
2073+
ext4_free_blocks(handle, inode, 0, start, num, flags);
20802074
} else if (from == le32_to_cpu(ex->ee_block)
20812075
&& to <= le32_to_cpu(ex->ee_block) + ee_len - 1) {
20822076
printk(KERN_INFO "strange request: removal %u-%u from %u:%u\n",
@@ -3319,8 +3313,8 @@ int ext4_ext_get_blocks(handle_t *handle, struct inode *inode,
33193313
/* not a good idea to call discard here directly,
33203314
* but otherwise we'd need to call it every free() */
33213315
ext4_discard_preallocations(inode);
3322-
ext4_free_blocks(handle, inode, ext_pblock(&newex),
3323-
ext4_ext_get_actual_len(&newex), 0);
3316+
ext4_free_blocks(handle, inode, 0, ext_pblock(&newex),
3317+
ext4_ext_get_actual_len(&newex), 0);
33243318
goto out2;
33253319
}
33263320

‎fs/ext4/inode.c‎

Lines changed: 26 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,7 @@ static int ext4_alloc_blocks(handle_t *handle, struct inode *inode,
669669
return ret;
670670
failed_out:
671671
for (i = 0; i < index; i++)
672-
ext4_free_blocks(handle, inode, new_blocks[i], 1, 0);
672+
ext4_free_blocks(handle, inode, 0, new_blocks[i], 1, 0);
673673
return ret;
674674
}
675675

@@ -765,20 +765,20 @@ static int ext4_alloc_branch(handle_t *handle, struct inode *inode,
765765
return err;
766766
failed:
767767
/* Allocation failed, free what we already allocated */
768+
ext4_free_blocks(handle, inode, 0, new_blocks[0], 1, 0);
768769
for (i = 1; i <= n ; i++) {
769-
BUFFER_TRACE(branch[i].bh, "call jbd2_journal_forget");
770770
/*
771-
* Note: is_metadata is 0 because branch[i].bh is
772-
* newly allocated, so there is no need to revoke the
773-
* block. If we do, it's harmless, but not necessary.
771+
* branch[i].bh is newly allocated, so there is no
772+
* need to revoke the block, which is why we don't
773+
* need to set EXT4_FREE_BLOCKS_METADATA.
774774
*/
775-
ext4_forget(handle, 0, inode, branch[i].bh,
776-
branch[i].bh->b_blocknr);
775+
ext4_free_blocks(handle, inode, 0, new_blocks[i], 1,
776+
EXT4_FREE_BLOCKS_FORGET);
777777
}
778-
for (i = 0; i < indirect_blks; i++)
779-
ext4_free_blocks(handle, inode, new_blocks[i], 1, 0);
778+
for (i = n+1; i < indirect_blks; i++)
779+
ext4_free_blocks(handle, inode, 0, new_blocks[i], 1, 0);
780780

781-
ext4_free_blocks(handle, inode, new_blocks[i], num, 0);
781+
ext4_free_blocks(handle, inode, 0, new_blocks[i], num, 0);
782782

783783
return err;
784784
}
@@ -857,18 +857,16 @@ static int ext4_splice_branch(handle_t *handle, struct inode *inode,
857857

858858
err_out:
859859
for (i = 1; i <= num; i++) {
860-
BUFFER_TRACE(where[i].bh, "call jbd2_journal_forget");
861860
/*
862-
* Note: is_metadata is 0 because branch[i].bh is
863-
* newly allocated, so there is no need to revoke the
864-
* block. If we do, it's harmless, but not necessary.
861+
* branch[i].bh is newly allocated, so there is no
862+
* need to revoke the block, which is why we don't
863+
* need to set EXT4_FREE_BLOCKS_METADATA.
865864
*/
866-
ext4_forget(handle, 0, inode, where[i].bh,
867-
where[i].bh->b_blocknr);
868-
ext4_free_blocks(handle, inode,
869-
le32_to_cpu(where[i-1].key), 1, 0);
865+
ext4_free_blocks(handle, inode, where[i].bh, 0, 1,
866+
EXT4_FREE_BLOCKS_FORGET);
870867
}
871-
ext4_free_blocks(handle, inode, le32_to_cpu(where[num].key), blks, 0);
868+
ext4_free_blocks(handle, inode, 0, le32_to_cpu(where[num].key),
869+
blks, 0);
872870

873871
return err;
874872
}
@@ -4080,7 +4078,10 @@ static void ext4_clear_blocks(handle_t *handle, struct inode *inode,
40804078
__le32 *last)
40814079
{
40824080
__le32 *p;
4083-
int is_metadata = S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode);
4081+
int flags = EXT4_FREE_BLOCKS_FORGET;
4082+
4083+
if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
4084+
flags |= EXT4_FREE_BLOCKS_METADATA;
40844085

40854086
if (try_to_extend_transaction(handle, inode)) {
40864087
if (bh) {
@@ -4096,27 +4097,10 @@ static void ext4_clear_blocks(handle_t *handle, struct inode *inode,
40964097
}
40974098
}
40984099

4099-
/*
4100-
* Any buffers which are on the journal will be in memory. We
4101-
* find them on the hash table so jbd2_journal_revoke() will
4102-
* run jbd2_journal_forget() on them. We've already detached
4103-
* each block from the file, so bforget() in
4104-
* jbd2_journal_forget() should be safe.
4105-
*
4106-
* AKPM: turn on bforget in jbd2_journal_forget()!!!
4107-
*/
4108-
for (p = first; p < last; p++) {
4109-
u32 nr = le32_to_cpu(*p);
4110-
if (nr) {
4111-
struct buffer_head *tbh;
4112-
4113-
*p = 0;
4114-
tbh = sb_find_get_block(inode->i_sb, nr);
4115-
ext4_forget(handle, is_metadata, inode, tbh, nr);
4116-
}
4117-
}
4100+
for (p = first; p < last; p++)
4101+
*p = 0;
41184102

4119-
ext4_free_blocks(handle, inode, block_to_free, count, is_metadata);
4103+
ext4_free_blocks(handle, inode, 0, block_to_free, count, flags);
41204104
}
41214105

41224106
/**
@@ -4304,7 +4288,8 @@ static void ext4_free_branches(handle_t *handle, struct inode *inode,
43044288
blocks_for_truncate(inode));
43054289
}
43064290

4307-
ext4_free_blocks(handle, inode, nr, 1, 1);
4291+
ext4_free_blocks(handle, inode, 0, nr, 1,
4292+
EXT4_FREE_BLOCKS_METADATA);
43084293

43094294
if (parent_bh) {
43104295
/*

‎fs/ext4/mballoc.c‎

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4436,8 +4436,8 @@ ext4_mb_free_metadata(handle_t *handle, struct ext4_buddy *e4b,
44364436
* @metadata: Are these metadata blocks
44374437
*/
44384438
void ext4_free_blocks(handle_t *handle, struct inode *inode,
4439-
ext4_fsblk_t block, unsigned long count,
4440-
int metadata)
4439+
struct buffer_head *bh, ext4_fsblk_t block,
4440+
unsigned long count, int flags)
44414441
{
44424442
struct buffer_head *bitmap_bh = NULL;
44434443
struct super_block *sb = inode->i_sb;
@@ -4454,15 +4454,12 @@ void ext4_free_blocks(handle_t *handle, struct inode *inode,
44544454
int err = 0;
44554455
int ret;
44564456

4457-
/*
4458-
* We need to make sure we don't reuse the freed block until
4459-
* after the transaction is committed, which we can do by
4460-
* treating the block as metadata, below. We make an
4461-
* exception if the inode is to be written in writeback mode
4462-
* since writeback mode has weak data consistency guarantees.
4463-
*/
4464-
if (!ext4_should_writeback_data(inode))
4465-
metadata = 1;
4457+
if (bh) {
4458+
if (block)
4459+
BUG_ON(block != bh->b_blocknr);
4460+
else
4461+
block = bh->b_blocknr;
4462+
}
44664463

44674464
sbi = EXT4_SB(sb);
44684465
es = EXT4_SB(sb)->s_es;
@@ -4476,7 +4473,32 @@ void ext4_free_blocks(handle_t *handle, struct inode *inode,
44764473
}
44774474

44784475
ext4_debug("freeing block %llu\n", block);
4479-
trace_ext4_free_blocks(inode, block, count, metadata);
4476+
trace_ext4_free_blocks(inode, block, count, flags);
4477+
4478+
if (flags & EXT4_FREE_BLOCKS_FORGET) {
4479+
struct buffer_head *tbh = bh;
4480+
int i;
4481+
4482+
BUG_ON(bh && (count > 1));
4483+
4484+
for (i = 0; i < count; i++) {
4485+
if (!bh)
4486+
tbh = sb_find_get_block(inode->i_sb,
4487+
block + i);
4488+
ext4_forget(handle, flags & EXT4_FREE_BLOCKS_METADATA,
4489+
inode, tbh, block + i);
4490+
}
4491+
}
4492+
4493+
/*
4494+
* We need to make sure we don't reuse the freed block until
4495+
* after the transaction is committed, which we can do by
4496+
* treating the block as metadata, below. We make an
4497+
* exception if the inode is to be written in writeback mode
4498+
* since writeback mode has weak data consistency guarantees.
4499+
*/
4500+
if (!ext4_should_writeback_data(inode))
4501+
flags |= EXT4_FREE_BLOCKS_METADATA;
44804502

44814503
ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
44824504
if (ac) {
@@ -4552,7 +4574,8 @@ void ext4_free_blocks(handle_t *handle, struct inode *inode,
45524574
err = ext4_mb_load_buddy(sb, block_group, &e4b);
45534575
if (err)
45544576
goto error_return;
4555-
if (metadata && ext4_handle_valid(handle)) {
4577+
4578+
if ((flags & EXT4_FREE_BLOCKS_METADATA) && ext4_handle_valid(handle)) {
45564579
struct ext4_free_data *new_entry;
45574580
/*
45584581
* blocks being freed are metadata. these blocks shouldn't

‎fs/ext4/migrate.c‎

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -262,13 +262,17 @@ static int free_dind_blocks(handle_t *handle,
262262
for (i = 0; i < max_entries; i++) {
263263
if (tmp_idata[i]) {
264264
extend_credit_for_blkdel(handle, inode);
265-
ext4_free_blocks(handle, inode,
266-
le32_to_cpu(tmp_idata[i]), 1, 1);
265+
ext4_free_blocks(handle, inode, 0,
266+
le32_to_cpu(tmp_idata[i]), 1,
267+
EXT4_FREE_BLOCKS_METADATA |
268+
EXT4_FREE_BLOCKS_FORGET);
267269
}
268270
}
269271
put_bh(bh);
270272
extend_credit_for_blkdel(handle, inode);
271-
ext4_free_blocks(handle, inode, le32_to_cpu(i_data), 1, 1);
273+
ext4_free_blocks(handle, inode, 0, le32_to_cpu(i_data), 1,
274+
EXT4_FREE_BLOCKS_METADATA |
275+
EXT4_FREE_BLOCKS_FORGET);
272276
return 0;
273277
}
274278

@@ -297,7 +301,9 @@ static int free_tind_blocks(handle_t *handle,
297301
}
298302
put_bh(bh);
299303
extend_credit_for_blkdel(handle, inode);
300-
ext4_free_blocks(handle, inode, le32_to_cpu(i_data), 1, 1);
304+
ext4_free_blocks(handle, inode, 0, le32_to_cpu(i_data), 1,
305+
EXT4_FREE_BLOCKS_METADATA |
306+
EXT4_FREE_BLOCKS_FORGET);
301307
return 0;
302308
}
303309

@@ -308,8 +314,10 @@ static int free_ind_block(handle_t *handle, struct inode *inode, __le32 *i_data)
308314
/* ei->i_data[EXT4_IND_BLOCK] */
309315
if (i_data[0]) {
310316
extend_credit_for_blkdel(handle, inode);
311-
ext4_free_blocks(handle, inode,
312-
le32_to_cpu(i_data[0]), 1, 1);
317+
ext4_free_blocks(handle, inode, 0,
318+
le32_to_cpu(i_data[0]), 1,
319+
EXT4_FREE_BLOCKS_METADATA |
320+
EXT4_FREE_BLOCKS_FORGET);
313321
}
314322

315323
/* ei->i_data[EXT4_DIND_BLOCK] */
@@ -419,7 +427,8 @@ static int free_ext_idx(handle_t *handle, struct inode *inode,
419427
}
420428
put_bh(bh);
421429
extend_credit_for_blkdel(handle, inode);
422-
ext4_free_blocks(handle, inode, block, 1, 1);
430+
ext4_free_blocks(handle, inode, 0, block, 1,
431+
EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
423432
return retval;
424433
}
425434

‎fs/ext4/xattr.c‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -482,9 +482,10 @@ ext4_xattr_release_block(handle_t *handle, struct inode *inode,
482482
ea_bdebug(bh, "refcount now=0; freeing");
483483
if (ce)
484484
mb_cache_entry_free(ce);
485-
ext4_free_blocks(handle, inode, bh->b_blocknr, 1, 1);
486485
get_bh(bh);
487-
ext4_forget(handle, 1, inode, bh, bh->b_blocknr);
486+
ext4_free_blocks(handle, inode, bh, 0, 1,
487+
EXT4_FREE_BLOCKS_METADATA |
488+
EXT4_FREE_BLOCKS_FORGET);
488489
} else {
489490
le32_add_cpu(&BHDR(bh)->h_refcount, -1);
490491
error = ext4_handle_dirty_metadata(handle, inode, bh);
@@ -832,7 +833,8 @@ ext4_xattr_block_set(handle_t *handle, struct inode *inode,
832833
new_bh = sb_getblk(sb, block);
833834
if (!new_bh) {
834835
getblk_failed:
835-
ext4_free_blocks(handle, inode, block, 1, 1);
836+
ext4_free_blocks(handle, inode, 0, block, 1,
837+
EXT4_FREE_BLOCKS_METADATA);
836838
error = -EIO;
837839
goto cleanup;
838840
}

0 commit comments

Comments
 (0)