Unix ch03 03
Unix ch03 03
2001.3.9
SS 1
File system
Kernel level
Buffer cache
block character Device driver
ipc schedular mm
Hardware control
Hardware level
2001.3.9
hardware
Figure 2.1 block diagram of the System Kernel
SS 1
Buffer Headers
Buffer
Memory array
Contains data from disk
Buffer header
Identifies the buffer Has
Device number Logical file system num not a physical device num Block number Pointer to a data array for the buffer
States of buffer
Locked Valid Delayed write Reading or writing Waiting for free
ptr to previous buf on hash queue ptr to previous buf on free list
Buffer header
Dev num Block num status
ptr to data area ptr to next buf on the hash queue ptr to next buf on free list
2001.3.9
SS 1
When take
Choose first element
Hash Queue
Separate queues : each Doubly linked list When insert
Hashed as a function device num, block num
2001.3.9 SS 1
else{ if ( there are no buffers on free list) { /*scenario 4*/ sleep(event any buffer becomes free) continue; } remove buffer from free list; if(buffer marked for delayed write) { /*scenario 3*/ asynchronous write buffer to disk; continue; } /*scenario 2*/ remove buffer from old hash queue; put buffer onto new hash queue; return buffer; } }}
SS 1
1st Scenario
Block is in hash queue, not busy
Choose that buffer
Blkno 0 mod 4
Blkno 1 mod 4 Blkno 2 mod 4 Blkno 3 mod 4
28
17 98 3
4
5 50 35
64
97 10 99
2001.3.9
SS 1
1st Scenario
After allocating
Blkno 0 mod 4
Blkno 1 mod 4 Blkno 2 mod 4 Blkno 3 mod 4
28
17 98 3
4
5 50 35
64
97 10 99
2001.3.9
SS 1
2nd Scenario
Not in the hash queue and exist free buff.
Choose one buffer in front of free list
Blkno 0 mod 4
Blkno 1 mod 4 Blkno 2 mod 4 Blkno 3 mod 4
28
17 98 3
4
5 50 35
64
97 10 99
2001.3.9
SS 1
2nd Scenario
After allocating
Blkno 0 mod 4
Blkno 1 mod 4 Blkno 2 mod 4 Blkno 3 mod 4
28
17 98
4
5 50 35
64
97 10 99 18
Freelist header (b) Remove first block from free list, assign to 18
2001.3.9 SS 1
3rd Scenario
Not in the hash queue and there exists delayed write buffer in the front of free list
Write delayed buffer async. and choose next
Blkno 0 mod 4 Blkno 1 mod 4 Blkno 2 mod 4 Blkno 3 mod 4 28 17 98 3 4 5 64 97 10 99
delay
50 35
delay
Freelist header
2001.3.9
(a) Search for block 18, delayed write blocks on free list
SS 1
3rd Scenario
After allocating
Blkno 0 mod 4 28 64
Blkno 1 mod 4
Blkno 2 mod 4 Blkno 3 mod 4
17
98 3
writing
50 35
97
10 99 18
writing
4th Scenario
Not in the hash queue and no free buffer
Wait until any buffer free and re-do
Blkno 0 mod 4
Blkno 1 mod 4 Blkno 2 mod 4 Blkno 3 mod 4
28
17 98 3
4
5 50 35
64
97 10 99
Freelist header (a) Search for block 18, empty free list
2001.3.9
SS 1
4th Scenario
Process A
Cannot find block b on the hash queue
No buffer on free list Sleep
Process B
SS 1
4th Scenario
What to remind
When process release a buffer wake all process waiting any buffer cache
2001.3.9
SS 1
5th Scenario
Block is in hash queue, but busy
Wait until usable and re-do
Blkno 0 mod 4
Blkno 1 mod 4 Blkno 2 mod 4 Blkno 3 mod 4
28
17 98 3
4
5 50 35
64
97 10 99
busy
2001.3.9
SS 1
5th Scenario
Process A
Allocate buffer to block b Lock buffer Initiate I/O Sleep until I/O done I/O done, wake up brelse(): wake up others Find block b on hash queue Buffer locked, sleep
Process B
Process C
Sleep waiting for any free buffer ( scenario 4 ) Get buffer previously assigned to block b reassign buffer to buffer b
time
2001.3.9 SS 1
Else
Calls disk driver to schedule a read request Sleep When I/O complete, disk controller interrupts the Process Disk interrupt handler awakens the sleeping process Now process can use wanted data
SS 1
2001.3.9
2001.3.9
SS 1
getblk()
ll_rw_block()
RAM
Buffer cache
DISK
Figure 13-3 block device handler architecture for buffer I/O operation 2001.3.9 SS 1 in Understanding the Linux Kernel
Use breada()
Algorithm
2001.3.9
Algorithm breada Input: (1) file system block number for immediate read (2) file system block number for asynchronous read Output: buffer containing data for immediate read { if (first block not in cache){ get buffer for first block(algorithm getblk); if(buffer data not valid) initiate disk read; } SS 1
2001.3.9
SS 1
Asynchronous write
the kernel starts the disk write. The kernel release the buffer when the I/O completes
Delayed write
The kernel put off the physical write to disk until buffer reallocated Look Scenario 3
Relese
Use brelse()
2001.3.9
SS 1
2001.3.9
SS 1
Disadvantages
Can be vulnerable to crashes
When delayed write
2001.3.9
SS 1
manipulate
release
2001.3.9
SS 1
Reference
LINUX KERNEL INTERNALS
Beck, Bohme, Dziadzka, Kunitz, Magnus, Verworner
In linux
Buffer_head : include/linux/fs.h Bread : fs/buffer.c Brelse : include/linux/fs.h
2001.3.9
SS 1