afs: Provide a function to get a ref on a call
[linux-2.6-microblaze.git] / fs / iomap.c
index 64ce240..a3088fa 100644 (file)
@@ -142,13 +142,14 @@ static void
 iomap_adjust_read_range(struct inode *inode, struct iomap_page *iop,
                loff_t *pos, loff_t length, unsigned *offp, unsigned *lenp)
 {
+       loff_t orig_pos = *pos;
+       loff_t isize = i_size_read(inode);
        unsigned block_bits = inode->i_blkbits;
        unsigned block_size = (1 << block_bits);
        unsigned poff = offset_in_page(*pos);
        unsigned plen = min_t(loff_t, PAGE_SIZE - poff, length);
        unsigned first = poff >> block_bits;
        unsigned last = (poff + plen - 1) >> block_bits;
-       unsigned end = offset_in_page(i_size_read(inode)) >> block_bits;
 
        /*
         * If the block size is smaller than the page size we need to check the
@@ -183,8 +184,12 @@ iomap_adjust_read_range(struct inode *inode, struct iomap_page *iop,
         * handle both halves separately so that we properly zero data in the
         * page cache for blocks that are entirely outside of i_size.
         */
-       if (first <= end && last > end)
-               plen -= (last - end) * block_size;
+       if (orig_pos <= isize && orig_pos + length > isize) {
+               unsigned end = offset_in_page(isize - 1) >> block_bits;
+
+               if (first <= end && last > end)
+                       plen -= (last - end) * block_size;
+       }
 
        *offp = poff;
        *lenp = plen;
@@ -487,16 +492,29 @@ done:
 }
 EXPORT_SYMBOL_GPL(iomap_readpages);
 
