Commit e6362609 authored by Theodore Ts'o's avatar Theodore Ts'o

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: default avatar"Theodore Ts'o" <tytso@mit.edu>
Cc: "Aneesh Kumar K.V" <aneesh.kumar@linux.vnet.ibm.com>
parent 44338711
...@@ -375,6 +375,12 @@ struct ext4_new_group_data { ...@@ -375,6 +375,12 @@ struct ext4_new_group_data {
#define EXT4_GET_BLOCKS_DIO_CONVERT_EXT (EXT4_GET_BLOCKS_CONVERT|\ #define EXT4_GET_BLOCKS_DIO_CONVERT_EXT (EXT4_GET_BLOCKS_CONVERT|\
EXT4_GET_BLOCKS_DIO_CREATE_EXT) EXT4_GET_BLOCKS_DIO_CREATE_EXT)
/*
* Flags used by ext4_free_blocks
*/
#define EXT4_FREE_BLOCKS_METADATA 0x0001
#define EXT4_FREE_BLOCKS_FORGET 0x0002
/* /*
* ioctl commands * ioctl commands
*/ */
...@@ -1384,8 +1390,8 @@ extern void ext4_discard_preallocations(struct inode *); ...@@ -1384,8 +1390,8 @@ extern void ext4_discard_preallocations(struct inode *);
extern int __init init_ext4_mballoc(void); extern int __init init_ext4_mballoc(void);
extern void exit_ext4_mballoc(void); extern void exit_ext4_mballoc(void);
extern void ext4_free_blocks(handle_t *handle, struct inode *inode, extern void ext4_free_blocks(handle_t *handle, struct inode *inode,
ext4_fsblk_t block, unsigned long count, struct buffer_head *bh, ext4_fsblk_t block,
int metadata); unsigned long count, int flags);
extern int ext4_mb_add_groupinfo(struct super_block *sb, extern int ext4_mb_add_groupinfo(struct super_block *sb,
ext4_group_t i, struct ext4_group_desc *desc); ext4_group_t i, struct ext4_group_desc *desc);
extern int ext4_mb_get_buddy_cache_lock(struct super_block *, ext4_group_t); extern int ext4_mb_get_buddy_cache_lock(struct super_block *, ext4_group_t);
......
...@@ -1007,7 +1007,8 @@ static int ext4_ext_split(handle_t *handle, struct inode *inode, ...@@ -1007,7 +1007,8 @@ static int ext4_ext_split(handle_t *handle, struct inode *inode,
for (i = 0; i < depth; i++) { for (i = 0; i < depth; i++) {
if (!ablocks[i]) if (!ablocks[i])
continue; continue;
ext4_free_blocks(handle, inode, ablocks[i], 1, 1); ext4_free_blocks(handle, inode, 0, ablocks[i], 1,
EXT4_FREE_BLOCKS_METADATA);
} }
} }
kfree(ablocks); kfree(ablocks);
...@@ -1957,7 +1958,6 @@ ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block, ...@@ -1957,7 +1958,6 @@ ext4_ext_in_cache(struct inode *inode, ext4_lblk_t block,
static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode, static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
struct ext4_ext_path *path) struct ext4_ext_path *path)
{ {
struct buffer_head *bh;
int err; int err;
ext4_fsblk_t leaf; ext4_fsblk_t leaf;
...@@ -1973,9 +1973,8 @@ static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode, ...@@ -1973,9 +1973,8 @@ static int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
if (err) if (err)
return err; return err;
ext_debug("index is empty, remove it, free block %llu\n", leaf); ext_debug("index is empty, remove it, free block %llu\n", leaf);
bh = sb_find_get_block(inode->i_sb, leaf); ext4_free_blocks(handle, inode, 0, leaf, 1,
ext4_forget(handle, 1, inode, bh, leaf); EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
ext4_free_blocks(handle, inode, leaf, 1, 1);
return err; return err;
} }
...@@ -2042,12 +2041,11 @@ static int ext4_remove_blocks(handle_t *handle, struct inode *inode, ...@@ -2042,12 +2041,11 @@ static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
struct ext4_extent *ex, struct ext4_extent *ex,
ext4_lblk_t from, ext4_lblk_t to) ext4_lblk_t from, ext4_lblk_t to)
{ {
struct buffer_head *bh;
unsigned short ee_len = ext4_ext_get_actual_len(ex); unsigned short ee_len = ext4_ext_get_actual_len(ex);
int i, metadata = 0; int flags = EXT4_FREE_BLOCKS_FORGET;
if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode)) if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
metadata = 1; flags |= EXT4_FREE_BLOCKS_METADATA;
#ifdef EXTENTS_STATS #ifdef EXTENTS_STATS
{ {
struct ext4_sb_info *sbi = EXT4_SB(inode->i_sb); 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, ...@@ -2072,11 +2070,7 @@ static int ext4_remove_blocks(handle_t *handle, struct inode *inode,
num = le32_to_cpu(ex->ee_block) + ee_len - from; num = le32_to_cpu(ex->ee_block) + ee_len - from;
start = ext_pblock(ex) + ee_len - num; start = ext_pblock(ex) + ee_len - num;
ext_debug("free last %u blocks starting %llu\n", num, start); ext_debug("free last %u blocks starting %llu\n", num, start);
for (i = 0; i < num; i++) { ext4_free_blocks(handle, inode, 0, start, num, flags);
bh = sb_find_get_block(inode->i_sb, start + i);
ext4_forget(handle, metadata, inode, bh, start + i);
}
ext4_free_blocks(handle, inode, start, num, metadata);
} else if (from == le32_to_cpu(ex->ee_block) } else if (from == le32_to_cpu(ex->ee_block)
&& to <= le32_to_cpu(ex->ee_block) + ee_len - 1) { && to <= le32_to_cpu(ex->ee_block) + ee_len - 1) {
printk(KERN_INFO "strange request: removal %u-%u from %u:%u\n", 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, ...@@ -3319,8 +3313,8 @@ int ext4_ext_get_blocks(handle_t *handle, struct inode *inode,
/* not a good idea to call discard here directly, /* not a good idea to call discard here directly,
* but otherwise we'd need to call it every free() */ * but otherwise we'd need to call it every free() */
ext4_discard_preallocations(inode); ext4_discard_preallocations(inode);
ext4_free_blocks(handle, inode, ext_pblock(&newex), ext4_free_blocks(handle, inode, 0, ext_pblock(&newex),
ext4_ext_get_actual_len(&newex), 0); ext4_ext_get_actual_len(&newex), 0);
goto out2; goto out2;
} }
......
...@@ -669,7 +669,7 @@ static int ext4_alloc_blocks(handle_t *handle, struct inode *inode, ...@@ -669,7 +669,7 @@ static int ext4_alloc_blocks(handle_t *handle, struct inode *inode,
return ret; return ret;
failed_out: failed_out:
for (i = 0; i < index; i++) for (i = 0; i < index; i++)
ext4_free_blocks(handle, inode, new_blocks[i], 1, 0); ext4_free_blocks(handle, inode, 0, new_blocks[i], 1, 0);
return ret; return ret;
} }
...@@ -765,20 +765,20 @@ static int ext4_alloc_branch(handle_t *handle, struct inode *inode, ...@@ -765,20 +765,20 @@ static int ext4_alloc_branch(handle_t *handle, struct inode *inode,
return err; return err;
failed: failed:
/* Allocation failed, free what we already allocated */ /* Allocation failed, free what we already allocated */
ext4_free_blocks(handle, inode, 0, new_blocks[0], 1, 0);
for (i = 1; i <= n ; i++) { for (i = 1; i <= n ; i++) {
BUFFER_TRACE(branch[i].bh, "call jbd2_journal_forget");
/* /*
* Note: is_metadata is 0 because branch[i].bh is * branch[i].bh is newly allocated, so there is no
* newly allocated, so there is no need to revoke the * need to revoke the block, which is why we don't
* block. If we do, it's harmless, but not necessary. * need to set EXT4_FREE_BLOCKS_METADATA.
*/ */
ext4_forget(handle, 0, inode, branch[i].bh, ext4_free_blocks(handle, inode, 0, new_blocks[i], 1,
branch[i].bh->b_blocknr); EXT4_FREE_BLOCKS_FORGET);
} }
for (i = 0; i < indirect_blks; i++) for (i = n+1; i < indirect_blks; i++)
ext4_free_blocks(handle, inode, new_blocks[i], 1, 0); ext4_free_blocks(handle, inode, 0, new_blocks[i], 1, 0);
ext4_free_blocks(handle, inode, new_blocks[i], num, 0); ext4_free_blocks(handle, inode, 0, new_blocks[i], num, 0);
return err; return err;
} }
...@@ -857,18 +857,16 @@ static int ext4_splice_branch(handle_t *handle, struct inode *inode, ...@@ -857,18 +857,16 @@ static int ext4_splice_branch(handle_t *handle, struct inode *inode,
err_out: err_out:
for (i = 1; i <= num; i++) { for (i = 1; i <= num; i++) {
BUFFER_TRACE(where[i].bh, "call jbd2_journal_forget");
/* /*
* Note: is_metadata is 0 because branch[i].bh is * branch[i].bh is newly allocated, so there is no
* newly allocated, so there is no need to revoke the * need to revoke the block, which is why we don't
* block. If we do, it's harmless, but not necessary. * need to set EXT4_FREE_BLOCKS_METADATA.
*/ */
ext4_forget(handle, 0, inode, where[i].bh, ext4_free_blocks(handle, inode, where[i].bh, 0, 1,
where[i].bh->b_blocknr); EXT4_FREE_BLOCKS_FORGET);
ext4_free_blocks(handle, inode,
le32_to_cpu(where[i-1].key), 1, 0);
} }
ext4_free_blocks(handle, inode, le32_to_cpu(where[num].key), blks, 0); ext4_free_blocks(handle, inode, 0, le32_to_cpu(where[num].key),
blks, 0);
return err; return err;
} }
...@@ -4080,7 +4078,10 @@ static void ext4_clear_blocks(handle_t *handle, struct inode *inode, ...@@ -4080,7 +4078,10 @@ static void ext4_clear_blocks(handle_t *handle, struct inode *inode,
__le32 *last) __le32 *last)
{ {
__le32 *p; __le32 *p;
int is_metadata = S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode); int flags = EXT4_FREE_BLOCKS_FORGET;
if (S_ISDIR(inode->i_mode) || S_ISLNK(inode->i_mode))
flags |= EXT4_FREE_BLOCKS_METADATA;
if (try_to_extend_transaction(handle, inode)) { if (try_to_extend_transaction(handle, inode)) {
if (bh) { if (bh) {
...@@ -4096,27 +4097,10 @@ static void ext4_clear_blocks(handle_t *handle, struct inode *inode, ...@@ -4096,27 +4097,10 @@ static void ext4_clear_blocks(handle_t *handle, struct inode *inode,
} }
} }
/* for (p = first; p < last; p++)
* Any buffers which are on the journal will be in memory. We *p = 0;
* find them on the hash table so jbd2_journal_revoke() will
* run jbd2_journal_forget() on them. We've already detached
* each block from the file, so bforget() in
* jbd2_journal_forget() should be safe.
*
* AKPM: turn on bforget in jbd2_journal_forget()!!!
*/
for (p = first; p < last; p++) {
u32 nr = le32_to_cpu(*p);
if (nr) {
struct buffer_head *tbh;
*p = 0;
tbh = sb_find_get_block(inode->i_sb, nr);
ext4_forget(handle, is_metadata, inode, tbh, nr);
}
}
ext4_free_blocks(handle, inode, block_to_free, count, is_metadata); ext4_free_blocks(handle, inode, 0, block_to_free, count, flags);
} }
/** /**
...@@ -4304,7 +4288,8 @@ static void ext4_free_branches(handle_t *handle, struct inode *inode, ...@@ -4304,7 +4288,8 @@ static void ext4_free_branches(handle_t *handle, struct inode *inode,
blocks_for_truncate(inode)); blocks_for_truncate(inode));
} }
ext4_free_blocks(handle, inode, nr, 1, 1); ext4_free_blocks(handle, inode, 0, nr, 1,
EXT4_FREE_BLOCKS_METADATA);
if (parent_bh) { if (parent_bh) {
/* /*
......
...@@ -4436,8 +4436,8 @@ ext4_mb_free_metadata(handle_t *handle, struct ext4_buddy *e4b, ...@@ -4436,8 +4436,8 @@ ext4_mb_free_metadata(handle_t *handle, struct ext4_buddy *e4b,
* @metadata: Are these metadata blocks * @metadata: Are these metadata blocks
*/ */
void ext4_free_blocks(handle_t *handle, struct inode *inode, void ext4_free_blocks(handle_t *handle, struct inode *inode,
ext4_fsblk_t block, unsigned long count, struct buffer_head *bh, ext4_fsblk_t block,
int metadata) unsigned long count, int flags)
{ {
struct buffer_head *bitmap_bh = NULL; struct buffer_head *bitmap_bh = NULL;
struct super_block *sb = inode->i_sb; struct super_block *sb = inode->i_sb;
...@@ -4454,15 +4454,12 @@ void ext4_free_blocks(handle_t *handle, struct inode *inode, ...@@ -4454,15 +4454,12 @@ void ext4_free_blocks(handle_t *handle, struct inode *inode,
int err = 0; int err = 0;
int ret; int ret;
/* if (bh) {
* We need to make sure we don't reuse the freed block until if (block)
* after the transaction is committed, which we can do by BUG_ON(block != bh->b_blocknr);
* treating the block as metadata, below. We make an else
* exception if the inode is to be written in writeback mode block = bh->b_blocknr;
* since writeback mode has weak data consistency guarantees. }
*/
if (!ext4_should_writeback_data(inode))
metadata = 1;
sbi = EXT4_SB(sb); sbi = EXT4_SB(sb);
es = EXT4_SB(sb)->s_es; es = EXT4_SB(sb)->s_es;
...@@ -4476,7 +4473,32 @@ void ext4_free_blocks(handle_t *handle, struct inode *inode, ...@@ -4476,7 +4473,32 @@ void ext4_free_blocks(handle_t *handle, struct inode *inode,
} }
ext4_debug("freeing block %llu\n", block); ext4_debug("freeing block %llu\n", block);
trace_ext4_free_blocks(inode, block, count, metadata); trace_ext4_free_blocks(inode, block, count, flags);
if (flags & EXT4_FREE_BLOCKS_FORGET) {
struct buffer_head *tbh = bh;
int i;
BUG_ON(bh && (count > 1));
for (i = 0; i < count; i++) {
if (!bh)
tbh = sb_find_get_block(inode->i_sb,
block + i);
ext4_forget(handle, flags & EXT4_FREE_BLOCKS_METADATA,
inode, tbh, block + i);
}
}
/*
* We need to make sure we don't reuse the freed block until
* after the transaction is committed, which we can do by
* treating the block as metadata, below. We make an
* exception if the inode is to be written in writeback mode
* since writeback mode has weak data consistency guarantees.
*/
if (!ext4_should_writeback_data(inode))
flags |= EXT4_FREE_BLOCKS_METADATA;
ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS); ac = kmem_cache_alloc(ext4_ac_cachep, GFP_NOFS);
if (ac) { if (ac) {
...@@ -4552,7 +4574,8 @@ void ext4_free_blocks(handle_t *handle, struct inode *inode, ...@@ -4552,7 +4574,8 @@ void ext4_free_blocks(handle_t *handle, struct inode *inode,
err = ext4_mb_load_buddy(sb, block_group, &e4b); err = ext4_mb_load_buddy(sb, block_group, &e4b);
if (err) if (err)
goto error_return; goto error_return;
if (metadata && ext4_handle_valid(handle)) {
if ((flags & EXT4_FREE_BLOCKS_METADATA) && ext4_handle_valid(handle)) {
struct ext4_free_data *new_entry; struct ext4_free_data *new_entry;
/* /*
* blocks being freed are metadata. these blocks shouldn't * blocks being freed are metadata. these blocks shouldn't
......
...@@ -262,13 +262,17 @@ static int free_dind_blocks(handle_t *handle, ...@@ -262,13 +262,17 @@ static int free_dind_blocks(handle_t *handle,
for (i = 0; i < max_entries; i++) { for (i = 0; i < max_entries; i++) {
if (tmp_idata[i]) { if (tmp_idata[i]) {
extend_credit_for_blkdel(handle, inode); extend_credit_for_blkdel(handle, inode);
ext4_free_blocks(handle, inode, ext4_free_blocks(handle, inode, 0,
le32_to_cpu(tmp_idata[i]), 1, 1); le32_to_cpu(tmp_idata[i]), 1,
EXT4_FREE_BLOCKS_METADATA |
EXT4_FREE_BLOCKS_FORGET);
} }
} }
put_bh(bh); put_bh(bh);
extend_credit_for_blkdel(handle, inode); extend_credit_for_blkdel(handle, inode);
ext4_free_blocks(handle, inode, le32_to_cpu(i_data), 1, 1); ext4_free_blocks(handle, inode, 0, le32_to_cpu(i_data), 1,
EXT4_FREE_BLOCKS_METADATA |
EXT4_FREE_BLOCKS_FORGET);
return 0; return 0;
} }
...@@ -297,7 +301,9 @@ static int free_tind_blocks(handle_t *handle, ...@@ -297,7 +301,9 @@ static int free_tind_blocks(handle_t *handle,
} }
put_bh(bh); put_bh(bh);
extend_credit_for_blkdel(handle, inode); extend_credit_for_blkdel(handle, inode);
ext4_free_blocks(handle, inode, le32_to_cpu(i_data), 1, 1); ext4_free_blocks(handle, inode, 0, le32_to_cpu(i_data), 1,
EXT4_FREE_BLOCKS_METADATA |
EXT4_FREE_BLOCKS_FORGET);
return 0; return 0;
} }
...@@ -308,8 +314,10 @@ static int free_ind_block(handle_t *handle, struct inode *inode, __le32 *i_data) ...@@ -308,8 +314,10 @@ static int free_ind_block(handle_t *handle, struct inode *inode, __le32 *i_data)
/* ei->i_data[EXT4_IND_BLOCK] */ /* ei->i_data[EXT4_IND_BLOCK] */
if (i_data[0]) { if (i_data[0]) {
extend_credit_for_blkdel(handle, inode); extend_credit_for_blkdel(handle, inode);
ext4_free_blocks(handle, inode, ext4_free_blocks(handle, inode, 0,
le32_to_cpu(i_data[0]), 1, 1); le32_to_cpu(i_data[0]), 1,
EXT4_FREE_BLOCKS_METADATA |
EXT4_FREE_BLOCKS_FORGET);
} }
/* ei->i_data[EXT4_DIND_BLOCK] */ /* ei->i_data[EXT4_DIND_BLOCK] */
...@@ -419,7 +427,8 @@ static int free_ext_idx(handle_t *handle, struct inode *inode, ...@@ -419,7 +427,8 @@ static int free_ext_idx(handle_t *handle, struct inode *inode,
} }
put_bh(bh); put_bh(bh);
extend_credit_for_blkdel(handle, inode); extend_credit_for_blkdel(handle, inode);
ext4_free_blocks(handle, inode, block, 1, 1); ext4_free_blocks(handle, inode, 0, block, 1,
EXT4_FREE_BLOCKS_METADATA | EXT4_FREE_BLOCKS_FORGET);
return retval; return retval;
} }
......
...@@ -482,9 +482,10 @@ ext4_xattr_release_block(handle_t *handle, struct inode *inode, ...@@ -482,9 +482,10 @@ ext4_xattr_release_block(handle_t *handle, struct inode *inode,
ea_bdebug(bh, "refcount now=0; freeing"); ea_bdebug(bh, "refcount now=0; freeing");
if (ce) if (ce)
mb_cache_entry_free(ce); mb_cache_entry_free(ce);
ext4_free_blocks(handle, inode, bh->b_blocknr, 1, 1);
get_bh(bh); get_bh(bh);
ext4_forget(handle, 1, inode, bh, bh->b_blocknr); ext4_free_blocks(handle, inode, bh, 0, 1,
EXT4_FREE_BLOCKS_METADATA |
EXT4_FREE_BLOCKS_FORGET);
} else { } else {
le32_add_cpu(&BHDR(bh)->h_refcount, -1); le32_add_cpu(&BHDR(bh)->h_refcount, -1);
error = ext4_handle_dirty_metadata(handle, inode, bh); error = ext4_handle_dirty_metadata(handle, inode, bh);
...@@ -832,7 +833,8 @@ ext4_xattr_block_set(handle_t *handle, struct inode *inode, ...@@ -832,7 +833,8 @@ ext4_xattr_block_set(handle_t *handle, struct inode *inode,
new_bh = sb_getblk(sb, block); new_bh = sb_getblk(sb, block);
if (!new_bh) { if (!new_bh) {
getblk_failed: getblk_failed:
ext4_free_blocks(handle, inode, block, 1, 1); ext4_free_blocks(handle, inode, 0, block, 1,
EXT4_FREE_BLOCKS_METADATA);
error = -EIO; error = -EIO;
goto cleanup; goto cleanup;
} }
......
...@@ -650,30 +650,32 @@ TRACE_EVENT(ext4_allocate_blocks, ...@@ -650,30 +650,32 @@ TRACE_EVENT(ext4_allocate_blocks,
TRACE_EVENT(ext4_free_blocks, TRACE_EVENT(ext4_free_blocks,
TP_PROTO(struct inode *inode, __u64 block, unsigned long count, TP_PROTO(struct inode *inode, __u64 block, unsigned long count,
int metadata), int flags),
TP_ARGS(inode, block, count, metadata), TP_ARGS(inode, block, count, flags),
TP_STRUCT__entry( TP_STRUCT__entry(
__field( dev_t, dev ) __field( dev_t, dev )
__field( ino_t, ino ) __field( ino_t, ino )
__field( umode_t, mode )
__field( __u64, block ) __field( __u64, block )
__field( unsigned long, count ) __field( unsigned long, count )
__field( int, metadata ) __field( int, flags )
), ),
TP_fast_assign( TP_fast_assign(
__entry->dev = inode->i_sb->s_dev; __entry->dev = inode->i_sb->s_dev;
__entry->ino = inode->i_ino; __entry->ino = inode->i_ino;
__entry->mode = inode->i_mode;
__entry->block = block; __entry->block = block;
__entry->count = count; __entry->count = count;
__entry->metadata = metadata; __entry->flags = flags;
), ),
TP_printk("dev %s ino %lu block %llu count %lu metadata %d", TP_printk("dev %s ino %lu mode 0%o block %llu count %lu flags %d",
jbd2_dev_to_name(__entry->dev), (unsigned long) __entry->ino, jbd2_dev_to_name(__entry->dev), (unsigned long) __entry->ino,
__entry->block, __entry->count, __entry->metadata) __entry->mode, __entry->block, __entry->count,
__entry->flags)
); );
TRACE_EVENT(ext4_sync_file, TRACE_EVENT(ext4_sync_file,
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment