Skip to content

Commit 12735f8

Browse files
jankaratytso
authored andcommitted
ext4: pre-zero allocated blocks for DAX IO
Currently ext4 treats DAX IO the same way as direct IO. I.e., it allocates unwritten extents before IO is done and converts unwritten extents afterwards. However this way DAX IO can race with page fault to the same area: ext4_ext_direct_IO() dax_fault() dax_io() get_block() - allocates unwritten extent copy_from_iter_pmem() get_block() - converts unwritten block to written and zeroes it out ext4_convert_unwritten_extents() So data written with DAX IO gets lost. Similarly dax_new_buf() called from dax_io() can overwrite data that has been already written to the block via mmap. Fix the problem by using pre-zeroed blocks for DAX IO the same way as we use them for DAX mmap. The downside of this solution is that every allocating write writes each block twice (once zeros, once data). Fixing the race with locking is possible as well however we would need to lock-out faults for the whole range written to by DAX IO. And that is not easy to do without locking-out faults for the whole file which seems too aggressive. Signed-off-by: Jan Kara <[email protected]> Signed-off-by: Theodore Ts'o <[email protected]>
1 parent 914f82a commit 12735f8

File tree

3 files changed

+44
-14
lines changed

3 files changed

+44
-14
lines changed

fs/ext4/ext4.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2527,8 +2527,8 @@ struct buffer_head *ext4_getblk(handle_t *, struct inode *, ext4_lblk_t, int);
25272527
struct buffer_head *ext4_bread(handle_t *, struct inode *, ext4_lblk_t, int);
25282528
int ext4_get_block_unwritten(struct inode *inode, sector_t iblock,
25292529
struct buffer_head *bh_result, int create);
2530-
int ext4_dax_mmap_get_block(struct inode *inode, sector_t iblock,
2531-
struct buffer_head *bh_result, int create);
2530+
int ext4_dax_get_block(struct inode *inode, sector_t iblock,
2531+
struct buffer_head *bh_result, int create);
25322532
int ext4_get_block(struct inode *inode, sector_t iblock,
25332533
struct buffer_head *bh_result, int create);
25342534
int ext4_dio_get_block(struct inode *inode, sector_t iblock,
@@ -3334,6 +3334,13 @@ static inline void ext4_clear_io_unwritten_flag(ext4_io_end_t *io_end)
33343334
}
33353335
}
33363336

3337+
static inline bool ext4_aligned_io(struct inode *inode, loff_t off, loff_t len)
3338+
{
3339+
int blksize = 1 << inode->i_blkbits;
3340+
3341+
return IS_ALIGNED(off, blksize) && IS_ALIGNED(len, blksize);
3342+
}
3343+
33373344
#endif /* __KERNEL__ */
33383345

33393346
#define EFSBADCRC EBADMSG /* Bad CRC detected */

fs/ext4/file.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ static int ext4_dax_fault(struct vm_area_struct *vma, struct vm_fault *vmf)
207207
if (IS_ERR(handle))
208208
result = VM_FAULT_SIGBUS;
209209
else
210-
result = __dax_fault(vma, vmf, ext4_dax_mmap_get_block, NULL);
210+
result = __dax_fault(vma, vmf, ext4_dax_get_block, NULL);
211211

