github.com/sandwichdev/go-internals@v0.0.0-20210605002614-12311ac6b2c5/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 "sync/atomic" 9 "syscall" 10 11 "github.com/SandwichDev/go-internals/syscall/unix" 12 ) 13 14 var copyFileRangeSupported int32 = -1 // accessed atomically 15 16 const maxCopyFileRangeRound = 1 << 30 17 18 func kernelVersion() (major int, minor int) { 19 var uname syscall.Utsname 20 if err := syscall.Uname(&uname); err != nil { 21 return 22 } 23 24 rl := uname.Release 25 var values [2]int 26 vi := 0 27 value := 0 28 for _, c := range rl { 29 if '0' <= c && c <= '9' { 30 value = (value * 10) + int(c-'0') 31 } else { 32 // Note that we're assuming N.N.N here. If we see anything else we are likely to 33 // mis-parse it. 34 values[vi] = value 35 vi++ 36 if vi >= len(values) { 37 break 38 } 39 value = 0 40 } 41 } 42 switch vi { 43 case 0: 44 return 0, 0 45 case 1: 46 return values[0], 0 47 case 2: 48 return values[0], values[1] 49 } 50 return 51 } 52 53 // CopyFileRange copies at most remain bytes of data from src to dst, using 54 // the copy_file_range system call. dst and src must refer to regular files. 55 func CopyFileRange(dst, src *FD, remain int64) (written int64, handled bool, err error) { 56 if supported := atomic.LoadInt32(©FileRangeSupported); supported == 0 { 57 return 0, false, nil 58 } else if supported == -1 { 59 major, minor := kernelVersion() 60 if major > 5 || (major == 5 && minor >= 3) { 61 atomic.StoreInt32(©FileRangeSupported, 1) 62 } else { 63 // copy_file_range(2) is broken in various ways on kernels older than 5.3, 64 // see issue #42400 and 65 // https://man7.org/linux/man-pages/man2/copy_file_range.2.html#VERSIONS 66 atomic.StoreInt32(©FileRangeSupported, 0) 67 return 0, false, nil 68 } 69 } 70 for remain > 0 { 71 max := remain 72 if max > maxCopyFileRangeRound { 73 max = maxCopyFileRangeRound 74 } 75 n, err := copyFileRange(dst, src, int(max)) 76 switch err { 77 case syscall.ENOSYS: 78 // copy_file_range(2) was introduced in Linux 4.5. 79 // Go supports Linux >= 2.6.33, so the system call 80 // may not be present. 81 // 82 // If we see ENOSYS, we have certainly not transfered 83 // any data, so we can tell the caller that we 84 // couldn't handle the transfer and let them fall 85 // back to more generic code. 86 // 87 // Seeing ENOSYS also means that we will not try to 88 // use copy_file_range(2) again. 89 atomic.StoreInt32(©FileRangeSupported, 0) 90 return 0, false, nil 91 case syscall.EXDEV, syscall.EINVAL, syscall.EIO, syscall.EOPNOTSUPP, syscall.EPERM: 92 // Prior to Linux 5.3, it was not possible to 93 // copy_file_range across file systems. Similarly to 94 // the ENOSYS case above, if we see EXDEV, we have 95 // not transfered any data, and we can let the caller 96 // fall back to generic code. 97 // 98 // As for EINVAL, that is what we see if, for example, 99 // dst or src refer to a pipe rather than a regular 100 // file. This is another case where no data has been 101 // transfered, so we consider it unhandled. 102 // 103 // If src and dst are on CIFS, we can see EIO. 104 // See issue #42334. 105 // 106 // If the file is on NFS, we can see EOPNOTSUPP. 107 // See issue #40731. 108 // 109 // If the process is running inside a Docker container, 110 // we might see EPERM instead of ENOSYS. See issue 111 // #40893. Since EPERM might also be a legitimate error, 112 // don't mark copy_file_range(2) as unsupported. 113 return 0, false, nil 114 case nil: 115 if n == 0 { 116 // If we did not read any bytes at all, 117 // then this file may be in a file system 118 // where copy_file_range silently fails. 119 // https://lore.kernel.org/linux-fsdevel/20210126233840.GG4626@dread.disaster.area/T/#m05753578c7f7882f6e9ffe01f981bc223edef2b0 120 if written == 0 { 121 return 0, false, nil 122 } 123 // Otherwise src is at EOF, which means 124 // we are done. 125 return written, true, nil 126 } 127 remain -= n 128 written += n 129 default: 130 return written, true, err 131 } 132 } 133 return written, true, nil 134 } 135 136 // copyFileRange performs one round of copy_file_range(2). 137 func copyFileRange(dst, src *FD, max int) (written int64, err error) { 138 // The signature of copy_file_range(2) is: 139 // 140 // ssize_t copy_file_range(int fd_in, loff_t *off_in, 141 // int fd_out, loff_t *off_out, 142 // size_t len, unsigned int flags); 143 // 144 // Note that in the call to unix.CopyFileRange below, we use nil 145 // values for off_in and off_out. For the system call, this means 146 // "use and update the file offsets". That is why we must acquire 147 // locks for both file descriptors (and why this whole machinery is 148 // in the internal/poll package to begin with). 149 if err := dst.writeLock(); err != nil { 150 return 0, err 151 } 152 defer dst.writeUnlock() 153 if err := src.readLock(); err != nil { 154 return 0, err 155 } 156 defer src.readUnlock() 157 var n int 158 for { 159 n, err = unix.CopyFileRange(src.Sysfd, nil, dst.Sysfd, nil, max, 0) 160 if err != syscall.EINTR { 161 break 162 } 163 } 164 return int64(n), err 165 }