ext4: bio_alloc with __GFP_DIRECT_RECLAIM never fails
[linux-2.6-microblaze.git] / fs / ext4 / page-io.c
index 12ceade..24aeedb 100644 (file)
 #include "acl.h"
 
 static struct kmem_cache *io_end_cachep;
+static struct kmem_cache *io_end_vec_cachep;
 
 int __init ext4_init_pageio(void)
 {
        io_end_cachep = KMEM_CACHE(ext4_io_end, SLAB_RECLAIM_ACCOUNT);
        if (io_end_cachep == NULL)
                return -ENOMEM;
+
+       io_end_vec_cachep = KMEM_CACHE(ext4_io_end_vec, 0);
+       if (io_end_vec_cachep == NULL) {
+               kmem_cache_destroy(io_end_cachep);
+               return -ENOMEM;
+       }
        return 0;
 }
 
 void ext4_exit_pageio(void)
 {
        kmem_cache_destroy(io_end_cachep);
+       kmem_cache_destroy(io_end_vec_cachep);
+}
+
+struct ext4_io_end_vec *ext4_alloc_io_end_vec(ext4_io_end_t *io_end)
+{
+       struct ext4_io_end_vec *io_end_vec;
+
+       io_end_vec = kmem_cache_zalloc(io_end_vec_cachep, GFP_NOFS);
+       if (!io_end_vec)
+               return ERR_PTR(-ENOMEM);
+       INIT_LIST_HEAD(&io_end_vec->list);
+       list_add_tail(&io_end_vec->list, &io_end->list_vec);
+       return io_end_vec;
+}
+
+static void ext4_free_io_end_vec(ext4_io_end_t *io_end)
+{
+       struct ext4_io_end_vec *io_end_vec, *tmp;
+
+       if (list_empty(&io_end->list_vec))
+               return;
+       list_for_each_entry_safe(io_end_vec, tmp, &io_end->list_vec, list) {
+               list_del(&io_end_vec->list);
+               kmem_cache_free(io_end_vec_cachep, io_end_vec);
+       }
+}
+
+struct ext4_io_end_vec *ext4_last_io_end_vec(ext4_io_end_t *io_end)
+{
+       BUG_ON(list_empty(&io_end->list_vec));
+       return list_last_entry(&io_end->list_vec, struct ext4_io_end_vec, list);
 }
 
 /*
@@ -125,6 +163,7 @@ static void ext4_release_io_end(ext4_io_end_t *io_end)
                ext4_finish_bio(bio);
                bio_put(bio);
        }
+       ext4_free_io_end_vec(io_end);
        kmem_cache_free(io_end_cachep, io_end);
 }
 
@@ -136,29 +175,26 @@ static void ext4_release_io_end(ext4_io_end_t *io_end)
  * cannot get to ext4_ext_truncate() before all IOs overlapping that range are
  * completed (happens from ext4_free_ioend()).
  */
-static int ext4_end_io(ext4_io_end_t *io)
+static int ext4_end_io_end(ext4_io_end_t *io_end)
 {
-       struct inode *inode = io->inode;
-       loff_t offset = io->offset;
-       ssize_t size = io->size;
-       handle_t *handle = io->handle;
+       struct inode *inode = io_end->inode;
+       handle_t *handle = io_end->handle;
        int ret = 0;
 
-       ext4_debug("ext4_end_io_nolock: io 0x%p from inode %lu,list->next 0x%p,"
+       ext4_debug("ext4_end_io_nolock: io_end 0x%p from inode %lu,list->next 0x%p,"
                   "list->prev 0x%p\n",
-                  io, inode->i_ino, io->list.next, io->list.prev);
+                  io_end, inode->i_ino, io_end->list.next, io_end->list.prev);
 
-       io->handle = NULL;      /* Following call will use up the handle */
-       ret = ext4_convert_unwritten_extents(handle, inode, offset, size);
+       io_end->handle = NULL;  /* Following call will use up the handle */
+       ret = ext4_convert_unwritten_io_end_vec(handle, io_end);
        if (ret < 0 && !ext4_forced_shutdown(EXT4_SB(inode->i_sb))) {
                ext4_msg(inode->i_sb, KERN_EMERG,
                         "failed to convert unwritten extents to written "
                         "extents -- potential data loss!  "
-                        "(inode %lu, offset %llu, size %zd, error %d)",
-                        inode->i_ino, offset, size, ret);
+                        "(inode %lu, error %d)", inode->i_ino, ret);
        }
