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)
58 memset(iter, 0, sizeof(*iter));
60 iter->mapping = ractl->mapping;
62 ret = bch2_filemap_get_contig_folios_d(iter->mapping,
63 ractl->_index << PAGE_SHIFT,
64 (ractl->_index + ractl->_nr_pages) << PAGE_SHIFT,
65 0, mapping_gfp_mask(iter->mapping),
70 darray_for_each(iter->folios, fi) {
71 ractl->_nr_pages -= 1U << folio_order(*fi);
72 __bch2_folio_create(*fi, __GFP_NOFAIL|GFP_KERNEL);
80 static inline struct folio *readpage_iter_peek(struct readpages_iter *iter)
82 if (iter->idx >= iter->folios.nr)
84 return iter->folios.data[iter->idx];
87 static inline void readpage_iter_advance(struct readpages_iter *iter)
92 static bool extent_partial_reads_expensive(struct bkey_s_c k)
94 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
95 struct bch_extent_crc_unpacked crc;
96 const union bch_extent_entry *i;
98 bkey_for_each_crc(k.k, ptrs, crc, i)
99 if (crc.csum_type || crc.compression_type)
104 static int readpage_bio_extend(struct btree_trans *trans,
105 struct readpages_iter *iter,
107 unsigned sectors_this_extent,
110 /* Don't hold btree locks while allocating memory: */
111 bch2_trans_unlock(trans);
113 while (bio_sectors(bio) < sectors_this_extent &&
114 bio->bi_vcnt < bio->bi_max_vecs) {
115 struct folio *folio = readpage_iter_peek(iter);
119 readpage_iter_advance(iter);
121 pgoff_t folio_offset = bio_end_sector(bio) >> PAGE_SECTORS_SHIFT;
126 folio = xa_load(&iter->mapping->i_pages, folio_offset);
127 if (folio && !xa_is_value(folio))
130 folio = filemap_alloc_folio(readahead_gfp_mask(iter->mapping), 0);
134 if (!__bch2_folio_create(folio, GFP_KERNEL)) {
139 ret = filemap_add_folio(iter->mapping, folio, folio_offset, GFP_KERNEL);
141 __bch2_folio_release(folio);
149 BUG_ON(folio_sector(folio) != bio_end_sector(bio));
151 BUG_ON(!bio_add_folio(bio, folio, folio_size(folio), 0));
154 return bch2_trans_relock(trans);
157 static void bchfs_read(struct btree_trans *trans,
158 struct bch_read_bio *rbio,
160 struct readpages_iter *readpages_iter)
162 struct bch_fs *c = trans->c;
163 struct btree_iter iter;
165 int flags = BCH_READ_RETRY_IF_STALE|
166 BCH_READ_MAY_PROMOTE;
171 rbio->start_time = local_clock();
172 rbio->subvol = inum.subvol;
174 bch2_bkey_buf_init(&sk);
176 bch2_trans_begin(trans);
177 iter = (struct btree_iter) { NULL };
179 ret = bch2_subvolume_get_snapshot(trans, inum.subvol, &snapshot);
183 bch2_trans_iter_init(trans, &iter, BTREE_ID_extents,
184 SPOS(inum.inum, rbio->bio.bi_iter.bi_sector, snapshot),
188 unsigned bytes, sectors, offset_into_extent;
189 enum btree_id data_btree = BTREE_ID_extents;
192 * read_extent -> io_time_reset may cause a transaction restart
193 * without returning an error, we need to check for that here:
195 ret = bch2_trans_relock(trans);
199 bch2_btree_iter_set_pos(&iter,
200 POS(inum.inum, rbio->bio.bi_iter.bi_sector));
202 k = bch2_btree_iter_peek_slot(&iter);
207 offset_into_extent = iter.pos.offset -
208 bkey_start_offset(k.k);
209 sectors = k.k->size - offset_into_extent;
211 bch2_bkey_buf_reassemble(&sk, c, k);
213 ret = bch2_read_indirect_extent(trans, &data_btree,
214 &offset_into_extent, &sk);
218 k = bkey_i_to_s_c(sk.k);
220 sectors = min(sectors, k.k->size - offset_into_extent);
222 if (readpages_iter) {
223 ret = readpage_bio_extend(trans, readpages_iter, &rbio->bio, sectors,
224 extent_partial_reads_expensive(k));
229 bytes = min(sectors, bio_sectors(&rbio->bio)) << 9;
230 swap(rbio->bio.bi_iter.bi_size, bytes);
232 if (rbio->bio.bi_iter.bi_size == bytes)
233 flags |= BCH_READ_LAST_FRAGMENT;
235 bch2_bio_page_state_set(&rbio->bio, k);
237 bch2_read_extent(trans, rbio, iter.pos,
238 data_btree, k, offset_into_extent, flags);
240 if (flags & BCH_READ_LAST_FRAGMENT)
243 swap(rbio->bio.bi_iter.bi_size, bytes);
244 bio_advance(&rbio->bio, bytes);
246 ret = btree_trans_too_many_iters(trans);
251 bch2_trans_iter_exit(trans, &iter);
253 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
257 bch_err_inum_offset_ratelimited(c,
259 iter.pos.offset << 9,
260 "read error %i from btree lookup", ret);
261 rbio->bio.bi_status = BLK_STS_IOERR;
262 bio_endio(&rbio->bio);
265 bch2_bkey_buf_exit(&sk, c);
268 void bch2_readahead(struct readahead_control *ractl)
270 struct bch_inode_info *inode = to_bch_ei(ractl->mapping->host);
271 struct bch_fs *c = inode->v.i_sb->s_fs_info;
272 struct bch_io_opts opts;
273 struct btree_trans *trans = bch2_trans_get(c);
275 struct readpages_iter readpages_iter;
278 bch2_inode_opts_get(&opts, c, &inode->ei_inode);
280 ret = readpages_iter_init(&readpages_iter, ractl);
283 bch2_pagecache_add_get(inode);
285 while ((folio = readpage_iter_peek(&readpages_iter))) {
286 unsigned n = min_t(unsigned,
287 readpages_iter.folios.nr -
290 struct bch_read_bio *rbio =
291 rbio_init(bio_alloc_bioset(NULL, n, REQ_OP_READ,
292 GFP_KERNEL, &c->bio_read),
295 readpage_iter_advance(&readpages_iter);
297 rbio->bio.bi_iter.bi_sector = folio_sector(folio);
298 rbio->bio.bi_end_io = bch2_readpages_end_io;
299 BUG_ON(!bio_add_folio(&rbio->bio, folio, folio_size(folio), 0));
301 bchfs_read(trans, rbio, inode_inum(inode),
303 bch2_trans_unlock(trans);
306 bch2_pagecache_add_put(inode);
308 bch2_trans_put(trans);
309 darray_exit(&readpages_iter.folios);
312 static void __bchfs_readfolio(struct bch_fs *c, struct bch_read_bio *rbio,
313 subvol_inum inum, struct folio *folio)
315 bch2_folio_create(folio, __GFP_NOFAIL);
317 rbio->bio.bi_opf = REQ_OP_READ|REQ_SYNC;
318 rbio->bio.bi_iter.bi_sector = folio_sector(folio);
319 BUG_ON(!bio_add_folio(&rbio->bio, folio, folio_size(folio), 0));
321 bch2_trans_run(c, (bchfs_read(trans, rbio, inum, NULL), 0));
324 static void bch2_read_single_folio_end_io(struct bio *bio)
326 complete(bio->bi_private);
329 int bch2_read_single_folio(struct folio *folio, struct address_space *mapping)
331 struct bch_inode_info *inode = to_bch_ei(mapping->host);
332 struct bch_fs *c = inode->v.i_sb->s_fs_info;
333 struct bch_read_bio *rbio;
334 struct bch_io_opts opts;
336 DECLARE_COMPLETION_ONSTACK(done);
338 bch2_inode_opts_get(&opts, c, &inode->ei_inode);
340 rbio = rbio_init(bio_alloc_bioset(NULL, 1, REQ_OP_READ, GFP_KERNEL, &c->bio_read),
342 rbio->bio.bi_private = &done;
343 rbio->bio.bi_end_io = bch2_read_single_folio_end_io;
345 __bchfs_readfolio(c, rbio, inode_inum(inode), folio);
346 wait_for_completion(&done);
348 ret = blk_status_to_errno(rbio->bio.bi_status);
354 folio_mark_uptodate(folio);
358 int bch2_read_folio(struct file *file, struct folio *folio)
362 ret = bch2_read_single_folio(folio, folio->mapping);
364 return bch2_err_class(ret);
369 struct bch_writepage_io {
370 struct bch_inode_info *inode;
373 struct bch_write_op op;
376 struct bch_writepage_state {
377 struct bch_writepage_io *io;
378 struct bch_io_opts opts;
379 struct bch_folio_sector *tmp;
380 unsigned tmp_sectors;
383 static inline struct bch_writepage_state bch_writepage_state_init(struct bch_fs *c,
384 struct bch_inode_info *inode)
386 struct bch_writepage_state ret = { 0 };
388 bch2_inode_opts_get(&ret.opts, c, &inode->ei_inode);
392 static void bch2_writepage_io_done(struct bch_write_op *op)
394 struct bch_writepage_io *io =
395 container_of(op, struct bch_writepage_io, op);
396 struct bch_fs *c = io->op.c;
397 struct bio *bio = &io->op.wbio.bio;
398 struct folio_iter fi;
402 set_bit(EI_INODE_ERROR, &io->inode->ei_flags);
404 bio_for_each_folio_all(fi, bio) {
407 folio_set_error(fi.folio);
408 mapping_set_error(fi.folio->mapping, -EIO);
410 s = __bch2_folio(fi.folio);
412 for (i = 0; i < folio_sectors(fi.folio); i++)
413 s->s[i].nr_replicas = 0;
414 spin_unlock(&s->lock);
418 if (io->op.flags & BCH_WRITE_WROTE_DATA_INLINE) {
419 bio_for_each_folio_all(fi, bio) {
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);
431 * racing with fallocate can cause us to add fewer sectors than
432 * expected - but we shouldn't add more sectors than expected:
434 WARN_ON_ONCE(io->op.i_sectors_delta > 0);
437 * (error (due to going RO) halfway through a page can screw that up
440 BUG_ON(io->op.op.i_sectors_delta >= PAGE_SECTORS);
444 * PageWriteback is effectively our ref on the inode - fixup i_blocks
445 * before calling end_page_writeback:
447 bch2_i_sectors_acct(c, io->inode, NULL, io->op.i_sectors_delta);
449 bio_for_each_folio_all(fi, bio) {
450 struct bch_folio *s = __bch2_folio(fi.folio);
452 if (atomic_dec_and_test(&s->write_count))
453 folio_end_writeback(fi.folio);
456 bio_put(&io->op.wbio.bio);
459 static void bch2_writepage_do_io(struct bch_writepage_state *w)
461 struct bch_writepage_io *io = w->io;
464 closure_call(&io->op.cl, bch2_write, NULL, NULL);
468 * Get a bch_writepage_io and add @page to it - appending to an existing one if
469 * possible, else allocating a new one:
471 static void bch2_writepage_io_alloc(struct bch_fs *c,
472 struct writeback_control *wbc,
473 struct bch_writepage_state *w,
474 struct bch_inode_info *inode,
476 unsigned nr_replicas)
478 struct bch_write_op *op;
480 w->io = container_of(bio_alloc_bioset(NULL, BIO_MAX_VECS,
483 &c->writepage_bioset),
484 struct bch_writepage_io, op.wbio.bio);
486 w->io->inode = inode;
488 bch2_write_op_init(op, c, w->opts);
489 op->target = w->opts.foreground_target;
490 op->nr_replicas = nr_replicas;
491 op->res.nr_replicas = nr_replicas;
492 op->write_point = writepoint_hashed(inode->ei_last_dirtied);
493 op->subvol = inode->ei_subvol;
494 op->pos = POS(inode->v.i_ino, sector);
495 op->end_io = bch2_writepage_io_done;
496 op->devs_need_flush = &inode->ei_devs_need_flush;
497 op->wbio.bio.bi_iter.bi_sector = sector;
498 op->wbio.bio.bi_opf = wbc_to_write_flags(wbc);
501 static int __bch2_writepage(struct folio *folio,
502 struct writeback_control *wbc,
505 struct bch_inode_info *inode = to_bch_ei(folio->mapping->host);
506 struct bch_fs *c = inode->v.i_sb->s_fs_info;
507 struct bch_writepage_state *w = data;
509 unsigned i, offset, f_sectors, nr_replicas_this_write = U32_MAX;
510 loff_t i_size = i_size_read(&inode->v);
513 EBUG_ON(!folio_test_uptodate(folio));
515 /* Is the folio fully inside i_size? */
516 if (folio_end_pos(folio) <= i_size)
519 /* Is the folio fully outside i_size? (truncate in progress) */
520 if (folio_pos(folio) >= i_size) {
526 * The folio straddles i_size. It must be zeroed out on each and every
527 * writepage invocation because it may be mmapped. "A file is mapped
528 * in multiples of the folio size. For a file that is not a multiple of
529 * the folio size, the remaining memory is zeroed when mapped, and
530 * writes to that region are not written out to the file."
532 folio_zero_segment(folio,
533 i_size - folio_pos(folio),
536 f_sectors = folio_sectors(folio);
537 s = bch2_folio(folio);
539 if (f_sectors > w->tmp_sectors) {
541 w->tmp = kcalloc(f_sectors, sizeof(struct bch_folio_sector), __GFP_NOFAIL);
542 w->tmp_sectors = f_sectors;
546 * Things get really hairy with errors during writeback:
548 ret = bch2_get_folio_disk_reservation(c, inode, folio, false);
551 /* Before unlocking the page, get copy of reservations: */
553 memcpy(w->tmp, s->s, sizeof(struct bch_folio_sector) * f_sectors);
555 for (i = 0; i < f_sectors; i++) {
556 if (s->s[i].state < SECTOR_dirty)
559 nr_replicas_this_write =
560 min_t(unsigned, nr_replicas_this_write,
561 s->s[i].nr_replicas +
562 s->s[i].replicas_reserved);
565 for (i = 0; i < f_sectors; i++) {
566 if (s->s[i].state < SECTOR_dirty)
569 s->s[i].nr_replicas = w->opts.compression
570 ? 0 : nr_replicas_this_write;
572 s->s[i].replicas_reserved = 0;
573 bch2_folio_sector_set(folio, s, i, SECTOR_allocated);
575 spin_unlock(&s->lock);
577 BUG_ON(atomic_read(&s->write_count));
578 atomic_set(&s->write_count, 1);
580 BUG_ON(folio_test_writeback(folio));
581 folio_start_writeback(folio);
587 unsigned sectors = 0, dirty_sectors = 0, reserved_sectors = 0;
590 while (offset < f_sectors &&
591 w->tmp[offset].state < SECTOR_dirty)
594 if (offset == f_sectors)
597 while (offset + sectors < f_sectors &&
598 w->tmp[offset + sectors].state >= SECTOR_dirty) {
599 reserved_sectors += w->tmp[offset + sectors].replicas_reserved;
600 dirty_sectors += w->tmp[offset + sectors].state == SECTOR_dirty;
605 sector = folio_sector(folio) + offset;
608 (w->io->op.res.nr_replicas != nr_replicas_this_write ||
609 bio_full(&w->io->op.wbio.bio, sectors << 9) ||
610 w->io->op.wbio.bio.bi_iter.bi_size + (sectors << 9) >=
611 (BIO_MAX_VECS * PAGE_SIZE) ||
612 bio_end_sector(&w->io->op.wbio.bio) != sector))
613 bch2_writepage_do_io(w);
616 bch2_writepage_io_alloc(c, wbc, w, inode, sector,
617 nr_replicas_this_write);
619 atomic_inc(&s->write_count);
621 BUG_ON(inode != w->io->inode);
622 BUG_ON(!bio_add_folio(&w->io->op.wbio.bio, folio,
623 sectors << 9, offset << 9));
625 /* Check for writing past i_size: */
626 WARN_ONCE((bio_end_sector(&w->io->op.wbio.bio) << 9) >
627 round_up(i_size, block_bytes(c)) &&
628 !test_bit(BCH_FS_EMERGENCY_RO, &c->flags),
629 "writing past i_size: %llu > %llu (unrounded %llu)\n",
630 bio_end_sector(&w->io->op.wbio.bio) << 9,
631 round_up(i_size, block_bytes(c)),
634 w->io->op.res.sectors += reserved_sectors;
635 w->io->op.i_sectors_delta -= dirty_sectors;
636 w->io->op.new_i_size = i_size;
641 if (atomic_dec_and_test(&s->write_count))
642 folio_end_writeback(folio);
647 int bch2_writepages(struct address_space *mapping, struct writeback_control *wbc)
649 struct bch_fs *c = mapping->host->i_sb->s_fs_info;
650 struct bch_writepage_state w =
651 bch_writepage_state_init(c, to_bch_ei(mapping->host));
652 struct blk_plug plug;
655 blk_start_plug(&plug);
656 ret = write_cache_pages(mapping, wbc, __bch2_writepage, &w);
658 bch2_writepage_do_io(&w);
659 blk_finish_plug(&plug);
661 return bch2_err_class(ret);
664 /* buffered writes: */
666 int bch2_write_begin(struct file *file, struct address_space *mapping,
667 loff_t pos, unsigned len,
668 struct page **pagep, void **fsdata)
670 struct bch_inode_info *inode = to_bch_ei(mapping->host);
671 struct bch_fs *c = inode->v.i_sb->s_fs_info;
672 struct bch2_folio_reservation *res;
677 res = kmalloc(sizeof(*res), GFP_KERNEL);
681 bch2_folio_reservation_init(c, inode, res);
684 bch2_pagecache_add_get(inode);
686 folio = __filemap_get_folio(mapping, pos >> PAGE_SHIFT,
687 FGP_LOCK|FGP_WRITE|FGP_CREAT|FGP_STABLE,
688 mapping_gfp_mask(mapping));
689 if (IS_ERR_OR_NULL(folio))
692 offset = pos - folio_pos(folio);
693 len = min_t(size_t, len, folio_end_pos(folio) - pos);
695 if (folio_test_uptodate(folio))
698 /* If we're writing entire folio, don't need to read it in first: */
699 if (!offset && len == folio_size(folio))
702 if (!offset && pos + len >= inode->v.i_size) {
703 folio_zero_segment(folio, len, folio_size(folio));
704 flush_dcache_folio(folio);
708 if (folio_pos(folio) >= inode->v.i_size) {
709 folio_zero_segments(folio, 0, offset, offset + len, folio_size(folio));
710 flush_dcache_folio(folio);
714 ret = bch2_read_single_folio(folio, mapping);
718 ret = bch2_folio_set(c, inode_inum(inode), &folio, 1);
722 ret = bch2_folio_reservation_get(c, inode, folio, res, offset, len);
724 if (!folio_test_uptodate(folio)) {
726 * If the folio hasn't been read in, we won't know if we
727 * actually need a reservation - we don't actually need
728 * to read here, we just need to check if the folio is
729 * fully backed by uncompressed data:
737 *pagep = &folio->page;
744 bch2_pagecache_add_put(inode);
747 return bch2_err_class(ret);
750 int bch2_write_end(struct file *file, struct address_space *mapping,
751 loff_t pos, unsigned len, unsigned copied,
752 struct page *page, void *fsdata)
754 struct bch_inode_info *inode = to_bch_ei(mapping->host);
755 struct bch_fs *c = inode->v.i_sb->s_fs_info;
756 struct bch2_folio_reservation *res = fsdata;
757 struct folio *folio = page_folio(page);
758 unsigned offset = pos - folio_pos(folio);
760 lockdep_assert_held(&inode->v.i_rwsem);
761 BUG_ON(offset + copied > folio_size(folio));
763 if (unlikely(copied < len && !folio_test_uptodate(folio))) {
765 * The folio needs to be read in, but that would destroy
766 * our partial write - simplest thing is to just force
767 * userspace to redo the write:
769 folio_zero_range(folio, 0, folio_size(folio));
770 flush_dcache_folio(folio);
774 spin_lock(&inode->v.i_lock);
775 if (pos + copied > inode->v.i_size)
776 i_size_write(&inode->v, pos + copied);
777 spin_unlock(&inode->v.i_lock);
780 if (!folio_test_uptodate(folio))
781 folio_mark_uptodate(folio);
783 bch2_set_folio_dirty(c, inode, folio, res, offset, copied);
785 inode->ei_last_dirtied = (unsigned long) current;
790 bch2_pagecache_add_put(inode);
792 bch2_folio_reservation_put(c, inode, res);
798 static noinline void folios_trunc(folios *fs, struct folio **fi)
800 while (fs->data + fs->nr > fi) {
801 struct folio *f = darray_pop(fs);
808 static int __bch2_buffered_write(struct bch_inode_info *inode,
809 struct address_space *mapping,
810 struct iov_iter *iter,
811 loff_t pos, unsigned len)
813 struct bch_fs *c = inode->v.i_sb->s_fs_info;
814 struct bch2_folio_reservation res;
816 struct folio **fi, *f;
817 unsigned copied = 0, f_offset, f_copied;
818 u64 end = pos + len, f_pos, f_len;
819 loff_t last_folio_pos = inode->v.i_size;
824 bch2_folio_reservation_init(c, inode, &res);
827 ret = bch2_filemap_get_contig_folios_d(mapping, pos, end,
828 FGP_LOCK|FGP_WRITE|FGP_STABLE|FGP_CREAT,
829 mapping_gfp_mask(mapping),
836 f = darray_first(fs);
837 if (pos != folio_pos(f) && !folio_test_uptodate(f)) {
838 ret = bch2_read_single_folio(f, mapping);
844 end = min(end, folio_end_pos(f));
845 last_folio_pos = folio_pos(f);
846 if (end != folio_end_pos(f) && !folio_test_uptodate(f)) {
847 if (end >= inode->v.i_size) {
848 folio_zero_range(f, 0, folio_size(f));
850 ret = bch2_read_single_folio(f, mapping);
856 ret = bch2_folio_set(c, inode_inum(inode), fs.data, fs.nr);
861 f_offset = pos - folio_pos(darray_first(fs));
862 darray_for_each(fs, fi) {
864 f_len = min(end, folio_end_pos(f)) - f_pos;
867 * XXX: per POSIX and fstests generic/275, on -ENOSPC we're
868 * supposed to write as much as we have disk space for.
870 * On failure here we should still write out a partial page if
871 * we aren't completely out of disk space - we don't do that
874 ret = bch2_folio_reservation_get(c, inode, f, &res, f_offset, f_len);
876 folios_trunc(&fs, fi);
880 end = min(end, folio_end_pos(darray_last(fs)));
884 f_pos = folio_end_pos(f);
888 if (mapping_writably_mapped(mapping))
889 darray_for_each(fs, fi)
890 flush_dcache_folio(*fi);
893 f_offset = pos - folio_pos(darray_first(fs));
894 darray_for_each(fs, fi) {
896 f_len = min(end, folio_end_pos(f)) - f_pos;
897 f_copied = copy_page_from_iter_atomic(&f->page, f_offset, f_len, iter);
899 folios_trunc(&fs, fi);
903 if (!folio_test_uptodate(f) &&
904 f_copied != folio_size(f) &&
905 pos + copied + f_copied < inode->v.i_size) {
906 iov_iter_revert(iter, f_copied);
907 folio_zero_range(f, 0, folio_size(f));
908 folios_trunc(&fs, fi);
912 flush_dcache_folio(f);
915 if (f_copied != f_len) {
916 folios_trunc(&fs, fi + 1);
920 f_pos = folio_end_pos(f);
929 spin_lock(&inode->v.i_lock);
930 if (end > inode->v.i_size)
931 i_size_write(&inode->v, end);
932 spin_unlock(&inode->v.i_lock);
935 f_offset = pos - folio_pos(darray_first(fs));
936 darray_for_each(fs, fi) {
938 f_len = min(end, folio_end_pos(f)) - f_pos;
940 if (!folio_test_uptodate(f))
941 folio_mark_uptodate(f);
943 bch2_set_folio_dirty(c, inode, f, &res, f_offset, f_len);
945 f_pos = folio_end_pos(f);
949 inode->ei_last_dirtied = (unsigned long) current;
951 darray_for_each(fs, fi) {
957 * If the last folio added to the mapping starts beyond current EOF, we
958 * performed a short write but left around at least one post-EOF folio.
959 * Clean up the mapping before we return.
961 if (last_folio_pos >= inode->v.i_size)
962 truncate_pagecache(&inode->v, inode->v.i_size);
965 bch2_folio_reservation_put(c, inode, &res);
967 return copied ?: ret;
970 static ssize_t bch2_buffered_write(struct kiocb *iocb, struct iov_iter *iter)
972 struct file *file = iocb->ki_filp;
973 struct address_space *mapping = file->f_mapping;
974 struct bch_inode_info *inode = file_bch_inode(file);
975 loff_t pos = iocb->ki_pos;
979 bch2_pagecache_add_get(inode);
982 unsigned offset = pos & (PAGE_SIZE - 1);
983 unsigned bytes = iov_iter_count(iter);
986 * Bring in the user page that we will copy from _first_.
987 * Otherwise there's a nasty deadlock on copying from the
988 * same page as we're writing to, without it being marked
991 * Not only is this an optimisation, but it is also required
992 * to check that the address is actually valid, when atomic
993 * usercopies are used, below.
995 if (unlikely(fault_in_iov_iter_readable(iter, bytes))) {
996 bytes = min_t(unsigned long, iov_iter_count(iter),
999 if (unlikely(fault_in_iov_iter_readable(iter, bytes))) {
1005 if (unlikely(fatal_signal_pending(current))) {
1010 ret = __bch2_buffered_write(inode, mapping, iter, pos, bytes);
1011 if (unlikely(ret < 0))
1016 if (unlikely(ret == 0)) {
1018 * If we were unable to copy any data at all, we must
1019 * fall back to a single segment length write.
1021 * If we didn't fallback here, we could livelock
1022 * because not all segments in the iov can be copied at
1023 * once without a pagefault.
1025 bytes = min_t(unsigned long, PAGE_SIZE - offset,
1026 iov_iter_single_seg_count(iter));
1033 balance_dirty_pages_ratelimited(mapping);
1034 } while (iov_iter_count(iter));
1036 bch2_pagecache_add_put(inode);
1038 return written ? written : ret;
1041 ssize_t bch2_write_iter(struct kiocb *iocb, struct iov_iter *from)
1043 struct file *file = iocb->ki_filp;
1044 struct bch_inode_info *inode = file_bch_inode(file);
1047 if (iocb->ki_flags & IOCB_DIRECT) {
1048 ret = bch2_direct_write(iocb, from);
1052 inode_lock(&inode->v);
1054 ret = generic_write_checks(iocb, from);
1058 ret = file_remove_privs(file);
1062 ret = file_update_time(file);
1066 ret = bch2_buffered_write(iocb, from);
1067 if (likely(ret > 0))
1068 iocb->ki_pos += ret;
1070 inode_unlock(&inode->v);
1073 ret = generic_write_sync(iocb, ret);
1075 return bch2_err_class(ret);
1078 void bch2_fs_fs_io_buffered_exit(struct bch_fs *c)
1080 bioset_exit(&c->writepage_bioset);
1083 int bch2_fs_fs_io_buffered_init(struct bch_fs *c)
1085 if (bioset_init(&c->writepage_bioset,
1086 4, offsetof(struct bch_writepage_io, op.wbio.bio),
1088 return -BCH_ERR_ENOMEM_writepage_bioset_init;
1093 #endif /* NO_BCACHEFS_FS */