5

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.

sarnold
  • 102,305
  • 22
  • 181
  • 238
Leonardo
  • 61
  • 1
  • 3

2 Answers2

4

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.

MacNewbie
  • 39
  • 2
1

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.

sarnold
  • 102,305
  • 22
  • 181
  • 238
  • My job is to make a system call that blocks for reading and writing a given file I am implementing the following: Step on the Path parameter for the system call that caught the inode created by filp_open function (path, O_CREAT, 00777) (this work) and add in a linked list. Then I created a function that takes as parameter an inode taken the path of this file and checks it in the list, if returns 1 otherwise returns 0. I place this function within the systemcalls open () but using filp_open (path, FLAG, x) followed by filp_close () occurs the following errors: – Leonardo Dec 19 '11 at 05:20
  • O_CREAT FLAG = x = 00777 -> kernel memory soon fills the kernel boot FLAG = 0 and x = O_RDONLY -> Kernel Panic on kernel boot – Leonardo Dec 19 '11 at 05:20
  • It'd probably be better to edit your original question with this description -- it's immensely difficult to write content that fits into comment boxes and is still legible. :) – sarnold Dec 19 '11 at 21:46