xref: /openbmc/qemu/tests/qtest/virtio-9p-test.c (revision a6caeee8)
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 /*
11  * Not so fast! You might want to read the 9p developer docs first:
12  * https://wiki.qemu.org/Documentation/9p
13  */
14 
15 #include "qemu/osdep.h"
16 #include "libqtest-single.h"
17 #include "qemu/module.h"
18 #include "hw/9pfs/9p.h"
19 #include "hw/9pfs/9p-synth.h"
20 #include "libqos/virtio-9p.h"
21 #include "libqos/qgraph.h"
22 
23 #define QVIRTIO_9P_TIMEOUT_US (10 * 1000 * 1000)
24 static QGuestAllocator *alloc;
25 
26 /*
27  * Used to auto generate new fids. Start with arbitrary high value to avoid
28  * collision with hard coded fids in basic test code.
29  */
30 static uint32_t fid_generator = 1000;
31 
32 static uint32_t genfid(void)
33 {
34     return fid_generator++;
35 }
36 
37 /**
38  * Splits the @a in string by @a delim into individual (non empty) strings
39  * and outputs them to @a out. The output array @a out is NULL terminated.
40  *
41  * Output array @a out must be freed by calling split_free().
42  *
43  * @returns number of individual elements in output array @a out (without the
44  *          final NULL terminating element)
45  */
46 static int split(const char *in, const char *delim, char ***out)
47 {
48     int n = 0, i = 0;
49     char *tmp, *p;
50 
51     tmp = g_strdup(in);
52     for (p = strtok(tmp, delim); p != NULL; p = strtok(NULL, delim)) {
53         if (strlen(p) > 0) {
54             ++n;
55         }
56     }
57     g_free(tmp);
58 
59     *out = g_new0(char *, n + 1); /* last element NULL delimiter */
60 
61     tmp = g_strdup(in);
62     for (p = strtok(tmp, delim); p != NULL; p = strtok(NULL, delim)) {
63         if (strlen(p) > 0) {
64             (*out)[i++] = g_strdup(p);
65         }
66     }
67     g_free(tmp);
68 
69     return n;
70 }
71 
72 static void split_free(char ***out)
73 {
74     int i;
75     for (i = 0; (*out)[i]; ++i) {
76         g_free((*out)[i]);
77     }
78     g_free(*out);
79     *out = NULL;
80 }
81 
82 static void pci_config(void *obj, void *data, QGuestAllocator *t_alloc)
83 {
84     QVirtio9P *v9p = obj;
85     alloc = t_alloc;
86     size_t tag_len = qvirtio_config_readw(v9p->vdev, 0);
87     g_autofree char *tag = NULL;
88     int i;
89 
90     g_assert_cmpint(tag_len, ==, strlen(MOUNT_TAG));
91 
92     tag = g_malloc(tag_len);
93     for (i = 0; i < tag_len; i++) {
94         tag[i] = qvirtio_config_readb(v9p->vdev, i + 2);
95     }
96     g_assert_cmpmem(tag, tag_len, MOUNT_TAG, tag_len);
97 }
98 
99 #define P9_MAX_SIZE 4096 /* Max size of a T-message or R-message */
100 
101 typedef struct {
102     QTestState *qts;
103     QVirtio9P *v9p;
104     uint16_t tag;
105     uint64_t t_msg;
106     uint32_t t_size;
107     uint64_t r_msg;
108     /* No r_size, it is hardcoded to P9_MAX_SIZE */
109     size_t t_off;
110     size_t r_off;
111     uint32_t free_head;
112 } P9Req;
113 
114 static void v9fs_memwrite(P9Req *req, const void *addr, size_t len)
115 {
116     qtest_memwrite(req->qts, req->t_msg + req->t_off, addr, len);
117     req->t_off += len;
118 }
119 
120 static void v9fs_memskip(P9Req *req, size_t len)
121 {
122     req->r_off += len;
123 }
124 
125 static void v9fs_memread(P9Req *req, void *addr, size_t len)
126 {
127     qtest_memread(req->qts, req->r_msg + req->r_off, addr, len);
128     req->r_off += len;
129 }
130 
131 static void v9fs_uint8_read(P9Req *req, uint8_t *val)
132 {
133     v9fs_memread(req, val, 1);
134 }
135 
136 static void v9fs_uint16_write(P9Req *req, uint16_t val)
137 {
138     uint16_t le_val = cpu_to_le16(val);
139 
140     v9fs_memwrite(req, &le_val, 2);
141 }
142 
143 static void v9fs_uint16_read(P9Req *req, uint16_t *val)
144 {
145     v9fs_memread(req, val, 2);
146     le16_to_cpus(val);
147 }
148 
149 static void v9fs_uint32_write(P9Req *req, uint32_t val)
150 {
151     uint32_t le_val = cpu_to_le32(val);
152 
153     v9fs_memwrite(req, &le_val, 4);
154 }
155 
156 static void v9fs_uint64_write(P9Req *req, uint64_t val)
157 {
158     uint64_t le_val = cpu_to_le64(val);
159 
160     v9fs_memwrite(req, &le_val, 8);
161 }
162 
163 static void v9fs_uint32_read(P9Req *req, uint32_t *val)
164 {
165     v9fs_memread(req, val, 4);
166     le32_to_cpus(val);
167 }
168 
169 static void v9fs_uint64_read(P9Req *req, uint64_t *val)
170 {
171     v9fs_memread(req, val, 8);
172     le64_to_cpus(val);
173 }
174 
175 /* len[2] string[len] */
176 static uint16_t v9fs_string_size(const char *string)
177 {
178     size_t len = strlen(string);
179 
180     g_assert_cmpint(len, <=, UINT16_MAX - 2);
181 
182     return 2 + len;
183 }
184 
185 static void v9fs_string_write(P9Req *req, const char *string)
186 {
187     int len = strlen(string);
188 
189     g_assert_cmpint(len, <=, UINT16_MAX);
190 
191     v9fs_uint16_write(req, (uint16_t) len);
192     v9fs_memwrite(req, string, len);
193 }
194 
195 static void v9fs_string_read(P9Req *req, uint16_t *len, char **string)
196 {
197     uint16_t local_len;
198 
199     v9fs_uint16_read(req, &local_len);
200     if (len) {
201         *len = local_len;
202     }
203     if (string) {
204         *string = g_malloc(local_len + 1);
205         v9fs_memread(req, *string, local_len);
206         (*string)[local_len] = 0;
207     } else {
208         v9fs_memskip(req, local_len);
209     }
210 }
211 
212  typedef struct {
213     uint32_t size;
214     uint8_t id;
215     uint16_t tag;
216 } QEMU_PACKED P9Hdr;
217 
218 static P9Req *v9fs_req_init(QVirtio9P *v9p, uint32_t size, uint8_t id,
219                             uint16_t tag)
220 {
221     P9Req *req = g_new0(P9Req, 1);
222     uint32_t total_size = 7; /* 9P header has well-known size of 7 bytes */
223     P9Hdr hdr = {
224         .id = id,
225         .tag = cpu_to_le16(tag)
226     };
227 
228     g_assert_cmpint(total_size, <=, UINT32_MAX - size);
229     total_size += size;
230     hdr.size = cpu_to_le32(total_size);
231 
232     g_assert_cmpint(total_size, <=, P9_MAX_SIZE);
233 
234     req->qts = global_qtest;
235     req->v9p = v9p;
236     req->t_size = total_size;
237     req->t_msg = guest_alloc(alloc, req->t_size);
238     v9fs_memwrite(req, &hdr, 7);
239     req->tag = tag;
240     return req;
241 }
242 
243 static void v9fs_req_send(P9Req *req)
244 {
245     QVirtio9P *v9p = req->v9p;
246 
247     req->r_msg = guest_alloc(alloc, P9_MAX_SIZE);
248     req->free_head = qvirtqueue_add(req->qts, v9p->vq, req->t_msg, req->t_size,
249                                     false, true);
250     qvirtqueue_add(req->qts, v9p->vq, req->r_msg, P9_MAX_SIZE, true, false);
251     qvirtqueue_kick(req->qts, v9p->vdev, v9p->vq, req->free_head);
252     req->t_off = 0;
253 }
254 
255 static const char *rmessage_name(uint8_t id)
256 {
257     return
258         id == P9_RLERROR ? "RLERROR" :
259         id == P9_RVERSION ? "RVERSION" :
260         id == P9_RATTACH ? "RATTACH" :
261         id == P9_RWALK ? "RWALK" :
262         id == P9_RLOPEN ? "RLOPEN" :
263         id == P9_RWRITE ? "RWRITE" :
264         id == P9_RMKDIR ? "RMKDIR" :
265         id == P9_RLCREATE ? "RLCREATE" :
266         id == P9_RSYMLINK ? "RSYMLINK" :
267         id == P9_RLINK ? "RLINK" :
268         id == P9_RUNLINKAT ? "RUNLINKAT" :
269         id == P9_RFLUSH ? "RFLUSH" :
270         id == P9_RREADDIR ? "READDIR" :
271         "<unknown>";
272 }
273 
274 static void v9fs_req_wait_for_reply(P9Req *req, uint32_t *len)
275 {
276     QVirtio9P *v9p = req->v9p;
277 
278     qvirtio_wait_used_elem(req->qts, v9p->vdev, v9p->vq, req->free_head, len,
279                            QVIRTIO_9P_TIMEOUT_US);
280 }
281 
282 static void v9fs_req_recv(P9Req *req, uint8_t id)
283 {
284     P9Hdr hdr;
285 
286     v9fs_memread(req, &hdr, 7);
287     hdr.size = ldl_le_p(&hdr.size);
288     hdr.tag = lduw_le_p(&hdr.tag);
289 
290     g_assert_cmpint(hdr.size, >=, 7);
291     g_assert_cmpint(hdr.size, <=, P9_MAX_SIZE);
292     g_assert_cmpint(hdr.tag, ==, req->tag);
293 
294     if (hdr.id != id) {
295         g_printerr("Received response %d (%s) instead of %d (%s)\n",
296                    hdr.id, rmessage_name(hdr.id), id, rmessage_name(id));
297 
298         if (hdr.id == P9_RLERROR) {
299             uint32_t err;
300             v9fs_uint32_read(req, &err);
301             g_printerr("Rlerror has errno %d (%s)\n", err, strerror(err));
302         }
303     }
304     g_assert_cmpint(hdr.id, ==, id);
305 }
306 
307 static void v9fs_req_free(P9Req *req)
308 {
309     guest_free(alloc, req->t_msg);
310     guest_free(alloc, req->r_msg);
311     g_free(req);
312 }
313 
314 /* size[4] Rlerror tag[2] ecode[4] */
315 static void v9fs_rlerror(P9Req *req, uint32_t *err)
316 {
317     v9fs_req_recv(req, P9_RLERROR);
318     v9fs_uint32_read(req, err);
319     v9fs_req_free(req);
320 }
321 
322 /* size[4] Tversion tag[2] msize[4] version[s] */
323 static P9Req *v9fs_tversion(QVirtio9P *v9p, uint32_t msize, const char *version,
324                             uint16_t tag)
325 {
326     P9Req *req;
327     uint32_t body_size = 4;
328     uint16_t string_size = v9fs_string_size(version);
329 
330     g_assert_cmpint(body_size, <=, UINT32_MAX - string_size);
331     body_size += string_size;
332     req = v9fs_req_init(v9p, body_size, P9_TVERSION, tag);
333 
334     v9fs_uint32_write(req, msize);
335     v9fs_string_write(req, version);
336     v9fs_req_send(req);
337     return req;
338 }
339 
340 /* size[4] Rversion tag[2] msize[4] version[s] */
341 static void v9fs_rversion(P9Req *req, uint16_t *len, char **version)
342 {
343     uint32_t msize;
344 
345     v9fs_req_recv(req, P9_RVERSION);
346     v9fs_uint32_read(req, &msize);
347 
348     g_assert_cmpint(msize, ==, P9_MAX_SIZE);
349 
350     if (len || version) {
351         v9fs_string_read(req, len, version);
352     }
353 
354     v9fs_req_free(req);
355 }
356 
357 /* size[4] Tattach tag[2] fid[4] afid[4] uname[s] aname[s] n_uname[4] */
358 static P9Req *v9fs_tattach(QVirtio9P *v9p, uint32_t fid, uint32_t n_uname,
359                            uint16_t tag)
360 {
361     const char *uname = ""; /* ignored by QEMU */
362     const char *aname = ""; /* ignored by QEMU */
363     P9Req *req = v9fs_req_init(v9p, 4 + 4 + 2 + 2 + 4, P9_TATTACH, tag);
364 
365     v9fs_uint32_write(req, fid);
366     v9fs_uint32_write(req, P9_NOFID);
367     v9fs_string_write(req, uname);
368     v9fs_string_write(req, aname);
369     v9fs_uint32_write(req, n_uname);
370     v9fs_req_send(req);
371     return req;
372 }
373 
374 /* type[1] version[4] path[8] */
375 typedef char v9fs_qid[13];
376 
377 static inline bool is_same_qid(v9fs_qid a, v9fs_qid b)
378 {
379     /* don't compare QID version for checking for file ID equalness */
380     return a[0] == b[0] && memcmp(&a[5], &b[5], 8) == 0;
381 }
382 
383 /* size[4] Rattach tag[2] qid[13] */
384 static void v9fs_rattach(P9Req *req, v9fs_qid *qid)
385 {
386     v9fs_req_recv(req, P9_RATTACH);
387     if (qid) {
388         v9fs_memread(req, qid, 13);
389     }
390     v9fs_req_free(req);
391 }
392 
393 /* size[4] Twalk tag[2] fid[4] newfid[4] nwname[2] nwname*(wname[s]) */
394 static P9Req *v9fs_twalk(QVirtio9P *v9p, uint32_t fid, uint32_t newfid,
395                          uint16_t nwname, char *const wnames[], uint16_t tag)
396 {
397     P9Req *req;
398     int i;
399     uint32_t body_size = 4 + 4 + 2;
400 
401     for (i = 0; i < nwname; i++) {
402         uint16_t wname_size = v9fs_string_size(wnames[i]);
403 
404         g_assert_cmpint(body_size, <=, UINT32_MAX - wname_size);
405         body_size += wname_size;
406     }
407     req = v9fs_req_init(v9p,  body_size, P9_TWALK, tag);
408     v9fs_uint32_write(req, fid);
409     v9fs_uint32_write(req, newfid);
410     v9fs_uint16_write(req, nwname);
411     for (i = 0; i < nwname; i++) {
412         v9fs_string_write(req, wnames[i]);
413     }
414     v9fs_req_send(req);
415     return req;
416 }
417 
418 /* size[4] Rwalk tag[2] nwqid[2] nwqid*(wqid[13]) */
419 static void v9fs_rwalk(P9Req *req, uint16_t *nwqid, v9fs_qid **wqid)
420 {
421     uint16_t local_nwqid;
422 
423     v9fs_req_recv(req, P9_RWALK);
424     v9fs_uint16_read(req, &local_nwqid);
425     if (nwqid) {
426         *nwqid = local_nwqid;
427     }
428     if (wqid) {
429         *wqid = g_malloc(local_nwqid * 13);
430         v9fs_memread(req, *wqid, local_nwqid * 13);
431     }
432     v9fs_req_free(req);
433 }
434 
435 /* size[4] Tgetattr tag[2] fid[4] request_mask[8] */
436 static P9Req *v9fs_tgetattr(QVirtio9P *v9p, uint32_t fid, uint64_t request_mask,
437                             uint16_t tag)
438 {
439     P9Req *req;
440 
441     req = v9fs_req_init(v9p, 4 + 8, P9_TGETATTR, tag);
442     v9fs_uint32_write(req, fid);
443     v9fs_uint64_write(req, request_mask);
444     v9fs_req_send(req);
445     return req;
446 }
447 
448 typedef struct v9fs_attr {
449     uint64_t valid;
450     v9fs_qid qid;
451     uint32_t mode;
452     uint32_t uid;
453     uint32_t gid;
454     uint64_t nlink;
455     uint64_t rdev;
456     uint64_t size;
457     uint64_t blksize;
458     uint64_t blocks;
459     uint64_t atime_sec;
460     uint64_t atime_nsec;
461     uint64_t mtime_sec;
462     uint64_t mtime_nsec;
463     uint64_t ctime_sec;
464     uint64_t ctime_nsec;
465     uint64_t btime_sec;
466     uint64_t btime_nsec;
467     uint64_t gen;
468     uint64_t data_version;
469 } v9fs_attr;
470 
471 #define P9_GETATTR_BASIC    0x000007ffULL /* Mask for fields up to BLOCKS */
472 
473 /*
474  * size[4] Rgetattr tag[2] valid[8] qid[13] mode[4] uid[4] gid[4] nlink[8]
475  *                  rdev[8] size[8] blksize[8] blocks[8]
476  *                  atime_sec[8] atime_nsec[8] mtime_sec[8] mtime_nsec[8]
477  *                  ctime_sec[8] ctime_nsec[8] btime_sec[8] btime_nsec[8]
478  *                  gen[8] data_version[8]
479  */
480 static void v9fs_rgetattr(P9Req *req, v9fs_attr *attr)
481 {
482     v9fs_req_recv(req, P9_RGETATTR);
483 
484     v9fs_uint64_read(req, &attr->valid);
485     v9fs_memread(req, &attr->qid, 13);
486     v9fs_uint32_read(req, &attr->mode);
487     v9fs_uint32_read(req, &attr->uid);
488     v9fs_uint32_read(req, &attr->gid);
489     v9fs_uint64_read(req, &attr->nlink);
490     v9fs_uint64_read(req, &attr->rdev);
491     v9fs_uint64_read(req, &attr->size);
492     v9fs_uint64_read(req, &attr->blksize);
493     v9fs_uint64_read(req, &attr->blocks);
494     v9fs_uint64_read(req, &attr->atime_sec);
495     v9fs_uint64_read(req, &attr->atime_nsec);
496     v9fs_uint64_read(req, &attr->mtime_sec);
497     v9fs_uint64_read(req, &attr->mtime_nsec);
498     v9fs_uint64_read(req, &attr->ctime_sec);
499     v9fs_uint64_read(req, &attr->ctime_nsec);
500     v9fs_uint64_read(req, &attr->btime_sec);
501     v9fs_uint64_read(req, &attr->btime_nsec);
502     v9fs_uint64_read(req, &attr->gen);
503     v9fs_uint64_read(req, &attr->data_version);
504 
505     v9fs_req_free(req);
506 }
507 
508 /* size[4] Treaddir tag[2] fid[4] offset[8] count[4] */
509 static P9Req *v9fs_treaddir(QVirtio9P *v9p, uint32_t fid, uint64_t offset,
510                             uint32_t count, uint16_t tag)
511 {
512     P9Req *req;
513 
514     req = v9fs_req_init(v9p, 4 + 8 + 4, P9_TREADDIR, tag);
515     v9fs_uint32_write(req, fid);
516     v9fs_uint64_write(req, offset);
517     v9fs_uint32_write(req, count);
518     v9fs_req_send(req);
519     return req;
520 }
521 
522 struct V9fsDirent {
523     v9fs_qid qid;
524     uint64_t offset;
525     uint8_t type;
526     char *name;
527     struct V9fsDirent *next;
528 };
529 
530 /* size[4] Rreaddir tag[2] count[4] data[count] */
531 static void v9fs_rreaddir(P9Req *req, uint32_t *count, uint32_t *nentries,
532                           struct V9fsDirent **entries)
533 {
534     uint32_t local_count;
535     struct V9fsDirent *e = NULL;
536     uint16_t slen;
537     uint32_t n = 0;
538 
539     v9fs_req_recv(req, P9_RREADDIR);
540     v9fs_uint32_read(req, &local_count);
541 
542     if (count) {
543         *count = local_count;
544     }
545 
546     for (int32_t togo = (int32_t)local_count;
547          togo >= 13 + 8 + 1 + 2;
548          togo -= 13 + 8 + 1 + 2 + slen, ++n)
549     {
550         if (!e) {
551             e = g_new(struct V9fsDirent, 1);
552             if (entries) {
553                 *entries = e;
554             }
555         } else {
556             e = e->next = g_new(struct V9fsDirent, 1);
557         }
558         e->next = NULL;
559         /* qid[13] offset[8] type[1] name[s] */
560         v9fs_memread(req, &e->qid, 13);
561         v9fs_uint64_read(req, &e->offset);
562         v9fs_uint8_read(req, &e->type);
563         v9fs_string_read(req, &slen, &e->name);
564     }
565 
566     if (nentries) {
567         *nentries = n;
568     }
569 
570     v9fs_req_free(req);
571 }
572 
573 static void v9fs_free_dirents(struct V9fsDirent *e)
574 {
575     struct V9fsDirent *next = NULL;
576 
577     for (; e; e = next) {
578         next = e->next;
579         g_free(e->name);
580         g_free(e);
581     }
582 }
583 
584 /* size[4] Tlopen tag[2] fid[4] flags[4] */
585 static P9Req *v9fs_tlopen(QVirtio9P *v9p, uint32_t fid, uint32_t flags,
586                           uint16_t tag)
587 {
588     P9Req *req;
589 
590     req = v9fs_req_init(v9p,  4 + 4, P9_TLOPEN, tag);
591     v9fs_uint32_write(req, fid);
592     v9fs_uint32_write(req, flags);
593     v9fs_req_send(req);
594     return req;
595 }
596 
597 /* size[4] Rlopen tag[2] qid[13] iounit[4] */
598 static void v9fs_rlopen(P9Req *req, v9fs_qid *qid, uint32_t *iounit)
599 {
600     v9fs_req_recv(req, P9_RLOPEN);
601     if (qid) {
602         v9fs_memread(req, qid, 13);
603     } else {
604         v9fs_memskip(req, 13);
605     }
606     if (iounit) {
607         v9fs_uint32_read(req, iounit);
608     }
609     v9fs_req_free(req);
610 }
611 
612 /* size[4] Twrite tag[2] fid[4] offset[8] count[4] data[count] */
613 static P9Req *v9fs_twrite(QVirtio9P *v9p, uint32_t fid, uint64_t offset,
614                           uint32_t count, const void *data, uint16_t tag)
615 {
616     P9Req *req;
617     uint32_t body_size = 4 + 8 + 4;
618 
619     g_assert_cmpint(body_size, <=, UINT32_MAX - count);
620     body_size += count;
621     req = v9fs_req_init(v9p,  body_size, P9_TWRITE, tag);
622     v9fs_uint32_write(req, fid);
623     v9fs_uint64_write(req, offset);
624     v9fs_uint32_write(req, count);
625     v9fs_memwrite(req, data, count);
626     v9fs_req_send(req);
627     return req;
628 }
629 
630 /* size[4] Rwrite tag[2] count[4] */
631 static void v9fs_rwrite(P9Req *req, uint32_t *count)
632 {
633     v9fs_req_recv(req, P9_RWRITE);
634     if (count) {
635         v9fs_uint32_read(req, count);
636     }
637     v9fs_req_free(req);
638 }
639 
640 /* size[4] Tflush tag[2] oldtag[2] */
641 static P9Req *v9fs_tflush(QVirtio9P *v9p, uint16_t oldtag, uint16_t tag)
642 {
643     P9Req *req;
644 
645     req = v9fs_req_init(v9p,  2, P9_TFLUSH, tag);
646     v9fs_uint32_write(req, oldtag);
647     v9fs_req_send(req);
648     return req;
649 }
650 
651 /* size[4] Rflush tag[2] */
652 static void v9fs_rflush(P9Req *req)
653 {
654     v9fs_req_recv(req, P9_RFLUSH);
655     v9fs_req_free(req);
656 }
657 
658 static void do_version(QVirtio9P *v9p)
659 {
660     const char *version = "9P2000.L";
661     uint16_t server_len;
662     g_autofree char *server_version = NULL;
663     P9Req *req;
664 
665     req = v9fs_tversion(v9p, P9_MAX_SIZE, version, P9_NOTAG);
666     v9fs_req_wait_for_reply(req, NULL);
667     v9fs_rversion(req, &server_len, &server_version);
668 
669     g_assert_cmpmem(server_version, server_len, version, strlen(version));
670 }
671 
672 /*
673  * utility function: walk to requested dir and return fid for that dir and
674  * the QIDs of server response
675  */
676 static uint32_t do_walk_rqids(QVirtio9P *v9p, const char *path, uint16_t *nwqid,
677                               v9fs_qid **wqid)
678 {
679     char **wnames;
680     P9Req *req;
681     const uint32_t fid = genfid();
682 
683     int nwnames = split(path, "/", &wnames);
684 
685     req = v9fs_twalk(v9p, 0, fid, nwnames, wnames, 0);
686     v9fs_req_wait_for_reply(req, NULL);
687     v9fs_rwalk(req, nwqid, wqid);
688 
689     split_free(&wnames);
690     return fid;
691 }
692 
693 /* utility function: walk to requested dir and return fid for that dir */
694 static uint32_t do_walk(QVirtio9P *v9p, const char *path)
695 {
696     return do_walk_rqids(v9p, path, NULL, NULL);
697 }
698 
699 /* utility function: walk to requested dir and expect passed error response */
700 static void do_walk_expect_error(QVirtio9P *v9p, const char *path, uint32_t err)
701 {
702     char **wnames;
703     P9Req *req;
704     uint32_t _err;
705     const uint32_t fid = genfid();
706 
707     int nwnames = split(path, "/", &wnames);
708 
709     req = v9fs_twalk(v9p, 0, fid, nwnames, wnames, 0);
710     v9fs_req_wait_for_reply(req, NULL);
711     v9fs_rlerror(req, &_err);
712 
713     g_assert_cmpint(_err, ==, err);
714 
715     split_free(&wnames);
716 }
717 
718 static void fs_version(void *obj, void *data, QGuestAllocator *t_alloc)
719 {
720     alloc = t_alloc;
721     do_version(obj);
722 }
723 
724 static void do_attach_rqid(QVirtio9P *v9p, v9fs_qid *qid)
725 {
726     P9Req *req;
727 
728     do_version(v9p);
729     req = v9fs_tattach(v9p, 0, getuid(), 0);
730     v9fs_req_wait_for_reply(req, NULL);
731     v9fs_rattach(req, qid);
732 }
733 
734 static void do_attach(QVirtio9P *v9p)
735 {
736     do_attach_rqid(v9p, NULL);
737 }
738 
739 static void fs_attach(void *obj, void *data, QGuestAllocator *t_alloc)
740 {
741     alloc = t_alloc;
742     do_attach(obj);
743 }
744 
745 static void fs_walk(void *obj, void *data, QGuestAllocator *t_alloc)
746 {
747     QVirtio9P *v9p = obj;
748     alloc = t_alloc;
749     char *wnames[P9_MAXWELEM];
750     uint16_t nwqid;
751     g_autofree v9fs_qid *wqid = NULL;
752     int i;
753     P9Req *req;
754 
755     for (i = 0; i < P9_MAXWELEM; i++) {
756         wnames[i] = g_strdup_printf(QTEST_V9FS_SYNTH_WALK_FILE, i);
757     }
758 
759     do_attach(v9p);
760     req = v9fs_twalk(v9p, 0, 1, P9_MAXWELEM, wnames, 0);
761     v9fs_req_wait_for_reply(req, NULL);
762     v9fs_rwalk(req, &nwqid, &wqid);
763 
764     g_assert_cmpint(nwqid, ==, P9_MAXWELEM);
765 
766     for (i = 0; i < P9_MAXWELEM; i++) {
767         g_free(wnames[i]);
768     }
769 }
770 
771 static bool fs_dirents_contain_name(struct V9fsDirent *e, const char* name)
772 {
773     for (; e; e = e->next) {
774         if (!strcmp(e->name, name)) {
775             return true;
776         }
777     }
778     return false;
779 }
780 
781 /* size[4] Tmkdir tag[2] dfid[4] name[s] mode[4] gid[4] */
782 static P9Req *v9fs_tmkdir(QVirtio9P *v9p, uint32_t dfid, const char *name,
783                           uint32_t mode, uint32_t gid, uint16_t tag)
784 {
785     P9Req *req;
786 
787     uint32_t body_size = 4 + 4 + 4;
788     uint16_t string_size = v9fs_string_size(name);
789 
790     g_assert_cmpint(body_size, <=, UINT32_MAX - string_size);
791     body_size += string_size;
792 
793     req = v9fs_req_init(v9p, body_size, P9_TMKDIR, tag);
794     v9fs_uint32_write(req, dfid);
795     v9fs_string_write(req, name);
796     v9fs_uint32_write(req, mode);
797     v9fs_uint32_write(req, gid);
798     v9fs_req_send(req);
799     return req;
800 }
801 
802 /* size[4] Rmkdir tag[2] qid[13] */
803 static void v9fs_rmkdir(P9Req *req, v9fs_qid *qid)
804 {
805     v9fs_req_recv(req, P9_RMKDIR);
806     if (qid) {
807         v9fs_memread(req, qid, 13);
808     } else {
809         v9fs_memskip(req, 13);
810     }
811     v9fs_req_free(req);
812 }
813 
814 /* size[4] Tlcreate tag[2] fid[4] name[s] flags[4] mode[4] gid[4] */
815 static P9Req *v9fs_tlcreate(QVirtio9P *v9p, uint32_t fid, const char *name,
816                             uint32_t flags, uint32_t mode, uint32_t gid,
817                             uint16_t tag)
818 {
819     P9Req *req;
820 
821     uint32_t body_size = 4 + 4 + 4 + 4;
822     uint16_t string_size = v9fs_string_size(name);
823 
824     g_assert_cmpint(body_size, <=, UINT32_MAX - string_size);
825     body_size += string_size;
826 
827     req = v9fs_req_init(v9p, body_size, P9_TLCREATE, tag);
828     v9fs_uint32_write(req, fid);
829     v9fs_string_write(req, name);
830     v9fs_uint32_write(req, flags);
831     v9fs_uint32_write(req, mode);
832     v9fs_uint32_write(req, gid);
833     v9fs_req_send(req);
834     return req;
835 }
836 
837 /* size[4] Rlcreate tag[2] qid[13] iounit[4] */
838 static void v9fs_rlcreate(P9Req *req, v9fs_qid *qid, uint32_t *iounit)
839 {
840     v9fs_req_recv(req, P9_RLCREATE);
841     if (qid) {
842         v9fs_memread(req, qid, 13);
843     } else {
844         v9fs_memskip(req, 13);
845     }
846     if (iounit) {
847         v9fs_uint32_read(req, iounit);
848     }
849     v9fs_req_free(req);
850 }
851 
852 /* size[4] Tsymlink tag[2] fid[4] name[s] symtgt[s] gid[4] */
853 static P9Req *v9fs_tsymlink(QVirtio9P *v9p, uint32_t fid, const char *name,
854                             const char *symtgt, uint32_t gid, uint16_t tag)
855 {
856     P9Req *req;
857 
858     uint32_t body_size = 4 + 4;
859     uint16_t string_size = v9fs_string_size(name) + v9fs_string_size(symtgt);
860 
861     g_assert_cmpint(body_size, <=, UINT32_MAX - string_size);
862     body_size += string_size;
863 
864     req = v9fs_req_init(v9p, body_size, P9_TSYMLINK, tag);
865     v9fs_uint32_write(req, fid);
866     v9fs_string_write(req, name);
867     v9fs_string_write(req, symtgt);
868     v9fs_uint32_write(req, gid);
869     v9fs_req_send(req);
870     return req;
871 }
872 
873 /* size[4] Rsymlink tag[2] qid[13] */
874 static void v9fs_rsymlink(P9Req *req, v9fs_qid *qid)
875 {
876     v9fs_req_recv(req, P9_RSYMLINK);
877     if (qid) {
878         v9fs_memread(req, qid, 13);
879     } else {
880         v9fs_memskip(req, 13);
881     }
882     v9fs_req_free(req);
883 }
884 
885 /* size[4] Tlink tag[2] dfid[4] fid[4] name[s] */
886 static P9Req *v9fs_tlink(QVirtio9P *v9p, uint32_t dfid, uint32_t fid,
887                          const char *name, uint16_t tag)
888 {
889     P9Req *req;
890 
891     uint32_t body_size = 4 + 4;
892     uint16_t string_size = v9fs_string_size(name);
893 
894     g_assert_cmpint(body_size, <=, UINT32_MAX - string_size);
895     body_size += string_size;
896 
897     req = v9fs_req_init(v9p, body_size, P9_TLINK, tag);
898     v9fs_uint32_write(req, dfid);
899     v9fs_uint32_write(req, fid);
900     v9fs_string_write(req, name);
901     v9fs_req_send(req);
902     return req;
903 }
904 
905 /* size[4] Rlink tag[2] */
906 static void v9fs_rlink(P9Req *req)
907 {
908     v9fs_req_recv(req, P9_RLINK);
909     v9fs_req_free(req);
910 }
911 
912 /* size[4] Tunlinkat tag[2] dirfd[4] name[s] flags[4] */
913 static P9Req *v9fs_tunlinkat(QVirtio9P *v9p, uint32_t dirfd, const char *name,
914                              uint32_t flags, uint16_t tag)
915 {
916     P9Req *req;
917 
918     uint32_t body_size = 4 + 4;
919     uint16_t string_size = v9fs_string_size(name);
920 
921     g_assert_cmpint(body_size, <=, UINT32_MAX - string_size);
922     body_size += string_size;
923 
924     req = v9fs_req_init(v9p, body_size, P9_TUNLINKAT, tag);
925     v9fs_uint32_write(req, dirfd);
926     v9fs_string_write(req, name);
927     v9fs_uint32_write(req, flags);
928     v9fs_req_send(req);
929     return req;
930 }
931 
932 /* size[4] Runlinkat tag[2] */
933 static void v9fs_runlinkat(P9Req *req)
934 {
935     v9fs_req_recv(req, P9_RUNLINKAT);
936     v9fs_req_free(req);
937 }
938 
939 /* basic readdir test where reply fits into a single response message */
940 static void fs_readdir(void *obj, void *data, QGuestAllocator *t_alloc)
941 {
942     QVirtio9P *v9p = obj;
943     alloc = t_alloc;
944     char *const wnames[] = { g_strdup(QTEST_V9FS_SYNTH_READDIR_DIR) };
945     uint16_t nqid;
946     v9fs_qid qid;
947     uint32_t count, nentries;
948     struct V9fsDirent *entries = NULL;
949     P9Req *req;
950 
951     do_attach(v9p);
952     req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0);
953     v9fs_req_wait_for_reply(req, NULL);
954     v9fs_rwalk(req, &nqid, NULL);
955     g_assert_cmpint(nqid, ==, 1);
956 
957     req = v9fs_tlopen(v9p, 1, O_DIRECTORY, 0);
958     v9fs_req_wait_for_reply(req, NULL);
959     v9fs_rlopen(req, &qid, NULL);
960 
961     /*
962      * submit count = msize - 11, because 11 is the header size of Rreaddir
963      */
964     req = v9fs_treaddir(v9p, 1, 0, P9_MAX_SIZE - 11, 0);
965     v9fs_req_wait_for_reply(req, NULL);
966     v9fs_rreaddir(req, &count, &nentries, &entries);
967 
968     /*
969      * Assuming msize (P9_MAX_SIZE) is large enough so we can retrieve all
970      * dir entries with only one readdir request.
971      */
972     g_assert_cmpint(
973         nentries, ==,
974         QTEST_V9FS_SYNTH_READDIR_NFILES + 2 /* "." and ".." */
975     );
976 
977     /*
978      * Check all file names exist in returned entries, ignore their order
979      * though.
980      */
981     g_assert_cmpint(fs_dirents_contain_name(entries, "."), ==, true);
982     g_assert_cmpint(fs_dirents_contain_name(entries, ".."), ==, true);
983     for (int i = 0; i < QTEST_V9FS_SYNTH_READDIR_NFILES; ++i) {
984         g_autofree char *name =
985             g_strdup_printf(QTEST_V9FS_SYNTH_READDIR_FILE, i);
986         g_assert_cmpint(fs_dirents_contain_name(entries, name), ==, true);
987     }
988 
989     v9fs_free_dirents(entries);
990     g_free(wnames[0]);
991 }
992 
993 /* readdir test where overall request is split over several messages */
994 static void do_readdir_split(QVirtio9P *v9p, uint32_t count)
995 {
996     char *const wnames[] = { g_strdup(QTEST_V9FS_SYNTH_READDIR_DIR) };
997     uint16_t nqid;
998     v9fs_qid qid;
999     uint32_t nentries, npartialentries;
1000     struct V9fsDirent *entries, *tail, *partialentries;
1001     P9Req *req;
1002     int fid;
1003     uint64_t offset;
1004 
1005     do_attach(v9p);
1006 
1007     fid = 1;
1008     offset = 0;
1009     entries = NULL;
1010     nentries = 0;
1011     tail = NULL;
1012 
1013     req = v9fs_twalk(v9p, 0, fid, 1, wnames, 0);
1014     v9fs_req_wait_for_reply(req, NULL);
1015     v9fs_rwalk(req, &nqid, NULL);
1016     g_assert_cmpint(nqid, ==, 1);
1017 
1018     req = v9fs_tlopen(v9p, fid, O_DIRECTORY, 0);
1019     v9fs_req_wait_for_reply(req, NULL);
1020     v9fs_rlopen(req, &qid, NULL);
1021 
1022     /*
1023      * send as many Treaddir requests as required to get all directory
1024      * entries
1025      */
1026     while (true) {
1027         npartialentries = 0;
1028         partialentries = NULL;
1029 
1030         req = v9fs_treaddir(v9p, fid, offset, count, 0);
1031         v9fs_req_wait_for_reply(req, NULL);
1032         v9fs_rreaddir(req, &count, &npartialentries, &partialentries);
1033         if (npartialentries > 0 && partialentries) {
1034             if (!entries) {
1035                 entries = partialentries;
1036                 nentries = npartialentries;
1037                 tail = partialentries;
1038             } else {
1039                 tail->next = partialentries;
1040                 nentries += npartialentries;
1041             }
1042             while (tail->next) {
1043                 tail = tail->next;
1044             }
1045             offset = tail->offset;
1046         } else {
1047             break;
1048         }
1049     }
1050 
1051     g_assert_cmpint(
1052         nentries, ==,
1053         QTEST_V9FS_SYNTH_READDIR_NFILES + 2 /* "." and ".." */
1054     );
1055 
1056     /*
1057      * Check all file names exist in returned entries, ignore their order
1058      * though.
1059      */
1060     g_assert_cmpint(fs_dirents_contain_name(entries, "."), ==, true);
1061     g_assert_cmpint(fs_dirents_contain_name(entries, ".."), ==, true);
1062     for (int i = 0; i < QTEST_V9FS_SYNTH_READDIR_NFILES; ++i) {
1063         char *name = g_strdup_printf(QTEST_V9FS_SYNTH_READDIR_FILE, i);
1064         g_assert_cmpint(fs_dirents_contain_name(entries, name), ==, true);
1065         g_free(name);
1066     }
1067 
1068     v9fs_free_dirents(entries);
1069 
1070     g_free(wnames[0]);
1071 }
1072 
1073 static void fs_walk_no_slash(void *obj, void *data, QGuestAllocator *t_alloc)
1074 {
1075     QVirtio9P *v9p = obj;
1076     alloc = t_alloc;
1077     char *const wnames[] = { g_strdup(" /") };
1078     P9Req *req;
1079     uint32_t err;
1080 
1081     do_attach(v9p);
1082     req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0);
1083     v9fs_req_wait_for_reply(req, NULL);
1084     v9fs_rlerror(req, &err);
1085 
1086     g_assert_cmpint(err, ==, ENOENT);
1087 
1088     g_free(wnames[0]);
1089 }
1090 
1091 static void fs_walk_nonexistent(void *obj, void *data, QGuestAllocator *t_alloc)
1092 {
1093     QVirtio9P *v9p = obj;
1094     alloc = t_alloc;
1095 
1096     do_attach(v9p);
1097     /*
1098      * The 9p2000 protocol spec says: "If the first element cannot be walked
1099      * for any reason, Rerror is returned."
1100      */
1101     do_walk_expect_error(v9p, "non-existent", ENOENT);
1102 }
1103 
1104 static void fs_walk_2nd_nonexistent(void *obj, void *data,
1105                                     QGuestAllocator *t_alloc)
1106 {
1107     QVirtio9P *v9p = obj;
1108     alloc = t_alloc;
1109     v9fs_qid root_qid;
1110     uint16_t nwqid;
1111     uint32_t fid, err;
1112     P9Req *req;
1113     g_autofree v9fs_qid *wqid = NULL;
1114     g_autofree char *path = g_strdup_printf(
1115         QTEST_V9FS_SYNTH_WALK_FILE "/non-existent", 0
1116     );
1117 
1118     do_attach_rqid(v9p, &root_qid);
1119     fid = do_walk_rqids(v9p, path, &nwqid, &wqid);
1120     /*
1121      * The 9p2000 protocol spec says: "nwqid is therefore either nwname or the
1122      * index of the first elementwise walk that failed."
1123      */
1124     assert(nwqid == 1);
1125 
1126     /* returned QID wqid[0] is file ID of 1st subdir */
1127     g_assert(wqid && wqid[0] && !is_same_qid(root_qid, wqid[0]));
1128 
1129     /* expect fid being unaffected by walk above */
1130     req = v9fs_tgetattr(v9p, fid, P9_GETATTR_BASIC, 0);
1131     v9fs_req_wait_for_reply(req, NULL);
1132     v9fs_rlerror(req, &err);
1133 
1134     g_assert_cmpint(err, ==, ENOENT);
1135 }
1136 
1137 static void fs_walk_none(void *obj, void *data, QGuestAllocator *t_alloc)
1138 {
1139     QVirtio9P *v9p = obj;
1140     alloc = t_alloc;
1141     v9fs_qid root_qid;
1142     g_autofree v9fs_qid *wqid = NULL;
1143     P9Req *req;
1144     struct v9fs_attr attr;
1145 
1146     do_version(v9p);
1147     req = v9fs_tattach(v9p, 0, getuid(), 0);
1148     v9fs_req_wait_for_reply(req, NULL);
1149     v9fs_rattach(req, &root_qid);
1150 
1151     req = v9fs_twalk(v9p, 0, 1, 0, NULL, 0);
1152     v9fs_req_wait_for_reply(req, NULL);
1153     v9fs_rwalk(req, NULL, &wqid);
1154 
1155     /* special case: no QID is returned if nwname=0 was sent */
1156     g_assert(wqid == NULL);
1157 
1158     req = v9fs_tgetattr(v9p, 1, P9_GETATTR_BASIC, 0);
1159     v9fs_req_wait_for_reply(req, NULL);
1160     v9fs_rgetattr(req, &attr);
1161 
1162     g_assert(is_same_qid(root_qid, attr.qid));
1163 }
1164 
1165 static void fs_walk_dotdot(void *obj, void *data, QGuestAllocator *t_alloc)
1166 {
1167     QVirtio9P *v9p = obj;
1168     alloc = t_alloc;
1169     char *const wnames[] = { g_strdup("..") };
1170     v9fs_qid root_qid;
1171     g_autofree v9fs_qid *wqid = NULL;
1172     P9Req *req;
1173 
1174     do_version(v9p);
1175     req = v9fs_tattach(v9p, 0, getuid(), 0);
1176     v9fs_req_wait_for_reply(req, NULL);
1177     v9fs_rattach(req, &root_qid);
1178 
1179     req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0);
1180     v9fs_req_wait_for_reply(req, NULL);
1181     v9fs_rwalk(req, NULL, &wqid); /* We now we'll get one qid */
1182 
1183     g_assert_cmpmem(&root_qid, 13, wqid[0], 13);
1184 
1185     g_free(wnames[0]);
1186 }
1187 
1188 static void fs_lopen(void *obj, void *data, QGuestAllocator *t_alloc)
1189 {
1190     QVirtio9P *v9p = obj;
1191     alloc = t_alloc;
1192     char *const wnames[] = { g_strdup(QTEST_V9FS_SYNTH_LOPEN_FILE) };
1193     P9Req *req;
1194 
1195     do_attach(v9p);
1196     req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0);
1197     v9fs_req_wait_for_reply(req, NULL);
1198     v9fs_rwalk(req, NULL, NULL);
1199 
1200     req = v9fs_tlopen(v9p, 1, O_WRONLY, 0);
1201     v9fs_req_wait_for_reply(req, NULL);
1202     v9fs_rlopen(req, NULL, NULL);
1203 
1204     g_free(wnames[0]);
1205 }
1206 
1207 static void fs_write(void *obj, void *data, QGuestAllocator *t_alloc)
1208 {
1209     QVirtio9P *v9p = obj;
1210     alloc = t_alloc;
1211     static const uint32_t write_count = P9_MAX_SIZE / 2;
1212     char *const wnames[] = { g_strdup(QTEST_V9FS_SYNTH_WRITE_FILE) };
1213     g_autofree char *buf = g_malloc0(write_count);
1214     uint32_t count;
1215     P9Req *req;
1216 
1217     do_attach(v9p);
1218     req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0);
1219     v9fs_req_wait_for_reply(req, NULL);
1220     v9fs_rwalk(req, NULL, NULL);
1221 
1222     req = v9fs_tlopen(v9p, 1, O_WRONLY, 0);
1223     v9fs_req_wait_for_reply(req, NULL);
1224     v9fs_rlopen(req, NULL, NULL);
1225 
1226     req = v9fs_twrite(v9p, 1, 0, write_count, buf, 0);
1227     v9fs_req_wait_for_reply(req, NULL);
1228     v9fs_rwrite(req, &count);
1229     g_assert_cmpint(count, ==, write_count);
1230 
1231     g_free(wnames[0]);
1232 }
1233 
1234 static void fs_flush_success(void *obj, void *data, QGuestAllocator *t_alloc)
1235 {
1236     QVirtio9P *v9p = obj;
1237     alloc = t_alloc;
1238     char *const wnames[] = { g_strdup(QTEST_V9FS_SYNTH_FLUSH_FILE) };
1239     P9Req *req, *flush_req;
1240     uint32_t reply_len;
1241     uint8_t should_block;
1242 
1243     do_attach(v9p);
1244     req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0);
1245     v9fs_req_wait_for_reply(req, NULL);
1246     v9fs_rwalk(req, NULL, NULL);
1247 
1248     req = v9fs_tlopen(v9p, 1, O_WRONLY, 0);
1249     v9fs_req_wait_for_reply(req, NULL);
1250     v9fs_rlopen(req, NULL, NULL);
1251 
1252     /* This will cause the 9p server to try to write data to the backend,
1253      * until the write request gets cancelled.
1254      */
1255     should_block = 1;
1256     req = v9fs_twrite(v9p, 1, 0, sizeof(should_block), &should_block, 0);
1257 
1258     flush_req = v9fs_tflush(v9p, req->tag, 1);
1259 
1260     /* The write request is supposed to be flushed: the server should just
1261      * mark the write request as used and reply to the flush request.
1262      */
1263     v9fs_req_wait_for_reply(req, &reply_len);
1264     g_assert_cmpint(reply_len, ==, 0);
1265     v9fs_req_free(req);
1266     v9fs_rflush(flush_req);
1267 
1268     g_free(wnames[0]);
1269 }
1270 
1271 static void fs_flush_ignored(void *obj, void *data, QGuestAllocator *t_alloc)
1272 {
1273     QVirtio9P *v9p = obj;
1274     alloc = t_alloc;
1275     char *const wnames[] = { g_strdup(QTEST_V9FS_SYNTH_FLUSH_FILE) };
1276     P9Req *req, *flush_req;
1277     uint32_t count;
1278     uint8_t should_block;
1279 
1280     do_attach(v9p);
1281     req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0);
1282     v9fs_req_wait_for_reply(req, NULL);
1283     v9fs_rwalk(req, NULL, NULL);
1284 
1285     req = v9fs_tlopen(v9p, 1, O_WRONLY, 0);
1286     v9fs_req_wait_for_reply(req, NULL);
1287     v9fs_rlopen(req, NULL, NULL);
1288 
1289     /* This will cause the write request to complete right away, before it
1290      * could be actually cancelled.
1291      */
1292     should_block = 0;
1293     req = v9fs_twrite(v9p, 1, 0, sizeof(should_block), &should_block, 0);
1294 
1295     flush_req = v9fs_tflush(v9p, req->tag, 1);
1296 
1297     /* The write request is supposed to complete. The server should
1298      * reply to the write request and the flush request.
1299      */
1300     v9fs_req_wait_for_reply(req, NULL);
1301     v9fs_rwrite(req, &count);
1302     g_assert_cmpint(count, ==, sizeof(should_block));
1303     v9fs_rflush(flush_req);
1304 
1305     g_free(wnames[0]);
1306 }
1307 
1308 static void do_mkdir(QVirtio9P *v9p, const char *path, const char *cname)
1309 {
1310     g_autofree char *name = g_strdup(cname);
1311     uint32_t fid;
1312     P9Req *req;
1313 
1314     fid = do_walk(v9p, path);
1315 
1316     req = v9fs_tmkdir(v9p, fid, name, 0750, 0, 0);
1317     v9fs_req_wait_for_reply(req, NULL);
1318     v9fs_rmkdir(req, NULL);
1319 }
1320 
1321 /* create a regular file with Tlcreate and return file's fid */
1322 static uint32_t do_lcreate(QVirtio9P *v9p, const char *path,
1323                            const char *cname)
1324 {
1325     g_autofree char *name = g_strdup(cname);
1326     uint32_t fid;
1327     P9Req *req;
1328 
1329     fid = do_walk(v9p, path);
1330 
1331     req = v9fs_tlcreate(v9p, fid, name, 0, 0750, 0, 0);
1332     v9fs_req_wait_for_reply(req, NULL);
1333     v9fs_rlcreate(req, NULL, NULL);
1334 
1335     return fid;
1336 }
1337 
1338 /* create symlink named @a clink in directory @a path pointing to @a to */
1339 static void do_symlink(QVirtio9P *v9p, const char *path, const char *clink,
1340                        const char *to)
1341 {
1342     g_autofree char *name = g_strdup(clink);
1343     g_autofree char *dst = g_strdup(to);
1344     uint32_t fid;
1345     P9Req *req;
1346 
1347     fid = do_walk(v9p, path);
1348 
1349     req = v9fs_tsymlink(v9p, fid, name, dst, 0, 0);
1350     v9fs_req_wait_for_reply(req, NULL);
1351     v9fs_rsymlink(req, NULL);
1352 }
1353 
1354 /* create a hard link named @a clink in directory @a path pointing to @a to */
1355 static void do_hardlink(QVirtio9P *v9p, const char *path, const char *clink,
1356                         const char *to)
1357 {
1358     uint32_t dfid, fid;
1359     P9Req *req;
1360 
1361     dfid = do_walk(v9p, path);
1362     fid = do_walk(v9p, to);
1363 
1364     req = v9fs_tlink(v9p, dfid, fid, clink, 0);
1365     v9fs_req_wait_for_reply(req, NULL);
1366     v9fs_rlink(req);
1367 }
1368 
1369 static void do_unlinkat(QVirtio9P *v9p, const char *atpath, const char *rpath,
1370                         uint32_t flags)
1371 {
1372     g_autofree char *name = g_strdup(rpath);
1373     uint32_t fid;
1374     P9Req *req;
1375 
1376     fid = do_walk(v9p, atpath);
1377 
1378     req = v9fs_tunlinkat(v9p, fid, name, flags, 0);
1379     v9fs_req_wait_for_reply(req, NULL);
1380     v9fs_runlinkat(req);
1381 }
1382 
1383 static void fs_readdir_split_128(void *obj, void *data,
1384                                  QGuestAllocator *t_alloc)
1385 {
1386     alloc = t_alloc;
1387     do_readdir_split(obj, 128);
1388 }
1389 
1390 static void fs_readdir_split_256(void *obj, void *data,
1391                                  QGuestAllocator *t_alloc)
1392 {
1393     alloc = t_alloc;
1394     do_readdir_split(obj, 256);
1395 }
1396 
1397 static void fs_readdir_split_512(void *obj, void *data,
1398                                  QGuestAllocator *t_alloc)
1399 {
1400     alloc = t_alloc;
1401     do_readdir_split(obj, 512);
1402 }
1403 
1404 
1405 /* tests using the 9pfs 'local' fs driver */
1406 
1407 static void fs_create_dir(void *obj, void *data, QGuestAllocator *t_alloc)
1408 {
1409     QVirtio9P *v9p = obj;
1410     alloc = t_alloc;
1411     struct stat st;
1412     g_autofree char *root_path = virtio_9p_test_path("");
1413     g_autofree char *new_dir = virtio_9p_test_path("01");
1414 
1415     g_assert(root_path != NULL);
1416 
1417     do_attach(v9p);
1418     do_mkdir(v9p, "/", "01");
1419 
1420     /* check if created directory really exists now ... */
1421     g_assert(stat(new_dir, &st) == 0);
1422     /* ... and is actually a directory */
1423     g_assert((st.st_mode & S_IFMT) == S_IFDIR);
1424 }
1425 
1426 static void fs_unlinkat_dir(void *obj, void *data, QGuestAllocator *t_alloc)
1427 {
1428     QVirtio9P *v9p = obj;
1429     alloc = t_alloc;
1430     struct stat st;
1431     g_autofree char *root_path = virtio_9p_test_path("");
1432     g_autofree char *new_dir = virtio_9p_test_path("02");
1433 
1434     g_assert(root_path != NULL);
1435 
1436     do_attach(v9p);
1437     do_mkdir(v9p, "/", "02");
1438 
1439     /* check if created directory really exists now ... */
1440     g_assert(stat(new_dir, &st) == 0);
1441     /* ... and is actually a directory */
1442     g_assert((st.st_mode & S_IFMT) == S_IFDIR);
1443 
1444     do_unlinkat(v9p, "/", "02", P9_DOTL_AT_REMOVEDIR);
1445     /* directory should be gone now */
1446     g_assert(stat(new_dir, &st) != 0);
1447 }
1448 
1449 static void fs_create_file(void *obj, void *data, QGuestAllocator *t_alloc)
1450 {
1451     QVirtio9P *v9p = obj;
1452     alloc = t_alloc;
1453     struct stat st;
1454     g_autofree char *new_file = virtio_9p_test_path("03/1st_file");
1455 
1456     do_attach(v9p);
1457     do_mkdir(v9p, "/", "03");
1458     do_lcreate(v9p, "03", "1st_file");
1459 
1460     /* check if created file exists now ... */
1461     g_assert(stat(new_file, &st) == 0);
1462     /* ... and is a regular file */
1463     g_assert((st.st_mode & S_IFMT) == S_IFREG);
1464 }
1465 
1466 static void fs_unlinkat_file(void *obj, void *data, QGuestAllocator *t_alloc)
1467 {
1468     QVirtio9P *v9p = obj;
1469     alloc = t_alloc;
1470     struct stat st;
1471     g_autofree char *new_file = virtio_9p_test_path("04/doa_file");
1472 
1473     do_attach(v9p);
1474     do_mkdir(v9p, "/", "04");
1475     do_lcreate(v9p, "04", "doa_file");
1476 
1477     /* check if created file exists now ... */
1478     g_assert(stat(new_file, &st) == 0);
1479     /* ... and is a regular file */
1480     g_assert((st.st_mode & S_IFMT) == S_IFREG);
1481 
1482     do_unlinkat(v9p, "04", "doa_file", 0);
1483     /* file should be gone now */
1484     g_assert(stat(new_file, &st) != 0);
1485 }
1486 
1487 static void fs_symlink_file(void *obj, void *data, QGuestAllocator *t_alloc)
1488 {
1489     QVirtio9P *v9p = obj;
1490     alloc = t_alloc;
1491     struct stat st;
1492     g_autofree char *real_file = virtio_9p_test_path("05/real_file");
1493     g_autofree char *symlink_file = virtio_9p_test_path("05/symlink_file");
1494 
1495     do_attach(v9p);
1496     do_mkdir(v9p, "/", "05");
1497     do_lcreate(v9p, "05", "real_file");
1498     g_assert(stat(real_file, &st) == 0);
1499     g_assert((st.st_mode & S_IFMT) == S_IFREG);
1500 
1501     do_symlink(v9p, "05", "symlink_file", "real_file");
1502 
1503     /* check if created link exists now */
1504     g_assert(stat(symlink_file, &st) == 0);
1505 }
1506 
1507 static void fs_unlinkat_symlink(void *obj, void *data,
1508                                 QGuestAllocator *t_alloc)
1509 {
1510     QVirtio9P *v9p = obj;
1511     alloc = t_alloc;
1512     struct stat st;
1513     g_autofree char *real_file = virtio_9p_test_path("06/real_file");
1514     g_autofree char *symlink_file = virtio_9p_test_path("06/symlink_file");
1515 
1516     do_attach(v9p);
1517     do_mkdir(v9p, "/", "06");
1518     do_lcreate(v9p, "06", "real_file");
1519     g_assert(stat(real_file, &st) == 0);
1520     g_assert((st.st_mode & S_IFMT) == S_IFREG);
1521 
1522     do_symlink(v9p, "06", "symlink_file", "real_file");
1523     g_assert(stat(symlink_file, &st) == 0);
1524 
1525     do_unlinkat(v9p, "06", "symlink_file", 0);
1526     /* symlink should be gone now */
1527     g_assert(stat(symlink_file, &st) != 0);
1528 }
1529 
1530 static void fs_hardlink_file(void *obj, void *data, QGuestAllocator *t_alloc)
1531 {
1532     QVirtio9P *v9p = obj;
1533     alloc = t_alloc;
1534     struct stat st_real, st_link;
1535     g_autofree char *real_file = virtio_9p_test_path("07/real_file");
1536     g_autofree char *hardlink_file = virtio_9p_test_path("07/hardlink_file");
1537 
1538     do_attach(v9p);
1539     do_mkdir(v9p, "/", "07");
1540     do_lcreate(v9p, "07", "real_file");
1541     g_assert(stat(real_file, &st_real) == 0);
1542     g_assert((st_real.st_mode & S_IFMT) == S_IFREG);
1543 
1544     do_hardlink(v9p, "07", "hardlink_file", "07/real_file");
1545 
1546     /* check if link exists now ... */
1547     g_assert(stat(hardlink_file, &st_link) == 0);
1548     /* ... and it's a hard link, right? */
1549     g_assert((st_link.st_mode & S_IFMT) == S_IFREG);
1550     g_assert(st_link.st_dev == st_real.st_dev);
1551     g_assert(st_link.st_ino == st_real.st_ino);
1552 }
1553 
1554 static void fs_unlinkat_hardlink(void *obj, void *data,
1555                                  QGuestAllocator *t_alloc)
1556 {
1557     QVirtio9P *v9p = obj;
1558     alloc = t_alloc;
1559     struct stat st_real, st_link;
1560     g_autofree char *real_file = virtio_9p_test_path("08/real_file");
1561     g_autofree char *hardlink_file = virtio_9p_test_path("08/hardlink_file");
1562 
1563     do_attach(v9p);
1564     do_mkdir(v9p, "/", "08");
1565     do_lcreate(v9p, "08", "real_file");
1566     g_assert(stat(real_file, &st_real) == 0);
1567     g_assert((st_real.st_mode & S_IFMT) == S_IFREG);
1568 
1569     do_hardlink(v9p, "08", "hardlink_file", "08/real_file");
1570     g_assert(stat(hardlink_file, &st_link) == 0);
1571 
1572     do_unlinkat(v9p, "08", "hardlink_file", 0);
1573     /* symlink should be gone now */
1574     g_assert(stat(hardlink_file, &st_link) != 0);
1575     /* and old file should still exist */
1576     g_assert(stat(real_file, &st_real) == 0);
1577 }
1578 
1579 static void *assign_9p_local_driver(GString *cmd_line, void *arg)
1580 {
1581     virtio_9p_assign_local_driver(cmd_line, "security_model=mapped-xattr");
1582     return arg;
1583 }
1584 
1585 static void register_virtio_9p_test(void)
1586 {
1587 
1588     QOSGraphTestOptions opts = {
1589     };
1590 
1591     /* 9pfs test cases using the 'synth' filesystem driver */
1592     qos_add_test("synth/config", "virtio-9p", pci_config, &opts);
1593     qos_add_test("synth/version/basic", "virtio-9p", fs_version,  &opts);
1594     qos_add_test("synth/attach/basic", "virtio-9p", fs_attach,  &opts);
1595     qos_add_test("synth/walk/basic", "virtio-9p", fs_walk,  &opts);
1596     qos_add_test("synth/walk/no_slash", "virtio-9p", fs_walk_no_slash,
1597                   &opts);
1598     qos_add_test("synth/walk/none", "virtio-9p", fs_walk_none, &opts);
1599     qos_add_test("synth/walk/dotdot_from_root", "virtio-9p",
1600                  fs_walk_dotdot,  &opts);
1601     qos_add_test("synth/walk/non_existent", "virtio-9p", fs_walk_nonexistent,
1602                   &opts);
1603     qos_add_test("synth/walk/2nd_non_existent", "virtio-9p",
1604                  fs_walk_2nd_nonexistent, &opts);
1605     qos_add_test("synth/lopen/basic", "virtio-9p", fs_lopen,  &opts);
1606     qos_add_test("synth/write/basic", "virtio-9p", fs_write,  &opts);
1607     qos_add_test("synth/flush/success", "virtio-9p", fs_flush_success,
1608                   &opts);
1609     qos_add_test("synth/flush/ignored", "virtio-9p", fs_flush_ignored,
1610                   &opts);
1611     qos_add_test("synth/readdir/basic", "virtio-9p", fs_readdir,  &opts);
1612     qos_add_test("synth/readdir/split_512", "virtio-9p",
1613                  fs_readdir_split_512,  &opts);
1614     qos_add_test("synth/readdir/split_256", "virtio-9p",
1615                  fs_readdir_split_256,  &opts);
1616     qos_add_test("synth/readdir/split_128", "virtio-9p",
1617                  fs_readdir_split_128,  &opts);
1618 
1619 
1620     /* 9pfs test cases using the 'local' filesystem driver */
1621 
1622     /*
1623      * XXX: Until we are sure that these tests can run everywhere,
1624      * keep them as "slow" so that they aren't run with "make check".
1625      */
1626     if (!g_test_slow()) {
1627         return;
1628     }
1629 
1630     opts.before = assign_9p_local_driver;
1631     qos_add_test("local/config", "virtio-9p", pci_config,  &opts);
1632     qos_add_test("local/create_dir", "virtio-9p", fs_create_dir, &opts);
1633     qos_add_test("local/unlinkat_dir", "virtio-9p", fs_unlinkat_dir, &opts);
1634     qos_add_test("local/create_file", "virtio-9p", fs_create_file, &opts);
1635     qos_add_test("local/unlinkat_file", "virtio-9p", fs_unlinkat_file, &opts);
1636     qos_add_test("local/symlink_file", "virtio-9p", fs_symlink_file, &opts);
1637     qos_add_test("local/unlinkat_symlink", "virtio-9p", fs_unlinkat_symlink,
1638                  &opts);
1639     qos_add_test("local/hardlink_file", "virtio-9p", fs_hardlink_file, &opts);
1640     qos_add_test("local/unlinkat_hardlink", "virtio-9p", fs_unlinkat_hardlink,
1641                  &opts);
1642 }
1643 
1644 libqos_init(register_virtio_9p_test);
1645 
1646 static void __attribute__((constructor)) construct_9p_test(void)
1647 {
1648     /* make sure test dir for the 'local' tests exists */
1649     virtio_9p_create_local_test_dir();
1650 }
1651 
1652 static void __attribute__((destructor)) destruct_9p_test(void)
1653 {
1654     /* remove previously created test dir when test suite completed */
1655     virtio_9p_remove_local_test_dir();
1656 }
1657