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/module.h> 10bd2d0210STheodore Ts'o #include <linux/fs.h> 11bd2d0210STheodore Ts'o #include <linux/time.h> 12bd2d0210STheodore Ts'o #include <linux/jbd2.h> 13bd2d0210STheodore Ts'o #include <linux/highuid.h> 14bd2d0210STheodore Ts'o #include <linux/pagemap.h> 15bd2d0210STheodore Ts'o #include <linux/quotaops.h> 16bd2d0210STheodore Ts'o #include <linux/string.h> 17bd2d0210STheodore Ts'o #include <linux/buffer_head.h> 18bd2d0210STheodore Ts'o #include <linux/writeback.h> 19bd2d0210STheodore Ts'o #include <linux/pagevec.h> 20bd2d0210STheodore Ts'o #include <linux/mpage.h> 21bd2d0210STheodore Ts'o #include <linux/namei.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> 27bd2d0210STheodore Ts'o 28bd2d0210STheodore Ts'o #include "ext4_jbd2.h" 29bd2d0210STheodore Ts'o #include "xattr.h" 30bd2d0210STheodore Ts'o #include "acl.h" 31bd2d0210STheodore Ts'o #include "ext4_extents.h" 32bd2d0210STheodore Ts'o 33bd2d0210STheodore Ts'o static struct kmem_cache *io_page_cachep, *io_end_cachep; 34bd2d0210STheodore Ts'o 355dabfc78STheodore Ts'o int __init ext4_init_pageio(void) 36bd2d0210STheodore Ts'o { 37bd2d0210STheodore Ts'o io_page_cachep = KMEM_CACHE(ext4_io_page, SLAB_RECLAIM_ACCOUNT); 38bd2d0210STheodore Ts'o if (io_page_cachep == NULL) 39bd2d0210STheodore Ts'o return -ENOMEM; 40bd2d0210STheodore Ts'o io_end_cachep = KMEM_CACHE(ext4_io_end, SLAB_RECLAIM_ACCOUNT); 4113195184SDan Carpenter if (io_end_cachep == NULL) { 42bd2d0210STheodore Ts'o kmem_cache_destroy(io_page_cachep); 43bd2d0210STheodore Ts'o return -ENOMEM; 44bd2d0210STheodore Ts'o } 45bd2d0210STheodore Ts'o return 0; 46bd2d0210STheodore Ts'o } 47bd2d0210STheodore Ts'o 485dabfc78STheodore Ts'o void ext4_exit_pageio(void) 49bd2d0210STheodore Ts'o { 50bd2d0210STheodore Ts'o kmem_cache_destroy(io_end_cachep); 51bd2d0210STheodore Ts'o kmem_cache_destroy(io_page_cachep); 52bd2d0210STheodore Ts'o } 53bd2d0210STheodore Ts'o 54f7ad6d2eSTheodore Ts'o void ext4_ioend_wait(struct inode *inode) 55f7ad6d2eSTheodore Ts'o { 56e9e3bcecSEric Sandeen wait_queue_head_t *wq = ext4_ioend_wq(inode); 57f7ad6d2eSTheodore Ts'o 58f7ad6d2eSTheodore Ts'o wait_event(*wq, (atomic_read(&EXT4_I(inode)->i_ioend_count) == 0)); 59f7ad6d2eSTheodore Ts'o } 60f7ad6d2eSTheodore Ts'o 6183668e71STheodore Ts'o static void put_io_page(struct ext4_io_page *io_page) 6283668e71STheodore Ts'o { 6383668e71STheodore Ts'o if (atomic_dec_and_test(&io_page->p_count)) { 6483668e71STheodore Ts'o end_page_writeback(io_page->p_page); 6583668e71STheodore Ts'o put_page(io_page->p_page); 6683668e71STheodore Ts'o kmem_cache_free(io_page_cachep, io_page); 6783668e71STheodore Ts'o } 6883668e71STheodore Ts'o } 6983668e71STheodore Ts'o 70bd2d0210STheodore Ts'o void ext4_free_io_end(ext4_io_end_t *io) 71bd2d0210STheodore Ts'o { 72bd2d0210STheodore Ts'o int i; 73f7ad6d2eSTheodore Ts'o wait_queue_head_t *wq; 74bd2d0210STheodore Ts'o 75bd2d0210STheodore Ts'o BUG_ON(!io); 76bd2d0210STheodore Ts'o if (io->page) 77bd2d0210STheodore Ts'o put_page(io->page); 7883668e71STheodore Ts'o for (i = 0; i < io->num_io_pages; i++) 7983668e71STheodore Ts'o put_io_page(io->pages[i]); 80bd2d0210STheodore Ts'o io->num_io_pages = 0; 81e9e3bcecSEric Sandeen wq = ext4_ioend_wq(io->inode); 82f7ad6d2eSTheodore Ts'o if (atomic_dec_and_test(&EXT4_I(io->inode)->i_ioend_count) && 83f7ad6d2eSTheodore Ts'o waitqueue_active(wq)) 84f7ad6d2eSTheodore Ts'o wake_up_all(wq); 85bd2d0210STheodore Ts'o kmem_cache_free(io_end_cachep, io); 86bd2d0210STheodore Ts'o } 87bd2d0210STheodore Ts'o 88bd2d0210STheodore Ts'o /* 89bd2d0210STheodore Ts'o * check a range of space and convert unwritten extents to written. 90bd2d0210STheodore Ts'o */ 91bd2d0210STheodore Ts'o int ext4_end_io_nolock(ext4_io_end_t *io) 92bd2d0210STheodore Ts'o { 93bd2d0210STheodore Ts'o struct inode *inode = io->inode; 94bd2d0210STheodore Ts'o loff_t offset = io->offset; 95bd2d0210STheodore Ts'o ssize_t size = io->size; 96e9e3bcecSEric Sandeen wait_queue_head_t *wq; 97bd2d0210STheodore Ts'o int ret = 0; 98bd2d0210STheodore Ts'o 99bd2d0210STheodore Ts'o ext4_debug("ext4_end_io_nolock: io 0x%p from inode %lu,list->next 0x%p," 100bd2d0210STheodore Ts'o "list->prev 0x%p\n", 101bd2d0210STheodore Ts'o io, inode->i_ino, io->list.next, io->list.prev); 102bd2d0210STheodore Ts'o 103bd2d0210STheodore Ts'o if (list_empty(&io->list)) 104bd2d0210STheodore Ts'o return ret; 105bd2d0210STheodore Ts'o 106bd2d0210STheodore Ts'o if (!(io->flag & EXT4_IO_END_UNWRITTEN)) 107bd2d0210STheodore Ts'o return ret; 108bd2d0210STheodore Ts'o 109bd2d0210STheodore Ts'o ret = ext4_convert_unwritten_extents(inode, offset, size); 110bd2d0210STheodore Ts'o if (ret < 0) { 111bd2d0210STheodore Ts'o printk(KERN_EMERG "%s: failed to convert unwritten " 112bd2d0210STheodore Ts'o "extents to written extents, error is %d " 113bd2d0210STheodore Ts'o "io is still on inode %lu aio dio list\n", 114bd2d0210STheodore Ts'o __func__, ret, inode->i_ino); 115bd2d0210STheodore Ts'o return ret; 116bd2d0210STheodore Ts'o } 117bd2d0210STheodore Ts'o 118bd2d0210STheodore Ts'o if (io->iocb) 119bd2d0210STheodore Ts'o aio_complete(io->iocb, io->result, 0); 120bd2d0210STheodore Ts'o /* clear the DIO AIO unwritten flag */ 121e9e3bcecSEric Sandeen if (io->flag & EXT4_IO_END_UNWRITTEN) { 122bd2d0210STheodore Ts'o io->flag &= ~EXT4_IO_END_UNWRITTEN; 123e9e3bcecSEric Sandeen /* Wake up anyone waiting on unwritten extent conversion */ 124e9e3bcecSEric Sandeen wq = ext4_ioend_wq(io->inode); 125e9e3bcecSEric Sandeen if (atomic_dec_and_test(&EXT4_I(inode)->i_aiodio_unwritten) && 126e9e3bcecSEric Sandeen waitqueue_active(wq)) { 127e9e3bcecSEric Sandeen wake_up_all(wq); 128e9e3bcecSEric Sandeen } 129e9e3bcecSEric Sandeen } 130e9e3bcecSEric Sandeen 131bd2d0210STheodore Ts'o return ret; 132bd2d0210STheodore Ts'o } 133bd2d0210STheodore Ts'o 134bd2d0210STheodore Ts'o /* 135bd2d0210STheodore Ts'o * work on completed aio dio IO, to convert unwritten extents to extents 136bd2d0210STheodore Ts'o */ 137bd2d0210STheodore Ts'o static void ext4_end_io_work(struct work_struct *work) 138bd2d0210STheodore Ts'o { 139bd2d0210STheodore Ts'o ext4_io_end_t *io = container_of(work, ext4_io_end_t, work); 140bd2d0210STheodore Ts'o struct inode *inode = io->inode; 141bd2d0210STheodore Ts'o struct ext4_inode_info *ei = EXT4_I(inode); 142bd2d0210STheodore Ts'o unsigned long flags; 143bd2d0210STheodore Ts'o int ret; 144bd2d0210STheodore Ts'o 145bd2d0210STheodore Ts'o mutex_lock(&inode->i_mutex); 146bd2d0210STheodore Ts'o ret = ext4_end_io_nolock(io); 147bd2d0210STheodore Ts'o if (ret < 0) { 148bd2d0210STheodore Ts'o mutex_unlock(&inode->i_mutex); 149bd2d0210STheodore Ts'o return; 150bd2d0210STheodore Ts'o } 151bd2d0210STheodore Ts'o 152bd2d0210STheodore Ts'o spin_lock_irqsave(&ei->i_completed_io_lock, flags); 153bd2d0210STheodore Ts'o if (!list_empty(&io->list)) 154bd2d0210STheodore Ts'o list_del_init(&io->list); 155bd2d0210STheodore Ts'o spin_unlock_irqrestore(&ei->i_completed_io_lock, flags); 156bd2d0210STheodore Ts'o mutex_unlock(&inode->i_mutex); 157bd2d0210STheodore Ts'o ext4_free_io_end(io); 158bd2d0210STheodore Ts'o } 159bd2d0210STheodore Ts'o 160bd2d0210STheodore Ts'o ext4_io_end_t *ext4_init_io_end(struct inode *inode, gfp_t flags) 161bd2d0210STheodore Ts'o { 162b17b35ecSJesper Juhl ext4_io_end_t *io = kmem_cache_zalloc(io_end_cachep, flags); 163bd2d0210STheodore Ts'o if (io) { 164f7ad6d2eSTheodore Ts'o atomic_inc(&EXT4_I(inode)->i_ioend_count); 165f7ad6d2eSTheodore Ts'o io->inode = inode; 166bd2d0210STheodore Ts'o INIT_WORK(&io->work, ext4_end_io_work); 167bd2d0210STheodore Ts'o INIT_LIST_HEAD(&io->list); 168bd2d0210STheodore Ts'o } 169bd2d0210STheodore Ts'o return io; 170bd2d0210STheodore Ts'o } 171bd2d0210STheodore Ts'o 172bd2d0210STheodore Ts'o /* 173bd2d0210STheodore Ts'o * Print an buffer I/O error compatible with the fs/buffer.c. This 174bd2d0210STheodore Ts'o * provides compatibility with dmesg scrapers that look for a specific 175bd2d0210STheodore Ts'o * buffer I/O error message. We really need a unified error reporting 176bd2d0210STheodore Ts'o * structure to userspace ala Digital Unix's uerf system, but it's 177bd2d0210STheodore Ts'o * probably not going to happen in my lifetime, due to LKML politics... 178bd2d0210STheodore Ts'o */ 179bd2d0210STheodore Ts'o static void buffer_io_error(struct buffer_head *bh) 180bd2d0210STheodore Ts'o { 181bd2d0210STheodore Ts'o char b[BDEVNAME_SIZE]; 182bd2d0210STheodore Ts'o printk(KERN_ERR "Buffer I/O error on device %s, logical block %llu\n", 183bd2d0210STheodore Ts'o bdevname(bh->b_bdev, b), 184bd2d0210STheodore Ts'o (unsigned long long)bh->b_blocknr); 185bd2d0210STheodore Ts'o } 186bd2d0210STheodore Ts'o 187bd2d0210STheodore Ts'o static void ext4_end_bio(struct bio *bio, int error) 188bd2d0210STheodore Ts'o { 189bd2d0210STheodore Ts'o ext4_io_end_t *io_end = bio->bi_private; 190bd2d0210STheodore Ts'o struct workqueue_struct *wq; 191bd2d0210STheodore Ts'o struct inode *inode; 192bd2d0210STheodore Ts'o unsigned long flags; 193bd2d0210STheodore Ts'o int i; 194d50bdd5aSCurt Wohlgemuth sector_t bi_sector = bio->bi_sector; 195bd2d0210STheodore Ts'o 196bd2d0210STheodore Ts'o BUG_ON(!io_end); 197bd2d0210STheodore Ts'o bio->bi_private = NULL; 198bd2d0210STheodore Ts'o bio->bi_end_io = NULL; 199bd2d0210STheodore Ts'o if (test_bit(BIO_UPTODATE, &bio->bi_flags)) 200bd2d0210STheodore Ts'o error = 0; 201bd2d0210STheodore Ts'o bio_put(bio); 202bd2d0210STheodore Ts'o 203bd2d0210STheodore Ts'o for (i = 0; i < io_end->num_io_pages; i++) { 204bd2d0210STheodore Ts'o struct page *page = io_end->pages[i]->p_page; 205bd2d0210STheodore Ts'o struct buffer_head *bh, *head; 206bd2d0210STheodore Ts'o int partial_write = 0; 207bd2d0210STheodore Ts'o 208bd2d0210STheodore Ts'o head = page_buffers(page); 209bd2d0210STheodore Ts'o if (error) 210bd2d0210STheodore Ts'o SetPageError(page); 211bd2d0210STheodore Ts'o BUG_ON(!head); 212d50bdd5aSCurt Wohlgemuth if (head->b_size != PAGE_CACHE_SIZE) { 213bd2d0210STheodore Ts'o loff_t offset; 214bd2d0210STheodore Ts'o loff_t io_end_offset = io_end->offset + io_end->size; 215bd2d0210STheodore Ts'o 216bd2d0210STheodore Ts'o offset = (sector_t) page->index << PAGE_CACHE_SHIFT; 217bd2d0210STheodore Ts'o bh = head; 218bd2d0210STheodore Ts'o do { 219bd2d0210STheodore Ts'o if ((offset >= io_end->offset) && 220bd2d0210STheodore Ts'o (offset+bh->b_size <= io_end_offset)) { 221bd2d0210STheodore Ts'o if (error) 222bd2d0210STheodore Ts'o buffer_io_error(bh); 223bd2d0210STheodore Ts'o 224bd2d0210STheodore Ts'o } 225bd2d0210STheodore Ts'o if (buffer_delay(bh)) 226bd2d0210STheodore Ts'o partial_write = 1; 227bd2d0210STheodore Ts'o else if (!buffer_mapped(bh)) 228bd2d0210STheodore Ts'o clear_buffer_dirty(bh); 229bd2d0210STheodore Ts'o else if (buffer_dirty(bh)) 230bd2d0210STheodore Ts'o partial_write = 1; 231bd2d0210STheodore Ts'o offset += bh->b_size; 232bd2d0210STheodore Ts'o bh = bh->b_this_page; 233bd2d0210STheodore Ts'o } while (bh != head); 234bd2d0210STheodore Ts'o } 235bd2d0210STheodore Ts'o 236bd2d0210STheodore Ts'o /* 237bd2d0210STheodore Ts'o * If this is a partial write which happened to make 238bd2d0210STheodore Ts'o * all buffers uptodate then we can optimize away a 239bd2d0210STheodore Ts'o * bogus readpage() for the next read(). Here we 240bd2d0210STheodore Ts'o * 'discover' whether the page went uptodate as a 241bd2d0210STheodore Ts'o * result of this (potentially partial) write. 242bd2d0210STheodore Ts'o */ 243bd2d0210STheodore Ts'o if (!partial_write) 244bd2d0210STheodore Ts'o SetPageUptodate(page); 24508da1193SMarkus Trippelsdorf 24608da1193SMarkus Trippelsdorf put_io_page(io_end->pages[i]); 247bd2d0210STheodore Ts'o } 248bd2d0210STheodore Ts'o io_end->num_io_pages = 0; 249f7ad6d2eSTheodore Ts'o inode = io_end->inode; 250f7ad6d2eSTheodore Ts'o 251f7ad6d2eSTheodore Ts'o if (error) { 252f7ad6d2eSTheodore Ts'o io_end->flag |= EXT4_IO_END_ERROR; 253f7ad6d2eSTheodore Ts'o ext4_warning(inode->i_sb, "I/O error writing to inode %lu " 254f7ad6d2eSTheodore Ts'o "(offset %llu size %ld starting block %llu)", 255f7ad6d2eSTheodore Ts'o inode->i_ino, 256f7ad6d2eSTheodore Ts'o (unsigned long long) io_end->offset, 257f7ad6d2eSTheodore Ts'o (long) io_end->size, 258f7ad6d2eSTheodore Ts'o (unsigned long long) 259d50bdd5aSCurt Wohlgemuth bi_sector >> (inode->i_blkbits - 9)); 260f7ad6d2eSTheodore Ts'o } 261bd2d0210STheodore Ts'o 262*b6168443STheodore Ts'o if (!(io_end->flag & EXT4_IO_END_UNWRITTEN)) { 263*b6168443STheodore Ts'o ext4_free_io_end(io_end); 264*b6168443STheodore Ts'o return; 265*b6168443STheodore Ts'o } 266*b6168443STheodore Ts'o 267bd2d0210STheodore Ts'o /* Add the io_end to per-inode completed io list*/ 268bd2d0210STheodore Ts'o spin_lock_irqsave(&EXT4_I(inode)->i_completed_io_lock, flags); 269bd2d0210STheodore Ts'o list_add_tail(&io_end->list, &EXT4_I(inode)->i_completed_io_list); 270bd2d0210STheodore Ts'o spin_unlock_irqrestore(&EXT4_I(inode)->i_completed_io_lock, flags); 271bd2d0210STheodore Ts'o 272bd2d0210STheodore Ts'o wq = EXT4_SB(inode->i_sb)->dio_unwritten_wq; 273bd2d0210STheodore Ts'o /* queue the work to convert unwritten extents to written */ 274bd2d0210STheodore Ts'o queue_work(wq, &io_end->work); 275bd2d0210STheodore Ts'o } 276bd2d0210STheodore Ts'o 277bd2d0210STheodore Ts'o void ext4_io_submit(struct ext4_io_submit *io) 278bd2d0210STheodore Ts'o { 279bd2d0210STheodore Ts'o struct bio *bio = io->io_bio; 280bd2d0210STheodore Ts'o 281bd2d0210STheodore Ts'o if (bio) { 282bd2d0210STheodore Ts'o bio_get(io->io_bio); 283bd2d0210STheodore Ts'o submit_bio(io->io_op, io->io_bio); 284bd2d0210STheodore Ts'o BUG_ON(bio_flagged(io->io_bio, BIO_EOPNOTSUPP)); 285bd2d0210STheodore Ts'o bio_put(io->io_bio); 286bd2d0210STheodore Ts'o } 2877dc57615SPeter Huewe io->io_bio = NULL; 288bd2d0210STheodore Ts'o io->io_op = 0; 2897dc57615SPeter Huewe io->io_end = NULL; 290bd2d0210STheodore Ts'o } 291bd2d0210STheodore Ts'o 292bd2d0210STheodore Ts'o static int io_submit_init(struct ext4_io_submit *io, 293bd2d0210STheodore Ts'o struct inode *inode, 294bd2d0210STheodore Ts'o struct writeback_control *wbc, 295bd2d0210STheodore Ts'o struct buffer_head *bh) 296bd2d0210STheodore Ts'o { 297bd2d0210STheodore Ts'o ext4_io_end_t *io_end; 298bd2d0210STheodore Ts'o struct page *page = bh->b_page; 299bd2d0210STheodore Ts'o int nvecs = bio_get_nr_vecs(bh->b_bdev); 300bd2d0210STheodore Ts'o struct bio *bio; 301bd2d0210STheodore Ts'o 302bd2d0210STheodore Ts'o io_end = ext4_init_io_end(inode, GFP_NOFS); 303bd2d0210STheodore Ts'o if (!io_end) 304bd2d0210STheodore Ts'o return -ENOMEM; 305bd2d0210STheodore Ts'o do { 306bd2d0210STheodore Ts'o bio = bio_alloc(GFP_NOIO, nvecs); 307bd2d0210STheodore Ts'o nvecs >>= 1; 308bd2d0210STheodore Ts'o } while (bio == NULL); 309bd2d0210STheodore Ts'o 310bd2d0210STheodore Ts'o bio->bi_sector = bh->b_blocknr * (bh->b_size >> 9); 311bd2d0210STheodore Ts'o bio->bi_bdev = bh->b_bdev; 312bd2d0210STheodore Ts'o bio->bi_private = io->io_end = io_end; 313bd2d0210STheodore Ts'o bio->bi_end_io = ext4_end_bio; 314bd2d0210STheodore Ts'o 315bd2d0210STheodore Ts'o io_end->offset = (page->index << PAGE_CACHE_SHIFT) + bh_offset(bh); 316bd2d0210STheodore Ts'o 317bd2d0210STheodore Ts'o io->io_bio = bio; 318bd2d0210STheodore Ts'o io->io_op = (wbc->sync_mode == WB_SYNC_ALL ? 319bd2d0210STheodore Ts'o WRITE_SYNC_PLUG : WRITE); 320bd2d0210STheodore Ts'o io->io_next_block = bh->b_blocknr; 321bd2d0210STheodore Ts'o return 0; 322bd2d0210STheodore Ts'o } 323bd2d0210STheodore Ts'o 324bd2d0210STheodore Ts'o static int io_submit_add_bh(struct ext4_io_submit *io, 325bd2d0210STheodore Ts'o struct ext4_io_page *io_page, 326bd2d0210STheodore Ts'o struct inode *inode, 327bd2d0210STheodore Ts'o struct writeback_control *wbc, 328bd2d0210STheodore Ts'o struct buffer_head *bh) 329bd2d0210STheodore Ts'o { 330bd2d0210STheodore Ts'o ext4_io_end_t *io_end; 331bd2d0210STheodore Ts'o int ret; 332bd2d0210STheodore Ts'o 333bd2d0210STheodore Ts'o if (buffer_new(bh)) { 334bd2d0210STheodore Ts'o clear_buffer_new(bh); 335bd2d0210STheodore Ts'o unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr); 336bd2d0210STheodore Ts'o } 337bd2d0210STheodore Ts'o 338bd2d0210STheodore Ts'o if (!buffer_mapped(bh) || buffer_delay(bh)) { 339bd2d0210STheodore Ts'o if (!buffer_mapped(bh)) 340bd2d0210STheodore Ts'o clear_buffer_dirty(bh); 341bd2d0210STheodore Ts'o if (io->io_bio) 342bd2d0210STheodore Ts'o ext4_io_submit(io); 343bd2d0210STheodore Ts'o return 0; 344bd2d0210STheodore Ts'o } 345bd2d0210STheodore Ts'o 346bd2d0210STheodore Ts'o if (io->io_bio && bh->b_blocknr != io->io_next_block) { 347bd2d0210STheodore Ts'o submit_and_retry: 348bd2d0210STheodore Ts'o ext4_io_submit(io); 349bd2d0210STheodore Ts'o } 350bd2d0210STheodore Ts'o if (io->io_bio == NULL) { 351bd2d0210STheodore Ts'o ret = io_submit_init(io, inode, wbc, bh); 352bd2d0210STheodore Ts'o if (ret) 353bd2d0210STheodore Ts'o return ret; 354bd2d0210STheodore Ts'o } 355bd2d0210STheodore Ts'o io_end = io->io_end; 356bd2d0210STheodore Ts'o if ((io_end->num_io_pages >= MAX_IO_PAGES) && 357bd2d0210STheodore Ts'o (io_end->pages[io_end->num_io_pages-1] != io_page)) 358bd2d0210STheodore Ts'o goto submit_and_retry; 359bd2d0210STheodore Ts'o if (buffer_uninit(bh)) 360bd2d0210STheodore Ts'o io->io_end->flag |= EXT4_IO_END_UNWRITTEN; 361bd2d0210STheodore Ts'o io->io_end->size += bh->b_size; 362bd2d0210STheodore Ts'o io->io_next_block++; 363bd2d0210STheodore Ts'o ret = bio_add_page(io->io_bio, bh->b_page, bh->b_size, bh_offset(bh)); 364bd2d0210STheodore Ts'o if (ret != bh->b_size) 365bd2d0210STheodore Ts'o goto submit_and_retry; 366bd2d0210STheodore Ts'o if ((io_end->num_io_pages == 0) || 367bd2d0210STheodore Ts'o (io_end->pages[io_end->num_io_pages-1] != io_page)) { 368bd2d0210STheodore Ts'o io_end->pages[io_end->num_io_pages++] = io_page; 36983668e71STheodore Ts'o atomic_inc(&io_page->p_count); 370bd2d0210STheodore Ts'o } 371bd2d0210STheodore Ts'o return 0; 372bd2d0210STheodore Ts'o } 373bd2d0210STheodore Ts'o 374bd2d0210STheodore Ts'o int ext4_bio_write_page(struct ext4_io_submit *io, 375bd2d0210STheodore Ts'o struct page *page, 376bd2d0210STheodore Ts'o int len, 377bd2d0210STheodore Ts'o struct writeback_control *wbc) 378bd2d0210STheodore Ts'o { 379bd2d0210STheodore Ts'o struct inode *inode = page->mapping->host; 380bd2d0210STheodore Ts'o unsigned block_start, block_end, blocksize; 381bd2d0210STheodore Ts'o struct ext4_io_page *io_page; 382bd2d0210STheodore Ts'o struct buffer_head *bh, *head; 383bd2d0210STheodore Ts'o int ret = 0; 384bd2d0210STheodore Ts'o 385bd2d0210STheodore Ts'o blocksize = 1 << inode->i_blkbits; 386bd2d0210STheodore Ts'o 387d50bdd5aSCurt Wohlgemuth BUG_ON(!PageLocked(page)); 388bd2d0210STheodore Ts'o BUG_ON(PageWriteback(page)); 389bd2d0210STheodore Ts'o 390bd2d0210STheodore Ts'o io_page = kmem_cache_alloc(io_page_cachep, GFP_NOFS); 391bd2d0210STheodore Ts'o if (!io_page) { 392bd2d0210STheodore Ts'o set_page_dirty(page); 393bd2d0210STheodore Ts'o unlock_page(page); 394bd2d0210STheodore Ts'o return -ENOMEM; 395bd2d0210STheodore Ts'o } 396bd2d0210STheodore Ts'o io_page->p_page = page; 39783668e71STheodore Ts'o atomic_set(&io_page->p_count, 1); 398bd2d0210STheodore Ts'o get_page(page); 399a54aa761STheodore Ts'o set_page_writeback(page); 400a54aa761STheodore Ts'o ClearPageError(page); 401bd2d0210STheodore Ts'o 402bd2d0210STheodore Ts'o for (bh = head = page_buffers(page), block_start = 0; 403bd2d0210STheodore Ts'o bh != head || !block_start; 404bd2d0210STheodore Ts'o block_start = block_end, bh = bh->b_this_page) { 405d50bdd5aSCurt Wohlgemuth 406bd2d0210STheodore Ts'o block_end = block_start + blocksize; 407bd2d0210STheodore Ts'o if (block_start >= len) { 408bd2d0210STheodore Ts'o clear_buffer_dirty(bh); 409bd2d0210STheodore Ts'o set_buffer_uptodate(bh); 410bd2d0210STheodore Ts'o continue; 411bd2d0210STheodore Ts'o } 412d50bdd5aSCurt Wohlgemuth clear_buffer_dirty(bh); 413bd2d0210STheodore Ts'o ret = io_submit_add_bh(io, io_page, inode, wbc, bh); 414bd2d0210STheodore Ts'o if (ret) { 415bd2d0210STheodore Ts'o /* 416bd2d0210STheodore Ts'o * We only get here on ENOMEM. Not much else 417bd2d0210STheodore Ts'o * we can do but mark the page as dirty, and 418bd2d0210STheodore Ts'o * better luck next time. 419bd2d0210STheodore Ts'o */ 420bd2d0210STheodore Ts'o set_page_dirty(page); 421bd2d0210STheodore Ts'o break; 422bd2d0210STheodore Ts'o } 423bd2d0210STheodore Ts'o } 424bd2d0210STheodore Ts'o unlock_page(page); 425bd2d0210STheodore Ts'o /* 426bd2d0210STheodore Ts'o * If the page was truncated before we could do the writeback, 427bd2d0210STheodore Ts'o * or we had a memory allocation error while trying to write 428bd2d0210STheodore Ts'o * the first buffer head, we won't have submitted any pages for 429bd2d0210STheodore Ts'o * I/O. In that case we need to make sure we've cleared the 430bd2d0210STheodore Ts'o * PageWriteback bit from the page to prevent the system from 431bd2d0210STheodore Ts'o * wedging later on. 432bd2d0210STheodore Ts'o */ 43383668e71STheodore Ts'o put_io_page(io_page); 434bd2d0210STheodore Ts'o return ret; 435bd2d0210STheodore Ts'o } 436