Received: by 2002:a89:2c3:0:b0:1ed:23cc:44d1 with SMTP id d3csp833924lqs; Tue, 5 Mar 2024 19:49:02 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCXXUZD+crnwleD1DpSXrjj+8VoGjPKTAqyvyZSnUtkolooBRxVBNMRYjmXAZdXQDLJDyXn4HPJxxsv2lvD9DTllo0MfcF1fNh6cn092AQ== X-Google-Smtp-Source: AGHT+IE7Y6EvsLKr3itodqTq0Rg10jr0xrPfbPp+YxyRSd4ScC4g7/JBsxHzjIoXdC6APBk7zO0W X-Received: by 2002:a17:903:455:b0:1dc:a28a:52b9 with SMTP id iw21-20020a170903045500b001dca28a52b9mr4306155plb.8.1709696942508; Tue, 05 Mar 2024 19:49:02 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1709696942; cv=pass; d=google.com; s=arc-20160816; b=QsFiB5X4jrN2Pk+wKKFc3ERB18XS+SeDXhPE5CULDUAN8u1bR7X+1BGiYIa0Zkmfqt AOmh0YkYuhRc5ee+UQKjAalt9kChv3n/R0md+kXeLHmxjNIQlDjRr7HSFix02pCiyG4U AkAheCi20cdhVSNKADPSkdFjP3M0b0wqXo5YkhG1IngwUuIndN2rLpTJumUpEUYd6CQX iPvE2FoG6nM6jNyDX/uBtgn5oTl0cTreCXi511/N0/wPoY9nn28Fq1Y+8hJdj2e/T82j 96W1nkmmQmJ7+Ik7CjAgVobrGUe7rSzLDnkIrz2EdxOL4XaX6FahZLa9U1aq53gi7z5k /g8g== 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; bh=E/EG/9OUysCCu2EIMuyX5R2856qMq7OXIrJcnhG3wx0=; fh=bh6UKUyFOmvIubC1nu1HsBtu7beYTxZFZ7OMgwEHxR0=; b=PREIrpT4MuJrfnF37SbFDG9lCxc0GogCyElO2mlhJ8yKexKOTN+Kztz+zD3+JP3BbG yqYu6XhWawN71K/HL6E//p+DzI7n31ZzrI0qlvVSqLPpBZVaAwWWMV02LjrD+rC6NWrR YmfNxUiHZ20kk4NVMESfgINorjcSUKoHDxlR8BRhgILywjR2E8FiMlmNGNvWRIwOxIt/ Sn6a4v5c40LcTwOI5nF0kH4OOy4vkmtDqmBMlZXgovFOy0ZASOnAuEOhMBINcmW0EiTN lCNa4DUpKyC2oh9x7wmQvUGWIAvnAnIjNb8fUr04r6VAOa7oySl7pbR+Ef5+kyMJy1AX xg/w==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; arc=pass (i=1 spf=pass spfdomain=unisoc.com); spf=pass (google.com: domain of linux-kernel+bounces-93298-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-93298-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from sv.mirrors.kernel.org (sv.mirrors.kernel.org. [139.178.88.99]) by mx.google.com with ESMTPS id y8-20020a17090322c800b001dcad9d9e7dsi11570887plg.576.2024.03.05.19.49.02 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Mar 2024 19:49:02 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-93298-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) client-ip=139.178.88.99; Authentication-Results: mx.google.com; arc=pass (i=1 spf=pass spfdomain=unisoc.com); spf=pass (google.com: domain of linux-kernel+bounces-93298-linux.lists.archive=gmail.com@vger.kernel.org designates 139.178.88.99 as permitted sender) smtp.mailfrom="linux-kernel+bounces-93298-linux.lists.archive=gmail.com@vger.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 sv.mirrors.kernel.org (Postfix) with ESMTPS id 3332228138D for ; Wed, 6 Mar 2024 03:49:02 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id AFEC217BBB; Wed, 6 Mar 2024 03:48:45 +0000 (UTC) Received: from SHSQR01.spreadtrum.com (unknown [222.66.158.135]) (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 BC896134BF for ; Wed, 6 Mar 2024 03:48:41 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=222.66.158.135 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709696925; cv=none; b=ShLvqOvBGqGJFpb8OCmyFiGJi8YhaJyd81pcBVxIRqvWu755JpEMZgaJmB0SxRGCbr5JbrI+49JcNL4BQUttmtA43FyKl6mHV34WUGh6J5YkULe/Baufd1ifMlUUtX7gbn38wNgnh6J5qrPeW93smtwPMiKumC6NSHJjgg/7EjM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1709696925; c=relaxed/simple; bh=ryCq9rmR08wSeIJS1t2fQGbNd6aKB7Mn8LOJhaMZoFk=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=R03SgQhkCF+oaiWF+2d5Ave83MkLm275a7jttO7blHodsLkRJYbZXU8onpFkk2Xg/7OyC/Iq6WvV47i6phTeJURoxndAa5kwZ7ZhxJygMtDk++OSBqJMbasixYl//b4tEfro2vV23mnyRjRt/eN153lSsuoNFLISSShNav1RmKI= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=unisoc.com; spf=pass smtp.mailfrom=unisoc.com; arc=none smtp.client-ip=222.66.158.135 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=unisoc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=unisoc.com Received: from dlp.unisoc.com ([10.29.3.86]) by SHSQR01.spreadtrum.com with ESMTP id 4263m2C3002928; Wed, 6 Mar 2024 11:48:02 +0800 (+08) (envelope-from Xiuhong.Wang@unisoc.com) Received: from SHDLP.spreadtrum.com (bjmbx01.spreadtrum.com [10.0.64.7]) by dlp.unisoc.com (SkyGuard) with ESMTPS id 4TqJK02Dtrz2KZgsX; Wed, 6 Mar 2024 11:47:00 +0800 (CST) Received: from tj10379pcu.spreadtrum.com (10.5.32.15) by BJMBX01.spreadtrum.com (10.0.64.7) with Microsoft SMTP Server (TLS) id 15.0.1497.23; Wed, 6 Mar 2024 11:48:00 +0800 From: Xiuhong Wang To: , , , CC: , , , Subject: [PATCH V2 2/2] f2fs: compress: fix reserve_cblocks counting error when out of space Date: Wed, 6 Mar 2024 11:47:46 +0800 Message-ID: <20240306034746.3722986-2-xiuhong.wang@unisoc.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20240306034746.3722986-1-xiuhong.wang@unisoc.com> References: <20240306034746.3722986-1-xiuhong.wang@unisoc.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Content-Type: text/plain X-ClientProxiedBy: SHCAS03.spreadtrum.com (10.0.1.207) To BJMBX01.spreadtrum.com (10.0.64.7) X-MAIL:SHSQR01.spreadtrum.com 4263m2C3002928 When a file only needs one direct_node, performing the following operations will cause the file to be unrepairable: unisoc # ./f2fs_io compress test.apk unisoc #df -h | grep dm-48 /dev/block/dm-48 112G 112G 1.2M 100% /data unisoc # ./f2fs_io release_cblocks test.apk 924 unisoc # df -h | grep dm-48 /dev/block/dm-48 112G 112G 4.8M 100% /data unisoc # dd if=/dev/random of=file4 bs=1M count=3 3145728 bytes (3.0 M) copied, 0.025 s, 120 M/s unisoc # df -h | grep dm-48 /dev/block/dm-48 112G 112G 1.8M 100% /data unisoc # ./f2fs_io reserve_cblocks test.apk F2FS_IOC_RESERVE_COMPRESS_BLOCKS failed: No space left on device adb reboot unisoc # df -h | grep dm-48 /dev/block/dm-48 112G 112G 11M 100% /data unisoc # ./f2fs_io reserve_cblocks test.apk 0 This is because the file has only one direct_node. After returning to -ENOSPC, reserved_blocks += ret will not be executed. As a result, the reserved_blocks at this time is still 0, which is not the real number of reserved blocks. Therefore, fsck cannot be set to repair the file. After this patch, the fsck flag will be set to fix this problem. unisoc # df -h | grep dm-48 /dev/block/dm-48 112G 112G 1.8M 100% /data unisoc # ./f2fs_io reserve_cblocks test.apk F2FS_IOC_RESERVE_COMPRESS_BLOCKS failed: No space left on device adb reboot then fsck will be executed unisoc # df -h | grep dm-48 /dev/block/dm-48 112G 112G 11M 100% /data unisoc # ./f2fs_io reserve_cblocks test.apk 924 Fixes: c75488fb4d82 ("f2fs: introduce F2FS_IOC_RESERVE_COMPRESS_BLOCKS") Signed-off-by: Xiuhong Wang Signed-off-by: Zhiguo Niu --- fs/f2fs/file.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c index 74c5e48fce22..dc9c6bac678d 100644 --- a/fs/f2fs/file.c +++ b/fs/f2fs/file.c @@ -3624,10 +3624,10 @@ static int f2fs_release_compress_blocks(struct file *filp, unsigned long arg) return ret; } -static int reserve_compress_blocks(struct dnode_of_data *dn, pgoff_t count) +static int reserve_compress_blocks(struct dnode_of_data *dn, pgoff_t count, + unsigned int *reserved_blocks) { struct f2fs_sb_info *sbi = F2FS_I_SB(dn->inode); - unsigned int reserved_blocks = 0; int cluster_size = F2FS_I(dn->inode)->i_cluster_size; block_t blkaddr; int i; @@ -3691,12 +3691,12 @@ static int reserve_compress_blocks(struct dnode_of_data *dn, pgoff_t count) f2fs_i_compr_blocks_update(dn->inode, compr_blocks, true); - reserved_blocks += reserved; + *reserved_blocks += reserved; next: count -= cluster_size; } - return reserved_blocks; + return 0; } static int f2fs_reserve_compress_blocks(struct file *filp, unsigned long arg) @@ -3757,7 +3757,7 @@ static int f2fs_reserve_compress_blocks(struct file *filp, unsigned long arg) count = min(end_offset - dn.ofs_in_node, last_idx - page_idx); count = round_up(count, F2FS_I(inode)->i_cluster_size); - ret = reserve_compress_blocks(&dn, count); + ret = reserve_compress_blocks(&dn, count, &reserved_blocks); f2fs_put_dnode(&dn); @@ -3765,13 +3765,12 @@ static int f2fs_reserve_compress_blocks(struct file *filp, unsigned long arg) break; page_idx += count; - reserved_blocks += ret; } filemap_invalidate_unlock(inode->i_mapping); f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]); - if (ret >= 0) { + if (!ret) { clear_inode_flag(inode, FI_COMPRESS_RELEASED); inode_set_ctime_current(inode); f2fs_mark_inode_dirty_sync(inode, true); @@ -3780,7 +3779,7 @@ static int f2fs_reserve_compress_blocks(struct file *filp, unsigned long arg) inode_unlock(inode); mnt_drop_write_file(filp); - if (ret >= 0) { + if (!ret) { ret = put_user(reserved_blocks, (u64 __user *)arg); } else if (reserved_blocks && atomic_read(&F2FS_I(inode)->i_compr_blocks)) { -- 2.25.1