101
1 第第第 Linux 第第第 概概 ext2 概 ext3 概概概 Linux 概概概概概 superblock inode dentry file 概概概概概概 VFS 概概概概 files_struct fs_struct 概概概概概概概概 Shell 概概

第五章 Linux 文件系统

  • Upload
    avent

  • View
    161

  • Download
    9

Embed Size (px)

DESCRIPTION

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

Citation preview

Page 1: 第五章   Linux 文件系统

1

第五章 Linux 文件系统概述ext2和 ext3 文件系统Linux 虚拟文件系统

superblock inode dentry file

与进程相关的 VFS 数据结构 files_struct fs_struct

与文件相关的常用 Shell 命令

Page 2: 第五章   Linux 文件系统

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

归纳起来,文件系统的功能包括以下几项: 管理外存的存储空间

• 实施存储空间分区、分配、回收与重组 实现文件的“按名存取”

• 按名建立、读 / 写、检索、修改、删除文件等操作 实现对文件的共享、保密和保护措施 提供文件访问接口

Page 3: 第五章   Linux 文件系统

3

概述 (2) 文件 (File)

文件是一组相关信息的有序集合,一般存放在外部存储器中• In Linux, a file is a linear stream of bytes.

文件是可以长久保存的信息形式• 文件的存储空间是具有长久记忆特性的外部存储器 ( 如磁盘、磁带等 )

• 所有需要在系统关机后仍能保留的信息都应以文件的形式存在 文件名是文件的标识

• 常见文件名命名规则: File Name + Filename Extension – *.h, *.c– Linux 中,文件名可以没有后缀,不能从文件名判断文件的类型

文件的属性 ( 元数据 ) :类型、属主、大小、创建时间 文件的内容:程序、数据、图片、视频等

Page 4: 第五章   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)

Page 5: 第五章   Linux 文件系统

概述 (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)

Page 6: 第五章   Linux 文件系统

6

概述 (5)属主与权限

User, group, other read, write, and execute

表 2-3 文件和访问权限及表示

访问权限 字符表示 含 义

读权限 r 可读取其内容

写权限 w 可修改其内容

执行权限 x 可执行其内容

无权限 - 不能做相应的操作

Page 7: 第五章   Linux 文件系统

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

Page 8: 第五章   Linux 文件系统

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

Page 9: 第五章   Linux 文件系统

9

概述 (8)文件的物理结构与存储方式

文件的物理结构与存储介质的物理特性有关

• 磁带、磁盘、闪存、光盘磁盘 (hard disk) 的物理构造

• Head ( 磁头 )

• Mechanical arm ( 机械臂 )

• Track( 磁道 )

• Cylinder( 柱面 )

• Sector( 扇区 ) 512B

• Sector 是磁盘存储的最小物理单位

旋转

移动

磁头

扇区

磁道

柱面

Page 10: 第五章   Linux 文件系统

10

第五章 Linux 文件系统概述ext2和 ext3 文件系统Linux 虚拟文件系统

superblock inode dentry file

与进程相关的 VFS 数据结构 files_struct fs_struct

与文件相关的常用 Shell 命令

Page 11: 第五章   Linux 文件系统

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 。

Page 12: 第五章   Linux 文件系统

12

分区与格式化分区 (Partitioning)

记录每个分区的起始和终止柱面 分区表存放在 MBR(master boot recorder) 中, 0 磁道 MBR仅最多能记录 4 个分区:

• 主分区 Primary vs. 扩展分区 Extension (逻辑分区 logical)

格式化 (Formatting) 将分区 format 为操作系统能够识别和支持的文件系统类型 传统上,每个分区只能对应一个文件系统。 随着新技术的使用,一个分区可格式化后支持多个文件系

统;或者,多个分区支持一个文件系统。

Page 13: 第五章   Linux 文件系统

块 (block)块 (block)

块 (block)由若干个相邻的扇区组成,它是对存储空间的逻辑划分

块是分区格式化时指定的“最小存储单位” • Sblock = 2k Ssector

• 1024, 2048, 4096 B

文件系统在读 / 写磁盘时不是以扇区为单位,而是以块为单位的

在文件系统看来,磁盘的存储空间是由许多在逻辑上连续的块组成的

每个块都有编号: 0到 n编号

Page 14: 第五章   Linux 文件系统

