1 / 101

第五章 Linux 文件系统

第五章 Linux 文件系统. 概述 ext2 和 ext3 文件系统 Linux 虚拟文件系统 superblock inode dentry file 与进程相关的 VFS 数据结构 files_struct fs_struct 与文件相关的常用 Shell 命令. 1. 概述 (1). 文件系统 (File System) 文件系统负责 组织和管理 系统中的 文件 ,为用户提供使用文件的 操作接口 file structure, manipulation, and protection

avent
Télécharger la présentation

第五章 Linux 文件系统

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 第五章 Linux文件系统 概述 ext2和ext3文件系统 Linux虚拟文件系统 superblock inode dentry file 与进程相关的VFS数据结构 files_struct fs_struct 与文件相关的常用Shell命令 1

  2. 概述 (1) • 文件系统(File System) • 文件系统负责组织和管理系统中的文件,为用户提供使用文件的操作接口 • file structure, manipulation, and protection • provides a user interface to data storage and transparently manipulates the physical data from the external drives • 归纳起来,文件系统的功能包括以下几项: • 管理外存的存储空间 • 实施存储空间分区、分配、回收与重组 • 实现文件的“按名存取” • 按名建立、读/写、检索、修改、删除文件等操作 • 实现对文件的共享、保密和保护措施 • 提供文件访问接口

  3. 概述 (2) • 文件(File) • 文件是一组相关信息的有序集合,一般存放在外部存储器中 • In Linux, a file is a linear stream of bytes. • 文件是可以长久保存的信息形式 • 文件的存储空间是具有长久记忆特性的外部存储器(如磁盘、磁带等) • 所有需要在系统关机后仍能保留的信息都应以文件的形式存在 • 文件名是文件的标识 • 常见文件名命名规则:File Name + Filename Extension • *.h, *.c • Linux中,文件名可以没有后缀,不能从文件名判断文件的类型 • 文件的属性(元数据):类型、属主、大小、创建时间 • 文件的内容:程序、数据、图片、视频等

  4. 概述 (3) • 目录(directory) • 文件系统采用目录来组织文件 • 目录好比一个文件夹,包含相关的文件。目录可包含子目录。 • 目录的信息是需要长久保存的,目录以文件的形式存在 • A directory is a file that maintains the hierarchical structure of the filesystem • cd (change directory) • ... / • tree 树形目录结构 • 路径名(pathname) • absolute pathname pwd (print name of working directory) • relative pathname • 区分: 目录 vs 路径分量 (目录项 dentry)

  5. 概述 (4) • 通过ls命令查看文件的常见属性 • 文件类型(File Type) • 普通文件(regular file) (-) • ASCII files / binary files / data • 目录(directory) • 连接(link) • 设备(device file) • block devices vs. character devices • socket, and pipe • forms of Interprocess Communication (IPC)

  6. 概述 (5) • 属主与权限 • User, group, other • read, write, and execute

  7. 对文件的操作 (File Operation) • 实质上通过对文件的数据结构的操作来实现 • 图形操作 • 命令 • mkdir (make directory) • rmdir (remove directory) • mv (move) • cat (concatenate and display files) • more • less • cp (copy) • touch • 系统调用函数 • chgrp • chmod • chown • wc (word count) • sort • file • umask • find • grep

  8. 概述 (7) • 文件描述符(File Descriptor, fd) • the system uses it to identify an open file • an int datatype • int open(const char *pathname, int flags); • int open(const char *pathname, int flags, mode_t mode); • int creat(const char *pathname, mode_t mode); • 进程 • file descriptor array

  9. 概述 (8) • 文件的物理结构与存储方式 • 文件的物理结构与存储介质的物理特性有关 • 磁带、磁盘、闪存、光盘 • 磁盘(hard disk)的物理构造 • Head (磁头) • Mechanical arm (机械臂) • Track(磁道) • Cylinder(柱面) • Sector(扇区) 512B • Sector是磁盘存储的最小物理单位

  10. 第五章 Linux文件系统 概述 ext2和ext3文件系统 Linux虚拟文件系统 superblock inode dentry file 与进程相关的VFS数据结构 files_struct fs_struct 与文件相关的常用Shell命令 10

  11. ext2和ext3文件系统 (1) • Ext2(Extended-2) 是专为Linux系统设计的一种文件系统 • 运行稳定,存取效率较高,可支持最大4TB磁盘分区 • 弱点: Ext2是一个非日志文件系统。 • 日志文件系统可以在系统发生断电或者其他系统故障时保证文件数据的完整性。 • 近年来,Ext2已逐渐被Ext3取代。 • Ext3是一个基于Ext2开发的日志(journaling)文件系统。具有健全的日志功能,可靠性很高。 • 在非正常关机后,文件系统可在数十秒钟内自行修复 • Ext3可以支持最大32TB的文件系统和最大2TB的文件。 • 目前Ext3已被许多Linux发行版作为默认安装的文件系统。 • Ext4正处于试用阶段。 • 改进了支持文件和文件系统的大小。 • Ext4文件系统的容量达到1024 PB(1P=1024T),而文件大小则可达到16TB。

  12. 分区与格式化 • 分区(Partitioning) • 记录每个分区的起始和终止柱面 • 分区表存放在MBR(master boot recorder)中,0磁道 • MBR仅最多能记录4个分区: • 主分区Primary vs. 扩展分区Extension (逻辑分区 logical) • 格式化(Formatting) • 将分区format为操作系统能够识别和支持的文件系统类型 • 传统上,每个分区只能对应一个文件系统。 • 随着新技术的使用,一个分区可格式化后支持多个文件系统;或者,多个分区支持一个文件系统。

  13. 块(block) • 块(block) • 块(block)由若干个相邻的扇区组成,它是对存储空间的逻辑划分 • 块是分区格式化时指定的“最小存储单位” • Sblock = 2k Ssector • 1024,2048, 4096 B • 文件系统在读/写磁盘时不是以扇区为单位,而是以块为单位的 • 在文件系统看来,磁盘的存储空间是由许多在逻辑上连续的块组成的 • 每个块都有编号:0到n编号

  14. 4 4 5 1 2 3 9 10 6 7 8 14 15 11 12 13 inode • inode • 记录文件的属性(大小、rwx、所有者等)以及文件放在哪个block上 • A file can refer to as regular file, directory, device, and pipe. • inode objects exist for the full lifetime of the file

  15. 创建文件(或目录)时分配inode和block • 创建目录 • 创建一个目录时,分配一个inode和至少一个block给该目录 • inode记录目录的属性,并指向block • block记录:与该目录相关的子目录或文件的inode信息 • 创建文件 • 创建一个文件时,分配一个inode和与文件大小对应的数个block • inode记录文件的属性,并指向存放文件内容的block • 多个block用来存放文件的内容

  16. 通过inode查找block,读取文件内容 • /etc/crontab 读取流程 目录的inode 文件的inode

  17. Block group1 Block group2 Block group3 Block group4 启动 扇区 文件 系统 描述 信息 superblock Block bitmap inode bitmap inode table Data Block Superblock • Superblock • 文件系统所在的分区的最前面有一个启动扇区(boot sector) • 这个扇区可以安装一个本地boot loader。(回忆:多重引导系统) • Superblock用来存放文件系统的相关信息

  18. 第五章 Linux文件系统 概述 ext2和ext3文件系统 Linux虚拟文件系统 superblock inode dentry file 与进程相关的VFS数据结构 files_struct fs_struct 与文件相关的常用Shell命令 18

  19. Linux支持的部分文件系统

  20. VFS: Virtual Filesystem • 想一想:为什么需要VFS?

  21. virtual filesystem (VFS)

  22. virtual filesystem (VFS) • VFS不直接操作文件,所有对文件的实际操作都要通过实际作用于某个介质的文件系统来完成 • 引入VFS的目的是为了屏蔽各种文件系统的差异 • VFS对实际文件系统进行抽像,采用统一的数据结构在内存中描述所有实际的文件系统,向用户提供一组标准的文件操作函数。 • VFS负责将标准文件操作映射到实际文件系统的操作。 • 正是这种抽像和映射,保证了Linux系统可以支持多种介质的不同文件系统,使所有文件系统都具有基本相同的外部表现

  23. virtual filesystem (VFS) • VFS通过一组统一的数据结构和操作函数接口实现抽象 • 数据结构的建立 • 结构体的数据由实际文件系统的相应的数据来填充 • 各个数据结构的对象存在于内存 • 操作函数 • 操作函数由实际的文件系统实现 • VFS依据这些对象提供的信息和操作函数来完成将用户的文件请求映射到实际文件系统的操作

  24. VFS的四个主要数据结构 • superblock • Stores information relating to a specific mounted file system • inode • Stores information relating to a specific file (metadata) • 目录也是一种文件 • dentry • Stores information related to a single component of a path • 目录项是路径的一个组成部分,不是代表一个目录 • file • Stores information related to files opened by a process • Note: • a directory is the same as a file, but dentry is not the same as a directory. • 文件的内容与inode有关,文件名与dentry有关 • 在路径中,包括普通文件在内,每一个部分都是目录项对象

  25. Superblock (1) • 每个文件系统都有自己的superblock结构体 • superblock结构体用来存储文件系统的相关信息 • 设备标识符、文件系统标志、数据块大小、文件系统的特殊信息、文件系统的使用信息、超级块操作集指针 • superblock又叫做file system superblock或者filesystem control block • superblock和文件系统一一对应,代表一个实际的文件系统 • superblock在文件系统挂装时建立,在文件系统卸载后撤销

  26. Superblock (2) • include/linux/fs.h L754

  27. Superblock (3) • struct list_head s_list; • Superblock结构体构成的双向循环链表 • unsigned char s_dirt; • 是否需要(superblock中字段的值)与(磁盘上superblock的属性)同步 • unsigned long long s_maxbytes; • Max file size allowed in the filesystem • struct super_operations*s_op; • a pointer of type super_operations struct • holds the table of superblock operations • struct dentry *s_root; • dentry struct holds the pathname of a file • s_root points to the mount directory name whose superblock belongs to • struct list_head s_files; • points to the first element of a list of file structs that are both in use and assigned to the superblock

  28. Superblock (4) • struct file_system_type*s_type; super_block与file_system_type之间的关系

  29. Superblock (5) • struct list_heads_dirty; • points to the first and last elements in the list of dirty inodes belonging to this filesystem. super_block与inode之间的关系

  30. Superblock (6) • VFS superblock在文件系统挂装时建立,在文件系统卸载后撤销 • 创建一个superblock (fs/super.c L50) • struct super_block *alloc_super(void) • 功能: • When mounted, a filesystem invokes this function, reads its superblock off of the disk, and fills in its superblock object.

  31. Block group1 Block group2 Block group3 Block group4 启动 扇区 文件 系统 描述 信息 superblock Block bitmap inode bitmap inode table Data Block Superblock (7) • 基于磁盘的文件系统的superblock存放在磁盘特定扇区 • 在系统运行过程中,由于文件操作会修改superblock的内容,造成与磁盘上的superblock内容不一致,VFS通过周期性地将所有发生改变的superblock写回磁盘来实现superblock的同步更新 • 非基于磁盘的文件系统的superblock(例sysfs)实时地构建并存在于内存中

  32. Superblock Operations (1) • superblock结构体中s_op指向一个super_operations结构体 (与具体文件系统有关一组函数指针) • include/linux/fs.h L970

  33. Superblock Operations (2) • super_operations结构体中的函数按操作对象主要分两类 • 操作文件系统superblock • sb -> s_op -> write_super(sb); • 操作文件系统中各文件对应的inode

  34. Superblock Operations (2) • struct inode *(*alloc_inode)(struct super_block *sb); • 在给定的superblock下创建并初始化一个inode对象. • 可以从slab分配器的inode专用cache中获取 • void (*destroy_inode)(struct inode *inode); • 释放指定文件的inode • 调用kmem_cache_free()还给slab分配器相应的cache • void (*read_inode) (struct inode *inode); • 根据inode->i_ino,从磁盘读取该inode,并填充内存中inode结构体 • int (*write_inode) (struct inode *, int wait); • 将inode信息写回磁盘. wait表示写操作是否需要同步 • void (*dirty_inode) (struct inode *inode); • inode被修改时调用该函数;将该inode加入到superblock的dirty inode list • void (*delete_inode) (struct inode *inode); • 从磁盘上删除指定的inode • generic_delete_inode()

  35. Superblock Operations (3) • void (*put_super) (struct super_block *sb); • 释放superblock (当卸载一个文件系统时) • void (*write_super) (struct super_block *sb); • 将指定的superblock写回到磁盘 • int (*sync_fs) (struct super_block *sb, int wait); • 使文件系统的元数据与磁盘上的同步 • void (*write_super_lockfs) (struct super_block *sb); • 先对文件系统上锁,用给定的superblock更新磁盘上superblock • void (*unlockfs) (struct super_block *sb); • 对文件系统解锁

  36. Superblock Operations (4) • int (*statfs) (struct super_block *sb, struct kstatfs *statfs); • 获取文件系统的统计信息。统计结果写入statfs指向的结构体中 • int (*remount_fs) (struct super_block *sb, int *flag, char *data); • 用更新的挂载选项重新挂载文件系统 • void (*umount_begin) (struct super_block *sb); • Called when a mount operation must be interrupted • void (*clear_inode) (struct inode *inode); • 释放指定的inode,并清除存放其文件内容的pages. • 说明: • 对某些文件系统来说,以上函数是可选的。不需要的函数指定设置为NULL。

  37. inode (1) • VFS inode结构体 • inode结构体包含了内核操作文件或者目录时所需要的信息 • 主要是:文件的属性信息以及文件放在哪个block上 • 文件可以是:普通文件、目录、设备、socket、pipe等 • inode objects exist for the full lifetime of the file • inode结构体代表文件系统中的一个文件 • 仅当文件被访问时,其对应的VFS inode结构体才在内存中创建 • inode信息的获取 • 对磁盘Unix文件系统来说,这些信息可以从磁盘inode直接读入 • 如果一个文件系统没有inode,那么,不管这些信息如何存放,VFS都将从中提取这些信息给VFS inode. • inode信息同步 • 与超级块相同,inode也存在同步更新的问题,所以,VFS也会周期性地将所有发生改变的inode写回磁盘

  38. inode (2) • include/linux/fs.h • L429

  39. inode (3) • struct hlist_node i_hash; • 指向hash表, 用于加速inode查找 • struct list_head i_list; • 指向inode链表 • inodes can find themselves in one of the three linked lists.

  40. inode (4) • struct list_head i_dentry; • points to a list of dentry structs that corresponds to the file. • The dentry struct contains the pathname pertaining to the file being represented by the inode. A file can have multiple dentry structs if it has multiple aliases. • unsigned long i_ino; • unique inode number is an automatically incremented value from a previously assigned inode ID. • When the superblock operation read_inode() is called, the inode indicated in this field is read from disk. • atomic_t i_count; //引用计数

  41. inode (5) • struct super_block *i_sb; • points to the superblock of the filesystem in which the file resides super_block与dirtyinode list 之间的关系

  42. s_dirty inode_unused inode_in_use inode (6) • unsigned long i_state;

  43. inode (6) • umode_t i_mode; //可读、可写、可执行 • uid_t i_uid; • gid_t i_gid; • loff_t i_size; //文件的大小,以字节为单位 • struct timespec i_atime; /* last access time */ • struct timespec i_mtime; /* last modify time */ • struct timespec i_ctime; /* create time */ • unsigned long i_blksize; //块大小 • unsigned long i_blocks; //该文件所占块数 • spinlock_t i_lock; • struct semaphore i_sem; • struct inode_operations *i_op; • struct file_operations *i_fop; • struct address_space *i_mapping;

  44. inode_operations (1) • inode_operations结构体包含了操作inode对象的方法 • include/linux/fs.h L936 • i -> i_op -> function(i); //调用方法

  45. inode_operations (2) • int create(struct inode *inode, struct dentry *dentry, int mode) • 系统调用create()和open()会调用该函数,创建一个inode • mode为初始访问权限 • struct dentry * lookup(struct inode *inode, struct dentry *dentry) • 从查找一个文件对应的inode • int mkdir(struct inode *inode, struct dentry *dentry, int mode) • int rmdir(struct inode *inode, struct dentry *dentry) • int mknod (struct inode *inode, struct dentry *dentry, int mode, dev_t rdev) • 为特殊设备文件(device、pipe、socket)创建inode • int rename(struct inode *old_i, struct dentry *old_dentry, struct inode *new_i, struct dentry *new_dentry)

  46. inode_operations (3) • void (*truncate) (struct inode *inode); • 修改文件的大小,设定在inode的i_size字段 • int (*permission) (struct inode *inode, int mask); • 检查文件的访问权限,若允许,返回0,否则,返回负值 • int (*setattr) (struct dentry *dentry, struct iattr *attr); • int (*getattr) (struct vfsmount *mnt, struct dentry *, struct kstat *); • int (*setxattr) (struct dentry *, const char *name, const void *value, 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 *); • 支持扩展属性是linux 2.6的一大特色。

  47. inode_operations (4) • int link(struct dentry *old_dentry, struct inode *inode, struct dentry *dentry) • 创建一个硬链接,为old_dentry创建一个新链接dentry • inode不变 • int unlink(struct inode *inode, struct dentry *dentry) • 取消链接 • int symlink(struct inode *inode, struct dentry *dentry, const char *symname) • 创建一个符号链接(symbolic link) • 实质是创建新的inode和dentry

  48. 补充:ln (1) • 文件名与dentry有关,文件内容与inode有关 • 考虑:有没有可能多个文件名对应于同一个inode呢? • hard link • 删除操作 • 如果将任何一个“文件名”删除,其inode和block都还存在,可以通过另一个“文件名”来读取到正确的文件数据 • 修改操作 • 不论使用哪个“文件名”编辑修改文件,最终结果都会写入相同的inode与block中,因此,都能进行完成数据的修改。 • Note: hard link不能对“目录”做链接

  49. 补充:ln (2) • Symbolic link • 创建独立的inode和block,block指向源文件的文件名 • 删除操作 • 若删除了链接文件,源文件仍然存在 • 但若删除了源文件,链接文件将无法访问源文件了 • 补充: • Symbolic link可以对目录做链接操作 • Symbolic link比较常用,其作用和Windows环境下的快捷方式相似

  50. dentry (1) • linux在定位一个文件时需要沿该文件的路径逐级访问路径中的目录 • 如果每次都要从磁盘读取目录文件的话,访问文件的效率就会很低。 • 为了方便查找操作,VFS引入了目录项(dentry)的概念。 • dentry代表一个路径分量。 • 路径由一系列的分量组成,每个分量都是一个目录或文件 • 例如,路径名/home/zhuge/memo中包含了“/”、“home”、“zhuge”和“memo”4个分量。 • 当VFS首次解析一个路径名时,它依次读取路径中的每个目录或文件,为每个分量建立一个dentry结构 • 每个dentry结构都与一个VFS inode相连 • VFS将这些已建立的dentry结构按目录关系链接在一起 • 在后续的文件查找操作中,VFS只需沿dentry的链接结构进行查找,可以很快地找到目标文件的dentry结构,然后得到它的VFS inode

More Related