Lecture 16
Lecture 16
Lecture 16
• Linux filesystem
– Linux virtual filesystem (VFS) overview
• Common file model
– Superblock, inode, file, dentry
• Object-oriented
– Ext2 filesystem
• Disk data structures
– Superblock, block group, inodes
• Memory data structures
• Disk space management
1
The Common File Model
• VFS introduces a common file model to represent all
supported filesystems
• The common file model is specifically geared toward
Unix filesystems, all other filesystems must map their
own concepts into the common file model
– For example, FAT filesystems do not have inodes
• The main components of the common file model are
– superblock (information about mounted filesystem)
– inode (information about a specific file)
– file (information about an open file)
– dentry (information about directory entry)
i_sb
superblock
Storage
Device
inode
Hardlink
f_dentry d_inode
proc1 fd file
dentry dentry
fd file
proc2 Dentry cache
f_dentry
2
Object-Oriented Approach of VFS
• Each concept object has a set of defined operations
that can be performed on the object (i.e., methods)
• VFS provides certain generic implementations for
some operations
• Specific filesystem implementations must provide
implementation specific operations definitions
(i.e., inheritance and method overloading)
• There are no objects in C, though, so a table of
function pointers is used for each object to provide
its own version of the specific operations
3
The Ext2 Filesystem
• The first versions of Linux used the Minix
filesystem
• Linux later introduced the Extended Filesystem,
which as an improvement but offered
unsatisfactory performance
• The Second Extended Filesystem (Ext2) was
introduced in 1994
4
Ext2 Disk Data Structures
• The first block in all Ext2 partitions is always reserved
for the boot sector
• The remainder of the partition is split into block
groups
– All block groups are the same size and are stored sequentially on
the disk
– Block groups reduce file fragmentation, since the kernel tries to
keep the data blocks belonging to a file in the one block group if
possible
– The next slide illustrates the block group structure
5
Superblock Disk Data Structure
• The superblock is stored in an ext2_super_block
structure
• Contains
– Total number of inodes
– Filesystem size in blocks
– Free block counter
– Free inode counter
– Block size
– Blocks per group
– Inodes per group
– 128-bit filesystem identifier
– Mount counter
– etc.
6
Inode Table Disk Data Structure
• The inode table consists of a series of consecutive
blocks, each packed with inodes of the structure
ext2_inode
• All inodes are the same size (128 bytes in Linux 2.2)
• An inode contains
– File type and access rights
– Owner and group identifiers
– File length in bytes
– Number of data blocks in the file
– Various timestamp attributes
– An array of (usually 15) data block pointers
– etc.
7
Ext2 Memory Data Structures
• For efficiency, most information stored in disk
data structures is copied into RAM when the
filesystem is mounted
• Consider how often data structures change
– Whenever a new file is created
– Whenever a file needs more disk blocks
– Whenever access times need to be updated
• Some in-memory data structures differ from on-
disk data structures
8
Superblock Memory Data Structure
• An ext2_sb_info structure pointer is placed in
the VFS superblock data structure when an Ext2
filesystem is mounted
– This memory data structure contains most of the information
from the disk data structure for the Ext2 superblock
– Contains data related to mount state, options, etc.
– Also contains a block bitmap cache and an inode bitmap
cache
• It is not feasible to keep all disk bitmaps in memory, so it is
necessary to cache some and leave the rest on disk
• Uses a LRU algorithm over (usually) 8 cache entries
9
Ext2 Operations
• Ext2 superblock operations
– Essentially, specific implementations are provided for all
VFS operations (except 2)
• Ext2 inode, directory, and file operations
– Many operations have specific implementations, but in many
cases the generic VFS operations are sufficient
Creating a Filesystem
• Ext2 filesystems are created with the utility program
/sbin/mke2fs
– Default options: block 1024 bytes, one inode for each group of
4096 bytes, 5% reserved blocks
– It performs these actions
• Initializes superblock and group descriptors
• Creates a list of defective blocks
• For each block group, reserves all blocks needed to store superblock,
descriptors, bitmaps, and inode table
• Initializes all bitmaps to zero
• Initializes all inode tables
• Creates root directory
• Creates lost+found directory
• Updates inode bitmap and data bitmap of block group where the above
directories were added
• Groups defective blocks in the lost+found directory
10
Creating a Filesystem
• Consider a filesystem created on a 1.4MB floppy disk
– A single group descriptor is sufficient, 72 (5% of 1440)
reserved blocks, 360 inodes in 45 blocks
Block Content
0 Boot block
1 Superblock
2 Block containing single block group descriptor
3 Data block bitmap
4 Inode bitmap
5-49 Inode table (inodes up to 10 are reserved, inode 11 is lost+found)
50 Root directory
51 lost+found directory
52-62 Reserved blocks preallocated for lost+found directory
63-1439 Free block
11
Ext2 Managing Disk Space
• Allocating inodes
– Occurs in ext2_new_inode()
– Requires the parent inode and the mode (i.e., type) of the file
to be created
– If the inode is for a directory
• Forward search from the parent’s block group for a block
group with free space and a low directory-to-inode ratio
• If that fails, searches for block groups with above average free
space and chooses the one with the fewest directories
– If the inode is for any other type
• Forward search from the parent’s block group for a free inode
– Updates inode bitmap, decrements inode counters, puts the
inode into the superblock’s dirty list
12
Ext2 Managing Disk Space
• Data block addressing
– A non-empty regular file consists of a group of data blocks
• The blocks can be referred to by their relative position inside
the file (file block number) or their position inside the disk
partition (logical block number)
– Deriving the logical block number from an offset f inside a
file is a two-step process
• Derive from f the file block number
– This is easy, divide f by block size and round down to an integer
• Translate the file block number to the logical block number
– This is not so easy
13
Ext2 Managing Disk Space
• Data block addressing
(b/4) 2 2
(b/4)2++ (b/4)
(b/4)2++ (b/4)
2(b/4) (b/4)++ ...
2(b/4)++ (b/4)
(b/4)++ 12 ...
11 12 12
File 11 12
block
numbers
11 66 12
12 ...
... ...
...
Logical
block numbers
(i.e., the location
...
... ...
... ...
...
of the pointer values)
i_block
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14
14
Ext2 Managing Disk Space
• Releasing data blocks
– Occurs in ext2_truncate()
– Requires an inode
– Walks i_block to get all of the data blocks to free them
– Updates the various bookkeeping records
15