Browse Source

refactor

pull/6015/head
chrislu 3 months ago
parent
commit
a9c9e1bcb3
  1. 23
      weed/mount/weedfs.go
  2. 2
      weed/mount/weedfs_attr.go
  3. 2
      weed/mount/weedfs_dir_lookup.go
  4. 22
      weed/mount/weedfs_dir_read.go
  5. 6
      weed/mount/weedfs_filehandle.go
  6. 2
      weed/mount/weedfs_forget.go
  7. 2
      weed/mount/weedfs_rename.go

23
weed/mount/weedfs.go

@ -2,6 +2,7 @@ package mount
import ( import (
"context" "context"
"errors"
"math/rand" "math/rand"
"os" "os"
"path" "path"
@ -76,8 +77,8 @@ type WFS struct {
signature int32 signature int32
concurrentWriters *util.LimitedConcurrentExecutor concurrentWriters *util.LimitedConcurrentExecutor
inodeToPath *InodeToPath inodeToPath *InodeToPath
fhmap *FileHandleToInode
dhmap *DirectoryHandleToInode
fhMap *FileHandleToInode
dhMap *DirectoryHandleToInode
fuseServer *fuse.Server fuseServer *fuse.Server
IsOverQuota bool IsOverQuota bool
fhLockTable *util.LockTable[FileHandleId] fhLockTable *util.LockTable[FileHandleId]
@ -89,8 +90,8 @@ func NewSeaweedFileSystem(option *Option) *WFS {
option: option, option: option,
signature: util.RandomInt32(), signature: util.RandomInt32(),
inodeToPath: NewInodeToPath(util.FullPath(option.FilerMountRootPath)), inodeToPath: NewInodeToPath(util.FullPath(option.FilerMountRootPath)),
fhmap: NewFileHandleToInode(),
dhmap: NewDirectoryHandleToInode(),
fhMap: NewFileHandleToInode(),
dhMap: NewDirectoryHandleToInode(),
fhLockTable: util.NewLockTable[FileHandleId](), fhLockTable: util.NewLockTable[FileHandleId](),
} }
@ -108,9 +109,9 @@ func NewSeaweedFileSystem(option *Option) *WFS {
return wfs.inodeToPath.IsChildrenCached(path) return wfs.inodeToPath.IsChildrenCached(path)
}, func(filePath util.FullPath, entry *filer_pb.Entry) { }, func(filePath util.FullPath, entry *filer_pb.Entry) {
// Find inode if it is not a deleted path // Find inode if it is not a deleted path
if inode, inode_found := wfs.inodeToPath.GetInode(filePath); inode_found {
if inode, inodeFound := wfs.inodeToPath.GetInode(filePath); inodeFound {
// Find open file handle // Find open file handle
if fh, fh_found := wfs.fhmap.FindFileHandle(inode); fh_found {
if fh, fhFound := wfs.fhMap.FindFileHandle(inode); fhFound {
fhActiveLock := fh.wfs.fhLockTable.AcquireLock("invalidateFunc", fh.fh, util.ExclusiveLock) fhActiveLock := fh.wfs.fhLockTable.AcquireLock("invalidateFunc", fh.fh, util.ExclusiveLock)
defer fh.wfs.fhLockTable.ReleaseLock(fh.fh, fhActiveLock) defer fh.wfs.fhLockTable.ReleaseLock(fh.fh, fhActiveLock)
@ -119,10 +120,10 @@ func NewSeaweedFileSystem(option *Option) *WFS {
fh.dirtyPages = newPageWriter(fh, wfs.option.ChunkSizeLimit) fh.dirtyPages = newPageWriter(fh, wfs.option.ChunkSizeLimit)
// Update handle entry // Update handle entry
newentry, status := wfs.maybeLoadEntry(filePath)
newEntry, status := wfs.maybeLoadEntry(filePath)
if status == fuse.OK { if status == fuse.OK {
if fh.GetEntry().GetEntry() != newentry {
fh.SetEntry(newentry)
if fh.GetEntry().GetEntry() != newEntry {
fh.SetEntry(newEntry)
} }
} }
} }
@ -160,7 +161,7 @@ func (wfs *WFS) maybeReadEntry(inode uint64) (path util.FullPath, fh *FileHandle
return return
} }
var found bool var found bool
if fh, found = wfs.fhmap.FindFileHandle(inode); found {
if fh, found = wfs.fhMap.FindFileHandle(inode); found {
entry = fh.UpdateEntry(func(entry *filer_pb.Entry) { entry = fh.UpdateEntry(func(entry *filer_pb.Entry) {
if entry != nil && fh.entry.Attributes == nil { if entry != nil && fh.entry.Attributes == nil {
entry.Attributes = &filer_pb.FuseAttributes{} entry.Attributes = &filer_pb.FuseAttributes{}
@ -195,7 +196,7 @@ func (wfs *WFS) maybeLoadEntry(fullpath util.FullPath) (*filer_pb.Entry, fuse.St
// read from async meta cache // read from async meta cache
meta_cache.EnsureVisited(wfs.metaCache, wfs, util.FullPath(dir)) meta_cache.EnsureVisited(wfs.metaCache, wfs, util.FullPath(dir))
cachedEntry, cacheErr := wfs.metaCache.FindEntry(context.Background(), fullpath) cachedEntry, cacheErr := wfs.metaCache.FindEntry(context.Background(), fullpath)
if cacheErr == filer_pb.ErrNotFound {
if errors.Is(cacheErr, filer_pb.ErrNotFound) {
return nil, fuse.ENOENT return nil, fuse.ENOENT
} }
return cachedEntry.ToProtoEntry(), fuse.OK return cachedEntry.ToProtoEntry(), fuse.OK

2
weed/mount/weedfs_attr.go

@ -23,7 +23,7 @@ func (wfs *WFS) GetAttr(cancel <-chan struct{}, input *fuse.GetAttrIn, out *fuse
wfs.setAttrByPbEntry(&out.Attr, inode, entry, true) wfs.setAttrByPbEntry(&out.Attr, inode, entry, true)
return status return status
} else { } else {
if fh, found := wfs.fhmap.FindFileHandle(inode); found {
if fh, found := wfs.fhMap.FindFileHandle(inode); found {
out.AttrValid = 1 out.AttrValid = 1
wfs.setAttrByPbEntry(&out.Attr, inode, fh.entry.GetEntry(), true) wfs.setAttrByPbEntry(&out.Attr, inode, fh.entry.GetEntry(), true)
out.Nlink = 0 out.Nlink = 0

2
weed/mount/weedfs_dir_lookup.go

@ -57,7 +57,7 @@ func (wfs *WFS) Lookup(cancel <-chan struct{}, header *fuse.InHeader, name strin
inode := wfs.inodeToPath.Lookup(fullFilePath, localEntry.Crtime.Unix(), localEntry.IsDirectory(), len(localEntry.HardLinkId) > 0, localEntry.Inode, true) inode := wfs.inodeToPath.Lookup(fullFilePath, localEntry.Crtime.Unix(), localEntry.IsDirectory(), len(localEntry.HardLinkId) > 0, localEntry.Inode, true)
if fh, found := wfs.fhmap.FindFileHandle(inode); found {
if fh, found := wfs.fhMap.FindFileHandle(inode); found {
fh.entryLock.RLock() fh.entryLock.RLock()
if entry := fh.GetEntry().GetEntry(); entry != nil { if entry := fh.GetEntry().GetEntry(); entry != nil {
glog.V(4).Infof("lookup opened file %s size %d", dirPath.Child(localEntry.Name()), filer.FileSize(entry)) glog.V(4).Infof("lookup opened file %s size %d", dirPath.Child(localEntry.Name()), filer.FileSize(entry))

22
weed/mount/weedfs_dir_read.go

@ -46,30 +46,30 @@ func NewDirectoryHandleToInode() *DirectoryHandleToInode {
func (wfs *WFS) AcquireDirectoryHandle() (DirectoryHandleId, *DirectoryHandle) { func (wfs *WFS) AcquireDirectoryHandle() (DirectoryHandleId, *DirectoryHandle) {
fh := FileHandleId(util.RandomUint64()) fh := FileHandleId(util.RandomUint64())
wfs.dhmap.Lock()
defer wfs.dhmap.Unlock()
wfs.dhMap.Lock()
defer wfs.dhMap.Unlock()
dh := new(DirectoryHandle) dh := new(DirectoryHandle)
dh.reset() dh.reset()
wfs.dhmap.dir2inode[DirectoryHandleId(fh)] = dh
wfs.dhMap.dir2inode[DirectoryHandleId(fh)] = dh
return DirectoryHandleId(fh), dh return DirectoryHandleId(fh), dh
} }
func (wfs *WFS) GetDirectoryHandle(dhid DirectoryHandleId) *DirectoryHandle { func (wfs *WFS) GetDirectoryHandle(dhid DirectoryHandleId) *DirectoryHandle {
wfs.dhmap.Lock()
defer wfs.dhmap.Unlock()
if dh, found := wfs.dhmap.dir2inode[dhid]; found {
wfs.dhMap.Lock()
defer wfs.dhMap.Unlock()
if dh, found := wfs.dhMap.dir2inode[dhid]; found {
return dh return dh
} }
dh := new(DirectoryHandle) dh := new(DirectoryHandle)
dh.reset() dh.reset()
wfs.dhmap.dir2inode[dhid] = dh
wfs.dhMap.dir2inode[dhid] = dh
return dh return dh
} }
func (wfs *WFS) ReleaseDirectoryHandle(dhid DirectoryHandleId) { func (wfs *WFS) ReleaseDirectoryHandle(dhid DirectoryHandleId) {
wfs.dhmap.Lock()
defer wfs.dhmap.Unlock()
delete(wfs.dhmap.dir2inode, dhid)
wfs.dhMap.Lock()
defer wfs.dhMap.Unlock()
delete(wfs.dhMap.dir2inode, dhid)
} }
// Directory handling // Directory handling
@ -169,7 +169,7 @@ func (wfs *WFS) doReadDirectory(input *fuse.ReadIn, out *fuse.DirEntryList, isPl
isEarlyTerminated = true isEarlyTerminated = true
return false return false
} }
if fh, found := wfs.fhmap.FindFileHandle(inode); found {
if fh, found := wfs.fhMap.FindFileHandle(inode); found {
glog.V(4).Infof("readdir opened file %s", dirPath.Child(dirEntry.Name)) glog.V(4).Infof("readdir opened file %s", dirPath.Child(dirEntry.Name))
entry = filer.FromPbEntry(string(dirPath), fh.GetEntry().GetEntry()) entry = filer.FromPbEntry(string(dirPath), fh.GetEntry().GetEntry())
} }

6
weed/mount/weedfs_filehandle.go

@ -18,15 +18,15 @@ func (wfs *WFS) AcquireHandle(inode uint64, flags, uid, gid uint32) (fileHandle
} }
} }
// need to AcquireFileHandle again to ensure correct handle counter // need to AcquireFileHandle again to ensure correct handle counter
fileHandle = wfs.fhmap.AcquireFileHandle(wfs, inode, entry)
fileHandle = wfs.fhMap.AcquireFileHandle(wfs, inode, entry)
} }
return return
} }
func (wfs *WFS) ReleaseHandle(handleId FileHandleId) { func (wfs *WFS) ReleaseHandle(handleId FileHandleId) {
wfs.fhmap.ReleaseByHandle(handleId)
wfs.fhMap.ReleaseByHandle(handleId)
} }
func (wfs *WFS) GetHandle(handleId FileHandleId) *FileHandle { func (wfs *WFS) GetHandle(handleId FileHandleId) *FileHandle {
return wfs.fhmap.GetFileHandle(handleId)
return wfs.fhMap.GetFileHandle(handleId)
} }

2
weed/mount/weedfs_forget.go

@ -65,5 +65,5 @@ func (wfs *WFS) Forget(nodeid, nlookup uint64) {
wfs.inodeToPath.Forget(nodeid, nlookup, func(dir util.FullPath) { wfs.inodeToPath.Forget(nodeid, nlookup, func(dir util.FullPath) {
wfs.metaCache.DeleteFolderChildren(context.Background(), dir) wfs.metaCache.DeleteFolderChildren(context.Background(), dir)
}) })
wfs.fhmap.ReleaseByInode(nodeid)
wfs.fhMap.ReleaseByInode(nodeid)
} }

2
weed/mount/weedfs_rename.go

@ -235,7 +235,7 @@ func (wfs *WFS) handleRenameResponse(ctx context.Context, resp *filer_pb.StreamR
sourceInode, targetInode := wfs.inodeToPath.MovePath(oldPath, newPath) sourceInode, targetInode := wfs.inodeToPath.MovePath(oldPath, newPath)
if sourceInode != 0 { if sourceInode != 0 {
fh, foundFh := wfs.fhmap.FindFileHandle(sourceInode)
fh, foundFh := wfs.fhMap.FindFileHandle(sourceInode)
if foundFh { if foundFh {
if entry := fh.GetEntry(); entry != nil { if entry := fh.GetEntry(); entry != nil {
entry.Name = newName entry.Name = newName

Loading…
Cancel
Save