Let consider following code. For someone who read Linux Device Driver the context will be clear.
In short, sbull is a driver which acts a disk device and the sbull_transfer function aims to transfer block of data from/to disk/user_space.
And everything is clear expect one thing. I don't understand why we can just simply use memcpy function. After all, we copy from user space so why it is possible? Usually, I noticed, that we should use copy_from_user()/copy_to_user().
I don't understand why we needn't to use them. Please explain.
static void sbull_transfer(struct sbull_dev *dev, unsigned long sector,
unsigned long nsect, char *buffer, int write)
{
unsigned long offset = sector*KERNEL_SECTOR_SIZE;
unsigned long nbytes = nsect*KERNEL_SECTOR_SIZE;
if ((offset + nbytes) > dev->size) {
printk (KERN_NOTICE "Beyond-end write (%ld %ld)\n", offset, nbytes);
return;
}
if (write)
memcpy(dev->data + offset, buffer, nbytes);
else
memcpy(buffer, dev->data + offset, nbytes);
}