Chapter 10: File Systems

Chapter 10: File Systems · Study Questions
Open-file tablesPath resolutionHard and symbolic linksrwx and umaskMBR and GPTFAT and i-nodeFree spacefsck and journalingVFS
Sercan KÜLCÜ

Multiple Choice · Introduction

Question 1: What Does a Disk Offer?

What does a disk (HDD or SSD) offer the operating system by itself, and what does the file system build on top of it? Which of the following is an abstraction provided by the file system?

A: A disk offers only numbered blocks: “read / write block k”. ECC and the remapping of bad sectors are also done inside the disk controller. Naming, directories, attributes, protection, and distributing blocks among files are the file system’s job.

Step-by-Step Trace · The File Concept

Question 2: File Offset After fork

The program runs without errors. What does it print? Trace the entries in the open-file table and their offsets.

parent fd0tty1tty2tty340123456789letters.txt

letters.txt holds 0123456789 (10 bytes). Only 0, 1, 2 (the terminal) are open.

parent fd0tty1tty2tty34E1offset 0 · ref 1i-node0123456789letters.txtE1

open returns the smallest free number (3); a system-wide entry E1 with offset 0 is created.

parent fd0tty1tty2tty34E1offset 3 · ref 1i-node0123456789letters.txtE1

3 bytes are read (012); E1’s offset becomes 3.

parent fdchild fd0tty1tty2tty340tty1tty2tty34E1offset 3 · ref 2i-node0123456789letters.txtE1

fork copies the descriptor table: the child’s fd 3 points to the same E1; the offset is shared, ref count 2.

parent fdchild fd0tty1tty2tty340tty1tty2tty34E1offset 5 · ref 2i-node0123456789letters.txtE1

The child reads from the shared offset (3): 34. E1’s offset becomes 5, for the parent too.

parent fdchild fd0tty1tty2tty340tty1tty2tty34E1offset 5 · ref 2i-node0123456789letters.txtE1

The child prints.

parent fd0tty1tty2tty34E1offset 5 · ref 1i-node0123456789letters.txtE1

exit closes the child’s descriptors: E1’s ref count drops to 1; the offset stays 5. wait returns.

parent fd0tty1tty2tty34E1offset 5 · ref 1E2offset 0 · ref 1i-node0123456789letters.txtE1E2

Opening the file again gives a new entry (E2) with its own offset (0); fd2 = 4.

parent fd0tty1tty2tty34E1offset 5 · ref 1E2offset 2 · ref 1i-node0123456789letters.txtE1E2

fd2 reads from the start of the file: 01. E1 is unaffected.

parent fd0tty1tty2tty34E1offset 8 · ref 1E2offset 2 · ref 1i-node0123456789letters.txtE1E2

The parent reads from fd at offset 5, where the child left it → 567, offset 8.

char b[4]; int n;
int fd = open("letters.txt", O_RDONLY);
read(fd, b, 3);
if (fork() == 0) {
  n = read(fd, b, 2); b[n] = 0;
  printf("child: %s\n", b);
  exit(0);
}
wait(NULL);
int fd2 = open("letters.txt", O_RDONLY);
n = read(fd2, b, 2); b[n] = 0;
printf("new: %s\n", b);
n = read(fd, b, 3); b[n] = 0;
printf("parent: %s\n", b);

Output Question · The File Concept

Question 3: Byte by Byte or Block by Block?

A 6 MiB (6,291,456 bytes) file is copied by the loop below. For (a) BUF = 1 and (b) BUF = 4096, how many read and write calls are made? If each call costs 0.3 µs to enter the kernel, how long does this overhead alone take?

char buf[BUF]; ssize_t n;
while ((n = read(in, buf, BUF)) > 0)
  write(out, buf, n);
Show answer

The last read returns 0 (end of file) and the loop ends: number of reads = data reads + 1.

(a) 6,291,456 + 1 = 6,291,457 reads, 6,291,456 writes → 12,582,913 calls × 0.3 µs ≈ 3.77 s.

(b) 6,291,456 / 4096 = 1536 full blocks → 1537 reads, 1536 writes → 3073 calls ≈ 0.92 ms.

The difference is about 4000-fold. Since 4096 bytes also matches the page and block size, the page cache and the disk work efficiently too. In real code, short writes and −1 returns of write should also be checked.

Multiple Choice · Directories

Question 4: Where Does This Relative Path Lead?

