All of lore.kernel.org
 help / color / mirror / Atom feed
* [LTP] [PATCH v5] Add test for misaligned fallocate()
@ 2020-02-24 10:09 Martin Doucha
  2020-02-26 13:57 ` Cyril Hrubis
  0 siblings, 1 reply; 7+ messages in thread
From: Martin Doucha @ 2020-02-24 10:09 UTC (permalink / raw)
  To: ltp

Make sure that space allocation and deallocation works (or fails) correctly
even when the requested file range does not align with filesystem blocks.

Test on:
- empty file system
- full file system
- also test with and without copy-on-write when supported

Signed-off-by: Martin Doucha <mdoucha@suse.cz>
---

I'd prefer to keep the if(blocksize < 2) sanity check. Over 90% of the
validation checks we have in code will never trigger but we lose nothing
by keeping them anyway. I'm actually more worried about fstat() bugs than
any filesystem really using tiny blocksize.

Changes since v1:
- Fix compilation with --std=c89
- Misalign by@most 512 bytes, otherwise the test may miss an XFS bug on PPC

Changes since v2:
- Use tst_purge_dir() for cleanup between test iterations

Changes since v4:
- Code style fixes

 runtest/syscalls                              |   1 +
 .../kernel/syscalls/fallocate/fallocate06.c   | 257 ++++++++++++++++++
 2 files changed, 258 insertions(+)
 create mode 100644 testcases/kernel/syscalls/fallocate/fallocate06.c

diff --git a/runtest/syscalls b/runtest/syscalls
index e42db9910..133102c4b 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -186,6 +186,7 @@ fallocate02 fallocate02
 fallocate03 fallocate03
 fallocate04 fallocate04
 fallocate05 fallocate05
+fallocate06 fallocate06
 
 fsetxattr01 fsetxattr01
 fsetxattr02 fsetxattr02
diff --git a/testcases/kernel/syscalls/fallocate/fallocate06.c b/testcases/kernel/syscalls/fallocate/fallocate06.c
new file mode 100644
index 000000000..4be2d34fd
--- /dev/null
+++ b/testcases/kernel/syscalls/fallocate/fallocate06.c
@@ -0,0 +1,257 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2019 SUSE LLC <mdoucha@suse.cz>
+ */
+
+/*
+ * Tests misaligned fallocate()
+ * Test scenario:
+ * 1. write() several blocks worth of data
+ * 2. fallocate() some more space (not aligned to FS blocks)
+ * 3. try to write() into the allocated space
+ * 4. deallocate misaligned part of file range written in step 1
+ * 5. read() the deallocated range and check that it was zeroed
+ *
+ * Subtests:
+ * - fill file system between step 2 and 3
+ * - disable copy-on-write on test file
+ * - combinations of above subtests
+ */
+
+#define _GNU_SOURCE
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#include <linux/fs.h>
+#include "tst_test.h"
+#include "lapi/fallocate.h"
+
+#define MNTPOINT "mntpoint"
+#define TEMPFILE MNTPOINT "/test_file"
+#define WRITE_BLOCKS 8
+#define FALLOCATE_BLOCKS 2
+#define DEALLOCATE_BLOCKS 3
+#define TESTED_FLAGS "fallocate(FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE)"
+
+const struct test_case {
+	int no_cow, fill_fs;
+} testcase_list[] = {
+	{1, 0},
+	{1, 1},
+	{0, 0},
+	{0, 1}
+};
+
+static int cow_support;
+static char *wbuf, *rbuf;
+static blksize_t blocksize;
+static long wbuf_size, rbuf_size, block_offset;
+
+static int toggle_cow(int fd, int enable)
+{
+	int ret, attr;
+
+	ret = ioctl(fd, FS_IOC_GETFLAGS, &attr);
+
+	if (ret)
+		return ret;
+
+	if (enable)
+		attr &= ~FS_NOCOW_FL;
+	else
+		attr |= FS_NOCOW_FL;
+
+	return ioctl(fd, FS_IOC_SETFLAGS, &attr);
+}
+
+static void setup(void)
+{
+	unsigned char ch;
+	long i;
+	int fd;
+	struct stat statbuf;
+
+	fd = SAFE_OPEN(TEMPFILE, O_WRONLY | O_CREAT | O_TRUNC);
+
+	/*
+	 * Set FS_NOCOW_FL flag on the temp file. Non-CoW filesystems will
+	 * return error.
+	 */
+	TEST(toggle_cow(fd, 0));
+	SAFE_FSTAT(fd, &statbuf);
+	blocksize = statbuf.st_blksize;
+	block_offset = MIN(blocksize / 2, 512);
+	wbuf_size = MAX(WRITE_BLOCKS, FALLOCATE_BLOCKS) * blocksize;
+	rbuf_size = (DEALLOCATE_BLOCKS + 1) * blocksize;
+	SAFE_CLOSE(fd);
+	SAFE_UNLINK(TEMPFILE);
+
+	if (blocksize < 2)
+		tst_brk(TCONF, "Block size %ld too small for test", blocksize);
+
+	if (!TST_RET) {
+		cow_support = 1;
+	} else {
+		switch (TST_ERR) {
+		case ENOTSUP:
+		case ENOTTY:
+		case EINVAL:
+		case ENOSYS:
+			cow_support = 0;
+			break;
+
+		default:
+			tst_brk(TBROK|TTERRNO,
+				"Error checking copy-on-write support");
+			break;
+		}
+	}
+
+	tst_res(TINFO, "Copy-on-write is%s supported",
+		cow_support ? "" : " not");
+	wbuf = SAFE_MALLOC(wbuf_size);
+	rbuf = SAFE_MALLOC(rbuf_size);
+
+	/* Fill the buffer with known values */
+	for (i = 0, ch = 1; i < wbuf_size; i++, ch++)
+		wbuf[i] = ch;
+}
+
+static int check_result(const struct test_case *tc, const char *func, long exp)
+{
+	if (tc->fill_fs && !tc->no_cow && TST_RET < 0) {
+		if (TST_RET != -1) {
+			tst_res(TFAIL, "%s returned unexpected value %ld",
+				func, TST_RET);
+			return 0;
+		}
+
+		if (TST_ERR != ENOSPC) {
+			tst_res(TFAIL | TTERRNO, "%s should fail with ENOSPC",
+				func);
+			return 0;
+		}
+
+		tst_res(TPASS | TTERRNO, "%s on full FS with CoW", func);
+		return 1;
+	}
+
+	if (TST_RET < 0) {
+		tst_res(TFAIL | TTERRNO, "%s failed unexpectedly", func);
+		return 0;
+	}
+
+	if (TST_RET != exp) {
+		tst_res(TFAIL,
+			"Unexpected return value from %s: %ld (expected %ld)",
+			func, TST_RET, exp);
+		return 0;
+	}
+
+	tst_res(TPASS, "%s successful", func);
+	return 1;
+}
+
+static void run(unsigned int n)
+{
+	int fd, i, err;
+	long offset, size;
+	const struct test_case *tc = testcase_list + n;
+
+	tst_res(TINFO, "Case %u. Fill FS: %s; Use copy on write: %s", n+1,
+		tc->fill_fs ? "yes" : "no", tc->no_cow ? "no" : "yes");
+	fd = SAFE_OPEN(TEMPFILE, O_RDWR | O_CREAT | O_TRUNC);
+
+	if (cow_support)
+		toggle_cow(fd, !tc->no_cow);
+	else if (!tc->no_cow)
+		tst_brk(TCONF, "File system does not support copy-on-write");
+
+	/* Prepare test data for deallocation test */
+	size = WRITE_BLOCKS * blocksize;
+	SAFE_WRITE(1, fd, wbuf, size);
+
+	/* Allocation test */
+	offset = size + block_offset;
+	size = FALLOCATE_BLOCKS * blocksize;
+	TEST(fallocate(fd, 0, offset, size));
+
+	if (TST_RET) {
+		SAFE_CLOSE(fd);
+
+		if (TST_ERR == ENOTSUP)
+			tst_brk(TCONF | TTERRNO, "fallocate() not supported");
+
+		tst_brk(TBROK | TTERRNO, "fallocate(fd, 0, %ld, %ld)", offset,
+			size);
+	}
+
+	if (tc->fill_fs)
+		tst_fill_fs(MNTPOINT, 1);
+
+	SAFE_LSEEK(fd, offset, SEEK_SET);
+	TEST(write(fd, wbuf, size));
+	if (check_result(tc, "write()", size))
+		tst_res(TPASS, "Misaligned allocation works as expected");
+
+	/* Deallocation test */
+	size = DEALLOCATE_BLOCKS * blocksize;
+	offset = block_offset;
+	TEST(fallocate(fd, FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE, offset,
+		size));
+
+	if (TST_RET == -1 && TST_ERR == ENOTSUP) {
+		tst_res(TCONF | TTERRNO, TESTED_FLAGS);
+		goto end;
+	}
+
+	if (!check_result(tc, TESTED_FLAGS, 0) || TST_RET)
+		goto end;
+
+	/* Validate that fallocate() cleared the correct file range */
+	SAFE_LSEEK(fd, 0, SEEK_SET);
+	SAFE_READ(1, fd, rbuf, rbuf_size);
+
+	for (err = 0, i = offset; i < offset + size; i++) {
+		if (rbuf[i]) {
+			err = 1;
+			break;
+		}
+	}
+
+	err = err || memcmp(rbuf, wbuf, offset);
+	offset += size;
+	size = rbuf_size - offset;
+	err = err || memcmp(rbuf + offset, wbuf + offset, size);
+
+	if (err)
+		tst_res(TFAIL, TESTED_FLAGS
+			" did not clear the correct file range.");
+	else
+		tst_res(TPASS, TESTED_FLAGS " cleared the correct file range");
+
+end:
+	SAFE_CLOSE(fd);
+	tst_purge_dir(MNTPOINT);
+}
+
+static void cleanup(void)
+{
+	free(wbuf);
+	free(rbuf);
+}
+
+static struct tst_test test = {
+	.test = run,
+	.tcnt = ARRAY_SIZE(testcase_list),
+	.needs_root = 1,
+	.mount_device = 1,
+	.dev_min_size = 512,
+	.mntpoint = MNTPOINT,
+	.all_filesystems = 1,
+	.setup = setup,
+	.cleanup = cleanup,
+};
-- 
2.25.0


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

* [LTP] [PATCH v5] Add test for misaligned fallocate()
  2020-02-24 10:09 [LTP] [PATCH v5] Add test for misaligned fallocate() Martin Doucha
@ 2020-02-26 13:57 ` Cyril Hrubis
  2020-02-26 16:40   ` Martin Doucha
  0 siblings, 1 reply; 7+ messages in thread
From: Cyril Hrubis @ 2020-02-26 13:57 UTC (permalink / raw)
  To: ltp

Hi!
Are the failures on xfs fixed by now?

Do we have a upstrem kernel commit we can add to the test as a tag?

-- 
Cyril Hrubis
chrubis@suse.cz

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

* [LTP] [PATCH v5] Add test for misaligned fallocate()
  2020-02-26 13:57 ` Cyril Hrubis
