Skip to contentSkip to conversation

Add test for misaligned fallocate()

PATCH Superseded Martin Doucha 2 participants 6 messages Jan 23, 2020 – Jan 24, 2020 Library code

v3 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

v3 cover Add test for misaligned fallocate()

This patch is a follow-up to the fallocate05 fix. The original fallocate05 test accidentally tested some misaligned allocation and deallocation on platforms with block size bigger than 8k but it didn't validate the results correctly. Test calling fallocate() on misaligned file range and this time validate the results properly, taking into account advanced FS features like copy-on-write.

Changes since v1:
- Fix compilation with --std=c89
- Misalign by at most 512 bytes, otherwise the test may miss an XFS bug on PPC
- Clear the temp directory using tst_purge_dir()

Martin Doucha (3):
Add tst_purge_dir() helper function
Add test for misaligned fallocate()
Purge temp directory after each run of fallocate05

 include/tst_device.h                          |   5 +
 lib/tst_tmpdir.c                              | 106 +++++---
 runtest/syscalls                              |   1 +
 .../kernel/syscalls/fallocate/fallocate05.c   |  11 +-
 .../kernel/syscalls/fallocate/fallocate06.c   | 253 ++++++++++++++++++
 5 files changed, 333 insertions(+), 43 deletions(-)
 create mode 100644 testcases/kernel/syscalls/fallocate/fallocate06.c
Signature · 1 line
2.24.1
#1 lore raw

PATCH v3 1/3 Add tst_purge_dir() helper function

This function deletes the contents of the given directory while leaving the directory itself intact. Useful for purging the mountpoint between test iterations or test cases.

---
 include/tst_device.h |   5 ++
 lib/tst_tmpdir.c     | 106 +++++++++++++++++++++++++++----------------
 2 files changed, 73 insertions(+), 38 deletions(-)
include/tst_device.h +5 -0
index 3db5275c9..d2e4ad5be 100644
@@ -88,4 +88,9 @@ static inline int tst_dev_sync(int fd)
  */
 unsigned long tst_dev_bytes_written(const char *dev);
 
+/*
+ *Wipe the contents of given directory but keep the directory itself
+ */
+void tst_purge_dir(const char *path);
+
 #endif	/* TST_DEVICE_H__ */
lib/tst_tmpdir.c +68 -38

137 lines not on this page · raw message

Signature · 1 line
2.24.1
#2 lore raw

PATCH v3 1/3 Add tst_purge_dir() helper function

Hi!

Quoted 15 lines

This function deletes the contents of the given directory while leaving the directory itself intact. Useful for purging the mountpoint between test iterations or test cases.

---
 include/tst_device.h |   5 ++
 lib/tst_tmpdir.c     | 106 +++++++++++++++++++++++++++----------------
 2 files changed, 73 insertions(+), 38 deletions(-)
include/tst_device.h +0 -0
index 3db5275c9..d2e4ad5be 100644

include/tst_device.h static inline int tst_dev_sync(int fd) · line 94

Quoted 7 lines
include/tst_device.h +4 -0
@@ -88,4 +88,9 @@ static inline int tst_dev_sync(int fd)
  */
 unsigned long tst_dev_bytes_written(const char *dev);
 
+/*
+ *Wipe the contents of given directory but keep the directory itself
+ */
+void tst_purge_dir(const char *path);

I'm not sure that it belongs to tst_device.h.

We do have functions such as tst_dir_is_empty() in tst_fs.h so maybe that would be a slightly better fit.

Quoted 39 lines
include/tst_device.h +0 -0
 #endif	/* TST_DEVICE_H__ */
lib/tst_tmpdir.c +5 -9
index 09b7b6e22..4b21dfad6 100644
@@ -99,6 +99,8 @@ static char test_start_work_dir[PATH_MAX];
 /* lib/tst_checkpoint.c */
 extern futex_t *tst_futexes;
 
+static int rmobj(const char *obj, char **errmsg);
+
 int tst_tmpdir_created(void)
 {
 	return TESTDIR != NULL;
@@ -119,60 +121,80 @@ const char *tst_get_startwd(void)
 	return test_start_work_dir;
 }
 
