f2fs学习笔记 - 4. f2fs文件系统组件说明
目錄
- 1. 前言
- 2. 文件系統(tǒng)組件說明
- 2.1 定義文件系統(tǒng)類型(fs/f2fs/super.c)
- 2.2 定義超級(jí)塊結(jié)構(gòu)與操作集(fs/f2fs/super.c)
- 2.3 定義inode結(jié)構(gòu)(include/linux/f2fs_fs.h)
- 2.4 定義inode操作函數(shù)集
- 2.5 定義inode文件操作函數(shù)集
- 2.6 定義inode address_space操作函數(shù)集
- 2.7 定義dentry操作函數(shù)集
1. 前言
本文主要是以f2fs文件系統(tǒng)為例來說明文件系統(tǒng)編程模式,要實(shí)現(xiàn)一個(gè)文件系統(tǒng),需要實(shí)現(xiàn)哪些組件,以及如何被使用的。
2. 文件系統(tǒng)組件說明
2.1 定義文件系統(tǒng)類型(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, };任何具體的文件系統(tǒng)都需要定義file_system_type 結(jié)構(gòu)體,它最主要的是指定了f2fs_mount的mount回調(diào),通過init_f2fs_fs初始化函數(shù)中調(diào)用register_filesystem來完成文件系統(tǒng)注冊(cè)
module_init(init_f2fs_fs)|--static int __init init_f2fs_fs(void)|--register_filesystem(&f2fs_fs_type);2.2 定義超級(jí)塊結(jié)構(gòu)與操作集(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;文件系統(tǒng)需要實(shí)現(xiàn)super_operations結(jié)構(gòu)體變量,它定義了操作inode相關(guān)的一系列回調(diào)。
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,.unfreeze_fs = f2fs_unfreeze,.statfs = f2fs_statfs,.remount_fs = f2fs_remount, };它主要會(huì)在文件系統(tǒng)類型file_system_type的mount回調(diào)中使用,用于初始化super_blcok的s_op
static struct dentry *f2fs_mount(struct file_system_type *fs_type, int flags,...,void *data)|--mount_bdev(fs_type, flags, dev_name, data, f2fs_fill_super);|--struct super_block *s| //~~~創(chuàng)建vfs超級(jí)塊 super_block|--s = sget(fs_type, test_bdev_super, set_bdev_super, flags | SB_NOSEC, bdev)|--f2fs_fill_super(s, data, flags & SB_SILENT ? 1 : 0)|--struct f2fs_sb_info *sbi|--struct f2fs_super_block *raw_super;| //~~~創(chuàng)建具體文件系統(tǒng)超級(jí)塊信息 f2fs_sb_info|--sbi = kzalloc(sizeof(struct f2fs_sb_info), GFP_KERNEL)|--read_raw_super_block(sbi, &raw_super, &valid_super_block,...)| |--struct f2fs_super_block *super| | //~~~創(chuàng)建具體文件系統(tǒng)超級(jí)塊 f2fs_super_block| |--super = kzalloc(sizeof(struct f2fs_super_block), GFP_KERNEL)| |--*raw_super = super| //~~~將具體文件系統(tǒng)超級(jí)塊信息初始化給VFS超級(jí)塊|--s->s_fs_info = sbi//~~~將具體文件系統(tǒng)超級(jí)塊初始化給具體文件系統(tǒng)超級(jí)塊信息sbi->raw_super = raw_super//~~~將創(chuàng)建的具體文件系統(tǒng)超級(jí)塊操作函數(shù)集 給super_block的s_ops->s_op = &f2fs_sops2.3 定義inode結(jié)構(gòu)(include/linux/f2fs_fs.h)
struct f2fs_inode {__le16 i_mode; /* file mode */__u8 i_advise; /* file hints */__u8 i_inline; /* file inline flags */__le32 i_uid; /* user ID */__le32 i_gid; /* group ID */__le32 i_links; /* links count */__le64 i_size; /* file size in bytes */__le64 i_blocks; /* file size in blocks */__le64 i_atime; /* access time */__le64 i_ctime; /* change time */__le64 i_mtime; /* modification time */__le32 i_atime_nsec; /* access time in nano scale */__le32 i_ctime_nsec; /* change time in nano scale */__le32 i_mtime_nsec; /* modification time in nano scale */__le32 i_generation; /* file version (for NFS) */union {__le32 i_current_depth; /* only for directory depth */__le16 i_gc_failures; /** # of gc failures on pinned file.* only for regular files.*/};__le32 i_xattr_nid; /* nid to save xattr */__le32 i_flags; /* file attributes */__le32 i_pino; /* parent inode number */__le32 i_namelen; /* file name length */__u8 i_name[F2FS_NAME_LEN]; /* file name for SPOR */__u8 i_dir_level; /* dentry_level for large dir */struct f2fs_extent i_ext; /* caching a largest extent */union {struct {__le16 i_extra_isize; /* extra inode attribute size */__le16 i_inline_xattr_size; /* inline xattr size, unit: 4 bytes */__le32 i_projid; /* project id */__le32 i_inode_checksum;/* inode meta checksum */__le64 i_crtime; /* creation time */__le32 i_crtime_nsec; /* creation time in nano scale */__le64 i_compr_blocks; /* # of compressed blocks */__u8 i_compress_algorithm; /* compress algorithm */__u8 i_log_cluster_size; /* log of cluster size */__le16 i_padding; /* padding */__le32 i_extra_end[0]; /* for attribute size calculation */} __packed;__le32 i_addr[DEF_ADDRS_PER_INODE]; /* Pointers to data blocks */};__le32 i_nid[DEF_NIDS_PER_INODE]; /* direct(2), indirect(2),double_indirect(1) node id */ } __packed;2.4 定義inode操作函數(shù)集
在f2fs_iget時(shí)會(huì)初始化inode操作函數(shù)集,而在通過inode號(hào)來獲取inode時(shí)均會(huì)調(diào)用到此函數(shù)
- 常規(guī)文件的inode操作函數(shù)集
- 目錄文件的inode操作函數(shù)集
- 加密鏈接的inode操作函數(shù)集
- 普通鏈接的inode操作函數(shù)集
- 特殊文件(字符設(shè)備/塊設(shè)備/管道文件)的inode操作函數(shù)集
2.5 定義inode文件操作函數(shù)集
在f2fs_iget時(shí)會(huì)初始化inode文件操作函數(shù)集,而在通過inode號(hào)來獲取inode時(shí)均會(huì)調(diào)用到此函數(shù)
- 常規(guī)文件的inode文件操作函數(shù)集
- 目錄文件的inode文件操作函數(shù)集
- 特殊文件的inode文件操作函數(shù)集
字符設(shè)備
塊設(shè)備
# fs/block_dev.c const struct file_operations def_blk_fops = { .open = blkdev_open,.release = blkdev_close, .llseek = block_llseek,.read_iter = blkdev_read_iter,.write_iter = blkdev_write_iter, .iopoll = blkdev_iopoll,.mmap = generic_file_mmap, .fsync = blkdev_fsync, .unlocked_ioctl = block_ioctl, #ifdef CONFIG_COMPAT.compat_ioctl = compat_blkdev_ioctl, #endif .splice_read = generic_file_splice_read, .splice_write = iter_file_splice_write, .fallocate = blkdev_fallocate, };管道設(shè)備
# fs/pipe.c const struct file_operations pipefifo_fops = { .open = fifo_open, .llseek = no_llseek, .read_iter = pipe_read, .write_iter = pipe_write, .poll = pipe_poll, .unlocked_ioctl = pipe_ioctl, .release = pipe_release, .fasync = pipe_fasync, }2.6 定義inode address_space操作函數(shù)集
在f2fs_iget時(shí)會(huì)初始化inode address_space操作函數(shù)集,而在通過inode號(hào)來獲取inode時(shí)均會(huì)調(diào)用到此函數(shù)
- node info的inode address_space操作函數(shù)集
- meta info的inode address_space操作函數(shù)集
- 常規(guī)文件的inode address_space操作函數(shù)集
- 目錄文件的inode address_space操作函數(shù)集
- 鏈接文件的inode address_space操作函數(shù)集
2.7 定義dentry操作函數(shù)集
# f2fs/dir.c #ifdef CONFIG_UNICODE const struct dentry_operations f2fs_dentry_ops = {.d_hash = generic_ci_d_hash,.d_compare = generic_ci_d_compare, }; #endif總結(jié)
以上是生活随笔為你收集整理的f2fs学习笔记 - 4. f2fs文件系统组件说明的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Ext4及F2FS文件系统 Debug
- 下一篇: java信息管理系统总结_java实现科