By modifying the VFS file system function jump table to intercept the NFS system

Everybody is good, the recent work requires me to rewrite the read system call. I am the method successfully replaced the call to the read system ext3.
The following code:
#include <linux/sched.h>
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <linux/file.h>

char *aim = "/root/tmp";

MODULE_AUTHOR("petsatan@sohu");
MODULE_DESCRIPTION("Get the kernel function address ");
MODULE_LICENSE("GPL");

typedef ssize_t (*read_t)(struct file *, char *, size_t, loff_t *);
read_t orig_aim_read = NULL;

ssize_t encry_read(struct file *file , char *buf , size_t count , loff_t *ppos)
{
printk("Congratulation! Now read file\n");

ssize_t encryfile;
encryfile = orig_aim_read(file ,buf ,count ,ppos);
return encryfile;
}

int switch_read(const char *p , read_t *orig_read , read_t new_read)
{
struct file *filep;
filep = filp_open(p , O_RDONLY , 0);
if(IS_ERR(filep))
return -1;
if(orig_read)
*orig_read = filep->f_dentry->d_inode->i_fop->read;

struct file_operations *fp =filep->f_dentry->d_inode->i_fop;
fp->read=new_read;
filp_close(filep,0);
return 0;
}

int unswitch_read(const char *p, read_t orig_read)
{
struct file *filep;
filep = filp_open(p , O_RDONLY , 0);
if(IS_ERR(filep))
return -1;
struct file_operations *fp = filep->f_dentry->d_inode->i_fop;
fp->read=orig_read;
filp_close(filep,0);
return 0;
}

static int encryfile_init(void)
{
switch_read(aim , &orig_aim_read , encry_read);
printk(" <1>read() has switch!\n");
return 0;
}

static void encryfile_cleanup(void)
{
unswitch_read(aim, orig_aim_read);
printk("<2>read() has unswitch! \n");
}

module_init(encryfile_init);
module_exit(encryfile_cleanup);

/root/TMP is a local disk file.

But when I change the /root/tmp to NFS files on the file system (such as /mnt/nfs/tmp) when, only the operation of file /mnt/nfs/tmp, just call me to write read, other file operations on the NFS, does not call the newly written read. Please help me analyze the reasons for you?

Started by Demi at November 30, 2016 - 7:05 PM

My solution is the aim file to the root directory of the NFS filesystem files, then call the NFS file system will be replaced. Hope that we can help people later.

Posted by Demi at December 14, 2016 - 7:44 PM

Hello, I am now working like you, also used this method to intercept file operations, but how do you put the intercepted IO information recorded.?

Posted by Omar at December 22, 2016 - 8:06 PM

hello,I use netlink, I hope you can help to.

Posted by Demi at January 04, 2017 - 8:37 PM