Thanks to visit codestin.com
Credit goes to www.scribd.com

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

Chapter 04 - FileSystems

Chapter 4 of 'Modern Operating Systems' discusses file systems, emphasizing their essential requirements for long-term information storage, including large capacity, data persistence, and concurrent access. It covers the organization of files and directories, file operations, and various file system implementations, such as FAT and UNIX. Additionally, the chapter addresses performance optimization, block size considerations, and methods for managing disk space and free blocks.

Uploaded by

zan
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 views61 pages

Chapter 04 - FileSystems

Chapter 4 of 'Modern Operating Systems' discusses file systems, emphasizing their essential requirements for long-term information storage, including large capacity, data persistence, and concurrent access. It covers the organization of files and directories, file operations, and various file system implementations, such as FAT and UNIX. Additionally, the chapter addresses performance optimization, block size considerations, and methods for managing disk space and free blocks.

Uploaded by

zan
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/ 61

Modern Operating Systems

Fifth Edition

Chapter 4
File Systems

Copyright © 2023, 2025, 2008 Pearson Education, Inc. All Rights Reserved
File Systems (1 of 4)
Essential requirements for long-term information storage:
1. It must be possible to store a very large amount of information.
2. Information must survive termination of process using it.
3. Multiple processes must be able to access information concurrently.

Copyright © 2023, 2025, 2008 Pearson Education, Inc. All Rights Reserved
File Systems (2 of 4)
Think of a disk as a linear sequence of fixed-size blocks and supporting two
operations:
1. Read block k.
2. Write block k

Copyright © 2023, 2025, 2008 Pearson Education, Inc. All Rights Reserved
File Systems (3 of 4)
Questions that quickly arise:
1. How do you find information?
2. How do you keep one user from reading another user’s data?
3. How do you know which blocks are free?

Copyright © 2023, 2025, 2008 Pearson Education, Inc. All Rights Reserved
File Systems (4 of 4)
File Systems are:
• A way to organize and (persistently) store information
• An abstraction over storage devices:
– Hard disk, SSD, network, RAM, …
• Organized in files and (typically) directories.
• Examples:
– FAT12/FAT16: MS-DOS
– NTFS: Windows
– Ext4: Linux
– APFS: macOS/iOS
Copyright © 2023, 2025, 2008 Pearson Education, Inc. All Rights Reserved
Overview
Storage

Operating System

I want to read /home/ast/shell.c

User program

Copyright © 2023, 2025, 2008 Pearson Education, Inc. All Rights Reserved
Overview
Storage
HDD, SSD, network, RAM, …

Operating System Buffer cache

FAT driver
Virtual File Page NTFS driver
System cache
Ext4 driver

...
Syscall
open, read, write, readdir, ...

User program

Copyright © 2023, 2025, 2008 Pearson Education, Inc. All Rights Reserved
Overview
Storage Chapter 5 (I/O)
HDD, SSD, network, RAM, …

Operating System Buffer cache

FAT driver
Virtual File Page NTFS driver
System cache
Ext4 driver

...
Chapter 4 (FS)
Syscall
open, read, write, readdir, ...

User program

Copyright © 2023, 2025, 2008 Pearson Education, Inc. All Rights Reserved
Files
• Abstract storage nodes
• File access:
– Sequential vs. random access
• File types:
– Regular files, directories, soft links
– Special files (e.g., device files, metadata files)

• File structure:
– OS’ perspective: Files as streams of bytes
– Program’s perspective: Archives, Executables, etc.

Copyright © 2023, 2025, 2008 Pearson Education, Inc. All Rights Reserved
File Naming
• Different file systems have different limitations/conventions for file names
• File extensions
• File name length
– FAT12: 8.3 characters (later extended to 255)
– Ext4: 255 characters

• Special characters in file names


– FAT12: No "*/:<>?\| and more
– Ext4: No '\0' and '/', or the special names "." and ".."

• Case sensitivity

Copyright © 2023, 2025, 2008 Pearson Education, Inc. All Rights Reserved
File Naming
Figure 4.1 Some typical file extensions.
Extension Meaning
.bak Backup file
.c C source program
.gif Compuserve Graphical Interchange Format image
.html World Wide Web HyperText Markup Language document
.jpg Still picture encoded with the J PEG standard
.mp3 Music encoded in MPEG layer 3 audio format
.mpg Movie encoded with the M PEG standard
.o Object file (compiler output, not yet linked)
.pdf Portable Document Format file
.ps PostScript file
.tex Input for the T EX formatting program
.txt General text file
.zip Compressed archive

Copyright © 2023, 2025, 2008 Pearson Education, Inc. All Rights Reserved
File Types

Figure 4-3. (a) An executable file. (b) An archive

Copyright © 2023, 2025, 2008 Pearson Education, Inc. All Rights Reserved
File Attributes (1 of 2)
Figure 4-5. Some possible file attributes.

Attribute Meaning
Protection Who can access the file and in what way
Password Password needed to access the file
Creator ID of the person who created the file
Owner Current owner
Read-only flag 0 for read/write; 1 for read only
Hidden flag 0 for normal; 1 for do not display in listings
System flag 0 for normal files; 1 for system file
Archive flag 0 for has been backed up; 1 for needs to be backed up
ASCII/binary flag 0 for ASCII file; 1 for binary file
Random access flag 0 for sequential access only; 1 for random access

Copyright © 2023, 2025, 2008 Pearson Education, Inc. All Rights Reserved
File Operations (1 of 2)
1. Create & Delete
2. Open & Close
3. Read & Write
4. Append
5. Seek
6. Get & Set attributes
7. Rename

Copyright © 2023, 2025, 2008 Pearson Education, Inc. All Rights Reserved
File Operations - UNIX
• Opening and reading file:
int fd = open(“foo.txt”, O_RDONLY);
char buf[512];
ssize_t bytes_read = read(fd, buf, 512);
close(fd);
printf(“read %zd: %s\n”, bytes_read, buf);
• Opening a file returns a handle (file descriptor) for future operations.
• Any function may return an error, e.g.:
– -ENOENT: File does not exist
– -EBADF: Bad file descriptor

Copyright © 2023, 2025, 2008 Pearson Education, Inc. All Rights Reserved
File Operations - UNIX
• Seeking in files:
int fd = open(“foo.txt”, O_RDONLY);
lseek(fd, 128, SEEK_CUR);
char buf[8];
read(fd, buf, 8);
close(fd);
• Move current position in file forwards or backwards.

Copyright © 2023, 2025, 2008 Pearson Education, Inc. All Rights Reserved
File Operations - UNIX
• Writing files:
int fd = open(“foo.txt”, O_WRONLY | O_CREAT | O_TRUNC);
char buf[] = “Hi there”;
write(fd, buf, strlen(buf));
close(fd);
• O_CREAT: Create file if it does not exist
• O_TRUNC: “Truncate” file to size 0 if it exists (i.e., throw away current file
contents)

Copyright © 2023, 2025, 2008 Pearson Education, Inc. All Rights Reserved
File Operations - UNIX
• Removing file:
– unlink(“foo.txt”);
• Renaming file:
– rename(“foo.txt”, “bar.txt”);
• Change file permission attribute:
– chmod(“foo.txt”, 0755);
• Change file owner attribute:
– chown(“foo.txt”, uid, gid);

Copyright © 2023, 2025, 2008 Pearson Education, Inc. All Rights Reserved
Directories
• Data structures organizing and maintaining information about files
• Often stored as file entries with special attributes
• Directories are denoted by “/” (Unix) or “\” (Windows)
• Special directory entries:
– . Current directory
– .. Parent directory

Copyright © 2023, 2025, 2008 Pearson Education, Inc. All Rights Reserved
Single-Level Directory Systems

Figure 4-7. A single-level directory system containing four files.

Copyright © 2023, 2025, 2008 Pearson Education, Inc. All Rights Reserved
Hierarchical Directory Systems

Figure 4-8. A hierarchical directory system.

Copyright © 2023, 2025, 2008 Pearson Education, Inc. All Rights Reserved
Directory Operations
1. Create & Delete
2. Opendir & Closedir
3. Readdir (no writedir)
4. Rename
5. Link & Unlink

Copyright © 2023, 2025, 2008 Pearson Education, Inc. All Rights Reserved
Directory Operations - UNIX
• Reading current directory contents:
DIR *dirp = opendir(“.”);

struct dirent *dirent;


while ((dirent = readdir(dirp)))
printf(“%s\n”, dirent->d_name);

closedir(dirp);

Copyright © 2023, 2025, 2008 Pearson Education, Inc. All Rights Reserved
Virtual File Systems (1 of 2)

Figure 4-21. Position of the virtual file system.

Copyright © 2023, 2025, 2008 Pearson Education, Inc. All Rights Reserved
File System Implementation
• How to store files?
• How to implement directories?
• How to manage disk space?
• How to ensure file system performance?
• How to ensure file system dependability?

Copyright © 2023, 2025, 2008 Pearson Education, Inc. All Rights Reserved
File System Layout

Figure 4-10. A possible file system layout.

Copyright © 2023, 2025, 2008 Pearson Education, Inc. All Rights Reserved
How to Store Files on the Disk?
Contiguous allocation
• Store files as a contiguous stream of bytes
• Requires max file size, prone to fragmentation

Copyright © 2023, 2025, 2008 Pearson Education, Inc. All Rights Reserved
Implementing Files Contiguous Layout

Figure 4-12. (a) Contiguous allocation of disk space for seven files. (b) The state of the
disk after files D and F have been removed.

Copyright © 2023, 2025, 2008 Pearson Education, Inc. All Rights Reserved
How to Store Files on the Disk?
Contiguous allocation
• Store files as a contiguous stream of bytes
• Requires max file size, prone to fragmentation

Block-based strategies
• Linked List
• File Allocation Table
• i-nodes

Copyright © 2023, 2025, 2008 Pearson Education, Inc. All Rights Reserved
Implementing Files - Linked List Allocation

Figure 4-13. Storing a file as a linked list of disk blocks.

Copyright © 2023, 2025, 2008 Pearson Education, Inc. All Rights Reserved
Implementing Files - File Allocation Table

Figure 4-14. Linked list allocation using a file allocation table in main memory.

Copyright © 2023, 2025, 2008 Pearson Education, Inc. All Rights Reserved
Implementing Files - inodes

Figure 4-15. An example i-node.

Copyright © 2023, 2025, 2008 Pearson Education, Inc. All Rights Reserved
Implementing Files - inodes with Indirect Blocks

Figure 4-35. A UNIX i-node

Copyright © 2023, 2025, 2008 Pearson Education, Inc. All Rights Reserved
File System Implementation
• How to store files?
• How to implement directories?
• How to manage disk space?
• How to ensure file system performance?
• How to ensure file system dependability?

Copyright © 2023, 2025, 2008 Pearson Education, Inc. All Rights Reserved
Directory Implementation
• How to store directory entries and attributes?
• How to find the root directory?
• How to find any other directory?

Copyright © 2023, 2025, 2008 Pearson Education, Inc. All Rights Reserved
FAT12/FAT16 Layout

Boot Reserved FAT #1 FAT #2 Root Clusters


Sector Directory

• Reserved later used for FS information


• FAT #1/#2: preallocated File Allocation Tables
• Preallocated root directory
• Clusters represent addressable blocks:
– FAT contains chains indexed by starting cluster

Copyright © 2023, 2025, 2008 Pearson Education, Inc. All Rights Reserved
FAT12/16 Directory Entry

Copyright © 2023, 2025, 2008 Pearson Education, Inc. All Rights Reserved
UNIX Directory Entry

• Directory entry stores only name and #i-node


• Attributes are stored in the i-node
• One i-node per file, first i-node is the root
• Where are i-nodes stored?

Copyright © 2023, 2025, 2008 Pearson Education, Inc. All Rights Reserved
UNIX FS Layout (MINIX Example)

Copyright © 2023, 2025, 2008 Pearson Education, Inc. All Rights Reserved
Directory Lookup

Figure 4-36. The steps in looking up /usr/ast/mbox.

Copyright © 2023, 2025, 2008 Pearson Education, Inc. All Rights Reserved
Hard Links vs. Soft Links
• Hard links reference a shared file (i-node)
– The file is removed only with no hard links left
– Space-efficient (only 1 directory entry per hard link)
– Good to manage shared files across owners

• Soft (or symbolic) links reference a file name


– Removing the file renders its soft links invalid
– Less space-efficient (need 1 i-node per soft link)
– More flexible (can reference file names across FSes)

Copyright © 2023, 2025, 2008 Pearson Education, Inc. All Rights Reserved
File System Implementation
• How to store files?
• How to implement directories?
• How to manage disk space?
• How to ensure file system performance?
• How to ensure file system dependability?

Copyright © 2023, 2025, 2008 Pearson Education, Inc. All Rights Reserved
Block Size
• How do choose the block size?
• Important speed-space tradeoff to consider

• Larger block sizes allow transferring more data per block


– Better overall data rates
• Smaller block sizes reduce space overhead for small files
– Less fragmentation

Copyright © 2023, 2025, 2008 Pearson Education, Inc. All Rights Reserved
Block Size Impact

Figure 4-23. The dashed curve (left-hand scale) gives the data rate of a disk. The solid curve (right-hand scale) gives
the disk space efficiency. All files are 4 KB.

Copyright © 2023, 2025, 2008 Pearson Education, Inc. All Rights Reserved
Keeping Track of Free Blocks (1 of 2)

Figure 4-24. (a) Storing the free list on a linked list. (b) A bitmap.

Copyright © 2023, 2025, 2008 Pearson Education, Inc. All Rights Reserved
File System Implementation
• How to store files?
• How to implement directories?
• How to manage disk space?
• How to ensure file system performance?
• How to ensure file system dependability?

Copyright © 2023, 2025, 2008 Pearson Education, Inc. All Rights Reserved
File System Performance
How to optimize file system performance?
• Minimize disk access
• Minimize seek time
• Minimize space usage

Copyright © 2023, 2025, 2008 Pearson Education, Inc. All Rights Reserved
Minimize Disk Access: Caching

