Whamcloud - gitweb
LU-15210 tests: fix sanity-lnet to handle duplicate IP
[fs/lustre-release.git] / lustre / tests / test_brw.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  * Copyright (c) 2014, Intel Corporation.
27  */
28 /*
29  * This file is part of Lustre, http://www.lustre.org/
30  */
31
32 /* for O_DIRECT */
33 #ifndef _GNU_SOURCE
34 #define _GNU_SOURCE
35 #endif
36
37 #include <inttypes.h>
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/mman.h>
45 #include <sys/types.h>
46 #include <sys/stat.h>
47 #include <linux/types.h>
48
49 #define READ  1
50 #define WRITE 2
51
52 #define LPDS sizeof(uint64_t)
53 int block_debug_setup(void *addr, int len, uint64_t off, uint64_t id)
54 {
55         off = htole64(off);
56         id = htole64(id);
57         memcpy(addr, (char *)&off, LPDS);
58         memcpy(addr + LPDS, (char *)&id, LPDS);
59
60         addr += len - LPDS - LPDS;
61         memcpy(addr, (char *)&off, LPDS);
62         memcpy(addr + LPDS, (char *)&id, LPDS);
63
64         return 0;
65 }
66
67 int block_debug_check(char *who, void *addr, int size, uint64_t off,
68                       uint64_t id)
69 {
70         uint64_t ne_off;
71         int err = 0;
72
73         ne_off = le64toh(off);
74         id = le64toh(id);
75         if (memcmp(addr, (char *)&ne_off, LPDS)) {
76                 fprintf(stderr, "%s: for offset %"PRIu64" off: %"PRIx64" != %"PRIx64"\n",
77                         who, off, *(uint64_t *)addr, ne_off);
78                 err = -EINVAL;
79         }
80         if (memcmp(addr + LPDS, (char *)&id, LPDS)) {
81                 fprintf(stderr, "%s: for offset %"PRIu64" id: %"PRIx64" != %"PRIx64"\n",
82                         who, off, *(uint64_t *)(addr + LPDS), id);
83                 err = -EINVAL;
84         }
85
86         addr += size - LPDS - LPDS;
87         if (memcmp(addr, (char *)&ne_off, LPDS)) {
88                 fprintf(stderr, "%s: for offset %"PRIu64" end off: %"PRIx64" != %"PRIx64"\n",
89                         who, off, *(uint64_t *)addr, ne_off);
90                 err = -EINVAL;
91         }
92         if (memcmp(addr + LPDS, (char *)&id, LPDS)) {
93                 fprintf(stderr, "%s: for offset %"PRIu64" end id: %"PRIx64" != %"PRIx64"\n",
94                         who, off, *(uint64_t *)(addr + LPDS), id);
95                 err = -EINVAL;
96         }
97
98         return err;
99 }
100
101 #undef LPDS
102
103 void usage(char *prog)
104 {
105         fprintf(stderr,
106                 "usage: %s file count [[d]{r|w|rw} [pages_per_vec [objid]]]\n",
107                 prog);
108         exit(1);
109 }
110
111 int main(int argc, char **argv)
112 {
113         int fd;
114         char *buf;
115         long long count, last;
116         long pg_vec, len;
117         uint64_t objid, offset;
118         struct stat st;
119         int flags = 0;
120         int cmd = 0;
121         char *end;
122         int rc;
123
124         if (argc < 3 || argc > 6)
125                 usage(argv[0]);
126
127         count = strtoull(argv[2], &end, 0);
128         if (*end) {
129                 fprintf(stderr, "%s: invalid count '%s'\n", argv[0], argv[2]);
130                 usage(argv[0]);
131         }
132         if (argc >= 4) {
133                 if (strchr(argv[3], 'r')) {
134                         cmd = READ;
135                         flags = O_RDONLY;
136                 }
137                 if (strchr(argv[3], 'w')) {
138                         cmd |= WRITE;
139                         flags = O_RDWR | O_CREAT;
140                 }
141                 if (strchr(argv[3], 'd')) {
142 #ifdef O_DIRECT
143                         flags |= O_DIRECT;
144 #else
145                         fprintf(stderr,
146                                 "%s: O_DIRECT not supported in this build\n",
147                                 argv[0]);
148                         exit(1);
149 #endif
150                 }
151                 if (!cmd)
152                         usage(argv[0]);
153         } else {
154                 cmd = READ | WRITE;
155                 flags = O_RDWR | O_CREAT;
156 #ifdef O_DIRECT
157                 flags |= O_DIRECT;
158 #else
159                 fprintf(stderr, "%s: warning: not setting O_DIRECT\n",
160                         argv[0]);
161 #endif
162         }
163
164         if (argc >= 5) {
165                 pg_vec = strtoul(argv[4], &end, 0);
166                 if (*end) {
167                         fprintf(stderr, "%s: invalid pages_per_vec '%s'\n",
168                                 argv[0], argv[4]);
169                         usage(argv[0]);
170                 }
171         } else {
172                 pg_vec = 16;
173         }
174
175         if (argc >= 6) {
176                 objid = strtoull(argv[5], &end, 0);
177                 if (*end) {
178                         fprintf(stderr, "%s: invalid objid '%s'\n",
179                                 argv[0], argv[5]);
180                         usage(argv[0]);
181                 }
182         } else {
183                 objid = 3;
184         }
185
186         printf("%s: %s on %s(objid %"PRIx64") for %llux%ld pages\n",
187                argv[0],
188 #ifdef O_DIRECT
189                flags & O_DIRECT ? "directio" : "i/o",
190 #else
191                "i/o",
192 #endif
193                argv[1], objid, count, pg_vec);
194
195         fd = open(argv[1], flags | O_LARGEFILE);
196         if (fd == -1) {
197                 fprintf(stderr, "%s: cannot open %s:  %s\n", argv[0],
198                         argv[1], strerror(errno));
199                 return 3;
200         }
201
202         rc = fstat(fd, &st);
203         if (rc < 0) {
204                 fprintf(stderr, "%s: cannot stat %s: %s\n", argv[0],
205                         argv[1], strerror(errno));
206                 return 4;
207         }
208
209         len = pg_vec * st.st_blksize;
210         last = (long long)count * len;
211
212         buf = mmap(0, len, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANON,
213                    0, 0);
214         if (buf == MAP_FAILED) {
215                 fprintf(stderr, "%s: no buffer memory %s\n",
216                         argv[0], strerror(errno));
217                 return 2;
218         }
219
220         for (offset = 0; offset < last && cmd & WRITE; offset += len) {
221                 unsigned int i;
222
223                 for (i = 0; i < len; i += st.st_blksize)
224                         block_debug_setup(buf + i, st.st_blksize,
225                                           offset + i, objid);
226
227                 rc = write(fd, buf, len);
228
229                 for (i = 0; i < len; i += st.st_blksize) {
230                         if (block_debug_check("write", buf + i, st.st_blksize,
231                                               offset + i, objid))
232                                 return 10;
233                 }
234
235                 if (rc != len) {
236                         fprintf(stderr, "%s: write error: %s, rc %d != %ld\n",
237                                 argv[0], strerror(errno), rc, len);
238                         return 4;
239                 }
240         }
241
242         if (lseek(fd, 0, SEEK_SET) != 0) {
243                 fprintf(stderr, "%s: cannot seek %s\n",
244                         argv[0], strerror(errno));
245                 return 5;
246         }
247
248         for (offset = 0; offset < last && cmd & READ; offset += len) {
249                 int i;
250
251                 rc = read(fd, buf, len);
252                 if (rc != len) {
253                         fprintf(stderr, "%s: read error: %s, rc %d != %ld\n",
254                                 argv[0], strerror(errno), rc, len);
255                         return 6;
256                 }
257
258                 for (i = 0; i < len; i += st.st_blksize) {
259                         if (block_debug_check("read", buf + i, st.st_blksize,
260                                               offset + i, objid))
261                                 return 11;
262                 }
263         }
264
265         return 0;
266 }