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.
11 * Not so fast! You might want to read the 9p developer docs first:
12 * https://wiki.qemu.org/Documentation/9p
15 #include "qemu/osdep.h"
16 #include "libqtest-single.h"
17 #include "qemu/module.h"
18 #include "hw/9pfs/9p.h"
19 #include "hw/9pfs/9p-synth.h"
20 #include "libqos/virtio-9p.h"
21 #include "libqos/qgraph.h"
23 #define QVIRTIO_9P_TIMEOUT_US (10 * 1000 * 1000)
24 static QGuestAllocator
*alloc
;
27 * Used to auto generate new fids. Start with arbitrary high value to avoid
28 * collision with hard coded fids in basic test code.
30 static uint32_t fid_generator
= 1000;
32 static uint32_t genfid(void)
34 return fid_generator
++;
38 * Splits the @a in string by @a delim into individual (non empty) strings
39 * and outputs them to @a out. The output array @a out is NULL terminated.
41 * Output array @a out must be freed by calling split_free().
43 * @returns number of individual elements in output array @a out (without the
44 * final NULL terminating element)
46 static int split(const char *in
, const char *delim
, char ***out
)
52 for (p
= strtok(tmp
, delim
); p
!= NULL
; p
= strtok(NULL
, delim
)) {
59 *out
= g_new0(char *, n
+ 1); /* last element NULL delimiter */
62 for (p
= strtok(tmp
, delim
); p
!= NULL
; p
= strtok(NULL
, delim
)) {
64 (*out
)[i
++] = g_strdup(p
);
72 static void split_free(char ***out
)
75 for (i
= 0; (*out
)[i
]; ++i
) {
82 static void pci_config(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
86 size_t tag_len
= qvirtio_config_readw(v9p
->vdev
, 0);
90 g_assert_cmpint(tag_len
, ==, strlen(MOUNT_TAG
));
92 tag
= g_malloc(tag_len
);
93 for (i
= 0; i
< tag_len
; i
++) {
94 tag
[i
] = qvirtio_config_readb(v9p
->vdev
, i
+ 2);
96 g_assert_cmpmem(tag
, tag_len
, MOUNT_TAG
, tag_len
);
100 #define P9_MAX_SIZE 4096 /* Max size of a T-message or R-message */
109 /* No r_size, it is hardcoded to P9_MAX_SIZE */
115 static void v9fs_memwrite(P9Req
*req
, const void *addr
, size_t len
)
117 qtest_memwrite(req
->qts
, req
->t_msg
+ req
->t_off
, addr
, len
);
121 static void v9fs_memskip(P9Req
*req
, size_t len
)
126 static void v9fs_memread(P9Req
*req
, void *addr
, size_t len
)
128 qtest_memread(req
->qts
, req
->r_msg
+ req
->r_off
, addr
, len
);
132 static void v9fs_uint8_read(P9Req
*req
, uint8_t *val
)
134 v9fs_memread(req
, val
, 1);
137 static void v9fs_uint16_write(P9Req
*req
, uint16_t val
)
139 uint16_t le_val
= cpu_to_le16(val
);
141 v9fs_memwrite(req
, &le_val
, 2);
144 static void v9fs_uint16_read(P9Req
*req
, uint16_t *val
)
146 v9fs_memread(req
, val
, 2);
150 static void v9fs_uint32_write(P9Req
*req
, uint32_t val
)
152 uint32_t le_val
= cpu_to_le32(val
);
154 v9fs_memwrite(req
, &le_val
, 4);
157 static void v9fs_uint64_write(P9Req
*req
, uint64_t val
)
159 uint64_t le_val
= cpu_to_le64(val
);
161 v9fs_memwrite(req
, &le_val
, 8);
164 static void v9fs_uint32_read(P9Req
*req
, uint32_t *val
)
166 v9fs_memread(req
, val
, 4);
170 static void v9fs_uint64_read(P9Req
*req
, uint64_t *val
)
172 v9fs_memread(req
, val
, 8);
176 /* len[2] string[len] */
177 static uint16_t v9fs_string_size(const char *string
)
179 size_t len
= strlen(string
);
181 g_assert_cmpint(len
, <=, UINT16_MAX
- 2);
186 static void v9fs_string_write(P9Req
*req
, const char *string
)
188 int len
= strlen(string
);
190 g_assert_cmpint(len
, <=, UINT16_MAX
);
192 v9fs_uint16_write(req
, (uint16_t) len
);
193 v9fs_memwrite(req
, string
, len
);
196 static void v9fs_string_read(P9Req
*req
, uint16_t *len
, char **string
)
200 v9fs_uint16_read(req
, &local_len
);
205 *string
= g_malloc(local_len
+ 1);
206 v9fs_memread(req
, *string
, local_len
);
207 (*string
)[local_len
] = 0;
209 v9fs_memskip(req
, local_len
);
219 static P9Req
*v9fs_req_init(QVirtio9P
*v9p
, uint32_t size
, uint8_t id
,
222 P9Req
*req
= g_new0(P9Req
, 1);
223 uint32_t total_size
= 7; /* 9P header has well-known size of 7 bytes */
226 .tag
= cpu_to_le16(tag
)
229 g_assert_cmpint(total_size
, <=, UINT32_MAX
- size
);
231 hdr
.size
= cpu_to_le32(total_size
);
233 g_assert_cmpint(total_size
, <=, P9_MAX_SIZE
);
235 req
->qts
= global_qtest
;
237 req
->t_size
= total_size
;
238 req
->t_msg
= guest_alloc(alloc
, req
->t_size
);
239 v9fs_memwrite(req
, &hdr
, 7);
244 static void v9fs_req_send(P9Req
*req
)
246 QVirtio9P
*v9p
= req
->v9p
;
248 req
->r_msg
= guest_alloc(alloc
, P9_MAX_SIZE
);
249 req
->free_head
= qvirtqueue_add(req
->qts
, v9p
->vq
, req
->t_msg
, req
->t_size
,
251 qvirtqueue_add(req
->qts
, v9p
->vq
, req
->r_msg
, P9_MAX_SIZE
, true, false);
252 qvirtqueue_kick(req
->qts
, v9p
->vdev
, v9p
->vq
, req
->free_head
);
256 static const char *rmessage_name(uint8_t id
)
259 id
== P9_RLERROR
? "RLERROR" :
260 id
== P9_RVERSION
? "RVERSION" :
261 id
== P9_RATTACH
? "RATTACH" :
262 id
== P9_RWALK
? "RWALK" :
263 id
== P9_RLOPEN
? "RLOPEN" :
264 id
== P9_RWRITE
? "RWRITE" :
265 id
== P9_RMKDIR
? "RMKDIR" :
266 id
== P9_RLCREATE
? "RLCREATE" :
267 id
== P9_RSYMLINK
? "RSYMLINK" :
268 id
== P9_RLINK
? "RLINK" :
269 id
== P9_RUNLINKAT
? "RUNLINKAT" :
270 id
== P9_RFLUSH
? "RFLUSH" :
271 id
== P9_RREADDIR
? "READDIR" :
275 static void v9fs_req_wait_for_reply(P9Req
*req
, uint32_t *len
)
277 QVirtio9P
*v9p
= req
->v9p
;
279 qvirtio_wait_used_elem(req
->qts
, v9p
->vdev
, v9p
->vq
, req
->free_head
, len
,
280 QVIRTIO_9P_TIMEOUT_US
);
283 static void v9fs_req_recv(P9Req
*req
, uint8_t id
)
287 v9fs_memread(req
, &hdr
, 7);
288 hdr
.size
= ldl_le_p(&hdr
.size
);
289 hdr
.tag
= lduw_le_p(&hdr
.tag
);
291 g_assert_cmpint(hdr
.size
, >=, 7);
292 g_assert_cmpint(hdr
.size
, <=, P9_MAX_SIZE
);
293 g_assert_cmpint(hdr
.tag
, ==, req
->tag
);
296 g_printerr("Received response %d (%s) instead of %d (%s)\n",
297 hdr
.id
, rmessage_name(hdr
.id
), id
, rmessage_name(id
));
299 if (hdr
.id
== P9_RLERROR
) {
301 v9fs_uint32_read(req
, &err
);
302 g_printerr("Rlerror has errno %d (%s)\n", err
, strerror(err
));
305 g_assert_cmpint(hdr
.id
, ==, id
);
308 static void v9fs_req_free(P9Req
*req
)
310 guest_free(alloc
, req
->t_msg
);
311 guest_free(alloc
, req
->r_msg
);
315 /* size[4] Rlerror tag[2] ecode[4] */
316 static void v9fs_rlerror(P9Req
*req
, uint32_t *err
)
318 v9fs_req_recv(req
, P9_RLERROR
);
319 v9fs_uint32_read(req
, err
);
323 /* size[4] Tversion tag[2] msize[4] version[s] */
324 static P9Req
*v9fs_tversion(QVirtio9P
*v9p
, uint32_t msize
, const char *version
,
328 uint32_t body_size
= 4;
329 uint16_t string_size
= v9fs_string_size(version
);
331 g_assert_cmpint(body_size
, <=, UINT32_MAX
- string_size
);
332 body_size
+= string_size
;
333 req
= v9fs_req_init(v9p
, body_size
, P9_TVERSION
, tag
);
335 v9fs_uint32_write(req
, msize
);
336 v9fs_string_write(req
, version
);
341 /* size[4] Rversion tag[2] msize[4] version[s] */
342 static void v9fs_rversion(P9Req
*req
, uint16_t *len
, char **version
)
346 v9fs_req_recv(req
, P9_RVERSION
);
347 v9fs_uint32_read(req
, &msize
);
349 g_assert_cmpint(msize
, ==, P9_MAX_SIZE
);
351 if (len
|| version
) {
352 v9fs_string_read(req
, len
, version
);
358 /* size[4] Tattach tag[2] fid[4] afid[4] uname[s] aname[s] n_uname[4] */
359 static P9Req
*v9fs_tattach(QVirtio9P
*v9p
, uint32_t fid
, uint32_t n_uname
,
362 const char *uname
= ""; /* ignored by QEMU */
363 const char *aname
= ""; /* ignored by QEMU */
364 P9Req
*req
= v9fs_req_init(v9p
, 4 + 4 + 2 + 2 + 4, P9_TATTACH
, tag
);
366 v9fs_uint32_write(req
, fid
);
367 v9fs_uint32_write(req
, P9_NOFID
);
368 v9fs_string_write(req
, uname
);
369 v9fs_string_write(req
, aname
);
370 v9fs_uint32_write(req
, n_uname
);
375 typedef char v9fs_qid
[13];
377 /* size[4] Rattach tag[2] qid[13] */
378 static void v9fs_rattach(P9Req
*req
, v9fs_qid
*qid
)
380 v9fs_req_recv(req
, P9_RATTACH
);
382 v9fs_memread(req
, qid
, 13);
387 /* size[4] Twalk tag[2] fid[4] newfid[4] nwname[2] nwname*(wname[s]) */
388 static P9Req
*v9fs_twalk(QVirtio9P
*v9p
, uint32_t fid
, uint32_t newfid
,
389 uint16_t nwname
, char *const wnames
[], uint16_t tag
)
393 uint32_t body_size
= 4 + 4 + 2;
395 for (i
= 0; i
< nwname
; i
++) {
396 uint16_t wname_size
= v9fs_string_size(wnames
[i
]);
398 g_assert_cmpint(body_size
, <=, UINT32_MAX
- wname_size
);
399 body_size
+= wname_size
;
401 req
= v9fs_req_init(v9p
, body_size
, P9_TWALK
, tag
);
402 v9fs_uint32_write(req
, fid
);
403 v9fs_uint32_write(req
, newfid
);
404 v9fs_uint16_write(req
, nwname
);
405 for (i
= 0; i
< nwname
; i
++) {
406 v9fs_string_write(req
, wnames
[i
]);
412 /* size[4] Rwalk tag[2] nwqid[2] nwqid*(wqid[13]) */
413 static void v9fs_rwalk(P9Req
*req
, uint16_t *nwqid
, v9fs_qid
**wqid
)
415 uint16_t local_nwqid
;
417 v9fs_req_recv(req
, P9_RWALK
);
418 v9fs_uint16_read(req
, &local_nwqid
);
420 *nwqid
= local_nwqid
;
423 *wqid
= g_malloc(local_nwqid
* 13);
424 v9fs_memread(req
, *wqid
, local_nwqid
* 13);
429 /* size[4] Treaddir tag[2] fid[4] offset[8] count[4] */
430 static P9Req
*v9fs_treaddir(QVirtio9P
*v9p
, uint32_t fid
, uint64_t offset
,
431 uint32_t count
, uint16_t tag
)
435 req
= v9fs_req_init(v9p
, 4 + 8 + 4, P9_TREADDIR
, tag
);
436 v9fs_uint32_write(req
, fid
);
437 v9fs_uint64_write(req
, offset
);
438 v9fs_uint32_write(req
, count
);
448 struct V9fsDirent
*next
;
451 /* size[4] Rreaddir tag[2] count[4] data[count] */
452 static void v9fs_rreaddir(P9Req
*req
, uint32_t *count
, uint32_t *nentries
,
453 struct V9fsDirent
**entries
)
455 uint32_t local_count
;
456 struct V9fsDirent
*e
= NULL
;
460 v9fs_req_recv(req
, P9_RREADDIR
);
461 v9fs_uint32_read(req
, &local_count
);
464 *count
= local_count
;
467 for (int32_t togo
= (int32_t)local_count
;
468 togo
>= 13 + 8 + 1 + 2;
469 togo
-= 13 + 8 + 1 + 2 + slen
, ++n
)
472 e
= g_malloc(sizeof(struct V9fsDirent
));
477 e
= e
->next
= g_malloc(sizeof(struct V9fsDirent
));
480 /* qid[13] offset[8] type[1] name[s] */
481 v9fs_memread(req
, &e
->qid
, 13);
482 v9fs_uint64_read(req
, &e
->offset
);
483 v9fs_uint8_read(req
, &e
->type
);
484 v9fs_string_read(req
, &slen
, &e
->name
);
494 static void v9fs_free_dirents(struct V9fsDirent
*e
)
496 struct V9fsDirent
*next
= NULL
;
498 for (; e
; e
= next
) {
505 /* size[4] Tlopen tag[2] fid[4] flags[4] */
506 static P9Req
*v9fs_tlopen(QVirtio9P
*v9p
, uint32_t fid
, uint32_t flags
,
511 req
= v9fs_req_init(v9p
, 4 + 4, P9_TLOPEN
, tag
);
512 v9fs_uint32_write(req
, fid
);
513 v9fs_uint32_write(req
, flags
);
518 /* size[4] Rlopen tag[2] qid[13] iounit[4] */
519 static void v9fs_rlopen(P9Req
*req
, v9fs_qid
*qid
, uint32_t *iounit
)
521 v9fs_req_recv(req
, P9_RLOPEN
);
523 v9fs_memread(req
, qid
, 13);
525 v9fs_memskip(req
, 13);
528 v9fs_uint32_read(req
, iounit
);
533 /* size[4] Twrite tag[2] fid[4] offset[8] count[4] data[count] */
534 static P9Req
*v9fs_twrite(QVirtio9P
*v9p
, uint32_t fid
, uint64_t offset
,
535 uint32_t count
, const void *data
, uint16_t tag
)
538 uint32_t body_size
= 4 + 8 + 4;
540 g_assert_cmpint(body_size
, <=, UINT32_MAX
- count
);
542 req
= v9fs_req_init(v9p
, body_size
, P9_TWRITE
, tag
);
543 v9fs_uint32_write(req
, fid
);
544 v9fs_uint64_write(req
, offset
);
545 v9fs_uint32_write(req
, count
);
546 v9fs_memwrite(req
, data
, count
);
551 /* size[4] Rwrite tag[2] count[4] */
552 static void v9fs_rwrite(P9Req
*req
, uint32_t *count
)
554 v9fs_req_recv(req
, P9_RWRITE
);
556 v9fs_uint32_read(req
, count
);
561 /* size[4] Tflush tag[2] oldtag[2] */
562 static P9Req
*v9fs_tflush(QVirtio9P
*v9p
, uint16_t oldtag
, uint16_t tag
)
566 req
= v9fs_req_init(v9p
, 2, P9_TFLUSH
, tag
);
567 v9fs_uint32_write(req
, oldtag
);
572 /* size[4] Rflush tag[2] */
573 static void v9fs_rflush(P9Req
*req
)
575 v9fs_req_recv(req
, P9_RFLUSH
);
579 static void do_version(QVirtio9P
*v9p
)
581 const char *version
= "9P2000.L";
583 char *server_version
;
586 req
= v9fs_tversion(v9p
, P9_MAX_SIZE
, version
, P9_NOTAG
);
587 v9fs_req_wait_for_reply(req
, NULL
);
588 v9fs_rversion(req
, &server_len
, &server_version
);
590 g_assert_cmpmem(server_version
, server_len
, version
, strlen(version
));
592 g_free(server_version
);
595 /* utility function: walk to requested dir and return fid for that dir */
596 static uint32_t do_walk(QVirtio9P
*v9p
, const char *path
)
600 const uint32_t fid
= genfid();
602 int nwnames
= split(path
, "/", &wnames
);
604 req
= v9fs_twalk(v9p
, 0, fid
, nwnames
, wnames
, 0);
605 v9fs_req_wait_for_reply(req
, NULL
);
606 v9fs_rwalk(req
, NULL
, NULL
);
612 static void fs_version(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
618 static void do_attach(QVirtio9P
*v9p
)
623 req
= v9fs_tattach(v9p
, 0, getuid(), 0);
624 v9fs_req_wait_for_reply(req
, NULL
);
625 v9fs_rattach(req
, NULL
);
628 static void fs_attach(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
634 static void fs_walk(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
636 QVirtio9P
*v9p
= obj
;
638 char *wnames
[P9_MAXWELEM
];
644 for (i
= 0; i
< P9_MAXWELEM
; i
++) {
645 wnames
[i
] = g_strdup_printf(QTEST_V9FS_SYNTH_WALK_FILE
, i
);
649 req
= v9fs_twalk(v9p
, 0, 1, P9_MAXWELEM
, wnames
, 0);
650 v9fs_req_wait_for_reply(req
, NULL
);
651 v9fs_rwalk(req
, &nwqid
, &wqid
);
653 g_assert_cmpint(nwqid
, ==, P9_MAXWELEM
);
655 for (i
= 0; i
< P9_MAXWELEM
; i
++) {
662 static bool fs_dirents_contain_name(struct V9fsDirent
*e
, const char* name
)
664 for (; e
; e
= e
->next
) {
665 if (!strcmp(e
->name
, name
)) {
672 /* size[4] Tmkdir tag[2] dfid[4] name[s] mode[4] gid[4] */
673 static P9Req
*v9fs_tmkdir(QVirtio9P
*v9p
, uint32_t dfid
, const char *name
,
674 uint32_t mode
, uint32_t gid
, uint16_t tag
)
678 uint32_t body_size
= 4 + 4 + 4;
679 uint16_t string_size
= v9fs_string_size(name
);
681 g_assert_cmpint(body_size
, <=, UINT32_MAX
- string_size
);
682 body_size
+= string_size
;
684 req
= v9fs_req_init(v9p
, body_size
, P9_TMKDIR
, tag
);
685 v9fs_uint32_write(req
, dfid
);
686 v9fs_string_write(req
, name
);
687 v9fs_uint32_write(req
, mode
);
688 v9fs_uint32_write(req
, gid
);
693 /* size[4] Rmkdir tag[2] qid[13] */
694 static void v9fs_rmkdir(P9Req
*req
, v9fs_qid
*qid
)
696 v9fs_req_recv(req
, P9_RMKDIR
);
698 v9fs_memread(req
, qid
, 13);
700 v9fs_memskip(req
, 13);
705 /* size[4] Tlcreate tag[2] fid[4] name[s] flags[4] mode[4] gid[4] */
706 static P9Req
*v9fs_tlcreate(QVirtio9P
*v9p
, uint32_t fid
, const char *name
,
707 uint32_t flags
, uint32_t mode
, uint32_t gid
,
712 uint32_t body_size
= 4 + 4 + 4 + 4;
713 uint16_t string_size
= v9fs_string_size(name
);
715 g_assert_cmpint(body_size
, <=, UINT32_MAX
- string_size
);
716 body_size
+= string_size
;
718 req
= v9fs_req_init(v9p
, body_size
, P9_TLCREATE
, tag
);
719 v9fs_uint32_write(req
, fid
);
720 v9fs_string_write(req
, name
);
721 v9fs_uint32_write(req
, flags
);
722 v9fs_uint32_write(req
, mode
);
723 v9fs_uint32_write(req
, gid
);
728 /* size[4] Rlcreate tag[2] qid[13] iounit[4] */
729 static void v9fs_rlcreate(P9Req
*req
, v9fs_qid
*qid
, uint32_t *iounit
)
731 v9fs_req_recv(req
, P9_RLCREATE
);
733 v9fs_memread(req
, qid
, 13);
735 v9fs_memskip(req
, 13);
738 v9fs_uint32_read(req
, iounit
);
743 /* size[4] Tsymlink tag[2] fid[4] name[s] symtgt[s] gid[4] */
744 static P9Req
*v9fs_tsymlink(QVirtio9P
*v9p
, uint32_t fid
, const char *name
,
745 const char *symtgt
, uint32_t gid
, uint16_t tag
)
749 uint32_t body_size
= 4 + 4;
750 uint16_t string_size
= v9fs_string_size(name
) + v9fs_string_size(symtgt
);
752 g_assert_cmpint(body_size
, <=, UINT32_MAX
- string_size
);
753 body_size
+= string_size
;
755 req
= v9fs_req_init(v9p
, body_size
, P9_TSYMLINK
, tag
);
756 v9fs_uint32_write(req
, fid
);
757 v9fs_string_write(req
, name
);
758 v9fs_string_write(req
, symtgt
);
759 v9fs_uint32_write(req
, gid
);
764 /* size[4] Rsymlink tag[2] qid[13] */
765 static void v9fs_rsymlink(P9Req
*req
, v9fs_qid
*qid
)
767 v9fs_req_recv(req
, P9_RSYMLINK
);
769 v9fs_memread(req
, qid
, 13);
771 v9fs_memskip(req
, 13);
776 /* size[4] Tlink tag[2] dfid[4] fid[4] name[s] */
777 static P9Req
*v9fs_tlink(QVirtio9P
*v9p
, uint32_t dfid
, uint32_t fid
,
778 const char *name
, uint16_t tag
)
782 uint32_t body_size
= 4 + 4;
783 uint16_t string_size
= v9fs_string_size(name
);
785 g_assert_cmpint(body_size
, <=, UINT32_MAX
- string_size
);
786 body_size
+= string_size
;
788 req
= v9fs_req_init(v9p
, body_size
, P9_TLINK
, tag
);
789 v9fs_uint32_write(req
, dfid
);
790 v9fs_uint32_write(req
, fid
);
791 v9fs_string_write(req
, name
);
796 /* size[4] Rlink tag[2] */
797 static void v9fs_rlink(P9Req
*req
)
799 v9fs_req_recv(req
, P9_RLINK
);
803 /* size[4] Tunlinkat tag[2] dirfd[4] name[s] flags[4] */
804 static P9Req
*v9fs_tunlinkat(QVirtio9P
*v9p
, uint32_t dirfd
, const char *name
,
805 uint32_t flags
, uint16_t tag
)
809 uint32_t body_size
= 4 + 4;
810 uint16_t string_size
= v9fs_string_size(name
);
812 g_assert_cmpint(body_size
, <=, UINT32_MAX
- string_size
);
813 body_size
+= string_size
;
815 req
= v9fs_req_init(v9p
, body_size
, P9_TUNLINKAT
, tag
);
816 v9fs_uint32_write(req
, dirfd
);
817 v9fs_string_write(req
, name
);
818 v9fs_uint32_write(req
, flags
);
823 /* size[4] Runlinkat tag[2] */
824 static void v9fs_runlinkat(P9Req
*req
)
826 v9fs_req_recv(req
, P9_RUNLINKAT
);
830 /* basic readdir test where reply fits into a single response message */
831 static void fs_readdir(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
833 QVirtio9P
*v9p
= obj
;
835 char *const wnames
[] = { g_strdup(QTEST_V9FS_SYNTH_READDIR_DIR
) };
838 uint32_t count
, nentries
;
839 struct V9fsDirent
*entries
= NULL
;
843 req
= v9fs_twalk(v9p
, 0, 1, 1, wnames
, 0);
844 v9fs_req_wait_for_reply(req
, NULL
);
845 v9fs_rwalk(req
, &nqid
, NULL
);
846 g_assert_cmpint(nqid
, ==, 1);
848 req
= v9fs_tlopen(v9p
, 1, O_DIRECTORY
, 0);
849 v9fs_req_wait_for_reply(req
, NULL
);
850 v9fs_rlopen(req
, &qid
, NULL
);
853 * submit count = msize - 11, because 11 is the header size of Rreaddir
855 req
= v9fs_treaddir(v9p
, 1, 0, P9_MAX_SIZE
- 11, 0);
856 v9fs_req_wait_for_reply(req
, NULL
);
857 v9fs_rreaddir(req
, &count
, &nentries
, &entries
);
860 * Assuming msize (P9_MAX_SIZE) is large enough so we can retrieve all
861 * dir entries with only one readdir request.
865 QTEST_V9FS_SYNTH_READDIR_NFILES
+ 2 /* "." and ".." */
869 * Check all file names exist in returned entries, ignore their order
872 g_assert_cmpint(fs_dirents_contain_name(entries
, "."), ==, true);
873 g_assert_cmpint(fs_dirents_contain_name(entries
, ".."), ==, true);
874 for (int i
= 0; i
< QTEST_V9FS_SYNTH_READDIR_NFILES
; ++i
) {
875 char *name
= g_strdup_printf(QTEST_V9FS_SYNTH_READDIR_FILE
, i
);
876 g_assert_cmpint(fs_dirents_contain_name(entries
, name
), ==, true);
880 v9fs_free_dirents(entries
);
884 /* readdir test where overall request is split over several messages */
885 static void do_readdir_split(QVirtio9P
*v9p
, uint32_t count
)
887 char *const wnames
[] = { g_strdup(QTEST_V9FS_SYNTH_READDIR_DIR
) };
890 uint32_t nentries
, npartialentries
;
891 struct V9fsDirent
*entries
, *tail
, *partialentries
;
904 req
= v9fs_twalk(v9p
, 0, fid
, 1, wnames
, 0);
905 v9fs_req_wait_for_reply(req
, NULL
);
906 v9fs_rwalk(req
, &nqid
, NULL
);
907 g_assert_cmpint(nqid
, ==, 1);
909 req
= v9fs_tlopen(v9p
, fid
, O_DIRECTORY
, 0);
910 v9fs_req_wait_for_reply(req
, NULL
);
911 v9fs_rlopen(req
, &qid
, NULL
);
914 * send as many Treaddir requests as required to get all directory
919 partialentries
= NULL
;
921 req
= v9fs_treaddir(v9p
, fid
, offset
, count
, 0);
922 v9fs_req_wait_for_reply(req
, NULL
);
923 v9fs_rreaddir(req
, &count
, &npartialentries
, &partialentries
);
924 if (npartialentries
> 0 && partialentries
) {
926 entries
= partialentries
;
927 nentries
= npartialentries
;
928 tail
= partialentries
;
930 tail
->next
= partialentries
;
931 nentries
+= npartialentries
;
936 offset
= tail
->offset
;
944 QTEST_V9FS_SYNTH_READDIR_NFILES
+ 2 /* "." and ".." */
948 * Check all file names exist in returned entries, ignore their order
951 g_assert_cmpint(fs_dirents_contain_name(entries
, "."), ==, true);
952 g_assert_cmpint(fs_dirents_contain_name(entries
, ".."), ==, true);
953 for (int i
= 0; i
< QTEST_V9FS_SYNTH_READDIR_NFILES
; ++i
) {
954 char *name
= g_strdup_printf(QTEST_V9FS_SYNTH_READDIR_FILE
, i
);
955 g_assert_cmpint(fs_dirents_contain_name(entries
, name
), ==, true);
959 v9fs_free_dirents(entries
);
964 static void fs_walk_no_slash(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
966 QVirtio9P
*v9p
= obj
;
968 char *const wnames
[] = { g_strdup(" /") };
973 req
= v9fs_twalk(v9p
, 0, 1, 1, wnames
, 0);
974 v9fs_req_wait_for_reply(req
, NULL
);
975 v9fs_rlerror(req
, &err
);
977 g_assert_cmpint(err
, ==, ENOENT
);
982 static void fs_walk_dotdot(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
984 QVirtio9P
*v9p
= obj
;
986 char *const wnames
[] = { g_strdup("..") };
987 v9fs_qid root_qid
, *wqid
;
991 req
= v9fs_tattach(v9p
, 0, getuid(), 0);
992 v9fs_req_wait_for_reply(req
, NULL
);
993 v9fs_rattach(req
, &root_qid
);
995 req
= v9fs_twalk(v9p
, 0, 1, 1, wnames
, 0);
996 v9fs_req_wait_for_reply(req
, NULL
);
997 v9fs_rwalk(req
, NULL
, &wqid
); /* We now we'll get one qid */
999 g_assert_cmpmem(&root_qid
, 13, wqid
[0], 13);
1005 static void fs_lopen(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
1007 QVirtio9P
*v9p
= obj
;
1009 char *const wnames
[] = { g_strdup(QTEST_V9FS_SYNTH_LOPEN_FILE
) };
1013 req
= v9fs_twalk(v9p
, 0, 1, 1, wnames
, 0);
1014 v9fs_req_wait_for_reply(req
, NULL
);
1015 v9fs_rwalk(req
, NULL
, NULL
);
1017 req
= v9fs_tlopen(v9p
, 1, O_WRONLY
, 0);
1018 v9fs_req_wait_for_reply(req
, NULL
);
1019 v9fs_rlopen(req
, NULL
, NULL
);
1024 static void fs_write(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
1026 QVirtio9P
*v9p
= obj
;
1028 static const uint32_t write_count
= P9_MAX_SIZE
/ 2;
1029 char *const wnames
[] = { g_strdup(QTEST_V9FS_SYNTH_WRITE_FILE
) };
1030 char *buf
= g_malloc0(write_count
);
1035 req
= v9fs_twalk(v9p
, 0, 1, 1, wnames
, 0);
1036 v9fs_req_wait_for_reply(req
, NULL
);
1037 v9fs_rwalk(req
, NULL
, NULL
);
1039 req
= v9fs_tlopen(v9p
, 1, O_WRONLY
, 0);
1040 v9fs_req_wait_for_reply(req
, NULL
);
1041 v9fs_rlopen(req
, NULL
, NULL
);
1043 req
= v9fs_twrite(v9p
, 1, 0, write_count
, buf
, 0);
1044 v9fs_req_wait_for_reply(req
, NULL
);
1045 v9fs_rwrite(req
, &count
);
1046 g_assert_cmpint(count
, ==, write_count
);
1052 static void fs_flush_success(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
1054 QVirtio9P
*v9p
= obj
;
1056 char *const wnames
[] = { g_strdup(QTEST_V9FS_SYNTH_FLUSH_FILE
) };
1057 P9Req
*req
, *flush_req
;
1059 uint8_t should_block
;
1062 req
= v9fs_twalk(v9p
, 0, 1, 1, wnames
, 0);
1063 v9fs_req_wait_for_reply(req
, NULL
);
1064 v9fs_rwalk(req
, NULL
, NULL
);
1066 req
= v9fs_tlopen(v9p
, 1, O_WRONLY
, 0);
1067 v9fs_req_wait_for_reply(req
, NULL
);
1068 v9fs_rlopen(req
, NULL
, NULL
);
1070 /* This will cause the 9p server to try to write data to the backend,
1071 * until the write request gets cancelled.
1074 req
= v9fs_twrite(v9p
, 1, 0, sizeof(should_block
), &should_block
, 0);
1076 flush_req
= v9fs_tflush(v9p
, req
->tag
, 1);
1078 /* The write request is supposed to be flushed: the server should just
1079 * mark the write request as used and reply to the flush request.
1081 v9fs_req_wait_for_reply(req
, &reply_len
);
1082 g_assert_cmpint(reply_len
, ==, 0);
1084 v9fs_rflush(flush_req
);
1089 static void fs_flush_ignored(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
1091 QVirtio9P
*v9p
= obj
;
1093 char *const wnames
[] = { g_strdup(QTEST_V9FS_SYNTH_FLUSH_FILE
) };
1094 P9Req
*req
, *flush_req
;
1096 uint8_t should_block
;
1099 req
= v9fs_twalk(v9p
, 0, 1, 1, wnames
, 0);
1100 v9fs_req_wait_for_reply(req
, NULL
);
1101 v9fs_rwalk(req
, NULL
, NULL
);
1103 req
= v9fs_tlopen(v9p
, 1, O_WRONLY
, 0);
1104 v9fs_req_wait_for_reply(req
, NULL
);
1105 v9fs_rlopen(req
, NULL
, NULL
);
1107 /* This will cause the write request to complete right away, before it
1108 * could be actually cancelled.
1111 req
= v9fs_twrite(v9p
, 1, 0, sizeof(should_block
), &should_block
, 0);
1113 flush_req
= v9fs_tflush(v9p
, req
->tag
, 1);
1115 /* The write request is supposed to complete. The server should
1116 * reply to the write request and the flush request.
1118 v9fs_req_wait_for_reply(req
, NULL
);
1119 v9fs_rwrite(req
, &count
);
1120 g_assert_cmpint(count
, ==, sizeof(should_block
));
1121 v9fs_rflush(flush_req
);
1126 static void do_mkdir(QVirtio9P
*v9p
, const char *path
, const char *cname
)
1128 char *const name
= g_strdup(cname
);
1132 fid
= do_walk(v9p
, path
);
1134 req
= v9fs_tmkdir(v9p
, fid
, name
, 0750, 0, 0);
1135 v9fs_req_wait_for_reply(req
, NULL
);
1136 v9fs_rmkdir(req
, NULL
);
1141 /* create a regular file with Tlcreate and return file's fid */
1142 static uint32_t do_lcreate(QVirtio9P
*v9p
, const char *path
,
1145 char *const name
= g_strdup(cname
);
1149 fid
= do_walk(v9p
, path
);
1151 req
= v9fs_tlcreate(v9p
, fid
, name
, 0, 0750, 0, 0);
1152 v9fs_req_wait_for_reply(req
, NULL
);
1153 v9fs_rlcreate(req
, NULL
, NULL
);
1159 /* create symlink named @a clink in directory @a path pointing to @a to */
1160 static void do_symlink(QVirtio9P
*v9p
, const char *path
, const char *clink
,
1163 char *const name
= g_strdup(clink
);
1164 char *const dst
= g_strdup(to
);
1168 fid
= do_walk(v9p
, path
);
1170 req
= v9fs_tsymlink(v9p
, fid
, name
, dst
, 0, 0);
1171 v9fs_req_wait_for_reply(req
, NULL
);
1172 v9fs_rsymlink(req
, NULL
);
1178 /* create a hard link named @a clink in directory @a path pointing to @a to */
1179 static void do_hardlink(QVirtio9P
*v9p
, const char *path
, const char *clink
,
1185 dfid
= do_walk(v9p
, path
);
1186 fid
= do_walk(v9p
, to
);
1188 req
= v9fs_tlink(v9p
, dfid
, fid
, clink
, 0);
1189 v9fs_req_wait_for_reply(req
, NULL
);
1193 static void do_unlinkat(QVirtio9P
*v9p
, const char *atpath
, const char *rpath
,
1196 char *const name
= g_strdup(rpath
);
1200 fid
= do_walk(v9p
, atpath
);
1202 req
= v9fs_tunlinkat(v9p
, fid
, name
, flags
, 0);
1203 v9fs_req_wait_for_reply(req
, NULL
);
1204 v9fs_runlinkat(req
);
1209 static void fs_readdir_split_128(void *obj
, void *data
,
1210 QGuestAllocator
*t_alloc
)
1213 do_readdir_split(obj
, 128);
1216 static void fs_readdir_split_256(void *obj
, void *data
,
1217 QGuestAllocator
*t_alloc
)
1220 do_readdir_split(obj
, 256);
1223 static void fs_readdir_split_512(void *obj
, void *data
,
1224 QGuestAllocator
*t_alloc
)
1227 do_readdir_split(obj
, 512);
1231 /* tests using the 9pfs 'local' fs driver */
1233 static void fs_create_dir(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
1235 QVirtio9P
*v9p
= obj
;
1238 char *root_path
= virtio_9p_test_path("");
1239 char *new_dir
= virtio_9p_test_path("01");
1241 g_assert(root_path
!= NULL
);
1244 do_mkdir(v9p
, "/", "01");
1246 /* check if created directory really exists now ... */
1247 g_assert(stat(new_dir
, &st
) == 0);
1248 /* ... and is actually a directory */
1249 g_assert((st
.st_mode
& S_IFMT
) == S_IFDIR
);
1255 static void fs_unlinkat_dir(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
1257 QVirtio9P
*v9p
= obj
;
1260 char *root_path
= virtio_9p_test_path("");
1261 char *new_dir
= virtio_9p_test_path("02");
1263 g_assert(root_path
!= NULL
);
1266 do_mkdir(v9p
, "/", "02");
1268 /* check if created directory really exists now ... */
1269 g_assert(stat(new_dir
, &st
) == 0);
1270 /* ... and is actually a directory */
1271 g_assert((st
.st_mode
& S_IFMT
) == S_IFDIR
);
1273 do_unlinkat(v9p
, "/", "02", AT_REMOVEDIR
);
1274 /* directory should be gone now */
1275 g_assert(stat(new_dir
, &st
) != 0);
1281 static void fs_create_file(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
1283 QVirtio9P
*v9p
= obj
;
1286 char *new_file
= virtio_9p_test_path("03/1st_file");
1289 do_mkdir(v9p
, "/", "03");
1290 do_lcreate(v9p
, "03", "1st_file");
1292 /* check if created file exists now ... */
1293 g_assert(stat(new_file
, &st
) == 0);
1294 /* ... and is a regular file */
1295 g_assert((st
.st_mode
& S_IFMT
) == S_IFREG
);
1300 static void fs_unlinkat_file(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
1302 QVirtio9P
*v9p
= obj
;
1305 char *new_file
= virtio_9p_test_path("04/doa_file");
1308 do_mkdir(v9p
, "/", "04");
1309 do_lcreate(v9p
, "04", "doa_file");
1311 /* check if created file exists now ... */
1312 g_assert(stat(new_file
, &st
) == 0);
1313 /* ... and is a regular file */
1314 g_assert((st
.st_mode
& S_IFMT
) == S_IFREG
);
1316 do_unlinkat(v9p
, "04", "doa_file", 0);
1317 /* file should be gone now */
1318 g_assert(stat(new_file
, &st
) != 0);
1323 static void fs_symlink_file(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
1325 QVirtio9P
*v9p
= obj
;
1328 char *real_file
= virtio_9p_test_path("05/real_file");
1329 char *symlink_file
= virtio_9p_test_path("05/symlink_file");
1332 do_mkdir(v9p
, "/", "05");
1333 do_lcreate(v9p
, "05", "real_file");
1334 g_assert(stat(real_file
, &st
) == 0);
1335 g_assert((st
.st_mode
& S_IFMT
) == S_IFREG
);
1337 do_symlink(v9p
, "05", "symlink_file", "real_file");
1339 /* check if created link exists now */
1340 g_assert(stat(symlink_file
, &st
) == 0);
1342 g_free(symlink_file
);
1346 static void fs_unlinkat_symlink(void *obj
, void *data
,
1347 QGuestAllocator
*t_alloc
)
1349 QVirtio9P
*v9p
= obj
;
1352 char *real_file
= virtio_9p_test_path("06/real_file");
1353 char *symlink_file
= virtio_9p_test_path("06/symlink_file");
1356 do_mkdir(v9p
, "/", "06");
1357 do_lcreate(v9p
, "06", "real_file");
1358 g_assert(stat(real_file
, &st
) == 0);
1359 g_assert((st
.st_mode
& S_IFMT
) == S_IFREG
);
1361 do_symlink(v9p
, "06", "symlink_file", "real_file");
1362 g_assert(stat(symlink_file
, &st
) == 0);
1364 do_unlinkat(v9p
, "06", "symlink_file", 0);
1365 /* symlink should be gone now */
1366 g_assert(stat(symlink_file
, &st
) != 0);
1368 g_free(symlink_file
);
1372 static void fs_hardlink_file(void *obj
, void *data
, QGuestAllocator
*t_alloc
)
1374 QVirtio9P
*v9p
= obj
;
1376 struct stat st_real
, st_link
;
1377 char *real_file
= virtio_9p_test_path("07/real_file");
1378 char *hardlink_file
= virtio_9p_test_path("07/hardlink_file");
1381 do_mkdir(v9p
, "/", "07");
1382 do_lcreate(v9p
, "07", "real_file");
1383 g_assert(stat(real_file
, &st_real
) == 0);
1384 g_assert((st_real
.st_mode
& S_IFMT
) == S_IFREG
);
1386 do_hardlink(v9p
, "07", "hardlink_file", "07/real_file");
1388 /* check if link exists now ... */
1389 g_assert(stat(hardlink_file
, &st_link
) == 0);
1390 /* ... and it's a hard link, right? */
1391 g_assert((st_link
.st_mode
& S_IFMT
) == S_IFREG
);
1392 g_assert(st_link
.st_dev
== st_real
.st_dev
);
1393 g_assert(st_link
.st_ino
== st_real
.st_ino
);
1395 g_free(hardlink_file
);
1399 static void fs_unlinkat_hardlink(void *obj
, void *data
,
1400 QGuestAllocator
*t_alloc
)
1402 QVirtio9P
*v9p
= obj
;
1404 struct stat st_real
, st_link
;
1405 char *real_file
= virtio_9p_test_path("08/real_file");
1406 char *hardlink_file
= virtio_9p_test_path("08/hardlink_file");
1409 do_mkdir(v9p
, "/", "08");
1410 do_lcreate(v9p
, "08", "real_file");
1411 g_assert(stat(real_file
, &st_real
) == 0);
1412 g_assert((st_real
.st_mode
& S_IFMT
) == S_IFREG
);
1414 do_hardlink(v9p
, "08", "hardlink_file", "08/real_file");
1415 g_assert(stat(hardlink_file
, &st_link
) == 0);
1417 do_unlinkat(v9p
, "08", "hardlink_file", 0);
1418 /* symlink should be gone now */
1419 g_assert(stat(hardlink_file
, &st_link
) != 0);
1420 /* and old file should still exist */
1421 g_assert(stat(real_file
, &st_real
) == 0);
1423 g_free(hardlink_file
);
1427 static void *assign_9p_local_driver(GString
*cmd_line
, void *arg
)
1429 virtio_9p_assign_local_driver(cmd_line
, "security_model=mapped-xattr");
1433 static void register_virtio_9p_test(void)
1436 QOSGraphTestOptions opts
= {
1439 /* 9pfs test cases using the 'synth' filesystem driver */
1440 qos_add_test("synth/config", "virtio-9p", pci_config
, &opts
);
1441 qos_add_test("synth/version/basic", "virtio-9p", fs_version
, &opts
);
1442 qos_add_test("synth/attach/basic", "virtio-9p", fs_attach
, &opts
);
1443 qos_add_test("synth/walk/basic", "virtio-9p", fs_walk
, &opts
);
1444 qos_add_test("synth/walk/no_slash", "virtio-9p", fs_walk_no_slash
,
1446 qos_add_test("synth/walk/dotdot_from_root", "virtio-9p",
1447 fs_walk_dotdot
, &opts
);
1448 qos_add_test("synth/lopen/basic", "virtio-9p", fs_lopen
, &opts
);
1449 qos_add_test("synth/write/basic", "virtio-9p", fs_write
, &opts
);
1450 qos_add_test("synth/flush/success", "virtio-9p", fs_flush_success
,
1452 qos_add_test("synth/flush/ignored", "virtio-9p", fs_flush_ignored
,
1454 qos_add_test("synth/readdir/basic", "virtio-9p", fs_readdir
, &opts
);
1455 qos_add_test("synth/readdir/split_512", "virtio-9p",
1456 fs_readdir_split_512
, &opts
);
1457 qos_add_test("synth/readdir/split_256", "virtio-9p",
1458 fs_readdir_split_256
, &opts
);
1459 qos_add_test("synth/readdir/split_128", "virtio-9p",
1460 fs_readdir_split_128
, &opts
);
1463 /* 9pfs test cases using the 'local' filesystem driver */
1466 * XXX: Until we are sure that these tests can run everywhere,
1467 * keep them as "slow" so that they aren't run with "make check".
1469 if (!g_test_slow()) {
1473 opts
.before
= assign_9p_local_driver
;
1474 qos_add_test("local/config", "virtio-9p", pci_config
, &opts
);
1475 qos_add_test("local/create_dir", "virtio-9p", fs_create_dir
, &opts
);
1476 qos_add_test("local/unlinkat_dir", "virtio-9p", fs_unlinkat_dir
, &opts
);
1477 qos_add_test("local/create_file", "virtio-9p", fs_create_file
, &opts
);
1478 qos_add_test("local/unlinkat_file", "virtio-9p", fs_unlinkat_file
, &opts
);
1479 qos_add_test("local/symlink_file", "virtio-9p", fs_symlink_file
, &opts
);
1480 qos_add_test("local/unlinkat_symlink", "virtio-9p", fs_unlinkat_symlink
,
1482 qos_add_test("local/hardlink_file", "virtio-9p", fs_hardlink_file
, &opts
);
1483 qos_add_test("local/unlinkat_hardlink", "virtio-9p", fs_unlinkat_hardlink
,
1487 libqos_init(register_virtio_9p_test
);
1489 static void __attribute__((constructor
)) construct_9p_test(void)
1491 /* make sure test dir for the 'local' tests exists */
1492 virtio_9p_create_local_test_dir();
1495 static void __attribute__((destructor
)) destruct_9p_test(void)
1497 /* remove previously created test dir when test suite completed */
1498 virtio_9p_remove_local_test_dir();