File System
1. Experiment Objectives
The file system (p583, programming problem) checks the relationship between files and inode on UNIX or Linux systems. On these systems, files are represented by inodes. In other words, an inode is a file (and vice versa). You can complete this exercise on the Linux virtual machine provided in this document. You can also complete the exercise on any Linux, UNIX, or Mac OS X system, but you need to create two simple text files named file1.txt and file3.txt, with the content being a single unique sentence.
2. Related Principles and Knowledge
- The file system and inode: The operating system uses the file system to manage files stored on the disk. The file system is responsible for creating, deleting, reading, and writing files. Inodes: Each file in the file system has a unique inode, which contains metadata about the file (such as file size, permissions, creation time, etc.) and the location of the file data blocks. Through inodes, the operating system can quickly access relevant information about files.
3. Experiment Process
Hard Links:
- File content

- Use the following command to get the inode number of the file: ls -li file1.txt

The inode number of the file is 5774966.
- Use the following command to create a hard link between file1.txt and file2.txt: ln file1.txt file2.txt

As shown, the inode number of file1.txt is 5774966, and the inode number of file2.txt is also 5774966. The inode numbers are the same. Checking the content, both files are identical.
- Next, edit file2.txt and change its content. After completion, check the content of file1.txt.

Checking the content, the files are still identical.
- Next, enter the following command to delete file1.txt: rm file1.txt

After deleting file1.txt, file2.txt still exists.
- Then, delete file2.txt by entering the following command: strace rm file2.txt. The strace command traces the system calls made during the execution of the rm file2.txt command.

Explanation:
List of system calls
execve:execve(“/usr/bin/rm”, [“rm”, “file2.txt”], NULL),0x7fffdc5b5487);Used to execute the program and load the rm command.
brk: brk(NULL)
Used to adjust the size of the program’s data segment, typically for memory allocation.
mmap: mmap(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0)
Used to map a memory area, allocating anonymous memory.
openat: openat(AT_FDCWD, “/etc/ld.so.cache”, O_RDONLY|O_CLOEXEC), opens the dynamic link library cache file.
openat(AT_FDCWD, “/lib/x86_64-linux-gnu/libc.so.6”, O_RDONLY|O_CLOEXEC). Opens the C standard library.
fstat: fstat(3, st_mode=S_IFREG)
Gets file status information.
pread64: pread64(3, “…”, 784, 64)
Reads data from the opened file.
mmap (again): mmap(NULL, 2055640, PROT_READ, MAP_PRIVATE|MAP_DENYWRITE, 3, 0)
Maps the library file into memory.
mprotect: mprotect(0x7f91ebb38000, 16384, PROT_READ)
Modifies memory protection attributes.
geteuid: geteuid()
Gets the effective user ID.
newfstatat: newfstatat(AT_FDCWD, “file2.txt”, st_mode=S_IFREG, AT_SYMLINK_NOFOLLOW)
Gets the status information of file2.txt.
faccessat2: faccessat2(AT_FDCWD, “file2.txt”, W_OK, AT_EACCESS)
Checks if the current user has permission to write (delete) the file.
unlinkat: unlinkat(AT_FDCWD, “file2.txt”, 0)
Deletes the specified file.
close: close(0) closes standard input.
close(1) closes standard output.
close(2) closes standard error.
exit_group: exit_group(0)
Ends the process and returns the status code.
Soft Links:
- A soft link (or symbolic link) creates a new file that “points to” the name of the file it links to. In this text, you can create a soft link to file3.txt by entering the following command: ln -s file3.txt file4.txt

As shown, the inode number of file.txt is 5774966, and the inode number of a.txt is 5774978. The inode numbers are different.
- Next, edit the content of file4.txt. Check that the content of file3.txt has also changed.
As shown, the result of file.txt has also changed.
- Finally, delete file3.txt. Observe what happens.

After deletion, the content of a.txt does not exist, and checking the file content results in an error, but the inode number information still exists.
4. Experiment Results and Analysis
- The structure of the file system: The operating system uses the file system to organize and manage data stored on the disk. The file system is responsible for creating, deleting, reading, and writing files. Common file systems include NTFS, FAT32, ext4, etc. Inodes: Each file in the file system has a unique inode, which contains metadata about the file (such as file size, permissions, creation time, etc.) and the location of the file data blocks. Through inodes, the operating system can quickly access relevant information about files.
5. Problem Summary
- Confusion between hard links and soft links: Solution: When creating links, clearly use ln (for hard links) and ln -s (for soft links) commands, and clearly annotate in the experiment records. Before conducting the experiment, first understand the basic principles of hard links and soft links to ensure understanding of their differences and use cases. During the experiment, use ls -l command to check the link status of files, confirming the type and target of the link.
6. Source Code
# 1. Create test file
touch experiment_file.txt
echo "This is the initial content of the experiment file." > experiment_file.txt
# 2. View file content
cat experiment_file.txt
# 3. Create hard link
ln experiment_file.txt hard_link.txt
# 4. Create soft link
ln -s experiment_file.txt soft_link.txt
# 5. View link information
ls -l
# 6. Modify file content
echo "This is the modified content." > experiment_file.txt
# 7. View content of hard link and soft link
cat hard_link.txt
cat soft_link.txt
# 8. Delete hard link
rm hard_link.txt
# 9. Delete original file
rm experiment_file.txt
# 10. Try to access soft link (this will fail)
cat soft_link.txt # This will report an error because the target file has been deleted
# 11. Delete soft link
rm soft_link.txt
