Skip to content

Commit f525152

Browse files
Christoph Hellwigbrauner
authored andcommitted
iomap: factor out a iomap_writepage_map_block helper
Split the loop body that calls into the file system to map a block and add it to the ioend into a separate helper to prefer for refactoring of the surrounding code. Note that this was the only place in iomap_writepage_map that could return an error, so include the call to ->discard_folio into the new helper as that will help to avoid code duplication in the future. Signed-off-by: Christoph Hellwig <hch@lst.de> Link: https://lore.kernel.org/r/20231207072710.176093-12-hch@lst.de Reviewed-by: Darrick J. Wong <djwong@kernel.org> Signed-off-by: Christian Brauner <brauner@kernel.org>
1 parent 6b865d6 commit f525152

File tree

1 file changed

+43
-27
lines changed

1 file changed

+43
-27
lines changed

fs/iomap/buffered-io.c

Lines changed: 43 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1712,6 +1712,45 @@ static void iomap_add_to_ioend(struct iomap_writepage_ctx *wpc,
17121712
wbc_account_cgroup_owner(wbc, &folio->page, len);
17131713
}
17141714

1715+
static int iomap_writepage_map_blocks(struct iomap_writepage_ctx *wpc,
1716+
struct writeback_control *wbc, struct folio *folio,
1717+
struct inode *inode, u64 pos, unsigned *count,
1718+
struct list_head *submit_list)
1719+
{
1720+
int error;
1721+
1722+
error = wpc->ops->map_blocks(wpc, inode, pos);
1723+
if (error)
1724+
goto fail;
1725+
trace_iomap_writepage_map(inode, &wpc->iomap);
1726+
1727+
switch (wpc->iomap.type) {
1728+
case IOMAP_INLINE:
1729+
WARN_ON_ONCE(1);
1730+
error = -EIO;
1731+
break;
1732+
case IOMAP_HOLE:
1733+
break;
1734+
default:
1735+
iomap_add_to_ioend(wpc, wbc, folio, inode, pos, submit_list);
1736+
(*count)++;
1737+
}
1738+
1739+
fail:
1740+
/*
1741+
* We cannot cancel the ioend directly here on error. We may have
1742+
* already set other pages under writeback and hence we have to run I/O
1743+
* completion to mark the error state of the pages under writeback
1744+
* appropriately.
1745+
*
1746+
* Just let the file system know what portion of the folio failed to
1747+
* map.
1748+
*/
1749+
if (error && wpc->ops->discard_folio)
1750+
wpc->ops->discard_folio(folio, pos);
1751+
return error;
1752+
}
1753+
17151754
/*
17161755
* Check interaction of the folio with the file end.
17171756
*
@@ -1796,7 +1835,8 @@ static int iomap_writepage_map(struct iomap_writepage_ctx *wpc,
17961835
unsigned nblocks = i_blocks_per_folio(inode, folio);
17971836
u64 pos = folio_pos(folio);
17981837
u64 end_pos = pos + folio_size(folio);
1799-
int error = 0, count = 0, i;
1838+
unsigned count = 0;
1839+
int error = 0, i;
18001840
LIST_HEAD(submit_list);
18011841

18021842
trace_iomap_writepage(inode, pos, folio_size(folio));
@@ -1822,19 +1862,10 @@ static int iomap_writepage_map(struct iomap_writepage_ctx *wpc,
18221862
for (i = 0; i < nblocks && pos < end_pos; i++, pos += len) {
18231863
if (ifs && !ifs_block_is_dirty(folio, ifs, i))
18241864
continue;
1825-
1826-
error = wpc->ops->map_blocks(wpc, inode, pos);
1865+
error = iomap_writepage_map_blocks(wpc, wbc, folio, inode, pos,
1866+
&count, &submit_list);
18271867
if (error)
18281868
break;
1829-
trace_iomap_writepage_map(inode, &wpc->iomap);
1830-
if (WARN_ON_ONCE(wpc->iomap.type == IOMAP_INLINE)) {
1831-
error = -EIO;
1832-
break;
1833-
}
1834-
if (wpc->iomap.type == IOMAP_HOLE)
1835-
continue;
1836-
iomap_add_to_ioend(wpc, wbc, folio, inode, pos, &submit_list);
1837-
count++;
18381869
}
18391870
if (count)
18401871
wpc->nr_folios++;
@@ -1844,21 +1875,6 @@ static int iomap_writepage_map(struct iomap_writepage_ctx *wpc,
18441875
WARN_ON_ONCE(folio_test_writeback(folio));
18451876
WARN_ON_ONCE(folio_test_dirty(folio));
18461877

1847-
/*
1848-
* We cannot cancel the ioend directly here on error. We may have
1849-
* already set other pages under writeback and hence we have to run I/O
1850-
* completion to mark the error state of the pages under writeback
1851-
* appropriately.
1852-
*/
1853-
if (unlikely(error)) {
1854-
/*
1855-
* Let the filesystem know what portion of the current page
1856-
* failed to map.
1857-
*/
1858-
if (wpc->ops->discard_folio)
1859-
wpc->ops->discard_folio(folio, pos);
1860-
}
1861-
18621878
/*
18631879
* We can have dirty bits set past end of file in page_mkwrite path
18641880
* while mapping the last partial folio. Hence it's better to clear

0 commit comments

Comments
 (0)