weedfs_file_read.go 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. package mount
  2. import (
  3. "bytes"
  4. "fmt"
  5. "github.com/seaweedfs/seaweedfs/weed/util"
  6. "io"
  7. "github.com/hanwen/go-fuse/v2/fuse"
  8. "github.com/seaweedfs/seaweedfs/weed/glog"
  9. )
  10. /**
  11. * Read data
  12. *
  13. * Read should send exactly the number of bytes requested except
  14. * on EOF or error, otherwise the rest of the data will be
  15. * substituted with zeroes. An exception to this is when the file
  16. * has been opened in 'direct_io' mode, in which case the return
  17. * value of the read system call will reflect the return value of
  18. * this operation.
  19. *
  20. * fi->fh will contain the value set by the open method, or will
  21. * be undefined if the open method didn't set any value.
  22. *
  23. * Valid replies:
  24. * fuse_reply_buf
  25. * fuse_reply_iov
  26. * fuse_reply_data
  27. * fuse_reply_err
  28. *
  29. * @param req request handle
  30. * @param ino the inode number
  31. * @param size number of bytes to read
  32. * @param off offset to read from
  33. * @param fi file information
  34. */
  35. func (wfs *WFS) Read(cancel <-chan struct{}, in *fuse.ReadIn, buff []byte) (fuse.ReadResult, fuse.Status) {
  36. fh := wfs.GetHandle(FileHandleId(in.Fh))
  37. if fh == nil {
  38. return nil, fuse.ENOENT
  39. }
  40. fhActiveLock := fh.wfs.fhLockTable.AcquireLock("Read", fh.fh, util.SharedLock)
  41. defer fh.wfs.fhLockTable.ReleaseLock(fh.fh, fhActiveLock)
  42. offset := int64(in.Offset)
  43. totalRead, err := readDataByFileHandle(buff, fh, offset)
  44. if err != nil {
  45. glog.Warningf("file handle read %s %d: %v", fh.FullPath(), totalRead, err)
  46. return nil, fuse.EIO
  47. }
  48. if IsDebugFileReadWrite {
  49. // print(".")
  50. mirrorData := make([]byte, totalRead)
  51. fh.mirrorFile.ReadAt(mirrorData, offset)
  52. if bytes.Compare(mirrorData, buff[:totalRead]) != 0 {
  53. againBuff := make([]byte, len(buff))
  54. againRead, _ := readDataByFileHandle(againBuff, fh, offset)
  55. againCorrect := bytes.Compare(mirrorData, againBuff[:againRead]) == 0
  56. againSame := bytes.Compare(buff[:totalRead], againBuff[:againRead]) == 0
  57. fmt.Printf("\ncompare %v [%d,%d) size:%d againSame:%v againCorrect:%v\n", fh.mirrorFile.Name(), offset, offset+totalRead, totalRead, againSame, againCorrect)
  58. //fmt.Printf("read mirrow data: %v\n", mirrorData)
  59. //fmt.Printf("read actual data: %v\n", againBuff[:totalRead])
  60. }
  61. }
  62. return fuse.ReadResultData(buff[:totalRead]), fuse.OK
  63. }
  64. func readDataByFileHandle(buff []byte, fhIn *FileHandle, offset int64) (int64, error) {
  65. // read data from source file
  66. size := len(buff)
  67. fhIn.lockForRead(offset, size)
  68. defer fhIn.unlockForRead(offset, size)
  69. n, tsNs, err := fhIn.readFromChunks(buff, offset)
  70. if err == nil || err == io.EOF {
  71. maxStop := fhIn.readFromDirtyPages(buff, offset, tsNs)
  72. n = max(maxStop-offset, n)
  73. }
  74. if err == io.EOF {
  75. err = nil
  76. }
  77. return n, err
  78. }