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_malloc(sizeof(struct V9fsDirent)); 472 if (entries) { 473 *entries = e; 474 } 475 } else { 476 e = e->next = g_malloc(sizeof(struct V9fsDirent)); 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 static void fs_version(void *obj, void *data, QGuestAllocator *t_alloc) 610 { 611 alloc = t_alloc; 612 do_version(obj); 613 } 614 615 static void do_attach(QVirtio9P *v9p) 616 { 617 P9Req *req; 618 619 do_version(v9p); 620 req = v9fs_tattach(v9p, 0, getuid(), 0); 621 v9fs_req_wait_for_reply(req, NULL); 622 v9fs_rattach(req, NULL); 623 } 624 625 static void fs_attach(void *obj, void *data, QGuestAllocator *t_alloc) 626 { 627 alloc = t_alloc; 628 do_attach(obj); 629 } 630 631 static void fs_walk(void *obj, void *data, QGuestAllocator *t_alloc) 632 { 633 QVirtio9P *v9p = obj; 634 alloc = t_alloc; 635 char *wnames[P9_MAXWELEM]; 636 uint16_t nwqid; 637 g_autofree v9fs_qid *wqid = NULL; 638 int i; 639 P9Req *req; 640 641 for (i = 0; i < P9_MAXWELEM; i++) { 642 wnames[i] = g_strdup_printf(QTEST_V9FS_SYNTH_WALK_FILE, i); 643 } 644 645 do_attach(v9p); 646 req = v9fs_twalk(v9p, 0, 1, P9_MAXWELEM, wnames, 0); 647 v9fs_req_wait_for_reply(req, NULL); 648 v9fs_rwalk(req, &nwqid, &wqid); 649 650 g_assert_cmpint(nwqid, ==, P9_MAXWELEM); 651 652 for (i = 0; i < P9_MAXWELEM; i++) { 653 g_free(wnames[i]); 654 } 655 } 656 657 static bool fs_dirents_contain_name(struct V9fsDirent *e, const char* name) 658 { 659 for (; e; e = e->next) { 660 if (!strcmp(e->name, name)) { 661 return true; 662 } 663 } 664 return false; 665 } 666 667 /* size[4] Tmkdir tag[2] dfid[4] name[s] mode[4] gid[4] */ 668 static P9Req *v9fs_tmkdir(QVirtio9P *v9p, uint32_t dfid, const char *name, 669 uint32_t mode, uint32_t gid, uint16_t tag) 670 { 671 P9Req *req; 672 673 uint32_t body_size = 4 + 4 + 4; 674 uint16_t string_size = v9fs_string_size(name); 675 676 g_assert_cmpint(body_size, <=, UINT32_MAX - string_size); 677 body_size += string_size; 678 679 req = v9fs_req_init(v9p, body_size, P9_TMKDIR, tag); 680 v9fs_uint32_write(req, dfid); 681 v9fs_string_write(req, name); 682 v9fs_uint32_write(req, mode); 683 v9fs_uint32_write(req, gid); 684 v9fs_req_send(req); 685 return req; 686 } 687 688 /* size[4] Rmkdir tag[2] qid[13] */ 689 static void v9fs_rmkdir(P9Req *req, v9fs_qid *qid) 690 { 691 v9fs_req_recv(req, P9_RMKDIR); 692 if (qid) { 693 v9fs_memread(req, qid, 13); 694 } else { 695 v9fs_memskip(req, 13); 696 } 697 v9fs_req_free(req); 698 } 699 700 /* size[4] Tlcreate tag[2] fid[4] name[s] flags[4] mode[4] gid[4] */ 701 static P9Req *v9fs_tlcreate(QVirtio9P *v9p, uint32_t fid, const char *name, 702 uint32_t flags, uint32_t mode, uint32_t gid, 703 uint16_t tag) 704 { 705 P9Req *req; 706 707 uint32_t body_size = 4 + 4 + 4 + 4; 708 uint16_t string_size = v9fs_string_size(name); 709 710 g_assert_cmpint(body_size, <=, UINT32_MAX - string_size); 711 body_size += string_size; 712 713 req = v9fs_req_init(v9p, body_size, P9_TLCREATE, tag); 714 v9fs_uint32_write(req, fid); 715 v9fs_string_write(req, name); 716 v9fs_uint32_write(req, flags); 717 v9fs_uint32_write(req, mode); 718 v9fs_uint32_write(req, gid); 719 v9fs_req_send(req); 720 return req; 721 } 722 723 /* size[4] Rlcreate tag[2] qid[13] iounit[4] */ 724 static void v9fs_rlcreate(P9Req *req, v9fs_qid *qid, uint32_t *iounit) 725 { 726 v9fs_req_recv(req, P9_RLCREATE); 727 if (qid) { 728 v9fs_memread(req, qid, 13); 729 } else { 730 v9fs_memskip(req, 13); 731 } 732 if (iounit) { 733 v9fs_uint32_read(req, iounit); 734 } 735 v9fs_req_free(req); 736 } 737 738 /* size[4] Tsymlink tag[2] fid[4] name[s] symtgt[s] gid[4] */ 739 static P9Req *v9fs_tsymlink(QVirtio9P *v9p, uint32_t fid, const char *name, 740 const char *symtgt, uint32_t gid, uint16_t tag) 741 { 742 P9Req *req; 743 744 uint32_t body_size = 4 + 4; 745 uint16_t string_size = v9fs_string_size(name) + v9fs_string_size(symtgt); 746 747 g_assert_cmpint(body_size, <=, UINT32_MAX - string_size); 748 body_size += string_size; 749 750 req = v9fs_req_init(v9p, body_size, P9_TSYMLINK, tag); 751 v9fs_uint32_write(req, fid); 752 v9fs_string_write(req, name); 753 v9fs_string_write(req, symtgt); 754 v9fs_uint32_write(req, gid); 755 v9fs_req_send(req); 756 return req; 757 } 758 759 /* size[4] Rsymlink tag[2] qid[13] */ 760 static void v9fs_rsymlink(P9Req *req, v9fs_qid *qid) 761 { 762 v9fs_req_recv(req, P9_RSYMLINK); 763 if (qid) { 764 v9fs_memread(req, qid, 13); 765 } else { 766 v9fs_memskip(req, 13); 767 } 768 v9fs_req_free(req); 769 } 770 771 /* size[4] Tlink tag[2] dfid[4] fid[4] name[s] */ 772 static P9Req *v9fs_tlink(QVirtio9P *v9p, uint32_t dfid, uint32_t fid, 773 const char *name, uint16_t tag) 774 { 775 P9Req *req; 776 777 uint32_t body_size = 4 + 4; 778 uint16_t string_size = v9fs_string_size(name); 779 780 g_assert_cmpint(body_size, <=, UINT32_MAX - string_size); 781 body_size += string_size; 782 783 req = v9fs_req_init(v9p, body_size, P9_TLINK, tag); 784 v9fs_uint32_write(req, dfid); 785 v9fs_uint32_write(req, fid); 786 v9fs_string_write(req, name); 787 v9fs_req_send(req); 788 return req; 789 } 790 791 /* size[4] Rlink tag[2] */ 792 static void v9fs_rlink(P9Req *req) 793 { 794 v9fs_req_recv(req, P9_RLINK); 795 v9fs_req_free(req); 796 } 797 798 /* size[4] Tunlinkat tag[2] dirfd[4] name[s] flags[4] */ 799 static P9Req *v9fs_tunlinkat(QVirtio9P *v9p, uint32_t dirfd, const char *name, 800 uint32_t flags, uint16_t tag) 801 { 802 P9Req *req; 803 804 uint32_t body_size = 4 + 4; 805 uint16_t string_size = v9fs_string_size(name); 806 807 g_assert_cmpint(body_size, <=, UINT32_MAX - string_size); 808 body_size += string_size; 809 810 req = v9fs_req_init(v9p, body_size, P9_TUNLINKAT, tag); 811 v9fs_uint32_write(req, dirfd); 812 v9fs_string_write(req, name); 813 v9fs_uint32_write(req, flags); 814 v9fs_req_send(req); 815 return req; 816 } 817 818 /* size[4] Runlinkat tag[2] */ 819 static void v9fs_runlinkat(P9Req *req) 820 { 821 v9fs_req_recv(req, P9_RUNLINKAT); 822 v9fs_req_free(req); 823 } 824 825 /* basic readdir test where reply fits into a single response message */ 826 static void fs_readdir(void *obj, void *data, QGuestAllocator *t_alloc) 827 { 828 QVirtio9P *v9p = obj; 829 alloc = t_alloc; 830 char *const wnames[] = { g_strdup(QTEST_V9FS_SYNTH_READDIR_DIR) }; 831 uint16_t nqid; 832 v9fs_qid qid; 833 uint32_t count, nentries; 834 struct V9fsDirent *entries = NULL; 835 P9Req *req; 836 837 do_attach(v9p); 838 req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0); 839 v9fs_req_wait_for_reply(req, NULL); 840 v9fs_rwalk(req, &nqid, NULL); 841 g_assert_cmpint(nqid, ==, 1); 842 843 req = v9fs_tlopen(v9p, 1, O_DIRECTORY, 0); 844 v9fs_req_wait_for_reply(req, NULL); 845 v9fs_rlopen(req, &qid, NULL); 846 847 /* 848 * submit count = msize - 11, because 11 is the header size of Rreaddir 849 */ 850 req = v9fs_treaddir(v9p, 1, 0, P9_MAX_SIZE - 11, 0); 851 v9fs_req_wait_for_reply(req, NULL); 852 v9fs_rreaddir(req, &count, &nentries, &entries); 853 854 /* 855 * Assuming msize (P9_MAX_SIZE) is large enough so we can retrieve all 856 * dir entries with only one readdir request. 857 */ 858 g_assert_cmpint( 859 nentries, ==, 860 QTEST_V9FS_SYNTH_READDIR_NFILES + 2 /* "." and ".." */ 861 ); 862 863 /* 864 * Check all file names exist in returned entries, ignore their order 865 * though. 866 */ 867 g_assert_cmpint(fs_dirents_contain_name(entries, "."), ==, true); 868 g_assert_cmpint(fs_dirents_contain_name(entries, ".."), ==, true); 869 for (int i = 0; i < QTEST_V9FS_SYNTH_READDIR_NFILES; ++i) { 870 g_autofree char *name = 871 g_strdup_printf(QTEST_V9FS_SYNTH_READDIR_FILE, i); 872 g_assert_cmpint(fs_dirents_contain_name(entries, name), ==, true); 873 } 874 875 v9fs_free_dirents(entries); 876 g_free(wnames[0]); 877 } 878 879 /* readdir test where overall request is split over several messages */ 880 static void do_readdir_split(QVirtio9P *v9p, uint32_t count) 881 { 882 char *const wnames[] = { g_strdup(QTEST_V9FS_SYNTH_READDIR_DIR) }; 883 uint16_t nqid; 884 v9fs_qid qid; 885 uint32_t nentries, npartialentries; 886 struct V9fsDirent *entries, *tail, *partialentries; 887 P9Req *req; 888 int fid; 889 uint64_t offset; 890 891 do_attach(v9p); 892 893 fid = 1; 894 offset = 0; 895 entries = NULL; 896 nentries = 0; 897 tail = NULL; 898 899 req = v9fs_twalk(v9p, 0, fid, 1, wnames, 0); 900 v9fs_req_wait_for_reply(req, NULL); 901 v9fs_rwalk(req, &nqid, NULL); 902 g_assert_cmpint(nqid, ==, 1); 903 904 req = v9fs_tlopen(v9p, fid, O_DIRECTORY, 0); 905 v9fs_req_wait_for_reply(req, NULL); 906 v9fs_rlopen(req, &qid, NULL); 907 908 /* 909 * send as many Treaddir requests as required to get all directory 910 * entries 911 */ 912 while (true) { 913 npartialentries = 0; 914 partialentries = NULL; 915 916 req = v9fs_treaddir(v9p, fid, offset, count, 0); 917 v9fs_req_wait_for_reply(req, NULL); 918 v9fs_rreaddir(req, &count, &npartialentries, &partialentries); 919 if (npartialentries > 0 && partialentries) { 920 if (!entries) { 921 entries = partialentries; 922 nentries = npartialentries; 923 tail = partialentries; 924 } else { 925 tail->next = partialentries; 926 nentries += npartialentries; 927 } 928 while (tail->next) { 929 tail = tail->next; 930 } 931 offset = tail->offset; 932 } else { 933 break; 934 } 935 } 936 937 g_assert_cmpint( 938 nentries, ==, 939 QTEST_V9FS_SYNTH_READDIR_NFILES + 2 /* "." and ".." */ 940 ); 941 942 /* 943 * Check all file names exist in returned entries, ignore their order 944 * though. 945 */ 946 g_assert_cmpint(fs_dirents_contain_name(entries, "."), ==, true); 947 g_assert_cmpint(fs_dirents_contain_name(entries, ".."), ==, true); 948 for (int i = 0; i < QTEST_V9FS_SYNTH_READDIR_NFILES; ++i) { 949 char *name = g_strdup_printf(QTEST_V9FS_SYNTH_READDIR_FILE, i); 950 g_assert_cmpint(fs_dirents_contain_name(entries, name), ==, true); 951 g_free(name); 952 } 953 954 v9fs_free_dirents(entries); 955 956 g_free(wnames[0]); 957 } 958 959 static void fs_walk_no_slash(void *obj, void *data, QGuestAllocator *t_alloc) 960 { 961 QVirtio9P *v9p = obj; 962 alloc = t_alloc; 963 char *const wnames[] = { g_strdup(" /") }; 964 P9Req *req; 965 uint32_t err; 966 967 do_attach(v9p); 968 req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0); 969 v9fs_req_wait_for_reply(req, NULL); 970 v9fs_rlerror(req, &err); 971 972 g_assert_cmpint(err, ==, ENOENT); 973 974 g_free(wnames[0]); 975 } 976 977 static void fs_walk_dotdot(void *obj, void *data, QGuestAllocator *t_alloc) 978 { 979 QVirtio9P *v9p = obj; 980 alloc = t_alloc; 981 char *const wnames[] = { g_strdup("..") }; 982 v9fs_qid root_qid; 983 g_autofree v9fs_qid *wqid = NULL; 984 P9Req *req; 985 986 do_version(v9p); 987 req = v9fs_tattach(v9p, 0, getuid(), 0); 988 v9fs_req_wait_for_reply(req, NULL); 989 v9fs_rattach(req, &root_qid); 990 991 req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0); 992 v9fs_req_wait_for_reply(req, NULL); 993 v9fs_rwalk(req, NULL, &wqid); /* We now we'll get one qid */ 994 995 g_assert_cmpmem(&root_qid, 13, wqid[0], 13); 996 997 g_free(wnames[0]); 998 } 999 1000 static void fs_lopen(void *obj, void *data, QGuestAllocator *t_alloc) 1001 { 1002 QVirtio9P *v9p = obj; 1003 alloc = t_alloc; 1004 char *const wnames[] = { g_strdup(QTEST_V9FS_SYNTH_LOPEN_FILE) }; 1005 P9Req *req; 1006 1007 do_attach(v9p); 1008 req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0); 1009 v9fs_req_wait_for_reply(req, NULL); 1010 v9fs_rwalk(req, NULL, NULL); 1011 1012 req = v9fs_tlopen(v9p, 1, O_WRONLY, 0); 1013 v9fs_req_wait_for_reply(req, NULL); 1014 v9fs_rlopen(req, NULL, NULL); 1015 1016 g_free(wnames[0]); 1017 } 1018 1019 static void fs_write(void *obj, void *data, QGuestAllocator *t_alloc) 1020 { 1021 QVirtio9P *v9p = obj; 1022 alloc = t_alloc; 1023 static const uint32_t write_count = P9_MAX_SIZE / 2; 1024 char *const wnames[] = { g_strdup(QTEST_V9FS_SYNTH_WRITE_FILE) }; 1025 g_autofree char *buf = g_malloc0(write_count); 1026 uint32_t count; 1027 P9Req *req; 1028 1029 do_attach(v9p); 1030 req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0); 1031 v9fs_req_wait_for_reply(req, NULL); 1032 v9fs_rwalk(req, NULL, NULL); 1033 1034 req = v9fs_tlopen(v9p, 1, O_WRONLY, 0); 1035 v9fs_req_wait_for_reply(req, NULL); 1036 v9fs_rlopen(req, NULL, NULL); 1037 1038 req = v9fs_twrite(v9p, 1, 0, write_count, buf, 0); 1039 v9fs_req_wait_for_reply(req, NULL); 1040 v9fs_rwrite(req, &count); 1041 g_assert_cmpint(count, ==, write_count); 1042 1043 g_free(wnames[0]); 1044 } 1045 1046 static void fs_flush_success(void *obj, void *data, QGuestAllocator *t_alloc) 1047 { 1048 QVirtio9P *v9p = obj; 1049 alloc = t_alloc; 1050 char *const wnames[] = { g_strdup(QTEST_V9FS_SYNTH_FLUSH_FILE) }; 1051 P9Req *req, *flush_req; 1052 uint32_t reply_len; 1053 uint8_t should_block; 1054 1055 do_attach(v9p); 1056 req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0); 1057 v9fs_req_wait_for_reply(req, NULL); 1058 v9fs_rwalk(req, NULL, NULL); 1059 1060 req = v9fs_tlopen(v9p, 1, O_WRONLY, 0); 1061 v9fs_req_wait_for_reply(req, NULL); 1062 v9fs_rlopen(req, NULL, NULL); 1063 1064 /* This will cause the 9p server to try to write data to the backend, 1065 * until the write request gets cancelled. 1066 */ 1067 should_block = 1; 1068 req = v9fs_twrite(v9p, 1, 0, sizeof(should_block), &should_block, 0); 1069 1070 flush_req = v9fs_tflush(v9p, req->tag, 1); 1071 1072 /* The write request is supposed to be flushed: the server should just 1073 * mark the write request as used and reply to the flush request. 1074 */ 1075 v9fs_req_wait_for_reply(req, &reply_len); 1076 g_assert_cmpint(reply_len, ==, 0); 1077 v9fs_req_free(req); 1078 v9fs_rflush(flush_req); 1079 1080 g_free(wnames[0]); 1081 } 1082 1083 static void fs_flush_ignored(void *obj, void *data, QGuestAllocator *t_alloc) 1084 { 1085 QVirtio9P *v9p = obj; 1086 alloc = t_alloc; 1087 char *const wnames[] = { g_strdup(QTEST_V9FS_SYNTH_FLUSH_FILE) }; 1088 P9Req *req, *flush_req; 1089 uint32_t count; 1090 uint8_t should_block; 1091 1092 do_attach(v9p); 1093 req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0); 1094 v9fs_req_wait_for_reply(req, NULL); 1095 v9fs_rwalk(req, NULL, NULL); 1096 1097 req = v9fs_tlopen(v9p, 1, O_WRONLY, 0); 1098 v9fs_req_wait_for_reply(req, NULL); 1099 v9fs_rlopen(req, NULL, NULL); 1100 1101 /* This will cause the write request to complete right away, before it 1102 * could be actually cancelled. 1103 */ 1104 should_block = 0; 1105 req = v9fs_twrite(v9p, 1, 0, sizeof(should_block), &should_block, 0); 1106 1107 flush_req = v9fs_tflush(v9p, req->tag, 1); 1108 1109 /* The write request is supposed to complete. The server should 1110 * reply to the write request and the flush request. 1111 */ 1112 v9fs_req_wait_for_reply(req, NULL); 1113 v9fs_rwrite(req, &count); 1114 g_assert_cmpint(count, ==, sizeof(should_block)); 1115 v9fs_rflush(flush_req); 1116 1117 g_free(wnames[0]); 1118 } 1119 1120 static void do_mkdir(QVirtio9P *v9p, const char *path, const char *cname) 1121 { 1122 g_autofree char *name = g_strdup(cname); 1123 uint32_t fid; 1124 P9Req *req; 1125 1126 fid = do_walk(v9p, path); 1127 1128 req = v9fs_tmkdir(v9p, fid, name, 0750, 0, 0); 1129 v9fs_req_wait_for_reply(req, NULL); 1130 v9fs_rmkdir(req, NULL); 1131 } 1132 1133 /* create a regular file with Tlcreate and return file's fid */ 1134 static uint32_t do_lcreate(QVirtio9P *v9p, const char *path, 1135 const char *cname) 1136 { 1137 g_autofree char *name = g_strdup(cname); 1138 uint32_t fid; 1139 P9Req *req; 1140 1141 fid = do_walk(v9p, path); 1142 1143 req = v9fs_tlcreate(v9p, fid, name, 0, 0750, 0, 0); 1144 v9fs_req_wait_for_reply(req, NULL); 1145 v9fs_rlcreate(req, NULL, NULL); 1146 1147 return fid; 1148 } 1149 1150 /* create symlink named @a clink in directory @a path pointing to @a to */ 1151 static void do_symlink(QVirtio9P *v9p, const char *path, const char *clink, 1152 const char *to) 1153 { 1154 g_autofree char *name = g_strdup(clink); 1155 g_autofree char *dst = g_strdup(to); 1156 uint32_t fid; 1157 P9Req *req; 1158 1159 fid = do_walk(v9p, path); 1160 1161 req = v9fs_tsymlink(v9p, fid, name, dst, 0, 0); 1162 v9fs_req_wait_for_reply(req, NULL); 1163 v9fs_rsymlink(req, NULL); 1164 } 1165 1166 /* create a hard link named @a clink in directory @a path pointing to @a to */ 1167 static void do_hardlink(QVirtio9P *v9p, const char *path, const char *clink, 1168 const char *to) 1169 { 1170 uint32_t dfid, fid; 1171 P9Req *req; 1172 1173 dfid = do_walk(v9p, path); 1174 fid = do_walk(v9p, to); 1175 1176 req = v9fs_tlink(v9p, dfid, fid, clink, 0); 1177 v9fs_req_wait_for_reply(req, NULL); 1178 v9fs_rlink(req); 1179 } 1180 1181 static void do_unlinkat(QVirtio9P *v9p, const char *atpath, const char *rpath, 1182 uint32_t flags) 1183 { 1184 g_autofree char *name = g_strdup(rpath); 1185 uint32_t fid; 1186 P9Req *req; 1187 1188 fid = do_walk(v9p, atpath); 1189 1190 req = v9fs_tunlinkat(v9p, fid, name, flags, 0); 1191 v9fs_req_wait_for_reply(req, NULL); 1192 v9fs_runlinkat(req); 1193 } 1194 1195 static void fs_readdir_split_128(void *obj, void *data, 1196 QGuestAllocator *t_alloc) 1197 { 1198 alloc = t_alloc; 1199 do_readdir_split(obj, 128); 1200 } 1201 1202 static void fs_readdir_split_256(void *obj, void *data, 1203 QGuestAllocator *t_alloc) 1204 { 1205 alloc = t_alloc; 1206 do_readdir_split(obj, 256); 1207 } 1208 1209 static void fs_readdir_split_512(void *obj, void *data, 1210 QGuestAllocator *t_alloc) 1211 { 1212 alloc = t_alloc; 1213 do_readdir_split(obj, 512); 1214 } 1215 1216 1217 /* tests using the 9pfs 'local' fs driver */ 1218 1219 static void fs_create_dir(void *obj, void *data, QGuestAllocator *t_alloc) 1220 { 1221 QVirtio9P *v9p = obj; 1222 alloc = t_alloc; 1223 struct stat st; 1224 g_autofree char *root_path = virtio_9p_test_path(""); 1225 g_autofree char *new_dir = virtio_9p_test_path("01"); 1226 1227 g_assert(root_path != NULL); 1228 1229 do_attach(v9p); 1230 do_mkdir(v9p, "/", "01"); 1231 1232 /* check if created directory really exists now ... */ 1233 g_assert(stat(new_dir, &st) == 0); 1234 /* ... and is actually a directory */ 1235 g_assert((st.st_mode & S_IFMT) == S_IFDIR); 1236 } 1237 1238 static void fs_unlinkat_dir(void *obj, void *data, QGuestAllocator *t_alloc) 1239 { 1240 QVirtio9P *v9p = obj; 1241 alloc = t_alloc; 1242 struct stat st; 1243 g_autofree char *root_path = virtio_9p_test_path(""); 1244 g_autofree char *new_dir = virtio_9p_test_path("02"); 1245 1246 g_assert(root_path != NULL); 1247 1248 do_attach(v9p); 1249 do_mkdir(v9p, "/", "02"); 1250 1251 /* check if created directory really exists now ... */ 1252 g_assert(stat(new_dir, &st) == 0); 1253 /* ... and is actually a directory */ 1254 g_assert((st.st_mode & S_IFMT) == S_IFDIR); 1255 1256 do_unlinkat(v9p, "/", "02", AT_REMOVEDIR); 1257 /* directory should be gone now */ 1258 g_assert(stat(new_dir, &st) != 0); 1259 } 1260 1261 static void fs_create_file(void *obj, void *data, QGuestAllocator *t_alloc) 1262 { 1263 QVirtio9P *v9p = obj; 1264 alloc = t_alloc; 1265 struct stat st; 1266 g_autofree char *new_file = virtio_9p_test_path("03/1st_file"); 1267 1268 do_attach(v9p); 1269 do_mkdir(v9p, "/", "03"); 1270 do_lcreate(v9p, "03", "1st_file"); 1271 1272 /* check if created file exists now ... */ 1273 g_assert(stat(new_file, &st) == 0); 1274 /* ... and is a regular file */ 1275 g_assert((st.st_mode & S_IFMT) == S_IFREG); 1276 } 1277 1278 static void fs_unlinkat_file(void *obj, void *data, QGuestAllocator *t_alloc) 1279 { 1280 QVirtio9P *v9p = obj; 1281 alloc = t_alloc; 1282 struct stat st; 1283 g_autofree char *new_file = virtio_9p_test_path("04/doa_file"); 1284 1285 do_attach(v9p); 1286 do_mkdir(v9p, "/", "04"); 1287 do_lcreate(v9p, "04", "doa_file"); 1288 1289 /* check if created file exists now ... */ 1290 g_assert(stat(new_file, &st) == 0); 1291 /* ... and is a regular file */ 1292 g_assert((st.st_mode & S_IFMT) == S_IFREG); 1293 1294 do_unlinkat(v9p, "04", "doa_file", 0); 1295 /* file should be gone now */ 1296 g_assert(stat(new_file, &st) != 0); 1297 } 1298 1299 static void fs_symlink_file(void *obj, void *data, QGuestAllocator *t_alloc) 1300 { 1301 QVirtio9P *v9p = obj; 1302 alloc = t_alloc; 1303 struct stat st; 1304 g_autofree char *real_file = virtio_9p_test_path("05/real_file"); 1305 g_autofree char *symlink_file = virtio_9p_test_path("05/symlink_file"); 1306 1307 do_attach(v9p); 1308 do_mkdir(v9p, "/", "05"); 1309 do_lcreate(v9p, "05", "real_file"); 1310 g_assert(stat(real_file, &st) == 0); 1311 g_assert((st.st_mode & S_IFMT) == S_IFREG); 1312 1313 do_symlink(v9p, "05", "symlink_file", "real_file"); 1314 1315 /* check if created link exists now */ 1316 g_assert(stat(symlink_file, &st) == 0); 1317 } 1318 1319 static void fs_unlinkat_symlink(void *obj, void *data, 1320 QGuestAllocator *t_alloc) 1321 { 1322 QVirtio9P *v9p = obj; 1323 alloc = t_alloc; 1324 struct stat st; 1325 g_autofree char *real_file = virtio_9p_test_path("06/real_file"); 1326 g_autofree char *symlink_file = virtio_9p_test_path("06/symlink_file"); 1327 1328 do_attach(v9p); 1329 do_mkdir(v9p, "/", "06"); 1330 do_lcreate(v9p, "06", "real_file"); 1331 g_assert(stat(real_file, &st) == 0); 1332 g_assert((st.st_mode & S_IFMT) == S_IFREG); 1333 1334 do_symlink(v9p, "06", "symlink_file", "real_file"); 1335 g_assert(stat(symlink_file, &st) == 0); 1336 1337 do_unlinkat(v9p, "06", "symlink_file", 0); 1338 /* symlink should be gone now */ 1339 g_assert(stat(symlink_file, &st) != 0); 1340 } 1341 1342 static void fs_hardlink_file(void *obj, void *data, QGuestAllocator *t_alloc) 1343 { 1344 QVirtio9P *v9p = obj; 1345 alloc = t_alloc; 1346 struct stat st_real, st_link; 1347 g_autofree char *real_file = virtio_9p_test_path("07/real_file"); 1348 g_autofree char *hardlink_file = virtio_9p_test_path("07/hardlink_file"); 1349 1350 do_attach(v9p); 1351 do_mkdir(v9p, "/", "07"); 1352 do_lcreate(v9p, "07", "real_file"); 1353 g_assert(stat(real_file, &st_real) == 0); 1354 g_assert((st_real.st_mode & S_IFMT) == S_IFREG); 1355 1356 do_hardlink(v9p, "07", "hardlink_file", "07/real_file"); 1357 1358 /* check if link exists now ... */ 1359 g_assert(stat(hardlink_file, &st_link) == 0); 1360 /* ... and it's a hard link, right? */ 1361 g_assert((st_link.st_mode & S_IFMT) == S_IFREG); 1362 g_assert(st_link.st_dev == st_real.st_dev); 1363 g_assert(st_link.st_ino == st_real.st_ino); 1364 } 1365 1366 static void fs_unlinkat_hardlink(void *obj, void *data, 1367 QGuestAllocator *t_alloc) 1368 { 1369 QVirtio9P *v9p = obj; 1370 alloc = t_alloc; 1371 struct stat st_real, st_link; 1372 g_autofree char *real_file = virtio_9p_test_path("08/real_file"); 1373 g_autofree char *hardlink_file = virtio_9p_test_path("08/hardlink_file"); 1374 1375 do_attach(v9p); 1376 do_mkdir(v9p, "/", "08"); 1377 do_lcreate(v9p, "08", "real_file"); 1378 g_assert(stat(real_file, &st_real) == 0); 1379 g_assert((st_real.st_mode & S_IFMT) == S_IFREG); 1380 1381 do_hardlink(v9p, "08", "hardlink_file", "08/real_file"); 1382 g_assert(stat(hardlink_file, &st_link) == 0); 1383 1384 do_unlinkat(v9p, "08", "hardlink_file", 0); 1385 /* symlink should be gone now */ 1386 g_assert(stat(hardlink_file, &st_link) != 0); 1387 /* and old file should still exist */ 1388 g_assert(stat(real_file, &st_real) == 0); 1389 } 1390 1391 static void *assign_9p_local_driver(GString *cmd_line, void *arg) 1392 { 1393 virtio_9p_assign_local_driver(cmd_line, "security_model=mapped-xattr"); 1394 return arg; 1395 } 1396 1397 static void register_virtio_9p_test(void) 1398 { 1399 1400 QOSGraphTestOptions opts = { 1401 }; 1402 1403 /* 9pfs test cases using the 'synth' filesystem driver */ 1404 qos_add_test("synth/config", "virtio-9p", pci_config, &opts); 1405 qos_add_test("synth/version/basic", "virtio-9p", fs_version, &opts); 1406 qos_add_test("synth/attach/basic", "virtio-9p", fs_attach, &opts); 1407 qos_add_test("synth/walk/basic", "virtio-9p", fs_walk, &opts); 1408 qos_add_test("synth/walk/no_slash", "virtio-9p", fs_walk_no_slash, 1409 &opts); 1410 qos_add_test("synth/walk/dotdot_from_root", "virtio-9p", 1411 fs_walk_dotdot, &opts); 1412 qos_add_test("synth/lopen/basic", "virtio-9p", fs_lopen, &opts); 1413 qos_add_test("synth/write/basic", "virtio-9p", fs_write, &opts); 1414 qos_add_test("synth/flush/success", "virtio-9p", fs_flush_success, 1415 &opts); 1416 qos_add_test("synth/flush/ignored", "virtio-9p", fs_flush_ignored, 1417 &opts); 1418 qos_add_test("synth/readdir/basic", "virtio-9p", fs_readdir, &opts); 1419 qos_add_test("synth/readdir/split_512", "virtio-9p", 1420 fs_readdir_split_512, &opts); 1421 qos_add_test("synth/readdir/split_256", "virtio-9p", 1422 fs_readdir_split_256, &opts); 1423 qos_add_test("synth/readdir/split_128", "virtio-9p", 1424 fs_readdir_split_128, &opts); 1425 1426 1427 /* 9pfs test cases using the 'local' filesystem driver */ 1428 1429 /* 1430 * XXX: Until we are sure that these tests can run everywhere, 1431 * keep them as "slow" so that they aren't run with "make check". 1432 */ 1433 if (!g_test_slow()) { 1434 return; 1435 } 1436 1437 opts.before = assign_9p_local_driver; 1438 qos_add_test("local/config", "virtio-9p", pci_config, &opts); 1439 qos_add_test("local/create_dir", "virtio-9p", fs_create_dir, &opts); 1440 qos_add_test("local/unlinkat_dir", "virtio-9p", fs_unlinkat_dir, &opts); 1441 qos_add_test("local/create_file", "virtio-9p", fs_create_file, &opts); 1442 qos_add_test("local/unlinkat_file", "virtio-9p", fs_unlinkat_file, &opts); 1443 qos_add_test("local/symlink_file", "virtio-9p", fs_symlink_file, &opts); 1444 qos_add_test("local/unlinkat_symlink", "virtio-9p", fs_unlinkat_symlink, 1445 &opts); 1446 qos_add_test("local/hardlink_file", "virtio-9p", fs_hardlink_file, &opts); 1447 qos_add_test("local/unlinkat_hardlink", "virtio-9p", fs_unlinkat_hardlink, 1448 &opts); 1449 } 1450 1451 libqos_init(register_virtio_9p_test); 1452 1453 static void __attribute__((constructor)) construct_9p_test(void) 1454 { 1455 /* make sure test dir for the 'local' tests exists */ 1456 virtio_9p_create_local_test_dir(); 1457 } 1458 1459 static void __attribute__((destructor)) destruct_9p_test(void) 1460 { 1461 /* remove previously created test dir when test suite completed */ 1462 virtio_9p_remove_local_test_dir(); 1463 } 1464