All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 0/5] xfstests: generic swap file tests
@ 2018-05-16 20:38 Omar Sandoval
  2018-05-16 20:38 ` [PATCH v2 1/5] xfstests: create swap group Omar Sandoval
                   ` (4 more replies)
  0 siblings, 5 replies; 11+ messages in thread
From: Omar Sandoval @ 2018-05-16 20:38 UTC (permalink / raw)
  To: fstests; +Cc: Darrick J . Wong, Eryu Guan, linux-btrfs, linux-xfs, kernel-team

From: Omar Sandoval <osandov@fb.com>

Changes since v1:

- Add patch 1 to create a group for swap
- Add a helper for formatting a swap file instead of open coding
  everywhere
- Use $CHATTR_PROG instead of chattr in a few places that I forgot

Thanks!

Omar Sandoval (5):
  xfstests: create swap group
  generic: enable swapfile tests on Btrfs
  generic: add test for dedupe on an active swapfile
  generic: add test for truncate/fpunch of an active swapfile
  generic: test invalid swap file activation

 .gitignore            |  2 ++
 common/rc             | 15 ++++++--
 src/Makefile          |  2 +-
 src/mkswap.c          | 83 +++++++++++++++++++++++++++++++++++++++++++
 src/swapon.c          | 24 +++++++++++++
 tests/generic/356     |  7 ++--
 tests/generic/357     |  6 ++--
 tests/generic/488     | 76 +++++++++++++++++++++++++++++++++++++++
 tests/generic/488.out |  7 ++++
 tests/generic/489     | 73 +++++++++++++++++++++++++++++++++++++
 tests/generic/489.out |  8 +++++
 tests/generic/490     | 77 +++++++++++++++++++++++++++++++++++++++
 tests/generic/490.out |  5 +++
 tests/generic/group   |  7 ++--
 tests/xfs/group       |  2 +-
 15 files changed, 381 insertions(+), 13 deletions(-)
 create mode 100644 src/mkswap.c
 create mode 100644 src/swapon.c
 create mode 100755 tests/generic/488
 create mode 100644 tests/generic/488.out
 create mode 100755 tests/generic/489
 create mode 100644 tests/generic/489.out
 create mode 100755 tests/generic/490
 create mode 100644 tests/generic/490.out

-- 
2.17.0


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH v2 1/5] xfstests: create swap group
  2018-05-16 20:38 [PATCH v2 0/5] xfstests: generic swap file tests Omar Sandoval
@ 2018-05-16 20:38 ` Omar Sandoval
  2018-05-16 20:38 ` [PATCH v2 2/5] generic: enable swapfile tests on Btrfs Omar Sandoval
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 11+ messages in thread
From: Omar Sandoval @ 2018-05-16 20:38 UTC (permalink / raw)
  To: fstests; +Cc: Darrick J . Wong, Eryu Guan, linux-btrfs, linux-xfs, kernel-team

From: Omar Sandoval <osandov@fb.com>

I'm going to add a bunch of tests for swap files, so create a group for
them and add the existing tests.

Signed-off-by: Omar Sandoval <osandov@fb.com>
---
 tests/generic/group | 4 ++--
 tests/xfs/group     | 2 +-
 2 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/tests/generic/group b/tests/generic/group
index dc637c96..cded10f8 100644
--- a/tests/generic/group
+++ b/tests/generic/group
@@ -358,8 +358,8 @@
 353 auto quick clone
 354 auto
 355 auto quick
-356 auto quick clone
-357 auto quick clone
+356 auto quick clone swap
+357 auto quick clone swap
 358 auto quick clone
 359 auto quick clone
 360 auto quick metadata
diff --git a/tests/xfs/group b/tests/xfs/group
index c2e6677e..dff8b99f 100644
--- a/tests/xfs/group
+++ b/tests/xfs/group
@@ -416,7 +416,7 @@
 416 dangerous_fuzzers dangerous_scrub dangerous_repair
 417 dangerous_fuzzers dangerous_scrub dangerous_online_repair
 418 dangerous_fuzzers dangerous_scrub dangerous_repair
-419 auto quick
+419 auto quick swap
 420 auto quick clone dedupe
 421 auto quick clone dedupe
 422 dangerous_scrub dangerous_online_repair
