0% found this document useful (0 votes)
4 views25 pages

Lec7 DR Eslam

The document discusses file-system implementation in operating systems, focusing on local file systems, directory structures, block allocation methods, and free-space management. It explores various allocation techniques such as contiguous, linked, FAT, and indexed allocation, along with their performance implications and management strategies for free disk space. Additionally, it addresses efficiency, performance optimization, and recovery from file system failures.

Uploaded by

Lazey Creature
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views25 pages

Lec7 DR Eslam

The document discusses file-system implementation in operating systems, focusing on local file systems, directory structures, block allocation methods, and free-space management. It explores various allocation techniques such as contiguous, linked, FAT, and indexed allocation, along with their performance implications and management strategies for free disk space. Additionally, it addresses efficiency, performance optimization, and recovery from file system failures.

Uploaded by

Lazey Creature
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 25

Operating System 2- CS402

Lecture 7
File-System Implementation
Dr. Eslam Omara
Spring-2025

Operating System Concepts – 10th Edition 13.1 Silberschatz, Galvin and Gagne ©2018
Objectives
▪ Describe the details of implementing local file systems and
directory structures.
▪ Discuss block allocation (An allocation method refers to how
disk blocks are allocated for files) and Free-Space
Management.
▪ Explore file system efficiency and performance issues.
▪ Look at recovery from file system failures.

Operating System Concepts – 10th Edition 13.2 Silberschatz, Galvin and Gagne ©2018
Indexed Allocation
▪ Linked allocation:
• Solves the external-fragmentation and size-declaration problems
of contiguous allocation.
• Cannot support efficient direct access, since the pointers to the
blocks are scattered with the blocks themselves all over the disk
and must be retrieved in order.
▪ Indexed allocation:
• Solves the direct access problem by bringing all the pointers
together into the index block.

Operating System Concepts – 10th Edition 13.3 Silberschatz, Galvin and Gagne ©2018
Indexed Allocation
▪ Each file has its own index block, an array of disk-block addresses.
▪ The directory contains the address of the index block.
▪ The ith entry in the index block points to the ith block of the file.
▪ To find and read the ith block, use the pointer in the ith index-block entry.

▪ Supports direct access, without


suffering from external fragmentation
▪ Suffer from:
• Wasted space: Pointer overhead
of the index block is greater than
the pointer overhead of linked
allocation (a file has only one or
two blocks).
• Index block Size.

Operating System Concepts – 10th Edition 13.4 Silberschatz, Galvin and Gagne ©2018
Performance
▪ Allocation methods vary in storage efficiency and data-block access time.
▪ Best allocation method depends on file access type:
• Contiguous great for sequential and random.
• Linked good for sequential, not random.
▪ Declare access type at file creation (contiguous or linked):
• If the system support direct-access files by using contiguous allocation
and sequential-access files by using linked allocation.
▪ Indexed more complex:
• Performance depends on index structure, size of the file, position of the
block desired.
• Single block access could require index block read and data block read.

Operating System Concepts – 10th Edition 13.5 Silberschatz, Galvin and Gagne ©2018
Performance
Method Advantages Disadvantages

▪Simple to implement ▪External fragmentation


Contiguous ▪Fast sequential and direct ▪Difficult to grow files
access ▪May need compaction

▪Poor random access


▪No external fragmentation
Linked ▪Pointer overhead
▪Easy file growth
▪Risk of broken links

▪No external fragmentation ▪Table must be in memory


FAT ▪Easier random access than ▪Linear traversal for access
linked ▪Fragmentation

▪Support direct access ▪Overhead of index block


Indexed ▪Easy to grow ▪Fixed-size index may limit file
▪No fragmentation size

Operating System Concepts – 10th Edition 13.6 Silberschatz, Galvin and Gagne ©2018
Free-Space Management
▪ File system maintains free-space list to keep track of free disk
space (available blocks/clusters).
▪ Free-space list records free disk blocks not allocated to some file
or directory.
▪ Create a file:
• Search the free-space list for the required amount of space.
• Allocate the space to the new file.
• Remove space from the free-space list.
▪ Deleted a file:
• Add disk space to the free-space list.

