xref: /openbmc/qemu/tests/qtest/virtio-9p-test.c (revision a68694cd)
1 /*
2  * QTest testcase for VirtIO 9P
3  *
4  * Copyright (c) 2014 SUSE LINUX Products GmbH
5  *
6  * This work is licensed under the terms of the GNU GPL, version 2 or later.
7  * See the COPYING file in the top-level directory.
8  */
9 
10 #include "qemu/osdep.h"
11 #include "libqtest-single.h"
12 #include "qemu/module.h"
13 #include "hw/9pfs/9p.h"
14 #include "hw/9pfs/9p-synth.h"
15 #include "libqos/virtio-9p.h"
16 #include "libqos/qgraph.h"
17 
18 #define QVIRTIO_9P_TIMEOUT_US (10 * 1000 * 1000)
19 static QGuestAllocator *alloc;
20 
21 static void pci_config(void *obj, void *data, QGuestAllocator *t_alloc)
22 {
23     QVirtio9P *v9p = obj;
24     alloc = t_alloc;
25     size_t tag_len = qvirtio_config_readw(v9p->vdev, 0);
26     char *tag;
27     int i;
28 
29     g_assert_cmpint(tag_len, ==, strlen(MOUNT_TAG));
30 
31     tag = g_malloc(tag_len);
32     for (i = 0; i < tag_len; i++) {
33         tag[i] = qvirtio_config_readb(v9p->vdev, i + 2);
34     }
35     g_assert_cmpmem(tag, tag_len, MOUNT_TAG, tag_len);
36     g_free(tag);
37 }
38 
39 #define P9_MAX_SIZE 4096 /* Max size of a T-message or R-message */
40 
41 typedef struct {
42     QTestState *qts;
43     QVirtio9P *v9p;
44     uint16_t tag;
45     uint64_t t_msg;
46     uint32_t t_size;
47     uint64_t r_msg;
48     /* No r_size, it is hardcoded to P9_MAX_SIZE */
49     size_t t_off;
50     size_t r_off;
51     uint32_t free_head;
52 } P9Req;
53 
54 static void v9fs_memwrite(P9Req *req, const void *addr, size_t len)
55 {
56     qtest_memwrite(req->qts, req->t_msg + req->t_off, addr, len);
57     req->t_off += len;
58 }
59 
60 static void v9fs_memskip(P9Req *req, size_t len)
61 {
62     req->r_off += len;
63 }
64 
65 static void v9fs_memread(P9Req *req, void *addr, size_t len)
66 {
67     qtest_memread(req->qts, req->r_msg + req->r_off, addr, len);
68     req->r_off += len;
69 }
70 
71 static void v9fs_uint8_read(P9Req *req, uint8_t *val)
72 {
73     v9fs_memread(req, val, 1);
74 }
75 
76 static void v9fs_uint16_write(P9Req *req, uint16_t val)
77 {
78     uint16_t le_val = cpu_to_le16(val);
79 
80     v9fs_memwrite(req, &le_val, 2);
81 }
82 
83 static void v9fs_uint16_read(P9Req *req, uint16_t *val)
84 {
85     v9fs_memread(req, val, 2);
86     le16_to_cpus(val);
87 }
88 
89 static void v9fs_uint32_write(P9Req *req, uint32_t val)
90 {
91     uint32_t le_val = cpu_to_le32(val);
92 
93     v9fs_memwrite(req, &le_val, 4);
94 }
95 
96 static void v9fs_uint64_write(P9Req *req, uint64_t val)
97 {
98     uint64_t le_val = cpu_to_le64(val);
99 
100     v9fs_memwrite(req, &le_val, 8);
101 }
102 
103 static void v9fs_uint32_read(P9Req *req, uint32_t *val)
104 {
105     v9fs_memread(req, val, 4);
106     le32_to_cpus(val);
107 }
108 
109 static void v9fs_uint64_read(P9Req *req, uint64_t *val)
110 {
111     v9fs_memread(req, val, 8);
112     le64_to_cpus(val);
113 }
114 
115 /* len[2] string[len] */
116 static uint16_t v9fs_string_size(const char *string)
117 {
118     size_t len = strlen(string);
119 
120     g_assert_cmpint(len, <=, UINT16_MAX - 2);
121 
122     return 2 + len;
123 }
124 
125 static void v9fs_string_write(P9Req *req, const char *string)
126 {
127     int len = strlen(string);
128 
129     g_assert_cmpint(len, <=, UINT16_MAX);
130 
131     v9fs_uint16_write(req, (uint16_t) len);
132     v9fs_memwrite(req, string, len);
133 }
134 
135 static void v9fs_string_read(P9Req *req, uint16_t *len, char **string)
136 {
137     uint16_t local_len;
138 
139     v9fs_uint16_read(req, &local_len);
140     if (len) {
141         *len = local_len;
142     }
143     if (string) {
144         *string = g_malloc(local_len + 1);
145         v9fs_memread(req, *string, local_len);
146         (*string)[local_len] = 0;
147     } else {
148         v9fs_memskip(req, local_len);
149     }
150 }
151 
152  typedef struct {
153     uint32_t size;
154     uint8_t id;
155     uint16_t tag;
156 } QEMU_PACKED P9Hdr;
157 
158 static P9Req *v9fs_req_init(QVirtio9P *v9p, uint32_t size, uint8_t id,
159                             uint16_t tag)
160 {
161     P9Req *req = g_new0(P9Req, 1);
162     uint32_t total_size = 7; /* 9P header has well-known size of 7 bytes */
163     P9Hdr hdr = {
164         .id = id,
165         .tag = cpu_to_le16(tag)
166     };
167 
168     g_assert_cmpint(total_size, <=, UINT32_MAX - size);
169     total_size += size;
170     hdr.size = cpu_to_le32(total_size);
171 
172     g_assert_cmpint(total_size, <=, P9_MAX_SIZE);
173 
174     req->qts = global_qtest;
175     req->v9p = v9p;
176     req->t_size = total_size;
177     req->t_msg = guest_alloc(alloc, req->t_size);
178     v9fs_memwrite(req, &hdr, 7);
179     req->tag = tag;
180     return req;
181 }
182 
183 static void v9fs_req_send(P9Req *req)
184 {
185     QVirtio9P *v9p = req->v9p;
186 
187     req->r_msg = guest_alloc(alloc, P9_MAX_SIZE);
188     req->free_head = qvirtqueue_add(req->qts, v9p->vq, req->t_msg, req->t_size,
189                                     false, true);
190     qvirtqueue_add(req->qts, v9p->vq, req->r_msg, P9_MAX_SIZE, true, false);
191     qvirtqueue_kick(req->qts, v9p->vdev, v9p->vq, req->free_head);
192     req->t_off = 0;
193 }
194 
195 static const char *rmessage_name(uint8_t id)
196 {
197     return
198         id == P9_RLERROR ? "RLERROR" :
199         id == P9_RVERSION ? "RVERSION" :
200         id == P9_RATTACH ? "RATTACH" :
201         id == P9_RWALK ? "RWALK" :
202         id == P9_RLOPEN ? "RLOPEN" :
203         id == P9_RWRITE ? "RWRITE" :
204         id == P9_RFLUSH ? "RFLUSH" :
205         id == P9_RREADDIR ? "READDIR" :
206         "<unknown>";
207 }
208 
209 static void v9fs_req_wait_for_reply(P9Req *req, uint32_t *len)
210 {
211     QVirtio9P *v9p = req->v9p;
212 
213     qvirtio_wait_used_elem(req->qts, v9p->vdev, v9p->vq, req->free_head, len,
214                            QVIRTIO_9P_TIMEOUT_US);
215 }
216 
217 static void v9fs_req_recv(P9Req *req, uint8_t id)
218 {
219     P9Hdr hdr;
220 
221     v9fs_memread(req, &hdr, 7);
222     hdr.size = ldl_le_p(&hdr.size);
223     hdr.tag = lduw_le_p(&hdr.tag);
224 
225     g_assert_cmpint(hdr.size, >=, 7);
226     g_assert_cmpint(hdr.size, <=, P9_MAX_SIZE);
227     g_assert_cmpint(hdr.tag, ==, req->tag);
228 
229     if (hdr.id != id) {
230         g_printerr("Received response %d (%s) instead of %d (%s)\n",
231                    hdr.id, rmessage_name(hdr.id), id, rmessage_name(id));
232 
233         if (hdr.id == P9_RLERROR) {
234             uint32_t err;
235             v9fs_uint32_read(req, &err);
236             g_printerr("Rlerror has errno %d (%s)\n", err, strerror(err));
237         }
238     }
239     g_assert_cmpint(hdr.id, ==, id);
240 }
241 
242 static void v9fs_req_free(P9Req *req)
243 {
244     guest_free(alloc, req->t_msg);
245     guest_free(alloc, req->r_msg);
246     g_free(req);
247 }
248 
249 /* size[4] Rlerror tag[2] ecode[4] */
250 static void v9fs_rlerror(P9Req *req, uint32_t *err)
251 {
252     v9fs_req_recv(req, P9_RLERROR);
253     v9fs_uint32_read(req, err);
254     v9fs_req_free(req);
255 }
256 
257 /* size[4] Tversion tag[2] msize[4] version[s] */
258 static P9Req *v9fs_tversion(QVirtio9P *v9p, uint32_t msize, const char *version,
259                             uint16_t tag)
260 {
261     P9Req *req;
262     uint32_t body_size = 4;
263     uint16_t string_size = v9fs_string_size(version);
264 
265     g_assert_cmpint(body_size, <=, UINT32_MAX - string_size);
266     body_size += string_size;
267     req = v9fs_req_init(v9p, body_size, P9_TVERSION, tag);
268 
269     v9fs_uint32_write(req, msize);
270     v9fs_string_write(req, version);
271     v9fs_req_send(req);
272     return req;
273 }
274 
275 /* size[4] Rversion tag[2] msize[4] version[s] */
276 static void v9fs_rversion(P9Req *req, uint16_t *len, char **version)
277 {
278     uint32_t msize;
279 
280     v9fs_req_recv(req, P9_RVERSION);
281     v9fs_uint32_read(req, &msize);
282 
283     g_assert_cmpint(msize, ==, P9_MAX_SIZE);
284 
285     if (len || version) {
286         v9fs_string_read(req, len, version);
287     }
288 
289     v9fs_req_free(req);
290 }
291 
292 /* size[4] Tattach tag[2] fid[4] afid[4] uname[s] aname[s] n_uname[4] */
293 static P9Req *v9fs_tattach(QVirtio9P *v9p, uint32_t fid, uint32_t n_uname,
294                            uint16_t tag)
295 {
296     const char *uname = ""; /* ignored by QEMU */
297     const char *aname = ""; /* ignored by QEMU */
298     P9Req *req = v9fs_req_init(v9p, 4 + 4 + 2 + 2 + 4, P9_TATTACH, tag);
299 
300     v9fs_uint32_write(req, fid);
301     v9fs_uint32_write(req, P9_NOFID);
302     v9fs_string_write(req, uname);
303     v9fs_string_write(req, aname);
304     v9fs_uint32_write(req, n_uname);
305     v9fs_req_send(req);
306     return req;
307 }
308 
309 typedef char v9fs_qid[13];
310 
311 /* size[4] Rattach tag[2] qid[13] */
312 static void v9fs_rattach(P9Req *req, v9fs_qid *qid)
313 {
314     v9fs_req_recv(req, P9_RATTACH);
315     if (qid) {
316         v9fs_memread(req, qid, 13);
317     }
318     v9fs_req_free(req);
319 }
320 
321 /* size[4] Twalk tag[2] fid[4] newfid[4] nwname[2] nwname*(wname[s]) */
322 static P9Req *v9fs_twalk(QVirtio9P *v9p, uint32_t fid, uint32_t newfid,
323                          uint16_t nwname, char *const wnames[], uint16_t tag)
324 {
325     P9Req *req;
326     int i;
327     uint32_t body_size = 4 + 4 + 2;
328 
329     for (i = 0; i < nwname; i++) {
330         uint16_t wname_size = v9fs_string_size(wnames[i]);
331 
332         g_assert_cmpint(body_size, <=, UINT32_MAX - wname_size);
333         body_size += wname_size;
334     }
335     req = v9fs_req_init(v9p,  body_size, P9_TWALK, tag);
336     v9fs_uint32_write(req, fid);
337     v9fs_uint32_write(req, newfid);
338     v9fs_uint16_write(req, nwname);
339     for (i = 0; i < nwname; i++) {
340         v9fs_string_write(req, wnames[i]);
341     }
342     v9fs_req_send(req);
343     return req;
344 }
345 
346 /* size[4] Rwalk tag[2] nwqid[2] nwqid*(wqid[13]) */
347 static void v9fs_rwalk(P9Req *req, uint16_t *nwqid, v9fs_qid **wqid)
348 {
349     uint16_t local_nwqid;
350 
351     v9fs_req_recv(req, P9_RWALK);
352     v9fs_uint16_read(req, &local_nwqid);
353     if (nwqid) {
354         *nwqid = local_nwqid;
355     }
356     if (wqid) {
357         *wqid = g_malloc(local_nwqid * 13);
358         v9fs_memread(req, *wqid, local_nwqid * 13);
359     }
360     v9fs_req_free(req);
361 }
362 
363 /* size[4] Treaddir tag[2] fid[4] offset[8] count[4] */
364 static P9Req *v9fs_treaddir(QVirtio9P *v9p, uint32_t fid, uint64_t offset,
365                             uint32_t count, uint16_t tag)
366 {
367     P9Req *req;
368 
369     req = v9fs_req_init(v9p, 4 + 8 + 4, P9_TREADDIR, tag);
370     v9fs_uint32_write(req, fid);
371     v9fs_uint64_write(req, offset);
372     v9fs_uint32_write(req, count);
373     v9fs_req_send(req);
374     return req;
375 }
376 
377 struct V9fsDirent {
378     v9fs_qid qid;
379     uint64_t offset;
380     uint8_t type;
381     char *name;
382     struct V9fsDirent *next;
383 };
384 
385 /* size[4] Rreaddir tag[2] count[4] data[count] */
386 static void v9fs_rreaddir(P9Req *req, uint32_t *count, uint32_t *nentries,
387                           struct V9fsDirent **entries)
388 {
389     uint32_t local_count;
390     struct V9fsDirent *e = NULL;
391     uint16_t slen;
392     uint32_t n = 0;
393 
394     v9fs_req_recv(req, P9_RREADDIR);
395     v9fs_uint32_read(req, &local_count);
396 
397     if (count) {
398         *count = local_count;
399     }
400 
401     for (int32_t togo = (int32_t)local_count;
402          togo >= 13 + 8 + 1 + 2;
403          togo -= 13 + 8 + 1 + 2 + slen, ++n)
404     {
405         if (!e) {
406             e = g_malloc(sizeof(struct V9fsDirent));
407             if (entries) {
408                 *entries = e;
409             }
410         } else {
411             e = e->next = g_malloc(sizeof(struct V9fsDirent));
412         }
413         e->next = NULL;
414         /* qid[13] offset[8] type[1] name[s] */
415         v9fs_memread(req, &e->qid, 13);
416         v9fs_uint64_read(req, &e->offset);
417         v9fs_uint8_read(req, &e->type);
418         v9fs_string_read(req, &slen, &e->name);
419     }
420 
421     if (nentries) {
422         *nentries = n;
423     }
424 
425     v9fs_req_free(req);
426 }
427 
428 static void v9fs_free_dirents(struct V9fsDirent *e)
429 {
430     struct V9fsDirent *next = NULL;
431 
432     for (; e; e = next) {
433         next = e->next;
434         g_free(e->name);
435         g_free(e);
436     }
437 }
438 
439 /* size[4] Tlopen tag[2] fid[4] flags[4] */
440 static P9Req *v9fs_tlopen(QVirtio9P *v9p, uint32_t fid, uint32_t flags,
441                           uint16_t tag)
442 {
443     P9Req *req;
444 
445     req = v9fs_req_init(v9p,  4 + 4, P9_TLOPEN, tag);
446     v9fs_uint32_write(req, fid);
447     v9fs_uint32_write(req, flags);
448     v9fs_req_send(req);
449     return req;
450 }
451 
452 /* size[4] Rlopen tag[2] qid[13] iounit[4] */
453 static void v9fs_rlopen(P9Req *req, v9fs_qid *qid, uint32_t *iounit)
454 {
455     v9fs_req_recv(req, P9_RLOPEN);
456     if (qid) {
457         v9fs_memread(req, qid, 13);
458     } else {
459         v9fs_memskip(req, 13);
460     }
461     if (iounit) {
462         v9fs_uint32_read(req, iounit);
463     }
464     v9fs_req_free(req);
465 }
466 
467 /* size[4] Twrite tag[2] fid[4] offset[8] count[4] data[count] */
468 static P9Req *v9fs_twrite(QVirtio9P *v9p, uint32_t fid, uint64_t offset,
469                           uint32_t count, const void *data, uint16_t tag)
470 {
471     P9Req *req;
472     uint32_t body_size = 4 + 8 + 4;
473 
474     g_assert_cmpint(body_size, <=, UINT32_MAX - count);
475     body_size += count;
476     req = v9fs_req_init(v9p,  body_size, P9_TWRITE, tag);
477     v9fs_uint32_write(req, fid);
478     v9fs_uint64_write(req, offset);
479     v9fs_uint32_write(req, count);
480     v9fs_memwrite(req, data, count);
481     v9fs_req_send(req);
482     return req;
483 }
484 
485 /* size[4] Rwrite tag[2] count[4] */
486 static void v9fs_rwrite(P9Req *req, uint32_t *count)
487 {
488     v9fs_req_recv(req, P9_RWRITE);
489     if (count) {
490         v9fs_uint32_read(req, count);
491     }
492     v9fs_req_free(req);
493 }
494 
495 /* size[4] Tflush tag[2] oldtag[2] */
496 static P9Req *v9fs_tflush(QVirtio9P *v9p, uint16_t oldtag, uint16_t tag)
497 {
498     P9Req *req;
499 
500     req = v9fs_req_init(v9p,  2, P9_TFLUSH, tag);
501     v9fs_uint32_write(req, oldtag);
502     v9fs_req_send(req);
503     return req;
504 }
505 
506 /* size[4] Rflush tag[2] */
507 static void v9fs_rflush(P9Req *req)
508 {
509     v9fs_req_recv(req, P9_RFLUSH);
510     v9fs_req_free(req);
511 }
512 
513 static void fs_version(void *obj, void *data, QGuestAllocator *t_alloc)
514 {
515     QVirtio9P *v9p = obj;
516     alloc = t_alloc;
517     const char *version = "9P2000.L";
518     uint16_t server_len;
519     char *server_version;
520     P9Req *req;
521 
522     req = v9fs_tversion(v9p, P9_MAX_SIZE, version, P9_NOTAG);
523     v9fs_req_wait_for_reply(req, NULL);
524     v9fs_rversion(req, &server_len, &server_version);
525 
526     g_assert_cmpmem(server_version, server_len, version, strlen(version));
527 
528     g_free(server_version);
529 }
530 
531 static void fs_attach(void *obj, void *data, QGuestAllocator *t_alloc)
532 {
533     QVirtio9P *v9p = obj;
534     alloc = t_alloc;
535     P9Req *req;
536 
537     fs_version(v9p, NULL, t_alloc);
538     req = v9fs_tattach(v9p, 0, getuid(), 0);
539     v9fs_req_wait_for_reply(req, NULL);
540     v9fs_rattach(req, NULL);
541 }
542 
543 static void fs_walk(void *obj, void *data, QGuestAllocator *t_alloc)
544 {
545     QVirtio9P *v9p = obj;
546     alloc = t_alloc;
547     char *wnames[P9_MAXWELEM];
548     uint16_t nwqid;
549     v9fs_qid *wqid;
550     int i;
551     P9Req *req;
552 
553     for (i = 0; i < P9_MAXWELEM; i++) {
554         wnames[i] = g_strdup_printf(QTEST_V9FS_SYNTH_WALK_FILE, i);
555     }
556 
557     fs_attach(v9p, NULL, t_alloc);
558     req = v9fs_twalk(v9p, 0, 1, P9_MAXWELEM, wnames, 0);
559     v9fs_req_wait_for_reply(req, NULL);
560     v9fs_rwalk(req, &nwqid, &wqid);
561 
562     g_assert_cmpint(nwqid, ==, P9_MAXWELEM);
563 
564     for (i = 0; i < P9_MAXWELEM; i++) {
565         g_free(wnames[i]);
566     }
567 
568     g_free(wqid);
569 }
570 
571 static bool fs_dirents_contain_name(struct V9fsDirent *e, const char* name)
572 {
573     for (; e; e = e->next) {
574         if (!strcmp(e->name, name)) {
575             return true;
576         }
577     }
578     return false;
579 }
580 
581 /* basic readdir test where reply fits into a single response message */
582 static void fs_readdir(void *obj, void *data, QGuestAllocator *t_alloc)
583 {
584     QVirtio9P *v9p = obj;
585     alloc = t_alloc;
586     char *const wnames[] = { g_strdup(QTEST_V9FS_SYNTH_READDIR_DIR) };
587     uint16_t nqid;
588     v9fs_qid qid;
589     uint32_t count, nentries;
590     struct V9fsDirent *entries = NULL;
591     P9Req *req;
592 
593     fs_attach(v9p, NULL, t_alloc);
594     req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0);
595     v9fs_req_wait_for_reply(req, NULL);
596     v9fs_rwalk(req, &nqid, NULL);
597     g_assert_cmpint(nqid, ==, 1);
598 
599     req = v9fs_tlopen(v9p, 1, O_DIRECTORY, 0);
600     v9fs_req_wait_for_reply(req, NULL);
601     v9fs_rlopen(req, &qid, NULL);
602 
603     /*
604      * submit count = msize - 11, because 11 is the header size of Rreaddir
605      */
606     req = v9fs_treaddir(v9p, 1, 0, P9_MAX_SIZE - 11, 0);
607     v9fs_req_wait_for_reply(req, NULL);
608     v9fs_rreaddir(req, &count, &nentries, &entries);
609 
610     /*
611      * Assuming msize (P9_MAX_SIZE) is large enough so we can retrieve all
612      * dir entries with only one readdir request.
613      */
614     g_assert_cmpint(
615         nentries, ==,
616         QTEST_V9FS_SYNTH_READDIR_NFILES + 2 /* "." and ".." */
617     );
618 
619     /*
620      * Check all file names exist in returned entries, ignore their order
621      * though.
622      */
623     g_assert_cmpint(fs_dirents_contain_name(entries, "."), ==, true);
624     g_assert_cmpint(fs_dirents_contain_name(entries, ".."), ==, true);
625     for (int i = 0; i < QTEST_V9FS_SYNTH_READDIR_NFILES; ++i) {
626         char *name = g_strdup_printf(QTEST_V9FS_SYNTH_READDIR_FILE, i);
627         g_assert_cmpint(fs_dirents_contain_name(entries, name), ==, true);
628         g_free(name);
629     }
630 
631     v9fs_free_dirents(entries);
632     g_free(wnames[0]);
633 }
634 
635 /* readdir test where overall request is split over several messages */
636 static void fs_readdir_split(void *obj, void *data, QGuestAllocator *t_alloc,
637                              uint32_t count)
638 {
639     QVirtio9P *v9p = obj;
640     alloc = t_alloc;
641     char *const wnames[] = { g_strdup(QTEST_V9FS_SYNTH_READDIR_DIR) };
642     uint16_t nqid;
643     v9fs_qid qid;
644     uint32_t nentries, npartialentries;
645     struct V9fsDirent *entries, *tail, *partialentries;
646     P9Req *req;
647     int fid;
648     uint64_t offset;
649 
650     fs_attach(v9p, NULL, t_alloc);
651 
652     fid = 1;
653     offset = 0;
654     entries = NULL;
655     nentries = 0;
656     tail = NULL;
657 
658     req = v9fs_twalk(v9p, 0, fid, 1, wnames, 0);
659     v9fs_req_wait_for_reply(req, NULL);
660     v9fs_rwalk(req, &nqid, NULL);
661     g_assert_cmpint(nqid, ==, 1);
662 
663     req = v9fs_tlopen(v9p, fid, O_DIRECTORY, 0);
664     v9fs_req_wait_for_reply(req, NULL);
665     v9fs_rlopen(req, &qid, NULL);
666 
667     /*
668      * send as many Treaddir requests as required to get all directory
669      * entries
670      */
671     while (true) {
672         npartialentries = 0;
673         partialentries = NULL;
674 
675         req = v9fs_treaddir(v9p, fid, offset, count, 0);
676         v9fs_req_wait_for_reply(req, NULL);
677         v9fs_rreaddir(req, &count, &npartialentries, &partialentries);
678         if (npartialentries > 0 && partialentries) {
679             if (!entries) {
680                 entries = partialentries;
681                 nentries = npartialentries;
682                 tail = partialentries;
683             } else {
684                 tail->next = partialentries;
685                 nentries += npartialentries;
686             }
687             while (tail->next) {
688                 tail = tail->next;
689             }
690             offset = tail->offset;
691         } else {
692             break;
693         }
694     }
695 
696     g_assert_cmpint(
697         nentries, ==,
698         QTEST_V9FS_SYNTH_READDIR_NFILES + 2 /* "." and ".." */
699     );
700 
701     /*
702      * Check all file names exist in returned entries, ignore their order
703      * though.
704      */
705     g_assert_cmpint(fs_dirents_contain_name(entries, "."), ==, true);
706     g_assert_cmpint(fs_dirents_contain_name(entries, ".."), ==, true);
707     for (int i = 0; i < QTEST_V9FS_SYNTH_READDIR_NFILES; ++i) {
708         char *name = g_strdup_printf(QTEST_V9FS_SYNTH_READDIR_FILE, i);
709         g_assert_cmpint(fs_dirents_contain_name(entries, name), ==, true);
710         g_free(name);
711     }
712 
713     v9fs_free_dirents(entries);
714 
715     g_free(wnames[0]);
716 }
717 
718 static void fs_walk_no_slash(void *obj, void *data, QGuestAllocator *t_alloc)
719 {
720     QVirtio9P *v9p = obj;
721     alloc = t_alloc;
722     char *const wnames[] = { g_strdup(" /") };
723     P9Req *req;
724     uint32_t err;
725 
726     fs_attach(v9p, NULL, t_alloc);
727     req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0);
728     v9fs_req_wait_for_reply(req, NULL);
729     v9fs_rlerror(req, &err);
730 
731     g_assert_cmpint(err, ==, ENOENT);
732 
733     g_free(wnames[0]);
734 }
735 
736 static void fs_walk_dotdot(void *obj, void *data, QGuestAllocator *t_alloc)
737 {
738     QVirtio9P *v9p = obj;
739     alloc = t_alloc;
740     char *const wnames[] = { g_strdup("..") };
741     v9fs_qid root_qid, *wqid;
742     P9Req *req;
743 
744     fs_version(v9p, NULL, t_alloc);
745     req = v9fs_tattach(v9p, 0, getuid(), 0);
746     v9fs_req_wait_for_reply(req, NULL);
747     v9fs_rattach(req, &root_qid);
748 
749     req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0);
750     v9fs_req_wait_for_reply(req, NULL);
751     v9fs_rwalk(req, NULL, &wqid); /* We now we'll get one qid */
752 
753     g_assert_cmpmem(&root_qid, 13, wqid[0], 13);
754 
755     g_free(wqid);
756     g_free(wnames[0]);
757 }
758 
759 static void fs_lopen(void *obj, void *data, QGuestAllocator *t_alloc)
760 {
761     QVirtio9P *v9p = obj;
762     alloc = t_alloc;
763     char *const wnames[] = { g_strdup(QTEST_V9FS_SYNTH_LOPEN_FILE) };
764     P9Req *req;
765 
766     fs_attach(v9p, NULL, t_alloc);
767     req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0);
768     v9fs_req_wait_for_reply(req, NULL);
769     v9fs_rwalk(req, NULL, NULL);
770 
771     req = v9fs_tlopen(v9p, 1, O_WRONLY, 0);
772     v9fs_req_wait_for_reply(req, NULL);
773     v9fs_rlopen(req, NULL, NULL);
774 
775     g_free(wnames[0]);
776 }
777 
778 static void fs_write(void *obj, void *data, QGuestAllocator *t_alloc)
779 {
780     QVirtio9P *v9p = obj;
781     alloc = t_alloc;
782     static const uint32_t write_count = P9_MAX_SIZE / 2;
783     char *const wnames[] = { g_strdup(QTEST_V9FS_SYNTH_WRITE_FILE) };
784     char *buf = g_malloc0(write_count);
785     uint32_t count;
786     P9Req *req;
787 
788     fs_attach(v9p, NULL, t_alloc);
789     req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0);
790     v9fs_req_wait_for_reply(req, NULL);
791     v9fs_rwalk(req, NULL, NULL);
792 
793     req = v9fs_tlopen(v9p, 1, O_WRONLY, 0);
794     v9fs_req_wait_for_reply(req, NULL);
795     v9fs_rlopen(req, NULL, NULL);
796 
797     req = v9fs_twrite(v9p, 1, 0, write_count, buf, 0);
798     v9fs_req_wait_for_reply(req, NULL);
799     v9fs_rwrite(req, &count);
800     g_assert_cmpint(count, ==, write_count);
801 
802     g_free(buf);
803     g_free(wnames[0]);
804 }
805 
806 static void fs_flush_success(void *obj, void *data, QGuestAllocator *t_alloc)
807 {
808     QVirtio9P *v9p = obj;
809     alloc = t_alloc;
810     char *const wnames[] = { g_strdup(QTEST_V9FS_SYNTH_FLUSH_FILE) };
811     P9Req *req, *flush_req;
812     uint32_t reply_len;
813     uint8_t should_block;
814 
815     fs_attach(v9p, NULL, t_alloc);
816     req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0);
817     v9fs_req_wait_for_reply(req, NULL);
818     v9fs_rwalk(req, NULL, NULL);
819 
820     req = v9fs_tlopen(v9p, 1, O_WRONLY, 0);
821     v9fs_req_wait_for_reply(req, NULL);
822     v9fs_rlopen(req, NULL, NULL);
823 
824     /* This will cause the 9p server to try to write data to the backend,
825      * until the write request gets cancelled.
826      */
827     should_block = 1;
828     req = v9fs_twrite(v9p, 1, 0, sizeof(should_block), &should_block, 0);
829 
830     flush_req = v9fs_tflush(v9p, req->tag, 1);
831 
832     /* The write request is supposed to be flushed: the server should just
833      * mark the write request as used and reply to the flush request.
834      */
835     v9fs_req_wait_for_reply(req, &reply_len);
836     g_assert_cmpint(reply_len, ==, 0);
837     v9fs_req_free(req);
838     v9fs_rflush(flush_req);
839 
840     g_free(wnames[0]);
841 }
842 
843 static void fs_flush_ignored(void *obj, void *data, QGuestAllocator *t_alloc)
844 {
845     QVirtio9P *v9p = obj;
846     alloc = t_alloc;
847     char *const wnames[] = { g_strdup(QTEST_V9FS_SYNTH_FLUSH_FILE) };
848     P9Req *req, *flush_req;
849     uint32_t count;
850     uint8_t should_block;
851 
852     fs_attach(v9p, NULL, t_alloc);
853     req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0);
854     v9fs_req_wait_for_reply(req, NULL);
855     v9fs_rwalk(req, NULL, NULL);
856 
857     req = v9fs_tlopen(v9p, 1, O_WRONLY, 0);
858     v9fs_req_wait_for_reply(req, NULL);
859     v9fs_rlopen(req, NULL, NULL);
860 
861     /* This will cause the write request to complete right away, before it
862      * could be actually cancelled.
863      */
864     should_block = 0;
865     req = v9fs_twrite(v9p, 1, 0, sizeof(should_block), &should_block, 0);
866 
867     flush_req = v9fs_tflush(v9p, req->tag, 1);
868 
869     /* The write request is supposed to complete. The server should
870      * reply to the write request and the flush request.
871      */
872     v9fs_req_wait_for_reply(req, NULL);
873     v9fs_rwrite(req, &count);
874     g_assert_cmpint(count, ==, sizeof(should_block));
875     v9fs_rflush(flush_req);
876 
877     g_free(wnames[0]);
878 }
879 
880 static void fs_readdir_split_128(void *obj, void *data,
881                                  QGuestAllocator *t_alloc)
882 {
883     fs_readdir_split(obj, data, t_alloc, 128);
884 }
885 
886 static void fs_readdir_split_256(void *obj, void *data,
887                                  QGuestAllocator *t_alloc)
888 {
889     fs_readdir_split(obj, data, t_alloc, 256);
890 }
891 
892 static void fs_readdir_split_512(void *obj, void *data,
893                                  QGuestAllocator *t_alloc)
894 {
895     fs_readdir_split(obj, data, t_alloc, 512);
896 }
897 
898 static void register_virtio_9p_test(void)
899 {
900     qos_add_test("config", "virtio-9p", pci_config, NULL);
901     qos_add_test("fs/version/basic", "virtio-9p", fs_version, NULL);
902     qos_add_test("fs/attach/basic", "virtio-9p", fs_attach, NULL);
903     qos_add_test("fs/walk/basic", "virtio-9p", fs_walk, NULL);
904     qos_add_test("fs/walk/no_slash", "virtio-9p", fs_walk_no_slash,
905                  NULL);
906     qos_add_test("fs/walk/dotdot_from_root", "virtio-9p",
907                  fs_walk_dotdot, NULL);
908     qos_add_test("fs/lopen/basic", "virtio-9p", fs_lopen, NULL);
909     qos_add_test("fs/write/basic", "virtio-9p", fs_write, NULL);
910     qos_add_test("fs/flush/success", "virtio-9p", fs_flush_success,
911                  NULL);
912     qos_add_test("fs/flush/ignored", "virtio-9p", fs_flush_ignored,
913                  NULL);
914     qos_add_test("fs/readdir/basic", "virtio-9p", fs_readdir, NULL);
915     qos_add_test("fs/readdir/split_512", "virtio-9p",
916                  fs_readdir_split_512, NULL);
917     qos_add_test("fs/readdir/split_256", "virtio-9p",
918                  fs_readdir_split_256, NULL);
919     qos_add_test("fs/readdir/split_128", "virtio-9p",
920                  fs_readdir_split_128, NULL);
921 }
922 
923 libqos_init(register_virtio_9p_test);
924