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