xfs: report iomap_bn in block base
[safe/jmp/linux-2.6] / fs / xfs / linux-2.6 / xfs_aops.c
index 8fbc97d..2b09cc3 100644 (file)
 #include "xfs_rw.h"
 #include "xfs_iomap.h"
 #include "xfs_vnodeops.h"
+#include "xfs_trace.h"
+#include "xfs_bmap.h"
+#include <linux/gfp.h>
 #include <linux/mpage.h>
 #include <linux/pagevec.h>
 #include <linux/writeback.h>
 
+
+/*
+ * Prime number of hash buckets since address is used as the key.
+ */
+#define NVSYNC         37
+#define to_ioend_wq(v) (&xfs_ioend_wq[((unsigned long)v) % NVSYNC])
+static wait_queue_head_t xfs_ioend_wq[NVSYNC];
+
+void __init
+xfs_ioend_init(void)
+{
+       int i;
+
+       for (i = 0; i < NVSYNC; i++)
+               init_waitqueue_head(&xfs_ioend_wq[i]);
+}
+
+void
+xfs_ioend_wait(
+       xfs_inode_t     *ip)
+{
+       wait_queue_head_t *wq = to_ioend_wq(ip);
+
+       wait_event(*wq, (atomic_read(&ip->i_iocount) == 0));
+}
+
 STATIC void
+xfs_ioend_wake(
+       xfs_inode_t     *ip)
+{
+       if (atomic_dec_and_test(&ip->i_iocount))
+               wake_up(to_ioend_wq(ip));
+}
+
+void
 xfs_count_page_state(
        struct page             *page,
        int                     *delalloc,
@@ -64,52 +101,11 @@ xfs_count_page_state(
        } while ((bh = bh->b_this_page) != head);
 }
 
-#if defined(XFS_RW_TRACE)
-void
-xfs_page_trace(
-       int             tag,
-       struct inode    *inode,
-       struct page     *page,
-       unsigned long   pgoff)
-{
-       xfs_inode_t     *ip;
-       loff_t          isize = i_size_read(inode);
-       loff_t          offset = page_offset(page);
-       int             delalloc = -1, unmapped = -1, unwritten = -1;
-
-       if (page_has_buffers(page))
-               xfs_count_page_state(page, &delalloc, &unmapped, &unwritten);
-
-       ip = XFS_I(inode);
-       if (!ip->i_rwtrace)
-               return;
-
-       ktrace_enter(ip->i_rwtrace,
-               (void *)((unsigned long)tag),
-               (void *)ip,
-               (void *)inode,
-               (void *)page,
-               (void *)pgoff,
-               (void *)((unsigned long)((ip->i_d.di_size >> 32) & 0xffffffff)),
-               (void *)((unsigned long)(ip->i_d.di_size & 0xffffffff)),
-               (void *)((unsigned long)((isize >> 32) & 0xffffffff)),
-               (void *)((unsigned long)(isize & 0xffffffff)),
-               (void *)((unsigned long)((offset >> 32) & 0xffffffff)),
-               (void *)((unsigned long)(offset & 0xffffffff)),
-               (void *)((unsigned long)delalloc),
-               (void *)((unsigned long)unmapped),
-               (void *)((unsigned long)unwritten),
-               (void *)((unsigned long)current_pid()),
-               (void *)NULL);
-}
-#else
-#define xfs_page_trace(tag, inode, page, pgoff)
-#endif
-
 STATIC struct block_device *
 xfs_find_bdev_for_inode(
-       struct xfs_inode        *ip)
+       struct inode            *inode)
 {
+       struct xfs_inode        *ip = XFS_I(inode);
        struct xfs_mount        *mp = ip->i_mount;
 
        if (XFS_IS_REALTIME_INODE(ip))
@@ -119,23 +115,6 @@ xfs_find_bdev_for_inode(
 }
 
 /*
- * Schedule IO completion handling on a xfsdatad if this was
- * the final hold on this ioend. If we are asked to wait,
- * flush the workqueue.
- */
-STATIC void
-xfs_finish_ioend(
-       xfs_ioend_t     *ioend,
-       int             wait)
-{
-       if (atomic_dec_and_test(&ioend->io_remaining)) {
-               queue_work(xfsdatad_workqueue, &ioend->io_work);
-               if (wait)
-                       flush_workqueue(xfsdatad_workqueue);
-       }
-}
-
-/*
  * We're now finished for good with this ioend structure.
  * Update the page state via the associated buffer_heads,
  * release holds on the inode and bio, and finally free
@@ -146,124 +125,149 @@ xfs_destroy_ioend(
        xfs_ioend_t             *ioend)
 {
        struct buffer_head      *bh, *next;
+       struct xfs_inode        *ip = XFS_I(ioend->io_inode);
 
        for (bh = ioend->io_buffer_head; bh; bh = next) {
                next = bh->b_private;
                bh->b_end_io(bh, !ioend->io_error);
        }
-       if (unlikely(ioend->io_error)) {
-               vn_ioerror(XFS_I(ioend->io_inode), ioend->io_error,
-                               __FILE__,__LINE__);
+
+       /*
+        * Volume managers supporting multiple paths can send back ENODEV
+        * when the final path disappears.  In this case continuing to fill
+        * the page cache with dirty data which cannot be written out is
+        * evil, so prevent that.
+        */
+       if (unlikely(ioend->io_error == -ENODEV)) {
+               xfs_do_force_shutdown(ip->i_mount, SHUTDOWN_DEVICE_REQ,
+                                     __FILE__, __LINE__);
        }
-       vn_iowake(XFS_I(ioend->io_inode));
+
+       xfs_ioend_wake(ip);
        mempool_free(ioend, xfs_ioend_pool);
 }
 
 /*
- * Update on-disk file size now that data has been written to disk.
- * The current in-memory file size is i_size.  If a write is beyond
- * eof i_new_size will be the intended file size until i_size is
- * updated.  If this write does not extend all the way to the valid
- * file size then restrict this update to the end of the write.
+ * If the end of the current ioend is beyond the current EOF,
+ * return the new EOF value, otherwise zero.
  */
