Skip to content

Commit 09edf4d

Browse files
matthewbobrowskitytso
authored andcommitted
ext4: introduce new callback for IOMAP_REPORT
As part of the ext4_iomap_begin() cleanups that precede this patch, we also split up the IOMAP_REPORT branch into a completely separate ->iomap_begin() callback named ext4_iomap_begin_report(). Again, the raionale for this change is to reduce the overall clutter within ext4_iomap_begin(). Signed-off-by: Matthew Bobrowski <[email protected]> Reviewed-by: Jan Kara <[email protected]> Reviewed-by: Ritesh Harjani <[email protected]> Link: https://lore.kernel.org/r/5c97a569e26ddb6696e3d3ac9fbde41317e029a0.1572949325.git.mbobrowski@mbobrowski.org Signed-off-by: Theodore Ts'o <[email protected]>
1 parent f063db5 commit 09edf4d

File tree

3 files changed

+85
-56
lines changed

3 files changed

+85
-56
lines changed

fs/ext4/ext4.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3388,6 +3388,7 @@ static inline void ext4_clear_io_unwritten_flag(ext4_io_end_t *io_end)
33883388
}
33893389

33903390
extern const struct iomap_ops ext4_iomap_ops;
3391+
extern const struct iomap_ops ext4_iomap_report_ops;
33913392

33923393
static inline int ext4_buffer_uptodate(struct buffer_head *bh)
33933394
{

fs/ext4/file.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -494,12 +494,14 @@ loff_t ext4_llseek(struct file *file, loff_t offset, int whence)
494494
maxbytes, i_size_read(inode));
495495
case SEEK_HOLE:
496496
inode_lock_shared(inode);
497-
offset = iomap_seek_hole(inode, offset, &ext4_iomap_ops);
497+
offset = iomap_seek_hole(inode, offset,
498+
&ext4_iomap_report_ops);
498499
inode_unlock_shared(inode);
499500
break;
500501
case SEEK_DATA:
501502
inode_lock_shared(inode);
502-
offset = iomap_seek_data(inode, offset, &ext4_iomap_ops);
503+
offset = iomap_seek_data(inode, offset,
504+
&ext4_iomap_report_ops);
503505
inode_unlock_shared(inode);
504506
break;
505507
}

fs/ext4/inode.c

