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