2 * QTest testcase for VirtIO 9P
4 * Copyright (c) 2014 SUSE LINUX Products GmbH
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.
10 #include "qemu/osdep.h"
11 #include "libqtest-single.h"
12 #include "qemu/module.h"
13 #include "hw/9pfs/9p.h"
14 #include "hw/9pfs/9p-synth.h"
15 #include "libqos/virtio-9p.h"
16 #include "libqos/qgraph.h"
18 #define QVIRTIO_9P_TIMEOUT_US (10 * 1000 * 1000)
19 static QGuestAllocator
*alloc
;
22 * Used to auto generate new fids. Start with arbitrary high value to avoid
23 * collision with hard coded fids in basic test code.
25 static uint32_t fid_generator
= 1000;
27 static uint32_t genfid(void)
29 return fid_generator
++;
33 * Splits the @a in string by @a delim into individual (non empty) strings
34 * and outputs them to @a out. The output array @a out is NULL terminated.
36 * Output array @a out must be freed by calling split_free().
38 * @returns number of individual elements in output array @a out (without the
39 * final NULL terminating element)
41 static int split(const char *in
, const char *delim
, char ***out
)
47 for (p
= strtok(tmp
, delim
); p
!= NULL
; p
= strtok(NULL
, delim
)) {
54 *out
= g_new0(char *, n
+ 1); /* last element NULL delimiter */
57 for (p
= strtok(tmp
, delim
); p
!= NULL
; p
= strtok(NULL
, delim
)) {
59 (*out
)[i
++] = g_strdup(p
);
67 static void split_free(char ***out
)
70 for (i
= 0; (*out
)[i
]; ++i
) {
77 static void pci_config(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
81 size_t tag_len
= qvirtio_config_readw(v9p
->vdev
, 0);
85 g_assert_cmpint(tag_len
, ==, strlen(MOUNT_TAG
));
87 tag
= g_malloc(tag_len
);
88 for (i
= 0; i
< tag_len
; i
++) {
89 tag
[i
] = qvirtio_config_readb(v9p
->vdev
, i
+ 2);
91 g_assert_cmpmem(tag
, tag_len
, MOUNT_TAG
, tag_len
);
95 #define P9_MAX_SIZE 4096 /* Max size of a T-message or R-message */
104 /* No r_size, it is hardcoded to P9_MAX_SIZE */
110 static void v9fs_memwrite(P9Req
*req
, const void *addr
, size_t len
)
112 qtest_memwrite(req
->qts
, req
->t_msg
+ req
->t_off
, addr
, len
);
116 static void v9fs_memskip(P9Req
*req
, size_t len
)
121 static void v9fs_memread(P9Req
*req
, void *addr
, size_t len
)
123 qtest_memread(req
->qts
, req
->r_msg
+ req
->r_off
, addr
, len
);
127 static void v9fs_uint8_read(P9Req
*req
, uint8_t *val
)
129 v9fs_memread(req
, val
, 1);
132 static void v9fs_uint16_write(P9Req
*req
, uint16_t val
)
134 uint16_t le_val
= cpu_to_le16(val
);
136 v9fs_memwrite(req
, &le_val
, 2);
139 static void v9fs_uint16_read(P9Req
*req
, uint16_t *val
)
141 v9fs_memread(req
, val
, 2);
145 static void v9fs_uint32_write(P9Req
*req
, uint32_t val
)
147 uint32_t le_val
= cpu_to_le32(val
);
149 v9fs_memwrite(req
, &le_val
, 4);
152 static void v9fs_uint64_write(P9Req
*req
, uint64_t val
)
154 uint64_t le_val
= cpu_to_le64(val
);
156 v9fs_memwrite(req
, &le_val
, 8);
159 static void v9fs_uint32_read(P9Req
*req
, uint32_t *val
)
161 v9fs_memread(req
, val
, 4);
165 static void v9fs_uint64_read(P9Req
*req
, uint64_t *val
)
167 v9fs_memread(req
, val
, 8);
171 /* len[2] string[len] */
172 static uint16_t v9fs_string_size(const char *string
)
174 size_t len
= strlen(string
);
176 g_assert_cmpint(len
, <=, UINT16_MAX
- 2);
181 static void v9fs_string_write(P9Req
*req
, const char *string
)
183 int len
= strlen(string
);
185 g_assert_cmpint(len
, <=, UINT16_MAX
);
187 v9fs_uint16_write(req
, (uint16_t) len
);
188 v9fs_memwrite(req
, string
, len
);
191 static void v9fs_string_read(P9Req
*req
, uint16_t *len
, char **string
)
195 v9fs_uint16_read(req
, &local_len
);
200 *string
= g_malloc(local_len
+ 1);
201 v9fs_memread(req
, *string
, local_len
);
202 (*string
)[local_len
] = 0;
204 v9fs_memskip(req
, local_len
);
214 static P9Req
*v9fs_req_init(QVirtio9P
*v9p
, uint32_t size
, uint8_t id
,
217 P9Req
*req
= g_new0(P9Req
, 1);
218 uint32_t total_size
= 7; /* 9P header has well-known size of 7 bytes */
221 .tag
= cpu_to_le16(tag
)
224 g_assert_cmpint(total_size
, <=, UINT32_MAX
- size
);
226 hdr
.size
= cpu_to_le32(total_size
);
228 g_assert_cmpint(total_size
, <=, P9_MAX_SIZE
);
230 req
->qts
= global_qtest
;
232 req
->t_size
= total_size
;
233 req
->t_msg
= guest_alloc(alloc
, req
->t_size
);
234 v9fs_memwrite(req
, &hdr
, 7);
239 static void v9fs_req_send(P9Req
*req
)
241 QVirtio9P
*v9p
= req
->v9p
;
243 req
->r_msg
= guest_alloc(alloc
, P9_MAX_SIZE
);
244 req
->free_head
= qvirtqueue_add(req
->qts
, v9p
->vq
, req
->t_msg
, req
->t_size
,
246 qvirtqueue_add(req
->qts
, v9p
->vq
, req
->r_msg
, P9_MAX_SIZE
, true, false);
247 qvirtqueue_kick(req
->qts
, v9p
->vdev
, v9p
->vq
, req
->free_head
);
251 static const char *rmessage_name(uint8_t id
)
254 id
== P9_RLERROR
? "RLERROR" :
255 id
== P9_RVERSION
? "RVERSION" :
256 id
== P9_RATTACH
? "RATTACH" :
257 id
== P9_RWALK
? "RWALK" :
258 id
== P9_RLOPEN
? "RLOPEN" :
259 id
== P9_RWRITE
? "RWRITE" :
260 id
== P9_RMKDIR
? "RMKDIR" :
261 id
== P9_RLCREATE
? "RLCREATE" :
262 id
== P9_RSYMLINK
? "RSYMLINK" :
263 id
== P9_RUNLINKAT
? "RUNLINKAT" :
264 id
== P9_RFLUSH
? "RFLUSH" :
265 id
== P9_RREADDIR
? "READDIR" :
269 static void v9fs_req_wait_for_reply(P9Req
*req
, uint32_t *len
)
271 QVirtio9P
*v9p
= req
->v9p
;
273 qvirtio_wait_used_elem(req
->qts
, v9p
->vdev
, v9p
->vq
, req
->free_head
, len
,
274 QVIRTIO_9P_TIMEOUT_US
);
277 static void v9fs_req_recv(P9Req
*req
, uint8_t id
)
281 v9fs_memread(req
, &hdr
, 7);
282 hdr
.size
= ldl_le_p(&hdr
.size
);
283 hdr
.tag
= lduw_le_p(&hdr
.tag
);
285 g_assert_cmpint(hdr
.size
, >=, 7);
286 g_assert_cmpint(hdr
.size
, <=, P9_MAX_SIZE
);
287 g_assert_cmpint(hdr
.tag
, ==, req
->tag
);
290 g_printerr("Received response %d (%s) instead of %d (%s)\n",
291 hdr
.id
, rmessage_name(hdr
.id
), id
, rmessage_name(id
));
293 if (hdr
.id
== P9_RLERROR
) {
295 v9fs_uint32_read(req
, &err
);
296 g_printerr("Rlerror has errno %d (%s)\n", err
, strerror(err
));
299 g_assert_cmpint(hdr
.id
, ==, id
);
302 static void v9fs_req_free(P9Req
*req
)
304 guest_free(alloc
, req
->t_msg
);
305 guest_free(alloc
, req
->r_msg
);
309 /* size[4] Rlerror tag[2] ecode[4] */
310 static void v9fs_rlerror(P9Req
*req
, uint32_t *err
)
312 v9fs_req_recv(req
, P9_RLERROR
);
313 v9fs_uint32_read(req
, err
);
317 /* size[4] Tversion tag[2] msize[4] version[s] */
318 static P9Req
*v9fs_tversion(QVirtio9P
*v9p
, uint32_t msize
, const char *version
,
322 uint32_t body_size
= 4;
323 uint16_t string_size
= v9fs_string_size(version
);
325 g_assert_cmpint(body_size
, <=, UINT32_MAX
- string_size
);
326 body_size
+= string_size
;
327 req
= v9fs_req_init(v9p
, body_size
, P9_TVERSION
, tag
);
329 v9fs_uint32_write(req
, msize
);
330 v9fs_string_write(req
, version
);
335 /* size[4] Rversion tag[2] msize[4] version[s] */
336 static void v9fs_rversion(P9Req
*req
, uint16_t *len
, char **version
)
340 v9fs_req_recv(req
, P9_RVERSION
);
341 v9fs_uint32_read(req
, &msize
);
343 g_assert_cmpint(msize
, ==, P9_MAX_SIZE
);
345 if (len
|| version
) {
346 v9fs_string_read(req
, len
, version
);
352 /* size[4] Tattach tag[2] fid[4] afid[4] uname[s] aname[s] n_uname[4] */
353 static P9Req
*v9fs_tattach(QVirtio9P
*v9p
, uint32_t fid
, uint32_t n_uname
,
356 const char *uname
= ""; /* ignored by QEMU */
357 const char *aname
= ""; /* ignored by QEMU */
358 P9Req
*req
= v9fs_req_init(v9p
, 4 + 4 + 2 + 2 + 4, P9_TATTACH
, tag
);
360 v9fs_uint32_write(req
, fid
);
361 v9fs_uint32_write(req
, P9_NOFID
);
362 v9fs_string_write(req
, uname
);
363 v9fs_string_write(req
, aname
);
364 v9fs_uint32_write(req
, n_uname
);
369 typedef char v9fs_qid
[13];
371 /* size[4] Rattach tag[2] qid[13] */
372 static void v9fs_rattach(P9Req
*req
, v9fs_qid
*qid
)
374 v9fs_req_recv(req
, P9_RATTACH
);
376 v9fs_memread(req
, qid
, 13);
381 /* size[4] Twalk tag[2] fid[4] newfid[4] nwname[2] nwname*(wname[s]) */
382 static P9Req
*v9fs_twalk(QVirtio9P
*v9p
, uint32_t fid
, uint32_t newfid
,
383 uint16_t nwname
, char *const wnames
[], uint16_t tag
)
387 uint32_t body_size
= 4 + 4 + 2;
389 for (i
= 0; i
< nwname
; i
++) {
390 uint16_t wname_size
= v9fs_string_size(wnames
[i
]);
392 g_assert_cmpint(body_size
, <=, UINT32_MAX
- wname_size
);
393 body_size
+= wname_size
;
395 req
= v9fs_req_init(v9p
, body_size
, P9_TWALK
, tag
);
396 v9fs_uint32_write(req
, fid
);
397 v9fs_uint32_write(req
, newfid
);
398 v9fs_uint16_write(req
, nwname
);
399 for (i
= 0; i
< nwname
; i
++) {
400 v9fs_string_write(req
, wnames
[i
]);
406 /* size[4] Rwalk tag[2] nwqid[2] nwqid*(wqid[13]) */
407 static void v9fs_rwalk(P9Req
*req
, uint16_t *nwqid
, v9fs_qid
**wqid
)
409 uint16_t local_nwqid
;
411 v9fs_req_recv(req
, P9_RWALK
);
412 v9fs_uint16_read(req
, &local_nwqid
);
414 *nwqid
= local_nwqid
;
417 *wqid
= g_malloc(local_nwqid
* 13);
418 v9fs_memread(req
, *wqid
, local_nwqid
* 13);
423 /* size[4] Treaddir tag[2] fid[4] offset[8] count[4] */
424 static P9Req
*v9fs_treaddir(QVirtio9P
*v9p
, uint32_t fid
, uint64_t offset
,
425 uint32_t count
, uint16_t tag
)
429 req
= v9fs_req_init(v9p
, 4 + 8 + 4, P9_TREADDIR
, tag
);
430 v9fs_uint32_write(req
, fid
);
431 v9fs_uint64_write(req
, offset
);
432 v9fs_uint32_write(req
, count
);
442 struct V9fsDirent
*next
;
445 /* size[4] Rreaddir tag[2] count[4] data[count] */
446 static void v9fs_rreaddir(P9Req
*req
, uint32_t *count
, uint32_t *nentries
,
447 struct V9fsDirent
**entries
)
449 uint32_t local_count
;
450 struct V9fsDirent
*e
= NULL
;
454 v9fs_req_recv(req
, P9_RREADDIR
);
455 v9fs_uint32_read(req
, &local_count
);
458 *count
= local_count
;
461 for (int32_t togo
= (int32_t)local_count
;
462 togo
>= 13 + 8 + 1 + 2;
463 togo
-= 13 + 8 + 1 + 2 + slen
, ++n
)
466 e
= g_malloc(sizeof(struct V9fsDirent
));
471 e
= e
->next
= g_malloc(sizeof(struct V9fsDirent
));
474 /* qid[13] offset[8] type[1] name[s] */
475 v9fs_memread(req
, &e
->qid
, 13);
476 v9fs_uint64_read(req
, &e
->offset
);
477 v9fs_uint8_read(req
, &e
->type
);
478 v9fs_string_read(req
, &slen
, &e
->name
);
488 static void v9fs_free_dirents(struct V9fsDirent
*e
)
490 struct V9fsDirent
*next
= NULL
;
492 for (; e
; e
= next
) {
499 /* size[4] Tlopen tag[2] fid[4] flags[4] */
500 static P9Req
*v9fs_tlopen(QVirtio9P
*v9p
, uint32_t fid
, uint32_t flags
,
505 req
= v9fs_req_init(v9p
, 4 + 4, P9_TLOPEN
, tag
);
506 v9fs_uint32_write(req
, fid
);
507 v9fs_uint32_write(req
, flags
);
512 /* size[4] Rlopen tag[2] qid[13] iounit[4] */
513 static void v9fs_rlopen(P9Req
*req
, v9fs_qid
*qid
, uint32_t *iounit
)
515 v9fs_req_recv(req
, P9_RLOPEN
);
517 v9fs_memread(req
, qid
, 13);
519 v9fs_memskip(req
, 13);
522 v9fs_uint32_read(req
, iounit
);
527 /* size[4] Twrite tag[2] fid[4] offset[8] count[4] data[count] */
528 static P9Req
*v9fs_twrite(QVirtio9P
*v9p
, uint32_t fid
, uint64_t offset
,
529 uint32_t count
, const void *data
, uint16_t tag
)
532 uint32_t body_size
= 4 + 8 + 4;
534 g_assert_cmpint(body_size
, <=, UINT32_MAX
- count
);
536 req
= v9fs_req_init(v9p
, body_size
, P9_TWRITE
, tag
);
537 v9fs_uint32_write(req
, fid
);
538 v9fs_uint64_write(req
, offset
);
539 v9fs_uint32_write(req
, count
);
540 v9fs_memwrite(req
, data
, count
);
545 /* size[4] Rwrite tag[2] count[4] */
546 static void v9fs_rwrite(P9Req
*req
, uint32_t *count
)
548 v9fs_req_recv(req
, P9_RWRITE
);
550 v9fs_uint32_read(req
, count
);
555 /* size[4] Tflush tag[2] oldtag[2] */
556 static P9Req
*v9fs_tflush(QVirtio9P
*v9p
, uint16_t oldtag
, uint16_t tag
)
560 req
= v9fs_req_init(v9p
, 2, P9_TFLUSH
, tag
);
561 v9fs_uint32_write(req
, oldtag
);
566 /* size[4] Rflush tag[2] */
567 static void v9fs_rflush(P9Req
*req
)
569 v9fs_req_recv(req
, P9_RFLUSH
);
573 static void do_version(QVirtio9P
*v9p
)
575 const char *version
= "9P2000.L";
577 char *server_version
;
580 req
= v9fs_tversion(v9p
, P9_MAX_SIZE
, version
, P9_NOTAG
);
581 v9fs_req_wait_for_reply(req
, NULL
);
582 v9fs_rversion(req
, &server_len
, &server_version
);
584 g_assert_cmpmem(server_version
, server_len
, version
, strlen(version
));
586 g_free(server_version
);
589 /* utility function: walk to requested dir and return fid for that dir */
590 static uint32_t do_walk(QVirtio9P
*v9p
, const char *path
)
594 const uint32_t fid
= genfid();
596 int nwnames
= split(path
, "/", &wnames
);
598 req
= v9fs_twalk(v9p
, 0, fid
, nwnames
, wnames
, 0);
599 v9fs_req_wait_for_reply(req
, NULL
);
600 v9fs_rwalk(req
, NULL
, NULL
);
606 static void fs_version(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
612 static void do_attach(QVirtio9P
*v9p
)
617 req
= v9fs_tattach(v9p
, 0, getuid(), 0);
618 v9fs_req_wait_for_reply(req
, NULL
);
619 v9fs_rattach(req
, NULL
);
622 static void fs_attach(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
628 static void fs_walk(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
630 QVirtio9P
*v9p
= obj
;
632 char *wnames
[P9_MAXWELEM
];
638 for (i
= 0; i
< P9_MAXWELEM
; i
++) {
639 wnames
[i
] = g_strdup_printf(QTEST_V9FS_SYNTH_WALK_FILE
, i
);
643 req
= v9fs_twalk(v9p
, 0, 1, P9_MAXWELEM
, wnames
, 0);
644 v9fs_req_wait_for_reply(req
, NULL
);
645 v9fs_rwalk(req
, &nwqid
, &wqid
);
647 g_assert_cmpint(nwqid
, ==, P9_MAXWELEM
);
649 for (i
= 0; i
< P9_MAXWELEM
; i
++) {
656 static bool fs_dirents_contain_name(struct V9fsDirent
*e
, const char* name
)
658 for (; e
; e
= e
->next
) {
659 if (!strcmp(e
->name
, name
)) {
666 /* size[4] Tmkdir tag[2] dfid[4] name[s] mode[4] gid[4] */
667 static P9Req
*v9fs_tmkdir(QVirtio9P
*v9p
, uint32_t dfid
, const char *name
,
668 uint32_t mode
, uint32_t gid
, uint16_t tag
)
672 uint32_t body_size
= 4 + 4 + 4;
673 uint16_t string_size
= v9fs_string_size(name
);
675 g_assert_cmpint(body_size
, <=, UINT32_MAX
- string_size
);
676 body_size
+= string_size
;
678 req
= v9fs_req_init(v9p
, body_size
, P9_TMKDIR
, tag
);
679 v9fs_uint32_write(req
, dfid
);
680 v9fs_string_write(req
, name
);
681 v9fs_uint32_write(req
, mode
);
682 v9fs_uint32_write(req
, gid
);
687 /* size[4] Rmkdir tag[2] qid[13] */
688 static void v9fs_rmkdir(P9Req
*req
, v9fs_qid
*qid
)
690 v9fs_req_recv(req
, P9_RMKDIR
);
692 v9fs_memread(req
, qid
, 13);
694 v9fs_memskip(req
, 13);
699 /* size[4] Tlcreate tag[2] fid[4] name[s] flags[4] mode[4] gid[4] */
700 static P9Req
*v9fs_tlcreate(QVirtio9P
*v9p
, uint32_t fid
, const char *name
,
701 uint32_t flags
, uint32_t mode
, uint32_t gid
,
706 uint32_t body_size
= 4 + 4 + 4 + 4;
707 uint16_t string_size
= v9fs_string_size(name
);
709 g_assert_cmpint(body_size
, <=, UINT32_MAX
- string_size
);
710 body_size
+= string_size
;
712 req
= v9fs_req_init(v9p
, body_size
, P9_TLCREATE
, tag
);
713 v9fs_uint32_write(req
, fid
);
714 v9fs_string_write(req
, name
);
715 v9fs_uint32_write(req
, flags
);
716 v9fs_uint32_write(req
, mode
);
717 v9fs_uint32_write(req
, gid
);
722 /* size[4] Rlcreate tag[2] qid[13] iounit[4] */
723 static void v9fs_rlcreate(P9Req
*req
, v9fs_qid
*qid
, uint32_t *iounit
)
725 v9fs_req_recv(req
, P9_RLCREATE
);
727 v9fs_memread(req
, qid
, 13);
729 v9fs_memskip(req
, 13);
732 v9fs_uint32_read(req
, iounit
);
737 /* size[4] Tsymlink tag[2] fid[4] name[s] symtgt[s] gid[4] */
738 static P9Req
*v9fs_tsymlink(QVirtio9P
*v9p
, uint32_t fid
, const char *name
,
739 const char *symtgt
, uint32_t gid
, uint16_t tag
)
743 uint32_t body_size
= 4 + 4;
744 uint16_t string_size
= v9fs_string_size(name
) + v9fs_string_size(symtgt
);
746 g_assert_cmpint(body_size
, <=, UINT32_MAX
- string_size
);
747 body_size
+= string_size
;
749 req
= v9fs_req_init(v9p
, body_size
, P9_TSYMLINK
, tag
);
750 v9fs_uint32_write(req
, fid
);
751 v9fs_string_write(req
, name
);
752 v9fs_string_write(req
, symtgt
);
753 v9fs_uint32_write(req
, gid
);
758 /* size[4] Rsymlink tag[2] qid[13] */
759 static void v9fs_rsymlink(P9Req
*req
, v9fs_qid
*qid
)
761 v9fs_req_recv(req
, P9_RSYMLINK
);
763 v9fs_memread(req
, qid
, 13);
765 v9fs_memskip(req
, 13);
770 /* size[4] Tunlinkat tag[2] dirfd[4] name[s] flags[4] */
771 static P9Req
*v9fs_tunlinkat(QVirtio9P
*v9p
, uint32_t dirfd
, const char *name
,
772 uint32_t flags
, uint16_t tag
)
776 uint32_t body_size
= 4 + 4;
777 uint16_t string_size
= v9fs_string_size(name
);
779 g_assert_cmpint(body_size
, <=, UINT32_MAX
- string_size
);
780 body_size
+= string_size
;
782 req
= v9fs_req_init(v9p
, body_size
, P9_TUNLINKAT
, tag
);
783 v9fs_uint32_write(req
, dirfd
);
784 v9fs_string_write(req
, name
);
785 v9fs_uint32_write(req
, flags
);
790 /* size[4] Runlinkat tag[2] */
791 static void v9fs_runlinkat(P9Req
*req
)
793 v9fs_req_recv(req
, P9_RUNLINKAT
);
797 /* basic readdir test where reply fits into a single response message */
798 static void fs_readdir(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
800 QVirtio9P
*v9p
= obj
;
802 char *const wnames
[] = { g_strdup(QTEST_V9FS_SYNTH_READDIR_DIR
) };
805 uint32_t count
, nentries
;
806 struct V9fsDirent
*entries
= NULL
;
810 req
= v9fs_twalk(v9p
, 0, 1, 1, wnames
, 0);
811 v9fs_req_wait_for_reply(req
, NULL
);
812 v9fs_rwalk(req
, &nqid
, NULL
);
813 g_assert_cmpint(nqid
, ==, 1);
815 req
= v9fs_tlopen(v9p
, 1, O_DIRECTORY
, 0);
816 v9fs_req_wait_for_reply(req
, NULL
);
817 v9fs_rlopen(req
, &qid
, NULL
);
820 * submit count = msize - 11, because 11 is the header size of Rreaddir
822 req
= v9fs_treaddir(v9p
, 1, 0, P9_MAX_SIZE
- 11, 0);
823 v9fs_req_wait_for_reply(req
, NULL
);
824 v9fs_rreaddir(req
, &count
, &nentries
, &entries
);
827 * Assuming msize (P9_MAX_SIZE) is large enough so we can retrieve all
828 * dir entries with only one readdir request.
832 QTEST_V9FS_SYNTH_READDIR_NFILES
+ 2 /* "." and ".." */
836 * Check all file names exist in returned entries, ignore their order
839 g_assert_cmpint(fs_dirents_contain_name(entries
, "."), ==, true);
840 g_assert_cmpint(fs_dirents_contain_name(entries
, ".."), ==, true);
841 for (int i
= 0; i
< QTEST_V9FS_SYNTH_READDIR_NFILES
; ++i
) {
842 char *name
= g_strdup_printf(QTEST_V9FS_SYNTH_READDIR_FILE
, i
);
843 g_assert_cmpint(fs_dirents_contain_name(entries
, name
), ==, true);
847 v9fs_free_dirents(entries
);
851 /* readdir test where overall request is split over several messages */
852 static void do_readdir_split(QVirtio9P
*v9p
, uint32_t count
)
854 char *const wnames
[] = { g_strdup(QTEST_V9FS_SYNTH_READDIR_DIR
) };
857 uint32_t nentries
, npartialentries
;
858 struct V9fsDirent
*entries
, *tail
, *partialentries
;
871 req
= v9fs_twalk(v9p
, 0, fid
, 1, wnames
, 0);
872 v9fs_req_wait_for_reply(req
, NULL
);
873 v9fs_rwalk(req
, &nqid
, NULL
);
874 g_assert_cmpint(nqid
, ==, 1);
876 req
= v9fs_tlopen(v9p
, fid
, O_DIRECTORY
, 0);
877 v9fs_req_wait_for_reply(req
, NULL
);
878 v9fs_rlopen(req
, &qid
, NULL
);
881 * send as many Treaddir requests as required to get all directory
886 partialentries
= NULL
;
888 req
= v9fs_treaddir(v9p
, fid
, offset
, count
, 0);
889 v9fs_req_wait_for_reply(req
, NULL
);
890 v9fs_rreaddir(req
, &count
, &npartialentries
, &partialentries
);
891 if (npartialentries
> 0 && partialentries
) {
893 entries
= partialentries
;
894 nentries
= npartialentries
;
895 tail
= partialentries
;
897 tail
->next
= partialentries
;
898 nentries
+= npartialentries
;
903 offset
= tail
->offset
;
911 QTEST_V9FS_SYNTH_READDIR_NFILES
+ 2 /* "." and ".." */
915 * Check all file names exist in returned entries, ignore their order
918 g_assert_cmpint(fs_dirents_contain_name(entries
, "."), ==, true);
919 g_assert_cmpint(fs_dirents_contain_name(entries
, ".."), ==, true);
920 for (int i
= 0; i
< QTEST_V9FS_SYNTH_READDIR_NFILES
; ++i
) {
921 char *name
= g_strdup_printf(QTEST_V9FS_SYNTH_READDIR_FILE
, i
);
922 g_assert_cmpint(fs_dirents_contain_name(entries
, name
), ==, true);
926 v9fs_free_dirents(entries
);
931 static void fs_walk_no_slash(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
933 QVirtio9P
*v9p
= obj
;
935 char *const wnames
[] = { g_strdup(" /") };
940 req
= v9fs_twalk(v9p
, 0, 1, 1, wnames
, 0);
941 v9fs_req_wait_for_reply(req
, NULL
);
942 v9fs_rlerror(req
, &err
);
944 g_assert_cmpint(err
, ==, ENOENT
);
949 static void fs_walk_dotdot(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
951 QVirtio9P
*v9p
= obj
;
953 char *const wnames
[] = { g_strdup("..") };
954 v9fs_qid root_qid
, *wqid
;
958 req
= v9fs_tattach(v9p
, 0, getuid(), 0);
959 v9fs_req_wait_for_reply(req
, NULL
);
960 v9fs_rattach(req
, &root_qid
);
962 req
= v9fs_twalk(v9p
, 0, 1, 1, wnames
, 0);
963 v9fs_req_wait_for_reply(req
, NULL
);
964 v9fs_rwalk(req
, NULL
, &wqid
); /* We now we'll get one qid */
966 g_assert_cmpmem(&root_qid
, 13, wqid
[0], 13);
972 static void fs_lopen(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
974 QVirtio9P
*v9p
= obj
;
976 char *const wnames
[] = { g_strdup(QTEST_V9FS_SYNTH_LOPEN_FILE
) };
980 req
= v9fs_twalk(v9p
, 0, 1, 1, wnames
, 0);
981 v9fs_req_wait_for_reply(req
, NULL
);
982 v9fs_rwalk(req
, NULL
, NULL
);
984 req
= v9fs_tlopen(v9p
, 1, O_WRONLY
, 0);
985 v9fs_req_wait_for_reply(req
, NULL
);
986 v9fs_rlopen(req
, NULL
, NULL
);
991 static void fs_write(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
993 QVirtio9P
*v9p
= obj
;
995 static const uint32_t write_count
= P9_MAX_SIZE
/ 2;
996 char *const wnames
[] = { g_strdup(QTEST_V9FS_SYNTH_WRITE_FILE
) };
997 char *buf
= g_malloc0(write_count
);
1002 req
= v9fs_twalk(v9p
, 0, 1, 1, wnames
, 0);
1003 v9fs_req_wait_for_reply(req
, NULL
);
1004 v9fs_rwalk(req
, NULL
, NULL
);
1006 req
= v9fs_tlopen(v9p
, 1, O_WRONLY
, 0);
1007 v9fs_req_wait_for_reply(req
, NULL
);
1008 v9fs_rlopen(req
, NULL
, NULL
);
1010 req
= v9fs_twrite(v9p
, 1, 0, write_count
, buf
, 0);
1011 v9fs_req_wait_for_reply(req
, NULL
);
1012 v9fs_rwrite(req
, &count
);
1013 g_assert_cmpint(count
, ==, write_count
);
1019 static void fs_flush_success(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
1021 QVirtio9P
*v9p
= obj
;
1023 char *const wnames
[] = { g_strdup(QTEST_V9FS_SYNTH_FLUSH_FILE
) };
1024 P9Req
*req
, *flush_req
;
1026 uint8_t should_block
;
1029 req
= v9fs_twalk(v9p
, 0, 1, 1, wnames
, 0);
1030 v9fs_req_wait_for_reply(req
, NULL
);
1031 v9fs_rwalk(req
, NULL
, NULL
);
1033 req
= v9fs_tlopen(v9p
, 1, O_WRONLY
, 0);
1034 v9fs_req_wait_for_reply(req
, NULL
);
1035 v9fs_rlopen(req
, NULL
, NULL
);
1037 /* This will cause the 9p server to try to write data to the backend,
1038 * until the write request gets cancelled.
1041 req
= v9fs_twrite(v9p
, 1, 0, sizeof(should_block
), &should_block
, 0);
1043 flush_req
= v9fs_tflush(v9p
, req
->tag
, 1);
1045 /* The write request is supposed to be flushed: the server should just
1046 * mark the write request as used and reply to the flush request.
1048 v9fs_req_wait_for_reply(req
, &reply_len
);
1049 g_assert_cmpint(reply_len
, ==, 0);
1051 v9fs_rflush(flush_req
);
1056 static void fs_flush_ignored(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
1058 QVirtio9P
*v9p
= obj
;
1060 char *const wnames
[] = { g_strdup(QTEST_V9FS_SYNTH_FLUSH_FILE
) };
1061 P9Req
*req
, *flush_req
;
1063 uint8_t should_block
;
1066 req
= v9fs_twalk(v9p
, 0, 1, 1, wnames
, 0);
1067 v9fs_req_wait_for_reply(req
, NULL
);
1068 v9fs_rwalk(req
, NULL
, NULL
);
1070 req
= v9fs_tlopen(v9p
, 1, O_WRONLY
, 0);
1071 v9fs_req_wait_for_reply(req
, NULL
);
1072 v9fs_rlopen(req
, NULL
, NULL
);
1074 /* This will cause the write request to complete right away, before it
1075 * could be actually cancelled.
1078 req
= v9fs_twrite(v9p
, 1, 0, sizeof(should_block
), &should_block
, 0);
1080 flush_req
= v9fs_tflush(v9p
, req
->tag
, 1);
1082 /* The write request is supposed to complete. The server should
1083 * reply to the write request and the flush request.
1085 v9fs_req_wait_for_reply(req
, NULL
);
1086 v9fs_rwrite(req
, &count
);
1087 g_assert_cmpint(count
, ==, sizeof(should_block
));
1088 v9fs_rflush(flush_req
);
1093 static void do_mkdir(QVirtio9P
*v9p
, const char *path
, const char *cname
)
1095 char *const name
= g_strdup(cname
);
1099 fid
= do_walk(v9p
, path
);
1101 req
= v9fs_tmkdir(v9p
, fid
, name
, 0750, 0, 0);
1102 v9fs_req_wait_for_reply(req
, NULL
);
1103 v9fs_rmkdir(req
, NULL
);
1108 /* create a regular file with Tlcreate and return file's fid */
1109 static uint32_t do_lcreate(QVirtio9P
*v9p
, const char *path
,
1112 char *const name
= g_strdup(cname
);
1116 fid
= do_walk(v9p
, path
);
1118 req
= v9fs_tlcreate(v9p
, fid
, name
, 0, 0750, 0, 0);
1119 v9fs_req_wait_for_reply(req
, NULL
);
1120 v9fs_rlcreate(req
, NULL
, NULL
);
1126 /* create symlink named @a clink in directory @a path pointing to @a to */
1127 static void do_symlink(QVirtio9P
*v9p
, const char *path
, const char *clink
,
1130 char *const name
= g_strdup(clink
);
1131 char *const dst
= g_strdup(to
);
1135 fid
= do_walk(v9p
, path
);
1137 req
= v9fs_tsymlink(v9p
, fid
, name
, dst
, 0, 0);
1138 v9fs_req_wait_for_reply(req
, NULL
);
1139 v9fs_rsymlink(req
, NULL
);
1145 static void do_unlinkat(QVirtio9P
*v9p
, const char *atpath
, const char *rpath
,
1148 char *const name
= g_strdup(rpath
);
1152 fid
= do_walk(v9p
, atpath
);
1154 req
= v9fs_tunlinkat(v9p
, fid
, name
, flags
, 0);
1155 v9fs_req_wait_for_reply(req
, NULL
);
1156 v9fs_runlinkat(req
);
1161 static void fs_readdir_split_128(void *obj
, void *data
,
1162 QGuestAllocator
*t_alloc
)
1165 do_readdir_split(obj
, 128);
1168 static void fs_readdir_split_256(void *obj
, void *data
,
1169 QGuestAllocator
*t_alloc
)
1172 do_readdir_split(obj
, 256);
1175 static void fs_readdir_split_512(void *obj
, void *data
,
1176 QGuestAllocator
*t_alloc
)
1179 do_readdir_split(obj
, 512);
1183 /* tests using the 9pfs 'local' fs driver */
1185 static void fs_create_dir(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
1187 QVirtio9P
*v9p
= obj
;
1190 char *root_path
= virtio_9p_test_path("");
1191 char *new_dir
= virtio_9p_test_path("01");
1193 g_assert(root_path
!= NULL
);
1196 do_mkdir(v9p
, "/", "01");
1198 /* check if created directory really exists now ... */
1199 g_assert(stat(new_dir
, &st
) == 0);
1200 /* ... and is actually a directory */
1201 g_assert((st
.st_mode
& S_IFMT
) == S_IFDIR
);
1207 static void fs_unlinkat_dir(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
1209 QVirtio9P
*v9p
= obj
;
1212 char *root_path
= virtio_9p_test_path("");
1213 char *new_dir
= virtio_9p_test_path("02");
1215 g_assert(root_path
!= NULL
);
1218 do_mkdir(v9p
, "/", "02");
1220 /* check if created directory really exists now ... */
1221 g_assert(stat(new_dir
, &st
) == 0);
1222 /* ... and is actually a directory */
1223 g_assert((st
.st_mode
& S_IFMT
) == S_IFDIR
);
1225 do_unlinkat(v9p
, "/", "02", AT_REMOVEDIR
);
1226 /* directory should be gone now */
1227 g_assert(stat(new_dir
, &st
) != 0);
1233 static void fs_create_file(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
1235 QVirtio9P
*v9p
= obj
;
1238 char *new_file
= virtio_9p_test_path("03/1st_file");
1241 do_mkdir(v9p
, "/", "03");
1242 do_lcreate(v9p
, "03", "1st_file");
1244 /* check if created file exists now ... */
1245 g_assert(stat(new_file
, &st
) == 0);
1246 /* ... and is a regular file */
1247 g_assert((st
.st_mode
& S_IFMT
) == S_IFREG
);
1252 static void fs_unlinkat_file(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
1254 QVirtio9P
*v9p
= obj
;
1257 char *new_file
= virtio_9p_test_path("04/doa_file");
1260 do_mkdir(v9p
, "/", "04");
1261 do_lcreate(v9p
, "04", "doa_file");
1263 /* check if created file exists now ... */
1264 g_assert(stat(new_file
, &st
) == 0);
1265 /* ... and is a regular file */
1266 g_assert((st
.st_mode
& S_IFMT
) == S_IFREG
);
1268 do_unlinkat(v9p
, "04", "doa_file", 0);
1269 /* file should be gone now */
1270 g_assert(stat(new_file
, &st
) != 0);
1275 static void fs_symlink_file(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
1277 QVirtio9P
*v9p
= obj
;
1280 char *real_file
= virtio_9p_test_path("05/real_file");
1281 char *symlink_file
= virtio_9p_test_path("05/symlink_file");
1284 do_mkdir(v9p
, "/", "05");
1285 do_lcreate(v9p
, "05", "real_file");
1286 g_assert(stat(real_file
, &st
) == 0);
1287 g_assert((st
.st_mode
& S_IFMT
) == S_IFREG
);
1289 do_symlink(v9p
, "05", "symlink_file", "real_file");
1291 /* check if created link exists now */
1292 g_assert(stat(symlink_file
, &st
) == 0);
1294 g_free(symlink_file
);
1298 static void fs_unlinkat_symlink(void *obj
, void *data
,
1299 QGuestAllocator
*t_alloc
)
1301 QVirtio9P
*v9p
= obj
;
1304 char *real_file
= virtio_9p_test_path("06/real_file");
1305 char *symlink_file
= virtio_9p_test_path("06/symlink_file");
1308 do_mkdir(v9p
, "/", "06");
1309 do_lcreate(v9p
, "06", "real_file");
1310 g_assert(stat(real_file
, &st
) == 0);
1311 g_assert((st
.st_mode
& S_IFMT
) == S_IFREG
);
1313 do_symlink(v9p
, "06", "symlink_file", "real_file");
1314 g_assert(stat(symlink_file
, &st
) == 0);
1316 do_unlinkat(v9p
, "06", "symlink_file", 0);
1317 /* symlink should be gone now */
1318 g_assert(stat(symlink_file
, &st
) != 0);
1320 g_free(symlink_file
);
1324 static void *assign_9p_local_driver(GString
*cmd_line
, void *arg
)
1326 virtio_9p_assign_local_driver(cmd_line
, "security_model=mapped-xattr");
1330 static void register_virtio_9p_test(void)
1333 QOSGraphTestOptions opts
= {
1336 /* 9pfs test cases using the 'synth' filesystem driver */
1337 qos_add_test("synth/config", "virtio-9p", pci_config
, &opts
);
1338 qos_add_test("synth/version/basic", "virtio-9p", fs_version
, &opts
);
1339 qos_add_test("synth/attach/basic", "virtio-9p", fs_attach
, &opts
);
1340 qos_add_test("synth/walk/basic", "virtio-9p", fs_walk
, &opts
);
1341 qos_add_test("synth/walk/no_slash", "virtio-9p", fs_walk_no_slash
,
1343 qos_add_test("synth/walk/dotdot_from_root", "virtio-9p",
1344 fs_walk_dotdot
, &opts
);
1345 qos_add_test("synth/lopen/basic", "virtio-9p", fs_lopen
, &opts
);
1346 qos_add_test("synth/write/basic", "virtio-9p", fs_write
, &opts
);
1347 qos_add_test("synth/flush/success", "virtio-9p", fs_flush_success
,
1349 qos_add_test("synth/flush/ignored", "virtio-9p", fs_flush_ignored
,
1351 qos_add_test("synth/readdir/basic", "virtio-9p", fs_readdir
, &opts
);
1352 qos_add_test("synth/readdir/split_512", "virtio-9p",
1353 fs_readdir_split_512
, &opts
);
1354 qos_add_test("synth/readdir/split_256", "virtio-9p",
1355 fs_readdir_split_256
, &opts
);
1356 qos_add_test("synth/readdir/split_128", "virtio-9p",
1357 fs_readdir_split_128
, &opts
);
1360 /* 9pfs test cases using the 'local' filesystem driver */
1361 opts
.before
= assign_9p_local_driver
;
1362 qos_add_test("local/config", "virtio-9p", pci_config
, &opts
);
1363 qos_add_test("local/create_dir", "virtio-9p", fs_create_dir
, &opts
);
1364 qos_add_test("local/unlinkat_dir", "virtio-9p", fs_unlinkat_dir
, &opts
);
1365 qos_add_test("local/create_file", "virtio-9p", fs_create_file
, &opts
);
1366 qos_add_test("local/unlinkat_file", "virtio-9p", fs_unlinkat_file
, &opts
);
1367 qos_add_test("local/symlink_file", "virtio-9p", fs_symlink_file
, &opts
);
1368 qos_add_test("local/unlinkat_symlink", "virtio-9p", fs_unlinkat_symlink
,
1372 libqos_init(register_virtio_9p_test
);
1374 static void __attribute__((constructor
)) construct_9p_test(void)
1376 /* make sure test dir for the 'local' tests exists */
1377 virtio_9p_create_local_test_dir();
1380 static void __attribute__((destructor
)) destruct_9p_test(void)
1382 /* remove previously created test dir when test suite completed */
1383 virtio_9p_remove_local_test_dir();