Received: by 2002:a05:7412:e794:b0:fa:551:50a7 with SMTP id o20csp2668135rdd; Fri, 12 Jan 2024 18:34:07 -0800 (PST) X-Google-Smtp-Source: AGHT+IFiMcjUcj6vVODjKc8wC7uYVwj6ezx+0s74dPpy8KdsJUsihoCn6RMjfX5kzakLhuctTVXy X-Received: by 2002:a17:907:36c8:b0:a27:6615:1653 with SMTP id bj8-20020a17090736c800b00a2766151653mr1417860ejc.34.1705113247823; Fri, 12 Jan 2024 18:34:07 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1705113247; cv=none; d=google.com; s=arc-20160816; b=v/SfEFxdpPe+RZGwVwcePu2JD5W8uPTGDJWcmMeVm2lAKdHtqMQ08CxBnpx1wSp/f3 XTO1sl6wi/LYgJutw29iWCWX/A2eXCj9wVqDw/6UWRc00mjzJBuzPt0Rj14kQgdFo9cR Mls0vwC1Q9ztlyGxL2FRpvw4QLnKuQssuE2pxiWFg5VYJifvCbMkKICBVnJJtnkNcIzs yVf6ItYDTKHsc4ytHTP54bVocOTSaUMB3R9tIJ6g77+RU+s4p3Cah/lIKiBbdrbE4rMV x3EBFrR63W+QtNExcqsX4ckIi4m5gFi/ih/EUdrOMTtgPGGcdHZY5bC5eDfTP8dUTrmL q6mg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:in-reply-to:autocrypt:from:references:cc :to:content-language:subject:user-agent:mime-version :list-unsubscribe:list-subscribe:list-id:precedence:date:message-id :dkim-signature; bh=5iosQm1jg1vMdIT6bDIPUKOhOrKMVp/JwfSihJ+zoL4=; fh=1D7eQ/0rc+iU6BAcs/KVKWP4G7WgjJ9VzUdfNwtKJ+I=; b=Y6bXhdlBPJa8Et98aN5Dy3ld2hC68tqfEC6O0dIdyXy76StKOTHLoYVPQma61abiMH kwiY726opTkX5MSSzqEu6ZQlOKON9kewCPBWuz5yZXsnybYibGaDcqQFV4LJGhZgQWt1 cZd0b0GwfVlrIuhGlf9gOKlKhYeQtW166QpqytbxVydIb2ItO+pCURa1y9hwcurd1EsX XhAiltBOhbE42AXtYOijkT+c0+ag01UGKnYHRgmqX11bivF7d6ia7YmUFoZOMODqr8U2 wq7+mZXhOb+WCsAV/2zmkkCMSxiKF+nHXtMW3ofKYou575EwOpZBlcWmlTfKLNpApvZW wljw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="a22o6Q/P"; spf=pass (google.com: domain of linux-kernel+bounces-25179-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-25179-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [147.75.80.249]) by mx.google.com with ESMTPS id z17-20020a170906241100b00a2d1c3c7506si263448eja.442.2024.01.12.18.34.07 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Jan 2024 18:34:07 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-25179-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) client-ip=147.75.80.249; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="a22o6Q/P"; spf=pass (google.com: domain of linux-kernel+bounces-25179-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.80.249 as permitted sender) smtp.mailfrom="linux-kernel+bounces-25179-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 539201F240BF for ; Sat, 13 Jan 2024 02:34:07 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 437696139; Sat, 13 Jan 2024 02:33:59 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="a22o6Q/P" Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 446E2184D for ; Sat, 13 Jan 2024 02:33:57 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 018F2C433C7; Sat, 13 Jan 2024 02:33:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1705113237; bh=hxcwXtTvwEHn6hIQwj9BeBtKbKvjTpJufYYmYZ76Hcc=; h=Date:Subject:To:Cc:References:From:In-Reply-To:From; b=a22o6Q/PR0XxzNMbRJ0lqUljE0DaQkZIGPeZ8wj0c3H6cPgAcOQZlHEDplgZPSa2U 4AK1Drk9r6PI+xPpKxDhm1F709eDC+FCo4+DSNDg2RLmvNFJayOz9ZrDvSNPTXedGf J7y/rkQi8huqRGqvENo63tghL3fWBbwJ/EP/F9JZDtKFnZ3xVV1MNvM9rD1fjz1bFm AA2QhB31sDiRNngAsLqEch/Eohdp0pjeEB9Ns+fCatnL9kgQBwPv1VOyEnbeauOWhF Ba0GVu2w8W0gjTd5BIP5EWPusiqVKVstDAAjLajdPnG13Gshshn7DXTAprjC8C0UJd G0FoRWBrABVoA== Message-ID: <09ca35ab-e599-41c3-86ce-73a0cb776858@kernel.org> Date: Sat, 13 Jan 2024 10:33:54 +0800 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH v4 2/6] f2fs: compress: fix to cover normal cluster write with cp_rwsem Content-Language: en-US To: Jaegeuk Kim Cc: linux-f2fs-devel@lists.sourceforge.net, linux-kernel@vger.kernel.org References: <20240111064208.2969599-1-chao@kernel.org> <20240111064208.2969599-2-chao@kernel.org> From: Chao Yu Autocrypt: addr=chao@kernel.org; keydata= xsFNBFYs6bUBEADJuxYGZRMvAEySns+DKVtVQRKDYcHlmj+s9is35mtlhrLyjm35FWJY099R 6DL9bp8tAzLJOMBn9RuTsu7hbRDErCCTiyXWAsFsPkpt5jgTOy90OQVyTon1i/fDz4sgGOrL 1tUfcx4m5i5EICpdSuXm0dLsC5lFB2KffLNw/ZfRuS+nNlzUm9lomLXxOgAsOpuEVps7RdYy UEC81IYCAnweojFbbK8U6u4Xuu5DNlFqRFe/MBkpOwz4Nb+caCx4GICBjybG1qLl2vcGFNkh eV2i8XEdUS8CJP2rnp0D8DM0+Js+QmAi/kNHP8jzr7CdG5tje1WIVGH6ec8g8oo7kIuFFadO kwy6FSG1kRzkt4Ui2d0z3MF5SYgA1EWQfSqhCPzrTl4rJuZ72ZVirVxQi49Ei2BI+PQhraJ+ pVXd8SnIKpn8L2A/kFMCklYUaLT8kl6Bm+HhKP9xYMtDhgZatqOiyVV6HFewfb58HyUjxpza 1C35+tplQ9klsejuJA4Fw9y4lhdiFk8y2MppskaqKg950oHiqbJcDMEOfdo3NY6/tXHFaeN1 etzLc1N3Y0pG8qS/mehcIXa3Qs2fcurIuLBa+mFiFWrdfgUkvicSYqOimsrE/Ezw9hYhAHq4 KoW4LQoKyLbrdOBJFW0bn5FWBI4Jir1kIFHNgg3POH8EZZDWbQARAQABzRlDaGFvIFl1IDxj aGFvQGtlcm5lbC5vcmc+wsF3BBMBCgAhBQJWLOm1AhsDBQsJCAcDBRUKCQgLBRYCAwEAAh4B AheAAAoJEKTPgB1/p52Gm2MP/0zawCU6QN7TZuJ8R1yfdhYr0cholc8ZuPoGim69udQ3otet wkTNARnpuK5FG5la0BxFKPlazdgAU1pt+dT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SNIeHvIbDHTUZrewD2A6ARw1VYg1vhJbqUE4qKoUL1wLmxHrk+zHUEyLHUq aDpDMZArdNKpT6Nh9ySUFzlWkHUsj7uUNxU3A6GTum2aU3Gh0CD1p8+FYlG1dGhO5boTIUsR 6ho73ZNk1bwUj/wOcqWu+ZdnQa3zbfvMI9o/kFlOu8iTGlD8sNjJK+Y/fPK3znFqoqqKmSFZ aiRALjAZH6ufspvYAJEJE9eZSX7Rtdyt30MMHQARAQABwsFfBBgBCgAJBQJWLOm1AhsMAAoJ EKTPgB1/p52GPpoP/2LOn/5KSkGHGmdjzRoQHBTdm2YV1YwgADg52/mU68Wo6viStZqcVEnX 3ALsWeETod3qeBCJ/TR2C6hnsqsALkXMFFJTX8aRi/E4WgBqNvNgAkWGsg5XKB3JUoJmQLqe CGVCT1OSQA/gTEfB8tTZAGFwlw1D3W988CiGnnRb2EEqU4pEuBoQir0sixJzFWybf0jjEi7P pODxw/NCyIf9GNRNYByUTVKnC7C51a3b1gNs10aTUmRfQuu+iM5yST5qMp4ls/yYl5ybr7N1 zSq9iuL13I35csBOn13U5NE67zEb/pCFspZ6ByU4zxChSOTdIJSm4/DEKlqQZhh3FnVHh2Ld eG/Wbc1KVLZYX1NNbXTz7gBlVYe8aGpPNffsEsfNCGsFDGth0tC32zLT+5/r43awmxSJfx2P 5aGkpdszvvyZ4hvcDfZ7U5CBItP/tWXYV0DDl8rCFmhZZw570vlx8AnTiC1v1FzrNfvtuxm3 92Qh98hAj3cMFKtEVbLKJvrc2AO+mQlS7zl1qWblEhpZnXi05S1AoT0gDW2lwe54VfT3ySon 8Klpbp5W4eEoY21tLwuNzgUMxmycfM4GaJWNCncKuMT4qGVQO9SPFs0vgUrdBUC5Pn5ZJ46X mZA0DUz0S8BJtYGI0DUC/jAKhIgy1vAx39y7sAshwu2VILa71tXJ In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Thanks, let me resend v5 w/ blow cleanups. On 2024/1/13 9:39, Jaegeuk Kim wrote: > Cleaned up a bit: > > --- a/fs/f2fs/compress.c > +++ b/fs/f2fs/compress.c > @@ -1443,13 +1443,14 @@ void f2fs_compress_write_end_io(struct bio *bio, struct page *page) > } > > static int f2fs_write_raw_pages(struct compress_ctx *cc, > - int *submitted, > + int *submitted_p, > struct writeback_control *wbc, > enum iostat_type io_type) > { > struct address_space *mapping = cc->inode->i_mapping; > struct f2fs_sb_info *sbi = F2FS_M_SB(mapping); > - int _submitted, compr_blocks, ret = 0, i; > + int submitted, compr_blocks, i; > + int ret = 0; > > compr_blocks = f2fs_compressed_blocks(cc); > > @@ -1492,7 +1493,7 @@ static int f2fs_write_raw_pages(struct compress_ctx *cc, > if (!clear_page_dirty_for_io(cc->rpages[i])) > goto continue_unlock; > > - ret = f2fs_write_single_data_page(cc->rpages[i], &_submitted, > + ret = f2fs_write_single_data_page(cc->rpages[i], &submitted, > NULL, NULL, wbc, io_type, > compr_blocks, false); > if (ret) { > @@ -1514,7 +1515,7 @@ static int f2fs_write_raw_pages(struct compress_ctx *cc, > goto out; > } > > - *submitted += _submitted; > + *submitted_p += submitted; > } > > out: > > On 01/11, Chao Yu wrote: >> When we overwrite compressed cluster w/ normal cluster, we should >> not unlock cp_rwsem during f2fs_write_raw_pages(), otherwise data >> will be corrupted if partial blocks were persisted before CP & SPOR, >> due to cluster metadata wasn't updated atomically. >> >> Fixes: 4c8ff7095bef ("f2fs: support data compression") >> Signed-off-by: Chao Yu >> --- >> fs/f2fs/compress.c | 20 ++++++++++++++------ >> fs/f2fs/data.c | 3 ++- >> 2 files changed, 16 insertions(+), 7 deletions(-) >> >> diff --git a/fs/f2fs/compress.c b/fs/f2fs/compress.c >> index 9940b7886e5d..bf4cfab67aec 100644 >> --- a/fs/f2fs/compress.c >> +++ b/fs/f2fs/compress.c >> @@ -1448,7 +1448,8 @@ static int f2fs_write_raw_pages(struct compress_ctx *cc, >> enum iostat_type io_type) >> { >> struct address_space *mapping = cc->inode->i_mapping; >> - int _submitted, compr_blocks, ret, i; >> + struct f2fs_sb_info *sbi = F2FS_M_SB(mapping); >> + int _submitted, compr_blocks, ret = 0, i; >> >> compr_blocks = f2fs_compressed_blocks(cc); >> >> @@ -1463,6 +1464,10 @@ static int f2fs_write_raw_pages(struct compress_ctx *cc, >> if (compr_blocks < 0) >> return compr_blocks; >> >> + /* overwrite compressed cluster w/ normal cluster */ >> + if (compr_blocks > 0) >> + f2fs_lock_op(sbi); >> + >> for (i = 0; i < cc->cluster_size; i++) { >> if (!cc->rpages[i]) >> continue; >> @@ -1495,26 +1500,29 @@ static int f2fs_write_raw_pages(struct compress_ctx *cc, >> unlock_page(cc->rpages[i]); >> ret = 0; >> } else if (ret == -EAGAIN) { >> + ret = 0; >> /* >> * for quota file, just redirty left pages to >> * avoid deadlock caused by cluster update race >> * from foreground operation. >> */ >> if (IS_NOQUOTA(cc->inode)) >> - return 0; >> - ret = 0; >> + goto out; >> f2fs_io_schedule_timeout(DEFAULT_IO_TIMEOUT); >> goto retry_write; >> } >> - return ret; >> + goto out; >> } >> >> *submitted += _submitted; >> } >> >> - f2fs_balance_fs(F2FS_M_SB(mapping), true); >> +out: >> + if (compr_blocks > 0) >> + f2fs_unlock_op(sbi); >> >> - return 0; >> + f2fs_balance_fs(sbi, true); >> + return ret; >> } >> >> int f2fs_write_multi_pages(struct compress_ctx *cc, >> diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c >> index 81f9e2cc49e2..b171a9980f6a 100644 >> --- a/fs/f2fs/data.c >> +++ b/fs/f2fs/data.c >> @@ -2839,7 +2839,7 @@ int f2fs_write_single_data_page(struct page *page, int *submitted, >> .encrypted_page = NULL, >> .submitted = 0, >> .compr_blocks = compr_blocks, >> - .need_lock = LOCK_RETRY, >> + .need_lock = compr_blocks ? LOCK_DONE : LOCK_RETRY, >> .post_read = f2fs_post_read_required(inode) ? 1 : 0, >> .io_type = io_type, >> .io_wbc = wbc, >> @@ -2920,6 +2920,7 @@ int f2fs_write_single_data_page(struct page *page, int *submitted, >> if (err == -EAGAIN) { >> err = f2fs_do_write_data_page(&fio); >> if (err == -EAGAIN) { >> + f2fs_bug_on(sbi, compr_blocks); >> fio.need_lock = LOCK_REQ; >> err = f2fs_do_write_data_page(&fio); >> } >> -- >> 2.40.1