+/*
+ * iomap_is_partially_uptodate checks whether blocks within a page are
+ * uptodate or not.
+ *
+ * Returns true if all blocks which correspond to a file portion
+ * we want to read within the page are uptodate.
+ */
 int
 iomap_is_partially_uptodate(struct page *page, unsigned long from,
                unsigned long count)
 {
        struct iomap_page *iop = to_iomap_page(page);
        struct inode *inode = page->mapping->host;
-       unsigned first = from >> inode->i_blkbits;
-       unsigned last = (from + count - 1) >> inode->i_blkbits;
+       unsigned len, first, last;
        unsigned i;
 
+       /* Limit range to one page */
+       len = min_t(unsigned, PAGE_SIZE - from, count);
+
+       /* First and last blocks in range within page */
+       first = from >> inode->i_blkbits;
+       last = (from + len - 1) >> inode->i_blkbits;
+
        if (iop) {
                for (i = first; i <= last; i++)
                        if (!test_bit(i, iop->uptodate))
@@ -545,7 +563,7 @@ iomap_migrate_page(struct address_space *mapping, struct page *newpage,
 {
        int ret;
 
-       ret = migrate_page_move_mapping(mapping, newpage, page, NULL, mode, 0);
+       ret = migrate_page_move_mapping(mapping, newpage, page, mode, 0);
        if (ret != MIGRATEPAGE_SUCCESS)
                return ret;
 
@@ -1525,7 +1543,7 @@ static void iomap_dio_bio_end_io(struct bio *bio)
                if (dio->wait_for_completion) {
                        struct task_struct *waiter = dio->submit.waiter;
                        WRITE_ONCE(dio->submit.waiter, NULL);
-                       wake_up_process(waiter);
+                       blk_wake_io_task(waiter);
                } else if (dio->flags & IOMAP_DIO_WRITE) {
                        struct inode *inode = file_inode(dio->iocb->ki_filp);
 
@@ -1553,6 +1571,7 @@ iomap_dio_zero(struct iomap_dio *dio, struct iomap *iomap, loff_t pos,
                unsigned len)
 {
        struct page *page = ZERO_PAGE(0);
+       int flags = REQ_SYNC | REQ_IDLE;
        struct bio *bio;
 
        bio = bio_alloc(GFP_KERNEL, 1);
@@ -1561,9 +1580,12 @@ iomap_dio_zero(struct iomap_dio *dio, struct iomap *iomap, loff_t pos,
        bio->bi_private = dio;
        bio->bi_end_io = iomap_dio_bio_end_io;
 
+       if (dio->iocb->ki_flags & IOCB_HIPRI)
+               flags |= REQ_HIPRI;
+
        get_page(page);
        __bio_add_page(bio, page, len, 0);
-       bio_set_op_attrs(bio, REQ_OP_WRITE, REQ_SYNC | REQ_IDLE);
+       bio_set_op_attrs(bio, REQ_OP_WRITE, flags);
 
        atomic_inc(&dio->ref);
        return submit_bio(bio);
@@ -1580,7 +1602,7 @@ iomap_dio_bio_actor(struct inode *inode, loff_t pos, loff_t length,
        struct bio *bio;
        bool need_zeroout = false;
        bool use_fua = false;
-       int nr_pages, ret;
+       int nr_pages, ret = 0;
        size_t copied = 0;
 
        if ((pos | length | align) & ((1 << blkbits) - 1))
@@ -1596,12 +1618,13 @@ iomap_dio_bio_actor(struct inode *inode, loff_t pos, loff_t length,
 
        if (iomap->flags & IOMAP_F_NEW) {
                need_zeroout = true;
-       } else {
+       } else if (iomap->type == IOMAP_MAPPED) {
                /*
-                * Use a FUA write if we need datasync semantics, this
-                * is a pure data IO that doesn't require any metadata
-                * updates and the underlying device supports FUA. This
-                * allows us to avoid cache flushes on IO completion.
+                * Use a FUA write if we need datasync semantics, this is a pure
+                * data IO that doesn't require any metadata updates (including
+                * after IO completion such as unwritten extent conversion) and
+                * the underlying device supports FUA. This allows us to avoid
+                * cache flushes on IO completion.
                 */
                if (!(iomap->flags & (IOMAP_F_SHARED|IOMAP_F_DIRTY)) &&
                    (dio->flags & IOMAP_DIO_WRITE_FUA) &&
@@ -1644,8 +1667,14 @@ iomap_dio_bio_actor(struct inode *inode, loff_t pos, loff_t length,
 
                ret = bio_iov_iter_get_pages(bio, &iter);
                if (unlikely(ret)) {
+                       /*
+                        * We have to stop part way through an IO. We must fall
+                        * through to the sub-block tail zeroing here, otherwise
+                        * this short IO may expose stale data in the tail of
+                        * the block we haven't written data to.
+                        */
                        bio_put(bio);
-                       return copied ? copied : ret;
+                       goto zero_tail;
                }
 
                n = bio->bi_iter.bi_size;
@@ -1662,6 +1691,9 @@ iomap_dio_bio_actor(struct inode *inode, loff_t pos, loff_t length,
                                bio_set_pages_dirty(bio);
                }
 
+               if (dio->iocb->ki_flags & IOCB_HIPRI)
+                       bio->bi_opf |= REQ_HIPRI;
+
                iov_iter_advance(dio->submit.iter, n);
 
                dio->size += n;
@@ -1676,13 +1708,21 @@ iomap_dio_bio_actor(struct inode *inode, loff_t pos, loff_t length,
                dio->submit.cookie = submit_bio(bio);
        } while (nr_pages);
 
-       if (need_zeroout) {
+       /*
+        * We need to zeroout the tail of a sub-block write if the extent type
+        * requires zeroing or the write extends beyond EOF. If we don't zero
+        * the block tail in the latter case, we can expose stale data via mmap
+        * reads of the EOF block.
+        */
+zero_tail:
+       if (need_zeroout ||
+           ((dio->flags & IOMAP_DIO_WRITE) && pos >= i_size_read(inode))) {
                /* zero out from the end of the write to the end of the block */
                pad = pos & (fs_block_size - 1);
                if (pad)
                        iomap_dio_zero(dio, iomap, pos, fs_block_size - pad);
        }
-       return copied;
+       return copied ? copied : ret;
 }
 
 static loff_t
@@ -1888,7 +1928,7 @@ iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
                        if (!(iocb->ki_flags & IOCB_HIPRI) ||
                            !dio->submit.last_queue ||
                            !blk_poll(dio->submit.last_queue,
-                                        dio->submit.cookie))
+                                        dio->submit.cookie, true))
                                io_schedule();
                }
                __set_current_state(TASK_RUNNING);