chapter 12: file system implementation 文件系统实现

104
Silberschatz, Galvin and Gagne 2002 12.1 Operating System Concepts Chapter 12: File System Implementation 文文文 文文 File System Structure 文文文 文文 File System Implementation 文文文 文文 Directory Implementation 文文文文 Allocation Methods 文文文文 Free-Space Management 文文文文文文 Efficiency and Performance 文文文文文 Recovery 文文 Log-Structured File Systems 文文文文文文文 NFS 文文文文文

Upload: xanthus-clemons

Post on 03-Jan-2016

158 views

Category:

Documents


0 download

DESCRIPTION

Chapter 12: File System Implementation 文件系统实现. File System Structure 文件系统结构 File System Implementation 文件系统实现 Directory Implementation 目录实现 Allocation Methods 分派方法 Free-Space Management 空闲空间管理 Efficiency and Performance 效率和性能 Recovery 恢复 Log-Structured File Systems 日志结构文件系统 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.1Operating System Concepts

Chapter 12: File System Implementation文件系统实现

File System Structure 文件系统结构 File System Implementation 文件系统实现 Directory Implementation 目录实现 Allocation Methods 分派方法 Free-Space Management 空闲空间管理 Efficiency and Performance 效率和性能 Recovery 恢复 Log-Structured File Systems 日志结构文件系统 NFS 网络文件系统

Page 2: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.2Operating System Concepts

12.1 File-System Structure( 文件系统结构)

File structure (文件结构) Logical storage unit (逻辑存储单元)Collection of related information (相关信息的集

合) File system resides on secondary storage (disks).

(文件系统存在于辅助存储器中—磁盘) File system organized into layers. (文件系统按层组

织) File control block – storage structure consisting of

information about a file.(文件控制块 FCB :由一个文件的相关信息组成的存储结构)

Page 3: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.3Operating System Concepts

Layered File System

Page 4: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.4Operating System Concepts

Layered File System

I/O control, the lowest level Device drivers Interrupt handlers

Basic file system Issue generic commands to read and write

File-organization module File and their logical bocks, physical blocks Free space manger

Logical file system Manage metadata information Manage directory structure Maintain file structure via file control blocks Responsible for protection and security

Page 5: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.5Operating System Concepts

Layered File System

Application Unix: UNIX file system(UFS) Windows NT: FAT, FAT32,NTFS Linux: ext、 ext2、msdos、 vfat、 iso9660、 hpfs等多种文件系统。使用命令 mkfs 创建各类文件系统

Page 6: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.6Operating System Concepts

文件系统的类型 FAT 文件系统( MS-DOS 文件系统、 msdos)    它是 MS-DOS 操作系统使用的文件系统,它也能由Windows98/NT、 linux、 SCO UNIX 等操作系统访问。文件地址以 FAT 表结构存放,文件目录 32B ,文件名为 8个基本名加上一个“ .” 和 3 个字符扩展名。

FAT32 文件系统( vfat) 它是 Windows98 使用的扩展的 DOS 文件系统,它在 MS-DOS 文件系统基础上增加了对长文件名(最多到 256B )支持。

NTFS(NT 文件系统) 它是 Windows NT 操作系统使用的文件系统,它具有很强的

安全特性和文件系统恢复功能,可以处理巨大的存储媒体,支持多种文件系统。

S51K/S52K( sysv)   它是 AT&T UNIX S V  操作系统使用的文件系统。

Page 7: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.7Operating System Concepts

文件系统的类型 ext2 (二级扩展文件系统) 它是 Linux 操作系统使用的高性能磁盘文件系统,它是对Minux 操作系统中使用的文件系统扩展( ext )的扩展。它支持 256 字符的文件名,最大可支持到 4TB 的文件系统大小。

HPFS (高性能文件系统、 hpfs)   它是 OS/2 操作系统使用的文件系统。 CD-ROM 文件系统 (iso9660) 它是符合 ISO9660 标准的支持 CD-ROM 的文件系统,它有

High sierra CD-ROM和 Rock Ridge CD-ROM 二种类型。

UDF 通用磁盘格式文件系统 UDF(Universal Disk Format) 文件系统是依据光学储存技术

协会( Optical Storage Technology Association, OSTA )的通用磁盘格式文件系统规格 1.02 版所制定的。它提供了对 UDF 格式媒体的只读访问(例如 DVD 光盘)。 Windows98 提供对 UDF 文件系统支持。

Page 8: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.8Operating System Concepts

操作系统 (Windows98/NT 、 Linux ) 对多种文件系统的支持

WindowsNT 多重文件系统 Windows NT 支持 FAT 文件系统、 NTFS、 HPFS、 CD-ROM 文件系统等多种文件系统。 Windows NT 执行体内I/O 系统分成 I/O 管理程序、文件系统驱动程序和盘驱动程序三层,不同的文件系统采用不同的文件系统驱动程序,系统用动态连接库对这些文件系统进行装入和卸出并适宜于将来的扩展, Windows NT 分层驱动程序如下图所示。

Windows NT 磁盘管理器窗口显示某台微机硬盘各分区安装的多种文件系统如下图所示,图中所示硬盘有二个分区, C盘是 FAT 文件系统, D 盘是 NTFS 文件系统, E 盘是 CD-ROM 文件系统。

Page 9: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.9Operating System Concepts

WindowsNT 多重文件系统 -1

WindowsNT 分层驱动程序

I/O 管理程序

FAT 驱动程序

NTFS 驱动程序

HPFS 驱动程序

软盘 驱动程序

硬盘 驱动程序

Page 10: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.10Operating System Concepts

WindowsNT 多重文件系统 -2

Page 11: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.11Operating System Concepts

Linux 虚拟文件系统 VFS Linux 采用虚拟文件系统 VFS 支持许多不同类型的文件系

统, VFS 是物理系统与服务之间的一个接口层,它屏蔽各类文件系统的差异,给用户和程序提供一个统一的接口。 Linux 支持ext、 ext2、msdos、 vfat、 iso9660、 hpfs 等多种文件系统。使用命令 mkfs 创建各类文件系统。

VFS 是物理文件系统与服务之间的一个接口,它对 Linux的每一个文件系统的所有细节进行抽象,使得不同的文件系统在 Linux 内核以及系统中运行的其它进程看来都是相同的。严格说来 VFS 并不是一种实际的文件系统,它只存在于内存中,不存在于任何外存空间, VFS 在系统启动时建立,在系统关闭时消亡。

VSF使 Linux 同时安装支持不同类型的文件系统成为可能。

Page 12: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.12Operating System Concepts

Linux 虚拟文件系统 VFS-1 VFS 拥有关于各种特殊文件系统的公共接口,如超级块