14

inodeinode

记录文件的属性 ( 大小、 rwx 、所有者等 ) 以及文件放在哪个 block 上

• A file can refer to as regular file, directory, device, and pipe.

inode objects exist for the full lifetime of the file

4

1 2 3 4 5

6 7 8 9 10

11 12 13 14 15

Page 15: 第五章   Linux 文件系统

创建文件 ( 或目录 ) 时分配 inode和block创建目录

创建一个目录时,分配一个 inode 和至少一个 block给该目录

• inode 记录目录的属性,并指向 block• block 记录:与该目录相关的子目录或文件的 inode 信息

创建文件 创建一个文件时,分配一个 inode 和与文件大小对应的数个 block

• inode 记录文件的属性,并指向存放文件内容的 block• 多个 block 用来存放文件的内容

Page 16: 第五章   Linux 文件系统

16

通过 inode 查找 block ,读取文件内容/etc/crontab 读取流程

目录的 inode

文件的 inode

Page 17: 第五章   Linux 文件系统

17

SuperblockSuperblock

文件系统所在的分区的最前面有一个启动扇区 (boot sector)

• 这个扇区可以安装一个本地 boot loader。 ( 回忆:多重引导系统 )

Superblock 用来存放文件系统的相关信息

Blockgroup1

Blockgroup2

Blockgroup3

Blockgroup4

superblockBlock bitmap

inode bitmap

inode table

Data Block

启动扇区

文件系统描述信息

Page 18: 第五章   Linux 文件系统

18

第五章 Linux 文件系统概述ext2和 ext3 文件系统Linux 虚拟文件系统

superblock inode dentry file

与进程相关的 VFS 数据结构 files_struct fs_struct

与文件相关的常用 Shell 命令

Page 19: 第五章   Linux 文件系统

19

Linux 支持的部分文件系统

Page 20: 第五章   Linux 文件系统

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

Page 21: 第五章   Linux 文件系统

21

virtual filesystem (VFS)

Page 22: 第五章   Linux 文件系统

22

virtual filesystem (VFS)VFS 不直接操作文件,所有对文件的实际操作都要通过

实际作用于某个介质的文件系统来完成引入 VFS 的目的是为了屏蔽各种文件系统的差异

VFS 对实际文件系统进行抽像,采用统一的数据结构在内存中描述所有实际的文件系统,向用户提供一组标准的文件操作函数。

VFS 负责将标准文件操作映射到实际文件系统的操作。 正是这种抽像和映射,保证了 Linux 系统可以支持多种介

质的不同文件系统,使所有文件系统都具有基本相同的外部表现

Page 23: 第五章   Linux 文件系统

23

virtual filesystem (VFS)VFS 通过一组统一的数据结构和操作函数接口实现抽象数据结构的建立

结构体的数据由实际文件系统的相应的数据来填充 各个数据结构的对象存在于内存

操作函数 操作函数由实际的文件系统实现

VFS依据这些对象提供的信息和操作函数来完成将用户的文件请求映射到实际文件系统的操作

Page 24: 第五章   Linux 文件系统

24

VFS 的四个主要数据结构superblock

Stores information relating to a specific mounted file systeminode

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 有关 在路径中,包括普通文件在内,每一个部分都是目录项对象

Page 25: 第五章   Linux 文件系统

Superblock (1)每个文件系统都有自己的 superblock 结构体

superblock 结构体用来存储文件系统的相关信息• 设备标识符、文件系统标志、数据块大小、文件系统的特殊信息、

文件系统的使用信息、超级块操作集指针 superblock又叫做 file system superblock 或者 filesystem

control block superblock 和文件系统一一对应 , 代表一个实际的文件系统 superblock 在文件系统挂装时建立,在文件系统卸载后撤销

Page 26: 第五章   Linux 文件系统

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

Page 27: 第五章   Linux 文件系统

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

Page 28: 第五章   Linux 文件系统

28

Superblock (4) struct file_system_type *s_type;

super_block与 file_system_type之间的关系

Page 29: 第五章   Linux 文件系统

29

Superblock (5)

struct list_head s_dirty; • points to the first and last elements in the list of dirty inodes

belonging to this filesystem.

super_block与 inode之间的关系

Page 30: 第五章   Linux 文件系统

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.

Page 31: 第五章   Linux 文件系统