• Buffer cache: Cache disk blocks in RAM


• Page cache: Cache VFS pages (before going to driver)
– Often same data as buffer cache, so OS may merge them
Copyright © 2023, 2025, 2008 Pearson Education, Inc. All Rights Reserved
Cache Management

• Caches have LRU semantics adapted to:


– Blocks with poor temporal locality
– Critical blocks for file system consistency

• Write-through caching vs. periodic syncing


• Disk read-ahead: read blocks that may be used soon
Copyright © 2023, 2025, 2008 Pearson Education, Inc. All Rights Reserved
Minimize Disk Access: Log-structure File Systems
i-nodes Data blocks segment segment

UNIX FS Log-Structured File System

• Idea: Optimize for frequent small writes


• Collect pending writes in a log segment with i-nodes, entries, blocks
• Segments are often flushed to disk and can be large (e.g., 1MB)
• i-node map to find i-nodes in the log
• Garbage collection to reclaim old log entries

Copyright © 2023, 2025, 2008 Pearson Education, Inc. All Rights Reserved
Minimize Seek Time: HDD Layout Management
• Not an issue for modern SSDs
– Seek time is constant

• Important for traditional HDDs


• Different strategies to minimize HDD seek time:
– Try to allocate files contiguously
– Defragment disk
– Store small file data “inline” in i-node
– Spread i-nodes over the disk rather than at the start

Copyright © 2023, 2025, 2008 Pearson Education, Inc. All Rights Reserved
File System Implementation
• How to store files?
• How to implement directories?
• How to manage disk space?
• How to ensure file system performance?
• How to ensure file system dependability?

Copyright © 2023, 2025, 2008 Pearson Education, Inc. All Rights Reserved
File System Dependability Threats
• Disk failures:
– Bad blocks
– Whole-disk errors

• Power failures:
– (Meta)-data inconsistently written to disk

• Software bugs:
– Bad (meta)-data written to disk

• User errors:
– rm *.o vs. rm * .o

• Lost or stolen computer


Copyright © 2023, 2025, 2008 Pearson Education, Inc. All Rights Reserved
File System Backups
Backups to disk are generally made to handle one of two
potential problems:
1. Recover from disaster.
2. Recover from stupidity.

Copyright © 2023, 2025, 2008 Pearson Education, Inc. All Rights Reserved
Backups: Properties
• Incremental vs. full
• Online vs. offline
• Physical vs. logical
• Compressed vs. uncompressed
• Local vs. remote

Copyright © 2023, 2025, 2008 Pearson Education, Inc. All Rights Reserved
RAID: Redundant Array of Independent Disks

RAID 0

RAID 1
RAID 5

• RAID 0 & 2: Expand file system over multiple disks (+storage)


• RAID 1: Duplicate file system over disks (+redundancy)
• RAID 3-6: Store parity information (+storage, +redundancy)

Copyright © 2023, 2025, 2008 Pearson Education, Inc. All Rights Reserved
File System Consistency Check
Programs like fsck and chkdsk try to find (and fix) consistency errors in file
system metadata
• Corrupt values
• Used blocks also marked as free
• Blocks not marked as free nor used
• Blocks being used multiple times
• ...

Copyright © 2023, 2025, 2008 Pearson Education, Inc. All Rights Reserved
File System Consistency

Figure 4-29. File system states. (a) Consistent. (b) Missing block. (c) Duplicate block in free list. (d) Duplicate data
block.

Copyright © 2023, 2025, 2008 Pearson Education, Inc. All Rights Reserved
Journaling File Systems
• Idea: Use “logs” for crash recovery
• First write transactional operations in log
– E.g., removing a file:
▪ Remove file from its directory
▪ Release i-node to the pool of free i-nodes
▪ Return all disk blocks to pool of free blocks
• After crash, replay operations from log
• Single operations need to be idempotent
• Should support multiple, arbitrary crashes
• Journaling widely used in modern FSes (e.g., Ext4, NTFS)
Copyright © 2023, 2025, 2008 Pearson Education, Inc. All Rights Reserved
Textbook (Chapter 4)
All sections except:
• Section 4.5.3

Copyright © 2023, 2025, 2008 Pearson Education, Inc. All Rights Reserved
Copyright

This Work is protected by the United States copyright laws and is


provided solely for the use of instructors teaching their courses and

!
assessing student learning. Dissemination or sale of any part of this Work
(including on the World Wide Web) will destroy the integrity of the Work
and is not permitted. The Work and materials from it should never be
made available to students except by instructors using the accompanying
text in their classes. All recipients of this work are expected to abide by
these restrictions and to honor the intended pedagogical purposes and
the needs of other instructors who rely on these materials.

Copyright © 2023, 2025, 2008 Pearson Education, Inc. All Rights Reserved

You might also like