4 * Copyright IBM, Corp. 2010
7 * Anthony Liguori <aliguori@us.ibm.com>
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");
334 fprintf(llogfile
, "TREADDIR: (");
335 pprint_int32(pdu
, 0, &offset
, "fid");
336 pprint_int64(pdu
, 0, &offset
, ", initial offset");
337 pprint_int32(pdu
, 0, &offset
, ", max count");
340 fprintf(llogfile
, "RREADDIR: (");
341 pprint_int32(pdu
, 1, &offset
, "count");
343 pprint_data(pdu
, 1, &offset
, ", data");
347 fprintf(llogfile
, "TVERSION: (");
348 pprint_int32(pdu
, 0, &offset
, "msize");
349 pprint_str(pdu
, 0, &offset
, ", version");
352 fprintf(llogfile
, "RVERSION: (");
353 pprint_int32(pdu
, 1, &offset
, "msize");
354 pprint_str(pdu
, 1, &offset
, ", version");
357 fprintf(llogfile
, "TAUTH: (");
358 pprint_int32(pdu
, 0, &offset
, "afid");
359 pprint_str(pdu
, 0, &offset
, ", uname");
360 pprint_str(pdu
, 0, &offset
, ", aname");
362 pprint_int32(pdu
, 0, &offset
, ", n_uname");
366 fprintf(llogfile
, "RAUTH: (");
367 pprint_qid(pdu
, 1, &offset
, "qid");
370 fprintf(llogfile
, "TATTACH: (");
371 pprint_int32(pdu
, 0, &offset
, "fid");
372 pprint_int32(pdu
, 0, &offset
, ", afid");
373 pprint_str(pdu
, 0, &offset
, ", uname");
374 pprint_str(pdu
, 0, &offset
, ", aname");
376 pprint_int32(pdu
, 0, &offset
, ", n_uname");
380 fprintf(llogfile
, "RATTACH: (");
381 pprint_qid(pdu
, 1, &offset
, "qid");
384 fprintf(llogfile
, "TERROR: (");
387 fprintf(llogfile
, "RERROR: (");
388 pprint_str(pdu
, 1, &offset
, "ename");
390 pprint_int32(pdu
, 1, &offset
, ", ecode");
394 fprintf(llogfile
, "TFLUSH: (");
395 pprint_int16(pdu
, 0, &offset
, "oldtag");
398 fprintf(llogfile
, "RFLUSH: (");
401 fprintf(llogfile
, "TWALK: (");
402 pprint_int32(pdu
, 0, &offset
, "fid");
403 pprint_int32(pdu
, 0, &offset
, ", newfid");
404 pprint_strs(pdu
, 0, &offset
, ", wnames");
407 fprintf(llogfile
, "RWALK: (");
408 pprint_qids(pdu
, 1, &offset
, "wqids");
411 fprintf(llogfile
, "TOPEN: (");
412 pprint_int32(pdu
, 0, &offset
, "fid");
413 pprint_int8(pdu
, 0, &offset
, ", mode");
416 fprintf(llogfile
, "ROPEN: (");
417 pprint_qid(pdu
, 1, &offset
, "qid");
418 pprint_int32(pdu
, 1, &offset
, ", iounit");
421 fprintf(llogfile
, "TCREATE: (");
422 pprint_int32(pdu
, 0, &offset
, "fid");
423 pprint_str(pdu
, 0, &offset
, ", name");
424 pprint_int32(pdu
, 0, &offset
, ", perm");
425 pprint_int8(pdu
, 0, &offset
, ", mode");
427 pprint_str(pdu
, 0, &offset
, ", extension");
431 fprintf(llogfile
, "RCREATE: (");
432 pprint_qid(pdu
, 1, &offset
, "qid");
433 pprint_int32(pdu
, 1, &offset
, ", iounit");
436 fprintf(llogfile
, "TREAD: (");
437 pprint_int32(pdu
, 0, &offset
, "fid");
438 pprint_int64(pdu
, 0, &offset
, ", offset");
439 pprint_int32(pdu
, 0, &offset
, ", count");
440 pprint_sg(pdu
, 0, &offset
, ", sg");
443 fprintf(llogfile
, "RREAD: (");
444 pprint_int32(pdu
, 1, &offset
, "count");
445 pprint_sg(pdu
, 1, &offset
, ", sg");
448 pprint_data(pdu
, 1, &offset
, ", data");
452 fprintf(llogfile
, "TWRITE: (");
453 pprint_int32(pdu
, 0, &offset
, "fid");
454 pprint_int64(pdu
, 0, &offset
, ", offset");
455 pprint_int32(pdu
, 0, &offset
, ", count");
458 fprintf(llogfile
, "RWRITE: (");
459 pprint_int32(pdu
, 1, &offset
, "count");
462 fprintf(llogfile
, "TCLUNK: (");
463 pprint_int32(pdu
, 0, &offset
, "fid");
466 fprintf(llogfile
, "RCLUNK: (");
469 fprintf(llogfile
, "TREMOVE: (");
470 pprint_int32(pdu
, 0, &offset
, "fid");
473 fprintf(llogfile
, "RREMOVE: (");
476 fprintf(llogfile
, "TSTAT: (");
477 pprint_int32(pdu
, 0, &offset
, "fid");
480 fprintf(llogfile
, "RSTAT: (");
481 offset
+= 2; /* ignored */
482 pprint_stat(pdu
, 1, &offset
, "stat");
485 fprintf(llogfile
, "TWSTAT: (");
486 pprint_int32(pdu
, 0, &offset
, "fid");
487 offset
+= 2; /* ignored */
488 pprint_stat(pdu
, 0, &offset
, ", stat");
491 fprintf(llogfile
, "RWSTAT: (");
494 fprintf(llogfile
, "unknown(%d): (", pdu
->id
);
498 fprintf(llogfile
, ")\n");
499 /* Flush the log message out */