4 * Copyright IBM, Corp. 2010
9 * This work is licensed under the terms of the GNU GPL, version 2. See
10 * the COPYING file in the top-level directory.
15 #include "virtio-9p.h"
16 #include "virtio-9p-debug.h"
18 #define BUG_ON(cond) assert(!(cond))
20 static FILE *llogfile;
22 static struct iovec *get_sg(V9fsPDU *pdu, int rx)
25 return pdu->elem.in_sg;
27 return pdu->elem.out_sg;
30 static int get_sg_count(V9fsPDU *pdu, int rx)
33 return pdu->elem.in_num;
35 return pdu->elem.out_num;
39 static void pprint_int8(V9fsPDU *pdu, int rx, size_t *offsetp,
43 int count = get_sg_count(pdu, rx);
44 size_t offset = *offsetp;
45 struct iovec *sg = get_sg(pdu, rx);
48 copied = do_pdu_unpack(&value, sg, count, offset, sizeof(value));
50 BUG_ON(copied != sizeof(value));
51 offset += sizeof(value);
52 fprintf(llogfile, "%s=0x%x", name, value);
56 static void pprint_int16(V9fsPDU *pdu, int rx, size_t *offsetp,
60 int count = get_sg_count(pdu, rx);
61 struct iovec *sg = get_sg(pdu, rx);
62 size_t offset = *offsetp;
66 copied = do_pdu_unpack(&value, sg, count, offset, sizeof(value));
68 BUG_ON(copied != sizeof(value));
69 offset += sizeof(value);
70 fprintf(llogfile, "%s=0x%x", name, value);
74 static void pprint_int32(V9fsPDU *pdu, int rx, size_t *offsetp,
78 int count = get_sg_count(pdu, rx);
79 struct iovec *sg = get_sg(pdu, rx);
80 size_t offset = *offsetp;
84 copied = do_pdu_unpack(&value, sg, count, offset, sizeof(value));
86 BUG_ON(copied != sizeof(value));
87 offset += sizeof(value);
88 fprintf(llogfile, "%s=0x%x", name, value);
92 static void pprint_int64(V9fsPDU *pdu, int rx, size_t *offsetp,
96 int count = get_sg_count(pdu, rx);
97 struct iovec *sg = get_sg(pdu, rx);
98 size_t offset = *offsetp;
102 copied = do_pdu_unpack(&value, sg, count, offset, sizeof(value));
104 BUG_ON(copied != sizeof(value));
105 offset += sizeof(value);
106 fprintf(llogfile, "%s=0x%" PRIx64, name, value);
110 static void pprint_str(V9fsPDU *pdu, int rx, size_t *offsetp, const char *name)
112 int sg_count = get_sg_count(pdu, rx);
113 struct iovec *sg = get_sg(pdu, rx);
114 size_t offset = *offsetp;
115 uint16_t tmp_size, size;
121 copied = do_pdu_unpack(&tmp_size, sg, sg_count, offset, sizeof(tmp_size));
122 BUG_ON(copied != sizeof(tmp_size));
123 size = le16_to_cpupu(&tmp_size);
126 fprintf(llogfile, "%s=", name);
127 for (i = 0; size && i < sg_count; i++) {
129 if (offset >= sg[i].iov_len) {
131 offset -= sg[i].iov_len;
134 len = MIN(sg[i].iov_len - offset, size);
135 result = fwrite(sg[i].iov_base + offset, 1, len, llogfile);
136 BUG_ON(result != len);
148 static void pprint_qid(V9fsPDU *pdu, int rx, size_t *offsetp, const char *name)
150 fprintf(llogfile, "%s={", name);
151 pprint_int8(pdu, rx, offsetp, "type");
152 pprint_int32(pdu, rx, offsetp, ", version");
153 pprint_int64(pdu, rx, offsetp, ", path");
154 fprintf(llogfile, "}");
157 static void pprint_stat(V9fsPDU *pdu, int rx, size_t *offsetp, const char *name)
159 fprintf(llogfile, "%s={", name);
160 pprint_int16(pdu, rx, offsetp, "size");
161 pprint_int16(pdu, rx, offsetp, ", type");
162 pprint_int32(pdu, rx, offsetp, ", dev");
163 pprint_qid(pdu, rx, offsetp, ", qid");
164 pprint_int32(pdu, rx, offsetp, ", mode");
165 pprint_int32(pdu, rx, offsetp, ", atime");
166 pprint_int32(pdu, rx, offsetp, ", mtime");
167 pprint_int64(pdu, rx, offsetp, ", length");
168 pprint_str(pdu, rx, offsetp, ", name");
169 pprint_str(pdu, rx, offsetp, ", uid");
170 pprint_str(pdu, rx, offsetp, ", gid");
171 pprint_str(pdu, rx, offsetp, ", muid");
173 pprint_str(pdu, rx, offsetp, ", extension");
174 pprint_int32(pdu, rx, offsetp, ", uid");
175 pprint_int32(pdu, rx, offsetp, ", gid");
176 pprint_int32(pdu, rx, offsetp, ", muid");
178 fprintf(llogfile, "}");
181 static void pprint_strs(V9fsPDU *pdu, int rx, size_t *offsetp, const char *name)
183 int sg_count = get_sg_count(pdu, rx);
184 struct iovec *sg = get_sg(pdu, rx);
185 size_t offset = *offsetp;
186 uint16_t tmp_count, count, i;
189 fprintf(llogfile, "%s={", name);
192 copied = do_pdu_unpack(&tmp_count, sg, sg_count, offset, sizeof(tmp_count));
193 BUG_ON(copied != sizeof(tmp_count));
194 count = le16_to_cpupu(&tmp_count);
197 for (i = 0; i < count; i++) {
200 fprintf(llogfile, ", ");
202 snprintf(str, sizeof(str), "[%d]", i);
203 pprint_str(pdu, rx, &offset, str);
206 fprintf(llogfile, "}");
211 static void pprint_qids(V9fsPDU *pdu, int rx, size_t *offsetp, const char *name)
213 int sg_count = get_sg_count(pdu, rx);
214 struct iovec *sg = get_sg(pdu, rx);
215 size_t offset = *offsetp;
216 uint16_t tmp_count, count, i;
219 fprintf(llogfile, "%s={", name);
221 copied = do_pdu_unpack(&tmp_count, sg, sg_count, offset, sizeof(tmp_count));
222 BUG_ON(copied != sizeof(tmp_count));
223 count = le16_to_cpupu(&tmp_count);
226 for (i = 0; i < count; i++) {
229 fprintf(llogfile, ", ");
231 snprintf(str, sizeof(str), "[%d]", i);
232 pprint_qid(pdu, rx, &offset, str);
235 fprintf(llogfile, "}");
240 static void pprint_sg(V9fsPDU *pdu, int rx, size_t *offsetp, const char *name)
242 struct iovec *sg = get_sg(pdu, rx);
247 count = pdu->elem.in_num;
249 count = pdu->elem.out_num;
252 fprintf(llogfile, "%s={", name);
253 for (i = 0; i < count; i++) {
255 fprintf(llogfile, ", ");
257 fprintf(llogfile, "(%p, 0x%zx)", sg[i].iov_base, sg[i].iov_len);
259 fprintf(llogfile, "}");
262 /* FIXME: read from a directory fid returns serialized stat_t's */
264 static void pprint_data(V9fsPDU *pdu, int rx, size_t *offsetp, const char *name)
266 struct iovec *sg = get_sg(pdu, rx);
267 size_t offset = *offsetp;
274 count = pdu->elem.in_num;
276 count = pdu->elem.out_num;
279 BUG_ON((offset + sizeof(size)) > sg[0].iov_len);
281 memcpy(&size, sg[0].iov_base + offset, sizeof(size));
282 offset += sizeof(size);
284 fprintf(llogfile, "size: %x\n", size);
286 sg[0].iov_base += 11; /* skip header */
290 for (i = 0; i < count; i++) {
291 total += sg[i].iov_len;
293 /* trim sg list so writev does the right thing */
294 sg[i].iov_len -= (total - size);
300 fprintf(llogfile, "%s={\"", name);
302 for (j = 0; j < i; j++) {
304 fprintf(llogfile, "\", \"");
309 len = writev(fileno(llogfile), &sg[j], 1);
310 } while (len == -1 && errno == EINTR);
311 fprintf(llogfile, "len == %ld: %m\n", len);
312 BUG_ON(len != sg[j].iov_len);
314 fprintf(llogfile, "\"}");
316 sg[0].iov_base -= 11;
322 void pprint_pdu(V9fsPDU *pdu)
326 if (llogfile == NULL) {
327 llogfile = fopen("/tmp/pdu.log", "w");
332 fprintf(llogfile, "TVERSION: (");
333 pprint_int32(pdu, 0, &offset, "msize");
334 pprint_str(pdu, 0, &offset, ", version");
337 fprintf(llogfile, "RVERSION: (");
338 pprint_int32(pdu, 1, &offset, "msize");
339 pprint_str(pdu, 1, &offset, ", version");
342 fprintf(llogfile, "TAUTH: (");
343 pprint_int32(pdu, 0, &offset, "afid");
344 pprint_str(pdu, 0, &offset, ", uname");
345 pprint_str(pdu, 0, &offset, ", aname");
347 pprint_int32(pdu, 0, &offset, ", n_uname");
351 fprintf(llogfile, "RAUTH: (");
352 pprint_qid(pdu, 1, &offset, "qid");
355 fprintf(llogfile, "TATTACH: (");
356 pprint_int32(pdu, 0, &offset, "fid");
357 pprint_int32(pdu, 0, &offset, ", afid");
358 pprint_str(pdu, 0, &offset, ", uname");
359 pprint_str(pdu, 0, &offset, ", aname");
361 pprint_int32(pdu, 0, &offset, ", n_uname");
365 fprintf(llogfile, "RATTACH: (");
366 pprint_qid(pdu, 1, &offset, "qid");
369 fprintf(llogfile, "TERROR: (");
372 fprintf(llogfile, "RERROR: (");
373 pprint_str(pdu, 1, &offset, "ename");
375 pprint_int32(pdu, 1, &offset, ", ecode");
379 fprintf(llogfile, "TFLUSH: (");
380 pprint_int16(pdu, 0, &offset, "oldtag");
383 fprintf(llogfile, "RFLUSH: (");
386 fprintf(llogfile, "TWALK: (");
387 pprint_int32(pdu, 0, &offset, "fid");
388 pprint_int32(pdu, 0, &offset, ", newfid");
389 pprint_strs(pdu, 0, &offset, ", wnames");
392 fprintf(llogfile, "RWALK: (");
393 pprint_qids(pdu, 1, &offset, "wqids");
396 fprintf(llogfile, "TOPEN: (");
397 pprint_int32(pdu, 0, &offset, "fid");
398 pprint_int8(pdu, 0, &offset, ", mode");
401 fprintf(llogfile, "ROPEN: (");
402 pprint_qid(pdu, 1, &offset, "qid");
403 pprint_int32(pdu, 1, &offset, ", iounit");
406 fprintf(llogfile, "TCREATE: (");
407 pprint_int32(pdu, 0, &offset, "fid");
408 pprint_str(pdu, 0, &offset, ", name");
409 pprint_int32(pdu, 0, &offset, ", perm");
410 pprint_int8(pdu, 0, &offset, ", mode");
412 pprint_str(pdu, 0, &offset, ", extension");
416 fprintf(llogfile, "RCREATE: (");
417 pprint_qid(pdu, 1, &offset, "qid");
418 pprint_int32(pdu, 1, &offset, ", iounit");
421 fprintf(llogfile, "TREAD: (");
422 pprint_int32(pdu, 0, &offset, "fid");
423 pprint_int64(pdu, 0, &offset, ", offset");
424 pprint_int32(pdu, 0, &offset, ", count");
425 pprint_sg(pdu, 0, &offset, ", sg");
428 fprintf(llogfile, "RREAD: (");
429 pprint_int32(pdu, 1, &offset, "count");
430 pprint_sg(pdu, 1, &offset, ", sg");
433 pprint_data(pdu, 1, &offset, ", data");
437 fprintf(llogfile, "TWRITE: (");
438 pprint_int32(pdu, 0, &offset, "fid");
439 pprint_int64(pdu, 0, &offset, ", offset");
440 pprint_int32(pdu, 0, &offset, ", count");
443 fprintf(llogfile, "RWRITE: (");
444 pprint_int32(pdu, 1, &offset, "count");
447 fprintf(llogfile, "TCLUNK: (");
448 pprint_int32(pdu, 0, &offset, "fid");
451 fprintf(llogfile, "RCLUNK: (");
454 fprintf(llogfile, "TREMOVE: (");
455 pprint_int32(pdu, 0, &offset, "fid");
458 fprintf(llogfile, "RREMOVE: (");
461 fprintf(llogfile, "TSTAT: (");
462 pprint_int32(pdu, 0, &offset, "fid");
465 fprintf(llogfile, "RSTAT: (");
466 offset += 2; /* ignored */
467 pprint_stat(pdu, 1, &offset, "stat");
470 fprintf(llogfile, "TWSTAT: (");
471 pprint_int32(pdu, 0, &offset, "fid");
472 offset += 2; /* ignored */
473 pprint_stat(pdu, 0, &offset, ", stat");
476 fprintf(llogfile, "RWSTAT: (");
479 fprintf(llogfile, "unknown(%d): (", pdu->id);
483 fprintf(llogfile, ")\n");