14 File System Implementation
14 File System Implementation
Zhejiang University
Acknowledgement: some pages are based on the slides from Zhi Wang(fsu).
Review
• File sharing
• ACL
•
File-System Structure
• File is a logical storage unit for a collection of related information
• There are many file systems; OS may support several simultaneously
• Linux has Ext2/3/4, Reiser FS/4, Btrfs…
• Windows has FAT, FAT32, NTFS…
• new ones still arriving – ZFS, GoogleFS, Oracle ASM, FUSE
• File system resides on secondary storage (disks)
• disk driver provides interfaces to read/write disk blocks
• fs provides user/program interface to storage, mapping logical to
physical
• file control block – storage structure consisting of information about a file
• File system is usually implemented and organized into layers
Layered File System
Layered File System
• Device drivers manage I/O devices at the I/O control layer
• Given commands like “read drive1, cylinder 72, track 2, sector 10, into memory
location 1060” outputs low-level hardware specific commands to hardware controller
• Basic file system given command like “retrieve block 123” translates to device driver
• File organization module understands files, logical address, and physical blocks
• Translates file name into file number, file handle, location by maintaining file control
blocks (inodes in UNIX)
• Directory management
• Protection
• Layering useful for reducing complexity and redundancy, but adds overhead and can
decrease performance
• Translates file name into file number, file handle, location by maintaining file control
blocks (inodes in UNIX)
• appropriate directory is updated with the new file name and FCB,
i.e., inode
Directory
• Unix – directories are treated as files containing special data
• if it is not, search the directory for the file name; once found, place
the FCB in the System-Wide Open-File Table
• make an entry, i.e., Unix file descriptor, Windows file handle in the
Per-Process Open-File Table, with pointers to the entry in the
System-Wide Open-File Table and other fields which include a
pointer to the current location in the file and the access mode in
which the file is open
Operations - open()
• increment the open cont in the System-Wide Open-File Table
• VFS
Directory Implementation
• Linear list of file names with pointer to the file metadata
• simple to program, but time-consuming to search (e.g., linear search)
• could keep files ordered alphabetically via linked list or use B+ tree
• Hash table: linear list with hash data structure to reduce search time
• collisions are possible: two or more file names hash to the same location
Disk Block Allocation
• Files need to be allocated with disk blocks to store data
• different allocation strategies have different complexity and performance
• Many allocation strategies:
• contiguous
• linked
• indexed
• …
Contiguous Allocation
• Contiguous allocation: each file occupies set of contiguous blocks
• best performance in most cases
• simple to implement: only starting location and length are required
• Contiguous allocation is not flexible
• how to increase/decrease file size?
• need to know file size at the file creation?
• external fragmentation
• how to compact files offline or online to reduce external fragmentation
• need for compaction off-line (downtime) or on-line
• appropriate for sequential disks like tape
• Some file systems use extent-based contiguous allocation
• extent is a set of contiguous blocks
• a file consists of extents, extents are not necessarily adjacent to each other
Contiguous Allocation
Linked Allocation
• Linked allocation: each file is a linked list of disk blocks
• each block contains pointer to next block, file ends at nil pointer
• blocks may be scattered anywhere on the disk (no external
fragmentation, no compaction)
• Disadvantages
• locating a file block can take many I/Os and disk seeks
• Pointer size: 4 of 512 bytes are used for pointer - 0.78% space is wasted
• Reliability: what about the pointer has corrupted!
• Improvements: cluster the blocks - like 4 blocks
• however, has internal fragmentation
Linked Allocation
File-Allocation Table (FAT): MS-DOS
index table
Indexed Allocation
Indexed Allocation
0! 1! 2! n-1!
…!
! !"#
1 ! block[i] free!
bit[i] =!
0 ! block[i] occupied!
Linked Free Space