1 /* 2 * Virtio 9p backend 3 * 4 * Copyright IBM, Corp. 2010 5 * 6 * Authors: 7 * Anthony Liguori <aliguori@us.ibm.com> 8 * 9 * This work is licensed under the terms of the GNU GPL, version 2. See 10 * the COPYING file in the top-level directory. 11 * 12 */ 13 14 /* 15 * Not so fast! You might want to read the 9p developer docs first: 16 * https://wiki.qemu.org/Documentation/9p 17 */ 18 19 #include "qemu/osdep.h" 20 #include <glib/gprintf.h> 21 #include "hw/virtio/virtio.h" 22 #include "qapi/error.h" 23 #include "qemu/error-report.h" 24 #include "qemu/iov.h" 25 #include "qemu/main-loop.h" 26 #include "qemu/sockets.h" 27 #include "virtio-9p.h" 28 #include "fsdev/qemu-fsdev.h" 29 #include "9p-xattr.h" 30 #include "coth.h" 31 #include "trace.h" 32 #include "migration/blocker.h" 33 #include "qemu/xxhash.h" 34 #include <math.h> 35 #include <linux/limits.h> 36 37 int open_fd_hw; 38 int total_open_fd; 39 static int open_fd_rc; 40 41 enum { 42 Oread = 0x00, 43 Owrite = 0x01, 44 Ordwr = 0x02, 45 Oexec = 0x03, 46 Oexcl = 0x04, 47 Otrunc = 0x10, 48 Orexec = 0x20, 49 Orclose = 0x40, 50 Oappend = 0x80, 51 }; 52 53 static ssize_t pdu_marshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...) 54 { 55 ssize_t ret; 56 va_list ap; 57 58 va_start(ap, fmt); 59 ret = pdu->s->transport->pdu_vmarshal(pdu, offset, fmt, ap); 60 va_end(ap); 61 62 return ret; 63 } 64 65 static ssize_t pdu_unmarshal(V9fsPDU *pdu, size_t offset, const char *fmt, ...) 66 { 67 ssize_t ret; 68 va_list ap; 69 70 va_start(ap, fmt); 71 ret = pdu->s->transport->pdu_vunmarshal(pdu, offset, fmt, ap); 72 va_end(ap); 73 74 return ret; 75 } 76 77 static int omode_to_uflags(int8_t mode) 78 { 79 int ret = 0; 80 81 switch (mode & 3) { 82 case Oread: 83 ret = O_RDONLY; 84 break; 85 case Ordwr: 86 ret = O_RDWR; 87 break; 88 case Owrite: 89 ret = O_WRONLY; 90 break; 91 case Oexec: 92 ret = O_RDONLY; 93 break; 94 } 95 96 if (mode & Otrunc) { 97 ret |= O_TRUNC; 98 } 99 100 if (mode & Oappend) { 101 ret |= O_APPEND; 102 } 103 104 if (mode & Oexcl) { 105 ret |= O_EXCL; 106 } 107 108 return ret; 109 } 110 111 typedef struct DotlOpenflagMap { 112 int dotl_flag; 113 int open_flag; 114 } DotlOpenflagMap; 115 116 static int dotl_to_open_flags(int flags) 117 { 118 int i; 119 /* 120 * We have same bits for P9_DOTL_READONLY, P9_DOTL_WRONLY 121 * and P9_DOTL_NOACCESS 122 */ 123 int oflags = flags & O_ACCMODE; 124 125 DotlOpenflagMap dotl_oflag_map[] = { 126 { P9_DOTL_CREATE, O_CREAT }, 127 { P9_DOTL_EXCL, O_EXCL }, 128 { P9_DOTL_NOCTTY , O_NOCTTY }, 129 { P9_DOTL_TRUNC, O_TRUNC }, 130 { P9_DOTL_APPEND, O_APPEND }, 131 { P9_DOTL_NONBLOCK, O_NONBLOCK } , 132 { P9_DOTL_DSYNC, O_DSYNC }, 133 { P9_DOTL_FASYNC, FASYNC }, 134 { P9_DOTL_DIRECT, O_DIRECT }, 135 { P9_DOTL_LARGEFILE, O_LARGEFILE }, 136 { P9_DOTL_DIRECTORY, O_DIRECTORY }, 137 { P9_DOTL_NOFOLLOW, O_NOFOLLOW }, 138 { P9_DOTL_NOATIME, O_NOATIME }, 139 { P9_DOTL_SYNC, O_SYNC }, 140 }; 141 142 for (i = 0; i < ARRAY_SIZE(dotl_oflag_map); i++) { 143 if (flags & dotl_oflag_map[i].dotl_flag) { 144 oflags |= dotl_oflag_map[i].open_flag; 145 } 146 } 147 148 return oflags; 149 } 150 151 void cred_init(FsCred *credp) 152 { 153 credp->fc_uid = -1; 154 credp->fc_gid = -1; 155 credp->fc_mode = -1; 156 credp->fc_rdev = -1; 157 } 158 159 static int get_dotl_openflags(V9fsState *s, int oflags) 160 { 161 int flags; 162 /* 163 * Filter the client open flags 164 */ 165 flags = dotl_to_open_flags(oflags); 166 flags &= ~(O_NOCTTY | O_ASYNC | O_CREAT); 167 /* 168 * Ignore direct disk access hint until the server supports it. 169 */ 170 flags &= ~O_DIRECT; 171 return flags; 172 } 173 174 void v9fs_path_init(V9fsPath *path) 175 { 176 path->data = NULL; 177 path->size = 0; 178 } 179 180 void v9fs_path_free(V9fsPath *path) 181 { 182 g_free(path->data); 183 path->data = NULL; 184 path->size = 0; 185 } 186 187 188 void GCC_FMT_ATTR(2, 3) 189 v9fs_path_sprintf(V9fsPath *path, const char *fmt, ...) 190 { 191 va_list ap; 192 193 v9fs_path_free(path); 194 195 va_start(ap, fmt); 196 /* Bump the size for including terminating NULL */ 197 path->size = g_vasprintf(&path->data, fmt, ap) + 1; 198 va_end(ap); 199 } 200 201 void v9fs_path_copy(V9fsPath *dst, const V9fsPath *src) 202 { 203 v9fs_path_free(dst); 204 dst->size = src->size; 205 dst->data = g_memdup(src->data, src->size); 206 } 207 208 int v9fs_name_to_path(V9fsState *s, V9fsPath *dirpath, 209 const char *name, V9fsPath *path) 210 { 211 int err; 212 err = s->ops->name_to_path(&s->ctx, dirpath, name, path); 213 if (err < 0) { 214 err = -errno; 215 } 216 return err; 217 } 218 219 /* 220 * Return TRUE if s1 is an ancestor of s2. 221 * 222 * E.g. "a/b" is an ancestor of "a/b/c" but not of "a/bc/d". 223 * As a special case, We treat s1 as ancestor of s2 if they are same! 224 */ 225 static int v9fs_path_is_ancestor(V9fsPath *s1, V9fsPath *s2) 226 { 227 if (!strncmp(s1->data, s2->data, s1->size - 1)) { 228 if (s2->data[s1->size - 1] == '\0' || s2->data[s1->size - 1] == '/') { 229 return 1; 230 } 231 } 232 return 0; 233 } 234 235 static size_t v9fs_string_size(V9fsString *str) 236 { 237 return str->size; 238 } 239 240 /* 241 * returns 0 if fid got re-opened, 1 if not, < 0 on error */ 242 static int coroutine_fn v9fs_reopen_fid(V9fsPDU *pdu, V9fsFidState *f) 243 { 244 int err = 1; 245 if (f->fid_type == P9_FID_FILE) { 246 if (f->fs.fd == -1) { 247 do { 248 err = v9fs_co_open(pdu, f, f->open_flags); 249 } while (err == -EINTR && !pdu->cancelled); 250 } 251 } else if (f->fid_type == P9_FID_DIR) { 252 if (f->fs.dir.stream == NULL) { 253 do { 254 err = v9fs_co_opendir(pdu, f); 255 } while (err == -EINTR && !pdu->cancelled); 256 } 257 } 258 return err; 259 } 260 261 static V9fsFidState *coroutine_fn get_fid(V9fsPDU *pdu, int32_t fid) 262 { 263 int err; 264 V9fsFidState *f; 265 V9fsState *s = pdu->s; 266 267 QSIMPLEQ_FOREACH(f, &s->fid_list, next) { 268 BUG_ON(f->clunked); 269 if (f->fid == fid) { 270 /* 271 * Update the fid ref upfront so that 272 * we don't get reclaimed when we yield 273 * in open later. 274 */ 275 f->ref++; 276 /* 277 * check whether we need to reopen the 278 * file. We might have closed the fd 279 * while trying to free up some file 280 * descriptors. 281 */ 282 err = v9fs_reopen_fid(pdu, f); 283 if (err < 0) { 284 f->ref--; 285 return NULL; 286 } 287 /* 288 * Mark the fid as referenced so that the LRU 289 * reclaim won't close the file descriptor 290 */ 291 f->flags |= FID_REFERENCED; 292 return f; 293 } 294 } 295 return NULL; 296 } 297 298 static V9fsFidState *alloc_fid(V9fsState *s, int32_t fid) 299 { 300 V9fsFidState *f; 301 302 QSIMPLEQ_FOREACH(f, &s->fid_list, next) { 303 /* If fid is already there return NULL */ 304 BUG_ON(f->clunked); 305 if (f->fid == fid) { 306 return NULL; 307 } 308 } 309 f = g_malloc0(sizeof(V9fsFidState)); 310 f->fid = fid; 311 f->fid_type = P9_FID_NONE; 312 f->ref = 1; 313 /* 314 * Mark the fid as referenced so that the LRU 315 * reclaim won't close the file descriptor 316 */ 317 f->flags |= FID_REFERENCED; 318 QSIMPLEQ_INSERT_TAIL(&s->fid_list, f, next); 319 320 v9fs_readdir_init(s->proto_version, &f->fs.dir); 321 v9fs_readdir_init(s->proto_version, &f->fs_reclaim.dir); 322 323 return f; 324 } 325 326 static int coroutine_fn v9fs_xattr_fid_clunk(V9fsPDU *pdu, V9fsFidState *fidp) 327 { 328 int retval = 0; 329 330 if (fidp->fs.xattr.xattrwalk_fid) { 331 /* getxattr/listxattr fid */ 332 goto free_value; 333 } 334 /* 335 * if this is fid for setxattr. clunk should 336 * result in setxattr localcall 337 */ 338 if (fidp->fs.xattr.len != fidp->fs.xattr.copied_len) { 339 /* clunk after partial write */ 340 retval = -EINVAL; 341 goto free_out; 342 } 343 if (fidp->fs.xattr.len) { 344 retval = v9fs_co_lsetxattr(pdu, &fidp->path, &fidp->fs.xattr.name, 345 fidp->fs.xattr.value, 346 fidp->fs.xattr.len, 347 fidp->fs.xattr.flags); 348 } else { 349 retval = v9fs_co_lremovexattr(pdu, &fidp->path, &fidp->fs.xattr.name); 350 } 351 free_out: 352 v9fs_string_free(&fidp->fs.xattr.name); 353 free_value: 354 g_free(fidp->fs.xattr.value); 355 return retval; 356 } 357 358 static int coroutine_fn free_fid(V9fsPDU *pdu, V9fsFidState *fidp) 359 { 360 int retval = 0; 361 362 if (fidp->fid_type == P9_FID_FILE) { 363 /* If we reclaimed the fd no need to close */ 364 if (fidp->fs.fd != -1) { 365 retval = v9fs_co_close(pdu, &fidp->fs); 366 } 367 } else if (fidp->fid_type == P9_FID_DIR) { 368 if (fidp->fs.dir.stream != NULL) { 369 retval = v9fs_co_closedir(pdu, &fidp->fs); 370 } 371 } else if (fidp->fid_type == P9_FID_XATTR) { 372 retval = v9fs_xattr_fid_clunk(pdu, fidp); 373 } 374 v9fs_path_free(&fidp->path); 375 g_free(fidp); 376 return retval; 377 } 378 379 static int coroutine_fn put_fid(V9fsPDU *pdu, V9fsFidState *fidp) 380 { 381 BUG_ON(!fidp->ref); 382 fidp->ref--; 383 /* 384 * Don't free the fid if it is in reclaim list 385 */ 386 if (!fidp->ref && fidp->clunked) { 387 if (fidp->fid == pdu->s->root_fid) { 388 /* 389 * if the clunked fid is root fid then we 390 * have unmounted the fs on the client side. 391 * delete the migration blocker. Ideally, this 392 * should be hooked to transport close notification 393 */ 394 if (pdu->s->migration_blocker) { 395 migrate_del_blocker(pdu->s->migration_blocker); 396 error_free(pdu->s->migration_blocker); 397 pdu->s->migration_blocker = NULL; 398 } 399 } 400 return free_fid(pdu, fidp); 401 } 402 return 0; 403 } 404 405 static V9fsFidState *clunk_fid(V9fsState *s, int32_t fid) 406 { 407 V9fsFidState *fidp; 408 409 QSIMPLEQ_FOREACH(fidp, &s->fid_list, next) { 410 if (fidp->fid == fid) { 411 QSIMPLEQ_REMOVE(&s->fid_list, fidp, V9fsFidState, next); 412 fidp->clunked = true; 413 return fidp; 414 } 415 } 416 return NULL; 417 } 418 419 void coroutine_fn v9fs_reclaim_fd(V9fsPDU *pdu) 420 { 421 int reclaim_count = 0; 422 V9fsState *s = pdu->s; 423 V9fsFidState *f; 424 QSLIST_HEAD(, V9fsFidState) reclaim_list = 425 QSLIST_HEAD_INITIALIZER(reclaim_list); 426 427 QSIMPLEQ_FOREACH(f, &s->fid_list, next) { 428 /* 429 * Unlink fids cannot be reclaimed. Check 430 * for them and skip them. Also skip fids 431 * currently being operated on. 432 */ 433 if (f->ref || f->flags & FID_NON_RECLAIMABLE) { 434 continue; 435 } 436 /* 437 * if it is a recently referenced fid 438 * we leave the fid untouched and clear the 439 * reference bit. We come back to it later 440 * in the next iteration. (a simple LRU without 441 * moving list elements around) 442 */ 443 if (f->flags & FID_REFERENCED) { 444 f->flags &= ~FID_REFERENCED; 445 continue; 446 } 447 /* 448 * Add fids to reclaim list. 449 */ 450 if (f->fid_type == P9_FID_FILE) { 451 if (f->fs.fd != -1) { 452 /* 453 * Up the reference count so that 454 * a clunk request won't free this fid 455 */ 456 f->ref++; 457 QSLIST_INSERT_HEAD(&reclaim_list, f, reclaim_next); 458 f->fs_reclaim.fd = f->fs.fd; 459 f->fs.fd = -1; 460 reclaim_count++; 461 } 462 } else if (f->fid_type == P9_FID_DIR) { 463 if (f->fs.dir.stream != NULL) { 464 /* 465 * Up the reference count so that 466 * a clunk request won't free this fid 467 */ 468 f->ref++; 469 QSLIST_INSERT_HEAD(&reclaim_list, f, reclaim_next); 470 f->fs_reclaim.dir.stream = f->fs.dir.stream; 471 f->fs.dir.stream = NULL; 472 reclaim_count++; 473 } 474 } 475 if (reclaim_count >= open_fd_rc) { 476 break; 477 } 478 } 479 /* 480 * Now close the fid in reclaim list. Free them if they 481 * are already clunked. 482 */ 483 while (!QSLIST_EMPTY(&reclaim_list)) { 484 f = QSLIST_FIRST(&reclaim_list); 485 QSLIST_REMOVE(&reclaim_list, f, V9fsFidState, reclaim_next); 486 if (f->fid_type == P9_FID_FILE) { 487 v9fs_co_close(pdu, &f->fs_reclaim); 488 } else if (f->fid_type == P9_FID_DIR) { 489 v9fs_co_closedir(pdu, &f->fs_reclaim); 490 } 491 /* 492 * Now drop the fid reference, free it 493 * if clunked. 494 */ 495 put_fid(pdu, f); 496 } 497 } 498 499 static int coroutine_fn v9fs_mark_fids_unreclaim(V9fsPDU *pdu, V9fsPath *path) 500 { 501 int err; 502 V9fsState *s = pdu->s; 503 V9fsFidState *fidp, *fidp_next; 504 505 fidp = QSIMPLEQ_FIRST(&s->fid_list); 506 if (!fidp) { 507 return 0; 508 } 509 510 /* 511 * v9fs_reopen_fid() can yield : a reference on the fid must be held 512 * to ensure its pointer remains valid and we can safely pass it to 513 * QSIMPLEQ_NEXT(). The corresponding put_fid() can also yield so 514 * we must keep a reference on the next fid as well. So the logic here 515 * is to get a reference on a fid and only put it back during the next 516 * iteration after we could get a reference on the next fid. Start with 517 * the first one. 518 */ 519 for (fidp->ref++; fidp; fidp = fidp_next) { 520 if (fidp->path.size == path->size && 521 !memcmp(fidp->path.data, path->data, path->size)) { 522 /* Mark the fid non reclaimable. */ 523 fidp->flags |= FID_NON_RECLAIMABLE; 524 525 /* reopen the file/dir if already closed */ 526 err = v9fs_reopen_fid(pdu, fidp); 527 if (err < 0) { 528 put_fid(pdu, fidp); 529 return err; 530 } 531 } 532 533 fidp_next = QSIMPLEQ_NEXT(fidp, next); 534 535 if (fidp_next) { 536 /* 537 * Ensure the next fid survives a potential clunk request during 538 * put_fid() below and v9fs_reopen_fid() in the next iteration. 539 */ 540 fidp_next->ref++; 541 } 542 543 /* We're done with this fid */ 544 put_fid(pdu, fidp); 545 } 546 547 return 0; 548 } 549 550 static void coroutine_fn virtfs_reset(V9fsPDU *pdu) 551 { 552 V9fsState *s = pdu->s; 553 V9fsFidState *fidp; 554 555 /* Free all fids */ 556 while (!QSIMPLEQ_EMPTY(&s->fid_list)) { 557 /* Get fid */ 558 fidp = QSIMPLEQ_FIRST(&s->fid_list); 559 fidp->ref++; 560 561 /* Clunk fid */ 562 QSIMPLEQ_REMOVE(&s->fid_list, fidp, V9fsFidState, next); 563 fidp->clunked = true; 564 565 put_fid(pdu, fidp); 566 } 567 } 568 569 #define P9_QID_TYPE_DIR 0x80 570 #define P9_QID_TYPE_SYMLINK 0x02 571 572 #define P9_STAT_MODE_DIR 0x80000000 573 #define P9_STAT_MODE_APPEND 0x40000000 574 #define P9_STAT_MODE_EXCL 0x20000000 575 #define P9_STAT_MODE_MOUNT 0x10000000 576 #define P9_STAT_MODE_AUTH 0x08000000 577 #define P9_STAT_MODE_TMP 0x04000000 578 #define P9_STAT_MODE_SYMLINK 0x02000000 579 #define P9_STAT_MODE_LINK 0x01000000 580 #define P9_STAT_MODE_DEVICE 0x00800000 581 #define P9_STAT_MODE_NAMED_PIPE 0x00200000 582 #define P9_STAT_MODE_SOCKET 0x00100000 583 #define P9_STAT_MODE_SETUID 0x00080000 584 #define P9_STAT_MODE_SETGID 0x00040000 585 #define P9_STAT_MODE_SETVTX 0x00010000 586 587 #define P9_STAT_MODE_TYPE_BITS (P9_STAT_MODE_DIR | \ 588 P9_STAT_MODE_SYMLINK | \ 589 P9_STAT_MODE_LINK | \ 590 P9_STAT_MODE_DEVICE | \ 591 P9_STAT_MODE_NAMED_PIPE | \ 592 P9_STAT_MODE_SOCKET) 593 594 /* Mirrors all bits of a byte. So e.g. binary 10100000 would become 00000101. */ 595 static inline uint8_t mirror8bit(uint8_t byte) 596 { 597 return (byte * 0x0202020202ULL & 0x010884422010ULL) % 1023; 598 } 599 600 /* Same as mirror8bit() just for a 64 bit data type instead for a byte. */ 601 static inline uint64_t mirror64bit(uint64_t value) 602 { 603 return ((uint64_t)mirror8bit(value & 0xff) << 56) | 604 ((uint64_t)mirror8bit((value >> 8) & 0xff) << 48) | 605 ((uint64_t)mirror8bit((value >> 16) & 0xff) << 40) | 606 ((uint64_t)mirror8bit((value >> 24) & 0xff) << 32) | 607 ((uint64_t)mirror8bit((value >> 32) & 0xff) << 24) | 608 ((uint64_t)mirror8bit((value >> 40) & 0xff) << 16) | 609 ((uint64_t)mirror8bit((value >> 48) & 0xff) << 8) | 610 ((uint64_t)mirror8bit((value >> 56) & 0xff)); 611 } 612 613 /** 614 * @brief Parameter k for the Exponential Golomb algorihm to be used. 615 * 616 * The smaller this value, the smaller the minimum bit count for the Exp. 617 * Golomb generated affixes will be (at lowest index) however for the 618 * price of having higher maximum bit count of generated affixes (at highest 619 * index). Likewise increasing this parameter yields in smaller maximum bit 620 * count for the price of having higher minimum bit count. 621 * 622 * In practice that means: a good value for k depends on the expected amount 623 * of devices to be exposed by one export. For a small amount of devices k 624 * should be small, for a large amount of devices k might be increased 625 * instead. The default of k=0 should be fine for most users though. 626 * 627 * @b IMPORTANT: In case this ever becomes a runtime parameter; the value of 628 * k should not change as long as guest is still running! Because that would 629 * cause completely different inode numbers to be generated on guest. 630 */ 631 #define EXP_GOLOMB_K 0 632 633 /** 634 * @brief Exponential Golomb algorithm for arbitrary k (including k=0). 635 * 636 * The Exponential Golomb algorithm generates @b prefixes (@b not suffixes!) 637 * with growing length and with the mathematical property of being 638 * "prefix-free". The latter means the generated prefixes can be prepended 639 * in front of arbitrary numbers and the resulting concatenated numbers are 640 * guaranteed to be always unique. 641 * 642 * This is a minor adjustment to the original Exp. Golomb algorithm in the 643 * sense that lowest allowed index (@param n) starts with 1, not with zero. 644 * 645 * @param n - natural number (or index) of the prefix to be generated 646 * (1, 2, 3, ...) 647 * @param k - parameter k of Exp. Golomb algorithm to be used 648 * (see comment on EXP_GOLOMB_K macro for details about k) 649 */ 650 static VariLenAffix expGolombEncode(uint64_t n, int k) 651 { 652 const uint64_t value = n + (1 << k) - 1; 653 const int bits = (int) log2(value) + 1; 654 return (VariLenAffix) { 655 .type = AffixType_Prefix, 656 .value = value, 657 .bits = bits + MAX((bits - 1 - k), 0) 658 }; 659 } 660 661 /** 662 * @brief Converts a suffix into a prefix, or a prefix into a suffix. 663 * 664 * Simply mirror all bits of the affix value, for the purpose to preserve 665 * respectively the mathematical "prefix-free" or "suffix-free" property 666 * after the conversion. 667 * 668 * If a passed prefix is suitable to create unique numbers, then the 669 * returned suffix is suitable to create unique numbers as well (and vice 670 * versa). 671 */ 672 static VariLenAffix invertAffix(const VariLenAffix *affix) 673 { 674 return (VariLenAffix) { 675 .type = 676 (affix->type == AffixType_Suffix) ? 677 AffixType_Prefix : AffixType_Suffix, 678 .value = 679 mirror64bit(affix->value) >> 680 ((sizeof(affix->value) * 8) - affix->bits), 681 .bits = affix->bits 682 }; 683 } 684 685 /** 686 * @brief Generates suffix numbers with "suffix-free" property. 687 * 688 * This is just a wrapper function on top of the Exp. Golomb algorithm. 689 * 690 * Since the Exp. Golomb algorithm generates prefixes, but we need suffixes, 691 * this function converts the Exp. Golomb prefixes into appropriate suffixes 692 * which are still suitable for generating unique numbers. 693 * 694 * @param n - natural number (or index) of the suffix to be generated 695 * (1, 2, 3, ...) 696 */ 697 static VariLenAffix affixForIndex(uint64_t index) 698 { 699 VariLenAffix prefix; 700 prefix = expGolombEncode(index, EXP_GOLOMB_K); 701 return invertAffix(&prefix); /* convert prefix to suffix */ 702 } 703 704 /* creative abuse of tb_hash_func7, which is based on xxhash */ 705 static uint32_t qpp_hash(QppEntry e) 706 { 707 return qemu_xxhash7(e.ino_prefix, e.dev, 0, 0, 0); 708 } 709 710 static uint32_t qpf_hash(QpfEntry e) 711 { 712 return qemu_xxhash7(e.ino, e.dev, 0, 0, 0); 713 } 714 715 static bool qpd_cmp_func(const void *obj, const void *userp) 716 { 717 const QpdEntry *e1 = obj, *e2 = userp; 718 return e1->dev == e2->dev; 719 } 720 721 static bool qpp_cmp_func(const void *obj, const void *userp) 722 { 723 const QppEntry *e1 = obj, *e2 = userp; 724 return e1->dev == e2->dev && e1->ino_prefix == e2->ino_prefix; 725 } 726 727 static bool qpf_cmp_func(const void *obj, const void *userp) 728 { 729 const QpfEntry *e1 = obj, *e2 = userp; 730 return e1->dev == e2->dev && e1->ino == e2->ino; 731 } 732 733 static void qp_table_remove(void *p, uint32_t h, void *up) 734 { 735 g_free(p); 736 } 737 738 static void qp_table_destroy(struct qht *ht) 739 { 740 if (!ht || !ht->map) { 741 return; 742 } 743 qht_iter(ht, qp_table_remove, NULL); 744 qht_destroy(ht); 745 } 746 747 static void qpd_table_init(struct qht *ht) 748 { 749 qht_init(ht, qpd_cmp_func, 1, QHT_MODE_AUTO_RESIZE); 750 } 751 752 static void qpp_table_init(struct qht *ht) 753 { 754 qht_init(ht, qpp_cmp_func, 1, QHT_MODE_AUTO_RESIZE); 755 } 756 757 static void qpf_table_init(struct qht *ht) 758 { 759 qht_init(ht, qpf_cmp_func, 1 << 16, QHT_MODE_AUTO_RESIZE); 760 } 761 762 /* 763 * Returns how many (high end) bits of inode numbers of the passed fs 764 * device shall be used (in combination with the device number) to 765 * generate hash values for qpp_table entries. 766 * 767 * This function is required if variable length suffixes are used for inode 768 * number mapping on guest level. Since a device may end up having multiple 769 * entries in qpp_table, each entry most probably with a different suffix 770 * length, we thus need this function in conjunction with qpd_table to 771 * "agree" about a fix amount of bits (per device) to be always used for 772 * generating hash values for the purpose of accessing qpp_table in order 773 * get consistent behaviour when accessing qpp_table. 774 */ 775 static int qid_inode_prefix_hash_bits(V9fsPDU *pdu, dev_t dev) 776 { 777 QpdEntry lookup = { 778 .dev = dev 779 }, *val; 780 uint32_t hash = dev; 781 VariLenAffix affix; 782 783 val = qht_lookup(&pdu->s->qpd_table, &lookup, hash); 784 if (!val) { 785 val = g_malloc0(sizeof(QpdEntry)); 786 *val = lookup; 787 affix = affixForIndex(pdu->s->qp_affix_next); 788 val->prefix_bits = affix.bits; 789 qht_insert(&pdu->s->qpd_table, val, hash, NULL); 790 pdu->s->qp_ndevices++; 791 } 792 return val->prefix_bits; 793 } 794 795 /** 796 * @brief Slow / full mapping host inode nr -> guest inode nr. 797 * 798 * This function performs a slower and much more costly remapping of an 799 * original file inode number on host to an appropriate different inode 800 * number on guest. For every (dev, inode) combination on host a new 801 * sequential number is generated, cached and exposed as inode number on 802 * guest. 803 * 804 * This is just a "last resort" fallback solution if the much faster/cheaper 805 * qid_path_suffixmap() failed. In practice this slow / full mapping is not 806 * expected ever to be used at all though. 807 * 808 * @see qid_path_suffixmap() for details 809 * 810 */ 811 static int qid_path_fullmap(V9fsPDU *pdu, const struct stat *stbuf, 812 uint64_t *path) 813 { 814 QpfEntry lookup = { 815 .dev = stbuf->st_dev, 816 .ino = stbuf->st_ino 817 }, *val; 818 uint32_t hash = qpf_hash(lookup); 819 VariLenAffix affix; 820 821 val = qht_lookup(&pdu->s->qpf_table, &lookup, hash); 822 823 if (!val) { 824 if (pdu->s->qp_fullpath_next == 0) { 825 /* no more files can be mapped :'( */ 826 error_report_once( 827 "9p: No more prefixes available for remapping inodes from " 828 "host to guest." 829 ); 830 return -ENFILE; 831 } 832 833 val = g_malloc0(sizeof(QppEntry)); 834 *val = lookup; 835 836 /* new unique inode and device combo */ 837 affix = affixForIndex( 838 1ULL << (sizeof(pdu->s->qp_affix_next) * 8) 839 ); 840 val->path = (pdu->s->qp_fullpath_next++ << affix.bits) | affix.value; 841 pdu->s->qp_fullpath_next &= ((1ULL << (64 - affix.bits)) - 1); 842 qht_insert(&pdu->s->qpf_table, val, hash, NULL); 843 } 844 845 *path = val->path; 846 return 0; 847 } 848 849 /** 850 * @brief Quick mapping host inode nr -> guest inode nr. 851 * 852 * This function performs quick remapping of an original file inode number 853 * on host to an appropriate different inode number on guest. This remapping 854 * of inodes is required to avoid inode nr collisions on guest which would 855 * happen if the 9p export contains more than 1 exported file system (or 856 * more than 1 file system data set), because unlike on host level where the 857 * files would have different device nrs, all files exported by 9p would 858 * share the same device nr on guest (the device nr of the virtual 9p device 859 * that is). 860 * 861 * Inode remapping is performed by chopping off high end bits of the original 862 * inode number from host, shifting the result upwards and then assigning a 863 * generated suffix number for the low end bits, where the same suffix number 864 * will be shared by all inodes with the same device id AND the same high end 865 * bits that have been chopped off. That approach utilizes the fact that inode 866 * numbers very likely share the same high end bits (i.e. due to their common 867 * sequential generation by file systems) and hence we only have to generate 868 * and track a very limited amount of suffixes in practice due to that. 869 * 870 * We generate variable size suffixes for that purpose. The 1st generated 871 * suffix will only have 1 bit and hence we only need to chop off 1 bit from 872 * the original inode number. The subsequent suffixes being generated will 873 * grow in (bit) size subsequently, i.e. the 2nd and 3rd suffix being 874 * generated will have 3 bits and hence we have to chop off 3 bits from their 875 * original inodes, and so on. That approach of using variable length suffixes 876 * (i.e. over fixed size ones) utilizes the fact that in practice only a very 877 * limited amount of devices are shared by the same export (e.g. typically 878 * less than 2 dozen devices per 9p export), so in practice we need to chop 879 * off less bits than with fixed size prefixes and yet are flexible to add 880 * new devices at runtime below host's export directory at any time without 881 * having to reboot guest nor requiring to reconfigure guest for that. And due 882 * to the very limited amount of original high end bits that we chop off that 883 * way, the total amount of suffixes we need to generate is less than by using 884 * fixed size prefixes and hence it also improves performance of the inode 885 * remapping algorithm, and finally has the nice side effect that the inode 886 * numbers on guest will be much smaller & human friendly. ;-) 887 */ 888 static int qid_path_suffixmap(V9fsPDU *pdu, const struct stat *stbuf, 889 uint64_t *path) 890 { 891 const int ino_hash_bits = qid_inode_prefix_hash_bits(pdu, stbuf->st_dev); 892 QppEntry lookup = { 893 .dev = stbuf->st_dev, 894 .ino_prefix = (uint16_t) (stbuf->st_ino >> (64 - ino_hash_bits)) 895 }, *val; 896 uint32_t hash = qpp_hash(lookup); 897 898 val = qht_lookup(&pdu->s->qpp_table, &lookup, hash); 899 900 if (!val) { 901 if (pdu->s->qp_affix_next == 0) { 902 /* we ran out of affixes */ 903 warn_report_once( 904 "9p: Potential degraded performance of inode remapping" 905 ); 906 return -ENFILE; 907 } 908 909 val = g_malloc0(sizeof(QppEntry)); 910 *val = lookup; 911 912 /* new unique inode affix and device combo */ 913 val->qp_affix_index = pdu->s->qp_affix_next++; 914 val->qp_affix = affixForIndex(val->qp_affix_index); 915 qht_insert(&pdu->s->qpp_table, val, hash, NULL); 916 } 917 /* assuming generated affix to be suffix type, not prefix */ 918 *path = (stbuf->st_ino << val->qp_affix.bits) | val->qp_affix.value; 919 return 0; 920 } 921 922 static int stat_to_qid(V9fsPDU *pdu, const struct stat *stbuf, V9fsQID *qidp) 923 { 924 int err; 925 size_t size; 926 927 if (pdu->s->ctx.export_flags & V9FS_REMAP_INODES) { 928 /* map inode+device to qid path (fast path) */ 929 err = qid_path_suffixmap(pdu, stbuf, &qidp->path); 930 if (err == -ENFILE) { 931 /* fast path didn't work, fall back to full map */ 932 err = qid_path_fullmap(pdu, stbuf, &qidp->path); 933 } 934 if (err) { 935 return err; 936 } 937 } else { 938 if (pdu->s->dev_id != stbuf->st_dev) { 939 if (pdu->s->ctx.export_flags & V9FS_FORBID_MULTIDEVS) { 940 error_report_once( 941 "9p: Multiple devices detected in same VirtFS export. " 942 "Access of guest to additional devices is (partly) " 943 "denied due to virtfs option 'multidevs=forbid' being " 944 "effective." 945 ); 946 return -ENODEV; 947 } else { 948 warn_report_once( 949 "9p: Multiple devices detected in same VirtFS export, " 950 "which might lead to file ID collisions and severe " 951 "misbehaviours on guest! You should either use a " 952 "separate export for each device shared from host or " 953 "use virtfs option 'multidevs=remap'!" 954 ); 955 } 956 } 957 memset(&qidp->path, 0, sizeof(qidp->path)); 958 size = MIN(sizeof(stbuf->st_ino), sizeof(qidp->path)); 959 memcpy(&qidp->path, &stbuf->st_ino, size); 960 } 961 962 qidp->version = stbuf->st_mtime ^ (stbuf->st_size << 8); 963 qidp->type = 0; 964 if (S_ISDIR(stbuf->st_mode)) { 965 qidp->type |= P9_QID_TYPE_DIR; 966 } 967 if (S_ISLNK(stbuf->st_mode)) { 968 qidp->type |= P9_QID_TYPE_SYMLINK; 969 } 970 971 return 0; 972 } 973 974 V9fsPDU *pdu_alloc(V9fsState *s) 975 { 976 V9fsPDU *pdu = NULL; 977 978 if (!QLIST_EMPTY(&s->free_list)) { 979 pdu = QLIST_FIRST(&s->free_list); 980 QLIST_REMOVE(pdu, next); 981 QLIST_INSERT_HEAD(&s->active_list, pdu, next); 982 } 983 return pdu; 984 } 985 986 void pdu_free(V9fsPDU *pdu) 987 { 988 V9fsState *s = pdu->s; 989 990 g_assert(!pdu->cancelled); 991 QLIST_REMOVE(pdu, next); 992 QLIST_INSERT_HEAD(&s->free_list, pdu, next); 993 } 994 995 static void coroutine_fn pdu_complete(V9fsPDU *pdu, ssize_t len) 996 { 997 int8_t id = pdu->id + 1; /* Response */ 998 V9fsState *s = pdu->s; 999 int ret; 1000 1001 /* 1002 * The 9p spec requires that successfully cancelled pdus receive no reply. 1003 * Sending a reply would confuse clients because they would 1004 * assume that any EINTR is the actual result of the operation, 1005 * rather than a consequence of the cancellation. However, if 1006 * the operation completed (succesfully or with an error other 1007 * than caused be cancellation), we do send out that reply, both 1008 * for efficiency and to avoid confusing the rest of the state machine 1009 * that assumes passing a non-error here will mean a successful 1010 * transmission of the reply. 1011 */ 1012 bool discard = pdu->cancelled && len == -EINTR; 1013 if (discard) { 1014 trace_v9fs_rcancel(pdu->tag, pdu->id); 1015 pdu->size = 0; 1016 goto out_notify; 1017 } 1018 1019 if (len < 0) { 1020 int err = -len; 1021 len = 7; 1022 1023 if (s->proto_version != V9FS_PROTO_2000L) { 1024 V9fsString str; 1025 1026 str.data = strerror(err); 1027 str.size = strlen(str.data); 1028 1029 ret = pdu_marshal(pdu, len, "s", &str); 1030 if (ret < 0) { 1031 goto out_notify; 1032 } 1033 len += ret; 1034 id = P9_RERROR; 1035 } 1036 1037 ret = pdu_marshal(pdu, len, "d", err); 1038 if (ret < 0) { 1039 goto out_notify; 1040 } 1041 len += ret; 1042 1043 if (s->proto_version == V9FS_PROTO_2000L) { 1044 id = P9_RLERROR; 1045 } 1046 trace_v9fs_rerror(pdu->tag, pdu->id, err); /* Trace ERROR */ 1047 } 1048 1049 /* fill out the header */ 1050 if (pdu_marshal(pdu, 0, "dbw", (int32_t)len, id, pdu->tag) < 0) { 1051 goto out_notify; 1052 } 1053 1054 /* keep these in sync */ 1055 pdu->size = len; 1056 pdu->id = id; 1057 1058 out_notify: 1059 pdu->s->transport->push_and_notify(pdu); 1060 1061 /* Now wakeup anybody waiting in flush for this request */ 1062 if (!qemu_co_queue_next(&pdu->complete)) { 1063 pdu_free(pdu); 1064 } 1065 } 1066 1067 static mode_t v9mode_to_mode(uint32_t mode, V9fsString *extension) 1068 { 1069 mode_t ret; 1070 1071 ret = mode & 0777; 1072 if (mode & P9_STAT_MODE_DIR) { 1073 ret |= S_IFDIR; 1074 } 1075 1076 if (mode & P9_STAT_MODE_SYMLINK) { 1077 ret |= S_IFLNK; 1078 } 1079 if (mode & P9_STAT_MODE_SOCKET) { 1080 ret |= S_IFSOCK; 1081 } 1082 if (mode & P9_STAT_MODE_NAMED_PIPE) { 1083 ret |= S_IFIFO; 1084 } 1085 if (mode & P9_STAT_MODE_DEVICE) { 1086 if (extension->size && extension->data[0] == 'c') { 1087 ret |= S_IFCHR; 1088 } else { 1089 ret |= S_IFBLK; 1090 } 1091 } 1092 1093 if (!(ret & ~0777)) { 1094 ret |= S_IFREG; 1095 } 1096 1097 if (mode & P9_STAT_MODE_SETUID) { 1098 ret |= S_ISUID; 1099 } 1100 if (mode & P9_STAT_MODE_SETGID) { 1101 ret |= S_ISGID; 1102 } 1103 if (mode & P9_STAT_MODE_SETVTX) { 1104 ret |= S_ISVTX; 1105 } 1106 1107 return ret; 1108 } 1109 1110 static int donttouch_stat(V9fsStat *stat) 1111 { 1112 if (stat->type == -1 && 1113 stat->dev == -1 && 1114 stat->qid.type == 0xff && 1115 stat->qid.version == (uint32_t) -1 && 1116 stat->qid.path == (uint64_t) -1 && 1117 stat->mode == -1 && 1118 stat->atime == -1 && 1119 stat->mtime == -1 && 1120 stat->length == -1 && 1121 !stat->name.size && 1122 !stat->uid.size && 1123 !stat->gid.size && 1124 !stat->muid.size && 1125 stat->n_uid == -1 && 1126 stat->n_gid == -1 && 1127 stat->n_muid == -1) { 1128 return 1; 1129 } 1130 1131 return 0; 1132 } 1133 1134 static void v9fs_stat_init(V9fsStat *stat) 1135 { 1136 v9fs_string_init(&stat->name); 1137 v9fs_string_init(&stat->uid); 1138 v9fs_string_init(&stat->gid); 1139 v9fs_string_init(&stat->muid); 1140 v9fs_string_init(&stat->extension); 1141 } 1142 1143 static void v9fs_stat_free(V9fsStat *stat) 1144 { 1145 v9fs_string_free(&stat->name); 1146 v9fs_string_free(&stat->uid); 1147 v9fs_string_free(&stat->gid); 1148 v9fs_string_free(&stat->muid); 1149 v9fs_string_free(&stat->extension); 1150 } 1151 1152 static uint32_t stat_to_v9mode(const struct stat *stbuf) 1153 { 1154 uint32_t mode; 1155 1156 mode = stbuf->st_mode & 0777; 1157 if (S_ISDIR(stbuf->st_mode)) { 1158 mode |= P9_STAT_MODE_DIR; 1159 } 1160 1161 if (S_ISLNK(stbuf->st_mode)) { 1162 mode |= P9_STAT_MODE_SYMLINK; 1163 } 1164 1165 if (S_ISSOCK(stbuf->st_mode)) { 1166 mode |= P9_STAT_MODE_SOCKET; 1167 } 1168 1169 if (S_ISFIFO(stbuf->st_mode)) { 1170 mode |= P9_STAT_MODE_NAMED_PIPE; 1171 } 1172 1173 if (S_ISBLK(stbuf->st_mode) || S_ISCHR(stbuf->st_mode)) { 1174 mode |= P9_STAT_MODE_DEVICE; 1175 } 1176 1177 if (stbuf->st_mode & S_ISUID) { 1178 mode |= P9_STAT_MODE_SETUID; 1179 } 1180 1181 if (stbuf->st_mode & S_ISGID) { 1182 mode |= P9_STAT_MODE_SETGID; 1183 } 1184 1185 if (stbuf->st_mode & S_ISVTX) { 1186 mode |= P9_STAT_MODE_SETVTX; 1187 } 1188 1189 return mode; 1190 } 1191 1192 static int coroutine_fn stat_to_v9stat(V9fsPDU *pdu, V9fsPath *path, 1193 const char *basename, 1194 const struct stat *stbuf, 1195 V9fsStat *v9stat) 1196 { 1197 int err; 1198 1199 memset(v9stat, 0, sizeof(*v9stat)); 1200 1201 err = stat_to_qid(pdu, stbuf, &v9stat->qid); 1202 if (err < 0) { 1203 return err; 1204 } 1205 v9stat->mode = stat_to_v9mode(stbuf); 1206 v9stat->atime = stbuf->st_atime; 1207 v9stat->mtime = stbuf->st_mtime; 1208 v9stat->length = stbuf->st_size; 1209 1210 v9fs_string_free(&v9stat->uid); 1211 v9fs_string_free(&v9stat->gid); 1212 v9fs_string_free(&v9stat->muid); 1213 1214 v9stat->n_uid = stbuf->st_uid; 1215 v9stat->n_gid = stbuf->st_gid; 1216 v9stat->n_muid = 0; 1217 1218 v9fs_string_free(&v9stat->extension); 1219 1220 if (v9stat->mode & P9_STAT_MODE_SYMLINK) { 1221 err = v9fs_co_readlink(pdu, path, &v9stat->extension); 1222 if (err < 0) { 1223 return err; 1224 } 1225 } else if (v9stat->mode & P9_STAT_MODE_DEVICE) { 1226 v9fs_string_sprintf(&v9stat->extension, "%c %u %u", 1227 S_ISCHR(stbuf->st_mode) ? 'c' : 'b', 1228 major(stbuf->st_rdev), minor(stbuf->st_rdev)); 1229 } else if (S_ISDIR(stbuf->st_mode) || S_ISREG(stbuf->st_mode)) { 1230 v9fs_string_sprintf(&v9stat->extension, "%s %lu", 1231 "HARDLINKCOUNT", (unsigned long)stbuf->st_nlink); 1232 } 1233 1234 v9fs_string_sprintf(&v9stat->name, "%s", basename); 1235 1236 v9stat->size = 61 + 1237 v9fs_string_size(&v9stat->name) + 1238 v9fs_string_size(&v9stat->uid) + 1239 v9fs_string_size(&v9stat->gid) + 1240 v9fs_string_size(&v9stat->muid) + 1241 v9fs_string_size(&v9stat->extension); 1242 return 0; 1243 } 1244 1245 #define P9_STATS_MODE 0x00000001ULL 1246 #define P9_STATS_NLINK 0x00000002ULL 1247 #define P9_STATS_UID 0x00000004ULL 1248 #define P9_STATS_GID 0x00000008ULL 1249 #define P9_STATS_RDEV 0x00000010ULL 1250 #define P9_STATS_ATIME 0x00000020ULL 1251 #define P9_STATS_MTIME 0x00000040ULL 1252 #define P9_STATS_CTIME 0x00000080ULL 1253 #define P9_STATS_INO 0x00000100ULL 1254 #define P9_STATS_SIZE 0x00000200ULL 1255 #define P9_STATS_BLOCKS 0x00000400ULL 1256 1257 #define P9_STATS_BTIME 0x00000800ULL 1258 #define P9_STATS_GEN 0x00001000ULL 1259 #define P9_STATS_DATA_VERSION 0x00002000ULL 1260 1261 #define P9_STATS_BASIC 0x000007ffULL /* Mask for fields up to BLOCKS */ 1262 #define P9_STATS_ALL 0x00003fffULL /* Mask for All fields above */ 1263 1264 1265 /** 1266 * Convert host filesystem's block size into an appropriate block size for 1267 * 9p client (guest OS side). The value returned suggests an "optimum" block 1268 * size for 9p I/O, i.e. to maximize performance. 1269 * 1270 * @pdu: 9p client request 1271 * @blksize: host filesystem's block size 1272 */ 1273 static int32_t blksize_to_iounit(const V9fsPDU *pdu, int32_t blksize) 1274 { 1275 int32_t iounit = 0; 1276 V9fsState *s = pdu->s; 1277 1278 /* 1279 * iounit should be multiples of blksize (host filesystem block size) 1280 * as well as less than (client msize - P9_IOHDRSZ) 1281 */ 1282 if (blksize) { 1283 iounit = blksize; 1284 iounit *= (s->msize - P9_IOHDRSZ) / blksize; 1285 } 1286 if (!iounit) { 1287 iounit = s->msize - P9_IOHDRSZ; 1288 } 1289 return iounit; 1290 } 1291 1292 static int32_t stat_to_iounit(const V9fsPDU *pdu, const struct stat *stbuf) 1293 { 1294 return blksize_to_iounit(pdu, stbuf->st_blksize); 1295 } 1296 1297 static int stat_to_v9stat_dotl(V9fsPDU *pdu, const struct stat *stbuf, 1298 V9fsStatDotl *v9lstat) 1299 { 1300 memset(v9lstat, 0, sizeof(*v9lstat)); 1301 1302 v9lstat->st_mode = stbuf->st_mode; 1303 v9lstat->st_nlink = stbuf->st_nlink; 1304 v9lstat->st_uid = stbuf->st_uid; 1305 v9lstat->st_gid = stbuf->st_gid; 1306 v9lstat->st_rdev = stbuf->st_rdev; 1307 v9lstat->st_size = stbuf->st_size; 1308 v9lstat->st_blksize = stat_to_iounit(pdu, stbuf); 1309 v9lstat->st_blocks = stbuf->st_blocks; 1310 v9lstat->st_atime_sec = stbuf->st_atime; 1311 v9lstat->st_atime_nsec = stbuf->st_atim.tv_nsec; 1312 v9lstat->st_mtime_sec = stbuf->st_mtime; 1313 v9lstat->st_mtime_nsec = stbuf->st_mtim.tv_nsec; 1314 v9lstat->st_ctime_sec = stbuf->st_ctime; 1315 v9lstat->st_ctime_nsec = stbuf->st_ctim.tv_nsec; 1316 /* Currently we only support BASIC fields in stat */ 1317 v9lstat->st_result_mask = P9_STATS_BASIC; 1318 1319 return stat_to_qid(pdu, stbuf, &v9lstat->qid); 1320 } 1321 1322 static void print_sg(struct iovec *sg, int cnt) 1323 { 1324 int i; 1325 1326 printf("sg[%d]: {", cnt); 1327 for (i = 0; i < cnt; i++) { 1328 if (i) { 1329 printf(", "); 1330 } 1331 printf("(%p, %zd)", sg[i].iov_base, sg[i].iov_len); 1332 } 1333 printf("}\n"); 1334 } 1335 1336 /* Will call this only for path name based fid */ 1337 static void v9fs_fix_path(V9fsPath *dst, V9fsPath *src, int len) 1338 { 1339 V9fsPath str; 1340 v9fs_path_init(&str); 1341 v9fs_path_copy(&str, dst); 1342 v9fs_path_sprintf(dst, "%s%s", src->data, str.data + len); 1343 v9fs_path_free(&str); 1344 } 1345 1346 static inline bool is_ro_export(FsContext *ctx) 1347 { 1348 return ctx->export_flags & V9FS_RDONLY; 1349 } 1350 1351 static void coroutine_fn v9fs_version(void *opaque) 1352 { 1353 ssize_t err; 1354 V9fsPDU *pdu = opaque; 1355 V9fsState *s = pdu->s; 1356 V9fsString version; 1357 size_t offset = 7; 1358 1359 v9fs_string_init(&version); 1360 err = pdu_unmarshal(pdu, offset, "ds", &s->msize, &version); 1361 if (err < 0) { 1362 goto out; 1363 } 1364 trace_v9fs_version(pdu->tag, pdu->id, s->msize, version.data); 1365 1366 virtfs_reset(pdu); 1367 1368 if (!strcmp(version.data, "9P2000.u")) { 1369 s->proto_version = V9FS_PROTO_2000U; 1370 } else if (!strcmp(version.data, "9P2000.L")) { 1371 s->proto_version = V9FS_PROTO_2000L; 1372 } else { 1373 v9fs_string_sprintf(&version, "unknown"); 1374 /* skip min. msize check, reporting invalid version has priority */ 1375 goto marshal; 1376 } 1377 1378 if (s->msize < P9_MIN_MSIZE) { 1379 err = -EMSGSIZE; 1380 error_report( 1381 "9pfs: Client requested msize < minimum msize (" 1382 stringify(P9_MIN_MSIZE) ") supported by this server." 1383 ); 1384 goto out; 1385 } 1386 1387 /* 8192 is the default msize of Linux clients */ 1388 if (s->msize <= 8192 && !(s->ctx.export_flags & V9FS_NO_PERF_WARN)) { 1389 warn_report_once( 1390 "9p: degraded performance: a reasonable high msize should be " 1391 "chosen on client/guest side (chosen msize is <= 8192). See " 1392 "https://wiki.qemu.org/Documentation/9psetup#msize for details." 1393 ); 1394 } 1395 1396 marshal: 1397 err = pdu_marshal(pdu, offset, "ds", s->msize, &version); 1398 if (err < 0) { 1399 goto out; 1400 } 1401 err += offset; 1402 trace_v9fs_version_return(pdu->tag, pdu->id, s->msize, version.data); 1403 out: 1404 pdu_complete(pdu, err); 1405 v9fs_string_free(&version); 1406 } 1407 1408 static void coroutine_fn v9fs_attach(void *opaque) 1409 { 1410 V9fsPDU *pdu = opaque; 1411 V9fsState *s = pdu->s; 1412 int32_t fid, afid, n_uname; 1413 V9fsString uname, aname; 1414 V9fsFidState *fidp; 1415 size_t offset = 7; 1416 V9fsQID qid; 1417 ssize_t err; 1418 struct stat stbuf; 1419 1420 v9fs_string_init(&uname); 1421 v9fs_string_init(&aname); 1422 err = pdu_unmarshal(pdu, offset, "ddssd", &fid, 1423 &afid, &uname, &aname, &n_uname); 1424 if (err < 0) { 1425 goto out_nofid; 1426 } 1427 trace_v9fs_attach(pdu->tag, pdu->id, fid, afid, uname.data, aname.data); 1428 1429 fidp = alloc_fid(s, fid); 1430 if (fidp == NULL) { 1431 err = -EINVAL; 1432 goto out_nofid; 1433 } 1434 fidp->uid = n_uname; 1435 err = v9fs_co_name_to_path(pdu, NULL, "/", &fidp->path); 1436 if (err < 0) { 1437 err = -EINVAL; 1438 clunk_fid(s, fid); 1439 goto out; 1440 } 1441 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf); 1442 if (err < 0) { 1443 err = -EINVAL; 1444 clunk_fid(s, fid); 1445 goto out; 1446 } 1447 err = stat_to_qid(pdu, &stbuf, &qid); 1448 if (err < 0) { 1449 err = -EINVAL; 1450 clunk_fid(s, fid); 1451 goto out; 1452 } 1453 1454 /* 1455 * disable migration if we haven't done already. 1456 * attach could get called multiple times for the same export. 1457 */ 1458 if (!s->migration_blocker) { 1459 error_setg(&s->migration_blocker, 1460 "Migration is disabled when VirtFS export path '%s' is mounted in the guest using mount_tag '%s'", 1461 s->ctx.fs_root ? s->ctx.fs_root : "NULL", s->tag); 1462 err = migrate_add_blocker(s->migration_blocker, NULL); 1463 if (err < 0) { 1464 error_free(s->migration_blocker); 1465 s->migration_blocker = NULL; 1466 clunk_fid(s, fid); 1467 goto out; 1468 } 1469 s->root_fid = fid; 1470 } 1471 1472 err = pdu_marshal(pdu, offset, "Q", &qid); 1473 if (err < 0) { 1474 clunk_fid(s, fid); 1475 goto out; 1476 } 1477 err += offset; 1478 1479 memcpy(&s->root_st, &stbuf, sizeof(stbuf)); 1480 trace_v9fs_attach_return(pdu->tag, pdu->id, 1481 qid.type, qid.version, qid.path); 1482 out: 1483 put_fid(pdu, fidp); 1484 out_nofid: 1485 pdu_complete(pdu, err); 1486 v9fs_string_free(&uname); 1487 v9fs_string_free(&aname); 1488 } 1489 1490 static void coroutine_fn v9fs_stat(void *opaque) 1491 { 1492 int32_t fid; 1493 V9fsStat v9stat; 1494 ssize_t err = 0; 1495 size_t offset = 7; 1496 struct stat stbuf; 1497 V9fsFidState *fidp; 1498 V9fsPDU *pdu = opaque; 1499 char *basename; 1500 1501 err = pdu_unmarshal(pdu, offset, "d", &fid); 1502 if (err < 0) { 1503 goto out_nofid; 1504 } 1505 trace_v9fs_stat(pdu->tag, pdu->id, fid); 1506 1507 fidp = get_fid(pdu, fid); 1508 if (fidp == NULL) { 1509 err = -ENOENT; 1510 goto out_nofid; 1511 } 1512 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf); 1513 if (err < 0) { 1514 goto out; 1515 } 1516 basename = g_path_get_basename(fidp->path.data); 1517 err = stat_to_v9stat(pdu, &fidp->path, basename, &stbuf, &v9stat); 1518 g_free(basename); 1519 if (err < 0) { 1520 goto out; 1521 } 1522 err = pdu_marshal(pdu, offset, "wS", 0, &v9stat); 1523 if (err < 0) { 1524 v9fs_stat_free(&v9stat); 1525 goto out; 1526 } 1527 trace_v9fs_stat_return(pdu->tag, pdu->id, v9stat.mode, 1528 v9stat.atime, v9stat.mtime, v9stat.length); 1529 err += offset; 1530 v9fs_stat_free(&v9stat); 1531 out: 1532 put_fid(pdu, fidp); 1533 out_nofid: 1534 pdu_complete(pdu, err); 1535 } 1536 1537 static void coroutine_fn v9fs_getattr(void *opaque) 1538 { 1539 int32_t fid; 1540 size_t offset = 7; 1541 ssize_t retval = 0; 1542 struct stat stbuf; 1543 V9fsFidState *fidp; 1544 uint64_t request_mask; 1545 V9fsStatDotl v9stat_dotl; 1546 V9fsPDU *pdu = opaque; 1547 1548 retval = pdu_unmarshal(pdu, offset, "dq", &fid, &request_mask); 1549 if (retval < 0) { 1550 goto out_nofid; 1551 } 1552 trace_v9fs_getattr(pdu->tag, pdu->id, fid, request_mask); 1553 1554 fidp = get_fid(pdu, fid); 1555 if (fidp == NULL) { 1556 retval = -ENOENT; 1557 goto out_nofid; 1558 } 1559 /* 1560 * Currently we only support BASIC fields in stat, so there is no 1561 * need to look at request_mask. 1562 */ 1563 retval = v9fs_co_lstat(pdu, &fidp->path, &stbuf); 1564 if (retval < 0) { 1565 goto out; 1566 } 1567 retval = stat_to_v9stat_dotl(pdu, &stbuf, &v9stat_dotl); 1568 if (retval < 0) { 1569 goto out; 1570 } 1571 1572 /* fill st_gen if requested and supported by underlying fs */ 1573 if (request_mask & P9_STATS_GEN) { 1574 retval = v9fs_co_st_gen(pdu, &fidp->path, stbuf.st_mode, &v9stat_dotl); 1575 switch (retval) { 1576 case 0: 1577 /* we have valid st_gen: update result mask */ 1578 v9stat_dotl.st_result_mask |= P9_STATS_GEN; 1579 break; 1580 case -EINTR: 1581 /* request cancelled, e.g. by Tflush */ 1582 goto out; 1583 default: 1584 /* failed to get st_gen: not fatal, ignore */ 1585 break; 1586 } 1587 } 1588 retval = pdu_marshal(pdu, offset, "A", &v9stat_dotl); 1589 if (retval < 0) { 1590 goto out; 1591 } 1592 retval += offset; 1593 trace_v9fs_getattr_return(pdu->tag, pdu->id, v9stat_dotl.st_result_mask, 1594 v9stat_dotl.st_mode, v9stat_dotl.st_uid, 1595 v9stat_dotl.st_gid); 1596 out: 1597 put_fid(pdu, fidp); 1598 out_nofid: 1599 pdu_complete(pdu, retval); 1600 } 1601 1602 /* Attribute flags */ 1603 #define P9_ATTR_MODE (1 << 0) 1604 #define P9_ATTR_UID (1 << 1) 1605 #define P9_ATTR_GID (1 << 2) 1606 #define P9_ATTR_SIZE (1 << 3) 1607 #define P9_ATTR_ATIME (1 << 4) 1608 #define P9_ATTR_MTIME (1 << 5) 1609 #define P9_ATTR_CTIME (1 << 6) 1610 #define P9_ATTR_ATIME_SET (1 << 7) 1611 #define P9_ATTR_MTIME_SET (1 << 8) 1612 1613 #define P9_ATTR_MASK 127 1614 1615 static void coroutine_fn v9fs_setattr(void *opaque) 1616 { 1617 int err = 0; 1618 int32_t fid; 1619 V9fsFidState *fidp; 1620 size_t offset = 7; 1621 V9fsIattr v9iattr; 1622 V9fsPDU *pdu = opaque; 1623 1624 err = pdu_unmarshal(pdu, offset, "dI", &fid, &v9iattr); 1625 if (err < 0) { 1626 goto out_nofid; 1627 } 1628 1629 trace_v9fs_setattr(pdu->tag, pdu->id, fid, 1630 v9iattr.valid, v9iattr.mode, v9iattr.uid, v9iattr.gid, 1631 v9iattr.size, v9iattr.atime_sec, v9iattr.mtime_sec); 1632 1633 fidp = get_fid(pdu, fid); 1634 if (fidp == NULL) { 1635 err = -EINVAL; 1636 goto out_nofid; 1637 } 1638 if (v9iattr.valid & P9_ATTR_MODE) { 1639 err = v9fs_co_chmod(pdu, &fidp->path, v9iattr.mode); 1640 if (err < 0) { 1641 goto out; 1642 } 1643 } 1644 if (v9iattr.valid & (P9_ATTR_ATIME | P9_ATTR_MTIME)) { 1645 struct timespec times[2]; 1646 if (v9iattr.valid & P9_ATTR_ATIME) { 1647 if (v9iattr.valid & P9_ATTR_ATIME_SET) { 1648 times[0].tv_sec = v9iattr.atime_sec; 1649 times[0].tv_nsec = v9iattr.atime_nsec; 1650 } else { 1651 times[0].tv_nsec = UTIME_NOW; 1652 } 1653 } else { 1654 times[0].tv_nsec = UTIME_OMIT; 1655 } 1656 if (v9iattr.valid & P9_ATTR_MTIME) { 1657 if (v9iattr.valid & P9_ATTR_MTIME_SET) { 1658 times[1].tv_sec = v9iattr.mtime_sec; 1659 times[1].tv_nsec = v9iattr.mtime_nsec; 1660 } else { 1661 times[1].tv_nsec = UTIME_NOW; 1662 } 1663 } else { 1664 times[1].tv_nsec = UTIME_OMIT; 1665 } 1666 err = v9fs_co_utimensat(pdu, &fidp->path, times); 1667 if (err < 0) { 1668 goto out; 1669 } 1670 } 1671 /* 1672 * If the only valid entry in iattr is ctime we can call 1673 * chown(-1,-1) to update the ctime of the file 1674 */ 1675 if ((v9iattr.valid & (P9_ATTR_UID | P9_ATTR_GID)) || 1676 ((v9iattr.valid & P9_ATTR_CTIME) 1677 && !((v9iattr.valid & P9_ATTR_MASK) & ~P9_ATTR_CTIME))) { 1678 if (!(v9iattr.valid & P9_ATTR_UID)) { 1679 v9iattr.uid = -1; 1680 } 1681 if (!(v9iattr.valid & P9_ATTR_GID)) { 1682 v9iattr.gid = -1; 1683 } 1684 err = v9fs_co_chown(pdu, &fidp->path, v9iattr.uid, 1685 v9iattr.gid); 1686 if (err < 0) { 1687 goto out; 1688 } 1689 } 1690 if (v9iattr.valid & (P9_ATTR_SIZE)) { 1691 err = v9fs_co_truncate(pdu, &fidp->path, v9iattr.size); 1692 if (err < 0) { 1693 goto out; 1694 } 1695 } 1696 err = offset; 1697 trace_v9fs_setattr_return(pdu->tag, pdu->id); 1698 out: 1699 put_fid(pdu, fidp); 1700 out_nofid: 1701 pdu_complete(pdu, err); 1702 } 1703 1704 static int v9fs_walk_marshal(V9fsPDU *pdu, uint16_t nwnames, V9fsQID *qids) 1705 { 1706 int i; 1707 ssize_t err; 1708 size_t offset = 7; 1709 1710 err = pdu_marshal(pdu, offset, "w", nwnames); 1711 if (err < 0) { 1712 return err; 1713 } 1714 offset += err; 1715 for (i = 0; i < nwnames; i++) { 1716 err = pdu_marshal(pdu, offset, "Q", &qids[i]); 1717 if (err < 0) { 1718 return err; 1719 } 1720 offset += err; 1721 } 1722 return offset; 1723 } 1724 1725 static bool name_is_illegal(const char *name) 1726 { 1727 return !*name || strchr(name, '/') != NULL; 1728 } 1729 1730 static bool same_stat_id(const struct stat *a, const struct stat *b) 1731 { 1732 return a->st_dev == b->st_dev && a->st_ino == b->st_ino; 1733 } 1734 1735 static void coroutine_fn v9fs_walk(void *opaque) 1736 { 1737 int name_idx; 1738 g_autofree V9fsQID *qids = NULL; 1739 int i, err = 0; 1740 V9fsPath dpath, path, *pathes = NULL; 1741 uint16_t nwnames; 1742 struct stat stbuf, fidst; 1743 g_autofree struct stat *stbufs = NULL; 1744 size_t offset = 7; 1745 int32_t fid, newfid; 1746 V9fsString *wnames = NULL; 1747 V9fsFidState *fidp; 1748 V9fsFidState *newfidp = NULL; 1749 V9fsPDU *pdu = opaque; 1750 V9fsState *s = pdu->s; 1751 V9fsQID qid; 1752 1753 err = pdu_unmarshal(pdu, offset, "ddw", &fid, &newfid, &nwnames); 1754 if (err < 0) { 1755 pdu_complete(pdu, err); 1756 return ; 1757 } 1758 offset += err; 1759 1760 trace_v9fs_walk(pdu->tag, pdu->id, fid, newfid, nwnames); 1761 1762 if (nwnames > P9_MAXWELEM) { 1763 err = -EINVAL; 1764 goto out_nofid; 1765 } 1766 if (nwnames) { 1767 wnames = g_new0(V9fsString, nwnames); 1768 qids = g_new0(V9fsQID, nwnames); 1769 stbufs = g_new0(struct stat, nwnames); 1770 pathes = g_new0(V9fsPath, nwnames); 1771 for (i = 0; i < nwnames; i++) { 1772 err = pdu_unmarshal(pdu, offset, "s", &wnames[i]); 1773 if (err < 0) { 1774 goto out_nofid; 1775 } 1776 if (name_is_illegal(wnames[i].data)) { 1777 err = -ENOENT; 1778 goto out_nofid; 1779 } 1780 offset += err; 1781 } 1782 } 1783 fidp = get_fid(pdu, fid); 1784 if (fidp == NULL) { 1785 err = -ENOENT; 1786 goto out_nofid; 1787 } 1788 1789 v9fs_path_init(&dpath); 1790 v9fs_path_init(&path); 1791 /* 1792 * Both dpath and path initially point to fidp. 1793 * Needed to handle request with nwnames == 0 1794 */ 1795 v9fs_path_copy(&dpath, &fidp->path); 1796 v9fs_path_copy(&path, &fidp->path); 1797 1798 /* 1799 * To keep latency (i.e. overall execution time for processing this 1800 * Twalk client request) as small as possible, run all the required fs 1801 * driver code altogether inside the following block. 1802 */ 1803 v9fs_co_run_in_worker({ 1804 if (v9fs_request_cancelled(pdu)) { 1805 err = -EINTR; 1806 break; 1807 } 1808 err = s->ops->lstat(&s->ctx, &dpath, &fidst); 1809 if (err < 0) { 1810 err = -errno; 1811 break; 1812 } 1813 stbuf = fidst; 1814 for (name_idx = 0; name_idx < nwnames; name_idx++) { 1815 if (v9fs_request_cancelled(pdu)) { 1816 err = -EINTR; 1817 break; 1818 } 1819 if (!same_stat_id(&pdu->s->root_st, &stbuf) || 1820 strcmp("..", wnames[name_idx].data)) 1821 { 1822 err = s->ops->name_to_path(&s->ctx, &dpath, 1823 wnames[name_idx].data, 1824 &pathes[name_idx]); 1825 if (err < 0) { 1826 err = -errno; 1827 break; 1828 } 1829 if (v9fs_request_cancelled(pdu)) { 1830 err = -EINTR; 1831 break; 1832 } 1833 err = s->ops->lstat(&s->ctx, &pathes[name_idx], &stbuf); 1834 if (err < 0) { 1835 err = -errno; 1836 break; 1837 } 1838 stbufs[name_idx] = stbuf; 1839 v9fs_path_copy(&dpath, &pathes[name_idx]); 1840 } 1841 } 1842 }); 1843 /* 1844 * Handle all the rest of this Twalk request on main thread ... 1845 */ 1846 if (err < 0) { 1847 goto out; 1848 } 1849 1850 err = stat_to_qid(pdu, &fidst, &qid); 1851 if (err < 0) { 1852 goto out; 1853 } 1854 stbuf = fidst; 1855 1856 /* reset dpath and path */ 1857 v9fs_path_copy(&dpath, &fidp->path); 1858 v9fs_path_copy(&path, &fidp->path); 1859 1860 for (name_idx = 0; name_idx < nwnames; name_idx++) { 1861 if (!same_stat_id(&pdu->s->root_st, &stbuf) || 1862 strcmp("..", wnames[name_idx].data)) 1863 { 1864 stbuf = stbufs[name_idx]; 1865 err = stat_to_qid(pdu, &stbuf, &qid); 1866 if (err < 0) { 1867 goto out; 1868 } 1869 v9fs_path_copy(&path, &pathes[name_idx]); 1870 v9fs_path_copy(&dpath, &path); 1871 } 1872 memcpy(&qids[name_idx], &qid, sizeof(qid)); 1873 } 1874 if (fid == newfid) { 1875 if (fidp->fid_type != P9_FID_NONE) { 1876 err = -EINVAL; 1877 goto out; 1878 } 1879 v9fs_path_write_lock(s); 1880 v9fs_path_copy(&fidp->path, &path); 1881 v9fs_path_unlock(s); 1882 } else { 1883 newfidp = alloc_fid(s, newfid); 1884 if (newfidp == NULL) { 1885 err = -EINVAL; 1886 goto out; 1887 } 1888 newfidp->uid = fidp->uid; 1889 v9fs_path_copy(&newfidp->path, &path); 1890 } 1891 err = v9fs_walk_marshal(pdu, nwnames, qids); 1892 trace_v9fs_walk_return(pdu->tag, pdu->id, nwnames, qids); 1893 out: 1894 put_fid(pdu, fidp); 1895 if (newfidp) { 1896 put_fid(pdu, newfidp); 1897 } 1898 v9fs_path_free(&dpath); 1899 v9fs_path_free(&path); 1900 out_nofid: 1901 pdu_complete(pdu, err); 1902 if (nwnames && nwnames <= P9_MAXWELEM) { 1903 for (name_idx = 0; name_idx < nwnames; name_idx++) { 1904 v9fs_string_free(&wnames[name_idx]); 1905 v9fs_path_free(&pathes[name_idx]); 1906 } 1907 g_free(wnames); 1908 g_free(pathes); 1909 } 1910 } 1911 1912 static int32_t coroutine_fn get_iounit(V9fsPDU *pdu, V9fsPath *path) 1913 { 1914 struct statfs stbuf; 1915 int err = v9fs_co_statfs(pdu, path, &stbuf); 1916 1917 return blksize_to_iounit(pdu, (err >= 0) ? stbuf.f_bsize : 0); 1918 } 1919 1920 static void coroutine_fn v9fs_open(void *opaque) 1921 { 1922 int flags; 1923 int32_t fid; 1924 int32_t mode; 1925 V9fsQID qid; 1926 int iounit = 0; 1927 ssize_t err = 0; 1928 size_t offset = 7; 1929 struct stat stbuf; 1930 V9fsFidState *fidp; 1931 V9fsPDU *pdu = opaque; 1932 V9fsState *s = pdu->s; 1933 1934 if (s->proto_version == V9FS_PROTO_2000L) { 1935 err = pdu_unmarshal(pdu, offset, "dd", &fid, &mode); 1936 } else { 1937 uint8_t modebyte; 1938 err = pdu_unmarshal(pdu, offset, "db", &fid, &modebyte); 1939 mode = modebyte; 1940 } 1941 if (err < 0) { 1942 goto out_nofid; 1943 } 1944 trace_v9fs_open(pdu->tag, pdu->id, fid, mode); 1945 1946 fidp = get_fid(pdu, fid); 1947 if (fidp == NULL) { 1948 err = -ENOENT; 1949 goto out_nofid; 1950 } 1951 if (fidp->fid_type != P9_FID_NONE) { 1952 err = -EINVAL; 1953 goto out; 1954 } 1955 1956 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf); 1957 if (err < 0) { 1958 goto out; 1959 } 1960 err = stat_to_qid(pdu, &stbuf, &qid); 1961 if (err < 0) { 1962 goto out; 1963 } 1964 if (S_ISDIR(stbuf.st_mode)) { 1965 err = v9fs_co_opendir(pdu, fidp); 1966 if (err < 0) { 1967 goto out; 1968 } 1969 fidp->fid_type = P9_FID_DIR; 1970 err = pdu_marshal(pdu, offset, "Qd", &qid, 0); 1971 if (err < 0) { 1972 goto out; 1973 } 1974 err += offset; 1975 } else { 1976 if (s->proto_version == V9FS_PROTO_2000L) { 1977 flags = get_dotl_openflags(s, mode); 1978 } else { 1979 flags = omode_to_uflags(mode); 1980 } 1981 if (is_ro_export(&s->ctx)) { 1982 if (mode & O_WRONLY || mode & O_RDWR || 1983 mode & O_APPEND || mode & O_TRUNC) { 1984 err = -EROFS; 1985 goto out; 1986 } 1987 } 1988 err = v9fs_co_open(pdu, fidp, flags); 1989 if (err < 0) { 1990 goto out; 1991 } 1992 fidp->fid_type = P9_FID_FILE; 1993 fidp->open_flags = flags; 1994 if (flags & O_EXCL) { 1995 /* 1996 * We let the host file system do O_EXCL check 1997 * We should not reclaim such fd 1998 */ 1999 fidp->flags |= FID_NON_RECLAIMABLE; 2000 } 2001 iounit = get_iounit(pdu, &fidp->path); 2002 err = pdu_marshal(pdu, offset, "Qd", &qid, iounit); 2003 if (err < 0) { 2004 goto out; 2005 } 2006 err += offset; 2007 } 2008 trace_v9fs_open_return(pdu->tag, pdu->id, 2009 qid.type, qid.version, qid.path, iounit); 2010 out: 2011 put_fid(pdu, fidp); 2012 out_nofid: 2013 pdu_complete(pdu, err); 2014 } 2015 2016 static void coroutine_fn v9fs_lcreate(void *opaque) 2017 { 2018 int32_t dfid, flags, mode; 2019 gid_t gid; 2020 ssize_t err = 0; 2021 ssize_t offset = 7; 2022 V9fsString name; 2023 V9fsFidState *fidp; 2024 struct stat stbuf; 2025 V9fsQID qid; 2026 int32_t iounit; 2027 V9fsPDU *pdu = opaque; 2028 2029 v9fs_string_init(&name); 2030 err = pdu_unmarshal(pdu, offset, "dsddd", &dfid, 2031 &name, &flags, &mode, &gid); 2032 if (err < 0) { 2033 goto out_nofid; 2034 } 2035 trace_v9fs_lcreate(pdu->tag, pdu->id, dfid, flags, mode, gid); 2036 2037 if (name_is_illegal(name.data)) { 2038 err = -ENOENT; 2039 goto out_nofid; 2040 } 2041 2042 if (!strcmp(".", name.data) || !strcmp("..", name.data)) { 2043 err = -EEXIST; 2044 goto out_nofid; 2045 } 2046 2047 fidp = get_fid(pdu, dfid); 2048 if (fidp == NULL) { 2049 err = -ENOENT; 2050 goto out_nofid; 2051 } 2052 if (fidp->fid_type != P9_FID_NONE) { 2053 err = -EINVAL; 2054 goto out; 2055 } 2056 2057 flags = get_dotl_openflags(pdu->s, flags); 2058 err = v9fs_co_open2(pdu, fidp, &name, gid, 2059 flags | O_CREAT, mode, &stbuf); 2060 if (err < 0) { 2061 goto out; 2062 } 2063 fidp->fid_type = P9_FID_FILE; 2064 fidp->open_flags = flags; 2065 if (flags & O_EXCL) { 2066 /* 2067 * We let the host file system do O_EXCL check 2068 * We should not reclaim such fd 2069 */ 2070 fidp->flags |= FID_NON_RECLAIMABLE; 2071 } 2072 iounit = get_iounit(pdu, &fidp->path); 2073 err = stat_to_qid(pdu, &stbuf, &qid); 2074 if (err < 0) { 2075 goto out; 2076 } 2077 err = pdu_marshal(pdu, offset, "Qd", &qid, iounit); 2078 if (err < 0) { 2079 goto out; 2080 } 2081 err += offset; 2082 trace_v9fs_lcreate_return(pdu->tag, pdu->id, 2083 qid.type, qid.version, qid.path, iounit); 2084 out: 2085 put_fid(pdu, fidp); 2086 out_nofid: 2087 pdu_complete(pdu, err); 2088 v9fs_string_free(&name); 2089 } 2090 2091 static void coroutine_fn v9fs_fsync(void *opaque) 2092 { 2093 int err; 2094 int32_t fid; 2095 int datasync; 2096 size_t offset = 7; 2097 V9fsFidState *fidp; 2098 V9fsPDU *pdu = opaque; 2099 2100 err = pdu_unmarshal(pdu, offset, "dd", &fid, &datasync); 2101 if (err < 0) { 2102 goto out_nofid; 2103 } 2104 trace_v9fs_fsync(pdu->tag, pdu->id, fid, datasync); 2105 2106 fidp = get_fid(pdu, fid); 2107 if (fidp == NULL) { 2108 err = -ENOENT; 2109 goto out_nofid; 2110 } 2111 err = v9fs_co_fsync(pdu, fidp, datasync); 2112 if (!err) { 2113 err = offset; 2114 } 2115 put_fid(pdu, fidp); 2116 out_nofid: 2117 pdu_complete(pdu, err); 2118 } 2119 2120 static void coroutine_fn v9fs_clunk(void *opaque) 2121 { 2122 int err; 2123 int32_t fid; 2124 size_t offset = 7; 2125 V9fsFidState *fidp; 2126 V9fsPDU *pdu = opaque; 2127 V9fsState *s = pdu->s; 2128 2129 err = pdu_unmarshal(pdu, offset, "d", &fid); 2130 if (err < 0) { 2131 goto out_nofid; 2132 } 2133 trace_v9fs_clunk(pdu->tag, pdu->id, fid); 2134 2135 fidp = clunk_fid(s, fid); 2136 if (fidp == NULL) { 2137 err = -ENOENT; 2138 goto out_nofid; 2139 } 2140 /* 2141 * Bump the ref so that put_fid will 2142 * free the fid. 2143 */ 2144 fidp->ref++; 2145 err = put_fid(pdu, fidp); 2146 if (!err) { 2147 err = offset; 2148 } 2149 out_nofid: 2150 pdu_complete(pdu, err); 2151 } 2152 2153 /* 2154 * Create a QEMUIOVector for a sub-region of PDU iovecs 2155 * 2156 * @qiov: uninitialized QEMUIOVector 2157 * @skip: number of bytes to skip from beginning of PDU 2158 * @size: number of bytes to include 2159 * @is_write: true - write, false - read 2160 * 2161 * The resulting QEMUIOVector has heap-allocated iovecs and must be cleaned up 2162 * with qemu_iovec_destroy(). 2163 */ 2164 static void v9fs_init_qiov_from_pdu(QEMUIOVector *qiov, V9fsPDU *pdu, 2165 size_t skip, size_t size, 2166 bool is_write) 2167 { 2168 QEMUIOVector elem; 2169 struct iovec *iov; 2170 unsigned int niov; 2171 2172 if (is_write) { 2173 pdu->s->transport->init_out_iov_from_pdu(pdu, &iov, &niov, size + skip); 2174 } else { 2175 pdu->s->transport->init_in_iov_from_pdu(pdu, &iov, &niov, size + skip); 2176 } 2177 2178 qemu_iovec_init_external(&elem, iov, niov); 2179 qemu_iovec_init(qiov, niov); 2180 qemu_iovec_concat(qiov, &elem, skip, size); 2181 } 2182 2183 static int v9fs_xattr_read(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp, 2184 uint64_t off, uint32_t max_count) 2185 { 2186 ssize_t err; 2187 size_t offset = 7; 2188 uint64_t read_count; 2189 QEMUIOVector qiov_full; 2190 2191 if (fidp->fs.xattr.len < off) { 2192 read_count = 0; 2193 } else { 2194 read_count = fidp->fs.xattr.len - off; 2195 } 2196 if (read_count > max_count) { 2197 read_count = max_count; 2198 } 2199 err = pdu_marshal(pdu, offset, "d", read_count); 2200 if (err < 0) { 2201 return err; 2202 } 2203 offset += err; 2204 2205 v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset, read_count, false); 2206 err = v9fs_pack(qiov_full.iov, qiov_full.niov, 0, 2207 ((char *)fidp->fs.xattr.value) + off, 2208 read_count); 2209 qemu_iovec_destroy(&qiov_full); 2210 if (err < 0) { 2211 return err; 2212 } 2213 offset += err; 2214 return offset; 2215 } 2216 2217 static int coroutine_fn v9fs_do_readdir_with_stat(V9fsPDU *pdu, 2218 V9fsFidState *fidp, 2219 uint32_t max_count) 2220 { 2221 V9fsPath path; 2222 V9fsStat v9stat; 2223 int len, err = 0; 2224 int32_t count = 0; 2225 struct stat stbuf; 2226 off_t saved_dir_pos; 2227 struct dirent *dent; 2228 2229 /* save the directory position */ 2230 saved_dir_pos = v9fs_co_telldir(pdu, fidp); 2231 if (saved_dir_pos < 0) { 2232 return saved_dir_pos; 2233 } 2234 2235 while (1) { 2236 v9fs_path_init(&path); 2237 2238 v9fs_readdir_lock(&fidp->fs.dir); 2239 2240 err = v9fs_co_readdir(pdu, fidp, &dent); 2241 if (err || !dent) { 2242 break; 2243 } 2244 err = v9fs_co_name_to_path(pdu, &fidp->path, dent->d_name, &path); 2245 if (err < 0) { 2246 break; 2247 } 2248 err = v9fs_co_lstat(pdu, &path, &stbuf); 2249 if (err < 0) { 2250 break; 2251 } 2252 err = stat_to_v9stat(pdu, &path, dent->d_name, &stbuf, &v9stat); 2253 if (err < 0) { 2254 break; 2255 } 2256 if ((count + v9stat.size + 2) > max_count) { 2257 v9fs_readdir_unlock(&fidp->fs.dir); 2258 2259 /* Ran out of buffer. Set dir back to old position and return */ 2260 v9fs_co_seekdir(pdu, fidp, saved_dir_pos); 2261 v9fs_stat_free(&v9stat); 2262 v9fs_path_free(&path); 2263 return count; 2264 } 2265 2266 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */ 2267 len = pdu_marshal(pdu, 11 + count, "S", &v9stat); 2268 2269 v9fs_readdir_unlock(&fidp->fs.dir); 2270 2271 if (len < 0) { 2272 v9fs_co_seekdir(pdu, fidp, saved_dir_pos); 2273 v9fs_stat_free(&v9stat); 2274 v9fs_path_free(&path); 2275 return len; 2276 } 2277 count += len; 2278 v9fs_stat_free(&v9stat); 2279 v9fs_path_free(&path); 2280 saved_dir_pos = dent->d_off; 2281 } 2282 2283 v9fs_readdir_unlock(&fidp->fs.dir); 2284 2285 v9fs_path_free(&path); 2286 if (err < 0) { 2287 return err; 2288 } 2289 return count; 2290 } 2291 2292 static void coroutine_fn v9fs_read(void *opaque) 2293 { 2294 int32_t fid; 2295 uint64_t off; 2296 ssize_t err = 0; 2297 int32_t count = 0; 2298 size_t offset = 7; 2299 uint32_t max_count; 2300 V9fsFidState *fidp; 2301 V9fsPDU *pdu = opaque; 2302 V9fsState *s = pdu->s; 2303 2304 err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &max_count); 2305 if (err < 0) { 2306 goto out_nofid; 2307 } 2308 trace_v9fs_read(pdu->tag, pdu->id, fid, off, max_count); 2309 2310 fidp = get_fid(pdu, fid); 2311 if (fidp == NULL) { 2312 err = -EINVAL; 2313 goto out_nofid; 2314 } 2315 if (fidp->fid_type == P9_FID_DIR) { 2316 if (s->proto_version != V9FS_PROTO_2000U) { 2317 warn_report_once( 2318 "9p: bad client: T_read request on directory only expected " 2319 "with 9P2000.u protocol version" 2320 ); 2321 err = -EOPNOTSUPP; 2322 goto out; 2323 } 2324 if (off == 0) { 2325 v9fs_co_rewinddir(pdu, fidp); 2326 } 2327 count = v9fs_do_readdir_with_stat(pdu, fidp, max_count); 2328 if (count < 0) { 2329 err = count; 2330 goto out; 2331 } 2332 err = pdu_marshal(pdu, offset, "d", count); 2333 if (err < 0) { 2334 goto out; 2335 } 2336 err += offset + count; 2337 } else if (fidp->fid_type == P9_FID_FILE) { 2338 QEMUIOVector qiov_full; 2339 QEMUIOVector qiov; 2340 int32_t len; 2341 2342 v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset + 4, max_count, false); 2343 qemu_iovec_init(&qiov, qiov_full.niov); 2344 do { 2345 qemu_iovec_reset(&qiov); 2346 qemu_iovec_concat(&qiov, &qiov_full, count, qiov_full.size - count); 2347 if (0) { 2348 print_sg(qiov.iov, qiov.niov); 2349 } 2350 /* Loop in case of EINTR */ 2351 do { 2352 len = v9fs_co_preadv(pdu, fidp, qiov.iov, qiov.niov, off); 2353 if (len >= 0) { 2354 off += len; 2355 count += len; 2356 } 2357 } while (len == -EINTR && !pdu->cancelled); 2358 if (len < 0) { 2359 /* IO error return the error */ 2360 err = len; 2361 goto out_free_iovec; 2362 } 2363 } while (count < max_count && len > 0); 2364 err = pdu_marshal(pdu, offset, "d", count); 2365 if (err < 0) { 2366 goto out_free_iovec; 2367 } 2368 err += offset + count; 2369 out_free_iovec: 2370 qemu_iovec_destroy(&qiov); 2371 qemu_iovec_destroy(&qiov_full); 2372 } else if (fidp->fid_type == P9_FID_XATTR) { 2373 err = v9fs_xattr_read(s, pdu, fidp, off, max_count); 2374 } else { 2375 err = -EINVAL; 2376 } 2377 trace_v9fs_read_return(pdu->tag, pdu->id, count, err); 2378 out: 2379 put_fid(pdu, fidp); 2380 out_nofid: 2381 pdu_complete(pdu, err); 2382 } 2383 2384 /** 2385 * Returns size required in Rreaddir response for the passed dirent @p name. 2386 * 2387 * @param name - directory entry's name (i.e. file name, directory name) 2388 * @returns required size in bytes 2389 */ 2390 size_t v9fs_readdir_response_size(V9fsString *name) 2391 { 2392 /* 2393 * Size of each dirent on the wire: size of qid (13) + size of offset (8) 2394 * size of type (1) + size of name.size (2) + strlen(name.data) 2395 */ 2396 return 24 + v9fs_string_size(name); 2397 } 2398 2399 static void v9fs_free_dirents(struct V9fsDirEnt *e) 2400 { 2401 struct V9fsDirEnt *next = NULL; 2402 2403 for (; e; e = next) { 2404 next = e->next; 2405 g_free(e->dent); 2406 g_free(e->st); 2407 g_free(e); 2408 } 2409 } 2410 2411 static int coroutine_fn v9fs_do_readdir(V9fsPDU *pdu, V9fsFidState *fidp, 2412 off_t offset, int32_t max_count) 2413 { 2414 size_t size; 2415 V9fsQID qid; 2416 V9fsString name; 2417 int len, err = 0; 2418 int32_t count = 0; 2419 struct dirent *dent; 2420 struct stat *st; 2421 struct V9fsDirEnt *entries = NULL; 2422 2423 /* 2424 * inode remapping requires the device id, which in turn might be 2425 * different for different directory entries, so if inode remapping is 2426 * enabled we have to make a full stat for each directory entry 2427 */ 2428 const bool dostat = pdu->s->ctx.export_flags & V9FS_REMAP_INODES; 2429 2430 /* 2431 * Fetch all required directory entries altogether on a background IO 2432 * thread from fs driver. We don't want to do that for each entry 2433 * individually, because hopping between threads (this main IO thread 2434 * and background IO driver thread) would sum up to huge latencies. 2435 */ 2436 count = v9fs_co_readdir_many(pdu, fidp, &entries, offset, max_count, 2437 dostat); 2438 if (count < 0) { 2439 err = count; 2440 count = 0; 2441 goto out; 2442 } 2443 count = 0; 2444 2445 for (struct V9fsDirEnt *e = entries; e; e = e->next) { 2446 dent = e->dent; 2447 2448 if (pdu->s->ctx.export_flags & V9FS_REMAP_INODES) { 2449 st = e->st; 2450 /* e->st should never be NULL, but just to be sure */ 2451 if (!st) { 2452 err = -1; 2453 break; 2454 } 2455 2456 /* remap inode */ 2457 err = stat_to_qid(pdu, st, &qid); 2458 if (err < 0) { 2459 break; 2460 } 2461 } else { 2462 /* 2463 * Fill up just the path field of qid because the client uses 2464 * only that. To fill the entire qid structure we will have 2465 * to stat each dirent found, which is expensive. For the 2466 * latter reason we don't call stat_to_qid() here. Only drawback 2467 * is that no multi-device export detection of stat_to_qid() 2468 * would be done and provided as error to the user here. But 2469 * user would get that error anyway when accessing those 2470 * files/dirs through other ways. 2471 */ 2472 size = MIN(sizeof(dent->d_ino), sizeof(qid.path)); 2473 memcpy(&qid.path, &dent->d_ino, size); 2474 /* Fill the other fields with dummy values */ 2475 qid.type = 0; 2476 qid.version = 0; 2477 } 2478 2479 v9fs_string_init(&name); 2480 v9fs_string_sprintf(&name, "%s", dent->d_name); 2481 2482 /* 11 = 7 + 4 (7 = start offset, 4 = space for storing count) */ 2483 len = pdu_marshal(pdu, 11 + count, "Qqbs", 2484 &qid, dent->d_off, 2485 dent->d_type, &name); 2486 2487 v9fs_string_free(&name); 2488 2489 if (len < 0) { 2490 err = len; 2491 break; 2492 } 2493 2494 count += len; 2495 } 2496 2497 out: 2498 v9fs_free_dirents(entries); 2499 if (err < 0) { 2500 return err; 2501 } 2502 return count; 2503 } 2504 2505 static void coroutine_fn v9fs_readdir(void *opaque) 2506 { 2507 int32_t fid; 2508 V9fsFidState *fidp; 2509 ssize_t retval = 0; 2510 size_t offset = 7; 2511 uint64_t initial_offset; 2512 int32_t count; 2513 uint32_t max_count; 2514 V9fsPDU *pdu = opaque; 2515 V9fsState *s = pdu->s; 2516 2517 retval = pdu_unmarshal(pdu, offset, "dqd", &fid, 2518 &initial_offset, &max_count); 2519 if (retval < 0) { 2520 goto out_nofid; 2521 } 2522 trace_v9fs_readdir(pdu->tag, pdu->id, fid, initial_offset, max_count); 2523 2524 /* Enough space for a R_readdir header: size[4] Rreaddir tag[2] count[4] */ 2525 if (max_count > s->msize - 11) { 2526 max_count = s->msize - 11; 2527 warn_report_once( 2528 "9p: bad client: T_readdir with count > msize - 11" 2529 ); 2530 } 2531 2532 fidp = get_fid(pdu, fid); 2533 if (fidp == NULL) { 2534 retval = -EINVAL; 2535 goto out_nofid; 2536 } 2537 if (!fidp->fs.dir.stream) { 2538 retval = -EINVAL; 2539 goto out; 2540 } 2541 if (s->proto_version != V9FS_PROTO_2000L) { 2542 warn_report_once( 2543 "9p: bad client: T_readdir request only expected with 9P2000.L " 2544 "protocol version" 2545 ); 2546 retval = -EOPNOTSUPP; 2547 goto out; 2548 } 2549 count = v9fs_do_readdir(pdu, fidp, (off_t) initial_offset, max_count); 2550 if (count < 0) { 2551 retval = count; 2552 goto out; 2553 } 2554 retval = pdu_marshal(pdu, offset, "d", count); 2555 if (retval < 0) { 2556 goto out; 2557 } 2558 retval += count + offset; 2559 trace_v9fs_readdir_return(pdu->tag, pdu->id, count, retval); 2560 out: 2561 put_fid(pdu, fidp); 2562 out_nofid: 2563 pdu_complete(pdu, retval); 2564 } 2565 2566 static int v9fs_xattr_write(V9fsState *s, V9fsPDU *pdu, V9fsFidState *fidp, 2567 uint64_t off, uint32_t count, 2568 struct iovec *sg, int cnt) 2569 { 2570 int i, to_copy; 2571 ssize_t err = 0; 2572 uint64_t write_count; 2573 size_t offset = 7; 2574 2575 2576 if (fidp->fs.xattr.len < off) { 2577 return -ENOSPC; 2578 } 2579 write_count = fidp->fs.xattr.len - off; 2580 if (write_count > count) { 2581 write_count = count; 2582 } 2583 err = pdu_marshal(pdu, offset, "d", write_count); 2584 if (err < 0) { 2585 return err; 2586 } 2587 err += offset; 2588 fidp->fs.xattr.copied_len += write_count; 2589 /* 2590 * Now copy the content from sg list 2591 */ 2592 for (i = 0; i < cnt; i++) { 2593 if (write_count > sg[i].iov_len) { 2594 to_copy = sg[i].iov_len; 2595 } else { 2596 to_copy = write_count; 2597 } 2598 memcpy((char *)fidp->fs.xattr.value + off, sg[i].iov_base, to_copy); 2599 /* updating vs->off since we are not using below */ 2600 off += to_copy; 2601 write_count -= to_copy; 2602 } 2603 2604 return err; 2605 } 2606 2607 static void coroutine_fn v9fs_write(void *opaque) 2608 { 2609 ssize_t err; 2610 int32_t fid; 2611 uint64_t off; 2612 uint32_t count; 2613 int32_t len = 0; 2614 int32_t total = 0; 2615 size_t offset = 7; 2616 V9fsFidState *fidp; 2617 V9fsPDU *pdu = opaque; 2618 V9fsState *s = pdu->s; 2619 QEMUIOVector qiov_full; 2620 QEMUIOVector qiov; 2621 2622 err = pdu_unmarshal(pdu, offset, "dqd", &fid, &off, &count); 2623 if (err < 0) { 2624 pdu_complete(pdu, err); 2625 return; 2626 } 2627 offset += err; 2628 v9fs_init_qiov_from_pdu(&qiov_full, pdu, offset, count, true); 2629 trace_v9fs_write(pdu->tag, pdu->id, fid, off, count, qiov_full.niov); 2630 2631 fidp = get_fid(pdu, fid); 2632 if (fidp == NULL) { 2633 err = -EINVAL; 2634 goto out_nofid; 2635 } 2636 if (fidp->fid_type == P9_FID_FILE) { 2637 if (fidp->fs.fd == -1) { 2638 err = -EINVAL; 2639 goto out; 2640 } 2641 } else if (fidp->fid_type == P9_FID_XATTR) { 2642 /* 2643 * setxattr operation 2644 */ 2645 err = v9fs_xattr_write(s, pdu, fidp, off, count, 2646 qiov_full.iov, qiov_full.niov); 2647 goto out; 2648 } else { 2649 err = -EINVAL; 2650 goto out; 2651 } 2652 qemu_iovec_init(&qiov, qiov_full.niov); 2653 do { 2654 qemu_iovec_reset(&qiov); 2655 qemu_iovec_concat(&qiov, &qiov_full, total, qiov_full.size - total); 2656 if (0) { 2657 print_sg(qiov.iov, qiov.niov); 2658 } 2659 /* Loop in case of EINTR */ 2660 do { 2661 len = v9fs_co_pwritev(pdu, fidp, qiov.iov, qiov.niov, off); 2662 if (len >= 0) { 2663 off += len; 2664 total += len; 2665 } 2666 } while (len == -EINTR && !pdu->cancelled); 2667 if (len < 0) { 2668 /* IO error return the error */ 2669 err = len; 2670 goto out_qiov; 2671 } 2672 } while (total < count && len > 0); 2673 2674 offset = 7; 2675 err = pdu_marshal(pdu, offset, "d", total); 2676 if (err < 0) { 2677 goto out_qiov; 2678 } 2679 err += offset; 2680 trace_v9fs_write_return(pdu->tag, pdu->id, total, err); 2681 out_qiov: 2682 qemu_iovec_destroy(&qiov); 2683 out: 2684 put_fid(pdu, fidp); 2685 out_nofid: 2686 qemu_iovec_destroy(&qiov_full); 2687 pdu_complete(pdu, err); 2688 } 2689 2690 static void coroutine_fn v9fs_create(void *opaque) 2691 { 2692 int32_t fid; 2693 int err = 0; 2694 size_t offset = 7; 2695 V9fsFidState *fidp; 2696 V9fsQID qid; 2697 int32_t perm; 2698 int8_t mode; 2699 V9fsPath path; 2700 struct stat stbuf; 2701 V9fsString name; 2702 V9fsString extension; 2703 int iounit; 2704 V9fsPDU *pdu = opaque; 2705 V9fsState *s = pdu->s; 2706 2707 v9fs_path_init(&path); 2708 v9fs_string_init(&name); 2709 v9fs_string_init(&extension); 2710 err = pdu_unmarshal(pdu, offset, "dsdbs", &fid, &name, 2711 &perm, &mode, &extension); 2712 if (err < 0) { 2713 goto out_nofid; 2714 } 2715 trace_v9fs_create(pdu->tag, pdu->id, fid, name.data, perm, mode); 2716 2717 if (name_is_illegal(name.data)) { 2718 err = -ENOENT; 2719 goto out_nofid; 2720 } 2721 2722 if (!strcmp(".", name.data) || !strcmp("..", name.data)) { 2723 err = -EEXIST; 2724 goto out_nofid; 2725 } 2726 2727 fidp = get_fid(pdu, fid); 2728 if (fidp == NULL) { 2729 err = -EINVAL; 2730 goto out_nofid; 2731 } 2732 if (fidp->fid_type != P9_FID_NONE) { 2733 err = -EINVAL; 2734 goto out; 2735 } 2736 if (perm & P9_STAT_MODE_DIR) { 2737 err = v9fs_co_mkdir(pdu, fidp, &name, perm & 0777, 2738 fidp->uid, -1, &stbuf); 2739 if (err < 0) { 2740 goto out; 2741 } 2742 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path); 2743 if (err < 0) { 2744 goto out; 2745 } 2746 v9fs_path_write_lock(s); 2747 v9fs_path_copy(&fidp->path, &path); 2748 v9fs_path_unlock(s); 2749 err = v9fs_co_opendir(pdu, fidp); 2750 if (err < 0) { 2751 goto out; 2752 } 2753 fidp->fid_type = P9_FID_DIR; 2754 } else if (perm & P9_STAT_MODE_SYMLINK) { 2755 err = v9fs_co_symlink(pdu, fidp, &name, 2756 extension.data, -1 , &stbuf); 2757 if (err < 0) { 2758 goto out; 2759 } 2760 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path); 2761 if (err < 0) { 2762 goto out; 2763 } 2764 v9fs_path_write_lock(s); 2765 v9fs_path_copy(&fidp->path, &path); 2766 v9fs_path_unlock(s); 2767 } else if (perm & P9_STAT_MODE_LINK) { 2768 int32_t ofid = atoi(extension.data); 2769 V9fsFidState *ofidp = get_fid(pdu, ofid); 2770 if (ofidp == NULL) { 2771 err = -EINVAL; 2772 goto out; 2773 } 2774 err = v9fs_co_link(pdu, ofidp, fidp, &name); 2775 put_fid(pdu, ofidp); 2776 if (err < 0) { 2777 goto out; 2778 } 2779 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path); 2780 if (err < 0) { 2781 fidp->fid_type = P9_FID_NONE; 2782 goto out; 2783 } 2784 v9fs_path_write_lock(s); 2785 v9fs_path_copy(&fidp->path, &path); 2786 v9fs_path_unlock(s); 2787 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf); 2788 if (err < 0) { 2789 fidp->fid_type = P9_FID_NONE; 2790 goto out; 2791 } 2792 } else if (perm & P9_STAT_MODE_DEVICE) { 2793 char ctype; 2794 uint32_t major, minor; 2795 mode_t nmode = 0; 2796 2797 if (sscanf(extension.data, "%c %u %u", &ctype, &major, &minor) != 3) { 2798 err = -errno; 2799 goto out; 2800 } 2801 2802 switch (ctype) { 2803 case 'c': 2804 nmode = S_IFCHR; 2805 break; 2806 case 'b': 2807 nmode = S_IFBLK; 2808 break; 2809 default: 2810 err = -EIO; 2811 goto out; 2812 } 2813 2814 nmode |= perm & 0777; 2815 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1, 2816 makedev(major, minor), nmode, &stbuf); 2817 if (err < 0) { 2818 goto out; 2819 } 2820 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path); 2821 if (err < 0) { 2822 goto out; 2823 } 2824 v9fs_path_write_lock(s); 2825 v9fs_path_copy(&fidp->path, &path); 2826 v9fs_path_unlock(s); 2827 } else if (perm & P9_STAT_MODE_NAMED_PIPE) { 2828 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1, 2829 0, S_IFIFO | (perm & 0777), &stbuf); 2830 if (err < 0) { 2831 goto out; 2832 } 2833 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path); 2834 if (err < 0) { 2835 goto out; 2836 } 2837 v9fs_path_write_lock(s); 2838 v9fs_path_copy(&fidp->path, &path); 2839 v9fs_path_unlock(s); 2840 } else if (perm & P9_STAT_MODE_SOCKET) { 2841 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, -1, 2842 0, S_IFSOCK | (perm & 0777), &stbuf); 2843 if (err < 0) { 2844 goto out; 2845 } 2846 err = v9fs_co_name_to_path(pdu, &fidp->path, name.data, &path); 2847 if (err < 0) { 2848 goto out; 2849 } 2850 v9fs_path_write_lock(s); 2851 v9fs_path_copy(&fidp->path, &path); 2852 v9fs_path_unlock(s); 2853 } else { 2854 err = v9fs_co_open2(pdu, fidp, &name, -1, 2855 omode_to_uflags(mode) | O_CREAT, perm, &stbuf); 2856 if (err < 0) { 2857 goto out; 2858 } 2859 fidp->fid_type = P9_FID_FILE; 2860 fidp->open_flags = omode_to_uflags(mode); 2861 if (fidp->open_flags & O_EXCL) { 2862 /* 2863 * We let the host file system do O_EXCL check 2864 * We should not reclaim such fd 2865 */ 2866 fidp->flags |= FID_NON_RECLAIMABLE; 2867 } 2868 } 2869 iounit = get_iounit(pdu, &fidp->path); 2870 err = stat_to_qid(pdu, &stbuf, &qid); 2871 if (err < 0) { 2872 goto out; 2873 } 2874 err = pdu_marshal(pdu, offset, "Qd", &qid, iounit); 2875 if (err < 0) { 2876 goto out; 2877 } 2878 err += offset; 2879 trace_v9fs_create_return(pdu->tag, pdu->id, 2880 qid.type, qid.version, qid.path, iounit); 2881 out: 2882 put_fid(pdu, fidp); 2883 out_nofid: 2884 pdu_complete(pdu, err); 2885 v9fs_string_free(&name); 2886 v9fs_string_free(&extension); 2887 v9fs_path_free(&path); 2888 } 2889 2890 static void coroutine_fn v9fs_symlink(void *opaque) 2891 { 2892 V9fsPDU *pdu = opaque; 2893 V9fsString name; 2894 V9fsString symname; 2895 V9fsFidState *dfidp; 2896 V9fsQID qid; 2897 struct stat stbuf; 2898 int32_t dfid; 2899 int err = 0; 2900 gid_t gid; 2901 size_t offset = 7; 2902 2903 v9fs_string_init(&name); 2904 v9fs_string_init(&symname); 2905 err = pdu_unmarshal(pdu, offset, "dssd", &dfid, &name, &symname, &gid); 2906 if (err < 0) { 2907 goto out_nofid; 2908 } 2909 trace_v9fs_symlink(pdu->tag, pdu->id, dfid, name.data, symname.data, gid); 2910 2911 if (name_is_illegal(name.data)) { 2912 err = -ENOENT; 2913 goto out_nofid; 2914 } 2915 2916 if (!strcmp(".", name.data) || !strcmp("..", name.data)) { 2917 err = -EEXIST; 2918 goto out_nofid; 2919 } 2920 2921 dfidp = get_fid(pdu, dfid); 2922 if (dfidp == NULL) { 2923 err = -EINVAL; 2924 goto out_nofid; 2925 } 2926 err = v9fs_co_symlink(pdu, dfidp, &name, symname.data, gid, &stbuf); 2927 if (err < 0) { 2928 goto out; 2929 } 2930 err = stat_to_qid(pdu, &stbuf, &qid); 2931 if (err < 0) { 2932 goto out; 2933 } 2934 err = pdu_marshal(pdu, offset, "Q", &qid); 2935 if (err < 0) { 2936 goto out; 2937 } 2938 err += offset; 2939 trace_v9fs_symlink_return(pdu->tag, pdu->id, 2940 qid.type, qid.version, qid.path); 2941 out: 2942 put_fid(pdu, dfidp); 2943 out_nofid: 2944 pdu_complete(pdu, err); 2945 v9fs_string_free(&name); 2946 v9fs_string_free(&symname); 2947 } 2948 2949 static void coroutine_fn v9fs_flush(void *opaque) 2950 { 2951 ssize_t err; 2952 int16_t tag; 2953 size_t offset = 7; 2954 V9fsPDU *cancel_pdu = NULL; 2955 V9fsPDU *pdu = opaque; 2956 V9fsState *s = pdu->s; 2957 2958 err = pdu_unmarshal(pdu, offset, "w", &tag); 2959 if (err < 0) { 2960 pdu_complete(pdu, err); 2961 return; 2962 } 2963 trace_v9fs_flush(pdu->tag, pdu->id, tag); 2964 2965 if (pdu->tag == tag) { 2966 warn_report("the guest sent a self-referencing 9P flush request"); 2967 } else { 2968 QLIST_FOREACH(cancel_pdu, &s->active_list, next) { 2969 if (cancel_pdu->tag == tag) { 2970 break; 2971 } 2972 } 2973 } 2974 if (cancel_pdu) { 2975 cancel_pdu->cancelled = 1; 2976 /* 2977 * Wait for pdu to complete. 2978 */ 2979 qemu_co_queue_wait(&cancel_pdu->complete, NULL); 2980 if (!qemu_co_queue_next(&cancel_pdu->complete)) { 2981 cancel_pdu->cancelled = 0; 2982 pdu_free(cancel_pdu); 2983 } 2984 } 2985 pdu_complete(pdu, 7); 2986 } 2987 2988 static void coroutine_fn v9fs_link(void *opaque) 2989 { 2990 V9fsPDU *pdu = opaque; 2991 int32_t dfid, oldfid; 2992 V9fsFidState *dfidp, *oldfidp; 2993 V9fsString name; 2994 size_t offset = 7; 2995 int err = 0; 2996 2997 v9fs_string_init(&name); 2998 err = pdu_unmarshal(pdu, offset, "dds", &dfid, &oldfid, &name); 2999 if (err < 0) { 3000 goto out_nofid; 3001 } 3002 trace_v9fs_link(pdu->tag, pdu->id, dfid, oldfid, name.data); 3003 3004 if (name_is_illegal(name.data)) { 3005 err = -ENOENT; 3006 goto out_nofid; 3007 } 3008 3009 if (!strcmp(".", name.data) || !strcmp("..", name.data)) { 3010 err = -EEXIST; 3011 goto out_nofid; 3012 } 3013 3014 dfidp = get_fid(pdu, dfid); 3015 if (dfidp == NULL) { 3016 err = -ENOENT; 3017 goto out_nofid; 3018 } 3019 3020 oldfidp = get_fid(pdu, oldfid); 3021 if (oldfidp == NULL) { 3022 err = -ENOENT; 3023 goto out; 3024 } 3025 err = v9fs_co_link(pdu, oldfidp, dfidp, &name); 3026 if (!err) { 3027 err = offset; 3028 } 3029 put_fid(pdu, oldfidp); 3030 out: 3031 put_fid(pdu, dfidp); 3032 out_nofid: 3033 v9fs_string_free(&name); 3034 pdu_complete(pdu, err); 3035 } 3036 3037 /* Only works with path name based fid */ 3038 static void coroutine_fn v9fs_remove(void *opaque) 3039 { 3040 int32_t fid; 3041 int err = 0; 3042 size_t offset = 7; 3043 V9fsFidState *fidp; 3044 V9fsPDU *pdu = opaque; 3045 3046 err = pdu_unmarshal(pdu, offset, "d", &fid); 3047 if (err < 0) { 3048 goto out_nofid; 3049 } 3050 trace_v9fs_remove(pdu->tag, pdu->id, fid); 3051 3052 fidp = get_fid(pdu, fid); 3053 if (fidp == NULL) { 3054 err = -EINVAL; 3055 goto out_nofid; 3056 } 3057 /* if fs driver is not path based, return EOPNOTSUPP */ 3058 if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) { 3059 err = -EOPNOTSUPP; 3060 goto out_err; 3061 } 3062 /* 3063 * IF the file is unlinked, we cannot reopen 3064 * the file later. So don't reclaim fd 3065 */ 3066 err = v9fs_mark_fids_unreclaim(pdu, &fidp->path); 3067 if (err < 0) { 3068 goto out_err; 3069 } 3070 err = v9fs_co_remove(pdu, &fidp->path); 3071 if (!err) { 3072 err = offset; 3073 } 3074 out_err: 3075 /* For TREMOVE we need to clunk the fid even on failed remove */ 3076 clunk_fid(pdu->s, fidp->fid); 3077 put_fid(pdu, fidp); 3078 out_nofid: 3079 pdu_complete(pdu, err); 3080 } 3081 3082 static void coroutine_fn v9fs_unlinkat(void *opaque) 3083 { 3084 int err = 0; 3085 V9fsString name; 3086 int32_t dfid, flags, rflags = 0; 3087 size_t offset = 7; 3088 V9fsPath path; 3089 V9fsFidState *dfidp; 3090 V9fsPDU *pdu = opaque; 3091 3092 v9fs_string_init(&name); 3093 err = pdu_unmarshal(pdu, offset, "dsd", &dfid, &name, &flags); 3094 if (err < 0) { 3095 goto out_nofid; 3096 } 3097 3098 if (name_is_illegal(name.data)) { 3099 err = -ENOENT; 3100 goto out_nofid; 3101 } 3102 3103 if (!strcmp(".", name.data)) { 3104 err = -EINVAL; 3105 goto out_nofid; 3106 } 3107 3108 if (!strcmp("..", name.data)) { 3109 err = -ENOTEMPTY; 3110 goto out_nofid; 3111 } 3112 3113 if (flags & ~P9_DOTL_AT_REMOVEDIR) { 3114 err = -EINVAL; 3115 goto out_nofid; 3116 } 3117 3118 if (flags & P9_DOTL_AT_REMOVEDIR) { 3119 rflags |= AT_REMOVEDIR; 3120 } 3121 3122 dfidp = get_fid(pdu, dfid); 3123 if (dfidp == NULL) { 3124 err = -EINVAL; 3125 goto out_nofid; 3126 } 3127 /* 3128 * IF the file is unlinked, we cannot reopen 3129 * the file later. So don't reclaim fd 3130 */ 3131 v9fs_path_init(&path); 3132 err = v9fs_co_name_to_path(pdu, &dfidp->path, name.data, &path); 3133 if (err < 0) { 3134 goto out_err; 3135 } 3136 err = v9fs_mark_fids_unreclaim(pdu, &path); 3137 if (err < 0) { 3138 goto out_err; 3139 } 3140 err = v9fs_co_unlinkat(pdu, &dfidp->path, &name, rflags); 3141 if (!err) { 3142 err = offset; 3143 } 3144 out_err: 3145 put_fid(pdu, dfidp); 3146 v9fs_path_free(&path); 3147 out_nofid: 3148 pdu_complete(pdu, err); 3149 v9fs_string_free(&name); 3150 } 3151 3152 3153 /* Only works with path name based fid */ 3154 static int coroutine_fn v9fs_complete_rename(V9fsPDU *pdu, V9fsFidState *fidp, 3155 int32_t newdirfid, 3156 V9fsString *name) 3157 { 3158 int err = 0; 3159 V9fsPath new_path; 3160 V9fsFidState *tfidp; 3161 V9fsState *s = pdu->s; 3162 V9fsFidState *dirfidp = NULL; 3163 3164 v9fs_path_init(&new_path); 3165 if (newdirfid != -1) { 3166 dirfidp = get_fid(pdu, newdirfid); 3167 if (dirfidp == NULL) { 3168 return -ENOENT; 3169 } 3170 if (fidp->fid_type != P9_FID_NONE) { 3171 err = -EINVAL; 3172 goto out; 3173 } 3174 err = v9fs_co_name_to_path(pdu, &dirfidp->path, name->data, &new_path); 3175 if (err < 0) { 3176 goto out; 3177 } 3178 } else { 3179 char *dir_name = g_path_get_dirname(fidp->path.data); 3180 V9fsPath dir_path; 3181 3182 v9fs_path_init(&dir_path); 3183 v9fs_path_sprintf(&dir_path, "%s", dir_name); 3184 g_free(dir_name); 3185 3186 err = v9fs_co_name_to_path(pdu, &dir_path, name->data, &new_path); 3187 v9fs_path_free(&dir_path); 3188 if (err < 0) { 3189 goto out; 3190 } 3191 } 3192 err = v9fs_co_rename(pdu, &fidp->path, &new_path); 3193 if (err < 0) { 3194 goto out; 3195 } 3196 /* 3197 * Fixup fid's pointing to the old name to 3198 * start pointing to the new name 3199 */ 3200 QSIMPLEQ_FOREACH(tfidp, &s->fid_list, next) { 3201 if (v9fs_path_is_ancestor(&fidp->path, &tfidp->path)) { 3202 /* replace the name */ 3203 v9fs_fix_path(&tfidp->path, &new_path, strlen(fidp->path.data)); 3204 } 3205 } 3206 out: 3207 if (dirfidp) { 3208 put_fid(pdu, dirfidp); 3209 } 3210 v9fs_path_free(&new_path); 3211 return err; 3212 } 3213 3214 /* Only works with path name based fid */ 3215 static void coroutine_fn v9fs_rename(void *opaque) 3216 { 3217 int32_t fid; 3218 ssize_t err = 0; 3219 size_t offset = 7; 3220 V9fsString name; 3221 int32_t newdirfid; 3222 V9fsFidState *fidp; 3223 V9fsPDU *pdu = opaque; 3224 V9fsState *s = pdu->s; 3225 3226 v9fs_string_init(&name); 3227 err = pdu_unmarshal(pdu, offset, "dds", &fid, &newdirfid, &name); 3228 if (err < 0) { 3229 goto out_nofid; 3230 } 3231 3232 if (name_is_illegal(name.data)) { 3233 err = -ENOENT; 3234 goto out_nofid; 3235 } 3236 3237 if (!strcmp(".", name.data) || !strcmp("..", name.data)) { 3238 err = -EISDIR; 3239 goto out_nofid; 3240 } 3241 3242 fidp = get_fid(pdu, fid); 3243 if (fidp == NULL) { 3244 err = -ENOENT; 3245 goto out_nofid; 3246 } 3247 if (fidp->fid_type != P9_FID_NONE) { 3248 err = -EINVAL; 3249 goto out; 3250 } 3251 /* if fs driver is not path based, return EOPNOTSUPP */ 3252 if (!(pdu->s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT)) { 3253 err = -EOPNOTSUPP; 3254 goto out; 3255 } 3256 v9fs_path_write_lock(s); 3257 err = v9fs_complete_rename(pdu, fidp, newdirfid, &name); 3258 v9fs_path_unlock(s); 3259 if (!err) { 3260 err = offset; 3261 } 3262 out: 3263 put_fid(pdu, fidp); 3264 out_nofid: 3265 pdu_complete(pdu, err); 3266 v9fs_string_free(&name); 3267 } 3268 3269 static int coroutine_fn v9fs_fix_fid_paths(V9fsPDU *pdu, V9fsPath *olddir, 3270 V9fsString *old_name, 3271 V9fsPath *newdir, 3272 V9fsString *new_name) 3273 { 3274 V9fsFidState *tfidp; 3275 V9fsPath oldpath, newpath; 3276 V9fsState *s = pdu->s; 3277 int err; 3278 3279 v9fs_path_init(&oldpath); 3280 v9fs_path_init(&newpath); 3281 err = v9fs_co_name_to_path(pdu, olddir, old_name->data, &oldpath); 3282 if (err < 0) { 3283 goto out; 3284 } 3285 err = v9fs_co_name_to_path(pdu, newdir, new_name->data, &newpath); 3286 if (err < 0) { 3287 goto out; 3288 } 3289 3290 /* 3291 * Fixup fid's pointing to the old name to 3292 * start pointing to the new name 3293 */ 3294 QSIMPLEQ_FOREACH(tfidp, &s->fid_list, next) { 3295 if (v9fs_path_is_ancestor(&oldpath, &tfidp->path)) { 3296 /* replace the name */ 3297 v9fs_fix_path(&tfidp->path, &newpath, strlen(oldpath.data)); 3298 } 3299 } 3300 out: 3301 v9fs_path_free(&oldpath); 3302 v9fs_path_free(&newpath); 3303 return err; 3304 } 3305 3306 static int coroutine_fn v9fs_complete_renameat(V9fsPDU *pdu, int32_t olddirfid, 3307 V9fsString *old_name, 3308 int32_t newdirfid, 3309 V9fsString *new_name) 3310 { 3311 int err = 0; 3312 V9fsState *s = pdu->s; 3313 V9fsFidState *newdirfidp = NULL, *olddirfidp = NULL; 3314 3315 olddirfidp = get_fid(pdu, olddirfid); 3316 if (olddirfidp == NULL) { 3317 err = -ENOENT; 3318 goto out; 3319 } 3320 if (newdirfid != -1) { 3321 newdirfidp = get_fid(pdu, newdirfid); 3322 if (newdirfidp == NULL) { 3323 err = -ENOENT; 3324 goto out; 3325 } 3326 } else { 3327 newdirfidp = get_fid(pdu, olddirfid); 3328 } 3329 3330 err = v9fs_co_renameat(pdu, &olddirfidp->path, old_name, 3331 &newdirfidp->path, new_name); 3332 if (err < 0) { 3333 goto out; 3334 } 3335 if (s->ctx.export_flags & V9FS_PATHNAME_FSCONTEXT) { 3336 /* Only for path based fid we need to do the below fixup */ 3337 err = v9fs_fix_fid_paths(pdu, &olddirfidp->path, old_name, 3338 &newdirfidp->path, new_name); 3339 } 3340 out: 3341 if (olddirfidp) { 3342 put_fid(pdu, olddirfidp); 3343 } 3344 if (newdirfidp) { 3345 put_fid(pdu, newdirfidp); 3346 } 3347 return err; 3348 } 3349 3350 static void coroutine_fn v9fs_renameat(void *opaque) 3351 { 3352 ssize_t err = 0; 3353 size_t offset = 7; 3354 V9fsPDU *pdu = opaque; 3355 V9fsState *s = pdu->s; 3356 int32_t olddirfid, newdirfid; 3357 V9fsString old_name, new_name; 3358 3359 v9fs_string_init(&old_name); 3360 v9fs_string_init(&new_name); 3361 err = pdu_unmarshal(pdu, offset, "dsds", &olddirfid, 3362 &old_name, &newdirfid, &new_name); 3363 if (err < 0) { 3364 goto out_err; 3365 } 3366 3367 if (name_is_illegal(old_name.data) || name_is_illegal(new_name.data)) { 3368 err = -ENOENT; 3369 goto out_err; 3370 } 3371 3372 if (!strcmp(".", old_name.data) || !strcmp("..", old_name.data) || 3373 !strcmp(".", new_name.data) || !strcmp("..", new_name.data)) { 3374 err = -EISDIR; 3375 goto out_err; 3376 } 3377 3378 v9fs_path_write_lock(s); 3379 err = v9fs_complete_renameat(pdu, olddirfid, 3380 &old_name, newdirfid, &new_name); 3381 v9fs_path_unlock(s); 3382 if (!err) { 3383 err = offset; 3384 } 3385 3386 out_err: 3387 pdu_complete(pdu, err); 3388 v9fs_string_free(&old_name); 3389 v9fs_string_free(&new_name); 3390 } 3391 3392 static void coroutine_fn v9fs_wstat(void *opaque) 3393 { 3394 int32_t fid; 3395 int err = 0; 3396 int16_t unused; 3397 V9fsStat v9stat; 3398 size_t offset = 7; 3399 struct stat stbuf; 3400 V9fsFidState *fidp; 3401 V9fsPDU *pdu = opaque; 3402 V9fsState *s = pdu->s; 3403 3404 v9fs_stat_init(&v9stat); 3405 err = pdu_unmarshal(pdu, offset, "dwS", &fid, &unused, &v9stat); 3406 if (err < 0) { 3407 goto out_nofid; 3408 } 3409 trace_v9fs_wstat(pdu->tag, pdu->id, fid, 3410 v9stat.mode, v9stat.atime, v9stat.mtime); 3411 3412 fidp = get_fid(pdu, fid); 3413 if (fidp == NULL) { 3414 err = -EINVAL; 3415 goto out_nofid; 3416 } 3417 /* do we need to sync the file? */ 3418 if (donttouch_stat(&v9stat)) { 3419 err = v9fs_co_fsync(pdu, fidp, 0); 3420 goto out; 3421 } 3422 if (v9stat.mode != -1) { 3423 uint32_t v9_mode; 3424 err = v9fs_co_lstat(pdu, &fidp->path, &stbuf); 3425 if (err < 0) { 3426 goto out; 3427 } 3428 v9_mode = stat_to_v9mode(&stbuf); 3429 if ((v9stat.mode & P9_STAT_MODE_TYPE_BITS) != 3430 (v9_mode & P9_STAT_MODE_TYPE_BITS)) { 3431 /* Attempting to change the type */ 3432 err = -EIO; 3433 goto out; 3434 } 3435 err = v9fs_co_chmod(pdu, &fidp->path, 3436 v9mode_to_mode(v9stat.mode, 3437 &v9stat.extension)); 3438 if (err < 0) { 3439 goto out; 3440 } 3441 } 3442 if (v9stat.mtime != -1 || v9stat.atime != -1) { 3443 struct timespec times[2]; 3444 if (v9stat.atime != -1) { 3445 times[0].tv_sec = v9stat.atime; 3446 times[0].tv_nsec = 0; 3447 } else { 3448 times[0].tv_nsec = UTIME_OMIT; 3449 } 3450 if (v9stat.mtime != -1) { 3451 times[1].tv_sec = v9stat.mtime; 3452 times[1].tv_nsec = 0; 3453 } else { 3454 times[1].tv_nsec = UTIME_OMIT; 3455 } 3456 err = v9fs_co_utimensat(pdu, &fidp->path, times); 3457 if (err < 0) { 3458 goto out; 3459 } 3460 } 3461 if (v9stat.n_gid != -1 || v9stat.n_uid != -1) { 3462 err = v9fs_co_chown(pdu, &fidp->path, v9stat.n_uid, v9stat.n_gid); 3463 if (err < 0) { 3464 goto out; 3465 } 3466 } 3467 if (v9stat.name.size != 0) { 3468 v9fs_path_write_lock(s); 3469 err = v9fs_complete_rename(pdu, fidp, -1, &v9stat.name); 3470 v9fs_path_unlock(s); 3471 if (err < 0) { 3472 goto out; 3473 } 3474 } 3475 if (v9stat.length != -1) { 3476 err = v9fs_co_truncate(pdu, &fidp->path, v9stat.length); 3477 if (err < 0) { 3478 goto out; 3479 } 3480 } 3481 err = offset; 3482 out: 3483 put_fid(pdu, fidp); 3484 out_nofid: 3485 v9fs_stat_free(&v9stat); 3486 pdu_complete(pdu, err); 3487 } 3488 3489 static int v9fs_fill_statfs(V9fsState *s, V9fsPDU *pdu, struct statfs *stbuf) 3490 { 3491 uint32_t f_type; 3492 uint32_t f_bsize; 3493 uint64_t f_blocks; 3494 uint64_t f_bfree; 3495 uint64_t f_bavail; 3496 uint64_t f_files; 3497 uint64_t f_ffree; 3498 uint64_t fsid_val; 3499 uint32_t f_namelen; 3500 size_t offset = 7; 3501 int32_t bsize_factor; 3502 3503 /* 3504 * compute bsize factor based on host file system block size 3505 * and client msize 3506 */ 3507 bsize_factor = (s->msize - P9_IOHDRSZ) / stbuf->f_bsize; 3508 if (!bsize_factor) { 3509 bsize_factor = 1; 3510 } 3511 f_type = stbuf->f_type; 3512 f_bsize = stbuf->f_bsize; 3513 f_bsize *= bsize_factor; 3514 /* 3515 * f_bsize is adjusted(multiplied) by bsize factor, so we need to 3516 * adjust(divide) the number of blocks, free blocks and available 3517 * blocks by bsize factor 3518 */ 3519 f_blocks = stbuf->f_blocks / bsize_factor; 3520 f_bfree = stbuf->f_bfree / bsize_factor; 3521 f_bavail = stbuf->f_bavail / bsize_factor; 3522 f_files = stbuf->f_files; 3523 f_ffree = stbuf->f_ffree; 3524 fsid_val = (unsigned int) stbuf->f_fsid.__val[0] | 3525 (unsigned long long)stbuf->f_fsid.__val[1] << 32; 3526 f_namelen = stbuf->f_namelen; 3527 3528 return pdu_marshal(pdu, offset, "ddqqqqqqd", 3529 f_type, f_bsize, f_blocks, f_bfree, 3530 f_bavail, f_files, f_ffree, 3531 fsid_val, f_namelen); 3532 } 3533 3534 static void coroutine_fn v9fs_statfs(void *opaque) 3535 { 3536 int32_t fid; 3537 ssize_t retval = 0; 3538 size_t offset = 7; 3539 V9fsFidState *fidp; 3540 struct statfs stbuf; 3541 V9fsPDU *pdu = opaque; 3542 V9fsState *s = pdu->s; 3543 3544 retval = pdu_unmarshal(pdu, offset, "d", &fid); 3545 if (retval < 0) { 3546 goto out_nofid; 3547 } 3548 fidp = get_fid(pdu, fid); 3549 if (fidp == NULL) { 3550 retval = -ENOENT; 3551 goto out_nofid; 3552 } 3553 retval = v9fs_co_statfs(pdu, &fidp->path, &stbuf); 3554 if (retval < 0) { 3555 goto out; 3556 } 3557 retval = v9fs_fill_statfs(s, pdu, &stbuf); 3558 if (retval < 0) { 3559 goto out; 3560 } 3561 retval += offset; 3562 out: 3563 put_fid(pdu, fidp); 3564 out_nofid: 3565 pdu_complete(pdu, retval); 3566 } 3567 3568 static void coroutine_fn v9fs_mknod(void *opaque) 3569 { 3570 3571 int mode; 3572 gid_t gid; 3573 int32_t fid; 3574 V9fsQID qid; 3575 int err = 0; 3576 int major, minor; 3577 size_t offset = 7; 3578 V9fsString name; 3579 struct stat stbuf; 3580 V9fsFidState *fidp; 3581 V9fsPDU *pdu = opaque; 3582 3583 v9fs_string_init(&name); 3584 err = pdu_unmarshal(pdu, offset, "dsdddd", &fid, &name, &mode, 3585 &major, &minor, &gid); 3586 if (err < 0) { 3587 goto out_nofid; 3588 } 3589 trace_v9fs_mknod(pdu->tag, pdu->id, fid, mode, major, minor); 3590 3591 if (name_is_illegal(name.data)) { 3592 err = -ENOENT; 3593 goto out_nofid; 3594 } 3595 3596 if (!strcmp(".", name.data) || !strcmp("..", name.data)) { 3597 err = -EEXIST; 3598 goto out_nofid; 3599 } 3600 3601 fidp = get_fid(pdu, fid); 3602 if (fidp == NULL) { 3603 err = -ENOENT; 3604 goto out_nofid; 3605 } 3606 err = v9fs_co_mknod(pdu, fidp, &name, fidp->uid, gid, 3607 makedev(major, minor), mode, &stbuf); 3608 if (err < 0) { 3609 goto out; 3610 } 3611 err = stat_to_qid(pdu, &stbuf, &qid); 3612 if (err < 0) { 3613 goto out; 3614 } 3615 err = pdu_marshal(pdu, offset, "Q", &qid); 3616 if (err < 0) { 3617 goto out; 3618 } 3619 err += offset; 3620 trace_v9fs_mknod_return(pdu->tag, pdu->id, 3621 qid.type, qid.version, qid.path); 3622 out: 3623 put_fid(pdu, fidp); 3624 out_nofid: 3625 pdu_complete(pdu, err); 3626 v9fs_string_free(&name); 3627 } 3628 3629 /* 3630 * Implement posix byte range locking code 3631 * Server side handling of locking code is very simple, because 9p server in 3632 * QEMU can handle only one client. And most of the lock handling 3633 * (like conflict, merging) etc is done by the VFS layer itself, so no need to 3634 * do any thing in * qemu 9p server side lock code path. 3635 * So when a TLOCK request comes, always return success 3636 */ 3637 static void coroutine_fn v9fs_lock(void *opaque) 3638 { 3639 V9fsFlock flock; 3640 size_t offset = 7; 3641 struct stat stbuf; 3642 V9fsFidState *fidp; 3643 int32_t fid, err = 0; 3644 V9fsPDU *pdu = opaque; 3645 3646 v9fs_string_init(&flock.client_id); 3647 err = pdu_unmarshal(pdu, offset, "dbdqqds", &fid, &flock.type, 3648 &flock.flags, &flock.start, &flock.length, 3649 &flock.proc_id, &flock.client_id); 3650 if (err < 0) { 3651 goto out_nofid; 3652 } 3653 trace_v9fs_lock(pdu->tag, pdu->id, fid, 3654 flock.type, flock.start, flock.length); 3655 3656 3657 /* We support only block flag now (that too ignored currently) */ 3658 if (flock.flags & ~P9_LOCK_FLAGS_BLOCK) { 3659 err = -EINVAL; 3660 goto out_nofid; 3661 } 3662 fidp = get_fid(pdu, fid); 3663 if (fidp == NULL) { 3664 err = -ENOENT; 3665 goto out_nofid; 3666 } 3667 err = v9fs_co_fstat(pdu, fidp, &stbuf); 3668 if (err < 0) { 3669 goto out; 3670 } 3671 err = pdu_marshal(pdu, offset, "b", P9_LOCK_SUCCESS); 3672 if (err < 0) { 3673 goto out; 3674 } 3675 err += offset; 3676 trace_v9fs_lock_return(pdu->tag, pdu->id, P9_LOCK_SUCCESS); 3677 out: 3678 put_fid(pdu, fidp); 3679 out_nofid: 3680 pdu_complete(pdu, err); 3681 v9fs_string_free(&flock.client_id); 3682 } 3683 3684 /* 3685 * When a TGETLOCK request comes, always return success because all lock 3686 * handling is done by client's VFS layer. 3687 */ 3688 static void coroutine_fn v9fs_getlock(void *opaque) 3689 { 3690 size_t offset = 7; 3691 struct stat stbuf; 3692 V9fsFidState *fidp; 3693 V9fsGetlock glock; 3694 int32_t fid, err = 0; 3695 V9fsPDU *pdu = opaque; 3696 3697 v9fs_string_init(&glock.client_id); 3698 err = pdu_unmarshal(pdu, offset, "dbqqds", &fid, &glock.type, 3699 &glock.start, &glock.length, &glock.proc_id, 3700 &glock.client_id); 3701 if (err < 0) { 3702 goto out_nofid; 3703 } 3704 trace_v9fs_getlock(pdu->tag, pdu->id, fid, 3705 glock.type, glock.start, glock.length); 3706 3707 fidp = get_fid(pdu, fid); 3708 if (fidp == NULL) { 3709 err = -ENOENT; 3710 goto out_nofid; 3711 } 3712 err = v9fs_co_fstat(pdu, fidp, &stbuf); 3713 if (err < 0) { 3714 goto out; 3715 } 3716 glock.type = P9_LOCK_TYPE_UNLCK; 3717 err = pdu_marshal(pdu, offset, "bqqds", glock.type, 3718 glock.start, glock.length, glock.proc_id, 3719 &glock.client_id); 3720 if (err < 0) { 3721 goto out; 3722 } 3723 err += offset; 3724 trace_v9fs_getlock_return(pdu->tag, pdu->id, glock.type, glock.start, 3725 glock.length, glock.proc_id); 3726 out: 3727 put_fid(pdu, fidp); 3728 out_nofid: 3729 pdu_complete(pdu, err); 3730 v9fs_string_free(&glock.client_id); 3731 } 3732 3733 static void coroutine_fn v9fs_mkdir(void *opaque) 3734 { 3735 V9fsPDU *pdu = opaque; 3736 size_t offset = 7; 3737 int32_t fid; 3738 struct stat stbuf; 3739 V9fsQID qid; 3740 V9fsString name; 3741 V9fsFidState *fidp; 3742 gid_t gid; 3743 int mode; 3744 int err = 0; 3745 3746 v9fs_string_init(&name); 3747 err = pdu_unmarshal(pdu, offset, "dsdd", &fid, &name, &mode, &gid); 3748 if (err < 0) { 3749 goto out_nofid; 3750 } 3751 trace_v9fs_mkdir(pdu->tag, pdu->id, fid, name.data, mode, gid); 3752 3753 if (name_is_illegal(name.data)) { 3754 err = -ENOENT; 3755 goto out_nofid; 3756 } 3757 3758 if (!strcmp(".", name.data) || !strcmp("..", name.data)) { 3759 err = -EEXIST; 3760 goto out_nofid; 3761 } 3762 3763 fidp = get_fid(pdu, fid); 3764 if (fidp == NULL) { 3765 err = -ENOENT; 3766 goto out_nofid; 3767 } 3768 err = v9fs_co_mkdir(pdu, fidp, &name, mode, fidp->uid, gid, &stbuf); 3769 if (err < 0) { 3770 goto out; 3771 } 3772 err = stat_to_qid(pdu, &stbuf, &qid); 3773 if (err < 0) { 3774 goto out; 3775 } 3776 err = pdu_marshal(pdu, offset, "Q", &qid); 3777 if (err < 0) { 3778 goto out; 3779 } 3780 err += offset; 3781 trace_v9fs_mkdir_return(pdu->tag, pdu->id, 3782 qid.type, qid.version, qid.path, err); 3783 out: 3784 put_fid(pdu, fidp); 3785 out_nofid: 3786 pdu_complete(pdu, err); 3787 v9fs_string_free(&name); 3788 } 3789 3790 static void coroutine_fn v9fs_xattrwalk(void *opaque) 3791 { 3792 int64_t size; 3793 V9fsString name; 3794 ssize_t err = 0; 3795 size_t offset = 7; 3796 int32_t fid, newfid; 3797 V9fsFidState *file_fidp; 3798 V9fsFidState *xattr_fidp = NULL; 3799 V9fsPDU *pdu = opaque; 3800 V9fsState *s = pdu->s; 3801 3802 v9fs_string_init(&name); 3803 err = pdu_unmarshal(pdu, offset, "dds", &fid, &newfid, &name); 3804 if (err < 0) { 3805 goto out_nofid; 3806 } 3807 trace_v9fs_xattrwalk(pdu->tag, pdu->id, fid, newfid, name.data); 3808 3809 file_fidp = get_fid(pdu, fid); 3810 if (file_fidp == NULL) { 3811 err = -ENOENT; 3812 goto out_nofid; 3813 } 3814 xattr_fidp = alloc_fid(s, newfid); 3815 if (xattr_fidp == NULL) { 3816 err = -EINVAL; 3817 goto out; 3818 } 3819 v9fs_path_copy(&xattr_fidp->path, &file_fidp->path); 3820 if (!v9fs_string_size(&name)) { 3821 /* 3822 * listxattr request. Get the size first 3823 */ 3824 size = v9fs_co_llistxattr(pdu, &xattr_fidp->path, NULL, 0); 3825 if (size < 0) { 3826 err = size; 3827 clunk_fid(s, xattr_fidp->fid); 3828 goto out; 3829 } 3830 /* 3831 * Read the xattr value 3832 */ 3833 xattr_fidp->fs.xattr.len = size; 3834 xattr_fidp->fid_type = P9_FID_XATTR; 3835 xattr_fidp->fs.xattr.xattrwalk_fid = true; 3836 xattr_fidp->fs.xattr.value = g_malloc0(size); 3837 if (size) { 3838 err = v9fs_co_llistxattr(pdu, &xattr_fidp->path, 3839 xattr_fidp->fs.xattr.value, 3840 xattr_fidp->fs.xattr.len); 3841 if (err < 0) { 3842 clunk_fid(s, xattr_fidp->fid); 3843 goto out; 3844 } 3845 } 3846 err = pdu_marshal(pdu, offset, "q", size); 3847 if (err < 0) { 3848 goto out; 3849 } 3850 err += offset; 3851 } else { 3852 /* 3853 * specific xattr fid. We check for xattr 3854 * presence also collect the xattr size 3855 */ 3856 size = v9fs_co_lgetxattr(pdu, &xattr_fidp->path, 3857 &name, NULL, 0); 3858 if (size < 0) { 3859 err = size; 3860 clunk_fid(s, xattr_fidp->fid); 3861 goto out; 3862 } 3863 /* 3864 * Read the xattr value 3865 */ 3866 xattr_fidp->fs.xattr.len = size; 3867 xattr_fidp->fid_type = P9_FID_XATTR; 3868 xattr_fidp->fs.xattr.xattrwalk_fid = true; 3869 xattr_fidp->fs.xattr.value = g_malloc0(size); 3870 if (size) { 3871 err = v9fs_co_lgetxattr(pdu, &xattr_fidp->path, 3872 &name, xattr_fidp->fs.xattr.value, 3873 xattr_fidp->fs.xattr.len); 3874 if (err < 0) { 3875 clunk_fid(s, xattr_fidp->fid); 3876 goto out; 3877 } 3878 } 3879 err = pdu_marshal(pdu, offset, "q", size); 3880 if (err < 0) { 3881 goto out; 3882 } 3883 err += offset; 3884 } 3885 trace_v9fs_xattrwalk_return(pdu->tag, pdu->id, size); 3886 out: 3887 put_fid(pdu, file_fidp); 3888 if (xattr_fidp) { 3889 put_fid(pdu, xattr_fidp); 3890 } 3891 out_nofid: 3892 pdu_complete(pdu, err); 3893 v9fs_string_free(&name); 3894 } 3895 3896 static void coroutine_fn v9fs_xattrcreate(void *opaque) 3897 { 3898 int flags, rflags = 0; 3899 int32_t fid; 3900 uint64_t size; 3901 ssize_t err = 0; 3902 V9fsString name; 3903 size_t offset = 7; 3904 V9fsFidState *file_fidp; 3905 V9fsFidState *xattr_fidp; 3906 V9fsPDU *pdu = opaque; 3907 3908 v9fs_string_init(&name); 3909 err = pdu_unmarshal(pdu, offset, "dsqd", &fid, &name, &size, &flags); 3910 if (err < 0) { 3911 goto out_nofid; 3912 } 3913 trace_v9fs_xattrcreate(pdu->tag, pdu->id, fid, name.data, size, flags); 3914 3915 if (flags & ~(P9_XATTR_CREATE | P9_XATTR_REPLACE)) { 3916 err = -EINVAL; 3917 goto out_nofid; 3918 } 3919 3920 if (flags & P9_XATTR_CREATE) { 3921 rflags |= XATTR_CREATE; 3922 } 3923 3924 if (flags & P9_XATTR_REPLACE) { 3925 rflags |= XATTR_REPLACE; 3926 } 3927 3928 if (size > XATTR_SIZE_MAX) { 3929 err = -E2BIG; 3930 goto out_nofid; 3931 } 3932 3933 file_fidp = get_fid(pdu, fid); 3934 if (file_fidp == NULL) { 3935 err = -EINVAL; 3936 goto out_nofid; 3937 } 3938 if (file_fidp->fid_type != P9_FID_NONE) { 3939 err = -EINVAL; 3940 goto out_put_fid; 3941 } 3942 3943 /* Make the file fid point to xattr */ 3944 xattr_fidp = file_fidp; 3945 xattr_fidp->fid_type = P9_FID_XATTR; 3946 xattr_fidp->fs.xattr.copied_len = 0; 3947 xattr_fidp->fs.xattr.xattrwalk_fid = false; 3948 xattr_fidp->fs.xattr.len = size; 3949 xattr_fidp->fs.xattr.flags = rflags; 3950 v9fs_string_init(&xattr_fidp->fs.xattr.name); 3951 v9fs_string_copy(&xattr_fidp->fs.xattr.name, &name); 3952 xattr_fidp->fs.xattr.value = g_malloc0(size); 3953 err = offset; 3954 out_put_fid: 3955 put_fid(pdu, file_fidp); 3956 out_nofid: 3957 pdu_complete(pdu, err); 3958 v9fs_string_free(&name); 3959 } 3960 3961 static void coroutine_fn v9fs_readlink(void *opaque) 3962 { 3963 V9fsPDU *pdu = opaque; 3964 size_t offset = 7; 3965 V9fsString target; 3966 int32_t fid; 3967 int err = 0; 3968 V9fsFidState *fidp; 3969 3970 err = pdu_unmarshal(pdu, offset, "d", &fid); 3971 if (err < 0) { 3972 goto out_nofid; 3973 } 3974 trace_v9fs_readlink(pdu->tag, pdu->id, fid); 3975 fidp = get_fid(pdu, fid); 3976 if (fidp == NULL) { 3977 err = -ENOENT; 3978 goto out_nofid; 3979 } 3980 3981 v9fs_string_init(&target); 3982 err = v9fs_co_readlink(pdu, &fidp->path, &target); 3983 if (err < 0) { 3984 goto out; 3985 } 3986 err = pdu_marshal(pdu, offset, "s", &target); 3987 if (err < 0) { 3988 v9fs_string_free(&target); 3989 goto out; 3990 } 3991 err += offset; 3992 trace_v9fs_readlink_return(pdu->tag, pdu->id, target.data); 3993 v9fs_string_free(&target); 3994 out: 3995 put_fid(pdu, fidp); 3996 out_nofid: 3997 pdu_complete(pdu, err); 3998 } 3999 4000 static CoroutineEntry *pdu_co_handlers[] = { 4001 [P9_TREADDIR] = v9fs_readdir, 4002 [P9_TSTATFS] = v9fs_statfs, 4003 [P9_TGETATTR] = v9fs_getattr, 4004 [P9_TSETATTR] = v9fs_setattr, 4005 [P9_TXATTRWALK] = v9fs_xattrwalk, 4006 [P9_TXATTRCREATE] = v9fs_xattrcreate, 4007 [P9_TMKNOD] = v9fs_mknod, 4008 [P9_TRENAME] = v9fs_rename, 4009 [P9_TLOCK] = v9fs_lock, 4010 [P9_TGETLOCK] = v9fs_getlock, 4011 [P9_TRENAMEAT] = v9fs_renameat, 4012 [P9_TREADLINK] = v9fs_readlink, 4013 [P9_TUNLINKAT] = v9fs_unlinkat, 4014 [P9_TMKDIR] = v9fs_mkdir, 4015 [P9_TVERSION] = v9fs_version, 4016 [P9_TLOPEN] = v9fs_open, 4017 [P9_TATTACH] = v9fs_attach, 4018 [P9_TSTAT] = v9fs_stat, 4019 [P9_TWALK] = v9fs_walk, 4020 [P9_TCLUNK] = v9fs_clunk, 4021 [P9_TFSYNC] = v9fs_fsync, 4022 [P9_TOPEN] = v9fs_open, 4023 [P9_TREAD] = v9fs_read, 4024 #if 0 4025 [P9_TAUTH] = v9fs_auth, 4026 #endif 4027 [P9_TFLUSH] = v9fs_flush, 4028 [P9_TLINK] = v9fs_link, 4029 [P9_TSYMLINK] = v9fs_symlink, 4030 [P9_TCREATE] = v9fs_create, 4031 [P9_TLCREATE] = v9fs_lcreate, 4032 [P9_TWRITE] = v9fs_write, 4033 [P9_TWSTAT] = v9fs_wstat, 4034 [P9_TREMOVE] = v9fs_remove, 4035 }; 4036 4037 static void coroutine_fn v9fs_op_not_supp(void *opaque) 4038 { 4039 V9fsPDU *pdu = opaque; 4040 pdu_complete(pdu, -EOPNOTSUPP); 4041 } 4042 4043 static void coroutine_fn v9fs_fs_ro(void *opaque) 4044 { 4045 V9fsPDU *pdu = opaque; 4046 pdu_complete(pdu, -EROFS); 4047 } 4048 4049 static inline bool is_read_only_op(V9fsPDU *pdu) 4050 { 4051 switch (pdu->id) { 4052 case P9_TREADDIR: 4053 case P9_TSTATFS: 4054 case P9_TGETATTR: 4055 case P9_TXATTRWALK: 4056 case P9_TLOCK: 4057 case P9_TGETLOCK: 4058 case P9_TREADLINK: 4059 case P9_TVERSION: 4060 case P9_TLOPEN: 4061 case P9_TATTACH: 4062 case P9_TSTAT: 4063 case P9_TWALK: 4064 case P9_TCLUNK: 4065 case P9_TFSYNC: 4066 case P9_TOPEN: 4067 case P9_TREAD: 4068 case P9_TAUTH: 4069 case P9_TFLUSH: 4070 return 1; 4071 default: 4072 return 0; 4073 } 4074 } 4075 4076 void pdu_submit(V9fsPDU *pdu, P9MsgHeader *hdr) 4077 { 4078 Coroutine *co; 4079 CoroutineEntry *handler; 4080 V9fsState *s = pdu->s; 4081 4082 pdu->size = le32_to_cpu(hdr->size_le); 4083 pdu->id = hdr->id; 4084 pdu->tag = le16_to_cpu(hdr->tag_le); 4085 4086 if (pdu->id >= ARRAY_SIZE(pdu_co_handlers) || 4087 (pdu_co_handlers[pdu->id] == NULL)) { 4088 handler = v9fs_op_not_supp; 4089 } else if (is_ro_export(&s->ctx) && !is_read_only_op(pdu)) { 4090 handler = v9fs_fs_ro; 4091 } else { 4092 handler = pdu_co_handlers[pdu->id]; 4093 } 4094 4095 qemu_co_queue_init(&pdu->complete); 4096 co = qemu_coroutine_create(handler, pdu); 4097 qemu_coroutine_enter(co); 4098 } 4099 4100 /* Returns 0 on success, 1 on failure. */ 4101 int v9fs_device_realize_common(V9fsState *s, const V9fsTransport *t, 4102 Error **errp) 4103 { 4104 ERRP_GUARD(); 4105 int i, len; 4106 struct stat stat; 4107 FsDriverEntry *fse; 4108 V9fsPath path; 4109 int rc = 1; 4110 4111 assert(!s->transport); 4112 s->transport = t; 4113 4114 /* initialize pdu allocator */ 4115 QLIST_INIT(&s->free_list); 4116 QLIST_INIT(&s->active_list); 4117 for (i = 0; i < MAX_REQ; i++) { 4118 QLIST_INSERT_HEAD(&s->free_list, &s->pdus[i], next); 4119 s->pdus[i].s = s; 4120 s->pdus[i].idx = i; 4121 } 4122 4123 v9fs_path_init(&path); 4124 4125 fse = get_fsdev_fsentry(s->fsconf.fsdev_id); 4126 4127 if (!fse) { 4128 /* We don't have a fsdev identified by fsdev_id */ 4129 error_setg(errp, "9pfs device couldn't find fsdev with the " 4130 "id = %s", 4131 s->fsconf.fsdev_id ? s->fsconf.fsdev_id : "NULL"); 4132 goto out; 4133 } 4134 4135 if (!s->fsconf.tag) { 4136 /* we haven't specified a mount_tag */ 4137 error_setg(errp, "fsdev with id %s needs mount_tag arguments", 4138 s->fsconf.fsdev_id); 4139 goto out; 4140 } 4141 4142 s->ctx.export_flags = fse->export_flags; 4143 s->ctx.fs_root = g_strdup(fse->path); 4144 s->ctx.exops.get_st_gen = NULL; 4145 len = strlen(s->fsconf.tag); 4146 if (len > MAX_TAG_LEN - 1) { 4147 error_setg(errp, "mount tag '%s' (%d bytes) is longer than " 4148 "maximum (%d bytes)", s->fsconf.tag, len, MAX_TAG_LEN - 1); 4149 goto out; 4150 } 4151 4152 s->tag = g_strdup(s->fsconf.tag); 4153 s->ctx.uid = -1; 4154 4155 s->ops = fse->ops; 4156 4157 s->ctx.fmode = fse->fmode; 4158 s->ctx.dmode = fse->dmode; 4159 4160 QSIMPLEQ_INIT(&s->fid_list); 4161 qemu_co_rwlock_init(&s->rename_lock); 4162 4163 if (s->ops->init(&s->ctx, errp) < 0) { 4164 error_prepend(errp, "cannot initialize fsdev '%s': ", 4165 s->fsconf.fsdev_id); 4166 goto out; 4167 } 4168 4169 /* 4170 * Check details of export path, We need to use fs driver 4171 * call back to do that. Since we are in the init path, we don't 4172 * use co-routines here. 4173 */ 4174 if (s->ops->name_to_path(&s->ctx, NULL, "/", &path) < 0) { 4175 error_setg(errp, 4176 "error in converting name to path %s", strerror(errno)); 4177 goto out; 4178 } 4179 if (s->ops->lstat(&s->ctx, &path, &stat)) { 4180 error_setg(errp, "share path %s does not exist", fse->path); 4181 goto out; 4182 } else if (!S_ISDIR(stat.st_mode)) { 4183 error_setg(errp, "share path %s is not a directory", fse->path); 4184 goto out; 4185 } 4186 4187 s->dev_id = stat.st_dev; 4188 4189 /* init inode remapping : */ 4190 /* hash table for variable length inode suffixes */ 4191 qpd_table_init(&s->qpd_table); 4192 /* hash table for slow/full inode remapping (most users won't need it) */ 4193 qpf_table_init(&s->qpf_table); 4194 /* hash table for quick inode remapping */ 4195 qpp_table_init(&s->qpp_table); 4196 s->qp_ndevices = 0; 4197 s->qp_affix_next = 1; /* reserve 0 to detect overflow */ 4198 s->qp_fullpath_next = 1; 4199 4200 s->ctx.fst = &fse->fst; 4201 fsdev_throttle_init(s->ctx.fst); 4202 4203 rc = 0; 4204 out: 4205 if (rc) { 4206 v9fs_device_unrealize_common(s); 4207 } 4208 v9fs_path_free(&path); 4209 return rc; 4210 } 4211 4212 void v9fs_device_unrealize_common(V9fsState *s) 4213 { 4214 if (s->ops && s->ops->cleanup) { 4215 s->ops->cleanup(&s->ctx); 4216 } 4217 if (s->ctx.fst) { 4218 fsdev_throttle_cleanup(s->ctx.fst); 4219 } 4220 g_free(s->tag); 4221 qp_table_destroy(&s->qpd_table); 4222 qp_table_destroy(&s->qpp_table); 4223 qp_table_destroy(&s->qpf_table); 4224 g_free(s->ctx.fs_root); 4225 } 4226 4227 typedef struct VirtfsCoResetData { 4228 V9fsPDU pdu; 4229 bool done; 4230 } VirtfsCoResetData; 4231 4232 static void coroutine_fn virtfs_co_reset(void *opaque) 4233 { 4234 VirtfsCoResetData *data = opaque; 4235 4236 virtfs_reset(&data->pdu); 4237 data->done = true; 4238 } 4239 4240 void v9fs_reset(V9fsState *s) 4241 { 4242 VirtfsCoResetData data = { .pdu = { .s = s }, .done = false }; 4243 Coroutine *co; 4244 4245 while (!QLIST_EMPTY(&s->active_list)) { 4246 aio_poll(qemu_get_aio_context(), true); 4247 } 4248 4249 co = qemu_coroutine_create(virtfs_co_reset, &data); 4250 qemu_coroutine_enter(co); 4251 4252 while (!data.done) { 4253 aio_poll(qemu_get_aio_context(), true); 4254 } 4255 } 4256 4257 static void __attribute__((__constructor__)) v9fs_set_fd_limit(void) 4258 { 4259 struct rlimit rlim; 4260 if (getrlimit(RLIMIT_NOFILE, &rlim) < 0) { 4261 error_report("Failed to get the resource limit"); 4262 exit(1); 4263 } 4264 open_fd_hw = rlim.rlim_cur - MIN(400, rlim.rlim_cur / 3); 4265 open_fd_rc = rlim.rlim_cur / 2; 4266 } 4267