Lab 8 File System: Symbolic links
Add symbolic links to xv6
Introduction
In this exercise you will add symbolic links to xv6. Symbolic links (or soft links) refer to a linked file by pathname; when a symbolic link is opened, the kernel follows the link to the referred file. Symbolic links resembles hard links, but hard links are restricted to pointing to file on the same disk, while symbolic links can cross disk devices. Although xv6 doesn’t support multiple devices, implementing this system call is a good exercise to understand how pathname lookup works.
Task
Implement the symlink(char target, char path) system call, which creates a new symbolic link at linkpath that refers to file named by target.
Solution
Create system call, add new file type and new flag
Create symlink
system call. Add a new file type (T_SYMLINK) to kernel/stat.h to represent a symbolic link. Add a new flag to kernel/fcntl.h, (O_NOFOLLOW), that can be used with the open system call.
Implement the symlink(target, path)
system call
symlink(target, path)
system callI decided to store the target path of a symbolic link in the inode
’s data blocks. Create a inode of T_SYMLINK type.
Modify open
system call to work with symbolic link
open
system call to work with symbolic linkIf the file does not exist, open must fail.
When a process specifies O_NOFOLLOW in the flags to open, open should open the symlink (and not follow the symbolic link).
Otherwise, open follows the symlink.
If the linked file is also a symbolic link, you must recursively follow it until a non-link file is reached. If the links form a cycle, you must return an error code.
sys_open
namei
: Look up and return the inode for a path name.
Thoughts
The key is where we store the target path. We should store it in inode
’s data blocks. By using readi
and writei
, we could read and write into/from the inode->data[0]’s block
Bad decision I made: I originally tried to add a new char array in inode
in memory file struct, but it does not use buffer cache, not sync to dinode
, not logged properly, and not store back to disk properly.
Reference: How readi
works
readi
worksA bit more on readi
and writei
bmap(ip, off/BSIZE)
returns the disk block address of the nth block in inode ip. readi
starts from offset, get the buffer containing the data, copy data from buffer’s data to the destination address. writei
works similar. Write data from src to inode
How bmap
works
bmap
worksHow bread works
心得
软连接和硬链接的不同是什么呢?
Term dinode
in OS represents an actual file in disk with metadata. A directory contains multiple dirent
, each has an inode
number, and a file name. Each dirent
points to an actual file, its number is set to the file's dinode
number.
A hard link is an additional name for an existing file. It is anotherdirent
which points to the actual file. The metadata in dinode
tracks the number of links to this file.
File(dinode
) can be freed if both number of hard links and number of references are 0.
软连接就是一个可有可无的索引,虽然也是一个存在directory下面的 dirent
,但是所指的目标可以不存在,或者在另一个device.
硬链接就是真正的恋人关系,是真的存在,生活中都有彼此的印记。
软连接是暗恋,花开无泪,暗恋无声。说的就是14岁的你,是吗?
Last updated