Skip to content

Commit f43dc4d

Browse files
Dave Chinnerdchinner
authored andcommitted
iomap: buffered write failure should not truncate the page cache
iomap_file_buffered_write_punch_delalloc() currently invalidates the page cache over the unused range of the delalloc extent that was allocated. While the write allocated the delalloc extent, it does not own it exclusively as the write does not hold any locks that prevent either writeback or mmap page faults from changing the state of either the page cache or the extent state backing this range. Whilst xfs_bmap_punch_delalloc_range() already handles races in extent conversion - it will only punch out delalloc extents and it ignores any other type of extent - the page cache truncate does not discriminate between data written by this write or some other task. As a result, truncating the page cache can result in data corruption if the write races with mmap modifications to the file over the same range. generic/346 exercises this workload, and if we randomly fail writes (as will happen when iomap gets stale iomap detection later in the patchset), it will randomly corrupt the file data because it removes data written by mmap() in the same page as the write() that failed. Hence we do not want to punch out the page cache over the range of the extent we failed to write to - what we actually need to do is detect the ranges that have dirty data in cache over them and *not punch them out*. To do this, we have to walk the page cache over the range of the delalloc extent we want to remove. This is made complex by the fact we have to handle partially up-to-date folios correctly and this can happen even when the FSB size == PAGE_SIZE because we now support multi-page folios in the page cache. Because we are only interested in discovering the edges of data ranges in the page cache (i.e. hole-data boundaries) we can make use of mapping_seek_hole_data() to find those transitions in the page cache. As we hold the invalidate_lock, we know that the boundaries are not going to change while we walk the range. This interface is also byte-based and is sub-page block aware, so we can find the data ranges in the cache based on byte offsets rather than page, folio or fs block sized chunks. This greatly simplifies the logic of finding dirty cached ranges in the page cache. Once we've identified a range that contains cached data, we can then iterate the range folio by folio. This allows us to determine if the data is dirty and hence perform the correct delalloc extent punching operations. The seek interface we use to iterate data ranges will give us sub-folio start/end granularity, so we may end up looking up the same folio multiple times as the seek interface iterates across each discontiguous data region in the folio. Signed-off-by: Dave Chinner <[email protected]> Reviewed-by: Darrick J. Wong <[email protected]>
1 parent 9c7babf commit f43dc4d

File tree

1 file changed

+180
-15
lines changed

1 file changed

+180
-15
lines changed

fs/iomap/buffered-io.c

Lines changed: 180 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -832,6 +832,165 @@ iomap_file_buffered_write(struct kiocb *iocb, struct iov_iter *i,
832832
}
833833
EXPORT_SYMBOL_GPL(iomap_file_buffered_write);
834834

