Chapter 4.

INTERNAL REPRESENTATION OF FILES

THE DESIGN OF THE UNIX OPERATING SYSTEM Maurice J. bach Prentice Hall
1

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

2

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

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

file owner identifier .4.1 Inodes • consists of .file access times .number of links to the file .table of contents for the disk address of data in a file .file size 5 .file access permissions .file type .

status of the in-core inode .logical device number of file system .pointers to other in-core inodes .4.1 Inodes • in-core copy of the inode contains .reference count 6 .inode number .

iget (inode_no) //getIncoreInode • while (not done) – if (inode in inode cache) • if (inode locked) – sleep(event inode becomes unlocked) – continue • if (inode on inode free list) – remove from free list – return locked inode – – – – – – – if (no inode on free list) return error remove new inode from free list set inode number remove inode from old hash queue and place on new one read inode from disk set reference count 1 return locked indoe 7 .

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 8 .

2 Structure of a regular file Inode direct0 direct1 direct2 direct3 Data Blocks direct4 direct5 direct6 direct7 direct8 direct9 single indirect double indirect triple indirect 9 .4.

then a block can hold up to 256 block numbers 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 10 .4.2 Structure of a regular file 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.

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) 11 .4.

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

the number of free blocks in the file system .the index of the next free inode in the free inode list .a list of free inodes in the file system .the size of the file system .a flag indicating that the super block has been modified 13 .the number of free inodes in the file system .4.the size of the inode list .5 Super block • File System boot block super block inode list data blocks • consists of .lock fields for the free block and free inode lists .the index of the next free block in the free block list .a list of free blocks available on the file system .

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

6 Inode assignment to a new file Super Block Free Inode List 470 empty 0 remembered inode Super Block Free Inode List 535 free inodes array1 index array2 476 475 471 0 48 49 50 index Assigning Free Inode – Super Block List Empty 15 .4.

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

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 17 .4.

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 18 .

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

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

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

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

.

66005.  *55* -..2-./52--.2/67  ..

2-.- 2-.. 5.52.+.37 -. 34/0. *55*...5.883203994.5.67  .3010 "83.

"83.2-..52.67 *55* /5..2-.5.6 .

6737  .  -. "83. 66005...52.2-.

 34/0.883203994.3010  .

2-.6  .... /5.5.672/5. -.2-. 50*"83..+.-2-.52.6 5.

..5.6 5.  .. 5.2-..+.2-. /5. -.-2-.

. /5.5. 5.2-..2-..-2-. . -.6 5..+.

 .

883203994.0/ $005 0..4. 0 349/430 W 1 8:507-4.-0.4:39 W #09:7334/0  . $0.7.0259 4. 0970202-0734/0147170034/080. 34/0.1700  134170034/014:3/43/8709:7307747 $097022-070/34/0147309170034/080.34/089 W 0934/0 W 79034/094/8 W 0.0398:507-4.:398:507-4.8:507-4.0/34/04.0:5 0.8:507-4.0398:507-4.7.3010 W 4.1:47342470170034/0 &34.7.42081700 4393:0 W 1 34/08938:507-4. W 0934/03:2-0717428:507-4.702039170034/0.3/.

4:39 18:507-4.700334/0 W 1700 34/0*34 3.702039170034/0.0/709:73 1 34/0891: .4.

.

.98:507-4.835:934/0 80 W $947034/03:2-07334/089 709:73  . W 1 34/03:2-0770202-070/34/0 $0970202-070/34/0.

.94341/8-4.8 . 4.

.

.

.

 .

 .

.

    .

.

.

.

.

   .

 .

 .

     .

-672//5. .-6+2.5  ..8+.

4.3/:34. 700. 70.3349-070.8 .94341/8-4.24/10/  ./8-4.9.-4. -4.4390398 /0.89 3.42081700 190.3948:507 -4.90890309..4.4792.031742 170089 ..4508-4.4:39 411700-4..:99. %00730.89 074-:1107.989.3/2.7024.90/-4.808-4.89411700-4.8.439. 4.9.90/ 90-4.1088902 9.3:2-0783-4.89-4.90/:399-0..4.1742.88:507-4....7020398949..39894.4..-0-4..3908:507-4..4.4.-:1107 .90. 900730970.0/17428:507 -4.. 90780  W %007308098-:1107147-4.8 W%007304.8 W .88:507-4.0.38.890.54390794.788:507-4.

 4.94341/8-4..5+2.8 683.67 .

 .

 .

.

    .

.

2/085*72 . 250*.

 .

 .

.

    .

.

.5    .5/5.0+2.8+. /7.

 4..94341/8-4.8 .

 .

 .

.

    .

.

/7.8+.5*6600+2.5  .

.

    .

.

.

.

.

  /7.8+.5 .5*6600+2.

 5./5.3.61683.5+2..67    .

Sign up to vote on this title
UsefulNot useful