Received: by 2002:a05:7412:e794:b0:fa:551:50a7 with SMTP id o20csp2689193rdd; Fri, 12 Jan 2024 19:58:02 -0800 (PST) X-Google-Smtp-Source: AGHT+IHqwhZcB2XNpAWH6BPgQnnDrvEQMc1ed1jOaP/G/zXyxm/j2punznGsTjzQkHF91WFO12i+ X-Received: by 2002:a05:6214:c4b:b0:681:9b4:210f with SMTP id r11-20020a0562140c4b00b0068109b4210fmr2358270qvj.97.1705118282756; Fri, 12 Jan 2024 19:58:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1705118282; cv=none; d=google.com; s=arc-20160816; b=S4HteM+8PKtmoccUm9qn9uxnm91Ivx3ixORjq06CH7NnEsHboYrOnKfd+fGVxA5AVn 3jZLQaYAhAjDCwa4lKRpokgeW6fdCs0G1vGrNL9oBxTzqfd3r45868Nzo9/3ZjHyGAYK 1a6+CiHuPGfT0d5sN0dPTPp/J+TJccItlACRGOWdWEJrdhD+m2MLnEm+guTD2GwwSiRc +Hmex3qCZj3SxMQUVK94dS58k7TTIEiOBRIyclDonRkXkCRk3m9UQ7ZAsZcreIcHdBfK +VSeFl4Ga0dCAwCidXbNodCslE8HVRq4iuiFWNIgtxK6GGugBHJlbgWCE6luC/qUn1uX 8Y4Q== 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=ZS356O51aGJ1N2N0Prl+HSBzuRtRUv+MzcWcawjdxZM=; fh=3gbPhre1OB9gPaTtmCq689W9XI462kLAqPsb9Z8n0A8=; b=rWgzlPTGYnnS3y4LPgp96oRodyro55+dZIjP1kSgNHFJ3H10yg47uRVZzHD+Ow0w5O gZPNKEkGm+BkvB1/02/wS/M3zGb/10NrhRjwPGJ7ICIv7d6/j11Lkn4tXn/Ph2mrfXxD C/750tnBrn3IqwgbkphZHtfQ5ZHdcuVnZwjvcCCZ3LEaP6NDjsBJZ/dvSL63ISN9NGL6 tXoiZ2nVv5P53LVLBr1V6kJ9DUnslD0GqsBftMrNPmzEimBrbL24VpSGgCv40uktc3+S 4z6oesD6DaCQb9M9pg5dGSh/BNA+zKP8qH/nWjx3qwOSeEvA6DPtOgH6/NSYdvOkseXS +NOA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=OTua4APu; spf=pass (google.com: domain of linux-kernel+bounces-25189-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-25189-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 b12-20020a0cb3cc000000b0067f8af9621dsi29491qvf.59.2024.01.12.19.58.02 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 12 Jan 2024 19:58:02 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-25189-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=OTua4APu; spf=pass (google.com: domain of linux-kernel+bounces-25189-linux.lists.archive=gmail.com@vger.kernel.org designates 147.75.199.223 as permitted sender) smtp.mailfrom="linux-kernel+bounces-25189-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 83C2C1C22ADF for ; Sat, 13 Jan 2024 03:58:02 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 01A25171DD; Sat, 13 Jan 2024 03:57:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="OTua4APu" 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 2F62DFC16 for ; Sat, 13 Jan 2024 03:57:27 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id EDE14C43394; Sat, 13 Jan 2024 03:57:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1705118247; bh=V21gcWFsGMtYKh3R/2HUCTY5gVSXqKTY8pGHYmR5nmQ=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=OTua4APuYBGQcI6gnfTPP3KBgkVKWQ6SWD2lgqSxyiRBEG5BZaeBkQ3F0L7P92cSM tHHcHWW8taPdMUsxw+M+ThkKTIAafdB7v9X+5/l58zp1nIxpxGsMOyQBfiMmqPFbJ3 3GZthW7qPAUTz4ydI2mk3UyBfvP15B/x0Bzmy+PWKNgQi+ELpPG9xqKtm/TwDg52op XAzdPg/vTB64SMrDt04+o9tFzbdPVPcNnnd6OZV0ljKuw8kLHjcz/XcBLDn1gqHCxQ gi1+LeGKPwxHc+wvadWyxGBiyFeMbNtPRGQ+oCJxj2XTTVdsyHcUsCu+IwVis5hrcZ Yt9E3Zw1XmTdw== From: Chao Yu To: jaegeuk@kernel.org Cc: linux-f2fs-devel@lists.sourceforge.net, linux-kernel@vger.kernel.org, Chao Yu , Daeho Jeong Subject: [PATCH v5 4/6] f2fs: compress: fix to avoid inconsistence bewteen i_blocks and dnode Date: Sat, 13 Jan 2024 03:41:30 +0800 Message-Id: <20240112194132.25637-4-chao@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20240112194132.25637-1-chao@kernel.org> References: <20240112194132.25637-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 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 --- 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 b171a9980f6a..8d2ace723310 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 50f3d546ded8..69e71460a950 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 4aef2310395f..a02c530c7e4b 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 4c8836ded90f..ef5b3848426b 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.40.1