Lines changed: 80 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -3553,74 +3553,32 @@ static int ext4_iomap_alloc(struct inode *inode, struct ext4_map_blocks *map,
35533553
static int ext4_iomap_begin(struct inode *inode, loff_t offset, loff_t length,
35543554
unsigned flags, struct iomap *iomap, struct iomap *srcmap)
35553555
{
3556-
unsigned int blkbits = inode->i_blkbits;
3557-
unsigned long first_block, last_block;
3558-
struct ext4_map_blocks map;
3559-
bool delalloc = false;
35603556
int ret;
3557+
struct ext4_map_blocks map;
3558+
u8 blkbits = inode->i_blkbits;
35613559

35623560
if ((offset >> blkbits) > EXT4_MAX_LOGICAL_BLOCK)
35633561
return -EINVAL;
3564-
first_block = offset >> blkbits;
3565-
last_block = min_t(loff_t, (offset + length - 1) >> blkbits,
3566-
EXT4_MAX_LOGICAL_BLOCK);
3567-
3568-
if (flags & IOMAP_REPORT) {
3569-
if (ext4_has_inline_data(inode)) {
3570-
ret = ext4_inline_data_iomap(inode, iomap);
3571-
if (ret != -EAGAIN) {
3572-
if (ret == 0 && offset >= iomap->length)
3573-
ret = -ENOENT;
3574-
return ret;
3575-
}
3576-
}
3577-
} else {
3578-
if (WARN_ON_ONCE(ext4_has_inline_data(inode)))
3579-
return -ERANGE;
3580-
}
35813562

3582-
map.m_lblk = first_block;
3583-
map.m_len = last_block - first_block + 1;
3584-
3585-
if (flags & IOMAP_REPORT) {
3586-
ret = ext4_map_blocks(NULL, inode, &map, 0);
3587-
if (ret < 0)
3588-
return ret;
3589-
3590-
if (ret == 0) {
3591-
ext4_lblk_t end = map.m_lblk + map.m_len - 1;
3592-
struct extent_status es;
3593-
3594-
ext4_es_find_extent_range(inode, &ext4_es_is_delayed,
3595-
map.m_lblk, end, &es);
3563+
if (WARN_ON_ONCE(ext4_has_inline_data(inode)))
3564+
return -ERANGE;
35963565

3597-
if (!es.es_len || es.es_lblk > end) {
3598-
/* entire range is a hole */
3599-
} else if (es.es_lblk > map.m_lblk) {
3600-
/* range starts with a hole */
3601-
map.m_len = es.es_lblk - map.m_lblk;
3602-
} else {
3603-
ext4_lblk_t offs = 0;
3566+
/*
3567+
* Calculate the first and last logical blocks respectively.
3568+
*/
3569+
map.m_lblk = offset >> blkbits;
3570+
map.m_len = min_t(loff_t, (offset + length - 1) >> blkbits,
3571+
EXT4_MAX_LOGICAL_BLOCK) - map.m_lblk + 1;
36043572

3605-
if (es.es_lblk < map.m_lblk)
3606-
offs = map.m_lblk - es.es_lblk;
3607-
map.m_lblk = es.es_lblk + offs;
3608-
map.m_len = es.es_len - offs;
3609-
delalloc = true;
3610-
}
3611-
}
3612-
} else if (flags & IOMAP_WRITE) {
3573+
if (flags & IOMAP_WRITE)
36133574
ret = ext4_iomap_alloc(inode, &map, flags);
3614-
} else {
3575+
else
36153576
ret = ext4_map_blocks(NULL, inode, &map, 0);
3616-
}
36173577

36183578
if (ret < 0)
36193579
return ret;
36203580

36213581
ext4_set_iomap(inode, iomap, &map, offset, length);
3622-
if (delalloc && iomap->type == IOMAP_HOLE)
3623-
iomap->type = IOMAP_DELALLOC;
36243582

36253583
return 0;
36263584
}
@@ -3682,6 +3640,74 @@ const struct iomap_ops ext4_iomap_ops = {
36823640
.iomap_end = ext4_iomap_end,
36833641
};
36843642

3643+
static bool ext4_iomap_is_delalloc(struct inode *inode,
3644+
struct ext4_map_blocks *map)
3645+
{
3646+
struct extent_status es;
3647+
ext4_lblk_t offset = 0, end = map->m_lblk + map->m_len - 1;
3648+
3649+
ext4_es_find_extent_range(inode, &ext4_es_is_delayed,
3650+
map->m_lblk, end, &es);
3651+
3652+
if (!es.es_len || es.es_lblk > end)
3653+
return false;
3654+
3655+
if (es.es_lblk > map->m_lblk) {
3656+
map->m_len = es.es_lblk - map->m_lblk;
3657+
return false;
3658+
}
3659+
3660+
offset = map->m_lblk - es.es_lblk;
3661+
map->m_len = es.es_len - offset;
3662+
3663+
return true;
3664+
}
3665+
3666+
static int ext4_iomap_begin_report(struct inode *inode, loff_t offset,
3667+
loff_t length, unsigned int flags,
3668+
struct iomap *iomap, struct iomap *srcmap)
3669+
{
3670+
int ret;
3671+
bool delalloc = false;
3672+
struct ext4_map_blocks map;
3673+
u8 blkbits = inode->i_blkbits;
3674+
3675+
if ((offset >> blkbits) > EXT4_MAX_LOGICAL_BLOCK)
3676+
return -EINVAL;
3677+
3678+
if (ext4_has_inline_data(inode)) {
3679+
ret = ext4_inline_data_iomap(inode, iomap);
3680+
if (ret != -EAGAIN) {
3681+
if (ret == 0 && offset >= iomap->length)
3682+
ret = -ENOENT;
3683+
return ret;
3684+
}
3685+
}
3686+
3687+
/*
3688+
* Calculate the first and last logical block respectively.
3689+
*/
3690+
map.m_lblk = offset >> blkbits;
3691+
map.m_len = min_t(loff_t, (offset + length - 1) >> blkbits,
3692+
EXT4_MAX_LOGICAL_BLOCK) - map.m_lblk + 1;
3693+
3694+
ret = ext4_map_blocks(NULL, inode, &map, 0);
3695+
if (ret < 0)
3696+
return ret;
3697+
if (ret == 0)
3698+
delalloc = ext4_iomap_is_delalloc(inode, &map);
3699+
3700+
ext4_set_iomap(inode, iomap, &map, offset, length);
3701+
if (delalloc && iomap->type == IOMAP_HOLE)
3702+
iomap->type = IOMAP_DELALLOC;
3703+
3704+
return 0;
3705+
}
3706+
3707+
const struct iomap_ops ext4_iomap_report_ops = {
3708+
.iomap_begin = ext4_iomap_begin_report,
3709+
};
3710+
36853711
static int ext4_end_io_dio(struct kiocb *iocb, loff_t offset,
36863712
ssize_t size, void *private)
36873713
{

0 commit comments

Comments
 (0)