-- 
2.17.0


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH v2 2/5] generic: enable swapfile tests on Btrfs
  2018-05-16 20:38 [PATCH v2 0/5] xfstests: generic swap file tests Omar Sandoval
  2018-05-16 20:38 ` [PATCH v2 1/5] xfstests: create swap group Omar Sandoval
@ 2018-05-16 20:38 ` Omar Sandoval
  2018-05-22  6:41   ` Eryu Guan
  2018-05-16 20:38 ` [PATCH v2 3/5] generic: add test for dedupe on an active swapfile Omar Sandoval
                   ` (2 subsequent siblings)
  4 siblings, 1 reply; 11+ messages in thread
From: Omar Sandoval @ 2018-05-16 20:38 UTC (permalink / raw)
  To: fstests; +Cc: Darrick J . Wong, Eryu Guan, linux-btrfs, linux-xfs, kernel-team

From: Omar Sandoval <osandov@fb.com>

Commit 8c96cfbfe530 ("generic/35[67]: disable swapfile tests on Btrfs")
disabled the swapfile tests on Btrfs because it did not support
swapfiles at the time. Now that we're adding support, we want these
tests to run, but they don't. _require_scratch_swapfile always fails for
Btrfs because swapfiles on Btrfs must be set to nocow. After fixing
that, generic/356 and generic/357 fail for the same reason. After fixing
_that_, both tests still fail because we don't allow reflinking a
non-checksummed extent (which nocow implies) to a checksummed extent.
Add a helper for formatting a swap file which does the chattr, and
chattr the second file, which gets these tests running on kernels
supporting Btrfs swapfiles.

Signed-off-by: Omar Sandoval <osandov@fb.com>
---
 common/rc         | 15 ++++++++++++---
 tests/generic/356 |  7 ++++---
 tests/generic/357 |  6 +++---
 3 files changed, 19 insertions(+), 9 deletions(-)

diff --git a/common/rc b/common/rc
index ffe53236..814b8b5c 100644
--- a/common/rc
+++ b/common/rc
@@ -2222,6 +2222,17 @@ _require_odirect()
 	rm -f $testfile 2>&1 > /dev/null
 }
 
+_format_swapfile() {
+	local fname="$1"
+	local sz="$2"
+
+	touch "$fname"
+	chmod 0600 "$fname"
+	$CHATTR_PROG +C "$fname" > /dev/null 2>&1
+	_pwrite_byte 0x61 0 "$sz" "$fname" >> $seqres.full
+	mkswap -U 27376b42-ff65-42ca-919f-6c9b62292a5c "$fname" >> $seqres.full
+}
+
 # Check that the filesystem supports swapfiles
 _require_scratch_swapfile()
 {
@@ -2231,10 +2242,8 @@ _require_scratch_swapfile()
 	_scratch_mount
 
 	# Minimum size for mkswap is 10 pages
-	local size=$(($(get_page_size) * 10))
+	_format_swapfile "$SCRATCH_MNT/swap" $(($(get_page_size) * 10))
 
-	_pwrite_byte 0x61 0 "$size" "$SCRATCH_MNT/swap" >/dev/null 2>&1
-	mkswap "$SCRATCH_MNT/swap" >/dev/null 2>&1
 	if ! swapon "$SCRATCH_MNT/swap" >/dev/null 2>&1; then
 		_scratch_unmount
 		_notrun "swapfiles are not supported"
diff --git a/tests/generic/356 b/tests/generic/356
index 51eeb652..b4a38f84 100755
--- a/tests/generic/356
+++ b/tests/generic/356
@@ -59,11 +59,12 @@ blocks=160
 blksz=65536
 
 echo "Initialize file"
-echo >> $seqres.full
-_pwrite_byte 0x61 0 $((blocks * blksz)) $testdir/file1 >> $seqres.full
-mkswap -U 27376b42-ff65-42ca-919f-6c9b62292a5c $testdir/file1 >> $seqres.full
+_format_swapfile "$testdir/file1" $((blocks * blksz))
 swapon $testdir/file1
 
+touch "$testdir/file2"
+$CHATTR_PROG +C "$testdir/file2" >/dev/null 2>&1
+
 echo "Try to reflink"
 _cp_reflink $testdir/file1 $testdir/file2 2>&1 | _filter_scratch
 
diff --git a/tests/generic/357 b/tests/generic/357
index 0dd0c10f..9a83a283 100755
--- a/tests/generic/357
+++ b/tests/generic/357
@@ -59,9 +59,9 @@ blocks=160
 blksz=65536
 
 echo "Initialize file"
-echo >> $seqres.full
-_pwrite_byte 0x61 0 $((blocks * blksz)) $testdir/file1 >> $seqres.full
-mkswap -U 27376b42-ff65-42ca-919f-6c9b62292a5c $testdir/file1 >> $seqres.full
+_format_swapfile "$testdir/file1" $((blocks * blksz))
+touch "$testdir/file2"
+$CHATTR_PROG +C "$testdir/file2" >/dev/null 2>&1
 _cp_reflink $testdir/file1 $testdir/file2 2>&1 | _filter_scratch
 
 echo "Try to swapon"
-- 
2.17.0


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH v2 3/5] generic: add test for dedupe on an active swapfile
  2018-05-16 20:38 [PATCH v2 0/5] xfstests: generic swap file tests Omar Sandoval
  2018-05-16 20:38 ` [PATCH v2 1/5] xfstests: create swap group Omar Sandoval
  2018-05-16 20:38 ` [PATCH v2 2/5] generic: enable swapfile tests on Btrfs Omar Sandoval
@ 2018-05-16 20:38 ` Omar Sandoval
  2018-05-22  6:47   ` Eryu Guan
  2018-05-16 20:38 ` [PATCH v2 4/5] generic: add test for truncate/fpunch of " Omar Sandoval
  2018-05-16 20:38 ` [PATCH v2 5/5] generic: test invalid swap file activation Omar Sandoval
  4 siblings, 1 reply; 11+ messages in thread
From: Omar Sandoval @ 2018-05-16 20:38 UTC (permalink / raw)
  To: fstests; +Cc: Darrick J . Wong, Eryu Guan, linux-btrfs, linux-xfs, kernel-team

From: Omar Sandoval <osandov@fb.com>

Similar to generic/356 that makes sure we can't reflink an active
swapfile.

Signed-off-by: Omar Sandoval <osandov@fb.com>
---
 tests/generic/488     | 76 +++++++++++++++++++++++++++++++++++++++++++
 tests/generic/488.out |  7 ++++
 tests/generic/group   |  1 +
 3 files changed, 84 insertions(+)
 create mode 100755 tests/generic/488
 create mode 100644 tests/generic/488.out

diff --git a/tests/generic/488 b/tests/generic/488
new file mode 100755
index 00000000..39fc887c
--- /dev/null
+++ b/tests/generic/488
@@ -0,0 +1,76 @@
+#! /bin/bash
+# FS QA Test 488
+#
+# Check that we can't dedupe a swapfile.
+#
+#-----------------------------------------------------------------------
+# Copyright (c) 2018 Facebook.  All Rights Reserved.
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License as
+# published by the Free Software Foundation.
+#
+# This program is distributed in the hope that it would be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write the Free Software Foundation,
+# Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+#-----------------------------------------------------------------------
+#
+
+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.*
+}
+
+. ./common/rc
+. ./common/filter
+. ./common/reflink
+
+rm -f $seqres.full
+
+_supported_fs generic
+_supported_os Linux
+_require_scratch_swapfile
+_require_scratch_dedupe
+
+echo "Format and mount"
+_scratch_mkfs > $seqres.full 2>&1
+_scratch_mount >> $seqres.full 2>&1
+
+testdir="$SCRATCH_MNT/test-$seq"
+mkdir "$testdir"
+
+blocks=160
+blksz=65536
+
+echo "Initialize file"
+_format_swapfile "$testdir/file1" $((blocks * blksz))
+swapon "$testdir/file1"
+
+touch "$testdir/file2"
+$CHATTR_PROG +C "$testdir/file2" >/dev/null 2>&1
+
+echo "Try to dedupe"
+cp "$testdir/file1" "$testdir/file2"
+_dedupe_range "$testdir/file1" 0 "$testdir/file2" 0 $((blocks * blksz))
+_dedupe_range "$testdir/file2" 0 "$testdir/file1" 0 $((blocks * blksz))
+
+echo "Tear it down"
+swapoff "$testdir/file1"
+
+status=0
+exit
diff --git a/tests/generic/488.out b/tests/generic/488.out
new file mode 100644
index 00000000..e5a195d1
--- /dev/null
+++ b/tests/generic/488.out
@@ -0,0 +1,7 @@
+QA output created by 488
+Format and mount
+Initialize file
+Try to dedupe
+XFS_IOC_FILE_EXTENT_SAME: Text file busy
+XFS_IOC_FILE_EXTENT_SAME: Text file busy
+Tear it down
diff --git a/tests/generic/group b/tests/generic/group
index cded10f8..1f504ce1 100644
--- a/tests/generic/group
+++ b/tests/generic/group
@@ -490,3 +490,4 @@
 485 auto quick insert
 486 auto quick attr
 487 auto quick
+488 auto quick swap
-- 
2.17.0


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH v2 4/5] generic: add test for truncate/fpunch of an active swapfile
  2018-05-16 20:38 [PATCH v2 0/5] xfstests: generic swap file tests Omar Sandoval
                   ` (2 preceding siblings ...)
  2018-05-16 20:38 ` [PATCH v2 3/5] generic: add test for dedupe on an active swapfile Omar Sandoval
@ 2018-05-16 20:38 ` Omar Sandoval
  2018-05-16 20:38 ` [PATCH v2 5/5] generic: test invalid swap file activation Omar Sandoval
  4 siblings, 0 replies; 11+ messages in thread
From: Omar Sandoval @ 2018-05-16 20:38 UTC (permalink / raw)
  To: fstests; +Cc: Darrick J . Wong, Eryu Guan, linux-btrfs, linux-xfs, kernel-team

From: Omar Sandoval <osandov@fb.com>

These should not be allowed.

Signed-off-by: Omar Sandoval <osandov@fb.com>
---
 tests/generic/489     | 73 +++++++++++++++++++++++++++++++++++++++++++
 tests/generic/489.out |  8 +++++
 tests/generic/group   |  1 +
 3 files changed, 82 insertions(+)
 create mode 100755 tests/generic/489
 create mode 100644 tests/generic/489.out

diff --git a/tests/generic/489 b/tests/generic/489
new file mode 100755
index 00000000..dd40a81d
--- /dev/null
+++ b/tests/generic/489
@@ -0,0 +1,73 @@
+#! /bin/bash
+# FS QA Test 489
+#
+# Test truncation/hole punching of an active swapfile.
+#
+#-----------------------------------------------------------------------
+# Copyright (c) 2018 Facebook.  All Rights Reserved.
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License as
+# published by the Free Software Foundation.
+#
+# This program is distributed in the hope that it would be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write the Free Software Foundation,
+# Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+#-----------------------------------------------------------------------
+#
+
+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.*
+}
+
+. ./common/rc
+. ./common/filter
+
+rm -f $seqres.full
+
+_supported_fs generic
+_supported_os Linux
+_require_scratch_swapfile
+_require_xfs_io_command "fpunch"
+
+echo "Format and mount"
+_scratch_mkfs > $seqres.full 2>&1
+_scratch_mount >> $seqres.full 2>&1
+
+testdir="$SCRATCH_MNT/test-$seq"
+mkdir "$testdir"
+
+blocks=160
+blksz=65536
+
+echo "Initialize file"
+_format_swapfile "$testdir/file1" $((blocks * blksz))
+swapon "$testdir/file1"
+
+echo "Try to truncate"
+$XFS_IO_PROG -c "truncate $blksz" "$testdir/file1"
+
+echo "Try to punch hole"
+$XFS_IO_PROG -c "fpunch $blksz $((2 * blksz))" "$testdir/file1"
+
+echo "Tear it down"
+swapoff "$testdir/file1"
+
+status=0
+exit
diff --git a/tests/generic/489.out b/tests/generic/489.out
new file mode 100644
index 00000000..e2ebc530
--- /dev/null
+++ b/tests/generic/489.out
@@ -0,0 +1,8 @@
+QA output created by 489
+Format and mount
+Initialize file
+Try to truncate
+ftruncate: Text file busy
+Try to punch hole
+fallocate: Text file busy
+Tear it down
diff --git a/tests/generic/group b/tests/generic/group
index 1f504ce1..a5252aeb 100644
--- a/tests/generic/group
+++ b/tests/generic/group
@@ -491,3 +491,4 @@
 486 auto quick attr
 487 auto quick
 488 auto quick swap
+489 auto quick swap
-- 
2.17.0


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH v2 5/5] generic: test invalid swap file activation
  2018-05-16 20:38 [PATCH v2 0/5] xfstests: generic swap file tests Omar Sandoval
                   ` (3 preceding siblings ...)
  2018-05-16 20:38 ` [PATCH v2 4/5] generic: add test for truncate/fpunch of " Omar Sandoval
@ 2018-05-16 20:38 ` Omar Sandoval
  2018-05-18 14:37   ` Darrick J. Wong
  4 siblings, 1 reply; 11+ messages in thread
From: Omar Sandoval @ 2018-05-16 20:38 UTC (permalink / raw)
  To: fstests; +Cc: Darrick J . Wong, Eryu Guan, linux-btrfs, linux-xfs, kernel-team

From: Omar Sandoval <osandov@fb.com>

Swap files cannot have holes, and they must at least two pages.
swapon(8) and mkswap(8) have stricter restrictions, so add versions of
those commands without any restrictions.

Signed-off-by: Omar Sandoval <osandov@fb.com>
---
 .gitignore            |  2 ++
 src/Makefile          |  2 +-
 src/mkswap.c          | 83 +++++++++++++++++++++++++++++++++++++++++++
 src/swapon.c          | 24 +++++++++++++
 tests/generic/490     | 77 +++++++++++++++++++++++++++++++++++++++
 tests/generic/490.out |  5 +++
 tests/generic/group   |  1 +
 7 files changed, 193 insertions(+), 1 deletion(-)
 create mode 100644 src/mkswap.c
 create mode 100644 src/swapon.c
 create mode 100755 tests/generic/490
 create mode 100644 tests/generic/490.out

diff --git a/.gitignore b/.gitignore
index 53029e24..efc73a7c 100644
--- a/.gitignore
+++ b/.gitignore
@@ -92,6 +92,7 @@
 /src/lstat64
 /src/makeextents
 /src/metaperf
+/src/mkswap
 /src/mmapcat
 /src/multi_open_unlink
 /src/nametest
@@ -111,6 +112,7 @@
 /src/seek_sanity_test
 /src/stale_handle
 /src/stat_test
+/src/swapon
 /src/t_access_root
 /src/t_dir_offset
 /src/t_dir_offset2
diff --git a/src/Makefile b/src/Makefile
index c42d3bb1..01fe99ef 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -26,7 +26,7 @@ LINUX_TARGETS = xfsctl bstat t_mtab getdevicesize preallo_rw_pattern_reader \
 	renameat2 t_getcwd e4compact test-nextquota punch-alternating \
 	attr-list-by-handle-cursor-test listxattr dio-interleaved t_dir_type \
 	dio-invalidate-cache stat_test t_encrypted_d_revalidate \
-	attr_replace_test
+	attr_replace_test swapon mkswap
 
 SUBDIRS = log-writes perf
 
diff --git a/src/mkswap.c b/src/mkswap.c
new file mode 100644
index 00000000..d0bce2bd
--- /dev/null
+++ b/src/mkswap.c
@@ -0,0 +1,83 @@
+/* mkswap(8) without any sanity checks */
+
+#include <stdint.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+
+struct swap_header {
+	char		bootbits[1024];
+	uint32_t	version;
+	uint32_t	last_page;
+	uint32_t	nr_badpages;
+	unsigned char	sws_uuid[16];
+	unsigned char	sws_volume[16];
+	uint32_t	padding[117];
+	uint32_t	badpages[1];
+};
+
+int main(int argc, char **argv)
+{
+	struct swap_header *hdr;
+	FILE *file;
+	struct stat st;
+	long page_size;
+	int ret;
+
+	if (argc != 2) {
+		fprintf(stderr, "usage: %s PATH\n", argv[0]);
+		return EXIT_FAILURE;
+	}
+
+	page_size = sysconf(_SC_PAGESIZE);
+	if (page_size == -1) {
+		perror("sysconf");
+		return EXIT_FAILURE;
+	}
+
+	hdr = calloc(1, page_size);
+	if (!hdr) {
+		perror("calloc");
+		return EXIT_FAILURE;
+	}
+
+	file = fopen(argv[1], "r+");
+	if (!file) {
+		perror("fopen");
+		free(hdr);
+		return EXIT_FAILURE;
+	}
+
+	ret = fstat(fileno(file), &st);
+	if (ret) {
+		perror("fstat");
+		free(hdr);
+		fclose(file);
+		return EXIT_FAILURE;
+	}
+
+	hdr->version = 1;
+	hdr->last_page = st.st_size / page_size - 1;
+	memset(&hdr->sws_uuid, 0x99, sizeof(hdr->sws_uuid));
+	memcpy((char *)hdr + page_size - 10, "SWAPSPACE2", 10);
+
+	if (fwrite(hdr, page_size, 1, file) != 1) {
+		perror("fwrite");
+		free(hdr);
+		fclose(file);
+		return EXIT_FAILURE;
+	}
+
+	if (fclose(file) == EOF) {
+		perror("fwrite");
+		free(hdr);
+		return EXIT_FAILURE;
+	}
+
+	free(hdr);
+
+	return EXIT_SUCCESS;
+}
diff --git a/src/swapon.c b/src/swapon.c
new file mode 100644
index 00000000..0cb7108a
--- /dev/null
+++ b/src/swapon.c
@@ -0,0 +1,24 @@
+/* swapon(8) without any sanity checks; simply calls swapon(2) directly. */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/swap.h>
+
+int main(int argc, char **argv)
+{
+	int ret;
+
+	if (argc != 2) {
+		fprintf(stderr, "usage: %s PATH\n", argv[0]);
+		return EXIT_FAILURE;
+	}
+
+	ret = swapon(argv[1], 0);
+	if (ret) {
+		perror("swapon");
+		return EXIT_FAILURE;
+	}
+
+	return EXIT_SUCCESS;
+}
diff --git a/tests/generic/490 b/tests/generic/490
new file mode 100755
index 00000000..6ba2ecb3
--- /dev/null
+++ b/tests/generic/490
@@ -0,0 +1,77 @@
+#! /bin/bash
+# FS QA Test 490
+#
+# Test invalid swap files.
+#
+#-----------------------------------------------------------------------
+# Copyright (c) 2018 Facebook.  All Rights Reserved.
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License as
+# published by the Free Software Foundation.
+#
+# This program is distributed in the hope that it would be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write the Free Software Foundation,
+# Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+#-----------------------------------------------------------------------
+#
+
+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
+
+# remove previous $seqres.full before test
+rm -f $seqres.full
+
+_supported_fs generic
+_supported_os Linux
+_require_scratch_swapfile
+_require_test_program mkswap
+_require_test_program swapon
+
+_scratch_mkfs >> $seqres.full 2>&1
+_scratch_mount
+
+echo "File with holes"
+# We can't use _format_swapfile because we're using our custom mkswap and
+# swapon.
+touch "$SCRATCH_MNT/swap"
+$CHATTR_PROG +C "$SCRATCH_MNT/swap"
+chmod 0600 "$SCRATCH_MNT/swap"
+$XFS_IO_PROG -c "truncate $(($(get_page_size) * 10))" "$SCRATCH_MNT/swap"
+"$here/src/mkswap" "$SCRATCH_MNT/swap"
+"$here/src/swapon" "$SCRATCH_MNT/swap"
+swapoff "$SCRATCH_MNT/swap" >/dev/null 2>&1
+
+echo "Empty swap file (only swap header)"
+rm -f "$SCRATCH_MNT/swap"
+touch "$SCRATCH_MNT/swap"
+$CHATTR_PROG +C "$SCRATCH_MNT/swap"
+chmod 0600 "$SCRATCH_MNT/swap"
+_pwrite_byte 0x61 0 $(get_page_size) "$SCRATCH_MNT/swap" >> $seqres.full
+"$here/src/mkswap" "$SCRATCH_MNT/swap"
+"$here/src/swapon" "$SCRATCH_MNT/swap"
+swapoff "$SCRATCH_MNT/swap" >/dev/null 2>&1
+
+status=0
+exit
diff --git a/tests/generic/490.out b/tests/generic/490.out
new file mode 100644
index 00000000..96226c64
--- /dev/null
+++ b/tests/generic/490.out
@@ -0,0 +1,5 @@
+QA output created by 490
+File with holes
+swapon: Invalid argument
+Empty swap file (only swap header)
+swapon: Invalid argument
diff --git a/tests/generic/group b/tests/generic/group
index a5252aeb..422e21e2 100644
--- a/tests/generic/group
+++ b/tests/generic/group
@@ -492,3 +492,4 @@
 487 auto quick
 488 auto quick swap
 489 auto quick swap
+490 auto quick swap
-- 
2.17.0


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH v2 5/5] generic: test invalid swap file activation
  2018-05-16 20:38 ` [PATCH v2 5/5] generic: test invalid swap file activation Omar Sandoval
