1bd2d0210STheodore Ts'o /* 2bd2d0210STheodore Ts'o * linux/fs/ext4/page-io.c 3bd2d0210STheodore Ts'o * 4bd2d0210STheodore Ts'o * This contains the new page_io functions for ext4 5bd2d0210STheodore Ts'o * 6bd2d0210STheodore Ts'o * Written by Theodore Ts'o, 2010. 7bd2d0210STheodore Ts'o */ 8bd2d0210STheodore Ts'o 9bd2d0210STheodore Ts'o #include <linux/fs.h> 10bd2d0210STheodore Ts'o #include <linux/time.h> 11bd2d0210STheodore Ts'o #include <linux/jbd2.h> 12bd2d0210STheodore Ts'o #include <linux/highuid.h> 13bd2d0210STheodore Ts'o #include <linux/pagemap.h> 14bd2d0210STheodore Ts'o #include <linux/quotaops.h> 15bd2d0210STheodore Ts'o #include <linux/string.h> 16bd2d0210STheodore Ts'o #include <linux/buffer_head.h> 17bd2d0210STheodore Ts'o #include <linux/writeback.h> 18bd2d0210STheodore Ts'o #include <linux/pagevec.h> 19bd2d0210STheodore Ts'o #include <linux/mpage.h> 20bd2d0210STheodore Ts'o #include <linux/namei.h> 21a27bb332SKent Overstreet #include <linux/aio.h> 22bd2d0210STheodore Ts'o #include <linux/uio.h> 23bd2d0210STheodore Ts'o #include <linux/bio.h> 24bd2d0210STheodore Ts'o #include <linux/workqueue.h> 25bd2d0210STheodore Ts'o #include <linux/kernel.h> 26bd2d0210STheodore Ts'o #include <linux/slab.h> 271ae48a63SJan Kara #include <linux/mm.h> 28bd2d0210STheodore Ts'o 29bd2d0210STheodore Ts'o #include "ext4_jbd2.h" 30bd2d0210STheodore Ts'o #include "xattr.h" 31bd2d0210STheodore Ts'o #include "acl.h" 32bd2d0210STheodore Ts'o 330058f965SJan Kara static struct kmem_cache *io_end_cachep; 34bd2d0210STheodore Ts'o 355dabfc78STheodore Ts'o int __init ext4_init_pageio(void) 36bd2d0210STheodore Ts'o { 37bd2d0210STheodore Ts'o io_end_cachep = KMEM_CACHE(ext4_io_end, SLAB_RECLAIM_ACCOUNT); 380058f965SJan Kara if (io_end_cachep == NULL) 39bd2d0210STheodore Ts'o return -ENOMEM; 40bd2d0210STheodore Ts'o return 0; 41bd2d0210STheodore Ts'o } 42bd2d0210STheodore Ts'o 435dabfc78STheodore Ts'o void ext4_exit_pageio(void) 44bd2d0210STheodore Ts'o { 45bd2d0210STheodore Ts'o kmem_cache_destroy(io_end_cachep); 46bd2d0210STheodore Ts'o } 47bd2d0210STheodore Ts'o 481ada47d9STheodore Ts'o /* 491ada47d9STheodore Ts'o * This function is called by ext4_evict_inode() to make sure there is 501ada47d9STheodore Ts'o * no more pending I/O completion work left to do. 511ada47d9STheodore Ts'o */ 521ada47d9STheodore Ts'o void ext4_ioend_shutdown(struct inode *inode) 53f7ad6d2eSTheodore Ts'o { 54e9e3bcecSEric Sandeen wait_queue_head_t *wq = ext4_ioend_wq(inode); 55f7ad6d2eSTheodore Ts'o 56f7ad6d2eSTheodore Ts'o wait_event(*wq, (atomic_read(&EXT4_I(inode)->i_ioend_count) == 0)); 571ada47d9STheodore Ts'o /* 581ada47d9STheodore Ts'o * We need to make sure the work structure is finished being 591ada47d9STheodore Ts'o * used before we let the inode get destroyed. 601ada47d9STheodore Ts'o */ 61*2e8fa54eSJan Kara if (work_pending(&EXT4_I(inode)->i_rsv_conversion_work)) 62*2e8fa54eSJan Kara cancel_work_sync(&EXT4_I(inode)->i_rsv_conversion_work); 63*2e8fa54eSJan Kara if (work_pending(&EXT4_I(inode)->i_unrsv_conversion_work)) 64*2e8fa54eSJan Kara cancel_work_sync(&EXT4_I(inode)->i_unrsv_conversion_work); 65f7ad6d2eSTheodore Ts'o } 66f7ad6d2eSTheodore Ts'o 6797a851edSJan Kara static void ext4_release_io_end(ext4_io_end_t *io_end) 68bd2d0210STheodore Ts'o { 6997a851edSJan Kara BUG_ON(!list_empty(&io_end->list)); 7097a851edSJan Kara BUG_ON(io_end->flag & EXT4_IO_END_UNWRITTEN); 716b523df4SJan Kara WARN_ON(io_end->handle); 72bd2d0210STheodore Ts'o 7397a851edSJan Kara if (atomic_dec_and_test(&EXT4_I(io_end->inode)->i_ioend_count)) 7497a851edSJan Kara wake_up_all(ext4_ioend_wq(io_end->inode)); 7597a851edSJan Kara if (io_end->flag & EXT4_IO_END_DIRECT) 7697a851edSJan Kara inode_dio_done(io_end->inode); 7797a851edSJan Kara if (io_end->iocb) 7897a851edSJan Kara aio_complete(io_end->iocb, io_end->result, 0); 7997a851edSJan Kara kmem_cache_free(io_end_cachep, io_end); 8097a851edSJan Kara } 8197a851edSJan Kara 8297a851edSJan Kara static void ext4_clear_io_unwritten_flag(ext4_io_end_t *io_end) 8397a851edSJan Kara { 8497a851edSJan Kara struct inode *inode = io_end->inode; 8597a851edSJan Kara 8697a851edSJan Kara io_end->flag &= ~EXT4_IO_END_UNWRITTEN; 8797a851edSJan Kara /* Wake up anyone waiting on unwritten extent conversion */ 8897a851edSJan Kara if (atomic_dec_and_test(&EXT4_I(inode)->i_unwritten)) 8997a851edSJan Kara wake_up_all(ext4_ioend_wq(inode)); 90bd2d0210STheodore Ts'o } 91bd2d0210STheodore Ts'o 9228a535f9SDmitry Monakhov /* check a range of space and convert unwritten extents to written. */ 9328a535f9SDmitry Monakhov static int ext4_end_io(ext4_io_end_t *io) 94bd2d0210STheodore Ts'o { 95bd2d0210STheodore Ts'o struct inode *inode = io->inode; 96bd2d0210STheodore Ts'o loff_t offset = io->offset; 97bd2d0210STheodore Ts'o ssize_t size = io->size; 986b523df4SJan Kara handle_t *handle = io->handle; 99bd2d0210STheodore Ts'o int ret = 0; 100bd2d0210STheodore Ts'o 101bd2d0210STheodore Ts'o ext4_debug("ext4_end_io_nolock: io 0x%p from inode %lu,list->next 0x%p," 102bd2d0210STheodore Ts'o "list->prev 0x%p\n", 103bd2d0210STheodore Ts'o io, inode->i_ino, io->list.next, io->list.prev); 104bd2d0210STheodore Ts'o 1056b523df4SJan Kara io->handle = NULL; /* Following call will use up the handle */ 1066b523df4SJan Kara ret = ext4_convert_unwritten_extents(handle, inode, offset, size); 107bd2d0210STheodore Ts'o if (ret < 0) { 108b82e384cSTheodore Ts'o ext4_msg(inode->i_sb, KERN_EMERG, 109b82e384cSTheodore Ts'o "failed to convert unwritten extents to written " 110b82e384cSTheodore Ts'o "extents -- potential data loss! " 111b82e384cSTheodore Ts'o "(inode %lu, offset %llu, size %zd, error %d)", 112b82e384cSTheodore Ts'o inode->i_ino, offset, size, ret); 113bd2d0210STheodore Ts'o } 11497a851edSJan Kara ext4_clear_io_unwritten_flag(io); 11597a851edSJan Kara ext4_release_io_end(io); 116bd2d0210STheodore Ts'o return ret; 117bd2d0210STheodore Ts'o } 118bd2d0210STheodore Ts'o 119*2e8fa54eSJan Kara static void dump_completed_IO(struct inode *inode, struct list_head *head) 12028a535f9SDmitry Monakhov { 12128a535f9SDmitry Monakhov #ifdef EXT4FS_DEBUG 12228a535f9SDmitry Monakhov struct list_head *cur, *before, *after; 12328a535f9SDmitry Monakhov ext4_io_end_t *io, *io0, *io1; 12428a535f9SDmitry Monakhov 125*2e8fa54eSJan Kara if (list_empty(head)) 12628a535f9SDmitry Monakhov return; 12728a535f9SDmitry Monakhov 128*2e8fa54eSJan Kara ext4_debug("Dump inode %lu completed io list\n", inode->i_ino); 129*2e8fa54eSJan Kara list_for_each_entry(io, head, list) { 13028a535f9SDmitry Monakhov cur = &io->list; 13128a535f9SDmitry Monakhov before = cur->prev; 13228a535f9SDmitry Monakhov io0 = container_of(before, ext4_io_end_t, list); 13328a535f9SDmitry Monakhov after = cur->next; 13428a535f9SDmitry Monakhov io1 = container_of(after, ext4_io_end_t, list); 13528a535f9SDmitry Monakhov 13628a535f9SDmitry Monakhov ext4_debug("io 0x%p from inode %lu,prev 0x%p,next 0x%p\n", 13728a535f9SDmitry Monakhov io, inode->i_ino, io0, io1); 13828a535f9SDmitry Monakhov } 13928a535f9SDmitry Monakhov #endif 14028a535f9SDmitry Monakhov } 14128a535f9SDmitry Monakhov 14228a535f9SDmitry Monakhov /* Add the io_end to per-inode completed end_io list. */ 14397a851edSJan Kara static void ext4_add_complete_io(ext4_io_end_t *io_end) 14428a535f9SDmitry Monakhov { 14528a535f9SDmitry Monakhov struct ext4_inode_info *ei = EXT4_I(io_end->inode); 14628a535f9SDmitry Monakhov struct workqueue_struct *wq; 14728a535f9SDmitry Monakhov unsigned long flags; 14828a535f9SDmitry Monakhov 14928a535f9SDmitry Monakhov BUG_ON(!(io_end->flag & EXT4_IO_END_UNWRITTEN)); 15028a535f9SDmitry Monakhov spin_lock_irqsave(&ei->i_completed_io_lock, flags); 151*2e8fa54eSJan Kara if (io_end->handle) { 152*2e8fa54eSJan Kara wq = EXT4_SB(io_end->inode->i_sb)->rsv_conversion_wq; 153*2e8fa54eSJan Kara if (list_empty(&ei->i_rsv_conversion_list)) 154*2e8fa54eSJan Kara queue_work(wq, &ei->i_rsv_conversion_work); 155*2e8fa54eSJan Kara list_add_tail(&io_end->list, &ei->i_rsv_conversion_list); 156*2e8fa54eSJan Kara } else { 157*2e8fa54eSJan Kara wq = EXT4_SB(io_end->inode->i_sb)->unrsv_conversion_wq; 158*2e8fa54eSJan Kara if (list_empty(&ei->i_unrsv_conversion_list)) 159*2e8fa54eSJan Kara queue_work(wq, &ei->i_unrsv_conversion_work); 160*2e8fa54eSJan Kara list_add_tail(&io_end->list, &ei->i_unrsv_conversion_list); 161*2e8fa54eSJan Kara } 16228a535f9SDmitry Monakhov spin_unlock_irqrestore(&ei->i_completed_io_lock, flags); 16328a535f9SDmitry Monakhov } 16428a535f9SDmitry Monakhov 165*2e8fa54eSJan Kara static int ext4_do_flush_completed_IO(struct inode *inode, 166*2e8fa54eSJan Kara struct list_head *head) 16728a535f9SDmitry Monakhov { 16828a535f9SDmitry Monakhov ext4_io_end_t *io; 169002bd7faSJan Kara struct list_head unwritten; 17028a535f9SDmitry Monakhov unsigned long flags; 17128a535f9SDmitry Monakhov struct ext4_inode_info *ei = EXT4_I(inode); 17228a535f9SDmitry Monakhov int err, ret = 0; 17328a535f9SDmitry Monakhov 17428a535f9SDmitry Monakhov spin_lock_irqsave(&ei->i_completed_io_lock, flags); 175*2e8fa54eSJan Kara dump_completed_IO(inode, head); 176*2e8fa54eSJan Kara list_replace_init(head, &unwritten); 17728a535f9SDmitry Monakhov spin_unlock_irqrestore(&ei->i_completed_io_lock, flags); 17828a535f9SDmitry Monakhov 17928a535f9SDmitry Monakhov while (!list_empty(&unwritten)) { 18028a535f9SDmitry Monakhov io = list_entry(unwritten.next, ext4_io_end_t, list); 18128a535f9SDmitry Monakhov BUG_ON(!(io->flag & EXT4_IO_END_UNWRITTEN)); 18228a535f9SDmitry Monakhov list_del_init(&io->list); 18328a535f9SDmitry Monakhov 18428a535f9SDmitry Monakhov err = ext4_end_io(io); 18528a535f9SDmitry Monakhov if (unlikely(!ret && err)) 18628a535f9SDmitry Monakhov ret = err; 18728a535f9SDmitry Monakhov } 18828a535f9SDmitry Monakhov return ret; 18928a535f9SDmitry Monakhov } 19028a535f9SDmitry Monakhov 191bd2d0210STheodore Ts'o /* 192*2e8fa54eSJan Kara * work on completed IO, to convert unwritten extents to extents 193bd2d0210STheodore Ts'o */ 194*2e8fa54eSJan Kara void ext4_end_io_rsv_work(struct work_struct *work) 195bd2d0210STheodore Ts'o { 19684c17543SJan Kara struct ext4_inode_info *ei = container_of(work, struct ext4_inode_info, 197*2e8fa54eSJan Kara i_rsv_conversion_work); 198*2e8fa54eSJan Kara ext4_do_flush_completed_IO(&ei->vfs_inode, &ei->i_rsv_conversion_list); 199*2e8fa54eSJan Kara } 200*2e8fa54eSJan Kara 201*2e8fa54eSJan Kara void ext4_end_io_unrsv_work(struct work_struct *work) 202*2e8fa54eSJan Kara { 203*2e8fa54eSJan Kara struct ext4_inode_info *ei = container_of(work, struct ext4_inode_info, 204*2e8fa54eSJan Kara i_unrsv_conversion_work); 205*2e8fa54eSJan Kara ext4_do_flush_completed_IO(&ei->vfs_inode, &ei->i_unrsv_conversion_list); 206d73d5046STao Ma } 207d73d5046STao Ma 208c278531dSDmitry Monakhov int ext4_flush_unwritten_io(struct inode *inode) 20928a535f9SDmitry Monakhov { 210*2e8fa54eSJan Kara int ret, err; 211*2e8fa54eSJan Kara 212c278531dSDmitry Monakhov WARN_ON_ONCE(!mutex_is_locked(&inode->i_mutex) && 213c278531dSDmitry Monakhov !(inode->i_state & I_FREEING)); 214*2e8fa54eSJan Kara ret = ext4_do_flush_completed_IO(inode, 215*2e8fa54eSJan Kara &EXT4_I(inode)->i_rsv_conversion_list); 216*2e8fa54eSJan Kara err = ext4_do_flush_completed_IO(inode, 217*2e8fa54eSJan Kara &EXT4_I(inode)->i_unrsv_conversion_list); 218*2e8fa54eSJan Kara if (!ret) 219*2e8fa54eSJan Kara ret = err; 220c278531dSDmitry Monakhov ext4_unwritten_wait(inode); 221c278531dSDmitry Monakhov return ret; 222bd2d0210STheodore Ts'o } 223bd2d0210STheodore Ts'o 224bd2d0210STheodore Ts'o ext4_io_end_t *ext4_init_io_end(struct inode *inode, gfp_t flags) 225bd2d0210STheodore Ts'o { 226b17b35ecSJesper Juhl ext4_io_end_t *io = kmem_cache_zalloc(io_end_cachep, flags); 227bd2d0210STheodore Ts'o if (io) { 228f7ad6d2eSTheodore Ts'o atomic_inc(&EXT4_I(inode)->i_ioend_count); 229f7ad6d2eSTheodore Ts'o io->inode = inode; 230bd2d0210STheodore Ts'o INIT_LIST_HEAD(&io->list); 23197a851edSJan Kara atomic_set(&io->count, 1); 232bd2d0210STheodore Ts'o } 233bd2d0210STheodore Ts'o return io; 234bd2d0210STheodore Ts'o } 235bd2d0210STheodore Ts'o 23697a851edSJan Kara void ext4_put_io_end_defer(ext4_io_end_t *io_end) 23797a851edSJan Kara { 23897a851edSJan Kara if (atomic_dec_and_test(&io_end->count)) { 23997a851edSJan Kara if (!(io_end->flag & EXT4_IO_END_UNWRITTEN) || !io_end->size) { 24097a851edSJan Kara ext4_release_io_end(io_end); 24197a851edSJan Kara return; 24297a851edSJan Kara } 24397a851edSJan Kara ext4_add_complete_io(io_end); 24497a851edSJan Kara } 24597a851edSJan Kara } 24697a851edSJan Kara 24797a851edSJan Kara int ext4_put_io_end(ext4_io_end_t *io_end) 24897a851edSJan Kara { 24997a851edSJan Kara int err = 0; 25097a851edSJan Kara 25197a851edSJan Kara if (atomic_dec_and_test(&io_end->count)) { 25297a851edSJan Kara if (io_end->flag & EXT4_IO_END_UNWRITTEN) { 2536b523df4SJan Kara err = ext4_convert_unwritten_extents(io_end->handle, 2546b523df4SJan Kara io_end->inode, io_end->offset, 2556b523df4SJan Kara io_end->size); 2566b523df4SJan Kara io_end->handle = NULL; 25797a851edSJan Kara ext4_clear_io_unwritten_flag(io_end); 25897a851edSJan Kara } 25997a851edSJan Kara ext4_release_io_end(io_end); 26097a851edSJan Kara } 26197a851edSJan Kara return err; 26297a851edSJan Kara } 26397a851edSJan Kara 26497a851edSJan Kara ext4_io_end_t *ext4_get_io_end(ext4_io_end_t *io_end) 26597a851edSJan Kara { 26697a851edSJan Kara atomic_inc(&io_end->count); 26797a851edSJan Kara return io_end; 26897a851edSJan Kara } 26997a851edSJan Kara 270bd2d0210STheodore Ts'o /* 271bd2d0210STheodore Ts'o * Print an buffer I/O error compatible with the fs/buffer.c. This 272bd2d0210STheodore Ts'o * provides compatibility with dmesg scrapers that look for a specific 273bd2d0210STheodore Ts'o * buffer I/O error message. We really need a unified error reporting 274bd2d0210STheodore Ts'o * structure to userspace ala Digital Unix's uerf system, but it's 275bd2d0210STheodore Ts'o * probably not going to happen in my lifetime, due to LKML politics... 276bd2d0210STheodore Ts'o */ 277bd2d0210STheodore Ts'o static void buffer_io_error(struct buffer_head *bh) 278bd2d0210STheodore Ts'o { 279bd2d0210STheodore Ts'o char b[BDEVNAME_SIZE]; 280bd2d0210STheodore Ts'o printk(KERN_ERR "Buffer I/O error on device %s, logical block %llu\n", 281bd2d0210STheodore Ts'o bdevname(bh->b_bdev, b), 282bd2d0210STheodore Ts'o (unsigned long long)bh->b_blocknr); 283bd2d0210STheodore Ts'o } 284bd2d0210STheodore Ts'o 285bd2d0210STheodore Ts'o static void ext4_end_bio(struct bio *bio, int error) 286bd2d0210STheodore Ts'o { 287bd2d0210STheodore Ts'o ext4_io_end_t *io_end = bio->bi_private; 288bd2d0210STheodore Ts'o struct inode *inode; 289bd2d0210STheodore Ts'o int i; 2900058f965SJan Kara int blocksize; 291d50bdd5aSCurt Wohlgemuth sector_t bi_sector = bio->bi_sector; 292bd2d0210STheodore Ts'o 293bd2d0210STheodore Ts'o BUG_ON(!io_end); 2940058f965SJan Kara inode = io_end->inode; 2950058f965SJan Kara blocksize = 1 << inode->i_blkbits; 296bd2d0210STheodore Ts'o bio->bi_private = NULL; 297bd2d0210STheodore Ts'o bio->bi_end_io = NULL; 298bd2d0210STheodore Ts'o if (test_bit(BIO_UPTODATE, &bio->bi_flags)) 299bd2d0210STheodore Ts'o error = 0; 3000058f965SJan Kara for (i = 0; i < bio->bi_vcnt; i++) { 3010058f965SJan Kara struct bio_vec *bvec = &bio->bi_io_vec[i]; 3020058f965SJan Kara struct page *page = bvec->bv_page; 303bd2d0210STheodore Ts'o struct buffer_head *bh, *head; 3040058f965SJan Kara unsigned bio_start = bvec->bv_offset; 3050058f965SJan Kara unsigned bio_end = bio_start + bvec->bv_len; 3060058f965SJan Kara unsigned under_io = 0; 3070058f965SJan Kara unsigned long flags; 3080058f965SJan Kara 3090058f965SJan Kara if (!page) 3100058f965SJan Kara continue; 31139db00f1SCurt Wohlgemuth 31239db00f1SCurt Wohlgemuth if (error) { 31339db00f1SCurt Wohlgemuth SetPageError(page); 31439db00f1SCurt Wohlgemuth set_bit(AS_EIO, &page->mapping->flags); 3150058f965SJan Kara } 3160058f965SJan Kara bh = head = page_buffers(page); 3170058f965SJan Kara /* 3180058f965SJan Kara * We check all buffers in the page under BH_Uptodate_Lock 3190058f965SJan Kara * to avoid races with other end io clearing async_write flags 3200058f965SJan Kara */ 3210058f965SJan Kara local_irq_save(flags); 3220058f965SJan Kara bit_spin_lock(BH_Uptodate_Lock, &head->b_state); 323bd2d0210STheodore Ts'o do { 3240058f965SJan Kara if (bh_offset(bh) < bio_start || 3250058f965SJan Kara bh_offset(bh) + blocksize > bio_end) { 3260058f965SJan Kara if (buffer_async_write(bh)) 3270058f965SJan Kara under_io++; 3280058f965SJan Kara continue; 3290058f965SJan Kara } 3300058f965SJan Kara clear_buffer_async_write(bh); 3310058f965SJan Kara if (error) 332bd2d0210STheodore Ts'o buffer_io_error(bh); 3330058f965SJan Kara } while ((bh = bh->b_this_page) != head); 3340058f965SJan Kara bit_spin_unlock(BH_Uptodate_Lock, &head->b_state); 3350058f965SJan Kara local_irq_restore(flags); 3360058f965SJan Kara if (!under_io) 3370058f965SJan Kara end_page_writeback(page); 338bd2d0210STheodore Ts'o } 3390058f965SJan Kara bio_put(bio); 340f7ad6d2eSTheodore Ts'o 341f7ad6d2eSTheodore Ts'o if (error) { 342f7ad6d2eSTheodore Ts'o io_end->flag |= EXT4_IO_END_ERROR; 343f7ad6d2eSTheodore Ts'o ext4_warning(inode->i_sb, "I/O error writing to inode %lu " 344f7ad6d2eSTheodore Ts'o "(offset %llu size %ld starting block %llu)", 345f7ad6d2eSTheodore Ts'o inode->i_ino, 346f7ad6d2eSTheodore Ts'o (unsigned long long) io_end->offset, 347f7ad6d2eSTheodore Ts'o (long) io_end->size, 348f7ad6d2eSTheodore Ts'o (unsigned long long) 349d50bdd5aSCurt Wohlgemuth bi_sector >> (inode->i_blkbits - 9)); 350f7ad6d2eSTheodore Ts'o } 351bd2d0210STheodore Ts'o 35297a851edSJan Kara ext4_put_io_end_defer(io_end); 353bd2d0210STheodore Ts'o } 354bd2d0210STheodore Ts'o 355bd2d0210STheodore Ts'o void ext4_io_submit(struct ext4_io_submit *io) 356bd2d0210STheodore Ts'o { 357bd2d0210STheodore Ts'o struct bio *bio = io->io_bio; 358bd2d0210STheodore Ts'o 359bd2d0210STheodore Ts'o if (bio) { 360bd2d0210STheodore Ts'o bio_get(io->io_bio); 361bd2d0210STheodore Ts'o submit_bio(io->io_op, io->io_bio); 362bd2d0210STheodore Ts'o BUG_ON(bio_flagged(io->io_bio, BIO_EOPNOTSUPP)); 363bd2d0210STheodore Ts'o bio_put(io->io_bio); 364bd2d0210STheodore Ts'o } 3657dc57615SPeter Huewe io->io_bio = NULL; 36697a851edSJan Kara } 36797a851edSJan Kara 36897a851edSJan Kara void ext4_io_submit_init(struct ext4_io_submit *io, 36997a851edSJan Kara struct writeback_control *wbc) 37097a851edSJan Kara { 37197a851edSJan Kara io->io_op = (wbc->sync_mode == WB_SYNC_ALL ? WRITE_SYNC : WRITE); 37297a851edSJan Kara io->io_bio = NULL; 3737dc57615SPeter Huewe io->io_end = NULL; 374bd2d0210STheodore Ts'o } 375bd2d0210STheodore Ts'o 37697a851edSJan Kara static int io_submit_init_bio(struct ext4_io_submit *io, 377bd2d0210STheodore Ts'o struct buffer_head *bh) 378bd2d0210STheodore Ts'o { 379bd2d0210STheodore Ts'o int nvecs = bio_get_nr_vecs(bh->b_bdev); 380bd2d0210STheodore Ts'o struct bio *bio; 381bd2d0210STheodore Ts'o 382275d3ba6STheodore Ts'o bio = bio_alloc(GFP_NOIO, min(nvecs, BIO_MAX_PAGES)); 383bd2d0210STheodore Ts'o bio->bi_sector = bh->b_blocknr * (bh->b_size >> 9); 384bd2d0210STheodore Ts'o bio->bi_bdev = bh->b_bdev; 385bd2d0210STheodore Ts'o bio->bi_end_io = ext4_end_bio; 38697a851edSJan Kara bio->bi_private = ext4_get_io_end(io->io_end); 387bd2d0210STheodore Ts'o io->io_bio = bio; 388bd2d0210STheodore Ts'o io->io_next_block = bh->b_blocknr; 389bd2d0210STheodore Ts'o return 0; 390bd2d0210STheodore Ts'o } 391bd2d0210STheodore Ts'o 392bd2d0210STheodore Ts'o static int io_submit_add_bh(struct ext4_io_submit *io, 393bd2d0210STheodore Ts'o struct inode *inode, 394bd2d0210STheodore Ts'o struct buffer_head *bh) 395bd2d0210STheodore Ts'o { 396bd2d0210STheodore Ts'o int ret; 397bd2d0210STheodore Ts'o 398bd2d0210STheodore Ts'o if (io->io_bio && bh->b_blocknr != io->io_next_block) { 399bd2d0210STheodore Ts'o submit_and_retry: 400bd2d0210STheodore Ts'o ext4_io_submit(io); 401bd2d0210STheodore Ts'o } 402bd2d0210STheodore Ts'o if (io->io_bio == NULL) { 40397a851edSJan Kara ret = io_submit_init_bio(io, bh); 404bd2d0210STheodore Ts'o if (ret) 405bd2d0210STheodore Ts'o return ret; 406bd2d0210STheodore Ts'o } 407a549984bSTheodore Ts'o ret = bio_add_page(io->io_bio, bh->b_page, bh->b_size, bh_offset(bh)); 408a549984bSTheodore Ts'o if (ret != bh->b_size) 409a549984bSTheodore Ts'o goto submit_and_retry; 41097a851edSJan Kara io->io_next_block++; 411bd2d0210STheodore Ts'o return 0; 412bd2d0210STheodore Ts'o } 413bd2d0210STheodore Ts'o 414bd2d0210STheodore Ts'o int ext4_bio_write_page(struct ext4_io_submit *io, 415bd2d0210STheodore Ts'o struct page *page, 416bd2d0210STheodore Ts'o int len, 417bd2d0210STheodore Ts'o struct writeback_control *wbc) 418bd2d0210STheodore Ts'o { 419bd2d0210STheodore Ts'o struct inode *inode = page->mapping->host; 4200058f965SJan Kara unsigned block_start, blocksize; 421bd2d0210STheodore Ts'o struct buffer_head *bh, *head; 422bd2d0210STheodore Ts'o int ret = 0; 4230058f965SJan Kara int nr_submitted = 0; 424bd2d0210STheodore Ts'o 425bd2d0210STheodore Ts'o blocksize = 1 << inode->i_blkbits; 426bd2d0210STheodore Ts'o 427d50bdd5aSCurt Wohlgemuth BUG_ON(!PageLocked(page)); 428bd2d0210STheodore Ts'o BUG_ON(PageWriteback(page)); 429bd2d0210STheodore Ts'o 430a54aa761STheodore Ts'o set_page_writeback(page); 431a54aa761STheodore Ts'o ClearPageError(page); 432bd2d0210STheodore Ts'o 4330058f965SJan Kara /* 4340058f965SJan Kara * In the first loop we prepare and mark buffers to submit. We have to 4350058f965SJan Kara * mark all buffers in the page before submitting so that 4360058f965SJan Kara * end_page_writeback() cannot be called from ext4_bio_end_io() when IO 4370058f965SJan Kara * on the first buffer finishes and we are still working on submitting 4380058f965SJan Kara * the second buffer. 4390058f965SJan Kara */ 4400058f965SJan Kara bh = head = page_buffers(page); 4410058f965SJan Kara do { 4420058f965SJan Kara block_start = bh_offset(bh); 443bd2d0210STheodore Ts'o if (block_start >= len) { 4445a0dc736SYongqiang Yang /* 4455a0dc736SYongqiang Yang * Comments copied from block_write_full_page_endio: 4465a0dc736SYongqiang Yang * 4475a0dc736SYongqiang Yang * The page straddles i_size. It must be zeroed out on 4485a0dc736SYongqiang Yang * each and every writepage invocation because it may 4495a0dc736SYongqiang Yang * be mmapped. "A file is mapped in multiples of the 4505a0dc736SYongqiang Yang * page size. For a file that is not a multiple of 4515a0dc736SYongqiang Yang * the page size, the remaining memory is zeroed when 4525a0dc736SYongqiang Yang * mapped, and writes to that region are not written 4535a0dc736SYongqiang Yang * out to the file." 4545a0dc736SYongqiang Yang */ 4550058f965SJan Kara zero_user_segment(page, block_start, 4560058f965SJan Kara block_start + blocksize); 457bd2d0210STheodore Ts'o clear_buffer_dirty(bh); 458bd2d0210STheodore Ts'o set_buffer_uptodate(bh); 459bd2d0210STheodore Ts'o continue; 460bd2d0210STheodore Ts'o } 4618a850c3fSJan Kara if (!buffer_dirty(bh) || buffer_delay(bh) || 4628a850c3fSJan Kara !buffer_mapped(bh) || buffer_unwritten(bh)) { 4638a850c3fSJan Kara /* A hole? We can safely clear the dirty bit */ 4648a850c3fSJan Kara if (!buffer_mapped(bh)) 4658a850c3fSJan Kara clear_buffer_dirty(bh); 4668a850c3fSJan Kara if (io->io_bio) 4678a850c3fSJan Kara ext4_io_submit(io); 4688a850c3fSJan Kara continue; 4698a850c3fSJan Kara } 4700058f965SJan Kara if (buffer_new(bh)) { 4710058f965SJan Kara clear_buffer_new(bh); 4720058f965SJan Kara unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr); 4730058f965SJan Kara } 4740058f965SJan Kara set_buffer_async_write(bh); 4750058f965SJan Kara } while ((bh = bh->b_this_page) != head); 4760058f965SJan Kara 4770058f965SJan Kara /* Now submit buffers to write */ 4780058f965SJan Kara bh = head = page_buffers(page); 4790058f965SJan Kara do { 4800058f965SJan Kara if (!buffer_async_write(bh)) 4810058f965SJan Kara continue; 48297a851edSJan Kara ret = io_submit_add_bh(io, inode, bh); 483bd2d0210STheodore Ts'o if (ret) { 484bd2d0210STheodore Ts'o /* 485bd2d0210STheodore Ts'o * We only get here on ENOMEM. Not much else 486bd2d0210STheodore Ts'o * we can do but mark the page as dirty, and 487bd2d0210STheodore Ts'o * better luck next time. 488bd2d0210STheodore Ts'o */ 4891ae48a63SJan Kara redirty_page_for_writepage(wbc, page); 490bd2d0210STheodore Ts'o break; 491bd2d0210STheodore Ts'o } 4920058f965SJan Kara nr_submitted++; 4931ae48a63SJan Kara clear_buffer_dirty(bh); 4940058f965SJan Kara } while ((bh = bh->b_this_page) != head); 4950058f965SJan Kara 4960058f965SJan Kara /* Error stopped previous loop? Clean up buffers... */ 4970058f965SJan Kara if (ret) { 4980058f965SJan Kara do { 4990058f965SJan Kara clear_buffer_async_write(bh); 5000058f965SJan Kara bh = bh->b_this_page; 5010058f965SJan Kara } while (bh != head); 502bd2d0210STheodore Ts'o } 503bd2d0210STheodore Ts'o unlock_page(page); 5040058f965SJan Kara /* Nothing submitted - we have to end page writeback */ 5050058f965SJan Kara if (!nr_submitted) 5060058f965SJan Kara end_page_writeback(page); 507bd2d0210STheodore Ts'o return ret; 508bd2d0210STheodore Ts'o } 509