4 * Copyright (c) Intel Corporation.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * * Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include "spdk/stdinc.h"
36 #define FUSE_USE_VERSION 30
37 #include "fuse3/fuse.h"
38 #include "fuse3/fuse_lowlevel.h"
40 #include "spdk/blobfs.h"
41 #include "spdk/bdev.h"
42 #include "spdk/event.h"
43 #include "spdk/thread.h"
44 #include "spdk/blob_bdev.h"
50 pthread_t g_fuse_thread
;
52 struct spdk_bs_dev
*g_bs_dev
;
53 struct spdk_filesystem
*g_fs
;
54 struct spdk_io_channel
*g_channel
;
55 struct spdk_file
*g_file
;
58 char **g_fuse_argv
= NULL
;
61 __call_fn(void *arg1
, void *arg2
)
65 fn
= (fs_request_fn
)arg1
;
70 __send_request(fs_request_fn fn
, void *arg
)
72 struct spdk_event
*event
;
74 event
= spdk_event_allocate(0, __call_fn
, (void *)fn
, arg
);
75 spdk_event_call(event
);
79 spdk_fuse_getattr(const char *path
, struct stat
*stbuf
, struct fuse_file_info
*fi
)
81 struct spdk_file_stat stat
;
84 if (!strcmp(path
, "/")) {
85 stbuf
->st_mode
= S_IFDIR
| 0755;
90 rc
= spdk_fs_file_stat(g_fs
, g_channel
, path
, &stat
);
92 stbuf
->st_mode
= S_IFREG
| 0644;
94 stbuf
->st_size
= stat
.size
;
101 spdk_fuse_readdir(const char *path
, void *buf
, fuse_fill_dir_t filler
,
102 off_t offset
, struct fuse_file_info
*fi
,
103 enum fuse_readdir_flags flags
)
105 struct spdk_file
*file
;
106 const char *filename
;
109 filler(buf
, ".", NULL
, 0, 0);
110 filler(buf
, "..", NULL
, 0, 0);
112 iter
= spdk_fs_iter_first(g_fs
);
113 while (iter
!= NULL
) {
114 file
= spdk_fs_iter_get_file(iter
);
115 iter
= spdk_fs_iter_next(iter
);
116 filename
= spdk_file_get_name(file
);
117 filler(buf
, &filename
[1], NULL
, 0, 0);
124 spdk_fuse_mknod(const char *path
, mode_t mode
, dev_t rdev
)
126 return spdk_fs_create_file(g_fs
, g_channel
, path
);
130 spdk_fuse_unlink(const char *path
)
132 return spdk_fs_delete_file(g_fs
, g_channel
, path
);
136 spdk_fuse_truncate(const char *path
, off_t size
, struct fuse_file_info
*fi
)
138 struct spdk_file
*file
;
141 rc
= spdk_fs_open_file(g_fs
, g_channel
, path
, 0, &file
);
146 rc
= spdk_file_truncate(file
, g_channel
, size
);
151 spdk_file_close(file
, g_channel
);
157 spdk_fuse_utimens(const char *path
, const struct timespec tv
[2], struct fuse_file_info
*fi
)
163 spdk_fuse_open(const char *path
, struct fuse_file_info
*info
)
165 struct spdk_file
*file
;
168 rc
= spdk_fs_open_file(g_fs
, g_channel
, path
, 0, &file
);
173 info
->fh
= (uintptr_t)file
;
178 spdk_fuse_release(const char *path
, struct fuse_file_info
*info
)
180 struct spdk_file
*file
= (struct spdk_file
*)info
->fh
;
182 return spdk_file_close(file
, g_channel
);
186 spdk_fuse_read(const char *path
, char *buf
, size_t len
, off_t offset
, struct fuse_file_info
*info
)
188 struct spdk_file
*file
= (struct spdk_file
*)info
->fh
;
190 return spdk_file_read(file
, g_channel
, buf
, offset
, len
);
194 spdk_fuse_write(const char *path
, const char *buf
, size_t len
, off_t offset
,
195 struct fuse_file_info
*info
)
197 struct spdk_file
*file
= (struct spdk_file
*)info
->fh
;
200 rc
= spdk_file_write(file
, g_channel
, (void *)buf
, offset
, len
);
209 spdk_fuse_flush(const char *path
, struct fuse_file_info
*info
)
215 spdk_fuse_fsync(const char *path
, int datasync
, struct fuse_file_info
*info
)
221 spdk_fuse_rename(const char *old_path
, const char *new_path
, unsigned int flags
)
223 return spdk_fs_rename_file(g_fs
, g_channel
, old_path
, new_path
);
226 static struct fuse_operations spdk_fuse_oper
= {
227 .getattr
= spdk_fuse_getattr
,
228 .readdir
= spdk_fuse_readdir
,
229 .mknod
= spdk_fuse_mknod
,
230 .unlink
= spdk_fuse_unlink
,
231 .truncate
= spdk_fuse_truncate
,
232 .utimens
= spdk_fuse_utimens
,
233 .open
= spdk_fuse_open
,
234 .release
= spdk_fuse_release
,
235 .read
= spdk_fuse_read
,
236 .write
= spdk_fuse_write
,
237 .flush
= spdk_fuse_flush
,
238 .fsync
= spdk_fuse_fsync
,
239 .rename
= spdk_fuse_rename
,
243 construct_targets(void)
245 struct spdk_bdev
*bdev
;
247 bdev
= spdk_bdev_get_by_name(g_bdev_name
);
249 SPDK_ERRLOG("bdev %s not found\n", g_bdev_name
);
253 g_bs_dev
= spdk_bdev_create_bs_dev(bdev
, NULL
, NULL
);
255 printf("Mounting BlobFS on bdev %s\n", spdk_bdev_get_name(bdev
));
259 start_fuse_fn(void *arg1
, void *arg2
)
261 struct fuse_args args
= FUSE_ARGS_INIT(g_fuse_argc
, g_fuse_argv
);
263 struct fuse_cmdline_opts opts
= {};
265 g_fuse_thread
= pthread_self();
266 rc
= fuse_parse_cmdline(&args
, &opts
);
269 fuse_opt_free_args(&args
);
272 g_fuse
= fuse_new(&args
, &spdk_fuse_oper
, sizeof(spdk_fuse_oper
), NULL
);
273 fuse_opt_free_args(&args
);
275 rc
= fuse_mount(g_fuse
, g_mountpoint
);
281 fuse_daemonize(true /* true = run in foreground */);
285 fuse_unmount(g_fuse
);
286 fuse_destroy(g_fuse
);
290 init_cb(void *ctx
, struct spdk_filesystem
*fs
, int fserrno
)
292 struct spdk_event
*event
;
295 g_channel
= spdk_fs_alloc_io_channel_sync(g_fs
);
296 event
= spdk_event_allocate(1, start_fuse_fn
, NULL
, NULL
);
297 spdk_event_call(event
);
301 spdk_fuse_run(void *arg1
, void *arg2
)
304 spdk_fs_load(g_bs_dev
, __send_request
, init_cb
, NULL
);
308 shutdown_cb(void *ctx
, int fserrno
)
310 fuse_session_exit(fuse_get_session(g_fuse
));
311 pthread_kill(g_fuse_thread
, SIGINT
);
312 spdk_fs_free_io_channel(g_channel
);
317 spdk_fuse_shutdown(void)
319 spdk_fs_unload(g_fs
, shutdown_cb
, NULL
);
322 int main(int argc
, char **argv
)
324 struct spdk_app_opts opts
= {};
328 fprintf(stderr
, "usage: %s <conffile> <bdev name> <mountpoint>\n", argv
[0]);
332 spdk_app_opts_init(&opts
);
333 opts
.name
= "spdk_fuse";
334 opts
.config_file
= argv
[1];
335 opts
.reactor_mask
= "0x3";
336 opts
.mem_size
= 6144;
337 opts
.shutdown_cb
= spdk_fuse_shutdown
;
339 g_bdev_name
= argv
[2];
340 g_mountpoint
= argv
[3];
341 g_fuse_argc
= argc
- 2;
342 g_fuse_argv
= &argv
[2];
344 rc
= spdk_app_start(&opts
, spdk_fuse_run
, NULL
, NULL
);