@ 2018-05-18 14:37   ` Darrick J. Wong
  2018-05-22  6:58     ` Eryu Guan
  0 siblings, 1 reply; 11+ messages in thread
From: Darrick J. Wong @ 2018-05-18 14:37 UTC (permalink / raw)
  To: Omar Sandoval; +Cc: fstests, Eryu Guan, linux-btrfs, linux-xfs, kernel-team

On Wed, May 16, 2018 at 01:38:49PM -0700, Omar Sandoval wrote:
> From: Omar Sandoval <osandov@fb.com>
> 
> Swap files cannot have holes, and they must at least two pages.
> swapon(8) and mkswap(8) have stricter restrictions, so add versions of
> those commands without any restrictions.
> 
> Signed-off-by: Omar Sandoval <osandov@fb.com>
> ---
>  .gitignore            |  2 ++
>  src/Makefile          |  2 +-
>  src/mkswap.c          | 83 +++++++++++++++++++++++++++++++++++++++++++
>  src/swapon.c          | 24 +++++++++++++
>  tests/generic/490     | 77 +++++++++++++++++++++++++++++++++++++++
>  tests/generic/490.out |  5 +++
>  tests/generic/group   |  1 +
>  7 files changed, 193 insertions(+), 1 deletion(-)
>  create mode 100644 src/mkswap.c
>  create mode 100644 src/swapon.c
>  create mode 100755 tests/generic/490
>  create mode 100644 tests/generic/490.out
> 
> diff --git a/.gitignore b/.gitignore
> index 53029e24..efc73a7c 100644
> --- a/.gitignore
> +++ b/.gitignore
> @@ -92,6 +92,7 @@
>  /src/lstat64
>  /src/makeextents
>  /src/metaperf
> +/src/mkswap
>  /src/mmapcat
>  /src/multi_open_unlink
>  /src/nametest
> @@ -111,6 +112,7 @@
>  /src/seek_sanity_test
>  /src/stale_handle
>  /src/stat_test
> +/src/swapon
>  /src/t_access_root
>  /src/t_dir_offset
>  /src/t_dir_offset2
> diff --git a/src/Makefile b/src/Makefile
> index c42d3bb1..01fe99ef 100644
> --- a/src/Makefile
> +++ b/src/Makefile
> @@ -26,7 +26,7 @@ LINUX_TARGETS = xfsctl bstat t_mtab getdevicesize preallo_rw_pattern_reader \
>  	renameat2 t_getcwd e4compact test-nextquota punch-alternating \
>  	attr-list-by-handle-cursor-test listxattr dio-interleaved t_dir_type \
>  	dio-invalidate-cache stat_test t_encrypted_d_revalidate \
> -	attr_replace_test
> +	attr_replace_test swapon mkswap
>  
>  SUBDIRS = log-writes perf
>  
> diff --git a/src/mkswap.c b/src/mkswap.c
> new file mode 100644
> index 00000000..d0bce2bd
> --- /dev/null
> +++ b/src/mkswap.c
> @@ -0,0 +1,83 @@
> +/* mkswap(8) without any sanity checks */
> +
> +#include <stdint.h>
> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <string.h>
> +#include <unistd.h>
> +#include <sys/stat.h>
> +#include <sys/types.h>
> +
> +struct swap_header {
> +	char		bootbits[1024];
> +	uint32_t	version;
> +	uint32_t	last_page;
> +	uint32_t	nr_badpages;
> +	unsigned char	sws_uuid[16];
> +	unsigned char	sws_volume[16];
> +	uint32_t	padding[117];
> +	uint32_t	badpages[1];
> +};
> +
> +int main(int argc, char **argv)
> +{
> +	struct swap_header *hdr;
> +	FILE *file;
> +	struct stat st;
> +	long page_size;
> +	int ret;
> +
> +	if (argc != 2) {
> +		fprintf(stderr, "usage: %s PATH\n", argv[0]);
> +		return EXIT_FAILURE;
> +	}
> +
> +	page_size = sysconf(_SC_PAGESIZE);
> +	if (page_size == -1) {
> +		perror("sysconf");
> +		return EXIT_FAILURE;
> +	}
> +
> +	hdr = calloc(1, page_size);
> +	if (!hdr) {
> +		perror("calloc");
> +		return EXIT_FAILURE;
> +	}
> +
> +	file = fopen(argv[1], "r+");
> +	if (!file) {
> +		perror("fopen");
> +		free(hdr);
> +		return EXIT_FAILURE;
> +	}
> +
> +	ret = fstat(fileno(file), &st);
> +	if (ret) {
> +		perror("fstat");
> +		free(hdr);
> +		fclose(file);
> +		return EXIT_FAILURE;
> +	}
> +
> +	hdr->version = 1;
> +	hdr->last_page = st.st_size / page_size - 1;
> +	memset(&hdr->sws_uuid, 0x99, sizeof(hdr->sws_uuid));
> +	memcpy((char *)hdr + page_size - 10, "SWAPSPACE2", 10);
> +
> +	if (fwrite(hdr, page_size, 1, file) != 1) {
> +		perror("fwrite");
> +		free(hdr);
> +		fclose(file);
> +		return EXIT_FAILURE;
> +	}
> +
> +	if (fclose(file) == EOF) {
> +		perror("fwrite");
> +		free(hdr);
> +		return EXIT_FAILURE;
> +	}
> +
> +	free(hdr);
> +
> +	return EXIT_SUCCESS;
> +}
> diff --git a/src/swapon.c b/src/swapon.c
> new file mode 100644
> index 00000000..0cb7108a
> --- /dev/null
> +++ b/src/swapon.c
> @@ -0,0 +1,24 @@
> +/* swapon(8) without any sanity checks; simply calls swapon(2) directly. */
> +
> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <unistd.h>
> +#include <sys/swap.h>
> +
> +int main(int argc, char **argv)
> +{
> +	int ret;
> +
> +	if (argc != 2) {
> +		fprintf(stderr, "usage: %s PATH\n", argv[0]);
> +		return EXIT_FAILURE;
> +	}
> +
> +	ret = swapon(argv[1], 0);
> +	if (ret) {
> +		perror("swapon");
> +		return EXIT_FAILURE;
> +	}
> +
> +	return EXIT_SUCCESS;
> +}
> diff --git a/tests/generic/490 b/tests/generic/490
> new file mode 100755
> index 00000000..6ba2ecb3
> --- /dev/null
> +++ b/tests/generic/490
> @@ -0,0 +1,77 @@
> +#! /bin/bash
> +# FS QA Test 490
> +#
> +# Test invalid swap files.
> +#
> +#-----------------------------------------------------------------------
> +# Copyright (c) 2018 Facebook.  All Rights Reserved.
> +#
> +# This program is free software; you can redistribute it and/or
> +# modify it under the terms of the GNU General Public License as
> +# published by the Free Software Foundation.
> +#
> +# This program is distributed in the hope that it would be useful,
> +# but WITHOUT ANY WARRANTY; without even the implied warranty of
> +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> +# GNU General Public License for more details.
> +#
> +# You should have received a copy of the GNU General Public License
> +# along with this program; if not, write the Free Software Foundation,
> +# Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
> +#-----------------------------------------------------------------------
> +#
> +
> +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
> +
> +# remove previous $seqres.full before test
> +rm -f $seqres.full
> +
> +_supported_fs generic
> +_supported_os Linux
> +_require_scratch_swapfile
> +_require_test_program mkswap
> +_require_test_program swapon
> +
> +_scratch_mkfs >> $seqres.full 2>&1
> +_scratch_mount
> +
> +echo "File with holes"
> +# We can't use _format_swapfile because we're using our custom mkswap and
> +# swapon.
> +touch "$SCRATCH_MNT/swap"
> +$CHATTR_PROG +C "$SCRATCH_MNT/swap"

