10 - File System Organization

Class: CSCE-313


Notes:

Outline

  1. File System Abstraction
  2. File System Organization
  3. UNIX File API
  4. Hard links vs. soft links

File System Abstraction

The file system abstraction

Notes:

00 - TAMU Brain/6th Semester (Spring 26)/CSCE-313/Lecture/Visual Aids/image-7.png465

Example:

An example data layout on disk

Consider the disk to be a linear sequence of blocks.

00 - TAMU Brain/6th Semester (Spring 26)/CSCE-313/Lecture/Visual Aids/image-8.png500

Notes:

Data region in a file system

How do we store these inodes in the file system?

Notes:

Inode table in a file system

00 - TAMU Brain/6th Semester (Spring 26)/CSCE-313/Lecture/Visual Aids/image-10.png500

Notes:

Allocation Structures

00 - TAMU Brain/6th Semester (Spring 26)/CSCE-313/Lecture/Visual Aids/image-11.png500

Notes:

Super Block

00 - TAMU Brain/6th Semester (Spring 26)/CSCE-313/Lecture/Visual Aids/image-12.png500

Notes:

File Organization

The inode

2026-03-18_14-09-24.png

Notes:

File System Layout

How do you build a hierarchy (tree) of files from the flat model of data blocks?

00 - TAMU Brain/6th Semester (Spring 26)/CSCE-313/Lecture/Visual Aids/image-14.png449

Notes:

Characteristics of a magnetic disk

00 - TAMU Brain/6th Semester (Spring 26)/CSCE-313/Lecture/Visual Aids/image-15.png331x256

Why are magnetic disks slow?

How a block is read from disk

  1. The arm moves the disk head to the right track. This is a "seek". Extremely slow (10 ms).
  2. Then, the head waits for the correct sector to come below. This is relatively faster because of constant motion (<<10 ms), determined by disk RPM.
  3. Read the sector and send to the CPU. Much faster (SATA rate: 500MB/s ).

Notes:

Building a hierarchy

00 - TAMU Brain/6th Semester (Spring 26)/CSCE-313/Lecture/Visual Aids/image-16.png327

Filesystems terms

00 - TAMU Brain/6th Semester (Spring 26)/CSCE-313/Lecture/Visual Aids/image-17.png348

Notes:

Hard link example:

> ln foo.md bar.md
> ls -li foo.md bar.md
69312997 -rw-r--r--  2 macc  staff  0 Mar 18 14:37 bar.md
69312997 -rw-r--r--  2 macc  staff  0 Mar 18 14:37 foo.md

Soft link example:

> ln -s foo.md fib.md
> ls -li foo.md fib.md
69313223 lrwxr-xr-x  1 macc  staff  6 Mar 18 14:38 fib.md -> foo.md
69312997 -rw-r--r--  2 macc  staff  0 Mar 18 14:37 foo.md

Hard link

Soft link or Symbolic link

00 - TAMU Brain/6th Semester (Spring 26)/CSCE-313/Lecture/Visual Aids/image-22.png400

shell command

ln /dirA/name1 /dirB/name2

is typically implemented using the link system call:

#include <stdio.h>
#include <unistd.h>

if (link(“/dirA/name1”, “/dirB/name2”) == -1)
perror(“failed to make new link in /dirB”);

00 - TAMU Brain/6th Semester (Spring 26)/CSCE-313/Lecture/Visual Aids/image-23.png429

  1. inode #
  2. links

00 - TAMU Brain/6th Semester (Spring 26)/CSCE-313/Lecture/Visual Aids/image-24.png400

#include <stdio.h>
#include <unistd.h>

if (unlink(“/dirA/name1”) == -1)
perror(“failed to delete link in /dirA”);

if (unlink(“/dirB/name2”) == -1)
perror(“failed to delete link in /dirB”);

File System Organization

00 - TAMU Brain/6th Semester (Spring 26)/CSCE-313/Lecture/Visual Aids/image-25.png600

image-72.png

After $mkdir testdir

image-73.png

Links: example
image-74.png

File system "Tree"

hard link: The mapping between the name and the underlying file

image-75.png

$ ln <existing file> <link>

UNIX Directory API

Current Directory

#include <unistd.h>

char * getcwd(char * buf, size_t size);
/* get current working directory */

Example:

void main(void) {
    char mycwd[PATH_MAX];
    if (getcwd(mycwd, PATH_MAX) == NULL) {
        perror ("Failed to get current working directory");
        return 1;
    }
    printf("Current working directory: %s\n", mycwd);
    return 0;
}

Notes:

Open, Read, Close

Read is stateful with a cursor.

Reading the same directory again gives back the next file in the directory.

#include <dirent.h>
int main(int argc, char * argv[]) {
    struct dirent *direntp;
    DIR *dirp = opendir(argv[1]);
    while((direntp = readdir(dirp)) != NULL)
        printf("%s\n", direntp->d_name);
        
    closedir(dirp);
    return 0;
}

Notes:

Traversal

Read is stateful with a cursor

#include <dirent.h>
DIR* opendir(const char *dirname);
/* returns pointer to directory object */
struct dirent *readdir(DIR *dirp);
/* read successive entries in directory 'dirp' */
int closedir(DIR *dirp);
/* close directory stream */
void rewinddir(DIR *dirp);
/* reposition pointer to beginning of directory */

Notes:

File System Organization

A disk drive is divided into one or more partitions.

Notes:

Partition

Super block Bitmaps inode table Data Area
Component Purpose
Superblock Contains metadata about the file system. Size is FS dependent. (File system type, size, sizes of block groups and location of inode tables etc.)
Bitmaps Use/free indicator of inodes & data blocks
inode table An array of inode structs, where each struct contains info about a file object (e.g., size, owner id, last modification). Each inode has an unique number, which is also the index into the inode table
Data Area Contains file content. Each file can be >=1 block

struct inode

struct inode {
	unsigned long i_ino;
	umode_t i_mode;
	unsigned int i_nlink;
	uid_t i_uid;
	gid_t i_gid;
	loff_t i_size;
	time_t i_atime;
	time_t i_mtime;
	time_t i_ctime;
	union {
		struct ext3_inode_info ext3_i;
		struct ntfs_inode_info ntfs_i;
	} u;
};
struct ext3_inode_info {
	__u32 i_data[15];
};

Steps for creating & writing a new file

  1. Store Properties:
    • Look for a free inode and sore metadata (e.g., permissions, size, creation data) in the inode.
  2. Store data and record allocations:
    • Look for enough free disk blocks and copy content
    • Update inode with block #s
  3. Add file name to directory:
    • Store the (inode#, filename) pair in the directory entry
  4. You could crash anywhere. The FS provides all-or-nothing guarantee.

Example:
Let us create a file called "newfile" that is 12 KB in size. A disk block is 4 KB .

First, we need a free inode to put the file metadata, then find 3 free disk blocks to put the actual data

image-77.png

Steps for reading a file

  1. Search the current directory for the file name and extract its inode
  2. Locate and read the inode
  3. Find the data block number from there
  4. Read each data block in sequence and output that

This is how the cat file command works.

The output goes to stdout.

inode's features: protection

File owner/creator should be able to control:

Types of access:

Block indirection

File Structure: Indexed Allocation

image-76.png543

Notes:

Block indirection

Why use block indirection?

image-78.png508

FFS: Data Storage

Small files: 12 pointers direct to data blocks:
image-79.png506

Large files: 1,2,3 level indirect pointers:
image-80.png508

Files: Big Picture

image-81.png611

Notes: