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)
55 memset(iter, 0, sizeof(*iter));
57 iter->mapping = ractl->mapping;
59 int ret = bch2_filemap_get_contig_folios_d(iter->mapping,
60 ractl->_index << PAGE_SHIFT,
61 (ractl->_index + ractl->_nr_pages) << PAGE_SHIFT,
62 0, mapping_gfp_mask(iter->mapping),
67 darray_for_each(iter->folios, fi) {
68 ractl->_nr_pages -= 1U << folio_order(*fi);
69 __bch2_folio_create(*fi, __GFP_NOFAIL|GFP_KERNEL);
77 static inline struct folio *readpage_iter_peek(struct readpages_iter *iter)
79 if (iter->idx >= iter->folios.nr)
81 return iter->folios.data[iter->idx];
84 static inline void readpage_iter_advance(struct readpages_iter *iter)
89 static bool extent_partial_reads_expensive(struct bkey_s_c k)
91 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
92 struct bch_extent_crc_unpacked crc;
93 const union bch_extent_entry *i;
95 bkey_for_each_crc(k.k, ptrs, crc, i)
96 if (crc.csum_type || crc.compression_type)
101 static int readpage_bio_extend(struct btree_trans *trans,
102 struct readpages_iter *iter,
104 unsigned sectors_this_extent,
107 /* Don't hold btree locks while allocating memory: */
108 bch2_trans_unlock(trans);
110 while (bio_sectors(bio) < sectors_this_extent &&
111 bio->bi_vcnt < bio->bi_max_vecs) {
112 struct folio *folio = readpage_iter_peek(iter);
116 readpage_iter_advance(iter);
118 pgoff_t folio_offset = bio_end_sector(bio) >> PAGE_SECTORS_SHIFT;
123 folio = xa_load(&iter->mapping->i_pages, folio_offset);
124 if (folio && !xa_is_value(folio))
127 folio = filemap_alloc_folio(readahead_gfp_mask(iter->mapping), 0);
131 if (!__bch2_folio_create(folio, GFP_KERNEL)) {
136 ret = filemap_add_folio(iter->mapping, folio, folio_offset, GFP_KERNEL);
138 __bch2_folio_release(folio);
146 BUG_ON(folio_sector(folio) != bio_end_sector(bio));
148 BUG_ON(!bio_add_folio(bio, folio, folio_size(folio), 0));
151 return bch2_trans_relock(trans);
154 static void bchfs_read(struct btree_trans *trans,
155 struct bch_read_bio *rbio,
157 struct readpages_iter *readpages_iter)
159 struct bch_fs *c = trans->c;
160 struct btree_iter iter;
162 int flags = BCH_READ_RETRY_IF_STALE|
163 BCH_READ_MAY_PROMOTE;
168 rbio->start_time = local_clock();
169 rbio->subvol = inum.subvol;
171 bch2_bkey_buf_init(&sk);
173 bch2_trans_begin(trans);
174 iter = (struct btree_iter) { NULL };
176 ret = bch2_subvolume_get_snapshot(trans, inum.subvol, &snapshot);
180 bch2_trans_iter_init(trans, &iter, BTREE_ID_extents,
181 SPOS(inum.inum, rbio->bio.bi_iter.bi_sector, snapshot),
185 unsigned bytes, sectors, offset_into_extent;
186 enum btree_id data_btree = BTREE_ID_extents;
189 * read_extent -> io_time_reset may cause a transaction restart
190 * without returning an error, we need to check for that here:
192 ret = bch2_trans_relock(trans);
196 bch2_btree_iter_set_pos(&iter,
197 POS(inum.inum, rbio->bio.bi_iter.bi_sector));
199 k = bch2_btree_iter_peek_slot(&iter);
204 offset_into_extent = iter.pos.offset -
205 bkey_start_offset(k.k);
206 sectors = k.k->size - offset_into_extent;
208 bch2_bkey_buf_reassemble(&sk, c, k);
210 ret = bch2_read_indirect_extent(trans, &data_btree,
211 &offset_into_extent, &sk);
215 k = bkey_i_to_s_c(sk.k);
217 sectors = min(sectors, k.k->size - offset_into_extent);
219 if (readpages_iter) {
220 ret = readpage_bio_extend(trans, readpages_iter, &rbio->bio, sectors,
221 extent_partial_reads_expensive(k));
226 bytes = min(sectors, bio_sectors(&rbio->bio)) << 9;
227 swap(rbio->bio.bi_iter.bi_size, bytes);
229 if (rbio->bio.bi_iter.bi_size == bytes)
230 flags |= BCH_READ_LAST_FRAGMENT;
232 bch2_bio_page_state_set(&rbio->bio, k);
234 bch2_read_extent(trans, rbio, iter.pos,
235 data_btree, k, offset_into_extent, flags);
237 if (flags & BCH_READ_LAST_FRAGMENT)
240 swap(rbio->bio.bi_iter.bi_size, bytes);
241 bio_advance(&rbio->bio, bytes);
243 ret = btree_trans_too_many_iters(trans);
248 bch2_trans_iter_exit(trans, &iter);
250 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
254 bch_err_inum_offset_ratelimited(c,
256 iter.pos.offset << 9,
257 "read error %i from btree lookup", ret);
258 rbio->bio.bi_status = BLK_STS_IOERR;
259 bio_endio(&rbio->bio);
262 bch2_bkey_buf_exit(&sk, c);
265 void bch2_readahead(struct readahead_control *ractl)
267 struct bch_inode_info *inode = to_bch_ei(ractl->mapping->host);
268 struct bch_fs *c = inode->v.i_sb->s_fs_info;
269 struct bch_io_opts opts;
270 struct btree_trans *trans = bch2_trans_get(c);
272 struct readpages_iter readpages_iter;
275 bch2_inode_opts_get(&opts, c, &inode->ei_inode);
277 ret = readpages_iter_init(&readpages_iter, ractl);
280 bch2_pagecache_add_get(inode);
282 while ((folio = readpage_iter_peek(&readpages_iter))) {
283 unsigned n = min_t(unsigned,
284 readpages_iter.folios.nr -
287 struct bch_read_bio *rbio =
288 rbio_init(bio_alloc_bioset(NULL, n, REQ_OP_READ,
289 GFP_KERNEL, &c->bio_read),
292 readpage_iter_advance(&readpages_iter);
294 rbio->bio.bi_iter.bi_sector = folio_sector(folio);
295 rbio->bio.bi_end_io = bch2_readpages_end_io;
296 BUG_ON(!bio_add_folio(&rbio->bio, folio, folio_size(folio), 0));
298 bchfs_read(trans, rbio, inode_inum(inode),
300 bch2_trans_unlock(trans);
303 bch2_pagecache_add_put(inode);
305 bch2_trans_put(trans);
306 darray_exit(&readpages_iter.folios);
309 static void __bchfs_readfolio(struct bch_fs *c, struct bch_read_bio *rbio,
310 subvol_inum inum, struct folio *folio)
312 bch2_folio_create(folio, __GFP_NOFAIL);
314 rbio->bio.bi_opf = REQ_OP_READ|REQ_SYNC;
315 rbio->bio.bi_iter.bi_sector = folio_sector(folio);
316 BUG_ON(!bio_add_folio(&rbio->bio, folio, folio_size(folio), 0));
318 bch2_trans_run(c, (bchfs_read(trans, rbio, inum, NULL), 0));
321 static void bch2_read_single_folio_end_io(struct bio *bio)
323 complete(bio->bi_private);
326 int bch2_read_single_folio(struct folio *folio, struct address_space *mapping)
328 struct bch_inode_info *inode = to_bch_ei(mapping->host);
329 struct bch_fs *c = inode->v.i_sb->s_fs_info;
330 struct bch_read_bio *rbio;
331 struct bch_io_opts opts;
333 DECLARE_COMPLETION_ONSTACK(done);
335 bch2_inode_opts_get(&opts, c, &inode->ei_inode);
337 rbio = rbio_init(bio_alloc_bioset(NULL, 1, REQ_OP_READ, GFP_KERNEL, &c->bio_read),
339 rbio->bio.bi_private = &done;
340 rbio->bio.bi_end_io = bch2_read_single_folio_end_io;
342 __bchfs_readfolio(c, rbio, inode_inum(inode), folio);
343 wait_for_completion(&done);
345 ret = blk_status_to_errno(rbio->bio.bi_status);
351 folio_mark_uptodate(folio);
355 int bch2_read_folio(struct file *file, struct folio *folio)
359 ret = bch2_read_single_folio(folio, folio->mapping);
361 return bch2_err_class(ret);
366 struct bch_writepage_io {
367 struct bch_inode_info *inode;
370 struct bch_write_op op;
373 struct bch_writepage_state {
374 struct bch_writepage_io *io;
375 struct bch_io_opts opts;
376 struct bch_folio_sector *tmp;
377 unsigned tmp_sectors;
380 static inline struct bch_writepage_state bch_writepage_state_init(struct bch_fs *c,
381 struct bch_inode_info *inode)
383 struct bch_writepage_state ret = { 0 };
385 bch2_inode_opts_get(&ret.opts, c, &inode->ei_inode);
390 * Determine when a writepage io is full. We have to limit writepage bios to a
391 * single page per bvec (i.e. 1MB with 4k pages) because that is the limit to
392 * what the bounce path in bch2_write_extent() can handle. In theory we could
393 * loosen this restriction for non-bounce I/O, but we don't have that context
394 * here. Ideally, we can up this limit and make it configurable in the future
395 * when the bounce path can be enhanced to accommodate larger source bios.
397 static inline bool bch_io_full(struct bch_writepage_io *io, unsigned len)
399 struct bio *bio = &io->op.wbio.bio;
400 return bio_full(bio, len) ||
401 (bio->bi_iter.bi_size + len > BIO_MAX_VECS * PAGE_SIZE);
404 static void bch2_writepage_io_done(struct bch_write_op *op)
406 struct bch_writepage_io *io =
407 container_of(op, struct bch_writepage_io, op);
408 struct bch_fs *c = io->op.c;
409 struct bio *bio = &io->op.wbio.bio;
410 struct folio_iter fi;
414 set_bit(EI_INODE_ERROR, &io->inode->ei_flags);
416 bio_for_each_folio_all(fi, bio) {
419 folio_set_error(fi.folio);
420 mapping_set_error(fi.folio->mapping, -EIO);
422 s = __bch2_folio(fi.folio);
424 for (i = 0; i < folio_sectors(fi.folio); i++)
425 s->s[i].nr_replicas = 0;
426 spin_unlock(&s->lock);
430 if (io->op.flags & BCH_WRITE_WROTE_DATA_INLINE) {
431 bio_for_each_folio_all(fi, bio) {
434 s = __bch2_folio(fi.folio);
436 for (i = 0; i < folio_sectors(fi.folio); i++)
437 s->s[i].nr_replicas = 0;
438 spin_unlock(&s->lock);
443 * racing with fallocate can cause us to add fewer sectors than
444 * expected - but we shouldn't add more sectors than expected:
446 WARN_ON_ONCE(io->op.i_sectors_delta > 0);
449 * (error (due to going RO) halfway through a page can screw that up
452 BUG_ON(io->op.op.i_sectors_delta >= PAGE_SECTORS);
456 * PageWriteback is effectively our ref on the inode - fixup i_blocks
457 * before calling end_page_writeback:
459 bch2_i_sectors_acct(c, io->inode, NULL, io->op.i_sectors_delta);
461 bio_for_each_folio_all(fi, bio) {
462 struct bch_folio *s = __bch2_folio(fi.folio);
464 if (atomic_dec_and_test(&s->write_count))
465 folio_end_writeback(fi.folio);
468 bio_put(&io->op.wbio.bio);
471 static void bch2_writepage_do_io(struct bch_writepage_state *w)
473 struct bch_writepage_io *io = w->io;
476 closure_call(&io->op.cl, bch2_write, NULL, NULL);
480 * Get a bch_writepage_io and add @page to it - appending to an existing one if
481 * possible, else allocating a new one:
483 static void bch2_writepage_io_alloc(struct bch_fs *c,
484 struct writeback_control *wbc,
485 struct bch_writepage_state *w,
486 struct bch_inode_info *inode,
488 unsigned nr_replicas)
490 struct bch_write_op *op;
492 w->io = container_of(bio_alloc_bioset(NULL, BIO_MAX_VECS,
495 &c->writepage_bioset),
496 struct bch_writepage_io, op.wbio.bio);
498 w->io->inode = inode;
500 bch2_write_op_init(op, c, w->opts);
501 op->target = w->opts.foreground_target;
502 op->nr_replicas = nr_replicas;
503 op->res.nr_replicas = nr_replicas;
504 op->write_point = writepoint_hashed(inode->ei_last_dirtied);
505 op->subvol = inode->ei_subvol;
506 op->pos = POS(inode->v.i_ino, sector);
507 op->end_io = bch2_writepage_io_done;
508 op->devs_need_flush = &inode->ei_devs_need_flush;
509 op->wbio.bio.bi_iter.bi_sector = sector;
510 op->wbio.bio.bi_opf = wbc_to_write_flags(wbc);
513 static int __bch2_writepage(struct folio *folio,
514 struct writeback_control *wbc,
517 struct bch_inode_info *inode = to_bch_ei(folio->mapping->host);
518 struct bch_fs *c = inode->v.i_sb->s_fs_info;
519 struct bch_writepage_state *w = data;
521 unsigned i, offset, f_sectors, nr_replicas_this_write = U32_MAX;
522 loff_t i_size = i_size_read(&inode->v);
525 EBUG_ON(!folio_test_uptodate(folio));
527 /* Is the folio fully inside i_size? */
528 if (folio_end_pos(folio) <= i_size)
531 /* Is the folio fully outside i_size? (truncate in progress) */
532 if (folio_pos(folio) >= i_size) {
538 * The folio straddles i_size. It must be zeroed out on each and every
539 * writepage invocation because it may be mmapped. "A file is mapped
540 * in multiples of the folio size. For a file that is not a multiple of
541 * the folio size, the remaining memory is zeroed when mapped, and
542 * writes to that region are not written out to the file."
544 folio_zero_segment(folio,
545 i_size - folio_pos(folio),
548 f_sectors = folio_sectors(folio);
549 s = bch2_folio(folio);
551 if (f_sectors > w->tmp_sectors) {
553 w->tmp = kcalloc(f_sectors, sizeof(struct bch_folio_sector), __GFP_NOFAIL);
554 w->tmp_sectors = f_sectors;
558 * Things get really hairy with errors during writeback:
560 ret = bch2_get_folio_disk_reservation(c, inode, folio, false);
563 /* Before unlocking the page, get copy of reservations: */
565 memcpy(w->tmp, s->s, sizeof(struct bch_folio_sector) * f_sectors);
567 for (i = 0; i < f_sectors; i++) {
568 if (s->s[i].state < SECTOR_dirty)
571 nr_replicas_this_write =
572 min_t(unsigned, nr_replicas_this_write,
573 s->s[i].nr_replicas +
574 s->s[i].replicas_reserved);
577 for (i = 0; i < f_sectors; i++) {
578 if (s->s[i].state < SECTOR_dirty)
581 s->s[i].nr_replicas = w->opts.compression
582 ? 0 : nr_replicas_this_write;
584 s->s[i].replicas_reserved = 0;
585 bch2_folio_sector_set(folio, s, i, SECTOR_allocated);
587 spin_unlock(&s->lock);
589 BUG_ON(atomic_read(&s->write_count));
590 atomic_set(&s->write_count, 1);
592 BUG_ON(folio_test_writeback(folio));
593 folio_start_writeback(folio);
599 unsigned sectors = 0, dirty_sectors = 0, reserved_sectors = 0;
602 while (offset < f_sectors &&
603 w->tmp[offset].state < SECTOR_dirty)
606 if (offset == f_sectors)
609 while (offset + sectors < f_sectors &&
610 w->tmp[offset + sectors].state >= SECTOR_dirty) {
611 reserved_sectors += w->tmp[offset + sectors].replicas_reserved;
612 dirty_sectors += w->tmp[offset + sectors].state == SECTOR_dirty;
617 sector = folio_sector(folio) + offset;
620 (w->io->op.res.nr_replicas != nr_replicas_this_write ||
621 bch_io_full(w->io, sectors << 9) ||
622 bio_end_sector(&w->io->op.wbio.bio) != sector))
623 bch2_writepage_do_io(w);
626 bch2_writepage_io_alloc(c, wbc, w, inode, sector,
627 nr_replicas_this_write);
629 atomic_inc(&s->write_count);
631 BUG_ON(inode != w->io->inode);
632 BUG_ON(!bio_add_folio(&w->io->op.wbio.bio, folio,
633 sectors << 9, offset << 9));
635 /* Check for writing past i_size: */
636 WARN_ONCE((bio_end_sector(&w->io->op.wbio.bio) << 9) >
637 round_up(i_size, block_bytes(c)) &&
638 !test_bit(BCH_FS_emergency_ro, &c->flags),
639 "writing past i_size: %llu > %llu (unrounded %llu)\n",
640 bio_end_sector(&w->io->op.wbio.bio) << 9,
641 round_up(i_size, block_bytes(c)),
644 w->io->op.res.sectors += reserved_sectors;
645 w->io->op.i_sectors_delta -= dirty_sectors;
646 w->io->op.new_i_size = i_size;
651 if (atomic_dec_and_test(&s->write_count))
652 folio_end_writeback(folio);
657 int bch2_writepages(struct address_space *mapping, struct writeback_control *wbc)
659 struct bch_fs *c = mapping->host->i_sb->s_fs_info;
660 struct bch_writepage_state w =
661 bch_writepage_state_init(c, to_bch_ei(mapping->host));
662 struct blk_plug plug;
665 blk_start_plug(&plug);
666 ret = write_cache_pages(mapping, wbc, __bch2_writepage, &w);
668 bch2_writepage_do_io(&w);
669 blk_finish_plug(&plug);
671 return bch2_err_class(ret);
674 /* buffered writes: */
676 int bch2_write_begin(struct file *file, struct address_space *mapping,
677 loff_t pos, unsigned len,
678 struct page **pagep, void **fsdata)
680 struct bch_inode_info *inode = to_bch_ei(mapping->host);
681 struct bch_fs *c = inode->v.i_sb->s_fs_info;
682 struct bch2_folio_reservation *res;
687 res = kmalloc(sizeof(*res), GFP_KERNEL);
691 bch2_folio_reservation_init(c, inode, res);
694 bch2_pagecache_add_get(inode);
696 folio = __filemap_get_folio(mapping, pos >> PAGE_SHIFT,
697 FGP_LOCK|FGP_WRITE|FGP_CREAT|FGP_STABLE,
698 mapping_gfp_mask(mapping));
699 if (IS_ERR_OR_NULL(folio))
702 offset = pos - folio_pos(folio);
703 len = min_t(size_t, len, folio_end_pos(folio) - pos);
705 if (folio_test_uptodate(folio))
708 /* If we're writing entire folio, don't need to read it in first: */
709 if (!offset && len == folio_size(folio))
712 if (!offset && pos + len >= inode->v.i_size) {
713 folio_zero_segment(folio, len, folio_size(folio));
714 flush_dcache_folio(folio);
718 if (folio_pos(folio) >= inode->v.i_size) {
719 folio_zero_segments(folio, 0, offset, offset + len, folio_size(folio));
720 flush_dcache_folio(folio);
724 ret = bch2_read_single_folio(folio, mapping);
728 ret = bch2_folio_set(c, inode_inum(inode), &folio, 1);
732 ret = bch2_folio_reservation_get(c, inode, folio, res, offset, len);
734 if (!folio_test_uptodate(folio)) {
736 * If the folio hasn't been read in, we won't know if we
737 * actually need a reservation - we don't actually need
738 * to read here, we just need to check if the folio is
739 * fully backed by uncompressed data:
747 *pagep = &folio->page;
754 bch2_pagecache_add_put(inode);
757 return bch2_err_class(ret);
760 int bch2_write_end(struct file *file, struct address_space *mapping,
761 loff_t pos, unsigned len, unsigned copied,
762 struct page *page, void *fsdata)
764 struct bch_inode_info *inode = to_bch_ei(mapping->host);
765 struct bch_fs *c = inode->v.i_sb->s_fs_info;
766 struct bch2_folio_reservation *res = fsdata;
767 struct folio *folio = page_folio(page);
768 unsigned offset = pos - folio_pos(folio);
770 lockdep_assert_held(&inode->v.i_rwsem);
771 BUG_ON(offset + copied > folio_size(folio));
773 if (unlikely(copied < len && !folio_test_uptodate(folio))) {
775 * The folio needs to be read in, but that would destroy
776 * our partial write - simplest thing is to just force
777 * userspace to redo the write:
779 folio_zero_range(folio, 0, folio_size(folio));
780 flush_dcache_folio(folio);
784 spin_lock(&inode->v.i_lock);
785 if (pos + copied > inode->v.i_size)
786 i_size_write(&inode->v, pos + copied);
787 spin_unlock(&inode->v.i_lock);
790 if (!folio_test_uptodate(folio))
791 folio_mark_uptodate(folio);
793 bch2_set_folio_dirty(c, inode, folio, res, offset, copied);
795 inode->ei_last_dirtied = (unsigned long) current;
800 bch2_pagecache_add_put(inode);
802 bch2_folio_reservation_put(c, inode, res);
808 static noinline void folios_trunc(folios *fs, struct folio **fi)
810 while (fs->data + fs->nr > fi) {
811 struct folio *f = darray_pop(fs);
818 static int __bch2_buffered_write(struct bch_inode_info *inode,
819 struct address_space *mapping,
820 struct iov_iter *iter,
821 loff_t pos, unsigned len)
823 struct bch_fs *c = inode->v.i_sb->s_fs_info;
824 struct bch2_folio_reservation res;
827 unsigned copied = 0, f_offset, f_copied;
828 u64 end = pos + len, f_pos, f_len;
829 loff_t last_folio_pos = inode->v.i_size;
834 bch2_folio_reservation_init(c, inode, &res);
837 ret = bch2_filemap_get_contig_folios_d(mapping, pos, end,
838 FGP_LOCK|FGP_WRITE|FGP_STABLE|FGP_CREAT,
839 mapping_gfp_mask(mapping),
846 f = darray_first(fs);
847 if (pos != folio_pos(f) && !folio_test_uptodate(f)) {
848 ret = bch2_read_single_folio(f, mapping);
854 end = min(end, folio_end_pos(f));
855 last_folio_pos = folio_pos(f);
856 if (end != folio_end_pos(f) && !folio_test_uptodate(f)) {
857 if (end >= inode->v.i_size) {
858 folio_zero_range(f, 0, folio_size(f));
860 ret = bch2_read_single_folio(f, mapping);
866 ret = bch2_folio_set(c, inode_inum(inode), fs.data, fs.nr);
871 f_offset = pos - folio_pos(darray_first(fs));
872 darray_for_each(fs, fi) {
874 f_len = min(end, folio_end_pos(f)) - f_pos;
877 * XXX: per POSIX and fstests generic/275, on -ENOSPC we're
878 * supposed to write as much as we have disk space for.
880 * On failure here we should still write out a partial page if
881 * we aren't completely out of disk space - we don't do that
884 ret = bch2_folio_reservation_get(c, inode, f, &res, f_offset, f_len);
886 folios_trunc(&fs, fi);
890 end = min(end, folio_end_pos(darray_last(fs)));
894 f_pos = folio_end_pos(f);
898 if (mapping_writably_mapped(mapping))
899 darray_for_each(fs, fi)
900 flush_dcache_folio(*fi);
903 f_offset = pos - folio_pos(darray_first(fs));
904 darray_for_each(fs, fi) {
906 f_len = min(end, folio_end_pos(f)) - f_pos;
907 f_copied = copy_page_from_iter_atomic(&f->page, f_offset, f_len, iter);
909 folios_trunc(&fs, fi);
913 if (!folio_test_uptodate(f) &&
914 f_copied != folio_size(f) &&
915 pos + copied + f_copied < inode->v.i_size) {
916 iov_iter_revert(iter, f_copied);
917 folio_zero_range(f, 0, folio_size(f));
918 folios_trunc(&fs, fi);
922 flush_dcache_folio(f);
925 if (f_copied != f_len) {
926 folios_trunc(&fs, fi + 1);
930 f_pos = folio_end_pos(f);
939 spin_lock(&inode->v.i_lock);
940 if (end > inode->v.i_size)
941 i_size_write(&inode->v, end);
942 spin_unlock(&inode->v.i_lock);
945 f_offset = pos - folio_pos(darray_first(fs));
946 darray_for_each(fs, fi) {
948 f_len = min(end, folio_end_pos(f)) - f_pos;
950 if (!folio_test_uptodate(f))
951 folio_mark_uptodate(f);
953 bch2_set_folio_dirty(c, inode, f, &res, f_offset, f_len);
955 f_pos = folio_end_pos(f);
959 inode->ei_last_dirtied = (unsigned long) current;
961 darray_for_each(fs, fi) {
967 * If the last folio added to the mapping starts beyond current EOF, we
968 * performed a short write but left around at least one post-EOF folio.
969 * Clean up the mapping before we return.
971 if (last_folio_pos >= inode->v.i_size)
972 truncate_pagecache(&inode->v, inode->v.i_size);
975 bch2_folio_reservation_put(c, inode, &res);
977 return copied ?: ret;
980 static ssize_t bch2_buffered_write(struct kiocb *iocb, struct iov_iter *iter)
982 struct file *file = iocb->ki_filp;
983 struct address_space *mapping = file->f_mapping;
984 struct bch_inode_info *inode = file_bch_inode(file);
985 loff_t pos = iocb->ki_pos;
989 bch2_pagecache_add_get(inode);
992 unsigned offset = pos & (PAGE_SIZE - 1);
993 unsigned bytes = iov_iter_count(iter);
996 * Bring in the user page that we will copy from _first_.
997 * Otherwise there's a nasty deadlock on copying from the
998 * same page as we're writing to, without it being marked
1001 * Not only is this an optimisation, but it is also required
1002 * to check that the address is actually valid, when atomic
1003 * usercopies are used, below.
1005 if (unlikely(fault_in_iov_iter_readable(iter, bytes))) {
1006 bytes = min_t(unsigned long, iov_iter_count(iter),
1007 PAGE_SIZE - offset);
1009 if (unlikely(fault_in_iov_iter_readable(iter, bytes))) {
1015 if (unlikely(fatal_signal_pending(current))) {
1020 ret = __bch2_buffered_write(inode, mapping, iter, pos, bytes);
1021 if (unlikely(ret < 0))
1026 if (unlikely(ret == 0)) {
1028 * If we were unable to copy any data at all, we must
1029 * fall back to a single segment length write.
1031 * If we didn't fallback here, we could livelock
1032 * because not all segments in the iov can be copied at
1033 * once without a pagefault.
1035 bytes = min_t(unsigned long, PAGE_SIZE - offset,
1036 iov_iter_single_seg_count(iter));
1043 balance_dirty_pages_ratelimited(mapping);
1044 } while (iov_iter_count(iter));
1046 bch2_pagecache_add_put(inode);
1048 return written ? written : ret;
1051 ssize_t bch2_write_iter(struct kiocb *iocb, struct iov_iter *from)
1053 struct file *file = iocb->ki_filp;
1054 struct bch_inode_info *inode = file_bch_inode(file);
1057 if (iocb->ki_flags & IOCB_DIRECT) {
1058 ret = bch2_direct_write(iocb, from);
1062 inode_lock(&inode->v);
1064 ret = generic_write_checks(iocb, from);
1068 ret = file_remove_privs(file);
1072 ret = file_update_time(file);
1076 ret = bch2_buffered_write(iocb, from);
1077 if (likely(ret > 0))
1078 iocb->ki_pos += ret;
1080 inode_unlock(&inode->v);
1083 ret = generic_write_sync(iocb, ret);
1085 return bch2_err_class(ret);
1088 void bch2_fs_fs_io_buffered_exit(struct bch_fs *c)
1090 bioset_exit(&c->writepage_bioset);
1093 int bch2_fs_fs_io_buffered_init(struct bch_fs *c)
1095 if (bioset_init(&c->writepage_bioset,
1096 4, offsetof(struct bch_writepage_io, op.wbio.bio),
1098 return -BCH_ERR_ENOMEM_writepage_bioset_init;
1103 #endif /* NO_BCACHEFS_FS */