[PATCH v5 2/4] folio: Add a function to get the host inode for a folio

From: David Howells
Date: Wed Nov 10 2021 - 16:08:50 EST


Add a convenience function, folio_inode() that will get the host inode from
a folio's mapping.

Changes:
ver #3:
- Fix mistake in function description[2].
ver #2:
- Fix contradiction between doc and implementation by disallowing use
with swap caches[1].

Signed-off-by: David Howells <dhowells@xxxxxxxxxx>
Reviewed-by: Matthew Wilcox (Oracle) <willy@xxxxxxxxxxxxx>
Tested-by: Jeff Layton <jlayton@xxxxxxxxxx>
Tested-by: Dominique Martinet <asmadeus@xxxxxxxxxxxxx>
Tested-by: kafs-testing@xxxxxxxxxxxx
Link: https://lore.kernel.org/r/YST8OcVNy02Rivbm@xxxxxxxxxxxxxxxxxxxx/ [1]
Link: https://lore.kernel.org/r/YYKLkBwQdtn4ja+i@xxxxxxxxxxxxxxxxxxxx/ [2]
Link: https://lore.kernel.org/r/162880453171.3369675.3704943108660112470.stgit@xxxxxxxxxxxxxxxxxxxxxx/ # rfc
Link: https://lore.kernel.org/r/162981151155.1901565.7010079316994382707.stgit@xxxxxxxxxxxxxxxxxxxxxx/
Link: https://lore.kernel.org/r/163005744370.2472992.18324470937328925723.stgit@xxxxxxxxxxxxxxxxxxxxxx/ # v2
Link: https://lore.kernel.org/r/163584184628.4023316.9386282630968981869.stgit@xxxxxxxxxxxxxxxxxxxxxx/ # v3
Link: https://lore.kernel.org/r/163649325519.309189.15072332908703129455.stgit@xxxxxxxxxxxxxxxxxxxxxx/ # v4
---

include/linux/pagemap.h | 14 ++++++++++++++
mm/page-writeback.c | 2 +-
2 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h
index 1f560aecd9b5..1a0c646eb6ff 100644
--- a/include/linux/pagemap.h
+++ b/include/linux/pagemap.h
@@ -253,6 +253,20 @@ static inline struct address_space *page_mapping_file(struct page *page)
return folio_mapping(folio);
}

+/**
+ * folio_inode - Get the host inode for this folio.
+ * @folio: The folio.
+ *
+ * For folios which are in the page cache, return the inode that this folio
+ * belongs to.
+ *
+ * Do not call this for folios which aren't in the page cache.
+ */
+static inline struct inode *folio_inode(struct folio *folio)
+{
+ return folio->mapping->host;
+}
+
static inline bool page_cache_add_speculative(struct page *page, int count)
{
VM_BUG_ON_PAGE(PageTail(page), page);
diff --git a/mm/page-writeback.c b/mm/page-writeback.c
index 2d498bb62248..a613f8ef6a02 100644
--- a/mm/page-writeback.c
+++ b/mm/page-writeback.c
@@ -2967,7 +2967,7 @@ EXPORT_SYMBOL_GPL(folio_wait_writeback_killable);
*/
void folio_wait_stable(struct folio *folio)
{
- if (folio->mapping->host->i_sb->s_iflags & SB_I_STABLE_WRITES)
+ if (folio_inode(folio)->i_sb->s_iflags & SB_I_STABLE_WRITES)
folio_wait_writeback(folio);
}
EXPORT_SYMBOL_GPL(folio_wait_stable);