1 Project: File System Textbook: pages 501-506 Lubomir Bic.

Post on 24-Dec-2015

221 views 2 download

transcript

1

Project: File System

Textbook: pages 501-506

Lubomir Bic

2

Assignment• Design and implement a simple file system using ldisk (a

file to emulate a physical disk)• Overall organization:

FileSystem

I/O System(ldisk)

user driver

• Inputcr fooop foowr 1 y 10sk 1 0rd 1 3

• Outputfoo createdfoo opened 110 bytes writtenposition is 0yyy

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 array• I/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 array• use type casting or conversion to r/w integers

• FS can access ldisk using only these functions (no direct access to ldisk is allowed)

4

File System -- User Interface• create(symbolic_file_name)• destroy(symbolic_file_name)• open(symbolic_file_name): return OFT index• close(index)• read(index, mem_area, count): return bytes read• write(index, mem_area, count): return #bytes written• lseek(index, pos)• directory: return list of files• init(file.txt): restore ldisk from file.txt or create new (if no file)• save(file.txt): save ldisk to file.txt

5

Organization of the file system• directory

– single flat list of all files– implemented as one regular file

• use regular file operations: read, write, lseek– organized as unsorted array of fixed-size slots– each slot contains:

• symbolic name (4 bytes max)• index of descriptor (int)

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 directory

• free storage management– bit map– kept in dedicated disk block 0

7

Organization of the file system

…bit map

file descriptors

Descriptor for FS directory

ldisk[0]...[k] ldisk[k+1] ...

Data blocks …

length

Each descriptorBlock numbers

File length in bytes

8

Create a file• cr abc

– find a free file descriptor– find a free directory entry– fill both entries

…free…lenbit map

0 ……lenbit map

…free…

abc

…i…

i0

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 ……lenbit map

abc

…i…

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

i0block 0

. . .

. . .

j:

11

12

Close a file• write buffer to disk• update file length in descriptor• free OFT entry• return status

13

Read an (open) file• compute position in the r/w buffer• copy from buffer to memory until

1. desired count or end of file is reached: • update current position, return status

2. end of buffer is reached• write the buffer to disk• read the next block• continue copying

14

15

Write a file• compute position in the r/w buffer• copy from memory into buffer until

1. desired count or end of file is reached: • update current pos, return status

2. 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 number

• write the buffer to disk block• continue copying

– update file length in descriptor

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 status

17

List the directory• read directory file• for each non-empty entry print file name

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 bits

19

The Bit Map• MASK (assume 16 bits only)

0 10…

1 010…

2 0010…

3 00010…

… …

15 0 … 01

• to set bit i of BM[j] to “1”:

BM[j] = BM[j] | MASK[i]

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] <<

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 1111

• set bit i of BM[j] to “0”:

BM[j] = BM[j] & MASK2[i]

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 search

Disk and FS Specifications• ldisk: 64 blocks• block = 64 B =16 integer• block 0 holds bitmap: 64 bits (one per block) = 2 integers• Q: how many blocks to reserve for descriptors?• descriptor: 4 integers (file length plus 3 block #s)• number of descriptors depends on directory size

– each directory entry: 2 integers• file name: maximum 4 chars, no extension (=1 int)• descriptor index: 1 integer

– directory size = 3 blocks = 3*64 B = 48 integers = 24 entries

• 24 descriptors = 24*4 = 96 integers = 6 blocks

23

Disk and FS Specifications• ldisk can be saved into a text file at any point with the sv

command• ldisk can be restored from a previously saved text file• a new empty ldisk is created if no saved file is given

– it consists of 64 blocks– block 0 contains the initial bitmap– next 6 blocks contain the descriptor slots– slot 0 describes the empty directory

• directory 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

24

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)

26

Shell commands and Output• cr <name>

– Output: <name> created• de <name>

– Output: <name> destroyed• op <name>

– Output: <name> opened <index>• cl <index>

– Output: <index> closed• rd <index> <count>

– Output: <xx...x>• wr <index> <char> <count>

– Output: <count> bytes written

27

Shell commands and Output• sk <index> <pos>

– Output: position is <pos>• dr

– Output: <file0> <file1> … <fileN>

• in <disk_cont.txt>

– if file does not exist, output: disk initialized

– if file does exist, output: disk restored

• sv <disk_cont.txt>– Output: disk saved

• If any command fails, output: error

Sample Interaction• Inputincr fooop foowr 1 x 60wr 1 y 10sk 1 55rd 1 10drsv dsk.txtin dsk.txtop foord 1 3cr foo

• Outputdisk initializedfoo createdfoo opened 160 bytes written10 bytes writtenposition is 55xxxxxyyyyyfoodisk saveddisk restoredfoo opened 1xxxerror

28

29

Summary of tasks• design and implement I/O interface (ldisk array plus

read/write operations)• design and implement FS using only read/write on ldisk• develop test/presentation shell• error checks on all commands • submit documentation• schedule testing