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