github.com/JimmyHuang454/JLS-go@v0.0.0-20230831150107-90d536585ba0/internal/poll/copy_file_range_linux.go (about) 1 // Copyright 2020 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package poll 6 7 import ( 8 "internal/syscall/unix" 9 "sync" 10 "syscall" 11 ) 12 13 var ( 14 kernelVersion53Once sync.Once 15 kernelVersion53 bool 16 ) 17 18 const maxCopyFileRangeRound = 1 << 30 19 20 // CopyFileRange copies at most remain bytes of data from src to dst, using 21 // the copy_file_range system call. dst and src must refer to regular files. 22 func CopyFileRange(dst, src *FD, remain int64) (written int64, handled bool, err error) { 23 kernelVersion53Once.Do(func() { 24 major, minor := unix.KernelVersion() 25 // copy_file_range(2) is broken in various ways on kernels older than 5.3, 26 // see issue #42400 and 27 // https://man7.org/linux/man-pages/man2/copy_file_range.2.html#VERSIONS 28 if major > 5 || (major == 5 && minor >= 3) { 29 kernelVersion53 = true 30 } 31 }) 32 33 if !kernelVersion53 { 34 return 0, false, nil 35 } 36 37 for remain > 0 { 38 max := remain 39 if max > maxCopyFileRangeRound { 40 max = maxCopyFileRangeRound 41 } 42 n, err := copyFileRange(dst, src, int(max)) 43 switch err { 44 case syscall.ENOSYS: 45 // copy_file_range(2) was introduced in Linux 4.5. 46 // Go supports Linux >= 2.6.33, so the system call 47 // may not be present. 48 // 49 // If we see ENOSYS, we have certainly not transferred 50 // any data, so we can tell the caller that we 51 // couldn't handle the transfer and let them fall 52 // back to more generic code. 53 return 0, false, nil 54 case syscall.EXDEV, syscall.EINVAL, syscall.EIO, syscall.EOPNOTSUPP, syscall.EPERM: 55 // Prior to Linux 5.3, it was not possible to 56 // copy_file_range across file systems. Similarly to 57 // the ENOSYS case above, if we see EXDEV, we have 58 // not transferred any data, and we can let the caller 59 // fall back to generic code. 60 // 61 // As for EINVAL, that is what we see if, for example, 62 // dst or src refer to a pipe rather than a regular 63 // file. This is another case where no data has been 64 // transferred, so we consider it unhandled. 65 // 66 // If src and dst are on CIFS, we can see EIO. 67 // See issue #42334. 68 // 69 // If the file is on NFS, we can see EOPNOTSUPP. 70 // See issue #40731. 71 // 72 // If the process is running inside a Docker container, 73 // we might see EPERM instead of ENOSYS. See issue 74 // #40893. Since EPERM might also be a legitimate error, 75 // don't mark copy_file_range(2) as unsupported. 76 return 0, false, nil 77 case nil: 78 if n == 0 { 79 // If we did not read any bytes at all, 80 // then this file may be in a file system 81 // where copy_file_range silently fails. 82 // https://lore.kernel.org/linux-fsdevel/20210126233840.GG4626@dread.disaster.area/T/#m05753578c7f7882f6e9ffe01f981bc223edef2b0 83 if written == 0 { 84 return 0, false, nil 85 } 86 // Otherwise src is at EOF, which means 87 // we are done. 88 return written, true, nil 89 } 90 remain -= n 91 written += n 92 default: 93 return written, true, err 94 } 95 } 96 return written, true, nil 97 } 98 99 // copyFileRange performs one round of copy_file_range(2). 100 func copyFileRange(dst, src *FD, max int) (written int64, err error) { 101 // The signature of copy_file_range(2) is: 102 // 103 // ssize_t copy_file_range(int fd_in, loff_t *off_in, 104 // int fd_out, loff_t *off_out, 105 // size_t len, unsigned int flags); 106 // 107 // Note that in the call to unix.CopyFileRange below, we use nil 108 // values for off_in and off_out. For the system call, this means 109 // "use and update the file offsets". That is why we must acquire 110 // locks for both file descriptors (and why this whole machinery is 111 // in the internal/poll package to begin with). 112 if err := dst.writeLock(); err != nil { 113 return 0, err 114 } 115 defer dst.writeUnlock() 116 if err := src.readLock(); err != nil { 117 return 0, err 118 } 119 defer src.readUnlock() 120 var n int 121 for { 122 n, err = unix.CopyFileRange(src.Sysfd, nil, dst.Sysfd, nil, max, 0) 123 if err != syscall.EINTR { 124 break 125 } 126 } 127 return int64(n), err 128 }