Chapter 12: File System Implementation
Total Page:16
File Type:pdf, Size:1020Kb
Chapter 12: File System Implementation This chapter is concerned with the details associated with file systems residing on secondary storage. Specific implementation issues are explored using the disk as the secondary storage device. I File System Structure I File System Implementation I Directory Implementation I Allocation Methods I Free-Space Management I Efficiency and Performance I Recovery I Log-Structured File Systems I NFS Operating System Concepts 12.1 Silberschatz, Galvin and Gagne 2002 File-System Structure Pertinent Disk Details I The physical unit of transfer is a disk sector (e.g., 512 bytes). I Sectors can be written in place. I Any given sector can be accessed directly. I The OS imposes a file system for efficient and convenient access to the disk. I The file system design deals with two distinct matters: 1. How should the file system look to the user. 2. Creating data structures and algorithms to map the logical file system onto the physical secondary- storage device. Operating System Concepts 12.2 Silberschatz, Galvin and Gagne 2002 Blocks and Fragments [4.2BSD] I Logical transfer of data is in blocks. I Blocks are “chunks” or clusters of disk sectors. I block size decision:: {a time space tradeoff} ! Uses two sizes block and fragment ! E.g. 4KB block; 1KB fragment Operating System Concepts 12.3 Silberschatz, Galvin and Gagne 2002 Layered File System A layered design abstraction I I/O control :: device drivers and interrupt service routines that perform the actual block transfers. I Basic file system :: issues generic low-level commands to device drivers. I File organization :: translates logical block addresses to physical block addresses. I Logical file system :: handles metadata that includes file- system structure (e.g., directory structure and file control blocks (FCB’s). Operating System Concepts 12.4 Silberschatz, Galvin and Gagne 2002 In-Memory File System Structures On-disk and in-memory structures are needed to implement a file system: On disk:: 1. Boot control block: needed to boot OS from a disk partition. 2. Partition control block: holds details about partition (e.g., blocks in partition, free- block count, …) superblock [UNIX],Master File Table [NTFS] 3. Directory structure: to organize files. 4. FCB (or inode) : Operating System Concepts 12.5 Silberschatz, Galvin and Gagne 2002 A Typical File Control Block [Linux] inode is the term for FCB Operating System Concepts 12.6 Silberschatz, Galvin and Gagne 2002 In-Memory File System Structures On-disk and in-memory structures needed to implement a file system: In-memory:: 1. Per-process open-file table 2. System-wide open-file table 3. In-memory directory structure 4. In-memory partition table ☛ In some OS’s file system scheme used as interface to other system aspects. [Unix uses system-wide open table for networking - e.g.,socket descriptors.] ☛ Caching used extensively – namely, all the information about an open file is in memory except actual data blocks. Operating System Concepts 12.7 Silberschatz, Galvin and Gagne 2002 In-Memory File System Structures (a) Open operation (b) Read operation Operating System Concepts 12.8 Silberschatz, Galvin and Gagne 2002 File System Implementation File Structure Disk Space Open File User Space Table Table Data block read (4, …) Read (4,…) Inode list (per process) In-core inode Modified Claypool Figure list [Figure A.7 page 837] Operating System Concepts 12.9 Silberschatz, Galvin and Gagne 2002 Virtual File Systems I Virtual File Systems (VFS) provide an object-oriented way of implementing file systems. I VFS allows the same system call interface (the API) to be used for different types of file systems. I The API is to the VFS interface, rather than any specific type of file system. I VFS provides a mechanism for integrating NFS into the OS. Operating System Concepts 12.10 Silberschatz, Galvin and Gagne 2002 Schematic View of Virtual File System Operating System Concepts 12.11 Silberschatz, Galvin and Gagne 2002 Directory Implementation I Linear list of file names with pointer to the data blocks. ✦ simple to program ✦ time-consuming to execute due to linear search I 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 {disadvantage: hash function is dependent on fixed size} Operating System Concepts 12.12 Silberschatz, Galvin and Gagne 2002 File System Implementation I Which blocks with which file? I File descriptor implementations: ✦ Contiguous ✦ Linked List ✦ Linked List with Index ✦ I-nodes File Descriptor Operating System Concepts 12.13 Silberschatz, Galvin and Gagne 2002 Allocation Methods I An allocation method refers to how disk blocks are allocated for files: I Contiguous allocation I Linked allocation I Indexed allocation Operating System Concepts 12.14 Silberschatz, Galvin and Gagne 2002 Contiguous Allocation I Each file occupies a set of contiguous blocks on the disk. I Simple – only starting location (block #) and length (number of blocks) are required. I Random access. I Wasteful of space (dynamic storage-allocation problem). I Files cannot grow. Operating System Concepts 12.15 Silberschatz, Galvin and Gagne 2002 Contiguous Allocation of Disk Space Operating System Concepts 12.16 Silberschatz, Galvin and Gagne 2002 Extent-Based Systems I Many newer file systems (I.e. Veritas File System) use a modified contiguous allocation scheme. I Extent-based file systems allocate disk blocks in extents. I An extent is a contiguous block of disks. Extents are allocated for file allocation. A file consists of one or more extents. Operating System Concepts 12.17 Silberschatz, Galvin and Gagne 2002 Linked Allocation I Each file is a linked list of disk blocks: blocks may be scattered anywhere on the disk. block = pointer Operating System Concepts 12.18 Silberschatz, Galvin and Gagne 2002 Linked Allocation (Cont.) I Simple – need only starting address I Free-space management system – no waste of space I No random access I Mapping Q LA/511 R 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. Operating System Concepts 12.19 Silberschatz, Galvin and Gagne 2002 Linked Allocation Operating System Concepts 12.20 Silberschatz, Galvin and Gagne 2002 File-Allocation Table Operating System Concepts 12.21 Silberschatz, Galvin and Gagne 2002 Indexed Allocation I Brings all pointers together into the index block. I Logical view. index table Operating System Concepts 12.22 Silberschatz, Galvin and Gagne 2002 Example of Indexed Allocation Operating System Concepts 12.23 Silberschatz, Galvin and Gagne 2002 Indexed Allocation (Cont.) I Need index table I Random access I Dynamic access without external fragmentation, but have overhead of index block. I 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. Q LA/512 R Q = displacement into index table R = displacement into block Operating System Concepts 12.24 Silberschatz, Galvin and Gagne 2002 Indexed Allocation – Mapping (Cont.) I Mapping from logical to physical in a file of unbounded length (block size of 512 words). I Linked scheme – Link blocks of index table (no limit on size). Q1 LA / (512 x 511) R1 Q1 = block of index table R1 is used as follows: Q2 R1 / 512 R2 Q2 = displacement into block of index table R2 displacement into block of file: Operating System Concepts 12.25 Silberschatz, Galvin and Gagne 2002 Indexed Allocation – Mapping (Cont.) I Two-level index (maximum file size is 5123) Q1 LA / (512 x 512) R1 Q1 = displacement into outer-index R1 is used as follows: Q2 R1 / 512 R2 Q2 = displacement into block of index table R2 displacement into block of file: Operating System Concepts 12.26 Silberschatz, Galvin and Gagne 2002 Indexed Allocation – Mapping (Cont.) Μ outer-index index table file Operating System Concepts 12.27 Silberschatz, Galvin and Gagne 2002 Combined Scheme: UNIX (4K bytes per block) Operating System Concepts 12.28 Silberschatz, Galvin and Gagne 2002 Hierarchical Directory (Unix) Claypool Figure I Tree I Entry: ✦ name ✦ inode number (try “ls –I” or “ls –iad .”) I example: /usr/bob/mbox inode name Operating System Concepts 12.29 Silberschatz, Galvin and Gagne 2002 Unix Directory Example Claypool Figure Root Directory Block 132 Block 406 1 . 6 . I-node 6 I-node 26 26 . 1 .. 1 .. 6 .. 4 bin 26 bob 12 grants 7 dev 17 jeff 81 books 14 lib 14 sue 132 406 60 mbox 9 etc 51 sam 17 Linux 6 usr 29 mark 8 tmp Aha! Looking up I-node 60 Looking up bob gives has contents usr gives Relevant I-node 26 /usr/bob is of mbox I-node 6 data (/usr) in block 406 is in block 132 Operating System Concepts 12.30 Silberschatz, Galvin and Gagne 2002 Unix Disk Layout Boot Sector Superblock Superblocks •Number of inodes •Number of blocks i-nodes •Block number of I-node bit-map block • first data block Data blocks •Max file size •Magic number – identifies cpu Architecture and executable type Operating System Concepts 12.31 Silberschatz, Galvin and Gagne 2002 Free-Space Management I Bit vector (n blocks) 01 2 n-1 … 0 ⇒ block[i] free bit[i] = 678 1 ⇒ block[i] occupied Block number calculation (number of bits per word) * (number of 0-value words) + offset of first 1 bit Operating System Concepts 12.32 Silberschatz, Galvin and Gagne 2002 Free-Space Management (Cont.) I Bit map requires extra space. Example: block size = 212 bytes disk size = 230 bytes (1 gigabyte) n = 230/212 = 218 bits (or 32K bytes) I Easy to get contiguous files I Linked list (free list) ✦ Cannot get contiguous space easily ✦ No waste of space I Grouping I Counting Operating System Concepts 12.33 Silberschatz, Galvin and Gagne 2002 Free-Space Management (Cont.) I Need to protect: ✦ Pointer to free list ✦ Bit map ✔ Must be kept on disk ✔ Copy in memory and disk may differ.