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