}
 EXPORT_SYMBOL(tag_pages_for_writeback);
 
+static bool folio_prepare_writeback(struct address_space *mapping,
+               struct writeback_control *wbc, struct folio *folio)
+{
+       /*
+        * Folio truncated or invalidated. We can freely skip it then,
+        * even for data integrity operations: the folio has disappeared
+        * concurrently, so there could be no real expectation of this
+        * data integrity operation even if there is now a new, dirty
+        * folio at the same pagecache index.
+        */
+       if (unlikely(folio->mapping != mapping))
+               return false;
+
+       /*
+        * Did somebody else write it for us?
+        */
+       if (!folio_test_dirty(folio))
+               return false;
+
+       if (folio_test_writeback(folio)) {
+               if (wbc->sync_mode == WB_SYNC_NONE)
+                       return false;
+               folio_wait_writeback(folio);
+       }
+       BUG_ON(folio_test_writeback(folio));
+
+       if (!folio_clear_dirty_for_io(folio))
+               return false;
+
+       return true;
+}
+
 /**
  * write_cache_pages - walk the list of dirty pages of the given address space and write all of them.
  * @mapping: address space structure to write
                for (i = 0; i < nr_folios; i++) {
                        folio = fbatch.folios[i];
                        folio_lock(folio);
-
-                       /*
-                        * Page truncated or invalidated. We can freely skip it
-                        * then, even for data integrity operations: the page
-                        * has disappeared concurrently, so there could be no
-                        * real expectation of this data integrity operation
-                        * even if there is now a new, dirty page at the same
-                        * pagecache address.
-                        */
-                       if (unlikely(folio->mapping != mapping)) {
-continue_unlock:
+                       if (!folio_prepare_writeback(mapping, wbc, folio)) {
                                folio_unlock(folio);
                                continue;
                        }
 
-                       if (!folio_test_dirty(folio)) {
-                               /* someone wrote it for us */
-                               goto continue_unlock;
-                       }
-
-                       if (folio_test_writeback(folio)) {
-                               if (wbc->sync_mode != WB_SYNC_NONE)
-                                       folio_wait_writeback(folio);
-                               else
-                                       goto continue_unlock;
-                       }
-
-                       BUG_ON(folio_test_writeback(folio));
-                       if (!folio_clear_dirty_for_io(folio))
-                               goto continue_unlock;
-
                        trace_wbc_writepage(wbc, inode_to_bdi(mapping->host));
+
                        error = writepage(folio, wbc, data);
                        wbc->nr_to_write -= folio_nr_pages(folio);