嵌入式 Linux根文件系统移植(一)――Linux文件系统简介

时间:2020-12-24 16:34:16

嵌入式 Linux根文件系统移植(一)――Linux文件系统简介

    本文对文件系统分析的代码来源于linux 2.6.35.7版本。

一、文件系统的体系结构

    文件系统是对存储设备上的数据和元数据进行组织的机制,便于用户和操作系统的交互。Linux支持多种文件系统,文件系统接口实现为分层的体系结构,将用户接口层、文件系统实现和操作存储设备的驱动程序分隔开。Linux文件系统的体系结构如下:

嵌入式 Linux根文件系统移植(一)――Linux文件系统简介

    用户空间包含一些应用程序(例如,文件系统的使用者)和 GNU C库(glibc),为文件系统调用(打开、读取、写和关闭)提供用户接口。系统调用接口的作用就像是交换器,将系统调用从用户空间发送到内核空间中的适当端点。

        VFS 是底层文件系统的主要接口,会导出一组接口,抽象到各个文件系统。有两个针对文件系统对象的缓存(inode 和 dentry),用于缓存最近使用过的文件系统对象。

    每个文件系统的实现(比如 ext2、yaffs2等等)导出一组通用接口,供VFS使用。缓冲区缓存会缓存文件系统和相关块设备之间的请求。例如,对底层设备驱动程序的读写请求会通过缓冲区缓存来传递允许在缓冲区缓存请求,减少访问物理设备的次数,加快访问速度。以最近使用(LRU)列表的形式管理缓冲区缓存但是可以使用sync命令将缓冲区缓存中的请求发送到存储媒体(迫使所有未写的数据发送到设备驱动程序,进而发送到存储设备)。

二、虚拟文件系统层

        VFS 作为文件系统接口的根层。VFS 记录当前支持的文件系统以及当前挂装的文件系统。VFS并不是一种实际的文件系统只存在于内存中,不存在于任何外存空间。VFS在系统启动时建立,在系统关闭时消亡。

    可以使用一组注册函数在Linux中动态地添加或删除文件系统。kernel保存当前支持的文件系统的列表,可以通过 /proc 文件系统在用户空间中查看这个列表。proc虚拟文件系统还显示当前与所支持文件系统相关联的设备。在Linux中添加新文件系统的方法是调用register_filesystem,函数的参数定义一个文件系统结构(file_system_type)的引用,文件系统结构定义了文件系统的名称、一组属性和两个超级块函数。register_filesystem函数也可以注销文件系统。

    在注册新的文件系统时,会把要注册的新文件系统及其相关信息添加到 file_systems链表中linux/include/linux/fs.h)。file_systems列表定义可以支持的文件系统。在命令行上输入cat /proc/filesystems,就可以查看当前linux系统支持的文件系统类型

int register_filesystem(struct file_system_type * fs)
{
int res = 0;
struct file_system_type ** p;
 
BUG_ON(strchr(fs->name, '.'));
if (fs->next)
return -EBUSY;
INIT_LIST_HEAD(&fs->fs_supers);
write_lock(&file_systems_lock);
p = find_filesystem(fs->name, strlen(fs->name));
if (*p)
res = -EBUSY;
else
*p = fs;
write_unlock(&file_systems_lock);
return res;
}

嵌入式 Linux根文件系统移植(一)――Linux文件系统简介

struct file_system_type {const char *name;int fs_flags;int (*get_sb) (struct file_system_type *, int,       const char *, void *, struct vfsmount *);void (*kill_sb) (struct super_block *);struct module *owner;struct file_system_type * next;struct list_head fs_supers;struct lock_class_key s_lock_key;struct lock_class_key s_umount_key;struct lock_class_key s_vfs_rename_key;struct lock_class_key i_lock_key;struct lock_class_key i_mutex_key;struct lock_class_key i_mutex_dir_key;struct lock_class_key i_alloc_sem_key;};

    VFS 中维护的另一个结构是挂载的文件系统,提供当前挂载的文件系统(见 linux/include/linux/mount.h),链接超级块结构。

嵌入式 Linux根文件系统移植(一)――Linux文件系统简介

