Whamcloud - gitweb
LU-2140 test: add fake nid with proper nettype
[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.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 /* for O_DIRECT */
36 #ifndef _GNU_SOURCE
37 #define _GNU_SOURCE
38 #endif
39
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/mman.h>
47 #include <sys/types.h>
48 #include <sys/stat.h>
49
50 #include <liblustre.h>
51
52 #define READ  1
53 #define WRITE 2
54
55 #define LPDS sizeof(__u64)
56 int block_debug_setup(void *addr, int len, __u64 off, __u64 id)
57 {
58         off = cpu_to_le64(off);
59         id = cpu_to_le64(id);
60         memcpy(addr, (char *)&off, LPDS);
61         memcpy(addr + LPDS, (char *)&id, LPDS);
62
63         addr += len - LPDS - LPDS;
64         memcpy(addr, (char *)&off, LPDS);
65         memcpy(addr + LPDS, (char *)&id, LPDS);
66
67         return 0;
68 }
69
70 int block_debug_check(char *who, void *addr, int size, __u64 off, __u64 id)
71 {
72         __u64 ne_off;
73         int err = 0;
74
75         ne_off = le64_to_cpu(off);
76         id = le64_to_cpu(id);
77         if (memcmp(addr, (char *)&ne_off, LPDS)) {
78                 CERROR("%s: for offset "LPU64" off: "LPX64" != "LPX64"\n",
79                        who, off, *(__u64 *)addr, ne_off);
80                 err = -EINVAL;
81         }
82         if (memcmp(addr + LPDS, (char *)&id, LPDS)) {
83                 CERROR("%s: for offset "LPU64" id: "LPX64" != "LPX64"\n",
84                        who, off, *(__u64 *)(addr + LPDS), id);
85                 err = -EINVAL;
86         }
87
88         addr += size - LPDS - LPDS;
89         if (memcmp(addr, (char *)&ne_off, LPDS)) {
90                 CERROR("%s: for offset "LPU64" end off: "LPX64" != "LPX64"\n",
91                        who, off, *(__u64 *)addr, ne_off);
92                 err = -EINVAL;
93         }
94         if (memcmp(addr + LPDS, (char *)&id, LPDS)) {
95                 CERROR("%s: for offset "LPU64" end id: "LPX64" != "LPX64"\n",
96                        who, off, *(__u64 *)(addr + LPDS), id);
97                 err = -EINVAL;
98         }
99
100         return err;
101 }
102 #undef LPDS
103
104 void usage(char *prog)
105 {
106         fprintf(stderr,
107                 "usage: %s file count [[d]{r|w|rw} [pages_per_vec [objid]]]\n",
108                 prog);
109         exit(1);
110 }
111
112 int main(int argc, char **argv)
113 {
114         int fd;
115         char *buf;
116         long long count, last, offset;
117         long pg_vec, len;
118         __u64 objid;
119         struct stat st;
120         int flags = 0;
121         int cmd = 0;
122         char *end;
123         int rc;
124
125         if (argc < 3 || argc > 6)
126                 usage(argv[0]);
127
128         count = strtoull(argv[2], &end, 0);
129         if (*end) {
130                 fprintf(stderr, "%s: invalid count '%s'\n", argv[0], argv[2]);
131                 usage(argv[0]);
132         }
133         if (argc >= 4) {
134                 if (strchr(argv[3], 'r')) {
135                         cmd = READ;
136                         flags = O_RDONLY;
137                 }
138                 if (strchr(argv[3], 'w')) {
139                         cmd |= WRITE;
140                         flags = O_RDWR | O_CREAT;
141                 }
142                 if (strchr(argv[3], 'd')) {
143 #ifdef O_DIRECT
144                         flags |= O_DIRECT;
145 #else
146                         fprintf(stderr,
147                                 "%s: O_DIRECT not supported in this build\n",
148                                 argv[0]);
149                         exit(1);
150 #endif
151                 }
152                 if (!cmd)
153                         usage(argv[0]);
154         } else {
155                 cmd = READ | WRITE;
156                 flags = O_RDWR | O_CREAT;
157 #ifdef O_DIRECT
158                 flags |= O_DIRECT;
159 #else
160                 fprintf(stderr, "%s: warning: not setting O_DIRECT\n",
161                         argv[0]);
162 #endif
163         }
164
165         if (argc >= 5) {
166                 pg_vec = strtoul(argv[4], &end, 0);
167                 if (*end) {
168                         fprintf(stderr, "%s: invalid pages_per_vec '%s'\n",
169                                 argv[0], argv[4]);
170                         usage(argv[0]);
171                 }
172         } else {
173                 pg_vec = 16;
174         }
175
176         if (argc >= 6) {
177                 objid = strtoull(argv[5], &end, 0);
178                 if (*end) {
179                         fprintf(stderr, "%s: invalid objid '%s'\n",
180                                 argv[0], argv[5]);
181                         usage(argv[0]);
182                 }
183         } else {
184                 objid = 3;
185         }
186
187         printf("%s: %s on %s(objid "LPX64") for %llux%ld pages \n",
188                argv[0],
189 #ifdef O_DIRECT
190                flags & O_DIRECT ? "directio" : "i/o",
191 #else
192                "i/o",
193 #endif
194                argv[1], objid, count, pg_vec);
195
196         fd = open(argv[1], flags | O_LARGEFILE);
197         if (fd == -1) {
198                 fprintf(stderr, "%s: cannot open %s:  %s\n", argv[0],
199                         argv[1], strerror(errno));
200                 return 3;
201         }
202
203         rc = fstat(fd, &st);
204         if (rc < 0) {
205                 fprintf(stderr, "%s: cannot stat %s: %s\n", argv[0],
206                         argv[1], strerror(errno));
207                 return 4;
208         }
209
210         len = pg_vec * st.st_blksize;
211         last = (long long)count * len;
212
213         buf = mmap(0, len, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 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                 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 }