<div dir="ltr">Hi,<br><div><div class="gmail_extra"><br><br><div class="gmail_quote">On Thu, Feb 13, 2014 at 12:14 PM, Abhijit Chandrakant Pawar <span dir="ltr"><<a href="mailto:abhi.c.pawar@gmail.com" target="_blank">abhi.c.pawar@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Hi,<br>
<div class=""><br>
On Thursday 13 February 2014 07:40 AM, freeman wrote:<br>
> Hi list,<br>
><br>
> I am a newbie in linux kernel programming. Recently I got stuck in a<br>
> problem when doing<br>
> practice in file system programming. I hope this list is the right place<br>
> I can turn to.<br>
><br>
> I want to replace some file operations of files in a certain<br>
> directory,so that data can be<br>
> decrypted/encrypted through read/write system call. So I:<br>
><br>
> #1: Find the directory inode, save its original inode operation<br>
> table,then replace<br>
> the table:<br>
><br>
> kern_path(pathname, LOOKUP_FOLLOW, &target_dir_path);<br>
> lower_iops = target_dir_path.dentry->d_inode->i_op;<br>
> target_dir_path.dentry->d_inode->i_op = &my_iops;<br>
><br>
</div>I assume that you are writing your own stackable filesystem.<br>
Take a look at WRAPFS[1] and ecryptfs[2]. As a matter of fact, ecryptfs<br>
does what you are expecting.<br>
<br>
To do this, you need to set your superblock operations for the lower<br>
directory inode so the VFS use your filesystem instead of the original<br>
filesystem.<br>
Important steps to look are:<br>
1. get lower superblock from the lower directory inode<br>
2. assign this superblock as an overlay for your own superblock.<br>
3. Set your own superblock operations for the new superblock<br>
4. get a root inode for your superblock using the lower directory inode<br>
5. While you are getting the inode, you can set the file operations on<br>
this inode which will help you achieve your case.<br>
<br>
The point to note that you need to interpose the inodes with VFS so that<br>
everything would be routed to your filesystem.<br>
<br>
--<br>
Abhijit.<br>
[1]<a href="http://wrapfs.filesystems.org/" target="_blank">http://wrapfs.filesystems.org/</a><br>
[2]<a href="http://ecryptfs.org/" target="_blank">http://ecryptfs.org/</a><br>
<div class="HOEnZb"><div class="h5"><br>
<br>
> #2: In my_iops, I mainly changed ".lookup" function like this to achive<br>
> my goal --<br>
> replace the file operation table of all files in the directory.<br>
><br>
> static struct dentry *my_inode_lookup(struct inode *dir, struct dentry<br>
> *dentry,<br>
> struct nameidata *nd)<br>
> {<br>
> struct dentry *ret_dentry;<br>
><br>
> ret_dentry = lower_iops->lookup(dir,dentry,nd);<br>
> if (!ret_dentry)<br>
> goto out;<br>
> ret_dentry->d_inode->i_fop = &my_fops;<br>
> out:<br>
> return ret_dentry;<br>
> }<br>
><br>
> Things turns out that replacement of inode operation table of directory<br>
> is successful<br>
> but the changes in file operations are not functional: system works as<br>
> it used to,<br>
> totally ignore my_fops!<br>
><br>
> I have no idea how to fix it. Can anybody help?<br>
> Thanks for your attention!<br>
> Regards<br>
><br>
> Freeman Zhang<br>
><br>
><br>
> _______________________________________________<br>
> Kernelnewbies mailing list<br>
> <a href="mailto:Kernelnewbies@kernelnewbies.org">Kernelnewbies@kernelnewbies.org</a><br>
> <a href="http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies" target="_blank">http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies</a><br>
><br>
<br>
_______________________________________________<br>
Kernelnewbies mailing list<br>
<a href="mailto:Kernelnewbies@kernelnewbies.org">Kernelnewbies@kernelnewbies.org</a><br>
<a href="http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies" target="_blank">http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies</a><br>
</div></div></blockquote></div><br><br></div><div class="gmail_extra">Freeman - What you are doing seems to be correct - you may have missed some pointer some where.<br><br></div><div class="gmail_extra">Which file system are you using, are you writing a layer on an existing file system or changing the code of a file system<br>
</div><div class="gmail_extra"><br></div><div class="gmail_extra">Try adding some debug messages whenever you change the operations which prints the dentry->name of the file, you will get an idea that the correct file's operations are getting modified.<br>
</div><div class="gmail_extra"><br clear="all"><br>-- <br>Regards,<br>Rishi Agrawal<br>
</div></div></div>