Merge tag 'pull-loongarch-20241016' of https://gitlab.com/gaosong/qemu into staging
[qemu/armbru.git] / block / export / fuse.c
blob3307b640896d7d4f43a703ea88687723a4f1eb71
1 /*
2 * Present a block device as a raw image through FUSE
4 * Copyright (c) 2020 Max Reitz <mreitz@redhat.com>
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.
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.
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/>.
19 #define FUSE_USE_VERSION 31
21 #include "qemu/osdep.h"
22 #include "qemu/memalign.h"
23 #include "block/aio.h"
24 #include "block/block_int-common.h"
25 #include "block/export.h"
26 #include "block/fuse.h"
27 #include "block/qapi.h"
28 #include "qapi/error.h"
29 #include "qapi/qapi-commands-block.h"
30 #include "qemu/main-loop.h"
31 #include "sysemu/block-backend.h"
33 #include <fuse.h>
34 #include <fuse_lowlevel.h>
36 #if defined(CONFIG_FALLOCATE_ZERO_RANGE)
37 #include <linux/falloc.h>
38 #endif
40 #ifdef __linux__
41 #include <linux/fs.h>
42 #endif
44 /* Prevent overly long bounce buffer allocations */
45 #define FUSE_MAX_BOUNCE_BYTES (MIN(BDRV_REQUEST_MAX_BYTES, 64 * 1024 * 1024))
48 typedef struct FuseExport {
49 BlockExport common;
51 struct fuse_session *fuse_session;
52 struct fuse_buf fuse_buf;
53 unsigned int in_flight; /* atomic */
54 bool mounted, fd_handler_set_up;
56 char *mountpoint;
57 bool writable;
58 bool growable;
59 /* Whether allow_other was used as a mount option or not */
60 bool allow_other;
62 mode_t st_mode;
63 uid_t st_uid;
64 gid_t st_gid;
65 } FuseExport;
67 static GHashTable *exports;
68 static const struct fuse_lowlevel_ops fuse_ops;
70 static void fuse_export_shutdown(BlockExport *exp);
71 static void fuse_export_delete(BlockExport *exp);
73 static void init_exports_table(void);
75 static int setup_fuse_export(FuseExport *exp, const char *mountpoint,
76 bool allow_other, Error **errp);
77 static void read_from_fuse_export(void *opaque);
79 static bool is_regular_file(const char *path, Error **errp);
82 static void fuse_export_drained_begin(void *opaque)
84 FuseExport *exp = opaque;
86 aio_set_fd_handler(exp->common.ctx,
87 fuse_session_fd(exp->fuse_session),
88 NULL, NULL, NULL, NULL, NULL);
89 exp->fd_handler_set_up = false;
92 static void fuse_export_drained_end(void *opaque)
94 FuseExport *exp = opaque;
96 /* Refresh AioContext in case it changed */
97 exp->common.ctx = blk_get_aio_context(exp->common.blk);
99 aio_set_fd_handler(exp->common.ctx,
100 fuse_session_fd(exp->fuse_session),
101 read_from_fuse_export, NULL, NULL, NULL, exp);
102 exp->fd_handler_set_up = true;
105 static bool fuse_export_drained_poll(void *opaque)
107 FuseExport *exp = opaque;
109 return qatomic_read(&exp->in_flight) > 0;
112 static const BlockDevOps fuse_export_blk_dev_ops = {
113 .drained_begin = fuse_export_drained_begin,
114 .drained_end = fuse_export_drained_end,
115 .drained_poll = fuse_export_drained_poll,
118 static int fuse_export_create(BlockExport *blk_exp,
119 BlockExportOptions *blk_exp_args,
120 Error **errp)
122 FuseExport *exp = container_of(blk_exp, FuseExport, common);
123 BlockExportOptionsFuse *args = &blk_exp_args->u.fuse;
124 int ret;
126 assert(blk_exp_args->type == BLOCK_EXPORT_TYPE_FUSE);
128 /* For growable and writable exports, take the RESIZE permission */
129 if (args->growable || blk_exp_args->writable) {
130 uint64_t blk_perm, blk_shared_perm;
132 blk_get_perm(exp->common.blk, &blk_perm, &blk_shared_perm);
134 ret = blk_set_perm(exp->common.blk, blk_perm | BLK_PERM_RESIZE,
135 blk_shared_perm, errp);
136 if (ret < 0) {
137 return ret;
141 blk_set_dev_ops(exp->common.blk, &fuse_export_blk_dev_ops, exp);
144 * We handle draining ourselves using an in-flight counter and by disabling
145 * the FUSE fd handler. Do not queue BlockBackend requests, they need to
146 * complete so the in-flight counter reaches zero.
148 blk_set_disable_request_queuing(exp->common.blk, true);
150 init_exports_table();
153 * It is important to do this check before calling is_regular_file() --
154 * that function will do a stat(), which we would have to handle if we
155 * already exported something on @mountpoint. But we cannot, because
156 * we are currently caught up here.
157 * (Note that ideally we would want to resolve relative paths here,
158 * but bdrv_make_absolute_filename() might do the wrong thing for
159 * paths that contain colons, and realpath() would resolve symlinks,
160 * which we do not want: The mount point is not going to be the
161 * symlink's destination, but the link itself.)
162 * So this will not catch all potential clashes, but hopefully at
163 * least the most common one of specifying exactly the same path
164 * string twice.
166 if (g_hash_table_contains(exports, args->mountpoint)) {
167 error_setg(errp, "There already is a FUSE export on '%s'",
168 args->mountpoint);
169 ret = -EEXIST;
170 goto fail;
173 if (!is_regular_file(args->mountpoint, errp)) {
174 ret = -EINVAL;
175 goto fail;
178 exp->mountpoint = g_strdup(args->mountpoint);
179 exp->writable = blk_exp_args->writable;
180 exp->growable = args->growable;
182 /* set default */
183 if (!args->has_allow_other) {
184 args->allow_other = FUSE_EXPORT_ALLOW_OTHER_AUTO;
187 exp->st_mode = S_IFREG | S_IRUSR;
188 if (exp->writable) {
189 exp->st_mode |= S_IWUSR;
191 exp->st_uid = getuid();
192 exp->st_gid = getgid();
194 if (args->allow_other == FUSE_EXPORT_ALLOW_OTHER_AUTO) {
195 /* Ignore errors on our first attempt */
196 ret = setup_fuse_export(exp, args->mountpoint, true, NULL);
197 exp->allow_other = ret == 0;
198 if (ret < 0) {
199 ret = setup_fuse_export(exp, args->mountpoint, false, errp);
201 } else {
202 exp->allow_other = args->allow_other == FUSE_EXPORT_ALLOW_OTHER_ON;
203 ret = setup_fuse_export(exp, args->mountpoint, exp->allow_other, errp);
205 if (ret < 0) {
206 goto fail;
209 return 0;
211 fail:
212 fuse_export_delete(blk_exp);
213 return ret;
217 * Allocates the global @exports hash table.
219 static void init_exports_table(void)
221 if (exports) {
222 return;
225 exports = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
229 * Create exp->fuse_session and mount it.
231 static int setup_fuse_export(FuseExport *exp, const char *mountpoint,
232 bool allow_other, Error **errp)
234 const char *fuse_argv[4];
235 char *mount_opts;
236 struct fuse_args fuse_args;
237 int ret;
240 * max_read needs to match what fuse_init() sets.
241 * max_write need not be supplied.
243 mount_opts = g_strdup_printf("max_read=%zu,default_permissions%s",
244 FUSE_MAX_BOUNCE_BYTES,
245 allow_other ? ",allow_other" : "");
247 fuse_argv[0] = ""; /* Dummy program name */
248 fuse_argv[1] = "-o";
249 fuse_argv[2] = mount_opts;
250 fuse_argv[3] = NULL;
251 fuse_args = (struct fuse_args)FUSE_ARGS_INIT(3, (char **)fuse_argv);
253 exp->fuse_session = fuse_session_new(&fuse_args, &fuse_ops,
254 sizeof(fuse_ops), exp);
255 g_free(mount_opts);
256 if (!exp->fuse_session) {
257 error_setg(errp, "Failed to set up FUSE session");
258 ret = -EIO;
259 goto fail;
262 ret = fuse_session_mount(exp->fuse_session, mountpoint);
263 if (ret < 0) {
264 error_setg(errp, "Failed to mount FUSE session to export");
265 ret = -EIO;
266 goto fail;
268 exp->mounted = true;
270 g_hash_table_insert(exports, g_strdup(mountpoint), NULL);
272 aio_set_fd_handler(exp->common.ctx,
273 fuse_session_fd(exp->fuse_session),
274 read_from_fuse_export, NULL, NULL, NULL, exp);
275 exp->fd_handler_set_up = true;
277 return 0;
279 fail:
280 fuse_export_shutdown(&exp->common);
281 return ret;
285 * Callback to be invoked when the FUSE session FD can be read from.
286 * (This is basically the FUSE event loop.)
288 static void read_from_fuse_export(void *opaque)
290 FuseExport *exp = opaque;
291 int ret;
293 blk_exp_ref(&exp->common);
295 qatomic_inc(&exp->in_flight);
297 do {
298 ret = fuse_session_receive_buf(exp->fuse_session, &exp->fuse_buf);
299 } while (ret == -EINTR);
300 if (ret < 0) {
301 goto out;
304 fuse_session_process_buf(exp->fuse_session, &exp->fuse_buf);
306 out:
307 if (qatomic_fetch_dec(&exp->in_flight) == 1) {
308 aio_wait_kick(); /* wake AIO_WAIT_WHILE() */
311 blk_exp_unref(&exp->common);
314 static void fuse_export_shutdown(BlockExport *blk_exp)
316 FuseExport *exp = container_of(blk_exp, FuseExport, common);
318 if (exp->fuse_session) {
319 fuse_session_exit(exp->fuse_session);
321 if (exp->fd_handler_set_up) {
322 aio_set_fd_handler(exp->common.ctx,
323 fuse_session_fd(exp->fuse_session),
324 NULL, NULL, NULL, NULL, NULL);
325 exp->fd_handler_set_up = false;
329 if (exp->mountpoint) {
331 * Safe to drop now, because we will not handle any requests
332 * for this export anymore anyway.
334 g_hash_table_remove(exports, exp->mountpoint);
338 static void fuse_export_delete(BlockExport *blk_exp)
340 FuseExport *exp = container_of(blk_exp, FuseExport, common);
342 if (exp->fuse_session) {
343 if (exp->mounted) {
344 fuse_session_unmount(exp->fuse_session);
347 fuse_session_destroy(exp->fuse_session);
350 free(exp->fuse_buf.mem);
351 g_free(exp->mountpoint);
355 * Check whether @path points to a regular file. If not, put an
356 * appropriate message into *errp.
358 static bool is_regular_file(const char *path, Error **errp)
360 struct stat statbuf;
361 int ret;
363 ret = stat(path, &statbuf);
364 if (ret < 0) {
365 error_setg_errno(errp, errno, "Failed to stat '%s'", path);
366 return false;
369 if (!S_ISREG(statbuf.st_mode)) {
370 error_setg(errp, "'%s' is not a regular file", path);
371 return false;
374 return true;
378 * A chance to set change some parameters supplied to FUSE_INIT.
380 static void fuse_init(void *userdata, struct fuse_conn_info *conn)
383 * MIN_NON_ZERO() would not be wrong here, but what we set here
384 * must equal what has been passed to fuse_session_new().
385 * Therefore, as long as max_read must be passed as a mount option
386 * (which libfuse claims will be changed at some point), we have
387 * to set max_read to a fixed value here.
389 conn->max_read = FUSE_MAX_BOUNCE_BYTES;
391 conn->max_write = MIN_NON_ZERO(BDRV_REQUEST_MAX_BYTES, conn->max_write);
395 * Let clients look up files. Always return ENOENT because we only
396 * care about the mountpoint itself.
398 static void fuse_lookup(fuse_req_t req, fuse_ino_t parent, const char *name)
400 fuse_reply_err(req, ENOENT);
404 * Let clients get file attributes (i.e., stat() the file).
406 static void fuse_getattr(fuse_req_t req, fuse_ino_t inode,
407 struct fuse_file_info *fi)
409 struct stat statbuf;
410 int64_t length, allocated_blocks;
411 time_t now = time(NULL);
412 FuseExport *exp = fuse_req_userdata(req);
414 length = blk_getlength(exp->common.blk);
415 if (length < 0) {
416 fuse_reply_err(req, -length);
417 return;
420 allocated_blocks = bdrv_get_allocated_file_size(blk_bs(exp->common.blk));
421 if (allocated_blocks <= 0) {
422 allocated_blocks = DIV_ROUND_UP(length, 512);
423 } else {
424 allocated_blocks = DIV_ROUND_UP(allocated_blocks, 512);
427 statbuf = (struct stat) {
428 .st_ino = inode,
429 .st_mode = exp->st_mode,
430 .st_nlink = 1,
431 .st_uid = exp->st_uid,
432 .st_gid = exp->st_gid,
433 .st_size = length,
434 .st_blksize = blk_bs(exp->common.blk)->bl.request_alignment,
435 .st_blocks = allocated_blocks,
436 .st_atime = now,
437 .st_mtime = now,
438 .st_ctime = now,
441 fuse_reply_attr(req, &statbuf, 1.);
444 static int fuse_do_truncate(const FuseExport *exp, int64_t size,
445 bool req_zero_write, PreallocMode prealloc)
447 uint64_t blk_perm, blk_shared_perm;
448 BdrvRequestFlags truncate_flags = 0;
449 bool add_resize_perm;
450 int ret, ret_check;
452 /* Growable and writable exports have a permanent RESIZE permission */
453 add_resize_perm = !exp->growable && !exp->writable;
455 if (req_zero_write) {
456 truncate_flags |= BDRV_REQ_ZERO_WRITE;
459 if (add_resize_perm) {
461 if (!qemu_in_main_thread()) {
462 /* Changing permissions like below only works in the main thread */
463 return -EPERM;
466 blk_get_perm(exp->common.blk, &blk_perm, &blk_shared_perm);
468 ret = blk_set_perm(exp->common.blk, blk_perm | BLK_PERM_RESIZE,
469 blk_shared_perm, NULL);
470 if (ret < 0) {
471 return ret;
475 ret = blk_truncate(exp->common.blk, size, true, prealloc,
476 truncate_flags, NULL);
478 if (add_resize_perm) {
479 /* Must succeed, because we are only giving up the RESIZE permission */
480 ret_check = blk_set_perm(exp->common.blk, blk_perm,
481 blk_shared_perm, &error_abort);
482 assert(ret_check == 0);
485 return ret;
489 * Let clients set file attributes. Only resizing and changing
490 * permissions (st_mode, st_uid, st_gid) is allowed.
491 * Changing permissions is only allowed as far as it will actually
492 * permit access: Read-only exports cannot be given +w, and exports
493 * without allow_other cannot be given a different UID or GID, and
494 * they cannot be given non-owner access.
496 static void fuse_setattr(fuse_req_t req, fuse_ino_t inode, struct stat *statbuf,
497 int to_set, struct fuse_file_info *fi)
499 FuseExport *exp = fuse_req_userdata(req);
500 int supported_attrs;
501 int ret;
503 supported_attrs = FUSE_SET_ATTR_SIZE | FUSE_SET_ATTR_MODE;
504 if (exp->allow_other) {
505 supported_attrs |= FUSE_SET_ATTR_UID | FUSE_SET_ATTR_GID;
508 if (to_set & ~supported_attrs) {
509 fuse_reply_err(req, ENOTSUP);
510 return;
513 /* Do some argument checks first before committing to anything */
514 if (to_set & FUSE_SET_ATTR_MODE) {
516 * Without allow_other, non-owners can never access the export, so do
517 * not allow setting permissions for them
519 if (!exp->allow_other &&
520 (statbuf->st_mode & (S_IRWXG | S_IRWXO)) != 0)
522 fuse_reply_err(req, EPERM);
523 return;
526 /* +w for read-only exports makes no sense, disallow it */
527 if (!exp->writable &&
528 (statbuf->st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)) != 0)
530 fuse_reply_err(req, EROFS);
531 return;
535 if (to_set & FUSE_SET_ATTR_SIZE) {
536 if (!exp->writable) {
537 fuse_reply_err(req, EACCES);
538 return;
541 ret = fuse_do_truncate(exp, statbuf->st_size, true, PREALLOC_MODE_OFF);
542 if (ret < 0) {
543 fuse_reply_err(req, -ret);
544 return;
548 if (to_set & FUSE_SET_ATTR_MODE) {
549 /* Ignore FUSE-supplied file type, only change the mode */
550 exp->st_mode = (statbuf->st_mode & 07777) | S_IFREG;
553 if (to_set & FUSE_SET_ATTR_UID) {
554 exp->st_uid = statbuf->st_uid;
557 if (to_set & FUSE_SET_ATTR_GID) {
558 exp->st_gid = statbuf->st_gid;
561 fuse_getattr(req, inode, fi);
565 * Let clients open a file (i.e., the exported image).
567 static void fuse_open(fuse_req_t req, fuse_ino_t inode,
568 struct fuse_file_info *fi)
570 fuse_reply_open(req, fi);
574 * Handle client reads from the exported image.
576 static void fuse_read(fuse_req_t req, fuse_ino_t inode,
577 size_t size, off_t offset, struct fuse_file_info *fi)
579 FuseExport *exp = fuse_req_userdata(req);
580 int64_t length;
581 void *buf;
582 int ret;
584 /* Limited by max_read, should not happen */
585 if (size > FUSE_MAX_BOUNCE_BYTES) {
586 fuse_reply_err(req, EINVAL);
587 return;
591 * Clients will expect short reads at EOF, so we have to limit
592 * offset+size to the image length.
594 length = blk_getlength(exp->common.blk);
595 if (length < 0) {
596 fuse_reply_err(req, -length);
597 return;
600 if (offset + size > length) {
601 size = length - offset;
604 buf = qemu_try_blockalign(blk_bs(exp->common.blk), size);
605 if (!buf) {
606 fuse_reply_err(req, ENOMEM);
607 return;
610 ret = blk_pread(exp->common.blk, offset, size, buf, 0);
611 if (ret >= 0) {
612 fuse_reply_buf(req, buf, size);
613 } else {
614 fuse_reply_err(req, -ret);
617 qemu_vfree(buf);
621 * Handle client writes to the exported image.
623 static void fuse_write(fuse_req_t req, fuse_ino_t inode, const char *buf,
624 size_t size, off_t offset, struct fuse_file_info *fi)
626 FuseExport *exp = fuse_req_userdata(req);
627 int64_t length;
628 int ret;
630 /* Limited by max_write, should not happen */
631 if (size > BDRV_REQUEST_MAX_BYTES) {
632 fuse_reply_err(req, EINVAL);
633 return;
636 if (!exp->writable) {
637 fuse_reply_err(req, EACCES);
638 return;
642 * Clients will expect short writes at EOF, so we have to limit
643 * offset+size to the image length.
645 length = blk_getlength(exp->common.blk);
646 if (length < 0) {
647 fuse_reply_err(req, -length);
648 return;
651 if (offset + size > length) {
652 if (exp->growable) {
653 ret = fuse_do_truncate(exp, offset + size, true, PREALLOC_MODE_OFF);
654 if (ret < 0) {
655 fuse_reply_err(req, -ret);
656 return;
658 } else {
659 size = length - offset;
663 ret = blk_pwrite(exp->common.blk, offset, size, buf, 0);
664 if (ret >= 0) {
665 fuse_reply_write(req, size);
666 } else {
667 fuse_reply_err(req, -ret);
672 * Let clients perform various fallocate() operations.
674 static void fuse_fallocate(fuse_req_t req, fuse_ino_t inode, int mode,
675 off_t offset, off_t length,
676 struct fuse_file_info *fi)
678 FuseExport *exp = fuse_req_userdata(req);
679 int64_t blk_len;
680 int ret;
682 if (!exp->writable) {
683 fuse_reply_err(req, EACCES);
684 return;
687 blk_len = blk_getlength(exp->common.blk);
688 if (blk_len < 0) {
689 fuse_reply_err(req, -blk_len);
690 return;
693 #ifdef CONFIG_FALLOCATE_PUNCH_HOLE
694 if (mode & FALLOC_FL_KEEP_SIZE) {
695 length = MIN(length, blk_len - offset);
697 #endif /* CONFIG_FALLOCATE_PUNCH_HOLE */
699 if (!mode) {
700 /* We can only fallocate at the EOF with a truncate */
701 if (offset < blk_len) {
702 fuse_reply_err(req, EOPNOTSUPP);
703 return;
706 if (offset > blk_len) {
707 /* No preallocation needed here */
708 ret = fuse_do_truncate(exp, offset, true, PREALLOC_MODE_OFF);
709 if (ret < 0) {
710 fuse_reply_err(req, -ret);
711 return;
715 ret = fuse_do_truncate(exp, offset + length, true,
716 PREALLOC_MODE_FALLOC);
718 #ifdef CONFIG_FALLOCATE_PUNCH_HOLE
719 else if (mode & FALLOC_FL_PUNCH_HOLE) {
720 if (!(mode & FALLOC_FL_KEEP_SIZE)) {
721 fuse_reply_err(req, EINVAL);
722 return;
725 do {
726 int size = MIN(length, BDRV_REQUEST_MAX_BYTES);
728 ret = blk_pwrite_zeroes(exp->common.blk, offset, size,
729 BDRV_REQ_MAY_UNMAP | BDRV_REQ_NO_FALLBACK);
730 if (ret == -ENOTSUP) {
732 * fallocate() specifies to return EOPNOTSUPP for unsupported
733 * operations
735 ret = -EOPNOTSUPP;
738 offset += size;
739 length -= size;
740 } while (ret == 0 && length > 0);
742 #endif /* CONFIG_FALLOCATE_PUNCH_HOLE */
743 #ifdef CONFIG_FALLOCATE_ZERO_RANGE
744 else if (mode & FALLOC_FL_ZERO_RANGE) {
745 if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + length > blk_len) {
746 /* No need for zeroes, we are going to write them ourselves */
747 ret = fuse_do_truncate(exp, offset + length, false,
748 PREALLOC_MODE_OFF);
749 if (ret < 0) {
750 fuse_reply_err(req, -ret);
751 return;
755 do {
756 int size = MIN(length, BDRV_REQUEST_MAX_BYTES);
758 ret = blk_pwrite_zeroes(exp->common.blk,
759 offset, size, 0);
760 offset += size;
761 length -= size;
762 } while (ret == 0 && length > 0);
764 #endif /* CONFIG_FALLOCATE_ZERO_RANGE */
765 else {
766 ret = -EOPNOTSUPP;
769 fuse_reply_err(req, ret < 0 ? -ret : 0);
773 * Let clients fsync the exported image.
775 static void fuse_fsync(fuse_req_t req, fuse_ino_t inode, int datasync,
776 struct fuse_file_info *fi)
778 FuseExport *exp = fuse_req_userdata(req);
779 int ret;
781 ret = blk_flush(exp->common.blk);
782 fuse_reply_err(req, ret < 0 ? -ret : 0);
786 * Called before an FD to the exported image is closed. (libfuse
787 * notes this to be a way to return last-minute errors.)
789 static void fuse_flush(fuse_req_t req, fuse_ino_t inode,
790 struct fuse_file_info *fi)
792 fuse_fsync(req, inode, 1, fi);
795 #ifdef CONFIG_FUSE_LSEEK
797 * Let clients inquire allocation status.
799 static void fuse_lseek(fuse_req_t req, fuse_ino_t inode, off_t offset,
800 int whence, struct fuse_file_info *fi)
802 FuseExport *exp = fuse_req_userdata(req);
804 if (whence != SEEK_HOLE && whence != SEEK_DATA) {
805 fuse_reply_err(req, EINVAL);
806 return;
809 while (true) {
810 int64_t pnum;
811 int ret;
813 ret = bdrv_block_status_above(blk_bs(exp->common.blk), NULL,
814 offset, INT64_MAX, &pnum, NULL, NULL);
815 if (ret < 0) {
816 fuse_reply_err(req, -ret);
817 return;
820 if (!pnum && (ret & BDRV_BLOCK_EOF)) {
821 int64_t blk_len;
824 * If blk_getlength() rounds (e.g. by sectors), then the
825 * export length will be rounded, too. However,
826 * bdrv_block_status_above() may return EOF at unaligned
827 * offsets. We must not let this become visible and thus
828 * always simulate a hole between @offset (the real EOF)
829 * and @blk_len (the client-visible EOF).
832 blk_len = blk_getlength(exp->common.blk);
833 if (blk_len < 0) {
834 fuse_reply_err(req, -blk_len);
835 return;
838 if (offset > blk_len || whence == SEEK_DATA) {
839 fuse_reply_err(req, ENXIO);
840 } else {
841 fuse_reply_lseek(req, offset);
843 return;
846 if (ret & BDRV_BLOCK_DATA) {
847 if (whence == SEEK_DATA) {
848 fuse_reply_lseek(req, offset);
849 return;
851 } else {
852 if (whence == SEEK_HOLE) {
853 fuse_reply_lseek(req, offset);
854 return;
858 /* Safety check against infinite loops */
859 if (!pnum) {
860 fuse_reply_err(req, ENXIO);
861 return;
864 offset += pnum;
867 #endif
869 static const struct fuse_lowlevel_ops fuse_ops = {
870 .init = fuse_init,
871 .lookup = fuse_lookup,
872 .getattr = fuse_getattr,
873 .setattr = fuse_setattr,
874 .open = fuse_open,
875 .read = fuse_read,
876 .write = fuse_write,
877 .fallocate = fuse_fallocate,
878 .flush = fuse_flush,
879 .fsync = fuse_fsync,
880 #ifdef CONFIG_FUSE_LSEEK
881 .lseek = fuse_lseek,
882 #endif
885 const BlockExportDriver blk_exp_fuse = {
886 .type = BLOCK_EXPORT_TYPE_FUSE,
887 .instance_size = sizeof(FuseExport),
888 .create = fuse_export_create,
889 .delete = fuse_export_delete,
890 .request_shutdown = fuse_export_shutdown,