Presentation is loading. Please wait.

Presentation is loading. Please wait.

Linux File System 文件系统 VFS VFS的作用 基于VFS的文件访问 VFS重要数据结构 文件系统的注册与安装

Similar presentations


Presentation on theme: "Linux File System 文件系统 VFS VFS的作用 基于VFS的文件访问 VFS重要数据结构 文件系统的注册与安装"— Presentation transcript:

1 Linux File System 文件系统 VFS VFS的作用 基于VFS的文件访问 VFS重要数据结构 文件系统的注册与安装
ext2、ext3文件系统 文件操作 proc文件系统 CRAMFS文件系统

2 各种各样的文件系统 Windows FAT16,FAT32, NTFS BSD文件系统FFS(Fast File System)
传统UNIX: UFS (Unix File System) BSD文件系统FFS(Fast File System) Proc File System:只存在于内存中 Linux File System ext2 ( is first introduced in kernel 2.0.x ) reiserfs ( is first introduced in kernel 2.2.x ) ext3 ( is first introduced in kernel 2.4.x ,default in RedHat now) xfs (from SGI ) Jfs (from IBM ) 嵌入式小型文件系统 CRAMFS JFFS2

3 linux文件系统目录布局 To comply with FSSTND(File System STaNDard):
/ first of mount point in linux /etc keep linux default configuration /boot - keep important linux booting files(can be a separate file system) /bin Essential command binaries for both root and ord. users /sbin - Essential system binaries for administrator /dev keep all device files /usr keep all user binary and X library /home - keep user home directory /proc - is pseudo file system for tracking running process and state of linux system /var keeping mail, log file and printer spooling /lib contain shared library that is required by system program /tmp - contain system temporary file /opt Add-on application software packages

4 UNIX文件系统文件类型 Directory --> catalogue of file name
Normal file --> format of data source file text file Symbolic link --> a pointer to another file Special file --> use for device controller in kernel Named pipe --> communication channel which can be used by serveral processes(may be irrelevant) in order to exchange data

5 硬链接(Hard Link) inode /root/link a b [root@localhost link]# ls -l
total 1 -rw-r--r root root Oct 15 13:39 a link]# ln a b total 2 -rw-r--r root root Oct 15 13:39 a -rw-r--r root root Oct 15 13:39 b link]# rm a rm: remove `a'? y -rw-r--r root root Oct 15 13:39 b inode /root/link a b

6 符号链接(Symbolic link) inode /root/link a b
symlink]# ls -l total 1 -rw-r--r root root Oct 15 13:39 a symlink]# ln -s a b lrwxrwxrwx 1 root root Oct 15 14:20 b -> a yy]# rm a rm: remove `a'? y total 0 symlink]# cat b cat: b: No such file or directory inode /root/link a b

7 VFS(Virtual FileSystem)的作用
User Programs System Call Interface Virtual File System Inter-process communication Process Control Subsystem Ext2 Ext3 ... Scheduler Memory management Buffer Cache Device Driver Hardware

8 基于VFS的文件访问

9 VFS的目录项(dentry) VFS的dentry定义在include/linux/dcache.h中
为了加快文件的查找,每一个曾被读取的目录或文件都可能在目录高速缓存(directory cache)中有一个dentry项;dentry描述了目录与文件的关系树。

10 VFS的目录项(dentry) struct dentry { /*include/linux/dcache.h*/
atomic_t d_count; unsigned int d_flags; struct inode * d_inode; /* Where the name belongs to - NULL is negative */ struct dentry * d_parent; /* parent directory */ struct list_head d_hash; /* lookup hash list */ struct list_head d_lru; /* d_count = 0 LRU list */ struct list_head d_child; /* child of parent list */ struct list_head d_subdirs; /* our children */ struct list_head d_alias; /* inode alias list */ int d_mounted; struct qstr d_name; unsigned long d_time; /* used by d_revalidate */ struct dentry_operations *d_op; struct super_block * d_sb; /* The root of the dentry tree */ unsigned long d_vfs_flags; void * d_fsdata; /* fs-specific data */ unsigned char d_iname[DNAME_INLINE_LEN]; /* small names */ };

11 打开文件表 linux系统运行期间维护一张以struct file (在include/linux/fs.h 中)作为节点的双向链表(系统打开文件表)。表头由first_file给出。 struct file *first_file = NULL; /* fs/file_table.c */ 对于每个进程,struct task_struct中的files指向的files_struct结构中有一个fd指针数组,即维护一张进程打开文件表。数组元素即是指向系统打开文件表中某一节点的指针。

12 VFS重要数据结构 files_struct (在sched.h); file (在fs.h); dentry (在 dcache.h);
superblock(在 fs.h); inode (在 fs.h)

13 文件系统类型 static struct file_system_type *file_systems =
(struct file_system_type *) NULL; struct file_system_type { struct super_block *(*read_super)(); /* 读出该文件系统在外存的super_block */ const char *name; /* 文件系统的类型名 */ int requires_dev; /* 支持文件系统的设备 */ struct file_system_type * next; /* 文件系统类型链表的后续指针 */ };

14 文件系统注册与注销 文件系统类型的注册和注销函数
int register_filesystem(struct file_system_type * fs) int unregister_filesystem(struct file_system_type * fs) file_systems file_system_type file_system_type file_system_type

15 文件系统的安装(mount) bin bin bin bin / / dev etc usr lib man Root filesystem
/usr filesystem / bin dev etc usr usr bin lib man Complete hierarchy after mounting /usr

