xref: /linux/fs/bcachefs/fs-io-direct.c (revision 4a4b30ea80d8cb5e8c4c62bb86201f4ea0d9b030)
1 // SPDX-License-Identifier: GPL-2.0
2 #ifndef NO_BCACHEFS_FS
3 
4 #include "bcachefs.h"
5 #include "alloc_foreground.h"
6 #include "fs.h"
7 #include "fs-io.h"
8 #include "fs-io-direct.h"
9 #include "fs-io-pagecache.h"
10 #include "io_read.h"
11 #include "io_write.h"
12 
13 #include <linux/kthread.h>
14 #include <linux/pagemap.h>
15 #include <linux/prefetch.h>
16 #include <linux/task_io_accounting_ops.h>
17 
18 /* O_DIRECT reads */
19 
20 struct dio_read {
21 	struct closure			cl;
22 	struct kiocb			*req;
23 	long				ret;
24 	bool				should_dirty;
25 	struct bch_read_bio		rbio;
26 };
27 
bio_check_or_release(struct bio * bio,bool check_dirty)28 static void bio_check_or_release(struct bio *bio, bool check_dirty)
29 {
30 	if (check_dirty) {
31 		bio_check_pages_dirty(bio);
32 	} else {
33 		bio_release_pages(bio, false);
34 		bio_put(bio);
35 	}
36 }
37 
CLOSURE_CALLBACK(bch2_dio_read_complete)38 static CLOSURE_CALLBACK(bch2_dio_read_complete)
39 {
40 	closure_type(dio, struct dio_read, cl);
41 
42 	dio->req->ki_complete(dio->req, dio->ret);
43 	bio_check_or_release(&dio->rbio.bio, dio->should_dirty);
44 }
45 
bch2_direct_IO_read_endio(struct bio * bio)46 static void bch2_direct_IO_read_endio(struct bio *bio)
47 {
48 	struct dio_read *dio = bio->bi_private;
49 
50 	if (bio->bi_status)
51 		dio->ret = blk_status_to_errno(bio->bi_status);
52 
53 	closure_put(&dio->cl);
54 }
55 
bch2_direct_IO_read_split_endio(struct bio * bio)56 static void bch2_direct_IO_read_split_endio(struct bio *bio)
57 {
58 	struct dio_read *dio = bio->bi_private;
59 	bool should_dirty = dio->should_dirty;
60 
61 	bch2_direct_IO_read_endio(bio);
62 	bio_check_or_release(bio, should_dirty);
63 }
64 
bch2_direct_IO_read(struct kiocb * req,struct iov_iter * iter)65 static int bch2_direct_IO_read(struct kiocb *req, struct iov_iter *iter)
66 {
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;
71 	struct dio_read *dio;
72 	struct bio *bio;
73 	struct blk_plug plug;
74 	loff_t offset = req->ki_pos;
75 	bool sync = is_sync_kiocb(req);
76 	bool split = false;
77 	size_t shorten;
78 	ssize_t ret;
79 
80 	bch2_inode_opts_get(&opts, c, &inode->ei_inode);
81 
82 	/* bios must be 512 byte aligned: */
83 	if ((offset|iter->count) & (SECTOR_SIZE - 1))
84 		return -EINVAL;
85 
86 	ret = min_t(loff_t, iter->count,
87 		    max_t(loff_t, 0, i_size_read(&inode->v) - offset));
88 
89 	if (!ret)
90 		return ret;
91 
92 	shorten = iov_iter_count(iter) - round_up(ret, block_bytes(c));
93 	if (shorten >= iter->count)
94 		shorten = 0;
95 	iter->count -= shorten;
96 
97 	bio = bio_alloc_bioset(NULL,
98 			       bio_iov_vecs_to_alloc(iter, BIO_MAX_VECS),
99 			       REQ_OP_READ,
100 			       GFP_KERNEL,
101 			       &c->dio_read_bioset);
102 
103 	dio = container_of(bio, struct dio_read, rbio.bio);
104 	closure_init(&dio->cl, NULL);
105 
106 	/*
107 	 * this is a _really_ horrible hack just to avoid an atomic sub at the
108 	 * end:
109 	 */
110 	if (!sync) {
111 		set_closure_fn(&dio->cl, bch2_dio_read_complete, NULL);
112 		atomic_set(&dio->cl.remaining,
113 			   CLOSURE_REMAINING_INITIALIZER -
114 			   CLOSURE_RUNNING +
115 			   CLOSURE_DESTRUCTOR);
116 	} else {
117 		atomic_set(&dio->cl.remaining,
118 			   CLOSURE_REMAINING_INITIALIZER + 1);
119 		dio->cl.closure_get_happened = true;
120 	}
121 
122 	dio->req	= req;
123 	dio->ret	= ret;
124 	/*
125 	 * This is one of the sketchier things I've encountered: we have to skip
126 	 * the dirtying of requests that are internal from the kernel (i.e. from
127 	 * loopback), because we'll deadlock on page_lock.
128 	 */
129 	dio->should_dirty = iter_is_iovec(iter);
130 
131 	blk_start_plug(&plug);
132 
133 	goto start;
134 	while (iter->count) {
135 		split = true;
136 
137 		bio = bio_alloc_bioset(NULL,
138 				       bio_iov_vecs_to_alloc(iter, BIO_MAX_VECS),
139 				       REQ_OP_READ,
140 				       GFP_KERNEL,
141 				       &c->bio_read);
142 start:
143 		bio->bi_opf		= REQ_OP_READ|REQ_SYNC;
144 		bio->bi_iter.bi_sector	= offset >> 9;
145 		bio->bi_private		= dio;
146 
147 		ret = bio_iov_iter_get_pages(bio, iter);
148 		if (ret < 0) {
149 			/* XXX: fault inject this path */
150 			bio->bi_status = BLK_STS_RESOURCE;
151 			bio_endio(bio);
152 			break;
153 		}
154 
155 		offset += bio->bi_iter.bi_size;
156 
157 		if (dio->should_dirty)
158 			bio_set_pages_dirty(bio);
159 
160 		if (iter->count)
161 			closure_get(&dio->cl);
162 
163 		struct bch_read_bio *rbio =
164 			rbio_init(bio,
165 				  c,
166 				  opts,
167 				  split
168 				  ? bch2_direct_IO_read_split_endio
169 				  : bch2_direct_IO_read_endio);
170 
171 		bch2_read(c, rbio, inode_inum(inode));
172 	}
173 
174 	blk_finish_plug(&plug);
175 
176 	iter->count += shorten;
177 
178 	if (sync) {
179 		closure_sync(&dio->cl);
180 		closure_debug_destroy(&dio->cl);
181 		ret = dio->ret;
182 		bio_check_or_release(&dio->rbio.bio, dio->should_dirty);
183 		return ret;
184 	} else {
185 		return -EIOCBQUEUED;
186 	}
187 }
188 
bch2_read_iter(struct kiocb * iocb,struct iov_iter * iter)189 ssize_t bch2_read_iter(struct kiocb *iocb, struct iov_iter *iter)
190 {
191 	struct file *file = iocb->ki_filp;
192 	struct bch_inode_info *inode = file_bch_inode(file);
193 	struct address_space *mapping = file->f_mapping;
194 	size_t count = iov_iter_count(iter);
195 	ssize_t ret = 0;
196 
197 	if (!count)
198 		return 0; /* skip atime */
199 
200 	if (iocb->ki_flags & IOCB_DIRECT) {
201 		struct blk_plug plug;
202 
203 		if (unlikely(mapping->nrpages)) {
204 			ret = filemap_write_and_wait_range(mapping,
205 						iocb->ki_pos,
206 						iocb->ki_pos + count - 1);
207 			if (ret < 0)
208 				goto out;
209 		}
210 
211 		file_accessed(file);
212 
213 		blk_start_plug(&plug);
214 		ret = bch2_direct_IO_read(iocb, iter);
215 		blk_finish_plug(&plug);
216 
217 		if (ret >= 0)
218 			iocb->ki_pos += ret;
219 	} else {
220 		bch2_pagecache_add_get(inode);
221 		ret = filemap_read(iocb, iter, ret);
222 		bch2_pagecache_add_put(inode);
223 	}
224 out:
225 	return bch2_err_class(ret);
226 }
227 
228 /* O_DIRECT writes */
229 
230 struct dio_write {
231 	struct kiocb			*req;
232 	struct address_space		*mapping;
233 	struct bch_inode_info		*inode;
234 	struct mm_struct		*mm;
235 	const struct iovec		*iov;
236 	unsigned			loop:1,
237 					extending:1,
238 					sync:1,
239 					flush:1;
240 	struct quota_res		quota_res;
241 	u64				written;
242 
243 	struct iov_iter			iter;
244 	struct iovec			inline_vecs[2];
245 
246 	/* must be last: */
247 	struct bch_write_op		op;
248 };
249 
bch2_check_range_allocated(struct bch_fs * c,subvol_inum inum,u64 offset,u64 size,unsigned nr_replicas,bool compressed)250 static bool bch2_check_range_allocated(struct bch_fs *c, subvol_inum inum,
251 				       u64 offset, u64 size,
252 				       unsigned nr_replicas, bool compressed)
253 {
254 	struct btree_trans *trans = bch2_trans_get(c);
255 	struct btree_iter iter;
256 	struct bkey_s_c k;
257 	u64 end = offset + size;
258 	u32 snapshot;
259 	bool ret = true;
260 	int err;
261 retry:
262 	bch2_trans_begin(trans);
263 
264 	err = bch2_subvolume_get_snapshot(trans, inum.subvol, &snapshot);
265 	if (err)
266 		goto err;
267 
268 	for_each_btree_key_norestart(trans, iter, BTREE_ID_extents,
269 			   SPOS(inum.inum, offset, snapshot),
270 			   BTREE_ITER_slots, k, err) {
271 		if (bkey_ge(bkey_start_pos(k.k), POS(inum.inum, end)))
272 			break;
273 
274 		if (k.k->p.snapshot != snapshot ||
275 		    nr_replicas > bch2_bkey_replicas(c, k) ||
276 		    (!compressed && bch2_bkey_sectors_compressed(k))) {
277 			ret = false;
278 			break;
279 		}
280 	}
281 
282 	offset = iter.pos.offset;
283 	bch2_trans_iter_exit(trans, &iter);
284 err:
285 	if (bch2_err_matches(err, BCH_ERR_transaction_restart))
286 		goto retry;
287 	bch2_trans_put(trans);
288 
289 	return err ? false : ret;
290 }
291 
bch2_dio_write_check_allocated(struct dio_write * dio)292 static noinline bool bch2_dio_write_check_allocated(struct dio_write *dio)
293 {
294 	struct bch_fs *c = dio->op.c;
295 	struct bch_inode_info *inode = dio->inode;
296 	struct bio *bio = &dio->op.wbio.bio;
297 
298 	return bch2_check_range_allocated(c, inode_inum(inode),
299 				dio->op.pos.offset, bio_sectors(bio),
300 				dio->op.opts.data_replicas,
301 				dio->op.opts.compression != 0);
302 }
303 
304 static void bch2_dio_write_loop_async(struct bch_write_op *);
305 static __always_inline long bch2_dio_write_done(struct dio_write *dio);
306 
307 /*
308  * We're going to return -EIOCBQUEUED, but we haven't finished consuming the
309  * iov_iter yet, so we need to stash a copy of the iovec: it might be on the
310  * caller's stack, we're not guaranteed that it will live for the duration of
311  * the IO:
312  */
bch2_dio_write_copy_iov(struct dio_write * dio)313 static noinline int bch2_dio_write_copy_iov(struct dio_write *dio)
314 {
315 	struct iovec *iov = dio->inline_vecs;
316 
317 	/*
318 	 * iov_iter has a single embedded iovec - nothing to do:
319 	 */
320 	if (iter_is_ubuf(&dio->iter))
321 		return 0;
322 
323 	/*
324 	 * We don't currently handle non-iovec iov_iters here - return an error,
325 	 * and we'll fall back to doing the IO synchronously:
326 	 */
327 	if (!iter_is_iovec(&dio->iter))
328 		return -1;
329 
330 	if (dio->iter.nr_segs > ARRAY_SIZE(dio->inline_vecs)) {
331 		dio->iov = iov = kmalloc_array(dio->iter.nr_segs, sizeof(*iov),
332 				    GFP_KERNEL);
333 		if (unlikely(!iov))
334 			return -ENOMEM;
335 	}
336 
337 	memcpy(iov, dio->iter.__iov, dio->iter.nr_segs * sizeof(*iov));
338 	dio->iter.__iov = iov;
339 	return 0;
340 }
341 
CLOSURE_CALLBACK(bch2_dio_write_flush_done)342 static CLOSURE_CALLBACK(bch2_dio_write_flush_done)
343 {
344 	closure_type(dio, struct dio_write, op.cl);
345 	struct bch_fs *c = dio->op.c;
346 
347 	closure_debug_destroy(cl);
348 
349 	dio->op.error = bch2_journal_error(&c->journal);
350 
351 	bch2_dio_write_done(dio);
352 }
353 
bch2_dio_write_flush(struct dio_write * dio)354 static noinline void bch2_dio_write_flush(struct dio_write *dio)
355 {
356 	struct bch_fs *c = dio->op.c;
357 	struct bch_inode_unpacked inode;
358 	int ret;
359 
360 	dio->flush = 0;
361 
362 	closure_init(&dio->op.cl, NULL);
363 
364 	if (!dio->op.error) {
365 		ret = bch2_inode_find_by_inum(c, inode_inum(dio->inode), &inode);
366 		if (ret) {
367 			dio->op.error = ret;
368 		} else {
369 			bch2_journal_flush_seq_async(&c->journal, inode.bi_journal_seq,
370 						     &dio->op.cl);
371 			bch2_inode_flush_nocow_writes_async(c, dio->inode, &dio->op.cl);
372 		}
373 	}
374 
375 	if (dio->sync) {
376 		closure_sync(&dio->op.cl);
377 		closure_debug_destroy(&dio->op.cl);
378 	} else {
379 		continue_at(&dio->op.cl, bch2_dio_write_flush_done, NULL);
380 	}
381 }
382 
bch2_dio_write_done(struct dio_write * dio)383 static __always_inline long bch2_dio_write_done(struct dio_write *dio)
384 {
385 	struct bch_fs *c = dio->op.c;
386 	struct kiocb *req = dio->req;
387 	struct bch_inode_info *inode = dio->inode;
388 	bool sync = dio->sync;
389 	long ret;
390 
391 	if (unlikely(dio->flush)) {
392 		bch2_dio_write_flush(dio);
393 		if (!sync)
394 			return -EIOCBQUEUED;
395 	}
396 
397 	bch2_pagecache_block_put(inode);
398 
399 	kfree(dio->iov);
400 
401 	ret = dio->op.error ?: ((long) dio->written << 9);
402 	bio_put(&dio->op.wbio.bio);
403 
404 	bch2_write_ref_put(c, BCH_WRITE_REF_dio_write);
405 
406 	/* inode->i_dio_count is our ref on inode and thus bch_fs */
407 	inode_dio_end(&inode->v);
408 
409 	if (ret < 0)
410 		ret = bch2_err_class(ret);
411 
412 	if (!sync) {
413 		req->ki_complete(req, ret);
414 		ret = -EIOCBQUEUED;
415 	}
416 	return ret;
417 }
418 
bch2_dio_write_end(struct dio_write * dio)419 static __always_inline void bch2_dio_write_end(struct dio_write *dio)
420 {
421 	struct bch_fs *c = dio->op.c;
422 	struct kiocb *req = dio->req;
423 	struct bch_inode_info *inode = dio->inode;
424 	struct bio *bio = &dio->op.wbio.bio;
425 
426 	req->ki_pos	+= (u64) dio->op.written << 9;
427 	dio->written	+= dio->op.written;
428 
429 	if (dio->extending) {
430 		spin_lock(&inode->v.i_lock);
431 		if (req->ki_pos > inode->v.i_size)
432 			i_size_write(&inode->v, req->ki_pos);
433 		spin_unlock(&inode->v.i_lock);
434 	}
435 
436 	if (dio->op.i_sectors_delta || dio->quota_res.sectors) {
437 		mutex_lock(&inode->ei_quota_lock);
438 		__bch2_i_sectors_acct(c, inode, &dio->quota_res, dio->op.i_sectors_delta);
439 		__bch2_quota_reservation_put(c, inode, &dio->quota_res);
440 		mutex_unlock(&inode->ei_quota_lock);
441 	}
442 
443 	bio_release_pages(bio, false);
444 
445 	if (unlikely(dio->op.error))
446 		set_bit(EI_INODE_ERROR, &inode->ei_flags);
447 }
448 
bch2_dio_write_loop(struct dio_write * dio)449 static __always_inline long bch2_dio_write_loop(struct dio_write *dio)
450 {
451 	struct bch_fs *c = dio->op.c;
452 	struct kiocb *req = dio->req;
453 	struct address_space *mapping = dio->mapping;
454 	struct bch_inode_info *inode = dio->inode;
455 	struct bch_io_opts opts;
456 	struct bio *bio = &dio->op.wbio.bio;
457 	unsigned unaligned, iter_count;
458 	bool sync = dio->sync, dropped_locks;
459 	long ret;
460 
461 	bch2_inode_opts_get(&opts, c, &inode->ei_inode);
462 
463 	while (1) {
464 		iter_count = dio->iter.count;
465 
466 		EBUG_ON(current->faults_disabled_mapping);
467 		current->faults_disabled_mapping = mapping;
468 
469 		ret = bio_iov_iter_get_pages(bio, &dio->iter);
470 
471 		dropped_locks = fdm_dropped_locks();
472 
473 		current->faults_disabled_mapping = NULL;
474 
475 		/*
476 		 * If the fault handler returned an error but also signalled
477 		 * that it dropped & retook ei_pagecache_lock, we just need to
478 		 * re-shoot down the page cache and retry:
479 		 */
480 		if (dropped_locks && ret)
481 			ret = 0;
482 
483 		if (unlikely(ret < 0))
484 			goto err;
485 
486 		if (unlikely(dropped_locks)) {
487 			ret = bch2_write_invalidate_inode_pages_range(mapping,
488 					req->ki_pos,
489 					req->ki_pos + iter_count - 1);
490 			if (unlikely(ret))
491 				goto err;
492 
493 			if (!bio->bi_iter.bi_size)
494 				continue;
495 		}
496 
497 		unaligned = bio->bi_iter.bi_size & (block_bytes(c) - 1);
498 		bio->bi_iter.bi_size -= unaligned;
499 		iov_iter_revert(&dio->iter, unaligned);
500 
501 		if (!bio->bi_iter.bi_size) {
502 			/*
503 			 * bio_iov_iter_get_pages was only able to get <
504 			 * blocksize worth of pages:
505 			 */
506 			ret = -EFAULT;
507 			goto err;
508 		}
509 
510 		bch2_write_op_init(&dio->op, c, opts);
511 		dio->op.end_io		= sync
512 			? NULL
513 			: bch2_dio_write_loop_async;
514 		dio->op.target		= dio->op.opts.foreground_target;
515 		dio->op.write_point	= writepoint_hashed((unsigned long) current);
516 		dio->op.nr_replicas	= dio->op.opts.data_replicas;
517 		dio->op.subvol		= inode->ei_inum.subvol;
518 		dio->op.pos		= POS(inode->v.i_ino, (u64) req->ki_pos >> 9);
519 		dio->op.devs_need_flush	= &inode->ei_devs_need_flush;
520 
521 		if (sync)
522 			dio->op.flags |= BCH_WRITE_sync;
523 		dio->op.flags |= BCH_WRITE_check_enospc;
524 
525 		ret = bch2_quota_reservation_add(c, inode, &dio->quota_res,
526 						 bio_sectors(bio), true);
527 		if (unlikely(ret))
528 			goto err;
529 
530 		ret = bch2_disk_reservation_get(c, &dio->op.res, bio_sectors(bio),
531 						dio->op.opts.data_replicas, 0);
532 		if (unlikely(ret) &&
533 		    !bch2_dio_write_check_allocated(dio))
534 			goto err;
535 
536 		task_io_account_write(bio->bi_iter.bi_size);
537 
538 		if (unlikely(dio->iter.count) &&
539 		    !dio->sync &&
540 		    !dio->loop &&
541 		    bch2_dio_write_copy_iov(dio))
542 			dio->sync = sync = true;
543 
544 		dio->loop = true;
545 		closure_call(&dio->op.cl, bch2_write, NULL, NULL);
546 
547 		if (!sync)
548 			return -EIOCBQUEUED;
549 
550 		bch2_dio_write_end(dio);
551 
552 		if (likely(!dio->iter.count) || dio->op.error)
553 			break;
554 
555 		bio_reset(bio, NULL, REQ_OP_WRITE | REQ_SYNC | REQ_IDLE);
556 	}
557 out:
558 	return bch2_dio_write_done(dio);
559 err:
560 	dio->op.error = ret;
561 
562 	bio_release_pages(bio, false);
563 
564 	bch2_quota_reservation_put(c, inode, &dio->quota_res);
565 	goto out;
566 }
567 
bch2_dio_write_continue(struct dio_write * dio)568 static noinline __cold void bch2_dio_write_continue(struct dio_write *dio)
569 {
570 	struct mm_struct *mm = dio->mm;
571 
572 	bio_reset(&dio->op.wbio.bio, NULL, REQ_OP_WRITE);
573 
574 	if (mm)
575 		kthread_use_mm(mm);
576 	bch2_dio_write_loop(dio);
577 	if (mm)
578 		kthread_unuse_mm(mm);
579 }
580 
bch2_dio_write_loop_async(struct bch_write_op * op)581 static void bch2_dio_write_loop_async(struct bch_write_op *op)
582 {
583 	struct dio_write *dio = container_of(op, struct dio_write, op);
584 
585 	bch2_dio_write_end(dio);
586 
587 	if (likely(!dio->iter.count) || dio->op.error)
588 		bch2_dio_write_done(dio);
589 	else
590 		bch2_dio_write_continue(dio);
591 }
592 
bch2_direct_write(struct kiocb * req,struct iov_iter * iter)593 ssize_t bch2_direct_write(struct kiocb *req, struct iov_iter *iter)
594 {
595 	struct file *file = req->ki_filp;
596 	struct address_space *mapping = file->f_mapping;
597 	struct bch_inode_info *inode = file_bch_inode(file);
598 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
599 	struct dio_write *dio;
600 	struct bio *bio;
601 	bool locked = true, extending;
602 	ssize_t ret;
603 
604 	prefetch(&c->opts);
605 	prefetch((void *) &c->opts + 64);
606 	prefetch(&inode->ei_inode);
607 	prefetch((void *) &inode->ei_inode + 64);
608 
609 	if (!bch2_write_ref_tryget(c, BCH_WRITE_REF_dio_write))
610 		return -EROFS;
611 
612 	inode_lock(&inode->v);
613 
614 	ret = generic_write_checks(req, iter);
615 	if (unlikely(ret <= 0))
616 		goto err_put_write_ref;
617 
618 	ret = file_remove_privs(file);
619 	if (unlikely(ret))
620 		goto err_put_write_ref;
621 
622 	ret = file_update_time(file);
623 	if (unlikely(ret))
624 		goto err_put_write_ref;
625 
626 	if (unlikely((req->ki_pos|iter->count) & (block_bytes(c) - 1))) {
627 		ret = -EINVAL;
628 		goto err_put_write_ref;
629 	}
630 
631 	inode_dio_begin(&inode->v);
632 	bch2_pagecache_block_get(inode);
633 
634 	extending = req->ki_pos + iter->count > inode->v.i_size;
635 	if (!extending) {
636 		inode_unlock(&inode->v);
637 		locked = false;
638 	}
639 
640 	bio = bio_alloc_bioset(NULL,
641 			       bio_iov_vecs_to_alloc(iter, BIO_MAX_VECS),
642 			       REQ_OP_WRITE | REQ_SYNC | REQ_IDLE,
643 			       GFP_KERNEL,
644 			       &c->dio_write_bioset);
645 	dio = container_of(bio, struct dio_write, op.wbio.bio);
646 	dio->req		= req;
647 	dio->mapping		= mapping;
648 	dio->inode		= inode;
649 	dio->mm			= current->mm;
650 	dio->iov		= NULL;
651 	dio->loop		= false;
652 	dio->extending		= extending;
653 	dio->sync		= is_sync_kiocb(req) || extending;
654 	dio->flush		= iocb_is_dsync(req) && !c->opts.journal_flush_disabled;
655 	dio->quota_res.sectors	= 0;
656 	dio->written		= 0;
657 	dio->iter		= *iter;
658 	dio->op.c		= c;
659 
660 	if (unlikely(mapping->nrpages)) {
661 		ret = bch2_write_invalidate_inode_pages_range(mapping,
662 						req->ki_pos,
663 						req->ki_pos + iter->count - 1);
664 		if (unlikely(ret))
665 			goto err_put_bio;
666 	}
667 
668 	ret = bch2_dio_write_loop(dio);
669 out:
670 	if (locked)
671 		inode_unlock(&inode->v);
672 	return ret;
673 err_put_bio:
674 	bch2_pagecache_block_put(inode);
675 	bio_put(bio);
676 	inode_dio_end(&inode->v);
677 err_put_write_ref:
678 	bch2_write_ref_put(c, BCH_WRITE_REF_dio_write);
679 	goto out;
680 }
681 
bch2_fs_fs_io_direct_exit(struct bch_fs * c)682 void bch2_fs_fs_io_direct_exit(struct bch_fs *c)
683 {
684 	bioset_exit(&c->dio_write_bioset);
685 	bioset_exit(&c->dio_read_bioset);
686 }
687 
bch2_fs_fs_io_direct_init(struct bch_fs * c)688 int bch2_fs_fs_io_direct_init(struct bch_fs *c)
689 {
690 	if (bioset_init(&c->dio_read_bioset,
691 			4, offsetof(struct dio_read, rbio.bio),
692 			BIOSET_NEED_BVECS))
693 		return -BCH_ERR_ENOMEM_dio_read_bioset_init;
694 
695 	if (bioset_init(&c->dio_write_bioset,
696 			4, offsetof(struct dio_write, op.wbio.bio),
697 			BIOSET_NEED_BVECS))
698 		return -BCH_ERR_ENOMEM_dio_write_bioset_init;
699 
700 	return 0;
701 }
702 
703 #endif /* NO_BCACHEFS_FS */
704