TDDB68 Concurrent programming and operating systems File System Implementation File-System Structure File-System Implementation Directory Implementation Allocation Methods Free-Space Management Recovery [SGG7/8/9] Chapter 11 Copyright Notice: The lecture notes are mainly based on Silberschatz’s, Galvin’s and Gagne’s book (“Operating System Concepts”, 7th ed., Wiley, 2005). No part of the lecture notes may be reproduced in any form, due to the copyrights reserved by Wiley. These lecture notes should only be used for internal teaching purposes at the Linköping University. Christoph Kessler, IDA, Linköpings universitet. TDDB68, C. Kessler, IDA, Linköpings universitet. File-System Structure 11.2 Silberschatz, Galvin and Gagne ©2005 In-Memory File System Structures File system resides on File API: secondary storage (disks) File system organized into layers: File control block (FCB) – (at the logical FS layer) storage structure consisting of information about a file filenames, directories, attributes, access... creat Logical block addresses (1D array of blocks) (a) Creating a new file Physical block addresses on disk (cylinder, sector,...) read/write block commands Layered File System TDDB68, C. Kessler, IDA, Linköpings universitet. 11.3 Directory Implementation Name File1 File2 File3 … Silberschatz, Galvin and Gagne ©2005 (b) Reading an open file TDDB68, C. Kessler, IDA, Linköpings universitet. 11.4 Silberschatz, Galvin and Gagne ©2005 Allocation Methods Directory An allocation method refers to File pointer F1 F2 F3 … how disk blocks are allocated for files Files F1 F2 F3 F4 Fn Directories contain pointers to files Linear list of file names with pointer to the data blocks. Contiguous allocation Linked allocation Indexed allocation simple to program time-consuming to execute Possible ways to speed up lookups Caching lookups Sorting directory entries Hash table structure TDDB68, C. Kessler, IDA, Linköpings universitet. 11.6 Silberschatz, Galvin and Gagne ©2005 TDDB68, C. Kessler, IDA, Linköpings universitet. 11.7 Silberschatz, Galvin and Gagne ©2005 Contiguous Allocation Linked Allocation Each file occupies a set of Each file is a linked list of disk blocks: contiguous blocks on the disk blocks may be scattered Simple – need only starting location (block index) and length (# of blocks) block = next-pointer Random access Wasteful of space Simple – (dynamic storageallocation problem) need only starting address Free-space management Files cannot grow easily Works well on CD-ROM TDDB68, C. Kessler, IDA, Linköpings universitet. No external fragmentation Silberschatz, Galvin and Gagne ©2005 11.8 No random access TDDB68, C. Kessler, IDA, Linköpings universitet. 11.10 Silberschatz, Galvin and Gagne ©2005 File-Allocation Table (FAT) Indexed Allocation File-allocation table (FAT) – disk-space allocation used by MS-DOS and OS/2. Brings all pointers together into an index block in FAT Variant of linked allocation: FAT resides in reserved section at beginning of each disk volume One entry for each disk block, indexed by block number, points to successor. Entry for last block in a chain has table value -1 Unused blocks have table value 0 Finding free blocks is easy Does not scale well to large disks or small block sizes TDDB68, C. Kessler, IDA, Linköpings universitet. 11.11 Silberschatz, Galvin and Gagne ©2005 Indexed Allocation (Cont.) TDDB68, C. Kessler, IDA, Linköpings universitet. 11.12 Silberschatz, Galvin and Gagne ©2005 Multilevel-indexed allocation Direct access once index block is loaded without external fragmentation, but overhead of index block. Directory All block pointers of a file must fit into the index block How large should an index block be? Small – Limits file size Large – Wastes space for small files Solution: Multi-level indexed allocation outer-index index table TDDB68, C. Kessler, IDA, Linköpings universitet. 11.13 Silberschatz, Galvin and Gagne ©2005 TDDB68, C. Kessler, IDA, Linköpings universitet. 11.14 file Silberschatz, Galvin and Gagne ©2005 Combined Scheme: UNIX inode Free-Space Management (1) Block size 4 KB -> With 12 direct block pointers kept in the inode, 48 KB can be addressed directly. Bit vector (of size n for n blocks) 0 1 2 n-1 bit[i] = Number of first free block = … 1 block[i] free 0 block[i] occupied (number of bits per word) * (number of 0-value words) + offset of first 1 bit Easy to get contiguous files Bit map requires extra space Small overhead for small files block size = 1 KB = 210 bytes disk size = 68 GB ~ 236 bytes n = 236/210 = 226 bits (or 67 MB) Still allows large files TDDB68, C. Kessler, IDA, Linköpings universitet. Example: Inefficient unless entire bit vector is kept in main memory 11.15 Silberschatz, Galvin and Gagne ©2005 Free-Space Management (2) TDDB68, C. Kessler, IDA, Linköpings universitet. Silberschatz, Galvin and Gagne ©2005 11.16 Free-Space Management (3) Grouping a really free block (n-1 references to free blocks) First ”free” block Linked free space list on disk Counting Only need to store the pointer to the first free block Finding k free blocks means reading in k blocks from disk No waste of space TDDB68, C. Kessler, IDA, Linköpings universitet. 11.17 Silberschatz, Galvin and Gagne ©2005 File System Consistency … Often, multiple subsequent blocks are allocated/freed together For sequences of free blocks located subsequently on disk, keep only reference to first one and length of sequence TDDB68, C. Kessler, IDA, Linköpings universitet. 11.18 Silberschatz, Galvin and Gagne ©2005 Memory-Mapped Files We know: Which blocks are used by each file and which blocks are free If a partition was not cleanly unmounted (crash, power failure) these can become inconsistent We can try to repair (fsck, scandisk) For each block Find which files use the block Check if the block is marked as free The block is used by 1 file xor is free – OK Two files use the same block – BAD: duplicate the block and give one to each file The block is both used and is marked free – BAD: remove from free list The block is neither free nor used – Wasted block: mark as free TDDB68, C. Kessler, IDA, Linköpings universitet. 11.19 Silberschatz, Galvin and Gagne ©2005 Memory-mapped file I/O allows file I/O to be treated as routine memory access by mapping a disk block to a page in memory A file is initially read using demand paging. A page-sized portion of the file is read from the file system into a physical page. Subsequent reads/writes to/from the file are treated as ordinary memory accesses. Simplifies file access by treating file I/O through memory rather than read() / write() system calls Also allows several processes to map the same file allowing the pages in memory to be shared TDDB68, C. Kessler, IDA, Linköpings universitet. 11.20 Silberschatz, Galvin and Gagne ©2005