diff --git a/weed/mount/weedfs.go b/weed/mount/weedfs.go index 1910239e58..2349976154 100644 --- a/weed/mount/weedfs.go +++ b/weed/mount/weedfs.go @@ -801,6 +801,11 @@ func (wfs *WFS) maybeReadEntry(ctx context.Context, inode uint64) (path util.Ful return "", nil, nil, wfs.pathlessInodeStatus(inode, status) } entry, _, status = wfs.maybeLoadEntry(ctx, path) + if status == fuse.ENOENT { + // The filer no longer has the inode's name: the kernel looks the name + // up again on ESTALE. + status = wfs.pathlessInodeStatus(inode, status) + } return } diff --git a/weed/mount/weedfs_filehandle.go b/weed/mount/weedfs_filehandle.go index 26795c9b3d..7e47f9f1ae 100644 --- a/weed/mount/weedfs_filehandle.go +++ b/weed/mount/weedfs_filehandle.go @@ -48,6 +48,12 @@ func (wfs *WFS) AcquireHandle(ctx context.Context, inode uint64, flags, uid, gid entryVersionTsNs = entryVersion{tsNs: existingFh.entryVersionTsNs.Load(), signature: existingFh.entryVersionSignature.Load()} } else { entry, entryVersionTsNs, status = wfs.maybeLoadEntry(ctx, path) + if status == fuse.ENOENT { + // The name the kernel resolved is gone from the filer: ESTALE + // has the open look it up again (with O_CREAT, create it), + // where ENOENT failed it. + return nil, 0, wfs.pathlessInodeStatus(inode, status) + } if status != fuse.OK { return nil, 0, status } diff --git a/weed/mount/weedfs_vanished_name_test.go b/weed/mount/weedfs_vanished_name_test.go new file mode 100644 index 0000000000..7f05d53bb4 --- /dev/null +++ b/weed/mount/weedfs_vanished_name_test.go @@ -0,0 +1,91 @@ +package mount + +import ( + "context" + "runtime" + "syscall" + "testing" + + "github.com/seaweedfs/go-fuse/v2/fuse" + "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" + + "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb" +) + +// staleStatus is the answer for an inode whose name is gone: ESTALE, which has +// Linux walk the path again; elsewhere ENOENT (see pathlessInodeStatus). +func staleStatus() fuse.Status { + if runtime.GOOS != "linux" { + return fuse.ENOENT + } + return fuse.Status(syscall.ESTALE) +} + +// vanishedNameFiler has the directory /dir and nothing in it. +type vanishedNameFiler struct { + filer_pb.UnimplementedSeaweedFilerServer +} + +func (s *vanishedNameFiler) LookupDirectoryEntry(ctx context.Context, r *filer_pb.LookupDirectoryEntryRequest) (*filer_pb.LookupDirectoryEntryResponse, error) { + if r.Directory == "/" && r.Name == "dir" { + return &filer_pb.LookupDirectoryEntryResponse{Entry: &filer_pb.Entry{Name: "dir", IsDirectory: true, Attributes: &filer_pb.FuseAttributes{FileMode: uint32(0o777) | 1<<31}}}, nil + } + return nil, status.Error(codes.NotFound, filer_pb.ErrNotFound.Error()) +} + +func (s *vanishedNameFiler) CreateEntry(ctx context.Context, r *filer_pb.CreateEntryRequest) (*filer_pb.CreateEntryResponse, error) { + return &filer_pb.CreateEntryResponse{}, nil +} + +func (s *vanishedNameFiler) UpdateEntry(ctx context.Context, r *filer_pb.UpdateEntryRequest) (*filer_pb.UpdateEntryResponse, error) { + return &filer_pb.UpdateEntryResponse{}, nil +} + +// A job retried a failed round by opening its tmp name again, which the +// kernel still resolved to the failed round's inode. The filer had no such +// file, and the open failed with ENOENT, O_CREAT notwithstanding. ESTALE has +// the kernel look the name up again and create the file. +func TestOpenOfVanishedNameAnswersEstale(t *testing.T) { + wfs := newInvalidateTestWFS(t) + wfs.option.FilerMountRootPath = "/" + startFakeFiler(t, wfs, &vanishedNameFiler{}) + dirInode := wfs.inodeToPath.Lookup("/dir", 1, true, false, 0, true) + const stale = uint64(197923552737547634) + wfs.inodeToPath.Lookup("/dir/0115.json.tmp-7", 1796393673, false, false, stale, true) + + caller := fuse.Caller{Owner: fuse.Owner{Uid: 1000, Gid: 20}} + var opened fuse.OpenOut + code := wfs.Open(make(chan struct{}), &fuse.OpenIn{InHeader: fuse.InHeader{NodeId: stale, Caller: caller}, Flags: syscall.O_WRONLY}, &opened) + if code != staleStatus() { + t.Fatalf("open of the vanished name's inode: %v, want %v", code, staleStatus()) + } + // The name stays mapped, so a handle another opener publishes meanwhile + // still follows a rename event about it. + if got, found := wfs.inodeToPath.GetInode("/dir/0115.json.tmp-7"); !found || got != stale { + t.Fatalf("name mapped to %d (%v), want it kept on %d", got, found, stale) + } + + // The kernel looks the name up again, finds nothing and creates it. + var out fuse.CreateOut + if code := wfs.Create(make(chan struct{}), &fuse.CreateIn{InHeader: fuse.InHeader{NodeId: dirInode, Caller: caller}, Flags: syscall.O_WRONLY | syscall.O_CREAT | syscall.O_TRUNC, Mode: 0o644}, "0115.json.tmp-7", &out); code != fuse.OK { + t.Fatalf("create after ESTALE: %v", code) + } + wfs.ReleaseHandle(FileHandleId(out.Fh)) +} + +// A stat of the inode, as the first request after the file went away, gets +// ESTALE too. +func TestGetAttrOfVanishedNameAnswersEstale(t *testing.T) { + wfs := newInvalidateTestWFS(t) + wfs.option.FilerMountRootPath = "/" + startFakeFiler(t, wfs, &vanishedNameFiler{}) + wfs.inodeToPath.Lookup("/dir", 1, true, false, 0, true) + const stale = uint64(197923552737547634) + wfs.inodeToPath.Lookup("/dir/0115.json.tmp-7", 1796393673, false, false, stale, true) + + var attr fuse.AttrOut + if code := wfs.GetAttr(make(chan struct{}), &fuse.GetAttrIn{InHeader: fuse.InHeader{NodeId: stale}}, &attr); code != staleStatus() { + t.Fatalf("getattr of the vanished name's inode: %v, want %v", code, staleStatus()) + } +}