1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
15 #include <sys/types.h>
18 #include <liblustre.h>
23 #define LPDS sizeof(__u64)
24 int block_debug_setup(void *addr, int len, __u64 off, __u64 id)
26 off = cpu_to_le64(off);
28 memcpy(addr, (char *)&off, LPDS);
29 memcpy(addr + LPDS, (char *)&id, LPDS);
31 addr += len - LPDS - LPDS;
32 memcpy(addr, (char *)&off, LPDS);
33 memcpy(addr + LPDS, (char *)&id, LPDS);
38 int block_debug_check(char *who, void *addr, int size, __u64 off, __u64 id)
43 ne_off = le64_to_cpu(off);
45 if (memcmp(addr, (char *)&ne_off, LPDS)) {
46 CERROR("%s: for offset "LPU64" off: "LPX64" != "LPX64"\n",
47 who, off, *(__u64 *)addr, ne_off);
50 if (memcmp(addr + LPDS, (char *)&id, LPDS)) {
51 CERROR("%s: for offset "LPU64" id: "LPX64" != "LPX64"\n",
52 who, off, *(__u64 *)(addr + LPDS), id);
56 addr += size - LPDS - LPDS;
57 if (memcmp(addr, (char *)&ne_off, LPDS)) {
58 CERROR("%s: for offset "LPU64" end off: "LPX64" != "LPX64"\n",
59 who, off, *(__u64 *)addr, ne_off);
62 if (memcmp(addr + LPDS, (char *)&id, LPDS)) {
63 CERROR("%s: for offset "LPU64" end id: "LPX64" != "LPX64"\n",
64 who, off, *(__u64 *)(addr + LPDS), id);
72 void usage(char *prog)
75 "usage: %s file count [[d]{r|w|rw} [pages_per_vec [objid]]]\n",
80 int main(int argc, char **argv)
84 loff_t count, last, offset;
93 if (argc < 3 || argc > 6)
96 count = strtoull(argv[2], &end, 0);
98 fprintf(stderr, "%s: invalid count '%s'\n", argv[0], argv[2]);
102 if (strchr(argv[3], 'r')) {
106 if (strchr(argv[3], 'w')) {
108 flags = O_RDWR | O_CREAT;
110 if (strchr(argv[3], 'd')) {
115 "%s: O_DIRECT not supported in this build\n",
124 flags = O_RDWR | O_CREAT;
128 fprintf(stderr, "%s: warning: not setting O_DIRECT\n",
134 pg_vec = strtoul(argv[4], &end, 0);
136 fprintf(stderr, "%s: invalid pages_per_vec '%s'\n",
145 objid = strtoull(argv[5], &end, 0);
147 fprintf(stderr, "%s: invalid objid '%s'\n",
155 printf("%s: %s on %s(objid "LPX64") for "LPU64"x%ld pages \n",
158 flags & O_DIRECT ? "directio" : "i/o",
162 argv[1], objid, count, pg_vec);
164 fd = open(argv[1], flags | O_LARGEFILE);
166 fprintf(stderr, "%s: cannot open %s: %s\n", argv[0],
167 argv[1], strerror(errno));
173 fprintf(stderr, "%s: cannot stat %s: %s\n", argv[0],
174 argv[1], strerror(errno));
178 len = pg_vec * st.st_blksize;
179 last = (long long)count * len;
181 buf = mmap(0, len, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANON, 0, 0);
182 if (buf == MAP_FAILED) {
183 fprintf(stderr, "%s: no buffer memory %s\n",
184 argv[0], strerror(errno));
188 for (offset = 0; offset < last && cmd & WRITE; offset += len) {
191 for (i = 0; i < len; i += st.st_blksize)
192 block_debug_setup(buf + i, st.st_blksize,
195 rc = write(fd, buf, len);
197 for (i = 0; i < len; i += st.st_blksize) {
198 if (block_debug_check("write", buf + i, st.st_blksize,
204 fprintf(stderr, "%s: write error: %s, rc %d != %ld\n",
205 argv[0], strerror(errno), rc, len);
210 if (lseek(fd, 0, SEEK_SET) != 0) {
211 fprintf(stderr, "%s: cannot seek %s\n",
212 argv[0], strerror(errno));
216 for (offset = 0; offset < last && cmd & READ; offset += len) {
219 rc = read(fd, buf, len);
221 fprintf(stderr, "%s: read error: %s, rc %d != %ld\n",
222 argv[0], strerror(errno), rc, len);
226 for (i = 0; i < len; i += st.st_blksize) {
227 if (block_debug_check("read", buf + i, st.st_blksize,