经过上一篇的分析,目前已经知道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. Compose

    安装与卸载 Compose 支持 Linux.macOS.Windows 10 三大平台. Compose 可以通过 Python 的包管理工具 pip 进行安装,也可以直接下载编译好的二进制文件使用 ...

  2. python入门示例程序

    该实例是raspi和dsp电机运动控制板的串口uart通信: import serial class SerialHandler(): ''' raspi serial for communicati ...

  3. django : related_name and related_query_name

    This post is about two Django ForeignKey parameters related_name related_query_name See an example b ...

  4. Matlab多项式拟合測试

    x=0:0.2:4; %生成等差数列 rnd=rand(1,size(x,2))*5; %生成一组随机数 y=x.*x.*x+x.*x+6+rnd; %生成y=x^3+x^2+6函数在垂直方向5个尺度 ...

  5. Bootstrap全局CSS样式之button和图片

    .btn-default--button的默认样式. .btn-primary--button的首选样式: .btn-success--button的成功样式: .btn-info--button的一 ...

  6. JAVA_MyEclipse如何加载Tomcat

          注意Tomcat不要放到Program Files这种有空格的路径下面!,下图所示是错误的      

  7. stl之multiset容器的应用

    与set集合容器一样,multiset多重集合容器也使用红黑树组织元素数据,仅仅是multiset容器同意将反复的元素健值插入.而set容器则不同意. set容器所使用的C++标准头文件set.事实上 ...

  8. linux操作系统下查看某rpm包是32bit 还是x64bit的命令

    [root@hosta ~]# rpm -qa --queryformat "%{NAME}-%{VERSION}-%{RELEASE} (%{ARCH})\n" | grep l ...

  9. weex splash页面

    1.Splash.vue <!-- splash页面 --> <template> <div class="wrap" @focus="ro ...

  10. cin,和几个get函数的用法

    1.cin.get(字符变量名):用来接收字符 ch = cin.get(); cin.get(ch); 以上两者均可以 2.cin.get(字符数组名,接收字符数目)用来接收一行字符串,可以接收空格 ...