嵌入式 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;
}

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_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定义与文件和目录相关的方法(标准系统调用)。

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应用中,主要的存储设备为 RAM(DRAM, SDRAM)和ROM(常采用FLASH存储器),常用的基于存储设备的文件系统类型包括:jffs2, yaffs, cramfs, romfs, ramdisk, ramfs/tmpfs等。Linux的文件系统结构如下:

1、基于FLASH的文件系统

Flash(闪存)作为嵌入式系统的主要存储媒介,有其自身的特性。Flash的写入操作只能把对应位置的1修改为0,而不能把0修改为1(擦除Flash就是把对应存储块的内容恢复为1)。一般情况下,向Flash写入内容时,需要先擦除对应的存储区间,擦除是以块(block)为单位进行的。

闪存主要有NOR和NAND两种技术。Flash存储器的擦写次数是有限的,NAND闪存还有特殊的硬件接口和读写时序。传统的文件系统如ext2、ext3等用作Flash的文件系统会有诸多弊端。因此,必须针对Flash的硬件特性设计符合应用要求的文件系统。

一块Flash芯片可以被划分为多个分区,各分区可以采用不同的文件系统;两块Flash芯片也可以合并为一个分区使用,采用一个文件系统。即文件系统是针对于存储器分区而言的,而非存储芯片。

在嵌入式Linux下,MTD(Memory Technology Device,存储技术设备)为底层硬件(闪存)和上层(文件系统)之间提供一个统一的抽象接口,即Flash的文件系统都是基于MTD驱动层的。MTD驱动程序的是专门针对各种非易失性存储器(以闪存为主)而设计的,因而它对Flash有更好的支持、管理和基于扇区的擦除、读/写操作接口。常见闪存设备的文件系统如下:

A、jffs2

JFFS文件系统最早是由瑞典Axis Communications公司基于Linux2.0的内核为嵌入式系统开发的文件系统。JFFS2是RedHat公司基于JFFS开发的闪存文件系 统,最初是针对RedHat公司的嵌入式产品eCos开发的嵌入式文件系统,所以JFFS2也可以用在Linux, uCLinux中。

Jffs2: 日志闪存文件系统版本2 (Journalling Flash FileSystem v2)

主要用于NOR型闪存,基于MTD驱动层,特点是:可读写的、支持数据压缩的、基于哈希表的日志型文件系统,并提供了崩溃/掉电安全保护,提供“写平衡”支持等。缺点主要是当文件系统已满或接近满时,因为垃圾收集的关系而使jffs2的运行速度大大放慢。

jffs不适合用于NAND闪存主要是因为NAND闪存的容量一般较大,这样导致jffs为维护日志节点所占用的内存空间迅速增大,另外,jffs文件系统在挂载时需要扫描整个FLASH的内容,以找出所有的日志节点,建立文件结构,对于大容量的NAND闪存会耗费大量时间。

B、yaffs

yaffs是Yet Another Flash File System的缩写,yaffs/yaffs2是专为嵌入式系统使用NAND型闪存而设计的一种日志型文件系统。与jffs2相比,它减少了一些功能(例如不支持数 据压缩),所以速度更快,挂载时间很短,对内存的占用较小。另外,它还是跨平台的文件系统,除了Linux和eCos,还支持WinCE, pSOS和ThreadX等。

yaffs/yaffs2自带NAND芯片的驱动,并且为嵌入式系统提供了直接访问文件系统的API,用户可以不使用Linux中的MTD与VFS,直接对文件系统操作。当然,yaffs也可与MTD驱动程序配合使用。

yaffs与yaffs2的主要区别在于,前者仅支持小页(512 Bytes) NAND闪存,后者则可支持大页(2KB) NAND闪存。同时,yaffs2在内存空间占用、垃圾回收速度、读/写速度等方面均有大幅提升。

C、Cramfs

Cramfs是Compressed ROM File System的缩写,是由Linux的创始人 Linus Torvalds参与开发的一种只读的压缩文件系统,基于MTD驱动程序。

在cramfs文件系统中,每一页(4KB)被单独压缩,可以随机页访问,其压缩比高达2:1,为嵌入式系统节省大量的Flash存储空间,使系统可通过更低容量的FLASH存储相同的文件,从而降低系统成本。

Cramfs文件系统以压缩方式存储,在运行时解压缩,所以不支持应用程序以XIP方式运行,所有的应用程序要求被拷到RAM里去运行,但这并不代表比Ramfs需求的RAM空间要大一点,因为Cramfs是采用分页压缩的方式存放档案,在读取档案时,不会一下子就耗用过多的内存空间,只针对目前实际读 取的部分分配内存,尚没有读取的部分不分配内存空间,当我们读取的档案不在内存时,Cramfs文件系统自动计算压缩后的资料所存的位置,再即时解压缩到 RAM中。