-       ext4_clear_io_unwritten_flag(io);
-       ext4_release_io_end(io);
+       ext4_clear_io_unwritten_flag(io_end);
+       ext4_release_io_end(io_end);
        return ret;
 }
 
@@ -166,21 +202,21 @@ static void dump_completed_IO(struct inode *inode, struct list_head *head)
 {
 #ifdef EXT4FS_DEBUG
        struct list_head *cur, *before, *after;
-       ext4_io_end_t *io, *io0, *io1;
+       ext4_io_end_t *io_end, *io_end0, *io_end1;
 
        if (list_empty(head))
                return;
 
        ext4_debug("Dump inode %lu completed io list\n", inode->i_ino);
-       list_for_each_entry(io, head, list) {
-               cur = &io->list;
+       list_for_each_entry(io_end, head, list) {
+               cur = &io_end->list;
                before = cur->prev;
-               io0 = container_of(before, ext4_io_end_t, list);
+               io_end0 = container_of(before, ext4_io_end_t, list);
                after = cur->next;
-               io1 = container_of(after, ext4_io_end_t, list);
+               io_end1 = container_of(after, ext4_io_end_t, list);
 
                ext4_debug("io 0x%p from inode %lu,prev 0x%p,next 0x%p\n",
-                           io, inode->i_ino, io0, io1);
+                           io_end, inode->i_ino, io_end0, io_end1);
        }
 #endif
 }
