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 folio_end_read(fi.folio, bio->bi_status == BLK_STS_OK);
39 struct readpages_iter {
40 struct address_space *mapping;
45 static int readpages_iter_init(struct readpages_iter *iter,
46 struct readahead_control *ractl)
50 *iter = (struct readpages_iter) { ractl->mapping };
52 while ((folio = __readahead_folio(ractl))) {
53 if (!bch2_folio_create(folio, GFP_KERNEL) ||
54 darray_push(&iter->folios, folio)) {
55 bch2_folio_release(folio);
56 ractl->_nr_pages += folio_nr_pages(folio);
57 ractl->_index -= folio_nr_pages(folio);
58 return iter->folios.nr ? 0 : -ENOMEM;
67 static inline struct folio *readpage_iter_peek(struct readpages_iter *iter)
69 if (iter->idx >= iter->folios.nr)
71 return iter->folios.data[iter->idx];
74 static inline void readpage_iter_advance(struct readpages_iter *iter)
79 static bool extent_partial_reads_expensive(struct bkey_s_c k)
81 struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
82 struct bch_extent_crc_unpacked crc;
83 const union bch_extent_entry *i;
85 bkey_for_each_crc(k.k, ptrs, crc, i)
86 if (crc.csum_type || crc.compression_type)
91 static int readpage_bio_extend(struct btree_trans *trans,
92 struct readpages_iter *iter,
94 unsigned sectors_this_extent,
97 /* Don't hold btree locks while allocating memory: */
98 bch2_trans_unlock(trans);
100 while (bio_sectors(bio) < sectors_this_extent &&
101 bio->bi_vcnt < bio->bi_max_vecs) {
102 struct folio *folio = readpage_iter_peek(iter);
106 readpage_iter_advance(iter);
108 pgoff_t folio_offset = bio_end_sector(bio) >> PAGE_SECTORS_SHIFT;
113 folio = xa_load(&iter->mapping->i_pages, folio_offset);
114 if (folio && !xa_is_value(folio))
117 folio = filemap_alloc_folio(readahead_gfp_mask(iter->mapping), 0);
121 if (!__bch2_folio_create(folio, GFP_KERNEL)) {
126 ret = filemap_add_folio(iter->mapping, folio, folio_offset, GFP_KERNEL);
128 __bch2_folio_release(folio);
136 BUG_ON(folio_sector(folio) != bio_end_sector(bio));
138 BUG_ON(!bio_add_folio(bio, folio, folio_size(folio), 0));
141 return bch2_trans_relock(trans);
144 static void bchfs_read(struct btree_trans *trans,
145 struct bch_read_bio *rbio,
147 struct readpages_iter *readpages_iter)
149 struct bch_fs *c = trans->c;
150 struct btree_iter iter;
152 int flags = BCH_READ_RETRY_IF_STALE|
153 BCH_READ_MAY_PROMOTE;
158 rbio->start_time = local_clock();
159 rbio->subvol = inum.subvol;
161 bch2_bkey_buf_init(&sk);
163 bch2_trans_begin(trans);
164 iter = (struct btree_iter) { NULL };
166 ret = bch2_subvolume_get_snapshot(trans, inum.subvol, &snapshot);
170 bch2_trans_iter_init(trans, &iter, BTREE_ID_extents,
171 SPOS(inum.inum, rbio->bio.bi_iter.bi_sector, snapshot),
175 unsigned bytes, sectors, offset_into_extent;
176 enum btree_id data_btree = BTREE_ID_extents;
179 * read_extent -> io_time_reset may cause a transaction restart
180 * without returning an error, we need to check for that here:
182 ret = bch2_trans_relock(trans);
186 bch2_btree_iter_set_pos(&iter,
187 POS(inum.inum, rbio->bio.bi_iter.bi_sector));
189 k = bch2_btree_iter_peek_slot(&iter);
194 offset_into_extent = iter.pos.offset -
195 bkey_start_offset(k.k);
196 sectors = k.k->size - offset_into_extent;
198 bch2_bkey_buf_reassemble(&sk, c, k);
200 ret = bch2_read_indirect_extent(trans, &data_btree,
201 &offset_into_extent, &sk);
205 k = bkey_i_to_s_c(sk.k);
207 sectors = min(sectors, k.k->size - offset_into_extent);
209 if (readpages_iter) {
210 ret = readpage_bio_extend(trans, readpages_iter, &rbio->bio, sectors,
211 extent_partial_reads_expensive(k));
216 bytes = min(sectors, bio_sectors(&rbio->bio)) << 9;
217 swap(rbio->bio.bi_iter.bi_size, bytes);
219 if (rbio->bio.bi_iter.bi_size == bytes)
220 flags |= BCH_READ_LAST_FRAGMENT;
222 bch2_bio_page_state_set(&rbio->bio, k);
224 bch2_read_extent(trans, rbio, iter.pos,
225 data_btree, k, offset_into_extent, flags);
227 if (flags & BCH_READ_LAST_FRAGMENT)
230 swap(rbio->bio.bi_iter.bi_size, bytes);
231 bio_advance(&rbio->bio, bytes);
233 ret = btree_trans_too_many_iters(trans);
238 bch2_trans_iter_exit(trans, &iter);
240 if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
244 bch_err_inum_offset_ratelimited(c,
246 iter.pos.offset << 9,
247 "read error %i from btree lookup", ret);
248 rbio->bio.bi_status = BLK_STS_IOERR;
249 bio_endio(&rbio->bio);
252 bch2_bkey_buf_exit(&sk, c);
255 void bch2_readahead(struct readahead_control *ractl)
257 struct bch_inode_info *inode = to_bch_ei(ractl->mapping->host);
258 struct bch_fs *c = inode->v.i_sb->s_fs_info;
259 struct bch_io_opts opts;
260 struct btree_trans *trans = bch2_trans_get(c);
262 struct readpages_iter readpages_iter;
264 bch2_inode_opts_get(&opts, c, &inode->ei_inode);
266 int ret = readpages_iter_init(&readpages_iter, ractl);
270 bch2_pagecache_add_get(inode);
272 while ((folio = readpage_iter_peek(&readpages_iter))) {
273 unsigned n = min_t(unsigned,
274 readpages_iter.folios.nr -
277 struct bch_read_bio *rbio =
278 rbio_init(bio_alloc_bioset(NULL, n, REQ_OP_READ,
279 GFP_KERNEL, &c->bio_read),
282 readpage_iter_advance(&readpages_iter);
284 rbio->bio.bi_iter.bi_sector = folio_sector(folio);
285 rbio->bio.bi_end_io = bch2_readpages_end_io;
286 BUG_ON(!bio_add_folio(&rbio->bio, folio, folio_size(folio), 0));
288 bchfs_read(trans, rbio, inode_inum(inode),
290 bch2_trans_unlock(trans);
293 bch2_pagecache_add_put(inode);
295 bch2_trans_put(trans);
296 darray_exit(&readpages_iter.folios);
299 static void bch2_read_single_folio_end_io(struct bio *bio)
301 complete(bio->bi_private);
304 int bch2_read_single_folio(struct folio *folio, struct address_space *mapping)
306 struct bch_inode_info *inode = to_bch_ei(mapping->host);
307 struct bch_fs *c = inode->v.i_sb->s_fs_info;
308 struct bch_read_bio *rbio;
309 struct bch_io_opts opts;
311 DECLARE_COMPLETION_ONSTACK(done);
313 if (!bch2_folio_create(folio, GFP_KERNEL))
316 bch2_inode_opts_get(&opts, c, &inode->ei_inode);
318 rbio = rbio_init(bio_alloc_bioset(NULL, 1, REQ_OP_READ, GFP_KERNEL, &c->bio_read),
320 rbio->bio.bi_private = &done;
321 rbio->bio.bi_end_io = bch2_read_single_folio_end_io;
323 rbio->bio.bi_opf = REQ_OP_READ|REQ_SYNC;
324 rbio->bio.bi_iter.bi_sector = folio_sector(folio);
325 BUG_ON(!bio_add_folio(&rbio->bio, folio, folio_size(folio), 0));
327 bch2_trans_run(c, (bchfs_read(trans, rbio, inode_inum(inode), NULL), 0));
328 wait_for_completion(&done);
330 ret = blk_status_to_errno(rbio->bio.bi_status);
336 folio_mark_uptodate(folio);
340 int bch2_read_folio(struct file *file, struct folio *folio)
344 ret = bch2_read_single_folio(folio, folio->mapping);
346 return bch2_err_class(ret);
351 struct bch_writepage_io {
352 struct bch_inode_info *inode;
355 struct bch_write_op op;
358 struct bch_writepage_state {
359 struct bch_writepage_io *io;
360 struct bch_io_opts opts;
361 struct bch_folio_sector *tmp;
362 unsigned tmp_sectors;
365 static inline struct bch_writepage_state bch_writepage_state_init(struct bch_fs *c,
366 struct bch_inode_info *inode)
368 struct bch_writepage_state ret = { 0 };
370 bch2_inode_opts_get(&ret.opts, c, &inode->ei_inode);
375 * Determine when a writepage io is full. We have to limit writepage bios to a
376 * single page per bvec (i.e. 1MB with 4k pages) because that is the limit to
377 * what the bounce path in bch2_write_extent() can handle. In theory we could
378 * loosen this restriction for non-bounce I/O, but we don't have that context
379 * here. Ideally, we can up this limit and make it configurable in the future
380 * when the bounce path can be enhanced to accommodate larger source bios.
382 static inline bool bch_io_full(struct bch_writepage_io *io, unsigned len)
384 struct bio *bio = &io->op.wbio.bio;
385 return bio_full(bio, len) ||
386 (bio->bi_iter.bi_size + len > BIO_MAX_VECS * PAGE_SIZE);
389 static void bch2_writepage_io_done(struct bch_write_op *op)
391 struct bch_writepage_io *io =
392 container_of(op, struct bch_writepage_io, op);
393 struct bch_fs *c = io->op.c;
394 struct bio *bio = &io->op.wbio.bio;
395 struct folio_iter fi;
399 set_bit(EI_INODE_ERROR, &io->inode->ei_flags);
401 bio_for_each_folio_all(fi, bio) {
404 mapping_set_error(fi.folio->mapping, -EIO);
406 s = __bch2_folio(fi.folio);
408 for (i = 0; i < folio_sectors(fi.folio); i++)
409 s->s[i].nr_replicas = 0;
410 spin_unlock(&s->lock);
414 if (io->op.flags & BCH_WRITE_WROTE_DATA_INLINE) {
415 bio_for_each_folio_all(fi, bio) {
418 s = __bch2_folio(fi.folio);
420 for (i = 0; i < folio_sectors(fi.folio); i++)
421 s->s[i].nr_replicas = 0;
422 spin_unlock(&s->lock);
427 * racing with fallocate can cause us to add fewer sectors than
428 * expected - but we shouldn't add more sectors than expected:
430 WARN_ON_ONCE(io->op.i_sectors_delta > 0);
433 * (error (due to going RO) halfway through a page can screw that up
436 BUG_ON(io->op.op.i_sectors_delta >= PAGE_SECTORS);
440 * PageWriteback is effectively our ref on the inode - fixup i_blocks
441 * before calling end_page_writeback:
443 bch2_i_sectors_acct(c, io->inode, NULL, io->op.i_sectors_delta);
445 bio_for_each_folio_all(fi, bio) {
446 struct bch_folio *s = __bch2_folio(fi.folio);
448 if (atomic_dec_and_test(&s->write_count))
449 folio_end_writeback(fi.folio);
452 bio_put(&io->op.wbio.bio);
455 static void bch2_writepage_do_io(struct bch_writepage_state *w)
457 struct bch_writepage_io *io = w->io;
460 closure_call(&io->op.cl, bch2_write, NULL, NULL);
464 * Get a bch_writepage_io and add @page to it - appending to an existing one if
465 * possible, else allocating a new one:
467 static void bch2_writepage_io_alloc(struct bch_fs *c,
468 struct writeback_control *wbc,
469 struct bch_writepage_state *w,
470 struct bch_inode_info *inode,
472 unsigned nr_replicas)
474 struct bch_write_op *op;
476 w->io = container_of(bio_alloc_bioset(NULL, BIO_MAX_VECS,
479 &c->writepage_bioset),
480 struct bch_writepage_io, op.wbio.bio);
482 w->io->inode = inode;
484 bch2_write_op_init(op, c, w->opts);
485 op->target = w->opts.foreground_target;
486 op->nr_replicas = nr_replicas;
487 op->res.nr_replicas = nr_replicas;
488 op->write_point = writepoint_hashed(inode->ei_last_dirtied);
489 op->subvol = inode->ei_subvol;
490 op->pos = POS(inode->v.i_ino, sector);
491 op->end_io = bch2_writepage_io_done;
492 op->devs_need_flush = &inode->ei_devs_need_flush;
493 op->wbio.bio.bi_iter.bi_sector = sector;
494 op->wbio.bio.bi_opf = wbc_to_write_flags(wbc);
497 static int __bch2_writepage(struct folio *folio,
498 struct writeback_control *wbc,
501 struct bch_inode_info *inode = to_bch_ei(folio->mapping->host);
502 struct bch_fs *c = inode->v.i_sb->s_fs_info;
503 struct bch_writepage_state *w = data;
505 unsigned i, offset, f_sectors, nr_replicas_this_write = U32_MAX;
506 loff_t i_size = i_size_read(&inode->v);
509 EBUG_ON(!folio_test_uptodate(folio));
511 /* Is the folio fully inside i_size? */
512 if (folio_end_pos(folio) <= i_size)
515 /* Is the folio fully outside i_size? (truncate in progress) */
516 if (folio_pos(folio) >= i_size) {
522 * The folio straddles i_size. It must be zeroed out on each and every
523 * writepage invocation because it may be mmapped. "A file is mapped
524 * in multiples of the folio size. For a file that is not a multiple of
525 * the folio size, the remaining memory is zeroed when mapped, and
526 * writes to that region are not written out to the file."
528 folio_zero_segment(folio,
529 i_size - folio_pos(folio),
532 f_sectors = folio_sectors(folio);
533 s = bch2_folio(folio);
535 if (f_sectors > w->tmp_sectors) {
537 w->tmp = kcalloc(f_sectors, sizeof(struct bch_folio_sector), __GFP_NOFAIL);
538 w->tmp_sectors = f_sectors;
542 * Things get really hairy with errors during writeback:
544 ret = bch2_get_folio_disk_reservation(c, inode, folio, false);
547 /* Before unlocking the page, get copy of reservations: */
549 memcpy(w->tmp, s->s, sizeof(struct bch_folio_sector) * f_sectors);
551 for (i = 0; i < f_sectors; i++) {
552 if (s->s[i].state < SECTOR_dirty)
555 nr_replicas_this_write =
556 min_t(unsigned, nr_replicas_this_write,
557 s->s[i].nr_replicas +
558 s->s[i].replicas_reserved);
561 for (i = 0; i < f_sectors; i++) {
562 if (s->s[i].state < SECTOR_dirty)
565 s->s[i].nr_replicas = w->opts.compression
566 ? 0 : nr_replicas_this_write;
568 s->s[i].replicas_reserved = 0;
569 bch2_folio_sector_set(folio, s, i, SECTOR_allocated);
571 spin_unlock(&s->lock);
573 BUG_ON(atomic_read(&s->write_count));
574 atomic_set(&s->write_count, 1);
576 BUG_ON(folio_test_writeback(folio));
577 folio_start_writeback(folio);
583 unsigned sectors = 0, dirty_sectors = 0, reserved_sectors = 0;
586 while (offset < f_sectors &&
587 w->tmp[offset].state < SECTOR_dirty)
590 if (offset == f_sectors)
593 while (offset + sectors < f_sectors &&
594 w->tmp[offset + sectors].state >= SECTOR_dirty) {
595 reserved_sectors += w->tmp[offset + sectors].replicas_reserved;
596 dirty_sectors += w->tmp[offset + sectors].state == SECTOR_dirty;
601 sector = folio_sector(folio) + offset;
604 (w->io->op.res.nr_replicas != nr_replicas_this_write ||
605 bch_io_full(w->io, sectors << 9) ||
606 bio_end_sector(&w->io->op.wbio.bio) != sector))
607 bch2_writepage_do_io(w);
610 bch2_writepage_io_alloc(c, wbc, w, inode, sector,
611 nr_replicas_this_write);
613 atomic_inc(&s->write_count);
615 BUG_ON(inode != w->io->inode);
616 BUG_ON(!bio_add_folio(&w->io->op.wbio.bio, folio,
617 sectors << 9, offset << 9));
619 /* Check for writing past i_size: */
620 WARN_ONCE((bio_end_sector(&w->io->op.wbio.bio) << 9) >
621 round_up(i_size, block_bytes(c)) &&
622 !test_bit(BCH_FS_emergency_ro, &c->flags),
623 "writing past i_size: %llu > %llu (unrounded %llu)\n",
624 bio_end_sector(&w->io->op.wbio.bio) << 9,
625 round_up(i_size, block_bytes(c)),
628 w->io->op.res.sectors += reserved_sectors;
629 w->io->op.i_sectors_delta -= dirty_sectors;
630 w->io->op.new_i_size = i_size;
635 if (atomic_dec_and_test(&s->write_count))
636 folio_end_writeback(folio);
641 int bch2_writepages(struct address_space *mapping, struct writeback_control *wbc)
643 struct bch_fs *c = mapping->host->i_sb->s_fs_info;
644 struct bch_writepage_state w =
645 bch_writepage_state_init(c, to_bch_ei(mapping->host));
646 struct blk_plug plug;
649 blk_start_plug(&plug);
650 ret = write_cache_pages(mapping, wbc, __bch2_writepage, &w);
652 bch2_writepage_do_io(&w);
653 blk_finish_plug(&plug);
655 return bch2_err_class(ret);
658 /* buffered writes: */
660 int bch2_write_begin(struct file *file, struct address_space *mapping,
661 loff_t pos, unsigned len,
662 struct page **pagep, void **fsdata)
664 struct bch_inode_info *inode = to_bch_ei(mapping->host);
665 struct bch_fs *c = inode->v.i_sb->s_fs_info;
666 struct bch2_folio_reservation *res;
671 res = kmalloc(sizeof(*res), GFP_KERNEL);
675 bch2_folio_reservation_init(c, inode, res);
678 bch2_pagecache_add_get(inode);
680 folio = __filemap_get_folio(mapping, pos >> PAGE_SHIFT,
681 FGP_LOCK|FGP_WRITE|FGP_CREAT|FGP_STABLE,
682 mapping_gfp_mask(mapping));
683 if (IS_ERR_OR_NULL(folio))
686 offset = pos - folio_pos(folio);
687 len = min_t(size_t, len, folio_end_pos(folio) - pos);
689 if (folio_test_uptodate(folio))
692 /* If we're writing entire folio, don't need to read it in first: */
693 if (!offset && len == folio_size(folio))
696 if (!offset && pos + len >= inode->v.i_size) {
697 folio_zero_segment(folio, len, folio_size(folio));
698 flush_dcache_folio(folio);
702 if (folio_pos(folio) >= inode->v.i_size) {
703 folio_zero_segments(folio, 0, offset, offset + len, folio_size(folio));
704 flush_dcache_folio(folio);
708 ret = bch2_read_single_folio(folio, mapping);
712 ret = bch2_folio_set(c, inode_inum(inode), &folio, 1);
716 ret = bch2_folio_reservation_get(c, inode, folio, res, offset, len);
718 if (!folio_test_uptodate(folio)) {
720 * If the folio hasn't been read in, we won't know if we
721 * actually need a reservation - we don't actually need
722 * to read here, we just need to check if the folio is
723 * fully backed by uncompressed data:
731 *pagep = &folio->page;
738 bch2_pagecache_add_put(inode);
741 return bch2_err_class(ret);
744 int bch2_write_end(struct file *file, struct address_space *mapping,
745 loff_t pos, unsigned len, unsigned copied,
746 struct page *page, void *fsdata)
748 struct bch_inode_info *inode = to_bch_ei(mapping->host);
749 struct bch_fs *c = inode->v.i_sb->s_fs_info;
750 struct bch2_folio_reservation *res = fsdata;
751 struct folio *folio = page_folio(page);
752 unsigned offset = pos - folio_pos(folio);
754 lockdep_assert_held(&inode->v.i_rwsem);
755 BUG_ON(offset + copied > folio_size(folio));
757 if (unlikely(copied < len && !folio_test_uptodate(folio))) {
759 * The folio needs to be read in, but that would destroy
760 * our partial write - simplest thing is to just force
761 * userspace to redo the write:
763 folio_zero_range(folio, 0, folio_size(folio));
764 flush_dcache_folio(folio);
768 spin_lock(&inode->v.i_lock);
769 if (pos + copied > inode->v.i_size)
770 i_size_write(&inode->v, pos + copied);
771 spin_unlock(&inode->v.i_lock);
774 if (!folio_test_uptodate(folio))
775 folio_mark_uptodate(folio);
777 bch2_set_folio_dirty(c, inode, folio, res, offset, copied);
779 inode->ei_last_dirtied = (unsigned long) current;
784 bch2_pagecache_add_put(inode);
786 bch2_folio_reservation_put(c, inode, res);
792 static noinline void folios_trunc(folios *fs, struct folio **fi)
794 while (fs->data + fs->nr > fi) {
795 struct folio *f = darray_pop(fs);
802 static int __bch2_buffered_write(struct bch_inode_info *inode,
803 struct address_space *mapping,
804 struct iov_iter *iter,
805 loff_t pos, unsigned len,
808 struct bch_fs *c = inode->v.i_sb->s_fs_info;
809 struct bch2_folio_reservation res;
812 unsigned copied = 0, f_offset, f_copied;
813 u64 end = pos + len, f_pos, f_len;
814 loff_t last_folio_pos = inode->v.i_size;
819 bch2_folio_reservation_init(c, inode, &res);
822 ret = bch2_filemap_get_contig_folios_d(mapping, pos, end,
823 FGP_LOCK|FGP_WRITE|FGP_STABLE|FGP_CREAT,
824 mapping_gfp_mask(mapping),
832 * If we're not using the inode lock, we need to lock all the folios for
833 * atomiticity of writes vs. other writes:
835 if (!inode_locked && folio_end_pos(darray_last(fs)) < end) {
836 ret = -BCH_ERR_need_inode_lock;
840 f = darray_first(fs);
841 if (pos != folio_pos(f) && !folio_test_uptodate(f)) {
842 ret = bch2_read_single_folio(f, mapping);
848 end = min(end, folio_end_pos(f));
849 last_folio_pos = folio_pos(f);
850 if (end != folio_end_pos(f) && !folio_test_uptodate(f)) {
851 if (end >= inode->v.i_size) {
852 folio_zero_range(f, 0, folio_size(f));
854 ret = bch2_read_single_folio(f, mapping);
860 ret = bch2_folio_set(c, inode_inum(inode), fs.data, fs.nr);
865 f_offset = pos - folio_pos(darray_first(fs));
866 darray_for_each(fs, fi) {
868 f_len = min(end, folio_end_pos(f)) - f_pos;
871 * XXX: per POSIX and fstests generic/275, on -ENOSPC we're
872 * supposed to write as much as we have disk space for.
874 * On failure here we should still write out a partial page if
875 * we aren't completely out of disk space - we don't do that
878 ret = bch2_folio_reservation_get(c, inode, f, &res, f_offset, f_len);
880 folios_trunc(&fs, fi);
884 end = min(end, folio_end_pos(darray_last(fs)));
888 f_pos = folio_end_pos(f);
892 if (mapping_writably_mapped(mapping))
893 darray_for_each(fs, fi)
894 flush_dcache_folio(*fi);
897 f_offset = pos - folio_pos(darray_first(fs));
898 darray_for_each(fs, fi) {
900 f_len = min(end, folio_end_pos(f)) - f_pos;
901 f_copied = copy_page_from_iter_atomic(&f->page, f_offset, f_len, iter);
903 folios_trunc(&fs, fi);
907 if (!folio_test_uptodate(f) &&
908 f_copied != folio_size(f) &&
909 pos + copied + f_copied < inode->v.i_size) {
910 iov_iter_revert(iter, f_copied);
911 folio_zero_range(f, 0, folio_size(f));
912 folios_trunc(&fs, fi);
916 flush_dcache_folio(f);
919 if (f_copied != f_len) {
920 folios_trunc(&fs, fi + 1);
924 f_pos = folio_end_pos(f);
933 spin_lock(&inode->v.i_lock);
934 if (end > inode->v.i_size) {
935 BUG_ON(!inode_locked);
936 i_size_write(&inode->v, end);
938 spin_unlock(&inode->v.i_lock);
941 f_offset = pos - folio_pos(darray_first(fs));
942 darray_for_each(fs, fi) {
944 f_len = min(end, folio_end_pos(f)) - f_pos;
946 if (!folio_test_uptodate(f))
947 folio_mark_uptodate(f);
949 bch2_set_folio_dirty(c, inode, f, &res, f_offset, f_len);
951 f_pos = folio_end_pos(f);
955 inode->ei_last_dirtied = (unsigned long) current;
957 darray_for_each(fs, fi) {
963 * If the last folio added to the mapping starts beyond current EOF, we
964 * performed a short write but left around at least one post-EOF folio.
965 * Clean up the mapping before we return.
967 if (last_folio_pos >= inode->v.i_size)
968 truncate_pagecache(&inode->v, inode->v.i_size);
971 bch2_folio_reservation_put(c, inode, &res);
973 return copied ?: ret;
976 static ssize_t bch2_buffered_write(struct kiocb *iocb, struct iov_iter *iter)
978 struct file *file = iocb->ki_filp;
979 struct address_space *mapping = file->f_mapping;
980 struct bch_inode_info *inode = file_bch_inode(file);
982 bool inode_locked = false;
983 ssize_t written = 0, written2 = 0, ret = 0;
986 * We don't take the inode lock unless i_size will be changing. Folio
987 * locks provide exclusion with other writes, and the pagecache add lock
988 * provides exclusion with truncate and hole punching.
990 * There is one nasty corner case where atomicity would be broken
991 * without great care: when copying data from userspace to the page
992 * cache, we do that with faults disable - a page fault would recurse
993 * back into the filesystem, taking filesystem locks again, and
994 * deadlock; so it's done with faults disabled, and we fault in the user
995 * buffer when we aren't holding locks.
997 * If we do part of the write, but we then race and in the userspace
998 * buffer have been evicted and are no longer resident, then we have to
999 * drop our folio locks to re-fault them in, breaking write atomicity.
1001 * To fix this, we restart the write from the start, if we weren't
1002 * holding the inode lock.
1004 * There is another wrinkle after that; if we restart the write from the
1005 * start, and then get an unrecoverable error, we _cannot_ claim to
1006 * userspace that we did not write data we actually did - so we must
1007 * track (written2) the most we ever wrote.
1010 if ((iocb->ki_flags & IOCB_APPEND) ||
1011 (iocb->ki_pos + iov_iter_count(iter) > i_size_read(&inode->v))) {
1012 inode_lock(&inode->v);
1013 inode_locked = true;
1016 ret = generic_write_checks(iocb, iter);
1020 ret = file_remove_privs_flags(file, !inode_locked ? IOCB_NOWAIT : 0);
1022 if (!inode_locked) {
1023 inode_lock(&inode->v);
1024 inode_locked = true;
1025 ret = file_remove_privs_flags(file, 0);
1031 ret = file_update_time(file);
1037 bch2_pagecache_add_get(inode);
1039 if (!inode_locked &&
1040 (iocb->ki_pos + iov_iter_count(iter) > i_size_read(&inode->v)))
1041 goto get_inode_lock;
1044 unsigned offset = pos & (PAGE_SIZE - 1);
1045 unsigned bytes = iov_iter_count(iter);
1048 * Bring in the user page that we will copy from _first_.
1049 * Otherwise there's a nasty deadlock on copying from the
1050 * same page as we're writing to, without it being marked
1053 * Not only is this an optimisation, but it is also required
1054 * to check that the address is actually valid, when atomic
1055 * usercopies are used, below.
1057 if (unlikely(fault_in_iov_iter_readable(iter, bytes))) {
1058 bytes = min_t(unsigned long, iov_iter_count(iter),
1059 PAGE_SIZE - offset);
1061 if (unlikely(fault_in_iov_iter_readable(iter, bytes))) {
1067 if (unlikely(bytes != iov_iter_count(iter) && !inode_locked))
1068 goto get_inode_lock;
1070 if (unlikely(fatal_signal_pending(current))) {
1075 ret = __bch2_buffered_write(inode, mapping, iter, pos, bytes, inode_locked);
1076 if (ret == -BCH_ERR_need_inode_lock)
1077 goto get_inode_lock;
1078 if (unlikely(ret < 0))
1083 if (unlikely(ret == 0)) {
1085 * If we were unable to copy any data at all, we must
1086 * fall back to a single segment length write.
1088 * If we didn't fallback here, we could livelock
1089 * because not all segments in the iov can be copied at
1090 * once without a pagefault.
1092 bytes = min_t(unsigned long, PAGE_SIZE - offset,
1093 iov_iter_single_seg_count(iter));
1098 written2 = max(written, written2);
1100 if (ret != bytes && !inode_locked)
1101 goto get_inode_lock;
1104 balance_dirty_pages_ratelimited(mapping);
1108 bch2_pagecache_add_put(inode);
1109 inode_lock(&inode->v);
1110 inode_locked = true;
1111 bch2_pagecache_add_get(inode);
1113 iov_iter_revert(iter, written);
1118 } while (iov_iter_count(iter));
1119 bch2_pagecache_add_put(inode);
1122 inode_unlock(&inode->v);
1124 iocb->ki_pos += written;
1126 ret = max(written, written2) ?: ret;
1128 ret = generic_write_sync(iocb, ret);
1132 ssize_t bch2_write_iter(struct kiocb *iocb, struct iov_iter *iter)
1134 ssize_t ret = iocb->ki_flags & IOCB_DIRECT
1135 ? bch2_direct_write(iocb, iter)
1136 : bch2_buffered_write(iocb, iter);
1138 return bch2_err_class(ret);
1141 void bch2_fs_fs_io_buffered_exit(struct bch_fs *c)
1143 bioset_exit(&c->writepage_bioset);
1146 int bch2_fs_fs_io_buffered_init(struct bch_fs *c)
1148 if (bioset_init(&c->writepage_bioset,
1149 4, offsetof(struct bch_writepage_io, op.wbio.bio),
1151 return -BCH_ERR_ENOMEM_writepage_bioset_init;
1156 #endif /* NO_BCACHEFS_FS */