| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
/* |
| 3 |
* Copyright (C) 2010 Red Hat, Inc. |
| 4 |
* Copyright (c) 2016-2025 Christoph Hellwig. |
| 5 |
*/ |
| 6 |
#include <linux/blk-crypto.h> |
| 7 |
#include <linux/fscrypt.h> |
| 8 |
#include <linux/pagemap.h> |
| 9 |
#include <linux/iomap.h> |
| 10 |
#include <linux/task_io_accounting_ops.h> |
| 11 |
#include <linux/fserror.h> |
| 12 |
#include "internal.h" |
| 13 |
#include "trace.h" |
| 14 |
|
| 15 |
#include "../internal.h" |
| 16 |
|
| 17 |
/* |
| 18 |
* Private flags for iomap_dio, must not overlap with the public ones in |
| 19 |
* iomap.h: |
| 20 |
*/ |
| 21 |
#define IOMAP_DIO_NO_INVALIDATE (1U << 26) |
| 22 |
#define IOMAP_DIO_COMP_WORK (1U << 27) |
| 23 |
#define IOMAP_DIO_WRITE_THROUGH (1U << 28) |
| 24 |
#define IOMAP_DIO_NEED_SYNC (1U << 29) |
| 25 |
#define IOMAP_DIO_WRITE (1U << 30) |
| 26 |
#define IOMAP_DIO_USER_BACKED (1U << 31) |
| 27 |
|
| 28 |
struct iomap_dio { |
| 29 |
struct kiocb *iocb; |
| 30 |
const struct iomap_dio_ops *dops; |
| 31 |
loff_t i_size; |
| 32 |
loff_t size; |
| 33 |
atomic_t ref; |
| 34 |
unsigned flags; |
| 35 |
int error; |
| 36 |
size_t done_before; |
| 37 |
bool wait_for_completion; |
| 38 |
|
| 39 |
union { |
| 40 |
/* used during submission and for synchronous completion: */ |
| 41 |
struct { |
| 42 |
struct iov_iter *iter; |
| 43 |
struct task_struct *waiter; |
| 44 |
} submit; |
| 45 |
|
| 46 |
/* used for aio completion: */ |
| 47 |
struct { |
| 48 |
struct work_struct work; |
| 49 |
} aio; |
| 50 |
}; |
| 51 |
}; |
| 52 |
|
| 53 |
static struct bio *iomap_dio_alloc_bio(const struct iomap_iter *iter, |
| 54 |
struct iomap_dio *dio, unsigned short nr_vecs, blk_opf_t opf) |
| 55 |
{ |
| 56 |
if (dio->dops && dio->dops->bio_set) |
| 57 |
return bio_alloc_bioset(iter->iomap.bdev, nr_vecs, opf, |
| 58 |
GFP_KERNEL, dio->dops->bio_set); |
| 59 |
return bio_alloc(iter->iomap.bdev, nr_vecs, opf, GFP_KERNEL); |
| 60 |
} |
| 61 |
|
| 62 |
static void iomap_dio_submit_bio(const struct iomap_iter *iter, |
| 63 |
struct iomap_dio *dio, struct bio *bio, loff_t pos) |
| 64 |
{ |
| 65 |
struct kiocb *iocb = dio->iocb; |
| 66 |
|
| 67 |
atomic_inc(&dio->ref); |
| 68 |
|
| 69 |
/* Sync dio can't be polled reliably */ |
| 70 |
if ((iocb->ki_flags & IOCB_HIPRI) && !is_sync_kiocb(iocb)) { |
| 71 |
bio_set_polled(bio, iocb); |
| 72 |
WRITE_ONCE(iocb->private, bio); |
| 73 |
} |
| 74 |
|
| 75 |
if (dio->dops && dio->dops->submit_io) { |
| 76 |
dio->dops->submit_io(iter, bio, pos); |
| 77 |
} else { |
| 78 |
WARN_ON_ONCE(iter->iomap.flags & IOMAP_F_ANON_WRITE); |
| 79 |
blk_crypto_submit_bio(bio); |
| 80 |
} |
| 81 |
} |
| 82 |
|
| 83 |
static inline enum fserror_type iomap_dio_err_type(const struct iomap_dio *dio) |
| 84 |
{ |
| 85 |
if (dio->flags & IOMAP_DIO_WRITE) |
| 86 |
return FSERR_DIRECTIO_WRITE; |
| 87 |
return FSERR_DIRECTIO_READ; |
| 88 |
} |
| 89 |
|
| 90 |
static inline bool should_report_dio_fserror(const struct iomap_dio *dio) |
| 91 |
{ |
| 92 |
switch (dio->error) { |
| 93 |
case 0: |
| 94 |
case -EAGAIN: |
| 95 |
case -ENOTBLK: |
| 96 |
/* don't send fsnotify for success or magic retry codes */ |
| 97 |
return false; |
| 98 |
default: |
| 99 |
return true; |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
ssize_t iomap_dio_complete(struct iomap_dio *dio) |
| 104 |
{ |
| 105 |
const struct iomap_dio_ops *dops = dio->dops; |
| 106 |
struct kiocb *iocb = dio->iocb; |
| 107 |
loff_t offset = iocb->ki_pos; |
| 108 |
ssize_t ret = dio->error; |
| 109 |
|
| 110 |
if (dops && dops->end_io) |
| 111 |
ret = dops->end_io(iocb, dio->size, ret, dio->flags); |
| 112 |
if (should_report_dio_fserror(dio)) |
| 113 |
fserror_report_io(file_inode(iocb->ki_filp), |
| 114 |
iomap_dio_err_type(dio), offset, dio->size, |
| 115 |
dio->error, GFP_NOFS); |
| 116 |
|
| 117 |
if (likely(!ret)) { |
| 118 |
ret = dio->size; |
| 119 |
/* check for short read */ |
| 120 |
if (offset + ret > dio->i_size && |
| 121 |
!(dio->flags & IOMAP_DIO_WRITE)) |
| 122 |
ret = dio->i_size - offset; |
| 123 |
} |
| 124 |
|
| 125 |
/* |
| 126 |
* Try again to invalidate clean pages which might have been cached by |
| 127 |
* non-direct readahead, or faulted in by get_user_pages() if the source |
| 128 |
* of the write was an mmap'ed region of the file we're writing. Either |
| 129 |
* one is a pretty crazy thing to do, so we don't support it 100%. If |
| 130 |
* this invalidation fails, tough, the write still worked... |
| 131 |
* |
| 132 |
* And this page cache invalidation has to be after ->end_io(), as some |
| 133 |
* filesystems convert unwritten extents to real allocations in |
| 134 |
* ->end_io() when necessary, otherwise a racing buffer read would cache |
| 135 |
* zeros from unwritten extents. |
| 136 |
*/ |
| 137 |
if (!dio->error && dio->size && (dio->flags & IOMAP_DIO_WRITE) && |
| 138 |
!(dio->flags & IOMAP_DIO_NO_INVALIDATE)) |
| 139 |
kiocb_invalidate_post_direct_write(iocb, dio->size); |
| 140 |
|
| 141 |
inode_dio_end(file_inode(iocb->ki_filp)); |
| 142 |
|
| 143 |
if (ret > 0) { |
| 144 |
iocb->ki_pos += ret; |
| 145 |
|
| 146 |
/* |
| 147 |
* If this is a DSYNC write, make sure we push it to stable |
| 148 |
* storage now that we've written data. |
| 149 |
*/ |
| 150 |
if (dio->flags & IOMAP_DIO_NEED_SYNC) |
| 151 |
ret = generic_write_sync(iocb, ret); |
| 152 |
if (ret > 0) |
| 153 |
ret += dio->done_before; |
| 154 |
} |
| 155 |
trace_iomap_dio_complete(iocb, dio->error, ret); |
| 156 |
kfree(dio); |
| 157 |
return ret; |
| 158 |
} |
| 159 |
EXPORT_SYMBOL_GPL(iomap_dio_complete); |
| 160 |
|
| 161 |
static void iomap_dio_complete_work(struct work_struct *work) |
| 162 |
{ |
| 163 |
struct iomap_dio *dio = container_of(work, struct iomap_dio, aio.work); |
| 164 |
struct kiocb *iocb = dio->iocb; |
| 165 |
|
| 166 |
iocb->ki_complete(iocb, iomap_dio_complete(dio)); |
| 167 |
} |
| 168 |
|
| 169 |
/* |
| 170 |
* Set an error in the dio if none is set yet. We have to use cmpxchg |
| 171 |
* as the submission context and the completion context(s) can race to |
| 172 |
* update the error. |
| 173 |
*/ |
| 174 |
static inline void iomap_dio_set_error(struct iomap_dio *dio, int ret) |
| 175 |
{ |
| 176 |
cmpxchg(&dio->error, 0, ret); |
| 177 |
} |
| 178 |
|
| 179 |
/* |
| 180 |
* Called when dio->ref reaches zero from an I/O completion. |
| 181 |
*/ |
| 182 |
static void iomap_dio_done(struct iomap_dio *dio) |
| 183 |
{ |
| 184 |
struct kiocb *iocb = dio->iocb; |
| 185 |
|
| 186 |
if (dio->wait_for_completion) { |
| 187 |
/* |
| 188 |
* Synchronous I/O, task itself will handle any completion work |
| 189 |
* that needs after IO. All we need to do is wake the task. |
| 190 |
*/ |
| 191 |
struct task_struct *waiter = dio->submit.waiter; |
| 192 |
|
| 193 |
WRITE_ONCE(dio->submit.waiter, NULL); |
| 194 |
blk_wake_io_task(waiter); |
| 195 |
return; |
| 196 |
} |
| 197 |
|
| 198 |
/* |
| 199 |
* Always run error completions in user context. These are not |
| 200 |
* performance critical and some code relies on taking sleeping locks |
| 201 |
* for error handling. |
| 202 |
*/ |
| 203 |
if (dio->error) |
| 204 |
dio->flags |= IOMAP_DIO_COMP_WORK; |
| 205 |
|
| 206 |
/* |
| 207 |
* Never invalidate pages from this context to avoid deadlocks with |
| 208 |
* buffered I/O completions when called from the ioend workqueue, |
| 209 |
* or avoid sleeping when called directly from ->bi_end_io. |
| 210 |
* Tough luck if you hit the tiny race with someone dirtying the range |
| 211 |
* right between this check and the actual completion. |
| 212 |
*/ |
| 213 |
if ((dio->flags & IOMAP_DIO_WRITE) && |
| 214 |
!(dio->flags & IOMAP_DIO_COMP_WORK)) { |
| 215 |
if (dio->iocb->ki_filp->f_mapping->nrpages) |
| 216 |
dio->flags |= IOMAP_DIO_COMP_WORK; |
| 217 |
else |
| 218 |
dio->flags |= IOMAP_DIO_NO_INVALIDATE; |
| 219 |
} |
| 220 |
|
| 221 |
if (dio->flags & IOMAP_DIO_COMP_WORK) { |
| 222 |
struct inode *inode = file_inode(iocb->ki_filp); |
| 223 |
|
| 224 |
/* |
| 225 |
* Async DIO completion that requires filesystem level |
| 226 |
* completion work gets punted to a work queue to complete as |
| 227 |
* the operation may require more IO to be issued to finalise |
| 228 |
* filesystem metadata changes or guarantee data integrity. |
| 229 |
*/ |
| 230 |
INIT_WORK(&dio->aio.work, iomap_dio_complete_work); |
| 231 |
queue_work(inode->i_sb->s_dio_done_wq, &dio->aio.work); |
| 232 |
return; |
| 233 |
} |
| 234 |
|
| 235 |
WRITE_ONCE(iocb->private, NULL); |
| 236 |
iomap_dio_complete_work(&dio->aio.work); |
| 237 |
} |
| 238 |
|
| 239 |
static void __iomap_dio_bio_end_io(struct bio *bio, bool inline_completion) |
| 240 |
{ |
| 241 |
struct iomap_dio *dio = bio->bi_private; |
| 242 |
|
| 243 |
if (dio->flags & IOMAP_DIO_BOUNCE) { |
| 244 |
bio_iov_iter_unbounce(bio, !!dio->error, |
| 245 |
dio->flags & IOMAP_DIO_USER_BACKED); |
| 246 |
bio_put(bio); |
| 247 |
} else if (dio->flags & IOMAP_DIO_USER_BACKED) { |
| 248 |
bio_check_pages_dirty(bio); |
| 249 |
} else { |
| 250 |
bio_release_pages(bio, false); |
| 251 |
bio_put(bio); |
| 252 |
} |
| 253 |
|
| 254 |
/* Do not touch bio below, we just gave up our reference. */ |
| 255 |
|
| 256 |
if (atomic_dec_and_test(&dio->ref)) { |
| 257 |
/* |
| 258 |
* Avoid another context switch for the completion when already |
| 259 |
* called from the ioend completion workqueue. |
| 260 |
*/ |
| 261 |
if (inline_completion) |
| 262 |
dio->flags &= ~IOMAP_DIO_COMP_WORK; |
| 263 |
iomap_dio_done(dio); |
| 264 |
} |
| 265 |
} |
| 266 |
|
| 267 |
void iomap_dio_bio_end_io(struct bio *bio) |
| 268 |
{ |
| 269 |
struct iomap_dio *dio = bio->bi_private; |
| 270 |
|
| 271 |
if (bio->bi_status) |
| 272 |
iomap_dio_set_error(dio, blk_status_to_errno(bio->bi_status)); |
| 273 |
__iomap_dio_bio_end_io(bio, false); |
| 274 |
} |
| 275 |
EXPORT_SYMBOL_GPL(iomap_dio_bio_end_io); |
| 276 |
|
| 277 |
u32 iomap_finish_ioend_direct(struct iomap_ioend *ioend) |
| 278 |
{ |
| 279 |
struct iomap_dio *dio = ioend->io_bio.bi_private; |
| 280 |
u32 vec_count = ioend->io_bio.bi_vcnt; |
| 281 |
|
| 282 |
if (ioend->io_error) |
| 283 |
iomap_dio_set_error(dio, ioend->io_error); |
| 284 |
__iomap_dio_bio_end_io(&ioend->io_bio, true); |
| 285 |
|
| 286 |
/* |
| 287 |
* Return the number of bvecs completed as even direct I/O completions |
| 288 |
* do significant per-folio work and we'll still want to give up the |
| 289 |
* CPU after a lot of completions. |
| 290 |
*/ |
| 291 |
return vec_count; |
| 292 |
} |
| 293 |
|
| 294 |
static int iomap_dio_zero(const struct iomap_iter *iter, struct iomap_dio *dio, |
| 295 |
loff_t pos, unsigned len) |
| 296 |
{ |
| 297 |
struct inode *inode = file_inode(dio->iocb->ki_filp); |
| 298 |
struct bio *bio; |
| 299 |
struct folio *zero_folio = largest_zero_folio(); |
| 300 |
int nr_vecs = max(1, i_blocksize(inode) / folio_size(zero_folio)); |
| 301 |
|
| 302 |
if (!len) |
| 303 |
return 0; |
| 304 |
|
| 305 |
/* |
| 306 |
* This limit shall never be reached as most filesystems have a |
| 307 |
* maximum blocksize of 64k. |
| 308 |
*/ |
| 309 |
if (WARN_ON_ONCE(nr_vecs > BIO_MAX_VECS)) |
| 310 |
return -EINVAL; |
| 311 |
|
| 312 |
bio = iomap_dio_alloc_bio(iter, dio, nr_vecs, |
| 313 |
REQ_OP_WRITE | REQ_SYNC | REQ_IDLE); |
| 314 |
fscrypt_set_bio_crypt_ctx(bio, inode, pos >> inode->i_blkbits, |
| 315 |
GFP_KERNEL); |
| 316 |
bio->bi_iter.bi_sector = iomap_sector(&iter->iomap, pos); |
| 317 |
bio->bi_private = dio; |
| 318 |
bio->bi_end_io = iomap_dio_bio_end_io; |
| 319 |
|
| 320 |
while (len > 0) { |
| 321 |
unsigned int io_len = min(len, folio_size(zero_folio)); |
| 322 |
|
| 323 |
bio_add_folio_nofail(bio, zero_folio, io_len, 0); |
| 324 |
len -= io_len; |
| 325 |
} |
| 326 |
iomap_dio_submit_bio(iter, dio, bio, pos); |
| 327 |
|
| 328 |
return 0; |
| 329 |
} |
| 330 |
|
| 331 |
static ssize_t iomap_dio_bio_iter_one(struct iomap_iter *iter, |
| 332 |
struct iomap_dio *dio, loff_t pos, unsigned int alignment, |
| 333 |
blk_opf_t op) |
| 334 |
{ |
| 335 |
unsigned int nr_vecs; |
| 336 |
struct bio *bio; |
| 337 |
ssize_t ret; |
| 338 |
|
| 339 |
if (dio->flags & IOMAP_DIO_BOUNCE) |
| 340 |
nr_vecs = bio_iov_bounce_nr_vecs(dio->submit.iter, op); |
| 341 |
else |
| 342 |
nr_vecs = bio_iov_vecs_to_alloc(dio->submit.iter, BIO_MAX_VECS); |
| 343 |
|
| 344 |
bio = iomap_dio_alloc_bio(iter, dio, nr_vecs, op); |
| 345 |
fscrypt_set_bio_crypt_ctx(bio, iter->inode, |
| 346 |
pos >> iter->inode->i_blkbits, GFP_KERNEL); |
| 347 |
bio->bi_iter.bi_sector = iomap_sector(&iter->iomap, pos); |
| 348 |
bio->bi_write_hint = iter->inode->i_write_hint; |
| 349 |
bio->bi_ioprio = dio->iocb->ki_ioprio; |
| 350 |
bio->bi_private = dio; |
| 351 |
bio->bi_end_io = iomap_dio_bio_end_io; |
| 352 |
|
| 353 |
if (dio->flags & IOMAP_DIO_BOUNCE) |
| 354 |
ret = bio_iov_iter_bounce(bio, dio->submit.iter); |
| 355 |
else |
| 356 |
ret = bio_iov_iter_get_pages(bio, dio->submit.iter, |
| 357 |
alignment - 1); |
| 358 |
if (unlikely(ret)) |
| 359 |
goto out_put_bio; |
| 360 |
ret = bio->bi_iter.bi_size; |
| 361 |
|
| 362 |
/* |
| 363 |
* An atomic write bio must cover the complete length. If it doesn't, |
| 364 |
* error out. |
| 365 |
*/ |
| 366 |
if ((op & REQ_ATOMIC) && WARN_ON_ONCE(ret != iomap_length(iter))) { |
| 367 |
ret = -EINVAL; |
| 368 |
goto out_put_bio; |
| 369 |
} |
| 370 |
|
| 371 |
if (dio->flags & IOMAP_DIO_WRITE) |
| 372 |
task_io_account_write(ret); |
| 373 |
else if ((dio->flags & IOMAP_DIO_USER_BACKED) && |
| 374 |
!(dio->flags & IOMAP_DIO_BOUNCE)) |
| 375 |
bio_set_pages_dirty(bio); |
| 376 |
|
| 377 |
/* |
| 378 |
* We can only poll for single bio I/Os. |
| 379 |
*/ |
| 380 |
if (iov_iter_count(dio->submit.iter)) |
| 381 |
dio->iocb->ki_flags &= ~IOCB_HIPRI; |
| 382 |
iomap_dio_submit_bio(iter, dio, bio, pos); |
| 383 |
return ret; |
| 384 |
|
| 385 |
out_put_bio: |
| 386 |
bio_put(bio); |
| 387 |
return ret; |
| 388 |
} |
| 389 |
|
| 390 |
static int iomap_dio_bio_iter(struct iomap_iter *iter, struct iomap_dio *dio) |
| 391 |
{ |
| 392 |
const struct iomap *iomap = &iter->iomap; |
| 393 |
struct inode *inode = iter->inode; |
| 394 |
unsigned int fs_block_size = i_blocksize(inode), pad; |
| 395 |
const loff_t length = iomap_length(iter); |
| 396 |
loff_t pos = iter->pos; |
| 397 |
blk_opf_t bio_opf = REQ_SYNC | REQ_IDLE; |
| 398 |
bool need_zeroout = false; |
| 399 |
u64 copied = 0; |
| 400 |
size_t orig_count; |
| 401 |
unsigned int alignment; |
| 402 |
ssize_t ret = 0; |
| 403 |
|
| 404 |
/* |
| 405 |
* File systems that write out of place and always allocate new blocks |
| 406 |
* need each bio to be block aligned as that's the unit of allocation. |
| 407 |
*/ |
| 408 |
if (dio->flags & IOMAP_DIO_FSBLOCK_ALIGNED) |
| 409 |
alignment = fs_block_size; |
| 410 |
else |
| 411 |
alignment = bdev_logical_block_size(iomap->bdev); |
| 412 |
|
| 413 |
if ((pos | length) & (alignment - 1)) |
| 414 |
return -EINVAL; |
| 415 |
|
| 416 |
if (dio->flags & IOMAP_DIO_WRITE) { |
| 417 |
bool need_completion_work = true; |
| 418 |
|
| 419 |
switch (iomap->type) { |
| 420 |
case IOMAP_MAPPED: |
| 421 |
/* |
| 422 |
* Directly mapped I/O does not inherently need to do |
| 423 |
* work at I/O completion time. But there are various |
| 424 |
* cases below where this will get set again. |
| 425 |
*/ |
| 426 |
need_completion_work = false; |
| 427 |
break; |
| 428 |
case IOMAP_UNWRITTEN: |
| 429 |
dio->flags |= IOMAP_DIO_UNWRITTEN; |
| 430 |
need_zeroout = true; |
| 431 |
break; |
| 432 |
default: |
| 433 |
break; |
| 434 |
} |
| 435 |
|
| 436 |
if (iomap->flags & IOMAP_F_ATOMIC_BIO) { |
| 437 |
/* |
| 438 |
* Ensure that the mapping covers the full write |
| 439 |
* length, otherwise it won't be submitted as a single |
| 440 |
* bio, which is required to use hardware atomics. |
| 441 |
*/ |
| 442 |
if (length != iter->len) |
| 443 |
return -EINVAL; |
| 444 |
bio_opf |= REQ_ATOMIC; |
| 445 |
} |
| 446 |
|
| 447 |
if (iomap->flags & IOMAP_F_SHARED) { |
| 448 |
/* |
| 449 |
* Unsharing of needs to update metadata at I/O |
| 450 |
* completion time. |
| 451 |
*/ |
| 452 |
need_completion_work = true; |
| 453 |
dio->flags |= IOMAP_DIO_COW; |
| 454 |
} |
| 455 |
|
| 456 |
if (iomap->flags & IOMAP_F_NEW) { |
| 457 |
/* |
| 458 |
* Newly allocated blocks might need recording in |
| 459 |
* metadata at I/O completion time. |
| 460 |
*/ |
| 461 |
need_completion_work = true; |
| 462 |
need_zeroout = true; |
| 463 |
} |
| 464 |
|
| 465 |
/* |
| 466 |
* Use a FUA write if we need datasync semantics and this is a |
| 467 |
* pure overwrite that doesn't require any metadata updates. |
| 468 |
* |
| 469 |
* This allows us to avoid cache flushes on I/O completion. |
| 470 |
*/ |
| 471 |
if (dio->flags & IOMAP_DIO_WRITE_THROUGH) { |
| 472 |
if (!need_completion_work && |
| 473 |
!(iomap->flags & IOMAP_F_DIRTY) && |
| 474 |
(!bdev_write_cache(iomap->bdev) || |
| 475 |
bdev_fua(iomap->bdev))) |
| 476 |
bio_opf |= REQ_FUA; |
| 477 |
else |
| 478 |
dio->flags &= ~IOMAP_DIO_WRITE_THROUGH; |
| 479 |
} |
| 480 |
|
| 481 |
/* |
| 482 |
* We can only do inline completion for pure overwrites that |
| 483 |
* don't require additional I/O at completion time. |
| 484 |
* |
| 485 |
* This rules out writes that need zeroing or metdata updates to |
| 486 |
* convert unwritten or shared extents. |
| 487 |
* |
| 488 |
* Writes that extend i_size are also not supported, but this is |
| 489 |
* handled in __iomap_dio_rw(). |
| 490 |
*/ |
| 491 |
if (need_completion_work) |
| 492 |
dio->flags |= IOMAP_DIO_COMP_WORK; |
| 493 |
|
| 494 |
bio_opf |= REQ_OP_WRITE; |
| 495 |
} else { |
| 496 |
bio_opf |= REQ_OP_READ; |
| 497 |
} |
| 498 |
|
| 499 |
/* |
| 500 |
* Save the original count and trim the iter to just the extent we |
| 501 |
* are operating on right now. The iter will be re-expanded once |
| 502 |
* we are done. |
| 503 |
*/ |
| 504 |
orig_count = iov_iter_count(dio->submit.iter); |
| 505 |
iov_iter_truncate(dio->submit.iter, length); |
| 506 |
|
| 507 |
if (!iov_iter_count(dio->submit.iter)) |
| 508 |
goto out; |
| 509 |
|
| 510 |
/* |
| 511 |
* The rules for polled IO completions follow the guidelines as the |
| 512 |
* ones we set for inline and deferred completions. If none of those |
| 513 |
* are available for this IO, clear the polled flag. |
| 514 |
*/ |
| 515 |
if (dio->flags & IOMAP_DIO_COMP_WORK) |
| 516 |
dio->iocb->ki_flags &= ~IOCB_HIPRI; |
| 517 |
|
| 518 |
if (need_zeroout) { |
| 519 |
/* zero out from the start of the block to the write offset */ |
| 520 |
pad = pos & (fs_block_size - 1); |
| 521 |
|
| 522 |
ret = iomap_dio_zero(iter, dio, pos - pad, pad); |
| 523 |
if (ret) |
| 524 |
goto out; |
| 525 |
} |
| 526 |
|
| 527 |
do { |
| 528 |
/* |
| 529 |
* If completions already occurred and reported errors, give up now and |
| 530 |
* don't bother submitting more bios. |
| 531 |
*/ |
| 532 |
if (unlikely(data_race(dio->error))) |
| 533 |
goto out; |
| 534 |
|
| 535 |
ret = iomap_dio_bio_iter_one(iter, dio, pos, alignment, bio_opf); |
| 536 |
if (unlikely(ret < 0)) { |
| 537 |
/* |
| 538 |
* We have to stop part way through an IO. We must fall |
| 539 |
* through to the sub-block tail zeroing here, otherwise |
| 540 |
* this short IO may expose stale data in the tail of |
| 541 |
* the block we haven't written data to. |
| 542 |
*/ |
| 543 |
break; |
| 544 |
} |
| 545 |
dio->size += ret; |
| 546 |
copied += ret; |
| 547 |
pos += ret; |
| 548 |
ret = 0; |
| 549 |
} while (iov_iter_count(dio->submit.iter)); |
| 550 |
|
| 551 |
/* |
| 552 |
* We need to zeroout the tail of a sub-block write if the extent type |
| 553 |
* requires zeroing or the write extends beyond EOF. If we don't zero |
| 554 |
* the block tail in the latter case, we can expose stale data via mmap |
| 555 |
* reads of the EOF block. |
| 556 |
*/ |
| 557 |
if (need_zeroout || |
| 558 |
((dio->flags & IOMAP_DIO_WRITE) && pos >= i_size_read(inode))) { |
| 559 |
/* zero out from the end of the write to the end of the block */ |
| 560 |
pad = pos & (fs_block_size - 1); |
| 561 |
if (pad) |
| 562 |
ret = iomap_dio_zero(iter, dio, pos, |
| 563 |
fs_block_size - pad); |
| 564 |
} |
| 565 |
out: |
| 566 |
/* Undo iter limitation to current extent */ |
| 567 |
iov_iter_reexpand(dio->submit.iter, orig_count - copied); |
| 568 |
if (copied) |
| 569 |
return iomap_iter_advance(iter, copied); |
| 570 |
return ret; |
| 571 |
} |
| 572 |
|
| 573 |
static int iomap_dio_hole_iter(struct iomap_iter *iter, struct iomap_dio *dio) |
| 574 |
{ |
| 575 |
loff_t length = iov_iter_zero(iomap_length(iter), dio->submit.iter); |
| 576 |
|
| 577 |
dio->size += length; |
| 578 |
if (!length) |
| 579 |
return -EFAULT; |
| 580 |
return iomap_iter_advance(iter, length); |
| 581 |
} |
| 582 |
|
| 583 |
static int iomap_dio_inline_iter(struct iomap_iter *iomi, struct iomap_dio *dio) |
| 584 |
{ |
| 585 |
const struct iomap *iomap = &iomi->iomap; |
| 586 |
struct iov_iter *iter = dio->submit.iter; |
| 587 |
void *inline_data = iomap_inline_data(iomap, iomi->pos); |
| 588 |
loff_t length = iomap_length(iomi); |
| 589 |
loff_t pos = iomi->pos; |
| 590 |
u64 copied; |
| 591 |
|
| 592 |
if (WARN_ON_ONCE(!inline_data)) |
| 593 |
return -EIO; |
| 594 |
|
| 595 |
if (WARN_ON_ONCE(!iomap_inline_data_valid(iomap))) |
| 596 |
return -EIO; |
| 597 |
|
| 598 |
if (dio->flags & IOMAP_DIO_WRITE) { |
| 599 |
loff_t size = iomi->inode->i_size; |
| 600 |
|
| 601 |
if (pos > size) |
| 602 |
memset(iomap_inline_data(iomap, size), 0, pos - size); |
| 603 |
copied = copy_from_iter(inline_data, length, iter); |
| 604 |
if (copied) { |
| 605 |
if (pos + copied > size) |
| 606 |
i_size_write(iomi->inode, pos + copied); |
| 607 |
mark_inode_dirty(iomi->inode); |
| 608 |
} |
| 609 |
} else { |
| 610 |
copied = copy_to_iter(inline_data, length, iter); |
| 611 |
} |
| 612 |
dio->size += copied; |
| 613 |
if (!copied) |
| 614 |
return -EFAULT; |
| 615 |
return iomap_iter_advance(iomi, copied); |
| 616 |
} |
| 617 |
|
| 618 |
static int iomap_dio_iter(struct iomap_iter *iter, struct iomap_dio *dio) |
| 619 |
{ |
| 620 |
switch (iter->iomap.type) { |
| 621 |
case IOMAP_HOLE: |
| 622 |
if (WARN_ON_ONCE(dio->flags & IOMAP_DIO_WRITE)) |
| 623 |
return -EIO; |
| 624 |
return iomap_dio_hole_iter(iter, dio); |
| 625 |
case IOMAP_UNWRITTEN: |
| 626 |
if (!(dio->flags & IOMAP_DIO_WRITE)) |
| 627 |
return iomap_dio_hole_iter(iter, dio); |
| 628 |
return iomap_dio_bio_iter(iter, dio); |
| 629 |
case IOMAP_MAPPED: |
| 630 |
return iomap_dio_bio_iter(iter, dio); |
| 631 |
case IOMAP_INLINE: |
| 632 |
return iomap_dio_inline_iter(iter, dio); |
| 633 |
case IOMAP_DELALLOC: |
| 634 |
/* |
| 635 |
* DIO is not serialised against mmap() access at all, and so |
| 636 |
* if the page_mkwrite occurs between the writeback and the |
| 637 |
* iomap_iter() call in the DIO path, then it will see the |
| 638 |
* DELALLOC block that the page-mkwrite allocated. |
| 639 |
*/ |
| 640 |
pr_warn_ratelimited("Direct I/O collision with buffered writes! File: %pD4 Comm: %.20s\n", |
| 641 |
dio->iocb->ki_filp, current->comm); |
| 642 |
return -EIO; |
| 643 |
default: |
| 644 |
WARN_ON_ONCE(1); |
| 645 |
return -EIO; |
| 646 |
} |
| 647 |
} |
| 648 |
|
| 649 |
/* |
| 650 |
* iomap_dio_rw() always completes O_[D]SYNC writes regardless of whether the IO |
| 651 |
* is being issued as AIO or not. This allows us to optimise pure data writes |
| 652 |
* to use REQ_FUA rather than requiring generic_write_sync() to issue a |
| 653 |
* REQ_FLUSH post write. This is slightly tricky because a single request here |
| 654 |
* can be mapped into multiple disjoint IOs and only a subset of the IOs issued |
| 655 |
* may be pure data writes. In that case, we still need to do a full data sync |
| 656 |
* completion. |
| 657 |
* |
| 658 |
* When page faults are disabled and @dio_flags includes IOMAP_DIO_PARTIAL, |
| 659 |
* __iomap_dio_rw can return a partial result if it encounters a non-resident |
| 660 |
* page in @iter after preparing a transfer. In that case, the non-resident |
| 661 |
* pages can be faulted in and the request resumed with @done_before set to the |
| 662 |
* number of bytes previously transferred. The request will then complete with |
| 663 |
* the correct total number of bytes transferred; this is essential for |
| 664 |
* completing partial requests asynchronously. |
| 665 |
* |
| 666 |
* Returns -ENOTBLK In case of a page invalidation invalidation failure for |
| 667 |
* writes. The callers needs to fall back to buffered I/O in this case. |
| 668 |
*/ |
| 669 |
struct iomap_dio * |
| 670 |
__iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter, |
| 671 |
const struct iomap_ops *ops, const struct iomap_dio_ops *dops, |
| 672 |
unsigned int dio_flags, void *private, size_t done_before) |
| 673 |
{ |
| 674 |
struct inode *inode = file_inode(iocb->ki_filp); |
| 675 |
struct iomap_iter iomi = { |
| 676 |
.inode = inode, |
| 677 |
.pos = iocb->ki_pos, |
| 678 |
.len = iov_iter_count(iter), |
| 679 |
.flags = IOMAP_DIRECT, |
| 680 |
.private = private, |
| 681 |
}; |
| 682 |
bool wait_for_completion = |
| 683 |
is_sync_kiocb(iocb) || (dio_flags & IOMAP_DIO_FORCE_WAIT); |
| 684 |
struct blk_plug plug; |
| 685 |
struct iomap_dio *dio; |
| 686 |
loff_t ret = 0; |
| 687 |
|
| 688 |
trace_iomap_dio_rw_begin(iocb, iter, dio_flags, done_before); |
| 689 |
|
| 690 |
if (!iomi.len) |
| 691 |
return NULL; |
| 692 |
|
| 693 |
dio = kmalloc_obj(*dio); |
| 694 |
if (!dio) |
| 695 |
return ERR_PTR(-ENOMEM); |
| 696 |
|
| 697 |
dio->iocb = iocb; |
| 698 |
atomic_set(&dio->ref, 1); |
| 699 |
dio->size = 0; |
| 700 |
dio->i_size = i_size_read(inode); |
| 701 |
dio->dops = dops; |
| 702 |
dio->error = 0; |
| 703 |
dio->flags = dio_flags & (IOMAP_DIO_FSBLOCK_ALIGNED | IOMAP_DIO_BOUNCE); |
| 704 |
dio->done_before = done_before; |
| 705 |
|
| 706 |
dio->submit.iter = iter; |
| 707 |
dio->submit.waiter = current; |
| 708 |
|
| 709 |
if (iocb->ki_flags & IOCB_NOWAIT) |
| 710 |
iomi.flags |= IOMAP_NOWAIT; |
| 711 |
|
| 712 |
if (iov_iter_rw(iter) == READ) { |
| 713 |
if (iomi.pos >= dio->i_size) |
| 714 |
goto out_free_dio; |
| 715 |
|
| 716 |
if (user_backed_iter(iter)) |
| 717 |
dio->flags |= IOMAP_DIO_USER_BACKED; |
| 718 |
|
| 719 |
ret = kiocb_write_and_wait(iocb, iomi.len); |
| 720 |
if (ret) |
| 721 |
goto out_free_dio; |
| 722 |
} else { |
| 723 |
iomi.flags |= IOMAP_WRITE; |
| 724 |
dio->flags |= IOMAP_DIO_WRITE; |
| 725 |
|
| 726 |
if (dio_flags & IOMAP_DIO_OVERWRITE_ONLY) { |
| 727 |
ret = -EAGAIN; |
| 728 |
if (iomi.pos >= dio->i_size || |
| 729 |
iomi.pos + iomi.len > dio->i_size) |
| 730 |
goto out_free_dio; |
| 731 |
iomi.flags |= IOMAP_OVERWRITE_ONLY; |
| 732 |
} |
| 733 |
|
| 734 |
if (iocb->ki_flags & IOCB_ATOMIC) |
| 735 |
iomi.flags |= IOMAP_ATOMIC; |
| 736 |
|
| 737 |
/* for data sync or sync, we need sync completion processing */ |
| 738 |
if (iocb_is_dsync(iocb)) { |
| 739 |
dio->flags |= IOMAP_DIO_NEED_SYNC; |
| 740 |
|
| 741 |
/* |
| 742 |
* For datasync only writes, we optimistically try using |
| 743 |
* WRITE_THROUGH for this IO. This flag requires either |
| 744 |
* FUA writes through the device's write cache, or a |
| 745 |
* normal write to a device without a volatile write |
| 746 |
* cache. For the former, Any non-FUA write that occurs |
| 747 |
* will clear this flag, hence we know before completion |
| 748 |
* whether a cache flush is necessary. |
| 749 |
*/ |
| 750 |
if (!(iocb->ki_flags & IOCB_SYNC)) |
| 751 |
dio->flags |= IOMAP_DIO_WRITE_THROUGH; |
| 752 |
} |
| 753 |
|
| 754 |
/* |
| 755 |
* i_size updates must to happen from process context. |
| 756 |
*/ |
| 757 |
if (iomi.pos + iomi.len > dio->i_size) |
| 758 |
dio->flags |= IOMAP_DIO_COMP_WORK; |
| 759 |
|
| 760 |
/* |
| 761 |
* Try to invalidate cache pages for the range we are writing. |
| 762 |
* If this invalidation fails, let the caller fall back to |
| 763 |
* buffered I/O. |
| 764 |
*/ |
| 765 |
ret = kiocb_invalidate_pages(iocb, iomi.len); |
| 766 |
if (ret) { |
| 767 |
if (ret != -EAGAIN) { |
| 768 |
trace_iomap_dio_invalidate_fail(inode, iomi.pos, |
| 769 |
iomi.len); |
| 770 |
if (iocb->ki_flags & IOCB_ATOMIC) { |
| 771 |
/* |
| 772 |
* folio invalidation failed, maybe |
| 773 |
* this is transient, unlock and see if |
| 774 |
* the caller tries again. |
| 775 |
*/ |
| 776 |
ret = -EAGAIN; |
| 777 |
} else { |
| 778 |
/* fall back to buffered write */ |
| 779 |
ret = -ENOTBLK; |
| 780 |
} |
| 781 |
} |
| 782 |
goto out_free_dio; |
| 783 |
} |
| 784 |
} |
| 785 |
|
| 786 |
if (!wait_for_completion && !inode->i_sb->s_dio_done_wq) { |
| 787 |
ret = sb_init_dio_done_wq(inode->i_sb); |
| 788 |
if (ret < 0) |
| 789 |
goto out_free_dio; |
| 790 |
} |
| 791 |
|
| 792 |
inode_dio_begin(inode); |
| 793 |
|
| 794 |
blk_start_plug(&plug); |
| 795 |
while ((ret = iomap_iter(&iomi, ops)) > 0) { |
| 796 |
iomi.status = iomap_dio_iter(&iomi, dio); |
| 797 |
|
| 798 |
/* |
| 799 |
* We can only poll for single bio I/Os. |
| 800 |
*/ |
| 801 |
iocb->ki_flags &= ~IOCB_HIPRI; |
| 802 |
} |
| 803 |
|
| 804 |
blk_finish_plug(&plug); |
| 805 |
|
| 806 |
/* |
| 807 |
* We only report that we've read data up to i_size. |
| 808 |
* Revert iter to a state corresponding to that as some callers (such |
| 809 |
* as the splice code) rely on it. |
| 810 |
*/ |
| 811 |
if (iov_iter_rw(iter) == READ && iomi.pos >= dio->i_size) |
| 812 |
iov_iter_revert(iter, iomi.pos - dio->i_size); |
| 813 |
|
| 814 |
if (ret == -EFAULT && dio->size && (dio_flags & IOMAP_DIO_PARTIAL)) { |
| 815 |
if (!(iocb->ki_flags & IOCB_NOWAIT)) |
| 816 |
wait_for_completion = true; |
| 817 |
ret = 0; |
| 818 |
} |
| 819 |
|
| 820 |
/* magic error code to fall back to buffered I/O */ |
| 821 |
if (ret == -ENOTBLK) { |
| 822 |
wait_for_completion = true; |
| 823 |
ret = 0; |
| 824 |
} |
| 825 |
if (ret < 0) |
| 826 |
iomap_dio_set_error(dio, ret); |
| 827 |
|
| 828 |
/* |
| 829 |
* If all the writes we issued were already written through to the |
| 830 |
* media, we don't need to flush the cache on IO completion. Clear the |
| 831 |
* sync flag for this case. |
| 832 |
* |
| 833 |
* Otherwise clear the inline completion flag if any sync work is |
| 834 |
* needed, as that needs to be performed from process context. |
| 835 |
*/ |
| 836 |
if (dio->flags & IOMAP_DIO_WRITE_THROUGH) |
| 837 |
dio->flags &= ~IOMAP_DIO_NEED_SYNC; |
| 838 |
else if (dio->flags & IOMAP_DIO_NEED_SYNC) |
| 839 |
dio->flags |= IOMAP_DIO_COMP_WORK; |
| 840 |
|
| 841 |
/* |
| 842 |
* We are about to drop our additional submission reference, which |
| 843 |
* might be the last reference to the dio. There are three different |
| 844 |
* ways we can progress here: |
| 845 |
* |
| 846 |
* (a) If this is the last reference we will always complete and free |
| 847 |
* the dio ourselves. |
| 848 |
* (b) If this is not the last reference, and we serve an asynchronous |
| 849 |
* iocb, we must never touch the dio after the decrement, the |
| 850 |
* I/O completion handler will complete and free it. |
| 851 |
* (c) If this is not the last reference, but we serve a synchronous |
| 852 |
* iocb, the I/O completion handler will wake us up on the drop |
| 853 |
* of the final reference, and we will complete and free it here |
| 854 |
* after we got woken by the I/O completion handler. |
| 855 |
*/ |
| 856 |
dio->wait_for_completion = wait_for_completion; |
| 857 |
if (!atomic_dec_and_test(&dio->ref)) { |
| 858 |
if (!wait_for_completion) { |
| 859 |
trace_iomap_dio_rw_queued(inode, iomi.pos, iomi.len); |
| 860 |
return ERR_PTR(-EIOCBQUEUED); |
| 861 |
} |
| 862 |
|
| 863 |
for (;;) { |
| 864 |
set_current_state(TASK_UNINTERRUPTIBLE); |
| 865 |
if (!READ_ONCE(dio->submit.waiter)) |
| 866 |
break; |
| 867 |
|
| 868 |
blk_io_schedule(); |
| 869 |
} |
| 870 |
__set_current_state(TASK_RUNNING); |
| 871 |
} |
| 872 |
|
| 873 |
return dio; |
| 874 |
|
| 875 |
out_free_dio: |
| 876 |
kfree(dio); |
| 877 |
if (ret) |
| 878 |
return ERR_PTR(ret); |
| 879 |
return NULL; |
| 880 |
} |
| 881 |
EXPORT_SYMBOL_GPL(__iomap_dio_rw); |
| 882 |
|
| 883 |
ssize_t |
| 884 |
iomap_dio_rw(struct kiocb *iocb, struct iov_iter *iter, |
| 885 |
const struct iomap_ops *ops, const struct iomap_dio_ops *dops, |
| 886 |
unsigned int dio_flags, void *private, size_t done_before) |
| 887 |
{ |
| 888 |
struct iomap_dio *dio; |
| 889 |
|
| 890 |
dio = __iomap_dio_rw(iocb, iter, ops, dops, dio_flags, private, |
| 891 |
done_before); |
| 892 |
if (IS_ERR_OR_NULL(dio)) |
| 893 |
return PTR_ERR_OR_ZERO(dio); |
| 894 |
return iomap_dio_complete(dio); |
| 895 |
} |
| 896 |
EXPORT_SYMBOL_GPL(iomap_dio_rw); |