xref: /aosp_15_r20/external/ltp/testcases/kernel/io/ltp-aiodio/dio_append.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2004 Daniel McNeil <[email protected]>
4  *				 2004 Open Source Development Lab
5  *				 2004  Marty Ridgeway <[email protected]>
6  * Copyright (C) 2021 SUSE LLC Andrea Cervesato <[email protected]>
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 volatile 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 = 10000;
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 	if (!tst_fs_has_free(".", appends, writesize))
48 		tst_brk(TCONF, "Not enough space to run the test");
49 
50 	run_child = SAFE_MMAP(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
51 }
52 
cleanup(void)53 static void cleanup(void)
54 {
55 	if (run_child) {
56 		*run_child = 0;
57 		SAFE_MUNMAP((void *)run_child, sizeof(int));
58 	}
59 }
60 
run(void)61 static void run(void)
62 {
63 	char *filename = "dio_append";
64 	int status;
65 	int i;
66 
67 	*run_child = 1;
68 
69 	for (i = 0; i < numchildren; i++) {
70 		if (!SAFE_FORK()) {
71 			io_read_eof(filename, run_child);
72 			return;
73 		}
74 	}
75 
76 	tst_res(TINFO, "Parent append to file");
77 
78 	io_append(filename, 0, O_DIRECT | O_WRONLY | O_CREAT, writesize, appends);
79 
80 	if (!tst_remaining_runtime())
81 		tst_res(TINFO, "Test out of runtime, exiting");
82 
83 	if (SAFE_WAITPID(-1, &status, WNOHANG))
84 		tst_res(TFAIL, "Non zero bytes read");
85 	else
86 		tst_res(TPASS, "All bytes read were zeroed");
87 
88 	*run_child = 0;
89 
90 	SAFE_UNLINK(filename);
91 }
92 
93 static struct tst_test test = {
94 	.test_all = run,
95 	.setup = setup,
96 	.cleanup = cleanup,
97 	.needs_tmpdir = 1,
98 	.forks_child = 1,
99 	.max_runtime = 1800,
100 	.options = (struct tst_option[]) {
101 		{"n:", &str_numchildren, "Number of processes (default 16)"},
102 		{"w:", &str_writesize, "Write size for each append (default 64K)"},
103 		{"c:", &str_appends, "Number of appends (default 10000)"},
104 		{}
105 	},
106 	.skip_filesystems = (const char *[]) {
107 		"tmpfs",
108 		NULL
109 	},
110 };
111