UNIX File Systems based on Chap 4 in
UNIX File Systems (based on Chap 4. in the book “the design of the UNIX OS”) Acknowledgement : Soongsil Univ. Presentation Materials 단국대 최종무 교수님 강의노트
Hard Disk Drive Structure n Characteristics n n Seek time, rotational latency, transmission time Disk Scheduling? , cylinder group? , Buffering? 2
Flash Memory n limitation n Block erasure : block 단위로 지워짐. Memory wear : # of erase limiation Read disturb Seek optimization 이 불필요 n Require strong reliability (e. g. power failure recovery) n 3
File System n the system in which files are named and where they are placed logically for storage and retrieval n 컴퓨터에서 파일이나 자료를 쉽게 발견 및 접근 (read/write)할 수 있도록 보관/조직하는 체제 n Properties n n Hierarchical structure Ability to create, read/write, and delete files Dynamic growth of files Protection of file data
File System 하는일 n Support file interfaces to user n n File management n n n File interface: open, close, read, write, … Directory manipulation: mkdir, cd, rmdir, … File system interfaces: mount, mkfs, fsck, … i-node, FAT Access control, attribute control, . . Storage management n Disk management (Block allocation, I/O handling, …) 5
OS별 File Systems UNIX File System n LINUX : EXT 2, EXT 3, EXT 4 n Windows : NTFS n OS X (Apple Mac OS) : HFS+ n Traditional OS (MS-Windows 9 x) : FAT 32 n 6
FAT n File Allocation Table 8
FAT 9
File System Layout • Root directory의 i-node 는 미리 정해져 있음.
Sample File System
File Sharing n n n Each user has a “file descriptor table” (or “per-user open file table”) Each entry in the channel table is a pointer to an entry in the systemwide “open file table” Each entry in the open file table contains a file offset (file pointer) and a pointer to an entry in the “memory-resident i-node table” If a process opens an already-open file, a new open file table entry is created (with a new file offset), pointing to the same entry in the memory-resident i-node table If a process forks, the child gets a copy of the channel table (and thus the same file offset) 14
UNIX File System Overview
EXT 2 file system LINUX 기본 file system n Ext 2 특징 n n n Ext 3 특징 n n block group 도입 : 같은 파일은 같은 block group (하드 디스크상 같은 cylinder)을 가능한 사용하여 성능 향상(disk seek time, fragmentation ) Journaling 기능 도입: Logging and Fast Recovery Ext 4 특징 n Large size 파일 지원 16
UNIX File System
Table of Contents n n n n n Inodes Structure of a regular file Directories Conversion of a path name to an Inode Super block Inode assignment to a new file Allocation of disk blocks Other file types Summary 18
Summary Inode is the data structure that describes the attributes of a file, including the layout of its data on disk. n Two versions of the inode n n n n Disk copy : store the inode information when file is not in use In-core copy : record the information about active files. Inode disk blocks Directories : files that correlate file name components to inode numbers namei : convert file names to inodes Inode assignment Disk block assignment 19
Table of Contents n n n n Inodes Structure of a Regular File Directories Conversion of a Path Name to an Inode Super Block Inode Assignment to a New File Allocation of Disk Blocks Summary 20
Definition of Inodes Every file has a unique inode n Contain the information necessary for a process to access a file n Exist in a static form on disk n Kernel reads them into an in-core inode to manipulate them. n 21
Contents of Disk inodes n n n n File owner identifier (individual/group owner) File type (regular, directory, . . ) File access permission (owner, group, other) File access time Number of links to the file Table of contents for the disk address of data in a file (byte stream vs discontiguous disk blocks) File size * inode does not specify the path name that access the file 22
Sample Disk Inode n n n n File owner identifier File type File access permission File access time Number of links to the file Table of contents for the disk address of data in a file File size Owner mjb Group os Type regular file Perms rwxr-xr-x Accessed Oct 23 1984 1: 45 P. M Modified Oct 22 1984 10: 3 A. M Inode Oct 23 1984 1: 30 P. M Size 6030 bytes Disk addresses 23
Distinction Between Writing inode and File change only when writing it. n Inode change when changing the file, or when changing its owner, permisson, or link settings. n Changing a file implies a change to the inode, n But, changing the inode does not imply that the file change. n 24
Contents of The In-core copy of The Inode Fields of the disk inode n Status of the in-core inode, indicating whether n n n Inode is locked Process is waiting for the inode to become unlocked In-core inode differs from the disk copy as a result of a change to the data in the inode In-core inode differs from the disk copy as a result of a change to the file data Inode number (linear array on disk, disk inode does not need this field) n Reference count n 25
Inode Lock and Reference Count Kernel manipulates them independently n Inode lock n n Set during execution of a system call to prevent other processes from accessing the inode while it is in use. Kernel releases the lock at the conclusion of the system call Reference count n n Kernel increase/decrease when reference is active/inactive Prevent the kernel from reallocating an active in-core inode 26
Table of Contents n n n n Inodes Structure of a Regular File Directories Conversion of a Path Name to an Inode Super Block Inode Assignment to a New File Allocation of Disk Blocks Summary 27
Direct and Indirect Blocks in Inode Data Blocks direct 0 direct 1 direct 2 direct 3 direct 4 direct 5 direct 6 direct 7 direct 8 direct 9 single indirect double indirect triple indirect 28
Byte Capacity of a File n System V UNIX. Assume that n n n Run with 13 entries 1 logical block : 1 K bytes Block number address : a 32 bit (4 byte) integer n 1 block can hold up to 256 block number (1024 byte / 4 byte) 10 direct blocks with 1 K bytes each=10 K bytes 1 indirect block with 256 direct blocks= 1 K*256=256 K bytes 1 double indirect block with 256 indirect blocks=256 K*256=64 M bytes 1 triple indirect block with 256 double indirect blocks=64 M*256=16 G n Size of a file : 4 G (232), if file size field in inode is 32 bits n n * Refer to the link on our class webpage for the case of current UNIX file system. 29
Byte Offset and Block Number Process access data in a file by byte offset. n The file starts at logical block 0 and continues to a logical block number corresponding to the file size n Kernel accesses the inode and converts the logical file block into the appropriate disk block n 30
Conversion of Byte Offset to Block Number Algorithm bmap /* block map of logical file byte offset to file system block */ Input : inode, byte offset Output: (1)block number in file system, (2)byte offset into block, (3)bytes of I/O in block, (4)read ahead block number calculate logical block number in file from byte offset; calculate start byte in block for I/O; /* output 2 */ calculate number of bytes to copy to user; /* output 3 */ check if read-ahead applicable, mark inode; /* output 4*/ determine level of indirection; while(not at necessary level of indirection) calculate index into inode or indirect block from logical block number in file; get disk block number from inode or indirect block; release buffer from previous disk read, if any (algorithm brelse); if(no more levels of indirection) return (block number); read indirect disk block (algorithm bread); adjust logical block number in file according to level of indirection; 31
Block Layout of a Sample File and Its inode 0 4096 228 45423 Byte 9000 in a file -> 8 block 808 th byte 0 0 11111 367 Data block 0 101 8 367 0 0 331 428 (10 K+256 K) 11 9156 Double indirect 75 3333 331 Single indirect 816 th byte 3333 Data block Byte 350, 000 in a file 824 32
Block Entry in the Inode is 0 Logical block entry contain no data. n Process never wrote data into the file at that byte offset n No disk space is wasted n Cause by using the lseek and write system call n 33
Table of Contents n n n n Inodes Structure of a Regular File Directories Conversion of a Path Name to an Inode Super Block Inode Assignment to a New File Allocation of Disk Blocks Summary 34
Directories A directory is a file n Its data is a sequence of entries, each consisting of an inode number and the name of a file contained in the directory n Path name is a null terminated character string divided by “/” n Each component except the last must be the name of a directory, last component may be a non-directory file n 35
Directory Layout for /etc Each entry : inode number and filename Inode Number (2 bytes) 83 2 1798 1276. . . 0 95 188 File Names. . . init fsck … crash mkfs inittab 36
Directories : How n How do you find files? n n How do you list files (ls) n n Read the directory, search for the name you want (checking for wildcards) Read directory contents, print name field How do you list file attributes (ls -l) n Read directory contents, open inodes, print name + attributes 37
Table of Contents n n n n Inodes Structure of a Regular File Directories Conversion of a Path Name to an Inode Super Block Inode Assignment to a New File Allocation of Disk Blocks Summary 38
Algorithm for Conversion of a Path Name to an Inode Algorithm namei Input : path name Output : locked inode { /* convert path name to inode */ if(path name starts from root) working inode = root inode (algorithm iget); else working inode = current directory inode (algorithm iget); while(there is more path name){ read next path name component from input; verify that working inode is of directory, access permission OK; if(working inode is of root and component is “. . ”) continue; /* loop back to while */ read directory (working inode) by repeated use of algorithms bmap, bread and brelse; … 39
Algorithm for Conversion of a Path Name to an Inode if(component matches an entry in directory (working inode)){ get inode number for matched component; release working inode (algorithm iput); working inode=inode of matched component(algorithm iget); } else /* component not in directory return (no inode); } return (working inode); } 40
Table of Contents n n n n n Inodes Structure of a Regular File Directories Conversion of a Path Name to an Inode Super Block Inode Assignment to a New File Allocation of Disk Blocks Other File Types Summary 41
Super block n File System boot block n super block inode list data blocks Super block consists of n n n n n the size of the file system the number of free blocks in the file system a list of free blocks available on the file system the index of the next free block in the free block list the size of the inode list the number of free inodes in the file system a list of free inodes in the file system the index of the next free inode in the free inode list lock fields for the free block and free inode lists a flag indicating that the super block has been modified 42
Table of Contents n n n n Inodes Structure of a Regular File Directories Conversion of a Path Name to an Inode Super Block Inode Assignment to a New File Allocation of Disk Blocks Summary 43
Inode Assignment to a New File Super block contains an array to store the index numbers of free inodes in the file system n remembered inode n n n 이 inode보다 작은 번호의 inode는 superblock free list에 없음을 보장한다. reduce free inode search time 44
Algorithm for Assigning New Inodes Algorithm ialloc /* allocate inode */ Input : file system Output : locked inode { while(not done){ if(super blocked) { sleep(event super block becomes free); continue; } if(inode list in super block is empty){ lock super block; get remembered inode for free inode search; search disk for free inodes until super block full, or no more free inodes (bread and brelese); unlock super block; wake up (event super block becomes free); if(no free inodes found on disk) return (no inode); set remembered inode for next free inode search; } 45
Algorithm for Assigning New Inodes /* there are inodes in super block inode list */ get inode number from super block inode list; get inode (algorithm iget); if(inode not free after all) { write inode to disk; release inode (algorithm iput); continue; /* while loop */ } /* inode is free */ initialize inode; write inode to disk; decrement file system free inode count; return (inode); } // end of while } 46
Assigning Free Inode from Middle of List Super Block Free Inode List free inodes 83 48 18 19 empty 20 array 1 index Super Block Free Inode List free inodes 83 18 19 empty 20 array 2 index 47
Assigning Free Inode – Super Block List Empty Super Block Free Inode List 470 empty 0 array 1 index remembered inode Super Block Free Inode List 535 free inodes 0 array 2 476 475 471 48 49 50 index 48
Algorithm for Freeing Inode Algorithm ifree /* inode free */ Input : file system inode number Output : none { increment file system free inode count; if(super blocked) return; if(inode list full){ if(inode number less than remembered inode for search) set remembered inode for search = input inode number; } else store inode number in inode list; return; } 49
Placing Free Inode Numbers Into the Super Block 535 476 475 471 free inodes remembered inode index Original Super Block List of Free Inodes 499 476 475 471 free inodes remembered inode index Free Inode 601 50
Table of Contents n n n n Inodes Structure of a Regular File Directories Conversion of a Path Name to an Inode Super Block Inode Assignment to a New File Allocation of Disk Blocks Summary 51
Linked List of Free Disk Block Numbers Super block list 109 106 103 100 ……………. . 109 211 208 205 202 ………… 211 310 307 304 301 112 ………… 214 310 409 406 403 400 ………… 313 52
Algorithm for Allocating Disk Block Algorithm alloc /* file system block allocation */ Input : file system number Output : buffer for new block { while(super blocked) sleep (event super block not locked); remove block from super block free list; if(removed last block from free list){ lock super block; read block just taken from free list (algorithm bread); copy block numbers in block into super block; release block buffer (algorithm brelse); unlock super block; wake up processes (event super block not locked); } … 53
Algorithm for Allocating Disk Block … get buffer form block removed from super block list (algorithm getblk); zero buffer contents; decrement total count of free blocks; mark super block modified; return buffer; } 54
Requesting and Freeing Disk Blocks super block list 109 …………………………… 109 211 208 205 202 ………………. . 112 original configuration 109 949 …………………………. . 109 211 208 205 202 ………………. 112 After freeing block number 949 55
Requesting and Freeing Disk Blocks 109 ……………………………. . 109 211 208 205 202 ………………. 112 After assigning block number(949) 211 208 205 202 ……………… 112 211 344 341 338 335 ………………. 243 After assigning block number(109) replenish super block free list 56
- Slides: 56