mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Tal Zussman <tz2294@columbia.edu>
To: Tyler Hicks <code@tyhicks.com>, Erez Zadok <ezk@cs.sunysb.edu>,
	Phillip Hellewell <phillip@hellewell.homeip.net>,
	Badari Pulavarty <pbadari@us.ibm.com>,
	Theodore Ts'o <tytso@mit.edu>,
	Andrew Morton <akpm@linux-foundation.org>
Cc: ecryptfs@vger.kernel.org, linux-kernel@vger.kernel.org,
	"Matthew Wilcox (Oracle)" <willy@infradead.org>,
	Stephan Mueller <smueller@chronox.de>,
	Adrian Bunk <bunk@stusta.de>,
	Michael Halcrow <mhalcrow@us.ibm.com>,
	Randy Dunlap <rdunlap@infradead.org>,
	Tal Zussman <tz2294@columbia.edu>
Subject: [PATCH v3 08/10] ecryptfs: zero the tail of the last folio in place on truncate
Date: Mon, 16 Nov 2026 17:08:14 -0500	[thread overview]
Message-ID: <20261116-ecryptfs-folio-v3-8-1aba8e620eda@columbia.edu> (raw)
In-Reply-To: <20261116-ecryptfs-folio-v3-0-1aba8e620eda@columbia.edu>

A shrinking truncate zeroes the rest of the folio that holds the new
end of the file by copying from the zero page through ecryptfs_write().
Zero the folio in place with folio_zero_range() instead, the way the
truncate helpers in other filesystems do, and encrypt it to the lower
file. This no longer bumps i_size to the end of the folio and writes
that to the metadata before the real size is written. It also removes
the last struct page usage in ecryptfs.

The folio stays locked while it is encrypted, as in
ecryptfs_writepages(), and is marked not uptodate if that fails, so a
later read goes back to the lower file instead of returning zeroes that
were never written out.

A page-aligned new size has no tail to zero, so skip it. The old code
wrote out a full page of zeroes past the new end of the file in that
case, which the truncate then discarded.

Signed-off-by: Tal Zussman <tz2294@columbia.edu>
---
 fs/ecryptfs/inode.c | 55 ++++++++++++++++++++++++++++++++++++++++-------------
 1 file changed, 42 insertions(+), 13 deletions(-)

diff --git a/fs/ecryptfs/inode.c b/fs/ecryptfs/inode.c
index 525297c7ebd8..ffab685688ab 100644
--- a/fs/ecryptfs/inode.c
+++ b/fs/ecryptfs/inode.c
@@ -718,6 +718,40 @@ upper_size_to_lower_size(struct ecryptfs_crypt_stat *crypt_stat,
 	return lower_size;
 }
 
+/**
+ * ecryptfs_zero_tail
+ * @inode: The ecryptfs inode
+ * @pos: The file offset from which to zero
+ *
+ * Zero the folio that holds @pos from @pos to its end, and write the
+ * result out to the lower file.
+ *
+ * Returns zero on success; non-zero otherwise
+ */
+static int ecryptfs_zero_tail(struct inode *inode, loff_t pos)
+{
+	struct folio *folio;
+	size_t from;
+	int rc;
+
+	/* A page-aligned @pos has no tail to zero. */
+	if (!offset_in_page(pos))
+		return 0;
+
+	folio = read_mapping_folio(inode->i_mapping, pos >> PAGE_SHIFT, NULL);
+	if (IS_ERR(folio))
+		return PTR_ERR(folio);
+	from = offset_in_folio(folio, pos);
+	folio_lock(folio);
+	folio_zero_range(folio, from, folio_size(folio) - from);
+	rc = ecryptfs_encrypt_page(folio);
+	if (rc)
+		folio_clear_uptodate(folio);
+	folio_unlock(folio);
+	folio_put(folio);
+	return rc;
+}
+
 /**
  * __ecryptfs_truncate
  * @dentry: The ecryptfs layer dentry
@@ -739,7 +773,6 @@ static int __ecryptfs_truncate(struct dentry *dentry, const struct iattr *ia)
 	loff_t lower_size_before_truncate;
 	loff_t lower_size_after_truncate;
 	struct iattr lower_ia;
-	size_t num_zeros;
 	int rc;
 
 	ecryptfs_iattr_to_lower(&lower_ia, ia);
@@ -788,19 +821,15 @@ static int __ecryptfs_truncate(struct dentry *dentry, const struct iattr *ia)
 	}
 
 	/*
-	 * We're chopping off all the pages down to the page in which
-	 * ia->ia_size is located. Fill in the end of that page from
-	 * (ia->ia_size & ~PAGE_MASK) to PAGE_SIZE with zeros.
+	 * We're chopping off all the folios down to the folio in which
+	 * ia->ia_size is located. Fill in the end of that folio from
+	 * ia->ia_size with zeros.
 	 */
