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