An inode has 12 direct pointers and one single, one double and one triple indirect pointer. Blocks are 4 KB and a block pointer is 4 bytes. What is the largest possible file?
A file system organises data on secondary storage: files and their attributes, access methods, directory structures, mounting, allocation (contiguous, linked, FAT, indexed, Unix inodes), free-space management and protection. Inode file-size numericals and allocation trade-offs are classic questions.
A file is a named collection of related information recorded on secondary storage — the smallest unit of logical storage a user sees. The OS maps files onto physical devices, so a program can treat a file as a contiguous logical address space of bytes or records, whatever the disk layout underneath. Files hold programs (source, object, executable) and data (text, numbers, images, databases).
| Attribute | Meaning |
|---|---|
| Name | The human-readable name — the only attribute kept in human form |
| Identifier | A unique number (e.g. the inode number) by which the file system knows the file |
| Type | Needed by systems that support different file types |
| Location | Pointer to the device and to the file's blocks on it |
| Size | Current size (bytes, words or blocks) and possibly the maximum allowed |
| Protection | Who may read, write, execute |
| Time, date and user identification | Creation, last modification and last access times; owner |
To avoid searching the directory on every access, a process first opens a file. The OS keeps a per-process open-file table (the current file pointer and access mode of every file the process has open) whose entries point into a system-wide open-file table (process-independent data: disk location, size, and an open count of how many processes have the file open). open() returns a small integer — the file descriptor — that indexes the per-process table; close() decrements the open count and frees the system-wide entry when it reaches 0. Files can also be locked: shared locks (many readers) or exclusive locks (one writer), mandatory (enforced by the OS, as in Windows) or advisory (honoured only by cooperating programs, as in UNIX).
| File type | Usual extensions | Contents |
|---|---|---|
| Executable | exe, com, bin, or none | Ready-to-run machine-language program |
| Object | obj, o | Compiled code, not yet linked |
| Source code | c, cpp, java, py, asm | Program text in a language |
| Batch / script | bat, sh | Commands for the command interpreter |
| Text / document | txt, doc, docx, odt | Textual data and documents |
| Library | lib, a, so, dll | Libraries of routines for programmers |
| Print or view | pdf, ps, jpg | Files formatted for printing or viewing |
| Archive | zip, tar, gz | Related files grouped (and often compressed) into one |
| Multimedia | mp3, mp4, mov | Audio or video |
Windows relies on extensions to decide how to open a file. UNIX treats extensions only as hints; it recognises some files by a magic number in their first bytes — #! for a script, the bytes 0x7F followed by ELF for an executable — and the file command uses the same trick.
read next, write next, reset (rewind) and perhaps skip n. It models a tape and suits editors, compilers and report generation.read n, write n, position to n. The number is a relative block number counted from the start of the file. Disks make this possible; databases and reservation systems depend on it.Sequential access is easy to simulate on a direct-access file: keep a current position cp, and implement read next as read cp; cp = cp + 1. The reverse — direct access on a sequential device such as a tape — is hopelessly slow, since reaching record n means passing every record before it.
A directory is a symbol table that translates file names into their file control blocks (inodes). It must support search, create, delete, list, rename and traverse (visiting every file, for example during backup). Five logical structures appear in every textbook:
| Structure | Description | Advantage | Drawback |
|---|---|---|---|
| Single-level | All files of all users in one directory | Very simple | Every name must be unique system-wide; no grouping |
| Two-level | A master file directory (MFD) indexed by user, pointing to each user's own file directory (UFD) | Users are isolated; different users may reuse names | No grouping within a user; sharing is awkward |
| Tree-structured | Directories nest to any depth from a root; absolute and relative path names; a current working directory | Natural grouping; efficient search | Sharing a file between two places is not possible |
| Acyclic graph | A file or subdirectory can appear in several directories through links | Sharing without copying | Aliases complicate traversal and deletion (dangling pointers; reference counts) |
| General graph | Links may create cycles | Fully flexible | Traversals can loop for ever; garbage collection is needed to reclaim space |
In a tree, deleting a directory raises a policy question: UNIX's rmdir refuses unless the directory is empty, while rm -r deletes the whole subtree. An acyclic graph adds another: when a shared file is deleted through one name, the others must not be left pointing at freed blocks — hence link counts for hard links, and dangling symbolic links that simply fail on use.
A file system must be mounted before its files can be used: the OS is given the device and a mount point — an (ideally empty) directory in the existing tree where the new file system's root will appear. On Linux, mount -t ext4 /dev/sdb1 /mnt/data attaches it and umount /mnt/data (spelt without the first n) detaches it. File systems to be mounted at boot are listed in /etc/fstab, one per line with six fields: device, mount point, type, options, a dump flag used by the dump backup program, and the fsck pass order. Windows instead gives each volume a drive letter (C:, D:).
| On-disk structure | Contents | UNIX (UFS/ext) name | NTFS name |
|---|---|---|---|
| Boot control block | Code needed to boot an OS from this volume | Boot block | Partition boot sector |
| Volume control block | Block count and size, free-block count and pointers, free-FCB count | Superblock | Master File Table |
| Directory structure | Names mapped to file control blocks | Directories (name → inode number) | Stored in the MFT |
| File control block (FCB) | Per-file metadata and block pointers | Inode | A row (record) of the MFT |
In memory, the OS keeps a mount table, a cache of recently used directories, the system-wide and per-process open-file tables, and buffers for blocks in transit. Linux's Virtual File System (VFS) layer gives ext4, XFS, FAT, NFS and others one common interface built from superblock, inode, dentry and file objects, so read() works identically on all of them.
An allocation method decides which disk blocks hold a file. The goals are good space utilisation and fast access. Three basic methods exist — contiguous, linked and indexed — and real systems mix them.
Contiguous allocation stores each file in consecutive blocks; the directory entry records the start block and the length. Block i of a file starting at b is simply b + i, so both sequential and direct access are easy, and head movement is minimal. The drawbacks are those of variable-partition memory: external fragmentation, the need to know a file's size when it is created, and difficulty growing a file. Modern systems use extents — contiguous chunks, with a file made of several extents — which is how ext4 and NTFS reduce the problem.
Linked allocation makes each file a linked list of blocks that may lie anywhere on the disk; the directory holds pointers to the first and last blocks, and each block holds a pointer to the next. There is no external fragmentation and files grow freely. But only sequential access is efficient: to reach block i, the OS must read the i blocks before it. Pointers also use space (4 bytes of every 512-byte block ≈ 0.78%), and one damaged pointer loses the rest of the file. Grouping blocks into clusters reduces the overhead.
The File Allocation Table (FAT), used by MS-DOS and still by USB drives and memory cards, is a variation of linked allocation that moves all the pointers into a table at the start of the volume, with one entry per block (cluster). The directory entry gives a file's first block; FAT[b] holds the number of the block after b; a special value marks end-of-file, and 0 marks a free block. Because the table is small enough to cache in memory, random access no longer needs a disk read per hop. FAT12, FAT16 and FAT32 differ in the width of each entry (FAT32 actually uses 28 bits).
Indexed allocation gathers all of a file's block pointers into one index block: entry i points to the file's block i, and the directory entry holds the index block's address. It supports direct access without external fragmentation. The cost is the index block itself — wasteful for a tiny file — and the question of what to do when a file needs more pointers than one block can hold. Three answers: a linked scheme (chain several index blocks), a multilevel index (a first-level index block pointing to second-level index blocks), or the combined scheme of the Unix inode.
A Unix inode holds the file's metadata: type and permission bits (the mode), owner UID, group GID, size in bytes, the timestamps atime (last access), mtime (last modification) and ctime (last inode change), the link count, and 15 block pointers: 12 direct pointers to data blocks, 1 single indirect pointer to a block of pointers, 1 double indirect pointer to a block of pointers to blocks of pointers, and 1 triple indirect pointer one level deeper. Small files need no indirection at all; huge files remain reachable.
ls -i). That is how one inode can have several names (hard links). A file system can run out of inodes before it runs out of space; df -i shows inode usage.With 1 KB blocks and 4-byte pointers, n = 256 and the same calculation gives 12 KB + 256 KB + 64 MB + 16 GB ≈ 16 GB. Reading a byte through the triple indirect pointer costs three extra block reads (plus the data block) if none of the pointer blocks is cached. ext4 replaces indirect blocks with extents of up to 128 MB each, but the classic inode is what examinations ask about.
The file system must remember which blocks are free. The free-space list — whatever its actual form — is consulted on every allocation and updated on every deletion.
Solid-state drives add a twist: the file system must tell the device which blocks are now free (the ATA TRIM command, NVMe deallocate), so the drive's garbage collector need not preserve stale data.
Protection controls which operations each user may perform: read, write, execute, append, delete and list (plus changing attributes). The general mechanism is an access-control list (ACL) per file naming users and their permitted operations — flexible but long. UNIX condenses it into three classes — owner, group and others (universe) — each with r, w, x bits, 9 bits in all: rwxr-x--- (octal 750) lets the owner do everything, the group read and execute, and others nothing. NTFS uses full ACLs; Linux adds optional POSIX ACLs (getfacl, setfacl). See the booting and user-accounts topic for chmod in detail.
A crash in the middle of an update can leave metadata inconsistent — a block marked free yet used by a file, a directory entry pointing at an unallocated inode. A consistency checker (fsck on UNIX, chkdsk on Windows) scans all metadata and repairs it, but that takes hours on large disks. Journaling (log-structured metadata) file systems first write each metadata change as a transaction to a log (the journal); once the transaction is committed it is applied to the real structures. After a crash, committed transactions in the log are replayed and incomplete ones discarded — recovery takes seconds. ext3/ext4 offer three modes: journal (data and metadata), ordered (the default: metadata journalled, data written first) and writeback. Copy-on-write file systems such as ZFS and Btrfs never overwrite live blocks, which also makes snapshots cheap.
| File system | Origin | Notable facts |
|---|---|---|
| FAT12 / FAT16 / FAT32 | MS-DOS, Windows 95 OSR2 (FAT32) | Linked allocation through a table; no journaling; FAT32 files are limited to 4 GB − 1 byte; universal on USB drives |
| exFAT | Microsoft, 2006 | For flash media; removes the 4 GB file limit; default on SDXC cards |
| NTFS | Windows NT 3.1, 1993 | Master File Table, journaling, ACLs, compression, EFS encryption, hard links |
| ext2 | Linux, 1993 | Inodes with direct/indirect pointers; no journal |
| ext3 | Linux, 2001 | ext2 plus a journal |
| ext4 | Linux, 2008 | Extents, delayed allocation, files up to 16 TB and volumes up to 1 EB (4 KB blocks); a common Linux default |
| XFS / Btrfs / ZFS | SGI 1993 / Oracle 2007 / Sun 2005 | XFS is the RHEL default; Btrfs and ZFS are copy-on-write with snapshots and checksums |
| HFS+ / APFS | Apple, 1998 / 2017 | APFS is copy-on-write, optimised for SSDs |
| ISO 9660 / UDF | CD-ROM / DVD and Blu-ray | Optical-disc formats |
Try the problem first, then reveal the working one step at a time.
An inode has 12 direct pointers and one single, one double and one triple indirect pointer. Blocks are 4 KB and a block pointer is 4 bytes. What is the largest possible file?
Same inode (4 KB blocks, 1024 pointers per block). A process reads byte 5,000,000 of a file. Which pointer leads to it, which indexes are used, and how many disk reads are needed if only the inode is in memory?
(a) A 16-block disk has blocks 1, 4, 5, 6, 9 and 12 free. Write the bit vector (1 = free), the counting list and the first free block using 8-bit words. (b) How large is the bitmap for a 1 TB disk with 4 KB blocks?
A file has 100 blocks. How many disk reads does each method need to read its 13th block (logical block 12), assuming the directory entry (or inode) is already in memory and nothing else is cached? Which method suits a read-only DVD, an append-only log and a database with random lookups?
Repeat the inode maximum-file-size calculation for 1 KB blocks and 4-byte pointers.
A 500 GB disk (1 GB = 2³⁰ bytes) uses 4 KB blocks. How big is a free-space bitmap? How big would a FAT with 4-byte entries be?
Alice creates report.txt. Bob makes a hard link rep2 and a symbolic link rep3 to it. Alice then deletes report.txt. What happens when Bob opens rep2 and rep3? What is the link count of the inode before and after?
Which access method suits each application? (a) Monthly payroll processing every employee record in order. (b) A railway reservation system fetching seat n of a coach file. (c) Looking up a student by roll number in a huge file kept sorted by roll number.
Give one advantage and one disadvantage of each allocation method: contiguous, linked, FAT and indexed.
10 questions. 70 % passes the topic quiz (+30 XP, +20 more for 100 %). Exam scoring: +1 correct, −⅓ wrong.
Modelled on real KPSC questions (reworded) with the same concept and answer; each links to its original paper.