<p dir="ltr">Thank for replies. About copy_to_user and copy_from_user, whats the better way? <br>
I dont have much experience in kernel development but I&#39;m trying to learn. Can you recommend me some books, documents, etc so I can learn more about filesystems in kernel. I am also interested to learn how mmap works because I have problems with execution of binary files in my encrypted filesystem. </p>
<div class="gmail_quote">On Jul 12, 2015 8:30 PM,  &lt;<a href="mailto:kernelnewbies-request@kernelnewbies.org">kernelnewbies-request@kernelnewbies.org</a>&gt; wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><br><div class="gmail_extra"><br><div class="gmail_quote">On Sun, Jul 12, 2015 at 8:08 PM, Freeman Zhang <span dir="ltr">&lt;<a href="mailto:freeman.zhang1992@gmail.com" target="_blank">freeman.zhang1992@gmail.com</a>&gt;</span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div>-------- Original Message --------<br>
&gt; hello<br>
&gt; I am working on adding a simple encryption to file contents in ext4 driver<br>
&gt; (for learning purposes) I added simple XOR encryption to aio_read and<br>
&gt; aio_write functions and it worked until I faced this problem:<br>
&gt;<br>
&gt; when I open a file in encrypted filesystem using VIM text editor and when I<br>
&gt; try to save it it gives me this error:<br>
&gt;<br>
&gt;&gt;&gt; pointer block id wrong<br>
&gt;&gt;&gt; can not find line 1<br>
&gt;<br>
&gt; and it just corrupts the entire file!<br>
&gt;<br>
&gt; this is my aio_write function:<br>
&gt;<br>
&gt; aio_write_enc(struct kiocb *iocb, const struct iovec *iov,<br>
&gt;         unsigned long nr_segs, loff_t pos)<br>
&gt; {<br>
&gt;     size_t i;<br>
&gt;     ssize_t ret;<br>
&gt;     char *data=vmalloc(sizeof(char)*iov-&gt;iov_len);<br>
&gt;     copy_from_user(data,iov-&gt;iov_base,iov-&gt;iov_len);<br>
&gt;<br>
&gt;     for(i=0;i&lt;iov-&gt;iov_len;i++)<br>
&gt;     {<br>
&gt;         data[i]^=5;<br>
&gt;     }<br>
&gt;     struct iovec iov_enc= { .iov_base = iov-&gt;iov_base, .iov_len =<br>
&gt; iov-&gt;iov_len };<br>
&gt;<br>
&gt;     copy_to_user(iov_enc.iov_base,data,iov-&gt;iov_len);<br>
&gt;     ret=ext4_file_write(iocb,&amp;iov_enc,nr_segs,pos);<br>
&gt;     vfree(data);<br>
&gt;     return ret;<br>
&gt; }<br>
&gt;<br>
&gt; this just changes the data and then calls original function.<br>
&gt;<br>
&gt; is there anything wrong with this function? can anyone help me?<br>
&gt;<br>
&gt;<br>
&gt;<br>
</div></div>Hi Amir,<br>
<br>
I&#39;m not quite sure about what&#39;s wrong with your function, but here are<br>
two suggestions I got from the list when I did similar things:<br>
<br>
1. wrapfs<br>
2. ecryptfs<br>
<br>
I think you should check these two stackable filesystems if you haven&#39;t.<br>
<br>
Hope this can help a little bit!<br>
<span><font color="#888888"><br>
Freeman<br>
<br>
</font></span><br>_______________________________________________<br>
Kernelnewbies mailing list<br>
<a href="mailto:Kernelnewbies@kernelnewbies.org" target="_blank">Kernelnewbies@kernelnewbies.org</a><br>
<a href="http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies" rel="noreferrer" target="_blank">http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies</a><br>
<br></blockquote></div><br></div><div class="gmail_extra">Hi Amir,</div><div class="gmail_extra"><br></div><div class="gmail_extra">I agree with Freeman Zhang over here. The way you are doing it is not right. There is a mechanism to create stacks of file system and you should go down that path.</div><div class="gmail_extra"><br></div><div class="gmail_extra">Having said this, you should definitely debug the issue that you are facing. Some pointers : -</div><div class="gmail_extra">1. As you have already mentioned that this is happening only for vim and not while regular read(using cat, etc), you need to check what vim does special to read a file. I would suggest make use of strace and do reading with and without vim, maybe you will get something of interest.</div><div class="gmail_extra">2. re-read code to check, you might be messing up while write or read.</div><div class="gmail_extra"><br></div><div class="gmail_extra">Apart from these some basic practices you need to follow is : -</div><div class="gmail_extra"><br></div><div class="gmail_extra">1. check for error conditions, like you missed checking error from vmalloc() and the below code will execute even if it failed, this should be avoided.</div><div class="gmail_extra">2. copy_from_user &amp; again copying back to user is in-efficient.</div><div class="gmail_extra"><br></div><div class="gmail_extra"><br></div><div class="gmail_extra">Enjoy life,</div><div class="gmail_extra">Rohan</div></div>
</blockquote></div>