FWIW I see:

/djwong/e2fsprogs/build-x64/misc/chattr: Operation not supported while setting flags on /opt/swap

on XFS (we don't support the nocow flag), so the stdout/stderr need to
be redirected elsewhere.

--D

> +chmod 0600 "$SCRATCH_MNT/swap"
> +$XFS_IO_PROG -c "truncate $(($(get_page_size) * 10))" "$SCRATCH_MNT/swap"
> +"$here/src/mkswap" "$SCRATCH_MNT/swap"
> +"$here/src/swapon" "$SCRATCH_MNT/swap"
> +swapoff "$SCRATCH_MNT/swap" >/dev/null 2>&1
> +
> +echo "Empty swap file (only swap header)"
> +rm -f "$SCRATCH_MNT/swap"
> +touch "$SCRATCH_MNT/swap"
> +$CHATTR_PROG +C "$SCRATCH_MNT/swap"
> +chmod 0600 "$SCRATCH_MNT/swap"
> +_pwrite_byte 0x61 0 $(get_page_size) "$SCRATCH_MNT/swap" >> $seqres.full
> +"$here/src/mkswap" "$SCRATCH_MNT/swap"
> +"$here/src/swapon" "$SCRATCH_MNT/swap"
> +swapoff "$SCRATCH_MNT/swap" >/dev/null 2>&1
> +
> +status=0
> +exit
> diff --git a/tests/generic/490.out b/tests/generic/490.out
> new file mode 100644
> index 00000000..96226c64
> --- /dev/null
> +++ b/tests/generic/490.out
> @@ -0,0 +1,5 @@
> +QA output created by 490
> +File with holes
> +swapon: Invalid argument
> +Empty swap file (only swap header)
> +swapon: Invalid argument
> diff --git a/tests/generic/group b/tests/generic/group
> index a5252aeb..422e21e2 100644
> --- a/tests/generic/group
> +++ b/tests/generic/group
> @@ -492,3 +492,4 @@
>  487 auto quick
>  488 auto quick swap
>  489 auto quick swap
> +490 auto quick swap
> -- 
> 2.17.0
> 
> --
> To unsubscribe from this list: send the line "unsubscribe fstests" in
> the body of a message to majordomo@vger.kernel.org
> More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v2 2/5] generic: enable swapfile tests on Btrfs
  2018-05-16 20:38 ` [PATCH v2 2/5] generic: enable swapfile tests on Btrfs Omar Sandoval
@ 2018-05-22  6:41   ` Eryu Guan
  2018-05-22 23:32     ` Omar Sandoval
  0 siblings, 1 reply; 11+ messages in thread
From: Eryu Guan @ 2018-05-22  6:41 UTC (permalink / raw)
  To: Omar Sandoval
  Cc: fstests, Darrick J . Wong, linux-btrfs, linux-xfs, kernel-team

On Wed, May 16, 2018 at 01:38:46PM -0700, Omar Sandoval wrote:
> From: Omar Sandoval <osandov@fb.com>
> 
> Commit 8c96cfbfe530 ("generic/35[67]: disable swapfile tests on Btrfs")
> disabled the swapfile tests on Btrfs because it did not support
> swapfiles at the time. Now that we're adding support, we want these

So currently btrfs has no swapfile support yet, and tests still _notrun
with current v4.17-rc5 kernel? Just want to make sure that's expected.

> tests to run, but they don't. _require_scratch_swapfile always fails for
> Btrfs because swapfiles on Btrfs must be set to nocow. After fixing
> that, generic/356 and generic/357 fail for the same reason. After fixing
> _that_, both tests still fail because we don't allow reflinking a
> non-checksummed extent (which nocow implies) to a checksummed extent.
> Add a helper for formatting a swap file which does the chattr, and
> chattr the second file, which gets these tests running on kernels
> supporting Btrfs swapfiles.
> 
> Signed-off-by: Omar Sandoval <osandov@fb.com>
> ---
>  common/rc         | 15 ++++++++++++---
>  tests/generic/356 |  7 ++++---
>  tests/generic/357 |  6 +++---
>  3 files changed, 19 insertions(+), 9 deletions(-)
> 
> diff --git a/common/rc b/common/rc
> index ffe53236..814b8b5c 100644
> --- a/common/rc
> +++ b/common/rc
> @@ -2222,6 +2222,17 @@ _require_odirect()
>  	rm -f $testfile 2>&1 > /dev/null
>  }
>  
> +_format_swapfile() {
> +	local fname="$1"
> +	local sz="$2"
> +
> +	touch "$fname"

Better to remove $fname first, just in case it's an existing file with
some blocks allocated, as chattr(1) says

"Note: For btrfs, the 'C' flag should be set on new or empty files.  If
it is set on a file which already has data blocks, it is undefined when
the blocks assigned to the file will be fully stable"

> +	chmod 0600 "$fname"
> +	$CHATTR_PROG +C "$fname" > /dev/null 2>&1

It'd be good to have some comments on this chattr +C.

> +	_pwrite_byte 0x61 0 "$sz" "$fname" >> $seqres.full
> +	mkswap -U 27376b42-ff65-42ca-919f-6c9b62292a5c "$fname" >> $seqres.full

Is the label really needed?

Thanks,
Eryu

> +}
> +
>  # Check that the filesystem supports swapfiles
>  _require_scratch_swapfile()
>  {
> @@ -2231,10 +2242,8 @@ _require_scratch_swapfile()
>  	_scratch_mount
>  
>  	# Minimum size for mkswap is 10 pages
> -	local size=$(($(get_page_size) * 10))
> +	_format_swapfile "$SCRATCH_MNT/swap" $(($(get_page_size) * 10))
>  
> -	_pwrite_byte 0x61 0 "$size" "$SCRATCH_MNT/swap" >/dev/null 2>&1
> -	mkswap "$SCRATCH_MNT/swap" >/dev/null 2>&1
>  	if ! swapon "$SCRATCH_MNT/swap" >/dev/null 2>&1; then
>  		_scratch_unmount
>  		_notrun "swapfiles are not supported"
> diff --git a/tests/generic/356 b/tests/generic/356
> index 51eeb652..b4a38f84 100755
> --- a/tests/generic/356
> +++ b/tests/generic/356
> @@ -59,11 +59,12 @@ blocks=160
>  blksz=65536
>  
>  echo "Initialize file"
> -echo >> $seqres.full
> -_pwrite_byte 0x61 0 $((blocks * blksz)) $testdir/file1 >> $seqres.full
> -mkswap -U 27376b42-ff65-42ca-919f-6c9b62292a5c $testdir/file1 >> $seqres.full
> +_format_swapfile "$testdir/file1" $((blocks * blksz))
>  swapon $testdir/file1
>  
> +touch "$testdir/file2"
> +$CHATTR_PROG +C "$testdir/file2" >/dev/null 2>&1
> +
>  echo "Try to reflink"
>  _cp_reflink $testdir/file1 $testdir/file2 2>&1 | _filter_scratch
>  
> diff --git a/tests/generic/357 b/tests/generic/357
> index 0dd0c10f..9a83a283 100755
> --- a/tests/generic/357
> +++ b/tests/generic/357
> @@ -59,9 +59,9 @@ blocks=160
>  blksz=65536
>  
>  echo "Initialize file"
> -echo >> $seqres.full
> -_pwrite_byte 0x61 0 $((blocks * blksz)) $testdir/file1 >> $seqres.full
> -mkswap -U 27376b42-ff65-42ca-919f-6c9b62292a5c $testdir/file1 >> $seqres.full
> +_format_swapfile "$testdir/file1" $((blocks * blksz))
> +touch "$testdir/file2"
> +$CHATTR_PROG +C "$testdir/file2" >/dev/null 2>&1
>  _cp_reflink $testdir/file1 $testdir/file2 2>&1 | _filter_scratch
>  
>  echo "Try to swapon"
> -- 
> 2.17.0
> 

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v2 3/5] generic: add test for dedupe on an active swapfile
  2018-05-16 20:38 ` [PATCH v2 3/5] generic: add test for dedupe on an active swapfile Omar Sandoval