-static int rmobj(char *obj, char **errmsg)
+static int purge_dir(const char *path, char **errptr)
 {
 	int ret_val = 0;
 	DIR *dir;
 	struct dirent *dir_ent;
 	char dirobj[PATH_MAX];
-	struct stat statbuf;
 	static char err_msg[1024];
 	int fd;
 
-	fd = open(obj, O_DIRECTORY | O_NOFOLLOW);
-	if (fd != -1) {
-		close(fd);
+	errno = 0;
+	fd = open(path, O_DIRECTORY | O_NOFOLLOW);
 
-		/* Do NOT perform the request if the directory is "/" */
-		if (!strcmp(obj, "/")) {
-			if (errmsg != NULL) {
-				sprintf(err_msg, "Cannot remove /");

lib/tst_tmpdir.c const char *tst_get_startwd(void) · line 142

Quoted 10 lines
lib/tst_tmpdir.c +6 -3
-				*errmsg = err_msg;
-			}
-			return -1;
+	if (fd < 0) {
+		if (errptr) {
+			sprintf(err_msg,
+				"Cannot open directory %s; errno=%d: %s",
+				path, errno, tst_strerrno(errno));
+			*errptr = err_msg;
 		}

Isn't this check useless here? Or are we trying to avoid escaping the directory via symlink?

If that's so maybe we should make it more obvious with stat() and S_ISLNK(), or at least add a comment.

Also it would be nice to add an paragraph describing this function into the doc/test-writing-guidelines.txt

Signature · 2 lines
Cyril Hrubis
[email protected]
#3 lore raw

PATCH v3 1/3 Add tst_purge_dir() helper function

Quoted 15 lines · Cyril Hrubis
Quoted 15 lines
lib/tst_tmpdir.c +4 -10
-	fd = open(obj, O_DIRECTORY | O_NOFOLLOW);
-	if (fd != -1) {
-		close(fd);
+	errno = 0;
+	fd = open(path, O_DIRECTORY | O_NOFOLLOW);
 
-		/* Do NOT perform the request if the directory is "/" */
-		if (!strcmp(obj, "/")) {
-			if (errmsg != NULL) {
-				sprintf(err_msg, "Cannot remove /");
-				*errmsg = err_msg;
-			}
-			return -1;
+	if (fd < 0) {
+		if (errptr) {
Quoted 8 lines

lib/tst_tmpdir.c const char *tst_get_startwd(void) · line 142

Quoted 5 lines
lib/tst_tmpdir.c +4 -0
+			sprintf(err_msg,
+				"Cannot open directory %s; errno=%d: %s",
+				path, errno, tst_strerrno(errno));
+			*errptr = err_msg;
 		}

Isn't this check useless here? Or are we trying to avoid escaping the directory via symlink?

You're right. I've just blindly copied the check from rmobj() which uses it to decide whether the FS node should be removed using rmdir() or unlink(). I'll move the open() check to tst_purge_dir() so it doesn't get called twice for each subdirectory and drop the useless O_NOFOLLOW.

Signature · 7 lines
Martin Doucha   [email protected]
QA Engineer for Software Maintenance
SUSE LINUX, s.r.o.
CORSO IIa
Krizikova 148/34
186 00 Prague 8
Czech Republic
#4 lore raw

PATCH v3 2/3 Add test for misaligned fallocate()

In reply to Martin Doucha

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

---
 runtest/syscalls                              |   1 +
 .../kernel/syscalls/fallocate/fallocate06.c   | 253 ++++++++++++++++++
 2 files changed, 254 insertions(+)
 create mode 100644 testcases/kernel/syscalls/fallocate/fallocate06.c
runtest/syscalls +1 -0
index fa87ef63f..ec522e8fa 100644
@@ -184,6 +184,7 @@ fallocate02 fallocate02
 fallocate03 fallocate03
 fallocate04 fallocate04
 fallocate05 fallocate05
+fallocate06 fallocate06
 
 fsetxattr01 fsetxattr01
 fsetxattr02 fsetxattr02
testcases/kernel/syscalls/fallocate/fallocate06.c new file +253 -0

253 lines not on this page · raw message

Signature · 1 line
2.24.1
#5 lore raw

PATCH v3 3/3 Purge temp directory after each run of fallocate05

In reply to Martin Doucha

The fallocate05 test fills the test device so iterating the test requires purging the temp directory after each run.

---
 testcases/kernel/syscalls/fallocate/fallocate05.c | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)
testcases/kernel/syscalls/fallocate/fallocate05.c +6 -5
index 00a1d3864..550d10258 100644
@@ -30,13 +30,13 @@
 #define DEALLOCATE_BLOCKS 4
 #define TESTED_FLAGS "fallocate(FALLOC_FL_PUNCH_HOLE | FALLOC_FL_KEEP_SIZE)"
 
-static int fd;
 static char *buf;
 static blksize_t blocksize;
 static long bufsize;
 
 static void setup(void)
 {
+	int fd;
 	struct stat statbuf;
 
 	fd = SAFE_OPEN(MNTPOINT "/test_file", O_WRONLY | O_CREAT);
@@ -49,12 +49,15 @@ static void setup(void)
 	blocksize = statbuf.st_blksize;
 	bufsize = FALLOCATE_BLOCKS * blocksize;
 	buf = SAFE_MALLOC(bufsize);
+	SAFE_CLOSE(fd);
 }
 
 static void run(void)
 {
+	int fd;
 	long extsize, tmp;
 
+	fd = SAFE_OPEN(MNTPOINT "/test_file", O_WRONLY | O_CREAT | O_TRUNC);
 	TEST(fallocate(fd, 0, 0, bufsize));
 
 	if (TST_RET) {
@@ -131,14 +134,12 @@ static void run(void)
 	else
 		tst_res(TPASS, "write()");
 
-	/* TODO: wipe the test device here to allow looping with -i/-I */
+	SAFE_CLOSE(fd);
+	tst_purge_dir(MNTPOINT);
 }
 
 static void cleanup(void)
 {
-	if (fd > 0)
-		SAFE_CLOSE(fd);
-
 	free(buf);
 }
 
Signature · 1 line
2.24.1