seaweedfs/weed/filesys/filehandle.go

339 lines
8.8 KiB
Go
Raw Normal View History

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