Received: by 2002:ab2:6857:0:b0:1ef:ffd0:ce49 with SMTP id l23csp2396459lqp; Sun, 24 Mar 2024 17:40:42 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCUbHj6v3gREDYNlnEFf48z5EeqhI2oAD0SMVNtYbSeg+HFep4A8QRuS4Fd2MJtVlsXVS+OVXZoRe2EogpihTok5MS9BP5rz789gYzam8Q== X-Google-Smtp-Source: AGHT+IHY9pOwYnEp3uwmjwrjC72ve0cyW1Jw4pETdZbePt3xuUA3h0drQDyR0EJqN/aRfwfFXx5l X-Received: by 2002:a05:620a:4691:b0:78a:54a9:d48 with SMTP id bq17-20020a05620a469100b0078a54a90d48mr3172030qkb.28.1711327242268; Sun, 24 Mar 2024 17:40:42 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1711327242; cv=pass; d=google.com; s=arc-20160816; b=VYrJJd7jdIUS0Ljg3T4VHQ+Zsw48DsUTOPKaqCmxHtGMgg/ddt7AvN8rl1Wg59CtbA yL1BjhNVIoo4Glk3URQstpJ6Ib4eZsGxCsxuRIoN1YO/QcpbDZ2ihFWqL7mQSspODvfo kBrfT6Ox93GDAvh2saS5aOUuJIbP4ucc9zn7jEYH5rUgaIQYTQdQinbQE5Wts6KkEWtZ zuSMSiDG13o3ymsRNYP3H6/ywzUGPWFr8c4MFa54yWX81psoKykj9H2pq5I3a5dVBVE3 lvyV98zyOpgT3tiiNzw6CsPTQA8dr4dNwVGIDUDtll8fXuisvKyhNK8RpszzVY4r9cG8 MgQA== ARC-Message-Signature: i=2; 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=YjbOic78LsVmhuQzB/Z4exvj6IR9lWZblL7ZaMjitCM=; fh=j/x7sSRnXo0LaHT7QL10tuC6FCXce9x5R+7s6GpGj34=; b=qO28HXnQYE1laZDJzS5+v9n5zsdLQTC8rm9HiY6nVf/Ge6temwLOeiEE0qAcldKZsa Kwjo+zXBql9dijc+Pn/LB8TjZwaSjq3wrZuSkI0+alhVaemCpG0o+FBlQ7Jr8ld89va6 Ue0Ci6gHgv000y5RF4Xve4RG4uzfoolEifnC5e4aAshHnY+d0/TnVk6+RhxzQqE/osxA Rk5ICD9LK7ZCYyii/1rFS5mrEJFbyz8ff0T/dEbugm9HlIdvHZz6YL/NP79vuowcO3J/ +90Q/ZgwnYGjH0FcgOq8ypBx0w3EqomCZENfS6QBe814zFcCJZDHZSlOSB6ilO2q3Bcu 1Y/g==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=fQc3oQfS; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-113453-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-113453-linux.lists.archive=gmail.com@vger.kernel.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [147.75.199.223]) by mx.google.com with ESMTPS id v20-20020a05620a0a9400b00789e8f39b9dsi4435351qkg.697.2024.03.24.17.40.42 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 24 Mar 2024 17:40:42 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-113453-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) client-ip=147.75.199.223; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=fQc3oQfS; arc=pass (i=1 dkim=pass dkdomain=kernel.org); spf=pass (google.com: domain of linux-kernel+bounces-113453-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-113453-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 ny.mirrors.kernel.org (Postfix) with ESMTPS id EC81F1C23D1C for ; Mon, 25 Mar 2024 00:40:41 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id F165C1AFCC2; Sun, 24 Mar 2024 22:44:15 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="fQc3oQfS" 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 E57BF1AF1AA; Sun, 24 Mar 2024 22:44: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=1711320255; cv=none; b=N9aUDktvqTuzPY46Md0sk91PimziIW1OAflKlSCP5IQdxC0a4jmg0camRcPBAWNdOe9GDd/Q3I14XEuGEEMqStVwU6EtwFkH0QkZtPVMQ2HnmR+wNQIl3JJR8JSIbMvziWjwU/OI1hoESjZPAgYh4F5tIcFmZXYeC0FwuXTrXnk= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1711320255; c=relaxed/simple; bh=Zvvm+DKLRRxl6RYhmY7CTyKsRJvMnQ1C16WmpR9zHBE=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=KHcT0geMIFfeXg8m9ZMB6vHm+eCs/cqWqISseFx2VJ12JuPdfgYwQAsEe1iokUlfesou6urDdW9IlhHbUzQag3CneAbLLqhj0GMG25dwwexFv55ZthErubZWksZw000OAhPTi4iT9eqTQ/BS3lj2AyjNuklk1ZuMfBZMiVP/JUQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=fQc3oQfS; arc=none smtp.client-ip=10.30.226.201 Received: by smtp.kernel.org (Postfix) with ESMTPSA id 19A90C43399; Sun, 24 Mar 2024 22:44:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1711320254; bh=Zvvm+DKLRRxl6RYhmY7CTyKsRJvMnQ1C16WmpR9zHBE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=fQc3oQfSBK/Oo+MpqXie2HTqGufgv1MMl2FYs5toq5gXJaAFmXN3i2Mv8YlEkU2hi bLSmbMTVsGOSEQFYfwtpKpdAoQgBTyt+egU4qJ4qWMxjfDukcVdBgavA19yDCCSwHg k4y9EkdqpaTpcqma7F7M8Oi1lbtnikXusM2L9XQcxF/jsazQ3gQL2Eqb84keJ5ex4e nqN/Nuq06Z2I0YpsdwqoAMObGJ23XwQZjthDkz6lueuBB4fjy7bfFTCa0NRD3e/AwP Q6lckBWoozNeDNWqZZXr227StM+ExU5Ul2PKa/xUIg6QEI4x6eLxoxLeway36Zy08b J+r1/wCKG4sqw== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Chao Yu , Daeho Jeong , Jaegeuk Kim , Sasha Levin Subject: [PATCH 6.8 562/715] f2fs: compress: fix to avoid inconsistence bewteen i_blocks and dnode Date: Sun, 24 Mar 2024 18:32:21 -0400 Message-ID: <20240324223455.1342824-563-sashal@kernel.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240324223455.1342824-1-sashal@kernel.org> References: <20240324223455.1342824-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: 8bit From: Chao Yu [ Upstream commit 54607494875edd636aff3c21ace3ad9a7da758a9 ] In reserve_compress_blocks(), we update blkaddrs of dnode in prior to inc_valid_block_count(), it may cause inconsistent status bewteen i_blocks and blkaddrs once inc_valid_block_count() fails. To fix this issue, it needs to reverse their invoking order. Fixes: c75488fb4d82 ("f2fs: introduce F2FS_IOC_RESERVE_COMPRESS_BLOCKS") Reviewed-by: Daeho Jeong Signed-off-by: Chao Yu Signed-off-by: Jaegeuk Kim Signed-off-by: Sasha Levin --- fs/f2fs/data.c | 5 +++-- fs/f2fs/f2fs.h | 7 ++++++- fs/f2fs/file.c | 26 ++++++++++++++------------ fs/f2fs/segment.c | 2 +- 4 files changed, 24 insertions(+), 16 deletions(-) diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c index 7a93a99fbd04b..65fe48bb17d16 100644 --- a/fs/f2fs/data.c +++ b/fs/f2fs/data.c @@ -1219,7 +1219,8 @@ int f2fs_reserve_new_blocks(struct dnode_of_data *dn, blkcnt_t count) if (unlikely(is_inode_flag_set(dn->inode, FI_NO_ALLOC))) return -EPERM; - if (unlikely((err = inc_valid_block_count(sbi, dn->inode, &count)))) + err = inc_valid_block_count(sbi, dn->inode, &count, true); + if (unlikely(err)) return err; trace_f2fs_reserve_new_blocks(dn->inode, dn->nid, @@ -1476,7 +1477,7 @@ static int __allocate_data_block(struct dnode_of_data *dn, int seg_type) dn->data_blkaddr = f2fs_data_blkaddr(dn); if (dn->data_blkaddr == NULL_ADDR) { - err = inc_valid_block_count(sbi, dn->inode, &count); + err = inc_valid_block_count(sbi, dn->inode, &count, true); if (unlikely(err)) return err; } diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h index 99511d9a92c0c..9fd2143378952 100644 --- a/fs/f2fs/f2fs.h +++ b/fs/f2fs/f2fs.h @@ -2252,7 +2252,7 @@ static inline bool __allow_reserved_blocks(struct f2fs_sb_info *sbi, static inline void f2fs_i_blocks_write(struct inode *, block_t, bool, bool); static inline int inc_valid_block_count(struct f2fs_sb_info *sbi, - struct inode *inode, blkcnt_t *count) + struct inode *inode, blkcnt_t *count, bool partial) { blkcnt_t diff = 0, release = 0; block_t avail_user_block_count; @@ -2292,6 +2292,11 @@ static inline int inc_valid_block_count(struct f2fs_sb_info *sbi, avail_user_block_count = 0; } if (unlikely(sbi->total_valid_block_count > avail_user_block_count)) { + if (!partial) { + spin_unlock(&sbi->stat_lock); + goto enospc; + } + diff = sbi->total_valid_block_count - avail_user_block_count; if (diff > *count) diff = *count; diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c index 941e02c0953cc..1ff1c45e19271 100644 --- a/fs/f2fs/file.c +++ b/fs/f2fs/file.c @@ -3614,14 +3614,16 @@ static int reserve_compress_blocks(struct dnode_of_data *dn, pgoff_t count) blkcnt_t reserved; int ret; - for (i = 0; i < cluster_size; i++, dn->ofs_in_node++) { - blkaddr = f2fs_data_blkaddr(dn); + for (i = 0; i < cluster_size; i++) { + blkaddr = data_blkaddr(dn->inode, dn->node_page, + dn->ofs_in_node + i); if (i == 0) { - if (blkaddr == COMPRESS_ADDR) - continue; - dn->ofs_in_node += cluster_size; - goto next; + if (blkaddr != COMPRESS_ADDR) { + dn->ofs_in_node += cluster_size; + goto next; + } + continue; } /* @@ -3634,8 +3636,6 @@ static int reserve_compress_blocks(struct dnode_of_data *dn, pgoff_t count) compr_blocks++; continue; } - - f2fs_set_data_blkaddr(dn, NEW_ADDR); } reserved = cluster_size - compr_blocks; @@ -3644,12 +3644,14 @@ static int reserve_compress_blocks(struct dnode_of_data *dn, pgoff_t count) if (reserved == 1) goto next; - ret = inc_valid_block_count(sbi, dn->inode, &reserved); - if (ret) + ret = inc_valid_block_count(sbi, dn->inode, &reserved, false); + if (unlikely(ret)) return ret; - if (reserved != cluster_size - compr_blocks) - return -ENOSPC; + for (i = 0; i < cluster_size; i++, dn->ofs_in_node++) { + if (f2fs_data_blkaddr(dn) == NULL_ADDR) + f2fs_set_data_blkaddr(dn, NEW_ADDR); + } f2fs_i_compr_blocks_update(dn->inode, compr_blocks, true); diff --git a/fs/f2fs/segment.c b/fs/f2fs/segment.c index 4c8836ded90fc..ef5b3848426b0 100644 --- a/fs/f2fs/segment.c +++ b/fs/f2fs/segment.c @@ -248,7 +248,7 @@ static int __replace_atomic_write_block(struct inode *inode, pgoff_t index, } else { blkcnt_t count = 1; - err = inc_valid_block_count(sbi, inode, &count); + err = inc_valid_block_count(sbi, inode, &count, true); if (err) { f2fs_put_dnode(&dn); return err; -- 2.43.0