Received: by 2002:ac0:a582:0:0:0:0:0 with SMTP id m2-v6csp3857441imm; Mon, 15 Oct 2018 05:27:50 -0700 (PDT) X-Google-Smtp-Source: ACcGV63gjMOeMxmZFQ/LEpgoe9tIgtSy27uYuWVkwVH/WhAvVZF9Q8t3N5/GUJMcnY8bXB+CKh4E X-Received: by 2002:a63:9855:: with SMTP id l21-v6mr15917662pgo.162.1539606470712; Mon, 15 Oct 2018 05:27:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1539606470; cv=none; d=google.com; s=arc-20160816; b=r4OYbmCwUw3+VvuWiOVyR/gqzLo+GOfERpyUqT6WbrJmkYWZBNVd5usW4le83mWycA jy4rzj1HyW8PzRbwvAbIa9EVzSRCmhmyKQBAksMnmx+Bhh7+f1wnZ4B8Tom9q5Y89GRM zJjxDozv+qvdXbDiYUfJkKz1GxQ9bRzWOk4QqzyySeUDiPDz2FMK8vw4EMnDN5xKngFp mlDcugNqialSriQ0gv8zIXFP3OhWHT+5Z3tWz5UPPniOagoSyCyjhpLh9Lsv4CUBSqkd U1DvJu2kV13eydtXlTxl7JRpW0/dwkSuLK8tWPPMe1YcvokpYOeyw64IdPwePJw7OZ/P xtxA== 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=VV/Fci6VuhyqPcd9qypuJLaNCnuQztm6M7uF+echFEk=; b=WtQgNHBi8Zhr4DIh2qSnbWTy7FaAji4GTx7oAnPtGnTIXJxHULG5NWHzmqXL5OOG1J w0eTRDak+8kV3L9QdFt0IjkuceUT+XmjVWucNNgGdvvMxlGaluccOwTV0DP38JAEPp1O fxFo7exapVrgtWxuS+ebANOhr77dsYE+9sv2Geb/WB0d47riyUU3ducKBI0zgcSb+Fgh XIN3E7V6dnycPAxQkkQV4Ps7W5KkgyB92wNmjQ1WxoC9VPPqPT0gCYnC4GkNhtzDXVcM EF4Q65G+YaoG3zSQFigdTpnvgDAYuSURH6qQUJtmVkT1RbBpsenhlpE37pjRyQu2Bjif ZqSg== 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 o2-v6si10208712pgj.111.2018.10.15.05.27.36; Mon, 15 Oct 2018 05:27:50 -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 S1726585AbeJOUL3 (ORCPT + 99 others); Mon, 15 Oct 2018 16:11:29 -0400 Received: from szxga04-in.huawei.com ([45.249.212.190]:13638 "EHLO huawei.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726524AbeJOUL3 (ORCPT ); Mon, 15 Oct 2018 16:11:29 -0400 Received: from DGGEMS413-HUB.china.huawei.com (unknown [172.30.72.60]) by Forcepoint Email with ESMTP id 5AE1A74CAE9DD; Mon, 15 Oct 2018 20:26:21 +0800 (CST) Received: from [127.0.0.1] (10.134.22.195) by DGGEMS413-HUB.china.huawei.com (10.3.19.213) with Microsoft SMTP Server id 14.3.399.0; Mon, 15 Oct 2018 20:26:21 +0800 Subject: Re: [f2fs-dev] [PATCH] f2fs: fix data corruption issue with hardware encryption To: Jaegeuk Kim , Sahitya Tummala CC: , References: <1539149182-12729-1-git-send-email-stummala@codeaurora.org> <20181010213402.GA52406@jaegeuk-macbookpro.roam.corp.google.com> <20181011002935.GA24669@codeaurora.org> <20181011021506.GA78526@jaegeuk-macbookpro.roam.corp.google.com> <20181011030544.GA82403@jaegeuk-macbookpro.roam.corp.google.com> From: Chao Yu Message-ID: <405c1a13-5c1e-2f5a-3924-54f8c55afdfe@huawei.com> Date: Mon, 15 Oct 2018 20:26:20 +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: <20181011030544.GA82403@jaegeuk-macbookpro.roam.corp.google.com> Content-Type: text/plain; charset="windows-1252" 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 On 2018/10/11 11:05, Jaegeuk Kim wrote: > On 10/10, Jaegeuk Kim wrote: >> On 10/11, Sahitya Tummala wrote: >>> On Wed, Oct 10, 2018 at 02:34:02PM -0700, Jaegeuk Kim wrote: >>>> On 10/10, Sahitya Tummala wrote: >>>>> Direct IO can be used in case of hardware encryption. The following >>>>> scenario results into data corruption issue in this path - >>>>> >>>>> Thread A - Thread B- >>>>> -> write file#1 in direct IO >>>>> -> GC gets kicked in >>>>> -> GC submitted bio on meta mapping >>>>> for file#1, but pending completion >>>>> -> write file#1 again with new data >>>>> in direct IO >>>>> -> GC bio gets completed now >>>>> -> GC writes old data to the new >>>>> location and thus file#1 is >>>>> corrupted. >>>>> >>>>> Fix this by submitting and waiting for pending io on meta mapping >>>>> for direct IO case in f2fs_map_blocks(). >>>>> >>>>> Signed-off-by: Sahitya Tummala >>>>> --- >>>>> fs/f2fs/data.c | 12 ++++++++++++ >>>>> 1 file changed, 12 insertions(+) >>>>> >>>>> diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c >>>>> index 9ef6f1f..7b2fef0 100644 >>>>> --- a/fs/f2fs/data.c >>>>> +++ b/fs/f2fs/data.c >>>>> @@ -1028,6 +1028,12 @@ int f2fs_map_blocks(struct inode *inode, struct f2fs_map_blocks *map, >>>>> map->m_pblk = ei.blk + pgofs - ei.fofs; >>>>> map->m_len = min((pgoff_t)maxblocks, ei.fofs + ei.len - pgofs); >>>>> map->m_flags = F2FS_MAP_MAPPED; >>>>> + /* for HW encryption, but to avoid potential issue in future */ >>>>> + if (flag == F2FS_GET_BLOCK_DIO) { >>>>> + blkaddr = map->m_pblk; >>>>> + for (; blkaddr < map->m_pblk + map->m_len; blkaddr++) >>>>> + f2fs_wait_on_block_writeback(sbi, blkaddr); >>>> >>>> Do we need this? IIRC, DIO would give create=1. >>> >>> Yes, we need it. When we are overwriting an existing file, DIO calls >>> f2fs_map_blocks() with create=0. From the DIO code, I see that this happens >>> because blockdev_direct_IO() passes this dio flag DIO_SKIP_HOLES. And then >>> in get_more_blocks(), below code updates create=0, when we are overwriting >>> an existing file. >>> >>> create = dio->op == REQ_OP_WRITE; >>> if (dio->flags & DIO_SKIP_HOLES) { >>> if (fs_startblk <= ((i_size_read(dio->inode) - 1) >> >>> i_blkbits)) >>> create = 0; >>> } >>> >>> ret = (*sdio->get_block)(dio->inode, fs_startblk, >>> map_bh, create); >>> >> >> Got it. >> How about this? >> > > Sorry, this is v2. > >>From b78dd7b2e0317be18716b9496269e9792829f63e Mon Sep 17 00:00:00 2001 > From: Sahitya Tummala > Date: Wed, 10 Oct 2018 10:56:22 +0530 > Subject: [PATCH] f2fs: fix data corruption issue with hardware encryption > > Direct IO can be used in case of hardware encryption. The following > scenario results into data corruption issue in this path - > > Thread A - Thread B- > -> write file#1 in direct IO > -> GC gets kicked in > -> GC submitted bio on meta mapping > for file#1, but pending completion > -> write file#1 again with new data > in direct IO > -> GC bio gets completed now > -> GC writes old data to the new > location and thus file#1 is > corrupted. > > Fix this by submitting and waiting for pending io on meta mapping > for direct IO case in f2fs_map_blocks(). > > Signed-off-by: Sahitya Tummala > Signed-off-by: Jaegeuk Kim Nice catch! Reviewed-by: Chao Yu Thanks,