1 // SPDX-License-Identifier: GPL-2.0
5 #include "alloc_foreground.h"
8 #include "fs-io-direct.h"
9 #include "fs-io-pagecache.h"
13 #include <linux/kthread.h>
14 #include <linux/pagemap.h>
15 #include <linux/prefetch.h>
16 #include <linux/task_io_accounting_ops.h>
25 struct bch_read_bio rbio;
28 static void bio_check_or_release(struct bio *bio, bool check_dirty)
31 bio_check_pages_dirty(bio);
33 bio_release_pages(bio, false);
38 static void bch2_dio_read_complete(struct closure *cl)
40 struct dio_read *dio = container_of(cl, struct dio_read, cl);
42 dio->req->ki_complete(dio->req, dio->ret);
43 bio_check_or_release(&dio->rbio.bio, dio->should_dirty);
46 static void bch2_direct_IO_read_endio(struct bio *bio)
48 struct dio_read *dio = bio->bi_private;
51 dio->ret = blk_status_to_errno(bio->bi_status);
53 closure_put(&dio->cl);
56 static void bch2_direct_IO_read_split_endio(struct bio *bio)
58 struct dio_read *dio = bio->bi_private;
59 bool should_dirty = dio->should_dirty;
61 bch2_direct_IO_read_endio(bio);
62 bio_check_or_release(bio, should_dirty);
65 static int bch2_direct_IO_read(struct kiocb *req, struct iov_iter *iter)
67 struct file *file = req->ki_filp;
68 struct bch_inode_info *inode = file_bch_inode(file);
69 struct bch_fs *c = inode->v.i_sb->s_fs_info;
70 struct bch_io_opts opts;
73 loff_t offset = req->ki_pos;
74 bool sync = is_sync_kiocb(req);
78 bch2_inode_opts_get(&opts, c, &inode->ei_inode);
80 if ((offset|iter->count) & (block_bytes(c) - 1))
83 ret = min_t(loff_t, iter->count,
84 max_t(loff_t, 0, i_size_read(&inode->v) - offset));
89 shorten = iov_iter_count(iter) - round_up(ret, block_bytes(c));
90 iter->count -= shorten;
92 bio = bio_alloc_bioset(NULL,
93 bio_iov_vecs_to_alloc(iter, BIO_MAX_VECS),
98 bio->bi_end_io = bch2_direct_IO_read_endio;
100 dio = container_of(bio, struct dio_read, rbio.bio);
101 closure_init(&dio->cl, NULL);
104 * this is a _really_ horrible hack just to avoid an atomic sub at the
108 set_closure_fn(&dio->cl, bch2_dio_read_complete, NULL);
109 atomic_set(&dio->cl.remaining,
110 CLOSURE_REMAINING_INITIALIZER -
114 atomic_set(&dio->cl.remaining,
115 CLOSURE_REMAINING_INITIALIZER + 1);
116 dio->cl.closure_get_happened = true;
122 * This is one of the sketchier things I've encountered: we have to skip
123 * the dirtying of requests that are internal from the kernel (i.e. from
124 * loopback), because we'll deadlock on page_lock.
126 dio->should_dirty = iter_is_iovec(iter);
129 while (iter->count) {
130 bio = bio_alloc_bioset(NULL,
131 bio_iov_vecs_to_alloc(iter, BIO_MAX_VECS),
135 bio->bi_end_io = bch2_direct_IO_read_split_endio;
137 bio->bi_opf = REQ_OP_READ|REQ_SYNC;
138 bio->bi_iter.bi_sector = offset >> 9;
139 bio->bi_private = dio;
141 ret = bio_iov_iter_get_pages(bio, iter);
143 /* XXX: fault inject this path */
144 bio->bi_status = BLK_STS_RESOURCE;
149 offset += bio->bi_iter.bi_size;
151 if (dio->should_dirty)
152 bio_set_pages_dirty(bio);
155 closure_get(&dio->cl);
157 bch2_read(c, rbio_init(bio, opts), inode_inum(inode));
160 iter->count += shorten;
163 closure_sync(&dio->cl);
164 closure_debug_destroy(&dio->cl);
166 bio_check_or_release(&dio->rbio.bio, dio->should_dirty);
173 ssize_t bch2_read_iter(struct kiocb *iocb, struct iov_iter *iter)
175 struct file *file = iocb->ki_filp;
176 struct bch_inode_info *inode = file_bch_inode(file);
177 struct address_space *mapping = file->f_mapping;
178 size_t count = iov_iter_count(iter);
182 return 0; /* skip atime */
184 if (iocb->ki_flags & IOCB_DIRECT) {
185 struct blk_plug plug;
187 if (unlikely(mapping->nrpages)) {
188 ret = filemap_write_and_wait_range(mapping,
190 iocb->ki_pos + count - 1);
197 blk_start_plug(&plug);
198 ret = bch2_direct_IO_read(iocb, iter);
199 blk_finish_plug(&plug);
204 bch2_pagecache_add_get(inode);
205 ret = generic_file_read_iter(iocb, iter);
206 bch2_pagecache_add_put(inode);
209 return bch2_err_class(ret);
212 /* O_DIRECT writes */
216 struct address_space *mapping;
217 struct bch_inode_info *inode;
218 struct mm_struct *mm;
224 struct quota_res quota_res;
227 struct iov_iter iter;
228 struct iovec inline_vecs[2];
231 struct bch_write_op op;
234 static bool bch2_check_range_allocated(struct bch_fs *c, subvol_inum inum,
235 u64 offset, u64 size,
236 unsigned nr_replicas, bool compressed)
238 struct btree_trans *trans = bch2_trans_get(c);
239 struct btree_iter iter;
241 u64 end = offset + size;
246 bch2_trans_begin(trans);
248 err = bch2_subvolume_get_snapshot(trans, inum.subvol, &snapshot);
252 for_each_btree_key_norestart(trans, iter, BTREE_ID_extents,
253 SPOS(inum.inum, offset, snapshot),
254 BTREE_ITER_SLOTS, k, err) {
255 if (bkey_ge(bkey_start_pos(k.k), POS(inum.inum, end)))
258 if (k.k->p.snapshot != snapshot ||
259 nr_replicas > bch2_bkey_replicas(c, k) ||
260 (!compressed && bch2_bkey_sectors_compressed(k))) {
266 offset = iter.pos.offset;
267 bch2_trans_iter_exit(trans, &iter);
269 if (bch2_err_matches(err, BCH_ERR_transaction_restart))
271 bch2_trans_put(trans);
273 return err ? false : ret;
276 static noinline bool bch2_dio_write_check_allocated(struct dio_write *dio)
278 struct bch_fs *c = dio->op.c;
279 struct bch_inode_info *inode = dio->inode;
280 struct bio *bio = &dio->op.wbio.bio;
282 return bch2_check_range_allocated(c, inode_inum(inode),
283 dio->op.pos.offset, bio_sectors(bio),
284 dio->op.opts.data_replicas,
285 dio->op.opts.compression != 0);
288 static void bch2_dio_write_loop_async(struct bch_write_op *);
289 static __always_inline long bch2_dio_write_done(struct dio_write *dio);
292 * We're going to return -EIOCBQUEUED, but we haven't finished consuming the
293 * iov_iter yet, so we need to stash a copy of the iovec: it might be on the
294 * caller's stack, we're not guaranteed that it will live for the duration of
297 static noinline int bch2_dio_write_copy_iov(struct dio_write *dio)
299 struct iovec *iov = dio->inline_vecs;
302 * iov_iter has a single embedded iovec - nothing to do:
304 if (iter_is_ubuf(&dio->iter))
308 * We don't currently handle non-iovec iov_iters here - return an error,
309 * and we'll fall back to doing the IO synchronously:
311 if (!iter_is_iovec(&dio->iter))
314 if (dio->iter.nr_segs > ARRAY_SIZE(dio->inline_vecs)) {
315 iov = kmalloc_array(dio->iter.nr_segs, sizeof(*iov),
320 dio->free_iov = true;
323 memcpy(iov, dio->iter.__iov, dio->iter.nr_segs * sizeof(*iov));
324 dio->iter.__iov = iov;
328 static void bch2_dio_write_flush_done(struct closure *cl)
330 struct dio_write *dio = container_of(cl, struct dio_write, op.cl);
331 struct bch_fs *c = dio->op.c;
333 closure_debug_destroy(cl);
335 dio->op.error = bch2_journal_error(&c->journal);
337 bch2_dio_write_done(dio);
340 static noinline void bch2_dio_write_flush(struct dio_write *dio)
342 struct bch_fs *c = dio->op.c;
343 struct bch_inode_unpacked inode;
348 closure_init(&dio->op.cl, NULL);
350 if (!dio->op.error) {
351 ret = bch2_inode_find_by_inum(c, inode_inum(dio->inode), &inode);
355 bch2_journal_flush_seq_async(&c->journal, inode.bi_journal_seq,
357 bch2_inode_flush_nocow_writes_async(c, dio->inode, &dio->op.cl);
362 closure_sync(&dio->op.cl);
363 closure_debug_destroy(&dio->op.cl);
365 continue_at(&dio->op.cl, bch2_dio_write_flush_done, NULL);
369 static __always_inline long bch2_dio_write_done(struct dio_write *dio)
371 struct kiocb *req = dio->req;
372 struct bch_inode_info *inode = dio->inode;
373 bool sync = dio->sync;
376 if (unlikely(dio->flush)) {
377 bch2_dio_write_flush(dio);
382 bch2_pagecache_block_put(inode);
385 kfree(dio->iter.__iov);
387 ret = dio->op.error ?: ((long) dio->written << 9);
388 bio_put(&dio->op.wbio.bio);
390 /* inode->i_dio_count is our ref on inode and thus bch_fs */
391 inode_dio_end(&inode->v);
394 ret = bch2_err_class(ret);
397 req->ki_complete(req, ret);
403 static __always_inline void bch2_dio_write_end(struct dio_write *dio)
405 struct bch_fs *c = dio->op.c;
406 struct kiocb *req = dio->req;
407 struct bch_inode_info *inode = dio->inode;
408 struct bio *bio = &dio->op.wbio.bio;
410 req->ki_pos += (u64) dio->op.written << 9;
411 dio->written += dio->op.written;
413 if (dio->extending) {
414 spin_lock(&inode->v.i_lock);
415 if (req->ki_pos > inode->v.i_size)
416 i_size_write(&inode->v, req->ki_pos);
417 spin_unlock(&inode->v.i_lock);
420 if (dio->op.i_sectors_delta || dio->quota_res.sectors) {
421 mutex_lock(&inode->ei_quota_lock);
422 __bch2_i_sectors_acct(c, inode, &dio->quota_res, dio->op.i_sectors_delta);
423 __bch2_quota_reservation_put(c, inode, &dio->quota_res);
424 mutex_unlock(&inode->ei_quota_lock);
427 bio_release_pages(bio, false);
429 if (unlikely(dio->op.error))
430 set_bit(EI_INODE_ERROR, &inode->ei_flags);
433 static __always_inline long bch2_dio_write_loop(struct dio_write *dio)
435 struct bch_fs *c = dio->op.c;
436 struct kiocb *req = dio->req;
437 struct address_space *mapping = dio->mapping;
438 struct bch_inode_info *inode = dio->inode;
439 struct bch_io_opts opts;
440 struct bio *bio = &dio->op.wbio.bio;
441 unsigned unaligned, iter_count;
442 bool sync = dio->sync, dropped_locks;
445 bch2_inode_opts_get(&opts, c, &inode->ei_inode);
448 iter_count = dio->iter.count;
450 EBUG_ON(current->faults_disabled_mapping);
451 current->faults_disabled_mapping = mapping;
453 ret = bio_iov_iter_get_pages(bio, &dio->iter);
455 dropped_locks = fdm_dropped_locks();
457 current->faults_disabled_mapping = NULL;
460 * If the fault handler returned an error but also signalled
461 * that it dropped & retook ei_pagecache_lock, we just need to
462 * re-shoot down the page cache and retry:
464 if (dropped_locks && ret)
467 if (unlikely(ret < 0))
470 if (unlikely(dropped_locks)) {
471 ret = bch2_write_invalidate_inode_pages_range(mapping,
473 req->ki_pos + iter_count - 1);
477 if (!bio->bi_iter.bi_size)
481 unaligned = bio->bi_iter.bi_size & (block_bytes(c) - 1);
482 bio->bi_iter.bi_size -= unaligned;
483 iov_iter_revert(&dio->iter, unaligned);
485 if (!bio->bi_iter.bi_size) {
487 * bio_iov_iter_get_pages was only able to get <
488 * blocksize worth of pages:
494 bch2_write_op_init(&dio->op, c, opts);
495 dio->op.end_io = sync
497 : bch2_dio_write_loop_async;
498 dio->op.target = dio->op.opts.foreground_target;
499 dio->op.write_point = writepoint_hashed((unsigned long) current);
500 dio->op.nr_replicas = dio->op.opts.data_replicas;
501 dio->op.subvol = inode->ei_subvol;
502 dio->op.pos = POS(inode->v.i_ino, (u64) req->ki_pos >> 9);
503 dio->op.devs_need_flush = &inode->ei_devs_need_flush;
506 dio->op.flags |= BCH_WRITE_SYNC;
507 dio->op.flags |= BCH_WRITE_CHECK_ENOSPC;
509 ret = bch2_quota_reservation_add(c, inode, &dio->quota_res,
510 bio_sectors(bio), true);
514 ret = bch2_disk_reservation_get(c, &dio->op.res, bio_sectors(bio),
515 dio->op.opts.data_replicas, 0);
517 !bch2_dio_write_check_allocated(dio))
520 task_io_account_write(bio->bi_iter.bi_size);
522 if (unlikely(dio->iter.count) &&
525 bch2_dio_write_copy_iov(dio))
526 dio->sync = sync = true;
529 closure_call(&dio->op.cl, bch2_write, NULL, NULL);
534 bch2_dio_write_end(dio);
536 if (likely(!dio->iter.count) || dio->op.error)
539 bio_reset(bio, NULL, REQ_OP_WRITE);
542 return bch2_dio_write_done(dio);
546 bio_release_pages(bio, false);
548 bch2_quota_reservation_put(c, inode, &dio->quota_res);
552 static noinline __cold void bch2_dio_write_continue(struct dio_write *dio)
554 struct mm_struct *mm = dio->mm;
556 bio_reset(&dio->op.wbio.bio, NULL, REQ_OP_WRITE);
560 bch2_dio_write_loop(dio);
562 kthread_unuse_mm(mm);
565 static void bch2_dio_write_loop_async(struct bch_write_op *op)
567 struct dio_write *dio = container_of(op, struct dio_write, op);
569 bch2_dio_write_end(dio);
571 if (likely(!dio->iter.count) || dio->op.error)
572 bch2_dio_write_done(dio);
574 bch2_dio_write_continue(dio);
577 ssize_t bch2_direct_write(struct kiocb *req, struct iov_iter *iter)
579 struct file *file = req->ki_filp;
580 struct address_space *mapping = file->f_mapping;
581 struct bch_inode_info *inode = file_bch_inode(file);
582 struct bch_fs *c = inode->v.i_sb->s_fs_info;
583 struct dio_write *dio;
585 bool locked = true, extending;
589 prefetch((void *) &c->opts + 64);
590 prefetch(&inode->ei_inode);
591 prefetch((void *) &inode->ei_inode + 64);
593 inode_lock(&inode->v);
595 ret = generic_write_checks(req, iter);
596 if (unlikely(ret <= 0))
599 ret = file_remove_privs(file);
603 ret = file_update_time(file);
607 if (unlikely((req->ki_pos|iter->count) & (block_bytes(c) - 1)))
610 inode_dio_begin(&inode->v);
611 bch2_pagecache_block_get(inode);
613 extending = req->ki_pos + iter->count > inode->v.i_size;
615 inode_unlock(&inode->v);
619 bio = bio_alloc_bioset(NULL,
620 bio_iov_vecs_to_alloc(iter, BIO_MAX_VECS),
623 &c->dio_write_bioset);
624 dio = container_of(bio, struct dio_write, op.wbio.bio);
626 dio->mapping = mapping;
628 dio->mm = current->mm;
630 dio->extending = extending;
631 dio->sync = is_sync_kiocb(req) || extending;
632 dio->flush = iocb_is_dsync(req) && !c->opts.journal_flush_disabled;
633 dio->free_iov = false;
634 dio->quota_res.sectors = 0;
639 if (unlikely(mapping->nrpages)) {
640 ret = bch2_write_invalidate_inode_pages_range(mapping,
642 req->ki_pos + iter->count - 1);
647 ret = bch2_dio_write_loop(dio);
650 inode_unlock(&inode->v);
653 bch2_pagecache_block_put(inode);
655 inode_dio_end(&inode->v);
659 void bch2_fs_fs_io_direct_exit(struct bch_fs *c)
661 bioset_exit(&c->dio_write_bioset);
662 bioset_exit(&c->dio_read_bioset);
665 int bch2_fs_fs_io_direct_init(struct bch_fs *c)
667 if (bioset_init(&c->dio_read_bioset,
668 4, offsetof(struct dio_read, rbio.bio),
670 return -BCH_ERR_ENOMEM_dio_read_bioset_init;
672 if (bioset_init(&c->dio_write_bioset,
673 4, offsetof(struct dio_write, op.wbio.bio),
675 return -BCH_ERR_ENOMEM_dio_write_bioset_init;
680 #endif /* NO_BCACHEFS_FS */