src/aio-dio-eof-race: handle aio pwrite errors and short reads
[xfstests-dev.git] / src / aio-dio-regress / aio-dio-eof-race.c
1 /*
2  * Launch 4 sub-block AIOs past EOF and ensure that we don't see
3  * corruption from racing sub-block zeroing when they're complete.
4  *
5  * Copyright (C) 2015 Red Hat, Inc. All Rights reserved.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20  */
21 #include <sys/stat.h>
22 #include <sys/types.h>
23 #include <errno.h>
24 #include <fcntl.h>
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <unistd.h>
28 #include <ctype.h>
29
30 #include <libaio.h>
31
32 unsigned long buf_size = 0;
33 unsigned long size_MB = 0;
34 #define IO_PATTERN      0xab
35
36 void
37 usage(char *progname)
38 {
39         fprintf(stderr, "usage: %s [-s filesize] [-b bufsize] filename\n"
40                 "\t-s filesize: specify the minimum file size"
41                 "\t-b bufsize: buffer size",
42                 progname);
43         exit(1);
44 }
45
46 void
47 dump_buffer(
48         void    *buf,
49         off64_t offset,
50         ssize_t len)
51 {
52         int     i, j;
53         char    *p;
54         int     new;
55
56         for (i = 0, p = (char *)buf; i < len; i += 16) {
57                 char    *s = p;
58
59                 if (i && !memcmp(p, p - 16, 16)) {
60                         new = 0;
61                 } else {
62                         if (i)
63                                 printf("*\n");
64                         new = 1;
65                 }
66
67                 if (!new) {
68                         p += 16;
69                         continue;
70                 }
71
72                 printf("%08llx  ", (unsigned long long)offset + i);
73                 for (j = 0; j < 16 && i + j < len; j++, p++)
74                         printf("%02x ", *p);
75                 printf(" ");
76                 for (j = 0; j < 16 && i + j < len; j++, s++) {
77                         if (isalnum((int)*s))
78                                 printf("%c", *s);
79                         else
80                                 printf(".");
81                 }
82                 printf("\n");
83
84         }
85         printf("%08llx\n", (unsigned long long)offset + i);
86 }
87
88 int main(int argc, char *argv[])
89 {
90         struct io_context *ctx = NULL;
91         struct io_event evs[4];
92         struct iocb iocb1, iocb2, iocb3, iocb4;
93         struct iocb *iocbs[] = { &iocb1, &iocb2, &iocb3, &iocb4 };
94         void *buf;
95         struct stat statbuf;
96         int fd, err = 0;
97         off_t eof;
98         int c;
99         char *cmp_buf = NULL;
100         char *filename = NULL;
101
102         while ((c = getopt(argc, argv, "s:b:")) != -1) {
103                 char *endp;
104
105                 switch (c) {
106                 case 's':       /* XXX MB size will be extended */
107                         size_MB = strtol(optarg, &endp, 0);
108                         break;
109                 case 'b':       /* buffer size */
110                         buf_size = strtol(optarg, &endp, 0);
111                         break;
112                 default:
113                         usage(argv[0]);
114                 }
115         }
116
117         if (size_MB == 0)       /* default size is 8MB */
118                 size_MB = 8;
119         if (buf_size < 2048)    /* default minimum buffer size is 2048 bytes */
120                 buf_size = 2048;
121
122         if (optind == argc - 1)
123                 filename = argv[optind];
124         else
125                 usage(argv[0]);
126
127
128
129         fd = open(filename, O_DIRECT | O_CREAT | O_TRUNC | O_RDWR, 0600);
130         if (fd == -1) {
131                 perror("open");
132                 return 1;
133         }
134
135         err = posix_memalign(&buf, getpagesize(), buf_size);
136         if (err) {
137                 fprintf(stderr, "error %s during %s\n",
138                         strerror(err),
139                         "posix_memalign");
140                 return 1;
141         }
142         cmp_buf = malloc(buf_size);
143         memset(cmp_buf, IO_PATTERN, buf_size);
144
145         err = io_setup(4, &ctx);
146         if (err) {
147                 fprintf(stderr, "error %s during %s\n",
148                         strerror(err),
149                         "io_setup");
150                 return 1;
151         }
152
153         eof = 0;
154
155         /* Keep extending until size_MB */
156         while (eof < size_MB * 1024 * 1024) {
157                 ssize_t sret;
158                 int i;
159
160                 memset(buf, IO_PATTERN, buf_size);
161                 fstat(fd, &statbuf);
162                 eof = statbuf.st_size;
163
164                 /*
165                  * Split the buffer into multiple I/Os to send a mix of block
166                  * aligned/unaligned writes as well as writes that start beyond
167                  * the current EOF. This stresses things like inode size
168                  * management and stale block zeroing for races and can lead to
169                  * data corruption when not handled properly.
170                  */
171                 io_prep_pwrite(&iocb1, fd, buf, buf_size/4, eof + 0*buf_size/4);
172                 io_prep_pwrite(&iocb2, fd, buf, buf_size/4, eof + 1*buf_size/4);
173                 io_prep_pwrite(&iocb3, fd, buf, buf_size/4, eof + 2*buf_size/4);
174                 io_prep_pwrite(&iocb4, fd, buf, buf_size/4, eof + 3*buf_size/4);
175
176                 err = io_submit(ctx, 4, iocbs);
177                 if (err != 4) {
178                         fprintf(stderr, "error %s during %s\n",
179                                 strerror(err),
180                                 "io_submit");
181                         return 1;
182                 }
183
184                 err = io_getevents(ctx, 4, 4, evs, NULL);
185                 if (err != 4) {
186                         fprintf(stderr, "error %s during %s\n",
187                                 strerror(err),
188                                 "io_getevents");
189                         return 1;
190                 }
191
192                 for (i = 0; i < err; i++) {
193                         /*
194                          * res is unsigned for some reason, so this is the best
195                          * way to detect that it contains a negative errno.
196                          */
197                         if (evs[i].res > buf_size / 4) {
198                                 fprintf(stderr, "pwrite: %s\n",
199                                         strerror(-evs[i].res));
200                                 return 1;
201                         }
202                 }
203
204                 /*
205                  * And then read it back.
206                  *
207                  * Using pread to keep it simple, but AIO has the same effect.
208                  * eof is the prior eof; we just wrote buf_size more.
209                  */
210                 sret = pread(fd, buf, buf_size, eof);
211                 if (sret == -1) {
212                         perror("pread");
213                         return 1;
214                 } else if (sret != buf_size) {
215                         fprintf(stderr, "short read %zd was less than %zu\n",
216                                 sret, buf_size);
217                         return 1;
218                 }
219
220                 /*
221                  * We launched 4 AIOs which, stitched together, should write
222                  * a seamless buf_size worth of IO_PATTERN to the last block.
223                  */
224                 if (memcmp(buf, cmp_buf, buf_size)) {
225                         printf("corruption while extending from %lld\n",
226                                (long long) eof);
227                         dump_buffer(buf, 0, buf_size);
228                         return 1;
229                 }
230         }
231
232         printf("Success, all done.\n");
233         return 0;
234 }