嵌入式 Linux根文件系統移植(一)——Linux文件系統簡介

Linux 操作系統 GNU cpp軟件架構獅 2019-05-01

本文對文件系統分析的代碼來源於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_SMP
int __percpu *mnt_writers;
#else
int 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_SECURITY
void *s_security;
#endif
const 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_inode和 write_inode讀寫inode,用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_QUOTA
ssize_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);
#endif
int (*bdev_try_to_free_page)(struct super_block*, struct page*, gfp_t);
};

2、inode結構

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_ORDERED
seqcount_ti_size_seqcount;
#endif
struct 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_QUOTA
struct dquot*i_dquot[MAXQUOTAS];
#endif
struct 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_INOTIFY
struct list_headinotify_watches; /* watches on this inode */
struct mutexinotify_mutex;/* protects the watches list */
#endif
unsigned longi_state;
unsigned longdirtied_when;/* jiffies of first dirtying */
unsigned inti_flags;
atomic_ti_writecount;
#ifdef CONFIG_SECURITY
void*i_security;
#endif
#ifdef CONFIG_FS_POSIX_ACL
struct posix_acl*i_acl;
struct posix_acl*i_default_acl;
#endif
void*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.mnt
const 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_SECURITY
void*f_security;
#endif
void*private_data;
#ifdef CONFIG_EPOLL
struct list_headf_ep_links;
#endif /* #ifdef CONFIG_EPOLL */
struct address_space*f_mapping;
#ifdef CONFIG_DEBUG_WRITECOUNT
unsigned long f_mnt_write_state;
#endif
};

四、緩衝區緩存

各個文件系統的實現在linux/fs中,文件系統層的底部是緩衝區緩存。緩衝區緩存跟蹤來自文件系統實現和物理設備(通過設備驅動程序)的讀寫請求。為了提高效率,Linux 對請求進行緩存,避免將所有請求發送到物理設備。緩存中緩存最近使用的緩衝區(頁面),可以快速提供給各個文件系統使用。

相關推薦

推薦中...