summaryrefslogtreecommitdiff
path: root/cmd/ioworkload/scenario_copy_file_range.go
blob: ce0524e99d0bdac0476c89bbbae0acf713db8608 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
package main

import (
	"fmt"
	"path/filepath"
	"syscall"
)

// SYS_COPY_FILE_RANGE on x86_64 Linux.
const sysCopyFileRange = 326

// copyFileRangeBasic copies bytes from a source file to a destination file
// using copy_file_range(2) with flags=0 as required by the manpage.
func copyFileRangeBasic() error {
	dir, cleanup, err := makeTempDir("copy-file-range-basic")
	if err != nil {
		return err
	}
	defer cleanup()

	srcPath := filepath.Join(dir, "copyrangesrc.txt")
	dstPath := filepath.Join(dir, "copyrangedst.txt")

	srcFd, err := syscall.Open(srcPath, syscall.O_RDWR|syscall.O_CREAT|syscall.O_TRUNC, 0o644)
	if err != nil {
		return fmt.Errorf("open source: %w", err)
	}
	defer syscall.Close(srcFd)

	dstFd, err := syscall.Open(dstPath, syscall.O_RDWR|syscall.O_CREAT|syscall.O_TRUNC, 0o644)
	if err != nil {
		return fmt.Errorf("open destination: %w", err)
	}
	defer syscall.Close(dstFd)

	data := []byte("copy_file_range integration data")
	if _, err := syscall.Write(srcFd, data); err != nil {
		return fmt.Errorf("write source: %w", err)
	}
	if _, err := syscall.Seek(srcFd, 0, 0); err != nil {
		return fmt.Errorf("seek source: %w", err)
	}

	n, _, errno := syscall.Syscall6(uintptr(sysCopyFileRange), uintptr(srcFd), 0, uintptr(dstFd), 0, uintptr(len(data)), 0)
	if errno != 0 {
		return fmt.Errorf("copy_file_range: %w", errno)
	}
	if n == 0 {
		return fmt.Errorf("copy_file_range copied 0 bytes")
	}

	return nil
}

// copyFileRangeBadDstFd calls copy_file_range(2) with an invalid destination fd.
// The syscall should fail with EBADF, while still emitting the enter tracepoint.
func copyFileRangeBadDstFd() error {
	dir, cleanup, err := makeTempDir("copy-file-range-bad-dst")
	if err != nil {
		return err
	}
	defer cleanup()

	srcPath := filepath.Join(dir, "copyrangeebadfsrc.txt")
	srcFd, err := syscall.Open(srcPath, syscall.O_RDWR|syscall.O_CREAT|syscall.O_TRUNC, 0o644)
	if err != nil {
		return fmt.Errorf("open source: %w", err)
	}
	defer syscall.Close(srcFd)

	if _, err := syscall.Write(srcFd, []byte("copy_file_range ebadf data")); err != nil {
		return fmt.Errorf("write source: %w", err)
	}

	_, _, errno := syscall.Syscall6(uintptr(sysCopyFileRange), uintptr(srcFd), 0, uintptr(99999), 0, uintptr(16), 0)
	if errno != syscall.EBADF {
		return fmt.Errorf("expected EBADF from copy_file_range with invalid dst fd, got %v", errno)
	}

	return nil
}