600 likes | 875 Views
第六章 檔案系統 File Systems. 鄧姚文 http://www.ywdeng.idv.tw. 大綱. 6.1 Files 檔案 6.2 Directories 目錄 6.3 File system implementation 檔案系統的實作 6.4 Example file systems 檔案系統實例. Long-term Information Storage. 檔案系統和辦公室裡的公文系統非常相似 公文櫃 --- 硬碟裡的一個檔案系統 公文櫃有許多個抽屜 --- 目錄 / 檔案匣 公文 --- 檔案 長期儲存資料的機制
E N D
第六章 檔案系統File Systems 鄧姚文 http://www.ywdeng.idv.tw Tanenbaum作業系統2Ed
大綱 • 6.1 Files 檔案 • 6.2 Directories 目錄 • 6.3 File system implementation 檔案系統的實作 • 6.4 Example file systems 檔案系統實例 Tanenbaum作業系統2Ed
Long-term Information Storage • 檔案系統和辦公室裡的公文系統非常相似 • 公文櫃 --- 硬碟裡的一個檔案系統 • 公文櫃有許多個抽屜 --- 目錄/檔案匣 • 公文 --- 檔案 • 長期儲存資料的機制 • Must be possible to store a vary large amount of information – 容量要大 • The information must survive the termination of the process using it – 獨立存在 • Multiple processes must be able to access the information concurrently – 可以共用 Tanenbaum作業系統2Ed
File Naming • Typical file extensions. Tanenbaum作業系統2Ed
File Structure • Three kinds of files • byte sequence • record sequence • tree Tanenbaum作業系統2Ed
File Types • Regular files 一般檔案 • 使用者的資料檔,各自定義其格式 • 可執行檔(Executables)須符合系統定義的格式 • ELF: executable and linkable format • Directories 目錄 • Character special files 對應到字元裝置 • Block special files 對應到區塊裝置 Tanenbaum作業系統2Ed
File Types (a) An executable file (b) An archive Tanenbaum作業系統2Ed
File Access • Sequential access 循序 • read all bytes/records from the beginning • cannot jump around, could rewind or back up • convenient when medium was magetic tape • Random access 隨機 • bytes/records read in any order • essential for database systems • read can be … • move file marker (seek), then read or … • read and then move file marker Tanenbaum作業系統2Ed
File Attributes • Possible file attributes Tanenbaum作業系統2Ed
File Operations • Create 創建 • Delete 刪除 • Open 開啟 • Close 關閉 • Read 讀 • Write 寫 • Append 附加 • Seek 搜尋 • Get attributes 取得屬性 • Set Attributes 設定屬性 • Rename 改名 Tanenbaum作業系統2Ed
An Example Program Using File System Calls (1/2) Tanenbaum作業系統2Ed
An Example Program Using File System Calls (2/2) Tanenbaum作業系統2Ed
Memory-Mapped Files記憶體映對檔案 • Segmented process before mapping files into its address space • Process after mapping • existing file abc into one segment • creating new segment for xyz 檔案長度? 超級大檔案? Tanenbaum作業系統2Ed
Directories 目錄、檔案夾Single-Level Directory Systems • A single level directory system • contains 4 files • owned by 3 different people, A, B, and C 檔名不能重複! Tanenbaum作業系統2Ed
Two-level Directory Systems • Letters indicate owners of the directories and files Tanenbaum作業系統2Ed
Hierarchical Directory Systems • A hierarchical directory system Tanenbaum作業系統2Ed
Path Names • A UNIX directory tree 絕對路徑 /usr/local/bin/wget 相對路徑 ../../bib/main.bib ./bin/mirror 目前目錄 . 上一層目錄 .. Tanenbaum作業系統2Ed
Directory Operations • Create • Delete • Opendir • Closedir • Readdir • Rename • Link • Unlink Tanenbaum作業系統2Ed
File System Implementation • A possible file system layout Tanenbaum作業系統2Ed
Implementing Files (1) • Contiguous allocation of disk space for 7 files • State of the disk after files D and E have been removed Tanenbaum作業系統2Ed
Implementing Files (2) • Storing a file as a linked list of disk blocks Tanenbaum作業系統2Ed
Implementing Files (3) • Linked list allocation using a file allocation table in RAM Tanenbaum作業系統2Ed
Implementing Files (4) • An example i-node Tanenbaum作業系統2Ed
Implementing Directories (1) • A simple directory • fixed size entries • disk addresses and attributes in directory entry • Directory in which each entry just refers to an i-node Tanenbaum作業系統2Ed
Implementing Directories (2) • Two ways of handling long file names in directory • In-line • In a heap Tanenbaum作業系統2Ed
Shared Files (1) • File system containing a shared file Tanenbaum作業系統2Ed
Shared Files (2) • (a) Situation prior to linking • (b) After the link is created • (c)After the original owner removes the file Tanenbaum作業系統2Ed
Hard Link and Soft Link • UNIX • Hard Link • ln target link_name • Soft Link (Symbolic Link) • ln -s target link_name • Windows (XP and Later) • Hard Link • fsutil hardlink create NewFilename ExistingFilename • Soft Link Tanenbaum作業系統2Ed
Disk Space Management (1) • Dark line (left hand scale) gives data rate of a disk • Dotted line (right hand scale) gives disk space efficiency • All files 2KB Compromise: 4KB Block size Block size(bytes) Tanenbaum作業系統2Ed
Disk Space Management (2)Keep Track of Free Blocks • Storing the free list on a linked list • A bit map Tanenbaum作業系統2Ed
Disk Space Management (3) • Almost-full block of pointers to free disk blocks in RAM • three blocks of pointers on disk • Result of freeing a 3-block file • Alternative strategy for handling 3 free blocks • shaded entries are pointers to free disk blocks Tanenbaum作業系統2Ed
Disk Space Management (4)Disk Quota 配額 • Quotas for keeping track of each user’s disk use Tanenbaum作業系統2Ed
Backups 備份 • Recover from disaster • Recover from stupidity • Don’t dump device special files • Incremental dump • Compress or not to compress… • Difficult to backup an active file system • Security problem • Physical dump / Logical dump Tanenbaum作業系統2Ed
File System Reliability (1) • A file system to be dumped • squares are directories, circles are files • shaded items, modified since last dump • each directory & file labeled by i-node number File that has not changed Tanenbaum作業系統2Ed
File System Reliability (2) • The logical dumping algorithm • Mark all directories and modifies files • Unmark any directories that have no modified files or directories in them or under them • Dump all the marked directories • Dump all the marked files Tanenbaum作業系統2Ed
File System Reliability (2) Bit maps used by the logical dumping algorithm Tanenbaum作業系統2Ed
File System Reliability (2) • Restore from backups • Create an empty file system • Restore the most recent full dump • Repeat with the first incremental dump after the full dump, then the next one, and so on. • Reconstruct the free block list Tanenbaum作業系統2Ed
File System Reliability (3) • File system states (a) consistent (b) missing block (c) duplicate block in free list (d) duplicate data block 1 1 1 Tanenbaum作業系統2Ed
File System Performance (1)block cache/buffer cache The block cache data structures h(device, disk address) Tanenbaum作業系統2Ed
File System Performance (1)block cache/buffer cache • Modified LRU • Is the block likely to be needed again soon? • Is the block essential to the consistency of the file system? • Unix: sync every 30 seconds • Write-through cache • All modified blocks are written back to the disk immediately • Block Read Ahead • Get blocks into the cache before they are needed to increase the hit rate • Only works for files that are being read sequentially Tanenbaum作業系統2Ed
File System Performance (2)reduce disk arm motion • I-nodes placed at the start of the disk • Disk divided into cylinder groups • each with its own blocks and i-nodes Tanenbaum作業系統2Ed
Log-Structured File Systems • With CPUs faster, memory larger • disk caches can also be larger • increasing number of read requests can come from cache • thus, most disk accesses will be writes • LFS Strategy structures entire disk as a log • have all writes initially buffered in memory • periodically write these to the end of the disk log • when file opened, locate i-node, then find blocks Tanenbaum作業系統2Ed
Example File SystemsCD-ROM File Systems • ISO 9660 • Level 1: 8+3 字檔名, 8 字目錄名, contiguous allocation • Level 2: 31 字檔名 • Level 3: relax the contiguous limitation • Rock Ridge Extensions • UNIX • Joliet Extension • Windows Tanenbaum作業系統2Ed
Example File SystemsCD-ROM File Systems: ISO 9660 • Logical block (logical sector): 2352 bytes • Preamble, error correction, overhead: 304 bytes • Payload: 2048 bytes • 1 CD Set: up to 216-1 CDs • The first 16 blocks • Not defined by ISO 9660 • Provide bootstrap program • Primary volume descriptor: general information Tanenbaum作業系統2Ed
Example File SystemsCD-ROM File Systems: ISO 9660 Binary fields encoded twice: little-endian (Pentium) and big-endian (SPARC) 16-bit numbers use 4 bytes, 32-bit numbers use 8 bytes Years begin to count at 1900, suffering from a Y2156 problem. Tanenbaum作業系統2Ed
Example File SystemsCD-ROM File Systems: Rock Ridge • Rock Ridge Extension • By the UNIX community • Use the SYSTEM USE field • PX: POSIX attributes, rwxrwxrwx, SETUID, SETGID • PN: Major and minor device numbers • SL: Symbolic link, link to another file system • NM: Alternative name, not subject to charset and length limit • CL: Child location • PL: Parent location • RE: Relocation • TF: Time stamps (file created, modified, accessed) Tanenbaum作業系統2Ed
Example File SystemsCD-ROM File Systems: Joliet • Joliet Extension • From Microsoft • Major extension • Long file name (64 characters) • Unicode character set (1 character = 2 bytes) • Directory nesting deeper than eight levels • Directory names with extensions Tanenbaum作業系統2Ed
The CP/M File System • CP/M: Control Program for Microcomputers (1977) • Gary Kildall, Digital Research, Inc • Intel 8080 CPU, 4 KB RAM, 8-inch floppy of 180 KB • Zilog Z80 CPU, 64 KB RAM, 720 KB Floppy • Historically important, direct ancestor of MS-DOS • Simplicity • A reference OS for embedded systems • Splitting BIOS from CP/M • Portability • CP/M interact with the hardware only by making BIOS calls • Only one directory: fixed-size entries (32-byte each) Tanenbaum作業系統2Ed
The CP/M File System (1) Memory layout of CP/M 17 I/O calls 3584 bytes 2KB 256 bytes Tanenbaum作業系統2Ed
The CP/M File System (2) • The CP/M directory entry format Block count: in units of 128 bytes (128-byte physical sectors) Disk block size: 1 KB Max. file size 16 KB Files larger than 16 KB use EXTENT field Tanenbaum作業系統2Ed