Skip to content

Commit fa491b1

Browse files
zhangyi089tytso
authored andcommitted
ext4: introduce new metadata buffer read helpers
The previous patch add clear_buffer_verified() before we read metadata block from disk again, but it's rather easy to miss clearing of this bit because currently we read metadata buffer through different open codes (e.g. ll_rw_block(), bh_submit_read() and invoke submit_bh() directly). So, it's time to add common helpers to unify in all the places reading metadata buffers instead. This patch add 3 helpers: - ext4_read_bh_nowait(): async read metadata buffer if it's actually not uptodate, clear buffer_verified bit before read from disk. - ext4_read_bh(): sync version of read metadata buffer, it will wait until the read operation return and check the return status. - ext4_read_bh_lock(): try to lock the buffer before read buffer, it will skip reading if the buffer is already locked. After this patch, we need to use these helpers in all the places reading metadata buffer instead of different open codes. Signed-off-by: zhangyi (F) <yi.zhang@huawei.com> Suggested-by: Jan Kara <jack@suse.cz> Link: https://lore.kernel.org/r/20200924073337.861472-3-yi.zhang@huawei.com Signed-off-by: Theodore Ts'o <tytso@mit.edu>
1 parent d9befed commit fa491b1

2 files changed

Lines changed: 67 additions & 0 deletions

File tree

‎fs/ext4/ext4.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2823,6 +2823,11 @@ extern int ext4_resize_fs(struct super_block *sb, ext4_fsblk_t n_blocks_count);
28232823
/* super.c */
28242824
extern struct buffer_head *ext4_sb_bread(struct super_block *sb,
28252825
sector_t block, int op_flags);
2826+
extern void ext4_read_bh_nowait(struct buffer_head *bh, int op_flags,
2827+
bh_end_io_t *end_io);
2828+
extern int ext4_read_bh(struct buffer_head *bh, int op_flags,
2829+
bh_end_io_t *end_io);
2830+
extern int ext4_read_bh_lock(struct buffer_head *bh, int op_flags, bool wait);
28262831
extern int ext4_seq_options_show(struct seq_file *seq, void *offset);
28272832
extern int ext4_calculate_overhead(struct super_block *sb);
28282833
extern void ext4_superblock_csum_set(struct super_block *sb);

‎fs/ext4/super.c‎

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,68 @@ MODULE_ALIAS_FS("ext3");
141141
MODULE_ALIAS("ext3");
142142
#define IS_EXT3_SB(sb) ((sb)->s_bdev->bd_holder == &ext3_fs_type)
143143

144+
145+
static inline void __ext4_read_bh(struct buffer_head *bh, int op_flags,
146+
bh_end_io_t *end_io)
147+
{
148+
/*
149+
* buffer's verified bit is no longer valid after reading from
150+
* disk again due to write out error, clear it to make sure we
151+
* recheck the buffer contents.
152+
*/
153+
clear_buffer_verified(bh);
154+
155+
bh->b_end_io = end_io ? end_io : end_buffer_read_sync;
156+
get_bh(bh);
157+
submit_bh(REQ_OP_READ, op_flags, bh);
158+
}
159+
160+
void ext4_read_bh_nowait(struct buffer_head *bh, int op_flags,
161+
bh_end_io_t *end_io)
162+
{
163+
BUG_ON(!buffer_locked(bh));
164+
165+
if (ext4_buffer_uptodate(bh)) {
166+
unlock_buffer(bh);
167+
return;
168+
}
169+
__ext4_read_bh(bh, op_flags, end_io);
170+
}
171+
172+
int ext4_read_bh(struct buffer_head *bh, int op_flags, bh_end_io_t *end_io)
173+
{
174+
BUG_ON(!buffer_locked(bh));
175+
176+
if (ext4_buffer_uptodate(bh)) {
177+
unlock_buffer(bh);
178+
return 0;
179+
}
180+
181+
__ext4_read_bh(bh, op_flags, end_io);
182+
183+
wait_on_buffer(bh);
184+
if (buffer_uptodate(bh))
185+
return 0;
186+
return -EIO;
187+
}
188+
189+
int ext4_read_bh_lock(struct buffer_head *bh, int op_flags, bool wait)
190+
{
191+
if (trylock_buffer(bh)) {
192+
if (wait)
193+
return ext4_read_bh(bh, op_flags, NULL);
194+
ext4_read_bh_nowait(bh, op_flags, NULL);
195+
return 0;
196+
}
197+
if (wait) {
198+
wait_on_buffer(bh);
199+
if (buffer_uptodate(bh))
200+
return 0;
201+
return -EIO;
202+
}
203+
return 0;
204+
}
205+
144206
/*
145207
* This works like sb_bread() except it uses ERR_PTR for error
146208
* returns. Currently with sb_bread it's impossible to distinguish

0 commit comments

Comments
 (0)