Skip to contentSkip to conversation

generic: fallocate two bytes at block boundary

PATCH Superseded Max Reitz 2 participants 4 messages Sep 26, 2019

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore

Max Reitz

author
#0 lore raw

PATCH 1/1 generic: fallocate two bytes at block boundary

Allocating two bytes at a block boundary with fallocate should allocate both blocks involved. Test this by writing both bytes with dd afterwards and see whether the on-disk size increases (it should not).

---
 tests/generic/568     | 63 +++++++++++++++++++++++++++++++++++++++++++
 tests/generic/568.out |  2 ++
 tests/generic/group   |  1 +
 3 files changed, 66 insertions(+)
 create mode 100755 tests/generic/568
 create mode 100644 tests/generic/568.out
tests/generic/568 new file +63 -0
new file mode 100755
index 00000000..8fbdcda0
@@ -0,0 +1,63 @@
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (c) 2019 Red Hat, Inc.  All Rights Reserved.
+#
+# FS QA Test No. generic/568
+#
+# Test that fallocating an unaligned range allocates all blocks
+# touched by that range
+#
+seq=$(basename $0)
+seqres="$RESULT_DIR/$seq"
+echo "QA output created by $seq"
+
+here=$PWD
+tmp=/tmp/$$
+status=1	# failure is the default!
+trap "_cleanup; exit \$status" 0 1 2 3 15
+
+_cleanup()
+{
+	cd /
+	rm -f "$tmp".*
+}
+
+# get standard environment, filters and checks
+. ./common/rc
+. ./common/filter
+
+# real QA test starts here
+_supported_fs generic
+_supported_os Linux
+_require_scratch
+
+testfile="$SCRATCH_MNT/testfile"
+
+_scratch_mkfs > /dev/null 2>&1
+_scratch_mount
+
+# Fallocate 2 bytes across a block boundary
+block_size=$(stat -fc '%S' "$SCRATCH_MNT")
+fallocate -o $((block_size - 1)) -l 2 "$testfile"
+
+# Both the first blocks should be allocated now.  Check that by
+# inquiring whether the file grows when we write to the two bytes we
+# have just fallocated.
+
+allocated_size_before=$(($(stat -c '%b * %B' "$testfile")))
+
+dd if=/dev/zero of="$testfile" bs=1 conv=notrunc \
+    seek=$((block_size - 1)) count=2 \
+    2>&1 | _filter_dd
+
+allocated_size_after=$(($(stat -c '%b * %B' "$testfile")))
+
+if [ $allocated_size_after -gt $allocated_size_before ]; then
+	echo "ERROR: File grew from ${allocated_size_before} B to" \
+	     "${allocated_size_after} when writing to the fallocated range."
+else
+	echo "OK: File did not grow."
+fi
+
+status=0
+exit
tests/generic/568.out new file +2 -0
new file mode 100644
index 00000000..d8210882
@@ -0,0 +1,2 @@
+QA output created by 568
+OK: File did not grow.
tests/generic/group +1 -0
index 7cf4f6c4..24ab29bc 100644
@@ -570,3 +570,4 @@
 565 auto quick copy_range
 566 auto quick quota metadata
 567 auto quick rw punch
+568 auto quick rw
Signature · 1 line
2.21.0
#1 lore raw
Quoted 49 lines · Max Reitz

Allocating two bytes at a block boundary with fallocate should allocate both blocks involved. Test this by writing both bytes with dd afterwards and see whether the on-disk size increases (it should not).

---
 tests/generic/568     | 63 +++++++++++++++++++++++++++++++++++++++++++
 tests/generic/568.out |  2 ++
 tests/generic/group   |  1 +
 3 files changed, 66 insertions(+)
 create mode 100755 tests/generic/568
 create mode 100644 tests/generic/568.out
