经过上一篇的分析,目前已经知道mount函数最终进入到mount.c 中的 int fuse_kern_mount(const char *mountpoint, struct fuse_args *args)

而主题函数进入到fuse.c中的 fuse_new_common

这两个函数都会在helper.c中的fuse_setup_common中返回,返回后进入helper.c的 fuse_daemonize 。fuse_daemonize使用 foreground参数也即-f参数。

如果-f参数为真,则进入fuse_daemonize的循环体,否则fuse_daemonize直接返回,然后fuse_setup_common也返回到fuse_main_common,进入fuse_loop_mt循环。

本篇主要研究fuse_kern_mount函数。

1. 该函数调用栈:

    fuse_setup_common->

        fuse_mount_common(const char *mountpoint,struct fuse_args *args) ->

         fuse_mount_compat25(const char *mountpoint, struct fuse_args *args)  ->

        fuse_kern_mount(const char *mountpoint, struct fuse_args *args)

2.  mountpoint就是命令行提供的目录参数。args也是根据命令行的参数生成的,struct fuse_args args = FUSE_ARGS_INIT(argc, argv);

#define FUSE_ARGS_INIT(argc, argv) { argc, argv, 0 }

在fuse_opt.h

struct fuse_args {
  /** Argument count */
  int argc;

  /** Argument vector. NULL terminated */
  char **argv;

  /** Is 'argv' allocated? */
  int allocated;
};

也在fuse_opt.h

注意,在fuse_setup_common中,将命令行的参数进行了规整,处理出mountpoint和args。mountpoint是挂载点的全路径字符串. 如果命令行是./ssfs dir1 -f , args含有两个字符串,第一个字符串是 ./ssfs,第二个字符串是-osubtype=ssfs

3. fuse_kern_mount首先构建struct mount_opts mo;

struct mount_opts {
int allow_other;
int allow_root;
int ishelp;
int flags;
int nonempty;
int auto_unmount;
int blkdev;
char *fsname;
char *subtype;
char *subtype_opt;
char *mtab_opts;

char *fusermount_opts;
char *kernel_opts;
};

然后调用 fuse_mount_sys(mountpoint, &mo, mnt_opts);函数,这里因为 mo->auto_unmount为真,该函数将返回-2,即fallback to fusermount ,

转而调用fuse_mount_fusermount,这里会调用第一个,并返回3.

fuse_mount_fusermount(mountpoint, &mo, tmp_opts, 1);

注意:fuse_mount_sys中主要是open了/dev/fuse以及使用了mount系统调用。 fuse_mount_fusermount中主要使用 socketpair系统调用。

注意2:fuse_mount_fusermount使用了fork,然后子线程做了一系列操作后_exit了。一个重要操作是 exec_fusermount(argv); argv里包含mountpint以及mount程序名即fusermount。

static void exec_fusermount(const char *argv[])
{
  execv(FUSERMOUNT_DIR "/" FUSERMOUNT_PROG, (char **) argv);
  execvp(FUSERMOUNT_PROG, (char **) argv);
}

FUSERMOUNT_DIR打印字符串显示的是 /usr/local/bin

FUSERMOUNT_DIR "/" FUSERMOUNT_PROG打印字符串显示的是/usr/local/bin/fusermount

原来c语言里将几个字符串直接放在一起就是连起来!

从这里看来,mount函数最终调用了 fusermount程序,一个自带main函数的程序。该程序的源代码就是 util目录下的fusermount.c

注意:后入如果需要打印到屏幕,需要在fuse_mount_fusermount注销掉改变sdio的代码。

4. fusermount.c最终调用了系统调用mount。

调用栈:

fusermount.c:main ->

  mount_fuse(const char *mnt, const char *opts) ->

  do_mount(const char *mnt, char **typep, mode_t rootmode,

      int fd, const char *opts, const char *dev, char **sourcep, char **mnt_optsp, off_t rootsize) ->

  mount(source, mnt, type, flags, optbuf).

在mount_fuse向do_mount传递mnt参数之前,调用了check_perm函数,该函数使用chdir将当前的工作路径改为mnt,然后使用"."即当前路径作为mnt。

后面的几个函数均使用"."作为mnt。

问题:自己写的程序按照上述的方式使用root调用mount,会出现errno=22即EINVAL source had an invalid superblock. 那么,为什么fuse就可以这样掉用mount呢?

经验证,在fuse的mount调用中,可以随意改变source,type,flags变量。改变source和type有时会反映到mount列表,有时却不会。

  也可以随便改变mnt变量,但是必须是一个存在可访问的路径,改变mnt变量不会反映到mount列表。

注意:貌似这里的mount返回了-1,errno为19表示filesystemtype not configured in the kernel. 但是不影响程序正确执行,而且,strace监测不到这个mount。继续发现,只要type里设置了fuse或fuse.xx就返回0,source可随意设置。 strace追踪不到的原因是mount在fork的子程序里运行的。

如果使用strace -f可以追中到子进程的mount,然而结果显示Operation not permitted。

