Textbook pages 501506 Lubomir Bic 2 Assignment Design and implement a simple file system using ldisk a file to emulate a physical disk Overall organization File System IO System ID: 598245
Download Presentation The PPT/PDF document "1 Project: File System" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.
Slide1
1
Project: File System
Textbook: pages 501-506
Lubomir
BicSlide2
2
Assignment
Design and implement a simple file system using
ldisk
(a file to emulate a physical disk)
Overall organization:
File
System
I/O System(ldisk)
user
driver
Input
cr
foo
op foowr 1 y 10sk 1 0rd 1 3
Output
foo
created
foo
opened 1
10 bytes written
position is 0
yyySlide3
3
I/O System
I/O system presents disk as a linear sequence of blocks:
ldisk
[L][B]
L is the number of logical blocks on
ldisk
B is the block length (in bytes)implement as byte arrayI/O system interface: read_block(int i, char *p) write_block(int i, char *p)each command reads or writes an entire block (B bytes)
memory area (*p) is also a byte arrayuse type casting or conversion to r/w integersFS can access ldisk using only these functions (no direct access to ldisk is allowed)Slide4
4
File System -- User Interface
create(
symbolic_file_name
)
destroy(symbolic_file_name)
open(symbolic_file_name
): return OFT indexclose(index)read(index, mem_area, count): return bytes readwrite(index, mem_area, count): return #bytes writtenlseek(index, pos)directory: return list of filesinit(file.txt): restore ldisk from file.txt or create new (if no file)save(file.txt): save ldisk to file.txtSlide5
5
Organization of the file system
directory
single flat list of all files
implemented as one regular file
use regular file operations: read, write,
lseekorganized as unsorted array of fixed-size slots
each slot contains: symbolic name (4 bytes max)index of descriptor (int)Slide6
6
Organization of the file system
file descriptors
kept in dedicated k disk blocks (
ldisk
[1..k])each contains: length (bytes), disk map
disk map: fixed list of max 3 disk blocks
descriptor 0 is reserved for directoryfree storage managementbit mapkept in dedicated disk block 0Slide7
7
Organization of the file system
…
bit map
file descriptors
Descriptor for FS directory
ldisk
[0
]...[k]
ldisk
[k+1]
...
Data blocks …
length
Each descriptor
Block numbers
File length in bytesSlide8
8
Create a file
cr
abc
find a free file descriptor
find a free directory entry
fill both entries
…free…
lenbit map
0
…
…
len
bit map
…
free
…
abc
…
i
…
i
0Slide9
9
Destroy a file
search directory to find file descriptor
remove directory entry
update bit map (if file was not empty)
free file descriptor
return status
0
……
len
bit map
abc
…
i
…Slide10
10
Open a file
search directory to find index of file descriptor (
i
)
allocate a free OFT entry (reuse deleted entries)
fill in current position (0) and file descriptor index (
i)read block 0 of file into the r/w buffer (read-ahead)return OFT index (j) (or return error)consider adding a file length field (to simplify checking)
OFT:
current position
index
. . .
. . .
j:
r/w buffer
i
0
block 0
. . .
. . .
j:Slide11
11Slide12
12
Close a file
write buffer to disk
update file length in descriptor
free OFT entry
return statusSlide13
13
Read an (open) file
compute position in the r/w buffer
copy from buffer to memory until
desired count or end of file is reached:
update current position, return status
end of buffer is reached
write the buffer to diskread the next blockcontinue copyingSlide14
14Slide15
15
Write a file
compute position in the r/w buffer
copy from memory into buffer until
desired count or end of file is reached:
update current pos, return status
end of buffer is reached
if block does not exist yet (file is expanding):allocate new block (search and update bit map)update file descriptor with new block numberwrite the buffer to disk blockcontinue copyingupdate file length in descriptorSlide16
16
Seek in a file
if the new position is not within the current block
write the buffer to disk
read the new block
set the current position to the new position
return statusSlide17
17
List the directory
read directory file
for each non-empty entry print file nameSlide18
18
The Bit Map (pg 217)
BM size: # of bits needed = # of
ldisk
blocks
represent bit map as an array of int
(32 bits each): BM[n]
How to set, reset, and search for bits in BM?prepare a mask array: MASK[32] diagonal contains “1”, all other fields are “0”use bit operations (bitwise or/and) to manipulate bitsSlide19
19
The Bit Map
MASK (assume 16 bits only)
0
10…
1
010…
2
0010…
3
00010…
…
…
15
0 … 01to set bit i of BM[j] to “1”:BM[j] = BM[j] | MASK[i]Slide20
20
The Bit Map
how to create MASK?
MASK[0] = 0x8000 (1000 0000 0000 0000)
MASK[1] = 0x4000 (0100 0000 0000 0000)
MASK[2] = 0x2000 (0010 0000 0000 0000)
MASK[3] = 0x1000 (0001 0000 0000 0000)
MASK[4] = 0x0800 (0000 1000 0000 0000)…MASK[15] = 0x0001 (0000 0000 0000 0001)another approach:MASK[15] = 1;MASK[i] = MASK[i+1] <<Slide21
21
The Bit Map
to set a bit to “0”:
create MASK2, where MASK2[
i
] = ~MASK[i]
e.g., 0010 0000 0000 0000
1101 1111 1111 1111set bit i of BM[j] to “0”: BM[j] = BM[j] & MASK2[i]Slide22
22
The Bit Map
to search for a bit equal to “0” in BM:
for (
i
=0; … /* search BM from the beginning
for (j=0; … /* check each bit in BM[i
] for “0”test = BM[i] & MASK[j])if (test == 0) then bit j of BM[i] is “0”; stop searchSlide23
Disk and FS Specificationsldisk: 64 blocksblock = 64 B =16 integerblock 0 holds bitmap: 64 bits (one per block) = 2 integersQ: how many blocks to reserve for descriptors?descriptor: 4 integers (file length plus 3 block #s)number of descriptors depends on directory sizeeach directory entry: 2 integersfile name: maximum 4 chars, no extension (=1 int)descriptor index: 1 integerdirectory size = 3 blocks = 3*64 B = 48 integers = 24 entries
24 descriptors = 24*4 = 96 integers = 6 blocks23Slide24
Disk and FS Specificationsldisk can be saved into a text file at any point with the sv commandldisk can be restored from a previously saved text filea new empty ldisk is created if no saved file is givenit consists of 64 blocksblock 0 contains the initial bitmap
next 6 blocks contain the descriptor slotsslot 0 describes the empty directorydirectory is opened automatically with init (OFT index = 0)
OFT has 4 entries: directory plus up to 3 other open files
all files (including directory) must close with
sv
command
24Slide25
25
Testing shell (driver)
develop testing shell:
repeatedly accept command (e.g.
cr
abc) from a file
invoke corresponding FS function (e.g. create(abc))write status/data to an output file(e.g. abc created or error)project will be tested using an input file containing multiple test sequences, each starting with the command in (initialize or restore disk)Slide26
26
Shell commands and Output
cr
<name>
Output
: <name> created
de <name>
Output: <name> destroyedop <name>Output: <name> opened <index>
cl <index>Output: <index> closedrd <index> <count>Output: <xx...x>wr <index> <char> <count>Output: <count> bytes writtenSlide27
27
Shell commands and Output
sk
<index> <
pos
>
Output
: position is <pos>drOutput: <file0> <file1> … <fileN>
in <disk_cont.txt>if file does not exist, output: disk initializedif file does exist, output: disk restoredsv <disk_cont.txt>Output:
disk savedIf any command fails, output: errorSlide28
Sample InteractionInputincr fooop foowr 1 x 60wr 1 y 10sk 1 55rd 1 10drsv
dsk.txtin
dsk.txt
op foo
rd
1 3
cr
fooOutputdisk initializedfoo createdfoo opened 160 bytes written10 bytes writtenposition is 55xxxxxyyyyyfoo
disk saveddisk restoredfoo opened 1xxxerror28Slide29
29
Summary of tasks
d
esign and implement I/O interface (
ldisk
array plus read/write operations)
design and implement FS using only read/write on
ldiskdevelop test/presentation shellerror checks on all commands submit documentationschedule testing