Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implementing symlinks in a virtual file system

Tags:

linux

symlink

vfs

I'm working on a virtual file system which isn't disk based, kind of like /proc. Now I want to create a symlink within it to a target on a ext3 file system. I haven't found any standard documentation on ways to achieve this. What I've guessed so far is that I have to write a function to put in for symlink in struct inode_operations. But frankly I'm at a loss even with the function parameters.

If it matters, I started off with this tutorial on LWN: http://lwn.net/Articles/13325/

EDIT: I'm working with libfs, not FUSE at the moment

like image 339
user108127 Avatar asked Dec 27 '09 19:12

user108127


People also ask

Can you symlink files?

Symlinks can take two forms: Soft links are similar to shortcuts, and can point to another file or directory in any file system. Hard links are also shortcuts for files and folders, but a hard link cannot be created for a folder or file in a different file system.

What is VFS and its implementation?

A virtual file system (VFS) or virtual filesystem switch is an abstract layer on top of a more concrete file system. The purpose of a VFS is to allow client applications to access different types of concrete file systems in a uniform way.

What file systems support symbolic links?

Symbolic links are supported by POSIX and by most Unix-like operating systems, such as FreeBSD, Linux, and macOS. Limited support also exists in Windows 7 and Windows Vista, and to some degree in Windows 2000 and Windows XP in the form of shortcut files.

Does FAT32 support symlinks?

I know FAT32, as well as FAT16/12 neither support symbolic links nor hard-links. However I came up with this idea: The FAT specification describes that every file is associated with a directory-entry. In my understanding, one could say that a file-entry in a directory somehow or other points to the file's content.


2 Answers

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.

like image 132
MarkR Avatar answered Oct 01 '22 08:10

MarkR


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

like image 31
user108127 Avatar answered Oct 01 '22 08:10

user108127