<p dir="ltr"><br>
On 13 Jul 2015 22:08, "Amir Hezarkhani" <<a href="mailto:amir6723@gmail.com">amir6723@gmail.com</a>> wrote:<br>
><br>
><br>
> On Jul 13, 2015 1:18 AM, "Rohan Puri" <<a href="mailto:rohan.puri15@gmail.com">rohan.puri15@gmail.com</a>> wrote:<br>
> ><br>
> > No issues, you are welcome.<br>
> ><br>
> > Enjoy life,<br>
> > Rohan<br>
> ><br>
> > On 13 Jul 2015 01:19, "Amir Hezarkhani" <<a href="mailto:amir6723@gmail.com">amir6723@gmail.com</a>> wrote:<br>
> >><br>
> >><br>
> >> On Jul 12, 2015 10:00 PM, "Rohan Puri" <<a href="mailto:rohan.puri15@gmail.com">rohan.puri15@gmail.com</a>> wrote:<br>
> >> ><br>
> >> ><br>
> >> > On 12 Jul 2015 22:20, "Amir Hezarkhani" <<a href="mailto:amir6723@gmail.com">amir6723@gmail.com</a>> wrote:<br>
> >> > ><br>
> >> > > 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.<br>
> >> > ><br>
> >> > > On Jul 12, 2015 8:30 PM, <<a href="mailto:kernelnewbies-request@kernelnewbies.org">kernelnewbies-request@kernelnewbies.org</a>> wrote:<br>
> >> > >><br>
> >> > >><br>
> >> > >><br>
> >> > >> On Sun, Jul 12, 2015 at 8:08 PM, Freeman Zhang <<a href="mailto:freeman.zhang1992@gmail.com">freeman.zhang1992@gmail.com</a>> wrote:<br>
> >> > >>><br>
> >> > >>> -------- 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>
> >> > >>> 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>
> >> > >>><br>
> >> > >>> Freeman<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">http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies</a><br>
> >> > >>><br>
> >> > >><br>
> >> > >> Hi Amir,<br>
> >> > >><br>
> >> > >> 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>
> >> > >><br>
> >> > >> Having said this, you should definitely debug the issue that you are facing. Some pointers : -<br>
> >> > >> 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>
> >> > >> 2. re-read code to check, you might be messing up while write or read.<br>
> >> > >><br>
> >> > >> Apart from these some basic practices you need to follow is : -<br>
> >> > >><br>
> >> > >> 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>
> >> > >> 2. copy_from_user & again copying back to user is in-efficient.<br>
><br>
> Yes you are right. This was the problem.<br>
><br>
> >> > >><br>
> >> > >><br>
> >> > >> Enjoy life,<br>
> >> > >> Rohan<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">http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies</a><br>
> >> > ><br>
> >> ><br>
> >> > Hi Amir,<br>
> >> ><br>
> >> > Please reply at the bottom. Regarding what's the better way would depend on how you design stuff.<br>
> >> ><br>
> >> > Following is my recommendation :-<br>
> >> ><br>
> >> > For conceptual knowledge of general file systems the best would be OS book by Prof Remzi Arpaci-Dusseau.<br>
> >> > Excellently explained.<br>
> >> ><br>
> >> > For linux kernel conceptual stuff get hold of Robert love Linux kernel development.<br>
> >> ><br>
> >> > Read lots of kernel generic filesystem code in FS dir. Lots of basic functionality is implemented in helper functions present in this dir.<br>
> >> ><br>
> >> > Enjoy life,<br>
> >> > Rohan<br>
> >><br>
> >> Thanks a lot Rohan. And sorry about the bad reply.<br>
><br>
> Ok I solved the problem. As Rohan said, copy_from_user and then copy_to_user was inefficient. So I removed 'copy to user' part and assigned 'data' to iov_enc.base . thanks guys and Happy codding.</p>
<p dir="ltr">Hi Amir,</p>
<p dir="ltr">Good to know.</p>
<p dir="ltr">Enjoy life,<br>
Rohan</p>