-STATIC void
-xfs_setfilesize(
+STATIC xfs_fsize_t
+xfs_ioend_new_eof(
        xfs_ioend_t             *ioend)
 {
        xfs_inode_t             *ip = XFS_I(ioend->io_inode);
        xfs_fsize_t             isize;
        xfs_fsize_t             bsize;
 
+       bsize = ioend->io_offset + ioend->io_size;
+       isize = MAX(ip->i_size, ip->i_new_size);
+       isize = MIN(isize, bsize);
+       return isize > ip->i_d.di_size ? isize : 0;
+}
+
+/*
+ * Update on-disk file size now that data has been written to disk.  The
+ * current in-memory file size is i_size.  If a write is beyond eof i_new_size
+ * will be the intended file size until i_size is updated.  If this write does
+ * not extend all the way to the valid file size then restrict this update to
+ * the end of the write.
+ *
+ * This function does not block as blocking on the inode lock in IO completion
+ * can lead to IO completion order dependency deadlocks.. If it can't get the
+ * inode ilock it will return EAGAIN. Callers must handle this.
+ */
+STATIC int
+xfs_setfilesize(
+       xfs_ioend_t             *ioend)
+{
+       xfs_inode_t             *ip = XFS_I(ioend->io_inode);
+       xfs_fsize_t             isize;
+
        ASSERT((ip->i_d.di_mode & S_IFMT) == S_IFREG);
        ASSERT(ioend->io_type != IOMAP_READ);
 
        if (unlikely(ioend->io_error))
-               return;
-
-       bsize = ioend->io_offset + ioend->io_size;
-
-       xfs_ilock(ip, XFS_ILOCK_EXCL);
+               return 0;
 
-       isize = MAX(ip->i_size, ip->i_new_size);
-       isize = MIN(isize, bsize);
+       if (!xfs_ilock_nowait(ip, XFS_ILOCK_EXCL))
+               return EAGAIN;
 
-       if (ip->i_d.di_size < isize) {
+       isize = xfs_ioend_new_eof(ioend);
+       if (isize) {
                ip->i_d.di_size = isize;
-               ip->i_update_core = 1;
-               ip->i_update_size = 1;
-               xfs_mark_inode_dirty_sync(ip);
+               xfs_mark_inode_dirty(ip);
        }
 
        xfs_iunlock(ip, XFS_ILOCK_EXCL);
+       return 0;
 }
 
 /*
- * Buffered IO write completion for delayed allocate extents.
+ * Schedule IO completion handling on a xfsdatad if this was
+ * the final hold on this ioend. If we are asked to wait,
+ * flush the workqueue.
  */
 STATIC void
-xfs_end_bio_delalloc(
-       struct work_struct      *work)
+xfs_finish_ioend(
+       xfs_ioend_t     *ioend,
+       int             wait)
 {
-       xfs_ioend_t             *ioend =
-               container_of(work, xfs_ioend_t, io_work);
+       if (atomic_dec_and_test(&ioend->io_remaining)) {
+               struct workqueue_struct *wq;
 
-       xfs_setfilesize(ioend);
-       xfs_destroy_ioend(ioend);
+               wq = (ioend->io_type == IOMAP_UNWRITTEN) ?
+                       xfsconvertd_workqueue : xfsdatad_workqueue;
+               queue_work(wq, &ioend->io_work);
+               if (wait)
+                       flush_workqueue(wq);
+       }
 }
 
 /*
- * Buffered IO write completion for regular, written extents.
+ * IO write completion.
  */
 STATIC void
-xfs_end_bio_written(
-       struct work_struct      *work)
+xfs_end_io(
+       struct work_struct *work)
 {
-       xfs_ioend_t             *ioend =
-               container_of(work, xfs_ioend_t, io_work);
+       xfs_ioend_t     *ioend = container_of(work, xfs_ioend_t, io_work);
+       struct xfs_inode *ip = XFS_I(ioend->io_inode);
+       int             error = 0;
 
-       xfs_setfilesize(ioend);
-       xfs_destroy_ioend(ioend);
-}
+       /*
+        * For unwritten extents we need to issue transactions to convert a
+        * range to normal written extens after the data I/O has finished.
+        */
+       if (ioend->io_type == IOMAP_UNWRITTEN &&
+           likely(!ioend->io_error && !XFS_FORCED_SHUTDOWN(ip->i_mount))) {
 
-/*
- * IO write completion for unwritten extents.
- *
- * Issue transactions to convert a buffer range from unwritten
- * to written extents.
- */
-STATIC void
-xfs_end_bio_unwritten(
-       struct work_struct      *work)
-{
-       xfs_ioend_t             *ioend =
-               container_of(work, xfs_ioend_t, io_work);
-       struct xfs_inode        *ip = XFS_I(ioend->io_inode);
-       xfs_off_t               offset = ioend->io_offset;
-       size_t                  size = ioend->io_size;
-
-       if (likely(!ioend->io_error)) {
-               if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
-                       int error;
-                       error = xfs_iomap_write_unwritten(ip, offset, size);
-                       if (error)
-                               ioend->io_error = error;
-               }
-               xfs_setfilesize(ioend);
+               error = xfs_iomap_write_unwritten(ip, ioend->io_offset,
+                                                ioend->io_size);
+               if (error)
+                       ioend->io_error = error;
        }
-       xfs_destroy_ioend(ioend);
-}
 
-/*
- * IO read completion for regular, written extents.
- */
-STATIC void
-xfs_end_bio_read(
-       struct work_struct      *work)
-{
-       xfs_ioend_t             *ioend =
-               container_of(work, xfs_ioend_t, io_work);
+       /*
+        * We might have to update the on-disk file size after extending
+        * writes.
+        */
+       if (ioend->io_type != IOMAP_READ) {
+               error = xfs_setfilesize(ioend);
+               ASSERT(!error || error == EAGAIN);
+       }
 
-       xfs_destroy_ioend(ioend);
+       /*
+        * If we didn't complete processing of the ioend, requeue it to the
+        * tail of the workqueue for another attempt later. Otherwise destroy
+        * it.
+        */
+       if (error == EAGAIN) {
+               atomic_inc(&ioend->io_remaining);
+               xfs_finish_ioend(ioend, 0);
+               /* ensure we don't spin on blocked ioends */
+               delay(1);
+       } else
+               xfs_destroy_ioend(ioend);
 }
 
 /*
@@ -297,15 +301,7 @@ xfs_alloc_ioend(
        ioend->io_offset = 0;
        ioend->io_size = 0;
 
-       if (type == IOMAP_UNWRITTEN)
-               INIT_WORK(&ioend->io_work, xfs_end_bio_unwritten);
-       else if (type == IOMAP_DELAY)
-               INIT_WORK(&ioend->io_work, xfs_end_bio_delalloc);
-       else if (type == IOMAP_READ)
-               INIT_WORK(&ioend->io_work, xfs_end_bio_read);
-       else
-               INIT_WORK(&ioend->io_work, xfs_end_bio_written);
-
+       INIT_WORK(&ioend->io_work, xfs_end_io);
        return ioend;
 }
 
@@ -317,23 +313,23 @@ xfs_map_blocks(
        xfs_iomap_t             *mapp,
        int                     flags)
 {
-       xfs_inode_t             *ip = XFS_I(inode);
-       int                     error, nmaps = 1;
-
-       error = xfs_iomap(ip, offset, count,
-                               flags, mapp, &nmaps);
-       if (!error && (flags & (BMAPI_WRITE|BMAPI_ALLOCATE)))
-               xfs_iflags_set(ip, XFS_IMODIFIED);
-       return -error;
+       int                     nmaps = 1;
+
+       return -xfs_iomap(XFS_I(inode), offset, count, flags, mapp, &nmaps);
 }
 
-STATIC_INLINE int
+STATIC int
 xfs_iomap_valid(
+       struct inode            *inode,
        xfs_iomap_t             *iomapp,
        loff_t                  offset)
 {
-       return offset >= iomapp->iomap_offset &&
-               offset < iomapp->iomap_offset + iomapp->iomap_bsize;
+       struct xfs_mount        *mp = XFS_I(inode)->i_mount;
+       xfs_off_t               iomap_offset = XFS_FSB_TO_B(mp, iomapp->iomap_offset);
+       xfs_off_t               iomap_bsize = XFS_FSB_TO_B(mp, iomapp->iomap_bsize);
+
+       return offset >= iomap_offset &&
+               offset < iomap_offset + iomap_bsize;
 }
 
 /*
@@ -359,15 +355,23 @@ xfs_end_bio(
 
 STATIC void
 xfs_submit_ioend_bio(
-       xfs_ioend_t     *ioend,
-       struct bio      *bio)
+       struct writeback_control *wbc,
+       xfs_ioend_t             *ioend,
+       struct bio              *bio)
 {
        atomic_inc(&ioend->io_remaining);
-
        bio->bi_private = ioend;
        bio->bi_end_io = xfs_end_bio;
 
-       submit_bio(WRITE, bio);
+       /*
+        * If the I/O is beyond EOF we mark the inode dirty immediately
+        * but don't update the inode size until I/O completion.
+        */
+       if (xfs_ioend_new_eof(ioend))
+               xfs_mark_inode_dirty(XFS_I(ioend->io_inode));
+
+       submit_bio(wbc->sync_mode == WB_SYNC_ALL ?
+                  WRITE_SYNC_PLUG : WRITE, bio);
        ASSERT(!bio_flagged(bio, BIO_EOPNOTSUPP));
        bio_put(bio);
 }
@@ -446,6 +450,7 @@ static inline int bio_add_buffer(struct bio *bio, struct buffer_head *bh)
  */
 STATIC void
 xfs_submit_ioend(
+       struct writeback_control *wbc,
        xfs_ioend_t             *ioend)
 {
        xfs_ioend_t             *head = ioend;
@@ -474,19 +479,19 @@ xfs_submit_ioend(
  retry:
                                bio = xfs_alloc_ioend_bio(bh);
                        } else if (bh->b_blocknr != lastblock + 1) {
-                               xfs_submit_ioend_bio(ioend, bio);
+                               xfs_submit_ioend_bio(wbc, ioend, bio);
                                goto retry;
                        }
 
                        if (bio_add_buffer(bio, bh) != bh->b_size) {
-                               xfs_submit_ioend_bio(ioend, bio);
+                               xfs_submit_ioend_bio(wbc, ioend, bio);
                                goto retry;
                        }
 
                        lastblock = bh->b_blocknr;
                }
                if (bio)
-                       xfs_submit_ioend_bio(ioend, bio);
+                       xfs_submit_ioend_bio(wbc, ioend, bio);
                xfs_finish_ioend(ioend, 0);
        } while ((ioend = next) != NULL);
 }