The working directory is /home/elif/hw/os. Which file does the command cat ../../notes/./chapter10.txt open?

D: A relative path starts at the working directory: the first .. → /home/elif/hw, the second .. → /home/elif, notes → /home/elif/notes, . is the current directory, then chapter10.txt. A behaves as if a single .. were applied, B as if three .. were applied; C ignores the .. components.

Step-by-Step Trace · Directories

Question 5: Path Resolution

The first data block of /home/ali/os/report.txt is to be read. Only the root i-node is in the cache and each directory fits in one block. What is the minimum number of disk reads?

i-node 2rootin memoryblock 50home→ 14i-node 14dirblock 120block 120ali→ 37i-node 37dirblock 301block 301os→ 88i-node 88dirblock 512block 512report.txt→ 105i-node 105fileblock 900block 900first datablockdisk reads: 0

The path is absolute: the search starts at the root. The root i-node was brought into memory at mount time, so no disk read is needed.

i-node 2rootin memoryblock 50home→ 14i-node 14dirblock 120block 120ali→ 37i-node 37dirblock 301block 301os→ 88i-node 88dirblock 512block 512report.txt→ 105i-node 105fileblock 900block 900first datablockdisk reads: 1

Directory block 50 is read and home is looked up in it → i-node 14. (Read 1)

i-node 2rootin memoryblock 50home→ 14i-node 14dirblock 120block 120ali→ 37i-node 37dirblock 301block 301os→ 88i-node 88dirblock 512block 512report.txt→ 105i-node 105fileblock 900block 900first datablockdisk reads: 2

i-node 14 is read: a directory, its contents in block 120; the x permission on the directory is checked too. (Read 2)

i-node 2rootin memoryblock 50home→ 14i-node 14dirblock 120block 120ali→ 37i-node 37dirblock 301block 301os→ 88i-node 88dirblock 512block 512report.txt→ 105i-node 105fileblock 900block 900first datablockdisk reads: 3

Directory block 120 is read and ali is looked up in it → i-node 37. (Read 3)

i-node 2rootin memoryblock 50home→ 14i-node 14dirblock 120block 120ali→ 37i-node 37dirblock 301block 301os→ 88i-node 88dirblock 512block 512report.txt→ 105i-node 105fileblock 900block 900first datablockdisk reads: 4

i-node 37 is read: a directory, its contents in block 301; the x permission on the directory is checked too. (Read 4)

i-node 2rootin memoryblock 50home→ 14i-node 14dirblock 120block 120ali→ 37i-node 37dirblock 301block 301os→ 88i-node 88dirblock 512block 512report.txt→ 105i-node 105fileblock 900block 900first datablockdisk reads: 5

Directory block 301 is read and os is looked up in it → i-node 88. (Read 5)

i-node 2rootin memoryblock 50home→ 14i-node 14dirblock 120block 120ali→ 37i-node 37dirblock 301block 301os→ 88i-node 88dirblock 512block 512report.txt→ 105i-node 105fileblock 900block 900first datablockdisk reads: 6

i-node 88 is read: a directory, its contents in block 512; the x permission on the directory is checked too. (Read 6)

i-node 2rootin memoryblock 50home→ 14i-node 14dirblock 120block 120ali→ 37i-node 37dirblock 301block 301os→ 88i-node 88dirblock 512block 512report.txt→ 105i-node 105fileblock 900block 900first datablockdisk reads: 7

Directory block 512 is read and report.txt is looked up in it → i-node 105. (Read 7)

i-node 2rootin memoryblock 50home→ 14i-node 14dirblock 120block 120ali→ 37i-node 37dirblock 301block 301os→ 88i-node 88dirblock 512block 512report.txt→ 105i-node 105fileblock 900block 900first datablockdisk reads: 8

i-node 105 is read: the file itself; the permissions and the address of the first block (900) come from here. (Read 8)

i-node 2rootin memoryblock 50home→ 14i-node 14dirblock 120block 120ali→ 37i-node 37dirblock 301block 301os→ 88i-node 88dirblock 512block 512report.txt→ 105i-node 105fileblock 900block 900first datablockdisk reads: 9

The first data block (900) is read. A total of 9 disk reads: 4 components × 2 + 1. On a second open, the dentry and i-node caches remove most of these.

