diff mbox series

[6/5] generic: test ftruncate zeroes bytes after EOF

Message ID 20230202204428.3267832-7-willy@infradead.org (mailing list archive)
State New, archived
Headers show
Series Fix a minor POSIX conformance problem | expand

Commit Message

Matthew Wilcox Feb. 2, 2023, 8:44 p.m. UTC
https://pubs.opengroup.org/onlinepubs/9699919799/functions/ftruncate.html
specifies that "If the file size is increased, the extended area shall
appear as if it were zero-filled."  Many filesystems do not currently
do this for the portion of the page after EOF.

Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
---
 .gitignore            |  1 +
 src/Makefile          |  2 +-
 src/truncate-zero.c   | 50 +++++++++++++++++++++++++++++++++++++++++++
 tests/generic/707     | 31 +++++++++++++++++++++++++++
 tests/generic/707.out |  2 ++
 5 files changed, 85 insertions(+), 1 deletion(-)
 create mode 100644 src/truncate-zero.c
 create mode 100755 tests/generic/707
 create mode 100644 tests/generic/707.out

Comments

Johannes Thumshirn Feb. 3, 2023, 11:57 a.m. UTC | #1
On 02.02.23 21:45, Matthew Wilcox (Oracle) wrote:
> +	fprintf(stderr, "Truncation did not zero new bytes:\n");
> +	for (i = 0; i < 5; i++)
> +		fprintf(stderr, "%#x ", buf[i]);
> +	fputc('\n', stderr);
>

[...]

> +
> +$here/src/truncate-zero $test_file > $seqres.full 2>&1 ||
> +	_fail "truncate zero failed!"
> +
Is '_fail' really needed here? truncate-zero will spit out an error message
in case the truncation doesn't work.
Matthew Wilcox Feb. 3, 2023, 1:13 p.m. UTC | #2
On Fri, Feb 03, 2023 at 11:57:11AM +0000, Johannes Thumshirn wrote:
> On 02.02.23 21:45, Matthew Wilcox (Oracle) wrote:
> > +	fprintf(stderr, "Truncation did not zero new bytes:\n");
> > +	for (i = 0; i < 5; i++)
> > +		fprintf(stderr, "%#x ", buf[i]);
> > +	fputc('\n', stderr);
> >
> 
> [...]
> 
> > +
> > +$here/src/truncate-zero $test_file > $seqres.full 2>&1 ||
> > +	_fail "truncate zero failed!"
> > +
> Is '_fail' really needed here? truncate-zero will spit out an error message
> in case the truncation doesn't work.

I don't know what I'm doing.  I totally cargo-culted 706 to make 707.
If someone wants to completely rewrite it, go ahead!
Darrick J. Wong Feb. 3, 2023, 4:35 p.m. UTC | #3
On Thu, Feb 02, 2023 at 08:44:28PM +0000, Matthew Wilcox (Oracle) wrote:
> https://pubs.opengroup.org/onlinepubs/9699919799/functions/ftruncate.html
> specifies that "If the file size is increased, the extended area shall
> appear as if it were zero-filled."  Many filesystems do not currently
> do this for the portion of the page after EOF.
> 
> Signed-off-by: Matthew Wilcox (Oracle) <willy@infradead.org>
> ---
>  .gitignore            |  1 +
>  src/Makefile          |  2 +-
>  src/truncate-zero.c   | 50 +++++++++++++++++++++++++++++++++++++++++++
>  tests/generic/707     | 31 +++++++++++++++++++++++++++
>  tests/generic/707.out |  2 ++
>  5 files changed, 85 insertions(+), 1 deletion(-)
>  create mode 100644 src/truncate-zero.c
>  create mode 100755 tests/generic/707
>  create mode 100644 tests/generic/707.out
> 
> diff --git a/.gitignore b/.gitignore
> index a6f433f1..6aa5bca9 100644
> --- a/.gitignore
> +++ b/.gitignore
> @@ -169,6 +169,7 @@ tags
>  /src/test-nextquota
>  /src/testx
>  /src/trunc
> +/src/truncate-zero
>  /src/truncfile
>  /src/unwritten_mmap
>  /src/unwritten_sync
> diff --git a/src/Makefile b/src/Makefile
> index afdf6b30..83ca11ac 100644
> --- a/src/Makefile
> +++ b/src/Makefile
> @@ -19,7 +19,7 @@ TARGETS = dirstress fill fill2 getpagesize holes lstat64 \
>  	t_ofd_locks t_mmap_collision mmap-write-concurrent \
>  	t_get_file_time t_create_short_dirs t_create_long_dirs t_enospc \
>  	t_mmap_writev_overlap checkpoint_journal mmap-rw-fault allocstale \
> -	t_mmap_cow_memory_failure fake-dump-rootino
> +	t_mmap_cow_memory_failure fake-dump-rootino truncate-zero
>  
>  LINUX_TARGETS = xfsctl bstat t_mtab getdevicesize preallo_rw_pattern_reader \
>  	preallo_rw_pattern_writer ftrunc trunc fs_perms testx looptest \
> diff --git a/src/truncate-zero.c b/src/truncate-zero.c
> new file mode 100644
> index 00000000..67f53912
> --- /dev/null
> +++ b/src/truncate-zero.c
> @@ -0,0 +1,50 @@

