FUSE 用户空间文件系统 (Filesystem in Userspace)

FUSE 仓库  Wiki Fuse内核说明文档 FUSE系统IO模式 FUSE 性能评测 # 2022年01月25日18:28:32 修


关于Fuse文件系统:

FUSE (Filesystem in Userspace) is an interface for userspace programs to export a filesystem to the Linux kernel. The FUSE project consists of two components: the fuse kernel module (maintained in the regular kernel repositories) and the libfuseuserspace library (maintained in this repository). libfuse provides the reference implementation for communicating with the FUSE kernel module.

A FUSE file system is typically implemented as a standalone application that links with libfuse. libfuse provides functions to mount the file system, unmount it, read requests from the kernel, and send responses back. libfuse offers two APIs: a "high-level", synchronous API, and a "low-level" asynchronous API. In both cases, incoming requests from the kernel are passed to the main program using callbacks. When using the high-level API, the callbacks may work with file names and paths instead of inodes, and processing of a request finishes when the callback function returns. When using the low-level API, the callbacks must work with inodes and responses must be sent explicitly using a separate set of API functions.


FUSE 宏观调用:

以open为例,整个调用的过程如下:

1- 用户态app调用glibc open接口,触发sys_open系统调用。

2- sys_open 调用fuse中inode节点定义的open方法。

3- inode中open生成一个request消息,并通过/dev/fuse发送request消息到用户态libfuse。

4- Libfuse调用fuse_application用户自定义的open的方法,并将返回值通过/dev/fuse通知给内核。

5- 内核收到request消息的处理完成的唤醒,并将结果放回给VFS系统调用结果。

