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