16 文件系统的安装(mount) root i_sb mnt_mountpoint mnt_root d_mounted!=0 下挂文件系统
安装点vfsmount 安装点dentry

17 已安装文件系统的描述 static LIST_HEAD(vfsmntlist); 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 */ atomic_t mnt_count; int mnt_flags; char *mnt_devname; /* Name of device e.g. /dev/dsk/hda1 */ struct list_head mnt_list; };

18 已安装文件系统的描述 s_type file_system_type vfsmount super_block vfsmntlist
mnt_sb s_type file_systems mnt_sb

19 路径查找 系统调用open、mkdir、rename、stat等要查找路径
open_namei() path_init() path_walk() link_path_walk(); 返回时,struct nameidata中的dentry和mnt标识找到的文件或目录 struct nameidata { /*include/linux/fs.h*/ struct dentry *dentry; /*找到的dentry指针*/ struct vfsmount *mnt; /*找到的文件所在文件系统*/ struct qstr last; unsigned int flags; int last_type;};

20 ext2文件系统 支持UNIX所有标准的文件系统特征,包括正文、目录、设备文件和连接文件等,这使得它很容易被UNIX程序员接受。事实上,ext2的绝大多数的数据结构和系统调用与经典的UNIX一致 能够管理海量存储介质。支持多达4TB的数据,即一个分区的容量最大可达4TB 支持长文件名,最多可达255个字符,并且可扩展到1012个字符 允许通过文件属性改变内核的行为;目录下的文件继承目录的属性 支持文件系统数据“即时同步”特性,即内存中的数据一旦改变,立即更新硬盘上的数据使之一致 实现了“快速连接”(fast symbolic links)的方式,使得连接文件只需要存放inode的空间 允许用户定制文件系统的数据单元(block)的大小,可以是 1024、2048 或 4096 个字节,使之适应不同环境的要求 使用专用文件记录文件系统的状态和错误信息,供下一次系统启动时决定是否需要检查文件系统

21 ext2体系结构

22 内存中的ext2 inode ext2_inode_info (在include/linux/ext2_fs_i.h)
struct ext2_inode_info { __u32 i_data[15]; __u32 i_flags; __u32 i_faddr; __u8 i_frag_no; __u8 i_frag_size; __u16 i_osync; __u32 i_file_acl; __u32 i_dir_acl; __u32 i_dtime; __u32 i_block_group; __u32 i_next_alloc_block; __u32 i_next_alloc_goal; __u32 i_prealloc_block; __u32 i_prealloc_count; __u32 i_dir_start_lookup; int i_new_inode:1; /* Is a freshly allocated inode */ };

23 外存中的ext2 inode 内、外存inode的读写: ext2_read_inode() ext2_update_inode()
struct ext2_inode(在include/linux/ext2_fs.h) 内、外存inode的读写: ext2_read_inode() ext2_update_inode()

24 Ext2_inode

25 文件读写 read()和write() int read(int fd, void *buf,size_t nbytes);
int write(int fd, void *buf,size_t nbytes); read()调用generic_file_read(),再调用do_generic_file_read()读入内核缓冲区,然后调用file_read_actor()将读入内容传入用户空间。最后调用update_atime()修改inode write()调用generic_file_write()写数据入缓冲区,如果是同步写(O_SYNC置位),则调用generic_osync_inode()将缓冲区中数据写入磁盘文件。 直接读写(read、write时将O_DIRECT置位) generic_file_read()先读page cache generic_file_write()先写入缓冲区 generic_file_direct_IO()直接读写(self-caching)

26 ext3文件系统 日志文件系统(journaling file system) 利用数据库的日志技术(log, checkpoint)
3种日志方式:journal, ordered, writeback 日志记录在/.journal中(隐藏的文件) Kjournald—5s Reiserfs

27 proc文件系统 /proc:一个虚拟文件系统,只存在于内存中,通过它可以查询、设置系统的运行情况及各种系统参数。
系统中的很多应用都依赖于proc文件系统,如命令lsmod等同于 cat /proc/modules。 文件的大小为0; 很多文件名体现了内核的相应参数,可以通过这个文件名修改参数值。如#echo 2048 > /proc/sys/shmmni,修改共享内存段的限制。 /proc下的“数字目录”指代了相应pid的进程,如目录“1”下的内容就是1#进程的各种信息。

28 CRAMFS 由Linus Torvalds 参与开发的小型只读压缩文件系统 Inode、文件名称和目录信息不压缩 单个文件最大为16MB
数据压缩存放 适合不需要写、且体积较大的文件系统,如/lib,/opt等 与JFFS2、Cloop相比,读取速度快 压缩率可以超过50% 读取文件时,每次读取4k内容,解压缩到cache中 Linux内核已提供了对cramfs的支持,只要编译时选中 创建文件系统(生成image文件) #mkcramfs /lib lib.cramfs #mkcramfs /usr usr.cramfs 挂载文件系统 #mount –t cramfs lib.cramfs /lib –o loop #mount –t cramfs usr.cramfs /usr –o loop

29 CRAMFS Super_block:76bytes, cramfs_inode:12bytes
Cramfs_inode中的offset的单位为4字节

30 CRAMFS 根目录的inode内容,根目录的内容在起始偏移19*4=76字节处 根目录下的子目录与文件
11* *4 = 188


Download ppt "Linux File System 文件系统 VFS VFS的作用 基于VFS的文件访问 VFS重要数据结构 文件系统的注册与安装"

Similar presentations


Ads by Google