Skip to content

Commit 9060bc4

Browse files
Andreas GruenbacherDarrick J. Wong
authored andcommitted
iomap/gfs2: Get page in page_prepare handler
Change the iomap ->page_prepare() handler to get and return a locked folio instead of doing that in iomap_write_begin(). This allows to recover from out-of-memory situations in ->page_prepare(), which eliminates the corresponding error handling code in iomap_write_begin(). The ->put_folio() handler now also isn't called with NULL as the folio value anymore. Filesystems are expected to use the iomap_get_folio() helper for getting locked folios in their ->page_prepare() handlers. Signed-off-by: Andreas Gruenbacher <[email protected]> Reviewed-by: Darrick J. Wong <[email protected]> Reviewed-by: Christoph Hellwig <[email protected]> Signed-off-by: Darrick J. Wong <[email protected]>
1 parent 98321b5 commit 9060bc4

File tree

3 files changed

+24
-23
lines changed

3 files changed

+24
-23
lines changed

fs/gfs2/bmap.c

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -956,15 +956,25 @@ static int __gfs2_iomap_get(struct inode *inode, loff_t pos, loff_t length,
956956
goto out;
957957
}
958958

959-
static int gfs2_iomap_page_prepare(struct inode *inode, loff_t pos,
960-
unsigned len)
959+
static struct folio *
960+
gfs2_iomap_page_prepare(struct iomap_iter *iter, loff_t pos, unsigned len)
961961
{
962+
struct inode *inode = iter->inode;
962963
unsigned int blockmask = i_blocksize(inode) - 1;
963964
struct gfs2_sbd *sdp = GFS2_SB(inode);
964965
unsigned int blocks;
966+
struct folio *folio;
967+
int status;
965968

966969
blocks = ((pos & blockmask) + len + blockmask) >> inode->i_blkbits;
967-
return gfs2_trans_begin(sdp, RES_DINODE + blocks, 0);
970+
status = gfs2_trans_begin(sdp, RES_DINODE + blocks, 0);
971+
if (status)
972+
return ERR_PTR(status);
973+
974+
folio = iomap_get_folio(iter, pos);
975+
if (IS_ERR(folio))
976+
gfs2_trans_end(sdp);
977+
return folio;
968978
}
969979

970980
static void gfs2_iomap_put_folio(struct inode *inode, loff_t pos,
@@ -974,11 +984,6 @@ static void gfs2_iomap_put_folio(struct inode *inode, loff_t pos,
974984
struct gfs2_inode *ip = GFS2_I(inode);
975985
struct gfs2_sbd *sdp = GFS2_SB(inode);
976986

977-
if (!folio) {
978-
gfs2_trans_end(sdp);
979-
return;
980-
}
981-
982987
if (!gfs2_is_stuffed(ip))
983988
gfs2_page_add_databufs(ip, &folio->page, offset_in_page(pos),
984989
copied);

fs/iomap/buffered-io.c

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ static void __iomap_put_folio(struct iomap_iter *iter, loff_t pos, size_t ret,
609609

610610
if (page_ops && page_ops->put_folio) {
611611
page_ops->put_folio(iter->inode, pos, ret, folio);
612-
} else if (folio) {
612+
} else {
613613
folio_unlock(folio);
614614
folio_put(folio);
615615
}
@@ -642,17 +642,12 @@ static int iomap_write_begin(struct iomap_iter *iter, loff_t pos,
642642
if (!mapping_large_folio_support(iter->inode->i_mapping))
643643
len = min_t(size_t, len, PAGE_SIZE - offset_in_page(pos));
644644

645-
if (page_ops && page_ops->page_prepare) {
646-
status = page_ops->page_prepare(iter->inode, pos, len);
647-
if (status)
648-
return status;
649-
}
650-
651-
folio = iomap_get_folio(iter, pos);
652-
if (IS_ERR(folio)) {
653-
__iomap_put_folio(iter, pos, 0, NULL);
645+
if (page_ops && page_ops->page_prepare)
646+
folio = page_ops->page_prepare(iter, pos, len);
647+
else
648+
folio = iomap_get_folio(iter, pos);
649+
if (IS_ERR(folio))
654650
return PTR_ERR(folio);
655-
}
656651

657652
/*
658653
* Now we have a locked folio, before we do anything with it we need to

include/linux/iomap.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
struct address_space;
1414
struct fiemap_extent_info;
1515
struct inode;
16+
struct iomap_iter;
1617
struct iomap_dio;
1718
struct iomap_writepage_ctx;
1819
struct iov_iter;
@@ -131,12 +132,12 @@ static inline bool iomap_inline_data_valid(const struct iomap *iomap)
131132
* associated with them.
132133
*
133134
* When page_prepare succeeds, put_folio will always be called to do any
134-
* cleanup work necessary. In that put_folio call, @folio will be NULL if the
135-
* associated folio could not be obtained. When folio is not NULL, put_folio
136-
* is responsible for unlocking and putting the folio.
135+
* cleanup work necessary. put_folio is responsible for unlocking and putting
136+
* @folio.
137137
*/
138138
struct iomap_page_ops {
139-
int (*page_prepare)(struct inode *inode, loff_t pos, unsigned len);
139+
struct folio *(*page_prepare)(struct iomap_iter *iter, loff_t pos,
140+
unsigned len);
140141
void (*put_folio)(struct inode *inode, loff_t pos, unsigned copied,
141142
struct folio *folio);
142143

0 commit comments

Comments
 (0)