tests/generic/568 new file +30 -0
new file mode 100755
index 00000000..8fbdcda0
@@ -0,0 +1,63 @@
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (c) 2019 Red Hat, Inc.  All Rights Reserved.
+#
+# FS QA Test No. generic/568
+#
+# Test that fallocating an unaligned range allocates all blocks
+# touched by that range
+#
+seq=$(basename $0)
+seqres="$RESULT_DIR/$seq"
+echo "QA output created by $seq"
+
+here=$PWD
+tmp=/tmp/$$
+status=1	# failure is the default!
+trap "_cleanup; exit \$status" 0 1 2 3 15
+
+_cleanup()
+{
+	cd /
+	rm -f "$tmp".*
+}
+
+# get standard environment, filters and checks
+. ./common/rc
+. ./common/filter
+
+# real QA test starts here
+_supported_fs generic

tests/generic/568 line 40

Quoted 10 lines
tests/generic/568 new file +10 -0
+_supported_os Linux
+_require_scratch
+
+testfile="$SCRATCH_MNT/testfile"
+
+_scratch_mkfs > /dev/null 2>&1
+_scratch_mount
+
+# Fallocate 2 bytes across a block boundary
+block_size=$(stat -fc '%S' "$SCRATCH_MNT")

block_size=$(_get_file_block_size $SCRATCH_MNT)

tests/generic/568 line 41

Quoted 1 line
tests/generic/568 new file +1 -0
+fallocate -o $((block_size - 1)) -l 2 "$testfile"

If you're going to use an external program, you need to gate the test on whether or not the program's installed, by calling _require_command.

Though probably the easier way would be to use xfs_io since fstests requires that xfsprogs be installed:

	$XFS_IO_PROG -f -c "falloc $((block_size - 1)) 2" $testfile

Though you do still have to put at the top of the test:

_require_xfs_io_command "falloc"

Because not all filesystems support fallocate.

tests/generic/568 line 51

Quoted 10 lines
tests/generic/568 new file +10 -0
+
+# Both the first blocks should be allocated now.  Check that by
+# inquiring whether the file grows when we write to the two bytes we
+# have just fallocated.
+
+allocated_size_before=$(($(stat -c '%b * %B' "$testfile")))
+
+dd if=/dev/zero of="$testfile" bs=1 conv=notrunc \
+    seek=$((block_size - 1)) count=2 \
+    2>&1 | _filter_dd

$XFS_IO_PROG -c "pwrite $((block_size - 1)) 2" $testfile

tests/generic/568 line 59

Quoted 8 lines
tests/generic/568 new file +8 -0
+
+allocated_size_after=$(($(stat -c '%b * %B' "$testfile")))
+
+if [ $allocated_size_after -gt $allocated_size_before ]; then
+	echo "ERROR: File grew from ${allocated_size_before} B to" \
+	     "${allocated_size_after} when writing to the fallocated range."
+else
+	echo "OK: File did not grow."

Other than that, the logic makes sense to me. Thanks for writing this up!

--D

Quoted 24 lines
tests/generic/568 new file +4 -0
+fi
+
+status=0
+exit
tests/generic/568.out new file +2 -0
new file mode 100644
index 00000000..d8210882
@@ -0,0 +1,2 @@
+QA output created by 568
+OK: File did not grow.
tests/generic/group +1 -0
index 7cf4f6c4..24ab29bc 100644
@@ -570,3 +570,4 @@
 565 auto quick copy_range
 566 auto quick quota metadata
 567 auto quick rw punch
+568 auto quick rw
Signature · 1 line
2.21.0
author
#2 lore raw
Quoted 54 lines · Darrick J. Wong
Quoted 53 lines · Max Reitz

Allocating two bytes at a block boundary with fallocate should allocate both blocks involved. Test this by writing both bytes with dd afterwards and see whether the on-disk size increases (it should not).

