问题
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.
回答1:
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);
}
回答2:
The question is wrong. Extended attributes are stored in the inode, thus as long as you have the inode you can just access them.
I have hooked the sys_open and due to this I need to get the dentry of the file without opening the file.
This is wrong on 2 accounts.
- sys_open is not the only place which can open a file.
- even if you hooked into all places, the additional lookup you perform can find a different file than the one which got opened (consider a joker doing cp file1 toopen; cp fil2 toopen in a loop)
What you want to is use the LSM framework (Linux Security Modules). In particular see security_file_open.
However, given the quality of the question I have to ask what is this about. Is this a college project? I would argue you are not prepared to work on it and would suggest changing to a non-kernel one if possible.
来源:https://stackoverflow.com/questions/43235313/how-to-find-a-dentry-from-an-inode-pathname