Skip to content

Commit f10f59f

Browse files
adam900710kdave
authored andcommitted
btrfs: fix the delalloc range locking if sector size < page size
Inside lock_delalloc_folios(), there are several problems related to sector size < page size handling: - Set the writer locks without checking if the folio is still valid We call btrfs_folio_start_writer_lock() just like it's folio_lock(). But since the folio may not even be the folio of the current mapping, we can easily screw up the folio->private. - The range is not clamped inside the page This means we can over write other bitmaps if the start/len is not properly handled, and trigger the btrfs_subpage_assert(). - @processed_end is always rounded up to page end If the delalloc range is not page aligned, and we need to retry (returning -EAGAIN), then we will unlock to the page end. Thankfully this is not a huge problem, as now btrfs_folio_end_writer_lock() can handle range larger than the locked range, and only unlock what is already locked. Fix all these problems by: - Lock and check the folio first, then call btrfs_folio_set_writer_lock() So that if we got a folio not belonging to the inode, we won't touch folio->private. - Properly truncate the range inside the page - Update @processed_end to the locked range end Fixes: 1e1de38 ("btrfs: make process_one_page() to handle subpage locking") CC: [email protected] # 6.1+ Signed-off-by: Qu Wenruo <[email protected]> Reviewed-by: David Sterba <[email protected]> Signed-off-by: David Sterba <[email protected]>
1 parent 5f9062a commit f10f59f

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

fs/btrfs/extent_io.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -262,22 +262,23 @@ static noinline int lock_delalloc_folios(struct inode *inode,
262262

263263
for (i = 0; i < found_folios; i++) {
264264
struct folio *folio = fbatch.folios[i];
265-
u32 len = end + 1 - start;
265+
u64 range_start;
266+
u32 range_len;
266267

267268
if (folio == locked_folio)
268269
continue;
269270

270-
if (btrfs_folio_start_writer_lock(fs_info, folio, start,
271-
len))
272-
goto out;
273-
271+
folio_lock(folio);
274272
if (!folio_test_dirty(folio) || folio->mapping != mapping) {
275-
btrfs_folio_end_writer_lock(fs_info, folio, start,
276-
len);
273+
folio_unlock(folio);
277274
goto out;
278275
}
276+
range_start = max_t(u64, folio_pos(folio), start);
277+
range_len = min_t(u64, folio_pos(folio) + folio_size(folio),
278+
end + 1) - range_start;
279+
btrfs_folio_set_writer_lock(fs_info, folio, range_start, range_len);
279280

280-
processed_end = folio_pos(folio) + folio_size(folio) - 1;
281+
processed_end = range_start + range_len - 1;
281282
}
282283
folio_batch_release(&fbatch);
283284
cond_resched();

0 commit comments

Comments
 (0)