Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/ebiederm...
[firefly-linux-kernel-4.4.55.git] / fs / cifs / file.c
index 666069811ab854daabf8683d39457e2f7129b001..4ab2f79ffa7a4eb7f2e6ac3a2a8d67131d8f8080 100644 (file)
@@ -1670,8 +1670,8 @@ cifs_write(struct cifsFileInfo *open_file, __u32 pid, const char *write_data,
                                        break;
                        }
 
-                       len = min((size_t)cifs_sb->wsize,
-                                 write_size - total_written);
+                       len = min(server->ops->wp_retry_size(dentry->d_inode),
+                                 (unsigned int)write_size - total_written);
                        /* iov[0] is reserved for smb header */
                        iov[1].iov_base = (char *)write_data + total_written;
                        iov[1].iov_len = len;
@@ -2031,6 +2031,7 @@ static int cifs_writepages(struct address_space *mapping,
                           struct writeback_control *wbc)
 {
        struct cifs_sb_info *cifs_sb = CIFS_SB(mapping->host->i_sb);
+       struct TCP_Server_Info *server;
        bool done = false, scanned = false, range_whole = false;
        pgoff_t end, index;
        struct cifs_writedata *wdata;
@@ -2053,23 +2054,30 @@ static int cifs_writepages(struct address_space *mapping,
                        range_whole = true;
                scanned = true;
        }
+       server = cifs_sb_master_tcon(cifs_sb)->ses->server;
 retry:
        while (!done && index <= end) {
-               unsigned int i, nr_pages, found_pages;
+               unsigned int i, nr_pages, found_pages, wsize, credits;
                pgoff_t next = 0, tofind, saved_index = index;
 
-               tofind = min((cifs_sb->wsize / PAGE_CACHE_SIZE) - 1,
-                               end - index) + 1;
+               rc = server->ops->wait_mtu_credits(server, cifs_sb->wsize,
+                                                  &wsize, &credits);
+               if (rc)
+                       break;
+
+               tofind = min((wsize / PAGE_CACHE_SIZE) - 1, end - index) + 1;
 
                wdata = wdata_alloc_and_fillpages(tofind, mapping, end, &index,
                                                  &found_pages);
                if (!wdata) {
                        rc = -ENOMEM;
+                       add_credits_and_wake_if(server, credits, 0);
                        break;
                }
 
                if (found_pages == 0) {
                        kref_put(&wdata->refcount, cifs_writedata_release);
+                       add_credits_and_wake_if(server, credits, 0);
                        break;
                }
 
@@ -2079,13 +2087,17 @@ retry:
                /* nothing to write? */
                if (nr_pages == 0) {
                        kref_put(&wdata->refcount, cifs_writedata_release);
+                       add_credits_and_wake_if(server, credits, 0);
                        continue;
                }
 
+               wdata->credits = credits;
+
                rc = wdata_send_pages(wdata, nr_pages, mapping, wbc);
 
                /* send failure -- clean up the mess */
                if (rc != 0) {
+                       add_credits_and_wake_if(server, wdata->credits, 0);
                        for (i = 0; i < nr_pages; ++i) {
                                if (rc == -EAGAIN)
                                        redirty_page_for_writepage(wbc,
@@ -2401,28 +2413,6 @@ cifs_uncached_writev_complete(struct work_struct *work)
        kref_put(&wdata->refcount, cifs_uncached_writedata_release);
 }
 
-/* attempt to send write to server, retry on any -EAGAIN errors */
-static int
-cifs_uncached_retry_writev(struct cifs_writedata *wdata)
-{
-       int rc;
-       struct TCP_Server_Info *server;
-
-       server = tlink_tcon(wdata->cfile->tlink)->ses->server;
-
-       do {
-               if (wdata->cfile->invalidHandle) {
-                       rc = cifs_reopen_file(wdata->cfile, false);
-                       if (rc != 0)
-                               continue;
-               }
-               rc = server->ops->async_writev(wdata,
-                                              cifs_uncached_writedata_release);
-       } while (rc == -EAGAIN);
-
-       return rc;
-}
-
 static int
 wdata_fill_from_iovec(struct cifs_writedata *wdata, struct iov_iter *from,
                      size_t *len, unsigned long *num_pages)
@@ -2474,25 +2464,40 @@ cifs_write_from_iter(loff_t offset, size_t len, struct iov_iter *from,
        size_t cur_len;
        unsigned long nr_pages, num_pages, i;
        struct cifs_writedata *wdata;
+       struct iov_iter saved_from;
+       loff_t saved_offset = offset;
        pid_t pid;
+       struct TCP_Server_Info *server;
 
        if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
                pid = open_file->pid;
        else
                pid = current->tgid;
 
+       server = tlink_tcon(open_file->tlink)->ses->server;
+       memcpy(&saved_from, from, sizeof(struct iov_iter));
+
        do {
-               nr_pages = get_numpages(cifs_sb->wsize, len, &cur_len);
+               unsigned int wsize, credits;
+
+               rc = server->ops->wait_mtu_credits(server, cifs_sb->wsize,
+                                                  &wsize, &credits);
+               if (rc)
+                       break;
+
+               nr_pages = get_numpages(wsize, len, &cur_len);
                wdata = cifs_writedata_alloc(nr_pages,
                                             cifs_uncached_writev_complete);
                if (!wdata) {
                        rc = -ENOMEM;
+                       add_credits_and_wake_if(server, credits, 0);
                        break;
                }
 
                rc = cifs_write_allocate_pages(wdata->pages, nr_pages);
                if (rc) {
                        kfree(wdata);
+                       add_credits_and_wake_if(server, credits, 0);
                        break;
                }
 
@@ -2502,6 +2507,7 @@ cifs_write_from_iter(loff_t offset, size_t len, struct iov_iter *from,
                        for (i = 0; i < nr_pages; i++)
                                put_page(wdata->pages[i]);
                        kfree(wdata);
+                       add_credits_and_wake_if(server, credits, 0);
                        break;
                }
 
@@ -2520,10 +2526,22 @@ cifs_write_from_iter(loff_t offset, size_t len, struct iov_iter *from,
                wdata->bytes = cur_len;
                wdata->pagesz = PAGE_SIZE;
                wdata->tailsz = cur_len - ((nr_pages - 1) * PAGE_SIZE);
-               rc = cifs_uncached_retry_writev(wdata);
+               wdata->credits = credits;
+
+               if (!wdata->cfile->invalidHandle ||
+                   !cifs_reopen_file(wdata->cfile, false))
+                       rc = server->ops->async_writev(wdata,
+                                       cifs_uncached_writedata_release);
                if (rc) {
+                       add_credits_and_wake_if(server, wdata->credits, 0);
                        kref_put(&wdata->refcount,
                                 cifs_uncached_writedata_release);
+                       if (rc == -EAGAIN) {
+                               memcpy(from, &saved_from,
+                                      sizeof(struct iov_iter));
+                               iov_iter_advance(from, offset - saved_offset);
+                               continue;
+                       }
                        break;
                }
 
@@ -2545,6 +2563,7 @@ cifs_iovec_write(struct file *file, struct iov_iter *from, loff_t *poffset)
        struct cifs_sb_info *cifs_sb;
        struct cifs_writedata *wdata, *tmp;
        struct list_head wdata_list;
+       struct iov_iter saved_from;
        int rc;
 
        len = iov_iter_count(from);
@@ -2565,6 +2584,8 @@ cifs_iovec_write(struct file *file, struct iov_iter *from, loff_t *poffset)
        if (!tcon->ses->server->ops->async_writev)
                return -ENOSYS;
 
+       memcpy(&saved_from, from, sizeof(struct iov_iter));
+
        rc = cifs_write_from_iter(*poffset, len, from, open_file, cifs_sb,
                                  &wdata_list);
 
@@ -2596,7 +2617,25 @@ restart_loop:
 
                        /* resend call if it's a retryable error */
                        if (rc == -EAGAIN) {
-                               rc = cifs_uncached_retry_writev(wdata);
+                               struct list_head tmp_list;
+                               struct iov_iter tmp_from;
+
+                               INIT_LIST_HEAD(&tmp_list);
+                               list_del_init(&wdata->list);
+
+                               memcpy(&tmp_from, &saved_from,
+                                      sizeof(struct iov_iter));
+                               iov_iter_advance(&tmp_from,
+                                                wdata->offset - *poffset);
+
+                               rc = cifs_write_from_iter(wdata->offset,
+                                               wdata->bytes, &tmp_from,
+                                               open_file, cifs_sb, &tmp_list);
+
+                               list_splice(&tmp_list, &wdata_list);
+
+                               kref_put(&wdata->refcount,
+                                        cifs_uncached_writedata_release);
                                goto restart_loop;
                        }
                }
@@ -2789,26 +2828,6 @@ cifs_uncached_readdata_release(struct kref *refcount)
        cifs_readdata_release(refcount);
 }
 
-static int
-cifs_retry_async_readv(struct cifs_readdata *rdata)
-{
-       int rc;
-       struct TCP_Server_Info *server;
-
-       server = tlink_tcon(rdata->cfile->tlink)->ses->server;
-
-       do {
-               if (rdata->cfile->invalidHandle) {
-                       rc = cifs_reopen_file(rdata->cfile, true);
-                       if (rc != 0)
-                               continue;
-               }
-               rc = server->ops->async_readv(rdata);
-       } while (rc == -EAGAIN);
-
-       return rc;
-}
-
 /**
  * cifs_readdata_to_iov - copy data from pages in response to an iovec
  * @rdata:     the readdata response with list of pages holding data
@@ -2821,7 +2840,7 @@ cifs_retry_async_readv(struct cifs_readdata *rdata)
 static int
 cifs_readdata_to_iov(struct cifs_readdata *rdata, struct iov_iter *iter)
 {
-       size_t remaining = rdata->bytes;
+       size_t remaining = rdata->got_bytes;
        unsigned int i;
 
        for (i = 0; i < rdata->nr_pages; i++) {
@@ -2849,11 +2868,12 @@ static int
 cifs_uncached_read_into_pages(struct TCP_Server_Info *server,
                        struct cifs_readdata *rdata, unsigned int len)
 {
-       int total_read = 0, result = 0;
+       int result = 0;
        unsigned int i;
        unsigned int nr_pages = rdata->nr_pages;
        struct kvec iov;
 
+       rdata->got_bytes = 0;
        rdata->tailsz = PAGE_SIZE;
        for (i = 0; i < nr_pages; i++) {
                struct page *page = rdata->pages[i];
@@ -2887,55 +2907,45 @@ cifs_uncached_read_into_pages(struct TCP_Server_Info *server,
                if (result < 0)
                        break;
 
-               total_read += result;
+               rdata->got_bytes += result;
        }
 
-       return total_read > 0 && result != -EAGAIN ? total_read : result;
+       return rdata->got_bytes > 0 && result != -ECONNABORTED ?
+                                               rdata->got_bytes : result;
 }
 
-ssize_t cifs_user_readv(struct kiocb *iocb, struct iov_iter *to)
+static int
+cifs_send_async_read(loff_t offset, size_t len, struct cifsFileInfo *open_file,
+                    struct cifs_sb_info *cifs_sb, struct list_head *rdata_list)
 {
-       struct file *file = iocb->ki_filp;
-       ssize_t rc;
-       size_t len, cur_len;
-       ssize_t total_read = 0;
-       loff_t offset = iocb->ki_pos;
-       unsigned int npages;
-       struct cifs_sb_info *cifs_sb;
-       struct cifs_tcon *tcon;
-       struct cifsFileInfo *open_file;
-       struct cifs_readdata *rdata, *tmp;
-       struct list_head rdata_list;
+       struct cifs_readdata *rdata;
+       unsigned int npages, rsize, credits;
+       size_t cur_len;
+       int rc;
        pid_t pid;
+       struct TCP_Server_Info *server;
 
-       len = iov_iter_count(to);
-       if (!len)
-               return 0;
-
-       INIT_LIST_HEAD(&rdata_list);
-       cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
-       open_file = file->private_data;
-       tcon = tlink_tcon(open_file->tlink);
-
-       if (!tcon->ses->server->ops->async_readv)
-               return -ENOSYS;
+       server = tlink_tcon(open_file->tlink)->ses->server;
 
        if (cifs_sb->mnt_cifs_flags & CIFS_MOUNT_RWPIDFORWARD)
                pid = open_file->pid;
        else
                pid = current->tgid;
 
-       if ((file->f_flags & O_ACCMODE) == O_WRONLY)
-               cifs_dbg(FYI, "attempting read on write only file instance\n");
-
        do {
-               cur_len = min_t(const size_t, len - total_read, cifs_sb->rsize);
+               rc = server->ops->wait_mtu_credits(server, cifs_sb->rsize,
+                                                  &rsize, &credits);
+               if (rc)
+                       break;
+
+               cur_len = min_t(const size_t, len, rsize);
                npages = DIV_ROUND_UP(cur_len, PAGE_SIZE);
 
                /* allocate a readdata struct */
                rdata = cifs_readdata_alloc(npages,
                                            cifs_uncached_readv_complete);
                if (!rdata) {
+                       add_credits_and_wake_if(server, credits, 0);
                        rc = -ENOMEM;
                        break;
                }
@@ -2951,44 +2961,113 @@ ssize_t cifs_user_readv(struct kiocb *iocb, struct iov_iter *to)
                rdata->pid = pid;
                rdata->pagesz = PAGE_SIZE;
                rdata->read_into_pages = cifs_uncached_read_into_pages;
+               rdata->credits = credits;
 
-               rc = cifs_retry_async_readv(rdata);
+               if (!rdata->cfile->invalidHandle ||
+                   !cifs_reopen_file(rdata->cfile, true))
+                       rc = server->ops->async_readv(rdata);
 error:
                if (rc) {
+                       add_credits_and_wake_if(server, rdata->credits, 0);
                        kref_put(&rdata->refcount,
                                 cifs_uncached_readdata_release);
+                       if (rc == -EAGAIN)
+                               continue;
                        break;
                }
 
-               list_add_tail(&rdata->list, &rdata_list);
+               list_add_tail(&rdata->list, rdata_list);
                offset += cur_len;
                len -= cur_len;
        } while (len > 0);
 
+       return rc;
+}
+
+ssize_t cifs_user_readv(struct kiocb *iocb, struct iov_iter *to)
+{
+       struct file *file = iocb->ki_filp;
+       ssize_t rc;
+       size_t len;
+       ssize_t total_read = 0;
+       loff_t offset = iocb->ki_pos;
+       struct cifs_sb_info *cifs_sb;
+       struct cifs_tcon *tcon;
+       struct cifsFileInfo *open_file;
+       struct cifs_readdata *rdata, *tmp;
+       struct list_head rdata_list;
+
+       len = iov_iter_count(to);
+       if (!len)
+               return 0;
+
+       INIT_LIST_HEAD(&rdata_list);
+       cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
+       open_file = file->private_data;
+       tcon = tlink_tcon(open_file->tlink);
+
+       if (!tcon->ses->server->ops->async_readv)
+               return -ENOSYS;
+
+       if ((file->f_flags & O_ACCMODE) == O_WRONLY)
+               cifs_dbg(FYI, "attempting read on write only file instance\n");
+
+       rc = cifs_send_async_read(offset, len, open_file, cifs_sb, &rdata_list);
+
        /* if at least one read request send succeeded, then reset rc */
        if (!list_empty(&rdata_list))
                rc = 0;
 
        len = iov_iter_count(to);
        /* the loop below should proceed in the order of increasing offsets */
+again:
        list_for_each_entry_safe(rdata, tmp, &rdata_list, list) {
-       again:
                if (!rc) {
                        /* FIXME: freezable sleep too? */
                        rc = wait_for_completion_killable(&rdata->done);
                        if (rc)
                                rc = -EINTR;
-                       else if (rdata->result) {
-                               rc = rdata->result;
+                       else if (rdata->result == -EAGAIN) {
                                /* resend call if it's a retryable error */
-                               if (rc == -EAGAIN) {
-                                       rc = cifs_retry_async_readv(rdata);
-                                       goto again;
+                               struct list_head tmp_list;
+                               unsigned int got_bytes = rdata->got_bytes;
+
+                               list_del_init(&rdata->list);
+                               INIT_LIST_HEAD(&tmp_list);
+
+                               /*
+                                * Got a part of data and then reconnect has
+                                * happened -- fill the buffer and continue
+                                * reading.
+                                */
+                               if (got_bytes && got_bytes < rdata->bytes) {
+                                       rc = cifs_readdata_to_iov(rdata, to);
+                                       if (rc) {
+                                               kref_put(&rdata->refcount,
+                                               cifs_uncached_readdata_release);
+                                               continue;
+                                       }
                                }
-                       } else {
+
+                               rc = cifs_send_async_read(
+                                               rdata->offset + got_bytes,
+                                               rdata->bytes - got_bytes,
+                                               rdata->cfile, cifs_sb,
+                                               &tmp_list);
+
+                               list_splice(&tmp_list, &rdata_list);
+
+                               kref_put(&rdata->refcount,
+                                        cifs_uncached_readdata_release);
+                               goto again;
+                       } else if (rdata->result)
+                               rc = rdata->result;
+                       else
                                rc = cifs_readdata_to_iov(rdata, to);
-                       }
 
+                       /* if there was a short read -- discard anything left */
+                       if (rdata->got_bytes && rdata->got_bytes < rdata->bytes)
+                               rc = -ENODATA;
                }
                list_del_init(&rdata->list);
                kref_put(&rdata->refcount, cifs_uncached_readdata_release);
@@ -3097,18 +3176,19 @@ cifs_read(struct file *file, char *read_data, size_t read_size, loff_t *offset)
 
        for (total_read = 0, cur_offset = read_data; read_size > total_read;
             total_read += bytes_read, cur_offset += bytes_read) {
-               current_read_size = min_t(uint, read_size - total_read, rsize);
-               /*
-                * For windows me and 9x we do not want to request more than it
-                * negotiated since it will refuse the read then.
-                */
-               if ((tcon->ses) && !(tcon->ses->capabilities &
+               do {
+                       current_read_size = min_t(uint, read_size - total_read,
+                                                 rsize);
+                       /*
+                        * For windows me and 9x we do not want to request more
+                        * than it negotiated since it will refuse the read
+                        * then.
+                        */
+                       if ((tcon->ses) && !(tcon->ses->capabilities &
                                tcon->ses->server->vals->cap_large_files)) {
-                       current_read_size = min_t(uint, current_read_size,
-                                       CIFSMaxBufSize);
-               }
-               rc = -EAGAIN;
-               while (rc == -EAGAIN) {
+                               current_read_size = min_t(uint,
+                                       current_read_size, CIFSMaxBufSize);
+                       }
                        if (open_file->invalidHandle) {
                                rc = cifs_reopen_file(open_file, true);
                                if (rc != 0)
@@ -3121,7 +3201,8 @@ cifs_read(struct file *file, char *read_data, size_t read_size, loff_t *offset)
                        rc = server->ops->sync_read(xid, open_file, &io_parms,
                                                    &bytes_read, &cur_offset,
                                                    &buf_type);
-               }
+               } while (rc == -EAGAIN);
+
                if (rc || (bytes_read == 0)) {
                        if (total_read) {
                                break;
@@ -3200,25 +3281,30 @@ int cifs_file_mmap(struct file *file, struct vm_area_struct *vma)
 static void
 cifs_readv_complete(struct work_struct *work)
 {
-       unsigned int i;
+       unsigned int i, got_bytes;
        struct cifs_readdata *rdata = container_of(work,
                                                struct cifs_readdata, work);
 
+       got_bytes = rdata->got_bytes;
        for (i = 0; i < rdata->nr_pages; i++) {
                struct page *page = rdata->pages[i];
 
                lru_cache_add_file(page);
 
-               if (rdata->result == 0) {
+               if (rdata->result == 0 ||
+                   (rdata->result == -EAGAIN && got_bytes)) {
                        flush_dcache_page(page);
                        SetPageUptodate(page);
                }
 
                unlock_page(page);
 
-               if (rdata->result == 0)
+               if (rdata->result == 0 ||
+                   (rdata->result == -EAGAIN && got_bytes))
                        cifs_readpage_to_fscache(rdata->mapping->host, page);
 
+               got_bytes -= min_t(unsigned int, PAGE_CACHE_SIZE, got_bytes);
+
                page_cache_release(page);
                rdata->pages[i] = NULL;
        }
@@ -3229,7 +3315,7 @@ static int
 cifs_readpages_read_into_pages(struct TCP_Server_Info *server,
                        struct cifs_readdata *rdata, unsigned int len)
 {
-       int total_read = 0, result = 0;
+       int result = 0;
        unsigned int i;
        u64 eof;
        pgoff_t eof_index;
@@ -3241,6 +3327,7 @@ cifs_readpages_read_into_pages(struct TCP_Server_Info *server,
        eof_index = eof ? (eof - 1) >> PAGE_CACHE_SHIFT : 0;
        cifs_dbg(FYI, "eof=%llu eof_index=%lu\n", eof, eof_index);
 
+       rdata->got_bytes = 0;
        rdata->tailsz = PAGE_CACHE_SIZE;
        for (i = 0; i < nr_pages; i++) {
                struct page *page = rdata->pages[i];
@@ -3295,10 +3382,70 @@ cifs_readpages_read_into_pages(struct TCP_Server_Info *server,
                if (result < 0)
                        break;
 
-               total_read += result;
+               rdata->got_bytes += result;
+       }
+
+       return rdata->got_bytes > 0 && result != -ECONNABORTED ?
+                                               rdata->got_bytes : result;
+}
+
+static int
+readpages_get_pages(struct address_space *mapping, struct list_head *page_list,
+                   unsigned int rsize, struct list_head *tmplist,
+                   unsigned int *nr_pages, loff_t *offset, unsigned int *bytes)
+{
+       struct page *page, *tpage;
+       unsigned int expected_index;
+       int rc;
+
+       INIT_LIST_HEAD(tmplist);
+
+       page = list_entry(page_list->prev, struct page, lru);
+
+       /*
+        * Lock the page and put it in the cache. Since no one else
+        * should have access to this page, we're safe to simply set
+        * PG_locked without checking it first.
+        */
+       __set_page_locked(page);
+       rc = add_to_page_cache_locked(page, mapping,
+                                     page->index, GFP_KERNEL);
+
+       /* give up if we can't stick it in the cache */
+       if (rc) {
+               __clear_page_locked(page);
+               return rc;
        }
 
-       return total_read > 0 && result != -EAGAIN ? total_read : result;
+       /* move first page to the tmplist */
+       *offset = (loff_t)page->index << PAGE_CACHE_SHIFT;
+       *bytes = PAGE_CACHE_SIZE;
+       *nr_pages = 1;
+       list_move_tail(&page->lru, tmplist);
+
+       /* now try and add more pages onto the request */
+       expected_index = page->index + 1;
+       list_for_each_entry_safe_reverse(page, tpage, page_list, lru) {
+               /* discontinuity ? */
+               if (page->index != expected_index)
+                       break;
+
+               /* would this page push the read over the rsize? */
+               if (*bytes + PAGE_CACHE_SIZE > rsize)
+                       break;
+
+               __set_page_locked(page);
+               if (add_to_page_cache_locked(page, mapping, page->index,
+                                                               GFP_KERNEL)) {
+                       __clear_page_locked(page);
+                       break;
+               }
+               list_move_tail(&page->lru, tmplist);
+               (*bytes) += PAGE_CACHE_SIZE;
+               expected_index++;
+               (*nr_pages)++;
+       }
+       return rc;
 }
 
 static int cifs_readpages(struct file *file, struct address_space *mapping,
@@ -3308,18 +3455,9 @@ static int cifs_readpages(struct file *file, struct address_space *mapping,
        struct list_head tmplist;
        struct cifsFileInfo *open_file = file->private_data;
        struct cifs_sb_info *cifs_sb = CIFS_SB(file->f_path.dentry->d_sb);
-       unsigned int rsize = cifs_sb->rsize;
+       struct TCP_Server_Info *server;
        pid_t pid;
 
-       /*
-        * Give up immediately if rsize is too small to read an entire page.
-        * The VFS will fall back to readpage. We should never reach this
-        * point however since we set ra_pages to 0 when the rsize is smaller
-        * than a cache page.
-        */
-       if (unlikely(rsize < PAGE_CACHE_SIZE))
-               return 0;
-
        /*
         * Reads as many pages as possible from fscache. Returns -ENOBUFS
         * immediately if the cookie is negative
@@ -3338,7 +3476,7 @@ static int cifs_readpages(struct file *file, struct address_space *mapping,
                pid = current->tgid;
 
        rc = 0;
-       INIT_LIST_HEAD(&tmplist);
+       server = tlink_tcon(open_file->tlink)->ses->server;
 
        cifs_dbg(FYI, "%s: file=%p mapping=%p num_pages=%u\n",
                 __func__, file, mapping, num_pages);
@@ -3355,58 +3493,35 @@ static int cifs_readpages(struct file *file, struct address_space *mapping,
         * the rdata->pages, then we want them in increasing order.
         */
        while (!list_empty(page_list)) {
-               unsigned int i;
-               unsigned int bytes = PAGE_CACHE_SIZE;
-               unsigned int expected_index;
-               unsigned int nr_pages = 1;
+               unsigned int i, nr_pages, bytes, rsize;
                loff_t offset;
                struct page *page, *tpage;
                struct cifs_readdata *rdata;
+               unsigned credits;
 
-               page = list_entry(page_list->prev, struct page, lru);
+               rc = server->ops->wait_mtu_credits(server, cifs_sb->rsize,
+                                                  &rsize, &credits);
+               if (rc)
+                       break;
 
                /*
-                * Lock the page and put it in the cache. Since no one else
-                * should have access to this page, we're safe to simply set
-                * PG_locked without checking it first.
+                * Give up immediately if rsize is too small to read an entire
+                * page. The VFS will fall back to readpage. We should never
+                * reach this point however since we set ra_pages to 0 when the
+                * rsize is smaller than a cache page.
                 */
-               __set_page_locked(page);
-               rc = add_to_page_cache_locked(page, mapping,
-                                             page->index, GFP_KERNEL);
+               if (unlikely(rsize < PAGE_CACHE_SIZE)) {
+                       add_credits_and_wake_if(server, credits, 0);
+                       return 0;
+               }
 
-               /* give up if we can't stick it in the cache */
+               rc = readpages_get_pages(mapping, page_list, rsize, &tmplist,
+                                        &nr_pages, &offset, &bytes);
                if (rc) {
-                       __clear_page_locked(page);
+                       add_credits_and_wake_if(server, credits, 0);
                        break;
                }
 
-               /* move first page to the tmplist */
-               offset = (loff_t)page->index << PAGE_CACHE_SHIFT;
-               list_move_tail(&page->lru, &tmplist);
-
-               /* now try and add more pages onto the request */
-               expected_index = page->index + 1;
-               list_for_each_entry_safe_reverse(page, tpage, page_list, lru) {
-                       /* discontinuity ? */
-                       if (page->index != expected_index)
-                               break;
-
-                       /* would this page push the read over the rsize? */
-                       if (bytes + PAGE_CACHE_SIZE > rsize)
-                               break;
-
-                       __set_page_locked(page);
-                       if (add_to_page_cache_locked(page, mapping,
-                                               page->index, GFP_KERNEL)) {
-                               __clear_page_locked(page);
-                               break;
-                       }
-                       list_move_tail(&page->lru, &tmplist);
-                       bytes += PAGE_CACHE_SIZE;
-                       expected_index++;
-                       nr_pages++;
-               }
-
                rdata = cifs_readdata_alloc(nr_pages, cifs_readv_complete);
                if (!rdata) {
                        /* best to give up if we're out of mem */
@@ -3417,6 +3532,7 @@ static int cifs_readpages(struct file *file, struct address_space *mapping,
                                page_cache_release(page);
                        }
                        rc = -ENOMEM;
+                       add_credits_and_wake_if(server, credits, 0);
                        break;
                }
 
@@ -3427,21 +3543,32 @@ static int cifs_readpages(struct file *file, struct address_space *mapping,
                rdata->pid = pid;
                rdata->pagesz = PAGE_CACHE_SIZE;
                rdata->read_into_pages = cifs_readpages_read_into_pages;
+               rdata->credits = credits;
 
                list_for_each_entry_safe(page, tpage, &tmplist, lru) {
                        list_del(&page->lru);
                        rdata->pages[rdata->nr_pages++] = page;
                }
 
-               rc = cifs_retry_async_readv(rdata);
-               if (rc != 0) {
+               if (!rdata->cfile->invalidHandle ||
+                   !cifs_reopen_file(rdata->cfile, true))
+                       rc = server->ops->async_readv(rdata);
+               if (rc) {
+                       add_credits_and_wake_if(server, rdata->credits, 0);
                        for (i = 0; i < rdata->nr_pages; i++) {
                                page = rdata->pages[i];
                                lru_cache_add_file(page);
                                unlock_page(page);
                                page_cache_release(page);
+                               if (rc == -EAGAIN)
+                                       list_add_tail(&page->lru, &tmplist);
                        }
                        kref_put(&rdata->refcount, cifs_readdata_release);
+                       if (rc == -EAGAIN) {
+                               /* Re-add pages to the page_list and retry */
+                               list_splice(&tmplist, page_list);
+                               continue;
+                       }
                        break;
                }
 
@@ -3685,13 +3812,6 @@ static int cifs_launder_page(struct page *page)
        return rc;
 }
 
-static int
-cifs_pending_writers_wait(void *unused)
-{
-       schedule();
-       return 0;
-}
-
 void cifs_oplock_break(struct work_struct *work)
 {
        struct cifsFileInfo *cfile = container_of(work, struct cifsFileInfo,
@@ -3703,7 +3823,7 @@ void cifs_oplock_break(struct work_struct *work)
        int rc = 0;
 
        wait_on_bit(&cinode->flags, CIFS_INODE_PENDING_WRITERS,
-                       cifs_pending_writers_wait, TASK_UNINTERRUPTIBLE);
+                       TASK_UNINTERRUPTIBLE);
 
        server->ops->downgrade_oplock(server, cinode,
                test_bit(CIFS_INODE_DOWNGRADE_OPLOCK_TO_L2, &cinode->flags));