From fe57a2e770bf666ffb6f901631a7407a593225de Mon Sep 17 00:00:00 2001 From: chrislu Date: Mon, 14 Feb 2022 01:36:10 -0800 Subject: [PATCH] file set attribute --- weed/mount/filehandle_map.go | 7 ++++++ weed/mount/page_writer.go | 4 +-- weed/mount/weedfs.go | 7 ++++-- weed/mount/weedfs_attr.go | 44 ++++++++++++++++++++++++++++++--- weed/mount/weedfs_dir_read.go | 1 - weed/mount/weedfs_filehandle.go | 2 +- weed/mount/weedfs_xattr.go | 8 +++--- 7 files changed, 59 insertions(+), 14 deletions(-) diff --git a/weed/mount/filehandle_map.go b/weed/mount/filehandle_map.go index 50ca6bcea..80cfd02c7 100644 --- a/weed/mount/filehandle_map.go +++ b/weed/mount/filehandle_map.go @@ -30,6 +30,13 @@ func (i *FileHandleToInode) GetFileHandle(fh FileHandleId) *FileHandle { return nil } +func (i *FileHandleToInode) FindFileHandle(inode uint64) (fh *FileHandle, found bool) { + i.RLock() + defer i.RUnlock() + fh, found = i.inode2fh[inode] + return +} + func (i *FileHandleToInode) AcquireFileHandle(wfs *WFS, inode uint64, entry *filer_pb.Entry) *FileHandle { i.Lock() defer i.Unlock() diff --git a/weed/mount/page_writer.go b/weed/mount/page_writer.go index eaf1fc176..8685b3d15 100644 --- a/weed/mount/page_writer.go +++ b/weed/mount/page_writer.go @@ -30,7 +30,7 @@ func newPageWriter(fh *FileHandle, chunkSize int64) *PageWriter { func (pw *PageWriter) AddPage(offset int64, data []byte) { - glog.V(4).Infof("%v AddPage [%d, %d)", pw.fh, offset, offset+int64(len(data))) + glog.V(4).Infof("%v AddPage [%d, %d)", pw.fh.fh, offset, offset+int64(len(data))) chunkIndex := offset / pw.chunkSize for i := chunkIndex; len(data) > 0; i++ { @@ -50,7 +50,7 @@ func (pw *PageWriter) FlushData() error { } func (pw *PageWriter) ReadDirtyDataAt(data []byte, offset int64) (maxStop int64) { - glog.V(4).Infof("ReadDirtyDataAt %v [%d, %d)", pw.fh, offset, offset+int64(len(data))) + glog.V(4).Infof("ReadDirtyDataAt %v [%d, %d)", pw.fh.fh, offset, offset+int64(len(data))) chunkIndex := offset / pw.chunkSize for i := chunkIndex; len(data) > 0; i++ { diff --git a/weed/mount/weedfs.go b/weed/mount/weedfs.go index 0fdd9bd28..072f562fc 100644 --- a/weed/mount/weedfs.go +++ b/weed/mount/weedfs.go @@ -120,8 +120,12 @@ func (wfs *WFS) String() string { return "seaweedfs" } -func (wfs *WFS) maybeReadEntry(inode uint64) (path util.FullPath, entry *filer_pb.Entry, status fuse.Status) { +func (wfs *WFS) maybeReadEntry(inode uint64) (path util.FullPath, fh *FileHandle, entry *filer_pb.Entry, status fuse.Status) { path = wfs.inodeToPath.GetPath(inode) + var found bool + if fh, found = wfs.fhmap.FindFileHandle(inode); found { + return path, fh, fh.entry, fuse.OK + } entry, status = wfs.maybeLoadEntry(path) return } @@ -146,7 +150,6 @@ func (wfs *WFS) maybeLoadEntry(fullpath util.FullPath) (*filer_pb.Entry, fuse.St }, fuse.OK } - // TODO Use inode to selectively filetering metadata updates // read from async meta cache meta_cache.EnsureVisited(wfs.metaCache, wfs, util.FullPath(dir)) cachedEntry, cacheErr := wfs.metaCache.FindEntry(context.Background(), fullpath) diff --git a/weed/mount/weedfs_attr.go b/weed/mount/weedfs_attr.go index a907493ad..f34885564 100644 --- a/weed/mount/weedfs_attr.go +++ b/weed/mount/weedfs_attr.go @@ -2,6 +2,7 @@ package mount import ( "github.com/chrislusf/seaweedfs/weed/filer" + "github.com/chrislusf/seaweedfs/weed/glog" "github.com/chrislusf/seaweedfs/weed/pb/filer_pb" "github.com/hanwen/go-fuse/v2/fuse" "os" @@ -15,7 +16,7 @@ func (wfs *WFS) GetAttr(cancel <-chan struct{}, input *fuse.GetAttrIn, out *fuse return fuse.OK } - _, entry, status := wfs.maybeReadEntry(input.NodeId) + _, _, entry, status := wfs.maybeReadEntry(input.NodeId) if status != fuse.OK { return status } @@ -27,13 +28,43 @@ func (wfs *WFS) GetAttr(cancel <-chan struct{}, input *fuse.GetAttrIn, out *fuse func (wfs *WFS) SetAttr(cancel <-chan struct{}, input *fuse.SetAttrIn, out *fuse.AttrOut) (code fuse.Status) { - // TODO this is only for directory. Filet setAttr involves open files and truncate to a size - - path, entry, status := wfs.maybeReadEntry(input.NodeId) + path, fh, entry, status := wfs.maybeReadEntry(input.NodeId) if status != fuse.OK { return status } + if size, ok := input.GetSize(); ok { + glog.V(4).Infof("%v setattr set size=%v chunks=%d", path, size, len(entry.Chunks)) + if size < filer.FileSize(entry) { + // fmt.Printf("truncate %v \n", fullPath) + var chunks []*filer_pb.FileChunk + var truncatedChunks []*filer_pb.FileChunk + for _, chunk := range entry.Chunks { + int64Size := int64(chunk.Size) + if chunk.Offset+int64Size > int64(size) { + // this chunk is truncated + int64Size = int64(size) - chunk.Offset + if int64Size > 0 { + chunks = append(chunks, chunk) + glog.V(4).Infof("truncated chunk %+v from %d to %d\n", chunk.GetFileIdString(), chunk.Size, int64Size) + chunk.Size = uint64(int64Size) + } else { + glog.V(4).Infof("truncated whole chunk %+v\n", chunk.GetFileIdString()) + truncatedChunks = append(truncatedChunks, chunk) + } + } + } + // set the new chunks and reset entry cache + entry.Chunks = chunks + if fh != nil { + fh.entryViewCache = nil + } + } + entry.Attributes.Mtime = time.Now().Unix() + entry.Attributes.FileSize = size + + } + if mode, ok := input.GetMode(); ok { entry.Attributes.FileMode = uint32(mode) } @@ -54,6 +85,11 @@ func (wfs *WFS) SetAttr(cancel <-chan struct{}, input *fuse.SetAttrIn, out *fuse out.AttrValid = 1 wfs.setAttrByPbEntry(&out.Attr, input.NodeId, entry) + if fh != nil { + fh.dirtyMetadata = true + return fuse.OK + } + return wfs.saveEntry(path, entry) } diff --git a/weed/mount/weedfs_dir_read.go b/weed/mount/weedfs_dir_read.go index 0177c1863..ad8a161d7 100644 --- a/weed/mount/weedfs_dir_read.go +++ b/weed/mount/weedfs_dir_read.go @@ -116,7 +116,6 @@ func (wfs *WFS) doReadDirectory(input *fuse.ReadIn, out *fuse.DirEntryList, isPl return true } - // TODO remove this with checking whether directory is not forgotten if err := meta_cache.EnsureVisited(wfs.metaCache, wfs, dirPath); err != nil { glog.Errorf("dir ReadDirAll %s: %v", dirPath, err) return fuse.EIO diff --git a/weed/mount/weedfs_filehandle.go b/weed/mount/weedfs_filehandle.go index 03f72282e..3e085df37 100644 --- a/weed/mount/weedfs_filehandle.go +++ b/weed/mount/weedfs_filehandle.go @@ -3,7 +3,7 @@ package mount import "github.com/hanwen/go-fuse/v2/fuse" func (wfs *WFS) AcquireHandle(inode uint64, uid, gid uint32) (fileHandle *FileHandle, code fuse.Status) { - _, entry, status := wfs.maybeReadEntry(inode) + _, _, entry, status := wfs.maybeReadEntry(inode) if status == fuse.OK { fileHandle = wfs.fhmap.AcquireFileHandle(wfs, inode, entry) fileHandle.entry = entry diff --git a/weed/mount/weedfs_xattr.go b/weed/mount/weedfs_xattr.go index 284e47ec0..389e86148 100644 --- a/weed/mount/weedfs_xattr.go +++ b/weed/mount/weedfs_xattr.go @@ -32,7 +32,7 @@ func (wfs *WFS) GetXAttr(cancel <-chan struct{}, header *fuse.InHeader, attr str return 0, fuse.EINVAL } - _, entry, status := wfs.maybeReadEntry(header.NodeId) + _, _, entry, status := wfs.maybeReadEntry(header.NodeId) if status != fuse.OK { return 0, status } @@ -89,7 +89,7 @@ func (wfs *WFS) SetXAttr(cancel <-chan struct{}, input *fuse.SetXAttrIn, attr st } } - path, entry, status := wfs.maybeReadEntry(input.NodeId) + path, _, entry, status := wfs.maybeReadEntry(input.NodeId) if status != fuse.OK { return status } @@ -117,7 +117,7 @@ func (wfs *WFS) SetXAttr(cancel <-chan struct{}, input *fuse.SetXAttrIn, attr st // slice, and return the number of bytes. If the buffer is too // small, return ERANGE, with the required buffer size. func (wfs *WFS) ListXAttr(cancel <-chan struct{}, header *fuse.InHeader, dest []byte) (n uint32, code fuse.Status) { - _, entry, status := wfs.maybeReadEntry(header.NodeId) + _, _, entry, status := wfs.maybeReadEntry(header.NodeId) if status != fuse.OK { return 0, status } @@ -149,7 +149,7 @@ func (wfs *WFS) RemoveXAttr(cancel <-chan struct{}, header *fuse.InHeader, attr if len(attr) == 0 { return fuse.EINVAL } - path, entry, status := wfs.maybeReadEntry(header.NodeId) + path, _, entry, status := wfs.maybeReadEntry(header.NodeId) if status != fuse.OK { return status }