Needs to have a SPDX header and the customary Oracle copyright.

> +#include <fcntl.h>
> +#include <stdio.h>
> +#include <string.h>
> +#include <sys/mman.h>
> +#include <unistd.h>
> +
> +int main(int argc, char **argv)
> +{
> +	char *buf;
> +	int i, fd;
> +
> +	if (argc != 2) {
> +		fprintf(stderr, "Usage: %s <file>\n", argv[0]);
> +		return 1;
> +	}
> +
> +	fd = open(argv[1], O_RDWR | O_CREAT, 0644);
> +	if (fd < 0) {
> +		perror(argv[1]);
> +		return 1;
> +	}
> +
> +	if (ftruncate(fd, 1) < 0) {
> +		perror("ftruncate");
> +		return 1;
> +	}
> +
> +	buf = mmap(NULL, 1024, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
> +	if (buf == MAP_FAILED) {
> +		perror("mmap");
> +		return 1;
> +	}
> +
> +	memset(buf, 'a', 10);
> +
> +	if (ftruncate(fd, 5) < 0) {
> +		perror("ftruncate");
> +		return 1;
> +	}
> +
> +	if (memcmp(buf, "a\0\0\0\0", 5) == 0)
> +		return 0;
> +
> +	fprintf(stderr, "Truncation did not zero new bytes:\n");
> +	for (i = 0; i < 5; i++)
> +		fprintf(stderr, "%#x ", buf[i]);
> +	fputc('\n', stderr);
> +
> +	return 2;
> +}
> diff --git a/tests/generic/707 b/tests/generic/707
> new file mode 100755
> index 00000000..ddc82a9a
> --- /dev/null
> +++ b/tests/generic/707
> @@ -0,0 +1,31 @@
> +#! /bin/bash
> +# SPDX-License-Identifier: GPL-2.0
> +# Copyright (c) 2023 Matthew Wilcox for Oracle.  All Rights Reserved.
> +#
> +# FS QA Test 707
> +#
> +# Test whether we obey this part of POSIX-2017 ftruncate:
> +# "If the file size is increased, the extended area shall appear as if
> +# it were zero-filled"
> +#
> +. ./common/preamble
> +_begin_fstest auto quick posix
> +
> +_supported_fs generic
> +_require_test
> +_require_test_program "truncate-zero"
> +
> +test_file=$TEST_DIR/test.$seq
> +
> +_cleanup()
> +{
> +	cd /
> +	rm -f $test_file
> +}
> +
> +$here/src/truncate-zero $test_file > $seqres.full 2>&1 ||
> +	_fail "truncate zero failed!"

Omit the _fail here because any extra stdout/stderr output that is not
in the .out file suffices to record the test as failed.

_fail is only useful if you need to exit the shell script immediately.

> +
> +echo "Silence is golden"

It's customary (though not required) to put "silence is golden" before
"but my eyes still see"^W^W^W^W^Wthe test starts running programs.

Other than those minor things, this looks reasonable to me.

--D

> +status=0
> +exit
> diff --git a/tests/generic/707.out b/tests/generic/707.out
> new file mode 100644
> index 00000000..8e57a1d8
> --- /dev/null
> +++ b/tests/generic/707.out
> @@ -0,0 +1,2 @@
> +QA output created by 707
> +Silence is golden
> -- 
> 2.35.1
>
diff mbox series

Patch

diff --git a/.gitignore b/.gitignore
index a6f433f1..6aa5bca9 100644
--- a/.gitignore
+++ b/.gitignore
@@ -169,6 +169,7 @@  tags
 /src/test-nextquota
 /src/testx
 /src/trunc
+/src/truncate-zero
 /src/truncfile
 /src/unwritten_mmap
 /src/unwritten_sync
diff --git a/src/Makefile b/src/Makefile
index afdf6b30..83ca11ac 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -19,7 +19,7 @@  TARGETS = dirstress fill fill2 getpagesize holes lstat64 \
 	t_ofd_locks t_mmap_collision mmap-write-concurrent \
 	t_get_file_time t_create_short_dirs t_create_long_dirs t_enospc \
 	t_mmap_writev_overlap checkpoint_journal mmap-rw-fault allocstale \
-	t_mmap_cow_memory_failure fake-dump-rootino
+	t_mmap_cow_memory_failure fake-dump-rootino truncate-zero
 
 LINUX_TARGETS = xfsctl bstat t_mtab getdevicesize preallo_rw_pattern_reader \
 	preallo_rw_pattern_writer ftrunc trunc fs_perms testx looptest \
diff --git a/src/truncate-zero.c b/src/truncate-zero.c
new file mode 100644
index 00000000..67f53912
--- /dev/null
+++ b/src/truncate-zero.c
@@ -0,0 +1,50 @@ 
+#include <fcntl.h>
+#include <stdio.h>
+#include <string.h>
+#include <sys/mman.h>
+#include <unistd.h>
+
+int main(int argc, char **argv)
+{
+	char *buf;
+	int i, fd;
+
+	if (argc != 2) {
+		fprintf(stderr, "Usage: %s <file>\n", argv[0]);
+		return 1;
+	}
+
+	fd = open(argv[1], O_RDWR | O_CREAT, 0644);
+	if (fd < 0) {
+		perror(argv[1]);
+		return 1;
+	}
+
+	if (ftruncate(fd, 1) < 0) {
+		perror("ftruncate");
+		return 1;
+	}
+
+	buf = mmap(NULL, 1024, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
+	if (buf == MAP_FAILED) {
+		perror("mmap");
+		return 1;
+	}
+
+	memset(buf, 'a', 10);
+
+	if (ftruncate(fd, 5) < 0) {
+		perror("ftruncate");
+		return 1;
+	}
+
+	if (memcmp(buf, "a\0\0\0\0", 5) == 0)
+		return 0;
+
+	fprintf(stderr, "Truncation did not zero new bytes:\n");
+	for (i = 0; i < 5; i++)
+		fprintf(stderr, "%#x ", buf[i]);
+	fputc('\n', stderr);
+
+	return 2;
+}
diff --git a/tests/generic/707 b/tests/generic/707
new file mode 100755
index 00000000..ddc82a9a
--- /dev/null
+++ b/tests/generic/707
@@ -0,0 +1,31 @@ 
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (c) 2023 Matthew Wilcox for Oracle.  All Rights Reserved.
+#
+# FS QA Test 707
+#
+# Test whether we obey this part of POSIX-2017 ftruncate:
+# "If the file size is increased, the extended area shall appear as if
+# it were zero-filled"
+#
+. ./common/preamble
+_begin_fstest auto quick posix
+
+_supported_fs generic
+_require_test
+_require_test_program "truncate-zero"
+
+test_file=$TEST_DIR/test.$seq
+
+_cleanup()
+{
+	cd /
+	rm -f $test_file
+}
+
+$here/src/truncate-zero $test_file > $seqres.full 2>&1 ||
+	_fail "truncate zero failed!"
+
+echo "Silence is golden"
+status=0
+exit
diff --git a/tests/generic/707.out b/tests/generic/707.out
new file mode 100644
index 00000000..8e57a1d8
--- /dev/null
+++ b/tests/generic/707.out
@@ -0,0 +1,2 @@ 
+QA output created by 707
+Silence is golden