@ 2020-02-26 16:40   ` Martin Doucha
  2020-02-27 14:13     ` Cyril Hrubis
  0 siblings, 1 reply; 7+ messages in thread
From: Martin Doucha @ 2020-02-26 16:40 UTC (permalink / raw)
  To: ltp

On 2/26/20 2:57 PM, Cyril Hrubis wrote:
> Hi!
> Are the failures on xfs fixed by now?
> 
> Do we have a upstrem kernel commit we can add to the test as a tag?
> 

Anthony Iliopoulos says in the BSC that the upstream fix is
commit e093c4be760e ("xfs: Fix tail rounding in xfs_alloc_file_space()")

See https://bugzilla.suse.com/show_bug.cgi?id=1161087#c1

-- 
Martin Doucha   mdoucha@suse.cz
QA Engineer for Software Maintenance
SUSE LINUX, s.r.o.
CORSO IIa
Krizikova 148/34
186 00 Prague 8
Czech Republic

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

* [LTP] [PATCH v5] Add test for misaligned fallocate()
  2020-02-26 16:40   ` Martin Doucha
@ 2020-02-27 14:13     ` Cyril Hrubis
  2020-03-13  4:31       ` Yang Xu
  0 siblings, 1 reply; 7+ messages in thread
From: Cyril Hrubis @ 2020-02-27 14:13 UTC (permalink / raw)
  To: ltp

Hi!
> > Are the failures on xfs fixed by now?
> > 
> > Do we have a upstrem kernel commit we can add to the test as a tag?
> > 
> 
> Anthony Iliopoulos says in the BSC that the upstream fix is
> commit e093c4be760e ("xfs: Fix tail rounding in xfs_alloc_file_space()")

I've added this as a tag and commend and also added a .gitignore entry
and pushed. Thanks.

-- 
Cyril Hrubis
chrubis@suse.cz

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

* [LTP] [PATCH v5] Add test for misaligned fallocate()
  2020-02-27 14:13     ` Cyril Hrubis
