weedfs_file_read.go 3.4 KB

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