Line data Source code
1 : // Copyright 2023 The LevelDB-Go and Pebble Authors. All rights reserved. Use
2 : // of this source code is governed by a BSD-style license that can be found in
3 : // the LICENSE file.
4 :
5 : //go:build linux
6 : // +build linux
7 :
8 : package vfs
9 :
10 : import (
11 : "os"
12 : "syscall"
13 :
14 : "github.com/cockroachdb/errors"
15 : "golang.org/x/sys/unix"
16 : )
17 :
18 1 : func wrapOSFileImpl(f *os.File) File {
19 1 : lf := &linuxFile{File: f, fd: f.Fd()}
20 1 : if lf.fd != InvalidFd {
21 1 : lf.useSyncRange = isSyncRangeSupported(lf.fd)
22 1 : }
23 1 : return lf
24 : }
25 :
26 1 : func (defaultFS) OpenDir(name string) (File, error) {
27 1 : f, err := os.OpenFile(name, syscall.O_CLOEXEC, 0)
28 1 : if err != nil {
29 0 : return nil, errors.WithStack(err)
30 0 : }
31 1 : return &linuxDir{f}, nil
32 : }
33 :
34 : // Assert that linuxFile and linuxDir implement vfs.File.
35 : var (
36 : _ File = (*linuxDir)(nil)
37 : _ File = (*linuxFile)(nil)
38 : )
39 :
40 : type linuxDir struct {
41 : *os.File
42 : }
43 :
44 0 : func (d *linuxDir) Prefetch(offset int64, length int64) error { return nil }
45 0 : func (d *linuxDir) Preallocate(offset, length int64) error { return nil }
46 0 : func (d *linuxDir) SyncData() error { return d.Sync() }
47 0 : func (d *linuxDir) SyncTo(offset int64) (fullSync bool, err error) { return false, nil }
48 :
49 : type linuxFile struct {
50 : *os.File
51 : fd uintptr
52 : useSyncRange bool
53 : }
54 :
55 1 : func (f *linuxFile) Prefetch(offset int64, length int64) error {
56 1 : _, _, err := unix.Syscall(unix.SYS_READAHEAD, uintptr(f.fd), uintptr(offset), uintptr(length))
57 1 : return err
58 1 : }
59 :
60 1 : func (f *linuxFile) Preallocate(offset, length int64) error {
61 1 : return unix.Fallocate(int(f.fd), unix.FALLOC_FL_KEEP_SIZE, offset, length)
62 1 : }
63 :
64 1 : func (f *linuxFile) SyncData() error {
65 1 : return unix.Fdatasync(int(f.fd))
66 1 : }
67 :
68 0 : func (f *linuxFile) SyncTo(offset int64) (fullSync bool, err error) {
69 0 : if !f.useSyncRange {
70 0 : // Use fdatasync, which does provide persistence guarantees but won't
71 0 : // update all file metadata. From the `fdatasync` man page:
72 0 : //
73 0 : // fdatasync() is similar to fsync(), but does not flush modified
74 0 : // metadata unless that metadata is needed in order to allow a
75 0 : // subsequent data retrieval to be correctly handled. For example,
76 0 : // changes to st_atime or st_mtime (respectively, time of last access
77 0 : // and time of last modification; see stat(2)) do not require flushing
78 0 : // because they are not necessary for a subsequent data read to be
79 0 : // handled correctly. On the other hand, a change to the file size
80 0 : // (st_size, as made by say ftruncate(2)), would require a metadata
81 0 : // flush.
82 0 : if err = unix.Fdatasync(int(f.fd)); err != nil {
83 0 : return false, err
84 0 : }
85 0 : return true, nil
86 : }
87 :
88 0 : const (
89 0 : waitBefore = 0x1
90 0 : write = 0x2
91 0 : // waitAfter = 0x4
92 0 : )
93 0 :
94 0 : // By specifying write|waitBefore for the flags, we're instructing
95 0 : // SyncFileRange to a) wait for any outstanding data being written to finish,
96 0 : // and b) to queue any other dirty data blocks in the range [0,offset] for
97 0 : // writing. The actual writing of this data will occur asynchronously. The
98 0 : // use of `waitBefore` is to limit how much dirty data is allowed to
99 0 : // accumulate. Linux sometimes behaves poorly when a large amount of dirty
100 0 : // data accumulates, impacting other I/O operations.
101 0 : return false, unix.SyncFileRange(int(f.fd), 0, offset, write|waitBefore)
102 : }
103 :
104 : type syncFileRange func(fd int, off int64, n int64, flags int) (err error)
105 :
106 : // sync_file_range depends on both the filesystem, and the broader kernel
107 : // support. In particular, Windows Subsystem for Linux does not support
108 : // sync_file_range, even when used with ext{2,3,4}. syncRangeSmokeTest performs
109 : // a test of of sync_file_range, returning false on ENOSYS, and true otherwise.
110 1 : func syncRangeSmokeTest(fd uintptr, syncFn syncFileRange) bool {
111 1 : err := syncFn(int(fd), 0 /* offset */, 0 /* nbytes */, 0 /* flags */)
112 1 : return err != unix.ENOSYS
113 1 : }
114 :
115 1 : func isSyncRangeSupported(fd uintptr) bool {
116 1 : var stat unix.Statfs_t
117 1 : if err := unix.Fstatfs(int(fd), &stat); err != nil {
118 0 : return false
119 0 : }
120 :
121 : // Allowlist which filesystems we allow using sync_file_range with as some
122 : // filesystems treat that syscall as a noop (notably ZFS). A allowlist is
123 : // used instead of a denylist in order to have a more graceful failure mode
124 : // in case a filesystem we haven't tested is encountered. Currently only
125 : // ext2/3/4 are known to work properly.
126 1 : const extMagic = 0xef53
127 1 : switch stat.Type {
128 1 : case extMagic:
129 1 : return syncRangeSmokeTest(fd, unix.SyncFileRange)
130 : }
131 0 : return false
132 : }
|