Whamcloud - gitweb
LU-5031 tests: obdfilter-survey fixes
[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.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2002, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  */
30 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  */
34
35 #ifndef _GNU_SOURCE
36 #define  _GNU_SOURCE
37 #endif
38 #include <stdio.h>
39 #include <string.h>
40 #include <unistd.h>
41 #include <fcntl.h>
42 #include <stdlib.h>
43 #include <errno.h>
44 #include <sys/types.h>
45 #include <sys/stat.h>
46 #include <sys/mman.h>
47
48 /* return index of the first byte not matching given byte
49  * or buffer size if all bytes are matching */
50 static size_t check_bytes(const char *buf, int byte, size_t len)
51 {
52         const char *p;
53
54         for (p = buf; p < buf + len; p++)
55                 if (*p != byte)
56                         break;
57         return p - buf;
58 }
59
60 int main(int argc, char **argv)
61 {
62 #ifdef O_DIRECT
63         int fd;
64         char *buf, *fname;
65         int blocks, seek_blocks;
66         long len;
67         off64_t seek;
68         struct stat64 st;
69         char pad = 0xba;
70         int action;
71         int rc;
72
73         if (argc < 5 || argc > 6) {
74                 printf("Usage: %s <read/write/rdwr/readhole> file seek nr_blocks [blocksize]\n", argv[0]);
75                 return 1;
76         }
77
78         if (!strcmp(argv[1], "read"))
79                 action = O_RDONLY;
80         else if (!strcmp(argv[1], "write"))
81                 action = O_WRONLY;
82         else if (!strcmp(argv[1], "rdwr"))
83                 action = O_RDWR;
84         else if (!strcmp(argv[1], "readhole")) {
85                 action = O_RDONLY;
86                 pad = 0;
87         } else {
88                 printf("Usage: %s <read/write/rdwr> file seek nr_blocks [blocksize]\n", argv[0]);
89                 return 1;
90         }
91
92         fname = argv[2];
93         seek_blocks = strtoul(argv[3], 0, 0);
94         blocks = strtoul(argv[4], 0, 0);
95         if (!blocks) {
96                 printf("Usage: %s <read/write/rdwr> file seek nr_blocks [blocksize]\n", argv[0]);
97                 return 1;
98         }
99
100         fd = open(fname, O_LARGEFILE | O_DIRECT | O_RDWR | O_CREAT, 0644);
101         if (fd == -1) {
102                 printf("Cannot open %s:  %s\n", fname, strerror(errno));
103                 return 1;
104         }
105
106         if (argc >= 6)
107                 st.st_blksize = strtoul(argv[5], 0, 0);
108         else if (fstat64(fd, &st) < 0) {
109                 printf("Cannot stat %s:  %s\n", fname, strerror(errno));
110                 return 1;
111         }
112
113         printf("directio on %s for %dx%lu bytes \n", fname, blocks,
114                st.st_blksize);
115
116         seek = (off64_t)seek_blocks * (off64_t)st.st_blksize;
117         len = blocks * st.st_blksize;
118
119         buf = mmap(0, len, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0);
120         if (buf == MAP_FAILED) {
121                 printf("No memory %s\n", strerror(errno));
122                 return 1;
123         }
124         memset(buf, pad, len);
125
126         if (action == O_WRONLY || action == O_RDWR) {
127                 if (lseek64(fd, seek, SEEK_SET) < 0) {
128                         printf("lseek64 failed: %s\n", strerror(errno));
129                         return 1;
130                 }
131
132                 rc = write(fd, buf, len);
133                 if (rc != len) {
134                         printf("Write error %s (rc = %d, len = %ld)\n",
135                                strerror(errno), rc, len);
136                         return 1;
137                 }
138         }
139
140         if (action == O_RDONLY || action == O_RDWR) {
141                 if (lseek64(fd, seek, SEEK_SET) < 0) {
142                         printf("Cannot seek %s\n", strerror(errno));
143                         return 1;
144                 }
145                 /* reset all bytes to something nor 0x0 neither 0xab */
146                 memset(buf, 0x5e, len);
147                 rc = read(fd, buf, len);
148                 if (rc != len) {
149                         printf("Read error: %s rc = %d\n",strerror(errno),rc);
150                         return 1;
151                 }
152
153                 if (check_bytes(buf, pad, len) != len) {
154                         printf("Data mismatch\n");
155                         return 1;
156                 }
157         }
158
159         printf("PASS\n");
160         return 0;
161 #else /* !O_DIRECT */
162 #warning O_DIRECT not defined, directio test will fail
163         printf("O_DIRECT not defined\n");
164         return 1;
165 #endif /* !O_DIRECT */
166 }