From ed1cf6ab3ef94c603f9d883aa6806a5a82d2bee3 Mon Sep 17 00:00:00 2001
From: Tobias Klauser <tklauser@distanz.ch>
Date: Mon, 27 Mar 2023 20:52:43 +0200
Subject: [PATCH] cmd/link/internal/ld, internal/syscall/unix: use
 posix_fallocate on freebsd

The posix_fallocate system call is available since FreeBSD 9.0, see
https://man.freebsd.org/cgi/man.cgi?query=posix_fallocate

Change-Id: Ie65e0a44341909707617d3b0d9a4f1710c45b935
Reviewed-on: https://go-review.googlesource.com/c/go/+/478035
TryBot-Result: Gopher Robot <gobot@golang.org>
Auto-Submit: Tobias Klauser <tobias.klauser@gmail.com>
Run-TryBot: Tobias Klauser <tobias.klauser@gmail.com>
Reviewed-by: Cherry Mui <cherryyz@google.com>
Reviewed-by: Than McIntosh <thanm@google.com>
---
 src/cmd/dist/buildtool.go                      |  3 ++-
 src/cmd/link/internal/ld/fallocate_test.go     |  3 +--
 src/cmd/link/internal/ld/outbuf_freebsd.go     | 13 +++++++++++++
 src/cmd/link/internal/ld/outbuf_nofallocate.go |  2 +-
 src/cmd/link/internal/ld/outbuf_notdarwin.go   |  1 -
 src/internal/syscall/unix/fallocate_freebsd.go | 18 ++++++++++++++++++
 6 files changed, 35 insertions(+), 5 deletions(-)
 create mode 100644 src/cmd/link/internal/ld/outbuf_freebsd.go
 create mode 100644 src/internal/syscall/unix/fallocate_freebsd.go

diff --git a/src/cmd/dist/buildtool.go b/src/cmd/dist/buildtool.go
index 1eeb32afd3b..f2228df33de 100644
--- a/src/cmd/dist/buildtool.go
+++ b/src/cmd/dist/buildtool.go
@@ -68,10 +68,11 @@ var bootstrapDirs = []string{
 	"internal/goroot",
 	"internal/goversion",
 	"internal/pkgbits",
+	"internal/platform",
 	"internal/profile",
 	"internal/race",
 	"internal/saferio",
-	"internal/platform",
+	"internal/syscall/unix",
 	"internal/types/errors",
 	"internal/unsafeheader",
 	"internal/xcoff",
diff --git a/src/cmd/link/internal/ld/fallocate_test.go b/src/cmd/link/internal/ld/fallocate_test.go
index 1ed0eb2ca74..ad77be536fc 100644
--- a/src/cmd/link/internal/ld/fallocate_test.go
+++ b/src/cmd/link/internal/ld/fallocate_test.go
@@ -2,8 +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 || linux
-// +build darwin linux
+//go:build darwin || (freebsd && go1.21) || linux
 
 package ld
 
diff --git a/src/cmd/link/internal/ld/outbuf_freebsd.go b/src/cmd/link/internal/ld/outbuf_freebsd.go
new file mode 100644
index 00000000000..5ff17300c19
--- /dev/null
+++ b/src/cmd/link/internal/ld/outbuf_freebsd.go
@@ -0,0 +1,13 @@
+// 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"
+
+func (out *OutBuf) fallocate(size uint64) error {
+	return unix.PosixFallocate(int(out.f.Fd()), 0, int64(size))
+}
diff --git a/src/cmd/link/internal/ld/outbuf_nofallocate.go b/src/cmd/link/internal/ld/outbuf_nofallocate.go
index dd5afc61db9..435be5e09fe 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 && !linux
+//go:build !darwin && !(freebsd && go1.21) && !linux
 
 package ld
 
diff --git a/src/cmd/link/internal/ld/outbuf_notdarwin.go b/src/cmd/link/internal/ld/outbuf_notdarwin.go
index f9caa413e3c..3e5c67a5c25 100644
--- a/src/cmd/link/internal/ld/outbuf_notdarwin.go
+++ b/src/cmd/link/internal/ld/outbuf_notdarwin.go
@@ -3,7 +3,6 @@
 // license that can be found in the LICENSE file.
 
 //go:build !darwin
-// +build !darwin
 
 package ld
 
diff --git a/src/internal/syscall/unix/fallocate_freebsd.go b/src/internal/syscall/unix/fallocate_freebsd.go
new file mode 100644
index 00000000000..6c3e80118b0
--- /dev/null
+++ b/src/internal/syscall/unix/fallocate_freebsd.go
@@ -0,0 +1,18 @@
+// 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"
+
+// FreeBSD posix_fallocate system call number.
+const posixFallocateTrap uintptr = 530
+
+func PosixFallocate(fd int, off int64, size int64) error {
+	_, _, errno := syscall.Syscall(posixFallocateTrap, uintptr(fd), uintptr(off), uintptr(size))
+	if errno != 0 {
+		return errno
+	}
+	return nil
+}
-- 
GitLab