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