另外,Cramfs的速度快,效率高,其只读的特点有利于保护文件系统免受破坏,提高了系统的可靠性,只读属性同时又是它的一大缺陷,使得用户无法对其内容对进扩充。

Cramfs映像通常是放在Flash中,也能放在别的文件系统里,使用loopback 设备可以把它安装别的文件系统里。

D、Romfs

传统型的Romfs文件系统是一种简单的、紧凑的、只读的文件系统,不支持动态擦写保存,按顺序存放数据,因而支持应用程序以 XIP(eXecute In Place,片内运行)方式运行,在系统运行时,节省RAM空间。uClinux系统通常采用Romfs文件系统。

其他文件系统:fat/fat32也可用于实际嵌入式系统的扩展存储器(例如PDA, Smartphone, 数码相机等的SD卡),这主要是为了更好的与最流行的Windows桌面操作系统相兼容。ext2也可以作为嵌入式Linux的文件系统,不过将它用于 FLASH闪存会有诸多弊端。

2、基于RAM的文件系统

A、Ramdisk

Ramdisk是将一部分固定大小的内存当作分区来使用。它并非一个实际的文件系统,而是一种将实际的文件系统装入内存的机制,并且可以作为根 文件系统。将一些经常被访问而又不会更改的文件(如只读的根文件系统)通过Ramdisk放在内存中,可以明显地提高系统的性能。

在Linux的启动阶段,initrd提供了一套机制,可以将内核映像和根文件系统一起载入内存。

B、ramfs/tmpfs

Ramfs是Linus Torvalds开发的一种基于内存的文件系统,工作于虚拟文件系统(VFS)层,不能格式化,可以创建多个,在创建时可以指定其最大能使用的内存大小。 (实际上,VFS本质上可看成一种内存文件系统,它统一了文件在内核中的表示方式,并对磁盘文件系统进行缓冲。)

Ramfs/tmpfs文件系统把所有的文件都放在RAM中,所以读/写操作发生在RAM中,可以用ramfs/tmpfs来存储一些临时性或经常要修改的数据,例如/tmp和/var目录,这样既避免了对Flash存储器的读写损耗,也提高了数据读写速度。

Ramfs/tmpfs相对于传统的Ramdisk的不同之处主要在于:不能格式化,文件系统大小可随所含文件内容大小变化。

Tmpfs的一个缺点是当系统重新引导时会丢失所有数据。

3、网络文件系统NFS (Network File System)

NFS是由Sun开发并发展起来的一项在不同机器、不同操作系统之间通过网络共享文件的技术。在嵌入式Linux系统的开发调试阶段,可以利用该技术在主机上建立基于NFS的根文件系统,挂载到嵌入式设备,可以很方便地修改根文件系统的内容。

嵌入式linux设备可以按照存储设备选择合适的根文件系统类型。

二、文件系统目录标准

Filesystem Hierarchy Standard(文件系统目录标准)是多数Linux版本采用的文件系统组织形式。FHS采用树形结构组织文件。FHS定义了系统中每个区域的用途、所需要的最小构成的文件和目录。

目录名

存放的内容

/bin

必备的用户命令,例如ls、cp等

/sbin

必备的系统管理员命令,例如ifconfig、reboot等

/dev

设备文件,例如mtdblock0、tty1等

/etc

系统配置文件,包括启动文件,例如inittab等

/lib

必要的链接库,例如C链接库、内核模块

/home

普通用户主目录

/root

root用户主目录

/usr/bin

非必备的用户程序,例如find、du等

/usr/sbin

非必备的管理员程序,例如chroot、inetd等

/usr/lib

库文件

/var

守护程序和工具程序所存放的可变,例如日志文件

/proc

用来提供内核与进程信息的虚拟文件系统,由内核自动生成目录下的内容

/sys

用来提供内核与设备信息的虚拟文件系统,由内核自动生成目录下的内容

/mnt

文件系统挂接点,用于临时安装文件系统

/tmp

临时性的文件,重启后将自动清除

三、根文件系统的挂载分析

Kernel启动过程中,start_kernel函数调用了vfs_caches_init函数,mnt_init函数中的init_rootfs()函数用于注册根文件系统,init_mount_tree()函数用于挂载根文件系统。

