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_RUNLINKAT
? "RUNLINKAT" :
263 id
== P9_RFLUSH
? "RFLUSH" :
264 id
== P9_RREADDIR
? "READDIR" :
268 static void v9fs_req_wait_for_reply(P9Req
*req
, uint32_t *len
)
270 QVirtio9P
*v9p
= req
->v9p
;
272 qvirtio_wait_used_elem(req
->qts
, v9p
->vdev
, v9p
->vq
, req
->free_head
, len
,
273 QVIRTIO_9P_TIMEOUT_US
);
276 static void v9fs_req_recv(P9Req
*req
, uint8_t id
)
280 v9fs_memread(req
, &hdr
, 7);
281 hdr
.size
= ldl_le_p(&hdr
.size
);
282 hdr
.tag
= lduw_le_p(&hdr
.tag
);
284 g_assert_cmpint(hdr
.size
, >=, 7);
285 g_assert_cmpint(hdr
.size
, <=, P9_MAX_SIZE
);
286 g_assert_cmpint(hdr
.tag
, ==, req
->tag
);
289 g_printerr("Received response %d (%s) instead of %d (%s)\n",
290 hdr
.id
, rmessage_name(hdr
.id
), id
, rmessage_name(id
));
292 if (hdr
.id
== P9_RLERROR
) {
294 v9fs_uint32_read(req
, &err
);
295 g_printerr("Rlerror has errno %d (%s)\n", err
, strerror(err
));
298 g_assert_cmpint(hdr
.id
, ==, id
);
301 static void v9fs_req_free(P9Req
*req
)
303 guest_free(alloc
, req
->t_msg
);
304 guest_free(alloc
, req
->r_msg
);
308 /* size[4] Rlerror tag[2] ecode[4] */
309 static void v9fs_rlerror(P9Req
*req
, uint32_t *err
)
311 v9fs_req_recv(req
, P9_RLERROR
);
312 v9fs_uint32_read(req
, err
);
316 /* size[4] Tversion tag[2] msize[4] version[s] */
317 static P9Req
*v9fs_tversion(QVirtio9P
*v9p
, uint32_t msize
, const char *version
,
321 uint32_t body_size
= 4;
322 uint16_t string_size
= v9fs_string_size(version
);
324 g_assert_cmpint(body_size
, <=, UINT32_MAX
- string_size
);
325 body_size
+= string_size
;
326 req
= v9fs_req_init(v9p
, body_size
, P9_TVERSION
, tag
);
328 v9fs_uint32_write(req
, msize
);
329 v9fs_string_write(req
, version
);
334 /* size[4] Rversion tag[2] msize[4] version[s] */
335 static void v9fs_rversion(P9Req
*req
, uint16_t *len
, char **version
)
339 v9fs_req_recv(req
, P9_RVERSION
);
340 v9fs_uint32_read(req
, &msize
);
342 g_assert_cmpint(msize
, ==, P9_MAX_SIZE
);
344 if (len
|| version
) {
345 v9fs_string_read(req
, len
, version
);
351 /* size[4] Tattach tag[2] fid[4] afid[4] uname[s] aname[s] n_uname[4] */
352 static P9Req
*v9fs_tattach(QVirtio9P
*v9p
, uint32_t fid
, uint32_t n_uname
,
355 const char *uname
= ""; /* ignored by QEMU */
356 const char *aname
= ""; /* ignored by QEMU */
357 P9Req
*req
= v9fs_req_init(v9p
, 4 + 4 + 2 + 2 + 4, P9_TATTACH
, tag
);
359 v9fs_uint32_write(req
, fid
);
360 v9fs_uint32_write(req
, P9_NOFID
);
361 v9fs_string_write(req
, uname
);
362 v9fs_string_write(req
, aname
);
363 v9fs_uint32_write(req
, n_uname
);
368 typedef char v9fs_qid
[13];
370 /* size[4] Rattach tag[2] qid[13] */
371 static void v9fs_rattach(P9Req
*req
, v9fs_qid
*qid
)
373 v9fs_req_recv(req
, P9_RATTACH
);
375 v9fs_memread(req
, qid
, 13);
380 /* size[4] Twalk tag[2] fid[4] newfid[4] nwname[2] nwname*(wname[s]) */
381 static P9Req
*v9fs_twalk(QVirtio9P
*v9p
, uint32_t fid
, uint32_t newfid
,
382 uint16_t nwname
, char *const wnames
[], uint16_t tag
)
386 uint32_t body_size
= 4 + 4 + 2;
388 for (i
= 0; i
< nwname
; i
++) {
389 uint16_t wname_size
= v9fs_string_size(wnames
[i
]);
391 g_assert_cmpint(body_size
, <=, UINT32_MAX
- wname_size
);
392 body_size
+= wname_size
;
394 req
= v9fs_req_init(v9p
, body_size
, P9_TWALK
, tag
);
395 v9fs_uint32_write(req
, fid
);
396 v9fs_uint32_write(req
, newfid
);
397 v9fs_uint16_write(req
, nwname
);
398 for (i
= 0; i
< nwname
; i
++) {
399 v9fs_string_write(req
, wnames
[i
]);
405 /* size[4] Rwalk tag[2] nwqid[2] nwqid*(wqid[13]) */
406 static void v9fs_rwalk(P9Req
*req
, uint16_t *nwqid
, v9fs_qid
**wqid
)
408 uint16_t local_nwqid
;
410 v9fs_req_recv(req
, P9_RWALK
);
411 v9fs_uint16_read(req
, &local_nwqid
);
413 *nwqid
= local_nwqid
;
416 *wqid
= g_malloc(local_nwqid
* 13);
417 v9fs_memread(req
, *wqid
, local_nwqid
* 13);
422 /* size[4] Treaddir tag[2] fid[4] offset[8] count[4] */
423 static P9Req
*v9fs_treaddir(QVirtio9P
*v9p
, uint32_t fid
, uint64_t offset
,
424 uint32_t count
, uint16_t tag
)
428 req
= v9fs_req_init(v9p
, 4 + 8 + 4, P9_TREADDIR
, tag
);
429 v9fs_uint32_write(req
, fid
);
430 v9fs_uint64_write(req
, offset
);
431 v9fs_uint32_write(req
, count
);
441 struct V9fsDirent
*next
;
444 /* size[4] Rreaddir tag[2] count[4] data[count] */
445 static void v9fs_rreaddir(P9Req
*req
, uint32_t *count
, uint32_t *nentries
,
446 struct V9fsDirent
**entries
)
448 uint32_t local_count
;
449 struct V9fsDirent
*e
= NULL
;
453 v9fs_req_recv(req
, P9_RREADDIR
);
454 v9fs_uint32_read(req
, &local_count
);
457 *count
= local_count
;
460 for (int32_t togo
= (int32_t)local_count
;
461 togo
>= 13 + 8 + 1 + 2;
462 togo
-= 13 + 8 + 1 + 2 + slen
, ++n
)
465 e
= g_malloc(sizeof(struct V9fsDirent
));
470 e
= e
->next
= g_malloc(sizeof(struct V9fsDirent
));
473 /* qid[13] offset[8] type[1] name[s] */
474 v9fs_memread(req
, &e
->qid
, 13);
475 v9fs_uint64_read(req
, &e
->offset
);
476 v9fs_uint8_read(req
, &e
->type
);
477 v9fs_string_read(req
, &slen
, &e
->name
);
487 static void v9fs_free_dirents(struct V9fsDirent
*e
)
489 struct V9fsDirent
*next
= NULL
;
491 for (; e
; e
= next
) {
498 /* size[4] Tlopen tag[2] fid[4] flags[4] */
499 static P9Req
*v9fs_tlopen(QVirtio9P
*v9p
, uint32_t fid
, uint32_t flags
,
504 req
= v9fs_req_init(v9p
, 4 + 4, P9_TLOPEN
, tag
);
505 v9fs_uint32_write(req
, fid
);
506 v9fs_uint32_write(req
, flags
);
511 /* size[4] Rlopen tag[2] qid[13] iounit[4] */
512 static void v9fs_rlopen(P9Req
*req
, v9fs_qid
*qid
, uint32_t *iounit
)
514 v9fs_req_recv(req
, P9_RLOPEN
);
516 v9fs_memread(req
, qid
, 13);
518 v9fs_memskip(req
, 13);
521 v9fs_uint32_read(req
, iounit
);
526 /* size[4] Twrite tag[2] fid[4] offset[8] count[4] data[count] */
527 static P9Req
*v9fs_twrite(QVirtio9P
*v9p
, uint32_t fid
, uint64_t offset
,
528 uint32_t count
, const void *data
, uint16_t tag
)
531 uint32_t body_size
= 4 + 8 + 4;
533 g_assert_cmpint(body_size
, <=, UINT32_MAX
- count
);
535 req
= v9fs_req_init(v9p
, body_size
, P9_TWRITE
, tag
);
536 v9fs_uint32_write(req
, fid
);
537 v9fs_uint64_write(req
, offset
);
538 v9fs_uint32_write(req
, count
);
539 v9fs_memwrite(req
, data
, count
);
544 /* size[4] Rwrite tag[2] count[4] */
545 static void v9fs_rwrite(P9Req
*req
, uint32_t *count
)
547 v9fs_req_recv(req
, P9_RWRITE
);
549 v9fs_uint32_read(req
, count
);
554 /* size[4] Tflush tag[2] oldtag[2] */
555 static P9Req
*v9fs_tflush(QVirtio9P
*v9p
, uint16_t oldtag
, uint16_t tag
)
559 req
= v9fs_req_init(v9p
, 2, P9_TFLUSH
, tag
);
560 v9fs_uint32_write(req
, oldtag
);
565 /* size[4] Rflush tag[2] */
566 static void v9fs_rflush(P9Req
*req
)
568 v9fs_req_recv(req
, P9_RFLUSH
);
572 static void do_version(QVirtio9P
*v9p
)
574 const char *version
= "9P2000.L";
576 char *server_version
;
579 req
= v9fs_tversion(v9p
, P9_MAX_SIZE
, version
, P9_NOTAG
);
580 v9fs_req_wait_for_reply(req
, NULL
);
581 v9fs_rversion(req
, &server_len
, &server_version
);
583 g_assert_cmpmem(server_version
, server_len
, version
, strlen(version
));
585 g_free(server_version
);
588 /* utility function: walk to requested dir and return fid for that dir */
589 static uint32_t do_walk(QVirtio9P
*v9p
, const char *path
)
593 const uint32_t fid
= genfid();
595 int nwnames
= split(path
, "/", &wnames
);
597 req
= v9fs_twalk(v9p
, 0, fid
, nwnames
, wnames
, 0);
598 v9fs_req_wait_for_reply(req
, NULL
);
599 v9fs_rwalk(req
, NULL
, NULL
);
605 static void fs_version(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
611 static void do_attach(QVirtio9P
*v9p
)
616 req
= v9fs_tattach(v9p
, 0, getuid(), 0);
617 v9fs_req_wait_for_reply(req
, NULL
);
618 v9fs_rattach(req
, NULL
);
621 static void fs_attach(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
627 static void fs_walk(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
629 QVirtio9P
*v9p
= obj
;
631 char *wnames
[P9_MAXWELEM
];
637 for (i
= 0; i
< P9_MAXWELEM
; i
++) {
638 wnames
[i
] = g_strdup_printf(QTEST_V9FS_SYNTH_WALK_FILE
, i
);
642 req
= v9fs_twalk(v9p
, 0, 1, P9_MAXWELEM
, wnames
, 0);
643 v9fs_req_wait_for_reply(req
, NULL
);
644 v9fs_rwalk(req
, &nwqid
, &wqid
);
646 g_assert_cmpint(nwqid
, ==, P9_MAXWELEM
);
648 for (i
= 0; i
< P9_MAXWELEM
; i
++) {
655 static bool fs_dirents_contain_name(struct V9fsDirent
*e
, const char* name
)
657 for (; e
; e
= e
->next
) {
658 if (!strcmp(e
->name
, name
)) {
665 /* size[4] Tmkdir tag[2] dfid[4] name[s] mode[4] gid[4] */
666 static P9Req
*v9fs_tmkdir(QVirtio9P
*v9p
, uint32_t dfid
, const char *name
,
667 uint32_t mode
, uint32_t gid
, uint16_t tag
)
671 uint32_t body_size
= 4 + 4 + 4;
672 uint16_t string_size
= v9fs_string_size(name
);
674 g_assert_cmpint(body_size
, <=, UINT32_MAX
- string_size
);
675 body_size
+= string_size
;
677 req
= v9fs_req_init(v9p
, body_size
, P9_TMKDIR
, tag
);
678 v9fs_uint32_write(req
, dfid
);
679 v9fs_string_write(req
, name
);
680 v9fs_uint32_write(req
, mode
);
681 v9fs_uint32_write(req
, gid
);
686 /* size[4] Rmkdir tag[2] qid[13] */
687 static void v9fs_rmkdir(P9Req
*req
, v9fs_qid
*qid
)
689 v9fs_req_recv(req
, P9_RMKDIR
);
691 v9fs_memread(req
, qid
, 13);
693 v9fs_memskip(req
, 13);
698 /* size[4] Tlcreate tag[2] fid[4] name[s] flags[4] mode[4] gid[4] */
699 static P9Req
*v9fs_tlcreate(QVirtio9P
*v9p
, uint32_t fid
, const char *name
,
700 uint32_t flags
, uint32_t mode
, uint32_t gid
,
705 uint32_t body_size
= 4 + 4 + 4 + 4;
706 uint16_t string_size
= v9fs_string_size(name
);
708 g_assert_cmpint(body_size
, <=, UINT32_MAX
- string_size
);
709 body_size
+= string_size
;
711 req
= v9fs_req_init(v9p
, body_size
, P9_TLCREATE
, tag
);
712 v9fs_uint32_write(req
, fid
);
713 v9fs_string_write(req
, name
);
714 v9fs_uint32_write(req
, flags
);
715 v9fs_uint32_write(req
, mode
);
716 v9fs_uint32_write(req
, gid
);
721 /* size[4] Rlcreate tag[2] qid[13] iounit[4] */
722 static void v9fs_rlcreate(P9Req
*req
, v9fs_qid
*qid
, uint32_t *iounit
)
724 v9fs_req_recv(req
, P9_RLCREATE
);
726 v9fs_memread(req
, qid
, 13);
728 v9fs_memskip(req
, 13);
731 v9fs_uint32_read(req
, iounit
);
736 /* size[4] Tunlinkat tag[2] dirfd[4] name[s] flags[4] */
737 static P9Req
*v9fs_tunlinkat(QVirtio9P
*v9p
, uint32_t dirfd
, const char *name
,
738 uint32_t flags
, uint16_t tag
)
742 uint32_t body_size
= 4 + 4;
743 uint16_t string_size
= v9fs_string_size(name
);
745 g_assert_cmpint(body_size
, <=, UINT32_MAX
- string_size
);
746 body_size
+= string_size
;
748 req
= v9fs_req_init(v9p
, body_size
, P9_TUNLINKAT
, tag
);
749 v9fs_uint32_write(req
, dirfd
);
750 v9fs_string_write(req
, name
);
751 v9fs_uint32_write(req
, flags
);
756 /* size[4] Runlinkat tag[2] */
757 static void v9fs_runlinkat(P9Req
*req
)
759 v9fs_req_recv(req
, P9_RUNLINKAT
);
763 /* basic readdir test where reply fits into a single response message */
764 static void fs_readdir(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
766 QVirtio9P
*v9p
= obj
;
768 char *const wnames
[] = { g_strdup(QTEST_V9FS_SYNTH_READDIR_DIR
) };
771 uint32_t count
, nentries
;
772 struct V9fsDirent
*entries
= NULL
;
776 req
= v9fs_twalk(v9p
, 0, 1, 1, wnames
, 0);
777 v9fs_req_wait_for_reply(req
, NULL
);
778 v9fs_rwalk(req
, &nqid
, NULL
);
779 g_assert_cmpint(nqid
, ==, 1);
781 req
= v9fs_tlopen(v9p
, 1, O_DIRECTORY
, 0);
782 v9fs_req_wait_for_reply(req
, NULL
);
783 v9fs_rlopen(req
, &qid
, NULL
);
786 * submit count = msize - 11, because 11 is the header size of Rreaddir
788 req
= v9fs_treaddir(v9p
, 1, 0, P9_MAX_SIZE
- 11, 0);
789 v9fs_req_wait_for_reply(req
, NULL
);
790 v9fs_rreaddir(req
, &count
, &nentries
, &entries
);
793 * Assuming msize (P9_MAX_SIZE) is large enough so we can retrieve all
794 * dir entries with only one readdir request.
798 QTEST_V9FS_SYNTH_READDIR_NFILES
+ 2 /* "." and ".." */
802 * Check all file names exist in returned entries, ignore their order
805 g_assert_cmpint(fs_dirents_contain_name(entries
, "."), ==, true);
806 g_assert_cmpint(fs_dirents_contain_name(entries
, ".."), ==, true);
807 for (int i
= 0; i
< QTEST_V9FS_SYNTH_READDIR_NFILES
; ++i
) {
808 char *name
= g_strdup_printf(QTEST_V9FS_SYNTH_READDIR_FILE
, i
);
809 g_assert_cmpint(fs_dirents_contain_name(entries
, name
), ==, true);
813 v9fs_free_dirents(entries
);
817 /* readdir test where overall request is split over several messages */
818 static void do_readdir_split(QVirtio9P
*v9p
, uint32_t count
)
820 char *const wnames
[] = { g_strdup(QTEST_V9FS_SYNTH_READDIR_DIR
) };
823 uint32_t nentries
, npartialentries
;
824 struct V9fsDirent
*entries
, *tail
, *partialentries
;
837 req
= v9fs_twalk(v9p
, 0, fid
, 1, wnames
, 0);
838 v9fs_req_wait_for_reply(req
, NULL
);
839 v9fs_rwalk(req
, &nqid
, NULL
);
840 g_assert_cmpint(nqid
, ==, 1);
842 req
= v9fs_tlopen(v9p
, fid
, O_DIRECTORY
, 0);
843 v9fs_req_wait_for_reply(req
, NULL
);
844 v9fs_rlopen(req
, &qid
, NULL
);
847 * send as many Treaddir requests as required to get all directory
852 partialentries
= NULL
;
854 req
= v9fs_treaddir(v9p
, fid
, offset
, count
, 0);
855 v9fs_req_wait_for_reply(req
, NULL
);
856 v9fs_rreaddir(req
, &count
, &npartialentries
, &partialentries
);
857 if (npartialentries
> 0 && partialentries
) {
859 entries
= partialentries
;
860 nentries
= npartialentries
;
861 tail
= partialentries
;
863 tail
->next
= partialentries
;
864 nentries
+= npartialentries
;
869 offset
= tail
->offset
;
877 QTEST_V9FS_SYNTH_READDIR_NFILES
+ 2 /* "." and ".." */
881 * Check all file names exist in returned entries, ignore their order
884 g_assert_cmpint(fs_dirents_contain_name(entries
, "."), ==, true);
885 g_assert_cmpint(fs_dirents_contain_name(entries
, ".."), ==, true);
886 for (int i
= 0; i
< QTEST_V9FS_SYNTH_READDIR_NFILES
; ++i
) {
887 char *name
= g_strdup_printf(QTEST_V9FS_SYNTH_READDIR_FILE
, i
);
888 g_assert_cmpint(fs_dirents_contain_name(entries
, name
), ==, true);
892 v9fs_free_dirents(entries
);
897 static void fs_walk_no_slash(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
899 QVirtio9P
*v9p
= obj
;
901 char *const wnames
[] = { g_strdup(" /") };
906 req
= v9fs_twalk(v9p
, 0, 1, 1, wnames
, 0);
907 v9fs_req_wait_for_reply(req
, NULL
);
908 v9fs_rlerror(req
, &err
);
910 g_assert_cmpint(err
, ==, ENOENT
);
915 static void fs_walk_dotdot(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
917 QVirtio9P
*v9p
= obj
;
919 char *const wnames
[] = { g_strdup("..") };
920 v9fs_qid root_qid
, *wqid
;
924 req
= v9fs_tattach(v9p
, 0, getuid(), 0);
925 v9fs_req_wait_for_reply(req
, NULL
);
926 v9fs_rattach(req
, &root_qid
);
928 req
= v9fs_twalk(v9p
, 0, 1, 1, wnames
, 0);
929 v9fs_req_wait_for_reply(req
, NULL
);
930 v9fs_rwalk(req
, NULL
, &wqid
); /* We now we'll get one qid */
932 g_assert_cmpmem(&root_qid
, 13, wqid
[0], 13);
938 static void fs_lopen(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
940 QVirtio9P
*v9p
= obj
;
942 char *const wnames
[] = { g_strdup(QTEST_V9FS_SYNTH_LOPEN_FILE
) };
946 req
= v9fs_twalk(v9p
, 0, 1, 1, wnames
, 0);
947 v9fs_req_wait_for_reply(req
, NULL
);
948 v9fs_rwalk(req
, NULL
, NULL
);
950 req
= v9fs_tlopen(v9p
, 1, O_WRONLY
, 0);
951 v9fs_req_wait_for_reply(req
, NULL
);
952 v9fs_rlopen(req
, NULL
, NULL
);
957 static void fs_write(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
959 QVirtio9P
*v9p
= obj
;
961 static const uint32_t write_count
= P9_MAX_SIZE
/ 2;
962 char *const wnames
[] = { g_strdup(QTEST_V9FS_SYNTH_WRITE_FILE
) };
963 char *buf
= g_malloc0(write_count
);
968 req
= v9fs_twalk(v9p
, 0, 1, 1, wnames
, 0);
969 v9fs_req_wait_for_reply(req
, NULL
);
970 v9fs_rwalk(req
, NULL
, NULL
);
972 req
= v9fs_tlopen(v9p
, 1, O_WRONLY
, 0);
973 v9fs_req_wait_for_reply(req
, NULL
);
974 v9fs_rlopen(req
, NULL
, NULL
);
976 req
= v9fs_twrite(v9p
, 1, 0, write_count
, buf
, 0);
977 v9fs_req_wait_for_reply(req
, NULL
);
978 v9fs_rwrite(req
, &count
);
979 g_assert_cmpint(count
, ==, write_count
);
985 static void fs_flush_success(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
987 QVirtio9P
*v9p
= obj
;
989 char *const wnames
[] = { g_strdup(QTEST_V9FS_SYNTH_FLUSH_FILE
) };
990 P9Req
*req
, *flush_req
;
992 uint8_t should_block
;
995 req
= v9fs_twalk(v9p
, 0, 1, 1, wnames
, 0);
996 v9fs_req_wait_for_reply(req
, NULL
);
997 v9fs_rwalk(req
, NULL
, NULL
);
999 req
= v9fs_tlopen(v9p
, 1, O_WRONLY
, 0);
1000 v9fs_req_wait_for_reply(req
, NULL
);
1001 v9fs_rlopen(req
, NULL
, NULL
);
1003 /* This will cause the 9p server to try to write data to the backend,
1004 * until the write request gets cancelled.
1007 req
= v9fs_twrite(v9p
, 1, 0, sizeof(should_block
), &should_block
, 0);
1009 flush_req
= v9fs_tflush(v9p
, req
->tag
, 1);
1011 /* The write request is supposed to be flushed: the server should just
1012 * mark the write request as used and reply to the flush request.
1014 v9fs_req_wait_for_reply(req
, &reply_len
);
1015 g_assert_cmpint(reply_len
, ==, 0);
1017 v9fs_rflush(flush_req
);
1022 static void fs_flush_ignored(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
1024 QVirtio9P
*v9p
= obj
;
1026 char *const wnames
[] = { g_strdup(QTEST_V9FS_SYNTH_FLUSH_FILE
) };
1027 P9Req
*req
, *flush_req
;
1029 uint8_t should_block
;
1032 req
= v9fs_twalk(v9p
, 0, 1, 1, wnames
, 0);
1033 v9fs_req_wait_for_reply(req
, NULL
);
1034 v9fs_rwalk(req
, NULL
, NULL
);
1036 req
= v9fs_tlopen(v9p
, 1, O_WRONLY
, 0);
1037 v9fs_req_wait_for_reply(req
, NULL
);
1038 v9fs_rlopen(req
, NULL
, NULL
);
1040 /* This will cause the write request to complete right away, before it
1041 * could be actually cancelled.
1044 req
= v9fs_twrite(v9p
, 1, 0, sizeof(should_block
), &should_block
, 0);
1046 flush_req
= v9fs_tflush(v9p
, req
->tag
, 1);
1048 /* The write request is supposed to complete. The server should
1049 * reply to the write request and the flush request.
1051 v9fs_req_wait_for_reply(req
, NULL
);
1052 v9fs_rwrite(req
, &count
);
1053 g_assert_cmpint(count
, ==, sizeof(should_block
));
1054 v9fs_rflush(flush_req
);
1059 static void do_mkdir(QVirtio9P
*v9p
, const char *path
, const char *cname
)
1061 char *const name
= g_strdup(cname
);
1065 fid
= do_walk(v9p
, path
);
1067 req
= v9fs_tmkdir(v9p
, fid
, name
, 0750, 0, 0);
1068 v9fs_req_wait_for_reply(req
, NULL
);
1069 v9fs_rmkdir(req
, NULL
);
1074 /* create a regular file with Tlcreate and return file's fid */
1075 static uint32_t do_lcreate(QVirtio9P
*v9p
, const char *path
,
1078 char *const name
= g_strdup(cname
);
1082 fid
= do_walk(v9p
, path
);
1084 req
= v9fs_tlcreate(v9p
, fid
, name
, 0, 0750, 0, 0);
1085 v9fs_req_wait_for_reply(req
, NULL
);
1086 v9fs_rlcreate(req
, NULL
, NULL
);
1092 static void do_unlinkat(QVirtio9P
*v9p
, const char *atpath
, const char *rpath
,
1095 char *const name
= g_strdup(rpath
);
1099 fid
= do_walk(v9p
, atpath
);
1101 req
= v9fs_tunlinkat(v9p
, fid
, name
, flags
, 0);
1102 v9fs_req_wait_for_reply(req
, NULL
);
1103 v9fs_runlinkat(req
);
1108 static void fs_readdir_split_128(void *obj
, void *data
,
1109 QGuestAllocator
*t_alloc
)
1112 do_readdir_split(obj
, 128);
1115 static void fs_readdir_split_256(void *obj
, void *data
,
1116 QGuestAllocator
*t_alloc
)
1119 do_readdir_split(obj
, 256);
1122 static void fs_readdir_split_512(void *obj
, void *data
,
1123 QGuestAllocator
*t_alloc
)
1126 do_readdir_split(obj
, 512);
1130 /* tests using the 9pfs 'local' fs driver */
1132 static void fs_create_dir(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
1134 QVirtio9P
*v9p
= obj
;
1137 char *root_path
= virtio_9p_test_path("");
1138 char *new_dir
= virtio_9p_test_path("01");
1140 g_assert(root_path
!= NULL
);
1143 do_mkdir(v9p
, "/", "01");
1145 /* check if created directory really exists now ... */
1146 g_assert(stat(new_dir
, &st
) == 0);
1147 /* ... and is actually a directory */
1148 g_assert((st
.st_mode
& S_IFMT
) == S_IFDIR
);
1154 static void fs_unlinkat_dir(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
1156 QVirtio9P
*v9p
= obj
;
1159 char *root_path
= virtio_9p_test_path("");
1160 char *new_dir
= virtio_9p_test_path("02");
1162 g_assert(root_path
!= NULL
);
1165 do_mkdir(v9p
, "/", "02");
1167 /* check if created directory really exists now ... */
1168 g_assert(stat(new_dir
, &st
) == 0);
1169 /* ... and is actually a directory */
1170 g_assert((st
.st_mode
& S_IFMT
) == S_IFDIR
);
1172 do_unlinkat(v9p
, "/", "02", AT_REMOVEDIR
);
1173 /* directory should be gone now */
1174 g_assert(stat(new_dir
, &st
) != 0);
1180 static void fs_create_file(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
1182 QVirtio9P
*v9p
= obj
;
1185 char *new_file
= virtio_9p_test_path("03/1st_file");
1188 do_mkdir(v9p
, "/", "03");
1189 do_lcreate(v9p
, "03", "1st_file");
1191 /* check if created file exists now ... */
1192 g_assert(stat(new_file
, &st
) == 0);
1193 /* ... and is a regular file */
1194 g_assert((st
.st_mode
& S_IFMT
) == S_IFREG
);
1199 static void *assign_9p_local_driver(GString
*cmd_line
, void *arg
)
1201 virtio_9p_assign_local_driver(cmd_line
, "security_model=mapped-xattr");
1205 static void register_virtio_9p_test(void)
1208 QOSGraphTestOptions opts
= {
1211 /* 9pfs test cases using the 'synth' filesystem driver */
1212 qos_add_test("synth/config", "virtio-9p", pci_config
, &opts
);
1213 qos_add_test("synth/version/basic", "virtio-9p", fs_version
, &opts
);
1214 qos_add_test("synth/attach/basic", "virtio-9p", fs_attach
, &opts
);
1215 qos_add_test("synth/walk/basic", "virtio-9p", fs_walk
, &opts
);
1216 qos_add_test("synth/walk/no_slash", "virtio-9p", fs_walk_no_slash
,
1218 qos_add_test("synth/walk/dotdot_from_root", "virtio-9p",
1219 fs_walk_dotdot
, &opts
);
1220 qos_add_test("synth/lopen/basic", "virtio-9p", fs_lopen
, &opts
);
1221 qos_add_test("synth/write/basic", "virtio-9p", fs_write
, &opts
);
1222 qos_add_test("synth/flush/success", "virtio-9p", fs_flush_success
,
1224 qos_add_test("synth/flush/ignored", "virtio-9p", fs_flush_ignored
,
1226 qos_add_test("synth/readdir/basic", "virtio-9p", fs_readdir
, &opts
);
1227 qos_add_test("synth/readdir/split_512", "virtio-9p",
1228 fs_readdir_split_512
, &opts
);
1229 qos_add_test("synth/readdir/split_256", "virtio-9p",
1230 fs_readdir_split_256
, &opts
);
1231 qos_add_test("synth/readdir/split_128", "virtio-9p",
1232 fs_readdir_split_128
, &opts
);
1235 /* 9pfs test cases using the 'local' filesystem driver */
1236 opts
.before
= assign_9p_local_driver
;
1237 qos_add_test("local/config", "virtio-9p", pci_config
, &opts
);
1238 qos_add_test("local/create_dir", "virtio-9p", fs_create_dir
, &opts
);
1239 qos_add_test("local/unlinkat_dir", "virtio-9p", fs_unlinkat_dir
, &opts
);
1240 qos_add_test("local/create_file", "virtio-9p", fs_create_file
, &opts
);
1243 libqos_init(register_virtio_9p_test
);
1245 static void __attribute__((constructor
)) construct_9p_test(void)
1247 /* make sure test dir for the 'local' tests exists */
1248 virtio_9p_create_local_test_dir();
1251 static void __attribute__((destructor
)) destruct_9p_test(void)
1253 /* remove previously created test dir when test suite completed */
1254 virtio_9p_remove_local_test_dir();