Whamcloud - gitweb
smash the HEAD with the contents of b_cmd. HEAD_PRE_CMD_SMASH and
[fs/lustre-release.git] / lustre / tests / directio.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  */
4 #define  _GNU_SOURCE
5 #include <stdio.h>
6 #include <string.h>
7 #include <unistd.h>
8 #include <fcntl.h>
9 #include <stdlib.h>
10 #include <errno.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <sys/mman.h>
14
15 int main(int argc, char **argv)
16 {
17         int fd;
18         char *rbuf, *wbuf;
19         int blocks, seek_blocks;
20         long len;
21         off64_t seek;
22         struct stat64 st;
23         int rc;
24
25         if (argc < 4 || argc > 5) {
26                 printf("Usage: %s file seek nr_blocks [blocksize]\n", argv[0]);
27                 return 1;
28         }
29
30         seek_blocks = strtoul(argv[2], 0, 0);
31         blocks = strtoul(argv[3], 0, 0);
32
33         fd = open(argv[1], O_LARGEFILE | O_DIRECT | O_RDWR | O_CREAT, 0644);
34         if (fd == -1) {
35                 printf("Cannot open %s:  %s\n", argv[1], strerror(errno));
36                 return 1;
37         }
38
39         if (argc == 5)
40                 st.st_blksize = strtoul(argv[4], 0, 0);
41         else if (fstat64(fd, &st) < 0) {
42                 printf("Cannot stat %s:  %s\n", argv[1], strerror(errno));
43                 return 1;
44         }
45
46         printf("directio on %s for %dx%lu bytes \n", argv[1], blocks,
47                st.st_blksize);
48
49         seek = (off64_t)seek_blocks * (off64_t)st.st_blksize;
50         if (lseek64(fd, seek, SEEK_SET) < 0) {
51                 printf("lseek64 failed: %s\n", strerror(errno));
52                 return 1;
53         }
54
55         len = blocks * st.st_blksize;
56         wbuf = mmap(0, len, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0);
57         if (wbuf == MAP_FAILED) {
58                 printf("No memory %s\n", strerror(errno));
59                 return 1;
60         }
61
62         rbuf = mmap(0, len, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0);
63         if (rbuf == MAP_FAILED) {
64                 printf("No memory %s\n", strerror(errno));
65                 return 1;
66         }
67
68         memset(wbuf, 0xba, len);
69         rc = write(fd, wbuf, len);
70         if (rc != len) {
71                 printf("Write error %s (rc = %d)\n", strerror(errno), rc);
72                 return 1;
73         }
74
75         if (lseek64(fd, seek, SEEK_SET) < 0) {
76                 printf("Cannot seek %s\n", strerror(errno));
77                 return 1;
78         }
79
80         rc = read(fd, rbuf, len);
81         if (rc != len) {
82                 printf("Read error: %s (rc = %d)\n", strerror(errno), rc);
83                 return 1;
84         }
85
86         if (memcmp(wbuf, rbuf, len)) {
87                 printf("Data mismatch\n");
88                 return 1;
89         }
90
91         printf("PASS\n");
92         return 0;
93 }