15 213 The course that gives CMU its Zip Files Oct 28 2008 Topics lecture 18 ppt Mapping file offsets to disk blocks File system buffering and you The directory hierarchy Announcements Exam Thursday 2 style like exam 1 in class open book notes no electronics class website has details and old exams 15 213 F 08 Reminder Unix I O Key Features Elegant mapping of files to devices allows kernel to export simple interface called Unix I O Important idea All input and output is handled in a consistent and uniform way Basic Unix I O operations system calls Opening and closing files Reading and writing a file open and close read and write Changing the current file position seek indicates next offset into file to read or write Lseek B0 B1 3 From lecture 13 ppt Bk 1 Bk Bk 1 Current File Position k 15 213 F 08 Reminder Disk Structure Read Write Head Arm Upper Surface Platter Lower Surface Cylinder Track Sector 4 From lecture 17 ppt Actuator 15 213 F 08 Reminder Disk storage as array of blocks 5 6 7 12 23 OS s view of storage device as exposed by SCSI or IDE ATA protocols Common logical block size 512 bytes Number of blocks device capacity block size Common OS to storage requests defined by few fields 5 R W block of blocks memory source dest From lecture 17 ppt 15 213 F 08 Mapping file offsets to disk LBNs Issue in question need to keep track of which LBNs hold which file data Most trivial mapping just remember start location then keep entire file in contiguous LBNs what happens when it grows alternately include a next pointer in each block how does one find location of a particular offset Most common approach block lists an array with one LBN per block in the file Note file block size can exceed one logical disk block 6 so groups of logical blocks get treated as a unit by file system e g 8KB 16 disk blocks of 512 bytes each 15 213 F 08 A common approach to recording a block list Direct Block 1 Direct Block 2 Direct Block 12 Indirect Block Data lbn 576 Data lbn 344 Data lbn 968 Data Block 13 Data lbn 632 Data lbn 1944 Data Block N Data lbn 480 Data Block 14 Double Indirect Block Indirect Block 1 Data Block N 1 Data lbn 96 Data lbn 176 Data lbn 72 15 213 F 08 Data Block N 2 Indirect Block 2 Data Block Q 1 7 Other per file information must also be stored somewhere Examples 8 length of file owner access permissions last modification time 15 213 F 08 Reminder File Metadata Metadata is data about data in this case file data Per file metadata maintained by kernel accessed by users with the stat and fstat functions Metadata returned by the stat and fstat functions struct stat dev t st dev device ino t st ino inode mode t st mode protection and file type nlink t st nlink number of hard links uid t st uid user ID of owner gid t st gid group ID of owner dev t st rdev device type if inode device off t st size total size in bytes unsigned long st blksize blocksize for filesystem I O unsigned long st blocks number of blocks allocated time t st atime time of last access time t st mtime time of last modification time t st ctime time of last change 15 213 F 08 9 From lecture 13 ppt Other per file information must also be stored somewhere Examples length of file owner access permissions last modification time Usually kept together with the block list 10 In a structure called an inode 15 213 F 08 File block allocation Two issues Keep track of which space is available When a new block is needed pick one of the free ones Malloc like solution free list maintain a linked list of free blocks grab block from this list when a new block is needed usually the list is used as a stack while simple this approach rarely yields good performance 11 using space in unused blocks to store the pointers why 15 213 F 08 File block allocation cont Most common approach a bitmap Use a large array of bits with one per allocatable unit Scan the array for a free setting when we need a block note we don t have to just take first free block in array we can look in particular regions or for particular patterns In choosing an allocation try to provide locality 12 one value says free and the other says in use e g second block should be right after first e g first block should be near inode 15 213 F 08 Reminder Reading Files Reading a file copies bytes from the current file position to memory and then updates file position char buf 512 int fd file descriptor int nbytes number of bytes read Open file fd Then read up to 512 bytes from file fd if nbytes read fd buf sizeof buf 0 perror read exit 1 Returns number of bytes read from file fd into buf 13 Return type ssize t is signed integer nbytes 0 indicates that an error occurred short counts nbytes sizeof buf are possible and 15 213 F 08 are not errors Managing file data in kernel buffers Staging area between disk and processes 14 15 213 F 08 Block based file buffer management user read fd buffer cnt buffer cnt logical file system buffers 1 0 1 disk 15 0 90256 2 3 2 51876 3 11948 32216 15 213 F 08 Note large I Os are more efficient Recall disk performance is location dependent milliseconds to position read write head microseconds to read next sector usually next LBN Small read s write s sometimes perform very poorly Process 1 read s 4KB from file 1 and waits for disk I O Process 2 read s 4KB from file 2 and waits for disk I O Process 1 continues and read s next 4KB from file 1 Process 2 continues and read s next 4KB from file 2 Result random like performance instead of sequential 16 bandwidth achieved would double with 8KB reads 15 213 F 08 Naturally OS keeps a buffer cache Disk I O costs milliseconds as compared to microseconds for in memory access so cache in kernel buffers from previous read s Each non free buffer often kept on a number of lists overflow list associated with hash index Least Recently Used list or other importance tracking lists so that all buffers associated with a file can be found quickly dirty block list 17 so that good choices can be made for replacement vnode list so that it can be found during read so that dirty buffers can be propagated to disk when desired 15 213 F 08 Managing file data in the kernel buffers Staging area between disk and processes Two parts of each buffer header describing controls and buffer containing data hash links free list links lru …
View Full Document