From nobody Wed Oct 15 22:31:23 2025 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 75F53178890; Sun, 24 Mar 2024 23:39:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711323554; cv=none; b=aoA8BnQ5PADWV9G5TfAxarhubdG9MMURRITcGFpZqXwu0ZUPm2nTcXTIwmmbFyWki9sWuhBX3FFG5Kzt9mCAWRjrmmO/Askizb2GN/8mZ/T719NmPxpl5NnkVpt2iEIcKgEC/b3eo7P2zQc+/uDzpH2/v4ujagB45sSWRKa1tIQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711323554; c=relaxed/simple; bh=ZUeJcfidzG8zRoHAeUDwNst0AIDDyixSOB647G2MPZc=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=gcSCeZGeErcUNqDOrAUe9PDAQlnop64q4MjIq77bNbB9ExyUXrWQhb1Nfk3zUZbbmzqAyabLyhU6BNgWTYWgxSsTp+IJWRAJQAPcUMFR1YQhxRQHVYIkCYw+70DpJBaxKsuT4EUAR5x8K59/zgL9hb5IoPTzuYK3xTaQMkOPF8I= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=sm6L9Oki; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="sm6L9Oki" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 9EEC6C43390; Sun, 24 Mar 2024 23:39:13 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1711323554; bh=ZUeJcfidzG8zRoHAeUDwNst0AIDDyixSOB647G2MPZc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=sm6L9OkioJ0jK/9RIIBKrFKfU3OY0BppfDk7r3DNAoDYaMQEjWkAtSCAMtkRZuds9 ERACsqEaXP47+bNEaKaBCPJ0+TsGWAnLSTbES9skqjKeXOFVqJUs5HxtUsQQs9m8iu Rk00/zmDHi69IO9UPvj4xJnVEtVLX+33PwmNm4O6Fvvlef7cjIztmD+F6LXJLjodIC 8V9tnb1zFYxO5qyPx4fXad3uCvZ6Zsqo8c9YZYr0wc6wymY1IpCjRXcg0yyEUwFukJ caAu5juRQ6uqTTg8kKv34Z/1iWtvqZ6ssXuLj+6VKyt2wzErER4Gh5fu9Zfp+lBjF0 kVY8VrCrLn3Qg== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Chao Yu , Daeho Jeong , Jaegeuk Kim , Sasha Levin Subject: [PATCH 5.15 261/317] f2fs: compress: fix to cover normal cluster write with cp_rwsem Date: Sun, 24 Mar 2024 19:34:01 -0400 Message-ID: <20240324233458.1352854-262-sashal@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240324233458.1352854-1-sashal@kernel.org> References: <20240324233458.1352854-1-sashal@kernel.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: Chao Yu [ Upstream commit fd244524c2cf07b5f4c3fe8abd6a99225c76544b ] 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") Reviewed-by: Daeho Jeong Signed-off-by: Chao Yu Signed-off-by: Jaegeuk Kim Signed-off-by: Sasha Levin --- fs/f2fs/compress.c | 27 ++++++++++++++++++--------- fs/f2fs/data.c | 3 ++- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/fs/f2fs/compress.c b/fs/f2fs/compress.c index f8de64cb8fbcc..88799c6ebd7de 100644 --- a/fs/f2fs/compress.c +++ b/fs/f2fs/compress.c @@ -1445,12 +1445,14 @@ void f2fs_compress_write_end_io(struct bio *bio, st= ruct page *page) } =20 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 =3D cc->inode->i_mapping; - int _submitted, compr_blocks, ret, i; + struct f2fs_sb_info *sbi =3D F2FS_M_SB(mapping); + int submitted, compr_blocks, i; + int ret =3D 0; =20 compr_blocks =3D f2fs_compressed_blocks(cc); =20 @@ -1465,6 +1467,10 @@ static int f2fs_write_raw_pages(struct compress_ctx = *cc, if (compr_blocks < 0) return compr_blocks; =20 + /* overwrite compressed cluster w/ normal cluster */ + if (compr_blocks > 0) + f2fs_lock_op(sbi); + for (i =3D 0; i < cc->cluster_size; i++) { if (!cc->rpages[i]) continue; @@ -1489,7 +1495,7 @@ static int f2fs_write_raw_pages(struct compress_ctx *= cc, if (!clear_page_dirty_for_io(cc->rpages[i])) goto continue_unlock; =20 - ret =3D f2fs_write_single_data_page(cc->rpages[i], &_submitted, + ret =3D f2fs_write_single_data_page(cc->rpages[i], &submitted, NULL, NULL, wbc, io_type, compr_blocks, false); if (ret) { @@ -1497,26 +1503,29 @@ static int f2fs_write_raw_pages(struct compress_ctx= *cc, unlock_page(cc->rpages[i]); ret =3D 0; } else if (ret =3D=3D -EAGAIN) { + ret =3D 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 =3D 0; + goto out; f2fs_io_schedule_timeout(DEFAULT_IO_TIMEOUT); goto retry_write; } - return ret; + goto out; } =20 - *submitted +=3D _submitted; + *submitted_p +=3D submitted; } =20 - f2fs_balance_fs(F2FS_M_SB(mapping), true); +out: + if (compr_blocks > 0) + f2fs_unlock_op(sbi); =20 - return 0; + f2fs_balance_fs(sbi, true); + return ret; } =20 int f2fs_write_multi_pages(struct compress_ctx *cc, diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c index cb63352589390..ecf7f1b5a3658 100644 --- a/fs/f2fs/data.c +++ b/fs/f2fs/data.c @@ -2802,7 +2802,7 @@ int f2fs_write_single_data_page(struct page *page, in= t *submitted, .encrypted_page =3D NULL, .submitted =3D 0, .compr_blocks =3D compr_blocks, - .need_lock =3D LOCK_RETRY, + .need_lock =3D compr_blocks ? LOCK_DONE : LOCK_RETRY, .post_read =3D f2fs_post_read_required(inode) ? 1 : 0, .io_type =3D io_type, .io_wbc =3D wbc, @@ -2887,6 +2887,7 @@ int f2fs_write_single_data_page(struct page *page, in= t *submitted, if (err =3D=3D -EAGAIN) { err =3D f2fs_do_write_data_page(&fio); if (err =3D=3D -EAGAIN) { + f2fs_bug_on(sbi, compr_blocks); fio.need_lock =3D LOCK_REQ; err =3D f2fs_do_write_data_page(&fio); } --=20 2.43.0