Received: by 2002:a25:1506:0:0:0:0:0 with SMTP id 6csp3414910ybv; Sun, 9 Feb 2020 22:59:29 -0800 (PST) X-Google-Smtp-Source: APXvYqynWE6Ye0tGwSnHJ9+wsWcaIsVwvkN+MOrDwxtiwp5Pqg43Z6bobQs5TzqeXtC+WAofj8ah X-Received: by 2002:aca:c507:: with SMTP id v7mr9945251oif.157.1581317969711; Sun, 09 Feb 2020 22:59:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1581317969; cv=none; d=google.com; s=arc-20160816; b=I7BiJrQAeQbAU61DgJjkMo4LznwmX6zWwTN6e41FsPzoYZFFvx5VT5UxG51QrcSwqd W0TxFfaRr2DssoQcDN7IUdB44ZEJ4T8rMSuloUOnseOvR4NE8JC/BukgvpIXmdw5xP30 +yx1QqqD5HaKrq0MilIWQ+GA8r8OdVkxhvpxrta+A0Ss3algwxYG7hZ0mZUx/do0JoKE l3Qsy0aGPBtkwF2FOlqjoIk8DhASu9WDEru/5LLcbQ6/J8V89WzgWbXCMYlSZPYFfX7a iEhI3JK2bqMlgTC7BANi8ww7lz/vXINSxd6IZY+NFpyeBruiQQtfLh8ChEcgW9ie4LQz H+JA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding :content-language:in-reply-to:mime-version:user-agent:date :message-id:from:references:cc:to:subject; bh=Nnclcdc/cVcbLzCBA1T34E0JgRzgihazsIjmmae5xXk=; b=et1l2MLiKH4iYFSKocWY1Ff0bVGiSiZgai3tsckoT36MscXCitP7dWoi/csfFlIsOh OTK4iCMvHLVLuB0DmIozOzZdbwVKl1fzVzz/s4lz3I09MxJuq0L4L9E2A3RuNnkN6hw4 xRrMZ3VUlx6p48aUhmPQItZ8Y224v03nHHqhQXuf6MBKCDaglbxm2sLKEo7rH8dWpesN bcqRUlgJljR1Q2oVi6eu3+Hmg0hxFB8S5yuhJxCqOLhOSps5aT1xjcyH8UrBD8jy1AQe QQVVLCEJyfA0KhV163o5jK0MEkM1Xrp4iSZTnUOVSj4fcZBml7KK5IR6lICCiQLtwCCL XuRw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id t20si3949342otr.64.2020.02.09.22.59.17; Sun, 09 Feb 2020 22:59:29 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727022AbgBJG6g (ORCPT + 99 others); Mon, 10 Feb 2020 01:58:36 -0500 Received: from szxga05-in.huawei.com ([45.249.212.191]:9713 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726231AbgBJG6g (ORCPT ); Mon, 10 Feb 2020 01:58:36 -0500 Received: from DGGEMS405-HUB.china.huawei.com (unknown [172.30.72.60]) by Forcepoint Email with ESMTP id CC4C6DCBC68D1ACC08DF; Mon, 10 Feb 2020 14:58:32 +0800 (CST) Received: from [10.134.22.195] (10.134.22.195) by smtp.huawei.com (10.3.19.205) with Microsoft SMTP Server (TLS) id 14.3.439.0; Mon, 10 Feb 2020 14:58:28 +0800 Subject: Re: [f2fs-dev] [PATCH] f2fs: fix to add swap extent correctly To: Ju Hyung Park CC: Jaegeuk Kim , , References: <20191227104456.24528-1-yuchao0@huawei.com> From: Chao Yu Message-ID: Date: Mon, 10 Feb 2020 14:58:27 +0800 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:52.0) Gecko/20100101 Thunderbird/52.9.1 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset="utf-8" Content-Language: en-US Content-Transfer-Encoding: 7bit X-Originating-IP: [10.134.22.195] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi Ju Hyung, On 2020/2/7 14:42, Ju Hyung Park wrote: > Hi Chao, > > I think this should be sent to linux-stable. > Thoughts? Yup, I forgot to Cc stable mailing list, let me resend it. Thanks for reminding me that. Thanks, > > Thanks. > > On Fri, Dec 27, 2019 at 7:45 PM Chao Yu wrote: >> >> As Youling reported in mailing list: >> >> https://www.linuxquestions.org/questions/linux-newbie-8/the-file-system-f2fs-is-broken-4175666043/ >> >> https://www.linux.org/threads/the-file-system-f2fs-is-broken.26490/ >> >> There is a test case can corrupt f2fs image: >> - dd if=/dev/zero of=/swapfile bs=1M count=4096 >> - chmod 600 /swapfile >> - mkswap /swapfile >> - swapon --discard /swapfile >> >> The root cause is f2fs_swap_activate() intends to return zero value >> to setup_swap_extents() to enable SWP_FS mode (swap file goes through >> fs), in this flow, setup_swap_extents() setups swap extent with wrong >> block address range, result in discard_swap() erasing incorrect address. >> >> Because f2fs_swap_activate() has pinned swapfile, its data block >> address will not change, it's safe to let swap to handle IO through >> raw device, so we can get rid of SWAP_FS mode and initial swap extents >> inside f2fs_swap_activate(), by this way, later discard_swap() can trim >> in right address range. >> >> Fixes: 4969c06a0d83 ("f2fs: support swap file w/ DIO") >> Signed-off-by: Chao Yu >> --- >> fs/f2fs/data.c | 32 +++++++++++++++++++++++++------- >> 1 file changed, 25 insertions(+), 7 deletions(-) >> >> diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c >> index 19cd03450066..ee4d3d284379 100644 >> --- a/fs/f2fs/data.c >> +++ b/fs/f2fs/data.c >> @@ -3608,7 +3608,8 @@ int f2fs_migrate_page(struct address_space *mapping, >> >> #ifdef CONFIG_SWAP >> /* Copied from generic_swapfile_activate() to check any holes */ >> -static int check_swap_activate(struct file *swap_file, unsigned int max) >> +static int check_swap_activate(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; >> @@ -3619,6 +3620,8 @@ static int check_swap_activate(struct file *swap_file, unsigned int max) >> sector_t last_block; >> sector_t lowest_block = -1; >> sector_t highest_block = 0; >> + int nr_extents = 0; >> + int ret; >> >> blkbits = inode->i_blkbits; >> blocks_per_page = PAGE_SIZE >> blkbits; >> @@ -3630,7 +3633,8 @@ static int check_swap_activate(struct file *swap_file, unsigned int max) >> probe_block = 0; >> page_no = 0; >> last_block = i_size_read(inode) >> blkbits; >> - while ((probe_block + blocks_per_page) <= last_block && page_no < max) { >> + while ((probe_block + blocks_per_page) <= last_block && >> + page_no < sis->max) { >> unsigned block_in_page; >> sector_t first_block; >> >> @@ -3670,13 +3674,27 @@ static int check_swap_activate(struct file *swap_file, unsigned int max) >> highest_block = first_block; >> } >> >> + /* >> + * We found a PAGE_SIZE-length, PAGE_SIZE-aligned run of blocks >> + */ >> + ret = add_swap_extent(sis, page_no, 1, first_block); >> + if (ret < 0) >> + goto out; >> + nr_extents += ret; >> page_no++; >> probe_block += blocks_per_page; >> reprobe: >> continue; >> } >> - return 0; >> - >> + ret = nr_extents; >> + *span = 1 + highest_block - lowest_block; >> + if (page_no == 0) >> + page_no = 1; /* force Empty message */ >> + sis->max = page_no; >> + sis->pages = page_no - 1; >> + sis->highest_bit = page_no - 1; >> +out: >> + return ret; >> bad_bmap: >> pr_err("swapon: swapfile has holes\n"); >> return -EINVAL; >> @@ -3701,14 +3719,14 @@ static int f2fs_swap_activate(struct swap_info_struct *sis, struct file *file, >> if (f2fs_disable_compressed_file(inode)) >> return -EINVAL; >> >> - ret = check_swap_activate(file, sis->max); >> - if (ret) >> + ret = check_swap_activate(sis, file, span); >> + if (ret < 0) >> return ret; >> >> set_inode_flag(inode, FI_PIN_FILE); >> f2fs_precache_extents(inode); >> f2fs_update_time(F2FS_I_SB(inode), REQ_TIME); >> - return 0; >> + return ret; >> } >> >> static void f2fs_swap_deactivate(struct file *file) >> -- >> 2.18.0.rc1 >> >> >> >> _______________________________________________ >> Linux-f2fs-devel mailing list >> Linux-f2fs-devel@lists.sourceforge.net >> https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel > . >