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_RLINK
? "RLINK" :
264 id
== P9_RUNLINKAT
? "RUNLINKAT" :
265 id
== P9_RFLUSH
? "RFLUSH" :
266 id
== P9_RREADDIR
? "READDIR" :
270 static void v9fs_req_wait_for_reply(P9Req
*req
, uint32_t *len
)
272 QVirtio9P
*v9p
= req
->v9p
;
274 qvirtio_wait_used_elem(req
->qts
, v9p
->vdev
, v9p
->vq
, req
->free_head
, len
,
275 QVIRTIO_9P_TIMEOUT_US
);
278 static void v9fs_req_recv(P9Req
*req
, uint8_t id
)
282 v9fs_memread(req
, &hdr
, 7);
283 hdr
.size
= ldl_le_p(&hdr
.size
);
284 hdr
.tag
= lduw_le_p(&hdr
.tag
);
286 g_assert_cmpint(hdr
.size
, >=, 7);
287 g_assert_cmpint(hdr
.size
, <=, P9_MAX_SIZE
);
288 g_assert_cmpint(hdr
.tag
, ==, req
->tag
);
291 g_printerr("Received response %d (%s) instead of %d (%s)\n",
292 hdr
.id
, rmessage_name(hdr
.id
), id
, rmessage_name(id
));
294 if (hdr
.id
== P9_RLERROR
) {
296 v9fs_uint32_read(req
, &err
);
297 g_printerr("Rlerror has errno %d (%s)\n", err
, strerror(err
));
300 g_assert_cmpint(hdr
.id
, ==, id
);
303 static void v9fs_req_free(P9Req
*req
)
305 guest_free(alloc
, req
->t_msg
);
306 guest_free(alloc
, req
->r_msg
);
310 /* size[4] Rlerror tag[2] ecode[4] */
311 static void v9fs_rlerror(P9Req
*req
, uint32_t *err
)
313 v9fs_req_recv(req
, P9_RLERROR
);
314 v9fs_uint32_read(req
, err
);
318 /* size[4] Tversion tag[2] msize[4] version[s] */
319 static P9Req
*v9fs_tversion(QVirtio9P
*v9p
, uint32_t msize
, const char *version
,
323 uint32_t body_size
= 4;
324 uint16_t string_size
= v9fs_string_size(version
);
326 g_assert_cmpint(body_size
, <=, UINT32_MAX
- string_size
);
327 body_size
+= string_size
;
328 req
= v9fs_req_init(v9p
, body_size
, P9_TVERSION
, tag
);
330 v9fs_uint32_write(req
, msize
);
331 v9fs_string_write(req
, version
);
336 /* size[4] Rversion tag[2] msize[4] version[s] */
337 static void v9fs_rversion(P9Req
*req
, uint16_t *len
, char **version
)
341 v9fs_req_recv(req
, P9_RVERSION
);
342 v9fs_uint32_read(req
, &msize
);
344 g_assert_cmpint(msize
, ==, P9_MAX_SIZE
);
346 if (len
|| version
) {
347 v9fs_string_read(req
, len
, version
);
353 /* size[4] Tattach tag[2] fid[4] afid[4] uname[s] aname[s] n_uname[4] */
354 static P9Req
*v9fs_tattach(QVirtio9P
*v9p
, uint32_t fid
, uint32_t n_uname
,
357 const char *uname
= ""; /* ignored by QEMU */
358 const char *aname
= ""; /* ignored by QEMU */
359 P9Req
*req
= v9fs_req_init(v9p
, 4 + 4 + 2 + 2 + 4, P9_TATTACH
, tag
);
361 v9fs_uint32_write(req
, fid
);
362 v9fs_uint32_write(req
, P9_NOFID
);
363 v9fs_string_write(req
, uname
);
364 v9fs_string_write(req
, aname
);
365 v9fs_uint32_write(req
, n_uname
);
370 typedef char v9fs_qid
[13];
372 /* size[4] Rattach tag[2] qid[13] */
373 static void v9fs_rattach(P9Req
*req
, v9fs_qid
*qid
)
375 v9fs_req_recv(req
, P9_RATTACH
);
377 v9fs_memread(req
, qid
, 13);
382 /* size[4] Twalk tag[2] fid[4] newfid[4] nwname[2] nwname*(wname[s]) */
383 static P9Req
*v9fs_twalk(QVirtio9P
*v9p
, uint32_t fid
, uint32_t newfid
,
384 uint16_t nwname
, char *const wnames
[], uint16_t tag
)
388 uint32_t body_size
= 4 + 4 + 2;
390 for (i
= 0; i
< nwname
; i
++) {
391 uint16_t wname_size
= v9fs_string_size(wnames
[i
]);
393 g_assert_cmpint(body_size
, <=, UINT32_MAX
- wname_size
);
394 body_size
+= wname_size
;
396 req
= v9fs_req_init(v9p
, body_size
, P9_TWALK
, tag
);
397 v9fs_uint32_write(req
, fid
);
398 v9fs_uint32_write(req
, newfid
);
399 v9fs_uint16_write(req
, nwname
);
400 for (i
= 0; i
< nwname
; i
++) {
401 v9fs_string_write(req
, wnames
[i
]);
407 /* size[4] Rwalk tag[2] nwqid[2] nwqid*(wqid[13]) */
408 static void v9fs_rwalk(P9Req
*req
, uint16_t *nwqid
, v9fs_qid
**wqid
)
410 uint16_t local_nwqid
;
412 v9fs_req_recv(req
, P9_RWALK
);
413 v9fs_uint16_read(req
, &local_nwqid
);
415 *nwqid
= local_nwqid
;
418 *wqid
= g_malloc(local_nwqid
* 13);
419 v9fs_memread(req
, *wqid
, local_nwqid
* 13);
424 /* size[4] Treaddir tag[2] fid[4] offset[8] count[4] */
425 static P9Req
*v9fs_treaddir(QVirtio9P
*v9p
, uint32_t fid
, uint64_t offset
,
426 uint32_t count
, uint16_t tag
)
430 req
= v9fs_req_init(v9p
, 4 + 8 + 4, P9_TREADDIR
, tag
);
431 v9fs_uint32_write(req
, fid
);
432 v9fs_uint64_write(req
, offset
);
433 v9fs_uint32_write(req
, count
);
443 struct V9fsDirent
*next
;
446 /* size[4] Rreaddir tag[2] count[4] data[count] */
447 static void v9fs_rreaddir(P9Req
*req
, uint32_t *count
, uint32_t *nentries
,
448 struct V9fsDirent
**entries
)
450 uint32_t local_count
;
451 struct V9fsDirent
*e
= NULL
;
455 v9fs_req_recv(req
, P9_RREADDIR
);
456 v9fs_uint32_read(req
, &local_count
);
459 *count
= local_count
;
462 for (int32_t togo
= (int32_t)local_count
;
463 togo
>= 13 + 8 + 1 + 2;
464 togo
-= 13 + 8 + 1 + 2 + slen
, ++n
)
467 e
= g_malloc(sizeof(struct V9fsDirent
));
472 e
= e
->next
= g_malloc(sizeof(struct V9fsDirent
));
475 /* qid[13] offset[8] type[1] name[s] */
476 v9fs_memread(req
, &e
->qid
, 13);
477 v9fs_uint64_read(req
, &e
->offset
);
478 v9fs_uint8_read(req
, &e
->type
);
479 v9fs_string_read(req
, &slen
, &e
->name
);
489 static void v9fs_free_dirents(struct V9fsDirent
*e
)
491 struct V9fsDirent
*next
= NULL
;
493 for (; e
; e
= next
) {
500 /* size[4] Tlopen tag[2] fid[4] flags[4] */
501 static P9Req
*v9fs_tlopen(QVirtio9P
*v9p
, uint32_t fid
, uint32_t flags
,
506 req
= v9fs_req_init(v9p
, 4 + 4, P9_TLOPEN
, tag
);
507 v9fs_uint32_write(req
, fid
);
508 v9fs_uint32_write(req
, flags
);
513 /* size[4] Rlopen tag[2] qid[13] iounit[4] */
514 static void v9fs_rlopen(P9Req
*req
, v9fs_qid
*qid
, uint32_t *iounit
)
516 v9fs_req_recv(req
, P9_RLOPEN
);
518 v9fs_memread(req
, qid
, 13);
520 v9fs_memskip(req
, 13);
523 v9fs_uint32_read(req
, iounit
);
528 /* size[4] Twrite tag[2] fid[4] offset[8] count[4] data[count] */
529 static P9Req
*v9fs_twrite(QVirtio9P
*v9p
, uint32_t fid
, uint64_t offset
,
530 uint32_t count
, const void *data
, uint16_t tag
)
533 uint32_t body_size
= 4 + 8 + 4;
535 g_assert_cmpint(body_size
, <=, UINT32_MAX
- count
);
537 req
= v9fs_req_init(v9p
, body_size
, P9_TWRITE
, tag
);
538 v9fs_uint32_write(req
, fid
);
539 v9fs_uint64_write(req
, offset
);
540 v9fs_uint32_write(req
, count
);
541 v9fs_memwrite(req
, data
, count
);
546 /* size[4] Rwrite tag[2] count[4] */
547 static void v9fs_rwrite(P9Req
*req
, uint32_t *count
)
549 v9fs_req_recv(req
, P9_RWRITE
);
551 v9fs_uint32_read(req
, count
);
556 /* size[4] Tflush tag[2] oldtag[2] */
557 static P9Req
*v9fs_tflush(QVirtio9P
*v9p
, uint16_t oldtag
, uint16_t tag
)
561 req
= v9fs_req_init(v9p
, 2, P9_TFLUSH
, tag
);
562 v9fs_uint32_write(req
, oldtag
);
567 /* size[4] Rflush tag[2] */
568 static void v9fs_rflush(P9Req
*req
)
570 v9fs_req_recv(req
, P9_RFLUSH
);
574 static void do_version(QVirtio9P
*v9p
)
576 const char *version
= "9P2000.L";
578 char *server_version
;
581 req
= v9fs_tversion(v9p
, P9_MAX_SIZE
, version
, P9_NOTAG
);
582 v9fs_req_wait_for_reply(req
, NULL
);
583 v9fs_rversion(req
, &server_len
, &server_version
);
585 g_assert_cmpmem(server_version
, server_len
, version
, strlen(version
));
587 g_free(server_version
);
590 /* utility function: walk to requested dir and return fid for that dir */
591 static uint32_t do_walk(QVirtio9P
*v9p
, const char *path
)
595 const uint32_t fid
= genfid();
597 int nwnames
= split(path
, "/", &wnames
);
599 req
= v9fs_twalk(v9p
, 0, fid
, nwnames
, wnames
, 0);
600 v9fs_req_wait_for_reply(req
, NULL
);
601 v9fs_rwalk(req
, NULL
, NULL
);
607 static void fs_version(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
613 static void do_attach(QVirtio9P
*v9p
)
618 req
= v9fs_tattach(v9p
, 0, getuid(), 0);
619 v9fs_req_wait_for_reply(req
, NULL
);
620 v9fs_rattach(req
, NULL
);
623 static void fs_attach(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
629 static void fs_walk(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
631 QVirtio9P
*v9p
= obj
;
633 char *wnames
[P9_MAXWELEM
];
639 for (i
= 0; i
< P9_MAXWELEM
; i
++) {
640 wnames
[i
] = g_strdup_printf(QTEST_V9FS_SYNTH_WALK_FILE
, i
);
644 req
= v9fs_twalk(v9p
, 0, 1, P9_MAXWELEM
, wnames
, 0);
645 v9fs_req_wait_for_reply(req
, NULL
);
646 v9fs_rwalk(req
, &nwqid
, &wqid
);
648 g_assert_cmpint(nwqid
, ==, P9_MAXWELEM
);
650 for (i
= 0; i
< P9_MAXWELEM
; i
++) {
657 static bool fs_dirents_contain_name(struct V9fsDirent
*e
, const char* name
)
659 for (; e
; e
= e
->next
) {
660 if (!strcmp(e
->name
, name
)) {
667 /* size[4] Tmkdir tag[2] dfid[4] name[s] mode[4] gid[4] */
668 static P9Req
*v9fs_tmkdir(QVirtio9P
*v9p
, uint32_t dfid
, const char *name
,
669 uint32_t mode
, uint32_t gid
, uint16_t tag
)
673 uint32_t body_size
= 4 + 4 + 4;
674 uint16_t string_size
= v9fs_string_size(name
);
676 g_assert_cmpint(body_size
, <=, UINT32_MAX
- string_size
);
677 body_size
+= string_size
;
679 req
= v9fs_req_init(v9p
, body_size
, P9_TMKDIR
, tag
);
680 v9fs_uint32_write(req
, dfid
);
681 v9fs_string_write(req
, name
);
682 v9fs_uint32_write(req
, mode
);
683 v9fs_uint32_write(req
, gid
);
688 /* size[4] Rmkdir tag[2] qid[13] */
689 static void v9fs_rmkdir(P9Req
*req
, v9fs_qid
*qid
)
691 v9fs_req_recv(req
, P9_RMKDIR
);
693 v9fs_memread(req
, qid
, 13);
695 v9fs_memskip(req
, 13);
700 /* size[4] Tlcreate tag[2] fid[4] name[s] flags[4] mode[4] gid[4] */
701 static P9Req
*v9fs_tlcreate(QVirtio9P
*v9p
, uint32_t fid
, const char *name
,
702 uint32_t flags
, uint32_t mode
, uint32_t gid
,
707 uint32_t body_size
= 4 + 4 + 4 + 4;
708 uint16_t string_size
= v9fs_string_size(name
);
710 g_assert_cmpint(body_size
, <=, UINT32_MAX
- string_size
);
711 body_size
+= string_size
;
713 req
= v9fs_req_init(v9p
, body_size
, P9_TLCREATE
, tag
);
714 v9fs_uint32_write(req
, fid
);
715 v9fs_string_write(req
, name
);
716 v9fs_uint32_write(req
, flags
);
717 v9fs_uint32_write(req
, mode
);
718 v9fs_uint32_write(req
, gid
);
723 /* size[4] Rlcreate tag[2] qid[13] iounit[4] */
724 static void v9fs_rlcreate(P9Req
*req
, v9fs_qid
*qid
, uint32_t *iounit
)
726 v9fs_req_recv(req
, P9_RLCREATE
);
728 v9fs_memread(req
, qid
, 13);
730 v9fs_memskip(req
, 13);
733 v9fs_uint32_read(req
, iounit
);
738 /* size[4] Tsymlink tag[2] fid[4] name[s] symtgt[s] gid[4] */
739 static P9Req
*v9fs_tsymlink(QVirtio9P
*v9p
, uint32_t fid
, const char *name
,
740 const char *symtgt
, uint32_t gid
, uint16_t tag
)
744 uint32_t body_size
= 4 + 4;
745 uint16_t string_size
= v9fs_string_size(name
) + v9fs_string_size(symtgt
);
747 g_assert_cmpint(body_size
, <=, UINT32_MAX
- string_size
);
748 body_size
+= string_size
;
750 req
= v9fs_req_init(v9p
, body_size
, P9_TSYMLINK
, tag
);
751 v9fs_uint32_write(req
, fid
);
752 v9fs_string_write(req
, name
);
753 v9fs_string_write(req
, symtgt
);
754 v9fs_uint32_write(req
, gid
);
759 /* size[4] Rsymlink tag[2] qid[13] */
760 static void v9fs_rsymlink(P9Req
*req
, v9fs_qid
*qid
)
762 v9fs_req_recv(req
, P9_RSYMLINK
);
764 v9fs_memread(req
, qid
, 13);
766 v9fs_memskip(req
, 13);
771 /* size[4] Tlink tag[2] dfid[4] fid[4] name[s] */
772 static P9Req
*v9fs_tlink(QVirtio9P
*v9p
, uint32_t dfid
, uint32_t fid
,
773 const char *name
, uint16_t tag
)
777 uint32_t body_size
= 4 + 4;
778 uint16_t string_size
= v9fs_string_size(name
);
780 g_assert_cmpint(body_size
, <=, UINT32_MAX
- string_size
);
781 body_size
+= string_size
;
783 req
= v9fs_req_init(v9p
, body_size
, P9_TLINK
, tag
);
784 v9fs_uint32_write(req
, dfid
);
785 v9fs_uint32_write(req
, fid
);
786 v9fs_string_write(req
, name
);
791 /* size[4] Rlink tag[2] */
792 static void v9fs_rlink(P9Req
*req
)
794 v9fs_req_recv(req
, P9_RLINK
);
798 /* size[4] Tunlinkat tag[2] dirfd[4] name[s] flags[4] */
799 static P9Req
*v9fs_tunlinkat(QVirtio9P
*v9p
, uint32_t dirfd
, const char *name
,
800 uint32_t flags
, uint16_t tag
)
804 uint32_t body_size
= 4 + 4;
805 uint16_t string_size
= v9fs_string_size(name
);
807 g_assert_cmpint(body_size
, <=, UINT32_MAX
- string_size
);
808 body_size
+= string_size
;
810 req
= v9fs_req_init(v9p
, body_size
, P9_TUNLINKAT
, tag
);
811 v9fs_uint32_write(req
, dirfd
);
812 v9fs_string_write(req
, name
);
813 v9fs_uint32_write(req
, flags
);
818 /* size[4] Runlinkat tag[2] */
819 static void v9fs_runlinkat(P9Req
*req
)
821 v9fs_req_recv(req
, P9_RUNLINKAT
);
825 /* basic readdir test where reply fits into a single response message */
826 static void fs_readdir(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
828 QVirtio9P
*v9p
= obj
;
830 char *const wnames
[] = { g_strdup(QTEST_V9FS_SYNTH_READDIR_DIR
) };
833 uint32_t count
, nentries
;
834 struct V9fsDirent
*entries
= NULL
;
838 req
= v9fs_twalk(v9p
, 0, 1, 1, wnames
, 0);
839 v9fs_req_wait_for_reply(req
, NULL
);
840 v9fs_rwalk(req
, &nqid
, NULL
);
841 g_assert_cmpint(nqid
, ==, 1);
843 req
= v9fs_tlopen(v9p
, 1, O_DIRECTORY
, 0);
844 v9fs_req_wait_for_reply(req
, NULL
);
845 v9fs_rlopen(req
, &qid
, NULL
);
848 * submit count = msize - 11, because 11 is the header size of Rreaddir
850 req
= v9fs_treaddir(v9p
, 1, 0, P9_MAX_SIZE
- 11, 0);
851 v9fs_req_wait_for_reply(req
, NULL
);
852 v9fs_rreaddir(req
, &count
, &nentries
, &entries
);
855 * Assuming msize (P9_MAX_SIZE) is large enough so we can retrieve all
856 * dir entries with only one readdir request.
860 QTEST_V9FS_SYNTH_READDIR_NFILES
+ 2 /* "." and ".." */
864 * Check all file names exist in returned entries, ignore their order
867 g_assert_cmpint(fs_dirents_contain_name(entries
, "."), ==, true);
868 g_assert_cmpint(fs_dirents_contain_name(entries
, ".."), ==, true);
869 for (int i
= 0; i
< QTEST_V9FS_SYNTH_READDIR_NFILES
; ++i
) {
870 char *name
= g_strdup_printf(QTEST_V9FS_SYNTH_READDIR_FILE
, i
);
871 g_assert_cmpint(fs_dirents_contain_name(entries
, name
), ==, true);
875 v9fs_free_dirents(entries
);
879 /* readdir test where overall request is split over several messages */
880 static void do_readdir_split(QVirtio9P
*v9p
, uint32_t count
)
882 char *const wnames
[] = { g_strdup(QTEST_V9FS_SYNTH_READDIR_DIR
) };
885 uint32_t nentries
, npartialentries
;
886 struct V9fsDirent
*entries
, *tail
, *partialentries
;
899 req
= v9fs_twalk(v9p
, 0, fid
, 1, wnames
, 0);
900 v9fs_req_wait_for_reply(req
, NULL
);
901 v9fs_rwalk(req
, &nqid
, NULL
);
902 g_assert_cmpint(nqid
, ==, 1);
904 req
= v9fs_tlopen(v9p
, fid
, O_DIRECTORY
, 0);
905 v9fs_req_wait_for_reply(req
, NULL
);
906 v9fs_rlopen(req
, &qid
, NULL
);
909 * send as many Treaddir requests as required to get all directory
914 partialentries
= NULL
;
916 req
= v9fs_treaddir(v9p
, fid
, offset
, count
, 0);
917 v9fs_req_wait_for_reply(req
, NULL
);
918 v9fs_rreaddir(req
, &count
, &npartialentries
, &partialentries
);
919 if (npartialentries
> 0 && partialentries
) {
921 entries
= partialentries
;
922 nentries
= npartialentries
;
923 tail
= partialentries
;
925 tail
->next
= partialentries
;
926 nentries
+= npartialentries
;
931 offset
= tail
->offset
;
939 QTEST_V9FS_SYNTH_READDIR_NFILES
+ 2 /* "." and ".." */
943 * Check all file names exist in returned entries, ignore their order
946 g_assert_cmpint(fs_dirents_contain_name(entries
, "."), ==, true);
947 g_assert_cmpint(fs_dirents_contain_name(entries
, ".."), ==, true);
948 for (int i
= 0; i
< QTEST_V9FS_SYNTH_READDIR_NFILES
; ++i
) {
949 char *name
= g_strdup_printf(QTEST_V9FS_SYNTH_READDIR_FILE
, i
);
950 g_assert_cmpint(fs_dirents_contain_name(entries
, name
), ==, true);
954 v9fs_free_dirents(entries
);
959 static void fs_walk_no_slash(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
961 QVirtio9P
*v9p
= obj
;
963 char *const wnames
[] = { g_strdup(" /") };
968 req
= v9fs_twalk(v9p
, 0, 1, 1, wnames
, 0);
969 v9fs_req_wait_for_reply(req
, NULL
);
970 v9fs_rlerror(req
, &err
);
972 g_assert_cmpint(err
, ==, ENOENT
);
977 static void fs_walk_dotdot(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
979 QVirtio9P
*v9p
= obj
;
981 char *const wnames
[] = { g_strdup("..") };
982 v9fs_qid root_qid
, *wqid
;
986 req
= v9fs_tattach(v9p
, 0, getuid(), 0);
987 v9fs_req_wait_for_reply(req
, NULL
);
988 v9fs_rattach(req
, &root_qid
);
990 req
= v9fs_twalk(v9p
, 0, 1, 1, wnames
, 0);
991 v9fs_req_wait_for_reply(req
, NULL
);
992 v9fs_rwalk(req
, NULL
, &wqid
); /* We now we'll get one qid */
994 g_assert_cmpmem(&root_qid
, 13, wqid
[0], 13);
1000 static void fs_lopen(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
1002 QVirtio9P
*v9p
= obj
;
1004 char *const wnames
[] = { g_strdup(QTEST_V9FS_SYNTH_LOPEN_FILE
) };
1008 req
= v9fs_twalk(v9p
, 0, 1, 1, wnames
, 0);
1009 v9fs_req_wait_for_reply(req
, NULL
);
1010 v9fs_rwalk(req
, NULL
, NULL
);
1012 req
= v9fs_tlopen(v9p
, 1, O_WRONLY
, 0);
1013 v9fs_req_wait_for_reply(req
, NULL
);
1014 v9fs_rlopen(req
, NULL
, NULL
);
1019 static void fs_write(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
1021 QVirtio9P
*v9p
= obj
;
1023 static const uint32_t write_count
= P9_MAX_SIZE
/ 2;
1024 char *const wnames
[] = { g_strdup(QTEST_V9FS_SYNTH_WRITE_FILE
) };
1025 char *buf
= g_malloc0(write_count
);
1030 req
= v9fs_twalk(v9p
, 0, 1, 1, wnames
, 0);
1031 v9fs_req_wait_for_reply(req
, NULL
);
1032 v9fs_rwalk(req
, NULL
, NULL
);
1034 req
= v9fs_tlopen(v9p
, 1, O_WRONLY
, 0);
1035 v9fs_req_wait_for_reply(req
, NULL
);
1036 v9fs_rlopen(req
, NULL
, NULL
);
1038 req
= v9fs_twrite(v9p
, 1, 0, write_count
, buf
, 0);
1039 v9fs_req_wait_for_reply(req
, NULL
);
1040 v9fs_rwrite(req
, &count
);
1041 g_assert_cmpint(count
, ==, write_count
);
1047 static void fs_flush_success(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
1049 QVirtio9P
*v9p
= obj
;
1051 char *const wnames
[] = { g_strdup(QTEST_V9FS_SYNTH_FLUSH_FILE
) };
1052 P9Req
*req
, *flush_req
;
1054 uint8_t should_block
;
1057 req
= v9fs_twalk(v9p
, 0, 1, 1, wnames
, 0);
1058 v9fs_req_wait_for_reply(req
, NULL
);
1059 v9fs_rwalk(req
, NULL
, NULL
);
1061 req
= v9fs_tlopen(v9p
, 1, O_WRONLY
, 0);
1062 v9fs_req_wait_for_reply(req
, NULL
);
1063 v9fs_rlopen(req
, NULL
, NULL
);
1065 /* This will cause the 9p server to try to write data to the backend,
1066 * until the write request gets cancelled.
1069 req
= v9fs_twrite(v9p
, 1, 0, sizeof(should_block
), &should_block
, 0);
1071 flush_req
= v9fs_tflush(v9p
, req
->tag
, 1);
1073 /* The write request is supposed to be flushed: the server should just
1074 * mark the write request as used and reply to the flush request.
1076 v9fs_req_wait_for_reply(req
, &reply_len
);
1077 g_assert_cmpint(reply_len
, ==, 0);
1079 v9fs_rflush(flush_req
);
1084 static void fs_flush_ignored(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
1086 QVirtio9P
*v9p
= obj
;
1088 char *const wnames
[] = { g_strdup(QTEST_V9FS_SYNTH_FLUSH_FILE
) };
1089 P9Req
*req
, *flush_req
;
1091 uint8_t should_block
;
1094 req
= v9fs_twalk(v9p
, 0, 1, 1, wnames
, 0);
1095 v9fs_req_wait_for_reply(req
, NULL
);
1096 v9fs_rwalk(req
, NULL
, NULL
);
1098 req
= v9fs_tlopen(v9p
, 1, O_WRONLY
, 0);
1099 v9fs_req_wait_for_reply(req
, NULL
);
1100 v9fs_rlopen(req
, NULL
, NULL
);
1102 /* This will cause the write request to complete right away, before it
1103 * could be actually cancelled.
1106 req
= v9fs_twrite(v9p
, 1, 0, sizeof(should_block
), &should_block
, 0);
1108 flush_req
= v9fs_tflush(v9p
, req
->tag
, 1);
1110 /* The write request is supposed to complete. The server should
1111 * reply to the write request and the flush request.
1113 v9fs_req_wait_for_reply(req
, NULL
);
1114 v9fs_rwrite(req
, &count
);
1115 g_assert_cmpint(count
, ==, sizeof(should_block
));
1116 v9fs_rflush(flush_req
);
1121 static void do_mkdir(QVirtio9P
*v9p
, const char *path
, const char *cname
)
1123 char *const name
= g_strdup(cname
);
1127 fid
= do_walk(v9p
, path
);
1129 req
= v9fs_tmkdir(v9p
, fid
, name
, 0750, 0, 0);
1130 v9fs_req_wait_for_reply(req
, NULL
);
1131 v9fs_rmkdir(req
, NULL
);
1136 /* create a regular file with Tlcreate and return file's fid */
1137 static uint32_t do_lcreate(QVirtio9P
*v9p
, const char *path
,
1140 char *const name
= g_strdup(cname
);
1144 fid
= do_walk(v9p
, path
);
1146 req
= v9fs_tlcreate(v9p
, fid
, name
, 0, 0750, 0, 0);
1147 v9fs_req_wait_for_reply(req
, NULL
);
1148 v9fs_rlcreate(req
, NULL
, NULL
);
1154 /* create symlink named @a clink in directory @a path pointing to @a to */
1155 static void do_symlink(QVirtio9P
*v9p
, const char *path
, const char *clink
,
1158 char *const name
= g_strdup(clink
);
1159 char *const dst
= g_strdup(to
);
1163 fid
= do_walk(v9p
, path
);
1165 req
= v9fs_tsymlink(v9p
, fid
, name
, dst
, 0, 0);
1166 v9fs_req_wait_for_reply(req
, NULL
);
1167 v9fs_rsymlink(req
, NULL
);
1173 /* create a hard link named @a clink in directory @a path pointing to @a to */
1174 static void do_hardlink(QVirtio9P
*v9p
, const char *path
, const char *clink
,
1180 dfid
= do_walk(v9p
, path
);
1181 fid
= do_walk(v9p
, to
);
1183 req
= v9fs_tlink(v9p
, dfid
, fid
, clink
, 0);
1184 v9fs_req_wait_for_reply(req
, NULL
);
1188 static void do_unlinkat(QVirtio9P
*v9p
, const char *atpath
, const char *rpath
,
1191 char *const name
= g_strdup(rpath
);
1195 fid
= do_walk(v9p
, atpath
);
1197 req
= v9fs_tunlinkat(v9p
, fid
, name
, flags
, 0);
1198 v9fs_req_wait_for_reply(req
, NULL
);
1199 v9fs_runlinkat(req
);
1204 static void fs_readdir_split_128(void *obj
, void *data
,
1205 QGuestAllocator
*t_alloc
)
1208 do_readdir_split(obj
, 128);
1211 static void fs_readdir_split_256(void *obj
, void *data
,
1212 QGuestAllocator
*t_alloc
)
1215 do_readdir_split(obj
, 256);
1218 static void fs_readdir_split_512(void *obj
, void *data
,
1219 QGuestAllocator
*t_alloc
)
1222 do_readdir_split(obj
, 512);
1226 /* tests using the 9pfs 'local' fs driver */
1228 static void fs_create_dir(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
1230 QVirtio9P
*v9p
= obj
;
1233 char *root_path
= virtio_9p_test_path("");
1234 char *new_dir
= virtio_9p_test_path("01");
1236 g_assert(root_path
!= NULL
);
1239 do_mkdir(v9p
, "/", "01");
1241 /* check if created directory really exists now ... */
1242 g_assert(stat(new_dir
, &st
) == 0);
1243 /* ... and is actually a directory */
1244 g_assert((st
.st_mode
& S_IFMT
) == S_IFDIR
);
1250 static void fs_unlinkat_dir(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
1252 QVirtio9P
*v9p
= obj
;
1255 char *root_path
= virtio_9p_test_path("");
1256 char *new_dir
= virtio_9p_test_path("02");
1258 g_assert(root_path
!= NULL
);
1261 do_mkdir(v9p
, "/", "02");
1263 /* check if created directory really exists now ... */
1264 g_assert(stat(new_dir
, &st
) == 0);
1265 /* ... and is actually a directory */
1266 g_assert((st
.st_mode
& S_IFMT
) == S_IFDIR
);
1268 do_unlinkat(v9p
, "/", "02", AT_REMOVEDIR
);
1269 /* directory should be gone now */
1270 g_assert(stat(new_dir
, &st
) != 0);
1276 static void fs_create_file(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
1278 QVirtio9P
*v9p
= obj
;
1281 char *new_file
= virtio_9p_test_path("03/1st_file");
1284 do_mkdir(v9p
, "/", "03");
1285 do_lcreate(v9p
, "03", "1st_file");
1287 /* check if created file exists now ... */
1288 g_assert(stat(new_file
, &st
) == 0);
1289 /* ... and is a regular file */
1290 g_assert((st
.st_mode
& S_IFMT
) == S_IFREG
);
1295 static void fs_unlinkat_file(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
1297 QVirtio9P
*v9p
= obj
;
1300 char *new_file
= virtio_9p_test_path("04/doa_file");
1303 do_mkdir(v9p
, "/", "04");
1304 do_lcreate(v9p
, "04", "doa_file");
1306 /* check if created file exists now ... */
1307 g_assert(stat(new_file
, &st
) == 0);
1308 /* ... and is a regular file */
1309 g_assert((st
.st_mode
& S_IFMT
) == S_IFREG
);
1311 do_unlinkat(v9p
, "04", "doa_file", 0);
1312 /* file should be gone now */
1313 g_assert(stat(new_file
, &st
) != 0);
1318 static void fs_symlink_file(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
1320 QVirtio9P
*v9p
= obj
;
1323 char *real_file
= virtio_9p_test_path("05/real_file");
1324 char *symlink_file
= virtio_9p_test_path("05/symlink_file");
1327 do_mkdir(v9p
, "/", "05");
1328 do_lcreate(v9p
, "05", "real_file");
1329 g_assert(stat(real_file
, &st
) == 0);
1330 g_assert((st
.st_mode
& S_IFMT
) == S_IFREG
);
1332 do_symlink(v9p
, "05", "symlink_file", "real_file");
1334 /* check if created link exists now */
1335 g_assert(stat(symlink_file
, &st
) == 0);
1337 g_free(symlink_file
);
1341 static void fs_unlinkat_symlink(void *obj
, void *data
,
1342 QGuestAllocator
*t_alloc
)
1344 QVirtio9P
*v9p
= obj
;
1347 char *real_file
= virtio_9p_test_path("06/real_file");
1348 char *symlink_file
= virtio_9p_test_path("06/symlink_file");
1351 do_mkdir(v9p
, "/", "06");
1352 do_lcreate(v9p
, "06", "real_file");
1353 g_assert(stat(real_file
, &st
) == 0);
1354 g_assert((st
.st_mode
& S_IFMT
) == S_IFREG
);
1356 do_symlink(v9p
, "06", "symlink_file", "real_file");
1357 g_assert(stat(symlink_file
, &st
) == 0);
1359 do_unlinkat(v9p
, "06", "symlink_file", 0);
1360 /* symlink should be gone now */
1361 g_assert(stat(symlink_file
, &st
) != 0);
1363 g_free(symlink_file
);
1367 static void fs_hardlink_file(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
1369 QVirtio9P
*v9p
= obj
;
1371 struct stat st_real
, st_link
;
1372 char *real_file
= virtio_9p_test_path("07/real_file");
1373 char *hardlink_file
= virtio_9p_test_path("07/hardlink_file");
1376 do_mkdir(v9p
, "/", "07");
1377 do_lcreate(v9p
, "07", "real_file");
1378 g_assert(stat(real_file
, &st_real
) == 0);
1379 g_assert((st_real
.st_mode
& S_IFMT
) == S_IFREG
);
1381 do_hardlink(v9p
, "07", "hardlink_file", "07/real_file");
1383 /* check if link exists now ... */
1384 g_assert(stat(hardlink_file
, &st_link
) == 0);
1385 /* ... and it's a hard link, right? */
1386 g_assert((st_link
.st_mode
& S_IFMT
) == S_IFREG
);
1387 g_assert(st_link
.st_dev
== st_real
.st_dev
);
1388 g_assert(st_link
.st_ino
== st_real
.st_ino
);
1390 g_free(hardlink_file
);
1394 static void fs_unlinkat_hardlink(void *obj
, void *data
,
1395 QGuestAllocator
*t_alloc
)
1397 QVirtio9P
*v9p
= obj
;
1399 struct stat st_real
, st_link
;
1400 char *real_file
= virtio_9p_test_path("08/real_file");
1401 char *hardlink_file
= virtio_9p_test_path("08/hardlink_file");
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
);
1409 do_hardlink(v9p
, "08", "hardlink_file", "08/real_file");
1410 g_assert(stat(hardlink_file
, &st_link
) == 0);
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);
1418 g_free(hardlink_file
);
1422 static void *assign_9p_local_driver(GString
*cmd_line
, void *arg
)
1424 virtio_9p_assign_local_driver(cmd_line
, "security_model=mapped-xattr");
1428 static void register_virtio_9p_test(void)
1431 QOSGraphTestOptions opts
= {
1434 /* 9pfs test cases using the 'synth' filesystem driver */
1435 qos_add_test("synth/config", "virtio-9p", pci_config
, &opts
);
1436 qos_add_test("synth/version/basic", "virtio-9p", fs_version
, &opts
);
1437 qos_add_test("synth/attach/basic", "virtio-9p", fs_attach
, &opts
);
1438 qos_add_test("synth/walk/basic", "virtio-9p", fs_walk
, &opts
);
1439 qos_add_test("synth/walk/no_slash", "virtio-9p", fs_walk_no_slash
,
1441 qos_add_test("synth/walk/dotdot_from_root", "virtio-9p",
1442 fs_walk_dotdot
, &opts
);
1443 qos_add_test("synth/lopen/basic", "virtio-9p", fs_lopen
, &opts
);
1444 qos_add_test("synth/write/basic", "virtio-9p", fs_write
, &opts
);
1445 qos_add_test("synth/flush/success", "virtio-9p", fs_flush_success
,
1447 qos_add_test("synth/flush/ignored", "virtio-9p", fs_flush_ignored
,
1449 qos_add_test("synth/readdir/basic", "virtio-9p", fs_readdir
, &opts
);
1450 qos_add_test("synth/readdir/split_512", "virtio-9p",
1451 fs_readdir_split_512
, &opts
);
1452 qos_add_test("synth/readdir/split_256", "virtio-9p",
1453 fs_readdir_split_256
, &opts
);
1454 qos_add_test("synth/readdir/split_128", "virtio-9p",
1455 fs_readdir_split_128
, &opts
);
1458 /* 9pfs test cases using the 'local' filesystem driver */
1461 * XXX: Until we are sure that these tests can run everywhere,
1462 * keep them as "slow" so that they aren't run with "make check".
1464 if (!g_test_slow()) {
1468 opts
.before
= assign_9p_local_driver
;
1469 qos_add_test("local/config", "virtio-9p", pci_config
, &opts
);
1470 qos_add_test("local/create_dir", "virtio-9p", fs_create_dir
, &opts
);
1471 qos_add_test("local/unlinkat_dir", "virtio-9p", fs_unlinkat_dir
, &opts
);
1472 qos_add_test("local/create_file", "virtio-9p", fs_create_file
, &opts
);
1473 qos_add_test("local/unlinkat_file", "virtio-9p", fs_unlinkat_file
, &opts
);
1474 qos_add_test("local/symlink_file", "virtio-9p", fs_symlink_file
, &opts
);
1475 qos_add_test("local/unlinkat_symlink", "virtio-9p", fs_unlinkat_symlink
,
1477 qos_add_test("local/hardlink_file", "virtio-9p", fs_hardlink_file
, &opts
);
1478 qos_add_test("local/unlinkat_hardlink", "virtio-9p", fs_unlinkat_hardlink
,
1482 libqos_init(register_virtio_9p_test
);
1484 static void __attribute__((constructor
)) construct_9p_test(void)
1486 /* make sure test dir for the 'local' tests exists */
1487 virtio_9p_create_local_test_dir();
1490 static void __attribute__((destructor
)) destruct_9p_test(void)
1492 /* remove previously created test dir when test suite completed */
1493 virtio_9p_remove_local_test_dir();