• 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  *				 2004  Marty Ridgeway <mridge@us.ibm.com>
6  * Copyright (C) 2021 SUSE LLC Andrea Cervesato <andrea.cervesato@suse.com>
7  */
8 
9 /*\
10  * [Description]
11  *
12  * Appends zeroed data to a file using O_DIRECT while a child processes are
13  * doing buffered reads after seeking to the end of the file and checks if the
14  * buffer reads always see zero.
15  */
16 
17 #define _GNU_SOURCE
18 
19 #include "tst_test.h"
20 #include "common.h"
21 
22 static int *run_child;
23 
24 static char *str_numchildren;
25 static char *str_writesize;
26 static char *str_appends;
27 
28 static int numchildren;
29 static long long writesize;
30 static int appends;
31 
setup(void)32 static void setup(void)
33 {
34 	numchildren = 16;
35 	writesize = 64 * 1024;
36 	appends = 1000;
37 
38 	if (tst_parse_int(str_numchildren, &numchildren, 1, INT_MAX))
39 		tst_brk(TBROK, "Invalid number of children '%s'", str_numchildren);
40 
41 	if (tst_parse_filesize(str_writesize, &writesize, 1, LLONG_MAX))
42 		tst_brk(TBROK, "Invalid write file size '%s'", str_writesize);
43 
44 	if (tst_parse_int(str_appends, &appends, 1, INT_MAX))
45 		tst_brk(TBROK, "Invalid number of appends '%s'", str_appends);
46 
47 	run_child = SAFE_MMAP(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
48 }
49 
cleanup(void)50 static void cleanup(void)
51 {
52 	SAFE_MUNMAP(run_child, sizeof(int));
53 }
54 
run(void)55 static void run(void)
56 {
57 	char *filename = "dio_append";
58 	int status;
59 	int i;
60 
61 	*run_child = 1;
62 
63 	for (i = 0; i < numchildren; i++) {
64 		if (!SAFE_FORK()) {
65 			io_read_eof(filename, run_child);
66 			return;
67 		}
68 	}
69 
70 	tst_res(TINFO, "Parent append to file");
71 
72 	io_append(filename, 0, O_DIRECT | O_WRONLY | O_CREAT, writesize, appends);
73 
74 	if (SAFE_WAITPID(-1, &status, WNOHANG))
75 		tst_res(TFAIL, "Non zero bytes read");
76 	else
77 		tst_res(TPASS, "All bytes read were zeroed");
78 
79 	*run_child = 0;
80 
81 	SAFE_UNLINK(filename);
82 }
83 
84 static struct tst_test test = {
85 	.test_all = run,
86 	.setup = setup,
87 	.cleanup = cleanup,
88 	.needs_tmpdir = 1,
89 	.forks_child = 1,
90 	.options = (struct tst_option[]) {
91 		{"n:", &str_numchildren, "Number of processes (default 16)"},
92 		{"w:", &str_writesize, "Write size for each append (default 64K)"},
93 		{"c:", &str_appends, "Number of appends (default 1000)"},
94 		{}
95 	},
96 	.skip_filesystems = (const char *[]) {
97 		"tmpfs",
98 		NULL
99 	},
100 };
101