<p dir="ltr"><br>
On 12 Jul 2015 22:20, &quot;Amir Hezarkhani&quot; &lt;<a href="mailto:amir6723@gmail.com">amir6723@gmail.com</a>&gt; wrote:<br>
&gt;<br>
&gt; Thank for replies. About copy_to_user and copy_from_user, whats the better way? <br>
&gt; 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.<br>
&gt;<br>
&gt; On Jul 12, 2015 8:30 PM, &lt;<a href="mailto:kernelnewbies-request@kernelnewbies.org">kernelnewbies-request@kernelnewbies.org</a>&gt; wrote:<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt; On Sun, Jul 12, 2015 at 8:08 PM, Freeman Zhang &lt;<a href="mailto:freeman.zhang1992@gmail.com">freeman.zhang1992@gmail.com</a>&gt; wrote:<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; -------- Original Message --------<br>
&gt;&gt;&gt; &gt; hello<br>
&gt;&gt;&gt; &gt; I am working on adding a simple encryption to file contents in ext4 driver<br>
&gt;&gt;&gt; &gt; (for learning purposes) I added simple XOR encryption to aio_read and<br>
&gt;&gt;&gt; &gt; aio_write functions and it worked until I faced this problem:<br>
&gt;&gt;&gt; &gt;<br>
&gt;&gt;&gt; &gt; when I open a file in encrypted filesystem using VIM text editor and when I<br>
&gt;&gt;&gt; &gt; try to save it it gives me this error:<br>
&gt;&gt;&gt; &gt;<br>
&gt;&gt;&gt; &gt;&gt;&gt; pointer block id wrong<br>
&gt;&gt;&gt; &gt;&gt;&gt; can not find line 1<br>
&gt;&gt;&gt; &gt;<br>
&gt;&gt;&gt; &gt; and it just corrupts the entire file!<br>
&gt;&gt;&gt; &gt;<br>
&gt;&gt;&gt; &gt; this is my aio_write function:<br>
&gt;&gt;&gt; &gt;<br>
&gt;&gt;&gt; &gt; aio_write_enc(struct kiocb *iocb, const struct iovec *iov,<br>
&gt;&gt;&gt; &gt;         unsigned long nr_segs, loff_t pos)<br>
&gt;&gt;&gt; &gt; {<br>
&gt;&gt;&gt; &gt;     size_t i;<br>
&gt;&gt;&gt; &gt;     ssize_t ret;<br>
&gt;&gt;&gt; &gt;     char *data=vmalloc(sizeof(char)*iov-&gt;iov_len);<br>
&gt;&gt;&gt; &gt;     copy_from_user(data,iov-&gt;iov_base,iov-&gt;iov_len);<br>
&gt;&gt;&gt; &gt;<br>
&gt;&gt;&gt; &gt;     for(i=0;i&lt;iov-&gt;iov_len;i++)<br>
&gt;&gt;&gt; &gt;     {<br>
&gt;&gt;&gt; &gt;         data[i]^=5;<br>
&gt;&gt;&gt; &gt;     }<br>
&gt;&gt;&gt; &gt;     struct iovec iov_enc= { .iov_base = iov-&gt;iov_base, .iov_len =<br>
&gt;&gt;&gt; &gt; iov-&gt;iov_len };<br>
&gt;&gt;&gt; &gt;<br>
&gt;&gt;&gt; &gt;     copy_to_user(iov_enc.iov_base,data,iov-&gt;iov_len);<br>
&gt;&gt;&gt; &gt;     ret=ext4_file_write(iocb,&amp;iov_enc,nr_segs,pos);<br>
&gt;&gt;&gt; &gt;     vfree(data);<br>
&gt;&gt;&gt; &gt;     return ret;<br>
&gt;&gt;&gt; &gt; }<br>
&gt;&gt;&gt; &gt;<br>
&gt;&gt;&gt; &gt; this just changes the data and then calls original function.<br>
&gt;&gt;&gt; &gt;<br>
&gt;&gt;&gt; &gt; is there anything wrong with this function? can anyone help me?<br>
&gt;&gt;&gt; &gt;<br>
&gt;&gt;&gt; &gt;<br>
&gt;&gt;&gt; &gt;<br>
&gt;&gt;&gt; Hi Amir,<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; I&#39;m not quite sure about what&#39;s wrong with your function, but here are<br>
&gt;&gt;&gt; two suggestions I got from the list when I did similar things:<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; 1. wrapfs<br>
&gt;&gt;&gt; 2. ecryptfs<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; I think you should check these two stackable filesystems if you haven&#39;t.<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; Hope this can help a little bit!<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; Freeman<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt;<br>
&gt;&gt;&gt; _______________________________________________<br>
&gt;&gt;&gt; Kernelnewbies mailing list<br>
&gt;&gt;&gt; <a href="mailto:Kernelnewbies@kernelnewbies.org">Kernelnewbies@kernelnewbies.org</a><br>
&gt;&gt;&gt; <a href="http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies">http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies</a><br>
&gt;&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt; Hi Amir,<br>
&gt;&gt;<br>
&gt;&gt; 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.<br>
&gt;&gt;<br>
&gt;&gt; Having said this, you should definitely debug the issue that you are facing. Some pointers : -<br>
&gt;&gt; 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.<br>
&gt;&gt; 2. re-read code to check, you might be messing up while write or read.<br>
&gt;&gt;<br>
&gt;&gt; Apart from these some basic practices you need to follow is : -<br>
&gt;&gt;<br>
&gt;&gt; 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.<br>
&gt;&gt; 2. copy_from_user &amp; again copying back to user is in-efficient.<br>
&gt;&gt;<br>
&gt;&gt;<br>
&gt;&gt; Enjoy life,<br>
&gt;&gt; Rohan<br>
&gt;<br>
&gt;<br>
&gt; _______________________________________________<br>
&gt; Kernelnewbies mailing list<br>
&gt; <a href="mailto:Kernelnewbies@kernelnewbies.org">Kernelnewbies@kernelnewbies.org</a><br>
&gt; <a href="http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies">http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies</a><br>
&gt;</p>
<p dir="ltr">Hi Amir,</p>
<p dir="ltr">Please reply at the bottom. Regarding what&#39;s the better way would depend on how you design stuff.</p>
<p dir="ltr">Following is my recommendation :-</p>
<p dir="ltr">For conceptual knowledge of general file systems the best would be OS book by Prof Remzi Arpaci-Dusseau.<br>
Excellently explained.</p>
<p dir="ltr">For linux kernel conceptual stuff get hold of Robert love Linux kernel development.</p>
<p dir="ltr">Read lots of kernel generic filesystem code in FS dir. Lots of basic functionality is implemented in helper functions present in this dir.</p>
<p dir="ltr">Enjoy life,<br>
Rohan</p>