Presentation is loading. Please wait.

Presentation is loading. Please wait.

1 Chapter 4. INTERNAL REPRESENTATION OF FILES THE DESIGN OF THE UNIX OPERATING SYSTEM Maurice J. bach Prentice Hall.

Similar presentations


Presentation on theme: "1 Chapter 4. INTERNAL REPRESENTATION OF FILES THE DESIGN OF THE UNIX OPERATING SYSTEM Maurice J. bach Prentice Hall."— Presentation transcript:

1 1 Chapter 4. INTERNAL REPRESENTATION OF FILES THE DESIGN OF THE UNIX OPERATING SYSTEM Maurice J. bach Prentice Hall

2 2 contents Inodes Structure of a regular file Conversion of a path name to an inode Super block Inode assignment to a new file Delete Inode

3 3 File System Algorithms Lower Level File system Algorithms namei alloc free ialloc ifree iget iput bmap buffer allocation algorithms getblk brelse bread bwrite

4 4 4.1 Inode contains the information necessary for a process to access a file It exits in a static form on disk and the kernel reads them into an in-core inode

5 5 4.1 Inodes Disk inode consists of : - file owner identifier Individual owner Group owner Set of users – access rights Super user – access rights to all files - file type (regular, directory character or block special of FIFO (pipes) - file access permissions (R, W & E) - file access times (the time - file was last modified, last accessed, when the Inode was last modified - number of links to the file ( the no. of names the file has in the directory - table of contents for the disk address of data in a file - file size

6 Fig: sample disk inode owner mjb group os type regular file perms rwxr-xr-x accessed Oct 23 1984 1:45 P.M modified Oct 22 1984 10:30 A.M inode Oct 23 1984 1:30 P.M size 6030 bytes Disk addresses 6

7 7 4.1 Inodes in-core copy of the inode contains - status of the in-core inode  Inode is locked  Process is waiting for the inode to become unlocked  In-core representation of the inode differs from the disk copy as a result of a change to the data in the inode  In-core representation of the inode differs from the disk copy as a result of a change to the file data  File is a mount point - logical device number of file system - inode number (stored as a linear array on disk) - pointers to other in-core inodes  Kernel links inodes on hash queues and on free list  Hash queue is identified according to the inode’s logical device number and inode number - reference count (no. of instances of the file are active)

8 8 algorithm iget Input: file system inode number Output: locked inode { while(not done) { if (inode in inode cache) { if (inode locked) { sleep(event inode becomes unlocked); continue; /* loop back to while */ } /* special processing for mount points (Chapter 5) */ if (inode on inode free list) remove from free list; increment inode reference count; return (inode); } /* inode not in inode cache */ if (no inodes on free list) return (error); remove new inode from free list; reset inode number and file system; remove inode from old hash queue, place on new one; read inode from disk (algorithim bread); initialize inode (e.g. reference count to 1); return (inode); }

9 4.1.2 Accessing inodes Kernel identifies inodes –File system and inode number –Allocates in-core inodes at the request of higher-level algorithms –Algo. iget allocates an in-core copy of an inode Kernel maps the device number and inode number into a hash queue and searches the queue for the inode –cannot find the inode, Allocates one from the free list and locks it Read the disk copy of the newly accessed inode into the in-core copy It knows the inode number and logical device – computes logical disk block that contains inode according to how many disk inodes fit into a disk block 9

10 Block bum = ((inode number – 1) / number of inodes per block) + start block of inode list Example –Block 2 is the beginning of the inode list –There are 8 inodes per block –Then inode number 8 is in disk block 2 and inode number 9 is in disk block 3 –If there are 16 inodes in a disk block then inode numbers 8 & 9 are in ---disk block and inode number 17 is in --- disk block 10

11 Kernel knows the device and disk block number – it reads the block using the algorithm bread It uses the following formula to compute the byte offset of the inode in the block: ((inode number – 1) modulo (number of inodes per block) ) * size of the disk inode + start block of inode list Copies the contents from the disk inode to the in-core inode and returns a locked inode The kernel manipulates the inode lock and reference count independently. The lock is set – execution of a system call to prevent other accesses It releases the lock at the conclusion of the system call 11

12 Kernel increments the reference count at for every active reference to a file ( when a process opens a file) Decrements when a reference becomes inactive ( when a process closes a file) The lock is free between system calls to allow processes to share simultaneous access to a file The reference count is set between system calls to prevent the kernel fro reallocating 12

13 Hanging 13

14 14

15 15 iput (inode_no) //releaseIncoreInode –lock inode if not locked –decrement inode refernece count –if (refernce count==0) if (inode link==0) –free disk block –set file type to 0 –free inode if (file accessed or inode changed or file changed) –update disk inode put inode on free list –Release inode lock

16 Structure of a Regular File Table of contents in an inode –Location of a file’s data on disk –A set of disk block # Each block on a disk is addressable by number –Not contiguous file allocation strategy Why ? When a file expand or contract… Fragmentations occur 16

17 17 Sample - Fragmentation File B was expanded Garbage collection – too high cost File A File BFile C 40 506070 …. ……. File A FreeFile C 40 506070 ….…….File B 85

18 18 Fig: Direct and indirect blocks in inode direct0 direct1 direct2 direct3 direct4 direct5 direct6 direct7 direct8 direct9 single indirect double indirect triple indirect InodeData Blocks

19 19 Suppose System V UNIX Assume that a logical on the file system holds 1K bytes and that a block number is addressable by a 32 bit integer, then a block can hold up to 256 block numbers 13 entries in the inode table of contents –10 direct, 1 indirect, 1 double indirect, 1 triple indirect block Assume –a logical block = 1K bytes –a block number is addressable by a 32 bit (4 bytes) integer –a block can hold up to 256 block numbers Byte Capacity of a File 10 direct blocks with 1K bytes each=10K bytes 1 indirect block with 256 direct blocks= 1K*256=256K bytes 1 double indirect block with 256 indirect blocks=256K*256=64M bytes 1 triple indirect block with 256 double indirect blocks=64M*256=16G bytes

20 Processes –access data in a file by byte offset –view a file as a stream of bytes The kernel –accesses the inode –converts the logical file block into the appropriate disk block 20

21 21 File byte offset into physical disk block algorithm bmap Input: (1) inode (2) 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 clock for I/O; calculate number of bytes to copy to user; check if read-ahead applicable, mark inode; determine level of indirection;

22 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; } // Conversion of byte offset to block number in file system 22

23 23 Block Layout of a Sample File and its Inode 4096 228 45423 0 11111 0 101 367 0 428 9156 824 331 3333 9156 331 3333 0 75 367 1 disk block = 1024 bytes byte offset 9000, byte offset 350,000 816th 808t h

24 4.3 Directories A directory is a file –It gives hierarchical structure to the file system –Play an important role in conversion of a file name to an inode number –Its data is a sequence of entries –Contents of each entries an inode number and the name of a file Path name is a null terminated character string divided into components by slash (“/”) Each component must be the name of a directory and last may not be UNIX System V –Maximum of component name : 14 characters –Inode number entry : 2 bytes –Size of a directory entry : 16 bytes 24

25 Fig Directory layout for /etc 25 Byte Offset in Directory Inode Number (2 bytes) File Name 0 16 32 48 64 80 96 112 128 144 160 176 192 208 224 240 256 83 2 1798 1276 85 1268 1799 88 2114 1717 1851 92 84 1432 0 95 188.. init fsck clri motd mount mknod passwd umount checklist fsdblb config getty crash mkfs inittab

26 Read permission Write permission –Creat –Mknod –Link –Unlink system calls Execute permission – search the directory for a file name 26

27 27 4.4 Path conversion to an inode if (path name starts with root) –working inode= root inode else –working inode= current directory inode while (there is more path name) –read next component from input –read directory content –if (component matches an entry in directory) get inode number for matched component release working inode working inode=inode of matched component –else – return no inode return (working inode)

28 28 4.4 Conversion of a path name to an inode algorithm namei - If path name starts from root, then the kernel assigns root inode(iget) to working inode - Otherwise, the kernel assigns current directory inode to working inode - While there is more path name, the kernel reads next path name component from input, and verifies that working inode is of directory, access permissions OK If working inode is of root and component is ‘..’, then the kernel checks whether there is more path name or not Otherwise the kernel reads directory by repeated use of bmap,bread,brelse

29 29 4.5 Super block File System consists of - 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 boot block super block inode list data blocks

30 30 4.6 Inode assignment to a new file free inodes 83 48 empty 18 19 20 array1 Super Block Free Inode List index free inodes 83 empty 18 19 20 array2 Super Block Free Inode List index Assigning Free Inode from Middle of List

31 31 4.6 Inode assignment to a new file index Assigning Free Inode – Super Block List Empty 470 empty array1 Super Block Free Inode List index 0 535 free inodes 476 475 471 array2Super Block Free Inode List 0 48 49 50 remembered inode

32 32 4.6 Inode assignment to a new file 535 476 475 471 free inodes remembered inode Original Super Block List of Free Inodes index Free Inode 499 499 476 475 471 free inodes remembered inode index Free Inode 601 499 476 475 471 free inodes remembered inode index

33 33 4.6 Inode assignment to a new file Locked inode illoc() –while (not done) If (super block locked) –Sleep (event super block becomes free) –Continue If (inode list in super block empty) –Lock super block –Get remember inode for free inode search –Search until super block full or no more free inode –Unlock super block and wake up (event super block free)\ –If no free inode found on disk return error –Set remmbered inode for next free inode search Get inode number from super block inode list Get inode Write inode to disk Decrement free inode count Return inode

34 34 Freeing inode ifree(inode_no) –Increment free inode count –If super block locked return –If (inode list full) //at super block if (inode number <remembered inode) –Set remembered inode as input inode –Else Store inode number in inode list –return

35 35 4.7 Allocation of disk blocks 109 106 103 100 ………………………….. 211 208 205 202 …………………… 112 109 310 307 304 301 …………………… 214 211 409 406 403 400 …………………… 313 310 linked list of free disk block number

36 36 4.7 Allocation of disk blocks algorithm alloc - The kernel wants to allocate a block from a file system it allocates the next available block in the super block list - Once allocated, the block cannot be reallocated until it becomes free - If the allocated block is the last block, the kernel treats it as a pointer to a block that contains a list of free blocks The kernel locks super block, reads block jut taken from free list, copies block numbers in block into super block, releases block buffer,and unlocks super block - Otherwise, The kernels gets buffer for block removed from super block list, zero buffer contents, decrements total count of free blocks, and marks super block modified

37 37 4.7 Allocation of disk blocks 109 ………………………………………………………… 211 208 205 202 …………………………….. 112 109 949 ………………………………………………….. 211 208 205 202 ………………………………. 112 super block list original configuration 109 After freeing block number 949

38 38 4.7 Allocation of disk blocks 211 208 205 202 ……………………………… 112 344 341 338 335 ………………………………. 243 After assigning block number(109) replenish super block free list 211 109 ……………………………………………………….. 211 208 205 202 ………………………………. 112 109 After assigning block number(949)


Download ppt "1 Chapter 4. INTERNAL REPRESENTATION OF FILES THE DESIGN OF THE UNIX OPERATING SYSTEM Maurice J. bach Prentice Hall."

Similar presentations


Ads by Google