ino = 2;                   /* root */
i = inode_in_memory(ino);
for (k = 0; k < 4; k++) {
  d = read_block(i.block[0]); /* directory contents */
  ino = lookup(d, name[k]);
  i = read_inode(ino);
}
v = read_block(i.block[0]);   /* first data block */

Multiple Choice · Directories

Question 6: When the Name Is Recreated

echo old > data.txt, ln data.txt hard, ln -s data.txt soft, rm data.txt and finally echo new > data.txt are run. What do cat hard and cat soft print?

A: The hard link points to the old file’s i-node; rm deletes only one name and lowers the link count to 1, so the data stays as “old”. The symbolic link stores only the path “data.txt”: once a new file (a new i-node) is created under that name, the symlink opens it. If data.txt had not been recreated, the symlink would dangle (ENOENT).

Output Question · Directories

Question 7: Reading Link Counts

The following commands are run in an empty directory (ext4). What do the stat lines print? (%h is the link count, %n the name.) Then, if rm d/a is run, what happens to d/b and d/c?

$ mkdir d
$ echo x > d/a
$ ln d/a d/b
$ ln -s a d/c
$ mkdir d/e
$ stat -c '%h %n' d d/a d/b d/c d/e
Show answer
3 d
2 d/a
2 d/b
1 d/c
2 d/e
  • d: the entry in the parent directory + its own . entry = 2; the .. entry of the subdirectory e also points to d → 3.
  • d/a and d/b are two names of the same i-node: 2.
  • d/c is the symbolic link’s own i-node (stat does not follow the link): 1.
  • d/e: the entry in d + its own . = 2.

After rm d/a, the link count of d/b becomes 1 and the data stays; d/c dangles because it looks for the name “a”.

Output Question · Protection

Question 8: Octal Permissions and umask

Find the following.

  1. What do the permissions look like symbolically after chmod 754 script.sh?
  2. What is the octal mode of a file shown as -rwsr-xr-x by ls -l?
  3. With umask 027, what permissions do a new file (default 666) and a new directory (default 777) get?
  4. In /tmp, with mode drwxrwxrwt, why can everyone create files but not delete other people’s files?
Show answer

(a) 7 = rwx, 5 = r-x, 4 = r-- → rwxr-xr--.

(b) The s shows that setuid is set in place of the owner’s x: setuid = 4 → 4755.

(c) umask clears bits: 666 & ~027 = 640 (rw-r-----), 777 & ~027 = 750 (rwxr-x---). Others can access nothing.

(d) The trailing t is the sticky bit (1777): anyone with w on the directory can add entries, but an entry can be deleted or renamed only by the file’s owner (or the directory’s owner, root).

Multiple Choice · Protection

Question 9: Why Can’t the Owner Write?

-r--rw-rw- 1 ali users 120 report.txt. The user ali is also a member of the group users. What happens if ali tries to write to the file?

B: The check order: is the user the owner? → owner rights; if not, in the group? → group rights; otherwise others. The first match is used and later classes are not consulted. Since ali is the owner, r-- applies and the write is denied (EACCES). The owner can grant w to themselves with chmod; the “always writes” in D is true only for root.

Multiple Choice · Protection

Question 10: The x Bit on a Directory

The directory /home/ali has mode drwx--x--x and the file plan.txt inside it has mode -rw-r--r--. Which statement is true for an ordinary user other than ali?

B: On a directory, r allows listing entries, w adding or deleting entries, and x entering it and looking up names. Others have only x: if the name is known, the path resolves and the file’s own r bit allows reading. ls fails; creating a file would need w on the directory.

Multiple Choice · Disk Layout

Question 11: A 3 TiB Disk and MBR

A new 3 TiB disk with 512-byte sectors is partitioned with an MBR partition table. What happens?

B: MBR partition entries store the start and length as 32-bit sector numbers: 2³² × 512 B = 2 TiB. No partition can be defined on the remaining 1 TiB. GPT uses 64-bit LBA, recognizes 128 partitions by default, and is part of the UEFI standard; it is required for disks over 2 TiB.

Output Question · Disk Layout

Question 12: Where Is i-node 100 on Disk?

In a file system, the block is 4 KiB, the i-node is 256 bytes, and i-node numbers start at 0. According to the superblock, the i-node table starts at block 6. The disk is addressed in 512-byte sectors.

  1. How many i-nodes fit in one block?
  2. In which block, and at which byte within the block, does i-node 100 start?
  3. In which sector, and at which byte within the sector, does it start?
