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.

103 lines
2.9 KiB

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