Superblock (7)基于磁盘的文件系统的 superblock 存放在磁盘特定扇区

在系统运行过程中,由于文件操作会修改 superblock 的内容,造成与磁盘上的 superblock 内容不一致, VFS 通过周期性地将所有发生改变的 superblock 写回磁盘来实现superblock 的同步更新

非基于磁盘的文件系统的 superblock(例 sysfs) 实时地构建并存在于内存中

Blockgroup1

Blockgroup2

Blockgroup3

Blockgroup4

superblockBlock bitmap

inode bitmap

inode table

Data Block

启动扇区

文件系统描述信息

Page 32: 第五章   Linux 文件系统

Superblock Operations (1)superblock 结构体中 s_op指向一个 super_operations

结构体 ( 与具体文件系统有关一组函数指针 ) include/linux/fs.h L970

Page 33: 第五章   Linux 文件系统

Superblock Operations (2)super_operations 结构体中的函数按操作对象主要分两

类 操作文件系统 superblock

• sb -> s_op -> write_super(sb);

操作文件系统中各文件对应的 inode

Page 34: 第五章   Linux 文件系统

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()

Page 35: 第五章   Linux 文件系统

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);• 对文件系统解锁

Page 36: 第五章   Linux 文件系统

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 。

Page 37: 第五章   Linux 文件系统

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 写回磁盘

Page 38: 第五章   Linux 文件系统

38

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

Page 39: 第五章   Linux 文件系统

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.

Page 40: 第五章   Linux 文件系统

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; //引用计数

Page 41: 第五章   Linux 文件系统

41

inode (5) struct super_block *i_sb;

• points to the superblock of the filesystem in which the file resides

super_block与 dirty inode list 之间的关系

Page 42: 第五章   Linux 文件系统

42

inode (6)

unsigned long i_state;

inode_in_use

inode_unused

s_dirty

Page 43: 第五章   Linux 文件系统

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;

Page 44: 第五章   Linux 文件系统

44

inode_operations (1)inode_operations 结构体包含了操作 inode 对象的方法

include/linux/fs.h L936 i -> i_op -> function(i); // 调用方法

Page 45: 第五章   Linux 文件系统

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)

Page 46: 第五章   Linux 文件系统

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 的一大特色。

Page 47: 第五章   Linux 文件系统

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

Page 48: 第五章   Linux 文件系统

补充: ln (1)文件名与 dentry 有关,文件内容与 inode 有关考虑:有没有可能多个文件名对应于同一个 inode呢?hard link

删除操作• 如果将任何一个“文件名”删除,其 inode和 block 都还存在,可以

通过另一个“文件名”来读取到正确的文件数据 修改操作

• 不论使用哪个“文件名”编辑修改文件,最终结果都会写入相同的inode与 block 中,因此,都能进行完成数据的修改。

Note: hard link 不能对“目录”做链接

Page 49: 第五章   Linux 文件系统

补充: ln (2)Symbolic link

创建独立的 inode和 block, block指向源文件的文件名 删除操作

• 若删除了链接文件,源文件仍然存在• 但若删除了源文件,链接文件将无法访问源文件了

补充: Symbolic link 可以对目录做链接操作 Symbolic link 比较常用,其作用和 Windows环境下的快捷

方式相似

Page 50: 第五章   Linux 文件系统

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

Page 51: 第五章   Linux 文件系统

51

dentry 与 inode 的关系 (2)dentry 是对文件的逻辑描述

dentry 描述的是文件的逻辑标识 ( 文件名 ) 以及该文件与其他文件的逻辑关系 ( 其在目录树中的位置 )

VFS inode 描述文件的属性及其内容的物理存储dentry与 inode 结合可以完整地表示一个文件

dentry 可看作是访问一个文件的入口,查找文件就是在dentry链表中沿路径找到目标 dentry 的过程

Page 52: 第五章   Linux 文件系统

52

dentry (3)

dentry 结构体用来记录目录项 (directory entry) 路径名中的每一个目录项都对应一个 dentry e.g., /home/lkp/Chapter06.txt

• 为 /, home, lkp, and Chapter06.txt 分别创建 dentry

说明 不同于 superblock和 inode, dentry 没有对应的磁盘数据结

构 VFS根据路径字符串在内存中实时创建 dentry 对象 dentry 对象不需要写回磁盘

