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