-	num_zeros = PAGE_SIZE - (ia->ia_size & ~PAGE_MASK);
-	if (num_zeros) {
-		rc = ecryptfs_write(inode, page_address(ZERO_PAGE(0)),
-				ia->ia_size, num_zeros);
-		if (rc) {
-			pr_err("Error attempting to zero out the remainder of the end page on reducing truncate; rc = [%d]\n",
-				rc);
-			goto out;
-		}
+	rc = ecryptfs_zero_tail(inode, ia->ia_size);
+	if (rc) {
+		pr_err("Error attempting to zero out the remainder of the end folio on reducing truncate; rc = [%d]\n",
+			rc);
+		goto out;
 	}
 	truncate_setsize(inode, ia->ia_size);
 	rc = ecryptfs_write_inode_size_to_metadata(inode);

-- 
2.39.5


  parent reply	other threads:[~2026-11-16 22:08 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-16 22:08 [PATCH v3 00/10] ecryptfs: remove the remaining struct page usage Tal Zussman
2026-11-16 22:08 ` [PATCH v3 01/10] ecryptfs: write from the right offset in ecryptfs_write_lower_page_segment() Tal Zussman
2026-11-16 22:08 ` [PATCH v3 02/10] ecryptfs: reject zero-length encrypted session keys Tal Zussman
2026-11-16 22:08 ` [PATCH v3 03/10] ecryptfs: keep the folio locked while ecryptfs_write() writes it out Tal Zussman
2026-11-16 22:08 ` [PATCH v3 04/10] ecryptfs: remove struct ecryptfs_page_crypt_context Tal Zussman
2026-11-16 22:08 ` [PATCH v3 05/10] ecryptfs: use a folio and a buffer in crypt_extent() Tal Zussman
2026-11-16 22:08 ` [PATCH v3 06/10] ecryptfs: use kzalloc() for the header buffer Tal Zussman
2026-11-16 22:08 ` [PATCH v3 07/10] ecryptfs: use sg_init_one() instead of virt_to_scatterlist() Tal Zussman
2026-11-16 22:08 ` Tal Zussman [this message]
2026-11-16 22:08 ` [PATCH v3 09/10] ecryptfs: rename functions to refer to folios Tal Zussman
2026-11-16 22:08 ` [PATCH v3 10/10] ecryptfs: refer to folios in comments and printk strings Tal Zussman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261116-ecryptfs-folio-v3-8-1aba8e620eda@columbia.edu \
    --to=tz2294@columbia.edu \
    --cc=akpm@linux-foundation.org \
    --cc=bunk@stusta.de \
    --cc=code@tyhicks.com \
    --cc=ecryptfs@vger.kernel.org \
    --cc=ezk@cs.sunysb.edu \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mhalcrow@us.ibm.com \
    --cc=pbadari@us.ibm.com \
    --cc=phillip@hellewell.homeip.net \
    --cc=rdunlap@infradead.org \
    --cc=smueller@chronox.de \
    --cc=tytso@mit.edu \
    --cc=willy@infradead.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®