LCOV - code coverage report
Current view: top level - module/blobfs/bdev - blobfs_fuse.c (source / functions) Hit Total Coverage
Test: ut_cov_unit.info Lines: 0 164 0.0 %
Date: 2024-11-18 14:10:48 Functions: 0 19 0.0 %

          Line data    Source code
       1             : /*   SPDX-License-Identifier: BSD-3-Clause
       2             :  *   Copyright (C) 2019 Intel Corporation.
       3             :  *   All rights reserved.
       4             :  */
       5             : 
       6             : #include "spdk/stdinc.h"
       7             : 
       8             : #include "spdk/log.h"
       9             : #include "spdk/env.h"
      10             : #include "spdk/event.h"
      11             : #include "spdk/thread.h"
      12             : #include "spdk/string.h"
      13             : #include "spdk/blobfs.h"
      14             : 
      15             : #include "blobfs_fuse.h"
      16             : 
      17             : #define FUSE_USE_VERSION 30
      18             : #include "fuse3/fuse.h"
      19             : #include "fuse3/fuse_lowlevel.h"
      20             : 
      21             : struct spdk_blobfs_fuse {
      22             :         char *bdev_name;
      23             :         char *mountpoint;
      24             :         struct spdk_fs_thread_ctx *channel;
      25             :         struct spdk_filesystem *fs;
      26             : 
      27             :         struct fuse *fuse_handle;
      28             :         pthread_t       fuse_tid;
      29             : 
      30             :         blobfs_fuse_unmount_cb cb_fn;
      31             :         void *cb_arg;
      32             : };
      33             : 
      34             : /* Each thread serves one blobfs */
      35             : static __thread struct spdk_blobfs_fuse *thd_bfuse;
      36             : 
      37             : static void
      38           0 : blobfs_fuse_free(struct spdk_blobfs_fuse *bfuse)
      39             : {
      40           0 :         if (bfuse == NULL) {
      41           0 :                 return;
      42             :         }
      43             : 
      44           0 :         free(bfuse->bdev_name);
      45           0 :         free(bfuse->mountpoint);
      46           0 :         free(bfuse);
      47           0 : }
      48             : 
      49             : static void
      50           0 : __call_fn(void *arg1, void *arg2)
      51             : {
      52           0 :         fs_request_fn fn;
      53             : 
      54           0 :         fn = (fs_request_fn)arg1;
      55           0 :         fn(arg2);
      56           0 : }
      57             : 
      58             : void
      59           0 : blobfs_fuse_send_request(fs_request_fn fn, void *arg)
      60             : {
      61           0 :         struct spdk_event *event;
      62             : 
      63           0 :         event = spdk_event_allocate(0, __call_fn, (void *)fn, arg);
      64           0 :         spdk_event_call(event);
      65           0 : }
      66             : 
      67             : static int
      68           0 : fuse_getattr(const char *path, struct stat *stbuf, struct fuse_file_info *fi)
      69             : {
      70           0 :         struct spdk_file_stat stat;
      71           0 :         int rc;
      72             : 
      73           0 :         if (!strcmp(path, "/")) {
      74           0 :                 stbuf->st_mode = S_IFDIR | 0755;
      75           0 :                 stbuf->st_nlink = 2;
      76           0 :                 return 0;
      77             :         }
      78             : 
      79           0 :         rc = spdk_fs_file_stat(thd_bfuse->fs, thd_bfuse->channel, path, &stat);
      80           0 :         if (rc == 0) {
      81           0 :                 stbuf->st_mode = S_IFREG | 0644;
      82           0 :                 stbuf->st_nlink = 1;
      83           0 :                 stbuf->st_size = stat.size;
      84           0 :         }
      85             : 
      86           0 :         return rc;
      87           0 : }
      88             : 
      89             : static int
      90           0 : fuse_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
      91             :              off_t offset, struct fuse_file_info *fi,
      92             :              enum fuse_readdir_flags flags)
      93             : {
      94           0 :         struct spdk_file *file;
      95           0 :         const char *filename;
      96           0 :         spdk_fs_iter iter;
      97             : 
      98           0 :         filler(buf, ".", NULL, 0, 0);
      99           0 :         filler(buf, "..", NULL, 0, 0);
     100             : 
     101           0 :         iter = spdk_fs_iter_first(thd_bfuse->fs);
     102           0 :         while (iter != NULL) {
     103           0 :                 file = spdk_fs_iter_get_file(iter);
     104           0 :                 iter = spdk_fs_iter_next(iter);
     105           0 :                 filename = spdk_file_get_name(file);
     106           0 :                 filler(buf, &filename[1], NULL, 0, 0);
     107             :         }
     108             : 
     109           0 :         return 0;
     110           0 : }
     111             : 
     112             : static int
     113           0 : fuse_mknod(const char *path, mode_t mode, dev_t rdev)
     114             : {
     115           0 :         return spdk_fs_create_file(thd_bfuse->fs, thd_bfuse->channel, path);
     116             : }
     117             : 
     118             : static int
     119           0 : fuse_unlink(const char *path)
     120             : {
     121           0 :         return spdk_fs_delete_file(thd_bfuse->fs, thd_bfuse->channel, path);
     122             : }
     123             : 
     124             : static int
     125           0 : fuse_truncate(const char *path, off_t size, struct fuse_file_info *fi)
     126             : {
     127           0 :         struct spdk_file *file;
     128           0 :         int rc;
     129             : 
     130           0 :         rc = spdk_fs_open_file(thd_bfuse->fs, thd_bfuse->channel, path, 0, &file);
     131           0 :         if (rc != 0) {
     132           0 :                 return -rc;
     133             :         }
     134             : 
     135           0 :         rc = spdk_file_truncate(file, thd_bfuse->channel, size);
     136           0 :         if (rc != 0) {
     137           0 :                 return -rc;
     138             :         }
     139             : 
     140           0 :         spdk_file_close(file, thd_bfuse->channel);
     141             : 
     142           0 :         return 0;
     143           0 : }
     144             : 
     145             : static int
     146           0 : fuse_utimens(const char *path, const struct timespec tv[2], struct fuse_file_info *fi)
     147             : {
     148           0 :         return 0;
     149             : }
     150             : 
     151             : static int
     152           0 : fuse_open(const char *path, struct fuse_file_info *info)
     153             : {
     154           0 :         struct spdk_file *file;
     155           0 :         int rc;
     156             : 
     157           0 :         rc = spdk_fs_open_file(thd_bfuse->fs, thd_bfuse->channel, path, 0, &file);
     158           0 :         if (rc != 0) {
     159           0 :                 return -rc;
     160             :         }
     161             : 
     162           0 :         info->fh = (uintptr_t)file;
     163           0 :         return 0;
     164           0 : }
     165             : 
     166             : static int
     167           0 : fuse_release(const char *path, struct fuse_file_info *info)
     168             : {
     169           0 :         struct spdk_file *file = (struct spdk_file *)info->fh;
     170             : 
     171           0 :         return spdk_file_close(file, thd_bfuse->channel);
     172           0 : }
     173             : 
     174             : static int
     175           0 : fuse_read(const char *path, char *buf, size_t len, off_t offset, struct fuse_file_info *info)
     176             : {
     177           0 :         struct spdk_file *file = (struct spdk_file *)info->fh;
     178             : 
     179           0 :         return spdk_file_read(file, thd_bfuse->channel, buf, offset, len);
     180           0 : }
     181             : 
     182             : static int
     183           0 : fuse_write(const char *path, const char *buf, size_t len, off_t offset,
     184             :            struct fuse_file_info *info)
     185             : {
     186           0 :         struct spdk_file *file = (struct spdk_file *)info->fh;
     187           0 :         int rc;
     188             : 
     189           0 :         rc = spdk_file_write(file, thd_bfuse->channel, (void *)buf, offset, len);
     190           0 :         if (rc == 0) {
     191           0 :                 return len;
     192             :         } else {
     193           0 :                 return rc;
     194             :         }
     195           0 : }
     196             : 
     197             : static int
     198           0 : fuse_flush(const char *path, struct fuse_file_info *info)
     199             : {
     200           0 :         return 0;
     201             : }
     202             : 
     203             : static int
     204           0 : fuse_fsync(const char *path, int datasync, struct fuse_file_info *info)
     205             : {
     206           0 :         return 0;
     207             : }
     208             : 
     209             : static int
     210           0 : fuse_rename(const char *old_path, const char *new_path, unsigned int flags)
     211             : {
     212           0 :         return spdk_fs_rename_file(thd_bfuse->fs, thd_bfuse->channel, old_path, new_path);
     213             : }
     214             : 
     215             : static struct fuse_operations spdk_fuse_oper = {
     216             :         .getattr        = fuse_getattr,
     217             :         .readdir        = fuse_readdir,
     218             :         .mknod          = fuse_mknod,
     219             :         .unlink         = fuse_unlink,
     220             :         .truncate       = fuse_truncate,
     221             :         .utimens        = fuse_utimens,
     222             :         .open           = fuse_open,
     223             :         .release        = fuse_release,
     224             :         .read           = fuse_read,
     225             :         .write          = fuse_write,
     226             :         .flush          = fuse_flush,
     227             :         .fsync          = fuse_fsync,
     228             :         .rename         = fuse_rename,
     229             : };
     230             : 
     231             : static void *
     232           0 : fuse_loop_new_thread(void *arg)
     233             : {
     234           0 :         struct spdk_blobfs_fuse *bfuse = arg;
     235             : 
     236           0 :         spdk_unaffinitize_thread();
     237             : 
     238           0 :         thd_bfuse = bfuse;
     239           0 :         SPDK_NOTICELOG("Start to loop blobfs on bdev %s mounted at %s\n", bfuse->bdev_name,
     240             :                        bfuse->mountpoint);
     241             : 
     242           0 :         bfuse->channel = spdk_fs_alloc_thread_ctx(bfuse->fs);
     243             : 
     244           0 :         fuse_loop(bfuse->fuse_handle);
     245           0 :         fuse_unmount(bfuse->fuse_handle);
     246           0 :         fuse_destroy(bfuse->fuse_handle);
     247           0 :         SPDK_NOTICELOG("Blobfs on bdev %s unmounted from %s\n", bfuse->bdev_name, bfuse->mountpoint);
     248             : 
     249           0 :         spdk_fs_free_thread_ctx(bfuse->channel);
     250             : 
     251           0 :         bfuse->cb_fn(bfuse->cb_arg);
     252             : 
     253           0 :         blobfs_fuse_free(bfuse);
     254             : 
     255           0 :         pthread_exit(NULL);
     256             : }
     257             : 
     258             : int
     259           0 : blobfs_fuse_start(const char *bdev_name, const char *mountpoint, struct spdk_filesystem *fs,
     260             :                   blobfs_fuse_unmount_cb cb_fn, void *cb_arg, struct spdk_blobfs_fuse **_bfuse)
     261             : {
     262             :         /* Set argv[1] as bdev_name in order to show bdev_name as the mounting source */
     263           0 :         char *argv[1] = {(char *)bdev_name};
     264           0 :         struct fuse_args args = FUSE_ARGS_INIT(1, argv);
     265           0 :         struct fuse_cmdline_opts opts = {};
     266           0 :         struct fuse *fuse_handle;
     267           0 :         struct spdk_blobfs_fuse *bfuse;
     268           0 :         pthread_t tid;
     269           0 :         int rc;
     270             : 
     271           0 :         bfuse = (struct spdk_blobfs_fuse *)calloc(1, sizeof(*bfuse));
     272           0 :         if (bfuse == NULL) {
     273           0 :                 return -ENOMEM;
     274             :         }
     275             : 
     276           0 :         bfuse->bdev_name = strdup(bdev_name);
     277           0 :         bfuse->mountpoint = strdup(mountpoint);
     278           0 :         if (!bfuse->bdev_name || !bfuse->mountpoint) {
     279           0 :                 rc = -ENOMEM;
     280           0 :                 goto err;
     281             :         }
     282           0 :         bfuse->fs = fs;
     283           0 :         bfuse->cb_fn = cb_fn;
     284           0 :         bfuse->cb_arg = cb_arg;
     285             : 
     286           0 :         rc = fuse_parse_cmdline(&args, &opts);
     287           0 :         assert(rc == 0);
     288             : 
     289           0 :         fuse_handle = fuse_new(&args, &spdk_fuse_oper, sizeof(spdk_fuse_oper), NULL);
     290           0 :         fuse_opt_free_args(&args);
     291           0 :         if (fuse_handle == NULL) {
     292           0 :                 SPDK_ERRLOG("could not create fuse handle!\n");
     293           0 :                 rc = -1;
     294           0 :                 goto err;
     295             :         }
     296           0 :         bfuse->fuse_handle = fuse_handle;
     297             : 
     298           0 :         rc = fuse_mount(bfuse->fuse_handle, bfuse->mountpoint);
     299           0 :         if (rc != 0) {
     300           0 :                 SPDK_ERRLOG("could not mount fuse handle\n");
     301           0 :                 rc = -1;
     302           0 :                 goto err;
     303             :         }
     304             : 
     305           0 :         rc = pthread_create(&tid, NULL, fuse_loop_new_thread, bfuse);
     306           0 :         if (rc != 0) {
     307           0 :                 SPDK_ERRLOG("could not create thread: %s\n", spdk_strerror(rc));
     308           0 :                 rc = -rc;
     309           0 :                 goto err;
     310             :         }
     311           0 :         bfuse->fuse_tid = tid;
     312             : 
     313           0 :         rc = pthread_detach(tid);
     314           0 :         if (rc != 0) {
     315           0 :                 SPDK_ERRLOG("could not detach thread for fuse loop thread: %s\n", spdk_strerror(rc));
     316           0 :                 rc = -rc;
     317           0 :                 goto err;
     318             :         }
     319             : 
     320           0 :         *_bfuse = bfuse;
     321           0 :         return 0;
     322             : 
     323             : err:
     324           0 :         blobfs_fuse_free(bfuse);
     325             : 
     326           0 :         return rc;
     327           0 : }
     328             : 
     329             : void
     330           0 : blobfs_fuse_stop(struct spdk_blobfs_fuse *bfuse)
     331             : {
     332           0 :         if (bfuse) {
     333           0 :                 fuse_session_exit(fuse_get_session(bfuse->fuse_handle));
     334           0 :                 pthread_kill(bfuse->fuse_tid, SIGINT);
     335           0 :         }
     336           0 : }

Generated by: LCOV version 1.15