@ 2018-05-22  6:47   ` Eryu Guan
  0 siblings, 0 replies; 11+ messages in thread
From: Eryu Guan @ 2018-05-22  6:47 UTC (permalink / raw)
  To: Omar Sandoval
  Cc: fstests, Darrick J . Wong, linux-btrfs, linux-xfs, kernel-team

On Wed, May 16, 2018 at 01:38:47PM -0700, Omar Sandoval wrote:
> From: Omar Sandoval <osandov@fb.com>
> 
> Similar to generic/356 that makes sure we can't reflink an active
                                                  ^^^^^^^ dedupe
I'll fix it on commit.

Thanks,
Eryu

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v2 5/5] generic: test invalid swap file activation
  2018-05-18 14:37   ` Darrick J. Wong
@ 2018-05-22  6:58     ` Eryu Guan
  0 siblings, 0 replies; 11+ messages in thread
From: Eryu Guan @ 2018-05-22  6:58 UTC (permalink / raw)
  To: Darrick J. Wong
  Cc: Omar Sandoval, fstests, linux-btrfs, linux-xfs, kernel-team

On Fri, May 18, 2018 at 07:37:07AM -0700, Darrick J. Wong wrote:
> On Wed, May 16, 2018 at 01:38:49PM -0700, Omar Sandoval wrote:
> > From: Omar Sandoval <osandov@fb.com>
> > 
> > Swap files cannot have holes, and they must at least two pages.
> > swapon(8) and mkswap(8) have stricter restrictions, so add versions of
> > those commands without any restrictions.
> > 
> > Signed-off-by: Omar Sandoval <osandov@fb.com>
> > ---
> >  .gitignore            |  2 ++
> >  src/Makefile          |  2 +-
> >  src/mkswap.c          | 83 +++++++++++++++++++++++++++++++++++++++++++
> >  src/swapon.c          | 24 +++++++++++++
> >  tests/generic/490     | 77 +++++++++++++++++++++++++++++++++++++++
> >  tests/generic/490.out |  5 +++
> >  tests/generic/group   |  1 +
> >  7 files changed, 193 insertions(+), 1 deletion(-)
> >  create mode 100644 src/mkswap.c
> >  create mode 100644 src/swapon.c
> >  create mode 100755 tests/generic/490
> >  create mode 100644 tests/generic/490.out
> > 
> > diff --git a/.gitignore b/.gitignore
> > index 53029e24..efc73a7c 100644
> > --- a/.gitignore
> > +++ b/.gitignore
> > @@ -92,6 +92,7 @@
> >  /src/lstat64
> >  /src/makeextents
> >  /src/metaperf
> > +/src/mkswap
> >  /src/mmapcat
> >  /src/multi_open_unlink
> >  /src/nametest
> > @@ -111,6 +112,7 @@
> >  /src/seek_sanity_test
> >  /src/stale_handle
> >  /src/stat_test
> > +/src/swapon
> >  /src/t_access_root
> >  /src/t_dir_offset
> >  /src/t_dir_offset2
> > diff --git a/src/Makefile b/src/Makefile
> > index c42d3bb1..01fe99ef 100644
> > --- a/src/Makefile
> > +++ b/src/Makefile
> > @@ -26,7 +26,7 @@ LINUX_TARGETS = xfsctl bstat t_mtab getdevicesize preallo_rw_pattern_reader \
> >  	renameat2 t_getcwd e4compact test-nextquota punch-alternating \
> >  	attr-list-by-handle-cursor-test listxattr dio-interleaved t_dir_type \
> >  	dio-invalidate-cache stat_test t_encrypted_d_revalidate \
> > -	attr_replace_test
> > +	attr_replace_test swapon mkswap
> >  
> >  SUBDIRS = log-writes perf
> >  
> > diff --git a/src/mkswap.c b/src/mkswap.c
> > new file mode 100644
> > index 00000000..d0bce2bd
> > --- /dev/null
> > +++ b/src/mkswap.c
> > @@ -0,0 +1,83 @@
> > +/* mkswap(8) without any sanity checks */
> > +
> > +#include <stdint.h>
> > +#include <stdio.h>
> > +#include <stdlib.h>
> > +#include <string.h>
> > +#include <unistd.h>
> > +#include <sys/stat.h>
> > +#include <sys/types.h>
> > +
> > +struct swap_header {
> > +	char		bootbits[1024];
> > +	uint32_t	version;
> > +	uint32_t	last_page;
> > +	uint32_t	nr_badpages;
> > +	unsigned char	sws_uuid[16];
> > +	unsigned char	sws_volume[16];
> > +	uint32_t	padding[117];
> > +	uint32_t	badpages[1];
> > +};
> > +
> > +int main(int argc, char **argv)
> > +{
> > +	struct swap_header *hdr;
> > +	FILE *file;
> > +	struct stat st;
> > +	long page_size;
> > +	int ret;
> > +
> > +	if (argc != 2) {
> > +		fprintf(stderr, "usage: %s PATH\n", argv[0]);
> > +		return EXIT_FAILURE;
> > +	}
> > +
> > +	page_size = sysconf(_SC_PAGESIZE);
> > +	if (page_size == -1) {
> > +		perror("sysconf");
> > +		return EXIT_FAILURE;
> > +	}
> > +
> > +	hdr = calloc(1, page_size);
> > +	if (!hdr) {
> > +		perror("calloc");
> > +		return EXIT_FAILURE;
> > +	}
> > +
> > +	file = fopen(argv[1], "r+");
> > +	if (!file) {
> > +		perror("fopen");
> > +		free(hdr);
> > +		return EXIT_FAILURE;
> > +	}
> > +
> > +	ret = fstat(fileno(file), &st);
> > +	if (ret) {
> > +		perror("fstat");
> > +		free(hdr);
> > +		fclose(file);
> > +		return EXIT_FAILURE;
> > +	}
> > +
> > +	hdr->version = 1;
> > +	hdr->last_page = st.st_size / page_size - 1;
> > +	memset(&hdr->sws_uuid, 0x99, sizeof(hdr->sws_uuid));
> > +	memcpy((char *)hdr + page_size - 10, "SWAPSPACE2", 10);
> > +
> > +	if (fwrite(hdr, page_size, 1, file) != 1) {
> > +		perror("fwrite");
> > +		free(hdr);
> > +		fclose(file);
> > +		return EXIT_FAILURE;
> > +	}
> > +
> > +	if (fclose(file) == EOF) {
> > +		perror("fwrite");
> > +		free(hdr);
> > +		return EXIT_FAILURE;
> > +	}
> > +
> > +	free(hdr);
> > +
> > +	return EXIT_SUCCESS;
> > +}
> > diff --git a/src/swapon.c b/src/swapon.c
> > new file mode 100644
> > index 00000000..0cb7108a
> > --- /dev/null
> > +++ b/src/swapon.c
> > @@ -0,0 +1,24 @@
> > +/* swapon(8) without any sanity checks; simply calls swapon(2) directly. */
> > +
> > +#include <stdio.h>
> > +#include <stdlib.h>
> > +#include <unistd.h>
> > +#include <sys/swap.h>
> > +
> > +int main(int argc, char **argv)
> > +{
> > +	int ret;
> > +
> > +	if (argc != 2) {
> > +		fprintf(stderr, "usage: %s PATH\n", argv[0]);
> > +		return EXIT_FAILURE;
> > +	}
> > +
> > +	ret = swapon(argv[1], 0);
> > +	if (ret) {
> > +		perror("swapon");
> > +		return EXIT_FAILURE;
> > +	}
> > +
> > +	return EXIT_SUCCESS;
> > +}
> > diff --git a/tests/generic/490 b/tests/generic/490
> > new file mode 100755
> > index 00000000..6ba2ecb3
> > --- /dev/null
> > +++ b/tests/generic/490
> > @@ -0,0 +1,77 @@
> > +#! /bin/bash
> > +# FS QA Test 490
> > +#
> > +# Test invalid swap files.
> > +#
> > +#-----------------------------------------------------------------------
> > +# Copyright (c) 2018 Facebook.  All Rights Reserved.
> > +#
> > +# This program is free software; you can redistribute it and/or
> > +# modify it under the terms of the GNU General Public License as
> > +# published by the Free Software Foundation.
> > +#
> > +# This program is distributed in the hope that it would be useful,
> > +# but WITHOUT ANY WARRANTY; without even the implied warranty of
> > +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> > +# GNU General Public License for more details.
> > +#
> > +# You should have received a copy of the GNU General Public License
> > +# along with this program; if not, write the Free Software Foundation,
> > +# Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
> > +#-----------------------------------------------------------------------
> > +#
> > +
> > +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
> > +
> > +# remove previous $seqres.full before test
> > +rm -f $seqres.full
> > +
> > +_supported_fs generic
> > +_supported_os Linux
> > +_require_scratch_swapfile
> > +_require_test_program mkswap
> > +_require_test_program swapon
> > +
> > +_scratch_mkfs >> $seqres.full 2>&1
> > +_scratch_mount
> > +
> > +echo "File with holes"
> > +# We can't use _format_swapfile because we're using our custom mkswap and
> > +# swapon.
> > +touch "$SCRATCH_MNT/swap"
> > +$CHATTR_PROG +C "$SCRATCH_MNT/swap"
> 
> FWIW I see:
> 
> /djwong/e2fsprogs/build-x64/misc/chattr: Operation not supported while setting flags on /opt/swap
> 
> on XFS (we don't support the nocow flag), so the stdout/stderr need to
> be redirected elsewhere.

I can fix it (and the later chattr +C) on commit. Thanks for review!

Eryu

> 
> --D
> 
> > +chmod 0600 "$SCRATCH_MNT/swap"
> > +$XFS_IO_PROG -c "truncate $(($(get_page_size) * 10))" "$SCRATCH_MNT/swap"
> > +"$here/src/mkswap" "$SCRATCH_MNT/swap"
> > +"$here/src/swapon" "$SCRATCH_MNT/swap"
> > +swapoff "$SCRATCH_MNT/swap" >/dev/null 2>&1
> > +
> > +echo "Empty swap file (only swap header)"
> > +rm -f "$SCRATCH_MNT/swap"
> > +touch "$SCRATCH_MNT/swap"
> > +$CHATTR_PROG +C "$SCRATCH_MNT/swap"
> > +chmod 0600 "$SCRATCH_MNT/swap"
> > +_pwrite_byte 0x61 0 $(get_page_size) "$SCRATCH_MNT/swap" >> $seqres.full
> > +"$here/src/mkswap" "$SCRATCH_MNT/swap"
> > +"$here/src/swapon" "$SCRATCH_MNT/swap"
> > +swapoff "$SCRATCH_MNT/swap" >/dev/null 2>&1
> > +
> > +status=0
> > +exit
> > diff --git a/tests/generic/490.out b/tests/generic/490.out
> > new file mode 100644
> > index 00000000..96226c64
> > --- /dev/null
> > +++ b/tests/generic/490.out
> > @@ -0,0 +1,5 @@
> > +QA output created by 490
> > +File with holes
> > +swapon: Invalid argument
> > +Empty swap file (only swap header)
> > +swapon: Invalid argument
> > diff --git a/tests/generic/group b/tests/generic/group
> > index a5252aeb..422e21e2 100644
> > --- a/tests/generic/group
> > +++ b/tests/generic/group
> > @@ -492,3 +492,4 @@
> >  487 auto quick
> >  488 auto quick swap
> >  489 auto quick swap
> > +490 auto quick swap
> > -- 
> > 2.17.0
> > 
> > --
> > To unsubscribe from this list: send the line "unsubscribe fstests" in
> > the body of a message to majordomo@vger.kernel.org
> > More majordomo info at  http://vger.kernel.org/majordomo-info.html

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v2 2/5] generic: enable swapfile tests on Btrfs
  2018-05-22  6:41   ` Eryu Guan