835+
/*
836+
* Scan the data range passed to us for dirty page cache folios. If we find a
837+
* dirty folio, punch out the preceeding range and update the offset from which
838+
* the next punch will start from.
839+
*
840+
* We can punch out storage reservations under clean pages because they either
841+
* contain data that has been written back - in which case the delalloc punch
842+
* over that range is a no-op - or they have been read faults in which case they
843+
* contain zeroes and we can remove the delalloc backing range and any new
844+
* writes to those pages will do the normal hole filling operation...
845+
*
846+
* This makes the logic simple: we only need to keep the delalloc extents only
847+
* over the dirty ranges of the page cache.
848+
*
849+
* This function uses [start_byte, end_byte) intervals (i.e. open ended) to
850+
* simplify range iterations.
851+
*/
852+
static int iomap_write_delalloc_scan(struct inode *inode,
853+
loff_t *punch_start_byte, loff_t start_byte, loff_t end_byte,
854+
int (*punch)(struct inode *inode, loff_t offset, loff_t length))
855+
{
856+
while (start_byte < end_byte) {
857+
struct folio *folio;
858+
859+
/* grab locked page */
860+
folio = filemap_lock_folio(inode->i_mapping,
861+
start_byte >> PAGE_SHIFT);
862+
if (!folio) {
863+
start_byte = ALIGN_DOWN(start_byte, PAGE_SIZE) +
864+
PAGE_SIZE;
865+
continue;
866+
}
867+
868+
/* if dirty, punch up to offset */
869+
if (folio_test_dirty(folio)) {
870+
if (start_byte > *punch_start_byte) {
871+
int error;
872+
873+
error = punch(inode, *punch_start_byte,
874+
start_byte - *punch_start_byte);
875+
if (error) {
876+
folio_unlock(folio);
877+
folio_put(folio);
878+
return error;
879+
}
880+
}
881+
882+
/*
883+
* Make sure the next punch start is correctly bound to
884+
* the end of this data range, not the end of the folio.
885+
*/
886+
*punch_start_byte = min_t(loff_t, end_byte,
887+
folio_next_index(folio) << PAGE_SHIFT);
888+
}
889+
890+
/* move offset to start of next folio in range */
891+
start_byte = folio_next_index(folio) << PAGE_SHIFT;
892+
folio_unlock(folio);
893+
folio_put(folio);
894+
}
895+
return 0;
896+
}
897+
898+
/*
899+
* Punch out all the delalloc blocks in the range given except for those that
900+
* have dirty data still pending in the page cache - those are going to be
901+
* written and so must still retain the delalloc backing for writeback.
902+
*
903+
* As we are scanning the page cache for data, we don't need to reimplement the
904+
* wheel - mapping_seek_hole_data() does exactly what we need to identify the
905+
* start and end of data ranges correctly even for sub-folio block sizes. This
906+
* byte range based iteration is especially convenient because it means we
907+
* don't have to care about variable size folios, nor where the start or end of
908+
* the data range lies within a folio, if they lie within the same folio or even
909+
* if there are multiple discontiguous data ranges within the folio.
910+
*
911+
* It should be noted that mapping_seek_hole_data() is not aware of EOF, and so
912+
* can return data ranges that exist in the cache beyond EOF. e.g. a page fault
913+
* spanning EOF will initialise the post-EOF data to zeroes and mark it up to
914+
* date. A write page fault can then mark it dirty. If we then fail a write()
915+
* beyond EOF into that up to date cached range, we allocate a delalloc block
916+
* beyond EOF and then have to punch it out. Because the range is up to date,
917+
* mapping_seek_hole_data() will return it, and we will skip the punch because
918+
* the folio is dirty. THis is incorrect - we always need to punch out delalloc
919+
* beyond EOF in this case as writeback will never write back and covert that
920+
* delalloc block beyond EOF. Hence we limit the cached data scan range to EOF,
921+
* resulting in always punching out the range from the EOF to the end of the
922+
* range the iomap spans.
923+
*
924+
* Intervals are of the form [start_byte, end_byte) (i.e. open ended) because it
925+
* matches the intervals returned by mapping_seek_hole_data(). i.e. SEEK_DATA
926+
* returns the start of a data range (start_byte), and SEEK_HOLE(start_byte)
927+
* returns the end of the data range (data_end). Using closed intervals would
928+
* require sprinkling this code with magic "+ 1" and "- 1" arithmetic and expose
929+
* the code to subtle off-by-one bugs....
930+
*/
931+
static int iomap_write_delalloc_release(struct inode *inode,
932+
loff_t start_byte, loff_t end_byte,
933+
int (*punch)(struct inode *inode, loff_t pos, loff_t length))
934+
{
935+
loff_t punch_start_byte = start_byte;
936+
loff_t scan_end_byte = min(i_size_read(inode), end_byte);
937+
int error = 0;
938+
939+
/*
940+
* Lock the mapping to avoid races with page faults re-instantiating
941+
* folios and dirtying them via ->page_mkwrite whilst we walk the
942+
* cache and perform delalloc extent removal. Failing to do this can
943+
* leave dirty pages with no space reservation in the cache.
944+
*/
945+
filemap_invalidate_lock(inode->i_mapping);
946+
while (start_byte < scan_end_byte) {
947+
loff_t data_end;
948+
949+
start_byte = mapping_seek_hole_data(inode->i_mapping,
950+
start_byte, scan_end_byte, SEEK_DATA);
951+
/*
952+
* If there is no more data to scan, all that is left is to
953+
* punch out the remaining range.
954+
*/
955+
if (start_byte == -ENXIO || start_byte == scan_end_byte)
956+
break;
957+
if (start_byte < 0) {
958+
error = start_byte;
959+
goto out_unlock;
960+
}
961+
WARN_ON_ONCE(start_byte < punch_start_byte);
962+
WARN_ON_ONCE(start_byte > scan_end_byte);
963+
964+
/*
965+
* We find the end of this contiguous cached data range by
966+
* seeking from start_byte to the beginning of the next hole.
967+
*/
968+
data_end = mapping_seek_hole_data(inode->i_mapping, start_byte,
969+
scan_end_byte, SEEK_HOLE);
970+
if (data_end < 0) {
971+
error = data_end;
972+
goto out_unlock;
973+
}
974+
WARN_ON_ONCE(data_end <= start_byte);
975+
WARN_ON_ONCE(data_end > scan_end_byte);
976+
977+
error = iomap_write_delalloc_scan(inode, &punch_start_byte,
978+
start_byte, data_end, punch);
979+
if (error)
980+
goto out_unlock;
981+
982+
/* The next data search starts at the end of this one. */
983+
start_byte = data_end;
984+
}
985+
986+
if (punch_start_byte < end_byte)
987+
error = punch(inode, punch_start_byte,
988+
end_byte - punch_start_byte);
989+
out_unlock:
990+
filemap_invalidate_unlock(inode->i_mapping);
991+
return error;
992+
}
993+
835994
/*
836995
* When a short write occurs, the filesystem may need to remove reserved space
837996
* that was allocated in ->iomap_begin from it's ->iomap_end method. For
@@ -842,8 +1001,25 @@ EXPORT_SYMBOL_GPL(iomap_file_buffered_write);
8421001
* allocated for this iomap.
8431002
*
8441003
* This function uses [start_byte, end_byte) intervals (i.e. open ended) to
845-
* simplify range iterations, but converts them back to {offset,len} tuples for
846-
* the punch callback.
1004+
* simplify range iterations.
1005+
*
1006+
* The punch() callback *must* only punch delalloc extents in the range passed
1007+
* to it. It must skip over all other types of extents in the range and leave
1008+
* them completely unchanged. It must do this punch atomically with respect to
1009+
* other extent modifications.
1010+
*
1011+
* The punch() callback may be called with a folio locked to prevent writeback
1012+
* extent allocation racing at the edge of the range we are currently punching.
1013+
* The locked folio may or may not cover the range being punched, so it is not
1014+
* safe for the punch() callback to lock folios itself.
1015+
*
1016+
* Lock order is:
1017+
*
1018+
* inode->i_rwsem (shared or exclusive)
1019+
* inode->i_mapping->invalidate_lock (exclusive)
1020+
* folio_lock()
1021+
* ->punch
1022+
* internal filesystem allocation lock
8471023
*/
8481024
int iomap_file_buffered_write_punch_delalloc(struct inode *inode,
8491025
struct iomap *iomap, loff_t pos, loff_t length,
@@ -853,7 +1029,6 @@ int iomap_file_buffered_write_punch_delalloc(struct inode *inode,
8531029
loff_t start_byte;
8541030
loff_t end_byte;
8551031
int blocksize = i_blocksize(inode);
856-
int error = 0;
8571032

8581033
if (iomap->type != IOMAP_DELALLOC)
8591034
return 0;
@@ -877,18 +1052,8 @@ int iomap_file_buffered_write_punch_delalloc(struct inode *inode,
8771052
if (start_byte >= end_byte)
8781053
return 0;
8791054

880-
/*
881-
* Lock the mapping to avoid races with page faults re-instantiating
882-
* folios and dirtying them via ->page_mkwrite between the page cache
883-
* truncation and the delalloc extent removal. Failing to do this can
884-
* leave dirty pages with no space reservation in the cache.
885-
*/
886-
filemap_invalidate_lock(inode->i_mapping);
887-
truncate_pagecache_range(inode, start_byte, end_byte - 1);
888-
error = punch(inode, start_byte, end_byte - start_byte);
889-
filemap_invalidate_unlock(inode->i_mapping);
890-
891-
return error;
1055+
return iomap_write_delalloc_release(inode, start_byte, end_byte,
1056+
punch);
8921057
}
8931058
EXPORT_SYMBOL_GPL(iomap_file_buffered_write_punch_delalloc);
8941059

0 commit comments

Comments
 (0)