Page 53: 第五章   Linux 文件系统

dentry (4) include/linux/dcache.h L83

Page 54: 第五章   Linux 文件系统

dentry (5)dentry 状态

used• 表明该 dentry 对应于一个有效的 inode节点 (由 d_inode指向该

inode)• 表明该 dentry 存在一个或多个使用者 (d_count 为正值 )

unused• 表明该 dentry 对应于一个有效的 inode节点 (由 d_inode指向该

inode)• 但该 dentry 没有被使用 (d_count为 0)

negative• 表明该 dentry 没有对应有效的 inode(d_inode为 NULL)• 处于 negative的 dentry 可以被销毁 (还给 Slab的 dentry cache)

Page 55: 第五章   Linux 文件系统

55

dentry_operations (1)dentry_operations 结构体包含 VFS对 dentry 的各种操

作 include/linux/dcache.h L111

Page 56: 第五章   Linux 文件系统

dentry_operations (2) int (*d_revalidate) (struct dentry *dentry, int flags)

• 判断 dentry 是否有效• VFS准备从 slab的 dcache 中使用一个 dentry 时,调用该函数判

断 int (*d_hash) (struct dentry *dentry, struct qstr *name)

• 为 dentry 生成 hash值,加入 hash表,加速查找 int (*d_compare) (struct dentry *dentry,

struct qstr *name1, struct qstr *name2) • 比较文件名 name1和 name2• 使用该函数时,须先加锁 dcache_lock

int (*d_delete) (struct dentry *dentry) • 当 dentry中 d_count为 0 时, VFS 调用该函数。须先加

dcache_lock

void (*d_release) (struct dentry *dentry)• VFS 调用该函数释放 dentry

Page 57: 第五章   Linux 文件系统

57

file (1)从用户 ( 进程 )角度看

进程直接处理文件,关心文件的访问模式、以及读、写等操作,而不是 superblock、 inode、 dentry

file 结构体描述进程打开的文件 当进程 open() 一个文件, VFS将为它建立一个 file 结构

• file 结构– 文件打开模式 (f_mode)– 读 / 写位置指针 (f_pos)– 文件访问计数 (f_count)– 文件的目录项指针 (f_dentry)– 文件操作集指针 (f_op)

• file_operations 文件操作集结构体– 它由一组对文件内容进行操作的函数指针组成,如

read()、 wirte()、 lseek() 等。file 结构体因 open() 系统调而创建,因 close()而销毁

Page 58: 第五章   Linux 文件系统

58

file (2)file、 dentry、 inode三者关系

Page 59: 第五章   Linux 文件系统

59

进程与其相关文件之间的关系 (3)

file descriptorstdin, stdout, stderr

Page 60: 第五章   Linux 文件系统

进程与其相关文件之间的关系 (4)多个进程可以同时打开和操作同一个文件

同一个文件在内存中可能存在多个 file 结构体对象一个文件

对应的 dentry和 inode 是唯一的 但其对应的 file 结构体对象可能不唯一 ( 被多个进程打开“

读” )• 当然,也可以在多个进程之家共享同一个 file 结构体对象

– 若存在“读、写”矛盾,须同步 ( 信号量、加锁等 )

Page 61: 第五章   Linux 文件系统

61

file (5)file 结构体

include/linux/fs.h L577

Page 62: 第五章   Linux 文件系统

62

file (6) struct list_head f_list;

Page 63: 第五章   Linux 文件系统

63

file_operations (1)

include/linux/fs.h L908

对文件操作的系统调用函数与file_operations中的函数与很类似

除了通用函数,真实的文件系统为特定的操作函数做具体的实现

Page 64: 第五章   Linux 文件系统

64

file_operations (2) int (*open) (struct inode *inode, struct file *file);

• 打开一个文件,创建 file 对象,同时将它和相对应的 inode 关联起来• 由系统调用 open() 调用

int (*release) (struct inode *inode, struct file *file) • 进程调用了 close() 系统调用,或者进程退出时,释放 file 结构体;• 是否释放 inode、 dentry 要视是否还有其他进程占用而定

ssize_t (*read) (struct file *file, char *buf, size_t count, loff_t *offset);

• 从指定文件的 offset偏移处读取 count字节的数据到 buf 中• 更新文件指针• 由系统调用 read() 调用

