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