diff --git a/third_party/patches/linux/0138-link-original-file.patch b/third_party/patches/linux/0138-link-original-file.patch new file mode 100644 index 0000000000..f092b76e1b --- /dev/null +++ b/third_party/patches/linux/0138-link-original-file.patch @@ -0,0 +1,751 @@ +From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 +From: Tyler Smith <74526874+tylertms@users.noreply.github.com> +Date: Wed, 4 Nov 2026 00:26:04 -0500 +Subject: [PATCH] refactor(vfs): link original file writeback and error cursors + +--- + fs/fs-writeback.c | 20 --- + fs/inode_evict.c | 20 +++ + mm/filemap.c | 332 --------------------------------------------- + mm/filemap_batch.c | 332 +++++++++++++++++++++++++++++++++++++++++++++ + 4 files changed, 352 insertions(+), 352 deletions(-) + +diff --git a/fs/fs-writeback.c b/fs/fs-writeback.c +index 219d5379655a793ea1397beaab5a854982f84173..b545df1e21342f856a29f913a8761ec480933ede 100644 +--- a/fs/fs-writeback.c ++++ b/fs/fs-writeback.c +@@ -2380,23 +2380,3 @@ void sync_inodes_sb(struct super_block *sb) + wait_sb_inodes(sb); + } + EXPORT_SYMBOL(sync_inodes_sb); +- +-/** +- * sync_inode_metadata - write an inode to disk +- * @inode: the inode to sync +- * @wait: wait for I/O to complete. +- * +- * Write an inode to disk and adjust its dirty state after completion. +- * +- * Note: only writes the actual inode, no associated data or other metadata. +- */ +-int sync_inode_metadata(struct inode *inode, int wait) +-{ +- struct writeback_control wbc = { +- .sync_mode = wait ? WB_SYNC_ALL : WB_SYNC_NONE, +- .nr_to_write = 0, /* metadata-only */ +- }; +- +- return writeback_single_inode(inode, &wbc); +-} +-EXPORT_SYMBOL(sync_inode_metadata); +diff --git a/fs/inode_evict.c b/fs/inode_evict.c +index 3ad1dcbefe6162cb0bd0d493f453c71f7e6624d1..ad2dbb96ea7ab771a85d53fc586b5973a8f78900 100644 +--- a/fs/inode_evict.c ++++ b/fs/inode_evict.c +@@ -1279,3 +1279,23 @@ void unlock_two_nondirectories(struct inode *inode1, struct inode *inode2) + } + } + EXPORT_SYMBOL(unlock_two_nondirectories); ++ ++/** ++ * sync_inode_metadata - write an inode to disk ++ * @inode: the inode to sync ++ * @wait: wait for I/O to complete. ++ * ++ * Write an inode to disk and adjust its dirty state after completion. ++ * ++ * Note: only writes the actual inode, no associated data or other metadata. ++ */ ++int sync_inode_metadata(struct inode *inode, int wait) ++{ ++ struct writeback_control wbc = { ++ .sync_mode = wait ? WB_SYNC_ALL : WB_SYNC_NONE, ++ .nr_to_write = 0, /* metadata-only */ ++ }; ++ ++ return writeback_single_inode(inode, &wbc); ++} ++EXPORT_SYMBOL(sync_inode_metadata); +diff --git a/mm/filemap.c b/mm/filemap.c +index f8d55ba54122f9402ef76a2ae33d5f2c8597e2da..4ee96f416ca507f39699e1986478e8889691f463 100644 +--- a/mm/filemap.c ++++ b/mm/filemap.c +@@ -128,338 +128,6 @@ + * ->private_lock (zap_pte_range->block_dirty_folio) + */ + +-static int filemap_writeback(struct address_space *mapping, loff_t start, +- loff_t end, enum writeback_sync_modes sync_mode, +- long *nr_to_write) +-{ +- struct writeback_control wbc = { +- .sync_mode = sync_mode, +- .nr_to_write = nr_to_write ? *nr_to_write : LONG_MAX, +- .range_start = start, +- .range_end = end, +- }; +- int ret; +- +- if (!mapping_can_writeback(mapping) || +- !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) +- return 0; +- +- wbc_attach_fdatawrite_inode(&wbc, mapping->host); +- ret = do_writepages(mapping, &wbc); +- wbc_detach_inode(&wbc); +- +- if (!ret && nr_to_write) +- *nr_to_write = wbc.nr_to_write; +- return ret; +-} +- +-/** +- * filemap_fdatawrite_range - start writeback on mapping dirty pages in range +- * @mapping: address space structure to write +- * @start: offset in bytes where the range starts +- * @end: offset in bytes where the range ends (inclusive) +- * +- * Start writeback against all of a mapping's dirty pages that lie +- * within the byte offsets inclusive. +- * +- * This is a data integrity operation that waits upon dirty or in writeback +- * pages. +- * +- * Return: %0 on success, negative error code otherwise. +- */ +-int filemap_fdatawrite_range(struct address_space *mapping, loff_t start, +- loff_t end) +-{ +- return filemap_writeback(mapping, start, end, WB_SYNC_ALL, NULL); +-} +-EXPORT_SYMBOL(filemap_fdatawrite_range); +- +-int filemap_fdatawrite(struct address_space *mapping) +-{ +- return filemap_fdatawrite_range(mapping, 0, LLONG_MAX); +-} +-EXPORT_SYMBOL(filemap_fdatawrite); +- +-/** +- * filemap_flush_range - start writeback on a range +- * @mapping: target address_space +- * @start: index to start writeback on +- * @end: last (inclusive) index for writeback +- * +- * This is a non-integrity writeback helper, to start writing back folios +- * for the indicated range. +- * +- * Return: %0 on success, negative error code otherwise. +- */ +-int filemap_flush_range(struct address_space *mapping, loff_t start, +- loff_t end) +-{ +- return filemap_writeback(mapping, start, end, WB_SYNC_NONE, NULL); +-} +-EXPORT_SYMBOL_GPL(filemap_flush_range); +- +-/** +- * filemap_flush - mostly a non-blocking flush +- * @mapping: target address_space +- * +- * This is a mostly non-blocking flush. Not suitable for data-integrity +- * purposes - I/O may not be started against all dirty pages. +- * +- * Return: %0 on success, negative error code otherwise. +- */ +-int filemap_flush(struct address_space *mapping) +-{ +- return filemap_flush_range(mapping, 0, LLONG_MAX); +-} +-EXPORT_SYMBOL(filemap_flush); +- +-/* +- * Start writeback on @nr_to_write pages from @mapping. No one but the existing +- * btrfs caller should be using this. Talk to linux-mm if you think adding a +- * new caller is a good idea. +- */ +-int filemap_flush_nr(struct address_space *mapping, long *nr_to_write) +-{ +- return filemap_writeback(mapping, 0, LLONG_MAX, WB_SYNC_NONE, +- nr_to_write); +-} +-EXPORT_SYMBOL_FOR_MODULES(filemap_flush_nr, "btrfs"); +- +-/** +- * filemap_range_has_page - check if a page exists in range. +- * @mapping: address space within which to check +- * @start_byte: offset in bytes where the range starts +- * @end_byte: offset in bytes where the range ends (inclusive) +- * +- * Find at least one page in the range supplied, usually used to check if +- * direct writing in this range will trigger a writeback. +- * +- * Return: %true if at least one page exists in the specified range, +- * %false otherwise. +- */ +-bool filemap_range_has_page(struct address_space *mapping, +- loff_t start_byte, loff_t end_byte) +-{ +- struct folio *folio; +- XA_STATE(xas, &mapping->i_pages, start_byte >> PAGE_SHIFT); +- pgoff_t max = end_byte >> PAGE_SHIFT; +- +- if (end_byte < start_byte) +- return false; +- +- rcu_read_lock(); +- for (;;) { +- folio = xas_find(&xas, max); +- if (xas_retry(&xas, folio)) +- continue; +- /* Shadow entries don't count */ +- if (xa_is_value(folio)) +- continue; +- /* +- * We don't need to try to pin this page; we're about to +- * release the RCU lock anyway. It is enough to know that +- * there was a page here recently. +- */ +- break; +- } +- rcu_read_unlock(); +- +- return folio != NULL; +-} +-EXPORT_SYMBOL(filemap_range_has_page); +- +-/** +- * file_fdatawait_range - wait for writeback to complete +- * @file: file pointing to 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 address space that file +- * refers to, in the given range and wait for all of them. Check error +- * status of the address space vs. the file->f_wb_err cursor and return it. +- * +- * Since the error status of the file is advanced 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 vs. the file->f_wb_err cursor. +- */ +-int file_fdatawait_range(struct file *file, loff_t start_byte, loff_t end_byte) +-{ +- struct address_space *mapping = file->f_mapping; +- +- __filemap_fdatawait_range(mapping, start_byte, end_byte); +- return file_check_and_advance_wb_err(file); +-} +-EXPORT_SYMBOL(file_fdatawait_range); +- +-/* Returns true if writeback might be needed or already in progress. */ +-static bool mapping_needs_writeback(struct address_space *mapping) +-{ +- return mapping->nrpages; +-} +- +-bool filemap_range_has_writeback(struct address_space *mapping, +- loff_t start_byte, loff_t end_byte) +-{ +- XA_STATE(xas, &mapping->i_pages, start_byte >> PAGE_SHIFT); +- pgoff_t max = end_byte >> PAGE_SHIFT; +- struct folio *folio; +- +- if (end_byte < start_byte) +- return false; +- +- rcu_read_lock(); +- xas_for_each(&xas, folio, max) { +- if (xas_retry(&xas, folio)) +- continue; +- if (xa_is_value(folio)) +- continue; +- if (folio_test_dirty(folio) || folio_test_locked(folio) || +- folio_test_writeback(folio)) +- break; +- } +- rcu_read_unlock(); +- return folio != NULL; +-} +-EXPORT_SYMBOL_GPL(filemap_range_has_writeback); +- +-/** +- * filemap_write_and_wait_range - write out & wait on a file range +- * @mapping: the address_space for the pages +- * @lstart: offset in bytes where the range starts +- * @lend: offset in bytes where the range ends (inclusive) +- * +- * Write out and wait upon file offsets lstart->lend, inclusive. +- * +- * Note that @lend is inclusive (describes the last byte to be written) so +- * that this function can be used to write to the very end-of-file (end = -1). +- * +- * Return: error status of the address space. +- */ +-int filemap_write_and_wait_range(struct address_space *mapping, +- loff_t lstart, loff_t lend) +-{ +- int err = 0, err2; +- +- if (lend < lstart) +- return 0; +- +- if (mapping_needs_writeback(mapping)) { +- err = filemap_fdatawrite_range(mapping, lstart, lend); +- /* +- * Even if the above returned error, the pages may be +- * written partially (e.g. -ENOSPC), so we wait for it. +- * But the -EIO is special case, it may indicate the worst +- * thing (e.g. bug) happened, so we avoid waiting for it. +- */ +- if (err != -EIO) +- __filemap_fdatawait_range(mapping, lstart, lend); +- } +- err2 = filemap_check_errors(mapping); +- if (!err) +- err = err2; +- return err; +-} +-EXPORT_SYMBOL(filemap_write_and_wait_range); +- +-void __filemap_set_wb_err(struct address_space *mapping, int err) +-{ +- errseq_t eseq = errseq_set(&mapping->wb_err, err); +- +- trace_filemap_set_wb_err(mapping, eseq); +-} +-EXPORT_SYMBOL(__filemap_set_wb_err); +- +-/** +- * file_check_and_advance_wb_err - report wb error (if any) that was previously +- * and advance wb_err to current one +- * @file: struct file on which the error is being reported +- * +- * When userland calls fsync (or something like nfsd does the equivalent), we +- * want to report any writeback errors that occurred since the last fsync (or +- * since the file was opened if there haven't been any). +- * +- * Grab the wb_err from the mapping. If it matches what we have in the file, +- * then just quickly return 0. The file is all caught up. +- * +- * If it doesn't match, then take the mapping value, set the "seen" flag in +- * it and try to swap it into place. If it works, or another task beat us +- * to it with the new value, then update the f_wb_err and return the error +- * portion. The error at this point must be reported via proper channels +- * (a'la fsync, or NFS COMMIT operation, etc.). +- * +- * While we handle mapping->wb_err with atomic operations, the f_wb_err +- * value is protected by the f_lock since we must ensure that it reflects +- * the latest value swapped in for this file descriptor. +- * +- * Return: %0 on success, negative error code otherwise. +- */ +-int file_check_and_advance_wb_err(struct file *file) +-{ +- int err = 0; +- errseq_t old = READ_ONCE(file->f_wb_err); +- struct address_space *mapping = file->f_mapping; +- +- /* Locklessly handle the common case where nothing has changed */ +- if (errseq_check(&mapping->wb_err, old)) { +- /* Something changed, must use slow path */ +- spin_lock(&file->f_lock); +- old = file->f_wb_err; +- err = errseq_check_and_advance(&mapping->wb_err, +- &file->f_wb_err); +- trace_file_check_and_advance_wb_err(file, old); +- spin_unlock(&file->f_lock); +- } +- +- /* +- * We're mostly using this function as a drop in replacement for +- * filemap_check_errors. Clear AS_EIO/AS_ENOSPC to emulate the effect +- * that the legacy code would have had on these flags. +- */ +- clear_bit(AS_EIO, &mapping->flags); +- clear_bit(AS_ENOSPC, &mapping->flags); +- return err; +-} +-EXPORT_SYMBOL(file_check_and_advance_wb_err); +- +-/** +- * file_write_and_wait_range - write out & wait on a file range +- * @file: file pointing to address_space with pages +- * @lstart: offset in bytes where the range starts +- * @lend: offset in bytes where the range ends (inclusive) +- * +- * Write out and wait upon file offsets lstart->lend, inclusive. +- * +- * Note that @lend is inclusive (describes the last byte to be written) so +- * that this function can be used to write to the very end-of-file (end = -1). +- * +- * After writing out and waiting on the data, we check and advance the +- * f_wb_err cursor to the latest value, and return any errors detected there. +- * +- * Return: %0 on success, negative error code otherwise. +- */ +-int file_write_and_wait_range(struct file *file, loff_t lstart, loff_t lend) +-{ +- int err = 0, err2; +- struct address_space *mapping = file->f_mapping; +- +- if (lend < lstart) +- return 0; +- +- if (mapping_needs_writeback(mapping)) { +- err = filemap_fdatawrite_range(mapping, lstart, lend); +- /* See comment of filemap_write_and_wait() */ +- if (err != -EIO) +- __filemap_fdatawait_range(mapping, lstart, lend); +- } +- err2 = file_check_and_advance_wb_err(file); +- if (!err) +- err = err2; +- return err; +-} +-EXPORT_SYMBOL(file_write_and_wait_range); +- + /** + * replace_page_cache_folio - replace a pagecache folio with a new one + * @old: folio to be replaced +diff --git a/mm/filemap_batch.c b/mm/filemap_batch.c +index 26e3bf185e696e028496ca1d31c64d7f7fbdad22..feb2962de8c1f8f4569f8a1f36a7520169411718 100644 +--- a/mm/filemap_batch.c ++++ b/mm/filemap_batch.c +@@ -1979,3 +1979,335 @@ void pagecache_isize_extended(struct inode *inode, loff_t from, loff_t to) + folio_put(folio); + } + EXPORT_SYMBOL(pagecache_isize_extended); ++ ++static int filemap_writeback(struct address_space *mapping, loff_t start, ++ loff_t end, enum writeback_sync_modes sync_mode, ++ long *nr_to_write) ++{ ++ struct writeback_control wbc = { ++ .sync_mode = sync_mode, ++ .nr_to_write = nr_to_write ? *nr_to_write : LONG_MAX, ++ .range_start = start, ++ .range_end = end, ++ }; ++ int ret; ++ ++ if (!mapping_can_writeback(mapping) || ++ !mapping_tagged(mapping, PAGECACHE_TAG_DIRTY)) ++ return 0; ++ ++ wbc_attach_fdatawrite_inode(&wbc, mapping->host); ++ ret = do_writepages(mapping, &wbc); ++ wbc_detach_inode(&wbc); ++ ++ if (!ret && nr_to_write) ++ *nr_to_write = wbc.nr_to_write; ++ return ret; ++} ++ ++/** ++ * filemap_fdatawrite_range - start writeback on mapping dirty pages in range ++ * @mapping: address space structure to write ++ * @start: offset in bytes where the range starts ++ * @end: offset in bytes where the range ends (inclusive) ++ * ++ * Start writeback against all of a mapping's dirty pages that lie ++ * within the byte offsets inclusive. ++ * ++ * This is a data integrity operation that waits upon dirty or in writeback ++ * pages. ++ * ++ * Return: %0 on success, negative error code otherwise. ++ */ ++int filemap_fdatawrite_range(struct address_space *mapping, loff_t start, ++ loff_t end) ++{ ++ return filemap_writeback(mapping, start, end, WB_SYNC_ALL, NULL); ++} ++EXPORT_SYMBOL(filemap_fdatawrite_range); ++ ++int filemap_fdatawrite(struct address_space *mapping) ++{ ++ return filemap_fdatawrite_range(mapping, 0, LLONG_MAX); ++} ++EXPORT_SYMBOL(filemap_fdatawrite); ++ ++/** ++ * filemap_flush_range - start writeback on a range ++ * @mapping: target address_space ++ * @start: index to start writeback on ++ * @end: last (inclusive) index for writeback ++ * ++ * This is a non-integrity writeback helper, to start writing back folios ++ * for the indicated range. ++ * ++ * Return: %0 on success, negative error code otherwise. ++ */ ++int filemap_flush_range(struct address_space *mapping, loff_t start, ++ loff_t end) ++{ ++ return filemap_writeback(mapping, start, end, WB_SYNC_NONE, NULL); ++} ++EXPORT_SYMBOL_GPL(filemap_flush_range); ++ ++/** ++ * filemap_flush - mostly a non-blocking flush ++ * @mapping: target address_space ++ * ++ * This is a mostly non-blocking flush. Not suitable for data-integrity ++ * purposes - I/O may not be started against all dirty pages. ++ * ++ * Return: %0 on success, negative error code otherwise. ++ */ ++int filemap_flush(struct address_space *mapping) ++{ ++ return filemap_flush_range(mapping, 0, LLONG_MAX); ++} ++EXPORT_SYMBOL(filemap_flush); ++ ++/* ++ * Start writeback on @nr_to_write pages from @mapping. No one but the existing ++ * btrfs caller should be using this. Talk to linux-mm if you think adding a ++ * new caller is a good idea. ++ */ ++int filemap_flush_nr(struct address_space *mapping, long *nr_to_write) ++{ ++ return filemap_writeback(mapping, 0, LLONG_MAX, WB_SYNC_NONE, ++ nr_to_write); ++} ++EXPORT_SYMBOL_FOR_MODULES(filemap_flush_nr, "btrfs"); ++ ++/** ++ * filemap_range_has_page - check if a page exists in range. ++ * @mapping: address space within which to check ++ * @start_byte: offset in bytes where the range starts ++ * @end_byte: offset in bytes where the range ends (inclusive) ++ * ++ * Find at least one page in the range supplied, usually used to check if ++ * direct writing in this range will trigger a writeback. ++ * ++ * Return: %true if at least one page exists in the specified range, ++ * %false otherwise. ++ */ ++bool filemap_range_has_page(struct address_space *mapping, ++ loff_t start_byte, loff_t end_byte) ++{ ++ struct folio *folio; ++ XA_STATE(xas, &mapping->i_pages, start_byte >> PAGE_SHIFT); ++ pgoff_t max = end_byte >> PAGE_SHIFT; ++ ++ if (end_byte < start_byte) ++ return false; ++ ++ rcu_read_lock(); ++ for (;;) { ++ folio = xas_find(&xas, max); ++ if (xas_retry(&xas, folio)) ++ continue; ++ /* Shadow entries don't count */ ++ if (xa_is_value(folio)) ++ continue; ++ /* ++ * We don't need to try to pin this page; we're about to ++ * release the RCU lock anyway. It is enough to know that ++ * there was a page here recently. ++ */ ++ break; ++ } ++ rcu_read_unlock(); ++ ++ return folio != NULL; ++} ++EXPORT_SYMBOL(filemap_range_has_page); ++ ++/** ++ * file_fdatawait_range - wait for writeback to complete ++ * @file: file pointing to 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 address space that file ++ * refers to, in the given range and wait for all of them. Check error ++ * status of the address space vs. the file->f_wb_err cursor and return it. ++ * ++ * Since the error status of the file is advanced 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 vs. the file->f_wb_err cursor. ++ */ ++int file_fdatawait_range(struct file *file, loff_t start_byte, loff_t end_byte) ++{ ++ struct address_space *mapping = file->f_mapping; ++ ++ __filemap_fdatawait_range(mapping, start_byte, end_byte); ++ return file_check_and_advance_wb_err(file); ++} ++EXPORT_SYMBOL(file_fdatawait_range); ++ ++/* Returns true if writeback might be needed or already in progress. */ ++static bool mapping_needs_writeback(struct address_space *mapping) ++{ ++ return mapping->nrpages; ++} ++ ++bool filemap_range_has_writeback(struct address_space *mapping, ++ loff_t start_byte, loff_t end_byte) ++{ ++ XA_STATE(xas, &mapping->i_pages, start_byte >> PAGE_SHIFT); ++ pgoff_t max = end_byte >> PAGE_SHIFT; ++ struct folio *folio; ++ ++ if (end_byte < start_byte) ++ return false; ++ ++ rcu_read_lock(); ++ xas_for_each(&xas, folio, max) { ++ if (xas_retry(&xas, folio)) ++ continue; ++ if (xa_is_value(folio)) ++ continue; ++ if (folio_test_dirty(folio) || folio_test_locked(folio) || ++ folio_test_writeback(folio)) ++ break; ++ } ++ rcu_read_unlock(); ++ return folio != NULL; ++} ++EXPORT_SYMBOL_GPL(filemap_range_has_writeback); ++ ++/** ++ * filemap_write_and_wait_range - write out & wait on a file range ++ * @mapping: the address_space for the pages ++ * @lstart: offset in bytes where the range starts ++ * @lend: offset in bytes where the range ends (inclusive) ++ * ++ * Write out and wait upon file offsets lstart->lend, inclusive. ++ * ++ * Note that @lend is inclusive (describes the last byte to be written) so ++ * that this function can be used to write to the very end-of-file (end = -1). ++ * ++ * Return: error status of the address space. ++ */ ++int filemap_write_and_wait_range(struct address_space *mapping, ++ loff_t lstart, loff_t lend) ++{ ++ int err = 0, err2; ++ ++ if (lend < lstart) ++ return 0; ++ ++ if (mapping_needs_writeback(mapping)) { ++ err = filemap_fdatawrite_range(mapping, lstart, lend); ++ /* ++ * Even if the above returned error, the pages may be ++ * written partially (e.g. -ENOSPC), so we wait for it. ++ * But the -EIO is special case, it may indicate the worst ++ * thing (e.g. bug) happened, so we avoid waiting for it. ++ */ ++ if (err != -EIO) ++ __filemap_fdatawait_range(mapping, lstart, lend); ++ } ++ err2 = filemap_check_errors(mapping); ++ if (!err) ++ err = err2; ++ return err; ++} ++EXPORT_SYMBOL(filemap_write_and_wait_range); ++ ++void __filemap_set_wb_err(struct address_space *mapping, int err) ++{ ++ errseq_t eseq = errseq_set(&mapping->wb_err, err); ++ ++ trace_filemap_set_wb_err(mapping, eseq); ++} ++EXPORT_SYMBOL(__filemap_set_wb_err); ++ ++/** ++ * file_check_and_advance_wb_err - report wb error (if any) that was previously ++ * and advance wb_err to current one ++ * @file: struct file on which the error is being reported ++ * ++ * When userland calls fsync (or something like nfsd does the equivalent), we ++ * want to report any writeback errors that occurred since the last fsync (or ++ * since the file was opened if there haven't been any). ++ * ++ * Grab the wb_err from the mapping. If it matches what we have in the file, ++ * then just quickly return 0. The file is all caught up. ++ * ++ * If it doesn't match, then take the mapping value, set the "seen" flag in ++ * it and try to swap it into place. If it works, or another task beat us ++ * to it with the new value, then update the f_wb_err and return the error ++ * portion. The error at this point must be reported via proper channels ++ * (a'la fsync, or NFS COMMIT operation, etc.). ++ * ++ * While we handle mapping->wb_err with atomic operations, the f_wb_err ++ * value is protected by the f_lock since we must ensure that it reflects ++ * the latest value swapped in for this file descriptor. ++ * ++ * Return: %0 on success, negative error code otherwise. ++ */ ++int file_check_and_advance_wb_err(struct file *file) ++{ ++ int err = 0; ++ errseq_t old = READ_ONCE(file->f_wb_err); ++ struct address_space *mapping = file->f_mapping; ++ ++ /* Locklessly handle the common case where nothing has changed */ ++ if (errseq_check(&mapping->wb_err, old)) { ++ /* Something changed, must use slow path */ ++ spin_lock(&file->f_lock); ++ old = file->f_wb_err; ++ err = errseq_check_and_advance(&mapping->wb_err, ++ &file->f_wb_err); ++ trace_file_check_and_advance_wb_err(file, old); ++ spin_unlock(&file->f_lock); ++ } ++ ++ /* ++ * We're mostly using this function as a drop in replacement for ++ * filemap_check_errors. Clear AS_EIO/AS_ENOSPC to emulate the effect ++ * that the legacy code would have had on these flags. ++ */ ++ clear_bit(AS_EIO, &mapping->flags); ++ clear_bit(AS_ENOSPC, &mapping->flags); ++ return err; ++} ++EXPORT_SYMBOL(file_check_and_advance_wb_err); ++ ++/** ++ * file_write_and_wait_range - write out & wait on a file range ++ * @file: file pointing to address_space with pages ++ * @lstart: offset in bytes where the range starts ++ * @lend: offset in bytes where the range ends (inclusive) ++ * ++ * Write out and wait upon file offsets lstart->lend, inclusive. ++ * ++ * Note that @lend is inclusive (describes the last byte to be written) so ++ * that this function can be used to write to the very end-of-file (end = -1). ++ * ++ * After writing out and waiting on the data, we check and advance the ++ * f_wb_err cursor to the latest value, and return any errors detected there. ++ * ++ * Return: %0 on success, negative error code otherwise. ++ */ ++int file_write_and_wait_range(struct file *file, loff_t lstart, loff_t lend) ++{ ++ int err = 0, err2; ++ struct address_space *mapping = file->f_mapping; ++ ++ if (lend < lstart) ++ return 0; ++ ++ if (mapping_needs_writeback(mapping)) { ++ err = filemap_fdatawrite_range(mapping, lstart, lend); ++ /* See comment of filemap_write_and_wait() */ ++ if (err != -EIO) ++ __filemap_fdatawait_range(mapping, lstart, lend); ++ } ++ err2 = file_check_and_advance_wb_err(file); ++ if (!err) ++ err = err2; ++ return err; ++} ++EXPORT_SYMBOL(file_write_and_wait_range);