xref: /aosp_15_r20/external/ltp/testcases/kernel/io/writetest/writetest.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker  *
3*49cdfc7eSAndroid Build Coastguard Worker  *   Copyright (c) CHANG Industry, Inc., 2004
4*49cdfc7eSAndroid Build Coastguard Worker  *
5*49cdfc7eSAndroid Build Coastguard Worker  *   This program is free software;  you can redistribute it and/or modify
6*49cdfc7eSAndroid Build Coastguard Worker  *   it under the terms of the GNU General Public License as published by
7*49cdfc7eSAndroid Build Coastguard Worker  *   the Free Software Foundation; either version 2 of the License, or
8*49cdfc7eSAndroid Build Coastguard Worker  *   (at your option) any later version.
9*49cdfc7eSAndroid Build Coastguard Worker  *
10*49cdfc7eSAndroid Build Coastguard Worker  *   This program is distributed in the hope that it will be useful,
11*49cdfc7eSAndroid Build Coastguard Worker  *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12*49cdfc7eSAndroid Build Coastguard Worker  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13*49cdfc7eSAndroid Build Coastguard Worker  *   the GNU General Public License for more details.
14*49cdfc7eSAndroid Build Coastguard Worker  *
15*49cdfc7eSAndroid Build Coastguard Worker  *   You should have received a copy of the GNU General Public License
16*49cdfc7eSAndroid Build Coastguard Worker  *   along with this program;  if not, write to the Free Software
17*49cdfc7eSAndroid Build Coastguard Worker  *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18*49cdfc7eSAndroid Build Coastguard Worker  */
19*49cdfc7eSAndroid Build Coastguard Worker 
20*49cdfc7eSAndroid Build Coastguard Worker /*
21*49cdfc7eSAndroid Build Coastguard Worker  *  FILE        : writetest.c
22*49cdfc7eSAndroid Build Coastguard Worker  *  DESCRIPTION : The purpose of this test is to verify that writes to
23*49cdfc7eSAndroid Build Coastguard Worker  *                disk occur without corruption.  It writes one 1MB
24*49cdfc7eSAndroid Build Coastguard Worker  *                buffer at a time, where each byte in the buffer is
25*49cdfc7eSAndroid Build Coastguard Worker  *                generated from a random number.  Once done
26*49cdfc7eSAndroid Build Coastguard Worker  *                completed, the file is re-opened, the random number
27*49cdfc7eSAndroid Build Coastguard Worker  *                generator is re-seeded, and the file is verified.
28*49cdfc7eSAndroid Build Coastguard Worker  *
29*49cdfc7eSAndroid Build Coastguard Worker  *  HISTORY:
30*49cdfc7eSAndroid Build Coastguard Worker  *   05/12/2004 : Written by Danny Sung <[email protected]> to
31*49cdfc7eSAndroid Build Coastguard Worker  *                verify integrity of disk writes.
32*49cdfc7eSAndroid Build Coastguard Worker  *
33*49cdfc7eSAndroid Build Coastguard Worker  */
34*49cdfc7eSAndroid Build Coastguard Worker 
35*49cdfc7eSAndroid Build Coastguard Worker #include <fcntl.h>
36*49cdfc7eSAndroid Build Coastguard Worker #include <getopt.h>
37*49cdfc7eSAndroid Build Coastguard Worker #include <stdarg.h>
38*49cdfc7eSAndroid Build Coastguard Worker #include <stdint.h>
39*49cdfc7eSAndroid Build Coastguard Worker #include <stdio.h>
40*49cdfc7eSAndroid Build Coastguard Worker #include <stdlib.h>
41*49cdfc7eSAndroid Build Coastguard Worker #include <string.h>
42*49cdfc7eSAndroid Build Coastguard Worker #include <time.h>
43*49cdfc7eSAndroid Build Coastguard Worker #include <unistd.h>
44*49cdfc7eSAndroid Build Coastguard Worker 
45*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
46*49cdfc7eSAndroid Build Coastguard Worker 
47*49cdfc7eSAndroid Build Coastguard Worker #define BLOCKSIZE (1024*1024)
48*49cdfc7eSAndroid Build Coastguard Worker #define FILE_OUT    "fileout"
49*49cdfc7eSAndroid Build Coastguard Worker #define FILE_MODE   0644
50*49cdfc7eSAndroid Build Coastguard Worker #define MAX_FILENAME_LEN 1024
51*49cdfc7eSAndroid Build Coastguard Worker 
52*49cdfc7eSAndroid Build Coastguard Worker int Verbosity = 0;
53*49cdfc7eSAndroid Build Coastguard Worker int DefaultSeed = 0;
54*49cdfc7eSAndroid Build Coastguard Worker char Filename[MAX_FILENAME_LEN] = FILE_OUT;
55*49cdfc7eSAndroid Build Coastguard Worker off_t NumBlocks = 1;
56*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "writetest";
57*49cdfc7eSAndroid Build Coastguard Worker int TST_TOTAL = 2;
58*49cdfc7eSAndroid Build Coastguard Worker 
buf_init(void)59*49cdfc7eSAndroid Build Coastguard Worker void buf_init(void)
60*49cdfc7eSAndroid Build Coastguard Worker {
61*49cdfc7eSAndroid Build Coastguard Worker 	static int seed = 0;
62*49cdfc7eSAndroid Build Coastguard Worker 	if (seed == 0)
63*49cdfc7eSAndroid Build Coastguard Worker 		seed = DefaultSeed;
64*49cdfc7eSAndroid Build Coastguard Worker 	srand(seed);
65*49cdfc7eSAndroid Build Coastguard Worker }
66*49cdfc7eSAndroid Build Coastguard Worker 
buf_fill(uint8_t * buf)67*49cdfc7eSAndroid Build Coastguard Worker void buf_fill(uint8_t * buf)
68*49cdfc7eSAndroid Build Coastguard Worker {
69*49cdfc7eSAndroid Build Coastguard Worker 	int i;
70*49cdfc7eSAndroid Build Coastguard Worker 	for (i = 0; i < BLOCKSIZE; i++) {
71*49cdfc7eSAndroid Build Coastguard Worker 		*buf = (rand() & 0xff);
72*49cdfc7eSAndroid Build Coastguard Worker 		buf++;
73*49cdfc7eSAndroid Build Coastguard Worker 	}
74*49cdfc7eSAndroid Build Coastguard Worker }
75*49cdfc7eSAndroid Build Coastguard Worker 
write_file(off_t num_blocks,const char * filename)76*49cdfc7eSAndroid Build Coastguard Worker int write_file(off_t num_blocks, const char *filename)
77*49cdfc7eSAndroid Build Coastguard Worker {
78*49cdfc7eSAndroid Build Coastguard Worker 	int fd;
79*49cdfc7eSAndroid Build Coastguard Worker 	int ret = 0;
80*49cdfc7eSAndroid Build Coastguard Worker 	off_t block;
81*49cdfc7eSAndroid Build Coastguard Worker 	uint8_t buf[BLOCKSIZE];
82*49cdfc7eSAndroid Build Coastguard Worker 
83*49cdfc7eSAndroid Build Coastguard Worker 	fd = open(filename, O_RDWR | O_CREAT | O_TRUNC | O_LARGEFILE,
84*49cdfc7eSAndroid Build Coastguard Worker 		  FILE_MODE);
85*49cdfc7eSAndroid Build Coastguard Worker 	if (fd < 0) {
86*49cdfc7eSAndroid Build Coastguard Worker 		perror(TCID);
87*49cdfc7eSAndroid Build Coastguard Worker 		return (-1);
88*49cdfc7eSAndroid Build Coastguard Worker 	}
89*49cdfc7eSAndroid Build Coastguard Worker 	for (block = 0; block < num_blocks; block++) {
90*49cdfc7eSAndroid Build Coastguard Worker 		int rv;
91*49cdfc7eSAndroid Build Coastguard Worker 		if (Verbosity > 2)
92*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TINFO, "Block: %lld/%lld  (%3lld%%)\r",
93*49cdfc7eSAndroid Build Coastguard Worker 				 (long long int)block,
94*49cdfc7eSAndroid Build Coastguard Worker 				 (long long int)num_blocks,
95*49cdfc7eSAndroid Build Coastguard Worker 				 (long long int)(block * 100 / num_blocks));
96*49cdfc7eSAndroid Build Coastguard Worker 		buf_fill(buf);
97*49cdfc7eSAndroid Build Coastguard Worker 		rv = write(fd, buf, BLOCKSIZE);
98*49cdfc7eSAndroid Build Coastguard Worker 		if (rv != BLOCKSIZE) {
99*49cdfc7eSAndroid Build Coastguard Worker 			ret = -1;
100*49cdfc7eSAndroid Build Coastguard Worker 			break;
101*49cdfc7eSAndroid Build Coastguard Worker 		}
102*49cdfc7eSAndroid Build Coastguard Worker 	}
103*49cdfc7eSAndroid Build Coastguard Worker 	if (Verbosity > 2)
104*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TINFO, "Block: %lld/%lld  (%3lld%%)\r",
105*49cdfc7eSAndroid Build Coastguard Worker 			 (long long int)block, (long long int)num_blocks,
106*49cdfc7eSAndroid Build Coastguard Worker 			 (long long int)(block * 100 / num_blocks));
107*49cdfc7eSAndroid Build Coastguard Worker 	close(fd);
108*49cdfc7eSAndroid Build Coastguard Worker 	return (ret);
109*49cdfc7eSAndroid Build Coastguard Worker }
110*49cdfc7eSAndroid Build Coastguard Worker 
verify_file(off_t num_blocks,const char * filename)111*49cdfc7eSAndroid Build Coastguard Worker int verify_file(off_t num_blocks, const char *filename)
112*49cdfc7eSAndroid Build Coastguard Worker {
113*49cdfc7eSAndroid Build Coastguard Worker 	int fd;
114*49cdfc7eSAndroid Build Coastguard Worker 	int ret = 0;
115*49cdfc7eSAndroid Build Coastguard Worker 	off_t block;
116*49cdfc7eSAndroid Build Coastguard Worker 	uint8_t buf_actual[BLOCKSIZE];
117*49cdfc7eSAndroid Build Coastguard Worker 	char buf_read[BLOCKSIZE];
118*49cdfc7eSAndroid Build Coastguard Worker 
119*49cdfc7eSAndroid Build Coastguard Worker 	fd = open(filename, O_RDONLY);
120*49cdfc7eSAndroid Build Coastguard Worker 	if (fd < 0) {
121*49cdfc7eSAndroid Build Coastguard Worker 		perror(TCID);
122*49cdfc7eSAndroid Build Coastguard Worker 		return (-1);
123*49cdfc7eSAndroid Build Coastguard Worker 	}
124*49cdfc7eSAndroid Build Coastguard Worker 	for (block = 0; block < num_blocks; block++) {
125*49cdfc7eSAndroid Build Coastguard Worker 		int rv;
126*49cdfc7eSAndroid Build Coastguard Worker 		int n;
127*49cdfc7eSAndroid Build Coastguard Worker 		if (Verbosity > 2)
128*49cdfc7eSAndroid Build Coastguard Worker 			tst_resm(TINFO, "Block: %lld/%lld  (%3lld%%)\r",
129*49cdfc7eSAndroid Build Coastguard Worker 				 (long long int)block,
130*49cdfc7eSAndroid Build Coastguard Worker 				 (long long int)num_blocks,
131*49cdfc7eSAndroid Build Coastguard Worker 				 (long long int)(block * 100 / num_blocks));
132*49cdfc7eSAndroid Build Coastguard Worker 		buf_fill(buf_actual);
133*49cdfc7eSAndroid Build Coastguard Worker 		rv = read(fd, buf_read, BLOCKSIZE);
134*49cdfc7eSAndroid Build Coastguard Worker 		if (rv != BLOCKSIZE) {
135*49cdfc7eSAndroid Build Coastguard Worker 			ret = -1;
136*49cdfc7eSAndroid Build Coastguard Worker 			break;
137*49cdfc7eSAndroid Build Coastguard Worker 		}
138*49cdfc7eSAndroid Build Coastguard Worker 		for (n = 0; n < BLOCKSIZE; n++) {
139*49cdfc7eSAndroid Build Coastguard Worker 			int ba, br;
140*49cdfc7eSAndroid Build Coastguard Worker 			ba = buf_actual[n] & 0xff;
141*49cdfc7eSAndroid Build Coastguard Worker 			br = buf_read[n] & 0xff;
142*49cdfc7eSAndroid Build Coastguard Worker 			if (ba != br) {
143*49cdfc7eSAndroid Build Coastguard Worker 				if (Verbosity > 2)
144*49cdfc7eSAndroid Build Coastguard Worker 					tst_resm(TINFO,
145*49cdfc7eSAndroid Build Coastguard Worker 						 "Mismatch: block=%lld +%d bytes offset=%lld read: %02xh actual: %02xh\n",
146*49cdfc7eSAndroid Build Coastguard Worker 						 (long long int)block, n,
147*49cdfc7eSAndroid Build Coastguard Worker 						 (long long
148*49cdfc7eSAndroid Build Coastguard Worker 						  int)((block * BLOCKSIZE) + n),
149*49cdfc7eSAndroid Build Coastguard Worker 						 br, ba);
150*49cdfc7eSAndroid Build Coastguard Worker 				ret++;
151*49cdfc7eSAndroid Build Coastguard Worker 			}
152*49cdfc7eSAndroid Build Coastguard Worker 		}
153*49cdfc7eSAndroid Build Coastguard Worker 	}
154*49cdfc7eSAndroid Build Coastguard Worker 	close(fd);
155*49cdfc7eSAndroid Build Coastguard Worker 	if (Verbosity > 2)
156*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TINFO, "Block: %lld/%lld  (%3lld%%)\r",
157*49cdfc7eSAndroid Build Coastguard Worker 			 (long long int)block, (long long int)num_blocks,
158*49cdfc7eSAndroid Build Coastguard Worker 			 (long long int)(block * 100 / num_blocks));
159*49cdfc7eSAndroid Build Coastguard Worker 	return (ret);
160*49cdfc7eSAndroid Build Coastguard Worker }
161*49cdfc7eSAndroid Build Coastguard Worker 
usage(void)162*49cdfc7eSAndroid Build Coastguard Worker void usage(void)
163*49cdfc7eSAndroid Build Coastguard Worker {
164*49cdfc7eSAndroid Build Coastguard Worker 	printf("%s [-v] [-b blocks] [-s seed] [-o filename]\n", TCID);
165*49cdfc7eSAndroid Build Coastguard Worker 	printf("\n"
166*49cdfc7eSAndroid Build Coastguard Worker 	       "   -v          - increase verbosity level\n"
167*49cdfc7eSAndroid Build Coastguard Worker 	       "   blocks      - number of blocks to write\n"
168*49cdfc7eSAndroid Build Coastguard Worker 	       "   seed        - seed to use (0 to use timestamp)\n"
169*49cdfc7eSAndroid Build Coastguard Worker 	       "   filename    - name of output file\n");
170*49cdfc7eSAndroid Build Coastguard Worker }
171*49cdfc7eSAndroid Build Coastguard Worker 
parse_args(int argc,char ** argv)172*49cdfc7eSAndroid Build Coastguard Worker void parse_args(int argc, char **argv)
173*49cdfc7eSAndroid Build Coastguard Worker {
174*49cdfc7eSAndroid Build Coastguard Worker 	int c;
175*49cdfc7eSAndroid Build Coastguard Worker 	TCID = argv[0];
176*49cdfc7eSAndroid Build Coastguard Worker 
177*49cdfc7eSAndroid Build Coastguard Worker 	while (1) {
178*49cdfc7eSAndroid Build Coastguard Worker 		int option_index = 0;
179*49cdfc7eSAndroid Build Coastguard Worker 		static struct option long_options[] = {
180*49cdfc7eSAndroid Build Coastguard Worker 			{"blocks", 1, 0, 'b'},
181*49cdfc7eSAndroid Build Coastguard Worker 			{"out", 1, 0, 'o'},
182*49cdfc7eSAndroid Build Coastguard Worker 			{"seed", 1, 0, 's'},
183*49cdfc7eSAndroid Build Coastguard Worker 			{"verbose", 0, 0, 'v'},
184*49cdfc7eSAndroid Build Coastguard Worker 			{"help", 0, 0, 'h'},
185*49cdfc7eSAndroid Build Coastguard Worker 			{0, 0, 0, 0}
186*49cdfc7eSAndroid Build Coastguard Worker 		};
187*49cdfc7eSAndroid Build Coastguard Worker 		c = getopt_long(argc, argv, "hvb:o:s:", long_options,
188*49cdfc7eSAndroid Build Coastguard Worker 				&option_index);
189*49cdfc7eSAndroid Build Coastguard Worker 		if (c == -1)
190*49cdfc7eSAndroid Build Coastguard Worker 			break;
191*49cdfc7eSAndroid Build Coastguard Worker 		switch (c) {
192*49cdfc7eSAndroid Build Coastguard Worker 		case 'b':
193*49cdfc7eSAndroid Build Coastguard Worker 			NumBlocks = strtoul(optarg, NULL, 0);
194*49cdfc7eSAndroid Build Coastguard Worker 			break;
195*49cdfc7eSAndroid Build Coastguard Worker 		case 'o':
196*49cdfc7eSAndroid Build Coastguard Worker 			strncpy(Filename, optarg, MAX_FILENAME_LEN);
197*49cdfc7eSAndroid Build Coastguard Worker 			break;
198*49cdfc7eSAndroid Build Coastguard Worker 		case 's':
199*49cdfc7eSAndroid Build Coastguard Worker 			DefaultSeed = strtoul(optarg, NULL, 0);
200*49cdfc7eSAndroid Build Coastguard Worker 			break;
201*49cdfc7eSAndroid Build Coastguard Worker 		case 'v':
202*49cdfc7eSAndroid Build Coastguard Worker 			Verbosity++;
203*49cdfc7eSAndroid Build Coastguard Worker 			break;
204*49cdfc7eSAndroid Build Coastguard Worker 		case 'h':
205*49cdfc7eSAndroid Build Coastguard Worker 		default:
206*49cdfc7eSAndroid Build Coastguard Worker 			usage();
207*49cdfc7eSAndroid Build Coastguard Worker 			exit(-1);
208*49cdfc7eSAndroid Build Coastguard Worker 		}
209*49cdfc7eSAndroid Build Coastguard Worker 	}
210*49cdfc7eSAndroid Build Coastguard Worker }
211*49cdfc7eSAndroid Build Coastguard Worker 
setup()212*49cdfc7eSAndroid Build Coastguard Worker void setup()
213*49cdfc7eSAndroid Build Coastguard Worker {
214*49cdfc7eSAndroid Build Coastguard Worker 	tst_tmpdir();
215*49cdfc7eSAndroid Build Coastguard Worker 
216*49cdfc7eSAndroid Build Coastguard Worker }
217*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)218*49cdfc7eSAndroid Build Coastguard Worker void cleanup(void)
219*49cdfc7eSAndroid Build Coastguard Worker {
220*49cdfc7eSAndroid Build Coastguard Worker 	tst_rmdir();
221*49cdfc7eSAndroid Build Coastguard Worker 	tst_exit();
222*49cdfc7eSAndroid Build Coastguard Worker }
223*49cdfc7eSAndroid Build Coastguard Worker 
main(int argc,char * argv[])224*49cdfc7eSAndroid Build Coastguard Worker int main(int argc, char *argv[])
225*49cdfc7eSAndroid Build Coastguard Worker {
226*49cdfc7eSAndroid Build Coastguard Worker 	int rv;
227*49cdfc7eSAndroid Build Coastguard Worker 
228*49cdfc7eSAndroid Build Coastguard Worker 	setup();
229*49cdfc7eSAndroid Build Coastguard Worker 
230*49cdfc7eSAndroid Build Coastguard Worker 	DefaultSeed = time(NULL);
231*49cdfc7eSAndroid Build Coastguard Worker 	parse_args(argc, argv);
232*49cdfc7eSAndroid Build Coastguard Worker 	tst_resm(TINFO, "Blocks:       %lld\n", (long long int)NumBlocks);
233*49cdfc7eSAndroid Build Coastguard Worker 	tst_resm(TINFO, "Seed:         %d\n", DefaultSeed);
234*49cdfc7eSAndroid Build Coastguard Worker 	tst_resm(TINFO, "Output file: '%s'\n", Filename);
235*49cdfc7eSAndroid Build Coastguard Worker 
236*49cdfc7eSAndroid Build Coastguard Worker 	tst_resm(TINFO, "Writing %lld blocks of %d bytes to '%s'\n",
237*49cdfc7eSAndroid Build Coastguard Worker 		 (long long int)NumBlocks, BLOCKSIZE, Filename);
238*49cdfc7eSAndroid Build Coastguard Worker 	buf_init();
239*49cdfc7eSAndroid Build Coastguard Worker 	rv = write_file(NumBlocks, Filename);
240*49cdfc7eSAndroid Build Coastguard Worker 	if (rv == 0) {
241*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TPASS, "Write: Success");
242*49cdfc7eSAndroid Build Coastguard Worker 	} else {
243*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TFAIL, "Write: Failure");
244*49cdfc7eSAndroid Build Coastguard Worker 	}
245*49cdfc7eSAndroid Build Coastguard Worker 
246*49cdfc7eSAndroid Build Coastguard Worker 	tst_resm(TINFO, "Verifying %lld blocks in '%s'\n",
247*49cdfc7eSAndroid Build Coastguard Worker 		 (long long int)NumBlocks, Filename);
248*49cdfc7eSAndroid Build Coastguard Worker 	buf_init();
249*49cdfc7eSAndroid Build Coastguard Worker 	rv = verify_file(NumBlocks, Filename);
250*49cdfc7eSAndroid Build Coastguard Worker 	if (rv == 0) {
251*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TPASS, "Verify: Success\n");
252*49cdfc7eSAndroid Build Coastguard Worker 	} else {
253*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TFAIL, "Verify: Failure");
254*49cdfc7eSAndroid Build Coastguard Worker 		tst_resm(TINFO, "Total mismatches: %d bytes\n", rv);
255*49cdfc7eSAndroid Build Coastguard Worker 	}
256*49cdfc7eSAndroid Build Coastguard Worker 
257*49cdfc7eSAndroid Build Coastguard Worker 	cleanup();
258*49cdfc7eSAndroid Build Coastguard Worker 	return 0;
259*49cdfc7eSAndroid Build Coastguard Worker }
260