exfat文件系统无法NFS导出的问题

        最近项目中移植了exfat-linux驱动,但发现exfat格式的U盘无法用exportfs命令在NFS上导出。这篇文章记录了分析、解决方法。

一、问题现象

问题描述:exfat驱动更新后,exfat格式的U盘用exportfs命令NFS导出会报错

$ exportfs -o ro,fsid=0,no_root_squash 192.168.100.74:/storage/9CE5-DFCB/Carlog

报错:"does not support NFS export"


问题原因:

        文件系统要被export必须满足两个条件:第一要有唯一标识,可以是fsid,uuid或者是设备号,第二是文件系统必须设置了export操作集 export_operations。第一个条件可以通过exportfs或在/etc/exports文件中指定fsid的值来满足,第二个条件需要文件系统源代码定义了export_operations

        在exfat-linux驱动源码 fs/exfat/super.c 未定义 export_operations,因此exfat文件系统导出NFS会报错。

二、分析过程及思考

1、exportfs命令介绍

        exportfs是Linux中的一个命令,用于将指定的目录或文件系统在NFS上导出,从而实现文件系统的共享和访问控制,通过NFS协议允许其他计算机通过网络访问共享文件。

具体来说,exportfs的作用包括:

  • 共享文件系统:通过exportfs命令,可以将文件系统或目录共享给其他计算机。这样,其他计算机就可以通过NFS协议挂载该共享文件系统,实现文件的共享和访问。
  • 访问权限控制:exportfs命令可以设置文件系统的访问权限,包括读写权限和只读权限。通过配置exportfs,可以控制哪些计算机可以访问共享文件系统以及访问权限的级别。
  • 配置NFS服务:exportfs命令是NFS服务的一部分,通过该命令可以将指定的目录或文件系统添加到NFS服务的导出列表中,从而启用NFS服务并提供共享。

2、分析过程

        首先看下 linux/include/linux/exportfs.h 文档,其中提到 export_operations,详细使用 export_operations 方法需阅读 Documentation/filesystems/nfs/exporting.rst 文档。

      linux/Documentation/filesystems/nfs/exporting.rst 文档,其中有一段:

A file system implementation declares that instances of the filesystem
are exportable by setting the s_export_op field in the struct
super_block.  This field must point to a "struct export_operations"
struct which has the following members:

 encode_fh  (optional)
    Takes a dentry and creates a filehandle fragment which can later be used
    to find or create a dentry for the same object.  The default
    implementation creates a filehandle fragment that encodes a 32bit inode
    and generation number for the inode encoded, and if necessary the
    same information for the parent.

  fh_to_dentry (mandatory)
    Given a filehandle fragment, this should find the implied object and
    create a dentry for it (possibly with d_obtain_alias).

  fh_to_parent (optional but strongly recommended)
    Given a filehandle fragment, this should find the parent of the
    implied object and create a dentry for it (possibly with
    d_obtain_alias).  May fail if the filehandle fragment is too small.

        文件系统导出功能可通过设置 super_block 中的 s_export_op 字段实现。s_export_op 字段必须指向 "struct export_operations"。

        可以看到,exfat-linux驱动源码 fs/exfat/super.c 并未定义 export_operations,因此NFS导出会失败。

3、解决方法

        将老exfat驱动中 s_export_op 相关代码移植到 exfat-linux驱动源码 fs/exfat/super.c 中,方法如下:

 添加 linux/exportfs.h 头文件

#include <linux/exportfs.h>

定义 struct export_operations 结构体

/*======================================================================*/
/*  Export Operations                                                   */
/*======================================================================*/

static struct inode *exfat_nfs_get_inode(struct super_block *sb,
                       u64 ino, u32 generation)
{
    struct inode *inode = NULL;
    if (ino < EXFAT_ROOT_INO)
        return inode;
    inode = ilookup(sb, ino);

    if (inode && generation && (inode->i_generation != generation)) {
        iput(inode);
        inode = NULL;
    }

    return inode;
}

static struct dentry *exfat_fh_to_dentry(struct super_block *sb, struct fid *fid,
                int fh_len, int fh_type)
{
    return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
                    exfat_nfs_get_inode);
}

static struct dentry *exfat_fh_to_parent(struct super_block *sb, struct fid *fid,
                int fh_len, int fh_type)
{
    return generic_fh_to_parent(sb, fid, fh_len, fh_type,
                    exfat_nfs_get_inode);
}

const struct export_operations exfat_export_ops = {
    .fh_to_dentry   = exfat_fh_to_dentry,
    .fh_to_parent   = exfat_fh_to_parent,
};

         s_export_op 指向 struct export_operations 结构体。exfat_fill_super() 函数中添加 sb->s_export_op = &exfat_export_ops;

static int exfat_fill_super(struct super_block *sb, void *data, int silent)
{
	struct exfat_sb_info *sbi;
	struct exfat_mount_options *opts;
	struct inode *root_inode;
	int err;

	err = exfat_init_sb_info(sb);
	if (err) {
		exfat_err(sb, "failed to initialize superblock info");
		goto failed;
	}

	sbi = sb->s_fs_info;
	opts = &sbi->options;

	sb->s_flags |= SB_NODIRATIME;
	sb->s_magic = EXFAT_SUPER_MAGIC;
	sb->s_op = &exfat_sops;
	sb->s_export_op = &exfat_export_ops;
    。。。 。。。
}

代码下载链接:https://download.csdn.net/download/hinewcc/89440534

说明:相关修改请看super.c文件!!!

修改后,重新编译kernel 运行 NFS 导出功能正常!

4、参考文档

Kernel File Handle 详解 - liuchao719 - 博客园 (cnblogs.com)

【linux3.10】【nfs】使文件系统可导出_linuxnfs导出文件-CSDN博客

jffs2文件系统不支持export的问题_sonfs-CSDN博客

  • 15
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值