]> git.proxmox.com Git - mirror_qemu.git/blame - tests/qtest/virtio-9p-test.c
tests/9pfs: walk to non-existent dir
[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
374typedef char v9fs_qid[13];
375
376/* size[4] Rattach tag[2] qid[13] */
377static void v9fs_rattach(P9Req *req, v9fs_qid *qid)
378{
379 v9fs_req_recv(req, P9_RATTACH);
380 if (qid) {
381 v9fs_memread(req, qid, 13);
382 }
383 v9fs_req_free(req);
384}
385
04b88c84 386/* size[4] Twalk tag[2] fid[4] newfid[4] nwname[2] nwname*(wname[s]) */
dfbe8b43 387static P9Req *v9fs_twalk(QVirtio9P *v9p, uint32_t fid, uint32_t newfid,
693b21d2 388 uint16_t nwname, char *const wnames[], uint16_t tag)
04b88c84
GK
389{
390 P9Req *req;
391 int i;
9ea776ee 392 uint32_t body_size = 4 + 4 + 2;
04b88c84
GK
393
394 for (i = 0; i < nwname; i++) {
9ea776ee
GK
395 uint16_t wname_size = v9fs_string_size(wnames[i]);
396
397 g_assert_cmpint(body_size, <=, UINT32_MAX - wname_size);
398 body_size += wname_size;
04b88c84 399 }
9ea776ee 400 req = v9fs_req_init(v9p, body_size, P9_TWALK, tag);
04b88c84
GK
401 v9fs_uint32_write(req, fid);
402 v9fs_uint32_write(req, newfid);
403 v9fs_uint16_write(req, nwname);
404 for (i = 0; i < nwname; i++) {
405 v9fs_string_write(req, wnames[i]);
406 }
407 v9fs_req_send(req);
408 return req;
409}
410
411/* size[4] Rwalk tag[2] nwqid[2] nwqid*(wqid[13]) */
412static void v9fs_rwalk(P9Req *req, uint16_t *nwqid, v9fs_qid **wqid)
413{
414 uint16_t local_nwqid;
415
416 v9fs_req_recv(req, P9_RWALK);
417 v9fs_uint16_read(req, &local_nwqid);
418 if (nwqid) {
419 *nwqid = local_nwqid;
420 }
421 if (wqid) {
422 *wqid = g_malloc(local_nwqid * 13);
423 v9fs_memread(req, *wqid, local_nwqid * 13);
424 }
425 v9fs_req_free(req);
426}
427
4829469f
CS
428/* size[4] Treaddir tag[2] fid[4] offset[8] count[4] */
429static P9Req *v9fs_treaddir(QVirtio9P *v9p, uint32_t fid, uint64_t offset,
430 uint32_t count, uint16_t tag)
431{
432 P9Req *req;
433
434 req = v9fs_req_init(v9p, 4 + 8 + 4, P9_TREADDIR, tag);
435 v9fs_uint32_write(req, fid);
436 v9fs_uint64_write(req, offset);
437 v9fs_uint32_write(req, count);
438 v9fs_req_send(req);
439 return req;
440}
441
442struct V9fsDirent {
443 v9fs_qid qid;
444 uint64_t offset;
445 uint8_t type;
446 char *name;
447 struct V9fsDirent *next;
448};
449
450/* size[4] Rreaddir tag[2] count[4] data[count] */
451static void v9fs_rreaddir(P9Req *req, uint32_t *count, uint32_t *nentries,
452 struct V9fsDirent **entries)
453{
454 uint32_t local_count;
455 struct V9fsDirent *e = NULL;
456 uint16_t slen;
457 uint32_t n = 0;
458
459 v9fs_req_recv(req, P9_RREADDIR);
460 v9fs_uint32_read(req, &local_count);
461
462 if (count) {
463 *count = local_count;
464 }
465
466 for (int32_t togo = (int32_t)local_count;
467 togo >= 13 + 8 + 1 + 2;
468 togo -= 13 + 8 + 1 + 2 + slen, ++n)
469 {
470 if (!e) {
1366244a 471 e = g_new(struct V9fsDirent, 1);
4829469f
CS
472 if (entries) {
473 *entries = e;
474 }
475 } else {
1366244a 476 e = e->next = g_new(struct V9fsDirent, 1);
4829469f
CS
477 }
478 e->next = NULL;
479 /* qid[13] offset[8] type[1] name[s] */
480 v9fs_memread(req, &e->qid, 13);
481 v9fs_uint64_read(req, &e->offset);
482 v9fs_uint8_read(req, &e->type);
483 v9fs_string_read(req, &slen, &e->name);
484 }
485
486 if (nentries) {
487 *nentries = n;
488 }
489
490 v9fs_req_free(req);
491}
492
493static void v9fs_free_dirents(struct V9fsDirent *e)
494{
495 struct V9fsDirent *next = NULL;
496
497 for (; e; e = next) {
498 next = e->next;
499 g_free(e->name);
500 g_free(e);
501 }
502}
503
82469aae 504/* size[4] Tlopen tag[2] fid[4] flags[4] */
dfbe8b43 505static P9Req *v9fs_tlopen(QVirtio9P *v9p, uint32_t fid, uint32_t flags,
82469aae
GK
506 uint16_t tag)
507{
508 P9Req *req;
509
510 req = v9fs_req_init(v9p, 4 + 4, P9_TLOPEN, tag);
511 v9fs_uint32_write(req, fid);
512 v9fs_uint32_write(req, flags);
513 v9fs_req_send(req);
514 return req;
515}
516
517/* size[4] Rlopen tag[2] qid[13] iounit[4] */
518static void v9fs_rlopen(P9Req *req, v9fs_qid *qid, uint32_t *iounit)
519{
520 v9fs_req_recv(req, P9_RLOPEN);
521 if (qid) {
522 v9fs_memread(req, qid, 13);
523 } else {
524 v9fs_memskip(req, 13);
525 }
526 if (iounit) {
527 v9fs_uint32_read(req, iounit);
528 }
529 v9fs_req_free(req);
530}
531
354b86f8 532/* size[4] Twrite tag[2] fid[4] offset[8] count[4] data[count] */
dfbe8b43 533static P9Req *v9fs_twrite(QVirtio9P *v9p, uint32_t fid, uint64_t offset,
354b86f8
GK
534 uint32_t count, const void *data, uint16_t tag)
535{
536 P9Req *req;
537 uint32_t body_size = 4 + 8 + 4;
538
539 g_assert_cmpint(body_size, <=, UINT32_MAX - count);
540 body_size += count;
541 req = v9fs_req_init(v9p, body_size, P9_TWRITE, tag);
542 v9fs_uint32_write(req, fid);
543 v9fs_uint64_write(req, offset);
544 v9fs_uint32_write(req, count);
545 v9fs_memwrite(req, data, count);
546 v9fs_req_send(req);
547 return req;
548}
549
550/* size[4] Rwrite tag[2] count[4] */
551static void v9fs_rwrite(P9Req *req, uint32_t *count)
552{
553 v9fs_req_recv(req, P9_RWRITE);
554 if (count) {
555 v9fs_uint32_read(req, count);
556 }
557 v9fs_req_free(req);
558}
559
357e2f7f 560/* size[4] Tflush tag[2] oldtag[2] */
dfbe8b43 561static P9Req *v9fs_tflush(QVirtio9P *v9p, uint16_t oldtag, uint16_t tag)
357e2f7f
GK
562{
563 P9Req *req;
564
565 req = v9fs_req_init(v9p, 2, P9_TFLUSH, tag);
566 v9fs_uint32_write(req, oldtag);
567 v9fs_req_send(req);
568 return req;
569}
570
571/* size[4] Rflush tag[2] */
572static void v9fs_rflush(P9Req *req)
573{
574 v9fs_req_recv(req, P9_RFLUSH);
575 v9fs_req_free(req);
576}
577
1c450e6e 578static void do_version(QVirtio9P *v9p)
6cc9906b
GK
579{
580 const char *version = "9P2000.L";
581 uint16_t server_len;
65ceee0a 582 g_autofree char *server_version = NULL;
6cc9906b
GK
583 P9Req *req;
584
693b21d2 585 req = v9fs_tversion(v9p, P9_MAX_SIZE, version, P9_NOTAG);
357e2f7f 586 v9fs_req_wait_for_reply(req, NULL);
6cc9906b
GK
587 v9fs_rversion(req, &server_len, &server_version);
588
589 g_assert_cmpmem(server_version, server_len, version, strlen(version));
6cc9906b
GK
590}
591
20018805
CS
592/* utility function: walk to requested dir and return fid for that dir */
593static uint32_t do_walk(QVirtio9P *v9p, const char *path)
594{
595 char **wnames;
596 P9Req *req;
597 const uint32_t fid = genfid();
598
599 int nwnames = split(path, "/", &wnames);
600
601 req = v9fs_twalk(v9p, 0, fid, nwnames, wnames, 0);
602 v9fs_req_wait_for_reply(req, NULL);
603 v9fs_rwalk(req, NULL, NULL);
604
605 split_free(&wnames);
606 return fid;
607}
608
9472a689
CS
609/* utility function: walk to requested dir and expect passed error response */
610static void do_walk_expect_error(QVirtio9P *v9p, const char *path, uint32_t err)
611{
612 char **wnames;
613 P9Req *req;
614 uint32_t _err;
615 const uint32_t fid = genfid();
616
617 int nwnames = split(path, "/", &wnames);
618
619 req = v9fs_twalk(v9p, 0, fid, nwnames, wnames, 0);
620 v9fs_req_wait_for_reply(req, NULL);
621 v9fs_rlerror(req, &_err);
622
623 g_assert_cmpint(_err, ==, err);
624
625 split_free(&wnames);
626}
627
1c450e6e
GK
628static void fs_version(void *obj, void *data, QGuestAllocator *t_alloc)
629{
630 alloc = t_alloc;
631 do_version(obj);
632}
633
3fe4baf4 634static void do_attach(QVirtio9P *v9p)
5c3df1f0
GK
635{
636 P9Req *req;
637
1c450e6e 638 do_version(v9p);
693b21d2 639 req = v9fs_tattach(v9p, 0, getuid(), 0);
357e2f7f 640 v9fs_req_wait_for_reply(req, NULL);
5c3df1f0
GK
641 v9fs_rattach(req, NULL);
642}
643
3fe4baf4
GK
644static void fs_attach(void *obj, void *data, QGuestAllocator *t_alloc)
645{
646 alloc = t_alloc;
647 do_attach(obj);
648}
649
dfbe8b43 650static void fs_walk(void *obj, void *data, QGuestAllocator *t_alloc)
04b88c84 651{
dfbe8b43
EGE
652 QVirtio9P *v9p = obj;
653 alloc = t_alloc;
2893ddd5 654 char *wnames[P9_MAXWELEM];
04b88c84 655 uint16_t nwqid;
65ceee0a 656 g_autofree v9fs_qid *wqid = NULL;
04b88c84
GK
657 int i;
658 P9Req *req;
659
660 for (i = 0; i < P9_MAXWELEM; i++) {
2893ddd5 661 wnames[i] = g_strdup_printf(QTEST_V9FS_SYNTH_WALK_FILE, i);
04b88c84
GK
662 }
663
3fe4baf4 664 do_attach(v9p);
693b21d2 665 req = v9fs_twalk(v9p, 0, 1, P9_MAXWELEM, wnames, 0);
357e2f7f 666 v9fs_req_wait_for_reply(req, NULL);
04b88c84
GK
667 v9fs_rwalk(req, &nwqid, &wqid);
668
669 g_assert_cmpint(nwqid, ==, P9_MAXWELEM);
670
671 for (i = 0; i < P9_MAXWELEM; i++) {
04b88c84
GK
672 g_free(wnames[i]);
673 }
04b88c84
GK
674}
675
4829469f
CS
676static bool fs_dirents_contain_name(struct V9fsDirent *e, const char* name)
677{
678 for (; e; e = e->next) {
679 if (!strcmp(e->name, name)) {
680 return true;
681 }
682 }
683 return false;
684}
685
653daf38
CS
686/* size[4] Tmkdir tag[2] dfid[4] name[s] mode[4] gid[4] */
687static P9Req *v9fs_tmkdir(QVirtio9P *v9p, uint32_t dfid, const char *name,
688 uint32_t mode, uint32_t gid, uint16_t tag)
689{
690 P9Req *req;
691
692 uint32_t body_size = 4 + 4 + 4;
693 uint16_t string_size = v9fs_string_size(name);
694
695 g_assert_cmpint(body_size, <=, UINT32_MAX - string_size);
696 body_size += string_size;
697
698 req = v9fs_req_init(v9p, body_size, P9_TMKDIR, tag);
699 v9fs_uint32_write(req, dfid);
700 v9fs_string_write(req, name);
701 v9fs_uint32_write(req, mode);
702 v9fs_uint32_write(req, gid);
703 v9fs_req_send(req);
704 return req;
705}
706
707/* size[4] Rmkdir tag[2] qid[13] */
708static void v9fs_rmkdir(P9Req *req, v9fs_qid *qid)
709{
710 v9fs_req_recv(req, P9_RMKDIR);
711 if (qid) {
712 v9fs_memread(req, qid, 13);
713 } else {
714 v9fs_memskip(req, 13);
715 }
716 v9fs_req_free(req);
717}
718
b09dbfdd
CS
719/* size[4] Tlcreate tag[2] fid[4] name[s] flags[4] mode[4] gid[4] */
720static P9Req *v9fs_tlcreate(QVirtio9P *v9p, uint32_t fid, const char *name,
721 uint32_t flags, uint32_t mode, uint32_t gid,
722 uint16_t tag)
723{
724 P9Req *req;
725
726 uint32_t body_size = 4 + 4 + 4 + 4;
727 uint16_t string_size = v9fs_string_size(name);
728
729 g_assert_cmpint(body_size, <=, UINT32_MAX - string_size);
730 body_size += string_size;
731
732 req = v9fs_req_init(v9p, body_size, P9_TLCREATE, tag);
733 v9fs_uint32_write(req, fid);
734 v9fs_string_write(req, name);
735 v9fs_uint32_write(req, flags);
736 v9fs_uint32_write(req, mode);
737 v9fs_uint32_write(req, gid);
738 v9fs_req_send(req);
739 return req;
740}
741
742/* size[4] Rlcreate tag[2] qid[13] iounit[4] */
743static void v9fs_rlcreate(P9Req *req, v9fs_qid *qid, uint32_t *iounit)
744{
745 v9fs_req_recv(req, P9_RLCREATE);
746 if (qid) {
747 v9fs_memread(req, qid, 13);
748 } else {
749 v9fs_memskip(req, 13);
750 }
751 if (iounit) {
752 v9fs_uint32_read(req, iounit);
753 }
754 v9fs_req_free(req);
755}
756
59ff563d
CS
757/* size[4] Tsymlink tag[2] fid[4] name[s] symtgt[s] gid[4] */
758static P9Req *v9fs_tsymlink(QVirtio9P *v9p, uint32_t fid, const char *name,
759 const char *symtgt, uint32_t gid, uint16_t tag)
760{
761 P9Req *req;
762
763 uint32_t body_size = 4 + 4;
764 uint16_t string_size = v9fs_string_size(name) + v9fs_string_size(symtgt);
765
766 g_assert_cmpint(body_size, <=, UINT32_MAX - string_size);
767 body_size += string_size;
768
769 req = v9fs_req_init(v9p, body_size, P9_TSYMLINK, tag);
770 v9fs_uint32_write(req, fid);
771 v9fs_string_write(req, name);
772 v9fs_string_write(req, symtgt);
773 v9fs_uint32_write(req, gid);
774 v9fs_req_send(req);
775 return req;
776}
777
778/* size[4] Rsymlink tag[2] qid[13] */
779static void v9fs_rsymlink(P9Req *req, v9fs_qid *qid)
780{
781 v9fs_req_recv(req, P9_RSYMLINK);
782 if (qid) {
783 v9fs_memread(req, qid, 13);
784 } else {
785 v9fs_memskip(req, 13);
786 }
787 v9fs_req_free(req);
788}
789
64e3d403
CS
790/* size[4] Tlink tag[2] dfid[4] fid[4] name[s] */
791static P9Req *v9fs_tlink(QVirtio9P *v9p, uint32_t dfid, uint32_t fid,
792 const char *name, uint16_t tag)
793{
794 P9Req *req;
795
796 uint32_t body_size = 4 + 4;
797 uint16_t string_size = v9fs_string_size(name);
798
799 g_assert_cmpint(body_size, <=, UINT32_MAX - string_size);
800 body_size += string_size;
801
802 req = v9fs_req_init(v9p, body_size, P9_TLINK, tag);
803 v9fs_uint32_write(req, dfid);
804 v9fs_uint32_write(req, fid);
805 v9fs_string_write(req, name);
806 v9fs_req_send(req);
807 return req;
808}
809
810/* size[4] Rlink tag[2] */
811static void v9fs_rlink(P9Req *req)
812{
813 v9fs_req_recv(req, P9_RLINK);
814 v9fs_req_free(req);
815}
816
b37d62d6
CS
817/* size[4] Tunlinkat tag[2] dirfd[4] name[s] flags[4] */
818static P9Req *v9fs_tunlinkat(QVirtio9P *v9p, uint32_t dirfd, const char *name,
819 uint32_t flags, uint16_t tag)
820{
821 P9Req *req;
822
823 uint32_t body_size = 4 + 4;
824 uint16_t string_size = v9fs_string_size(name);
825
826 g_assert_cmpint(body_size, <=, UINT32_MAX - string_size);
827 body_size += string_size;
828
829 req = v9fs_req_init(v9p, body_size, P9_TUNLINKAT, tag);
830 v9fs_uint32_write(req, dirfd);
831 v9fs_string_write(req, name);
832 v9fs_uint32_write(req, flags);
833 v9fs_req_send(req);
834 return req;
835}
836
837/* size[4] Runlinkat tag[2] */
838static void v9fs_runlinkat(P9Req *req)
839{
840 v9fs_req_recv(req, P9_RUNLINKAT);
841 v9fs_req_free(req);
842}
843
46488b62 844/* basic readdir test where reply fits into a single response message */
4829469f
CS
845static void fs_readdir(void *obj, void *data, QGuestAllocator *t_alloc)
846{
847 QVirtio9P *v9p = obj;
848 alloc = t_alloc;
849 char *const wnames[] = { g_strdup(QTEST_V9FS_SYNTH_READDIR_DIR) };
850 uint16_t nqid;
851 v9fs_qid qid;
852 uint32_t count, nentries;
853 struct V9fsDirent *entries = NULL;
854 P9Req *req;
855
3fe4baf4 856 do_attach(v9p);
4829469f
CS
857 req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0);
858 v9fs_req_wait_for_reply(req, NULL);
859 v9fs_rwalk(req, &nqid, NULL);
860 g_assert_cmpint(nqid, ==, 1);
861
862 req = v9fs_tlopen(v9p, 1, O_DIRECTORY, 0);
863 v9fs_req_wait_for_reply(req, NULL);
864 v9fs_rlopen(req, &qid, NULL);
865
866 /*
867 * submit count = msize - 11, because 11 is the header size of Rreaddir
868 */
869 req = v9fs_treaddir(v9p, 1, 0, P9_MAX_SIZE - 11, 0);
870 v9fs_req_wait_for_reply(req, NULL);
871 v9fs_rreaddir(req, &count, &nentries, &entries);
872
873 /*
874 * Assuming msize (P9_MAX_SIZE) is large enough so we can retrieve all
875 * dir entries with only one readdir request.
876 */
877 g_assert_cmpint(
878 nentries, ==,
879 QTEST_V9FS_SYNTH_READDIR_NFILES + 2 /* "." and ".." */
880 );
881
882 /*
883 * Check all file names exist in returned entries, ignore their order
884 * though.
885 */
886 g_assert_cmpint(fs_dirents_contain_name(entries, "."), ==, true);
887 g_assert_cmpint(fs_dirents_contain_name(entries, ".."), ==, true);
888 for (int i = 0; i < QTEST_V9FS_SYNTH_READDIR_NFILES; ++i) {
65ceee0a
CS
889 g_autofree char *name =
890 g_strdup_printf(QTEST_V9FS_SYNTH_READDIR_FILE, i);
4829469f 891 g_assert_cmpint(fs_dirents_contain_name(entries, name), ==, true);
4829469f
CS
892 }
893
894 v9fs_free_dirents(entries);
895 g_free(wnames[0]);
896}
897
46488b62 898/* readdir test where overall request is split over several messages */
1d98613d 899static void do_readdir_split(QVirtio9P *v9p, uint32_t count)
46488b62 900{
46488b62
CS
901 char *const wnames[] = { g_strdup(QTEST_V9FS_SYNTH_READDIR_DIR) };
902 uint16_t nqid;
903 v9fs_qid qid;
904 uint32_t nentries, npartialentries;
905 struct V9fsDirent *entries, *tail, *partialentries;
906 P9Req *req;
907 int fid;
908 uint64_t offset;
909
3fe4baf4 910 do_attach(v9p);
46488b62
CS
911
912 fid = 1;
913 offset = 0;
914 entries = NULL;
915 nentries = 0;
916 tail = NULL;
917
918 req = v9fs_twalk(v9p, 0, fid, 1, wnames, 0);
919 v9fs_req_wait_for_reply(req, NULL);
920 v9fs_rwalk(req, &nqid, NULL);
921 g_assert_cmpint(nqid, ==, 1);
922
923 req = v9fs_tlopen(v9p, fid, O_DIRECTORY, 0);
924 v9fs_req_wait_for_reply(req, NULL);
925 v9fs_rlopen(req, &qid, NULL);
926
927 /*
928 * send as many Treaddir requests as required to get all directory
929 * entries
930 */
931 while (true) {
932 npartialentries = 0;
933 partialentries = NULL;
934
935 req = v9fs_treaddir(v9p, fid, offset, count, 0);
936 v9fs_req_wait_for_reply(req, NULL);
937 v9fs_rreaddir(req, &count, &npartialentries, &partialentries);
938 if (npartialentries > 0 && partialentries) {
939 if (!entries) {
940 entries = partialentries;
941 nentries = npartialentries;
942 tail = partialentries;
943 } else {
944 tail->next = partialentries;
945 nentries += npartialentries;
946 }
947 while (tail->next) {
948 tail = tail->next;
949 }
950 offset = tail->offset;
951 } else {
952 break;
953 }
954 }
955
956 g_assert_cmpint(
957 nentries, ==,
958 QTEST_V9FS_SYNTH_READDIR_NFILES + 2 /* "." and ".." */
959 );
960
961 /*
962 * Check all file names exist in returned entries, ignore their order
963 * though.
964 */
965 g_assert_cmpint(fs_dirents_contain_name(entries, "."), ==, true);
966 g_assert_cmpint(fs_dirents_contain_name(entries, ".."), ==, true);
967 for (int i = 0; i < QTEST_V9FS_SYNTH_READDIR_NFILES; ++i) {
968 char *name = g_strdup_printf(QTEST_V9FS_SYNTH_READDIR_FILE, i);
969 g_assert_cmpint(fs_dirents_contain_name(entries, name), ==, true);
970 g_free(name);
971 }
972
973 v9fs_free_dirents(entries);
974
975 g_free(wnames[0]);
976}
977
dfbe8b43 978static void fs_walk_no_slash(void *obj, void *data, QGuestAllocator *t_alloc)
ba0d1037 979{
dfbe8b43
EGE
980 QVirtio9P *v9p = obj;
981 alloc = t_alloc;
ba0d1037
GK
982 char *const wnames[] = { g_strdup(" /") };
983 P9Req *req;
984 uint32_t err;
985
3fe4baf4 986 do_attach(v9p);
693b21d2 987 req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0);
357e2f7f 988 v9fs_req_wait_for_reply(req, NULL);
ba0d1037
GK
989 v9fs_rlerror(req, &err);
990
991 g_assert_cmpint(err, ==, ENOENT);
992
993 g_free(wnames[0]);
994}
995
9472a689
CS
996static void fs_walk_nonexistent(void *obj, void *data, QGuestAllocator *t_alloc)
997{
998 QVirtio9P *v9p = obj;
999 alloc = t_alloc;
1000
1001 do_attach(v9p);
1002 do_walk_expect_error(v9p, "non-existent", ENOENT);
1003}
1004
dfbe8b43 1005static void fs_walk_dotdot(void *obj, void *data, QGuestAllocator *t_alloc)
a37c0702 1006{
dfbe8b43
EGE
1007 QVirtio9P *v9p = obj;
1008 alloc = t_alloc;
a37c0702 1009 char *const wnames[] = { g_strdup("..") };
65ceee0a
CS
1010 v9fs_qid root_qid;
1011 g_autofree v9fs_qid *wqid = NULL;
a37c0702
GK
1012 P9Req *req;
1013
1c450e6e 1014 do_version(v9p);
693b21d2 1015 req = v9fs_tattach(v9p, 0, getuid(), 0);
357e2f7f 1016 v9fs_req_wait_for_reply(req, NULL);
a37c0702
GK
1017 v9fs_rattach(req, &root_qid);
1018
693b21d2 1019 req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0);
357e2f7f 1020 v9fs_req_wait_for_reply(req, NULL);
a37c0702
GK
1021 v9fs_rwalk(req, NULL, &wqid); /* We now we'll get one qid */
1022
1023 g_assert_cmpmem(&root_qid, 13, wqid[0], 13);
1024
a37c0702
GK
1025 g_free(wnames[0]);
1026}
1027
dfbe8b43 1028static void fs_lopen(void *obj, void *data, QGuestAllocator *t_alloc)
82469aae 1029{
dfbe8b43
EGE
1030 QVirtio9P *v9p = obj;
1031 alloc = t_alloc;
82469aae
GK
1032 char *const wnames[] = { g_strdup(QTEST_V9FS_SYNTH_LOPEN_FILE) };
1033 P9Req *req;
1034
3fe4baf4 1035 do_attach(v9p);
82469aae 1036 req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0);
357e2f7f 1037 v9fs_req_wait_for_reply(req, NULL);
82469aae
GK
1038 v9fs_rwalk(req, NULL, NULL);
1039
1040 req = v9fs_tlopen(v9p, 1, O_WRONLY, 0);
357e2f7f 1041 v9fs_req_wait_for_reply(req, NULL);
82469aae
GK
1042 v9fs_rlopen(req, NULL, NULL);
1043
1044 g_free(wnames[0]);
1045}
1046
dfbe8b43 1047static void fs_write(void *obj, void *data, QGuestAllocator *t_alloc)
354b86f8 1048{
dfbe8b43
EGE
1049 QVirtio9P *v9p = obj;
1050 alloc = t_alloc;
354b86f8
GK
1051 static const uint32_t write_count = P9_MAX_SIZE / 2;
1052 char *const wnames[] = { g_strdup(QTEST_V9FS_SYNTH_WRITE_FILE) };
65ceee0a 1053 g_autofree char *buf = g_malloc0(write_count);
354b86f8
GK
1054 uint32_t count;
1055 P9Req *req;
1056
3fe4baf4 1057 do_attach(v9p);
354b86f8 1058 req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0);
357e2f7f 1059 v9fs_req_wait_for_reply(req, NULL);
354b86f8
GK
1060 v9fs_rwalk(req, NULL, NULL);
1061
1062 req = v9fs_tlopen(v9p, 1, O_WRONLY, 0);
357e2f7f 1063 v9fs_req_wait_for_reply(req, NULL);
354b86f8
GK
1064 v9fs_rlopen(req, NULL, NULL);
1065
1066 req = v9fs_twrite(v9p, 1, 0, write_count, buf, 0);
357e2f7f 1067 v9fs_req_wait_for_reply(req, NULL);
354b86f8
GK
1068 v9fs_rwrite(req, &count);
1069 g_assert_cmpint(count, ==, write_count);
1070
354b86f8
GK
1071 g_free(wnames[0]);
1072}
1073
dfbe8b43 1074static void fs_flush_success(void *obj, void *data, QGuestAllocator *t_alloc)
357e2f7f 1075{
dfbe8b43
EGE
1076 QVirtio9P *v9p = obj;
1077 alloc = t_alloc;
357e2f7f
GK
1078 char *const wnames[] = { g_strdup(QTEST_V9FS_SYNTH_FLUSH_FILE) };
1079 P9Req *req, *flush_req;
1080 uint32_t reply_len;
1081 uint8_t should_block;
1082
3fe4baf4 1083 do_attach(v9p);
357e2f7f
GK
1084 req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0);
1085 v9fs_req_wait_for_reply(req, NULL);
1086 v9fs_rwalk(req, NULL, NULL);
1087
1088 req = v9fs_tlopen(v9p, 1, O_WRONLY, 0);
1089 v9fs_req_wait_for_reply(req, NULL);
1090 v9fs_rlopen(req, NULL, NULL);
1091
1092 /* This will cause the 9p server to try to write data to the backend,
1093 * until the write request gets cancelled.
1094 */
1095 should_block = 1;
1096 req = v9fs_twrite(v9p, 1, 0, sizeof(should_block), &should_block, 0);
1097
1098 flush_req = v9fs_tflush(v9p, req->tag, 1);
1099
1100 /* The write request is supposed to be flushed: the server should just
1101 * mark the write request as used and reply to the flush request.
1102 */
1103 v9fs_req_wait_for_reply(req, &reply_len);
1104 g_assert_cmpint(reply_len, ==, 0);
1105 v9fs_req_free(req);
1106 v9fs_rflush(flush_req);
1107
1108 g_free(wnames[0]);
1109}
1110
dfbe8b43 1111static void fs_flush_ignored(void *obj, void *data, QGuestAllocator *t_alloc)
357e2f7f 1112{
dfbe8b43
EGE
1113 QVirtio9P *v9p = obj;
1114 alloc = t_alloc;
357e2f7f
GK
1115 char *const wnames[] = { g_strdup(QTEST_V9FS_SYNTH_FLUSH_FILE) };
1116 P9Req *req, *flush_req;
1117 uint32_t count;
1118 uint8_t should_block;
1119
3fe4baf4 1120 do_attach(v9p);
357e2f7f
GK
1121 req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0);
1122 v9fs_req_wait_for_reply(req, NULL);
1123 v9fs_rwalk(req, NULL, NULL);
1124
1125 req = v9fs_tlopen(v9p, 1, O_WRONLY, 0);
1126 v9fs_req_wait_for_reply(req, NULL);
1127 v9fs_rlopen(req, NULL, NULL);
1128
1129 /* This will cause the write request to complete right away, before it
1130 * could be actually cancelled.
1131 */
1132 should_block = 0;
1133 req = v9fs_twrite(v9p, 1, 0, sizeof(should_block), &should_block, 0);
1134
1135 flush_req = v9fs_tflush(v9p, req->tag, 1);
1136
1137 /* The write request is supposed to complete. The server should
1138 * reply to the write request and the flush request.
1139 */
1140 v9fs_req_wait_for_reply(req, NULL);
1141 v9fs_rwrite(req, &count);
1142 g_assert_cmpint(count, ==, sizeof(should_block));
1143 v9fs_rflush(flush_req);
1144
1145 g_free(wnames[0]);
1146}
1147
c1934f63 1148static void do_mkdir(QVirtio9P *v9p, const char *path, const char *cname)
653daf38 1149{
65ceee0a 1150 g_autofree char *name = g_strdup(cname);
20018805 1151 uint32_t fid;
653daf38 1152 P9Req *req;
653daf38 1153
20018805 1154 fid = do_walk(v9p, path);
653daf38
CS
1155
1156 req = v9fs_tmkdir(v9p, fid, name, 0750, 0, 0);
1157 v9fs_req_wait_for_reply(req, NULL);
1158 v9fs_rmkdir(req, NULL);
653daf38
CS
1159}
1160
b09dbfdd
CS
1161/* create a regular file with Tlcreate and return file's fid */
1162static uint32_t do_lcreate(QVirtio9P *v9p, const char *path,
1163 const char *cname)
1164{
65ceee0a 1165 g_autofree char *name = g_strdup(cname);
b09dbfdd
CS
1166 uint32_t fid;
1167 P9Req *req;
1168
1169 fid = do_walk(v9p, path);
1170
1171 req = v9fs_tlcreate(v9p, fid, name, 0, 0750, 0, 0);
1172 v9fs_req_wait_for_reply(req, NULL);
1173 v9fs_rlcreate(req, NULL, NULL);
1174
b09dbfdd
CS
1175 return fid;
1176}
1177
59ff563d
CS
1178/* create symlink named @a clink in directory @a path pointing to @a to */
1179static void do_symlink(QVirtio9P *v9p, const char *path, const char *clink,
1180 const char *to)
1181{
65ceee0a
CS
1182 g_autofree char *name = g_strdup(clink);
1183 g_autofree char *dst = g_strdup(to);
59ff563d
CS
1184 uint32_t fid;
1185 P9Req *req;
1186
1187 fid = do_walk(v9p, path);
1188
1189 req = v9fs_tsymlink(v9p, fid, name, dst, 0, 0);
1190 v9fs_req_wait_for_reply(req, NULL);
1191 v9fs_rsymlink(req, NULL);
59ff563d
CS
1192}
1193
64e3d403
CS
1194/* create a hard link named @a clink in directory @a path pointing to @a to */
1195static void do_hardlink(QVirtio9P *v9p, const char *path, const char *clink,
1196 const char *to)
1197{
1198 uint32_t dfid, fid;
1199 P9Req *req;
1200
1201 dfid = do_walk(v9p, path);
1202 fid = do_walk(v9p, to);
1203
1204 req = v9fs_tlink(v9p, dfid, fid, clink, 0);
1205 v9fs_req_wait_for_reply(req, NULL);
1206 v9fs_rlink(req);
1207}
1208
b37d62d6
CS
1209static void do_unlinkat(QVirtio9P *v9p, const char *atpath, const char *rpath,
1210 uint32_t flags)
1211{
65ceee0a 1212 g_autofree char *name = g_strdup(rpath);
b37d62d6
CS
1213 uint32_t fid;
1214 P9Req *req;
1215
1216 fid = do_walk(v9p, atpath);
1217
1218 req = v9fs_tunlinkat(v9p, fid, name, flags, 0);
1219 v9fs_req_wait_for_reply(req, NULL);
1220 v9fs_runlinkat(req);
b37d62d6
CS
1221}
1222
46488b62
CS
1223static void fs_readdir_split_128(void *obj, void *data,
1224 QGuestAllocator *t_alloc)
1225{
1d98613d
GK
1226 alloc = t_alloc;
1227 do_readdir_split(obj, 128);
46488b62
CS
1228}
1229
1230static void fs_readdir_split_256(void *obj, void *data,
1231 QGuestAllocator *t_alloc)
1232{
1d98613d
GK
1233 alloc = t_alloc;
1234 do_readdir_split(obj, 256);
46488b62
CS
1235}
1236
1237static void fs_readdir_split_512(void *obj, void *data,
1238 QGuestAllocator *t_alloc)
1239{
1d98613d
GK
1240 alloc = t_alloc;
1241 do_readdir_split(obj, 512);
46488b62
CS
1242}
1243
653daf38
CS
1244
1245/* tests using the 9pfs 'local' fs driver */
1246
1247static void fs_create_dir(void *obj, void *data, QGuestAllocator *t_alloc)
1248{
1249 QVirtio9P *v9p = obj;
382619ef 1250 alloc = t_alloc;
653daf38 1251 struct stat st;
65ceee0a
CS
1252 g_autofree char *root_path = virtio_9p_test_path("");
1253 g_autofree char *new_dir = virtio_9p_test_path("01");
653daf38
CS
1254
1255 g_assert(root_path != NULL);
1256
3fe4baf4 1257 do_attach(v9p);
c1934f63 1258 do_mkdir(v9p, "/", "01");
653daf38
CS
1259
1260 /* check if created directory really exists now ... */
1261 g_assert(stat(new_dir, &st) == 0);
1262 /* ... and is actually a directory */
1263 g_assert((st.st_mode & S_IFMT) == S_IFDIR);
653daf38
CS
1264}
1265
b37d62d6
CS
1266static void fs_unlinkat_dir(void *obj, void *data, QGuestAllocator *t_alloc)
1267{
1268 QVirtio9P *v9p = obj;
1269 alloc = t_alloc;
1270 struct stat st;
65ceee0a
CS
1271 g_autofree char *root_path = virtio_9p_test_path("");
1272 g_autofree char *new_dir = virtio_9p_test_path("02");
b37d62d6
CS
1273
1274 g_assert(root_path != NULL);
1275
1276 do_attach(v9p);
1277 do_mkdir(v9p, "/", "02");
1278
1279 /* check if created directory really exists now ... */
1280 g_assert(stat(new_dir, &st) == 0);
1281 /* ... and is actually a directory */
1282 g_assert((st.st_mode & S_IFMT) == S_IFDIR);
1283
d3671fd9 1284 do_unlinkat(v9p, "/", "02", P9_DOTL_AT_REMOVEDIR);
b37d62d6
CS
1285 /* directory should be gone now */
1286 g_assert(stat(new_dir, &st) != 0);
b37d62d6
CS
1287}
1288
b09dbfdd
CS
1289static void fs_create_file(void *obj, void *data, QGuestAllocator *t_alloc)
1290{
1291 QVirtio9P *v9p = obj;
1292 alloc = t_alloc;
1293 struct stat st;
65ceee0a 1294 g_autofree char *new_file = virtio_9p_test_path("03/1st_file");
b09dbfdd
CS
1295
1296 do_attach(v9p);
1297 do_mkdir(v9p, "/", "03");
1298 do_lcreate(v9p, "03", "1st_file");
1299
1300 /* check if created file exists now ... */
1301 g_assert(stat(new_file, &st) == 0);
1302 /* ... and is a regular file */
1303 g_assert((st.st_mode & S_IFMT) == S_IFREG);
b09dbfdd
CS
1304}
1305
472c18b8
CS
1306static void fs_unlinkat_file(void *obj, void *data, QGuestAllocator *t_alloc)
1307{
1308 QVirtio9P *v9p = obj;
1309 alloc = t_alloc;
1310 struct stat st;
65ceee0a 1311 g_autofree char *new_file = virtio_9p_test_path("04/doa_file");
472c18b8
CS
1312
1313 do_attach(v9p);
1314 do_mkdir(v9p, "/", "04");
1315 do_lcreate(v9p, "04", "doa_file");
1316
1317 /* check if created file exists now ... */
1318 g_assert(stat(new_file, &st) == 0);
1319 /* ... and is a regular file */
1320 g_assert((st.st_mode & S_IFMT) == S_IFREG);
1321
1322 do_unlinkat(v9p, "04", "doa_file", 0);
1323 /* file should be gone now */
1324 g_assert(stat(new_file, &st) != 0);
472c18b8
CS
1325}
1326
59ff563d
CS
1327static void fs_symlink_file(void *obj, void *data, QGuestAllocator *t_alloc)
1328{
1329 QVirtio9P *v9p = obj;
1330 alloc = t_alloc;
1331 struct stat st;
65ceee0a
CS
1332 g_autofree char *real_file = virtio_9p_test_path("05/real_file");
1333 g_autofree char *symlink_file = virtio_9p_test_path("05/symlink_file");
59ff563d
CS
1334
1335 do_attach(v9p);
1336 do_mkdir(v9p, "/", "05");
1337 do_lcreate(v9p, "05", "real_file");
1338 g_assert(stat(real_file, &st) == 0);
1339 g_assert((st.st_mode & S_IFMT) == S_IFREG);
1340
1341 do_symlink(v9p, "05", "symlink_file", "real_file");
1342
1343 /* check if created link exists now */
1344 g_assert(stat(symlink_file, &st) == 0);
59ff563d
CS
1345}
1346
5b28ab8b
CS
1347static void fs_unlinkat_symlink(void *obj, void *data,
1348 QGuestAllocator *t_alloc)
1349{
1350 QVirtio9P *v9p = obj;
1351 alloc = t_alloc;
1352 struct stat st;
65ceee0a
CS
1353 g_autofree char *real_file = virtio_9p_test_path("06/real_file");
1354 g_autofree char *symlink_file = virtio_9p_test_path("06/symlink_file");
5b28ab8b
CS
1355
1356 do_attach(v9p);
1357 do_mkdir(v9p, "/", "06");
1358 do_lcreate(v9p, "06", "real_file");
1359 g_assert(stat(real_file, &st) == 0);
1360 g_assert((st.st_mode & S_IFMT) == S_IFREG);
1361
1362 do_symlink(v9p, "06", "symlink_file", "real_file");
1363 g_assert(stat(symlink_file, &st) == 0);
1364
1365 do_unlinkat(v9p, "06", "symlink_file", 0);
1366 /* symlink should be gone now */
1367 g_assert(stat(symlink_file, &st) != 0);
5b28ab8b
CS
1368}
1369
64e3d403
CS
1370static void fs_hardlink_file(void *obj, void *data, QGuestAllocator *t_alloc)
1371{
1372 QVirtio9P *v9p = obj;
1373 alloc = t_alloc;
1374 struct stat st_real, st_link;
65ceee0a
CS
1375 g_autofree char *real_file = virtio_9p_test_path("07/real_file");
1376 g_autofree char *hardlink_file = virtio_9p_test_path("07/hardlink_file");
64e3d403
CS
1377
1378 do_attach(v9p);
1379 do_mkdir(v9p, "/", "07");
1380 do_lcreate(v9p, "07", "real_file");
1381 g_assert(stat(real_file, &st_real) == 0);
1382 g_assert((st_real.st_mode & S_IFMT) == S_IFREG);
1383
1384 do_hardlink(v9p, "07", "hardlink_file", "07/real_file");
1385
1386 /* check if link exists now ... */
1387 g_assert(stat(hardlink_file, &st_link) == 0);
1388 /* ... and it's a hard link, right? */
1389 g_assert((st_link.st_mode & S_IFMT) == S_IFREG);
1390 g_assert(st_link.st_dev == st_real.st_dev);
1391 g_assert(st_link.st_ino == st_real.st_ino);
64e3d403
CS
1392}
1393
4d0746e2
CS
1394static void fs_unlinkat_hardlink(void *obj, void *data,
1395 QGuestAllocator *t_alloc)
1396{
1397 QVirtio9P *v9p = obj;
1398 alloc = t_alloc;
1399 struct stat st_real, st_link;
65ceee0a
CS
1400 g_autofree char *real_file = virtio_9p_test_path("08/real_file");
1401 g_autofree char *hardlink_file = virtio_9p_test_path("08/hardlink_file");
4d0746e2
CS
1402
1403 do_attach(v9p);
1404 do_mkdir(v9p, "/", "08");
1405 do_lcreate(v9p, "08", "real_file");
1406 g_assert(stat(real_file, &st_real) == 0);
1407 g_assert((st_real.st_mode & S_IFMT) == S_IFREG);
1408
1409 do_hardlink(v9p, "08", "hardlink_file", "08/real_file");
1410 g_assert(stat(hardlink_file, &st_link) == 0);
1411
1412 do_unlinkat(v9p, "08", "hardlink_file", 0);
1413 /* symlink should be gone now */
1414 g_assert(stat(hardlink_file, &st_link) != 0);
1415 /* and old file should still exist */
1416 g_assert(stat(real_file, &st_real) == 0);
4d0746e2
CS
1417}
1418
3a565c64
CS
1419static void *assign_9p_local_driver(GString *cmd_line, void *arg)
1420{
1421 virtio_9p_assign_local_driver(cmd_line, "security_model=mapped-xattr");
1422 return arg;
1423}
1424
dfbe8b43 1425static void register_virtio_9p_test(void)
1211d81b 1426{
3a565c64
CS
1427
1428 QOSGraphTestOptions opts = {
1429 };
1430
1431 /* 9pfs test cases using the 'synth' filesystem driver */
1432 qos_add_test("synth/config", "virtio-9p", pci_config, &opts);
1433 qos_add_test("synth/version/basic", "virtio-9p", fs_version, &opts);
1434 qos_add_test("synth/attach/basic", "virtio-9p", fs_attach, &opts);
1435 qos_add_test("synth/walk/basic", "virtio-9p", fs_walk, &opts);
eefd2394 1436 qos_add_test("synth/walk/no_slash", "virtio-9p", fs_walk_no_slash,
3a565c64 1437 &opts);
eefd2394 1438 qos_add_test("synth/walk/dotdot_from_root", "virtio-9p",
3a565c64 1439 fs_walk_dotdot, &opts);
9472a689
CS
1440 qos_add_test("synth/walk/non_existent", "virtio-9p", fs_walk_nonexistent,
1441 &opts);
3a565c64
CS
1442 qos_add_test("synth/lopen/basic", "virtio-9p", fs_lopen, &opts);
1443 qos_add_test("synth/write/basic", "virtio-9p", fs_write, &opts);
eefd2394 1444 qos_add_test("synth/flush/success", "virtio-9p", fs_flush_success,
3a565c64 1445 &opts);
eefd2394 1446 qos_add_test("synth/flush/ignored", "virtio-9p", fs_flush_ignored,
3a565c64
CS
1447 &opts);
1448 qos_add_test("synth/readdir/basic", "virtio-9p", fs_readdir, &opts);
eefd2394 1449 qos_add_test("synth/readdir/split_512", "virtio-9p",
3a565c64 1450 fs_readdir_split_512, &opts);
eefd2394 1451 qos_add_test("synth/readdir/split_256", "virtio-9p",
3a565c64 1452 fs_readdir_split_256, &opts);
eefd2394 1453 qos_add_test("synth/readdir/split_128", "virtio-9p",
3a565c64
CS
1454 fs_readdir_split_128, &opts);
1455
1456
1457 /* 9pfs test cases using the 'local' filesystem driver */
558f5c42
GK
1458
1459 /*
1460 * XXX: Until we are sure that these tests can run everywhere,
1461 * keep them as "slow" so that they aren't run with "make check".
1462 */
1463 if (!g_test_slow()) {
1464 return;
1465 }
1466
3a565c64
CS
1467 opts.before = assign_9p_local_driver;
1468 qos_add_test("local/config", "virtio-9p", pci_config, &opts);
653daf38 1469 qos_add_test("local/create_dir", "virtio-9p", fs_create_dir, &opts);
b37d62d6 1470 qos_add_test("local/unlinkat_dir", "virtio-9p", fs_unlinkat_dir, &opts);
b09dbfdd 1471 qos_add_test("local/create_file", "virtio-9p", fs_create_file, &opts);
472c18b8 1472 qos_add_test("local/unlinkat_file", "virtio-9p", fs_unlinkat_file, &opts);
59ff563d 1473 qos_add_test("local/symlink_file", "virtio-9p", fs_symlink_file, &opts);
5b28ab8b
CS
1474 qos_add_test("local/unlinkat_symlink", "virtio-9p", fs_unlinkat_symlink,
1475 &opts);
64e3d403 1476 qos_add_test("local/hardlink_file", "virtio-9p", fs_hardlink_file, &opts);
4d0746e2
CS
1477 qos_add_test("local/unlinkat_hardlink", "virtio-9p", fs_unlinkat_hardlink,
1478 &opts);
557a4cc0
GK
1479}
1480
dfbe8b43 1481libqos_init(register_virtio_9p_test);
136b7af2
CS
1482
1483static void __attribute__((constructor)) construct_9p_test(void)
1484{
1485 /* make sure test dir for the 'local' tests exists */
1486 virtio_9p_create_local_test_dir();
1487}
1488
1489static void __attribute__((destructor)) destruct_9p_test(void)
1490{
1491 /* remove previously created test dir when test suite completed */
1492 virtio_9p_remove_local_test_dir();
1493}