6- 用户态app收到open的返回结果。

 


  FUSE 包含两个大的发行版本:fuse2 和 fuse3 ,那么fuse2 和 fuse3之间有什么区别呢?(What exactly is the difference between fuse2 and fuse3?


FUSE 安装

FUSE 安装分为两种方式,一种是 编译安装,另外一张是通过包管理软件(YUM)进行安装。编译安装按照官方教程安装即可。

我使用的操作系统是 Fedora 29, 所以尝试使用yum进行安装:

首先使用rpm 命令查一下当前系统是否已经安装了fuse:

[root@docker 4.19.13-300.fc29.x86_64]# rpm -aq | grep fuse
fuse-common-3.2.3-14.fc29.x86_64
fuse-2.9.7-14.fc29.x86_64
fuse-libs-2.9.7-14.fc29.x86_64
glusterfs-fuse-5.2-1.fc29.x86_64
gvfs-fuse-1.38.1-1.fc29.x86_64
zfs-fuse-0.7.2.2-6.fc27.x86_64

可见已经安装好了,当前系统中使用的是fuse的2.9.7版本,并且是有相应的库。所以算是已经安装好了。如果系统中没有上述安装包,可以使用yum进行安装。

[root@docker 4.19.13-300.fc29.x86_64]# yum search fuse
Last metadata expiration check: 1:10:16 ago on Sat 12 Jan 2019 10:13:49 PM CST.
============================================================================ Summary & Name Matched: fuse ============================================================================
fuse.x86_64 : File System in Userspace (FUSE) v2 utilities
fuse.x86_64 : File System in Userspace (FUSE) v2 utilities
fuse-libs.x86_64 : File System in Userspace (FUSE) v2 libraries
fuse-libs.i686 : File System in Userspace (FUSE) v2 libraries
fuse-libs.x86_64 : File System in Userspace (FUSE) v2 libraries
fuse3-libs.i686 : File System in Userspace (FUSE) v3 libraries
fuse3-libs.x86_64 : File System in Userspace (FUSE) v3 libraries
fuse-devel.i686 : File System in Userspace (FUSE) v2 devel files
fuse-devel.x86_64 : File System in Userspace (FUSE) v2 devel files
fuse3-devel.i686 : File System in Userspace (FUSE) v3 devel files
fuse3-devel.x86_64 : File System in Userspace (FUSE) v3 devel files
fuse-common.x86_64 : Common files for File System in Userspace (FUSE) v2 and v3
fuse-common.x86_64 : Common files for File System in Userspace (FUSE) v2 and v3

通过自己的需求,安装对应版本的安装包:fuse-common.x86_64 这个软件包在v2和v3两个版本中都能进行使用。当然你也可以查看包里的内容,进一步了解fuse的组成。

fuse 的内核模块也已经内置到操作系统内核之中了:

[root@docker fuse]# pwd
/lib/modules/4.19.10-300.fc29.x86_64/kernel/fs/fuse
[root@docker fuse]# ls
fuse.ko.xz

为了能够开发属于自己的文件系统,还要安装fuse开发包:

fuse-devel.x86_64 : File System in Userspace (FUSE) v2 devel files
Name         : fuse-devel
Version      : 2.9.7
Release      : 14.fc29
Arch         : x86_64
Size         : 36 k
Source       : fuse-2.9.7-14.fc29.src.rpm
Repo         : fedora
Summary      : File System in Userspace (FUSE) v2 devel files
URL          : http://fuse.sf.net
License      : LGPLv2+
Description  : With FUSE it is possible to implement a fully functional filesystem in a
             : userspace program. This package contains development files (headers,
             : pgk-config) to develop FUSE v2 based applications/filesystems.

我们从包描述可以看到,包中包括了一些开发所需要一些头文件, 动态链接库(.so),pgk-config 文件等。


FUSE 简单使用(DEMO)

 high-level API:

/*
  FUSE: Filesystem in Userspace
  Copyright (C) 2001-2007  Miklos Szeredi <miklos@szeredi.hu>

  
  This program can be distributed under the terms of the GNU GPL.
  See the file COPYING.

  gcc -Wall hello.c `pkg-config fuse --cflags --libs` -o hello
*/

#define FUSE_USE_VERSION 26 //先定义, fuse.h中有判断

#include <fuse.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>

static const char *hello_str = "Hello World!\n";
static const char *hello_path = "/hello";

// 与函数stat()类似,用于得到文件属性,并将其存入到结构体struct stat当中 struct stat *stbuf
static int hello_getattr(const char *path, struct stat *stbuf)
{
    int res = 0;

    memset(stbuf, 0, sizeof(struct stat));  // 使用memset进行初始化结构体
    if (strcmp(path, "/") == 0) {
        stbuf->st_mode = S_IFDIR | 0755; // S_IFDIR 用于说明 / 为目录
        stbuf->st_nlink = 2;
    } else if (strcmp(path, hello_path) == 0) {
        stbuf->st_mode = S_IFREG | 0444; // S_IFREG 用于说明/hello 为常规文件
        stbuf->st_nlink = 1;
        stbuf->st_size = strlen(hello_str); // 设置文件长度为hello_str的长度
    } else
        res = -ENOENT; // 返回错误信息,没有该文件或者目录

    return res; // 成功执行的时候,此函数返回值为 0
}

// 该函数用于读取目录中的内容,并在/目录下增加了. .. hello 三个目录项
static int hello_readdir(const char *path, void *buf, fuse_fill_dir_t filler,
             off_t offset, struct fuse_file_info *fi)
{
    (void) offset;
    (void) fi;

    if (strcmp(path, "/") != 0)
        return -ENOENT;

    /* fill, 其作用是在readdir函数中增加一个目录项
    typedef int (*fuse_fill_dir_t) (void *buf, const char *name,
        const struct stat *stbuf, off_t off);
    */

    filler(buf, ".", NULL, 0);
    filler(buf, "..", NULL, 0);
    filler(buf, hello_path + 1, NULL, 0); //指针+1(/hello), 即增加 hello 目录项,去掉前面的'/'

    return 0;
}

// 打开文件函数
static int hello_open(const char *path, struct fuse_file_info *fi)
{
    if (strcmp(path, hello_path) != 0)
        return -ENOENT;

    if ((fi->flags & 3) != O_RDONLY)
        return -EACCES;

    return 0;
}

// 读文件函数
static int hello_read(const char *path, char *buf, size_t size, off_t offset,
              struct fuse_file_info *fi)
{
    size_t len;
    (void) fi;
    if(strcmp(path, hello_path) != 0)
        return -ENOENT;

    len = strlen(hello_str);
    if (offset < len) {
        if (offset + size > len)
            size = len - offset;
        memcpy(buf, hello_str + offset, size);
    } else
        size = 0;

    return size;
}

// 注册自定义函数
static struct fuse_operations hello_oper = {
    .getattr    = hello_getattr,
    .readdir    = hello_readdir,
    .open        = hello_open,
    .read        = hello_read, // 读文件函数
};

// 调用 fuse_main , 把控制权交给了fuse
int main(int argc, char *argv[])
{
    return fuse_main(argc, argv, &hello_oper, NULL);
}

执行:

挂载:
[root@docker example]# mkdir /tmp/fuse
[root@docker fuse
-example]# ./hello /tmp/fuse/ [root@docker fuse-example]# cd /tmp/fuse/
读取文件属性: [root@docker fuse]# ll total 0 -r--r--r--. 1 root root 13 Jan 1 1970 hello 读取目录属性: [root@docker tmp]# ll /tmp/ total 4 drwxr-xr-x. 2 root root 0 Jan 1 1970 fuse

卸载:
[root@docker tmp]# fusermount -u /tmp/fuse
[root@docker tmp]# ls /tmp/fuse/

当然你也可以修改代码,对fuse的特性进一步尝试。

low-level API: low-level api 相对于 high-level api 来说,有更好的自由度;high-level相对于 low-level api 则有更简单的api使用,如果阅读过fuse源码过后,使用low-level api将使得可控性更强。下面是hello.c 的low-level api 版本

/*
  FUSE: Filesystem in Userspace
  Copyright (C) 2001-2007  Miklos Szeredi <miklos@szeredi.hu>

  This program can be distributed under the terms of the GNU GPL.
  See the file COPYING.

  gcc -Wall hello_ll.c `pkg-config fuse --cflags --libs` -o hello_ll
*/

#define FUSE_USE_VERSION 26

#include <fuse_lowlevel.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <assert.h>

static const char *hello_str = "Hello World!\n";
static const char *hello_name = "hello";

static int hello_stat(fuse_ino_t ino, struct stat *stbuf)
{
    stbuf->st_ino = ino;
    switch (ino) {
    case 1:
        stbuf->st_mode = S_IFDIR | 0755;
        stbuf->st_nlink = 2;
        break;

    case 2:
        stbuf->st_mode = S_IFREG | 0444;
        stbuf->st_nlink = 1;
        stbuf->st_size = strlen(hello_str);
        break;

    default:
        return -1;
    }
    return 0;
}

static void hello_ll_getattr(fuse_req_t req, fuse_ino_t ino,
                 struct fuse_file_info *fi)
{
    struct stat stbuf;

    (void) fi;

    memset(&stbuf, 0, sizeof(stbuf));
    if (hello_stat(ino, &stbuf) == -1)
        fuse_reply_err(req, ENOENT);
    else
        fuse_reply_attr(req, &stbuf, 1.0);
}

static void hello_ll_lookup(fuse_req_t req, fuse_ino_t parent, const char *name)
{
    struct fuse_entry_param e;

    if (parent != 1 || strcmp(name, hello_name) != 0)
        fuse_reply_err(req, ENOENT);
    else {
        memset(&e, 0, sizeof(e));
        e.ino = 2;
        e.attr_timeout = 1.0;
        e.entry_timeout = 1.0;
        hello_stat(e.ino, &e.attr);

        fuse_reply_entry(req, &e);
    }
}

struct dirbuf {
    char *p;
    size_t size;
};

static void dirbuf_add(fuse_req_t req, struct dirbuf *b, const char *name,
               fuse_ino_t ino)
{
    struct stat stbuf;
    size_t oldsize = b->size;
    b->size += fuse_add_direntry(req, NULL, 0, name, NULL, 0);
    b->p = (char *) realloc(b->p, b->size);
    memset(&stbuf, 0, sizeof(stbuf));
    stbuf.st_ino = ino;
    fuse_add_direntry(req, b->p + oldsize, b->size - oldsize, name, &stbuf,
              b->size);
}

#define min(x, y) ((x) < (y) ? (x) : (y))

static int reply_buf_limited(fuse_req_t req, const char *buf, size_t bufsize,
                 off_t off, size_t maxsize)
{
    if (off < bufsize)
        return fuse_reply_buf(req, buf + off,
                      min(bufsize - off, maxsize));
    else
        return fuse_reply_buf(req, NULL, 0);
}

// 读取文件夹目录
static void hello_ll_readdir(fuse_req_t req, fuse_ino_t ino, size_t size,
                 off_t off, struct fuse_file_info *fi)
{
    (void) fi;

    if (ino != 1)
        fuse_reply_err(req, ENOTDIR);
    else {
        struct dirbuf b;

        memset(&b, 0, sizeof(b));
        dirbuf_add(req, &b, ".", 1);
        dirbuf_add(req, &b, "..", 1);
        dirbuf_add(req, &b, hello_name, 2);
        reply_buf_limited(req, b.p, b.size, off, size);
        free(b.p);
    }
}

// 处理打开操作函数,检查权限
static void hello_ll_open(fuse_req_t req, fuse_ino_t ino,
              struct fuse_file_info *fi)
{
    if (ino != 2)
        fuse_reply_err(req, EISDIR);
    else if ((fi->flags & 3) != O_RDONLY)
        fuse_reply_err(req, EACCES);
    else
        fuse_reply_open(req, fi);
}

// 读取函数 返回 hello_str 中的值
static void hello_ll_read(fuse_req_t req, fuse_ino_t ino, size_t size,
              off_t off, struct fuse_file_info *fi)
{
    (void) fi;

    assert(ino == 2);
    reply_buf_limited(req, hello_str, strlen(hello_str), off, size);
}

// 注册的自定义函数
static struct fuse_lowlevel_ops hello_ll_oper = {
    .lookup        = hello_ll_lookup,
    .getattr    = hello_ll_getattr,
    .readdir    = hello_ll_readdir,
    .open        = hello_ll_open,
    .read        = hello_ll_read,
};

int main(int argc, char *argv[])
{
    // FUSE_ARGS_INIT 本身是一个宏函数,用于生成 struct fuse_args 结构体, 结构体在源代码 fuse_opt.h 文件建中定义;
    struct fuse_args args = FUSE_ARGS_INIT(argc, argv); 
    
    // A communication channel, providing hooks for sending and receiving* messages
    struct fuse_chan *ch; // 与 fuse 中 session 相关, 在源代码 fuse_session.c 中进行定义 一个channel 

    char *mountpoint; // 一个字符串指针
    int err = -1; // 错误返回值,默认的返回值为 -1

    // fuse_parse_cmdline 是解析命令行参数的一种工具类,他可以解析fuse本身自身的选项,第一个出现的非选项参数被认为是挂载点
    // 如果出现多个非选项参数,则认为是错误!
    if (fuse_parse_cmdline(&args, &mountpoint, NULL, NULL) != -1 &&
        // 挂载, 调用了底层的 fuse_mount_common 函数
        (ch = fuse_mount(mountpoint, &args)) != NULL) {
        struct fuse_session *se;

        // 创建一个session
        se = fuse_lowlevel_new(&args, &hello_ll_oper,
                       sizeof(hello_ll_oper), NULL);
        if (se != NULL) {
            if (fuse_set_signal_handlers(se) != -1) {
                fuse_session_add_chan(se, ch);
                err = fuse_session_loop(se); // 处理信息
                fuse_remove_signal_handlers(se);
                fuse_session_remove_chan(ch);
            }
            fuse_session_destroy(se);
        }
        fuse_unmount(mountpoint, ch);
    }
    // 释放参数占用的空间
    fuse_opt_free_args(&args);

    return err ? 1 : 0;
}

执行:(同样执行和High-level API 相同的操作进行验证)

上面第二部分代码,在当时书写时因对底层理解较浅,只是进行了简要的注释。后期做项目的时候,也未对 low-level 的api进行使用,所以这里我就不进行补充了。# 2022年01月25日18:33:58

FUSE 文件系统 example部分 源码注释 (libfuse 2.9.9) 

资源来源自网络,保持更新,转载请注明出处。https://www.cnblogs.com/xuyaowen/p/fuse.html

posted @ 2019-01-12 15:52  "Michael_Xu"  阅读(5857)  评论(2编辑  收藏  举报