From 78e2a4c2b456667003dc779652bc63ee4d0a2108 Mon Sep 17 00:00:00 2001 From: Yip Leo Date: Mon, 23 Nov 2026 02:17:25 +0800 Subject: [PATCH] mount: check the next inode number before AllocateInode returns it (#12884) AllocateInode probes from the number a path hashes to while that number is taken, but the loop re-checked the number it had just stepped off and then advanced once more. When the first candidate was taken, it returned the number after the first free one without looking at it, and that number can belong to a live file. The new entry stores the number in its attributes and the mount reports it as st_ino, so the two files stat as the same file. A writer that replaces a file with tmp + rename several times a second runs into it: the next temp file hashes to the number the previous one carried to the target, and GNU mv refuses the replacement with "are the same file". With readers holding the replaced file open, 56 of 3446 such replacements failed that way on our mounts; none did with this change. Lookup's probe already checks the next number; AllocateInode now does the same. Co-authored-by: Claude Opus 5.5 --- weed/mount/inode_to_path.go | 2 +- weed/mount/inode_to_path_test.go | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/weed/mount/inode_to_path.go b/weed/mount/inode_to_path.go index 74eb1fc568..a6c6e94969 100644 --- a/weed/mount/inode_to_path.go +++ b/weed/mount/inode_to_path.go @@ -263,7 +263,7 @@ func (i *InodeToPath) AllocateInode(path util.FullPath, unixTime int64) uint64 { defer i.Unlock() inode := path.AsInode(unixTime) for _, found := i.inode2path[inode]; found; inode++ { - _, found = i.inode2path[inode] + _, found = i.inode2path[inode+1] } return inode } diff --git a/weed/mount/inode_to_path_test.go b/weed/mount/inode_to_path_test.go index 5d6e3367af..818adcc1b7 100644 --- a/weed/mount/inode_to_path_test.go +++ b/weed/mount/inode_to_path_test.go @@ -245,3 +245,28 @@ func TestRemoveAliasKeepsSoleName(t *testing.T) { t.Fatal("removed the surviving name") } } + +// AllocateInode must return the first number no inode holds. It used to +// re-check the number it had just stepped off and return the next one +// unchecked, so a file created in the same second as a renamed one could store +// that file's inode number and stat as the same file (GNU mv refused with +// "are the same file"). +func TestAllocateInodeReturnsFirstFreeNumber(t *testing.T) { + itp := NewInodeToPath(util.FullPath("/"), 60) + tmp := util.FullPath("/run/active.json.tmp") + now := time.Now().Unix() + a := tmp.AsInode(now) + + // a and a+2 are held, a+1 is free + itp.Lookup("/run/x", now, false, false, a, true) + itp.Lookup("/run/y", now, false, false, a+2, true) + if got := itp.AllocateInode(tmp, now); got != a+1 { + t.Fatalf("AllocateInode = a%+d, want a+1", int64(got-a)) + } + + // a, a+1 and a+2 are held + itp.Lookup("/run/z", now, false, false, a+1, true) + if got := itp.AllocateInode(tmp, now); got != a+3 { + t.Fatalf("AllocateInode = a%+d, want a+3", int64(got-a)) + } +}