Download presentation
Presentation is loading. Please wait.
1
Chapter 12: File System Implementation
2
Chapter 12: File System Implementation
File-System Structure File-System Implementation Directory Implementation Allocation Methods Free-Space Management Efficiency and Performance Recovery NFS Example: WAFL File System
3
Objectives To describe the details of implementing local file systems and directory structures To describe the implementation of remote file systems To discuss block allocation and free-block algorithms and trade-offs
4
File-System Structure
File structure Logical storage unit. Collection of related information File system organized into layers File system resides on secondary storage (disks, flash, solid state) Provided user interface to storage, mapping logical to physical Provides efficient and convenient access to disk by allowing data to be stored, located and retrieved easily Disk provides in-place rewrite and random access I/O transfers performed in blocks of sectors (often 512 bytes) File control block – storage structure consisting of information about a file Device driver controls the physical device
5
Layered File System
6
File System Structure - Layered
When talking about “the file system”, you are making a statement about both the rules used for file access, and about the algorithms used to implement those rules. Here’s a breakdown of those algorithmic pieces. Application Programs The code that's making a file request. Logical File System This is the highest level in the OS; it does protection, and security. Uses the directory structure to do name resolution. File-organization Module Here we read the file control block maintained in the directory so we know about files and the logical blocks where information about that file is located. Basic File System Knowing specific blocks to access, we can now make generic requests to the appropriate device driver. IO Control These are device drivers and interrupt handlers. They cause the device to transfer information between that device and CPU memory. Devices The disks / tapes / etc.
7
Many File Systems Many file systems, sometimes many within an operating system Each with its own format (CD-ROM is ISO 9660; Unix has UFS, FFS; Windows has FAT, FAT32, NTFS as well as floppy, CD, DVD Blu-ray, Linux has more than 40 types, with extended file system ext2, ext3 and ext4 leading; plus distributed file systems, etc.) New ones still arriving – ZFS, GoogleFS, Oracle ASM, FUSE
8
File-System Implementation
Per-file File Control Block (FCB) contains many details about the file (METADATA) An entry per-file inode number, permissions, size, dates NTFS stores into in master file table using relational DB structures
9
In-Memory File System Structures
Mount table storing file system mounts, mount points, file system types system-wide open-file table contains a copy of the FCB of each file and other info per-process open-file table contains pointers to appropriate entries in system-wide open-file table as well as other info The following figure illustrates the necessary file system structures provided by the operating systems Figure 12-3(a) refers to opening a file Figure 12-3(b) refers to reading a file Plus buffers hold data blocks from secondary storage Open returns a file handle for subsequent use Data from read eventually copied to specified user process memory address
10
In-Memory File System Structures
11
File System Structures
Application program calls open Logical File System determines access, protection, directory lookup. Logical File System populates per-process open file table and system-wide open file table. File organization module reads File Control Block containing logical file location. Basic File System maps logical blocks to physical blocks. Device Drivers – I/O control driver ops table accessed for open call. Application program calls read Logical File System uses file descriptor/handle to access per-process open file table for file pointer. File organization module maps file pointer to logic blocks based on file system implementation. Basic File System maps logical blocks to physical blocks and maps data read into memory and cache. Device Drivers – I/O control reads physical blocks as drive, cylinders, tracks, sectors.
12
Virtual File Systems Virtual File Systems (VFS) on Unix provide an object-oriented way of implementing file systems VFS allows the same system call interface (the API) to be used for different types of file systems Separates file-system generic operations from implementation details Implementation can be one of many file systems types, or network file system Implements vnodes which hold inodes or network file details Then dispatches operation to appropriate file system implementation routines
13
Virtual File Systems (Cont.)
The API is to the VFS interface, rather than any specific type of file system
14
Virtual File System Implementation
For example, Linux has four object types: inode, file, superblock, dentry VFS defines set of operations on the objects that must be implemented Every object has a pointer to a function table Function table has addresses of routines to implement that function on that object For example: • int open(. . .)—Open a file • int close(. . .)—Close an already-open file • ssize t read(. . .)—Read from a file • ssize t write(. . .)—Write to a file • int mmap(. . .)—Memory-map a file
15
Directory Implementation
Linear list of file names with pointer to the data blocks Simple to program Time-consuming to execute Linear search time Could keep ordered alphabetically via linked list or use B+ tree Hash Table – linear list with hash data structure Decreases directory search time Collisions – situations where two file names hash to the same location Only good if entries are fixed size, or use chained-overflow method
16
File Allocation Methods
In all allocation methods, disk is formatted into blocks. As a result, it is possible to have both internal and external fragmentation in schedules that use contiguous blocks. Each allocation method has a different way of calculating the Physical Address from the Logical Address (LA in slides) Calculate the PA from the LA as a function of blocksize. An allocation method refers to how disk blocks are allocated for files: Contiguous Linked List Indexed Good online source for this topic
17
Allocation Methods - Contiguous
Contiguous allocation – each file occupies set of contiguous blocks Advantages Best performance in most cases because it limits seek time. Simple – only starting location (block #) and length (number of blocks) are required Disadvantages Problems include finding space for file, knowing file size, external fragmentation, need for compaction off-line (downtime) or on-line. Hard to grow files. Contiguous allocation of blocks results in both internal and external fragmentation
18
Contiguous Allocation
Mapping from logical to physical Accessing the file requires a minimum of head movement. Easy to calculate block location: block i of a file, starting at disk address b, is b + i. Difficulty is in finding the contiguous space, especially for a large file. Problem is one of dynamic allocation (first fit, best fit, etc.) which has external fragmentation. If many files are created/deleted, compaction will be necessary. It's hard to estimate at create time what the size of the file will ultimately be. What happens when we want to extend the file --- we must either terminate the owner of the file, or try to find a bigger hole. Notice the greyed areas, 0-1, 6-7, 14-16, 19-24, Each group is a file. LA/512 Q R Block to be accessed = Q + starting address Displacement into block = R
19
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 set of contiguous blocks Extents are allocated for file allocation as a set of blocks A file consists of one or more extents Purpose is for large files to save space on meta data: Linux ext4 supports extents. List the extents which saves space by not listing data for each individual block.
20
Allocation Methods - Linked
Linked allocation – each file a linked list of blocks Advantages No external fragmentation, No compaction needed. Free space management system called to allocate new block Internal fragmentation still a problem. Improve efficiency by clustering blocks into groups. As disk fills, some files will be allocated on distant blocks. Disadvantages Each block contains pointer to next block, ends in nil pointer. Reliability can be a problem because pointers can become corrupted. lose pointer a in the middle = a large portion of the file lost. Locating a block can take many I/Os and disk seeks Supports only sequential files well
21
Linked Allocation Each file is a linked list of disk blocks: blocks may be scattered anywhere on the disk pointer block = Mapping – 508 bytes for content, byte for pointer to next block LA/508 Q R Block to be accessed is the Qth block in the linked chain of blocks representing the file. Displacement into block = R + 4 - Remember to skip the pointer at the beginning.
22
Linked Allocation
23
Linked Allocation LINKED ALLOCATION
Each file is a linked list of disk blocks, scattered anywhere on the disk. At file creation time, simply tell the directory about the file. When writing, get a free block and write to it, enqueueing it to the file header. There's no external fragmentation since each request is for one block. Method can only be effectively used for sequential files.
24
Allocation Methods – Linked (Cont.)
FAT (File Allocation Table) variation Beginning of volume has table, indexed by block number The pointers are removed from the blocks and moved into a table. The index corresponds to the block under. Follow the links in the table. Advantages Much like a linked list, but faster on disk and cacheable New block allocation simple Don’t lose anything to pointers internally. Just the table. More stable Disadvantages Still best for sequential files.
25
File-Allocation Table
26
Linked Allocation (con’t)
Pointers use up space in each block. Reliability is not high because any loss of a pointer loses the rest of the file. A File Allocation Table is a variation of this. It uses a separate disk area to hold the links. This method doesn't use space in data blocks. Many pointers may remain in memory. A FAT file system is used by MS-DOS.
27
Allocation Methods - Indexed
Indexed allocation Each file has its own index block(s) of pointers to its data blocks Logical view
28
Example of Indexed Allocation
29
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 bytes and block size of 512 bytes. We need only 1 block for index table LA/512 Q R Q = displacement into index table R = displacement into block
30
Logical Address Mapping
Logical Address is a byte offset from the beginning of the file. Given a Logical Address (LA) calculate the Physical Address using the different allocation schemes Contiguous Allocation LA / blocksize Integer Quotient gives the block number Remainder gives the offset into the block. Linked Allocation LA/508 LA / (blocksize - pointersize) gives the block number Integer Quotient gives block number Remainder + pointersize gives the offset into the block. LA/512 Q R
31
Logical Address Mapping (cont)
FAT Allocation LA/512 LA / blocksize Quotient is block number, Remainder is offset into block. Same as contiguous allocation because the links are held in a separate table. The block number defines the number of table entries to traverse. Direct Indexed Allocation LA / blocksize LA/512 Quotient is index in the index table Remainder is the offset into the data block
32
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 R1 Q1 = block of index table – traverse this number of linked blocks R1 is used as follows: R1 / 512 Q2 R2 Q2 = displacement into block of index table R2 displacement into block of file:
33
Indexed Allocation – Mapping (Cont.)
Two-level index (4K blocks could store 1,024 four-byte pointers in outer index -> 1,048,567 data blocks and file size of up to 4GB) LA / (512 x 512) Q1 R1 Q1 = displacement into outer-index R1 is used as follows: R1 / 512 Q2 R2 Q2 = displacement into block of index table R2 displacement into block of file:
34
Combined Scheme: UNIX UFS
4K bytes per block, 32-bit addresses More index blocks than can be addressed with 32-bit file pointer
35
Indexed Allocation – Mapping (Cont.)
36
Free Space Management We need a way to keep track of space currently free. This information is needed when we want to create or add (allocate) to a file. When a file is deleted, we need to show what space is freed up. BIT VECTOR METHOD Each block is represented by a bit means blocks 2, 3, 6 are free. This method allows an easy way of finding contiguous free blocks. Requires the overhead of disk space to hold the bitmap. A block is not REALLY allocated on the disk unless the bitmap is updated. What operations (disk requests) are required to create and allocate a file using this implementation?
37
Free-Space Management
File system maintains free-space list to track available blocks/clusters (Using term “block” for simplicity) Bit vector or bit map (n blocks) … 1 2 n-1 bit[i] = 1 block[i] free 0 block[i] occupied First Free Block number calculation (number of bits per word) * (number of 0-value words) + offset of first 1 bit CPUs have instructions to return offset within word of first “1” bit
38
Free-Space Management (Cont.)
Bit map requires extra space Example: block size = 4KB = 212 bytes disk size = 240 bytes (1 terabyte) n = 240/212 = 228 bits (or 32MB) if clusters of 4 blocks -> 8MB of memory Easy to get contiguous files using a bitmap. Find a sequence of 1’s that matches the file size: Filesize / Blocksize = sequence of 1’s required.
39
Free Space Management FREE LIST METHOD
Free blocks are chained together, each holding a pointer to the next one free. Link List This is very inefficient since a disk access is required to look at each sector. GROUPING METHOD In one free block, put lots of pointers to other free blocks. Include a pointer to the next block of pointers. Linked indexed list. COUNTING METHOD Since many free blocks are contiguous, keep a list of dyads holding the starting address of a "chunk", and the number of blocks in that chunk. Format < disk address, number of free blocks >
40
Linked Free Space List on Disk
Linked list (free list) Cannot get contiguous space easily No waste of space No need to traverse the entire list (if # free blocks recorded) Easy to go from Linked File allocation to liked free list.
41
Free-Space Management (Cont.)
Grouping Modify linked list to store address of next n-1 free blocks in first free block, plus a pointer to next block that contains free-block-pointers (like this one) Easy to go from Linked Index Allocation to Grouped Free List. Counting Because space is frequently contiguously used and freed, with contiguous-allocation allocation, extents, or clustering Keep address of first free block and count of following free blocks Free space list then has entries containing addresses and counts
42
Free-Space Management (Cont.)
Space Maps Used in ZFS Consider meta-data I/O on very large file systems Full data structures like bit maps couldn’t fit in memory -> thousands of I/Os Divides device space into metaslab units and manages metaslabs Given volume can contain hundreds of metaslabs Each metaslab has associated space map Uses counting algorithm But records to log file rather than file system Log of all block activity, in time order, in counting format Metaslab activity -> load space map into memory in balanced-tree structure, indexed by offset Replay log into that structure Combine contiguous free blocks into single entry
43
TRIMing Unused Blocks HDDS overwrite in place so need only free list
Blocks not treated specially when freed Keeps its data but without any file pointers to it, until overwritten Storage devices not allowing overwrite (like NVM) suffer badly with same algorithm Must be erased before written, erases made in large chunks (blocks, composed of pages) and are slow TRIM is a newer mechanism for the file system to inform the NVM storage device that a page is free Can be garbage collected or if block is free, now block can be erased
44
Efficiency and Performance
Efficiency dependent on: Disk allocation and directory algorithms Types of data kept in file’s directory entry Pre-allocation or as-needed allocation of metadata structures Fixed-size or varying-size data structures
45
Efficiency and Performance
Efficiency dependent on: Disk allocation and directory algorithms Depends on file access methods File sizes Types of data kept in file’s directory entry Pre-allocation or as-needed allocation of metadata structures Some systems pre-allocate inodes for more efficient file creation. Fixed-size or varying-size data structures Fixed-size often based on blocksize or some multiple
46
Efficiency and Performance (Cont.)
Keeping data and metadata close together Buffer cache – separate section of main memory for frequently used blocks Synchronous writes sometimes requested by apps or needed by OS No buffering / caching – writes must hit disk before acknowledgement Asynchronous writes more common, buffer-able, faster Free-behind and read-ahead – techniques to optimize sequential access [buffering into memory] Reads frequently slower than writes – write caching makes it look like the write has completed. A read has to complete.
47
I/O Without a Unified Buffer Cache
48
Unified Buffer Cache A unified buffer cache uses the same page cache to cache both memory-mapped pages and ordinary file system I/O to avoid double caching But which caches get priority, and what replacement algorithms to use? LRU – most common but loses on referential regularity such as sequential references and looping. Always researching better cache replacement algorithms
49
I/O Using a Unified Buffer Cache
50
Recovery Consistency checking – compares data in directory structure with data blocks on disk, and tries to fix inconsistencies Can be slow and sometimes fails Advances in journaling and checksums help consistency checking – ext4 file system Use system programs to back up data from disk to another storage device (magnetic tape, other magnetic disk, optical) Recover lost file or disk by restoring data from backup
51
Consistency/Recovery
GAINING CONSISTENCY Required when system crashes or data on the disk may be inconsistent: Consistency checker - compares data in the directory structure with data blocks on disk and tries to fix inconsistencies. For example, What if a file has a pointer to a block, but the bit map for the free-space-management says that block isn't allocated. Back-up- provides consistency by copying data to a "safe" place. Recovery - occurs when lost data is retrieved from backup.
52
Log Structured File Systems
Log structured (or journaling) file systems record each metadata 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 (sequentially) Sometimes to a separate device or section of disk However, the file system may not yet be updated The transactions in the log are asynchronously written to the file system structures When the file system structures are modified, the transaction is removed from the log If the file system crashes, all remaining transactions in the log must still be performed Faster recovery from crash, removes chance of inconsistency of metadata
53
The Apple File System
54
End of Chapter 12
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.