Slide 1

Slide 1 text

Hard links and soft links Aaron Meurer

Slide 2

Slide 2 text

First understand a little bit about how filesystems work • Every file system is a little different, but the basic ideas are similar. I’ll be describing a very rough version of a (logical) Unix filesystem. • Filesystems are broken up into blocks • Blocks are the smallest unit of data that the filesystem deals with

Slide 3

Slide 3 text

Examples • On Unix, use df

Slide 4

Slide 4 text

• There are two basic types of blocks: • data blocks • inode blocks (metadata) • inode blocks represent files (or directories). • Every inode has a unique inode number • A file inode holds all the metadata for that file, as well as pointers on disk to all the data blocks for that file (one possibility, could also be a linked list)

Slide 5

Slide 5 text

• Directory inodes list all the inode numbers for the files in that directory • Block locations of all inodes are usually organized in an inode table so that they can be found by number

Slide 6

Slide 6 text

File inode example inode 41335 link count: 4 mtime … block list data data data

Slide 7

Slide 7 text

File inode example (cont.) • The inode contains a lot of metadata about the file (mtime, permissions, size) • But note that it does NOT contain the file name or directory location of the file

Slide 8

Slide 8 text

Directory inode example directory inode 41335 type: directory name: mydir pardir: 24312 mtime … inode table file name inode stuff.txt 23351 music.mp3 32154 presentation .pdf 41335 presentation 2.pdf 41335 … …

Slide 9

Slide 9 text

Directory inode example (cont.) • Notice that I have two files with the same inode (presentation.pdf and presentation2.pdf both have inode 41335) • Go back to the original file inode example

Slide 10

Slide 10 text

File inode example inode 41335 link count: 4 mtime … block list

Slide 11

Slide 11 text

Directory inode example (cont.) • The file has a link count of 4, meaning that there are four directory listings somewhere in the filesystem referencing it. • Whenever a file is deleted, it’s inode count is decremented. When the count reaches 0, the data is freed.

Slide 12

Slide 12 text

Hard links • A “hard link” is just a reference to an inode • Every file is a hard link! • When two files are “hard links to each other,” they are the exact same inode (i.e., data) on disk.

Slide 13

Slide 13 text

Hard links (cont.) inode number link count

Slide 14

Slide 14 text

Hard links (cont.) • If we read or write to the files, they will both be the same • Note that some text editors intentionally break hard links

Slide 15

Slide 15 text

Symlinks • A symbolic link (symlink) is a special kind of inode that references a path • Alternately a special entry in a directory listing • Paths can usually be relative or absolute • Symlinks are separate files from the files they point to • Most system calls read through symlinks by default (act as if they were called on the file the symlink points to)

Slide 16

Slide 16 text

A Python analogy Hard links are like this Symlinks are like this

Slide 17

Slide 17 text

A Python analogy (cont.) • Link counts are like reference counts • del a doesn’t delete a, it lowers its reference count. a is deleted when its reference count reaches 0. • Inode numbers are like id(a).