xref: /openbmc/qemu/block/export/fuse.c (revision 6c3a9247)
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 exports, take the RESIZE permission */
90     if (args->growable) {
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     int ret;
396 
397     if (req_zero_write) {
398         truncate_flags |= BDRV_REQ_ZERO_WRITE;
399     }
400 
401     /* Growable exports have a permanent RESIZE permission */
402     if (!exp->growable) {
403         blk_get_perm(exp->common.blk, &blk_perm, &blk_shared_perm);
404 
405         ret = blk_set_perm(exp->common.blk, blk_perm | BLK_PERM_RESIZE,
406                            blk_shared_perm, NULL);
407         if (ret < 0) {
408             return ret;
409         }
410     }
411 
412     ret = blk_truncate(exp->common.blk, size, true, prealloc,
413                        truncate_flags, NULL);
414 
415     if (!exp->growable) {
416         /* Must succeed, because we are only giving up the RESIZE permission */
417         blk_set_perm(exp->common.blk, blk_perm, blk_shared_perm, &error_abort);
418     }
419 
420     return ret;
421 }
422 
423 /**
424  * Let clients set file attributes.  Only resizing and changing
425  * permissions (st_mode, st_uid, st_gid) is allowed.
426  * Changing permissions is only allowed as far as it will actually
427  * permit access: Read-only exports cannot be given +w, and exports
428  * without allow_other cannot be given a different UID or GID, and
429  * they cannot be given non-owner access.
430  */
431 static void fuse_setattr(fuse_req_t req, fuse_ino_t inode, struct stat *statbuf,
432                          int to_set, struct fuse_file_info *fi)
433 {
434     FuseExport *exp = fuse_req_userdata(req);
435     int supported_attrs;
436     int ret;
437 
438     supported_attrs = FUSE_SET_ATTR_SIZE | FUSE_SET_ATTR_MODE;
439     if (exp->allow_other) {
440         supported_attrs |= FUSE_SET_ATTR_UID | FUSE_SET_ATTR_GID;
441     }
442 
443     if (to_set & ~supported_attrs) {
444         fuse_reply_err(req, ENOTSUP);
445         return;
446     }
447 
448     /* Do some argument checks first before committing to anything */
449     if (to_set & FUSE_SET_ATTR_MODE) {
450         /*
451          * Without allow_other, non-owners can never access the export, so do
452          * not allow setting permissions for them
453          */
454         if (!exp->allow_other &&
455             (statbuf->st_mode & (S_IRWXG | S_IRWXO)) != 0)
456         {
457             fuse_reply_err(req, EPERM);
458             return;
459         }
460 
461         /* +w for read-only exports makes no sense, disallow it */
462         if (!exp->writable &&
463             (statbuf->st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)) != 0)
464         {
465             fuse_reply_err(req, EROFS);
466             return;
467         }
468     }
469 
470     if (to_set & FUSE_SET_ATTR_SIZE) {
471         if (!exp->writable) {
472             fuse_reply_err(req, EACCES);
473             return;
474         }
475 
476         ret = fuse_do_truncate(exp, statbuf->st_size, true, PREALLOC_MODE_OFF);
477         if (ret < 0) {
478             fuse_reply_err(req, -ret);
479             return;
480         }
481     }
482 
483     if (to_set & FUSE_SET_ATTR_MODE) {
484         /* Ignore FUSE-supplied file type, only change the mode */
485         exp->st_mode = (statbuf->st_mode & 07777) | S_IFREG;
486     }
487 
488     if (to_set & FUSE_SET_ATTR_UID) {
489         exp->st_uid = statbuf->st_uid;
490     }
491 
492     if (to_set & FUSE_SET_ATTR_GID) {
493         exp->st_gid = statbuf->st_gid;
494     }
495 
496     fuse_getattr(req, inode, fi);
497 }
498 
499 /**
500  * Let clients open a file (i.e., the exported image).
501  */
502 static void fuse_open(fuse_req_t req, fuse_ino_t inode,
503                       struct fuse_file_info *fi)
504 {
505     fuse_reply_open(req, fi);
506 }
507 
508 /**
509  * Handle client reads from the exported image.
510  */
511 static void fuse_read(fuse_req_t req, fuse_ino_t inode,
512                       size_t size, off_t offset, struct fuse_file_info *fi)
513 {
514     FuseExport *exp = fuse_req_userdata(req);
515     int64_t length;
516     void *buf;
517     int ret;
518 
519     /* Limited by max_read, should not happen */
520     if (size > FUSE_MAX_BOUNCE_BYTES) {
521         fuse_reply_err(req, EINVAL);
522         return;
523     }
524 
525     /**
526      * Clients will expect short reads at EOF, so we have to limit
527      * offset+size to the image length.
528      */
529     length = blk_getlength(exp->common.blk);
530     if (length < 0) {
531         fuse_reply_err(req, -length);
532         return;
533     }
534 
535     if (offset + size > length) {
536         size = length - offset;
537     }
538 
539     buf = qemu_try_blockalign(blk_bs(exp->common.blk), size);
540     if (!buf) {
541         fuse_reply_err(req, ENOMEM);
542         return;
543     }
544 
545     ret = blk_pread(exp->common.blk, offset, buf, size);
546     if (ret >= 0) {
547         fuse_reply_buf(req, buf, size);
548     } else {
549         fuse_reply_err(req, -ret);
550     }
551 
552     qemu_vfree(buf);
553 }
554 
555 /**
556  * Handle client writes to the exported image.
557  */
558 static void fuse_write(fuse_req_t req, fuse_ino_t inode, const char *buf,
559                        size_t size, off_t offset, struct fuse_file_info *fi)
560 {
561     FuseExport *exp = fuse_req_userdata(req);
562     int64_t length;
563     int ret;
564 
565     /* Limited by max_write, should not happen */
566     if (size > BDRV_REQUEST_MAX_BYTES) {
567         fuse_reply_err(req, EINVAL);
568         return;
569     }
570 
571     if (!exp->writable) {
572         fuse_reply_err(req, EACCES);
573         return;
574     }
575 
576     /**
577      * Clients will expect short writes at EOF, so we have to limit
578      * offset+size to the image length.
579      */
580     length = blk_getlength(exp->common.blk);
581     if (length < 0) {
582         fuse_reply_err(req, -length);
583         return;
584     }
585 
586     if (offset + size > length) {
587         if (exp->growable) {
588             ret = fuse_do_truncate(exp, offset + size, true, PREALLOC_MODE_OFF);
589             if (ret < 0) {
590                 fuse_reply_err(req, -ret);
591                 return;
592             }
593         } else {
594             size = length - offset;
595         }
596     }
597 
598     ret = blk_pwrite(exp->common.blk, offset, buf, size, 0);
599     if (ret >= 0) {
600         fuse_reply_write(req, size);
601     } else {
602         fuse_reply_err(req, -ret);
603     }
604 }
605 
606 /**
607  * Let clients perform various fallocate() operations.
608  */
609 static void fuse_fallocate(fuse_req_t req, fuse_ino_t inode, int mode,
610                            off_t offset, off_t length,
611                            struct fuse_file_info *fi)
612 {
613     FuseExport *exp = fuse_req_userdata(req);
614     int64_t blk_len;
615     int ret;
616 
617     if (!exp->writable) {
618         fuse_reply_err(req, EACCES);
619         return;
620     }
621 
622     blk_len = blk_getlength(exp->common.blk);
623     if (blk_len < 0) {
624         fuse_reply_err(req, -blk_len);
625         return;
626     }
627 
628 #ifdef CONFIG_FALLOCATE_PUNCH_HOLE
629     if (mode & FALLOC_FL_KEEP_SIZE) {
630         length = MIN(length, blk_len - offset);
631     }
632 #endif /* CONFIG_FALLOCATE_PUNCH_HOLE */
633 
634     if (!mode) {
635         /* We can only fallocate at the EOF with a truncate */
636         if (offset < blk_len) {
637             fuse_reply_err(req, EOPNOTSUPP);
638             return;
639         }
640 
641         if (offset > blk_len) {
642             /* No preallocation needed here */
643             ret = fuse_do_truncate(exp, offset, true, PREALLOC_MODE_OFF);
644             if (ret < 0) {
645                 fuse_reply_err(req, -ret);
646                 return;
647             }
648         }
649 
650         ret = fuse_do_truncate(exp, offset + length, true,
651                                PREALLOC_MODE_FALLOC);
652     }
653 #ifdef CONFIG_FALLOCATE_PUNCH_HOLE
654     else if (mode & FALLOC_FL_PUNCH_HOLE) {
655         if (!(mode & FALLOC_FL_KEEP_SIZE)) {
656             fuse_reply_err(req, EINVAL);
657             return;
658         }
659 
660         do {
661             int size = MIN(length, BDRV_REQUEST_MAX_BYTES);
662 
663             ret = blk_pdiscard(exp->common.blk, offset, size);
664             offset += size;
665             length -= size;
666         } while (ret == 0 && length > 0);
667     }
668 #endif /* CONFIG_FALLOCATE_PUNCH_HOLE */
669 #ifdef CONFIG_FALLOCATE_ZERO_RANGE
670     else if (mode & FALLOC_FL_ZERO_RANGE) {
671         if (!(mode & FALLOC_FL_KEEP_SIZE) && offset + length > blk_len) {
672             /* No need for zeroes, we are going to write them ourselves */
673             ret = fuse_do_truncate(exp, offset + length, false,
674                                    PREALLOC_MODE_OFF);
675             if (ret < 0) {
676                 fuse_reply_err(req, -ret);
677                 return;
678             }
679         }
680 
681         do {
682             int size = MIN(length, BDRV_REQUEST_MAX_BYTES);
683 
684             ret = blk_pwrite_zeroes(exp->common.blk,
685                                     offset, size, 0);
686             offset += size;
687             length -= size;
688         } while (ret == 0 && length > 0);
689     }
690 #endif /* CONFIG_FALLOCATE_ZERO_RANGE */
691     else {
692         ret = -EOPNOTSUPP;
693     }
694 
695     fuse_reply_err(req, ret < 0 ? -ret : 0);
696 }
697 
698 /**
699  * Let clients fsync the exported image.
700  */
701 static void fuse_fsync(fuse_req_t req, fuse_ino_t inode, int datasync,
702                        struct fuse_file_info *fi)
703 {
704     FuseExport *exp = fuse_req_userdata(req);
705     int ret;
706 
707     ret = blk_flush(exp->common.blk);
708     fuse_reply_err(req, ret < 0 ? -ret : 0);
709 }
710 
711 /**
712  * Called before an FD to the exported image is closed.  (libfuse
713  * notes this to be a way to return last-minute errors.)
714  */
715 static void fuse_flush(fuse_req_t req, fuse_ino_t inode,
716                         struct fuse_file_info *fi)
717 {
718     fuse_fsync(req, inode, 1, fi);
719 }
720 
721 #ifdef CONFIG_FUSE_LSEEK
722 /**
723  * Let clients inquire allocation status.
724  */
725 static void fuse_lseek(fuse_req_t req, fuse_ino_t inode, off_t offset,
726                        int whence, struct fuse_file_info *fi)
727 {
728     FuseExport *exp = fuse_req_userdata(req);
729 
730     if (whence != SEEK_HOLE && whence != SEEK_DATA) {
731         fuse_reply_err(req, EINVAL);
732         return;
733     }
734 
735     while (true) {
736         int64_t pnum;
737         int ret;
738 
739         ret = bdrv_block_status_above(blk_bs(exp->common.blk), NULL,
740                                       offset, INT64_MAX, &pnum, NULL, NULL);
741         if (ret < 0) {
742             fuse_reply_err(req, -ret);
743             return;
744         }
745 
746         if (!pnum && (ret & BDRV_BLOCK_EOF)) {
747             int64_t blk_len;
748 
749             /*
750              * If blk_getlength() rounds (e.g. by sectors), then the
751              * export length will be rounded, too.  However,
752              * bdrv_block_status_above() may return EOF at unaligned
753              * offsets.  We must not let this become visible and thus
754              * always simulate a hole between @offset (the real EOF)
755              * and @blk_len (the client-visible EOF).
756              */
757 
758             blk_len = blk_getlength(exp->common.blk);
759             if (blk_len < 0) {
760                 fuse_reply_err(req, -blk_len);
761                 return;
762             }
763 
764             if (offset > blk_len || whence == SEEK_DATA) {
765                 fuse_reply_err(req, ENXIO);
766             } else {
767                 fuse_reply_lseek(req, offset);
768             }
769             return;
770         }
771 
772         if (ret & BDRV_BLOCK_DATA) {
773             if (whence == SEEK_DATA) {
774                 fuse_reply_lseek(req, offset);
775                 return;
776             }
777         } else {
778             if (whence == SEEK_HOLE) {
779                 fuse_reply_lseek(req, offset);
780                 return;
781             }
782         }
783 
784         /* Safety check against infinite loops */
785         if (!pnum) {
786             fuse_reply_err(req, ENXIO);
787             return;
788         }
789 
790         offset += pnum;
791     }
792 }
793 #endif
794 
795 static const struct fuse_lowlevel_ops fuse_ops = {
796     .init       = fuse_init,
797     .lookup     = fuse_lookup,
798     .getattr    = fuse_getattr,
799     .setattr    = fuse_setattr,
800     .open       = fuse_open,
801     .read       = fuse_read,
802     .write      = fuse_write,
803     .fallocate  = fuse_fallocate,
804     .flush      = fuse_flush,
805     .fsync      = fuse_fsync,
806 #ifdef CONFIG_FUSE_LSEEK
807     .lseek      = fuse_lseek,
808 #endif
809 };
810 
811 const BlockExportDriver blk_exp_fuse = {
812     .type               = BLOCK_EXPORT_TYPE_FUSE,
813     .instance_size      = sizeof(FuseExport),
814     .create             = fuse_export_create,
815     .delete             = fuse_export_delete,
816     .request_shutdown   = fuse_export_shutdown,
817 };
818