Exercise: Symbolic Links
Moderate/Hard
In this exercise you will add symbolic links to xv6. Symbolic links (or symlinks) point to a file or directory (called target) by pathname. When a symbolic link is opened, the kernel looks up the linked-to name. Symbolic links resemble hard links, but hard links are restricted to pointing to files on the same disk, cannot refer to directories, and are tied to a specific target i-node rather than (as with symbolic links) referring to whatever happens at the moment to be at the target name, if anything. Implementing this system call is a good exercise to understand how pathname lookup works.
You will implement the int symlink(char *target, char *path) system call, which creates a new symbolic link at path that refers to file named by target. For further information, see the man page symlink.
At the end of the exercise, you can test your implementation with the following test file.
Part 1: add the system call
- First, create a new system call number for symlink, add an entry to
user/usys.pl,user/user.h, and implement an emptysys_symlinkinkernel/sysfile.c. - Add a new file type (
T_SYMLINK) tokernel/stat.hto represent a symbolic link. - Add a new flag to
kernel/fcntl.h, (O_NOFOLLOW), that can be used with the open system call. Note that flags passed to open are combined using a bitwise OR operator, so your new flag should not overlap with any existing flags. This will let you compileuser/symlinktest.conce you add it to the Makefile.
Part 2: implement symlink
Implement system call symlink(target, path). Use the following code skeleton that makes sure the locks are correctly acquired/released, including in error handling code.
// int sys_symlink(const char *target, const char *path)
// create a new symlink at path that refers to target
uint64
sys_symlink(void)
{
// Declare variables
struct inode *ip;
[TODO]
// Get arguments
[TODO]
begin_op(); // We open a file system transaction
// Create the new inode `ip` with type symlink
// Hint: Read Section 10.14 of the xv6 book again and look at the `create ` syscall and how it is used from other system calls.
[TODO]
if([TODO: error condition]){
end_op(); // We close the transation on failure
return -1;
}
// Store the target path in the data block of the inode.
// Note that target does not need to exist for the system call to succeed.
// Hint: You can use the function `writei`.
[TODO]
if([TODO: error condition]){
iunlockput(ip); // We unlock inode on failure
end_op(); // We close the transation on failure
return -1;
}
iunlockput(ip); // Release inode
end_op(); // Close transaction
return 0;
}
Part 3: open symbolic link
Modify sys_open to handle the case where the path refers to a symbolic link.
-
Note that when a process specifies
O_NOFOLLOWin the flags to open, open should open the symlink (and not follow the symbolic link). Otherwise, open must read the target path and follow it. -
If the linked file is also a symbolic link, we recursively follow it until a non-link file is reached. If the links form a cycle, we return an error code. We approximate this by returning an error code if the depth of links reaches some threshold (e.g., 10).
-
Other system calls (e.g., link and unlink) must not follow symbolic links; these system calls operate on the symbolic link itself, so they don’t need to be modified.
-
You can use the following code skeleton for
open(just replaceopenwith this code and fill the TODOs). Keep in mind that this will overwrite your changes to the previous exercise, make sure to preserve those changes if you’d like!
uint64
sys_open(void)
{
char path[MAXPATH];
int fd, omode;
struct file *f;
struct inode *ip;
int n;
argint(1, &omode);
if((n = argstr(0, path, MAXPATH)) < 0)
return -1;
begin_op();
if(omode & O_CREATE){
ip = create(path, T_FILE, 0, 0);
if(ip == 0){
end_op();
return -1;
}
} else {
if((ip = namei(path)) == 0){
end_op();
return -1;
}
/*=========START OF SYMLINK CHANGES=========*/
// Follow symbolic links for up to 10 links
for (int i = 0; i < 10; ++i) {
ilock(ip);
// If inode is not symbolic link or O_NOFOLLOW is set,
// we just continue on the normal open path with the current inode
if([TODO: CONDITION]) { goto found; }
// Otherwise, we need to follow the link to target
// First, we need to get the target path from the inode's data block
// Hint: you can use `readi`
[TODO]
if ([TODO: error condition]) {
iunlockput(ip);
end_op();
return -1;
}
iunlockput(ip);
// Now, we need to set `ip` to the inode corresponding to the target path
// Hint: you can use namei
[TODO]
if([TODO: error condition]){
end_op();
return -1;
}
}
// We have reached the threashold of following symbolic links
// This is probably a cycle so we exit with an error
end_op();
return -1;
found:
// We have successfully found our target inode `ip`
// We can continue the normal flow of open
/*==========END OF SYMLINK CHANGES==========*/
if(ip->type == T_DIR && omode != O_RDONLY){
iunlockput(ip);
end_op();
return -1;
}
}
if(ip->type == T_DEVICE && (ip->major < 0 || ip->major >= NDEV)){
iunlockput(ip);
end_op();
return -1;
}
if((f = filealloc()) == 0 || (fd = fdalloc(f)) < 0){
if(f)
fileclose(f);
iunlockput(ip);
end_op();
return -1;
}
if(ip->type == T_DEVICE){
f->type = FD_DEVICE;
f->major = ip->major;
} else {
f->type = FD_INODE;
f->off = 0;
}
f->ip = ip;
f->readable = !(omode & O_WRONLY);
f->writable = (omode & O_WRONLY) || (omode & O_RDWR);
if((omode & O_TRUNC) && ip->type == T_FILE){
itrunc(ip);
}
iunlock(ip);
end_op();
return fd;
}
To test, add symlinktest to the Makefile and run it. Your solution is complete when the tests produce the following output (including usertests succeeding).
$ symlinktest
Start: test symlinks
test symlinks: ok
Start: test concurrent symlinks
test concurrent symlinks: ok
$ usertests -q
...
ALL TESTS PASSED