f79c0469c2732e765aba0e1c2a492fd5facfbbb2
[xfstests-dev.git] / src / aio-dio-regress / aio-dio-hole-filling-race.c
1 /*
2  * Read from a sparse file immedialy after filling a hole to test for races
3  * in unwritten extent conversion.
4  *
5  * Copyright (C) 2010 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
29 #include <libaio.h>
30
31 #define BUF_SIZE        4096
32 #define IO_PATTERN      0xab
33
34 int main(int argc, char *argv[])
35 {
36         struct io_context *ctx = NULL;
37         struct io_event ev;
38         struct iocb iocb, *iocbs[] = { &iocb };
39         void *buf;
40         char cmp_buf[BUF_SIZE];
41         int fd, err = 0;
42
43         fd = open(argv[1], O_DIRECT | O_CREAT | O_TRUNC | O_RDWR, 0600);
44         if (fd == -1) {
45                 perror("open");
46                 return 1;
47         }
48
49         err = posix_memalign(&buf, BUF_SIZE, BUF_SIZE);
50         if (err) {
51                 fprintf(stderr, "error %s during %s\n",
52                         strerror(-err),
53                         "posix_memalign");
54                 return 1;
55         }
56         memset(buf, IO_PATTERN, BUF_SIZE);
57         memset(cmp_buf, IO_PATTERN, BUF_SIZE);
58
59         /*
60          * Truncate to some random large file size.  Just make sure
61          * it's not smaller than our I/O size.
62          */
63         if (ftruncate(fd, 1024 * 1024 * 1024) < 0) {
64                 perror("ftruncate");
65                 return 1;
66         }
67
68
69         /*
70          * Do a simple 4k write into a hole using aio.
71          */
72         err = io_setup(1, &ctx);
73         if (err) {
74                 fprintf(stderr, "error %s during %s\n",
75                         strerror(-err),
76                         "io_setup");
77                 return 1;
78         }
79
80         io_prep_pwrite(&iocb, fd, buf, BUF_SIZE, 0);
81
82         err = io_submit(ctx, 1, iocbs);
83         if (err != 1) {
84                 fprintf(stderr, "error %s during %s\n",
85                         strerror(-err),
86                         "io_submit");
87                 return 1;
88         }
89
90         err = io_getevents(ctx, 1, 1, &ev, NULL);
91         if (err != 1) {
92                 fprintf(stderr, "error %s during %s\n",
93                         strerror(-err),
94                         "io_getevents");
95                 return 1;
96         }
97
98         /*
99          * And then read it back.
100          *
101          * Using pread to keep it simple, but AIO has the same effect.
102          */
103         if (pread(fd, buf, BUF_SIZE, 0) != BUF_SIZE) {
104                 perror("pread");
105                 return 1;
106         }
107
108         /*
109          * And depending on the machine we'll just get zeroes back quite
110          * often here.  That's because the unwritten extent conversion
111          * hasn't finished.
112          */
113         if (memcmp(buf, cmp_buf, BUF_SIZE)) {
114                 unsigned long long *ubuf = (unsigned long long *)buf;
115                 int i;
116
117                 for (i = 0; i < BUF_SIZE / sizeof(unsigned long long); i++)
118                         printf("%d: 0x%llx\n", i, ubuf[i]);
119                 return 1;
120         }
121
122         return 0;
123 }