struct vfsmount {struct list_head mnt_hash;struct vfsmount *mnt_parent;/* fs we are mounted on */struct dentry *mnt_mountpoint;/* dentry of mountpoint */struct dentry *mnt_root;/* root of the mounted tree */struct super_block *mnt_sb;/* pointer to superblock */struct list_head mnt_mounts;/* list of children, anchored here */struct list_head mnt_child;/* and going through their mnt_child */int mnt_flags;const char *mnt_devname;/* Name of device e.g. /dev/dsk/hda1 */struct list_head mnt_list;struct list_head mnt_expire;/* link in fs-specific expiry list */struct list_head mnt_share;/* circular list of shared mounts */struct list_head mnt_slave_list;/* list of slave mounts */struct list_head mnt_slave;/* slave list entry */struct vfsmount *mnt_master;/* slave is on master->mnt_slave_list */struct mnt_namespace *mnt_ns;/* containing namespace */int mnt_id;/* mount identifier */int mnt_group_id;/* peer group identifier */atomic_t mnt_count;int mnt_expiry_mark;/* true if marked for expiry */int mnt_pinned;int mnt_ghosts;#ifdef CONFIG_SMPint __percpu *mnt_writers;#elseint mnt_writers;#endif};

三、文件的结构

        VFS对Linux的每个文件系统的所有细节进行抽象,使得不同的文件系统在Linux核心以及系统中运行的其他进程看来,都是相同的,这种抽象的结构就是通用文件模型,由超级块(superblock)、inode、dentry 和文件组成。超级块在每个文件系统的根上,用于描述和维护文件系统的状态。文件系统中管理的每个文件(文件、目录、设备,linux中一切皆是文件)在 Linux 中表示为一个 inode。inode 包含管理文件系统中的文件所需的所有元数据(包括可以在文件上执行的操作)。dentry用来实现文件名称和inode之间的映射,有一个目录缓存用来保存最近使用的dentry。dentry还维护目录和文件之间的关系,从而支持文件在文件系统中移动。VFS文件表示一个打开的文件(保存打开的文件的状态,比如写偏移量等等)。

1、超级块

    超级块结构表示一个文件系统,包含管理文件系统所需的信息,包括文件系统名称(比如 ext2)、文件系统的大小和状态、块设备的引用和元数据信息(比如空闲列表等等)。超级块通常存储在存储媒体上,但是如果超级块不存在,也可以实时创建它。可以在 ./linux/include/linux/fs.h 中找到超级块结构。

嵌入式 Linux根文件系统移植(一)――Linux文件系统简介

struct super_block {struct list_heads_list;/* Keep this first */dev_ts_dev;/* search index; _not_ kdev_t */unsigned chars_dirt;unsigned chars_blocksize_bits;unsigned longs_blocksize;loff_ts_maxbytes;/* Max file size */struct file_system_type*s_type;const struct super_operations*s_op;const struct dquot_operations*dq_op;const struct quotactl_ops*s_qcop;const struct export_operations *s_export_op;unsigned longs_flags;unsigned longs_magic;struct dentry*s_root;struct rw_semaphores_umount;struct mutexs_lock;ints_count;atomic_ts_active;#ifdef CONFIG_SECURITYvoid                    *s_security;#endifconst struct xattr_handler **s_xattr;struct list_heads_inodes;/* all inodes */struct hlist_heads_anon;/* anonymous dentries for (nfs) exporting */struct list_heads_files;struct list_heads_dentry_lru;/* unused dentry lru */ints_nr_dentry_unused;/* # of dentry on lru */struct block_device*s_bdev;struct backing_dev_info *s_bdi;struct mtd_info*s_mtd;struct list_heads_instances;struct quota_infos_dquot;/* Diskquota specific options */ints_frozen;wait_queue_head_ts_wait_unfrozen;char s_id[32];/* Informational name */void *s_fs_info;/* Filesystem private info */fmode_ts_mode;u32   s_time_gran;struct mutex s_vfs_rename_mutex;/* Kludge */char *s_subtype;char *s_options;};

