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.
5 * Copyright (C) 2015 Red Hat, Inc. All Rights reserved.
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.
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.
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.
22 #include <sys/types.h>
32 unsigned long buf_size = 0;
33 unsigned long size_MB = 0;
34 #define IO_PATTERN 0xab
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",
56 for (i = 0, p = (char *)buf; i < len; i += 16) {
59 if (i && !memcmp(p, p - 16, 16)) {
72 printf("%08llx ", (unsigned long long)offset + i);
73 for (j = 0; j < 16 && i + j < len; j++, p++)
76 for (j = 0; j < 16 && i + j < len; j++, s++) {
85 printf("%08llx\n", (unsigned long long)offset + i);
88 int main(int argc, char *argv[])
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 };
100 char *filename = NULL;
102 while ((c = getopt(argc, argv, "s:b:")) != -1) {
106 case 's': /* XXX MB size will be extended */
107 size_MB = strtol(optarg, &endp, 0);
109 case 'b': /* buffer size */
110 buf_size = strtol(optarg, &endp, 0);
117 if (size_MB == 0) /* default size is 8MB */
119 if (buf_size < 2048) /* default minimum buffer size is 2048 bytes */
122 if (optind == argc - 1)
123 filename = argv[optind];
129 fd = open(filename, O_DIRECT | O_CREAT | O_TRUNC | O_RDWR, 0600);
135 err = posix_memalign(&buf, getpagesize(), buf_size);
137 fprintf(stderr, "error %s during %s\n",
142 cmp_buf = malloc(buf_size);
143 memset(cmp_buf, IO_PATTERN, buf_size);
145 err = io_setup(4, &ctx);
147 fprintf(stderr, "error %s during %s\n",
155 /* Keep extending until size_MB */
156 while (eof < size_MB * 1024 * 1024) {
160 memset(buf, IO_PATTERN, buf_size);
162 eof = statbuf.st_size;
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.
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);
176 err = io_submit(ctx, 4, iocbs);
178 fprintf(stderr, "error %s during %s\n",
184 err = io_getevents(ctx, 4, 4, evs, NULL);
186 fprintf(stderr, "error %s during %s\n",
192 for (i = 0; i < err; i++) {
194 * res is unsigned for some reason, so this is the best
195 * way to detect that it contains a negative errno.
197 if (evs[i].res > buf_size / 4) {
198 fprintf(stderr, "pwrite: %s\n",
199 strerror(-evs[i].res));
205 * And then read it back.
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.
210 sret = pread(fd, buf, buf_size, eof);
214 } else if (sret != buf_size) {
215 fprintf(stderr, "short read %zd was less than %zu\n",
221 * We launched 4 AIOs which, stitched together, should write
222 * a seamless buf_size worth of IO_PATTERN to the last block.
224 if (memcmp(buf, cmp_buf, buf_size)) {
225 printf("corruption while extending from %lld\n",
227 dump_buffer(buf, 0, buf_size);
232 printf("Success, all done.\n");