You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
3.3 KiB

  1. package mount
  2. import (
  3. "io"
  4. "net/http"
  5. "github.com/hanwen/go-fuse/v2/fuse"
  6. "github.com/seaweedfs/seaweedfs/weed/glog"
  7. )
  8. // CopyFileRange copies data from one file to another from and to specified offsets.
  9. //
  10. // See https://man7.org/linux/man-pages/man2/copy_file_range.2.html
  11. // See https://github.com/libfuse/libfuse/commit/fe4f9428fc403fa8b99051f52d84ea5bd13f3855
  12. /**
  13. * Copy a range of data from one file to another
  14. *
  15. * Niels de Vos: libfuse: add copy_file_range() support
  16. *
  17. * Performs an optimized copy between two file descriptors without the
  18. * additional cost of transferring data through the FUSE kernel module
  19. * to user space (glibc) and then back into the FUSE filesystem again.
  20. *
  21. * In case this method is not implemented, applications are expected to
  22. * fall back to a regular file copy. (Some glibc versions did this
  23. * emulation automatically, but the emulation has been removed from all
  24. * glibc release branches.)
  25. */
  26. func (wfs *WFS) CopyFileRange(cancel <-chan struct{}, in *fuse.CopyFileRangeIn) (written uint32, code fuse.Status) {
  27. // flags must equal 0 for this syscall as of now
  28. if in.Flags != 0 {
  29. return 0, fuse.EINVAL
  30. }
  31. // files must exist
  32. fhOut := wfs.GetHandle(FileHandleId(in.FhOut))
  33. if fhOut == nil {
  34. return 0, fuse.EBADF
  35. }
  36. fhIn := wfs.GetHandle(FileHandleId(in.FhIn))
  37. if fhIn == nil {
  38. return 0, fuse.EBADF
  39. }
  40. // lock source and target file handles
  41. fhOut.Lock()
  42. defer fhOut.Unlock()
  43. fhOut.entryLock.Lock()
  44. defer fhOut.entryLock.Unlock()
  45. if fhOut.entry == nil {
  46. return 0, fuse.ENOENT
  47. }
  48. if fhIn.fh != fhOut.fh {
  49. fhIn.Lock()
  50. defer fhIn.Unlock()
  51. fhIn.entryLock.Lock()
  52. defer fhIn.entryLock.Unlock()
  53. }
  54. // directories are not supported
  55. if fhIn.entry.IsDirectory || fhOut.entry.IsDirectory {
  56. return 0, fuse.EISDIR
  57. }
  58. glog.V(4).Infof(
  59. "CopyFileRange %s fhIn %d -> %s fhOut %d, [%d,%d) -> [%d,%d)",
  60. fhIn.FullPath(), fhIn.fh,
  61. fhOut.FullPath(), fhOut.fh,
  62. in.OffIn, in.OffIn+in.Len,
  63. in.OffOut, in.OffOut+in.Len,
  64. )
  65. data, totalRead, err := readDataByFileHandle(fhIn, in)
  66. if err != nil {
  67. glog.Warningf("file handle read %s %d: %v", fhIn.FullPath(), totalRead, err)
  68. return 0, fuse.EIO
  69. }
  70. if totalRead == 0 {
  71. return 0, fuse.OK
  72. }
  73. // put data at the specified offset in target file
  74. fhOut.dirtyPages.writerPattern.MonitorWriteAt(int64(in.OffOut), int(in.Len))
  75. fhOut.entry.Content = nil
  76. fhOut.dirtyPages.AddPage(int64(in.OffOut), data[:totalRead], fhOut.dirtyPages.writerPattern.IsSequentialMode())
  77. fhOut.entry.Attributes.FileSize = uint64(max(int64(in.OffOut)+totalRead, int64(fhOut.entry.Attributes.FileSize)))
  78. fhOut.dirtyMetadata = true
  79. written = uint32(totalRead)
  80. // detect mime type
  81. if written > 0 && in.OffOut <= 512 {
  82. fhOut.contentType = http.DetectContentType(data[:min(totalRead, 512)-1])
  83. }
  84. return written, fuse.OK
  85. }
  86. func readDataByFileHandle(fhIn *FileHandle, in *fuse.CopyFileRangeIn) ([]byte, int64, error) {
  87. // read data from source file
  88. fhIn.lockForRead(int64(in.OffIn), int(in.Len))
  89. defer fhIn.unlockForRead(int64(in.OffIn), int(in.Len))
  90. data := make([]byte, int(in.Len))
  91. totalRead, err := fhIn.readFromChunks(data, int64(in.OffIn))
  92. if err == nil || err == io.EOF {
  93. maxStop := fhIn.readFromDirtyPages(data, int64(in.OffIn))
  94. totalRead = max(maxStop-int64(in.OffIn), totalRead)
  95. }
  96. if err == io.EOF {
  97. err = nil
  98. }
  99. return data, totalRead, err
  100. }