Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 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 | |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 9 | #include <linux/fs.h> |
| 10 | #include <linux/time.h> |
| 11 | #include <linux/jbd2.h> |
| 12 | #include <linux/highuid.h> |
| 13 | #include <linux/pagemap.h> |
| 14 | #include <linux/quotaops.h> |
| 15 | #include <linux/string.h> |
| 16 | #include <linux/buffer_head.h> |
| 17 | #include <linux/writeback.h> |
| 18 | #include <linux/pagevec.h> |
| 19 | #include <linux/mpage.h> |
| 20 | #include <linux/namei.h> |
| 21 | #include <linux/uio.h> |
| 22 | #include <linux/bio.h> |
| 23 | #include <linux/workqueue.h> |
| 24 | #include <linux/kernel.h> |
| 25 | #include <linux/slab.h> |
Jan Kara | 1ae48a6 | 2013-01-28 09:32:54 -0500 | [diff] [blame] | 26 | #include <linux/mm.h> |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 27 | |
| 28 | #include "ext4_jbd2.h" |
| 29 | #include "xattr.h" |
| 30 | #include "acl.h" |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 31 | |
Jan Kara | 0058f96 | 2013-04-11 23:48:32 -0400 | [diff] [blame] | 32 | static struct kmem_cache *io_end_cachep; |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 33 | |
Theodore Ts'o | 5dabfc7 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 34 | int __init ext4_init_pageio(void) |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 35 | { |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 36 | io_end_cachep = KMEM_CACHE(ext4_io_end, SLAB_RECLAIM_ACCOUNT); |
Jan Kara | 0058f96 | 2013-04-11 23:48:32 -0400 | [diff] [blame] | 37 | if (io_end_cachep == NULL) |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 38 | return -ENOMEM; |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 39 | return 0; |
| 40 | } |
| 41 | |
Theodore Ts'o | 5dabfc7 | 2010-10-27 21:30:14 -0400 | [diff] [blame] | 42 | void ext4_exit_pageio(void) |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 43 | { |
| 44 | kmem_cache_destroy(io_end_cachep); |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 45 | } |
| 46 | |
Theodore Ts'o | 1ada47d | 2013-03-20 09:39:42 -0400 | [diff] [blame] | 47 | /* |
| 48 | * This function is called by ext4_evict_inode() to make sure there is |
| 49 | * no more pending I/O completion work left to do. |
| 50 | */ |
| 51 | void ext4_ioend_shutdown(struct inode *inode) |
Theodore Ts'o | f7ad6d2 | 2010-11-08 13:43:33 -0500 | [diff] [blame] | 52 | { |
Eric Sandeen | e9e3bce | 2011-02-12 08:17:34 -0500 | [diff] [blame] | 53 | wait_queue_head_t *wq = ext4_ioend_wq(inode); |
Theodore Ts'o | f7ad6d2 | 2010-11-08 13:43:33 -0500 | [diff] [blame] | 54 | |
| 55 | wait_event(*wq, (atomic_read(&EXT4_I(inode)->i_ioend_count) == 0)); |
Theodore Ts'o | 1ada47d | 2013-03-20 09:39:42 -0400 | [diff] [blame] | 56 | /* |
| 57 | * We need to make sure the work structure is finished being |
| 58 | * used before we let the inode get destroyed. |
| 59 | */ |
| 60 | if (work_pending(&EXT4_I(inode)->i_unwritten_work)) |
| 61 | cancel_work_sync(&EXT4_I(inode)->i_unwritten_work); |
Theodore Ts'o | f7ad6d2 | 2010-11-08 13:43:33 -0500 | [diff] [blame] | 62 | } |
| 63 | |
Jan Kara | 4eec708 | 2013-04-11 23:56:53 -0400 | [diff] [blame] | 64 | static void ext4_release_io_end(ext4_io_end_t *io_end) |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 65 | { |
Jan Kara | 4eec708 | 2013-04-11 23:56:53 -0400 | [diff] [blame] | 66 | BUG_ON(!list_empty(&io_end->list)); |
| 67 | BUG_ON(io_end->flag & EXT4_IO_END_UNWRITTEN); |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 68 | |
Jan Kara | 4eec708 | 2013-04-11 23:56:53 -0400 | [diff] [blame] | 69 | if (atomic_dec_and_test(&EXT4_I(io_end->inode)->i_ioend_count)) |
| 70 | wake_up_all(ext4_ioend_wq(io_end->inode)); |
| 71 | if (io_end->flag & EXT4_IO_END_DIRECT) |
| 72 | inode_dio_done(io_end->inode); |
| 73 | if (io_end->iocb) |
| 74 | aio_complete(io_end->iocb, io_end->result, 0); |
| 75 | kmem_cache_free(io_end_cachep, io_end); |
| 76 | } |
Dmitry Monakhov | 82e5422 | 2012-09-28 23:36:25 -0400 | [diff] [blame] | 77 | |
Jan Kara | 4eec708 | 2013-04-11 23:56:53 -0400 | [diff] [blame] | 78 | static void ext4_clear_io_unwritten_flag(ext4_io_end_t *io_end) |
| 79 | { |
| 80 | struct inode *inode = io_end->inode; |
| 81 | |
| 82 | io_end->flag &= ~EXT4_IO_END_UNWRITTEN; |
| 83 | /* Wake up anyone waiting on unwritten extent conversion */ |
| 84 | if (atomic_dec_and_test(&EXT4_I(inode)->i_unwritten)) |
| 85 | wake_up_all(ext4_ioend_wq(inode)); |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 86 | } |
| 87 | |
Dmitry Monakhov | 28a535f | 2012-09-29 00:14:55 -0400 | [diff] [blame] | 88 | /* check a range of space and convert unwritten extents to written. */ |
| 89 | static int ext4_end_io(ext4_io_end_t *io) |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 90 | { |
| 91 | struct inode *inode = io->inode; |
| 92 | loff_t offset = io->offset; |
| 93 | ssize_t size = io->size; |
| 94 | int ret = 0; |
| 95 | |
| 96 | ext4_debug("ext4_end_io_nolock: io 0x%p from inode %lu,list->next 0x%p," |
| 97 | "list->prev 0x%p\n", |
| 98 | io, inode->i_ino, io->list.next, io->list.prev); |
| 99 | |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 100 | ret = ext4_convert_unwritten_extents(inode, offset, size); |
| 101 | if (ret < 0) { |
Theodore Ts'o | b82e384 | 2011-10-31 10:56:32 -0400 | [diff] [blame] | 102 | ext4_msg(inode->i_sb, KERN_EMERG, |
| 103 | "failed to convert unwritten extents to written " |
| 104 | "extents -- potential data loss! " |
| 105 | "(inode %lu, offset %llu, size %zd, error %d)", |
| 106 | inode->i_ino, offset, size, ret); |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 107 | } |
Jan Kara | 4eec708 | 2013-04-11 23:56:53 -0400 | [diff] [blame] | 108 | ext4_clear_io_unwritten_flag(io); |
| 109 | ext4_release_io_end(io); |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 110 | return ret; |
| 111 | } |
| 112 | |
Dmitry Monakhov | 28a535f | 2012-09-29 00:14:55 -0400 | [diff] [blame] | 113 | static void dump_completed_IO(struct inode *inode) |
| 114 | { |
| 115 | #ifdef EXT4FS_DEBUG |
| 116 | struct list_head *cur, *before, *after; |
| 117 | ext4_io_end_t *io, *io0, *io1; |
Dmitry Monakhov | 28a535f | 2012-09-29 00:14:55 -0400 | [diff] [blame] | 118 | |
| 119 | if (list_empty(&EXT4_I(inode)->i_completed_io_list)) { |
| 120 | ext4_debug("inode %lu completed_io list is empty\n", |
| 121 | inode->i_ino); |
| 122 | return; |
| 123 | } |
| 124 | |
| 125 | ext4_debug("Dump inode %lu completed_io list\n", inode->i_ino); |
| 126 | list_for_each_entry(io, &EXT4_I(inode)->i_completed_io_list, list) { |
| 127 | cur = &io->list; |
| 128 | before = cur->prev; |
| 129 | io0 = container_of(before, ext4_io_end_t, list); |
| 130 | after = cur->next; |
| 131 | io1 = container_of(after, ext4_io_end_t, list); |
| 132 | |
| 133 | ext4_debug("io 0x%p from inode %lu,prev 0x%p,next 0x%p\n", |
| 134 | io, inode->i_ino, io0, io1); |
| 135 | } |
| 136 | #endif |
| 137 | } |
| 138 | |
| 139 | /* Add the io_end to per-inode completed end_io list. */ |
Jan Kara | 4eec708 | 2013-04-11 23:56:53 -0400 | [diff] [blame] | 140 | static void ext4_add_complete_io(ext4_io_end_t *io_end) |
Dmitry Monakhov | 28a535f | 2012-09-29 00:14:55 -0400 | [diff] [blame] | 141 | { |
| 142 | struct ext4_inode_info *ei = EXT4_I(io_end->inode); |
| 143 | struct workqueue_struct *wq; |
| 144 | unsigned long flags; |
| 145 | |
| 146 | BUG_ON(!(io_end->flag & EXT4_IO_END_UNWRITTEN)); |
| 147 | wq = EXT4_SB(io_end->inode->i_sb)->dio_unwritten_wq; |
| 148 | |
| 149 | spin_lock_irqsave(&ei->i_completed_io_lock, flags); |
Jan Kara | 84c1754 | 2013-01-28 09:43:46 -0500 | [diff] [blame] | 150 | if (list_empty(&ei->i_completed_io_list)) |
| 151 | queue_work(wq, &ei->i_unwritten_work); |
Dmitry Monakhov | 28a535f | 2012-09-29 00:14:55 -0400 | [diff] [blame] | 152 | list_add_tail(&io_end->list, &ei->i_completed_io_list); |
| 153 | spin_unlock_irqrestore(&ei->i_completed_io_lock, flags); |
| 154 | } |
| 155 | |
Jan Kara | 84c1754 | 2013-01-28 09:43:46 -0500 | [diff] [blame] | 156 | static int ext4_do_flush_completed_IO(struct inode *inode) |
Dmitry Monakhov | 28a535f | 2012-09-29 00:14:55 -0400 | [diff] [blame] | 157 | { |
| 158 | ext4_io_end_t *io; |
Jan Kara | 002bd7f | 2013-01-28 09:49:15 -0500 | [diff] [blame] | 159 | struct list_head unwritten; |
Dmitry Monakhov | 28a535f | 2012-09-29 00:14:55 -0400 | [diff] [blame] | 160 | unsigned long flags; |
| 161 | struct ext4_inode_info *ei = EXT4_I(inode); |
| 162 | int err, ret = 0; |
| 163 | |
Dmitry Monakhov | 28a535f | 2012-09-29 00:14:55 -0400 | [diff] [blame] | 164 | spin_lock_irqsave(&ei->i_completed_io_lock, flags); |
| 165 | dump_completed_IO(inode); |
| 166 | list_replace_init(&ei->i_completed_io_list, &unwritten); |
| 167 | spin_unlock_irqrestore(&ei->i_completed_io_lock, flags); |
| 168 | |
| 169 | while (!list_empty(&unwritten)) { |
| 170 | io = list_entry(unwritten.next, ext4_io_end_t, list); |
| 171 | BUG_ON(!(io->flag & EXT4_IO_END_UNWRITTEN)); |
| 172 | list_del_init(&io->list); |
| 173 | |
| 174 | err = ext4_end_io(io); |
| 175 | if (unlikely(!ret && err)) |
| 176 | ret = err; |
Dmitry Monakhov | 28a535f | 2012-09-29 00:14:55 -0400 | [diff] [blame] | 177 | } |
| 178 | return ret; |
| 179 | } |
| 180 | |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 181 | /* |
| 182 | * work on completed aio dio IO, to convert unwritten extents to extents |
| 183 | */ |
Jan Kara | 84c1754 | 2013-01-28 09:43:46 -0500 | [diff] [blame] | 184 | void ext4_end_io_work(struct work_struct *work) |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 185 | { |
Jan Kara | 84c1754 | 2013-01-28 09:43:46 -0500 | [diff] [blame] | 186 | struct ext4_inode_info *ei = container_of(work, struct ext4_inode_info, |
| 187 | i_unwritten_work); |
| 188 | ext4_do_flush_completed_IO(&ei->vfs_inode); |
Dmitry Monakhov | 28a535f | 2012-09-29 00:14:55 -0400 | [diff] [blame] | 189 | } |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 190 | |
Dmitry Monakhov | c278531 | 2012-10-05 11:31:55 -0400 | [diff] [blame] | 191 | int ext4_flush_unwritten_io(struct inode *inode) |
Dmitry Monakhov | 28a535f | 2012-09-29 00:14:55 -0400 | [diff] [blame] | 192 | { |
Dmitry Monakhov | c278531 | 2012-10-05 11:31:55 -0400 | [diff] [blame] | 193 | int ret; |
| 194 | WARN_ON_ONCE(!mutex_is_locked(&inode->i_mutex) && |
| 195 | !(inode->i_state & I_FREEING)); |
Jan Kara | 84c1754 | 2013-01-28 09:43:46 -0500 | [diff] [blame] | 196 | ret = ext4_do_flush_completed_IO(inode); |
Dmitry Monakhov | c278531 | 2012-10-05 11:31:55 -0400 | [diff] [blame] | 197 | ext4_unwritten_wait(inode); |
| 198 | return ret; |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | ext4_io_end_t *ext4_init_io_end(struct inode *inode, gfp_t flags) |
| 202 | { |
Jesper Juhl | b17b35e | 2010-12-19 21:41:55 -0500 | [diff] [blame] | 203 | ext4_io_end_t *io = kmem_cache_zalloc(io_end_cachep, flags); |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 204 | if (io) { |
Theodore Ts'o | f7ad6d2 | 2010-11-08 13:43:33 -0500 | [diff] [blame] | 205 | atomic_inc(&EXT4_I(inode)->i_ioend_count); |
| 206 | io->inode = inode; |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 207 | INIT_LIST_HEAD(&io->list); |
Jan Kara | 4eec708 | 2013-04-11 23:56:53 -0400 | [diff] [blame] | 208 | atomic_set(&io->count, 1); |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 209 | } |
| 210 | return io; |
| 211 | } |
| 212 | |
Jan Kara | 4eec708 | 2013-04-11 23:56:53 -0400 | [diff] [blame] | 213 | void ext4_put_io_end_defer(ext4_io_end_t *io_end) |
| 214 | { |
| 215 | if (atomic_dec_and_test(&io_end->count)) { |
| 216 | if (!(io_end->flag & EXT4_IO_END_UNWRITTEN) || !io_end->size) { |
| 217 | ext4_release_io_end(io_end); |
| 218 | return; |
| 219 | } |
| 220 | ext4_add_complete_io(io_end); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | int ext4_put_io_end(ext4_io_end_t *io_end) |
| 225 | { |
| 226 | int err = 0; |
| 227 | |
| 228 | if (atomic_dec_and_test(&io_end->count)) { |
| 229 | if (io_end->flag & EXT4_IO_END_UNWRITTEN) { |
| 230 | err = ext4_convert_unwritten_extents(io_end->inode, |
| 231 | io_end->offset, io_end->size); |
| 232 | ext4_clear_io_unwritten_flag(io_end); |
| 233 | } |
| 234 | ext4_release_io_end(io_end); |
| 235 | } |
| 236 | return err; |
| 237 | } |
| 238 | |
| 239 | ext4_io_end_t *ext4_get_io_end(ext4_io_end_t *io_end) |
| 240 | { |
| 241 | atomic_inc(&io_end->count); |
| 242 | return io_end; |
| 243 | } |
| 244 | |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 245 | /* |
| 246 | * Print an buffer I/O error compatible with the fs/buffer.c. This |
| 247 | * provides compatibility with dmesg scrapers that look for a specific |
| 248 | * buffer I/O error message. We really need a unified error reporting |
| 249 | * structure to userspace ala Digital Unix's uerf system, but it's |
| 250 | * probably not going to happen in my lifetime, due to LKML politics... |
| 251 | */ |
| 252 | static void buffer_io_error(struct buffer_head *bh) |
| 253 | { |
| 254 | char b[BDEVNAME_SIZE]; |
| 255 | printk(KERN_ERR "Buffer I/O error on device %s, logical block %llu\n", |
| 256 | bdevname(bh->b_bdev, b), |
| 257 | (unsigned long long)bh->b_blocknr); |
| 258 | } |
| 259 | |
| 260 | static void ext4_end_bio(struct bio *bio, int error) |
| 261 | { |
| 262 | ext4_io_end_t *io_end = bio->bi_private; |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 263 | struct inode *inode; |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 264 | int i; |
Jan Kara | 0058f96 | 2013-04-11 23:48:32 -0400 | [diff] [blame] | 265 | int blocksize; |
Curt Wohlgemuth | d50bdd5 | 2011-02-07 12:46:14 -0500 | [diff] [blame] | 266 | sector_t bi_sector = bio->bi_sector; |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 267 | |
| 268 | BUG_ON(!io_end); |
Jan Kara | 0058f96 | 2013-04-11 23:48:32 -0400 | [diff] [blame] | 269 | inode = io_end->inode; |
| 270 | blocksize = 1 << inode->i_blkbits; |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 271 | bio->bi_private = NULL; |
| 272 | bio->bi_end_io = NULL; |
| 273 | if (test_bit(BIO_UPTODATE, &bio->bi_flags)) |
| 274 | error = 0; |
Jan Kara | 0058f96 | 2013-04-11 23:48:32 -0400 | [diff] [blame] | 275 | for (i = 0; i < bio->bi_vcnt; i++) { |
| 276 | struct bio_vec *bvec = &bio->bi_io_vec[i]; |
| 277 | struct page *page = bvec->bv_page; |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 278 | struct buffer_head *bh, *head; |
Jan Kara | 0058f96 | 2013-04-11 23:48:32 -0400 | [diff] [blame] | 279 | unsigned bio_start = bvec->bv_offset; |
| 280 | unsigned bio_end = bio_start + bvec->bv_len; |
| 281 | unsigned under_io = 0; |
| 282 | unsigned long flags; |
| 283 | |
| 284 | if (!page) |
| 285 | continue; |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 286 | |
Curt Wohlgemuth | 39db00f | 2011-04-30 13:26:26 -0400 | [diff] [blame] | 287 | if (error) { |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 288 | SetPageError(page); |
Curt Wohlgemuth | 39db00f | 2011-04-30 13:26:26 -0400 | [diff] [blame] | 289 | set_bit(AS_EIO, &page->mapping->flags); |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 290 | } |
Jan Kara | 0058f96 | 2013-04-11 23:48:32 -0400 | [diff] [blame] | 291 | bh = head = page_buffers(page); |
| 292 | /* |
| 293 | * We check all buffers in the page under BH_Uptodate_Lock |
| 294 | * to avoid races with other end io clearing async_write flags |
| 295 | */ |
| 296 | local_irq_save(flags); |
| 297 | bit_spin_lock(BH_Uptodate_Lock, &head->b_state); |
| 298 | do { |
| 299 | if (bh_offset(bh) < bio_start || |
| 300 | bh_offset(bh) + blocksize > bio_end) { |
| 301 | if (buffer_async_write(bh)) |
| 302 | under_io++; |
| 303 | continue; |
| 304 | } |
| 305 | clear_buffer_async_write(bh); |
| 306 | if (error) |
| 307 | buffer_io_error(bh); |
| 308 | } while ((bh = bh->b_this_page) != head); |
| 309 | bit_spin_unlock(BH_Uptodate_Lock, &head->b_state); |
| 310 | local_irq_restore(flags); |
| 311 | if (!under_io) |
| 312 | end_page_writeback(page); |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 313 | } |
Jan Kara | 0058f96 | 2013-04-11 23:48:32 -0400 | [diff] [blame] | 314 | bio_put(bio); |
Theodore Ts'o | f7ad6d2 | 2010-11-08 13:43:33 -0500 | [diff] [blame] | 315 | |
| 316 | if (error) { |
| 317 | io_end->flag |= EXT4_IO_END_ERROR; |
| 318 | ext4_warning(inode->i_sb, "I/O error writing to inode %lu " |
| 319 | "(offset %llu size %ld starting block %llu)", |
| 320 | inode->i_ino, |
| 321 | (unsigned long long) io_end->offset, |
| 322 | (long) io_end->size, |
| 323 | (unsigned long long) |
Curt Wohlgemuth | d50bdd5 | 2011-02-07 12:46:14 -0500 | [diff] [blame] | 324 | bi_sector >> (inode->i_blkbits - 9)); |
Theodore Ts'o | f7ad6d2 | 2010-11-08 13:43:33 -0500 | [diff] [blame] | 325 | } |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 326 | |
Jan Kara | 4eec708 | 2013-04-11 23:56:53 -0400 | [diff] [blame] | 327 | ext4_put_io_end_defer(io_end); |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | void ext4_io_submit(struct ext4_io_submit *io) |
| 331 | { |
| 332 | struct bio *bio = io->io_bio; |
| 333 | |
| 334 | if (bio) { |
| 335 | bio_get(io->io_bio); |
| 336 | submit_bio(io->io_op, io->io_bio); |
| 337 | BUG_ON(bio_flagged(io->io_bio, BIO_EOPNOTSUPP)); |
| 338 | bio_put(io->io_bio); |
| 339 | } |
Peter Huewe | 7dc5761 | 2011-02-21 21:01:42 -0500 | [diff] [blame] | 340 | io->io_bio = NULL; |
Jan Kara | 4eec708 | 2013-04-11 23:56:53 -0400 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | void ext4_io_submit_init(struct ext4_io_submit *io, |
| 344 | struct writeback_control *wbc) |
| 345 | { |
| 346 | io->io_op = (wbc->sync_mode == WB_SYNC_ALL ? WRITE_SYNC : WRITE); |
| 347 | io->io_bio = NULL; |
Peter Huewe | 7dc5761 | 2011-02-21 21:01:42 -0500 | [diff] [blame] | 348 | io->io_end = NULL; |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 349 | } |
| 350 | |
Jan Kara | 4eec708 | 2013-04-11 23:56:53 -0400 | [diff] [blame] | 351 | static int io_submit_init_bio(struct ext4_io_submit *io, |
| 352 | struct buffer_head *bh) |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 353 | { |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 354 | int nvecs = bio_get_nr_vecs(bh->b_bdev); |
| 355 | struct bio *bio; |
| 356 | |
Theodore Ts'o | 275d3ba | 2011-06-29 21:44:45 -0400 | [diff] [blame] | 357 | bio = bio_alloc(GFP_NOIO, min(nvecs, BIO_MAX_PAGES)); |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 358 | bio->bi_sector = bh->b_blocknr * (bh->b_size >> 9); |
| 359 | bio->bi_bdev = bh->b_bdev; |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 360 | bio->bi_end_io = ext4_end_bio; |
Jan Kara | 4eec708 | 2013-04-11 23:56:53 -0400 | [diff] [blame] | 361 | bio->bi_private = ext4_get_io_end(io->io_end); |
| 362 | if (!io->io_end->size) |
| 363 | io->io_end->offset = (bh->b_page->index << PAGE_CACHE_SHIFT) |
| 364 | + bh_offset(bh); |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 365 | io->io_bio = bio; |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 366 | io->io_next_block = bh->b_blocknr; |
| 367 | return 0; |
| 368 | } |
| 369 | |
| 370 | static int io_submit_add_bh(struct ext4_io_submit *io, |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 371 | struct inode *inode, |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 372 | struct buffer_head *bh) |
| 373 | { |
| 374 | ext4_io_end_t *io_end; |
| 375 | int ret; |
| 376 | |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 377 | if (io->io_bio && bh->b_blocknr != io->io_next_block) { |
| 378 | submit_and_retry: |
| 379 | ext4_io_submit(io); |
| 380 | } |
| 381 | if (io->io_bio == NULL) { |
Jan Kara | 4eec708 | 2013-04-11 23:56:53 -0400 | [diff] [blame] | 382 | ret = io_submit_init_bio(io, bh); |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 383 | if (ret) |
| 384 | return ret; |
| 385 | } |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 386 | ret = bio_add_page(io->io_bio, bh->b_page, bh->b_size, bh_offset(bh)); |
| 387 | if (ret != bh->b_size) |
| 388 | goto submit_and_retry; |
Jan Kara | 4eec708 | 2013-04-11 23:56:53 -0400 | [diff] [blame] | 389 | io_end = io->io_end; |
Jan Kara | 7b001d6 | 2013-04-12 00:03:19 -0400 | [diff] [blame] | 390 | if (test_clear_buffer_uninit(bh)) |
Jan Kara | 4eec708 | 2013-04-11 23:56:53 -0400 | [diff] [blame] | 391 | ext4_set_io_unwritten_flag(inode, io_end); |
| 392 | io_end->size += bh->b_size; |
| 393 | io->io_next_block++; |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 394 | return 0; |
| 395 | } |
| 396 | |
| 397 | int ext4_bio_write_page(struct ext4_io_submit *io, |
| 398 | struct page *page, |
| 399 | int len, |
| 400 | struct writeback_control *wbc) |
| 401 | { |
| 402 | struct inode *inode = page->mapping->host; |
Jan Kara | 0058f96 | 2013-04-11 23:48:32 -0400 | [diff] [blame] | 403 | unsigned block_start, blocksize; |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 404 | struct buffer_head *bh, *head; |
| 405 | int ret = 0; |
Jan Kara | 0058f96 | 2013-04-11 23:48:32 -0400 | [diff] [blame] | 406 | int nr_submitted = 0; |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 407 | |
| 408 | blocksize = 1 << inode->i_blkbits; |
| 409 | |
Curt Wohlgemuth | d50bdd5 | 2011-02-07 12:46:14 -0500 | [diff] [blame] | 410 | BUG_ON(!PageLocked(page)); |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 411 | BUG_ON(PageWriteback(page)); |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 412 | |
Theodore Ts'o | a54aa76 | 2011-02-27 16:43:24 -0500 | [diff] [blame] | 413 | set_page_writeback(page); |
| 414 | ClearPageError(page); |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 415 | |
Jan Kara | 0058f96 | 2013-04-11 23:48:32 -0400 | [diff] [blame] | 416 | /* |
| 417 | * In the first loop we prepare and mark buffers to submit. We have to |
| 418 | * mark all buffers in the page before submitting so that |
| 419 | * end_page_writeback() cannot be called from ext4_bio_end_io() when IO |
| 420 | * on the first buffer finishes and we are still working on submitting |
| 421 | * the second buffer. |
| 422 | */ |
| 423 | bh = head = page_buffers(page); |
| 424 | do { |
| 425 | block_start = bh_offset(bh); |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 426 | if (block_start >= len) { |
Yongqiang Yang | 5a0dc73 | 2011-12-13 22:29:12 -0500 | [diff] [blame] | 427 | /* |
| 428 | * Comments copied from block_write_full_page_endio: |
| 429 | * |
| 430 | * The page straddles i_size. It must be zeroed out on |
| 431 | * each and every writepage invocation because it may |
| 432 | * be mmapped. "A file is mapped in multiples of the |
| 433 | * page size. For a file that is not a multiple of |
| 434 | * the page size, the remaining memory is zeroed when |
| 435 | * mapped, and writes to that region are not written |
| 436 | * out to the file." |
| 437 | */ |
Jan Kara | 0058f96 | 2013-04-11 23:48:32 -0400 | [diff] [blame] | 438 | zero_user_segment(page, block_start, |
| 439 | block_start + blocksize); |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 440 | clear_buffer_dirty(bh); |
| 441 | set_buffer_uptodate(bh); |
| 442 | continue; |
| 443 | } |
Jan Kara | 8a850c3 | 2013-01-28 20:53:28 -0500 | [diff] [blame] | 444 | if (!buffer_dirty(bh) || buffer_delay(bh) || |
| 445 | !buffer_mapped(bh) || buffer_unwritten(bh)) { |
| 446 | /* A hole? We can safely clear the dirty bit */ |
| 447 | if (!buffer_mapped(bh)) |
| 448 | clear_buffer_dirty(bh); |
| 449 | if (io->io_bio) |
| 450 | ext4_io_submit(io); |
| 451 | continue; |
| 452 | } |
Jan Kara | 0058f96 | 2013-04-11 23:48:32 -0400 | [diff] [blame] | 453 | if (buffer_new(bh)) { |
| 454 | clear_buffer_new(bh); |
| 455 | unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr); |
| 456 | } |
| 457 | set_buffer_async_write(bh); |
| 458 | } while ((bh = bh->b_this_page) != head); |
| 459 | |
| 460 | /* Now submit buffers to write */ |
| 461 | bh = head = page_buffers(page); |
| 462 | do { |
| 463 | if (!buffer_async_write(bh)) |
| 464 | continue; |
Jan Kara | 4eec708 | 2013-04-11 23:56:53 -0400 | [diff] [blame] | 465 | ret = io_submit_add_bh(io, inode, bh); |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 466 | if (ret) { |
| 467 | /* |
| 468 | * We only get here on ENOMEM. Not much else |
| 469 | * we can do but mark the page as dirty, and |
| 470 | * better luck next time. |
| 471 | */ |
Jan Kara | 1ae48a6 | 2013-01-28 09:32:54 -0500 | [diff] [blame] | 472 | redirty_page_for_writepage(wbc, page); |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 473 | break; |
| 474 | } |
Jan Kara | 0058f96 | 2013-04-11 23:48:32 -0400 | [diff] [blame] | 475 | nr_submitted++; |
Jan Kara | 1ae48a6 | 2013-01-28 09:32:54 -0500 | [diff] [blame] | 476 | clear_buffer_dirty(bh); |
Jan Kara | 0058f96 | 2013-04-11 23:48:32 -0400 | [diff] [blame] | 477 | } while ((bh = bh->b_this_page) != head); |
| 478 | |
| 479 | /* Error stopped previous loop? Clean up buffers... */ |
| 480 | if (ret) { |
| 481 | do { |
| 482 | clear_buffer_async_write(bh); |
| 483 | bh = bh->b_this_page; |
| 484 | } while (bh != head); |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 485 | } |
| 486 | unlock_page(page); |
Jan Kara | 0058f96 | 2013-04-11 23:48:32 -0400 | [diff] [blame] | 487 | /* Nothing submitted - we have to end page writeback */ |
| 488 | if (!nr_submitted) |
| 489 | end_page_writeback(page); |
Theodore Ts'o | bd2d021 | 2010-10-27 21:30:10 -0400 | [diff] [blame] | 490 | return ret; |
| 491 | } |