    超级块中的一个重要元素是超级块操作的定义super_operations,super_operations结构定义一组用来管理文件系统中的 inode 的函数。例如,可以用alloc_inode分配 inode,用destroy_inode删除inode。可以用read_inodewrite_inodeinode,用sync_fs执行文件系统同步。可以在 /linux/include/linux/fs.h 中找到 super_operations 结构。每个文件系统提供自己的inode方法,这些方法实现操作并向 VFS 层提供通用的抽象。

struct super_operations {   struct inode *(*alloc_inode)(struct super_block *sb);void (*destroy_inode)(struct inode *);   void (*dirty_inode) (struct inode *);int (*write_inode) (struct inode *, struct writeback_control *wbc);void (*drop_inode) (struct inode *);void (*delete_inode) (struct inode *);void (*put_super) (struct super_block *);void (*write_super) (struct super_block *);int (*sync_fs)(struct super_block *sb, int wait);int (*freeze_fs) (struct super_block *);int (*unfreeze_fs) (struct super_block *);int (*statfs) (struct dentry *, struct kstatfs *);int (*remount_fs) (struct super_block *, int *, char *);void (*clear_inode) (struct inode *);void (*umount_begin) (struct super_block *);int (*show_options)(struct seq_file *, struct vfsmount *);int (*show_stats)(struct seq_file *, struct vfsmount *);#ifdef CONFIG_QUOTAssize_t (*quota_read)(struct super_block *, int, char *, size_t, loff_t);ssize_t (*quota_write)(struct super_block *, int, const char *, size_t, loff_t);#endifint (*bdev_try_to_free_page)(struct super_block*, struct page*, gfp_t);};

2inode结构

    inode 表示文件系统中的一个对象,具有惟一标识符。各个文件系统提供将文件名映射为惟一inode标识符和inode引用的方法。inode结构中的inode_operations file_operations是重要的操作方法成员inode_operations 定义直接在inode上执行的操作,而file_operations定义与文件和目录相关的方法(标准系统调用)。

嵌入式 Linux根文件系统移植(一)――Linux文件系统简介

struct inode {struct hlist_nodei_hash;struct list_headi_list;/* backing dev IO list */struct list_headi_sb_list;struct list_headi_dentry;unsigned longi_ino;atomic_ti_count;unsigned inti_nlink;uid_ti_uid;gid_ti_gid;dev_ti_rdev;unsigned inti_blkbits;u64i_version;loff_ti_size;#ifdef __NEED_I_SIZE_ORDEREDseqcount_ti_size_seqcount;#endifstruct timespeci_atime;struct timespeci_mtime;struct timespeci_ctime;blkcnt_ti_blocks;unsigned short          i_bytes;umode_ti_mode;spinlock_ti_lock;/* i_blocks, i_bytes, maybe i_size */struct mutexi_mutex;struct rw_semaphorei_alloc_sem;const struct inode_operations*i_op;const struct file_operations*i_fop;/* former ->i_op->default_file_ops */struct super_block*i_sb;struct file_lock*i_flock;struct address_space*i_mapping;struct address_spacei_data;#ifdef CONFIG_QUOTAstruct dquot*i_dquot[MAXQUOTAS];#endifstruct list_headi_devices;union {struct pipe_inode_info*i_pipe;struct block_device*i_bdev;struct cdev*i_cdev;};__u32i_generation;#ifdef CONFIG_FSNOTIFY__u32i_fsnotify_mask; /* all events this inode cares about */struct hlist_headi_fsnotify_mark_entries; /* fsnotify mark entries */#endif#ifdef CONFIG_INOTIFYstruct list_headinotify_watches; /* watches on this inode */struct mutexinotify_mutex;/* protects the watches list */#endifunsigned longi_state;unsigned longdirtied_when;/* jiffies of first dirtying */unsigned inti_flags;atomic_ti_writecount;#ifdef CONFIG_SECURITYvoid*i_security;#endif#ifdef CONFIG_FS_POSIX_ACLstruct posix_acl*i_acl;struct posix_acl*i_default_acl;#endifvoid*i_private; /* fs or device private pointer */}; struct inode_operations {int (*create) (struct inode *,struct dentry *,int, struct nameidata *);struct dentry * (*lookup) (struct inode *,struct dentry *, struct nameidata *);int (*link) (struct dentry *,struct inode *,struct dentry *);int (*unlink) (struct inode *,struct dentry *);int (*symlink) (struct inode *,struct dentry *,const char *);int (*mkdir) (struct inode *,struct dentry *,int);int (*rmdir) (struct inode *,struct dentry *);int (*mknod) (struct inode *,struct dentry *,int,dev_t);int (*rename) (struct inode *, struct dentry *,struct inode *, struct dentry *);int (*readlink) (struct dentry *, char __user *,int);void * (*follow_link) (struct dentry *, struct nameidata *);void (*put_link) (struct dentry *, struct nameidata *, void *);void (*truncate) (struct inode *);int (*permission) (struct inode *, int);int (*check_acl)(struct inode *, int);int (*setattr) (struct dentry *, struct iattr *);int (*getattr) (struct vfsmount *mnt, struct dentry *, struct kstat *);int (*setxattr) (struct dentry *, const char *,const void *,size_t,int);ssize_t (*getxattr) (struct dentry *, const char *, void *, size_t);ssize_t (*listxattr) (struct dentry *, char *, size_t);int (*removexattr) (struct dentry *, const char *);void (*truncate_range)(struct inode *, loff_t, loff_t);long (*fallocate)(struct inode *inode, int mode, loff_t offset,  loff_t len);int (*fiemap)(struct inode *, struct fiemap_extent_info *, u64 start,      u64 len);};

3、目录项dentry

