linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Huang Jianan <huangjianan@oppo.com>
To: linux-f2fs-devel@lists.sourceforge.net
Cc: huangjianan@oppo.com, guoweichao@oppo.com, zhangshiming@oppo.com,
	linux-kernel@vger.kernel.org
Subject: [PATCH 3/3] f2fs: check if swapfile is section-alligned
Date: Sat, 27 Feb 2021 20:02:31 +0800	[thread overview]
Message-ID: <20210227120231.136559-3-huangjianan@oppo.com> (raw)
In-Reply-To: <20210227120231.136559-1-huangjianan@oppo.com>

If the swapfile isn't created by pin and fallocate, it cann't be
guaranteed section-aligned, so it may be selected by f2fs gc. When
gc_pin_file_threshold is reached, the address of swapfile may change,
but won't be synchroniz to swap_extent, so swap will write to wrong
address, which will cause data corruption.

Signed-off-by: Huang Jianan <huangjianan@oppo.com>
Signed-off-by: Guo Weichao <guoweichao@oppo.com>
---
 fs/f2fs/data.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 63 insertions(+)

diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c
index 4dbc1cafc55d..3e523d6e4643 100644
--- a/fs/f2fs/data.c
+++ b/fs/f2fs/data.c
@@ -3781,11 +3781,63 @@ int f2fs_migrate_page(struct address_space *mapping,
 #endif
 
 #ifdef CONFIG_SWAP
+static int f2fs_check_file_aligned(struct inode *inode)
+{
+	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
+	block_t main_blkaddr = SM_I(sbi)->main_blkaddr;
+	block_t cur_lblock;
+	block_t last_lblock;
+	block_t pblock;
+	unsigned long len;
+	unsigned long nr_pblocks;
+	unsigned int blocks_per_sec = sbi->blocks_per_seg * sbi->segs_per_sec;
+	int ret;
+
+	cur_lblock = 0;
+	last_lblock = bytes_to_blks(inode, i_size_read(inode));
+	len = i_size_read(inode);
+
+	while (cur_lblock < last_lblock) {
+		struct f2fs_map_blocks map;
+		pgoff_t next_pgofs;
+
+		memset(&map, 0, sizeof(map));
+		map.m_lblk = cur_lblock;
+		map.m_len = bytes_to_blks(inode, len) - cur_lblock;
+		map.m_next_pgofs = &next_pgofs;
+		map.m_seg_type = NO_CHECK_TYPE;
+
+		ret = f2fs_map_blocks(inode, &map, 0, F2FS_GET_BLOCK_FIEMAP);
+
+		if (ret)
+			goto err_out;
+
+		/* hole */
+		if (!(map.m_flags & F2FS_MAP_FLAGS))
+			goto err_out;
+
+		pblock = map.m_pblk;
+		nr_pblocks = map.m_len;
+
+		if ((pblock - main_blkaddr) & (blocks_per_sec - 1) ||
+			nr_pblocks & (blocks_per_sec - 1))
+			goto err_out;
+
+		cur_lblock += nr_pblocks;
+	}
+
+	return 0;
+err_out:
+	pr_err("swapon: swapfile isn't section-aligned\n");
+	return -EINVAL;
+}
+
 static int check_swap_activate_fast(struct swap_info_struct *sis,
 				struct file *swap_file, sector_t *span)
 {
 	struct address_space *mapping = swap_file->f_mapping;
 	struct inode *inode = mapping->host;
+	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
 	sector_t cur_lblock;
 	sector_t last_lblock;
 	sector_t pblock;
@@ -3793,6 +3845,7 @@ static int check_swap_activate_fast(struct swap_info_struct *sis,
 	sector_t highest_pblock = 0;
 	int nr_extents = 0;
 	unsigned long nr_pblocks;
+	unsigned int blocks_per_sec = sbi->blocks_per_seg * sbi->segs_per_sec;
 	u64 len;
 	int ret;
 
@@ -3827,6 +3880,13 @@ static int check_swap_activate_fast(struct swap_info_struct *sis,
 		pblock = map.m_pblk;
 		nr_pblocks = map.m_len;
 
+		if ((pblock - SM_I(sbi)->main_blkaddr) & (blocks_per_sec - 1) ||
+			nr_pblocks & (blocks_per_sec - 1)) {
+			pr_err("swapon: swapfile isn't section-aligned\n");
+			ret = -EINVAL;
+			goto out;
+		}
+
 		if (cur_lblock + nr_pblocks >= sis->max)
 			nr_pblocks = sis->max - cur_lblock;
 
@@ -3878,6 +3938,9 @@ static int check_swap_activate(struct swap_info_struct *sis,
 	if (PAGE_SIZE == F2FS_BLKSIZE)
 		return check_swap_activate_fast(sis, swap_file, span);
 
+	if (f2fs_check_file_aligned(inode))
+		return -EINVAL;
+
 	blocks_per_page = bytes_to_blks(inode, PAGE_SIZE);
 
 	/*
-- 
2.25.1


  parent reply	other threads:[~2021-02-27 12:05 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-27 12:02 [PATCH 1/3] f2fs: remove unnecessary IS_SWAPFILE check Huang Jianan
2021-02-27 12:02 ` [PATCH 2/3] f2fs: fix last_lblock check in check_swap_activate_fast Huang Jianan
2021-03-01  2:01   ` [f2fs-dev] " Chao Yu
2021-02-27 12:02 ` Huang Jianan [this message]
2021-03-01  2:41   ` [f2fs-dev] [PATCH 3/3] f2fs: check if swapfile is section-alligned Chao Yu
2021-03-02  4:20     ` Jaegeuk Kim
2021-03-02  4:25       ` Jaegeuk Kim
2021-03-01  1:58 ` [f2fs-dev] [PATCH 1/3] f2fs: remove unnecessary IS_SWAPFILE check Chao Yu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20210227120231.136559-3-huangjianan@oppo.com \
    --to=huangjianan@oppo.com \
    --cc=guoweichao@oppo.com \
    --cc=linux-f2fs-devel@lists.sourceforge.net \
    --cc=linux-kernel@vger.kernel.org \
    --cc=zhangshiming@oppo.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).