[ Previous | Next | Table of Contents | Index | Library Home | Legal | Search ]

General Programming Concepts: Writing and Debugging Programs

Linking for Programmers

A link is a connection between a file name and an i-node (hard link) or between file names (symbolic link). Linking allows access to an i-node (Working with JFS i-nodes) from multiple file names. Directory entries pair file names with i-nodes. File names are easy for users to identify, and i-nodes contain the real disk addresses of the file's data. A reference count of all links into an i-node is maintained in the i_nlink field of the i-node. Subroutines that create and destroy links use file names, not file descriptors (Using File Descriptors). Therefore, it is not necessary to open files when creating a link.

Processes can access and change the contents of the i-node by any of the linked file names. Two kinds of links exist in this operating system, hard links and symbolic links.

Hard Links

link Subroutine that creates hard links.The presence of a hard link guarantees the existence of a file because a hard link increments the link count in the i_nlink field of the i-node.
unlink Subroutine that releases links. When all hard links to an i-node are released, the file is no longer accessible.

The user ID that created the original file owns the file and retains access mode authority over the file. Otherwise, all hard links are treated equally by the operating system. Hard links must link file names and i-nodes within the same file system since the i-node number is relative to a single file system.

Hard links always refer to a specific file because the directory entry created by the hard link pairs the new file name to an i-node.

Example: If the /u/tom/bob file is linked to the /u/jack/foo file, the link count in the i_nlink field of the foo file is 2. Both hard links are equal. If /u/jack/foo is removed, the file continues to exist by the name /u/tom/bob and can be accessed by users with access to the tom directory. However, the owner of the file is jack even though /u/jack/foo was removed. The space occupied by the file is charged to jack's quota account. Change file ownership using the chown subroutine.

Symbolic Links

symlink Subroutine that creates symbolic links

Symbolic links are implemented as a file that contains a path name. When a process encounters a symbolic link, the path contained in the symbolic link is prepended to the path the process was searching. If the path name in the symbolic link is an absolute path name, the process searches from the root directory for the named file. If the path name in the symbolic link does not begin with a / (slash), the process interprets the rest of the path relative to the position of the symbolic link. The unlink subroutine also removes symbolic links.

Symbolic links can traverse file systems because they are treated as regular files by the operating system rather than as part of the file system structure. The presence of a symbolic link does not guarantee the existence of the target file because a symbolic link has no effect on the i_nlink field of the i-node.

readlink Subroutine that reads the contents of a symbolic link. Many subroutines (including the open and stat subroutines) follow symbolic paths.
lstat Subroutine created to report on the status of the file containing the symbolic link and does not follow the link. See the symlink subroutine for a list of subroutines that traverse symbolic links.

Symbolic links are also called soft links because they link to a file by path name. If the target file is renamed or removed, the symbolic link cannot resolve.

Example: The symbolic link to /u/joe/foo is a file that contains the literal data /u/joe/foo. When the owner of the foo file removes this file, subroutine calls made to the symbolic link cannot succeed. If the file owner then creates a new file named foo in the same directory, the symbolic link leads to the new file. Therefore, the link is considered soft because it is linked to interchangeable i-nodes.

In the ls -l command listing, an l in the first position indicates a linked file. In the final column of that listing, the links between files are represented as Path2 -> Path1 (or Newname -> Oldname).

unlink Subroutine that removes a directory entry. The Path parameter in the subroutine identifies the file to be disconnected. At the completion of the unlink call, the link count of the i-node is reduced by the value of 1.
remove Subroutine that also removes a file name by calling either the unlink or rmdir subroutine.

Directory Links

mkdir Subroutine that creates directory entries for new directories, which creates hard links to the i-node representing the directory

Symbolic links are recommended for creating additional links to a directory. Symbolic links do not interfere with the . and .. directory entries and will maintain the empty, well-formed directory status. See the Understanding Directory Links "figure" for a graphic example of the empty, well-formed directory /u/joe/foo and the i_nlink values.



j o e 0

mkdir ("foo", 0666)


n 0 0 0

n n 0 0

f o o 0



n 0 0 0

n n 0 0

i_nlink Values

i = 68
n_link 3

For i = 68, the n_link value is 3 (/u; /u/joe; /u/joe/foo).

i = 235
n_link 2

For i = 235, the n_link value is 2 (/u/joe; /u/joe/foo).

Understanding Directory Links

rmdir or remove Remove links to directories

Related Information

Chapter 5, File Systems and Directories

JFS Directories

Working with File I/O

ls command

chown subroutine, link subroutine, statx, lstat, stat, fstatx, fstat, fullstat, or ffullstat subroutine, open, openx, or creat subroutine, symlink subroutine, remove subroutine, rmdir subroutine unlink subroutine

[ Previous | Next | Table of Contents | Index | Library Home | Legal | Search ]