52

Click here to load reader

Chapter 12 File-System Implementation 文件系统实现 12.1 File-System Structure 文件系统结构 12.1 File-System Structure 文件系统结构File-System StructureFile-System

Embed Size (px)

Citation preview

1.01

A typical file control block

Operating SystemsX.J.Lee 2014812.2 File-System Implementation In this section, we delve into the structures and operations used to implement file-system operations.1. Overview2. Partitions and Mounting 3. Virtual File Systems Operating SystemsX.J.Lee 20145Indexed Allocation Brings all pointers together into the index block.Each file has its own index block. The ith entry in the index block points to the ith block of the file.iiThe directory contains the address of the index block.index tableOperating SystemsX.J.Lee 20148.3312.4 Allocation Methods An allocation method refers to how disk blocks are allocated for files.Goaldisk space is utilized effectively and file can be accessed quickly.Three major methodsContiguous allocation Linked allocation Indexed allocation Operating SystemsX.J.Lee 20148.2312.5 Free-Space Management Bit vector (bit map) 012n-1bit[i] ={1 block[i] free0 block[i] occupiedBlock number calculationThe block# of the first free block=(number of bits per word) *(number of 0-value words) +offset of first 1 bit*01Operating SystemsX.J.Lee 20148.42Grouping Store the addresses of n free blocks in the first free block. The first n-1 of these blocks are actually free. The last block contains the addresses of another n free blocks, and so on. nn-1nOperating SystemsX.J.Lee 20148.47The in-memory structures :An in-memory partition table containing information about each mounted partition.An in-memory directory structure that holds the directory information of recently accessed directories.The system-wide open-file table contains a copy of the FCB of each open file, as well as other information.The per-process open-file table contains a pointer to the appropriate entry in the system-wide open-file table, as well as other information.Operating SystemsX.J.Lee 20147Create a new file The process:An application program calls the logical file system.Logical file system allocates a new FCBRead the appropriate directory into memory, updates it with the new file name and FCB, and writes it back to the disk.Operating SystemsX.J.Lee 20148.9About directorySome operating systems (including UNIX) treat a directory as a file.Some operating systems (including Windows NT) treat a directories as entities separate from files. (separate system calls for files and directories.)Operating SystemsX.J.Lee 20148.10Open a file The process:The directory structures is searched for the given file name. (Parts of the directory structure are usually cached in memory to speed directory operations.)The FCB is copied into a system-wide open-file table in memory.An entry is made in the per-process open-file table. (with a pointer to the entry in the system-wide open-file table, a pointer to the current location in the file, the access mode in which the file is open, )Operating SystemsX.J.Lee 20148.11The open call returns a pointer to the appropriate entry in the per-process open-file table. All file operations are then performed via this pointer.About the pointer returned by the open callUnix systems refer to it as a file descriptor.Windows 2000 refers to it as a file handle.Operating SystemsX.J.Lee 20148.12Close a file The process:The per-process table entry is removed, and the system-wide entrys open count is decremented.When all users that have opened the file close it, the updated file information is copied back to the disk-based directory structure and the system-wide open-file table entry is removed.Operating SystemsX.J.Lee 20148.13Partitions and Mounting The layout of disk can have many variations, depending on the operating system. A disk can be sliced into multiple partitions, or a partition can span multiple disks (---a form of RAID).Each partition can be: raw : containing no file systemRaw disk is used where no file system is appropriate.Examples: UNIX swap space, some databases.cooked : containing a file systemOperating SystemsX.J.Lee 20148.15Boot information can be stored in a separate partition. it has its own formatbecause at boot time the system does not have file-system device drivers loaded and therefore cannot interpret the file-system format. it is usually a sequential series of blocks, loaded as an image into memory. Execution of the image starts at a predefined location, such as the first byte.Operating SystemsX.J.Lee 20148.16root partition which contains the operating-system kernel and potentially other system files, is mounted at boot time. Other partitions can be automatically mounted at boot or manually mounted later, depending on the operating system. Operating SystemsX.J.Lee 20148.17As part of a successful mount operation, the operating system verifies that the device contains a valid file system. It does so by asking the device driver to read the device directory and verifying that the directory has the expected format. If the format is invalid, the partition must have its consistency checked and possibly correct, either with or without user intervention. Finally, the operating system notes in its in-memory mount table structure that a file system is mounted, and the type of the file system.Operating SystemsX.J.Lee 20148.18Virtual File Systems How does an operating system allow multiple types of file systems to be integrated into a directory structure? How can users seamlessly move between file-system types as they navigate the file-system space?Operating SystemsX.J.Lee 20148.19Virtual File Systems --VFS provide an object-oriented way of implementing file systems.VFS allows the same system call interface (the API) to be used for different types of file systems.The API is to the VFS interface, rather than any specific type of file system.Operating SystemsX.J.Lee 20148.20Schematic View of Virtual File System

Operating SystemsX.J.Lee 201421Contiguous Allocation of Disk SpaceEach file occupies a set of contiguous blocks on the disk.The directory entry for each file indicates the address of the starting block (block #) and the length of the area allocated for this file (number of blocks).Operating SystemsX.J.Lee 201425AdvantagesThe least number of disk seek. (for only one access job)Accessing a file is easy. (for both sequential and direct access)DrawbacksDifficulty for finding space for a new file.Wasteful of space: external fragmentation. (dynamic storage-allocation problem)Size-declaration problem: files cannot grow.Operating SystemsX.J.Lee 20148.26Extent-Based Systems Many newer file systems (I.e. Veritas File System) use a modified contiguous allocation scheme.Extent-based file systems allocate disk blocks in extents. An extent is a contiguous block of disks. Extents are allocated for file allocation. A file consists of one or more extents.Operating SystemsX.J.Lee 20148.27AdvantagesSimple need only starting addressFree-space management system no waste of space DisadvantagesNo random accessWasteful of space: the space required for the pointers.Solution: collect blocks into multiples---clustersReliability.Partial solution: use double linked lists or to store the file name and relative block number in each block. (however, these schemes require even more overhead for each file.)Operating SystemsX.J.Lee 20148.29FAT Allocation File-allocation table (FAT) disk-space allocation used by MS-DOS and OS/2.A section of disk at the beginning of each partition is set aside to contain the FAT.FAT has one entry for each disk block, and is indexed by block#.Each entry contains the block# of the next block in the file.Unused blocks are indicated by a 0 table value.The directory entry contains the block# of the first block of the file.Operating SystemsX.J.Lee 20148.30File-Allocation Table

Operating SystemsX.J.Lee 201431The FAT allocation scheme can result in a significant number of disk head seeks, unless the FAT is cached.A benefit is that random access time is improvedOperating SystemsX.J.Lee 20148.32Example of Indexed Allocation

Operating SystemsX.J.Lee 201434AdvantagesIndex allocation supports random access Dynamic access without external fragmentation DisadvantagesWasteful of space the pointer overhead of the index block is generally greater than the pointer overhead of linked allocation. Operating SystemsX.J.Lee 20148.35Problem How large the index block should be?Linked schemelink together several index blocks.Multilevel index Combined scheme Operating SystemsX.J.Lee 20148.36Linked scheme Mapping from logical to physical in a file of maximum size of 256K words and block size of 512 words. We need only 1 block for index table.256K5121LA/512QRQ = displacement into index table R = displacement into block Operating SystemsX.J.Lee 20148.37Mapping from logical to physical in a file of unbounded length (block size of 512 words).512Linked scheme Link blocks of index table (no limit on size).LA / (512 x 511)Q1R1Q1 = block of index table R1 is used as follows: R1 R1 / 512Q2R2Q2 = displacement into block of index table R2 displacement into block of file Operating SystemsX.J.Lee 20148.38Multilevel index Two-level index (maximum file size is 5123) LA / (512 x 512)Q1R1Q1 = displacement into outer-indexR1 is used as follows:R1 / 512Q2R2Q2 = displacement into block of index tableR2 displacement into block of fileOperating SystemsX.J.Lee 20148.39Multilevel indexouter-indexindex tablefileOperating SystemsX.J.Lee 20148.40Combined Scheme: UNIX (4K bytes per block)

The UNIX inode.

Operating SystemsX.J.Lee 201441This scheme is not efficient: to traverse the list, we must read each block, which requires substantial I/O time. I/OThe FAT method incorporates free-block accounting into the allocation data structure. No separate method is needed. FATOperating SystemsX.J.Lee 20148.46

Operating SystemsX.J.Lee 201448Advantage:the addresses of a large number of free blocks can be found quickly, unlike in the standard linked-list approach. Operating SystemsX.J.Lee 20148.494. Counting Keep the address of the first free block and the number n of free contiguous blocks that follow the first block. n.Each entry in the free space list then consists of a disk address and a count.

Operating SystemsX.J.Lee 20148.50Exercises

12.15 Consider a file system on a disk that has both logical and physical block size of 512 bytes. Assume that the information about each file is already in memory. For each of the three allocation strategies (contiguous, linked, and indexed), answer these questions:a. How is the logical-to-physical address mapping accomplished in this system? (For the indexed allocation, assume that a file is always less than 512 blocks long.)b. If we are currently at logical block 10 (the last block accessed was block 10) and want to access logical block 4, how many physical blocks must be read from the disk?Operating SystemsX.J.Lee 20148.51End of Chapter 12