1f08c3bdfSopenharmony_ci
2f08c3bdfSopenharmony_ci#include <stdio.h>
3f08c3bdfSopenharmony_ci#include <stdlib.h>
4f08c3bdfSopenharmony_ci#include <unistd.h>
5f08c3bdfSopenharmony_ci#include <string.h>
6f08c3bdfSopenharmony_ci#include <ctype.h>
7f08c3bdfSopenharmony_ci#include <errno.h>
8f08c3bdfSopenharmony_ci#include <math.h>
9f08c3bdfSopenharmony_ci#include <time.h>
10f08c3bdfSopenharmony_ci#include <ftw.h>
11f08c3bdfSopenharmony_ci#include <sys/types.h>
12f08c3bdfSopenharmony_ci#include <sys/stat.h>
13f08c3bdfSopenharmony_ci#include <fcntl.h>
14f08c3bdfSopenharmony_ci#include <sys/ioctl.h>
15f08c3bdfSopenharmony_ci#include <linux/kd.h>
16f08c3bdfSopenharmony_ci#include <linux/errno.h>
17f08c3bdfSopenharmony_ci
18f08c3bdfSopenharmony_ci#include "Ltpfs.h"
19f08c3bdfSopenharmony_ci
20f08c3bdfSopenharmony_ci#define M_2PI (M_PI*2)
21f08c3bdfSopenharmony_ci#define MAXN 4096
22f08c3bdfSopenharmony_ci#define MAXFSIZE 1024 * 192
23f08c3bdfSopenharmony_ci#define FILE_CREATE_COUNT 256
24f08c3bdfSopenharmony_ci#define FAIL 0
25f08c3bdfSopenharmony_ci#define SUCCESS 1
26f08c3bdfSopenharmony_ci#define MAXNUM   5000
27f08c3bdfSopenharmony_ci#define BUFFSIZE 8192
28f08c3bdfSopenharmony_ci#define AVEFSIZE (MAXFSIZE/2)
29f08c3bdfSopenharmony_ci#define POOLDISKSPACE (AVEFSIZE*128)
30f08c3bdfSopenharmony_ci#define MAXERROR  1024
31f08c3bdfSopenharmony_ci#define FILES_ONLY 0x01
32f08c3bdfSopenharmony_ci#define ALL        0x00
33f08c3bdfSopenharmony_ci
34f08c3bdfSopenharmony_ci// Globals
35f08c3bdfSopenharmony_ci
36f08c3bdfSopenharmony_cichar wbuf[MAXFSIZE];
37f08c3bdfSopenharmony_ciint startc = 0;
38f08c3bdfSopenharmony_ciint showchar[] = { 124, 47, 45, 92, 124, 47, 45, 92 };
39f08c3bdfSopenharmony_ci
40f08c3bdfSopenharmony_ciint nullFileHandle;
41f08c3bdfSopenharmony_cistatic int openlog[2] = { 0, 0 };
42f08c3bdfSopenharmony_ci
43f08c3bdfSopenharmony_ciint cFileCount, dFileCount, errorCount;
44f08c3bdfSopenharmony_cistatic int disk_space_pool = 0;
45f08c3bdfSopenharmony_cichar rootPath[BUFFSIZE];
46f08c3bdfSopenharmony_ci
47f08c3bdfSopenharmony_ciint LTP_fs_open_block_device(void);
48f08c3bdfSopenharmony_ciint do_fs_thump_tests(char *path);
49f08c3bdfSopenharmony_ciint do_create_file_test(char *path);
50f08c3bdfSopenharmony_ciint makedir(char *dir1);
51f08c3bdfSopenharmony_ciint changedir(char *dir);
52f08c3bdfSopenharmony_ciint do_random_access_test(int maxNum);
53f08c3bdfSopenharmony_ciint do_random_create_delete(int maxNum);
54f08c3bdfSopenharmony_ciint create_file(char *filename);
55f08c3bdfSopenharmony_ciint delete_file(char *filename);
56f08c3bdfSopenharmony_ciint gen_random_file_size(int min, int max);
57f08c3bdfSopenharmony_ciint open_read_close(char *fname);
58f08c3bdfSopenharmony_ciint create_or_delete(char *fname);
59f08c3bdfSopenharmony_ciint do_tree_cleanup(char *path, int flag);
60f08c3bdfSopenharmony_ciint cleanup_files(char *file, struct stat *statBuff, int flag);
61f08c3bdfSopenharmony_ciint cleanup_dirs(char *file, struct stat *statBuff, int flag);
62f08c3bdfSopenharmony_ci
63f08c3bdfSopenharmony_ciint ltp_block_dev_handle = 0;	/* handle to LTP Test block device */
64f08c3bdfSopenharmony_ciint ltp_fileHandle = 0;
65f08c3bdfSopenharmony_cichar *fileBuf;
66f08c3bdfSopenharmony_ci
67f08c3bdfSopenharmony_ciint main(int argc, char **argv)
68f08c3bdfSopenharmony_ci{
69f08c3bdfSopenharmony_ci
70f08c3bdfSopenharmony_ci	ltpdev_cmd_t cmd = { 0, 0 };
71f08c3bdfSopenharmony_ci	int rc, i, tmpHandle;
72f08c3bdfSopenharmony_ci	struct stat statBuf;
73f08c3bdfSopenharmony_ci
74f08c3bdfSopenharmony_ci	printf("[%s] - Running test program\n", argv[0]);
75f08c3bdfSopenharmony_ci
76f08c3bdfSopenharmony_ci	rc = LTP_fs_open_block_device();
77f08c3bdfSopenharmony_ci
78f08c3bdfSopenharmony_ci	if (!rc) {
79f08c3bdfSopenharmony_ci
80f08c3bdfSopenharmony_ci		ltp_block_dev_handle = open(LTP_FS_DEVICE_NAME, O_RDWR);
81f08c3bdfSopenharmony_ci
82f08c3bdfSopenharmony_ci		if (ltp_block_dev_handle < 0) {
83f08c3bdfSopenharmony_ci			printf
84f08c3bdfSopenharmony_ci			    ("ERROR: Open of device %s failed %d errno = %d\n",
85f08c3bdfSopenharmony_ci			     LTP_FS_DEVICE_NAME, ltp_block_dev_handle, errno);
86f08c3bdfSopenharmony_ci		} else {
87f08c3bdfSopenharmony_ci			rc = ioctl(ltp_block_dev_handle, LTPAIODEV_CMD, &cmd);
88f08c3bdfSopenharmony_ci
89f08c3bdfSopenharmony_ci			printf("return from AIO ioctl %d \n", rc);
90f08c3bdfSopenharmony_ci
91f08c3bdfSopenharmony_ci			rc = ioctl(ltp_block_dev_handle, LTPBIODEV_CMD, &cmd);
92f08c3bdfSopenharmony_ci
93f08c3bdfSopenharmony_ci			printf("return from BIO ioctl %d \n", rc);
94f08c3bdfSopenharmony_ci		}
95f08c3bdfSopenharmony_ci
96f08c3bdfSopenharmony_ci	} else {
97f08c3bdfSopenharmony_ci		printf("ERROR: Create/open block device failed\n");
98f08c3bdfSopenharmony_ci	}
99f08c3bdfSopenharmony_ci
100f08c3bdfSopenharmony_ci	ltp_fileHandle =
101f08c3bdfSopenharmony_ci	    open("/tmp/testfile", O_CREAT | O_RDWR | O_SYNC | FASYNC,
102f08c3bdfSopenharmony_ci		 S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
103f08c3bdfSopenharmony_ci
104f08c3bdfSopenharmony_ci	if (ltp_fileHandle > 0) {
105f08c3bdfSopenharmony_ci
106f08c3bdfSopenharmony_ci		tmpHandle = open("/usr/include/ctype.h", O_RDONLY);
107f08c3bdfSopenharmony_ci
108f08c3bdfSopenharmony_ci		if (tmpHandle > 0) {
109f08c3bdfSopenharmony_ci
110f08c3bdfSopenharmony_ci			rc = fstat(tmpHandle, &statBuf);
111f08c3bdfSopenharmony_ci
112f08c3bdfSopenharmony_ci			if (!rc) {
113f08c3bdfSopenharmony_ci				fileBuf = malloc(statBuf.st_size);
114f08c3bdfSopenharmony_ci
115f08c3bdfSopenharmony_ci				if (fileBuf) {
116f08c3bdfSopenharmony_ci
117f08c3bdfSopenharmony_ci					read(tmpHandle, fileBuf,
118f08c3bdfSopenharmony_ci					     statBuf.st_size);
119f08c3bdfSopenharmony_ci					close(tmpHandle);
120f08c3bdfSopenharmony_ci					write(ltp_fileHandle, fileBuf,
121f08c3bdfSopenharmony_ci					      statBuf.st_size);
122f08c3bdfSopenharmony_ci
123f08c3bdfSopenharmony_ci					for (i = 0; i < 100; i++) {
124f08c3bdfSopenharmony_ci						read(ltp_fileHandle, fileBuf,
125f08c3bdfSopenharmony_ci						     statBuf.st_size * i);
126f08c3bdfSopenharmony_ci						write(ltp_fileHandle, fileBuf,
127f08c3bdfSopenharmony_ci						      statBuf.st_size * i);
128f08c3bdfSopenharmony_ci					}
129f08c3bdfSopenharmony_ci				}
130f08c3bdfSopenharmony_ci
131f08c3bdfSopenharmony_ci			}
132f08c3bdfSopenharmony_ci
133f08c3bdfSopenharmony_ci		} else {
134f08c3bdfSopenharmony_ci			printf("ERROR: Create/open file failed\n");
135f08c3bdfSopenharmony_ci		}
136f08c3bdfSopenharmony_ci	}
137f08c3bdfSopenharmony_ci
138f08c3bdfSopenharmony_ci	printf("*** Starting FileSystem thump tests....****\n");
139f08c3bdfSopenharmony_ci	printf("*** Please be patient, this may take a little while... ***\n");
140f08c3bdfSopenharmony_ci
141f08c3bdfSopenharmony_ci	for (i = 1; i < argc; i++) {
142f08c3bdfSopenharmony_ci		printf("Running test %d of %d on FileSystem %s \n", i, argc - 1,
143f08c3bdfSopenharmony_ci		       argv[i]);
144f08c3bdfSopenharmony_ci		if (strcmp(argv[i], "|") != 0) {
145f08c3bdfSopenharmony_ci			strcpy(rootPath, argv[i]);
146f08c3bdfSopenharmony_ci			rc = do_fs_thump_tests(argv[i]);
147f08c3bdfSopenharmony_ci			if (rc != 0 && rc != ENOSPC) {
148f08c3bdfSopenharmony_ci				printf
149f08c3bdfSopenharmony_ci				    ("ERROR: Failed on FileSystem %s with errno %d \n",
150f08c3bdfSopenharmony_ci				     argv[i], rc);
151f08c3bdfSopenharmony_ci			}
152f08c3bdfSopenharmony_ci		} else {
153f08c3bdfSopenharmony_ci			printf("Test Program complete..\n");
154f08c3bdfSopenharmony_ci			break;
155f08c3bdfSopenharmony_ci		}
156f08c3bdfSopenharmony_ci
157f08c3bdfSopenharmony_ci	}
158f08c3bdfSopenharmony_ci
159f08c3bdfSopenharmony_ci	printf("Test Program complete..\n");
160f08c3bdfSopenharmony_ci
161f08c3bdfSopenharmony_ci	return 0;
162f08c3bdfSopenharmony_ci}
163f08c3bdfSopenharmony_ci
164f08c3bdfSopenharmony_ciint do_fs_thump_tests(char *path)
165f08c3bdfSopenharmony_ci{
166f08c3bdfSopenharmony_ci	int rc = 0;
167f08c3bdfSopenharmony_ci
168f08c3bdfSopenharmony_ci	printf("Changing to directory %s \n", path);
169f08c3bdfSopenharmony_ci
170f08c3bdfSopenharmony_ci	changedir(path);
171f08c3bdfSopenharmony_ci
172f08c3bdfSopenharmony_ci	cFileCount = 0;
173f08c3bdfSopenharmony_ci	dFileCount = 0;
174f08c3bdfSopenharmony_ci
175f08c3bdfSopenharmony_ci	rc |= do_create_file_test(path);
176f08c3bdfSopenharmony_ci	rc |= do_random_access_test(MAXNUM);
177f08c3bdfSopenharmony_ci	rc |= do_tree_cleanup(path, FILES_ONLY);
178f08c3bdfSopenharmony_ci	rc |= do_random_create_delete(MAXNUM);
179f08c3bdfSopenharmony_ci	rc |= do_tree_cleanup(path, ALL);
180f08c3bdfSopenharmony_ci
181f08c3bdfSopenharmony_ci	return rc;
182f08c3bdfSopenharmony_ci
183f08c3bdfSopenharmony_ci}
184f08c3bdfSopenharmony_ci
185f08c3bdfSopenharmony_ciint do_tree_cleanup(char *path, int flag)
186f08c3bdfSopenharmony_ci{
187f08c3bdfSopenharmony_ci
188f08c3bdfSopenharmony_ci	if (flag == FILES_ONLY) {
189f08c3bdfSopenharmony_ci		printf("Cleaning up test files...\n");
190f08c3bdfSopenharmony_ci		ftw(path, (void *)cleanup_files, MAXNUM);
191f08c3bdfSopenharmony_ci	} else {
192f08c3bdfSopenharmony_ci		printf("Cleaning up everything in the test directory...\n");
193f08c3bdfSopenharmony_ci		ftw(path, (void *)cleanup_files, MAXNUM);
194f08c3bdfSopenharmony_ci		ftw(path, (void *)cleanup_dirs, MAXNUM);
195f08c3bdfSopenharmony_ci	}
196f08c3bdfSopenharmony_ci
197f08c3bdfSopenharmony_ci	return 0;
198f08c3bdfSopenharmony_ci}
199f08c3bdfSopenharmony_ci
200f08c3bdfSopenharmony_ciint cleanup_files(char *file, struct stat *statBuff, int flag)
201f08c3bdfSopenharmony_ci{
202f08c3bdfSopenharmony_ci	int rc = 0;
203f08c3bdfSopenharmony_ci
204f08c3bdfSopenharmony_ci	if (flag == FTW_F) {
205f08c3bdfSopenharmony_ci		if (unlink(file)) {
206f08c3bdfSopenharmony_ci			printf("ERROR:%d removing file %s\n", errno, file);
207f08c3bdfSopenharmony_ci		}
208f08c3bdfSopenharmony_ci	}
209f08c3bdfSopenharmony_ci
210f08c3bdfSopenharmony_ci	return rc;
211f08c3bdfSopenharmony_ci}
212f08c3bdfSopenharmony_ci
213f08c3bdfSopenharmony_ciint cleanup_dirs(char *file, struct stat *statBuff, int flag)
214f08c3bdfSopenharmony_ci{
215f08c3bdfSopenharmony_ci	int rc = 0;
216f08c3bdfSopenharmony_ci
217f08c3bdfSopenharmony_ci	//printf("%s:Cleaning up directory %s \n", __FUNCTION__, file);
218f08c3bdfSopenharmony_ci
219f08c3bdfSopenharmony_ci	if (strcmp(rootPath, file) == 0) {
220f08c3bdfSopenharmony_ci		return 0;
221f08c3bdfSopenharmony_ci	}
222f08c3bdfSopenharmony_ci
223f08c3bdfSopenharmony_ci	if (flag == FTW_F) {
224f08c3bdfSopenharmony_ci		if (unlink(file)) {
225f08c3bdfSopenharmony_ci			printf("ERROR:%d removing file %s\n", errno, file);
226f08c3bdfSopenharmony_ci		}
227f08c3bdfSopenharmony_ci	} else if (flag == FTW_D) {
228f08c3bdfSopenharmony_ci		changedir(file);
229f08c3bdfSopenharmony_ci		ftw(file, (void *)cleanup_dirs, MAXNUM);
230f08c3bdfSopenharmony_ci		rmdir(file);
231f08c3bdfSopenharmony_ci
232f08c3bdfSopenharmony_ci	} else {
233f08c3bdfSopenharmony_ci		printf("No idea what we found here\n");
234f08c3bdfSopenharmony_ci	}
235f08c3bdfSopenharmony_ci
236f08c3bdfSopenharmony_ci	return rc;
237f08c3bdfSopenharmony_ci}
238f08c3bdfSopenharmony_ci
239f08c3bdfSopenharmony_ciint do_create_file_test(char *path)
240f08c3bdfSopenharmony_ci{
241f08c3bdfSopenharmony_ci	int i = 0;
242f08c3bdfSopenharmony_ci	int j = 0;
243f08c3bdfSopenharmony_ci	int k = 0;
244f08c3bdfSopenharmony_ci	int l = 0;
245f08c3bdfSopenharmony_ci	int rc = 0;
246f08c3bdfSopenharmony_ci
247f08c3bdfSopenharmony_ci	char dir1[MAXN];
248f08c3bdfSopenharmony_ci	char dir2[MAXN];
249f08c3bdfSopenharmony_ci	char dir3[MAXN];
250f08c3bdfSopenharmony_ci	char filename[MAXN];
251f08c3bdfSopenharmony_ci
252f08c3bdfSopenharmony_ci	time_t t;
253f08c3bdfSopenharmony_ci
254f08c3bdfSopenharmony_ci	int maxfiles = 0xFFFFFF;
255f08c3bdfSopenharmony_ci
256f08c3bdfSopenharmony_ci	time(&t);
257f08c3bdfSopenharmony_ci
258f08c3bdfSopenharmony_ci	srandom((unsigned int)getpid() ^
259f08c3bdfSopenharmony_ci		(((unsigned int)t << 16) | (unsigned int)t >> 16));
260f08c3bdfSopenharmony_ci
261f08c3bdfSopenharmony_ci	printf("Creating files...\n");
262f08c3bdfSopenharmony_ci
263f08c3bdfSopenharmony_ci	for (i = 0; i < FILE_CREATE_COUNT; i++) {
264f08c3bdfSopenharmony_ci
265f08c3bdfSopenharmony_ci		sprintf(dir1, "%2.2x", i);
266f08c3bdfSopenharmony_ci
267f08c3bdfSopenharmony_ci		makedir(dir1);
268f08c3bdfSopenharmony_ci
269f08c3bdfSopenharmony_ci		changedir(dir1);
270f08c3bdfSopenharmony_ci
271f08c3bdfSopenharmony_ci		for (j = 0; j < FILE_CREATE_COUNT; j++) {
272f08c3bdfSopenharmony_ci
273f08c3bdfSopenharmony_ci			sprintf(dir2, "%2.2x", j);
274f08c3bdfSopenharmony_ci
275f08c3bdfSopenharmony_ci			makedir(dir2);
276f08c3bdfSopenharmony_ci
277f08c3bdfSopenharmony_ci			changedir(dir2);
278f08c3bdfSopenharmony_ci
279f08c3bdfSopenharmony_ci			for (k = 0; k < FILE_CREATE_COUNT; k++) {
280f08c3bdfSopenharmony_ci
281f08c3bdfSopenharmony_ci				sprintf(dir3, "%2.2x", k);
282f08c3bdfSopenharmony_ci				makedir(dir3);
283f08c3bdfSopenharmony_ci				changedir(dir3);
284f08c3bdfSopenharmony_ci
285f08c3bdfSopenharmony_ci				for (l = 0; l < FILE_CREATE_COUNT; l++) {
286f08c3bdfSopenharmony_ci					sprintf(filename, "%s%s%s%2.2x", dir1,
287f08c3bdfSopenharmony_ci						dir2, dir3, l);
288f08c3bdfSopenharmony_ci					rc = create_file(filename);
289f08c3bdfSopenharmony_ci					if (rc != 0 || maxfiles < dFileCount++) {
290f08c3bdfSopenharmony_ci						if (rc != ENOSPC) {
291f08c3bdfSopenharmony_ci							printf
292f08c3bdfSopenharmony_ci							    ("ERROR: failed error:%d creating all the test files ! \n",
293f08c3bdfSopenharmony_ci							     errno);
294f08c3bdfSopenharmony_ci							printf
295f08c3bdfSopenharmony_ci							    ("ERROR2: rc:%d -- dFileCount:%d \n",
296f08c3bdfSopenharmony_ci							     rc, dFileCount);
297f08c3bdfSopenharmony_ci						}
298f08c3bdfSopenharmony_ci						goto end;
299f08c3bdfSopenharmony_ci					}
300f08c3bdfSopenharmony_ci				}
301f08c3bdfSopenharmony_ci				changedir("../");
302f08c3bdfSopenharmony_ci			}
303f08c3bdfSopenharmony_ci			changedir("../");
304f08c3bdfSopenharmony_ci		}
305f08c3bdfSopenharmony_ci		changedir("../");
306f08c3bdfSopenharmony_ci	}
307f08c3bdfSopenharmony_ciend:
308f08c3bdfSopenharmony_ci	fprintf(stderr, "\nTotal create files: %d\n", cFileCount);
309f08c3bdfSopenharmony_ci	printf("Done\n");
310f08c3bdfSopenharmony_ci	return rc;
311f08c3bdfSopenharmony_ci}
312f08c3bdfSopenharmony_ci
313f08c3bdfSopenharmony_ciint makedir(char *dir1)
314f08c3bdfSopenharmony_ci{
315f08c3bdfSopenharmony_ci	if (mkdir(dir1, S_IRWXU) < 0) {
316f08c3bdfSopenharmony_ci		perror(dir1);
317f08c3bdfSopenharmony_ci		return (errno);
318f08c3bdfSopenharmony_ci	}
319f08c3bdfSopenharmony_ci	return 0;
320f08c3bdfSopenharmony_ci}
321f08c3bdfSopenharmony_ci
322f08c3bdfSopenharmony_ciint changedir(char *dir)
323f08c3bdfSopenharmony_ci{
324f08c3bdfSopenharmony_ci	if (chdir(dir) < 0) {
325f08c3bdfSopenharmony_ci		perror(dir);
326f08c3bdfSopenharmony_ci		return (errno);
327f08c3bdfSopenharmony_ci	}
328f08c3bdfSopenharmony_ci
329f08c3bdfSopenharmony_ci	return 0;
330f08c3bdfSopenharmony_ci}
331f08c3bdfSopenharmony_ci
332f08c3bdfSopenharmony_ciint create_file(char *filename)
333f08c3bdfSopenharmony_ci{
334f08c3bdfSopenharmony_ci	int fileHandle;
335f08c3bdfSopenharmony_ci	int randomsize;
336f08c3bdfSopenharmony_ci
337f08c3bdfSopenharmony_ci	if ((fileHandle = creat(filename, S_IRWXU)) < 0) {
338f08c3bdfSopenharmony_ci
339f08c3bdfSopenharmony_ci		fprintf(stderr, "\nERROR line %d: Total create files: %d\n",
340f08c3bdfSopenharmony_ci			__LINE__, cFileCount);
341f08c3bdfSopenharmony_ci		perror(filename);
342f08c3bdfSopenharmony_ci		return (errno);
343f08c3bdfSopenharmony_ci	}
344f08c3bdfSopenharmony_ci
345f08c3bdfSopenharmony_ci	if ((randomsize = gen_random_file_size(0, MAXFSIZE)) < 0) {
346f08c3bdfSopenharmony_ci		randomsize = MAXFSIZE;
347f08c3bdfSopenharmony_ci	}
348f08c3bdfSopenharmony_ci	if (write(fileHandle, wbuf, randomsize) < 0) {
349f08c3bdfSopenharmony_ci
350f08c3bdfSopenharmony_ci		fprintf(stderr, "\nERROR:%d line%d: Total create files: %d\n",
351f08c3bdfSopenharmony_ci			errno, __LINE__, cFileCount);
352f08c3bdfSopenharmony_ci		close(fileHandle);
353f08c3bdfSopenharmony_ci
354f08c3bdfSopenharmony_ci		perror(filename);
355f08c3bdfSopenharmony_ci		return (errno);
356f08c3bdfSopenharmony_ci	}
357f08c3bdfSopenharmony_ci
358f08c3bdfSopenharmony_ci	cFileCount++;
359f08c3bdfSopenharmony_ci	close(fileHandle);
360f08c3bdfSopenharmony_ci	return 0;
361f08c3bdfSopenharmony_ci}
362f08c3bdfSopenharmony_ci
363f08c3bdfSopenharmony_ciint delete_file(char *filename)
364f08c3bdfSopenharmony_ci{
365f08c3bdfSopenharmony_ci	struct stat buf;
366f08c3bdfSopenharmony_ci	int st;
367f08c3bdfSopenharmony_ci
368f08c3bdfSopenharmony_ci	st = stat(filename, &buf);
369f08c3bdfSopenharmony_ci
370f08c3bdfSopenharmony_ci	if (st < 0) {
371f08c3bdfSopenharmony_ci		errorCount++;
372f08c3bdfSopenharmony_ci		printf("ERROR line %d: Getting file stats %s \n", __LINE__,
373f08c3bdfSopenharmony_ci		       filename);
374f08c3bdfSopenharmony_ci		return (-1);
375f08c3bdfSopenharmony_ci	}
376f08c3bdfSopenharmony_ci
377f08c3bdfSopenharmony_ci	disk_space_pool += buf.st_size;
378f08c3bdfSopenharmony_ci
379f08c3bdfSopenharmony_ci	if (unlink(filename) < 0) {
380f08c3bdfSopenharmony_ci		errorCount++;
381f08c3bdfSopenharmony_ci		printf("ERROR line %d: Removing file %s \n", __LINE__,
382f08c3bdfSopenharmony_ci		       filename);
383f08c3bdfSopenharmony_ci		return (-1);
384f08c3bdfSopenharmony_ci	}
385f08c3bdfSopenharmony_ci
386f08c3bdfSopenharmony_ci	dFileCount++;
387f08c3bdfSopenharmony_ci	return 0;
388f08c3bdfSopenharmony_ci}
389f08c3bdfSopenharmony_ci
390f08c3bdfSopenharmony_ciint LTP_fs_open_block_device()
391f08c3bdfSopenharmony_ci{
392f08c3bdfSopenharmony_ci	dev_t devt;
393f08c3bdfSopenharmony_ci	struct stat statbuf;
394f08c3bdfSopenharmony_ci	int rc = 0;
395f08c3bdfSopenharmony_ci
396f08c3bdfSopenharmony_ci	if (ltp_block_dev_handle == 0) {
397f08c3bdfSopenharmony_ci
398f08c3bdfSopenharmony_ci		/* check for the /dev/LTPFSTest subdir, and create if it does not exist.
399f08c3bdfSopenharmony_ci		 *
400f08c3bdfSopenharmony_ci		 * If devfs is running and mounted on /dev, these checks will all pass,
401f08c3bdfSopenharmony_ci		 * so a new node will not be created.
402f08c3bdfSopenharmony_ci		 */
403f08c3bdfSopenharmony_ci		devt = makedev(LTPMAJOR, 0);
404f08c3bdfSopenharmony_ci
405f08c3bdfSopenharmony_ci		rc = stat(LTP_FS_DEV_NODE_PATH, &statbuf);
406f08c3bdfSopenharmony_ci
407f08c3bdfSopenharmony_ci		if (rc) {
408f08c3bdfSopenharmony_ci			if (errno == ENOENT) {
409f08c3bdfSopenharmony_ci				/* dev node does not exist. */
410f08c3bdfSopenharmony_ci				rc = mkdir(LTP_FS_DEV_NODE_PATH,
411f08c3bdfSopenharmony_ci					   (S_IFDIR | S_IRWXU | S_IRGRP |
412f08c3bdfSopenharmony_ci					    S_IXGRP | S_IROTH | S_IXOTH));
413f08c3bdfSopenharmony_ci			} else {
414f08c3bdfSopenharmony_ci				printf
415f08c3bdfSopenharmony_ci				    ("ERROR: Problem with LTP FS dev directory.  Error code from stat() is %d\n\n",
416f08c3bdfSopenharmony_ci				     errno);
417f08c3bdfSopenharmony_ci			}
418f08c3bdfSopenharmony_ci
419f08c3bdfSopenharmony_ci		} else {
420f08c3bdfSopenharmony_ci			if (!(statbuf.st_mode & S_IFDIR)) {
421f08c3bdfSopenharmony_ci				rc = unlink(LTP_FS_DEV_NODE_PATH);
422f08c3bdfSopenharmony_ci				if (!rc) {
423f08c3bdfSopenharmony_ci					rc = mkdir(LTP_FS_DEV_NODE_PATH,
424f08c3bdfSopenharmony_ci						   (S_IFDIR | S_IRWXU | S_IRGRP
425f08c3bdfSopenharmony_ci						    | S_IXGRP | S_IROTH |
426f08c3bdfSopenharmony_ci						    S_IXOTH));
427f08c3bdfSopenharmony_ci				}
428f08c3bdfSopenharmony_ci			}
429f08c3bdfSopenharmony_ci		}
430f08c3bdfSopenharmony_ci
431f08c3bdfSopenharmony_ci		/*
432f08c3bdfSopenharmony_ci		 * Check for the /dev/ltp-fs/block_device node, and create if it does not
433f08c3bdfSopenharmony_ci		 * exist.
434f08c3bdfSopenharmony_ci		 */
435f08c3bdfSopenharmony_ci		rc = stat(LTP_FS_DEVICE_NAME, &statbuf);
436f08c3bdfSopenharmony_ci		if (rc) {
437f08c3bdfSopenharmony_ci			if (errno == ENOENT) {
438f08c3bdfSopenharmony_ci				/* dev node does not exist */
439f08c3bdfSopenharmony_ci				rc = mknod(LTP_FS_DEVICE_NAME,
440f08c3bdfSopenharmony_ci					   (S_IFBLK | S_IRUSR | S_IWUSR |
441f08c3bdfSopenharmony_ci					    S_IRGRP | S_IWGRP), devt);
442f08c3bdfSopenharmony_ci			} else {
443f08c3bdfSopenharmony_ci				printf
444f08c3bdfSopenharmony_ci				    ("ERROR:Problem with LTP FS block device node directory.  Error code form stat() is %d\n\n",
445f08c3bdfSopenharmony_ci				     errno);
446f08c3bdfSopenharmony_ci			}
447f08c3bdfSopenharmony_ci
448f08c3bdfSopenharmony_ci		} else {
449f08c3bdfSopenharmony_ci			/*
450f08c3bdfSopenharmony_ci			 * /dev/ltp-fs/block_device exists.  Check to make sure it is for a
451f08c3bdfSopenharmony_ci			 * block device and that it has the right major and minor.
452f08c3bdfSopenharmony_ci			 */
453f08c3bdfSopenharmony_ci			if ((!(statbuf.st_mode & S_IFBLK)) ||
454f08c3bdfSopenharmony_ci			    (statbuf.st_rdev != devt)) {
455f08c3bdfSopenharmony_ci
456f08c3bdfSopenharmony_ci				/* Recreate the dev node. */
457f08c3bdfSopenharmony_ci				rc = unlink(LTP_FS_DEVICE_NAME);
458f08c3bdfSopenharmony_ci				if (!rc) {
459f08c3bdfSopenharmony_ci					rc = mknod(LTP_FS_DEVICE_NAME,
460f08c3bdfSopenharmony_ci						   (S_IFBLK | S_IRUSR | S_IWUSR
461f08c3bdfSopenharmony_ci						    | S_IRGRP | S_IWGRP), devt);
462f08c3bdfSopenharmony_ci				}
463f08c3bdfSopenharmony_ci			}
464f08c3bdfSopenharmony_ci		}
465f08c3bdfSopenharmony_ci
466f08c3bdfSopenharmony_ci	}
467f08c3bdfSopenharmony_ci
468f08c3bdfSopenharmony_ci	return rc;
469f08c3bdfSopenharmony_ci}
470f08c3bdfSopenharmony_ci
471f08c3bdfSopenharmony_ciint gen_random_file_size(int min, int max)
472f08c3bdfSopenharmony_ci{
473f08c3bdfSopenharmony_ci	double u1, u2, z;
474f08c3bdfSopenharmony_ci	int i;
475f08c3bdfSopenharmony_ci	int ave;
476f08c3bdfSopenharmony_ci	int range;
477f08c3bdfSopenharmony_ci	int ZZ;
478f08c3bdfSopenharmony_ci	if (min >= max) {
479f08c3bdfSopenharmony_ci		return (-1);
480f08c3bdfSopenharmony_ci	}
481f08c3bdfSopenharmony_ci	range = max - min;
482f08c3bdfSopenharmony_ci	ave = range / 2;
483f08c3bdfSopenharmony_ci	for (i = 0; i < 10; i++) {
484f08c3bdfSopenharmony_ci		u1 = ((double)(random() % 1000000)) / 1000000;
485f08c3bdfSopenharmony_ci		u2 = ((double)(random() % 1000000)) / 1000000;
486f08c3bdfSopenharmony_ci		z = sqrt(-2.0 * log(u1)) * cos(M_2PI * u2);
487f08c3bdfSopenharmony_ci		ZZ = min + (ave + (z * (ave / 4)));
488f08c3bdfSopenharmony_ci		if (ZZ >= min && ZZ < max) {
489f08c3bdfSopenharmony_ci			return (ZZ);
490f08c3bdfSopenharmony_ci		}
491f08c3bdfSopenharmony_ci	}
492f08c3bdfSopenharmony_ci	return (-1);
493f08c3bdfSopenharmony_ci}
494f08c3bdfSopenharmony_ci
495f08c3bdfSopenharmony_ciint do_random_access_test(int maxNum)
496f08c3bdfSopenharmony_ci{
497f08c3bdfSopenharmony_ci	int r;
498f08c3bdfSopenharmony_ci	char fname[1024];
499f08c3bdfSopenharmony_ci	time_t t;
500f08c3bdfSopenharmony_ci	int i;
501f08c3bdfSopenharmony_ci
502f08c3bdfSopenharmony_ci	printf("Running random access test...\n");
503f08c3bdfSopenharmony_ci	changedir(rootPath);
504f08c3bdfSopenharmony_ci
505f08c3bdfSopenharmony_ci	if (maxNum < 1 || maxNum > MAXNUM) {
506f08c3bdfSopenharmony_ci		printf("out of size %d\n", maxNum);
507f08c3bdfSopenharmony_ci		return 1;
508f08c3bdfSopenharmony_ci	}
509f08c3bdfSopenharmony_ci
510f08c3bdfSopenharmony_ci	time(&t);
511f08c3bdfSopenharmony_ci	srandom((unsigned int)getpid() ^
512f08c3bdfSopenharmony_ci		(((unsigned int)t << 16) | (unsigned int)t >> 16));
513f08c3bdfSopenharmony_ci
514f08c3bdfSopenharmony_ci	if ((nullFileHandle = open("/dev/null", O_WRONLY)) < 0) {
515f08c3bdfSopenharmony_ci		perror("/dev/null");
516f08c3bdfSopenharmony_ci		return (errno);
517f08c3bdfSopenharmony_ci	}
518f08c3bdfSopenharmony_ci
519f08c3bdfSopenharmony_ci	/* 00/00/00/00 */
520f08c3bdfSopenharmony_ci	for (i = 0; i < maxNum; i++) {
521f08c3bdfSopenharmony_ci
522f08c3bdfSopenharmony_ci		r = random() % maxNum;
523f08c3bdfSopenharmony_ci
524f08c3bdfSopenharmony_ci		sprintf(fname, "00/%2.2x/%2.2x/00%2.2x%2.2x%2.2x",
525f08c3bdfSopenharmony_ci			((r >> 16) & 0xFF),
526f08c3bdfSopenharmony_ci			((r >> 8) & 0xFF),
527f08c3bdfSopenharmony_ci			((r >> 16) & 0xFF), ((r >> 8) & 0xFF), (r & 0xFF));
528f08c3bdfSopenharmony_ci
529f08c3bdfSopenharmony_ci		open_read_close(fname);
530f08c3bdfSopenharmony_ci	}
531f08c3bdfSopenharmony_ci	close(nullFileHandle);
532f08c3bdfSopenharmony_ci	printf("Success:\t%d\nFail:\t%d\n", openlog[SUCCESS], openlog[FAIL]);
533f08c3bdfSopenharmony_ci	return 0;
534f08c3bdfSopenharmony_ci}
535f08c3bdfSopenharmony_ci
536f08c3bdfSopenharmony_ciint open_read_close(char *fname)
537f08c3bdfSopenharmony_ci{
538f08c3bdfSopenharmony_ci	int fileHandle, fileHandle2;
539f08c3bdfSopenharmony_ci	char buffer[BUFFSIZE];
540f08c3bdfSopenharmony_ci	int c;
541f08c3bdfSopenharmony_ci
542f08c3bdfSopenharmony_ci	if ((fileHandle = open(fname, O_RDONLY | O_SYNC | O_ASYNC)) < 0) {
543f08c3bdfSopenharmony_ci		openlog[FAIL]++;
544f08c3bdfSopenharmony_ci		printf("ERROR:opening file %s failed %d \n", fname, errno);
545f08c3bdfSopenharmony_ci		return (errno);
546f08c3bdfSopenharmony_ci	}
547f08c3bdfSopenharmony_ci
548f08c3bdfSopenharmony_ci	if ((fileHandle2 = open(fname, O_RDONLY | O_SYNC | O_ASYNC)) < 0) {
549f08c3bdfSopenharmony_ci		openlog[FAIL]++;
550f08c3bdfSopenharmony_ci		printf("ERROR:2nd opening file %s failed %d \n", fname, errno);
551f08c3bdfSopenharmony_ci		return (errno);
552f08c3bdfSopenharmony_ci	}
553f08c3bdfSopenharmony_ci
554f08c3bdfSopenharmony_ci	openlog[SUCCESS]++;
555f08c3bdfSopenharmony_ci
556f08c3bdfSopenharmony_ci	while ((c = read(fileHandle, buffer, BUFFSIZE)) > 0) {
557f08c3bdfSopenharmony_ci		if (write(nullFileHandle, buffer, c) < 0) {
558f08c3bdfSopenharmony_ci			perror("/dev/null");
559f08c3bdfSopenharmony_ci			printf("Opened\t %d\nUnopend:\t%d\n", openlog[SUCCESS],
560f08c3bdfSopenharmony_ci			       openlog[FAIL]);
561f08c3bdfSopenharmony_ci			close(fileHandle2);
562f08c3bdfSopenharmony_ci			close(fileHandle);
563f08c3bdfSopenharmony_ci			return (errno);
564f08c3bdfSopenharmony_ci		}
565f08c3bdfSopenharmony_ci		if ((c = read(fileHandle2, buffer, BUFFSIZE)) > 0) {
566f08c3bdfSopenharmony_ci			if (write(nullFileHandle, buffer, c) < 0) {
567f08c3bdfSopenharmony_ci				perror("/dev/null");
568f08c3bdfSopenharmony_ci				printf("Opened\t %d\nUnopend:\t%d\n",
569f08c3bdfSopenharmony_ci				       openlog[SUCCESS], openlog[FAIL]);
570f08c3bdfSopenharmony_ci				close(fileHandle2);
571f08c3bdfSopenharmony_ci				close(fileHandle);
572f08c3bdfSopenharmony_ci				return (errno);
573f08c3bdfSopenharmony_ci			}
574f08c3bdfSopenharmony_ci		}
575f08c3bdfSopenharmony_ci	}
576f08c3bdfSopenharmony_ci
577f08c3bdfSopenharmony_ci	if (c < 0) {
578f08c3bdfSopenharmony_ci		perror(fname);
579f08c3bdfSopenharmony_ci		printf("Opened\t %d\nUnopend:\t%d\n", openlog[SUCCESS],
580f08c3bdfSopenharmony_ci		       openlog[FAIL]);
581f08c3bdfSopenharmony_ci		return (errno);
582f08c3bdfSopenharmony_ci	}
583f08c3bdfSopenharmony_ci
584f08c3bdfSopenharmony_ci	close(fileHandle2);
585f08c3bdfSopenharmony_ci	close(fileHandle);
586f08c3bdfSopenharmony_ci	return 0;
587f08c3bdfSopenharmony_ci}
588f08c3bdfSopenharmony_ci
589f08c3bdfSopenharmony_ciint create_or_delete(char *fname)
590f08c3bdfSopenharmony_ci{
591f08c3bdfSopenharmony_ci	int r, rc;
592f08c3bdfSopenharmony_ci
593f08c3bdfSopenharmony_ci	r = (random() & 1);
594f08c3bdfSopenharmony_ci
595f08c3bdfSopenharmony_ci	/* create */
596f08c3bdfSopenharmony_ci	if ((create_file(fname) == 0)) {
597f08c3bdfSopenharmony_ci		rc = delete_file(fname);
598f08c3bdfSopenharmony_ci	} else {
599f08c3bdfSopenharmony_ci		printf("Error: %d creating random file \n", errno);
600f08c3bdfSopenharmony_ci	}
601f08c3bdfSopenharmony_ci
602f08c3bdfSopenharmony_ci	if ((errorCount > dFileCount || errorCount > cFileCount)
603f08c3bdfSopenharmony_ci	    && (errorCount > MAXERROR)) {
604f08c3bdfSopenharmony_ci		fprintf(stderr, "Too many errors -- Aborting test\n");
605f08c3bdfSopenharmony_ci		fprintf(stderr, "Total create files: %d\n", cFileCount);
606f08c3bdfSopenharmony_ci		fprintf(stderr, "Total delete files: %d\n", dFileCount);
607f08c3bdfSopenharmony_ci		fprintf(stderr, "Total error       : %d\n", errorCount);
608f08c3bdfSopenharmony_ci		return (MAXERROR);
609f08c3bdfSopenharmony_ci	}
610f08c3bdfSopenharmony_ci
611f08c3bdfSopenharmony_ci	return 0;
612f08c3bdfSopenharmony_ci}
613f08c3bdfSopenharmony_ci
614f08c3bdfSopenharmony_ciint do_random_create_delete(int maxNum)
615f08c3bdfSopenharmony_ci{
616f08c3bdfSopenharmony_ci	int r, rc = 0;
617f08c3bdfSopenharmony_ci	char fname[1024];
618f08c3bdfSopenharmony_ci	time_t t;
619f08c3bdfSopenharmony_ci	int i;
620f08c3bdfSopenharmony_ci
621f08c3bdfSopenharmony_ci	printf("Running random create/delete test...\n");
622f08c3bdfSopenharmony_ci
623f08c3bdfSopenharmony_ci	if (maxNum < 1 || maxNum > MAXNUM) {
624f08c3bdfSopenharmony_ci		printf("MAX out of size %d\n", maxNum);
625f08c3bdfSopenharmony_ci		return (maxNum);
626f08c3bdfSopenharmony_ci	}
627f08c3bdfSopenharmony_ci
628f08c3bdfSopenharmony_ci	time(&t);
629f08c3bdfSopenharmony_ci	srandom((unsigned int)getpid() ^
630f08c3bdfSopenharmony_ci		(((unsigned int)t << 16) | (unsigned int)t >> 16));
631f08c3bdfSopenharmony_ci
632f08c3bdfSopenharmony_ci	/* 00/00/00/00 */
633f08c3bdfSopenharmony_ci	for (i = 0; i < maxNum && rc != MAXERROR; i++) {
634f08c3bdfSopenharmony_ci		r = random() % maxNum;
635f08c3bdfSopenharmony_ci		sprintf(fname, "00/%2.2x/%2.2x/00%2.2x%2.2x%2.2x",
636f08c3bdfSopenharmony_ci			((r >> 16) & 0xFF),
637f08c3bdfSopenharmony_ci			((r >> 8) & 0xFF),
638f08c3bdfSopenharmony_ci			((r >> 16) & 0xFF), ((r >> 8) & 0xFF), (r & 0xFF));
639f08c3bdfSopenharmony_ci
640f08c3bdfSopenharmony_ci		rc = create_or_delete(fname);
641f08c3bdfSopenharmony_ci	}
642f08c3bdfSopenharmony_ci
643f08c3bdfSopenharmony_ci	fprintf(stderr, "Total create files: %d\n", cFileCount);
644f08c3bdfSopenharmony_ci	fprintf(stderr, "Total delete files: %d\n", dFileCount);
645f08c3bdfSopenharmony_ci	fprintf(stderr, "Total error       : %d\n", errorCount);
646f08c3bdfSopenharmony_ci	return (rc);
647f08c3bdfSopenharmony_ci}
648