COMS 4995 Advanced Systems Programming

UNIX Filesystem

Filesystem Hierarchy

A filesystem hierarchy is composed of disjoint filesystems grafted (i.e., mounted) onto one unified tree. Directories and files are created within a filesystem. The UNIX filesystem hierarchy starts at /, the root directory.

mount -l lists all mounted filesystems on the system. Here are a few notable entries on SPOC:

There are other kinds of mounts:

Berkeley Fast File System (FFS) Layout

Recall inode data structure, the internal representation of a file.

The following diagram, APUE Figure 4.13, shows the layout of FFS as if it were formatted onto one partition of a storage device:

ffs-layout

Disk drive partitions

Disk drive can be paritioned into multiple operating systems

Within a single OS, can also partition disk into several filesystems

File system

Occupies a disk partition. At the top-level of FFS we have:

Cylinder group

Maintains copy of the superblock and some cylinder group metadata for performance. The crucial parts of the filesystem are:

Inodes and Data Blocks

ffs-inode-and-data

A given inode in the inode array represents a single file

Directories are pretty much just “special” files – they also occupy data blocks. A directory’s data block houses directory entries:

Given an empty directory (represented by inode 1267 below), let’s mkdir testdir:

mkdir-ffs-inode-and-data

Notice the dentries for the directory with ino 1267:

Directory with ino 2549:

Notice how ino 1267 is referred to by:

Notice how ino 2549 is referred to by:

Files can have different names but refer to the same inode!

$ # starting off with an empty dir
$ ls -al
total 8
drwx------ 2 hans hans 4096 Apr  7 22:30 .
drwx------ 9 hans hans 4096 Apr  7 22:30 ..
$ echo howdy > f1
$ # f1 is 6 bytes: howdy + newline
$ ls -al
total 12
drwx------ 2 hans hans 4096 Apr  7 22:31 .
drwx------ 9 hans hans 4096 Apr  7 22:30 ..
-rw------- 1 hans hans    6 Apr  7 22:31 f1
$ # we can create a hard link to f1 using ln
$ ln f1 f2
$ ls -al
total 16
drwx------ 2 hans hans 4096 Apr  7 22:33 .
drwx------ 9 hans hans 4096 Apr  7 22:30 ..
-rw------- 2 hans hans    6 Apr  7 22:31 f1
-rw------- 2 hans hans    6 Apr  7 22:31 f2
$ # f2 is not a copy or shortcut, refers to the same inode as f1
$ ls -ali
total 16
1154149 drwx------ 2 hans hans 4096 Apr  7 22:33 .
1028714 drwx------ 9 hans hans 4096 Apr  7 22:30 ..
1027866 -rw------- 2 hans hans    6 Apr  7 22:31 f1
1027866 -rw------- 2 hans hans    6 Apr  7 22:31 f2
$ # let's create a dir and make a hard link to f2 in there
$ mkdir d1
$ cd d1
$ ln ../f2 f3
$ # notice f3 has the same inode number
$ # also notice link count is 3, used be 2
$ # three dentries refer to the ino 1027866
$ ls -ali
total 12
1154150 drwx------ 2 hans hans 4096 Apr  7 22:34 .
1154149 drwx------ 3 hans hans 4096 Apr  7 22:34 ..
1027866 -rw------- 3 hans hans    6 Apr  7 22:31 f3
$ # let's inspect d1 now
$ cd ..
$ ls -ali
total 20
1154149 drwx------ 3 hans hans 4096 Apr  7 22:34 .
1028714 drwx------ 9 hans hans 4096 Apr  7 22:30 ..
1154150 drwx------ 2 hans hans 4096 Apr  7 22:34 d1
1027866 -rw------- 3 hans hans    6 Apr  7 22:31 f1
1027866 -rw------- 3 hans hans    6 Apr  7 22:31 f2
$ # d1's link count is 2: the "." inside of it and "d1" from the parent dir
$ # let's try creating a hard link to d1
$ ln d1 d2
ln: d1: hard link not allowed for directory
$ # fs typically disallows users from creating hard link to dir to prevent buggy
$ # code from going into infinite traversal. hard links to dirs do exist though (. and ..)
$ # also can't create hard links to files outside fs... different inodes!
$ # 
$ # side note: link count is the reason why there is no delete() -- just `unlink()`
$ #
$ # let's mess with symlinks now:
$ ln -s f1 s1
$ ls -ali
total 20
1154149 drwx------ 3 hans hans 4096 Apr  7 22:40 .
1028714 drwx------ 9 hans hans 4096 Apr  7 22:30 ..
1154150 drwx------ 2 hans hans 4096 Apr  7 22:34 d1
1027866 -rw------- 3 hans hans    6 Apr  7 22:31 f1
1027866 -rw------- 3 hans hans    6 Apr  7 22:31 f2
1028812 lrwxrwxrwx 1 hans hans    2 Apr  7 22:40 s1 -> f1
$ # notice that symlink is a special file with different ino.
$ # file content is literally path to the targetted file
$ # can create symlinks to directories and files outside fs because this really is a shortcut 

Summary


Last updated: 2024-04-10