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 <fcntl.h> 34 #include <stdio.h> 35 #include <unistd.h> 36 #include <memory.h> 37 #include <string.h> 38 #include <limits.h> 39 40 #define NUM_CHILDREN 8 41 42 char *check_zero(unsigned char *buf, int size) 43 { 44 unsigned char *p; 45 46 p = buf; 47 48 while (size > 0) { 49 if (*buf != 0) { 50 fprintf(stderr, 51 "non zero buffer at buf[%d] => 0x%02x,%02x,%02x,%02x\n", 52 buf - p, (unsigned int)buf[0], 53 size > 1 ? (unsigned int)buf[1] : 0, 54 size > 2 ? (unsigned int)buf[2] : 0, 55 size > 3 ? (unsigned int)buf[3] : 0); 56 fprintf(stderr, "buf %p, p %p\n", buf, p); 57 return buf; 58 } 59 buf++; 60 size--; 61 } 62 return 0; /* all zeros */ 63 } 64 65 int dio_read(char *filename) 66 { 67 int fd; 68 int r; 69 void *bufptr; 70 71 if (posix_memalign(&bufptr, 4096, 64 * 1024)) { 72 perror("cannot malloc aligned memory"); 73 return -1; 74 } 75 76 while ((fd = open(filename, O_DIRECT | O_RDONLY)) < 0) { 77 } 78 fprintf(stderr, "dio_truncate: child reading file\n"); 79 while (1) { 80 off_t offset; 81 char *bufoff; 82 83 /* read the file, checking for zeros */ 84 offset = lseek(fd, SEEK_SET, 0); 85 do { 86 r = read(fd, bufptr, 64 * 1024); 87 if (r > 0) { 88 if ((bufoff = check_zero(bufptr, r))) { 89 fprintf(stderr, 90 "non-zero read at offset %p\n", 91 offset + bufoff); 92 exit(1); 93 } 94 offset += r; 95 } 96 } while (r > 0); 97 } 98 return 0; 99 } 100 101 void dio_append(char *filename, int fill) 102 { 103 int fd; 104 void *bufptr; 105 int i; 106 int w; 107 108 fd = open(filename, O_DIRECT | O_WRONLY | O_CREAT, 0666); 109 110 if (fd < 0) { 111 perror("cannot create file"); 112 return; 113 } 114 115 if (posix_memalign(&bufptr, 4096, 64 * 1024)) { 116 perror("cannot malloc aligned memory"); 117 return; 118 } 119 120 memset(bufptr, fill, 64 * 1024); 121 122 for (i = 0; i < 1000; i++) { 123 if ((w = write(fd, bufptr, 64 * 1024)) != 64 * 1024) { 124 fprintf(stderr, "write %d returned %d\n", i, w); 125 } 126 } 127 close(fd); 128 } 129 130 int main(int argc, char **argv) 131 { 132 char filename[PATH_MAX]; 133 int pid[NUM_CHILDREN]; 134 int num_children = 1; 135 int i; 136 137 snprintf(filename, sizeof(filename), "%s/aiodio/file", 138 getenv("TMP") ? getenv("TMP") : "/tmp"); 139 140 for (i = 0; i < num_children; i++) { 141 if ((pid[i] = fork()) == 0) { 142 /* child */ 143 return dio_read(filename); 144 } else if (pid[i] < 0) { 145 /* error */ 146 perror("fork error"); 147 break; 148 } else { 149 /* Parent */ 150 continue; 151 } 152 } 153 154 /* 155 * Parent creates a zero file using DIO. 156 * Truncates it to zero 157 * Create another file with '0xaa' 158 */ 159 for (i = 0; i < 100; i++) { 160 dio_append(filename, 0); 161 truncate(filename, 0); 162 dio_append("junkfile", 0xaa); 163 truncate("junkfile", 0); 164 } 165 166 for (i = 0; i < num_children; i++) { 167 kill(pid[i], SIGTERM); 168 } 169 170 return 0; 171 } 172