xfs: replace open-coded calls to xfs_logprint with helpers
[xfstests-dev.git] / src / unwritten_sync.c
1 #include <stdlib.h>
2 #include <string.h>
3 #include <sys/types.h>
4 #include <fcntl.h>
5 #include <errno.h>
6 #include <malloc.h>
7 #include <unistd.h>
8 #include <xfs/xfs.h>
9
10 /* test thanks to judith@sgi.com */
11
12 #define IO_SIZE 1048576
13
14 void
15 print_getbmapx(
16         const char      *pathname,
17         int             fd,
18         int64_t         start,
19         int64_t         limit);
20
21 int
22 main(int argc, char *argv[])
23 {
24         int i;
25         int fd;
26         char *buf;
27         struct dioattr dio;
28         xfs_flock64_t flock;
29         off_t offset;
30         char    *file;
31         int     loops;
32         char    *dio_env;
33
34         if(argc != 3) {
35                 fprintf(stderr, "%s <loops> <file>\n", argv[0]);
36                 exit(1);
37         }
38
39         errno = 0;
40         loops = strtoull(argv[1], NULL, 0);
41         if (errno) {
42                 perror("strtoull");
43                 exit(errno);
44         }
45         file = argv[2];
46
47         while (loops-- > 0) {
48                 sleep(1);
49                 fd = open(file, O_RDWR|O_CREAT|O_DIRECT, 0666);
50                 if (fd < 0) {
51                         perror("open");
52                         exit(1);
53                 }
54                 if (xfsctl(file, fd, XFS_IOC_DIOINFO, &dio) < 0) {
55                         perror("dioinfo");
56                         exit(1);
57                 }
58
59                 dio_env = getenv("XFS_DIO_MIN");
60                 if (dio_env)
61                         dio.d_mem = dio.d_miniosz = atoi(dio_env);
62
63                 if ((dio.d_miniosz > IO_SIZE) || (dio.d_maxiosz < IO_SIZE)) {
64                         fprintf(stderr, "Test won't work - iosize out of range"
65                                 " (dio.d_miniosz=%d, dio.d_maxiosz=%d)\n",
66                                 dio.d_miniosz, dio.d_maxiosz);
67
68                         exit(1);
69                 }
70                 buf = (char *)memalign(dio.d_mem , IO_SIZE);
71                 if (buf == NULL) {
72                         fprintf(stderr,"Can't get memory\n");
73                         exit(1);
74                 }
75                 memset(buf,'Z',IO_SIZE);
76                 offset = 0;
77
78                 flock.l_whence = 0;
79                 flock.l_start= 0;
80                 flock.l_len = IO_SIZE*21;
81                 if (xfsctl(file, fd, XFS_IOC_RESVSP64, &flock) < 0) {
82                         perror("xfsctl ");
83                         exit(1);
84                 }
85                 for (i = 0; i < 21; i++) {
86                         if (pwrite(fd, buf, IO_SIZE, offset) != IO_SIZE) {
87                                 perror("pwrite");
88                                 exit(1);
89                         }
90                         offset += IO_SIZE;
91                 }
92
93                 print_getbmapx(file, fd, 0, 0);
94
95                 flock.l_whence = 0;
96                 flock.l_start= 0;
97                 flock.l_len = 0;
98                 xfsctl(file, fd, XFS_IOC_FREESP64, &flock);
99                 print_getbmapx(file, fd, 0, 0);
100                 close(fd);
101         }
102         exit(0);
103 }
104
105 void
106 print_getbmapx(
107 const   char    *pathname,
108         int     fd,
109         int64_t start,
110         int64_t limit)
111 {
112         struct getbmapx bmapx[50];
113         int array_size = sizeof(bmapx) / sizeof(bmapx[0]);
114         int x;
115         int foundone = 0;
116         int foundany = 0;
117
118 again:
119         foundone = 0;
120         memset(bmapx, '\0', sizeof(bmapx));
121
122         bmapx[0].bmv_offset = start;
123         bmapx[0].bmv_length = -1; /* limit - start; */
124         bmapx[0].bmv_count = array_size;
125         bmapx[0].bmv_entries = 0;       /* no entries filled in yet */
126
127         bmapx[0].bmv_iflags = BMV_IF_PREALLOC;
128
129         x = array_size;
130         for (;;) {
131                 if (x > bmapx[0].bmv_entries) {
132                         if (x != array_size) {
133                                 break;  /* end of file */
134                         }
135                         if (xfsctl(pathname, fd, XFS_IOC_GETBMAPX, bmapx) < 0) {
136                                 fprintf(stderr, "XFS_IOC_GETBMAPX failed\n");
137                                 exit(1);
138                         }
139                         if (bmapx[0].bmv_entries == 0) {
140                                 break;
141                         }
142                         x = 1;  /* back at first extent in buffer */
143                 }
144                 if (bmapx[x].bmv_oflags & 1) {
145                         fprintf(stderr, "FOUND ONE %lld %lld %x\n",
146                                 (long long) bmapx[x].bmv_offset,
147                                 (long long) bmapx[x].bmv_length,
148                                 bmapx[x].bmv_oflags);
149                         foundone = 1;
150                         foundany = 1;
151                 }
152                 x++;
153         }
154         if (foundone) {
155                 sleep(1);
156                 fprintf(stderr,"Repeat\n");
157                 goto again;
158         }
159         if (foundany) {
160                 exit(1);
161         }
162 }
163