ssize_t (*write) (struct file *file, const char *buf, size_t count, loff_t *offset);

• 从给定的 buf 中取 count字节的数据,写入指定文件的 offset处• 更新文件指针• 由系统调用 write() 调用

Page 65: 第五章   Linux 文件系统

file_operations (3) loff_t (*llseek) (struct file *file, loff_t offset, int origin);

• 更新偏移量 offset• 由系统调用 llseek() 调用

int (*mmap) (struct file *file, struct vm_area_struct *vma);• 将文件映射到指定地址的存储空间上• 由系统调用 mmap() 调用

int (*fsync) (struct file *, struct dentry *, int datasync);• 将文件的数据同步到磁盘• 由系统调用 fsync() 调用

Page 66: 第五章   Linux 文件系统

66

file_operations (4)

int (*lock) (struct file *file, int cmd, struct file_lock *lock);• 给指定的文件上锁

ssize_t (*sendfile) (struct file *file, loff_t *offset, size_t count,

read_actor_t actor, void *target);• 从文件 file的 offset 开始拷贝 ( 发送 )count字节到 target• 由系统函数 sendfile() 调用

Page 67: 第五章   Linux 文件系统

和文件系统相关的数据结构 (1)除了以上 4种 VFS 基础数据结构,内核还提供了一些标准数据结构用来管理文件系统的其他相关数据。 file_system_type

• 描述各种文件系统类型 vfsmount

• 描述文件系统实例的挂载

Page 68: 第五章   Linux 文件系统

和文件系统相关的数据结构 (2)file_system_type (include/linux/fs.h L1137)

在一个运行的 OS 中,对特定的一种文件系统来说,内存中可能存在多个 super_block 结构体对象,但只有一个file_system_type 对象

Page 69: 第五章   Linux 文件系统

和文件系统相关的数据结构 (3)vfsmount

include/linux/mount.h L23

Page 70: 第五章   Linux 文件系统

70

VFS 全局变量 (1)

Page 71: 第五章   Linux 文件系统

71

VFS 全局变量 (2)

Page 72: 第五章   Linux 文件系统

72

VFS 全局变量 (3)

Page 73: 第五章   Linux 文件系统

73

VFS 全局变量 (4)

Page 74: 第五章   Linux 文件系统

下课了!

Page 75: 第五章   Linux 文件系统

75

第五章 Linux 文件系统概述ext2和 ext3 文件系统Linux 虚拟文件系统

superblock inode dentry file

与进程相关的 VFS 数据结构 files_struct fs_struct

与文件相关的常用 Shell 命令

Page 76: 第五章   Linux 文件系统

76

与进程相关的 VFS 数据结构 (1)每个进程都有一组打开的文件

stdin、 stdout、 stderr 文件系统根目录、当前工作目录、挂载点等

VFS 为进程提供了访问文件系统的接口 files_struct : 进程打开的文件及其文件描述符 fs_struct : 进程与文件系统的相关信息 namespace: 使进程有唯一根目录,唯一文件系统层次结构

Page 77: 第五章   Linux 文件系统

77

与进程相关的 VFS 数据结构 (2)files_struct (include/linux/file.h L22)

“Open file table” 进程打开的文件及其文件描述符

NR_OPEN_DEFAULT 为 32 默认情况下, fd指向 fd_array;当打开的文件数超过 32 时

,由 fd指向新开创的文件描述符数组

Page 78: 第五章   Linux 文件系统

78

与进程相关的 VFS 数据结构 (3)fs_struct (include/linux/fs_struct.h L7)

进程与文件系统的相关信息

Page 79: 第五章   Linux 文件系统

与进程相关的 VFS 数据结构 (4)namespace (include/linux/namespace.h L8)

由 task_struct的 namespace字段指向该 namespace 结构体

使每个进程看到唯一的根目录,唯一的文件系统层次结构

Page 80: 第五章   Linux 文件系统

80

第五章 Linux 文件系统概述ext2和 ext3 文件系统Linux 虚拟文件系统

superblock inode dentry file

与进程相关的 VFS 数据结构 files_struct fs_struct

与文件系统相关的常用 Shell 命令

Page 81: 第五章   Linux 文件系统

与文件相关的 shell 命令 (1)目录操作

