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