Show answer

(a) 4096 / 256 = 16 i-nodes per block (2 per sector).

(b) Offset from the start of the table: 100 × 256 = 25,600 = 6 × 4096 + 1024 → block 6 of the table (counting from 0); disk block = 6 + 6 = block 12, at byte 1024 within the block (i-node number 4 of that block: 100 mod 16 = 4).

(c) Byte address 12 × 4096 + 1024 = 50,176 → 50,176 / 512 = sector 98, at byte 0 within it.

This calculation cannot be done without the superblock’s “where is the i-node table” information; ext4 also splits the table across block groups.

Step-by-Step Trace · Allocation Methods

Question 13: Following a FAT Chain

The cluster size is 4 KiB. The first cluster of slides.pptx is 5. In which clusters does the file reside, and in which cluster is byte 9000? Clusters are numbered from 0.

0—1—21137405960788EOF9210011EOF120130140150chain: —

The FAT is in memory; 0 is free, — is a reserved cluster. The directory entry of slides.pptx gives the first cluster as 5. Byte 9000: 9000 / 4096 = 2 → logical cluster 2.

0—1—21137405960788EOF9210011EOF120130140150chain: 5

n = 0: cluster 5 is the file’s cluster 0. FAT[5] = 9 → next is 9.

0—1—21137405960788EOF9210011EOF120130140150chain: 5 → 9

n = 1: cluster 9 is the file’s cluster 1. FAT[9] = 2 → next is 2.

0—1—21137405960788EOF9210011EOF120130140150chain: 5 → 9 → 2

n = 2: cluster 2 is the file’s cluster 2 — the target (byte 9000 is here). FAT[2] = 11 → next is 11.

0—1—21137405960788EOF9210011EOF120130140150chain: 5 → 9 → 2 → 11

n = 3: cluster 11 is the file’s cluster 3. FAT[11] = EOF → no next (end of file).

0—1—21137405960788EOF9210011EOF120130140150chain: 5 → 9 → 2 → 11

k = EOF, the loop ends. The chain 5 → 9 → 2 → 11: 4 clusters (at most 16 KiB), scattered on the disk. Byte 9000 is in cluster 2, at byte 9000 − 2 × 4096 = 808 within it.

k = 5;              /* first cluster */
n = 0;
while (k != EOF) {
  if (n == 9000 / 4096) target = k;
  n++;
  k = FAT[k];        /* table in memory */
}

Classic Question · Allocation Methods

Question 14: The Same Block, Four Methods

Logical block 699 (counting from 0) of a 1000-block file is to be read. The directory entry and the i-node are in memory, and nothing else is cached. The block is 4 KiB, a pointer is 4 bytes, and the i-node has 12 direct, 1 single, 1 double and 1 triple indirect pointer. How many disk reads does each method need?

  1. Contiguous allocation
  2. Linked list (pointer inside the block)
  3. FAT (table in memory)
  4. i-node
Show answer
methodreadswhy
contiguous1address = start + 699
linked list700the next address is unknown until blocks 0…698 are read
FAT1699 steps are followed in memory, then a single read
i-node2699 − 12 = 687 < 1024 → one single-indirect block + data

A linked list needs O(n) disk reads for random access; FAT moves the pointers into memory and reduces this to O(n) memory steps, but the table grows with the disk size. The i-node table is in memory only for open files; contiguous allocation is the fastest but causes external fragmentation and the file cannot grow.

Output Question · Allocation Methods

Question 15: Indirect Blocks

An i-node has 10 direct, 1 single-indirect and 1 double-indirect pointer. The block is 2 KiB and a block address is 4 bytes.

  1. What is the maximum size of a file in bytes? Which part dominates?
  2. In which logical block of the file is byte number 5,000,000, and through which pointer path is it found? If the i-node is in memory, how many disk reads are needed?
Show answer

Pointers per block: 2048 / 4 = 512.

(a) 10 + 512 + 512² = 10 + 512 + 262,144 = 262,666 blocks × 2048 = 537,939,968 bytes (about 513 MiB). The double-indirect part (512 MiB) dominates; the direct blocks cover only 20 KiB, the single-indirect 1 MiB.

(b) ⌊5,000,000 / 2048⌋ = 2441, at byte 832 within the block. 0–9 are direct, 10–521 single-indirect; 2441 ≥ 522 → double-indirect, index 2441 − 522 = 1919 = 3 × 512 + 383: entry 3 of the first level, entry 383 of the second level. Reads: double-indirect block + single-indirect block + data = 3.

