Commit 2f28cc35 authored by Andrew Morton's avatar Andrew Morton Committed by Christoph Hellwig

[PATCH] tmpfs: shmem_file_sendfile

Patch from Hugh Dickins

Added shmem_file_sendfile to allow sendfile from tmpfs.  Checked
do_shmem_file_read and shmem_file_read against filemap equivalents
to add in any recent fixes (-EINVAL when count < 0 was missing).
parent 1956960b
...@@ -1239,6 +1239,7 @@ extern int sb_min_blocksize(struct super_block *, int); ...@@ -1239,6 +1239,7 @@ extern int sb_min_blocksize(struct super_block *, int);
extern int generic_file_mmap(struct file *, struct vm_area_struct *); extern int generic_file_mmap(struct file *, struct vm_area_struct *);
extern int file_read_actor(read_descriptor_t * desc, struct page *page, unsigned long offset, unsigned long size); extern int file_read_actor(read_descriptor_t * desc, struct page *page, unsigned long offset, unsigned long size);
extern int file_send_actor(read_descriptor_t * desc, struct page *page, unsigned long offset, unsigned long size);
extern ssize_t generic_file_read(struct file *, char *, size_t, loff_t *); extern ssize_t generic_file_read(struct file *, char *, size_t, loff_t *);
extern ssize_t generic_file_write(struct file *, const char *, size_t, loff_t *); extern ssize_t generic_file_write(struct file *, const char *, size_t, loff_t *);
extern ssize_t generic_file_aio_read(struct kiocb *, char *, size_t, loff_t); extern ssize_t generic_file_aio_read(struct kiocb *, char *, size_t, loff_t);
......
...@@ -916,7 +916,7 @@ generic_file_read(struct file *filp, char *buf, size_t count, loff_t *ppos) ...@@ -916,7 +916,7 @@ generic_file_read(struct file *filp, char *buf, size_t count, loff_t *ppos)
return ret; return ret;
} }
static int file_send_actor(read_descriptor_t * desc, struct page *page, unsigned long offset, unsigned long size) int file_send_actor(read_descriptor_t * desc, struct page *page, unsigned long offset, unsigned long size)
{ {
ssize_t written; ssize_t written;
unsigned long count = desc->count; unsigned long count = desc->count;
......
...@@ -1136,19 +1136,18 @@ shmem_file_write(struct file *file, const char *buf, size_t count, loff_t *ppos) ...@@ -1136,19 +1136,18 @@ shmem_file_write(struct file *file, const char *buf, size_t count, loff_t *ppos)
goto release; goto release;
} }
static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc) static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_t *desc, read_actor_t actor)
{ {
struct inode *inode = filp->f_dentry->d_inode; struct inode *inode = filp->f_dentry->d_inode;
struct address_space *mapping = inode->i_mapping; struct address_space *mapping = inode->i_mapping;
unsigned long index, offset; unsigned long index, offset;
int nr = 1;
index = *ppos >> PAGE_CACHE_SHIFT; index = *ppos >> PAGE_CACHE_SHIFT;
offset = *ppos & ~PAGE_CACHE_MASK; offset = *ppos & ~PAGE_CACHE_MASK;
while (nr && desc->count) { for (;;) {
struct page *page; struct page *page;
unsigned long end_index, nr; unsigned long end_index, nr, ret;
end_index = inode->i_size >> PAGE_CACHE_SHIFT; end_index = inode->i_size >> PAGE_CACHE_SHIFT;
if (index > end_index) if (index > end_index)
...@@ -1181,6 +1180,10 @@ static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_ ...@@ -1181,6 +1180,10 @@ static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_
} }
nr -= offset; nr -= offset;
/* If users can be writing to this page using arbitrary
* virtual addresses, take care about potential aliasing
* before reading the page on the kernel side.
*/
if (!list_empty(&mapping->i_mmap_shared) && if (!list_empty(&mapping->i_mmap_shared) &&
page != ZERO_PAGE(0)) page != ZERO_PAGE(0))
flush_dcache_page(page); flush_dcache_page(page);
...@@ -1195,12 +1198,14 @@ static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_ ...@@ -1195,12 +1198,14 @@ static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_
* "pos" here (the actor routine has to update the user buffer * "pos" here (the actor routine has to update the user buffer
* pointers and the remaining count). * pointers and the remaining count).
*/ */
nr = file_read_actor(desc, page, offset, nr); ret = actor(desc, page, offset, nr);
offset += nr; offset += ret;
index += offset >> PAGE_CACHE_SHIFT; index += offset >> PAGE_CACHE_SHIFT;
offset &= ~PAGE_CACHE_MASK; offset &= ~PAGE_CACHE_MASK;
page_cache_release(page); page_cache_release(page);
if (ret != nr || !desc->count)
break;
} }
*ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset; *ppos = ((loff_t) index << PAGE_CACHE_SHIFT) + offset;
...@@ -1209,27 +1214,43 @@ static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_ ...@@ -1209,27 +1214,43 @@ static void do_shmem_file_read(struct file *filp, loff_t *ppos, read_descriptor_
static ssize_t shmem_file_read(struct file *filp, char *buf, size_t count, loff_t *ppos) static ssize_t shmem_file_read(struct file *filp, char *buf, size_t count, loff_t *ppos)
{ {
ssize_t retval; read_descriptor_t desc;
if ((ssize_t) count < 0)
return -EINVAL;
if (!access_ok(VERIFY_WRITE, buf, count))
return -EFAULT;
if (!count)
return 0;
retval = -EFAULT; desc.written = 0;
if (access_ok(VERIFY_WRITE, buf, count)) { desc.count = count;
retval = 0; desc.buf = buf;
desc.error = 0;
if (count) { do_shmem_file_read(filp, ppos, &desc, file_read_actor);
read_descriptor_t desc; if (desc.written)
return desc.written;
return desc.error;
}
desc.written = 0; static ssize_t shmem_file_sendfile(struct file *out_file,
desc.count = count; struct file *in_file, loff_t *ppos, size_t count)
desc.buf = buf; {
desc.error = 0; read_descriptor_t desc;
do_shmem_file_read(filp, ppos, &desc);
retval = desc.written; if (!count)
if (!retval) return 0;
retval = desc.error;
} desc.written = 0;
} desc.count = count;
return retval; desc.buf = (char *)out_file;
desc.error = 0;
do_shmem_file_read(in_file, ppos, &desc, file_send_actor);
if (desc.written)
return desc.written;
return desc.error;
} }
static int shmem_statfs(struct super_block *sb, struct statfs *buf) static int shmem_statfs(struct super_block *sb, struct statfs *buf)
...@@ -1649,6 +1670,7 @@ static struct file_operations shmem_file_operations = { ...@@ -1649,6 +1670,7 @@ static struct file_operations shmem_file_operations = {
.read = shmem_file_read, .read = shmem_file_read,
.write = shmem_file_write, .write = shmem_file_write,
.fsync = simple_sync_file, .fsync = simple_sync_file,
.sendfile = shmem_file_sendfile,
#endif #endif
}; };
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment