How to obtain a pathname or dentry or struct file

2020-04-12 01:55发布

I need to know how to obtain a pathname or dentry or struct file from a given inode.

I was using file_open to obtain struct file from a pathname but but always gave kernel panic. I need a way to compare an inode from my list of inodes with a inode from a pathname or compare all inodes in the disk to find corresponding pathnames, and then compare with my list of inodes.

2条回答
乱世女痞
2楼-- · 2020-04-12 02:15

This is, in general, extremely difficult to do.

An inode may have thousands of pathnames. All names are equally "valid". Even on filesystems that do not support multiple links, the file could be bind-mounted thousands of times to anywhere else in the system.

Both the AppArmor and TOMOYO mandatory access control systems rely upon pathnames -- but with a gigantic difference: access controls are performed on a specific file descriptor, which was opened with a specific name, and both tools use that specific name.

Look into the security/apparmor/path.c function aa_get_name() or security/tomoyo/file.c function tomoyo_get_realpath() for details on finding pathnames from an inode -- given additional supporting information. From just the plain inode object, I think you're probably out of luck.

查看更多
唯我独甜
3楼-- · 2020-04-12 02:19

This sample code will work well in Linux kernel version 2.6.xx

struct dentry *sample_dentry = NULL;
struct inode *tmp_inode = &inode_need_to_get;
struct list_head *tmp_list = NULL;
list_for_each(tmp_list, &(tmp_inode->i_dentry))
{
    sample_dentry = list_entry(tmp_list, struct dentry, d_alias);
    printk(KERN_EMERG, "name of file is %s\n", sample_dentry->d_iname);
}

Each inode object will have one or more dentries object in case this file have a hard link.

查看更多
登录 后发表回答