Skip to content

Commit b799474

Browse files
Taotao Chenbrauner
authored andcommitted
mm/pagemap: add write_begin_get_folio() helper function
Add write_begin_get_folio() to simplify the common folio lookup logic used by filesystem ->write_begin() implementations. This helper wraps __filemap_get_folio() with common flags such as FGP_WRITEBEGIN, conditional FGP_DONTCACHE, and set folio order based on the write length. Part of a series refactoring address_space_operations write_begin and write_end callbacks to use struct kiocb for passing write context and flags. Signed-off-by: Taotao Chen <chentaotao@didiglobal.com> Link: https://lore.kernel.org/20250716093559.217344-5-chentaotao@didiglobal.com Signed-off-by: Christian Brauner <brauner@kernel.org>
1 parent e9d8e2b commit b799474

1 file changed

Lines changed: 27 additions & 0 deletions

File tree

include/linux/pagemap.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,33 @@ struct folio *__filemap_get_folio(struct address_space *mapping, pgoff_t index,
750750
struct page *pagecache_get_page(struct address_space *mapping, pgoff_t index,
751751
fgf_t fgp_flags, gfp_t gfp);
752752

753+
/**
754+
* write_begin_get_folio - Get folio for write_begin with flags.
755+
* @iocb: The kiocb passed from write_begin (may be NULL).
756+
* @mapping: The address space to search.
757+
* @index: The page cache index.
758+
* @len: Length of data being written.
759+
*
760+
* This is a helper for filesystem write_begin() implementations.
761+
* It wraps __filemap_get_folio(), setting appropriate flags in
762+
* the write begin context.
763+
*
764+
* Return: A folio or an ERR_PTR.
765+
*/
766+
static inline struct folio *write_begin_get_folio(const struct kiocb *iocb,
767+
struct address_space *mapping, pgoff_t index, size_t len)
768+
{
769+
fgf_t fgp_flags = FGP_WRITEBEGIN;
770+
771+
fgp_flags |= fgf_set_order(len);
772+
773+
if (iocb && iocb->ki_flags & IOCB_DONTCACHE)
774+
fgp_flags |= FGP_DONTCACHE;
775+
776+
return __filemap_get_folio(mapping, index, fgp_flags,
777+
mapping_gfp_mask(mapping));
778+
}
779+
753780
/**
754781
* filemap_get_folio - Find and get a folio.
755782
* @mapping: The address_space to search.

0 commit comments

Comments
 (0)