<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'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, <<a href="mailto:kernelnewbies-request@kernelnewbies.org">kernelnewbies-request@kernelnewbies.org</a>> 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"><<a href="mailto:freeman.zhang1992@gmail.com" target="_blank">freeman.zhang1992@gmail.com</a>></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>
> hello<br>
> I am working on adding a simple encryption to file contents in ext4 driver<br>
> (for learning purposes) I added simple XOR encryption to aio_read and<br>
> aio_write functions and it worked until I faced this problem:<br>
><br>
> when I open a file in encrypted filesystem using VIM text editor and when I<br>
> try to save it it gives me this error:<br>
><br>
>>> pointer block id wrong<br>
>>> can not find line 1<br>
><br>
> and it just corrupts the entire file!<br>
><br>
> this is my aio_write function:<br>
><br>
> aio_write_enc(struct kiocb *iocb, const struct iovec *iov,<br>
> unsigned long nr_segs, loff_t pos)<br>
> {<br>
> size_t i;<br>
> ssize_t ret;<br>
> char *data=vmalloc(sizeof(char)*iov->iov_len);<br>
> copy_from_user(data,iov->iov_base,iov->iov_len);<br>
><br>
> for(i=0;i<iov->iov_len;i++)<br>
> {<br>
> data[i]^=5;<br>
> }<br>
> struct iovec iov_enc= { .iov_base = iov->iov_base, .iov_len =<br>
> iov->iov_len };<br>
><br>
> copy_to_user(iov_enc.iov_base,data,iov->iov_len);<br>
> ret=ext4_file_write(iocb,&iov_enc,nr_segs,pos);<br>
> vfree(data);<br>
> return ret;<br>
> }<br>
><br>
> this just changes the data and then calls original function.<br>
><br>
> is there anything wrong with this function? can anyone help me?<br>
><br>
><br>
><br>
</div></div>Hi Amir,<br>
<br>
I'm not quite sure about what'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'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 & 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>