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.
Asked
Active
Viewed 5,786 times
1

vinod maverick
- 670
- 4
- 14

Ben Atkinson
- 53
- 1
- 8
-
http://stackoverflow.com/questions/8556461/how-to-obtain-a-pathname-or-dentry-or-struct-file-from-a-given-inode – ilansch Apr 06 '17 at 04:57
-
Possible duplicate of [How to obtain a pathname or dentry or struct file from a given inode?](https://stackoverflow.com/q/8556461/608639) – jww May 07 '18 at 03:54
2 Answers
3
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);
}

firo
- 1,002
- 12
- 20
2
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);
}

vinod maverick
- 670
- 4
- 14
-
We are actually trying to get the dentry struct from the file path. We need that to access the xattributes in the dentry that belongs to the file. We do not need the dentry path itself – Ben Atkinson Apr 06 '17 at 03:46
-
@BenAtkinson I did not understand. ***What you mean by get the dentry structure***. Are you looking to get the the dentry structure form your custom function like `custom_structure->mydent = file->f_path.dentry;` or something more? – vinod maverick Apr 06 '17 at 05:07