Operating System Concepts – 10th Edition 13.7 Silberschatz, Galvin and Gagne ©2018
Free-Space Management
▪ Bit vector or bit map:
• Free-space list implemented as a bit map or bit vector.
• If the block is free, the bit is 1; if the block is allocated, the bit is 0.

0 1 2 n-1
Free blocks 2, 3, 4, 5, 8, 9, 10, 11, 12, 13, …
17, 18 and n=20

 
1  block[i] free
The free-space bit map Bit [i] =
0  block[i] occupied
00111100111111000110

▪ Finding the first free block to allocate disk space:


▪ Divide the bit map into words of length k.
▪ Check each word if it is a 0-valued word.
▪ First non-0 word is scanned for the first 1 bit (the location of the
first free block).

Operating System Concepts – 10th Edition 13.8 Silberschatz, Galvin and Gagne ©2018
Free-Space Management
▪ First free block number calculation =
(number of bits per word) * (number of 0-value words) + offset of
first 1 bit

0 1 2 n-1
Free blocks 2, 3, 4, 5, 8, 9, 10, 11, 12, 13, …
17, 18 and n=20

 
1  block[i] free
The free-space bit map: Bit [i] =
0  block[i] occupied
00111100111111000110
▪ n = 20.
▪ k = 2.
▪ Offset = 0.
▪ First free block = 2*1 + 0 = 2.

▪ CPUs have bit-manipulation instructions to return offset within word of


first “1” bit.

Operating System Concepts – 10th Edition 13.9 Silberschatz, Galvin and Gagne ©2018
Free-Space Management
▪ Advantages:
• Its simplicity and efficiency in finding the first free block or n
consecutive free blocks.
• Easy to allocate contiguous files.
• To be efficient the entire Bit vector is kept in main memory (and
written to disk occasionally for recovery).
▪ Disadvantages: Bit map requires extra space.
• Example:
Block size = 4KB = 212 bytes.
Disk size = 240 bytes (1 terabyte).
No. of blocks n = 240/212 = 228 .
One bit for each block → 228 bits (or 32 MB).
If clusters of 4 blocks → 8 MB of memory.

Operating System Concepts – 10th Edition 13.10 Silberschatz, Galvin and Gagne ©2018
Free-Space Management
▪ Linked Free Space List (Free List).
▪ Link together all the free disk blocks, keep a pointer to the first free block
in a special location on the disk and caching it in memory.
▪ First block contains a pointer to the next free disk block, and so on.
▪ No need to traverse the entire list (if number of free blocks is recorded).
▪ To allocate a block to a file, the first block in the free list is used.
▪ Cannot get contiguous space easily.

Operating System Concepts – 10th Edition 13.11 Silberschatz, Galvin and Gagne ©2018
Free-Space Management
▪ Grouping:
• Modify linked list to store the addresses of n free blocks in the first
free block.
• The first n−1 of these blocks are free.
• The last block contains the addresses of another n free blocks.
• The addresses of many free blocks can be found quickly.
▪ Counting:
• As space is frequently contiguously used and freed with
contiguous-allocation (extents or clustering).
• Keep address of first free block and count of following free
contiguous blocks.
• Free space list entries contain addresses and counts.

Operating System Concepts – 10th Edition 13.12 Silberschatz, Galvin and Gagne ©2018
Free-Space Management
▪ Space Maps:
▪ Oracle’s ZFS file system (Solaris)
• Includes huge numbers of files, directories, and even file systems.
▪ Metaslab: space on the device is divided into chunks of manageable size.
• A given volume may contain hundreds of Metaslabs.
▪ ZFS uses:
• Counting algorithm to store information about free blocks.
• Log-structured file-system.
▪ Space map: a log file contains all block activity (allocating and freeing), in time order,
in counting format.
• Each Metaslab has an associated space map.

