CSE Roadmap

Core tier · about 3 hours · 7 parts

Part 4 of 7 · 4 min read

Hard links

Think of one house with two street addresses, one on each of two streets. Both addresses lead to the same rooms. The house stays as long as at least one address is in use. The analogy stops in one place: a house has a fixed number of addresses by law, while a file's link count can be any size up to a limit.

In plain words: A hard link is a directory entry that points to an existing inode. link("a.txt", "hard.txt") adds a second entry for inode 12. The two names are equal. Neither is the original. The inode keeps a link count, which is the number of entries that point to it. unlink removes one entry and lowers the count by one. When the count reaches 0 and no process has the file open, the OS frees the inode and its data blocks.

A worked example. The program makes a file, adds a hard link and a symbolic link, and then removes the first name.

#include <fcntl.h>
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
#include <unistd.h>

static void show(const char *label, const char *path, int follow) {
    struct stat st;
    int r = follow ? stat(path, &st) : lstat(path, &st);
    if (r < 0) { printf("%-22s cannot stat (%s)\n", label, "no such target"); return; }
    printf("%-22s inode %lu, links %lu, size %ld, type %s\n", label,
           (unsigned long)st.st_ino, (unsigned long)st.st_nlink, (long)st.st_size,
           S_ISLNK(st.st_mode) ? "symlink" : S_ISDIR(st.st_mode) ? "directory" : "file");
}

int main(void) {
    int fd = open("a.txt", O_CREAT | O_WRONLY | O_TRUNC, 0644);
    if (write(fd, "hello\n", 6) != 6) return 1;
    close(fd);
    show("a.txt", "a.txt", 0);
    link("a.txt", "hard.txt");                        /* a second name for the same inode */
    symlink("a.txt", "soft.txt");                      /* a small file that holds a path */
    show("a.txt after link", "a.txt", 0);
    show("hard.txt", "hard.txt", 0);
    show("soft.txt (lstat)", "soft.txt", 0);
    show("soft.txt (stat)", "soft.txt", 1);
    unlink("a.txt");                                   /* remove one name */
    show("hard.txt after unlink", "hard.txt", 0);
    show("soft.txt now", "soft.txt", 1);
    char buf[16]; ssize_t n = readlink("soft.txt", buf, sizeof buf);
    printf("readlink soft.txt: \"%.*s\" (%zd bytes)\n", (int)n, buf, n);
    unlink("hard.txt"); unlink("soft.txt");
    return 0;
}

Output on the test machine:

a.txt                  inode 12, links 1, size 6, type file
a.txt after link       inode 12, links 2, size 6, type file
hard.txt               inode 12, links 2, size 6, type file
soft.txt (lstat)       inode 13, links 1, size 5, type symlink
soft.txt (stat)        inode 12, links 2, size 6, type file
hard.txt after unlink  inode 12, links 1, size 6, type file
soft.txt now           cannot stat (no such target)
readlink soft.txt: "a.txt" (5 bytes)

After link, both names show inode 12 and a count of 2. After unlink("a.txt") the data is still there under hard.txt, with a count of 1.

The key lines. Hard links have two limits. They cannot cross file systems, because an inode number is meaningful only inside one file system. Most systems do not allow a hard link to a directory, because it could make loops that the tools cannot handle.

Hard link

A hard link is a directory entry that refers to an inode that already has another entry. All the names of one inode are equal, and the file's data is freed only after the last name is removed and the last open handle is closed.

Example: after link("a.txt", "hard.txt") the inode 12 has two names and a link count of 2.

Check yourself: 1. What does link change in the metadata of the file? 2. Why can a hard link not point to a file on another file system? 3. After the last unlink, what else must be true before the data blocks are freed?