Files
seaweedfs/weed/mount/inode_to_path_test.go
T
Chris LuandGitHub b1fecf3b44 mount: mark windows files archived and ignore a zero timestamp (#10559)
* mount: mark windows files archived and ignore a zero timestamp

Windows synthesises NORMAL when a file reports no attributes at all, which
is not the same as ARCHIVE and is what create_fileattr_test checks.

Utimens also wrote a zero timestamp through. Windows sends zero for a field
it is not setting, and storing it put 1970 in the atime overlay, which then
overrode the entry's real time — so a file created a moment ago reported an
access time of 1970 whenever the caller asked through an open handle.
Reading the path instead went down a different route and looked right,
which is why a probe of a fresh file showed nothing wrong.

* mount: match the file type by its mask, and only treat the epoch as unset

S_IFDIR is part of the multi-bit type field rather than a flag, so masking
against it alone also matched a symlink, which shares the bit. A regular
file is now identified by the type mask.

Rejecting every timestamp at or below zero also rejected a date genuinely
before 1970. Only the epoch itself is what Windows sends for a field it is
not setting, so that is all that is refused.

create_fileattr goes back on the known-failures list: the archive fix
works and the test simply moves on to ask for READONLY too, which needs
Chflags. Taking it off was premature.

* mount: drop the time overlays when an inode is released

atimeMap and dirMtimeMap are keyed by inode and were only ever trimmed by
a random eviction at capacity. Inodes are derived from the path, so a
delete and recreate hands the same number to a different file, which then
reported the previous file's access time — a file created a moment ago
answering with a time from long before it existed.

Cleared when Forget actually releases the inode, not on every decrement:
a partial forget still has users. Forget now reports that so callers
holding state keyed by the inode know when to drop it.

* ci: keep getfileinfo listed while its access time is unexplained

Two causes have been fixed and neither closed it, so the honest state is
listed-with-a-reason rather than removed in hope.

* mount: drop timestamp overlays while the inode table is locked

Forget released the inode under the table's lock but cleaned up the
atime and dir-mtime overlays after returning from it. Inode numbers are
derived from the path, so a lookup arriving in that window is handed the
same number back and can store a time that the cleanup then deletes.

Run the cleanup at the release point instead, as a callback under the
lock. The directory-cache purge stays deferred until after the unlock,
where it has to be.

Claude-Session: https://claude.ai/code/session_01EgY2QA3iiPtiu6ww3P2EBn
2026-08-04 16:42:33 -07:00

202 lines
5.1 KiB
Go

package mount
import (
"testing"
"time"
"github.com/seaweedfs/seaweedfs/weed/util"
)
func TestInodeEntry_removeOnePath(t *testing.T) {
tests := []struct {
name string
entry InodeEntry
p util.FullPath
want bool
count int
}{
{
name: "actual case",
entry: InodeEntry{
paths: []util.FullPath{"/pjd/nx", "/pjd/n0"},
},
p: "/pjd/nx",
want: true,
count: 1,
},
{
name: "empty",
entry: InodeEntry{},
p: "x",
want: false,
count: 0,
},
{
name: "single",
entry: InodeEntry{
paths: []util.FullPath{"/x"},
},
p: "/x",
want: true,
count: 0,
},
{
name: "first",
entry: InodeEntry{
paths: []util.FullPath{"/x", "/y", "/z"},
},
p: "/x",
want: true,
count: 2,
},
{
name: "middle",
entry: InodeEntry{
paths: []util.FullPath{"/x", "/y", "/z"},
},
p: "/y",
want: true,
count: 2,
},
{
name: "last",
entry: InodeEntry{
paths: []util.FullPath{"/x", "/y", "/z"},
},
p: "/z",
want: true,
count: 2,
},
{
name: "not found",
entry: InodeEntry{
paths: []util.FullPath{"/x", "/y", "/z"},
},
p: "/t",
want: false,
count: 3,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := tt.entry.removeOnePath(tt.p); got != tt.want {
t.Errorf("removeOnePath() = %v, want %v", got, tt.want)
}
if tt.count != len(tt.entry.paths) {
t.Errorf("removeOnePath path count = %v, want %v", len(tt.entry.paths), tt.count)
}
for i, p := range tt.entry.paths {
if p == tt.p {
t.Errorf("removeOnePath found path still exists at %v, %v", i, p)
}
}
})
}
}
// Only directory inodes carry dirState; files never do. This keeps the file
// InodeEntry in the smaller size class, which is the memory win on large mounts.
func TestOnlyDirectoriesGetDirState(t *testing.T) {
itp := NewInodeToPath(util.FullPath("/"), 60)
file := util.FullPath("/data/file.txt")
fileInode := itp.Lookup(file, time.Now().Unix(), false, false, 0, true)
if _, ok := itp.dirStates[fileInode]; ok {
t.Fatal("file inode must not have a dirState")
}
// dir queries against a file are no-ops and must not register one
itp.GetSubdirCount(file)
itp.IsChildrenCached(file)
itp.ShouldReadDirectoryDirect(file)
itp.MarkChildrenCached(file)
if _, ok := itp.dirStates[fileInode]; ok {
t.Fatal("dir queries on a file inode must not create a dirState")
}
dir := util.FullPath("/data")
dirInode := itp.Lookup(dir, time.Now().Unix(), true, false, 0, true)
if _, ok := itp.dirStates[dirInode]; !ok {
t.Fatal("directory inode should be registered in dirStates at creation")
}
// forgetting the directory drops its dirState
itp.Forget(dirInode, 1, nil, nil)
if _, ok := itp.dirStates[dirInode]; ok {
t.Fatal("forgotten directory must be removed from dirStates")
}
}
func TestRecordDirectoryUpdateSwitchesDirectoryToReadThrough(t *testing.T) {
root := util.FullPath("/")
dir := util.FullPath("/data")
inodeToPath := NewInodeToPath(root, 60)
inodeToPath.Lookup(dir, time.Now().Unix(), true, false, 0, true)
inodeToPath.MarkChildrenCached(dir)
now := time.Now()
if !inodeToPath.RecordDirectoryUpdate(dir, now, time.Second, 1) {
t.Fatal("expected directory to switch to read-through mode")
}
if inodeToPath.IsChildrenCached(dir) {
t.Fatal("directory should no longer be marked cached")
}
if !inodeToPath.ShouldReadDirectoryDirect(dir) {
t.Fatal("directory should be served via direct reads after hot invalidation")
}
}
func TestMarkChildrenCachedClearsReadThroughMode(t *testing.T) {
root := util.FullPath("/")
dir := util.FullPath("/data")
inodeToPath := NewInodeToPath(root, 60)
inodeToPath.Lookup(dir, time.Now().Unix(), true, false, 0, true)
if !inodeToPath.MarkDirectoryReadThrough(dir, time.Now()) {
t.Fatal("expected read-through flag to be set")
}
inodeToPath.MarkChildrenCached(dir)
if !inodeToPath.IsChildrenCached(dir) {
t.Fatal("directory should be cached after MarkChildrenCached")
}
if inodeToPath.ShouldReadDirectoryDirect(dir) {
t.Fatal("directory should leave read-through mode after caching")
}
}
// State keyed by an inode number has to be dropped while the table is locked.
// The numbers come from the path, so a lookup racing a forget is handed the
// same one back, and a cleanup running after the unlock would wipe what that
// lookup just stored.
func TestForgetOnReleaseRunsUnderLock(t *testing.T) {
itp := NewInodeToPath(util.FullPath("/"), 60)
file := util.FullPath("/data/f.txt")
inode := itp.Lookup(file, time.Now().Unix(), false, false, 0, true)
itp.Lookup(file, time.Now().Unix(), false, false, 0, true) // nlookup is 2 now
calls := 0
onRelease := func(released uint64) {
calls++
if released != inode {
t.Errorf("released inode = %d, want %d", released, inode)
}
if itp.TryLock() {
itp.Unlock()
t.Error("onRelease ran outside the critical section")
}
}
itp.Forget(inode, 1, onRelease, nil)
if calls != 0 {
t.Fatalf("partial forget must not release: onRelease called %d times", calls)
}
itp.Forget(inode, 1, onRelease, nil)
if calls != 1 {
t.Fatalf("onRelease called %d times, want 1", calls)
}
}