使用sudo的话,实际运行的mount代码是fuse_mount_sys函数里的。

fuse的mount机制 2 -系统调用mount的更多相关文章

  1. fuse的mount机制-流程及参数

    在bbfs中,传递的参数有两个目录,fuse将一个目录挂载在另一个目录下. 在ssfs中,传递的参数只有一个目录(传递两个目录fuse会出错). 问题:那么fuse的mount机制到底需要几个目录参数 ...

  2. mount机制3-/etc/mtab

    这次查看fuse_mount_sys函数的执行过程,理解mount的各个阶段. 这个函数能够执行的前提是命令行使用root账户. 1. 首先,该函数仍然是主要使用 mount(const char * ...

  3. linux系统调用mount全过程分析【转】

    本文转载自:https://blog.csdn.net/skyflying2012/article/details/9748133 系统调用本身是软中断,使用系统调用,内核也陷入内核态,异常处理,找到 ...

  4. [mount]linux 挂载时 mount: wrong fs type, bad option, bad superblock on /dev/sdb

    原因:挂载时未格式化,使用的文件系统格式不对 解决方案:格式化 sudo mkfs -t ext4 /dev/sdb 再挂载 sudo mount /dev/sdb /xxx/ 用df -h检查,发现 ...

  5. Linux文件系统的设计

    总论: linux的文件系统设计非常优秀,总的来讲有两大部分,第一部分就是树形的组织结构,第二部分就是vfs,树形的组织结构组织了文件系统的表象,用户非常方便的使用,而vfs是文件系统的实现机理,它处 ...

  6. linux文件系统调用(1)---mount

    术语表: struct mount:挂载点 struct mountpoint:挂载点节点 struct vfsmount:挂载项 源文件系统:用户将要挂载的文件系统 目的文件系统:挂载源文件系统的文 ...

  7. mount源码分析 【转】

    转自:http://blog.chinaunix.net/uid-10769062-id-3230811.html Busybox- 在util-linux/mount.c的line:1609行首先映 ...

  8. mount

    产品,平台,RS6000, pseries 软件版本, aix 当NFS在NFS客户端加载时,系统会问是使用 soft-mount 还是hard-mount, 它们之间有什么区别? 它们的区别在于当发 ...

  9. mount nfs的可选参数

    mount nfs的可选参数:HARD mount和SOFT MOUNT:HARD:NFS CLIENT会不断的尝试与SERVER的连接(在后台,不会给出任何提示信息,在LINUX下有的版本仍然会给出 ...

随机推荐

  1. 关于整合spring+mybatis 第二种方式

    和第一种方式一样的步骤,不过bean.xml中有些许差异 <!-- 配置sqlSessionFactory --> <bean id="sqlSessionFactory& ...

  2. Working with multiple environments

    ASP.NET Core引入了对多个环境(例如开发,暂存和生产环境)的支持. 可以用环境变量来指示应用程序正在运行的环境,从而让app来做相应的配置. Development, Staging, Pr ...

  3. 快速掌握RabbitMQ(四)——两种消费模式和QOS的C#实现

    本篇介绍一下RabbitMQ中的消费模式,在前边的所有栗子中我们采用的消费者都是EventingBasicConsumer,其实RabbitMQ中还有其他两种消费模式:BasicGet和QueueBa ...

  4. rsync故障排查整理

    1.客户端的错误现象:No route to host rsync服务端开启了iptables防火墙 rsync -avz /etc/hosts rsync_backup@172.16.1.41::b ...

  5. Junit4 断言新方法

    话不多少说,直接上代码 package ASSERTTEST; import org.junit.Assert; import org.hamcrest.*;import org.junit.Test ...

  6. ASP.NET Core 如何记录每次响应的Response信息 - sky 胡萝卜星星 - CSDN博客

    原文:ASP.NET Core 如何记录每次响应的Response信息 - sky 胡萝卜星星 - CSDN博客 上一篇文章中我们已经成功的记录了Request部分的信息,现在我们来看下如何记录Res ...

  7. kafka的安装和使用;kafka常用操作命令

    kafka:基于发布/订阅的分布式消息系统.数据管道:最初用来记录活动数据--包括页面访问量(Page View).被查看内容方面的信息以及搜索情况等内容和运营数据--服务器的性能数据(CPU.IO使 ...

  8. Intent传递简单对象与集合

    我们在Intent传递传递对象.能够有三种方式,实现Serializable接口.实现Parcelable接口,使用json格式序列化与反序列化. 在此我们使用第二方式,现实Parcelable接口, ...

  9. UVA 1482 - Playing With Stones(SG打表规律)

    UVA 1482 - Playing With Stones 题目链接 题意:给定n堆石头,每次选一堆取至少一个.不超过一半的石子,最后不能取的输,问是否先手必胜 思路:数值非常大.无法直接递推sg函 ...

  10. [Cypress] install, configure, and script Cypress for JavaScript web applications -- part2

    Use Cypress to test user registration Let’s write a test to fill out our registration form. Because ...