• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2004 Daniel McNeil <daniel@osdl.org>
4  *				 2004 Open Source Development Lab
5  * Copyright (C) 2021 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
6  */
7 
8 /*\
9  * [Description]
10  *
11  * This test is mixing direct I/O and truncate operations checking if they can
12  * be used together at the same time. Multiple children are spawned to read a
13  * file that is written to using direct I/O and truncated in a loop.
14  *
15  * [Algorithm]
16  *
17  * - Spawn multiple children which start to read on 'file'
18  * - Parent start to fill and truncate 'file' many times with zero char when
19  *   children are reading
20  * - Parent start to fill and truncate a junk file many times with non-zero char
21  *
22  * If no issues occur on direct IO/truncate operations and the file always
23  * contains zero characters, test PASS. Otherwise, test will FAIL.
24  */
25 
26 #define _GNU_SOURCE
27 
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <sys/stat.h>
31 #include <sys/types.h>
32 #include <fcntl.h>
33 #include "tst_test.h"
34 #include "common.h"
35 
36 static int *run_child;
37 
38 static char *str_numchildren;
39 static char *str_filesize;
40 static char *str_numappends;
41 static char *str_numwrites;
42 
43 static int numchildren = 16;
44 static long long filesize = 64 * 1024;
45 static long long alignment;
46 static int numappends = 100;
47 static int numwrites = 100;
48 
dio_read(const char * filename,long long align,size_t bs)49 static void dio_read(const char *filename, long long align, size_t bs)
50 {
51 	int fd;
52 	int r;
53 	char *bufptr;
54 
55 	while ((fd = open(filename, O_RDONLY | O_DIRECT, 0666)) < 0)
56 		usleep(100);
57 
58 	bufptr = SAFE_MEMALIGN(align, bs);
59 
60 	tst_res(TINFO, "child %i reading file", getpid());
61 	while (*run_child) {
62 		off_t offset;
63 		char *bufoff;
64 
65 		offset = SAFE_LSEEK(fd, SEEK_SET, 0);
66 		do {
67 			r = read(fd, bufptr, 64 * 1024);
68 			if (r > 0) {
69 				bufoff = check_zero(bufptr, r);
70 				if (bufoff) {
71 					tst_res(TINFO, "non-zero read at offset %zu",
72 						offset + (bufoff - bufptr));
73 					free(bufptr);
74 					SAFE_CLOSE(fd);
75 					return;
76 				}
77 				offset += r;
78 			}
79 		} while (r > 0);
80 	}
81 
82 	free(bufptr);
83 	SAFE_CLOSE(fd);
84 }
85 
setup(void)86 static void setup(void)
87 {
88 	struct stat sb;
89 
90 	if (tst_parse_int(str_numchildren, &numchildren, 1, INT_MAX))
91 		tst_brk(TBROK, "Invalid number of children '%s'", str_numchildren);
92 
93 	if (tst_parse_filesize(str_filesize, &filesize, 1, LLONG_MAX))
94 		tst_brk(TBROK, "Invalid file size '%s'", str_filesize);
95 
96 	if (tst_parse_int(str_numappends, &numappends, 1, INT_MAX))
97 		tst_brk(TBROK, "Invalid number of appends '%s'", str_numappends);
98 
99 	if (tst_parse_int(str_numwrites, &numwrites, 1, INT_MAX))
100 		tst_brk(TBROK, "Invalid number of truncate/append '%s'", str_numwrites);
101 
102 	SAFE_STAT(".", &sb);
103 	alignment = sb.st_blksize;
104 
105 	run_child = SAFE_MMAP(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
106 }
107 
cleanup(void)108 static void cleanup(void)
109 {
110 	if (run_child) {
111 		*run_child = 0;
112 		SAFE_MUNMAP(run_child, sizeof(int));
113 	}
114 }
115 
run(void)116 static void run(void)
117 {
118 	char *filename = "file.bin";
119 	int wflags = O_DIRECT | O_WRONLY | O_CREAT;
120 	int status;
121 	int i;
122 	int fail = 0;
123 
124 	*run_child = 1;
125 
126 	for (i = 0; i < numchildren; i++) {
127 		if (!SAFE_FORK()) {
128 			dio_read(filename, alignment, filesize);
129 			return;
130 		}
131 	}
132 
133 	tst_res(TINFO, "Parent writes/truncates the file");
134 
135 	for (i = 0; i < numwrites; i++) {
136 		io_append(filename, 0, wflags, filesize, numappends);
137 		SAFE_TRUNCATE(filename, 0);
138 		io_append("junkfile", 0xaa, wflags, filesize, numappends);
139 		SAFE_TRUNCATE("junkfile", 0);
140 
141 		if (SAFE_WAITPID(-1, &status, WNOHANG)) {
142 			fail = 1;
143 			break;
144 		}
145 	}
146 
147 	if (fail)
148 		tst_res(TFAIL, "Non zero bytes read");
149 	else
150 		tst_res(TPASS, "All bytes read were zeroed");
151 
152 	*run_child = 0;
153 }
154 
155 static struct tst_test test = {
156 	.test_all = run,
157 	.setup = setup,
158 	.cleanup = cleanup,
159 	.needs_tmpdir = 1,
160 	.forks_child = 1,
161 	.options = (struct tst_option[]) {
162 		{"n:", &str_numchildren, "Number of threads (default 16)"},
163 		{"s:", &str_filesize, "Size of file (default 64K)"},
164 		{"a:", &str_numappends, "Number of appends (default 100)"},
165 		{"c:", &str_numwrites, "Number of append & truncate (default 100)"},
166 		{}
167 	},
168 	.skip_filesystems = (const char *[]) {
169 		"tmpfs",
170 		NULL
171 	},
172 };
173