2

I want to know on what file in /sys/* the functionstatic ssize_t sysfs_write_file(struct file *file, const char __user *buf, size_t count, loff_t *ppos) were called. the question is how to get the file name/path by having struct file *file ?

struct file {
  mode_t f_mode;
  loff_t f_pos;
  unsigned short f_flags;
  unsigned short f_count;
  unsigned long f_reada, f_ramax, f_raend, f_ralen, f_rawin;
  struct file *f_next, *f_prev;
  int f_owner;         /* pid or -pgrp where SIGIO should be sent */
  struct inode * f_inode;
  struct file_operations * f_op;
  unsigned long f_version;
  void *private_data;  /* needed for tty driver, and maybe others */
};

EDIT: I understand there is no one-to-one mapping between inode to a name, but I must know the major/minor/file descriptor and can search for some of the inode's name in the file system.

0x90
  • 39,472
  • 36
  • 165
  • 245

2 Answers2

4

Actuall you can retrieve the file path used to open the file. In linux struct file represents an opened file and has the path used to open that file associate with the structure. Before 2.6.20 it was a member called f_dentry and from 2.6.20 it is a new member called f_path. You can use function d_path() to retrieve the full path of your file directly.

Refer to the following Linux source code:

http://lxr.free-electrons.com/source/include/linux/fs.h?v=3.10#L763

http://lxr.free-electrons.com/source/fs/dcache.c?v=3.0#L2626

And also answer: How can I get a filename from a file descriptor inside a kernel module?

Community
  • 1
  • 1
xiay
  • 855
  • 8
  • 19
  • 1
    The actual code would depend on the kernel version you are using, as both struct file and d_path() has changed over time. You can refer to the code in the other question as reference - just ignore the part for getting a pointer to struct file as you already have it. – xiay Nov 19 '14 at 21:16
3

In Linux and other Unix-like OSs, a file object can be pointed to by any number of names, and the file object itself has no pointers back to any of them. Names are not a feature of the file, they are just an external index.

Lee Daniel Crocker
  • 12,927
  • 3
  • 29
  • 55
  • Can I get the major and minor of the file if it is a device? How can I map back from file pointer to the path in the files system? – 0x90 Jun 18 '13 at 19:42
  • The file pointer has a pointer to the inode, which will contain data like device numbers and such. But you miss the point: an open file doesn't have a name. You may have used one of its names to get to it, but that's a fundamentally one-way operation. – Lee Daniel Crocker Jun 18 '13 at 20:42
  • I didn't miss the point, but my question is how to map from a pointer back to a file on file system, and it seems to be possible. – 0x90 Jun 18 '13 at 20:44
  • Why do you think it's possible? There might be 20 or more names mapping to the same file, or none. – Lee Daniel Crocker Jun 18 '13 at 20:46
  • because if I know the minor and major and I know i look for the one name under /sys/bla/specific-bla/very-specific/ that must to be unique if it is not have symbolic. – 0x90 Jun 18 '13 at 20:48
  • There are hard links as well as symbolic links. And sure, you might be able to search the filename space for one of the names that maps to the same inode as open file, but the open file structure itself will have no info on how to get there. – Lee Daniel Crocker Jun 18 '13 at 20:51
  • This is not true for Linux. – xiay Nov 19 '14 at 00:23