CIFS: Separate pages initialization from writepages
[deliverable/linux.git] / fs / cifs / file.c
index 6b6df30cfd899bab81de7d0fd6587fc2cd569848..0064d38fdb76b4cf039d6b5014f9bb00bc7ebc53 100644 (file)
@@ -1878,6 +1878,157 @@ static int cifs_partialpagewrite(struct page *page, unsigned from, unsigned to)
        return rc;
 }
 
+static struct cifs_writedata *
+wdata_alloc_and_fillpages(pgoff_t tofind, struct address_space *mapping,
+                         pgoff_t end, pgoff_t *index,
+                         unsigned int *found_pages)
+{
+       unsigned int nr_pages;
+       struct page **pages;
+       struct cifs_writedata *wdata;
+
+       wdata = cifs_writedata_alloc((unsigned int)tofind,
+                                    cifs_writev_complete);
+       if (!wdata)
+               return NULL;
+
+       /*
+        * find_get_pages_tag seems to return a max of 256 on each
+        * iteration, so we must call it several times in order to
+        * fill the array or the wsize is effectively limited to
+        * 256 * PAGE_CACHE_SIZE.
+        */
+       *found_pages = 0;
+       pages = wdata->pages;
+       do {
+               nr_pages = find_get_pages_tag(mapping, index,
+                                             PAGECACHE_TAG_DIRTY, tofind,
+                                             pages);
+               *found_pages += nr_pages;
+               tofind -= nr_pages;
+               pages += nr_pages;
+       } while (nr_pages && tofind && *index <= end);
+
+       return wdata;
+}
+
+static unsigned int
+wdata_prepare_pages(struct cifs_writedata *wdata, unsigned int found_pages,
+                   struct address_space *mapping,
+                   struct writeback_control *wbc,
+                   pgoff_t end, pgoff_t *index, pgoff_t *next, bool *done)
+{
+       unsigned int nr_pages = 0, i;
+       struct page *page;
+
+       for (i = 0; i < found_pages; i++) {
+               page = wdata->pages[i];
+               /*
+                * At this point we hold neither mapping->tree_lock nor
+                * lock on the page itself: the page may be truncated or
+                * invalidated (changing page->mapping to NULL), or even
+                * swizzled back from swapper_space to tmpfs file
+                * mapping
+                */
+
+               if (nr_pages == 0)
+                       lock_page(page);
+               else if (!trylock_page(page))
+                       break;
+
+               if (unlikely(page->mapping != mapping)) {
+                       unlock_page(page);
+                       break;
+               }
+
+               if (!wbc->range_cyclic && page->index > end) {
+                       *done = true;
+                       unlock_page(page);
+                       break;
+               }
+
+               if (*next && (page->index != *next)) {
+                       /* Not next consecutive page */
+                       unlock_page(page);
+                       break;
+               }
+
+               if (wbc->sync_mode != WB_SYNC_NONE)
+                       wait_on_page_writeback(page);
+
+               if (PageWriteback(page) ||
+                               !clear_page_dirty_for_io(page)) {
+                       unlock_page(page);
+                       break;
+               }
+
+               /*
+                * This actually clears the dirty bit in the radix tree.
+                * See cifs_writepage() for more commentary.
+                */
+               set_page_writeback(page);
+               if (page_offset(page) >= i_size_read(mapping->host)) {
+                       *done = true;
+                       unlock_page(page);
+                       end_page_writeback(page);
+                       break;
+               }
+
+               wdata->pages[i] = page;
+               *next = page->index + 1;
+               ++nr_pages;
+       }
+
+       /* reset index to refind any pages skipped */
+       if (nr_pages == 0)
+               *index = wdata->pages[0]->index + 1;
+
+       /* put any pages we aren't going to use */
+       for (i = nr_pages; i < found_pages; i++) {
+               page_cache_release(wdata->pages[i]);
+               wdata->pages[i] = NULL;
+       }
+
+       return nr_pages;
+}
+
+static int
+wdata_send_pages(struct cifs_writedata *wdata, unsigned int nr_pages,
+                struct address_space *mapping, struct writeback_control *wbc)
+{
+       int rc = 0;
+       struct TCP_Server_Info *server;
+       unsigned int i;
+
+       wdata->sync_mode = wbc->sync_mode;
+       wdata->nr_pages = nr_pages;
+       wdata->offset = page_offset(wdata->pages[0]);
+       wdata->pagesz = PAGE_CACHE_SIZE;
+       wdata->tailsz = min(i_size_read(mapping->host) -
+                       page_offset(wdata->pages[nr_pages - 1]),
+                       (loff_t)PAGE_CACHE_SIZE);
+       wdata->bytes = ((nr_pages - 1) * PAGE_CACHE_SIZE) + wdata->tailsz;
+
+       do {
+               if (wdata->cfile != NULL)
+                       cifsFileInfo_put(wdata->cfile);
+               wdata->cfile = find_writable_file(CIFS_I(mapping->host), false);
+               if (!wdata->cfile) {
+                       cifs_dbg(VFS, "No writable handles for inode\n");
+                       rc = -EBADF;
+                       break;
+               }
+               wdata->pid = wdata->cfile->pid;
+               server = tlink_tcon(wdata->cfile->tlink)->ses->server;
+               rc = server->ops->async_writev(wdata, cifs_writedata_release);
+       } while (wbc->sync_mode == WB_SYNC_ALL && rc == -EAGAIN);
+
+       for (i = 0; i < nr_pages; ++i)
+               unlock_page(wdata->pages[i]);
+
+       return rc;
+}
+
 static int cifs_writepages(struct address_space *mapping,
                           struct writeback_control *wbc)
 {
@@ -1885,8 +2036,6 @@ static int cifs_writepages(struct address_space *mapping,
        bool done = false, scanned = false, range_whole = false;
        pgoff_t end, index;
        struct cifs_writedata *wdata;
-       struct TCP_Server_Info *server;
-       struct page *page;
        int rc = 0;
 
        /*
@@ -1910,109 +2059,24 @@ retry:
        while (!done && index <= end) {
                unsigned int i, nr_pages, found_pages;
                pgoff_t next = 0, tofind;
-               struct page **pages;
 
                tofind = min((cifs_sb->wsize / PAGE_CACHE_SIZE) - 1,
                                end - index) + 1;
 
-               wdata = cifs_writedata_alloc((unsigned int)tofind,
-                                            cifs_writev_complete);
+               wdata = wdata_alloc_and_fillpages(tofind, mapping, end, &index,
+                                                 &found_pages);
                if (!wdata) {
                        rc = -ENOMEM;
                        break;
                }
 
-               /*
-                * find_get_pages_tag seems to return a max of 256 on each
-                * iteration, so we must call it several times in order to
-                * fill the array or the wsize is effectively limited to
-                * 256 * PAGE_CACHE_SIZE.
-                */
-               found_pages = 0;
-               pages = wdata->pages;
-               do {
-                       nr_pages = find_get_pages_tag(mapping, &index,
-                                                       PAGECACHE_TAG_DIRTY,
-                                                       tofind, pages);
-                       found_pages += nr_pages;
-                       tofind -= nr_pages;
-                       pages += nr_pages;
-               } while (nr_pages && tofind && index <= end);
-
                if (found_pages == 0) {
                        kref_put(&wdata->refcount, cifs_writedata_release);
                        break;
                }
 
-               nr_pages = 0;
-               for (i = 0; i < found_pages; i++) {
-                       page = wdata->pages[i];
-                       /*
-                        * At this point we hold neither mapping->tree_lock nor
-                        * lock on the page itself: the page may be truncated or
-                        * invalidated (changing page->mapping to NULL), or even
-                        * swizzled back from swapper_space to tmpfs file
-                        * mapping
-                        */
-
-                       if (nr_pages == 0)
-                               lock_page(page);
-                       else if (!trylock_page(page))
-                               break;
-
-                       if (unlikely(page->mapping != mapping)) {
-                               unlock_page(page);
-                               break;
-                       }
-
-                       if (!wbc->range_cyclic && page->index > end) {
-                               done = true;
-                               unlock_page(page);
-                               break;
-                       }
-
-                       if (next && (page->index != next)) {
-                               /* Not next consecutive page */
-                               unlock_page(page);
-                               break;
-                       }
-
-                       if (wbc->sync_mode != WB_SYNC_NONE)
-                               wait_on_page_writeback(page);
-
-                       if (PageWriteback(page) ||
-                                       !clear_page_dirty_for_io(page)) {
-                               unlock_page(page);
-                               break;
-                       }
-
-                       /*
-                        * This actually clears the dirty bit in the radix tree.
-                        * See cifs_writepage() for more commentary.
-                        */
-                       set_page_writeback(page);
-
-                       if (page_offset(page) >= i_size_read(mapping->host)) {
-                               done = true;
-                               unlock_page(page);
-                               end_page_writeback(page);
-                               break;
-                       }
-
-                       wdata->pages[i] = page;
-                       next = page->index + 1;
-                       ++nr_pages;
-               }
-
-               /* reset index to refind any pages skipped */
-               if (nr_pages == 0)
-                       index = wdata->pages[0]->index + 1;
-
-               /* put any pages we aren't going to use */
-               for (i = nr_pages; i < found_pages; i++) {
-                       page_cache_release(wdata->pages[i]);
-                       wdata->pages[i] = NULL;
-               }
+               nr_pages = wdata_prepare_pages(wdata, found_pages, mapping, wbc,
+                                              end, &index, &next, &done);
 
                /* nothing to write? */
                if (nr_pages == 0) {
@@ -2020,35 +2084,7 @@ retry:
                        continue;
                }
 
-               wdata->sync_mode = wbc->sync_mode;
-               wdata->nr_pages = nr_pages;
-               wdata->offset = page_offset(wdata->pages[0]);
-               wdata->pagesz = PAGE_CACHE_SIZE;
-               wdata->tailsz =
-                       min(i_size_read(mapping->host) -
-                           page_offset(wdata->pages[nr_pages - 1]),
-                           (loff_t)PAGE_CACHE_SIZE);
-               wdata->bytes = ((nr_pages - 1) * PAGE_CACHE_SIZE) +
-                                       wdata->tailsz;
-
-               do {
-                       if (wdata->cfile != NULL)
-                               cifsFileInfo_put(wdata->cfile);
-                       wdata->cfile = find_writable_file(CIFS_I(mapping->host),
-                                                         false);
-                       if (!wdata->cfile) {
-                               cifs_dbg(VFS, "No writable handles for inode\n");
-                               rc = -EBADF;
-                               break;
-                       }
-                       wdata->pid = wdata->cfile->pid;
-                       server = tlink_tcon(wdata->cfile->tlink)->ses->server;
-                       rc = server->ops->async_writev(wdata,
-                                                       cifs_writedata_release);
-               } while (wbc->sync_mode == WB_SYNC_ALL && rc == -EAGAIN);
-
-               for (i = 0; i < nr_pages; ++i)
-                       unlock_page(wdata->pages[i]);
+               rc = wdata_send_pages(wdata, nr_pages, mapping, wbc);
 
                /* send failure -- clean up the mess */
                if (rc != 0) {
This page took 0.029058 seconds and 5 git commands to generate.