ls (list) mkdir (make directory) rmdir (remove directory) cd (change directory) pwd (print working directory)

文件和目录 复制

• cp (copy)

移动• mv (move)

删除• rm (remove)

umask tree

文件操作 cat (concatenate and display

files) more less wc (word count) sort touch file type

特殊目录 . .. ~ /

链接 ln (link)

Page 82: 第五章   Linux 文件系统

与文件相关的 shell 命令 (2)磁盘与目录容量

df (disk usage of filesystem) du (disk usage of each FILE)

挂载 mount umount

挂载记录文件 /etc/fstab 开机挂载

• file systems table

/etc/mtab 动态挂载记录• mounted file systems table

/proc/mounts 动态挂载记录

更改文件属性 chown chgrp chmod

文件查找 which whereis locate find grep

Page 83: 第五章   Linux 文件系统

df (disk usage of filesystem)df (report file system disk space usage)

df [-akmhiHT] [ 目录或者文件名 ] -a : all, include dummy file systems -h : human-readable 以 GB/MB/KB格式显示 -T : print file system type -i : list inode information instead of block usage

E.g. df df -h df –ah df –ahT df -ih df -h /etc

• 后面是文件或者目录时,显示文件或目录所在的磁盘分区的容量信息说明: df 命令从 superblock 中读取数据,故速度快

Page 84: 第五章   Linux 文件系统

du (disk usage of each FILE)

du (estimate file space usage) du [-ahskm] 文件或者目录 -a : for all files, not just directories -h : print sizes in human readable format -s : display only a total for each argument

E.g. du /home //列出 /home 下面各目录的大小 du -a /home //列出 /home 下面各个目录和文件的大小 du -ah /home du -sh /home //只显示 /home总量,不显示子目录和文件 du –s /*

说明 : du深入到文件系统中统计每个文件的属性信息

Page 85: 第五章   Linux 文件系统

与文件相关的 shell 命令 (2)磁盘与目录容量

df (disk usage of filesystem) du (disk usage of each FILE)

挂载 mount umount

挂载记录文件 /etc/fstab 开机挂载

• file systems table

/etc/mtab 动态挂载记录• mounted file systems table

/proc/mounts 动态挂载记录

更改文件属性 chown chgrp chmod

文件查找 which whereis locate find grep

Page 86: 第五章   Linux 文件系统

挂载挂载 (mount)

将一个文件系统 ( 设备 ) 与一个挂载点相关联• the filesystem is attached and accessible from the main tree

挂载点 (mount point) 挂载点应该是整个文件系统目录树中的一个目录

• 常用 /mnt 空目录作为挂载点来挂载• 或者在 /mnt 下建立新的空目录作为挂载点

一个挂载点不应该重复挂载多个文件系统

Page 87: 第五章   Linux 文件系统

87

mount 挂载OS启动时, mount 自动按照 /etc/fstab 中的记录挂载

mount -a• 按照 /etc/fstab 配置文件中,将所有未挂载的选项都挂载上来

mount [-t vfstype] [-o options] device dire.g.

在一个双系统机器上,将Windows 下分区 /dev/sda7挂载到 Linux 下

• fdisk -l• mount -t vfat -o ro /dev/sda7 /mnt //ro: read only

挂载光驱• mount –t iso9660 /dev/cdrom /mnt/cdrom

挂载 LABEL=/home 分区到 /home• cat /etc/fstab• mount LABEL=/home /home

Page 88: 第五章   Linux 文件系统

umount 取消挂载umount (unmount file systems)

-f :进行强制卸载 两种卸载方式

• umount filesystem• umount mountpoint

即• mount –t iso9660 /dev/cdrom /mnt/cdrom• umount /mnt/cdrom 或• umount /dev/cdrom

经验: 卸载前,用户要退出挂载的文件系统目录,否则卸载会因

为文件系统 busy而无法卸载

Page 89: 第五章   Linux 文件系统

与文件相关的 shell 命令 (2)磁盘与目录容量

df (disk usage of filesystem) du (disk usage of each FILE)

挂载 mount umount

挂载记录文件 /etc/fstab 开机挂载

• file systems table

/etc/mtab 动态挂载记录• mounted file systems table

/proc/mounts 动态挂载记录

更改文件属性 chown chgrp chmod

文件查找 which whereis locate find grep

Page 90: 第五章   Linux 文件系统