@@ -512,7 +517,7 @@ xfs_cancel_ioend(
                        unlock_buffer(bh);
                } while ((bh = next_bh) != NULL);
 
-               vn_iowake(XFS_I(ioend->io_inode));
+               xfs_ioend_wake(XFS_I(ioend->io_inode));
                mempool_free(ioend, xfs_ioend_pool);
        } while ((ioend = next) != NULL);
 }
@@ -555,19 +560,23 @@ xfs_add_to_ioend(
 
 STATIC void
 xfs_map_buffer(
+       struct inode            *inode,
        struct buffer_head      *bh,
        xfs_iomap_t             *mp,
-       xfs_off_t               offset,
-       uint                    block_bits)
+       xfs_off_t               offset)
 {
        sector_t                bn;
+       struct xfs_mount        *m = XFS_I(inode)->i_mount;
+       xfs_off_t               iomap_offset = XFS_FSB_TO_B(m, mp->iomap_offset);
+       xfs_daddr_t             iomap_bn = xfs_fsb_to_db(XFS_I(inode), mp->iomap_bn);
 
-       ASSERT(mp->iomap_bn != IOMAP_DADDR_NULL);
+       ASSERT(mp->iomap_bn != HOLESTARTBLOCK);
+       ASSERT(mp->iomap_bn != DELAYSTARTBLOCK);
 
-       bn = (mp->iomap_bn >> (block_bits - BBSHIFT)) +
-             ((offset - mp->iomap_offset) >> block_bits);
+       bn = (iomap_bn >> (inode->i_blkbits - BBSHIFT)) +
+             ((offset - iomap_offset) >> inode->i_blkbits);
 
-       ASSERT(bn || (mp->iomap_flags & IOMAP_REALTIME));
+       ASSERT(bn || XFS_IS_REALTIME_INODE(XFS_I(inode)));
 
        bh->b_blocknr = bn;
        set_buffer_mapped(bh);
@@ -575,17 +584,17 @@ xfs_map_buffer(
 
 STATIC void
 xfs_map_at_offset(
+       struct inode            *inode,
        struct buffer_head      *bh,
-       loff_t                  offset,
-       int                     block_bits,
-       xfs_iomap_t             *iomapp)
+       xfs_iomap_t             *iomapp,
+       xfs_off_t               offset)
 {
-       ASSERT(!(iomapp->iomap_flags & IOMAP_HOLE));
-       ASSERT(!(iomapp->iomap_flags & IOMAP_DELAY));
+       ASSERT(iomapp->iomap_bn != HOLESTARTBLOCK);
+       ASSERT(iomapp->iomap_bn != DELAYSTARTBLOCK);
 
        lock_buffer(bh);
-       xfs_map_buffer(bh, iomapp, offset, block_bits);
-       bh->b_bdev = iomapp->iomap_target->bt_bdev;
+       xfs_map_buffer(inode, bh, iomapp, offset);
+       bh->b_bdev = xfs_find_bdev_for_inode(inode);
        set_buffer_mapped(bh);
        clear_buffer_delay(bh);
        clear_buffer_unwritten(bh);
@@ -751,7 +760,6 @@ xfs_convert_page(
        xfs_off_t               end_offset;
        unsigned long           p_offset;
        unsigned int            type;
-       int                     bbits = inode->i_blkbits;
        int                     len, page_dirty;
        int                     count = 0, done = 0, uptodate = 1;
        xfs_off_t               offset = page_offset(page);
@@ -807,15 +815,15 @@ xfs_convert_page(
                        else
                                type = IOMAP_DELAY;
 
-                       if (!xfs_iomap_valid(mp, offset)) {
+                       if (!xfs_iomap_valid(inode, mp, offset)) {
                                done = 1;
                                continue;
                        }
 
-                       ASSERT(!(mp->iomap_flags & IOMAP_HOLE));
-                       ASSERT(!(mp->iomap_flags & IOMAP_DELAY));
+                       ASSERT(mp->iomap_bn != HOLESTARTBLOCK);
+                       ASSERT(mp->iomap_bn != DELAYSTARTBLOCK);
 
-                       xfs_map_at_offset(bh, offset, bbits, mp);
+                       xfs_map_at_offset(inode, bh, mp, offset);
                        if (startio) {
                                xfs_add_to_ioend(inode, bh, offset,
                                                type, ioendp, done);
@@ -845,16 +853,9 @@ xfs_convert_page(
 
        if (startio) {
                if (count) {
-                       struct backing_dev_info *bdi;
-
-                       bdi = inode->i_mapping->backing_dev_info;
                        wbc->nr_to_write--;
-                       if (bdi_write_congested(bdi)) {
-                               wbc->encountered_congestion = 1;
+                       if (wbc->nr_to_write <= 0)
                                done = 1;
-                       } else if (wbc->nr_to_write <= 0) {
-                               done = 1;
-                       }
                }
                xfs_start_page_writeback(page, !page_dirty, count);
        }
@@ -903,6 +904,125 @@ xfs_cluster_write(
        }
 }
 
+STATIC void
+xfs_vm_invalidatepage(
+       struct page             *page,
+       unsigned long           offset)
+{
+       trace_xfs_invalidatepage(page->mapping->host, page, offset);
+       block_invalidatepage(page, offset);
+}
+
+/*
+ * If the page has delalloc buffers on it, we need to punch them out before we
+ * invalidate the page. If we don't, we leave a stale delalloc mapping on the
+ * inode that can trip a BUG() in xfs_get_blocks() later on if a direct IO read
+ * is done on that same region - the delalloc extent is returned when none is
+ * supposed to be there.
+ *
+ * We prevent this by truncating away the delalloc regions on the page before
+ * invalidating it. Because they are delalloc, we can do this without needing a
+ * transaction. Indeed - if we get ENOSPC errors, we have to be able to do this
+ * truncation without a transaction as there is no space left for block
+ * reservation (typically why we see a ENOSPC in writeback).
+ *
+ * This is not a performance critical path, so for now just do the punching a
+ * buffer head at a time.
+ */
+STATIC void
+xfs_aops_discard_page(
+       struct page             *page)
+{
+       struct inode            *inode = page->mapping->host;
+       struct xfs_inode        *ip = XFS_I(inode);
+       struct buffer_head      *bh, *head;
+       loff_t                  offset = page_offset(page);
+       ssize_t                 len = 1 << inode->i_blkbits;
+
+       if (!xfs_is_delayed_page(page, IOMAP_DELAY))
+               goto out_invalidate;
+
+       if (XFS_FORCED_SHUTDOWN(ip->i_mount))
+               goto out_invalidate;
+
+       xfs_fs_cmn_err(CE_ALERT, ip->i_mount,
+               "page discard on page %p, inode 0x%llx, offset %llu.",
+                       page, ip->i_ino, offset);
+
+       xfs_ilock(ip, XFS_ILOCK_EXCL);
+       bh = head = page_buffers(page);
+       do {
+               int             done;
+               xfs_fileoff_t   offset_fsb;
+               xfs_bmbt_irec_t imap;
+               int             nimaps = 1;
+               int             error;
+               xfs_fsblock_t   firstblock;
+               xfs_bmap_free_t flist;
+
+               if (!buffer_delay(bh))
+                       goto next_buffer;
+
+               offset_fsb = XFS_B_TO_FSBT(ip->i_mount, offset);
+
+               /*
+                * Map the range first and check that it is a delalloc extent
+                * before trying to unmap the range. Otherwise we will be
+                * trying to remove a real extent (which requires a
+                * transaction) or a hole, which is probably a bad idea...
+                */
+               error = xfs_bmapi(NULL, ip, offset_fsb, 1,
+                               XFS_BMAPI_ENTIRE,  NULL, 0, &imap,
+                               &nimaps, NULL, NULL);
+
+               if (error) {
+                       /* something screwed, just bail */
+                       if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
+                               xfs_fs_cmn_err(CE_ALERT, ip->i_mount,
+                               "page discard failed delalloc mapping lookup.");
+                       }
+                       break;
+               }
+               if (!nimaps) {
+                       /* nothing there */
+                       goto next_buffer;
+               }
+               if (imap.br_startblock != DELAYSTARTBLOCK) {
+                       /* been converted, ignore */
+                       goto next_buffer;
+               }
+               WARN_ON(imap.br_blockcount == 0);
+
+               /*
+                * Note: while we initialise the firstblock/flist pair, they
+                * should never be used because blocks should never be
+                * allocated or freed for a delalloc extent and hence we need
+                * don't cancel or finish them after the xfs_bunmapi() call.
+                */
+               xfs_bmap_init(&flist, &firstblock);
+               error = xfs_bunmapi(NULL, ip, offset_fsb, 1, 0, 1, &firstblock,
+                                       &flist, NULL, &done);
+
+               ASSERT(!flist.xbf_count && !flist.xbf_first);
+               if (error) {
+                       /* something screwed, just bail */
+                       if (!XFS_FORCED_SHUTDOWN(ip->i_mount)) {
+                               xfs_fs_cmn_err(CE_ALERT, ip->i_mount,
+                       "page discard unable to remove delalloc mapping.");
+                       }
+                       break;
+               }
+next_buffer:
+               offset += len;
+
+       } while ((bh = bh->b_this_page) != head);
+
+       xfs_iunlock(ip, XFS_ILOCK_EXCL);
+out_invalidate:
+       xfs_vm_invalidatepage(page, 0);
+       return;
+}
+
 /*
  * Calling this without startio set means we are being asked to make a dirty
  * page ready for freeing it's buffers.  When called with startio set then
@@ -1005,7 +1125,7 @@ xfs_page_state_convert(
                }
 
                if (iomap_valid)
-                       iomap_valid = xfs_iomap_valid(&iomap, offset);
+                       iomap_valid = xfs_iomap_valid(inode, &iomap, offset);
 
                /*
                 * First case, map an unwritten extent and prepare for
@@ -1060,11 +1180,10 @@ xfs_page_state_convert(
                                                &iomap, flags);
                                if (err)
                                        goto error;
-                               iomap_valid = xfs_iomap_valid(&iomap, offset);
+                               iomap_valid = xfs_iomap_valid(inode, &iomap, offset);
                        }
                        if (iomap_valid) {
-                               xfs_map_at_offset(bh, offset,
-                                               inode->i_blkbits, &iomap);
+                               xfs_map_at_offset(inode, bh, &iomap, offset);
                                if (startio) {
                                        xfs_add_to_ioend(inode, bh, offset,
                                                        type, &ioend,
@@ -1091,7 +1210,7 @@ xfs_page_state_convert(
                                                &iomap, flags);
                                if (err)
                                        goto error;
-                               iomap_valid = xfs_iomap_valid(&iomap, offset);
+                               iomap_valid = xfs_iomap_valid(inode, &iomap, offset);
                        }
 
                        /*
@@ -1131,7 +1250,11 @@ xfs_page_state_convert(
                xfs_start_page_writeback(page, 1, count);
 
        if (ioend && iomap_valid) {
-               offset = (iomap.iomap_offset + iomap.iomap_bsize - 1) >>
+               struct xfs_mount        *m = XFS_I(inode)->i_mount;
+               xfs_off_t               iomap_offset = XFS_FSB_TO_B(m, iomap.iomap_offset);
+               xfs_off_t               iomap_bsize = XFS_FSB_TO_B(m, iomap.iomap_bsize);
+
+               offset = (iomap_offset + iomap_bsize - 1) >>
                                        PAGE_CACHE_SHIFT;
                tlast = min_t(pgoff_t, offset, last_index);
                xfs_cluster_write(inode, page->index + 1, &iomap, &ioend,
@@ -1139,7 +1262,7 @@ xfs_page_state_convert(
        }
 
        if (iohead)
-               xfs_submit_ioend(iohead);
+               xfs_submit_ioend(wbc, iohead);
 
        return page_dirty;
 
@@ -1154,7 +1277,7 @@ error:
         */
        if (err != -EAGAIN) {
                if (!unmapped)
-                       block_invalidatepage(page, 0);
+                       xfs_aops_discard_page(page);
                ClearPageUptodate(page);
        }
        return err;
@@ -1190,7 +1313,7 @@ xfs_vm_writepage(
        int                     delalloc, unmapped, unwritten;
        struct inode            *inode = page->mapping->host;
 
-       xfs_page_trace(XFS_WRITEPAGE_ENTER, inode, page, 0);
+       trace_xfs_writepage(inode, page, 0);
 
        /*
         * We need a transaction if:
@@ -1226,6 +1349,14 @@ xfs_vm_writepage(
        if (!page_has_buffers(page))
                create_empty_buffers(page, 1 << inode->i_blkbits, 0);
 
+
+       /*
+        *  VM calculation for nr_to_write seems off.  Bump it way
+        *  up, this gets simple streaming writes zippy again.
+        *  To be reviewed again after Jens' writeback changes.
+        */
+       wbc->nr_to_write *= 4;
+
        /*
         * Convert delayed allocate, unwritten or unmapped space
         * to real space and flush out to disk.
@@ -1287,7 +1418,7 @@ xfs_vm_releasepage(
                .nr_to_write = 1,
        };
 
-       xfs_page_trace(XFS_RELEASEPAGE_ENTER, inode, page, 0);
+       trace_xfs_releasepage(inode, page, 0);
 
        if (!page_has_buffers(page))
                return 0;
@@ -1349,15 +1480,14 @@ __xfs_get_blocks(
        if (niomap == 0)
                return 0;
 
-       if (iomap.iomap_bn != IOMAP_DADDR_NULL) {
+       if (iomap.iomap_bn != HOLESTARTBLOCK &&
+           iomap.iomap_bn != DELAYSTARTBLOCK) {
                /*
                 * For unwritten extents do not report a disk address on
                 * the read case (treat as if we're reading into a hole).
                 */
-               if (create || !(iomap.iomap_flags & IOMAP_UNWRITTEN)) {
-                       xfs_map_buffer(bh_result, &iomap, offset,
-                                      inode->i_blkbits);
-               }
+               if (create || !(iomap.iomap_flags & IOMAP_UNWRITTEN))
+                       xfs_map_buffer(inode, bh_result, &iomap, offset);
                if (create && (iomap.iomap_flags & IOMAP_UNWRITTEN)) {
                        if (direct)
                                bh_result->b_private = inode;
@@ -1369,7 +1499,7 @@ __xfs_get_blocks(
         * If this is a realtime file, data may be on a different device.
         * to that pointed to from the buffer_head b_bdev currently.
         */
-       bh_result->b_bdev = iomap.iomap_target->bt_bdev;
+       bh_result->b_bdev = xfs_find_bdev_for_inode(inode);
 
        /*
         * If we previously allocated a block out beyond eof and we are now
@@ -1386,7 +1516,7 @@ __xfs_get_blocks(
             (iomap.iomap_flags & (IOMAP_NEW|IOMAP_UNWRITTEN))))
                set_buffer_new(bh_result);
 
-       if (iomap.iomap_flags & IOMAP_DELAY) {
+       if (iomap.iomap_bn == DELAYSTARTBLOCK) {
                BUG_ON(direct);
                if (create) {
                        set_buffer_uptodate(bh_result);
@@ -1396,9 +1526,14 @@ __xfs_get_blocks(
        }
 
        if (direct || size > (1 << inode->i_blkbits)) {
-               ASSERT(iomap.iomap_bsize - iomap.iomap_delta > 0);
+               struct xfs_mount        *mp = XFS_I(inode)->i_mount;
+               xfs_off_t               iomap_offset = XFS_FSB_TO_B(mp, iomap.iomap_offset);
+               xfs_off_t               iomap_delta = offset - iomap_offset;
+               xfs_off_t               iomap_bsize = XFS_FSB_TO_B(mp, iomap.iomap_bsize);
+
+               ASSERT(iomap_bsize - iomap_delta > 0);
                offset = min_t(xfs_off_t,
-                               iomap.iomap_bsize - iomap.iomap_delta, size);
+                               iomap_bsize - iomap_delta, size);
                bh_result->b_size = (ssize_t)min_t(xfs_off_t, LONG_MAX, offset);
        }
 
@@ -1468,7 +1603,7 @@ xfs_end_io_direct(
                 * didn't map an unwritten extent so switch it's completion
                 * handler.
                 */
-               INIT_WORK(&ioend->io_work, xfs_end_bio_written);
+               ioend->io_type = IOMAP_NEW;
                xfs_finish_ioend(ioend, 0);
        }
 
@@ -1493,21 +1628,15 @@ xfs_vm_direct_IO(
        struct block_device *bdev;
        ssize_t         ret;
 
-       bdev = xfs_find_bdev_for_inode(XFS_I(inode));
+       bdev = xfs_find_bdev_for_inode(inode);
 
-       if (rw == WRITE) {
-               iocb->private = xfs_alloc_ioend(inode, IOMAP_UNWRITTEN);
-               ret = blockdev_direct_IO_own_locking(rw, iocb, inode,
-                       bdev, iov, offset, nr_segs,
-                       xfs_get_blocks_direct,
-                       xfs_end_io_direct);
-       } else {
-               iocb->private = xfs_alloc_ioend(inode, IOMAP_READ);
-               ret = blockdev_direct_IO_no_locking(rw, iocb, inode,
-                       bdev, iov, offset, nr_segs,
-                       xfs_get_blocks_direct,
-                       xfs_end_io_direct);
-       }
+       iocb->private = xfs_alloc_ioend(inode, rw == WRITE ?
+                                       IOMAP_UNWRITTEN : IOMAP_READ);
+
+       ret = blockdev_direct_IO_no_locking(rw, iocb, inode, bdev, iov,
+                                           offset, nr_segs,
+                                           xfs_get_blocks_direct,
+                                           xfs_end_io_direct);
 
        if (unlikely(ret != -EIOCBQUEUED && iocb->private))
                xfs_destroy_ioend(iocb->private);
@@ -1562,16 +1691,6 @@ xfs_vm_readpages(
        return mpage_readpages(mapping, pages, nr_pages, xfs_get_blocks);
 }
 
-STATIC void
-xfs_vm_invalidatepage(
-       struct page             *page,
-       unsigned long           offset)
-{
-       xfs_page_trace(XFS_INVALIDPAGE_ENTER,
-                       page->mapping->host, page, offset);
-       block_invalidatepage(page, offset);
-}
-
 const struct address_space_operations xfs_address_space_operations = {
        .readpage               = xfs_vm_readpage,
        .readpages              = xfs_vm_readpages,
@@ -1585,4 +1704,6 @@ const struct address_space_operations xfs_address_space_operations = {
        .bmap                   = xfs_vm_bmap,
        .direct_IO              = xfs_vm_direct_IO,
        .migratepage            = buffer_migrate_page,
+       .is_partially_uptodate  = block_is_partially_uptodate,
+       .error_remove_page      = generic_error_remove_page,
 };