mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Showta Ishizaki <zakinko@snowrabbit.org>
To: Konstantin Komarov <almaz.alexandrovich@paragon-software.com>
Cc: ntfs3@lists.linux.dev, linux-fsdevel@vger.kernel.org,
	linux-kernel@vger.kernel.org,
	Showta Ishizaki <zakinko@snowrabbit.org>
Subject: [PATCH] fs/ntfs3: drop the mapping pairs when a file loses all its clusters
Date: Wed, 25 Nov 2026 23:18:20 +0900	[thread overview]
Message-ID: <20261125-ntfs3-03-v1@snowrabbit.org> (raw)

When a non-resident attribute is truncated to nothing,
attr_set_size_ex() sets evcn to -1 and leaves the old mapping pairs in
the record.  chkdsk on Windows reports such a record as corrupt:

  Attribute record (80, "") from file record segment AD is corrupt.

Leave only the zero byte that ends the mapping pairs, and shrink the
attribute to it.  mi_pack_runs() cannot be used for this, it returns
-ENOSPC when there is nothing to pack.

I tried it on two volumes, one where files were written and some of them
truncated to nothing, and one after fsstress (-n 4000 -p 4).  Without
the patch all 16 empty files of the first and 3 of the 90 of the second
still had mapping pairs, and chkdsk exited 3 on both.  With it there
were none, and chkdsk exited 0 on both.  File contents were the same
after a remount.

Fixes: be71b5cba2e6 ("fs/ntfs3: Add attrib operations")
Signed-off-by: Showta Ishizaki <zakinko@snowrabbit.org>
---
 fs/ntfs3/attrib.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/fs/ntfs3/attrib.c b/fs/ntfs3/attrib.c
index d0f8c48..fbda12b 100644
--- a/fs/ntfs3/attrib.c
+++ b/fs/ntfs3/attrib.c
@@ -818,6 +818,21 @@ pack_runs:
 
 			le = (struct ATTR_LIST_ENTRY *)((u8 *)le - le_sz);
 		} else {
+			u32 asize = le32_to_cpu(attr->size);
+			u32 roff = le16_to_cpu(attr->nres.run_off);
+			u32 nsize = ALIGN(roff + 1, 8);
+
+			/*
+			 * Every cluster goes.  Setting evcn alone left the old
+			 * mapping pairs behind it, and chkdsk on Windows
+			 * reports such an attribute record as corrupt.  Leave
+			 * only the zero byte which ends the mapping pairs.
+			 * mi_pack_runs() does not take an empty range.
+			 */
+			if (nsize <= asize) {
+				memset(Add2Ptr(attr, roff), 0, nsize - roff);
+				mi_resize_attr(mi, attr, (int)nsize - (int)asize);
+			}
 			attr->nres.evcn = cpu_to_le64((u64)vcn - 1);
 			mi->dirty = true;
 		}
-- 
2.54.0 (Apple Git-157)


                 reply	other threads:[~2026-11-25 14:18 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261125-ntfs3-03-v1@snowrabbit.org \
    --to=zakinko@snowrabbit.org \
    --cc=almaz.alexandrovich@paragon-software.com \
    --cc=linux-fsdevel@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=ntfs3@lists.linux.dev \
    /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®