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 <noreply@anthropic.com>
This commit is contained in:
Yip LeoandClaude Opus 5.5 authored and GitHub committed 2026-11-22 10:17:25 -08:00
1 parent 32d4633298
commit 78e2a4c2b4
2 files changed
+26 -1

No files matched your search

+1 -1
View File
@@ -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
}
+25
View File
@@ -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))
}
}