1 // SPDX-License-Identifier: GPL-2.0
5 #include "alloc_foreground.h"
8 #include "fs-io-buffered.h"
9 #include "fs-io-direct.h"
10 #include "fs-io-pagecache.h"
14 #include <linux/backing-dev.h>
15 #include <linux/pagemap.h>
16 #include <linux/writeback.h>
18 static inline bool bio_full(struct bio *bio, unsigned len)
20 if (bio->bi_vcnt >= bio->bi_max_vecs)
22 if (bio->bi_iter.bi_size > UINT_MAX - len)
29 static void bch2_readpages_end_io(struct bio *bio)
33 bio_for_each_folio_all(fi, bio) {
34 if (!bio->bi_status) {
35 folio_mark_uptodate(fi.folio);
37 folio_clear_uptodate(fi.folio);
38 folio_set_error(fi.folio);
40 folio_unlock(fi.folio);
46 struct readpages_iter {
47 struct address_space *mapping;
52 static int readpages_iter_init(struct readpages_iter *iter,
53 struct readahead_control *ractl)
57 *iter = (struct readpages_iter) { ractl->mapping };
59 while ((folio = __readahead_folio(ractl))) {
60 if (!bch2_folio_create(folio, GFP_KERNEL) ||
61 darray_push(&iter->folios, folio)) {
62 bch2_folio_release(folio);
63 ractl->_nr_pages += folio_nr_pages(folio);
64 ractl->_index -= folio_nr_pages(folio);
65 return iter->folios.nr ? 0 : -ENOMEM;
74 static inline struct folio *readpage_iter_peek(struct readpages_iter *iter)
76 if (iter->idx >= iter->folios.nr)
78 return iter->folios.data[iter->idx];
81 static inline void readpage_iter_advance(struct readpages_iter *iter)
86 static bool extent_partial_reads_expensive(struct bkey_s_c k)
88 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
89 struct bch_extent_crc_unpacked crc;
90 const union bch_extent_entry *i;
92 bkey_for_each_crc(k.k, ptrs, crc, i)
93 if (crc.csum_type || crc.compression_type)
98 static int readpage_bio_extend(struct btree_trans *trans,
99 struct readpages_iter *iter,
101 unsigned sectors_this_extent,
104 /* Don't hold btree locks while allocating memory: */
105 bch2_trans_unlock(trans);
107 while (bio_sectors(bio) < sectors_this_extent &&
108 bio->bi_vcnt < bio->bi_max_vecs) {
109 struct folio *folio = readpage_iter_peek(iter);
113 readpage_iter_advance(iter);
115 pgoff_t folio_offset = bio_end_sector(bio) >> PAGE_SECTORS_SHIFT;
120 folio = xa_load(&iter->mapping->i_pages, folio_offset);
121 if (folio && !xa_is_value(folio))
124 folio = filemap_alloc_folio(readahead_gfp_mask(iter->mapping), 0);
128 if (!__bch2_folio_create(folio, GFP_KERNEL)) {
133 ret = filemap_add_folio(iter->mapping, folio, folio_offset, GFP_KERNEL);
135 __bch2_folio_release(folio);
143 BUG_ON(folio_sector(folio) != bio_end_sector(bio));
145 BUG_ON(!bio_add_folio(bio, folio, folio_size(folio), 0));
148 return bch2_trans_relock(trans);
151 static void bchfs_read(struct btree_trans *trans,
152 struct bch_read_bio *rbio,
154 struct readpages_iter *readpages_iter)
156 struct bch_fs *c = trans->c;
157 struct btree_iter iter;
159 int flags = BCH_READ_RETRY_IF_STALE|
160 BCH_READ_MAY_PROMOTE;
165 rbio->start_time = local_clock();
166 rbio->subvol = inum.subvol;
168 bch2_bkey_buf_init(&sk);
170 bch2_trans_begin(trans);
171 iter = (struct btree_iter) { NULL };
173 ret = bch2_subvolume_get_snapshot(trans, inum.subvol, &snapshot);
177 bch2_trans_iter_init(trans, &iter, BTREE_ID_extents,
178 SPOS(inum.inum, rbio->bio.bi_iter.bi_sector, snapshot),
182 unsigned bytes, sectors, offset_into_extent;
183 enum btree_id data_btree = BTREE_ID_extents;
186 * read_extent -> io_time_reset may cause a transaction restart
187 * without returning an error, we need to check for that here:
189 ret = bch2_trans_relock(trans);
193 bch2_btree_iter_set_pos(&iter,
194 POS(inum.inum, rbio->bio.bi_iter.bi_sector));
196 k = bch2_btree_iter_peek_slot(&iter);
201 offset_into_extent = iter.pos.offset -
202 bkey_start_offset(k.k);
203 sectors = k.k->size - offset_into_extent;
205 bch2_bkey_buf_reassemble(&sk, c, k);
207 ret = bch2_read_indirect_extent(trans, &data_btree,
208 &offset_into_extent, &sk);
212 k = bkey_i_to_s_c(sk.k);
214 sectors = min(sectors, k.k->size - offset_into_extent);
216 if (readpages_iter) {
217 ret = readpage_bio_extend(trans, readpages_iter, &rbio->bio, sectors,
218 extent_partial_reads_expensive(k));
223 bytes = min(sectors, bio_sectors(&rbio->bio)) << 9;
224 swap(rbio->bio.bi_iter.bi_size, bytes);
226 if (rbio->bio.bi_iter.bi_size == bytes)
227 flags |= BCH_READ_LAST_FRAGMENT;
229 bch2_bio_page_state_set(&rbio->bio, k);
231 bch2_read_extent(trans, rbio, iter.pos,
232 data_btree, k, offset_into_extent, flags);
234 if (flags & BCH_READ_LAST_FRAGMENT)
237 swap(rbio->bio.bi_iter.bi_size, bytes);
238 bio_advance(&rbio->bio, bytes);
240 ret = btree_trans_too_many_iters(trans);
245 bch2_trans_iter_exit(trans, &iter);
247 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
251 bch_err_inum_offset_ratelimited(c,
253 iter.pos.offset << 9,
254 "read error %i from btree lookup", ret);
255 rbio->bio.bi_status = BLK_STS_IOERR;
256 bio_endio(&rbio->bio);
259 bch2_bkey_buf_exit(&sk, c);
262 void bch2_readahead(struct readahead_control *ractl)
264 struct bch_inode_info *inode = to_bch_ei(ractl->mapping->host);
265 struct bch_fs *c = inode->v.i_sb->s_fs_info;
266 struct bch_io_opts opts;
267 struct btree_trans *trans = bch2_trans_get(c);
269 struct readpages_iter readpages_iter;
271 bch2_inode_opts_get(&opts, c, &inode->ei_inode);
273 int ret = readpages_iter_init(&readpages_iter, ractl);
277 bch2_pagecache_add_get(inode);
279 while ((folio = readpage_iter_peek(&readpages_iter))) {
280 unsigned n = min_t(unsigned,
281 readpages_iter.folios.nr -
284 struct bch_read_bio *rbio =
285 rbio_init(bio_alloc_bioset(NULL, n, REQ_OP_READ,
286 GFP_KERNEL, &c->bio_read),
289 readpage_iter_advance(&readpages_iter);
291 rbio->bio.bi_iter.bi_sector = folio_sector(folio);
292 rbio->bio.bi_end_io = bch2_readpages_end_io;
293 BUG_ON(!bio_add_folio(&rbio->bio, folio, folio_size(folio), 0));
295 bchfs_read(trans, rbio, inode_inum(inode),
297 bch2_trans_unlock(trans);
300 bch2_pagecache_add_put(inode);
302 bch2_trans_put(trans);
303 darray_exit(&readpages_iter.folios);
306 static void __bchfs_readfolio(struct bch_fs *c, struct bch_read_bio *rbio,
307 subvol_inum inum, struct folio *folio)
309 bch2_folio_create(folio, __GFP_NOFAIL);
311 rbio->bio.bi_opf = REQ_OP_READ|REQ_SYNC;
312 rbio->bio.bi_iter.bi_sector = folio_sector(folio);
313 BUG_ON(!bio_add_folio(&rbio->bio, folio, folio_size(folio), 0));
315 bch2_trans_run(c, (bchfs_read(trans, rbio, inum, NULL), 0));
318 static void bch2_read_single_folio_end_io(struct bio *bio)
320 complete(bio->bi_private);
323 int bch2_read_single_folio(struct folio *folio, struct address_space *mapping)
325 struct bch_inode_info *inode = to_bch_ei(mapping->host);
326 struct bch_fs *c = inode->v.i_sb->s_fs_info;
327 struct bch_read_bio *rbio;
328 struct bch_io_opts opts;
330 DECLARE_COMPLETION_ONSTACK(done);
332 bch2_inode_opts_get(&opts, c, &inode->ei_inode);
334 rbio = rbio_init(bio_alloc_bioset(NULL, 1, REQ_OP_READ, GFP_KERNEL, &c->bio_read),
336 rbio->bio.bi_private = &done;
337 rbio->bio.bi_end_io = bch2_read_single_folio_end_io;
339 __bchfs_readfolio(c, rbio, inode_inum(inode), folio);
340 wait_for_completion(&done);
342 ret = blk_status_to_errno(rbio->bio.bi_status);
348 folio_mark_uptodate(folio);
352 int bch2_read_folio(struct file *file, struct folio *folio)
356 ret = bch2_read_single_folio(folio, folio->mapping);
358 return bch2_err_class(ret);
363 struct bch_writepage_io {
364 struct bch_inode_info *inode;
367 struct bch_write_op op;
370 struct bch_writepage_state {
371 struct bch_writepage_io *io;
372 struct bch_io_opts opts;
373 struct bch_folio_sector *tmp;
374 unsigned tmp_sectors;
377 static inline struct bch_writepage_state bch_writepage_state_init(struct bch_fs *c,
378 struct bch_inode_info *inode)
380 struct bch_writepage_state ret = { 0 };
382 bch2_inode_opts_get(&ret.opts, c, &inode->ei_inode);
387 * Determine when a writepage io is full. We have to limit writepage bios to a
388 * single page per bvec (i.e. 1MB with 4k pages) because that is the limit to
389 * what the bounce path in bch2_write_extent() can handle. In theory we could
390 * loosen this restriction for non-bounce I/O, but we don't have that context
391 * here. Ideally, we can up this limit and make it configurable in the future
392 * when the bounce path can be enhanced to accommodate larger source bios.
394 static inline bool bch_io_full(struct bch_writepage_io *io, unsigned len)
396 struct bio *bio = &io->op.wbio.bio;
397 return bio_full(bio, len) ||
398 (bio->bi_iter.bi_size + len > BIO_MAX_VECS * PAGE_SIZE);
401 static void bch2_writepage_io_done(struct bch_write_op *op)
403 struct bch_writepage_io *io =
404 container_of(op, struct bch_writepage_io, op);
405 struct bch_fs *c = io->op.c;
406 struct bio *bio = &io->op.wbio.bio;
407 struct folio_iter fi;
411 set_bit(EI_INODE_ERROR, &io->inode->ei_flags);
413 bio_for_each_folio_all(fi, bio) {
416 folio_set_error(fi.folio);
417 mapping_set_error(fi.folio->mapping, -EIO);
419 s = __bch2_folio(fi.folio);
421 for (i = 0; i < folio_sectors(fi.folio); i++)
422 s->s[i].nr_replicas = 0;
423 spin_unlock(&s->lock);
427 if (io->op.flags & BCH_WRITE_WROTE_DATA_INLINE) {
428 bio_for_each_folio_all(fi, bio) {
431 s = __bch2_folio(fi.folio);
433 for (i = 0; i < folio_sectors(fi.folio); i++)
434 s->s[i].nr_replicas = 0;
435 spin_unlock(&s->lock);
440 * racing with fallocate can cause us to add fewer sectors than
441 * expected - but we shouldn't add more sectors than expected:
443 WARN_ON_ONCE(io->op.i_sectors_delta > 0);
446 * (error (due to going RO) halfway through a page can screw that up
449 BUG_ON(io->op.op.i_sectors_delta >= PAGE_SECTORS);
453 * PageWriteback is effectively our ref on the inode - fixup i_blocks
454 * before calling end_page_writeback:
456 bch2_i_sectors_acct(c, io->inode, NULL, io->op.i_sectors_delta);
458 bio_for_each_folio_all(fi, bio) {
459 struct bch_folio *s = __bch2_folio(fi.folio);
461 if (atomic_dec_and_test(&s->write_count))
462 folio_end_writeback(fi.folio);
465 bio_put(&io->op.wbio.bio);
468 static void bch2_writepage_do_io(struct bch_writepage_state *w)
470 struct bch_writepage_io *io = w->io;
473 closure_call(&io->op.cl, bch2_write, NULL, NULL);
477 * Get a bch_writepage_io and add @page to it - appending to an existing one if
478 * possible, else allocating a new one:
480 static void bch2_writepage_io_alloc(struct bch_fs *c,
481 struct writeback_control *wbc,
482 struct bch_writepage_state *w,
483 struct bch_inode_info *inode,
485 unsigned nr_replicas)
487 struct bch_write_op *op;
489 w->io = container_of(bio_alloc_bioset(NULL, BIO_MAX_VECS,
492 &c->writepage_bioset),
493 struct bch_writepage_io, op.wbio.bio);
495 w->io->inode = inode;
497 bch2_write_op_init(op, c, w->opts);
498 op->target = w->opts.foreground_target;
499 op->nr_replicas = nr_replicas;
500 op->res.nr_replicas = nr_replicas;
501 op->write_point = writepoint_hashed(inode->ei_last_dirtied);
502 op->subvol = inode->ei_subvol;
503 op->pos = POS(inode->v.i_ino, sector);
504 op->end_io = bch2_writepage_io_done;
505 op->devs_need_flush = &inode->ei_devs_need_flush;
506 op->wbio.bio.bi_iter.bi_sector = sector;
507 op->wbio.bio.bi_opf = wbc_to_write_flags(wbc);
510 static int __bch2_writepage(struct folio *folio,
511 struct writeback_control *wbc,
514 struct bch_inode_info *inode = to_bch_ei(folio->mapping->host);
515 struct bch_fs *c = inode->v.i_sb->s_fs_info;
516 struct bch_writepage_state *w = data;
518 unsigned i, offset, f_sectors, nr_replicas_this_write = U32_MAX;
519 loff_t i_size = i_size_read(&inode->v);
522 EBUG_ON(!folio_test_uptodate(folio));
524 /* Is the folio fully inside i_size? */
525 if (folio_end_pos(folio) <= i_size)
528 /* Is the folio fully outside i_size? (truncate in progress) */
529 if (folio_pos(folio) >= i_size) {
535 * The folio straddles i_size. It must be zeroed out on each and every
536 * writepage invocation because it may be mmapped. "A file is mapped
537 * in multiples of the folio size. For a file that is not a multiple of
538 * the folio size, the remaining memory is zeroed when mapped, and
539 * writes to that region are not written out to the file."
541 folio_zero_segment(folio,
542 i_size - folio_pos(folio),
545 f_sectors = folio_sectors(folio);
546 s = bch2_folio(folio);
548 if (f_sectors > w->tmp_sectors) {
550 w->tmp = kcalloc(f_sectors, sizeof(struct bch_folio_sector), __GFP_NOFAIL);
551 w->tmp_sectors = f_sectors;
555 * Things get really hairy with errors during writeback:
557 ret = bch2_get_folio_disk_reservation(c, inode, folio, false);
560 /* Before unlocking the page, get copy of reservations: */
562 memcpy(w->tmp, s->s, sizeof(struct bch_folio_sector) * f_sectors);
564 for (i = 0; i < f_sectors; i++) {
565 if (s->s[i].state < SECTOR_dirty)
568 nr_replicas_this_write =
569 min_t(unsigned, nr_replicas_this_write,
570 s->s[i].nr_replicas +
571 s->s[i].replicas_reserved);
574 for (i = 0; i < f_sectors; i++) {
575 if (s->s[i].state < SECTOR_dirty)
578 s->s[i].nr_replicas = w->opts.compression
579 ? 0 : nr_replicas_this_write;
581 s->s[i].replicas_reserved = 0;
582 bch2_folio_sector_set(folio, s, i, SECTOR_allocated);
584 spin_unlock(&s->lock);
586 BUG_ON(atomic_read(&s->write_count));
587 atomic_set(&s->write_count, 1);
589 BUG_ON(folio_test_writeback(folio));
590 folio_start_writeback(folio);
596 unsigned sectors = 0, dirty_sectors = 0, reserved_sectors = 0;
599 while (offset < f_sectors &&
600 w->tmp[offset].state < SECTOR_dirty)
603 if (offset == f_sectors)
606 while (offset + sectors < f_sectors &&
607 w->tmp[offset + sectors].state >= SECTOR_dirty) {
608 reserved_sectors += w->tmp[offset + sectors].replicas_reserved;
609 dirty_sectors += w->tmp[offset + sectors].state == SECTOR_dirty;
614 sector = folio_sector(folio) + offset;
617 (w->io->op.res.nr_replicas != nr_replicas_this_write ||
618 bch_io_full(w->io, sectors << 9) ||
619 bio_end_sector(&w->io->op.wbio.bio) != sector))
620 bch2_writepage_do_io(w);
623 bch2_writepage_io_alloc(c, wbc, w, inode, sector,
624 nr_replicas_this_write);
626 atomic_inc(&s->write_count);
628 BUG_ON(inode != w->io->inode);
629 BUG_ON(!bio_add_folio(&w->io->op.wbio.bio, folio,
630 sectors << 9, offset << 9));
632 /* Check for writing past i_size: */
633 WARN_ONCE((bio_end_sector(&w->io->op.wbio.bio) << 9) >
634 round_up(i_size, block_bytes(c)) &&
635 !test_bit(BCH_FS_emergency_ro, &c->flags),
636 "writing past i_size: %llu > %llu (unrounded %llu)\n",
637 bio_end_sector(&w->io->op.wbio.bio) << 9,
638 round_up(i_size, block_bytes(c)),
641 w->io->op.res.sectors += reserved_sectors;
642 w->io->op.i_sectors_delta -= dirty_sectors;
643 w->io->op.new_i_size = i_size;
648 if (atomic_dec_and_test(&s->write_count))
649 folio_end_writeback(folio);
654 int bch2_writepages(struct address_space *mapping, struct writeback_control *wbc)
656 struct bch_fs *c = mapping->host->i_sb->s_fs_info;
657 struct bch_writepage_state w =
658 bch_writepage_state_init(c, to_bch_ei(mapping->host));
659 struct blk_plug plug;
662 blk_start_plug(&plug);
663 ret = write_cache_pages(mapping, wbc, __bch2_writepage, &w);
665 bch2_writepage_do_io(&w);
666 blk_finish_plug(&plug);
668 return bch2_err_class(ret);
671 /* buffered writes: */
673 int bch2_write_begin(struct file *file, struct address_space *mapping,
674 loff_t pos, unsigned len,
675 struct page **pagep, void **fsdata)
677 struct bch_inode_info *inode = to_bch_ei(mapping->host);
678 struct bch_fs *c = inode->v.i_sb->s_fs_info;
679 struct bch2_folio_reservation *res;
684 res = kmalloc(sizeof(*res), GFP_KERNEL);
688 bch2_folio_reservation_init(c, inode, res);
691 bch2_pagecache_add_get(inode);
693 folio = __filemap_get_folio(mapping, pos >> PAGE_SHIFT,
694 FGP_LOCK|FGP_WRITE|FGP_CREAT|FGP_STABLE,
695 mapping_gfp_mask(mapping));
696 if (IS_ERR_OR_NULL(folio))
699 offset = pos - folio_pos(folio);
700 len = min_t(size_t, len, folio_end_pos(folio) - pos);
702 if (folio_test_uptodate(folio))
705 /* If we're writing entire folio, don't need to read it in first: */
706 if (!offset && len == folio_size(folio))
709 if (!offset && pos + len >= inode->v.i_size) {
710 folio_zero_segment(folio, len, folio_size(folio));
711 flush_dcache_folio(folio);
715 if (folio_pos(folio) >= inode->v.i_size) {
716 folio_zero_segments(folio, 0, offset, offset + len, folio_size(folio));
717 flush_dcache_folio(folio);
721 ret = bch2_read_single_folio(folio, mapping);
725 ret = bch2_folio_set(c, inode_inum(inode), &folio, 1);
729 ret = bch2_folio_reservation_get(c, inode, folio, res, offset, len);
731 if (!folio_test_uptodate(folio)) {
733 * If the folio hasn't been read in, we won't know if we
734 * actually need a reservation - we don't actually need
735 * to read here, we just need to check if the folio is
736 * fully backed by uncompressed data:
744 *pagep = &folio->page;
751 bch2_pagecache_add_put(inode);
754 return bch2_err_class(ret);
757 int bch2_write_end(struct file *file, struct address_space *mapping,
758 loff_t pos, unsigned len, unsigned copied,
759 struct page *page, void *fsdata)
761 struct bch_inode_info *inode = to_bch_ei(mapping->host);
762 struct bch_fs *c = inode->v.i_sb->s_fs_info;
763 struct bch2_folio_reservation *res = fsdata;
764 struct folio *folio = page_folio(page);
765 unsigned offset = pos - folio_pos(folio);
767 lockdep_assert_held(&inode->v.i_rwsem);
768 BUG_ON(offset + copied > folio_size(folio));
770 if (unlikely(copied < len && !folio_test_uptodate(folio))) {
772 * The folio needs to be read in, but that would destroy
773 * our partial write - simplest thing is to just force
774 * userspace to redo the write:
776 folio_zero_range(folio, 0, folio_size(folio));
777 flush_dcache_folio(folio);
781 spin_lock(&inode->v.i_lock);
782 if (pos + copied > inode->v.i_size)
783 i_size_write(&inode->v, pos + copied);
784 spin_unlock(&inode->v.i_lock);
787 if (!folio_test_uptodate(folio))
788 folio_mark_uptodate(folio);
790 bch2_set_folio_dirty(c, inode, folio, res, offset, copied);
792 inode->ei_last_dirtied = (unsigned long) current;
797 bch2_pagecache_add_put(inode);
799 bch2_folio_reservation_put(c, inode, res);
805 static noinline void folios_trunc(folios *fs, struct folio **fi)
807 while (fs->data + fs->nr > fi) {
808 struct folio *f = darray_pop(fs);
815 static int __bch2_buffered_write(struct bch_inode_info *inode,
816 struct address_space *mapping,
817 struct iov_iter *iter,
818 loff_t pos, unsigned len)
820 struct bch_fs *c = inode->v.i_sb->s_fs_info;
821 struct bch2_folio_reservation res;
824 unsigned copied = 0, f_offset, f_copied;
825 u64 end = pos + len, f_pos, f_len;
826 loff_t last_folio_pos = inode->v.i_size;
831 bch2_folio_reservation_init(c, inode, &res);
834 ret = bch2_filemap_get_contig_folios_d(mapping, pos, end,
835 FGP_LOCK|FGP_WRITE|FGP_STABLE|FGP_CREAT,
836 mapping_gfp_mask(mapping),
843 f = darray_first(fs);
844 if (pos != folio_pos(f) && !folio_test_uptodate(f)) {
845 ret = bch2_read_single_folio(f, mapping);
851 end = min(end, folio_end_pos(f));
852 last_folio_pos = folio_pos(f);
853 if (end != folio_end_pos(f) && !folio_test_uptodate(f)) {
854 if (end >= inode->v.i_size) {
855 folio_zero_range(f, 0, folio_size(f));
857 ret = bch2_read_single_folio(f, mapping);
863 ret = bch2_folio_set(c, inode_inum(inode), fs.data, fs.nr);
868 f_offset = pos - folio_pos(darray_first(fs));
869 darray_for_each(fs, fi) {
871 f_len = min(end, folio_end_pos(f)) - f_pos;
874 * XXX: per POSIX and fstests generic/275, on -ENOSPC we're
875 * supposed to write as much as we have disk space for.
877 * On failure here we should still write out a partial page if
878 * we aren't completely out of disk space - we don't do that
881 ret = bch2_folio_reservation_get(c, inode, f, &res, f_offset, f_len);
883 folios_trunc(&fs, fi);
887 end = min(end, folio_end_pos(darray_last(fs)));
891 f_pos = folio_end_pos(f);
895 if (mapping_writably_mapped(mapping))
896 darray_for_each(fs, fi)
897 flush_dcache_folio(*fi);
900 f_offset = pos - folio_pos(darray_first(fs));
901 darray_for_each(fs, fi) {
903 f_len = min(end, folio_end_pos(f)) - f_pos;
904 f_copied = copy_page_from_iter_atomic(&f->page, f_offset, f_len, iter);
906 folios_trunc(&fs, fi);
910 if (!folio_test_uptodate(f) &&
911 f_copied != folio_size(f) &&
912 pos + copied + f_copied < inode->v.i_size) {
913 iov_iter_revert(iter, f_copied);
914 folio_zero_range(f, 0, folio_size(f));
915 folios_trunc(&fs, fi);
919 flush_dcache_folio(f);
922 if (f_copied != f_len) {
923 folios_trunc(&fs, fi + 1);
927 f_pos = folio_end_pos(f);
936 spin_lock(&inode->v.i_lock);
937 if (end > inode->v.i_size)
938 i_size_write(&inode->v, end);
939 spin_unlock(&inode->v.i_lock);
942 f_offset = pos - folio_pos(darray_first(fs));
943 darray_for_each(fs, fi) {
945 f_len = min(end, folio_end_pos(f)) - f_pos;
947 if (!folio_test_uptodate(f))
948 folio_mark_uptodate(f);
950 bch2_set_folio_dirty(c, inode, f, &res, f_offset, f_len);
952 f_pos = folio_end_pos(f);
956 inode->ei_last_dirtied = (unsigned long) current;
958 darray_for_each(fs, fi) {
964 * If the last folio added to the mapping starts beyond current EOF, we
965 * performed a short write but left around at least one post-EOF folio.
966 * Clean up the mapping before we return.
968 if (last_folio_pos >= inode->v.i_size)
969 truncate_pagecache(&inode->v, inode->v.i_size);
972 bch2_folio_reservation_put(c, inode, &res);
974 return copied ?: ret;
977 static ssize_t bch2_buffered_write(struct kiocb *iocb, struct iov_iter *iter)
979 struct file *file = iocb->ki_filp;
980 struct address_space *mapping = file->f_mapping;
981 struct bch_inode_info *inode = file_bch_inode(file);
982 loff_t pos = iocb->ki_pos;
986 bch2_pagecache_add_get(inode);
989 unsigned offset = pos & (PAGE_SIZE - 1);
990 unsigned bytes = iov_iter_count(iter);
993 * Bring in the user page that we will copy from _first_.
994 * Otherwise there's a nasty deadlock on copying from the
995 * same page as we're writing to, without it being marked
998 * Not only is this an optimisation, but it is also required
999 * to check that the address is actually valid, when atomic
1000 * usercopies are used, below.
1002 if (unlikely(fault_in_iov_iter_readable(iter, bytes))) {
1003 bytes = min_t(unsigned long, iov_iter_count(iter),
1004 PAGE_SIZE - offset);
1006 if (unlikely(fault_in_iov_iter_readable(iter, bytes))) {
1012 if (unlikely(fatal_signal_pending(current))) {
1017 ret = __bch2_buffered_write(inode, mapping, iter, pos, bytes);
1018 if (unlikely(ret < 0))
1023 if (unlikely(ret == 0)) {
1025 * If we were unable to copy any data at all, we must
1026 * fall back to a single segment length write.
1028 * If we didn't fallback here, we could livelock
1029 * because not all segments in the iov can be copied at
1030 * once without a pagefault.
1032 bytes = min_t(unsigned long, PAGE_SIZE - offset,
1033 iov_iter_single_seg_count(iter));
1040 balance_dirty_pages_ratelimited(mapping);
1041 } while (iov_iter_count(iter));
1043 bch2_pagecache_add_put(inode);
1045 return written ? written : ret;
1048 ssize_t bch2_write_iter(struct kiocb *iocb, struct iov_iter *from)
1050 struct file *file = iocb->ki_filp;
1051 struct bch_inode_info *inode = file_bch_inode(file);
1054 if (iocb->ki_flags & IOCB_DIRECT) {
1055 ret = bch2_direct_write(iocb, from);
1059 inode_lock(&inode->v);
1061 ret = generic_write_checks(iocb, from);
1065 ret = file_remove_privs(file);
1069 ret = file_update_time(file);
1073 ret = bch2_buffered_write(iocb, from);
1074 if (likely(ret > 0))
1075 iocb->ki_pos += ret;
1077 inode_unlock(&inode->v);
1080 ret = generic_write_sync(iocb, ret);
1082 return bch2_err_class(ret);
1085 void bch2_fs_fs_io_buffered_exit(struct bch_fs *c)
1087 bioset_exit(&c->writepage_bioset);
1090 int bch2_fs_fs_io_buffered_init(struct bch_fs *c)
1092 if (bioset_init(&c->writepage_bioset,
1093 4, offsetof(struct bch_writepage_io, op.wbio.bio),
1095 return -BCH_ERR_ENOMEM_writepage_bioset_init;
1100 #endif /* NO_BCACHEFS_FS */