[1/2] mm: filemap: Add filemap_has_folio function

Message ID 98624c2f481966492b4eb8272aef747790229b73.1683069252.git.ackerleytng@google.com
State New
Headers
Series Fix fallocate error in hugetlbfs when fallocating again |

Commit Message

Ackerley Tng May 2, 2023, 11:56 p.m. UTC
  filemap_has_folio() will return whether there is a folio at a given
index in a mapping. This function does not affect the folio refcount.

Signed-off-by: Ackerley Tng <ackerleytng@google.com>
---
 include/linux/pagemap.h |  1 +
 mm/filemap.c            | 17 +++++++++++++++++
 2 files changed, 18 insertions(+)
  

Patch

diff --git a/include/linux/pagemap.h b/include/linux/pagemap.h
index a56308a9d1a4..e49f07cdbff7 100644
--- a/include/linux/pagemap.h
+++ b/include/linux/pagemap.h
@@ -508,6 +508,7 @@  pgoff_t page_cache_prev_miss(struct address_space *mapping,
 
 #define FGP_WRITEBEGIN		(FGP_LOCK | FGP_WRITE | FGP_CREAT | FGP_STABLE)
 
+bool filemap_has_folio(struct address_space *mapping, pgoff_t index);
 void *filemap_get_entry(struct address_space *mapping, pgoff_t index);
 struct folio *__filemap_get_folio(struct address_space *mapping, pgoff_t index,
 		int fgp_flags, gfp_t gfp);
diff --git a/mm/filemap.c b/mm/filemap.c
index a34abfe8c654..a7a6e229e33d 100644
--- a/mm/filemap.c
+++ b/mm/filemap.c
@@ -1835,6 +1835,23 @@  EXPORT_SYMBOL(page_cache_prev_miss);
  * folio_put().
  */
 
+/**
+ * filemap_has_folio - Check if filemap has a folio at given index
+ * @mapping: The address_space to search.
+ * @index: The page index.
+ *
+ * Unlike filemap_get_entry, this does not increment refcount of the folio.
+ *
+ * Return: true if folio exists else false.
+ */
+bool filemap_has_folio(struct address_space *mapping, pgoff_t index)
+{
+	void *entry = xa_load(&mapping->i_pages, index);
+
+	return entry && !xa_is_value(entry);
+}
+EXPORT_SYMBOL(filemap_has_folio);
+
 /*
  * filemap_get_entry - Get a page cache entry.
  * @mapping: the address_space to search