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