305 lines
10 KiB
Diff
305 lines
10 KiB
Diff
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
|
|
From: Tyler Smith <74526874+tylertms@users.noreply.github.com>
|
|
Date: Tue, 3 Nov 2026 17:46:42 +0000
|
|
Subject: [PATCH] refactor(mm): link original mapping writeback waits
|
|
|
|
---
|
|
mm/filemap.c | 120 --------------------------------------------
|
|
mm/filemap_batch.c | 121 +++++++++++++++++++++++++++++++++++++++++++++
|
|
mm/filemap_batch.h | 3 ++
|
|
3 files changed, 124 insertions(+), 120 deletions(-)
|
|
|
|
diff --git a/mm/filemap.c b/mm/filemap.c
|
|
index 0d4bcc6ef5ba40bf57aae582e6bca4ff6df00f07..74e7715698cc3adc28dfb5e32917038bf26c4e15 100644
|
|
--- a/mm/filemap.c
|
|
+++ b/mm/filemap.c
|
|
@@ -345,30 +345,6 @@ void delete_from_page_cache_batch(struct address_space *mapping,
|
|
filemap_free_folio(mapping, fbatch->folios[i]);
|
|
}
|
|
|
|
-int filemap_check_errors(struct address_space *mapping)
|
|
-{
|
|
- int ret = 0;
|
|
- /* Check for outstanding write errors */
|
|
- if (test_bit(AS_ENOSPC, &mapping->flags) &&
|
|
- test_and_clear_bit(AS_ENOSPC, &mapping->flags))
|
|
- ret = -ENOSPC;
|
|
- if (test_bit(AS_EIO, &mapping->flags) &&
|
|
- test_and_clear_bit(AS_EIO, &mapping->flags))
|
|
- ret = -EIO;
|
|
- return ret;
|
|
-}
|
|
-EXPORT_SYMBOL(filemap_check_errors);
|
|
-
|
|
-static int filemap_check_and_keep_errors(struct address_space *mapping)
|
|
-{
|
|
- /* Check for outstanding write errors */
|
|
- if (test_bit(AS_EIO, &mapping->flags))
|
|
- return -EIO;
|
|
- if (test_bit(AS_ENOSPC, &mapping->flags))
|
|
- return -ENOSPC;
|
|
- return 0;
|
|
-}
|
|
-
|
|
static int filemap_writeback(struct address_space *mapping, loff_t start,
|
|
loff_t end, enum writeback_sync_modes sync_mode,
|
|
long *nr_to_write)
|
|
@@ -509,81 +485,6 @@ bool filemap_range_has_page(struct address_space *mapping,
|
|
}
|
|
EXPORT_SYMBOL(filemap_range_has_page);
|
|
|
|
-static void __filemap_fdatawait_range(struct address_space *mapping,
|
|
- loff_t start_byte, loff_t end_byte)
|
|
-{
|
|
- pgoff_t index = start_byte >> PAGE_SHIFT;
|
|
- pgoff_t end = end_byte >> PAGE_SHIFT;
|
|
- struct folio_batch fbatch;
|
|
- unsigned nr_folios;
|
|
-
|
|
- folio_batch_init(&fbatch);
|
|
-
|
|
- while (index <= end) {
|
|
- unsigned i;
|
|
-
|
|
- nr_folios = filemap_get_folios_tag(mapping, &index, end,
|
|
- PAGECACHE_TAG_WRITEBACK, &fbatch);
|
|
-
|
|
- if (!nr_folios)
|
|
- break;
|
|
-
|
|
- for (i = 0; i < nr_folios; i++) {
|
|
- struct folio *folio = fbatch.folios[i];
|
|
-
|
|
- folio_wait_writeback(folio);
|
|
- }
|
|
- folio_batch_release(&fbatch);
|
|
- cond_resched();
|
|
- }
|
|
-}
|
|
-
|
|
-/**
|
|
- * filemap_fdatawait_range - wait for writeback to complete
|
|
- * @mapping: address space structure to wait for
|
|
- * @start_byte: offset in bytes where the range starts
|
|
- * @end_byte: offset in bytes where the range ends (inclusive)
|
|
- *
|
|
- * Walk the list of under-writeback pages of the given address space
|
|
- * in the given range and wait for all of them. Check error status of
|
|
- * the address space and return it.
|
|
- *
|
|
- * Since the error status of the address space is cleared by this function,
|
|
- * callers are responsible for checking the return value and handling and/or
|
|
- * reporting the error.
|
|
- *
|
|
- * Return: error status of the address space.
|
|
- */
|
|
-int filemap_fdatawait_range(struct address_space *mapping, loff_t start_byte,
|
|
- loff_t end_byte)
|
|
-{
|
|
- __filemap_fdatawait_range(mapping, start_byte, end_byte);
|
|
- return filemap_check_errors(mapping);
|
|
-}
|
|
-EXPORT_SYMBOL(filemap_fdatawait_range);
|
|
-
|
|
-/**
|
|
- * filemap_fdatawait_range_keep_errors - wait for writeback to complete
|
|
- * @mapping: address space structure to wait for
|
|
- * @start_byte: offset in bytes where the range starts
|
|
- * @end_byte: offset in bytes where the range ends (inclusive)
|
|
- *
|
|
- * Walk the list of under-writeback pages of the given address space in the
|
|
- * given range and wait for all of them. Unlike filemap_fdatawait_range(),
|
|
- * this function does not clear error status of the address space.
|
|
- *
|
|
- * Use this function if callers don't handle errors themselves. Expected
|
|
- * call sites are system-wide / filesystem-wide data flushers: e.g. sync(2),
|
|
- * fsfreeze(8)
|
|
- */
|
|
-int filemap_fdatawait_range_keep_errors(struct address_space *mapping,
|
|
- loff_t start_byte, loff_t end_byte)
|
|
-{
|
|
- __filemap_fdatawait_range(mapping, start_byte, end_byte);
|
|
- return filemap_check_and_keep_errors(mapping);
|
|
-}
|
|
-EXPORT_SYMBOL(filemap_fdatawait_range_keep_errors);
|
|
-
|
|
/**
|
|
* file_fdatawait_range - wait for writeback to complete
|
|
* @file: file pointing to address space structure to wait for
|
|
@@ -609,27 +510,6 @@ int file_fdatawait_range(struct file *file, loff_t start_byte, loff_t end_byte)
|
|
}
|
|
EXPORT_SYMBOL(file_fdatawait_range);
|
|
|
|
-/**
|
|
- * filemap_fdatawait_keep_errors - wait for writeback without clearing errors
|
|
- * @mapping: address space structure to wait for
|
|
- *
|
|
- * Walk the list of under-writeback pages of the given address space
|
|
- * and wait for all of them. Unlike filemap_fdatawait(), this function
|
|
- * does not clear error status of the address space.
|
|
- *
|
|
- * Use this function if callers don't handle errors themselves. Expected
|
|
- * call sites are system-wide / filesystem-wide data flushers: e.g. sync(2),
|
|
- * fsfreeze(8)
|
|
- *
|
|
- * Return: error status of the address space.
|
|
- */
|
|
-int filemap_fdatawait_keep_errors(struct address_space *mapping)
|
|
-{
|
|
- __filemap_fdatawait_range(mapping, 0, LLONG_MAX);
|
|
- return filemap_check_and_keep_errors(mapping);
|
|
-}
|
|
-EXPORT_SYMBOL(filemap_fdatawait_keep_errors);
|
|
-
|
|
/* Returns true if writeback might be needed or already in progress. */
|
|
static bool mapping_needs_writeback(struct address_space *mapping)
|
|
{
|
|
diff --git a/mm/filemap_batch.c b/mm/filemap_batch.c
|
|
index f3d9ecec2922c877ec87eded0490fc14d081fe6b..5054ba82fb838f4cf1c91b845d574e3600454b33 100644
|
|
--- a/mm/filemap_batch.c
|
|
+++ b/mm/filemap_batch.c
|
|
@@ -2,6 +2,7 @@
|
|
/* Page-cache batch helpers from mm/filemap.c and mm/swap.c. */
|
|
#include <linux/dax.h>
|
|
#include <linux/folio_batch.h>
|
|
+#include <linux/sched.h>
|
|
#include <linux/shmem_fs.h>
|
|
|
|
#include <linux/swap.h>
|
|
@@ -334,3 +335,123 @@ out:
|
|
return folio_batch_count(fbatch);
|
|
}
|
|
EXPORT_SYMBOL(filemap_get_folios_tag);
|
|
+
|
|
+int filemap_check_errors(struct address_space *mapping)
|
|
+{
|
|
+ int ret = 0;
|
|
+ /* Check for outstanding write errors */
|
|
+ if (test_bit(AS_ENOSPC, &mapping->flags) &&
|
|
+ test_and_clear_bit(AS_ENOSPC, &mapping->flags))
|
|
+ ret = -ENOSPC;
|
|
+ if (test_bit(AS_EIO, &mapping->flags) &&
|
|
+ test_and_clear_bit(AS_EIO, &mapping->flags))
|
|
+ ret = -EIO;
|
|
+ return ret;
|
|
+}
|
|
+EXPORT_SYMBOL(filemap_check_errors);
|
|
+
|
|
+static int filemap_check_and_keep_errors(struct address_space *mapping)
|
|
+{
|
|
+ /* Check for outstanding write errors */
|
|
+ if (test_bit(AS_EIO, &mapping->flags))
|
|
+ return -EIO;
|
|
+ if (test_bit(AS_ENOSPC, &mapping->flags))
|
|
+ return -ENOSPC;
|
|
+ return 0;
|
|
+}
|
|
+
|
|
+void __filemap_fdatawait_range(struct address_space *mapping,
|
|
+ loff_t start_byte, loff_t end_byte)
|
|
+{
|
|
+ pgoff_t index = start_byte >> PAGE_SHIFT;
|
|
+ pgoff_t end = end_byte >> PAGE_SHIFT;
|
|
+ struct folio_batch fbatch;
|
|
+ unsigned nr_folios;
|
|
+
|
|
+ folio_batch_init(&fbatch);
|
|
+
|
|
+ while (index <= end) {
|
|
+ unsigned i;
|
|
+
|
|
+ nr_folios = filemap_get_folios_tag(mapping, &index, end,
|
|
+ PAGECACHE_TAG_WRITEBACK, &fbatch);
|
|
+
|
|
+ if (!nr_folios)
|
|
+ break;
|
|
+
|
|
+ for (i = 0; i < nr_folios; i++) {
|
|
+ struct folio *folio = fbatch.folios[i];
|
|
+
|
|
+ folio_wait_writeback(folio);
|
|
+ }
|
|
+ folio_batch_release(&fbatch);
|
|
+ cond_resched();
|
|
+ }
|
|
+}
|
|
+
|
|
+/**
|
|
+ * filemap_fdatawait_range - wait for writeback to complete
|
|
+ * @mapping: address space structure to wait for
|
|
+ * @start_byte: offset in bytes where the range starts
|
|
+ * @end_byte: offset in bytes where the range ends (inclusive)
|
|
+ *
|
|
+ * Walk the list of under-writeback pages of the given address space
|
|
+ * in the given range and wait for all of them. Check error status of
|
|
+ * the address space and return it.
|
|
+ *
|
|
+ * Since the error status of the address space is cleared by this function,
|
|
+ * callers are responsible for checking the return value and handling and/or
|
|
+ * reporting the error.
|
|
+ *
|
|
+ * Return: error status of the address space.
|
|
+ */
|
|
+int filemap_fdatawait_range(struct address_space *mapping, loff_t start_byte,
|
|
+ loff_t end_byte)
|
|
+{
|
|
+ __filemap_fdatawait_range(mapping, start_byte, end_byte);
|
|
+ return filemap_check_errors(mapping);
|
|
+}
|
|
+EXPORT_SYMBOL(filemap_fdatawait_range);
|
|
+
|
|
+/**
|
|
+ * filemap_fdatawait_range_keep_errors - wait for writeback to complete
|
|
+ * @mapping: address space structure to wait for
|
|
+ * @start_byte: offset in bytes where the range starts
|
|
+ * @end_byte: offset in bytes where the range ends (inclusive)
|
|
+ *
|
|
+ * Walk the list of under-writeback pages of the given address space in the
|
|
+ * given range and wait for all of them. Unlike filemap_fdatawait_range(),
|
|
+ * this function does not clear error status of the address space.
|
|
+ *
|
|
+ * Use this function if callers don't handle errors themselves. Expected
|
|
+ * call sites are system-wide / filesystem-wide data flushers: e.g. sync(2),
|
|
+ * fsfreeze(8)
|
|
+ */
|
|
+int filemap_fdatawait_range_keep_errors(struct address_space *mapping,
|
|
+ loff_t start_byte, loff_t end_byte)
|
|
+{
|
|
+ __filemap_fdatawait_range(mapping, start_byte, end_byte);
|
|
+ return filemap_check_and_keep_errors(mapping);
|
|
+}
|
|
+EXPORT_SYMBOL(filemap_fdatawait_range_keep_errors);
|
|
+
|
|
+/**
|
|
+ * filemap_fdatawait_keep_errors - wait for writeback without clearing errors
|
|
+ * @mapping: address space structure to wait for
|
|
+ *
|
|
+ * Walk the list of under-writeback pages of the given address space
|
|
+ * and wait for all of them. Unlike filemap_fdatawait(), this function
|
|
+ * does not clear error status of the address space.
|
|
+ *
|
|
+ * Use this function if callers don't handle errors themselves. Expected
|
|
+ * call sites are system-wide / filesystem-wide data flushers: e.g. sync(2),
|
|
+ * fsfreeze(8)
|
|
+ *
|
|
+ * Return: error status of the address space.
|
|
+ */
|
|
+int filemap_fdatawait_keep_errors(struct address_space *mapping)
|
|
+{
|
|
+ __filemap_fdatawait_range(mapping, 0, LLONG_MAX);
|
|
+ return filemap_check_and_keep_errors(mapping);
|
|
+}
|
|
+EXPORT_SYMBOL(filemap_fdatawait_keep_errors);
|
|
diff --git a/mm/filemap_batch.h b/mm/filemap_batch.h
|
|
index a8e3314ce7b7d9c3327349d089e4362061eafafd..3cde19cd998f6928bb9affc3c31791b24c527fb1 100644
|
|
--- a/mm/filemap_batch.h
|
|
+++ b/mm/filemap_batch.h
|
|
@@ -46,4 +46,7 @@ void clear_shadow_entries(struct address_space *mapping,
|
|
void truncate_folio_batch_exceptionals(struct address_space *mapping,
|
|
struct folio_batch *fbatch, pgoff_t *indices);
|
|
|
|
+void __filemap_fdatawait_range(struct address_space *mapping,
|
|
+ loff_t start_byte, loff_t end_byte);
|
|
+
|
|
#endif
|