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