212212
if (write) {
213213
if (!IS_ERR(handle))
@@ -243,7 +243,7 @@ static int ext4_dax_pmd_fault(struct vm_area_struct *vma, unsigned long addr,
243243
result = VM_FAULT_SIGBUS;
244244
else
245245
result = __dax_pmd_fault(vma, addr, pmd, flags,
246-
ext4_dax_mmap_get_block, NULL);
246+
ext4_dax_get_block, NULL);
247247

248248
if (write) {
249249
if (!IS_ERR(handle))

fs/ext4/inode.c

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3229,13 +3229,17 @@ static int ext4_releasepage(struct page *page, gfp_t wait)
32293229
}
32303230

32313231
#ifdef CONFIG_FS_DAX
3232-
int ext4_dax_mmap_get_block(struct inode *inode, sector_t iblock,
3233-
struct buffer_head *bh_result, int create)
3232+
/*
3233+
* Get block function for DAX IO and mmap faults. It takes care of converting
3234+
* unwritten extents to written ones and initializes new / converted blocks
3235+
* to zeros.
3236+
*/
3237+
int ext4_dax_get_block(struct inode *inode, sector_t iblock,
3238+
struct buffer_head *bh_result, int create)
32343239
{
32353240
int ret;
32363241

3237-
ext4_debug("ext4_dax_mmap_get_block: inode %lu, create flag %d\n",
3238-
inode->i_ino, create);
3242+
ext4_debug("inode %lu, create flag %d\n", inode->i_ino, create);
32393243
if (!create)
32403244
return _ext4_get_block(inode, iblock, bh_result, 0);
32413245

@@ -3247,9 +3251,9 @@ int ext4_dax_mmap_get_block(struct inode *inode, sector_t iblock,
32473251

32483252
if (buffer_unwritten(bh_result)) {
32493253
/*
3250-
* We are protected by i_mmap_sem so we know block cannot go
3251-
* away from under us even though we dropped i_data_sem.
3252-
* Convert extent to written and write zeros there.
3254+
* We are protected by i_mmap_sem or i_mutex so we know block
3255+
* cannot go away from under us even though we dropped
3256+
* i_data_sem. Convert extent to written and write zeros there.
32533257
*/
32543258
ret = ext4_get_block_trans(inode, iblock, bh_result,
32553259
EXT4_GET_BLOCKS_CONVERT |
@@ -3264,6 +3268,14 @@ int ext4_dax_mmap_get_block(struct inode *inode, sector_t iblock,
32643268
clear_buffer_new(bh_result);
32653269
return 0;
32663270
}
3271+
#else
3272+
/* Just define empty function, it will never get called. */
3273+
int ext4_dax_get_block(struct inode *inode, sector_t iblock,
3274+
struct buffer_head *bh_result, int create)
3275+
{
3276+
BUG();
3277+
return 0;
3278+
}
32673279
#endif
32683280

32693281
static int ext4_end_io_dio(struct kiocb *iocb, loff_t offset,
@@ -3385,8 +3397,20 @@ static ssize_t ext4_direct_IO_write(struct kiocb *iocb, struct iov_iter *iter,
33853397
iocb->private = NULL;
33863398
if (overwrite)
33873399
get_block_func = ext4_dio_get_block_overwrite;
3388-
else if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS) ||
3389-
round_down(offset, 1 << inode->i_blkbits) >= inode->i_size) {
3400+
else if (IS_DAX(inode)) {
3401+
/*
3402+
* We can avoid zeroing for aligned DAX writes beyond EOF. Other
3403+
* writes need zeroing either because they can race with page
3404+
* faults or because they use partial blocks.
3405+
*/
3406+
if (round_down(offset, 1<<inode->i_blkbits) >= inode->i_size &&
3407+
ext4_aligned_io(inode, offset, count))
3408+
get_block_func = ext4_dio_get_block;
3409+
else
3410+
get_block_func = ext4_dax_get_block;
3411+
dio_flags = DIO_LOCKING;
3412+
} else if (!ext4_test_inode_flag(inode, EXT4_INODE_EXTENTS) ||
3413+
round_down(offset, 1 << inode->i_blkbits) >= inode->i_size) {
33903414
get_block_func = ext4_dio_get_block;
33913415
dio_flags = DIO_LOCKING | DIO_SKIP_HOLES;
33923416
} else if (is_sync_kiocb(iocb)) {
@@ -3400,7 +3424,6 @@ static ssize_t ext4_direct_IO_write(struct kiocb *iocb, struct iov_iter *iter,
34003424
BUG_ON(ext4_encrypted_inode(inode) && S_ISREG(inode->i_mode));
34013425
#endif
34023426
if (IS_DAX(inode)) {
3403-
dio_flags &= ~DIO_SKIP_HOLES;
34043427
ret = dax_do_io(iocb, inode, iter, offset, get_block_func,
34053428
ext4_end_io_dio, dio_flags);
34063429
} else

0 commit comments

Comments
 (0)