Skip to content

Commit c2f8226

Browse files
fdmananakdave
authored andcommitted
btrfs: respect the max size in the header when activating swap file
If we extended the size of a swapfile after its header was created (by the mkswap utility) and then try to activate it, we will map the entire file when activating the swap file, instead of limiting to the max size defined in the swap file's header. Currently test case generic/643 from fstests fails because we do not respect that size limit defined in the swap file's header. So fix this by not mapping file ranges beyond the max size defined in the swap header. This is the same type of bug that iomap used to have, and was fixed in commit 36ca794 ("mm/swap: consider max pages in iomap_swapfile_add_extent"). Fixes: ed46ff3 ("Btrfs: support swap files") CC: stable@vger.kernel.org # 5.4+ Reviewed-and-tested-by: Josef Bacik <josef@toxicpanda.com Signed-off-by: Filipe Manana <fdmanana@suse.com> Reviewed-by: David Sterba <dsterba@suse.com> Signed-off-by: David Sterba <dsterba@suse.com>
1 parent be8d1a2 commit c2f8226

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

fs/btrfs/inode.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10269,16 +10269,27 @@ static int btrfs_add_swap_extent(struct swap_info_struct *sis,
1026910269
struct btrfs_swap_info *bsi)
1027010270
{
1027110271
unsigned long nr_pages;
10272+
unsigned long max_pages;
1027210273
u64 first_ppage, first_ppage_reported, next_ppage;
1027310274
int ret;
1027410275

10276+
/*
10277+
* Our swapfile may have had its size extended after the swap header was
10278+
* written. In that case activating the swapfile should not go beyond
10279+
* the max size set in the swap header.
10280+
*/
10281+
if (bsi->nr_pages >= sis->max)
10282+
return 0;
10283+
10284+
max_pages = sis->max - bsi->nr_pages;
1027510285
first_ppage = ALIGN(bsi->block_start, PAGE_SIZE) >> PAGE_SHIFT;
1027610286
next_ppage = ALIGN_DOWN(bsi->block_start + bsi->block_len,
1027710287
PAGE_SIZE) >> PAGE_SHIFT;
1027810288

1027910289
if (first_ppage >= next_ppage)
1028010290
return 0;
1028110291
nr_pages = next_ppage - first_ppage;
10292+
nr_pages = min(nr_pages, max_pages);
1028210293

1028310294
first_ppage_reported = first_ppage;
1028410295
if (bsi->start == 0)

0 commit comments

Comments
 (0)