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
;
49 /* Whether allow_other was used as a mount option or not */
57 static GHashTable
*exports
;
58 static const struct fuse_lowlevel_ops fuse_ops
;
60 static void fuse_export_shutdown(BlockExport
*exp
);
61 static void fuse_export_delete(BlockExport
*exp
);
63 static void init_exports_table(void);
65 static int setup_fuse_export(FuseExport
*exp
, const char *mountpoint
,
66 bool allow_other
, Error
**errp
);
67 static void read_from_fuse_export(void *opaque
);
69 static bool is_regular_file(const char *path
, Error
**errp
);
72 static int fuse_export_create(BlockExport
*blk_exp
,
73 BlockExportOptions
*blk_exp_args
,
76 FuseExport
*exp
= container_of(blk_exp
, FuseExport
, common
);
77 BlockExportOptionsFuse
*args
= &blk_exp_args
->u
.fuse
;
80 assert(blk_exp_args
->type
== BLOCK_EXPORT_TYPE_FUSE
);
82 /* For growable exports, take the RESIZE permission */
84 uint64_t blk_perm
, blk_shared_perm
;
86 blk_get_perm(exp
->common
.blk
, &blk_perm
, &blk_shared_perm
);
88 ret
= blk_set_perm(exp
->common
.blk
, blk_perm
| BLK_PERM_RESIZE
,
89 blk_shared_perm
, errp
);
98 * It is important to do this check before calling is_regular_file() --
99 * that function will do a stat(), which we would have to handle if we
100 * already exported something on @mountpoint. But we cannot, because
101 * we are currently caught up here.
102 * (Note that ideally we would want to resolve relative paths here,
103 * but bdrv_make_absolute_filename() might do the wrong thing for
104 * paths that contain colons, and realpath() would resolve symlinks,
105 * which we do not want: The mount point is not going to be the
106 * symlink's destination, but the link itself.)
107 * So this will not catch all potential clashes, but hopefully at
108 * least the most common one of specifying exactly the same path
111 if (g_hash_table_contains(exports
, args
->mountpoint
)) {
112 error_setg(errp
, "There already is a FUSE export on '%s'",
118 if (!is_regular_file(args
->mountpoint
, errp
)) {
123 exp
->mountpoint
= g_strdup(args
->mountpoint
);
124 exp
->writable
= blk_exp_args
->writable
;
125 exp
->growable
= args
->growable
;
128 if (!args
->has_allow_other
) {
129 args
->allow_other
= FUSE_EXPORT_ALLOW_OTHER_AUTO
;
132 exp
->st_mode
= S_IFREG
| S_IRUSR
;
134 exp
->st_mode
|= S_IWUSR
;
136 exp
->st_uid
= getuid();
137 exp
->st_gid
= getgid();
139 if (args
->allow_other
== FUSE_EXPORT_ALLOW_OTHER_AUTO
) {
140 /* Ignore errors on our first attempt */
141 ret
= setup_fuse_export(exp
, args
->mountpoint
, true, NULL
);
142 exp
->allow_other
= ret
== 0;
144 ret
= setup_fuse_export(exp
, args
->mountpoint
, false, errp
);
147 exp
->allow_other
= args
->allow_other
== FUSE_EXPORT_ALLOW_OTHER_ON
;
148 ret
= setup_fuse_export(exp
, args
->mountpoint
, exp
->allow_other
, errp
);
157 fuse_export_delete(blk_exp
);
162 * Allocates the global @exports hash table.
164 static void init_exports_table(void)
170 exports
= g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
, NULL
);
174 * Create exp->fuse_session and mount it.
176 static int setup_fuse_export(FuseExport
*exp
, const char *mountpoint
,
177 bool allow_other
, Error
**errp
)
179 const char *fuse_argv
[4];
181 struct fuse_args fuse_args
;
185 * max_read needs to match what fuse_init() sets.
186 * max_write need not be supplied.
188 mount_opts
= g_strdup_printf("max_read=%zu,default_permissions%s",
189 FUSE_MAX_BOUNCE_BYTES
,
190 allow_other
? ",allow_other" : "");
192 fuse_argv
[0] = ""; /* Dummy program name */
194 fuse_argv
[2] = mount_opts
;
196 fuse_args
= (struct fuse_args
)FUSE_ARGS_INIT(3, (char **)fuse_argv
);
198 exp
->fuse_session
= fuse_session_new(&fuse_args
, &fuse_ops
,
199 sizeof(fuse_ops
), exp
);
201 if (!exp
->fuse_session
) {
202 error_setg(errp
, "Failed to set up FUSE session");
207 ret
= fuse_session_mount(exp
->fuse_session
, mountpoint
);
209 error_setg(errp
, "Failed to mount FUSE session to export");
215 g_hash_table_insert(exports
, g_strdup(mountpoint
), NULL
);
217 aio_set_fd_handler(exp
->common
.ctx
,
218 fuse_session_fd(exp
->fuse_session
), true,
219 read_from_fuse_export
, NULL
, NULL
, exp
);
220 exp
->fd_handler_set_up
= true;
225 fuse_export_shutdown(&exp
->common
);
230 * Callback to be invoked when the FUSE session FD can be read from.
231 * (This is basically the FUSE event loop.)
233 static void read_from_fuse_export(void *opaque
)
235 FuseExport
*exp
= opaque
;
238 blk_exp_ref(&exp
->common
);
241 ret
= fuse_session_receive_buf(exp
->fuse_session
, &exp
->fuse_buf
);
242 } while (ret
== -EINTR
);
247 fuse_session_process_buf(exp
->fuse_session
, &exp
->fuse_buf
);
250 blk_exp_unref(&exp
->common
);
253 static void fuse_export_shutdown(BlockExport
*blk_exp
)
255 FuseExport
*exp
= container_of(blk_exp
, FuseExport
, common
);
257 if (exp
->fuse_session
) {
258 fuse_session_exit(exp
->fuse_session
);
260 if (exp
->fd_handler_set_up
) {
261 aio_set_fd_handler(exp
->common
.ctx
,
262 fuse_session_fd(exp
->fuse_session
), true,
263 NULL
, NULL
, NULL
, NULL
);
264 exp
->fd_handler_set_up
= false;
268 if (exp
->mountpoint
) {
270 * Safe to drop now, because we will not handle any requests
271 * for this export anymore anyway.
273 g_hash_table_remove(exports
, exp
->mountpoint
);
277 static void fuse_export_delete(BlockExport
*blk_exp
)
279 FuseExport
*exp
= container_of(blk_exp
, FuseExport
, common
);
281 if (exp
->fuse_session
) {
283 fuse_session_unmount(exp
->fuse_session
);
286 fuse_session_destroy(exp
->fuse_session
);
289 free(exp
->fuse_buf
.mem
);
290 g_free(exp
->mountpoint
);
294 * Check whether @path points to a regular file. If not, put an
295 * appropriate message into *errp.
297 static bool is_regular_file(const char *path
, Error
**errp
)
302 ret
= stat(path
, &statbuf
);
304 error_setg_errno(errp
, errno
, "Failed to stat '%s'", path
);
308 if (!S_ISREG(statbuf
.st_mode
)) {
309 error_setg(errp
, "'%s' is not a regular file", path
);
317 * A chance to set change some parameters supplied to FUSE_INIT.
319 static void fuse_init(void *userdata
, struct fuse_conn_info
*conn
)
322 * MIN_NON_ZERO() would not be wrong here, but what we set here
323 * must equal what has been passed to fuse_session_new().
324 * Therefore, as long as max_read must be passed as a mount option
325 * (which libfuse claims will be changed at some point), we have
326 * to set max_read to a fixed value here.
328 conn
->max_read
= FUSE_MAX_BOUNCE_BYTES
;
330 conn
->max_write
= MIN_NON_ZERO(BDRV_REQUEST_MAX_BYTES
, conn
->max_write
);
334 * Let clients look up files. Always return ENOENT because we only
335 * care about the mountpoint itself.
337 static void fuse_lookup(fuse_req_t req
, fuse_ino_t parent
, const char *name
)
339 fuse_reply_err(req
, ENOENT
);
343 * Let clients get file attributes (i.e., stat() the file).
345 static void fuse_getattr(fuse_req_t req
, fuse_ino_t inode
,
346 struct fuse_file_info
*fi
)
349 int64_t length
, allocated_blocks
;
350 time_t now
= time(NULL
);
351 FuseExport
*exp
= fuse_req_userdata(req
);
353 length
= blk_getlength(exp
->common
.blk
);
355 fuse_reply_err(req
, -length
);
359 allocated_blocks
= bdrv_get_allocated_file_size(blk_bs(exp
->common
.blk
));
360 if (allocated_blocks
<= 0) {
361 allocated_blocks
= DIV_ROUND_UP(length
, 512);
363 allocated_blocks
= DIV_ROUND_UP(allocated_blocks
, 512);
366 statbuf
= (struct stat
) {
368 .st_mode
= exp
->st_mode
,
370 .st_uid
= exp
->st_uid
,
371 .st_gid
= exp
->st_gid
,
373 .st_blksize
= blk_bs(exp
->common
.blk
)->bl
.request_alignment
,
374 .st_blocks
= allocated_blocks
,
380 fuse_reply_attr(req
, &statbuf
, 1.);
383 static int fuse_do_truncate(const FuseExport
*exp
, int64_t size
,
384 bool req_zero_write
, PreallocMode prealloc
)
386 uint64_t blk_perm
, blk_shared_perm
;
387 BdrvRequestFlags truncate_flags
= 0;
390 if (req_zero_write
) {
391 truncate_flags
|= BDRV_REQ_ZERO_WRITE
;
394 /* Growable exports have a permanent RESIZE permission */
395 if (!exp
->growable
) {
396 blk_get_perm(exp
->common
.blk
, &blk_perm
, &blk_shared_perm
);
398 ret
= blk_set_perm(exp
->common
.blk
, blk_perm
| BLK_PERM_RESIZE
,
399 blk_shared_perm
, NULL
);
405 ret
= blk_truncate(exp
->common
.blk
, size
, true, prealloc
,
406 truncate_flags
, NULL
);
408 if (!exp
->growable
) {
409 /* Must succeed, because we are only giving up the RESIZE permission */
410 blk_set_perm(exp
->common
.blk
, blk_perm
, blk_shared_perm
, &error_abort
);
417 * Let clients set file attributes. Only resizing and changing
418 * permissions (st_mode, st_uid, st_gid) is allowed.
419 * Changing permissions is only allowed as far as it will actually
420 * permit access: Read-only exports cannot be given +w, and exports
421 * without allow_other cannot be given a different UID or GID, and
422 * they cannot be given non-owner access.
424 static void fuse_setattr(fuse_req_t req
, fuse_ino_t inode
, struct stat
*statbuf
,
425 int to_set
, struct fuse_file_info
*fi
)
427 FuseExport
*exp
= fuse_req_userdata(req
);
431 supported_attrs
= FUSE_SET_ATTR_SIZE
| FUSE_SET_ATTR_MODE
;
432 if (exp
->allow_other
) {
433 supported_attrs
|= FUSE_SET_ATTR_UID
| FUSE_SET_ATTR_GID
;
436 if (to_set
& ~supported_attrs
) {
437 fuse_reply_err(req
, ENOTSUP
);
441 /* Do some argument checks first before committing to anything */
442 if (to_set
& FUSE_SET_ATTR_MODE
) {
444 * Without allow_other, non-owners can never access the export, so do
445 * not allow setting permissions for them
447 if (!exp
->allow_other
&&
448 (statbuf
->st_mode
& (S_IRWXG
| S_IRWXO
)) != 0)
450 fuse_reply_err(req
, EPERM
);
454 /* +w for read-only exports makes no sense, disallow it */
455 if (!exp
->writable
&&
456 (statbuf
->st_mode
& (S_IWUSR
| S_IWGRP
| S_IWOTH
)) != 0)
458 fuse_reply_err(req
, EROFS
);
463 if (to_set
& FUSE_SET_ATTR_SIZE
) {
464 if (!exp
->writable
) {
465 fuse_reply_err(req
, EACCES
);
469 ret
= fuse_do_truncate(exp
, statbuf
->st_size
, true, PREALLOC_MODE_OFF
);
471 fuse_reply_err(req
, -ret
);
476 if (to_set
& FUSE_SET_ATTR_MODE
) {
477 /* Ignore FUSE-supplied file type, only change the mode */
478 exp
->st_mode
= (statbuf
->st_mode
& 07777) | S_IFREG
;
481 if (to_set
& FUSE_SET_ATTR_UID
) {
482 exp
->st_uid
= statbuf
->st_uid
;
485 if (to_set
& FUSE_SET_ATTR_GID
) {
486 exp
->st_gid
= statbuf
->st_gid
;
489 fuse_getattr(req
, inode
, fi
);
493 * Let clients open a file (i.e., the exported image).
495 static void fuse_open(fuse_req_t req
, fuse_ino_t inode
,
496 struct fuse_file_info
*fi
)
498 fuse_reply_open(req
, fi
);
502 * Handle client reads from the exported image.
504 static void fuse_read(fuse_req_t req
, fuse_ino_t inode
,
505 size_t size
, off_t offset
, struct fuse_file_info
*fi
)
507 FuseExport
*exp
= fuse_req_userdata(req
);
512 /* Limited by max_read, should not happen */
513 if (size
> FUSE_MAX_BOUNCE_BYTES
) {
514 fuse_reply_err(req
, EINVAL
);
519 * Clients will expect short reads at EOF, so we have to limit
520 * offset+size to the image length.
522 length
= blk_getlength(exp
->common
.blk
);
524 fuse_reply_err(req
, -length
);
528 if (offset
+ size
> length
) {
529 size
= length
- offset
;
532 buf
= qemu_try_blockalign(blk_bs(exp
->common
.blk
), size
);
534 fuse_reply_err(req
, ENOMEM
);
538 ret
= blk_pread(exp
->common
.blk
, offset
, buf
, size
);
540 fuse_reply_buf(req
, buf
, size
);
542 fuse_reply_err(req
, -ret
);
549 * Handle client writes to the exported image.
551 static void fuse_write(fuse_req_t req
, fuse_ino_t inode
, const char *buf
,
552 size_t size
, off_t offset
, struct fuse_file_info
*fi
)
554 FuseExport
*exp
= fuse_req_userdata(req
);
558 /* Limited by max_write, should not happen */
559 if (size
> BDRV_REQUEST_MAX_BYTES
) {
560 fuse_reply_err(req
, EINVAL
);
564 if (!exp
->writable
) {
565 fuse_reply_err(req
, EACCES
);
570 * Clients will expect short writes at EOF, so we have to limit
571 * offset+size to the image length.
573 length
= blk_getlength(exp
->common
.blk
);
575 fuse_reply_err(req
, -length
);
579 if (offset
+ size
> length
) {
581 ret
= fuse_do_truncate(exp
, offset
+ size
, true, PREALLOC_MODE_OFF
);
583 fuse_reply_err(req
, -ret
);
587 size
= length
- offset
;
591 ret
= blk_pwrite(exp
->common
.blk
, offset
, buf
, size
, 0);
593 fuse_reply_write(req
, size
);
595 fuse_reply_err(req
, -ret
);
600 * Let clients perform various fallocate() operations.
602 static void fuse_fallocate(fuse_req_t req
, fuse_ino_t inode
, int mode
,
603 off_t offset
, off_t length
,
604 struct fuse_file_info
*fi
)
606 FuseExport
*exp
= fuse_req_userdata(req
);
610 if (!exp
->writable
) {
611 fuse_reply_err(req
, EACCES
);
615 blk_len
= blk_getlength(exp
->common
.blk
);
617 fuse_reply_err(req
, -blk_len
);
621 if (mode
& FALLOC_FL_KEEP_SIZE
) {
622 length
= MIN(length
, blk_len
- offset
);
625 if (mode
& FALLOC_FL_PUNCH_HOLE
) {
626 if (!(mode
& FALLOC_FL_KEEP_SIZE
)) {
627 fuse_reply_err(req
, EINVAL
);
632 int size
= MIN(length
, BDRV_REQUEST_MAX_BYTES
);
634 ret
= blk_pdiscard(exp
->common
.blk
, offset
, size
);
637 } while (ret
== 0 && length
> 0);
638 } else if (mode
& FALLOC_FL_ZERO_RANGE
) {
639 if (!(mode
& FALLOC_FL_KEEP_SIZE
) && offset
+ length
> blk_len
) {
640 /* No need for zeroes, we are going to write them ourselves */
641 ret
= fuse_do_truncate(exp
, offset
+ length
, false,
644 fuse_reply_err(req
, -ret
);
650 int size
= MIN(length
, BDRV_REQUEST_MAX_BYTES
);
652 ret
= blk_pwrite_zeroes(exp
->common
.blk
,
656 } while (ret
== 0 && length
> 0);
658 /* We can only fallocate at the EOF with a truncate */
659 if (offset
< blk_len
) {
660 fuse_reply_err(req
, EOPNOTSUPP
);
664 if (offset
> blk_len
) {
665 /* No preallocation needed here */
666 ret
= fuse_do_truncate(exp
, offset
, true, PREALLOC_MODE_OFF
);
668 fuse_reply_err(req
, -ret
);
673 ret
= fuse_do_truncate(exp
, offset
+ length
, true,
674 PREALLOC_MODE_FALLOC
);
679 fuse_reply_err(req
, ret
< 0 ? -ret
: 0);
683 * Let clients fsync the exported image.
685 static void fuse_fsync(fuse_req_t req
, fuse_ino_t inode
, int datasync
,
686 struct fuse_file_info
*fi
)
688 FuseExport
*exp
= fuse_req_userdata(req
);
691 ret
= blk_flush(exp
->common
.blk
);
692 fuse_reply_err(req
, ret
< 0 ? -ret
: 0);
696 * Called before an FD to the exported image is closed. (libfuse
697 * notes this to be a way to return last-minute errors.)
699 static void fuse_flush(fuse_req_t req
, fuse_ino_t inode
,
700 struct fuse_file_info
*fi
)
702 fuse_fsync(req
, inode
, 1, fi
);
705 #ifdef CONFIG_FUSE_LSEEK
707 * Let clients inquire allocation status.
709 static void fuse_lseek(fuse_req_t req
, fuse_ino_t inode
, off_t offset
,
710 int whence
, struct fuse_file_info
*fi
)
712 FuseExport
*exp
= fuse_req_userdata(req
);
714 if (whence
!= SEEK_HOLE
&& whence
!= SEEK_DATA
) {
715 fuse_reply_err(req
, EINVAL
);
723 ret
= bdrv_block_status_above(blk_bs(exp
->common
.blk
), NULL
,
724 offset
, INT64_MAX
, &pnum
, NULL
, NULL
);
726 fuse_reply_err(req
, -ret
);
730 if (!pnum
&& (ret
& BDRV_BLOCK_EOF
)) {
734 * If blk_getlength() rounds (e.g. by sectors), then the
735 * export length will be rounded, too. However,
736 * bdrv_block_status_above() may return EOF at unaligned
737 * offsets. We must not let this become visible and thus
738 * always simulate a hole between @offset (the real EOF)
739 * and @blk_len (the client-visible EOF).
742 blk_len
= blk_getlength(exp
->common
.blk
);
744 fuse_reply_err(req
, -blk_len
);
748 if (offset
> blk_len
|| whence
== SEEK_DATA
) {
749 fuse_reply_err(req
, ENXIO
);
751 fuse_reply_lseek(req
, offset
);
756 if (ret
& BDRV_BLOCK_DATA
) {
757 if (whence
== SEEK_DATA
) {
758 fuse_reply_lseek(req
, offset
);
762 if (whence
== SEEK_HOLE
) {
763 fuse_reply_lseek(req
, offset
);
768 /* Safety check against infinite loops */
770 fuse_reply_err(req
, ENXIO
);
779 static const struct fuse_lowlevel_ops fuse_ops
= {
781 .lookup
= fuse_lookup
,
782 .getattr
= fuse_getattr
,
783 .setattr
= fuse_setattr
,
787 .fallocate
= fuse_fallocate
,
790 #ifdef CONFIG_FUSE_LSEEK
795 const BlockExportDriver blk_exp_fuse
= {
796 .type
= BLOCK_EXPORT_TYPE_FUSE
,
797 .instance_size
= sizeof(FuseExport
),
798 .create
= fuse_export_create
,
799 .delete = fuse_export_delete
,
800 .request_shutdown
= fuse_export_shutdown
,