From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796123335; b=vrO6EV5TiXMWA1YEk++5DsJGVck41eyov/J9bqOl8li9tHnv3iqz1bl+PBYoScCoyKwJ I7kCPYn7YsJM1TOnp1xC5B+g3caxEvvfH0695JxSyvb89vXMcKGOaTlMiLvUxSB5OmCAp PAzuENK0hYd886mxvQwFcphze+ri72TVqJqVSDwtYnC0i/ZgisFhm4WwcFp12/cBuoZ6f GLWIsn3gtiXKsiQA1/ev/afTrGx+M2H7yohSPJI4E9xyu5d5xKwEhPwyovpdDdSpH/Ha7 C6PUQztWUhn7N6hobiqo+mXAMsuYQSicJ0D7tARvxQJFUtDqaAirgy3sZbv5mo4J9Og== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796123335; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=TLBrFh9Z6gb4mx9C6gKYjUEGCBWLv8io12Gj7RHCZ3c=; b=AxSZpBIgMLvX+2WrO5k7kIsF0V/8yrP1Kt/9yNJ7yWp+SfJF5ra22JiLJBAn7M5weWiD A+JXvhJGZj/lQ6tauiTPiY8s7KHiLfM8Duu//ljpAlupXpmAzKN4ymzg+as32kGEML6HY UDq9oynGqzuIB4BEZcVZuKzAL/upMnWKIvo9ascHfHhiy161WcGNtLhnsCFikgRuj/B2B Ow1VRcHZOo4YTKatGPQtZcCxl9MqS8UA18Wm6s3xIr5ZUcy9hgCkyvnT03WK/yVwKzoLa ij2IOZ/DP0yyhaoQfPxXkqOl7vwX9tn8n7c1rMTPGJ2rYXkO247hoEMtkzAfHQVee8Q== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=RzjNpboA; arc=none smtp.remote-ip=100.103.45.18 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (prime256v1)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 1529B606A320; Tue, 1 Dec 2026 11:08:54 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=RzjNpboA Received: by smtp.kernel.org (Postfix) with ESMTPSA id A0CF21F00893; Tue, 1 Dec 2026 11:08:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796123334; bh=TLBrFh9Z6gb4mx9C6gKYjUEGCBWLv8io12Gj7RHCZ3c=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=RzjNpboAMVmWRvf5s7z0PYEX9dM3rG7VhoED5UQFl+za2BvZLJp5Plfw39uM4vnid ASsrk3eM6e0MZq94M0vHOSffIqZaIWHVFMavACLyEm9wqFwmZTUEn44SmwwBR/vcAz ndLnp5N433Wro0H2NrZohfsggxcoPJt3NVjb7lmkFVIalcn1tG88e0C6mCE8MvE4lT nHg4HUUFuTtEu1i8mNflXe+1o88SvL7rms+0urTE2mPV4wWld8WaOOmdTgapHSmwe/ wQrXmwKiXlWTHdgYkKs8x6xmRaeRGisDv9F3DUoeumF4w3HsBsVpYEZim7wYMpsf2N pPWCem0kYazcg== From: Chao Yu To: jaegeuk@kernel.org Cc: linux-f2fs-devel@lists.sourceforge.net, linux-kernel@vger.kernel.org, Chao Yu , stable@vger.kernel.org, Daeho Jeong Subject: [PATCH 4/6] f2fs: atomic: fix to keep committed data of atomic file in f2fs_write_failed() Date: Tue, 1 Dec 2026 11:08:46 +0000 Message-ID: <20261201110842.1825797-10-chao@kernel.org> X-Mailer: git-send-email 2.56.1.839.g22a8290db9-goog In-Reply-To: <20261201110842.1825797-7-chao@kernel.org> References: <20261201110842.1825797-7-chao@kernel.org> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Chao Yu With F2FS_IOC_START_ATOMIC_REPLACE, i_size of the original inode is set to zero, and the old i_size is saved in fi->original_i_size. If a later write past i_size fails in f2fs_write_begin(), f2fs_write_failed() will truncate blocks of the original inode beyond i_size, i.e. committed data: f2fs_ioc_start_atomic_write(truncate=true) original_i_size = 512KB, i_size = 0 pwrite(0, 100000) i_size = 100000, blocks are reserved in COW inode pwrite(300000, 4096) f2fs_write_begin f2fs_filemap_get_folio -ENOMEM f2fs_write_failed(inode, 304096) f2fs_truncate_blocks(inode, 100000) frees committed blocks 25..127 f2fs_ioc_abort_atomic_write i_size = original_i_size (512KB) Then, aborting the atomic write, or a crash before commit, leaves holes in the original file, or loses the whole file if the failing write comes first, while fsck.f2fs reports nothing. Reproducer (4KB blocks): write 512KB to a file, fsync ioctl(fd, F2FS_IOC_START_ATOMIC_REPLACE) pwrite(fd, buf, 100000, 0) pwrite(fd, buf, 4096, 300000) -> -ENOMEM w/ FAULT_PAGE_GET injected ioctl(fd, F2FS_IOC_ABORT_ATOMIC_WRITE) Before: 103 of 128 blocks are zeroed, and freed on disk After: original data is intact Fix it by never truncating an atomic file below original_i_size in f2fs_write_failed(), so that only blocks preallocated w/ FALLOC_FL_KEEP_SIZE beyond it are freed, as for a regular file. Also round it up to the block size, otherwise truncate_partial_data_block() zeroes and dirties the partial block via the original inode, while the folio may hold uncommitted data and have lost its atomic flag after it was written back to the COW inode, then the uncommitted data would be written to the original inode. For non-replace atomic write, i_size never goes below original_i_size, so only the round up applies. Regular files are not affected. Fixes: 41e8f85a75fc ("f2fs: introduce F2FS_IOC_START_ATOMIC_REPLACE") Cc: stable@vger.kernel.org Cc: Daeho Jeong Signed-off-by: Chao Yu --- fs/f2fs/data.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c index aecedd3a4298..5f6ad38c0de1 100644 --- a/fs/f2fs/data.c +++ b/fs/f2fs/data.c @@ -5168,6 +5168,17 @@ void f2fs_write_failed(struct inode *inode, loff_t to) filemap_invalidate_lock(inode->i_mapping); truncate_pagecache(inode, i_size); + /* + * The original inode of an atomic file holds committed data + * below original_i_size, don't truncate it, as i_size can be + * smaller in atomic replace mode. Round it up as well, so the + * partial block, which may hold uncommitted data, won't be + * dirtied via the original inode. + */ + if (f2fs_is_atomic_file(inode)) + i_size = round_up(max_t(loff_t, i_size, + F2FS_I(inode)->original_i_size), + F2FS_BLKSIZE(F2FS_I_SB(inode))); f2fs_truncate_blocks(inode, i_size, true); filemap_invalidate_unlock(inode->i_mapping); -- 2.49.0 From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from lists.sourceforge.net (lists.sourceforge.net [216.105.38.7]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8E922606A329 for ; Tue, 1 Dec 2026 11:09:18 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=lists.sourceforge.net header.i=@lists.sourceforge.net header.a=rsa-sha256 header.s=beta header.b=GXo6dtFM; dkim=fail reason="signature verification failed" (1024-bit key) header.d=sourceforge.net header.i=@sourceforge.net header.a=rsa-sha256 header.s=x header.b=hnQuYYQp; dkim=fail reason="signature verification failed" (1024-bit key) header.d=sf.net header.i=@sf.net header.a=rsa-sha256 header.s=x header.b=NVSG7nv7; dkim=neutral header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=RzjNpboA DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=lists.sourceforge.net; s=beta; h=Content-Transfer-Encoding:Content-Type:Cc: Reply-To:From:List-Subscribe:List-Help:List-Post:List-Archive: List-Unsubscribe:List-Id:Subject:MIME-Version:References:In-Reply-To: Message-ID:Date:To:Sender:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID: List-Unsubscribe-Post:List-Owner; bh=2Wk5XwhtAVXLqGYeANwUFaNdaiQWLAOWyFed6Fd3KFA=; b=GXo6dtFMZ7sElmE9wMOwlKrTUx H60/66fW6Sl3saOnUjhP6uJkmzteQ7+keyhK1taCGkCGhQBBjcuQWrtzlsKeqmojvjDoVa6GhTciR Keg3REpmfal8nH2rxvbijVlFwKsJckZ2Hb+NOUYbr1A/vMmhsxmceLm03SZtGSea3j3Y=; Received: from [127.0.0.1] (helo=sfs-ml-4.v29.lw.sourceforge.com) by sfs-ml-4.v29.lw.sourceforge.com with esmtp (Exim 4.100.1) (envelope-from ) id 1xYLja-000000005co-1Hrw; Tue, 01 Dec 2026 11:09:18 +0000 Received: from [172.30.29.66] (helo=mx.sourceforge.net) by sfs-ml-4.v29.lw.sourceforge.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.100.1) (envelope-from ) id 1xYLjN-000000005cK-39L8 for linux-f2fs-devel@lists.sourceforge.net; Tue, 01 Dec 2026 11:09:05 +0000 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sourceforge.net; s=x; h=Content-Transfer-Encoding:MIME-Version:References: In-Reply-To:Message-ID:Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Unsubscribe-Post:List-Subscribe:List-Post:List-Owner:List-Archive; bh=TLBrFh9Z6gb4mx9C6gKYjUEGCBWLv8io12Gj7RHCZ3c=; b=hnQuYYQpeqi49tQHBNE+4olqOy ITvMjGYpK61x1fBkwIGebl9wQZG929ZewKuCfz0qqDlien+0N/V8zlr8aSBZkpZO9VpLxxMzOyDH/ 1ZFqrTFUWPQ/kAnvUTbSAmn9sYk/+lr4c9i4TAKps6k5Zl171J0WKj+5b9ESMnqyL0Lk=; DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=sf.net; s=x ; h=Content-Transfer-Encoding:MIME-Version:References:In-Reply-To:Message-ID: Date:Subject:Cc:To:From:Sender:Reply-To:Content-Type:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:List-Id:List-Help:List-Unsubscribe:List-Unsubscribe-Post: List-Subscribe:List-Post:List-Owner:List-Archive; bh=TLBrFh9Z6gb4mx9C6gKYjUEGCBWLv8io12Gj7RHCZ3c=; b=NVSG7nv7fl1ogYaggExdvP2nHL z0ANYMFMjV0ezS2vGktX8RNiPEWLvWCAk1iyZVQ5Qz19vqCdw3CyP685N8gZ04uDtd9XWNyPs+6+T Vwr0YYCJtUBLwzRXO4RBDaE1/fxp+dhN0bojOVvVrOPW5HLZpP+/2Lsp1us0LOvRJx0A=; Received: from tor.source.kernel.org ([172.105.4.254]) by sfi-mx-1.v28.lw.sourceforge.com with esmtps (TLS1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.100.1) id 1xYLjI-000000001Jc-1bru for linux-f2fs-devel@lists.sourceforge.net; Tue, 01 Dec 2026 11:09:05 +0000 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by tor.source.kernel.org (Postfix) with ESMTP id CC7D362493; Tue, 1 Dec 2026 11:08:54 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id A0CF21F00893; Tue, 1 Dec 2026 11:08:53 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796123334; bh=TLBrFh9Z6gb4mx9C6gKYjUEGCBWLv8io12Gj7RHCZ3c=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=RzjNpboAMVmWRvf5s7z0PYEX9dM3rG7VhoED5UQFl+za2BvZLJp5Plfw39uM4vnid ASsrk3eM6e0MZq94M0vHOSffIqZaIWHVFMavACLyEm9wqFwmZTUEn44SmwwBR/vcAz ndLnp5N433Wro0H2NrZohfsggxcoPJt3NVjb7lmkFVIalcn1tG88e0C6mCE8MvE4lT nHg4HUUFuTtEu1i8mNflXe+1o88SvL7rms+0urTE2mPV4wWld8WaOOmdTgapHSmwe/ wQrXmwKiXlWTHdgYkKs8x6xmRaeRGisDv9F3DUoeumF4w3HsBsVpYEZim7wYMpsf2N pPWCem0kYazcg== To: jaegeuk@kernel.org Date: Tue, 1 Dec 2026 11:08:46 +0000 Message-ID: <20261201110842.1825797-10-chao@kernel.org> X-Mailer: git-send-email 2.56.1.839.g22a8290db9-goog In-Reply-To: <20261201110842.1825797-7-chao@kernel.org> References: <20261201110842.1825797-7-chao@kernel.org> X-Archived-By: MIME-Version: 1.0 X-Spam-Score: -0.2 (/) X-Headers-End: 1xYLjI-000000001Jc-1bru Subject: [f2fs-dev] [PATCH 4/6] f2fs: atomic: fix to keep committed data of atomic file in f2fs_write_failed() X-BeenThere: linux-f2fs-devel@lists.sourceforge.net X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Archive: List-Post: List-Help: List-Subscribe: , From: Chao Yu via Linux-f2fs-devel Reply-To: Chao Yu Cc: Daeho Jeong , linux-kernel@vger.kernel.org, stable@vger.kernel.org, linux-f2fs-devel@lists.sourceforge.net Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-19.1 required=3.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,SPF_HELO_PASS,SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=4.0.1 X-Spam-Checker-Version: SpamAssassin 4.0.1 (2024-03-26) on aws-us-west-2-korg-listserv-alma10-1 From: Chao Yu With F2FS_IOC_START_ATOMIC_REPLACE, i_size of the original inode is set to zero, and the old i_size is saved in fi->original_i_size. If a later write past i_size fails in f2fs_write_begin(), f2fs_write_failed() will truncate blocks of the original inode beyond i_size, i.e. committed data: f2fs_ioc_start_atomic_write(truncate=true) original_i_size = 512KB, i_size = 0 pwrite(0, 100000) i_size = 100000, blocks are reserved in COW inode pwrite(300000, 4096) f2fs_write_begin f2fs_filemap_get_folio -ENOMEM f2fs_write_failed(inode, 304096) f2fs_truncate_blocks(inode, 100000) frees committed blocks 25..127 f2fs_ioc_abort_atomic_write i_size = original_i_size (512KB) Then, aborting the atomic write, or a crash before commit, leaves holes in the original file, or loses the whole file if the failing write comes first, while fsck.f2fs reports nothing. Reproducer (4KB blocks): write 512KB to a file, fsync ioctl(fd, F2FS_IOC_START_ATOMIC_REPLACE) pwrite(fd, buf, 100000, 0) pwrite(fd, buf, 4096, 300000) -> -ENOMEM w/ FAULT_PAGE_GET injected ioctl(fd, F2FS_IOC_ABORT_ATOMIC_WRITE) Before: 103 of 128 blocks are zeroed, and freed on disk After: original data is intact Fix it by never truncating an atomic file below original_i_size in f2fs_write_failed(), so that only blocks preallocated w/ FALLOC_FL_KEEP_SIZE beyond it are freed, as for a regular file. Also round it up to the block size, otherwise truncate_partial_data_block() zeroes and dirties the partial block via the original inode, while the folio may hold uncommitted data and have lost its atomic flag after it was written back to the COW inode, then the uncommitted data would be written to the original inode. For non-replace atomic write, i_size never goes below original_i_size, so only the round up applies. Regular files are not affected. Fixes: 41e8f85a75fc ("f2fs: introduce F2FS_IOC_START_ATOMIC_REPLACE") Cc: stable@vger.kernel.org Cc: Daeho Jeong Signed-off-by: Chao Yu --- fs/f2fs/data.c | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c index aecedd3a4298..5f6ad38c0de1 100644 --- a/fs/f2fs/data.c +++ b/fs/f2fs/data.c @@ -5168,6 +5168,17 @@ void f2fs_write_failed(struct inode *inode, loff_t to) filemap_invalidate_lock(inode->i_mapping); truncate_pagecache(inode, i_size); + /* + * The original inode of an atomic file holds committed data + * below original_i_size, don't truncate it, as i_size can be + * smaller in atomic replace mode. Round it up as well, so the + * partial block, which may hold uncommitted data, won't be + * dirtied via the original inode. + */ + if (f2fs_is_atomic_file(inode)) + i_size = round_up(max_t(loff_t, i_size, + F2FS_I(inode)->original_i_size), + F2FS_BLKSIZE(F2FS_I_SB(inode))); f2fs_truncate_blocks(inode, i_size, true); filemap_invalidate_unlock(inode->i_mapping); -- 2.49.0 _______________________________________________ Linux-f2fs-devel mailing list Linux-f2fs-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel