xref: /qemu/tests/qtest/virtio-9p-test.c (revision a9a53769318503c4d8884e642e00603ea6885cb1)
1 /*
2  * QTest testcase for VirtIO 9P
3  *
4  * Copyright (c) 2014 SUSE LINUX Products GmbH
5  *
6  * This work is licensed under the terms of the GNU GPL, version 2 or later.
7  * See the COPYING file in the top-level directory.
8  */
9 
10 /*
11  * Not so fast! You might want to read the 9p developer docs first:
12  * https://wiki.qemu.org/Documentation/9p
13  */
14 
15 #include "qemu/osdep.h"
16 #include "qemu/module.h"
17 #include "libqos/virtio-9p-client.h"
18 
19 #define twalk(...) v9fs_twalk((TWalkOpt) __VA_ARGS__)
20 #define tversion(...) v9fs_tversion((TVersionOpt) __VA_ARGS__)
21 #define tattach(...) v9fs_tattach((TAttachOpt) __VA_ARGS__)
22 #define tgetattr(...) v9fs_tgetattr((TGetAttrOpt) __VA_ARGS__)
23 #define treaddir(...) v9fs_treaddir((TReadDirOpt) __VA_ARGS__)
24 
25 static void pci_config(void *obj, void *data, QGuestAllocator *t_alloc)
26 {
27     QVirtio9P *v9p = obj;
28     v9fs_set_allocator(t_alloc);
29     size_t tag_len = qvirtio_config_readw(v9p->vdev, 0);
30     g_autofree char *tag = NULL;
31     int i;
32 
33     g_assert_cmpint(tag_len, ==, strlen(MOUNT_TAG));
34 
35     tag = g_malloc(tag_len);
36     for (i = 0; i < tag_len; i++) {
37         tag[i] = qvirtio_config_readb(v9p->vdev, i + 2);
38     }
39     g_assert_cmpmem(tag, tag_len, MOUNT_TAG, tag_len);
40 }
41 
42 static inline bool is_same_qid(v9fs_qid a, v9fs_qid b)
43 {
44     /* don't compare QID version for checking for file ID equalness */
45     return a[0] == b[0] && memcmp(&a[5], &b[5], 8) == 0;
46 }
47 
48 static void fs_version(void *obj, void *data, QGuestAllocator *t_alloc)
49 {
50     v9fs_set_allocator(t_alloc);
51     tversion({ .client = obj });
52 }
53 
54 static void fs_attach(void *obj, void *data, QGuestAllocator *t_alloc)
55 {
56     v9fs_set_allocator(t_alloc);
57     tattach({ .client = obj });
58 }
59 
60 static void fs_walk(void *obj, void *data, QGuestAllocator *t_alloc)
61 {
62     QVirtio9P *v9p = obj;
63     v9fs_set_allocator(t_alloc);
64     char *wnames[P9_MAXWELEM];
65     uint16_t nwqid;
66     g_autofree v9fs_qid *wqid = NULL;
67     int i;
68 
69     for (i = 0; i < P9_MAXWELEM; i++) {
70         wnames[i] = g_strdup_printf(QTEST_V9FS_SYNTH_WALK_FILE, i);
71     }
72 
73     tattach({ .client = v9p });
74     twalk({
75         .client = v9p, .fid = 0, .newfid = 1,
76         .nwname = P9_MAXWELEM, .wnames = wnames,
77         .rwalk = { .nwqid = &nwqid, .wqid = &wqid }
78     });
79 
80     g_assert_cmpint(nwqid, ==, P9_MAXWELEM);
81 
82     for (i = 0; i < P9_MAXWELEM; i++) {
83         g_free(wnames[i]);
84     }
85 }
86 
87 static bool fs_dirents_contain_name(struct V9fsDirent *e, const char* name)
88 {
89     for (; e; e = e->next) {
90         if (!strcmp(e->name, name)) {
91             return true;
92         }
93     }
94     return false;
95 }
96 
97 /* basic readdir test where reply fits into a single response message */
98 static void fs_readdir(void *obj, void *data, QGuestAllocator *t_alloc)
99 {
100     QVirtio9P *v9p = obj;
101     v9fs_set_allocator(t_alloc);
102     char *wnames[] = { g_strdup(QTEST_V9FS_SYNTH_READDIR_DIR) };
103     uint16_t nqid;
104     v9fs_qid qid;
105     uint32_t count, nentries;
106     struct V9fsDirent *entries = NULL;
107     P9Req *req;
108 
109     tattach({ .client = v9p });
110     twalk({
111         .client = v9p, .fid = 0, .newfid = 1,
112         .nwname = 1, .wnames = wnames, .rwalk.nwqid = &nqid
113     });
114     g_assert_cmpint(nqid, ==, 1);
115 
116     req = v9fs_tlopen(v9p, 1, O_DIRECTORY, 0);
117     v9fs_req_wait_for_reply(req, NULL);
118     v9fs_rlopen(req, &qid, NULL);
119 
120     /*
121      * submit count = msize - 11, because 11 is the header size of Rreaddir
122      */
123     treaddir({
124         .client = v9p, .fid = 1, .offset = 0, .count = P9_MAX_SIZE - 11,
125         .rreaddir = {
126             .count = &count, .nentries = &nentries, .entries = &entries
127         }
128     });
129 
130     /*
131      * Assuming msize (P9_MAX_SIZE) is large enough so we can retrieve all
132      * dir entries with only one readdir request.
133      */
134     g_assert_cmpint(
135         nentries, ==,
136         QTEST_V9FS_SYNTH_READDIR_NFILES + 2 /* "." and ".." */
137     );
138 
139     /*
140      * Check all file names exist in returned entries, ignore their order
141      * though.
142      */
143     g_assert_cmpint(fs_dirents_contain_name(entries, "."), ==, true);
144     g_assert_cmpint(fs_dirents_contain_name(entries, ".."), ==, true);
145     for (int i = 0; i < QTEST_V9FS_SYNTH_READDIR_NFILES; ++i) {
146         g_autofree char *name =
147             g_strdup_printf(QTEST_V9FS_SYNTH_READDIR_FILE, i);
148         g_assert_cmpint(fs_dirents_contain_name(entries, name), ==, true);
149     }
150 
151     v9fs_free_dirents(entries);
152     g_free(wnames[0]);
153 }
154 
155 /* readdir test where overall request is split over several messages */
156 static void do_readdir_split(QVirtio9P *v9p, uint32_t count)
157 {
158     char *wnames[] = { g_strdup(QTEST_V9FS_SYNTH_READDIR_DIR) };
159     uint16_t nqid;
160     v9fs_qid qid;
161     uint32_t nentries, npartialentries;
162     struct V9fsDirent *entries, *tail, *partialentries;
163     P9Req *req;
164     int fid;
165     uint64_t offset;
166 
167     tattach({ .client = v9p });
168 
169     fid = 1;
170     offset = 0;
171     entries = NULL;
172     nentries = 0;
173     tail = NULL;
174 
175     twalk({
176         .client = v9p, .fid = 0, .newfid = fid,
177         .nwname = 1, .wnames = wnames, .rwalk.nwqid = &nqid
178     });
179     g_assert_cmpint(nqid, ==, 1);
180 
181     req = v9fs_tlopen(v9p, fid, O_DIRECTORY, 0);
182     v9fs_req_wait_for_reply(req, NULL);
183     v9fs_rlopen(req, &qid, NULL);
184 
185     /*
186      * send as many Treaddir requests as required to get all directory
187      * entries
188      */
189     while (true) {
190         npartialentries = 0;
191         partialentries = NULL;
192 
193         treaddir({
194             .client = v9p, .fid = fid, .offset = offset, .count = count,
195             .rreaddir = {
196                 .count = &count, .nentries = &npartialentries,
197                 .entries = &partialentries
198             }
199         });
200         if (npartialentries > 0 && partialentries) {
201             if (!entries) {
202                 entries = partialentries;
203                 nentries = npartialentries;
204                 tail = partialentries;
205             } else {
206                 tail->next = partialentries;
207                 nentries += npartialentries;
208             }
209             while (tail->next) {
210                 tail = tail->next;
211             }
212             offset = tail->offset;
213         } else {
214             break;
215         }
216     }
217 
218     g_assert_cmpint(
219         nentries, ==,
220         QTEST_V9FS_SYNTH_READDIR_NFILES + 2 /* "." and ".." */
221     );
222 
223     /*
224      * Check all file names exist in returned entries, ignore their order
225      * though.
226      */
227     g_assert_cmpint(fs_dirents_contain_name(entries, "."), ==, true);
228     g_assert_cmpint(fs_dirents_contain_name(entries, ".."), ==, true);
229     for (int i = 0; i < QTEST_V9FS_SYNTH_READDIR_NFILES; ++i) {
230         char *name = g_strdup_printf(QTEST_V9FS_SYNTH_READDIR_FILE, i);
231         g_assert_cmpint(fs_dirents_contain_name(entries, name), ==, true);
232         g_free(name);
233     }
234 
235     v9fs_free_dirents(entries);
236 
237     g_free(wnames[0]);
238 }
239 
240 static void fs_walk_no_slash(void *obj, void *data, QGuestAllocator *t_alloc)
241 {
242     QVirtio9P *v9p = obj;
243     v9fs_set_allocator(t_alloc);
244     char *wnames[] = { g_strdup(" /") };
245 
246     tattach({ .client = v9p });
247     twalk({
248         .client = v9p, .fid = 0, .newfid = 1, .nwname = 1, .wnames = wnames,
249         .expectErr = ENOENT
250     });
251 
252     g_free(wnames[0]);
253 }
254 
255 static void fs_walk_nonexistent(void *obj, void *data, QGuestAllocator *t_alloc)
256 {
257     QVirtio9P *v9p = obj;
258     v9fs_set_allocator(t_alloc);
259 
260     tattach({ .client = v9p });
261     /*
262      * The 9p2000 protocol spec says: "If the first element cannot be walked
263      * for any reason, Rerror is returned."
264      */
265     twalk({ .client = v9p, .path = "non-existent", .expectErr = ENOENT });
266 }
267 
268 static void fs_walk_2nd_nonexistent(void *obj, void *data,
269                                     QGuestAllocator *t_alloc)
270 {
271     QVirtio9P *v9p = obj;
272     v9fs_set_allocator(t_alloc);
273     v9fs_qid root_qid;
274     uint16_t nwqid;
275     uint32_t fid;
276     g_autofree v9fs_qid *wqid = NULL;
277     g_autofree char *path = g_strdup_printf(
278         QTEST_V9FS_SYNTH_WALK_FILE "/non-existent", 0
279     );
280 
281     tattach({ .client = v9p, .rattach.qid = &root_qid });
282     fid = twalk({
283         .client = v9p, .path = path,
284         .rwalk = { .nwqid = &nwqid, .wqid = &wqid }
285     }).newfid;
286     /*
287      * The 9p2000 protocol spec says: "nwqid is therefore either nwname or the
288      * index of the first elementwise walk that failed."
289      */
290     assert(nwqid == 1);
291 
292     /* returned QID wqid[0] is file ID of 1st subdir */
293     g_assert(wqid && wqid[0] && !is_same_qid(root_qid, wqid[0]));
294 
295     /* expect fid being unaffected by walk above */
296     tgetattr({
297         .client = v9p, .fid = fid, .request_mask = P9_GETATTR_BASIC,
298         .expectErr = ENOENT
299     });
300 }
301 
302 static void fs_walk_none(void *obj, void *data, QGuestAllocator *t_alloc)
303 {
304     QVirtio9P *v9p = obj;
305     v9fs_set_allocator(t_alloc);
306     v9fs_qid root_qid;
307     g_autofree v9fs_qid *wqid = NULL;
308     struct v9fs_attr attr;
309 
310     tversion({ .client = v9p });
311     tattach({
312         .client = v9p, .fid = 0, .n_uname = getuid(),
313         .rattach.qid = &root_qid
314     });
315 
316     twalk({
317         .client = v9p, .fid = 0, .newfid = 1, .nwname = 0, .wnames = NULL,
318         .rwalk.wqid = &wqid
319     });
320 
321     /* special case: no QID is returned if nwname=0 was sent */
322     g_assert(wqid == NULL);
323 
324     tgetattr({
325         .client = v9p, .fid = 1, .request_mask = P9_GETATTR_BASIC,
326         .rgetattr.attr = &attr
327     });
328 
329     g_assert(is_same_qid(root_qid, attr.qid));
330 }
331 
332 static void fs_walk_dotdot(void *obj, void *data, QGuestAllocator *t_alloc)
333 {
334     QVirtio9P *v9p = obj;
335     v9fs_set_allocator(t_alloc);
336     char *wnames[] = { g_strdup("..") };
337     v9fs_qid root_qid;
338     g_autofree v9fs_qid *wqid = NULL;
339 
340     tversion({ .client = v9p });
341     tattach({
342         .client = v9p, .fid = 0, .n_uname = getuid(),
343         .rattach.qid = &root_qid
344     });
345 
346     twalk({
347         .client = v9p, .fid = 0, .newfid = 1, .nwname = 1, .wnames = wnames,
348         .rwalk.wqid = &wqid /* We now we'll get one qid */
349     });
350 
351     g_assert_cmpmem(&root_qid, 13, wqid[0], 13);
352 
353     g_free(wnames[0]);
354 }
355 
356 static void fs_lopen(void *obj, void *data, QGuestAllocator *t_alloc)
357 {
358     QVirtio9P *v9p = obj;
359     v9fs_set_allocator(t_alloc);
360     char *wnames[] = { g_strdup(QTEST_V9FS_SYNTH_LOPEN_FILE) };
361     P9Req *req;
362 
363     tattach({ .client = v9p });
364     twalk({
365         .client = v9p, .fid = 0, .newfid = 1, .nwname = 1, .wnames = wnames
366     });
367 
368     req = v9fs_tlopen(v9p, 1, O_WRONLY, 0);
369     v9fs_req_wait_for_reply(req, NULL);
370     v9fs_rlopen(req, NULL, NULL);
371 
372     g_free(wnames[0]);
373 }
374 
375 static void fs_write(void *obj, void *data, QGuestAllocator *t_alloc)
376 {
377     QVirtio9P *v9p = obj;
378     v9fs_set_allocator(t_alloc);
379     static const uint32_t write_count = P9_MAX_SIZE / 2;
380     char *wnames[] = { g_strdup(QTEST_V9FS_SYNTH_WRITE_FILE) };
381     g_autofree char *buf = g_malloc0(write_count);
382     uint32_t count;
383     P9Req *req;
384 
385     tattach({ .client = v9p });
386     twalk({
387         .client = v9p, .fid = 0, .newfid = 1, .nwname = 1, .wnames = wnames
388     });
389 
390     req = v9fs_tlopen(v9p, 1, O_WRONLY, 0);
391     v9fs_req_wait_for_reply(req, NULL);
392     v9fs_rlopen(req, NULL, NULL);
393 
394     req = v9fs_twrite(v9p, 1, 0, write_count, buf, 0);
395     v9fs_req_wait_for_reply(req, NULL);
396     v9fs_rwrite(req, &count);
397     g_assert_cmpint(count, ==, write_count);
398 
399     g_free(wnames[0]);
400 }
401 
402 static void fs_flush_success(void *obj, void *data, QGuestAllocator *t_alloc)
403 {
404     QVirtio9P *v9p = obj;
405     v9fs_set_allocator(t_alloc);
406     char *wnames[] = { g_strdup(QTEST_V9FS_SYNTH_FLUSH_FILE) };
407     P9Req *req, *flush_req;
408     uint32_t reply_len;
409     uint8_t should_block;
410 
411     tattach({ .client = v9p });
412     twalk({
413         .client = v9p, .fid = 0, .newfid = 1, .nwname = 1, .wnames = wnames
414     });
415 
416     req = v9fs_tlopen(v9p, 1, O_WRONLY, 0);
417     v9fs_req_wait_for_reply(req, NULL);
418     v9fs_rlopen(req, NULL, NULL);
419 
420     /* This will cause the 9p server to try to write data to the backend,
421      * until the write request gets cancelled.
422      */
423     should_block = 1;
424     req = v9fs_twrite(v9p, 1, 0, sizeof(should_block), &should_block, 0);
425 
426     flush_req = v9fs_tflush(v9p, req->tag, 1);
427 
428     /* The write request is supposed to be flushed: the server should just
429      * mark the write request as used and reply to the flush request.
430      */
431     v9fs_req_wait_for_reply(req, &reply_len);
432     g_assert_cmpint(reply_len, ==, 0);
433     v9fs_req_free(req);
434     v9fs_rflush(flush_req);
435 
436     g_free(wnames[0]);
437 }
438 
439 static void fs_flush_ignored(void *obj, void *data, QGuestAllocator *t_alloc)
440 {
441     QVirtio9P *v9p = obj;
442     v9fs_set_allocator(t_alloc);
443     char *wnames[] = { g_strdup(QTEST_V9FS_SYNTH_FLUSH_FILE) };
444     P9Req *req, *flush_req;
445     uint32_t count;
446     uint8_t should_block;
447 
448     tattach({ .client = v9p });
449     twalk({
450         .client = v9p, .fid = 0, .newfid = 1, .nwname = 1, .wnames = wnames
451     });
452 
453     req = v9fs_tlopen(v9p, 1, O_WRONLY, 0);
454     v9fs_req_wait_for_reply(req, NULL);
455     v9fs_rlopen(req, NULL, NULL);
456 
457     /* This will cause the write request to complete right away, before it
458      * could be actually cancelled.
459      */
460     should_block = 0;
461     req = v9fs_twrite(v9p, 1, 0, sizeof(should_block), &should_block, 0);
462 
463     flush_req = v9fs_tflush(v9p, req->tag, 1);
464 
465     /* The write request is supposed to complete. The server should
466      * reply to the write request and the flush request.
467      */
468     v9fs_req_wait_for_reply(req, NULL);
469     v9fs_rwrite(req, &count);
470     g_assert_cmpint(count, ==, sizeof(should_block));
471     v9fs_rflush(flush_req);
472 
473     g_free(wnames[0]);
474 }
475 
476 static void do_mkdir(QVirtio9P *v9p, const char *path, const char *cname)
477 {
478     g_autofree char *name = g_strdup(cname);
479     uint32_t fid;
480     P9Req *req;
481 
482     fid = twalk({ .client = v9p, .path = path }).newfid;
483 
484     req = v9fs_tmkdir(v9p, fid, name, 0750, 0, 0);
485     v9fs_req_wait_for_reply(req, NULL);
486     v9fs_rmkdir(req, NULL);
487 }
488 
489 /* create a regular file with Tlcreate and return file's fid */
490 static uint32_t do_lcreate(QVirtio9P *v9p, const char *path,
491                            const char *cname)
492 {
493     g_autofree char *name = g_strdup(cname);
494     uint32_t fid;
495     P9Req *req;
496 
497     fid = twalk({ .client = v9p, .path = path }).newfid;
498 
499     req = v9fs_tlcreate(v9p, fid, name, 0, 0750, 0, 0);
500     v9fs_req_wait_for_reply(req, NULL);
501     v9fs_rlcreate(req, NULL, NULL);
502 
503     return fid;
504 }
505 
506 /* create symlink named @a clink in directory @a path pointing to @a to */
507 static void do_symlink(QVirtio9P *v9p, const char *path, const char *clink,
508                        const char *to)
509 {
510     g_autofree char *name = g_strdup(clink);
511     g_autofree char *dst = g_strdup(to);
512     uint32_t fid;
513     P9Req *req;
514 
515     fid = twalk({ .client = v9p, .path = path }).newfid;
516 
517     req = v9fs_tsymlink(v9p, fid, name, dst, 0, 0);
518     v9fs_req_wait_for_reply(req, NULL);
519     v9fs_rsymlink(req, NULL);
520 }
521 
522 /* create a hard link named @a clink in directory @a path pointing to @a to */
523 static void do_hardlink(QVirtio9P *v9p, const char *path, const char *clink,
524                         const char *to)
525 {
526     uint32_t dfid, fid;
527     P9Req *req;
528 
529     dfid = twalk({ .client = v9p, .path = path }).newfid;
530     fid = twalk({ .client = v9p, .path = to }).newfid;
531 
532     req = v9fs_tlink(v9p, dfid, fid, clink, 0);
533     v9fs_req_wait_for_reply(req, NULL);
534     v9fs_rlink(req);
535 }
536 
537 static void do_unlinkat(QVirtio9P *v9p, const char *atpath, const char *rpath,
538                         uint32_t flags)
539 {
540     g_autofree char *name = g_strdup(rpath);
541     uint32_t fid;
542     P9Req *req;
543 
544     fid = twalk({ .client = v9p, .path = atpath }).newfid;
545 
546     req = v9fs_tunlinkat(v9p, fid, name, flags, 0);
547     v9fs_req_wait_for_reply(req, NULL);
548     v9fs_runlinkat(req);
549 }
550 
551 static void fs_readdir_split_128(void *obj, void *data,
552                                  QGuestAllocator *t_alloc)
553 {
554     v9fs_set_allocator(t_alloc);
555     do_readdir_split(obj, 128);
556 }
557 
558 static void fs_readdir_split_256(void *obj, void *data,
559                                  QGuestAllocator *t_alloc)
560 {
561     v9fs_set_allocator(t_alloc);
562     do_readdir_split(obj, 256);
563 }
564 
565 static void fs_readdir_split_512(void *obj, void *data,
566                                  QGuestAllocator *t_alloc)
567 {
568     v9fs_set_allocator(t_alloc);
569     do_readdir_split(obj, 512);
570 }
571 
572 
573 /* tests using the 9pfs 'local' fs driver */
574 
575 static void fs_create_dir(void *obj, void *data, QGuestAllocator *t_alloc)
576 {
577     QVirtio9P *v9p = obj;
578     v9fs_set_allocator(t_alloc);
579     struct stat st;
580     g_autofree char *root_path = virtio_9p_test_path("");
581     g_autofree char *new_dir = virtio_9p_test_path("01");
582 
583     g_assert(root_path != NULL);
584 
585     tattach({ .client = v9p });
586     do_mkdir(v9p, "/", "01");
587 
588     /* check if created directory really exists now ... */
589     g_assert(stat(new_dir, &st) == 0);
590     /* ... and is actually a directory */
591     g_assert((st.st_mode & S_IFMT) == S_IFDIR);
592 }
593 
594 static void fs_unlinkat_dir(void *obj, void *data, QGuestAllocator *t_alloc)
595 {
596     QVirtio9P *v9p = obj;
597     v9fs_set_allocator(t_alloc);
598     struct stat st;
599     g_autofree char *root_path = virtio_9p_test_path("");
600     g_autofree char *new_dir = virtio_9p_test_path("02");
601 
602     g_assert(root_path != NULL);
603 
604     tattach({ .client = v9p });
605     do_mkdir(v9p, "/", "02");
606 
607     /* check if created directory really exists now ... */
608     g_assert(stat(new_dir, &st) == 0);
609     /* ... and is actually a directory */
610     g_assert((st.st_mode & S_IFMT) == S_IFDIR);
611 
612     do_unlinkat(v9p, "/", "02", P9_DOTL_AT_REMOVEDIR);
613     /* directory should be gone now */
614     g_assert(stat(new_dir, &st) != 0);
615 }
616 
617 static void fs_create_file(void *obj, void *data, QGuestAllocator *t_alloc)
618 {
619     QVirtio9P *v9p = obj;
620     v9fs_set_allocator(t_alloc);
621     struct stat st;
622     g_autofree char *new_file = virtio_9p_test_path("03/1st_file");
623 
624     tattach({ .client = v9p });
625     do_mkdir(v9p, "/", "03");
626     do_lcreate(v9p, "03", "1st_file");
627 
628     /* check if created file exists now ... */
629     g_assert(stat(new_file, &st) == 0);
630     /* ... and is a regular file */
631     g_assert((st.st_mode & S_IFMT) == S_IFREG);
632 }
633 
634 static void fs_unlinkat_file(void *obj, void *data, QGuestAllocator *t_alloc)
635 {
636     QVirtio9P *v9p = obj;
637     v9fs_set_allocator(t_alloc);
638     struct stat st;
639     g_autofree char *new_file = virtio_9p_test_path("04/doa_file");
640 
641     tattach({ .client = v9p });
642     do_mkdir(v9p, "/", "04");
643     do_lcreate(v9p, "04", "doa_file");
644 
645     /* check if created file exists now ... */
646     g_assert(stat(new_file, &st) == 0);
647     /* ... and is a regular file */
648     g_assert((st.st_mode & S_IFMT) == S_IFREG);
649 
650     do_unlinkat(v9p, "04", "doa_file", 0);
651     /* file should be gone now */
652     g_assert(stat(new_file, &st) != 0);
653 }
654 
655 static void fs_symlink_file(void *obj, void *data, QGuestAllocator *t_alloc)
656 {
657     QVirtio9P *v9p = obj;
658     v9fs_set_allocator(t_alloc);
659     struct stat st;
660     g_autofree char *real_file = virtio_9p_test_path("05/real_file");
661     g_autofree char *symlink_file = virtio_9p_test_path("05/symlink_file");
662 
663     tattach({ .client = v9p });
664     do_mkdir(v9p, "/", "05");
665     do_lcreate(v9p, "05", "real_file");
666     g_assert(stat(real_file, &st) == 0);
667     g_assert((st.st_mode & S_IFMT) == S_IFREG);
668 
669     do_symlink(v9p, "05", "symlink_file", "real_file");
670 
671     /* check if created link exists now */
672     g_assert(stat(symlink_file, &st) == 0);
673 }
674 
675 static void fs_unlinkat_symlink(void *obj, void *data,
676                                 QGuestAllocator *t_alloc)
677 {
678     QVirtio9P *v9p = obj;
679     v9fs_set_allocator(t_alloc);
680     struct stat st;
681     g_autofree char *real_file = virtio_9p_test_path("06/real_file");
682     g_autofree char *symlink_file = virtio_9p_test_path("06/symlink_file");
683 
684     tattach({ .client = v9p });
685     do_mkdir(v9p, "/", "06");
686     do_lcreate(v9p, "06", "real_file");
687     g_assert(stat(real_file, &st) == 0);
688     g_assert((st.st_mode & S_IFMT) == S_IFREG);
689 
690     do_symlink(v9p, "06", "symlink_file", "real_file");
691     g_assert(stat(symlink_file, &st) == 0);
692 
693     do_unlinkat(v9p, "06", "symlink_file", 0);
694     /* symlink should be gone now */
695     g_assert(stat(symlink_file, &st) != 0);
696 }
697 
698 static void fs_hardlink_file(void *obj, void *data, QGuestAllocator *t_alloc)
699 {
700     QVirtio9P *v9p = obj;
701     v9fs_set_allocator(t_alloc);
702     struct stat st_real, st_link;
703     g_autofree char *real_file = virtio_9p_test_path("07/real_file");
704     g_autofree char *hardlink_file = virtio_9p_test_path("07/hardlink_file");
705 
706     tattach({ .client = v9p });
707     do_mkdir(v9p, "/", "07");
708     do_lcreate(v9p, "07", "real_file");
709     g_assert(stat(real_file, &st_real) == 0);
710     g_assert((st_real.st_mode & S_IFMT) == S_IFREG);
711 
712     do_hardlink(v9p, "07", "hardlink_file", "07/real_file");
713 
714     /* check if link exists now ... */
715     g_assert(stat(hardlink_file, &st_link) == 0);
716     /* ... and it's a hard link, right? */
717     g_assert((st_link.st_mode & S_IFMT) == S_IFREG);
718     g_assert(st_link.st_dev == st_real.st_dev);
719     g_assert(st_link.st_ino == st_real.st_ino);
720 }
721 
722 static void fs_unlinkat_hardlink(void *obj, void *data,
723                                  QGuestAllocator *t_alloc)
724 {
725     QVirtio9P *v9p = obj;
726     v9fs_set_allocator(t_alloc);
727     struct stat st_real, st_link;
728     g_autofree char *real_file = virtio_9p_test_path("08/real_file");
729     g_autofree char *hardlink_file = virtio_9p_test_path("08/hardlink_file");
730 
731     tattach({ .client = v9p });
732     do_mkdir(v9p, "/", "08");
733     do_lcreate(v9p, "08", "real_file");
734     g_assert(stat(real_file, &st_real) == 0);
735     g_assert((st_real.st_mode & S_IFMT) == S_IFREG);
736 
737     do_hardlink(v9p, "08", "hardlink_file", "08/real_file");
738     g_assert(stat(hardlink_file, &st_link) == 0);
739 
740     do_unlinkat(v9p, "08", "hardlink_file", 0);
741     /* symlink should be gone now */
742     g_assert(stat(hardlink_file, &st_link) != 0);
743     /* and old file should still exist */
744     g_assert(stat(real_file, &st_real) == 0);
745 }
746 
747 static void *assign_9p_local_driver(GString *cmd_line, void *arg)
748 {
749     virtio_9p_assign_local_driver(cmd_line, "security_model=mapped-xattr");
750     return arg;
751 }
752 
753 static void register_virtio_9p_test(void)
754 {
755 
756     QOSGraphTestOptions opts = {
757     };
758 
759     /* 9pfs test cases using the 'synth' filesystem driver */
760     qos_add_test("synth/config", "virtio-9p", pci_config, &opts);
761     qos_add_test("synth/version/basic", "virtio-9p", fs_version,  &opts);
762     qos_add_test("synth/attach/basic", "virtio-9p", fs_attach,  &opts);
763     qos_add_test("synth/walk/basic", "virtio-9p", fs_walk,  &opts);
764     qos_add_test("synth/walk/no_slash", "virtio-9p", fs_walk_no_slash,
765                   &opts);
766     qos_add_test("synth/walk/none", "virtio-9p", fs_walk_none, &opts);
767     qos_add_test("synth/walk/dotdot_from_root", "virtio-9p",
768                  fs_walk_dotdot,  &opts);
769     qos_add_test("synth/walk/non_existent", "virtio-9p", fs_walk_nonexistent,
770                   &opts);
771     qos_add_test("synth/walk/2nd_non_existent", "virtio-9p",
772                  fs_walk_2nd_nonexistent, &opts);
773     qos_add_test("synth/lopen/basic", "virtio-9p", fs_lopen,  &opts);
774     qos_add_test("synth/write/basic", "virtio-9p", fs_write,  &opts);
775     qos_add_test("synth/flush/success", "virtio-9p", fs_flush_success,
776                   &opts);
777     qos_add_test("synth/flush/ignored", "virtio-9p", fs_flush_ignored,
778                   &opts);
779     qos_add_test("synth/readdir/basic", "virtio-9p", fs_readdir,  &opts);
780     qos_add_test("synth/readdir/split_512", "virtio-9p",
781                  fs_readdir_split_512,  &opts);
782     qos_add_test("synth/readdir/split_256", "virtio-9p",
783                  fs_readdir_split_256,  &opts);
784     qos_add_test("synth/readdir/split_128", "virtio-9p",
785                  fs_readdir_split_128,  &opts);
786 
787 
788     /* 9pfs test cases using the 'local' filesystem driver */
789 
790     /*
791      * XXX: Until we are sure that these tests can run everywhere,
792      * keep them as "slow" so that they aren't run with "make check".
793      */
794     if (!g_test_slow()) {
795         return;
796     }
797 
798     opts.before = assign_9p_local_driver;
799     qos_add_test("local/config", "virtio-9p", pci_config,  &opts);
800     qos_add_test("local/create_dir", "virtio-9p", fs_create_dir, &opts);
801     qos_add_test("local/unlinkat_dir", "virtio-9p", fs_unlinkat_dir, &opts);
802     qos_add_test("local/create_file", "virtio-9p", fs_create_file, &opts);
803     qos_add_test("local/unlinkat_file", "virtio-9p", fs_unlinkat_file, &opts);
804     qos_add_test("local/symlink_file", "virtio-9p", fs_symlink_file, &opts);
805     qos_add_test("local/unlinkat_symlink", "virtio-9p", fs_unlinkat_symlink,
806                  &opts);
807     qos_add_test("local/hardlink_file", "virtio-9p", fs_hardlink_file, &opts);
808     qos_add_test("local/unlinkat_hardlink", "virtio-9p", fs_unlinkat_hardlink,
809                  &opts);
810 }
811 
812 libqos_init(register_virtio_9p_test);
813 
814 static void __attribute__((constructor)) construct_9p_test(void)
815 {
816     /* make sure test dir for the 'local' tests exists */
817     virtio_9p_create_local_test_dir();
818 }
819 
820 static void __attribute__((destructor)) destruct_9p_test(void)
821 {
822     /* remove previously created test dir when test suite completed */
823     virtio_9p_remove_local_test_dir();
824 }
825