---
 tests/generic/568     | 63 +++++++++++++++++++++++++++++++++++++++++++
 tests/generic/568.out |  2 ++
 tests/generic/group   |  1 +
 3 files changed, 66 insertions(+)
 create mode 100755 tests/generic/568
 create mode 100644 tests/generic/568.out
tests/generic/568 new file +34 -0
new file mode 100755
index 00000000..8fbdcda0
@@ -0,0 +1,63 @@
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (c) 2019 Red Hat, Inc.  All Rights Reserved.
+#
+# FS QA Test No. generic/568
+#
+# Test that fallocating an unaligned range allocates all blocks
+# touched by that range
+#
+seq=$(basename $0)
+seqres="$RESULT_DIR/$seq"
+echo "QA output created by $seq"
+
+here=$PWD
+tmp=/tmp/$$
+status=1	# failure is the default!
+trap "_cleanup; exit \$status" 0 1 2 3 15
+
+_cleanup()
+{
+	cd /
+	rm -f "$tmp".*
+}
+
+# get standard environment, filters and checks
+. ./common/rc
+. ./common/filter
+
+# real QA test starts here
+_supported_fs generic
+_supported_os Linux
+_require_scratch
+
+testfile="$SCRATCH_MNT/testfile"
Quoted 8 lines

tests/generic/568 line 40

Quoted 6 lines
tests/generic/568 new file +6 -0
+
+_scratch_mkfs > /dev/null 2>&1
+_scratch_mount
+
+# Fallocate 2 bytes across a block boundary
+block_size=$(stat -fc '%S' "$SCRATCH_MNT")

block_size=$(_get_file_block_size $SCRATCH_MNT)

Ah, nice.

Quoted 4 lines

tests/generic/568 line 41

Quoted 1 line
tests/generic/568 new file +1 -0
+fallocate -o $((block_size - 1)) -l 2 "$testfile"

If you're going to use an external program, you need to gate the test on whether or not the program's installed, by calling _require_command.

OK.

Quoted 10 lines

Though probably the easier way would be to use xfs_io since fstests requires that xfsprogs be installed:

	$XFS_IO_PROG -f -c "falloc $((block_size - 1)) 2" $testfile

Though you do still have to put at the top of the test:

_require_xfs_io_command "falloc"

Because not all filesystems support fallocate.

So I suppose as long as one doesn’t use special XFS commands, xfs_io is filesystem-agnostic?

Quoted 13 lines

tests/generic/568 line 51

Quoted 10 lines
tests/generic/568 new file +10 -0
+
+# Both the first blocks should be allocated now.  Check that by
+# inquiring whether the file grows when we write to the two bytes we
+# have just fallocated.
+
+allocated_size_before=$(($(stat -c '%b * %B' "$testfile")))
+
+dd if=/dev/zero of="$testfile" bs=1 conv=notrunc \
+    seek=$((block_size - 1)) count=2 \
+    2>&1 | _filter_dd

$XFS_IO_PROG -c "pwrite $((block_size - 1)) 2" $testfile

Quoted 11 lines

tests/generic/568 line 59

Quoted 8 lines
tests/generic/568 new file +8 -0
+
+allocated_size_after=$(($(stat -c '%b * %B' "$testfile")))
+
+if [ $allocated_size_after -gt $allocated_size_before ]; then
+	echo "ERROR: File grew from ${allocated_size_before} B to" \
+	     "${allocated_size_after} when writing to the fallocated range."
+else
+	echo "OK: File did not grow."

Other than that, the logic makes sense to me. Thanks for writing this up!

OK, thanks, I’ll prepare a v2.

Max

#3 lore raw
Quoted 78 lines · Max Reitz
Quoted 54 lines · Darrick J. Wong
Quoted 53 lines · Max Reitz

Allocating two bytes at a block boundary with fallocate should allocate both blocks involved. Test this by writing both bytes with dd afterwards and see whether the on-disk size increases (it should not).

