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