]> git.proxmox.com Git - mirror_qemu.git/blame - tests/qtest/virtio-9p-test.c
tests/qtest: enable tests for virtio-gpio
[mirror_qemu.git] / tests / qtest / virtio-9p-test.c
CommitLineData
2d888c09
AF
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
6f569084
CS
10/*
11 * Not so fast! You might want to read the 9p developer docs first:
12 * https://wiki.qemu.org/Documentation/9p
13 */
14
fbc04127 15#include "qemu/osdep.h"
dd210749 16#include "libqtest-single.h"
0b8fa32f 17#include "qemu/module.h"
6cc9906b 18#include "hw/9pfs/9p.h"
2893ddd5 19#include "hw/9pfs/9p-synth.h"
dfbe8b43
EGE
20#include "libqos/virtio-9p.h"
21#include "libqos/qgraph.h"
2d888c09 22
65b70fc7 23#define QVIRTIO_9P_TIMEOUT_US (10 * 1000 * 1000)
dfbe8b43 24static QGuestAllocator *alloc;
65b70fc7 25
653daf38
CS
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 */
30static uint32_t fid_generator = 1000;
31
32static 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 */
46static 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
72static 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
dfbe8b43 82static void pci_config(void *obj, void *data, QGuestAllocator *t_alloc)
557a4cc0 83{
dfbe8b43
EGE
84 QVirtio9P *v9p = obj;
85 alloc = t_alloc;
86 size_t tag_len = qvirtio_config_readw(v9p->vdev, 0);
65ceee0a 87 g_autofree char *tag = NULL;
557a4cc0 88 int i;
557a4cc0 89
dfbe8b43 90 g_assert_cmpint(tag_len, ==, strlen(MOUNT_TAG));
557a4cc0
GK
91
92 tag = g_malloc(tag_len);
93 for (i = 0; i < tag_len; i++) {
dfbe8b43 94 tag[i] = qvirtio_config_readb(v9p->vdev, i + 2);
557a4cc0 95 }
dfbe8b43 96 g_assert_cmpmem(tag, tag_len, MOUNT_TAG, tag_len);
1211d81b
GK
97}
98
6cc9906b
GK
99#define P9_MAX_SIZE 4096 /* Max size of a T-message or R-message */
100
101typedef struct {
1999a70a 102 QTestState *qts;
dfbe8b43 103 QVirtio9P *v9p;
6cc9906b
GK
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;
65b70fc7 111 uint32_t free_head;
6cc9906b
GK
112} P9Req;
113
114static void v9fs_memwrite(P9Req *req, const void *addr, size_t len)
115{
1999a70a 116 qtest_memwrite(req->qts, req->t_msg + req->t_off, addr, len);
6cc9906b
GK
117 req->t_off += len;
118}
119
120static void v9fs_memskip(P9Req *req, size_t len)
121{
122 req->r_off += len;
123}
124
6cc9906b
GK
125static void v9fs_memread(P9Req *req, void *addr, size_t len)
126{
1999a70a 127 qtest_memread(req->qts, req->r_msg + req->r_off, addr, len);
6cc9906b
GK
128 req->r_off += len;
129}
130
4829469f
CS
131static void v9fs_uint8_read(P9Req *req, uint8_t *val)
132{
133 v9fs_memread(req, val, 1);
134}
135
6cc9906b
GK
136static 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
143static void v9fs_uint16_read(P9Req *req, uint16_t *val)
144{
145 v9fs_memread(req, val, 2);
146 le16_to_cpus(val);
147}
148
149static 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
354b86f8
GK
156static 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
6cc9906b
GK
163static void v9fs_uint32_read(P9Req *req, uint32_t *val)
164{
165 v9fs_memread(req, val, 4);
166 le32_to_cpus(val);
167}
168
4829469f
CS
169static void v9fs_uint64_read(P9Req *req, uint64_t *val)
170{
171 v9fs_memread(req, val, 8);
172 le64_to_cpus(val);
173}
174
6cc9906b
GK
175/* len[2] string[len] */
176static uint16_t v9fs_string_size(const char *string)
177{
178 size_t len = strlen(string);
179
9ea776ee 180 g_assert_cmpint(len, <=, UINT16_MAX - 2);
6cc9906b
GK
181
182 return 2 + len;
183}
184
185static 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
195static 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) {
2e2293c2 204 *string = g_malloc(local_len + 1);
6cc9906b 205 v9fs_memread(req, *string, local_len);
2e2293c2 206 (*string)[local_len] = 0;
6cc9906b
GK
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
dfbe8b43 218static P9Req *v9fs_req_init(QVirtio9P *v9p, uint32_t size, uint8_t id,
6cc9906b
GK
219 uint16_t tag)
220{
221 P9Req *req = g_new0(P9Req, 1);
9ea776ee 222 uint32_t total_size = 7; /* 9P header has well-known size of 7 bytes */
6cc9906b 223 P9Hdr hdr = {
6cc9906b
GK
224 .id = id,
225 .tag = cpu_to_le16(tag)
226 };
227
9ea776ee
GK
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);
6cc9906b 233
1999a70a 234 req->qts = global_qtest;
6cc9906b 235 req->v9p = v9p;
9ea776ee 236 req->t_size = total_size;
dfbe8b43 237 req->t_msg = guest_alloc(alloc, req->t_size);
6cc9906b
GK
238 v9fs_memwrite(req, &hdr, 7);
239 req->tag = tag;
240 return req;
241}
242
243static void v9fs_req_send(P9Req *req)
244{
dfbe8b43 245 QVirtio9P *v9p = req->v9p;
6cc9906b 246
dfbe8b43 247 req->r_msg = guest_alloc(alloc, P9_MAX_SIZE);
1999a70a
TH
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);
6cc9906b
GK
252 req->t_off = 0;
253}
254
6e37f458
GK
255static 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" :
82469aae 262 id == P9_RLOPEN ? "RLOPEN" :
354b86f8 263 id == P9_RWRITE ? "RWRITE" :
653daf38 264 id == P9_RMKDIR ? "RMKDIR" :
b09dbfdd 265 id == P9_RLCREATE ? "RLCREATE" :
59ff563d 266 id == P9_RSYMLINK ? "RSYMLINK" :
64e3d403 267 id == P9_RLINK ? "RLINK" :
b37d62d6 268 id == P9_RUNLINKAT ? "RUNLINKAT" :
357e2f7f 269 id == P9_RFLUSH ? "RFLUSH" :
4829469f 270 id == P9_RREADDIR ? "READDIR" :
6e37f458
GK
271 "<unknown>";
272}
273
357e2f7f 274static void v9fs_req_wait_for_reply(P9Req *req, uint32_t *len)
6cc9906b 275{
dfbe8b43 276 QVirtio9P *v9p = req->v9p;
6cc9906b 277
1999a70a 278 qvirtio_wait_used_elem(req->qts, v9p->vdev, v9p->vq, req->free_head, len,
65b70fc7 279 QVIRTIO_9P_TIMEOUT_US);
60b1fa9d
GK
280}
281
282static void v9fs_req_recv(P9Req *req, uint8_t id)
283{
284 P9Hdr hdr;
6cc9906b 285
65b70fc7
GK
286 v9fs_memread(req, &hdr, 7);
287 hdr.size = ldl_le_p(&hdr.size);
288 hdr.tag = lduw_le_p(&hdr.tag);
6cc9906b
GK
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
6e37f458
GK
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 }
6cc9906b
GK
303 }
304 g_assert_cmpint(hdr.id, ==, id);
305}
306
307static void v9fs_req_free(P9Req *req)
308{
dfbe8b43
EGE
309 guest_free(alloc, req->t_msg);
310 guest_free(alloc, req->r_msg);
6cc9906b
GK
311 g_free(req);
312}
313
ba0d1037
GK
314/* size[4] Rlerror tag[2] ecode[4] */
315static 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
6cc9906b 322/* size[4] Tversion tag[2] msize[4] version[s] */
dfbe8b43 323static P9Req *v9fs_tversion(QVirtio9P *v9p, uint32_t msize, const char *version,
693b21d2 324 uint16_t tag)
6cc9906b 325{
9ea776ee
GK
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);
6cc9906b
GK
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] */
341static 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
5c3df1f0 357/* size[4] Tattach tag[2] fid[4] afid[4] uname[s] aname[s] n_uname[4] */
dfbe8b43 358static P9Req *v9fs_tattach(QVirtio9P *v9p, uint32_t fid, uint32_t n_uname,
693b21d2 359 uint16_t tag)
5c3df1f0
GK
360{
361 const char *uname = ""; /* ignored by QEMU */
362 const char *aname = ""; /* ignored by QEMU */
693b21d2 363 P9Req *req = v9fs_req_init(v9p, 4 + 4 + 2 + 2 + 4, P9_TATTACH, tag);
5c3df1f0
GK
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
a6821b82 374/* type[1] version[4] path[8] */
5c3df1f0
GK
375typedef char v9fs_qid[13];
376
a6821b82
CS
377static 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
5c3df1f0
GK
383/* size[4] Rattach tag[2] qid[13] */
384static 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
04b88c84 393/* size[4] Twalk tag[2] fid[4] newfid[4] nwname[2] nwname*(wname[s]) */
dfbe8b43 394static P9Req *v9fs_twalk(QVirtio9P *v9p, uint32_t fid, uint32_t newfid,
693b21d2 395 uint16_t nwname, char *const wnames[], uint16_t tag)
04b88c84
GK
396{
397 P9Req *req;
398 int i;
9ea776ee 399 uint32_t body_size = 4 + 4 + 2;
04b88c84
GK
400
401 for (i = 0; i < nwname; i++) {
9ea776ee
GK
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;
04b88c84 406 }
9ea776ee 407 req = v9fs_req_init(v9p, body_size, P9_TWALK, tag);
04b88c84
GK
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]) */
419static 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
a6821b82
CS
435/* size[4] Tgetattr tag[2] fid[4] request_mask[8] */
436static 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
448typedef 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 */
480static 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
4829469f
CS
508/* size[4] Treaddir tag[2] fid[4] offset[8] count[4] */
509static 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
522struct 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] */
531static 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) {
1366244a 551 e = g_new(struct V9fsDirent, 1);
4829469f
CS
552 if (entries) {
553 *entries = e;
554 }
555 } else {
1366244a 556 e = e->next = g_new(struct V9fsDirent, 1);
4829469f
CS
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
573static 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
82469aae 584/* size[4] Tlopen tag[2] fid[4] flags[4] */
dfbe8b43 585static P9Req *v9fs_tlopen(QVirtio9P *v9p, uint32_t fid, uint32_t flags,
82469aae
GK
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] */
598static 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
354b86f8 612/* size[4] Twrite tag[2] fid[4] offset[8] count[4] data[count] */
dfbe8b43 613static P9Req *v9fs_twrite(QVirtio9P *v9p, uint32_t fid, uint64_t offset,
354b86f8
GK
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] */
631static 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
357e2f7f 640/* size[4] Tflush tag[2] oldtag[2] */
dfbe8b43 641static P9Req *v9fs_tflush(QVirtio9P *v9p, uint16_t oldtag, uint16_t tag)
357e2f7f
GK
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] */
652static void v9fs_rflush(P9Req *req)
653{
654 v9fs_req_recv(req, P9_RFLUSH);
655 v9fs_req_free(req);
656}
657
1c450e6e 658static void do_version(QVirtio9P *v9p)
6cc9906b
GK
659{
660 const char *version = "9P2000.L";
661 uint16_t server_len;
65ceee0a 662 g_autofree char *server_version = NULL;
6cc9906b
GK
663 P9Req *req;
664
693b21d2 665 req = v9fs_tversion(v9p, P9_MAX_SIZE, version, P9_NOTAG);
357e2f7f 666 v9fs_req_wait_for_reply(req, NULL);
6cc9906b
GK
667 v9fs_rversion(req, &server_len, &server_version);
668
669 g_assert_cmpmem(server_version, server_len, version, strlen(version));
6cc9906b
GK
670}
671
15fbff48
CS
672/*
673 * utility function: walk to requested dir and return fid for that dir and
674 * the QIDs of server response
675 */
676static uint32_t do_walk_rqids(QVirtio9P *v9p, const char *path, uint16_t *nwqid,
677 v9fs_qid **wqid)
20018805
CS
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);
15fbff48 687 v9fs_rwalk(req, nwqid, wqid);
20018805
CS
688
689 split_free(&wnames);
690 return fid;
691}
692
15fbff48
CS
693/* utility function: walk to requested dir and return fid for that dir */
694static uint32_t do_walk(QVirtio9P *v9p, const char *path)
695{
696 return do_walk_rqids(v9p, path, NULL, NULL);
697}
698
9472a689
CS
699/* utility function: walk to requested dir and expect passed error response */
700static 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
1c450e6e
GK
718static void fs_version(void *obj, void *data, QGuestAllocator *t_alloc)
719{
720 alloc = t_alloc;
721 do_version(obj);
722}
723
0e43495d 724static void do_attach_rqid(QVirtio9P *v9p, v9fs_qid *qid)
5c3df1f0
GK
725{
726 P9Req *req;
727
1c450e6e 728 do_version(v9p);
693b21d2 729 req = v9fs_tattach(v9p, 0, getuid(), 0);
357e2f7f 730 v9fs_req_wait_for_reply(req, NULL);
0e43495d
CS
731 v9fs_rattach(req, qid);
732}
733
734static void do_attach(QVirtio9P *v9p)
735{
736 do_attach_rqid(v9p, NULL);
5c3df1f0
GK
737}
738
3fe4baf4
GK
739static void fs_attach(void *obj, void *data, QGuestAllocator *t_alloc)
740{
741 alloc = t_alloc;
742 do_attach(obj);
743}
744
dfbe8b43 745static void fs_walk(void *obj, void *data, QGuestAllocator *t_alloc)
04b88c84 746{
dfbe8b43
EGE
747 QVirtio9P *v9p = obj;
748 alloc = t_alloc;
2893ddd5 749 char *wnames[P9_MAXWELEM];
04b88c84 750 uint16_t nwqid;
65ceee0a 751 g_autofree v9fs_qid *wqid = NULL;
04b88c84
GK
752 int i;
753 P9Req *req;
754
755 for (i = 0; i < P9_MAXWELEM; i++) {
2893ddd5 756 wnames[i] = g_strdup_printf(QTEST_V9FS_SYNTH_WALK_FILE, i);
04b88c84
GK
757 }
758
3fe4baf4 759 do_attach(v9p);
693b21d2 760 req = v9fs_twalk(v9p, 0, 1, P9_MAXWELEM, wnames, 0);
357e2f7f 761 v9fs_req_wait_for_reply(req, NULL);
04b88c84
GK
762 v9fs_rwalk(req, &nwqid, &wqid);
763
764 g_assert_cmpint(nwqid, ==, P9_MAXWELEM);
765
766 for (i = 0; i < P9_MAXWELEM; i++) {
04b88c84
GK
767 g_free(wnames[i]);
768 }
04b88c84
GK
769}
770
4829469f
CS
771static 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
653daf38
CS
781/* size[4] Tmkdir tag[2] dfid[4] name[s] mode[4] gid[4] */
782static 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] */
803static 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
b09dbfdd
CS
814/* size[4] Tlcreate tag[2] fid[4] name[s] flags[4] mode[4] gid[4] */
815static 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] */
838static 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
59ff563d
CS
852/* size[4] Tsymlink tag[2] fid[4] name[s] symtgt[s] gid[4] */
853static 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] */
874static 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
64e3d403
CS
885/* size[4] Tlink tag[2] dfid[4] fid[4] name[s] */
886static 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] */
906static void v9fs_rlink(P9Req *req)
907{
908 v9fs_req_recv(req, P9_RLINK);
909 v9fs_req_free(req);
910}
911
b37d62d6
CS
912/* size[4] Tunlinkat tag[2] dirfd[4] name[s] flags[4] */
913static 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] */
933static void v9fs_runlinkat(P9Req *req)
934{
935 v9fs_req_recv(req, P9_RUNLINKAT);
936 v9fs_req_free(req);
937}
938
46488b62 939/* basic readdir test where reply fits into a single response message */
4829469f
CS
940static 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
3fe4baf4 951 do_attach(v9p);
4829469f
CS
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) {
65ceee0a
CS
984 g_autofree char *name =
985 g_strdup_printf(QTEST_V9FS_SYNTH_READDIR_FILE, i);
4829469f 986 g_assert_cmpint(fs_dirents_contain_name(entries, name), ==, true);
4829469f
CS
987 }
988
989 v9fs_free_dirents(entries);
990 g_free(wnames[0]);
991}
992
46488b62 993/* readdir test where overall request is split over several messages */
1d98613d 994static void do_readdir_split(QVirtio9P *v9p, uint32_t count)
46488b62 995{
46488b62
CS
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
3fe4baf4 1005 do_attach(v9p);
46488b62
CS
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
dfbe8b43 1073static void fs_walk_no_slash(void *obj, void *data, QGuestAllocator *t_alloc)
ba0d1037 1074{
dfbe8b43
EGE
1075 QVirtio9P *v9p = obj;
1076 alloc = t_alloc;
ba0d1037
GK
1077 char *const wnames[] = { g_strdup(" /") };
1078 P9Req *req;
1079 uint32_t err;
1080
3fe4baf4 1081 do_attach(v9p);
693b21d2 1082 req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0);
357e2f7f 1083 v9fs_req_wait_for_reply(req, NULL);
ba0d1037
GK
1084 v9fs_rlerror(req, &err);
1085
1086 g_assert_cmpint(err, ==, ENOENT);
1087
1088 g_free(wnames[0]);
1089}
1090
9472a689
CS
1091static 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);
15fbff48
CS
1097 /*
1098 * The 9p2000 protocol spec says: "If the first element cannot be walked
1099 * for any reason, Rerror is returned."
1100 */
9472a689
CS
1101 do_walk_expect_error(v9p, "non-existent", ENOENT);
1102}
1103
15fbff48
CS
1104static void fs_walk_2nd_nonexistent(void *obj, void *data,
1105 QGuestAllocator *t_alloc)
1106{
1107 QVirtio9P *v9p = obj;
1108 alloc = t_alloc;
0e43495d 1109 v9fs_qid root_qid;
15fbff48 1110 uint16_t nwqid;
0e43495d
CS
1111 uint32_t fid, err;
1112 P9Req *req;
15fbff48
CS
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
0e43495d
CS
1118 do_attach_rqid(v9p, &root_qid);
1119 fid = do_walk_rqids(v9p, path, &nwqid, &wqid);
15fbff48
CS
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);
0e43495d
CS
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);
15fbff48
CS
1135}
1136
c1668948
CS
1137static 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;
a6821b82 1144 struct v9fs_attr attr;
c1668948
CS
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);
a6821b82
CS
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));
c1668948
CS
1163}
1164
dfbe8b43 1165static void fs_walk_dotdot(void *obj, void *data, QGuestAllocator *t_alloc)
a37c0702 1166{
dfbe8b43
EGE
1167 QVirtio9P *v9p = obj;
1168 alloc = t_alloc;
a37c0702 1169 char *const wnames[] = { g_strdup("..") };
65ceee0a
CS
1170 v9fs_qid root_qid;
1171 g_autofree v9fs_qid *wqid = NULL;
a37c0702
GK
1172 P9Req *req;
1173
1c450e6e 1174 do_version(v9p);
693b21d2 1175 req = v9fs_tattach(v9p, 0, getuid(), 0);
357e2f7f 1176 v9fs_req_wait_for_reply(req, NULL);
a37c0702
GK
1177 v9fs_rattach(req, &root_qid);
1178
693b21d2 1179 req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0);
357e2f7f 1180 v9fs_req_wait_for_reply(req, NULL);
a37c0702
GK
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
a37c0702
GK
1185 g_free(wnames[0]);
1186}
1187
dfbe8b43 1188static void fs_lopen(void *obj, void *data, QGuestAllocator *t_alloc)
82469aae 1189{
dfbe8b43
EGE
1190 QVirtio9P *v9p = obj;
1191 alloc = t_alloc;
82469aae
GK
1192 char *const wnames[] = { g_strdup(QTEST_V9FS_SYNTH_LOPEN_FILE) };
1193 P9Req *req;
1194
3fe4baf4 1195 do_attach(v9p);
82469aae 1196 req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0);
357e2f7f 1197 v9fs_req_wait_for_reply(req, NULL);
82469aae
GK
1198 v9fs_rwalk(req, NULL, NULL);
1199
1200 req = v9fs_tlopen(v9p, 1, O_WRONLY, 0);
357e2f7f 1201 v9fs_req_wait_for_reply(req, NULL);
82469aae
GK
1202 v9fs_rlopen(req, NULL, NULL);
1203
1204 g_free(wnames[0]);
1205}
1206
dfbe8b43 1207static void fs_write(void *obj, void *data, QGuestAllocator *t_alloc)
354b86f8 1208{
dfbe8b43
EGE
1209 QVirtio9P *v9p = obj;
1210 alloc = t_alloc;
354b86f8
GK
1211 static const uint32_t write_count = P9_MAX_SIZE / 2;
1212 char *const wnames[] = { g_strdup(QTEST_V9FS_SYNTH_WRITE_FILE) };
65ceee0a 1213 g_autofree char *buf = g_malloc0(write_count);
354b86f8
GK
1214 uint32_t count;
1215 P9Req *req;
1216
3fe4baf4 1217 do_attach(v9p);
354b86f8 1218 req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0);
357e2f7f 1219 v9fs_req_wait_for_reply(req, NULL);
354b86f8
GK
1220 v9fs_rwalk(req, NULL, NULL);
1221
1222 req = v9fs_tlopen(v9p, 1, O_WRONLY, 0);
357e2f7f 1223 v9fs_req_wait_for_reply(req, NULL);
354b86f8
GK
1224 v9fs_rlopen(req, NULL, NULL);
1225
1226 req = v9fs_twrite(v9p, 1, 0, write_count, buf, 0);
357e2f7f 1227 v9fs_req_wait_for_reply(req, NULL);
354b86f8
GK
1228 v9fs_rwrite(req, &count);
1229 g_assert_cmpint(count, ==, write_count);
1230
354b86f8
GK
1231 g_free(wnames[0]);
1232}
1233
dfbe8b43 1234static void fs_flush_success(void *obj, void *data, QGuestAllocator *t_alloc)
357e2f7f 1235{
dfbe8b43
EGE
1236 QVirtio9P *v9p = obj;
1237 alloc = t_alloc;
357e2f7f
GK
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
3fe4baf4 1243 do_attach(v9p);
357e2f7f
GK
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
dfbe8b43 1271static void fs_flush_ignored(void *obj, void *data, QGuestAllocator *t_alloc)
357e2f7f 1272{
dfbe8b43
EGE
1273 QVirtio9P *v9p = obj;
1274 alloc = t_alloc;
357e2f7f
GK
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
3fe4baf4 1280 do_attach(v9p);
357e2f7f
GK
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
c1934f63 1308static void do_mkdir(QVirtio9P *v9p, const char *path, const char *cname)
653daf38 1309{
65ceee0a 1310 g_autofree char *name = g_strdup(cname);
20018805 1311 uint32_t fid;
653daf38 1312 P9Req *req;
653daf38 1313
20018805 1314 fid = do_walk(v9p, path);
653daf38
CS
1315
1316 req = v9fs_tmkdir(v9p, fid, name, 0750, 0, 0);
1317 v9fs_req_wait_for_reply(req, NULL);
1318 v9fs_rmkdir(req, NULL);
653daf38
CS
1319}
1320
b09dbfdd
CS
1321/* create a regular file with Tlcreate and return file's fid */
1322static uint32_t do_lcreate(QVirtio9P *v9p, const char *path,
1323 const char *cname)
1324{
65ceee0a 1325 g_autofree char *name = g_strdup(cname);
b09dbfdd
CS
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
b09dbfdd
CS
1335 return fid;
1336}
1337
59ff563d
CS
1338/* create symlink named @a clink in directory @a path pointing to @a to */
1339static void do_symlink(QVirtio9P *v9p, const char *path, const char *clink,
1340 const char *to)
1341{
65ceee0a
CS
1342 g_autofree char *name = g_strdup(clink);
1343 g_autofree char *dst = g_strdup(to);
59ff563d
CS
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);
59ff563d
CS
1352}
1353
64e3d403
CS
1354/* create a hard link named @a clink in directory @a path pointing to @a to */
1355static 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
b37d62d6
CS
1369static void do_unlinkat(QVirtio9P *v9p, const char *atpath, const char *rpath,
1370 uint32_t flags)
1371{
65ceee0a 1372 g_autofree char *name = g_strdup(rpath);
b37d62d6
CS
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);
b37d62d6
CS
1381}
1382
46488b62
CS
1383static void fs_readdir_split_128(void *obj, void *data,
1384 QGuestAllocator *t_alloc)
1385{
1d98613d
GK
1386 alloc = t_alloc;
1387 do_readdir_split(obj, 128);
46488b62
CS
1388}
1389
1390static void fs_readdir_split_256(void *obj, void *data,
1391 QGuestAllocator *t_alloc)
1392{
1d98613d
GK
1393 alloc = t_alloc;
1394 do_readdir_split(obj, 256);
46488b62
CS
1395}
1396
1397static void fs_readdir_split_512(void *obj, void *data,
1398 QGuestAllocator *t_alloc)
1399{
1d98613d
GK
1400 alloc = t_alloc;
1401 do_readdir_split(obj, 512);
46488b62
CS
1402}
1403
653daf38
CS
1404
1405/* tests using the 9pfs 'local' fs driver */
1406
1407static void fs_create_dir(void *obj, void *data, QGuestAllocator *t_alloc)
1408{
1409 QVirtio9P *v9p = obj;
382619ef 1410 alloc = t_alloc;
653daf38 1411 struct stat st;
65ceee0a
CS
1412 g_autofree char *root_path = virtio_9p_test_path("");
1413 g_autofree char *new_dir = virtio_9p_test_path("01");
653daf38
CS
1414
1415 g_assert(root_path != NULL);
1416
3fe4baf4 1417 do_attach(v9p);
c1934f63 1418 do_mkdir(v9p, "/", "01");
653daf38
CS
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);
653daf38
CS
1424}
1425
b37d62d6
CS
1426static void fs_unlinkat_dir(void *obj, void *data, QGuestAllocator *t_alloc)
1427{
1428 QVirtio9P *v9p = obj;
1429 alloc = t_alloc;
1430 struct stat st;
65ceee0a
CS
1431 g_autofree char *root_path = virtio_9p_test_path("");
1432 g_autofree char *new_dir = virtio_9p_test_path("02");
b37d62d6
CS
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
d3671fd9 1444 do_unlinkat(v9p, "/", "02", P9_DOTL_AT_REMOVEDIR);
b37d62d6
CS
1445 /* directory should be gone now */
1446 g_assert(stat(new_dir, &st) != 0);
b37d62d6
CS
1447}
1448
b09dbfdd
CS
1449static void fs_create_file(void *obj, void *data, QGuestAllocator *t_alloc)
1450{
1451 QVirtio9P *v9p = obj;
1452 alloc = t_alloc;
1453 struct stat st;
65ceee0a 1454 g_autofree char *new_file = virtio_9p_test_path("03/1st_file");
b09dbfdd
CS
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);
b09dbfdd
CS
1464}
1465
472c18b8
CS
1466static void fs_unlinkat_file(void *obj, void *data, QGuestAllocator *t_alloc)
1467{
1468 QVirtio9P *v9p = obj;
1469 alloc = t_alloc;
1470 struct stat st;
65ceee0a 1471 g_autofree char *new_file = virtio_9p_test_path("04/doa_file");
472c18b8
CS
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);
472c18b8
CS
1485}
1486
59ff563d
CS
1487static void fs_symlink_file(void *obj, void *data, QGuestAllocator *t_alloc)
1488{
1489 QVirtio9P *v9p = obj;
1490 alloc = t_alloc;
1491 struct stat st;
65ceee0a
CS
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");
59ff563d
CS
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);
59ff563d
CS
1505}
1506
5b28ab8b
CS
1507static 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;
65ceee0a
CS
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");
5b28ab8b
CS
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);
5b28ab8b
CS
1528}
1529
64e3d403
CS
1530static 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;
65ceee0a
CS
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");
64e3d403
CS
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);
64e3d403
CS
1552}
1553
4d0746e2
CS
1554static 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;
65ceee0a
CS
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");
4d0746e2
CS
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);
4d0746e2
CS
1577}
1578
3a565c64
CS
1579static 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
dfbe8b43 1585static void register_virtio_9p_test(void)
1211d81b 1586{
3a565c64
CS
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);
eefd2394 1596 qos_add_test("synth/walk/no_slash", "virtio-9p", fs_walk_no_slash,
3a565c64 1597 &opts);
c1668948 1598 qos_add_test("synth/walk/none", "virtio-9p", fs_walk_none, &opts);
eefd2394 1599 qos_add_test("synth/walk/dotdot_from_root", "virtio-9p",
3a565c64 1600 fs_walk_dotdot, &opts);
9472a689
CS
1601 qos_add_test("synth/walk/non_existent", "virtio-9p", fs_walk_nonexistent,
1602 &opts);
15fbff48
CS
1603 qos_add_test("synth/walk/2nd_non_existent", "virtio-9p",
1604 fs_walk_2nd_nonexistent, &opts);
3a565c64
CS
1605 qos_add_test("synth/lopen/basic", "virtio-9p", fs_lopen, &opts);
1606 qos_add_test("synth/write/basic", "virtio-9p", fs_write, &opts);
eefd2394 1607 qos_add_test("synth/flush/success", "virtio-9p", fs_flush_success,
3a565c64 1608 &opts);
eefd2394 1609 qos_add_test("synth/flush/ignored", "virtio-9p", fs_flush_ignored,
3a565c64
CS
1610 &opts);
1611 qos_add_test("synth/readdir/basic", "virtio-9p", fs_readdir, &opts);
eefd2394 1612 qos_add_test("synth/readdir/split_512", "virtio-9p",
3a565c64 1613 fs_readdir_split_512, &opts);
eefd2394 1614 qos_add_test("synth/readdir/split_256", "virtio-9p",
3a565c64 1615 fs_readdir_split_256, &opts);
eefd2394 1616 qos_add_test("synth/readdir/split_128", "virtio-9p",
3a565c64
CS
1617 fs_readdir_split_128, &opts);
1618
1619
1620 /* 9pfs test cases using the 'local' filesystem driver */
558f5c42
GK
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
3a565c64
CS
1630 opts.before = assign_9p_local_driver;
1631 qos_add_test("local/config", "virtio-9p", pci_config, &opts);
653daf38 1632 qos_add_test("local/create_dir", "virtio-9p", fs_create_dir, &opts);
b37d62d6 1633 qos_add_test("local/unlinkat_dir", "virtio-9p", fs_unlinkat_dir, &opts);
b09dbfdd 1634 qos_add_test("local/create_file", "virtio-9p", fs_create_file, &opts);
472c18b8 1635 qos_add_test("local/unlinkat_file", "virtio-9p", fs_unlinkat_file, &opts);
59ff563d 1636 qos_add_test("local/symlink_file", "virtio-9p", fs_symlink_file, &opts);
5b28ab8b
CS
1637 qos_add_test("local/unlinkat_symlink", "virtio-9p", fs_unlinkat_symlink,
1638 &opts);
64e3d403 1639 qos_add_test("local/hardlink_file", "virtio-9p", fs_hardlink_file, &opts);
4d0746e2
CS
1640 qos_add_test("local/unlinkat_hardlink", "virtio-9p", fs_unlinkat_hardlink,
1641 &opts);
557a4cc0
GK
1642}
1643
dfbe8b43 1644libqos_init(register_virtio_9p_test);
136b7af2
CS
1645
1646static 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
1652static 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}