@ 2020-03-13  4:31       ` Yang Xu
  2020-03-13  8:17         ` Martin Doucha
  0 siblings, 1 reply; 7+ messages in thread
From: Yang Xu @ 2020-03-13  4:31 UTC (permalink / raw)
  To: ltp

Hi Cyril, Martin

When I test this case by using btrfs, this case failed as below:

fallocate06.c:174: CONF: File system does not support copy-on-write
tst_test.c:1290: INFO: Testing on btrfs
tst_mkfs.c:90: INFO: Formatting /dev/loop0 with btrfs opts='' extra opts=''
tst_test.c:1229: INFO: Timeout per run is 0h 05m 00s
fallocate06.c:117: INFO: Copy-on-write is supported
fallocate06.c:168: INFO: Case 1. Fill FS: no; Use copy on write: no
fallocate06.c:157: PASS: write() successful
fallocate06.c:201: PASS: Misaligned allocation works as expected
fallocate06.c:157: PASS: fallocate(FALLOC_FL_PUNCH_HOLE | 
FALLOC_FL_KEEP_SIZE) successful
fallocate06.c:237: PASS: fallocate(FALLOC_FL_PUNCH_HOLE | 
FALLOC_FL_KEEP_SIZE) cleared the correct file range
fallocate06.c:168: INFO: Case 2. Fill FS: yes; Use copy on write: no
tst_fill_fs.c:32: INFO: Creating file mntpoint/file0 size 21710183
tst_fill_fs.c:32: INFO: Creating file mntpoint/file1 size 8070086
tst_fill_fs.c:32: INFO: Creating file mntpoint/file2 size 3971177
tst_fill_fs.c:32: INFO: Creating file mntpoint/file3 size 36915315
tst_fill_fs.c:32: INFO: Creating file mntpoint/file4 size 70310993
tst_fill_fs.c:32: INFO: Creating file mntpoint/file5 size 4807935
tst_fill_fs.c:32: INFO: Creating file mntpoint/file6 size 90739786
tst_fill_fs.c:32: INFO: Creating file mntpoint/file7 size 76896492
tst_fill_fs.c:32: INFO: Creating file mntpoint/file8 size 72228649
tst_fill_fs.c:32: INFO: Creating file mntpoint/file9 size 36207821
tst_fill_fs.c:32: INFO: Creating file mntpoint/file10 size 81483962
tst_fill_fs.c:59: INFO: write(): ENOSPC (28)
fallocate06.c:157: PASS: write() successful
fallocate06.c:201: PASS: Misaligned allocation works as expected
fallocate06.c:146: FAIL: fallocate(FALLOC_FL_PUNCH_HOLE | 
FALLOC_FL_KEEP_SIZE) failed unexpectedly: ENOSPC (28)

