]> git.proxmox.com Git - ceph.git/blame - ceph/src/spdk/test/lib/blobfs/fuse/fuse.c
bump version to 12.2.12-pve1
[ceph.git] / ceph / src / spdk / test / lib / blobfs / fuse / fuse.c
CommitLineData
7c673cae
FG
1/*-
2 * BSD LICENSE
3 *
4 * Copyright (c) Intel Corporation.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 *
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
16 * distribution.
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.
20 *
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.
32 */
33#define FUSE_USE_VERSION 30
34
35#include "fuse3/fuse.h"
36#include "fuse3/fuse_lowlevel.h"
37
38#include <stdlib.h>
39#include <unistd.h>
40#include <errno.h>
41#include <stdio.h>
42#include <string.h>
43#include <pthread.h>
44#include <stdbool.h>
45#include <signal.h>
46
47#include "spdk/blobfs.h"
48#include "spdk/bdev.h"
49#include "spdk/event.h"
50#include "spdk/io_channel.h"
51#include "spdk/blob_bdev.h"
52#include "spdk/log.h"
53
54struct fuse *g_fuse;
55char *g_bdev_name;
56char *g_mountpoint;
57pthread_t g_fuse_thread;
58
59struct spdk_bs_dev *g_bs_dev;
60struct spdk_filesystem *g_fs;
61struct spdk_io_channel *g_channel;
62struct spdk_file *g_file;
63int g_fserrno;
64int g_fuse_argc = 0;
65char **g_fuse_argv = NULL;
66
67static void
68__call_fn(void *arg1, void *arg2)
69{
70 fs_request_fn fn;
71
72 fn = (fs_request_fn)arg1;
73 fn(arg2);
74}
75
76static void
77__send_request(fs_request_fn fn, void *arg)
78{
79 struct spdk_event *event;
80
81 event = spdk_event_allocate(0, __call_fn, (void *)fn, arg);
82 spdk_event_call(event);
83}
84
85static int
86spdk_fuse_getattr(const char *path, struct stat *stbuf, struct fuse_file_info *fi)
87{
88 struct spdk_file_stat stat;
89 int rc;
90
91 if (!strcmp(path, "/")) {
92 stbuf->st_mode = S_IFDIR | 0755;
93 stbuf->st_nlink = 2;
94 return 0;
95 }
96
97 rc = spdk_fs_file_stat(g_fs, g_channel, &path[1], &stat);
98 if (rc == 0) {
99 stbuf->st_mode = S_IFREG | 0644;
100 stbuf->st_nlink = 1;
101 stbuf->st_size = stat.size;
102 }
103
104 return rc;
105}
106
107static int
108spdk_fuse_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
109 off_t offset, struct fuse_file_info *fi,
110 enum fuse_readdir_flags flags)
111{
112 struct spdk_file *file;
113 const char *filename;
114 spdk_fs_iter iter;
115
116 filler(buf, ".", NULL, 0, 0);
117 filler(buf, "..", NULL, 0, 0);
118
119 iter = spdk_fs_iter_first(g_fs);
120 while (iter != NULL) {
121 file = spdk_fs_iter_get_file(iter);
122 iter = spdk_fs_iter_next(iter);
123 filename = spdk_file_get_name(file);
124 filler(buf, filename, NULL, 0, 0);
125 }
126
127 return 0;
128}
129
130static int
131spdk_fuse_mknod(const char *path, mode_t mode, dev_t rdev)
132{
133 return spdk_fs_create_file(g_fs, g_channel, &path[1]);
134}
135
136static int
137spdk_fuse_unlink(const char *path)
138{
139 return spdk_fs_delete_file(g_fs, g_channel, &path[1]);
140}
141
142static int
143spdk_fuse_truncate(const char *path, off_t size, struct fuse_file_info *fi)
144{
145 struct spdk_file *file;
146 int rc;
147
148 rc = spdk_fs_open_file(g_fs, g_channel, &path[1], 0, &file);
149 if (rc != 0) {
150 return -rc;
151 }
152
153 spdk_file_truncate(file, g_channel, size);
154 spdk_file_close(file, g_channel);
155
156 return 0;
157}
158
159static int
160spdk_fuse_utimens(const char *path, const struct timespec tv[2], struct fuse_file_info *fi)
161{
162 return 0;
163}
164
165static int
166spdk_fuse_open(const char *path, struct fuse_file_info *info)
167{
168 struct spdk_file *file;
169 int rc;
170
171 rc = spdk_fs_open_file(g_fs, g_channel, &path[1], 0, &file);
172 if (rc != 0) {
173 return -rc;
174 }
175
176 info->fh = (uintptr_t)file;
177 return 0;
178}
179
180static int
181spdk_fuse_release(const char *path, struct fuse_file_info *info)
182{
183 struct spdk_file *file = (struct spdk_file *)info->fh;
184
185 return spdk_file_close(file, g_channel);
186}
187
188static int
189spdk_fuse_read(const char *path, char *buf, size_t len, off_t offset, struct fuse_file_info *info)
190{
191 struct spdk_file *file = (struct spdk_file *)info->fh;
192
193 return spdk_file_read(file, g_channel, buf, offset, len);
194}
195
196static int
197spdk_fuse_write(const char *path, const char *buf, size_t len, off_t offset,
198 struct fuse_file_info *info)
199{
200 struct spdk_file *file = (struct spdk_file *)info->fh;
201 int rc;
202
203 rc = spdk_file_write(file, g_channel, (void *)buf, offset, len);
204 if (rc == 0) {
205 return len;
206 } else {
207 return rc;
208 }
209}
210
211static int
212spdk_fuse_flush(const char *path, struct fuse_file_info *info)
213{
214 return 0;
215}
216
217static int
218spdk_fuse_fsync(const char *path, int datasync, struct fuse_file_info *info)
219{
220 return 0;
221}
222
223static int
224spdk_fuse_rename(const char *old_path, const char *new_path, unsigned int flags)
225{
226 return spdk_fs_rename_file(g_fs, g_channel, &old_path[1], &new_path[1]);
227}
228
229static struct fuse_operations spdk_fuse_oper = {
230 .getattr = spdk_fuse_getattr,
231 .readdir = spdk_fuse_readdir,
232 .mknod = spdk_fuse_mknod,
233 .unlink = spdk_fuse_unlink,
234 .truncate = spdk_fuse_truncate,
235 .utimens = spdk_fuse_utimens,
236 .open = spdk_fuse_open,
237 .release = spdk_fuse_release,
238 .read = spdk_fuse_read,
239 .write = spdk_fuse_write,
240 .flush = spdk_fuse_flush,
241 .fsync = spdk_fuse_fsync,
242 .rename = spdk_fuse_rename,
243};
244
245static void
246construct_targets(void)
247{
248 struct spdk_bdev *bdev;
249
250 bdev = spdk_bdev_get_by_name(g_bdev_name);
251 if (bdev == NULL) {
252 SPDK_ERRLOG("bdev %s not found\n", g_bdev_name);
253 exit(1);
254 }
255
256 if (!spdk_bdev_claim(bdev, NULL, NULL)) {
257 SPDK_ERRLOG("could not claim bdev %s\n", bdev->name);
258 exit(1);
259 }
260
261 g_bs_dev = spdk_bdev_create_bs_dev(bdev);
262
263 printf("Mounting BlobFS on bdev %s\n", bdev->name);
264}
265
266static void
267start_fuse_fn(void *arg1, void *arg2)
268{
269 struct fuse_args args = FUSE_ARGS_INIT(g_fuse_argc, g_fuse_argv);
270 int rc;
271 struct fuse_cmdline_opts opts = {};
272
273 g_fuse_thread = pthread_self();
274 rc = fuse_parse_cmdline(&args, &opts);
275 if (rc != 0) {
276 spdk_app_stop(-1);
277 fuse_opt_free_args(&args);
278 return;
279 }
280 g_fuse = fuse_new(&args, &spdk_fuse_oper, sizeof(spdk_fuse_oper), NULL);
281 fuse_opt_free_args(&args);
282
283 rc = fuse_mount(g_fuse, g_mountpoint);
284 if (rc != 0) {
285 spdk_app_stop(-1);
286 return;
287 }
288
289 fuse_daemonize(true /* true = run in foreground */);
290
291 fuse_loop(g_fuse);
292
293 fuse_unmount(g_fuse);
294 fuse_destroy(g_fuse);
295}
296
297static void
298init_cb(void *ctx, struct spdk_filesystem *fs, int fserrno)
299{
300 struct spdk_event *event;
301
302 g_fs = fs;
303 g_channel = spdk_fs_alloc_io_channel_sync(g_fs, SPDK_IO_PRIORITY_DEFAULT);
304 event = spdk_event_allocate(1, start_fuse_fn, NULL, NULL);
305 spdk_event_call(event);
306}
307
308static void
309spdk_fuse_run(void *arg1, void *arg2)
310{
311 construct_targets();
312 spdk_fs_load(g_bs_dev, __send_request, init_cb, NULL);
313}
314
315static void
316shutdown_cb(void *ctx, int fserrno)
317{
318 fuse_session_exit(fuse_get_session(g_fuse));
319 pthread_kill(g_fuse_thread, SIGINT);
320 spdk_fs_free_io_channel(g_channel);
321 spdk_app_stop(0);
322}
323
324static void
325spdk_fuse_shutdown(void)
326{
327 spdk_fs_unload(g_fs, shutdown_cb, NULL);
328}
329
330int main(int argc, char **argv)
331{
332 struct spdk_app_opts opts = {};
333
334 if (argc < 4) {
335 fprintf(stderr, "usage: %s <conffile> <bdev name> <mountpoint>\n", argv[0]);
336 exit(1);
337 }
338
339 spdk_app_opts_init(&opts);
340 opts.name = "spdk_fuse";
341 opts.config_file = argv[1];
342 opts.reactor_mask = "0x3";
343 opts.dpdk_mem_size = 6144;
344 opts.shutdown_cb = spdk_fuse_shutdown;
345 spdk_app_init(&opts);
346
347 g_bdev_name = argv[2];
348 g_mountpoint = argv[3];
349 g_fuse_argc = argc - 2;
350 g_fuse_argv = &argv[2];
351
352 spdk_app_start(spdk_fuse_run, NULL, NULL);
353 spdk_app_fini();
354
355 return 0;
356}