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