Skip to content

Commit 1183627

Browse files
committed
start write-out once and a chunk at a time, stopping at unlink
starting write-out can block for a long time on a busy disk, and the file stays open meanwhile Signed-off-by: Benjamin Elder <[email protected]>
1 parent 32432e1 commit 1183627

2 files changed

Lines changed: 68 additions & 4 deletions

File tree

‎internal/snapshotcache/snapshotcache.go‎

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func StartWriteback(paths []string, done func()) error {
140140
}()
141141
// poll instead of waiting on write-out, so a file unlinked meanwhile is
142142
// closed and its dirty pages dropped instead of written back
143-
for i := 0; pollWritebackFiles(files, i > 0); i++ {
143+
for i := 0; pollWritebackFiles(files, i == 1); i++ {
144144
time.Sleep(RetryDelays[min(i, len(RetryDelays)-1)])
145145
}
146146
}()
@@ -172,14 +172,36 @@ func pollWritebackFiles(files []*os.File, startAsyncWrite bool) bool {
172172
files[i] = nil
173173
continue
174174
}
175-
anyRemaining = true
176-
if startAsyncWrite && cstat.Dirty > 0 {
177-
_ = syncFileRangeFn(int(f.Fd()), 0, 0, unix.SYNC_FILE_RANGE_WRITE)
175+
if startAsyncWrite && cstat.Dirty > 0 && !startWriteOut(f) {
176+
_ = f.Close()
177+
files[i] = nil
178+
continue
178179
}
180+
anyRemaining = true
179181
}
180182
return anyRemaining
181183
}
182184

185+
// writeOutChunk is how much of a file startWriteOut submits per call
186+
const writeOutChunk = 64 << 20
187+
188+
// startWriteOut starts write-out of f's dirty pages, and reports whether f is
189+
// still linked. Starting write-out can block for a long time on a busy disk, so
190+
// it goes a chunk at a time and stops once f is unlinked.
191+
// https://github.com/torvalds/linux/blob/v6.12/fs/sync.c#L315-L317
192+
func startWriteOut(f *os.File) bool {
193+
for off := int64(0); ; off += writeOutChunk {
194+
info, err := f.Stat()
195+
if err != nil || isUnlinked(info) {
196+
return false
197+
}
198+
if off >= info.Size() {
199+
return true
200+
}
201+
_ = syncFileRangeFn(int(f.Fd()), off, writeOutChunk, unix.SYNC_FILE_RANGE_WRITE)
202+
}
203+
}
204+
183205
func isUnlinked(info fs.FileInfo) bool {
184206
st, ok := info.Sys().(*syscall.Stat_t)
185207
return ok && st.Nlink == 0

‎internal/snapshotcache/snapshotcache_test.go‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,48 @@ func TestStartWritebackClosesUnlinked(t *testing.T) {
289289
}
290290
}
291291

292+
func TestStartWritebackStopsWriteOutOnUnlink(t *testing.T) {
293+
origCachestat := cachestatFn
294+
origSyncFileRange := syncFileRangeFn
295+
origDelays := RetryDelays
296+
t.Cleanup(func() {
297+
cachestatFn = origCachestat
298+
syncFileRangeFn = origSyncFileRange
299+
RetryDelays = origDelays
300+
})
301+
RetryDelays = []time.Duration{time.Millisecond}
302+
img := filepath.Join(t.TempDir(), "memory-ranges")
303+
if err := os.WriteFile(img, nil, 0o600); err != nil {
304+
t.Fatal(err)
305+
}
306+
if err := os.Truncate(img, 3*writeOutChunk); err != nil {
307+
t.Fatal(err)
308+
}
309+
cachestatFn = func(_ uint, _ *unix.CachestatRange, cstat *unix.Cachestat_t, _ uint) error {
310+
*cstat = unix.Cachestat_t{Cache: 16, Dirty: 8}
311+
return nil
312+
}
313+
var writes atomic.Int32
314+
syncFileRangeFn = func(int, int64, int64, int) error {
315+
if writes.Add(1) == 1 {
316+
return os.Remove(img)
317+
}
318+
return nil
319+
}
320+
done := make(chan struct{})
321+
if err := StartWriteback([]string{img}, func() { close(done) }); err != nil {
322+
t.Fatalf("StartWriteback: %v", err)
323+
}
324+
select {
325+
case <-done:
326+
case <-time.After(10 * time.Second):
327+
t.Fatal("done never ran for a file unlinked during write-out")
328+
}
329+
if got := writes.Load(); got != 1 {
330+
t.Errorf("%d write-out calls, want 1 before the file was unlinked", got)
331+
}
332+
}
333+
292334
func TestStartWritebackWithoutCachestat(t *testing.T) {
293335
origCachestat := cachestatFn
294336
origSyncFileRange := syncFileRangeFn

0 commit comments

Comments
 (0)