2022-02-14 03:14:34 +00:00
|
|
|
package mount
|
|
|
|
|
|
|
|
import (
|
2023-01-03 07:20:45 +00:00
|
|
|
"bytes"
|
|
|
|
"fmt"
|
2023-09-21 18:08:26 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/util"
|
2022-08-22 00:11:43 +00:00
|
|
|
"io"
|
|
|
|
|
2022-02-14 03:14:34 +00:00
|
|
|
"github.com/hanwen/go-fuse/v2/fuse"
|
2022-08-22 00:11:43 +00:00
|
|
|
|
2022-07-29 07:17:28 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
2022-02-14 03:14:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Read data
|
|
|
|
*
|
|
|
|
* Read should send exactly the number of bytes requested except
|
|
|
|
* on EOF or error, otherwise the rest of the data will be
|
|
|
|
* substituted with zeroes. An exception to this is when the file
|
|
|
|
* has been opened in 'direct_io' mode, in which case the return
|
|
|
|
* value of the read system call will reflect the return value of
|
|
|
|
* this operation.
|
|
|
|
*
|
|
|
|
* fi->fh will contain the value set by the open method, or will
|
|
|
|
* be undefined if the open method didn't set any value.
|
|
|
|
*
|
|
|
|
* Valid replies:
|
|
|
|
* fuse_reply_buf
|
|
|
|
* fuse_reply_iov
|
|
|
|
* fuse_reply_data
|
|
|
|
* fuse_reply_err
|
|
|
|
*
|
|
|
|
* @param req request handle
|
|
|
|
* @param ino the inode number
|
|
|
|
* @param size number of bytes to read
|
|
|
|
* @param off offset to read from
|
|
|
|
* @param fi file information
|
|
|
|
*/
|
2022-02-14 06:50:44 +00:00
|
|
|
func (wfs *WFS) Read(cancel <-chan struct{}, in *fuse.ReadIn, buff []byte) (fuse.ReadResult, fuse.Status) {
|
|
|
|
fh := wfs.GetHandle(FileHandleId(in.Fh))
|
|
|
|
if fh == nil {
|
|
|
|
return nil, fuse.ENOENT
|
|
|
|
}
|
|
|
|
|
2023-09-21 18:08:26 +00:00
|
|
|
fhActiveLock := fh.wfs.fhLockTable.AcquireLock("Read", fh.fh, util.SharedLock)
|
|
|
|
defer fh.wfs.fhLockTable.ReleaseLock(fh.fh, fhActiveLock)
|
2022-08-22 00:11:43 +00:00
|
|
|
|
2022-02-14 06:50:44 +00:00
|
|
|
offset := int64(in.Offset)
|
2022-08-22 15:24:06 +00:00
|
|
|
totalRead, err := readDataByFileHandle(buff, fh, offset)
|
2022-02-14 06:50:44 +00:00
|
|
|
if err != nil {
|
|
|
|
glog.Warningf("file handle read %s %d: %v", fh.FullPath(), totalRead, err)
|
|
|
|
return nil, fuse.EIO
|
|
|
|
}
|
|
|
|
|
2023-01-03 07:20:45 +00:00
|
|
|
if IsDebugFileReadWrite {
|
|
|
|
// print(".")
|
|
|
|
mirrorData := make([]byte, totalRead)
|
|
|
|
fh.mirrorFile.ReadAt(mirrorData, offset)
|
|
|
|
if bytes.Compare(mirrorData, buff[:totalRead]) != 0 {
|
|
|
|
|
|
|
|
againBuff := make([]byte, len(buff))
|
2023-07-03 15:33:34 +00:00
|
|
|
againRead, _ := readDataByFileHandle(againBuff, fh, offset)
|
2023-01-03 07:20:45 +00:00
|
|
|
againCorrect := bytes.Compare(mirrorData, againBuff[:againRead]) == 0
|
|
|
|
againSame := bytes.Compare(buff[:totalRead], againBuff[:againRead]) == 0
|
|
|
|
|
|
|
|
fmt.Printf("\ncompare %v [%d,%d) size:%d againSame:%v againCorrect:%v\n", fh.mirrorFile.Name(), offset, offset+totalRead, totalRead, againSame, againCorrect)
|
|
|
|
//fmt.Printf("read mirrow data: %v\n", mirrorData)
|
2023-07-03 15:33:34 +00:00
|
|
|
//fmt.Printf("read actual data: %v\n", againBuff[:totalRead])
|
2023-01-03 07:20:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-14 06:50:44 +00:00
|
|
|
return fuse.ReadResultData(buff[:totalRead]), fuse.OK
|
2022-02-14 03:14:34 +00:00
|
|
|
}
|
2022-08-22 15:24:06 +00:00
|
|
|
|
|
|
|
func readDataByFileHandle(buff []byte, fhIn *FileHandle, offset int64) (int64, error) {
|
|
|
|
// read data from source file
|
|
|
|
size := len(buff)
|
|
|
|
fhIn.lockForRead(offset, size)
|
|
|
|
defer fhIn.unlockForRead(offset, size)
|
|
|
|
|
2023-01-03 07:20:45 +00:00
|
|
|
n, tsNs, err := fhIn.readFromChunks(buff, offset)
|
2022-08-22 15:24:06 +00:00
|
|
|
if err == nil || err == io.EOF {
|
2023-01-03 07:20:45 +00:00
|
|
|
maxStop := fhIn.readFromDirtyPages(buff, offset, tsNs)
|
2022-08-22 15:24:06 +00:00
|
|
|
n = max(maxStop-offset, n)
|
|
|
|
}
|
|
|
|
if err == io.EOF {
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
return n, err
|
|
|
|
}
|