Skip to content

Commit a1e09b0

Browse files
ebiggersJaegeuk Kim
authored andcommitted
f2fs: use iomap for direct I/O
Make f2fs_file_read_iter() and f2fs_file_write_iter() use the iomap direct I/O implementation instead of the fs/direct-io.c one. The iomap implementation is more efficient, and it also avoids the need to add new features and optimizations to the old implementation. This new implementation also eliminates the need for f2fs to hook bio submission and completion and to allocate memory per-bio. This is because it's possible to correctly update f2fs's in-flight DIO counters using __iomap_dio_rw() in combination with an implementation of iomap_dio_ops::end_io() (as suggested by Christoph Hellwig). When possible, this new implementation preserves existing f2fs behavior such as the conditions for falling back to buffered I/O. This patch has been tested with xfstests by running 'gce-xfstests -c f2fs -g auto -X generic/017' with and without this patch; no regressions were seen. (Some tests fail both before and after. generic/017 hangs both before and after, so it had to be excluded.) Signed-off-by: Eric Biggers <[email protected]> [Jaegeuk Kim: use spin_lock_bh for f2fs_update_iostat in softirq] Signed-off-by: Jaegeuk Kim <[email protected]>
1 parent 1517c1a commit a1e09b0

File tree

4 files changed

+322
-273
lines changed

4 files changed

+322
-273
lines changed

fs/f2fs/data.c

Lines changed: 2 additions & 203 deletions
Original file line numberDiff line numberDiff line change
@@ -1377,11 +1377,6 @@ static int __allocate_data_block(struct dnode_of_data *dn, int seg_type)
13771377
f2fs_invalidate_compress_page(sbi, old_blkaddr);
13781378
}
13791379
f2fs_update_data_blkaddr(dn, dn->data_blkaddr);
1380-
1381-
/*
1382-
* i_size will be updated by direct_IO. Otherwise, we'll get stale
1383-
* data from unwritten block via dio_read.
1384-
*/
13851380
return 0;
13861381
}
13871382

@@ -1743,50 +1738,6 @@ static inline u64 blks_to_bytes(struct inode *inode, u64 blks)
17431738
return (blks << inode->i_blkbits);
17441739
}
17451740

1746-
static int __get_data_block(struct inode *inode, sector_t iblock,
1747-
struct buffer_head *bh, int create, int flag,
1748-
pgoff_t *next_pgofs, int seg_type, bool may_write)
1749-
{
1750-
struct f2fs_map_blocks map;
1751-
int err;
1752-
1753-
map.m_lblk = iblock;
1754-
map.m_len = bytes_to_blks(inode, bh->b_size);
1755-
map.m_next_pgofs = next_pgofs;
1756-
map.m_next_extent = NULL;
1757-
map.m_seg_type = seg_type;
1758-
map.m_may_create = may_write;
1759-
1760-
err = f2fs_map_blocks(inode, &map, create, flag);
1761-
if (!err) {
1762-
map_bh(bh, inode->i_sb, map.m_pblk);
1763-
bh->b_state = (bh->b_state & ~F2FS_MAP_FLAGS) | map.m_flags;
1764-
bh->b_size = blks_to_bytes(inode, map.m_len);
1765-
1766-
if (map.m_multidev_dio)
1767-
bh->b_bdev = map.m_bdev;
1768-
}
1769-
return err;
1770-
}
1771-
1772-
static int get_data_block_dio_write(struct inode *inode, sector_t iblock,
1773-
struct buffer_head *bh_result, int create)
1774-
{
1775-
return __get_data_block(inode, iblock, bh_result, create,
1776-
F2FS_GET_BLOCK_DIO, NULL,
1777-
f2fs_rw_hint_to_seg_type(inode->i_write_hint),
1778-
true);
1779-
}
1780-
1781-
static int get_data_block_dio(struct inode *inode, sector_t iblock,
1782-
struct buffer_head *bh_result, int create)
1783-
{
1784-
return __get_data_block(inode, iblock, bh_result, create,
1785-
F2FS_GET_BLOCK_DIO, NULL,
1786-
f2fs_rw_hint_to_seg_type(inode->i_write_hint),
1787-
false);
1788-
}
1789-
17901741
static int f2fs_xattr_fiemap(struct inode *inode,
17911742
struct fiemap_extent_info *fieinfo)
17921743
{
@@ -3263,7 +3214,7 @@ static int f2fs_write_data_pages(struct address_space *mapping,
32633214
FS_CP_DATA_IO : FS_DATA_IO);
32643215
}
32653216

