Project: File System Textbook: pages 501-506 Lubomir Bic 1 Assignment • Design and implement a simple file system using ldisk (a file to emulate a physical disk) • Overall organization: user driver • Input cr foo op foo wr 1 y 10 sk 1 0 rd 1 3 File System I/O System (ldisk) • Output foo created foo opened 1 10 bytes written position is 0 yyy 2 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) 3 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 4 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) 5 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 6 Organization of the file system ldisk[0]...[k] file descriptors … bit map ldisk[k+1] ... Data blocks … Descriptor for FS directory Each descriptor Block numbers length File length in bytes 7 Create a file • cr abc – find a free file descriptor – find a free directory entry – fill both entries 0 bit map len i … free … bit map len … free 0 … abc i … … … … 8 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 bit map len … 0 … abc i … … 9 Open a file OFT: current position r/w buffer index ... j: ... • • • • • • ... j: block 0 ... 0 i 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) 10 11 Close a file • • • • write buffer to disk update file length in descriptor free OFT entry return status 12 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 13 14 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 15 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 16 List the directory • read directory file • for each non-empty entry print file name 17 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 18 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] 19 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] << 20 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] 21 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 22 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 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) 25 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 26 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 27 Sample Interaction • Input • Output in cr op wr wr sk rd dr sv in op rd cr disk initialized foo created foo opened 1 60 bytes written 10 bytes written position is 55 xxxxxyyyyy foo disk saved disk restored foo opened 1 xxx error foo foo 1 x 60 1 y 10 1 55 1 10 dsk.txt dsk.txt foo 1 3 foo 28 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 29
© Copyright 2024