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