It failed when we fill fs and not using cow. Is it a known issue(I see 
the previous eamils, but found nothing)?
I also used 5.6-rc3 kernel to test, it still failed.

Best Regards
Yang Xu

> Hi!
>>> Are the failures on xfs fixed by now?
>>>
>>> Do we have a upstrem kernel commit we can add to the test as a tag?
>>>
>>
>> Anthony Iliopoulos says in the BSC that the upstream fix is
>> commit e093c4be760e ("xfs: Fix tail rounding in xfs_alloc_file_space()")
> 
> I've added this as a tag and commend and also added a .gitignore entry
> and pushed. Thanks.
> 



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

* [LTP] [PATCH v5] Add test for misaligned fallocate()
  2020-03-13  4:31       ` Yang Xu
@ 2020-03-13  8:17         ` Martin Doucha
  2020-03-13  8:42           ` Yang Xu
  0 siblings, 1 reply; 7+ messages in thread
From: Martin Doucha @ 2020-03-13  8:17 UTC (permalink / raw)
  To: ltp

On 13. 03. 20 5:31, Yang Xu wrote:
> Hi Cyril, Martin
> 
> When I test this case by using btrfs, this case failed as below:
> 
> ...
> 
> It failed when we fill fs and not using cow. Is it a known issue(I see
> the previous eamils, but found nothing)?
> I also used 5.6-rc3 kernel to test, it still failed.

Yes, this is a known issue. Upstream kernel fix is currently being reviewed:
https://patchwork.kernel.org/patch/11357415/

-- 
Martin Doucha   mdoucha@suse.cz
QA Engineer for Software Maintenance
SUSE LINUX, s.r.o.
CORSO IIa
Krizikova 148/34
186 00 Prague 8
Czech Republic

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

* [LTP] [PATCH v5] Add test for misaligned fallocate()
  2020-03-13  8:17         ` Martin Doucha
@ 2020-03-13  8:42           ` Yang Xu
  0 siblings, 0 replies; 7+ messages in thread
From: Yang Xu @ 2020-03-13  8:42 UTC (permalink / raw)
  To: ltp

Hi Martin


> On 13. 03. 20 5:31, Yang Xu wrote:
>> Hi Cyril, Martin
>>
>> When I test this case by using btrfs, this case failed as below:
>>
>> ...
>>
>> It failed when we fill fs and not using cow. Is it a known issue(I see
>> the previous eamils, but found nothing)?
>> I also used 5.6-rc3 kernel to test, it still failed.
> 
> Yes, this is a known issue. Upstream kernel fix is currently being reviewed:
> https://patchwork.kernel.org/patch/11357415/
Thanks for a quick response.

Best Regards
Yang Xu
> 



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

end of thread, other threads:[~2020-03-13  8:42 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-02-24 10:09 [LTP] [PATCH v5] Add test for misaligned fallocate() Martin Doucha
2020-02-26 13:57 ` Cyril Hrubis
2020-02-26 16:40   ` Martin Doucha
2020-02-27 14:13     ` Cyril Hrubis
2020-03-13  4:31       ` Yang Xu
2020-03-13  8:17         ` Martin Doucha
2020-03-13  8:42           ` Yang Xu

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.