Operating System Concepts – 10th Edition 13.13 Silberschatz, Galvin and Gagne ©2018
Free-Space Management
▪ Space Maps:
▪ To allocate or free space from a Metaslab.
• Loads the associated space map into memory in
a tree structure, indexed by offset.
• Replays the log into that structure.
• The in-memory space map is then an accurate
representation of the allocated and free space in
the metaslab.
• Condenses the map by combining contiguous
free blocks into a single entry.
• Update the free-space list on disk as part of the
transaction-oriented operations.

Operating System Concepts – 10th Edition 13.14 Silberschatz, Galvin and Gagne ©2018
TRIMing Unused Blocks
▪ NVM (Non-Volatile Memory) SSDs:
• Can’t overwrite directly: requires a block to be erased before rewritten.
• Erases are slow and done in large units:
 Written in pages (usually 4–16 KB),
 Erased in blocks (usually 128–512 KB).
• Garbage Collection (GC):
 When space is needed, the storage controller gathers all valid data in a
block, moves it elsewhere, and erases the block.
 This process slows down performance.
▪ TRIM Command:
• TRIM lets the file system inform the NVM device which blocks are not used.
• Improves performance as:
• It can erase blocks in advance during idle time, making writes faster.
• Without TRIM, SSDs may waste time moving data during garbage collection.

Operating System Concepts – 10th Edition 13.15 Silberschatz, Galvin and Gagne ©2018
Efficiency and Performance
▪ Disks affect system performance, the slowest main computer component.
▪ Efficiency depends on:
• Block-allocation and Directory management algorithms.
• Types of data kept in a file’s directory entry.
 Last access date: determine when the file was last read .
 If the file is read, a field in the directory structure must be written to.
That means the block must be read into memory, a section
changed, and the block written back to disk
• The size of the pointers used to access data.
 32-bit or 64-bit pointers (64-bit requires more space to store).
• Pre-allocation or as-needed allocation of metadata structures.
• Fixed-size or varying-size data structures.
 Process table and Open-file table can become full.

Operating System Concepts – 10th Edition 13.16 Silberschatz, Galvin and Gagne ©2018
Efficiency and Performance
▪ Performance:
▪ On-board cache: disk controllers include local memory, enough to store entire
tracks.
• Once a seek is performed, the track is read into the disk cache starting at
the sector under the disk head (reducing latency time).
• Disk controller transfers any sector requests to OS.
• OS cache the blocks in main memory.
▪ OS
• Maintain a separate section of main memory for a Buffer Cache.
• Cache file data using a Page Cache.
 Cache file data as pages rather than file-system-oriented blocks.
 Using virtual addresses is efficient than caching through physical disk
blocks.
 Interface with virtual memory rather than the file system.

Operating System Concepts – 10th Edition 13.17 Silberschatz, Galvin and Gagne ©2018
Page Cache
▪ Alternatives for opening and accessing a file
▪ Standard system calls read() and write().
• System calls go through the buffer cache.
▪ Use memory mapping.
▪ Reads in disk blocks from the file system and stores
them in buffer cache.
▪ Requires using two caches the page cache and the
buffer cache.
• Virtual memory system does not interface with
the buffer cache, contents of the file in the
buffer cache must be copied into page cache.
▪ Double caching, caching file-system data twice.

Operating System Concepts – 10th Edition 13.18 Silberschatz, Galvin and Gagne ©2018
I/O Using Unified Buffer Cache

▪ Double Caching
• Wastes memory.
• Wastes CPU and I/O cycles due to extra data
movement in system memory.
• Inconsistency between two caches can corrupt files.

▪ Unified buffer cache:


• Memory mapping and read() and write() system calls
use the same page cache.
• Avoid double caching.
• Allows the virtual memory system to manage file data.

