19CS2106S Lecture Notes File System and Buffer Cache Allocation Algorithms Session - 4
19CS2106S Lecture Notes File System and Buffer Cache Allocation Algorithms Session - 4
Session plan:
Session Outcome: Understand and Explore the Design of File Subsystem and Buffer Cache Algorithms.
Time Topic BTL Teaching - Learning Active Learning
(min) Methods Methods
10 Xv6 functions: bget, brelse. Design and Implementation 3 Lecture & Discussion LTC
of bio.c
References
1. Maurice J. Bach, The Design of The Unix Operating System, 2013 PHI Publishing. CH 2.2.1 Page No [22 – 24, 44, 46]
2. Frans Kaashoek, Robert Morris, and Russ Cox, The xv6 source code booklet (draft) (revision 11).
https://pdos.csail.mit.edu/6.828/2018/xv6/xv6-rev11.pdf Sheet No [44]
File System:
A file system is a logical collection of files on a partition or disk. A partition is a container for information and can span an entire
hard drive if desired.
Your hard drive can have various partitions which usually contain only one file system, such as one file system housing the /file
system or another containing the /home file system.
One file system per partition allows for the logical maintenance and management of differing file systems.
Everything in Unix is considered to be a file, including physical devices such as DVD-ROMs, USB devices, and floppy drives.
Directory Structure:
Unix uses a hierarchical file system structure, much like an upside-down tree, with root (/) at the base of the file system and all
other directories spreading from there.
A Unix file system is a collection of files and directories that has the following properties −
• It has a root directory (/) that contains other files and directories.
• Each file or directory is uniquely identified by its name, the directory in which it resides, and a unique identifier,
typically called an inode.
• By convention, the root directory has an inode number of 2 and the lost+found directory has an inode number of 3.
Inode numbers 0 and 1 are not used. File inode numbers can be seen by specifying the -i option to ls command.
• It is self-contained. There are no dependencies between one filesystem and another.
File System Layout:
Algorithm :Getblk
When searching for a block in the buffer pool by its device-block number combination, the kernel finds the hash queue that
should contain the block.
It searches the hash queue, following the linked list of buffers until (in the first scenario) it finds the buffer whose device and
block number match those for which it is searching. The kernel checks that the buffer is free and, if so, marks the buffer "busy"
so that other processes cannot access it.
The kernel then removes the buffer from the free list, because a buffer cannot be both busy and on the free list. If other
processes attempt to access the block while the buffer is busy, they sleep until the buffer is released. Figure 2.4 depicts the first
scenario, where the kernel searches for block 4 on the hash queue marked "blkno 0 mod 4." Finding the buffer, the kernel
removes it from the free list, leaving blocks 5 and 28 adjacent on the free list.
In the second scenario in algorithm getblk, the kernel searches the hash queue that should contain the block but fails to find it
there. Since the block cannot be on another hash queue because it cannot "hash" elsewhere, it is not in the buffer cache.
So the kernel removes the first buffer from the free list instead; that buffer had been allocated to another disk block and is also
on a hash queue. If the buffer has not been marked for a delayed write, the kernel marks the buffer busy, removes it from the
hash queue where it currently resides, reassigns the buffer header's device and block number to that of the disk block for which
the process is searching, and places the buffer on the correct hash queue .
The kernel uses the buffer but has no record that the buffer formerly contained data for another disk block. A process searching
for the old disk block will not find it in the pool and will have to allocate a new buffer for it from the free list. In the third
scenario in algorithm getblk, the kernel also has to allocate a buffer from the free list. However, it discovers that the buffer it
removes from the free list has been marked for "delayed write," so it must write the contents of the buffer to disk before using
the buffer.
The kernel starts an asynchronous write to disk and tries to allocate another buffer from the free list. When the asynchronous
write completes, the kernel releases the buffer and places it at the head of the free list. The buffer had started at the end of the
free list and had traveled to the head of the free list.
// Look through buffer cache for block on device dev.
// If not found, allocate a buffer.
// In either case, return locked buffer.
static struct buf*
bget(uint dev, uint blockno)
{
struct buf *b;
acquire(&bcache.lock);