1
2/*
3 * Copyright (c) 2004 Daniel McNeil <daniel@osdl.org>
4 *               2004 Open Source Development Lab
5 *   This program is free software;  you can redistribute it and/or modify
6 *   it under the terms of the GNU General Public License as published by
7 *   the Free Software Foundation; either version 2 of the License, or
8 *   (at your option) any later version.
9 *
10 *   This program is distributed in the hope that it will be useful,
11 *   but WITHOUT ANY WARRANTY;  without even the implied warranty of
12 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See
13 *   the GNU General Public License for more details.
14 *
15 *   You should have received a copy of the GNU General Public License
16 *   along with this program;  if not, write to the Free Software
17 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Module: .c
20 */
21
22/*
23 * Change History:
24 *
25 * 2/2004  Marty Ridgeway (mridge@us.ibm.com) Changes to adapt to LTP
26 *
27 */
28/*
29 * dio_append - append zeroed data to a file using O_DIRECT while
30 *	a 2nd process is doing buffered reads and check if the buffer
31 *	reads always see zero.
32 */
33#define _GNU_SOURCE
34
35#include <stdlib.h>
36#include <sys/types.h>
37#include <signal.h>
38#include <fcntl.h>
39#include <stdio.h>
40#include <unistd.h>
41#include <memory.h>
42#include <limits.h>
43#define NUM_CHILDREN 8
44
45char *check_zero(unsigned char *buf, int size)
46{
47	unsigned char *p;
48
49	p = buf;
50
51	while (size > 0) {
52		if (*buf != 0) {
53			fprintf(stderr,
54				"non zero buffer at buf[%d] => 0x%02x,%02x,%02x,%02x\n",
55				buf - p, (unsigned int)buf[0],
56				size > 1 ? (unsigned int)buf[1] : 0,
57				size > 2 ? (unsigned int)buf[2] : 0,
58				size > 3 ? (unsigned int)buf[3] : 0);
59			fprintf(stderr, "buf %p, p %p\n", buf, p);
60			return buf;
61		}
62		buf++;
63		size--;
64	}
65	return 0;		/* all zeros */
66}
67
68int read_eof(char *filename)
69{
70	int fd;
71	int i;
72	int r;
73	char buf[4096];
74
75	while ((fd = open(filename, O_RDONLY)) < 0) {
76		sleep(1);	/* wait for file to be created */
77	}
78
79	for (i = 0; i < 1000000; i++) {
80		off_t offset;
81		char *bufoff;
82
83		offset = lseek(fd, SEEK_END, 0);
84		r = read(fd, buf, 4096);
85		if (r > 0) {
86			if ((bufoff = check_zero(buf, r))) {
87				fprintf(stderr, "non-zero read at offset %p\n",
88					offset + bufoff);
89				exit(1);
90			}
91		}
92	}
93	return 0;
94}
95
96void dio_append(char *filename)
97{
98	int fd;
99	void *bufptr;
100	int i;
101	int w;
102
103	fd = open(filename, O_DIRECT | O_WRONLY | O_CREAT, 0666);
104
105	if (fd < 0) {
106		perror("cannot create file");
107		return;
108	}
109
110	if (posix_memalign(&bufptr, 4096, 64 * 1024)) {
111		perror("cannot malloc aligned memory");
112		return;
113	}
114
115	memset(bufptr, 0, 64 * 1024);
116	for (i = 0; i < 1000; i++) {
117		if ((w = write(fd, bufptr, 64 * 1024)) != 64 * 1024) {
118			fprintf(stderr, "write %d returned %d\n", i, w);
119		}
120	}
121}
122
123int main(int argc, char **argv)
124{
125	char filename[PATH_MAX];
126	int pid[NUM_CHILDREN];
127	int num_children = 1;
128	int i;
129
130	snprintf(filename, sizeof(filename), "%s/aiodio/file",
131		 getenv("TMP") ? getenv("TMP") : "/tmp");
132
133	printf("Begin dio_append test...\n");
134
135	for (i = 0; i < num_children; i++) {
136		if ((pid[i] = fork()) == 0) {
137			/* child */
138			return read_eof(filename);
139		} else if (pid[i] < 0) {
140			/* error */
141			perror("fork error");
142			break;
143		} else {
144			/* Parent */
145			continue;
146		}
147	}
148
149	/*
150	 * Parent appends to end of file using direct i/o
151	 */
152
153	dio_append(filename);
154
155	for (i = 0; i < num_children; i++) {
156		kill(pid[i], SIGTERM);
157	}
158	return 0;
159}
160