[master] 8643ead83 Simplify VFIL_allocate() by using posix_fallocate(2) when insisting (=VSHM)

Poul-Henning Kamp phk at FreeBSD.org
Tue Apr 30 20:22:07 UTC 2024


commit 8643ead83fe32b0027992e1254dac3f09c059f6a
Author: Poul-Henning Kamp <phk at FreeBSD.org>
Date:   Tue Apr 30 20:20:54 2024 +0000

    Simplify VFIL_allocate() by using posix_fallocate(2) when insisting (=VSHM)

diff --git a/lib/libvarnish/vfil.c b/lib/libvarnish/vfil.c
index 8d1f3ab60..4df3999d0 100644
--- a/lib/libvarnish/vfil.c
+++ b/lib/libvarnish/vfil.c
@@ -230,11 +230,6 @@ VFIL_allocate(int fd, uintmax_t size, int insist)
 {
 	struct stat st;
 	uintmax_t fsspace;
-	size_t l;
-	ssize_t l2, l3;
-	char *buf;
-	ssize_t bufsiz;
-	int retval = 0;
 
 	if (ftruncate(fd, size))
 		return (-1);
@@ -248,6 +243,10 @@ VFIL_allocate(int fd, uintmax_t size, int insist)
 		errno = ENOSPC;
 		return (-1);
 	}
+	if (insist) {
+		/* This falls back to writing zeros, as we want */
+		return (posix_fallocate(fd, 0, size) ? -1 : 0);
+	}
 #if defined(__linux__) && defined(HAVE_FALLOCATE)
 	{
 		/* fallocate will for some filesystems (e.g. xfs) not take
@@ -267,31 +266,7 @@ VFIL_allocate(int fd, uintmax_t size, int insist)
 		}
 	}
 #endif
-	if (!insist)
-		return (0);
-
-	/* Write size zero bytes to make sure the entire file is allocated
-	   in the file system */
-	if (size > 65536)
-		bufsiz = 64 * 1024;
-	else
-		bufsiz = size;
-	buf = calloc(1, bufsiz);
-	AN(buf);
-	assert(lseek(fd, 0, SEEK_SET) == 0);
-	for (l = 0; l < size; l += l2) {
-		l2 = bufsiz;
-		if (l + l2 > size)
-			l2 = size - l;
-		l3 = write(fd, buf, l2);
-		if (l3 != l2) {
-			retval = -1;
-			break;
-		}
-	}
-	assert(lseek(fd, 0, SEEK_SET) == 0);
-	free(buf);
-	return (retval);
+	return (0);
 }
 
 struct vfil_dir {


More information about the varnish-commit mailing list