History

Merge tag 'for-6.14-rc2-tag' of git://git.kernel.org/pub/scm/linux/kernel/git/kdave/linux

Pull btrfs fixes from David Sterba: - fix stale page cache after race between readahead and direct IO write - fix hole expansion when writing at an offset beyond EOF, the range will not be zeroed - use proper way to calculate offsets in folio ranges * tag 'for-6.14-rc2-tag' of git://git.kernel.org/pub/scm/linux/kernel/git/kdave/linux: btrfs: fix hole expansion when writing at an offset beyond EOF btrfs: fix stale page cache after race between readahead and direct IO write btrfs: fix two misuses of folio_shift()

Linus Torvalds committed 1 year ago

Commit 945ce412 parents 1854c7f da2dccd2 files · +21 −12Browse files

Unified · Split

2 files
  1. fs/btrfs/extent_io.c +20 −9
  2. fs/btrfs/file.c +1 −3
fs/btrfs/extent_io.c+20−9
@@ -523,8 +523,6 @@ static void end_bbio_data_read(struct btrfs_bio *bbio) · 522 unmodified lines
523523 u64 end;
524524 u32 len;
525525
526− /* For now only order 0 folios are supported for data. */
527− ASSERT(folio_order(folio) == 0);
528526 btrfs_debug(fs_info,
529527 "%s: bi_sector=%llu, err=%d, mirror=%u",
530528 __func__, bio->bi_iter.bi_sector, bio->bi_status,
@@ -552,7 +550,6 @@ static void end_bbio_data_read(struct btrfs_bio *bbio) · 21 unmodified lines
552550
553551 if (likely(uptodate)) {
554552 loff_t i_size = i_size_read(inode);
555− pgoff_t end_index = i_size >> folio_shift(folio);
556553
557554 /*
558555 * Zero out the remaining part if this range straddles
@@ -561,9 +558,11 @@ static void end_bbio_data_read(struct btrfs_bio *bbio) · 2 unmodified lines
561558 * Here we should only zero the range inside the folio,
562559 * not touch anything else.
563560 *
564− * NOTE: i_size is exclusive while end is inclusive.
561+ * NOTE: i_size is exclusive while end is inclusive and
562+ * folio_contains() takes PAGE_SIZE units.
565563 */
566− if (folio_index(folio) == end_index && i_size <= end) {
564+ if (folio_contains(folio, i_size >> PAGE_SHIFT) &&
565+ i_size <= end) {
567566 u32 zero_start = max(offset_in_folio(folio, i_size),
568567 offset_in_folio(folio, start));
569568 u32 zero_len = offset_in_folio(folio, end) + 1 -
@@ -899,7 +898,6 @@ static struct extent_map *get_extent_map(struct btrfs_inode *inode, · 329 unmodified lines
899898 u64 len, struct extent_map **em_cached)
900899 {
901900 struct extent_map *em;
902− struct extent_state *cached_state = NULL;
903901
904902 ASSERT(em_cached);
905903
@@ -915,14 +913,12 @@ static struct extent_map *get_extent_map(struct btrfs_inode *inode, · 9 unmodified lines
915913 *em_cached = NULL;
916914 }
917915
918− btrfs_lock_and_flush_ordered_range(inode, start, start + len - 1, &cached_state);
919916 em = btrfs_get_extent(inode, folio, start, len);
920917 if (!IS_ERR(em)) {
921918 BUG_ON(*em_cached);
922919 refcount_inc(&em->refs);
923920 *em_cached = em;
924921 }
925− unlock_extent(&inode->io_tree, start, start + len - 1, &cached_state);
926922
927923 return em;
928924 }
@@ -956,7 +952,7 @@ static int btrfs_do_readpage(struct folio *folio, struct extent_map **em_cached, · 27 unmodified lines
956952 return ret;
957953 }
958954
959− if (folio->index == last_byte >> folio_shift(folio)) {
955+ if (folio_contains(folio, last_byte >> PAGE_SHIFT)) {
960956 size_t zero_offset = offset_in_folio(folio, last_byte);
961957
962958 if (zero_offset) {
@@ -1079,11 +1075,18 @@ static int btrfs_do_readpage(struct folio *folio, struct extent_map **em_cached, · 116 unmodified lines
10791075
10801076 int btrfs_read_folio(struct file *file, struct folio *folio)
10811077 {
1078+ struct btrfs_inode *inode = folio_to_inode(folio);
1079+ const u64 start = folio_pos(folio);
1080+ const u64 end = start + folio_size(folio) - 1;
1081+ struct extent_state *cached_state = NULL;
10821082 struct btrfs_bio_ctrl bio_ctrl = { .opf = REQ_OP_READ };
10831083 struct extent_map *em_cached = NULL;
10841084 int ret;
10851085
1086+ btrfs_lock_and_flush_ordered_range(inode, start, end, &cached_state);
10861087 ret = btrfs_do_readpage(folio, &em_cached, &bio_ctrl, NULL);
1088+ unlock_extent(&inode->io_tree, start, end, &cached_state);
1089+
10871090 free_extent_map(em_cached);
10881091
10891092 /*
@@ -2380,12 +2383,20 @@ void btrfs_readahead(struct readahead_control *rac) · 1290 unmodified lines
23802383 {
23812384 struct btrfs_bio_ctrl bio_ctrl = { .opf = REQ_OP_READ | REQ_RAHEAD };
23822385 struct folio *folio;
2386+ struct btrfs_inode *inode = BTRFS_I(rac->mapping->host);
2387+ const u64 start = readahead_pos(rac);
2388+ const u64 end = start + readahead_length(rac) - 1;
2389+ struct extent_state *cached_state = NULL;
23832390 struct extent_map *em_cached = NULL;
23842391 u64 prev_em_start = (u64)-1;
23852392
2393+ btrfs_lock_and_flush_ordered_range(inode, start, end, &cached_state);
2394+
23862395 while ((folio = readahead_folio(rac)) != NULL)
23872396 btrfs_do_readpage(folio, &em_cached, &bio_ctrl, &prev_em_start);
23882397
2398+ unlock_extent(&inode->io_tree, start, end, &cached_state);
2399+
23892400 if (em_cached)
23902401 free_extent_map(em_cached);
23912402 submit_one_bio(&bio_ctrl);
fs/btrfs/file.c+1−3
@@ -1039,7 +1039,6 @@ int btrfs_write_check(struct kiocb *iocb, size_t count) · 1038 unmodified lines
10391039 loff_t pos = iocb->ki_pos;
10401040 int ret;
10411041 loff_t oldsize;
1042− loff_t start_pos;
10431042
10441043 /*
10451044 * Quickly bail out on NOWAIT writes if we don't have the nodatacow or
@@ -1066,9 +1065,8 @@ int btrfs_write_check(struct kiocb *iocb, size_t count) · 20 unmodified lines
10661065 inode_inc_iversion(inode);
10671066 }
10681067
1069− start_pos = round_down(pos, fs_info->sectorsize);
10701068 oldsize = i_size_read(inode);
1071− if (start_pos > oldsize) {
1069+ if (pos > oldsize) {
10721070 /* Expand hole size to cover write data, preventing empty gap */
10731071 loff_t end_pos = round_up(pos + count, fs_info->sectorsize);
10741072