Home | History | Annotate | Download | only in ltp-aiodio
      1 
      2 /*
      3  * Copyright (c) 2004 Daniel McNeil <daniel (at) 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 (at) us.ibm.com) Changes to adapt to LTP
     26  *
     27  */
     28 #define _GNU_SOURCE
     29 
     30 #include <stdlib.h>
     31 #include <sys/types.h>
     32 #include <signal.h>
     33 #include <errno.h>
     34 #include <fcntl.h>
     35 #include <stdio.h>
     36 #include <unistd.h>
     37 #include <memory.h>
     38 #include <string.h>
     39 #include <limits.h>
     40 
     41 #include "test.h"
     42 
     43 #define NUM_CHILDREN 8
     44 
     45 char *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 
     68 int dio_read(char *filename)
     69 {
     70 	int fd;
     71 	int r;
     72 	void *bufptr;
     73 
     74 	TEST(posix_memalign(&bufptr, 4096, 64 * 1024));
     75 	if (TEST_RETURN) {
     76 		tst_resm(TBROK | TRERRNO, "cannot malloc aligned memory");
     77 		return -1;
     78 	}
     79 
     80 	while ((fd = open(filename, O_DIRECT | O_RDONLY)) < 0) {
     81 	}
     82 	fprintf(stderr, "dio_truncate: child reading file\n");
     83 	while (1) {
     84 		off_t offset;
     85 		char *bufoff;
     86 
     87 		/* read the file, checking for zeros */
     88 		offset = lseek(fd, SEEK_SET, 0);
     89 		do {
     90 			r = read(fd, bufptr, 64 * 1024);
     91 			if (r > 0) {
     92 				if ((bufoff = check_zero(bufptr, r))) {
     93 					fprintf(stderr,
     94 						"non-zero read at offset %p\n",
     95 						offset + bufoff);
     96 					exit(1);
     97 				}
     98 				offset += r;
     99 			}
    100 		} while (r > 0);
    101 	}
    102 	return 0;
    103 }
    104 
    105 void dio_append(char *filename, int fill)
    106 {
    107 	int fd;
    108 	void *bufptr;
    109 	int i;
    110 	int w;
    111 
    112 	fd = open(filename, O_DIRECT | O_WRONLY | O_CREAT, 0666);
    113 
    114 	if (fd < 0) {
    115 		perror("cannot create file");
    116 		return;
    117 	}
    118 
    119 	TEST(posix_memalign(&bufptr, 4096, 64 * 1024));
    120 	if (TEST_RETURN) {
    121 		tst_resm(TBROK | TRERRNO, "cannot malloc aligned memory");
    122 		close(fd);
    123 		return;
    124 	}
    125 
    126 	memset(bufptr, fill, 64 * 1024);
    127 
    128 	for (i = 0; i < 1000; i++) {
    129 		if ((w = write(fd, bufptr, 64 * 1024)) != 64 * 1024) {
    130 			fprintf(stderr, "write %d returned %d\n", i, w);
    131 		}
    132 	}
    133 	close(fd);
    134 }
    135 
    136 int main(int argc, char **argv)
    137 {
    138 	char filename[PATH_MAX];
    139 	int pid[NUM_CHILDREN];
    140 	int num_children = 1;
    141 	int i;
    142 
    143 	snprintf(filename, sizeof(filename), "%s/aiodio/file",
    144 		 getenv("TMP") ? getenv("TMP") : "/tmp");
    145 
    146 	for (i = 0; i < num_children; i++) {
    147 		if ((pid[i] = fork()) == 0) {
    148 			/* child */
    149 			return dio_read(filename);
    150 		} else if (pid[i] < 0) {
    151 			/* error */
    152 			perror("fork error");
    153 			break;
    154 		} else {
    155 			/* Parent */
    156 			continue;
    157 		}
    158 	}
    159 
    160 	/*
    161 	 * Parent creates a zero file using DIO.
    162 	 * Truncates it to zero
    163 	 * Create another file with '0xaa'
    164 	 */
    165 	for (i = 0; i < 100; i++) {
    166 		dio_append(filename, 0);
    167 		truncate(filename, 0);
    168 		dio_append("junkfile", 0xaa);
    169 		truncate("junkfile", 0);
    170 	}
    171 
    172 	for (i = 0; i < num_children; i++) {
    173 		kill(pid[i], SIGTERM);
    174 	}
    175 
    176 	return 0;
    177 }
    178