Whamcloud - gitweb
e4ee62df0c181ecd416095fa75a6058861345ebb
[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 int main(int argc, char **argv)
51 {
52 #ifdef O_DIRECT
53         int fd;
54         char *wbuf, *fname;
55         int blocks, seek_blocks;
56         long len;
57         off64_t seek;
58         struct stat64 st;
59         char pad = 0xba;
60         int action;
61         int rc;
62
63         if (argc < 5 || argc > 6) {
64                 printf("Usage: %s <read/write/rdwr/readhole> file seek nr_blocks [blocksize]\n", argv[0]);
65                 return 1;
66         }
67
68         if (!strcmp(argv[1], "read"))
69                 action = O_RDONLY;
70         else if (!strcmp(argv[1], "write"))
71                 action = O_WRONLY;
72         else if (!strcmp(argv[1], "rdwr"))
73                 action = O_RDWR;
74         else if (!strcmp(argv[1], "readhole")) {
75                 action = O_RDONLY;
76                 pad = 0;
77         } else {
78                 printf("Usage: %s <read/write/rdwr> file seek nr_blocks [blocksize]\n", argv[0]);
79                 return 1;
80         }
81
82         fname = argv[2];
83         seek_blocks = strtoul(argv[3], 0, 0);
84         blocks = strtoul(argv[4], 0, 0);
85         if (!blocks) {
86                 printf("Usage: %s <read/write/rdwr> file seek nr_blocks [blocksize]\n", argv[0]);
87                 return 1;
88         }
89
90         fd = open(fname, O_LARGEFILE | O_DIRECT | O_RDWR | O_CREAT, 0644);
91         if (fd == -1) {
92                 printf("Cannot open %s:  %s\n", fname, strerror(errno));
93                 return 1;
94         }
95
96         if (argc >= 6)
97                 st.st_blksize = strtoul(argv[5], 0, 0);
98         else if (fstat64(fd, &st) < 0) {
99                 printf("Cannot stat %s:  %s\n", fname, strerror(errno));
100                 return 1;
101         }
102
103         printf("directio on %s for %dx%lu bytes \n", fname, blocks,
104                st.st_blksize);
105
106         seek = (off64_t)seek_blocks * (off64_t)st.st_blksize;
107         len = blocks * st.st_blksize;
108
109         wbuf = mmap(0, len, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0);
110         if (wbuf == MAP_FAILED) {
111                 printf("No memory %s\n", strerror(errno));
112                 return 1;
113         }
114         memset(wbuf, pad, len);
115
116         if (action == O_WRONLY || action == O_RDWR) {
117                 if (lseek64(fd, seek, SEEK_SET) < 0) {
118                         printf("lseek64 failed: %s\n", strerror(errno));
119                         return 1;
120                 }
121
122                 rc = write(fd, wbuf, len);
123                 if (rc != len) {
124                         printf("Write error %s (rc = %d, len = %ld)\n",
125                                strerror(errno), rc, len);
126                         return 1;
127                 }
128         }
129
130         if (action == O_RDONLY || action == O_RDWR) {
131                 char *rbuf;
132
133                 if (lseek64(fd, seek, SEEK_SET) < 0) {
134                         printf("Cannot seek %s\n", strerror(errno));
135                         return 1;
136                 }
137
138                 rbuf =mmap(0,len,PROT_READ|PROT_WRITE,MAP_PRIVATE|MAP_ANON,0,0);
139                 if (rbuf == MAP_FAILED) {
140                         printf("No memory %s\n", strerror(errno));
141                         return 1;
142                 }
143
144                 rc = read(fd, rbuf, len);
145                 if (rc != len) {
146                         printf("Read error: %s rc = %d\n",strerror(errno),rc);
147                         return 1;
148                 }
149
150                 if (memcmp(wbuf, rbuf, len)) {
151                         printf("Data mismatch\n");
152                         return 1;
153                 }
154         }
155
156         printf("PASS\n");
157         return 0;
158 #else /* !O_DIRECT */
159 #warning O_DIRECT not defined, directio test will fail
160         printf("O_DIRECT not defined\n");
161         return 1;
162 #endif /* !O_DIRECT */
163 }