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=1796123338; b=qv/nasB3SqRw7Y+KCNGYZ7F1n42PzA37tBNHdU5bGpYNE7cqvEXhidKq95Ecg6cy5uBM V5xvzQinFC1WwSQ8ixQvWULfBBfsq3k9JxL3WFMXPks2YP6euKMMh3imZ7i7Ba27mj9A5 oYI3xgEoMdQsw0nsMMD5zv17bKDXxm74oRHFWZC10X2i+ukT9Viiw6yaloK3rJQ1JyJRr W3liyaYriQyIfQsoOdnBlSQt9uwsY2lRKxcup0s8ChVMTjW4IvofGwARKaCQITyDTaPYR 1rfKXK1+/maXa/3ybeFy2skRylCOoUmTMPRRgMflskl1yojE+dn7LXSEo1DUk4F6xDw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796123338; h=Received:DKIM-Signature:From:To:Cc:Subject:Date:Message-ID:X-Mailer: In-Reply-To:References:MIME-Version:Content-Transfer-Encoding; bh=STTVNH8j8GVHQ6CK4Up0uefpVa3ap/twHcgKtODMDug=; b=MLiD9qfothkFYtk2rklEGTfEsJMxqq5r1ku4okVh+oyx6kqwdm7jwQPAdms/HDUqe68U jd4XFxV9yW2Nq6wydHFOFHwpRZHeynXE0xq7dV9CPQD51Wu/uJhYHJEMhvg6P48Wm27Rs 6cdkS6HLrJIFVFEcq5EnZuKIgzWP7o7gmNS6rXKOGHrlqZtQBD23KmQyTVgwzb2QQBNeL V/28wDYDPr3QxuPB3apYE4jlzY3eeB064gc2n0Ekln31iH3eYxCCko+DlKyL7GIyn7hAT OxDg2K6E/ChMGTFghtE70AJvkbkvkSLBwG/iDQgSt53qHYDE+ouQYtV7NAcHNV3OoAw== 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=YrrHOHzX; 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 9C1B3606A684; Tue, 1 Dec 2026 11:08:57 +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=YrrHOHzX Received: by smtp.kernel.org (Postfix) with ESMTPSA id 57D911F0089C; Tue, 1 Dec 2026 11:08:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796123337; bh=STTVNH8j8GVHQ6CK4Up0uefpVa3ap/twHcgKtODMDug=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=YrrHOHzX5fBQwqgESrtxAUAXvhjqZPq8exbOt98V9mtEC/4zdjM6nKQmoqO2jOfV/ ljH4cCxe4KxX3Qmh+eyp9PeidZBWpS8krx2iBsUTpaUm4tKOpxq9bk0PuLOMS6oy+b HCZbDMl6trA7T2zz5ll5WqIFzBhpavSU3Pl84aNm46V3x7GzPbIaHEpIwqqzamn6sN v1WdsDUAGa4CAx8IC6XeRu465/dafzmLMCdF6/rJ9lVAN/lMEMTHFJ+zS+gYpm2gp2 YAlcUupbnE9ChEEYxgz2EXW26giyoC1bGyXV7RYYLb4Fi4JYkXYx4DA6L09eo/OU0V Ql2fPNNIaASrA== 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 6/6] f2fs: atomic: fix to disallow shrinking atomic file via truncate Date: Tue, 1 Dec 2026 11:08:48 +0000 Message-ID: <20261201110842.1825797-12-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: stable@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 In f2fs_setattr(), truncating an atomic file to a smaller or the same size calls f2fs_truncate() on the original inode, which can't be rolled back by aborting the atomic write: 1. It zeroes and dirties the partial last 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, and it frees committed blocks beyond the new size: write 10000 bytes of 0x41, fsync committed data ioctl(F2FS_IOC_START_ATOMIC_WRITE) pwrite(904 bytes of 0x55, 4096) uncommitted data sync_file_range(0, 16384, WAIT_BEFORE | WRITE | WAIT_AFTER) ftruncate(5000) sync_file_range(0, 16384, WAIT_BEFORE | WRITE | WAIT_AFTER) ioctl(F2FS_IOC_ABORT_ATOMIC_WRITE) After the abort, the original file has 904 bytes of aborted 0x55, and one 4KB block of zeroes, w/ 4KB or 16KB blocks. 2. In atomic replace mode, i_size only covers the new data, so even truncating to the current i_size frees committed data: write 512KB, fsync committed blocks 0..127 ioctl(F2FS_IOC_START_ATOMIC_REPLACE) i_size = 0 pwrite(0, 100000) written to COW inode ftruncate(100000) frees committed blocks 25..127 ioctl(F2FS_IOC_ABORT_ATOMIC_WRITE) After the abort, 103 of 128 blocks of the original file are zeroes, w/ 4KB blocks. SQLite, the major user of atomic write, never shrinks the database file in an atomic write, it only shrinks it after commit. So let's return -EOPNOTSUPP for shrinking an atomic file, as commit 06dfe4a7eb69 ("f2fs: disallow mmap write and data-modifying fallocate on atomic files") does for FALLOC_FL_PUNCH_HOLE and FALLOC_FL_COLLAPSE_RANGE, and skip f2fs_truncate() when truncating it to its current size. Extending an atomic file is still allowed. Then ftruncate() returns -EOPNOTSUPP in 1., and the original data is intact in both cases. Fixes: 3db1de0e582c ("f2fs: change the current atomic write way") Fixes: 41e8f85a75fc ("f2fs: introduce F2FS_IOC_START_ATOMIC_REPLACE") Cc: stable@vger.kernel.org Cc: Daeho Jeong Signed-off-by: Chao Yu --- fs/f2fs/file.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c index 5ad772a41f9b..fcf326e3d19b 100644 --- a/fs/f2fs/file.c +++ b/fs/f2fs/file.c @@ -1279,6 +1279,14 @@ int f2fs_setattr(const struct mnt_idmap *idmap, struct dentry *dentry, F2FS_BLK_TO_BYTES(sbi, CAP_BLKS_PER_SEC(sbi)))) return -EINVAL; } + + /* + * Shrinking an atomic file would change the original inode, + * which can't be rolled back by abort, as for fallocate(). + */ + if (f2fs_is_atomic_file(inode) && + attr->ia_size < i_size_read(inode)) + return -EOPNOTSUPP; } if (is_quota_modification(idmap, inode, attr)) { @@ -1338,7 +1346,12 @@ int f2fs_setattr(const struct mnt_idmap *idmap, struct dentry *dentry, } truncate_setsize(inode, attr->ia_size); - if (attr->ia_size <= old_size) + /* + * Nothing to truncate for an atomic file w/ the same size, and + * f2fs_truncate() would change the original inode. + */ + if (attr->ia_size < old_size || + (attr->ia_size == old_size && !f2fs_is_atomic_file(inode))) err = f2fs_truncate(inode); /* * do not trim all blocks after i_size if target size is -- 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 35697606A329 for ; Tue, 1 Dec 2026 11:09:19 +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=eqj8qyF2; 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=GSaykwkO; 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=iyxBUH2w; dkim=neutral header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=YrrHOHzX 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=GBQ88fVtfmgsBrgmbNznyCsU6KkBwUxRQT6JmsaiTb0=; b=eqj8qyF2j45PHhP+A0pvfSkYtN r8ImYSoedO596lKYXMLGXSUBRufXAwKeNb9pQWGUnI+0TUYLaee/bCYWEOCAorIydMHNSadR6G318 azJwVxxrLx5Dm3fnEJBTUCYbXiZIMBPDMOGwtf97raIMdpLWwZNFK3jNo5dfojWmGpSU=; Received: from [127.0.0.1] (helo=sfs-ml-3.v29.lw.sourceforge.com) by sfs-ml-3.v29.lw.sourceforge.com with esmtp (Exim 4.100.1) (envelope-from ) id 1xYLja-000000008SN-3wrY; Tue, 01 Dec 2026 11:09:18 +0000 Received: from [172.30.29.66] (helo=mx.sourceforge.net) by sfs-ml-3.v29.lw.sourceforge.com with esmtps (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.100.1) (envelope-from ) id 1xYLjM-000000008Rw-32et for linux-f2fs-devel@lists.sourceforge.net; Tue, 01 Dec 2026 11:09:04 +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=STTVNH8j8GVHQ6CK4Up0uefpVa3ap/twHcgKtODMDug=; b=GSaykwkOy+0W1SG5qu006rVNLm 6RDm9JZeMzen0wCd4top6kYZ6SF0I1Lu+zvt1IEJlCypIBN1rQuVJmo8N1u5uCa5dQtr3wxz3ucxX 2UyKF7ojZHNd7sdZaMNJ+RuUevHmXDpJT8eLhsRMImYHpcPH7VbvENN8aGkwaHBh+0Po=; 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=STTVNH8j8GVHQ6CK4Up0uefpVa3ap/twHcgKtODMDug=; b=iyxBUH2w/Ge/4lGvJliVSfyL5W gVXMHAHgi91QCxQfNz3Zi/hN1gVX81Ne8EBaJfQzLHfO0adkD6eM/SjzjCG3sAI/BLA8Trf4rvqaX wf56TFo9uJQqixeHcdPNG5NqoZMuYUKkkjZ2gEsF89kYyrvfdqFXzJs8AcMxM2MBIxCE=; Received: from tor.source.kernel.org ([172.105.4.254]) by sfi-mx-2.v28.lw.sourceforge.com with esmtps (TLS1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.100.1) id 1xYLjL-000000005YW-0NHl for linux-f2fs-devel@lists.sourceforge.net; Tue, 01 Dec 2026 11:09:04 +0000 Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18]) by tor.source.kernel.org (Postfix) with ESMTP id 8381362316; Tue, 1 Dec 2026 11:08:57 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 57D911F0089C; Tue, 1 Dec 2026 11:08:56 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org; s=k20260515; t=1796123337; bh=STTVNH8j8GVHQ6CK4Up0uefpVa3ap/twHcgKtODMDug=; h=From:To:Cc:Subject:Date:In-Reply-To:References; b=YrrHOHzX5fBQwqgESrtxAUAXvhjqZPq8exbOt98V9mtEC/4zdjM6nKQmoqO2jOfV/ ljH4cCxe4KxX3Qmh+eyp9PeidZBWpS8krx2iBsUTpaUm4tKOpxq9bk0PuLOMS6oy+b HCZbDMl6trA7T2zz5ll5WqIFzBhpavSU3Pl84aNm46V3x7GzPbIaHEpIwqqzamn6sN v1WdsDUAGa4CAx8IC6XeRu465/dafzmLMCdF6/rJ9lVAN/lMEMTHFJ+zS+gYpm2gp2 YAlcUupbnE9ChEEYxgz2EXW26giyoC1bGyXV7RYYLb4Fi4JYkXYx4DA6L09eo/OU0V Ql2fPNNIaASrA== To: jaegeuk@kernel.org Date: Tue, 1 Dec 2026 11:08:48 +0000 Message-ID: <20261201110842.1825797-12-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: 1xYLjL-000000005YW-0NHl Subject: [f2fs-dev] [PATCH 6/6] f2fs: atomic: fix to disallow shrinking atomic file via truncate 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 In f2fs_setattr(), truncating an atomic file to a smaller or the same size calls f2fs_truncate() on the original inode, which can't be rolled back by aborting the atomic write: 1. It zeroes and dirties the partial last 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, and it frees committed blocks beyond the new size: write 10000 bytes of 0x41, fsync committed data ioctl(F2FS_IOC_START_ATOMIC_WRITE) pwrite(904 bytes of 0x55, 4096) uncommitted data sync_file_range(0, 16384, WAIT_BEFORE | WRITE | WAIT_AFTER) ftruncate(5000) sync_file_range(0, 16384, WAIT_BEFORE | WRITE | WAIT_AFTER) ioctl(F2FS_IOC_ABORT_ATOMIC_WRITE) After the abort, the original file has 904 bytes of aborted 0x55, and one 4KB block of zeroes, w/ 4KB or 16KB blocks. 2. In atomic replace mode, i_size only covers the new data, so even truncating to the current i_size frees committed data: write 512KB, fsync committed blocks 0..127 ioctl(F2FS_IOC_START_ATOMIC_REPLACE) i_size = 0 pwrite(0, 100000) written to COW inode ftruncate(100000) frees committed blocks 25..127 ioctl(F2FS_IOC_ABORT_ATOMIC_WRITE) After the abort, 103 of 128 blocks of the original file are zeroes, w/ 4KB blocks. SQLite, the major user of atomic write, never shrinks the database file in an atomic write, it only shrinks it after commit. So let's return -EOPNOTSUPP for shrinking an atomic file, as commit 06dfe4a7eb69 ("f2fs: disallow mmap write and data-modifying fallocate on atomic files") does for FALLOC_FL_PUNCH_HOLE and FALLOC_FL_COLLAPSE_RANGE, and skip f2fs_truncate() when truncating it to its current size. Extending an atomic file is still allowed. Then ftruncate() returns -EOPNOTSUPP in 1., and the original data is intact in both cases. Fixes: 3db1de0e582c ("f2fs: change the current atomic write way") Fixes: 41e8f85a75fc ("f2fs: introduce F2FS_IOC_START_ATOMIC_REPLACE") Cc: stable@vger.kernel.org Cc: Daeho Jeong Signed-off-by: Chao Yu --- fs/f2fs/file.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c index 5ad772a41f9b..fcf326e3d19b 100644 --- a/fs/f2fs/file.c +++ b/fs/f2fs/file.c @@ -1279,6 +1279,14 @@ int f2fs_setattr(const struct mnt_idmap *idmap, struct dentry *dentry, F2FS_BLK_TO_BYTES(sbi, CAP_BLKS_PER_SEC(sbi)))) return -EINVAL; } + + /* + * Shrinking an atomic file would change the original inode, + * which can't be rolled back by abort, as for fallocate(). + */ + if (f2fs_is_atomic_file(inode) && + attr->ia_size < i_size_read(inode)) + return -EOPNOTSUPP; } if (is_quota_modification(idmap, inode, attr)) { @@ -1338,7 +1346,12 @@ int f2fs_setattr(const struct mnt_idmap *idmap, struct dentry *dentry, } truncate_setsize(inode, attr->ia_size); - if (attr->ia_size <= old_size) + /* + * Nothing to truncate for an atomic file w/ the same size, and + * f2fs_truncate() would change the original inode. + */ + if (attr->ia_size < old_size || + (attr->ia_size == old_size && !f2fs_is_atomic_file(inode))) err = f2fs_truncate(inode); /* * do not trim all blocks after i_size if target size is -- 2.49.0 _______________________________________________ Linux-f2fs-devel mailing list Linux-f2fs-devel@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel