seaweedfs/weed/filesys/file.go

315 lines
7.8 KiB
Go
Raw Normal View History

2018-05-06 13:47:16 +08:00
package filesys
import (
2018-05-28 02:52:26 +08:00
"context"
"io"
"os"
"sort"
"time"
"github.com/seaweedfs/fuse"
"github.com/seaweedfs/fuse/fs"
2018-05-28 02:52:26 +08:00
"github.com/chrislusf/seaweedfs/weed/filer2"
2018-05-08 16:59:43 +08:00
"github.com/chrislusf/seaweedfs/weed/glog"
2018-05-10 14:18:02 +08:00
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
2020-03-23 15:01:34 +08:00
"github.com/chrislusf/seaweedfs/weed/util"
2018-05-06 13:47:16 +08:00
)
const blockSize = 512
2018-05-08 16:59:43 +08:00
var _ = fs.Node(&File{})
var _ = fs.NodeOpener(&File{})
var _ = fs.NodeFsyncer(&File{})
var _ = fs.NodeSetattrer(&File{})
2019-12-16 13:07:01 +08:00
var _ = fs.NodeGetxattrer(&File{})
var _ = fs.NodeSetxattrer(&File{})
var _ = fs.NodeRemovexattrer(&File{})
var _ = fs.NodeListxattrer(&File{})
var _ = fs.NodeForgetter(&File{})
2018-05-08 16:59:43 +08:00
2018-05-06 13:47:16 +08:00
type File struct {
Name string
dir *Dir
wfs *WFS
entry *filer_pb.Entry
2019-01-01 07:10:14 +08:00
entryViewCache []filer2.VisibleInterval
isOpen int
reader io.ReaderAt
dirtyMetadata bool
2018-05-06 13:47:16 +08:00
}
2020-03-23 15:01:34 +08:00
func (file *File) fullpath() util.FullPath {
return util.NewFullPath(file.dir.FullPath(), file.Name)
2018-06-06 17:09:57 +08:00
}
2018-06-06 17:09:57 +08:00
func (file *File) Attr(ctx context.Context, attr *fuse.Attr) error {
glog.V(5).Infof("file Attr %s, open:%v, existing attr: %+v", file.fullpath(), file.isOpen, attr)
if file.isOpen <= 0 {
2020-01-20 15:59:46 +08:00
if err := file.maybeLoadEntry(ctx); err != nil {
return err
}
}
2018-05-08 16:59:43 +08:00
2020-01-20 15:59:46 +08:00
attr.Inode = file.fullpath().AsInode()
attr.Valid = time.Second
attr.Mode = os.FileMode(file.entry.Attributes.FileMode)
attr.Size = filer2.FileSize(file.entry)
if file.isOpen > 0 {
2020-01-20 04:07:04 +08:00
attr.Size = file.entry.Attributes.FileSize
2020-01-25 16:32:18 +08:00
glog.V(4).Infof("file Attr %s, open:%v, size: %d", file.fullpath(), file.isOpen, attr.Size)
2020-01-20 04:07:04 +08:00
}
attr.Crtime = time.Unix(file.entry.Attributes.Crtime, 0)
attr.Mtime = time.Unix(file.entry.Attributes.Mtime, 0)
attr.Gid = file.entry.Attributes.Gid
attr.Uid = file.entry.Attributes.Uid
attr.Blocks = attr.Size/blockSize + 1
2018-10-14 15:18:52 +08:00
attr.BlockSize = uint32(file.wfs.option.ChunkSizeLimit)
2018-05-20 04:51:44 +08:00
return nil
2018-05-08 16:59:43 +08:00
}
2019-12-16 13:07:01 +08:00
func (file *File) Getxattr(ctx context.Context, req *fuse.GetxattrRequest, resp *fuse.GetxattrResponse) error {
2020-03-23 07:19:00 +08:00
glog.V(4).Infof("file Getxattr %s", file.fullpath())
2019-12-16 13:07:01 +08:00
if err := file.maybeLoadEntry(ctx); err != nil {
return err
}
return getxattr(file.entry, req, resp)
}
func (file *File) Open(ctx context.Context, req *fuse.OpenRequest, resp *fuse.OpenResponse) (fs.Handle, error) {
2018-05-23 18:08:46 +08:00
2020-08-19 14:42:09 +08:00
glog.V(4).Infof("file %v open %+v", file.fullpath(), req)
file.isOpen++
2018-06-06 17:09:57 +08:00
handle := file.wfs.AcquireHandle(file, req.Uid, req.Gid)
resp.Handle = fuse.HandleID(handle.handle)
2020-08-19 14:42:26 +08:00
glog.V(4).Infof("%v file open handle id = %d", file.fullpath(), handle.handle)
2018-06-06 17:09:57 +08:00
return handle, nil
}
func (file *File) Setattr(ctx context.Context, req *fuse.SetattrRequest, resp *fuse.SetattrResponse) error {
2020-08-16 12:09:31 +08:00
glog.V(5).Infof("%v file setattr %+v", file.fullpath(), req)
2019-12-16 13:07:01 +08:00
if err := file.maybeLoadEntry(ctx); err != nil {
return err
}
if file.isOpen > 0 {
file.wfs.handlesLock.Lock()
fileHandle := file.wfs.handles[file.fullpath().AsInode()]
file.wfs.handlesLock.Unlock()
if fileHandle != nil {
fileHandle.Lock()
defer fileHandle.Unlock()
}
}
if req.Valid.Size() {
glog.V(4).Infof("%v file setattr set size=%v chunks=%d", file.fullpath(), req.Size, len(file.entry.Chunks))
2020-01-20 16:00:08 +08:00
if req.Size < filer2.TotalSize(file.entry.Chunks) {
2018-05-25 14:20:12 +08:00
// fmt.Printf("truncate %v \n", fullPath)
2020-01-20 16:00:08 +08:00
var chunks []*filer_pb.FileChunk
var truncatedChunks []*filer_pb.FileChunk
2020-01-20 16:00:08 +08:00
for _, chunk := range file.entry.Chunks {
int64Size := int64(chunk.Size)
if chunk.Offset+int64Size > int64(req.Size) {
// this chunk is truncated
int64Size = int64(req.Size) - chunk.Offset
if int64Size > 0 {
chunks = append(chunks, chunk)
2020-08-16 10:55:28 +08:00
glog.V(4).Infof("truncated chunk %+v from %d to %d\n", chunk.GetFileIdString(), chunk.Size, int64Size)
chunk.Size = uint64(int64Size)
} else {
2020-08-16 10:55:28 +08:00
glog.V(4).Infof("truncated whole chunk %+v\n", chunk.GetFileIdString())
truncatedChunks = append(truncatedChunks, chunk)
}
2020-01-20 16:00:08 +08:00
}
}
file.wfs.deleteFileChunks(truncatedChunks)
2020-01-20 16:00:08 +08:00
file.entry.Chunks = chunks
file.entryViewCache = nil
file.reader = nil
}
file.entry.Attributes.FileSize = req.Size
2020-08-16 12:09:31 +08:00
file.dirtyMetadata = true
}
2020-08-16 12:09:31 +08:00
if req.Valid.Mode() {
file.entry.Attributes.FileMode = uint32(req.Mode)
2020-08-16 05:15:07 +08:00
file.dirtyMetadata = true
}
if req.Valid.Uid() {
file.entry.Attributes.Uid = req.Uid
2020-08-16 05:15:07 +08:00
file.dirtyMetadata = true
}
if req.Valid.Gid() {
file.entry.Attributes.Gid = req.Gid
2020-08-16 05:15:07 +08:00
file.dirtyMetadata = true
}
2018-12-29 14:37:18 +08:00
if req.Valid.Crtime() {
file.entry.Attributes.Crtime = req.Crtime.Unix()
2020-08-16 05:15:07 +08:00
file.dirtyMetadata = true
2018-12-29 14:37:18 +08:00
}
if req.Valid.Mtime() {
file.entry.Attributes.Mtime = req.Mtime.Unix()
2020-08-16 05:15:07 +08:00
file.dirtyMetadata = true
}
if req.Valid.Handle() {
// fmt.Printf("file handle => %d\n", req.Handle)
}
if file.isOpen > 0 {
2019-02-16 02:00:27 +08:00
return nil
}
2020-08-16 10:56:24 +08:00
if !file.dirtyMetadata {
return nil
}
return file.saveEntry()
2019-12-16 13:07:01 +08:00
}
2019-12-16 13:07:01 +08:00
func (file *File) Setxattr(ctx context.Context, req *fuse.SetxattrRequest) error {
2019-12-16 13:07:01 +08:00
glog.V(4).Infof("file Setxattr %s: %s", file.fullpath(), req.Name)
2019-12-16 13:07:01 +08:00
if err := file.maybeLoadEntry(ctx); err != nil {
return err
}
2019-12-16 13:07:01 +08:00
if err := setxattr(file.entry, req); err != nil {
return err
}
return file.saveEntry()
2018-05-20 04:51:44 +08:00
2018-05-06 13:47:16 +08:00
}
2019-12-16 13:07:01 +08:00
func (file *File) Removexattr(ctx context.Context, req *fuse.RemovexattrRequest) error {
2019-12-16 13:07:01 +08:00
glog.V(4).Infof("file Removexattr %s: %s", file.fullpath(), req.Name)
2019-12-16 13:07:01 +08:00
if err := file.maybeLoadEntry(ctx); err != nil {
return err
}
if err := removexattr(file.entry, req); err != nil {
return err
}
return file.saveEntry()
2019-12-16 13:07:01 +08:00
}
2019-12-16 13:07:01 +08:00
func (file *File) Listxattr(ctx context.Context, req *fuse.ListxattrRequest, resp *fuse.ListxattrResponse) error {
2019-12-16 14:24:06 +08:00
glog.V(4).Infof("file Listxattr %s", file.fullpath())
2019-12-16 13:07:01 +08:00
if err := file.maybeLoadEntry(ctx); err != nil {
return err
}
2019-12-16 13:07:01 +08:00
if err := listxattr(file.entry, req, resp); err != nil {
return err
}
2019-12-18 12:38:56 +08:00
return nil
2018-11-08 03:35:13 +08:00
2019-12-16 13:07:01 +08:00
}
2019-12-16 13:07:01 +08:00
func (file *File) Fsync(ctx context.Context, req *fuse.FsyncRequest) error {
// fsync works at OS level
// write the file chunks to the filerGrpcAddress
2020-08-14 15:22:21 +08:00
glog.V(4).Infof("%s/%s fsync file %+v", file.dir.FullPath(), file.Name, req)
2019-12-16 13:07:01 +08:00
return nil
}
func (file *File) Forget() {
2020-05-02 16:33:49 +08:00
t := util.NewFullPath(file.dir.FullPath(), file.Name)
2020-08-16 09:24:35 +08:00
glog.V(5).Infof("Forget file %s", t)
2020-08-10 12:56:09 +08:00
file.wfs.fsNodeCache.DeleteFsNode(t)
}
2019-12-16 13:07:01 +08:00
func (file *File) maybeLoadEntry(ctx context.Context) error {
if file.entry == nil || file.isOpen <= 0 {
entry, err := file.wfs.maybeLoadEntry(file.dir.FullPath(), file.Name)
2019-12-16 13:07:01 +08:00
if err != nil {
glog.V(3).Infof("maybeLoadEntry file %s/%s: %v", file.dir.FullPath(), file.Name, err)
2019-12-16 13:07:01 +08:00
return err
}
if entry != nil {
file.setEntry(entry)
}
}
return nil
}
2019-01-06 07:16:39 +08:00
func (file *File) addChunks(chunks []*filer_pb.FileChunk) {
sort.Slice(chunks, func(i, j int) bool {
2020-08-23 15:00:36 +08:00
return chunks[i].Fid.FileKey < chunks[j].Fid.FileKey
})
var newVisibles []filer2.VisibleInterval
2019-01-06 07:16:39 +08:00
for _, chunk := range chunks {
newVisibles = filer2.MergeIntoVisibles(file.entryViewCache, newVisibles, chunk)
t := file.entryViewCache[:0]
file.entryViewCache = newVisibles
newVisibles = t
2019-01-06 07:16:39 +08:00
}
file.reader = nil
2020-08-14 15:22:21 +08:00
glog.V(4).Infof("%s existing %d chunks adds %d more", file.fullpath(), len(file.entry.Chunks), len(chunks))
2019-06-22 03:05:00 +08:00
file.entry.Chunks = append(file.entry.Chunks, chunks...)
2019-01-06 07:16:39 +08:00
}
2019-01-06 07:21:56 +08:00
func (file *File) setEntry(entry *filer_pb.Entry) {
file.entry = entry
file.entryViewCache, _ = filer2.NonOverlappingVisibleIntervals(filer2.LookupFn(file.wfs), file.entry.Chunks)
file.reader = nil
2019-01-06 07:21:56 +08:00
}
2019-12-16 13:07:01 +08:00
func (file *File) saveEntry() error {
return file.wfs.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
2019-12-16 13:07:01 +08:00
request := &filer_pb.UpdateEntryRequest{
Directory: file.dir.FullPath(),
2019-12-16 13:07:01 +08:00
Entry: file.entry,
}
2020-08-16 12:09:31 +08:00
glog.V(4).Infof("save file entry: %v", request)
_, err := client.UpdateEntry(context.Background(), request)
2019-12-16 13:07:01 +08:00
if err != nil {
2020-08-19 14:42:09 +08:00
glog.Errorf("UpdateEntry file %s/%s: %v", file.dir.FullPath(), file.Name, err)
2019-12-16 13:07:01 +08:00
return fuse.EIO
}
2020-06-29 01:18:32 +08:00
file.wfs.metaCache.UpdateEntry(context.Background(), filer2.FromPbEntry(request.Directory, request.Entry))
2019-12-16 13:07:01 +08:00
return nil
})
}