Implementing symlinks in a virtual file system Implementing symlinks in a virtual file system linux linux

Implementing symlinks in a virtual file system


Presumably you're using fuse, if you're not, do :)

All you have to do is implement the getattr function to tell the kernel that the object is a symlink, then implement the readlink function and return the path that the link should link to; the kernel will do the rest.


I was able to accomplish it finally. Here is what I did (some details may differ depending on what the filesystem wants to achieve):

  1. Create inode of the symlink with the S_IFLNK mode and add the target to the i_private field.

  2. Implement follow_link because generic_readlink requires it to be present

static void *sample_follow_link (struct dentry *dentry, struct nameidata *nd){    nd->depth = 0;    nd_set_link(nd, (char *)dentry->d_inode->i_private);    return NULL;}static struct inode_operations sample_inode_ops = {    .readlink = generic_readlink,    .follow_link = sample_follow_link,};.....//in the function for the dentry and inode creation inode->i_op = sample_inode_ops


I would suggest to take a look at linux/fs/ext2/ source code. Files symlink.c, inode.c, namei.c and probably few others. You will get some idea as of what needs to be done. Contrary to expectation, filesystem code of the individual filesystems is actually very short and easy to read.

But maybe instead of creating new virtual filesystem, you might ask yourself another question, wouldn't fuse user level filesystem be enough in my case? They have slightly better documentation to creating virtual filesystems and a few more examples.