Operating System Concepts – 10th Edition 13.19 Silberschatz, Galvin and Gagne ©2018
Efficiency and Performance
▪ Performance:
▪ Optimize page cache: using different page-replacement algorithms, depending
on the access type of the file.
▪ Random Access optimized by:
• Pages replaced by the Least Recently Used (LRU) algorithm.
• Replace the page that has not been used for the longest time.
▪ Sequential access optimized by:
• Most recently used page will not be used soon, or perhaps never again.
• Free-behind:
 Removes a page from the buffer as soon as the next page is requested.
 Previous pages are not likely to be used again and waste buffer space.
• Read-ahead:
 Requested page and several subsequent pages are read and cached.
 Pages are likely to be requested after the current page is processed.
Retrieving data from disk in one transfer and caching them saves time.

Operating System Concepts – 10th Edition 13.20 Silberschatz, Galvin and Gagne ©2018
Efficiency and Performance
▪ Performance:
▪ Synchronously or asynchronously writes to the file, affects I/O performance.
▪ Synchronous writes.
• Occur in order as the disk subsystem receives them.
• Writes are not buffered.
• Calling routine must wait for the data to reach the disk drive to proceed.
▪ Asynchronous writes.
• Data are stored in the cache, and control returns to the caller.
▪ Most writes are asynchronous.
▪ Metadata writes can be synchronous.
▪ OS include a flag in the open system call to allow a process to request that
writes be performed synchronously.
• Databases use it for atomic transactions, to assure that data reach storage
in the required order.

Operating System Concepts – 10th Edition 13.21 Silberschatz, Galvin and Gagne ©2018
Recovery
▪ Files and directories are kept both in memory and on disk.
▪ A system crash may result in:
• Loss of data or data Inconsistency.
• Inconsistency among on-disk file-system data structures, as directory
structures, free-block pointers, and free FCB pointers.
▪ How Inconsistency occurs:
• Because of the caching OS do to optimize I/O performance.
• Some changes may go directly to disk, while others may be cached.
• Cached changes do not reach disk before a crash occurs.
▪ Example: Free FCB count might indicate that an FCB had been allocated, but
the directory structure might not point to the FCB.
▪ Reasons for File system corruption:
• System crashes.
• Bugs in file-system implementation, disk controllers.
• User applications.
▪ File system must detect the problems and correct them.

Operating System Concepts – 10th Edition 13.22 Silberschatz, Galvin and Gagne ©2018
Consistency Checking
▪ Consistency checker: a systems program compares data in the directory
structure with the data blocks on disk and fixes any inconsistencies.
• Can be slow and sometimes fails.
▪ Loss of a directory entry:
• linked allocation (there is a link from a block to its next block) the directory
structure can be recreated (reconstructed file from the data blocks).
• Indexed allocation: can be disastrous, as data blocks have no knowledge
of one another.
▪ Disadvantages:
• Allow structures to break and repair them on recovery.
• May not be able to recover the structures, resulting in loss of files and even
entire directories.
• Takes system time.

Operating System Concepts – 10th Edition 13.23 Silberschatz, Galvin and Gagne ©2018
Log Structured File Systems
▪ Log-based recovery techniques (or journaling) all metadata changes are
written sequentially to a log.
• Transaction: a set of operations for performing a specific task.
• Transactions written sequentially to the log are considered committed.
• Transactions are asynchronously written to actual file system structures.
 System call returns to user process, allowing it to continue execution.
• When a transaction is completed (file system structures are modified), it
is removed from the log file.
▪ If the file system crashes:
• Undo changes from aborted transaction applied to the file system.
• Remaining transactions in the log must be performed.
▪ Faster recovery from crash, preserve consistency of metadata.

Operating System Concepts – 10th Edition 13.24 Silberschatz, Galvin and Gagne ©2018
End of Lecture

Operating System Concepts – 10h Edition Silberschatz, Galvin and Gagne ©2018

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy