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_stat_dotl(V9fsPDU
*pdu
, int rx
, size_t *offsetp
,
184 fprintf(llogfile
, "%s={", name
);
185 pprint_qid(pdu
, rx
, offsetp
, "qid");
186 pprint_int32(pdu
, rx
, offsetp
, ", st_mode");
187 pprint_int64(pdu
, rx
, offsetp
, ", st_nlink");
188 pprint_int32(pdu
, rx
, offsetp
, ", st_uid");
189 pprint_int32(pdu
, rx
, offsetp
, ", st_gid");
190 pprint_int64(pdu
, rx
, offsetp
, ", st_rdev");
191 pprint_int64(pdu
, rx
, offsetp
, ", st_size");
192 pprint_int64(pdu
, rx
, offsetp
, ", st_blksize");
193 pprint_int64(pdu
, rx
, offsetp
, ", st_blocks");
194 pprint_int64(pdu
, rx
, offsetp
, ", atime");
195 pprint_int64(pdu
, rx
, offsetp
, ", atime_nsec");
196 pprint_int64(pdu
, rx
, offsetp
, ", mtime");
197 pprint_int64(pdu
, rx
, offsetp
, ", mtime_nsec");
198 pprint_int64(pdu
, rx
, offsetp
, ", ctime");
199 pprint_int64(pdu
, rx
, offsetp
, ", ctime_nsec");
200 fprintf(llogfile
, "}");
205 static void pprint_strs(V9fsPDU
*pdu
, int rx
, size_t *offsetp
, const char *name
)
207 int sg_count
= get_sg_count(pdu
, rx
);
208 struct iovec
*sg
= get_sg(pdu
, rx
);
209 size_t offset
= *offsetp
;
210 uint16_t tmp_count
, count
, i
;
213 fprintf(llogfile
, "%s={", name
);
216 copied
= do_pdu_unpack(&tmp_count
, sg
, sg_count
, offset
, sizeof(tmp_count
));
217 BUG_ON(copied
!= sizeof(tmp_count
));
218 count
= le16_to_cpupu(&tmp_count
);
221 for (i
= 0; i
< count
; i
++) {
224 fprintf(llogfile
, ", ");
226 snprintf(str
, sizeof(str
), "[%d]", i
);
227 pprint_str(pdu
, rx
, &offset
, str
);
230 fprintf(llogfile
, "}");
235 static void pprint_qids(V9fsPDU
*pdu
, int rx
, size_t *offsetp
, const char *name
)
237 int sg_count
= get_sg_count(pdu
, rx
);
238 struct iovec
*sg
= get_sg(pdu
, rx
);
239 size_t offset
= *offsetp
;
240 uint16_t tmp_count
, count
, i
;
243 fprintf(llogfile
, "%s={", name
);
245 copied
= do_pdu_unpack(&tmp_count
, sg
, sg_count
, offset
, sizeof(tmp_count
));
246 BUG_ON(copied
!= sizeof(tmp_count
));
247 count
= le16_to_cpupu(&tmp_count
);
250 for (i
= 0; i
< count
; i
++) {
253 fprintf(llogfile
, ", ");
255 snprintf(str
, sizeof(str
), "[%d]", i
);
256 pprint_qid(pdu
, rx
, &offset
, str
);
259 fprintf(llogfile
, "}");
264 static void pprint_sg(V9fsPDU
*pdu
, int rx
, size_t *offsetp
, const char *name
)
266 struct iovec
*sg
= get_sg(pdu
, rx
);
271 count
= pdu
->elem
.in_num
;
273 count
= pdu
->elem
.out_num
;
276 fprintf(llogfile
, "%s={", name
);
277 for (i
= 0; i
< count
; i
++) {
279 fprintf(llogfile
, ", ");
281 fprintf(llogfile
, "(%p, 0x%zx)", sg
[i
].iov_base
, sg
[i
].iov_len
);
283 fprintf(llogfile
, "}");
286 /* FIXME: read from a directory fid returns serialized stat_t's */
288 static void pprint_data(V9fsPDU
*pdu
, int rx
, size_t *offsetp
, const char *name
)
290 struct iovec
*sg
= get_sg(pdu
, rx
);
291 size_t offset
= *offsetp
;
298 count
= pdu
->elem
.in_num
;
300 count
= pdu
->elem
.out_num
;
303 BUG_ON((offset
+ sizeof(size
)) > sg
[0].iov_len
);
305 memcpy(&size
, sg
[0].iov_base
+ offset
, sizeof(size
));
306 offset
+= sizeof(size
);
308 fprintf(llogfile
, "size: %x\n", size
);
310 sg
[0].iov_base
+= 11; /* skip header */
314 for (i
= 0; i
< count
; i
++) {
315 total
+= sg
[i
].iov_len
;
317 /* trim sg list so writev does the right thing */
318 sg
[i
].iov_len
-= (total
- size
);
324 fprintf(llogfile
, "%s={\"", name
);
326 for (j
= 0; j
< i
; j
++) {
328 fprintf(llogfile
, "\", \"");
333 len
= writev(fileno(llogfile
), &sg
[j
], 1);
334 } while (len
== -1 && errno
== EINTR
);
335 fprintf(llogfile
, "len == %ld: %m\n", len
);
336 BUG_ON(len
!= sg
[j
].iov_len
);
338 fprintf(llogfile
, "\"}");
340 sg
[0].iov_base
-= 11;
346 void pprint_pdu(V9fsPDU
*pdu
)
350 if (llogfile
== NULL
) {
351 llogfile
= fopen("/tmp/pdu.log", "w");
358 fprintf(llogfile
, "TREADDIR: (");
359 pprint_int32(pdu
, 0, &offset
, "fid");
360 pprint_int64(pdu
, 0, &offset
, ", initial offset");
361 pprint_int32(pdu
, 0, &offset
, ", max count");
364 fprintf(llogfile
, "RREADDIR: (");
365 pprint_int32(pdu
, 1, &offset
, "count");
367 pprint_data(pdu
, 1, &offset
, ", data");
371 fprintf(llogfile
, "TVERSION: (");
372 pprint_int32(pdu
, 0, &offset
, "msize");
373 pprint_str(pdu
, 0, &offset
, ", version");
376 fprintf(llogfile
, "RVERSION: (");
377 pprint_int32(pdu
, 1, &offset
, "msize");
378 pprint_str(pdu
, 1, &offset
, ", version");
381 fprintf(llogfile
, "TGETATTR: (");
382 pprint_int32(pdu
, 0, &offset
, "fid");
385 fprintf(llogfile
, "RGETATTR: (");
386 pprint_stat_dotl(pdu
, 1, &offset
, "getattr");
389 fprintf(llogfile
, "TAUTH: (");
390 pprint_int32(pdu
, 0, &offset
, "afid");
391 pprint_str(pdu
, 0, &offset
, ", uname");
392 pprint_str(pdu
, 0, &offset
, ", aname");
394 pprint_int32(pdu
, 0, &offset
, ", n_uname");
398 fprintf(llogfile
, "RAUTH: (");
399 pprint_qid(pdu
, 1, &offset
, "qid");
402 fprintf(llogfile
, "TATTACH: (");
403 pprint_int32(pdu
, 0, &offset
, "fid");
404 pprint_int32(pdu
, 0, &offset
, ", afid");
405 pprint_str(pdu
, 0, &offset
, ", uname");
406 pprint_str(pdu
, 0, &offset
, ", aname");
408 pprint_int32(pdu
, 0, &offset
, ", n_uname");
412 fprintf(llogfile
, "RATTACH: (");
413 pprint_qid(pdu
, 1, &offset
, "qid");
416 fprintf(llogfile
, "TERROR: (");
419 fprintf(llogfile
, "RERROR: (");
420 pprint_str(pdu
, 1, &offset
, "ename");
422 pprint_int32(pdu
, 1, &offset
, ", ecode");
426 fprintf(llogfile
, "TFLUSH: (");
427 pprint_int16(pdu
, 0, &offset
, "oldtag");
430 fprintf(llogfile
, "RFLUSH: (");
433 fprintf(llogfile
, "TWALK: (");
434 pprint_int32(pdu
, 0, &offset
, "fid");
435 pprint_int32(pdu
, 0, &offset
, ", newfid");
436 pprint_strs(pdu
, 0, &offset
, ", wnames");
439 fprintf(llogfile
, "RWALK: (");
440 pprint_qids(pdu
, 1, &offset
, "wqids");
443 fprintf(llogfile
, "TOPEN: (");
444 pprint_int32(pdu
, 0, &offset
, "fid");
445 pprint_int8(pdu
, 0, &offset
, ", mode");
448 fprintf(llogfile
, "ROPEN: (");
449 pprint_qid(pdu
, 1, &offset
, "qid");
450 pprint_int32(pdu
, 1, &offset
, ", iounit");
453 fprintf(llogfile
, "TCREATE: (");
454 pprint_int32(pdu
, 0, &offset
, "fid");
455 pprint_str(pdu
, 0, &offset
, ", name");
456 pprint_int32(pdu
, 0, &offset
, ", perm");
457 pprint_int8(pdu
, 0, &offset
, ", mode");
459 pprint_str(pdu
, 0, &offset
, ", extension");
463 fprintf(llogfile
, "RCREATE: (");
464 pprint_qid(pdu
, 1, &offset
, "qid");
465 pprint_int32(pdu
, 1, &offset
, ", iounit");
468 fprintf(llogfile
, "TREAD: (");
469 pprint_int32(pdu
, 0, &offset
, "fid");
470 pprint_int64(pdu
, 0, &offset
, ", offset");
471 pprint_int32(pdu
, 0, &offset
, ", count");
472 pprint_sg(pdu
, 0, &offset
, ", sg");
475 fprintf(llogfile
, "RREAD: (");
476 pprint_int32(pdu
, 1, &offset
, "count");
477 pprint_sg(pdu
, 1, &offset
, ", sg");
480 pprint_data(pdu
, 1, &offset
, ", data");
484 fprintf(llogfile
, "TWRITE: (");
485 pprint_int32(pdu
, 0, &offset
, "fid");
486 pprint_int64(pdu
, 0, &offset
, ", offset");
487 pprint_int32(pdu
, 0, &offset
, ", count");
490 fprintf(llogfile
, "RWRITE: (");
491 pprint_int32(pdu
, 1, &offset
, "count");
494 fprintf(llogfile
, "TCLUNK: (");
495 pprint_int32(pdu
, 0, &offset
, "fid");
498 fprintf(llogfile
, "RCLUNK: (");
501 fprintf(llogfile
, "TREMOVE: (");
502 pprint_int32(pdu
, 0, &offset
, "fid");
505 fprintf(llogfile
, "RREMOVE: (");
508 fprintf(llogfile
, "TSTAT: (");
509 pprint_int32(pdu
, 0, &offset
, "fid");
512 fprintf(llogfile
, "RSTAT: (");
513 offset
+= 2; /* ignored */
514 pprint_stat(pdu
, 1, &offset
, "stat");
517 fprintf(llogfile
, "TWSTAT: (");
518 pprint_int32(pdu
, 0, &offset
, "fid");
519 offset
+= 2; /* ignored */
520 pprint_stat(pdu
, 0, &offset
, ", stat");
523 fprintf(llogfile
, "RWSTAT: (");
526 fprintf(llogfile
, "unknown(%d): (", pdu
->id
);
530 fprintf(llogfile
, ")\n");
531 /* Flush the log message out */