L11 - File System Implementations
L11 - File System Implementations
Lecture 11
1
Overview
File System Implementation:
File system layout
Disk organization
file.txt
User
OS - FS
Storage
[ CS2106 L11 - AY2021 S1 ]
4
Disk Organization: Overview
Disk organization:
Master Boot Record (MBR) at sector 0 with partition table
Followed by one or more partitions
Each partition can contain an independent file system
7
File Implementation: Overview
Logical view of a file:
A collection of logical blocks
When file size != multiple of logical blocks
Last block may contain wasted space
i.e. internal fragmentation
A good file implementation must:
Keep track of the logical blocks
Allow efficient access
Disk space is utilized effectively
Basically focuses on how to allocate file data on disk
0 1 2 3 file size
start length
count 02 2
4 5 6 7
tr 143 3
8 9 10 11 mail 196 6
list 284 4
12 13 14 15
f 62 2
16 17 18 19
20 21 22 23
Disk
Block
24 25 26 27
Block
28 29 30 31 Number
file size
start end
0 1 2 3
jeep 59 25
4 5 6 7
First Disk
8 9 10 11 Block Number
12 13 14 15 Last Disk
Block Number
16 17 18 19
20 21 22 23
Next Disk Block Number
24 25 26 27 contains a special value to
indicate the end of list
28 29 30 31
12 13 14 15 9 16
Disk Block 10 25
16 17 18 19 Number used
to index table
20 21 22 23 16 1
24 25 26 27
25 -1
28 29 30 31
n-1
[ CS2106 L11 - AY2021 S1 ]
14
File Block Allocation 3: Indexed Allocation
General Idea:
Each file has an index block
An array of disk block addresses
IndexBlock[ N ] == Nth Block address
Pros:
Lesser memory overhead
Only index block of opened file needs to be in memory
Fast direct access
Cons:
Limited maximum file size
Max number of blocks == Number of index block entries
Index block overhead
[ CS2106 L11 - AY2021 S1 ]
15
Indexed Allocation
8 9 10 11 9
16
12 13 14 15
1
Disk Block
16 17 18 19 19 10
used by this
25 file (in order)
20 21 22 23
-1
24 25 26 27 -1
-1
28 29 30 31
17
Free Space Management: Overview
To perform file allocation:
Need to know which disk block is free
i.e. maintain a free space list
Free space management:
Maintain free space information
Allocate:
Remove free disk block from free space list
Needed when file is created or enlarged (appended)
Free:
Add free disk block to free space list
Needed when file is deleted or truncated
21
Directory Structure: Overview
The main tasks of a directory structure:
1. Keep tracks of the files in a directory
Possibly with the file metadata
2. Map the file name to the file information
Remember:
File must be opened before use
Something like open( "data.txt" );
The purpose of the open operation:
Locate the file information using pathname + file name
Path name
List of directory names traversed from root
E.g. /dir2/dir3/data.txt
[ CS2106 L11 - AY2021 S1 ]
22
Directory Structure: Overview (cont)
Given a full path name:
Need to recursively search the directories along the path to arrive at
the file information
Example:
Full path name: /dir2/dir3/data.txt
1. Find "dir2" in directory "/"
Stop if not found (or incorrect type)
2. Find "dir3" in directory "dir2"
Stop if not found (or incorrect type)
3. Find "data.txt" in directory "dir3"
Stop if not found (or incorrect type)
Sub-directory is usually stored as file entry with special type
in a directory
[ CS2106 L11 - AY2021 S1 ]
23
Directory Implementation: Linear List
Directory consists of a list:
Each entry represents a file:
Store file name (minimum) and possibly other metadata
Store file information or pointer to file information
Pros:
Fast lookup
Cons:
Hash table has limited size
Depends on good hash function
[ CS2106 L11 - AY2021 S1 ]
25
Directory Implementation: File Information
File information consists of:
File name and other metadata
Disk blocks information
As discussed in the file allocation schemes earlier
CASE STUDIES
M
B partition 1 partition 2 partition 3 partition 4
R
FAT
FAT Duplicate
optional
Boot root data blocks
directory
8 bytes 3 1 10 2 2 2 4
File
Extension Creation Date
+ Time
Group
Descriptors
Data Blocks
Block
Super- Bitmap
Block I-node I-node
Bitmap Table
I-Node Bitmap
Keep track of the usage status of I-Nodes of this block group (1 =
Occupied, 0 = Free )
I-Node table
An array of I-Nodes
Each I-Node can be access by a unique index
Contains only I-Nodes of this block group
[ CS2106 L11 - AY2021 S1 ]
41
Ext2: I-Node Structure (128 Bytes)
File type (regular, directory,
special, etc) + File permission
Mode (2)
User Id (2 bytes)
Owner Info (4) + Group Id (2 bytes)
91 F 4 Hi.c 39 F 8 lab5.pdf
74 D 3 sub 0
Name
Use a 0 I-Node
Name Length number to indicate
unused entry
Entry Type
Entry Size
I-Node Number
[ CS2106 L11 - AY2021 S1 ]
47
Ext2: Putting The Parts Together
File
1
Metadata 8 D 3 sub
2
15 Data
3 Block
4 Pointers
I-Node Disk Block 15
5
6
File
Metadata
7
23
8 6 F 4 file
…
… I-Node Disk Block 20
Directory
Proc P PCB … … Structure
0
Open( 1
filePath … …
Files Info
, …) … …
fd … …
File Data
File Descriptor
Table
Offset: …
E Status: …
I-node ptr: …
Ref count: File type: …
… … V File locks: …
I-node:
Ref count: …
Hard link:
Creates a directory entry in B which use the same I-Node number
XN
Can have different filename
Symbolic Link:
Creates a new file Y in B
Y contains the pathname of X
[ CS2106 L11 - AY2021 S1 ]
53
Hard/Symbolic Link Illustration
8 F 1 X
Directory Entries ...
for directory A/ ...
I-Node No. 8
8 F 1 Z
Directory Entries
for directory B/
Hard Link created for B/
File
Metadata Go to /A/X
123
13 F 1 Y Content of file Y
Directory Entries I-Node No. 13
for directory B/
Symbolic Link created for B/
[ CS2106 L11 - AY2021 S1 ]
54
Ext2 FS: Hard Link and Symbolic Link
Hard Link problems:
With multiple references of a I-Node
Maintain a I-Node reference count
Decremented for every deletion
Symbolic Link problems:
As only the pathname is stored, the link can be easily invalidated:
File name changes, file deletion etc
In Windows:
Official and 3rd party software to alleviate the problem
In Linux:
File allocation algorithm is more intelligent:
Files are allocated further apart
Free blocks near to existing data block are use if possible
Fragmentation is very low when the drive occupancy is < ~90%
[ CS2106 L11 - AY2021 S1 ]
59
Journaling
Keep additional information to recover from system crash
Basic Idea:
1. Write the information and / or the actual data into a separate log file
2. Perform the actual file operation