Received: by 2002:a05:7412:b995:b0:f9:9502:5bb8 with SMTP id it21csp3981873rdb; Thu, 28 Dec 2023 06:32:30 -0800 (PST) X-Google-Smtp-Source: AGHT+IGAGVc401LMEzhK2tQoXJgv7YE2gKcNKhJrgnLdNN2HKVgKA1prdGcDpBnL1FEoPT3xlEql X-Received: by 2002:a17:907:90c4:b0:a27:32aa:2dd7 with SMTP id gk4-20020a17090790c400b00a2732aa2dd7mr1455692ejb.90.1703773949913; Thu, 28 Dec 2023 06:32:29 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1703773949; cv=none; d=google.com; s=arc-20160816; b=gm3dlz7oLQhHjH6cCrRr65jp3w7HQG6FJR1MOoaeI07iYC87h2rmey3powhsWwp1LT iN3aWdy1vZ/JsESO8MhhNdVc/k0jvTzOUGVsYFyCBOYlPqgB4snGXRWPLIcT3NG7nIPj Df1KB9BXKEeqKHoVQONu+zCBFnIgRxQcHn/D5zxLt44SQBZ7yUUGk9PIAecJ6UYk1Njh r8O1FkKavZmwD1rnBQeFKca/W1HOY+p9cI3+8rTrzC6AdIgTkx3UwRg590l4A0FqYGk/ ceHU0QjzJ/xjAXucVgJutNIVqe2nSn9UT8i6+XAqIIxNZ0miBIif5+KIG6dt3GYVS6CI AeWg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=I7XXUYIr8Q9KF+4scUbYcSJk+NkBf5I8stoKQ5y70L8=; fh=X1/oBJvTGJUQ0YtRp03u+3tPfRw/dXXALWynfOSlfQQ=; b=tJcMiip3JJ0Br8UoefexW0y3sgOFP6UHHMSM00HlvZTL5h0ues9ZfAzXDEue07VOEA YTJ7tDarrKNgP1kxt77Lksj10Dmz/JfAXnfGiWNvsXfWxcVL6Vwk1jyEkGGc6QTXfgWK z9KEPeH7173nFZvyZNOYsKvq9KgdwVDDZRRsyiWCzDs1sYrvtSMieqjdxJBi/bXwwZuh s/CLvTckqOotgVrGszzLQjEutJtFajUKXiRLJk8kDd5yVpDpkqNUHxCdFda+bTm3S1Qi T1fQHPhSp4IB7ye85MkjL6Bm8y2ZnRQfw0GKeqiUUO9C88NtbElOwV+2R0esR+WYXfhI wRNA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="KbL/mcEp"; spf=pass (google.com: domain of linux-kernel+bounces-12704-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-12704-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. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id l7-20020a1709061c4700b00a2359752f88si6810832ejg.557.2023.12.28.06.32.29 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Dec 2023 06:32:29 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-12704-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b="KbL/mcEp"; spf=pass (google.com: domain of linux-kernel+bounces-12704-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-12704-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 790651F23451 for ; Thu, 28 Dec 2023 14:32:29 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id F18A1F51F; Thu, 28 Dec 2023 14:32:06 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="KbL/mcEp" X-Original-To: linux-kernel@vger.kernel.org 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 30050F4E4 for ; Thu, 28 Dec 2023 14:32:06 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9777DC433C9; Thu, 28 Dec 2023 14:32:04 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1703773926; bh=/uXMDvSOj4ElzaKYgiipftBm0qNMtUebJdjXyJwsNz0=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KbL/mcEp6TTfyO6ngjT22PS7q4xzi+uVlJnUWiUE6+Lad500sZw+hkyeIF2jx0jMh S3//2yv+6t5dC/rAMejbCiK/h30KLr+9LQgThbN8oyH45LKus/kS5aa/4en7zxybGj 5yR7Sx0HWr5+Iy5MNXeMiARK61DzVOYcay4jXxhlZBZsRHt3qiiG/BmBVUdgk3Rd4Z JZ81dAfN4Lws9PwMTqW+CBzZ8i/dyn1HOT6P07RfzpZ6RxzUspMjSIcrXuKBBB8uiO VRuk+9wdHU+U5AJWsXbJvz+n+/1Iu8epGMprZ7+0ZjJM5r4gGUOmOM6HAlOLhH9Jds rK0j1Kbvw1gFw== From: Chao Yu To: jaegeuk@kernel.org Cc: linux-f2fs-devel@lists.sourceforge.net, linux-kernel@vger.kernel.org, Chao Yu Subject: [PATCH v3 2/6] f2fs: compress: fix to cover normal cluster write with cp_rwsem Date: Thu, 28 Dec 2023 22:31:48 +0800 Message-Id: <20231228143152.1543509-2-chao@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20231228143152.1543509-1-chao@kernel.org> References: <20231228143152.1543509-1-chao@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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