---
 tests/generic/568     | 63 +++++++++++++++++++++++++++++++++++++++++++
 tests/generic/568.out |  2 ++
 tests/generic/group   |  1 +
 3 files changed, 66 insertions(+)
 create mode 100755 tests/generic/568
 create mode 100644 tests/generic/568.out
tests/generic/568 new file +34 -0
new file mode 100755
index 00000000..8fbdcda0
@@ -0,0 +1,63 @@
+#! /bin/bash
+# SPDX-License-Identifier: GPL-2.0
+# Copyright (c) 2019 Red Hat, Inc.  All Rights Reserved.
+#
+# FS QA Test No. generic/568
+#
+# Test that fallocating an unaligned range allocates all blocks
+# touched by that range
+#
+seq=$(basename $0)
+seqres="$RESULT_DIR/$seq"
+echo "QA output created by $seq"
+
+here=$PWD
+tmp=/tmp/$$
+status=1	# failure is the default!
+trap "_cleanup; exit \$status" 0 1 2 3 15
+
+_cleanup()
+{
+	cd /
+	rm -f "$tmp".*
+}
+
+# get standard environment, filters and checks
+. ./common/rc
+. ./common/filter
+
+# real QA test starts here
+_supported_fs generic
+_supported_os Linux
+_require_scratch
+
+testfile="$SCRATCH_MNT/testfile"
Quoted 8 lines

tests/generic/568 line 40

Quoted 6 lines
tests/generic/568 new file +6 -0
+
+_scratch_mkfs > /dev/null 2>&1
+_scratch_mount
+
+# Fallocate 2 bytes across a block boundary
+block_size=$(stat -fc '%S' "$SCRATCH_MNT")

block_size=$(_get_file_block_size $SCRATCH_MNT)

Ah, nice.

Quoted 4 lines

tests/generic/568 line 41

Quoted 1 line
tests/generic/568 new file +1 -0
+fallocate -o $((block_size - 1)) -l 2 "$testfile"

If you're going to use an external program, you need to gate the test on whether or not the program's installed, by calling _require_command.

OK.

Quoted 5 lines

Though probably the easier way would be to use xfs_io since fstests requires that xfsprogs be installed:

	$XFS_IO_PROG -f -c "falloc $((block_size - 1)) 2" $testfile
Quoted 8 lines
Quoted 5 lines

Though you do still have to put at the top of the test:

_require_xfs_io_command "falloc"

Because not all filesystems support fallocate.

So I suppose as long as one doesn’t use special XFS commands, xfs_io is filesystem-agnostic?

Right. At this point xfs_io is largely a testing vehicle for fstests scripts to call various system calls or ioctls, some of which aren't even supported on XFS itself. :P

--D

Quoted 29 lines
Quoted 13 lines

tests/generic/568 line 51

Quoted 10 lines
tests/generic/568 new file +10 -0
+
+# Both the first blocks should be allocated now.  Check that by
+# inquiring whether the file grows when we write to the two bytes we
+# have just fallocated.
+
+allocated_size_before=$(($(stat -c '%b * %B' "$testfile")))
+
+dd if=/dev/zero of="$testfile" bs=1 conv=notrunc \
+    seek=$((block_size - 1)) count=2 \
+    2>&1 | _filter_dd

$XFS_IO_PROG -c "pwrite $((block_size - 1)) 2" $testfile

Quoted 11 lines

tests/generic/568 line 59

Quoted 8 lines
tests/generic/568 new file +8 -0
+
+allocated_size_after=$(($(stat -c '%b * %B' "$testfile")))
+
+if [ $allocated_size_after -gt $allocated_size_before ]; then
+	echo "ERROR: File grew from ${allocated_size_before} B to" \
+	     "${allocated_size_after} when writing to the fallocated range."
+else
+	echo "OK: File did not grow."

Other than that, the logic makes sense to me. Thanks for writing this up!

OK, thanks, I’ll prepare a v2.

Max