1 /* 2 * 9p Posix callback 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 "9p.h" 16 #include "9p-local.h" 17 #include "9p-xattr.h" 18 #include "9p-util.h" 19 #include "fsdev/qemu-fsdev.h" /* local_ops */ 20 #include <arpa/inet.h> 21 #include <pwd.h> 22 #include <grp.h> 23 #include <sys/socket.h> 24 #include <sys/un.h> 25 #include "qemu/xattr.h" 26 #include "qemu/cutils.h" 27 #include "qemu/error-report.h" 28 #include <libgen.h> 29 #include <linux/fs.h> 30 #ifdef CONFIG_LINUX_MAGIC_H 31 #include <linux/magic.h> 32 #endif 33 #include <sys/ioctl.h> 34 35 #ifndef XFS_SUPER_MAGIC 36 #define XFS_SUPER_MAGIC 0x58465342 37 #endif 38 #ifndef EXT2_SUPER_MAGIC 39 #define EXT2_SUPER_MAGIC 0xEF53 40 #endif 41 #ifndef REISERFS_SUPER_MAGIC 42 #define REISERFS_SUPER_MAGIC 0x52654973 43 #endif 44 #ifndef BTRFS_SUPER_MAGIC 45 #define BTRFS_SUPER_MAGIC 0x9123683E 46 #endif 47 48 typedef struct { 49 int mountfd; 50 } LocalData; 51 52 int local_open_nofollow(FsContext *fs_ctx, const char *path, int flags, 53 mode_t mode) 54 { 55 LocalData *data = fs_ctx->private; 56 57 /* All paths are relative to the path data->mountfd points to */ 58 while (*path == '/') { 59 path++; 60 } 61 62 return relative_openat_nofollow(data->mountfd, path, flags, mode); 63 } 64 65 int local_opendir_nofollow(FsContext *fs_ctx, const char *path) 66 { 67 return local_open_nofollow(fs_ctx, path, O_DIRECTORY | O_RDONLY, 0); 68 } 69 70 static void renameat_preserve_errno(int odirfd, const char *opath, int ndirfd, 71 const char *npath) 72 { 73 int serrno = errno; 74 renameat(odirfd, opath, ndirfd, npath); 75 errno = serrno; 76 } 77 78 #define VIRTFS_META_DIR ".virtfs_metadata" 79 80 static char *local_mapped_attr_path(FsContext *ctx, const char *path) 81 { 82 int dirlen; 83 const char *name = strrchr(path, '/'); 84 if (name) { 85 dirlen = name - path; 86 ++name; 87 } else { 88 name = path; 89 dirlen = 0; 90 } 91 return g_strdup_printf("%s/%.*s/%s/%s", ctx->fs_root, 92 dirlen, path, VIRTFS_META_DIR, name); 93 } 94 95 static FILE *local_fopen(const char *path, const char *mode) 96 { 97 int fd, o_mode = 0; 98 FILE *fp; 99 int flags = O_NOFOLLOW; 100 /* 101 * only supports two modes 102 */ 103 if (mode[0] == 'r') { 104 flags |= O_RDONLY; 105 } else if (mode[0] == 'w') { 106 flags |= O_WRONLY | O_TRUNC | O_CREAT; 107 o_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; 108 } else { 109 return NULL; 110 } 111 fd = open(path, flags, o_mode); 112 if (fd == -1) { 113 return NULL; 114 } 115 fp = fdopen(fd, mode); 116 if (!fp) { 117 close(fd); 118 } 119 return fp; 120 } 121 122 static FILE *local_fopenat(int dirfd, const char *name, const char *mode) 123 { 124 int fd, o_mode = 0; 125 FILE *fp; 126 int flags; 127 /* 128 * only supports two modes 129 */ 130 if (mode[0] == 'r') { 131 flags = O_RDONLY; 132 } else if (mode[0] == 'w') { 133 flags = O_WRONLY | O_TRUNC | O_CREAT; 134 o_mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH; 135 } else { 136 return NULL; 137 } 138 fd = openat_file(dirfd, name, flags, o_mode); 139 if (fd == -1) { 140 return NULL; 141 } 142 fp = fdopen(fd, mode); 143 if (!fp) { 144 close(fd); 145 } 146 return fp; 147 } 148 149 #define ATTR_MAX 100 150 static void local_mapped_file_attr(int dirfd, const char *name, 151 struct stat *stbuf) 152 { 153 FILE *fp; 154 char buf[ATTR_MAX]; 155 int map_dirfd; 156 157 map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR); 158 if (map_dirfd == -1) { 159 return; 160 } 161 162 fp = local_fopenat(map_dirfd, name, "r"); 163 close_preserve_errno(map_dirfd); 164 if (!fp) { 165 return; 166 } 167 memset(buf, 0, ATTR_MAX); 168 while (fgets(buf, ATTR_MAX, fp)) { 169 if (!strncmp(buf, "virtfs.uid", 10)) { 170 stbuf->st_uid = atoi(buf+11); 171 } else if (!strncmp(buf, "virtfs.gid", 10)) { 172 stbuf->st_gid = atoi(buf+11); 173 } else if (!strncmp(buf, "virtfs.mode", 11)) { 174 stbuf->st_mode = atoi(buf+12); 175 } else if (!strncmp(buf, "virtfs.rdev", 11)) { 176 stbuf->st_rdev = atoi(buf+12); 177 } 178 memset(buf, 0, ATTR_MAX); 179 } 180 fclose(fp); 181 } 182 183 static int local_lstat(FsContext *fs_ctx, V9fsPath *fs_path, struct stat *stbuf) 184 { 185 int err = -1; 186 char *dirpath = g_path_get_dirname(fs_path->data); 187 char *name = g_path_get_basename(fs_path->data); 188 int dirfd; 189 190 dirfd = local_opendir_nofollow(fs_ctx, dirpath); 191 if (dirfd == -1) { 192 goto out; 193 } 194 195 err = fstatat(dirfd, name, stbuf, AT_SYMLINK_NOFOLLOW); 196 if (err) { 197 goto err_out; 198 } 199 if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 200 /* Actual credentials are part of extended attrs */ 201 uid_t tmp_uid; 202 gid_t tmp_gid; 203 mode_t tmp_mode; 204 dev_t tmp_dev; 205 206 if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.uid", &tmp_uid, 207 sizeof(uid_t)) > 0) { 208 stbuf->st_uid = le32_to_cpu(tmp_uid); 209 } 210 if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.gid", &tmp_gid, 211 sizeof(gid_t)) > 0) { 212 stbuf->st_gid = le32_to_cpu(tmp_gid); 213 } 214 if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.mode", &tmp_mode, 215 sizeof(mode_t)) > 0) { 216 stbuf->st_mode = le32_to_cpu(tmp_mode); 217 } 218 if (fgetxattrat_nofollow(dirfd, name, "user.virtfs.rdev", &tmp_dev, 219 sizeof(dev_t)) > 0) { 220 stbuf->st_rdev = le64_to_cpu(tmp_dev); 221 } 222 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 223 local_mapped_file_attr(dirfd, name, stbuf); 224 } 225 226 err_out: 227 close_preserve_errno(dirfd); 228 out: 229 g_free(name); 230 g_free(dirpath); 231 return err; 232 } 233 234 static int local_create_mapped_attr_dir(FsContext *ctx, const char *path) 235 { 236 int err; 237 char *attr_dir; 238 char *tmp_path = g_strdup(path); 239 240 attr_dir = g_strdup_printf("%s/%s/%s", 241 ctx->fs_root, dirname(tmp_path), VIRTFS_META_DIR); 242 243 err = mkdir(attr_dir, 0700); 244 if (err < 0 && errno == EEXIST) { 245 err = 0; 246 } 247 g_free(attr_dir); 248 g_free(tmp_path); 249 return err; 250 } 251 252 static int local_set_mapped_file_attr(FsContext *ctx, 253 const char *path, FsCred *credp) 254 { 255 FILE *fp; 256 int ret = 0; 257 char buf[ATTR_MAX]; 258 char *attr_path; 259 int uid = -1, gid = -1, mode = -1, rdev = -1; 260 261 attr_path = local_mapped_attr_path(ctx, path); 262 fp = local_fopen(attr_path, "r"); 263 if (!fp) { 264 goto create_map_file; 265 } 266 memset(buf, 0, ATTR_MAX); 267 while (fgets(buf, ATTR_MAX, fp)) { 268 if (!strncmp(buf, "virtfs.uid", 10)) { 269 uid = atoi(buf+11); 270 } else if (!strncmp(buf, "virtfs.gid", 10)) { 271 gid = atoi(buf+11); 272 } else if (!strncmp(buf, "virtfs.mode", 11)) { 273 mode = atoi(buf+12); 274 } else if (!strncmp(buf, "virtfs.rdev", 11)) { 275 rdev = atoi(buf+12); 276 } 277 memset(buf, 0, ATTR_MAX); 278 } 279 fclose(fp); 280 goto update_map_file; 281 282 create_map_file: 283 ret = local_create_mapped_attr_dir(ctx, path); 284 if (ret < 0) { 285 goto err_out; 286 } 287 288 update_map_file: 289 fp = local_fopen(attr_path, "w"); 290 if (!fp) { 291 ret = -1; 292 goto err_out; 293 } 294 295 if (credp->fc_uid != -1) { 296 uid = credp->fc_uid; 297 } 298 if (credp->fc_gid != -1) { 299 gid = credp->fc_gid; 300 } 301 if (credp->fc_mode != -1) { 302 mode = credp->fc_mode; 303 } 304 if (credp->fc_rdev != -1) { 305 rdev = credp->fc_rdev; 306 } 307 308 309 if (uid != -1) { 310 fprintf(fp, "virtfs.uid=%d\n", uid); 311 } 312 if (gid != -1) { 313 fprintf(fp, "virtfs.gid=%d\n", gid); 314 } 315 if (mode != -1) { 316 fprintf(fp, "virtfs.mode=%d\n", mode); 317 } 318 if (rdev != -1) { 319 fprintf(fp, "virtfs.rdev=%d\n", rdev); 320 } 321 fclose(fp); 322 323 err_out: 324 g_free(attr_path); 325 return ret; 326 } 327 328 static int local_set_xattr(const char *path, FsCred *credp) 329 { 330 int err; 331 332 if (credp->fc_uid != -1) { 333 uint32_t tmp_uid = cpu_to_le32(credp->fc_uid); 334 err = setxattr(path, "user.virtfs.uid", &tmp_uid, sizeof(uid_t), 0); 335 if (err) { 336 return err; 337 } 338 } 339 if (credp->fc_gid != -1) { 340 uint32_t tmp_gid = cpu_to_le32(credp->fc_gid); 341 err = setxattr(path, "user.virtfs.gid", &tmp_gid, sizeof(gid_t), 0); 342 if (err) { 343 return err; 344 } 345 } 346 if (credp->fc_mode != -1) { 347 uint32_t tmp_mode = cpu_to_le32(credp->fc_mode); 348 err = setxattr(path, "user.virtfs.mode", &tmp_mode, sizeof(mode_t), 0); 349 if (err) { 350 return err; 351 } 352 } 353 if (credp->fc_rdev != -1) { 354 uint64_t tmp_rdev = cpu_to_le64(credp->fc_rdev); 355 err = setxattr(path, "user.virtfs.rdev", &tmp_rdev, sizeof(dev_t), 0); 356 if (err) { 357 return err; 358 } 359 } 360 return 0; 361 } 362 363 static int local_post_create_passthrough(FsContext *fs_ctx, const char *path, 364 FsCred *credp) 365 { 366 char *buffer; 367 368 buffer = rpath(fs_ctx, path); 369 if (lchown(buffer, credp->fc_uid, credp->fc_gid) < 0) { 370 /* 371 * If we fail to change ownership and if we are 372 * using security model none. Ignore the error 373 */ 374 if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) { 375 goto err; 376 } 377 } 378 379 if (chmod(buffer, credp->fc_mode & 07777) < 0) { 380 goto err; 381 } 382 383 g_free(buffer); 384 return 0; 385 err: 386 g_free(buffer); 387 return -1; 388 } 389 390 static ssize_t local_readlink(FsContext *fs_ctx, V9fsPath *fs_path, 391 char *buf, size_t bufsz) 392 { 393 ssize_t tsize = -1; 394 395 if ((fs_ctx->export_flags & V9FS_SM_MAPPED) || 396 (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE)) { 397 int fd; 398 399 fd = local_open_nofollow(fs_ctx, fs_path->data, O_RDONLY, 0); 400 if (fd == -1) { 401 return -1; 402 } 403 do { 404 tsize = read(fd, (void *)buf, bufsz); 405 } while (tsize == -1 && errno == EINTR); 406 close_preserve_errno(fd); 407 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || 408 (fs_ctx->export_flags & V9FS_SM_NONE)) { 409 char *dirpath = g_path_get_dirname(fs_path->data); 410 char *name = g_path_get_basename(fs_path->data); 411 int dirfd; 412 413 dirfd = local_opendir_nofollow(fs_ctx, dirpath); 414 if (dirfd == -1) { 415 goto out; 416 } 417 418 tsize = readlinkat(dirfd, name, buf, bufsz); 419 close_preserve_errno(dirfd); 420 out: 421 g_free(name); 422 g_free(dirpath); 423 } 424 return tsize; 425 } 426 427 static int local_close(FsContext *ctx, V9fsFidOpenState *fs) 428 { 429 return close(fs->fd); 430 } 431 432 static int local_closedir(FsContext *ctx, V9fsFidOpenState *fs) 433 { 434 return closedir(fs->dir.stream); 435 } 436 437 static int local_open(FsContext *ctx, V9fsPath *fs_path, 438 int flags, V9fsFidOpenState *fs) 439 { 440 int fd; 441 442 fd = local_open_nofollow(ctx, fs_path->data, flags, 0); 443 if (fd == -1) { 444 return -1; 445 } 446 fs->fd = fd; 447 return fs->fd; 448 } 449 450 static int local_opendir(FsContext *ctx, 451 V9fsPath *fs_path, V9fsFidOpenState *fs) 452 { 453 int dirfd; 454 DIR *stream; 455 456 dirfd = local_opendir_nofollow(ctx, fs_path->data); 457 if (dirfd == -1) { 458 return -1; 459 } 460 461 stream = fdopendir(dirfd); 462 if (!stream) { 463 return -1; 464 } 465 fs->dir.stream = stream; 466 return 0; 467 } 468 469 static void local_rewinddir(FsContext *ctx, V9fsFidOpenState *fs) 470 { 471 rewinddir(fs->dir.stream); 472 } 473 474 static off_t local_telldir(FsContext *ctx, V9fsFidOpenState *fs) 475 { 476 return telldir(fs->dir.stream); 477 } 478 479 static struct dirent *local_readdir(FsContext *ctx, V9fsFidOpenState *fs) 480 { 481 struct dirent *entry; 482 483 again: 484 entry = readdir(fs->dir.stream); 485 if (!entry) { 486 return NULL; 487 } 488 489 if (ctx->export_flags & V9FS_SM_MAPPED) { 490 entry->d_type = DT_UNKNOWN; 491 } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { 492 if (!strcmp(entry->d_name, VIRTFS_META_DIR)) { 493 /* skp the meta data directory */ 494 goto again; 495 } 496 entry->d_type = DT_UNKNOWN; 497 } 498 499 return entry; 500 } 501 502 static void local_seekdir(FsContext *ctx, V9fsFidOpenState *fs, off_t off) 503 { 504 seekdir(fs->dir.stream, off); 505 } 506 507 static ssize_t local_preadv(FsContext *ctx, V9fsFidOpenState *fs, 508 const struct iovec *iov, 509 int iovcnt, off_t offset) 510 { 511 #ifdef CONFIG_PREADV 512 return preadv(fs->fd, iov, iovcnt, offset); 513 #else 514 int err = lseek(fs->fd, offset, SEEK_SET); 515 if (err == -1) { 516 return err; 517 } else { 518 return readv(fs->fd, iov, iovcnt); 519 } 520 #endif 521 } 522 523 static ssize_t local_pwritev(FsContext *ctx, V9fsFidOpenState *fs, 524 const struct iovec *iov, 525 int iovcnt, off_t offset) 526 { 527 ssize_t ret; 528 #ifdef CONFIG_PREADV 529 ret = pwritev(fs->fd, iov, iovcnt, offset); 530 #else 531 int err = lseek(fs->fd, offset, SEEK_SET); 532 if (err == -1) { 533 return err; 534 } else { 535 ret = writev(fs->fd, iov, iovcnt); 536 } 537 #endif 538 #ifdef CONFIG_SYNC_FILE_RANGE 539 if (ret > 0 && ctx->export_flags & V9FS_IMMEDIATE_WRITEOUT) { 540 /* 541 * Initiate a writeback. This is not a data integrity sync. 542 * We want to ensure that we don't leave dirty pages in the cache 543 * after write when writeout=immediate is sepcified. 544 */ 545 sync_file_range(fs->fd, offset, ret, 546 SYNC_FILE_RANGE_WAIT_BEFORE | SYNC_FILE_RANGE_WRITE); 547 } 548 #endif 549 return ret; 550 } 551 552 static int local_chmod(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp) 553 { 554 char *buffer; 555 int ret = -1; 556 char *path = fs_path->data; 557 558 if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 559 buffer = rpath(fs_ctx, path); 560 ret = local_set_xattr(buffer, credp); 561 g_free(buffer); 562 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 563 return local_set_mapped_file_attr(fs_ctx, path, credp); 564 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || 565 (fs_ctx->export_flags & V9FS_SM_NONE)) { 566 buffer = rpath(fs_ctx, path); 567 ret = chmod(buffer, credp->fc_mode); 568 g_free(buffer); 569 } 570 return ret; 571 } 572 573 static int local_mknod(FsContext *fs_ctx, V9fsPath *dir_path, 574 const char *name, FsCred *credp) 575 { 576 char *path; 577 int err = -1; 578 int serrno = 0; 579 V9fsString fullname; 580 char *buffer = NULL; 581 582 v9fs_string_init(&fullname); 583 v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name); 584 path = fullname.data; 585 586 /* Determine the security model */ 587 if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 588 buffer = rpath(fs_ctx, path); 589 err = mknod(buffer, SM_LOCAL_MODE_BITS|S_IFREG, 0); 590 if (err == -1) { 591 goto out; 592 } 593 err = local_set_xattr(buffer, credp); 594 if (err == -1) { 595 serrno = errno; 596 goto err_end; 597 } 598 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 599 600 buffer = rpath(fs_ctx, path); 601 err = mknod(buffer, SM_LOCAL_MODE_BITS|S_IFREG, 0); 602 if (err == -1) { 603 goto out; 604 } 605 err = local_set_mapped_file_attr(fs_ctx, path, credp); 606 if (err == -1) { 607 serrno = errno; 608 goto err_end; 609 } 610 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || 611 (fs_ctx->export_flags & V9FS_SM_NONE)) { 612 buffer = rpath(fs_ctx, path); 613 err = mknod(buffer, credp->fc_mode, credp->fc_rdev); 614 if (err == -1) { 615 goto out; 616 } 617 err = local_post_create_passthrough(fs_ctx, path, credp); 618 if (err == -1) { 619 serrno = errno; 620 goto err_end; 621 } 622 } 623 goto out; 624 625 err_end: 626 remove(buffer); 627 errno = serrno; 628 out: 629 g_free(buffer); 630 v9fs_string_free(&fullname); 631 return err; 632 } 633 634 static int local_mkdir(FsContext *fs_ctx, V9fsPath *dir_path, 635 const char *name, FsCred *credp) 636 { 637 char *path; 638 int err = -1; 639 int serrno = 0; 640 V9fsString fullname; 641 char *buffer = NULL; 642 643 v9fs_string_init(&fullname); 644 v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name); 645 path = fullname.data; 646 647 /* Determine the security model */ 648 if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 649 buffer = rpath(fs_ctx, path); 650 err = mkdir(buffer, SM_LOCAL_DIR_MODE_BITS); 651 if (err == -1) { 652 goto out; 653 } 654 credp->fc_mode = credp->fc_mode|S_IFDIR; 655 err = local_set_xattr(buffer, credp); 656 if (err == -1) { 657 serrno = errno; 658 goto err_end; 659 } 660 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 661 buffer = rpath(fs_ctx, path); 662 err = mkdir(buffer, SM_LOCAL_DIR_MODE_BITS); 663 if (err == -1) { 664 goto out; 665 } 666 credp->fc_mode = credp->fc_mode|S_IFDIR; 667 err = local_set_mapped_file_attr(fs_ctx, path, credp); 668 if (err == -1) { 669 serrno = errno; 670 goto err_end; 671 } 672 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || 673 (fs_ctx->export_flags & V9FS_SM_NONE)) { 674 buffer = rpath(fs_ctx, path); 675 err = mkdir(buffer, credp->fc_mode); 676 if (err == -1) { 677 goto out; 678 } 679 err = local_post_create_passthrough(fs_ctx, path, credp); 680 if (err == -1) { 681 serrno = errno; 682 goto err_end; 683 } 684 } 685 goto out; 686 687 err_end: 688 remove(buffer); 689 errno = serrno; 690 out: 691 g_free(buffer); 692 v9fs_string_free(&fullname); 693 return err; 694 } 695 696 static int local_fstat(FsContext *fs_ctx, int fid_type, 697 V9fsFidOpenState *fs, struct stat *stbuf) 698 { 699 int err, fd; 700 701 if (fid_type == P9_FID_DIR) { 702 fd = dirfd(fs->dir.stream); 703 } else { 704 fd = fs->fd; 705 } 706 707 err = fstat(fd, stbuf); 708 if (err) { 709 return err; 710 } 711 if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 712 /* Actual credentials are part of extended attrs */ 713 uid_t tmp_uid; 714 gid_t tmp_gid; 715 mode_t tmp_mode; 716 dev_t tmp_dev; 717 718 if (fgetxattr(fd, "user.virtfs.uid", &tmp_uid, sizeof(uid_t)) > 0) { 719 stbuf->st_uid = le32_to_cpu(tmp_uid); 720 } 721 if (fgetxattr(fd, "user.virtfs.gid", &tmp_gid, sizeof(gid_t)) > 0) { 722 stbuf->st_gid = le32_to_cpu(tmp_gid); 723 } 724 if (fgetxattr(fd, "user.virtfs.mode", &tmp_mode, sizeof(mode_t)) > 0) { 725 stbuf->st_mode = le32_to_cpu(tmp_mode); 726 } 727 if (fgetxattr(fd, "user.virtfs.rdev", &tmp_dev, sizeof(dev_t)) > 0) { 728 stbuf->st_rdev = le64_to_cpu(tmp_dev); 729 } 730 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 731 errno = EOPNOTSUPP; 732 return -1; 733 } 734 return err; 735 } 736 737 static int local_open2(FsContext *fs_ctx, V9fsPath *dir_path, const char *name, 738 int flags, FsCred *credp, V9fsFidOpenState *fs) 739 { 740 char *path; 741 int fd = -1; 742 int err = -1; 743 int serrno = 0; 744 V9fsString fullname; 745 char *buffer = NULL; 746 747 /* 748 * Mark all the open to not follow symlinks 749 */ 750 flags |= O_NOFOLLOW; 751 752 v9fs_string_init(&fullname); 753 v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name); 754 path = fullname.data; 755 756 /* Determine the security model */ 757 if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 758 buffer = rpath(fs_ctx, path); 759 fd = open(buffer, flags, SM_LOCAL_MODE_BITS); 760 if (fd == -1) { 761 err = fd; 762 goto out; 763 } 764 credp->fc_mode = credp->fc_mode|S_IFREG; 765 /* Set cleint credentials in xattr */ 766 err = local_set_xattr(buffer, credp); 767 if (err == -1) { 768 serrno = errno; 769 goto err_end; 770 } 771 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 772 buffer = rpath(fs_ctx, path); 773 fd = open(buffer, flags, SM_LOCAL_MODE_BITS); 774 if (fd == -1) { 775 err = fd; 776 goto out; 777 } 778 credp->fc_mode = credp->fc_mode|S_IFREG; 779 /* Set client credentials in .virtfs_metadata directory files */ 780 err = local_set_mapped_file_attr(fs_ctx, path, credp); 781 if (err == -1) { 782 serrno = errno; 783 goto err_end; 784 } 785 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || 786 (fs_ctx->export_flags & V9FS_SM_NONE)) { 787 buffer = rpath(fs_ctx, path); 788 fd = open(buffer, flags, credp->fc_mode); 789 if (fd == -1) { 790 err = fd; 791 goto out; 792 } 793 err = local_post_create_passthrough(fs_ctx, path, credp); 794 if (err == -1) { 795 serrno = errno; 796 goto err_end; 797 } 798 } 799 err = fd; 800 fs->fd = fd; 801 goto out; 802 803 err_end: 804 close(fd); 805 remove(buffer); 806 errno = serrno; 807 out: 808 g_free(buffer); 809 v9fs_string_free(&fullname); 810 return err; 811 } 812 813 814 static int local_symlink(FsContext *fs_ctx, const char *oldpath, 815 V9fsPath *dir_path, const char *name, FsCred *credp) 816 { 817 int err = -1; 818 int serrno = 0; 819 char *newpath; 820 V9fsString fullname; 821 char *buffer = NULL; 822 823 v9fs_string_init(&fullname); 824 v9fs_string_sprintf(&fullname, "%s/%s", dir_path->data, name); 825 newpath = fullname.data; 826 827 /* Determine the security model */ 828 if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 829 int fd; 830 ssize_t oldpath_size, write_size; 831 buffer = rpath(fs_ctx, newpath); 832 fd = open(buffer, O_CREAT|O_EXCL|O_RDWR|O_NOFOLLOW, SM_LOCAL_MODE_BITS); 833 if (fd == -1) { 834 err = fd; 835 goto out; 836 } 837 /* Write the oldpath (target) to the file. */ 838 oldpath_size = strlen(oldpath); 839 do { 840 write_size = write(fd, (void *)oldpath, oldpath_size); 841 } while (write_size == -1 && errno == EINTR); 842 843 if (write_size != oldpath_size) { 844 serrno = errno; 845 close(fd); 846 err = -1; 847 goto err_end; 848 } 849 close(fd); 850 /* Set cleint credentials in symlink's xattr */ 851 credp->fc_mode = credp->fc_mode|S_IFLNK; 852 err = local_set_xattr(buffer, credp); 853 if (err == -1) { 854 serrno = errno; 855 goto err_end; 856 } 857 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 858 int fd; 859 ssize_t oldpath_size, write_size; 860 buffer = rpath(fs_ctx, newpath); 861 fd = open(buffer, O_CREAT|O_EXCL|O_RDWR|O_NOFOLLOW, SM_LOCAL_MODE_BITS); 862 if (fd == -1) { 863 err = fd; 864 goto out; 865 } 866 /* Write the oldpath (target) to the file. */ 867 oldpath_size = strlen(oldpath); 868 do { 869 write_size = write(fd, (void *)oldpath, oldpath_size); 870 } while (write_size == -1 && errno == EINTR); 871 872 if (write_size != oldpath_size) { 873 serrno = errno; 874 close(fd); 875 err = -1; 876 goto err_end; 877 } 878 close(fd); 879 /* Set cleint credentials in symlink's xattr */ 880 credp->fc_mode = credp->fc_mode|S_IFLNK; 881 err = local_set_mapped_file_attr(fs_ctx, newpath, credp); 882 if (err == -1) { 883 serrno = errno; 884 goto err_end; 885 } 886 } else if ((fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || 887 (fs_ctx->export_flags & V9FS_SM_NONE)) { 888 buffer = rpath(fs_ctx, newpath); 889 err = symlink(oldpath, buffer); 890 if (err) { 891 goto out; 892 } 893 err = lchown(buffer, credp->fc_uid, credp->fc_gid); 894 if (err == -1) { 895 /* 896 * If we fail to change ownership and if we are 897 * using security model none. Ignore the error 898 */ 899 if ((fs_ctx->export_flags & V9FS_SEC_MASK) != V9FS_SM_NONE) { 900 serrno = errno; 901 goto err_end; 902 } else 903 err = 0; 904 } 905 } 906 goto out; 907 908 err_end: 909 remove(buffer); 910 errno = serrno; 911 out: 912 g_free(buffer); 913 v9fs_string_free(&fullname); 914 return err; 915 } 916 917 static int local_link(FsContext *ctx, V9fsPath *oldpath, 918 V9fsPath *dirpath, const char *name) 919 { 920 int ret; 921 V9fsString newpath; 922 char *buffer, *buffer1; 923 924 v9fs_string_init(&newpath); 925 v9fs_string_sprintf(&newpath, "%s/%s", dirpath->data, name); 926 927 buffer = rpath(ctx, oldpath->data); 928 buffer1 = rpath(ctx, newpath.data); 929 ret = link(buffer, buffer1); 930 g_free(buffer); 931 g_free(buffer1); 932 933 /* now link the virtfs_metadata files */ 934 if (!ret && (ctx->export_flags & V9FS_SM_MAPPED_FILE)) { 935 /* Link the .virtfs_metadata files. Create the metada directory */ 936 ret = local_create_mapped_attr_dir(ctx, newpath.data); 937 if (ret < 0) { 938 goto err_out; 939 } 940 buffer = local_mapped_attr_path(ctx, oldpath->data); 941 buffer1 = local_mapped_attr_path(ctx, newpath.data); 942 ret = link(buffer, buffer1); 943 g_free(buffer); 944 g_free(buffer1); 945 if (ret < 0 && errno != ENOENT) { 946 goto err_out; 947 } 948 } 949 err_out: 950 v9fs_string_free(&newpath); 951 return ret; 952 } 953 954 static int local_truncate(FsContext *ctx, V9fsPath *fs_path, off_t size) 955 { 956 int fd, ret; 957 958 fd = local_open_nofollow(ctx, fs_path->data, O_WRONLY, 0); 959 if (fd == -1) { 960 return -1; 961 } 962 ret = ftruncate(fd, size); 963 close_preserve_errno(fd); 964 return ret; 965 } 966 967 static int local_rename(FsContext *ctx, const char *oldpath, 968 const char *newpath) 969 { 970 int err; 971 char *buffer, *buffer1; 972 973 if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { 974 err = local_create_mapped_attr_dir(ctx, newpath); 975 if (err < 0) { 976 return err; 977 } 978 /* rename the .virtfs_metadata files */ 979 buffer = local_mapped_attr_path(ctx, oldpath); 980 buffer1 = local_mapped_attr_path(ctx, newpath); 981 err = rename(buffer, buffer1); 982 g_free(buffer); 983 g_free(buffer1); 984 if (err < 0 && errno != ENOENT) { 985 return err; 986 } 987 } 988 989 buffer = rpath(ctx, oldpath); 990 buffer1 = rpath(ctx, newpath); 991 err = rename(buffer, buffer1); 992 g_free(buffer); 993 g_free(buffer1); 994 return err; 995 } 996 997 static int local_chown(FsContext *fs_ctx, V9fsPath *fs_path, FsCred *credp) 998 { 999 char *buffer; 1000 int ret = -1; 1001 char *path = fs_path->data; 1002 1003 if ((credp->fc_uid == -1 && credp->fc_gid == -1) || 1004 (fs_ctx->export_flags & V9FS_SM_PASSTHROUGH) || 1005 (fs_ctx->export_flags & V9FS_SM_NONE)) { 1006 buffer = rpath(fs_ctx, path); 1007 ret = lchown(buffer, credp->fc_uid, credp->fc_gid); 1008 g_free(buffer); 1009 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED) { 1010 buffer = rpath(fs_ctx, path); 1011 ret = local_set_xattr(buffer, credp); 1012 g_free(buffer); 1013 } else if (fs_ctx->export_flags & V9FS_SM_MAPPED_FILE) { 1014 return local_set_mapped_file_attr(fs_ctx, path, credp); 1015 } 1016 return ret; 1017 } 1018 1019 static int local_utimensat(FsContext *s, V9fsPath *fs_path, 1020 const struct timespec *buf) 1021 { 1022 char *dirpath = g_path_get_dirname(fs_path->data); 1023 char *name = g_path_get_basename(fs_path->data); 1024 int dirfd, ret = -1; 1025 1026 dirfd = local_opendir_nofollow(s, dirpath); 1027 if (dirfd == -1) { 1028 goto out; 1029 } 1030 1031 ret = utimensat(dirfd, name, buf, AT_SYMLINK_NOFOLLOW); 1032 close_preserve_errno(dirfd); 1033 out: 1034 g_free(dirpath); 1035 g_free(name); 1036 return ret; 1037 } 1038 1039 static int local_unlinkat_common(FsContext *ctx, int dirfd, const char *name, 1040 int flags) 1041 { 1042 int ret = -1; 1043 1044 if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { 1045 int map_dirfd; 1046 1047 if (flags == AT_REMOVEDIR) { 1048 int fd; 1049 1050 fd = openat(dirfd, name, O_RDONLY | O_DIRECTORY | O_PATH); 1051 if (fd == -1) { 1052 goto err_out; 1053 } 1054 /* 1055 * If directory remove .virtfs_metadata contained in the 1056 * directory 1057 */ 1058 ret = unlinkat(fd, VIRTFS_META_DIR, AT_REMOVEDIR); 1059 close_preserve_errno(fd); 1060 if (ret < 0 && errno != ENOENT) { 1061 /* 1062 * We didn't had the .virtfs_metadata file. May be file created 1063 * in non-mapped mode ?. Ignore ENOENT. 1064 */ 1065 goto err_out; 1066 } 1067 } 1068 /* 1069 * Now remove the name from parent directory 1070 * .virtfs_metadata directory. 1071 */ 1072 map_dirfd = openat_dir(dirfd, VIRTFS_META_DIR); 1073 ret = unlinkat(map_dirfd, name, 0); 1074 close_preserve_errno(map_dirfd); 1075 if (ret < 0 && errno != ENOENT) { 1076 /* 1077 * We didn't had the .virtfs_metadata file. May be file created 1078 * in non-mapped mode ?. Ignore ENOENT. 1079 */ 1080 goto err_out; 1081 } 1082 } 1083 1084 ret = unlinkat(dirfd, name, flags); 1085 err_out: 1086 return ret; 1087 } 1088 1089 static int local_remove(FsContext *ctx, const char *path) 1090 { 1091 struct stat stbuf; 1092 char *dirpath = g_path_get_dirname(path); 1093 char *name = g_path_get_basename(path); 1094 int flags = 0; 1095 int dirfd; 1096 int err = -1; 1097 1098 dirfd = local_opendir_nofollow(ctx, dirpath); 1099 if (dirfd) { 1100 goto out; 1101 } 1102 1103 if (fstatat(dirfd, path, &stbuf, AT_SYMLINK_NOFOLLOW) < 0) { 1104 goto err_out; 1105 } 1106 1107 if (S_ISDIR(stbuf.st_mode)) { 1108 flags |= AT_REMOVEDIR; 1109 } 1110 1111 err = local_unlinkat_common(ctx, dirfd, name, flags); 1112 err_out: 1113 close_preserve_errno(dirfd); 1114 out: 1115 g_free(name); 1116 g_free(dirpath); 1117 return err; 1118 } 1119 1120 static int local_fsync(FsContext *ctx, int fid_type, 1121 V9fsFidOpenState *fs, int datasync) 1122 { 1123 int fd; 1124 1125 if (fid_type == P9_FID_DIR) { 1126 fd = dirfd(fs->dir.stream); 1127 } else { 1128 fd = fs->fd; 1129 } 1130 1131 if (datasync) { 1132 return qemu_fdatasync(fd); 1133 } else { 1134 return fsync(fd); 1135 } 1136 } 1137 1138 static int local_statfs(FsContext *s, V9fsPath *fs_path, struct statfs *stbuf) 1139 { 1140 int fd, ret; 1141 1142 fd = local_open_nofollow(s, fs_path->data, O_RDONLY, 0); 1143 ret = fstatfs(fd, stbuf); 1144 close_preserve_errno(fd); 1145 return ret; 1146 } 1147 1148 static ssize_t local_lgetxattr(FsContext *ctx, V9fsPath *fs_path, 1149 const char *name, void *value, size_t size) 1150 { 1151 char *path = fs_path->data; 1152 1153 return v9fs_get_xattr(ctx, path, name, value, size); 1154 } 1155 1156 static ssize_t local_llistxattr(FsContext *ctx, V9fsPath *fs_path, 1157 void *value, size_t size) 1158 { 1159 char *path = fs_path->data; 1160 1161 return v9fs_list_xattr(ctx, path, value, size); 1162 } 1163 1164 static int local_lsetxattr(FsContext *ctx, V9fsPath *fs_path, const char *name, 1165 void *value, size_t size, int flags) 1166 { 1167 char *path = fs_path->data; 1168 1169 return v9fs_set_xattr(ctx, path, name, value, size, flags); 1170 } 1171 1172 static int local_lremovexattr(FsContext *ctx, V9fsPath *fs_path, 1173 const char *name) 1174 { 1175 char *path = fs_path->data; 1176 1177 return v9fs_remove_xattr(ctx, path, name); 1178 } 1179 1180 static int local_name_to_path(FsContext *ctx, V9fsPath *dir_path, 1181 const char *name, V9fsPath *target) 1182 { 1183 if (dir_path) { 1184 v9fs_path_sprintf(target, "%s/%s", dir_path->data, name); 1185 } else { 1186 v9fs_path_sprintf(target, "%s", name); 1187 } 1188 return 0; 1189 } 1190 1191 static int local_renameat(FsContext *ctx, V9fsPath *olddir, 1192 const char *old_name, V9fsPath *newdir, 1193 const char *new_name) 1194 { 1195 int ret; 1196 int odirfd, ndirfd; 1197 1198 odirfd = local_opendir_nofollow(ctx, olddir->data); 1199 if (odirfd == -1) { 1200 return -1; 1201 } 1202 1203 ndirfd = local_opendir_nofollow(ctx, newdir->data); 1204 if (ndirfd == -1) { 1205 close_preserve_errno(odirfd); 1206 return -1; 1207 } 1208 1209 ret = renameat(odirfd, old_name, ndirfd, new_name); 1210 if (ret < 0) { 1211 goto out; 1212 } 1213 1214 if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { 1215 int omap_dirfd, nmap_dirfd; 1216 1217 ret = mkdirat(ndirfd, VIRTFS_META_DIR, 0700); 1218 if (ret < 0 && errno != EEXIST) { 1219 goto err_undo_rename; 1220 } 1221 1222 omap_dirfd = openat(odirfd, VIRTFS_META_DIR, 1223 O_RDONLY | O_DIRECTORY | O_NOFOLLOW); 1224 if (omap_dirfd == -1) { 1225 goto err; 1226 } 1227 1228 nmap_dirfd = openat(ndirfd, VIRTFS_META_DIR, 1229 O_RDONLY | O_DIRECTORY | O_NOFOLLOW); 1230 if (nmap_dirfd == -1) { 1231 close_preserve_errno(omap_dirfd); 1232 goto err; 1233 } 1234 1235 /* rename the .virtfs_metadata files */ 1236 ret = renameat(omap_dirfd, old_name, nmap_dirfd, new_name); 1237 close_preserve_errno(nmap_dirfd); 1238 close_preserve_errno(omap_dirfd); 1239 if (ret < 0 && errno != ENOENT) { 1240 goto err_undo_rename; 1241 } 1242 1243 ret = 0; 1244 } 1245 goto out; 1246 1247 err: 1248 ret = -1; 1249 err_undo_rename: 1250 renameat_preserve_errno(ndirfd, new_name, odirfd, old_name); 1251 out: 1252 close_preserve_errno(ndirfd); 1253 close_preserve_errno(odirfd); 1254 return ret; 1255 } 1256 1257 static int local_unlinkat(FsContext *ctx, V9fsPath *dir, 1258 const char *name, int flags) 1259 { 1260 int ret; 1261 int dirfd; 1262 1263 dirfd = local_opendir_nofollow(ctx, dir->data); 1264 if (dirfd == -1) { 1265 return -1; 1266 } 1267 1268 ret = local_unlinkat_common(ctx, dirfd, name, flags); 1269 close_preserve_errno(dirfd); 1270 return ret; 1271 } 1272 1273 static int local_ioc_getversion(FsContext *ctx, V9fsPath *path, 1274 mode_t st_mode, uint64_t *st_gen) 1275 { 1276 #ifdef FS_IOC_GETVERSION 1277 int err; 1278 V9fsFidOpenState fid_open; 1279 1280 /* 1281 * Do not try to open special files like device nodes, fifos etc 1282 * We can get fd for regular files and directories only 1283 */ 1284 if (!S_ISREG(st_mode) && !S_ISDIR(st_mode)) { 1285 errno = ENOTTY; 1286 return -1; 1287 } 1288 err = local_open(ctx, path, O_RDONLY, &fid_open); 1289 if (err < 0) { 1290 return err; 1291 } 1292 err = ioctl(fid_open.fd, FS_IOC_GETVERSION, st_gen); 1293 local_close(ctx, &fid_open); 1294 return err; 1295 #else 1296 errno = ENOTTY; 1297 return -1; 1298 #endif 1299 } 1300 1301 static int local_init(FsContext *ctx) 1302 { 1303 struct statfs stbuf; 1304 LocalData *data = g_malloc(sizeof(*data)); 1305 1306 data->mountfd = open(ctx->fs_root, O_DIRECTORY | O_RDONLY); 1307 if (data->mountfd == -1) { 1308 goto err; 1309 } 1310 1311 #ifdef FS_IOC_GETVERSION 1312 /* 1313 * use ioc_getversion only if the ioctl is definied 1314 */ 1315 if (fstatfs(data->mountfd, &stbuf) < 0) { 1316 close_preserve_errno(data->mountfd); 1317 goto err; 1318 } 1319 switch (stbuf.f_type) { 1320 case EXT2_SUPER_MAGIC: 1321 case BTRFS_SUPER_MAGIC: 1322 case REISERFS_SUPER_MAGIC: 1323 case XFS_SUPER_MAGIC: 1324 ctx->exops.get_st_gen = local_ioc_getversion; 1325 break; 1326 } 1327 #endif 1328 1329 if (ctx->export_flags & V9FS_SM_PASSTHROUGH) { 1330 ctx->xops = passthrough_xattr_ops; 1331 } else if (ctx->export_flags & V9FS_SM_MAPPED) { 1332 ctx->xops = mapped_xattr_ops; 1333 } else if (ctx->export_flags & V9FS_SM_NONE) { 1334 ctx->xops = none_xattr_ops; 1335 } else if (ctx->export_flags & V9FS_SM_MAPPED_FILE) { 1336 /* 1337 * xattr operation for mapped-file and passthrough 1338 * remain same. 1339 */ 1340 ctx->xops = passthrough_xattr_ops; 1341 } 1342 ctx->export_flags |= V9FS_PATHNAME_FSCONTEXT; 1343 1344 ctx->private = data; 1345 return 0; 1346 1347 err: 1348 g_free(data); 1349 return -1; 1350 } 1351 1352 static void local_cleanup(FsContext *ctx) 1353 { 1354 LocalData *data = ctx->private; 1355 1356 close(data->mountfd); 1357 g_free(data); 1358 } 1359 1360 static int local_parse_opts(QemuOpts *opts, struct FsDriverEntry *fse) 1361 { 1362 const char *sec_model = qemu_opt_get(opts, "security_model"); 1363 const char *path = qemu_opt_get(opts, "path"); 1364 1365 if (!sec_model) { 1366 error_report("Security model not specified, local fs needs security model"); 1367 error_printf("valid options are:" 1368 "\tsecurity_model=[passthrough|mapped-xattr|mapped-file|none]\n"); 1369 return -1; 1370 } 1371 1372 if (!strcmp(sec_model, "passthrough")) { 1373 fse->export_flags |= V9FS_SM_PASSTHROUGH; 1374 } else if (!strcmp(sec_model, "mapped") || 1375 !strcmp(sec_model, "mapped-xattr")) { 1376 fse->export_flags |= V9FS_SM_MAPPED; 1377 } else if (!strcmp(sec_model, "none")) { 1378 fse->export_flags |= V9FS_SM_NONE; 1379 } else if (!strcmp(sec_model, "mapped-file")) { 1380 fse->export_flags |= V9FS_SM_MAPPED_FILE; 1381 } else { 1382 error_report("Invalid security model %s specified", sec_model); 1383 error_printf("valid options are:" 1384 "\t[passthrough|mapped-xattr|mapped-file|none]\n"); 1385 return -1; 1386 } 1387 1388 if (!path) { 1389 error_report("fsdev: No path specified"); 1390 return -1; 1391 } 1392 fse->path = g_strdup(path); 1393 1394 return 0; 1395 } 1396 1397 FileOperations local_ops = { 1398 .parse_opts = local_parse_opts, 1399 .init = local_init, 1400 .cleanup = local_cleanup, 1401 .lstat = local_lstat, 1402 .readlink = local_readlink, 1403 .close = local_close, 1404 .closedir = local_closedir, 1405 .open = local_open, 1406 .opendir = local_opendir, 1407 .rewinddir = local_rewinddir, 1408 .telldir = local_telldir, 1409 .readdir = local_readdir, 1410 .seekdir = local_seekdir, 1411 .preadv = local_preadv, 1412 .pwritev = local_pwritev, 1413 .chmod = local_chmod, 1414 .mknod = local_mknod, 1415 .mkdir = local_mkdir, 1416 .fstat = local_fstat, 1417 .open2 = local_open2, 1418 .symlink = local_symlink, 1419 .link = local_link, 1420 .truncate = local_truncate, 1421 .rename = local_rename, 1422 .chown = local_chown, 1423 .utimensat = local_utimensat, 1424 .remove = local_remove, 1425 .fsync = local_fsync, 1426 .statfs = local_statfs, 1427 .lgetxattr = local_lgetxattr, 1428 .llistxattr = local_llistxattr, 1429 .lsetxattr = local_lsetxattr, 1430 .lremovexattr = local_lremovexattr, 1431 .name_to_path = local_name_to_path, 1432 .renameat = local_renameat, 1433 .unlinkat = local_unlinkat, 1434 }; 1435