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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
|
package main
import (
"fmt"
"path/filepath"
"runtime"
"syscall"
"time"
"unsafe"
)
// utimbuf mirrors struct utimbuf from <utime.h>: the actime/modtime pair that
// utime(2) reads from userspace. It is passed by pointer as the second arg.
type utimbuf struct {
actime int64 // access time (seconds)
modtime int64 // modification time (seconds)
}
// timeval mirrors struct timeval used by utimes(2): a 2-element array of
// {tv_sec, tv_usec} for the new access and modification times.
type timeval struct {
tvSec int64
tvUsec int64
}
// utimeBasic creates a file and changes its timestamps via raw SYS_UTIME.
// utime(2) takes a real filesystem path at args[0] ("filename"), so ior must
// capture it as a path event (KindPathname) and tag it FamilyFS, matching its
// siblings utimensat/futimesat. We use the raw syscall because Go's os.Chtimes
// wraps utimensat on amd64, not utime.
func utimeBasic() error {
dir, cleanup, err := makeTempDir("utime-basic")
if err != nil {
return err
}
defer cleanup()
path := filepath.Join(dir, "utimefile.txt")
fd, err := syscall.Open(path, syscall.O_RDWR|syscall.O_CREAT, 0o644)
if err != nil {
return fmt.Errorf("open: %w", err)
}
syscall.Close(fd)
pathBytes, err := syscall.BytePtrFromString(path)
if err != nil {
return fmt.Errorf("path bytes: %w", err)
}
times := utimbuf{actime: 1000000000, modtime: 1000000000}
_, _, errno := syscall.Syscall(
syscall.SYS_UTIME,
uintptr(unsafe.Pointer(pathBytes)),
uintptr(unsafe.Pointer(×)),
0,
)
runtime.KeepAlive(pathBytes)
runtime.KeepAlive(×)
if errno != 0 {
return fmt.Errorf("utime: %w", errno)
}
return nil
}
// utimeUtimes creates a file and changes its timestamps via raw SYS_UTIMES.
// utimes(2) is utime's microsecond-resolution sibling and likewise takes a
// real path at args[0] ("filename"), so it is path-classified and FamilyFS.
func utimeUtimes() error {
dir, cleanup, err := makeTempDir("utime-utimes")
if err != nil {
return err
}
defer cleanup()
path := filepath.Join(dir, "utimesfile.txt")
fd, err := syscall.Open(path, syscall.O_RDWR|syscall.O_CREAT, 0o644)
if err != nil {
return fmt.Errorf("open: %w", err)
}
syscall.Close(fd)
pathBytes, err := syscall.BytePtrFromString(path)
if err != nil {
return fmt.Errorf("path bytes: %w", err)
}
times := [2]timeval{
{tvSec: 1000000000, tvUsec: 0},
{tvSec: 1000000000, tvUsec: 0},
}
_, _, errno := syscall.Syscall(
syscall.SYS_UTIMES,
uintptr(unsafe.Pointer(pathBytes)),
uintptr(unsafe.Pointer(×[0])),
0,
)
runtime.KeepAlive(pathBytes)
runtime.KeepAlive(×)
if errno != 0 {
return fmt.Errorf("utimes: %w", errno)
}
return nil
}
// timespec mirrors struct timespec used by utimensat(2): a {tv_sec, tv_nsec}
// pair. utimensat takes a 2-element array for the new access and modification
// times.
type timespec struct {
tvSec int64
tvNsec int64
}
// utimeFutimesat creates a file and changes its timestamps via raw
// SYS_FUTIMESAT. futimesat(2) takes a dirfd at args[0] and a pathname at
// args[1] ("filename"), so ior must capture the path from args[1] (after the
// dirfd), classify it as a path event (KindPathname), and tag it FamilyFS like
// its siblings utime/utimes/utimensat. We pass AT_FDCWD as the dirfd so the
// absolute path resolves relative to the cwd, and a 2-element timeval array to
// set explicit times. The raw syscall guarantees the exact enter_futimesat
// tracepoint fires.
func utimeFutimesat() error {
dir, cleanup, err := makeTempDir("utime-futimesat")
if err != nil {
return err
}
defer cleanup()
path := filepath.Join(dir, "futimesatfile.txt")
fd, err := syscall.Open(path, syscall.O_RDWR|syscall.O_CREAT, 0o644)
if err != nil {
return fmt.Errorf("open: %w", err)
}
syscall.Close(fd)
pathBytes, err := syscall.BytePtrFromString(path)
if err != nil {
return fmt.Errorf("path bytes: %w", err)
}
times := [2]timeval{
{tvSec: 1000000000, tvUsec: 0},
{tvSec: 1000000000, tvUsec: 0},
}
// Use a runtime int variable so the negative AT_FDCWD survives the uintptr
// conversion: converting the negative constant directly overflows uintptr.
dirfd := _AT_FDCWD
_, _, errno := syscall.Syscall(
syscall.SYS_FUTIMESAT,
uintptr(dirfd),
uintptr(unsafe.Pointer(pathBytes)),
uintptr(unsafe.Pointer(×[0])),
)
runtime.KeepAlive(pathBytes)
runtime.KeepAlive(×)
if errno != 0 {
return fmt.Errorf("futimesat: %w", errno)
}
return nil
}
// utimeUtimensat creates a file and changes its timestamps via raw
// SYS_UTIMENSAT. utimensat(2) is the nanosecond-resolution sibling: it takes a
// dirfd at args[0] and a pathname at args[1] ("filename"), so the path must be
// captured from args[1] (after the dirfd) and is path-classified and FamilyFS.
// We pass AT_FDCWD as the dirfd, a 2-element timespec array for the times, and
// 0 for flags. The raw syscall guarantees the exact enter_utimensat tracepoint
// fires (Go's os.Chtimes also wraps utimensat, but going raw keeps the dirfd
// and arg layout explicit).
func utimeUtimensat() error {
dir, cleanup, err := makeTempDir("utime-utimensat")
if err != nil {
return err
}
defer cleanup()
path := filepath.Join(dir, "utimensatfile.txt")
fd, err := syscall.Open(path, syscall.O_RDWR|syscall.O_CREAT, 0o644)
if err != nil {
return fmt.Errorf("open: %w", err)
}
syscall.Close(fd)
pathBytes, err := syscall.BytePtrFromString(path)
if err != nil {
return fmt.Errorf("path bytes: %w", err)
}
times := [2]timespec{
{tvSec: 1000000000, tvNsec: 0},
{tvSec: 1000000000, tvNsec: 0},
}
// Use a runtime int variable so the negative AT_FDCWD survives the uintptr
// conversion: converting the negative constant directly overflows uintptr.
dirfd := _AT_FDCWD
_, _, errno := syscall.Syscall6(
syscall.SYS_UTIMENSAT,
uintptr(dirfd),
uintptr(unsafe.Pointer(pathBytes)),
uintptr(unsafe.Pointer(×[0])),
0, // flags
0, 0,
)
runtime.KeepAlive(pathBytes)
runtime.KeepAlive(×)
if errno != 0 {
return fmt.Errorf("utimensat: %w", errno)
}
return nil
}
// utimeEnoent calls raw SYS_UTIME on a nonexistent path. The syscall fails
// with ENOENT, but ior still captures the enter_utime tracepoint because the
// filename path is read on syscall entry. This locks in that the path is
// captured even on the error path.
func utimeEnoent() error {
dir, cleanup, err := makeTempDir("utime-enoent")
if err != nil {
return err
}
defer cleanup()
path := filepath.Join(dir, "utime-enoent-missing.txt")
pathBytes, err := syscall.BytePtrFromString(path)
if err != nil {
return fmt.Errorf("path bytes: %w", err)
}
times := utimbuf{actime: 1000000000, modtime: 1000000000}
// Retry a few times to reduce dropped-event flakiness under high parallelism.
for i := 0; i < 5; i++ {
_, _, errno := syscall.Syscall(
syscall.SYS_UTIME,
uintptr(unsafe.Pointer(pathBytes)),
uintptr(unsafe.Pointer(×)),
0,
)
runtime.KeepAlive(pathBytes)
runtime.KeepAlive(×)
if errno == 0 {
return fmt.Errorf("expected ENOENT, but utime succeeded")
}
if i < 4 {
time.Sleep(statRetryDelay)
}
}
return nil
}
|