computer systems lab
play

Computer Systems Lab Matteo Corti Informatikdienste, ETH Z urich - PowerPoint PPT Presentation

Computer Systems Lab Matteo Corti Informatikdienste, ETH Z urich 2007-04-30 Matteo Corti (Informatikdienste, ETH Z urich) Computer Systems Lab 2007-04-30 1 / 23 A simple disk driver bios.c provides a simple disk-driver to access FAT


  1. Computer Systems Lab Matteo Corti Informatikdienste, ETH Z¨ urich 2007-04-30 Matteo Corti (Informatikdienste, ETH Z¨ urich) Computer Systems Lab 2007-04-30 1 / 23

  2. A simple disk driver bios.c provides a simple disk-driver to access FAT disk images using 512-bytes blocks. int bios init(char * name) initializes the disk driver with the given image. void bios shutdown() closes the image file void bios read(int number, char * block) reads a 512 bytes sector from the disk image void bios write(int number, char * block) writes a 512 bytes sector to the disk image. Matteo Corti (Informatikdienste, ETH Z¨ urich) Computer Systems Lab 2007-04-30 2 / 23

  3. Goal The goal of this lab is to implement a minimal FAT driver with the following simplified API: int fs open(char * path) opens a file for reading specified by path for reading and writing and return a file descriptor int fs read(int fd, char * buff, int len) reads len bytes from fd , puts them into buff and returns the actual number of read bytes int fs write(inf fd, char * buff, int len) writes len bytes from buff to the file specified by the file descriptor fd and returns the actual number of written bytes Matteo Corti (Informatikdienste, ETH Z¨ urich) Computer Systems Lab 2007-04-30 3 / 23

  4. Goal void fs close(int fd) closes the file specified by the file descriptor fd int fs creat(char * path) creates a new file specified by path and opens it for writing Please note that this simple API does not allow to specify the read position. A file must be sequentially read and written. In addition we do not specify a way to delete a file. Matteo Corti (Informatikdienste, ETH Z¨ urich) Computer Systems Lab 2007-04-30 4 / 23

  5. File descriptors Your filesystem driver will deliver to user a file descriptor : a key to uniquely identify a file. The file descriptor will be then used to identify the kernel (or driver) data structures relative to the given file. user program fd = open("file"); user space fs driver fd *internal_data file pos buffer ... disk driver Matteo Corti (Informatikdienste, ETH Z¨ urich) Computer Systems Lab 2007-04-30 5 / 23

  6. Steps Divide you work in steps: 1 Understand the filesystem layout 2 Read the boot sector and the structure of the FAT partition you are handling (i.e., number of clusters, number of FATS and position of the root directory). 3 Read the directory structure: parse the directory entries to located the starting cluster of the handled file. 4 Access files: read and write the requested clusters Test your work after each step! Matteo Corti (Informatikdienste, ETH Z¨ urich) Computer Systems Lab 2007-04-30 6 / 23

  7. Testing We provide several images for testing: • each image is FAT12 and contains only files and directories named with the 8.3 schema • For each image we provide a tarball with the image content • You can inspect the image content with: > hexdump -C partition.img ... 00008000 74 65 73 74 20 20 20 20 20 20 20 08 00 00 7d 72 |test ...}r| 00008010 b2 34 b2 34 00 00 7d 72 b2 34 00 00 00 00 00 00 |.4.4..}r.4......| 00008020 41 66 00 69 00 6c 00 65 00 00 00 0f 00 bc ff ff |Af.i.l.e........| 00008030 ff ff ff ff ff ff ff ff ff ff 00 00 ff ff ff ff |................| 00008040 46 49 4c 45 20 20 20 20 20 20 20 20 00 64 7d 72 |FILE .d}r| 00008050 b2 34 b2 34 00 00 7d 72 b2 34 03 00 0d 00 00 00 |.4.4..}r.4......| ... Matteo Corti (Informatikdienste, ETH Z¨ urich) Computer Systems Lab 2007-04-30 7 / 23

  8. Create test images Example for Linux: # create an empty file dd if=/dev/zero of=/tmp/disk.img \ count=<number of blocks> # format the disk image mkdosfs -f 2 -F 12 -r 512 -S 512 -n disk \ /tmp/disk.img # mount the disk image sudo mount -t vfat -o rw,loop=/dev/loop0 \ test.img /mnt # copy the files sudo cp -vr * /mnt/ # unmount the disk image sudo umount /mnt Matteo Corti (Informatikdienste, ETH Z¨ urich) Computer Systems Lab 2007-04-30 8 / 23

  9. Hints • The first cluster is cluster 2 (clusters 0 and 1 don’t exist) • Names (8+3) are padded with spaces: "EXAMPLE "."TXT" • FAT 12: two 12 bit entries are packed into three bytes: uv.wx.yz = ⇒ xuv , yzw • Root dir position: reserved + number of FATs · sectors per FAT • Root dir length: rootdir − entries · 32 sector length • Even if the file has a 8.3 name a long name entry is usually created. You can recognize (and skip) this directory entries by their attribute: 0x0F . • FAT type: • if there are less than 4085 clusters FAT12 • if less than 65525 FAT16 • otherwise FAT32 We provide FAT12 images Matteo Corti (Informatikdienste, ETH Z¨ urich) Computer Systems Lab 2007-04-30 9 / 23

  10. Hints: packed structures Tell the compiler not to word align structures. GCC example: struct fat_boot_sector { __u8 ignored[3]; /* Boot strap short or near jump */ __u8 system_id[8]; /* Name - can be used to special case * * partition manager volumes */ __u16 sector_size; /* bytes per logical sector */ __u8 sec_per_clus; /* sectors/cluster */ __u16 reserved; /* reserved sectors */ __u8 fats; /* number of FATs */ __u16 dir_entries; /* root directory entries */ __u16 sectors; /* number of sectors */ __u8 media; /* media code */ __u16 fat_length; /* sectors/FAT */ __u16 secs_track; /* sectors per track */ __u16 heads; /* number of heads */ __u32 hidden; /* hidden sectors (unused) */ __u32 total_sect; /* number of sectors (if sectors = 0) */ } __attribute__((packed)) ; Matteo Corti (Informatikdienste, ETH Z¨ urich) Computer Systems Lab 2007-04-30 10 / 23

  11. Hints: driver.h We provide: • Definitions for the boot sector and directory entries: • struct fat_boot_sector { __u8 ignored[3]; __u8 system_id[8]; __u16 sector_size; __u8 sec_per_clus; • struct dos_dir_entry { __u8 name[8], ext[3]; __u8 attr; __u8 lcase; __u8 ctime_cs; • Constants for attributes: FILE ATTR RONLY , FILE ATTR HIDDEN , . . . • Disk driver ( bios.c ) Matteo Corti (Informatikdienste, ETH Z¨ urich) Computer Systems Lab 2007-04-30 11 / 23

  12. Malloc lab: Sample solutions Red-black tree: 57 (util) + 40 (thru) = 97/100 • Best-fit algorithm • Free blocks stored in a red-black tree (sorted by size) • Freed blocks are temporarily stored in a list ( blog ) and put in the tree before a malloc . • Blocks have a header and a footer Buddy (J¨ urg Billeter): 57 (util) + 40 (thru) = 97/100 • 128 free lists: one unsorted (similar to the blog ), one for large blocks • Blocks have a header and a footer • Best-fit algorithm Matteo Corti (Informatikdienste, ETH Z¨ urich) Computer Systems Lab 2007-04-30 12 / 23

  13. Red-Black Trees (Beyer 1972) • Self-balancing binary search tree 11 • Search, insert and delete in O ( log n ) • Good worst-case run time 8 42 • Properties: 1 10 12 100 • Each node is either red or black • The root is black • All leaves are black nil nil nil 150 • Children of a red node are black • All paths from any node to its leaf nodes nil contain the same number of black nodes. • The longest path (root–leaf) is no more than twice the shortest path. R. Bayer Symmetric binary B-Trees: Data structure and maintenance algorithms Acta Informatica, 1(4):290–306, Springer, December 1972 Matteo Corti (Informatikdienste, ETH Z¨ urich) Computer Systems Lab 2007-04-30 13 / 23

  14. Red-Black Tree — Algorithm free(b) if next block is free then coalesce end if if previous block is free then coalesce end if put b in the blob Matteo Corti (Informatikdienste, ETH Z¨ urich) Computer Systems Lab 2007-04-30 14 / 23

  15. Red-Black Tree — Algorithm malloc(size) insert all the nodes of the blob into the tree search a block ( b ) in the free tree if no free blocks are available then if the heap can be grown then grow the heap and return the newly allocated block else error end if else if the block can be split then split the block put the remainder in the blob end if return b end if Matteo Corti (Informatikdienste, ETH Z¨ urich) Computer Systems Lab 2007-04-30 15 / 23

  16. Red-Black Tree — Algorithm realloc(b, size) if b = NULL then return malloc(size) end if if size = 0 then free(b) return end if if we can expand to the next block 1 then coalesce the next block return new block end if b 2 = malloc(size) copy b to b 2 free(b) 1 This includes growing the heap if b is the last block Matteo Corti (Informatikdienste, ETH Z¨ urich) Computer Systems Lab 2007-04-30 16 / 23

  17. Buddy — Algorithm free(b) if next block is free then coalesce end if if previous block is free then coalesce end if put b in the unsorted list Matteo Corti (Informatikdienste, ETH Z¨ urich) Computer Systems Lab 2007-04-30 17 / 23

  18. Buddy — Algorithm malloc(size) sort the unsorted list search a block ( b ) in the free lists if no free blocks are available then if the heap can be grown then grow the heap and return the newly allocated block else error end if else if the block can be split then split the block put the remainder in the corresponding list end if return b end if Matteo Corti (Informatikdienste, ETH Z¨ urich) Computer Systems Lab 2007-04-30 18 / 23

  19. Buddy — Algorithm realloc(b, size) if b = NULL then return malloc(size) end if if size = 0 then free(b) return end if if we can expand to the previous or next block then coalesce the next block return new block end if b 2 = malloc(size) copy b to b 2 free(b) Matteo Corti (Informatikdienste, ETH Z¨ urich) Computer Systems Lab 2007-04-30 19 / 23

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend