Getting file descriptors and parts in kernel space without open ()

Can someone provide some code to solve this problem?

Effectively, how do we get struct inode* from the kernel level given the file /dev/driver1 ?

The user field indicates that:

 int fd; fd = open("/dev/driver1", O_RDWR | O_SYNC); 

In the sound space:

 static long dev_ioctl(struct file *file, unsigned cmd, unsigned long arg) struct dev_handle *handle; handle = file->private_data; 

Assuming we are not following this path,

How do we get in the kernel itself, for example. hard coding file->private_data to handle?

+6
source share
2 answers

You are looking for the filp_open function. From include/linux/fs.h :

 struct file *filp_open(const char *filename, int flags, umode_t mode); 

Here is a link to the source and function documentation: http://lxr.free-electrons.com/source/fs/open.c#L937

If you really need FD, you can use sys_open (not exported to newer kernels):

 long sys_open(const char __user *filename, int flags, int mode); 

You can find a very good answer on a similar question: How to read / write files in the Linux kernel module?

Edit (how to get inode ):

You can get the cached inode from the struct file :

 struct file *file = ...; struct inode *inode = file->inode; 

If you want it with a lock: here is the background: Documentation/filesystems/path-lookup.txt

The starting point for the move is current->fs->root . There are a number of functions in the kernel that already do the work, you can find them in the source file fs/namei.c .

There is a function: kern_path :

 int error; struct inode *inode; struct path path; error = kern_path(pathname, LOOKUP_FOLLOW, &path); if (error) ...; inode = path.dentry->d_inode; 
+2
source

Is your code in dev_ioctl function? If so, then

 static long dev_ioctl(struct file *file, unsigned cmd, unsigned long arg) struct dev_handle *handle; struct inode *inode; handle = file->private_data; inode = file->f_inode; 

There seems to be no reasonable documentation for locking requirements, so you should probably try to dig up similar code and see how it works with the f_inode element.

+1
source

All Articles