From ce3f3e2ae73dcc0c270bc73a59ea5e7be6cf6a8d Mon Sep 17 00:00:00 2001 From: Tobias Klauser Date: Wed, 6 Aug 2025 17:50:14 +0200 Subject: cmd/link/internal/ld, internal/syscall/unix: use posix_fallocate on netbsd The posix_fallocate system call is available since NetBSD 7.0, see https://man.netbsd.org/posix_fallocate.2 Re-use the syscall wrappers already in place for freebsd. Note that posix_fallocate on netbsd also returns the result in r1 rather than in errno: > If successful, posix_fallocate() returns zero. It returns an error on failure, without > setting errno. Source: https://man.netbsd.org/posix_fallocate.2#RETURN%20VALUES Cq-Include-Trybots: luci.golang.try:gotip-netbsd-arm64 Change-Id: Iaa1f6a805d511645da7f1d2737235bfd42da3407 Reviewed-on: https://go-review.googlesource.com/c/go/+/480475 Reviewed-by: Cherry Mui Reviewed-by: Benny Siegert Auto-Submit: Tobias Klauser LUCI-TryBot-Result: Go LUCI --- src/cmd/link/internal/ld/fallocate_test.go | 2 +- src/cmd/link/internal/ld/outbuf_bsd.go | 21 ++++++++++++++++++ src/cmd/link/internal/ld/outbuf_freebsd.go | 21 ------------------ src/cmd/link/internal/ld/outbuf_mmap.go | 2 +- src/cmd/link/internal/ld/outbuf_nofallocate.go | 2 +- src/internal/syscall/unix/at_sysnum_netbsd.go | 21 +++++++++--------- src/internal/syscall/unix/fallocate_bsd_386.go | 20 +++++++++++++++++ src/internal/syscall/unix/fallocate_bsd_64bit.go | 20 +++++++++++++++++ src/internal/syscall/unix/fallocate_bsd_arm.go | 25 ++++++++++++++++++++++ src/internal/syscall/unix/fallocate_freebsd_386.go | 17 --------------- .../syscall/unix/fallocate_freebsd_64bit.go | 19 ---------------- src/internal/syscall/unix/fallocate_freebsd_arm.go | 22 ------------------- 12 files changed, 100 insertions(+), 92 deletions(-) create mode 100644 src/cmd/link/internal/ld/outbuf_bsd.go delete mode 100644 src/cmd/link/internal/ld/outbuf_freebsd.go create mode 100644 src/internal/syscall/unix/fallocate_bsd_386.go create mode 100644 src/internal/syscall/unix/fallocate_bsd_64bit.go create mode 100644 src/internal/syscall/unix/fallocate_bsd_arm.go delete mode 100644 src/internal/syscall/unix/fallocate_freebsd_386.go delete mode 100644 src/internal/syscall/unix/fallocate_freebsd_64bit.go delete mode 100644 src/internal/syscall/unix/fallocate_freebsd_arm.go (limited to 'src') diff --git a/src/cmd/link/internal/ld/fallocate_test.go b/src/cmd/link/internal/ld/fallocate_test.go index d95fec788a..163ffc26e8 100644 --- a/src/cmd/link/internal/ld/fallocate_test.go +++ b/src/cmd/link/internal/ld/fallocate_test.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -//go:build darwin || (freebsd && go1.21) || linux +//go:build darwin || (freebsd && go1.21) || linux || (netbsd && go1.25) package ld diff --git a/src/cmd/link/internal/ld/outbuf_bsd.go b/src/cmd/link/internal/ld/outbuf_bsd.go new file mode 100644 index 0000000000..5dce83fefd --- /dev/null +++ b/src/cmd/link/internal/ld/outbuf_bsd.go @@ -0,0 +1,21 @@ +// Copyright 2023 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +//go:build (freebsd && go1.21) || (netbsd && go1.25) + +package ld + +import ( + "internal/syscall/unix" + "syscall" +) + +func (out *OutBuf) fallocate(size uint64) error { + err := unix.PosixFallocate(int(out.f.Fd()), 0, int64(size)) + // ZFS on FreeBSD does not support posix_fallocate and returns EINVAL in that case. + if err == syscall.EINVAL { + return errNoFallocate + } + return err +} diff --git a/src/cmd/link/internal/ld/outbuf_freebsd.go b/src/cmd/link/internal/ld/outbuf_freebsd.go deleted file mode 100644 index 7e718c1408..0000000000 --- a/src/cmd/link/internal/ld/outbuf_freebsd.go +++ /dev/null @@ -1,21 +0,0 @@ -// Copyright 2023 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -//go:build freebsd && go1.21 - -package ld - -import ( - "internal/syscall/unix" - "syscall" -) - -func (out *OutBuf) fallocate(size uint64) error { - err := unix.PosixFallocate(int(out.f.Fd()), 0, int64(size)) - // ZFS on FreeBSD does not support posix_fallocate and returns EINVAL in that case. - if err == syscall.EINVAL { - return errNoFallocate - } - return err -} diff --git a/src/cmd/link/internal/ld/outbuf_mmap.go b/src/cmd/link/internal/ld/outbuf_mmap.go index b8b8dc5158..e92a06dcb2 100644 --- a/src/cmd/link/internal/ld/outbuf_mmap.go +++ b/src/cmd/link/internal/ld/outbuf_mmap.go @@ -28,7 +28,7 @@ func (out *OutBuf) Mmap(filesize uint64) (err error) { // Some file systems do not support fallocate. We ignore that error as linking // can still take place, but you might SIGBUS when you write to the mmapped // area. - if err != syscall.ENOTSUP && err != syscall.EPERM && err != errNoFallocate { + if err != syscall.ENOTSUP && err != syscall.EOPNOTSUPP && err != syscall.EPERM && err != errNoFallocate { return err } } diff --git a/src/cmd/link/internal/ld/outbuf_nofallocate.go b/src/cmd/link/internal/ld/outbuf_nofallocate.go index 435be5e09f..9169379e23 100644 --- a/src/cmd/link/internal/ld/outbuf_nofallocate.go +++ b/src/cmd/link/internal/ld/outbuf_nofallocate.go @@ -2,7 +2,7 @@ // Use of this source code is governed by a BSD-style // license that can be found in the LICENSE file. -//go:build !darwin && !(freebsd && go1.21) && !linux +//go:build !darwin && !(freebsd && go1.21) && !linux && !(netbsd && go1.25) package ld diff --git a/src/internal/syscall/unix/at_sysnum_netbsd.go b/src/internal/syscall/unix/at_sysnum_netbsd.go index b59b5e0cf9..db17852b74 100644 --- a/src/internal/syscall/unix/at_sysnum_netbsd.go +++ b/src/internal/syscall/unix/at_sysnum_netbsd.go @@ -7,16 +7,17 @@ package unix import "syscall" const ( - unlinkatTrap uintptr = syscall.SYS_UNLINKAT - openatTrap uintptr = syscall.SYS_OPENAT - fstatatTrap uintptr = syscall.SYS_FSTATAT - readlinkatTrap uintptr = syscall.SYS_READLINKAT - mkdiratTrap uintptr = syscall.SYS_MKDIRAT - fchmodatTrap uintptr = syscall.SYS_FCHMODAT - fchownatTrap uintptr = syscall.SYS_FCHOWNAT - renameatTrap uintptr = syscall.SYS_RENAMEAT - linkatTrap uintptr = syscall.SYS_LINKAT - symlinkatTrap uintptr = syscall.SYS_SYMLINKAT + unlinkatTrap uintptr = syscall.SYS_UNLINKAT + openatTrap uintptr = syscall.SYS_OPENAT + fstatatTrap uintptr = syscall.SYS_FSTATAT + readlinkatTrap uintptr = syscall.SYS_READLINKAT + mkdiratTrap uintptr = syscall.SYS_MKDIRAT + fchmodatTrap uintptr = syscall.SYS_FCHMODAT + fchownatTrap uintptr = syscall.SYS_FCHOWNAT + renameatTrap uintptr = syscall.SYS_RENAMEAT + linkatTrap uintptr = syscall.SYS_LINKAT + symlinkatTrap uintptr = syscall.SYS_SYMLINKAT + posixFallocateTrap uintptr = 479 ) const ( diff --git a/src/internal/syscall/unix/fallocate_bsd_386.go b/src/internal/syscall/unix/fallocate_bsd_386.go new file mode 100644 index 0000000000..1dcdff4a53 --- /dev/null +++ b/src/internal/syscall/unix/fallocate_bsd_386.go @@ -0,0 +1,20 @@ +// Copyright 2023 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +//go:build (freebsd || netbsd) && 386 + +package unix + +import "syscall" + +func PosixFallocate(fd int, off int64, size int64) error { + // If successful, posix_fallocate() returns zero. It returns an error on failure, without + // setting errno. See https://man.freebsd.org/cgi/man.cgi?query=posix_fallocate&sektion=2&n=1 + // and https://man.netbsd.org/posix_fallocate.2#RETURN%20VALUES + r1, _, _ := syscall.Syscall6(posixFallocateTrap, uintptr(fd), uintptr(off), uintptr(off>>32), uintptr(size), uintptr(size>>32), 0) + if r1 != 0 { + return syscall.Errno(r1) + } + return nil +} diff --git a/src/internal/syscall/unix/fallocate_bsd_64bit.go b/src/internal/syscall/unix/fallocate_bsd_64bit.go new file mode 100644 index 0000000000..177bb48382 --- /dev/null +++ b/src/internal/syscall/unix/fallocate_bsd_64bit.go @@ -0,0 +1,20 @@ +// Copyright 2023 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +//go:build (freebsd || netbsd) && (amd64 || arm64 || riscv64) + +package unix + +import "syscall" + +func PosixFallocate(fd int, off int64, size int64) error { + // If successful, posix_fallocate() returns zero. It returns an error on failure, without + // setting errno. See https://man.freebsd.org/cgi/man.cgi?query=posix_fallocate&sektion=2&n=1 + // and https://man.netbsd.org/posix_fallocate.2#RETURN%20VALUES + r1, _, _ := syscall.Syscall(posixFallocateTrap, uintptr(fd), uintptr(off), uintptr(size)) + if r1 != 0 { + return syscall.Errno(r1) + } + return nil +} diff --git a/src/internal/syscall/unix/fallocate_bsd_arm.go b/src/internal/syscall/unix/fallocate_bsd_arm.go new file mode 100644 index 0000000000..15e99d02b1 --- /dev/null +++ b/src/internal/syscall/unix/fallocate_bsd_arm.go @@ -0,0 +1,25 @@ +// Copyright 2023 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +//go:build (freebsd || netbsd) && arm + +package unix + +import "syscall" + +func PosixFallocate(fd int, off int64, size int64) error { + // If successful, posix_fallocate() returns zero. It returns an error on failure, without + // setting errno. See https://man.freebsd.org/cgi/man.cgi?query=posix_fallocate&sektion=2&n=1 + // and https://man.netbsd.org/posix_fallocate.2#RETURN%20VALUES + // + // The padding 0 argument is needed because the ARM calling convention requires that if an + // argument (off in this case) needs double-word alignment (8-byte), the NCRN (next core + // register number) is rounded up to the next even register number. + // See https://github.com/ARM-software/abi-aa/blob/2bcab1e3b22d55170c563c3c7940134089176746/aapcs32/aapcs32.rst#parameter-passing + r1, _, _ := syscall.Syscall6(posixFallocateTrap, uintptr(fd), 0, uintptr(off), uintptr(off>>32), uintptr(size), uintptr(size>>32)) + if r1 != 0 { + return syscall.Errno(r1) + } + return nil +} diff --git a/src/internal/syscall/unix/fallocate_freebsd_386.go b/src/internal/syscall/unix/fallocate_freebsd_386.go deleted file mode 100644 index 535b23dbc5..0000000000 --- a/src/internal/syscall/unix/fallocate_freebsd_386.go +++ /dev/null @@ -1,17 +0,0 @@ -// Copyright 2023 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package unix - -import "syscall" - -func PosixFallocate(fd int, off int64, size int64) error { - // If successful, posix_fallocate() returns zero. It returns an error on failure, without - // setting errno. See https://man.freebsd.org/cgi/man.cgi?query=posix_fallocate&sektion=2&n=1 - r1, _, _ := syscall.Syscall6(posixFallocateTrap, uintptr(fd), uintptr(off), uintptr(off>>32), uintptr(size), uintptr(size>>32), 0) - if r1 != 0 { - return syscall.Errno(r1) - } - return nil -} diff --git a/src/internal/syscall/unix/fallocate_freebsd_64bit.go b/src/internal/syscall/unix/fallocate_freebsd_64bit.go deleted file mode 100644 index a9d52283f0..0000000000 --- a/src/internal/syscall/unix/fallocate_freebsd_64bit.go +++ /dev/null @@ -1,19 +0,0 @@ -// Copyright 2023 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -//go:build freebsd && (amd64 || arm64 || riscv64) - -package unix - -import "syscall" - -func PosixFallocate(fd int, off int64, size int64) error { - // If successful, posix_fallocate() returns zero. It returns an error on failure, without - // setting errno. See https://man.freebsd.org/cgi/man.cgi?query=posix_fallocate&sektion=2&n=1 - r1, _, _ := syscall.Syscall(posixFallocateTrap, uintptr(fd), uintptr(off), uintptr(size)) - if r1 != 0 { - return syscall.Errno(r1) - } - return nil -} diff --git a/src/internal/syscall/unix/fallocate_freebsd_arm.go b/src/internal/syscall/unix/fallocate_freebsd_arm.go deleted file mode 100644 index 1ded50f3b9..0000000000 --- a/src/internal/syscall/unix/fallocate_freebsd_arm.go +++ /dev/null @@ -1,22 +0,0 @@ -// Copyright 2023 The Go Authors. All rights reserved. -// Use of this source code is governed by a BSD-style -// license that can be found in the LICENSE file. - -package unix - -import "syscall" - -func PosixFallocate(fd int, off int64, size int64) error { - // If successful, posix_fallocate() returns zero. It returns an error on failure, without - // setting errno. See https://man.freebsd.org/cgi/man.cgi?query=posix_fallocate&sektion=2&n=1 - // - // The padding 0 argument is needed because the ARM calling convention requires that if an - // argument (off in this case) needs double-word alignment (8-byte), the NCRN (next core - // register number) is rounded up to the next even register number. - // See https://github.com/ARM-software/abi-aa/blob/2bcab1e3b22d55170c563c3c7940134089176746/aapcs32/aapcs32.rst#parameter-passing - r1, _, _ := syscall.Syscall6(posixFallocateTrap, uintptr(fd), 0, uintptr(off), uintptr(off>>32), uintptr(size), uintptr(size>>32)) - if r1 != 0 { - return syscall.Errno(r1) - } - return nil -} -- cgit v1.3