xref: /linux/fs/bcachefs/fs-io.c (revision 65d48e35250fe46a560dffa13876830336b152c9)
11c6fdbd8SKent Overstreet // SPDX-License-Identifier: GPL-2.0
21c6fdbd8SKent Overstreet #ifndef NO_BCACHEFS_FS
31c6fdbd8SKent Overstreet 
41c6fdbd8SKent Overstreet #include "bcachefs.h"
57b3f84eaSKent Overstreet #include "alloc_foreground.h"
607a1006aSKent Overstreet #include "bkey_buf.h"
71c6fdbd8SKent Overstreet #include "btree_update.h"
81c6fdbd8SKent Overstreet #include "buckets.h"
91c6fdbd8SKent Overstreet #include "clock.h"
101c6fdbd8SKent Overstreet #include "error.h"
11e2d9912cSKent Overstreet #include "extents.h"
1208c07feaSKent Overstreet #include "extent_update.h"
131c6fdbd8SKent Overstreet #include "fs.h"
141c6fdbd8SKent Overstreet #include "fs-io.h"
151c6fdbd8SKent Overstreet #include "fsck.h"
161c6fdbd8SKent Overstreet #include "inode.h"
171c6fdbd8SKent Overstreet #include "journal.h"
181c6fdbd8SKent Overstreet #include "io.h"
191c6fdbd8SKent Overstreet #include "keylist.h"
201c6fdbd8SKent Overstreet #include "quota.h"
2176426098SKent Overstreet #include "reflink.h"
221c6fdbd8SKent Overstreet #include "trace.h"
231c6fdbd8SKent Overstreet 
241c6fdbd8SKent Overstreet #include <linux/aio.h>
251c6fdbd8SKent Overstreet #include <linux/backing-dev.h>
261c6fdbd8SKent Overstreet #include <linux/falloc.h>
271c6fdbd8SKent Overstreet #include <linux/migrate.h>
281c6fdbd8SKent Overstreet #include <linux/mmu_context.h>
291c6fdbd8SKent Overstreet #include <linux/pagevec.h>
309ba2eb25SKent Overstreet #include <linux/rmap.h>
311c6fdbd8SKent Overstreet #include <linux/sched/signal.h>
321c6fdbd8SKent Overstreet #include <linux/task_io_accounting_ops.h>
331c6fdbd8SKent Overstreet #include <linux/uio.h>
341c6fdbd8SKent Overstreet #include <linux/writeback.h>
351c6fdbd8SKent Overstreet 
361c6fdbd8SKent Overstreet #include <trace/events/writeback.h>
371c6fdbd8SKent Overstreet 
38a8b3a677SKent Overstreet struct nocow_flush {
39a8b3a677SKent Overstreet 	struct closure	*cl;
40a8b3a677SKent Overstreet 	struct bch_dev	*ca;
41a8b3a677SKent Overstreet 	struct bio	bio;
42a8b3a677SKent Overstreet };
43a8b3a677SKent Overstreet 
44a8b3a677SKent Overstreet static void nocow_flush_endio(struct bio *_bio)
45a8b3a677SKent Overstreet {
46a8b3a677SKent Overstreet 
47a8b3a677SKent Overstreet 	struct nocow_flush *bio = container_of(_bio, struct nocow_flush, bio);
48a8b3a677SKent Overstreet 
49a8b3a677SKent Overstreet 	closure_put(bio->cl);
50a8b3a677SKent Overstreet 	percpu_ref_put(&bio->ca->io_ref);
51a8b3a677SKent Overstreet 	bio_put(&bio->bio);
52a8b3a677SKent Overstreet }
53a8b3a677SKent Overstreet 
54a8b3a677SKent Overstreet static void bch2_inode_flush_nocow_writes_async(struct bch_fs *c,
55a8b3a677SKent Overstreet 						struct bch_inode_info *inode,
56a8b3a677SKent Overstreet 						struct closure *cl)
57a8b3a677SKent Overstreet {
58a8b3a677SKent Overstreet 	struct nocow_flush *bio;
59a8b3a677SKent Overstreet 	struct bch_dev *ca;
60a8b3a677SKent Overstreet 	struct bch_devs_mask devs;
61a8b3a677SKent Overstreet 	unsigned dev;
62a8b3a677SKent Overstreet 
63a8b3a677SKent Overstreet 	dev = find_first_bit(inode->ei_devs_need_flush.d, BCH_SB_MEMBERS_MAX);
64a8b3a677SKent Overstreet 	if (dev == BCH_SB_MEMBERS_MAX)
65a8b3a677SKent Overstreet 		return;
66a8b3a677SKent Overstreet 
67a8b3a677SKent Overstreet 	devs = inode->ei_devs_need_flush;
68a8b3a677SKent Overstreet 	memset(&inode->ei_devs_need_flush, 0, sizeof(inode->ei_devs_need_flush));
69a8b3a677SKent Overstreet 
70a8b3a677SKent Overstreet 	for_each_set_bit(dev, devs.d, BCH_SB_MEMBERS_MAX) {
71a8b3a677SKent Overstreet 		rcu_read_lock();
72a8b3a677SKent Overstreet 		ca = rcu_dereference(c->devs[dev]);
73a8b3a677SKent Overstreet 		if (ca && !percpu_ref_tryget(&ca->io_ref))
74a8b3a677SKent Overstreet 			ca = NULL;
75a8b3a677SKent Overstreet 		rcu_read_unlock();
76a8b3a677SKent Overstreet 
77a8b3a677SKent Overstreet 		if (!ca)
78a8b3a677SKent Overstreet 			continue;
79a8b3a677SKent Overstreet 
80a8b3a677SKent Overstreet 		bio = container_of(bio_alloc_bioset(ca->disk_sb.bdev, 0,
81a8b3a677SKent Overstreet 						    REQ_OP_FLUSH,
82a8b3a677SKent Overstreet 						    GFP_KERNEL,
83a8b3a677SKent Overstreet 						    &c->nocow_flush_bioset),
84a8b3a677SKent Overstreet 				   struct nocow_flush, bio);
85a8b3a677SKent Overstreet 		bio->cl			= cl;
86a8b3a677SKent Overstreet 		bio->ca			= ca;
87a8b3a677SKent Overstreet 		bio->bio.bi_end_io	= nocow_flush_endio;
88a8b3a677SKent Overstreet 		closure_bio_submit(&bio->bio, cl);
89a8b3a677SKent Overstreet 	}
90a8b3a677SKent Overstreet }
91a8b3a677SKent Overstreet 
92a8b3a677SKent Overstreet static int bch2_inode_flush_nocow_writes(struct bch_fs *c,
93a8b3a677SKent Overstreet 					 struct bch_inode_info *inode)
94a8b3a677SKent Overstreet {
95a8b3a677SKent Overstreet 	struct closure cl;
96a8b3a677SKent Overstreet 
97a8b3a677SKent Overstreet 	closure_init_stack(&cl);
98a8b3a677SKent Overstreet 	bch2_inode_flush_nocow_writes_async(c, inode, &cl);
99a8b3a677SKent Overstreet 	closure_sync(&cl);
100a8b3a677SKent Overstreet 
101a8b3a677SKent Overstreet 	return 0;
102a8b3a677SKent Overstreet }
103a8b3a677SKent Overstreet 
1047f5e31e1SKent Overstreet static inline bool bio_full(struct bio *bio, unsigned len)
1057f5e31e1SKent Overstreet {
1067f5e31e1SKent Overstreet 	if (bio->bi_vcnt >= bio->bi_max_vecs)
1077f5e31e1SKent Overstreet 		return true;
1087f5e31e1SKent Overstreet 	if (bio->bi_iter.bi_size > UINT_MAX - len)
1097f5e31e1SKent Overstreet 		return true;
1107f5e31e1SKent Overstreet 	return false;
1117f5e31e1SKent Overstreet }
1127f5e31e1SKent Overstreet 
113eb8e6e9cSKent Overstreet static inline struct address_space *faults_disabled_mapping(void)
114eb8e6e9cSKent Overstreet {
115eb8e6e9cSKent Overstreet 	return (void *) (((unsigned long) current->faults_disabled_mapping) & ~1UL);
116eb8e6e9cSKent Overstreet }
117eb8e6e9cSKent Overstreet 
118eb8e6e9cSKent Overstreet static inline void set_fdm_dropped_locks(void)
119eb8e6e9cSKent Overstreet {
120eb8e6e9cSKent Overstreet 	current->faults_disabled_mapping =
121eb8e6e9cSKent Overstreet 		(void *) (((unsigned long) current->faults_disabled_mapping)|1);
122eb8e6e9cSKent Overstreet }
123eb8e6e9cSKent Overstreet 
124eb8e6e9cSKent Overstreet static inline bool fdm_dropped_locks(void)
125eb8e6e9cSKent Overstreet {
126eb8e6e9cSKent Overstreet 	return ((unsigned long) current->faults_disabled_mapping) & 1;
127eb8e6e9cSKent Overstreet }
128eb8e6e9cSKent Overstreet 
1291c6fdbd8SKent Overstreet struct quota_res {
1301c6fdbd8SKent Overstreet 	u64				sectors;
1311c6fdbd8SKent Overstreet };
1321c6fdbd8SKent Overstreet 
1339a3df993SKent Overstreet struct bch_writepage_io {
1341c6fdbd8SKent Overstreet 	struct bch_inode_info		*inode;
1351c6fdbd8SKent Overstreet 
1361c6fdbd8SKent Overstreet 	/* must be last: */
1371c6fdbd8SKent Overstreet 	struct bch_write_op		op;
1381c6fdbd8SKent Overstreet };
1391c6fdbd8SKent Overstreet 
1401c6fdbd8SKent Overstreet struct dio_write {
1411c6fdbd8SKent Overstreet 	struct kiocb			*req;
142182c7bbfSKent Overstreet 	struct address_space		*mapping;
143182c7bbfSKent Overstreet 	struct bch_inode_info		*inode;
144ed484030SKent Overstreet 	struct mm_struct		*mm;
1451c6fdbd8SKent Overstreet 	unsigned			loop:1,
1466b1b186aSKent Overstreet 					extending:1,
1471c6fdbd8SKent Overstreet 					sync:1,
148a1ee777bSKent Overstreet 					flush:1,
1491c6fdbd8SKent Overstreet 					free_iov:1;
1501c6fdbd8SKent Overstreet 	struct quota_res		quota_res;
151042a1f26SKent Overstreet 	u64				written;
1521c6fdbd8SKent Overstreet 
1531c6fdbd8SKent Overstreet 	struct iov_iter			iter;
1541c6fdbd8SKent Overstreet 	struct iovec			inline_vecs[2];
1551c6fdbd8SKent Overstreet 
1561c6fdbd8SKent Overstreet 	/* must be last: */
1579a3df993SKent Overstreet 	struct bch_write_op		op;
1581c6fdbd8SKent Overstreet };
1591c6fdbd8SKent Overstreet 
1601c6fdbd8SKent Overstreet struct dio_read {
1611c6fdbd8SKent Overstreet 	struct closure			cl;
1621c6fdbd8SKent Overstreet 	struct kiocb			*req;
1631c6fdbd8SKent Overstreet 	long				ret;
164b4725cc1SKent Overstreet 	bool				should_dirty;
1651c6fdbd8SKent Overstreet 	struct bch_read_bio		rbio;
1661c6fdbd8SKent Overstreet };
1671c6fdbd8SKent Overstreet 
1681c6fdbd8SKent Overstreet /* pagecache_block must be held */
169a023127aSKent Overstreet static noinline int write_invalidate_inode_pages_range(struct address_space *mapping,
1701c6fdbd8SKent Overstreet 					      loff_t start, loff_t end)
1711c6fdbd8SKent Overstreet {
1721c6fdbd8SKent Overstreet 	int ret;
1731c6fdbd8SKent Overstreet 
1741c6fdbd8SKent Overstreet 	/*
1751c6fdbd8SKent Overstreet 	 * XXX: the way this is currently implemented, we can spin if a process
1761c6fdbd8SKent Overstreet 	 * is continually redirtying a specific page
1771c6fdbd8SKent Overstreet 	 */
1781c6fdbd8SKent Overstreet 	do {
1791c6fdbd8SKent Overstreet 		if (!mapping->nrpages)
1801c6fdbd8SKent Overstreet 			return 0;
1811c6fdbd8SKent Overstreet 
1821c6fdbd8SKent Overstreet 		ret = filemap_write_and_wait_range(mapping, start, end);
1831c6fdbd8SKent Overstreet 		if (ret)
1841c6fdbd8SKent Overstreet 			break;
1851c6fdbd8SKent Overstreet 
1861c6fdbd8SKent Overstreet 		if (!mapping->nrpages)
1871c6fdbd8SKent Overstreet 			return 0;
1881c6fdbd8SKent Overstreet 
1891c6fdbd8SKent Overstreet 		ret = invalidate_inode_pages2_range(mapping,
1901c6fdbd8SKent Overstreet 				start >> PAGE_SHIFT,
1911c6fdbd8SKent Overstreet 				end >> PAGE_SHIFT);
1921c6fdbd8SKent Overstreet 	} while (ret == -EBUSY);
1931c6fdbd8SKent Overstreet 
1941c6fdbd8SKent Overstreet 	return ret;
1951c6fdbd8SKent Overstreet }
1961c6fdbd8SKent Overstreet 
1971c6fdbd8SKent Overstreet /* quotas */
1981c6fdbd8SKent Overstreet 
1991c6fdbd8SKent Overstreet #ifdef CONFIG_BCACHEFS_QUOTA
2001c6fdbd8SKent Overstreet 
2016b1b186aSKent Overstreet static void __bch2_quota_reservation_put(struct bch_fs *c,
2021c6fdbd8SKent Overstreet 					 struct bch_inode_info *inode,
2031c6fdbd8SKent Overstreet 					 struct quota_res *res)
2041c6fdbd8SKent Overstreet {
2051c6fdbd8SKent Overstreet 	BUG_ON(res->sectors > inode->ei_quota_reserved);
2061c6fdbd8SKent Overstreet 
2071c6fdbd8SKent Overstreet 	bch2_quota_acct(c, inode->ei_qid, Q_SPC,
20826609b61SKent Overstreet 			-((s64) res->sectors), KEY_TYPE_QUOTA_PREALLOC);
2091c6fdbd8SKent Overstreet 	inode->ei_quota_reserved -= res->sectors;
2101c6fdbd8SKent Overstreet 	res->sectors = 0;
2111c6fdbd8SKent Overstreet }
2121c6fdbd8SKent Overstreet 
2136b1b186aSKent Overstreet static void bch2_quota_reservation_put(struct bch_fs *c,
2146b1b186aSKent Overstreet 				       struct bch_inode_info *inode,
2156b1b186aSKent Overstreet 				       struct quota_res *res)
2166b1b186aSKent Overstreet {
2176b1b186aSKent Overstreet 	if (res->sectors) {
2186b1b186aSKent Overstreet 		mutex_lock(&inode->ei_quota_lock);
2196b1b186aSKent Overstreet 		__bch2_quota_reservation_put(c, inode, res);
2206b1b186aSKent Overstreet 		mutex_unlock(&inode->ei_quota_lock);
2216b1b186aSKent Overstreet 	}
2226b1b186aSKent Overstreet }
2236b1b186aSKent Overstreet 
2241c6fdbd8SKent Overstreet static int bch2_quota_reservation_add(struct bch_fs *c,
2251c6fdbd8SKent Overstreet 				      struct bch_inode_info *inode,
2261c6fdbd8SKent Overstreet 				      struct quota_res *res,
227e8540e56SKent Overstreet 				      u64 sectors,
2281c6fdbd8SKent Overstreet 				      bool check_enospc)
2291c6fdbd8SKent Overstreet {
2301c6fdbd8SKent Overstreet 	int ret;
2311c6fdbd8SKent Overstreet 
2321c6fdbd8SKent Overstreet 	mutex_lock(&inode->ei_quota_lock);
2331c6fdbd8SKent Overstreet 	ret = bch2_quota_acct(c, inode->ei_qid, Q_SPC, sectors,
23426609b61SKent Overstreet 			      check_enospc ? KEY_TYPE_QUOTA_PREALLOC : KEY_TYPE_QUOTA_NOCHECK);
2351c6fdbd8SKent Overstreet 	if (likely(!ret)) {
2361c6fdbd8SKent Overstreet 		inode->ei_quota_reserved += sectors;
2371c6fdbd8SKent Overstreet 		res->sectors += sectors;
2381c6fdbd8SKent Overstreet 	}
2391c6fdbd8SKent Overstreet 	mutex_unlock(&inode->ei_quota_lock);
2401c6fdbd8SKent Overstreet 
2411c6fdbd8SKent Overstreet 	return ret;
2421c6fdbd8SKent Overstreet }
2431c6fdbd8SKent Overstreet 
2441c6fdbd8SKent Overstreet #else
2451c6fdbd8SKent Overstreet 
2466b1b186aSKent Overstreet static void __bch2_quota_reservation_put(struct bch_fs *c,
2476b1b186aSKent Overstreet 					 struct bch_inode_info *inode,
2486b1b186aSKent Overstreet 					 struct quota_res *res) {}
2496b1b186aSKent Overstreet 
2501c6fdbd8SKent Overstreet static void bch2_quota_reservation_put(struct bch_fs *c,
2511c6fdbd8SKent Overstreet 				       struct bch_inode_info *inode,
2526b1b186aSKent Overstreet 				       struct quota_res *res) {}
2531c6fdbd8SKent Overstreet 
2541c6fdbd8SKent Overstreet static int bch2_quota_reservation_add(struct bch_fs *c,
2551c6fdbd8SKent Overstreet 				      struct bch_inode_info *inode,
2561c6fdbd8SKent Overstreet 				      struct quota_res *res,
2571c6fdbd8SKent Overstreet 				      unsigned sectors,
2581c6fdbd8SKent Overstreet 				      bool check_enospc)
2591c6fdbd8SKent Overstreet {
2601c6fdbd8SKent Overstreet 	return 0;
2611c6fdbd8SKent Overstreet }
2621c6fdbd8SKent Overstreet 
2631c6fdbd8SKent Overstreet #endif
2641c6fdbd8SKent Overstreet 
2651c6fdbd8SKent Overstreet /* i_size updates: */
2661c6fdbd8SKent Overstreet 
2672ea90048SKent Overstreet struct inode_new_size {
2682ea90048SKent Overstreet 	loff_t		new_size;
2692ea90048SKent Overstreet 	u64		now;
2702ea90048SKent Overstreet 	unsigned	fields;
2712ea90048SKent Overstreet };
2722ea90048SKent Overstreet 
2731c6fdbd8SKent Overstreet static int inode_set_size(struct bch_inode_info *inode,
2741c6fdbd8SKent Overstreet 			  struct bch_inode_unpacked *bi,
2751c6fdbd8SKent Overstreet 			  void *p)
2761c6fdbd8SKent Overstreet {
2772ea90048SKent Overstreet 	struct inode_new_size *s = p;
2781c6fdbd8SKent Overstreet 
2792ea90048SKent Overstreet 	bi->bi_size = s->new_size;
2802ea90048SKent Overstreet 	if (s->fields & ATTR_ATIME)
2812ea90048SKent Overstreet 		bi->bi_atime = s->now;
2822ea90048SKent Overstreet 	if (s->fields & ATTR_MTIME)
2832ea90048SKent Overstreet 		bi->bi_mtime = s->now;
2842ea90048SKent Overstreet 	if (s->fields & ATTR_CTIME)
2852ea90048SKent Overstreet 		bi->bi_ctime = s->now;
2861c6fdbd8SKent Overstreet 
2871c6fdbd8SKent Overstreet 	return 0;
2881c6fdbd8SKent Overstreet }
2891c6fdbd8SKent Overstreet 
29076426098SKent Overstreet int __must_check bch2_write_inode_size(struct bch_fs *c,
2911c6fdbd8SKent Overstreet 				       struct bch_inode_info *inode,
2922ea90048SKent Overstreet 				       loff_t new_size, unsigned fields)
2931c6fdbd8SKent Overstreet {
2942ea90048SKent Overstreet 	struct inode_new_size s = {
2952ea90048SKent Overstreet 		.new_size	= new_size,
2962ea90048SKent Overstreet 		.now		= bch2_current_time(c),
2972ea90048SKent Overstreet 		.fields		= fields,
2982ea90048SKent Overstreet 	};
2992ea90048SKent Overstreet 
3002ea90048SKent Overstreet 	return bch2_write_inode(c, inode, inode_set_size, &s, fields);
3011c6fdbd8SKent Overstreet }
3021c6fdbd8SKent Overstreet 
3036b1b186aSKent Overstreet static void __i_sectors_acct(struct bch_fs *c, struct bch_inode_info *inode,
304190fa7afSKent Overstreet 			   struct quota_res *quota_res, s64 sectors)
3051c6fdbd8SKent Overstreet {
306b33bf1bcSKent Overstreet 	bch2_fs_inconsistent_on((s64) inode->v.i_blocks + sectors < 0, c,
307b33bf1bcSKent Overstreet 				"inode %lu i_blocks underflow: %llu + %lli < 0 (ondisk %lli)",
308b33bf1bcSKent Overstreet 				inode->v.i_ino, (u64) inode->v.i_blocks, sectors,
309b33bf1bcSKent Overstreet 				inode->ei_inode.bi_sectors);
310b44a66a6SKent Overstreet 	inode->v.i_blocks += sectors;
311b44a66a6SKent Overstreet 
3121c6fdbd8SKent Overstreet #ifdef CONFIG_BCACHEFS_QUOTA
3131c6fdbd8SKent Overstreet 	if (quota_res && sectors > 0) {
3141c6fdbd8SKent Overstreet 		BUG_ON(sectors > quota_res->sectors);
3151c6fdbd8SKent Overstreet 		BUG_ON(sectors > inode->ei_quota_reserved);
3161c6fdbd8SKent Overstreet 
3171c6fdbd8SKent Overstreet 		quota_res->sectors -= sectors;
3181c6fdbd8SKent Overstreet 		inode->ei_quota_reserved -= sectors;
3191c6fdbd8SKent Overstreet 	} else {
32026609b61SKent Overstreet 		bch2_quota_acct(c, inode->ei_qid, Q_SPC, sectors, KEY_TYPE_QUOTA_WARN);
3211c6fdbd8SKent Overstreet 	}
3221c6fdbd8SKent Overstreet #endif
3236b1b186aSKent Overstreet }
3246b1b186aSKent Overstreet 
3256b1b186aSKent Overstreet static void i_sectors_acct(struct bch_fs *c, struct bch_inode_info *inode,
3266b1b186aSKent Overstreet 			   struct quota_res *quota_res, s64 sectors)
3276b1b186aSKent Overstreet {
3286b1b186aSKent Overstreet 	if (sectors) {
3296b1b186aSKent Overstreet 		mutex_lock(&inode->ei_quota_lock);
3306b1b186aSKent Overstreet 		__i_sectors_acct(c, inode, quota_res, sectors);
3311c6fdbd8SKent Overstreet 		mutex_unlock(&inode->ei_quota_lock);
3321c6fdbd8SKent Overstreet 	}
3336b1b186aSKent Overstreet }
3341c6fdbd8SKent Overstreet 
3351c6fdbd8SKent Overstreet /* page state: */
3361c6fdbd8SKent Overstreet 
3371c6fdbd8SKent Overstreet /* stored in page->private: */
3381c6fdbd8SKent Overstreet 
339f57a6a5dSKent Overstreet struct bch_page_sector {
340b44a66a6SKent Overstreet 	/* Uncompressed, fully allocated replicas (or on disk reservation): */
341b44a66a6SKent Overstreet 	unsigned		nr_replicas:4;
342f81b648dSKent Overstreet 
343b44a66a6SKent Overstreet 	/* Owns PAGE_SECTORS * replicas_reserved sized in memory reservation: */
344b44a66a6SKent Overstreet 	unsigned		replicas_reserved:4;
3451c6fdbd8SKent Overstreet 
346f57a6a5dSKent Overstreet 	/* i_sectors: */
347f57a6a5dSKent Overstreet 	enum {
348f57a6a5dSKent Overstreet 		SECTOR_UNALLOCATED,
3492ba5d38bSKent Overstreet 		SECTOR_RESERVED,
350f57a6a5dSKent Overstreet 		SECTOR_DIRTY,
351b44a66a6SKent Overstreet 		SECTOR_DIRTY_RESERVED,
352f57a6a5dSKent Overstreet 		SECTOR_ALLOCATED,
353b44a66a6SKent Overstreet 	}			state:8;
3541c6fdbd8SKent Overstreet };
3551c6fdbd8SKent Overstreet 
356f57a6a5dSKent Overstreet struct bch_page_state {
3573826ee0bSKent Overstreet 	spinlock_t		lock;
3587f5e31e1SKent Overstreet 	atomic_t		write_count;
359e6ec361fSKent Overstreet 	bool			uptodate;
360f57a6a5dSKent Overstreet 	struct bch_page_sector	s[PAGE_SECTORS];
361f57a6a5dSKent Overstreet };
362f57a6a5dSKent Overstreet 
363f57a6a5dSKent Overstreet static inline struct bch_page_state *__bch2_page_state(struct page *page)
3641c6fdbd8SKent Overstreet {
365f57a6a5dSKent Overstreet 	return page_has_private(page)
366f57a6a5dSKent Overstreet 		? (struct bch_page_state *) page_private(page)
367f57a6a5dSKent Overstreet 		: NULL;
368f57a6a5dSKent Overstreet }
3691c6fdbd8SKent Overstreet 
370f57a6a5dSKent Overstreet static inline struct bch_page_state *bch2_page_state(struct page *page)
371f57a6a5dSKent Overstreet {
372e1036a2aSKent Overstreet 	EBUG_ON(!PageLocked(page));
3731c6fdbd8SKent Overstreet 
374f57a6a5dSKent Overstreet 	return __bch2_page_state(page);
375f57a6a5dSKent Overstreet }
376f57a6a5dSKent Overstreet 
377f57a6a5dSKent Overstreet /* for newly allocated pages: */
378f57a6a5dSKent Overstreet static void __bch2_page_state_release(struct page *page)
379f57a6a5dSKent Overstreet {
38000276f9fSMatthew Wilcox (Oracle) 	kfree(detach_page_private(page));
381f57a6a5dSKent Overstreet }
382f57a6a5dSKent Overstreet 
383f57a6a5dSKent Overstreet static void bch2_page_state_release(struct page *page)
384f57a6a5dSKent Overstreet {
38500276f9fSMatthew Wilcox (Oracle) 	EBUG_ON(!PageLocked(page));
38600276f9fSMatthew Wilcox (Oracle) 	__bch2_page_state_release(page);
387f57a6a5dSKent Overstreet }
388f57a6a5dSKent Overstreet 
389f57a6a5dSKent Overstreet /* for newly allocated pages: */
390f57a6a5dSKent Overstreet static struct bch_page_state *__bch2_page_state_create(struct page *page,
391f57a6a5dSKent Overstreet 						       gfp_t gfp)
392f57a6a5dSKent Overstreet {
393f57a6a5dSKent Overstreet 	struct bch_page_state *s;
394f57a6a5dSKent Overstreet 
395f57a6a5dSKent Overstreet 	s = kzalloc(sizeof(*s), GFP_NOFS|gfp);
396f57a6a5dSKent Overstreet 	if (!s)
397f57a6a5dSKent Overstreet 		return NULL;
398f57a6a5dSKent Overstreet 
3993826ee0bSKent Overstreet 	spin_lock_init(&s->lock);
40000276f9fSMatthew Wilcox (Oracle) 	attach_page_private(page, s);
4011c6fdbd8SKent Overstreet 	return s;
4021c6fdbd8SKent Overstreet }
4031c6fdbd8SKent Overstreet 
404f57a6a5dSKent Overstreet static struct bch_page_state *bch2_page_state_create(struct page *page,
405f57a6a5dSKent Overstreet 						     gfp_t gfp)
406f57a6a5dSKent Overstreet {
407f57a6a5dSKent Overstreet 	return bch2_page_state(page) ?: __bch2_page_state_create(page, gfp);
408f57a6a5dSKent Overstreet }
409f57a6a5dSKent Overstreet 
41079203111SKent Overstreet static unsigned bkey_to_sector_state(struct bkey_s_c k)
411b44a66a6SKent Overstreet {
41279203111SKent Overstreet 	if (bkey_extent_is_reservation(k))
413b44a66a6SKent Overstreet 		return SECTOR_RESERVED;
41479203111SKent Overstreet 	if (bkey_extent_is_allocation(k.k))
415b44a66a6SKent Overstreet 		return SECTOR_ALLOCATED;
416b44a66a6SKent Overstreet 	return SECTOR_UNALLOCATED;
417b44a66a6SKent Overstreet }
418b44a66a6SKent Overstreet 
419e6ec361fSKent Overstreet static void __bch2_page_state_set(struct page *page,
420e6ec361fSKent Overstreet 				  unsigned pg_offset, unsigned pg_len,
421e6ec361fSKent Overstreet 				  unsigned nr_ptrs, unsigned state)
422e6ec361fSKent Overstreet {
423e6ec361fSKent Overstreet 	struct bch_page_state *s = bch2_page_state_create(page, __GFP_NOFAIL);
424e6ec361fSKent Overstreet 	unsigned i;
425e6ec361fSKent Overstreet 
426e6ec361fSKent Overstreet 	BUG_ON(pg_offset >= PAGE_SECTORS);
427e6ec361fSKent Overstreet 	BUG_ON(pg_offset + pg_len > PAGE_SECTORS);
428e6ec361fSKent Overstreet 
429e6ec361fSKent Overstreet 	spin_lock(&s->lock);
430e6ec361fSKent Overstreet 
431e6ec361fSKent Overstreet 	for (i = pg_offset; i < pg_offset + pg_len; i++) {
432e6ec361fSKent Overstreet 		s->s[i].nr_replicas = nr_ptrs;
433e6ec361fSKent Overstreet 		s->s[i].state = state;
434e6ec361fSKent Overstreet 	}
435e6ec361fSKent Overstreet 
436e6ec361fSKent Overstreet 	if (i == PAGE_SECTORS)
437e6ec361fSKent Overstreet 		s->uptodate = true;
438e6ec361fSKent Overstreet 
439e6ec361fSKent Overstreet 	spin_unlock(&s->lock);
440e6ec361fSKent Overstreet }
441e6ec361fSKent Overstreet 
442e6ec361fSKent Overstreet static int bch2_page_state_set(struct bch_fs *c, subvol_inum inum,
443e6ec361fSKent Overstreet 			       struct page **pages, unsigned nr_pages)
444e6ec361fSKent Overstreet {
445e6ec361fSKent Overstreet 	struct btree_trans trans;
446e6ec361fSKent Overstreet 	struct btree_iter iter;
447e6ec361fSKent Overstreet 	struct bkey_s_c k;
448e6ec361fSKent Overstreet 	u64 offset = pages[0]->index << PAGE_SECTORS_SHIFT;
449e6ec361fSKent Overstreet 	unsigned pg_idx = 0;
450e6ec361fSKent Overstreet 	u32 snapshot;
451e6ec361fSKent Overstreet 	int ret;
452e6ec361fSKent Overstreet 
453e6ec361fSKent Overstreet 	bch2_trans_init(&trans, c, 0, 0);
454e6ec361fSKent Overstreet retry:
455e6ec361fSKent Overstreet 	bch2_trans_begin(&trans);
456e6ec361fSKent Overstreet 
457e6ec361fSKent Overstreet 	ret = bch2_subvolume_get_snapshot(&trans, inum.subvol, &snapshot);
458e6ec361fSKent Overstreet 	if (ret)
459e6ec361fSKent Overstreet 		goto err;
460e6ec361fSKent Overstreet 
461e6ec361fSKent Overstreet 	for_each_btree_key_norestart(&trans, iter, BTREE_ID_extents,
462e6ec361fSKent Overstreet 			   SPOS(inum.inum, offset, snapshot),
463e6ec361fSKent Overstreet 			   BTREE_ITER_SLOTS, k, ret) {
464e6ec361fSKent Overstreet 		unsigned nr_ptrs = bch2_bkey_nr_ptrs_fully_allocated(k);
46579203111SKent Overstreet 		unsigned state = bkey_to_sector_state(k);
466e6ec361fSKent Overstreet 
467e6ec361fSKent Overstreet 		while (pg_idx < nr_pages) {
468e6ec361fSKent Overstreet 			struct page *page = pages[pg_idx];
469e6ec361fSKent Overstreet 			u64 pg_start = page->index << PAGE_SECTORS_SHIFT;
470e6ec361fSKent Overstreet 			u64 pg_end = (page->index + 1) << PAGE_SECTORS_SHIFT;
471e6ec361fSKent Overstreet 			unsigned pg_offset = max(bkey_start_offset(k.k), pg_start) - pg_start;
472e6ec361fSKent Overstreet 			unsigned pg_len = min(k.k->p.offset, pg_end) - pg_offset - pg_start;
473e6ec361fSKent Overstreet 
474e6ec361fSKent Overstreet 			BUG_ON(k.k->p.offset < pg_start);
475e6ec361fSKent Overstreet 			BUG_ON(bkey_start_offset(k.k) > pg_end);
476e6ec361fSKent Overstreet 
477e6ec361fSKent Overstreet 			if (!bch2_page_state_create(page, __GFP_NOFAIL)->uptodate)
478e6ec361fSKent Overstreet 				__bch2_page_state_set(page, pg_offset, pg_len, nr_ptrs, state);
479e6ec361fSKent Overstreet 
480e6ec361fSKent Overstreet 			if (k.k->p.offset < pg_end)
481e6ec361fSKent Overstreet 				break;
482e6ec361fSKent Overstreet 			pg_idx++;
483e6ec361fSKent Overstreet 		}
484e6ec361fSKent Overstreet 
485e6ec361fSKent Overstreet 		if (pg_idx == nr_pages)
486e6ec361fSKent Overstreet 			break;
487e6ec361fSKent Overstreet 	}
488e6ec361fSKent Overstreet 
489e6ec361fSKent Overstreet 	offset = iter.pos.offset;
490e6ec361fSKent Overstreet 	bch2_trans_iter_exit(&trans, &iter);
491e6ec361fSKent Overstreet err:
492549d173cSKent Overstreet 	if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
493e6ec361fSKent Overstreet 		goto retry;
494e6ec361fSKent Overstreet 	bch2_trans_exit(&trans);
495e6ec361fSKent Overstreet 
496e6ec361fSKent Overstreet 	return ret;
497e6ec361fSKent Overstreet }
498e6ec361fSKent Overstreet 
499b44a66a6SKent Overstreet static void bch2_bio_page_state_set(struct bio *bio, struct bkey_s_c k)
500b44a66a6SKent Overstreet {
501b44a66a6SKent Overstreet 	struct bvec_iter iter;
502b44a66a6SKent Overstreet 	struct bio_vec bv;
503b44a66a6SKent Overstreet 	unsigned nr_ptrs = k.k->type == KEY_TYPE_reflink_v
504b44a66a6SKent Overstreet 		? 0 : bch2_bkey_nr_ptrs_fully_allocated(k);
50579203111SKent Overstreet 	unsigned state = bkey_to_sector_state(k);
506b44a66a6SKent Overstreet 
507e6ec361fSKent Overstreet 	bio_for_each_segment(bv, bio, iter)
508e6ec361fSKent Overstreet 		__bch2_page_state_set(bv.bv_page, bv.bv_offset >> 9,
509e6ec361fSKent Overstreet 				      bv.bv_len >> 9, nr_ptrs, state);
510b44a66a6SKent Overstreet }
511b44a66a6SKent Overstreet 
512dcfc593fSKent Overstreet static void mark_pagecache_unallocated(struct bch_inode_info *inode,
513dcfc593fSKent Overstreet 				       u64 start, u64 end)
514dcfc593fSKent Overstreet {
515dcfc593fSKent Overstreet 	pgoff_t index = start >> PAGE_SECTORS_SHIFT;
516dcfc593fSKent Overstreet 	pgoff_t end_index = (end - 1) >> PAGE_SECTORS_SHIFT;
517dcfc593fSKent Overstreet 	struct folio_batch fbatch;
518dcfc593fSKent Overstreet 	unsigned i, j;
519dcfc593fSKent Overstreet 
520dcfc593fSKent Overstreet 	if (end <= start)
521dcfc593fSKent Overstreet 		return;
522dcfc593fSKent Overstreet 
523dcfc593fSKent Overstreet 	folio_batch_init(&fbatch);
524dcfc593fSKent Overstreet 
525dcfc593fSKent Overstreet 	while (filemap_get_folios(inode->v.i_mapping,
526dcfc593fSKent Overstreet 				  &index, end_index, &fbatch)) {
527dcfc593fSKent Overstreet 		for (i = 0; i < folio_batch_count(&fbatch); i++) {
528dcfc593fSKent Overstreet 			struct folio *folio = fbatch.folios[i];
529dcfc593fSKent Overstreet 			u64 pg_start = folio->index << PAGE_SECTORS_SHIFT;
530dcfc593fSKent Overstreet 			u64 pg_end = (folio->index + 1) << PAGE_SECTORS_SHIFT;
531dcfc593fSKent Overstreet 			unsigned pg_offset = max(start, pg_start) - pg_start;
532dcfc593fSKent Overstreet 			unsigned pg_len = min(end, pg_end) - pg_offset - pg_start;
533dcfc593fSKent Overstreet 			struct bch_page_state *s;
534dcfc593fSKent Overstreet 
535dcfc593fSKent Overstreet 			BUG_ON(end <= pg_start);
536dcfc593fSKent Overstreet 			BUG_ON(pg_offset >= PAGE_SECTORS);
537dcfc593fSKent Overstreet 			BUG_ON(pg_offset + pg_len > PAGE_SECTORS);
538dcfc593fSKent Overstreet 
539dcfc593fSKent Overstreet 			folio_lock(folio);
540dcfc593fSKent Overstreet 			s = bch2_page_state(&folio->page);
541dcfc593fSKent Overstreet 
542dcfc593fSKent Overstreet 			if (s) {
543dcfc593fSKent Overstreet 				spin_lock(&s->lock);
544dcfc593fSKent Overstreet 				for (j = pg_offset; j < pg_offset + pg_len; j++)
545dcfc593fSKent Overstreet 					s->s[j].nr_replicas = 0;
546dcfc593fSKent Overstreet 				spin_unlock(&s->lock);
547dcfc593fSKent Overstreet 			}
548dcfc593fSKent Overstreet 
549dcfc593fSKent Overstreet 			folio_unlock(folio);
550dcfc593fSKent Overstreet 		}
551dcfc593fSKent Overstreet 		folio_batch_release(&fbatch);
552dcfc593fSKent Overstreet 		cond_resched();
553dcfc593fSKent Overstreet 	}
554dcfc593fSKent Overstreet }
555dcfc593fSKent Overstreet 
556dcfc593fSKent Overstreet static void mark_pagecache_reserved(struct bch_inode_info *inode,
557dcfc593fSKent Overstreet 				    u64 start, u64 end)
558dcfc593fSKent Overstreet {
559dcfc593fSKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
560dcfc593fSKent Overstreet 	pgoff_t index = start >> PAGE_SECTORS_SHIFT;
561dcfc593fSKent Overstreet 	pgoff_t end_index = (end - 1) >> PAGE_SECTORS_SHIFT;
562dcfc593fSKent Overstreet 	struct folio_batch fbatch;
563dcfc593fSKent Overstreet 	s64 i_sectors_delta = 0;
564dcfc593fSKent Overstreet 	unsigned i, j;
565dcfc593fSKent Overstreet 
566dcfc593fSKent Overstreet 	if (end <= start)
567dcfc593fSKent Overstreet 		return;
568dcfc593fSKent Overstreet 
569dcfc593fSKent Overstreet 	folio_batch_init(&fbatch);
570dcfc593fSKent Overstreet 
571dcfc593fSKent Overstreet 	while (filemap_get_folios(inode->v.i_mapping,
572dcfc593fSKent Overstreet 				  &index, end_index, &fbatch)) {
573dcfc593fSKent Overstreet 		for (i = 0; i < folio_batch_count(&fbatch); i++) {
574dcfc593fSKent Overstreet 			struct folio *folio = fbatch.folios[i];
575dcfc593fSKent Overstreet 			u64 pg_start = folio->index << PAGE_SECTORS_SHIFT;
576dcfc593fSKent Overstreet 			u64 pg_end = (folio->index + 1) << PAGE_SECTORS_SHIFT;
577dcfc593fSKent Overstreet 			unsigned pg_offset = max(start, pg_start) - pg_start;
578dcfc593fSKent Overstreet 			unsigned pg_len = min(end, pg_end) - pg_offset - pg_start;
579dcfc593fSKent Overstreet 			struct bch_page_state *s;
580dcfc593fSKent Overstreet 
581dcfc593fSKent Overstreet 			BUG_ON(end <= pg_start);
582dcfc593fSKent Overstreet 			BUG_ON(pg_offset >= PAGE_SECTORS);
583dcfc593fSKent Overstreet 			BUG_ON(pg_offset + pg_len > PAGE_SECTORS);
584dcfc593fSKent Overstreet 
585dcfc593fSKent Overstreet 			folio_lock(folio);
586dcfc593fSKent Overstreet 			s = bch2_page_state(&folio->page);
587dcfc593fSKent Overstreet 
588dcfc593fSKent Overstreet 			if (s) {
589dcfc593fSKent Overstreet 				spin_lock(&s->lock);
590dcfc593fSKent Overstreet 				for (j = pg_offset; j < pg_offset + pg_len; j++)
591dcfc593fSKent Overstreet 					switch (s->s[j].state) {
592dcfc593fSKent Overstreet 					case SECTOR_UNALLOCATED:
593dcfc593fSKent Overstreet 						s->s[j].state = SECTOR_RESERVED;
594dcfc593fSKent Overstreet 						break;
595dcfc593fSKent Overstreet 					case SECTOR_DIRTY:
596dcfc593fSKent Overstreet 						s->s[j].state = SECTOR_DIRTY_RESERVED;
597dcfc593fSKent Overstreet 						i_sectors_delta--;
598dcfc593fSKent Overstreet 						break;
599dcfc593fSKent Overstreet 					default:
600dcfc593fSKent Overstreet 						break;
601dcfc593fSKent Overstreet 					}
602dcfc593fSKent Overstreet 				spin_unlock(&s->lock);
603dcfc593fSKent Overstreet 			}
604dcfc593fSKent Overstreet 
605dcfc593fSKent Overstreet 			folio_unlock(folio);
606dcfc593fSKent Overstreet 		}
607dcfc593fSKent Overstreet 		folio_batch_release(&fbatch);
608dcfc593fSKent Overstreet 		cond_resched();
609dcfc593fSKent Overstreet 	}
610dcfc593fSKent Overstreet 
611dcfc593fSKent Overstreet 	i_sectors_acct(c, inode, NULL, i_sectors_delta);
612dcfc593fSKent Overstreet }
613dcfc593fSKent Overstreet 
614e1036a2aSKent Overstreet static inline unsigned inode_nr_replicas(struct bch_fs *c, struct bch_inode_info *inode)
615e1036a2aSKent Overstreet {
616e1036a2aSKent Overstreet 	/* XXX: this should not be open coded */
617e1036a2aSKent Overstreet 	return inode->ei_inode.bi_data_replicas
618e1036a2aSKent Overstreet 		? inode->ei_inode.bi_data_replicas - 1
619e1036a2aSKent Overstreet 		: c->opts.data_replicas;
620e1036a2aSKent Overstreet }
621e1036a2aSKent Overstreet 
622f57a6a5dSKent Overstreet static inline unsigned sectors_to_reserve(struct bch_page_sector *s,
623f57a6a5dSKent Overstreet 						  unsigned nr_replicas)
624f57a6a5dSKent Overstreet {
625f57a6a5dSKent Overstreet 	return max(0, (int) nr_replicas -
626f57a6a5dSKent Overstreet 		   s->nr_replicas -
627f57a6a5dSKent Overstreet 		   s->replicas_reserved);
628f57a6a5dSKent Overstreet }
629f57a6a5dSKent Overstreet 
630f57a6a5dSKent Overstreet static int bch2_get_page_disk_reservation(struct bch_fs *c,
631f57a6a5dSKent Overstreet 				struct bch_inode_info *inode,
6321c6fdbd8SKent Overstreet 				struct page *page, bool check_enospc)
6331c6fdbd8SKent Overstreet {
634f57a6a5dSKent Overstreet 	struct bch_page_state *s = bch2_page_state_create(page, 0);
635e1036a2aSKent Overstreet 	unsigned nr_replicas = inode_nr_replicas(c, inode);
636f57a6a5dSKent Overstreet 	struct disk_reservation disk_res = { 0 };
637f57a6a5dSKent Overstreet 	unsigned i, disk_res_sectors = 0;
638f81b648dSKent Overstreet 	int ret;
6391c6fdbd8SKent Overstreet 
640f57a6a5dSKent Overstreet 	if (!s)
641f57a6a5dSKent Overstreet 		return -ENOMEM;
6421c6fdbd8SKent Overstreet 
643f57a6a5dSKent Overstreet 	for (i = 0; i < ARRAY_SIZE(s->s); i++)
644f57a6a5dSKent Overstreet 		disk_res_sectors += sectors_to_reserve(&s->s[i], nr_replicas);
645f57a6a5dSKent Overstreet 
646f57a6a5dSKent Overstreet 	if (!disk_res_sectors)
647f57a6a5dSKent Overstreet 		return 0;
648f57a6a5dSKent Overstreet 
649f57a6a5dSKent Overstreet 	ret = bch2_disk_reservation_get(c, &disk_res,
650f57a6a5dSKent Overstreet 					disk_res_sectors, 1,
651f57a6a5dSKent Overstreet 					!check_enospc
652f57a6a5dSKent Overstreet 					? BCH_DISK_RESERVATION_NOFAIL
653f57a6a5dSKent Overstreet 					: 0);
6541c6fdbd8SKent Overstreet 	if (unlikely(ret))
655f81b648dSKent Overstreet 		return ret;
656f81b648dSKent Overstreet 
657f57a6a5dSKent Overstreet 	for (i = 0; i < ARRAY_SIZE(s->s); i++)
658f57a6a5dSKent Overstreet 		s->s[i].replicas_reserved +=
659f57a6a5dSKent Overstreet 			sectors_to_reserve(&s->s[i], nr_replicas);
660f57a6a5dSKent Overstreet 
661f57a6a5dSKent Overstreet 	return 0;
6621c6fdbd8SKent Overstreet }
6631c6fdbd8SKent Overstreet 
664d1542e03SKent Overstreet struct bch2_page_reservation {
665d1542e03SKent Overstreet 	struct disk_reservation	disk;
666d1542e03SKent Overstreet 	struct quota_res	quota;
667d1542e03SKent Overstreet };
668d1542e03SKent Overstreet 
669d1542e03SKent Overstreet static void bch2_page_reservation_init(struct bch_fs *c,
670f57a6a5dSKent Overstreet 			struct bch_inode_info *inode,
671d1542e03SKent Overstreet 			struct bch2_page_reservation *res)
672d1542e03SKent Overstreet {
673d1542e03SKent Overstreet 	memset(res, 0, sizeof(*res));
674d1542e03SKent Overstreet 
675d1542e03SKent Overstreet 	res->disk.nr_replicas = inode_nr_replicas(c, inode);
676d1542e03SKent Overstreet }
677d1542e03SKent Overstreet 
678d1542e03SKent Overstreet static void bch2_page_reservation_put(struct bch_fs *c,
679d1542e03SKent Overstreet 			struct bch_inode_info *inode,
680d1542e03SKent Overstreet 			struct bch2_page_reservation *res)
681d1542e03SKent Overstreet {
682d1542e03SKent Overstreet 	bch2_disk_reservation_put(c, &res->disk);
683d1542e03SKent Overstreet 	bch2_quota_reservation_put(c, inode, &res->quota);
684d1542e03SKent Overstreet }
685d1542e03SKent Overstreet 
686d1542e03SKent Overstreet static int bch2_page_reservation_get(struct bch_fs *c,
687d1542e03SKent Overstreet 			struct bch_inode_info *inode, struct page *page,
688d1542e03SKent Overstreet 			struct bch2_page_reservation *res,
689bd954215SKent Overstreet 			unsigned offset, unsigned len)
690f57a6a5dSKent Overstreet {
691f57a6a5dSKent Overstreet 	struct bch_page_state *s = bch2_page_state_create(page, 0);
692d1542e03SKent Overstreet 	unsigned i, disk_sectors = 0, quota_sectors = 0;
693f57a6a5dSKent Overstreet 	int ret;
694f57a6a5dSKent Overstreet 
695f57a6a5dSKent Overstreet 	if (!s)
696f57a6a5dSKent Overstreet 		return -ENOMEM;
697f57a6a5dSKent Overstreet 
698e6ec361fSKent Overstreet 	BUG_ON(!s->uptodate);
699e6ec361fSKent Overstreet 
7004b0a66d5SKent Overstreet 	for (i = round_down(offset, block_bytes(c)) >> 9;
7014b0a66d5SKent Overstreet 	     i < round_up(offset + len, block_bytes(c)) >> 9;
702d1542e03SKent Overstreet 	     i++) {
703d1542e03SKent Overstreet 		disk_sectors += sectors_to_reserve(&s->s[i],
704d1542e03SKent Overstreet 						res->disk.nr_replicas);
705d1542e03SKent Overstreet 		quota_sectors += s->s[i].state == SECTOR_UNALLOCATED;
7061c6fdbd8SKent Overstreet 	}
7071c6fdbd8SKent Overstreet 
708d1542e03SKent Overstreet 	if (disk_sectors) {
709bd954215SKent Overstreet 		ret = bch2_disk_reservation_add(c, &res->disk, disk_sectors, 0);
710d1542e03SKent Overstreet 		if (unlikely(ret))
711d1542e03SKent Overstreet 			return ret;
712d1542e03SKent Overstreet 	}
713d1542e03SKent Overstreet 
714d1542e03SKent Overstreet 	if (quota_sectors) {
715d1542e03SKent Overstreet 		ret = bch2_quota_reservation_add(c, inode, &res->quota,
716bd954215SKent Overstreet 						 quota_sectors, true);
717d1542e03SKent Overstreet 		if (unlikely(ret)) {
718d1542e03SKent Overstreet 			struct disk_reservation tmp = {
719d1542e03SKent Overstreet 				.sectors = disk_sectors
720d1542e03SKent Overstreet 			};
721d1542e03SKent Overstreet 
722d1542e03SKent Overstreet 			bch2_disk_reservation_put(c, &tmp);
723d1542e03SKent Overstreet 			res->disk.sectors -= disk_sectors;
724d1542e03SKent Overstreet 			return ret;
725d1542e03SKent Overstreet 		}
726d1542e03SKent Overstreet 	}
727d1542e03SKent Overstreet 
728d1542e03SKent Overstreet 	return 0;
729f57a6a5dSKent Overstreet }
730f57a6a5dSKent Overstreet 
7311c6fdbd8SKent Overstreet static void bch2_clear_page_bits(struct page *page)
7321c6fdbd8SKent Overstreet {
7331c6fdbd8SKent Overstreet 	struct bch_inode_info *inode = to_bch_ei(page->mapping->host);
7341c6fdbd8SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
735f57a6a5dSKent Overstreet 	struct bch_page_state *s = bch2_page_state(page);
736d1542e03SKent Overstreet 	struct disk_reservation disk_res = { 0 };
737f57a6a5dSKent Overstreet 	int i, dirty_sectors = 0;
7381c6fdbd8SKent Overstreet 
739f57a6a5dSKent Overstreet 	if (!s)
7401c6fdbd8SKent Overstreet 		return;
7411c6fdbd8SKent Overstreet 
7423826ee0bSKent Overstreet 	EBUG_ON(!PageLocked(page));
7433826ee0bSKent Overstreet 	EBUG_ON(PageWriteback(page));
7443826ee0bSKent Overstreet 
745f57a6a5dSKent Overstreet 	for (i = 0; i < ARRAY_SIZE(s->s); i++) {
746d1542e03SKent Overstreet 		disk_res.sectors += s->s[i].replicas_reserved;
747d1542e03SKent Overstreet 		s->s[i].replicas_reserved = 0;
748d1542e03SKent Overstreet 
749b44a66a6SKent Overstreet 		switch (s->s[i].state) {
750b44a66a6SKent Overstreet 		case SECTOR_DIRTY:
751f57a6a5dSKent Overstreet 			s->s[i].state = SECTOR_UNALLOCATED;
752b44a66a6SKent Overstreet 			--dirty_sectors;
753b44a66a6SKent Overstreet 			break;
754b44a66a6SKent Overstreet 		case SECTOR_DIRTY_RESERVED:
755b44a66a6SKent Overstreet 			s->s[i].state = SECTOR_RESERVED;
756b44a66a6SKent Overstreet 			break;
757b44a66a6SKent Overstreet 		default:
758b44a66a6SKent Overstreet 			break;
759f57a6a5dSKent Overstreet 		}
760f57a6a5dSKent Overstreet 	}
761adfcfaf0SKent Overstreet 
762d1542e03SKent Overstreet 	bch2_disk_reservation_put(c, &disk_res);
763d1542e03SKent Overstreet 
764b44a66a6SKent Overstreet 	i_sectors_acct(c, inode, NULL, dirty_sectors);
765adfcfaf0SKent Overstreet 
766f57a6a5dSKent Overstreet 	bch2_page_state_release(page);
7671c6fdbd8SKent Overstreet }
7681c6fdbd8SKent Overstreet 
769d1542e03SKent Overstreet static void bch2_set_page_dirty(struct bch_fs *c,
770d1542e03SKent Overstreet 			struct bch_inode_info *inode, struct page *page,
771d1542e03SKent Overstreet 			struct bch2_page_reservation *res,
772d1542e03SKent Overstreet 			unsigned offset, unsigned len)
7731c6fdbd8SKent Overstreet {
774d1542e03SKent Overstreet 	struct bch_page_state *s = bch2_page_state(page);
775f57a6a5dSKent Overstreet 	unsigned i, dirty_sectors = 0;
7761c6fdbd8SKent Overstreet 
777877dfb34SKent Overstreet 	WARN_ON((u64) page_offset(page) + offset + len >
778877dfb34SKent Overstreet 		round_up((u64) i_size_read(&inode->v), block_bytes(c)));
779fb472ac5SKent Overstreet 
7803826ee0bSKent Overstreet 	spin_lock(&s->lock);
7813826ee0bSKent Overstreet 
7824b0a66d5SKent Overstreet 	for (i = round_down(offset, block_bytes(c)) >> 9;
7834b0a66d5SKent Overstreet 	     i < round_up(offset + len, block_bytes(c)) >> 9;
784d1542e03SKent Overstreet 	     i++) {
785d1542e03SKent Overstreet 		unsigned sectors = sectors_to_reserve(&s->s[i],
786d1542e03SKent Overstreet 						res->disk.nr_replicas);
7871c6fdbd8SKent Overstreet 
788406d6d5aSKent Overstreet 		/*
789406d6d5aSKent Overstreet 		 * This can happen if we race with the error path in
790406d6d5aSKent Overstreet 		 * bch2_writepage_io_done():
791406d6d5aSKent Overstreet 		 */
792406d6d5aSKent Overstreet 		sectors = min_t(unsigned, sectors, res->disk.sectors);
793406d6d5aSKent Overstreet 
794d1542e03SKent Overstreet 		s->s[i].replicas_reserved += sectors;
795d1542e03SKent Overstreet 		res->disk.sectors -= sectors;
796adfcfaf0SKent Overstreet 
797b44a66a6SKent Overstreet 		switch (s->s[i].state) {
798b44a66a6SKent Overstreet 		case SECTOR_UNALLOCATED:
799b44a66a6SKent Overstreet 			s->s[i].state = SECTOR_DIRTY;
800f57a6a5dSKent Overstreet 			dirty_sectors++;
801b44a66a6SKent Overstreet 			break;
802b44a66a6SKent Overstreet 		case SECTOR_RESERVED:
803b44a66a6SKent Overstreet 			s->s[i].state = SECTOR_DIRTY_RESERVED;
804b44a66a6SKent Overstreet 			break;
805b44a66a6SKent Overstreet 		default:
806b44a66a6SKent Overstreet 			break;
807b44a66a6SKent Overstreet 		}
808f57a6a5dSKent Overstreet 	}
809f57a6a5dSKent Overstreet 
8103826ee0bSKent Overstreet 	spin_unlock(&s->lock);
8113826ee0bSKent Overstreet 
812d1542e03SKent Overstreet 	i_sectors_acct(c, inode, &res->quota, dirty_sectors);
8131c6fdbd8SKent Overstreet 
814d1542e03SKent Overstreet 	if (!PageDirty(page))
815d1542e03SKent Overstreet 		filemap_dirty_folio(inode->v.i_mapping, page_folio(page));
8161c6fdbd8SKent Overstreet }
8171c6fdbd8SKent Overstreet 
8181c6fdbd8SKent Overstreet vm_fault_t bch2_page_fault(struct vm_fault *vmf)
8191c6fdbd8SKent Overstreet {
8201c6fdbd8SKent Overstreet 	struct file *file = vmf->vma->vm_file;
821eb8e6e9cSKent Overstreet 	struct address_space *mapping = file->f_mapping;
822eb8e6e9cSKent Overstreet 	struct address_space *fdm = faults_disabled_mapping();
8231c6fdbd8SKent Overstreet 	struct bch_inode_info *inode = file_bch_inode(file);
8241c6fdbd8SKent Overstreet 	int ret;
8251c6fdbd8SKent Overstreet 
826eb8e6e9cSKent Overstreet 	if (fdm == mapping)
827eb8e6e9cSKent Overstreet 		return VM_FAULT_SIGBUS;
828eb8e6e9cSKent Overstreet 
829eb8e6e9cSKent Overstreet 	/* Lock ordering: */
830eb8e6e9cSKent Overstreet 	if (fdm > mapping) {
831eb8e6e9cSKent Overstreet 		struct bch_inode_info *fdm_host = to_bch_ei(fdm->host);
832eb8e6e9cSKent Overstreet 
833a7ecd30cSKent Overstreet 		if (bch2_pagecache_add_tryget(inode))
834eb8e6e9cSKent Overstreet 			goto got_lock;
835eb8e6e9cSKent Overstreet 
836a7ecd30cSKent Overstreet 		bch2_pagecache_block_put(fdm_host);
837eb8e6e9cSKent Overstreet 
838a7ecd30cSKent Overstreet 		bch2_pagecache_add_get(inode);
839a7ecd30cSKent Overstreet 		bch2_pagecache_add_put(inode);
840eb8e6e9cSKent Overstreet 
841a7ecd30cSKent Overstreet 		bch2_pagecache_block_get(fdm_host);
842eb8e6e9cSKent Overstreet 
843eb8e6e9cSKent Overstreet 		/* Signal that lock has been dropped: */
844eb8e6e9cSKent Overstreet 		set_fdm_dropped_locks();
845eb8e6e9cSKent Overstreet 		return VM_FAULT_SIGBUS;
846eb8e6e9cSKent Overstreet 	}
847eb8e6e9cSKent Overstreet 
848a7ecd30cSKent Overstreet 	bch2_pagecache_add_get(inode);
849eb8e6e9cSKent Overstreet got_lock:
8501c6fdbd8SKent Overstreet 	ret = filemap_fault(vmf);
851a7ecd30cSKent Overstreet 	bch2_pagecache_add_put(inode);
8521c6fdbd8SKent Overstreet 
8531c6fdbd8SKent Overstreet 	return ret;
8541c6fdbd8SKent Overstreet }
8551c6fdbd8SKent Overstreet 
8561c6fdbd8SKent Overstreet vm_fault_t bch2_page_mkwrite(struct vm_fault *vmf)
8571c6fdbd8SKent Overstreet {
8581c6fdbd8SKent Overstreet 	struct page *page = vmf->page;
8591c6fdbd8SKent Overstreet 	struct file *file = vmf->vma->vm_file;
8601c6fdbd8SKent Overstreet 	struct bch_inode_info *inode = file_bch_inode(file);
8611c6fdbd8SKent Overstreet 	struct address_space *mapping = file->f_mapping;
8621c6fdbd8SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
863d1542e03SKent Overstreet 	struct bch2_page_reservation res;
8646cc3535dSKent Overstreet 	unsigned len;
8656cc3535dSKent Overstreet 	loff_t isize;
866e6ec361fSKent Overstreet 	int ret;
8671c6fdbd8SKent Overstreet 
868d1542e03SKent Overstreet 	bch2_page_reservation_init(c, inode, &res);
869d1542e03SKent Overstreet 
8701c6fdbd8SKent Overstreet 	sb_start_pagefault(inode->v.i_sb);
8711c6fdbd8SKent Overstreet 	file_update_time(file);
8721c6fdbd8SKent Overstreet 
8731c6fdbd8SKent Overstreet 	/*
8741c6fdbd8SKent Overstreet 	 * Not strictly necessary, but helps avoid dio writes livelocking in
8751c6fdbd8SKent Overstreet 	 * write_invalidate_inode_pages_range() - can drop this if/when we get
8761c6fdbd8SKent Overstreet 	 * a write_invalidate_inode_pages_range() that works without dropping
8771c6fdbd8SKent Overstreet 	 * page lock before invalidating page
8781c6fdbd8SKent Overstreet 	 */
879a7ecd30cSKent Overstreet 	bch2_pagecache_add_get(inode);
8801c6fdbd8SKent Overstreet 
8811c6fdbd8SKent Overstreet 	lock_page(page);
8826cc3535dSKent Overstreet 	isize = i_size_read(&inode->v);
8836cc3535dSKent Overstreet 
8846cc3535dSKent Overstreet 	if (page->mapping != mapping || page_offset(page) >= isize) {
8851c6fdbd8SKent Overstreet 		unlock_page(page);
8861c6fdbd8SKent Overstreet 		ret = VM_FAULT_NOPAGE;
8871c6fdbd8SKent Overstreet 		goto out;
8881c6fdbd8SKent Overstreet 	}
8891c6fdbd8SKent Overstreet 
890d55460bbSKent Overstreet 	len = min_t(loff_t, PAGE_SIZE, isize - page_offset(page));
8916cc3535dSKent Overstreet 
892e6ec361fSKent Overstreet 	if (!bch2_page_state_create(page, __GFP_NOFAIL)->uptodate) {
893e6ec361fSKent Overstreet 		if (bch2_page_state_set(c, inode_inum(inode), &page, 1)) {
894e6ec361fSKent Overstreet 			unlock_page(page);
895e6ec361fSKent Overstreet 			ret = VM_FAULT_SIGBUS;
896e6ec361fSKent Overstreet 			goto out;
897e6ec361fSKent Overstreet 		}
898e6ec361fSKent Overstreet 	}
899e6ec361fSKent Overstreet 
900bd954215SKent Overstreet 	if (bch2_page_reservation_get(c, inode, page, &res, 0, len)) {
9011c6fdbd8SKent Overstreet 		unlock_page(page);
9021c6fdbd8SKent Overstreet 		ret = VM_FAULT_SIGBUS;
9031c6fdbd8SKent Overstreet 		goto out;
9041c6fdbd8SKent Overstreet 	}
9051c6fdbd8SKent Overstreet 
9066cc3535dSKent Overstreet 	bch2_set_page_dirty(c, inode, page, &res, 0, len);
9071b783a69SKent Overstreet 	bch2_page_reservation_put(c, inode, &res);
9081b783a69SKent Overstreet 
9091c6fdbd8SKent Overstreet 	wait_for_stable_page(page);
910e6ec361fSKent Overstreet 	ret = VM_FAULT_LOCKED;
9111c6fdbd8SKent Overstreet out:
912a7ecd30cSKent Overstreet 	bch2_pagecache_add_put(inode);
9131c6fdbd8SKent Overstreet 	sb_end_pagefault(inode->v.i_sb);
914d1542e03SKent Overstreet 
9151c6fdbd8SKent Overstreet 	return ret;
9161c6fdbd8SKent Overstreet }
9171c6fdbd8SKent Overstreet 
9181c6fdbd8SKent Overstreet void bch2_invalidate_folio(struct folio *folio, size_t offset, size_t length)
9191c6fdbd8SKent Overstreet {
9201c6fdbd8SKent Overstreet 	if (offset || length < folio_size(folio))
9211c6fdbd8SKent Overstreet 		return;
9221c6fdbd8SKent Overstreet 
9231c6fdbd8SKent Overstreet 	bch2_clear_page_bits(&folio->page);
9241c6fdbd8SKent Overstreet }
9251c6fdbd8SKent Overstreet 
9261c6fdbd8SKent Overstreet bool bch2_release_folio(struct folio *folio, gfp_t gfp_mask)
9271c6fdbd8SKent Overstreet {
928a6d90385SKent Overstreet 	if (folio_test_dirty(folio) || folio_test_writeback(folio))
9291c6fdbd8SKent Overstreet 		return false;
9301c6fdbd8SKent Overstreet 
9311c6fdbd8SKent Overstreet 	bch2_clear_page_bits(&folio->page);
9321c6fdbd8SKent Overstreet 	return true;
9331c6fdbd8SKent Overstreet }
9341c6fdbd8SKent Overstreet 
9351c6fdbd8SKent Overstreet /* readpage(s): */
9361c6fdbd8SKent Overstreet 
9371c6fdbd8SKent Overstreet static void bch2_readpages_end_io(struct bio *bio)
9381c6fdbd8SKent Overstreet {
9391c6fdbd8SKent Overstreet 	struct bvec_iter_all iter;
9401c6fdbd8SKent Overstreet 	struct bio_vec *bv;
9411c6fdbd8SKent Overstreet 
9421c6fdbd8SKent Overstreet 	bio_for_each_segment_all(bv, bio, iter) {
9431c6fdbd8SKent Overstreet 		struct page *page = bv->bv_page;
9441c6fdbd8SKent Overstreet 
9451c6fdbd8SKent Overstreet 		if (!bio->bi_status) {
9461c6fdbd8SKent Overstreet 			SetPageUptodate(page);
9471c6fdbd8SKent Overstreet 		} else {
9481c6fdbd8SKent Overstreet 			ClearPageUptodate(page);
9491c6fdbd8SKent Overstreet 			SetPageError(page);
9501c6fdbd8SKent Overstreet 		}
9511c6fdbd8SKent Overstreet 		unlock_page(page);
9521c6fdbd8SKent Overstreet 	}
9531c6fdbd8SKent Overstreet 
9541c6fdbd8SKent Overstreet 	bio_put(bio);
9551c6fdbd8SKent Overstreet }
9561c6fdbd8SKent Overstreet 
9571c6fdbd8SKent Overstreet struct readpages_iter {
9581c6fdbd8SKent Overstreet 	struct address_space	*mapping;
9591c6fdbd8SKent Overstreet 	struct page		**pages;
9601c6fdbd8SKent Overstreet 	unsigned		nr_pages;
9611c6fdbd8SKent Overstreet 	unsigned		idx;
9621c6fdbd8SKent Overstreet 	pgoff_t			offset;
9631c6fdbd8SKent Overstreet };
9641c6fdbd8SKent Overstreet 
9651c6fdbd8SKent Overstreet static int readpages_iter_init(struct readpages_iter *iter,
9661c6fdbd8SKent Overstreet 			       struct readahead_control *ractl)
9671c6fdbd8SKent Overstreet {
9681c6fdbd8SKent Overstreet 	unsigned i, nr_pages = readahead_count(ractl);
9691c6fdbd8SKent Overstreet 
9701c6fdbd8SKent Overstreet 	memset(iter, 0, sizeof(*iter));
9711c6fdbd8SKent Overstreet 
9721c6fdbd8SKent Overstreet 	iter->mapping	= ractl->mapping;
9731c6fdbd8SKent Overstreet 	iter->offset	= readahead_index(ractl);
9741c6fdbd8SKent Overstreet 	iter->nr_pages	= nr_pages;
9751c6fdbd8SKent Overstreet 
9761c6fdbd8SKent Overstreet 	iter->pages = kmalloc_array(nr_pages, sizeof(struct page *), GFP_NOFS);
9771c6fdbd8SKent Overstreet 	if (!iter->pages)
9781c6fdbd8SKent Overstreet 		return -ENOMEM;
9791c6fdbd8SKent Overstreet 
98089931472SKent Overstreet 	nr_pages = __readahead_batch(ractl, iter->pages, nr_pages);
9811c6fdbd8SKent Overstreet 	for (i = 0; i < nr_pages; i++) {
982f57a6a5dSKent Overstreet 		__bch2_page_state_create(iter->pages[i], __GFP_NOFAIL);
9831c6fdbd8SKent Overstreet 		put_page(iter->pages[i]);
9841c6fdbd8SKent Overstreet 	}
9851c6fdbd8SKent Overstreet 
9861c6fdbd8SKent Overstreet 	return 0;
9871c6fdbd8SKent Overstreet }
9881c6fdbd8SKent Overstreet 
9891c6fdbd8SKent Overstreet static inline struct page *readpage_iter_next(struct readpages_iter *iter)
9901c6fdbd8SKent Overstreet {
9911c6fdbd8SKent Overstreet 	if (iter->idx >= iter->nr_pages)
9921c6fdbd8SKent Overstreet 		return NULL;
9931c6fdbd8SKent Overstreet 
9941c6fdbd8SKent Overstreet 	EBUG_ON(iter->pages[iter->idx]->index != iter->offset + iter->idx);
9951c6fdbd8SKent Overstreet 
9961c6fdbd8SKent Overstreet 	return iter->pages[iter->idx];
9971c6fdbd8SKent Overstreet }
9981c6fdbd8SKent Overstreet 
99935189e09SKent Overstreet static bool extent_partial_reads_expensive(struct bkey_s_c k)
100035189e09SKent Overstreet {
100135189e09SKent Overstreet 	struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
100235189e09SKent Overstreet 	struct bch_extent_crc_unpacked crc;
100335189e09SKent Overstreet 	const union bch_extent_entry *i;
100435189e09SKent Overstreet 
100535189e09SKent Overstreet 	bkey_for_each_crc(k.k, ptrs, crc, i)
100635189e09SKent Overstreet 		if (crc.csum_type || crc.compression_type)
100735189e09SKent Overstreet 			return true;
100835189e09SKent Overstreet 	return false;
100935189e09SKent Overstreet }
101035189e09SKent Overstreet 
10111c6fdbd8SKent Overstreet static void readpage_bio_extend(struct readpages_iter *iter,
101276426098SKent Overstreet 				struct bio *bio,
101376426098SKent Overstreet 				unsigned sectors_this_extent,
10141c6fdbd8SKent Overstreet 				bool get_more)
10151c6fdbd8SKent Overstreet {
101676426098SKent Overstreet 	while (bio_sectors(bio) < sectors_this_extent &&
10171c6fdbd8SKent Overstreet 	       bio->bi_vcnt < bio->bi_max_vecs) {
10187279c1a2SKent Overstreet 		pgoff_t page_offset = bio_end_sector(bio) >> PAGE_SECTORS_SHIFT;
10191c6fdbd8SKent Overstreet 		struct page *page = readpage_iter_next(iter);
10201c6fdbd8SKent Overstreet 		int ret;
10211c6fdbd8SKent Overstreet 
10221c6fdbd8SKent Overstreet 		if (page) {
10231c6fdbd8SKent Overstreet 			if (iter->offset + iter->idx != page_offset)
10241c6fdbd8SKent Overstreet 				break;
10251c6fdbd8SKent Overstreet 
10261c6fdbd8SKent Overstreet 			iter->idx++;
10271c6fdbd8SKent Overstreet 		} else {
10281c6fdbd8SKent Overstreet 			if (!get_more)
10291c6fdbd8SKent Overstreet 				break;
10301c6fdbd8SKent Overstreet 
10311c6fdbd8SKent Overstreet 			page = xa_load(&iter->mapping->i_pages, page_offset);
10321c6fdbd8SKent Overstreet 			if (page && !xa_is_value(page))
10331c6fdbd8SKent Overstreet 				break;
10341c6fdbd8SKent Overstreet 
10351c6fdbd8SKent Overstreet 			page = __page_cache_alloc(readahead_gfp_mask(iter->mapping));
10361c6fdbd8SKent Overstreet 			if (!page)
10371c6fdbd8SKent Overstreet 				break;
10381c6fdbd8SKent Overstreet 
1039f57a6a5dSKent Overstreet 			if (!__bch2_page_state_create(page, 0)) {
1040f57a6a5dSKent Overstreet 				put_page(page);
1041f57a6a5dSKent Overstreet 				break;
1042f57a6a5dSKent Overstreet 			}
10431c6fdbd8SKent Overstreet 
10441c6fdbd8SKent Overstreet 			ret = add_to_page_cache_lru(page, iter->mapping,
10451c6fdbd8SKent Overstreet 						    page_offset, GFP_NOFS);
10461c6fdbd8SKent Overstreet 			if (ret) {
1047f57a6a5dSKent Overstreet 				__bch2_page_state_release(page);
10481c6fdbd8SKent Overstreet 				put_page(page);
10491c6fdbd8SKent Overstreet 				break;
10501c6fdbd8SKent Overstreet 			}
10511c6fdbd8SKent Overstreet 
10521c6fdbd8SKent Overstreet 			put_page(page);
10531c6fdbd8SKent Overstreet 		}
10541c6fdbd8SKent Overstreet 
1055885678f6SKent Overstreet 		BUG_ON(!bio_add_page(bio, page, PAGE_SIZE, 0));
10561c6fdbd8SKent Overstreet 	}
10571c6fdbd8SKent Overstreet }
10581c6fdbd8SKent Overstreet 
10598c6d298aSKent Overstreet static void bchfs_read(struct btree_trans *trans,
10608c6d298aSKent Overstreet 		       struct bch_read_bio *rbio,
10618c6d298aSKent Overstreet 		       subvol_inum inum,
10621c6fdbd8SKent Overstreet 		       struct readpages_iter *readpages_iter)
10631c6fdbd8SKent Overstreet {
10640f238367SKent Overstreet 	struct bch_fs *c = trans->c;
10658c6d298aSKent Overstreet 	struct btree_iter iter;
106607a1006aSKent Overstreet 	struct bkey_buf sk;
10671c6fdbd8SKent Overstreet 	int flags = BCH_READ_RETRY_IF_STALE|
10681c6fdbd8SKent Overstreet 		BCH_READ_MAY_PROMOTE;
10698c6d298aSKent Overstreet 	u32 snapshot;
107076426098SKent Overstreet 	int ret = 0;
10711c6fdbd8SKent Overstreet 
10721c6fdbd8SKent Overstreet 	rbio->c = c;
10731c6fdbd8SKent Overstreet 	rbio->start_time = local_clock();
10748c6d298aSKent Overstreet 	rbio->subvol = inum.subvol;
107535189e09SKent Overstreet 
107607a1006aSKent Overstreet 	bch2_bkey_buf_init(&sk);
107776426098SKent Overstreet retry:
1078700c25b3SKent Overstreet 	bch2_trans_begin(trans);
10798c6d298aSKent Overstreet 	iter = (struct btree_iter) { NULL };
1080700c25b3SKent Overstreet 
10818c6d298aSKent Overstreet 	ret = bch2_subvolume_get_snapshot(trans, inum.subvol, &snapshot);
10828c6d298aSKent Overstreet 	if (ret)
10838c6d298aSKent Overstreet 		goto err;
10848c6d298aSKent Overstreet 
10858c6d298aSKent Overstreet 	bch2_trans_iter_init(trans, &iter, BTREE_ID_extents,
10868c6d298aSKent Overstreet 			     SPOS(inum.inum, rbio->bio.bi_iter.bi_sector, snapshot),
108757cfdd8bSKent Overstreet 			     BTREE_ITER_SLOTS);
10881c6fdbd8SKent Overstreet 	while (1) {
10891c6fdbd8SKent Overstreet 		struct bkey_s_c k;
109076426098SKent Overstreet 		unsigned bytes, sectors, offset_into_extent;
10915ff75ccbSKent Overstreet 		enum btree_id data_btree = BTREE_ID_extents;
10921c6fdbd8SKent Overstreet 
10933737e0ddSKent Overstreet 		/*
10943737e0ddSKent Overstreet 		 * read_extent -> io_time_reset may cause a transaction restart
10953737e0ddSKent Overstreet 		 * without returning an error, we need to check for that here:
10963737e0ddSKent Overstreet 		 */
1097549d173cSKent Overstreet 		ret = bch2_trans_relock(trans);
1098549d173cSKent Overstreet 		if (ret)
10993737e0ddSKent Overstreet 			break;
11003737e0ddSKent Overstreet 
11018c6d298aSKent Overstreet 		bch2_btree_iter_set_pos(&iter,
11028c6d298aSKent Overstreet 				POS(inum.inum, rbio->bio.bi_iter.bi_sector));
11031c6fdbd8SKent Overstreet 
11048c6d298aSKent Overstreet 		k = bch2_btree_iter_peek_slot(&iter);
110576426098SKent Overstreet 		ret = bkey_err(k);
110676426098SKent Overstreet 		if (ret)
110776426098SKent Overstreet 			break;
11081c6fdbd8SKent Overstreet 
11098c6d298aSKent Overstreet 		offset_into_extent = iter.pos.offset -
111006ed8558SKent Overstreet 			bkey_start_offset(k.k);
111176426098SKent Overstreet 		sectors = k.k->size - offset_into_extent;
111276426098SKent Overstreet 
111307a1006aSKent Overstreet 		bch2_bkey_buf_reassemble(&sk, c, k);
111413dcd4abSKent Overstreet 
11155ff75ccbSKent Overstreet 		ret = bch2_read_indirect_extent(trans, &data_btree,
111622d8a33dSYuxuan Shui 					&offset_into_extent, &sk);
111776426098SKent Overstreet 		if (ret)
111876426098SKent Overstreet 			break;
111976426098SKent Overstreet 
112013dcd4abSKent Overstreet 		k = bkey_i_to_s_c(sk.k);
112113dcd4abSKent Overstreet 
112276426098SKent Overstreet 		sectors = min(sectors, k.k->size - offset_into_extent);
112376426098SKent Overstreet 
112435189e09SKent Overstreet 		if (readpages_iter)
112535189e09SKent Overstreet 			readpage_bio_extend(readpages_iter, &rbio->bio, sectors,
112635189e09SKent Overstreet 					    extent_partial_reads_expensive(k));
11271c6fdbd8SKent Overstreet 
112876426098SKent Overstreet 		bytes = min(sectors, bio_sectors(&rbio->bio)) << 9;
112906ed8558SKent Overstreet 		swap(rbio->bio.bi_iter.bi_size, bytes);
11301c6fdbd8SKent Overstreet 
113106ed8558SKent Overstreet 		if (rbio->bio.bi_iter.bi_size == bytes)
11321c6fdbd8SKent Overstreet 			flags |= BCH_READ_LAST_FRAGMENT;
11331c6fdbd8SKent Overstreet 
1134b44a66a6SKent Overstreet 		bch2_bio_page_state_set(&rbio->bio, k);
11351c6fdbd8SKent Overstreet 
11368c6d298aSKent Overstreet 		bch2_read_extent(trans, rbio, iter.pos,
11375ff75ccbSKent Overstreet 				 data_btree, k, offset_into_extent, flags);
11381c6fdbd8SKent Overstreet 
11391c6fdbd8SKent Overstreet 		if (flags & BCH_READ_LAST_FRAGMENT)
114035189e09SKent Overstreet 			break;
11411c6fdbd8SKent Overstreet 
114206ed8558SKent Overstreet 		swap(rbio->bio.bi_iter.bi_size, bytes);
114306ed8558SKent Overstreet 		bio_advance(&rbio->bio, bytes);
1144084d42bbSKent Overstreet 
1145084d42bbSKent Overstreet 		ret = btree_trans_too_many_iters(trans);
1146084d42bbSKent Overstreet 		if (ret)
1147084d42bbSKent Overstreet 			break;
11481c6fdbd8SKent Overstreet 	}
11498c6d298aSKent Overstreet err:
11508c6d298aSKent Overstreet 	bch2_trans_iter_exit(trans, &iter);
115176426098SKent Overstreet 
1152549d173cSKent Overstreet 	if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
115376426098SKent Overstreet 		goto retry;
115476426098SKent Overstreet 
115535189e09SKent Overstreet 	if (ret) {
11567fec8266SKent Overstreet 		bch_err_inum_offset_ratelimited(c,
11577fec8266SKent Overstreet 				iter.pos.inode,
11587fec8266SKent Overstreet 				iter.pos.offset << 9,
11590fefe8d8SKent Overstreet 				"read error %i from btree lookup", ret);
11600fefe8d8SKent Overstreet 		rbio->bio.bi_status = BLK_STS_IOERR;
116176426098SKent Overstreet 		bio_endio(&rbio->bio);
11621c6fdbd8SKent Overstreet 	}
11631c6fdbd8SKent Overstreet 
116407a1006aSKent Overstreet 	bch2_bkey_buf_exit(&sk, c);
116535189e09SKent Overstreet }
116635189e09SKent Overstreet 
11671c6fdbd8SKent Overstreet void bch2_readahead(struct readahead_control *ractl)
11681c6fdbd8SKent Overstreet {
11691c6fdbd8SKent Overstreet 	struct bch_inode_info *inode = to_bch_ei(ractl->mapping->host);
11701c6fdbd8SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
117101ad6737SKent Overstreet 	struct bch_io_opts opts;
1172424eb881SKent Overstreet 	struct btree_trans trans;
11731c6fdbd8SKent Overstreet 	struct page *page;
11741c6fdbd8SKent Overstreet 	struct readpages_iter readpages_iter;
11751c6fdbd8SKent Overstreet 	int ret;
11761c6fdbd8SKent Overstreet 
117701ad6737SKent Overstreet 	bch2_inode_opts_get(&opts, c, &inode->ei_inode);
117801ad6737SKent Overstreet 
11791c6fdbd8SKent Overstreet 	ret = readpages_iter_init(&readpages_iter, ractl);
11801c6fdbd8SKent Overstreet 	BUG_ON(ret);
11811c6fdbd8SKent Overstreet 
118220bceecbSKent Overstreet 	bch2_trans_init(&trans, c, 0, 0);
11831c6fdbd8SKent Overstreet 
1184a7ecd30cSKent Overstreet 	bch2_pagecache_add_get(inode);
11851c6fdbd8SKent Overstreet 
11861c6fdbd8SKent Overstreet 	while ((page = readpage_iter_next(&readpages_iter))) {
11871c6fdbd8SKent Overstreet 		pgoff_t index = readpages_iter.offset + readpages_iter.idx;
11881c6fdbd8SKent Overstreet 		unsigned n = min_t(unsigned,
11891c6fdbd8SKent Overstreet 				   readpages_iter.nr_pages -
11901c6fdbd8SKent Overstreet 				   readpages_iter.idx,
11911c6fdbd8SKent Overstreet 				   BIO_MAX_VECS);
11921c6fdbd8SKent Overstreet 		struct bch_read_bio *rbio =
11931c6fdbd8SKent Overstreet 			rbio_init(bio_alloc_bioset(NULL, n, REQ_OP_READ,
11941c6fdbd8SKent Overstreet 						   GFP_NOFS, &c->bio_read),
11951c6fdbd8SKent Overstreet 				  opts);
11961c6fdbd8SKent Overstreet 
11971c6fdbd8SKent Overstreet 		readpages_iter.idx++;
11981c6fdbd8SKent Overstreet 
11997279c1a2SKent Overstreet 		rbio->bio.bi_iter.bi_sector = (sector_t) index << PAGE_SECTORS_SHIFT;
12001c6fdbd8SKent Overstreet 		rbio->bio.bi_end_io = bch2_readpages_end_io;
1201885678f6SKent Overstreet 		BUG_ON(!bio_add_page(&rbio->bio, page, PAGE_SIZE, 0));
12021c6fdbd8SKent Overstreet 
12038c6d298aSKent Overstreet 		bchfs_read(&trans, rbio, inode_inum(inode),
12040f238367SKent Overstreet 			   &readpages_iter);
12051c6fdbd8SKent Overstreet 	}
12061c6fdbd8SKent Overstreet 
1207a7ecd30cSKent Overstreet 	bch2_pagecache_add_put(inode);
1208424eb881SKent Overstreet 
1209424eb881SKent Overstreet 	bch2_trans_exit(&trans);
12101c6fdbd8SKent Overstreet 	kfree(readpages_iter.pages);
12111c6fdbd8SKent Overstreet }
12121c6fdbd8SKent Overstreet 
12131c6fdbd8SKent Overstreet static void __bchfs_readpage(struct bch_fs *c, struct bch_read_bio *rbio,
12148c6d298aSKent Overstreet 			     subvol_inum inum, struct page *page)
12151c6fdbd8SKent Overstreet {
1216424eb881SKent Overstreet 	struct btree_trans trans;
12171c6fdbd8SKent Overstreet 
1218f57a6a5dSKent Overstreet 	bch2_page_state_create(page, __GFP_NOFAIL);
12191c6fdbd8SKent Overstreet 
12201c6fdbd8SKent Overstreet 	rbio->bio.bi_opf = REQ_OP_READ|REQ_SYNC;
12217f5e31e1SKent Overstreet 	rbio->bio.bi_iter.bi_sector =
12227279c1a2SKent Overstreet 		(sector_t) page->index << PAGE_SECTORS_SHIFT;
12237f5e31e1SKent Overstreet 	BUG_ON(!bio_add_page(&rbio->bio, page, PAGE_SIZE, 0));
12241c6fdbd8SKent Overstreet 
122520bceecbSKent Overstreet 	bch2_trans_init(&trans, c, 0, 0);
12268c6d298aSKent Overstreet 	bchfs_read(&trans, rbio, inum, NULL);
1227424eb881SKent Overstreet 	bch2_trans_exit(&trans);
12281c6fdbd8SKent Overstreet }
12291c6fdbd8SKent Overstreet 
12301c6fdbd8SKent Overstreet static void bch2_read_single_page_end_io(struct bio *bio)
12311c6fdbd8SKent Overstreet {
12321c6fdbd8SKent Overstreet 	complete(bio->bi_private);
12331c6fdbd8SKent Overstreet }
12341c6fdbd8SKent Overstreet 
12351c6fdbd8SKent Overstreet static int bch2_read_single_page(struct page *page,
12361c6fdbd8SKent Overstreet 				 struct address_space *mapping)
12371c6fdbd8SKent Overstreet {
12381c6fdbd8SKent Overstreet 	struct bch_inode_info *inode = to_bch_ei(mapping->host);
12391c6fdbd8SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
12401c6fdbd8SKent Overstreet 	struct bch_read_bio *rbio;
124101ad6737SKent Overstreet 	struct bch_io_opts opts;
12421c6fdbd8SKent Overstreet 	int ret;
12431c6fdbd8SKent Overstreet 	DECLARE_COMPLETION_ONSTACK(done);
12441c6fdbd8SKent Overstreet 
124501ad6737SKent Overstreet 	bch2_inode_opts_get(&opts, c, &inode->ei_inode);
124601ad6737SKent Overstreet 
12471c6fdbd8SKent Overstreet 	rbio = rbio_init(bio_alloc_bioset(NULL, 1, REQ_OP_READ, GFP_NOFS, &c->bio_read),
124801ad6737SKent Overstreet 			 opts);
12491c6fdbd8SKent Overstreet 	rbio->bio.bi_private = &done;
12501c6fdbd8SKent Overstreet 	rbio->bio.bi_end_io = bch2_read_single_page_end_io;
12511c6fdbd8SKent Overstreet 
12528c6d298aSKent Overstreet 	__bchfs_readpage(c, rbio, inode_inum(inode), page);
12531c6fdbd8SKent Overstreet 	wait_for_completion(&done);
12541c6fdbd8SKent Overstreet 
12551c6fdbd8SKent Overstreet 	ret = blk_status_to_errno(rbio->bio.bi_status);
12561c6fdbd8SKent Overstreet 	bio_put(&rbio->bio);
12571c6fdbd8SKent Overstreet 
12581c6fdbd8SKent Overstreet 	if (ret < 0)
12591c6fdbd8SKent Overstreet 		return ret;
12601c6fdbd8SKent Overstreet 
12611c6fdbd8SKent Overstreet 	SetPageUptodate(page);
12621c6fdbd8SKent Overstreet 	return 0;
12631c6fdbd8SKent Overstreet }
12641c6fdbd8SKent Overstreet 
12651c6fdbd8SKent Overstreet int bch2_read_folio(struct file *file, struct folio *folio)
12661c6fdbd8SKent Overstreet {
12671c6fdbd8SKent Overstreet 	struct page *page = &folio->page;
12681c6fdbd8SKent Overstreet 	int ret;
12691c6fdbd8SKent Overstreet 
12701c6fdbd8SKent Overstreet 	ret = bch2_read_single_page(page, page->mapping);
12711c6fdbd8SKent Overstreet 	folio_unlock(folio);
12725c1ef830SKent Overstreet 	return bch2_err_class(ret);
12731c6fdbd8SKent Overstreet }
12741c6fdbd8SKent Overstreet 
12751c6fdbd8SKent Overstreet /* writepages: */
12761c6fdbd8SKent Overstreet 
12771c6fdbd8SKent Overstreet struct bch_writepage_state {
12781c6fdbd8SKent Overstreet 	struct bch_writepage_io	*io;
12791c6fdbd8SKent Overstreet 	struct bch_io_opts	opts;
12801c6fdbd8SKent Overstreet };
12811c6fdbd8SKent Overstreet 
12821c6fdbd8SKent Overstreet static inline struct bch_writepage_state bch_writepage_state_init(struct bch_fs *c,
12831c6fdbd8SKent Overstreet 								  struct bch_inode_info *inode)
12841c6fdbd8SKent Overstreet {
128501ad6737SKent Overstreet 	struct bch_writepage_state ret = { 0 };
128601ad6737SKent Overstreet 
128701ad6737SKent Overstreet 	bch2_inode_opts_get(&ret.opts, c, &inode->ei_inode);
128801ad6737SKent Overstreet 	return ret;
12891c6fdbd8SKent Overstreet }
12901c6fdbd8SKent Overstreet 
12919f311f21SKent Overstreet static void bch2_writepage_io_done(struct bch_write_op *op)
12921c6fdbd8SKent Overstreet {
12939f311f21SKent Overstreet 	struct bch_writepage_io *io =
12949f311f21SKent Overstreet 		container_of(op, struct bch_writepage_io, op);
12959a3df993SKent Overstreet 	struct bch_fs *c = io->op.c;
12969a3df993SKent Overstreet 	struct bio *bio = &io->op.wbio.bio;
12971c6fdbd8SKent Overstreet 	struct bvec_iter_all iter;
12981c6fdbd8SKent Overstreet 	struct bio_vec *bvec;
1299b3fce09cSKent Overstreet 	unsigned i;
13001c6fdbd8SKent Overstreet 
13019a3df993SKent Overstreet 	if (io->op.error) {
130233c74e41SKent Overstreet 		set_bit(EI_INODE_ERROR, &io->inode->ei_flags);
130333c74e41SKent Overstreet 
130475812e70SKent Overstreet 		bio_for_each_segment_all(bvec, bio, iter) {
1305b3fce09cSKent Overstreet 			struct bch_page_state *s;
1306b3fce09cSKent Overstreet 
13071c6fdbd8SKent Overstreet 			SetPageError(bvec->bv_page);
130875812e70SKent Overstreet 			mapping_set_error(bvec->bv_page->mapping, -EIO);
1309b3fce09cSKent Overstreet 
13103826ee0bSKent Overstreet 			s = __bch2_page_state(bvec->bv_page);
13113826ee0bSKent Overstreet 			spin_lock(&s->lock);
1312b3fce09cSKent Overstreet 			for (i = 0; i < PAGE_SECTORS; i++)
1313b3fce09cSKent Overstreet 				s->s[i].nr_replicas = 0;
13143826ee0bSKent Overstreet 			spin_unlock(&s->lock);
131575812e70SKent Overstreet 		}
13161c6fdbd8SKent Overstreet 	}
13171c6fdbd8SKent Overstreet 
13184be1a412SKent Overstreet 	if (io->op.flags & BCH_WRITE_WROTE_DATA_INLINE) {
13194be1a412SKent Overstreet 		bio_for_each_segment_all(bvec, bio, iter) {
13204be1a412SKent Overstreet 			struct bch_page_state *s;
13214be1a412SKent Overstreet 
13224be1a412SKent Overstreet 			s = __bch2_page_state(bvec->bv_page);
13234be1a412SKent Overstreet 			spin_lock(&s->lock);
13244be1a412SKent Overstreet 			for (i = 0; i < PAGE_SECTORS; i++)
13254be1a412SKent Overstreet 				s->s[i].nr_replicas = 0;
13264be1a412SKent Overstreet 			spin_unlock(&s->lock);
13274be1a412SKent Overstreet 		}
13284be1a412SKent Overstreet 	}
13294be1a412SKent Overstreet 
13301c6fdbd8SKent Overstreet 	/*
13311c6fdbd8SKent Overstreet 	 * racing with fallocate can cause us to add fewer sectors than
13321c6fdbd8SKent Overstreet 	 * expected - but we shouldn't add more sectors than expected:
13331c6fdbd8SKent Overstreet 	 */
1334f8494d25SKent Overstreet 	WARN_ON_ONCE(io->op.i_sectors_delta > 0);
13351c6fdbd8SKent Overstreet 
13361c6fdbd8SKent Overstreet 	/*
13371c6fdbd8SKent Overstreet 	 * (error (due to going RO) halfway through a page can screw that up
13381c6fdbd8SKent Overstreet 	 * slightly)
13391c6fdbd8SKent Overstreet 	 * XXX wtf?
13409a3df993SKent Overstreet 	   BUG_ON(io->op.op.i_sectors_delta >= PAGE_SECTORS);
13411c6fdbd8SKent Overstreet 	 */
13421c6fdbd8SKent Overstreet 
13431c6fdbd8SKent Overstreet 	/*
13441c6fdbd8SKent Overstreet 	 * PageWriteback is effectively our ref on the inode - fixup i_blocks
13451c6fdbd8SKent Overstreet 	 * before calling end_page_writeback:
13461c6fdbd8SKent Overstreet 	 */
13479a3df993SKent Overstreet 	i_sectors_acct(c, io->inode, NULL, io->op.i_sectors_delta);
13481c6fdbd8SKent Overstreet 
13497f5e31e1SKent Overstreet 	bio_for_each_segment_all(bvec, bio, iter) {
13507f5e31e1SKent Overstreet 		struct bch_page_state *s = __bch2_page_state(bvec->bv_page);
13517f5e31e1SKent Overstreet 
13527f5e31e1SKent Overstreet 		if (atomic_dec_and_test(&s->write_count))
13531c6fdbd8SKent Overstreet 			end_page_writeback(bvec->bv_page);
13547f5e31e1SKent Overstreet 	}
13551c6fdbd8SKent Overstreet 
13569f311f21SKent Overstreet 	bio_put(&io->op.wbio.bio);
13571c6fdbd8SKent Overstreet }
13581c6fdbd8SKent Overstreet 
13591c6fdbd8SKent Overstreet static void bch2_writepage_do_io(struct bch_writepage_state *w)
13601c6fdbd8SKent Overstreet {
13611c6fdbd8SKent Overstreet 	struct bch_writepage_io *io = w->io;
13621c6fdbd8SKent Overstreet 
13631c6fdbd8SKent Overstreet 	w->io = NULL;
13649f311f21SKent Overstreet 	closure_call(&io->op.cl, bch2_write, NULL, NULL);
13651c6fdbd8SKent Overstreet }
13661c6fdbd8SKent Overstreet 
13671c6fdbd8SKent Overstreet /*
13681c6fdbd8SKent Overstreet  * Get a bch_writepage_io and add @page to it - appending to an existing one if
13691c6fdbd8SKent Overstreet  * possible, else allocating a new one:
13701c6fdbd8SKent Overstreet  */
13711c6fdbd8SKent Overstreet static void bch2_writepage_io_alloc(struct bch_fs *c,
137250fe5bd6SKent Overstreet 				    struct writeback_control *wbc,
13731c6fdbd8SKent Overstreet 				    struct bch_writepage_state *w,
13741c6fdbd8SKent Overstreet 				    struct bch_inode_info *inode,
13757f5e31e1SKent Overstreet 				    u64 sector,
13761c6fdbd8SKent Overstreet 				    unsigned nr_replicas)
13771c6fdbd8SKent Overstreet {
13781c6fdbd8SKent Overstreet 	struct bch_write_op *op;
13791c6fdbd8SKent Overstreet 
13801c6fdbd8SKent Overstreet 	w->io = container_of(bio_alloc_bioset(NULL, BIO_MAX_VECS,
13811c6fdbd8SKent Overstreet 					      REQ_OP_WRITE,
13821c6fdbd8SKent Overstreet 					      GFP_NOFS,
13831c6fdbd8SKent Overstreet 					      &c->writepage_bioset),
13849a3df993SKent Overstreet 			     struct bch_writepage_io, op.wbio.bio);
13851c6fdbd8SKent Overstreet 
13869a3df993SKent Overstreet 	w->io->inode		= inode;
13879a3df993SKent Overstreet 	op			= &w->io->op;
13889a3df993SKent Overstreet 	bch2_write_op_init(op, c, w->opts);
13899a3df993SKent Overstreet 	op->target		= w->opts.foreground_target;
13901c6fdbd8SKent Overstreet 	op->nr_replicas		= nr_replicas;
13911c6fdbd8SKent Overstreet 	op->res.nr_replicas	= nr_replicas;
13921c6fdbd8SKent Overstreet 	op->write_point		= writepoint_hashed(inode->ei_last_dirtied);
13938c6d298aSKent Overstreet 	op->subvol		= inode->ei_subvol;
13947f5e31e1SKent Overstreet 	op->pos			= POS(inode->v.i_ino, sector);
13959f311f21SKent Overstreet 	op->end_io		= bch2_writepage_io_done;
1396a8b3a677SKent Overstreet 	op->devs_need_flush	= &inode->ei_devs_need_flush;
13977f5e31e1SKent Overstreet 	op->wbio.bio.bi_iter.bi_sector = sector;
139850fe5bd6SKent Overstreet 	op->wbio.bio.bi_opf	= wbc_to_write_flags(wbc);
13991c6fdbd8SKent Overstreet }
14001c6fdbd8SKent Overstreet 
14011c6fdbd8SKent Overstreet static int __bch2_writepage(struct folio *folio,
14021c6fdbd8SKent Overstreet 			    struct writeback_control *wbc,
14031c6fdbd8SKent Overstreet 			    void *data)
14041c6fdbd8SKent Overstreet {
14051c6fdbd8SKent Overstreet 	struct page *page = &folio->page;
14061c6fdbd8SKent Overstreet 	struct bch_inode_info *inode = to_bch_ei(page->mapping->host);
14071c6fdbd8SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
14081c6fdbd8SKent Overstreet 	struct bch_writepage_state *w = data;
14097f5e31e1SKent Overstreet 	struct bch_page_state *s, orig;
14107f5e31e1SKent Overstreet 	unsigned i, offset, nr_replicas_this_write = U32_MAX;
14111c6fdbd8SKent Overstreet 	loff_t i_size = i_size_read(&inode->v);
14121c6fdbd8SKent Overstreet 	pgoff_t end_index = i_size >> PAGE_SHIFT;
1413e1036a2aSKent Overstreet 	int ret;
14141c6fdbd8SKent Overstreet 
14151c6fdbd8SKent Overstreet 	EBUG_ON(!PageUptodate(page));
14161c6fdbd8SKent Overstreet 
14171c6fdbd8SKent Overstreet 	/* Is the page fully inside i_size? */
14181c6fdbd8SKent Overstreet 	if (page->index < end_index)
14191c6fdbd8SKent Overstreet 		goto do_io;
14201c6fdbd8SKent Overstreet 
14211c6fdbd8SKent Overstreet 	/* Is the page fully outside i_size? (truncate in progress) */
14221c6fdbd8SKent Overstreet 	offset = i_size & (PAGE_SIZE - 1);
14231c6fdbd8SKent Overstreet 	if (page->index > end_index || !offset) {
14241c6fdbd8SKent Overstreet 		unlock_page(page);
14251c6fdbd8SKent Overstreet 		return 0;
14261c6fdbd8SKent Overstreet 	}
14271c6fdbd8SKent Overstreet 
14281c6fdbd8SKent Overstreet 	/*
14291c6fdbd8SKent Overstreet 	 * The page straddles i_size.  It must be zeroed out on each and every
14301c6fdbd8SKent Overstreet 	 * writepage invocation because it may be mmapped.  "A file is mapped
14311c6fdbd8SKent Overstreet 	 * in multiples of the page size.  For a file that is not a multiple of
14321c6fdbd8SKent Overstreet 	 * the  page size, the remaining memory is zeroed when mapped, and
14331c6fdbd8SKent Overstreet 	 * writes to that region are not written out to the file."
14341c6fdbd8SKent Overstreet 	 */
14351c6fdbd8SKent Overstreet 	zero_user_segment(page, offset, PAGE_SIZE);
14361c6fdbd8SKent Overstreet do_io:
1437f57a6a5dSKent Overstreet 	s = bch2_page_state_create(page, __GFP_NOFAIL);
1438f81b648dSKent Overstreet 
1439f74a5051SKent Overstreet 	/*
1440f74a5051SKent Overstreet 	 * Things get really hairy with errors during writeback:
1441f74a5051SKent Overstreet 	 */
1442f74a5051SKent Overstreet 	ret = bch2_get_page_disk_reservation(c, inode, page, false);
1443f74a5051SKent Overstreet 	BUG_ON(ret);
1444e1036a2aSKent Overstreet 
14457f5e31e1SKent Overstreet 	/* Before unlocking the page, get copy of reservations: */
1446f74a5051SKent Overstreet 	spin_lock(&s->lock);
14477f5e31e1SKent Overstreet 	orig = *s;
1448f74a5051SKent Overstreet 	spin_unlock(&s->lock);
14497f5e31e1SKent Overstreet 
14507f5e31e1SKent Overstreet 	for (i = 0; i < PAGE_SECTORS; i++) {
14512ba5d38bSKent Overstreet 		if (s->s[i].state < SECTOR_DIRTY)
14527f5e31e1SKent Overstreet 			continue;
14537f5e31e1SKent Overstreet 
1454f81b648dSKent Overstreet 		nr_replicas_this_write =
1455f57a6a5dSKent Overstreet 			min_t(unsigned, nr_replicas_this_write,
1456f57a6a5dSKent Overstreet 			      s->s[i].nr_replicas +
1457f57a6a5dSKent Overstreet 			      s->s[i].replicas_reserved);
14587f5e31e1SKent Overstreet 	}
14591c6fdbd8SKent Overstreet 
1460f57a6a5dSKent Overstreet 	for (i = 0; i < PAGE_SECTORS; i++) {
14612ba5d38bSKent Overstreet 		if (s->s[i].state < SECTOR_DIRTY)
14627f5e31e1SKent Overstreet 			continue;
14637f5e31e1SKent Overstreet 
1464f57a6a5dSKent Overstreet 		s->s[i].nr_replicas = w->opts.compression
1465f57a6a5dSKent Overstreet 			? 0 : nr_replicas_this_write;
1466e1036a2aSKent Overstreet 
1467f57a6a5dSKent Overstreet 		s->s[i].replicas_reserved = 0;
1468f57a6a5dSKent Overstreet 		s->s[i].state = SECTOR_ALLOCATED;
1469f57a6a5dSKent Overstreet 	}
14701c6fdbd8SKent Overstreet 
14717f5e31e1SKent Overstreet 	BUG_ON(atomic_read(&s->write_count));
14727f5e31e1SKent Overstreet 	atomic_set(&s->write_count, 1);
14737f5e31e1SKent Overstreet 
14741c6fdbd8SKent Overstreet 	BUG_ON(PageWriteback(page));
14751c6fdbd8SKent Overstreet 	set_page_writeback(page);
14767f5e31e1SKent Overstreet 
14771c6fdbd8SKent Overstreet 	unlock_page(page);
14781c6fdbd8SKent Overstreet 
14797f5e31e1SKent Overstreet 	offset = 0;
14807f5e31e1SKent Overstreet 	while (1) {
1481f74a5051SKent Overstreet 		unsigned sectors = 0, dirty_sectors = 0, reserved_sectors = 0;
14827f5e31e1SKent Overstreet 		u64 sector;
14837f5e31e1SKent Overstreet 
14847f5e31e1SKent Overstreet 		while (offset < PAGE_SECTORS &&
14852ba5d38bSKent Overstreet 		       orig.s[offset].state < SECTOR_DIRTY)
14867f5e31e1SKent Overstreet 			offset++;
14877f5e31e1SKent Overstreet 
14887f5e31e1SKent Overstreet 		if (offset == PAGE_SECTORS)
14897f5e31e1SKent Overstreet 			break;
14907f5e31e1SKent Overstreet 
14917f5e31e1SKent Overstreet 		while (offset + sectors < PAGE_SECTORS &&
1492f74a5051SKent Overstreet 		       orig.s[offset + sectors].state >= SECTOR_DIRTY) {
1493f74a5051SKent Overstreet 			reserved_sectors += orig.s[offset + sectors].replicas_reserved;
1494f74a5051SKent Overstreet 			dirty_sectors += orig.s[offset + sectors].state == SECTOR_DIRTY;
14957f5e31e1SKent Overstreet 			sectors++;
14967f5e31e1SKent Overstreet 		}
1497f74a5051SKent Overstreet 		BUG_ON(!sectors);
1498f74a5051SKent Overstreet 
14997279c1a2SKent Overstreet 		sector = ((u64) page->index << PAGE_SECTORS_SHIFT) + offset;
15007f5e31e1SKent Overstreet 
15011c6fdbd8SKent Overstreet 		if (w->io &&
15029a3df993SKent Overstreet 		    (w->io->op.res.nr_replicas != nr_replicas_this_write ||
15039a3df993SKent Overstreet 		     bio_full(&w->io->op.wbio.bio, PAGE_SIZE) ||
1504f59b3464SKent Overstreet 		     w->io->op.wbio.bio.bi_iter.bi_size + (sectors << 9) >=
1505f59b3464SKent Overstreet 		     (BIO_MAX_VECS * PAGE_SIZE) ||
15069a3df993SKent Overstreet 		     bio_end_sector(&w->io->op.wbio.bio) != sector))
15071c6fdbd8SKent Overstreet 			bch2_writepage_do_io(w);
15081c6fdbd8SKent Overstreet 
15091c6fdbd8SKent Overstreet 		if (!w->io)
151050fe5bd6SKent Overstreet 			bch2_writepage_io_alloc(c, wbc, w, inode, sector,
1511f81b648dSKent Overstreet 						nr_replicas_this_write);
15121c6fdbd8SKent Overstreet 
15137f5e31e1SKent Overstreet 		atomic_inc(&s->write_count);
15147f5e31e1SKent Overstreet 
15159a3df993SKent Overstreet 		BUG_ON(inode != w->io->inode);
15169a3df993SKent Overstreet 		BUG_ON(!bio_add_page(&w->io->op.wbio.bio, page,
15177f5e31e1SKent Overstreet 				     sectors << 9, offset << 9));
15181c6fdbd8SKent Overstreet 
15196cc3535dSKent Overstreet 		/* Check for writing past i_size: */
15208eb71e9eSKent Overstreet 		WARN_ONCE((bio_end_sector(&w->io->op.wbio.bio) << 9) >
152180fe580cSKent Overstreet 			  round_up(i_size, block_bytes(c)) &&
15228eb71e9eSKent Overstreet 			  !test_bit(BCH_FS_EMERGENCY_RO, &c->flags),
15238eb71e9eSKent Overstreet 			  "writing past i_size: %llu > %llu (unrounded %llu)\n",
15248eb71e9eSKent Overstreet 			  bio_end_sector(&w->io->op.wbio.bio) << 9,
15258eb71e9eSKent Overstreet 			  round_up(i_size, block_bytes(c)),
15268eb71e9eSKent Overstreet 			  i_size);
15276cc3535dSKent Overstreet 
15289a3df993SKent Overstreet 		w->io->op.res.sectors += reserved_sectors;
15299a3df993SKent Overstreet 		w->io->op.i_sectors_delta -= dirty_sectors;
15301c6fdbd8SKent Overstreet 		w->io->op.new_i_size = i_size;
15311c6fdbd8SKent Overstreet 
15327f5e31e1SKent Overstreet 		offset += sectors;
15337f5e31e1SKent Overstreet 	}
15347f5e31e1SKent Overstreet 
15357f5e31e1SKent Overstreet 	if (atomic_dec_and_test(&s->write_count))
15367f5e31e1SKent Overstreet 		end_page_writeback(page);
15377f5e31e1SKent Overstreet 
15381c6fdbd8SKent Overstreet 	return 0;
15391c6fdbd8SKent Overstreet }
15401c6fdbd8SKent Overstreet 
15411c6fdbd8SKent Overstreet int bch2_writepages(struct address_space *mapping, struct writeback_control *wbc)
15421c6fdbd8SKent Overstreet {
15431c6fdbd8SKent Overstreet 	struct bch_fs *c = mapping->host->i_sb->s_fs_info;
15441c6fdbd8SKent Overstreet 	struct bch_writepage_state w =
15451c6fdbd8SKent Overstreet 		bch_writepage_state_init(c, to_bch_ei(mapping->host));
15461c6fdbd8SKent Overstreet 	struct blk_plug plug;
15471c6fdbd8SKent Overstreet 	int ret;
15481c6fdbd8SKent Overstreet 
15491c6fdbd8SKent Overstreet 	blk_start_plug(&plug);
15501c6fdbd8SKent Overstreet 	ret = write_cache_pages(mapping, wbc, __bch2_writepage, &w);
15511c6fdbd8SKent Overstreet 	if (w.io)
15521c6fdbd8SKent Overstreet 		bch2_writepage_do_io(&w);
15531c6fdbd8SKent Overstreet 	blk_finish_plug(&plug);
15545c1ef830SKent Overstreet 	return bch2_err_class(ret);
15551c6fdbd8SKent Overstreet }
15561c6fdbd8SKent Overstreet 
15571c6fdbd8SKent Overstreet /* buffered writes: */
15581c6fdbd8SKent Overstreet 
15591c6fdbd8SKent Overstreet int bch2_write_begin(struct file *file, struct address_space *mapping,
15601c6fdbd8SKent Overstreet 		     loff_t pos, unsigned len,
15611c6fdbd8SKent Overstreet 		     struct page **pagep, void **fsdata)
15621c6fdbd8SKent Overstreet {
15631c6fdbd8SKent Overstreet 	struct bch_inode_info *inode = to_bch_ei(mapping->host);
15641c6fdbd8SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
1565d1542e03SKent Overstreet 	struct bch2_page_reservation *res;
15661c6fdbd8SKent Overstreet 	pgoff_t index = pos >> PAGE_SHIFT;
15671c6fdbd8SKent Overstreet 	unsigned offset = pos & (PAGE_SIZE - 1);
15681c6fdbd8SKent Overstreet 	struct page *page;
15691c6fdbd8SKent Overstreet 	int ret = -ENOMEM;
15701c6fdbd8SKent Overstreet 
1571d1542e03SKent Overstreet 	res = kmalloc(sizeof(*res), GFP_KERNEL);
1572d1542e03SKent Overstreet 	if (!res)
1573d1542e03SKent Overstreet 		return -ENOMEM;
1574d1542e03SKent Overstreet 
1575d1542e03SKent Overstreet 	bch2_page_reservation_init(c, inode, res);
1576d1542e03SKent Overstreet 	*fsdata = res;
15771c6fdbd8SKent Overstreet 
1578a7ecd30cSKent Overstreet 	bch2_pagecache_add_get(inode);
15791c6fdbd8SKent Overstreet 
15801c6fdbd8SKent Overstreet 	page = grab_cache_page_write_begin(mapping, index);
15811c6fdbd8SKent Overstreet 	if (!page)
15821c6fdbd8SKent Overstreet 		goto err_unlock;
15831c6fdbd8SKent Overstreet 
15841c6fdbd8SKent Overstreet 	if (PageUptodate(page))
15851c6fdbd8SKent Overstreet 		goto out;
15861c6fdbd8SKent Overstreet 
15871c6fdbd8SKent Overstreet 	/* If we're writing entire page, don't need to read it in first: */
15881c6fdbd8SKent Overstreet 	if (len == PAGE_SIZE)
15891c6fdbd8SKent Overstreet 		goto out;
15901c6fdbd8SKent Overstreet 
15911c6fdbd8SKent Overstreet 	if (!offset && pos + len >= inode->v.i_size) {
15921c6fdbd8SKent Overstreet 		zero_user_segment(page, len, PAGE_SIZE);
15931c6fdbd8SKent Overstreet 		flush_dcache_page(page);
15941c6fdbd8SKent Overstreet 		goto out;
15951c6fdbd8SKent Overstreet 	}
15961c6fdbd8SKent Overstreet 
15971c6fdbd8SKent Overstreet 	if (index > inode->v.i_size >> PAGE_SHIFT) {
15981c6fdbd8SKent Overstreet 		zero_user_segments(page, 0, offset, offset + len, PAGE_SIZE);
15991c6fdbd8SKent Overstreet 		flush_dcache_page(page);
16001c6fdbd8SKent Overstreet 		goto out;
16011c6fdbd8SKent Overstreet 	}
16021c6fdbd8SKent Overstreet readpage:
16031c6fdbd8SKent Overstreet 	ret = bch2_read_single_page(page, mapping);
16041c6fdbd8SKent Overstreet 	if (ret)
16051c6fdbd8SKent Overstreet 		goto err;
16061c6fdbd8SKent Overstreet out:
1607e6ec361fSKent Overstreet 	if (!bch2_page_state_create(page, __GFP_NOFAIL)->uptodate) {
1608e6ec361fSKent Overstreet 		ret = bch2_page_state_set(c, inode_inum(inode), &page, 1);
1609e6ec361fSKent Overstreet 		if (ret)
16103a4d3656SKent Overstreet 			goto err;
1611e6ec361fSKent Overstreet 	}
1612e6ec361fSKent Overstreet 
1613bd954215SKent Overstreet 	ret = bch2_page_reservation_get(c, inode, page, res, offset, len);
16141c6fdbd8SKent Overstreet 	if (ret) {
16151c6fdbd8SKent Overstreet 		if (!PageUptodate(page)) {
16161c6fdbd8SKent Overstreet 			/*
16171c6fdbd8SKent Overstreet 			 * If the page hasn't been read in, we won't know if we
16181c6fdbd8SKent Overstreet 			 * actually need a reservation - we don't actually need
16191c6fdbd8SKent Overstreet 			 * to read here, we just need to check if the page is
16201c6fdbd8SKent Overstreet 			 * fully backed by uncompressed data:
16211c6fdbd8SKent Overstreet 			 */
16221c6fdbd8SKent Overstreet 			goto readpage;
16231c6fdbd8SKent Overstreet 		}
16241c6fdbd8SKent Overstreet 
16251c6fdbd8SKent Overstreet 		goto err;
16261c6fdbd8SKent Overstreet 	}
16271c6fdbd8SKent Overstreet 
16281c6fdbd8SKent Overstreet 	*pagep = page;
16291c6fdbd8SKent Overstreet 	return 0;
16301c6fdbd8SKent Overstreet err:
16311c6fdbd8SKent Overstreet 	unlock_page(page);
16321c6fdbd8SKent Overstreet 	put_page(page);
16331c6fdbd8SKent Overstreet 	*pagep = NULL;
16341c6fdbd8SKent Overstreet err_unlock:
1635a7ecd30cSKent Overstreet 	bch2_pagecache_add_put(inode);
1636d1542e03SKent Overstreet 	kfree(res);
1637d1542e03SKent Overstreet 	*fsdata = NULL;
16385c1ef830SKent Overstreet 	return bch2_err_class(ret);
16391c6fdbd8SKent Overstreet }
16401c6fdbd8SKent Overstreet 
16411c6fdbd8SKent Overstreet int bch2_write_end(struct file *file, struct address_space *mapping,
16421c6fdbd8SKent Overstreet 		   loff_t pos, unsigned len, unsigned copied,
16431c6fdbd8SKent Overstreet 		   struct page *page, void *fsdata)
16441c6fdbd8SKent Overstreet {
16451c6fdbd8SKent Overstreet 	struct bch_inode_info *inode = to_bch_ei(mapping->host);
16461c6fdbd8SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
1647d1542e03SKent Overstreet 	struct bch2_page_reservation *res = fsdata;
1648d1542e03SKent Overstreet 	unsigned offset = pos & (PAGE_SIZE - 1);
16491c6fdbd8SKent Overstreet 
16501c6fdbd8SKent Overstreet 	lockdep_assert_held(&inode->v.i_rwsem);
16511c6fdbd8SKent Overstreet 
16521c6fdbd8SKent Overstreet 	if (unlikely(copied < len && !PageUptodate(page))) {
16531c6fdbd8SKent Overstreet 		/*
16541c6fdbd8SKent Overstreet 		 * The page needs to be read in, but that would destroy
16551c6fdbd8SKent Overstreet 		 * our partial write - simplest thing is to just force
16561c6fdbd8SKent Overstreet 		 * userspace to redo the write:
16571c6fdbd8SKent Overstreet 		 */
16581c6fdbd8SKent Overstreet 		zero_user(page, 0, PAGE_SIZE);
16591c6fdbd8SKent Overstreet 		flush_dcache_page(page);
16601c6fdbd8SKent Overstreet 		copied = 0;
16611c6fdbd8SKent Overstreet 	}
16621c6fdbd8SKent Overstreet 
16631c6fdbd8SKent Overstreet 	spin_lock(&inode->v.i_lock);
16641c6fdbd8SKent Overstreet 	if (pos + copied > inode->v.i_size)
16651c6fdbd8SKent Overstreet 		i_size_write(&inode->v, pos + copied);
16661c6fdbd8SKent Overstreet 	spin_unlock(&inode->v.i_lock);
16671c6fdbd8SKent Overstreet 
16681c6fdbd8SKent Overstreet 	if (copied) {
16691c6fdbd8SKent Overstreet 		if (!PageUptodate(page))
16701c6fdbd8SKent Overstreet 			SetPageUptodate(page);
1671d1542e03SKent Overstreet 
1672d1542e03SKent Overstreet 		bch2_set_page_dirty(c, inode, page, res, offset, copied);
16731c6fdbd8SKent Overstreet 
16741c6fdbd8SKent Overstreet 		inode->ei_last_dirtied = (unsigned long) current;
16751c6fdbd8SKent Overstreet 	}
16761c6fdbd8SKent Overstreet 
16771c6fdbd8SKent Overstreet 	unlock_page(page);
16781c6fdbd8SKent Overstreet 	put_page(page);
1679a7ecd30cSKent Overstreet 	bch2_pagecache_add_put(inode);
16801c6fdbd8SKent Overstreet 
1681d1542e03SKent Overstreet 	bch2_page_reservation_put(c, inode, res);
1682d1542e03SKent Overstreet 	kfree(res);
1683d1542e03SKent Overstreet 
16841c6fdbd8SKent Overstreet 	return copied;
16851c6fdbd8SKent Overstreet }
16861c6fdbd8SKent Overstreet 
16871c6fdbd8SKent Overstreet #define WRITE_BATCH_PAGES	32
16881c6fdbd8SKent Overstreet 
16891c6fdbd8SKent Overstreet static int __bch2_buffered_write(struct bch_inode_info *inode,
16901c6fdbd8SKent Overstreet 				 struct address_space *mapping,
16911c6fdbd8SKent Overstreet 				 struct iov_iter *iter,
16921c6fdbd8SKent Overstreet 				 loff_t pos, unsigned len)
16931c6fdbd8SKent Overstreet {
16941c6fdbd8SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
16951c6fdbd8SKent Overstreet 	struct page *pages[WRITE_BATCH_PAGES];
1696d1542e03SKent Overstreet 	struct bch2_page_reservation res;
16971c6fdbd8SKent Overstreet 	unsigned long index = pos >> PAGE_SHIFT;
16981c6fdbd8SKent Overstreet 	unsigned offset = pos & (PAGE_SIZE - 1);
16991c6fdbd8SKent Overstreet 	unsigned nr_pages = DIV_ROUND_UP(offset + len, PAGE_SIZE);
1700d1542e03SKent Overstreet 	unsigned i, reserved = 0, set_dirty = 0;
1701d1542e03SKent Overstreet 	unsigned copied = 0, nr_pages_copied = 0;
17021c6fdbd8SKent Overstreet 	int ret = 0;
17031c6fdbd8SKent Overstreet 
17041c6fdbd8SKent Overstreet 	BUG_ON(!len);
17051c6fdbd8SKent Overstreet 	BUG_ON(nr_pages > ARRAY_SIZE(pages));
17061c6fdbd8SKent Overstreet 
1707d1542e03SKent Overstreet 	bch2_page_reservation_init(c, inode, &res);
1708d1542e03SKent Overstreet 
17091c6fdbd8SKent Overstreet 	for (i = 0; i < nr_pages; i++) {
17101c6fdbd8SKent Overstreet 		pages[i] = grab_cache_page_write_begin(mapping, index + i);
17111c6fdbd8SKent Overstreet 		if (!pages[i]) {
17121c6fdbd8SKent Overstreet 			nr_pages = i;
17138de819f8SKent Overstreet 			if (!i) {
17141c6fdbd8SKent Overstreet 				ret = -ENOMEM;
17151c6fdbd8SKent Overstreet 				goto out;
17161c6fdbd8SKent Overstreet 			}
17178de819f8SKent Overstreet 			len = min_t(unsigned, len,
17188de819f8SKent Overstreet 				    nr_pages * PAGE_SIZE - offset);
17198de819f8SKent Overstreet 			break;
17208de819f8SKent Overstreet 		}
17211c6fdbd8SKent Overstreet 	}
17221c6fdbd8SKent Overstreet 
17231c6fdbd8SKent Overstreet 	if (offset && !PageUptodate(pages[0])) {
17241c6fdbd8SKent Overstreet 		ret = bch2_read_single_page(pages[0], mapping);
17251c6fdbd8SKent Overstreet 		if (ret)
17261c6fdbd8SKent Overstreet 			goto out;
17271c6fdbd8SKent Overstreet 	}
17281c6fdbd8SKent Overstreet 
17291c6fdbd8SKent Overstreet 	if ((pos + len) & (PAGE_SIZE - 1) &&
17301c6fdbd8SKent Overstreet 	    !PageUptodate(pages[nr_pages - 1])) {
17311c6fdbd8SKent Overstreet 		if ((index + nr_pages - 1) << PAGE_SHIFT >= inode->v.i_size) {
17321c6fdbd8SKent Overstreet 			zero_user(pages[nr_pages - 1], 0, PAGE_SIZE);
17331c6fdbd8SKent Overstreet 		} else {
17341c6fdbd8SKent Overstreet 			ret = bch2_read_single_page(pages[nr_pages - 1], mapping);
17351c6fdbd8SKent Overstreet 			if (ret)
17361c6fdbd8SKent Overstreet 				goto out;
17371c6fdbd8SKent Overstreet 		}
17381c6fdbd8SKent Overstreet 	}
17391c6fdbd8SKent Overstreet 
1740d1542e03SKent Overstreet 	while (reserved < len) {
1741e6ec361fSKent Overstreet 		unsigned i = (offset + reserved) >> PAGE_SHIFT;
1742e6ec361fSKent Overstreet 		struct page *page = pages[i];
1743d1542e03SKent Overstreet 		unsigned pg_offset = (offset + reserved) & (PAGE_SIZE - 1);
1744d1542e03SKent Overstreet 		unsigned pg_len = min_t(unsigned, len - reserved,
1745d1542e03SKent Overstreet 					PAGE_SIZE - pg_offset);
17461c6fdbd8SKent Overstreet 
1747e6ec361fSKent Overstreet 		if (!bch2_page_state_create(page, __GFP_NOFAIL)->uptodate) {
1748e6ec361fSKent Overstreet 			ret = bch2_page_state_set(c, inode_inum(inode),
1749e6ec361fSKent Overstreet 						  pages + i, nr_pages - i);
1750e6ec361fSKent Overstreet 			if (ret)
1751e6ec361fSKent Overstreet 				goto out;
17521c6fdbd8SKent Overstreet 		}
17531c6fdbd8SKent Overstreet 
1754353448f3SKent Overstreet 		/*
1755353448f3SKent Overstreet 		 * XXX: per POSIX and fstests generic/275, on -ENOSPC we're
1756353448f3SKent Overstreet 		 * supposed to write as much as we have disk space for.
1757353448f3SKent Overstreet 		 *
1758353448f3SKent Overstreet 		 * On failure here we should still write out a partial page if
1759353448f3SKent Overstreet 		 * we aren't completely out of disk space - we don't do that
1760353448f3SKent Overstreet 		 * yet:
1761353448f3SKent Overstreet 		 */
1762e6ec361fSKent Overstreet 		ret = bch2_page_reservation_get(c, inode, page, &res,
1763bd954215SKent Overstreet 						pg_offset, pg_len);
1764353448f3SKent Overstreet 		if (unlikely(ret)) {
1765353448f3SKent Overstreet 			if (!reserved)
17661c6fdbd8SKent Overstreet 				goto out;
1767353448f3SKent Overstreet 			break;
1768353448f3SKent Overstreet 		}
1769d1542e03SKent Overstreet 
1770d1542e03SKent Overstreet 		reserved += pg_len;
17711c6fdbd8SKent Overstreet 	}
17721c6fdbd8SKent Overstreet 
17731c6fdbd8SKent Overstreet 	if (mapping_writably_mapped(mapping))
17741c6fdbd8SKent Overstreet 		for (i = 0; i < nr_pages; i++)
17751c6fdbd8SKent Overstreet 			flush_dcache_page(pages[i]);
17761c6fdbd8SKent Overstreet 
1777353448f3SKent Overstreet 	while (copied < reserved) {
17781c6fdbd8SKent Overstreet 		struct page *page = pages[(offset + copied) >> PAGE_SHIFT];
17791c6fdbd8SKent Overstreet 		unsigned pg_offset = (offset + copied) & (PAGE_SIZE - 1);
1780353448f3SKent Overstreet 		unsigned pg_len = min_t(unsigned, reserved - copied,
17811c6fdbd8SKent Overstreet 					PAGE_SIZE - pg_offset);
17821c6fdbd8SKent Overstreet 		unsigned pg_copied = copy_page_from_iter_atomic(page,
1783d1542e03SKent Overstreet 						pg_offset, pg_len, iter);
1784d1542e03SKent Overstreet 
1785d1542e03SKent Overstreet 		if (!pg_copied)
1786d1542e03SKent Overstreet 			break;
17871c6fdbd8SKent Overstreet 
1788912bdf17SKent Overstreet 		if (!PageUptodate(page) &&
1789912bdf17SKent Overstreet 		    pg_copied != PAGE_SIZE &&
1790912bdf17SKent Overstreet 		    pos + copied + pg_copied < inode->v.i_size) {
1791912bdf17SKent Overstreet 			zero_user(page, 0, PAGE_SIZE);
1792912bdf17SKent Overstreet 			break;
1793912bdf17SKent Overstreet 		}
1794912bdf17SKent Overstreet 
17951c6fdbd8SKent Overstreet 		flush_dcache_page(page);
17961c6fdbd8SKent Overstreet 		copied += pg_copied;
1797912bdf17SKent Overstreet 
1798912bdf17SKent Overstreet 		if (pg_copied != pg_len)
1799912bdf17SKent Overstreet 			break;
18001c6fdbd8SKent Overstreet 	}
18011c6fdbd8SKent Overstreet 
18021c6fdbd8SKent Overstreet 	if (!copied)
18031c6fdbd8SKent Overstreet 		goto out;
18041c6fdbd8SKent Overstreet 
1805877dfb34SKent Overstreet 	spin_lock(&inode->v.i_lock);
1806877dfb34SKent Overstreet 	if (pos + copied > inode->v.i_size)
1807877dfb34SKent Overstreet 		i_size_write(&inode->v, pos + copied);
1808877dfb34SKent Overstreet 	spin_unlock(&inode->v.i_lock);
1809877dfb34SKent Overstreet 
1810d1542e03SKent Overstreet 	while (set_dirty < copied) {
1811d1542e03SKent Overstreet 		struct page *page = pages[(offset + set_dirty) >> PAGE_SHIFT];
1812d1542e03SKent Overstreet 		unsigned pg_offset = (offset + set_dirty) & (PAGE_SIZE - 1);
1813d1542e03SKent Overstreet 		unsigned pg_len = min_t(unsigned, copied - set_dirty,
1814d1542e03SKent Overstreet 					PAGE_SIZE - pg_offset);
1815d1542e03SKent Overstreet 
1816d1542e03SKent Overstreet 		if (!PageUptodate(page))
1817d1542e03SKent Overstreet 			SetPageUptodate(page);
1818d1542e03SKent Overstreet 
1819d1542e03SKent Overstreet 		bch2_set_page_dirty(c, inode, page, &res, pg_offset, pg_len);
1820d1542e03SKent Overstreet 		unlock_page(page);
1821d1542e03SKent Overstreet 		put_page(page);
1822d1542e03SKent Overstreet 
1823d1542e03SKent Overstreet 		set_dirty += pg_len;
1824d1542e03SKent Overstreet 	}
1825877dfb34SKent Overstreet 
1826877dfb34SKent Overstreet 	nr_pages_copied = DIV_ROUND_UP(offset + copied, PAGE_SIZE);
1827877dfb34SKent Overstreet 	inode->ei_last_dirtied = (unsigned long) current;
18281c6fdbd8SKent Overstreet out:
1829d1542e03SKent Overstreet 	for (i = nr_pages_copied; i < nr_pages; i++) {
18301c6fdbd8SKent Overstreet 		unlock_page(pages[i]);
18311c6fdbd8SKent Overstreet 		put_page(pages[i]);
18321c6fdbd8SKent Overstreet 	}
18331c6fdbd8SKent Overstreet 
1834d1542e03SKent Overstreet 	bch2_page_reservation_put(c, inode, &res);
18351c6fdbd8SKent Overstreet 
18361c6fdbd8SKent Overstreet 	return copied ?: ret;
18371c6fdbd8SKent Overstreet }
18381c6fdbd8SKent Overstreet 
18391c6fdbd8SKent Overstreet static ssize_t bch2_buffered_write(struct kiocb *iocb, struct iov_iter *iter)
18401c6fdbd8SKent Overstreet {
18411c6fdbd8SKent Overstreet 	struct file *file = iocb->ki_filp;
18421c6fdbd8SKent Overstreet 	struct address_space *mapping = file->f_mapping;
18431c6fdbd8SKent Overstreet 	struct bch_inode_info *inode = file_bch_inode(file);
18441c6fdbd8SKent Overstreet 	loff_t pos = iocb->ki_pos;
18451c6fdbd8SKent Overstreet 	ssize_t written = 0;
18461c6fdbd8SKent Overstreet 	int ret = 0;
18471c6fdbd8SKent Overstreet 
1848a7ecd30cSKent Overstreet 	bch2_pagecache_add_get(inode);
18491c6fdbd8SKent Overstreet 
18501c6fdbd8SKent Overstreet 	do {
18511c6fdbd8SKent Overstreet 		unsigned offset = pos & (PAGE_SIZE - 1);
18521c6fdbd8SKent Overstreet 		unsigned bytes = min_t(unsigned long, iov_iter_count(iter),
18531c6fdbd8SKent Overstreet 			      PAGE_SIZE * WRITE_BATCH_PAGES - offset);
18541c6fdbd8SKent Overstreet again:
18551c6fdbd8SKent Overstreet 		/*
18561c6fdbd8SKent Overstreet 		 * Bring in the user page that we will copy from _first_.
18571c6fdbd8SKent Overstreet 		 * Otherwise there's a nasty deadlock on copying from the
18581c6fdbd8SKent Overstreet 		 * same page as we're writing to, without it being marked
18591c6fdbd8SKent Overstreet 		 * up-to-date.
18601c6fdbd8SKent Overstreet 		 *
18611c6fdbd8SKent Overstreet 		 * Not only is this an optimisation, but it is also required
18621c6fdbd8SKent Overstreet 		 * to check that the address is actually valid, when atomic
18631c6fdbd8SKent Overstreet 		 * usercopies are used, below.
18641c6fdbd8SKent Overstreet 		 */
18651c6fdbd8SKent Overstreet 		if (unlikely(fault_in_iov_iter_readable(iter, bytes))) {
18661c6fdbd8SKent Overstreet 			bytes = min_t(unsigned long, iov_iter_count(iter),
18671c6fdbd8SKent Overstreet 				      PAGE_SIZE - offset);
18681c6fdbd8SKent Overstreet 
18691c6fdbd8SKent Overstreet 			if (unlikely(fault_in_iov_iter_readable(iter, bytes))) {
18701c6fdbd8SKent Overstreet 				ret = -EFAULT;
18711c6fdbd8SKent Overstreet 				break;
18721c6fdbd8SKent Overstreet 			}
18731c6fdbd8SKent Overstreet 		}
18741c6fdbd8SKent Overstreet 
18751c6fdbd8SKent Overstreet 		if (unlikely(fatal_signal_pending(current))) {
18761c6fdbd8SKent Overstreet 			ret = -EINTR;
18771c6fdbd8SKent Overstreet 			break;
18781c6fdbd8SKent Overstreet 		}
18791c6fdbd8SKent Overstreet 
18801c6fdbd8SKent Overstreet 		ret = __bch2_buffered_write(inode, mapping, iter, pos, bytes);
18811c6fdbd8SKent Overstreet 		if (unlikely(ret < 0))
18821c6fdbd8SKent Overstreet 			break;
18831c6fdbd8SKent Overstreet 
18841c6fdbd8SKent Overstreet 		cond_resched();
18851c6fdbd8SKent Overstreet 
18861c6fdbd8SKent Overstreet 		if (unlikely(ret == 0)) {
18871c6fdbd8SKent Overstreet 			/*
18881c6fdbd8SKent Overstreet 			 * If we were unable to copy any data at all, we must
18891c6fdbd8SKent Overstreet 			 * fall back to a single segment length write.
18901c6fdbd8SKent Overstreet 			 *
18911c6fdbd8SKent Overstreet 			 * If we didn't fallback here, we could livelock
18921c6fdbd8SKent Overstreet 			 * because not all segments in the iov can be copied at
18931c6fdbd8SKent Overstreet 			 * once without a pagefault.
18941c6fdbd8SKent Overstreet 			 */
18951c6fdbd8SKent Overstreet 			bytes = min_t(unsigned long, PAGE_SIZE - offset,
18961c6fdbd8SKent Overstreet 				      iov_iter_single_seg_count(iter));
18971c6fdbd8SKent Overstreet 			goto again;
18981c6fdbd8SKent Overstreet 		}
18991c6fdbd8SKent Overstreet 		pos += ret;
19001c6fdbd8SKent Overstreet 		written += ret;
1901912bdf17SKent Overstreet 		ret = 0;
19021c6fdbd8SKent Overstreet 
19031c6fdbd8SKent Overstreet 		balance_dirty_pages_ratelimited(mapping);
19041c6fdbd8SKent Overstreet 	} while (iov_iter_count(iter));
19051c6fdbd8SKent Overstreet 
1906a7ecd30cSKent Overstreet 	bch2_pagecache_add_put(inode);
19071c6fdbd8SKent Overstreet 
19081c6fdbd8SKent Overstreet 	return written ? written : ret;
19091c6fdbd8SKent Overstreet }
19101c6fdbd8SKent Overstreet 
19111c6fdbd8SKent Overstreet /* O_DIRECT reads */
19121c6fdbd8SKent Overstreet 
1913b4725cc1SKent Overstreet static void bio_check_or_release(struct bio *bio, bool check_dirty)
1914b4725cc1SKent Overstreet {
1915b4725cc1SKent Overstreet 	if (check_dirty) {
1916b4725cc1SKent Overstreet 		bio_check_pages_dirty(bio);
1917b4725cc1SKent Overstreet 	} else {
1918b4725cc1SKent Overstreet 		bio_release_pages(bio, false);
1919b4725cc1SKent Overstreet 		bio_put(bio);
1920b4725cc1SKent Overstreet 	}
1921b4725cc1SKent Overstreet }
1922b4725cc1SKent Overstreet 
19231c6fdbd8SKent Overstreet static void bch2_dio_read_complete(struct closure *cl)
19241c6fdbd8SKent Overstreet {
19251c6fdbd8SKent Overstreet 	struct dio_read *dio = container_of(cl, struct dio_read, cl);
19261c6fdbd8SKent Overstreet 
19271c6fdbd8SKent Overstreet 	dio->req->ki_complete(dio->req, dio->ret);
1928b4725cc1SKent Overstreet 	bio_check_or_release(&dio->rbio.bio, dio->should_dirty);
19291c6fdbd8SKent Overstreet }
19301c6fdbd8SKent Overstreet 
19311c6fdbd8SKent Overstreet static void bch2_direct_IO_read_endio(struct bio *bio)
19321c6fdbd8SKent Overstreet {
19331c6fdbd8SKent Overstreet 	struct dio_read *dio = bio->bi_private;
19341c6fdbd8SKent Overstreet 
19351c6fdbd8SKent Overstreet 	if (bio->bi_status)
19361c6fdbd8SKent Overstreet 		dio->ret = blk_status_to_errno(bio->bi_status);
19371c6fdbd8SKent Overstreet 
19381c6fdbd8SKent Overstreet 	closure_put(&dio->cl);
19391c6fdbd8SKent Overstreet }
19401c6fdbd8SKent Overstreet 
19411c6fdbd8SKent Overstreet static void bch2_direct_IO_read_split_endio(struct bio *bio)
19421c6fdbd8SKent Overstreet {
1943b4725cc1SKent Overstreet 	struct dio_read *dio = bio->bi_private;
1944b4725cc1SKent Overstreet 	bool should_dirty = dio->should_dirty;
1945b4725cc1SKent Overstreet 
19461c6fdbd8SKent Overstreet 	bch2_direct_IO_read_endio(bio);
1947b4725cc1SKent Overstreet 	bio_check_or_release(bio, should_dirty);
19481c6fdbd8SKent Overstreet }
19491c6fdbd8SKent Overstreet 
19501c6fdbd8SKent Overstreet static int bch2_direct_IO_read(struct kiocb *req, struct iov_iter *iter)
19511c6fdbd8SKent Overstreet {
19521c6fdbd8SKent Overstreet 	struct file *file = req->ki_filp;
19531c6fdbd8SKent Overstreet 	struct bch_inode_info *inode = file_bch_inode(file);
19541c6fdbd8SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
195501ad6737SKent Overstreet 	struct bch_io_opts opts;
19561c6fdbd8SKent Overstreet 	struct dio_read *dio;
19571c6fdbd8SKent Overstreet 	struct bio *bio;
19581c6fdbd8SKent Overstreet 	loff_t offset = req->ki_pos;
19591c6fdbd8SKent Overstreet 	bool sync = is_sync_kiocb(req);
19601c6fdbd8SKent Overstreet 	size_t shorten;
19611c6fdbd8SKent Overstreet 	ssize_t ret;
19621c6fdbd8SKent Overstreet 
196301ad6737SKent Overstreet 	bch2_inode_opts_get(&opts, c, &inode->ei_inode);
196401ad6737SKent Overstreet 
19651c6fdbd8SKent Overstreet 	if ((offset|iter->count) & (block_bytes(c) - 1))
19661c6fdbd8SKent Overstreet 		return -EINVAL;
19671c6fdbd8SKent Overstreet 
19681c6fdbd8SKent Overstreet 	ret = min_t(loff_t, iter->count,
19691c6fdbd8SKent Overstreet 		    max_t(loff_t, 0, i_size_read(&inode->v) - offset));
19701c6fdbd8SKent Overstreet 
19711c6fdbd8SKent Overstreet 	if (!ret)
19721c6fdbd8SKent Overstreet 		return ret;
19731c6fdbd8SKent Overstreet 
19741c6fdbd8SKent Overstreet 	shorten = iov_iter_count(iter) - round_up(ret, block_bytes(c));
19751c6fdbd8SKent Overstreet 	iter->count -= shorten;
19761c6fdbd8SKent Overstreet 
19771c6fdbd8SKent Overstreet 	bio = bio_alloc_bioset(NULL,
19784d126dc8SKent Overstreet 			       bio_iov_vecs_to_alloc(iter, BIO_MAX_VECS),
19791c6fdbd8SKent Overstreet 			       REQ_OP_READ,
19801c6fdbd8SKent Overstreet 			       GFP_KERNEL,
19811c6fdbd8SKent Overstreet 			       &c->dio_read_bioset);
19821c6fdbd8SKent Overstreet 
19831c6fdbd8SKent Overstreet 	bio->bi_end_io = bch2_direct_IO_read_endio;
19841c6fdbd8SKent Overstreet 
19851c6fdbd8SKent Overstreet 	dio = container_of(bio, struct dio_read, rbio.bio);
19861c6fdbd8SKent Overstreet 	closure_init(&dio->cl, NULL);
19871c6fdbd8SKent Overstreet 
19881c6fdbd8SKent Overstreet 	/*
19891c6fdbd8SKent Overstreet 	 * this is a _really_ horrible hack just to avoid an atomic sub at the
19901c6fdbd8SKent Overstreet 	 * end:
19911c6fdbd8SKent Overstreet 	 */
19921c6fdbd8SKent Overstreet 	if (!sync) {
19931c6fdbd8SKent Overstreet 		set_closure_fn(&dio->cl, bch2_dio_read_complete, NULL);
19941c6fdbd8SKent Overstreet 		atomic_set(&dio->cl.remaining,
19951c6fdbd8SKent Overstreet 			   CLOSURE_REMAINING_INITIALIZER -
19961c6fdbd8SKent Overstreet 			   CLOSURE_RUNNING +
19971c6fdbd8SKent Overstreet 			   CLOSURE_DESTRUCTOR);
19981c6fdbd8SKent Overstreet 	} else {
19991c6fdbd8SKent Overstreet 		atomic_set(&dio->cl.remaining,
20001c6fdbd8SKent Overstreet 			   CLOSURE_REMAINING_INITIALIZER + 1);
20011c6fdbd8SKent Overstreet 	}
20021c6fdbd8SKent Overstreet 
20031c6fdbd8SKent Overstreet 	dio->req	= req;
20041c6fdbd8SKent Overstreet 	dio->ret	= ret;
2005b4725cc1SKent Overstreet 	/*
2006b4725cc1SKent Overstreet 	 * This is one of the sketchier things I've encountered: we have to skip
2007b4725cc1SKent Overstreet 	 * the dirtying of requests that are internal from the kernel (i.e. from
2008b4725cc1SKent Overstreet 	 * loopback), because we'll deadlock on page_lock.
2009b4725cc1SKent Overstreet 	 */
2010b4725cc1SKent Overstreet 	dio->should_dirty = iter_is_iovec(iter);
20111c6fdbd8SKent Overstreet 
20121c6fdbd8SKent Overstreet 	goto start;
20131c6fdbd8SKent Overstreet 	while (iter->count) {
20141c6fdbd8SKent Overstreet 		bio = bio_alloc_bioset(NULL,
20154d126dc8SKent Overstreet 				       bio_iov_vecs_to_alloc(iter, BIO_MAX_VECS),
20161c6fdbd8SKent Overstreet 				       REQ_OP_READ,
20171c6fdbd8SKent Overstreet 				       GFP_KERNEL,
20181c6fdbd8SKent Overstreet 				       &c->bio_read);
20191c6fdbd8SKent Overstreet 		bio->bi_end_io		= bch2_direct_IO_read_split_endio;
20201c6fdbd8SKent Overstreet start:
20211c6fdbd8SKent Overstreet 		bio->bi_opf		= REQ_OP_READ|REQ_SYNC;
20221c6fdbd8SKent Overstreet 		bio->bi_iter.bi_sector	= offset >> 9;
20231c6fdbd8SKent Overstreet 		bio->bi_private		= dio;
20241c6fdbd8SKent Overstreet 
20251c6fdbd8SKent Overstreet 		ret = bio_iov_iter_get_pages(bio, iter);
20261c6fdbd8SKent Overstreet 		if (ret < 0) {
20271c6fdbd8SKent Overstreet 			/* XXX: fault inject this path */
20281c6fdbd8SKent Overstreet 			bio->bi_status = BLK_STS_RESOURCE;
20291c6fdbd8SKent Overstreet 			bio_endio(bio);
20301c6fdbd8SKent Overstreet 			break;
20311c6fdbd8SKent Overstreet 		}
20321c6fdbd8SKent Overstreet 
20331c6fdbd8SKent Overstreet 		offset += bio->bi_iter.bi_size;
2034b4725cc1SKent Overstreet 
2035b4725cc1SKent Overstreet 		if (dio->should_dirty)
20361c6fdbd8SKent Overstreet 			bio_set_pages_dirty(bio);
20371c6fdbd8SKent Overstreet 
20381c6fdbd8SKent Overstreet 		if (iter->count)
20391c6fdbd8SKent Overstreet 			closure_get(&dio->cl);
20401c6fdbd8SKent Overstreet 
20418c6d298aSKent Overstreet 		bch2_read(c, rbio_init(bio, opts), inode_inum(inode));
20421c6fdbd8SKent Overstreet 	}
20431c6fdbd8SKent Overstreet 
20441c6fdbd8SKent Overstreet 	iter->count += shorten;
20451c6fdbd8SKent Overstreet 
20461c6fdbd8SKent Overstreet 	if (sync) {
20471c6fdbd8SKent Overstreet 		closure_sync(&dio->cl);
20481c6fdbd8SKent Overstreet 		closure_debug_destroy(&dio->cl);
20491c6fdbd8SKent Overstreet 		ret = dio->ret;
2050b4725cc1SKent Overstreet 		bio_check_or_release(&dio->rbio.bio, dio->should_dirty);
20511c6fdbd8SKent Overstreet 		return ret;
20521c6fdbd8SKent Overstreet 	} else {
20531c6fdbd8SKent Overstreet 		return -EIOCBQUEUED;
20541c6fdbd8SKent Overstreet 	}
20551c6fdbd8SKent Overstreet }
20561c6fdbd8SKent Overstreet 
20571c6fdbd8SKent Overstreet ssize_t bch2_read_iter(struct kiocb *iocb, struct iov_iter *iter)
20581c6fdbd8SKent Overstreet {
20591c6fdbd8SKent Overstreet 	struct file *file = iocb->ki_filp;
20601c6fdbd8SKent Overstreet 	struct bch_inode_info *inode = file_bch_inode(file);
20611c6fdbd8SKent Overstreet 	struct address_space *mapping = file->f_mapping;
20621c6fdbd8SKent Overstreet 	size_t count = iov_iter_count(iter);
20631c6fdbd8SKent Overstreet 	ssize_t ret;
20641c6fdbd8SKent Overstreet 
20651c6fdbd8SKent Overstreet 	if (!count)
20661c6fdbd8SKent Overstreet 		return 0; /* skip atime */
20671c6fdbd8SKent Overstreet 
20681c6fdbd8SKent Overstreet 	if (iocb->ki_flags & IOCB_DIRECT) {
20691c6fdbd8SKent Overstreet 		struct blk_plug plug;
20701c6fdbd8SKent Overstreet 
2071a023127aSKent Overstreet 		if (unlikely(mapping->nrpages)) {
20721c6fdbd8SKent Overstreet 			ret = filemap_write_and_wait_range(mapping,
20731c6fdbd8SKent Overstreet 						iocb->ki_pos,
20741c6fdbd8SKent Overstreet 						iocb->ki_pos + count - 1);
20751c6fdbd8SKent Overstreet 			if (ret < 0)
20765c1ef830SKent Overstreet 				goto out;
2077a023127aSKent Overstreet 		}
20781c6fdbd8SKent Overstreet 
20791c6fdbd8SKent Overstreet 		file_accessed(file);
20801c6fdbd8SKent Overstreet 
20811c6fdbd8SKent Overstreet 		blk_start_plug(&plug);
20821c6fdbd8SKent Overstreet 		ret = bch2_direct_IO_read(iocb, iter);
20831c6fdbd8SKent Overstreet 		blk_finish_plug(&plug);
20841c6fdbd8SKent Overstreet 
20851c6fdbd8SKent Overstreet 		if (ret >= 0)
20861c6fdbd8SKent Overstreet 			iocb->ki_pos += ret;
20871c6fdbd8SKent Overstreet 	} else {
2088a7ecd30cSKent Overstreet 		bch2_pagecache_add_get(inode);
20891c6fdbd8SKent Overstreet 		ret = generic_file_read_iter(iocb, iter);
2090a7ecd30cSKent Overstreet 		bch2_pagecache_add_put(inode);
20911c6fdbd8SKent Overstreet 	}
20925c1ef830SKent Overstreet out:
20935c1ef830SKent Overstreet 	return bch2_err_class(ret);
20941c6fdbd8SKent Overstreet }
20951c6fdbd8SKent Overstreet 
20961c6fdbd8SKent Overstreet /* O_DIRECT writes */
20971c6fdbd8SKent Overstreet 
20986fed42bbSKent Overstreet static bool bch2_check_range_allocated(struct bch_fs *c, subvol_inum inum,
20996fed42bbSKent Overstreet 				       u64 offset, u64 size,
21006fed42bbSKent Overstreet 				       unsigned nr_replicas, bool compressed)
21016fed42bbSKent Overstreet {
21026fed42bbSKent Overstreet 	struct btree_trans trans;
21036fed42bbSKent Overstreet 	struct btree_iter iter;
21046fed42bbSKent Overstreet 	struct bkey_s_c k;
21056fed42bbSKent Overstreet 	u64 end = offset + size;
21066fed42bbSKent Overstreet 	u32 snapshot;
21076fed42bbSKent Overstreet 	bool ret = true;
21086fed42bbSKent Overstreet 	int err;
21096fed42bbSKent Overstreet 
21106fed42bbSKent Overstreet 	bch2_trans_init(&trans, c, 0, 0);
21116fed42bbSKent Overstreet retry:
21126fed42bbSKent Overstreet 	bch2_trans_begin(&trans);
21136fed42bbSKent Overstreet 
21146fed42bbSKent Overstreet 	err = bch2_subvolume_get_snapshot(&trans, inum.subvol, &snapshot);
21156fed42bbSKent Overstreet 	if (err)
21166fed42bbSKent Overstreet 		goto err;
21176fed42bbSKent Overstreet 
2118e5fa91d7SKent Overstreet 	for_each_btree_key_norestart(&trans, iter, BTREE_ID_extents,
21196fed42bbSKent Overstreet 			   SPOS(inum.inum, offset, snapshot),
21206fed42bbSKent Overstreet 			   BTREE_ITER_SLOTS, k, err) {
2121e88a75ebSKent Overstreet 		if (bkey_ge(bkey_start_pos(k.k), POS(inum.inum, end)))
21226fed42bbSKent Overstreet 			break;
21236fed42bbSKent Overstreet 
21248c6d298aSKent Overstreet 		if (k.k->p.snapshot != snapshot ||
21258c6d298aSKent Overstreet 		    nr_replicas > bch2_bkey_replicas(c, k) ||
21266fed42bbSKent Overstreet 		    (!compressed && bch2_bkey_sectors_compressed(k))) {
21276fed42bbSKent Overstreet 			ret = false;
21286fed42bbSKent Overstreet 			break;
21296fed42bbSKent Overstreet 		}
21306fed42bbSKent Overstreet 	}
21316fed42bbSKent Overstreet 
21326fed42bbSKent Overstreet 	offset = iter.pos.offset;
21336fed42bbSKent Overstreet 	bch2_trans_iter_exit(&trans, &iter);
21346fed42bbSKent Overstreet err:
2135549d173cSKent Overstreet 	if (bch2_err_matches(err, BCH_ERR_transaction_restart))
21366fed42bbSKent Overstreet 		goto retry;
21376fed42bbSKent Overstreet 	bch2_trans_exit(&trans);
21386fed42bbSKent Overstreet 
21396fed42bbSKent Overstreet 	return err ? false : ret;
21406fed42bbSKent Overstreet }
21416fed42bbSKent Overstreet 
2142182c7bbfSKent Overstreet static noinline bool bch2_dio_write_check_allocated(struct dio_write *dio)
2143182c7bbfSKent Overstreet {
2144182c7bbfSKent Overstreet 	struct bch_fs *c = dio->op.c;
2145182c7bbfSKent Overstreet 	struct bch_inode_info *inode = dio->inode;
2146182c7bbfSKent Overstreet 	struct bio *bio = &dio->op.wbio.bio;
2147182c7bbfSKent Overstreet 
2148182c7bbfSKent Overstreet 	return bch2_check_range_allocated(c, inode_inum(inode),
2149182c7bbfSKent Overstreet 				dio->op.pos.offset, bio_sectors(bio),
2150182c7bbfSKent Overstreet 				dio->op.opts.data_replicas,
2151182c7bbfSKent Overstreet 				dio->op.opts.compression != 0);
2152182c7bbfSKent Overstreet }
2153182c7bbfSKent Overstreet 
2154a1ee777bSKent Overstreet static void bch2_dio_write_loop_async(struct bch_write_op *);
2155a1ee777bSKent Overstreet static __always_inline long bch2_dio_write_done(struct dio_write *dio);
2156a1ee777bSKent Overstreet 
21571c6fdbd8SKent Overstreet /*
21581c6fdbd8SKent Overstreet  * We're going to return -EIOCBQUEUED, but we haven't finished consuming the
21591c6fdbd8SKent Overstreet  * iov_iter yet, so we need to stash a copy of the iovec: it might be on the
21601c6fdbd8SKent Overstreet  * caller's stack, we're not guaranteed that it will live for the duration of
21611c6fdbd8SKent Overstreet  * the IO:
21621c6fdbd8SKent Overstreet  */
21631c6fdbd8SKent Overstreet static noinline int bch2_dio_write_copy_iov(struct dio_write *dio)
21641c6fdbd8SKent Overstreet {
21651c6fdbd8SKent Overstreet 	struct iovec *iov = dio->inline_vecs;
21661c6fdbd8SKent Overstreet 
21671c6fdbd8SKent Overstreet 	/*
21681c6fdbd8SKent Overstreet 	 * iov_iter has a single embedded iovec - nothing to do:
21691c6fdbd8SKent Overstreet 	 */
21701c6fdbd8SKent Overstreet 	if (iter_is_ubuf(&dio->iter))
21711c6fdbd8SKent Overstreet 		return 0;
21721c6fdbd8SKent Overstreet 
21731c6fdbd8SKent Overstreet 	/*
21741c6fdbd8SKent Overstreet 	 * We don't currently handle non-iovec iov_iters here - return an error,
21751c6fdbd8SKent Overstreet 	 * and we'll fall back to doing the IO synchronously:
21761c6fdbd8SKent Overstreet 	 */
21771c6fdbd8SKent Overstreet 	if (!iter_is_iovec(&dio->iter))
21781c6fdbd8SKent Overstreet 		return -1;
21791c6fdbd8SKent Overstreet 
21801c6fdbd8SKent Overstreet 	if (dio->iter.nr_segs > ARRAY_SIZE(dio->inline_vecs)) {
21811c6fdbd8SKent Overstreet 		iov = kmalloc_array(dio->iter.nr_segs, sizeof(*iov),
21821c6fdbd8SKent Overstreet 				    GFP_KERNEL);
21831c6fdbd8SKent Overstreet 		if (unlikely(!iov))
21841c6fdbd8SKent Overstreet 			return -ENOMEM;
21851c6fdbd8SKent Overstreet 
21861c6fdbd8SKent Overstreet 		dio->free_iov = true;
21871c6fdbd8SKent Overstreet 	}
21881c6fdbd8SKent Overstreet 
21891c6fdbd8SKent Overstreet 	memcpy(iov, dio->iter.__iov, dio->iter.nr_segs * sizeof(*iov));
21901c6fdbd8SKent Overstreet 	dio->iter.__iov = iov;
21911c6fdbd8SKent Overstreet 	return 0;
21921c6fdbd8SKent Overstreet }
21931c6fdbd8SKent Overstreet 
2194a1ee777bSKent Overstreet static void bch2_dio_write_flush_done(struct closure *cl)
2195a1ee777bSKent Overstreet {
2196a1ee777bSKent Overstreet 	struct dio_write *dio = container_of(cl, struct dio_write, op.cl);
2197a1ee777bSKent Overstreet 	struct bch_fs *c = dio->op.c;
2198a1ee777bSKent Overstreet 
2199a1ee777bSKent Overstreet 	closure_debug_destroy(cl);
2200a1ee777bSKent Overstreet 
2201a1ee777bSKent Overstreet 	dio->op.error = bch2_journal_error(&c->journal);
2202a1ee777bSKent Overstreet 
2203a1ee777bSKent Overstreet 	bch2_dio_write_done(dio);
2204a1ee777bSKent Overstreet }
2205a1ee777bSKent Overstreet 
2206a1ee777bSKent Overstreet static noinline void bch2_dio_write_flush(struct dio_write *dio)
2207a1ee777bSKent Overstreet {
2208a1ee777bSKent Overstreet 	struct bch_fs *c = dio->op.c;
2209a1ee777bSKent Overstreet 	struct bch_inode_unpacked inode;
2210a1ee777bSKent Overstreet 	int ret;
2211a1ee777bSKent Overstreet 
2212a1ee777bSKent Overstreet 	dio->flush = 0;
2213a1ee777bSKent Overstreet 
2214a1ee777bSKent Overstreet 	closure_init(&dio->op.cl, NULL);
2215a1ee777bSKent Overstreet 
2216a1ee777bSKent Overstreet 	if (!dio->op.error) {
2217a1ee777bSKent Overstreet 		ret = bch2_inode_find_by_inum(c, inode_inum(dio->inode), &inode);
2218a8b3a677SKent Overstreet 		if (ret) {
2219a1ee777bSKent Overstreet 			dio->op.error = ret;
2220a8b3a677SKent Overstreet 		} else {
2221a1ee777bSKent Overstreet 			bch2_journal_flush_seq_async(&c->journal, inode.bi_journal_seq, &dio->op.cl);
2222a8b3a677SKent Overstreet 			bch2_inode_flush_nocow_writes_async(c, dio->inode, &dio->op.cl);
2223a8b3a677SKent Overstreet 		}
2224a1ee777bSKent Overstreet 	}
2225a1ee777bSKent Overstreet 
2226a1ee777bSKent Overstreet 	if (dio->sync) {
2227a1ee777bSKent Overstreet 		closure_sync(&dio->op.cl);
2228a1ee777bSKent Overstreet 		closure_debug_destroy(&dio->op.cl);
2229a1ee777bSKent Overstreet 	} else {
2230a1ee777bSKent Overstreet 		continue_at(&dio->op.cl, bch2_dio_write_flush_done, NULL);
2231a1ee777bSKent Overstreet 	}
2232a1ee777bSKent Overstreet }
2233042a1f26SKent Overstreet 
2234182c7bbfSKent Overstreet static __always_inline long bch2_dio_write_done(struct dio_write *dio)
2235182c7bbfSKent Overstreet {
2236182c7bbfSKent Overstreet 	struct kiocb *req = dio->req;
2237182c7bbfSKent Overstreet 	struct bch_inode_info *inode = dio->inode;
2238182c7bbfSKent Overstreet 	bool sync = dio->sync;
2239a1ee777bSKent Overstreet 	long ret;
2240a1ee777bSKent Overstreet 
2241a1ee777bSKent Overstreet 	if (unlikely(dio->flush)) {
2242a1ee777bSKent Overstreet 		bch2_dio_write_flush(dio);
2243a1ee777bSKent Overstreet 		if (!sync)
2244a1ee777bSKent Overstreet 			return -EIOCBQUEUED;
2245a1ee777bSKent Overstreet 	}
2246182c7bbfSKent Overstreet 
2247a7ecd30cSKent Overstreet 	bch2_pagecache_block_put(inode);
2248182c7bbfSKent Overstreet 
2249182c7bbfSKent Overstreet 	if (dio->free_iov)
2250182c7bbfSKent Overstreet 		kfree(dio->iter.__iov);
2251a1ee777bSKent Overstreet 
2252a1ee777bSKent Overstreet 	ret = dio->op.error ?: ((long) dio->written << 9);
2253182c7bbfSKent Overstreet 	bio_put(&dio->op.wbio.bio);
2254182c7bbfSKent Overstreet 
2255182c7bbfSKent Overstreet 	/* inode->i_dio_count is our ref on inode and thus bch_fs */
2256182c7bbfSKent Overstreet 	inode_dio_end(&inode->v);
2257182c7bbfSKent Overstreet 
2258182c7bbfSKent Overstreet 	if (ret < 0)
2259182c7bbfSKent Overstreet 		ret = bch2_err_class(ret);
2260182c7bbfSKent Overstreet 
2261182c7bbfSKent Overstreet 	if (!sync) {
2262182c7bbfSKent Overstreet 		req->ki_complete(req, ret);
2263182c7bbfSKent Overstreet 		ret = -EIOCBQUEUED;
2264182c7bbfSKent Overstreet 	}
2265182c7bbfSKent Overstreet 	return ret;
2266182c7bbfSKent Overstreet }
2267182c7bbfSKent Overstreet 
2268182c7bbfSKent Overstreet static __always_inline void bch2_dio_write_end(struct dio_write *dio)
2269182c7bbfSKent Overstreet {
2270182c7bbfSKent Overstreet 	struct bch_fs *c = dio->op.c;
2271182c7bbfSKent Overstreet 	struct kiocb *req = dio->req;
2272182c7bbfSKent Overstreet 	struct bch_inode_info *inode = dio->inode;
2273182c7bbfSKent Overstreet 	struct bio *bio = &dio->op.wbio.bio;
2274182c7bbfSKent Overstreet 
2275182c7bbfSKent Overstreet 	req->ki_pos	+= (u64) dio->op.written << 9;
2276182c7bbfSKent Overstreet 	dio->written	+= dio->op.written;
2277182c7bbfSKent Overstreet 
22786b1b186aSKent Overstreet 	if (dio->extending) {
2279182c7bbfSKent Overstreet 		spin_lock(&inode->v.i_lock);
2280182c7bbfSKent Overstreet 		if (req->ki_pos > inode->v.i_size)
2281182c7bbfSKent Overstreet 			i_size_write(&inode->v, req->ki_pos);
2282182c7bbfSKent Overstreet 		spin_unlock(&inode->v.i_lock);
22836b1b186aSKent Overstreet 	}
22846b1b186aSKent Overstreet 
22856b1b186aSKent Overstreet 	if (dio->op.i_sectors_delta || dio->quota_res.sectors) {
22866b1b186aSKent Overstreet 		mutex_lock(&inode->ei_quota_lock);
22876b1b186aSKent Overstreet 		__i_sectors_acct(c, inode, &dio->quota_res, dio->op.i_sectors_delta);
22886b1b186aSKent Overstreet 		__bch2_quota_reservation_put(c, inode, &dio->quota_res);
22896b1b186aSKent Overstreet 		mutex_unlock(&inode->ei_quota_lock);
22906b1b186aSKent Overstreet 	}
2291182c7bbfSKent Overstreet 
2292182c7bbfSKent Overstreet 	bio_release_pages(bio, false);
2293182c7bbfSKent Overstreet 
2294182c7bbfSKent Overstreet 	if (unlikely(dio->op.error))
2295182c7bbfSKent Overstreet 		set_bit(EI_INODE_ERROR, &inode->ei_flags);
2296182c7bbfSKent Overstreet }
2297182c7bbfSKent Overstreet 
22984d868d18SKent Overstreet static __always_inline long bch2_dio_write_loop(struct dio_write *dio)
22991c6fdbd8SKent Overstreet {
2300182c7bbfSKent Overstreet 	struct bch_fs *c = dio->op.c;
23011c6fdbd8SKent Overstreet 	struct kiocb *req = dio->req;
2302182c7bbfSKent Overstreet 	struct address_space *mapping = dio->mapping;
2303182c7bbfSKent Overstreet 	struct bch_inode_info *inode = dio->inode;
230401ad6737SKent Overstreet 	struct bch_io_opts opts;
23059a3df993SKent Overstreet 	struct bio *bio = &dio->op.wbio.bio;
2306eb8e6e9cSKent Overstreet 	unsigned unaligned, iter_count;
2307eb8e6e9cSKent Overstreet 	bool sync = dio->sync, dropped_locks;
23081c6fdbd8SKent Overstreet 	long ret;
23091c6fdbd8SKent Overstreet 
231001ad6737SKent Overstreet 	bch2_inode_opts_get(&opts, c, &inode->ei_inode);
231101ad6737SKent Overstreet 
23121c6fdbd8SKent Overstreet 	while (1) {
2313eb8e6e9cSKent Overstreet 		iter_count = dio->iter.count;
2314eb8e6e9cSKent Overstreet 
2315182c7bbfSKent Overstreet 		EBUG_ON(current->faults_disabled_mapping);
23161c6fdbd8SKent Overstreet 		current->faults_disabled_mapping = mapping;
23171c6fdbd8SKent Overstreet 
23181c6fdbd8SKent Overstreet 		ret = bio_iov_iter_get_pages(bio, &dio->iter);
23191c6fdbd8SKent Overstreet 
2320eb8e6e9cSKent Overstreet 		dropped_locks = fdm_dropped_locks();
2321eb8e6e9cSKent Overstreet 
23221c6fdbd8SKent Overstreet 		current->faults_disabled_mapping = NULL;
23231c6fdbd8SKent Overstreet 
2324eb8e6e9cSKent Overstreet 		/*
2325eb8e6e9cSKent Overstreet 		 * If the fault handler returned an error but also signalled
2326eb8e6e9cSKent Overstreet 		 * that it dropped & retook ei_pagecache_lock, we just need to
2327eb8e6e9cSKent Overstreet 		 * re-shoot down the page cache and retry:
2328eb8e6e9cSKent Overstreet 		 */
2329eb8e6e9cSKent Overstreet 		if (dropped_locks && ret)
2330eb8e6e9cSKent Overstreet 			ret = 0;
2331eb8e6e9cSKent Overstreet 
23321c6fdbd8SKent Overstreet 		if (unlikely(ret < 0))
23331c6fdbd8SKent Overstreet 			goto err;
23341c6fdbd8SKent Overstreet 
2335eb8e6e9cSKent Overstreet 		if (unlikely(dropped_locks)) {
2336eb8e6e9cSKent Overstreet 			ret = write_invalidate_inode_pages_range(mapping,
2337eb8e6e9cSKent Overstreet 					req->ki_pos,
2338eb8e6e9cSKent Overstreet 					req->ki_pos + iter_count - 1);
2339eb8e6e9cSKent Overstreet 			if (unlikely(ret))
2340eb8e6e9cSKent Overstreet 				goto err;
2341eb8e6e9cSKent Overstreet 
2342eb8e6e9cSKent Overstreet 			if (!bio->bi_iter.bi_size)
2343eb8e6e9cSKent Overstreet 				continue;
2344eb8e6e9cSKent Overstreet 		}
2345eb8e6e9cSKent Overstreet 
23460a426c32SKent Overstreet 		unaligned = bio->bi_iter.bi_size & (block_bytes(c) - 1);
23470a426c32SKent Overstreet 		bio->bi_iter.bi_size -= unaligned;
23480a426c32SKent Overstreet 		iov_iter_revert(&dio->iter, unaligned);
23490a426c32SKent Overstreet 
23500a426c32SKent Overstreet 		if (!bio->bi_iter.bi_size) {
23510a426c32SKent Overstreet 			/*
23520a426c32SKent Overstreet 			 * bio_iov_iter_get_pages was only able to get <
23530a426c32SKent Overstreet 			 * blocksize worth of pages:
23540a426c32SKent Overstreet 			 */
23550a426c32SKent Overstreet 			ret = -EFAULT;
23560a426c32SKent Overstreet 			goto err;
23570a426c32SKent Overstreet 		}
23580a426c32SKent Overstreet 
235901ad6737SKent Overstreet 		bch2_write_op_init(&dio->op, c, opts);
2360182c7bbfSKent Overstreet 		dio->op.end_io		= sync
2361182c7bbfSKent Overstreet 			? NULL
2362182c7bbfSKent Overstreet 			: bch2_dio_write_loop_async;
2363042a1f26SKent Overstreet 		dio->op.target		= dio->op.opts.foreground_target;
2364042a1f26SKent Overstreet 		dio->op.write_point	= writepoint_hashed((unsigned long) current);
2365042a1f26SKent Overstreet 		dio->op.nr_replicas	= dio->op.opts.data_replicas;
23668c6d298aSKent Overstreet 		dio->op.subvol		= inode->ei_subvol;
2367042a1f26SKent Overstreet 		dio->op.pos		= POS(inode->v.i_ino, (u64) req->ki_pos >> 9);
2368a8b3a677SKent Overstreet 		dio->op.devs_need_flush	= &inode->ei_devs_need_flush;
2369042a1f26SKent Overstreet 
23701df3e199SKent Overstreet 		if (sync)
23711df3e199SKent Overstreet 			dio->op.flags |= BCH_WRITE_SYNC;
2372a6336910SKent Overstreet 		dio->op.flags |= BCH_WRITE_CHECK_ENOSPC;
2373042a1f26SKent Overstreet 
23746b1b186aSKent Overstreet 		ret = bch2_quota_reservation_add(c, inode, &dio->quota_res,
23756b1b186aSKent Overstreet 						 bio_sectors(bio), true);
23766b1b186aSKent Overstreet 		if (unlikely(ret))
23776b1b186aSKent Overstreet 			goto err;
23786b1b186aSKent Overstreet 
2379042a1f26SKent Overstreet 		ret = bch2_disk_reservation_get(c, &dio->op.res, bio_sectors(bio),
2380042a1f26SKent Overstreet 						dio->op.opts.data_replicas, 0);
2381042a1f26SKent Overstreet 		if (unlikely(ret) &&
2382182c7bbfSKent Overstreet 		    !bch2_dio_write_check_allocated(dio))
2383042a1f26SKent Overstreet 			goto err;
23841c6fdbd8SKent Overstreet 
23851c6fdbd8SKent Overstreet 		task_io_account_write(bio->bi_iter.bi_size);
23861c6fdbd8SKent Overstreet 
2387182c7bbfSKent Overstreet 		if (unlikely(dio->iter.count) &&
2388182c7bbfSKent Overstreet 		    !dio->sync &&
2389182c7bbfSKent Overstreet 		    !dio->loop &&
2390182c7bbfSKent Overstreet 		    bch2_dio_write_copy_iov(dio))
2391286d8ad0SKent Overstreet 			dio->sync = sync = true;
2392182c7bbfSKent Overstreet 
23931c6fdbd8SKent Overstreet 		dio->loop = true;
2394f8f30863SKent Overstreet 		closure_call(&dio->op.cl, bch2_write, NULL, NULL);
23951c6fdbd8SKent Overstreet 
2396182c7bbfSKent Overstreet 		if (!sync)
23971c6fdbd8SKent Overstreet 			return -EIOCBQUEUED;
23989a3df993SKent Overstreet 
2399182c7bbfSKent Overstreet 		bch2_dio_write_end(dio);
24009a3df993SKent Overstreet 
2401182c7bbfSKent Overstreet 		if (likely(!dio->iter.count) || dio->op.error)
24021c6fdbd8SKent Overstreet 			break;
2403f8f30863SKent Overstreet 
24041c6fdbd8SKent Overstreet 		bio_reset(bio, NULL, REQ_OP_WRITE);
24051c6fdbd8SKent Overstreet 	}
2406182c7bbfSKent Overstreet out:
2407182c7bbfSKent Overstreet 	return bch2_dio_write_done(dio);
24081c6fdbd8SKent Overstreet err:
2409182c7bbfSKent Overstreet 	dio->op.error = ret;
24101c6fdbd8SKent Overstreet 
24115468f119SKent Overstreet 	bio_release_pages(bio, false);
24126b1b186aSKent Overstreet 
24136b1b186aSKent Overstreet 	bch2_quota_reservation_put(c, inode, &dio->quota_res);
2414182c7bbfSKent Overstreet 	goto out;
24151c6fdbd8SKent Overstreet }
24161c6fdbd8SKent Overstreet 
24174d868d18SKent Overstreet static noinline __cold void bch2_dio_write_continue(struct dio_write *dio)
24181c6fdbd8SKent Overstreet {
2419182c7bbfSKent Overstreet 	struct mm_struct *mm = dio->mm;
24201c6fdbd8SKent Overstreet 
2421182c7bbfSKent Overstreet 	bio_reset(&dio->op.wbio.bio, NULL, REQ_OP_WRITE);
2422182c7bbfSKent Overstreet 
2423182c7bbfSKent Overstreet 	if (mm)
2424182c7bbfSKent Overstreet 		kthread_use_mm(mm);
24251c6fdbd8SKent Overstreet 	bch2_dio_write_loop(dio);
2426182c7bbfSKent Overstreet 	if (mm)
2427182c7bbfSKent Overstreet 		kthread_unuse_mm(mm);
24281c6fdbd8SKent Overstreet }
24291c6fdbd8SKent Overstreet 
24304d868d18SKent Overstreet static void bch2_dio_write_loop_async(struct bch_write_op *op)
24314d868d18SKent Overstreet {
24324d868d18SKent Overstreet 	struct dio_write *dio = container_of(op, struct dio_write, op);
24334d868d18SKent Overstreet 
24344d868d18SKent Overstreet 	bch2_dio_write_end(dio);
24354d868d18SKent Overstreet 
24364d868d18SKent Overstreet 	if (likely(!dio->iter.count) || dio->op.error)
24374d868d18SKent Overstreet 		bch2_dio_write_done(dio);
24384d868d18SKent Overstreet 	else
24394d868d18SKent Overstreet 		bch2_dio_write_continue(dio);
24404d868d18SKent Overstreet }
24414d868d18SKent Overstreet 
24421c6fdbd8SKent Overstreet static noinline
24431c6fdbd8SKent Overstreet ssize_t bch2_direct_write(struct kiocb *req, struct iov_iter *iter)
24441c6fdbd8SKent Overstreet {
24451c6fdbd8SKent Overstreet 	struct file *file = req->ki_filp;
244654847d25SKent Overstreet 	struct address_space *mapping = file->f_mapping;
24471c6fdbd8SKent Overstreet 	struct bch_inode_info *inode = file_bch_inode(file);
24481c6fdbd8SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
24491c6fdbd8SKent Overstreet 	struct dio_write *dio;
24501c6fdbd8SKent Overstreet 	struct bio *bio;
24517edcfbfeSKent Overstreet 	bool locked = true, extending;
24521c6fdbd8SKent Overstreet 	ssize_t ret;
24531c6fdbd8SKent Overstreet 
24547edcfbfeSKent Overstreet 	prefetch(&c->opts);
24557edcfbfeSKent Overstreet 	prefetch((void *) &c->opts + 64);
24567edcfbfeSKent Overstreet 	prefetch(&inode->ei_inode);
24577edcfbfeSKent Overstreet 	prefetch((void *) &inode->ei_inode + 64);
24581c6fdbd8SKent Overstreet 
24597edcfbfeSKent Overstreet 	inode_lock(&inode->v);
24607edcfbfeSKent Overstreet 
24617edcfbfeSKent Overstreet 	ret = generic_write_checks(req, iter);
24627edcfbfeSKent Overstreet 	if (unlikely(ret <= 0))
24637edcfbfeSKent Overstreet 		goto err;
24647edcfbfeSKent Overstreet 
24657edcfbfeSKent Overstreet 	ret = file_remove_privs(file);
24667edcfbfeSKent Overstreet 	if (unlikely(ret))
24677edcfbfeSKent Overstreet 		goto err;
24687edcfbfeSKent Overstreet 
24697edcfbfeSKent Overstreet 	ret = file_update_time(file);
24707edcfbfeSKent Overstreet 	if (unlikely(ret))
24717edcfbfeSKent Overstreet 		goto err;
24721c6fdbd8SKent Overstreet 
2473919dbbd1SKent Overstreet 	if (unlikely((req->ki_pos|iter->count) & (block_bytes(c) - 1)))
24747edcfbfeSKent Overstreet 		goto err;
24757edcfbfeSKent Overstreet 
24767edcfbfeSKent Overstreet 	inode_dio_begin(&inode->v);
2477a7ecd30cSKent Overstreet 	bch2_pagecache_block_get(inode);
24787edcfbfeSKent Overstreet 
24797edcfbfeSKent Overstreet 	extending = req->ki_pos + iter->count > inode->v.i_size;
24807edcfbfeSKent Overstreet 	if (!extending) {
24817edcfbfeSKent Overstreet 		inode_unlock(&inode->v);
24827edcfbfeSKent Overstreet 		locked = false;
24837edcfbfeSKent Overstreet 	}
24841c6fdbd8SKent Overstreet 
24851c6fdbd8SKent Overstreet 	bio = bio_alloc_bioset(NULL,
24864d126dc8SKent Overstreet 			       bio_iov_vecs_to_alloc(iter, BIO_MAX_VECS),
24871c6fdbd8SKent Overstreet 			       REQ_OP_WRITE,
24881c6fdbd8SKent Overstreet 			       GFP_KERNEL,
24891c6fdbd8SKent Overstreet 			       &c->dio_write_bioset);
24909a3df993SKent Overstreet 	dio = container_of(bio, struct dio_write, op.wbio.bio);
24911c6fdbd8SKent Overstreet 	dio->req		= req;
2492182c7bbfSKent Overstreet 	dio->mapping		= mapping;
2493182c7bbfSKent Overstreet 	dio->inode		= inode;
2494ed484030SKent Overstreet 	dio->mm			= current->mm;
24951c6fdbd8SKent Overstreet 	dio->loop		= false;
24966b1b186aSKent Overstreet 	dio->extending		= extending;
24977edcfbfeSKent Overstreet 	dio->sync		= is_sync_kiocb(req) || extending;
2498a1ee777bSKent Overstreet 	dio->flush		= iocb_is_dsync(req) && !c->opts.journal_flush_disabled;
24991c6fdbd8SKent Overstreet 	dio->free_iov		= false;
25001c6fdbd8SKent Overstreet 	dio->quota_res.sectors	= 0;
2501042a1f26SKent Overstreet 	dio->written		= 0;
25021c6fdbd8SKent Overstreet 	dio->iter		= *iter;
2503182c7bbfSKent Overstreet 	dio->op.c		= c;
25049a3df993SKent Overstreet 
2505a023127aSKent Overstreet 	if (unlikely(mapping->nrpages)) {
250654847d25SKent Overstreet 		ret = write_invalidate_inode_pages_range(mapping,
250754847d25SKent Overstreet 						req->ki_pos,
250854847d25SKent Overstreet 						req->ki_pos + iter->count - 1);
250954847d25SKent Overstreet 		if (unlikely(ret))
251054847d25SKent Overstreet 			goto err_put_bio;
2511a023127aSKent Overstreet 	}
251254847d25SKent Overstreet 
25137edcfbfeSKent Overstreet 	ret = bch2_dio_write_loop(dio);
25141c6fdbd8SKent Overstreet err:
25157edcfbfeSKent Overstreet 	if (locked)
25167edcfbfeSKent Overstreet 		inode_unlock(&inode->v);
25177edcfbfeSKent Overstreet 	return ret;
25187edcfbfeSKent Overstreet err_put_bio:
2519a7ecd30cSKent Overstreet 	bch2_pagecache_block_put(inode);
25201c6fdbd8SKent Overstreet 	bio_put(bio);
25217edcfbfeSKent Overstreet 	inode_dio_end(&inode->v);
25227edcfbfeSKent Overstreet 	goto err;
25231c6fdbd8SKent Overstreet }
25241c6fdbd8SKent Overstreet 
25257edcfbfeSKent Overstreet ssize_t bch2_write_iter(struct kiocb *iocb, struct iov_iter *from)
25261c6fdbd8SKent Overstreet {
25271c6fdbd8SKent Overstreet 	struct file *file = iocb->ki_filp;
25287edcfbfeSKent Overstreet 	struct bch_inode_info *inode = file_bch_inode(file);
25291c6fdbd8SKent Overstreet 	ssize_t	ret;
25301c6fdbd8SKent Overstreet 
25315c1ef830SKent Overstreet 	if (iocb->ki_flags & IOCB_DIRECT) {
25325c1ef830SKent Overstreet 		ret = bch2_direct_write(iocb, from);
25335c1ef830SKent Overstreet 		goto out;
25345c1ef830SKent Overstreet 	}
25351c6fdbd8SKent Overstreet 
25367edcfbfeSKent Overstreet 	inode_lock(&inode->v);
25377edcfbfeSKent Overstreet 
25387edcfbfeSKent Overstreet 	ret = generic_write_checks(iocb, from);
25397edcfbfeSKent Overstreet 	if (ret <= 0)
25407edcfbfeSKent Overstreet 		goto unlock;
25417edcfbfeSKent Overstreet 
25421c6fdbd8SKent Overstreet 	ret = file_remove_privs(file);
25431c6fdbd8SKent Overstreet 	if (ret)
25447edcfbfeSKent Overstreet 		goto unlock;
25451c6fdbd8SKent Overstreet 
25461c6fdbd8SKent Overstreet 	ret = file_update_time(file);
25471c6fdbd8SKent Overstreet 	if (ret)
25487edcfbfeSKent Overstreet 		goto unlock;
25491c6fdbd8SKent Overstreet 
25507edcfbfeSKent Overstreet 	ret = bch2_buffered_write(iocb, from);
25511c6fdbd8SKent Overstreet 	if (likely(ret > 0))
25521c6fdbd8SKent Overstreet 		iocb->ki_pos += ret;
25537edcfbfeSKent Overstreet unlock:
25541c6fdbd8SKent Overstreet 	inode_unlock(&inode->v);
25551c6fdbd8SKent Overstreet 
25567edcfbfeSKent Overstreet 	if (ret > 0)
25571c6fdbd8SKent Overstreet 		ret = generic_write_sync(iocb, ret);
25585c1ef830SKent Overstreet out:
25595c1ef830SKent Overstreet 	return bch2_err_class(ret);
25601c6fdbd8SKent Overstreet }
25611c6fdbd8SKent Overstreet 
25621c6fdbd8SKent Overstreet /* fsync: */
25631c6fdbd8SKent Overstreet 
256468a2054dSKent Overstreet /*
256568a2054dSKent Overstreet  * inode->ei_inode.bi_journal_seq won't be up to date since it's set in an
256668a2054dSKent Overstreet  * insert trigger: look up the btree inode instead
256768a2054dSKent Overstreet  */
2568a8b3a677SKent Overstreet static int bch2_flush_inode(struct bch_fs *c,
2569a8b3a677SKent Overstreet 			    struct bch_inode_info *inode)
257068a2054dSKent Overstreet {
2571a8b3a677SKent Overstreet 	struct bch_inode_unpacked u;
257268a2054dSKent Overstreet 	int ret;
257368a2054dSKent Overstreet 
257468a2054dSKent Overstreet 	if (c->opts.journal_flush_disabled)
257568a2054dSKent Overstreet 		return 0;
257668a2054dSKent Overstreet 
2577a8b3a677SKent Overstreet 	ret = bch2_inode_find_by_inum(c, inode_inum(inode), &u);
257868a2054dSKent Overstreet 	if (ret)
257968a2054dSKent Overstreet 		return ret;
258068a2054dSKent Overstreet 
2581a8b3a677SKent Overstreet 	return bch2_journal_flush_seq(&c->journal, u.bi_journal_seq) ?:
2582a8b3a677SKent Overstreet 		bch2_inode_flush_nocow_writes(c, inode);
258368a2054dSKent Overstreet }
258468a2054dSKent Overstreet 
25851c6fdbd8SKent Overstreet int bch2_fsync(struct file *file, loff_t start, loff_t end, int datasync)
25861c6fdbd8SKent Overstreet {
25871c6fdbd8SKent Overstreet 	struct bch_inode_info *inode = file_bch_inode(file);
25881c6fdbd8SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
258968a2054dSKent Overstreet 	int ret, ret2, ret3;
25901c6fdbd8SKent Overstreet 
25911c6fdbd8SKent Overstreet 	ret = file_write_and_wait_range(file, start, end);
259268a2054dSKent Overstreet 	ret2 = sync_inode_metadata(&inode->v, 1);
2593a8b3a677SKent Overstreet 	ret3 = bch2_flush_inode(c, inode);
25941c6fdbd8SKent Overstreet 
25955c1ef830SKent Overstreet 	return bch2_err_class(ret ?: ret2 ?: ret3);
25961c6fdbd8SKent Overstreet }
25971c6fdbd8SKent Overstreet 
25981c6fdbd8SKent Overstreet /* truncate: */
25991c6fdbd8SKent Overstreet 
26006fed42bbSKent Overstreet static inline int range_has_data(struct bch_fs *c, u32 subvol,
26011c6fdbd8SKent Overstreet 				 struct bpos start,
26021c6fdbd8SKent Overstreet 				 struct bpos end)
26031c6fdbd8SKent Overstreet {
2604424eb881SKent Overstreet 	struct btree_trans trans;
260567e0dd8fSKent Overstreet 	struct btree_iter iter;
26061c6fdbd8SKent Overstreet 	struct bkey_s_c k;
26071c6fdbd8SKent Overstreet 	int ret = 0;
26081c6fdbd8SKent Overstreet 
260920bceecbSKent Overstreet 	bch2_trans_init(&trans, c, 0, 0);
26106fed42bbSKent Overstreet retry:
26116fed42bbSKent Overstreet 	bch2_trans_begin(&trans);
26126fed42bbSKent Overstreet 
26136fed42bbSKent Overstreet 	ret = bch2_subvolume_get_snapshot(&trans, subvol, &start.snapshot);
26146fed42bbSKent Overstreet 	if (ret)
26156fed42bbSKent Overstreet 		goto err;
2616424eb881SKent Overstreet 
2617c72f687aSKent Overstreet 	for_each_btree_key_upto_norestart(&trans, iter, BTREE_ID_extents, start, end, 0, k, ret)
26181c6fdbd8SKent Overstreet 		if (bkey_extent_is_data(k.k)) {
26191c6fdbd8SKent Overstreet 			ret = 1;
26201c6fdbd8SKent Overstreet 			break;
26211c6fdbd8SKent Overstreet 		}
26226fed42bbSKent Overstreet 	start = iter.pos;
262367e0dd8fSKent Overstreet 	bch2_trans_iter_exit(&trans, &iter);
26246fed42bbSKent Overstreet err:
2625549d173cSKent Overstreet 	if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
26266fed42bbSKent Overstreet 		goto retry;
26271c6fdbd8SKent Overstreet 
26289a796fdbSKent Overstreet 	bch2_trans_exit(&trans);
26299a796fdbSKent Overstreet 	return ret;
26301c6fdbd8SKent Overstreet }
26311c6fdbd8SKent Overstreet 
26321c6fdbd8SKent Overstreet static int __bch2_truncate_page(struct bch_inode_info *inode,
26331c6fdbd8SKent Overstreet 				pgoff_t index, loff_t start, loff_t end)
26341c6fdbd8SKent Overstreet {
26351c6fdbd8SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
26361c6fdbd8SKent Overstreet 	struct address_space *mapping = inode->v.i_mapping;
2637a99b1cafSKent Overstreet 	struct bch_page_state *s;
26381c6fdbd8SKent Overstreet 	unsigned start_offset = start & (PAGE_SIZE - 1);
26391c6fdbd8SKent Overstreet 	unsigned end_offset = ((end - 1) & (PAGE_SIZE - 1)) + 1;
2640a99b1cafSKent Overstreet 	unsigned i;
26411c6fdbd8SKent Overstreet 	struct page *page;
2642b19d307dSKent Overstreet 	s64 i_sectors_delta = 0;
26431c6fdbd8SKent Overstreet 	int ret = 0;
26441c6fdbd8SKent Overstreet 
26451c6fdbd8SKent Overstreet 	/* Page boundary? Nothing to do */
26461c6fdbd8SKent Overstreet 	if (!((index == start >> PAGE_SHIFT && start_offset) ||
26471c6fdbd8SKent Overstreet 	      (index == end >> PAGE_SHIFT && end_offset != PAGE_SIZE)))
26481c6fdbd8SKent Overstreet 		return 0;
26491c6fdbd8SKent Overstreet 
26501c6fdbd8SKent Overstreet 	/* Above i_size? */
26511c6fdbd8SKent Overstreet 	if (index << PAGE_SHIFT >= inode->v.i_size)
26521c6fdbd8SKent Overstreet 		return 0;
26531c6fdbd8SKent Overstreet 
26541c6fdbd8SKent Overstreet 	page = find_lock_page(mapping, index);
26551c6fdbd8SKent Overstreet 	if (!page) {
26561c6fdbd8SKent Overstreet 		/*
26571c6fdbd8SKent Overstreet 		 * XXX: we're doing two index lookups when we end up reading the
26581c6fdbd8SKent Overstreet 		 * page
26591c6fdbd8SKent Overstreet 		 */
26606fed42bbSKent Overstreet 		ret = range_has_data(c, inode->ei_subvol,
2661c72f687aSKent Overstreet 				POS(inode->v.i_ino, (index << PAGE_SECTORS_SHIFT)),
2662c72f687aSKent Overstreet 				POS(inode->v.i_ino, (index << PAGE_SECTORS_SHIFT) + PAGE_SECTORS));
26631c6fdbd8SKent Overstreet 		if (ret <= 0)
26641c6fdbd8SKent Overstreet 			return ret;
26651c6fdbd8SKent Overstreet 
26661c6fdbd8SKent Overstreet 		page = find_or_create_page(mapping, index, GFP_KERNEL);
26671c6fdbd8SKent Overstreet 		if (unlikely(!page)) {
26681c6fdbd8SKent Overstreet 			ret = -ENOMEM;
26691c6fdbd8SKent Overstreet 			goto out;
26701c6fdbd8SKent Overstreet 		}
26711c6fdbd8SKent Overstreet 	}
26721c6fdbd8SKent Overstreet 
2673a99b1cafSKent Overstreet 	s = bch2_page_state_create(page, 0);
2674a99b1cafSKent Overstreet 	if (!s) {
2675a99b1cafSKent Overstreet 		ret = -ENOMEM;
2676a99b1cafSKent Overstreet 		goto unlock;
2677a99b1cafSKent Overstreet 	}
2678a99b1cafSKent Overstreet 
26791c6fdbd8SKent Overstreet 	if (!PageUptodate(page)) {
26801c6fdbd8SKent Overstreet 		ret = bch2_read_single_page(page, mapping);
26811c6fdbd8SKent Overstreet 		if (ret)
26821c6fdbd8SKent Overstreet 			goto unlock;
26831c6fdbd8SKent Overstreet 	}
26841c6fdbd8SKent Overstreet 
2685a99b1cafSKent Overstreet 	if (index != start >> PAGE_SHIFT)
2686a99b1cafSKent Overstreet 		start_offset = 0;
2687a99b1cafSKent Overstreet 	if (index != end >> PAGE_SHIFT)
2688a99b1cafSKent Overstreet 		end_offset = PAGE_SIZE;
2689a99b1cafSKent Overstreet 
2690a99b1cafSKent Overstreet 	for (i = round_up(start_offset, block_bytes(c)) >> 9;
2691a99b1cafSKent Overstreet 	     i < round_down(end_offset, block_bytes(c)) >> 9;
2692a99b1cafSKent Overstreet 	     i++) {
2693a99b1cafSKent Overstreet 		s->s[i].nr_replicas	= 0;
2694b19d307dSKent Overstreet 		if (s->s[i].state == SECTOR_DIRTY)
2695b19d307dSKent Overstreet 			i_sectors_delta--;
2696a99b1cafSKent Overstreet 		s->s[i].state		= SECTOR_UNALLOCATED;
2697a99b1cafSKent Overstreet 	}
2698a99b1cafSKent Overstreet 
2699b19d307dSKent Overstreet 	i_sectors_acct(c, inode, NULL, i_sectors_delta);
2700b19d307dSKent Overstreet 
270174163da7SKent Overstreet 	/*
270274163da7SKent Overstreet 	 * Caller needs to know whether this page will be written out by
270374163da7SKent Overstreet 	 * writeback - doing an i_size update if necessary - or whether it will
270474163da7SKent Overstreet 	 * be responsible for the i_size update:
270574163da7SKent Overstreet 	 */
270674163da7SKent Overstreet 	ret = s->s[(min_t(u64, inode->v.i_size - (index << PAGE_SHIFT),
270774163da7SKent Overstreet 			  PAGE_SIZE) - 1) >> 9].state >= SECTOR_DIRTY;
270874163da7SKent Overstreet 
2709a99b1cafSKent Overstreet 	zero_user_segment(page, start_offset, end_offset);
2710a99b1cafSKent Overstreet 
27111c6fdbd8SKent Overstreet 	/*
27121c6fdbd8SKent Overstreet 	 * Bit of a hack - we don't want truncate to fail due to -ENOSPC.
27131c6fdbd8SKent Overstreet 	 *
27141c6fdbd8SKent Overstreet 	 * XXX: because we aren't currently tracking whether the page has actual
27151c6fdbd8SKent Overstreet 	 * data in it (vs. just 0s, or only partially written) this wrong. ick.
27161c6fdbd8SKent Overstreet 	 */
271774163da7SKent Overstreet 	BUG_ON(bch2_get_page_disk_reservation(c, inode, page, false));
27181c6fdbd8SKent Overstreet 
27199ba2eb25SKent Overstreet 	/*
27209ba2eb25SKent Overstreet 	 * This removes any writeable userspace mappings; we need to force
27219ba2eb25SKent Overstreet 	 * .page_mkwrite to be called again before any mmapped writes, to
27229ba2eb25SKent Overstreet 	 * redirty the full page:
27239ba2eb25SKent Overstreet 	 */
27249ba2eb25SKent Overstreet 	page_mkclean(page);
2725d1542e03SKent Overstreet 	filemap_dirty_folio(mapping, page_folio(page));
27261c6fdbd8SKent Overstreet unlock:
27271c6fdbd8SKent Overstreet 	unlock_page(page);
27281c6fdbd8SKent Overstreet 	put_page(page);
27291c6fdbd8SKent Overstreet out:
27301c6fdbd8SKent Overstreet 	return ret;
27311c6fdbd8SKent Overstreet }
27321c6fdbd8SKent Overstreet 
27331c6fdbd8SKent Overstreet static int bch2_truncate_page(struct bch_inode_info *inode, loff_t from)
27341c6fdbd8SKent Overstreet {
27351c6fdbd8SKent Overstreet 	return __bch2_truncate_page(inode, from >> PAGE_SHIFT,
2736a99b1cafSKent Overstreet 				    from, round_up(from, PAGE_SIZE));
27371c6fdbd8SKent Overstreet }
27381c6fdbd8SKent Overstreet 
273974163da7SKent Overstreet static int bch2_truncate_pages(struct bch_inode_info *inode,
274074163da7SKent Overstreet 			       loff_t start, loff_t end)
274174163da7SKent Overstreet {
274274163da7SKent Overstreet 	int ret = __bch2_truncate_page(inode, start >> PAGE_SHIFT,
274374163da7SKent Overstreet 				       start, end);
274474163da7SKent Overstreet 
274574163da7SKent Overstreet 	if (ret >= 0 &&
274674163da7SKent Overstreet 	    start >> PAGE_SHIFT != end >> PAGE_SHIFT)
274774163da7SKent Overstreet 		ret = __bch2_truncate_page(inode,
274874163da7SKent Overstreet 					   end >> PAGE_SHIFT,
274974163da7SKent Overstreet 					   start, end);
275074163da7SKent Overstreet 	return ret;
275174163da7SKent Overstreet }
275274163da7SKent Overstreet 
275368a507a2SKent Overstreet static int bch2_extend(struct mnt_idmap *idmap,
275468a507a2SKent Overstreet 		       struct bch_inode_info *inode,
2755e0541a93SKent Overstreet 		       struct bch_inode_unpacked *inode_u,
2756e0541a93SKent Overstreet 		       struct iattr *iattr)
27571c6fdbd8SKent Overstreet {
27581c6fdbd8SKent Overstreet 	struct address_space *mapping = inode->v.i_mapping;
27591c6fdbd8SKent Overstreet 	int ret;
27601c6fdbd8SKent Overstreet 
2761e0541a93SKent Overstreet 	/*
2762e0541a93SKent Overstreet 	 * sync appends:
27632925fc49SKent Overstreet 	 *
27642925fc49SKent Overstreet 	 * this has to be done _before_ extending i_size:
2765e0541a93SKent Overstreet 	 */
2766e0541a93SKent Overstreet 	ret = filemap_write_and_wait_range(mapping, inode_u->bi_size, S64_MAX);
27671c6fdbd8SKent Overstreet 	if (ret)
27681c6fdbd8SKent Overstreet 		return ret;
27691c6fdbd8SKent Overstreet 
27701c6fdbd8SKent Overstreet 	truncate_setsize(&inode->v, iattr->ia_size);
27711c6fdbd8SKent Overstreet 
277268a507a2SKent Overstreet 	return bch2_setattr_nonsize(idmap, inode, iattr);
27731c6fdbd8SKent Overstreet }
27741c6fdbd8SKent Overstreet 
277554e2264eSKent Overstreet static int bch2_truncate_finish_fn(struct bch_inode_info *inode,
277654e2264eSKent Overstreet 				   struct bch_inode_unpacked *bi,
277754e2264eSKent Overstreet 				   void *p)
277854e2264eSKent Overstreet {
277954e2264eSKent Overstreet 	bi->bi_flags &= ~BCH_INODE_I_SIZE_DIRTY;
278054e2264eSKent Overstreet 	return 0;
278154e2264eSKent Overstreet }
278254e2264eSKent Overstreet 
278354e2264eSKent Overstreet static int bch2_truncate_start_fn(struct bch_inode_info *inode,
278454e2264eSKent Overstreet 				  struct bch_inode_unpacked *bi, void *p)
278554e2264eSKent Overstreet {
278654e2264eSKent Overstreet 	u64 *new_i_size = p;
278754e2264eSKent Overstreet 
278854e2264eSKent Overstreet 	bi->bi_flags |= BCH_INODE_I_SIZE_DIRTY;
278954e2264eSKent Overstreet 	bi->bi_size = *new_i_size;
279054e2264eSKent Overstreet 	return 0;
279154e2264eSKent Overstreet }
279254e2264eSKent Overstreet 
279368a507a2SKent Overstreet int bch2_truncate(struct mnt_idmap *idmap,
279468a507a2SKent Overstreet 		  struct bch_inode_info *inode, struct iattr *iattr)
27951c6fdbd8SKent Overstreet {
27961c6fdbd8SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
27971c6fdbd8SKent Overstreet 	struct address_space *mapping = inode->v.i_mapping;
2798e0541a93SKent Overstreet 	struct bch_inode_unpacked inode_u;
279954e2264eSKent Overstreet 	u64 new_i_size = iattr->ia_size;
28002e87eae1SKent Overstreet 	s64 i_sectors_delta = 0;
28011c6fdbd8SKent Overstreet 	int ret = 0;
28021c6fdbd8SKent Overstreet 
280368a507a2SKent Overstreet 	/*
280478d66ab1SDan Robertson 	 * If the truncate call with change the size of the file, the
280578d66ab1SDan Robertson 	 * cmtimes should be updated. If the size will not change, we
280678d66ab1SDan Robertson 	 * do not need to update the cmtimes.
280768a507a2SKent Overstreet 	 */
280878d66ab1SDan Robertson 	if (iattr->ia_size != inode->v.i_size) {
280968a507a2SKent Overstreet 		if (!(iattr->ia_valid & ATTR_MTIME))
281068a507a2SKent Overstreet 			ktime_get_coarse_real_ts64(&iattr->ia_mtime);
281168a507a2SKent Overstreet 		if (!(iattr->ia_valid & ATTR_CTIME))
281268a507a2SKent Overstreet 			ktime_get_coarse_real_ts64(&iattr->ia_ctime);
281368a507a2SKent Overstreet 		iattr->ia_valid |= ATTR_MTIME|ATTR_CTIME;
281478d66ab1SDan Robertson 	}
281568a507a2SKent Overstreet 
28161c6fdbd8SKent Overstreet 	inode_dio_wait(&inode->v);
2817a7ecd30cSKent Overstreet 	bch2_pagecache_block_get(inode);
28181c6fdbd8SKent Overstreet 
28196fed42bbSKent Overstreet 	ret = bch2_inode_find_by_inum(c, inode_inum(inode), &inode_u);
2820e0541a93SKent Overstreet 	if (ret)
2821e0541a93SKent Overstreet 		goto err;
28221c6fdbd8SKent Overstreet 
2823c45d473dSKent Overstreet 	/*
2824c45d473dSKent Overstreet 	 * check this before next assertion; on filesystem error our normal
2825c45d473dSKent Overstreet 	 * invariants are a bit broken (truncate has to truncate the page cache
2826c45d473dSKent Overstreet 	 * before the inode).
2827c45d473dSKent Overstreet 	 */
2828c45d473dSKent Overstreet 	ret = bch2_journal_error(&c->journal);
2829c45d473dSKent Overstreet 	if (ret)
2830c45d473dSKent Overstreet 		goto err;
2831c45d473dSKent Overstreet 
28328eb71e9eSKent Overstreet 	WARN_ONCE(!test_bit(EI_INODE_ERROR, &inode->ei_flags) &&
28338eb71e9eSKent Overstreet 		  inode->v.i_size < inode_u.bi_size,
28348eb71e9eSKent Overstreet 		  "truncate spotted in mem i_size < btree i_size: %llu < %llu\n",
28358eb71e9eSKent Overstreet 		  (u64) inode->v.i_size, inode_u.bi_size);
2836e0541a93SKent Overstreet 
2837e0541a93SKent Overstreet 	if (iattr->ia_size > inode->v.i_size) {
283868a507a2SKent Overstreet 		ret = bch2_extend(idmap, inode, &inode_u, iattr);
283954e2264eSKent Overstreet 		goto err;
28401c6fdbd8SKent Overstreet 	}
28411c6fdbd8SKent Overstreet 
284268a507a2SKent Overstreet 	iattr->ia_valid &= ~ATTR_SIZE;
284368a507a2SKent Overstreet 
28441c6fdbd8SKent Overstreet 	ret = bch2_truncate_page(inode, iattr->ia_size);
284574163da7SKent Overstreet 	if (unlikely(ret < 0))
284654e2264eSKent Overstreet 		goto err;
28471c6fdbd8SKent Overstreet 
28486cc3535dSKent Overstreet 	/*
28496cc3535dSKent Overstreet 	 * When extending, we're going to write the new i_size to disk
28506cc3535dSKent Overstreet 	 * immediately so we need to flush anything above the current on disk
28516cc3535dSKent Overstreet 	 * i_size first:
28526cc3535dSKent Overstreet 	 *
28536cc3535dSKent Overstreet 	 * Also, when extending we need to flush the page that i_size currently
28546cc3535dSKent Overstreet 	 * straddles - if it's mapped to userspace, we need to ensure that
28556cc3535dSKent Overstreet 	 * userspace has to redirty it and call .mkwrite -> set_page_dirty
28566cc3535dSKent Overstreet 	 * again to allocate the part of the page that was extended.
28576cc3535dSKent Overstreet 	 */
2858e0541a93SKent Overstreet 	if (iattr->ia_size > inode_u.bi_size)
28591c6fdbd8SKent Overstreet 		ret = filemap_write_and_wait_range(mapping,
2860e0541a93SKent Overstreet 				inode_u.bi_size,
28611c6fdbd8SKent Overstreet 				iattr->ia_size - 1);
28621c6fdbd8SKent Overstreet 	else if (iattr->ia_size & (PAGE_SIZE - 1))
28631c6fdbd8SKent Overstreet 		ret = filemap_write_and_wait_range(mapping,
28641c6fdbd8SKent Overstreet 				round_down(iattr->ia_size, PAGE_SIZE),
28651c6fdbd8SKent Overstreet 				iattr->ia_size - 1);
28661c6fdbd8SKent Overstreet 	if (ret)
286754e2264eSKent Overstreet 		goto err;
28681c6fdbd8SKent Overstreet 
286954e2264eSKent Overstreet 	mutex_lock(&inode->ei_update_lock);
287054e2264eSKent Overstreet 	ret = bch2_write_inode(c, inode, bch2_truncate_start_fn,
287154e2264eSKent Overstreet 			       &new_i_size, 0);
287254e2264eSKent Overstreet 	mutex_unlock(&inode->ei_update_lock);
28731c6fdbd8SKent Overstreet 
28741c6fdbd8SKent Overstreet 	if (unlikely(ret))
287554e2264eSKent Overstreet 		goto err;
28761c6fdbd8SKent Overstreet 
28771c6fdbd8SKent Overstreet 	truncate_setsize(&inode->v, iattr->ia_size);
28781c6fdbd8SKent Overstreet 
28798c6d298aSKent Overstreet 	ret = bch2_fpunch(c, inode_inum(inode),
2880a99b1cafSKent Overstreet 			round_up(iattr->ia_size, block_bytes(c)) >> 9,
288168a2054dSKent Overstreet 			U64_MAX, &i_sectors_delta);
28822e87eae1SKent Overstreet 	i_sectors_acct(c, inode, NULL, i_sectors_delta);
28832e87eae1SKent Overstreet 
2884b33bf1bcSKent Overstreet 	bch2_fs_inconsistent_on(!inode->v.i_size && inode->v.i_blocks &&
2885b33bf1bcSKent Overstreet 				!bch2_journal_error(&c->journal), c,
2886b33bf1bcSKent Overstreet 				"inode %lu truncated to 0 but i_blocks %llu (ondisk %lli)",
2887b33bf1bcSKent Overstreet 				inode->v.i_ino, (u64) inode->v.i_blocks,
2888b33bf1bcSKent Overstreet 				inode->ei_inode.bi_sectors);
28891c6fdbd8SKent Overstreet 	if (unlikely(ret))
289054e2264eSKent Overstreet 		goto err;
28911c6fdbd8SKent Overstreet 
289254e2264eSKent Overstreet 	mutex_lock(&inode->ei_update_lock);
289368a507a2SKent Overstreet 	ret = bch2_write_inode(c, inode, bch2_truncate_finish_fn, NULL, 0);
289454e2264eSKent Overstreet 	mutex_unlock(&inode->ei_update_lock);
289568a507a2SKent Overstreet 
289668a507a2SKent Overstreet 	ret = bch2_setattr_nonsize(idmap, inode, iattr);
289754e2264eSKent Overstreet err:
2898a7ecd30cSKent Overstreet 	bch2_pagecache_block_put(inode);
28995c1ef830SKent Overstreet 	return bch2_err_class(ret);
29001c6fdbd8SKent Overstreet }
29011c6fdbd8SKent Overstreet 
29021c6fdbd8SKent Overstreet /* fallocate: */
29031c6fdbd8SKent Overstreet 
2904050197b1SKent Overstreet static int inode_update_times_fn(struct bch_inode_info *inode,
2905050197b1SKent Overstreet 				 struct bch_inode_unpacked *bi, void *p)
2906050197b1SKent Overstreet {
2907050197b1SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
2908050197b1SKent Overstreet 
2909050197b1SKent Overstreet 	bi->bi_mtime = bi->bi_ctime = bch2_current_time(c);
2910050197b1SKent Overstreet 	return 0;
2911050197b1SKent Overstreet }
2912050197b1SKent Overstreet 
29132e87eae1SKent Overstreet static long bchfs_fpunch(struct bch_inode_info *inode, loff_t offset, loff_t len)
29141c6fdbd8SKent Overstreet {
29151c6fdbd8SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
291674163da7SKent Overstreet 	u64 end		= offset + len;
291774163da7SKent Overstreet 	u64 block_start	= round_up(offset, block_bytes(c));
291874163da7SKent Overstreet 	u64 block_end	= round_down(end, block_bytes(c));
291974163da7SKent Overstreet 	bool truncated_last_page;
29201c6fdbd8SKent Overstreet 	int ret = 0;
29211c6fdbd8SKent Overstreet 
292274163da7SKent Overstreet 	ret = bch2_truncate_pages(inode, offset, end);
292374163da7SKent Overstreet 	if (unlikely(ret < 0))
29241c6fdbd8SKent Overstreet 		goto err;
29251c6fdbd8SKent Overstreet 
292674163da7SKent Overstreet 	truncated_last_page = ret;
29271c6fdbd8SKent Overstreet 
292874163da7SKent Overstreet 	truncate_pagecache_range(&inode->v, offset, end - 1);
29291c6fdbd8SKent Overstreet 
293074163da7SKent Overstreet 	if (block_start < block_end) {
29312e87eae1SKent Overstreet 		s64 i_sectors_delta = 0;
29322e87eae1SKent Overstreet 
29338c6d298aSKent Overstreet 		ret = bch2_fpunch(c, inode_inum(inode),
293474163da7SKent Overstreet 				  block_start >> 9, block_end >> 9,
29352e87eae1SKent Overstreet 				  &i_sectors_delta);
29362e87eae1SKent Overstreet 		i_sectors_acct(c, inode, NULL, i_sectors_delta);
29372e87eae1SKent Overstreet 	}
2938050197b1SKent Overstreet 
2939050197b1SKent Overstreet 	mutex_lock(&inode->ei_update_lock);
294074163da7SKent Overstreet 	if (end >= inode->v.i_size && !truncated_last_page) {
294174163da7SKent Overstreet 		ret = bch2_write_inode_size(c, inode, inode->v.i_size,
294274163da7SKent Overstreet 					    ATTR_MTIME|ATTR_CTIME);
294374163da7SKent Overstreet 	} else {
2944050197b1SKent Overstreet 		ret = bch2_write_inode(c, inode, inode_update_times_fn, NULL,
294574163da7SKent Overstreet 				       ATTR_MTIME|ATTR_CTIME);
294674163da7SKent Overstreet 	}
2947050197b1SKent Overstreet 	mutex_unlock(&inode->ei_update_lock);
29481c6fdbd8SKent Overstreet err:
29491c6fdbd8SKent Overstreet 	return ret;
29501c6fdbd8SKent Overstreet }
29511c6fdbd8SKent Overstreet 
29522e87eae1SKent Overstreet static long bchfs_fcollapse_finsert(struct bch_inode_info *inode,
29535f786787SKent Overstreet 				   loff_t offset, loff_t len,
29545f786787SKent Overstreet 				   bool insert)
29551c6fdbd8SKent Overstreet {
29561c6fdbd8SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
29571c6fdbd8SKent Overstreet 	struct address_space *mapping = inode->v.i_mapping;
295807a1006aSKent Overstreet 	struct bkey_buf copy;
2959d69f41d6SKent Overstreet 	struct btree_trans trans;
296067e0dd8fSKent Overstreet 	struct btree_iter src, dst, del;
29615f786787SKent Overstreet 	loff_t shift, new_size;
29625f786787SKent Overstreet 	u64 src_start;
296350dc0f69SKent Overstreet 	int ret = 0;
29641c6fdbd8SKent Overstreet 
29651c6fdbd8SKent Overstreet 	if ((offset | len) & (block_bytes(c) - 1))
29661c6fdbd8SKent Overstreet 		return -EINVAL;
29671c6fdbd8SKent Overstreet 
29685f786787SKent Overstreet 	if (insert) {
29695f786787SKent Overstreet 		if (inode->v.i_sb->s_maxbytes - inode->v.i_size < len)
297074163da7SKent Overstreet 			return -EFBIG;
29715f786787SKent Overstreet 
29725f786787SKent Overstreet 		if (offset >= inode->v.i_size)
297374163da7SKent Overstreet 			return -EINVAL;
29745f786787SKent Overstreet 
29755f786787SKent Overstreet 		src_start	= U64_MAX;
29765f786787SKent Overstreet 		shift		= len;
29775f786787SKent Overstreet 	} else {
29781c6fdbd8SKent Overstreet 		if (offset + len >= inode->v.i_size)
297974163da7SKent Overstreet 			return -EINVAL;
29801c6fdbd8SKent Overstreet 
29815f786787SKent Overstreet 		src_start	= offset + len;
29825f786787SKent Overstreet 		shift		= -len;
29835f786787SKent Overstreet 	}
29841c6fdbd8SKent Overstreet 
29855f786787SKent Overstreet 	new_size = inode->v.i_size + shift;
29861c6fdbd8SKent Overstreet 
29871c6fdbd8SKent Overstreet 	ret = write_invalidate_inode_pages_range(mapping, offset, LLONG_MAX);
29881c6fdbd8SKent Overstreet 	if (ret)
298974163da7SKent Overstreet 		return ret;
29901c6fdbd8SKent Overstreet 
29915f786787SKent Overstreet 	if (insert) {
29925f786787SKent Overstreet 		i_size_write(&inode->v, new_size);
29935f786787SKent Overstreet 		mutex_lock(&inode->ei_update_lock);
29945f786787SKent Overstreet 		ret = bch2_write_inode_size(c, inode, new_size,
29955f786787SKent Overstreet 					    ATTR_MTIME|ATTR_CTIME);
29965f786787SKent Overstreet 		mutex_unlock(&inode->ei_update_lock);
29975f786787SKent Overstreet 	} else {
29982e87eae1SKent Overstreet 		s64 i_sectors_delta = 0;
29992e87eae1SKent Overstreet 
30008c6d298aSKent Overstreet 		ret = bch2_fpunch(c, inode_inum(inode),
30012e87eae1SKent Overstreet 				  offset >> 9, (offset + len) >> 9,
30022e87eae1SKent Overstreet 				  &i_sectors_delta);
30032e87eae1SKent Overstreet 		i_sectors_acct(c, inode, NULL, i_sectors_delta);
30042e87eae1SKent Overstreet 
300563095894SKent Overstreet 		if (ret)
300674163da7SKent Overstreet 			return ret;
30075f786787SKent Overstreet 	}
30088ef231bdSKent Overstreet 
300950dc0f69SKent Overstreet 	bch2_bkey_buf_init(&copy);
3010f7beb4caSKent Overstreet 	bch2_trans_init(&trans, c, BTREE_ITER_MAX, 1024);
301167e0dd8fSKent Overstreet 	bch2_trans_iter_init(&trans, &src, BTREE_ID_extents,
30125f786787SKent Overstreet 			POS(inode->v.i_ino, src_start >> 9),
301363095894SKent Overstreet 			BTREE_ITER_INTENT);
301467e0dd8fSKent Overstreet 	bch2_trans_copy_iter(&dst, &src);
301567e0dd8fSKent Overstreet 	bch2_trans_copy_iter(&del, &src);
30165f786787SKent Overstreet 
3017549d173cSKent Overstreet 	while (ret == 0 ||
3018549d173cSKent Overstreet 	       bch2_err_matches(ret, BCH_ERR_transaction_restart)) {
301963095894SKent Overstreet 		struct disk_reservation disk_res =
302063095894SKent Overstreet 			bch2_disk_reservation_init(c, 0);
302163095894SKent Overstreet 		struct bkey_i delete;
302263095894SKent Overstreet 		struct bkey_s_c k;
302363095894SKent Overstreet 		struct bpos next_pos;
30245f786787SKent Overstreet 		struct bpos move_pos = POS(inode->v.i_ino, offset >> 9);
30255f786787SKent Overstreet 		struct bpos atomic_end;
30262d594dfbSKent Overstreet 		unsigned trigger_flags = 0;
30276fed42bbSKent Overstreet 		u32 snapshot;
30286fed42bbSKent Overstreet 
30296fed42bbSKent Overstreet 		bch2_trans_begin(&trans);
30306fed42bbSKent Overstreet 
30316fed42bbSKent Overstreet 		ret = bch2_subvolume_get_snapshot(&trans,
30326fed42bbSKent Overstreet 					inode->ei_subvol, &snapshot);
30336fed42bbSKent Overstreet 		if (ret)
30346fed42bbSKent Overstreet 			continue;
30356fed42bbSKent Overstreet 
30366fed42bbSKent Overstreet 		bch2_btree_iter_set_snapshot(&src, snapshot);
30376fed42bbSKent Overstreet 		bch2_btree_iter_set_snapshot(&dst, snapshot);
30386fed42bbSKent Overstreet 		bch2_btree_iter_set_snapshot(&del, snapshot);
303963095894SKent Overstreet 
3040700c25b3SKent Overstreet 		bch2_trans_begin(&trans);
3041700c25b3SKent Overstreet 
30425f786787SKent Overstreet 		k = insert
304367e0dd8fSKent Overstreet 			? bch2_btree_iter_peek_prev(&src)
3044c72f687aSKent Overstreet 			: bch2_btree_iter_peek_upto(&src, POS(inode->v.i_ino, U64_MAX));
304563095894SKent Overstreet 		if ((ret = bkey_err(k)))
304650dc0f69SKent Overstreet 			continue;
304763095894SKent Overstreet 
304863095894SKent Overstreet 		if (!k.k || k.k->p.inode != inode->v.i_ino)
304963095894SKent Overstreet 			break;
305063095894SKent Overstreet 
30515f786787SKent Overstreet 		if (insert &&
3052e88a75ebSKent Overstreet 		    bkey_le(k.k->p, POS(inode->v.i_ino, offset >> 9)))
30535f786787SKent Overstreet 			break;
30545f786787SKent Overstreet reassemble:
305507a1006aSKent Overstreet 		bch2_bkey_buf_reassemble(&copy, c, k);
30565f786787SKent Overstreet 
30575f786787SKent Overstreet 		if (insert &&
3058e88a75ebSKent Overstreet 		    bkey_lt(bkey_start_pos(k.k), move_pos))
305935189e09SKent Overstreet 			bch2_cut_front(move_pos, copy.k);
30605f786787SKent Overstreet 
306135189e09SKent Overstreet 		copy.k->k.p.offset += shift >> 9;
306267e0dd8fSKent Overstreet 		bch2_btree_iter_set_pos(&dst, bkey_start_pos(&copy.k->k));
30631c6fdbd8SKent Overstreet 
306467e0dd8fSKent Overstreet 		ret = bch2_extent_atomic_end(&trans, &dst, copy.k, &atomic_end);
30653c7f3b7aSKent Overstreet 		if (ret)
306650dc0f69SKent Overstreet 			continue;
3067e2d9912cSKent Overstreet 
3068e88a75ebSKent Overstreet 		if (!bkey_eq(atomic_end, copy.k->k.p)) {
30695f786787SKent Overstreet 			if (insert) {
30705f786787SKent Overstreet 				move_pos = atomic_end;
30715f786787SKent Overstreet 				move_pos.offset -= shift >> 9;
30725f786787SKent Overstreet 				goto reassemble;
30735f786787SKent Overstreet 			} else {
3074085ab693SKent Overstreet 				bch2_cut_back(atomic_end, copy.k);
30755f786787SKent Overstreet 			}
30765f786787SKent Overstreet 		}
30775f786787SKent Overstreet 
307863095894SKent Overstreet 		bkey_init(&delete.k);
3079283eda57SKent Overstreet 		delete.k.p = copy.k->k.p;
3080283eda57SKent Overstreet 		delete.k.size = copy.k->k.size;
3081283eda57SKent Overstreet 		delete.k.p.offset -= shift >> 9;
308267e0dd8fSKent Overstreet 		bch2_btree_iter_set_pos(&del, bkey_start_pos(&delete.k));
30831c6fdbd8SKent Overstreet 
30845f786787SKent Overstreet 		next_pos = insert ? bkey_start_pos(&delete.k) : delete.k.p;
308563095894SKent Overstreet 
30867c4ca54aSKent Overstreet 		if (copy.k->k.size != k.k->size) {
308763095894SKent Overstreet 			/* We might end up splitting compressed extents: */
308863095894SKent Overstreet 			unsigned nr_ptrs =
30894de77495SKent Overstreet 				bch2_bkey_nr_ptrs_allocated(bkey_i_to_s_c(copy.k));
309063095894SKent Overstreet 
309163095894SKent Overstreet 			ret = bch2_disk_reservation_get(c, &disk_res,
309235189e09SKent Overstreet 					copy.k->k.size, nr_ptrs,
30931c6fdbd8SKent Overstreet 					BCH_DISK_RESERVATION_NOFAIL);
30941c6fdbd8SKent Overstreet 			BUG_ON(ret);
309563095894SKent Overstreet 		}
30961c6fdbd8SKent Overstreet 
309767e0dd8fSKent Overstreet 		ret =   bch2_btree_iter_traverse(&del) ?:
309867e0dd8fSKent Overstreet 			bch2_trans_update(&trans, &del, &delete, trigger_flags) ?:
309967e0dd8fSKent Overstreet 			bch2_trans_update(&trans, &dst, copy.k, trigger_flags) ?:
310068a2054dSKent Overstreet 			bch2_trans_commit(&trans, &disk_res, NULL,
31012d594dfbSKent Overstreet 					  BTREE_INSERT_NOFAIL);
31021c6fdbd8SKent Overstreet 		bch2_disk_reservation_put(c, &disk_res);
310350dc0f69SKent Overstreet 
310463095894SKent Overstreet 		if (!ret)
310567e0dd8fSKent Overstreet 			bch2_btree_iter_set_pos(&src, next_pos);
310650dc0f69SKent Overstreet 	}
310767e0dd8fSKent Overstreet 	bch2_trans_iter_exit(&trans, &del);
310867e0dd8fSKent Overstreet 	bch2_trans_iter_exit(&trans, &dst);
310967e0dd8fSKent Overstreet 	bch2_trans_iter_exit(&trans, &src);
311050dc0f69SKent Overstreet 	bch2_trans_exit(&trans);
311150dc0f69SKent Overstreet 	bch2_bkey_buf_exit(&copy, c);
311263095894SKent Overstreet 
31138ef231bdSKent Overstreet 	if (ret)
311474163da7SKent Overstreet 		return ret;
31151c6fdbd8SKent Overstreet 
311674163da7SKent Overstreet 	mutex_lock(&inode->ei_update_lock);
31175f786787SKent Overstreet 	if (!insert) {
31188ef231bdSKent Overstreet 		i_size_write(&inode->v, new_size);
31198ef231bdSKent Overstreet 		ret = bch2_write_inode_size(c, inode, new_size,
31208ef231bdSKent Overstreet 					    ATTR_MTIME|ATTR_CTIME);
312174163da7SKent Overstreet 	} else {
312274163da7SKent Overstreet 		/* We need an inode update to update bi_journal_seq for fsync: */
312374163da7SKent Overstreet 		ret = bch2_write_inode(c, inode, inode_update_times_fn, NULL,
312474163da7SKent Overstreet 				       ATTR_MTIME|ATTR_CTIME);
31255f786787SKent Overstreet 	}
312674163da7SKent Overstreet 	mutex_unlock(&inode->ei_update_lock);
31271c6fdbd8SKent Overstreet 	return ret;
31281c6fdbd8SKent Overstreet }
31291c6fdbd8SKent Overstreet 
3130694015c2SKent Overstreet static int __bchfs_fallocate(struct bch_inode_info *inode, int mode,
3131694015c2SKent Overstreet 			     u64 start_sector, u64 end_sector)
31321c6fdbd8SKent Overstreet {
31331c6fdbd8SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
3134190fa7afSKent Overstreet 	struct btree_trans trans;
313567e0dd8fSKent Overstreet 	struct btree_iter iter;
3136694015c2SKent Overstreet 	struct bpos end_pos = POS(inode->v.i_ino, end_sector);
313701ad6737SKent Overstreet 	struct bch_io_opts opts;
3138694015c2SKent Overstreet 	int ret = 0;
31391c6fdbd8SKent Overstreet 
314001ad6737SKent Overstreet 	bch2_inode_opts_get(&opts, c, &inode->ei_inode);
3141f7beb4caSKent Overstreet 	bch2_trans_init(&trans, c, BTREE_ITER_MAX, 512);
31421c6fdbd8SKent Overstreet 
314367e0dd8fSKent Overstreet 	bch2_trans_iter_init(&trans, &iter, BTREE_ID_extents,
3144694015c2SKent Overstreet 			POS(inode->v.i_ino, start_sector),
3145190fa7afSKent Overstreet 			BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
31461c6fdbd8SKent Overstreet 
3147e88a75ebSKent Overstreet 	while (!ret && bkey_lt(iter.pos, end_pos)) {
31482e87eae1SKent Overstreet 		s64 i_sectors_delta = 0;
3149190fa7afSKent Overstreet 		struct quota_res quota_res = { 0 };
31501c6fdbd8SKent Overstreet 		struct bkey_s_c k;
3151694015c2SKent Overstreet 		unsigned sectors;
31526fed42bbSKent Overstreet 		u32 snapshot;
31531c6fdbd8SKent Overstreet 
3154163e885aSKent Overstreet 		bch2_trans_begin(&trans);
3155a8abd3a7SKent Overstreet 
31566fed42bbSKent Overstreet 		ret = bch2_subvolume_get_snapshot(&trans,
31576fed42bbSKent Overstreet 					inode->ei_subvol, &snapshot);
31586fed42bbSKent Overstreet 		if (ret)
31596fed42bbSKent Overstreet 			goto bkey_err;
31606fed42bbSKent Overstreet 
31616fed42bbSKent Overstreet 		bch2_btree_iter_set_snapshot(&iter, snapshot);
31626fed42bbSKent Overstreet 
316367e0dd8fSKent Overstreet 		k = bch2_btree_iter_peek_slot(&iter);
31640f238367SKent Overstreet 		if ((ret = bkey_err(k)))
31650f238367SKent Overstreet 			goto bkey_err;
31661c6fdbd8SKent Overstreet 
31671c6fdbd8SKent Overstreet 		/* already reserved */
316879203111SKent Overstreet 		if (bkey_extent_is_reservation(k) &&
316979203111SKent Overstreet 		    bch2_bkey_nr_ptrs_fully_allocated(k) >= opts.data_replicas) {
317067e0dd8fSKent Overstreet 			bch2_btree_iter_advance(&iter);
31711c6fdbd8SKent Overstreet 			continue;
31721c6fdbd8SKent Overstreet 		}
31731c6fdbd8SKent Overstreet 
3174190fa7afSKent Overstreet 		if (bkey_extent_is_data(k.k) &&
3175190fa7afSKent Overstreet 		    !(mode & FALLOC_FL_ZERO_RANGE)) {
317667e0dd8fSKent Overstreet 			bch2_btree_iter_advance(&iter);
31771c6fdbd8SKent Overstreet 			continue;
31781c6fdbd8SKent Overstreet 		}
31791c6fdbd8SKent Overstreet 
3180a8b3a677SKent Overstreet 		/*
3181a8b3a677SKent Overstreet 		 * XXX: for nocow mode, we should promote shared extents to
3182a8b3a677SKent Overstreet 		 * unshared here
3183a8b3a677SKent Overstreet 		 */
3184a8b3a677SKent Overstreet 
318570de7a47SKent Overstreet 		sectors = bpos_min(k.k->p, end_pos).offset - iter.pos.offset;
31861c6fdbd8SKent Overstreet 
31871c6fdbd8SKent Overstreet 		if (!bkey_extent_is_allocation(k.k)) {
31881c6fdbd8SKent Overstreet 			ret = bch2_quota_reservation_add(c, inode,
3189190fa7afSKent Overstreet 					&quota_res,
31901c6fdbd8SKent Overstreet 					sectors, true);
31911c6fdbd8SKent Overstreet 			if (unlikely(ret))
31920f238367SKent Overstreet 				goto bkey_err;
31931c6fdbd8SKent Overstreet 		}
31941c6fdbd8SKent Overstreet 
319570de7a47SKent Overstreet 		ret = bch2_extent_fallocate(&trans, inode_inum(inode), &iter,
319670de7a47SKent Overstreet 					    sectors, opts, &i_sectors_delta,
319770de7a47SKent Overstreet 					    writepoint_hashed((unsigned long) current));
31988810386fSKent Overstreet 		if (ret)
31998810386fSKent Overstreet 			goto bkey_err;
320070de7a47SKent Overstreet 
32012e87eae1SKent Overstreet 		i_sectors_acct(c, inode, &quota_res, i_sectors_delta);
32020f238367SKent Overstreet bkey_err:
3203190fa7afSKent Overstreet 		bch2_quota_reservation_put(c, inode, &quota_res);
3204549d173cSKent Overstreet 		if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
32051c6fdbd8SKent Overstreet 			ret = 0;
320650dc0f69SKent Overstreet 	}
320774163da7SKent Overstreet 
3208dcfc593fSKent Overstreet 	bch2_trans_unlock(&trans); /* lock ordering, before taking pagecache locks: */
3209dcfc593fSKent Overstreet 	mark_pagecache_reserved(inode, start_sector, iter.pos.offset);
3210dcfc593fSKent Overstreet 
3211098ef98dSKent Overstreet 	if (bch2_err_matches(ret, ENOSPC) && (mode & FALLOC_FL_ZERO_RANGE)) {
321274163da7SKent Overstreet 		struct quota_res quota_res = { 0 };
321374163da7SKent Overstreet 		s64 i_sectors_delta = 0;
321474163da7SKent Overstreet 
321574163da7SKent Overstreet 		bch2_fpunch_at(&trans, &iter, inode_inum(inode),
321674163da7SKent Overstreet 			       end_sector, &i_sectors_delta);
321774163da7SKent Overstreet 		i_sectors_acct(c, inode, &quota_res, i_sectors_delta);
321874163da7SKent Overstreet 		bch2_quota_reservation_put(c, inode, &quota_res);
321974163da7SKent Overstreet 	}
322074163da7SKent Overstreet 
322167e0dd8fSKent Overstreet 	bch2_trans_iter_exit(&trans, &iter);
3222694015c2SKent Overstreet 	bch2_trans_exit(&trans);
3223694015c2SKent Overstreet 	return ret;
3224694015c2SKent Overstreet }
322550dc0f69SKent Overstreet 
3226694015c2SKent Overstreet static long bchfs_fallocate(struct bch_inode_info *inode, int mode,
3227694015c2SKent Overstreet 			    loff_t offset, loff_t len)
3228694015c2SKent Overstreet {
3229694015c2SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
323074163da7SKent Overstreet 	u64 end		= offset + len;
323174163da7SKent Overstreet 	u64 block_start	= round_down(offset,	block_bytes(c));
323274163da7SKent Overstreet 	u64 block_end	= round_up(end,		block_bytes(c));
323374163da7SKent Overstreet 	bool truncated_last_page = false;
323474163da7SKent Overstreet 	int ret, ret2 = 0;
3235694015c2SKent Overstreet 
3236694015c2SKent Overstreet 	if (!(mode & FALLOC_FL_KEEP_SIZE) && end > inode->v.i_size) {
3237694015c2SKent Overstreet 		ret = inode_newsize_ok(&inode->v, end);
3238694015c2SKent Overstreet 		if (ret)
323974163da7SKent Overstreet 			return ret;
3240694015c2SKent Overstreet 	}
3241694015c2SKent Overstreet 
3242694015c2SKent Overstreet 	if (mode & FALLOC_FL_ZERO_RANGE) {
324374163da7SKent Overstreet 		ret = bch2_truncate_pages(inode, offset, end);
324474163da7SKent Overstreet 		if (unlikely(ret < 0))
324574163da7SKent Overstreet 			return ret;
3246694015c2SKent Overstreet 
324774163da7SKent Overstreet 		truncated_last_page = ret;
3248694015c2SKent Overstreet 
3249694015c2SKent Overstreet 		truncate_pagecache_range(&inode->v, offset, end - 1);
325074163da7SKent Overstreet 
325174163da7SKent Overstreet 		block_start	= round_up(offset,	block_bytes(c));
325274163da7SKent Overstreet 		block_end	= round_down(end,	block_bytes(c));
3253694015c2SKent Overstreet 	}
3254694015c2SKent Overstreet 
3255694015c2SKent Overstreet 	ret = __bchfs_fallocate(inode, mode, block_start >> 9, block_end >> 9);
3256e0541a93SKent Overstreet 
3257e0541a93SKent Overstreet 	/*
325874163da7SKent Overstreet 	 * On -ENOSPC in ZERO_RANGE mode, we still want to do the inode update,
325974163da7SKent Overstreet 	 * so that the VFS cache i_size is consistent with the btree i_size:
3260e0541a93SKent Overstreet 	 */
326174163da7SKent Overstreet 	if (ret &&
3262098ef98dSKent Overstreet 	    !(bch2_err_matches(ret, ENOSPC) && (mode & FALLOC_FL_ZERO_RANGE)))
326374163da7SKent Overstreet 		return ret;
32641c6fdbd8SKent Overstreet 
326574163da7SKent Overstreet 	if (mode & FALLOC_FL_KEEP_SIZE && end > inode->v.i_size)
3266e0541a93SKent Overstreet 		end = inode->v.i_size;
326774163da7SKent Overstreet 
326874163da7SKent Overstreet 	if (end >= inode->v.i_size &&
326974163da7SKent Overstreet 	    (((mode & FALLOC_FL_ZERO_RANGE) && !truncated_last_page) ||
327074163da7SKent Overstreet 	     !(mode & FALLOC_FL_KEEP_SIZE))) {
327174163da7SKent Overstreet 		spin_lock(&inode->v.i_lock);
3272e0541a93SKent Overstreet 		i_size_write(&inode->v, end);
327374163da7SKent Overstreet 		spin_unlock(&inode->v.i_lock);
3274e0541a93SKent Overstreet 
32751c6fdbd8SKent Overstreet 		mutex_lock(&inode->ei_update_lock);
327674163da7SKent Overstreet 		ret2 = bch2_write_inode_size(c, inode, end, 0);
32771c6fdbd8SKent Overstreet 		mutex_unlock(&inode->ei_update_lock);
32781c6fdbd8SKent Overstreet 	}
327974163da7SKent Overstreet 
328074163da7SKent Overstreet 	return ret ?: ret2;
32811c6fdbd8SKent Overstreet }
32821c6fdbd8SKent Overstreet 
32831c6fdbd8SKent Overstreet long bch2_fallocate_dispatch(struct file *file, int mode,
32841c6fdbd8SKent Overstreet 			     loff_t offset, loff_t len)
32851c6fdbd8SKent Overstreet {
32861c6fdbd8SKent Overstreet 	struct bch_inode_info *inode = file_bch_inode(file);
32872a9101a9SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
32882a9101a9SKent Overstreet 	long ret;
32892a9101a9SKent Overstreet 
3290d94189adSKent Overstreet 	if (!bch2_write_ref_tryget(c, BCH_WRITE_REF_fallocate))
32912a9101a9SKent Overstreet 		return -EROFS;
32921c6fdbd8SKent Overstreet 
329374163da7SKent Overstreet 	inode_lock(&inode->v);
329474163da7SKent Overstreet 	inode_dio_wait(&inode->v);
3295a7ecd30cSKent Overstreet 	bch2_pagecache_block_get(inode);
329674163da7SKent Overstreet 
329707bfcc0bSKent Overstreet 	ret = file_modified(file);
329807bfcc0bSKent Overstreet 	if (ret)
329907bfcc0bSKent Overstreet 		goto err;
330007bfcc0bSKent Overstreet 
33011c6fdbd8SKent Overstreet 	if (!(mode & ~(FALLOC_FL_KEEP_SIZE|FALLOC_FL_ZERO_RANGE)))
33022a9101a9SKent Overstreet 		ret = bchfs_fallocate(inode, mode, offset, len);
33032a9101a9SKent Overstreet 	else if (mode == (FALLOC_FL_PUNCH_HOLE|FALLOC_FL_KEEP_SIZE))
33042a9101a9SKent Overstreet 		ret = bchfs_fpunch(inode, offset, len);
33052a9101a9SKent Overstreet 	else if (mode == FALLOC_FL_INSERT_RANGE)
33062a9101a9SKent Overstreet 		ret = bchfs_fcollapse_finsert(inode, offset, len, true);
33072a9101a9SKent Overstreet 	else if (mode == FALLOC_FL_COLLAPSE_RANGE)
33082a9101a9SKent Overstreet 		ret = bchfs_fcollapse_finsert(inode, offset, len, false);
33092a9101a9SKent Overstreet 	else
33102a9101a9SKent Overstreet 		ret = -EOPNOTSUPP;
331107bfcc0bSKent Overstreet err:
3312a7ecd30cSKent Overstreet 	bch2_pagecache_block_put(inode);
331374163da7SKent Overstreet 	inode_unlock(&inode->v);
3314d94189adSKent Overstreet 	bch2_write_ref_put(c, BCH_WRITE_REF_fallocate);
33151c6fdbd8SKent Overstreet 
33165c1ef830SKent Overstreet 	return bch2_err_class(ret);
33171c6fdbd8SKent Overstreet }
33181c6fdbd8SKent Overstreet 
3319c72f687aSKent Overstreet /*
3320c72f687aSKent Overstreet  * Take a quota reservation for unallocated blocks in a given file range
3321c72f687aSKent Overstreet  * Does not check pagecache
3322c72f687aSKent Overstreet  */
3323e8540e56SKent Overstreet static int quota_reserve_range(struct bch_inode_info *inode,
3324e8540e56SKent Overstreet 			       struct quota_res *res,
3325e8540e56SKent Overstreet 			       u64 start, u64 end)
3326e8540e56SKent Overstreet {
3327e8540e56SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
3328e8540e56SKent Overstreet 	struct btree_trans trans;
3329e8540e56SKent Overstreet 	struct btree_iter iter;
3330e8540e56SKent Overstreet 	struct bkey_s_c k;
3331e8540e56SKent Overstreet 	u32 snapshot;
3332e8540e56SKent Overstreet 	u64 sectors = end - start;
3333e8540e56SKent Overstreet 	u64 pos = start;
3334e8540e56SKent Overstreet 	int ret;
3335e8540e56SKent Overstreet 
3336e8540e56SKent Overstreet 	bch2_trans_init(&trans, c, 0, 0);
3337e8540e56SKent Overstreet retry:
3338e8540e56SKent Overstreet 	bch2_trans_begin(&trans);
3339e8540e56SKent Overstreet 
3340e8540e56SKent Overstreet 	ret = bch2_subvolume_get_snapshot(&trans, inode->ei_subvol, &snapshot);
3341e8540e56SKent Overstreet 	if (ret)
3342e8540e56SKent Overstreet 		goto err;
3343e8540e56SKent Overstreet 
3344e8540e56SKent Overstreet 	bch2_trans_iter_init(&trans, &iter, BTREE_ID_extents,
3345e8540e56SKent Overstreet 			     SPOS(inode->v.i_ino, pos, snapshot), 0);
3346e8540e56SKent Overstreet 
3347e8540e56SKent Overstreet 	while (!(ret = btree_trans_too_many_iters(&trans)) &&
3348e8540e56SKent Overstreet 	       (k = bch2_btree_iter_peek_upto(&iter, POS(inode->v.i_ino, end - 1))).k &&
3349e8540e56SKent Overstreet 	       !(ret = bkey_err(k))) {
3350e8540e56SKent Overstreet 		if (bkey_extent_is_allocation(k.k)) {
3351e8540e56SKent Overstreet 			u64 s = min(end, k.k->p.offset) -
3352e8540e56SKent Overstreet 				max(start, bkey_start_offset(k.k));
3353e8540e56SKent Overstreet 			BUG_ON(s > sectors);
3354e8540e56SKent Overstreet 			sectors -= s;
3355e8540e56SKent Overstreet 		}
3356e8540e56SKent Overstreet 		bch2_btree_iter_advance(&iter);
3357e8540e56SKent Overstreet 	}
3358e8540e56SKent Overstreet 	pos = iter.pos.offset;
3359e8540e56SKent Overstreet 	bch2_trans_iter_exit(&trans, &iter);
3360e8540e56SKent Overstreet err:
3361e8540e56SKent Overstreet 	if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
3362e8540e56SKent Overstreet 		goto retry;
3363e8540e56SKent Overstreet 
3364e8540e56SKent Overstreet 	bch2_trans_exit(&trans);
3365e8540e56SKent Overstreet 
3366e8540e56SKent Overstreet 	if (ret)
3367e8540e56SKent Overstreet 		return ret;
3368e8540e56SKent Overstreet 
3369e8540e56SKent Overstreet 	return bch2_quota_reservation_add(c, inode, res, sectors, true);
3370e8540e56SKent Overstreet }
3371e8540e56SKent Overstreet 
337276426098SKent Overstreet loff_t bch2_remap_file_range(struct file *file_src, loff_t pos_src,
337376426098SKent Overstreet 			     struct file *file_dst, loff_t pos_dst,
337476426098SKent Overstreet 			     loff_t len, unsigned remap_flags)
337576426098SKent Overstreet {
337676426098SKent Overstreet 	struct bch_inode_info *src = file_bch_inode(file_src);
337776426098SKent Overstreet 	struct bch_inode_info *dst = file_bch_inode(file_dst);
337876426098SKent Overstreet 	struct bch_fs *c = src->v.i_sb->s_fs_info;
3379e8540e56SKent Overstreet 	struct quota_res quota_res = { 0 };
33802e87eae1SKent Overstreet 	s64 i_sectors_delta = 0;
3381677fc056SKent Overstreet 	u64 aligned_len;
338276426098SKent Overstreet 	loff_t ret = 0;
338376426098SKent Overstreet 
338476426098SKent Overstreet 	if (remap_flags & ~(REMAP_FILE_DEDUP|REMAP_FILE_ADVISORY))
338576426098SKent Overstreet 		return -EINVAL;
338676426098SKent Overstreet 
338776426098SKent Overstreet 	if (remap_flags & REMAP_FILE_DEDUP)
338876426098SKent Overstreet 		return -EOPNOTSUPP;
338976426098SKent Overstreet 
339076426098SKent Overstreet 	if ((pos_src & (block_bytes(c) - 1)) ||
339176426098SKent Overstreet 	    (pos_dst & (block_bytes(c) - 1)))
339276426098SKent Overstreet 		return -EINVAL;
339376426098SKent Overstreet 
339476426098SKent Overstreet 	if (src == dst &&
339576426098SKent Overstreet 	    abs(pos_src - pos_dst) < len)
339676426098SKent Overstreet 		return -EINVAL;
339776426098SKent Overstreet 
339876426098SKent Overstreet 	bch2_lock_inodes(INODE_LOCK|INODE_PAGECACHE_BLOCK, src, dst);
339976426098SKent Overstreet 
340076426098SKent Overstreet 	inode_dio_wait(&src->v);
340176426098SKent Overstreet 	inode_dio_wait(&dst->v);
340276426098SKent Overstreet 
340376426098SKent Overstreet 	ret = generic_remap_file_range_prep(file_src, pos_src,
340476426098SKent Overstreet 					    file_dst, pos_dst,
340576426098SKent Overstreet 					    &len, remap_flags);
340676426098SKent Overstreet 	if (ret < 0 || len == 0)
34072e87eae1SKent Overstreet 		goto err;
340876426098SKent Overstreet 
3409677fc056SKent Overstreet 	aligned_len = round_up((u64) len, block_bytes(c));
341076426098SKent Overstreet 
341176426098SKent Overstreet 	ret = write_invalidate_inode_pages_range(dst->v.i_mapping,
3412677fc056SKent Overstreet 				pos_dst, pos_dst + len - 1);
341376426098SKent Overstreet 	if (ret)
34142e87eae1SKent Overstreet 		goto err;
341576426098SKent Overstreet 
3416e8540e56SKent Overstreet 	ret = quota_reserve_range(dst, &quota_res, pos_dst >> 9,
3417e8540e56SKent Overstreet 				  (pos_dst + aligned_len) >> 9);
3418e8540e56SKent Overstreet 	if (ret)
3419e8540e56SKent Overstreet 		goto err;
3420e8540e56SKent Overstreet 
3421e8540e56SKent Overstreet 	file_update_time(file_dst);
3422e8540e56SKent Overstreet 
3423dcfc593fSKent Overstreet 	mark_pagecache_unallocated(src, pos_src >> 9,
3424dcfc593fSKent Overstreet 				   (pos_src + aligned_len) >> 9);
342576426098SKent Overstreet 
34262e87eae1SKent Overstreet 	ret = bch2_remap_range(c,
34276fed42bbSKent Overstreet 			       inode_inum(dst), pos_dst >> 9,
34286fed42bbSKent Overstreet 			       inode_inum(src), pos_src >> 9,
342976426098SKent Overstreet 			       aligned_len >> 9,
34302e87eae1SKent Overstreet 			       pos_dst + len, &i_sectors_delta);
34312e87eae1SKent Overstreet 	if (ret < 0)
34322e87eae1SKent Overstreet 		goto err;
343376426098SKent Overstreet 
34342e87eae1SKent Overstreet 	/*
34352e87eae1SKent Overstreet 	 * due to alignment, we might have remapped slightly more than requsted
34362e87eae1SKent Overstreet 	 */
3437677fc056SKent Overstreet 	ret = min((u64) ret << 9, (u64) len);
34382e87eae1SKent Overstreet 
3439e8540e56SKent Overstreet 	i_sectors_acct(c, dst, &quota_res, i_sectors_delta);
34402e87eae1SKent Overstreet 
34412e87eae1SKent Overstreet 	spin_lock(&dst->v.i_lock);
3442677fc056SKent Overstreet 	if (pos_dst + ret > dst->v.i_size)
3443677fc056SKent Overstreet 		i_size_write(&dst->v, pos_dst + ret);
34442e87eae1SKent Overstreet 	spin_unlock(&dst->v.i_lock);
3445e7084c9cSKent Overstreet 
344668a2054dSKent Overstreet 	if ((file_dst->f_flags & (__O_SYNC | O_DSYNC)) ||
344768a2054dSKent Overstreet 	    IS_SYNC(file_inode(file_dst)))
3448a8b3a677SKent Overstreet 		ret = bch2_flush_inode(c, dst);
34492e87eae1SKent Overstreet err:
3450e8540e56SKent Overstreet 	bch2_quota_reservation_put(c, dst, &quota_res);
345176426098SKent Overstreet 	bch2_unlock_inodes(INODE_LOCK|INODE_PAGECACHE_BLOCK, src, dst);
345276426098SKent Overstreet 
34535c1ef830SKent Overstreet 	return bch2_err_class(ret);
345476426098SKent Overstreet }
345576426098SKent Overstreet 
34561c6fdbd8SKent Overstreet /* fseek: */
34571c6fdbd8SKent Overstreet 
3458543ef2ebSKent Overstreet static int folio_data_offset(struct folio *folio, unsigned offset)
34591c6fdbd8SKent Overstreet {
3460f57a6a5dSKent Overstreet 	struct bch_page_state *s = bch2_page_state(&folio->page);
3461f57a6a5dSKent Overstreet 	unsigned i;
3462f81b648dSKent Overstreet 
3463543ef2ebSKent Overstreet 	if (s)
3464543ef2ebSKent Overstreet 		for (i = offset >> 9; i < PAGE_SECTORS; i++)
3465f57a6a5dSKent Overstreet 			if (s->s[i].state >= SECTOR_DIRTY)
3466543ef2ebSKent Overstreet 				return i << 9;
3467f57a6a5dSKent Overstreet 
3468543ef2ebSKent Overstreet 	return -1;
34691c6fdbd8SKent Overstreet }
34701c6fdbd8SKent Overstreet 
3471543ef2ebSKent Overstreet static loff_t bch2_seek_pagecache_data(struct inode *vinode,
34721c6fdbd8SKent Overstreet 				       loff_t start_offset,
34731c6fdbd8SKent Overstreet 				       loff_t end_offset)
34741c6fdbd8SKent Overstreet {
34751c6fdbd8SKent Overstreet 	struct folio_batch fbatch;
34761c6fdbd8SKent Overstreet 	pgoff_t start_index	= start_offset >> PAGE_SHIFT;
34771c6fdbd8SKent Overstreet 	pgoff_t end_index	= end_offset >> PAGE_SHIFT;
34781c6fdbd8SKent Overstreet 	pgoff_t index		= start_index;
34791c6fdbd8SKent Overstreet 	unsigned i;
3480543ef2ebSKent Overstreet 	loff_t ret;
3481543ef2ebSKent Overstreet 	int offset;
34821c6fdbd8SKent Overstreet 
34831c6fdbd8SKent Overstreet 	folio_batch_init(&fbatch);
34841c6fdbd8SKent Overstreet 
34851c6fdbd8SKent Overstreet 	while (filemap_get_folios(vinode->i_mapping,
34861c6fdbd8SKent Overstreet 				  &index, end_index, &fbatch)) {
34871c6fdbd8SKent Overstreet 		for (i = 0; i < folio_batch_count(&fbatch); i++) {
34881c6fdbd8SKent Overstreet 			struct folio *folio = fbatch.folios[i];
34891c6fdbd8SKent Overstreet 
34901c6fdbd8SKent Overstreet 			folio_lock(folio);
3491543ef2ebSKent Overstreet 			offset = folio_data_offset(folio,
3492543ef2ebSKent Overstreet 					folio->index == start_index
3493543ef2ebSKent Overstreet 					? start_offset & (PAGE_SIZE - 1)
3494543ef2ebSKent Overstreet 					: 0);
3495543ef2ebSKent Overstreet 			if (offset >= 0) {
3496543ef2ebSKent Overstreet 				ret = clamp(((loff_t) folio->index << PAGE_SHIFT) +
3497543ef2ebSKent Overstreet 					    offset,
3498543ef2ebSKent Overstreet 					    start_offset, end_offset);
34991c6fdbd8SKent Overstreet 				folio_unlock(folio);
35001c6fdbd8SKent Overstreet 				folio_batch_release(&fbatch);
3501543ef2ebSKent Overstreet 				return ret;
35021c6fdbd8SKent Overstreet 			}
35031c6fdbd8SKent Overstreet 			folio_unlock(folio);
35041c6fdbd8SKent Overstreet 		}
35051c6fdbd8SKent Overstreet 		folio_batch_release(&fbatch);
35061c6fdbd8SKent Overstreet 		cond_resched();
35071c6fdbd8SKent Overstreet 	}
35081c6fdbd8SKent Overstreet 
35091c6fdbd8SKent Overstreet 	return end_offset;
35101c6fdbd8SKent Overstreet }
35111c6fdbd8SKent Overstreet 
35121c6fdbd8SKent Overstreet static loff_t bch2_seek_data(struct file *file, u64 offset)
35131c6fdbd8SKent Overstreet {
35141c6fdbd8SKent Overstreet 	struct bch_inode_info *inode = file_bch_inode(file);
35151c6fdbd8SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
3516424eb881SKent Overstreet 	struct btree_trans trans;
351767e0dd8fSKent Overstreet 	struct btree_iter iter;
35181c6fdbd8SKent Overstreet 	struct bkey_s_c k;
35196fed42bbSKent Overstreet 	subvol_inum inum = inode_inum(inode);
35201c6fdbd8SKent Overstreet 	u64 isize, next_data = MAX_LFS_FILESIZE;
35216fed42bbSKent Overstreet 	u32 snapshot;
35221c6fdbd8SKent Overstreet 	int ret;
35231c6fdbd8SKent Overstreet 
35241c6fdbd8SKent Overstreet 	isize = i_size_read(&inode->v);
35251c6fdbd8SKent Overstreet 	if (offset >= isize)
35261c6fdbd8SKent Overstreet 		return -ENXIO;
35271c6fdbd8SKent Overstreet 
352820bceecbSKent Overstreet 	bch2_trans_init(&trans, c, 0, 0);
35296fed42bbSKent Overstreet retry:
35306fed42bbSKent Overstreet 	bch2_trans_begin(&trans);
35316fed42bbSKent Overstreet 
35326fed42bbSKent Overstreet 	ret = bch2_subvolume_get_snapshot(&trans, inum.subvol, &snapshot);
35336fed42bbSKent Overstreet 	if (ret)
35346fed42bbSKent Overstreet 		goto err;
3535424eb881SKent Overstreet 
3536c72f687aSKent Overstreet 	for_each_btree_key_upto_norestart(&trans, iter, BTREE_ID_extents,
3537c72f687aSKent Overstreet 			   SPOS(inode->v.i_ino, offset >> 9, snapshot),
3538c72f687aSKent Overstreet 			   POS(inode->v.i_ino, U64_MAX),
3539c72f687aSKent Overstreet 			   0, k, ret) {
3540c72f687aSKent Overstreet 		if (bkey_extent_is_data(k.k)) {
35411c6fdbd8SKent Overstreet 			next_data = max(offset, bkey_start_offset(k.k) << 9);
35421c6fdbd8SKent Overstreet 			break;
35431c6fdbd8SKent Overstreet 		} else if (k.k->p.offset >> 9 > isize)
35441c6fdbd8SKent Overstreet 			break;
35451c6fdbd8SKent Overstreet 	}
354667e0dd8fSKent Overstreet 	bch2_trans_iter_exit(&trans, &iter);
35476fed42bbSKent Overstreet err:
3548549d173cSKent Overstreet 	if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
35496fed42bbSKent Overstreet 		goto retry;
35501c6fdbd8SKent Overstreet 
35519a796fdbSKent Overstreet 	bch2_trans_exit(&trans);
35521c6fdbd8SKent Overstreet 	if (ret)
35531c6fdbd8SKent Overstreet 		return ret;
35541c6fdbd8SKent Overstreet 
35551c6fdbd8SKent Overstreet 	if (next_data > offset)
3556543ef2ebSKent Overstreet 		next_data = bch2_seek_pagecache_data(&inode->v,
35571c6fdbd8SKent Overstreet 						     offset, next_data);
35581c6fdbd8SKent Overstreet 
3559e10d3094SKent Overstreet 	if (next_data >= isize)
35601c6fdbd8SKent Overstreet 		return -ENXIO;
35611c6fdbd8SKent Overstreet 
35621c6fdbd8SKent Overstreet 	return vfs_setpos(file, next_data, MAX_LFS_FILESIZE);
35631c6fdbd8SKent Overstreet }
35641c6fdbd8SKent Overstreet 
3565543ef2ebSKent Overstreet static int __page_hole_offset(struct page *page, unsigned offset)
35661c6fdbd8SKent Overstreet {
3567543ef2ebSKent Overstreet 	struct bch_page_state *s = bch2_page_state(page);
3568543ef2ebSKent Overstreet 	unsigned i;
3569543ef2ebSKent Overstreet 
3570543ef2ebSKent Overstreet 	if (!s)
3571543ef2ebSKent Overstreet 		return 0;
3572543ef2ebSKent Overstreet 
3573543ef2ebSKent Overstreet 	for (i = offset >> 9; i < PAGE_SECTORS; i++)
3574543ef2ebSKent Overstreet 		if (s->s[i].state < SECTOR_DIRTY)
3575543ef2ebSKent Overstreet 			return i << 9;
3576543ef2ebSKent Overstreet 
3577543ef2ebSKent Overstreet 	return -1;
3578543ef2ebSKent Overstreet }
3579543ef2ebSKent Overstreet 
3580543ef2ebSKent Overstreet static loff_t page_hole_offset(struct address_space *mapping, loff_t offset)
3581543ef2ebSKent Overstreet {
3582543ef2ebSKent Overstreet 	pgoff_t index = offset >> PAGE_SHIFT;
35831c6fdbd8SKent Overstreet 	struct page *page;
3584543ef2ebSKent Overstreet 	int pg_offset;
3585543ef2ebSKent Overstreet 	loff_t ret = -1;
35861c6fdbd8SKent Overstreet 
35871c6fdbd8SKent Overstreet 	page = find_lock_page(mapping, index);
35881c6fdbd8SKent Overstreet 	if (!page)
3589543ef2ebSKent Overstreet 		return offset;
35901c6fdbd8SKent Overstreet 
3591543ef2ebSKent Overstreet 	pg_offset = __page_hole_offset(page, offset & (PAGE_SIZE - 1));
3592543ef2ebSKent Overstreet 	if (pg_offset >= 0)
3593543ef2ebSKent Overstreet 		ret = ((loff_t) index << PAGE_SHIFT) + pg_offset;
3594543ef2ebSKent Overstreet 
35951c6fdbd8SKent Overstreet 	unlock_page(page);
35961c6fdbd8SKent Overstreet 
35971c6fdbd8SKent Overstreet 	return ret;
35981c6fdbd8SKent Overstreet }
35991c6fdbd8SKent Overstreet 
3600543ef2ebSKent Overstreet static loff_t bch2_seek_pagecache_hole(struct inode *vinode,
36011c6fdbd8SKent Overstreet 				       loff_t start_offset,
36021c6fdbd8SKent Overstreet 				       loff_t end_offset)
36031c6fdbd8SKent Overstreet {
36041c6fdbd8SKent Overstreet 	struct address_space *mapping = vinode->i_mapping;
3605543ef2ebSKent Overstreet 	loff_t offset = start_offset, hole;
36061c6fdbd8SKent Overstreet 
3607543ef2ebSKent Overstreet 	while (offset < end_offset) {
3608543ef2ebSKent Overstreet 		hole = page_hole_offset(mapping, offset);
3609543ef2ebSKent Overstreet 		if (hole >= 0 && hole <= end_offset)
3610543ef2ebSKent Overstreet 			return max(start_offset, hole);
3611543ef2ebSKent Overstreet 
3612543ef2ebSKent Overstreet 		offset += PAGE_SIZE;
3613543ef2ebSKent Overstreet 		offset &= PAGE_MASK;
3614543ef2ebSKent Overstreet 	}
36151c6fdbd8SKent Overstreet 
36161c6fdbd8SKent Overstreet 	return end_offset;
36171c6fdbd8SKent Overstreet }
36181c6fdbd8SKent Overstreet 
36191c6fdbd8SKent Overstreet static loff_t bch2_seek_hole(struct file *file, u64 offset)
36201c6fdbd8SKent Overstreet {
36211c6fdbd8SKent Overstreet 	struct bch_inode_info *inode = file_bch_inode(file);
36221c6fdbd8SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
3623424eb881SKent Overstreet 	struct btree_trans trans;
362467e0dd8fSKent Overstreet 	struct btree_iter iter;
36251c6fdbd8SKent Overstreet 	struct bkey_s_c k;
36266fed42bbSKent Overstreet 	subvol_inum inum = inode_inum(inode);
36271c6fdbd8SKent Overstreet 	u64 isize, next_hole = MAX_LFS_FILESIZE;
36286fed42bbSKent Overstreet 	u32 snapshot;
36291c6fdbd8SKent Overstreet 	int ret;
36301c6fdbd8SKent Overstreet 
36311c6fdbd8SKent Overstreet 	isize = i_size_read(&inode->v);
36321c6fdbd8SKent Overstreet 	if (offset >= isize)
36331c6fdbd8SKent Overstreet 		return -ENXIO;
36341c6fdbd8SKent Overstreet 
363520bceecbSKent Overstreet 	bch2_trans_init(&trans, c, 0, 0);
36366fed42bbSKent Overstreet retry:
36376fed42bbSKent Overstreet 	bch2_trans_begin(&trans);
36386fed42bbSKent Overstreet 
36396fed42bbSKent Overstreet 	ret = bch2_subvolume_get_snapshot(&trans, inum.subvol, &snapshot);
36406fed42bbSKent Overstreet 	if (ret)
36416fed42bbSKent Overstreet 		goto err;
3642424eb881SKent Overstreet 
3643e5fa91d7SKent Overstreet 	for_each_btree_key_norestart(&trans, iter, BTREE_ID_extents,
36446fed42bbSKent Overstreet 			   SPOS(inode->v.i_ino, offset >> 9, snapshot),
364594f651e2SKent Overstreet 			   BTREE_ITER_SLOTS, k, ret) {
36461c6fdbd8SKent Overstreet 		if (k.k->p.inode != inode->v.i_ino) {
3647543ef2ebSKent Overstreet 			next_hole = bch2_seek_pagecache_hole(&inode->v,
36481c6fdbd8SKent Overstreet 					offset, MAX_LFS_FILESIZE);
36491c6fdbd8SKent Overstreet 			break;
36501c6fdbd8SKent Overstreet 		} else if (!bkey_extent_is_data(k.k)) {
3651543ef2ebSKent Overstreet 			next_hole = bch2_seek_pagecache_hole(&inode->v,
36521c6fdbd8SKent Overstreet 					max(offset, bkey_start_offset(k.k) << 9),
36531c6fdbd8SKent Overstreet 					k.k->p.offset << 9);
36541c6fdbd8SKent Overstreet 
36551c6fdbd8SKent Overstreet 			if (next_hole < k.k->p.offset << 9)
36561c6fdbd8SKent Overstreet 				break;
36571c6fdbd8SKent Overstreet 		} else {
36581c6fdbd8SKent Overstreet 			offset = max(offset, bkey_start_offset(k.k) << 9);
36591c6fdbd8SKent Overstreet 		}
36601c6fdbd8SKent Overstreet 	}
366167e0dd8fSKent Overstreet 	bch2_trans_iter_exit(&trans, &iter);
36626fed42bbSKent Overstreet err:
3663549d173cSKent Overstreet 	if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
36646fed42bbSKent Overstreet 		goto retry;
36651c6fdbd8SKent Overstreet 
36669a796fdbSKent Overstreet 	bch2_trans_exit(&trans);
36671c6fdbd8SKent Overstreet 	if (ret)
36681c6fdbd8SKent Overstreet 		return ret;
36691c6fdbd8SKent Overstreet 
36701c6fdbd8SKent Overstreet 	if (next_hole > isize)
36711c6fdbd8SKent Overstreet 		next_hole = isize;
36721c6fdbd8SKent Overstreet 
36731c6fdbd8SKent Overstreet 	return vfs_setpos(file, next_hole, MAX_LFS_FILESIZE);
36741c6fdbd8SKent Overstreet }
36751c6fdbd8SKent Overstreet 
36761c6fdbd8SKent Overstreet loff_t bch2_llseek(struct file *file, loff_t offset, int whence)
36771c6fdbd8SKent Overstreet {
36785c1ef830SKent Overstreet 	loff_t ret;
36795c1ef830SKent Overstreet 
36801c6fdbd8SKent Overstreet 	switch (whence) {
36811c6fdbd8SKent Overstreet 	case SEEK_SET:
36821c6fdbd8SKent Overstreet 	case SEEK_CUR:
36831c6fdbd8SKent Overstreet 	case SEEK_END:
36845c1ef830SKent Overstreet 		ret = generic_file_llseek(file, offset, whence);
36855c1ef830SKent Overstreet 		break;
36861c6fdbd8SKent Overstreet 	case SEEK_DATA:
36875c1ef830SKent Overstreet 		ret = bch2_seek_data(file, offset);
36885c1ef830SKent Overstreet 		break;
36891c6fdbd8SKent Overstreet 	case SEEK_HOLE:
36905c1ef830SKent Overstreet 		ret = bch2_seek_hole(file, offset);
36915c1ef830SKent Overstreet 		break;
36925c1ef830SKent Overstreet 	default:
36935c1ef830SKent Overstreet 		ret = -EINVAL;
36945c1ef830SKent Overstreet 		break;
36951c6fdbd8SKent Overstreet 	}
36961c6fdbd8SKent Overstreet 
36975c1ef830SKent Overstreet 	return bch2_err_class(ret);
36981c6fdbd8SKent Overstreet }
36991c6fdbd8SKent Overstreet 
37001c6fdbd8SKent Overstreet void bch2_fs_fsio_exit(struct bch_fs *c)
37011c6fdbd8SKent Overstreet {
3702a8b3a677SKent Overstreet 	bioset_exit(&c->nocow_flush_bioset);
37031c6fdbd8SKent Overstreet 	bioset_exit(&c->dio_write_bioset);
37041c6fdbd8SKent Overstreet 	bioset_exit(&c->dio_read_bioset);
37051c6fdbd8SKent Overstreet 	bioset_exit(&c->writepage_bioset);
37061c6fdbd8SKent Overstreet }
37071c6fdbd8SKent Overstreet 
37081c6fdbd8SKent Overstreet int bch2_fs_fsio_init(struct bch_fs *c)
37091c6fdbd8SKent Overstreet {
37101c6fdbd8SKent Overstreet 	int ret = 0;
37111c6fdbd8SKent Overstreet 
37121c6fdbd8SKent Overstreet 	pr_verbose_init(c->opts, "");
37131c6fdbd8SKent Overstreet 
37141c6fdbd8SKent Overstreet 	if (bioset_init(&c->writepage_bioset,
37159a3df993SKent Overstreet 			4, offsetof(struct bch_writepage_io, op.wbio.bio),
3716*65d48e35SKent Overstreet 			BIOSET_NEED_BVECS))
3717*65d48e35SKent Overstreet 		return -BCH_ERR_ENOMEM_writepage_bioset_init;
3718*65d48e35SKent Overstreet 
3719*65d48e35SKent Overstreet 	if (bioset_init(&c->dio_read_bioset,
37201c6fdbd8SKent Overstreet 			4, offsetof(struct dio_read, rbio.bio),
3721*65d48e35SKent Overstreet 			BIOSET_NEED_BVECS))
3722*65d48e35SKent Overstreet 		return -BCH_ERR_ENOMEM_dio_read_bioset_init;
3723*65d48e35SKent Overstreet 
3724*65d48e35SKent Overstreet 	if (bioset_init(&c->dio_write_bioset,
37259a3df993SKent Overstreet 			4, offsetof(struct dio_write, op.wbio.bio),
3726*65d48e35SKent Overstreet 			BIOSET_NEED_BVECS))
3727*65d48e35SKent Overstreet 		return -BCH_ERR_ENOMEM_dio_write_bioset_init;
3728*65d48e35SKent Overstreet 
3729*65d48e35SKent Overstreet 	if (bioset_init(&c->nocow_flush_bioset,
3730a8b3a677SKent Overstreet 			1, offsetof(struct nocow_flush, bio), 0))
3731*65d48e35SKent Overstreet 		return -BCH_ERR_ENOMEM_nocow_flush_bioset_init;
37321c6fdbd8SKent Overstreet 
37331c6fdbd8SKent Overstreet 	pr_verbose_init(c->opts, "ret %i", ret);
37341c6fdbd8SKent Overstreet 	return ret;
37351c6fdbd8SKent Overstreet }
37361c6fdbd8SKent Overstreet 
37371c6fdbd8SKent Overstreet #endif /* NO_BCACHEFS_FS */
3738