1 /* 2 * Present a block device as a raw image through FUSE 3 * 4 * Copyright (c) 2020 Max Reitz <mreitz@redhat.com> 5 * 6 * This program is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; under version 2 or later of the License. 9 * 10 * This program is distributed in the hope that it will be useful, 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 * GNU General Public License for more details. 14 * 15 * You should have received a copy of the GNU General Public License 16 * along with this program; if not, see <http://www.gnu.org/licenses/>. 17 */ 18 19 #define FUSE_USE_VERSION 31 20 21 #include "qemu/osdep.h" 22 #include "block/aio.h" 23 #include "block/block.h" 24 #include "block/export.h" 25 #include "block/fuse.h" 26 #include "block/qapi.h" 27 #include "qapi/error.h" 28 #include "qapi/qapi-commands-block.h" 29 #include "sysemu/block-backend.h" 30 31 #include <fuse.h> 32 #include <fuse_lowlevel.h> 33 34 #if defined(CONFIG_FALLOCATE_ZERO_RANGE) 35 #include <linux/falloc.h> 36 #endif 37 38 #ifdef __linux__ 39 #include <linux/fs.h> 40 #endif 41 42 /* Prevent overly long bounce buffer allocations */ 43 #define FUSE_MAX_BOUNCE_BYTES (MIN(BDRV_REQUEST_MAX_BYTES, 64 * 1024 * 1024)) 44 45 46 typedef struct FuseExport { 47 BlockExport common; 48 49 struct fuse_session *fuse_session; 50 struct fuse_buf fuse_buf; 51 bool mounted, fd_handler_set_up; 52 53 char *mountpoint; 54 bool writable; 55 bool growable; 56 /* Whether allow_other was used as a mount option or not */ 57 bool allow_other; 58 59 mode_t st_mode; 60 uid_t st_uid; 61 gid_t st_gid; 62 } FuseExport; 63 64 static GHashTable *exports; 65 static const struct fuse_lowlevel_ops fuse_ops; 66 67 static void fuse_export_shutdown(BlockExport *exp); 68 static void fuse_export_delete(BlockExport *exp); 69 70 static void init_exports_table(void); 71 72 static int setup_fuse_export(FuseExport *exp, const char *mountpoint, 73 bool allow_other, Error **errp); 74 static void read_from_fuse_export(void *opaque); 75 76 static bool is_regular_file(const char *path, Error **errp); 77 78 79 static int fuse_export_create(BlockExport *blk_exp, 80 BlockExportOptions *blk_exp_args, 81 Error **errp) 82 { 83 FuseExport *exp = container_of(blk_exp, FuseExport, common); 84 BlockExportOptionsFuse *args = &blk_exp_args->u.fuse; 85 int ret; 86 87 assert(blk_exp_args->type == BLOCK_EXPORT_TYPE_FUSE); 88 89 /* For growable and writable exports, take the RESIZE permission */ 90 if (args->growable || blk_exp_args->writable) { 91 uint64_t blk_perm, blk_shared_perm; 92 93 blk_get_perm(exp->common.blk, &blk_perm, &blk_shared_perm); 94 95 ret = blk_set_perm(exp->common.blk, blk_perm | BLK_PERM_RESIZE, 96 blk_shared_perm, errp); 97 if (ret < 0) { 98 return ret; 99 } 100 } 101 102 init_exports_table(); 103 104 /* 105 * It is important to do this check before calling is_regular_file() -- 106 * that function will do a stat(), which we would have to handle if we 107 * already exported something on @mountpoint. But we cannot, because 108 * we are currently caught up here. 109 * (Note that ideally we would want to resolve relative paths here, 110 * but bdrv_make_absolute_filename() might do the wrong thing for 111 * paths that contain colons, and realpath() would resolve symlinks, 112 * which we do not want: The mount point is not going to be the 113 * symlink's destination, but the link itself.) 114 * So this will not catch all potential clashes, but hopefully at 115 * least the most common one of specifying exactly the same path 116 * string twice. 117 */ 118 if (g_hash_table_contains(exports, args->mountpoint)) { 119 error_setg(errp, "There already is a FUSE export on '%s'", 120 args->mountpoint); 121 ret = -EEXIST; 122 goto fail; 123 } 124 125 if (!is_regular_file(args->mountpoint, errp)) { 126 ret = -EINVAL; 127 goto fail; 128 } 129 130 exp->mountpoint = g_strdup(args->mountpoint); 131 exp->writable = blk_exp_args->writable; 132 exp->growable = args->growable; 133 134 /* set default */ 135 if (!args->has_allow_other) { 136 args->allow_other = FUSE_EXPORT_ALLOW_OTHER_AUTO; 137 } 138 139 exp->st_mode = S_IFREG | S_IRUSR; 140 if (exp->writable) { 141 exp->st_mode |= S_IWUSR; 142 } 143 exp->st_uid = getuid(); 144 exp->st_gid = getgid(); 145 146 if (args->allow_other == FUSE_EXPORT_ALLOW_OTHER_AUTO) { 147 /* Ignore errors on our first attempt */ 148 ret = setup_fuse_export(exp, args->mountpoint, true, NULL); 149 exp->allow_other = ret == 0; 150 if (ret < 0) { 151 ret = setup_fuse_export(exp, args->mountpoint, false, errp); 152 } 153 } else { 154 exp->allow_other = args->allow_other == FUSE_EXPORT_ALLOW_OTHER_ON; 155 ret = setup_fuse_export(exp, args->mountpoint, exp->allow_other, errp); 156 } 157 if (ret < 0) { 158 goto fail; 159 } 160 161 return 0; 162 163 fail: 164 fuse_export_delete(blk_exp); 165 return ret; 166 } 167 168 /** 169 * Allocates the global @exports hash table. 170 */ 171 static void init_exports_table(void) 172 { 173 if (exports) { 174 return; 175 } 176 177 exports = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL); 178 } 179 180 /** 181 * Create exp->fuse_session and mount it. 182 */ 183 static int setup_fuse_export(FuseExport *exp, const char *mountpoint, 184 bool allow_other, Error **errp) 185 { 186 const char *fuse_argv[4]; 187 char *mount_opts; 188 struct fuse_args fuse_args; 189 int ret; 190 191 /* 192 * max_read needs to match what fuse_init() sets. 193 * max_write need not be supplied. 194 */ 195 mount_opts = g_strdup_printf("max_read=%zu,default_permissions%s", 196 FUSE_MAX_BOUNCE_BYTES, 197 allow_other ? ",allow_other" : ""); 198 199 fuse_argv[0] = ""; /* Dummy program name */ 200 fuse_argv[1] = "-o"; 201 fuse_argv[2] = mount_opts; 202 fuse_argv[3] = NULL; 203 fuse_args = (struct fuse_args)FUSE_ARGS_INIT(3, (char **)fuse_argv); 204 205 exp->fuse_session = fuse_session_new(&fuse_args, &fuse_ops, 206 sizeof(fuse_ops), exp); 207 g_free(mount_opts); 208 if (!exp->fuse_session) { 209 error_setg(errp, "Failed to set up FUSE session"); 210 ret = -EIO; 211 goto fail; 212 } 213 214 ret = fuse_session_mount(exp->fuse_session, mountpoint); 215 if (ret < 0) { 216 error_setg(errp, "Failed to mount FUSE session to export"); 217 ret = -EIO; 218 goto fail; 219 } 220 exp->mounted = true; 221 222 g_hash_table_insert(exports, g_strdup(mountpoint), NULL); 223 224 aio_set_fd_handler(exp->common.ctx, 225 fuse_session_fd(exp->fuse_session), true, 226 read_from_fuse_export, NULL, NULL, NULL, exp); 227 exp->fd_handler_set_up = true; 228 229 return 0; 230 231 fail: 232 fuse_export_shutdown(&exp->common); 233 return ret; 234 } 235 236 /** 237 * Callback to be invoked when the FUSE session FD can be read from. 238 * (This is basically the FUSE event loop.) 239 */ 240 static void read_from_fuse_export(void *opaque) 241 { 242 FuseExport *exp = opaque; 243 int ret; 244 245 blk_exp_ref(&exp->common); 246 247 do { 248 ret = fuse_session_receive_buf(exp->fuse_session, &exp->fuse_buf); 249 } while (ret == -EINTR); 250 if (ret < 0) { 251 goto out; 252 } 253 254 fuse_session_process_buf(exp->fuse_session, &exp->fuse_buf); 255 256 out: 257 blk_exp_unref(&exp->common); 258 } 259 260 static void fuse_export_shutdown(BlockExport *blk_exp) 261 { 262 FuseExport *exp = container_of(blk_exp, FuseExport, common); 263 264 if (exp->fuse_session) { 265 fuse_session_exit(exp->fuse_session); 266 267 if (exp->fd_handler_set_up) { 268 aio_set_fd_handler(exp->common.ctx, 269 fuse_session_fd(exp->fuse_session), true, 270 NULL, NULL, NULL, NULL, NULL); 271 exp->fd_handler_set_up = false; 272 } 273 } 274 275 if (exp->mountpoint) { 276 /* 277 * Safe to drop now, because we will not handle any requests 278 * for this export anymore anyway. 279 */ 280 g_hash_table_remove(exports, exp->mountpoint); 281 } 282 } 283 284 static void fuse_export_delete(BlockExport *blk_exp) 285 { 286 FuseExport *exp = container_of(blk_exp, FuseExport, common); 287 288 if (exp->fuse_session) { 289 if (exp->mounted) { 290 fuse_session_unmount(exp->fuse_session); 291 } 292 293 fuse_session_destroy(exp->fuse_session); 294 } 295 296 free(exp->fuse_buf.mem); 297 g_free(exp->mountpoint); 298 } 299 300 /** 301 * Check whether @path points to a regular file. If not, put an 302 * appropriate message into *errp. 303 */ 304 static bool is_regular_file(const char *path, Error **errp) 305 { 306 struct stat statbuf; 307 int ret; 308 309 ret = stat(path, &statbuf); 310 if (ret < 0) { 311 error_setg_errno(errp, errno, "Failed to stat '%s'", path); 312 return false; 313 } 314 315 if (!S_ISREG(statbuf.st_mode)) { 316 error_setg(errp, "'%s' is not a regular file", path); 317 return false; 318 } 319 320 return true; 321 } 322 323 /** 324 * A chance to set change some parameters supplied to FUSE_INIT. 325 */ 326 static void fuse_init(void *userdata, struct fuse_conn_info *conn) 327 { 328 /* 329 * MIN_NON_ZERO() would not be wrong here, but what we set here 330 * must equal what has been passed to fuse_session_new(). 331 * Therefore, as long as max_read must be passed as a mount option 332 * (which libfuse claims will be changed at some point), we have 333 * to set max_read to a fixed value here. 334 */ 335 conn->max_read = FUSE_MAX_BOUNCE_BYTES; 336 337 conn->max_write = MIN_NON_ZERO(BDRV_REQUEST_MAX_BYTES, conn->max_write); 338 } 339 340 /** 341 * Let clients look up files. Always return ENOENT because we only 342 * care about the mountpoint itself. 343 */ 344 static void fuse_lookup(fuse_req_t req, fuse_ino_t parent, const char *name) 345 { 346 fuse_reply_err(req, ENOENT); 347 } 348 349 /** 350 * Let clients get file attributes (i.e., stat() the file). 351 */ 352 static void fuse_getattr(fuse_req_t req, fuse_ino_t inode, 353 struct fuse_file_info *fi) 354 { 355 struct stat statbuf; 356 int64_t length, allocated_blocks; 357 time_t now = time(NULL); 358 FuseExport *exp = fuse_req_userdata(req); 359 360 length = blk_getlength(exp->common.blk); 361 if (length < 0) { 362 fuse_reply_err(req, -length); 363 return; 364 } 365 366 allocated_blocks = bdrv_get_allocated_file_size(blk_bs(exp->common.blk)); 367 if (allocated_blocks <= 0) { 368 allocated_blocks = DIV_ROUND_UP(length, 512); 369 } else { 370 allocated_blocks = DIV_ROUND_UP(allocated_blocks, 512); 371 } 372 373 statbuf = (struct stat) { 374 .st_ino = inode, 375 .st_mode = exp->st_mode, 376 .st_nlink = 1, 377 .st_uid = exp->st_uid, 378 .st_gid = exp->st_gid, 379 .st_size = length, 380 .st_blksize = blk_bs(exp->common.blk)->bl.request_alignment, 381 .st_blocks = allocated_blocks, 382 .st_atime = now, 383 .st_mtime = now, 384 .st_ctime = now, 385 }; 386 387 fuse_reply_attr(req, &statbuf, 1.); 388 } 389 390 static int fuse_do_truncate(const FuseExport *exp, int64_t size, 391 bool req_zero_write, PreallocMode prealloc) 392 { 393 uint64_t blk_perm, blk_shared_perm; 394 BdrvRequestFlags truncate_flags = 0; 395 bool add_resize_perm; 396 int ret, ret_check; 397 398 /* Growable and writable exports have a permanent RESIZE permission */ 399 add_resize_perm = !exp->growable && !exp->writable; 400 401 if (req_zero_write) { 402 truncate_flags |= BDRV_REQ_ZERO_WRITE; 403 } 404 405 if (add_resize_perm) { 406 407 if (!qemu_in_main_thread()) { 408 /* Changing permissions like below only works in the main thread */ 409 return -EPERM; 410 } 411 412 blk_get_perm(exp->common.blk, &blk_perm, &blk_shared_perm); 413 414 ret = blk_set_perm(exp->common.blk, blk_perm | BLK_PERM_RESIZE, 415 blk_shared_perm, NULL); 416 if (ret < 0) { 417 return ret; 418 } 419 } 420 421 ret = blk_truncate(exp->common.blk, size, true, prealloc, 422 truncate_flags, NULL); 423 424 if (add_resize_perm) { 425 /* Must succeed, because we are only giving up the RESIZE permission */ 426 ret_check = blk_set_perm(exp->common.blk, blk_perm, 427 blk_shared_perm, &error_abort); 428 assert(ret_check == 0); 429 } 430 431 return ret; 432 } 433 434 /** 435 * Let clients set file attributes. Only resizing and changing 436 * permissions (st_mode, st_uid, st_gid) is allowed. 437 * Changing permissions is only allowed as far as it will actually 438 * permit access: Read-only exports cannot be given +w, and exports 439 * without allow_other cannot be given a different UID or GID, and 440 * they cannot be given non-owner access. 441 */ 442 static void fuse_setattr(fuse_req_t req, fuse_ino_t inode, struct stat *statbuf, 443 int to_set, struct fuse_file_info *fi) 444 { 445 FuseExport *exp = fuse_req_userdata(req); 446 int supported_attrs; 447 int ret; 448 449 supported_attrs = FUSE_SET_ATTR_SIZE | FUSE_SET_ATTR_MODE; 450 if (exp->allow_other) { 451 supported_attrs |= FUSE_SET_ATTR_UID | FUSE_SET_ATTR_GID; 452 } 453 454 if (to_set & ~supported_attrs) { 455 fuse_reply_err(req, ENOTSUP); 456 return; 457 } 458 459 /* Do some argument checks first before committing to anything */ 460 if (to_set & FUSE_SET_ATTR_MODE) { 461 /* 462 * Without allow_other, non-owners can never access the export, so do 463 * not allow setting permissions for them 464 */ 465 if (!exp->allow_other && 466 (statbuf->st_mode & (S_IRWXG | S_IRWXO)) != 0) 467 { 468 fuse_reply_err(req, EPERM); 469 return; 470 } 471 472 /* +w for read-only exports makes no sense, disallow it */ 473 if (!exp->writable && 474 (statbuf->st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)) != 0) 475 { 476 fuse_reply_err(req, EROFS); 477 return; 478 } 479 } 480 481 if (to_set & FUSE_SET_ATTR_SIZE) { 482 if (!exp->writable) { 483 fuse_reply_err(req, EACCES); 484 return; 485 } 486 487 ret = fuse_do_truncate(exp, statbuf->st_size, true, PREALLOC_MODE_OFF); 488 if (ret < 0) { 489 fuse_reply_err(req, -ret); 490 return; 491 } 492 } 493 494 if (to_set & FUSE_SET_ATTR_MODE) { 495 /* Ignore FUSE-supplied file type, only change the mode */ 496 exp->st_mode = (statbuf->st_mode & 07777) | S_IFREG; 497 } 498 499 if (to_set & FUSE_SET_ATTR_UID) { 500 exp->st_uid = statbuf->st_uid; 501 } 502 503 if (to_set & FUSE_SET_ATTR_GID) { 504 exp->st_gid = statbuf->st_gid; 505 } 506 507 fuse_getattr(req, inode, fi); 508 } 509 510 /** 511 * Let clients open a file (i.e., the exported image). 512 */ 513 static void fuse_open(fuse_req_t req, fuse_ino_t inode, 514 struct fuse_file_info *fi) 515 { 516 fuse_reply_open(req, fi); 517 } 518 519 /** 520 * Handle client reads from the exported image. 521 */ 522 static void fuse_read(fuse_req_t req, fuse_ino_t inode, 523 size_t size, off_t offset, struct fuse_file_info *fi) 524 { 525 FuseExport *exp = fuse_req_userdata(req); 526 int64_t length; 527 void *buf; 528 int ret; 529 530 /* Limited by max_read, should not happen */ 531 if (size > FUSE_MAX_BOUNCE_BYTES) { 532 fuse_reply_err(req, EINVAL); 533 return; 534 } 535 536 /** 537 * Clients will expect short reads at EOF, so we have to limit 538 * offset+size to the image length. 539 */ 540 length = blk_getlength(exp->common.blk); 541 if (length < 0) { 542 fuse_reply_err(req, -length); 543 return; 544 } 545 546 if (offset + size > length) { 547 size = length - offset; 548 } 549 550 buf = qemu_try_blockalign(blk_bs(exp->common.blk), size); 551 if (!buf) { 552 fuse_reply_err(req, ENOMEM); 553 return; 554 } 555 556 ret = blk_pread(exp->common.blk, offset, buf, size); 557 if (ret >= 0) { 558 fuse_reply_buf(req, buf, size); 559 } else { 560 fuse_reply_err(req, -ret); 561 } 562 563 qemu_vfree(buf); 564 } 565 566 /** 567 * Handle client writes to the exported image. 568 */ 569 static void fuse_write(fuse_req_t req, fuse_ino_t inode, const char *buf, 570 size_t size, off_t offset, struct fuse_file_info *fi) 571 { 572 FuseExport *exp = fuse_req_userdata(req); 573 int64_t length; 574 int ret; 575 576 /* Limited by max_write, should not happen */ 577 if (size > BDRV_REQUEST_MAX_BYTES) { 578 fuse_reply_err(req, EINVAL); 579 return; 580 } 581 582 if (!exp->writable) { 583 fuse_reply_err(req, EACCES); 584 return; 585 } 586 587 /** 588 * Clients will expect short writes at EOF, so we have to limit 589 * offset+size to the image length. 590 */ 591 length = blk_getlength(exp->common.blk); 592 if (length < 0) { 593 fuse_reply_err(req, -length); 594 return; 595 } 596 597 if (offset + size > length) { 598 if (exp->growable) { 599 ret = fuse_do_truncate(exp, offset + size, true, PREALLOC_MODE_OFF); 600 if (ret < 0) { 601 fuse_reply_err(req, -ret); 602 return; 603 } 604 } else { 605 size = length - offset; 606 } 607 } 608 609 ret = blk_pwrite(exp->common.blk, offset, buf, size, 0); 610 if (ret >= 0) { 611 fuse_reply_write(req, size); 612 } else { 613 fuse_reply_err(req, -ret); 614 } 615 } 616 617 /** 618 * Let clients perform various fallocate() operations. 619 */ 620 static void fuse_fallocate(fuse_req_t req, fuse_ino_t inode, int mode, 621 off_t offset, off_t length, 622 struct fuse_file_info *fi) 623 { 624 FuseExport *exp = fuse_req_userdata(req); 625 int64_t blk_len; 626 int ret; 627 628 if (!exp->writable) { 629 fuse_reply_err(req, EACCES); 630 return; 631 } 632 633 blk_len = blk_getlength(exp->common.blk); 634 if (blk_len < 0) { 635 fuse_reply_err(req, -blk_len); 636 return; 637 } 638 639 #ifdef CONFIG_FALLOCATE_PUNCH_HOLE 640 if (mode & FALLOC_FL_KEEP_SIZE) { 641 length = MIN(length, blk_len - offset); 642 } 643 #endif /* CONFIG_FALLOCATE_PUNCH_HOLE */ 644 645 if (!mode) { 646 /* We can only fallocate at the EOF with a truncate */ 647 if (offset < blk_len) { 648 fuse_reply_err(req, EOPNOTSUPP); 649 return; 650 } 651 652 if (offset > blk_len) { 653 /* No preallocation needed here */ 654 ret = fuse_do_truncate(exp, offset, true, PREALLOC_MODE_OFF); 655 if (ret < 0) { 656 fuse_reply_err(req, -ret); 657 return; 658 } 659 } 660 661 ret = fuse_do_truncate(exp, offset + length, true, 662 PREALLOC_MODE_FALLOC); 663 } 664 #ifdef CONFIG_FALLOCATE_PUNCH_HOLE 665 else if (mode & FALLOC_FL_PUNCH_HOLE) { 666 if (!(mode & FALLOC_FL_KEEP_SIZE)) { 667 fuse_reply_err(req, EINVAL); 668 return; 669 } 670 671 do { 672 int size = MIN(length, BDRV_REQUEST_MAX_BYTES); 673 674 ret = blk_pdiscard(exp->common.blk, offset, size); 675 offset += size; 676 length -= size; 677 } while (ret == 0 && length > 0); 678 } 679 #endif /* CONFIG_FALLOCATE_PUNCH_HOLE */ 680 #ifdef CONFIG_FALLOCATE_ZERO_RANGE 681 else if (mode & FALLOC_FL_ZERO_RANGE) { 682 if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + length > blk_len) { 683 /* No need for zeroes, we are going to write them ourselves */ 684 ret = fuse_do_truncate(exp, offset + length, false, 685 PREALLOC_MODE_OFF); 686 if (ret < 0) { 687 fuse_reply_err(req, -ret); 688 return; 689 } 690 } 691 692 do { 693 int size = MIN(length, BDRV_REQUEST_MAX_BYTES); 694 695 ret = blk_pwrite_zeroes(exp->common.blk, 696 offset, size, 0); 697 offset += size; 698 length -= size; 699 } while (ret == 0 && length > 0); 700 } 701 #endif /* CONFIG_FALLOCATE_ZERO_RANGE */ 702 else { 703 ret = -EOPNOTSUPP; 704 } 705 706 fuse_reply_err(req, ret < 0 ? -ret : 0); 707 } 708 709 /** 710 * Let clients fsync the exported image. 711 */ 712 static void fuse_fsync(fuse_req_t req, fuse_ino_t inode, int datasync, 713 struct fuse_file_info *fi) 714 { 715 FuseExport *exp = fuse_req_userdata(req); 716 int ret; 717 718 ret = blk_flush(exp->common.blk); 719 fuse_reply_err(req, ret < 0 ? -ret : 0); 720 } 721 722 /** 723 * Called before an FD to the exported image is closed. (libfuse 724 * notes this to be a way to return last-minute errors.) 725 */ 726 static void fuse_flush(fuse_req_t req, fuse_ino_t inode, 727 struct fuse_file_info *fi) 728 { 729 fuse_fsync(req, inode, 1, fi); 730 } 731 732 #ifdef CONFIG_FUSE_LSEEK 733 /** 734 * Let clients inquire allocation status. 735 */ 736 static void fuse_lseek(fuse_req_t req, fuse_ino_t inode, off_t offset, 737 int whence, struct fuse_file_info *fi) 738 { 739 FuseExport *exp = fuse_req_userdata(req); 740 741 if (whence != SEEK_HOLE && whence != SEEK_DATA) { 742 fuse_reply_err(req, EINVAL); 743 return; 744 } 745 746 while (true) { 747 int64_t pnum; 748 int ret; 749 750 ret = bdrv_block_status_above(blk_bs(exp->common.blk), NULL, 751 offset, INT64_MAX, &pnum, NULL, NULL); 752 if (ret < 0) { 753 fuse_reply_err(req, -ret); 754 return; 755 } 756 757 if (!pnum && (ret & BDRV_BLOCK_EOF)) { 758 int64_t blk_len; 759 760 /* 761 * If blk_getlength() rounds (e.g. by sectors), then the 762 * export length will be rounded, too. However, 763 * bdrv_block_status_above() may return EOF at unaligned 764 * offsets. We must not let this become visible and thus 765 * always simulate a hole between @offset (the real EOF) 766 * and @blk_len (the client-visible EOF). 767 */ 768 769 blk_len = blk_getlength(exp->common.blk); 770 if (blk_len < 0) { 771 fuse_reply_err(req, -blk_len); 772 return; 773 } 774 775 if (offset > blk_len || whence == SEEK_DATA) { 776 fuse_reply_err(req, ENXIO); 777 } else { 778 fuse_reply_lseek(req, offset); 779 } 780 return; 781 } 782 783 if (ret & BDRV_BLOCK_DATA) { 784 if (whence == SEEK_DATA) { 785 fuse_reply_lseek(req, offset); 786 return; 787 } 788 } else { 789 if (whence == SEEK_HOLE) { 790 fuse_reply_lseek(req, offset); 791 return; 792 } 793 } 794 795 /* Safety check against infinite loops */ 796 if (!pnum) { 797 fuse_reply_err(req, ENXIO); 798 return; 799 } 800 801 offset += pnum; 802 } 803 } 804 #endif 805 806 static const struct fuse_lowlevel_ops fuse_ops = { 807 .init = fuse_init, 808 .lookup = fuse_lookup, 809 .getattr = fuse_getattr, 810 .setattr = fuse_setattr, 811 .open = fuse_open, 812 .read = fuse_read, 813 .write = fuse_write, 814 .fallocate = fuse_fallocate, 815 .flush = fuse_flush, 816 .fsync = fuse_fsync, 817 #ifdef CONFIG_FUSE_LSEEK 818 .lseek = fuse_lseek, 819 #endif 820 }; 821 822 const BlockExportDriver blk_exp_fuse = { 823 .type = BLOCK_EXPORT_TYPE_FUSE, 824 .instance_size = sizeof(FuseExport), 825 .create = fuse_export_create, 826 .delete = fuse_export_delete, 827 .request_shutdown = fuse_export_shutdown, 828 }; 829