LCOV - code coverage report
Current view: top level - fs/iomap - direct-io.c (source / functions) Hit Total Coverage
Test: combined.info Lines: 0 258 0.0 %
Date: 2022-03-28 16:04:14 Functions: 0 8 0.0 %
Branches: 0 189 0.0 %

           Branch data     Line data    Source code
       1                 :            : // SPDX-License-Identifier: GPL-2.0
       2                 :            : /*
       3                 :            :  * Copyright (C) 2010 Red Hat, Inc.
       4                 :            :  * Copyright (c) 2016-2018 Christoph Hellwig.
       5                 :            :  */
       6                 :            : #include <linux/module.h>
       7                 :            : #include <linux/compiler.h>
       8                 :            : #include <linux/fs.h>
       9                 :            : #include <linux/iomap.h>
      10                 :            : #include <linux/backing-dev.h>
      11                 :            : #include <linux/uio.h>
      12                 :            : #include <linux/task_io_accounting_ops.h>
      13                 :            : 
      14                 :            : #include "../internal.h"
      15                 :            : 
      16                 :            : /*
      17                 :            :  * Private flags for iomap_dio, must not overlap with the public ones in
      18                 :            :  * iomap.h:
      19                 :            :  */
      20                 :            : #define IOMAP_DIO_WRITE_FUA     (1 << 28)
      21                 :            : #define IOMAP_DIO_NEED_SYNC     (1 << 29)
      22                 :            : #define IOMAP_DIO_WRITE         (1 << 30)
      23                 :            : #define IOMAP_DIO_DIRTY         (1 << 31)
      24                 :            : 
      25                 :            : struct iomap_dio {
      26                 :            :         struct kiocb            *iocb;
      27                 :            :         const struct iomap_dio_ops *dops;
      28                 :            :         loff_t                  i_size;
      29                 :            :         loff_t                  size;
      30                 :            :         atomic_t                ref;
      31                 :            :         unsigned                flags;
      32                 :            :         int                     error;
      33                 :            :         bool                    wait_for_completion;
      34                 :            : 
      35                 :            :         union {
      36                 :            :                 /* used during submission and for synchronous completion: */
      37                 :            :                 struct {
      38                 :            :                         struct iov_iter         *iter;
      39                 :            :                         struct task_struct      *waiter;
      40                 :            :                         struct request_queue    *last_queue;
      41                 :            :                         blk_qc_t                cookie;
      42                 :            :                 } submit;
      43                 :            : 
      44                 :            :                 /* used for aio completion: */
      45                 :            :                 struct {
      46                 :            :                         struct work_struct      work;
      47                 :            :                 } aio;
      48                 :            :         };
      49                 :            : };
      50                 :            : 
      51                 :          0 : int iomap_dio_iopoll(struct kiocb *kiocb, bool spin)
      52                 :            : {
      53         [ #  # ]:          0 :         struct request_queue *q = READ_ONCE(kiocb->private);
      54                 :            : 
      55         [ #  # ]:          0 :         if (!q)
      56                 :            :                 return 0;
      57                 :          0 :         return blk_poll(q, READ_ONCE(kiocb->ki_cookie), spin);
      58                 :            : }
      59                 :            : EXPORT_SYMBOL_GPL(iomap_dio_iopoll);
      60                 :            : 
      61                 :            : static void iomap_dio_submit_bio(struct iomap_dio *dio, struct iomap *iomap,
      62                 :            :                 struct bio *bio)
      63                 :            : {
      64                 :            :         atomic_inc(&dio->ref);
      65                 :            : 
      66                 :            :         if (dio->iocb->ki_flags & IOCB_HIPRI)
      67                 :            :                 bio_set_polled(bio, dio->iocb);
      68                 :            : 
      69                 :            :         dio->submit.last_queue = bdev_get_queue(iomap->bdev);
      70                 :            :         dio->submit.cookie = submit_bio(bio);
      71                 :            : }
      72                 :            : 
      73                 :          0 : static ssize_t iomap_dio_complete(struct iomap_dio *dio)
      74                 :            : {
      75                 :          0 :         const struct iomap_dio_ops *dops = dio->dops;
      76                 :          0 :         struct kiocb *iocb = dio->iocb;
      77         [ #  # ]:          0 :         struct inode *inode = file_inode(iocb->ki_filp);
      78                 :          0 :         loff_t offset = iocb->ki_pos;
      79                 :          0 :         ssize_t ret = dio->error;
      80                 :            : 
      81   [ #  #  #  # ]:          0 :         if (dops && dops->end_io)
      82                 :          0 :                 ret = dops->end_io(iocb, dio->size, ret, dio->flags);
      83                 :            : 
      84         [ #  # ]:          0 :         if (likely(!ret)) {
      85                 :          0 :                 ret = dio->size;
      86                 :            :                 /* check for short read */
      87         [ #  # ]:          0 :                 if (offset + ret > dio->i_size &&
      88         [ #  # ]:          0 :                     !(dio->flags & IOMAP_DIO_WRITE))
      89                 :          0 :                         ret = dio->i_size - offset;
      90                 :          0 :                 iocb->ki_pos += ret;
      91                 :            :         }
      92                 :            : 
      93                 :            :         /*
      94                 :            :          * Try again to invalidate clean pages which might have been cached by
      95                 :            :          * non-direct readahead, or faulted in by get_user_pages() if the source
      96                 :            :          * of the write was an mmap'ed region of the file we're writing.  Either
      97                 :            :          * one is a pretty crazy thing to do, so we don't support it 100%.  If
      98                 :            :          * this invalidation fails, tough, the write still worked...
      99                 :            :          *
     100                 :            :          * And this page cache invalidation has to be after ->end_io(), as some
     101                 :            :          * filesystems convert unwritten extents to real allocations in
     102                 :            :          * ->end_io() when necessary, otherwise a racing buffer read would cache
     103                 :            :          * zeros from unwritten extents.
     104                 :            :          */
     105         [ #  # ]:          0 :         if (!dio->error &&
     106   [ #  #  #  # ]:          0 :             (dio->flags & IOMAP_DIO_WRITE) && inode->i_mapping->nrpages) {
     107                 :          0 :                 int err;
     108                 :          0 :                 err = invalidate_inode_pages2_range(inode->i_mapping,
     109                 :          0 :                                 offset >> PAGE_SHIFT,
     110                 :          0 :                                 (offset + dio->size - 1) >> PAGE_SHIFT);
     111         [ #  # ]:          0 :                 if (err)
     112                 :          0 :                         dio_warn_stale_pagecache(iocb->ki_filp);
     113                 :            :         }
     114                 :            : 
     115                 :            :         /*
     116                 :            :          * If this is a DSYNC write, make sure we push it to stable storage now
     117                 :            :          * that we've written data.
     118                 :            :          */
     119   [ #  #  #  # ]:          0 :         if (ret > 0 && (dio->flags & IOMAP_DIO_NEED_SYNC))
     120                 :          0 :                 ret = generic_write_sync(iocb, ret);
     121                 :            : 
     122                 :          0 :         inode_dio_end(file_inode(iocb->ki_filp));
     123                 :          0 :         kfree(dio);
     124                 :            : 
     125                 :          0 :         return ret;
     126                 :            : }
     127                 :            : 
     128                 :          0 : static void iomap_dio_complete_work(struct work_struct *work)
     129                 :            : {
     130                 :          0 :         struct iomap_dio *dio = container_of(work, struct iomap_dio, aio.work);
     131                 :          0 :         struct kiocb *iocb = dio->iocb;
     132                 :            : 
     133                 :          0 :         iocb->ki_complete(iocb, iomap_dio_complete(dio), 0);
     134                 :          0 : }
     135                 :            : 
     136                 :            : /*
     137                 :            :  * Set an error in the dio if none is set yet.  We have to use cmpxchg
     138                 :            :  * as the submission context and the completion context(s) can race to
     139                 :            :  * update the error.
     140                 :            :  */
     141                 :          0 : static inline void iomap_dio_set_error(struct iomap_dio *dio, int ret)
     142                 :            : {
     143                 :          0 :         cmpxchg(&dio->error, 0, ret);
     144                 :          0 : }
     145                 :            : 
     146                 :          0 : static void iomap_dio_bio_end_io(struct bio *bio)
     147                 :            : {
     148                 :          0 :         struct iomap_dio *dio = bio->bi_private;
     149                 :          0 :         bool should_dirty = (dio->flags & IOMAP_DIO_DIRTY);
     150                 :            : 
     151         [ #  # ]:          0 :         if (bio->bi_status)
     152                 :          0 :                 iomap_dio_set_error(dio, blk_status_to_errno(bio->bi_status));
     153                 :            : 
     154         [ #  # ]:          0 :         if (atomic_dec_and_test(&dio->ref)) {
     155         [ #  # ]:          0 :                 if (dio->wait_for_completion) {
     156                 :          0 :                         struct task_struct *waiter = dio->submit.waiter;
     157         [ #  # ]:          0 :                         WRITE_ONCE(dio->submit.waiter, NULL);
     158         [ #  # ]:          0 :                         blk_wake_io_task(waiter);
     159         [ #  # ]:          0 :                 } else if (dio->flags & IOMAP_DIO_WRITE) {
     160                 :          0 :                         struct inode *inode = file_inode(dio->iocb->ki_filp);
     161                 :            : 
     162                 :          0 :                         INIT_WORK(&dio->aio.work, iomap_dio_complete_work);
     163                 :          0 :                         queue_work(inode->i_sb->s_dio_done_wq, &dio->aio.work);
     164                 :            :                 } else {
     165                 :          0 :                         iomap_dio_complete_work(&dio->aio.work);
     166                 :            :                 }
     167                 :            :         }
     168                 :            : 
     169         [ #  # ]:          0 :         if (should_dirty) {
     170                 :          0 :                 bio_check_pages_dirty(bio);
     171                 :            :         } else {
     172                 :          0 :                 bio_release_pages(bio, false);
     173                 :          0 :                 bio_put(bio);
     174                 :            :         }
     175                 :          0 : }
     176                 :            : 
     177                 :            : static void
     178                 :          0 : iomap_dio_zero(struct iomap_dio *dio, struct iomap *iomap, loff_t pos,
     179                 :            :                 unsigned len)
     180                 :            : {
     181         [ #  # ]:          0 :         struct page *page = ZERO_PAGE(0);
     182                 :          0 :         int flags = REQ_SYNC | REQ_IDLE;
     183                 :          0 :         struct bio *bio;
     184                 :            : 
     185                 :          0 :         bio = bio_alloc(GFP_KERNEL, 1);
     186   [ #  #  #  # ]:          0 :         bio_set_dev(bio, iomap->bdev);
     187         [ #  # ]:          0 :         bio->bi_iter.bi_sector = iomap_sector(iomap, pos);
     188                 :          0 :         bio->bi_private = dio;
     189                 :          0 :         bio->bi_end_io = iomap_dio_bio_end_io;
     190                 :            : 
     191         [ #  # ]:          0 :         get_page(page);
     192                 :          0 :         __bio_add_page(bio, page, len, 0);
     193                 :          0 :         bio_set_op_attrs(bio, REQ_OP_WRITE, flags);
     194                 :          0 :         iomap_dio_submit_bio(dio, iomap, bio);
     195                 :          0 : }
     196                 :            : 
     197                 :            : static loff_t
     198                 :          0 : iomap_dio_bio_actor(struct inode *inode, loff_t pos, loff_t length,
     199                 :            :                 struct iomap_dio *dio, struct iomap *iomap)
     200                 :            : {
     201         [ #  # ]:          0 :         unsigned int blkbits = blksize_bits(bdev_logical_block_size(iomap->bdev));
     202                 :          0 :         unsigned int fs_block_size = i_blocksize(inode), pad;
     203                 :          0 :         unsigned int align = iov_iter_alignment(dio->submit.iter);
     204                 :          0 :         struct bio *bio;
     205                 :          0 :         bool need_zeroout = false;
     206                 :          0 :         bool use_fua = false;
     207                 :          0 :         int nr_pages, ret = 0;
     208                 :          0 :         size_t copied = 0;
     209                 :          0 :         size_t orig_count;
     210                 :            : 
     211         [ #  # ]:          0 :         if ((pos | length | align) & ((1 << blkbits) - 1))
     212                 :            :                 return -EINVAL;
     213                 :            : 
     214         [ #  # ]:          0 :         if (iomap->type == IOMAP_UNWRITTEN) {
     215                 :          0 :                 dio->flags |= IOMAP_DIO_UNWRITTEN;
     216                 :          0 :                 need_zeroout = true;
     217                 :            :         }
     218                 :            : 
     219         [ #  # ]:          0 :         if (iomap->flags & IOMAP_F_SHARED)
     220                 :          0 :                 dio->flags |= IOMAP_DIO_COW;
     221                 :            : 
     222         [ #  # ]:          0 :         if (iomap->flags & IOMAP_F_NEW) {
     223                 :            :                 need_zeroout = true;
     224         [ #  # ]:          0 :         } else if (iomap->type == IOMAP_MAPPED) {
     225                 :            :                 /*
     226                 :            :                  * Use a FUA write if we need datasync semantics, this is a pure
     227                 :            :                  * data IO that doesn't require any metadata updates (including
     228                 :            :                  * after IO completion such as unwritten extent conversion) and
     229                 :            :                  * the underlying device supports FUA. This allows us to avoid
     230                 :            :                  * cache flushes on IO completion.
     231                 :            :                  */
     232         [ #  # ]:          0 :                 if (!(iomap->flags & (IOMAP_F_SHARED|IOMAP_F_DIRTY)) &&
     233   [ #  #  #  # ]:          0 :                     (dio->flags & IOMAP_DIO_WRITE_FUA) &&
     234                 :          0 :                     blk_queue_fua(bdev_get_queue(iomap->bdev)))
     235                 :          0 :                         use_fua = true;
     236                 :            :         }
     237                 :            : 
     238                 :            :         /*
     239                 :            :          * Save the original count and trim the iter to just the extent we
     240                 :            :          * are operating on right now.  The iter will be re-expanded once
     241                 :            :          * we are done.
     242                 :            :          */
     243         [ #  # ]:          0 :         orig_count = iov_iter_count(dio->submit.iter);
     244         [ #  # ]:          0 :         iov_iter_truncate(dio->submit.iter, length);
     245                 :            : 
     246                 :          0 :         nr_pages = iov_iter_npages(dio->submit.iter, BIO_MAX_PAGES);
     247         [ #  # ]:          0 :         if (nr_pages <= 0) {
     248                 :          0 :                 ret = nr_pages;
     249                 :          0 :                 goto out;
     250                 :            :         }
     251                 :            : 
     252         [ #  # ]:          0 :         if (need_zeroout) {
     253                 :            :                 /* zero out from the start of the block to the write offset */
     254                 :          0 :                 pad = pos & (fs_block_size - 1);
     255         [ #  # ]:          0 :                 if (pad)
     256                 :          0 :                         iomap_dio_zero(dio, iomap, pos - pad, pad);
     257                 :            :         }
     258                 :            : 
     259                 :          0 :         do {
     260                 :          0 :                 size_t n;
     261         [ #  # ]:          0 :                 if (dio->error) {
     262                 :          0 :                         iov_iter_revert(dio->submit.iter, copied);
     263                 :          0 :                         copied = ret = 0;
     264                 :          0 :                         goto out;
     265                 :            :                 }
     266                 :            : 
     267                 :          0 :                 bio = bio_alloc(GFP_KERNEL, nr_pages);
     268         [ #  # ]:          0 :                 bio_set_dev(bio, iomap->bdev);
     269                 :          0 :                 bio->bi_iter.bi_sector = iomap_sector(iomap, pos);
     270                 :          0 :                 bio->bi_write_hint = dio->iocb->ki_hint;
     271                 :          0 :                 bio->bi_ioprio = dio->iocb->ki_ioprio;
     272                 :          0 :                 bio->bi_private = dio;
     273                 :          0 :                 bio->bi_end_io = iomap_dio_bio_end_io;
     274                 :            : 
     275                 :          0 :                 ret = bio_iov_iter_get_pages(bio, dio->submit.iter);
     276         [ #  # ]:          0 :                 if (unlikely(ret)) {
     277                 :            :                         /*
     278                 :            :                          * We have to stop part way through an IO. We must fall
     279                 :            :                          * through to the sub-block tail zeroing here, otherwise
     280                 :            :                          * this short IO may expose stale data in the tail of
     281                 :            :                          * the block we haven't written data to.
     282                 :            :                          */
     283                 :          0 :                         bio_put(bio);
     284                 :          0 :                         goto zero_tail;
     285                 :            :                 }
     286                 :            : 
     287                 :          0 :                 n = bio->bi_iter.bi_size;
     288         [ #  # ]:          0 :                 if (dio->flags & IOMAP_DIO_WRITE) {
     289                 :          0 :                         bio->bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_IDLE;
     290         [ #  # ]:          0 :                         if (use_fua)
     291                 :          0 :                                 bio->bi_opf |= REQ_FUA;
     292                 :            :                         else
     293                 :          0 :                                 dio->flags &= ~IOMAP_DIO_WRITE_FUA;
     294                 :          0 :                         task_io_account_write(n);
     295                 :            :                 } else {
     296                 :          0 :                         bio->bi_opf = REQ_OP_READ;
     297         [ #  # ]:          0 :                         if (dio->flags & IOMAP_DIO_DIRTY)
     298                 :          0 :                                 bio_set_pages_dirty(bio);
     299                 :            :                 }
     300                 :            : 
     301                 :          0 :                 dio->size += n;
     302                 :          0 :                 pos += n;
     303                 :          0 :                 copied += n;
     304                 :            : 
     305                 :          0 :                 nr_pages = iov_iter_npages(dio->submit.iter, BIO_MAX_PAGES);
     306                 :          0 :                 iomap_dio_submit_bio(dio, iomap, bio);
     307         [ #  # ]:          0 :         } while (nr_pages);
     308                 :            : 
     309                 :            :         /*
     310                 :            :          * We need to zeroout the tail of a sub-block write if the extent type
     311                 :            :          * requires zeroing or the write extends beyond EOF. If we don't zero
     312                 :            :          * the block tail in the latter case, we can expose stale data via mmap
     313                 :            :          * reads of the EOF block.
     314                 :            :          */
     315                 :          0 : zero_tail:
     316         [ #  # ]:          0 :         if (need_zeroout ||
     317   [ #  #  #  # ]:          0 :             ((dio->flags & IOMAP_DIO_WRITE) && pos >= i_size_read(inode))) {
     318                 :            :                 /* zero out from the end of the write to the end of the block */
     319                 :          0 :                 pad = pos & (fs_block_size - 1);
     320         [ #  # ]:          0 :                 if (pad)
     321                 :          0 :                         iomap_dio_zero(dio, iomap, pos, fs_block_size - pad);
     322                 :            :         }
     323                 :          0 : out:
     324                 :            :         /* Undo iter limitation to current extent */
     325         [ #  # ]:          0 :         iov_iter_reexpand(dio->submit.iter, orig_count - copied);
     326         [ #  # ]:          0 :         if (copied)
     327                 :          0 :                 return copied;
     328                 :          0 :         return ret;
     329                 :            : }
     330                 :            : 
     331                 :            : static loff_t
     332                 :          0 : iomap_dio_hole_actor(loff_t length, struct iomap_dio *dio)
     333                 :            : {
     334                 :          0 :         length = iov_iter_zero(length, dio->submit.iter);
     335                 :          0 :         dio->size += length;
     336                 :          0 :         return length;
     337                 :            : }
     338                 :            : 
     339                 :            : static loff_t
     340                 :            : iomap_dio_inline_actor(struct inode *inode, loff_t pos, loff_t length,
     341                 :            :                 struct iomap_dio *dio, struct iomap *iomap)
     342                 :            : {
     343                 :            :         struct iov_iter *iter = dio->submit.iter;
     344                 :            :         size_t copied;
     345                 :            : 
     346                 :            :         BUG_ON(pos + length > PAGE_SIZE - offset_in_page(iomap->inline_data));
     347                 :            : 
     348                 :            :         if (dio->flags & IOMAP_DIO_WRITE) {
     349                 :            :                 loff_t size = inode->i_size;
     350                 :            : 
     351                 :            :                 if (pos > size)
     352                 :            :                         memset(iomap->inline_data + size, 0, pos - size);
     353                 :            :                 copied = copy_from_iter(iomap->inline_data + pos, length, iter);
     354                 :            :                 if (copied) {
     355                 :            :                         if (pos + copied > size)
     356                 :            :                                 i_size_write(inode, pos + copied);
     357                 :            :                         mark_inode_dirty(inode);
     358                 :            :                 }
     359                 :            :         } else {
     360                 :            :                 copied = copy_to_iter(iomap->inline_data + pos, length, iter);
     361                 :            :         }
     362                 :            :         dio->size += copied;
     363                 :            :         return copied;
     364                 :            : }
     365                 :            : 
     366                 :            : static loff_t
     367                 :          0 : iomap_dio_actor(struct inode *inode, loff_t pos, loff_t length,
     368                 :            :                 void *data, struct iomap *iomap, struct iomap *srcmap)
     369                 :            : {
     370                 :          0 :         struct iomap_dio *dio = data;
     371                 :            : 
     372   [ #  #  #  #  :          0 :         switch (iomap->type) {
                      # ]
     373                 :          0 :         case IOMAP_HOLE:
     374   [ #  #  #  # ]:          0 :                 if (WARN_ON_ONCE(dio->flags & IOMAP_DIO_WRITE))
     375                 :            :                         return -EIO;
     376                 :          0 :                 return iomap_dio_hole_actor(length, dio);
     377                 :          0 :         case IOMAP_UNWRITTEN:
     378         [ #  # ]:          0 :                 if (!(dio->flags & IOMAP_DIO_WRITE))
     379                 :          0 :                         return iomap_dio_hole_actor(length, dio);
     380                 :          0 :                 return iomap_dio_bio_actor(inode, pos, length, dio, iomap);
     381                 :          0 :         case IOMAP_MAPPED:
     382                 :          0 :                 return iomap_dio_bio_actor(inode, pos, length, dio, iomap);
     383                 :          0 :         case IOMAP_INLINE:
     384                 :          0 :                 return iomap_dio_inline_actor(inode, pos, length, dio, iomap);
     385                 :            :         default:
     386                 :          0 :                 WARN_ON_ONCE(1);
     387                 :          0 :                 return -EIO;
     388                 :            :         }
     389                 :            : }
     390                 :            : 
     391                 :            : /*
     392                 :            :  * iomap_dio_rw() always completes O_[D]SYNC writes regardless of whether the IO
     393                 :            :  * is being issued as AIO or not.  This allows us to optimise pure data writes
     394                 :            :  * to use REQ_FUA rather than requiring generic_write_sync() to issue a
     395                 :            :  * REQ_FLUSH post write. This is slightly tricky because a single request here
     396                 :            :  * can be mapped into multiple disjoint IOs and only a subset of the IOs issued
     397                 :            :  * may be pure data writes. In that case, we still need to do a full data sync
     398                 :            :  * completion.
     399                 :            :  */
     400                 :            : ssize_t
     401                 :          0 : iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter,
     402                 :            :                 const struct iomap_ops *ops, const struct iomap_dio_ops *dops,
     403                 :            :                 bool wait_for_completion)
     404                 :            : {
     405                 :          0 :         struct address_space *mapping = iocb->ki_filp->f_mapping;
     406         [ #  # ]:          0 :         struct inode *inode = file_inode(iocb->ki_filp);
     407         [ #  # ]:          0 :         size_t count = iov_iter_count(iter);
     408                 :          0 :         loff_t pos = iocb->ki_pos;
     409                 :          0 :         loff_t end = iocb->ki_pos + count - 1, ret = 0;
     410                 :          0 :         unsigned int flags = IOMAP_DIRECT;
     411                 :          0 :         struct blk_plug plug;
     412                 :          0 :         struct iomap_dio *dio;
     413                 :            : 
     414                 :          0 :         lockdep_assert_held(&inode->i_rwsem);
     415                 :            : 
     416         [ #  # ]:          0 :         if (!count)
     417                 :            :                 return 0;
     418                 :            : 
     419   [ #  #  #  #  :          0 :         if (WARN_ON(is_sync_kiocb(iocb) && !wait_for_completion))
             #  #  #  # ]
     420                 :            :                 return -EIO;
     421                 :            : 
     422                 :          0 :         dio = kmalloc(sizeof(*dio), GFP_KERNEL);
     423         [ #  # ]:          0 :         if (!dio)
     424                 :            :                 return -ENOMEM;
     425                 :            : 
     426                 :          0 :         dio->iocb = iocb;
     427                 :          0 :         atomic_set(&dio->ref, 1);
     428                 :          0 :         dio->size = 0;
     429         [ #  # ]:          0 :         dio->i_size = i_size_read(inode);
     430                 :          0 :         dio->dops = dops;
     431                 :          0 :         dio->error = 0;
     432                 :          0 :         dio->flags = 0;
     433                 :            : 
     434                 :          0 :         dio->submit.iter = iter;
     435         [ #  # ]:          0 :         dio->submit.waiter = current;
     436                 :          0 :         dio->submit.cookie = BLK_QC_T_NONE;
     437                 :          0 :         dio->submit.last_queue = NULL;
     438                 :            : 
     439         [ #  # ]:          0 :         if (iov_iter_rw(iter) == READ) {
     440         [ #  # ]:          0 :                 if (pos >= dio->i_size)
     441                 :          0 :                         goto out_free_dio;
     442                 :            : 
     443         [ #  # ]:          0 :                 if (iter_is_iovec(iter))
     444                 :          0 :                         dio->flags |= IOMAP_DIO_DIRTY;
     445                 :            :         } else {
     446                 :          0 :                 flags |= IOMAP_WRITE;
     447                 :          0 :                 dio->flags |= IOMAP_DIO_WRITE;
     448                 :            : 
     449                 :            :                 /* for data sync or sync, we need sync completion processing */
     450         [ #  # ]:          0 :                 if (iocb->ki_flags & IOCB_DSYNC)
     451                 :          0 :                         dio->flags |= IOMAP_DIO_NEED_SYNC;
     452                 :            : 
     453                 :            :                 /*
     454                 :            :                  * For datasync only writes, we optimistically try using FUA for
     455                 :            :                  * this IO.  Any non-FUA write that occurs will clear this flag,
     456                 :            :                  * hence we know before completion whether a cache flush is
     457                 :            :                  * necessary.
     458                 :            :                  */
     459         [ #  # ]:          0 :                 if ((iocb->ki_flags & (IOCB_DSYNC | IOCB_SYNC)) == IOCB_DSYNC)
     460                 :          0 :                         dio->flags |= IOMAP_DIO_WRITE_FUA;
     461                 :            :         }
     462                 :            : 
     463         [ #  # ]:          0 :         if (iocb->ki_flags & IOCB_NOWAIT) {
     464         [ #  # ]:          0 :                 if (filemap_range_has_page(mapping, pos, end)) {
     465                 :          0 :                         ret = -EAGAIN;
     466                 :          0 :                         goto out_free_dio;
     467                 :            :                 }
     468                 :          0 :                 flags |= IOMAP_NOWAIT;
     469                 :            :         }
     470                 :            : 
     471                 :          0 :         ret = filemap_write_and_wait_range(mapping, pos, end);
     472         [ #  # ]:          0 :         if (ret)
     473                 :          0 :                 goto out_free_dio;
     474                 :            : 
     475                 :            :         /*
     476                 :            :          * Try to invalidate cache pages for the range we're direct
     477                 :            :          * writing.  If this invalidation fails, tough, the write will
     478                 :            :          * still work, but racing two incompatible write paths is a
     479                 :            :          * pretty crazy thing to do, so we don't support it 100%.
     480                 :            :          */
     481                 :          0 :         ret = invalidate_inode_pages2_range(mapping,
     482                 :          0 :                         pos >> PAGE_SHIFT, end >> PAGE_SHIFT);
     483         [ #  # ]:          0 :         if (ret)
     484                 :          0 :                 dio_warn_stale_pagecache(iocb->ki_filp);
     485                 :          0 :         ret = 0;
     486                 :            : 
     487   [ #  #  #  # ]:          0 :         if (iov_iter_rw(iter) == WRITE && !wait_for_completion &&
     488         [ #  # ]:          0 :             !inode->i_sb->s_dio_done_wq) {
     489                 :          0 :                 ret = sb_init_dio_done_wq(inode->i_sb);
     490         [ #  # ]:          0 :                 if (ret < 0)
     491                 :          0 :                         goto out_free_dio;
     492                 :            :         }
     493                 :            : 
     494                 :          0 :         inode_dio_begin(inode);
     495                 :            : 
     496                 :          0 :         blk_start_plug(&plug);
     497                 :          0 :         do {
     498                 :          0 :                 ret = iomap_apply(inode, pos, count, flags, ops, dio,
     499                 :            :                                 iomap_dio_actor);
     500         [ #  # ]:          0 :                 if (ret <= 0) {
     501                 :            :                         /* magic error code to fall back to buffered I/O */
     502         [ #  # ]:          0 :                         if (ret == -ENOTBLK) {
     503                 :          0 :                                 wait_for_completion = true;
     504                 :          0 :                                 ret = 0;
     505                 :            :                         }
     506                 :            :                         break;
     507                 :            :                 }
     508                 :          0 :                 pos += ret;
     509                 :            : 
     510   [ #  #  #  # ]:          0 :                 if (iov_iter_rw(iter) == READ && pos >= dio->i_size) {
     511                 :            :                         /*
     512                 :            :                          * We only report that we've read data up to i_size.
     513                 :            :                          * Revert iter to a state corresponding to that as
     514                 :            :                          * some callers (such as splice code) rely on it.
     515                 :            :                          */
     516                 :          0 :                         iov_iter_revert(iter, pos - dio->i_size);
     517                 :          0 :                         break;
     518                 :            :                 }
     519         [ #  # ]:          0 :         } while ((count = iov_iter_count(iter)) > 0);
     520                 :          0 :         blk_finish_plug(&plug);
     521                 :            : 
     522         [ #  # ]:          0 :         if (ret < 0)
     523                 :          0 :                 iomap_dio_set_error(dio, ret);
     524                 :            : 
     525                 :            :         /*
     526                 :            :          * If all the writes we issued were FUA, we don't need to flush the
     527                 :            :          * cache on IO completion. Clear the sync flag for this case.
     528                 :            :          */
     529         [ #  # ]:          0 :         if (dio->flags & IOMAP_DIO_WRITE_FUA)
     530                 :          0 :                 dio->flags &= ~IOMAP_DIO_NEED_SYNC;
     531                 :            : 
     532                 :          0 :         WRITE_ONCE(iocb->ki_cookie, dio->submit.cookie);
     533                 :          0 :         WRITE_ONCE(iocb->private, dio->submit.last_queue);
     534                 :            : 
     535                 :            :         /*
     536                 :            :          * We are about to drop our additional submission reference, which
     537                 :            :          * might be the last reference to the dio.  There are three three
     538                 :            :          * different ways we can progress here:
     539                 :            :          *
     540                 :            :          *  (a) If this is the last reference we will always complete and free
     541                 :            :          *      the dio ourselves.
     542                 :            :          *  (b) If this is not the last reference, and we serve an asynchronous
     543                 :            :          *      iocb, we must never touch the dio after the decrement, the
     544                 :            :          *      I/O completion handler will complete and free it.
     545                 :            :          *  (c) If this is not the last reference, but we serve a synchronous
     546                 :            :          *      iocb, the I/O completion handler will wake us up on the drop
     547                 :            :          *      of the final reference, and we will complete and free it here
     548                 :            :          *      after we got woken by the I/O completion handler.
     549                 :            :          */
     550                 :          0 :         dio->wait_for_completion = wait_for_completion;
     551         [ #  # ]:          0 :         if (!atomic_dec_and_test(&dio->ref)) {
     552         [ #  # ]:          0 :                 if (!wait_for_completion)
     553                 :            :                         return -EIOCBQUEUED;
     554                 :            : 
     555                 :          0 :                 for (;;) {
     556                 :          0 :                         set_current_state(TASK_UNINTERRUPTIBLE);
     557         [ #  # ]:          0 :                         if (!READ_ONCE(dio->submit.waiter))
     558                 :            :                                 break;
     559                 :            : 
     560         [ #  # ]:          0 :                         if (!(iocb->ki_flags & IOCB_HIPRI) ||
     561   [ #  #  #  # ]:          0 :                             !dio->submit.last_queue ||
     562                 :          0 :                             !blk_poll(dio->submit.last_queue,
     563                 :            :                                          dio->submit.cookie, true))
     564                 :          0 :                                 io_schedule();
     565                 :            :                 }
     566                 :          0 :                 __set_current_state(TASK_RUNNING);
     567                 :            :         }
     568                 :            : 
     569                 :          0 :         return iomap_dio_complete(dio);
     570                 :            : 
     571                 :          0 : out_free_dio:
     572                 :          0 :         kfree(dio);
     573                 :          0 :         return ret;
     574                 :            : }
     575                 :            : EXPORT_SYMBOL_GPL(iomap_dio_rw);

Generated by: LCOV version 1.14