Whamcloud - gitweb
LU-15210 tests: fix sanity-lnet to handle duplicate IP
[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  */
29
30 #ifndef _GNU_SOURCE
31 #define  _GNU_SOURCE
32 #endif
33 #include <stdio.h>
34 #include <string.h>
35 #include <unistd.h>
36 #include <fcntl.h>
37 #include <stdlib.h>
38 #include <errno.h>
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #include <sys/mman.h>
42
43 /*
44  * return index of the first byte not matching given byte
45  * or buffer size if all bytes are matching
46  */
47 static size_t check_bytes(const char *buf, int byte, size_t len)
48 {
49         const char *p;
50
51         for (p = buf; p < buf + len; p++)
52                 if (*p != byte)
53                         break;
54         return p - buf;
55 }
56
57 int main(int argc, char **argv)
58 {
59 #ifdef O_DIRECT
60         int fd;
61         char *buf, *fname;
62         int blocks, seek_blocks;
63         long len;
64         off64_t seek;
65         struct stat64 st;
66         char pad = 0xba;
67         int action;
68         int rc;
69
70         if (argc < 5 || argc > 6) {
71                 printf("Usage: %s <read/write/rdwr/readhole> file seek nr_blocks [blocksize]\n",
72                        argv[0]);
73                 return 1;
74         }
75
76         if (!strcmp(argv[1], "read")) {
77                 action = O_RDONLY;
78         } else if (!strcmp(argv[1], "write")) {
79                 action = O_WRONLY;
80         } else if (!strcmp(argv[1], "rdwr")) {
81                 action = O_RDWR;
82         } else if (!strcmp(argv[1], "readhole")) {
83                 action = O_RDONLY;
84                 pad = 0;
85         } else {
86                 printf("Usage: %s <read/write/rdwr> file seek nr_blocks [blocksize]\n",
87                        argv[0]);
88                 return 1;
89         }
90
91         fname = argv[2];
92         seek_blocks = strtoul(argv[3], 0, 0);
93         blocks = strtoul(argv[4], 0, 0);
94         if (!blocks) {
95                 printf("Usage: %s <read/write/rdwr> file seek nr_blocks [blocksize]\n",
96                        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                (unsigned long)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,
120                    PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON, 0, 0);
121         if (buf == MAP_FAILED) {
122                 printf("No memory %s\n", strerror(errno));
123                 return 1;
124         }
125         memset(buf, pad, len);
126
127         if (action == O_WRONLY || action == O_RDWR) {
128                 if (lseek64(fd, seek, SEEK_SET) < 0) {
129                         printf("lseek64 failed: %s\n", strerror(errno));
130                         return 1;
131                 }
132
133                 rc = write(fd, buf, len);
134                 if (rc != len) {
135                         printf("Write error %s (rc = %d, len = %ld)\n",
136                                strerror(errno), rc, len);
137                         return 1;
138                 }
139         }
140
141         if (action == O_RDONLY || action == O_RDWR) {
142                 if (lseek64(fd, seek, SEEK_SET) < 0) {
143                         printf("Cannot seek %s\n", strerror(errno));
144                         return 1;
145                 }
146                 /* reset all bytes to something nor 0x0 neither 0xab */
147                 memset(buf, 0x5e, len);
148                 rc = read(fd, buf, len);
149                 if (rc != len) {
150                         printf("Read error: %s rc = %d\n", strerror(errno), rc);
151                         return 1;
152                 }
153
154                 if (check_bytes(buf, pad, len) != len) {
155                         printf("Data mismatch\n");
156                         return 1;
157                 }
158         }
159
160         printf("PASS\n");
161         return 0;
162 #else /* !O_DIRECT */
163 #warning O_DIRECT not defined, directio test will fail
164         printf("O_DIRECT not defined\n");
165         return 1;
166 #endif /* !O_DIRECT */
167 }