Received: by 2002:ac0:a5a6:0:0:0:0:0 with SMTP id m35-v6csp5179554imm; Tue, 18 Sep 2018 05:42:18 -0700 (PDT) X-Google-Smtp-Source: ANB0Vdb0uWdIH0SBYC5KWuhufTJCMVbE/CRAAqn2x2hrLPXt4we3S/dxVYOGexCX4PCdDNsM/3gz X-Received: by 2002:a17:902:6b89:: with SMTP id p9-v6mr29148718plk.272.1537274538861; Tue, 18 Sep 2018 05:42:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1537274538; cv=none; d=google.com; s=arc-20160816; b=qSYuibBeWIQ3JoC2YU/Oj35qh1Zd5AoTWPeZYiaokRjug9+3dLja+lAGz4PXONpYqH UOEvcijaGLkqxMRpFlveSM6UrR6dc9jcUY+naJvAXOJSCBmYsoMR5eOQWYX971XXB5ps XrifbGyAPs4ah2ldxiu2Jyea/ttFknF5y/zC5+sawJRGep/gK/xT9H/WsLndLyo+rB8K 3PwWhukjvtYN8pK0fZyFe916J6sryTAcohOZ70FvoJBrh+cdIwiTgVm+uStp1HvVsG+r ou0Wpa3T+9vLJnJ7whMBOzigcXE+Dqxptu8nQVnSSjuVq+kgfMGfplNFzekhW0mOEkR3 lD0w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:message-id:date:subject:cc :to:from; bh=uDV606wabf7Pwdg+A0dWj8DJeA5/khccBEA+izNEu2Y=; b=LWng9s9yOPrKCgBVxNgZQge1rRm/IjieEDy2mfEjIIfA4BDkmXpVtdlnGt9h7iqgKM xuQsnIoMagnLnE9z6YZV26NbzR6UZOobbx9C6X0o0RRjJing3wqNYSIruqhZaY/igg8E Xfri2eTZbcM2wnlK3rvrI4jW6HP0koRCLEBvj/pRUi1Kd7vOd80A6Rj2U0xAjjY8S8/S znBstUeEjHoGhViygI6zRtZkAhBqbFNxVnLdSRWro92KntAXtCSmUsNsiRey74V1wCNS x5KY3121yvOzhayv2npaJiF1BfsT09QiWy4SzTta9WCxwQ+My4ZQ8kzusUO7p83bAmHI TwLA== 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 x3-v6si18159918pgo.542.2018.09.18.05.42.01; Tue, 18 Sep 2018 05:42:18 -0700 (PDT) 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 S1729585AbeIRSMc (ORCPT + 99 others); Tue, 18 Sep 2018 14:12:32 -0400 Received: from szxga04-in.huawei.com ([45.249.212.190]:12605 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1729501AbeIRSMc (ORCPT ); Tue, 18 Sep 2018 14:12:32 -0400 Received: from DGGEMS402-HUB.china.huawei.com (unknown [172.30.72.60]) by Forcepoint Email with ESMTP id 7E35AC0980175; Tue, 18 Sep 2018 20:40:04 +0800 (CST) Received: from huawei.com (10.113.189.234) by DGGEMS402-HUB.china.huawei.com (10.3.19.202) with Microsoft SMTP Server id 14.3.399.0; Tue, 18 Sep 2018 20:39:58 +0800 From: Yunlong Song To: , , , , CC: , , , , , Subject: [PATCH] f2fs: avoid GC causing encrypted file corrupted Date: Tue, 18 Sep 2018 20:39:53 +0800 Message-ID: <1537274393-78441-1-git-send-email-yunlong.song@huawei.com> X-Mailer: git-send-email 1.8.5.2 MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.113.189.234] X-CFilter-Loop: Reflected Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org The encrypted file may be corrupted by GC in following case: Time 1: | segment 1 blkaddr = A | GC -> | segment 2 blkaddr = B | Encrypted block 1 is moved from blkaddr A of segment 1 to blkaddr B of segment 2, Time 2: | segment 1 blkaddr = B | GC -> | segment 3 blkaddr = C | Before page 1 is written back and if segment 2 become a victim, then page 1 is moved from blkaddr B of segment 2 to blkaddr Cof segment 3, during the GC process of Time 2, f2fs should wait for page 1 written back before reading it, or move_data_block will read a garbage block from blkaddr B since page is not written back to blkaddr B yet. Commit 6aa58d8a ("f2fs: readahead encrypted block during GC") introduce ra_data_block to read encrypted block, but it forgets to add f2fs_wait_on_page_writeback to avoid racing between GC and flush. Signed-off-by: Yunlong Song --- fs/f2fs/gc.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/fs/f2fs/gc.c b/fs/f2fs/gc.c index a4c1a41..c55fb62 100644 --- a/fs/f2fs/gc.c +++ b/fs/f2fs/gc.c @@ -641,6 +641,14 @@ static int ra_data_block(struct inode *inode, pgoff_t index) fio.page = page; fio.new_blkaddr = fio.old_blkaddr = dn.data_blkaddr; + /* + * don't cache encrypted data into meta inode until previous dirty + * data were writebacked to avoid racing between GC and flush. + */ + f2fs_wait_on_page_writeback(page, DATA, true); + + f2fs_wait_on_block_writeback(inode, dn.data_blkaddr); + fio.encrypted_page = f2fs_pagecache_get_page(META_MAPPING(sbi), dn.data_blkaddr, FGP_LOCK | FGP_CREAT, GFP_NOFS); @@ -723,6 +731,8 @@ static void move_data_block(struct inode *inode, block_t bidx, */ f2fs_wait_on_page_writeback(page, DATA, true); + f2fs_wait_on_block_writeback(inode, dn.data_blkaddr); + err = f2fs_get_node_info(fio.sbi, dn.nid, &ni); if (err) goto put_out; -- 1.8.5.2