xref: /qemu/block/export/fuse.c (revision 13f934e7)
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 
35 /* Prevent overly long bounce buffer allocations */
36 #define FUSE_MAX_BOUNCE_BYTES (MIN(BDRV_REQUEST_MAX_BYTES, 64 * 1024 * 1024))
37 
38 
39 typedef struct FuseExport {
40     BlockExport common;
41 
42     struct fuse_session *fuse_session;
43     struct fuse_buf fuse_buf;
44     bool mounted, fd_handler_set_up;
45 
46     char *mountpoint;
47     bool writable;
48     bool growable;
49     /* Whether allow_other was used as a mount option or not */
50     bool allow_other;
51 
52     mode_t st_mode;
53     uid_t st_uid;
54     gid_t st_gid;
55 } FuseExport;
56 
57 static GHashTable *exports;
58 static const struct fuse_lowlevel_ops fuse_ops;
59 
60 static void fuse_export_shutdown(BlockExport *exp);
61 static void fuse_export_delete(BlockExport *exp);
62 
63 static void init_exports_table(void);
64 
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);
68 
69 static bool is_regular_file(const char *path, Error **errp);
70 
71 
72 static int fuse_export_create(BlockExport *blk_exp,
73                               BlockExportOptions *blk_exp_args,
74                               Error **errp)
75 {
76     FuseExport *exp = container_of(blk_exp, FuseExport, common);
77     BlockExportOptionsFuse *args = &blk_exp_args->u.fuse;
78     int ret;
79 
80     assert(blk_exp_args->type == BLOCK_EXPORT_TYPE_FUSE);
81 
82     /* For growable exports, take the RESIZE permission */
83     if (args->growable) {
84         uint64_t blk_perm, blk_shared_perm;
85 
86         blk_get_perm(exp->common.blk, &blk_perm, &blk_shared_perm);
87 
88         ret = blk_set_perm(exp->common.blk, blk_perm | BLK_PERM_RESIZE,
89                            blk_shared_perm, errp);
90         if (ret < 0) {
91             return ret;
92         }
93     }
94 
95     init_exports_table();
96 
97     /*
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
109      * string twice.
110      */
111     if (g_hash_table_contains(exports, args->mountpoint)) {
112         error_setg(errp, "There already is a FUSE export on '%s'",
113                    args->mountpoint);
114         ret = -EEXIST;
115         goto fail;
116     }
117 
118     if (!is_regular_file(args->mountpoint, errp)) {
119         ret = -EINVAL;
120         goto fail;
121     }
122 
123     exp->mountpoint = g_strdup(args->mountpoint);
124     exp->writable = blk_exp_args->writable;
125     exp->growable = args->growable;
126 
127     /* set default */
128     if (!args->has_allow_other) {
129         args->allow_other = FUSE_EXPORT_ALLOW_OTHER_AUTO;
130     }
131 
132     exp->st_mode = S_IFREG | S_IRUSR;
133     if (exp->writable) {
134         exp->st_mode |= S_IWUSR;
135     }
136     exp->st_uid = getuid();
137     exp->st_gid = getgid();
138 
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;
143         if (ret < 0) {
144             ret = setup_fuse_export(exp, args->mountpoint, false, errp);
145         }
146     } else {
147         exp->allow_other = args->allow_other == FUSE_EXPORT_ALLOW_OTHER_ON;
148         ret = setup_fuse_export(exp, args->mountpoint, exp->allow_other, errp);
149     }
150     if (ret < 0) {
151         goto fail;
152     }
153 
154     return 0;
155 
156 fail:
157     fuse_export_delete(blk_exp);
158     return ret;
159 }
160 
161 /**
162  * Allocates the global @exports hash table.
163  */
164 static void init_exports_table(void)
165 {
166     if (exports) {
167         return;
168     }
169 
170     exports = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
171 }
172 
173 /**
174  * Create exp->fuse_session and mount it.
175  */
176 static int setup_fuse_export(FuseExport *exp, const char *mountpoint,
177                              bool allow_other, Error **errp)
178 {
179     const char *fuse_argv[4];
180     char *mount_opts;
181     struct fuse_args fuse_args;
182     int ret;
183 
184     /*
185      * max_read needs to match what fuse_init() sets.
186      * max_write need not be supplied.
187      */
188     mount_opts = g_strdup_printf("max_read=%zu,default_permissions%s",
189                                  FUSE_MAX_BOUNCE_BYTES,
190                                  allow_other ? ",allow_other" : "");
191 
192     fuse_argv[0] = ""; /* Dummy program name */
193     fuse_argv[1] = "-o";
194     fuse_argv[2] = mount_opts;
195     fuse_argv[3] = NULL;
196     fuse_args = (struct fuse_args)FUSE_ARGS_INIT(3, (char **)fuse_argv);
197 
198     exp->fuse_session = fuse_session_new(&fuse_args, &fuse_ops,
199                                          sizeof(fuse_ops), exp);
200     g_free(mount_opts);
201     if (!exp->fuse_session) {
202         error_setg(errp, "Failed to set up FUSE session");
203         ret = -EIO;
204         goto fail;
205     }
206 
207     ret = fuse_session_mount(exp->fuse_session, mountpoint);
208     if (ret < 0) {
209         error_setg(errp, "Failed to mount FUSE session to export");
210         ret = -EIO;
211         goto fail;
212     }
213     exp->mounted = true;
214 
215     g_hash_table_insert(exports, g_strdup(mountpoint), NULL);
216 
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;
221 
222     return 0;
223 
224 fail:
225     fuse_export_shutdown(&exp->common);
226     return ret;
227 }
228 
229 /**
230  * Callback to be invoked when the FUSE session FD can be read from.
231  * (This is basically the FUSE event loop.)
232  */
233 static void read_from_fuse_export(void *opaque)
234 {
235     FuseExport *exp = opaque;
236     int ret;
237 
238     blk_exp_ref(&exp->common);
239 
240     do {
241         ret = fuse_session_receive_buf(exp->fuse_session, &exp->fuse_buf);
242     } while (ret == -EINTR);
243     if (ret < 0) {
244         goto out;
245     }
246 
247     fuse_session_process_buf(exp->fuse_session, &exp->fuse_buf);
248 
249 out:
250     blk_exp_unref(&exp->common);
251 }
252 
253 static void fuse_export_shutdown(BlockExport *blk_exp)
254 {
255     FuseExport *exp = container_of(blk_exp, FuseExport, common);
256 
257     if (exp->fuse_session) {
258         fuse_session_exit(exp->fuse_session);
259 
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;
265         }
266     }
267 
268     if (exp->mountpoint) {
269         /*
270          * Safe to drop now, because we will not handle any requests
271          * for this export anymore anyway.
272          */
273         g_hash_table_remove(exports, exp->mountpoint);
274     }
275 }
276 
277 static void fuse_export_delete(BlockExport *blk_exp)
278 {
279     FuseExport *exp = container_of(blk_exp, FuseExport, common);
280 
281     if (exp->fuse_session) {
282         if (exp->mounted) {
283             fuse_session_unmount(exp->fuse_session);
284         }
285 
286         fuse_session_destroy(exp->fuse_session);
287     }
288 
289     free(exp->fuse_buf.mem);
290     g_free(exp->mountpoint);
291 }
292 
293 /**
294  * Check whether @path points to a regular file.  If not, put an
295  * appropriate message into *errp.
296  */
297 static bool is_regular_file(const char *path, Error **errp)
298 {
299     struct stat statbuf;
300     int ret;
301 
302     ret = stat(path, &statbuf);
303     if (ret < 0) {
304         error_setg_errno(errp, errno, "Failed to stat '%s'", path);
305         return false;
306     }
307 
308     if (!S_ISREG(statbuf.st_mode)) {
309         error_setg(errp, "'%s' is not a regular file", path);
310         return false;
311     }
312 
313     return true;
314 }
315 
316 /**
317  * A chance to set change some parameters supplied to FUSE_INIT.
318  */
319 static void fuse_init(void *userdata, struct fuse_conn_info *conn)
320 {
321     /*
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.
327      */
328     conn->max_read = FUSE_MAX_BOUNCE_BYTES;
329 
330     conn->max_write = MIN_NON_ZERO(BDRV_REQUEST_MAX_BYTES, conn->max_write);
331 }
332 
333 /**
334  * Let clients look up files.  Always return ENOENT because we only
335  * care about the mountpoint itself.
336  */
337 static void fuse_lookup(fuse_req_t req, fuse_ino_t parent, const char *name)
338 {
339     fuse_reply_err(req, ENOENT);
340 }
341 
342 /**
343  * Let clients get file attributes (i.e., stat() the file).
344  */
345 static void fuse_getattr(fuse_req_t req, fuse_ino_t inode,
346                          struct fuse_file_info *fi)
347 {
348     struct stat statbuf;
349     int64_t length, allocated_blocks;
350     time_t now = time(NULL);
351     FuseExport *exp = fuse_req_userdata(req);
352 
353     length = blk_getlength(exp->common.blk);
354     if (length < 0) {
355         fuse_reply_err(req, -length);
356         return;
357     }
358 
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);
362     } else {
363         allocated_blocks = DIV_ROUND_UP(allocated_blocks, 512);
364     }
365 
366     statbuf = (struct stat) {
367         .st_ino     = inode,
368         .st_mode    = exp->st_mode,
369         .st_nlink   = 1,
370         .st_uid     = exp->st_uid,
371         .st_gid     = exp->st_gid,
372         .st_size    = length,
373         .st_blksize = blk_bs(exp->common.blk)->bl.request_alignment,
374         .st_blocks  = allocated_blocks,
375         .st_atime   = now,
376         .st_mtime   = now,
377         .st_ctime   = now,
378     };
379 
380     fuse_reply_attr(req, &statbuf, 1.);
381 }
382 
383 static int fuse_do_truncate(const FuseExport *exp, int64_t size,
384                             bool req_zero_write, PreallocMode prealloc)
385 {
386     uint64_t blk_perm, blk_shared_perm;
387     BdrvRequestFlags truncate_flags = 0;
388     int ret;
389 
390     if (req_zero_write) {
391         truncate_flags |= BDRV_REQ_ZERO_WRITE;
392     }
393 
394     /* Growable exports have a permanent RESIZE permission */
395     if (!exp->growable) {
396         blk_get_perm(exp->common.blk, &blk_perm, &blk_shared_perm);
397 
398         ret = blk_set_perm(exp->common.blk, blk_perm | BLK_PERM_RESIZE,
399                            blk_shared_perm, NULL);
400         if (ret < 0) {
401             return ret;
402         }
403     }
404 
405     ret = blk_truncate(exp->common.blk, size, true, prealloc,
406                        truncate_flags, NULL);
407 
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);
411     }
412 
413     return ret;
414 }
415 
416 /**
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.
423  */
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)
426 {
427     FuseExport *exp = fuse_req_userdata(req);
428     int supported_attrs;
429     int ret;
430 
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;
434     }
435 
436     if (to_set & ~supported_attrs) {
437         fuse_reply_err(req, ENOTSUP);
438         return;
439     }
440 
441     /* Do some argument checks first before committing to anything */
442     if (to_set & FUSE_SET_ATTR_MODE) {
443         /*
444          * Without allow_other, non-owners can never access the export, so do
445          * not allow setting permissions for them
446          */
447         if (!exp->allow_other &&
448             (statbuf->st_mode & (S_IRWXG | S_IRWXO)) != 0)
449         {
450             fuse_reply_err(req, EPERM);
451             return;
452         }
453 
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)
457         {
458             fuse_reply_err(req, EROFS);
459             return;
460         }
461     }
462 
463     if (to_set & FUSE_SET_ATTR_SIZE) {
464         if (!exp->writable) {
465             fuse_reply_err(req, EACCES);
466             return;
467         }
468 
469         ret = fuse_do_truncate(exp, statbuf->st_size, true, PREALLOC_MODE_OFF);
470         if (ret < 0) {
471             fuse_reply_err(req, -ret);
472             return;
473         }
474     }
475 
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;
479     }
480 
481     if (to_set & FUSE_SET_ATTR_UID) {
482         exp->st_uid = statbuf->st_uid;
483     }
484 
485     if (to_set & FUSE_SET_ATTR_GID) {
486         exp->st_gid = statbuf->st_gid;
487     }
488 
489     fuse_getattr(req, inode, fi);
490 }
491 
492 /**
493  * Let clients open a file (i.e., the exported image).
494  */
495 static void fuse_open(fuse_req_t req, fuse_ino_t inode,
496                       struct fuse_file_info *fi)
497 {
498     fuse_reply_open(req, fi);
499 }
500 
501 /**
502  * Handle client reads from the exported image.
503  */
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)
506 {
507     FuseExport *exp = fuse_req_userdata(req);
508     int64_t length;
509     void *buf;
510     int ret;
511 
512     /* Limited by max_read, should not happen */
513     if (size > FUSE_MAX_BOUNCE_BYTES) {
514         fuse_reply_err(req, EINVAL);
515         return;
516     }
517 
518     /**
519      * Clients will expect short reads at EOF, so we have to limit
520      * offset+size to the image length.
521      */
522     length = blk_getlength(exp->common.blk);
523     if (length < 0) {
524         fuse_reply_err(req, -length);
525         return;
526     }
527 
528     if (offset + size > length) {
529         size = length - offset;
530     }
531 
532     buf = qemu_try_blockalign(blk_bs(exp->common.blk), size);
533     if (!buf) {
534         fuse_reply_err(req, ENOMEM);
535         return;
536     }
537 
538     ret = blk_pread(exp->common.blk, offset, buf, size);
539     if (ret >= 0) {
540         fuse_reply_buf(req, buf, size);
541     } else {
542         fuse_reply_err(req, -ret);
543     }
544 
545     qemu_vfree(buf);
546 }
547 
548 /**
549  * Handle client writes to the exported image.
550  */
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)
553 {
554     FuseExport *exp = fuse_req_userdata(req);
555     int64_t length;
556     int ret;
557 
558     /* Limited by max_write, should not happen */
559     if (size > BDRV_REQUEST_MAX_BYTES) {
560         fuse_reply_err(req, EINVAL);
561         return;
562     }
563 
564     if (!exp->writable) {
565         fuse_reply_err(req, EACCES);
566         return;
567     }
568 
569     /**
570      * Clients will expect short writes at EOF, so we have to limit
571      * offset+size to the image length.
572      */
573     length = blk_getlength(exp->common.blk);
574     if (length < 0) {
575         fuse_reply_err(req, -length);
576         return;
577     }
578 
579     if (offset + size > length) {
580         if (exp->growable) {
581             ret = fuse_do_truncate(exp, offset + size, true, PREALLOC_MODE_OFF);
582             if (ret < 0) {
583                 fuse_reply_err(req, -ret);
584                 return;
585             }
586         } else {
587             size = length - offset;
588         }
589     }
590 
591     ret = blk_pwrite(exp->common.blk, offset, buf, size, 0);
592     if (ret >= 0) {
593         fuse_reply_write(req, size);
594     } else {
595         fuse_reply_err(req, -ret);
596     }
597 }
598 
599 /**
600  * Let clients perform various fallocate() operations.
601  */
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)
605 {
606     FuseExport *exp = fuse_req_userdata(req);
607     int64_t blk_len;
608     int ret;
609 
610     if (!exp->writable) {
611         fuse_reply_err(req, EACCES);
612         return;
613     }
614 
615     blk_len = blk_getlength(exp->common.blk);
616     if (blk_len < 0) {
617         fuse_reply_err(req, -blk_len);
618         return;
619     }
620 
621     if (mode & FALLOC_FL_KEEP_SIZE) {
622         length = MIN(length, blk_len - offset);
623     }
624 
625     if (mode & FALLOC_FL_PUNCH_HOLE) {
626         if (!(mode & FALLOC_FL_KEEP_SIZE)) {
627             fuse_reply_err(req, EINVAL);
628             return;
629         }
630 
631         do {
632             int size = MIN(length, BDRV_REQUEST_MAX_BYTES);
633 
634             ret = blk_pdiscard(exp->common.blk, offset, size);
635             offset += size;
636             length -= 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,
642                                    PREALLOC_MODE_OFF);
643             if (ret < 0) {
644                 fuse_reply_err(req, -ret);
645                 return;
646             }
647         }
648 
649         do {
650             int size = MIN(length, BDRV_REQUEST_MAX_BYTES);
651 
652             ret = blk_pwrite_zeroes(exp->common.blk,
653                                     offset, size, 0);
654             offset += size;
655             length -= size;
656         } while (ret == 0 && length > 0);
657     } else if (!mode) {
658         /* We can only fallocate at the EOF with a truncate */
659         if (offset < blk_len) {
660             fuse_reply_err(req, EOPNOTSUPP);
661             return;
662         }
663 
664         if (offset > blk_len) {
665             /* No preallocation needed here */
666             ret = fuse_do_truncate(exp, offset, true, PREALLOC_MODE_OFF);
667             if (ret < 0) {
668                 fuse_reply_err(req, -ret);
669                 return;
670             }
671         }
672 
673         ret = fuse_do_truncate(exp, offset + length, true,
674                                PREALLOC_MODE_FALLOC);
675     } else {
676         ret = -EOPNOTSUPP;
677     }
678 
679     fuse_reply_err(req, ret < 0 ? -ret : 0);
680 }
681 
682 /**
683  * Let clients fsync the exported image.
684  */
685 static void fuse_fsync(fuse_req_t req, fuse_ino_t inode, int datasync,
686                        struct fuse_file_info *fi)
687 {
688     FuseExport *exp = fuse_req_userdata(req);
689     int ret;
690 
691     ret = blk_flush(exp->common.blk);
692     fuse_reply_err(req, ret < 0 ? -ret : 0);
693 }
694 
695 /**
696  * Called before an FD to the exported image is closed.  (libfuse
697  * notes this to be a way to return last-minute errors.)
698  */
699 static void fuse_flush(fuse_req_t req, fuse_ino_t inode,
700                         struct fuse_file_info *fi)
701 {
702     fuse_fsync(req, inode, 1, fi);
703 }
704 
705 #ifdef CONFIG_FUSE_LSEEK
706 /**
707  * Let clients inquire allocation status.
708  */
709 static void fuse_lseek(fuse_req_t req, fuse_ino_t inode, off_t offset,
710                        int whence, struct fuse_file_info *fi)
711 {
712     FuseExport *exp = fuse_req_userdata(req);
713 
714     if (whence != SEEK_HOLE && whence != SEEK_DATA) {
715         fuse_reply_err(req, EINVAL);
716         return;
717     }
718 
719     while (true) {
720         int64_t pnum;
721         int ret;
722 
723         ret = bdrv_block_status_above(blk_bs(exp->common.blk), NULL,
724                                       offset, INT64_MAX, &pnum, NULL, NULL);
725         if (ret < 0) {
726             fuse_reply_err(req, -ret);
727             return;
728         }
729 
730         if (!pnum && (ret & BDRV_BLOCK_EOF)) {
731             int64_t blk_len;
732 
733             /*
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).
740              */
741 
742             blk_len = blk_getlength(exp->common.blk);
743             if (blk_len < 0) {
744                 fuse_reply_err(req, -blk_len);
745                 return;
746             }
747 
748             if (offset > blk_len || whence == SEEK_DATA) {
749                 fuse_reply_err(req, ENXIO);
750             } else {
751                 fuse_reply_lseek(req, offset);
752             }
753             return;
754         }
755 
756         if (ret & BDRV_BLOCK_DATA) {
757             if (whence == SEEK_DATA) {
758                 fuse_reply_lseek(req, offset);
759                 return;
760             }
761         } else {
762             if (whence == SEEK_HOLE) {
763                 fuse_reply_lseek(req, offset);
764                 return;
765             }
766         }
767 
768         /* Safety check against infinite loops */
769         if (!pnum) {
770             fuse_reply_err(req, ENXIO);
771             return;
772         }
773 
774         offset += pnum;
775     }
776 }
777 #endif
778 
779 static const struct fuse_lowlevel_ops fuse_ops = {
780     .init       = fuse_init,
781     .lookup     = fuse_lookup,
782     .getattr    = fuse_getattr,
783     .setattr    = fuse_setattr,
784     .open       = fuse_open,
785     .read       = fuse_read,
786     .write      = fuse_write,
787     .fallocate  = fuse_fallocate,
788     .flush      = fuse_flush,
789     .fsync      = fuse_fsync,
790 #ifdef CONFIG_FUSE_LSEEK
791     .lseek      = fuse_lseek,
792 #endif
793 };
794 
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,
801 };
802