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 #include "qemu/osdep.h" 11 #include "libqtest-single.h" 12 #include "qemu/module.h" 13 #include "hw/9pfs/9p.h" 14 #include "hw/9pfs/9p-synth.h" 15 #include "libqos/virtio-9p.h" 16 #include "libqos/qgraph.h" 17 18 #define QVIRTIO_9P_TIMEOUT_US (10 * 1000 * 1000) 19 static QGuestAllocator *alloc; 20 21 /* 22 * Used to auto generate new fids. Start with arbitrary high value to avoid 23 * collision with hard coded fids in basic test code. 24 */ 25 static uint32_t fid_generator = 1000; 26 27 static uint32_t genfid(void) 28 { 29 return fid_generator++; 30 } 31 32 /** 33 * Splits the @a in string by @a delim into individual (non empty) strings 34 * and outputs them to @a out. The output array @a out is NULL terminated. 35 * 36 * Output array @a out must be freed by calling split_free(). 37 * 38 * @returns number of individual elements in output array @a out (without the 39 * final NULL terminating element) 40 */ 41 static int split(const char *in, const char *delim, char ***out) 42 { 43 int n = 0, i = 0; 44 char *tmp, *p; 45 46 tmp = g_strdup(in); 47 for (p = strtok(tmp, delim); p != NULL; p = strtok(NULL, delim)) { 48 if (strlen(p) > 0) { 49 ++n; 50 } 51 } 52 g_free(tmp); 53 54 *out = g_new0(char *, n + 1); /* last element NULL delimiter */ 55 56 tmp = g_strdup(in); 57 for (p = strtok(tmp, delim); p != NULL; p = strtok(NULL, delim)) { 58 if (strlen(p) > 0) { 59 (*out)[i++] = g_strdup(p); 60 } 61 } 62 g_free(tmp); 63 64 return n; 65 } 66 67 static void split_free(char ***out) 68 { 69 int i; 70 for (i = 0; (*out)[i]; ++i) { 71 g_free((*out)[i]); 72 } 73 g_free(*out); 74 *out = NULL; 75 } 76 77 static void pci_config(void *obj, void *data, QGuestAllocator *t_alloc) 78 { 79 QVirtio9P *v9p = obj; 80 alloc = t_alloc; 81 size_t tag_len = qvirtio_config_readw(v9p->vdev, 0); 82 char *tag; 83 int i; 84 85 g_assert_cmpint(tag_len, ==, strlen(MOUNT_TAG)); 86 87 tag = g_malloc(tag_len); 88 for (i = 0; i < tag_len; i++) { 89 tag[i] = qvirtio_config_readb(v9p->vdev, i + 2); 90 } 91 g_assert_cmpmem(tag, tag_len, MOUNT_TAG, tag_len); 92 g_free(tag); 93 } 94 95 #define P9_MAX_SIZE 4096 /* Max size of a T-message or R-message */ 96 97 typedef struct { 98 QTestState *qts; 99 QVirtio9P *v9p; 100 uint16_t tag; 101 uint64_t t_msg; 102 uint32_t t_size; 103 uint64_t r_msg; 104 /* No r_size, it is hardcoded to P9_MAX_SIZE */ 105 size_t t_off; 106 size_t r_off; 107 uint32_t free_head; 108 } P9Req; 109 110 static void v9fs_memwrite(P9Req *req, const void *addr, size_t len) 111 { 112 qtest_memwrite(req->qts, req->t_msg + req->t_off, addr, len); 113 req->t_off += len; 114 } 115 116 static void v9fs_memskip(P9Req *req, size_t len) 117 { 118 req->r_off += len; 119 } 120 121 static void v9fs_memread(P9Req *req, void *addr, size_t len) 122 { 123 qtest_memread(req->qts, req->r_msg + req->r_off, addr, len); 124 req->r_off += len; 125 } 126 127 static void v9fs_uint8_read(P9Req *req, uint8_t *val) 128 { 129 v9fs_memread(req, val, 1); 130 } 131 132 static void v9fs_uint16_write(P9Req *req, uint16_t val) 133 { 134 uint16_t le_val = cpu_to_le16(val); 135 136 v9fs_memwrite(req, &le_val, 2); 137 } 138 139 static void v9fs_uint16_read(P9Req *req, uint16_t *val) 140 { 141 v9fs_memread(req, val, 2); 142 le16_to_cpus(val); 143 } 144 145 static void v9fs_uint32_write(P9Req *req, uint32_t val) 146 { 147 uint32_t le_val = cpu_to_le32(val); 148 149 v9fs_memwrite(req, &le_val, 4); 150 } 151 152 static void v9fs_uint64_write(P9Req *req, uint64_t val) 153 { 154 uint64_t le_val = cpu_to_le64(val); 155 156 v9fs_memwrite(req, &le_val, 8); 157 } 158 159 static void v9fs_uint32_read(P9Req *req, uint32_t *val) 160 { 161 v9fs_memread(req, val, 4); 162 le32_to_cpus(val); 163 } 164 165 static void v9fs_uint64_read(P9Req *req, uint64_t *val) 166 { 167 v9fs_memread(req, val, 8); 168 le64_to_cpus(val); 169 } 170 171 /* len[2] string[len] */ 172 static uint16_t v9fs_string_size(const char *string) 173 { 174 size_t len = strlen(string); 175 176 g_assert_cmpint(len, <=, UINT16_MAX - 2); 177 178 return 2 + len; 179 } 180 181 static void v9fs_string_write(P9Req *req, const char *string) 182 { 183 int len = strlen(string); 184 185 g_assert_cmpint(len, <=, UINT16_MAX); 186 187 v9fs_uint16_write(req, (uint16_t) len); 188 v9fs_memwrite(req, string, len); 189 } 190 191 static void v9fs_string_read(P9Req *req, uint16_t *len, char **string) 192 { 193 uint16_t local_len; 194 195 v9fs_uint16_read(req, &local_len); 196 if (len) { 197 *len = local_len; 198 } 199 if (string) { 200 *string = g_malloc(local_len + 1); 201 v9fs_memread(req, *string, local_len); 202 (*string)[local_len] = 0; 203 } else { 204 v9fs_memskip(req, local_len); 205 } 206 } 207 208 typedef struct { 209 uint32_t size; 210 uint8_t id; 211 uint16_t tag; 212 } QEMU_PACKED P9Hdr; 213 214 static P9Req *v9fs_req_init(QVirtio9P *v9p, uint32_t size, uint8_t id, 215 uint16_t tag) 216 { 217 P9Req *req = g_new0(P9Req, 1); 218 uint32_t total_size = 7; /* 9P header has well-known size of 7 bytes */ 219 P9Hdr hdr = { 220 .id = id, 221 .tag = cpu_to_le16(tag) 222 }; 223 224 g_assert_cmpint(total_size, <=, UINT32_MAX - size); 225 total_size += size; 226 hdr.size = cpu_to_le32(total_size); 227 228 g_assert_cmpint(total_size, <=, P9_MAX_SIZE); 229 230 req->qts = global_qtest; 231 req->v9p = v9p; 232 req->t_size = total_size; 233 req->t_msg = guest_alloc(alloc, req->t_size); 234 v9fs_memwrite(req, &hdr, 7); 235 req->tag = tag; 236 return req; 237 } 238 239 static void v9fs_req_send(P9Req *req) 240 { 241 QVirtio9P *v9p = req->v9p; 242 243 req->r_msg = guest_alloc(alloc, P9_MAX_SIZE); 244 req->free_head = qvirtqueue_add(req->qts, v9p->vq, req->t_msg, req->t_size, 245 false, true); 246 qvirtqueue_add(req->qts, v9p->vq, req->r_msg, P9_MAX_SIZE, true, false); 247 qvirtqueue_kick(req->qts, v9p->vdev, v9p->vq, req->free_head); 248 req->t_off = 0; 249 } 250 251 static const char *rmessage_name(uint8_t id) 252 { 253 return 254 id == P9_RLERROR ? "RLERROR" : 255 id == P9_RVERSION ? "RVERSION" : 256 id == P9_RATTACH ? "RATTACH" : 257 id == P9_RWALK ? "RWALK" : 258 id == P9_RLOPEN ? "RLOPEN" : 259 id == P9_RWRITE ? "RWRITE" : 260 id == P9_RMKDIR ? "RMKDIR" : 261 id == P9_RLCREATE ? "RLCREATE" : 262 id == P9_RSYMLINK ? "RSYMLINK" : 263 id == P9_RLINK ? "RLINK" : 264 id == P9_RUNLINKAT ? "RUNLINKAT" : 265 id == P9_RFLUSH ? "RFLUSH" : 266 id == P9_RREADDIR ? "READDIR" : 267 "<unknown>"; 268 } 269 270 static void v9fs_req_wait_for_reply(P9Req *req, uint32_t *len) 271 { 272 QVirtio9P *v9p = req->v9p; 273 274 qvirtio_wait_used_elem(req->qts, v9p->vdev, v9p->vq, req->free_head, len, 275 QVIRTIO_9P_TIMEOUT_US); 276 } 277 278 static void v9fs_req_recv(P9Req *req, uint8_t id) 279 { 280 P9Hdr hdr; 281 282 v9fs_memread(req, &hdr, 7); 283 hdr.size = ldl_le_p(&hdr.size); 284 hdr.tag = lduw_le_p(&hdr.tag); 285 286 g_assert_cmpint(hdr.size, >=, 7); 287 g_assert_cmpint(hdr.size, <=, P9_MAX_SIZE); 288 g_assert_cmpint(hdr.tag, ==, req->tag); 289 290 if (hdr.id != id) { 291 g_printerr("Received response %d (%s) instead of %d (%s)\n", 292 hdr.id, rmessage_name(hdr.id), id, rmessage_name(id)); 293 294 if (hdr.id == P9_RLERROR) { 295 uint32_t err; 296 v9fs_uint32_read(req, &err); 297 g_printerr("Rlerror has errno %d (%s)\n", err, strerror(err)); 298 } 299 } 300 g_assert_cmpint(hdr.id, ==, id); 301 } 302 303 static void v9fs_req_free(P9Req *req) 304 { 305 guest_free(alloc, req->t_msg); 306 guest_free(alloc, req->r_msg); 307 g_free(req); 308 } 309 310 /* size[4] Rlerror tag[2] ecode[4] */ 311 static void v9fs_rlerror(P9Req *req, uint32_t *err) 312 { 313 v9fs_req_recv(req, P9_RLERROR); 314 v9fs_uint32_read(req, err); 315 v9fs_req_free(req); 316 } 317 318 /* size[4] Tversion tag[2] msize[4] version[s] */ 319 static P9Req *v9fs_tversion(QVirtio9P *v9p, uint32_t msize, const char *version, 320 uint16_t tag) 321 { 322 P9Req *req; 323 uint32_t body_size = 4; 324 uint16_t string_size = v9fs_string_size(version); 325 326 g_assert_cmpint(body_size, <=, UINT32_MAX - string_size); 327 body_size += string_size; 328 req = v9fs_req_init(v9p, body_size, P9_TVERSION, tag); 329 330 v9fs_uint32_write(req, msize); 331 v9fs_string_write(req, version); 332 v9fs_req_send(req); 333 return req; 334 } 335 336 /* size[4] Rversion tag[2] msize[4] version[s] */ 337 static void v9fs_rversion(P9Req *req, uint16_t *len, char **version) 338 { 339 uint32_t msize; 340 341 v9fs_req_recv(req, P9_RVERSION); 342 v9fs_uint32_read(req, &msize); 343 344 g_assert_cmpint(msize, ==, P9_MAX_SIZE); 345 346 if (len || version) { 347 v9fs_string_read(req, len, version); 348 } 349 350 v9fs_req_free(req); 351 } 352 353 /* size[4] Tattach tag[2] fid[4] afid[4] uname[s] aname[s] n_uname[4] */ 354 static P9Req *v9fs_tattach(QVirtio9P *v9p, uint32_t fid, uint32_t n_uname, 355 uint16_t tag) 356 { 357 const char *uname = ""; /* ignored by QEMU */ 358 const char *aname = ""; /* ignored by QEMU */ 359 P9Req *req = v9fs_req_init(v9p, 4 + 4 + 2 + 2 + 4, P9_TATTACH, tag); 360 361 v9fs_uint32_write(req, fid); 362 v9fs_uint32_write(req, P9_NOFID); 363 v9fs_string_write(req, uname); 364 v9fs_string_write(req, aname); 365 v9fs_uint32_write(req, n_uname); 366 v9fs_req_send(req); 367 return req; 368 } 369 370 typedef char v9fs_qid[13]; 371 372 /* size[4] Rattach tag[2] qid[13] */ 373 static void v9fs_rattach(P9Req *req, v9fs_qid *qid) 374 { 375 v9fs_req_recv(req, P9_RATTACH); 376 if (qid) { 377 v9fs_memread(req, qid, 13); 378 } 379 v9fs_req_free(req); 380 } 381 382 /* size[4] Twalk tag[2] fid[4] newfid[4] nwname[2] nwname*(wname[s]) */ 383 static P9Req *v9fs_twalk(QVirtio9P *v9p, uint32_t fid, uint32_t newfid, 384 uint16_t nwname, char *const wnames[], uint16_t tag) 385 { 386 P9Req *req; 387 int i; 388 uint32_t body_size = 4 + 4 + 2; 389 390 for (i = 0; i < nwname; i++) { 391 uint16_t wname_size = v9fs_string_size(wnames[i]); 392 393 g_assert_cmpint(body_size, <=, UINT32_MAX - wname_size); 394 body_size += wname_size; 395 } 396 req = v9fs_req_init(v9p, body_size, P9_TWALK, tag); 397 v9fs_uint32_write(req, fid); 398 v9fs_uint32_write(req, newfid); 399 v9fs_uint16_write(req, nwname); 400 for (i = 0; i < nwname; i++) { 401 v9fs_string_write(req, wnames[i]); 402 } 403 v9fs_req_send(req); 404 return req; 405 } 406 407 /* size[4] Rwalk tag[2] nwqid[2] nwqid*(wqid[13]) */ 408 static void v9fs_rwalk(P9Req *req, uint16_t *nwqid, v9fs_qid **wqid) 409 { 410 uint16_t local_nwqid; 411 412 v9fs_req_recv(req, P9_RWALK); 413 v9fs_uint16_read(req, &local_nwqid); 414 if (nwqid) { 415 *nwqid = local_nwqid; 416 } 417 if (wqid) { 418 *wqid = g_malloc(local_nwqid * 13); 419 v9fs_memread(req, *wqid, local_nwqid * 13); 420 } 421 v9fs_req_free(req); 422 } 423 424 /* size[4] Treaddir tag[2] fid[4] offset[8] count[4] */ 425 static P9Req *v9fs_treaddir(QVirtio9P *v9p, uint32_t fid, uint64_t offset, 426 uint32_t count, uint16_t tag) 427 { 428 P9Req *req; 429 430 req = v9fs_req_init(v9p, 4 + 8 + 4, P9_TREADDIR, tag); 431 v9fs_uint32_write(req, fid); 432 v9fs_uint64_write(req, offset); 433 v9fs_uint32_write(req, count); 434 v9fs_req_send(req); 435 return req; 436 } 437 438 struct V9fsDirent { 439 v9fs_qid qid; 440 uint64_t offset; 441 uint8_t type; 442 char *name; 443 struct V9fsDirent *next; 444 }; 445 446 /* size[4] Rreaddir tag[2] count[4] data[count] */ 447 static void v9fs_rreaddir(P9Req *req, uint32_t *count, uint32_t *nentries, 448 struct V9fsDirent **entries) 449 { 450 uint32_t local_count; 451 struct V9fsDirent *e = NULL; 452 uint16_t slen; 453 uint32_t n = 0; 454 455 v9fs_req_recv(req, P9_RREADDIR); 456 v9fs_uint32_read(req, &local_count); 457 458 if (count) { 459 *count = local_count; 460 } 461 462 for (int32_t togo = (int32_t)local_count; 463 togo >= 13 + 8 + 1 + 2; 464 togo -= 13 + 8 + 1 + 2 + slen, ++n) 465 { 466 if (!e) { 467 e = g_malloc(sizeof(struct V9fsDirent)); 468 if (entries) { 469 *entries = e; 470 } 471 } else { 472 e = e->next = g_malloc(sizeof(struct V9fsDirent)); 473 } 474 e->next = NULL; 475 /* qid[13] offset[8] type[1] name[s] */ 476 v9fs_memread(req, &e->qid, 13); 477 v9fs_uint64_read(req, &e->offset); 478 v9fs_uint8_read(req, &e->type); 479 v9fs_string_read(req, &slen, &e->name); 480 } 481 482 if (nentries) { 483 *nentries = n; 484 } 485 486 v9fs_req_free(req); 487 } 488 489 static void v9fs_free_dirents(struct V9fsDirent *e) 490 { 491 struct V9fsDirent *next = NULL; 492 493 for (; e; e = next) { 494 next = e->next; 495 g_free(e->name); 496 g_free(e); 497 } 498 } 499 500 /* size[4] Tlopen tag[2] fid[4] flags[4] */ 501 static P9Req *v9fs_tlopen(QVirtio9P *v9p, uint32_t fid, uint32_t flags, 502 uint16_t tag) 503 { 504 P9Req *req; 505 506 req = v9fs_req_init(v9p, 4 + 4, P9_TLOPEN, tag); 507 v9fs_uint32_write(req, fid); 508 v9fs_uint32_write(req, flags); 509 v9fs_req_send(req); 510 return req; 511 } 512 513 /* size[4] Rlopen tag[2] qid[13] iounit[4] */ 514 static void v9fs_rlopen(P9Req *req, v9fs_qid *qid, uint32_t *iounit) 515 { 516 v9fs_req_recv(req, P9_RLOPEN); 517 if (qid) { 518 v9fs_memread(req, qid, 13); 519 } else { 520 v9fs_memskip(req, 13); 521 } 522 if (iounit) { 523 v9fs_uint32_read(req, iounit); 524 } 525 v9fs_req_free(req); 526 } 527 528 /* size[4] Twrite tag[2] fid[4] offset[8] count[4] data[count] */ 529 static P9Req *v9fs_twrite(QVirtio9P *v9p, uint32_t fid, uint64_t offset, 530 uint32_t count, const void *data, uint16_t tag) 531 { 532 P9Req *req; 533 uint32_t body_size = 4 + 8 + 4; 534 535 g_assert_cmpint(body_size, <=, UINT32_MAX - count); 536 body_size += count; 537 req = v9fs_req_init(v9p, body_size, P9_TWRITE, tag); 538 v9fs_uint32_write(req, fid); 539 v9fs_uint64_write(req, offset); 540 v9fs_uint32_write(req, count); 541 v9fs_memwrite(req, data, count); 542 v9fs_req_send(req); 543 return req; 544 } 545 546 /* size[4] Rwrite tag[2] count[4] */ 547 static void v9fs_rwrite(P9Req *req, uint32_t *count) 548 { 549 v9fs_req_recv(req, P9_RWRITE); 550 if (count) { 551 v9fs_uint32_read(req, count); 552 } 553 v9fs_req_free(req); 554 } 555 556 /* size[4] Tflush tag[2] oldtag[2] */ 557 static P9Req *v9fs_tflush(QVirtio9P *v9p, uint16_t oldtag, uint16_t tag) 558 { 559 P9Req *req; 560 561 req = v9fs_req_init(v9p, 2, P9_TFLUSH, tag); 562 v9fs_uint32_write(req, oldtag); 563 v9fs_req_send(req); 564 return req; 565 } 566 567 /* size[4] Rflush tag[2] */ 568 static void v9fs_rflush(P9Req *req) 569 { 570 v9fs_req_recv(req, P9_RFLUSH); 571 v9fs_req_free(req); 572 } 573 574 static void do_version(QVirtio9P *v9p) 575 { 576 const char *version = "9P2000.L"; 577 uint16_t server_len; 578 char *server_version; 579 P9Req *req; 580 581 req = v9fs_tversion(v9p, P9_MAX_SIZE, version, P9_NOTAG); 582 v9fs_req_wait_for_reply(req, NULL); 583 v9fs_rversion(req, &server_len, &server_version); 584 585 g_assert_cmpmem(server_version, server_len, version, strlen(version)); 586 587 g_free(server_version); 588 } 589 590 /* utility function: walk to requested dir and return fid for that dir */ 591 static uint32_t do_walk(QVirtio9P *v9p, const char *path) 592 { 593 char **wnames; 594 P9Req *req; 595 const uint32_t fid = genfid(); 596 597 int nwnames = split(path, "/", &wnames); 598 599 req = v9fs_twalk(v9p, 0, fid, nwnames, wnames, 0); 600 v9fs_req_wait_for_reply(req, NULL); 601 v9fs_rwalk(req, NULL, NULL); 602 603 split_free(&wnames); 604 return fid; 605 } 606 607 static void fs_version(void *obj, void *data, QGuestAllocator *t_alloc) 608 { 609 alloc = t_alloc; 610 do_version(obj); 611 } 612 613 static void do_attach(QVirtio9P *v9p) 614 { 615 P9Req *req; 616 617 do_version(v9p); 618 req = v9fs_tattach(v9p, 0, getuid(), 0); 619 v9fs_req_wait_for_reply(req, NULL); 620 v9fs_rattach(req, NULL); 621 } 622 623 static void fs_attach(void *obj, void *data, QGuestAllocator *t_alloc) 624 { 625 alloc = t_alloc; 626 do_attach(obj); 627 } 628 629 static void fs_walk(void *obj, void *data, QGuestAllocator *t_alloc) 630 { 631 QVirtio9P *v9p = obj; 632 alloc = t_alloc; 633 char *wnames[P9_MAXWELEM]; 634 uint16_t nwqid; 635 v9fs_qid *wqid; 636 int i; 637 P9Req *req; 638 639 for (i = 0; i < P9_MAXWELEM; i++) { 640 wnames[i] = g_strdup_printf(QTEST_V9FS_SYNTH_WALK_FILE, i); 641 } 642 643 do_attach(v9p); 644 req = v9fs_twalk(v9p, 0, 1, P9_MAXWELEM, wnames, 0); 645 v9fs_req_wait_for_reply(req, NULL); 646 v9fs_rwalk(req, &nwqid, &wqid); 647 648 g_assert_cmpint(nwqid, ==, P9_MAXWELEM); 649 650 for (i = 0; i < P9_MAXWELEM; i++) { 651 g_free(wnames[i]); 652 } 653 654 g_free(wqid); 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 char *name = g_strdup_printf(QTEST_V9FS_SYNTH_READDIR_FILE, i); 871 g_assert_cmpint(fs_dirents_contain_name(entries, name), ==, true); 872 g_free(name); 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, *wqid; 983 P9Req *req; 984 985 do_version(v9p); 986 req = v9fs_tattach(v9p, 0, getuid(), 0); 987 v9fs_req_wait_for_reply(req, NULL); 988 v9fs_rattach(req, &root_qid); 989 990 req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0); 991 v9fs_req_wait_for_reply(req, NULL); 992 v9fs_rwalk(req, NULL, &wqid); /* We now we'll get one qid */ 993 994 g_assert_cmpmem(&root_qid, 13, wqid[0], 13); 995 996 g_free(wqid); 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 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(buf); 1044 g_free(wnames[0]); 1045 } 1046 1047 static void fs_flush_success(void *obj, void *data, QGuestAllocator *t_alloc) 1048 { 1049 QVirtio9P *v9p = obj; 1050 alloc = t_alloc; 1051 char *const wnames[] = { g_strdup(QTEST_V9FS_SYNTH_FLUSH_FILE) }; 1052 P9Req *req, *flush_req; 1053 uint32_t reply_len; 1054 uint8_t should_block; 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 /* This will cause the 9p server to try to write data to the backend, 1066 * until the write request gets cancelled. 1067 */ 1068 should_block = 1; 1069 req = v9fs_twrite(v9p, 1, 0, sizeof(should_block), &should_block, 0); 1070 1071 flush_req = v9fs_tflush(v9p, req->tag, 1); 1072 1073 /* The write request is supposed to be flushed: the server should just 1074 * mark the write request as used and reply to the flush request. 1075 */ 1076 v9fs_req_wait_for_reply(req, &reply_len); 1077 g_assert_cmpint(reply_len, ==, 0); 1078 v9fs_req_free(req); 1079 v9fs_rflush(flush_req); 1080 1081 g_free(wnames[0]); 1082 } 1083 1084 static void fs_flush_ignored(void *obj, void *data, QGuestAllocator *t_alloc) 1085 { 1086 QVirtio9P *v9p = obj; 1087 alloc = t_alloc; 1088 char *const wnames[] = { g_strdup(QTEST_V9FS_SYNTH_FLUSH_FILE) }; 1089 P9Req *req, *flush_req; 1090 uint32_t count; 1091 uint8_t should_block; 1092 1093 do_attach(v9p); 1094 req = v9fs_twalk(v9p, 0, 1, 1, wnames, 0); 1095 v9fs_req_wait_for_reply(req, NULL); 1096 v9fs_rwalk(req, NULL, NULL); 1097 1098 req = v9fs_tlopen(v9p, 1, O_WRONLY, 0); 1099 v9fs_req_wait_for_reply(req, NULL); 1100 v9fs_rlopen(req, NULL, NULL); 1101 1102 /* This will cause the write request to complete right away, before it 1103 * could be actually cancelled. 1104 */ 1105 should_block = 0; 1106 req = v9fs_twrite(v9p, 1, 0, sizeof(should_block), &should_block, 0); 1107 1108 flush_req = v9fs_tflush(v9p, req->tag, 1); 1109 1110 /* The write request is supposed to complete. The server should 1111 * reply to the write request and the flush request. 1112 */ 1113 v9fs_req_wait_for_reply(req, NULL); 1114 v9fs_rwrite(req, &count); 1115 g_assert_cmpint(count, ==, sizeof(should_block)); 1116 v9fs_rflush(flush_req); 1117 1118 g_free(wnames[0]); 1119 } 1120 1121 static void do_mkdir(QVirtio9P *v9p, const char *path, const char *cname) 1122 { 1123 char *const name = g_strdup(cname); 1124 uint32_t fid; 1125 P9Req *req; 1126 1127 fid = do_walk(v9p, path); 1128 1129 req = v9fs_tmkdir(v9p, fid, name, 0750, 0, 0); 1130 v9fs_req_wait_for_reply(req, NULL); 1131 v9fs_rmkdir(req, NULL); 1132 1133 g_free(name); 1134 } 1135 1136 /* create a regular file with Tlcreate and return file's fid */ 1137 static uint32_t do_lcreate(QVirtio9P *v9p, const char *path, 1138 const char *cname) 1139 { 1140 char *const name = g_strdup(cname); 1141 uint32_t fid; 1142 P9Req *req; 1143 1144 fid = do_walk(v9p, path); 1145 1146 req = v9fs_tlcreate(v9p, fid, name, 0, 0750, 0, 0); 1147 v9fs_req_wait_for_reply(req, NULL); 1148 v9fs_rlcreate(req, NULL, NULL); 1149 1150 g_free(name); 1151 return fid; 1152 } 1153 1154 /* create symlink named @a clink in directory @a path pointing to @a to */ 1155 static void do_symlink(QVirtio9P *v9p, const char *path, const char *clink, 1156 const char *to) 1157 { 1158 char *const name = g_strdup(clink); 1159 char *const dst = g_strdup(to); 1160 uint32_t fid; 1161 P9Req *req; 1162 1163 fid = do_walk(v9p, path); 1164 1165 req = v9fs_tsymlink(v9p, fid, name, dst, 0, 0); 1166 v9fs_req_wait_for_reply(req, NULL); 1167 v9fs_rsymlink(req, NULL); 1168 1169 g_free(dst); 1170 g_free(name); 1171 } 1172 1173 /* create a hard link named @a clink in directory @a path pointing to @a to */ 1174 static void do_hardlink(QVirtio9P *v9p, const char *path, const char *clink, 1175 const char *to) 1176 { 1177 uint32_t dfid, fid; 1178 P9Req *req; 1179 1180 dfid = do_walk(v9p, path); 1181 fid = do_walk(v9p, to); 1182 1183 req = v9fs_tlink(v9p, dfid, fid, clink, 0); 1184 v9fs_req_wait_for_reply(req, NULL); 1185 v9fs_rlink(req); 1186 } 1187 1188 static void do_unlinkat(QVirtio9P *v9p, const char *atpath, const char *rpath, 1189 uint32_t flags) 1190 { 1191 char *const name = g_strdup(rpath); 1192 uint32_t fid; 1193 P9Req *req; 1194 1195 fid = do_walk(v9p, atpath); 1196 1197 req = v9fs_tunlinkat(v9p, fid, name, flags, 0); 1198 v9fs_req_wait_for_reply(req, NULL); 1199 v9fs_runlinkat(req); 1200 1201 g_free(name); 1202 } 1203 1204 static void fs_readdir_split_128(void *obj, void *data, 1205 QGuestAllocator *t_alloc) 1206 { 1207 alloc = t_alloc; 1208 do_readdir_split(obj, 128); 1209 } 1210 1211 static void fs_readdir_split_256(void *obj, void *data, 1212 QGuestAllocator *t_alloc) 1213 { 1214 alloc = t_alloc; 1215 do_readdir_split(obj, 256); 1216 } 1217 1218 static void fs_readdir_split_512(void *obj, void *data, 1219 QGuestAllocator *t_alloc) 1220 { 1221 alloc = t_alloc; 1222 do_readdir_split(obj, 512); 1223 } 1224 1225 1226 /* tests using the 9pfs 'local' fs driver */ 1227 1228 static void fs_create_dir(void *obj, void *data, QGuestAllocator *t_alloc) 1229 { 1230 QVirtio9P *v9p = obj; 1231 alloc = t_alloc; 1232 struct stat st; 1233 char *root_path = virtio_9p_test_path(""); 1234 char *new_dir = virtio_9p_test_path("01"); 1235 1236 g_assert(root_path != NULL); 1237 1238 do_attach(v9p); 1239 do_mkdir(v9p, "/", "01"); 1240 1241 /* check if created directory really exists now ... */ 1242 g_assert(stat(new_dir, &st) == 0); 1243 /* ... and is actually a directory */ 1244 g_assert((st.st_mode & S_IFMT) == S_IFDIR); 1245 1246 g_free(new_dir); 1247 g_free(root_path); 1248 } 1249 1250 static void fs_unlinkat_dir(void *obj, void *data, QGuestAllocator *t_alloc) 1251 { 1252 QVirtio9P *v9p = obj; 1253 alloc = t_alloc; 1254 struct stat st; 1255 char *root_path = virtio_9p_test_path(""); 1256 char *new_dir = virtio_9p_test_path("02"); 1257 1258 g_assert(root_path != NULL); 1259 1260 do_attach(v9p); 1261 do_mkdir(v9p, "/", "02"); 1262 1263 /* check if created directory really exists now ... */ 1264 g_assert(stat(new_dir, &st) == 0); 1265 /* ... and is actually a directory */ 1266 g_assert((st.st_mode & S_IFMT) == S_IFDIR); 1267 1268 do_unlinkat(v9p, "/", "02", AT_REMOVEDIR); 1269 /* directory should be gone now */ 1270 g_assert(stat(new_dir, &st) != 0); 1271 1272 g_free(new_dir); 1273 g_free(root_path); 1274 } 1275 1276 static void fs_create_file(void *obj, void *data, QGuestAllocator *t_alloc) 1277 { 1278 QVirtio9P *v9p = obj; 1279 alloc = t_alloc; 1280 struct stat st; 1281 char *new_file = virtio_9p_test_path("03/1st_file"); 1282 1283 do_attach(v9p); 1284 do_mkdir(v9p, "/", "03"); 1285 do_lcreate(v9p, "03", "1st_file"); 1286 1287 /* check if created file exists now ... */ 1288 g_assert(stat(new_file, &st) == 0); 1289 /* ... and is a regular file */ 1290 g_assert((st.st_mode & S_IFMT) == S_IFREG); 1291 1292 g_free(new_file); 1293 } 1294 1295 static void fs_unlinkat_file(void *obj, void *data, QGuestAllocator *t_alloc) 1296 { 1297 QVirtio9P *v9p = obj; 1298 alloc = t_alloc; 1299 struct stat st; 1300 char *new_file = virtio_9p_test_path("04/doa_file"); 1301 1302 do_attach(v9p); 1303 do_mkdir(v9p, "/", "04"); 1304 do_lcreate(v9p, "04", "doa_file"); 1305 1306 /* check if created file exists now ... */ 1307 g_assert(stat(new_file, &st) == 0); 1308 /* ... and is a regular file */ 1309 g_assert((st.st_mode & S_IFMT) == S_IFREG); 1310 1311 do_unlinkat(v9p, "04", "doa_file", 0); 1312 /* file should be gone now */ 1313 g_assert(stat(new_file, &st) != 0); 1314 1315 g_free(new_file); 1316 } 1317 1318 static void fs_symlink_file(void *obj, void *data, QGuestAllocator *t_alloc) 1319 { 1320 QVirtio9P *v9p = obj; 1321 alloc = t_alloc; 1322 struct stat st; 1323 char *real_file = virtio_9p_test_path("05/real_file"); 1324 char *symlink_file = virtio_9p_test_path("05/symlink_file"); 1325 1326 do_attach(v9p); 1327 do_mkdir(v9p, "/", "05"); 1328 do_lcreate(v9p, "05", "real_file"); 1329 g_assert(stat(real_file, &st) == 0); 1330 g_assert((st.st_mode & S_IFMT) == S_IFREG); 1331 1332 do_symlink(v9p, "05", "symlink_file", "real_file"); 1333 1334 /* check if created link exists now */ 1335 g_assert(stat(symlink_file, &st) == 0); 1336 1337 g_free(symlink_file); 1338 g_free(real_file); 1339 } 1340 1341 static void fs_unlinkat_symlink(void *obj, void *data, 1342 QGuestAllocator *t_alloc) 1343 { 1344 QVirtio9P *v9p = obj; 1345 alloc = t_alloc; 1346 struct stat st; 1347 char *real_file = virtio_9p_test_path("06/real_file"); 1348 char *symlink_file = virtio_9p_test_path("06/symlink_file"); 1349 1350 do_attach(v9p); 1351 do_mkdir(v9p, "/", "06"); 1352 do_lcreate(v9p, "06", "real_file"); 1353 g_assert(stat(real_file, &st) == 0); 1354 g_assert((st.st_mode & S_IFMT) == S_IFREG); 1355 1356 do_symlink(v9p, "06", "symlink_file", "real_file"); 1357 g_assert(stat(symlink_file, &st) == 0); 1358 1359 do_unlinkat(v9p, "06", "symlink_file", 0); 1360 /* symlink should be gone now */ 1361 g_assert(stat(symlink_file, &st) != 0); 1362 1363 g_free(symlink_file); 1364 g_free(real_file); 1365 } 1366 1367 static void fs_hardlink_file(void *obj, void *data, QGuestAllocator *t_alloc) 1368 { 1369 QVirtio9P *v9p = obj; 1370 alloc = t_alloc; 1371 struct stat st_real, st_link; 1372 char *real_file = virtio_9p_test_path("07/real_file"); 1373 char *hardlink_file = virtio_9p_test_path("07/hardlink_file"); 1374 1375 do_attach(v9p); 1376 do_mkdir(v9p, "/", "07"); 1377 do_lcreate(v9p, "07", "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, "07", "hardlink_file", "07/real_file"); 1382 1383 /* check if link exists now ... */ 1384 g_assert(stat(hardlink_file, &st_link) == 0); 1385 /* ... and it's a hard link, right? */ 1386 g_assert((st_link.st_mode & S_IFMT) == S_IFREG); 1387 g_assert(st_link.st_dev == st_real.st_dev); 1388 g_assert(st_link.st_ino == st_real.st_ino); 1389 1390 g_free(hardlink_file); 1391 g_free(real_file); 1392 } 1393 1394 static void fs_unlinkat_hardlink(void *obj, void *data, 1395 QGuestAllocator *t_alloc) 1396 { 1397 QVirtio9P *v9p = obj; 1398 alloc = t_alloc; 1399 struct stat st_real, st_link; 1400 char *real_file = virtio_9p_test_path("08/real_file"); 1401 char *hardlink_file = virtio_9p_test_path("08/hardlink_file"); 1402 1403 do_attach(v9p); 1404 do_mkdir(v9p, "/", "08"); 1405 do_lcreate(v9p, "08", "real_file"); 1406 g_assert(stat(real_file, &st_real) == 0); 1407 g_assert((st_real.st_mode & S_IFMT) == S_IFREG); 1408 1409 do_hardlink(v9p, "08", "hardlink_file", "08/real_file"); 1410 g_assert(stat(hardlink_file, &st_link) == 0); 1411 1412 do_unlinkat(v9p, "08", "hardlink_file", 0); 1413 /* symlink should be gone now */ 1414 g_assert(stat(hardlink_file, &st_link) != 0); 1415 /* and old file should still exist */ 1416 g_assert(stat(real_file, &st_real) == 0); 1417 1418 g_free(hardlink_file); 1419 g_free(real_file); 1420 } 1421 1422 static void *assign_9p_local_driver(GString *cmd_line, void *arg) 1423 { 1424 virtio_9p_assign_local_driver(cmd_line, "security_model=mapped-xattr"); 1425 return arg; 1426 } 1427 1428 static void register_virtio_9p_test(void) 1429 { 1430 1431 QOSGraphTestOptions opts = { 1432 }; 1433 1434 /* 9pfs test cases using the 'synth' filesystem driver */ 1435 qos_add_test("synth/config", "virtio-9p", pci_config, &opts); 1436 qos_add_test("synth/version/basic", "virtio-9p", fs_version, &opts); 1437 qos_add_test("synth/attach/basic", "virtio-9p", fs_attach, &opts); 1438 qos_add_test("synth/walk/basic", "virtio-9p", fs_walk, &opts); 1439 qos_add_test("synth/walk/no_slash", "virtio-9p", fs_walk_no_slash, 1440 &opts); 1441 qos_add_test("synth/walk/dotdot_from_root", "virtio-9p", 1442 fs_walk_dotdot, &opts); 1443 qos_add_test("synth/lopen/basic", "virtio-9p", fs_lopen, &opts); 1444 qos_add_test("synth/write/basic", "virtio-9p", fs_write, &opts); 1445 qos_add_test("synth/flush/success", "virtio-9p", fs_flush_success, 1446 &opts); 1447 qos_add_test("synth/flush/ignored", "virtio-9p", fs_flush_ignored, 1448 &opts); 1449 qos_add_test("synth/readdir/basic", "virtio-9p", fs_readdir, &opts); 1450 qos_add_test("synth/readdir/split_512", "virtio-9p", 1451 fs_readdir_split_512, &opts); 1452 qos_add_test("synth/readdir/split_256", "virtio-9p", 1453 fs_readdir_split_256, &opts); 1454 qos_add_test("synth/readdir/split_128", "virtio-9p", 1455 fs_readdir_split_128, &opts); 1456 1457 1458 /* 9pfs test cases using the 'local' filesystem driver */ 1459 opts.before = assign_9p_local_driver; 1460 qos_add_test("local/config", "virtio-9p", pci_config, &opts); 1461 qos_add_test("local/create_dir", "virtio-9p", fs_create_dir, &opts); 1462 qos_add_test("local/unlinkat_dir", "virtio-9p", fs_unlinkat_dir, &opts); 1463 qos_add_test("local/create_file", "virtio-9p", fs_create_file, &opts); 1464 qos_add_test("local/unlinkat_file", "virtio-9p", fs_unlinkat_file, &opts); 1465 qos_add_test("local/symlink_file", "virtio-9p", fs_symlink_file, &opts); 1466 qos_add_test("local/unlinkat_symlink", "virtio-9p", fs_unlinkat_symlink, 1467 &opts); 1468 qos_add_test("local/hardlink_file", "virtio-9p", fs_hardlink_file, &opts); 1469 qos_add_test("local/unlinkat_hardlink", "virtio-9p", fs_unlinkat_hardlink, 1470 &opts); 1471 } 1472 1473 libqos_init(register_virtio_9p_test); 1474 1475 static void __attribute__((constructor)) construct_9p_test(void) 1476 { 1477 /* make sure test dir for the 'local' tests exists */ 1478 virtio_9p_create_local_test_dir(); 1479 } 1480 1481 static void __attribute__((destructor)) destruct_9p_test(void) 1482 { 1483 /* remove previously created test dir when test suite completed */ 1484 virtio_9p_remove_local_test_dir(); 1485 } 1486