Multiple Choice · Directory Implementation

Question 16: Searching a Large Directory

A name is looked up in a directory with 100,000 entries. Which statement about the ways of storing directory entries is correct?

A: A linear list is simple but every search scans from the start. A hash picks the bucket from the name’s digest; collisions are resolved by chaining, and with a fixed bucket count the chains grow in a growing directory. A hash does not preserve order (B is wrong). ext4 uses htree, and NTFS, XFS and Btrfs use B+ trees: O(log n) search and sorted listing together. In a list, deletion marks the entry empty or moves the last entry.

Output Question · Free-Space Management

Question 17: Bitmap and Counting

(a) A 512 GiB disk is divided into 4 KiB blocks. How many bytes and how many blocks does the bitmap that tracks free space take?

(b) On a small 32-block disk, the free blocks are 3–6, 10–11, 20–24 and 30. Show them with the counting method (start, length) and with a bitmap (1 = free, block 0 at the left). Which representation stays small when the disk is very full?

Show answer

(a) 512 GiB / 4 KiB = 2³⁹ / 2¹² = 2²⁷ = 134,217,728 blocks → 2²⁷ bits = 2²⁴ bytes = 16 MiB = 16 MiB / 4 KiB = 4096 blocks (0.003% of the disk).

(b) Counting: (3, 4) (10, 2) (20, 5) (30, 1).

Bitmap: 00011110 00110000 00001111 10000010

A bitmap has a fixed size however full the disk is and makes finding contiguous free blocks easy. A list and counting hold only the free blocks, so they shrink as the disk fills; counting is also very short when the free space is in contiguous runs.

Multiple Choice · Performance

Question 18: write() Returned, Where Is the Data?

A program wrote a record with write() and the call returned successfully; two seconds later the power failed and the record was lost. Why, and what is the right way to make the record durable?

D: A write-back cache accumulates dirty pages; Linux writes pages that have been dirty for more than 30 seconds in the background. fsync (or O_SYNC) waits until the data reaches durable storage. close() alone does not guarantee writing to disk. The safe-save pattern: write to a temporary file → fsync → rename.

Output Question · Consistency and Recovery

Question 19: fsck Counters

fsck read all the i-nodes and the free-block list and kept two counters for each block. Which blocks have problems, what does each mean, and how is each fixed? Which is the most dangerous?

block0123456789
in use1011021001
in free list0100100020
Show answer

In a consistent state, in use + free = 1 for every block. Three blocks break this rule:

  • Block 7 (0, 0): a missing block. Harmless but wasted space; it is added to the free list.
  • Block 8 (0, 2): twice in the free list. The same block could be given to two files; the free list is rebuilt. (With a bitmap this situation could not arise.)
  • Block 5 (2, 0): two files use the same data block. The most dangerous: the block is copied, one of the files is given a separate block, and the user is informed.

Also, a block seen both in use and in the free list (1, 1) would be removed from the free list.

Multiple Choice · Consistency and Recovery

Question 20: Where Did the Journal Stop?

In a journaling file system a transaction proceeds in this order: (1) TxB and the changed metadata blocks go to the journal, (2) the end-of-transaction record TxE goes to the journal, (3) the blocks are written to their final places, (4) the journal entry is released. The power fails after (2) has finished, during (3). What happens at boot?

C: If TxE is on disk, the transaction is committed: during recovery the blocks in the journal are rewritten to their final places (redo). Writing the same content twice is harmless; transactions are idempotent. Had the power failed before TxE was written, the transaction would be ignored (A). In both cases the file system stays consistent, and boot takes seconds without needing fsck.

Multiple Choice · VFS and Network

Question 21: The Same read(), Different Code

A process reads two files, one on ext4 and one on NFS, with the same read() call. How does the kernel pick the right code?

C: At mount time each file system gives the VFS its own operation tables; the file object created at open carries this table. On a read call the VFS simply calls the pointer file->f_op->read_iter: for an ext4 file, ext4’s function runs; for an NFS file, an RPC goes over the network. The application sees no difference; the extra cost is a single indirect call.

The End

Chapter 10: File Systems: Study Questions
Lecture slides: Chapter 10: File Systems · Lecture notes
1 / 1 Sercan KÜLCÜ. All rights reserved.