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 "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"
32 #include <fuse_lowlevel.h>
35 /* Prevent overly long bounce buffer allocations */
36 #define FUSE_MAX_BOUNCE_BYTES (MIN(BDRV_REQUEST_MAX_BYTES, 64 * 1024 * 1024))
39 typedef struct FuseExport
{
42 struct fuse_session
*fuse_session
;
43 struct fuse_buf fuse_buf
;
44 bool mounted
, fd_handler_set_up
;
51 static GHashTable
*exports
;
52 static const struct fuse_lowlevel_ops fuse_ops
;
54 static void fuse_export_shutdown(BlockExport
*exp
);
55 static void fuse_export_delete(BlockExport
*exp
);
57 static void init_exports_table(void);
59 static int setup_fuse_export(FuseExport
*exp
, const char *mountpoint
,
61 static void read_from_fuse_export(void *opaque
);
63 static bool is_regular_file(const char *path
, Error
**errp
);
66 static int fuse_export_create(BlockExport
*blk_exp
,
67 BlockExportOptions
*blk_exp_args
,
70 FuseExport
*exp
= container_of(blk_exp
, FuseExport
, common
);
71 BlockExportOptionsFuse
*args
= &blk_exp_args
->u
.fuse
;
74 assert(blk_exp_args
->type
== BLOCK_EXPORT_TYPE_FUSE
);
76 /* For growable exports, take the RESIZE permission */
78 uint64_t blk_perm
, blk_shared_perm
;
80 blk_get_perm(exp
->common
.blk
, &blk_perm
, &blk_shared_perm
);
82 ret
= blk_set_perm(exp
->common
.blk
, blk_perm
| BLK_PERM_RESIZE
,
83 blk_shared_perm
, errp
);
92 * It is important to do this check before calling is_regular_file() --
93 * that function will do a stat(), which we would have to handle if we
94 * already exported something on @mountpoint. But we cannot, because
95 * we are currently caught up here.
96 * (Note that ideally we would want to resolve relative paths here,
97 * but bdrv_make_absolute_filename() might do the wrong thing for
98 * paths that contain colons, and realpath() would resolve symlinks,
99 * which we do not want: The mount point is not going to be the
100 * symlink's destination, but the link itself.)
101 * So this will not catch all potential clashes, but hopefully at
102 * least the most common one of specifying exactly the same path
105 if (g_hash_table_contains(exports
, args
->mountpoint
)) {
106 error_setg(errp
, "There already is a FUSE export on '%s'",
112 if (!is_regular_file(args
->mountpoint
, errp
)) {
117 exp
->mountpoint
= g_strdup(args
->mountpoint
);
118 exp
->writable
= blk_exp_args
->writable
;
119 exp
->growable
= args
->growable
;
121 ret
= setup_fuse_export(exp
, args
->mountpoint
, errp
);
129 fuse_export_delete(blk_exp
);
134 * Allocates the global @exports hash table.
136 static void init_exports_table(void)
142 exports
= g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
, NULL
);
146 * Create exp->fuse_session and mount it.
148 static int setup_fuse_export(FuseExport
*exp
, const char *mountpoint
,
151 const char *fuse_argv
[4];
153 struct fuse_args fuse_args
;
156 /* Needs to match what fuse_init() sets. Only max_read must be supplied. */
157 mount_opts
= g_strdup_printf("max_read=%zu", FUSE_MAX_BOUNCE_BYTES
);
159 fuse_argv
[0] = ""; /* Dummy program name */
161 fuse_argv
[2] = mount_opts
;
163 fuse_args
= (struct fuse_args
)FUSE_ARGS_INIT(3, (char **)fuse_argv
);
165 exp
->fuse_session
= fuse_session_new(&fuse_args
, &fuse_ops
,
166 sizeof(fuse_ops
), exp
);
168 if (!exp
->fuse_session
) {
169 error_setg(errp
, "Failed to set up FUSE session");
174 ret
= fuse_session_mount(exp
->fuse_session
, mountpoint
);
176 error_setg(errp
, "Failed to mount FUSE session to export");
182 g_hash_table_insert(exports
, g_strdup(mountpoint
), NULL
);
184 aio_set_fd_handler(exp
->common
.ctx
,
185 fuse_session_fd(exp
->fuse_session
), true,
186 read_from_fuse_export
, NULL
, NULL
, exp
);
187 exp
->fd_handler_set_up
= true;
192 fuse_export_shutdown(&exp
->common
);
197 * Callback to be invoked when the FUSE session FD can be read from.
198 * (This is basically the FUSE event loop.)
200 static void read_from_fuse_export(void *opaque
)
202 FuseExport
*exp
= opaque
;
205 blk_exp_ref(&exp
->common
);
208 ret
= fuse_session_receive_buf(exp
->fuse_session
, &exp
->fuse_buf
);
209 } while (ret
== -EINTR
);
214 fuse_session_process_buf(exp
->fuse_session
, &exp
->fuse_buf
);
217 blk_exp_unref(&exp
->common
);
220 static void fuse_export_shutdown(BlockExport
*blk_exp
)
222 FuseExport
*exp
= container_of(blk_exp
, FuseExport
, common
);
224 if (exp
->fuse_session
) {
225 fuse_session_exit(exp
->fuse_session
);
227 if (exp
->fd_handler_set_up
) {
228 aio_set_fd_handler(exp
->common
.ctx
,
229 fuse_session_fd(exp
->fuse_session
), true,
230 NULL
, NULL
, NULL
, NULL
);
231 exp
->fd_handler_set_up
= false;
235 if (exp
->mountpoint
) {
237 * Safe to drop now, because we will not handle any requests
238 * for this export anymore anyway.
240 g_hash_table_remove(exports
, exp
->mountpoint
);
244 static void fuse_export_delete(BlockExport
*blk_exp
)
246 FuseExport
*exp
= container_of(blk_exp
, FuseExport
, common
);
248 if (exp
->fuse_session
) {
250 fuse_session_unmount(exp
->fuse_session
);
253 fuse_session_destroy(exp
->fuse_session
);
256 free(exp
->fuse_buf
.mem
);
257 g_free(exp
->mountpoint
);
261 * Check whether @path points to a regular file. If not, put an
262 * appropriate message into *errp.
264 static bool is_regular_file(const char *path
, Error
**errp
)
269 ret
= stat(path
, &statbuf
);
271 error_setg_errno(errp
, errno
, "Failed to stat '%s'", path
);
275 if (!S_ISREG(statbuf
.st_mode
)) {
276 error_setg(errp
, "'%s' is not a regular file", path
);
284 * A chance to set change some parameters supplied to FUSE_INIT.
286 static void fuse_init(void *userdata
, struct fuse_conn_info
*conn
)
289 * MIN_NON_ZERO() would not be wrong here, but what we set here
290 * must equal what has been passed to fuse_session_new().
291 * Therefore, as long as max_read must be passed as a mount option
292 * (which libfuse claims will be changed at some point), we have
293 * to set max_read to a fixed value here.
295 conn
->max_read
= FUSE_MAX_BOUNCE_BYTES
;
297 conn
->max_write
= MIN_NON_ZERO(BDRV_REQUEST_MAX_BYTES
, conn
->max_write
);
301 * Let clients look up files. Always return ENOENT because we only
302 * care about the mountpoint itself.
304 static void fuse_lookup(fuse_req_t req
, fuse_ino_t parent
, const char *name
)
306 fuse_reply_err(req
, ENOENT
);
310 * Let clients get file attributes (i.e., stat() the file).
312 static void fuse_getattr(fuse_req_t req
, fuse_ino_t inode
,
313 struct fuse_file_info
*fi
)
316 int64_t length
, allocated_blocks
;
317 time_t now
= time(NULL
);
318 FuseExport
*exp
= fuse_req_userdata(req
);
321 length
= blk_getlength(exp
->common
.blk
);
323 fuse_reply_err(req
, -length
);
327 allocated_blocks
= bdrv_get_allocated_file_size(blk_bs(exp
->common
.blk
));
328 if (allocated_blocks
<= 0) {
329 allocated_blocks
= DIV_ROUND_UP(length
, 512);
331 allocated_blocks
= DIV_ROUND_UP(allocated_blocks
, 512);
334 mode
= S_IFREG
| S_IRUSR
;
339 statbuf
= (struct stat
) {
346 .st_blksize
= blk_bs(exp
->common
.blk
)->bl
.request_alignment
,
347 .st_blocks
= allocated_blocks
,
353 fuse_reply_attr(req
, &statbuf
, 1.);
356 static int fuse_do_truncate(const FuseExport
*exp
, int64_t size
,
357 bool req_zero_write
, PreallocMode prealloc
)
359 uint64_t blk_perm
, blk_shared_perm
;
360 BdrvRequestFlags truncate_flags
= 0;
363 if (req_zero_write
) {
364 truncate_flags
|= BDRV_REQ_ZERO_WRITE
;
367 /* Growable exports have a permanent RESIZE permission */
368 if (!exp
->growable
) {
369 blk_get_perm(exp
->common
.blk
, &blk_perm
, &blk_shared_perm
);
371 ret
= blk_set_perm(exp
->common
.blk
, blk_perm
| BLK_PERM_RESIZE
,
372 blk_shared_perm
, NULL
);
378 ret
= blk_truncate(exp
->common
.blk
, size
, true, prealloc
,
379 truncate_flags
, NULL
);
381 if (!exp
->growable
) {
382 /* Must succeed, because we are only giving up the RESIZE permission */
383 blk_set_perm(exp
->common
.blk
, blk_perm
, blk_shared_perm
, &error_abort
);
390 * Let clients set file attributes. Only resizing is supported.
392 static void fuse_setattr(fuse_req_t req
, fuse_ino_t inode
, struct stat
*statbuf
,
393 int to_set
, struct fuse_file_info
*fi
)
395 FuseExport
*exp
= fuse_req_userdata(req
);
398 if (!exp
->writable
) {
399 fuse_reply_err(req
, EACCES
);
403 if (to_set
& ~FUSE_SET_ATTR_SIZE
) {
404 fuse_reply_err(req
, ENOTSUP
);
408 ret
= fuse_do_truncate(exp
, statbuf
->st_size
, true, PREALLOC_MODE_OFF
);
410 fuse_reply_err(req
, -ret
);
414 fuse_getattr(req
, inode
, fi
);
418 * Let clients open a file (i.e., the exported image).
420 static void fuse_open(fuse_req_t req
, fuse_ino_t inode
,
421 struct fuse_file_info
*fi
)
423 fuse_reply_open(req
, fi
);
427 * Handle client reads from the exported image.
429 static void fuse_read(fuse_req_t req
, fuse_ino_t inode
,
430 size_t size
, off_t offset
, struct fuse_file_info
*fi
)
432 FuseExport
*exp
= fuse_req_userdata(req
);
437 /* Limited by max_read, should not happen */
438 if (size
> FUSE_MAX_BOUNCE_BYTES
) {
439 fuse_reply_err(req
, EINVAL
);
444 * Clients will expect short reads at EOF, so we have to limit
445 * offset+size to the image length.
447 length
= blk_getlength(exp
->common
.blk
);
449 fuse_reply_err(req
, -length
);
453 if (offset
+ size
> length
) {
454 size
= length
- offset
;
457 buf
= qemu_try_blockalign(blk_bs(exp
->common
.blk
), size
);
459 fuse_reply_err(req
, ENOMEM
);
463 ret
= blk_pread(exp
->common
.blk
, offset
, buf
, size
);
465 fuse_reply_buf(req
, buf
, size
);
467 fuse_reply_err(req
, -ret
);
474 * Handle client writes to the exported image.
476 static void fuse_write(fuse_req_t req
, fuse_ino_t inode
, const char *buf
,
477 size_t size
, off_t offset
, struct fuse_file_info
*fi
)
479 FuseExport
*exp
= fuse_req_userdata(req
);
483 /* Limited by max_write, should not happen */
484 if (size
> BDRV_REQUEST_MAX_BYTES
) {
485 fuse_reply_err(req
, EINVAL
);
489 if (!exp
->writable
) {
490 fuse_reply_err(req
, EACCES
);
495 * Clients will expect short writes at EOF, so we have to limit
496 * offset+size to the image length.
498 length
= blk_getlength(exp
->common
.blk
);
500 fuse_reply_err(req
, -length
);
504 if (offset
+ size
> length
) {
506 ret
= fuse_do_truncate(exp
, offset
+ size
, true, PREALLOC_MODE_OFF
);
508 fuse_reply_err(req
, -ret
);
512 size
= length
- offset
;
516 ret
= blk_pwrite(exp
->common
.blk
, offset
, buf
, size
, 0);
518 fuse_reply_write(req
, size
);
520 fuse_reply_err(req
, -ret
);
525 * Let clients perform various fallocate() operations.
527 static void fuse_fallocate(fuse_req_t req
, fuse_ino_t inode
, int mode
,
528 off_t offset
, off_t length
,
529 struct fuse_file_info
*fi
)
531 FuseExport
*exp
= fuse_req_userdata(req
);
535 if (!exp
->writable
) {
536 fuse_reply_err(req
, EACCES
);
540 blk_len
= blk_getlength(exp
->common
.blk
);
542 fuse_reply_err(req
, -blk_len
);
546 if (mode
& FALLOC_FL_KEEP_SIZE
) {
547 length
= MIN(length
, blk_len
- offset
);
550 if (mode
& FALLOC_FL_PUNCH_HOLE
) {
551 if (!(mode
& FALLOC_FL_KEEP_SIZE
)) {
552 fuse_reply_err(req
, EINVAL
);
557 int size
= MIN(length
, BDRV_REQUEST_MAX_BYTES
);
559 ret
= blk_pdiscard(exp
->common
.blk
, offset
, size
);
562 } while (ret
== 0 && length
> 0);
563 } else if (mode
& FALLOC_FL_ZERO_RANGE
) {
564 if (!(mode
& FALLOC_FL_KEEP_SIZE
) && offset
+ length
> blk_len
) {
565 /* No need for zeroes, we are going to write them ourselves */
566 ret
= fuse_do_truncate(exp
, offset
+ length
, false,
569 fuse_reply_err(req
, -ret
);
575 int size
= MIN(length
, BDRV_REQUEST_MAX_BYTES
);
577 ret
= blk_pwrite_zeroes(exp
->common
.blk
,
581 } while (ret
== 0 && length
> 0);
583 /* We can only fallocate at the EOF with a truncate */
584 if (offset
< blk_len
) {
585 fuse_reply_err(req
, EOPNOTSUPP
);
589 if (offset
> blk_len
) {
590 /* No preallocation needed here */
591 ret
= fuse_do_truncate(exp
, offset
, true, PREALLOC_MODE_OFF
);
593 fuse_reply_err(req
, -ret
);
598 ret
= fuse_do_truncate(exp
, offset
+ length
, true,
599 PREALLOC_MODE_FALLOC
);
604 fuse_reply_err(req
, ret
< 0 ? -ret
: 0);
608 * Let clients fsync the exported image.
610 static void fuse_fsync(fuse_req_t req
, fuse_ino_t inode
, int datasync
,
611 struct fuse_file_info
*fi
)
613 FuseExport
*exp
= fuse_req_userdata(req
);
616 ret
= blk_flush(exp
->common
.blk
);
617 fuse_reply_err(req
, ret
< 0 ? -ret
: 0);
621 * Called before an FD to the exported image is closed. (libfuse
622 * notes this to be a way to return last-minute errors.)
624 static void fuse_flush(fuse_req_t req
, fuse_ino_t inode
,
625 struct fuse_file_info
*fi
)
627 fuse_fsync(req
, inode
, 1, fi
);
630 #ifdef CONFIG_FUSE_LSEEK
632 * Let clients inquire allocation status.
634 static void fuse_lseek(fuse_req_t req
, fuse_ino_t inode
, off_t offset
,
635 int whence
, struct fuse_file_info
*fi
)
637 FuseExport
*exp
= fuse_req_userdata(req
);
639 if (whence
!= SEEK_HOLE
&& whence
!= SEEK_DATA
) {
640 fuse_reply_err(req
, EINVAL
);
648 ret
= bdrv_block_status_above(blk_bs(exp
->common
.blk
), NULL
,
649 offset
, INT64_MAX
, &pnum
, NULL
, NULL
);
651 fuse_reply_err(req
, -ret
);
655 if (!pnum
&& (ret
& BDRV_BLOCK_EOF
)) {
659 * If blk_getlength() rounds (e.g. by sectors), then the
660 * export length will be rounded, too. However,
661 * bdrv_block_status_above() may return EOF at unaligned
662 * offsets. We must not let this become visible and thus
663 * always simulate a hole between @offset (the real EOF)
664 * and @blk_len (the client-visible EOF).
667 blk_len
= blk_getlength(exp
->common
.blk
);
669 fuse_reply_err(req
, -blk_len
);
673 if (offset
> blk_len
|| whence
== SEEK_DATA
) {
674 fuse_reply_err(req
, ENXIO
);
676 fuse_reply_lseek(req
, offset
);
681 if (ret
& BDRV_BLOCK_DATA
) {
682 if (whence
== SEEK_DATA
) {
683 fuse_reply_lseek(req
, offset
);
687 if (whence
== SEEK_HOLE
) {
688 fuse_reply_lseek(req
, offset
);
693 /* Safety check against infinite loops */
695 fuse_reply_err(req
, ENXIO
);
704 static const struct fuse_lowlevel_ops fuse_ops
= {
706 .lookup
= fuse_lookup
,
707 .getattr
= fuse_getattr
,
708 .setattr
= fuse_setattr
,
712 .fallocate
= fuse_fallocate
,
715 #ifdef CONFIG_FUSE_LSEEK
720 const BlockExportDriver blk_exp_fuse
= {
721 .type
= BLOCK_EXPORT_TYPE_FUSE
,
722 .instance_size
= sizeof(FuseExport
),
723 .create
= fuse_export_create
,
724 .delete = fuse_export_delete
,
725 .request_shutdown
= fuse_export_shutdown
,