aboutsummaryrefslogtreecommitdiffstats
path: root/fs
diff options
context:
space:
mode:
authorTheodore Ts'o <tytso@mit.edu>2010-10-27 21:30:10 -0400
committerTheodore Ts'o <tytso@mit.edu>2010-10-27 21:30:10 -0400
commitbd2d0210cf22f2bd0cef72eb97cf94fc7d31d8cc (patch)
treef0d1902b7ff4294114614cc706855c3d6b131f73 /fs
parent1de3e3df917459422cb2aecac440febc8879d410 (diff)
ext4: use bio layer instead of buffer layer in mpage_da_submit_io
Call the block I/O layer directly instad of going through the buffer layer. This should give us much better performance and scalability, as well as lowering our CPU utilization when doing buffered writeback. Signed-off-by: "Theodore Ts'o" <tytso@mit.edu>
Diffstat (limited to 'fs')
-rw-r--r--fs/ext4/Makefile2
-rw-r--r--fs/ext4/ext4.h36
-rw-r--r--fs/ext4/extents.c4
-rw-r--r--fs/ext4/inode.c118
-rw-r--r--fs/ext4/page-io.c430
-rw-r--r--fs/ext4/super.c8
6 files changed, 489 insertions, 109 deletions
diff --git a/fs/ext4/Makefile b/fs/ext4/Makefile
index 8867b2a1e5fe..c947e36eda6c 100644
--- a/fs/ext4/Makefile
+++ b/fs/ext4/Makefile
@@ -4,7 +4,7 @@
4 4
5obj-$(CONFIG_EXT4_FS) += ext4.o 5obj-$(CONFIG_EXT4_FS) += ext4.o
6 6
7ext4-y := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o \ 7ext4-y := balloc.o bitmap.o dir.o file.o fsync.o ialloc.o inode.o page-io.o \
8 ioctl.o namei.o super.o symlink.o hash.o resize.o extents.o \ 8 ioctl.o namei.o super.o symlink.o hash.o resize.o extents.o \
9 ext4_jbd2.o migrate.o mballoc.o block_validity.o move_extent.o 9 ext4_jbd2.o migrate.o mballoc.o block_validity.o move_extent.o
10 10
diff --git a/fs/ext4/ext4.h b/fs/ext4/ext4.h
index 22833691e98c..ca9fda64dd4f 100644
--- a/fs/ext4/ext4.h
+++ b/fs/ext4/ext4.h
@@ -168,7 +168,20 @@ struct mpage_da_data {
168 int pages_written; 168 int pages_written;
169 int retval; 169 int retval;
170}; 170};
171#define EXT4_IO_UNWRITTEN 0x1 171
172/*
173 * Flags for ext4_io_end->flags
174 */
175#define EXT4_IO_END_UNWRITTEN 0x0001
176#define EXT4_IO_END_ERROR 0x0002
177
178struct ext4_io_page {
179 struct page *p_page;
180 int p_count;
181};
182
183#define MAX_IO_PAGES 128
184
172typedef struct ext4_io_end { 185typedef struct ext4_io_end {
173 struct list_head list; /* per-file finished IO list */ 186 struct list_head list; /* per-file finished IO list */
174 struct inode *inode; /* file being written to */ 187 struct inode *inode; /* file being written to */
@@ -179,8 +192,18 @@ typedef struct ext4_io_end {
179 struct work_struct work; /* data work queue */ 192 struct work_struct work; /* data work queue */
180 struct kiocb *iocb; /* iocb struct for AIO */ 193 struct kiocb *iocb; /* iocb struct for AIO */
181 int result; /* error value for AIO */ 194 int result; /* error value for AIO */
195 int num_io_pages;
196 struct ext4_io_page *pages[MAX_IO_PAGES];
182} ext4_io_end_t; 197} ext4_io_end_t;
183 198
199struct ext4_io_submit {
200 int io_op;
201 struct bio *io_bio;
202 ext4_io_end_t *io_end;
203 struct ext4_io_page *io_page;
204 sector_t io_next_block;
205};
206
184/* 207/*
185 * Special inodes numbers 208 * Special inodes numbers
186 */ 209 */
@@ -2044,6 +2067,17 @@ extern int ext4_move_extents(struct file *o_filp, struct file *d_filp,
2044 __u64 start_orig, __u64 start_donor, 2067 __u64 start_orig, __u64 start_donor,
2045 __u64 len, __u64 *moved_len); 2068 __u64 len, __u64 *moved_len);
2046 2069
2070/* page-io.c */
2071extern int __init init_ext4_pageio(void);
2072extern void exit_ext4_pageio(void);
2073extern void ext4_free_io_end(ext4_io_end_t *io);
2074extern ext4_io_end_t *ext4_init_io_end(struct inode *inode, gfp_t flags);
2075extern int ext4_end_io_nolock(ext4_io_end_t *io);
2076extern void ext4_io_submit(struct ext4_io_submit *io);
2077extern int ext4_bio_write_page(struct ext4_io_submit *io,
2078 struct page *page,
2079 int len,
2080 struct writeback_control *wbc);
2047 2081
2048/* BH_Uninit flag: blocks are allocated but uninitialized on disk */ 2082/* BH_Uninit flag: blocks are allocated but uninitialized on disk */
2049enum ext4_state_bits { 2083enum ext4_state_bits {
diff --git a/fs/ext4/extents.c b/fs/ext4/extents.c
index a0e623055955..a1e20c8c4e0c 100644
--- a/fs/ext4/extents.c
+++ b/fs/ext4/extents.c
@@ -3202,7 +3202,7 @@ ext4_ext_handle_uninitialized_extents(handle_t *handle, struct inode *inode,
3202 * completed 3202 * completed
3203 */ 3203 */
3204 if (io) 3204 if (io)
3205 io->flag = EXT4_IO_UNWRITTEN; 3205 io->flag = EXT4_IO_END_UNWRITTEN;
3206 else 3206 else
3207 ext4_set_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN); 3207 ext4_set_inode_state(inode, EXT4_STATE_DIO_UNWRITTEN);
3208 if (ext4_should_dioread_nolock(inode)) 3208 if (ext4_should_dioread_nolock(inode))
@@ -3494,7 +3494,7 @@ int ext4_ext_map_blocks(handle_t *handle, struct inode *inode,
3494 */ 3494 */
3495 if ((flags & EXT4_GET_BLOCKS_PRE_IO)) { 3495 if ((flags & EXT4_GET_BLOCKS_PRE_IO)) {
3496 if (io) 3496 if (io)
3497 io->flag = EXT4_IO_UNWRITTEN; 3497 io->flag = EXT4_IO_END_UNWRITTEN;
3498 else 3498 else
3499 ext4_set_inode_state(inode, 3499 ext4_set_inode_state(inode,
3500 EXT4_STATE_DIO_UNWRITTEN); 3500 EXT4_STATE_DIO_UNWRITTEN);
diff --git a/fs/ext4/inode.c b/fs/ext4/inode.c
index c65d647378f9..58604fe11f4f 100644
--- a/fs/ext4/inode.c
+++ b/fs/ext4/inode.c
@@ -2016,8 +2016,10 @@ static int mpage_da_submit_io(struct mpage_da_data *mpd,
2016 struct buffer_head *bh, *page_bufs = NULL; 2016 struct buffer_head *bh, *page_bufs = NULL;
2017 int journal_data = ext4_should_journal_data(inode); 2017 int journal_data = ext4_should_journal_data(inode);
2018 sector_t pblock = 0, cur_logical = 0; 2018 sector_t pblock = 0, cur_logical = 0;
2019 struct ext4_io_submit io_submit;
2019 2020
2020 BUG_ON(mpd->next_page <= mpd->first_page); 2021 BUG_ON(mpd->next_page <= mpd->first_page);
2022 memset(&io_submit, 0, sizeof(io_submit));
2021 /* 2023 /*
2022 * We need to start from the first_page to the next_page - 1 2024 * We need to start from the first_page to the next_page - 1
2023 * to make sure we also write the mapped dirty buffer_heads. 2025 * to make sure we also write the mapped dirty buffer_heads.
@@ -2109,16 +2111,16 @@ static int mpage_da_submit_io(struct mpage_da_data *mpd,
2109 /* mark the buffer_heads as dirty & uptodate */ 2111 /* mark the buffer_heads as dirty & uptodate */
2110 block_commit_write(page, 0, len); 2112 block_commit_write(page, 0, len);
2111 2113
2112 if (journal_data && PageChecked(page)) 2114 /*
2115 * Delalloc doesn't support data journalling,
2116 * but eventually maybe we'll lift this
2117 * restriction.
2118 */
2119 if (unlikely(journal_data && PageChecked(page)))
2113 err = __ext4_journalled_writepage(page, len); 2120 err = __ext4_journalled_writepage(page, len);
2114 else if (buffer_uninit(page_bufs)) { 2121 else
2115 ext4_set_bh_endio(page_bufs, inode); 2122 err = ext4_bio_write_page(&io_submit, page,
2116 err = block_write_full_page_endio(page, 2123 len, mpd->wbc);
2117 noalloc_get_block_write,
2118 mpd->wbc, ext4_end_io_buffer_write);
2119 } else
2120 err = block_write_full_page(page,
2121 noalloc_get_block_write, mpd->wbc);
2122 2124
2123 if (!err) 2125 if (!err)
2124 mpd->pages_written++; 2126 mpd->pages_written++;
@@ -2131,6 +2133,7 @@ static int mpage_da_submit_io(struct mpage_da_data *mpd,
2131 } 2133 }
2132 pagevec_release(&pvec); 2134 pagevec_release(&pvec);
2133 } 2135 }
2136 ext4_io_submit(&io_submit);
2134 return ret; 2137 return ret;
2135} 2138}
2136 2139
@@ -3426,15 +3429,6 @@ ext4_readpages(struct file *file, struct address_space *mapping,
3426 return mpage_readpages(mapping, pages, nr_pages, ext4_get_block); 3429 return mpage_readpages(mapping, pages, nr_pages, ext4_get_block);
3427} 3430}
3428 3431
3429static void ext4_free_io_end(ext4_io_end_t *io)
3430{
3431 BUG_ON(!io);
3432 if (io->page)
3433 put_page(io->page);
3434 iput(io->inode);
3435 kfree(io);
3436}
3437
3438static void ext4_invalidatepage_free_endio(struct page *page, unsigned long offset) 3432static void ext4_invalidatepage_free_endio(struct page *page, unsigned long offset)
3439{ 3433{
3440 struct buffer_head *head, *bh; 3434 struct buffer_head *head, *bh;
@@ -3640,68 +3634,6 @@ static void dump_completed_IO(struct inode * inode)
3640} 3634}
3641 3635
3642/* 3636/*
3643 * check a range of space and convert unwritten extents to written.
3644 */
3645static int ext4_end_io_nolock(ext4_io_end_t *io)
3646{
3647 struct inode *inode = io->inode;
3648 loff_t offset = io->offset;
3649 ssize_t size = io->size;
3650 int ret = 0;
3651
3652 ext4_debug("ext4_end_io_nolock: io 0x%p from inode %lu,list->next 0x%p,"
3653 "list->prev 0x%p\n",
3654 io, inode->i_ino, io->list.next, io->list.prev);
3655
3656 if (list_empty(&io->list))
3657 return ret;
3658
3659 if (io->flag != EXT4_IO_UNWRITTEN)
3660 return ret;
3661
3662 ret = ext4_convert_unwritten_extents(inode, offset, size);
3663 if (ret < 0) {
3664 printk(KERN_EMERG "%s: failed to convert unwritten"
3665 "extents to written extents, error is %d"
3666 " io is still on inode %lu aio dio list\n",
3667 __func__, ret, inode->i_ino);
3668 return ret;
3669 }
3670
3671 if (io->iocb)
3672 aio_complete(io->iocb, io->result, 0);
3673 /* clear the DIO AIO unwritten flag */
3674 io->flag = 0;
3675 return ret;
3676}
3677
3678/*
3679 * work on completed aio dio IO, to convert unwritten extents to extents
3680 */
3681static void ext4_end_io_work(struct work_struct *work)
3682{
3683 ext4_io_end_t *io = container_of(work, ext4_io_end_t, work);
3684 struct inode *inode = io->inode;
3685 struct ext4_inode_info *ei = EXT4_I(inode);
3686 unsigned long flags;
3687 int ret;
3688
3689 mutex_lock(&inode->i_mutex);
3690 ret = ext4_end_io_nolock(io);
3691 if (ret < 0) {
3692 mutex_unlock(&inode->i_mutex);
3693 return;
3694 }
3695
3696 spin_lock_irqsave(&ei->i_completed_io_lock, flags);
3697 if (!list_empty(&io->list))
3698 list_del_init(&io->list);
3699 spin_unlock_irqrestore(&ei->i_completed_io_lock, flags);
3700 mutex_unlock(&inode->i_mutex);
3701 ext4_free_io_end(io);
3702}
3703
3704/*
3705 * This function is called from ext4_sync_file(). 3637 * This function is called from ext4_sync_file().
3706 * 3638 *
3707 * When IO is completed, the work to convert unwritten extents to 3639 * When IO is completed, the work to convert unwritten extents to
@@ -3756,28 +3688,6 @@ int flush_completed_IO(struct inode *inode)
3756 return (ret2 < 0) ? ret2 : 0; 3688 return (ret2 < 0) ? ret2 : 0;
3757} 3689}
3758 3690
3759static ext4_io_end_t *ext4_init_io_end (struct inode *inode, gfp_t flags)
3760{
3761 ext4_io_end_t *io = NULL;
3762
3763 io = kmalloc(sizeof(*io), flags);
3764
3765 if (io) {
3766 igrab(inode);
3767 io->inode = inode;
3768 io->flag = 0;
3769 io->offset = 0;
3770 io->size = 0;
3771 io->page = NULL;
3772 io->iocb = NULL;
3773 io->result = 0;
3774 INIT_WORK(&io->work, ext4_end_io_work);
3775 INIT_LIST_HEAD(&io->list);
3776 }
3777
3778 return io;
3779}
3780
3781static void ext4_end_io_dio(struct kiocb *iocb, loff_t offset, 3691static void ext4_end_io_dio(struct kiocb *iocb, loff_t offset,
3782 ssize_t size, void *private, int ret, 3692 ssize_t size, void *private, int ret,
3783 bool is_async) 3693 bool is_async)
@@ -3797,7 +3707,7 @@ static void ext4_end_io_dio(struct kiocb *iocb, loff_t offset,
3797 size); 3707 size);
3798 3708
3799 /* if not aio dio with unwritten extents, just free io and return */ 3709 /* if not aio dio with unwritten extents, just free io and return */
3800 if (io_end->flag != EXT4_IO_UNWRITTEN){ 3710 if (!(io_end->flag & EXT4_IO_END_UNWRITTEN)) {
3801 ext4_free_io_end(io_end); 3711 ext4_free_io_end(io_end);
3802 iocb->private = NULL; 3712 iocb->private = NULL;
3803out: 3713out:
@@ -3842,7 +3752,7 @@ static void ext4_end_io_buffer_write(struct buffer_head *bh, int uptodate)
3842 goto out; 3752 goto out;
3843 } 3753 }
3844 3754
3845 io_end->flag = EXT4_IO_UNWRITTEN; 3755 io_end->flag = EXT4_IO_END_UNWRITTEN;
3846 inode = io_end->inode; 3756 inode = io_end->inode;
3847 3757
3848 /* Add the io_end to per-inode completed io list*/ 3758 /* Add the io_end to per-inode completed io list*/
diff --git a/fs/ext4/page-io.c b/fs/ext4/page-io.c
new file mode 100644
index 000000000000..b972ca50f851
--- /dev/null
+++ b/fs/ext4/page-io.c
@@ -0,0 +1,430 @@
1/*
2 * linux/fs/ext4/page-io.c
3 *
4 * This contains the new page_io functions for ext4
5 *
6 * Written by Theodore Ts'o, 2010.
7 */
8
9#include <linux/module.h>
10#include <linux/fs.h>
11#include <linux/time.h>
12#include <linux/jbd2.h>
13#include <linux/highuid.h>
14#include <linux/pagemap.h>
15#include <linux/quotaops.h>
16#include <linux/string.h>
17#include <linux/buffer_head.h>
18#include <linux/writeback.h>
19#include <linux/pagevec.h>
20#include <linux/mpage.h>
21#include <linux/namei.h>
22#include <linux/uio.h>
23#include <linux/bio.h>
24#include <linux/workqueue.h>
25#include <linux/kernel.h>
26#include <linux/slab.h>
27
28#include "ext4_jbd2.h"
29#include "xattr.h"
30#include "acl.h"
31#include "ext4_extents.h"
32
33static struct kmem_cache *io_page_cachep, *io_end_cachep;
34
35int __init init_ext4_pageio(void)
36{
37 io_page_cachep = KMEM_CACHE(ext4_io_page, SLAB_RECLAIM_ACCOUNT);
38 if (io_page_cachep == NULL)
39 return -ENOMEM;
40 io_end_cachep = KMEM_CACHE(ext4_io_end, SLAB_RECLAIM_ACCOUNT);
41 if (io_page_cachep == NULL) {
42 kmem_cache_destroy(io_page_cachep);
43 return -ENOMEM;
44 }
45
46 return 0;
47}
48
49void exit_ext4_pageio(void)
50{
51 kmem_cache_destroy(io_end_cachep);
52 kmem_cache_destroy(io_page_cachep);
53}
54
55void ext4_free_io_end(ext4_io_end_t *io)
56{
57 int i;
58
59 BUG_ON(!io);
60 if (io->page)
61 put_page(io->page);
62 for (i = 0; i < io->num_io_pages; i++) {
63 if (--io->pages[i]->p_count == 0) {
64 struct page *page = io->pages[i]->p_page;
65
66 end_page_writeback(page);
67 put_page(page);
68 kmem_cache_free(io_page_cachep, io->pages[i]);
69 }
70 }
71 io->num_io_pages = 0;
72 iput(io->inode);
73 kmem_cache_free(io_end_cachep, io);
74}
75
76/*
77 * check a range of space and convert unwritten extents to written.
78 */
79int ext4_end_io_nolock(ext4_io_end_t *io)
80{
81 struct inode *inode = io->inode;
82 loff_t offset = io->offset;
83 ssize_t size = io->size;
84 int ret = 0;
85
86 ext4_debug("ext4_end_io_nolock: io 0x%p from inode %lu,list->next 0x%p,"
87 "list->prev 0x%p\n",
88 io, inode->i_ino, io->list.next, io->list.prev);
89
90 if (list_empty(&io->list))
91 return ret;
92
93 if (!(io->flag & EXT4_IO_END_UNWRITTEN))
94 return ret;
95
96 ret = ext4_convert_unwritten_extents(inode, offset, size);
97 if (ret < 0) {
98 printk(KERN_EMERG "%s: failed to convert unwritten "
99 "extents to written extents, error is %d "
100 "io is still on inode %lu aio dio list\n",
101 __func__, ret, inode->i_ino);
102 return ret;
103 }
104
105 if (io->iocb)
106 aio_complete(io->iocb, io->result, 0);
107 /* clear the DIO AIO unwritten flag */
108 io->flag &= ~EXT4_IO_END_UNWRITTEN;
109 return ret;
110}
111
112/*
113 * work on completed aio dio IO, to convert unwritten extents to extents
114 */
115static void ext4_end_io_work(struct work_struct *work)
116{
117 ext4_io_end_t *io = container_of(work, ext4_io_end_t, work);
118 struct inode *inode = io->inode;
119 struct ext4_inode_info *ei = EXT4_I(inode);
120 unsigned long flags;
121 int ret;
122
123 mutex_lock(&inode->i_mutex);
124 ret = ext4_end_io_nolock(io);
125 if (ret < 0) {
126 mutex_unlock(&inode->i_mutex);
127 return;
128 }
129
130 spin_lock_irqsave(&ei->i_completed_io_lock, flags);
131 if (!list_empty(&io->list))
132 list_del_init(&io->list);
133 spin_unlock_irqrestore(&ei->i_completed_io_lock, flags);
134 mutex_unlock(&inode->i_mutex);
135 ext4_free_io_end(io);
136}
137
138ext4_io_end_t *ext4_init_io_end(struct inode *inode, gfp_t flags)
139{
140 ext4_io_end_t *io = NULL;
141
142 io = kmem_cache_alloc(io_end_cachep, flags);
143 if (io) {
144 memset(io, 0, sizeof(*io));
145 io->inode = igrab(inode);
146 BUG_ON(!io->inode);
147 INIT_WORK(&io->work, ext4_end_io_work);
148 INIT_LIST_HEAD(&io->list);
149 }
150 return io;
151}
152
153/*
154 * Print an buffer I/O error compatible with the fs/buffer.c. This
155 * provides compatibility with dmesg scrapers that look for a specific
156 * buffer I/O error message. We really need a unified error reporting
157 * structure to userspace ala Digital Unix's uerf system, but it's
158 * probably not going to happen in my lifetime, due to LKML politics...
159 */
160static void buffer_io_error(struct buffer_head *bh)
161{
162 char b[BDEVNAME_SIZE];
163 printk(KERN_ERR "Buffer I/O error on device %s, logical block %llu\n",
164 bdevname(bh->b_bdev, b),
165 (unsigned long long)bh->b_blocknr);
166}
167
168static void ext4_end_bio(struct bio *bio, int error)
169{
170 ext4_io_end_t *io_end = bio->bi_private;
171 struct workqueue_struct *wq;
172 struct inode *inode;
173 unsigned long flags;
174 ext4_fsblk_t err_block;
175 int i;
176
177 BUG_ON(!io_end);
178 inode = io_end->inode;
179 bio->bi_private = NULL;
180 bio->bi_end_io = NULL;
181 if (test_bit(BIO_UPTODATE, &bio->bi_flags))
182 error = 0;
183 err_block = bio->bi_sector >> (inode->i_blkbits - 9);
184 bio_put(bio);
185
186 if (!(inode->i_sb->s_flags & MS_ACTIVE)) {
187 pr_err("sb umounted, discard end_io request for inode %lu\n",
188 io_end->inode->i_ino);
189 ext4_free_io_end(io_end);
190 return;
191 }
192
193 if (error) {
194 io_end->flag |= EXT4_IO_END_ERROR;
195 ext4_warning(inode->i_sb, "I/O error writing to inode %lu "
196 "(offset %llu size %ld starting block %llu)",
197 inode->i_ino,
198 (unsigned long long) io_end->offset,
199 (long) io_end->size,
200 (unsigned long long) err_block);
201 }
202
203 for (i = 0; i < io_end->num_io_pages; i++) {
204 struct page *page = io_end->pages[i]->p_page;
205 struct buffer_head *bh, *head;
206 int partial_write = 0;
207
208 head = page_buffers(page);
209 if (error)
210 SetPageError(page);
211 BUG_ON(!head);
212 if (head->b_size == PAGE_CACHE_SIZE)
213 clear_buffer_dirty(head);
214 else {
215 loff_t offset;
216 loff_t io_end_offset = io_end->offset + io_end->size;
217
218 offset = (sector_t) page->index << PAGE_CACHE_SHIFT;
219 bh = head;
220 do {
221 if ((offset >= io_end->offset) &&
222 (offset+bh->b_size <= io_end_offset)) {
223 if (error)
224 buffer_io_error(bh);
225
226 clear_buffer_dirty(bh);
227 }
228 if (buffer_delay(bh))
229 partial_write = 1;
230 else if (!buffer_mapped(bh))
231 clear_buffer_dirty(bh);
232 else if (buffer_dirty(bh))
233 partial_write = 1;
234 offset += bh->b_size;
235 bh = bh->b_this_page;
236 } while (bh != head);
237 }
238
239 if (--io_end->pages[i]->p_count == 0) {
240 struct page *page = io_end->pages[i]->p_page;
241
242 end_page_writeback(page);
243 put_page(page);
244 kmem_cache_free(io_page_cachep, io_end->pages[i]);
245 }
246
247 /*
248 * If this is a partial write which happened to make
249 * all buffers uptodate then we can optimize away a
250 * bogus readpage() for the next read(). Here we
251 * 'discover' whether the page went uptodate as a
252 * result of this (potentially partial) write.
253 */
254 if (!partial_write)
255 SetPageUptodate(page);
256 }
257
258 io_end->num_io_pages = 0;
259
260 /* Add the io_end to per-inode completed io list*/
261 spin_lock_irqsave(&EXT4_I(inode)->i_completed_io_lock, flags);
262 list_add_tail(&io_end->list, &EXT4_I(inode)->i_completed_io_list);
263 spin_unlock_irqrestore(&EXT4_I(inode)->i_completed_io_lock, flags);
264
265 wq = EXT4_SB(inode->i_sb)->dio_unwritten_wq;
266 /* queue the work to convert unwritten extents to written */
267 queue_work(wq, &io_end->work);
268}
269
270void ext4_io_submit(struct ext4_io_submit *io)
271{
272 struct bio *bio = io->io_bio;
273
274 if (bio) {
275 bio_get(io->io_bio);
276 submit_bio(io->io_op, io->io_bio);
277 BUG_ON(bio_flagged(io->io_bio, BIO_EOPNOTSUPP));
278 bio_put(io->io_bio);
279 }
280 io->io_bio = 0;
281 io->io_op = 0;
282 io->io_end = 0;
283}
284
285static int io_submit_init(struct ext4_io_submit *io,
286 struct inode *inode,
287 struct writeback_control *wbc,
288 struct buffer_head *bh)
289{
290 ext4_io_end_t *io_end;
291 struct page *page = bh->b_page;
292 int nvecs = bio_get_nr_vecs(bh->b_bdev);
293 struct bio *bio;
294
295 io_end = ext4_init_io_end(inode, GFP_NOFS);
296 if (!io_end)
297 return -ENOMEM;
298 do {
299 bio = bio_alloc(GFP_NOIO, nvecs);
300 nvecs >>= 1;
301 } while (bio == NULL);
302
303 bio->bi_sector = bh->b_blocknr * (bh->b_size >> 9);
304 bio->bi_bdev = bh->b_bdev;
305 bio->bi_private = io->io_end = io_end;
306 bio->bi_end_io = ext4_end_bio;
307
308 io_end->inode = inode;
309 io_end->offset = (page->index << PAGE_CACHE_SHIFT) + bh_offset(bh);
310
311 io->io_bio = bio;
312 io->io_op = (wbc->sync_mode == WB_SYNC_ALL ?
313 WRITE_SYNC_PLUG : WRITE);
314 io->io_next_block = bh->b_blocknr;
315 return 0;
316}
317
318static int io_submit_add_bh(struct ext4_io_submit *io,
319 struct ext4_io_page *io_page,
320 struct inode *inode,
321 struct writeback_control *wbc,
322 struct buffer_head *bh)
323{
324 ext4_io_end_t *io_end;
325 int ret;
326
327 if (buffer_new(bh)) {
328 clear_buffer_new(bh);
329 unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
330 }
331
332 if (!buffer_mapped(bh) || buffer_delay(bh)) {
333 if (!buffer_mapped(bh))
334 clear_buffer_dirty(bh);
335 if (io->io_bio)
336 ext4_io_submit(io);
337 return 0;
338 }
339
340 if (io->io_bio && bh->b_blocknr != io->io_next_block) {
341submit_and_retry:
342 ext4_io_submit(io);
343 }
344 if (io->io_bio == NULL) {
345 ret = io_submit_init(io, inode, wbc, bh);
346 if (ret)
347 return ret;
348 }
349 io_end = io->io_end;
350 if ((io_end->num_io_pages >= MAX_IO_PAGES) &&
351 (io_end->pages[io_end->num_io_pages-1] != io_page))
352 goto submit_and_retry;
353 if (buffer_uninit(bh))
354 io->io_end->flag |= EXT4_IO_END_UNWRITTEN;
355 io->io_end->size += bh->b_size;
356 io->io_next_block++;
357 ret = bio_add_page(io->io_bio, bh->b_page, bh->b_size, bh_offset(bh));
358 if (ret != bh->b_size)
359 goto submit_and_retry;
360 if ((io_end->num_io_pages == 0) ||
361 (io_end->pages[io_end->num_io_pages-1] != io_page)) {
362 io_end->pages[io_end->num_io_pages++] = io_page;
363 io_page->p_count++;
364 }
365 return 0;
366}
367
368int ext4_bio_write_page(struct ext4_io_submit *io,
369 struct page *page,
370 int len,
371 struct writeback_control *wbc)
372{
373 struct inode *inode = page->mapping->host;
374 unsigned block_start, block_end, blocksize;
375 struct ext4_io_page *io_page;
376 struct buffer_head *bh, *head;
377 int ret = 0;
378
379 blocksize = 1 << inode->i_blkbits;
380
381 BUG_ON(PageWriteback(page));
382 set_page_writeback(page);
383 ClearPageError(page);
384
385 io_page = kmem_cache_alloc(io_page_cachep, GFP_NOFS);
386 if (!io_page) {
387 set_page_dirty(page);
388 unlock_page(page);
389 return -ENOMEM;
390 }
391 io_page->p_page = page;
392 io_page->p_count = 0;
393 get_page(page);
394
395 for (bh = head = page_buffers(page), block_start = 0;
396 bh != head || !block_start;
397 block_start = block_end, bh = bh->b_this_page) {
398 block_end = block_start + blocksize;
399 if (block_start >= len) {
400 clear_buffer_dirty(bh);
401 set_buffer_uptodate(bh);
402 continue;
403 }
404 ret = io_submit_add_bh(io, io_page, inode, wbc, bh);
405 if (ret) {
406 /*
407 * We only get here on ENOMEM. Not much else
408 * we can do but mark the page as dirty, and
409 * better luck next time.
410 */
411 set_page_dirty(page);
412 break;
413 }
414 }
415 unlock_page(page);
416 /*
417 * If the page was truncated before we could do the writeback,
418 * or we had a memory allocation error while trying to write
419 * the first buffer head, we won't have submitted any pages for
420 * I/O. In that case we need to make sure we've cleared the
421 * PageWriteback bit from the page to prevent the system from
422 * wedging later on.
423 */
424 if (io_page->p_count == 0) {
425 put_page(page);
426 end_page_writeback(page);
427 kmem_cache_free(io_page_cachep, io_page);
428 }
429 return ret;
430}
diff --git a/fs/ext4/super.c b/fs/ext4/super.c
index 8a24e9be7cb0..e13b3c3534d7 100644
--- a/fs/ext4/super.c
+++ b/fs/ext4/super.c
@@ -4769,9 +4769,12 @@ static int __init init_ext4_fs(void)
4769 int err; 4769 int err;
4770 4770
4771 ext4_check_flag_values(); 4771 ext4_check_flag_values();
4772 err = init_ext4_system_zone(); 4772 err = init_ext4_pageio();
4773 if (err) 4773 if (err)
4774 return err; 4774 return err;
4775 err = init_ext4_system_zone();
4776 if (err)
4777 goto out5;
4775 ext4_kset = kset_create_and_add("ext4", NULL, fs_kobj); 4778 ext4_kset = kset_create_and_add("ext4", NULL, fs_kobj);
4776 if (!ext4_kset) 4779 if (!ext4_kset)
4777 goto out4; 4780 goto out4;
@@ -4812,6 +4815,8 @@ out3:
4812 kset_unregister(ext4_kset); 4815 kset_unregister(ext4_kset);
4813out4: 4816out4:
4814 exit_ext4_system_zone(); 4817 exit_ext4_system_zone();
4818out5:
4819 exit_ext4_pageio();
4815 return err; 4820 return err;
4816} 4821}
4817 4822
@@ -4827,6 +4832,7 @@ static void __exit exit_ext4_fs(void)
4827 remove_proc_entry("fs/ext4", NULL); 4832 remove_proc_entry("fs/ext4", NULL);
4828 kset_unregister(ext4_kset); 4833 kset_unregister(ext4_kset);
4829 exit_ext4_system_zone(); 4834 exit_ext4_system_zone();
4835 exit_ext4_pageio();
4830} 4836}
4831 4837
4832MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others"); 4838MODULE_AUTHOR("Remy Card, Stephen Tweedie, Andrew Morton, Andreas Dilger, Theodore Ts'o and others");