1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
17 #include <sys/types.h>
20 #include <liblustre.h>
25 #define LPDS sizeof(__u64)
26 int block_debug_setup(void *addr, int len, __u64 off, __u64 id)
28 off = cpu_to_le64(off);
30 memcpy(addr, (char *)&off, LPDS);
31 memcpy(addr + LPDS, (char *)&id, LPDS);
33 addr += len - LPDS - LPDS;
34 memcpy(addr, (char *)&off, LPDS);
35 memcpy(addr + LPDS, (char *)&id, LPDS);
40 int block_debug_check(char *who, void *addr, int size, __u64 off, __u64 id)
45 ne_off = le64_to_cpu(off);
47 if (memcmp(addr, (char *)&ne_off, LPDS)) {
48 CERROR("%s: for offset "LPU64" off: "LPX64" != "LPX64"\n",
49 who, off, *(__u64 *)addr, ne_off);
52 if (memcmp(addr + LPDS, (char *)&id, LPDS)) {
53 CERROR("%s: for offset "LPU64" id: "LPX64" != "LPX64"\n",
54 who, off, *(__u64 *)(addr + LPDS), id);
58 addr += size - LPDS - LPDS;
59 if (memcmp(addr, (char *)&ne_off, LPDS)) {
60 CERROR("%s: for offset "LPU64" end off: "LPX64" != "LPX64"\n",
61 who, off, *(__u64 *)addr, ne_off);
64 if (memcmp(addr + LPDS, (char *)&id, LPDS)) {
65 CERROR("%s: for offset "LPU64" end id: "LPX64" != "LPX64"\n",
66 who, off, *(__u64 *)(addr + LPDS), id);
74 void usage(char *prog)
77 "usage: %s file count [[d]{r|w|rw} [pages_per_vec [objid]]]\n",
82 int main(int argc, char **argv)
86 long long count, last, offset;
95 if (argc < 3 || argc > 6)
98 count = strtoull(argv[2], &end, 0);
100 fprintf(stderr, "%s: invalid count '%s'\n", argv[0], argv[2]);
104 if (strchr(argv[3], 'r')) {
108 if (strchr(argv[3], 'w')) {
110 flags = O_RDWR | O_CREAT;
112 if (strchr(argv[3], 'd')) {
117 "%s: O_DIRECT not supported in this build\n",
126 flags = O_RDWR | O_CREAT;
130 fprintf(stderr, "%s: warning: not setting O_DIRECT\n",
136 pg_vec = strtoul(argv[4], &end, 0);
138 fprintf(stderr, "%s: invalid pages_per_vec '%s'\n",
147 objid = strtoull(argv[5], &end, 0);
149 fprintf(stderr, "%s: invalid objid '%s'\n",
157 printf("%s: %s on %s(objid "LPX64") for %llux%ld pages \n",
160 flags & O_DIRECT ? "directio" : "i/o",
164 argv[1], objid, count, pg_vec);
166 fd = open(argv[1], flags | O_LARGEFILE);
168 fprintf(stderr, "%s: cannot open %s: %s\n", argv[0],
169 argv[1], strerror(errno));
175 fprintf(stderr, "%s: cannot stat %s: %s\n", argv[0],
176 argv[1], strerror(errno));
180 len = pg_vec * st.st_blksize;
181 last = (long long)count * len;
183 buf = mmap(0, len, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0);
184 if (buf == MAP_FAILED) {
185 fprintf(stderr, "%s: no buffer memory %s\n",
186 argv[0], strerror(errno));
190 for (offset = 0; offset < last && cmd & WRITE; offset += len) {
193 for (i = 0; i < len; i += st.st_blksize)
194 block_debug_setup(buf + i, st.st_blksize,
197 rc = write(fd, buf, len);
199 for (i = 0; i < len; i += st.st_blksize) {
200 if (block_debug_check("write", buf + i, st.st_blksize,
206 fprintf(stderr, "%s: write error: %s, rc %d != %ld\n",
207 argv[0], strerror(errno), rc, len);
212 if (lseek(fd, 0, SEEK_SET) != 0) {
213 fprintf(stderr, "%s: cannot seek %s\n",
214 argv[0], strerror(errno));
218 for (offset = 0; offset < last && cmd & READ; offset += len) {
221 rc = read(fd, buf, len);
223 fprintf(stderr, "%s: read error: %s, rc %d != %ld\n",
224 argv[0], strerror(errno), rc, len);
228 for (i = 0; i < len; i += st.st_blksize) {
229 if (block_debug_check("read", buf + i, st.st_blksize,