xref: /aosp_15_r20/external/ltp/testcases/kernel/io/ltp-aiodio/dio_sparse.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  *
6  *   Copyright (c) 2004 Marty Ridgeway <[email protected]>
7  *
8  *   Copyright (c) 2011 Cyril Hrubis <[email protected]>
9  *
10  *   Copyright (C) 2021 SUSE LLC Andrea Cervesato <[email protected]>
11  */
12 
13 /*\
14  * [Description]
15  *
16  * Create a sparse file using O_DIRECT while other processes are doing
17  * buffered reads and check if the buffer reads always see zero.
18  */
19 
20 #define _GNU_SOURCE
21 
22 #include <stdlib.h>
23 #include <unistd.h>
24 #include <string.h>
25 #include <sys/wait.h>
26 #include "tst_test.h"
27 #include "common.h"
28 
29 static volatile int *run_child;
30 
31 static char *str_numchildren;
32 static char *str_writesize;
33 static char *str_filesize;
34 static char *str_offset;
35 
36 static int numchildren = 16;
37 static long long writesize = 1024;
38 static long long filesize = 100 * 1024 * 1024;
39 static long long offset = 0;
40 static long long alignment;
41 
dio_sparse(int fd,int align,long long fs,int ws,long long off)42 static void dio_sparse(int fd, int align, long long fs, int ws, long long off)
43 {
44 	void *bufptr = NULL;
45 	long long i;
46 	int w;
47 
48 	bufptr = SAFE_MEMALIGN(align, ws);
49 
50 	memset(bufptr, 0, ws);
51 	SAFE_LSEEK(fd, off, SEEK_SET);
52 
53 	for (i = off; i < fs;) {
54 		if (!tst_remaining_runtime()) {
55 			tst_res(TINFO, "Test runtime is over, exiting");
56 			return;
57 		}
58 		w = SAFE_WRITE(SAFE_WRITE_ANY, fd, bufptr, ws);
59 		i += w;
60 	}
61 }
62 
setup(void)63 static void setup(void)
64 {
65 	struct stat sb;
66 
67 	if (tst_parse_int(str_numchildren, &numchildren, 1, INT_MAX))
68 		tst_brk(TBROK, "Invalid number of children '%s'", str_numchildren);
69 
70 	if (tst_parse_filesize(str_writesize, &writesize, 1, LLONG_MAX))
71 		tst_brk(TBROK, "Invalid write blocks size '%s'", str_writesize);
72 
73 	if (tst_parse_filesize(str_filesize, &filesize, 1, LLONG_MAX))
74 		tst_brk(TBROK, "Invalid file size '%s'", str_filesize);
75 
76 	if (tst_parse_filesize(str_offset, &offset, 0, LLONG_MAX))
77 		tst_brk(TBROK, "Invalid file offset '%s'", str_offset);
78 
79 	SAFE_STAT(".", &sb);
80 	alignment = sb.st_blksize;
81 
82 	run_child = SAFE_MMAP(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
83 
84 	tst_res(TINFO, "Dirtying free blocks");
85 	dirty_freeblocks(100 * 1024 * 1024);
86 }
87 
cleanup(void)88 static void cleanup(void)
89 {
90 	if (run_child) {
91 		*run_child = 0;
92 		SAFE_MUNMAP((void *)run_child, sizeof(int));
93 	}
94 }
95 
run(void)96 static void run(void)
97 {
98 	char *filename = "dio_sparse";
99 	int fd;
100 	int i;
101 
102 	fd = SAFE_OPEN(filename, O_DIRECT | O_WRONLY | O_CREAT, 0666);
103 	SAFE_FTRUNCATE(fd, filesize);
104 
105 	*run_child = 1;
106 
107 	for (i = 0; i < numchildren; i++) {
108 		if (!SAFE_FORK()) {
109 			io_read(filename, filesize, run_child);
110 			return;
111 		}
112 	}
113 
114 	dio_sparse(fd, alignment, filesize, writesize, offset);
115 	*run_child = 0;
116 
117 	if (!tst_validate_children(numchildren))
118 		tst_res(TPASS, "All bytes read were zeroed");
119 }
120 
121 static struct tst_test test = {
122 	.test_all = run,
123 	.setup = setup,
124 	.cleanup = cleanup,
125 	.needs_tmpdir = 1,
126 	.forks_child = 1,
127 	.options = (struct tst_option[]) {
128 		{"n:", &str_numchildren, "Number of threads (default 16)"},
129 		{"w:", &str_writesize, "Size of writing blocks (default 1K)"},
130 		{"s:", &str_filesize, "Size of file (default 100M)"},
131 		{"o:", &str_offset, "File offset (default 0)"},
132 		{}
133 	},
134 	.skip_filesystems = (const char *[]) {
135 		"tmpfs",
136 		NULL
137 	},
138 	.max_runtime = 1800,
139 };
140