xref: /linux/fs/bcachefs/fs-io.c (revision d94189ad568f6cbd80d372cf7aa6e4898b6c5c17)
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 
387f5e31e1SKent Overstreet static inline bool bio_full(struct bio *bio, unsigned len)
397f5e31e1SKent Overstreet {
407f5e31e1SKent Overstreet 	if (bio->bi_vcnt >= bio->bi_max_vecs)
417f5e31e1SKent Overstreet 		return true;
427f5e31e1SKent Overstreet 	if (bio->bi_iter.bi_size > UINT_MAX - len)
437f5e31e1SKent Overstreet 		return true;
447f5e31e1SKent Overstreet 	return false;
457f5e31e1SKent Overstreet }
467f5e31e1SKent Overstreet 
47eb8e6e9cSKent Overstreet static inline struct address_space *faults_disabled_mapping(void)
48eb8e6e9cSKent Overstreet {
49eb8e6e9cSKent Overstreet 	return (void *) (((unsigned long) current->faults_disabled_mapping) & ~1UL);
50eb8e6e9cSKent Overstreet }
51eb8e6e9cSKent Overstreet 
52eb8e6e9cSKent Overstreet static inline void set_fdm_dropped_locks(void)
53eb8e6e9cSKent Overstreet {
54eb8e6e9cSKent Overstreet 	current->faults_disabled_mapping =
55eb8e6e9cSKent Overstreet 		(void *) (((unsigned long) current->faults_disabled_mapping)|1);
56eb8e6e9cSKent Overstreet }
57eb8e6e9cSKent Overstreet 
58eb8e6e9cSKent Overstreet static inline bool fdm_dropped_locks(void)
59eb8e6e9cSKent Overstreet {
60eb8e6e9cSKent Overstreet 	return ((unsigned long) current->faults_disabled_mapping) & 1;
61eb8e6e9cSKent Overstreet }
62eb8e6e9cSKent Overstreet 
631c6fdbd8SKent Overstreet struct quota_res {
641c6fdbd8SKent Overstreet 	u64				sectors;
651c6fdbd8SKent Overstreet };
661c6fdbd8SKent Overstreet 
679a3df993SKent Overstreet struct bch_writepage_io {
681c6fdbd8SKent Overstreet 	struct bch_inode_info		*inode;
691c6fdbd8SKent Overstreet 
701c6fdbd8SKent Overstreet 	/* must be last: */
711c6fdbd8SKent Overstreet 	struct bch_write_op		op;
721c6fdbd8SKent Overstreet };
731c6fdbd8SKent Overstreet 
741c6fdbd8SKent Overstreet struct dio_write {
751c6fdbd8SKent Overstreet 	struct kiocb			*req;
76182c7bbfSKent Overstreet 	struct address_space		*mapping;
77182c7bbfSKent Overstreet 	struct bch_inode_info		*inode;
78ed484030SKent Overstreet 	struct mm_struct		*mm;
791c6fdbd8SKent Overstreet 	unsigned			loop:1,
806b1b186aSKent Overstreet 					extending:1,
811c6fdbd8SKent Overstreet 					sync:1,
82a1ee777bSKent Overstreet 					flush:1,
831c6fdbd8SKent Overstreet 					free_iov:1;
841c6fdbd8SKent Overstreet 	struct quota_res		quota_res;
85042a1f26SKent Overstreet 	u64				written;
861c6fdbd8SKent Overstreet 
871c6fdbd8SKent Overstreet 	struct iov_iter			iter;
881c6fdbd8SKent Overstreet 	struct iovec			inline_vecs[2];
891c6fdbd8SKent Overstreet 
901c6fdbd8SKent Overstreet 	/* must be last: */
919a3df993SKent Overstreet 	struct bch_write_op		op;
921c6fdbd8SKent Overstreet };
931c6fdbd8SKent Overstreet 
941c6fdbd8SKent Overstreet struct dio_read {
951c6fdbd8SKent Overstreet 	struct closure			cl;
961c6fdbd8SKent Overstreet 	struct kiocb			*req;
971c6fdbd8SKent Overstreet 	long				ret;
98b4725cc1SKent Overstreet 	bool				should_dirty;
991c6fdbd8SKent Overstreet 	struct bch_read_bio		rbio;
1001c6fdbd8SKent Overstreet };
1011c6fdbd8SKent Overstreet 
1021c6fdbd8SKent Overstreet /* pagecache_block must be held */
103a023127aSKent Overstreet static noinline int write_invalidate_inode_pages_range(struct address_space *mapping,
1041c6fdbd8SKent Overstreet 					      loff_t start, loff_t end)
1051c6fdbd8SKent Overstreet {
1061c6fdbd8SKent Overstreet 	int ret;
1071c6fdbd8SKent Overstreet 
1081c6fdbd8SKent Overstreet 	/*
1091c6fdbd8SKent Overstreet 	 * XXX: the way this is currently implemented, we can spin if a process
1101c6fdbd8SKent Overstreet 	 * is continually redirtying a specific page
1111c6fdbd8SKent Overstreet 	 */
1121c6fdbd8SKent Overstreet 	do {
1131c6fdbd8SKent Overstreet 		if (!mapping->nrpages)
1141c6fdbd8SKent Overstreet 			return 0;
1151c6fdbd8SKent Overstreet 
1161c6fdbd8SKent Overstreet 		ret = filemap_write_and_wait_range(mapping, start, end);
1171c6fdbd8SKent Overstreet 		if (ret)
1181c6fdbd8SKent Overstreet 			break;
1191c6fdbd8SKent Overstreet 
1201c6fdbd8SKent Overstreet 		if (!mapping->nrpages)
1211c6fdbd8SKent Overstreet 			return 0;
1221c6fdbd8SKent Overstreet 
1231c6fdbd8SKent Overstreet 		ret = invalidate_inode_pages2_range(mapping,
1241c6fdbd8SKent Overstreet 				start >> PAGE_SHIFT,
1251c6fdbd8SKent Overstreet 				end >> PAGE_SHIFT);
1261c6fdbd8SKent Overstreet 	} while (ret == -EBUSY);
1271c6fdbd8SKent Overstreet 
1281c6fdbd8SKent Overstreet 	return ret;
1291c6fdbd8SKent Overstreet }
1301c6fdbd8SKent Overstreet 
1311c6fdbd8SKent Overstreet /* quotas */
1321c6fdbd8SKent Overstreet 
1331c6fdbd8SKent Overstreet #ifdef CONFIG_BCACHEFS_QUOTA
1341c6fdbd8SKent Overstreet 
1356b1b186aSKent Overstreet static void __bch2_quota_reservation_put(struct bch_fs *c,
1361c6fdbd8SKent Overstreet 					 struct bch_inode_info *inode,
1371c6fdbd8SKent Overstreet 					 struct quota_res *res)
1381c6fdbd8SKent Overstreet {
1391c6fdbd8SKent Overstreet 	BUG_ON(res->sectors > inode->ei_quota_reserved);
1401c6fdbd8SKent Overstreet 
1411c6fdbd8SKent Overstreet 	bch2_quota_acct(c, inode->ei_qid, Q_SPC,
14226609b61SKent Overstreet 			-((s64) res->sectors), KEY_TYPE_QUOTA_PREALLOC);
1431c6fdbd8SKent Overstreet 	inode->ei_quota_reserved -= res->sectors;
1441c6fdbd8SKent Overstreet 	res->sectors = 0;
1451c6fdbd8SKent Overstreet }
1461c6fdbd8SKent Overstreet 
1476b1b186aSKent Overstreet static void bch2_quota_reservation_put(struct bch_fs *c,
1486b1b186aSKent Overstreet 				       struct bch_inode_info *inode,
1496b1b186aSKent Overstreet 				       struct quota_res *res)
1506b1b186aSKent Overstreet {
1516b1b186aSKent Overstreet 	if (res->sectors) {
1526b1b186aSKent Overstreet 		mutex_lock(&inode->ei_quota_lock);
1536b1b186aSKent Overstreet 		__bch2_quota_reservation_put(c, inode, res);
1546b1b186aSKent Overstreet 		mutex_unlock(&inode->ei_quota_lock);
1556b1b186aSKent Overstreet 	}
1566b1b186aSKent Overstreet }
1576b1b186aSKent Overstreet 
1581c6fdbd8SKent Overstreet static int bch2_quota_reservation_add(struct bch_fs *c,
1591c6fdbd8SKent Overstreet 				      struct bch_inode_info *inode,
1601c6fdbd8SKent Overstreet 				      struct quota_res *res,
161e8540e56SKent Overstreet 				      u64 sectors,
1621c6fdbd8SKent Overstreet 				      bool check_enospc)
1631c6fdbd8SKent Overstreet {
1641c6fdbd8SKent Overstreet 	int ret;
1651c6fdbd8SKent Overstreet 
1661c6fdbd8SKent Overstreet 	mutex_lock(&inode->ei_quota_lock);
1671c6fdbd8SKent Overstreet 	ret = bch2_quota_acct(c, inode->ei_qid, Q_SPC, sectors,
16826609b61SKent Overstreet 			      check_enospc ? KEY_TYPE_QUOTA_PREALLOC : KEY_TYPE_QUOTA_NOCHECK);
1691c6fdbd8SKent Overstreet 	if (likely(!ret)) {
1701c6fdbd8SKent Overstreet 		inode->ei_quota_reserved += sectors;
1711c6fdbd8SKent Overstreet 		res->sectors += sectors;
1721c6fdbd8SKent Overstreet 	}
1731c6fdbd8SKent Overstreet 	mutex_unlock(&inode->ei_quota_lock);
1741c6fdbd8SKent Overstreet 
1751c6fdbd8SKent Overstreet 	return ret;
1761c6fdbd8SKent Overstreet }
1771c6fdbd8SKent Overstreet 
1781c6fdbd8SKent Overstreet #else
1791c6fdbd8SKent Overstreet 
1806b1b186aSKent Overstreet static void __bch2_quota_reservation_put(struct bch_fs *c,
1816b1b186aSKent Overstreet 					 struct bch_inode_info *inode,
1826b1b186aSKent Overstreet 					 struct quota_res *res) {}
1836b1b186aSKent Overstreet 
1841c6fdbd8SKent Overstreet static void bch2_quota_reservation_put(struct bch_fs *c,
1851c6fdbd8SKent Overstreet 				       struct bch_inode_info *inode,
1866b1b186aSKent Overstreet 				       struct quota_res *res) {}
1871c6fdbd8SKent Overstreet 
1881c6fdbd8SKent Overstreet static int bch2_quota_reservation_add(struct bch_fs *c,
1891c6fdbd8SKent Overstreet 				      struct bch_inode_info *inode,
1901c6fdbd8SKent Overstreet 				      struct quota_res *res,
1911c6fdbd8SKent Overstreet 				      unsigned sectors,
1921c6fdbd8SKent Overstreet 				      bool check_enospc)
1931c6fdbd8SKent Overstreet {
1941c6fdbd8SKent Overstreet 	return 0;
1951c6fdbd8SKent Overstreet }
1961c6fdbd8SKent Overstreet 
1971c6fdbd8SKent Overstreet #endif
1981c6fdbd8SKent Overstreet 
1991c6fdbd8SKent Overstreet /* i_size updates: */
2001c6fdbd8SKent Overstreet 
2012ea90048SKent Overstreet struct inode_new_size {
2022ea90048SKent Overstreet 	loff_t		new_size;
2032ea90048SKent Overstreet 	u64		now;
2042ea90048SKent Overstreet 	unsigned	fields;
2052ea90048SKent Overstreet };
2062ea90048SKent Overstreet 
2071c6fdbd8SKent Overstreet static int inode_set_size(struct bch_inode_info *inode,
2081c6fdbd8SKent Overstreet 			  struct bch_inode_unpacked *bi,
2091c6fdbd8SKent Overstreet 			  void *p)
2101c6fdbd8SKent Overstreet {
2112ea90048SKent Overstreet 	struct inode_new_size *s = p;
2121c6fdbd8SKent Overstreet 
2132ea90048SKent Overstreet 	bi->bi_size = s->new_size;
2142ea90048SKent Overstreet 	if (s->fields & ATTR_ATIME)
2152ea90048SKent Overstreet 		bi->bi_atime = s->now;
2162ea90048SKent Overstreet 	if (s->fields & ATTR_MTIME)
2172ea90048SKent Overstreet 		bi->bi_mtime = s->now;
2182ea90048SKent Overstreet 	if (s->fields & ATTR_CTIME)
2192ea90048SKent Overstreet 		bi->bi_ctime = s->now;
2201c6fdbd8SKent Overstreet 
2211c6fdbd8SKent Overstreet 	return 0;
2221c6fdbd8SKent Overstreet }
2231c6fdbd8SKent Overstreet 
22476426098SKent Overstreet int __must_check bch2_write_inode_size(struct bch_fs *c,
2251c6fdbd8SKent Overstreet 				       struct bch_inode_info *inode,
2262ea90048SKent Overstreet 				       loff_t new_size, unsigned fields)
2271c6fdbd8SKent Overstreet {
2282ea90048SKent Overstreet 	struct inode_new_size s = {
2292ea90048SKent Overstreet 		.new_size	= new_size,
2302ea90048SKent Overstreet 		.now		= bch2_current_time(c),
2312ea90048SKent Overstreet 		.fields		= fields,
2322ea90048SKent Overstreet 	};
2332ea90048SKent Overstreet 
2342ea90048SKent Overstreet 	return bch2_write_inode(c, inode, inode_set_size, &s, fields);
2351c6fdbd8SKent Overstreet }
2361c6fdbd8SKent Overstreet 
2376b1b186aSKent Overstreet static void __i_sectors_acct(struct bch_fs *c, struct bch_inode_info *inode,
238190fa7afSKent Overstreet 			   struct quota_res *quota_res, s64 sectors)
2391c6fdbd8SKent Overstreet {
240b33bf1bcSKent Overstreet 	bch2_fs_inconsistent_on((s64) inode->v.i_blocks + sectors < 0, c,
241b33bf1bcSKent Overstreet 				"inode %lu i_blocks underflow: %llu + %lli < 0 (ondisk %lli)",
242b33bf1bcSKent Overstreet 				inode->v.i_ino, (u64) inode->v.i_blocks, sectors,
243b33bf1bcSKent Overstreet 				inode->ei_inode.bi_sectors);
244b44a66a6SKent Overstreet 	inode->v.i_blocks += sectors;
245b44a66a6SKent Overstreet 
2461c6fdbd8SKent Overstreet #ifdef CONFIG_BCACHEFS_QUOTA
2471c6fdbd8SKent Overstreet 	if (quota_res && sectors > 0) {
2481c6fdbd8SKent Overstreet 		BUG_ON(sectors > quota_res->sectors);
2491c6fdbd8SKent Overstreet 		BUG_ON(sectors > inode->ei_quota_reserved);
2501c6fdbd8SKent Overstreet 
2511c6fdbd8SKent Overstreet 		quota_res->sectors -= sectors;
2521c6fdbd8SKent Overstreet 		inode->ei_quota_reserved -= sectors;
2531c6fdbd8SKent Overstreet 	} else {
25426609b61SKent Overstreet 		bch2_quota_acct(c, inode->ei_qid, Q_SPC, sectors, KEY_TYPE_QUOTA_WARN);
2551c6fdbd8SKent Overstreet 	}
2561c6fdbd8SKent Overstreet #endif
2576b1b186aSKent Overstreet }
2586b1b186aSKent Overstreet 
2596b1b186aSKent Overstreet static void i_sectors_acct(struct bch_fs *c, struct bch_inode_info *inode,
2606b1b186aSKent Overstreet 			   struct quota_res *quota_res, s64 sectors)
2616b1b186aSKent Overstreet {
2626b1b186aSKent Overstreet 	if (sectors) {
2636b1b186aSKent Overstreet 		mutex_lock(&inode->ei_quota_lock);
2646b1b186aSKent Overstreet 		__i_sectors_acct(c, inode, quota_res, sectors);
2651c6fdbd8SKent Overstreet 		mutex_unlock(&inode->ei_quota_lock);
2661c6fdbd8SKent Overstreet 	}
2676b1b186aSKent Overstreet }
2681c6fdbd8SKent Overstreet 
2691c6fdbd8SKent Overstreet /* page state: */
2701c6fdbd8SKent Overstreet 
2711c6fdbd8SKent Overstreet /* stored in page->private: */
2721c6fdbd8SKent Overstreet 
273f57a6a5dSKent Overstreet struct bch_page_sector {
274b44a66a6SKent Overstreet 	/* Uncompressed, fully allocated replicas (or on disk reservation): */
275b44a66a6SKent Overstreet 	unsigned		nr_replicas:4;
276f81b648dSKent Overstreet 
277b44a66a6SKent Overstreet 	/* Owns PAGE_SECTORS * replicas_reserved sized in memory reservation: */
278b44a66a6SKent Overstreet 	unsigned		replicas_reserved:4;
2791c6fdbd8SKent Overstreet 
280f57a6a5dSKent Overstreet 	/* i_sectors: */
281f57a6a5dSKent Overstreet 	enum {
282f57a6a5dSKent Overstreet 		SECTOR_UNALLOCATED,
2832ba5d38bSKent Overstreet 		SECTOR_RESERVED,
284f57a6a5dSKent Overstreet 		SECTOR_DIRTY,
285b44a66a6SKent Overstreet 		SECTOR_DIRTY_RESERVED,
286f57a6a5dSKent Overstreet 		SECTOR_ALLOCATED,
287b44a66a6SKent Overstreet 	}			state:8;
2881c6fdbd8SKent Overstreet };
2891c6fdbd8SKent Overstreet 
290f57a6a5dSKent Overstreet struct bch_page_state {
2913826ee0bSKent Overstreet 	spinlock_t		lock;
2927f5e31e1SKent Overstreet 	atomic_t		write_count;
293e6ec361fSKent Overstreet 	bool			uptodate;
294f57a6a5dSKent Overstreet 	struct bch_page_sector	s[PAGE_SECTORS];
295f57a6a5dSKent Overstreet };
296f57a6a5dSKent Overstreet 
297f57a6a5dSKent Overstreet static inline struct bch_page_state *__bch2_page_state(struct page *page)
2981c6fdbd8SKent Overstreet {
299f57a6a5dSKent Overstreet 	return page_has_private(page)
300f57a6a5dSKent Overstreet 		? (struct bch_page_state *) page_private(page)
301f57a6a5dSKent Overstreet 		: NULL;
302f57a6a5dSKent Overstreet }
3031c6fdbd8SKent Overstreet 
304f57a6a5dSKent Overstreet static inline struct bch_page_state *bch2_page_state(struct page *page)
305f57a6a5dSKent Overstreet {
306e1036a2aSKent Overstreet 	EBUG_ON(!PageLocked(page));
3071c6fdbd8SKent Overstreet 
308f57a6a5dSKent Overstreet 	return __bch2_page_state(page);
309f57a6a5dSKent Overstreet }
310f57a6a5dSKent Overstreet 
311f57a6a5dSKent Overstreet /* for newly allocated pages: */
312f57a6a5dSKent Overstreet static void __bch2_page_state_release(struct page *page)
313f57a6a5dSKent Overstreet {
31400276f9fSMatthew Wilcox (Oracle) 	kfree(detach_page_private(page));
315f57a6a5dSKent Overstreet }
316f57a6a5dSKent Overstreet 
317f57a6a5dSKent Overstreet static void bch2_page_state_release(struct page *page)
318f57a6a5dSKent Overstreet {
31900276f9fSMatthew Wilcox (Oracle) 	EBUG_ON(!PageLocked(page));
32000276f9fSMatthew Wilcox (Oracle) 	__bch2_page_state_release(page);
321f57a6a5dSKent Overstreet }
322f57a6a5dSKent Overstreet 
323f57a6a5dSKent Overstreet /* for newly allocated pages: */
324f57a6a5dSKent Overstreet static struct bch_page_state *__bch2_page_state_create(struct page *page,
325f57a6a5dSKent Overstreet 						       gfp_t gfp)
326f57a6a5dSKent Overstreet {
327f57a6a5dSKent Overstreet 	struct bch_page_state *s;
328f57a6a5dSKent Overstreet 
329f57a6a5dSKent Overstreet 	s = kzalloc(sizeof(*s), GFP_NOFS|gfp);
330f57a6a5dSKent Overstreet 	if (!s)
331f57a6a5dSKent Overstreet 		return NULL;
332f57a6a5dSKent Overstreet 
3333826ee0bSKent Overstreet 	spin_lock_init(&s->lock);
33400276f9fSMatthew Wilcox (Oracle) 	attach_page_private(page, s);
3351c6fdbd8SKent Overstreet 	return s;
3361c6fdbd8SKent Overstreet }
3371c6fdbd8SKent Overstreet 
338f57a6a5dSKent Overstreet static struct bch_page_state *bch2_page_state_create(struct page *page,
339f57a6a5dSKent Overstreet 						     gfp_t gfp)
340f57a6a5dSKent Overstreet {
341f57a6a5dSKent Overstreet 	return bch2_page_state(page) ?: __bch2_page_state_create(page, gfp);
342f57a6a5dSKent Overstreet }
343f57a6a5dSKent Overstreet 
344b44a66a6SKent Overstreet static unsigned bkey_to_sector_state(const struct bkey *k)
345b44a66a6SKent Overstreet {
346b44a66a6SKent Overstreet 	if (k->type == KEY_TYPE_reservation)
347b44a66a6SKent Overstreet 		return SECTOR_RESERVED;
348b44a66a6SKent Overstreet 	if (bkey_extent_is_allocation(k))
349b44a66a6SKent Overstreet 		return SECTOR_ALLOCATED;
350b44a66a6SKent Overstreet 	return SECTOR_UNALLOCATED;
351b44a66a6SKent Overstreet }
352b44a66a6SKent Overstreet 
353e6ec361fSKent Overstreet static void __bch2_page_state_set(struct page *page,
354e6ec361fSKent Overstreet 				  unsigned pg_offset, unsigned pg_len,
355e6ec361fSKent Overstreet 				  unsigned nr_ptrs, unsigned state)
356e6ec361fSKent Overstreet {
357e6ec361fSKent Overstreet 	struct bch_page_state *s = bch2_page_state_create(page, __GFP_NOFAIL);
358e6ec361fSKent Overstreet 	unsigned i;
359e6ec361fSKent Overstreet 
360e6ec361fSKent Overstreet 	BUG_ON(pg_offset >= PAGE_SECTORS);
361e6ec361fSKent Overstreet 	BUG_ON(pg_offset + pg_len > PAGE_SECTORS);
362e6ec361fSKent Overstreet 
363e6ec361fSKent Overstreet 	spin_lock(&s->lock);
364e6ec361fSKent Overstreet 
365e6ec361fSKent Overstreet 	for (i = pg_offset; i < pg_offset + pg_len; i++) {
366e6ec361fSKent Overstreet 		s->s[i].nr_replicas = nr_ptrs;
367e6ec361fSKent Overstreet 		s->s[i].state = state;
368e6ec361fSKent Overstreet 	}
369e6ec361fSKent Overstreet 
370e6ec361fSKent Overstreet 	if (i == PAGE_SECTORS)
371e6ec361fSKent Overstreet 		s->uptodate = true;
372e6ec361fSKent Overstreet 
373e6ec361fSKent Overstreet 	spin_unlock(&s->lock);
374e6ec361fSKent Overstreet }
375e6ec361fSKent Overstreet 
376e6ec361fSKent Overstreet static int bch2_page_state_set(struct bch_fs *c, subvol_inum inum,
377e6ec361fSKent Overstreet 			       struct page **pages, unsigned nr_pages)
378e6ec361fSKent Overstreet {
379e6ec361fSKent Overstreet 	struct btree_trans trans;
380e6ec361fSKent Overstreet 	struct btree_iter iter;
381e6ec361fSKent Overstreet 	struct bkey_s_c k;
382e6ec361fSKent Overstreet 	u64 offset = pages[0]->index << PAGE_SECTORS_SHIFT;
383e6ec361fSKent Overstreet 	unsigned pg_idx = 0;
384e6ec361fSKent Overstreet 	u32 snapshot;
385e6ec361fSKent Overstreet 	int ret;
386e6ec361fSKent Overstreet 
387e6ec361fSKent Overstreet 	bch2_trans_init(&trans, c, 0, 0);
388e6ec361fSKent Overstreet retry:
389e6ec361fSKent Overstreet 	bch2_trans_begin(&trans);
390e6ec361fSKent Overstreet 
391e6ec361fSKent Overstreet 	ret = bch2_subvolume_get_snapshot(&trans, inum.subvol, &snapshot);
392e6ec361fSKent Overstreet 	if (ret)
393e6ec361fSKent Overstreet 		goto err;
394e6ec361fSKent Overstreet 
395e6ec361fSKent Overstreet 	for_each_btree_key_norestart(&trans, iter, BTREE_ID_extents,
396e6ec361fSKent Overstreet 			   SPOS(inum.inum, offset, snapshot),
397e6ec361fSKent Overstreet 			   BTREE_ITER_SLOTS, k, ret) {
398e6ec361fSKent Overstreet 		unsigned nr_ptrs = bch2_bkey_nr_ptrs_fully_allocated(k);
399e6ec361fSKent Overstreet 		unsigned state = bkey_to_sector_state(k.k);
400e6ec361fSKent Overstreet 
401e6ec361fSKent Overstreet 		while (pg_idx < nr_pages) {
402e6ec361fSKent Overstreet 			struct page *page = pages[pg_idx];
403e6ec361fSKent Overstreet 			u64 pg_start = page->index << PAGE_SECTORS_SHIFT;
404e6ec361fSKent Overstreet 			u64 pg_end = (page->index + 1) << PAGE_SECTORS_SHIFT;
405e6ec361fSKent Overstreet 			unsigned pg_offset = max(bkey_start_offset(k.k), pg_start) - pg_start;
406e6ec361fSKent Overstreet 			unsigned pg_len = min(k.k->p.offset, pg_end) - pg_offset - pg_start;
407e6ec361fSKent Overstreet 
408e6ec361fSKent Overstreet 			BUG_ON(k.k->p.offset < pg_start);
409e6ec361fSKent Overstreet 			BUG_ON(bkey_start_offset(k.k) > pg_end);
410e6ec361fSKent Overstreet 
411e6ec361fSKent Overstreet 			if (!bch2_page_state_create(page, __GFP_NOFAIL)->uptodate)
412e6ec361fSKent Overstreet 				__bch2_page_state_set(page, pg_offset, pg_len, nr_ptrs, state);
413e6ec361fSKent Overstreet 
414e6ec361fSKent Overstreet 			if (k.k->p.offset < pg_end)
415e6ec361fSKent Overstreet 				break;
416e6ec361fSKent Overstreet 			pg_idx++;
417e6ec361fSKent Overstreet 		}
418e6ec361fSKent Overstreet 
419e6ec361fSKent Overstreet 		if (pg_idx == nr_pages)
420e6ec361fSKent Overstreet 			break;
421e6ec361fSKent Overstreet 	}
422e6ec361fSKent Overstreet 
423e6ec361fSKent Overstreet 	offset = iter.pos.offset;
424e6ec361fSKent Overstreet 	bch2_trans_iter_exit(&trans, &iter);
425e6ec361fSKent Overstreet err:
426549d173cSKent Overstreet 	if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
427e6ec361fSKent Overstreet 		goto retry;
428e6ec361fSKent Overstreet 	bch2_trans_exit(&trans);
429e6ec361fSKent Overstreet 
430e6ec361fSKent Overstreet 	return ret;
431e6ec361fSKent Overstreet }
432e6ec361fSKent Overstreet 
433b44a66a6SKent Overstreet static void bch2_bio_page_state_set(struct bio *bio, struct bkey_s_c k)
434b44a66a6SKent Overstreet {
435b44a66a6SKent Overstreet 	struct bvec_iter iter;
436b44a66a6SKent Overstreet 	struct bio_vec bv;
437b44a66a6SKent Overstreet 	unsigned nr_ptrs = k.k->type == KEY_TYPE_reflink_v
438b44a66a6SKent Overstreet 		? 0 : bch2_bkey_nr_ptrs_fully_allocated(k);
439b44a66a6SKent Overstreet 	unsigned state = bkey_to_sector_state(k.k);
440b44a66a6SKent Overstreet 
441e6ec361fSKent Overstreet 	bio_for_each_segment(bv, bio, iter)
442e6ec361fSKent Overstreet 		__bch2_page_state_set(bv.bv_page, bv.bv_offset >> 9,
443e6ec361fSKent Overstreet 				      bv.bv_len >> 9, nr_ptrs, state);
444b44a66a6SKent Overstreet }
445b44a66a6SKent Overstreet 
446dcfc593fSKent Overstreet static void mark_pagecache_unallocated(struct bch_inode_info *inode,
447dcfc593fSKent Overstreet 				       u64 start, u64 end)
448dcfc593fSKent Overstreet {
449dcfc593fSKent Overstreet 	pgoff_t index = start >> PAGE_SECTORS_SHIFT;
450dcfc593fSKent Overstreet 	pgoff_t end_index = (end - 1) >> PAGE_SECTORS_SHIFT;
451dcfc593fSKent Overstreet 	struct folio_batch fbatch;
452dcfc593fSKent Overstreet 	unsigned i, j;
453dcfc593fSKent Overstreet 
454dcfc593fSKent Overstreet 	if (end <= start)
455dcfc593fSKent Overstreet 		return;
456dcfc593fSKent Overstreet 
457dcfc593fSKent Overstreet 	folio_batch_init(&fbatch);
458dcfc593fSKent Overstreet 
459dcfc593fSKent Overstreet 	while (filemap_get_folios(inode->v.i_mapping,
460dcfc593fSKent Overstreet 				  &index, end_index, &fbatch)) {
461dcfc593fSKent Overstreet 		for (i = 0; i < folio_batch_count(&fbatch); i++) {
462dcfc593fSKent Overstreet 			struct folio *folio = fbatch.folios[i];
463dcfc593fSKent Overstreet 			u64 pg_start = folio->index << PAGE_SECTORS_SHIFT;
464dcfc593fSKent Overstreet 			u64 pg_end = (folio->index + 1) << PAGE_SECTORS_SHIFT;
465dcfc593fSKent Overstreet 			unsigned pg_offset = max(start, pg_start) - pg_start;
466dcfc593fSKent Overstreet 			unsigned pg_len = min(end, pg_end) - pg_offset - pg_start;
467dcfc593fSKent Overstreet 			struct bch_page_state *s;
468dcfc593fSKent Overstreet 
469dcfc593fSKent Overstreet 			BUG_ON(end <= pg_start);
470dcfc593fSKent Overstreet 			BUG_ON(pg_offset >= PAGE_SECTORS);
471dcfc593fSKent Overstreet 			BUG_ON(pg_offset + pg_len > PAGE_SECTORS);
472dcfc593fSKent Overstreet 
473dcfc593fSKent Overstreet 			folio_lock(folio);
474dcfc593fSKent Overstreet 			s = bch2_page_state(&folio->page);
475dcfc593fSKent Overstreet 
476dcfc593fSKent Overstreet 			if (s) {
477dcfc593fSKent Overstreet 				spin_lock(&s->lock);
478dcfc593fSKent Overstreet 				for (j = pg_offset; j < pg_offset + pg_len; j++)
479dcfc593fSKent Overstreet 					s->s[j].nr_replicas = 0;
480dcfc593fSKent Overstreet 				spin_unlock(&s->lock);
481dcfc593fSKent Overstreet 			}
482dcfc593fSKent Overstreet 
483dcfc593fSKent Overstreet 			folio_unlock(folio);
484dcfc593fSKent Overstreet 		}
485dcfc593fSKent Overstreet 		folio_batch_release(&fbatch);
486dcfc593fSKent Overstreet 		cond_resched();
487dcfc593fSKent Overstreet 	}
488dcfc593fSKent Overstreet }
489dcfc593fSKent Overstreet 
490dcfc593fSKent Overstreet static void mark_pagecache_reserved(struct bch_inode_info *inode,
491dcfc593fSKent Overstreet 				    u64 start, u64 end)
492dcfc593fSKent Overstreet {
493dcfc593fSKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
494dcfc593fSKent Overstreet 	pgoff_t index = start >> PAGE_SECTORS_SHIFT;
495dcfc593fSKent Overstreet 	pgoff_t end_index = (end - 1) >> PAGE_SECTORS_SHIFT;
496dcfc593fSKent Overstreet 	struct folio_batch fbatch;
497dcfc593fSKent Overstreet 	s64 i_sectors_delta = 0;
498dcfc593fSKent Overstreet 	unsigned i, j;
499dcfc593fSKent Overstreet 
500dcfc593fSKent Overstreet 	if (end <= start)
501dcfc593fSKent Overstreet 		return;
502dcfc593fSKent Overstreet 
503dcfc593fSKent Overstreet 	folio_batch_init(&fbatch);
504dcfc593fSKent Overstreet 
505dcfc593fSKent Overstreet 	while (filemap_get_folios(inode->v.i_mapping,
506dcfc593fSKent Overstreet 				  &index, end_index, &fbatch)) {
507dcfc593fSKent Overstreet 		for (i = 0; i < folio_batch_count(&fbatch); i++) {
508dcfc593fSKent Overstreet 			struct folio *folio = fbatch.folios[i];
509dcfc593fSKent Overstreet 			u64 pg_start = folio->index << PAGE_SECTORS_SHIFT;
510dcfc593fSKent Overstreet 			u64 pg_end = (folio->index + 1) << PAGE_SECTORS_SHIFT;
511dcfc593fSKent Overstreet 			unsigned pg_offset = max(start, pg_start) - pg_start;
512dcfc593fSKent Overstreet 			unsigned pg_len = min(end, pg_end) - pg_offset - pg_start;
513dcfc593fSKent Overstreet 			struct bch_page_state *s;
514dcfc593fSKent Overstreet 
515dcfc593fSKent Overstreet 			BUG_ON(end <= pg_start);
516dcfc593fSKent Overstreet 			BUG_ON(pg_offset >= PAGE_SECTORS);
517dcfc593fSKent Overstreet 			BUG_ON(pg_offset + pg_len > PAGE_SECTORS);
518dcfc593fSKent Overstreet 
519dcfc593fSKent Overstreet 			folio_lock(folio);
520dcfc593fSKent Overstreet 			s = bch2_page_state(&folio->page);
521dcfc593fSKent Overstreet 
522dcfc593fSKent Overstreet 			if (s) {
523dcfc593fSKent Overstreet 				spin_lock(&s->lock);
524dcfc593fSKent Overstreet 				for (j = pg_offset; j < pg_offset + pg_len; j++)
525dcfc593fSKent Overstreet 					switch (s->s[j].state) {
526dcfc593fSKent Overstreet 					case SECTOR_UNALLOCATED:
527dcfc593fSKent Overstreet 						s->s[j].state = SECTOR_RESERVED;
528dcfc593fSKent Overstreet 						break;
529dcfc593fSKent Overstreet 					case SECTOR_DIRTY:
530dcfc593fSKent Overstreet 						s->s[j].state = SECTOR_DIRTY_RESERVED;
531dcfc593fSKent Overstreet 						i_sectors_delta--;
532dcfc593fSKent Overstreet 						break;
533dcfc593fSKent Overstreet 					default:
534dcfc593fSKent Overstreet 						break;
535dcfc593fSKent Overstreet 					}
536dcfc593fSKent Overstreet 				spin_unlock(&s->lock);
537dcfc593fSKent Overstreet 			}
538dcfc593fSKent Overstreet 
539dcfc593fSKent Overstreet 			folio_unlock(folio);
540dcfc593fSKent Overstreet 		}
541dcfc593fSKent Overstreet 		folio_batch_release(&fbatch);
542dcfc593fSKent Overstreet 		cond_resched();
543dcfc593fSKent Overstreet 	}
544dcfc593fSKent Overstreet 
545dcfc593fSKent Overstreet 	i_sectors_acct(c, inode, NULL, i_sectors_delta);
546dcfc593fSKent Overstreet }
547dcfc593fSKent Overstreet 
548e1036a2aSKent Overstreet static inline unsigned inode_nr_replicas(struct bch_fs *c, struct bch_inode_info *inode)
549e1036a2aSKent Overstreet {
550e1036a2aSKent Overstreet 	/* XXX: this should not be open coded */
551e1036a2aSKent Overstreet 	return inode->ei_inode.bi_data_replicas
552e1036a2aSKent Overstreet 		? inode->ei_inode.bi_data_replicas - 1
553e1036a2aSKent Overstreet 		: c->opts.data_replicas;
554e1036a2aSKent Overstreet }
555e1036a2aSKent Overstreet 
556f57a6a5dSKent Overstreet static inline unsigned sectors_to_reserve(struct bch_page_sector *s,
557f57a6a5dSKent Overstreet 						  unsigned nr_replicas)
558f57a6a5dSKent Overstreet {
559f57a6a5dSKent Overstreet 	return max(0, (int) nr_replicas -
560f57a6a5dSKent Overstreet 		   s->nr_replicas -
561f57a6a5dSKent Overstreet 		   s->replicas_reserved);
562f57a6a5dSKent Overstreet }
563f57a6a5dSKent Overstreet 
564f57a6a5dSKent Overstreet static int bch2_get_page_disk_reservation(struct bch_fs *c,
565f57a6a5dSKent Overstreet 				struct bch_inode_info *inode,
5661c6fdbd8SKent Overstreet 				struct page *page, bool check_enospc)
5671c6fdbd8SKent Overstreet {
568f57a6a5dSKent Overstreet 	struct bch_page_state *s = bch2_page_state_create(page, 0);
569e1036a2aSKent Overstreet 	unsigned nr_replicas = inode_nr_replicas(c, inode);
570f57a6a5dSKent Overstreet 	struct disk_reservation disk_res = { 0 };
571f57a6a5dSKent Overstreet 	unsigned i, disk_res_sectors = 0;
572f81b648dSKent Overstreet 	int ret;
5731c6fdbd8SKent Overstreet 
574f57a6a5dSKent Overstreet 	if (!s)
575f57a6a5dSKent Overstreet 		return -ENOMEM;
5761c6fdbd8SKent Overstreet 
577f57a6a5dSKent Overstreet 	for (i = 0; i < ARRAY_SIZE(s->s); i++)
578f57a6a5dSKent Overstreet 		disk_res_sectors += sectors_to_reserve(&s->s[i], nr_replicas);
579f57a6a5dSKent Overstreet 
580f57a6a5dSKent Overstreet 	if (!disk_res_sectors)
581f57a6a5dSKent Overstreet 		return 0;
582f57a6a5dSKent Overstreet 
583f57a6a5dSKent Overstreet 	ret = bch2_disk_reservation_get(c, &disk_res,
584f57a6a5dSKent Overstreet 					disk_res_sectors, 1,
585f57a6a5dSKent Overstreet 					!check_enospc
586f57a6a5dSKent Overstreet 					? BCH_DISK_RESERVATION_NOFAIL
587f57a6a5dSKent Overstreet 					: 0);
5881c6fdbd8SKent Overstreet 	if (unlikely(ret))
589f81b648dSKent Overstreet 		return ret;
590f81b648dSKent Overstreet 
591f57a6a5dSKent Overstreet 	for (i = 0; i < ARRAY_SIZE(s->s); i++)
592f57a6a5dSKent Overstreet 		s->s[i].replicas_reserved +=
593f57a6a5dSKent Overstreet 			sectors_to_reserve(&s->s[i], nr_replicas);
594f57a6a5dSKent Overstreet 
595f57a6a5dSKent Overstreet 	return 0;
5961c6fdbd8SKent Overstreet }
5971c6fdbd8SKent Overstreet 
598d1542e03SKent Overstreet struct bch2_page_reservation {
599d1542e03SKent Overstreet 	struct disk_reservation	disk;
600d1542e03SKent Overstreet 	struct quota_res	quota;
601d1542e03SKent Overstreet };
602d1542e03SKent Overstreet 
603d1542e03SKent Overstreet static void bch2_page_reservation_init(struct bch_fs *c,
604f57a6a5dSKent Overstreet 			struct bch_inode_info *inode,
605d1542e03SKent Overstreet 			struct bch2_page_reservation *res)
606d1542e03SKent Overstreet {
607d1542e03SKent Overstreet 	memset(res, 0, sizeof(*res));
608d1542e03SKent Overstreet 
609d1542e03SKent Overstreet 	res->disk.nr_replicas = inode_nr_replicas(c, inode);
610d1542e03SKent Overstreet }
611d1542e03SKent Overstreet 
612d1542e03SKent Overstreet static void bch2_page_reservation_put(struct bch_fs *c,
613d1542e03SKent Overstreet 			struct bch_inode_info *inode,
614d1542e03SKent Overstreet 			struct bch2_page_reservation *res)
615d1542e03SKent Overstreet {
616d1542e03SKent Overstreet 	bch2_disk_reservation_put(c, &res->disk);
617d1542e03SKent Overstreet 	bch2_quota_reservation_put(c, inode, &res->quota);
618d1542e03SKent Overstreet }
619d1542e03SKent Overstreet 
620d1542e03SKent Overstreet static int bch2_page_reservation_get(struct bch_fs *c,
621d1542e03SKent Overstreet 			struct bch_inode_info *inode, struct page *page,
622d1542e03SKent Overstreet 			struct bch2_page_reservation *res,
623bd954215SKent Overstreet 			unsigned offset, unsigned len)
624f57a6a5dSKent Overstreet {
625f57a6a5dSKent Overstreet 	struct bch_page_state *s = bch2_page_state_create(page, 0);
626d1542e03SKent Overstreet 	unsigned i, disk_sectors = 0, quota_sectors = 0;
627f57a6a5dSKent Overstreet 	int ret;
628f57a6a5dSKent Overstreet 
629f57a6a5dSKent Overstreet 	if (!s)
630f57a6a5dSKent Overstreet 		return -ENOMEM;
631f57a6a5dSKent Overstreet 
632e6ec361fSKent Overstreet 	BUG_ON(!s->uptodate);
633e6ec361fSKent Overstreet 
6344b0a66d5SKent Overstreet 	for (i = round_down(offset, block_bytes(c)) >> 9;
6354b0a66d5SKent Overstreet 	     i < round_up(offset + len, block_bytes(c)) >> 9;
636d1542e03SKent Overstreet 	     i++) {
637d1542e03SKent Overstreet 		disk_sectors += sectors_to_reserve(&s->s[i],
638d1542e03SKent Overstreet 						res->disk.nr_replicas);
639d1542e03SKent Overstreet 		quota_sectors += s->s[i].state == SECTOR_UNALLOCATED;
6401c6fdbd8SKent Overstreet 	}
6411c6fdbd8SKent Overstreet 
642d1542e03SKent Overstreet 	if (disk_sectors) {
643bd954215SKent Overstreet 		ret = bch2_disk_reservation_add(c, &res->disk, disk_sectors, 0);
644d1542e03SKent Overstreet 		if (unlikely(ret))
645d1542e03SKent Overstreet 			return ret;
646d1542e03SKent Overstreet 	}
647d1542e03SKent Overstreet 
648d1542e03SKent Overstreet 	if (quota_sectors) {
649d1542e03SKent Overstreet 		ret = bch2_quota_reservation_add(c, inode, &res->quota,
650bd954215SKent Overstreet 						 quota_sectors, true);
651d1542e03SKent Overstreet 		if (unlikely(ret)) {
652d1542e03SKent Overstreet 			struct disk_reservation tmp = {
653d1542e03SKent Overstreet 				.sectors = disk_sectors
654d1542e03SKent Overstreet 			};
655d1542e03SKent Overstreet 
656d1542e03SKent Overstreet 			bch2_disk_reservation_put(c, &tmp);
657d1542e03SKent Overstreet 			res->disk.sectors -= disk_sectors;
658d1542e03SKent Overstreet 			return ret;
659d1542e03SKent Overstreet 		}
660d1542e03SKent Overstreet 	}
661d1542e03SKent Overstreet 
662d1542e03SKent Overstreet 	return 0;
663f57a6a5dSKent Overstreet }
664f57a6a5dSKent Overstreet 
6651c6fdbd8SKent Overstreet static void bch2_clear_page_bits(struct page *page)
6661c6fdbd8SKent Overstreet {
6671c6fdbd8SKent Overstreet 	struct bch_inode_info *inode = to_bch_ei(page->mapping->host);
6681c6fdbd8SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
669f57a6a5dSKent Overstreet 	struct bch_page_state *s = bch2_page_state(page);
670d1542e03SKent Overstreet 	struct disk_reservation disk_res = { 0 };
671f57a6a5dSKent Overstreet 	int i, dirty_sectors = 0;
6721c6fdbd8SKent Overstreet 
673f57a6a5dSKent Overstreet 	if (!s)
6741c6fdbd8SKent Overstreet 		return;
6751c6fdbd8SKent Overstreet 
6763826ee0bSKent Overstreet 	EBUG_ON(!PageLocked(page));
6773826ee0bSKent Overstreet 	EBUG_ON(PageWriteback(page));
6783826ee0bSKent Overstreet 
679f57a6a5dSKent Overstreet 	for (i = 0; i < ARRAY_SIZE(s->s); i++) {
680d1542e03SKent Overstreet 		disk_res.sectors += s->s[i].replicas_reserved;
681d1542e03SKent Overstreet 		s->s[i].replicas_reserved = 0;
682d1542e03SKent Overstreet 
683b44a66a6SKent Overstreet 		switch (s->s[i].state) {
684b44a66a6SKent Overstreet 		case SECTOR_DIRTY:
685f57a6a5dSKent Overstreet 			s->s[i].state = SECTOR_UNALLOCATED;
686b44a66a6SKent Overstreet 			--dirty_sectors;
687b44a66a6SKent Overstreet 			break;
688b44a66a6SKent Overstreet 		case SECTOR_DIRTY_RESERVED:
689b44a66a6SKent Overstreet 			s->s[i].state = SECTOR_RESERVED;
690b44a66a6SKent Overstreet 			break;
691b44a66a6SKent Overstreet 		default:
692b44a66a6SKent Overstreet 			break;
693f57a6a5dSKent Overstreet 		}
694f57a6a5dSKent Overstreet 	}
695adfcfaf0SKent Overstreet 
696d1542e03SKent Overstreet 	bch2_disk_reservation_put(c, &disk_res);
697d1542e03SKent Overstreet 
698b44a66a6SKent Overstreet 	i_sectors_acct(c, inode, NULL, dirty_sectors);
699adfcfaf0SKent Overstreet 
700f57a6a5dSKent Overstreet 	bch2_page_state_release(page);
7011c6fdbd8SKent Overstreet }
7021c6fdbd8SKent Overstreet 
703d1542e03SKent Overstreet static void bch2_set_page_dirty(struct bch_fs *c,
704d1542e03SKent Overstreet 			struct bch_inode_info *inode, struct page *page,
705d1542e03SKent Overstreet 			struct bch2_page_reservation *res,
706d1542e03SKent Overstreet 			unsigned offset, unsigned len)
7071c6fdbd8SKent Overstreet {
708d1542e03SKent Overstreet 	struct bch_page_state *s = bch2_page_state(page);
709f57a6a5dSKent Overstreet 	unsigned i, dirty_sectors = 0;
7101c6fdbd8SKent Overstreet 
711877dfb34SKent Overstreet 	WARN_ON((u64) page_offset(page) + offset + len >
712877dfb34SKent Overstreet 		round_up((u64) i_size_read(&inode->v), block_bytes(c)));
713fb472ac5SKent Overstreet 
7143826ee0bSKent Overstreet 	spin_lock(&s->lock);
7153826ee0bSKent Overstreet 
7164b0a66d5SKent Overstreet 	for (i = round_down(offset, block_bytes(c)) >> 9;
7174b0a66d5SKent Overstreet 	     i < round_up(offset + len, block_bytes(c)) >> 9;
718d1542e03SKent Overstreet 	     i++) {
719d1542e03SKent Overstreet 		unsigned sectors = sectors_to_reserve(&s->s[i],
720d1542e03SKent Overstreet 						res->disk.nr_replicas);
7211c6fdbd8SKent Overstreet 
722406d6d5aSKent Overstreet 		/*
723406d6d5aSKent Overstreet 		 * This can happen if we race with the error path in
724406d6d5aSKent Overstreet 		 * bch2_writepage_io_done():
725406d6d5aSKent Overstreet 		 */
726406d6d5aSKent Overstreet 		sectors = min_t(unsigned, sectors, res->disk.sectors);
727406d6d5aSKent Overstreet 
728d1542e03SKent Overstreet 		s->s[i].replicas_reserved += sectors;
729d1542e03SKent Overstreet 		res->disk.sectors -= sectors;
730adfcfaf0SKent Overstreet 
731b44a66a6SKent Overstreet 		switch (s->s[i].state) {
732b44a66a6SKent Overstreet 		case SECTOR_UNALLOCATED:
733b44a66a6SKent Overstreet 			s->s[i].state = SECTOR_DIRTY;
734f57a6a5dSKent Overstreet 			dirty_sectors++;
735b44a66a6SKent Overstreet 			break;
736b44a66a6SKent Overstreet 		case SECTOR_RESERVED:
737b44a66a6SKent Overstreet 			s->s[i].state = SECTOR_DIRTY_RESERVED;
738b44a66a6SKent Overstreet 			break;
739b44a66a6SKent Overstreet 		default:
740b44a66a6SKent Overstreet 			break;
741b44a66a6SKent Overstreet 		}
742f57a6a5dSKent Overstreet 	}
743f57a6a5dSKent Overstreet 
7443826ee0bSKent Overstreet 	spin_unlock(&s->lock);
7453826ee0bSKent Overstreet 
746d1542e03SKent Overstreet 	i_sectors_acct(c, inode, &res->quota, dirty_sectors);
7471c6fdbd8SKent Overstreet 
748d1542e03SKent Overstreet 	if (!PageDirty(page))
749d1542e03SKent Overstreet 		filemap_dirty_folio(inode->v.i_mapping, page_folio(page));
7501c6fdbd8SKent Overstreet }
7511c6fdbd8SKent Overstreet 
7521c6fdbd8SKent Overstreet vm_fault_t bch2_page_fault(struct vm_fault *vmf)
7531c6fdbd8SKent Overstreet {
7541c6fdbd8SKent Overstreet 	struct file *file = vmf->vma->vm_file;
755eb8e6e9cSKent Overstreet 	struct address_space *mapping = file->f_mapping;
756eb8e6e9cSKent Overstreet 	struct address_space *fdm = faults_disabled_mapping();
7571c6fdbd8SKent Overstreet 	struct bch_inode_info *inode = file_bch_inode(file);
7581c6fdbd8SKent Overstreet 	int ret;
7591c6fdbd8SKent Overstreet 
760eb8e6e9cSKent Overstreet 	if (fdm == mapping)
761eb8e6e9cSKent Overstreet 		return VM_FAULT_SIGBUS;
762eb8e6e9cSKent Overstreet 
763eb8e6e9cSKent Overstreet 	/* Lock ordering: */
764eb8e6e9cSKent Overstreet 	if (fdm > mapping) {
765eb8e6e9cSKent Overstreet 		struct bch_inode_info *fdm_host = to_bch_ei(fdm->host);
766eb8e6e9cSKent Overstreet 
767a7ecd30cSKent Overstreet 		if (bch2_pagecache_add_tryget(inode))
768eb8e6e9cSKent Overstreet 			goto got_lock;
769eb8e6e9cSKent Overstreet 
770a7ecd30cSKent Overstreet 		bch2_pagecache_block_put(fdm_host);
771eb8e6e9cSKent Overstreet 
772a7ecd30cSKent Overstreet 		bch2_pagecache_add_get(inode);
773a7ecd30cSKent Overstreet 		bch2_pagecache_add_put(inode);
774eb8e6e9cSKent Overstreet 
775a7ecd30cSKent Overstreet 		bch2_pagecache_block_get(fdm_host);
776eb8e6e9cSKent Overstreet 
777eb8e6e9cSKent Overstreet 		/* Signal that lock has been dropped: */
778eb8e6e9cSKent Overstreet 		set_fdm_dropped_locks();
779eb8e6e9cSKent Overstreet 		return VM_FAULT_SIGBUS;
780eb8e6e9cSKent Overstreet 	}
781eb8e6e9cSKent Overstreet 
782a7ecd30cSKent Overstreet 	bch2_pagecache_add_get(inode);
783eb8e6e9cSKent Overstreet got_lock:
7841c6fdbd8SKent Overstreet 	ret = filemap_fault(vmf);
785a7ecd30cSKent Overstreet 	bch2_pagecache_add_put(inode);
7861c6fdbd8SKent Overstreet 
7871c6fdbd8SKent Overstreet 	return ret;
7881c6fdbd8SKent Overstreet }
7891c6fdbd8SKent Overstreet 
7901c6fdbd8SKent Overstreet vm_fault_t bch2_page_mkwrite(struct vm_fault *vmf)
7911c6fdbd8SKent Overstreet {
7921c6fdbd8SKent Overstreet 	struct page *page = vmf->page;
7931c6fdbd8SKent Overstreet 	struct file *file = vmf->vma->vm_file;
7941c6fdbd8SKent Overstreet 	struct bch_inode_info *inode = file_bch_inode(file);
7951c6fdbd8SKent Overstreet 	struct address_space *mapping = file->f_mapping;
7961c6fdbd8SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
797d1542e03SKent Overstreet 	struct bch2_page_reservation res;
7986cc3535dSKent Overstreet 	unsigned len;
7996cc3535dSKent Overstreet 	loff_t isize;
800e6ec361fSKent Overstreet 	int ret;
8011c6fdbd8SKent Overstreet 
802d1542e03SKent Overstreet 	bch2_page_reservation_init(c, inode, &res);
803d1542e03SKent Overstreet 
8041c6fdbd8SKent Overstreet 	sb_start_pagefault(inode->v.i_sb);
8051c6fdbd8SKent Overstreet 	file_update_time(file);
8061c6fdbd8SKent Overstreet 
8071c6fdbd8SKent Overstreet 	/*
8081c6fdbd8SKent Overstreet 	 * Not strictly necessary, but helps avoid dio writes livelocking in
8091c6fdbd8SKent Overstreet 	 * write_invalidate_inode_pages_range() - can drop this if/when we get
8101c6fdbd8SKent Overstreet 	 * a write_invalidate_inode_pages_range() that works without dropping
8111c6fdbd8SKent Overstreet 	 * page lock before invalidating page
8121c6fdbd8SKent Overstreet 	 */
813a7ecd30cSKent Overstreet 	bch2_pagecache_add_get(inode);
8141c6fdbd8SKent Overstreet 
8151c6fdbd8SKent Overstreet 	lock_page(page);
8166cc3535dSKent Overstreet 	isize = i_size_read(&inode->v);
8176cc3535dSKent Overstreet 
8186cc3535dSKent Overstreet 	if (page->mapping != mapping || page_offset(page) >= isize) {
8191c6fdbd8SKent Overstreet 		unlock_page(page);
8201c6fdbd8SKent Overstreet 		ret = VM_FAULT_NOPAGE;
8211c6fdbd8SKent Overstreet 		goto out;
8221c6fdbd8SKent Overstreet 	}
8231c6fdbd8SKent Overstreet 
824d55460bbSKent Overstreet 	len = min_t(loff_t, PAGE_SIZE, isize - page_offset(page));
8256cc3535dSKent Overstreet 
826e6ec361fSKent Overstreet 	if (!bch2_page_state_create(page, __GFP_NOFAIL)->uptodate) {
827e6ec361fSKent Overstreet 		if (bch2_page_state_set(c, inode_inum(inode), &page, 1)) {
828e6ec361fSKent Overstreet 			unlock_page(page);
829e6ec361fSKent Overstreet 			ret = VM_FAULT_SIGBUS;
830e6ec361fSKent Overstreet 			goto out;
831e6ec361fSKent Overstreet 		}
832e6ec361fSKent Overstreet 	}
833e6ec361fSKent Overstreet 
834bd954215SKent Overstreet 	if (bch2_page_reservation_get(c, inode, page, &res, 0, len)) {
8351c6fdbd8SKent Overstreet 		unlock_page(page);
8361c6fdbd8SKent Overstreet 		ret = VM_FAULT_SIGBUS;
8371c6fdbd8SKent Overstreet 		goto out;
8381c6fdbd8SKent Overstreet 	}
8391c6fdbd8SKent Overstreet 
8406cc3535dSKent Overstreet 	bch2_set_page_dirty(c, inode, page, &res, 0, len);
8411b783a69SKent Overstreet 	bch2_page_reservation_put(c, inode, &res);
8421b783a69SKent Overstreet 
8431c6fdbd8SKent Overstreet 	wait_for_stable_page(page);
844e6ec361fSKent Overstreet 	ret = VM_FAULT_LOCKED;
8451c6fdbd8SKent Overstreet out:
846a7ecd30cSKent Overstreet 	bch2_pagecache_add_put(inode);
8471c6fdbd8SKent Overstreet 	sb_end_pagefault(inode->v.i_sb);
848d1542e03SKent Overstreet 
8491c6fdbd8SKent Overstreet 	return ret;
8501c6fdbd8SKent Overstreet }
8511c6fdbd8SKent Overstreet 
8521c6fdbd8SKent Overstreet void bch2_invalidate_folio(struct folio *folio, size_t offset, size_t length)
8531c6fdbd8SKent Overstreet {
8541c6fdbd8SKent Overstreet 	if (offset || length < folio_size(folio))
8551c6fdbd8SKent Overstreet 		return;
8561c6fdbd8SKent Overstreet 
8571c6fdbd8SKent Overstreet 	bch2_clear_page_bits(&folio->page);
8581c6fdbd8SKent Overstreet }
8591c6fdbd8SKent Overstreet 
8601c6fdbd8SKent Overstreet bool bch2_release_folio(struct folio *folio, gfp_t gfp_mask)
8611c6fdbd8SKent Overstreet {
862a6d90385SKent Overstreet 	if (folio_test_dirty(folio) || folio_test_writeback(folio))
8631c6fdbd8SKent Overstreet 		return false;
8641c6fdbd8SKent Overstreet 
8651c6fdbd8SKent Overstreet 	bch2_clear_page_bits(&folio->page);
8661c6fdbd8SKent Overstreet 	return true;
8671c6fdbd8SKent Overstreet }
8681c6fdbd8SKent Overstreet 
8691c6fdbd8SKent Overstreet /* readpage(s): */
8701c6fdbd8SKent Overstreet 
8711c6fdbd8SKent Overstreet static void bch2_readpages_end_io(struct bio *bio)
8721c6fdbd8SKent Overstreet {
8731c6fdbd8SKent Overstreet 	struct bvec_iter_all iter;
8741c6fdbd8SKent Overstreet 	struct bio_vec *bv;
8751c6fdbd8SKent Overstreet 
8761c6fdbd8SKent Overstreet 	bio_for_each_segment_all(bv, bio, iter) {
8771c6fdbd8SKent Overstreet 		struct page *page = bv->bv_page;
8781c6fdbd8SKent Overstreet 
8791c6fdbd8SKent Overstreet 		if (!bio->bi_status) {
8801c6fdbd8SKent Overstreet 			SetPageUptodate(page);
8811c6fdbd8SKent Overstreet 		} else {
8821c6fdbd8SKent Overstreet 			ClearPageUptodate(page);
8831c6fdbd8SKent Overstreet 			SetPageError(page);
8841c6fdbd8SKent Overstreet 		}
8851c6fdbd8SKent Overstreet 		unlock_page(page);
8861c6fdbd8SKent Overstreet 	}
8871c6fdbd8SKent Overstreet 
8881c6fdbd8SKent Overstreet 	bio_put(bio);
8891c6fdbd8SKent Overstreet }
8901c6fdbd8SKent Overstreet 
8911c6fdbd8SKent Overstreet struct readpages_iter {
8921c6fdbd8SKent Overstreet 	struct address_space	*mapping;
8931c6fdbd8SKent Overstreet 	struct page		**pages;
8941c6fdbd8SKent Overstreet 	unsigned		nr_pages;
8951c6fdbd8SKent Overstreet 	unsigned		idx;
8961c6fdbd8SKent Overstreet 	pgoff_t			offset;
8971c6fdbd8SKent Overstreet };
8981c6fdbd8SKent Overstreet 
8991c6fdbd8SKent Overstreet static int readpages_iter_init(struct readpages_iter *iter,
9001c6fdbd8SKent Overstreet 			       struct readahead_control *ractl)
9011c6fdbd8SKent Overstreet {
9021c6fdbd8SKent Overstreet 	unsigned i, nr_pages = readahead_count(ractl);
9031c6fdbd8SKent Overstreet 
9041c6fdbd8SKent Overstreet 	memset(iter, 0, sizeof(*iter));
9051c6fdbd8SKent Overstreet 
9061c6fdbd8SKent Overstreet 	iter->mapping	= ractl->mapping;
9071c6fdbd8SKent Overstreet 	iter->offset	= readahead_index(ractl);
9081c6fdbd8SKent Overstreet 	iter->nr_pages	= nr_pages;
9091c6fdbd8SKent Overstreet 
9101c6fdbd8SKent Overstreet 	iter->pages = kmalloc_array(nr_pages, sizeof(struct page *), GFP_NOFS);
9111c6fdbd8SKent Overstreet 	if (!iter->pages)
9121c6fdbd8SKent Overstreet 		return -ENOMEM;
9131c6fdbd8SKent Overstreet 
91489931472SKent Overstreet 	nr_pages = __readahead_batch(ractl, iter->pages, nr_pages);
9151c6fdbd8SKent Overstreet 	for (i = 0; i < nr_pages; i++) {
916f57a6a5dSKent Overstreet 		__bch2_page_state_create(iter->pages[i], __GFP_NOFAIL);
9171c6fdbd8SKent Overstreet 		put_page(iter->pages[i]);
9181c6fdbd8SKent Overstreet 	}
9191c6fdbd8SKent Overstreet 
9201c6fdbd8SKent Overstreet 	return 0;
9211c6fdbd8SKent Overstreet }
9221c6fdbd8SKent Overstreet 
9231c6fdbd8SKent Overstreet static inline struct page *readpage_iter_next(struct readpages_iter *iter)
9241c6fdbd8SKent Overstreet {
9251c6fdbd8SKent Overstreet 	if (iter->idx >= iter->nr_pages)
9261c6fdbd8SKent Overstreet 		return NULL;
9271c6fdbd8SKent Overstreet 
9281c6fdbd8SKent Overstreet 	EBUG_ON(iter->pages[iter->idx]->index != iter->offset + iter->idx);
9291c6fdbd8SKent Overstreet 
9301c6fdbd8SKent Overstreet 	return iter->pages[iter->idx];
9311c6fdbd8SKent Overstreet }
9321c6fdbd8SKent Overstreet 
93335189e09SKent Overstreet static bool extent_partial_reads_expensive(struct bkey_s_c k)
93435189e09SKent Overstreet {
93535189e09SKent Overstreet 	struct bkey_ptrs_c ptrs = bch2_bkey_ptrs_c(k);
93635189e09SKent Overstreet 	struct bch_extent_crc_unpacked crc;
93735189e09SKent Overstreet 	const union bch_extent_entry *i;
93835189e09SKent Overstreet 
93935189e09SKent Overstreet 	bkey_for_each_crc(k.k, ptrs, crc, i)
94035189e09SKent Overstreet 		if (crc.csum_type || crc.compression_type)
94135189e09SKent Overstreet 			return true;
94235189e09SKent Overstreet 	return false;
94335189e09SKent Overstreet }
94435189e09SKent Overstreet 
9451c6fdbd8SKent Overstreet static void readpage_bio_extend(struct readpages_iter *iter,
94676426098SKent Overstreet 				struct bio *bio,
94776426098SKent Overstreet 				unsigned sectors_this_extent,
9481c6fdbd8SKent Overstreet 				bool get_more)
9491c6fdbd8SKent Overstreet {
95076426098SKent Overstreet 	while (bio_sectors(bio) < sectors_this_extent &&
9511c6fdbd8SKent Overstreet 	       bio->bi_vcnt < bio->bi_max_vecs) {
9527279c1a2SKent Overstreet 		pgoff_t page_offset = bio_end_sector(bio) >> PAGE_SECTORS_SHIFT;
9531c6fdbd8SKent Overstreet 		struct page *page = readpage_iter_next(iter);
9541c6fdbd8SKent Overstreet 		int ret;
9551c6fdbd8SKent Overstreet 
9561c6fdbd8SKent Overstreet 		if (page) {
9571c6fdbd8SKent Overstreet 			if (iter->offset + iter->idx != page_offset)
9581c6fdbd8SKent Overstreet 				break;
9591c6fdbd8SKent Overstreet 
9601c6fdbd8SKent Overstreet 			iter->idx++;
9611c6fdbd8SKent Overstreet 		} else {
9621c6fdbd8SKent Overstreet 			if (!get_more)
9631c6fdbd8SKent Overstreet 				break;
9641c6fdbd8SKent Overstreet 
9651c6fdbd8SKent Overstreet 			page = xa_load(&iter->mapping->i_pages, page_offset);
9661c6fdbd8SKent Overstreet 			if (page && !xa_is_value(page))
9671c6fdbd8SKent Overstreet 				break;
9681c6fdbd8SKent Overstreet 
9691c6fdbd8SKent Overstreet 			page = __page_cache_alloc(readahead_gfp_mask(iter->mapping));
9701c6fdbd8SKent Overstreet 			if (!page)
9711c6fdbd8SKent Overstreet 				break;
9721c6fdbd8SKent Overstreet 
973f57a6a5dSKent Overstreet 			if (!__bch2_page_state_create(page, 0)) {
974f57a6a5dSKent Overstreet 				put_page(page);
975f57a6a5dSKent Overstreet 				break;
976f57a6a5dSKent Overstreet 			}
9771c6fdbd8SKent Overstreet 
9781c6fdbd8SKent Overstreet 			ret = add_to_page_cache_lru(page, iter->mapping,
9791c6fdbd8SKent Overstreet 						    page_offset, GFP_NOFS);
9801c6fdbd8SKent Overstreet 			if (ret) {
981f57a6a5dSKent Overstreet 				__bch2_page_state_release(page);
9821c6fdbd8SKent Overstreet 				put_page(page);
9831c6fdbd8SKent Overstreet 				break;
9841c6fdbd8SKent Overstreet 			}
9851c6fdbd8SKent Overstreet 
9861c6fdbd8SKent Overstreet 			put_page(page);
9871c6fdbd8SKent Overstreet 		}
9881c6fdbd8SKent Overstreet 
989885678f6SKent Overstreet 		BUG_ON(!bio_add_page(bio, page, PAGE_SIZE, 0));
9901c6fdbd8SKent Overstreet 	}
9911c6fdbd8SKent Overstreet }
9921c6fdbd8SKent Overstreet 
9938c6d298aSKent Overstreet static void bchfs_read(struct btree_trans *trans,
9948c6d298aSKent Overstreet 		       struct bch_read_bio *rbio,
9958c6d298aSKent Overstreet 		       subvol_inum inum,
9961c6fdbd8SKent Overstreet 		       struct readpages_iter *readpages_iter)
9971c6fdbd8SKent Overstreet {
9980f238367SKent Overstreet 	struct bch_fs *c = trans->c;
9998c6d298aSKent Overstreet 	struct btree_iter iter;
100007a1006aSKent Overstreet 	struct bkey_buf sk;
10011c6fdbd8SKent Overstreet 	int flags = BCH_READ_RETRY_IF_STALE|
10021c6fdbd8SKent Overstreet 		BCH_READ_MAY_PROMOTE;
10038c6d298aSKent Overstreet 	u32 snapshot;
100476426098SKent Overstreet 	int ret = 0;
10051c6fdbd8SKent Overstreet 
10061c6fdbd8SKent Overstreet 	rbio->c = c;
10071c6fdbd8SKent Overstreet 	rbio->start_time = local_clock();
10088c6d298aSKent Overstreet 	rbio->subvol = inum.subvol;
100935189e09SKent Overstreet 
101007a1006aSKent Overstreet 	bch2_bkey_buf_init(&sk);
101176426098SKent Overstreet retry:
1012700c25b3SKent Overstreet 	bch2_trans_begin(trans);
10138c6d298aSKent Overstreet 	iter = (struct btree_iter) { NULL };
1014700c25b3SKent Overstreet 
10158c6d298aSKent Overstreet 	ret = bch2_subvolume_get_snapshot(trans, inum.subvol, &snapshot);
10168c6d298aSKent Overstreet 	if (ret)
10178c6d298aSKent Overstreet 		goto err;
10188c6d298aSKent Overstreet 
10198c6d298aSKent Overstreet 	bch2_trans_iter_init(trans, &iter, BTREE_ID_extents,
10208c6d298aSKent Overstreet 			     SPOS(inum.inum, rbio->bio.bi_iter.bi_sector, snapshot),
102157cfdd8bSKent Overstreet 			     BTREE_ITER_SLOTS);
10221c6fdbd8SKent Overstreet 	while (1) {
10231c6fdbd8SKent Overstreet 		struct bkey_s_c k;
102476426098SKent Overstreet 		unsigned bytes, sectors, offset_into_extent;
10255ff75ccbSKent Overstreet 		enum btree_id data_btree = BTREE_ID_extents;
10261c6fdbd8SKent Overstreet 
10273737e0ddSKent Overstreet 		/*
10283737e0ddSKent Overstreet 		 * read_extent -> io_time_reset may cause a transaction restart
10293737e0ddSKent Overstreet 		 * without returning an error, we need to check for that here:
10303737e0ddSKent Overstreet 		 */
1031549d173cSKent Overstreet 		ret = bch2_trans_relock(trans);
1032549d173cSKent Overstreet 		if (ret)
10333737e0ddSKent Overstreet 			break;
10343737e0ddSKent Overstreet 
10358c6d298aSKent Overstreet 		bch2_btree_iter_set_pos(&iter,
10368c6d298aSKent Overstreet 				POS(inum.inum, rbio->bio.bi_iter.bi_sector));
10371c6fdbd8SKent Overstreet 
10388c6d298aSKent Overstreet 		k = bch2_btree_iter_peek_slot(&iter);
103976426098SKent Overstreet 		ret = bkey_err(k);
104076426098SKent Overstreet 		if (ret)
104176426098SKent Overstreet 			break;
10421c6fdbd8SKent Overstreet 
10438c6d298aSKent Overstreet 		offset_into_extent = iter.pos.offset -
104406ed8558SKent Overstreet 			bkey_start_offset(k.k);
104576426098SKent Overstreet 		sectors = k.k->size - offset_into_extent;
104676426098SKent Overstreet 
104707a1006aSKent Overstreet 		bch2_bkey_buf_reassemble(&sk, c, k);
104813dcd4abSKent Overstreet 
10495ff75ccbSKent Overstreet 		ret = bch2_read_indirect_extent(trans, &data_btree,
105022d8a33dSYuxuan Shui 					&offset_into_extent, &sk);
105176426098SKent Overstreet 		if (ret)
105276426098SKent Overstreet 			break;
105376426098SKent Overstreet 
105413dcd4abSKent Overstreet 		k = bkey_i_to_s_c(sk.k);
105513dcd4abSKent Overstreet 
105676426098SKent Overstreet 		sectors = min(sectors, k.k->size - offset_into_extent);
105776426098SKent Overstreet 
105835189e09SKent Overstreet 		if (readpages_iter)
105935189e09SKent Overstreet 			readpage_bio_extend(readpages_iter, &rbio->bio, sectors,
106035189e09SKent Overstreet 					    extent_partial_reads_expensive(k));
10611c6fdbd8SKent Overstreet 
106276426098SKent Overstreet 		bytes = min(sectors, bio_sectors(&rbio->bio)) << 9;
106306ed8558SKent Overstreet 		swap(rbio->bio.bi_iter.bi_size, bytes);
10641c6fdbd8SKent Overstreet 
106506ed8558SKent Overstreet 		if (rbio->bio.bi_iter.bi_size == bytes)
10661c6fdbd8SKent Overstreet 			flags |= BCH_READ_LAST_FRAGMENT;
10671c6fdbd8SKent Overstreet 
1068b44a66a6SKent Overstreet 		bch2_bio_page_state_set(&rbio->bio, k);
10691c6fdbd8SKent Overstreet 
10708c6d298aSKent Overstreet 		bch2_read_extent(trans, rbio, iter.pos,
10715ff75ccbSKent Overstreet 				 data_btree, k, offset_into_extent, flags);
10721c6fdbd8SKent Overstreet 
10731c6fdbd8SKent Overstreet 		if (flags & BCH_READ_LAST_FRAGMENT)
107435189e09SKent Overstreet 			break;
10751c6fdbd8SKent Overstreet 
107606ed8558SKent Overstreet 		swap(rbio->bio.bi_iter.bi_size, bytes);
107706ed8558SKent Overstreet 		bio_advance(&rbio->bio, bytes);
1078084d42bbSKent Overstreet 
1079084d42bbSKent Overstreet 		ret = btree_trans_too_many_iters(trans);
1080084d42bbSKent Overstreet 		if (ret)
1081084d42bbSKent Overstreet 			break;
10821c6fdbd8SKent Overstreet 	}
10838c6d298aSKent Overstreet err:
10848c6d298aSKent Overstreet 	bch2_trans_iter_exit(trans, &iter);
108576426098SKent Overstreet 
1086549d173cSKent Overstreet 	if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
108776426098SKent Overstreet 		goto retry;
108876426098SKent Overstreet 
108935189e09SKent Overstreet 	if (ret) {
10907fec8266SKent Overstreet 		bch_err_inum_offset_ratelimited(c,
10917fec8266SKent Overstreet 				iter.pos.inode,
10927fec8266SKent Overstreet 				iter.pos.offset << 9,
10930fefe8d8SKent Overstreet 				"read error %i from btree lookup", ret);
10940fefe8d8SKent Overstreet 		rbio->bio.bi_status = BLK_STS_IOERR;
109576426098SKent Overstreet 		bio_endio(&rbio->bio);
10961c6fdbd8SKent Overstreet 	}
10971c6fdbd8SKent Overstreet 
109807a1006aSKent Overstreet 	bch2_bkey_buf_exit(&sk, c);
109935189e09SKent Overstreet }
110035189e09SKent Overstreet 
11011c6fdbd8SKent Overstreet void bch2_readahead(struct readahead_control *ractl)
11021c6fdbd8SKent Overstreet {
11031c6fdbd8SKent Overstreet 	struct bch_inode_info *inode = to_bch_ei(ractl->mapping->host);
11041c6fdbd8SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
110501ad6737SKent Overstreet 	struct bch_io_opts opts;
1106424eb881SKent Overstreet 	struct btree_trans trans;
11071c6fdbd8SKent Overstreet 	struct page *page;
11081c6fdbd8SKent Overstreet 	struct readpages_iter readpages_iter;
11091c6fdbd8SKent Overstreet 	int ret;
11101c6fdbd8SKent Overstreet 
111101ad6737SKent Overstreet 	bch2_inode_opts_get(&opts, c, &inode->ei_inode);
111201ad6737SKent Overstreet 
11131c6fdbd8SKent Overstreet 	ret = readpages_iter_init(&readpages_iter, ractl);
11141c6fdbd8SKent Overstreet 	BUG_ON(ret);
11151c6fdbd8SKent Overstreet 
111620bceecbSKent Overstreet 	bch2_trans_init(&trans, c, 0, 0);
11171c6fdbd8SKent Overstreet 
1118a7ecd30cSKent Overstreet 	bch2_pagecache_add_get(inode);
11191c6fdbd8SKent Overstreet 
11201c6fdbd8SKent Overstreet 	while ((page = readpage_iter_next(&readpages_iter))) {
11211c6fdbd8SKent Overstreet 		pgoff_t index = readpages_iter.offset + readpages_iter.idx;
11221c6fdbd8SKent Overstreet 		unsigned n = min_t(unsigned,
11231c6fdbd8SKent Overstreet 				   readpages_iter.nr_pages -
11241c6fdbd8SKent Overstreet 				   readpages_iter.idx,
11251c6fdbd8SKent Overstreet 				   BIO_MAX_VECS);
11261c6fdbd8SKent Overstreet 		struct bch_read_bio *rbio =
11271c6fdbd8SKent Overstreet 			rbio_init(bio_alloc_bioset(NULL, n, REQ_OP_READ,
11281c6fdbd8SKent Overstreet 						   GFP_NOFS, &c->bio_read),
11291c6fdbd8SKent Overstreet 				  opts);
11301c6fdbd8SKent Overstreet 
11311c6fdbd8SKent Overstreet 		readpages_iter.idx++;
11321c6fdbd8SKent Overstreet 
11337279c1a2SKent Overstreet 		rbio->bio.bi_iter.bi_sector = (sector_t) index << PAGE_SECTORS_SHIFT;
11341c6fdbd8SKent Overstreet 		rbio->bio.bi_end_io = bch2_readpages_end_io;
1135885678f6SKent Overstreet 		BUG_ON(!bio_add_page(&rbio->bio, page, PAGE_SIZE, 0));
11361c6fdbd8SKent Overstreet 
11378c6d298aSKent Overstreet 		bchfs_read(&trans, rbio, inode_inum(inode),
11380f238367SKent Overstreet 			   &readpages_iter);
11391c6fdbd8SKent Overstreet 	}
11401c6fdbd8SKent Overstreet 
1141a7ecd30cSKent Overstreet 	bch2_pagecache_add_put(inode);
1142424eb881SKent Overstreet 
1143424eb881SKent Overstreet 	bch2_trans_exit(&trans);
11441c6fdbd8SKent Overstreet 	kfree(readpages_iter.pages);
11451c6fdbd8SKent Overstreet }
11461c6fdbd8SKent Overstreet 
11471c6fdbd8SKent Overstreet static void __bchfs_readpage(struct bch_fs *c, struct bch_read_bio *rbio,
11488c6d298aSKent Overstreet 			     subvol_inum inum, struct page *page)
11491c6fdbd8SKent Overstreet {
1150424eb881SKent Overstreet 	struct btree_trans trans;
11511c6fdbd8SKent Overstreet 
1152f57a6a5dSKent Overstreet 	bch2_page_state_create(page, __GFP_NOFAIL);
11531c6fdbd8SKent Overstreet 
11541c6fdbd8SKent Overstreet 	rbio->bio.bi_opf = REQ_OP_READ|REQ_SYNC;
11557f5e31e1SKent Overstreet 	rbio->bio.bi_iter.bi_sector =
11567279c1a2SKent Overstreet 		(sector_t) page->index << PAGE_SECTORS_SHIFT;
11577f5e31e1SKent Overstreet 	BUG_ON(!bio_add_page(&rbio->bio, page, PAGE_SIZE, 0));
11581c6fdbd8SKent Overstreet 
115920bceecbSKent Overstreet 	bch2_trans_init(&trans, c, 0, 0);
11608c6d298aSKent Overstreet 	bchfs_read(&trans, rbio, inum, NULL);
1161424eb881SKent Overstreet 	bch2_trans_exit(&trans);
11621c6fdbd8SKent Overstreet }
11631c6fdbd8SKent Overstreet 
11641c6fdbd8SKent Overstreet static void bch2_read_single_page_end_io(struct bio *bio)
11651c6fdbd8SKent Overstreet {
11661c6fdbd8SKent Overstreet 	complete(bio->bi_private);
11671c6fdbd8SKent Overstreet }
11681c6fdbd8SKent Overstreet 
11691c6fdbd8SKent Overstreet static int bch2_read_single_page(struct page *page,
11701c6fdbd8SKent Overstreet 				 struct address_space *mapping)
11711c6fdbd8SKent Overstreet {
11721c6fdbd8SKent Overstreet 	struct bch_inode_info *inode = to_bch_ei(mapping->host);
11731c6fdbd8SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
11741c6fdbd8SKent Overstreet 	struct bch_read_bio *rbio;
117501ad6737SKent Overstreet 	struct bch_io_opts opts;
11761c6fdbd8SKent Overstreet 	int ret;
11771c6fdbd8SKent Overstreet 	DECLARE_COMPLETION_ONSTACK(done);
11781c6fdbd8SKent Overstreet 
117901ad6737SKent Overstreet 	bch2_inode_opts_get(&opts, c, &inode->ei_inode);
118001ad6737SKent Overstreet 
11811c6fdbd8SKent Overstreet 	rbio = rbio_init(bio_alloc_bioset(NULL, 1, REQ_OP_READ, GFP_NOFS, &c->bio_read),
118201ad6737SKent Overstreet 			 opts);
11831c6fdbd8SKent Overstreet 	rbio->bio.bi_private = &done;
11841c6fdbd8SKent Overstreet 	rbio->bio.bi_end_io = bch2_read_single_page_end_io;
11851c6fdbd8SKent Overstreet 
11868c6d298aSKent Overstreet 	__bchfs_readpage(c, rbio, inode_inum(inode), page);
11871c6fdbd8SKent Overstreet 	wait_for_completion(&done);
11881c6fdbd8SKent Overstreet 
11891c6fdbd8SKent Overstreet 	ret = blk_status_to_errno(rbio->bio.bi_status);
11901c6fdbd8SKent Overstreet 	bio_put(&rbio->bio);
11911c6fdbd8SKent Overstreet 
11921c6fdbd8SKent Overstreet 	if (ret < 0)
11931c6fdbd8SKent Overstreet 		return ret;
11941c6fdbd8SKent Overstreet 
11951c6fdbd8SKent Overstreet 	SetPageUptodate(page);
11961c6fdbd8SKent Overstreet 	return 0;
11971c6fdbd8SKent Overstreet }
11981c6fdbd8SKent Overstreet 
11991c6fdbd8SKent Overstreet int bch2_read_folio(struct file *file, struct folio *folio)
12001c6fdbd8SKent Overstreet {
12011c6fdbd8SKent Overstreet 	struct page *page = &folio->page;
12021c6fdbd8SKent Overstreet 	int ret;
12031c6fdbd8SKent Overstreet 
12041c6fdbd8SKent Overstreet 	ret = bch2_read_single_page(page, page->mapping);
12051c6fdbd8SKent Overstreet 	folio_unlock(folio);
12065c1ef830SKent Overstreet 	return bch2_err_class(ret);
12071c6fdbd8SKent Overstreet }
12081c6fdbd8SKent Overstreet 
12091c6fdbd8SKent Overstreet /* writepages: */
12101c6fdbd8SKent Overstreet 
12111c6fdbd8SKent Overstreet struct bch_writepage_state {
12121c6fdbd8SKent Overstreet 	struct bch_writepage_io	*io;
12131c6fdbd8SKent Overstreet 	struct bch_io_opts	opts;
12141c6fdbd8SKent Overstreet };
12151c6fdbd8SKent Overstreet 
12161c6fdbd8SKent Overstreet static inline struct bch_writepage_state bch_writepage_state_init(struct bch_fs *c,
12171c6fdbd8SKent Overstreet 								  struct bch_inode_info *inode)
12181c6fdbd8SKent Overstreet {
121901ad6737SKent Overstreet 	struct bch_writepage_state ret = { 0 };
122001ad6737SKent Overstreet 
122101ad6737SKent Overstreet 	bch2_inode_opts_get(&ret.opts, c, &inode->ei_inode);
122201ad6737SKent Overstreet 	return ret;
12231c6fdbd8SKent Overstreet }
12241c6fdbd8SKent Overstreet 
12259f311f21SKent Overstreet static void bch2_writepage_io_done(struct bch_write_op *op)
12261c6fdbd8SKent Overstreet {
12279f311f21SKent Overstreet 	struct bch_writepage_io *io =
12289f311f21SKent Overstreet 		container_of(op, struct bch_writepage_io, op);
12299a3df993SKent Overstreet 	struct bch_fs *c = io->op.c;
12309a3df993SKent Overstreet 	struct bio *bio = &io->op.wbio.bio;
12311c6fdbd8SKent Overstreet 	struct bvec_iter_all iter;
12321c6fdbd8SKent Overstreet 	struct bio_vec *bvec;
1233b3fce09cSKent Overstreet 	unsigned i;
12341c6fdbd8SKent Overstreet 
12359a3df993SKent Overstreet 	if (io->op.error) {
123633c74e41SKent Overstreet 		set_bit(EI_INODE_ERROR, &io->inode->ei_flags);
123733c74e41SKent Overstreet 
123875812e70SKent Overstreet 		bio_for_each_segment_all(bvec, bio, iter) {
1239b3fce09cSKent Overstreet 			struct bch_page_state *s;
1240b3fce09cSKent Overstreet 
12411c6fdbd8SKent Overstreet 			SetPageError(bvec->bv_page);
124275812e70SKent Overstreet 			mapping_set_error(bvec->bv_page->mapping, -EIO);
1243b3fce09cSKent Overstreet 
12443826ee0bSKent Overstreet 			s = __bch2_page_state(bvec->bv_page);
12453826ee0bSKent Overstreet 			spin_lock(&s->lock);
1246b3fce09cSKent Overstreet 			for (i = 0; i < PAGE_SECTORS; i++)
1247b3fce09cSKent Overstreet 				s->s[i].nr_replicas = 0;
12483826ee0bSKent Overstreet 			spin_unlock(&s->lock);
124975812e70SKent Overstreet 		}
12501c6fdbd8SKent Overstreet 	}
12511c6fdbd8SKent Overstreet 
12524be1a412SKent Overstreet 	if (io->op.flags & BCH_WRITE_WROTE_DATA_INLINE) {
12534be1a412SKent Overstreet 		bio_for_each_segment_all(bvec, bio, iter) {
12544be1a412SKent Overstreet 			struct bch_page_state *s;
12554be1a412SKent Overstreet 
12564be1a412SKent Overstreet 			s = __bch2_page_state(bvec->bv_page);
12574be1a412SKent Overstreet 			spin_lock(&s->lock);
12584be1a412SKent Overstreet 			for (i = 0; i < PAGE_SECTORS; i++)
12594be1a412SKent Overstreet 				s->s[i].nr_replicas = 0;
12604be1a412SKent Overstreet 			spin_unlock(&s->lock);
12614be1a412SKent Overstreet 		}
12624be1a412SKent Overstreet 	}
12634be1a412SKent Overstreet 
12641c6fdbd8SKent Overstreet 	/*
12651c6fdbd8SKent Overstreet 	 * racing with fallocate can cause us to add fewer sectors than
12661c6fdbd8SKent Overstreet 	 * expected - but we shouldn't add more sectors than expected:
12671c6fdbd8SKent Overstreet 	 */
1268f8494d25SKent Overstreet 	WARN_ON_ONCE(io->op.i_sectors_delta > 0);
12691c6fdbd8SKent Overstreet 
12701c6fdbd8SKent Overstreet 	/*
12711c6fdbd8SKent Overstreet 	 * (error (due to going RO) halfway through a page can screw that up
12721c6fdbd8SKent Overstreet 	 * slightly)
12731c6fdbd8SKent Overstreet 	 * XXX wtf?
12749a3df993SKent Overstreet 	   BUG_ON(io->op.op.i_sectors_delta >= PAGE_SECTORS);
12751c6fdbd8SKent Overstreet 	 */
12761c6fdbd8SKent Overstreet 
12771c6fdbd8SKent Overstreet 	/*
12781c6fdbd8SKent Overstreet 	 * PageWriteback is effectively our ref on the inode - fixup i_blocks
12791c6fdbd8SKent Overstreet 	 * before calling end_page_writeback:
12801c6fdbd8SKent Overstreet 	 */
12819a3df993SKent Overstreet 	i_sectors_acct(c, io->inode, NULL, io->op.i_sectors_delta);
12821c6fdbd8SKent Overstreet 
12837f5e31e1SKent Overstreet 	bio_for_each_segment_all(bvec, bio, iter) {
12847f5e31e1SKent Overstreet 		struct bch_page_state *s = __bch2_page_state(bvec->bv_page);
12857f5e31e1SKent Overstreet 
12867f5e31e1SKent Overstreet 		if (atomic_dec_and_test(&s->write_count))
12871c6fdbd8SKent Overstreet 			end_page_writeback(bvec->bv_page);
12887f5e31e1SKent Overstreet 	}
12891c6fdbd8SKent Overstreet 
12909f311f21SKent Overstreet 	bio_put(&io->op.wbio.bio);
12911c6fdbd8SKent Overstreet }
12921c6fdbd8SKent Overstreet 
12931c6fdbd8SKent Overstreet static void bch2_writepage_do_io(struct bch_writepage_state *w)
12941c6fdbd8SKent Overstreet {
12951c6fdbd8SKent Overstreet 	struct bch_writepage_io *io = w->io;
12961c6fdbd8SKent Overstreet 
12971c6fdbd8SKent Overstreet 	w->io = NULL;
12989f311f21SKent Overstreet 	closure_call(&io->op.cl, bch2_write, NULL, NULL);
12991c6fdbd8SKent Overstreet }
13001c6fdbd8SKent Overstreet 
13011c6fdbd8SKent Overstreet /*
13021c6fdbd8SKent Overstreet  * Get a bch_writepage_io and add @page to it - appending to an existing one if
13031c6fdbd8SKent Overstreet  * possible, else allocating a new one:
13041c6fdbd8SKent Overstreet  */
13051c6fdbd8SKent Overstreet static void bch2_writepage_io_alloc(struct bch_fs *c,
130650fe5bd6SKent Overstreet 				    struct writeback_control *wbc,
13071c6fdbd8SKent Overstreet 				    struct bch_writepage_state *w,
13081c6fdbd8SKent Overstreet 				    struct bch_inode_info *inode,
13097f5e31e1SKent Overstreet 				    u64 sector,
13101c6fdbd8SKent Overstreet 				    unsigned nr_replicas)
13111c6fdbd8SKent Overstreet {
13121c6fdbd8SKent Overstreet 	struct bch_write_op *op;
13131c6fdbd8SKent Overstreet 
13141c6fdbd8SKent Overstreet 	w->io = container_of(bio_alloc_bioset(NULL, BIO_MAX_VECS,
13151c6fdbd8SKent Overstreet 					      REQ_OP_WRITE,
13161c6fdbd8SKent Overstreet 					      GFP_NOFS,
13171c6fdbd8SKent Overstreet 					      &c->writepage_bioset),
13189a3df993SKent Overstreet 			     struct bch_writepage_io, op.wbio.bio);
13191c6fdbd8SKent Overstreet 
13209a3df993SKent Overstreet 	w->io->inode		= inode;
13219a3df993SKent Overstreet 	op			= &w->io->op;
13229a3df993SKent Overstreet 	bch2_write_op_init(op, c, w->opts);
13239a3df993SKent Overstreet 	op->target		= w->opts.foreground_target;
13241c6fdbd8SKent Overstreet 	op->nr_replicas		= nr_replicas;
13251c6fdbd8SKent Overstreet 	op->res.nr_replicas	= nr_replicas;
13261c6fdbd8SKent Overstreet 	op->write_point		= writepoint_hashed(inode->ei_last_dirtied);
13278c6d298aSKent Overstreet 	op->subvol		= inode->ei_subvol;
13287f5e31e1SKent Overstreet 	op->pos			= POS(inode->v.i_ino, sector);
13299f311f21SKent Overstreet 	op->end_io		= bch2_writepage_io_done;
13307f5e31e1SKent Overstreet 	op->wbio.bio.bi_iter.bi_sector = sector;
133150fe5bd6SKent Overstreet 	op->wbio.bio.bi_opf	= wbc_to_write_flags(wbc);
13321c6fdbd8SKent Overstreet }
13331c6fdbd8SKent Overstreet 
13341c6fdbd8SKent Overstreet static int __bch2_writepage(struct folio *folio,
13351c6fdbd8SKent Overstreet 			    struct writeback_control *wbc,
13361c6fdbd8SKent Overstreet 			    void *data)
13371c6fdbd8SKent Overstreet {
13381c6fdbd8SKent Overstreet 	struct page *page = &folio->page;
13391c6fdbd8SKent Overstreet 	struct bch_inode_info *inode = to_bch_ei(page->mapping->host);
13401c6fdbd8SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
13411c6fdbd8SKent Overstreet 	struct bch_writepage_state *w = data;
13427f5e31e1SKent Overstreet 	struct bch_page_state *s, orig;
13437f5e31e1SKent Overstreet 	unsigned i, offset, nr_replicas_this_write = U32_MAX;
13441c6fdbd8SKent Overstreet 	loff_t i_size = i_size_read(&inode->v);
13451c6fdbd8SKent Overstreet 	pgoff_t end_index = i_size >> PAGE_SHIFT;
1346e1036a2aSKent Overstreet 	int ret;
13471c6fdbd8SKent Overstreet 
13481c6fdbd8SKent Overstreet 	EBUG_ON(!PageUptodate(page));
13491c6fdbd8SKent Overstreet 
13501c6fdbd8SKent Overstreet 	/* Is the page fully inside i_size? */
13511c6fdbd8SKent Overstreet 	if (page->index < end_index)
13521c6fdbd8SKent Overstreet 		goto do_io;
13531c6fdbd8SKent Overstreet 
13541c6fdbd8SKent Overstreet 	/* Is the page fully outside i_size? (truncate in progress) */
13551c6fdbd8SKent Overstreet 	offset = i_size & (PAGE_SIZE - 1);
13561c6fdbd8SKent Overstreet 	if (page->index > end_index || !offset) {
13571c6fdbd8SKent Overstreet 		unlock_page(page);
13581c6fdbd8SKent Overstreet 		return 0;
13591c6fdbd8SKent Overstreet 	}
13601c6fdbd8SKent Overstreet 
13611c6fdbd8SKent Overstreet 	/*
13621c6fdbd8SKent Overstreet 	 * The page straddles i_size.  It must be zeroed out on each and every
13631c6fdbd8SKent Overstreet 	 * writepage invocation because it may be mmapped.  "A file is mapped
13641c6fdbd8SKent Overstreet 	 * in multiples of the page size.  For a file that is not a multiple of
13651c6fdbd8SKent Overstreet 	 * the  page size, the remaining memory is zeroed when mapped, and
13661c6fdbd8SKent Overstreet 	 * writes to that region are not written out to the file."
13671c6fdbd8SKent Overstreet 	 */
13681c6fdbd8SKent Overstreet 	zero_user_segment(page, offset, PAGE_SIZE);
13691c6fdbd8SKent Overstreet do_io:
1370f57a6a5dSKent Overstreet 	s = bch2_page_state_create(page, __GFP_NOFAIL);
1371f81b648dSKent Overstreet 
1372f74a5051SKent Overstreet 	/*
1373f74a5051SKent Overstreet 	 * Things get really hairy with errors during writeback:
1374f74a5051SKent Overstreet 	 */
1375f74a5051SKent Overstreet 	ret = bch2_get_page_disk_reservation(c, inode, page, false);
1376f74a5051SKent Overstreet 	BUG_ON(ret);
1377e1036a2aSKent Overstreet 
13787f5e31e1SKent Overstreet 	/* Before unlocking the page, get copy of reservations: */
1379f74a5051SKent Overstreet 	spin_lock(&s->lock);
13807f5e31e1SKent Overstreet 	orig = *s;
1381f74a5051SKent Overstreet 	spin_unlock(&s->lock);
13827f5e31e1SKent Overstreet 
13837f5e31e1SKent Overstreet 	for (i = 0; i < PAGE_SECTORS; i++) {
13842ba5d38bSKent Overstreet 		if (s->s[i].state < SECTOR_DIRTY)
13857f5e31e1SKent Overstreet 			continue;
13867f5e31e1SKent Overstreet 
1387f81b648dSKent Overstreet 		nr_replicas_this_write =
1388f57a6a5dSKent Overstreet 			min_t(unsigned, nr_replicas_this_write,
1389f57a6a5dSKent Overstreet 			      s->s[i].nr_replicas +
1390f57a6a5dSKent Overstreet 			      s->s[i].replicas_reserved);
13917f5e31e1SKent Overstreet 	}
13921c6fdbd8SKent Overstreet 
1393f57a6a5dSKent Overstreet 	for (i = 0; i < PAGE_SECTORS; i++) {
13942ba5d38bSKent Overstreet 		if (s->s[i].state < SECTOR_DIRTY)
13957f5e31e1SKent Overstreet 			continue;
13967f5e31e1SKent Overstreet 
1397f57a6a5dSKent Overstreet 		s->s[i].nr_replicas = w->opts.compression
1398f57a6a5dSKent Overstreet 			? 0 : nr_replicas_this_write;
1399e1036a2aSKent Overstreet 
1400f57a6a5dSKent Overstreet 		s->s[i].replicas_reserved = 0;
1401f57a6a5dSKent Overstreet 		s->s[i].state = SECTOR_ALLOCATED;
1402f57a6a5dSKent Overstreet 	}
14031c6fdbd8SKent Overstreet 
14047f5e31e1SKent Overstreet 	BUG_ON(atomic_read(&s->write_count));
14057f5e31e1SKent Overstreet 	atomic_set(&s->write_count, 1);
14067f5e31e1SKent Overstreet 
14071c6fdbd8SKent Overstreet 	BUG_ON(PageWriteback(page));
14081c6fdbd8SKent Overstreet 	set_page_writeback(page);
14097f5e31e1SKent Overstreet 
14101c6fdbd8SKent Overstreet 	unlock_page(page);
14111c6fdbd8SKent Overstreet 
14127f5e31e1SKent Overstreet 	offset = 0;
14137f5e31e1SKent Overstreet 	while (1) {
1414f74a5051SKent Overstreet 		unsigned sectors = 0, dirty_sectors = 0, reserved_sectors = 0;
14157f5e31e1SKent Overstreet 		u64 sector;
14167f5e31e1SKent Overstreet 
14177f5e31e1SKent Overstreet 		while (offset < PAGE_SECTORS &&
14182ba5d38bSKent Overstreet 		       orig.s[offset].state < SECTOR_DIRTY)
14197f5e31e1SKent Overstreet 			offset++;
14207f5e31e1SKent Overstreet 
14217f5e31e1SKent Overstreet 		if (offset == PAGE_SECTORS)
14227f5e31e1SKent Overstreet 			break;
14237f5e31e1SKent Overstreet 
14247f5e31e1SKent Overstreet 		while (offset + sectors < PAGE_SECTORS &&
1425f74a5051SKent Overstreet 		       orig.s[offset + sectors].state >= SECTOR_DIRTY) {
1426f74a5051SKent Overstreet 			reserved_sectors += orig.s[offset + sectors].replicas_reserved;
1427f74a5051SKent Overstreet 			dirty_sectors += orig.s[offset + sectors].state == SECTOR_DIRTY;
14287f5e31e1SKent Overstreet 			sectors++;
14297f5e31e1SKent Overstreet 		}
1430f74a5051SKent Overstreet 		BUG_ON(!sectors);
1431f74a5051SKent Overstreet 
14327279c1a2SKent Overstreet 		sector = ((u64) page->index << PAGE_SECTORS_SHIFT) + offset;
14337f5e31e1SKent Overstreet 
14341c6fdbd8SKent Overstreet 		if (w->io &&
14359a3df993SKent Overstreet 		    (w->io->op.res.nr_replicas != nr_replicas_this_write ||
14369a3df993SKent Overstreet 		     bio_full(&w->io->op.wbio.bio, PAGE_SIZE) ||
1437f59b3464SKent Overstreet 		     w->io->op.wbio.bio.bi_iter.bi_size + (sectors << 9) >=
1438f59b3464SKent Overstreet 		     (BIO_MAX_VECS * PAGE_SIZE) ||
14399a3df993SKent Overstreet 		     bio_end_sector(&w->io->op.wbio.bio) != sector))
14401c6fdbd8SKent Overstreet 			bch2_writepage_do_io(w);
14411c6fdbd8SKent Overstreet 
14421c6fdbd8SKent Overstreet 		if (!w->io)
144350fe5bd6SKent Overstreet 			bch2_writepage_io_alloc(c, wbc, w, inode, sector,
1444f81b648dSKent Overstreet 						nr_replicas_this_write);
14451c6fdbd8SKent Overstreet 
14467f5e31e1SKent Overstreet 		atomic_inc(&s->write_count);
14477f5e31e1SKent Overstreet 
14489a3df993SKent Overstreet 		BUG_ON(inode != w->io->inode);
14499a3df993SKent Overstreet 		BUG_ON(!bio_add_page(&w->io->op.wbio.bio, page,
14507f5e31e1SKent Overstreet 				     sectors << 9, offset << 9));
14511c6fdbd8SKent Overstreet 
14526cc3535dSKent Overstreet 		/* Check for writing past i_size: */
14538eb71e9eSKent Overstreet 		WARN_ONCE((bio_end_sector(&w->io->op.wbio.bio) << 9) >
145480fe580cSKent Overstreet 			  round_up(i_size, block_bytes(c)) &&
14558eb71e9eSKent Overstreet 			  !test_bit(BCH_FS_EMERGENCY_RO, &c->flags),
14568eb71e9eSKent Overstreet 			  "writing past i_size: %llu > %llu (unrounded %llu)\n",
14578eb71e9eSKent Overstreet 			  bio_end_sector(&w->io->op.wbio.bio) << 9,
14588eb71e9eSKent Overstreet 			  round_up(i_size, block_bytes(c)),
14598eb71e9eSKent Overstreet 			  i_size);
14606cc3535dSKent Overstreet 
14619a3df993SKent Overstreet 		w->io->op.res.sectors += reserved_sectors;
14629a3df993SKent Overstreet 		w->io->op.i_sectors_delta -= dirty_sectors;
14631c6fdbd8SKent Overstreet 		w->io->op.new_i_size = i_size;
14641c6fdbd8SKent Overstreet 
14657f5e31e1SKent Overstreet 		offset += sectors;
14667f5e31e1SKent Overstreet 	}
14677f5e31e1SKent Overstreet 
14687f5e31e1SKent Overstreet 	if (atomic_dec_and_test(&s->write_count))
14697f5e31e1SKent Overstreet 		end_page_writeback(page);
14707f5e31e1SKent Overstreet 
14711c6fdbd8SKent Overstreet 	return 0;
14721c6fdbd8SKent Overstreet }
14731c6fdbd8SKent Overstreet 
14741c6fdbd8SKent Overstreet int bch2_writepages(struct address_space *mapping, struct writeback_control *wbc)
14751c6fdbd8SKent Overstreet {
14761c6fdbd8SKent Overstreet 	struct bch_fs *c = mapping->host->i_sb->s_fs_info;
14771c6fdbd8SKent Overstreet 	struct bch_writepage_state w =
14781c6fdbd8SKent Overstreet 		bch_writepage_state_init(c, to_bch_ei(mapping->host));
14791c6fdbd8SKent Overstreet 	struct blk_plug plug;
14801c6fdbd8SKent Overstreet 	int ret;
14811c6fdbd8SKent Overstreet 
14821c6fdbd8SKent Overstreet 	blk_start_plug(&plug);
14831c6fdbd8SKent Overstreet 	ret = write_cache_pages(mapping, wbc, __bch2_writepage, &w);
14841c6fdbd8SKent Overstreet 	if (w.io)
14851c6fdbd8SKent Overstreet 		bch2_writepage_do_io(&w);
14861c6fdbd8SKent Overstreet 	blk_finish_plug(&plug);
14875c1ef830SKent Overstreet 	return bch2_err_class(ret);
14881c6fdbd8SKent Overstreet }
14891c6fdbd8SKent Overstreet 
14901c6fdbd8SKent Overstreet /* buffered writes: */
14911c6fdbd8SKent Overstreet 
14921c6fdbd8SKent Overstreet int bch2_write_begin(struct file *file, struct address_space *mapping,
14931c6fdbd8SKent Overstreet 		     loff_t pos, unsigned len,
14941c6fdbd8SKent Overstreet 		     struct page **pagep, void **fsdata)
14951c6fdbd8SKent Overstreet {
14961c6fdbd8SKent Overstreet 	struct bch_inode_info *inode = to_bch_ei(mapping->host);
14971c6fdbd8SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
1498d1542e03SKent Overstreet 	struct bch2_page_reservation *res;
14991c6fdbd8SKent Overstreet 	pgoff_t index = pos >> PAGE_SHIFT;
15001c6fdbd8SKent Overstreet 	unsigned offset = pos & (PAGE_SIZE - 1);
15011c6fdbd8SKent Overstreet 	struct page *page;
15021c6fdbd8SKent Overstreet 	int ret = -ENOMEM;
15031c6fdbd8SKent Overstreet 
1504d1542e03SKent Overstreet 	res = kmalloc(sizeof(*res), GFP_KERNEL);
1505d1542e03SKent Overstreet 	if (!res)
1506d1542e03SKent Overstreet 		return -ENOMEM;
1507d1542e03SKent Overstreet 
1508d1542e03SKent Overstreet 	bch2_page_reservation_init(c, inode, res);
1509d1542e03SKent Overstreet 	*fsdata = res;
15101c6fdbd8SKent Overstreet 
1511a7ecd30cSKent Overstreet 	bch2_pagecache_add_get(inode);
15121c6fdbd8SKent Overstreet 
15131c6fdbd8SKent Overstreet 	page = grab_cache_page_write_begin(mapping, index);
15141c6fdbd8SKent Overstreet 	if (!page)
15151c6fdbd8SKent Overstreet 		goto err_unlock;
15161c6fdbd8SKent Overstreet 
15171c6fdbd8SKent Overstreet 	if (PageUptodate(page))
15181c6fdbd8SKent Overstreet 		goto out;
15191c6fdbd8SKent Overstreet 
15201c6fdbd8SKent Overstreet 	/* If we're writing entire page, don't need to read it in first: */
15211c6fdbd8SKent Overstreet 	if (len == PAGE_SIZE)
15221c6fdbd8SKent Overstreet 		goto out;
15231c6fdbd8SKent Overstreet 
15241c6fdbd8SKent Overstreet 	if (!offset && pos + len >= inode->v.i_size) {
15251c6fdbd8SKent Overstreet 		zero_user_segment(page, len, PAGE_SIZE);
15261c6fdbd8SKent Overstreet 		flush_dcache_page(page);
15271c6fdbd8SKent Overstreet 		goto out;
15281c6fdbd8SKent Overstreet 	}
15291c6fdbd8SKent Overstreet 
15301c6fdbd8SKent Overstreet 	if (index > inode->v.i_size >> PAGE_SHIFT) {
15311c6fdbd8SKent Overstreet 		zero_user_segments(page, 0, offset, offset + len, PAGE_SIZE);
15321c6fdbd8SKent Overstreet 		flush_dcache_page(page);
15331c6fdbd8SKent Overstreet 		goto out;
15341c6fdbd8SKent Overstreet 	}
15351c6fdbd8SKent Overstreet readpage:
15361c6fdbd8SKent Overstreet 	ret = bch2_read_single_page(page, mapping);
15371c6fdbd8SKent Overstreet 	if (ret)
15381c6fdbd8SKent Overstreet 		goto err;
15391c6fdbd8SKent Overstreet out:
1540e6ec361fSKent Overstreet 	if (!bch2_page_state_create(page, __GFP_NOFAIL)->uptodate) {
1541e6ec361fSKent Overstreet 		ret = bch2_page_state_set(c, inode_inum(inode), &page, 1);
1542e6ec361fSKent Overstreet 		if (ret)
15433a4d3656SKent Overstreet 			goto err;
1544e6ec361fSKent Overstreet 	}
1545e6ec361fSKent Overstreet 
1546bd954215SKent Overstreet 	ret = bch2_page_reservation_get(c, inode, page, res, offset, len);
15471c6fdbd8SKent Overstreet 	if (ret) {
15481c6fdbd8SKent Overstreet 		if (!PageUptodate(page)) {
15491c6fdbd8SKent Overstreet 			/*
15501c6fdbd8SKent Overstreet 			 * If the page hasn't been read in, we won't know if we
15511c6fdbd8SKent Overstreet 			 * actually need a reservation - we don't actually need
15521c6fdbd8SKent Overstreet 			 * to read here, we just need to check if the page is
15531c6fdbd8SKent Overstreet 			 * fully backed by uncompressed data:
15541c6fdbd8SKent Overstreet 			 */
15551c6fdbd8SKent Overstreet 			goto readpage;
15561c6fdbd8SKent Overstreet 		}
15571c6fdbd8SKent Overstreet 
15581c6fdbd8SKent Overstreet 		goto err;
15591c6fdbd8SKent Overstreet 	}
15601c6fdbd8SKent Overstreet 
15611c6fdbd8SKent Overstreet 	*pagep = page;
15621c6fdbd8SKent Overstreet 	return 0;
15631c6fdbd8SKent Overstreet err:
15641c6fdbd8SKent Overstreet 	unlock_page(page);
15651c6fdbd8SKent Overstreet 	put_page(page);
15661c6fdbd8SKent Overstreet 	*pagep = NULL;
15671c6fdbd8SKent Overstreet err_unlock:
1568a7ecd30cSKent Overstreet 	bch2_pagecache_add_put(inode);
1569d1542e03SKent Overstreet 	kfree(res);
1570d1542e03SKent Overstreet 	*fsdata = NULL;
15715c1ef830SKent Overstreet 	return bch2_err_class(ret);
15721c6fdbd8SKent Overstreet }
15731c6fdbd8SKent Overstreet 
15741c6fdbd8SKent Overstreet int bch2_write_end(struct file *file, struct address_space *mapping,
15751c6fdbd8SKent Overstreet 		   loff_t pos, unsigned len, unsigned copied,
15761c6fdbd8SKent Overstreet 		   struct page *page, void *fsdata)
15771c6fdbd8SKent Overstreet {
15781c6fdbd8SKent Overstreet 	struct bch_inode_info *inode = to_bch_ei(mapping->host);
15791c6fdbd8SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
1580d1542e03SKent Overstreet 	struct bch2_page_reservation *res = fsdata;
1581d1542e03SKent Overstreet 	unsigned offset = pos & (PAGE_SIZE - 1);
15821c6fdbd8SKent Overstreet 
15831c6fdbd8SKent Overstreet 	lockdep_assert_held(&inode->v.i_rwsem);
15841c6fdbd8SKent Overstreet 
15851c6fdbd8SKent Overstreet 	if (unlikely(copied < len && !PageUptodate(page))) {
15861c6fdbd8SKent Overstreet 		/*
15871c6fdbd8SKent Overstreet 		 * The page needs to be read in, but that would destroy
15881c6fdbd8SKent Overstreet 		 * our partial write - simplest thing is to just force
15891c6fdbd8SKent Overstreet 		 * userspace to redo the write:
15901c6fdbd8SKent Overstreet 		 */
15911c6fdbd8SKent Overstreet 		zero_user(page, 0, PAGE_SIZE);
15921c6fdbd8SKent Overstreet 		flush_dcache_page(page);
15931c6fdbd8SKent Overstreet 		copied = 0;
15941c6fdbd8SKent Overstreet 	}
15951c6fdbd8SKent Overstreet 
15961c6fdbd8SKent Overstreet 	spin_lock(&inode->v.i_lock);
15971c6fdbd8SKent Overstreet 	if (pos + copied > inode->v.i_size)
15981c6fdbd8SKent Overstreet 		i_size_write(&inode->v, pos + copied);
15991c6fdbd8SKent Overstreet 	spin_unlock(&inode->v.i_lock);
16001c6fdbd8SKent Overstreet 
16011c6fdbd8SKent Overstreet 	if (copied) {
16021c6fdbd8SKent Overstreet 		if (!PageUptodate(page))
16031c6fdbd8SKent Overstreet 			SetPageUptodate(page);
1604d1542e03SKent Overstreet 
1605d1542e03SKent Overstreet 		bch2_set_page_dirty(c, inode, page, res, offset, copied);
16061c6fdbd8SKent Overstreet 
16071c6fdbd8SKent Overstreet 		inode->ei_last_dirtied = (unsigned long) current;
16081c6fdbd8SKent Overstreet 	}
16091c6fdbd8SKent Overstreet 
16101c6fdbd8SKent Overstreet 	unlock_page(page);
16111c6fdbd8SKent Overstreet 	put_page(page);
1612a7ecd30cSKent Overstreet 	bch2_pagecache_add_put(inode);
16131c6fdbd8SKent Overstreet 
1614d1542e03SKent Overstreet 	bch2_page_reservation_put(c, inode, res);
1615d1542e03SKent Overstreet 	kfree(res);
1616d1542e03SKent Overstreet 
16171c6fdbd8SKent Overstreet 	return copied;
16181c6fdbd8SKent Overstreet }
16191c6fdbd8SKent Overstreet 
16201c6fdbd8SKent Overstreet #define WRITE_BATCH_PAGES	32
16211c6fdbd8SKent Overstreet 
16221c6fdbd8SKent Overstreet static int __bch2_buffered_write(struct bch_inode_info *inode,
16231c6fdbd8SKent Overstreet 				 struct address_space *mapping,
16241c6fdbd8SKent Overstreet 				 struct iov_iter *iter,
16251c6fdbd8SKent Overstreet 				 loff_t pos, unsigned len)
16261c6fdbd8SKent Overstreet {
16271c6fdbd8SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
16281c6fdbd8SKent Overstreet 	struct page *pages[WRITE_BATCH_PAGES];
1629d1542e03SKent Overstreet 	struct bch2_page_reservation res;
16301c6fdbd8SKent Overstreet 	unsigned long index = pos >> PAGE_SHIFT;
16311c6fdbd8SKent Overstreet 	unsigned offset = pos & (PAGE_SIZE - 1);
16321c6fdbd8SKent Overstreet 	unsigned nr_pages = DIV_ROUND_UP(offset + len, PAGE_SIZE);
1633d1542e03SKent Overstreet 	unsigned i, reserved = 0, set_dirty = 0;
1634d1542e03SKent Overstreet 	unsigned copied = 0, nr_pages_copied = 0;
16351c6fdbd8SKent Overstreet 	int ret = 0;
16361c6fdbd8SKent Overstreet 
16371c6fdbd8SKent Overstreet 	BUG_ON(!len);
16381c6fdbd8SKent Overstreet 	BUG_ON(nr_pages > ARRAY_SIZE(pages));
16391c6fdbd8SKent Overstreet 
1640d1542e03SKent Overstreet 	bch2_page_reservation_init(c, inode, &res);
1641d1542e03SKent Overstreet 
16421c6fdbd8SKent Overstreet 	for (i = 0; i < nr_pages; i++) {
16431c6fdbd8SKent Overstreet 		pages[i] = grab_cache_page_write_begin(mapping, index + i);
16441c6fdbd8SKent Overstreet 		if (!pages[i]) {
16451c6fdbd8SKent Overstreet 			nr_pages = i;
16468de819f8SKent Overstreet 			if (!i) {
16471c6fdbd8SKent Overstreet 				ret = -ENOMEM;
16481c6fdbd8SKent Overstreet 				goto out;
16491c6fdbd8SKent Overstreet 			}
16508de819f8SKent Overstreet 			len = min_t(unsigned, len,
16518de819f8SKent Overstreet 				    nr_pages * PAGE_SIZE - offset);
16528de819f8SKent Overstreet 			break;
16538de819f8SKent Overstreet 		}
16541c6fdbd8SKent Overstreet 	}
16551c6fdbd8SKent Overstreet 
16561c6fdbd8SKent Overstreet 	if (offset && !PageUptodate(pages[0])) {
16571c6fdbd8SKent Overstreet 		ret = bch2_read_single_page(pages[0], mapping);
16581c6fdbd8SKent Overstreet 		if (ret)
16591c6fdbd8SKent Overstreet 			goto out;
16601c6fdbd8SKent Overstreet 	}
16611c6fdbd8SKent Overstreet 
16621c6fdbd8SKent Overstreet 	if ((pos + len) & (PAGE_SIZE - 1) &&
16631c6fdbd8SKent Overstreet 	    !PageUptodate(pages[nr_pages - 1])) {
16641c6fdbd8SKent Overstreet 		if ((index + nr_pages - 1) << PAGE_SHIFT >= inode->v.i_size) {
16651c6fdbd8SKent Overstreet 			zero_user(pages[nr_pages - 1], 0, PAGE_SIZE);
16661c6fdbd8SKent Overstreet 		} else {
16671c6fdbd8SKent Overstreet 			ret = bch2_read_single_page(pages[nr_pages - 1], mapping);
16681c6fdbd8SKent Overstreet 			if (ret)
16691c6fdbd8SKent Overstreet 				goto out;
16701c6fdbd8SKent Overstreet 		}
16711c6fdbd8SKent Overstreet 	}
16721c6fdbd8SKent Overstreet 
1673d1542e03SKent Overstreet 	while (reserved < len) {
1674e6ec361fSKent Overstreet 		unsigned i = (offset + reserved) >> PAGE_SHIFT;
1675e6ec361fSKent Overstreet 		struct page *page = pages[i];
1676d1542e03SKent Overstreet 		unsigned pg_offset = (offset + reserved) & (PAGE_SIZE - 1);
1677d1542e03SKent Overstreet 		unsigned pg_len = min_t(unsigned, len - reserved,
1678d1542e03SKent Overstreet 					PAGE_SIZE - pg_offset);
16791c6fdbd8SKent Overstreet 
1680e6ec361fSKent Overstreet 		if (!bch2_page_state_create(page, __GFP_NOFAIL)->uptodate) {
1681e6ec361fSKent Overstreet 			ret = bch2_page_state_set(c, inode_inum(inode),
1682e6ec361fSKent Overstreet 						  pages + i, nr_pages - i);
1683e6ec361fSKent Overstreet 			if (ret)
1684e6ec361fSKent Overstreet 				goto out;
16851c6fdbd8SKent Overstreet 		}
16861c6fdbd8SKent Overstreet 
1687353448f3SKent Overstreet 		/*
1688353448f3SKent Overstreet 		 * XXX: per POSIX and fstests generic/275, on -ENOSPC we're
1689353448f3SKent Overstreet 		 * supposed to write as much as we have disk space for.
1690353448f3SKent Overstreet 		 *
1691353448f3SKent Overstreet 		 * On failure here we should still write out a partial page if
1692353448f3SKent Overstreet 		 * we aren't completely out of disk space - we don't do that
1693353448f3SKent Overstreet 		 * yet:
1694353448f3SKent Overstreet 		 */
1695e6ec361fSKent Overstreet 		ret = bch2_page_reservation_get(c, inode, page, &res,
1696bd954215SKent Overstreet 						pg_offset, pg_len);
1697353448f3SKent Overstreet 		if (unlikely(ret)) {
1698353448f3SKent Overstreet 			if (!reserved)
16991c6fdbd8SKent Overstreet 				goto out;
1700353448f3SKent Overstreet 			break;
1701353448f3SKent Overstreet 		}
1702d1542e03SKent Overstreet 
1703d1542e03SKent Overstreet 		reserved += pg_len;
17041c6fdbd8SKent Overstreet 	}
17051c6fdbd8SKent Overstreet 
17061c6fdbd8SKent Overstreet 	if (mapping_writably_mapped(mapping))
17071c6fdbd8SKent Overstreet 		for (i = 0; i < nr_pages; i++)
17081c6fdbd8SKent Overstreet 			flush_dcache_page(pages[i]);
17091c6fdbd8SKent Overstreet 
1710353448f3SKent Overstreet 	while (copied < reserved) {
17111c6fdbd8SKent Overstreet 		struct page *page = pages[(offset + copied) >> PAGE_SHIFT];
17121c6fdbd8SKent Overstreet 		unsigned pg_offset = (offset + copied) & (PAGE_SIZE - 1);
1713353448f3SKent Overstreet 		unsigned pg_len = min_t(unsigned, reserved - copied,
17141c6fdbd8SKent Overstreet 					PAGE_SIZE - pg_offset);
17151c6fdbd8SKent Overstreet 		unsigned pg_copied = copy_page_from_iter_atomic(page,
1716d1542e03SKent Overstreet 						pg_offset, pg_len, iter);
1717d1542e03SKent Overstreet 
1718d1542e03SKent Overstreet 		if (!pg_copied)
1719d1542e03SKent Overstreet 			break;
17201c6fdbd8SKent Overstreet 
1721912bdf17SKent Overstreet 		if (!PageUptodate(page) &&
1722912bdf17SKent Overstreet 		    pg_copied != PAGE_SIZE &&
1723912bdf17SKent Overstreet 		    pos + copied + pg_copied < inode->v.i_size) {
1724912bdf17SKent Overstreet 			zero_user(page, 0, PAGE_SIZE);
1725912bdf17SKent Overstreet 			break;
1726912bdf17SKent Overstreet 		}
1727912bdf17SKent Overstreet 
17281c6fdbd8SKent Overstreet 		flush_dcache_page(page);
17291c6fdbd8SKent Overstreet 		copied += pg_copied;
1730912bdf17SKent Overstreet 
1731912bdf17SKent Overstreet 		if (pg_copied != pg_len)
1732912bdf17SKent Overstreet 			break;
17331c6fdbd8SKent Overstreet 	}
17341c6fdbd8SKent Overstreet 
17351c6fdbd8SKent Overstreet 	if (!copied)
17361c6fdbd8SKent Overstreet 		goto out;
17371c6fdbd8SKent Overstreet 
1738877dfb34SKent Overstreet 	spin_lock(&inode->v.i_lock);
1739877dfb34SKent Overstreet 	if (pos + copied > inode->v.i_size)
1740877dfb34SKent Overstreet 		i_size_write(&inode->v, pos + copied);
1741877dfb34SKent Overstreet 	spin_unlock(&inode->v.i_lock);
1742877dfb34SKent Overstreet 
1743d1542e03SKent Overstreet 	while (set_dirty < copied) {
1744d1542e03SKent Overstreet 		struct page *page = pages[(offset + set_dirty) >> PAGE_SHIFT];
1745d1542e03SKent Overstreet 		unsigned pg_offset = (offset + set_dirty) & (PAGE_SIZE - 1);
1746d1542e03SKent Overstreet 		unsigned pg_len = min_t(unsigned, copied - set_dirty,
1747d1542e03SKent Overstreet 					PAGE_SIZE - pg_offset);
1748d1542e03SKent Overstreet 
1749d1542e03SKent Overstreet 		if (!PageUptodate(page))
1750d1542e03SKent Overstreet 			SetPageUptodate(page);
1751d1542e03SKent Overstreet 
1752d1542e03SKent Overstreet 		bch2_set_page_dirty(c, inode, page, &res, pg_offset, pg_len);
1753d1542e03SKent Overstreet 		unlock_page(page);
1754d1542e03SKent Overstreet 		put_page(page);
1755d1542e03SKent Overstreet 
1756d1542e03SKent Overstreet 		set_dirty += pg_len;
1757d1542e03SKent Overstreet 	}
1758877dfb34SKent Overstreet 
1759877dfb34SKent Overstreet 	nr_pages_copied = DIV_ROUND_UP(offset + copied, PAGE_SIZE);
1760877dfb34SKent Overstreet 	inode->ei_last_dirtied = (unsigned long) current;
17611c6fdbd8SKent Overstreet out:
1762d1542e03SKent Overstreet 	for (i = nr_pages_copied; i < nr_pages; i++) {
17631c6fdbd8SKent Overstreet 		unlock_page(pages[i]);
17641c6fdbd8SKent Overstreet 		put_page(pages[i]);
17651c6fdbd8SKent Overstreet 	}
17661c6fdbd8SKent Overstreet 
1767d1542e03SKent Overstreet 	bch2_page_reservation_put(c, inode, &res);
17681c6fdbd8SKent Overstreet 
17691c6fdbd8SKent Overstreet 	return copied ?: ret;
17701c6fdbd8SKent Overstreet }
17711c6fdbd8SKent Overstreet 
17721c6fdbd8SKent Overstreet static ssize_t bch2_buffered_write(struct kiocb *iocb, struct iov_iter *iter)
17731c6fdbd8SKent Overstreet {
17741c6fdbd8SKent Overstreet 	struct file *file = iocb->ki_filp;
17751c6fdbd8SKent Overstreet 	struct address_space *mapping = file->f_mapping;
17761c6fdbd8SKent Overstreet 	struct bch_inode_info *inode = file_bch_inode(file);
17771c6fdbd8SKent Overstreet 	loff_t pos = iocb->ki_pos;
17781c6fdbd8SKent Overstreet 	ssize_t written = 0;
17791c6fdbd8SKent Overstreet 	int ret = 0;
17801c6fdbd8SKent Overstreet 
1781a7ecd30cSKent Overstreet 	bch2_pagecache_add_get(inode);
17821c6fdbd8SKent Overstreet 
17831c6fdbd8SKent Overstreet 	do {
17841c6fdbd8SKent Overstreet 		unsigned offset = pos & (PAGE_SIZE - 1);
17851c6fdbd8SKent Overstreet 		unsigned bytes = min_t(unsigned long, iov_iter_count(iter),
17861c6fdbd8SKent Overstreet 			      PAGE_SIZE * WRITE_BATCH_PAGES - offset);
17871c6fdbd8SKent Overstreet again:
17881c6fdbd8SKent Overstreet 		/*
17891c6fdbd8SKent Overstreet 		 * Bring in the user page that we will copy from _first_.
17901c6fdbd8SKent Overstreet 		 * Otherwise there's a nasty deadlock on copying from the
17911c6fdbd8SKent Overstreet 		 * same page as we're writing to, without it being marked
17921c6fdbd8SKent Overstreet 		 * up-to-date.
17931c6fdbd8SKent Overstreet 		 *
17941c6fdbd8SKent Overstreet 		 * Not only is this an optimisation, but it is also required
17951c6fdbd8SKent Overstreet 		 * to check that the address is actually valid, when atomic
17961c6fdbd8SKent Overstreet 		 * usercopies are used, below.
17971c6fdbd8SKent Overstreet 		 */
17981c6fdbd8SKent Overstreet 		if (unlikely(fault_in_iov_iter_readable(iter, bytes))) {
17991c6fdbd8SKent Overstreet 			bytes = min_t(unsigned long, iov_iter_count(iter),
18001c6fdbd8SKent Overstreet 				      PAGE_SIZE - offset);
18011c6fdbd8SKent Overstreet 
18021c6fdbd8SKent Overstreet 			if (unlikely(fault_in_iov_iter_readable(iter, bytes))) {
18031c6fdbd8SKent Overstreet 				ret = -EFAULT;
18041c6fdbd8SKent Overstreet 				break;
18051c6fdbd8SKent Overstreet 			}
18061c6fdbd8SKent Overstreet 		}
18071c6fdbd8SKent Overstreet 
18081c6fdbd8SKent Overstreet 		if (unlikely(fatal_signal_pending(current))) {
18091c6fdbd8SKent Overstreet 			ret = -EINTR;
18101c6fdbd8SKent Overstreet 			break;
18111c6fdbd8SKent Overstreet 		}
18121c6fdbd8SKent Overstreet 
18131c6fdbd8SKent Overstreet 		ret = __bch2_buffered_write(inode, mapping, iter, pos, bytes);
18141c6fdbd8SKent Overstreet 		if (unlikely(ret < 0))
18151c6fdbd8SKent Overstreet 			break;
18161c6fdbd8SKent Overstreet 
18171c6fdbd8SKent Overstreet 		cond_resched();
18181c6fdbd8SKent Overstreet 
18191c6fdbd8SKent Overstreet 		if (unlikely(ret == 0)) {
18201c6fdbd8SKent Overstreet 			/*
18211c6fdbd8SKent Overstreet 			 * If we were unable to copy any data at all, we must
18221c6fdbd8SKent Overstreet 			 * fall back to a single segment length write.
18231c6fdbd8SKent Overstreet 			 *
18241c6fdbd8SKent Overstreet 			 * If we didn't fallback here, we could livelock
18251c6fdbd8SKent Overstreet 			 * because not all segments in the iov can be copied at
18261c6fdbd8SKent Overstreet 			 * once without a pagefault.
18271c6fdbd8SKent Overstreet 			 */
18281c6fdbd8SKent Overstreet 			bytes = min_t(unsigned long, PAGE_SIZE - offset,
18291c6fdbd8SKent Overstreet 				      iov_iter_single_seg_count(iter));
18301c6fdbd8SKent Overstreet 			goto again;
18311c6fdbd8SKent Overstreet 		}
18321c6fdbd8SKent Overstreet 		pos += ret;
18331c6fdbd8SKent Overstreet 		written += ret;
1834912bdf17SKent Overstreet 		ret = 0;
18351c6fdbd8SKent Overstreet 
18361c6fdbd8SKent Overstreet 		balance_dirty_pages_ratelimited(mapping);
18371c6fdbd8SKent Overstreet 	} while (iov_iter_count(iter));
18381c6fdbd8SKent Overstreet 
1839a7ecd30cSKent Overstreet 	bch2_pagecache_add_put(inode);
18401c6fdbd8SKent Overstreet 
18411c6fdbd8SKent Overstreet 	return written ? written : ret;
18421c6fdbd8SKent Overstreet }
18431c6fdbd8SKent Overstreet 
18441c6fdbd8SKent Overstreet /* O_DIRECT reads */
18451c6fdbd8SKent Overstreet 
1846b4725cc1SKent Overstreet static void bio_check_or_release(struct bio *bio, bool check_dirty)
1847b4725cc1SKent Overstreet {
1848b4725cc1SKent Overstreet 	if (check_dirty) {
1849b4725cc1SKent Overstreet 		bio_check_pages_dirty(bio);
1850b4725cc1SKent Overstreet 	} else {
1851b4725cc1SKent Overstreet 		bio_release_pages(bio, false);
1852b4725cc1SKent Overstreet 		bio_put(bio);
1853b4725cc1SKent Overstreet 	}
1854b4725cc1SKent Overstreet }
1855b4725cc1SKent Overstreet 
18561c6fdbd8SKent Overstreet static void bch2_dio_read_complete(struct closure *cl)
18571c6fdbd8SKent Overstreet {
18581c6fdbd8SKent Overstreet 	struct dio_read *dio = container_of(cl, struct dio_read, cl);
18591c6fdbd8SKent Overstreet 
18601c6fdbd8SKent Overstreet 	dio->req->ki_complete(dio->req, dio->ret);
1861b4725cc1SKent Overstreet 	bio_check_or_release(&dio->rbio.bio, dio->should_dirty);
18621c6fdbd8SKent Overstreet }
18631c6fdbd8SKent Overstreet 
18641c6fdbd8SKent Overstreet static void bch2_direct_IO_read_endio(struct bio *bio)
18651c6fdbd8SKent Overstreet {
18661c6fdbd8SKent Overstreet 	struct dio_read *dio = bio->bi_private;
18671c6fdbd8SKent Overstreet 
18681c6fdbd8SKent Overstreet 	if (bio->bi_status)
18691c6fdbd8SKent Overstreet 		dio->ret = blk_status_to_errno(bio->bi_status);
18701c6fdbd8SKent Overstreet 
18711c6fdbd8SKent Overstreet 	closure_put(&dio->cl);
18721c6fdbd8SKent Overstreet }
18731c6fdbd8SKent Overstreet 
18741c6fdbd8SKent Overstreet static void bch2_direct_IO_read_split_endio(struct bio *bio)
18751c6fdbd8SKent Overstreet {
1876b4725cc1SKent Overstreet 	struct dio_read *dio = bio->bi_private;
1877b4725cc1SKent Overstreet 	bool should_dirty = dio->should_dirty;
1878b4725cc1SKent Overstreet 
18791c6fdbd8SKent Overstreet 	bch2_direct_IO_read_endio(bio);
1880b4725cc1SKent Overstreet 	bio_check_or_release(bio, should_dirty);
18811c6fdbd8SKent Overstreet }
18821c6fdbd8SKent Overstreet 
18831c6fdbd8SKent Overstreet static int bch2_direct_IO_read(struct kiocb *req, struct iov_iter *iter)
18841c6fdbd8SKent Overstreet {
18851c6fdbd8SKent Overstreet 	struct file *file = req->ki_filp;
18861c6fdbd8SKent Overstreet 	struct bch_inode_info *inode = file_bch_inode(file);
18871c6fdbd8SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
188801ad6737SKent Overstreet 	struct bch_io_opts opts;
18891c6fdbd8SKent Overstreet 	struct dio_read *dio;
18901c6fdbd8SKent Overstreet 	struct bio *bio;
18911c6fdbd8SKent Overstreet 	loff_t offset = req->ki_pos;
18921c6fdbd8SKent Overstreet 	bool sync = is_sync_kiocb(req);
18931c6fdbd8SKent Overstreet 	size_t shorten;
18941c6fdbd8SKent Overstreet 	ssize_t ret;
18951c6fdbd8SKent Overstreet 
189601ad6737SKent Overstreet 	bch2_inode_opts_get(&opts, c, &inode->ei_inode);
189701ad6737SKent Overstreet 
18981c6fdbd8SKent Overstreet 	if ((offset|iter->count) & (block_bytes(c) - 1))
18991c6fdbd8SKent Overstreet 		return -EINVAL;
19001c6fdbd8SKent Overstreet 
19011c6fdbd8SKent Overstreet 	ret = min_t(loff_t, iter->count,
19021c6fdbd8SKent Overstreet 		    max_t(loff_t, 0, i_size_read(&inode->v) - offset));
19031c6fdbd8SKent Overstreet 
19041c6fdbd8SKent Overstreet 	if (!ret)
19051c6fdbd8SKent Overstreet 		return ret;
19061c6fdbd8SKent Overstreet 
19071c6fdbd8SKent Overstreet 	shorten = iov_iter_count(iter) - round_up(ret, block_bytes(c));
19081c6fdbd8SKent Overstreet 	iter->count -= shorten;
19091c6fdbd8SKent Overstreet 
19101c6fdbd8SKent Overstreet 	bio = bio_alloc_bioset(NULL,
19114d126dc8SKent Overstreet 			       bio_iov_vecs_to_alloc(iter, BIO_MAX_VECS),
19121c6fdbd8SKent Overstreet 			       REQ_OP_READ,
19131c6fdbd8SKent Overstreet 			       GFP_KERNEL,
19141c6fdbd8SKent Overstreet 			       &c->dio_read_bioset);
19151c6fdbd8SKent Overstreet 
19161c6fdbd8SKent Overstreet 	bio->bi_end_io = bch2_direct_IO_read_endio;
19171c6fdbd8SKent Overstreet 
19181c6fdbd8SKent Overstreet 	dio = container_of(bio, struct dio_read, rbio.bio);
19191c6fdbd8SKent Overstreet 	closure_init(&dio->cl, NULL);
19201c6fdbd8SKent Overstreet 
19211c6fdbd8SKent Overstreet 	/*
19221c6fdbd8SKent Overstreet 	 * this is a _really_ horrible hack just to avoid an atomic sub at the
19231c6fdbd8SKent Overstreet 	 * end:
19241c6fdbd8SKent Overstreet 	 */
19251c6fdbd8SKent Overstreet 	if (!sync) {
19261c6fdbd8SKent Overstreet 		set_closure_fn(&dio->cl, bch2_dio_read_complete, NULL);
19271c6fdbd8SKent Overstreet 		atomic_set(&dio->cl.remaining,
19281c6fdbd8SKent Overstreet 			   CLOSURE_REMAINING_INITIALIZER -
19291c6fdbd8SKent Overstreet 			   CLOSURE_RUNNING +
19301c6fdbd8SKent Overstreet 			   CLOSURE_DESTRUCTOR);
19311c6fdbd8SKent Overstreet 	} else {
19321c6fdbd8SKent Overstreet 		atomic_set(&dio->cl.remaining,
19331c6fdbd8SKent Overstreet 			   CLOSURE_REMAINING_INITIALIZER + 1);
19341c6fdbd8SKent Overstreet 	}
19351c6fdbd8SKent Overstreet 
19361c6fdbd8SKent Overstreet 	dio->req	= req;
19371c6fdbd8SKent Overstreet 	dio->ret	= ret;
1938b4725cc1SKent Overstreet 	/*
1939b4725cc1SKent Overstreet 	 * This is one of the sketchier things I've encountered: we have to skip
1940b4725cc1SKent Overstreet 	 * the dirtying of requests that are internal from the kernel (i.e. from
1941b4725cc1SKent Overstreet 	 * loopback), because we'll deadlock on page_lock.
1942b4725cc1SKent Overstreet 	 */
1943b4725cc1SKent Overstreet 	dio->should_dirty = iter_is_iovec(iter);
19441c6fdbd8SKent Overstreet 
19451c6fdbd8SKent Overstreet 	goto start;
19461c6fdbd8SKent Overstreet 	while (iter->count) {
19471c6fdbd8SKent Overstreet 		bio = bio_alloc_bioset(NULL,
19484d126dc8SKent Overstreet 				       bio_iov_vecs_to_alloc(iter, BIO_MAX_VECS),
19491c6fdbd8SKent Overstreet 				       REQ_OP_READ,
19501c6fdbd8SKent Overstreet 				       GFP_KERNEL,
19511c6fdbd8SKent Overstreet 				       &c->bio_read);
19521c6fdbd8SKent Overstreet 		bio->bi_end_io		= bch2_direct_IO_read_split_endio;
19531c6fdbd8SKent Overstreet start:
19541c6fdbd8SKent Overstreet 		bio->bi_opf		= REQ_OP_READ|REQ_SYNC;
19551c6fdbd8SKent Overstreet 		bio->bi_iter.bi_sector	= offset >> 9;
19561c6fdbd8SKent Overstreet 		bio->bi_private		= dio;
19571c6fdbd8SKent Overstreet 
19581c6fdbd8SKent Overstreet 		ret = bio_iov_iter_get_pages(bio, iter);
19591c6fdbd8SKent Overstreet 		if (ret < 0) {
19601c6fdbd8SKent Overstreet 			/* XXX: fault inject this path */
19611c6fdbd8SKent Overstreet 			bio->bi_status = BLK_STS_RESOURCE;
19621c6fdbd8SKent Overstreet 			bio_endio(bio);
19631c6fdbd8SKent Overstreet 			break;
19641c6fdbd8SKent Overstreet 		}
19651c6fdbd8SKent Overstreet 
19661c6fdbd8SKent Overstreet 		offset += bio->bi_iter.bi_size;
1967b4725cc1SKent Overstreet 
1968b4725cc1SKent Overstreet 		if (dio->should_dirty)
19691c6fdbd8SKent Overstreet 			bio_set_pages_dirty(bio);
19701c6fdbd8SKent Overstreet 
19711c6fdbd8SKent Overstreet 		if (iter->count)
19721c6fdbd8SKent Overstreet 			closure_get(&dio->cl);
19731c6fdbd8SKent Overstreet 
19748c6d298aSKent Overstreet 		bch2_read(c, rbio_init(bio, opts), inode_inum(inode));
19751c6fdbd8SKent Overstreet 	}
19761c6fdbd8SKent Overstreet 
19771c6fdbd8SKent Overstreet 	iter->count += shorten;
19781c6fdbd8SKent Overstreet 
19791c6fdbd8SKent Overstreet 	if (sync) {
19801c6fdbd8SKent Overstreet 		closure_sync(&dio->cl);
19811c6fdbd8SKent Overstreet 		closure_debug_destroy(&dio->cl);
19821c6fdbd8SKent Overstreet 		ret = dio->ret;
1983b4725cc1SKent Overstreet 		bio_check_or_release(&dio->rbio.bio, dio->should_dirty);
19841c6fdbd8SKent Overstreet 		return ret;
19851c6fdbd8SKent Overstreet 	} else {
19861c6fdbd8SKent Overstreet 		return -EIOCBQUEUED;
19871c6fdbd8SKent Overstreet 	}
19881c6fdbd8SKent Overstreet }
19891c6fdbd8SKent Overstreet 
19901c6fdbd8SKent Overstreet ssize_t bch2_read_iter(struct kiocb *iocb, struct iov_iter *iter)
19911c6fdbd8SKent Overstreet {
19921c6fdbd8SKent Overstreet 	struct file *file = iocb->ki_filp;
19931c6fdbd8SKent Overstreet 	struct bch_inode_info *inode = file_bch_inode(file);
19941c6fdbd8SKent Overstreet 	struct address_space *mapping = file->f_mapping;
19951c6fdbd8SKent Overstreet 	size_t count = iov_iter_count(iter);
19961c6fdbd8SKent Overstreet 	ssize_t ret;
19971c6fdbd8SKent Overstreet 
19981c6fdbd8SKent Overstreet 	if (!count)
19991c6fdbd8SKent Overstreet 		return 0; /* skip atime */
20001c6fdbd8SKent Overstreet 
20011c6fdbd8SKent Overstreet 	if (iocb->ki_flags & IOCB_DIRECT) {
20021c6fdbd8SKent Overstreet 		struct blk_plug plug;
20031c6fdbd8SKent Overstreet 
2004a023127aSKent Overstreet 		if (unlikely(mapping->nrpages)) {
20051c6fdbd8SKent Overstreet 			ret = filemap_write_and_wait_range(mapping,
20061c6fdbd8SKent Overstreet 						iocb->ki_pos,
20071c6fdbd8SKent Overstreet 						iocb->ki_pos + count - 1);
20081c6fdbd8SKent Overstreet 			if (ret < 0)
20095c1ef830SKent Overstreet 				goto out;
2010a023127aSKent Overstreet 		}
20111c6fdbd8SKent Overstreet 
20121c6fdbd8SKent Overstreet 		file_accessed(file);
20131c6fdbd8SKent Overstreet 
20141c6fdbd8SKent Overstreet 		blk_start_plug(&plug);
20151c6fdbd8SKent Overstreet 		ret = bch2_direct_IO_read(iocb, iter);
20161c6fdbd8SKent Overstreet 		blk_finish_plug(&plug);
20171c6fdbd8SKent Overstreet 
20181c6fdbd8SKent Overstreet 		if (ret >= 0)
20191c6fdbd8SKent Overstreet 			iocb->ki_pos += ret;
20201c6fdbd8SKent Overstreet 	} else {
2021a7ecd30cSKent Overstreet 		bch2_pagecache_add_get(inode);
20221c6fdbd8SKent Overstreet 		ret = generic_file_read_iter(iocb, iter);
2023a7ecd30cSKent Overstreet 		bch2_pagecache_add_put(inode);
20241c6fdbd8SKent Overstreet 	}
20255c1ef830SKent Overstreet out:
20265c1ef830SKent Overstreet 	return bch2_err_class(ret);
20271c6fdbd8SKent Overstreet }
20281c6fdbd8SKent Overstreet 
20291c6fdbd8SKent Overstreet /* O_DIRECT writes */
20301c6fdbd8SKent Overstreet 
20316fed42bbSKent Overstreet static bool bch2_check_range_allocated(struct bch_fs *c, subvol_inum inum,
20326fed42bbSKent Overstreet 				       u64 offset, u64 size,
20336fed42bbSKent Overstreet 				       unsigned nr_replicas, bool compressed)
20346fed42bbSKent Overstreet {
20356fed42bbSKent Overstreet 	struct btree_trans trans;
20366fed42bbSKent Overstreet 	struct btree_iter iter;
20376fed42bbSKent Overstreet 	struct bkey_s_c k;
20386fed42bbSKent Overstreet 	u64 end = offset + size;
20396fed42bbSKent Overstreet 	u32 snapshot;
20406fed42bbSKent Overstreet 	bool ret = true;
20416fed42bbSKent Overstreet 	int err;
20426fed42bbSKent Overstreet 
20436fed42bbSKent Overstreet 	bch2_trans_init(&trans, c, 0, 0);
20446fed42bbSKent Overstreet retry:
20456fed42bbSKent Overstreet 	bch2_trans_begin(&trans);
20466fed42bbSKent Overstreet 
20476fed42bbSKent Overstreet 	err = bch2_subvolume_get_snapshot(&trans, inum.subvol, &snapshot);
20486fed42bbSKent Overstreet 	if (err)
20496fed42bbSKent Overstreet 		goto err;
20506fed42bbSKent Overstreet 
2051e5fa91d7SKent Overstreet 	for_each_btree_key_norestart(&trans, iter, BTREE_ID_extents,
20526fed42bbSKent Overstreet 			   SPOS(inum.inum, offset, snapshot),
20536fed42bbSKent Overstreet 			   BTREE_ITER_SLOTS, k, err) {
2054e88a75ebSKent Overstreet 		if (bkey_ge(bkey_start_pos(k.k), POS(inum.inum, end)))
20556fed42bbSKent Overstreet 			break;
20566fed42bbSKent Overstreet 
20578c6d298aSKent Overstreet 		if (k.k->p.snapshot != snapshot ||
20588c6d298aSKent Overstreet 		    nr_replicas > bch2_bkey_replicas(c, k) ||
20596fed42bbSKent Overstreet 		    (!compressed && bch2_bkey_sectors_compressed(k))) {
20606fed42bbSKent Overstreet 			ret = false;
20616fed42bbSKent Overstreet 			break;
20626fed42bbSKent Overstreet 		}
20636fed42bbSKent Overstreet 	}
20646fed42bbSKent Overstreet 
20656fed42bbSKent Overstreet 	offset = iter.pos.offset;
20666fed42bbSKent Overstreet 	bch2_trans_iter_exit(&trans, &iter);
20676fed42bbSKent Overstreet err:
2068549d173cSKent Overstreet 	if (bch2_err_matches(err, BCH_ERR_transaction_restart))
20696fed42bbSKent Overstreet 		goto retry;
20706fed42bbSKent Overstreet 	bch2_trans_exit(&trans);
20716fed42bbSKent Overstreet 
20726fed42bbSKent Overstreet 	return err ? false : ret;
20736fed42bbSKent Overstreet }
20746fed42bbSKent Overstreet 
2075182c7bbfSKent Overstreet static noinline bool bch2_dio_write_check_allocated(struct dio_write *dio)
2076182c7bbfSKent Overstreet {
2077182c7bbfSKent Overstreet 	struct bch_fs *c = dio->op.c;
2078182c7bbfSKent Overstreet 	struct bch_inode_info *inode = dio->inode;
2079182c7bbfSKent Overstreet 	struct bio *bio = &dio->op.wbio.bio;
2080182c7bbfSKent Overstreet 
2081182c7bbfSKent Overstreet 	return bch2_check_range_allocated(c, inode_inum(inode),
2082182c7bbfSKent Overstreet 				dio->op.pos.offset, bio_sectors(bio),
2083182c7bbfSKent Overstreet 				dio->op.opts.data_replicas,
2084182c7bbfSKent Overstreet 				dio->op.opts.compression != 0);
2085182c7bbfSKent Overstreet }
2086182c7bbfSKent Overstreet 
2087a1ee777bSKent Overstreet static void bch2_dio_write_loop_async(struct bch_write_op *);
2088a1ee777bSKent Overstreet static __always_inline long bch2_dio_write_done(struct dio_write *dio);
2089a1ee777bSKent Overstreet 
20901c6fdbd8SKent Overstreet /*
20911c6fdbd8SKent Overstreet  * We're going to return -EIOCBQUEUED, but we haven't finished consuming the
20921c6fdbd8SKent Overstreet  * iov_iter yet, so we need to stash a copy of the iovec: it might be on the
20931c6fdbd8SKent Overstreet  * caller's stack, we're not guaranteed that it will live for the duration of
20941c6fdbd8SKent Overstreet  * the IO:
20951c6fdbd8SKent Overstreet  */
20961c6fdbd8SKent Overstreet static noinline int bch2_dio_write_copy_iov(struct dio_write *dio)
20971c6fdbd8SKent Overstreet {
20981c6fdbd8SKent Overstreet 	struct iovec *iov = dio->inline_vecs;
20991c6fdbd8SKent Overstreet 
21001c6fdbd8SKent Overstreet 	/*
21011c6fdbd8SKent Overstreet 	 * iov_iter has a single embedded iovec - nothing to do:
21021c6fdbd8SKent Overstreet 	 */
21031c6fdbd8SKent Overstreet 	if (iter_is_ubuf(&dio->iter))
21041c6fdbd8SKent Overstreet 		return 0;
21051c6fdbd8SKent Overstreet 
21061c6fdbd8SKent Overstreet 	/*
21071c6fdbd8SKent Overstreet 	 * We don't currently handle non-iovec iov_iters here - return an error,
21081c6fdbd8SKent Overstreet 	 * and we'll fall back to doing the IO synchronously:
21091c6fdbd8SKent Overstreet 	 */
21101c6fdbd8SKent Overstreet 	if (!iter_is_iovec(&dio->iter))
21111c6fdbd8SKent Overstreet 		return -1;
21121c6fdbd8SKent Overstreet 
21131c6fdbd8SKent Overstreet 	if (dio->iter.nr_segs > ARRAY_SIZE(dio->inline_vecs)) {
21141c6fdbd8SKent Overstreet 		iov = kmalloc_array(dio->iter.nr_segs, sizeof(*iov),
21151c6fdbd8SKent Overstreet 				    GFP_KERNEL);
21161c6fdbd8SKent Overstreet 		if (unlikely(!iov))
21171c6fdbd8SKent Overstreet 			return -ENOMEM;
21181c6fdbd8SKent Overstreet 
21191c6fdbd8SKent Overstreet 		dio->free_iov = true;
21201c6fdbd8SKent Overstreet 	}
21211c6fdbd8SKent Overstreet 
21221c6fdbd8SKent Overstreet 	memcpy(iov, dio->iter.__iov, dio->iter.nr_segs * sizeof(*iov));
21231c6fdbd8SKent Overstreet 	dio->iter.__iov = iov;
21241c6fdbd8SKent Overstreet 	return 0;
21251c6fdbd8SKent Overstreet }
21261c6fdbd8SKent Overstreet 
2127a1ee777bSKent Overstreet static void bch2_dio_write_flush_done(struct closure *cl)
2128a1ee777bSKent Overstreet {
2129a1ee777bSKent Overstreet 	struct dio_write *dio = container_of(cl, struct dio_write, op.cl);
2130a1ee777bSKent Overstreet 	struct bch_fs *c = dio->op.c;
2131a1ee777bSKent Overstreet 
2132a1ee777bSKent Overstreet 	closure_debug_destroy(cl);
2133a1ee777bSKent Overstreet 
2134a1ee777bSKent Overstreet 	dio->op.error = bch2_journal_error(&c->journal);
2135a1ee777bSKent Overstreet 
2136a1ee777bSKent Overstreet 	bch2_dio_write_done(dio);
2137a1ee777bSKent Overstreet }
2138a1ee777bSKent Overstreet 
2139a1ee777bSKent Overstreet static noinline void bch2_dio_write_flush(struct dio_write *dio)
2140a1ee777bSKent Overstreet {
2141a1ee777bSKent Overstreet 	struct bch_fs *c = dio->op.c;
2142a1ee777bSKent Overstreet 	struct bch_inode_unpacked inode;
2143a1ee777bSKent Overstreet 	int ret;
2144a1ee777bSKent Overstreet 
2145a1ee777bSKent Overstreet 	dio->flush = 0;
2146a1ee777bSKent Overstreet 
2147a1ee777bSKent Overstreet 	closure_init(&dio->op.cl, NULL);
2148a1ee777bSKent Overstreet 
2149a1ee777bSKent Overstreet 	if (!dio->op.error) {
2150a1ee777bSKent Overstreet 		ret = bch2_inode_find_by_inum(c, inode_inum(dio->inode), &inode);
2151a1ee777bSKent Overstreet 		if (ret)
2152a1ee777bSKent Overstreet 			dio->op.error = ret;
2153a1ee777bSKent Overstreet 		else
2154a1ee777bSKent Overstreet 			bch2_journal_flush_seq_async(&c->journal, inode.bi_journal_seq, &dio->op.cl);
2155a1ee777bSKent Overstreet 	}
2156a1ee777bSKent Overstreet 
2157a1ee777bSKent Overstreet 	if (dio->sync) {
2158a1ee777bSKent Overstreet 		closure_sync(&dio->op.cl);
2159a1ee777bSKent Overstreet 		closure_debug_destroy(&dio->op.cl);
2160a1ee777bSKent Overstreet 	} else {
2161a1ee777bSKent Overstreet 		continue_at(&dio->op.cl, bch2_dio_write_flush_done, NULL);
2162a1ee777bSKent Overstreet 	}
2163a1ee777bSKent Overstreet }
2164042a1f26SKent Overstreet 
2165182c7bbfSKent Overstreet static __always_inline long bch2_dio_write_done(struct dio_write *dio)
2166182c7bbfSKent Overstreet {
2167182c7bbfSKent Overstreet 	struct kiocb *req = dio->req;
2168182c7bbfSKent Overstreet 	struct bch_inode_info *inode = dio->inode;
2169182c7bbfSKent Overstreet 	bool sync = dio->sync;
2170a1ee777bSKent Overstreet 	long ret;
2171a1ee777bSKent Overstreet 
2172a1ee777bSKent Overstreet 	if (unlikely(dio->flush)) {
2173a1ee777bSKent Overstreet 		bch2_dio_write_flush(dio);
2174a1ee777bSKent Overstreet 		if (!sync)
2175a1ee777bSKent Overstreet 			return -EIOCBQUEUED;
2176a1ee777bSKent Overstreet 	}
2177182c7bbfSKent Overstreet 
2178a7ecd30cSKent Overstreet 	bch2_pagecache_block_put(inode);
2179182c7bbfSKent Overstreet 
2180182c7bbfSKent Overstreet 	if (dio->free_iov)
2181182c7bbfSKent Overstreet 		kfree(dio->iter.__iov);
2182a1ee777bSKent Overstreet 
2183a1ee777bSKent Overstreet 	ret = dio->op.error ?: ((long) dio->written << 9);
2184182c7bbfSKent Overstreet 	bio_put(&dio->op.wbio.bio);
2185182c7bbfSKent Overstreet 
2186182c7bbfSKent Overstreet 	/* inode->i_dio_count is our ref on inode and thus bch_fs */
2187182c7bbfSKent Overstreet 	inode_dio_end(&inode->v);
2188182c7bbfSKent Overstreet 
2189182c7bbfSKent Overstreet 	if (ret < 0)
2190182c7bbfSKent Overstreet 		ret = bch2_err_class(ret);
2191182c7bbfSKent Overstreet 
2192182c7bbfSKent Overstreet 	if (!sync) {
2193182c7bbfSKent Overstreet 		req->ki_complete(req, ret);
2194182c7bbfSKent Overstreet 		ret = -EIOCBQUEUED;
2195182c7bbfSKent Overstreet 	}
2196182c7bbfSKent Overstreet 	return ret;
2197182c7bbfSKent Overstreet }
2198182c7bbfSKent Overstreet 
2199182c7bbfSKent Overstreet static __always_inline void bch2_dio_write_end(struct dio_write *dio)
2200182c7bbfSKent Overstreet {
2201182c7bbfSKent Overstreet 	struct bch_fs *c = dio->op.c;
2202182c7bbfSKent Overstreet 	struct kiocb *req = dio->req;
2203182c7bbfSKent Overstreet 	struct bch_inode_info *inode = dio->inode;
2204182c7bbfSKent Overstreet 	struct bio *bio = &dio->op.wbio.bio;
2205182c7bbfSKent Overstreet 
2206182c7bbfSKent Overstreet 	req->ki_pos	+= (u64) dio->op.written << 9;
2207182c7bbfSKent Overstreet 	dio->written	+= dio->op.written;
2208182c7bbfSKent Overstreet 
22096b1b186aSKent Overstreet 	if (dio->extending) {
2210182c7bbfSKent Overstreet 		spin_lock(&inode->v.i_lock);
2211182c7bbfSKent Overstreet 		if (req->ki_pos > inode->v.i_size)
2212182c7bbfSKent Overstreet 			i_size_write(&inode->v, req->ki_pos);
2213182c7bbfSKent Overstreet 		spin_unlock(&inode->v.i_lock);
22146b1b186aSKent Overstreet 	}
22156b1b186aSKent Overstreet 
22166b1b186aSKent Overstreet 	if (dio->op.i_sectors_delta || dio->quota_res.sectors) {
22176b1b186aSKent Overstreet 		mutex_lock(&inode->ei_quota_lock);
22186b1b186aSKent Overstreet 		__i_sectors_acct(c, inode, &dio->quota_res, dio->op.i_sectors_delta);
22196b1b186aSKent Overstreet 		__bch2_quota_reservation_put(c, inode, &dio->quota_res);
22206b1b186aSKent Overstreet 		mutex_unlock(&inode->ei_quota_lock);
22216b1b186aSKent Overstreet 	}
2222182c7bbfSKent Overstreet 
2223182c7bbfSKent Overstreet 	bio_release_pages(bio, false);
2224182c7bbfSKent Overstreet 
2225182c7bbfSKent Overstreet 	if (unlikely(dio->op.error))
2226182c7bbfSKent Overstreet 		set_bit(EI_INODE_ERROR, &inode->ei_flags);
2227182c7bbfSKent Overstreet }
2228182c7bbfSKent Overstreet 
22294d868d18SKent Overstreet static __always_inline long bch2_dio_write_loop(struct dio_write *dio)
22301c6fdbd8SKent Overstreet {
2231182c7bbfSKent Overstreet 	struct bch_fs *c = dio->op.c;
22321c6fdbd8SKent Overstreet 	struct kiocb *req = dio->req;
2233182c7bbfSKent Overstreet 	struct address_space *mapping = dio->mapping;
2234182c7bbfSKent Overstreet 	struct bch_inode_info *inode = dio->inode;
223501ad6737SKent Overstreet 	struct bch_io_opts opts;
22369a3df993SKent Overstreet 	struct bio *bio = &dio->op.wbio.bio;
2237eb8e6e9cSKent Overstreet 	unsigned unaligned, iter_count;
2238eb8e6e9cSKent Overstreet 	bool sync = dio->sync, dropped_locks;
22391c6fdbd8SKent Overstreet 	long ret;
22401c6fdbd8SKent Overstreet 
224101ad6737SKent Overstreet 	bch2_inode_opts_get(&opts, c, &inode->ei_inode);
224201ad6737SKent Overstreet 
22431c6fdbd8SKent Overstreet 	while (1) {
2244eb8e6e9cSKent Overstreet 		iter_count = dio->iter.count;
2245eb8e6e9cSKent Overstreet 
2246182c7bbfSKent Overstreet 		EBUG_ON(current->faults_disabled_mapping);
22471c6fdbd8SKent Overstreet 		current->faults_disabled_mapping = mapping;
22481c6fdbd8SKent Overstreet 
22491c6fdbd8SKent Overstreet 		ret = bio_iov_iter_get_pages(bio, &dio->iter);
22501c6fdbd8SKent Overstreet 
2251eb8e6e9cSKent Overstreet 		dropped_locks = fdm_dropped_locks();
2252eb8e6e9cSKent Overstreet 
22531c6fdbd8SKent Overstreet 		current->faults_disabled_mapping = NULL;
22541c6fdbd8SKent Overstreet 
2255eb8e6e9cSKent Overstreet 		/*
2256eb8e6e9cSKent Overstreet 		 * If the fault handler returned an error but also signalled
2257eb8e6e9cSKent Overstreet 		 * that it dropped & retook ei_pagecache_lock, we just need to
2258eb8e6e9cSKent Overstreet 		 * re-shoot down the page cache and retry:
2259eb8e6e9cSKent Overstreet 		 */
2260eb8e6e9cSKent Overstreet 		if (dropped_locks && ret)
2261eb8e6e9cSKent Overstreet 			ret = 0;
2262eb8e6e9cSKent Overstreet 
22631c6fdbd8SKent Overstreet 		if (unlikely(ret < 0))
22641c6fdbd8SKent Overstreet 			goto err;
22651c6fdbd8SKent Overstreet 
2266eb8e6e9cSKent Overstreet 		if (unlikely(dropped_locks)) {
2267eb8e6e9cSKent Overstreet 			ret = write_invalidate_inode_pages_range(mapping,
2268eb8e6e9cSKent Overstreet 					req->ki_pos,
2269eb8e6e9cSKent Overstreet 					req->ki_pos + iter_count - 1);
2270eb8e6e9cSKent Overstreet 			if (unlikely(ret))
2271eb8e6e9cSKent Overstreet 				goto err;
2272eb8e6e9cSKent Overstreet 
2273eb8e6e9cSKent Overstreet 			if (!bio->bi_iter.bi_size)
2274eb8e6e9cSKent Overstreet 				continue;
2275eb8e6e9cSKent Overstreet 		}
2276eb8e6e9cSKent Overstreet 
22770a426c32SKent Overstreet 		unaligned = bio->bi_iter.bi_size & (block_bytes(c) - 1);
22780a426c32SKent Overstreet 		bio->bi_iter.bi_size -= unaligned;
22790a426c32SKent Overstreet 		iov_iter_revert(&dio->iter, unaligned);
22800a426c32SKent Overstreet 
22810a426c32SKent Overstreet 		if (!bio->bi_iter.bi_size) {
22820a426c32SKent Overstreet 			/*
22830a426c32SKent Overstreet 			 * bio_iov_iter_get_pages was only able to get <
22840a426c32SKent Overstreet 			 * blocksize worth of pages:
22850a426c32SKent Overstreet 			 */
22860a426c32SKent Overstreet 			ret = -EFAULT;
22870a426c32SKent Overstreet 			goto err;
22880a426c32SKent Overstreet 		}
22890a426c32SKent Overstreet 
229001ad6737SKent Overstreet 		bch2_write_op_init(&dio->op, c, opts);
2291182c7bbfSKent Overstreet 		dio->op.end_io		= sync
2292182c7bbfSKent Overstreet 			? NULL
2293182c7bbfSKent Overstreet 			: bch2_dio_write_loop_async;
2294042a1f26SKent Overstreet 		dio->op.target		= dio->op.opts.foreground_target;
2295042a1f26SKent Overstreet 		dio->op.write_point	= writepoint_hashed((unsigned long) current);
2296042a1f26SKent Overstreet 		dio->op.nr_replicas	= dio->op.opts.data_replicas;
22978c6d298aSKent Overstreet 		dio->op.subvol		= inode->ei_subvol;
2298042a1f26SKent Overstreet 		dio->op.pos		= POS(inode->v.i_ino, (u64) req->ki_pos >> 9);
2299042a1f26SKent Overstreet 
23001df3e199SKent Overstreet 		if (sync)
23011df3e199SKent Overstreet 			dio->op.flags |= BCH_WRITE_SYNC;
2302a6336910SKent Overstreet 		dio->op.flags |= BCH_WRITE_CHECK_ENOSPC;
2303042a1f26SKent Overstreet 
23046b1b186aSKent Overstreet 		ret = bch2_quota_reservation_add(c, inode, &dio->quota_res,
23056b1b186aSKent Overstreet 						 bio_sectors(bio), true);
23066b1b186aSKent Overstreet 		if (unlikely(ret))
23076b1b186aSKent Overstreet 			goto err;
23086b1b186aSKent Overstreet 
2309042a1f26SKent Overstreet 		ret = bch2_disk_reservation_get(c, &dio->op.res, bio_sectors(bio),
2310042a1f26SKent Overstreet 						dio->op.opts.data_replicas, 0);
2311042a1f26SKent Overstreet 		if (unlikely(ret) &&
2312182c7bbfSKent Overstreet 		    !bch2_dio_write_check_allocated(dio))
2313042a1f26SKent Overstreet 			goto err;
23141c6fdbd8SKent Overstreet 
23151c6fdbd8SKent Overstreet 		task_io_account_write(bio->bi_iter.bi_size);
23161c6fdbd8SKent Overstreet 
2317182c7bbfSKent Overstreet 		if (unlikely(dio->iter.count) &&
2318182c7bbfSKent Overstreet 		    !dio->sync &&
2319182c7bbfSKent Overstreet 		    !dio->loop &&
2320182c7bbfSKent Overstreet 		    bch2_dio_write_copy_iov(dio))
2321286d8ad0SKent Overstreet 			dio->sync = sync = true;
2322182c7bbfSKent Overstreet 
23231c6fdbd8SKent Overstreet 		dio->loop = true;
2324f8f30863SKent Overstreet 		closure_call(&dio->op.cl, bch2_write, NULL, NULL);
23251c6fdbd8SKent Overstreet 
2326182c7bbfSKent Overstreet 		if (!sync)
23271c6fdbd8SKent Overstreet 			return -EIOCBQUEUED;
23289a3df993SKent Overstreet 
2329182c7bbfSKent Overstreet 		bch2_dio_write_end(dio);
23309a3df993SKent Overstreet 
2331182c7bbfSKent Overstreet 		if (likely(!dio->iter.count) || dio->op.error)
23321c6fdbd8SKent Overstreet 			break;
2333f8f30863SKent Overstreet 
23341c6fdbd8SKent Overstreet 		bio_reset(bio, NULL, REQ_OP_WRITE);
23351c6fdbd8SKent Overstreet 	}
2336182c7bbfSKent Overstreet out:
2337182c7bbfSKent Overstreet 	return bch2_dio_write_done(dio);
23381c6fdbd8SKent Overstreet err:
2339182c7bbfSKent Overstreet 	dio->op.error = ret;
23401c6fdbd8SKent Overstreet 
23415468f119SKent Overstreet 	bio_release_pages(bio, false);
23426b1b186aSKent Overstreet 
23436b1b186aSKent Overstreet 	bch2_quota_reservation_put(c, inode, &dio->quota_res);
2344182c7bbfSKent Overstreet 	goto out;
23451c6fdbd8SKent Overstreet }
23461c6fdbd8SKent Overstreet 
23474d868d18SKent Overstreet static noinline __cold void bch2_dio_write_continue(struct dio_write *dio)
23481c6fdbd8SKent Overstreet {
2349182c7bbfSKent Overstreet 	struct mm_struct *mm = dio->mm;
23501c6fdbd8SKent Overstreet 
2351182c7bbfSKent Overstreet 	bio_reset(&dio->op.wbio.bio, NULL, REQ_OP_WRITE);
2352182c7bbfSKent Overstreet 
2353182c7bbfSKent Overstreet 	if (mm)
2354182c7bbfSKent Overstreet 		kthread_use_mm(mm);
23551c6fdbd8SKent Overstreet 	bch2_dio_write_loop(dio);
2356182c7bbfSKent Overstreet 	if (mm)
2357182c7bbfSKent Overstreet 		kthread_unuse_mm(mm);
23581c6fdbd8SKent Overstreet }
23591c6fdbd8SKent Overstreet 
23604d868d18SKent Overstreet static void bch2_dio_write_loop_async(struct bch_write_op *op)
23614d868d18SKent Overstreet {
23624d868d18SKent Overstreet 	struct dio_write *dio = container_of(op, struct dio_write, op);
23634d868d18SKent Overstreet 
23644d868d18SKent Overstreet 	bch2_dio_write_end(dio);
23654d868d18SKent Overstreet 
23664d868d18SKent Overstreet 	if (likely(!dio->iter.count) || dio->op.error)
23674d868d18SKent Overstreet 		bch2_dio_write_done(dio);
23684d868d18SKent Overstreet 	else
23694d868d18SKent Overstreet 		bch2_dio_write_continue(dio);
23704d868d18SKent Overstreet }
23714d868d18SKent Overstreet 
23721c6fdbd8SKent Overstreet static noinline
23731c6fdbd8SKent Overstreet ssize_t bch2_direct_write(struct kiocb *req, struct iov_iter *iter)
23741c6fdbd8SKent Overstreet {
23751c6fdbd8SKent Overstreet 	struct file *file = req->ki_filp;
237654847d25SKent Overstreet 	struct address_space *mapping = file->f_mapping;
23771c6fdbd8SKent Overstreet 	struct bch_inode_info *inode = file_bch_inode(file);
23781c6fdbd8SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
23791c6fdbd8SKent Overstreet 	struct dio_write *dio;
23801c6fdbd8SKent Overstreet 	struct bio *bio;
23817edcfbfeSKent Overstreet 	bool locked = true, extending;
23821c6fdbd8SKent Overstreet 	ssize_t ret;
23831c6fdbd8SKent Overstreet 
23847edcfbfeSKent Overstreet 	prefetch(&c->opts);
23857edcfbfeSKent Overstreet 	prefetch((void *) &c->opts + 64);
23867edcfbfeSKent Overstreet 	prefetch(&inode->ei_inode);
23877edcfbfeSKent Overstreet 	prefetch((void *) &inode->ei_inode + 64);
23881c6fdbd8SKent Overstreet 
23897edcfbfeSKent Overstreet 	inode_lock(&inode->v);
23907edcfbfeSKent Overstreet 
23917edcfbfeSKent Overstreet 	ret = generic_write_checks(req, iter);
23927edcfbfeSKent Overstreet 	if (unlikely(ret <= 0))
23937edcfbfeSKent Overstreet 		goto err;
23947edcfbfeSKent Overstreet 
23957edcfbfeSKent Overstreet 	ret = file_remove_privs(file);
23967edcfbfeSKent Overstreet 	if (unlikely(ret))
23977edcfbfeSKent Overstreet 		goto err;
23987edcfbfeSKent Overstreet 
23997edcfbfeSKent Overstreet 	ret = file_update_time(file);
24007edcfbfeSKent Overstreet 	if (unlikely(ret))
24017edcfbfeSKent Overstreet 		goto err;
24021c6fdbd8SKent Overstreet 
2403919dbbd1SKent Overstreet 	if (unlikely((req->ki_pos|iter->count) & (block_bytes(c) - 1)))
24047edcfbfeSKent Overstreet 		goto err;
24057edcfbfeSKent Overstreet 
24067edcfbfeSKent Overstreet 	inode_dio_begin(&inode->v);
2407a7ecd30cSKent Overstreet 	bch2_pagecache_block_get(inode);
24087edcfbfeSKent Overstreet 
24097edcfbfeSKent Overstreet 	extending = req->ki_pos + iter->count > inode->v.i_size;
24107edcfbfeSKent Overstreet 	if (!extending) {
24117edcfbfeSKent Overstreet 		inode_unlock(&inode->v);
24127edcfbfeSKent Overstreet 		locked = false;
24137edcfbfeSKent Overstreet 	}
24141c6fdbd8SKent Overstreet 
24151c6fdbd8SKent Overstreet 	bio = bio_alloc_bioset(NULL,
24164d126dc8SKent Overstreet 			       bio_iov_vecs_to_alloc(iter, BIO_MAX_VECS),
24171c6fdbd8SKent Overstreet 			       REQ_OP_WRITE,
24181c6fdbd8SKent Overstreet 			       GFP_KERNEL,
24191c6fdbd8SKent Overstreet 			       &c->dio_write_bioset);
24209a3df993SKent Overstreet 	dio = container_of(bio, struct dio_write, op.wbio.bio);
24211c6fdbd8SKent Overstreet 	dio->req		= req;
2422182c7bbfSKent Overstreet 	dio->mapping		= mapping;
2423182c7bbfSKent Overstreet 	dio->inode		= inode;
2424ed484030SKent Overstreet 	dio->mm			= current->mm;
24251c6fdbd8SKent Overstreet 	dio->loop		= false;
24266b1b186aSKent Overstreet 	dio->extending		= extending;
24277edcfbfeSKent Overstreet 	dio->sync		= is_sync_kiocb(req) || extending;
2428a1ee777bSKent Overstreet 	dio->flush		= iocb_is_dsync(req) && !c->opts.journal_flush_disabled;
24291c6fdbd8SKent Overstreet 	dio->free_iov		= false;
24301c6fdbd8SKent Overstreet 	dio->quota_res.sectors	= 0;
2431042a1f26SKent Overstreet 	dio->written		= 0;
24321c6fdbd8SKent Overstreet 	dio->iter		= *iter;
2433182c7bbfSKent Overstreet 	dio->op.c		= c;
24349a3df993SKent Overstreet 
2435a023127aSKent Overstreet 	if (unlikely(mapping->nrpages)) {
243654847d25SKent Overstreet 		ret = write_invalidate_inode_pages_range(mapping,
243754847d25SKent Overstreet 						req->ki_pos,
243854847d25SKent Overstreet 						req->ki_pos + iter->count - 1);
243954847d25SKent Overstreet 		if (unlikely(ret))
244054847d25SKent Overstreet 			goto err_put_bio;
2441a023127aSKent Overstreet 	}
244254847d25SKent Overstreet 
24437edcfbfeSKent Overstreet 	ret = bch2_dio_write_loop(dio);
24441c6fdbd8SKent Overstreet err:
24457edcfbfeSKent Overstreet 	if (locked)
24467edcfbfeSKent Overstreet 		inode_unlock(&inode->v);
24477edcfbfeSKent Overstreet 	return ret;
24487edcfbfeSKent Overstreet err_put_bio:
2449a7ecd30cSKent Overstreet 	bch2_pagecache_block_put(inode);
24501c6fdbd8SKent Overstreet 	bio_put(bio);
24517edcfbfeSKent Overstreet 	inode_dio_end(&inode->v);
24527edcfbfeSKent Overstreet 	goto err;
24531c6fdbd8SKent Overstreet }
24541c6fdbd8SKent Overstreet 
24557edcfbfeSKent Overstreet ssize_t bch2_write_iter(struct kiocb *iocb, struct iov_iter *from)
24561c6fdbd8SKent Overstreet {
24571c6fdbd8SKent Overstreet 	struct file *file = iocb->ki_filp;
24587edcfbfeSKent Overstreet 	struct bch_inode_info *inode = file_bch_inode(file);
24591c6fdbd8SKent Overstreet 	ssize_t	ret;
24601c6fdbd8SKent Overstreet 
24615c1ef830SKent Overstreet 	if (iocb->ki_flags & IOCB_DIRECT) {
24625c1ef830SKent Overstreet 		ret = bch2_direct_write(iocb, from);
24635c1ef830SKent Overstreet 		goto out;
24645c1ef830SKent Overstreet 	}
24651c6fdbd8SKent Overstreet 
24667edcfbfeSKent Overstreet 	inode_lock(&inode->v);
24677edcfbfeSKent Overstreet 
24687edcfbfeSKent Overstreet 	ret = generic_write_checks(iocb, from);
24697edcfbfeSKent Overstreet 	if (ret <= 0)
24707edcfbfeSKent Overstreet 		goto unlock;
24717edcfbfeSKent Overstreet 
24721c6fdbd8SKent Overstreet 	ret = file_remove_privs(file);
24731c6fdbd8SKent Overstreet 	if (ret)
24747edcfbfeSKent Overstreet 		goto unlock;
24751c6fdbd8SKent Overstreet 
24761c6fdbd8SKent Overstreet 	ret = file_update_time(file);
24771c6fdbd8SKent Overstreet 	if (ret)
24787edcfbfeSKent Overstreet 		goto unlock;
24791c6fdbd8SKent Overstreet 
24807edcfbfeSKent Overstreet 	ret = bch2_buffered_write(iocb, from);
24811c6fdbd8SKent Overstreet 	if (likely(ret > 0))
24821c6fdbd8SKent Overstreet 		iocb->ki_pos += ret;
24837edcfbfeSKent Overstreet unlock:
24841c6fdbd8SKent Overstreet 	inode_unlock(&inode->v);
24851c6fdbd8SKent Overstreet 
24867edcfbfeSKent Overstreet 	if (ret > 0)
24871c6fdbd8SKent Overstreet 		ret = generic_write_sync(iocb, ret);
24885c1ef830SKent Overstreet out:
24895c1ef830SKent Overstreet 	return bch2_err_class(ret);
24901c6fdbd8SKent Overstreet }
24911c6fdbd8SKent Overstreet 
24921c6fdbd8SKent Overstreet /* fsync: */
24931c6fdbd8SKent Overstreet 
249468a2054dSKent Overstreet /*
249568a2054dSKent Overstreet  * inode->ei_inode.bi_journal_seq won't be up to date since it's set in an
249668a2054dSKent Overstreet  * insert trigger: look up the btree inode instead
249768a2054dSKent Overstreet  */
249868a2054dSKent Overstreet static int bch2_flush_inode(struct bch_fs *c, subvol_inum inum)
249968a2054dSKent Overstreet {
250068a2054dSKent Overstreet 	struct bch_inode_unpacked inode;
250168a2054dSKent Overstreet 	int ret;
250268a2054dSKent Overstreet 
250368a2054dSKent Overstreet 	if (c->opts.journal_flush_disabled)
250468a2054dSKent Overstreet 		return 0;
250568a2054dSKent Overstreet 
250668a2054dSKent Overstreet 	ret = bch2_inode_find_by_inum(c, inum, &inode);
250768a2054dSKent Overstreet 	if (ret)
250868a2054dSKent Overstreet 		return ret;
250968a2054dSKent Overstreet 
251068a2054dSKent Overstreet 	return bch2_journal_flush_seq(&c->journal, inode.bi_journal_seq);
251168a2054dSKent Overstreet }
251268a2054dSKent Overstreet 
25131c6fdbd8SKent Overstreet int bch2_fsync(struct file *file, loff_t start, loff_t end, int datasync)
25141c6fdbd8SKent Overstreet {
25151c6fdbd8SKent Overstreet 	struct bch_inode_info *inode = file_bch_inode(file);
25161c6fdbd8SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
251768a2054dSKent Overstreet 	int ret, ret2, ret3;
25181c6fdbd8SKent Overstreet 
25191c6fdbd8SKent Overstreet 	ret = file_write_and_wait_range(file, start, end);
252068a2054dSKent Overstreet 	ret2 = sync_inode_metadata(&inode->v, 1);
252168a2054dSKent Overstreet 	ret3 = bch2_flush_inode(c, inode_inum(inode));
25221c6fdbd8SKent Overstreet 
25235c1ef830SKent Overstreet 	return bch2_err_class(ret ?: ret2 ?: ret3);
25241c6fdbd8SKent Overstreet }
25251c6fdbd8SKent Overstreet 
25261c6fdbd8SKent Overstreet /* truncate: */
25271c6fdbd8SKent Overstreet 
25286fed42bbSKent Overstreet static inline int range_has_data(struct bch_fs *c, u32 subvol,
25291c6fdbd8SKent Overstreet 				 struct bpos start,
25301c6fdbd8SKent Overstreet 				 struct bpos end)
25311c6fdbd8SKent Overstreet {
2532424eb881SKent Overstreet 	struct btree_trans trans;
253367e0dd8fSKent Overstreet 	struct btree_iter iter;
25341c6fdbd8SKent Overstreet 	struct bkey_s_c k;
25351c6fdbd8SKent Overstreet 	int ret = 0;
25361c6fdbd8SKent Overstreet 
253720bceecbSKent Overstreet 	bch2_trans_init(&trans, c, 0, 0);
25386fed42bbSKent Overstreet retry:
25396fed42bbSKent Overstreet 	bch2_trans_begin(&trans);
25406fed42bbSKent Overstreet 
25416fed42bbSKent Overstreet 	ret = bch2_subvolume_get_snapshot(&trans, subvol, &start.snapshot);
25426fed42bbSKent Overstreet 	if (ret)
25436fed42bbSKent Overstreet 		goto err;
2544424eb881SKent Overstreet 
2545c72f687aSKent Overstreet 	for_each_btree_key_upto_norestart(&trans, iter, BTREE_ID_extents, start, end, 0, k, ret)
25461c6fdbd8SKent Overstreet 		if (bkey_extent_is_data(k.k)) {
25471c6fdbd8SKent Overstreet 			ret = 1;
25481c6fdbd8SKent Overstreet 			break;
25491c6fdbd8SKent Overstreet 		}
25506fed42bbSKent Overstreet 	start = iter.pos;
255167e0dd8fSKent Overstreet 	bch2_trans_iter_exit(&trans, &iter);
25526fed42bbSKent Overstreet err:
2553549d173cSKent Overstreet 	if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
25546fed42bbSKent Overstreet 		goto retry;
25551c6fdbd8SKent Overstreet 
25569a796fdbSKent Overstreet 	bch2_trans_exit(&trans);
25579a796fdbSKent Overstreet 	return ret;
25581c6fdbd8SKent Overstreet }
25591c6fdbd8SKent Overstreet 
25601c6fdbd8SKent Overstreet static int __bch2_truncate_page(struct bch_inode_info *inode,
25611c6fdbd8SKent Overstreet 				pgoff_t index, loff_t start, loff_t end)
25621c6fdbd8SKent Overstreet {
25631c6fdbd8SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
25641c6fdbd8SKent Overstreet 	struct address_space *mapping = inode->v.i_mapping;
2565a99b1cafSKent Overstreet 	struct bch_page_state *s;
25661c6fdbd8SKent Overstreet 	unsigned start_offset = start & (PAGE_SIZE - 1);
25671c6fdbd8SKent Overstreet 	unsigned end_offset = ((end - 1) & (PAGE_SIZE - 1)) + 1;
2568a99b1cafSKent Overstreet 	unsigned i;
25691c6fdbd8SKent Overstreet 	struct page *page;
2570b19d307dSKent Overstreet 	s64 i_sectors_delta = 0;
25711c6fdbd8SKent Overstreet 	int ret = 0;
25721c6fdbd8SKent Overstreet 
25731c6fdbd8SKent Overstreet 	/* Page boundary? Nothing to do */
25741c6fdbd8SKent Overstreet 	if (!((index == start >> PAGE_SHIFT && start_offset) ||
25751c6fdbd8SKent Overstreet 	      (index == end >> PAGE_SHIFT && end_offset != PAGE_SIZE)))
25761c6fdbd8SKent Overstreet 		return 0;
25771c6fdbd8SKent Overstreet 
25781c6fdbd8SKent Overstreet 	/* Above i_size? */
25791c6fdbd8SKent Overstreet 	if (index << PAGE_SHIFT >= inode->v.i_size)
25801c6fdbd8SKent Overstreet 		return 0;
25811c6fdbd8SKent Overstreet 
25821c6fdbd8SKent Overstreet 	page = find_lock_page(mapping, index);
25831c6fdbd8SKent Overstreet 	if (!page) {
25841c6fdbd8SKent Overstreet 		/*
25851c6fdbd8SKent Overstreet 		 * XXX: we're doing two index lookups when we end up reading the
25861c6fdbd8SKent Overstreet 		 * page
25871c6fdbd8SKent Overstreet 		 */
25886fed42bbSKent Overstreet 		ret = range_has_data(c, inode->ei_subvol,
2589c72f687aSKent Overstreet 				POS(inode->v.i_ino, (index << PAGE_SECTORS_SHIFT)),
2590c72f687aSKent Overstreet 				POS(inode->v.i_ino, (index << PAGE_SECTORS_SHIFT) + PAGE_SECTORS));
25911c6fdbd8SKent Overstreet 		if (ret <= 0)
25921c6fdbd8SKent Overstreet 			return ret;
25931c6fdbd8SKent Overstreet 
25941c6fdbd8SKent Overstreet 		page = find_or_create_page(mapping, index, GFP_KERNEL);
25951c6fdbd8SKent Overstreet 		if (unlikely(!page)) {
25961c6fdbd8SKent Overstreet 			ret = -ENOMEM;
25971c6fdbd8SKent Overstreet 			goto out;
25981c6fdbd8SKent Overstreet 		}
25991c6fdbd8SKent Overstreet 	}
26001c6fdbd8SKent Overstreet 
2601a99b1cafSKent Overstreet 	s = bch2_page_state_create(page, 0);
2602a99b1cafSKent Overstreet 	if (!s) {
2603a99b1cafSKent Overstreet 		ret = -ENOMEM;
2604a99b1cafSKent Overstreet 		goto unlock;
2605a99b1cafSKent Overstreet 	}
2606a99b1cafSKent Overstreet 
26071c6fdbd8SKent Overstreet 	if (!PageUptodate(page)) {
26081c6fdbd8SKent Overstreet 		ret = bch2_read_single_page(page, mapping);
26091c6fdbd8SKent Overstreet 		if (ret)
26101c6fdbd8SKent Overstreet 			goto unlock;
26111c6fdbd8SKent Overstreet 	}
26121c6fdbd8SKent Overstreet 
2613a99b1cafSKent Overstreet 	if (index != start >> PAGE_SHIFT)
2614a99b1cafSKent Overstreet 		start_offset = 0;
2615a99b1cafSKent Overstreet 	if (index != end >> PAGE_SHIFT)
2616a99b1cafSKent Overstreet 		end_offset = PAGE_SIZE;
2617a99b1cafSKent Overstreet 
2618a99b1cafSKent Overstreet 	for (i = round_up(start_offset, block_bytes(c)) >> 9;
2619a99b1cafSKent Overstreet 	     i < round_down(end_offset, block_bytes(c)) >> 9;
2620a99b1cafSKent Overstreet 	     i++) {
2621a99b1cafSKent Overstreet 		s->s[i].nr_replicas	= 0;
2622b19d307dSKent Overstreet 		if (s->s[i].state == SECTOR_DIRTY)
2623b19d307dSKent Overstreet 			i_sectors_delta--;
2624a99b1cafSKent Overstreet 		s->s[i].state		= SECTOR_UNALLOCATED;
2625a99b1cafSKent Overstreet 	}
2626a99b1cafSKent Overstreet 
2627b19d307dSKent Overstreet 	i_sectors_acct(c, inode, NULL, i_sectors_delta);
2628b19d307dSKent Overstreet 
262974163da7SKent Overstreet 	/*
263074163da7SKent Overstreet 	 * Caller needs to know whether this page will be written out by
263174163da7SKent Overstreet 	 * writeback - doing an i_size update if necessary - or whether it will
263274163da7SKent Overstreet 	 * be responsible for the i_size update:
263374163da7SKent Overstreet 	 */
263474163da7SKent Overstreet 	ret = s->s[(min_t(u64, inode->v.i_size - (index << PAGE_SHIFT),
263574163da7SKent Overstreet 			  PAGE_SIZE) - 1) >> 9].state >= SECTOR_DIRTY;
263674163da7SKent Overstreet 
2637a99b1cafSKent Overstreet 	zero_user_segment(page, start_offset, end_offset);
2638a99b1cafSKent Overstreet 
26391c6fdbd8SKent Overstreet 	/*
26401c6fdbd8SKent Overstreet 	 * Bit of a hack - we don't want truncate to fail due to -ENOSPC.
26411c6fdbd8SKent Overstreet 	 *
26421c6fdbd8SKent Overstreet 	 * XXX: because we aren't currently tracking whether the page has actual
26431c6fdbd8SKent Overstreet 	 * data in it (vs. just 0s, or only partially written) this wrong. ick.
26441c6fdbd8SKent Overstreet 	 */
264574163da7SKent Overstreet 	BUG_ON(bch2_get_page_disk_reservation(c, inode, page, false));
26461c6fdbd8SKent Overstreet 
26479ba2eb25SKent Overstreet 	/*
26489ba2eb25SKent Overstreet 	 * This removes any writeable userspace mappings; we need to force
26499ba2eb25SKent Overstreet 	 * .page_mkwrite to be called again before any mmapped writes, to
26509ba2eb25SKent Overstreet 	 * redirty the full page:
26519ba2eb25SKent Overstreet 	 */
26529ba2eb25SKent Overstreet 	page_mkclean(page);
2653d1542e03SKent Overstreet 	filemap_dirty_folio(mapping, page_folio(page));
26541c6fdbd8SKent Overstreet unlock:
26551c6fdbd8SKent Overstreet 	unlock_page(page);
26561c6fdbd8SKent Overstreet 	put_page(page);
26571c6fdbd8SKent Overstreet out:
26581c6fdbd8SKent Overstreet 	return ret;
26591c6fdbd8SKent Overstreet }
26601c6fdbd8SKent Overstreet 
26611c6fdbd8SKent Overstreet static int bch2_truncate_page(struct bch_inode_info *inode, loff_t from)
26621c6fdbd8SKent Overstreet {
26631c6fdbd8SKent Overstreet 	return __bch2_truncate_page(inode, from >> PAGE_SHIFT,
2664a99b1cafSKent Overstreet 				    from, round_up(from, PAGE_SIZE));
26651c6fdbd8SKent Overstreet }
26661c6fdbd8SKent Overstreet 
266774163da7SKent Overstreet static int bch2_truncate_pages(struct bch_inode_info *inode,
266874163da7SKent Overstreet 			       loff_t start, loff_t end)
266974163da7SKent Overstreet {
267074163da7SKent Overstreet 	int ret = __bch2_truncate_page(inode, start >> PAGE_SHIFT,
267174163da7SKent Overstreet 				       start, end);
267274163da7SKent Overstreet 
267374163da7SKent Overstreet 	if (ret >= 0 &&
267474163da7SKent Overstreet 	    start >> PAGE_SHIFT != end >> PAGE_SHIFT)
267574163da7SKent Overstreet 		ret = __bch2_truncate_page(inode,
267674163da7SKent Overstreet 					   end >> PAGE_SHIFT,
267774163da7SKent Overstreet 					   start, end);
267874163da7SKent Overstreet 	return ret;
267974163da7SKent Overstreet }
268074163da7SKent Overstreet 
268168a507a2SKent Overstreet static int bch2_extend(struct mnt_idmap *idmap,
268268a507a2SKent Overstreet 		       struct bch_inode_info *inode,
2683e0541a93SKent Overstreet 		       struct bch_inode_unpacked *inode_u,
2684e0541a93SKent Overstreet 		       struct iattr *iattr)
26851c6fdbd8SKent Overstreet {
26861c6fdbd8SKent Overstreet 	struct address_space *mapping = inode->v.i_mapping;
26871c6fdbd8SKent Overstreet 	int ret;
26881c6fdbd8SKent Overstreet 
2689e0541a93SKent Overstreet 	/*
2690e0541a93SKent Overstreet 	 * sync appends:
26912925fc49SKent Overstreet 	 *
26922925fc49SKent Overstreet 	 * this has to be done _before_ extending i_size:
2693e0541a93SKent Overstreet 	 */
2694e0541a93SKent Overstreet 	ret = filemap_write_and_wait_range(mapping, inode_u->bi_size, S64_MAX);
26951c6fdbd8SKent Overstreet 	if (ret)
26961c6fdbd8SKent Overstreet 		return ret;
26971c6fdbd8SKent Overstreet 
26981c6fdbd8SKent Overstreet 	truncate_setsize(&inode->v, iattr->ia_size);
26991c6fdbd8SKent Overstreet 
270068a507a2SKent Overstreet 	return bch2_setattr_nonsize(idmap, inode, iattr);
27011c6fdbd8SKent Overstreet }
27021c6fdbd8SKent Overstreet 
270354e2264eSKent Overstreet static int bch2_truncate_finish_fn(struct bch_inode_info *inode,
270454e2264eSKent Overstreet 				   struct bch_inode_unpacked *bi,
270554e2264eSKent Overstreet 				   void *p)
270654e2264eSKent Overstreet {
270754e2264eSKent Overstreet 	bi->bi_flags &= ~BCH_INODE_I_SIZE_DIRTY;
270854e2264eSKent Overstreet 	return 0;
270954e2264eSKent Overstreet }
271054e2264eSKent Overstreet 
271154e2264eSKent Overstreet static int bch2_truncate_start_fn(struct bch_inode_info *inode,
271254e2264eSKent Overstreet 				  struct bch_inode_unpacked *bi, void *p)
271354e2264eSKent Overstreet {
271454e2264eSKent Overstreet 	u64 *new_i_size = p;
271554e2264eSKent Overstreet 
271654e2264eSKent Overstreet 	bi->bi_flags |= BCH_INODE_I_SIZE_DIRTY;
271754e2264eSKent Overstreet 	bi->bi_size = *new_i_size;
271854e2264eSKent Overstreet 	return 0;
271954e2264eSKent Overstreet }
272054e2264eSKent Overstreet 
272168a507a2SKent Overstreet int bch2_truncate(struct mnt_idmap *idmap,
272268a507a2SKent Overstreet 		  struct bch_inode_info *inode, struct iattr *iattr)
27231c6fdbd8SKent Overstreet {
27241c6fdbd8SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
27251c6fdbd8SKent Overstreet 	struct address_space *mapping = inode->v.i_mapping;
2726e0541a93SKent Overstreet 	struct bch_inode_unpacked inode_u;
272754e2264eSKent Overstreet 	u64 new_i_size = iattr->ia_size;
27282e87eae1SKent Overstreet 	s64 i_sectors_delta = 0;
27291c6fdbd8SKent Overstreet 	int ret = 0;
27301c6fdbd8SKent Overstreet 
273168a507a2SKent Overstreet 	/*
273278d66ab1SDan Robertson 	 * If the truncate call with change the size of the file, the
273378d66ab1SDan Robertson 	 * cmtimes should be updated. If the size will not change, we
273478d66ab1SDan Robertson 	 * do not need to update the cmtimes.
273568a507a2SKent Overstreet 	 */
273678d66ab1SDan Robertson 	if (iattr->ia_size != inode->v.i_size) {
273768a507a2SKent Overstreet 		if (!(iattr->ia_valid & ATTR_MTIME))
273868a507a2SKent Overstreet 			ktime_get_coarse_real_ts64(&iattr->ia_mtime);
273968a507a2SKent Overstreet 		if (!(iattr->ia_valid & ATTR_CTIME))
274068a507a2SKent Overstreet 			ktime_get_coarse_real_ts64(&iattr->ia_ctime);
274168a507a2SKent Overstreet 		iattr->ia_valid |= ATTR_MTIME|ATTR_CTIME;
274278d66ab1SDan Robertson 	}
274368a507a2SKent Overstreet 
27441c6fdbd8SKent Overstreet 	inode_dio_wait(&inode->v);
2745a7ecd30cSKent Overstreet 	bch2_pagecache_block_get(inode);
27461c6fdbd8SKent Overstreet 
27476fed42bbSKent Overstreet 	ret = bch2_inode_find_by_inum(c, inode_inum(inode), &inode_u);
2748e0541a93SKent Overstreet 	if (ret)
2749e0541a93SKent Overstreet 		goto err;
27501c6fdbd8SKent Overstreet 
2751c45d473dSKent Overstreet 	/*
2752c45d473dSKent Overstreet 	 * check this before next assertion; on filesystem error our normal
2753c45d473dSKent Overstreet 	 * invariants are a bit broken (truncate has to truncate the page cache
2754c45d473dSKent Overstreet 	 * before the inode).
2755c45d473dSKent Overstreet 	 */
2756c45d473dSKent Overstreet 	ret = bch2_journal_error(&c->journal);
2757c45d473dSKent Overstreet 	if (ret)
2758c45d473dSKent Overstreet 		goto err;
2759c45d473dSKent Overstreet 
27608eb71e9eSKent Overstreet 	WARN_ONCE(!test_bit(EI_INODE_ERROR, &inode->ei_flags) &&
27618eb71e9eSKent Overstreet 		  inode->v.i_size < inode_u.bi_size,
27628eb71e9eSKent Overstreet 		  "truncate spotted in mem i_size < btree i_size: %llu < %llu\n",
27638eb71e9eSKent Overstreet 		  (u64) inode->v.i_size, inode_u.bi_size);
2764e0541a93SKent Overstreet 
2765e0541a93SKent Overstreet 	if (iattr->ia_size > inode->v.i_size) {
276668a507a2SKent Overstreet 		ret = bch2_extend(idmap, inode, &inode_u, iattr);
276754e2264eSKent Overstreet 		goto err;
27681c6fdbd8SKent Overstreet 	}
27691c6fdbd8SKent Overstreet 
277068a507a2SKent Overstreet 	iattr->ia_valid &= ~ATTR_SIZE;
277168a507a2SKent Overstreet 
27721c6fdbd8SKent Overstreet 	ret = bch2_truncate_page(inode, iattr->ia_size);
277374163da7SKent Overstreet 	if (unlikely(ret < 0))
277454e2264eSKent Overstreet 		goto err;
27751c6fdbd8SKent Overstreet 
27766cc3535dSKent Overstreet 	/*
27776cc3535dSKent Overstreet 	 * When extending, we're going to write the new i_size to disk
27786cc3535dSKent Overstreet 	 * immediately so we need to flush anything above the current on disk
27796cc3535dSKent Overstreet 	 * i_size first:
27806cc3535dSKent Overstreet 	 *
27816cc3535dSKent Overstreet 	 * Also, when extending we need to flush the page that i_size currently
27826cc3535dSKent Overstreet 	 * straddles - if it's mapped to userspace, we need to ensure that
27836cc3535dSKent Overstreet 	 * userspace has to redirty it and call .mkwrite -> set_page_dirty
27846cc3535dSKent Overstreet 	 * again to allocate the part of the page that was extended.
27856cc3535dSKent Overstreet 	 */
2786e0541a93SKent Overstreet 	if (iattr->ia_size > inode_u.bi_size)
27871c6fdbd8SKent Overstreet 		ret = filemap_write_and_wait_range(mapping,
2788e0541a93SKent Overstreet 				inode_u.bi_size,
27891c6fdbd8SKent Overstreet 				iattr->ia_size - 1);
27901c6fdbd8SKent Overstreet 	else if (iattr->ia_size & (PAGE_SIZE - 1))
27911c6fdbd8SKent Overstreet 		ret = filemap_write_and_wait_range(mapping,
27921c6fdbd8SKent Overstreet 				round_down(iattr->ia_size, PAGE_SIZE),
27931c6fdbd8SKent Overstreet 				iattr->ia_size - 1);
27941c6fdbd8SKent Overstreet 	if (ret)
279554e2264eSKent Overstreet 		goto err;
27961c6fdbd8SKent Overstreet 
279754e2264eSKent Overstreet 	mutex_lock(&inode->ei_update_lock);
279854e2264eSKent Overstreet 	ret = bch2_write_inode(c, inode, bch2_truncate_start_fn,
279954e2264eSKent Overstreet 			       &new_i_size, 0);
280054e2264eSKent Overstreet 	mutex_unlock(&inode->ei_update_lock);
28011c6fdbd8SKent Overstreet 
28021c6fdbd8SKent Overstreet 	if (unlikely(ret))
280354e2264eSKent Overstreet 		goto err;
28041c6fdbd8SKent Overstreet 
28051c6fdbd8SKent Overstreet 	truncate_setsize(&inode->v, iattr->ia_size);
28061c6fdbd8SKent Overstreet 
28078c6d298aSKent Overstreet 	ret = bch2_fpunch(c, inode_inum(inode),
2808a99b1cafSKent Overstreet 			round_up(iattr->ia_size, block_bytes(c)) >> 9,
280968a2054dSKent Overstreet 			U64_MAX, &i_sectors_delta);
28102e87eae1SKent Overstreet 	i_sectors_acct(c, inode, NULL, i_sectors_delta);
28112e87eae1SKent Overstreet 
2812b33bf1bcSKent Overstreet 	bch2_fs_inconsistent_on(!inode->v.i_size && inode->v.i_blocks &&
2813b33bf1bcSKent Overstreet 				!bch2_journal_error(&c->journal), c,
2814b33bf1bcSKent Overstreet 				"inode %lu truncated to 0 but i_blocks %llu (ondisk %lli)",
2815b33bf1bcSKent Overstreet 				inode->v.i_ino, (u64) inode->v.i_blocks,
2816b33bf1bcSKent Overstreet 				inode->ei_inode.bi_sectors);
28171c6fdbd8SKent Overstreet 	if (unlikely(ret))
281854e2264eSKent Overstreet 		goto err;
28191c6fdbd8SKent Overstreet 
282054e2264eSKent Overstreet 	mutex_lock(&inode->ei_update_lock);
282168a507a2SKent Overstreet 	ret = bch2_write_inode(c, inode, bch2_truncate_finish_fn, NULL, 0);
282254e2264eSKent Overstreet 	mutex_unlock(&inode->ei_update_lock);
282368a507a2SKent Overstreet 
282468a507a2SKent Overstreet 	ret = bch2_setattr_nonsize(idmap, inode, iattr);
282554e2264eSKent Overstreet err:
2826a7ecd30cSKent Overstreet 	bch2_pagecache_block_put(inode);
28275c1ef830SKent Overstreet 	return bch2_err_class(ret);
28281c6fdbd8SKent Overstreet }
28291c6fdbd8SKent Overstreet 
28301c6fdbd8SKent Overstreet /* fallocate: */
28311c6fdbd8SKent Overstreet 
2832050197b1SKent Overstreet static int inode_update_times_fn(struct bch_inode_info *inode,
2833050197b1SKent Overstreet 				 struct bch_inode_unpacked *bi, void *p)
2834050197b1SKent Overstreet {
2835050197b1SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
2836050197b1SKent Overstreet 
2837050197b1SKent Overstreet 	bi->bi_mtime = bi->bi_ctime = bch2_current_time(c);
2838050197b1SKent Overstreet 	return 0;
2839050197b1SKent Overstreet }
2840050197b1SKent Overstreet 
28412e87eae1SKent Overstreet static long bchfs_fpunch(struct bch_inode_info *inode, loff_t offset, loff_t len)
28421c6fdbd8SKent Overstreet {
28431c6fdbd8SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
284474163da7SKent Overstreet 	u64 end		= offset + len;
284574163da7SKent Overstreet 	u64 block_start	= round_up(offset, block_bytes(c));
284674163da7SKent Overstreet 	u64 block_end	= round_down(end, block_bytes(c));
284774163da7SKent Overstreet 	bool truncated_last_page;
28481c6fdbd8SKent Overstreet 	int ret = 0;
28491c6fdbd8SKent Overstreet 
285074163da7SKent Overstreet 	ret = bch2_truncate_pages(inode, offset, end);
285174163da7SKent Overstreet 	if (unlikely(ret < 0))
28521c6fdbd8SKent Overstreet 		goto err;
28531c6fdbd8SKent Overstreet 
285474163da7SKent Overstreet 	truncated_last_page = ret;
28551c6fdbd8SKent Overstreet 
285674163da7SKent Overstreet 	truncate_pagecache_range(&inode->v, offset, end - 1);
28571c6fdbd8SKent Overstreet 
285874163da7SKent Overstreet 	if (block_start < block_end) {
28592e87eae1SKent Overstreet 		s64 i_sectors_delta = 0;
28602e87eae1SKent Overstreet 
28618c6d298aSKent Overstreet 		ret = bch2_fpunch(c, inode_inum(inode),
286274163da7SKent Overstreet 				  block_start >> 9, block_end >> 9,
28632e87eae1SKent Overstreet 				  &i_sectors_delta);
28642e87eae1SKent Overstreet 		i_sectors_acct(c, inode, NULL, i_sectors_delta);
28652e87eae1SKent Overstreet 	}
2866050197b1SKent Overstreet 
2867050197b1SKent Overstreet 	mutex_lock(&inode->ei_update_lock);
286874163da7SKent Overstreet 	if (end >= inode->v.i_size && !truncated_last_page) {
286974163da7SKent Overstreet 		ret = bch2_write_inode_size(c, inode, inode->v.i_size,
287074163da7SKent Overstreet 					    ATTR_MTIME|ATTR_CTIME);
287174163da7SKent Overstreet 	} else {
2872050197b1SKent Overstreet 		ret = bch2_write_inode(c, inode, inode_update_times_fn, NULL,
287374163da7SKent Overstreet 				       ATTR_MTIME|ATTR_CTIME);
287474163da7SKent Overstreet 	}
2875050197b1SKent Overstreet 	mutex_unlock(&inode->ei_update_lock);
28761c6fdbd8SKent Overstreet err:
28771c6fdbd8SKent Overstreet 	return ret;
28781c6fdbd8SKent Overstreet }
28791c6fdbd8SKent Overstreet 
28802e87eae1SKent Overstreet static long bchfs_fcollapse_finsert(struct bch_inode_info *inode,
28815f786787SKent Overstreet 				   loff_t offset, loff_t len,
28825f786787SKent Overstreet 				   bool insert)
28831c6fdbd8SKent Overstreet {
28841c6fdbd8SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
28851c6fdbd8SKent Overstreet 	struct address_space *mapping = inode->v.i_mapping;
288607a1006aSKent Overstreet 	struct bkey_buf copy;
2887d69f41d6SKent Overstreet 	struct btree_trans trans;
288867e0dd8fSKent Overstreet 	struct btree_iter src, dst, del;
28895f786787SKent Overstreet 	loff_t shift, new_size;
28905f786787SKent Overstreet 	u64 src_start;
289150dc0f69SKent Overstreet 	int ret = 0;
28921c6fdbd8SKent Overstreet 
28931c6fdbd8SKent Overstreet 	if ((offset | len) & (block_bytes(c) - 1))
28941c6fdbd8SKent Overstreet 		return -EINVAL;
28951c6fdbd8SKent Overstreet 
28965f786787SKent Overstreet 	if (insert) {
28975f786787SKent Overstreet 		if (inode->v.i_sb->s_maxbytes - inode->v.i_size < len)
289874163da7SKent Overstreet 			return -EFBIG;
28995f786787SKent Overstreet 
29005f786787SKent Overstreet 		if (offset >= inode->v.i_size)
290174163da7SKent Overstreet 			return -EINVAL;
29025f786787SKent Overstreet 
29035f786787SKent Overstreet 		src_start	= U64_MAX;
29045f786787SKent Overstreet 		shift		= len;
29055f786787SKent Overstreet 	} else {
29061c6fdbd8SKent Overstreet 		if (offset + len >= inode->v.i_size)
290774163da7SKent Overstreet 			return -EINVAL;
29081c6fdbd8SKent Overstreet 
29095f786787SKent Overstreet 		src_start	= offset + len;
29105f786787SKent Overstreet 		shift		= -len;
29115f786787SKent Overstreet 	}
29121c6fdbd8SKent Overstreet 
29135f786787SKent Overstreet 	new_size = inode->v.i_size + shift;
29141c6fdbd8SKent Overstreet 
29151c6fdbd8SKent Overstreet 	ret = write_invalidate_inode_pages_range(mapping, offset, LLONG_MAX);
29161c6fdbd8SKent Overstreet 	if (ret)
291774163da7SKent Overstreet 		return ret;
29181c6fdbd8SKent Overstreet 
29195f786787SKent Overstreet 	if (insert) {
29205f786787SKent Overstreet 		i_size_write(&inode->v, new_size);
29215f786787SKent Overstreet 		mutex_lock(&inode->ei_update_lock);
29225f786787SKent Overstreet 		ret = bch2_write_inode_size(c, inode, new_size,
29235f786787SKent Overstreet 					    ATTR_MTIME|ATTR_CTIME);
29245f786787SKent Overstreet 		mutex_unlock(&inode->ei_update_lock);
29255f786787SKent Overstreet 	} else {
29262e87eae1SKent Overstreet 		s64 i_sectors_delta = 0;
29272e87eae1SKent Overstreet 
29288c6d298aSKent Overstreet 		ret = bch2_fpunch(c, inode_inum(inode),
29292e87eae1SKent Overstreet 				  offset >> 9, (offset + len) >> 9,
29302e87eae1SKent Overstreet 				  &i_sectors_delta);
29312e87eae1SKent Overstreet 		i_sectors_acct(c, inode, NULL, i_sectors_delta);
29322e87eae1SKent Overstreet 
293363095894SKent Overstreet 		if (ret)
293474163da7SKent Overstreet 			return ret;
29355f786787SKent Overstreet 	}
29368ef231bdSKent Overstreet 
293750dc0f69SKent Overstreet 	bch2_bkey_buf_init(&copy);
2938f7beb4caSKent Overstreet 	bch2_trans_init(&trans, c, BTREE_ITER_MAX, 1024);
293967e0dd8fSKent Overstreet 	bch2_trans_iter_init(&trans, &src, BTREE_ID_extents,
29405f786787SKent Overstreet 			POS(inode->v.i_ino, src_start >> 9),
294163095894SKent Overstreet 			BTREE_ITER_INTENT);
294267e0dd8fSKent Overstreet 	bch2_trans_copy_iter(&dst, &src);
294367e0dd8fSKent Overstreet 	bch2_trans_copy_iter(&del, &src);
29445f786787SKent Overstreet 
2945549d173cSKent Overstreet 	while (ret == 0 ||
2946549d173cSKent Overstreet 	       bch2_err_matches(ret, BCH_ERR_transaction_restart)) {
294763095894SKent Overstreet 		struct disk_reservation disk_res =
294863095894SKent Overstreet 			bch2_disk_reservation_init(c, 0);
294963095894SKent Overstreet 		struct bkey_i delete;
295063095894SKent Overstreet 		struct bkey_s_c k;
295163095894SKent Overstreet 		struct bpos next_pos;
29525f786787SKent Overstreet 		struct bpos move_pos = POS(inode->v.i_ino, offset >> 9);
29535f786787SKent Overstreet 		struct bpos atomic_end;
29542d594dfbSKent Overstreet 		unsigned trigger_flags = 0;
29556fed42bbSKent Overstreet 		u32 snapshot;
29566fed42bbSKent Overstreet 
29576fed42bbSKent Overstreet 		bch2_trans_begin(&trans);
29586fed42bbSKent Overstreet 
29596fed42bbSKent Overstreet 		ret = bch2_subvolume_get_snapshot(&trans,
29606fed42bbSKent Overstreet 					inode->ei_subvol, &snapshot);
29616fed42bbSKent Overstreet 		if (ret)
29626fed42bbSKent Overstreet 			continue;
29636fed42bbSKent Overstreet 
29646fed42bbSKent Overstreet 		bch2_btree_iter_set_snapshot(&src, snapshot);
29656fed42bbSKent Overstreet 		bch2_btree_iter_set_snapshot(&dst, snapshot);
29666fed42bbSKent Overstreet 		bch2_btree_iter_set_snapshot(&del, snapshot);
296763095894SKent Overstreet 
2968700c25b3SKent Overstreet 		bch2_trans_begin(&trans);
2969700c25b3SKent Overstreet 
29705f786787SKent Overstreet 		k = insert
297167e0dd8fSKent Overstreet 			? bch2_btree_iter_peek_prev(&src)
2972c72f687aSKent Overstreet 			: bch2_btree_iter_peek_upto(&src, POS(inode->v.i_ino, U64_MAX));
297363095894SKent Overstreet 		if ((ret = bkey_err(k)))
297450dc0f69SKent Overstreet 			continue;
297563095894SKent Overstreet 
297663095894SKent Overstreet 		if (!k.k || k.k->p.inode != inode->v.i_ino)
297763095894SKent Overstreet 			break;
297863095894SKent Overstreet 
29795f786787SKent Overstreet 		if (insert &&
2980e88a75ebSKent Overstreet 		    bkey_le(k.k->p, POS(inode->v.i_ino, offset >> 9)))
29815f786787SKent Overstreet 			break;
29825f786787SKent Overstreet reassemble:
298307a1006aSKent Overstreet 		bch2_bkey_buf_reassemble(&copy, c, k);
29845f786787SKent Overstreet 
29855f786787SKent Overstreet 		if (insert &&
2986e88a75ebSKent Overstreet 		    bkey_lt(bkey_start_pos(k.k), move_pos))
298735189e09SKent Overstreet 			bch2_cut_front(move_pos, copy.k);
29885f786787SKent Overstreet 
298935189e09SKent Overstreet 		copy.k->k.p.offset += shift >> 9;
299067e0dd8fSKent Overstreet 		bch2_btree_iter_set_pos(&dst, bkey_start_pos(&copy.k->k));
29911c6fdbd8SKent Overstreet 
299267e0dd8fSKent Overstreet 		ret = bch2_extent_atomic_end(&trans, &dst, copy.k, &atomic_end);
29933c7f3b7aSKent Overstreet 		if (ret)
299450dc0f69SKent Overstreet 			continue;
2995e2d9912cSKent Overstreet 
2996e88a75ebSKent Overstreet 		if (!bkey_eq(atomic_end, copy.k->k.p)) {
29975f786787SKent Overstreet 			if (insert) {
29985f786787SKent Overstreet 				move_pos = atomic_end;
29995f786787SKent Overstreet 				move_pos.offset -= shift >> 9;
30005f786787SKent Overstreet 				goto reassemble;
30015f786787SKent Overstreet 			} else {
3002085ab693SKent Overstreet 				bch2_cut_back(atomic_end, copy.k);
30035f786787SKent Overstreet 			}
30045f786787SKent Overstreet 		}
30055f786787SKent Overstreet 
300663095894SKent Overstreet 		bkey_init(&delete.k);
3007283eda57SKent Overstreet 		delete.k.p = copy.k->k.p;
3008283eda57SKent Overstreet 		delete.k.size = copy.k->k.size;
3009283eda57SKent Overstreet 		delete.k.p.offset -= shift >> 9;
301067e0dd8fSKent Overstreet 		bch2_btree_iter_set_pos(&del, bkey_start_pos(&delete.k));
30111c6fdbd8SKent Overstreet 
30125f786787SKent Overstreet 		next_pos = insert ? bkey_start_pos(&delete.k) : delete.k.p;
301363095894SKent Overstreet 
30147c4ca54aSKent Overstreet 		if (copy.k->k.size != k.k->size) {
301563095894SKent Overstreet 			/* We might end up splitting compressed extents: */
301663095894SKent Overstreet 			unsigned nr_ptrs =
30174de77495SKent Overstreet 				bch2_bkey_nr_ptrs_allocated(bkey_i_to_s_c(copy.k));
301863095894SKent Overstreet 
301963095894SKent Overstreet 			ret = bch2_disk_reservation_get(c, &disk_res,
302035189e09SKent Overstreet 					copy.k->k.size, nr_ptrs,
30211c6fdbd8SKent Overstreet 					BCH_DISK_RESERVATION_NOFAIL);
30221c6fdbd8SKent Overstreet 			BUG_ON(ret);
302363095894SKent Overstreet 		}
30241c6fdbd8SKent Overstreet 
302567e0dd8fSKent Overstreet 		ret =   bch2_btree_iter_traverse(&del) ?:
302667e0dd8fSKent Overstreet 			bch2_trans_update(&trans, &del, &delete, trigger_flags) ?:
302767e0dd8fSKent Overstreet 			bch2_trans_update(&trans, &dst, copy.k, trigger_flags) ?:
302868a2054dSKent Overstreet 			bch2_trans_commit(&trans, &disk_res, NULL,
30292d594dfbSKent Overstreet 					  BTREE_INSERT_NOFAIL);
30301c6fdbd8SKent Overstreet 		bch2_disk_reservation_put(c, &disk_res);
303150dc0f69SKent Overstreet 
303263095894SKent Overstreet 		if (!ret)
303367e0dd8fSKent Overstreet 			bch2_btree_iter_set_pos(&src, next_pos);
303450dc0f69SKent Overstreet 	}
303567e0dd8fSKent Overstreet 	bch2_trans_iter_exit(&trans, &del);
303667e0dd8fSKent Overstreet 	bch2_trans_iter_exit(&trans, &dst);
303767e0dd8fSKent Overstreet 	bch2_trans_iter_exit(&trans, &src);
303850dc0f69SKent Overstreet 	bch2_trans_exit(&trans);
303950dc0f69SKent Overstreet 	bch2_bkey_buf_exit(&copy, c);
304063095894SKent Overstreet 
30418ef231bdSKent Overstreet 	if (ret)
304274163da7SKent Overstreet 		return ret;
30431c6fdbd8SKent Overstreet 
304474163da7SKent Overstreet 	mutex_lock(&inode->ei_update_lock);
30455f786787SKent Overstreet 	if (!insert) {
30468ef231bdSKent Overstreet 		i_size_write(&inode->v, new_size);
30478ef231bdSKent Overstreet 		ret = bch2_write_inode_size(c, inode, new_size,
30488ef231bdSKent Overstreet 					    ATTR_MTIME|ATTR_CTIME);
304974163da7SKent Overstreet 	} else {
305074163da7SKent Overstreet 		/* We need an inode update to update bi_journal_seq for fsync: */
305174163da7SKent Overstreet 		ret = bch2_write_inode(c, inode, inode_update_times_fn, NULL,
305274163da7SKent Overstreet 				       ATTR_MTIME|ATTR_CTIME);
30535f786787SKent Overstreet 	}
305474163da7SKent Overstreet 	mutex_unlock(&inode->ei_update_lock);
30551c6fdbd8SKent Overstreet 	return ret;
30561c6fdbd8SKent Overstreet }
30571c6fdbd8SKent Overstreet 
3058694015c2SKent Overstreet static int __bchfs_fallocate(struct bch_inode_info *inode, int mode,
3059694015c2SKent Overstreet 			     u64 start_sector, u64 end_sector)
30601c6fdbd8SKent Overstreet {
30611c6fdbd8SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
3062190fa7afSKent Overstreet 	struct btree_trans trans;
306367e0dd8fSKent Overstreet 	struct btree_iter iter;
3064694015c2SKent Overstreet 	struct bpos end_pos = POS(inode->v.i_ino, end_sector);
306501ad6737SKent Overstreet 	struct bch_io_opts opts;
3066694015c2SKent Overstreet 	int ret = 0;
30671c6fdbd8SKent Overstreet 
306801ad6737SKent Overstreet 	bch2_inode_opts_get(&opts, c, &inode->ei_inode);
3069f7beb4caSKent Overstreet 	bch2_trans_init(&trans, c, BTREE_ITER_MAX, 512);
30701c6fdbd8SKent Overstreet 
307167e0dd8fSKent Overstreet 	bch2_trans_iter_init(&trans, &iter, BTREE_ID_extents,
3072694015c2SKent Overstreet 			POS(inode->v.i_ino, start_sector),
3073190fa7afSKent Overstreet 			BTREE_ITER_SLOTS|BTREE_ITER_INTENT);
30741c6fdbd8SKent Overstreet 
3075e88a75ebSKent Overstreet 	while (!ret && bkey_lt(iter.pos, end_pos)) {
30762e87eae1SKent Overstreet 		s64 i_sectors_delta = 0;
30771c6fdbd8SKent Overstreet 		struct disk_reservation disk_res = { 0 };
3078190fa7afSKent Overstreet 		struct quota_res quota_res = { 0 };
30791c6fdbd8SKent Overstreet 		struct bkey_i_reservation reservation;
30801c6fdbd8SKent Overstreet 		struct bkey_s_c k;
3081694015c2SKent Overstreet 		unsigned sectors;
30826fed42bbSKent Overstreet 		u32 snapshot;
30831c6fdbd8SKent Overstreet 
3084163e885aSKent Overstreet 		bch2_trans_begin(&trans);
3085a8abd3a7SKent Overstreet 
30866fed42bbSKent Overstreet 		ret = bch2_subvolume_get_snapshot(&trans,
30876fed42bbSKent Overstreet 					inode->ei_subvol, &snapshot);
30886fed42bbSKent Overstreet 		if (ret)
30896fed42bbSKent Overstreet 			goto bkey_err;
30906fed42bbSKent Overstreet 
30916fed42bbSKent Overstreet 		bch2_btree_iter_set_snapshot(&iter, snapshot);
30926fed42bbSKent Overstreet 
309367e0dd8fSKent Overstreet 		k = bch2_btree_iter_peek_slot(&iter);
30940f238367SKent Overstreet 		if ((ret = bkey_err(k)))
30950f238367SKent Overstreet 			goto bkey_err;
30961c6fdbd8SKent Overstreet 
30971c6fdbd8SKent Overstreet 		/* already reserved */
309826609b61SKent Overstreet 		if (k.k->type == KEY_TYPE_reservation &&
309901ad6737SKent Overstreet 		    bkey_s_c_to_reservation(k).v->nr_replicas >= opts.data_replicas) {
310067e0dd8fSKent Overstreet 			bch2_btree_iter_advance(&iter);
31011c6fdbd8SKent Overstreet 			continue;
31021c6fdbd8SKent Overstreet 		}
31031c6fdbd8SKent Overstreet 
3104190fa7afSKent Overstreet 		if (bkey_extent_is_data(k.k) &&
3105190fa7afSKent Overstreet 		    !(mode & FALLOC_FL_ZERO_RANGE)) {
310667e0dd8fSKent Overstreet 			bch2_btree_iter_advance(&iter);
31071c6fdbd8SKent Overstreet 			continue;
31081c6fdbd8SKent Overstreet 		}
31091c6fdbd8SKent Overstreet 
31101c6fdbd8SKent Overstreet 		bkey_reservation_init(&reservation.k_i);
311126609b61SKent Overstreet 		reservation.k.type	= KEY_TYPE_reservation;
31121c6fdbd8SKent Overstreet 		reservation.k.p		= k.k->p;
31131c6fdbd8SKent Overstreet 		reservation.k.size	= k.k->size;
31141c6fdbd8SKent Overstreet 
311567e0dd8fSKent Overstreet 		bch2_cut_front(iter.pos,	&reservation.k_i);
3116085ab693SKent Overstreet 		bch2_cut_back(end_pos,		&reservation.k_i);
31171c6fdbd8SKent Overstreet 
31181c6fdbd8SKent Overstreet 		sectors = reservation.k.size;
31194de77495SKent Overstreet 		reservation.v.nr_replicas = bch2_bkey_nr_ptrs_allocated(k);
31201c6fdbd8SKent Overstreet 
31211c6fdbd8SKent Overstreet 		if (!bkey_extent_is_allocation(k.k)) {
31221c6fdbd8SKent Overstreet 			ret = bch2_quota_reservation_add(c, inode,
3123190fa7afSKent Overstreet 					&quota_res,
31241c6fdbd8SKent Overstreet 					sectors, true);
31251c6fdbd8SKent Overstreet 			if (unlikely(ret))
31260f238367SKent Overstreet 				goto bkey_err;
31271c6fdbd8SKent Overstreet 		}
31281c6fdbd8SKent Overstreet 
312901ad6737SKent Overstreet 		if (reservation.v.nr_replicas < opts.data_replicas ||
31304de77495SKent Overstreet 		    bch2_bkey_sectors_compressed(k)) {
31311c6fdbd8SKent Overstreet 			ret = bch2_disk_reservation_get(c, &disk_res, sectors,
313201ad6737SKent Overstreet 							opts.data_replicas, 0);
31331c6fdbd8SKent Overstreet 			if (unlikely(ret))
31340f238367SKent Overstreet 				goto bkey_err;
31351c6fdbd8SKent Overstreet 
31361c6fdbd8SKent Overstreet 			reservation.v.nr_replicas = disk_res.nr_replicas;
31371c6fdbd8SKent Overstreet 		}
31381c6fdbd8SKent Overstreet 
31398c6d298aSKent Overstreet 		ret = bch2_extent_update(&trans, inode_inum(inode), &iter,
3140a1ee777bSKent Overstreet 				&reservation.k_i, &disk_res,
3141a6336910SKent Overstreet 				0, &i_sectors_delta, true);
31428810386fSKent Overstreet 		if (ret)
31438810386fSKent Overstreet 			goto bkey_err;
31442e87eae1SKent Overstreet 		i_sectors_acct(c, inode, &quota_res, i_sectors_delta);
31450f238367SKent Overstreet bkey_err:
3146190fa7afSKent Overstreet 		bch2_quota_reservation_put(c, inode, &quota_res);
31471c6fdbd8SKent Overstreet 		bch2_disk_reservation_put(c, &disk_res);
3148549d173cSKent Overstreet 		if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
31491c6fdbd8SKent Overstreet 			ret = 0;
315050dc0f69SKent Overstreet 	}
315174163da7SKent Overstreet 
3152dcfc593fSKent Overstreet 	bch2_trans_unlock(&trans); /* lock ordering, before taking pagecache locks: */
3153dcfc593fSKent Overstreet 	mark_pagecache_reserved(inode, start_sector, iter.pos.offset);
3154dcfc593fSKent Overstreet 
3155098ef98dSKent Overstreet 	if (bch2_err_matches(ret, ENOSPC) && (mode & FALLOC_FL_ZERO_RANGE)) {
315674163da7SKent Overstreet 		struct quota_res quota_res = { 0 };
315774163da7SKent Overstreet 		s64 i_sectors_delta = 0;
315874163da7SKent Overstreet 
315974163da7SKent Overstreet 		bch2_fpunch_at(&trans, &iter, inode_inum(inode),
316074163da7SKent Overstreet 			       end_sector, &i_sectors_delta);
316174163da7SKent Overstreet 		i_sectors_acct(c, inode, &quota_res, i_sectors_delta);
316274163da7SKent Overstreet 		bch2_quota_reservation_put(c, inode, &quota_res);
316374163da7SKent Overstreet 	}
316474163da7SKent Overstreet 
316567e0dd8fSKent Overstreet 	bch2_trans_iter_exit(&trans, &iter);
3166694015c2SKent Overstreet 	bch2_trans_exit(&trans);
3167694015c2SKent Overstreet 	return ret;
3168694015c2SKent Overstreet }
316950dc0f69SKent Overstreet 
3170694015c2SKent Overstreet static long bchfs_fallocate(struct bch_inode_info *inode, int mode,
3171694015c2SKent Overstreet 			    loff_t offset, loff_t len)
3172694015c2SKent Overstreet {
3173694015c2SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
317474163da7SKent Overstreet 	u64 end		= offset + len;
317574163da7SKent Overstreet 	u64 block_start	= round_down(offset,	block_bytes(c));
317674163da7SKent Overstreet 	u64 block_end	= round_up(end,		block_bytes(c));
317774163da7SKent Overstreet 	bool truncated_last_page = false;
317874163da7SKent Overstreet 	int ret, ret2 = 0;
3179694015c2SKent Overstreet 
3180694015c2SKent Overstreet 	if (!(mode & FALLOC_FL_KEEP_SIZE) && end > inode->v.i_size) {
3181694015c2SKent Overstreet 		ret = inode_newsize_ok(&inode->v, end);
3182694015c2SKent Overstreet 		if (ret)
318374163da7SKent Overstreet 			return ret;
3184694015c2SKent Overstreet 	}
3185694015c2SKent Overstreet 
3186694015c2SKent Overstreet 	if (mode & FALLOC_FL_ZERO_RANGE) {
318774163da7SKent Overstreet 		ret = bch2_truncate_pages(inode, offset, end);
318874163da7SKent Overstreet 		if (unlikely(ret < 0))
318974163da7SKent Overstreet 			return ret;
3190694015c2SKent Overstreet 
319174163da7SKent Overstreet 		truncated_last_page = ret;
3192694015c2SKent Overstreet 
3193694015c2SKent Overstreet 		truncate_pagecache_range(&inode->v, offset, end - 1);
319474163da7SKent Overstreet 
319574163da7SKent Overstreet 		block_start	= round_up(offset,	block_bytes(c));
319674163da7SKent Overstreet 		block_end	= round_down(end,	block_bytes(c));
3197694015c2SKent Overstreet 	}
3198694015c2SKent Overstreet 
3199694015c2SKent Overstreet 	ret = __bchfs_fallocate(inode, mode, block_start >> 9, block_end >> 9);
3200e0541a93SKent Overstreet 
3201e0541a93SKent Overstreet 	/*
320274163da7SKent Overstreet 	 * On -ENOSPC in ZERO_RANGE mode, we still want to do the inode update,
320374163da7SKent Overstreet 	 * so that the VFS cache i_size is consistent with the btree i_size:
3204e0541a93SKent Overstreet 	 */
320574163da7SKent Overstreet 	if (ret &&
3206098ef98dSKent Overstreet 	    !(bch2_err_matches(ret, ENOSPC) && (mode & FALLOC_FL_ZERO_RANGE)))
320774163da7SKent Overstreet 		return ret;
32081c6fdbd8SKent Overstreet 
320974163da7SKent Overstreet 	if (mode & FALLOC_FL_KEEP_SIZE && end > inode->v.i_size)
3210e0541a93SKent Overstreet 		end = inode->v.i_size;
321174163da7SKent Overstreet 
321274163da7SKent Overstreet 	if (end >= inode->v.i_size &&
321374163da7SKent Overstreet 	    (((mode & FALLOC_FL_ZERO_RANGE) && !truncated_last_page) ||
321474163da7SKent Overstreet 	     !(mode & FALLOC_FL_KEEP_SIZE))) {
321574163da7SKent Overstreet 		spin_lock(&inode->v.i_lock);
3216e0541a93SKent Overstreet 		i_size_write(&inode->v, end);
321774163da7SKent Overstreet 		spin_unlock(&inode->v.i_lock);
3218e0541a93SKent Overstreet 
32191c6fdbd8SKent Overstreet 		mutex_lock(&inode->ei_update_lock);
322074163da7SKent Overstreet 		ret2 = bch2_write_inode_size(c, inode, end, 0);
32211c6fdbd8SKent Overstreet 		mutex_unlock(&inode->ei_update_lock);
32221c6fdbd8SKent Overstreet 	}
322374163da7SKent Overstreet 
322474163da7SKent Overstreet 	return ret ?: ret2;
32251c6fdbd8SKent Overstreet }
32261c6fdbd8SKent Overstreet 
32271c6fdbd8SKent Overstreet long bch2_fallocate_dispatch(struct file *file, int mode,
32281c6fdbd8SKent Overstreet 			     loff_t offset, loff_t len)
32291c6fdbd8SKent Overstreet {
32301c6fdbd8SKent Overstreet 	struct bch_inode_info *inode = file_bch_inode(file);
32312a9101a9SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
32322a9101a9SKent Overstreet 	long ret;
32332a9101a9SKent Overstreet 
3234*d94189adSKent Overstreet 	if (!bch2_write_ref_tryget(c, BCH_WRITE_REF_fallocate))
32352a9101a9SKent Overstreet 		return -EROFS;
32361c6fdbd8SKent Overstreet 
323774163da7SKent Overstreet 	inode_lock(&inode->v);
323874163da7SKent Overstreet 	inode_dio_wait(&inode->v);
3239a7ecd30cSKent Overstreet 	bch2_pagecache_block_get(inode);
324074163da7SKent Overstreet 
324107bfcc0bSKent Overstreet 	ret = file_modified(file);
324207bfcc0bSKent Overstreet 	if (ret)
324307bfcc0bSKent Overstreet 		goto err;
324407bfcc0bSKent Overstreet 
32451c6fdbd8SKent Overstreet 	if (!(mode & ~(FALLOC_FL_KEEP_SIZE|FALLOC_FL_ZERO_RANGE)))
32462a9101a9SKent Overstreet 		ret = bchfs_fallocate(inode, mode, offset, len);
32472a9101a9SKent Overstreet 	else if (mode == (FALLOC_FL_PUNCH_HOLE|FALLOC_FL_KEEP_SIZE))
32482a9101a9SKent Overstreet 		ret = bchfs_fpunch(inode, offset, len);
32492a9101a9SKent Overstreet 	else if (mode == FALLOC_FL_INSERT_RANGE)
32502a9101a9SKent Overstreet 		ret = bchfs_fcollapse_finsert(inode, offset, len, true);
32512a9101a9SKent Overstreet 	else if (mode == FALLOC_FL_COLLAPSE_RANGE)
32522a9101a9SKent Overstreet 		ret = bchfs_fcollapse_finsert(inode, offset, len, false);
32532a9101a9SKent Overstreet 	else
32542a9101a9SKent Overstreet 		ret = -EOPNOTSUPP;
325507bfcc0bSKent Overstreet err:
3256a7ecd30cSKent Overstreet 	bch2_pagecache_block_put(inode);
325774163da7SKent Overstreet 	inode_unlock(&inode->v);
3258*d94189adSKent Overstreet 	bch2_write_ref_put(c, BCH_WRITE_REF_fallocate);
32591c6fdbd8SKent Overstreet 
32605c1ef830SKent Overstreet 	return bch2_err_class(ret);
32611c6fdbd8SKent Overstreet }
32621c6fdbd8SKent Overstreet 
3263c72f687aSKent Overstreet /*
3264c72f687aSKent Overstreet  * Take a quota reservation for unallocated blocks in a given file range
3265c72f687aSKent Overstreet  * Does not check pagecache
3266c72f687aSKent Overstreet  */
3267e8540e56SKent Overstreet static int quota_reserve_range(struct bch_inode_info *inode,
3268e8540e56SKent Overstreet 			       struct quota_res *res,
3269e8540e56SKent Overstreet 			       u64 start, u64 end)
3270e8540e56SKent Overstreet {
3271e8540e56SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
3272e8540e56SKent Overstreet 	struct btree_trans trans;
3273e8540e56SKent Overstreet 	struct btree_iter iter;
3274e8540e56SKent Overstreet 	struct bkey_s_c k;
3275e8540e56SKent Overstreet 	u32 snapshot;
3276e8540e56SKent Overstreet 	u64 sectors = end - start;
3277e8540e56SKent Overstreet 	u64 pos = start;
3278e8540e56SKent Overstreet 	int ret;
3279e8540e56SKent Overstreet 
3280e8540e56SKent Overstreet 	bch2_trans_init(&trans, c, 0, 0);
3281e8540e56SKent Overstreet retry:
3282e8540e56SKent Overstreet 	bch2_trans_begin(&trans);
3283e8540e56SKent Overstreet 
3284e8540e56SKent Overstreet 	ret = bch2_subvolume_get_snapshot(&trans, inode->ei_subvol, &snapshot);
3285e8540e56SKent Overstreet 	if (ret)
3286e8540e56SKent Overstreet 		goto err;
3287e8540e56SKent Overstreet 
3288e8540e56SKent Overstreet 	bch2_trans_iter_init(&trans, &iter, BTREE_ID_extents,
3289e8540e56SKent Overstreet 			     SPOS(inode->v.i_ino, pos, snapshot), 0);
3290e8540e56SKent Overstreet 
3291e8540e56SKent Overstreet 	while (!(ret = btree_trans_too_many_iters(&trans)) &&
3292e8540e56SKent Overstreet 	       (k = bch2_btree_iter_peek_upto(&iter, POS(inode->v.i_ino, end - 1))).k &&
3293e8540e56SKent Overstreet 	       !(ret = bkey_err(k))) {
3294e8540e56SKent Overstreet 		if (bkey_extent_is_allocation(k.k)) {
3295e8540e56SKent Overstreet 			u64 s = min(end, k.k->p.offset) -
3296e8540e56SKent Overstreet 				max(start, bkey_start_offset(k.k));
3297e8540e56SKent Overstreet 			BUG_ON(s > sectors);
3298e8540e56SKent Overstreet 			sectors -= s;
3299e8540e56SKent Overstreet 		}
3300e8540e56SKent Overstreet 		bch2_btree_iter_advance(&iter);
3301e8540e56SKent Overstreet 	}
3302e8540e56SKent Overstreet 	pos = iter.pos.offset;
3303e8540e56SKent Overstreet 	bch2_trans_iter_exit(&trans, &iter);
3304e8540e56SKent Overstreet err:
3305e8540e56SKent Overstreet 	if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
3306e8540e56SKent Overstreet 		goto retry;
3307e8540e56SKent Overstreet 
3308e8540e56SKent Overstreet 	bch2_trans_exit(&trans);
3309e8540e56SKent Overstreet 
3310e8540e56SKent Overstreet 	if (ret)
3311e8540e56SKent Overstreet 		return ret;
3312e8540e56SKent Overstreet 
3313e8540e56SKent Overstreet 	return bch2_quota_reservation_add(c, inode, res, sectors, true);
3314e8540e56SKent Overstreet }
3315e8540e56SKent Overstreet 
331676426098SKent Overstreet loff_t bch2_remap_file_range(struct file *file_src, loff_t pos_src,
331776426098SKent Overstreet 			     struct file *file_dst, loff_t pos_dst,
331876426098SKent Overstreet 			     loff_t len, unsigned remap_flags)
331976426098SKent Overstreet {
332076426098SKent Overstreet 	struct bch_inode_info *src = file_bch_inode(file_src);
332176426098SKent Overstreet 	struct bch_inode_info *dst = file_bch_inode(file_dst);
332276426098SKent Overstreet 	struct bch_fs *c = src->v.i_sb->s_fs_info;
3323e8540e56SKent Overstreet 	struct quota_res quota_res = { 0 };
33242e87eae1SKent Overstreet 	s64 i_sectors_delta = 0;
3325677fc056SKent Overstreet 	u64 aligned_len;
332676426098SKent Overstreet 	loff_t ret = 0;
332776426098SKent Overstreet 
332876426098SKent Overstreet 	if (remap_flags & ~(REMAP_FILE_DEDUP|REMAP_FILE_ADVISORY))
332976426098SKent Overstreet 		return -EINVAL;
333076426098SKent Overstreet 
333176426098SKent Overstreet 	if (remap_flags & REMAP_FILE_DEDUP)
333276426098SKent Overstreet 		return -EOPNOTSUPP;
333376426098SKent Overstreet 
333476426098SKent Overstreet 	if ((pos_src & (block_bytes(c) - 1)) ||
333576426098SKent Overstreet 	    (pos_dst & (block_bytes(c) - 1)))
333676426098SKent Overstreet 		return -EINVAL;
333776426098SKent Overstreet 
333876426098SKent Overstreet 	if (src == dst &&
333976426098SKent Overstreet 	    abs(pos_src - pos_dst) < len)
334076426098SKent Overstreet 		return -EINVAL;
334176426098SKent Overstreet 
334276426098SKent Overstreet 	bch2_lock_inodes(INODE_LOCK|INODE_PAGECACHE_BLOCK, src, dst);
334376426098SKent Overstreet 
334476426098SKent Overstreet 	inode_dio_wait(&src->v);
334576426098SKent Overstreet 	inode_dio_wait(&dst->v);
334676426098SKent Overstreet 
334776426098SKent Overstreet 	ret = generic_remap_file_range_prep(file_src, pos_src,
334876426098SKent Overstreet 					    file_dst, pos_dst,
334976426098SKent Overstreet 					    &len, remap_flags);
335076426098SKent Overstreet 	if (ret < 0 || len == 0)
33512e87eae1SKent Overstreet 		goto err;
335276426098SKent Overstreet 
3353677fc056SKent Overstreet 	aligned_len = round_up((u64) len, block_bytes(c));
335476426098SKent Overstreet 
335576426098SKent Overstreet 	ret = write_invalidate_inode_pages_range(dst->v.i_mapping,
3356677fc056SKent Overstreet 				pos_dst, pos_dst + len - 1);
335776426098SKent Overstreet 	if (ret)
33582e87eae1SKent Overstreet 		goto err;
335976426098SKent Overstreet 
3360e8540e56SKent Overstreet 	ret = quota_reserve_range(dst, &quota_res, pos_dst >> 9,
3361e8540e56SKent Overstreet 				  (pos_dst + aligned_len) >> 9);
3362e8540e56SKent Overstreet 	if (ret)
3363e8540e56SKent Overstreet 		goto err;
3364e8540e56SKent Overstreet 
3365e8540e56SKent Overstreet 	file_update_time(file_dst);
3366e8540e56SKent Overstreet 
3367dcfc593fSKent Overstreet 	mark_pagecache_unallocated(src, pos_src >> 9,
3368dcfc593fSKent Overstreet 				   (pos_src + aligned_len) >> 9);
336976426098SKent Overstreet 
33702e87eae1SKent Overstreet 	ret = bch2_remap_range(c,
33716fed42bbSKent Overstreet 			       inode_inum(dst), pos_dst >> 9,
33726fed42bbSKent Overstreet 			       inode_inum(src), pos_src >> 9,
337376426098SKent Overstreet 			       aligned_len >> 9,
33742e87eae1SKent Overstreet 			       pos_dst + len, &i_sectors_delta);
33752e87eae1SKent Overstreet 	if (ret < 0)
33762e87eae1SKent Overstreet 		goto err;
337776426098SKent Overstreet 
33782e87eae1SKent Overstreet 	/*
33792e87eae1SKent Overstreet 	 * due to alignment, we might have remapped slightly more than requsted
33802e87eae1SKent Overstreet 	 */
3381677fc056SKent Overstreet 	ret = min((u64) ret << 9, (u64) len);
33822e87eae1SKent Overstreet 
3383e8540e56SKent Overstreet 	i_sectors_acct(c, dst, &quota_res, i_sectors_delta);
33842e87eae1SKent Overstreet 
33852e87eae1SKent Overstreet 	spin_lock(&dst->v.i_lock);
3386677fc056SKent Overstreet 	if (pos_dst + ret > dst->v.i_size)
3387677fc056SKent Overstreet 		i_size_write(&dst->v, pos_dst + ret);
33882e87eae1SKent Overstreet 	spin_unlock(&dst->v.i_lock);
3389e7084c9cSKent Overstreet 
339068a2054dSKent Overstreet 	if ((file_dst->f_flags & (__O_SYNC | O_DSYNC)) ||
339168a2054dSKent Overstreet 	    IS_SYNC(file_inode(file_dst)))
339268a2054dSKent Overstreet 		ret = bch2_flush_inode(c, inode_inum(dst));
33932e87eae1SKent Overstreet err:
3394e8540e56SKent Overstreet 	bch2_quota_reservation_put(c, dst, &quota_res);
339576426098SKent Overstreet 	bch2_unlock_inodes(INODE_LOCK|INODE_PAGECACHE_BLOCK, src, dst);
339676426098SKent Overstreet 
33975c1ef830SKent Overstreet 	return bch2_err_class(ret);
339876426098SKent Overstreet }
339976426098SKent Overstreet 
34001c6fdbd8SKent Overstreet /* fseek: */
34011c6fdbd8SKent Overstreet 
3402543ef2ebSKent Overstreet static int folio_data_offset(struct folio *folio, unsigned offset)
34031c6fdbd8SKent Overstreet {
3404f57a6a5dSKent Overstreet 	struct bch_page_state *s = bch2_page_state(&folio->page);
3405f57a6a5dSKent Overstreet 	unsigned i;
3406f81b648dSKent Overstreet 
3407543ef2ebSKent Overstreet 	if (s)
3408543ef2ebSKent Overstreet 		for (i = offset >> 9; i < PAGE_SECTORS; i++)
3409f57a6a5dSKent Overstreet 			if (s->s[i].state >= SECTOR_DIRTY)
3410543ef2ebSKent Overstreet 				return i << 9;
3411f57a6a5dSKent Overstreet 
3412543ef2ebSKent Overstreet 	return -1;
34131c6fdbd8SKent Overstreet }
34141c6fdbd8SKent Overstreet 
3415543ef2ebSKent Overstreet static loff_t bch2_seek_pagecache_data(struct inode *vinode,
34161c6fdbd8SKent Overstreet 				       loff_t start_offset,
34171c6fdbd8SKent Overstreet 				       loff_t end_offset)
34181c6fdbd8SKent Overstreet {
34191c6fdbd8SKent Overstreet 	struct folio_batch fbatch;
34201c6fdbd8SKent Overstreet 	pgoff_t start_index	= start_offset >> PAGE_SHIFT;
34211c6fdbd8SKent Overstreet 	pgoff_t end_index	= end_offset >> PAGE_SHIFT;
34221c6fdbd8SKent Overstreet 	pgoff_t index		= start_index;
34231c6fdbd8SKent Overstreet 	unsigned i;
3424543ef2ebSKent Overstreet 	loff_t ret;
3425543ef2ebSKent Overstreet 	int offset;
34261c6fdbd8SKent Overstreet 
34271c6fdbd8SKent Overstreet 	folio_batch_init(&fbatch);
34281c6fdbd8SKent Overstreet 
34291c6fdbd8SKent Overstreet 	while (filemap_get_folios(vinode->i_mapping,
34301c6fdbd8SKent Overstreet 				  &index, end_index, &fbatch)) {
34311c6fdbd8SKent Overstreet 		for (i = 0; i < folio_batch_count(&fbatch); i++) {
34321c6fdbd8SKent Overstreet 			struct folio *folio = fbatch.folios[i];
34331c6fdbd8SKent Overstreet 
34341c6fdbd8SKent Overstreet 			folio_lock(folio);
3435543ef2ebSKent Overstreet 			offset = folio_data_offset(folio,
3436543ef2ebSKent Overstreet 					folio->index == start_index
3437543ef2ebSKent Overstreet 					? start_offset & (PAGE_SIZE - 1)
3438543ef2ebSKent Overstreet 					: 0);
3439543ef2ebSKent Overstreet 			if (offset >= 0) {
3440543ef2ebSKent Overstreet 				ret = clamp(((loff_t) folio->index << PAGE_SHIFT) +
3441543ef2ebSKent Overstreet 					    offset,
3442543ef2ebSKent Overstreet 					    start_offset, end_offset);
34431c6fdbd8SKent Overstreet 				folio_unlock(folio);
34441c6fdbd8SKent Overstreet 				folio_batch_release(&fbatch);
3445543ef2ebSKent Overstreet 				return ret;
34461c6fdbd8SKent Overstreet 			}
34471c6fdbd8SKent Overstreet 			folio_unlock(folio);
34481c6fdbd8SKent Overstreet 		}
34491c6fdbd8SKent Overstreet 		folio_batch_release(&fbatch);
34501c6fdbd8SKent Overstreet 		cond_resched();
34511c6fdbd8SKent Overstreet 	}
34521c6fdbd8SKent Overstreet 
34531c6fdbd8SKent Overstreet 	return end_offset;
34541c6fdbd8SKent Overstreet }
34551c6fdbd8SKent Overstreet 
34561c6fdbd8SKent Overstreet static loff_t bch2_seek_data(struct file *file, u64 offset)
34571c6fdbd8SKent Overstreet {
34581c6fdbd8SKent Overstreet 	struct bch_inode_info *inode = file_bch_inode(file);
34591c6fdbd8SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
3460424eb881SKent Overstreet 	struct btree_trans trans;
346167e0dd8fSKent Overstreet 	struct btree_iter iter;
34621c6fdbd8SKent Overstreet 	struct bkey_s_c k;
34636fed42bbSKent Overstreet 	subvol_inum inum = inode_inum(inode);
34641c6fdbd8SKent Overstreet 	u64 isize, next_data = MAX_LFS_FILESIZE;
34656fed42bbSKent Overstreet 	u32 snapshot;
34661c6fdbd8SKent Overstreet 	int ret;
34671c6fdbd8SKent Overstreet 
34681c6fdbd8SKent Overstreet 	isize = i_size_read(&inode->v);
34691c6fdbd8SKent Overstreet 	if (offset >= isize)
34701c6fdbd8SKent Overstreet 		return -ENXIO;
34711c6fdbd8SKent Overstreet 
347220bceecbSKent Overstreet 	bch2_trans_init(&trans, c, 0, 0);
34736fed42bbSKent Overstreet retry:
34746fed42bbSKent Overstreet 	bch2_trans_begin(&trans);
34756fed42bbSKent Overstreet 
34766fed42bbSKent Overstreet 	ret = bch2_subvolume_get_snapshot(&trans, inum.subvol, &snapshot);
34776fed42bbSKent Overstreet 	if (ret)
34786fed42bbSKent Overstreet 		goto err;
3479424eb881SKent Overstreet 
3480c72f687aSKent Overstreet 	for_each_btree_key_upto_norestart(&trans, iter, BTREE_ID_extents,
3481c72f687aSKent Overstreet 			   SPOS(inode->v.i_ino, offset >> 9, snapshot),
3482c72f687aSKent Overstreet 			   POS(inode->v.i_ino, U64_MAX),
3483c72f687aSKent Overstreet 			   0, k, ret) {
3484c72f687aSKent Overstreet 		if (bkey_extent_is_data(k.k)) {
34851c6fdbd8SKent Overstreet 			next_data = max(offset, bkey_start_offset(k.k) << 9);
34861c6fdbd8SKent Overstreet 			break;
34871c6fdbd8SKent Overstreet 		} else if (k.k->p.offset >> 9 > isize)
34881c6fdbd8SKent Overstreet 			break;
34891c6fdbd8SKent Overstreet 	}
349067e0dd8fSKent Overstreet 	bch2_trans_iter_exit(&trans, &iter);
34916fed42bbSKent Overstreet err:
3492549d173cSKent Overstreet 	if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
34936fed42bbSKent Overstreet 		goto retry;
34941c6fdbd8SKent Overstreet 
34959a796fdbSKent Overstreet 	bch2_trans_exit(&trans);
34961c6fdbd8SKent Overstreet 	if (ret)
34971c6fdbd8SKent Overstreet 		return ret;
34981c6fdbd8SKent Overstreet 
34991c6fdbd8SKent Overstreet 	if (next_data > offset)
3500543ef2ebSKent Overstreet 		next_data = bch2_seek_pagecache_data(&inode->v,
35011c6fdbd8SKent Overstreet 						     offset, next_data);
35021c6fdbd8SKent Overstreet 
3503e10d3094SKent Overstreet 	if (next_data >= isize)
35041c6fdbd8SKent Overstreet 		return -ENXIO;
35051c6fdbd8SKent Overstreet 
35061c6fdbd8SKent Overstreet 	return vfs_setpos(file, next_data, MAX_LFS_FILESIZE);
35071c6fdbd8SKent Overstreet }
35081c6fdbd8SKent Overstreet 
3509543ef2ebSKent Overstreet static int __page_hole_offset(struct page *page, unsigned offset)
35101c6fdbd8SKent Overstreet {
3511543ef2ebSKent Overstreet 	struct bch_page_state *s = bch2_page_state(page);
3512543ef2ebSKent Overstreet 	unsigned i;
3513543ef2ebSKent Overstreet 
3514543ef2ebSKent Overstreet 	if (!s)
3515543ef2ebSKent Overstreet 		return 0;
3516543ef2ebSKent Overstreet 
3517543ef2ebSKent Overstreet 	for (i = offset >> 9; i < PAGE_SECTORS; i++)
3518543ef2ebSKent Overstreet 		if (s->s[i].state < SECTOR_DIRTY)
3519543ef2ebSKent Overstreet 			return i << 9;
3520543ef2ebSKent Overstreet 
3521543ef2ebSKent Overstreet 	return -1;
3522543ef2ebSKent Overstreet }
3523543ef2ebSKent Overstreet 
3524543ef2ebSKent Overstreet static loff_t page_hole_offset(struct address_space *mapping, loff_t offset)
3525543ef2ebSKent Overstreet {
3526543ef2ebSKent Overstreet 	pgoff_t index = offset >> PAGE_SHIFT;
35271c6fdbd8SKent Overstreet 	struct page *page;
3528543ef2ebSKent Overstreet 	int pg_offset;
3529543ef2ebSKent Overstreet 	loff_t ret = -1;
35301c6fdbd8SKent Overstreet 
35311c6fdbd8SKent Overstreet 	page = find_lock_page(mapping, index);
35321c6fdbd8SKent Overstreet 	if (!page)
3533543ef2ebSKent Overstreet 		return offset;
35341c6fdbd8SKent Overstreet 
3535543ef2ebSKent Overstreet 	pg_offset = __page_hole_offset(page, offset & (PAGE_SIZE - 1));
3536543ef2ebSKent Overstreet 	if (pg_offset >= 0)
3537543ef2ebSKent Overstreet 		ret = ((loff_t) index << PAGE_SHIFT) + pg_offset;
3538543ef2ebSKent Overstreet 
35391c6fdbd8SKent Overstreet 	unlock_page(page);
35401c6fdbd8SKent Overstreet 
35411c6fdbd8SKent Overstreet 	return ret;
35421c6fdbd8SKent Overstreet }
35431c6fdbd8SKent Overstreet 
3544543ef2ebSKent Overstreet static loff_t bch2_seek_pagecache_hole(struct inode *vinode,
35451c6fdbd8SKent Overstreet 				       loff_t start_offset,
35461c6fdbd8SKent Overstreet 				       loff_t end_offset)
35471c6fdbd8SKent Overstreet {
35481c6fdbd8SKent Overstreet 	struct address_space *mapping = vinode->i_mapping;
3549543ef2ebSKent Overstreet 	loff_t offset = start_offset, hole;
35501c6fdbd8SKent Overstreet 
3551543ef2ebSKent Overstreet 	while (offset < end_offset) {
3552543ef2ebSKent Overstreet 		hole = page_hole_offset(mapping, offset);
3553543ef2ebSKent Overstreet 		if (hole >= 0 && hole <= end_offset)
3554543ef2ebSKent Overstreet 			return max(start_offset, hole);
3555543ef2ebSKent Overstreet 
3556543ef2ebSKent Overstreet 		offset += PAGE_SIZE;
3557543ef2ebSKent Overstreet 		offset &= PAGE_MASK;
3558543ef2ebSKent Overstreet 	}
35591c6fdbd8SKent Overstreet 
35601c6fdbd8SKent Overstreet 	return end_offset;
35611c6fdbd8SKent Overstreet }
35621c6fdbd8SKent Overstreet 
35631c6fdbd8SKent Overstreet static loff_t bch2_seek_hole(struct file *file, u64 offset)
35641c6fdbd8SKent Overstreet {
35651c6fdbd8SKent Overstreet 	struct bch_inode_info *inode = file_bch_inode(file);
35661c6fdbd8SKent Overstreet 	struct bch_fs *c = inode->v.i_sb->s_fs_info;
3567424eb881SKent Overstreet 	struct btree_trans trans;
356867e0dd8fSKent Overstreet 	struct btree_iter iter;
35691c6fdbd8SKent Overstreet 	struct bkey_s_c k;
35706fed42bbSKent Overstreet 	subvol_inum inum = inode_inum(inode);
35711c6fdbd8SKent Overstreet 	u64 isize, next_hole = MAX_LFS_FILESIZE;
35726fed42bbSKent Overstreet 	u32 snapshot;
35731c6fdbd8SKent Overstreet 	int ret;
35741c6fdbd8SKent Overstreet 
35751c6fdbd8SKent Overstreet 	isize = i_size_read(&inode->v);
35761c6fdbd8SKent Overstreet 	if (offset >= isize)
35771c6fdbd8SKent Overstreet 		return -ENXIO;
35781c6fdbd8SKent Overstreet 
357920bceecbSKent Overstreet 	bch2_trans_init(&trans, c, 0, 0);
35806fed42bbSKent Overstreet retry:
35816fed42bbSKent Overstreet 	bch2_trans_begin(&trans);
35826fed42bbSKent Overstreet 
35836fed42bbSKent Overstreet 	ret = bch2_subvolume_get_snapshot(&trans, inum.subvol, &snapshot);
35846fed42bbSKent Overstreet 	if (ret)
35856fed42bbSKent Overstreet 		goto err;
3586424eb881SKent Overstreet 
3587e5fa91d7SKent Overstreet 	for_each_btree_key_norestart(&trans, iter, BTREE_ID_extents,
35886fed42bbSKent Overstreet 			   SPOS(inode->v.i_ino, offset >> 9, snapshot),
358994f651e2SKent Overstreet 			   BTREE_ITER_SLOTS, k, ret) {
35901c6fdbd8SKent Overstreet 		if (k.k->p.inode != inode->v.i_ino) {
3591543ef2ebSKent Overstreet 			next_hole = bch2_seek_pagecache_hole(&inode->v,
35921c6fdbd8SKent Overstreet 					offset, MAX_LFS_FILESIZE);
35931c6fdbd8SKent Overstreet 			break;
35941c6fdbd8SKent Overstreet 		} else if (!bkey_extent_is_data(k.k)) {
3595543ef2ebSKent Overstreet 			next_hole = bch2_seek_pagecache_hole(&inode->v,
35961c6fdbd8SKent Overstreet 					max(offset, bkey_start_offset(k.k) << 9),
35971c6fdbd8SKent Overstreet 					k.k->p.offset << 9);
35981c6fdbd8SKent Overstreet 
35991c6fdbd8SKent Overstreet 			if (next_hole < k.k->p.offset << 9)
36001c6fdbd8SKent Overstreet 				break;
36011c6fdbd8SKent Overstreet 		} else {
36021c6fdbd8SKent Overstreet 			offset = max(offset, bkey_start_offset(k.k) << 9);
36031c6fdbd8SKent Overstreet 		}
36041c6fdbd8SKent Overstreet 	}
360567e0dd8fSKent Overstreet 	bch2_trans_iter_exit(&trans, &iter);
36066fed42bbSKent Overstreet err:
3607549d173cSKent Overstreet 	if (bch2_err_matches(ret, BCH_ERR_transaction_restart))
36086fed42bbSKent Overstreet 		goto retry;
36091c6fdbd8SKent Overstreet 
36109a796fdbSKent Overstreet 	bch2_trans_exit(&trans);
36111c6fdbd8SKent Overstreet 	if (ret)
36121c6fdbd8SKent Overstreet 		return ret;
36131c6fdbd8SKent Overstreet 
36141c6fdbd8SKent Overstreet 	if (next_hole > isize)
36151c6fdbd8SKent Overstreet 		next_hole = isize;
36161c6fdbd8SKent Overstreet 
36171c6fdbd8SKent Overstreet 	return vfs_setpos(file, next_hole, MAX_LFS_FILESIZE);
36181c6fdbd8SKent Overstreet }
36191c6fdbd8SKent Overstreet 
36201c6fdbd8SKent Overstreet loff_t bch2_llseek(struct file *file, loff_t offset, int whence)
36211c6fdbd8SKent Overstreet {
36225c1ef830SKent Overstreet 	loff_t ret;
36235c1ef830SKent Overstreet 
36241c6fdbd8SKent Overstreet 	switch (whence) {
36251c6fdbd8SKent Overstreet 	case SEEK_SET:
36261c6fdbd8SKent Overstreet 	case SEEK_CUR:
36271c6fdbd8SKent Overstreet 	case SEEK_END:
36285c1ef830SKent Overstreet 		ret = generic_file_llseek(file, offset, whence);
36295c1ef830SKent Overstreet 		break;
36301c6fdbd8SKent Overstreet 	case SEEK_DATA:
36315c1ef830SKent Overstreet 		ret = bch2_seek_data(file, offset);
36325c1ef830SKent Overstreet 		break;
36331c6fdbd8SKent Overstreet 	case SEEK_HOLE:
36345c1ef830SKent Overstreet 		ret = bch2_seek_hole(file, offset);
36355c1ef830SKent Overstreet 		break;
36365c1ef830SKent Overstreet 	default:
36375c1ef830SKent Overstreet 		ret = -EINVAL;
36385c1ef830SKent Overstreet 		break;
36391c6fdbd8SKent Overstreet 	}
36401c6fdbd8SKent Overstreet 
36415c1ef830SKent Overstreet 	return bch2_err_class(ret);
36421c6fdbd8SKent Overstreet }
36431c6fdbd8SKent Overstreet 
36441c6fdbd8SKent Overstreet void bch2_fs_fsio_exit(struct bch_fs *c)
36451c6fdbd8SKent Overstreet {
36461c6fdbd8SKent Overstreet 	bioset_exit(&c->dio_write_bioset);
36471c6fdbd8SKent Overstreet 	bioset_exit(&c->dio_read_bioset);
36481c6fdbd8SKent Overstreet 	bioset_exit(&c->writepage_bioset);
36491c6fdbd8SKent Overstreet }
36501c6fdbd8SKent Overstreet 
36511c6fdbd8SKent Overstreet int bch2_fs_fsio_init(struct bch_fs *c)
36521c6fdbd8SKent Overstreet {
36531c6fdbd8SKent Overstreet 	int ret = 0;
36541c6fdbd8SKent Overstreet 
36551c6fdbd8SKent Overstreet 	pr_verbose_init(c->opts, "");
36561c6fdbd8SKent Overstreet 
36571c6fdbd8SKent Overstreet 	if (bioset_init(&c->writepage_bioset,
36589a3df993SKent Overstreet 			4, offsetof(struct bch_writepage_io, op.wbio.bio),
36591c6fdbd8SKent Overstreet 			BIOSET_NEED_BVECS) ||
36601c6fdbd8SKent Overstreet 	    bioset_init(&c->dio_read_bioset,
36611c6fdbd8SKent Overstreet 			4, offsetof(struct dio_read, rbio.bio),
36621c6fdbd8SKent Overstreet 			BIOSET_NEED_BVECS) ||
36631c6fdbd8SKent Overstreet 	    bioset_init(&c->dio_write_bioset,
36649a3df993SKent Overstreet 			4, offsetof(struct dio_write, op.wbio.bio),
36651c6fdbd8SKent Overstreet 			BIOSET_NEED_BVECS))
36661c6fdbd8SKent Overstreet 		ret = -ENOMEM;
36671c6fdbd8SKent Overstreet 
36681c6fdbd8SKent Overstreet 	pr_verbose_init(c->opts, "ret %i", ret);
36691c6fdbd8SKent Overstreet 	return ret;
36701c6fdbd8SKent Overstreet }
36711c6fdbd8SKent Overstreet 
36721c6fdbd8SKent Overstreet #endif /* NO_BCACHEFS_FS */
3673