incde ,文件操作函数入口等。特殊文件系统的细节问题统一由 VFS 的公共接口来翻译,它们对系统内核和用户进程是透明的, VFS 与实际文件系统的封装关系如下:

ext2 FSMINIX FS ext FS MSDOS FS

Buffer Cache

I/O 设备驱动

VFS

Page 13: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.13Operating System Concepts

Schematic View of Virtual File SystemVFS 示意图

Page 14: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.14Operating System Concepts

文件系统模型

文件系统接口

逻辑文件系统

基本 I/ O管理程序(文件组织模块)

基本文件系统(物理 I/O层)

对对象操纵和管理的软件集合

I/O控制层(设备驱动程序)

对象及其属性说明

Page 15: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.15Operating System Concepts

12.2 File System Implementation

Overview On-disk and in-memory structure To create a new file To use a file To close a file

Partitions and Mounting Virtual File System

Page 16: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.16Operating System Concepts

12.2.1 Overview

On-disk structures: A boot control block

Boot block in UFS Partition boot sector in NTFS

A partition control block—contains partition details Called Superblock in UFS Called Master File Table in NTFS

A directory structure A File Control block(FCB)–--contains many of a file’s

details Called inode in UFS Included in Master File Table in NTFS

Page 17: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.17Operating System Concepts

Overview(2)

In-memory information : used for file-system management and performance improvement via caching in-memory partition table– information about each

mounted partition in-memory directory structure– recently accessed

directory infomation System-wide open-file table, a copy of the FCB of

each open file Per-process open-file table, a pointer to the

appropriate entry in the system-wide open-file table

Page 18: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.18Operating System Concepts

Create, open, use and close a file

Create a new file: Locate a new FCB Read appropriate directory into memory Update it with the new file name and FCB Write it back to the disk

Open a file Find the file Copy the FCB to a system-wide open-file table in

memory Made an entry in the per-process open-file table Return a pointer to the entry in the per-process open-

file table File descriptor in Unix File handler in Windows

Page 19: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.19Operating System Concepts

Create, open, use and close a file(2)

Use All operations are performed via the file pointer

Close a file The entry in per-process open-file table is removed Open count in system-wide open-file table is

decremented. If the open count is 0, then the entry is removed

Page 20: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.20Operating System Concepts

A Typical File Control Block

Page 21: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.21Operating System Concepts

In-Memory File System Structures

The following figure illustrates the necessary file system structures provided by the operating systems.

下面图例描述了 OS 中文件系统所需的系统结构 Figure 12-3(a) refers to opening a file.

Figure 12-3(b) refers to reading a file.

Page 22: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.22Operating System Concepts

In-Memory File System Structures

Page 23: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.23Operating System Concepts

12.2.2 Partitions and Mounting

A disk be sliced into multiple partitions A partition span multiple disks

RAID

“raw partition/disk”– no file system “cooked partition”– contain a file system Boot information in a separate partition Root partition

OS kernel Other system file potentially

Mount table in its in-memory contains the information of mounted file systems

Page 24: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.24Operating System Concepts

12.2.3 Virtual File Systems

Virtual File Systems (VFS) provide an object-oriented way of implementing file systems.

虚拟文件系统( VFS) 提供一个面向对象的文件系统实现方法 VFS allows the same system call interface (the API)

to be used for different types of file systems(API).

VFS 允许不同类型的文件系统使用相同的系统调用接口 (API)

The API is to the VFS interface, rather than any specific type of file system.

API 作为 VFS 接口要好于规定的文件系统类型 三个层次

Page 25: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.25Operating System Concepts

Schematic View of Virtual File SystemVFS 示意图

Page 26: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.26Operating System Concepts

12.3 Directory Implementation (目录实现)

Linear list of file names with pointer to the data blocks. (线性的文件名列表,包括指向数据块的指针) simple to program (容易编程) time-consuming to execute (to find a file )

(耗 CPU 的执行时间) Improve: cache the most recently used directory

information

Hash Table – linear list with hash data structure.

(哈希表—有着哈希数据结构的线性表) decreases directory search time (减少目录的搜索时间) collisions – situations where two file names hash to the

same location (碰撞:两个名字映射到同样的位置) fixed size (固定长度)

需要改变长度时 (e.g. 0-640-127), 必须重新组织目录项

Page 27: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.27Operating System Concepts

Directory Implementation (+)

为了实现用户对文件的按名存取,系统必须首先利用用户提供的文件名,对文件目录进行查询,找出该文件的文件控制块 FCB ,对UNIX 系统即要找出该文件的索引节点。然后根据找到的 FCB 中所记录的文件物理地址,(对UNIX 系统即是索引节点中的用于 UNIX直接、间接混合寻址的 13个地址项 di-addr[13]),并根据文件物理组织方式找出文件的盘块号,进而换算出文件在磁盘上的物理位置(柱面号、磁头号、扇区号),最后启动磁盘驱动程序,将所需文件读入内存。

对目录查询的技术有两种:线性检索法和 Hash 法。这里介绍UNIX 中使用的线性检索法,又称为顺序检索法。

Page 28: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.28Operating System Concepts

UNIX树型的目录结构

ls

I=1

bin boot unix usr mnt etc tmp dey 2 3 4 5

I=2 I=5

ls10

dir10

I=10

返8

Page 29: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.29Operating System Concepts

目录查询技术 下面以在上图所示 UNIX树型目录中查找文件 /bin/ls 为

例介绍线性检索法。 首先系统读入根索引节点(其索引节点号为 1 ),从文件地址项查

找根目录文件所在物理块号读入内存。同时从用户提供的文件名中读入根目录下第一个文件分量 bin , 用它与根目录文件中各个目录项的文件名顺序地进行比较,从中找到匹配号,得到匹配项的索引节点号为 2 。

然后将磁盘第 2 个索引节点读入内存,从中找出 bin 目录文件所在物理块号,并将它读入内存。同时从用户提供的文件名中读入第二个文件分量 ls ,用它与 bin 目录文件中目录项的文件名顺序地进行比较,从中找出匹配号,得到匹配项的索引号为 10。尔后,将磁盘第 10号节点读入内存,从中判断 ls 文件所在的物理块号。目录查询操作到此结束,如果顺序查找过程中发现一个文件分量名未能找到,则停止查找并送回“文件未找到”信息。

Page 30: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.30Operating System Concepts

12.4 Allocation Methods

An allocation method refers to how disk blocks are allocated for files:

一种分派方法指出如何为文件分派磁盘块 Contiguous allocation

连续分派 Linked allocation

链接分派 Indexed allocation

索引分派

Page 31: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.31Operating System Concepts

12.4.1 Contiguous Allocation(连续分配 )