@@ -207,7 +243,7 @@ static void ext4_add_complete_io(ext4_io_end_t *io_end)
 static int ext4_do_flush_completed_IO(struct inode *inode,
                                      struct list_head *head)
 {
-       ext4_io_end_t *io;
+       ext4_io_end_t *io_end;
        struct list_head unwritten;
        unsigned long flags;
        struct ext4_inode_info *ei = EXT4_I(inode);
@@ -219,11 +255,11 @@ static int ext4_do_flush_completed_IO(struct inode *inode,
        spin_unlock_irqrestore(&ei->i_completed_io_lock, flags);
 
        while (!list_empty(&unwritten)) {
-               io = list_entry(unwritten.next, ext4_io_end_t, list);
-               BUG_ON(!(io->flag & EXT4_IO_END_UNWRITTEN));
-               list_del_init(&io->list);
+               io_end = list_entry(unwritten.next, ext4_io_end_t, list);
+               BUG_ON(!(io_end->flag & EXT4_IO_END_UNWRITTEN));
+               list_del_init(&io_end->list);
 
-               err = ext4_end_io(io);
+               err = ext4_end_io_end(io_end);
                if (unlikely(!ret && err))
                        ret = err;
        }
@@ -242,19 +278,22 @@ void ext4_end_io_rsv_work(struct work_struct *work)
 
 ext4_io_end_t *ext4_init_io_end(struct inode *inode, gfp_t flags)
 {
-       ext4_io_end_t *io = kmem_cache_zalloc(io_end_cachep, flags);
-       if (io) {
-               io->inode = inode;
-               INIT_LIST_HEAD(&io->list);
-               atomic_set(&io->count, 1);
+       ext4_io_end_t *io_end = kmem_cache_zalloc(io_end_cachep, flags);
+
+       if (io_end) {
+               io_end->inode = inode;
+               INIT_LIST_HEAD(&io_end->list);
+               INIT_LIST_HEAD(&io_end->list_vec);
+               atomic_set(&io_end->count, 1);
        }
-       return io;
+       return io_end;
 }
 
 void ext4_put_io_end_defer(ext4_io_end_t *io_end)
 {
        if (atomic_dec_and_test(&io_end->count)) {
-               if (!(io_end->flag & EXT4_IO_END_UNWRITTEN) || !io_end->size) {
+               if (!(io_end->flag & EXT4_IO_END_UNWRITTEN) ||
+                               list_empty(&io_end->list_vec)) {
                        ext4_release_io_end(io_end);
                        return;
                }
@@ -268,9 +307,8 @@ int ext4_put_io_end(ext4_io_end_t *io_end)
 
        if (atomic_dec_and_test(&io_end->count)) {
                if (io_end->flag & EXT4_IO_END_UNWRITTEN) {
-                       err = ext4_convert_unwritten_extents(io_end->handle,
-                                               io_end->inode, io_end->offset,
-                                               io_end->size);
+                       err = ext4_convert_unwritten_io_end_vec(io_end->handle,
+                                                               io_end);
                        io_end->handle = NULL;
                        ext4_clear_io_unwritten_flag(io_end);
                }
@@ -307,10 +345,8 @@ static void ext4_end_bio(struct bio *bio)
                struct inode *inode = io_end->inode;
 
                ext4_warning(inode->i_sb, "I/O error %d writing to inode %lu "
-                            "(offset %llu size %ld starting block %llu)",
+                            "starting block %llu)",
                             bio->bi_status, inode->i_ino,
-                            (unsigned long long) io_end->offset,
-                            (long) io_end->size,
                             (unsigned long long)
                             bi_sector >> (inode->i_blkbits - 9));
                mapping_set_error(inode->i_mapping,
@@ -358,14 +394,16 @@ void ext4_io_submit_init(struct ext4_io_submit *io,
        io->io_end = NULL;
 }
 
-static int io_submit_init_bio(struct ext4_io_submit *io,
-                             struct buffer_head *bh)
+static void io_submit_init_bio(struct ext4_io_submit *io,
+                              struct buffer_head *bh)
 {
        struct bio *bio;
 
+       /*
+        * bio_alloc will _always_ be able to allocate a bio if
+        * __GFP_DIRECT_RECLAIM is set, see comments for bio_alloc_bioset().
+        */
        bio = bio_alloc(GFP_NOIO, BIO_MAX_PAGES);
-       if (!bio)
-               return -ENOMEM;
        bio->bi_iter.bi_sector = bh->b_blocknr * (bh->b_size >> 9);
        bio_set_dev(bio, bh->b_bdev);
        bio->bi_end_io = ext4_end_bio;
@@ -373,13 +411,12 @@ static int io_submit_init_bio(struct ext4_io_submit *io,
        io->io_bio = bio;
        io->io_next_block = bh->b_blocknr;
        wbc_init_bio(io->io_wbc, bio);
-       return 0;
 }
 
-static int io_submit_add_bh(struct ext4_io_submit *io,
-                           struct inode *inode,
-                           struct page *page,
-                           struct buffer_head *bh)
+static void io_submit_add_bh(struct ext4_io_submit *io,
+                            struct inode *inode,
+                            struct page *page,
+                            struct buffer_head *bh)
 {
        int ret;
 
@@ -388,9 +425,7 @@ submit_and_retry:
                ext4_io_submit(io);
        }
        if (io->io_bio == NULL) {
-               ret = io_submit_init_bio(io, bh);
-               if (ret)
-                       return ret;
+               io_submit_init_bio(io, bh);
                io->io_bio->bi_write_hint = inode->i_write_hint;
        }
        ret = bio_add_page(io->io_bio, page, bh->b_size, bh_offset(bh));
@@ -398,7 +433,6 @@ submit_and_retry:
                goto submit_and_retry;
        wbc_account_cgroup_owner(io->io_wbc, page, bh->b_size);
        io->io_next_block++;
-       return 0;
 }
 
 int ext4_bio_write_page(struct ext4_io_submit *io,
@@ -491,8 +525,14 @@ int ext4_bio_write_page(struct ext4_io_submit *io,
                                gfp_flags |= __GFP_NOFAIL;
                                goto retry_encrypt;
                        }
-                       bounce_page = NULL;
-                       goto out;
+
+                       printk_ratelimited(KERN_ERR "%s: ret = %d\n", __func__, ret);
+                       redirty_page_for_writepage(wbc, page);
+                       do {
+                               clear_buffer_async_write(bh);
+                               bh = bh->b_this_page;
+                       } while (bh != head);
+                       goto unlock;
                }
        }
 
@@ -500,30 +540,13 @@ int ext4_bio_write_page(struct ext4_io_submit *io,
        do {
                if (!buffer_async_write(bh))
                        continue;
-               ret = io_submit_add_bh(io, inode, bounce_page ?: page, bh);
-               if (ret) {
-                       /*
-                        * We only get here on ENOMEM.  Not much else
-                        * we can do but mark the page as dirty, and
-                        * better luck next time.
-                        */
-                       break;
-               }
+               io_submit_add_bh(io, inode,
+                                bounce_page ? bounce_page : page, bh);
                nr_submitted++;
                clear_buffer_dirty(bh);
        } while ((bh = bh->b_this_page) != head);
 
-       /* Error stopped previous loop? Clean up buffers... */
-       if (ret) {
-       out:
-               fscrypt_free_bounce_page(bounce_page);
-               printk_ratelimited(KERN_ERR "%s: ret = %d\n", __func__, ret);
-               redirty_page_for_writepage(wbc, page);
-               do {
-                       clear_buffer_async_write(bh);
-                       bh = bh->b_this_page;
-               } while (bh != head);
-       }
+unlock:
        unlock_page(page);
        /* Nothing submitted - we have to end page writeback */
        if (!nr_submitted)