    目录项是描述文件的逻辑属性,只存在于内存中,并没有实际对应的磁盘上的描述,更确切的说是存在于内存的目录项缓存,为了提高查找性能而设计。所有的文件,都是属于目录项,所有的目录项在一起构成一颗庞大的目录树。

        inode 和目录缓存分别保存最近使用的 inode dentry。注意,对于 inode 缓存中的每个 inode,在目录缓存中都有一个对应的 dentry

struct dentry {atomic_t d_count;unsigned int d_flags;/* protected by d_lock */spinlock_t d_lock;/* per dentry lock */int d_mounted;struct inode *d_inode;/* Where the name belongs to - NULL is negative */struct hlist_node d_hash;/* lookup hash list */struct dentry *d_parent;/* parent directory */struct qstr d_name;struct list_head d_lru;/* LRU list */union {struct list_head d_child;/* child of parent list */ struct rcu_head d_rcu;} d_u;struct list_head d_subdirs;/* our children */struct list_head d_alias;/* inode alias list */unsigned long d_time;/* used by d_revalidate */const struct dentry_operations *d_op;struct super_block *d_sb;/* The root of the dentry tree */void *d_fsdata;/* fs-specific data */unsigned char d_iname[DNAME_INLINE_LEN_MIN];/* small names */};

4、file文件对象

    文件对象是已打开的文件在内存中的表示,主要用于建立进程和磁盘上的文件的对应关系,由sys_open() 现场创建,由sys_close()销毁。文件对象和物理文件的关系有点像进程和程序的关系一样。

struct file {union {struct list_headfu_list;struct rcu_head fu_rcuhead;} f_u;struct pathf_path;#define f_dentryf_path.dentry#define f_vfsmntf_path.mntconst struct file_operations*f_op;spinlock_tf_lock;  /* f_ep_links, f_flags, no IRQ */atomic_long_tf_count;unsigned int f_flags;fmode_tf_mode;loff_tf_pos;struct fown_structf_owner;const struct cred*f_cred;struct file_ra_statef_ra;u64f_version;#ifdef CONFIG_SECURITYvoid*f_security;#endifvoid*private_data;#ifdef CONFIG_EPOLLstruct list_headf_ep_links;#endif /* #ifdef CONFIG_EPOLL */struct address_space*f_mapping;#ifdef CONFIG_DEBUG_WRITECOUNTunsigned long f_mnt_write_state;#endif};

四、缓冲区缓存

    各个文件系统实现在linux/fs,文件系统层的底部是缓冲区缓存。缓冲区缓存跟踪来自文件系统实现和物理设备(通过设备驱动程序)的读写请求。为了提高效率,Linux 对请求进行缓存,避免将所有请求发送到物理设备。缓存中缓存最近使用的缓冲区(页面),可以快速提供给各个文件系统使用

 

 

参考博文:

Linux 文件系统剖析(IBM developerworkks M.Tim Jones)


本文出自 “生命不息,奋斗不止” 博客,转载请与作者联系!