2017-12-06 21:07:24 +01:00
|
|
|
package syscallcompat
|
|
|
|
|
|
|
|
import (
|
2017-12-07 00:05:28 +01:00
|
|
|
"syscall"
|
|
|
|
|
2017-12-06 21:07:24 +01:00
|
|
|
"golang.org/x/sys/unix"
|
|
|
|
)
|
|
|
|
|
2018-06-19 20:13:56 +02:00
|
|
|
// PATH_MAX is the maximum allowed path length on Linux.
|
|
|
|
// It is not defined on Darwin, so we use the Linux value.
|
|
|
|
const PATH_MAX = 4096
|
2018-06-08 00:47:48 +02:00
|
|
|
|
2018-11-04 22:01:18 +01:00
|
|
|
// Readlinkat is a convenience wrapper around unix.Readlinkat() that takes
|
|
|
|
// care of buffer sizing. Implemented like os.Readlink().
|
2017-12-06 21:07:24 +01:00
|
|
|
func Readlinkat(dirfd int, path string) (string, error) {
|
|
|
|
// Allocate the buffer exponentially like os.Readlink does.
|
|
|
|
for bufsz := 128; ; bufsz *= 2 {
|
|
|
|
buf := make([]byte, bufsz)
|
|
|
|
n, err := unix.Readlinkat(dirfd, path, buf)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
if n < bufsz {
|
|
|
|
return string(buf[0:n]), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-12-07 00:05:28 +01:00
|
|
|
|
2020-10-14 00:35:16 +02:00
|
|
|
// Openat wraps the Openat syscall.
|
|
|
|
// Retries on EINTR.
|
2019-01-14 02:32:28 +01:00
|
|
|
func Openat(dirfd int, path string, flags int, mode uint32) (fd int, err error) {
|
2021-06-08 21:25:50 +02:00
|
|
|
if flags&syscall.O_CREAT == 0 {
|
2019-01-14 02:32:28 +01:00
|
|
|
// If O_CREAT is not used, we should use O_NOFOLLOW
|
|
|
|
if flags&syscall.O_NOFOLLOW == 0 {
|
|
|
|
flags |= syscall.O_NOFOLLOW
|
|
|
|
}
|
|
|
|
}
|
2020-10-14 00:35:16 +02:00
|
|
|
fd, err = retryEINTR2(func() (int, error) {
|
|
|
|
return unix.Openat(dirfd, path, flags, mode)
|
|
|
|
})
|
|
|
|
return fd, err
|
2019-01-14 02:32:28 +01:00
|
|
|
}
|
|
|
|
|
2019-01-14 03:00:49 +01:00
|
|
|
// Fstatat syscall.
|
2020-10-14 00:35:16 +02:00
|
|
|
// Retries on EINTR.
|
2019-01-14 03:00:49 +01:00
|
|
|
func Fstatat(dirfd int, path string, stat *unix.Stat_t, flags int) (err error) {
|
|
|
|
// Why would we ever want to call this without AT_SYMLINK_NOFOLLOW?
|
|
|
|
if flags&unix.AT_SYMLINK_NOFOLLOW == 0 {
|
|
|
|
flags |= unix.AT_SYMLINK_NOFOLLOW
|
|
|
|
}
|
2020-10-14 00:35:16 +02:00
|
|
|
err = retryEINTR(func() error {
|
|
|
|
return unix.Fstatat(dirfd, path, stat, flags)
|
|
|
|
})
|
|
|
|
return err
|
2019-01-14 03:00:49 +01:00
|
|
|
}
|
|
|
|
|
2020-06-11 23:39:27 +02:00
|
|
|
// Fstatat2 is a more convenient version of Fstatat. It allocates a Stat_t
|
|
|
|
// for you and also handles the Unix2syscall conversion.
|
2020-10-14 00:35:16 +02:00
|
|
|
// Retries on EINTR.
|
2020-06-11 23:39:27 +02:00
|
|
|
func Fstatat2(dirfd int, path string, flags int) (*syscall.Stat_t, error) {
|
|
|
|
var stUnix unix.Stat_t
|
|
|
|
err := Fstatat(dirfd, path, &stUnix, flags)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
st := Unix2syscall(stUnix)
|
|
|
|
return &st, nil
|
|
|
|
}
|
|
|
|
|
2019-01-02 16:56:23 +01:00
|
|
|
const XATTR_SIZE_MAX = 65536
|
|
|
|
|
syscallcompat: add getxattr fastpaths
The allocations from Lgetxattr were #1 in the tar extract
allocation profile (caused by security.capability lookups).
No more!
$ benchstat old.txt new.txt
name old time/op new time/op delta
Lgetxattr-4 15.2µs ± 0% 1.8µs ± 0% ~ (p=1.000 n=1+1)
$ ./benchmark.bash
Testing gocryptfs at /tmp/benchmark.bash.H8p: gocryptfs v2.0-beta1-4-g95ea738-dirty; go-fuse v2.0.4-0.20200908172753-0b6cbc515082 => github.com/rfjakob/go-fuse/v2 v2.0.4-0.20201015204057-88b12c99f8af; 2020-10-18 go1.15.3 linux/amd64
/tmp/benchmark.bash.H8p.mnt is a mountpoint
WRITE: 262144000 bytes (262 MB, 250 MiB) copied, 0,520109 s, 504 MB/s
READ: 262144000 bytes (262 MB, 250 MiB) copied, 0,255672 s, 1,0 GB/s
UNTAR: 30,238
MD5: 12,721
LS: 10,038
RM: 16,536
2020-10-18 00:25:42 +02:00
|
|
|
// Make the buffer 1kB bigger so we can detect overflows. Unfortunately,
|
|
|
|
// slices larger than 64kB are always allocated on the heap.
|
2019-01-02 16:56:23 +01:00
|
|
|
const XATTR_BUFSZ = XATTR_SIZE_MAX + 1024
|
|
|
|
|
syscallcompat: add getxattr fastpaths
The allocations from Lgetxattr were #1 in the tar extract
allocation profile (caused by security.capability lookups).
No more!
$ benchstat old.txt new.txt
name old time/op new time/op delta
Lgetxattr-4 15.2µs ± 0% 1.8µs ± 0% ~ (p=1.000 n=1+1)
$ ./benchmark.bash
Testing gocryptfs at /tmp/benchmark.bash.H8p: gocryptfs v2.0-beta1-4-g95ea738-dirty; go-fuse v2.0.4-0.20200908172753-0b6cbc515082 => github.com/rfjakob/go-fuse/v2 v2.0.4-0.20201015204057-88b12c99f8af; 2020-10-18 go1.15.3 linux/amd64
/tmp/benchmark.bash.H8p.mnt is a mountpoint
WRITE: 262144000 bytes (262 MB, 250 MiB) copied, 0,520109 s, 504 MB/s
READ: 262144000 bytes (262 MB, 250 MiB) copied, 0,255672 s, 1,0 GB/s
UNTAR: 30,238
MD5: 12,721
LS: 10,038
RM: 16,536
2020-10-18 00:25:42 +02:00
|
|
|
// We try with a small buffer first - this one can be allocated on the stack.
|
|
|
|
const XATTR_BUFSZ_SMALL = 500
|