3266-
static void f2fs_write_failed(struct inode *inode, loff_t to)
3217+
void f2fs_write_failed(struct inode *inode, loff_t to)
32673218
{
32683219
loff_t i_size = i_size_read(inode);
32693220

@@ -3551,158 +3502,6 @@ static int f2fs_write_end(struct file *file,
35513502
return copied;
35523503
}
35533504

3554-
static int check_direct_IO(struct inode *inode, struct iov_iter *iter,
3555-
loff_t offset)
3556-
{
3557-
unsigned i_blkbits = READ_ONCE(inode->i_blkbits);
3558-
unsigned blkbits = i_blkbits;
3559-
unsigned blocksize_mask = (1 << blkbits) - 1;
3560-
unsigned long align = offset | iov_iter_alignment(iter);
3561-
struct block_device *bdev = inode->i_sb->s_bdev;
3562-
3563-
if (iov_iter_rw(iter) == READ && offset >= i_size_read(inode))
3564-
return 1;
3565-
3566-
if (align & blocksize_mask) {
3567-
if (bdev)
3568-
blkbits = blksize_bits(bdev_logical_block_size(bdev));
3569-
blocksize_mask = (1 << blkbits) - 1;
3570-
if (align & blocksize_mask)
3571-
return -EINVAL;
3572-
return 1;
3573-
}
3574-
return 0;
3575-
}
3576-
3577-
static void f2fs_dio_end_io(struct bio *bio)
3578-
{
3579-
struct f2fs_private_dio *dio = bio->bi_private;
3580-
3581-
dec_page_count(F2FS_I_SB(dio->inode),
3582-
dio->write ? F2FS_DIO_WRITE : F2FS_DIO_READ);
3583-
3584-
bio->bi_private = dio->orig_private;
3585-
bio->bi_end_io = dio->orig_end_io;
3586-
3587-
kfree(dio);
3588-
3589-
bio_endio(bio);
3590-
}
3591-
3592-
static void f2fs_dio_submit_bio(struct bio *bio, struct inode *inode,
3593-
loff_t file_offset)
3594-
{
3595-
struct f2fs_private_dio *dio;
3596-
bool write = (bio_op(bio) == REQ_OP_WRITE);
3597-
3598-
dio = f2fs_kzalloc(F2FS_I_SB(inode),
3599-
sizeof(struct f2fs_private_dio), GFP_NOFS);
3600-
if (!dio)
3601-
goto out;
3602-
3603-
dio->inode = inode;
3604-
dio->orig_end_io = bio->bi_end_io;
3605-
dio->orig_private = bio->bi_private;
3606-
dio->write = write;
3607-
3608-
bio->bi_end_io = f2fs_dio_end_io;
3609-
bio->bi_private = dio;
3610-
3611-
inc_page_count(F2FS_I_SB(inode),
3612-
write ? F2FS_DIO_WRITE : F2FS_DIO_READ);
3613-
3614-
submit_bio(bio);
3615-
return;
3616-
out:
3617-
bio->bi_status = BLK_STS_IOERR;
3618-
bio_endio(bio);
3619-
}
3620-
3621-
static ssize_t f2fs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
3622-
{
3623-
struct address_space *mapping = iocb->ki_filp->f_mapping;
3624-
struct inode *inode = mapping->host;
3625-
struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
3626-
struct f2fs_inode_info *fi = F2FS_I(inode);
3627-
size_t count = iov_iter_count(iter);
3628-
loff_t offset = iocb->ki_pos;
3629-
int rw = iov_iter_rw(iter);
3630-
int err;
3631-
enum rw_hint hint = iocb->ki_hint;
3632-
int whint_mode = F2FS_OPTION(sbi).whint_mode;
3633-
bool do_opu;
3634-
3635-
err = check_direct_IO(inode, iter, offset);
3636-
if (err)
3637-
return err < 0 ? err : 0;
3638-
3639-
if (f2fs_force_buffered_io(inode, iocb, iter))
3640-
return 0;
3641-
3642-
do_opu = rw == WRITE && f2fs_lfs_mode(sbi);
3643-
3644-
trace_f2fs_direct_IO_enter(inode, offset, count, rw);
3645-
3646-
if (rw == WRITE && whint_mode == WHINT_MODE_OFF)
3647-
iocb->ki_hint = WRITE_LIFE_NOT_SET;
3648-
3649-
if (iocb->ki_flags & IOCB_NOWAIT) {
3650-
if (!down_read_trylock(&fi->i_gc_rwsem[rw])) {
3651-
iocb->ki_hint = hint;
3652-
err = -EAGAIN;
3653-
goto out;
3654-
}
3655-
if (do_opu && !down_read_trylock(&fi->i_gc_rwsem[READ])) {
3656-
up_read(&fi->i_gc_rwsem[rw]);
3657-
iocb->ki_hint = hint;
3658-
err = -EAGAIN;
3659-
goto out;
3660-
}
3661-
} else {
3662-
down_read(&fi->i_gc_rwsem[rw]);
3663-
if (do_opu)
3664-
down_read(&fi->i_gc_rwsem[READ]);
3665-
}
3666-
3667-
err = __blockdev_direct_IO(iocb, inode, inode->i_sb->s_bdev,
3668-
iter, rw == WRITE ? get_data_block_dio_write :
3669-
get_data_block_dio, NULL, f2fs_dio_submit_bio,
3670-
rw == WRITE ? DIO_LOCKING | DIO_SKIP_HOLES :
3671-
DIO_SKIP_HOLES);
3672-
3673-
if (do_opu)
3674-
up_read(&fi->i_gc_rwsem[READ]);
3675-
3676-
up_read(&fi->i_gc_rwsem[rw]);
3677-
3678-
if (rw == WRITE) {
3679-
if (whint_mode == WHINT_MODE_OFF)
3680-
iocb->ki_hint = hint;
3681-
if (err > 0) {
3682-
f2fs_update_iostat(F2FS_I_SB(inode), APP_DIRECT_IO,
3683-
err);
3684-
if (!do_opu)
3685-
set_inode_flag(inode, FI_UPDATE_WRITE);
3686-
} else if (err == -EIOCBQUEUED) {
3687-
f2fs_update_iostat(F2FS_I_SB(inode), APP_DIRECT_IO,
3688-
count - iov_iter_count(iter));
3689-
} else if (err < 0) {
3690-
f2fs_write_failed(inode, offset + count);
3691-
}
3692-
} else {
3693-
if (err > 0)
3694-
f2fs_update_iostat(sbi, APP_DIRECT_READ_IO, err);
3695-
else if (err == -EIOCBQUEUED)
3696-
f2fs_update_iostat(F2FS_I_SB(inode), APP_DIRECT_READ_IO,
3697-
count - iov_iter_count(iter));
3698-
}
3699-
3700-
out:
3701-
trace_f2fs_direct_IO_exit(inode, offset, count, rw, err);
3702-
3703-
return err;
3704-
}
3705-
37063505
void f2fs_invalidate_page(struct page *page, unsigned int offset,
37073506
unsigned int length)
37083507
{
@@ -4158,7 +3957,7 @@ const struct address_space_operations f2fs_dblock_aops = {
41583957
.set_page_dirty = f2fs_set_data_page_dirty,
41593958
.invalidatepage = f2fs_invalidate_page,
41603959
.releasepage = f2fs_release_page,
4161-
.direct_IO = f2fs_direct_IO,
3960+
.direct_IO = noop_direct_IO,
41623961
.bmap = f2fs_bmap,
41633962
.swap_activate = f2fs_swap_activate,
41643963
.swap_deactivate = f2fs_swap_deactivate,

fs/f2fs/f2fs.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1807,13 +1807,6 @@ struct f2fs_sb_info {
18071807
#endif
18081808
};
18091809

1810-
struct f2fs_private_dio {
1811-
struct inode *inode;
1812-
void *orig_private;
1813-
bio_end_io_t *orig_end_io;
1814-
bool write;
1815-
};
1816-
18171810
#ifdef CONFIG_F2FS_FAULT_INJECTION
18181811
#define f2fs_show_injection_info(sbi, type) \
18191812
printk_ratelimited("%sF2FS-fs (%s) : inject %s in %s of %pS\n", \
@@ -3642,6 +3635,7 @@ int f2fs_write_single_data_page(struct page *page, int *submitted,
36423635
struct writeback_control *wbc,
36433636
enum iostat_type io_type,
36443637
int compr_blocks, bool allow_balance);
3638+
void f2fs_write_failed(struct inode *inode, loff_t to);
36453639
void f2fs_invalidate_page(struct page *page, unsigned int offset,
36463640
unsigned int length);
36473641
int f2fs_release_page(struct page *page, gfp_t wait);

0 commit comments

Comments
 (0)