weedfs_file_copy_range.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. package mount
  2. import (
  3. "github.com/seaweedfs/seaweedfs/weed/util"
  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. fhOutActiveLock := fhOut.wfs.fhLockTable.AcquireLock("CopyFileRange", fhOut.fh, util.ExclusiveLock)
  43. defer fhOut.wfs.fhLockTable.ReleaseLock(fhOut.fh, fhOutActiveLock)
  44. if fhOut.entry == nil {
  45. return 0, fuse.ENOENT
  46. }
  47. if fhIn.fh != fhOut.fh {
  48. fhInActiveLock := fhIn.wfs.fhLockTable.AcquireLock("CopyFileRange", fhIn.fh, util.SharedLock)
  49. defer fhIn.wfs.fhLockTable.ReleaseLock(fhIn.fh, fhInActiveLock)
  50. }
  51. // directories are not supported
  52. if fhIn.entry.IsDirectory || fhOut.entry.IsDirectory {
  53. return 0, fuse.EISDIR
  54. }
  55. glog.V(4).Infof(
  56. "CopyFileRange %s fhIn %d -> %s fhOut %d, [%d,%d) -> [%d,%d)",
  57. fhIn.FullPath(), fhIn.fh,
  58. fhOut.FullPath(), fhOut.fh,
  59. in.OffIn, in.OffIn+in.Len,
  60. in.OffOut, in.OffOut+in.Len,
  61. )
  62. data := make([]byte, in.Len)
  63. totalRead, err := readDataByFileHandle(data, fhIn, int64(in.OffIn))
  64. if err != nil {
  65. glog.Warningf("file handle read %s %d: %v", fhIn.FullPath(), totalRead, err)
  66. return 0, fuse.EIO
  67. }
  68. data = data[:totalRead]
  69. if totalRead == 0 {
  70. return 0, fuse.OK
  71. }
  72. // put data at the specified offset in target file
  73. fhOut.dirtyPages.writerPattern.MonitorWriteAt(int64(in.OffOut), int(in.Len))
  74. fhOut.entry.Content = nil
  75. fhOut.dirtyPages.AddPage(int64(in.OffOut), data, fhOut.dirtyPages.writerPattern.IsSequentialMode(), time.Now().UnixNano())
  76. fhOut.entry.Attributes.FileSize = uint64(max(int64(in.OffOut)+totalRead, int64(fhOut.entry.Attributes.FileSize)))
  77. fhOut.dirtyMetadata = true
  78. written = uint32(totalRead)
  79. // detect mime type
  80. if written > 0 && in.OffOut <= 512 {
  81. fhOut.contentType = http.DetectContentType(data)
  82. }
  83. return written, fuse.OK
  84. }