Whamcloud - gitweb
Allow test_brw to run in either read-only, write-only, or read-write mode,
[fs/lustre-release.git] / lustre / tests / test_brw.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <unistd.h>
4 #include <fcntl.h>
5 #include <stdlib.h>
6 #include <errno.h>
7 #include <sys/mman.h>
8
9 // not correctly in the headers yet!!
10 //#define O_DIRECT 0
11 #ifndef O_DIRECT
12 #define O_DIRECT         040000 /* direct disk access hint */
13 #endif
14
15 #define BLOCKSIZE 4096
16 #define CERROR(fmt, arg...) fprintf(stderr, fmt, ## arg)
17 #define __u64 long long
18 #define LASSERT(v) do {} while(0)
19 #define HTON__u64(v) (v)
20 #define LPU64 "%Lu"
21 #define LPX64 "%Lx"
22
23 #define READ  1
24 #define WRITE 2
25
26 #define LPDS sizeof(__u64)
27 int page_debug_setup(void *addr, int len, __u64 off, __u64 id)
28 {
29         LASSERT(addr);
30
31         off = HTON__u64(off);
32         id = HTON__u64(id);
33         memcpy(addr, (char *)&off, LPDS);
34         memcpy(addr + LPDS, (char *)&id, LPDS);
35
36         addr += len - LPDS - LPDS;
37         memcpy(addr, (char *)&off, LPDS);
38         memcpy(addr + LPDS, (char *)&id, LPDS);
39
40         return 0;
41 }
42
43 int page_debug_check(char *who, void *addr, int end, __u64 off, __u64 id)
44 {
45         __u64 ne_off;
46         int err = 0;
47
48         LASSERT(addr);
49
50         ne_off = HTON__u64(off);
51         id = HTON__u64(id);
52         if (memcmp(addr, (char *)&ne_off, LPDS)) {
53                 CERROR("%s: for offset "LPU64" off: "LPX64" != "LPX64"\n",
54                        who, off, *(__u64 *)addr, ne_off);
55                 err = -EINVAL;
56         }
57         if (memcmp(addr + LPDS, (char *)&id, LPDS)) {
58                 CERROR("%s: for offset "LPU64" id: "LPX64" != "LPX64"\n",
59                        who, off, *(__u64 *)(addr + LPDS), id);
60                 err = -EINVAL;
61         }
62
63         addr += end - LPDS - LPDS;
64         if (memcmp(addr, (char *)&ne_off, LPDS)) {
65                 CERROR("%s: for offset "LPU64" end off: "LPX64" != "LPX64"\n",
66                        who, off, *(__u64 *)addr, ne_off);
67                 err = -EINVAL;
68         }
69         if (memcmp(addr + LPDS, (char *)&id, LPDS)) {
70                 CERROR("%s: for offset "LPU64" end id: "LPX64" != "LPX64"\n",
71                        who, off, *(__u64 *)(addr + LPDS), id);
72                 err = -EINVAL;
73         }
74
75         return err;
76 }
77 #undef LPDS
78
79 void usage(char *prog)
80 {
81         fprintf(stderr, "usage: %s file count [[d]{r|w|rw} [pages_per_vec [objid]]]\n",
82                 prog);
83         exit(1);
84 }
85
86 int main(int argc, char **argv)
87 {
88         int fd;
89         char *buf;
90         long pg_vec, count;
91         long len;
92         long long end, offset;
93         long long objid = 3;
94         int flags = O_RDWR | O_CREAT;
95         int cmd = 0;
96         int rc;
97
98         if (argc < 3 || argc > 6)
99                 usage(argv[0]);
100
101         count = strtoul(argv[2], 0, 0);
102         if (argc >= 4) {
103                 if (strchr(argv[3], 'r')) {
104                         cmd |= READ;
105                         printf("reading\n");
106                 }
107                 if (strchr(argv[3], 'w')) {
108                         cmd |= WRITE;
109                         printf("writing\n");
110                 }
111                 if (strchr(argv[3], 'd')) {
112                         flags |= O_DIRECT;
113                         printf("directing\n");
114                 }
115         }
116         if (!cmd)
117                 usage(argv[0]);
118         printf("cmd = %x, flags = %x\n", cmd, flags);
119
120         if (argc >= 5)
121                 pg_vec = strtoul(argv[4], 0, 0);
122         len = pg_vec * BLOCKSIZE;
123         end = (long long)count * len;
124
125         if (argc >= 6) {
126                 objid = strtoull(argv[5], 0, 0);
127                 printf("objid %s = 0x%Lx\n", argv[5], objid);
128         }
129
130         printf("%s: %s on %s(objid 0x"LPX64") for %ldx%ld pages \n",
131                argv[0], flags & O_DIRECT ? "directio" : "i/o",
132                argv[1], objid, count, pg_vec);
133
134         buf = mmap(0, len, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0);
135         if (!buf) {
136                 fprintf(stderr, "%s: no buffer memory %s\n",
137                         argv[0], strerror(errno));
138                 return 2;
139         }
140
141         fd = open(argv[1], flags);
142         if (fd == -1) {
143                 fprintf(stderr, "%s: cannot open %s:  %s\n", argv[1],
144                         argv[0], strerror(errno));
145                 return 3;
146         }
147
148         for (offset = 0; offset < end && cmd & WRITE; offset += len) {
149                 int i;
150
151                 for (i = 0; i < len; i += BLOCKSIZE)
152                         page_debug_setup(buf + i, BLOCKSIZE, offset + i, objid);
153
154                 rc = write(fd, buf, len);
155
156                 for (i = 0; i < len; i += BLOCKSIZE) {
157                         if (page_debug_check("write", buf + i, BLOCKSIZE,
158                                              offset + i, objid))
159                                 return 10;
160                 }
161
162                 if (rc != len) {
163                         fprintf(stderr, "%s: write error: %s, rc %d\n",
164                                 argv[0], strerror(errno), rc);
165                         return 4;
166                 }
167         }
168
169         if (lseek(fd, 0, SEEK_SET) != 0) {
170                 fprintf(stderr, "%s: cannot seek %s\n",
171                         argv[0], strerror(errno));
172                 return 5;
173         }
174
175         for (offset = 0; offset < end && cmd && READ; offset += len) {
176                 int i;
177
178                 rc = read(fd, buf, len);
179                 if (rc != len) {
180                         fprintf(stderr, "%s: read error: %s, rc %d\n",
181                                 argv[0], strerror(errno), rc);
182                         return 6;
183                 }
184
185                 for (i = 0; i < len; i += BLOCKSIZE) {
186                         if (page_debug_check("read", buf + i, BLOCKSIZE,
187                                              offset + i, objid))
188                                 return 11;
189                 }
190         }
191
192         return 0;
193 }