// Copyright 2018 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package poll

import (
	
	
	
	
)

const (
	// spliceNonblock makes calls to splice(2) non-blocking.
	spliceNonblock = 0x2

	// maxSpliceSize is the maximum amount of data Splice asks
	// the kernel to move in a single call to splice(2).
	maxSpliceSize = 4 << 20
)

// Splice transfers at most remain bytes of data from src to dst, using the
// splice system call to minimize copies of data from and to userspace.
//
// Splice creates a temporary pipe, to serve as a buffer for the data transfer.
// src and dst must both be stream-oriented sockets.
//
// If err != nil, sc is the system call which caused the error.
func (,  *FD,  int64) ( int64,  bool,  string,  error) {
	, , ,  := newTempPipe()
	if  != nil {
		return 0, false, , 
	}
	defer destroyTempPipe(, )
	var ,  int
	for  == nil &&  > 0 {
		 := maxSpliceSize
		if int64() >  {
			 = int()
		}
		,  = spliceDrain(, , )
		// The operation is considered handled if splice returns no
		// error, or an error other than EINVAL. An EINVAL means the
		// kernel does not support splice for the socket type of src.
		// The failed syscall does not consume any data so it is safe
		// to fall back to a generic copy.
		//
		// spliceDrain should never return EAGAIN, so if err != nil,
		// Splice cannot continue.
		//
		// If inPipe == 0 && err == nil, src is at EOF, and the
		// transfer is complete.
		 =  || ( != syscall.EINVAL)
		if  != nil || ( == 0 &&  == nil) {
			break
		}
		,  = splicePump(, , )
		if  > 0 {
			 += int64()
			 -= int64()
		}
	}
	if  != nil {
		return , , "splice", 
	}
	return , true, "", nil
}

// spliceDrain moves data from a socket to a pipe.
//
// Invariant: when entering spliceDrain, the pipe is empty. It is either in its
// initial state, or splicePump has emptied it previously.
//
// Given this, spliceDrain can reasonably assume that the pipe is ready for
// writing, so if splice returns EAGAIN, it must be because the socket is not
// ready for reading.
//
// If spliceDrain returns (0, nil), src is at EOF.
func spliceDrain( int,  *FD,  int) (int, error) {
	if  := .readLock();  != nil {
		return 0, 
	}
	defer .readUnlock()
	if  := .pd.prepareRead(.isFile);  != nil {
		return 0, 
	}
	for {
		,  := splice(, .Sysfd, , spliceNonblock)
		if  == syscall.EINTR {
			continue
		}
		if  != syscall.EAGAIN {
			return , 
		}
		if  := .pd.waitRead(.isFile);  != nil {
			return , 
		}
	}
}

// splicePump moves all the buffered data from a pipe to a socket.
//
// Invariant: when entering splicePump, there are exactly inPipe
// bytes of data in the pipe, from a previous call to spliceDrain.
//
// By analogy to the condition from spliceDrain, splicePump
// only needs to poll the socket for readiness, if splice returns
// EAGAIN.
//
// If splicePump cannot move all the data in a single call to
// splice(2), it loops over the buffered data until it has written
// all of it to the socket. This behavior is similar to the Write
// step of an io.Copy in userspace.
func splicePump( *FD,  int,  int) (int, error) {
	if  := .writeLock();  != nil {
		return 0, 
	}
	defer .writeUnlock()
	if  := .pd.prepareWrite(.isFile);  != nil {
		return 0, 
	}
	 := 0
	for  > 0 {
		,  := splice(.Sysfd, , , spliceNonblock)
		// Here, the condition n == 0 && err == nil should never be
		// observed, since Splice controls the write side of the pipe.
		if  > 0 {
			 -= 
			 += 
			continue
		}
		if  != syscall.EAGAIN {
			return , 
		}
		if  := .pd.waitWrite(.isFile);  != nil {
			return , 
		}
	}
	return , nil
}

// splice wraps the splice system call. Since the current implementation
// only uses splice on sockets and pipes, the offset arguments are unused.
// splice returns int instead of int64, because callers never ask it to
// move more data in a single call than can fit in an int32.
func splice( int,  int,  int,  int) (int, error) {
	,  := syscall.Splice(, nil, , nil, , )
	return int(), 
}

var disableSplice unsafe.Pointer

// newTempPipe sets up a temporary pipe for a splice operation.
func newTempPipe() (,  int,  string,  error) {
	 := (*bool)(atomic.LoadPointer(&disableSplice))
	if  != nil && * {
		return -1, -1, "splice", syscall.EINVAL
	}

	var  [2]int
	// pipe2 was added in 2.6.27 and our minimum requirement is 2.6.23, so it
	// might not be implemented. Falling back to pipe is possible, but prior to
	// 2.6.29 splice returns -EAGAIN instead of 0 when the connection is
	// closed.
	const  = syscall.O_CLOEXEC | syscall.O_NONBLOCK
	if  := syscall.Pipe2([:], );  != nil {
		return -1, -1, "pipe2", 
	}

	if  == nil {
		 = new(bool)
		defer atomic.StorePointer(&disableSplice, unsafe.Pointer())

		// F_GETPIPE_SZ was added in 2.6.35, which does not have the -EAGAIN bug.
		if , ,  := syscall.Syscall(unix.FcntlSyscall, uintptr([0]), syscall.F_GETPIPE_SZ, 0);  != 0 {
			* = true
			destroyTempPipe([0], [1])
			return -1, -1, "fcntl", 
		}
	}

	return [0], [1], "", nil
}

// destroyTempPipe destroys a temporary pipe.
func destroyTempPipe(,  int) error {
	 := CloseFunc()
	 := CloseFunc()
	if  == nil {
		return 
	}
	return 
}