f2fs学习笔记 - 4. f2fs文件系统组件说明

本文以F2FS文件系统为例,详细介绍了文件系统编程模式及其实现所需的各个组件,包括文件系统类型定义、超级块结构与操作集、inode结构及其操作函数集等。

1. 前言

本文主要是以f2fs文件系统为例来说明文件系统编程模式,要实现一个文件系统,需要实现哪些组件,以及如何被使用的。

2. 文件系统组件说明

2.1 定义文件系统类型(fs/f2fs/super.c)

static struct file_system_type f2fs_fs_type = {
   
   
        .owner          = THIS_MODULE,
        .name           = "f2fs",
        .mount          = f2fs_mount,
        .kill_sb        = kill_block_super,
        .fs_flags       = FS_REQUIRES_DEV,
};

任何具体的文件系统都需要定义file_system_type 结构体,它最主要的是指定了f2fs_mount的mount回调,通过init_f2fs_fs初始化函数中调用register_filesystem来完成文件系统注册

module_init(init_f2fs_fs)
  |--static int __init init_f2fs_fs(void)
       |--register_filesystem(&f2fs_fs_type);

2.2 定义超级块结构与操作集(fs/f2fs/super.c)

struct f2fs_super_block {
   
       
        __le32 magic;                   /* Magic Number */   
        __le16 major_ver;               /* Major Version */  
        __le16 minor_ver;               /* Minor Version */  
        __le32 log_sectorsize;          /* log2 sector size in bytes */
        __le32 log_sectors_per_block;   /* log2 # of sectors per block */
        __le32 log_blocksize;           /* log2 block size in bytes */
        __le32 log_blocks_per_seg;      /* log2 # of blocks per segment */
        __le32 segs_per_sec;            /* # of segments per section */
        __le32 secs_per_zone;           /* # of sections per zone */
        __le32 checksum_offset;         /* checksum offset inside super block */
        __le64 block_count;             /* total # of user blocks */
        __le32 section_count;           /* total # of sections */
        __le32 segment_count;           /* total # of segments */
        __le32 segment_count_ckpt;      /* # of segments for checkpoint */
        __le32 segment_count_sit;       /* # of segments for SIT */
        __le32 segment_count_nat;       /* # of segments for NAT */
        __le32 segment_count_ssa;       /* # of segments for SSA */
        __le32 segment_count_main;      /* # of segments for main area */
        __le32 segment0_blkaddr;        /* start block address of segment 0 */
        __le32 cp_blkaddr;              /* start block address of checkpoint */
        __le32 sit_blkaddr;             /* start block address of SIT */
        __le32 nat_blkaddr;             /* start block address of NAT */
        __le32 ssa_blkaddr;             /* start block address of SSA */
        __le32 main_blkaddr;            /* start block address of main area */
        __le32 root_ino;                /* root inode number */
        __le32 node_ino;                /* node inode number */
        __le32 meta_ino;                /* meta inode number */
        __u8 uuid[16];                  /* 128-bit uuid for volume */
        __le16 volume_name[MAX_VOLUME_NAME];    /* volume name */
        __le32 extension_count;         /* # of extensions below */
        __u8 extension_list[F2FS_MAX_EXTENSION][F2FS_EXTENSION_LEN];/* extension array */
        __le32 cp_payload;   
        __u8 version[VERSION_LEN];      /* the kernel version */
        __u8 init_version[VERSION_LEN]; /* the initial kernel version */
        __le32 feature;                 /* defined features */
        __u8 encryption_level;          /* versioning level for encryption */
        __u8 encrypt_pw_salt[16];       /* Salt used for string2key algorithm */
        struct f2fs_device devs[MAX_DEVICES];   /* device list */
        __le32 qf_ino[F2FS_MAX_QUOTAS]; /* quota inode numbers */
        __u8 hot_ext_count;             /* # of hot file extension */
        __le16  s_encoding;             /* Filename charset encoding */
        __le16  s_encoding_flags;       /* Filename charset encoding flags */
        __u8 reserved[306];             /* valid reserved region */
        __le32 crc;                     /* checksum of superblock */
} __packed;

文件系统需要实现super_operations结构体变量,它定义了操作inode相关的一系列回调。

static const struct super_operations f2fs_sops = {
   
   
        .alloc_inode    = f2fs_alloc_inode,
        .free_inode     = f2fs_free_inode,
        .drop_inode     = f2fs_drop_inode,
        .write_inode    = f2fs_write_inode,
        .dirty_inode    = f2fs_dirty_inode,
        .show_options   = f2fs_show_options,
#ifdef CONFIG_QUOTA
        .quota_read     = f2fs_quota_read,
        .quota_write    = f2fs_quota_write,
        .get_dquots     = f2fs_get_dquots,
#endif
        .evict_inode    = f2fs_evict_inode,
        .put_super      = f2fs_put_super,
        .sync_fs        = f2fs_sync_fs,
        .freeze_fs      = f2fs_freeze,
        
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值