[PATCH] Vectorize aio_read/aio_write fileop methods
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / mm / filemap.c
index d087fc3d3281cdeab1d751652b34702d65bf023e..f6c1d22b504fed71b4239330a42817b7c66ad91b 100644 (file)
@@ -488,6 +488,12 @@ struct page *page_cache_alloc_cold(struct address_space *x)
 EXPORT_SYMBOL(page_cache_alloc_cold);
 #endif
 
+static int __sleep_on_page_lock(void *word)
+{
+       io_schedule();
+       return 0;
+}
+
 /*
  * In order to wait for pages to become available there must be
  * waitqueues associated with pages. By using a hash table of
@@ -577,13 +583,24 @@ void fastcall __lock_page(struct page *page)
 }
 EXPORT_SYMBOL(__lock_page);
 
+/*
+ * Variant of lock_page that does not require the caller to hold a reference
+ * on the page's mapping.
+ */
+void fastcall __lock_page_nosync(struct page *page)
+{
+       DEFINE_WAIT_BIT(wait, &page->flags, PG_locked);
+       __wait_on_bit_lock(page_waitqueue(page), &wait, __sleep_on_page_lock,
+                                                       TASK_UNINTERRUPTIBLE);
+}
+
 /**
  * find_get_page - find and get a page reference
  * @mapping: the address_space to search
  * @offset: the page index
  *
- * A rather lightweight function, finding and getting a reference to a
- * hashed page atomically.
+ * Is there a pagecache struct page at the given (mapping, offset) tuple?
+ * If yes, increment its refcount and return it; if no, return NULL.
  */
 struct page * find_get_page(struct address_space *mapping, unsigned long offset)
 {
@@ -849,8 +866,6 @@ static void shrink_readahead_size_eio(struct file *filp,
                return;
 
        ra->ra_pages /= 4;
-       printk(KERN_WARNING "Reducing readahead size to %luK\n",
-                       ra->ra_pages << (PAGE_CACHE_SHIFT - 10));
 }
 
 /**
@@ -972,7 +987,7 @@ page_not_up_to_date:
                /* Get exclusive access to the page ... */
                lock_page(page);
 
-               /* Did it get unhashed before we got the lock? */
+               /* Did it get truncated before we got the lock? */
                if (!page->mapping) {
                        unlock_page(page);
                        page_cache_release(page);
@@ -1211,12 +1226,11 @@ out:
 EXPORT_SYMBOL(__generic_file_aio_read);
 
 ssize_t
-generic_file_aio_read(struct kiocb *iocb, char __user *buf, size_t count, loff_t pos)
+generic_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
+               unsigned long nr_segs, loff_t pos)
 {
-       struct iovec local_iov = { .iov_base = buf, .iov_len = count };
-
        BUG_ON(iocb->ki_pos != pos);
-       return __generic_file_aio_read(iocb, &local_iov, 1, &iocb->ki_pos);
+       return __generic_file_aio_read(iocb, iov, nr_segs, &iocb->ki_pos);
 }
 EXPORT_SYMBOL(generic_file_aio_read);
 
@@ -1456,7 +1470,7 @@ outside_data_content:
         * accessible..
         */
        if (area->vm_mm == current->mm)
-               return NULL;
+               return NOPAGE_SIGBUS;
        /* Fall through to the non-read-ahead case */
 no_cached_page:
        /*
@@ -1481,7 +1495,7 @@ no_cached_page:
         */
        if (error == -ENOMEM)
                return NOPAGE_OOM;
-       return NULL;
+       return NOPAGE_SIGBUS;
 
 page_not_uptodate:
        if (!did_readaround) {
@@ -1550,7 +1564,7 @@ page_not_uptodate:
         */
        shrink_readahead_size_eio(file, ra);
        page_cache_release(page);
-       return NULL;
+       return NOPAGE_SIGBUS;
 }
 EXPORT_SYMBOL(filemap_nopage);
 
@@ -1612,7 +1626,7 @@ no_cached_page:
 page_not_uptodate:
        lock_page(page);
 
-       /* Did it get unhashed while we waited for it? */
+       /* Did it get truncated while we waited for it? */
        if (!page->mapping) {
                unlock_page(page);
                goto err;
@@ -2005,6 +2019,7 @@ inline int generic_write_checks(struct file *file, loff_t *pos, size_t *count, i
                if (unlikely(*pos + *count > inode->i_sb->s_maxbytes))
                        *count = inode->i_sb->s_maxbytes - *pos;
        } else {
+#ifdef CONFIG_BLOCK
                loff_t isize;
                if (bdev_read_only(I_BDEV(inode)))
                        return -EPERM;
@@ -2016,6 +2031,9 @@ inline int generic_write_checks(struct file *file, loff_t *pos, size_t *count, i
 
                if (*pos + *count > isize)
                        *count = isize - *pos;
+#else
+               return -EPERM;
+#endif
        }
        return 0;
 }
@@ -2296,22 +2314,22 @@ out:
        current->backing_dev_info = NULL;
        return written ? written : err;
 }
-EXPORT_SYMBOL(generic_file_aio_write_nolock);
 
-ssize_t
-generic_file_aio_write_nolock(struct kiocb *iocb, const struct iovec *iov,
-                               unsigned long nr_segs, loff_t *ppos)
+ssize_t generic_file_aio_write_nolock(struct kiocb *iocb,
+               const struct iovec *iov, unsigned long nr_segs, loff_t pos)
 {
        struct file *file = iocb->ki_filp;
        struct address_space *mapping = file->f_mapping;
        struct inode *inode = mapping->host;
        ssize_t ret;
-       loff_t pos = *ppos;
 
-       ret = __generic_file_aio_write_nolock(iocb, iov, nr_segs, ppos);
+       BUG_ON(iocb->ki_pos != pos);
+
+       ret = __generic_file_aio_write_nolock(iocb, iov, nr_segs,
+                       &iocb->ki_pos);
 
        if (ret > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
-               int err;
+               ssize_t err;
 
                err = sync_page_range_nolock(inode, mapping, pos, ret);
                if (err < 0)
@@ -2319,6 +2337,7 @@ generic_file_aio_write_nolock(struct kiocb *iocb, const struct iovec *iov,
        }
        return ret;
 }
+EXPORT_SYMBOL(generic_file_aio_write_nolock);
 
 static ssize_t
 __generic_file_write_nolock(struct file *file, const struct iovec *iov,
@@ -2328,8 +2347,9 @@ __generic_file_write_nolock(struct file *file, const struct iovec *iov,
        ssize_t ret;
 
        init_sync_kiocb(&kiocb, file);
+       kiocb.ki_pos = *ppos;
        ret = __generic_file_aio_write_nolock(&kiocb, iov, nr_segs, ppos);
-       if (ret == -EIOCBQUEUED)
+       if (-EIOCBQUEUED == ret)
                ret = wait_on_sync_kiocb(&kiocb);
        return ret;
 }
@@ -2342,28 +2362,28 @@ generic_file_write_nolock(struct file *file, const struct iovec *iov,
        ssize_t ret;
 
        init_sync_kiocb(&kiocb, file);
-       ret = generic_file_aio_write_nolock(&kiocb, iov, nr_segs, ppos);
+       kiocb.ki_pos = *ppos;
+       ret = generic_file_aio_write_nolock(&kiocb, iov, nr_segs, *ppos);
        if (-EIOCBQUEUED == ret)
                ret = wait_on_sync_kiocb(&kiocb);
+       *ppos = kiocb.ki_pos;
        return ret;
 }
 EXPORT_SYMBOL(generic_file_write_nolock);
 
-ssize_t generic_file_aio_write(struct kiocb *iocb, const char __user *buf,
-                              size_t count, loff_t pos)
+ssize_t generic_file_aio_write(struct kiocb *iocb, const struct iovec *iov,
+               unsigned long nr_segs, loff_t pos)
 {
        struct file *file = iocb->ki_filp;
        struct address_space *mapping = file->f_mapping;
        struct inode *inode = mapping->host;
        ssize_t ret;
-       struct iovec local_iov = { .iov_base = (void __user *)buf,
-                                       .iov_len = count };
 
        BUG_ON(iocb->ki_pos != pos);
 
        mutex_lock(&inode->i_mutex);
-       ret = __generic_file_aio_write_nolock(iocb, &local_iov, 1,
-                                               &iocb->ki_pos);
+       ret = __generic_file_aio_write_nolock(iocb, iov, nr_segs,
+                       &iocb->ki_pos);
        mutex_unlock(&inode->i_mutex);
 
        if (ret > 0 && ((file->f_flags & O_SYNC) || IS_SYNC(inode))) {
@@ -2476,3 +2496,33 @@ generic_file_direct_IO(int rw, struct kiocb *iocb, const struct iovec *iov,
        }
        return retval;
 }
+
+/**
+ * try_to_release_page() - release old fs-specific metadata on a page
+ *
+ * @page: the page which the kernel is trying to free
+ * @gfp_mask: memory allocation flags (and I/O mode)
+ *
+ * The address_space is to try to release any data against the page
+ * (presumably at page->private).  If the release was successful, return `1'.
+ * Otherwise return zero.
+ *
+ * The @gfp_mask argument specifies whether I/O may be performed to release
+ * this page (__GFP_IO), and whether the call may block (__GFP_WAIT).
+ *
+ * NOTE: @gfp_mask may go away, and this function may become non-blocking.
+ */
+int try_to_release_page(struct page *page, gfp_t gfp_mask)
+{
+       struct address_space * const mapping = page->mapping;
+
+       BUG_ON(!PageLocked(page));
+       if (PageWriteback(page))
+               return 0;
+
+       if (mapping && mapping->a_ops->releasepage)
+               return mapping->a_ops->releasepage(page, gfp_mask);
+       return try_to_free_buffers(page);
+}
+
+EXPORT_SYMBOL(try_to_release_page);