2018-05-23 10:08:46 +00:00
|
|
|
package filesys
|
|
|
|
|
|
|
|
import (
|
2018-05-27 18:52:26 +00:00
|
|
|
"context"
|
2018-05-23 10:08:46 +00:00
|
|
|
"fmt"
|
2020-06-10 01:04:40 +00:00
|
|
|
"io"
|
2020-03-22 08:00:36 +00:00
|
|
|
"math"
|
2020-04-14 18:32:31 +00:00
|
|
|
"net/http"
|
2020-06-25 06:26:54 +00:00
|
|
|
"os"
|
2020-08-23 22:48:02 +00:00
|
|
|
"sync"
|
2019-05-03 07:24:35 +00:00
|
|
|
"time"
|
|
|
|
|
2020-08-06 17:04:17 +00:00
|
|
|
"github.com/seaweedfs/fuse"
|
|
|
|
"github.com/seaweedfs/fuse/fs"
|
|
|
|
|
2020-09-01 07:21:19 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/filer"
|
2018-05-23 10:08:46 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
2018-05-27 18:52:26 +00:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
2018-05-23 10:08:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type FileHandle struct {
|
|
|
|
// cache file has been written to
|
2020-08-15 16:32:47 +00:00
|
|
|
dirtyPages *ContinuousDirtyPages
|
|
|
|
contentType string
|
|
|
|
handle uint64
|
2020-08-23 22:48:02 +00:00
|
|
|
sync.RWMutex
|
2018-06-06 09:09:57 +00:00
|
|
|
|
2018-05-25 07:57:25 +00:00
|
|
|
f *File
|
|
|
|
RequestId fuse.RequestID // unique ID for request
|
|
|
|
NodeId fuse.NodeID // file or directory the request is about
|
|
|
|
Uid uint32 // user ID of process making request
|
|
|
|
Gid uint32 // group ID of process making request
|
2020-03-27 11:50:51 +00:00
|
|
|
|
2018-05-23 10:08:46 +00:00
|
|
|
}
|
|
|
|
|
2018-06-06 09:09:57 +00:00
|
|
|
func newFileHandle(file *File, uid, gid uint32) *FileHandle {
|
2020-04-08 19:50:59 +00:00
|
|
|
fh := &FileHandle{
|
2018-06-06 09:09:57 +00:00
|
|
|
f: file,
|
|
|
|
dirtyPages: newDirtyPages(file),
|
|
|
|
Uid: uid,
|
|
|
|
Gid: gid,
|
|
|
|
}
|
2020-04-08 19:50:59 +00:00
|
|
|
if fh.f.entry != nil {
|
2020-09-01 07:21:19 +00:00
|
|
|
fh.f.entry.Attributes.FileSize = filer.FileSize(fh.f.entry)
|
2020-04-08 19:50:59 +00:00
|
|
|
}
|
2020-08-23 22:48:02 +00:00
|
|
|
|
2020-04-08 19:50:59 +00:00
|
|
|
return fh
|
2018-06-06 09:09:57 +00:00
|
|
|
}
|
|
|
|
|
2018-05-23 10:08:46 +00:00
|
|
|
var _ = fs.Handle(&FileHandle{})
|
2018-05-27 18:52:26 +00:00
|
|
|
|
2018-05-24 08:22:37 +00:00
|
|
|
// var _ = fs.HandleReadAller(&FileHandle{})
|
|
|
|
var _ = fs.HandleReader(&FileHandle{})
|
2018-05-23 10:08:46 +00:00
|
|
|
var _ = fs.HandleFlusher(&FileHandle{})
|
|
|
|
var _ = fs.HandleWriter(&FileHandle{})
|
|
|
|
var _ = fs.HandleReleaser(&FileHandle{})
|
|
|
|
|
2018-05-24 08:22:37 +00:00
|
|
|
func (fh *FileHandle) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadResponse) error {
|
2018-05-23 10:08:46 +00:00
|
|
|
|
2020-08-18 19:52:54 +00:00
|
|
|
glog.V(4).Infof("%s read fh %d: [%d,%d) size %d resp.Data cap=%d", fh.f.fullpath(), fh.handle, req.Offset, req.Offset+int64(req.Size), req.Size, cap(resp.Data))
|
2020-08-23 22:48:02 +00:00
|
|
|
fh.RLock()
|
|
|
|
defer fh.RUnlock()
|
|
|
|
|
|
|
|
if req.Size <= 0 {
|
|
|
|
return nil
|
|
|
|
}
|
2018-05-23 10:08:46 +00:00
|
|
|
|
2020-08-16 23:24:40 +00:00
|
|
|
buff := resp.Data[:cap(resp.Data)]
|
|
|
|
if req.Size > cap(resp.Data) {
|
|
|
|
// should not happen
|
|
|
|
buff = make([]byte, req.Size)
|
|
|
|
}
|
2020-01-22 21:42:03 +00:00
|
|
|
|
2020-02-26 06:23:59 +00:00
|
|
|
totalRead, err := fh.readFromChunks(buff, req.Offset)
|
2021-01-18 09:14:42 +00:00
|
|
|
if err == nil || err == io.EOF {
|
2020-08-17 06:47:34 +00:00
|
|
|
maxStop := fh.readFromDirtyPages(buff, req.Offset)
|
2020-08-23 22:48:02 +00:00
|
|
|
totalRead = max(maxStop-req.Offset, totalRead)
|
2020-01-22 21:42:03 +00:00
|
|
|
}
|
|
|
|
|
2020-10-17 18:03:46 +00:00
|
|
|
if err == io.EOF {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
|
2020-01-22 21:42:03 +00:00
|
|
|
if err != nil {
|
2020-08-31 04:01:44 +00:00
|
|
|
glog.Warningf("file handle read %s %d: %v", fh.f.fullpath(), totalRead, err)
|
2020-10-10 22:43:22 +00:00
|
|
|
return fuse.EIO
|
2020-01-22 21:42:03 +00:00
|
|
|
}
|
|
|
|
|
2020-08-17 23:04:56 +00:00
|
|
|
if totalRead > int64(len(buff)) {
|
|
|
|
glog.Warningf("%s FileHandle Read %d: [%d,%d) size %d totalRead %d", fh.f.fullpath(), fh.handle, req.Offset, req.Offset+int64(req.Size), req.Size, totalRead)
|
|
|
|
totalRead = min(int64(len(buff)), totalRead)
|
|
|
|
}
|
2021-01-23 06:39:46 +00:00
|
|
|
if err == nil {
|
|
|
|
resp.Data = buff[:totalRead]
|
|
|
|
}
|
2020-08-17 06:49:10 +00:00
|
|
|
|
2020-01-22 21:42:03 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-08-17 06:47:34 +00:00
|
|
|
func (fh *FileHandle) readFromDirtyPages(buff []byte, startOffset int64) (maxStop int64) {
|
2020-08-23 22:48:02 +00:00
|
|
|
maxStop = fh.dirtyPages.ReadDirtyDataAt(buff, startOffset)
|
|
|
|
return
|
2020-01-22 21:42:03 +00:00
|
|
|
}
|
|
|
|
|
2020-02-26 06:23:59 +00:00
|
|
|
func (fh *FileHandle) readFromChunks(buff []byte, offset int64) (int64, error) {
|
2020-01-22 21:42:03 +00:00
|
|
|
|
2020-09-01 07:21:19 +00:00
|
|
|
fileSize := int64(filer.FileSize(fh.f.entry))
|
2020-08-16 07:49:08 +00:00
|
|
|
|
|
|
|
if fileSize == 0 {
|
2020-01-25 15:34:09 +00:00
|
|
|
glog.V(1).Infof("empty fh %v", fh.f.fullpath())
|
2020-08-16 07:49:08 +00:00
|
|
|
return 0, io.EOF
|
2018-05-23 10:08:46 +00:00
|
|
|
}
|
|
|
|
|
2020-11-30 12:34:04 +00:00
|
|
|
if offset+int64(len(buff)) <= int64(len(fh.f.entry.Content)) {
|
|
|
|
totalRead := copy(buff, fh.f.entry.Content[offset:])
|
|
|
|
glog.V(4).Infof("file handle read cached %s [%d,%d] %d", fh.f.fullpath(), offset, offset+int64(totalRead), totalRead)
|
|
|
|
return int64(totalRead), nil
|
|
|
|
}
|
|
|
|
|
2020-07-20 00:59:43 +00:00
|
|
|
var chunkResolveErr error
|
2018-12-30 08:51:44 +00:00
|
|
|
if fh.f.entryViewCache == nil {
|
2021-01-25 03:01:58 +00:00
|
|
|
fh.f.entryViewCache, chunkResolveErr = filer.NonOverlappingVisibleIntervals(fh.f.wfs.LookupFn(), fh.f.entry.Chunks)
|
2020-07-20 00:59:43 +00:00
|
|
|
if chunkResolveErr != nil {
|
|
|
|
return 0, fmt.Errorf("fail to resolve chunk manifest: %v", chunkResolveErr)
|
|
|
|
}
|
2020-03-22 08:00:36 +00:00
|
|
|
fh.f.reader = nil
|
|
|
|
}
|
2020-03-27 11:50:51 +00:00
|
|
|
|
2021-01-25 01:19:35 +00:00
|
|
|
reader := fh.f.reader
|
|
|
|
if reader == nil {
|
2020-09-01 07:21:19 +00:00
|
|
|
chunkViews := filer.ViewFromVisibleIntervals(fh.f.entryViewCache, 0, math.MaxInt64)
|
2021-01-25 03:03:02 +00:00
|
|
|
reader = filer.NewChunkReaderAtFromClient(fh.f.wfs.LookupFn(), chunkViews, fh.f.wfs.chunkCache, fileSize)
|
2018-12-30 08:51:44 +00:00
|
|
|
}
|
2021-01-25 01:19:35 +00:00
|
|
|
fh.f.reader = reader
|
2018-12-30 08:51:44 +00:00
|
|
|
|
2021-01-25 01:19:35 +00:00
|
|
|
totalRead, err := reader.ReadAt(buff, offset)
|
2018-05-24 08:22:37 +00:00
|
|
|
|
2020-10-25 03:12:04 +00:00
|
|
|
if err != nil && err != io.EOF {
|
2019-06-21 06:45:30 +00:00
|
|
|
glog.Errorf("file handle read %s: %v", fh.f.fullpath(), err)
|
|
|
|
}
|
|
|
|
|
2020-08-18 19:52:54 +00:00
|
|
|
glog.V(4).Infof("file handle read %s [%d,%d] %d : %v", fh.f.fullpath(), offset, offset+int64(totalRead), totalRead, err)
|
2020-03-22 08:00:36 +00:00
|
|
|
|
|
|
|
return int64(totalRead), err
|
2018-05-23 10:08:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write to the file handle
|
|
|
|
func (fh *FileHandle) Write(ctx context.Context, req *fuse.WriteRequest, resp *fuse.WriteResponse) error {
|
2018-05-24 03:55:24 +00:00
|
|
|
|
2020-08-23 22:48:02 +00:00
|
|
|
fh.Lock()
|
|
|
|
defer fh.Unlock()
|
|
|
|
|
2018-05-23 10:08:46 +00:00
|
|
|
// write the request to volume servers
|
2020-10-21 05:54:21 +00:00
|
|
|
data := req.Data
|
2020-10-23 18:31:57 +00:00
|
|
|
if len(data) <= 512 {
|
|
|
|
// fuse message cacheable size
|
|
|
|
data = make([]byte, len(req.Data))
|
|
|
|
copy(data, req.Data)
|
|
|
|
}
|
2018-05-24 03:55:24 +00:00
|
|
|
|
2020-11-30 12:34:04 +00:00
|
|
|
fh.f.entry.Content = nil
|
2020-04-09 05:31:19 +00:00
|
|
|
fh.f.entry.Attributes.FileSize = uint64(max(req.Offset+int64(len(data)), int64(fh.f.entry.Attributes.FileSize)))
|
2020-08-18 16:09:29 +00:00
|
|
|
glog.V(4).Infof("%v write [%d,%d) %d", fh.f.fullpath(), req.Offset, req.Offset+int64(len(req.Data)), len(req.Data))
|
2020-01-24 05:59:58 +00:00
|
|
|
|
2020-10-15 06:28:03 +00:00
|
|
|
fh.dirtyPages.AddPage(req.Offset, data)
|
2018-05-23 10:08:46 +00:00
|
|
|
|
2020-04-09 05:31:19 +00:00
|
|
|
resp.Size = len(data)
|
2018-05-23 10:08:46 +00:00
|
|
|
|
2018-05-31 03:24:57 +00:00
|
|
|
if req.Offset == 0 {
|
2019-03-27 21:25:18 +00:00
|
|
|
// detect mime type
|
2020-04-14 18:32:31 +00:00
|
|
|
fh.contentType = http.DetectContentType(data)
|
2020-08-15 16:32:47 +00:00
|
|
|
fh.f.dirtyMetadata = true
|
2018-05-31 03:24:57 +00:00
|
|
|
}
|
|
|
|
|
2020-10-15 06:28:03 +00:00
|
|
|
fh.f.dirtyMetadata = true
|
2018-05-23 10:08:46 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fh *FileHandle) Release(ctx context.Context, req *fuse.ReleaseRequest) error {
|
|
|
|
|
2020-08-16 02:55:28 +00:00
|
|
|
glog.V(4).Infof("Release %v fh %d", fh.f.fullpath(), fh.handle)
|
2018-06-06 09:09:57 +00:00
|
|
|
|
2020-08-23 22:48:02 +00:00
|
|
|
fh.Lock()
|
|
|
|
defer fh.Unlock()
|
|
|
|
|
2021-01-28 12:46:37 +00:00
|
|
|
if fh.f.isOpen <= 0 {
|
2020-08-23 22:48:02 +00:00
|
|
|
glog.V(0).Infof("Release reset %s open count %d => %d", fh.f.Name, fh.f.isOpen, 0)
|
|
|
|
fh.f.isOpen = 0
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-01-28 12:46:37 +00:00
|
|
|
if fh.f.isOpen == 1 {
|
2020-11-02 22:20:38 +00:00
|
|
|
|
2020-10-15 18:01:43 +00:00
|
|
|
if err := fh.doFlush(ctx, req.Header); err != nil {
|
|
|
|
glog.Errorf("Release doFlush %s: %v", fh.f.Name, err)
|
2021-01-28 12:46:37 +00:00
|
|
|
return err
|
2020-10-15 18:01:43 +00:00
|
|
|
}
|
2020-11-02 22:20:38 +00:00
|
|
|
|
2021-01-28 12:46:37 +00:00
|
|
|
fh.f.isOpen--
|
2020-11-02 22:20:38 +00:00
|
|
|
|
2020-01-22 21:42:03 +00:00
|
|
|
fh.f.wfs.ReleaseHandle(fh.f.fullpath(), fuse.HandleID(fh.handle))
|
2020-12-09 06:26:46 +00:00
|
|
|
if closer, ok := fh.f.reader.(io.Closer); ok {
|
|
|
|
closer.Close()
|
|
|
|
}
|
2020-12-09 06:48:18 +00:00
|
|
|
fh.f.reader = nil
|
2020-01-22 21:42:03 +00:00
|
|
|
}
|
2018-05-25 08:27:21 +00:00
|
|
|
|
2018-05-23 10:08:46 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (fh *FileHandle) Flush(ctx context.Context, req *fuse.FlushRequest) error {
|
2020-08-23 22:48:02 +00:00
|
|
|
|
2021-01-28 12:46:37 +00:00
|
|
|
glog.V(4).Infof("Flush %v fh %d", fh.f.fullpath(), fh.handle)
|
|
|
|
|
2020-08-23 22:48:02 +00:00
|
|
|
fh.Lock()
|
|
|
|
defer fh.Unlock()
|
|
|
|
|
2021-01-28 12:46:37 +00:00
|
|
|
if err := fh.doFlush(ctx, req.Header); err != nil {
|
|
|
|
glog.Errorf("Flush doFlush %s: %v", fh.f.Name, err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
glog.V(4).Infof("Flush %v fh %d success", fh.f.fullpath(), fh.handle)
|
|
|
|
return nil
|
2020-08-19 08:27:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (fh *FileHandle) doFlush(ctx context.Context, header fuse.Header) error {
|
2020-10-15 06:28:03 +00:00
|
|
|
// flush works at fh level
|
2018-05-23 10:08:46 +00:00
|
|
|
// send the data to the OS
|
2020-08-29 18:56:22 +00:00
|
|
|
glog.V(4).Infof("doFlush %s fh %d", fh.f.fullpath(), fh.handle)
|
2018-05-23 10:08:46 +00:00
|
|
|
|
2020-10-15 06:28:03 +00:00
|
|
|
fh.dirtyPages.saveExistingPagesToStorage()
|
2019-01-05 23:16:39 +00:00
|
|
|
|
2020-10-15 06:28:03 +00:00
|
|
|
fh.dirtyPages.writeWaitGroup.Wait()
|
2020-08-23 22:48:02 +00:00
|
|
|
|
2020-10-21 09:16:21 +00:00
|
|
|
if fh.dirtyPages.lastErr != nil {
|
2021-01-28 12:46:37 +00:00
|
|
|
glog.Errorf("%v doFlush last err: %v", fh.f.fullpath(), fh.dirtyPages.lastErr)
|
|
|
|
return fuse.EIO
|
2020-01-23 07:00:04 +00:00
|
|
|
}
|
2018-05-28 19:30:17 +00:00
|
|
|
|
2020-08-15 16:32:47 +00:00
|
|
|
if !fh.f.dirtyMetadata {
|
2018-05-23 10:08:46 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-10-21 09:16:21 +00:00
|
|
|
err := fh.f.wfs.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
|
2018-05-23 10:08:46 +00:00
|
|
|
|
2018-09-22 07:11:46 +00:00
|
|
|
if fh.f.entry.Attributes != nil {
|
|
|
|
fh.f.entry.Attributes.Mime = fh.contentType
|
2020-07-24 17:13:45 +00:00
|
|
|
if fh.f.entry.Attributes.Uid == 0 {
|
2020-08-19 08:27:10 +00:00
|
|
|
fh.f.entry.Attributes.Uid = header.Uid
|
2020-07-24 17:06:43 +00:00
|
|
|
}
|
2020-07-24 17:13:45 +00:00
|
|
|
if fh.f.entry.Attributes.Gid == 0 {
|
2020-08-19 08:27:10 +00:00
|
|
|
fh.f.entry.Attributes.Gid = header.Gid
|
2020-07-24 17:06:43 +00:00
|
|
|
}
|
2020-07-24 17:13:45 +00:00
|
|
|
if fh.f.entry.Attributes.Crtime == 0 {
|
|
|
|
fh.f.entry.Attributes.Crtime = time.Now().Unix()
|
|
|
|
}
|
2018-12-17 07:20:08 +00:00
|
|
|
fh.f.entry.Attributes.Mtime = time.Now().Unix()
|
2020-06-25 06:26:54 +00:00
|
|
|
fh.f.entry.Attributes.FileMode = uint32(os.FileMode(fh.f.entry.Attributes.FileMode) &^ fh.f.wfs.option.Umask)
|
2020-02-25 06:28:45 +00:00
|
|
|
fh.f.entry.Attributes.Collection = fh.dirtyPages.collection
|
|
|
|
fh.f.entry.Attributes.Replication = fh.dirtyPages.replication
|
2018-07-06 06:16:34 +00:00
|
|
|
}
|
|
|
|
|
2018-09-22 07:11:46 +00:00
|
|
|
request := &filer_pb.CreateEntryRequest{
|
2020-08-29 06:48:48 +00:00
|
|
|
Directory: fh.f.dir.FullPath(),
|
|
|
|
Entry: fh.f.entry,
|
|
|
|
Signatures: []int32{fh.f.wfs.signature},
|
2018-05-23 10:08:46 +00:00
|
|
|
}
|
|
|
|
|
2020-08-14 07:22:21 +00:00
|
|
|
glog.V(4).Infof("%s set chunks: %v", fh.f.fullpath(), len(fh.f.entry.Chunks))
|
2019-06-21 19:14:40 +00:00
|
|
|
for i, chunk := range fh.f.entry.Chunks {
|
2020-08-16 02:55:28 +00:00
|
|
|
glog.V(4).Infof("%s chunks %d: %v [%d,%d)", fh.f.fullpath(), i, chunk.GetFileIdString(), chunk.Offset, chunk.Offset+int64(chunk.Size))
|
2019-06-21 19:14:40 +00:00
|
|
|
}
|
2019-01-01 10:33:57 +00:00
|
|
|
|
2020-09-01 07:21:19 +00:00
|
|
|
manifestChunks, nonManifestChunks := filer.SeparateManifestChunks(fh.f.entry.Chunks)
|
2020-08-23 23:59:01 +00:00
|
|
|
|
2021-01-25 03:01:58 +00:00
|
|
|
chunks, _ := filer.CompactFileChunks(fh.f.wfs.LookupFn(), nonManifestChunks)
|
2020-10-25 22:46:29 +00:00
|
|
|
chunks, manifestErr := filer.MaybeManifestize(fh.f.wfs.saveDataAsChunk(fh.f.fullpath()), chunks)
|
2020-07-20 00:59:43 +00:00
|
|
|
if manifestErr != nil {
|
|
|
|
// not good, but should be ok
|
|
|
|
glog.V(0).Infof("MaybeManifestize: %v", manifestErr)
|
|
|
|
}
|
2020-08-24 00:15:12 +00:00
|
|
|
fh.f.entry.Chunks = append(chunks, manifestChunks...)
|
2020-08-06 17:04:17 +00:00
|
|
|
|
2020-09-03 07:07:22 +00:00
|
|
|
fh.f.wfs.mapPbIdFromLocalToFiler(request.Entry)
|
|
|
|
defer fh.f.wfs.mapPbIdFromFilerToLocal(request.Entry)
|
|
|
|
|
2020-02-26 05:50:12 +00:00
|
|
|
if err := filer_pb.CreateEntry(client, request); err != nil {
|
|
|
|
glog.Errorf("fh flush create %s: %v", fh.f.fullpath(), err)
|
|
|
|
return fmt.Errorf("fh flush create %s: %v", fh.f.fullpath(), err)
|
2018-05-23 10:08:46 +00:00
|
|
|
}
|
|
|
|
|
2020-09-01 07:21:19 +00:00
|
|
|
fh.f.wfs.metaCache.InsertEntry(context.Background(), filer.FromPbEntry(request.Directory, request.Entry))
|
2020-04-22 22:40:47 +00:00
|
|
|
|
2018-05-23 10:08:46 +00:00
|
|
|
return nil
|
|
|
|
})
|
2020-01-22 21:42:03 +00:00
|
|
|
|
|
|
|
if err == nil {
|
2020-08-15 16:32:47 +00:00
|
|
|
fh.f.dirtyMetadata = false
|
2020-01-22 21:42:03 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 09:41:31 +00:00
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("%v fh %d flush: %v", fh.f.fullpath(), fh.handle, err)
|
|
|
|
return fuse.EIO
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2018-05-23 10:08:46 +00:00
|
|
|
}
|