2 * Read from a sparse file immedialy after filling a hole to test for races
3 * in unwritten extent conversion.
5 * Copyright (C) 2010 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 #define IO_PATTERN 0xab
34 int main(int argc, char *argv[])
36 struct io_context *ctx = NULL;
38 struct iocb iocb, *iocbs[] = { &iocb };
40 char cmp_buf[BUF_SIZE];
43 fd = open(argv[1], O_DIRECT | O_CREAT | O_TRUNC | O_RDWR, 0600);
49 err = posix_memalign(&buf, BUF_SIZE, BUF_SIZE);
51 fprintf(stderr, "error %s during %s\n",
56 memset(buf, IO_PATTERN, BUF_SIZE);
57 memset(cmp_buf, IO_PATTERN, BUF_SIZE);
60 * Truncate to some random large file size. Just make sure
61 * it's not smaller than our I/O size.
63 if (ftruncate(fd, 1024 * 1024 * 1024) < 0) {
70 * Do a simple 4k write into a hole using aio.
72 err = io_setup(1, &ctx);
74 fprintf(stderr, "error %s during %s\n",
80 io_prep_pwrite(&iocb, fd, buf, BUF_SIZE, 0);
82 err = io_submit(ctx, 1, iocbs);
84 fprintf(stderr, "error %s during %s\n",
90 err = io_getevents(ctx, 1, 1, &ev, NULL);
92 fprintf(stderr, "error %s during %s\n",
99 * And then read it back.
101 * Using pread to keep it simple, but AIO has the same effect.
103 if (pread(fd, buf, BUF_SIZE, 0) != BUF_SIZE) {
109 * And depending on the machine we'll just get zeroes back quite
110 * often here. That's because the unwritten extent conversion
113 if (memcmp(buf, cmp_buf, BUF_SIZE)) {
114 unsigned long long *ubuf = (unsigned long long *)buf;
117 for (i = 0; i < BUF_SIZE / sizeof(unsigned long long); i++)
118 printf("%d: 0x%llx\n", i, ubuf[i]);