连续分配 (contiguous) :只需记录第一个簇的位置,适用于预分配方法。可以通过紧缩 (compact)将外存空闲空间合并成连续的区域。

Each file occupies a set of contiguous blocks on the disk. (每一个文件占用一个连续的磁盘块的集合)

Simple – only starting location (block #) and length (number of blocks) are required. (简单:只需要起始块号和长度)

Random access.( 可以随机存取 ) Wasteful of space (dynamic storage-allocation problem).

(浪费空间:动态存储分配问题) Files cannot grow. (文件不能增长) External fragmentation

Improve: compact

Page 32: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.32Operating System Concepts

Contiguous Allocation of Disk Space

Page 33: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.33Operating System Concepts

Extent-Based Systems (基于长度系统)

Many newer file systems (i.e. Veritas File System) use a modified contiguous allocation scheme.

许多新的文件系统使用一种修改连续分派方法 Extent-based file systems allocate disk blocks in extents.

基于长度文件系统以定长分派磁盘块 An extent is a contiguous block of disks. Extents are

allocated for file allocation. A file consists of one or more extents.

一个长度是一段连续的磁盘块,(文件分派以长度为单位),一个文件有一个或多个长度

Page 34: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.34Operating System Concepts

12.4.2 Linked Allocation (链接分配)

链式分配 (chained):在每个簇中有指向下一个簇的指针。可以通过合并 (consolidation)将一个文件的各个簇连续存放,以提高 I/O访问性能。

Each file is a linked list of disk blocks: blocks may be scattered anywhere on the disk. 每个文件是一个磁盘块的链接列表:块可以分散在磁盘各处

pointerblock =

Page 35: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.35Operating System Concepts

Linked Allocation (Cont.) (链接分配,续)

Simple – need only starting address (简单:只需要起始地址) Free-space management system – no waste of space

(自由空间管理系统:没有磁盘空间浪费) No random access (无法随机存取) Mapping (映射)

Block to be accessed is the Qth block in the linked chain of blocks representing the file.

Displacement into block = R + 1 File-allocation table (FAT) – disk-space allocation used by

MS-DOS and OS/2. (文件分配表:被MS-DOS和 OS/2 所采用)

LA/511Q

R

Page 36: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.36Operating System Concepts

Linked Allocation

Page 37: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.37Operating System Concepts

Linked Allocation

Disadvantages Effectively only for sequential-access files,

inefficient to support a direct-access capability Space required for the pointers

Improve: clusters Cost: increase internal fragmentation

Problem of reliability Improve

FAT

Page 38: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.38Operating System Concepts

File-Allocation Table ( FAT链接结构)

Page 39: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.39Operating System Concepts

MS-DOS / Windows98 FAT 表结构 MS-DOS 文件系统的文件物理结构采用 FAT 表结构。该结构为了克服链接文件随机读取任一逻辑块需要化费多次盘 I/O 操作的不足,将各盘块中的链接指针集中存放在盘的开始部分,构成一张表,称为 FAT表。 FAT 表每一项存放链接指针(下一个簇号),每个 FAT 表项占 12位或 16 位,称为 FAT12或 FAT16 。对于软盘因为容量小,簇数也少,采用 12位 FAT 表,对于硬盘则采用 16位 FAT 表。

FAT 表文件系统原为小硬盘的目录结构而设计,由于簇的数目最多只能用 16 位表示,即最多只能有 64K 个簇,要用 FAT 表管理大的磁盘分区,只能采取增大每簇所包含的扇区数,一般根据磁盘的类型和容量大小来决定簇的大小,如下表所示。当然每簇包含扇区数增加,带来内碎片的浪费,这对小文件特别严重。

Windows98 为了减少内另头的浪费,可采取每簇的数目用 32 位表示,减少每簇包含扇区数,这称为 FAT32 。 FAT16 、 FAT32 文件系统簇和扇区关系也见下表所示。

Page 40: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.40Operating System Concepts

12.4.3 Indexed Allocation (索引分配)

索引分配 (indexed) :文件的第一个簇中记录了该文件的其他簇的位置。可以每处存放一个簇或连续多个簇(只需在索引中记录连续簇的数目)。

Brings all pointers together into the index block. (把所有的指针都一起放在索引块里)

Logical view. (逻辑形式)

index table

Page 41: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.41Operating System Concepts

Example of Indexed Allocation

Page 42: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.42Operating System Concepts

Indexed Allocation (Cont.)索引分配 续 .

Need index table (需要索引表) Random access (随机存取) Dynamic access without external fragmentation , but have

overhead of index block. (可以动态存取而没有外部碎片) Mapping from logical to physical in a file of maximum size of

256K words and block size of 512 words. We need only 1 block for index table. (为了从逻辑映射到实际的文件系统,一个 256K字的文件,块为 512 字,我们只需一个块作为索引表)

LA/512Q

R

Q = displacement into index tableR = displacement into block

Page 43: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.43Operating System Concepts

Indexed Allocation – Mapping (Cont.)索引分配—映射(续)

Mapping from logical to physical in a file of unbounded length (block size of 512 words). (无限长度的文件从逻辑向物理的映射)

Methods for large file Linked scheme

Link together several index blocks Multilevel index Combined scheme (e.g. 15 pointers)

Direct blocks (e.g. 12 pointers) Indirect blocks (e.g. 3 pointers)

– Single indirect block– Double indirect block– Triple indirect block

Page 44: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.44Operating System Concepts

Indexed Allocation – Mapping (Cont.)索引分配—映射(续)

Mapping from logical to physical in a file of unbounded length (block size of 512 words). (无限长度的文件从逻辑向物理的映射)

Linked scheme – Link blocks of index table (no limit on size). (链接策略—把索引块链接起来)(没有长度限制)

LA / (512 x 511)Q1

R1Q1 = block of index tableR1 is used as follows:

R1 / 512Q2

R2

Q2 = displacement into block of index tableR2 displacement into block of file:

Page 45: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.45Operating System Concepts

Indexed Allocation – Mapping (Cont.)索引分配—映射(续)

Two-level index (maximum file size is 5123)

LA / (512 x 512)Q1

R1

Q1 = displacement into outer-indexR1 is used as follows:

R1 / 512Q2

R2

Q2 = displacement into block of index tableR2 displacement into block of file:

Page 46: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.46Operating System Concepts

Indexed Allocation – Mapping (Cont.)

outer-index

index table file

Page 47: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.47Operating System Concepts

UNIX/Linux直接间接混合分配方式 由于 80%以上文件是小文件,为了解决能高速存取小文件和

管理大文件的矛盾, UNIX将直接寻址、一级索引、二级索引和三级索引结合起来,形成了混合寻址方式,如下图所示。

在UNIX S V 的索引结点中设有 13 个地址项 di_addr[13]( Linux的 ext2设有 15 个地址项)它们把所存的地址项分成两类,其中最后三个地址项分别是一级索引、二级索引和三级索引的指针,而前面 10 个( ext2为 12 个)为直接寻址的地址项,即存放文件逻辑块第0 - 9 块的盘块号。

如每个盘块大小为 4KB 时,当文件不大于 40KB 时,便可直接从索引结点中读出该文件全部盘块号,这样读小文件时速度快;如文件大于 40KB 时,系统再逐步增加一级索引、二级索引和三级索引,这样最大管理的文件为 40KB+ 4MB+4GB+ 4TB ,达到管理大文件的目标。

Page 48: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.48Operating System Concepts

Combined Scheme: UNIX (4K bytes per block)

Page 49: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.49Operating System Concepts

例 1

一个文件系统中有一个 20MB大文件和一个 20KB小文件 , 当分别采用连续、链接、链接索引、二级索引和 UNIX Sytem V 分配方案时 ( 每块大小为 4096B, 每块地址用 4B 表示 ) ,问 :1. 各文件系统管理的最大的文件是多少 ? 2. 每种方案对大、小二文件各需要多少专用块来记录文件的物理地址 (

说明各块的用途 ) ?3.如需要读大文件前面第 5.5KB 和后面( 16M+ 5.5KB )信息,则每个方案各需要多少次盘 I/O 操作 ?

这个范例是帮助读者深入比较文件物理组织的各种方案:顺序文件的连续分配、链接文件的链接分配、二级索引分配、链接索引分配和UNIX 的直接间接混合分配,明确各种分配方案的优缺点和 UNIX 分配方案的设计特点。

Page 50: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.50Operating System Concepts

例 1-解答1. 各种分配方案的文件系统可管理的最大文件 连续分配:不受限制,可大到整个磁盘文件区。 链接分配:同上。 链接索引:同上。 二级索引:由于盘块大小为 4KB ,每个地址用 4B 表示,一个盘

块可存 1K 个索引表目,二级索引可管理的最大文件容量为4KB×1K×1K= 4GB ,如要管理更大的文件需采用三索引,它可管理 4TB大小文件。

UNIX混合分配:可管理的最大文件为 40KB+ 4MB+4GB+4TB 。

2. 每种分配方案对 20MB大文件和 20KB小文件各需要多少专用块来记录文件的物理地址 ?

连续分配:对大小二个文件都只需在文件控制块 FCB 中设二项,一是首块物理块块号,另一是文件总块数,不需专用块来记录文件的物理地址。

Page 51: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.51Operating System Concepts

例 1-解答 链接分配:对大小二个文件都只需在文件控制块 FCB 中设二项,一是

首块物理块块号,另一是文件总块数;同时在每块存文件的物理块中设置存贮下一块块号的指针。

链接索引:对 20KB小文件只有 5 个物理块大小,所以只需一块专用物理块来作索引块,用来保存文件的各块物理块块号。对于 20MB大文件有 5K 个物理块大小,由于链接索引的每个索引块只能保存( 1K- 1 )个物理块块号(还有一个表目作索引块链接指针),所以它需要 6 块专用物理块来作链接索引块,用于保存文件各块的物理地址。

二级索引:对大小文件都固定要用二级索引,对 20KB小文件,用一块作第一级索引,用另一块作二级索引,共用二块专用物理块作索引块,对于 20MB大文件,用一块作第一级索引,用 5 块作第二级索引,共用六块专用物理块作索引块。

Page 52: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.52Operating System Concepts

例 1-解答  UNIX 的混合分配:对 20KB 小文件只需在文件控制块FCB的 i_addr[13] 中使用前 5 个表目存放文件的物理块号,不需专用物理块。对 20MB 大文件, FCB的 i_addr[13]中使用前 10 个表目存放大文件前 10 块物理块块号,用一级索引块一块保存大文件接着的 1K 块块号,还要用二级索引存大文件以后的块号,二级索引使用第一级索引 1 块,第二级索引 4 块。总共也需要 6 块专用物理块来存放文件物理地址。

3. 为读大文件前面第 5.5KB 和后面( 16M+ 5.5KB )信息需要多少次盘 I/O 操作 ?

连续分配:为读大文件前面和后面信息都需先计算信息在文件中相对块数,前面信息相对逻辑块号为 5.5K/ 4K=1 ,后面信息相对逻辑块号为( 16M+ 5.5K) /4K=4097 。再计算物理块号=文件首块号+相对逻辑块号,最后化一次盘 I/O 操作读出该块信息。

Page 53: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.53Operating System Concepts

例 1-解答 链接分配:为读大文件前面 5.5.KB 的信息,只需先读一次

文件头块得到信息所在块的块号,再读一次第 1 号逻辑块得到所需信息。而读大文件后面 16MB+ 5.5KB 的信息,要先把该信息所在块前面块顺序读出,共化费 4097 次盘 I/O 操作,才能得到信息所在块的块号,最后化一次 I/O 操作读出该块信息。所以总共需要 4098 次盘 I/ O 才能读取( 16MB+5.5KB )字节信息。

链接索引:为读大文件前面 5.5KB 的信息,只需先读一次第一块索引块得到信息所在块的块号,再读一次第 1 号逻辑块得到所需信息,共化费 2 次盘 I/ O 操作。为读大文件后面( 16MB+5.5KB )的信息,需要先化 5 次盘 I/ O 操作依次读出各索引块,才能得到信息所在块的块号,再化一次盘I/O 操作读出该块信息。共化费 6 次盘 I/ O 操作。

Page 54: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.54Operating System Concepts

例 1-解答

二级索引:为读大文件前面和后面信息的操作相同,首先进行一次盘 I/ O 读第一级索引块,然后根据它的相对逻辑块号计算应该读第二级索引的那块,第一级索引块表目号 =相对逻辑块号/ 1K ,对文件前面信息 1 / 1K= 0 ,对文件后面信息 4097/ 1K= 4 ,第二次根据第一级索引块的相应表目内容又化一次盘 I/ O 读第二级索引块,得到信息所在块块号,再化一次盘 I/ O 读出信息所在盘块,这样读取大文件前面或后面信息都只需要 3 次盘 I/ O 操作。

Page 55: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.55Operating System Concepts

例 1-解答 UNIX混合分配:为读大文件前面 5.5KB 信息,先根据它的相对逻辑块

号,在内存文件控制块 FCB的 i_addr[13] 第二个表目中读取信息所在块块号,而只化费一次盘 I/ O 操作即可读出该块信息。为读大文件后在( 16MB+ 5.5KB )信息,先根据它的相对逻辑块号判断它是在UNIX 二级索引管理范围,先根据 i_addr[11] 内容化一次盘 I/ O 操作读出第一级索引块,再计算信息所在块的索引块号在第一级索引块的表目号为( 4097-9-1024 )/ 1024= 3 ,根据第一级索引块第 3 个表目内容再化费一次盘 I/ O 操作,读出第二级索引块,就可以得到信息所在块块号,最后化一次盘 I/ O 读出信息所在盘块,这样总共需要 3次盘 I/ O 操作才能读出文件后面的信息。

Page 56: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.56Operating System Concepts

例 1-解答为清楚起见将结果列表。从上分析可知 UNNX混合分配方案既可管理很大的文件,又能化费较小的代价管理并快速存取小文件。

Page 57: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.57Operating System Concepts

12.4.4 Performance

Two aspects Storage efficiency Data-block access time

Contiguous allocation Linked allocation

Access to the ith block may require i disk reads

Index allocation, depend on Index structure Size of the file Position of the block desired

Page 58: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.58Operating System Concepts

12.5 Free-Space Management (自由空间管理)

Bit map or Bit vector (n blocks) (位图)

…0 1 2 n-1

bit[i] = 1 block[i] free

0 block[i] occupied

Block number calculation (块数计算)

(to find the first free block)

=(number of bits per word) *(number of 0-value

words) +offset of first 1 bit

Page 59: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.59Operating System Concepts

Free-Space Management (Cont.)

Bit map requires extra space. Example (位图需要额外空间) :

block size = 212 bytes

disk size = 230 bytes (1 gigabyte)

n = 230/212 = 218 bits (or 32K bytes) Using bit map, it’s easy to get contiguous files (比较容易得到连续的文件)

Clustering the blocks in groups of n blocks to reduce the bitmap block size

Page 60: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.60Operating System Concepts

Free-Space Management (Cont.) Linked list ( free list)(链接表—自由空间表)

Cannot get contiguous space easily (得到连续空间困难) No waste of space (没有浪费空间)

Grouping (分组) Store the address of n free blocks in the first free block First n-1 of the blocks are actually free The last block contains the address of another n free blocks …

Counting (计数) Keep the address of the first block and the number n of

free contiguous blocks that follows the first block

Page 61: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.61Operating System Concepts

Linked Free Space List on Disk

Page 62: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.62Operating System Concepts

Free-Space Management (Cont.)

Need to protect:(需要保护) Pointer to free list (指向自由空间表的指针) Bit map (位图)

Must be kept on disk (必须保存在磁盘上) Copy in memory and disk may differ.

(内存中的拷贝可能和磁盘上的不同) Cannot allow for block[i] to have a situation where bit[i] = 1 in

memory and bit[i] = 0 on disk.

(磁盘上的值要和内存中的值相一致) Solution:(解决方案)

Set bit[i] = 1 in disk. (磁盘上设置为 1 ) Allocate block[i] (分配空间) Set bit[i] = 1 in memory (内存中设置为 1 )

Page 63: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.63Operating System Concepts

UNIX 成组链接法

UNIX S V 操作系统采用成组链接法管理磁盘空闲块,该方法是空闲表法和空闲块链接法的结合,具备分配回收方便,不需专用块来存放分配表等优点。

成组链接法将磁盘空闲块分成若干组,如将每 100 个盘块作为一组,该组空闲块总数和各空闲块块号存入下一组的第一个空闲块中。最后不满 100 块的那组空闲块总数和各空闲块块号记入磁盘区专用管理块的空闲块管理的数据结构:s_nfree和 s_free[100] 中,如下图所示。

Page 64: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.64Operating System Concepts

成组链接法 -2

盘专用管理块

s_nfree01  

s_free[100]                                                         449 块

38                             50 块 150 块 350 块99

                            49 块 149 块 12 块 51 块 251 块 351 块

39 50 49 .12

100150149

. . . 51

100 . . . . . . . .

1000499

. . .351

Page 65: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.65Operating System Concepts

成组链接法 -3

磁盘专用管理块中 s_free[100] 作为空闲块栈,而s_nfree 作为空闲块栈指针,用于空闲块的分配和回收。

在要分配一个空闲块时,只要先按空闲块栈指针 s_nfree到空闲块栈中取表目为 s_free[s_nfree] 的盘块号分配,再将 s_nfree 存数减 1 ,即弹出栈。

当要回收一块空闲块时,则先将栈指针 s_nfree 存数加 1,再将回收的空闲块号存入空闲块栈 s_free[s_nfree] 表目中,即压入栈。

当空闲块栈分配完或回收满时分配和回收操作复杂点。 由于文件系统要安装时,磁盘专用块拷入内存系统缓冲区,

所以成组链接法分配和回收操作大部分情况只与内存进行读写,所以速度较快。

Page 66: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.66Operating System Concepts

12.6 Efficiency and Performance (效率和性能)

Efficiency dependent on:(效率取决于) disk allocation and directory algorithms

磁盘分配和目录算法 types of data kept in file’s directory entry

保存在文件目录项中的数据结构 Performance (性能)

disk cache – separate section of main memory for frequently sued blocks

磁盘高速缓存—主存中用于存放经常访问块的单独区域

Page 67: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.67Operating System Concepts

12.6 Efficiency and Performance (效率和性能)

Performance (性能) free-behind and read-ahead – techniques to

optimize sequential access

free-behind – remove a page from the buffer as soon as the next page is required.

read-ahead – a requested page and several subsequent pages are read and cached.

free-behind&read-ahead :优化顺序存取的技术 improve PC performance by dedicating section

of memory as virtual disk , or RAM disk.

改进 PC 性能可以靠用内存来模拟磁盘,即 RAM

Page 68: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.68Operating System Concepts

Page Cache 页缓冲 A page cache caches pages rather than disk

blocks using virtual memory techniques.

一个页缓冲的缓冲页优于使用虚存技术的磁盘块 Memory-mapped I/O uses a page cache.

内存映像 I/O 使用页缓冲 Routine I/O through the file system uses the

buffer (disk) cache.

例程 I/O 通过文件系统使用磁盘缓冲区 This leads to the following figure.

缺点 : Wasteful of memory Wasteful of significant CPU and I/O cycle Inconsistent between the two caches

Page 69: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.69Operating System Concepts

I/O Without a Unified Buffer Cache

Page 70: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.70Operating System Concepts

Unified Buffer Cache

A unified buffer cache uses the same page cache to cache both memory-mapped pages and ordinary file system I/O.

一个统一高速缓冲器使用相同页缓冲去缓冲内存映射页和不同文件系统 I/O

Avoid double caches

Page 71: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.71Operating System Concepts

I/O Using a Unified Buffer Cache

Page 72: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.72Operating System Concepts

Synchronous writes Writes are not buffered

Asynchronous writes Are done the majority of the time Date is stored in the cache and returns control to

the caller. Free-behind

Remove a page from the buffer as soon as the next page is requested. The previous pages are not likely to be used again and waste buffer space.

Read-ahead A requested page and several subsequent pages

are read and cached. These pages are likely to be request after the current page is processed.

Virtual disk or RAM disk

Page 73: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.73Operating System Concepts

Various Disk-Caching Locations

•Virtual disk or RAM disk

Page 74: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.74Operating System Concepts

12.7 Recovery 恢复

Consistency checker – compares data in directory structure with data blocks on disk , and tries to fix inconsistencies.

一致性检查:比较目录结构中的数据和磁盘块中的数据,尝试着去修正不一致 UNIX caches directory entries for reads, but any data write is

done synchronously. Use system programs to back up data from disk to another

storage device ( floppy disk , magnetic tape) .

使用系统程序来备份数据到其他的存储设备,软盘,磁带 Full backup 完全备份 Incremental backup 增量备份

Recover lost file or disk by restoring data from backup.

通过从备份重建来恢复丢失的文件或磁盘

Page 75: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.75Operating System Concepts

12.8 Log Structured File Systems日志结构文件系统

Log structured (or journaling) file systems record each update to the file system as a transaction.日志结构文件系统记录每一次更新文件系统事务

All transactions are written to a log. A transaction is considered committed once it is written to the log. However, the file system may not yet be updated.所有的事务都被记录在 log 中 . 。一个事务被认为是记录在 log 中的一个提交(委托)。然而文件系统可能仍然没有更新。

The transactions in the log are asynchronously written to the file system. When the file system is modified, the transaction is removed from the log.日志中的事务异步写入文件系统,当文件系统被修改,这个事务从日志中删除。

If the file system crashes, all remaining transactions in the log must still be performed.如果文件系统崩溃,在日志所有保存事务仍然可执行(恢复)

Page 76: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.76Operating System Concepts

12.9 Network File System (NFS)

An implementation and a specification of a software system for accessing remote files across LANs (or WANs).为通过 LAN或WAN访问远程文件,制定一个软件系统的实现和规范

The implementation is part of the Solaris and SunOS operating systems running on Sun workstations using an unreliable datagram protocol (UDP/IP protocol and Ethernet. 运行在 SUN工作站中的 SUN OS和 Solaris 实现部分是使用不可靠的数据报协议( UDP/IP)

Page 77: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.77Operating System Concepts

NFS (Cont.)

Interconnected workstations viewed as a set of independent machines with independent file systems, which allows sharing among these file systems in a transparent manner.互联工作站被认为是独立文件系统用的独立机器, 它允许在一种透明的方式在这些文件系统之中共享A remote directory is mounted over a local file

system directory. The mounted directory looks like an integral subtree of the local file system, replacing the subtree descending from the local directory.一个远程的目录在一个本地的文件系统目录上被安装,安装的目录看起来像本地的文件系统的一棵不可分的子树 , 替换子树从本地目录递减。

Page 78: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.78Operating System Concepts

NFS (Cont.) Specification of the remote directory for the mount

operation is nontransparent; the host name of the remote directory has to be provided. Files in the remote directory can then be accessed in a transparent manner.为安装操作的远程目录的说明是不透明的 ; 远程目录的主机名字必须被提供。在远程目录的文件能以一种透明的方式被存取。

Subject to access-rights accreditation, potentially any file system (or directory within a file system), can be mounted remotely on top of any local directory. 经存取正确认证,潜在地任何文件系统 ( 或在一个文件系统以内的目录 ), 能在任何本地目录上被远程安装

Page 79: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.79Operating System Concepts

NFS (Cont.)

NFS is designed to operate in a heterogeneous environment of different machines, operating systems, and network architectures; the NFS specifications independent of these media. NFS被设计在不同的机器、操作系统、网络结构的不同环境中运行 ;NFS 说明是独立于这些介质的。

This independence is achieved through the use of RPC primitives built on top of an External Data Representation (XDR) protocol used between two implementation-independent interfaces.通过使用建立在 2 个实现独立接口之间使用了 XDR 协议的 RPC原语来完成这独立,

The NFS specification distinguishes between the services provided by a mount mechanism and the actual remote-file-access services.在服务器之间 NFS 说明的区分是由安装机制和实际的远程文件访问服务提供的

Page 80: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.80Operating System Concepts

Three Independent File Systems机器名

Page 81: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.81Operating System Concepts

Mounting in NFS

Mounts Cascading mounts

(shared)

(From S1)(actually

dir2)

(shared)

Page 82: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.82Operating System Concepts

12.9.2 NFS Mount Protocol( NFS 安装协议)

Establishes initial logical connection between server and client.在服务器和客户端之间建立初始逻辑连接

Mount operation includes name of remote directory to be mounted and name of server machine storing it. 安装操作包括远程目录名字被安装并且存储它的服务器名字。 Mount request is mapped to corresponding RPC and

forwarded to mount server running on server machine.安装请求被映射到相应的 RPC 并且提交给在服务器上运行的安装服务程序。

Export list – specifies local file systems that server exports for mounting, along with names of machines that are permitted to mount them. 输出列表 - 指定那个服务器为安装输出的本地文件系统 , 与机器的名字一起那被允许安装他们。

Page 83: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.83Operating System Concepts

NFS Mount Protocol Cont.( NFS 安装协议 续 )

Following a mount request that conforms to its export list, the server returns a file handle—a key for further accesses.接下是符合输出列表的一个安装请求,服务器返回一个文件柄 -为进一步的存取的一个键值

File handle – a file-system identifier, and an inode number to identify the mounted directory within the exported file system.文件句柄 -— 一个文件系统标识符,在输出文件系统内确定安装目录的一个结点号

The mount operation changes only the user’s view and does not affect the server side. 安装操作仅仅改变用户视图但不影响服务器一侧。

服务器上保持了一张安装表格,包括客户端机器名及相应的安装目录 目的:用于系统管理

Page 84: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.84Operating System Concepts

12.9.3 NFS Protocol ( NFS协议) Provides a set of remote procedure calls for remote file

operations. The procedures support the following operations:

为远程文件操作提供一套远过程调用。 这些过程支持下列操作:

searching for a file within a directory (在目录里查找文件) reading a set of directory entries (读一组目录的入口) manipulating links and directories (操纵链接和目录) accessing file attributes (访问文件属性) reading and writing files (读写文件)

Page 85: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.85Operating System Concepts

NFS Protocol( NFS协议)

NFS servers are stateless; each request has to provide a full set of arguments.

NFS 服务器是无状态的 ;每个请求必须提供一组全部参数

Modified data must be committed to the server’s disk before results are returned to the client (lose advantages of caching).

在结果被返回到客户端以前,修改的数据必须被提交给服务器的磁盘 ( 失去缓冲的优点 )

The NFS protocol does not provide concurrency-control mechanisms。NFS 协议不提供并发控制机制。但可提供 locking机制。

Page 86: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.86Operating System Concepts

Three Major Layers of NFS ArchitectureNFS 结构的 3个主要层

UNIX file-system interface (based on the open, read, write, and close calls, and file descriptors).UNIX 文件系统接口 ( 基于打开、读、写、关闭调用 ,文件描述符 ) 。

通过 VFS, NFS被融合到操作系统的文件管理中去 Virtual File System (VFS) layer – distinguishes local files from remote

ones, and local files are further distinguished according to their file-system types. VFS 层 - 区分本地文件与远程文件,并且本地文件根据他们的文件系统类型进一步被区分。 The VFS activates file-system-specific operations to handle local

requests according to their file-system types. 根据文件系统类型, VFS 激活文件系统指定的操作来处理本地的请求

Calls the NFS protocol procedures for remote requests. 为远程请求调用 NFS 协议过程。

NFS service layer – bottom layer of the architecture; implements the NFS protocol.NFS 服务层 --体系结构的底层 ;实现 NFS 协议。

Page 87: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.87Operating System Concepts

Schematic View of NFS Architecture NFS 结构的示意图

Page 88: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.88Operating System Concepts

NFS Path-Name TranslationNFS 路径名翻译

Performed by breaking the path into component names and performing a separate NFS lookup call for every pair of component name and directory vnode.通过把路径放入部件名字实现和为每对部件名和目录节点分开的 NFS 查找调用来完成。

To make lookup faster, a directory name lookup cache on the client’s side holds the vnodes for remote directory names. 为更快查找,目录名字查找缓存保存在客户端一侧远程目录名节点。

Page 89: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.89Operating System Concepts

NFS Remote OperationsNFS远程操作

Nearly one-to-one correspondence between regular UNIX system calls and the NFS protocol RPCs (except opening and closing files).在常规的 UNIX 系统调用与 NFS协议的 RPCs 之间的几乎是一对一的通讯。 ( 除了开并和关闭文件 )

NFS adheres to the remote-service paradigm, but employs buffering and caching techniques for the sake of performance. NFS 遵守远程服务范例,但是出于性能考虑使用缓冲和高速缓冲技术。

File-blocks cache – when a file is opened, the kernel checks with the remote server whether to fetch or revalidate the cached attributes. Cached file blocks are used only if the corresponding cached attributes are up to date.文件块缓存 - 当一个文件被打开时,内核与远程服务器一起检查是否取或重新设置缓冲的属性。只要相应缓冲的属性是最新的,缓冲文件块被使用。

Page 90: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.90Operating System Concepts

NFS Remote Operations Cont. NFS远程操作 (续)

File-attribute cache – the attribute cache is updated whenever new attributes arrive from the server.

文件属性缓存 -无论何时新属性从服务器到达,属性缓存被更新。

Clients do not free delayed-write blocks until the server confirms that the data have been written to disk.

顾客端不释放延期写的块,直到服务器确认数据已被写入磁盘。

Page 91: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.91Operating System Concepts

12.10 文件的保护和安全

一、文件的存取控制

文件系统对文件的保护常采用存取控制方式进行,所谓存取控制就是不同的用户对文件的访问规定不同的权限,以防止文件被未经文件主同意的用户访问。

1. 存取控制矩阵 理论上存取控制方法可用存取控制矩阵,它是一个二维矩阵

,一维列出计算机的全部用户,另一维列出系统中的全部文件,矩阵中每个元素 Aij 是表示第 i个用户对第 j个文件的存取权限。通常存取权限有可读、可写、可执行以及它们的组合如下表所示。

Page 92: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.92Operating System Concepts

文件的存取控制 -2

   文件            用户 

 ALPHA 

 BETA 

 REPORT 

   SQRT 

   

   张三    RWX    - - -                        R-X    - - -      

   李四    R-X    - - -     RWX    R-X  …   

   王五    - - -     RWX    R-X    R-X     

   赵六    - - -     - - -       - - -     RWX     

      .         .            

                                                     存取控制矩阵    存取控制矩阵在概念上是简单清楚的,但实现上却有困难。当一个系统用户数和文件数很大时,二维矩阵要占很大的存储空间,验证过程也费时。 

Page 93: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.93Operating System Concepts

文件的存取控制 -3

2. 存取控制表 存取控制矩阵由于太大而往往无法实现。一个改进的办法是按用户对文

件的访问权力的差别对用户进行分类,由于某一文件往往只与少数几个用户有关,所以这种分类方法可使存取控制表大为简化。 UNIX 系统就是使用这种存取控制表方法。它把用户分成三类:文件主、同组用户和其它用户,每类用户的存取权限为可读、可写、可执行以及它们的组合。在用 ls 长列表显示时每组存取权限用三个字母 RWX 表示,如读、写和执行中那一样存取不允许则用“ -” 字符表示,用 ls -l 长列表显示 ls文件如下:

-r-xr-xr-t  1  bin   bin   43296   May 13   1997   /opt/K/SCO/Unix/5.0.4Eb/bin/ls

显示前 2-10 共 9 个字符表示文件的存取权限,每 3 个字符为一组,分别表示文件主、同组用户和其它用户的存取权限。

Page 94: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.94Operating System Concepts

文件的存取控制 -4

由于存取控制表对每个文件将用户分类,所以该存取控制表可存放在每个文件的文件控制块 (即目录表目)中,对 UNIX 它只需 9位二进制来表示三类用户对文件的存取权限,它存在文件索引节点的 di_mode 中。

3.用户权限表 改进存取控制矩阵的另一种方法是以用户或用户组为单位将用户可存取的

文件各集中起来存入一表,这称为用户权限表,表中每个表目表示该用户对应文件的存取权限,如下表所示。这相当于存取控制矩阵一行的简化。

Page 95: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.95Operating System Concepts

二、分级安全管理

随着计算机应用范围扩大,在所有稍具规模的系统中,都从多个级别上来保证系统的安全性。

1. 系统级安全管理 系统级安全管理的主要任务是不允许未经核准的用户进入系统,从而也

防止了他人非法使用系统中各类资源(包括文件)。系统级管理的主要措施有:

注册 注册的主要目的是使系统管理员能够掌握要使用的各用户的情况,并保证用户在系统中的唯一性。例如 linux 操作系统中 Passwd 文件为系统的每一个帐号保存一行记录,这条记录给出了每个账号的一些属性,如用户真实名字、口令等, Passwd是 ASCII 文件,普通用户可读,只有root  可写,为使口令保密,使用 shadow 口令可使 Passwd 文件中存放口令的地方放上一个“×” 。

Page 96: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.96Operating System Concepts

系统级安全管理 -2

而加密口令和口令有效期信息存放在 Shodow 文件中,只有 root 才能读取。任何一个新用户在使用系统前,必须先向系统管理员申请,由系统管理员 root 使用 adduser 命令创建用户帐号。当用户不再使用系统时,由root帐号使用 userdel 命令删除一个帐号和帐号的主目录。

登录 用户经注册后就成为该系统用户,但在上机时还必须进行登录。登录的主

要目的是通过核实该用户的注册名及口令来检查该用户使用系统的合法性。WindowsNT 需用户同时按下 Ctrl+Alt+Del键来启动登录界面,提示输入用户名和口令。在用户输入后系统调用身份验证包来接收登录信息,并与安全帐号管理库中存放的用户名和口令进行对比,如果找到匹配,则登录成功,于是允许用户进入系统。为了防止非法用户窃取口令,在用户键入口令时,系统将不在屏幕上给予回显,凡未通过用户名及口令检查的用户,将不能进入系统。

Page 97: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.97Operating System Concepts

分级安全管理 -3

为了进一步保证系统及安全性,防止有恶意者通过多次尝试猜口令方式而打入系统, SCO UNIX 可设置访问注册限制次数,当不成功注册次数超过这个限度后,帐号和终端就被封锁,这称为凶兆监视( threat monitoring)。系统还可设置参数控制口令有效时间,当一口令到了退役时间,口令死亡,该用户帐号也封闭。 WindowsNT 采用 Ctrl+Alt+Del键来启动登录界面也是为了防止非法程序模拟登录界面扮作操作系统来窃取用户名和口令。

2.用户级安全管理 用户级安全管理是通过对不同的用户对不同文件设置不同

的存取权限来实现。

Page 98: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.98Operating System Concepts

分级安全管理 -4

3.目录级安全管理 目录级安全管理,是为了保护系统中各种目录而设计的,它与用户权限无关。为保证目录的安全规定只有系统核心才具有写目录的权利。

用户对目录的读、写和执行与一般文件的读、写和执行的含义有所不同,对于目录的读权限意味着允许打开并读该目录的信息,例如 UNIX 系统使用 ls 命令可列出该目录的子目录和文件名。对于目录的写权限意味着可以在此目录中创建或删除文件,禁止对于某个目录的写权限并不意味着在该目录中的文件不能修改,只有在一个文件上的写权限才真正地控制修改文件的能力。对于一个目录的执行权限意味着系统在分析一个文件时可检索此目录。禁止一个目录的执行权限可真正地防止用户使用该目录中的文件,用户不能使用 cd 命令来进入此目录。

Page 99: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.99Operating System Concepts

分级安全管理 -5

4.文件级安全管理 文件级安全管理,是通过系统管理员或文件主对文件属性的设置来控制

用户对文件的访问。通常可设置以下几种属性: 只执行―只允许用户执行该文件,主要针对 .exe 和 .com 文件。 隐含―指示该文件为隐含属性文件。 索引―指示该文件是索引文件。 修改―指示该文件自上次备份后是否还被修改。 只读―只允许用户对读该文件。 读/写―允许用户对文件进行读和写。 共享―指示该文件是可读共享的文件。 系统―指示该系统是系统文件。

Page 100: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.100Operating System Concepts

文件级安全管理 -6

用户对文件的访问,将由用户访问权、目录访问权限及文件属性三者的权限所确定。或者说是有效权限和文件属性的交集。例如对于只读文件,尽管用户的有效权限是读/写,但都不能对只读文件进行修改、更名和删除。对于一个非共享文件,将禁止在同一时间内由多个用户对它们进行访问。通过上述四级文件保护措施,可有效地对文件的保护。

Page 101: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.101Operating System Concepts

三、 WindowsNT 安全性 WindowsNT 提供了一组全面的、可配置的安全性服务,这些服务达到了美国政府用于受托操作系统的国防部 C2 级要求。

1.Windows NT 安全性服务 Windows NT 的安全性服务及其基本特征如下: 登录服务:要求在允许用户访问系统之前,输入唯一的登录标识符和密码来标识自已。

谨慎访问控制:允许资源的所有者决定哪些用户可以访问资源和它们可以处理这些资源。属主可以授权给某个用户或用户组,允许他们进行访问。

安全审核:提供检测和记录与安全有关的任何创建、访问或删除系统资源的事件或尝试的能力。登录标识符记录所有用户的身份,这们便于跟踪任何非法操作的用户。

内存保护:防止非法进程访问其它进程专用虚拟内存。它保证当物理内存页面分配给某个用户进程前被重新初始化,保证这一页绝对不含有其它进程的脏数据。

Page 102: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.102Operating System Concepts

WindowsNT 安全性 -2

2.存取控制NT利用面向对象的概念提供了一个强大而灵活的存取控制能力, NT 提供一个统一的存取控制机制应用到进程、线程、文件、信号量窗口和其它对象。存取控制是由二个实体来管理,与每个进程相关的存取令牌和使进程存取可能的每个对象相关的安全描述体。

存取令牌( access token) 存取令牌的一般结构包括以下参数:

在网络所有机器中唯一标识一个用户的安全标识符 SID ,它相应于一个用户的登录名。

用户归属组的安全标识符组 SID。 一个安全性敏感系统服务于这个用户可能用和禁止用的特权列表。 缺省的拥存者。缺省的存取控制表 ACL。

Page 103: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.103Operating System Concepts

WindowsNT 安全性 -3 安全描述体( security descriptor )参数:

定义一个安全描述体的类型和内容标志。 对象的拥有者,它能在对象上完成任何操作。 系统存取控制列表( SACL),它确定在对象上那类操作应该产生审查信息

。 描述体存取控制表( DACL),它决定那个用户和组能用那种操作来存取这

个对象,它是由一个存取控制实体( ACE )的列表。3. 存取控制方式 当一个用户在 NT登录接受后, NT 为用户产生了一个进程和一个进程相

连的存取令牌,存取令牌保持所有所需安全信息。而与每个对象相连的是安全描述体,安全描述体有一个存取控制表,它确定不同的用户和用户组对这个对象的存取权。当一个用户相连的进程试图存取一个对象时, NT的安全子系统把进程的安全标识符 SID与对象的存取控制表进行查对,以决定存取是否允许。

Page 104: Chapter 12: File System Implementation 文件系统实现

Silberschatz, Galvin and Gagne 200212.104Operating System Concepts

Exercises

1, 6