void __init vfs_caches_init(unsigned long mempages)
{
unsigned long reserve;
 
reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);
mempages -= reserve;
names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
dcache_init();
inode_init();
files_init(mempages);
mnt_init();
bdev_cache_init();
chrdev_init();
}
void __init mnt_init(void)
{
unsigned u;
int err;
init_rwsem(&namespace_sem);
mnt_cache = kmem_cache_create("mnt_cache", sizeof(struct vfsmount),
0, SLAB_HWCACHE_ALIGN | SLAB_PANIC, NULL);
mount_hashtable = (struct list_head *)__get_free_page(GFP_ATOMIC);
if (!mount_hashtable)
panic("Failed to allocate mount hash table\n");
printk("Mount-cache hash table entries: %lu\n", HASH_SIZE);
for (u = 0; u < HASH_SIZE; u++)
INIT_LIST_HEAD(&mount_hashtable[u]);
err = sysfs_init();
if (err)
printk(KERN_WARNING "%s: sysfs_init error: %d\n",
__func__, err);
fs_kobj = kobject_create_and_add("fs", NULL);
if (!fs_kobj)
printk(KERN_WARNING "%s: kobj create error\n", __func__);
init_rootfs();//根文件系统注册
init_mount_tree();//根文件系统挂载
}
static void __init init_mount_tree(void)
{
struct vfsmount *mnt;
struct mnt_namespace *ns;
struct path root;
mnt = do_kern_mount("rootfs", 0, "rootfs", NULL);
if (IS_ERR(mnt))
panic("Can't create rootfs");
ns = create_mnt_ns(mnt);
if (IS_ERR(ns))
panic("Can't allocate initial namespace");
init_task.nsproxy->mnt_ns = ns;
get_mnt_ns(ns);
root.mnt = ns->root;
root.dentry = ns->root->mnt_root;
set_fs_pwd(current->fs, &root);
set_fs_root(current->fs, &root);
}
struct vfsmount *
do_kern_mount(const char *fstype, int flags, const char *name, void *data)
{
struct file_system_type *type = get_fs_type(fstype);
struct vfsmount *mnt;
if (!type)
return ERR_PTR(-ENODEV);
mnt = vfs_kern_mount(type, flags, name, data);
if (!IS_ERR(mnt) && (type->fs_flags & FS_HAS_SUBTYPE) &&
 !mnt->mnt_sb->s_subtype)
mnt = fs_set_subtype(mnt, fstype);
put_filesystem(type);
return mnt;
}
 
struct vfsmount *
vfs_kern_mount(struct file_system_type *type, int flags, const char *name, void *data)
{
struct vfsmount *mnt;
char *secdata = NULL;
int error;
if (!type)
return ERR_PTR(-ENODEV);
error = -ENOMEM;
mnt = alloc_vfsmnt(name);
if (!mnt)
goto out;
if (flags & MS_KERNMOUNT)
mnt->mnt_flags = MNT_INTERNAL;
if (data && !(type->fs_flags & FS_BINARY_MOUNTDATA)) {
secdata = alloc_secdata();
if (!secdata)
goto out_mnt;
 
error = security_sb_copy_data(data, secdata);
if (error)
goto out_free_secdata;
}
error = type->get_sb(type, flags, name, data, mnt);
if (error < 0)
goto out_free_secdata;
BUG_ON(!mnt->mnt_sb);
WARN_ON(!mnt->mnt_sb->s_bdi);
mnt->mnt_sb->s_flags |= MS_BORN;
error = security_sb_kern_mount(mnt->mnt_sb, flags, secdata);
if (error)
goto out_sb;
WARN((mnt->mnt_sb->s_maxbytes < 0), "%s set sb->s_maxbytes to "
"negative value (%lld)\n", type->name, mnt->mnt_sb->s_maxbytes);
mnt->mnt_mountpoint = mnt->mnt_root;
mnt->mnt_parent = mnt;
up_write(&mnt->mnt_sb->s_umount);
free_secdata(secdata);
return mnt;
out_sb:
dput(mnt->mnt_root);
deactivate_locked_super(mnt->mnt_sb);
out_free_secdata:
free_secdata(secdata);
out_mnt:
free_vfsmnt(mnt);
out:
return ERR_PTR(error);
}

四、NFS方式挂载根文件系统

要使用NFS方式挂载根文件系统,则kernel必须支持NFS方式。编译配置kernel时需要make menuconfig配置部分如下:

1、配置网络部分

Networking support

Networking options

TCP/IP networking

IP: kernel level autoconfiguration

[*] IP: DHCP support

[*] IP: BOOTP support

2、NFS方式配置

File systems --->

Network File Systems --->

<*> NFS client support

[*] NFS client support for NFS version 3 [*] NFS client support for the NFSv3 ACL protocol extension

[*] NFS client support for NFS version 4 (EXPERIMENTAL)

[*] NFS client support for NFSv4.1 (DEVELOPER ONLY)

[*] Root file system on NFS

3、kernel启动参数的设置

在uboot中设置kernel的启动参数:

setenv bootargs root=/dev/nfs nfsroot=192.168.6.200:/home/nfs/rootfs ip=192.168.6.210:192.168.6.200:192.168.6.1:255.255.255.0::eth0:off init=/linuxrc console=ttySAC2,115200

举报
评论 0