开机挂载文件 /etc/fstabcat /etc/fstab //file systems table

说明 fdisk –l 可查看设备名列表 可通过 dumpe2fs /dev/sda1 查看文件系统信息

每次开机挂载 nfs server 上的目录,则在 /etc/fstab 中添加一行 mount -t nfs -o nosuid,ro hostname:/directory /mountponit

若每次开机挂载 windows 下的 /dev/sda7 ,则添加一行 mount [-t vfat] /dev/sda7 /mnt

设备名或设备label

mount point Filesystem Type

FS参数, rw/ro

dump?0 不1每天2 不定期

fsck?0 不检

查1 最先2 其次

Page 91: 第五章   Linux 文件系统

动态挂载记录 /etc/mtab 、 /proc/mounts/etc/fstab

OS 在启动时,读取 /etc/fstab ,然后按照其指示进行挂载文件系统那么,在系统运行过程中,用户动态挂载的文件系统记录在哪里?

/etc/mtab /proc/mounts

例子 (1) 看系统中目前挂载了 /home

• cat /etc/mtab //注意 : 有 /home 行• cat /proc/mounts

(2) 卸载 /home • umount /home

(3) 查看,是否没了 /home 行• cat /etc/mtab

• cat /proc/mounts

Page 92: 第五章   Linux 文件系统

与文件相关的 shell 命令 (2)磁盘与目录容量

df (disk usage of filesystem) du (disk usage of each FILE)

挂载 mount umount

挂载记录文件 /etc/fstab 开机挂载

• file systems table)

/etc/mtab 动态挂载记录• mounted file systems table

/proc/mounts 动态挂载记录

更改文件属性 chown chgrp chmod

文件查找 which whereis locate find grep

Page 93: 第五章   Linux 文件系统

chown chgrpchown [-R] [owner] :[group] [file name]chgrp [-R] [group] [file name]

-R: operate on files and directories recursively 例

chown amao test.txt chgrp xjtu test.txt chown amao:xjtu test.txt

Page 94: 第五章   Linux 文件系统

chmodchmod (change mode) 设定权限

= chmod u=rwx, go=rx test.sh

+ chmod o+w test.sh chmod +x test.sh 等同于 chmod a+x test.sh

- chmod o-x test.sh (对 other 用于去掉 x 权限 )

用数字表示 chmod 755 test.sh

Page 95: 第五章   Linux 文件系统

与文件相关的 shell 命令 (2)磁盘与目录容量

df (disk usage of filesystem) du (disk usage of each FILE)

挂载 mount umount

挂载记录文件 /etc/fstab 开机挂载

• file systems table

/etc/mtab 动态挂载记录• mounted file systems table

/proc/mounts 动态挂载记录

更改文件属性 chown chgrp chmod

文件查找 which whereis locate find grep

Page 96: 第五章   Linux 文件系统

文件查找 (1)which

在当前用户的 $PATH 中查找指定的可执行文件 ( 命令 )

Page 97: 第五章   Linux 文件系统

文件查找 (2)whereis

利用数据库来查找文件

locate 利用数据库模糊查找文件

Page 98: 第五章   Linux 文件系统

文件查找 (3)find

find [path] [option] [action]

find / -mtime 0 //找出从现在算起 24 小时内修改过的文件 find /home -user jack //找出主人为 jack 的文件

find / -name ifconfig find /usr -name “*httpd*” //模式匹配,模糊查找

find / -size +1000k -exec ls -l {} \;

Page 99: 第五章   Linux 文件系统

文件中查找行 ( 内容 ) grep

grep (global search regular expression(RE) and print out the line, 全面搜索正则表达式并把行打印出来)

grep 是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。

举例 grep bash trap.sh unset.sh grep -v bash trap.sh unset.sh // 没有包含 bash 的行 grep -c bash trap.sh unset.sh //输出出现过的行数 可以和其他命令结合适用,如:

• netstat -npl | grep xinetd

Page 100: 第五章   Linux 文件系统

100

第五章 Linux 文件系统概述ext2和 ext3 文件系统Linux 虚拟文件系统

superblock inode dentry file

与进程相关的 VFS 数据结构 files_struct fs_struct

与文件相关的常用 Shell 命令

Page 101: 第五章   Linux 文件系统

下课了,开心一下 !