Whamcloud - gitweb
LU-10212 test: ESTALE read
[fs/lustre-release.git] / lustre / tests / directio.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.gnu.org/licenses/gpl-2.0.html
19  *
20  * GPL HEADER END
21  */
22 /*
23  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Use is subject to license terms.
25  */
26 /*
27  * This file is part of Lustre, http://www.lustre.org/
28  * Lustre is a trademark of Sun Microsystems, Inc.
29  */
30
31 #ifndef _GNU_SOURCE
32 #define  _GNU_SOURCE
33 #endif
34 #include <stdio.h>
35 #include <string.h>
36 #include <unistd.h>
37 #include <fcntl.h>
38 #include <stdlib.h>
39 #include <errno.h>
40 #include <sys/types.h>
41 #include <sys/stat.h>
42 #include <sys/mman.h>
43
44 /* return index of the first byte not matching given byte
45  * or buffer size if all bytes are matching */
46 static size_t check_bytes(const char *buf, int byte, size_t len)
47 {
48         const char *p;
49
50         for (p = buf; p < buf + len; p++)
51                 if (*p != byte)
52                         break;
53         return p - buf;
54 }
55
56 int main(int argc, char **argv)
57 {
58 #ifdef O_DIRECT
59         int fd;
60         char *buf, *fname;
61         int blocks, seek_blocks;
62         long len;
63         off64_t seek;
64         struct stat64 st;
65         char pad = 0xba;
66         int action;
67         int rc;
68
69         if (argc < 5 || argc > 6) {
70                 printf("Usage: %s <read/write/rdwr/readhole> file seek nr_blocks [blocksize]\n", argv[0]);
71                 return 1;
72         }
73
74         if (!strcmp(argv[1], "read"))
75                 action = O_RDONLY;
76         else if (!strcmp(argv[1], "write"))
77                 action = O_WRONLY;
78         else if (!strcmp(argv[1], "rdwr"))
79                 action = O_RDWR;
80         else if (!strcmp(argv[1], "readhole")) {
81                 action = O_RDONLY;
82                 pad = 0;
83         } else {
84                 printf("Usage: %s <read/write/rdwr> file seek nr_blocks [blocksize]\n", argv[0]);
85                 return 1;
86         }
87
88         fname = argv[2];
89         seek_blocks = strtoul(argv[3], 0, 0);
90         blocks = strtoul(argv[4], 0, 0);
91         if (!blocks) {
92                 printf("Usage: %s <read/write/rdwr> file seek nr_blocks [blocksize]\n", argv[0]);
93                 return 1;
94         }
95
96         fd = open(fname, O_LARGEFILE | O_DIRECT | O_RDWR | O_CREAT, 0644);
97         if (fd == -1) {
98                 printf("Cannot open %s:  %s\n", fname, strerror(errno));
99                 return 1;
100         }
101
102         if (argc >= 6)
103                 st.st_blksize = strtoul(argv[5], 0, 0);
104         else if (fstat64(fd, &st) < 0) {
105                 printf("Cannot stat %s:  %s\n", fname, strerror(errno));
106                 return 1;
107         }
108
109         printf("directio on %s for %dx%lu bytes\n", fname, blocks,
110                (unsigned long)st.st_blksize);
111
112         seek = (off64_t)seek_blocks * (off64_t)st.st_blksize;
113         len = blocks * st.st_blksize;
114
115         buf = mmap(0, len, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0);
116         if (buf == MAP_FAILED) {
117                 printf("No memory %s\n", strerror(errno));
118                 return 1;
119         }
120         memset(buf, pad, len);
121
122         if (action == O_WRONLY || action == O_RDWR) {
123                 if (lseek64(fd, seek, SEEK_SET) < 0) {
124                         printf("lseek64 failed: %s\n", strerror(errno));
125                         return 1;
126                 }
127
128                 rc = write(fd, buf, len);
129                 if (rc != len) {
130                         printf("Write error %s (rc = %d, len = %ld)\n",
131                                strerror(errno), rc, len);
132                         return 1;
133                 }
134         }
135
136         if (action == O_RDONLY || action == O_RDWR) {
137                 if (lseek64(fd, seek, SEEK_SET) < 0) {
138                         printf("Cannot seek %s\n", strerror(errno));
139                         return 1;
140                 }
141                 /* reset all bytes to something nor 0x0 neither 0xab */
142                 memset(buf, 0x5e, len);
143                 rc = read(fd, buf, len);
144                 if (rc != len) {
145                         printf("Read error: %s rc = %d\n",strerror(errno),rc);
146                         return 1;
147                 }
148
149                 if (check_bytes(buf, pad, len) != len) {
150                         printf("Data mismatch\n");
151                         return 1;
152                 }
153         }
154
155         printf("PASS\n");
156         return 0;
157 #else /* !O_DIRECT */
158 #warning O_DIRECT not defined, directio test will fail
159         printf("O_DIRECT not defined\n");
160         return 1;
161 #endif /* !O_DIRECT */
162 }