Skip to content

Commit a77a595

Browse files
committed
Merge patch series "Add and use folio_next_pos()"
Matthew Wilcox (Oracle) <willy@infradead.org> says: It's relatively common in filesystems to want to know the end of the current folio we're looking at. So common in fact that btrfs has its own helper for that. Lift that helper to filemap and use it everywhere that I've noticed it could be used. This actually fixes a long-standing bug in ocfs2 on 32-bit systems with files larger than 2GiB. Presumably this is not a common configuration, but I've marked it for backport anyway. The other filesystems are all fine; none of them have a bug, they're just mildly inefficient. I think this should all go in via Christian's tree, ideally with acks from the various fs maintainers (cc'd on their individual patches). * patches from https://patch.msgid.link/20251024170822.1427218-1-willy@infradead.org: mm: Use folio_next_pos() xfs: Use folio_next_pos() netfs: Use folio_next_pos() iomap: Use folio_next_pos() gfs2: Use folio_next_pos() f2fs: Use folio_next_pos() ext4: Use folio_next_pos() buffer: Use folio_next_pos() btrfs: Use folio_next_pos() filemap: Add folio_next_pos() Link: https://patch.msgid.link/20251024170822.1427218-1-willy@infradead.org Signed-off-by: Christian Brauner <brauner@kernel.org>
2 parents 3a86608 + 60a70e6 commit a77a595

21 files changed

Lines changed: 59 additions & 52 deletions

File tree

fs/btrfs/compression.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ static inline u32 btrfs_calc_input_length(struct folio *folio, u64 range_end, u6
8585
{
8686
/* @cur must be inside the folio. */
8787
ASSERT(folio_pos(folio) <= cur);
88-
ASSERT(cur < folio_end(folio));
89-
return min(range_end, folio_end(folio)) - cur;
88+
ASSERT(cur < folio_next_pos(folio));
89+
return umin(range_end, folio_next_pos(folio)) - cur;
9090
}
9191

9292
int btrfs_alloc_compress_wsm(struct btrfs_fs_info *fs_info);

fs/btrfs/defrag.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -886,7 +886,7 @@ static struct folio *defrag_prepare_one_folio(struct btrfs_inode *inode, pgoff_t
886886
}
887887

888888
lock_start = folio_pos(folio);
889-
lock_end = folio_end(folio) - 1;
889+
lock_end = folio_next_pos(folio) - 1;
890890
/* Wait for any existing ordered extent in the range */
891891
while (1) {
892892
struct btrfs_ordered_extent *ordered;
@@ -1178,7 +1178,8 @@ static int defrag_one_locked_target(struct btrfs_inode *inode,
11781178

11791179
if (!folio)
11801180
break;
1181-
if (start >= folio_end(folio) || start + len <= folio_pos(folio))
1181+
if (start >= folio_next_pos(folio) ||
1182+
start + len <= folio_pos(folio))
11821183
continue;
11831184
btrfs_folio_clamp_clear_checked(fs_info, folio, start, len);
11841185
btrfs_folio_clamp_set_dirty(fs_info, folio, start, len);
@@ -1219,7 +1220,7 @@ static int defrag_one_range(struct btrfs_inode *inode, u64 start, u32 len,
12191220
folios[i] = NULL;
12201221
goto free_folios;
12211222
}
1222-
cur = folio_end(folios[i]);
1223+
cur = folio_next_pos(folios[i]);
12231224
}
12241225
for (int i = 0; i < nr_pages; i++) {
12251226
if (!folios[i])

fs/btrfs/extent_io.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ static noinline int lock_delalloc_folios(struct inode *inode,
333333
goto out;
334334
}
335335
range_start = max_t(u64, folio_pos(folio), start);
336-
range_len = min_t(u64, folio_end(folio), end + 1) - range_start;
336+
range_len = min_t(u64, folio_next_pos(folio), end + 1) - range_start;
337337
btrfs_folio_set_lock(fs_info, folio, range_start, range_len);
338338

339339
processed_end = range_start + range_len - 1;
@@ -387,7 +387,7 @@ noinline_for_stack bool find_lock_delalloc_range(struct inode *inode,
387387
ASSERT(orig_end > orig_start);
388388

389389
/* The range should at least cover part of the folio */
390-
ASSERT(!(orig_start >= folio_end(locked_folio) ||
390+
ASSERT(!(orig_start >= folio_next_pos(locked_folio) ||
391391
orig_end <= folio_pos(locked_folio)));
392392
again:
393393
/* step one, find a bunch of delalloc bytes starting at start */
@@ -493,7 +493,7 @@ static void end_folio_read(struct folio *folio, bool uptodate, u64 start, u32 le
493493
struct btrfs_fs_info *fs_info = folio_to_fs_info(folio);
494494

495495
ASSERT(folio_pos(folio) <= start &&
496-
start + len <= folio_end(folio));
496+
start + len <= folio_next_pos(folio));
497497

498498
if (uptodate && btrfs_verify_folio(folio, start, len))
499499
btrfs_folio_set_uptodate(fs_info, folio, start, len);
@@ -1201,7 +1201,7 @@ static bool can_skip_one_ordered_range(struct btrfs_inode *inode,
12011201
* finished our folio read and unlocked the folio.
12021202
*/
12031203
if (btrfs_folio_test_dirty(fs_info, folio, cur, blocksize)) {
1204-
u64 range_len = min(folio_end(folio),
1204+
u64 range_len = umin(folio_next_pos(folio),
12051205
ordered->file_offset + ordered->num_bytes) - cur;
12061206

12071207
ret = true;
@@ -1223,7 +1223,7 @@ static bool can_skip_one_ordered_range(struct btrfs_inode *inode,
12231223
* So we return true and update @next_ret to the OE/folio boundary.
12241224
*/
12251225
if (btrfs_folio_test_uptodate(fs_info, folio, cur, blocksize)) {
1226-
u64 range_len = min(folio_end(folio),
1226+
u64 range_len = umin(folio_next_pos(folio),
12271227
ordered->file_offset + ordered->num_bytes) - cur;
12281228

12291229
/*
@@ -2215,7 +2215,7 @@ static noinline_for_stack void write_one_eb(struct extent_buffer *eb,
22152215
for (int i = 0; i < num_extent_folios(eb); i++) {
22162216
struct folio *folio = eb->folios[i];
22172217
u64 range_start = max_t(u64, eb->start, folio_pos(folio));
2218-
u32 range_len = min_t(u64, folio_end(folio),
2218+
u32 range_len = min_t(u64, folio_next_pos(folio),
22192219
eb->start + eb->len) - range_start;
22202220

22212221
folio_lock(folio);
@@ -2619,7 +2619,7 @@ void extent_write_locked_range(struct inode *inode, const struct folio *locked_f
26192619
continue;
26202620
}
26212621

2622-
cur_end = min_t(u64, folio_end(folio) - 1, end);
2622+
cur_end = min_t(u64, folio_next_pos(folio) - 1, end);
26232623
cur_len = cur_end + 1 - cur;
26242624

26252625
ASSERT(folio_test_locked(folio));
@@ -3860,7 +3860,7 @@ int read_extent_buffer_pages_nowait(struct extent_buffer *eb, int mirror_num,
38603860
for (int i = 0; i < num_extent_folios(eb); i++) {
38613861
struct folio *folio = eb->folios[i];
38623862
u64 range_start = max_t(u64, eb->start, folio_pos(folio));
3863-
u32 range_len = min_t(u64, folio_end(folio),
3863+
u32 range_len = min_t(u64, folio_next_pos(folio),
38643864
eb->start + eb->len) - range_start;
38653865

38663866
bio_add_folio_nofail(&bbio->bio, folio, range_len,

fs/btrfs/file.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ int btrfs_dirty_folio(struct btrfs_inode *inode, struct folio *folio, loff_t pos
8989
num_bytes = round_up(write_bytes + pos - start_pos,
9090
fs_info->sectorsize);
9191
ASSERT(num_bytes <= U32_MAX);
92-
ASSERT(folio_pos(folio) <= pos && folio_end(folio) >= pos + write_bytes);
92+
ASSERT(folio_pos(folio) <= pos &&
93+
folio_next_pos(folio) >= pos + write_bytes);
9394

9495
end_of_last_block = start_pos + num_bytes - 1;
9596

@@ -799,7 +800,7 @@ static int prepare_uptodate_folio(struct inode *inode, struct folio *folio, u64
799800
u64 len)
800801
{
801802
u64 clamp_start = max_t(u64, pos, folio_pos(folio));
802-
u64 clamp_end = min_t(u64, pos + len, folio_end(folio));
803+
u64 clamp_end = min_t(u64, pos + len, folio_next_pos(folio));
803804
const u32 blocksize = inode_to_fs_info(inode)->sectorsize;
804805
int ret = 0;
805806

@@ -1254,8 +1255,8 @@ static int copy_one_range(struct btrfs_inode *inode, struct iov_iter *iter,
12541255
* The reserved range goes beyond the current folio, shrink the reserved
12551256
* space to the folio boundary.
12561257
*/
1257-
if (reserved_start + reserved_len > folio_end(folio)) {
1258-
const u64 last_block = folio_end(folio);
1258+
if (reserved_start + reserved_len > folio_next_pos(folio)) {
1259+
const u64 last_block = folio_next_pos(folio);
12591260

12601261
shrink_reserved_space(inode, *data_reserved, reserved_start,
12611262
reserved_len, last_block - reserved_start,

fs/btrfs/inode.c

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -409,7 +409,7 @@ static inline void btrfs_cleanup_ordered_extents(struct btrfs_inode *inode,
409409
continue;
410410
}
411411

412-
index = folio_end(folio) >> PAGE_SHIFT;
412+
index = folio_next_index(folio);
413413
/*
414414
* Here we just clear all Ordered bits for every page in the
415415
* range, then btrfs_mark_ordered_io_finished() will handle
@@ -2336,7 +2336,8 @@ int btrfs_run_delalloc_range(struct btrfs_inode *inode, struct folio *locked_fol
23362336
* The range must cover part of the @locked_folio, or a return of 1
23372337
* can confuse the caller.
23382338
*/
2339-
ASSERT(!(end <= folio_pos(locked_folio) || start >= folio_end(locked_folio)));
2339+
ASSERT(!(end <= folio_pos(locked_folio) ||
2340+
start >= folio_next_pos(locked_folio)));
23402341

23412342
if (should_nocow(inode, start, end)) {
23422343
ret = run_delalloc_nocow(inode, locked_folio, start, end);
@@ -2743,7 +2744,7 @@ static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
27432744
struct btrfs_inode *inode = fixup->inode;
27442745
struct btrfs_fs_info *fs_info = inode->root->fs_info;
27452746
u64 page_start = folio_pos(folio);
2746-
u64 page_end = folio_end(folio) - 1;
2747+
u64 page_end = folio_next_pos(folio) - 1;
27472748
int ret = 0;
27482749
bool free_delalloc_space = true;
27492750

@@ -4855,7 +4856,7 @@ static int truncate_block_zero_beyond_eof(struct btrfs_inode *inode, u64 start)
48554856
*/
48564857

48574858
zero_start = max_t(u64, folio_pos(folio), start);
4858-
zero_end = folio_end(folio);
4859+
zero_end = folio_next_pos(folio);
48594860
folio_zero_range(folio, zero_start - folio_pos(folio),
48604861
zero_end - zero_start);
48614862

@@ -5038,7 +5039,7 @@ int btrfs_truncate_block(struct btrfs_inode *inode, u64 offset, u64 start, u64 e
50385039
* not reach disk, it still affects our page caches.
50395040
*/
50405041
zero_start = max_t(u64, folio_pos(folio), start);
5041-
zero_end = min_t(u64, folio_end(folio) - 1, end);
5042+
zero_end = min_t(u64, folio_next_pos(folio) - 1, end);
50425043
} else {
50435044
zero_start = max_t(u64, block_start, start);
50445045
zero_end = min_t(u64, block_end, end);

fs/btrfs/misc.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -209,9 +209,4 @@ static inline bool bitmap_test_range_all_zero(const unsigned long *addr,
209209
return (found_set == start + nbits);
210210
}
211211

212-
static inline u64 folio_end(struct folio *folio)
213-
{
214-
return folio_pos(folio) + folio_size(folio);
215-
}
216-
217212
#endif

fs/btrfs/ordered-data.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ static bool can_finish_ordered_extent(struct btrfs_ordered_extent *ordered,
359359
if (folio) {
360360
ASSERT(folio->mapping);
361361
ASSERT(folio_pos(folio) <= file_offset);
362-
ASSERT(file_offset + len <= folio_end(folio));
362+
ASSERT(file_offset + len <= folio_next_pos(folio));
363363

364364
/*
365365
* Ordered flag indicates whether we still have

fs/btrfs/subpage.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,8 @@ static void btrfs_subpage_assert(const struct btrfs_fs_info *fs_info,
186186
* unmapped page like dummy extent buffer pages.
187187
*/
188188
if (folio->mapping)
189-
ASSERT(folio_pos(folio) <= start && start + len <= folio_end(folio),
189+
ASSERT(folio_pos(folio) <= start &&
190+
start + len <= folio_next_pos(folio),
190191
"start=%llu len=%u folio_pos=%llu folio_size=%zu",
191192
start, len, folio_pos(folio), folio_size(folio));
192193
}
@@ -217,7 +218,7 @@ static void btrfs_subpage_clamp_range(struct folio *folio, u64 *start, u32 *len)
217218
if (folio_pos(folio) >= orig_start + orig_len)
218219
*len = 0;
219220
else
220-
*len = min_t(u64, folio_end(folio), orig_start + orig_len) - *start;
221+
*len = min_t(u64, folio_next_pos(folio), orig_start + orig_len) - *start;
221222
}
222223

223224
static bool btrfs_subpage_end_and_test_lock(const struct btrfs_fs_info *fs_info,

fs/buffer.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2732,7 +2732,7 @@ int block_write_full_folio(struct folio *folio, struct writeback_control *wbc,
27322732
loff_t i_size = i_size_read(inode);
27332733

27342734
/* Is the folio fully inside i_size? */
2735-
if (folio_pos(folio) + folio_size(folio) <= i_size)
2735+
if (folio_next_pos(folio) <= i_size)
27362736
return __block_write_full_folio(inode, folio, get_block, wbc);
27372737

27382738
/* Is the folio fully outside i_size? (truncate in progress) */

fs/ext4/inode.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1319,8 +1319,8 @@ static int ext4_write_begin(const struct kiocb *iocb,
13191319
if (IS_ERR(folio))
13201320
return PTR_ERR(folio);
13211321

1322-
if (pos + len > folio_pos(folio) + folio_size(folio))
1323-
len = folio_pos(folio) + folio_size(folio) - pos;
1322+
if (len > folio_next_pos(folio) - pos)
1323+
len = folio_next_pos(folio) - pos;
13241324

13251325
from = offset_in_folio(folio, pos);
13261326
to = from + len;
@@ -2704,7 +2704,7 @@ static int mpage_prepare_extent_to_map(struct mpage_da_data *mpd)
27042704

27052705
if (mpd->map.m_len == 0)
27062706
mpd->start_pos = folio_pos(folio);
2707-
mpd->next_pos = folio_pos(folio) + folio_size(folio);
2707+
mpd->next_pos = folio_next_pos(folio);
27082708
/*
27092709
* Writeout when we cannot modify metadata is simple.
27102710
* Just submit the page. For data=journal mode we
@@ -3146,8 +3146,8 @@ static int ext4_da_write_begin(const struct kiocb *iocb,
31463146
if (IS_ERR(folio))
31473147
return PTR_ERR(folio);
31483148

3149-
if (pos + len > folio_pos(folio) + folio_size(folio))
3150-
len = folio_pos(folio) + folio_size(folio) - pos;
3149+
if (len > folio_next_pos(folio) - pos)
3150+
len = folio_next_pos(folio) - pos;
31513151

31523152
ret = ext4_block_write_begin(NULL, folio, pos, len,
31533153
ext4_da_get_block_prep);

0 commit comments

Comments
 (0)