From 633f9e206045176a12c301eb2c249c1c1d9a5d2e Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Mon, 2 Nov 2020 12:01:33 +0100 Subject: internal/poll: treat copy_file_range EIO as not-handled Fixes #42334 Change-Id: Ife51df4e7d2539a04393abfdec45e3f902975fca Reviewed-on: https://go-review.googlesource.com/c/go/+/266940 Trust: Tobias Klauser Run-TryBot: Tobias Klauser TryBot-Result: Go Bot Reviewed-by: Ian Lance Taylor --- src/internal/poll/copy_file_range_linux.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) (limited to 'src/internal/poll/copy_file_range_linux.go') diff --git a/src/internal/poll/copy_file_range_linux.go b/src/internal/poll/copy_file_range_linux.go index 09de299ff7..24bee614a6 100644 --- a/src/internal/poll/copy_file_range_linux.go +++ b/src/internal/poll/copy_file_range_linux.go @@ -41,7 +41,7 @@ func CopyFileRange(dst, src *FD, remain int64) (written int64, handled bool, err // use copy_file_range(2) again. atomic.StoreInt32(©FileRangeSupported, 0) return 0, false, nil - case syscall.EXDEV, syscall.EINVAL, syscall.EOPNOTSUPP, syscall.EPERM: + case syscall.EXDEV, syscall.EINVAL, syscall.EIO, syscall.EOPNOTSUPP, syscall.EPERM: // Prior to Linux 5.3, it was not possible to // copy_file_range across file systems. Similarly to // the ENOSYS case above, if we see EXDEV, we have @@ -53,6 +53,9 @@ func CopyFileRange(dst, src *FD, remain int64) (written int64, handled bool, err // file. This is another case where no data has been // transfered, so we consider it unhandled. // + // If src and dst are on CIFS, we can see EIO. + // See issue #42334. + // // If the file is on NFS, we can see EOPNOTSUPP. // See issue #40731. // -- cgit v1.3 From 1c7650aa93bd53b7df0bbb34693fc5a16d9f67af Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Mon, 9 Nov 2020 11:25:38 +0100 Subject: internal/poll: use copy_file_range only on Linux kernel >= 5.3 https://man7.org/linux/man-pages/man2/copy_file_range.2.html#VERSIONS states: A major rework of the kernel implementation occurred in 5.3. Areas of the API that weren't clearly defined were clarified and the API bounds are much more strictly checked than on earlier kernels. Applications should target the behaviour and requirements of 5.3 kernels. Rather than attempting to detect the file system for source and destination files (which means two additional statfs syscalls) and skip copy_file_range in case of known defects (e.g. CIFS -> CIFS), just assume copy_file_range to be broken on kernels < 5.3. Fixes #42400 Change-Id: I3a531296182c1d6e341772cc9d2be5bf83e52575 Reviewed-on: https://go-review.googlesource.com/c/go/+/268338 Trust: Tobias Klauser Run-TryBot: Tobias Klauser TryBot-Result: Go Bot Reviewed-by: Ian Lance Taylor --- src/internal/poll/copy_file_range_linux.go | 49 ++++++++++++++++++++++++++++-- 1 file changed, 47 insertions(+), 2 deletions(-) (limited to 'src/internal/poll/copy_file_range_linux.go') diff --git a/src/internal/poll/copy_file_range_linux.go b/src/internal/poll/copy_file_range_linux.go index 24bee614a6..1635bb1bfc 100644 --- a/src/internal/poll/copy_file_range_linux.go +++ b/src/internal/poll/copy_file_range_linux.go @@ -10,15 +10,60 @@ import ( "syscall" ) -var copyFileRangeSupported int32 = 1 // accessed atomically +var copyFileRangeSupported int32 = -1 // accessed atomically const maxCopyFileRangeRound = 1 << 30 +func kernelVersion() (major int, minor int) { + var uname syscall.Utsname + if err := syscall.Uname(&uname); err != nil { + return + } + + rl := uname.Release + var values [2]int + vi := 0 + value := 0 + for _, c := range rl { + if '0' <= c && c <= '9' { + value = (value * 10) + int(c-'0') + } else { + // Note that we're assuming N.N.N here. If we see anything else we are likely to + // mis-parse it. + values[vi] = value + vi++ + if vi >= len(values) { + break + } + } + } + switch vi { + case 0: + return 0, 0 + case 1: + return values[0], 0 + case 2: + return values[0], values[1] + } + return +} + // CopyFileRange copies at most remain bytes of data from src to dst, using // the copy_file_range system call. dst and src must refer to regular files. func CopyFileRange(dst, src *FD, remain int64) (written int64, handled bool, err error) { - if atomic.LoadInt32(©FileRangeSupported) == 0 { + if supported := atomic.LoadInt32(©FileRangeSupported); supported == 0 { return 0, false, nil + } else if supported == -1 { + major, minor := kernelVersion() + if major > 5 || (major == 5 && minor >= 3) { + atomic.StoreInt32(©FileRangeSupported, 1) + } else { + // copy_file_range(2) is broken in various ways on kernels older than 5.3, + // see issue #42400 and + // https://man7.org/linux/man-pages/man2/copy_file_range.2.html#VERSIONS + atomic.StoreInt32(©FileRangeSupported, 0) + return 0, false, nil + } } for remain > 0 { max := remain -- cgit v1.3 From 66c02645062561ac29d00297e8d8c49698b2e4da Mon Sep 17 00:00:00 2001 From: Ian Lance Taylor Date: Thu, 19 Nov 2020 19:15:57 -0800 Subject: net, internal/poll: reset value before adding in minor kernel version Fixes #42733 Change-Id: I5446aeb5de13cd70212755fb12c9bc484f343c74 Reviewed-on: https://go-review.googlesource.com/c/go/+/271846 Trust: Ian Lance Taylor Run-TryBot: Ian Lance Taylor Reviewed-by: Dmitri Shuralyov Reviewed-by: Cuong Manh Le TryBot-Result: Go Bot --- src/internal/poll/copy_file_range_linux.go | 1 + src/net/sock_linux.go | 1 + 2 files changed, 2 insertions(+) (limited to 'src/internal/poll/copy_file_range_linux.go') diff --git a/src/internal/poll/copy_file_range_linux.go b/src/internal/poll/copy_file_range_linux.go index 1635bb1bfc..fc34aef4cb 100644 --- a/src/internal/poll/copy_file_range_linux.go +++ b/src/internal/poll/copy_file_range_linux.go @@ -35,6 +35,7 @@ func kernelVersion() (major int, minor int) { if vi >= len(values) { break } + value = 0 } } switch vi { diff --git a/src/net/sock_linux.go b/src/net/sock_linux.go index 4d91001937..9f62ed3dee 100644 --- a/src/net/sock_linux.go +++ b/src/net/sock_linux.go @@ -27,6 +27,7 @@ func kernelVersion() (major int, minor int) { if vi >= len(values) { break } + value = 0 } } switch vi { -- cgit v1.3