@ 2018-05-22 23:32     ` Omar Sandoval
  0 siblings, 0 replies; 11+ messages in thread
From: Omar Sandoval @ 2018-05-22 23:32 UTC (permalink / raw)
  To: Eryu Guan; +Cc: fstests, Darrick J . Wong, linux-btrfs, linux-xfs, kernel-team

On Tue, May 22, 2018 at 02:41:45PM +0800, Eryu Guan wrote:
> On Wed, May 16, 2018 at 01:38:46PM -0700, Omar Sandoval wrote:
> > From: Omar Sandoval <osandov@fb.com>
> > 
> > Commit 8c96cfbfe530 ("generic/35[67]: disable swapfile tests on Btrfs")
> > disabled the swapfile tests on Btrfs because it did not support
> > swapfiles at the time. Now that we're adding support, we want these
> 
> So currently btrfs has no swapfile support yet, and tests still _notrun
> with current v4.17-rc5 kernel? Just want to make sure that's expected.

Yes, that's correct.

> > tests to run, but they don't. _require_scratch_swapfile always fails for
> > Btrfs because swapfiles on Btrfs must be set to nocow. After fixing
> > that, generic/356 and generic/357 fail for the same reason. After fixing
> > _that_, both tests still fail because we don't allow reflinking a
> > non-checksummed extent (which nocow implies) to a checksummed extent.
> > Add a helper for formatting a swap file which does the chattr, and
> > chattr the second file, which gets these tests running on kernels
> > supporting Btrfs swapfiles.
> > 
> > Signed-off-by: Omar Sandoval <osandov@fb.com>
> > ---
> >  common/rc         | 15 ++++++++++++---
> >  tests/generic/356 |  7 ++++---
> >  tests/generic/357 |  6 +++---
> >  3 files changed, 19 insertions(+), 9 deletions(-)
> > 
> > diff --git a/common/rc b/common/rc
> > index ffe53236..814b8b5c 100644
> > --- a/common/rc
> > +++ b/common/rc
> > @@ -2222,6 +2222,17 @@ _require_odirect()
> >  	rm -f $testfile 2>&1 > /dev/null
> >  }
> >  
> > +_format_swapfile() {
> > +	local fname="$1"
> > +	local sz="$2"
> > +
> > +	touch "$fname"
> 
> Better to remove $fname first, just in case it's an existing file with
> some blocks allocated, as chattr(1) says
> 
> "Note: For btrfs, the 'C' flag should be set on new or empty files.  If
> it is set on a file which already has data blocks, it is undefined when
> the blocks assigned to the file will be fully stable"

Good point.

> > +	chmod 0600 "$fname"
> > +	$CHATTR_PROG +C "$fname" > /dev/null 2>&1
> 
> It'd be good to have some comments on this chattr +C.

Will do.

> > +	_pwrite_byte 0x61 0 "$sz" "$fname" >> $seqres.full
> > +	mkswap -U 27376b42-ff65-42ca-919f-6c9b62292a5c "$fname" >> $seqres.full
> 
> Is the label really needed?

Nope, doesn't look like it. Thanks!

^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2018-05-22 23:32 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-05-16 20:38 [PATCH v2 0/5] xfstests: generic swap file tests Omar Sandoval
2018-05-16 20:38 ` [PATCH v2 1/5] xfstests: create swap group Omar Sandoval
2018-05-16 20:38 ` [PATCH v2 2/5] generic: enable swapfile tests on Btrfs Omar Sandoval
2018-05-22  6:41   ` Eryu Guan
2018-05-22 23:32     ` Omar Sandoval
2018-05-16 20:38 ` [PATCH v2 3/5] generic: add test for dedupe on an active swapfile Omar Sandoval
2018-05-22  6:47   ` Eryu Guan
2018-05-16 20:38 ` [PATCH v2 4/5] generic: add test for truncate/fpunch of " Omar Sandoval
2018-05-16 20:38 ` [PATCH v2 5/5] generic: test invalid swap file activation Omar Sandoval
2018-05-18 14:37   ` Darrick J. Wong
2018-05-22  6:58     ` Eryu Guan

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.