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 "libqtest-single.h" 17 #include "qemu/module.h" 18 #include "hw/9pfs/9p.h" 19 #include "hw/9pfs/9p-synth.h" 20 #include "libqos/virtio-9p.h" 21 #include "libqos/qgraph.h" 22 23 #define QVIRTIO_9P_TIMEOUT_US (10 * 1000 * 1000) 24 static QGuestAllocator *alloc; 25 26 /* 27 * Used to auto generate new fids. Start with arbitrary high value to avoid 28 * collision with hard coded fids in basic test code. 29 */ 30 static uint32_t fid_generator = 1000; 31 32 static uint32_t genfid(void) 33 { 34 return fid_generator++; 35 } 36 37 /** 38 * Splits the @a in string by @a delim into individual (non empty) strings 39 * and outputs them to @a out. The output array @a out is NULL terminated. 40 * 41 * Output array @a out must be freed by calling split_free(). 42 * 43 * @returns number of individual elements in output array @a out (without the 44 * final NULL terminating element) 45 */ 46 static int split(const char *in, const char *delim, char ***out) 47 { 48 int n = 0, i = 0; 49 char *tmp, *p; 50 51 tmp = g_strdup(in); 52 for (p = strtok(tmp, delim); p != NULL; p = strtok(NULL, delim)) { 53 if (strlen(p) > 0) { 54 ++n; 55 } 56 } 57 g_free(tmp); 58 59 *out = g_new0(char *, n + 1); /* last element NULL delimiter */ 60 61 tmp = g_strdup(in); 62 for (p = strtok(tmp, delim); p != NULL; p = strtok(NULL, delim)) { 63 if (strlen(p) > 0) { 64 (*out)[i++] = g_strdup(p); 65 } 66 } 67 g_free(tmp); 68 69 return n; 70 } 71 72 static void split_free(char ***out) 73 { 74 int i; 75 for (i = 0; (*out)[i]; ++i) { 76 g_free((*out)[i]); 77 } 78 g_free(*out); 79 *out = NULL; 80 } 81 82 static void pci_config(void *obj, void *data, QGuestAllocator *t_alloc) 83 { 84 QVirtio9P *v9p = obj; 85 alloc = t_alloc; 86 size_t tag_len = qvirtio_config_readw(v9p->vdev, 0); 87 g_autofree char *tag = NULL; 88 int i; 89 90 g_assert_cmpint(tag_len, ==, strlen(MOUNT_TAG)); 91 92 tag = g_malloc(tag_len); 93 for (i = 0; i < tag_len; i++) { 94 tag[i] = qvirtio_config_readb(v9p->vdev, i + 2); 95 } 96 g_assert_cmpmem(tag, tag_len, MOUNT_TAG, tag_len); 97 } 98 99 #define P9_MAX_SIZE 4096 /* Max size of a T-message or R-message */ 100 101 typedef struct { 102 QTestState *qts; 103 QVirtio9P *v9p; 104 uint16_t tag; 105 uint64_t t_msg; 106 uint32_t t_size; 107 uint64_t r_msg; 108 /* No r_size, it is hardcoded to P9_MAX_SIZE */ 109 size_t t_off; 110 size_t r_off; 111 uint32_t free_head; 112 } P9Req; 113 114 static void v9fs_memwrite(P9Req *req, const void *addr, size_t len) 115 { 116 qtest_memwrite(req->qts, req->t_msg + req->t_off, addr, len); 117 req->t_off += len; 118 } 119 120 static void v9fs_memskip(P9Req *req, size_t len) 121 { 122 req->r_off += len; 123 } 124 125 static void v9fs_memread(P9Req *req, void *addr, size_t len) 126 { 127 qtest_memread(req->qts, req->r_msg + req->r_off, addr, len); 128 req->r_off += len; 129 } 130 131 static void v9fs_uint8_read(P9Req *req, uint8_t *val) 132 { 133 v9fs_memread(req, val, 1); 134 } 135 136 static void v9fs_uint16_write(P9Req *req, uint16_t val) 137 { 138 uint16_t le_val = cpu_to_le16(val); 139 140 v9fs_memwrite(req, &le_val, 2); 141 } 142 143 static void v9fs_uint16_read(P9Req *req, uint16_t *val) 144 { 145 v9fs_memread(req, val, 2); 146 le16_to_cpus(val); 147 } 148 149 static void v9fs_uint32_write(P9Req *req, uint32_t val) 150 { 151 uint32_t le_val = cpu_to_le32(val); 152 153 v9fs_memwrite(req, &le_val, 4); 154 } 155 156 static void v9fs_uint64_write(P9Req *req, uint64_t val) 157 { 158 uint64_t le_val = cpu_to_le64(val); 159 160 v9fs_memwrite(req, &le_val, 8); 161 } 162 163 static void v9fs_uint32_read(P9Req *req, uint32_t *val) 164 { 165 v9fs_memread(req, val, 4); 166 le32_to_cpus(val); 167 } 168 169 static void v9fs_uint64_read(P9Req *req, uint64_t *val) 170 { 171 v9fs_memread(req, val, 8); 172 le64_to_cpus(val); 173 } 174 175 /* len[2] string[len] */ 176 static uint16_t v9fs_string_size(const char *string) 177 { 178 size_t len = strlen(string); 179 180 g_assert_cmpint(len, <=, UINT16_MAX - 2); 181 182 return 2 + len; 183 } 184 185 static void v9fs_string_write(P9Req *req, const char *string) 186 { 187 int len = strlen(string); 188 189 g_assert_cmpint(len, <=, UINT16_MAX); 190 191 v9fs_uint16_write(req, (uint16_t) len); 192 v9fs_memwrite(req, string, len); 193 } 194 195 static void v9fs_string_read(P9Req *req, uint16_t *len, char **string) 196 { 197 uint16_t local_len; 198 199 v9fs_uint16_read(req, &local_len); 200 if (len) { 201 *len = local_len; 202 } 203 if (string) { 204 *string = g_malloc(local_len + 1); 205 v9fs_memread(req, *string, local_len); 206 (*string)[local_len] = 0; 207 } else { 208 v9fs_memskip(req, local_len); 209 } 210 } 211 212 typedef struct { 213 uint32_t size; 214 uint8_t id; 215 uint16_t tag; 216 } QEMU_PACKED P9Hdr; 217 218 static P9Req *v9fs_req_init(QVirtio9P *v9p, uint32_t size, uint8_t id, 219 uint16_t tag) 220 { 221 P9Req *req = g_new0(P9Req, 1); 222 uint32_t total_size = 7; /* 9P header has well-known size of 7 bytes */ 223 P9Hdr hdr = { 224 .id = id, 225 .tag = cpu_to_le16(tag) 226 }; 227 228 g_assert_cmpint(total_size, <=, UINT32_MAX - size); 229 total_size += size; 230 hdr.size = cpu_to_le32(total_size); 231 232 g_assert_cmpint(total_size, <=, P9_MAX_SIZE); 233 234 req->qts = global_qtest; 235 req->v9p = v9p; 236 req->t_size = total_size; 237 req->t_msg = guest_alloc(alloc, req->t_size); 238 v9fs_memwrite(req, &hdr, 7); 239 req->tag = tag; 240 return req; 241 } 242 243 static void v9fs_req_send(P9Req *req) 244 { 245 QVirtio9P *v9p = req->v9p; 246 247 req->r_msg = guest_alloc(alloc, P9_MAX_SIZE); 248 req->free_head = qvirtqueue_add(req->qts, v9p->vq, req->t_msg, req->t_size, 249 false, true); 250 qvirtqueue_add(req->qts, v9p->vq, req->r_msg, P9_MAX_SIZE, true, false); 251 qvirtqueue_kick(req->qts, v9p->vdev, v9p->vq, req->free_head); 252 req->t_off = 0; 253 } 254 255 static const char *rmessage_name(uint8_t id) 256 { 257 return 258 id == P9_RLERROR ? "RLERROR" : 259 id == P9_RVERSION ? "RVERSION" : 260 id == P9_RATTACH ? "RATTACH" : 261 id == P9_RWALK ? "RWALK" : 262 id == P9_RLOPEN ? "RLOPEN" : 263 id == P9_RWRITE ? "RWRITE" : 264 id == P9_RMKDIR ? "RMKDIR" : 265 id == P9_RLCREATE ? "RLCREATE" : 266 id == P9_RSYMLINK ? "RSYMLINK" : 267 id == P9_RLINK ? "RLINK" : 268 id == P9_RUNLINKAT ? "RUNLINKAT" : 269 id == P9_RFLUSH ? "RFLUSH" : 270 id == P9_RREADDIR ? "READDIR" : 271 "<unknown>"; 272 } 273 274 static void v9fs_req_wait_for_reply(P9Req *req, uint32_t *len) 275 { 276 QVirtio9P *v9p = req->v9p; 277 278 qvirtio_wait_used_elem(req->qts, v9p->vdev, v9p->vq, req->free_head, len, 279 QVIRTIO_9P_TIMEOUT_US); 280 } 281 282 static void v9fs_req_recv(P9Req *req, uint8_t id) 283 { 284 P9Hdr hdr; 285 286 v9fs_memread(req, &hdr, 7); 287 hdr.size = ldl_le_p(&hdr.size); 288 hdr.tag = lduw_le_p(&hdr.tag); 289 290 g_assert_cmpint(hdr.size, >=, 7); 291 g_assert_cmpint(hdr.size, <=, P9_MAX_SIZE); 292 g_assert_cmpint(hdr.tag, ==, req->tag); 293 294 if (hdr.id != id) { 295 g_printerr("Received response %d (%s) instead of %d (%s)\n", 296 hdr.id, rmessage_name(hdr.id), id, rmessage_name(id)); 297 298 if (hdr.id == P9_RLERROR) { 299 uint32_t err; 300 v9fs_uint32_read(req, &err); 301 g_printerr("Rlerror has errno %d (%s)\n", err, strerror(err)); 302 } 303 } 304 g_assert_cmpint(hdr.id, ==, id); 305 } 306 307 static void v9fs_req_free(P9Req *req) 308 { 309 guest_free(alloc, req->t_msg); 310 guest_free(alloc, req->r_msg); 311 g_free(req); 312 } 313 314 /* size[4] Rlerror tag[2] ecode[4] */ 315 static void v9fs_rlerror(P9Req *req, uint32_t *err) 316 { 317 v9fs_req_recv(req, P9_RLERROR); 318 v9fs_uint32_read(req, err); 319 v9fs_req_free(req); 320 } 321 322 /* size[4] Tversion tag[2] msize[4] version[s] */ 323 static P9Req *v9fs_tversion(QVirtio9P *v9p, uint32_t msize, const char *version, 324 uint16_t tag) 325 { 326 P9Req *req; 327 uint32_t body_size = 4; 328 uint16_t string_size = v9fs_string_size(version); 329 330 g_assert_cmpint(body_size, <=, UINT32_MAX - string_size); 331 body_size += string_size; 332 req = v9fs_req_init(v9p, body_size, P9_TVERSION, tag); 333 334 v9fs_uint32_write(req, msize); 335 v9fs_string_write(req, version); 336 v9fs_req_send(req); 337 return req; 338 } 339 340 /* size[4] Rversion tag[2] msize[4] version[s] */ 341 static void v9fs_rversion(P9Req *req, uint16_t *len, char **version) 342 { 343 uint32_t msize; 344 345 v9fs_req_recv(req, P9_RVERSION); 346 v9fs_uint32_read(req, &msize); 347 348 g_assert_cmpint(msize, ==, P9_MAX_SIZE); 349 350 if (len || version) { 351 v9fs_string_read(req, len, version); 352 } 353 354 v9fs_req_free(req); 355 } 356 357 /* size[4] Tattach tag[2] fid[4] afid[4] uname[s] aname[s] n_uname[4] */ 358 static P9Req *v9fs_tattach(QVirtio9P *v9p, uint32_t fid, uint32_t n_uname, 359 uint16_t tag) 360 { 361 const char *uname = ""; /* ignored by QEMU */ 362 const char *aname = ""; /* ignored by QEMU */ 363 P9Req *req = v9fs_req_init(v9p, 4 + 4 + 2 + 2 + 4, P9_TATTACH, tag); 364 365 v9fs_uint32_write(req, fid); 366 v9fs_uint32_write(req, P9_NOFID); 367 v9fs_string_write(req, uname); 368 v9fs_string_write(req, aname); 369 v9fs_uint32_write(req, n_uname); 370 v9fs_req_send(req); 371 return req; 372 } 373 374 typedef char v9fs_qid[13]; 375 376 /* size[4] Rattach tag[2] qid[13] */ 377 static void v9fs_rattach(P9Req *req, v9fs_qid *qid) 378 { 379 v9fs_req_recv(req, P9_RATTACH); 380 if (qid) { 381 v9fs_memread(req, qid, 13); 382 } 383 v9fs_req_free(req); 384 } 385 386 /* size[4] Twalk tag[2] fid[4] newfid[4] nwname[2] nwname*(wname[s]) */ 387 static P9Req *v9fs_twalk(QVirtio9P *v9p, uint32_t fid, uint32_t newfid, 388 uint16_t nwname, char *const wnames[], uint16_t tag) 389 { 390 P9Req *req; 391 int i; 392 uint32_t body_size = 4 + 4 + 2; 393 394 for (i = 0; i < nwname; i++) { 395 uint16_t wname_size = v9fs_string_size(wnames[i]); 396 397 g_assert_cmpint(body_size, <=, UINT32_MAX - wname_size); 398 body_size += wname_size; 399 } 400 req = v9fs_req_init(v9p, body_size, P9_TWALK, tag); 401 v9fs_uint32_write(req, fid); 402 v9fs_uint32_write(req, newfid); 403 v9fs_uint16_write(req, nwname); 404 for (i = 0; i < nwname; i++) { 405 v9fs_string_write(req, wnames[i]); 406 } 407 v9fs_req_send(req); 408 return req; 409 } 410 411 /* size[4] Rwalk tag[2] nwqid[2] nwqid*(wqid[13]) */ 412 static void v9fs_rwalk(P9Req *req, uint16_t *nwqid, v9fs_qid **wqid) 413 { 414 uint16_t local_nwqid; 415 416 v9fs_req_recv(req, P9_RWALK); 417 v9fs_uint16_read(req, &local_nwqid); 418 if (nwqid) { 419 *nwqid = local_nwqid; 420 } 421 if (wqid) { 422 *wqid = g_malloc(local_nwqid * 13); 423 v9fs_memread(req, *wqid, local_nwqid * 13); 424 } 425 v9fs_req_free(req); 426 } 427 428 /* size[4] Treaddir tag[2] fid[4] offset[8] count[4] */ 429 static P9Req *v9fs_treaddir(QVirtio9P *v9p, uint32_t fid, uint64_t offset, 430 uint32_t count, uint16_t tag) 431 { 432 P9Req *req; 433 434 req = v9fs_req_init(v9p, 4 + 8 + 4, P9_TREADDIR, tag); 435 v9fs_uint32_write(req, fid); 436 v9fs_uint64_write(req, offset); 437 v9fs_uint32_write(req, count); 438 v9fs_req_send(req); 439 return req; 440 } 441 442 struct V9fsDirent { 443 v9fs_qid qid; 444 uint64_t offset; 445 uint8_t type; 446 char *name; 447 struct V9fsDirent *next; 448 }; 449 450 /* size[4] Rreaddir tag[2] count[4] data[count] */ 451 static void v9fs_rreaddir(P9Req *req, uint32_t *count, uint32_t *nentries, 452 struct V9fsDirent **entries) 453 { 454 uint32_t local_count; 455 struct V9fsDirent *e = NULL; 456 uint16_t slen; 457 uint32_t n = 0; 458 459 v9fs_req_recv(req, P9_RREADDIR); 460 v9fs_uint32_read(req, &local_count); 461 462 if (count) { 463 *count = local_count; 464 } 465 466 for (int32_t togo = (int32_t)local_count; 467 togo >= 13 + 8 + 1 + 2; 468 togo -= 13 + 8 + 1 + 2 + slen, ++n) 469 { 470 if (!e) { 471 e = g_new(struct V9fsDirent, 1); 472 if (entries) { 473 *entries = e; 474 } 475 } else { 476 e = e->next = g_new(struct V9fsDirent, 1); 477 } 478 e->next = NULL; 479 /* qid[13] offset[8] type[1] name[s] */ 480 v9fs_memread(req, &e->qid, 13); 481 v9fs_uint64_read(req, &e->offset); 482 v9fs_uint8_read(req, &e->type); 483 v9fs_string_read(req, &slen, &e->name); 484 } 485 486 if (nentries) { 487 *nentries = n; 488 } 489 490 v9fs_req_free(req); 491 } 492 493 static void v9fs_free_dirents(struct V9fsDirent *e) 494 { 495 struct V9fsDirent *next = NULL; 496 497 for (; e; e = next) { 498 next = e->next; 499 g_free(e->name); 500 g_free(e); 501 } 502 } 503 504 /* size[4] Tlopen tag[2] fid[4] flags[4] */ 505 static P9Req *v9fs_tlopen(QVirtio9P *v9p, uint32_t fid, uint32_t flags, 506 uint16_t tag) 507 { 508 P9Req *req; 509 510 req = v9fs_req_init(v9p, 4 + 4, P9_TLOPEN, tag); 511 v9fs_uint32_write(req, fid); 512 v9fs_uint32_write(req, flags); 513 v9fs_req_send(req); 514 return req; 515 } 516 517 /* size[4] Rlopen tag[2] qid[13] iounit[4] */ 518 static void v9fs_rlopen(P9Req *req, v9fs_qid *qid, uint32_t *iounit) 519 { 520 v9fs_req_recv(req, P9_RLOPEN); 521 if (qid) { 522 v9fs_memread(req, qid, 13); 523 } else { 524 v9fs_memskip(req, 13); 525 } 526 if (iounit) { 527 v9fs_uint32_read(req, iounit); 528 } 529 v9fs_req_free(req); 530 } 531 532 /* size[4] Twrite tag[2] fid[4] offset[8] count[4] data[count] */ 533 static P9Req *v9fs_twrite(QVirtio9P *v9p, uint32_t fid, uint64_t offset, 534 uint32_t count, const void *data, uint16_t tag) 535 { 536 P9Req *req; 537 uint32_t body_size = 4 + 8 + 4; 538 539 g_assert_cmpint(body_size, <=, UINT32_MAX - count); 540 body_size += count; 541 req = v9fs_req_init(v9p, body_size, P9_TWRITE, tag); 542 v9fs_uint32_write(req, fid); 543 v9fs_uint64_write(req, offset); 544 v9fs_uint32_write(req, count); 545 v9fs_memwrite(req, data, count); 546 v9fs_req_send(req); 547 return req; 548 } 549 550 /* size[4] Rwrite tag[2] count[4] */ 551 static void v9fs_rwrite(P9Req *req, uint32_t *count) 552 { 553 v9fs_req_recv(req, P9_RWRITE); 554 if (count) { 555 v9fs_uint32_read(req, count); 556 } 557 v9fs_req_free(req); 558 } 559 560 /* size[4] Tflush tag[2] oldtag[2] */ 561 static P9Req *v9fs_tflush(QVirtio9P *v9p, uint16_t oldtag, uint16_t tag) 562 { 563 P9Req *req; 564 565 req = v9fs_req_init(v9p, 2, P9_TFLUSH, tag); 566 v9fs_uint32_write(req, oldtag); 567 v9fs_req_send(req); 568 return req; 569 } 570 571 /* size[4] Rflush tag[2] */ 572 static void v9fs_rflush(P9Req *req) 573 { 574 v9fs_req_recv(req, P9_RFLUSH); 575 v9fs_req_free(req); 576 } 577 578 static void do_version(QVirtio9P *v9p) 579 { 580 const char *version = "9P2000.L"; 581 uint16_t server_len; 582 g_autofree char *server_version = NULL; 583 P9Req *req; 584 585 req = v9fs_tversion(v9p, P9_MAX_SIZE, version, P9_NOTAG); 586 v9fs_req_wait_for_reply(req, NULL); 587 v9fs_rversion(req, &server_len, &server_version); 588 589 g_assert_cmpmem(server_version, server_len, version, strlen(version)); 590 } 591 592 /* utility function: walk to requested dir and return fid for that dir */ 593 static uint32_t do_walk(QVirtio9P *v9p, const char *path) 594 { 595 char **wnames; 596 P9Req *req; 597 const uint32_t fid = genfid(); 598 599 int nwnames = split(path, "/", &wnames); 600 601 req = v9fs_twalk(v9p, 0, fid, nwnames, wnames, 0); 602 v9fs_req_wait_for_reply(req, NULL); 603 v9fs_rwalk(req, NULL, NULL); 604 605 split_free(&wnames); 606 return fid; 607 } 608 609 /* utility function: walk to requested dir and expect passed error response */ 610 static void do_walk_expect_error(QVirtio9P *v9p, const char *path, uint32_t err) 611 { 612 char **wnames; 613 P9Req *req; 614 uint32_t _err; 615 const uint32_t fid = genfid(); 616 617 int nwnames = split(path, "/", &wnames); 618 619 req = v9fs_twalk(v9p, 0, fid, nwnames, wnames, 0); 620 v9fs_req_wait_for_reply(req, NULL); 621 v9fs_rlerror(req, &_err); 622 623 g_assert_cmpint(_err, ==, err); 624 625 split_free(&wnames); 626 } 627 628 static void fs_version(void *obj, void *data, QGuestAllocator *t_alloc) 629 { 630 alloc = t_alloc; 631 do_version(obj); 632 } 633 634 static void do_attach(QVirtio9P *v9p) 635 { 636 P9Req *req; 637 638 do_version(v9p); 639 req = v9fs_tattach(v9p, 0, getuid(), 0); 640 v9fs_req_wait_for_reply(req, NULL); 641 v9fs_rattach(req, NULL); 642 } 643 644 static void fs_attach(void *obj, void *data, QGuestAllocator *t_alloc) 645 { 646 alloc = t_alloc; 647 do_attach(obj); 648 } 649 650 static void fs_walk(void *obj, void *data, QGuestAllocator *t_alloc) 651 { 652 QVirtio9P *v9p = obj; 653 alloc = t_alloc; 654 char *wnames[P9_MAXWELEM]; 655 uint16_t nwqid; 656 g_autofree v9fs_qid *wqid = NULL; 657 int i; 658 P9Req *req; 659 660 for (i = 0; i < P9_MAXWELEM; i++) { 661 wnames[i] = g_strdup_printf(QTEST_V9FS_SYNTH_WALK_FILE, i); 662 } 663 664 do_attach(v9p); 665 req = v9fs_twalk(v9p, 0, 1, P9_MAXWELEM, wnames, 0); 666 v9fs_req_wait_for_reply(req, NULL); 667 v9fs_rwalk(req, &nwqid, &wqid); 668 669 g_assert_cmpint(nwqid, ==, P9_MAXWELEM); 670 671 for (i = 0; i < P9_MAXWELEM; i++) { 672 g_free(wnames[i]); 673 } 674 } 675 676 static bool fs_dirents_contain_name(struct V9fsDirent *e, const char* name) 677 { 678 for (; e; e = e->next) { 679 if (!strcmp(e->name, name)) { 680 return true; 681 } 682 } 683 return false; 684 } 685 686 /* size[4] Tmkdir tag[2] dfid[4] name[s] mode[4] gid[4] */ 687 static P9Req *v9fs_tmkdir(QVirtio9P *v9p, uint32_t dfid, const char *name, 688 uint32_t mode, uint32_t gid, uint16_t tag) 689 { 690 P9Req *req; 691 692 uint32_t body_size = 4 + 4 + 4; 693 uint16_t string_size = v9fs_string_size(name); 694 695 g_assert_cmpint(body_size, <=, UINT32_MAX - string_size); 696 body_size += string_size; 697 698 req = v9fs_req_init(v9p, body_size, P9_TMKDIR, tag); 699 v9fs_uint32_write(req, dfid); 700 v9fs_string_write(req, name); 701 v9fs_uint32_write(req, mode); 702 v9fs_uint32_write(req, gid); 703 v9fs_req_send(req); 704 return req; 705 } 706 707 /* size[4] Rmkdir tag[2] qid[13] */ 708 static void v9fs_rmkdir(P9Req *req, v9fs_qid *qid) 709 { 710 v9fs_req_recv(req, P9_RMKDIR); 711 if (qid) { 712 v9fs_memread(req, qid, 13); 713 } else { 714 v9fs_memskip(req, 13); 715 } 716 v9fs_req_free(req); 717 } 718 719 /* size[4] Tlcreate tag[2] fid[4] name[s] flags[4] mode[4] gid[4] */ 720 static P9Req *v9fs_tlcreate(QVirtio9P *v9p, uint32_t fid, const char *name, 721 uint32_t flags, uint32_t mode, uint32_t gid, 722 uint16_t tag) 723 { 724 P9Req *req; 725 726 uint32_t body_size = 4 + 4 + 4 + 4; 727 uint16_t string_size = v9fs_string_size(name); 728 729 g_assert_cmpint(body_size, <=, UINT32_MAX - string_size); 730 body_size += string_size; 731 732 req = v9fs_req_init(v9p, body_size, P9_TLCREATE, tag); 733 v9fs_uint32_write(req, fid); 734 v9fs_string_write(req, name); 735 v9fs_uint32_write(req, flags); 736 v9fs_uint32_write(req, mode); 737 v9fs_uint32_write(req, gid); 738 v9fs_req_send(req); 739 return req; 740 } 741 742 /* size[4] Rlcreate tag[2] qid[13] iounit[4] */ 743 static void v9fs_rlcreate(P9Req *req, v9fs_qid *qid, uint32_t *iounit) 744 { 745 v9fs_req_recv(req, P9_RLCREATE); 746 if (qid) { 747 v9fs_memread(req, qid, 13); 748 } else { 749 v9fs_memskip(req, 13); 750 } 751 if (iounit) { 752 v9fs_uint32_read(req, iounit); 753 } 754 v9fs_req_free(req); 755 } 756 757 /* size[4] Tsymlink tag[2] fid[4] name[s] symtgt[s] gid[4] */ 758 static P9Req *v9fs_tsymlink(QVirtio9P *v9p, uint32_t fid, const char *name, 759 const char *symtgt, uint32_t gid, uint16_t tag) 760 { 761 P9Req *req; 762 763 uint32_t body_size = 4 + 4; 764 uint16_t string_size = v9fs_string_size(name) + v9fs_string_size(symtgt); 765 766 g_assert_cmpint(body_size, <=, UINT32_MAX - string_size); 767 body_size += string_size; 768 769 req = v9fs_req_init(v9p, body_size, P9_TSYMLINK, tag); 770 v9fs_uint32_write(req, fid); 771 v9fs_string_write(req, name); 772 v9fs_string_write(req, symtgt); 773 v9fs_uint32_write(req, gid); 774 v9fs_req_send(req); 775 return req; 776 } 777 778 /* size[4] Rsymlink tag[2] qid[13] */ 779 static void v9fs_rsymlink(P9Req *req, v9fs_qid *qid) 780 { 781 v9fs_req_recv(req, P9_RSYMLINK); 782 if (qid) { 783 v9fs_memread(req, qid, 13); 784 } else { 785 v9fs_memskip(req, 13); 786 } 787 v9fs_req_free(req); 788 } 789 790 /* size[4] Tlink tag[2] dfid[4] fid[4] name[s] */ 791 static P9Req *v9fs_tlink(QVirtio9P *v9p, uint32_t dfid, uint32_t fid, 792 const char *name, uint16_t tag) 793 { 794 P9Req *req; 795 796 uint32_t body_size = 4 + 4; 797 uint16_t string_size = v9fs_string_size(name); 798 799 g_assert_cmpint(body_size, <=, UINT32_MAX - string_size); 800 body_size += string_size; 801 802 req = v9fs_req_init(v9p, body_size, P9_TLINK, tag); 803 v9fs_uint32_write(req, dfid); 804 v9fs_uint32_write(req, fid); 805 v9fs_string_write(req, name); 806 v9fs_req_send(req); 807 return req; 808 } 809 810 /* size[4] Rlink tag[2] */ 811 static void v9fs_rlink(P9Req *req) 812 { 813 v9fs_req_recv(req, P9_RLINK); 814 v9fs_req_free(req); 815 } 816 817 /* size[4] Tunlinkat tag[2] dirfd[4] name[s] flags[4] */ 818 static P9Req *v9fs_tunlinkat(QVirtio9P *v9p, uint32_t dirfd, const char *name, 819 uint32_t flags, uint16_t tag) 820 { 821 P9Req *req; 822 823 uint32_t body_size = 4 + 4; 824 uint16_t string_size = v9fs_string_size(name); 825 826 g_assert_cmpint(body_size, <=, UINT32_MAX - string_size); 827 body_size += string_size; 828 829 req = v9fs_req_init(v9p, body_size, P9_TUNLINKAT, tag); 830 v9fs_uint32_write(req, dirfd); 831 v9fs_string_write(req, name); 832 v9fs_uint32_write(req, flags); 833 v9fs_req_send(req); 834 return req; 835 } 836 837 /* size[4] Runlinkat tag[2] */ 838 static void v9fs_runlinkat(P9Req *req) 839 { 840 v9fs_req_recv(req, P9_RUNLINKAT); 841 v9fs_req_free(req); 842 } 843 844 /* basic readdir test where reply fits into a single response message */ 845 static void fs_readdir(void *obj, void *data, QGuestAllocator *t_alloc) 846 { 847 QVirtio9P *v9p = obj; 848 alloc = t_alloc; 849 char *const wnames[] = { g_strdup(QTEST_V9FS_SYNTH_READDIR_DIR) }; 850 uint16_t nqid; 851 v9fs_qid qid; 852 uint32_t count, nentries; 853 struct V9fsDirent *entries = NULL; 854 P9Req *req; 855 856 do_attach(v9p); 857 req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0); 858 v9fs_req_wait_for_reply(req, NULL); 859 v9fs_rwalk(req, &nqid, NULL); 860 g_assert_cmpint(nqid, ==, 1); 861 862 req = v9fs_tlopen(v9p, 1, O_DIRECTORY, 0); 863 v9fs_req_wait_for_reply(req, NULL); 864 v9fs_rlopen(req, &qid, NULL); 865 866 /* 867 * submit count = msize - 11, because 11 is the header size of Rreaddir 868 */ 869 req = v9fs_treaddir(v9p, 1, 0, P9_MAX_SIZE - 11, 0); 870 v9fs_req_wait_for_reply(req, NULL); 871 v9fs_rreaddir(req, &count, &nentries, &entries); 872 873 /* 874 * Assuming msize (P9_MAX_SIZE) is large enough so we can retrieve all 875 * dir entries with only one readdir request. 876 */ 877 g_assert_cmpint( 878 nentries, ==, 879 QTEST_V9FS_SYNTH_READDIR_NFILES + 2 /* "." and ".." */ 880 ); 881 882 /* 883 * Check all file names exist in returned entries, ignore their order 884 * though. 885 */ 886 g_assert_cmpint(fs_dirents_contain_name(entries, "."), ==, true); 887 g_assert_cmpint(fs_dirents_contain_name(entries, ".."), ==, true); 888 for (int i = 0; i < QTEST_V9FS_SYNTH_READDIR_NFILES; ++i) { 889 g_autofree char *name = 890 g_strdup_printf(QTEST_V9FS_SYNTH_READDIR_FILE, i); 891 g_assert_cmpint(fs_dirents_contain_name(entries, name), ==, true); 892 } 893 894 v9fs_free_dirents(entries); 895 g_free(wnames[0]); 896 } 897 898 /* readdir test where overall request is split over several messages */ 899 static void do_readdir_split(QVirtio9P *v9p, uint32_t count) 900 { 901 char *const wnames[] = { g_strdup(QTEST_V9FS_SYNTH_READDIR_DIR) }; 902 uint16_t nqid; 903 v9fs_qid qid; 904 uint32_t nentries, npartialentries; 905 struct V9fsDirent *entries, *tail, *partialentries; 906 P9Req *req; 907 int fid; 908 uint64_t offset; 909 910 do_attach(v9p); 911 912 fid = 1; 913 offset = 0; 914 entries = NULL; 915 nentries = 0; 916 tail = NULL; 917 918 req = v9fs_twalk(v9p, 0, fid, 1, wnames, 0); 919 v9fs_req_wait_for_reply(req, NULL); 920 v9fs_rwalk(req, &nqid, NULL); 921 g_assert_cmpint(nqid, ==, 1); 922 923 req = v9fs_tlopen(v9p, fid, O_DIRECTORY, 0); 924 v9fs_req_wait_for_reply(req, NULL); 925 v9fs_rlopen(req, &qid, NULL); 926 927 /* 928 * send as many Treaddir requests as required to get all directory 929 * entries 930 */ 931 while (true) { 932 npartialentries = 0; 933 partialentries = NULL; 934 935 req = v9fs_treaddir(v9p, fid, offset, count, 0); 936 v9fs_req_wait_for_reply(req, NULL); 937 v9fs_rreaddir(req, &count, &npartialentries, &partialentries); 938 if (npartialentries > 0 && partialentries) { 939 if (!entries) { 940 entries = partialentries; 941 nentries = npartialentries; 942 tail = partialentries; 943 } else { 944 tail->next = partialentries; 945 nentries += npartialentries; 946 } 947 while (tail->next) { 948 tail = tail->next; 949 } 950 offset = tail->offset; 951 } else { 952 break; 953 } 954 } 955 956 g_assert_cmpint( 957 nentries, ==, 958 QTEST_V9FS_SYNTH_READDIR_NFILES + 2 /* "." and ".." */ 959 ); 960 961 /* 962 * Check all file names exist in returned entries, ignore their order 963 * though. 964 */ 965 g_assert_cmpint(fs_dirents_contain_name(entries, "."), ==, true); 966 g_assert_cmpint(fs_dirents_contain_name(entries, ".."), ==, true); 967 for (int i = 0; i < QTEST_V9FS_SYNTH_READDIR_NFILES; ++i) { 968 char *name = g_strdup_printf(QTEST_V9FS_SYNTH_READDIR_FILE, i); 969 g_assert_cmpint(fs_dirents_contain_name(entries, name), ==, true); 970 g_free(name); 971 } 972 973 v9fs_free_dirents(entries); 974 975 g_free(wnames[0]); 976 } 977 978 static void fs_walk_no_slash(void *obj, void *data, QGuestAllocator *t_alloc) 979 { 980 QVirtio9P *v9p = obj; 981 alloc = t_alloc; 982 char *const wnames[] = { g_strdup(" /") }; 983 P9Req *req; 984 uint32_t err; 985 986 do_attach(v9p); 987 req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0); 988 v9fs_req_wait_for_reply(req, NULL); 989 v9fs_rlerror(req, &err); 990 991 g_assert_cmpint(err, ==, ENOENT); 992 993 g_free(wnames[0]); 994 } 995 996 static void fs_walk_nonexistent(void *obj, void *data, QGuestAllocator *t_alloc) 997 { 998 QVirtio9P *v9p = obj; 999 alloc = t_alloc; 1000 1001 do_attach(v9p); 1002 do_walk_expect_error(v9p, "non-existent", ENOENT); 1003 } 1004 1005 static void fs_walk_none(void *obj, void *data, QGuestAllocator *t_alloc) 1006 { 1007 QVirtio9P *v9p = obj; 1008 alloc = t_alloc; 1009 v9fs_qid root_qid; 1010 g_autofree v9fs_qid *wqid = NULL; 1011 P9Req *req; 1012 1013 do_version(v9p); 1014 req = v9fs_tattach(v9p, 0, getuid(), 0); 1015 v9fs_req_wait_for_reply(req, NULL); 1016 v9fs_rattach(req, &root_qid); 1017 1018 req = v9fs_twalk(v9p, 0, 1, 0, NULL, 0); 1019 v9fs_req_wait_for_reply(req, NULL); 1020 v9fs_rwalk(req, NULL, &wqid); 1021 1022 /* special case: no QID is returned if nwname=0 was sent */ 1023 g_assert(wqid == NULL); 1024 } 1025 1026 static void fs_walk_dotdot(void *obj, void *data, QGuestAllocator *t_alloc) 1027 { 1028 QVirtio9P *v9p = obj; 1029 alloc = t_alloc; 1030 char *const wnames[] = { g_strdup("..") }; 1031 v9fs_qid root_qid; 1032 g_autofree v9fs_qid *wqid = NULL; 1033 P9Req *req; 1034 1035 do_version(v9p); 1036 req = v9fs_tattach(v9p, 0, getuid(), 0); 1037 v9fs_req_wait_for_reply(req, NULL); 1038 v9fs_rattach(req, &root_qid); 1039 1040 req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0); 1041 v9fs_req_wait_for_reply(req, NULL); 1042 v9fs_rwalk(req, NULL, &wqid); /* We now we'll get one qid */ 1043 1044 g_assert_cmpmem(&root_qid, 13, wqid[0], 13); 1045 1046 g_free(wnames[0]); 1047 } 1048 1049 static void fs_lopen(void *obj, void *data, QGuestAllocator *t_alloc) 1050 { 1051 QVirtio9P *v9p = obj; 1052 alloc = t_alloc; 1053 char *const wnames[] = { g_strdup(QTEST_V9FS_SYNTH_LOPEN_FILE) }; 1054 P9Req *req; 1055 1056 do_attach(v9p); 1057 req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0); 1058 v9fs_req_wait_for_reply(req, NULL); 1059 v9fs_rwalk(req, NULL, NULL); 1060 1061 req = v9fs_tlopen(v9p, 1, O_WRONLY, 0); 1062 v9fs_req_wait_for_reply(req, NULL); 1063 v9fs_rlopen(req, NULL, NULL); 1064 1065 g_free(wnames[0]); 1066 } 1067 1068 static void fs_write(void *obj, void *data, QGuestAllocator *t_alloc) 1069 { 1070 QVirtio9P *v9p = obj; 1071 alloc = t_alloc; 1072 static const uint32_t write_count = P9_MAX_SIZE / 2; 1073 char *const wnames[] = { g_strdup(QTEST_V9FS_SYNTH_WRITE_FILE) }; 1074 g_autofree char *buf = g_malloc0(write_count); 1075 uint32_t count; 1076 P9Req *req; 1077 1078 do_attach(v9p); 1079 req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0); 1080 v9fs_req_wait_for_reply(req, NULL); 1081 v9fs_rwalk(req, NULL, NULL); 1082 1083 req = v9fs_tlopen(v9p, 1, O_WRONLY, 0); 1084 v9fs_req_wait_for_reply(req, NULL); 1085 v9fs_rlopen(req, NULL, NULL); 1086 1087 req = v9fs_twrite(v9p, 1, 0, write_count, buf, 0); 1088 v9fs_req_wait_for_reply(req, NULL); 1089 v9fs_rwrite(req, &count); 1090 g_assert_cmpint(count, ==, write_count); 1091 1092 g_free(wnames[0]); 1093 } 1094 1095 static void fs_flush_success(void *obj, void *data, QGuestAllocator *t_alloc) 1096 { 1097 QVirtio9P *v9p = obj; 1098 alloc = t_alloc; 1099 char *const wnames[] = { g_strdup(QTEST_V9FS_SYNTH_FLUSH_FILE) }; 1100 P9Req *req, *flush_req; 1101 uint32_t reply_len; 1102 uint8_t should_block; 1103 1104 do_attach(v9p); 1105 req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0); 1106 v9fs_req_wait_for_reply(req, NULL); 1107 v9fs_rwalk(req, NULL, NULL); 1108 1109 req = v9fs_tlopen(v9p, 1, O_WRONLY, 0); 1110 v9fs_req_wait_for_reply(req, NULL); 1111 v9fs_rlopen(req, NULL, NULL); 1112 1113 /* This will cause the 9p server to try to write data to the backend, 1114 * until the write request gets cancelled. 1115 */ 1116 should_block = 1; 1117 req = v9fs_twrite(v9p, 1, 0, sizeof(should_block), &should_block, 0); 1118 1119 flush_req = v9fs_tflush(v9p, req->tag, 1); 1120 1121 /* The write request is supposed to be flushed: the server should just 1122 * mark the write request as used and reply to the flush request. 1123 */ 1124 v9fs_req_wait_for_reply(req, &reply_len); 1125 g_assert_cmpint(reply_len, ==, 0); 1126 v9fs_req_free(req); 1127 v9fs_rflush(flush_req); 1128 1129 g_free(wnames[0]); 1130 } 1131 1132 static void fs_flush_ignored(void *obj, void *data, QGuestAllocator *t_alloc) 1133 { 1134 QVirtio9P *v9p = obj; 1135 alloc = t_alloc; 1136 char *const wnames[] = { g_strdup(QTEST_V9FS_SYNTH_FLUSH_FILE) }; 1137 P9Req *req, *flush_req; 1138 uint32_t count; 1139 uint8_t should_block; 1140 1141 do_attach(v9p); 1142 req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0); 1143 v9fs_req_wait_for_reply(req, NULL); 1144 v9fs_rwalk(req, NULL, NULL); 1145 1146 req = v9fs_tlopen(v9p, 1, O_WRONLY, 0); 1147 v9fs_req_wait_for_reply(req, NULL); 1148 v9fs_rlopen(req, NULL, NULL); 1149 1150 /* This will cause the write request to complete right away, before it 1151 * could be actually cancelled. 1152 */ 1153 should_block = 0; 1154 req = v9fs_twrite(v9p, 1, 0, sizeof(should_block), &should_block, 0); 1155 1156 flush_req = v9fs_tflush(v9p, req->tag, 1); 1157 1158 /* The write request is supposed to complete. The server should 1159 * reply to the write request and the flush request. 1160 */ 1161 v9fs_req_wait_for_reply(req, NULL); 1162 v9fs_rwrite(req, &count); 1163 g_assert_cmpint(count, ==, sizeof(should_block)); 1164 v9fs_rflush(flush_req); 1165 1166 g_free(wnames[0]); 1167 } 1168 1169 static void do_mkdir(QVirtio9P *v9p, const char *path, const char *cname) 1170 { 1171 g_autofree char *name = g_strdup(cname); 1172 uint32_t fid; 1173 P9Req *req; 1174 1175 fid = do_walk(v9p, path); 1176 1177 req = v9fs_tmkdir(v9p, fid, name, 0750, 0, 0); 1178 v9fs_req_wait_for_reply(req, NULL); 1179 v9fs_rmkdir(req, NULL); 1180 } 1181 1182 /* create a regular file with Tlcreate and return file's fid */ 1183 static uint32_t do_lcreate(QVirtio9P *v9p, const char *path, 1184 const char *cname) 1185 { 1186 g_autofree char *name = g_strdup(cname); 1187 uint32_t fid; 1188 P9Req *req; 1189 1190 fid = do_walk(v9p, path); 1191 1192 req = v9fs_tlcreate(v9p, fid, name, 0, 0750, 0, 0); 1193 v9fs_req_wait_for_reply(req, NULL); 1194 v9fs_rlcreate(req, NULL, NULL); 1195 1196 return fid; 1197 } 1198 1199 /* create symlink named @a clink in directory @a path pointing to @a to */ 1200 static void do_symlink(QVirtio9P *v9p, const char *path, const char *clink, 1201 const char *to) 1202 { 1203 g_autofree char *name = g_strdup(clink); 1204 g_autofree char *dst = g_strdup(to); 1205 uint32_t fid; 1206 P9Req *req; 1207 1208 fid = do_walk(v9p, path); 1209 1210 req = v9fs_tsymlink(v9p, fid, name, dst, 0, 0); 1211 v9fs_req_wait_for_reply(req, NULL); 1212 v9fs_rsymlink(req, NULL); 1213 } 1214 1215 /* create a hard link named @a clink in directory @a path pointing to @a to */ 1216 static void do_hardlink(QVirtio9P *v9p, const char *path, const char *clink, 1217 const char *to) 1218 { 1219 uint32_t dfid, fid; 1220 P9Req *req; 1221 1222 dfid = do_walk(v9p, path); 1223 fid = do_walk(v9p, to); 1224 1225 req = v9fs_tlink(v9p, dfid, fid, clink, 0); 1226 v9fs_req_wait_for_reply(req, NULL); 1227 v9fs_rlink(req); 1228 } 1229 1230 static void do_unlinkat(QVirtio9P *v9p, const char *atpath, const char *rpath, 1231 uint32_t flags) 1232 { 1233 g_autofree char *name = g_strdup(rpath); 1234 uint32_t fid; 1235 P9Req *req; 1236 1237 fid = do_walk(v9p, atpath); 1238 1239 req = v9fs_tunlinkat(v9p, fid, name, flags, 0); 1240 v9fs_req_wait_for_reply(req, NULL); 1241 v9fs_runlinkat(req); 1242 } 1243 1244 static void fs_readdir_split_128(void *obj, void *data, 1245 QGuestAllocator *t_alloc) 1246 { 1247 alloc = t_alloc; 1248 do_readdir_split(obj, 128); 1249 } 1250 1251 static void fs_readdir_split_256(void *obj, void *data, 1252 QGuestAllocator *t_alloc) 1253 { 1254 alloc = t_alloc; 1255 do_readdir_split(obj, 256); 1256 } 1257 1258 static void fs_readdir_split_512(void *obj, void *data, 1259 QGuestAllocator *t_alloc) 1260 { 1261 alloc = t_alloc; 1262 do_readdir_split(obj, 512); 1263 } 1264 1265 1266 /* tests using the 9pfs 'local' fs driver */ 1267 1268 static void fs_create_dir(void *obj, void *data, QGuestAllocator *t_alloc) 1269 { 1270 QVirtio9P *v9p = obj; 1271 alloc = t_alloc; 1272 struct stat st; 1273 g_autofree char *root_path = virtio_9p_test_path(""); 1274 g_autofree char *new_dir = virtio_9p_test_path("01"); 1275 1276 g_assert(root_path != NULL); 1277 1278 do_attach(v9p); 1279 do_mkdir(v9p, "/", "01"); 1280 1281 /* check if created directory really exists now ... */ 1282 g_assert(stat(new_dir, &st) == 0); 1283 /* ... and is actually a directory */ 1284 g_assert((st.st_mode & S_IFMT) == S_IFDIR); 1285 } 1286 1287 static void fs_unlinkat_dir(void *obj, void *data, QGuestAllocator *t_alloc) 1288 { 1289 QVirtio9P *v9p = obj; 1290 alloc = t_alloc; 1291 struct stat st; 1292 g_autofree char *root_path = virtio_9p_test_path(""); 1293 g_autofree char *new_dir = virtio_9p_test_path("02"); 1294 1295 g_assert(root_path != NULL); 1296 1297 do_attach(v9p); 1298 do_mkdir(v9p, "/", "02"); 1299 1300 /* check if created directory really exists now ... */ 1301 g_assert(stat(new_dir, &st) == 0); 1302 /* ... and is actually a directory */ 1303 g_assert((st.st_mode & S_IFMT) == S_IFDIR); 1304 1305 do_unlinkat(v9p, "/", "02", P9_DOTL_AT_REMOVEDIR); 1306 /* directory should be gone now */ 1307 g_assert(stat(new_dir, &st) != 0); 1308 } 1309 1310 static void fs_create_file(void *obj, void *data, QGuestAllocator *t_alloc) 1311 { 1312 QVirtio9P *v9p = obj; 1313 alloc = t_alloc; 1314 struct stat st; 1315 g_autofree char *new_file = virtio_9p_test_path("03/1st_file"); 1316 1317 do_attach(v9p); 1318 do_mkdir(v9p, "/", "03"); 1319 do_lcreate(v9p, "03", "1st_file"); 1320 1321 /* check if created file exists now ... */ 1322 g_assert(stat(new_file, &st) == 0); 1323 /* ... and is a regular file */ 1324 g_assert((st.st_mode & S_IFMT) == S_IFREG); 1325 } 1326 1327 static void fs_unlinkat_file(void *obj, void *data, QGuestAllocator *t_alloc) 1328 { 1329 QVirtio9P *v9p = obj; 1330 alloc = t_alloc; 1331 struct stat st; 1332 g_autofree char *new_file = virtio_9p_test_path("04/doa_file"); 1333 1334 do_attach(v9p); 1335 do_mkdir(v9p, "/", "04"); 1336 do_lcreate(v9p, "04", "doa_file"); 1337 1338 /* check if created file exists now ... */ 1339 g_assert(stat(new_file, &st) == 0); 1340 /* ... and is a regular file */ 1341 g_assert((st.st_mode & S_IFMT) == S_IFREG); 1342 1343 do_unlinkat(v9p, "04", "doa_file", 0); 1344 /* file should be gone now */ 1345 g_assert(stat(new_file, &st) != 0); 1346 } 1347 1348 static void fs_symlink_file(void *obj, void *data, QGuestAllocator *t_alloc) 1349 { 1350 QVirtio9P *v9p = obj; 1351 alloc = t_alloc; 1352 struct stat st; 1353 g_autofree char *real_file = virtio_9p_test_path("05/real_file"); 1354 g_autofree char *symlink_file = virtio_9p_test_path("05/symlink_file"); 1355 1356 do_attach(v9p); 1357 do_mkdir(v9p, "/", "05"); 1358 do_lcreate(v9p, "05", "real_file"); 1359 g_assert(stat(real_file, &st) == 0); 1360 g_assert((st.st_mode & S_IFMT) == S_IFREG); 1361 1362 do_symlink(v9p, "05", "symlink_file", "real_file"); 1363 1364 /* check if created link exists now */ 1365 g_assert(stat(symlink_file, &st) == 0); 1366 } 1367 1368 static void fs_unlinkat_symlink(void *obj, void *data, 1369 QGuestAllocator *t_alloc) 1370 { 1371 QVirtio9P *v9p = obj; 1372 alloc = t_alloc; 1373 struct stat st; 1374 g_autofree char *real_file = virtio_9p_test_path("06/real_file"); 1375 g_autofree char *symlink_file = virtio_9p_test_path("06/symlink_file"); 1376 1377 do_attach(v9p); 1378 do_mkdir(v9p, "/", "06"); 1379 do_lcreate(v9p, "06", "real_file"); 1380 g_assert(stat(real_file, &st) == 0); 1381 g_assert((st.st_mode & S_IFMT) == S_IFREG); 1382 1383 do_symlink(v9p, "06", "symlink_file", "real_file"); 1384 g_assert(stat(symlink_file, &st) == 0); 1385 1386 do_unlinkat(v9p, "06", "symlink_file", 0); 1387 /* symlink should be gone now */ 1388 g_assert(stat(symlink_file, &st) != 0); 1389 } 1390 1391 static void fs_hardlink_file(void *obj, void *data, QGuestAllocator *t_alloc) 1392 { 1393 QVirtio9P *v9p = obj; 1394 alloc = t_alloc; 1395 struct stat st_real, st_link; 1396 g_autofree char *real_file = virtio_9p_test_path("07/real_file"); 1397 g_autofree char *hardlink_file = virtio_9p_test_path("07/hardlink_file"); 1398 1399 do_attach(v9p); 1400 do_mkdir(v9p, "/", "07"); 1401 do_lcreate(v9p, "07", "real_file"); 1402 g_assert(stat(real_file, &st_real) == 0); 1403 g_assert((st_real.st_mode & S_IFMT) == S_IFREG); 1404 1405 do_hardlink(v9p, "07", "hardlink_file", "07/real_file"); 1406 1407 /* check if link exists now ... */ 1408 g_assert(stat(hardlink_file, &st_link) == 0); 1409 /* ... and it's a hard link, right? */ 1410 g_assert((st_link.st_mode & S_IFMT) == S_IFREG); 1411 g_assert(st_link.st_dev == st_real.st_dev); 1412 g_assert(st_link.st_ino == st_real.st_ino); 1413 } 1414 1415 static void fs_unlinkat_hardlink(void *obj, void *data, 1416 QGuestAllocator *t_alloc) 1417 { 1418 QVirtio9P *v9p = obj; 1419 alloc = t_alloc; 1420 struct stat st_real, st_link; 1421 g_autofree char *real_file = virtio_9p_test_path("08/real_file"); 1422 g_autofree char *hardlink_file = virtio_9p_test_path("08/hardlink_file"); 1423 1424 do_attach(v9p); 1425 do_mkdir(v9p, "/", "08"); 1426 do_lcreate(v9p, "08", "real_file"); 1427 g_assert(stat(real_file, &st_real) == 0); 1428 g_assert((st_real.st_mode & S_IFMT) == S_IFREG); 1429 1430 do_hardlink(v9p, "08", "hardlink_file", "08/real_file"); 1431 g_assert(stat(hardlink_file, &st_link) == 0); 1432 1433 do_unlinkat(v9p, "08", "hardlink_file", 0); 1434 /* symlink should be gone now */ 1435 g_assert(stat(hardlink_file, &st_link) != 0); 1436 /* and old file should still exist */ 1437 g_assert(stat(real_file, &st_real) == 0); 1438 } 1439 1440 static void *assign_9p_local_driver(GString *cmd_line, void *arg) 1441 { 1442 virtio_9p_assign_local_driver(cmd_line, "security_model=mapped-xattr"); 1443 return arg; 1444 } 1445 1446 static void register_virtio_9p_test(void) 1447 { 1448 1449 QOSGraphTestOptions opts = { 1450 }; 1451 1452 /* 9pfs test cases using the 'synth' filesystem driver */ 1453 qos_add_test("synth/config", "virtio-9p", pci_config, &opts); 1454 qos_add_test("synth/version/basic", "virtio-9p", fs_version, &opts); 1455 qos_add_test("synth/attach/basic", "virtio-9p", fs_attach, &opts); 1456 qos_add_test("synth/walk/basic", "virtio-9p", fs_walk, &opts); 1457 qos_add_test("synth/walk/no_slash", "virtio-9p", fs_walk_no_slash, 1458 &opts); 1459 qos_add_test("synth/walk/none", "virtio-9p", fs_walk_none, &opts); 1460 qos_add_test("synth/walk/dotdot_from_root", "virtio-9p", 1461 fs_walk_dotdot, &opts); 1462 qos_add_test("synth/walk/non_existent", "virtio-9p", fs_walk_nonexistent, 1463 &opts); 1464 qos_add_test("synth/lopen/basic", "virtio-9p", fs_lopen, &opts); 1465 qos_add_test("synth/write/basic", "virtio-9p", fs_write, &opts); 1466 qos_add_test("synth/flush/success", "virtio-9p", fs_flush_success, 1467 &opts); 1468 qos_add_test("synth/flush/ignored", "virtio-9p", fs_flush_ignored, 1469 &opts); 1470 qos_add_test("synth/readdir/basic", "virtio-9p", fs_readdir, &opts); 1471 qos_add_test("synth/readdir/split_512", "virtio-9p", 1472 fs_readdir_split_512, &opts); 1473 qos_add_test("synth/readdir/split_256", "virtio-9p", 1474 fs_readdir_split_256, &opts); 1475 qos_add_test("synth/readdir/split_128", "virtio-9p", 1476 fs_readdir_split_128, &opts); 1477 1478 1479 /* 9pfs test cases using the 'local' filesystem driver */ 1480 1481 /* 1482 * XXX: Until we are sure that these tests can run everywhere, 1483 * keep them as "slow" so that they aren't run with "make check". 1484 */ 1485 if (!g_test_slow()) { 1486 return; 1487 } 1488 1489 opts.before = assign_9p_local_driver; 1490 qos_add_test("local/config", "virtio-9p", pci_config, &opts); 1491 qos_add_test("local/create_dir", "virtio-9p", fs_create_dir, &opts); 1492 qos_add_test("local/unlinkat_dir", "virtio-9p", fs_unlinkat_dir, &opts); 1493 qos_add_test("local/create_file", "virtio-9p", fs_create_file, &opts); 1494 qos_add_test("local/unlinkat_file", "virtio-9p", fs_unlinkat_file, &opts); 1495 qos_add_test("local/symlink_file", "virtio-9p", fs_symlink_file, &opts); 1496 qos_add_test("local/unlinkat_symlink", "virtio-9p", fs_unlinkat_symlink, 1497 &opts); 1498 qos_add_test("local/hardlink_file", "virtio-9p", fs_hardlink_file, &opts); 1499 qos_add_test("local/unlinkat_hardlink", "virtio-9p", fs_unlinkat_hardlink, 1500 &opts); 1501 } 1502 1503 libqos_init(register_virtio_9p_test); 1504 1505 static void __attribute__((constructor)) construct_9p_test(void) 1506 { 1507 /* make sure test dir for the 'local' tests exists */ 1508 virtio_9p_create_local_test_dir(); 1509 } 1510 1511 static void __attribute__((destructor)) destruct_9p_test(void) 1512 { 1513 /* remove previously created test dir when test suite completed */ 1514 virtio_9p_remove_local_test_dir(); 1515 } 1516