github.com/demonoid81/moby@v0.0.0-20200517203328-62dd8e17c460/pkg/loopback/ioctl.go (about)

     1  // +build linux
     2  
     3  package loopback // import "github.com/demonoid81/moby/pkg/loopback"
     4  
     5  import (
     6  	"unsafe"
     7  
     8  	"golang.org/x/sys/unix"
     9  )
    10  
    11  func ioctlLoopCtlGetFree(fd uintptr) (int, error) {
    12  	// The ioctl interface for /dev/loop-control (since Linux 3.1) is a bit
    13  	// off compared to what you'd expect: instead of writing an integer to a
    14  	// parameter pointer like unix.IoctlGetInt() expects, it returns the first
    15  	// available loop device index directly.
    16  	ioctlReturn, _, err := unix.Syscall(unix.SYS_IOCTL, fd, LoopCtlGetFree, 0)
    17  	if err != 0 {
    18  		return 0, err
    19  	}
    20  	return int(ioctlReturn), nil
    21  }
    22  
    23  func ioctlLoopSetFd(loopFd, sparseFd uintptr) error {
    24  	return unix.IoctlSetInt(int(loopFd), unix.LOOP_SET_FD, int(sparseFd))
    25  }
    26  
    27  func ioctlLoopSetStatus64(loopFd uintptr, loopInfo *unix.LoopInfo64) error {
    28  	if _, _, err := unix.Syscall(unix.SYS_IOCTL, loopFd, unix.LOOP_SET_STATUS64, uintptr(unsafe.Pointer(loopInfo))); err != 0 {
    29  		return err
    30  	}
    31  	return nil
    32  }
    33  
    34  func ioctlLoopClrFd(loopFd uintptr) error {
    35  	if _, _, err := unix.Syscall(unix.SYS_IOCTL, loopFd, unix.LOOP_CLR_FD, 0); err != 0 {
    36  		return err
    37  	}
    38  	return nil
    39  }
    40  
    41  func ioctlLoopGetStatus64(loopFd uintptr) (*unix.LoopInfo64, error) {
    42  	loopInfo := &unix.LoopInfo64{}
    43  
    44  	if _, _, err := unix.Syscall(unix.SYS_IOCTL, loopFd, unix.LOOP_GET_STATUS64, uintptr(unsafe.Pointer(loopInfo))); err != 0 {
    45  		return nil, err
    46  	}
    47  	return loopInfo, nil
    48  }
    49  
    50  func ioctlLoopSetCapacity(loopFd uintptr, value int) error {
    51  	return unix.IoctlSetInt(int(loopFd), unix.LOOP_SET_CAPACITY, value)
    52  }