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