Skip to content

Keep the name from disk when preparing the first hard link - #132

Open
ozzycattt wants to merge 1 commit into
linux-apfs:masterfrom
ozzycattt:hardlink-keep-disk-name
Open

ozzycattt wants to merge 1 commit into
linux-apfs:masterfrom
ozzycattt:hardlink-keep-disk-name

Conversation

@ozzycattt

Copy link
Copy Markdown
Contributor

Fixes the apfsck warning from #130.

When a file gets its second link, apfs_prepare_dentry_for_link() deletes the dentry record and creates it again with the sibling records, using dentry->d_name. On a case-insensitive volume the lookup can find the record under a different case, so:

echo abc > a.bin
# remount, or anything else that drops the dentry
ln A.BIN c.lnk

renames the entry to A.BIN (that's what ls shows afterwards), while the inode keeps a.bin. apfsck then reports Inode record: wrong name for primary link.

The patch copies the name from the record before deleting it, and uses it for both the sibling records and the new dentry record. New links still get the name from their own dentry.

Tested on a Raspberry Pi 4 (kernel 6.18). With apfsck after each case, all clean:

  • the case above, and the same with Polish letters (żaba.bin / ŻABA.BIN): ls keeps the original name, and both names read the same data,
  • a link through the same name after a remount,
  • a link with a different case without a remount,
  • three links, then removing the original,
  • a link into another directory after a remount,
  • a third link through a different case, when the file already has sibling records.

I also ran one 7-minute round of my stress test together with #131 (about 15k operations, same seed as the failing run): 0 mismatches out of 3179 files, apfsck clean, fsck_apfs -n -W clean.

When a file gets its second link, apfs_prepare_dentry_for_link() deletes
the dentry record and creates it again together with the sibling records.
It uses dentry->d_name for both, but on case-insensitive volumes the lookup
may have found the record under a different case, while the inode keeps
its original name:

  echo abc > a.bin
  (remount, or anything else that drops the dentry)
  ln A.BIN c.lnk

After this, the directory lists "A.BIN" instead of "a.bin", and apfsck
reports "Inode record: wrong name for primary link."

Copy the name from the record before deleting it, and use that instead.
New links still get the name from their own dentry.

Reported in linux-apfs#130.

Signed-off-by: ozzycattt <ozzycattt@users.noreply.github.com>
@eafer

eafer commented Oct 16, 2026 •

Copy link
Copy Markdown
Member

Thanks again. Renaming files like this is definitely wrong, independently of the mismatch with the primary name.

By the way, are these patches generated by an LLM? I ask because I'm not sure if I want to end up with this much LLM code in the driver, it might be better if I write a solution myself from your bug report.

@ozzycattt

Copy link
Copy Markdown
Contributor Author

Yes, I wrote them with an LLM. It wasn't fire-and-forget, though. The bugs came out of my own stress tests on real hardware. Each fix was checked against a reproducer on your unpatched driver, with apfsck and fsck_apfs, and with the same stress round before and after. I only sent what held up. The patch is small, and I'm happy to change anything in it.

That said, it's your driver, so if you'd rather write the fix yourself from the report, that's completely fine with me. The reproducer is in the PR description.

@eafer

eafer commented Oct 16, 2026

Copy link
Copy Markdown
Member

I wasn't questioning the quality of the work, you've helped a lot so far. I'm just not confident about the copyright status of LLM code and I don't want to risk tainting the driver somehow. I'll come up with a patch myself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants