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