嵌入式linux根文件系统移植——linux文件系统简介

author author     2022-07-31     255

关键词:

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

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

一、文件系统的体系结构

    文件系统是对存储设备上的数据和元数据进行组织的机制,便于用户和操作系统的交互。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;
}

技术分享

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),链接超级块结构。

技术分享

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 中找到超级块结构。

技术分享

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_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_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);
};

2inode结构

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

技术分享

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 对请求进行缓存,避免将所有请求发送到物理设备。缓存中缓存最近使用的缓冲区(页面),可以快速提供给各个文件系统使用

 

 

参考博文:

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


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

linux基于busybox移植rootfs根文件系统(代码片段)

文章目录1.前言2.下载Busybox3.编译Busybox4.向rootfs根文件系统添加lib库5.根文件系统初步测试6.完善rootfs根文件系统7.根文件系统最终测试8.移植过程错误汇总1.前言我们所熟悉的Linux主要由三部分组成:uboot、LinuxKernel、rootfs(根文... 查看详情

linux系统移植:根文件系统构建(代码片段)

文章目录Linux系统移植:根文件系统构建一、什么是根文件系统二、根文件系统目录2.1子目录bin2.2子目录dev2.3子目录etc2.4子目录lib2.5子目录mnt2.6子目录proc2.7子目录usr2.8子目录var2.9子目录sbin2.10子目录sys2.11子目录opt三、BusyBox创... 查看详情

linux系统移植:根文件系统构建(代码片段)

文章目录Linux系统移植:根文件系统构建一、什么是根文件系统二、根文件系统目录2.1子目录bin2.2子目录dev2.3子目录etc2.4子目录lib2.5子目录mnt2.6子目录proc2.7子目录usr2.8子目录var2.9子目录sbin2.10子目录sys2.11子目录opt三、BusyBox创... 查看详情

根文件系统制作

...文件、目录全部都制作好了,接下来讨论如何把它移植到嵌入式系统开发板上。要把根文件系统的所有目录和文件移植到arm开发板上,需要一个名叫mtd.utils的工具软件,它可以直接到官网(ftp://ftp.infradead.org/pub/mtd-utils/)上去下... 查看详情

嵌入式linux与物联网进阶之路三:根文件系统制作

承接前篇,我们的linux内核终于制作好了,也顺利的加载起来了,但是由于没有根文件系统,所以说加载到最后,是无法进入系统的。而本节内容则是讲解如何来制作根文件系统的。BuildRoot创建根文件系统由于BuildRoot工具可以构... 查看详情

嵌入式linux系统中根文件系统构建方式(代码片段)

...码2.2.配置BusyBox3.构建根文件系统4.制作根文件系统镜像 嵌入式Linux根文件系统布局,建议还是按照FHS标准来安排,事实上大多数嵌入式Linux都是这样做的。但是,嵌入式系统可能并不需要桌面/服务器那样庞大系统的... 查看详情

嵌入式linux内核以及根文件系统制作(代码片段)

...用nandflsh中bootloader启动,sd卡bootloader启动有问题制作嵌入式平台使用的Linux内核,方法和制作PC平台的Linux内核基本一致。清除原有配置与中间文件x86:makedistcleanarm:makedistclean配置内核x86:makemenuconfigarm:makemenuconfigA 查看详情

嵌入式linux内核以及根文件系统制作(代码片段)

...用nandflsh中bootloader启动,sd卡bootloader启动有问题制作嵌入式平台使用的Linux内核,方法和制作PC平台的Linux内核基本一致。清除原有配置与中间文件x86:makedistcleanarm:makedistclean配置内核x86:makemenuconfigarm:makemenuconfigA 查看详情

什么时候要重新制作linux的根文件系统?谢谢

...首先要理解什么是根文件系统,如果你不是做应用开发如嵌入式等等话,就不需要修改。1.根文件系统  文件系统是包括在一个磁盘(包括光盘、软盘、闪盘及其它存储设备)或分区的目录结构;一个可应用的磁盘设备可以包... 查看详情

根文件系统构建

...ux“三巨头”已经完成了2个了,就剩最后一个rootfs(根文件系统)了,本章我们就来学习一下根文件系统的组成以及如何构建根文件系统。这是Linux移植的最后一步,根文件系统构建好以后就意味着我们已经拥有了一个... 查看详情

linux基于busybox移植rootfs根文件系统(代码片段)

文章目录1.前言2.下载Busybox3.编译Busybox4.向rootfs根文件系统添加lib库5.根文件系统初步测试6.完善rootfs根文件系统7.根文件系统最终测试8.移植过程错误汇总1.前言我们所熟悉的Linux主要由三部分组成:uboot、LinuxKernel、rootfs(根文... 查看详情

基于龙芯2k1000的嵌入式linux系统移植和驱动程序设计

2.1需求分析本课题以龙芯2K1000处理器为嵌入式系统的处理器,需要实现一个完成的嵌入式软件系统,系统能够正常启动并可以稳定运行嵌入式Linux。设计网络设备驱动,可以实现板卡与其他网络设备之间的网络连接和... 查看详情

基于itop-4412开发板(精英版)的linux4.14.2根文件系统移植(代码片段)

...件里,功能基本不变,而大小却小很多倍,在嵌入式linux应用中busybox有非常广的应用。另外,大多数linux发行版的安装程序中都有busybox的身影,安装linux的时候案ctrl+alt+F 查看详情

嵌入式基础(代码片段)

文章目录操作网络一些命令入门LinuxGCCMakefile函数通用Makefile使用文件IO系统调用函数怎么进入内核?Linux软件架构Linux启动过程如何理解Bootloader与Kernel文件系统概念虚拟文件系统、根文件系统和文件系统VFS:根文件系统其他文... 查看详情

嵌入式基础(代码片段)

文章目录操作网络一些命令入门LinuxGCCMakefile函数通用Makefile使用文件IO系统调用函数怎么进入内核?Linux软件架构Linux启动过程如何理解Bootloader与Kernel文件系统概念虚拟文件系统、根文件系统和文件系统VFS:根文件系统其他文... 查看详情

嵌入式linux第二部分-裸机开发/系统移植/驱动开发/内核开发

本部分主要专注构建从0到1的嵌入式Linux学习知识体系。主要涉及Linux环境配置,嵌入式Linux裸机开发,Linux文件系统及系统移植,驱动开发等部分。目前持续更新中,更新时间:2022年11月27日【嵌入式Linux】裸机开发篇LinuxC语言及M... 查看详情

基于itop-4412开发板(精英版)的linux4.14.2根文件系统移植(代码片段)

准备工作虚拟机:VM-Ware15.0pro操作系统:UbuntuLinux16.04交叉编译器:arm-linux-gcc4.6.2gBusyBox:busybox-1.26.2.tar工作文件夹:/home/用户名/work一、编译Linux命令集1、BusyBox源码获取Busybox官方下载地址:https 查看详情

《基于armcortex-a9的嵌入式linux内核移植研究与实现》

[1]罗名驹.基于ARMCortex-A9的嵌入式Linux内核移植研究与实现[D].广东工业大学,2017.文章目录​​移植环境搭建​​​​设备树​​​​uboot​​​​内核移植​​​​根文件系统构建​​移植环境搭建1.ubuntu安装嵌入式开发通常是在... 查看详情