Siksha Sarovar

Siksha Sarovar (sikshasarovar.com) is a free educational web application that helps students in India learn programming and prepare for academic and competitive exams. The platform offers structured coding courses (C, C++, Python, Java, HTML, CSS, PHP, Power BI, AI, Machine Learning, Data Science), complete university curriculum notes for BCA/MCA students with previous year question papers, Class 10 and Class 12 CBSE/HBSE school notes, and dedicated preparation material for SSC, UPSC, Banking, Railway and other government exams. Browsing the site is completely free and requires no account. Users may optionally sign in with Google solely to save their learning progress, quiz scores and personal preferences across devices.

Privacy Policy | Terms of Service | Contact Siksha Sarovar | About Siksha Sarovar

v4.0.9 · PWA
Siksha Sarovar logo
Siksha Sarovar
Your Learning Universe

Siksha Sarovar is a free e-learning platform for coding courses, BCA university notes and competitive exam preparation. Optional Google sign-in saves your learning progress across devices.

Initializing knowledge base…
Compiling modules 0%

Accessing and Releasing Inodes

Lesson 14 of 32 in the free Design of Unix Operating System notes on Siksha Sarovar, written by Rohit Jangra.

Introduction

The UNIX kernel manages inodes through a system of accessing (allocating) and releasing (freeing) them. When a process needs a file, the kernel must retrieve the file's inode. When the process is done, the inode must be properly released. This mechanism is essential for file system integrity and performance.

In-Core Inode Table

  • The kernel maintains an in-core inode table (also called the inode cache) in memory.
  • This table is a fixed-size array of in-core inode structures.
  • Each in-core inode contains:
  • All fields from the disk inode
  • Reference count — number of active references to this inode
  • Lock flag — to prevent simultaneous access
  • Modified flag — indicates if the inode needs to be written back to disk
  • Device and inode number — to identify which file system and which inode

The iget Algorithm (Accessing an Inode)

The kernel uses the iget algorithm to get an inode into memory.

Steps:

  1. The kernel receives a (device number, inode number) pair.
  2. It searches the in-core inode table for a matching entry.
  3. If found in cache (cache hit):
  • Check if the inode is locked. If locked, the process sleeps until it's unlocked.
  • Increment the reference count.
  • Return the in-core inode to the caller.
  1. If NOT found in cache (cache miss):
  • Allocate a free slot in the in-core inode table.
  • If no free slots are available, return an error.
  • Read the inode from disk into the allocated slot.
  • Initialize the reference count to 1.
  • Return the in-core inode to the caller.

The iput Algorithm (Releasing an Inode)

The kernel uses the iput algorithm to release an inode when a process no longer needs it.

Steps:

  1. Lock the in-core inode (if not already locked).
  2. Decrement the reference count.
  3. If reference count becomes 0 (no process is using this inode):
  • If the inode has been modified, write it back to disk (sync).
  • If the link count is 0 (no directory entries point to this file):
  • Free all data blocks associated with the file.
  • Set the file type to 0 (marks inode as free).
  • Free the inode (add it to the free inode list in the super block).
  • Place the in-core inode on the free list of the inode cache (for reuse).
  1. Unlock the inode and wake up any processes waiting for it.

Race Conditions and Locking

  • Multiple processes may try to access the same inode simultaneously.
  • The kernel uses locking to prevent race conditions:
  • A process that needs a locked inode must sleep until it is unlocked.
  • When the inode is unlocked, sleeping processes are woken up.
  • The lock ensures that only one process modifies the inode at a time.

Key Points

OperationAlgorithmAction
Get inodeigetSearch cache → Load from disk if needed → Increment ref count
Release inodeiputDecrement ref count → Write back if modified → Free if link count = 0

Summary

  • iget retrieves an inode into the in-core table, either from cache or disk.
  • iput releases an inode by decrementing the reference count.
  • When the reference count reaches 0, the inode may be written back to disk or freed.
  • Locking prevents race conditions when multiple processes access the same inode.