Ben Atkinson
Ben Atkinson

Reputation: 53

How to find a dentry from an inode/pathname?

I am working on a module to read a files xattributes on open. I have hooked the sys_open and due to this I need to get the dentry of the file without opening the file. In brief I have the inode and the absolute path but having trouble to figure out; how to get a dentry from these. All comments are very much appreciated.

Upvotes: 1

Views: 6494

Answers (3)

Vladimir Vaschenko
Vladimir Vaschenko

Reputation: 576

If you have access to struct file poiner, then in kernel header include file "include/linux/fs.h" there is inline function:

static inline struct dentry *file_dentry(const struct file *file)
{
    return d_real(file->f_path.dentry, file_inode(file));
}

Upvotes: 0

firo
firo

Reputation: 1062

OK. Other answers didn't cover how to obtain a dentry from pathname/absolute path. The following code snippet could it.

    int ret = 0;
    struct path path;

    ret = kern_path("/proc/", LOOKUP_DIRECTORY, &path);
    if (ret)
            pr_err("Failed to lookup /proc/ err %d\n", ret);
    else {
            if (PROC_SUPER_MAGIC != path.mnt->mnt_sb->s_magic)
                    printk("BUG /proc is not mounted as Proc FS, magic %lx\n", path.mnt->mnt_sb->s_magic);
            path_put(&path);
    }

Upvotes: 3

vinod maverick
vinod maverick

Reputation: 678

As per my understating you are trying to get the dentry path from your driver module during the open callback function . If so; then before putting down the way I am adding the structure list which are required to access the the dentry information.

include/linux/fs.h

Struct file{
struct path             f_path;
};

include/linux/path.h

struct path {
           struct vfsmount *mnt;
           struct dentry *dentry;
  };

include/linux/dcache.h

struct dentry {
};

So you can do like this.

 static int sample_open(struct inode *inode, struct file *file)
    { 
    char *path, *dentry,*par_dentry;
    char buff[256];
    dentry = file->f_path.dentry->d_iname;
    pr_info("dentry :%s\n",dentry);
    par_dentry = file->f_path.dentry->d_parent->d_iname;
    pr_info("parent dentry :%s\n",par_dentry);
    path=dentry_path_raw(file->f_path.dentry,buff,256);
    pr_info("Dentry path %s\n",path);
    }

Upvotes: 2

Related Questions