log-writes: Add support to output human readable flags
[xfstests-dev.git] / src / unwritten_mmap.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <fcntl.h>
5 #include <xfs/xfs.h>
6 #include <sys/mman.h>
7
8 /*
9  * mmap a preallocated file and write to a set of offsets
10  * in it. We'll check to see if the underlying extents are
11  * converted correctly on writeback.
12  */
13 int main(int argc, char **argv) {
14         unsigned long long o;
15         int fd, i;
16         struct xfs_flock64 space;
17         unsigned char *buf;
18
19         if(argc < 3) {
20                 fprintf(stderr, "%s <count> <file [file...]>\n", argv[0]);
21                 exit(1);
22         }
23
24         errno = 0;
25         o = strtoull(argv[1], NULL, 0);
26         if (errno) {
27                 perror("strtoull");
28                 exit(errno);
29         }
30
31         for(i = 2; i < argc; i++) {
32                 unlink(argv[i]);
33                 fd = open(argv[i], O_RDWR|O_CREAT|O_LARGEFILE, 0666);
34                 if(fd < 0) {
35                         perror("open");
36                         exit(2);
37                 }
38
39                 if(ftruncate64(fd, o) < 0) {
40                         perror("ftruncate64");
41                         exit(3);
42                 }
43
44                 space.l_whence = SEEK_SET;
45                 space.l_start = 0;
46                 space.l_len = o;
47
48                 if(ioctl(fd, XFS_IOC_RESVSP64, &space)) {
49                         perror("ioctl()");
50                         exit(4);
51                 }
52
53                 buf = mmap(NULL, (int)o, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
54                 if(buf == MAP_FAILED) {
55                         perror("mmap()");
56                         exit(5);
57                 } else {
58                         buf[o-1] = 0;
59                         buf[o/2] = 0;
60                         buf[0] = 0;
61                         munmap(buf, o);
62                 }
63
64                 fsync(fd);
65                 if(close(fd) == -1) {
66                         perror("close");
67                         exit(6);
68                 }
69         }
70         exit(0);
71 }